0
|
1 /*
|
|
2 * ConnectBot: simple, powerful, open-source SSH client for Android
|
|
3 * Copyright 2007 Kenny Root, Jeffrey Sharkey
|
|
4 *
|
|
5 * Licensed under the Apache License, Version 2.0 (the "License");
|
|
6 * you may not use this file except in compliance with the License.
|
|
7 * You may obtain a copy of the License at
|
|
8 *
|
|
9 * http://www.apache.org/licenses/LICENSE-2.0
|
|
10 *
|
|
11 * Unless required by applicable law or agreed to in writing, software
|
|
12 * distributed under the License is distributed on an "AS IS" BASIS,
|
|
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14 * See the License for the specific language governing permissions and
|
|
15 * limitations under the License.
|
|
16 */
|
|
17
|
|
18 package com.five_ten_sg.connectbot.bean;
|
|
19
|
|
20 import java.security.NoSuchAlgorithmException;
|
|
21 import java.security.PrivateKey;
|
|
22 import java.security.PublicKey;
|
|
23 import java.security.interfaces.ECPublicKey;
|
|
24 import java.security.interfaces.RSAPublicKey;
|
|
25 import java.security.spec.InvalidKeySpecException;
|
|
26
|
|
27 import com.five_ten_sg.connectbot.util.PubkeyDatabase;
|
|
28 import com.five_ten_sg.connectbot.util.PubkeyUtils;
|
|
29 import android.content.ContentValues;
|
|
30
|
|
31 /**
|
|
32 * @author Kenny Root
|
|
33 *
|
|
34 */
|
|
35 public class PubkeyBean extends AbstractBean {
|
|
36 public static final String BEAN_NAME = "pubkey";
|
|
37
|
|
38 private static final String KEY_TYPE_RSA = "RSA";
|
|
39
|
|
40 private static final String KEY_TYPE_DSA = "DSA";
|
|
41
|
|
42 private static final String KEY_TYPE_EC = "EC";
|
|
43
|
|
44 /* Database fields */
|
|
45 private long id;
|
|
46 private String nickname;
|
|
47 private String type;
|
|
48 private byte[] privateKey;
|
|
49 private byte[] publicKey;
|
|
50 private boolean encrypted = false;
|
|
51 private boolean startup = false;
|
|
52 private boolean confirmUse = false;
|
|
53 private int lifetime = 0;
|
|
54
|
|
55 /* Transient values */
|
|
56 private transient boolean unlocked = false;
|
|
57 private transient Object unlockedPrivate = null;
|
|
58 private transient String description;
|
|
59
|
|
60 @Override
|
|
61 public String getBeanName() {
|
|
62 return BEAN_NAME;
|
|
63 }
|
|
64
|
|
65 public void setId(long id) {
|
|
66 this.id = id;
|
|
67 }
|
|
68
|
|
69 public long getId() {
|
|
70 return id;
|
|
71 }
|
|
72
|
|
73 public void setNickname(String nickname) {
|
|
74 this.nickname = nickname;
|
|
75 }
|
|
76
|
|
77 public String getNickname() {
|
|
78 return nickname;
|
|
79 }
|
|
80
|
|
81 public void setType(String type) {
|
|
82 this.type = type;
|
|
83 }
|
|
84
|
|
85 public String getType() {
|
|
86 return type;
|
|
87 }
|
|
88
|
|
89 public void setPrivateKey(byte[] privateKey) {
|
|
90 if (privateKey == null)
|
|
91 this.privateKey = null;
|
|
92 else
|
|
93 this.privateKey = privateKey.clone();
|
|
94 }
|
|
95
|
|
96 public byte[] getPrivateKey() {
|
|
97 if (privateKey == null)
|
|
98 return null;
|
|
99 else
|
|
100 return privateKey.clone();
|
|
101 }
|
|
102
|
|
103 public void setPublicKey(byte[] encoded) {
|
|
104 if (encoded == null)
|
|
105 publicKey = null;
|
|
106 else
|
|
107 publicKey = encoded.clone();
|
|
108 }
|
|
109
|
|
110 public byte[] getPublicKey() {
|
|
111 if (publicKey == null)
|
|
112 return null;
|
|
113 else
|
|
114 return publicKey.clone();
|
|
115 }
|
|
116
|
|
117 public void setEncrypted(boolean encrypted) {
|
|
118 this.encrypted = encrypted;
|
|
119 }
|
|
120
|
|
121 public boolean isEncrypted() {
|
|
122 return encrypted;
|
|
123 }
|
|
124
|
|
125 public void setStartup(boolean startup) {
|
|
126 this.startup = startup;
|
|
127 }
|
|
128
|
|
129 public boolean isStartup() {
|
|
130 return startup;
|
|
131 }
|
|
132
|
|
133 public void setConfirmUse(boolean confirmUse) {
|
|
134 this.confirmUse = confirmUse;
|
|
135 }
|
|
136
|
|
137 public boolean isConfirmUse() {
|
|
138 return confirmUse;
|
|
139 }
|
|
140
|
|
141 public void setLifetime(int lifetime) {
|
|
142 this.lifetime = lifetime;
|
|
143 }
|
|
144
|
|
145 public int getLifetime() {
|
|
146 return lifetime;
|
|
147 }
|
|
148
|
|
149 public void setUnlocked(boolean unlocked) {
|
|
150 this.unlocked = unlocked;
|
|
151 }
|
|
152
|
|
153 public boolean isUnlocked() {
|
|
154 return unlocked;
|
|
155 }
|
|
156
|
|
157 public void setUnlockedPrivate(Object unlockedPrivate) {
|
|
158 this.unlockedPrivate = unlockedPrivate;
|
|
159 }
|
|
160
|
|
161 public Object getUnlockedPrivate() {
|
|
162 return unlockedPrivate;
|
|
163 }
|
|
164
|
|
165 public String getDescription() {
|
|
166 if (description == null) {
|
|
167 final StringBuilder sb = new StringBuilder();
|
|
168
|
|
169 try {
|
|
170 final PublicKey pubKey = PubkeyUtils.decodePublic(privateKey, type);
|
|
171
|
|
172 if (PubkeyDatabase.KEY_TYPE_RSA.equals(type)) {
|
|
173 int bits = ((RSAPublicKey) pubKey).getModulus().bitLength();
|
|
174 sb.append("RSA ");
|
|
175 sb.append(bits);
|
|
176 sb.append("-bit");
|
|
177 }
|
|
178 else if (PubkeyDatabase.KEY_TYPE_DSA.equals(type)) {
|
|
179 sb.append("DSA 1024-bit");
|
|
180 }
|
|
181 else if (PubkeyDatabase.KEY_TYPE_EC.equals(type)) {
|
|
182 int bits = ((ECPublicKey) pubKey).getParams().getCurve().getField()
|
|
183 .getFieldSize();
|
|
184 sb.append("EC ");
|
|
185 sb.append(bits);
|
|
186 sb.append("-bit");
|
|
187 }
|
|
188 else {
|
|
189 sb.append("Unknown Key Type");
|
|
190 }
|
|
191 }
|
|
192 catch (NoSuchAlgorithmException e) {
|
|
193 sb.append("Unknown Key Type");
|
|
194 }
|
|
195 catch (InvalidKeySpecException e) {
|
|
196 sb.append("Unknown Key Type");
|
|
197 }
|
|
198
|
|
199 if (encrypted) sb.append(" (encrypted)");
|
|
200
|
|
201 description = sb.toString();
|
|
202 }
|
|
203
|
|
204 return description;
|
|
205 }
|
|
206
|
|
207 /* (non-Javadoc)
|
|
208 * @see com.five_ten_sg.connectbot.bean.AbstractBean#getValues()
|
|
209 */
|
|
210 @Override
|
|
211 public ContentValues getValues() {
|
|
212 ContentValues values = new ContentValues();
|
|
213 values.put(PubkeyDatabase.FIELD_PUBKEY_NICKNAME, nickname);
|
|
214 values.put(PubkeyDatabase.FIELD_PUBKEY_TYPE, type);
|
|
215 values.put(PubkeyDatabase.FIELD_PUBKEY_PRIVATE, privateKey);
|
|
216 values.put(PubkeyDatabase.FIELD_PUBKEY_PUBLIC, publicKey);
|
|
217 values.put(PubkeyDatabase.FIELD_PUBKEY_ENCRYPTED, encrypted ? 1 : 0);
|
|
218 values.put(PubkeyDatabase.FIELD_PUBKEY_STARTUP, startup ? 1 : 0);
|
|
219 values.put(PubkeyDatabase.FIELD_PUBKEY_CONFIRMUSE, confirmUse ? 1 : 0);
|
|
220 values.put(PubkeyDatabase.FIELD_PUBKEY_LIFETIME, lifetime);
|
|
221 return values;
|
|
222 }
|
|
223
|
|
224 public boolean changePassword(String oldPassword, String newPassword) throws Exception {
|
|
225 PrivateKey priv;
|
|
226
|
|
227 try {
|
|
228 priv = PubkeyUtils.decodePrivate(getPrivateKey(), getType(), oldPassword);
|
|
229 }
|
|
230 catch (Exception e) {
|
|
231 return false;
|
|
232 }
|
|
233
|
|
234 setPrivateKey(PubkeyUtils.getEncodedPrivate(priv, newPassword));
|
|
235 setEncrypted(newPassword.length() > 0);
|
|
236 return true;
|
|
237 }
|
|
238 }
|