1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
/*
ConnectBot: simple, powerful, open-source SSH client for Android
Copyright (C) 2007-2008 Kenny Root, Jeffrey Sharkey
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.connectbot.util;
import java.util.LinkedList;
import java.util.List;
import org.connectbot.bean.PubkeyBean;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Public Key Encryption database. Contains private and public key pairs
* for public key authentication.
*
* @author Kenny Root
*/
public class PubkeyDatabase extends SQLiteOpenHelper {
public final static String TAG = PubkeyDatabase.class.toString();
public final static String DB_NAME = "pubkeys";
public final static int DB_VERSION = 1;
public final static String TABLE_PUBKEYS = "pubkeys";
public final static String FIELD_PUBKEY_NICKNAME = "nickname";
public final static String FIELD_PUBKEY_TYPE = "type";
public final static String FIELD_PUBKEY_PRIVATE = "private";
public final static String FIELD_PUBKEY_PUBLIC = "public";
public final static String FIELD_PUBKEY_ENCRYPTED = "encrypted";
public final static String FIELD_PUBKEY_STARTUP = "startup";
public final static String KEY_TYPE_RSA = "RSA",
KEY_TYPE_DSA = "DSA",
KEY_TYPE_IMPORTED = "IMPORTED";
private Context context;
public PubkeyDatabase(Context context) {
super(context, DB_NAME, null, DB_VERSION);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_PUBKEYS
+ " (_id INTEGER PRIMARY KEY, "
+ FIELD_PUBKEY_NICKNAME + " TEXT, "
+ FIELD_PUBKEY_TYPE + " TEXT, "
+ FIELD_PUBKEY_PRIVATE + " BLOB, "
+ FIELD_PUBKEY_PUBLIC + " BLOB, "
+ FIELD_PUBKEY_ENCRYPTED + " INTEGER, "
+ FIELD_PUBKEY_STARTUP + " INTEGER)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
/**
* Delete a specific host by its <code>_id</code> value.
*/
public void deletePubkey(PubkeyBean pubkey) {
HostDatabase hostdb = new HostDatabase(context);
hostdb.stopUsingPubkey(pubkey.getId());
hostdb.close();
SQLiteDatabase db = getWritableDatabase();
db.delete(TABLE_PUBKEYS, "_id = ?", new String[] { Long.toString(pubkey.getId()) });
db.close();
}
/**
* Return a cursor that contains information about all known hosts.
*/
/*
public Cursor allPubkeys() {
SQLiteDatabase db = this.getReadableDatabase();
return db.query(TABLE_PUBKEYS, new String[] { "_id",
FIELD_PUBKEY_NICKNAME, FIELD_PUBKEY_TYPE, FIELD_PUBKEY_PRIVATE,
FIELD_PUBKEY_PUBLIC, FIELD_PUBKEY_ENCRYPTED, FIELD_PUBKEY_STARTUP },
null, null, null, null, null);
}*/
public List<PubkeyBean> allPubkeys() {
return getPubkeys(null, null);
}
public List<PubkeyBean> getAllStartPubkeys() {
return getPubkeys(FIELD_PUBKEY_STARTUP + " = 1 AND " + FIELD_PUBKEY_ENCRYPTED + " = 0", null);
}
private List<PubkeyBean> getPubkeys(String selection, String[] selectionArgs) {
SQLiteDatabase db = getReadableDatabase();
List<PubkeyBean> pubkeys = new LinkedList<PubkeyBean>();
Cursor c = db.query(TABLE_PUBKEYS, null, selection, selectionArgs, null, null, null);
final int COL_ID = c.getColumnIndexOrThrow("_id"),
COL_NICKNAME = c.getColumnIndexOrThrow(FIELD_PUBKEY_NICKNAME),
COL_TYPE = c.getColumnIndexOrThrow(FIELD_PUBKEY_TYPE),
COL_PRIVATE = c.getColumnIndexOrThrow(FIELD_PUBKEY_PRIVATE),
COL_PUBLIC = c.getColumnIndexOrThrow(FIELD_PUBKEY_PUBLIC),
COL_ENCRYPTED = c.getColumnIndexOrThrow(FIELD_PUBKEY_ENCRYPTED),
COL_STARTUP = c.getColumnIndexOrThrow(FIELD_PUBKEY_STARTUP);
while (c.moveToNext()) {
PubkeyBean pubkey = new PubkeyBean();
pubkey.setId(c.getLong(COL_ID));
pubkey.setNickname(c.getString(COL_NICKNAME));
pubkey.setType(c.getString(COL_TYPE));
pubkey.setPrivateKey(c.getBlob(COL_PRIVATE));
pubkey.setPublicKey(c.getBlob(COL_PUBLIC));
pubkey.setEncrypted(c.getInt(COL_ENCRYPTED) > 0);
pubkey.setStartup(c.getInt(COL_STARTUP) > 0);
pubkeys.add(pubkey);
}
c.close();
db.close();
return pubkeys;
}
/**
* @param hostId
* @return
*/
public PubkeyBean findPubkeyById(long pubkeyId) {
SQLiteDatabase db = getReadableDatabase();
Cursor c = db.query(TABLE_PUBKEYS, null,
"_id = ?", new String[] { String.valueOf(pubkeyId) },
null, null, null);
PubkeyBean pubkey = null;
if (c != null && c.moveToFirst()) {
pubkey = createPubkeyBean(c);
}
c.close();
db.close();
return pubkey;
}
private PubkeyBean createPubkeyBean(Cursor c) {
PubkeyBean pubkey = new PubkeyBean();
pubkey.setId(c.getLong(c.getColumnIndexOrThrow("_id")));
pubkey.setNickname(c.getString(c.getColumnIndexOrThrow(FIELD_PUBKEY_NICKNAME)));
pubkey.setType(c.getString(c.getColumnIndexOrThrow(FIELD_PUBKEY_TYPE)));
pubkey.setPrivateKey(c.getBlob(c.getColumnIndexOrThrow(FIELD_PUBKEY_PRIVATE)));
pubkey.setPublicKey(c.getBlob(c.getColumnIndexOrThrow(FIELD_PUBKEY_PUBLIC)));
pubkey.setEncrypted(c.getInt(c.getColumnIndexOrThrow(FIELD_PUBKEY_ENCRYPTED)) > 0);
pubkey.setStartup(c.getInt(c.getColumnIndexOrThrow(FIELD_PUBKEY_STARTUP)) > 0);
return pubkey;
}
/**
* Pull all values for a given column as a list of Strings, probably for use
* in a ListPreference. Sorted by <code>_id</code> ascending.
*/
public List<CharSequence> allValues(String column) {
List<CharSequence> list = new LinkedList<CharSequence>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.query(TABLE_PUBKEYS, new String[] { "_id", column },
null, null, null, null, "_id ASC");
int COL = c.getColumnIndexOrThrow(column);
while(c.moveToNext()) {
list.add(c.getString(COL));
}
c.close();
return list;
}
public String getNickname(long id) {
String nickname = null;
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.query(TABLE_PUBKEYS, new String[] { "_id",
FIELD_PUBKEY_NICKNAME }, "_id = ?",
new String[] { Long.toString(id) }, null, null, null);
if (c != null && c.moveToFirst())
nickname = c.getString(c.getColumnIndexOrThrow(FIELD_PUBKEY_NICKNAME));
c.close();
return nickname;
}
/*
public void setOnStart(long id, boolean onStart) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(FIELD_PUBKEY_STARTUP, onStart ? 1 : 0);
db.update(TABLE_PUBKEYS, values, "_id = ?", new String[] { Long.toString(id) });
}
public boolean changePassword(long id, String oldPassword, String newPassword) throws NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, InvalidKeyException, BadPaddingException {
SQLiteDatabase db = this.getWritableDatabase();
Cursor c = db.query(TABLE_PUBKEYS, new String[] { FIELD_PUBKEY_TYPE,
FIELD_PUBKEY_PRIVATE, FIELD_PUBKEY_ENCRYPTED },
"_id = ?", new String[] { String.valueOf(id) },
null, null, null);
if (!c.moveToFirst())
return false;
String keyType = c.getString(0);
byte[] encPriv = c.getBlob(1);
c.close();
PrivateKey priv;
try {
priv = PubkeyUtils.decodePrivate(encPriv, keyType, oldPassword);
} catch (InvalidKeyException e) {
return false;
} catch (BadPaddingException e) {
return false;
} catch (InvalidKeySpecException e) {
return false;
}
ContentValues values = new ContentValues();
values.put(FIELD_PUBKEY_PRIVATE, PubkeyUtils.getEncodedPrivate(priv, newPassword));
values.put(FIELD_PUBKEY_ENCRYPTED, newPassword.length() > 0 ? 1 : 0);
db.update(TABLE_PUBKEYS, values, "_id = ?", new String[] { String.valueOf(id) });
return true;
}
*/
/**
* @param pubkey
*/
public PubkeyBean savePubkey(PubkeyBean pubkey) {
SQLiteDatabase db = this.getWritableDatabase();
boolean success = false;
ContentValues values = pubkey.getValues();
if (pubkey.getId() > 0) {
values.remove("_id");
if (db.update(TABLE_PUBKEYS, values, "_id = ?", new String[] { String.valueOf(pubkey.getId()) }) > 0)
success = true;
}
if (!success) {
long id = db.insert(TABLE_PUBKEYS, null, pubkey.getValues());
pubkey.setId(id);
}
db.close();
return pubkey;
}
}
|