提交 f73f93be 编写于 作者: V valeriep

7196805: DH Key interoperability testing between SunJCE and JsafeJCE not successful

Summary: Check equality based on component values instead of encoding which may vary due to optional components
Reviewed-by: weijun
上级 9068ecfc
/* /*
* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1997, 2013, 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
...@@ -83,7 +83,7 @@ public final class DHKeyFactory extends KeyFactorySpi { ...@@ -83,7 +83,7 @@ public final class DHKeyFactory extends KeyFactorySpi {
} }
} catch (InvalidKeyException e) { } catch (InvalidKeyException e) {
throw new InvalidKeySpecException throw new InvalidKeySpecException
("Inappropriate key specification"); ("Inappropriate key specification", e);
} }
} }
...@@ -118,7 +118,7 @@ public final class DHKeyFactory extends KeyFactorySpi { ...@@ -118,7 +118,7 @@ public final class DHKeyFactory extends KeyFactorySpi {
} }
} catch (InvalidKeyException e) { } catch (InvalidKeyException e) {
throw new InvalidKeySpecException throw new InvalidKeySpecException
("Inappropriate key specification"); ("Inappropriate key specification", e);
} }
} }
...@@ -227,7 +227,7 @@ public final class DHKeyFactory extends KeyFactorySpi { ...@@ -227,7 +227,7 @@ public final class DHKeyFactory extends KeyFactorySpi {
} }
} catch (InvalidKeySpecException e) { } catch (InvalidKeySpecException e) {
throw new InvalidKeyException("Cannot translate key"); throw new InvalidKeyException("Cannot translate key", e);
} }
} }
} }
...@@ -167,15 +167,16 @@ public final class DHKeyPairGenerator extends KeyPairGeneratorSpi { ...@@ -167,15 +167,16 @@ public final class DHKeyPairGenerator extends KeyPairGeneratorSpi {
BigInteger pMinus2 = p.subtract(BigInteger.valueOf(2)); BigInteger pMinus2 = p.subtract(BigInteger.valueOf(2));
// //
// Handbook of Applied Cryptography: Menezes, et.al. // PKCS#3 section 7.1 "Private-value generation"
// Repeat if the following does not hold: // Repeat if either of the followings does not hold:
// 1 <= x <= p-2 // 0 < x < p-1
// 2^(lSize-1) <= x < 2^(lSize)
// //
do { do {
// generate random x up to 2^lSize bits long // generate random x up to 2^lSize bits long
x = new BigInteger(lSize, random); x = new BigInteger(lSize, random);
} while ((x.compareTo(BigInteger.ONE) < 0) || } while ((x.compareTo(BigInteger.ONE) < 0) ||
((x.compareTo(pMinus2) > 0))); ((x.compareTo(pMinus2) > 0)) || (x.bitLength() != lSize));
// calculate public value y // calculate public value y
BigInteger y = g.modPow(x, p); BigInteger y = g.modPow(x, p);
......
/* /*
* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1997, 2013, 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
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
package com.sun.crypto.provider; package com.sun.crypto.provider;
import java.io.*; import java.io.*;
import java.util.Objects;
import java.math.BigInteger; import java.math.BigInteger;
import java.security.KeyRep; import java.security.KeyRep;
import java.security.PrivateKey; import java.security.PrivateKey;
...@@ -67,7 +68,7 @@ javax.crypto.interfaces.DHPrivateKey, Serializable { ...@@ -67,7 +68,7 @@ javax.crypto.interfaces.DHPrivateKey, Serializable {
// the base generator // the base generator
private BigInteger g; private BigInteger g;
// the private-value length // the private-value length (optional)
private int l; private int l;
private int DH_data[] = { 1, 2, 840, 113549, 1, 3, 1 }; private int DH_data[] = { 1, 2, 840, 113549, 1, 3, 1 };
...@@ -179,20 +180,9 @@ javax.crypto.interfaces.DHPrivateKey, Serializable { ...@@ -179,20 +180,9 @@ javax.crypto.interfaces.DHPrivateKey, Serializable {
this.key = val.data.getOctetString(); this.key = val.data.getOctetString();
parseKeyBits(); parseKeyBits();
// ignore OPTIONAL attributes
this.encodedKey = encodedKey.clone(); this.encodedKey = encodedKey.clone();
} catch (IOException | NumberFormatException e) {
} catch (NumberFormatException e) { throw new InvalidKeyException("Error parsing key encoding", e);
InvalidKeyException ike = new InvalidKeyException(
"Private-value length too big");
ike.initCause(e);
throw ike;
} catch (IOException e) {
InvalidKeyException ike = new InvalidKeyException(
"Error parsing key encoding: " + e.getMessage());
ike.initCause(e);
throw ike;
} }
} }
...@@ -234,8 +224,9 @@ javax.crypto.interfaces.DHPrivateKey, Serializable { ...@@ -234,8 +224,9 @@ javax.crypto.interfaces.DHPrivateKey, Serializable {
DerOutputStream params = new DerOutputStream(); DerOutputStream params = new DerOutputStream();
params.putInteger(this.p); params.putInteger(this.p);
params.putInteger(this.g); params.putInteger(this.g);
if (this.l != 0) if (this.l != 0) {
params.putInteger(this.l); params.putInteger(this.l);
}
// wrap parameters into SEQUENCE // wrap parameters into SEQUENCE
DerValue paramSequence = new DerValue(DerValue.tag_Sequence, DerValue paramSequence = new DerValue(DerValue.tag_Sequence,
params.toByteArray()); params.toByteArray());
...@@ -273,11 +264,12 @@ javax.crypto.interfaces.DHPrivateKey, Serializable { ...@@ -273,11 +264,12 @@ javax.crypto.interfaces.DHPrivateKey, Serializable {
* @return the key parameters * @return the key parameters
*/ */
public DHParameterSpec getParams() { public DHParameterSpec getParams() {
if (this.l != 0) if (this.l != 0) {
return new DHParameterSpec(this.p, this.g, this.l); return new DHParameterSpec(this.p, this.g, this.l);
else } else {
return new DHParameterSpec(this.p, this.g); return new DHParameterSpec(this.p, this.g);
} }
}
public String toString() { public String toString() {
String LINE_SEP = System.getProperty("line.separator"); String LINE_SEP = System.getProperty("line.separator");
...@@ -312,26 +304,21 @@ javax.crypto.interfaces.DHPrivateKey, Serializable { ...@@ -312,26 +304,21 @@ javax.crypto.interfaces.DHPrivateKey, Serializable {
* Objects that are equal will also have the same hashcode. * Objects that are equal will also have the same hashcode.
*/ */
public int hashCode() { public int hashCode() {
int retval = 0; return Objects.hash(x, p, g);
byte[] enc = getEncoded();
for (int i = 1; i < enc.length; i++) {
retval += enc[i] * i;
}
return(retval);
} }
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (this == obj) if (this == obj) return true;
return true;
if (!(obj instanceof PrivateKey)) if (!(obj instanceof javax.crypto.interfaces.DHPrivateKey)) {
return false; return false;
}
byte[] thisEncoded = this.getEncoded(); javax.crypto.interfaces.DHPrivateKey other =
byte[] thatEncoded = ((PrivateKey)obj).getEncoded(); (javax.crypto.interfaces.DHPrivateKey) obj;
DHParameterSpec otherParams = other.getParams();
return java.util.Arrays.equals(thisEncoded, thatEncoded); return ((this.x.compareTo(other.getX()) == 0) &&
(this.p.compareTo(otherParams.getP()) == 0) &&
(this.g.compareTo(otherParams.getG()) == 0));
} }
/** /**
......
/* /*
* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1997, 2013, 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
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
package com.sun.crypto.provider; package com.sun.crypto.provider;
import java.io.*; import java.io.*;
import java.util.Objects;
import java.math.BigInteger; import java.math.BigInteger;
import java.security.KeyRep; import java.security.KeyRep;
import java.security.InvalidKeyException; import java.security.InvalidKeyException;
...@@ -64,7 +65,7 @@ javax.crypto.interfaces.DHPublicKey, Serializable { ...@@ -64,7 +65,7 @@ javax.crypto.interfaces.DHPublicKey, Serializable {
// the base generator // the base generator
private BigInteger g; private BigInteger g;
// the private-value length // the private-value length (optional)
private int l; private int l;
private int DH_data[] = { 1, 2, 840, 113549, 1, 3, 1 }; private int DH_data[] = { 1, 2, 840, 113549, 1, 3, 1 };
...@@ -173,13 +174,8 @@ javax.crypto.interfaces.DHPublicKey, Serializable { ...@@ -173,13 +174,8 @@ javax.crypto.interfaces.DHPublicKey, Serializable {
} }
this.encodedKey = encodedKey.clone(); this.encodedKey = encodedKey.clone();
} catch (IOException | NumberFormatException e) {
} catch (NumberFormatException e) { throw new InvalidKeyException("Error parsing key encoding", e);
throw new InvalidKeyException("Private-value length too big");
} catch (IOException e) {
throw new InvalidKeyException(
"Error parsing key encoding: " + e.toString());
} }
} }
...@@ -212,8 +208,9 @@ javax.crypto.interfaces.DHPublicKey, Serializable { ...@@ -212,8 +208,9 @@ javax.crypto.interfaces.DHPublicKey, Serializable {
DerOutputStream params = new DerOutputStream(); DerOutputStream params = new DerOutputStream();
params.putInteger(this.p); params.putInteger(this.p);
params.putInteger(this.g); params.putInteger(this.g);
if (this.l != 0) if (this.l != 0) {
params.putInteger(this.l); params.putInteger(this.l);
}
// wrap parameters into SEQUENCE // wrap parameters into SEQUENCE
DerValue paramSequence = new DerValue(DerValue.tag_Sequence, DerValue paramSequence = new DerValue(DerValue.tag_Sequence,
params.toByteArray()); params.toByteArray());
...@@ -253,11 +250,12 @@ javax.crypto.interfaces.DHPublicKey, Serializable { ...@@ -253,11 +250,12 @@ javax.crypto.interfaces.DHPublicKey, Serializable {
* @return the key parameters * @return the key parameters
*/ */
public DHParameterSpec getParams() { public DHParameterSpec getParams() {
if (this.l != 0) if (this.l != 0) {
return new DHParameterSpec(this.p, this.g, this.l); return new DHParameterSpec(this.p, this.g, this.l);
else } else {
return new DHParameterSpec(this.p, this.g); return new DHParameterSpec(this.p, this.g);
} }
}
public String toString() { public String toString() {
String LINE_SEP = System.getProperty("line.separator"); String LINE_SEP = System.getProperty("line.separator");
...@@ -290,26 +288,22 @@ javax.crypto.interfaces.DHPublicKey, Serializable { ...@@ -290,26 +288,22 @@ javax.crypto.interfaces.DHPublicKey, Serializable {
* Objects that are equal will also have the same hashcode. * Objects that are equal will also have the same hashcode.
*/ */
public int hashCode() { public int hashCode() {
int retval = 0; return Objects.hash(y, p, g);
byte[] enc = getEncoded();
for (int i = 1; i < enc.length; i++) {
retval += enc[i] * i;
}
return(retval);
} }
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (this == obj) if (this == obj) return true;
return true;
if (!(obj instanceof PublicKey)) if (!(obj instanceof javax.crypto.interfaces.DHPublicKey)) {
return false; return false;
}
byte[] thisEncoded = this.getEncoded(); javax.crypto.interfaces.DHPublicKey other =
byte[] thatEncoded = ((PublicKey)obj).getEncoded(); (javax.crypto.interfaces.DHPublicKey) obj;
DHParameterSpec otherParams = other.getParams();
return java.util.Arrays.equals(thisEncoded, thatEncoded); return ((this.y.compareTo(other.getY()) == 0) &&
(this.p.compareTo(otherParams.getP()) == 0) &&
(this.g.compareTo(otherParams.getG()) == 0));
} }
/** /**
......
/* /*
* Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2013, 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
...@@ -881,6 +881,29 @@ abstract class P11Key implements Key, Length { ...@@ -881,6 +881,29 @@ abstract class P11Key implements Key, Length {
return super.toString() + "\n x: " + x + "\n p: " + params.getP() return super.toString() + "\n x: " + x + "\n p: " + params.getP()
+ "\n g: " + params.getG(); + "\n g: " + params.getG();
} }
public int hashCode() {
if (token.isValid() == false) {
return 0;
}
fetchValues();
return Objects.hash(x, params.getP(), params.getG());
}
public boolean equals(Object obj) {
if (this == obj) return true;
// equals() should never throw exceptions
if (token.isValid() == false) {
return false;
}
if (!(obj instanceof DHPrivateKey)) {
return false;
}
fetchValues();
DHPrivateKey other = (DHPrivateKey) obj;
DHParameterSpec otherParams = other.getParams();
return ((this.x.compareTo(other.getX()) == 0) &&
(this.params.getP().compareTo(otherParams.getP()) == 0) &&
(this.params.getG().compareTo(otherParams.getG()) == 0));
}
} }
private static final class P11DHPublicKey extends P11Key private static final class P11DHPublicKey extends P11Key
...@@ -945,6 +968,29 @@ abstract class P11Key implements Key, Length { ...@@ -945,6 +968,29 @@ abstract class P11Key implements Key, Length {
return super.toString() + "\n y: " + y + "\n p: " + params.getP() return super.toString() + "\n y: " + y + "\n p: " + params.getP()
+ "\n g: " + params.getG(); + "\n g: " + params.getG();
} }
public int hashCode() {
if (token.isValid() == false) {
return 0;
}
fetchValues();
return Objects.hash(y, params.getP(), params.getG());
}
public boolean equals(Object obj) {
if (this == obj) return true;
// equals() should never throw exceptions
if (token.isValid() == false) {
return false;
}
if (!(obj instanceof DHPublicKey)) {
return false;
}
fetchValues();
DHPublicKey other = (DHPublicKey) obj;
DHParameterSpec otherParams = other.getParams();
return ((this.y.compareTo(other.getY()) == 0) &&
(this.params.getP().compareTo(otherParams.getP()) == 0) &&
(this.params.getG().compareTo(otherParams.getG()) == 0));
}
} }
private static final class P11ECPrivateKey extends P11Key private static final class P11ECPrivateKey extends P11Key
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册