# HG changeset patch # User coffeys # Date 1535020634 -3600 # Node ID c9a3e3cac9c733a434dbb7cc53a808f99c532c65 # Parent 0265a70ea2a5ee10e761e8b8652edaf3d4606d44 8209129: Further improvements to cipher buffer management Reviewed-by: weijun, igerasim diff -r 0265a70ea2a5 -r c9a3e3cac9c7 src/java.base/share/classes/com/sun/crypto/provider/HmacPKCS12PBESHA1.java --- a/src/java.base/share/classes/com/sun/crypto/provider/HmacPKCS12PBESHA1.java Thu Aug 23 10:52:27 2018 +0200 +++ b/src/java.base/share/classes/com/sun/crypto/provider/HmacPKCS12PBESHA1.java Thu Aug 23 11:37:14 2018 +0100 @@ -73,62 +73,69 @@ salt = pbeKey.getSalt(); // maybe null if unspecified iCount = pbeKey.getIterationCount(); // maybe 0 if unspecified } else if (key instanceof SecretKey) { - byte[] passwdBytes = key.getEncoded(); - if ((passwdBytes == null) || - !(key.getAlgorithm().regionMatches(true, 0, "PBE", 0, 3))) { + byte[] passwdBytes; + if (!(key.getAlgorithm().regionMatches(true, 0, "PBE", 0, 3)) || + (passwdBytes = key.getEncoded()) == null) { throw new InvalidKeyException("Missing password"); } passwdChars = new char[passwdBytes.length]; for (int i=0; i { - java.util.Arrays.fill(k, (byte)0x00); - java.util.Arrays.fill(p, '0'); - }); + // Use the cleaner to zero the key when no longer referenced + final byte[] k = this.key; + final char[] p = this.passwd; + CleanerFactory.cleaner().register(this, + () -> { + Arrays.fill(k, (byte) 0x00); + Arrays.fill(p, '\0'); + }); + } } private static byte[] deriveKey(final Mac prf, final byte[] password, @@ -266,8 +270,8 @@ if (!(that.getFormat().equalsIgnoreCase("RAW"))) return false; byte[] thatEncoded = that.getEncoded(); - boolean ret = MessageDigest.isEqual(key, that.getEncoded()); - java.util.Arrays.fill(thatEncoded, (byte)0x00); + boolean ret = MessageDigest.isEqual(key, thatEncoded); + Arrays.fill(thatEncoded, (byte)0x00); return ret; } diff -r 0265a70ea2a5 -r c9a3e3cac9c7 src/java.base/share/classes/com/sun/crypto/provider/PBMAC1Core.java --- a/src/java.base/share/classes/com/sun/crypto/provider/PBMAC1Core.java Thu Aug 23 10:52:27 2018 +0200 +++ b/src/java.base/share/classes/com/sun/crypto/provider/PBMAC1Core.java Thu Aug 23 11:37:14 2018 +0100 @@ -108,72 +108,76 @@ salt = pbeKey.getSalt(); // maybe null if unspecified iCount = pbeKey.getIterationCount(); // maybe 0 if unspecified } else if (key instanceof SecretKey) { - byte[] passwdBytes = key.getEncoded(); - if ((passwdBytes == null) || - !(key.getAlgorithm().regionMatches(true, 0, "PBE", 0, 3))) { + byte[] passwdBytes; + if (!(key.getAlgorithm().regionMatches(true, 0, "PBE", 0, 3)) || + (passwdBytes = key.getEncoded()) == null) { throw new InvalidKeyException("Missing password"); } passwdChars = new char[passwdBytes.length]; for (int i=0; i