提交 a42ac139 编写于 作者: V valeriep

8074865: General crypto resilience changes

Reviewed-by: mullan, xuelei
上级 67d79b25
/* /*
* Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2002, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -37,7 +37,7 @@ ...@@ -37,7 +37,7 @@
package com.sun.crypto.provider; package com.sun.crypto.provider;
import java.security.InvalidKeyException; import java.security.InvalidKeyException;
import java.util.Arrays; import java.security.MessageDigest;
/** /**
* Rijndael --pronounced Reindaal-- is a symmetric cipher with a 128-bit * Rijndael --pronounced Reindaal-- is a symmetric cipher with a 128-bit
...@@ -88,7 +88,7 @@ final class AESCrypt extends SymmetricCipher implements AESConstants ...@@ -88,7 +88,7 @@ final class AESCrypt extends SymmetricCipher implements AESConstants
key.length + " bytes"); key.length + " bytes");
} }
if (!Arrays.equals(key, lastKey)) { if (!MessageDigest.isEqual(key, lastKey)) {
// re-generate session key 'sessionK' when cipher key changes // re-generate session key 'sessionK' when cipher key changes
makeSessionKey(key); makeSessionKey(key);
lastKey = key.clone(); // save cipher key lastKey = key.clone(); // save cipher key
......
/* /*
* Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2002, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -568,7 +568,7 @@ final class CipherCore { ...@@ -568,7 +568,7 @@ final class CipherCore {
// check key+iv for encryption in GCM mode // check key+iv for encryption in GCM mode
requireReinit = requireReinit =
Arrays.equals(ivBytes, lastEncIv) && Arrays.equals(ivBytes, lastEncIv) &&
Arrays.equals(keyBytes, lastEncKey); MessageDigest.isEqual(keyBytes, lastEncKey);
if (requireReinit) { if (requireReinit) {
throw new InvalidAlgorithmParameterException throw new InvalidAlgorithmParameterException
("Cannot reuse iv for GCM encryption"); ("Cannot reuse iv for GCM encryption");
......
/* /*
* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
package com.sun.crypto.provider; package com.sun.crypto.provider;
import java.security.MessageDigest;
import java.security.KeyRep; import java.security.KeyRep;
import java.security.InvalidKeyException; import java.security.InvalidKeyException;
import javax.crypto.SecretKey; import javax.crypto.SecretKey;
...@@ -113,7 +114,7 @@ final class DESKey implements SecretKey { ...@@ -113,7 +114,7 @@ final class DESKey implements SecretKey {
return false; return false;
byte[] thatKey = ((SecretKey)obj).getEncoded(); byte[] thatKey = ((SecretKey)obj).getEncoded();
boolean ret = java.util.Arrays.equals(this.key, thatKey); boolean ret = MessageDigest.isEqual(this.key, thatKey);
java.util.Arrays.fill(thatKey, (byte)0x00); java.util.Arrays.fill(thatKey, (byte)0x00);
return ret; return ret;
} }
......
/* /*
* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
package com.sun.crypto.provider; package com.sun.crypto.provider;
import java.security.MessageDigest;
import java.security.KeyRep; import java.security.KeyRep;
import java.security.InvalidKeyException; import java.security.InvalidKeyException;
import javax.crypto.SecretKey; import javax.crypto.SecretKey;
...@@ -114,7 +115,7 @@ final class DESedeKey implements SecretKey { ...@@ -114,7 +115,7 @@ final class DESedeKey implements SecretKey {
return false; return false;
byte[] thatKey = ((SecretKey)obj).getEncoded(); byte[] thatKey = ((SecretKey)obj).getEncoded();
boolean ret = java.util.Arrays.equals(this.key, thatKey); boolean ret = MessageDigest.isEqual(this.key, thatKey);
java.util.Arrays.fill(thatKey, (byte)0x00); java.util.Arrays.fill(thatKey, (byte)0x00);
return ret; return ret;
} }
......
/* /*
* Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
package com.sun.crypto.provider; package com.sun.crypto.provider;
import java.security.MessageDigest;
import java.security.KeyRep; import java.security.KeyRep;
import java.security.spec.InvalidKeySpecException; import java.security.spec.InvalidKeySpecException;
import javax.crypto.SecretKey; import javax.crypto.SecretKey;
...@@ -107,7 +108,7 @@ final class PBEKey implements SecretKey { ...@@ -107,7 +108,7 @@ final class PBEKey implements SecretKey {
return false; return false;
byte[] thatEncoded = that.getEncoded(); byte[] thatEncoded = that.getEncoded();
boolean ret = java.util.Arrays.equals(this.key, thatEncoded); boolean ret = MessageDigest.isEqual(this.key, thatEncoded);
java.util.Arrays.fill(thatEncoded, (byte)0x00); java.util.Arrays.fill(thatEncoded, (byte)0x00);
return ret; return ret;
} }
......
/* /*
* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -30,6 +30,7 @@ import java.nio.ByteBuffer; ...@@ -30,6 +30,7 @@ import java.nio.ByteBuffer;
import java.nio.CharBuffer; import java.nio.CharBuffer;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.Arrays; import java.util.Arrays;
import java.security.MessageDigest;
import java.security.KeyRep; import java.security.KeyRep;
import java.security.GeneralSecurityException; import java.security.GeneralSecurityException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
...@@ -152,7 +153,7 @@ final class PBKDF2KeyImpl implements javax.crypto.interfaces.PBEKey { ...@@ -152,7 +153,7 @@ final class PBKDF2KeyImpl implements javax.crypto.interfaces.PBEKey {
SecretKey sk = (SecretKey)obj; SecretKey sk = (SecretKey)obj;
return prf.getAlgorithm().equalsIgnoreCase( return prf.getAlgorithm().equalsIgnoreCase(
sk.getAlgorithm()) && sk.getAlgorithm()) &&
Arrays.equals(password, sk.getEncoded()); MessageDigest.isEqual(password, sk.getEncoded());
} }
}; };
prf.init(macKey); prf.init(macKey);
...@@ -238,7 +239,7 @@ final class PBKDF2KeyImpl implements javax.crypto.interfaces.PBEKey { ...@@ -238,7 +239,7 @@ final class PBKDF2KeyImpl implements javax.crypto.interfaces.PBEKey {
if (!(that.getFormat().equalsIgnoreCase("RAW"))) if (!(that.getFormat().equalsIgnoreCase("RAW")))
return false; return false;
byte[] thatEncoded = that.getEncoded(); byte[] thatEncoded = that.getEncoded();
boolean ret = Arrays.equals(key, that.getEncoded()); boolean ret = MessageDigest.isEqual(key, that.getEncoded());
java.util.Arrays.fill(thatEncoded, (byte)0x00); java.util.Arrays.fill(thatEncoded, (byte)0x00);
return ret; return ret;
} }
......
/* /*
* Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1996, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -261,7 +261,7 @@ public abstract class Identity implements Principal, Serializable { ...@@ -261,7 +261,7 @@ public abstract class Identity implements Principal, Serializable {
certificates.addElement(certificate); certificates.addElement(certificate);
} }
private boolean keyEquals(Key aKey, Key anotherKey) { private boolean keyEquals(PublicKey aKey, PublicKey anotherKey) {
String aKeyFormat = aKey.getFormat(); String aKeyFormat = aKey.getFormat();
String anotherKeyFormat = anotherKey.getFormat(); String anotherKeyFormat = anotherKey.getFormat();
if ((aKeyFormat == null) ^ (anotherKeyFormat == null)) if ((aKeyFormat == null) ^ (anotherKeyFormat == null))
......
/* /*
* Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1996, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -440,6 +440,10 @@ public abstract class MessageDigest extends MessageDigestSpi { ...@@ -440,6 +440,10 @@ public abstract class MessageDigest extends MessageDigestSpi {
* @return true if the digests are equal, false otherwise. * @return true if the digests are equal, false otherwise.
*/ */
public static boolean isEqual(byte[] digesta, byte[] digestb) { public static boolean isEqual(byte[] digesta, byte[] digestb) {
if (digesta == digestb) return true;
if (digesta == null || digestb == null) {
return false;
}
if (digesta.length != digestb.length) { if (digesta.length != digestb.length) {
return false; return false;
} }
......
/* /*
* Copyright (c) 1996, 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1996, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -1316,7 +1316,7 @@ public abstract class Signature extends SignatureSpi { ...@@ -1316,7 +1316,7 @@ public abstract class Signature extends SignatureSpi {
byte[] out = cipher.doFinal(sigBytes); byte[] out = cipher.doFinal(sigBytes);
byte[] dataBytes = data.toByteArray(); byte[] dataBytes = data.toByteArray();
data.reset(); data.reset();
return Arrays.equals(out, dataBytes); return MessageDigest.isEqual(out, dataBytes);
} catch (BadPaddingException e) { } catch (BadPaddingException e) {
// e.g. wrong public key used // e.g. wrong public key used
// return false rather than throwing exception // return false rather than throwing exception
......
/* /*
* Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1998, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
package javax.crypto.spec; package javax.crypto.spec;
import java.security.MessageDigest;
import java.security.spec.KeySpec; import java.security.spec.KeySpec;
import javax.crypto.SecretKey; import javax.crypto.SecretKey;
...@@ -226,6 +227,6 @@ public class SecretKeySpec implements KeySpec, SecretKey { ...@@ -226,6 +227,6 @@ public class SecretKeySpec implements KeySpec, SecretKey {
byte[] thatKey = ((SecretKey)obj).getEncoded(); byte[] thatKey = ((SecretKey)obj).getEncoded();
return java.util.Arrays.equals(this.key, thatKey); return MessageDigest.isEqual(this.key, thatKey);
} }
} }
/* /*
* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -165,7 +165,7 @@ abstract class P11Key implements Key, Length { ...@@ -165,7 +165,7 @@ abstract class P11Key implements Key, Length {
} else { } else {
otherEnc = other.getEncoded(); otherEnc = other.getEncoded();
} }
return Arrays.equals(thisEnc, otherEnc); return MessageDigest.isEqual(thisEnc, otherEnc);
} }
public int hashCode() { public int hashCode() {
......
/* /*
* Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
*/ */
/* Copyright (c) 2002 Graz University of Technology. All rights reserved. /* Copyright (c) 2002 Graz University of Technology. All rights reserved.
...@@ -444,22 +444,6 @@ public class Functions { ...@@ -444,22 +444,6 @@ public class Functions {
return getId(objectClassIds, name); return getId(objectClassIds, name);
} }
/**
* Check the given arrays for equalitiy. This method considers both arrays as
* equal, if both are <code>null</code> or both have the same length and
* contain exactly the same byte values.
*
* @param array1 The first array.
* @param array2 The second array.
* @return True, if both arrays are <code>null</code> or both have the same
* length and contain exactly the same byte values. False, otherwise.
* @preconditions
* @postconditions
*/
public static boolean equals(byte[] array1, byte[] array2) {
return Arrays.equals(array1, array2);
}
/** /**
* Check the given arrays for equalitiy. This method considers both arrays as * Check the given arrays for equalitiy. This method considers both arrays as
* equal, if both are <code>null</code> or both have the same length and * equal, if both are <code>null</code> or both have the same length and
...@@ -472,7 +456,7 @@ public class Functions { ...@@ -472,7 +456,7 @@ public class Functions {
* @preconditions * @preconditions
* @postconditions * @postconditions
*/ */
public static boolean equals(char[] array1, char[] array2) { private static boolean equals(char[] array1, char[] array2) {
return Arrays.equals(array1, array2); return Arrays.equals(array1, array2);
} }
......
/* /*
* Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -2012,7 +2012,7 @@ public final class PKCS12KeyStore extends KeyStoreSpi { ...@@ -2012,7 +2012,7 @@ public final class PKCS12KeyStore extends KeyStoreSpi {
"(MAC algorithm: " + m.getAlgorithm() + ")"); "(MAC algorithm: " + m.getAlgorithm() + ")");
} }
if (!Arrays.equals(macData.getDigest(), macResult)) { if (!MessageDigest.isEqual(macData.getDigest(), macResult)) {
throw new SecurityException("Failed PKCS12" + throw new SecurityException("Failed PKCS12" +
" integrity checking"); " integrity checking");
} }
......
...@@ -27,7 +27,6 @@ package sun.security.rsa; ...@@ -27,7 +27,6 @@ package sun.security.rsa;
import java.io.IOException; import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.Arrays;
import java.security.*; import java.security.*;
import java.security.interfaces.*; import java.security.interfaces.*;
...@@ -194,7 +193,7 @@ public abstract class RSASignature extends SignatureSpi { ...@@ -194,7 +193,7 @@ public abstract class RSASignature extends SignatureSpi {
byte[] decrypted = RSACore.rsa(sigBytes, publicKey); byte[] decrypted = RSACore.rsa(sigBytes, publicKey);
byte[] unpadded = padding.unpad(decrypted); byte[] unpadded = padding.unpad(decrypted);
byte[] decodedDigest = decodeSignature(digestOID, unpadded); byte[] decodedDigest = decodeSignature(digestOID, unpadded);
return Arrays.equals(digest, decodedDigest); return MessageDigest.isEqual(digest, decodedDigest);
} catch (javax.crypto.BadPaddingException e) { } catch (javax.crypto.BadPaddingException e) {
// occurs if the app has used the wrong RSA public key // occurs if the app has used the wrong RSA public key
// or if sigBytes is invalid // or if sigBytes is invalid
......
...@@ -485,7 +485,7 @@ final class ClientHandshaker extends Handshaker { ...@@ -485,7 +485,7 @@ final class ClientHandshaker extends Handshaker {
0, clientVerifyData.length); 0, clientVerifyData.length);
System.arraycopy(serverVerifyData, 0, verifyData, System.arraycopy(serverVerifyData, 0, verifyData,
clientVerifyData.length, serverVerifyData.length); clientVerifyData.length, serverVerifyData.length);
if (!Arrays.equals(verifyData, if (!MessageDigest.isEqual(verifyData,
serverHelloRI.getRenegotiatedConnection())) { serverHelloRI.getRenegotiatedConnection())) {
fatalSE(Alerts.alert_handshake_failure, fatalSE(Alerts.alert_handshake_failure,
"Incorrect verify data in ServerHello " + "Incorrect verify data in ServerHello " +
......
/* /*
* Copyright (c) 1996, 2012, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1996, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -1907,7 +1907,7 @@ static final class Finished extends HandshakeMessage { ...@@ -1907,7 +1907,7 @@ static final class Finished extends HandshakeMessage {
*/ */
boolean verify(HandshakeHash handshakeHash, int sender, SecretKey master) { boolean verify(HandshakeHash handshakeHash, int sender, SecretKey master) {
byte[] myFinished = getFinished(handshakeHash, sender, master); byte[] myFinished = getFinished(handshakeHash, sender, master);
return Arrays.equals(myFinished, verifyData); return MessageDigest.isEqual(myFinished, verifyData);
} }
/* /*
......
...@@ -413,7 +413,7 @@ final class ServerHandshaker extends Handshaker { ...@@ -413,7 +413,7 @@ final class ServerHandshaker extends Handshaker {
} }
// verify the client_verify_data value // verify the client_verify_data value
if (!Arrays.equals(clientVerifyData, if (!MessageDigest.isEqual(clientVerifyData,
clientHelloRI.getRenegotiatedConnection())) { clientHelloRI.getRenegotiatedConnection())) {
fatalSE(Alerts.alert_handshake_failure, fatalSE(Alerts.alert_handshake_failure,
"Incorrect verify data in ClientHello " + "Incorrect verify data in ClientHello " +
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册