diff options
author | Kenny Root <kenny@the-b.org> | 2008-11-20 22:17:50 +0000 |
---|---|---|
committer | Kenny Root <kenny@the-b.org> | 2008-11-20 22:17:50 +0000 |
commit | d923d82ffa9e85f4c64a7b403865cde90ec0635b (patch) | |
tree | 7ed5220a7558746367dc667b9b8c2c51c3475e0e | |
parent | 7f0163a63584e95d16c2a031b1263db4cc108c04 (diff) | |
download | connectbot-d923d82ffa9e85f4c64a7b403865cde90ec0635b.tar.gz connectbot-d923d82ffa9e85f4c64a7b403865cde90ec0635b.tar.bz2 connectbot-d923d82ffa9e85f4c64a7b403865cde90ec0635b.zip |
Continued pubkey bean conversion
-rw-r--r-- | src/org/connectbot/GeneratePubkeyActivity.java | 17 | ||||
-rw-r--r-- | src/org/connectbot/PubkeyListActivity.java | 8 | ||||
-rw-r--r-- | src/org/connectbot/bean/PubkeyBean.java | 4 | ||||
-rw-r--r-- | src/org/connectbot/service/TerminalBridge.java | 52 | ||||
-rw-r--r-- | src/org/connectbot/util/PubkeyDatabase.java | 47 |
5 files changed, 42 insertions, 86 deletions
diff --git a/src/org/connectbot/GeneratePubkeyActivity.java b/src/org/connectbot/GeneratePubkeyActivity.java index 1c80173..79be141 100644 --- a/src/org/connectbot/GeneratePubkeyActivity.java +++ b/src/org/connectbot/GeneratePubkeyActivity.java @@ -24,6 +24,7 @@ import java.security.PrivateKey; import java.security.PublicKey; import java.security.SecureRandom; +import org.connectbot.bean.PubkeyBean; import org.connectbot.util.EntropyDialog; import org.connectbot.util.EntropyView; import org.connectbot.util.OnEntropyGatheredListener; @@ -250,14 +251,16 @@ public class GeneratePubkeyActivity extends Activity implements OnEntropyGathere Log.d(TAG, "private: " + PubkeyUtils.formatKey(priv)); Log.d(TAG, "public: " + PubkeyUtils.formatKey(pub)); + PubkeyBean pubkey = new PubkeyBean(); + pubkey.setNickname(nickname.getText().toString()); + pubkey.setType(keyType); + pubkey.setPrivateKey(PubkeyUtils.getEncodedPrivate(priv, secret)); + pubkey.setPublicKey(PubkeyUtils.getEncodedPublic(pub)); + pubkey.setEncrypted(encrypted); + pubkey.setStartup(unlockAtStartup.isChecked()); + PubkeyDatabase pubkeydb = new PubkeyDatabase(GeneratePubkeyActivity.this); - pubkeydb.createPubkey(null, - nickname.getText().toString(), - keyType, - PubkeyUtils.getEncodedPrivate(priv, secret), - PubkeyUtils.getEncodedPublic(pub), - encrypted, - unlockAtStartup.isChecked()); + pubkeydb.savePubkey(pubkey); pubkeydb.close(); } catch (Exception e) { Log.e(TAG, "Could not generate key pair"); diff --git a/src/org/connectbot/PubkeyListActivity.java b/src/org/connectbot/PubkeyListActivity.java index dc2ae94..869855c 100644 --- a/src/org/connectbot/PubkeyListActivity.java +++ b/src/org/connectbot/PubkeyListActivity.java @@ -242,7 +242,13 @@ public class PubkeyListActivity extends ListActivity implements EventListener { boolean encrypted = PEMDecoder.isPEMEncrypted(struct); // write new value into database - pubkeydb.createPubkey(null, name, PubkeyDatabase.KEY_TYPE_IMPORTED, raw, new byte[] {}, encrypted, false); + PubkeyBean pubkey = new PubkeyBean(); + pubkey.setNickname(name); + pubkey.setType(PubkeyDatabase.KEY_TYPE_IMPORTED); + pubkey.setPrivateKey(raw); + pubkey.setEncrypted(encrypted); + + pubkeydb.savePubkey(pubkey); updateHandler.sendEmptyMessage(-1); } catch(Exception e) { diff --git a/src/org/connectbot/bean/PubkeyBean.java b/src/org/connectbot/bean/PubkeyBean.java index 2e5606d..b3d3108 100644 --- a/src/org/connectbot/bean/PubkeyBean.java +++ b/src/org/connectbot/bean/PubkeyBean.java @@ -42,8 +42,8 @@ public class PubkeyBean extends AbstractBean { private String type; private byte[] privateKey; private byte[] publicKey; - private boolean encrypted; - private boolean startup; + private boolean encrypted = false; + private boolean startup = false; /* Transient values */ private boolean unlocked = false; diff --git a/src/org/connectbot/service/TerminalBridge.java b/src/org/connectbot/service/TerminalBridge.java index 14a3644..41d80d3 100644 --- a/src/org/connectbot/service/TerminalBridge.java +++ b/src/org/connectbot/service/TerminalBridge.java @@ -32,11 +32,11 @@ import org.connectbot.R; import org.connectbot.TerminalView; import org.connectbot.bean.HostBean; import org.connectbot.bean.PortForwardBean; +import org.connectbot.bean.PubkeyBean; import org.connectbot.util.HostDatabase; import org.connectbot.util.PubkeyDatabase; import org.connectbot.util.PubkeyUtils; -import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; @@ -323,60 +323,48 @@ public class TerminalBridge implements VDUDisplay, OnKeyListener, InteractiveCal * @throws InvalidKeySpecException * @throws IOException */ - private boolean tryPublicKey(Cursor c) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException { - int COL_NICKNAME = c.getColumnIndexOrThrow(PubkeyDatabase.FIELD_PUBKEY_NICKNAME), - COL_TYPE = c.getColumnIndexOrThrow(PubkeyDatabase.FIELD_PUBKEY_TYPE), - COL_PRIVATE = c.getColumnIndexOrThrow(PubkeyDatabase.FIELD_PUBKEY_PRIVATE), - COL_PUBLIC = c.getColumnIndexOrThrow(PubkeyDatabase.FIELD_PUBKEY_PUBLIC), - COL_ENCRYPTED = c.getColumnIndexOrThrow(PubkeyDatabase.FIELD_PUBKEY_ENCRYPTED); - - String keyNickname = c.getString(COL_NICKNAME); - int encrypted = c.getInt(COL_ENCRYPTED); - + private boolean tryPublicKey(PubkeyBean pubkey) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException { Object trileadKey = null; - if(manager.isKeyLoaded(keyNickname)) { + if(manager.isKeyLoaded(pubkey.getNickname())) { // load this key from memory if its already there - Log.d(TAG, String.format("Found unlocked key '%s' already in-memory", keyNickname)); - trileadKey = manager.getKey(keyNickname); + Log.d(TAG, String.format("Found unlocked key '%s' already in-memory", pubkey.getNickname())); + trileadKey = manager.getKey(pubkey.getNickname()); } else { // otherwise load key from database and prompt for password as needed String password = null; - if (encrypted != 0) - password = promptHelper.requestStringPrompt(String.format("Password for key '%s'", keyNickname)); + if (pubkey.isEncrypted()) + password = promptHelper.requestStringPrompt(String.format("Password for key '%s'", pubkey.getNickname())); - String type = c.getString(COL_TYPE); - if(PubkeyDatabase.KEY_TYPE_IMPORTED.equals(type)) { + if(PubkeyDatabase.KEY_TYPE_IMPORTED.equals(pubkey.getType())) { // load specific key using pem format - byte[] raw = c.getBlob(COL_PRIVATE); - trileadKey = PEMDecoder.decode(new String(raw).toCharArray(), password); - + trileadKey = PEMDecoder.decode(new String(pubkey.getPrivateKey()).toCharArray(), password); } else { // load using internal generated format PrivateKey privKey; try { - privKey = PubkeyUtils.decodePrivate(c.getBlob(COL_PRIVATE), - c.getString(COL_TYPE), password); + privKey = PubkeyUtils.decodePrivate(pubkey.getPrivateKey(), + pubkey.getType(), password); } catch (Exception e) { - String message = String.format("Bad password for key '%s'. Authentication failed.", keyNickname); + String message = String.format("Bad password for key '%s'. Authentication failed.", pubkey.getNickname()); Log.e(TAG, message, e); outputLine(message); return false; } - PublicKey pubKey = PubkeyUtils.decodePublic(c.getBlob(COL_PUBLIC), - c.getString(COL_TYPE)); + PublicKey pubKey = PubkeyUtils.decodePublic(pubkey.getPublicKey(), + pubkey.getType()); // convert key to trilead format trileadKey = PubkeyUtils.convertToTrilead(privKey, pubKey); Log.d(TAG, "Unlocked key " + PubkeyUtils.formatKey(pubKey)); } - Log.d(TAG, String.format("Unlocked key '%s'", keyNickname)); + Log.d(TAG, String.format("Unlocked key '%s'", pubkey.getNickname())); // save this key in-memory if option enabled if(manager.isSavingKeys()) { - manager.addKey(keyNickname, trileadKey); + manager.addKey(pubkey.getNickname(), trileadKey); } } @@ -428,11 +416,9 @@ public class TerminalBridge implements VDUDisplay, OnKeyListener, InteractiveCal } else { outputLine("Attempting 'publickey' authentication with a specific SSH key"); // use a specific key for this host, as requested - Cursor cursor = manager.pubkeydb.getPubkey(pubkeyId); - if (cursor.moveToFirst()) - if (tryPublicKey(cursor)) - finishConnection(); - cursor.close(); + PubkeyBean pubkey = manager.pubkeydb.findPubkeyById(pubkeyId); + if (tryPublicKey(pubkey)) + finishConnection(); } diff --git a/src/org/connectbot/util/PubkeyDatabase.java b/src/org/connectbot/util/PubkeyDatabase.java index 685c960..bf043d5 100644 --- a/src/org/connectbot/util/PubkeyDatabase.java +++ b/src/org/connectbot/util/PubkeyDatabase.java @@ -77,28 +77,6 @@ public class PubkeyDatabase extends SQLiteOpenHelper { public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } - - /** - * Create a new pubkey using the given parameters, and return its new - * <code>_id</code> value. - */ - public long createPubkey(SQLiteDatabase db, String nickname, String type, byte[] privatekey, - byte[] publickey, boolean encrypted, boolean startup) { - // create and insert new host - - if (db == null) - db = this.getWritableDatabase(); - - ContentValues values = new ContentValues(); - values.put(FIELD_PUBKEY_NICKNAME, nickname); - values.put(FIELD_PUBKEY_TYPE, type); - values.put(FIELD_PUBKEY_PRIVATE, privatekey); - values.put(FIELD_PUBKEY_PUBLIC, publickey); - values.put(FIELD_PUBKEY_ENCRYPTED, encrypted ? 1 : 0); - values.put(FIELD_PUBKEY_STARTUP, startup ? 1 : 0); - - return db.insert(TABLE_PUBKEYS, null, values); - } /** * Delete a specific host by its <code>_id</code> value. @@ -156,8 +134,8 @@ public class PubkeyDatabase extends SQLiteOpenHelper { pubkey.setType(c.getString(COL_TYPE)); pubkey.setPrivateKey(c.getBlob(COL_PRIVATE)); pubkey.setPublicKey(c.getBlob(COL_PUBLIC)); - pubkey.setEncrypted(Boolean.valueOf(c.getString(COL_ENCRYPTED))); - pubkey.setStartup(Boolean.valueOf(c.getString(COL_STARTUP))); + pubkey.setEncrypted(c.getInt(COL_ENCRYPTED) > 0); + pubkey.setStartup(c.getInt(COL_STARTUP) > 0); pubkeys.add(pubkey); } @@ -168,23 +146,6 @@ public class PubkeyDatabase extends SQLiteOpenHelper { return pubkeys; } - public Cursor getPubkey(long id) { - 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 }, - "_id = ?", new String[] { String.valueOf(id) }, - null, null, null); - } - - /*public Cursor getAllStartPubkeys() { - 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 }, - FIELD_PUBKEY_STARTUP + " = 1 AND " + FIELD_PUBKEY_ENCRYPTED + " = 0", null, null, null, null); - }*/ - /** * @param hostId * @return @@ -216,8 +177,8 @@ public class PubkeyDatabase extends SQLiteOpenHelper { 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(Boolean.valueOf(c.getString(c.getColumnIndexOrThrow(FIELD_PUBKEY_ENCRYPTED)))); - pubkey.setStartup(Boolean.valueOf(c.getString(c.getColumnIndexOrThrow(FIELD_PUBKEY_STARTUP)))); + pubkey.setEncrypted(c.getInt(c.getColumnIndexOrThrow(FIELD_PUBKEY_ENCRYPTED)) > 0); + pubkey.setStartup(c.getInt(c.getColumnIndexOrThrow(FIELD_PUBKEY_STARTUP)) > 0); return pubkey; } |