1 /* |
1 /* |
2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
24 */ |
24 */ |
25 |
25 |
26 package com.sun.crypto.provider; |
26 package com.sun.crypto.provider; |
27 |
27 |
28 import java.io.*; |
28 import java.io.*; |
|
29 import java.util.Objects; |
29 import java.math.BigInteger; |
30 import java.math.BigInteger; |
30 import java.security.KeyRep; |
31 import java.security.KeyRep; |
31 import java.security.PrivateKey; |
32 import java.security.PrivateKey; |
32 import java.security.InvalidKeyException; |
33 import java.security.InvalidKeyException; |
33 import java.security.ProviderException; |
34 import java.security.ProviderException; |
177 // privateKey |
178 // privateKey |
178 // |
179 // |
179 this.key = val.data.getOctetString(); |
180 this.key = val.data.getOctetString(); |
180 parseKeyBits(); |
181 parseKeyBits(); |
181 |
182 |
182 // ignore OPTIONAL attributes |
|
183 |
|
184 this.encodedKey = encodedKey.clone(); |
183 this.encodedKey = encodedKey.clone(); |
185 |
184 } catch (IOException | NumberFormatException e) { |
186 } catch (NumberFormatException e) { |
185 throw new InvalidKeyException("Error parsing key encoding", e); |
187 InvalidKeyException ike = new InvalidKeyException( |
|
188 "Private-value length too big"); |
|
189 ike.initCause(e); |
|
190 throw ike; |
|
191 } catch (IOException e) { |
|
192 InvalidKeyException ike = new InvalidKeyException( |
|
193 "Error parsing key encoding: " + e.getMessage()); |
|
194 ike.initCause(e); |
|
195 throw ike; |
|
196 } |
186 } |
197 } |
187 } |
198 |
188 |
199 /** |
189 /** |
200 * Returns the encoding format of this key: "PKCS#8" |
190 * Returns the encoding format of this key: "PKCS#8" |
232 algid.putOID(new ObjectIdentifier(DH_data)); |
222 algid.putOID(new ObjectIdentifier(DH_data)); |
233 // encode parameters |
223 // encode parameters |
234 DerOutputStream params = new DerOutputStream(); |
224 DerOutputStream params = new DerOutputStream(); |
235 params.putInteger(this.p); |
225 params.putInteger(this.p); |
236 params.putInteger(this.g); |
226 params.putInteger(this.g); |
237 if (this.l != 0) |
227 if (this.l != 0) { |
238 params.putInteger(this.l); |
228 params.putInteger(this.l); |
|
229 } |
239 // wrap parameters into SEQUENCE |
230 // wrap parameters into SEQUENCE |
240 DerValue paramSequence = new DerValue(DerValue.tag_Sequence, |
231 DerValue paramSequence = new DerValue(DerValue.tag_Sequence, |
241 params.toByteArray()); |
232 params.toByteArray()); |
242 // store parameter SEQUENCE in algid |
233 // store parameter SEQUENCE in algid |
243 algid.putDerValue(paramSequence); |
234 algid.putDerValue(paramSequence); |
271 * Returns the key parameters. |
262 * Returns the key parameters. |
272 * |
263 * |
273 * @return the key parameters |
264 * @return the key parameters |
274 */ |
265 */ |
275 public DHParameterSpec getParams() { |
266 public DHParameterSpec getParams() { |
276 if (this.l != 0) |
267 if (this.l != 0) { |
277 return new DHParameterSpec(this.p, this.g, this.l); |
268 return new DHParameterSpec(this.p, this.g, this.l); |
278 else |
269 } else { |
279 return new DHParameterSpec(this.p, this.g); |
270 return new DHParameterSpec(this.p, this.g); |
|
271 } |
280 } |
272 } |
281 |
273 |
282 public String toString() { |
274 public String toString() { |
283 String LINE_SEP = System.getProperty("line.separator"); |
275 String LINE_SEP = System.getProperty("line.separator"); |
284 |
276 |
310 /** |
302 /** |
311 * Calculates a hash code value for the object. |
303 * Calculates a hash code value for the object. |
312 * Objects that are equal will also have the same hashcode. |
304 * Objects that are equal will also have the same hashcode. |
313 */ |
305 */ |
314 public int hashCode() { |
306 public int hashCode() { |
315 int retval = 0; |
307 return Objects.hash(x, p, g); |
316 byte[] enc = getEncoded(); |
|
317 |
|
318 for (int i = 1; i < enc.length; i++) { |
|
319 retval += enc[i] * i; |
|
320 } |
|
321 return(retval); |
|
322 } |
308 } |
323 |
309 |
324 public boolean equals(Object obj) { |
310 public boolean equals(Object obj) { |
325 if (this == obj) |
311 if (this == obj) return true; |
326 return true; |
312 |
327 |
313 if (!(obj instanceof javax.crypto.interfaces.DHPrivateKey)) { |
328 if (!(obj instanceof PrivateKey)) |
|
329 return false; |
314 return false; |
330 |
315 } |
331 byte[] thisEncoded = this.getEncoded(); |
316 javax.crypto.interfaces.DHPrivateKey other = |
332 byte[] thatEncoded = ((PrivateKey)obj).getEncoded(); |
317 (javax.crypto.interfaces.DHPrivateKey) obj; |
333 |
318 DHParameterSpec otherParams = other.getParams(); |
334 return java.util.Arrays.equals(thisEncoded, thatEncoded); |
319 return ((this.x.compareTo(other.getX()) == 0) && |
|
320 (this.p.compareTo(otherParams.getP()) == 0) && |
|
321 (this.g.compareTo(otherParams.getG()) == 0)); |
335 } |
322 } |
336 |
323 |
337 /** |
324 /** |
338 * Replace the DH private key to be serialized. |
325 * Replace the DH private key to be serialized. |
339 * |
326 * |