diff options
author | Brian Carlstrom <bdc@google.com> | 2010-10-19 09:52:34 -0700 |
---|---|---|
committer | Android Git Automerger <android-git-automerger@android.com> | 2010-10-19 09:52:34 -0700 |
commit | ce23c2dff2f5ad5b3ada0a6a58aba65ac87ed08b (patch) | |
tree | 3e1e8621bb017cc797aeead893743f4f2e9f27d9 /luni/src/main/java/javax/crypto | |
parent | 90224a5fd4a49b519039bd29d72c0a6a06a2752b (diff) | |
parent | 0a480846a9798c763b088a122ab0dcd3dc3a17b6 (diff) | |
download | libcore-ce23c2dff2f5ad5b3ada0a6a58aba65ac87ed08b.zip libcore-ce23c2dff2f5ad5b3ada0a6a58aba65ac87ed08b.tar.gz libcore-ce23c2dff2f5ad5b3ada0a6a58aba65ac87ed08b.tar.bz2 |
am 0a480846: Remove Engine.spi memory leak by clearing after access
Merge commit '0a480846a9798c763b088a122ab0dcd3dc3a17b6' into dalvik-dev
* commit '0a480846a9798c763b088a122ab0dcd3dc3a17b6':
Remove Engine.spi memory leak by clearing after access
Diffstat (limited to 'luni/src/main/java/javax/crypto')
-rw-r--r-- | luni/src/main/java/javax/crypto/Cipher.java | 89 | ||||
-rw-r--r-- | luni/src/main/java/javax/crypto/ExemptionMechanism.java | 16 | ||||
-rw-r--r-- | luni/src/main/java/javax/crypto/KeyAgreement.java | 20 | ||||
-rw-r--r-- | luni/src/main/java/javax/crypto/KeyGenerator.java | 20 | ||||
-rw-r--r-- | luni/src/main/java/javax/crypto/Mac.java | 14 | ||||
-rw-r--r-- | luni/src/main/java/javax/crypto/SecretKeyFactory.java | 16 |
6 files changed, 91 insertions, 84 deletions
diff --git a/luni/src/main/java/javax/crypto/Cipher.java b/luni/src/main/java/javax/crypto/Cipher.java index 20dca98..209a040 100644 --- a/luni/src/main/java/javax/crypto/Cipher.java +++ b/luni/src/main/java/javax/crypto/Cipher.java @@ -107,7 +107,7 @@ public class Cipher { /** * Used to access common engine functionality. */ - private static final Engine engine = new Engine(SERVICE); + private static final Engine ENGINE = new Engine(SERVICE); /** * The provider. @@ -264,53 +264,57 @@ public class Cipher { boolean needSetPadding = false; boolean needSetMode = false; - if (transf[1] == null && transf[2] == null) { // "algorithm" - if (provider == null) { - engine.getInstance(transf[0], null); + Object engineSpi; + Provider engineProvider; + synchronized (ENGINE) { + if (transf[1] == null && transf[2] == null) { // "algorithm" + if (provider == null) { + ENGINE.getInstance(transf[0], null); + } else { + ENGINE.getInstance(transf[0], provider, null); + } } else { - engine.getInstance(transf[0], provider, null); - } - } else { - String[] searhOrder = { + String[] searhOrder = { transf[0] + "/" + transf[1] + "/" + transf[2], // "algorithm/mode/padding" transf[0] + "/" + transf[1], // "algorithm/mode" transf[0] + "//" + transf[2], // "algorithm//padding" transf[0] // "algorithm" - }; - int i; - for (i = 0; i < searhOrder.length; i++) { - try { - if (provider == null) { - engine.getInstance(searhOrder[i], null); - } else { - engine.getInstance(searhOrder[i], provider, null); - } - break; - } catch (NoSuchAlgorithmException e) { - if ( i == searhOrder.length-1) { - throw new NoSuchAlgorithmException(transformation); + }; + int i; + for (i = 0; i < searhOrder.length; i++) { + try { + if (provider == null) { + ENGINE.getInstance(searhOrder[i], null); + } else { + ENGINE.getInstance(searhOrder[i], provider, null); + } + break; + } catch (NoSuchAlgorithmException e) { + if ( i == searhOrder.length-1) { + throw new NoSuchAlgorithmException(transformation); + } } } + switch (i) { + case 1: // "algorithm/mode" + needSetPadding = true; + break; + case 2: // "algorithm//padding" + needSetMode = true; + break; + case 3: // "algorithm" + needSetPadding = true; + needSetMode = true; + } } - switch (i) { - case 1: // "algorithm/mode" - needSetPadding = true; - break; - case 2: // "algorithm//padding" - needSetMode = true; - break; - case 3: // "algorithm" - needSetPadding = true; - needSetMode = true; - } + engineSpi = ENGINE.getSpi(); + engineProvider = ENGINE.getProvider(); } - CipherSpi cspi; - try { - cspi = (CipherSpi) engine.spi; - } catch (ClassCastException e) { - throw new NoSuchAlgorithmException(e); + if (!(engineSpi instanceof CipherSpi)) { + throw new NoSuchAlgorithmException(engineSpi.getClass().getName()); } - Cipher c = new Cipher(cspi, engine.provider, transformation); + CipherSpi cspi = (CipherSpi) engineSpi; + Cipher c = new Cipher(cspi, engineProvider, transformation); if (needSetMode) { c.spiImpl.engineSetMode(transf[1]); } @@ -513,7 +517,8 @@ public class Cipher { } private void checkMode(int mode) { - if (mode != ENCRYPT_MODE && mode != DECRYPT_MODE && mode != UNWRAP_MODE && mode != WRAP_MODE) { + if (mode != ENCRYPT_MODE && mode != DECRYPT_MODE + && mode != UNWRAP_MODE && mode != WRAP_MODE) { throw new InvalidParameterException("Invalid mode: " + mode); } } @@ -801,9 +806,11 @@ public class Cipher { // decipherOnly (8) } if (keyUsage != null) { if (opmode == ENCRYPT_MODE && (!keyUsage[7])) { - throw new InvalidKeyException("The public key in the certificate cannot be used for ENCRYPT_MODE"); + throw new InvalidKeyException("The public key in the certificate " + + "cannot be used for ENCRYPT_MODE"); } else if (opmode == DECRYPT_MODE && (!keyUsage[8])) { - throw new InvalidKeyException("The public key in the certificate cannot be used for DECRYPT_MODE"); + throw new InvalidKeyException("The public key in the certificate " + + "cannot be used for DECRYPT_MODE"); } } } diff --git a/luni/src/main/java/javax/crypto/ExemptionMechanism.java b/luni/src/main/java/javax/crypto/ExemptionMechanism.java index e3e6975..16da203 100644 --- a/luni/src/main/java/javax/crypto/ExemptionMechanism.java +++ b/luni/src/main/java/javax/crypto/ExemptionMechanism.java @@ -36,7 +36,7 @@ import org.apache.harmony.security.fortress.Engine; public class ExemptionMechanism { // Used to access common engine functionality - private static final Engine engine = new Engine("ExemptionMechanism"); + private static final Engine ENGINE = new Engine("ExemptionMechanism"); // Store used provider private final Provider provider; @@ -100,10 +100,10 @@ public class ExemptionMechanism { if (algorithm == null) { throw new NullPointerException(); } - synchronized (engine) { - engine.getInstance(algorithm, null); - return new ExemptionMechanism((ExemptionMechanismSpi) engine.spi, - engine.provider, algorithm); + synchronized (ENGINE) { + ENGINE.getInstance(algorithm, null); + return new ExemptionMechanism((ExemptionMechanismSpi) ENGINE.getSpi(), + ENGINE.getProvider(), algorithm); } } @@ -167,9 +167,9 @@ public class ExemptionMechanism { if (provider == null) { throw new IllegalArgumentException("provider == null"); } - synchronized (engine) { - engine.getInstance(algorithm, provider, null); - return new ExemptionMechanism((ExemptionMechanismSpi) engine.spi, + synchronized (ENGINE) { + ENGINE.getInstance(algorithm, provider, null); + return new ExemptionMechanism((ExemptionMechanismSpi) ENGINE.getSpi(), provider, algorithm); } } diff --git a/luni/src/main/java/javax/crypto/KeyAgreement.java b/luni/src/main/java/javax/crypto/KeyAgreement.java index 8df7447..4640c96 100644 --- a/luni/src/main/java/javax/crypto/KeyAgreement.java +++ b/luni/src/main/java/javax/crypto/KeyAgreement.java @@ -36,10 +36,10 @@ import org.apache.harmony.security.fortress.Engine; public class KeyAgreement { // Used to access common engine functionality - private static final Engine engine = new Engine("KeyAgreement"); + private static final Engine ENGINE = new Engine("KeyAgreement"); // Store SecureRandom - private static final SecureRandom rndm = new SecureRandom(); + private static final SecureRandom RANDOM = new SecureRandom(); // Store used provider private final Provider provider; @@ -101,9 +101,9 @@ public class KeyAgreement { if (algorithm == null) { throw new NullPointerException(); } - synchronized (engine) { - engine.getInstance(algorithm, null); - return new KeyAgreement((KeyAgreementSpi) engine.spi, engine.provider, + synchronized (ENGINE) { + ENGINE.getInstance(algorithm, null); + return new KeyAgreement((KeyAgreementSpi) ENGINE.getSpi(), ENGINE.getProvider(), algorithm); } } @@ -166,9 +166,9 @@ public class KeyAgreement { if (algorithm == null) { throw new NullPointerException(); } - synchronized (engine) { - engine.getInstance(algorithm, provider, null); - return new KeyAgreement((KeyAgreementSpi) engine.spi, provider, + synchronized (ENGINE) { + ENGINE.getInstance(algorithm, provider, null); + return new KeyAgreement((KeyAgreementSpi) ENGINE.getSpi(), provider, algorithm); } } @@ -183,7 +183,7 @@ public class KeyAgreement { * agreement. */ public final void init(Key key) throws InvalidKeyException { - spiImpl.engineInit(key, rndm);//new SecureRandom()); + spiImpl.engineInit(key, RANDOM);//new SecureRandom()); } /** @@ -220,7 +220,7 @@ public class KeyAgreement { */ public final void init(Key key, AlgorithmParameterSpec params) throws InvalidKeyException, InvalidAlgorithmParameterException { - spiImpl.engineInit(key, params, rndm);//new SecureRandom()); + spiImpl.engineInit(key, params, RANDOM);//new SecureRandom()); } /** diff --git a/luni/src/main/java/javax/crypto/KeyGenerator.java b/luni/src/main/java/javax/crypto/KeyGenerator.java index 915662c..1cca416 100644 --- a/luni/src/main/java/javax/crypto/KeyGenerator.java +++ b/luni/src/main/java/javax/crypto/KeyGenerator.java @@ -34,10 +34,10 @@ import org.apache.harmony.security.fortress.Engine; public class KeyGenerator { // Used to access common engine functionality - private static final Engine engine = new Engine("KeyGenerator"); + private static final Engine ENGINE = new Engine("KeyGenerator"); // Store SecureRandom - private static final SecureRandom rndm = new SecureRandom(); + private static final SecureRandom RANDOM = new SecureRandom(); // Store used provider private final Provider provider; @@ -100,9 +100,9 @@ public class KeyGenerator { if (algorithm == null) { throw new NullPointerException(); } - synchronized (engine) { - engine.getInstance(algorithm, null); - return new KeyGenerator((KeyGeneratorSpi) engine.spi, engine.provider, + synchronized (ENGINE) { + ENGINE.getInstance(algorithm, null); + return new KeyGenerator((KeyGeneratorSpi) ENGINE.getSpi(), ENGINE.getProvider(), algorithm); } } @@ -163,9 +163,9 @@ public class KeyGenerator { if (algorithm == null) { throw new NullPointerException(); } - synchronized (engine) { - engine.getInstance(algorithm, provider, null); - return new KeyGenerator((KeyGeneratorSpi) engine.spi, provider, + synchronized (ENGINE) { + ENGINE.getInstance(algorithm, provider, null); + return new KeyGenerator((KeyGeneratorSpi) ENGINE.getSpi(), provider, algorithm); } } @@ -191,7 +191,7 @@ public class KeyGenerator { */ public final void init(AlgorithmParameterSpec params) throws InvalidAlgorithmParameterException { - spiImpl.engineInit(params, rndm);//new SecureRandom()); + spiImpl.engineInit(params, RANDOM);//new SecureRandom()); } /** @@ -219,7 +219,7 @@ public class KeyGenerator { * the size of the key (in bits). */ public final void init(int keysize) { - spiImpl.engineInit(keysize, rndm);//new SecureRandom()); + spiImpl.engineInit(keysize, RANDOM);//new SecureRandom()); } /** diff --git a/luni/src/main/java/javax/crypto/Mac.java b/luni/src/main/java/javax/crypto/Mac.java index 1e98db8..e677515 100644 --- a/luni/src/main/java/javax/crypto/Mac.java +++ b/luni/src/main/java/javax/crypto/Mac.java @@ -36,7 +36,7 @@ import org.apache.harmony.security.fortress.Engine; public class Mac implements Cloneable { //Used to access common engine functionality - private static final Engine engine = new Engine("Mac"); + private static final Engine ENGINE = new Engine("Mac"); // Store used provider private final Provider provider; @@ -103,9 +103,9 @@ public class Mac implements Cloneable { if (algorithm == null) { throw new NullPointerException(); } - synchronized (engine) { - engine.getInstance(algorithm, null); - return new Mac((MacSpi) engine.spi, engine.provider, algorithm); + synchronized (ENGINE) { + ENGINE.getInstance(algorithm, null); + return new Mac((MacSpi) ENGINE.getSpi(), ENGINE.getProvider(), algorithm); } } @@ -167,9 +167,9 @@ public class Mac implements Cloneable { if (algorithm == null) { throw new NullPointerException(); } - synchronized (engine) { - engine.getInstance(algorithm, provider, null); - return new Mac((MacSpi) engine.spi, provider, algorithm); + synchronized (ENGINE) { + ENGINE.getInstance(algorithm, provider, null); + return new Mac((MacSpi) ENGINE.getSpi(), provider, algorithm); } } diff --git a/luni/src/main/java/javax/crypto/SecretKeyFactory.java b/luni/src/main/java/javax/crypto/SecretKeyFactory.java index 05dc1ba..f4c15eb 100644 --- a/luni/src/main/java/javax/crypto/SecretKeyFactory.java +++ b/luni/src/main/java/javax/crypto/SecretKeyFactory.java @@ -42,7 +42,7 @@ import org.apache.harmony.security.fortress.Engine; public class SecretKeyFactory { // Used to access common engine functionality - private static final Engine engine = new Engine("SecretKeyFactory"); + private static final Engine ENGINE = new Engine("SecretKeyFactory"); // Store used provider private final Provider provider; @@ -105,10 +105,10 @@ public class SecretKeyFactory { if (algorithm == null) { throw new NullPointerException(); } - synchronized (engine) { - engine.getInstance(algorithm, null); - return new SecretKeyFactory((SecretKeyFactorySpi) engine.spi, - engine.provider, algorithm); + synchronized (ENGINE) { + ENGINE.getInstance(algorithm, null); + return new SecretKeyFactory((SecretKeyFactorySpi) ENGINE.getSpi(), + ENGINE.getProvider(), algorithm); } } @@ -170,9 +170,9 @@ public class SecretKeyFactory { if (algorithm == null) { throw new NullPointerException(); } - synchronized (engine) { - engine.getInstance(algorithm, provider, null); - return new SecretKeyFactory((SecretKeyFactorySpi) engine.spi, provider, + synchronized (ENGINE) { + ENGINE.getInstance(algorithm, provider, null); + return new SecretKeyFactory((SecretKeyFactorySpi) ENGINE.getSpi(), provider, algorithm); } } |