Skip to content
Permalink
Browse files
8258915: Temporary buffer cleanup
Reviewed-by: valeriep
  • Loading branch information
wangweij committed Apr 22, 2021
1 parent 31d8a19 commit f834557ae0ddc5a0c54aeec952c0b3391d70c3ac
Showing with 1,510 additions and 1,032 deletions.
  1. +9 −4 src/java.base/share/classes/com/sun/crypto/provider/AESCipher.java
  2. +16 −2 src/java.base/share/classes/com/sun/crypto/provider/AESCrypt.java
  3. +3 −1 src/java.base/share/classes/com/sun/crypto/provider/AESKeyGenerator.java
  4. +71 −56 src/java.base/share/classes/com/sun/crypto/provider/AESWrapCipher.java
  5. +21 −4 src/java.base/share/classes/com/sun/crypto/provider/ARCFOURCipher.java
  6. +6 −2 src/java.base/share/classes/com/sun/crypto/provider/BlowfishCipher.java
  7. +7 −2 src/java.base/share/classes/com/sun/crypto/provider/BlowfishKeyGenerator.java
  8. +9 −1 src/java.base/share/classes/com/sun/crypto/provider/ChaCha20Cipher.java
  9. +96 −79 src/java.base/share/classes/com/sun/crypto/provider/CipherCore.java
  10. +0 −259 src/java.base/share/classes/com/sun/crypto/provider/CipherWithWrappingSpi.java
  11. +6 −5 src/java.base/share/classes/com/sun/crypto/provider/ConstructKeys.java
  12. +3 −1 src/java.base/share/classes/com/sun/crypto/provider/DESCipher.java
  13. +2 −2 src/java.base/share/classes/com/sun/crypto/provider/DESKey.java
  14. +21 −11 src/java.base/share/classes/com/sun/crypto/provider/DESKeyFactory.java
  15. +3 −1 src/java.base/share/classes/com/sun/crypto/provider/DESKeyGenerator.java
  16. +3 −1 src/java.base/share/classes/com/sun/crypto/provider/DESedeCipher.java
  17. +4 −4 src/java.base/share/classes/com/sun/crypto/provider/DESedeKey.java
  18. +20 −8 src/java.base/share/classes/com/sun/crypto/provider/DESedeKeyFactory.java
  19. +94 −63 src/java.base/share/classes/com/sun/crypto/provider/DESedeWrapCipher.java
  20. +14 −7 src/java.base/share/classes/com/sun/crypto/provider/DHKeyFactory.java
  21. +32 −15 src/java.base/share/classes/com/sun/crypto/provider/DHPrivateKey.java
  22. +7 −2 src/java.base/share/classes/com/sun/crypto/provider/HmacMD5KeyGenerator.java
  23. +7 −3 src/java.base/share/classes/com/sun/crypto/provider/HmacSHA1KeyGenerator.java
  24. +7 −2 src/java.base/share/classes/com/sun/crypto/provider/KeyGeneratorCore.java
  25. +12 −4 src/java.base/share/classes/com/sun/crypto/provider/KeyProtector.java
  26. +4 −4 src/java.base/share/classes/com/sun/crypto/provider/PBEKey.java
  27. +7 −3 src/java.base/share/classes/com/sun/crypto/provider/PBEKeyFactory.java
  28. +8 −5 src/java.base/share/classes/com/sun/crypto/provider/PBES1Core.java
  29. +6 −4 src/java.base/share/classes/com/sun/crypto/provider/PBES2Core.java
  30. +26 −9 src/java.base/share/classes/com/sun/crypto/provider/PBKDF2Core.java
  31. +26 −9 src/java.base/share/classes/com/sun/crypto/provider/PBKDF2HmacSHA1Factory.java
  32. +9 −4 src/java.base/share/classes/com/sun/crypto/provider/PBKDF2KeyImpl.java
  33. +11 −4 src/java.base/share/classes/com/sun/crypto/provider/PBMAC1Core.java
  34. +47 −31 src/java.base/share/classes/com/sun/crypto/provider/PKCS12PBECipherCore.java
  35. +24 −14 src/java.base/share/classes/com/sun/crypto/provider/PrivateKeyInfo.java
  36. +3 −1 src/java.base/share/classes/com/sun/crypto/provider/RC2Cipher.java
  37. +80 −75 src/java.base/share/classes/com/sun/crypto/provider/TlsKeyMaterialGenerator.java
  38. +10 −3 src/java.base/share/classes/com/sun/crypto/provider/TlsMasterSecretGenerator.java
  39. +15 −2 src/java.base/share/classes/com/sun/crypto/provider/TlsPrfGenerator.java
  40. +7 −2 src/java.base/share/classes/com/sun/crypto/provider/TlsRsaPremasterSecretGenerator.java
  41. +17 −1 src/java.base/share/classes/java/math/BigInteger.java
  42. +8 −2 src/java.base/share/classes/java/math/MutableBigInteger.java
  43. +24 −2 src/java.base/share/classes/java/security/spec/EncodedKeySpec.java
  44. +4 −2 src/java.base/share/classes/javax/crypto/SealedObject.java
  45. +28 −2 src/java.base/share/classes/javax/crypto/spec/SecretKeySpec.java
  46. +32 −0 src/java.base/share/classes/jdk/internal/access/JavaSecuritySpecAccess.java
  47. +32 −0 src/java.base/share/classes/jdk/internal/access/JavaxCryptoSpecAccess.java
  48. +26 −0 src/java.base/share/classes/jdk/internal/access/SharedSecrets.java
  49. +62 −34 src/java.base/share/classes/sun/security/pkcs/PKCS8Key.java
  50. +132 −61 src/java.base/share/classes/sun/security/pkcs12/PKCS12KeyStore.java
  51. +9 −6 src/java.base/share/classes/sun/security/provider/CtrDrbg.java
  52. +14 −6 src/java.base/share/classes/sun/security/provider/DSAKeyFactory.java
  53. +7 −3 src/java.base/share/classes/sun/security/provider/DSAPrivateKey.java
  54. +6 −6 src/java.base/share/classes/sun/security/provider/HashDrbg.java
  55. +5 −2 src/java.base/share/classes/sun/security/provider/KeyProtector.java
  56. +5 −1 src/java.base/share/classes/sun/security/provider/SHAKE256.java
  57. +21 −5 src/java.base/share/classes/sun/security/rsa/RSAKeyFactory.java
  58. +62 −33 src/java.base/share/classes/sun/security/rsa/RSAPrivateCrtKeyImpl.java
  59. +13 −6 src/java.base/share/classes/sun/security/rsa/RSAPrivateKeyImpl.java
  60. +16 −1 src/java.base/share/classes/sun/security/util/DerOutputStream.java
  61. +28 −3 src/java.base/share/classes/sun/security/util/DerValue.java
  62. +8 −3 src/java.base/share/classes/sun/security/util/ECUtil.java
  63. +7 −2 src/java.base/share/classes/sun/security/util/KeyUtil.java
  64. +26 −52 src/java.base/share/classes/sun/security/x509/AlgIdDSA.java
  65. +15 −29 src/java.base/share/classes/sun/security/x509/AlgorithmId.java
  66. +2 −2 src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11Key.java
  67. +19 −3 src/jdk.crypto.ec/share/classes/sun/security/ec/ECKeyFactory.java
  68. +3 −1 src/jdk.crypto.ec/share/classes/sun/security/ec/ECKeyPairGenerator.java
  69. +10 −5 src/jdk.crypto.ec/share/classes/sun/security/ec/ECPrivateKeyImpl.java
  70. +36 −12 src/jdk.crypto.ec/share/classes/sun/security/ec/XDHKeyFactory.java
  71. +7 −2 src/jdk.crypto.ec/share/classes/sun/security/ec/XDHKeyPairGenerator.java
  72. +6 −4 src/jdk.crypto.ec/share/classes/sun/security/ec/XDHPrivateKeyImpl.java
  73. +38 −13 src/jdk.crypto.ec/share/classes/sun/security/ec/ed/EdDSAKeyFactory.java
  74. +4 −1 src/jdk.crypto.ec/share/classes/sun/security/ec/ed/EdDSAKeyPairGenerator.java
  75. +11 −3 src/jdk.crypto.ec/share/classes/sun/security/ec/ed/EdDSAParameters.java
  76. +5 −4 src/jdk.crypto.ec/share/classes/sun/security/ec/ed/EdDSAPrivateKeyImpl.java
  77. +2 −3 test/jdk/sun/security/pkcs/pkcs8/PKCS8Test.java
  78. +2 −4 test/jdk/sun/security/pkcs11/rsa/TestKeyFactory.java
  79. +2 −4 test/jdk/sun/security/rsa/TestKeyFactory.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -40,6 +40,7 @@
import java.security.ProviderException;
import java.security.SecureRandom;
import java.security.spec.AlgorithmParameterSpec;
import java.util.Arrays;

/**
* This class implements the AES algorithm in its various modes
@@ -163,9 +164,12 @@ static final void checkKeySize(Key key, int fixedKeySize)
byte[] value = key.getEncoded();
if (value == null) {
throw new InvalidKeyException("Key encoding must not be null");
} else if (value.length != fixedKeySize) {
throw new InvalidKeyException("The key must be " +
fixedKeySize + " bytes");
} else {
Arrays.fill(value, (byte)0);
if (value.length != fixedKeySize) {
throw new InvalidKeyException("The key must be " +
fixedKeySize + " bytes");
}
}
}
}
@@ -515,6 +519,7 @@ protected int engineDoFinal(byte[] input, int inputOffset, int inputLen,
*/
protected int engineGetKeySize(Key key) throws InvalidKeyException {
byte[] encoded = key.getEncoded();
Arrays.fill(encoded, (byte)0);
if (!AESCrypt.isKeySizeValid(encoded.length)) {
throw new InvalidKeyException("Invalid AES key length: " +
encoded.length + " bytes");
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -94,6 +94,9 @@ void init(boolean decrypting, String algorithm, byte[] key)
if (!MessageDigest.isEqual(key, lastKey)) {
// re-generate session key 'sessionK' when cipher key changes
makeSessionKey(key);
if (lastKey != null) {
Arrays.fill(lastKey, (byte)0);
}
lastKey = key.clone(); // save cipher key
}

@@ -673,12 +676,23 @@ private void makeSessionKey(byte[] k) throws InvalidKeyException {
// and expand them into arrays of ints.
int[] expandedKe = expandToSubKey(Ke, false); // decrypting==false
int[] expandedKd = expandToSubKey(Kd, true); // decrypting==true

Arrays.fill(tk, 0);
for (int[] ia: Ke) {
Arrays.fill(ia, 0);
}
for (int[] ia: Kd) {
Arrays.fill(ia, 0);
}
ROUNDS_12 = (ROUNDS>=12);
ROUNDS_14 = (ROUNDS==14);
limit = ROUNDS*4;

// store the expanded sub keys into 'sessionK'
if (sessionK != null) {
// erase the previous values in sessionK
Arrays.fill(sessionK[0], 0);
Arrays.fill(sessionK[1], 0);
}
sessionK = new int[][] { expandedKe, expandedKd };
}

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -30,6 +30,7 @@
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.spec.AlgorithmParameterSpec;
import java.util.Arrays;
import javax.crypto.KeyGeneratorSpi;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
@@ -112,6 +113,7 @@ protected SecretKey engineGenerateKey() {
byte[] keyBytes = new byte[keySize];
this.random.nextBytes(keyBytes);
aesKey = new SecretKeySpec(keyBytes, "AES");
Arrays.fill(keyBytes, (byte)0);
return aesKey;
}
}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2004, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2004, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -198,7 +198,12 @@ protected void engineInit(int opmode, Key key, SecureRandom random)
"only be used for key wrapping and unwrapping");
}
AESCipher.checkKeySize(key, fixedKeySize);
cipher.init(decrypting, key.getAlgorithm(), key.getEncoded());
byte[] encoded = key.getEncoded();
try {
cipher.init(decrypting, key.getAlgorithm(), encoded);
} finally {
Arrays.fill(encoded, (byte)0);
}
}

/**
@@ -374,6 +379,7 @@ protected AlgorithmParameters engineGetParameters() {
*/
protected int engineGetKeySize(Key key) throws InvalidKeyException {
byte[] encoded = key.getEncoded();
Arrays.fill(encoded, (byte)0);
if (!AESCrypt.isKeySizeValid(encoded.length)) {
throw new InvalidKeyException("Invalid key length: " +
encoded.length + " bytes");
@@ -404,38 +410,42 @@ protected int engineGetKeySize(Key key) throws InvalidKeyException {
throw new InvalidKeyException("Cannot get an encoding of " +
"the key to be wrapped");
}
byte[] out = new byte[Math.addExact(keyVal.length, 8)];
try {
byte[] out = new byte[Math.addExact(keyVal.length, 8)];

if (keyVal.length == 8) {
System.arraycopy(IV, 0, out, 0, IV.length);
System.arraycopy(keyVal, 0, out, IV.length, 8);
cipher.encryptBlock(out, 0, out, 0);
} else {
if (keyVal.length % 8 != 0) {
throw new IllegalBlockSizeException("length of the " +
"to be wrapped key should be multiples of 8 bytes");
}
System.arraycopy(IV, 0, out, 0, IV.length);
System.arraycopy(keyVal, 0, out, IV.length, keyVal.length);
int N = keyVal.length/8;
byte[] buffer = new byte[blksize];
for (int j = 0; j < 6; j++) {
for (int i = 1; i <= N; i++) {
int T = i + j*N;
System.arraycopy(out, 0, buffer, 0, IV.length);
System.arraycopy(out, i*8, buffer, IV.length, 8);
cipher.encryptBlock(buffer, 0, buffer, 0);
for (int k = 1; T != 0; k++) {
byte v = (byte) T;
buffer[IV.length - k] ^= v;
T >>>= 8;
if (keyVal.length == 8) {
System.arraycopy(IV, 0, out, 0, IV.length);
System.arraycopy(keyVal, 0, out, IV.length, 8);
cipher.encryptBlock(out, 0, out, 0);
} else {
if (keyVal.length % 8 != 0) {
throw new IllegalBlockSizeException("length of the " +
"to be wrapped key should be multiples of 8 bytes");
}
System.arraycopy(IV, 0, out, 0, IV.length);
System.arraycopy(keyVal, 0, out, IV.length, keyVal.length);
int N = keyVal.length / 8;
byte[] buffer = new byte[blksize];
for (int j = 0; j < 6; j++) {
for (int i = 1; i <= N; i++) {
int T = i + j * N;
System.arraycopy(out, 0, buffer, 0, IV.length);
System.arraycopy(out, i * 8, buffer, IV.length, 8);
cipher.encryptBlock(buffer, 0, buffer, 0);
for (int k = 1; T != 0; k++) {
byte v = (byte) T;
buffer[IV.length - k] ^= v;
T >>>= 8;
}
System.arraycopy(buffer, 0, out, 0, IV.length);
System.arraycopy(buffer, 8, out, 8 * i, 8);
}
System.arraycopy(buffer, 0, out, 0, IV.length);
System.arraycopy(buffer, 8, out, 8*i, 8);
}
}
return out;
} finally {
Arrays.fill(keyVal, (byte)0);
}
return out;
}

/**
@@ -474,38 +484,43 @@ protected Key engineUnwrap(byte[] wrappedKey,
}
byte[] out = new byte[wrappedKeyLen - 8];
byte[] buffer = new byte[blksize];
if (wrappedKeyLen == 16) {
cipher.decryptBlock(wrappedKey, 0, buffer, 0);
for (int i = 0; i < IV.length; i++) {
if (IV[i] != buffer[i]) {
throw new InvalidKeyException("Integrity check failed");
try {
if (wrappedKeyLen == 16) {
cipher.decryptBlock(wrappedKey, 0, buffer, 0);
for (int i = 0; i < IV.length; i++) {
if (IV[i] != buffer[i]) {
throw new InvalidKeyException("Integrity check failed");
}
}
}
System.arraycopy(buffer, IV.length, out, 0, out.length);
} else {
System.arraycopy(wrappedKey, 0, buffer, 0, IV.length);
System.arraycopy(wrappedKey, IV.length, out, 0, out.length);
int N = out.length/8;
for (int j = 5; j >= 0; j--) {
for (int i = N; i > 0; i--) {
int T = i + j*N;
System.arraycopy(out, 8*(i-1), buffer, IV.length, 8);
for (int k = 1; T != 0; k++) {
byte v = (byte) T;
buffer[IV.length - k] ^= v;
T >>>= 8;
System.arraycopy(buffer, IV.length, out, 0, out.length);
} else {
System.arraycopy(wrappedKey, 0, buffer, 0, IV.length);
System.arraycopy(wrappedKey, IV.length, out, 0, out.length);
int N = out.length / 8;
for (int j = 5; j >= 0; j--) {
for (int i = N; i > 0; i--) {
int T = i + j * N;
System.arraycopy(out, 8 * (i - 1), buffer, IV.length, 8);
for (int k = 1; T != 0; k++) {
byte v = (byte) T;
buffer[IV.length - k] ^= v;
T >>>= 8;
}
cipher.decryptBlock(buffer, 0, buffer, 0);
System.arraycopy(buffer, IV.length, out, 8 * (i - 1), 8);
}
cipher.decryptBlock(buffer, 0, buffer, 0);
System.arraycopy(buffer, IV.length, out, 8*(i-1), 8);
}
}
for (int i = 0; i < IV.length; i++) {
if (IV[i] != buffer[i]) {
throw new InvalidKeyException("Integrity check failed");
for (int i = 0; i < IV.length; i++) {
if (IV[i] != buffer[i]) {
throw new InvalidKeyException("Integrity check failed");
}
}
}
return ConstructKeys.constructKey(out, wrappedKeyAlgorithm,
wrappedKeyType);
} finally {
Arrays.fill(out, (byte)0);
Arrays.fill(buffer, (byte)0);
}
return ConstructKeys.constructKey(out, wrappedKeyAlgorithm,
wrappedKeyType);
}
}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -27,6 +27,7 @@

import java.security.*;
import java.security.spec.AlgorithmParameterSpec;
import java.util.Arrays;

import javax.crypto.*;

@@ -180,6 +181,9 @@ protected void engineInit(int opmode, Key key,

// init method. Check opmode and key, then call init(byte[]).
private void init(int opmode, Key key) throws InvalidKeyException {
if (lastKey != null) {
Arrays.fill(lastKey, (byte)0);
}
if ((opmode < Cipher.ENCRYPT_MODE) || (opmode > Cipher.UNWRAP_MODE)) {
throw new InvalidKeyException("Unknown opmode: " + opmode);
}
@@ -199,6 +203,7 @@ private void init(int opmode, Key key) throws InvalidKeyException {
}
byte[] encodedKey = key.getEncoded();
if ((encodedKey.length < 5) || (encodedKey.length > 128)) {
Arrays.fill(encodedKey, (byte)0);
throw new InvalidKeyException
("Key length must be between 40 and 1024 bit");
}
@@ -244,19 +249,31 @@ protected int engineDoFinal(byte[] in, int inOfs, int inLen,
if ((encoded == null) || (encoded.length == 0)) {
throw new InvalidKeyException("Could not obtain encoded key");
}
return engineDoFinal(encoded, 0, encoded.length);
try {
return engineDoFinal(encoded, 0, encoded.length);
} finally {
Arrays.fill(encoded, (byte)0);
}
}

// see JCE spec
protected Key engineUnwrap(byte[] wrappedKey, String algorithm,
int type) throws InvalidKeyException, NoSuchAlgorithmException {
byte[] encoded = engineDoFinal(wrappedKey, 0, wrappedKey.length);
return ConstructKeys.constructKey(encoded, algorithm, type);
byte[] encoded = null;
try {
encoded = engineDoFinal(wrappedKey, 0, wrappedKey.length);
return ConstructKeys.constructKey(encoded, algorithm, type);
} finally {
if (encoded != null) {
Arrays.fill(encoded, (byte) 0);
}
}
}

// see JCE spec
protected int engineGetKeySize(Key key) throws InvalidKeyException {
byte[] encodedKey = getEncodedKey(key);
Arrays.fill(encodedKey, (byte)0);
return Math.multiplyExact(encodedKey.length, 8);
}

@@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -27,6 +27,8 @@

import java.security.*;
import java.security.spec.*;
import java.util.Arrays;

import sun.security.util.*;
import javax.crypto.*;
import javax.crypto.spec.*;
@@ -373,7 +375,9 @@ protected int engineDoFinal(byte[] input, int inputOffset, int inputLen,
* @exception InvalidKeyException if <code>key</code> is invalid.
*/
protected int engineGetKeySize(Key key) throws InvalidKeyException {
return Math.multiplyExact(key.getEncoded().length, 8);
byte[] encodedKey = key.getEncoded();
Arrays.fill(encodedKey, (byte)0);
return Math.multiplyExact(encodedKey.length, 8);
}

/**
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -29,6 +29,7 @@
import java.security.InvalidParameterException;
import java.security.InvalidAlgorithmParameterException;
import java.security.spec.AlgorithmParameterSpec;
import java.util.Arrays;
import javax.crypto.KeyGeneratorSpi;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
@@ -110,6 +111,10 @@ protected SecretKey engineGenerateKey() {
byte[] keyBytes = new byte[this.keysize];
this.random.nextBytes(keyBytes);

return new SecretKeySpec(keyBytes, "Blowfish");
try {
return new SecretKeySpec(keyBytes, "Blowfish");
} finally {
Arrays.fill(keyBytes, (byte)0);
}
}
}
Loading

1 comment on commit f834557

@openjdk-notifier

This comment has been minimized.

Copy link

@openjdk-notifier openjdk-notifier bot commented on f834557 Apr 22, 2021

Please sign in to comment.