Skip to content
This repository has been archived by the owner on Sep 2, 2022. It is now read-only.

8268525: Some new memory leak after JDK-8248268 and JDK-8255557 #3

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
76 changes: 40 additions & 36 deletions src/java.base/share/classes/com/sun/crypto/provider/CipherCore.java
Expand Up @@ -435,50 +435,54 @@ void init(int opmode, Key key, AlgorithmParameterSpec params,

byte[] keyBytes = getKeyBytes(key);
byte[] ivBytes = null;
if (params != null) {
if (params instanceof IvParameterSpec) {
ivBytes = ((IvParameterSpec) params).getIV();
if ((ivBytes == null) || (ivBytes.length != blockSize)) {
try {
if (params != null) {
if (params instanceof IvParameterSpec) {
ivBytes = ((IvParameterSpec) params).getIV();
if ((ivBytes == null) || (ivBytes.length != blockSize)) {
throw new InvalidAlgorithmParameterException
("Wrong IV length: must be " + blockSize +
" bytes long");
}
} else if (params instanceof RC2ParameterSpec) {
ivBytes = ((RC2ParameterSpec) params).getIV();
if ((ivBytes != null) && (ivBytes.length != blockSize)) {
throw new InvalidAlgorithmParameterException
("Wrong IV length: must be " + blockSize +
" bytes long");
}
} else {
throw new InvalidAlgorithmParameterException
("Wrong IV length: must be " + blockSize +
" bytes long");
("Unsupported parameter: " + params);
}
} else if (params instanceof RC2ParameterSpec) {
ivBytes = ((RC2ParameterSpec) params).getIV();
if ((ivBytes != null) && (ivBytes.length != blockSize)) {
}
if (cipherMode == ECB_MODE) {
if (ivBytes != null) {
throw new InvalidAlgorithmParameterException
("Wrong IV length: must be " + blockSize +
" bytes long");
("ECB mode cannot use IV");
}
} else if (ivBytes == null) {
if (decrypting) {
throw new InvalidAlgorithmParameterException("Parameters "
+ "missing");
}
} else {
throw new InvalidAlgorithmParameterException
("Unsupported parameter: " + params);
}
}
if (cipherMode == ECB_MODE) {
if (ivBytes != null) {
throw new InvalidAlgorithmParameterException
("ECB mode cannot use IV");
}
} else if (ivBytes == null) {
if (decrypting) {
throw new InvalidAlgorithmParameterException("Parameters "
+ "missing");
}

if (random == null) {
random = SunJCE.getRandom();
}
if (random == null) {
random = SunJCE.getRandom();
}

ivBytes = new byte[blockSize];
random.nextBytes(ivBytes);
}
ivBytes = new byte[blockSize];
random.nextBytes(ivBytes);
}

buffered = 0;
diffBlocksize = blockSize;
buffered = 0;
diffBlocksize = blockSize;

String algorithm = key.getAlgorithm();
cipher.init(decrypting, algorithm, keyBytes, ivBytes);
String algorithm = key.getAlgorithm();
cipher.init(decrypting, algorithm, keyBytes, ivBytes);
} finally {
Arrays.fill(keyBytes, (byte)0);
}
}

void init(int opmode, Key key, AlgorithmParameters params,
Expand Down
Expand Up @@ -25,6 +25,8 @@

package com.sun.crypto.provider;

import jdk.internal.access.SharedSecrets;

import java.security.Key;
import java.security.PublicKey;
import java.security.PrivateKey;
Expand Down Expand Up @@ -111,20 +113,18 @@ private static final PrivateKey constructPrivateKey(byte[] encodedKey,
throws InvalidKeyException, NoSuchAlgorithmException {
PrivateKey key = null;

PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encodedKey);
try {
KeyFactory keyFactory =
KeyFactory.getInstance(encodedKeyAlgorithm,
SunJCE.getInstance());
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encodedKey);
return keyFactory.generatePrivate(keySpec);
} catch (NoSuchAlgorithmException nsae) {
// Try to see whether there is another
// provider which supports this algorithm
try {
KeyFactory keyFactory =
KeyFactory.getInstance(encodedKeyAlgorithm);
PKCS8EncodedKeySpec keySpec =
new PKCS8EncodedKeySpec(encodedKey);
key = keyFactory.generatePrivate(keySpec);
} catch (NoSuchAlgorithmException nsae2) {
throw new NoSuchAlgorithmException("No installed providers " +
Expand All @@ -142,6 +142,8 @@ private static final PrivateKey constructPrivateKey(byte[] encodedKey,
new InvalidKeyException("Cannot construct private key");
ike.initCause(ikse);
throw ike;
} finally {
SharedSecrets.getJavaSecuritySpecAccess().clearEncodedKeySpec(keySpec);
}

return key;
Expand Down
Expand Up @@ -163,7 +163,13 @@ void init(int opmode, Key key, GCMParameterSpec spec)
reInit = false;

// always encrypt mode for embedded cipher
blockCipher.init(false, key.getAlgorithm(), keyValue);
try {
blockCipher.init(false, key.getAlgorithm(), keyValue);
} finally {
if (!encryption) {
Arrays.fill(keyValue, (byte) 0);
}
}
}

@Override
Expand Down
Expand Up @@ -124,6 +124,7 @@ static final int W_INV(byte[] in, int inLen, byte[] icvOut,
}
}
System.arraycopy(buffer, 0, icvOut, 0, SEMI_BLKSIZE);
Arrays.fill(buffer, (byte)0);
return inLen - SEMI_BLKSIZE;
}
}
Expand Up @@ -472,7 +472,11 @@ protected byte[] engineDoFinal(byte[] in, int inOfs, int inLen)
int outLen = engineDoFinal(in, inOfs, inLen, out, 0);

if (outLen < estOutLen) {
return Arrays.copyOf(out, outLen);
try {
return Arrays.copyOf(out, outLen);
} finally {
Arrays.fill(out, (byte)0);
}
} else {
return out;
}
Expand Down Expand Up @@ -529,6 +533,9 @@ protected int engineDoFinal(byte[] in, int inOfs, int inLen,
return outLen;
}
} finally {
if (dataBuf != null) {
Arrays.fill(dataBuf, (byte)0);
}
dataBuf = null;
dataIdx = 0;
}
Expand Down Expand Up @@ -559,8 +566,14 @@ private int implDoFinal(byte[] in, int inOfs, int inLen, byte[] out)
len += inLen;
}

return (opmode == Cipher.ENCRYPT_MODE?
helperEncrypt(out, len) : helperDecrypt(out, len));
try {
return (opmode == Cipher.ENCRYPT_MODE ?
helperEncrypt(out, len) : helperDecrypt(out, len));
} finally {
if (dataBuf != null && dataBuf != out) {
Arrays.fill(dataBuf, (byte)0);
}
}
}

// helper routine for in-place encryption.
Expand Down