diff options
author | Kenny Root <kroot@google.com> | 2012-03-26 12:13:08 -0700 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2012-03-26 12:13:08 -0700 |
commit | 087043baca7e2de81bd10c7955f73f8597d7bb83 (patch) | |
tree | ce94ec54207e73e15ddd18528ccadb65007f5a89 /luni/src | |
parent | 600dc4949de6bf5608e5f5a5214cde59299b683a (diff) | |
parent | beac31ef5949d994a7096f20f12fcf929b06884d (diff) | |
download | libcore-087043baca7e2de81bd10c7955f73f8597d7bb83.zip libcore-087043baca7e2de81bd10c7955f73f8597d7bb83.tar.gz libcore-087043baca7e2de81bd10c7955f73f8597d7bb83.tar.bz2 |
Merge "More support for ENGINE-based keys"
Diffstat (limited to 'luni/src')
6 files changed, 65 insertions, 22 deletions
diff --git a/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/OpenSSLDSAParams.java b/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/OpenSSLDSAParams.java index bc2887c..08aebf0 100644 --- a/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/OpenSSLDSAParams.java +++ b/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/OpenSSLDSAParams.java @@ -44,7 +44,7 @@ public class OpenSSLDSAParams implements DSAParams, AlgorithmParameterSpec { return key; } - private void ensureReadParams() { + private synchronized final void ensureReadParams() { if (fetchedParams) { return; } @@ -53,8 +53,12 @@ public class OpenSSLDSAParams implements DSAParams, AlgorithmParameterSpec { g = new BigInteger(params[0]); p = new BigInteger(params[1]); q = new BigInteger(params[2]); - y = new BigInteger(params[3]); - x = new BigInteger(params[4]); + if (params[3] != null) { + y = new BigInteger(params[3]); + } + if (params[4] != null) { + x = new BigInteger(params[4]); + } fetchedParams = true; } diff --git a/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/OpenSSLDSAPrivateKey.java b/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/OpenSSLDSAPrivateKey.java index cb58615..5009d09 100644 --- a/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/OpenSSLDSAPrivateKey.java +++ b/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/OpenSSLDSAPrivateKey.java @@ -89,7 +89,7 @@ public class OpenSSLDSAPrivateKey implements DSAPrivateKey { * the key. Returning {@code null} tells the caller that there's no * encoded format. */ - if (key.getEngine() != null) { + if (key.isEngineBased()) { return null; } @@ -103,7 +103,7 @@ public class OpenSSLDSAPrivateKey implements DSAPrivateKey { * the key. Returning {@code null} tells the caller that there's no * encoded format. */ - if (key.getEngine() != null) { + if (key.isEngineBased()) { return null; } @@ -140,22 +140,42 @@ public class OpenSSLDSAPrivateKey implements DSAPrivateKey { ensureReadParams(); - DSAPrivateKey other = (DSAPrivateKey) o; - return params.getX().equals(other.getX()) && params.equals(other.getParams()); + final BigInteger x = params.getX(); + if (x == null) { + /* + * If our X is null, we can't tell if these two private keys are + * equivalent. This usually happens if this key is ENGINE-based. If + * the other key was ENGINE-based, we should have caught it in the + * OpenSSLDSAPrivateKey case. + */ + return false; + } + + final DSAPrivateKey other = (DSAPrivateKey) o; + return x.equals(other.getX()) && params.equals(other.getParams()); } @Override public int hashCode() { ensureReadParams(); - return getX().hashCode() ^ params.hashCode(); + int hash = 1; + + final BigInteger x = getX(); + if (x != null) { + hash = hash * 3 + x.hashCode(); + } + + hash = hash * 7 + params.hashCode(); + + return hash; } @Override public String toString() { final StringBuilder sb = new StringBuilder("OpenSSLDSAPrivateKey{"); - if (key.getEngine() != null) { + if (key.isEngineBased()) { sb.append("key="); sb.append(key); sb.append('}'); diff --git a/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/OpenSSLKey.java b/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/OpenSSLKey.java index fcf4597..90eb0e2 100644 --- a/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/OpenSSLKey.java +++ b/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/OpenSSLKey.java @@ -39,6 +39,10 @@ class OpenSSLKey { return engine; } + boolean isEngineBased() { + return engine != null; + } + @Override protected void finalize() throws Throwable { try { diff --git a/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/OpenSSLRSAPrivateCrtKey.java b/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/OpenSSLRSAPrivateCrtKey.java index 8abcfff..8376515 100644 --- a/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/OpenSSLRSAPrivateCrtKey.java +++ b/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/OpenSSLRSAPrivateCrtKey.java @@ -232,7 +232,7 @@ public class OpenSSLRSAPrivateCrtKey extends OpenSSLRSAPrivateKey implements RSA public String toString() { final StringBuilder sb = new StringBuilder("OpenSSLRSAPrivateCrtKey{"); - if (getOpenSSLKey().getEngine() != null) { + if (getOpenSSLKey().isEngineBased()) { sb.append("key="); sb.append(getOpenSSLKey()); sb.append('}'); diff --git a/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/OpenSSLRSAPrivateKey.java b/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/OpenSSLRSAPrivateKey.java index a823b4f..3874db2 100644 --- a/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/OpenSSLRSAPrivateKey.java +++ b/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/OpenSSLRSAPrivateKey.java @@ -118,11 +118,18 @@ public class OpenSSLRSAPrivateKey implements RSAPrivateKey { } void readParams(byte[][] params) { - if (params[0] == null || params[2] == null) { - throw new RuntimeException("modulus == null || privateExponent == null"); + if (params[0] == null) { + throw new NullPointerException("modulus == null"); + } else if (params[2] == null && !key.isEngineBased()) { + throw new NullPointerException("privateExponent == null"); } + modulus = new BigInteger(params[0]); - privateExponent = new BigInteger(params[2]); + + // ENGINE-based keys are not guaranteed to have a private exponent. + if (params[2] != null) { + privateExponent = new BigInteger(params[2]); + } } @Override @@ -144,7 +151,7 @@ public class OpenSSLRSAPrivateKey implements RSAPrivateKey { * the key. Returning {@code null} tells the caller that there's no * encoded format. */ - if (key.getEngine() != null) { + if (key.isEngineBased()) { return null; } @@ -157,7 +164,7 @@ public class OpenSSLRSAPrivateKey implements RSAPrivateKey { * the key. Returning {@code null} tells the caller that there's no * encoded format. */ - if (key.getEngine() != null) { + if (key.isEngineBased()) { return null; } @@ -201,15 +208,21 @@ public class OpenSSLRSAPrivateKey implements RSAPrivateKey { @Override public int hashCode() { ensureReadParams(); - int hashCode = modulus.hashCode() ^ privateExponent.hashCode(); - return hashCode; + int hash = 1; + + hash = hash * 3 + modulus.hashCode(); + if (privateExponent != null) { + hash = hash * 7 + privateExponent.hashCode(); + } + + return hash; } @Override public String toString() { final StringBuilder sb = new StringBuilder("OpenSSLRSAPrivateKey{"); - if (key.getEngine() != null) { + if (key.isEngineBased()) { sb.append("key="); sb.append(key); sb.append('}'); diff --git a/luni/src/main/native/org_apache_harmony_xnet_provider_jsse_NativeCrypto.cpp b/luni/src/main/native/org_apache_harmony_xnet_provider_jsse_NativeCrypto.cpp index 1c31ec0..ba4940a 100644 --- a/luni/src/main/native/org_apache_harmony_xnet_provider_jsse_NativeCrypto.cpp +++ b/luni/src/main/native/org_apache_harmony_xnet_provider_jsse_NativeCrypto.cpp @@ -1075,11 +1075,13 @@ static jobjectArray NativeCrypto_get_RSA_private_params(JNIEnv* env, jclass, jin env->SetObjectArrayElement(joa, 1, e); } - jbyteArray d = bignumToArray(env, rsa->d); - if (env->ExceptionCheck()) { - return NULL; + if (rsa->d != NULL) { + jbyteArray d = bignumToArray(env, rsa->d); + if (env->ExceptionCheck()) { + return NULL; + } + env->SetObjectArrayElement(joa, 2, d); } - env->SetObjectArrayElement(joa, 2, d); if (rsa->p != NULL) { jbyteArray p = bignumToArray(env, rsa->p); |