aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKenny Root <kenny@the-b.org>2013-04-14 16:35:21 -0700
committerKenny Root <kenny@the-b.org>2013-04-14 16:35:21 -0700
commitc6cc322ab28ba55c0253c40b0774bba5a751bc35 (patch)
tree2a3bbaa4f9887fb34e6e523c7d1966bdd61aa167
parentfc0fd37d666bb8bd1452c2263cd6fdcf652230cd (diff)
downloadconnectbot-c6cc322ab28ba55c0253c40b0774bba5a751bc35.tar.gz
connectbot-c6cc322ab28ba55c0253c40b0774bba5a751bc35.tar.bz2
connectbot-c6cc322ab28ba55c0253c40b0774bba5a751bc35.zip
Split recoverKeyPair method for testing
-rw-r--r--src/org/connectbot/util/PubkeyUtils.java27
-rw-r--r--tests/src/org/connectbot/util/PubkeyUtilsTest.java23
2 files changed, 38 insertions, 12 deletions
diff --git a/src/org/connectbot/util/PubkeyUtils.java b/src/org/connectbot/util/PubkeyUtils.java
index 314624b..6e390b6 100644
--- a/src/org/connectbot/util/PubkeyUtils.java
+++ b/src/org/connectbot/util/PubkeyUtils.java
@@ -199,7 +199,8 @@ public class PubkeyUtils {
}
}
- public static KeyPair recoverKeyPair(byte[] encoded) throws NoSuchAlgorithmException, InvalidKeySpecException {
+ public static KeyPair recoverKeyPair(byte[] encoded) throws NoSuchAlgorithmException,
+ InvalidKeySpecException {
final String algo = getAlgorithmForOid(getOidFromPkcs8Encoded(encoded));
final KeySpec privKeySpec = new PKCS8EncodedKeySpec(encoded);
@@ -207,10 +208,15 @@ public class PubkeyUtils {
final KeyFactory kf = KeyFactory.getInstance(algo);
final PrivateKey priv = kf.generatePrivate(privKeySpec);
- final PublicKey pub;
+ return new KeyPair(recoverPublicKey(kf, priv), priv);
+ }
+
+ static PublicKey recoverPublicKey(KeyFactory kf, PrivateKey priv)
+ throws NoSuchAlgorithmException, InvalidKeySpecException {
if (priv instanceof RSAPrivateCrtKey) {
RSAPrivateCrtKey rsaPriv = (RSAPrivateCrtKey) priv;
- pub = kf.generatePublic(new RSAPublicKeySpec(rsaPriv.getModulus(), rsaPriv.getPublicExponent()));
+ return kf.generatePublic(new RSAPublicKeySpec(rsaPriv.getModulus(), rsaPriv
+ .getPublicExponent()));
} else if (priv instanceof DSAPrivateKey) {
DSAPrivateKey dsaPriv = (DSAPrivateKey) priv;
DSAParams params = dsaPriv.getParams();
@@ -218,25 +224,22 @@ public class PubkeyUtils {
// Calculate public key Y
BigInteger y = params.getG().modPow(dsaPriv.getX(), params.getP());
- pub = kf.generatePublic(new DSAPublicKeySpec(y, params.getP(), params.getQ(), params.getG()));
+ return kf.generatePublic(new DSAPublicKeySpec(y, params.getP(), params.getQ(), params
+ .getG()));
} else if (priv instanceof ECPrivateKey) {
ECPrivateKey ecPriv = (ECPrivateKey) priv;
ECParameterSpec params = ecPriv.getParams();
// Calculate public key Y
ECPoint generator = params.getGenerator();
- BigInteger[] wCoords = EcCore.multiplyPointA(new BigInteger[] {
- generator.getAffineX(),
- generator.getAffineY() },
- ecPriv.getS(), params);
+ BigInteger[] wCoords = EcCore.multiplyPointA(new BigInteger[] { generator.getAffineX(),
+ generator.getAffineY() }, ecPriv.getS(), params);
ECPoint w = new ECPoint(wCoords[0], wCoords[1]);
- pub = kf.generatePublic(new ECPublicKeySpec(w, params));
+ return kf.generatePublic(new ECPublicKeySpec(w, params));
} else {
- throw new NoSuchAlgorithmException("Unknown algorithm: " + algo);
+ throw new NoSuchAlgorithmException("Key type must be RSA, DSA, or EC");
}
-
- return new KeyPair(pub, priv);
}
/*
diff --git a/tests/src/org/connectbot/util/PubkeyUtilsTest.java b/tests/src/org/connectbot/util/PubkeyUtilsTest.java
index fb5bb13..1eb1ee6 100644
--- a/tests/src/org/connectbot/util/PubkeyUtilsTest.java
+++ b/tests/src/org/connectbot/util/PubkeyUtilsTest.java
@@ -20,6 +20,7 @@ package org.connectbot.util;
import java.math.BigInteger;
import java.security.KeyPair;
import java.security.NoSuchAlgorithmException;
+import java.security.PrivateKey;
import java.security.interfaces.DSAParams;
import java.security.interfaces.DSAPublicKey;
import java.security.interfaces.ECPublicKey;
@@ -319,4 +320,26 @@ public class PubkeyUtilsTest extends AndroidTestCase {
assertEquals(EC_KEY_pub_x, pubKey.getW().getAffineX());
assertEquals(EC_KEY_pub_y, pubKey.getW().getAffineY());
}
+
+ private static class MyPrivateKey implements PrivateKey {
+ public String getAlgorithm() {
+ throw new UnsupportedOperationException();
+ }
+
+ public byte[] getEncoded() {
+ throw new UnsupportedOperationException();
+ }
+
+ public String getFormat() {
+ throw new UnsupportedOperationException();
+ }
+ }
+
+ public void testRecoverPublicKey_FakeKey_Failure() throws Exception {
+ try {
+ PubkeyUtils.recoverPublicKey(null, new MyPrivateKey());
+ fail("Should not accept unknown key types");
+ } catch (NoSuchAlgorithmException expected) {
+ }
+ }
}