src/share/classes/com/sun/crypto/provider/DHPrivateKey.java

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

*** 1,7 **** /* ! * Copyright (c) 1997, 2011, 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 * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this --- 1,7 ---- /* ! * Copyright (c) 1997, 2013, 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 * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this
*** 24,33 **** --- 24,34 ---- */ package com.sun.crypto.provider; import java.io.*; + import java.util.Objects; import java.math.BigInteger; import java.security.KeyRep; import java.security.PrivateKey; import java.security.InvalidKeyException; import java.security.ProviderException;
*** 65,75 **** private BigInteger p; // the base generator private BigInteger g; ! // the private-value length private int l; private int DH_data[] = { 1, 2, 840, 113549, 1, 3, 1 }; /** --- 66,76 ---- private BigInteger p; // the base generator private BigInteger g; ! // the private-value length (optional) private int l; private int DH_data[] = { 1, 2, 840, 113549, 1, 3, 1 }; /**
*** 177,200 **** // privateKey // this.key = val.data.getOctetString(); parseKeyBits(); - // ignore OPTIONAL attributes - this.encodedKey = encodedKey.clone(); ! ! } catch (NumberFormatException 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; } } /** * Returns the encoding format of this key: "PKCS#8" --- 178,190 ---- // privateKey // this.key = val.data.getOctetString(); parseKeyBits(); this.encodedKey = encodedKey.clone(); ! } catch (IOException | NumberFormatException e) { ! throw new InvalidKeyException("Error parsing key encoding", e); } } /** * Returns the encoding format of this key: "PKCS#8"
*** 232,243 **** algid.putOID(new ObjectIdentifier(DH_data)); // encode parameters DerOutputStream params = new DerOutputStream(); params.putInteger(this.p); params.putInteger(this.g); ! if (this.l != 0) params.putInteger(this.l); // wrap parameters into SEQUENCE DerValue paramSequence = new DerValue(DerValue.tag_Sequence, params.toByteArray()); // store parameter SEQUENCE in algid algid.putDerValue(paramSequence); --- 222,234 ---- algid.putOID(new ObjectIdentifier(DH_data)); // encode parameters DerOutputStream params = new DerOutputStream(); params.putInteger(this.p); params.putInteger(this.g); ! if (this.l != 0) { params.putInteger(this.l); + } // wrap parameters into SEQUENCE DerValue paramSequence = new DerValue(DerValue.tag_Sequence, params.toByteArray()); // store parameter SEQUENCE in algid algid.putDerValue(paramSequence);
*** 271,285 **** * Returns the key parameters. * * @return the key parameters */ public DHParameterSpec getParams() { ! if (this.l != 0) return new DHParameterSpec(this.p, this.g, this.l); ! else return new DHParameterSpec(this.p, this.g); } public String toString() { String LINE_SEP = System.getProperty("line.separator"); StringBuffer strbuf --- 262,277 ---- * Returns the key parameters. * * @return the key parameters */ public DHParameterSpec getParams() { ! if (this.l != 0) { return new DHParameterSpec(this.p, this.g, this.l); ! } else { return new DHParameterSpec(this.p, this.g); } + } public String toString() { String LINE_SEP = System.getProperty("line.separator"); StringBuffer strbuf
*** 310,340 **** /** * Calculates a hash code value for the object. * Objects that are equal will also have the same hashcode. */ public int hashCode() { ! int retval = 0; ! byte[] enc = getEncoded(); ! ! for (int i = 1; i < enc.length; i++) { ! retval += enc[i] * i; } - return(retval); - } public boolean equals(Object obj) { ! if (this == obj) ! return true; ! if (!(obj instanceof PrivateKey)) return false; - - byte[] thisEncoded = this.getEncoded(); - byte[] thatEncoded = ((PrivateKey)obj).getEncoded(); - - return java.util.Arrays.equals(thisEncoded, thatEncoded); } /** * Replace the DH private key to be serialized. * * @return the standard KeyRep object to be serialized --- 302,327 ---- /** * Calculates a hash code value for the object. * Objects that are equal will also have the same hashcode. */ public int hashCode() { ! return Objects.hash(x, p, g); } public boolean equals(Object obj) { ! if (this == obj) return true; ! if (!(obj instanceof javax.crypto.interfaces.DHPrivateKey)) { return false; } + javax.crypto.interfaces.DHPrivateKey other = + (javax.crypto.interfaces.DHPrivateKey) obj; + DHParameterSpec otherParams = other.getParams(); + return ((this.x.compareTo(other.getX()) == 0) && + (this.p.compareTo(otherParams.getP()) == 0) && + (this.g.compareTo(otherParams.getG()) == 0)); + } /** * Replace the DH private key to be serialized. * * @return the standard KeyRep object to be serialized