Mercurial > 510Connectbot
annotate app/src/main/java/com/five_ten_sg/connectbot/bean/PubkeyBean.java @ 489:b257517e9bef
Added tag stable-1.9.4-1 for changeset 869070df0e80
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Wed, 14 Oct 2020 12:26:53 -0700 |
parents | d29cce60f393 |
children |
rev | line source |
---|---|
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 /* Database fields */ | |
39 private long id; | |
40 private String nickname; | |
41 private String type; | |
42 private byte[] privateKey; | |
43 private byte[] publicKey; | |
44 private boolean encrypted = false; | |
45 private boolean startup = false; | |
46 private boolean confirmUse = false; | |
47 private int lifetime = 0; | |
48 | |
49 /* Transient values */ | |
50 private transient boolean unlocked = false; | |
51 private transient Object unlockedPrivate = null; | |
52 private transient String description; | |
53 | |
54 @Override | |
55 public String getBeanName() { | |
56 return BEAN_NAME; | |
57 } | |
58 | |
59 public void setId(long id) { | |
60 this.id = id; | |
61 } | |
62 | |
63 public long getId() { | |
64 return id; | |
65 } | |
66 | |
67 public void setNickname(String nickname) { | |
68 this.nickname = nickname; | |
69 } | |
70 | |
71 public String getNickname() { | |
72 return nickname; | |
73 } | |
74 | |
75 public void setType(String type) { | |
76 this.type = type; | |
77 } | |
78 | |
79 public String getType() { | |
80 return type; | |
81 } | |
82 | |
83 public void setPrivateKey(byte[] privateKey) { | |
84 if (privateKey == null) | |
85 this.privateKey = null; | |
86 else | |
87 this.privateKey = privateKey.clone(); | |
88 } | |
89 | |
90 public byte[] getPrivateKey() { | |
91 if (privateKey == null) | |
92 return null; | |
93 else | |
94 return privateKey.clone(); | |
95 } | |
96 | |
97 public void setPublicKey(byte[] encoded) { | |
98 if (encoded == null) | |
99 publicKey = null; | |
100 else | |
101 publicKey = encoded.clone(); | |
102 } | |
103 | |
104 public byte[] getPublicKey() { | |
105 if (publicKey == null) | |
106 return null; | |
107 else | |
108 return publicKey.clone(); | |
109 } | |
110 | |
111 public void setEncrypted(boolean encrypted) { | |
112 this.encrypted = encrypted; | |
113 } | |
114 | |
115 public boolean isEncrypted() { | |
116 return encrypted; | |
117 } | |
118 | |
119 public void setStartup(boolean startup) { | |
120 this.startup = startup; | |
121 } | |
122 | |
123 public boolean isStartup() { | |
124 return startup; | |
125 } | |
126 | |
127 public void setConfirmUse(boolean confirmUse) { | |
128 this.confirmUse = confirmUse; | |
129 } | |
130 | |
131 public boolean isConfirmUse() { | |
132 return confirmUse; | |
133 } | |
134 | |
135 public void setLifetime(int lifetime) { | |
136 this.lifetime = lifetime; | |
137 } | |
138 | |
139 public int getLifetime() { | |
140 return lifetime; | |
141 } | |
142 | |
143 public void setUnlocked(boolean unlocked) { | |
144 this.unlocked = unlocked; | |
145 } | |
146 | |
147 public boolean isUnlocked() { | |
148 return unlocked; | |
149 } | |
150 | |
151 public void setUnlockedPrivate(Object unlockedPrivate) { | |
152 this.unlockedPrivate = unlockedPrivate; | |
153 } | |
154 | |
155 public Object getUnlockedPrivate() { | |
156 return unlockedPrivate; | |
157 } | |
158 | |
159 public String getDescription() { | |
160 if (description == null) { | |
161 final StringBuilder sb = new StringBuilder(); | |
162 | |
163 try { | |
340
82d127ae1550
fix pubkey display; better layout for generate pub keys
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
164 final PublicKey pubKey = PubkeyUtils.decodePublic(publicKey, type); |
0 | 165 |
166 if (PubkeyDatabase.KEY_TYPE_RSA.equals(type)) { | |
167 int bits = ((RSAPublicKey) pubKey).getModulus().bitLength(); | |
168 sb.append("RSA "); | |
169 sb.append(bits); | |
170 sb.append("-bit"); | |
171 } | |
172 else if (PubkeyDatabase.KEY_TYPE_DSA.equals(type)) { | |
173 sb.append("DSA 1024-bit"); | |
174 } | |
175 else if (PubkeyDatabase.KEY_TYPE_EC.equals(type)) { | |
176 int bits = ((ECPublicKey) pubKey).getParams().getCurve().getField() | |
177 .getFieldSize(); | |
178 sb.append("EC "); | |
179 sb.append(bits); | |
180 sb.append("-bit"); | |
181 } | |
182 else { | |
183 sb.append("Unknown Key Type"); | |
184 } | |
185 } | |
186 catch (NoSuchAlgorithmException e) { | |
187 sb.append("Unknown Key Type"); | |
188 } | |
189 catch (InvalidKeySpecException e) { | |
190 sb.append("Unknown Key Type"); | |
191 } | |
192 | |
193 if (encrypted) sb.append(" (encrypted)"); | |
194 | |
195 description = sb.toString(); | |
196 } | |
197 | |
198 return description; | |
199 } | |
200 | |
201 /* (non-Javadoc) | |
202 * @see com.five_ten_sg.connectbot.bean.AbstractBean#getValues() | |
203 */ | |
204 @Override | |
205 public ContentValues getValues() { | |
206 ContentValues values = new ContentValues(); | |
207 values.put(PubkeyDatabase.FIELD_PUBKEY_NICKNAME, nickname); | |
208 values.put(PubkeyDatabase.FIELD_PUBKEY_TYPE, type); | |
209 values.put(PubkeyDatabase.FIELD_PUBKEY_PRIVATE, privateKey); | |
210 values.put(PubkeyDatabase.FIELD_PUBKEY_PUBLIC, publicKey); | |
211 values.put(PubkeyDatabase.FIELD_PUBKEY_ENCRYPTED, encrypted ? 1 : 0); | |
212 values.put(PubkeyDatabase.FIELD_PUBKEY_STARTUP, startup ? 1 : 0); | |
213 values.put(PubkeyDatabase.FIELD_PUBKEY_CONFIRMUSE, confirmUse ? 1 : 0); | |
214 values.put(PubkeyDatabase.FIELD_PUBKEY_LIFETIME, lifetime); | |
215 return values; | |
216 } | |
217 | |
218 public boolean changePassword(String oldPassword, String newPassword) throws Exception { | |
219 PrivateKey priv; | |
220 | |
221 try { | |
222 priv = PubkeyUtils.decodePrivate(getPrivateKey(), getType(), oldPassword); | |
223 } | |
224 catch (Exception e) { | |
225 return false; | |
226 } | |
227 | |
228 setPrivateKey(PubkeyUtils.getEncodedPrivate(priv, newPassword)); | |
229 setEncrypted(newPassword.length() > 0); | |
230 return true; | |
231 } | |
232 } |