diff options
author | Kenny Root <kroot@google.com> | 2012-09-27 11:17:18 -0700 |
---|---|---|
committer | Android Git Automerger <android-git-automerger@android.com> | 2012-09-27 11:17:18 -0700 |
commit | 0df5a7ea6de1b66a1a27678e66909b85c1e464fe (patch) | |
tree | 50a06c1d7d6cce6b56dbb3b3e58890090d6daed9 | |
parent | a493eed8059bde8ac51fe70430270b4efc54e904 (diff) | |
parent | 96612f9cbfe5666958ec3608a669e6c585432049 (diff) | |
download | libcore-0df5a7ea6de1b66a1a27678e66909b85c1e464fe.zip libcore-0df5a7ea6de1b66a1a27678e66909b85c1e464fe.tar.gz libcore-0df5a7ea6de1b66a1a27678e66909b85c1e464fe.tar.bz2 |
am 96612f9c: am a233144d: Merge "Add serialization to OpenSSL-based keys"
* commit '96612f9cbfe5666958ec3608a669e6c585432049':
Add serialization to OpenSSL-based keys
6 files changed, 172 insertions, 10 deletions
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 761b08e..54e5d08 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 @@ -16,6 +16,10 @@ package org.apache.harmony.xnet.provider.jsse; +import java.io.IOException; +import java.io.NotSerializableException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; import java.math.BigInteger; import java.security.InvalidKeyException; import java.security.interfaces.DSAParams; @@ -26,9 +30,9 @@ import java.security.spec.InvalidKeySpecException; public class OpenSSLDSAPrivateKey implements DSAPrivateKey { private static final long serialVersionUID = 6524734576187424628L; - private final OpenSSLKey key; + private transient OpenSSLKey key; - private OpenSSLDSAParams params; + private transient OpenSSLDSAParams params; OpenSSLDSAPrivateKey(OpenSSLKey key) { this.key = key; @@ -200,4 +204,33 @@ public class OpenSSLDSAPrivateKey implements DSAPrivateKey { return sb.toString(); } + + private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { + stream.defaultReadObject(); + + final BigInteger g = (BigInteger) stream.readObject(); + final BigInteger p = (BigInteger) stream.readObject(); + final BigInteger q = (BigInteger) stream.readObject(); + final BigInteger x = (BigInteger) stream.readObject(); + + key = new OpenSSLKey(NativeCrypto.EVP_PKEY_new_DSA( + p.toByteArray(), + q.toByteArray(), + g.toByteArray(), + null, + x.toByteArray())); + } + + private void writeObject(ObjectOutputStream stream) throws IOException { + if (getOpenSSLKey().isEngineBased()) { + throw new NotSerializableException("engine-based keys can not be serialized"); + } + stream.defaultWriteObject(); + + ensureReadParams(); + stream.writeObject(params.getG()); + stream.writeObject(params.getP()); + stream.writeObject(params.getQ()); + stream.writeObject(params.getX()); + } } diff --git a/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/OpenSSLDSAPublicKey.java b/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/OpenSSLDSAPublicKey.java index 25869bb..84e0d36 100644 --- a/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/OpenSSLDSAPublicKey.java +++ b/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/OpenSSLDSAPublicKey.java @@ -16,6 +16,10 @@ package org.apache.harmony.xnet.provider.jsse; +import java.io.IOException; +import java.io.NotSerializableException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; import java.math.BigInteger; import java.security.InvalidKeyException; import java.security.interfaces.DSAParams; @@ -26,9 +30,9 @@ import java.security.spec.InvalidKeySpecException; public class OpenSSLDSAPublicKey implements DSAPublicKey { private static final long serialVersionUID = 5238609500353792232L; - private final OpenSSLKey key; + private transient OpenSSLKey key; - private OpenSSLDSAParams params; + private transient OpenSSLDSAParams params; OpenSSLDSAPublicKey(OpenSSLKey key) { this.key = key; @@ -147,4 +151,33 @@ public class OpenSSLDSAPublicKey implements DSAPublicKey { return sb.toString(); } + + private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { + stream.defaultReadObject(); + + final BigInteger g = (BigInteger) stream.readObject(); + final BigInteger p = (BigInteger) stream.readObject(); + final BigInteger q = (BigInteger) stream.readObject(); + final BigInteger y = (BigInteger) stream.readObject(); + + key = new OpenSSLKey(NativeCrypto.EVP_PKEY_new_DSA( + p.toByteArray(), + q.toByteArray(), + g.toByteArray(), + y.toByteArray(), + null)); + } + + private void writeObject(ObjectOutputStream stream) throws IOException { + if (getOpenSSLKey().isEngineBased()) { + throw new NotSerializableException("engine-based keys can not be serialized"); + } + stream.defaultWriteObject(); + + ensureReadParams(); + stream.writeObject(params.getG()); + stream.writeObject(params.getP()); + stream.writeObject(params.getQ()); + stream.writeObject(params.getY()); + } } 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 4303e5a..0f43bf9 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 @@ -16,6 +16,10 @@ package org.apache.harmony.xnet.provider.jsse; +import java.io.IOException; +import java.io.NotSerializableException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; import java.math.BigInteger; import java.security.InvalidKeyException; import java.security.interfaces.RSAPrivateCrtKey; @@ -24,6 +28,8 @@ import java.security.spec.InvalidKeySpecException; import java.security.spec.RSAPrivateCrtKeySpec; public class OpenSSLRSAPrivateCrtKey extends OpenSSLRSAPrivateKey implements RSAPrivateCrtKey { + private static final long serialVersionUID = 3785291944868707197L; + private BigInteger publicExponent; private BigInteger primeP; @@ -305,4 +311,27 @@ public class OpenSSLRSAPrivateCrtKey extends OpenSSLRSAPrivateKey implements RSA return sb.toString(); } + + private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { + stream.defaultReadObject(); + + key = new OpenSSLKey(NativeCrypto.EVP_PKEY_new_RSA( + modulus.toByteArray(), + publicExponent == null ? null : publicExponent.toByteArray(), + privateExponent.toByteArray(), + primeP == null ? null : primeP.toByteArray(), + primeQ == null ? null : primeQ.toByteArray(), + primeExponentP == null ? null : primeExponentP.toByteArray(), + primeExponentQ == null ? null : primeExponentQ.toByteArray(), + crtCoefficient == null ? null : crtCoefficient.toByteArray())); + fetchedParams = true; + } + + private void writeObject(ObjectOutputStream stream) throws IOException { + if (getOpenSSLKey().isEngineBased()) { + throw new NotSerializableException("engine-based keys can not be serialized"); + } + ensureReadParams(); + stream.defaultWriteObject(); + } } 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 adb05a9..6ad89b2 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 @@ -16,6 +16,10 @@ package org.apache.harmony.xnet.provider.jsse; +import java.io.IOException; +import java.io.NotSerializableException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; import java.math.BigInteger; import java.security.InvalidKeyException; import java.security.interfaces.RSAPrivateKey; @@ -25,13 +29,13 @@ import java.security.spec.RSAPrivateKeySpec; public class OpenSSLRSAPrivateKey implements RSAPrivateKey { private static final long serialVersionUID = 4872170254439578735L; - private final OpenSSLKey key; + protected transient OpenSSLKey key; - private boolean fetchedParams; + protected transient boolean fetchedParams; - private BigInteger modulus; + protected BigInteger modulus; - private BigInteger privateExponent; + protected BigInteger privateExponent; OpenSSLRSAPrivateKey(OpenSSLKey key) { this.key = key; @@ -252,4 +256,27 @@ public class OpenSSLRSAPrivateKey implements RSAPrivateKey { return sb.toString(); } + + private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { + stream.defaultReadObject(); + + key = new OpenSSLKey(NativeCrypto.EVP_PKEY_new_RSA( + modulus.toByteArray(), + null, + privateExponent.toByteArray(), + null, + null, + null, + null, + null)); + fetchedParams = true; + } + + private void writeObject(ObjectOutputStream stream) throws IOException { + if (getOpenSSLKey().isEngineBased()) { + throw new NotSerializableException("engine-based keys can not be serialized"); + } + ensureReadParams(); + stream.defaultWriteObject(); + } } diff --git a/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/OpenSSLRSAPublicKey.java b/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/OpenSSLRSAPublicKey.java index 1613cf0..4959a16 100644 --- a/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/OpenSSLRSAPublicKey.java +++ b/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/OpenSSLRSAPublicKey.java @@ -16,6 +16,9 @@ package org.apache.harmony.xnet.provider.jsse; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; import java.math.BigInteger; import java.security.InvalidKeyException; import java.security.interfaces.RSAPublicKey; @@ -25,13 +28,13 @@ import java.security.spec.RSAPublicKeySpec; public class OpenSSLRSAPublicKey implements RSAPublicKey { private static final long serialVersionUID = 123125005824688292L; - private final OpenSSLKey key; + private transient OpenSSLKey key; private BigInteger publicExponent; private BigInteger modulus; - private boolean fetchedParams; + private transient boolean fetchedParams; OpenSSLRSAPublicKey(OpenSSLKey key) { this.key = key; @@ -162,4 +165,24 @@ public class OpenSSLRSAPublicKey implements RSAPublicKey { return sb.toString(); } + + private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { + stream.defaultReadObject(); + + key = new OpenSSLKey(NativeCrypto.EVP_PKEY_new_RSA( + modulus.toByteArray(), + publicExponent.toByteArray(), + null, + null, + null, + null, + null, + null)); + fetchedParams = true; + } + + private void writeObject(ObjectOutputStream stream) throws IOException { + ensureReadParams(); + stream.defaultWriteObject(); + } } diff --git a/luni/src/test/java/libcore/java/security/KeyPairGeneratorTest.java b/luni/src/test/java/libcore/java/security/KeyPairGeneratorTest.java index cc90147..93b58df 100644 --- a/luni/src/test/java/libcore/java/security/KeyPairGeneratorTest.java +++ b/luni/src/test/java/libcore/java/security/KeyPairGeneratorTest.java @@ -16,6 +16,10 @@ package libcore.java.security; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; import java.math.BigInteger; import java.security.Key; import java.security.KeyFactory; @@ -149,6 +153,19 @@ public class KeyPairGeneratorTest extends TestCase { assertNotNull(k.getEncoded()); assertNotNull(k.getFormat()); + // Test serialization + { + ByteArrayOutputStream baos = new ByteArrayOutputStream(16384); + ObjectOutputStream oos = new ObjectOutputStream(baos); + oos.writeObject(k); + + ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + ObjectInputStream ois = new ObjectInputStream(bais); + Key inflatedKey = (Key) ois.readObject(); + + assertEquals(k, inflatedKey); + } + test_KeyWithAllKeyFactories(k); } |