comparison src/com/five_ten_sg/connectbot/util/PubkeyDatabase.java @ 0:0ce5cc452d02

initial version
author Carl Byington <carl@five-ten-sg.com>
date Thu, 22 May 2014 10:41:19 -0700
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:0ce5cc452d02
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.util;
19
20 import java.util.LinkedList;
21 import java.util.List;
22
23 import com.five_ten_sg.connectbot.bean.PubkeyBean;
24 import android.content.ContentValues;
25 import android.content.Context;
26 import android.database.Cursor;
27 import android.database.sqlite.SQLiteDatabase;
28 import android.database.sqlite.SQLiteException;
29
30 /**
31 * Public Key Encryption database. Contains private and public key pairs
32 * for public key authentication.
33 *
34 * @author Kenny Root
35 */
36 public class PubkeyDatabase extends RobustSQLiteOpenHelper {
37 public final static String TAG = "ConnectBot.PubkeyDatabase";
38
39 public final static String DB_NAME = "pubkeys";
40 public final static int DB_VERSION = 2;
41
42 public final static String TABLE_PUBKEYS = "pubkeys";
43 public final static String FIELD_PUBKEY_NICKNAME = "nickname";
44 public final static String FIELD_PUBKEY_TYPE = "type";
45 public final static String FIELD_PUBKEY_PRIVATE = "private";
46 public final static String FIELD_PUBKEY_PUBLIC = "public";
47 public final static String FIELD_PUBKEY_ENCRYPTED = "encrypted";
48 public final static String FIELD_PUBKEY_STARTUP = "startup";
49 public final static String FIELD_PUBKEY_CONFIRMUSE = "confirmuse";
50 public final static String FIELD_PUBKEY_LIFETIME = "lifetime";
51
52 public final static String KEY_TYPE_RSA = "RSA",
53 KEY_TYPE_DSA = "DSA",
54 KEY_TYPE_IMPORTED = "IMPORTED",
55 KEY_TYPE_EC = "EC";
56
57 private Context context;
58
59 static {
60 addTableName(TABLE_PUBKEYS);
61 }
62
63 public PubkeyDatabase(Context context) {
64 super(context, DB_NAME, null, DB_VERSION);
65 this.context = context;
66 }
67
68 @Override
69 public void onCreate(SQLiteDatabase db) {
70 super.onCreate(db);
71 db.execSQL("CREATE TABLE " + TABLE_PUBKEYS
72 + " (_id INTEGER PRIMARY KEY, "
73 + FIELD_PUBKEY_NICKNAME + " TEXT, "
74 + FIELD_PUBKEY_TYPE + " TEXT, "
75 + FIELD_PUBKEY_PRIVATE + " BLOB, "
76 + FIELD_PUBKEY_PUBLIC + " BLOB, "
77 + FIELD_PUBKEY_ENCRYPTED + " INTEGER, "
78 + FIELD_PUBKEY_STARTUP + " INTEGER, "
79 + FIELD_PUBKEY_CONFIRMUSE + " INTEGER DEFAULT 0, "
80 + FIELD_PUBKEY_LIFETIME + " INTEGER DEFAULT 0)");
81 }
82
83 @Override
84 public void onRobustUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) throws SQLiteException {
85 switch (oldVersion) {
86 case 1:
87 db.execSQL("ALTER TABLE " + TABLE_PUBKEYS
88 + " ADD COLUMN " + FIELD_PUBKEY_CONFIRMUSE + " INTEGER DEFAULT 0");
89 db.execSQL("ALTER TABLE " + TABLE_PUBKEYS
90 + " ADD COLUMN " + FIELD_PUBKEY_LIFETIME + " INTEGER DEFAULT 0");
91 }
92 }
93
94 /**
95 * Delete a specific host by its <code>_id</code> value.
96 */
97 public void deletePubkey(PubkeyBean pubkey) {
98 HostDatabase hostdb = new HostDatabase(context);
99 hostdb.stopUsingPubkey(pubkey.getId());
100 hostdb.close();
101 SQLiteDatabase db = getWritableDatabase();
102 db.delete(TABLE_PUBKEYS, "_id = ?", new String[] { Long.toString(pubkey.getId()) });
103 db.close();
104 }
105
106 /**
107 * Return a cursor that contains information about all known hosts.
108 */
109 /*
110 public Cursor allPubkeys() {
111 SQLiteDatabase db = this.getReadableDatabase();
112 return db.query(TABLE_PUBKEYS, new String[] { "_id",
113 FIELD_PUBKEY_NICKNAME, FIELD_PUBKEY_TYPE, FIELD_PUBKEY_PRIVATE,
114 FIELD_PUBKEY_PUBLIC, FIELD_PUBKEY_ENCRYPTED, FIELD_PUBKEY_STARTUP },
115 null, null, null, null, null);
116 }*/
117
118 public List<PubkeyBean> allPubkeys() {
119 return getPubkeys(null, null);
120 }
121
122 public List<PubkeyBean> getAllStartPubkeys() {
123 return getPubkeys(FIELD_PUBKEY_STARTUP + " = 1 AND " + FIELD_PUBKEY_ENCRYPTED + " = 0", null);
124 }
125
126 private List<PubkeyBean> getPubkeys(String selection, String[] selectionArgs) {
127 SQLiteDatabase db = getReadableDatabase();
128 List<PubkeyBean> pubkeys = new LinkedList<PubkeyBean>();
129 Cursor c = db.query(TABLE_PUBKEYS, null, selection, selectionArgs, null, null, null);
130
131 if (c != null) {
132 final int COL_ID = c.getColumnIndexOrThrow("_id"),
133 COL_NICKNAME = c.getColumnIndexOrThrow(FIELD_PUBKEY_NICKNAME),
134 COL_TYPE = c.getColumnIndexOrThrow(FIELD_PUBKEY_TYPE),
135 COL_PRIVATE = c.getColumnIndexOrThrow(FIELD_PUBKEY_PRIVATE),
136 COL_PUBLIC = c.getColumnIndexOrThrow(FIELD_PUBKEY_PUBLIC),
137 COL_ENCRYPTED = c.getColumnIndexOrThrow(FIELD_PUBKEY_ENCRYPTED),
138 COL_STARTUP = c.getColumnIndexOrThrow(FIELD_PUBKEY_STARTUP),
139 COL_CONFIRMUSE = c.getColumnIndexOrThrow(FIELD_PUBKEY_CONFIRMUSE),
140 COL_LIFETIME = c.getColumnIndexOrThrow(FIELD_PUBKEY_LIFETIME);
141
142 while (c.moveToNext()) {
143 PubkeyBean pubkey = new PubkeyBean();
144 pubkey.setId(c.getLong(COL_ID));
145 pubkey.setNickname(c.getString(COL_NICKNAME));
146 pubkey.setType(c.getString(COL_TYPE));
147 pubkey.setPrivateKey(c.getBlob(COL_PRIVATE));
148 pubkey.setPublicKey(c.getBlob(COL_PUBLIC));
149 pubkey.setEncrypted(c.getInt(COL_ENCRYPTED) > 0);
150 pubkey.setStartup(c.getInt(COL_STARTUP) > 0);
151 pubkey.setConfirmUse(c.getInt(COL_CONFIRMUSE) > 0);
152 pubkey.setLifetime(c.getInt(COL_LIFETIME));
153 pubkeys.add(pubkey);
154 }
155
156 c.close();
157 }
158
159 db.close();
160 return pubkeys;
161 }
162
163 /**
164 * @param hostId
165 * @return
166 */
167 public PubkeyBean findPubkeyById(long pubkeyId) {
168 SQLiteDatabase db = getReadableDatabase();
169 Cursor c = db.query(TABLE_PUBKEYS, null,
170 "_id = ?", new String[] { String.valueOf(pubkeyId) },
171 null, null, null);
172 PubkeyBean pubkey = null;
173
174 if (c != null) {
175 if (c.moveToFirst())
176 pubkey = createPubkeyBean(c);
177
178 c.close();
179 }
180
181 db.close();
182 return pubkey;
183 }
184
185 private PubkeyBean createPubkeyBean(Cursor c) {
186 PubkeyBean pubkey = new PubkeyBean();
187 pubkey.setId(c.getLong(c.getColumnIndexOrThrow("_id")));
188 pubkey.setNickname(c.getString(c.getColumnIndexOrThrow(FIELD_PUBKEY_NICKNAME)));
189 pubkey.setType(c.getString(c.getColumnIndexOrThrow(FIELD_PUBKEY_TYPE)));
190 pubkey.setPrivateKey(c.getBlob(c.getColumnIndexOrThrow(FIELD_PUBKEY_PRIVATE)));
191 pubkey.setPublicKey(c.getBlob(c.getColumnIndexOrThrow(FIELD_PUBKEY_PUBLIC)));
192 pubkey.setEncrypted(c.getInt(c.getColumnIndexOrThrow(FIELD_PUBKEY_ENCRYPTED)) > 0);
193 pubkey.setStartup(c.getInt(c.getColumnIndexOrThrow(FIELD_PUBKEY_STARTUP)) > 0);
194 pubkey.setConfirmUse(c.getInt(c.getColumnIndexOrThrow(FIELD_PUBKEY_CONFIRMUSE)) > 0);
195 pubkey.setLifetime(c.getInt(c.getColumnIndexOrThrow(FIELD_PUBKEY_LIFETIME)));
196 return pubkey;
197 }
198
199 /**
200 * Pull all values for a given column as a list of Strings, probably for use
201 * in a ListPreference. Sorted by <code>_id</code> ascending.
202 */
203 public List<CharSequence> allValues(String column) {
204 List<CharSequence> list = new LinkedList<CharSequence>();
205 SQLiteDatabase db = this.getReadableDatabase();
206 Cursor c = db.query(TABLE_PUBKEYS, new String[] { "_id", column },
207 null, null, null, null, "_id ASC");
208
209 if (c != null) {
210 int COL = c.getColumnIndexOrThrow(column);
211
212 while (c.moveToNext())
213 list.add(c.getString(COL));
214
215 c.close();
216 }
217
218 db.close();
219 return list;
220 }
221
222 public String getNickname(long id) {
223 String nickname = null;
224 SQLiteDatabase db = this.getReadableDatabase();
225 Cursor c = db.query(TABLE_PUBKEYS, new String[] { "_id",
226 FIELD_PUBKEY_NICKNAME
227 }, "_id = ?",
228 new String[] { Long.toString(id) }, null, null, null);
229
230 if (c != null) {
231 if (c.moveToFirst())
232 nickname = c.getString(c.getColumnIndexOrThrow(FIELD_PUBKEY_NICKNAME));
233
234 c.close();
235 }
236
237 db.close();
238 return nickname;
239 }
240
241 /*
242 public void setOnStart(long id, boolean onStart) {
243
244 SQLiteDatabase db = this.getWritableDatabase();
245
246 ContentValues values = new ContentValues();
247 values.put(FIELD_PUBKEY_STARTUP, onStart ? 1 : 0);
248
249 db.update(TABLE_PUBKEYS, values, "_id = ?", new String[] { Long.toString(id) });
250
251 }
252
253 public boolean changePassword(long id, String oldPassword, String newPassword) throws NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, InvalidKeyException, BadPaddingException {
254 SQLiteDatabase db = this.getWritableDatabase();
255
256 Cursor c = db.query(TABLE_PUBKEYS, new String[] { FIELD_PUBKEY_TYPE,
257 FIELD_PUBKEY_PRIVATE, FIELD_PUBKEY_ENCRYPTED },
258 "_id = ?", new String[] { String.valueOf(id) },
259 null, null, null);
260
261 if (!c.moveToFirst())
262 return false;
263
264 String keyType = c.getString(0);
265 byte[] encPriv = c.getBlob(1);
266 c.close();
267
268 PrivateKey priv;
269 try {
270 priv = PubkeyUtils.decodePrivate(encPriv, keyType, oldPassword);
271 } catch (InvalidKeyException e) {
272 return false;
273 } catch (BadPaddingException e) {
274 return false;
275 } catch (InvalidKeySpecException e) {
276 return false;
277 }
278
279 ContentValues values = new ContentValues();
280 values.put(FIELD_PUBKEY_PRIVATE, PubkeyUtils.getEncodedPrivate(priv, newPassword));
281 values.put(FIELD_PUBKEY_ENCRYPTED, newPassword.length() > 0 ? 1 : 0);
282 db.update(TABLE_PUBKEYS, values, "_id = ?", new String[] { String.valueOf(id) });
283
284 return true;
285 }
286 */
287
288 /**
289 * @param pubkey
290 */
291 public PubkeyBean savePubkey(PubkeyBean pubkey) {
292 SQLiteDatabase db = this.getWritableDatabase();
293 boolean success = false;
294 ContentValues values = pubkey.getValues();
295
296 if (pubkey.getId() > 0) {
297 values.remove("_id");
298
299 if (db.update(TABLE_PUBKEYS, values, "_id = ?", new String[] { String.valueOf(pubkey.getId()) }) > 0)
300 success = true;
301 }
302
303 if (!success) {
304 long id = db.insert(TABLE_PUBKEYS, null, pubkey.getValues());
305 pubkey.setId(id);
306 }
307
308 db.close();
309 return pubkey;
310 }
311 }