author | valeriep |
Tue, 08 May 2012 17:57:48 -0700 | |
changeset 12685 | 8a448b5b9006 |
parent 10336 | 0bb1999251f8 |
child 18809 | 97f5713a0f1a |
child 20813 | 0ad12d66a652 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7043
diff
changeset
|
2 |
* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package com.sun.crypto.provider; |
|
27 |
||
28 |
import java.io.*; |
|
29 |
import java.math.BigInteger; |
|
30 |
import java.security.KeyRep; |
|
31 |
import java.security.PrivateKey; |
|
32 |
import java.security.InvalidKeyException; |
|
33 |
import java.security.ProviderException; |
|
34 |
import javax.crypto.spec.DHParameterSpec; |
|
35 |
import sun.security.util.*; |
|
36 |
||
37 |
/** |
|
38 |
* A private key in PKCS#8 format for the Diffie-Hellman key agreement |
|
39 |
* algorithm. |
|
40 |
* |
|
41 |
* @author Jan Luehe |
|
42 |
* |
|
43 |
* |
|
44 |
* @see DHPublicKey |
|
45 |
* @see java.security.KeyAgreement |
|
46 |
*/ |
|
47 |
final class DHPrivateKey implements PrivateKey, |
|
48 |
javax.crypto.interfaces.DHPrivateKey, Serializable { |
|
49 |
||
50 |
static final long serialVersionUID = 7565477590005668886L; |
|
51 |
||
52 |
// only supported version of PKCS#8 PrivateKeyInfo |
|
53 |
private static final BigInteger PKCS8_VERSION = BigInteger.ZERO; |
|
54 |
||
55 |
// the private key |
|
56 |
private BigInteger x; |
|
57 |
||
58 |
// the key bytes, without the algorithm information |
|
59 |
private byte[] key; |
|
60 |
||
61 |
// the encoded key |
|
62 |
private byte[] encodedKey; |
|
63 |
||
64 |
// the prime modulus |
|
65 |
private BigInteger p; |
|
66 |
||
67 |
// the base generator |
|
68 |
private BigInteger g; |
|
69 |
||
70 |
// the private-value length |
|
71 |
private int l; |
|
72 |
||
73 |
private int DH_data[] = { 1, 2, 840, 113549, 1, 3, 1 }; |
|
74 |
||
75 |
/** |
|
76 |
* Make a DH private key out of a private value <code>x</code>, a prime |
|
77 |
* modulus <code>p</code>, and a base generator <code>g</code>. |
|
78 |
* |
|
79 |
* @param x the private value |
|
80 |
* @param p the prime modulus |
|
81 |
* @param g the base generator |
|
82 |
* |
|
83 |
* @exception ProviderException if the key cannot be encoded |
|
84 |
*/ |
|
85 |
DHPrivateKey(BigInteger x, BigInteger p, BigInteger g) |
|
86 |
throws InvalidKeyException { |
|
87 |
this(x, p, g, 0); |
|
88 |
} |
|
89 |
||
90 |
/** |
|
91 |
* Make a DH private key out of a private value <code>x</code>, a prime |
|
92 |
* modulus <code>p</code>, a base generator <code>g</code>, and a |
|
93 |
* private-value length <code>l</code>. |
|
94 |
* |
|
95 |
* @param x the private value |
|
96 |
* @param p the prime modulus |
|
97 |
* @param g the base generator |
|
98 |
* @param l the private-value length |
|
99 |
* |
|
100 |
* @exception InvalidKeyException if the key cannot be encoded |
|
101 |
*/ |
|
102 |
DHPrivateKey(BigInteger x, BigInteger p, BigInteger g, int l) { |
|
103 |
this.x = x; |
|
104 |
this.p = p; |
|
105 |
this.g = g; |
|
106 |
this.l = l; |
|
107 |
try { |
|
108 |
this.key = new DerValue(DerValue.tag_Integer, |
|
109 |
this.x.toByteArray()).toByteArray(); |
|
110 |
this.encodedKey = getEncoded(); |
|
111 |
} catch (IOException e) { |
|
112 |
throw new ProviderException("Cannot produce ASN.1 encoding", e); |
|
113 |
} |
|
114 |
} |
|
115 |
||
116 |
/** |
|
117 |
* Make a DH private key from its DER encoding (PKCS #8). |
|
118 |
* |
|
119 |
* @param encodedKey the encoded key |
|
120 |
* |
|
121 |
* @exception InvalidKeyException if the encoded key does not represent |
|
122 |
* a Diffie-Hellman private key |
|
123 |
*/ |
|
124 |
DHPrivateKey(byte[] encodedKey) throws InvalidKeyException { |
|
125 |
InputStream inStream = new ByteArrayInputStream(encodedKey); |
|
126 |
try { |
|
127 |
DerValue val = new DerValue(inStream); |
|
128 |
if (val.tag != DerValue.tag_Sequence) { |
|
129 |
throw new InvalidKeyException ("Key not a SEQUENCE"); |
|
130 |
} |
|
131 |
||
132 |
// |
|
133 |
// version |
|
134 |
// |
|
135 |
BigInteger parsedVersion = val.data.getBigInteger(); |
|
136 |
if (!parsedVersion.equals(PKCS8_VERSION)) { |
|
137 |
throw new IOException("version mismatch: (supported: " + |
|
138 |
PKCS8_VERSION + ", parsed: " + |
|
139 |
parsedVersion); |
|
140 |
} |
|
141 |
||
142 |
// |
|
143 |
// privateKeyAlgorithm |
|
144 |
// |
|
145 |
DerValue algid = val.data.getDerValue(); |
|
146 |
if (algid.tag != DerValue.tag_Sequence) { |
|
147 |
throw new InvalidKeyException("AlgId is not a SEQUENCE"); |
|
148 |
} |
|
149 |
DerInputStream derInStream = algid.toDerInputStream(); |
|
150 |
ObjectIdentifier oid = derInStream.getOID(); |
|
151 |
if (oid == null) { |
|
152 |
throw new InvalidKeyException("Null OID"); |
|
153 |
} |
|
154 |
if (derInStream.available() == 0) { |
|
155 |
throw new InvalidKeyException("Parameters missing"); |
|
156 |
} |
|
157 |
// parse the parameters |
|
158 |
DerValue params = derInStream.getDerValue(); |
|
159 |
if (params.tag == DerValue.tag_Null) { |
|
160 |
throw new InvalidKeyException("Null parameters"); |
|
161 |
} |
|
162 |
if (params.tag != DerValue.tag_Sequence) { |
|
163 |
throw new InvalidKeyException("Parameters not a SEQUENCE"); |
|
164 |
} |
|
165 |
params.data.reset(); |
|
166 |
this.p = params.data.getBigInteger(); |
|
167 |
this.g = params.data.getBigInteger(); |
|
168 |
// Private-value length is OPTIONAL |
|
169 |
if (params.data.available() != 0) { |
|
170 |
this.l = params.data.getInteger(); |
|
171 |
} |
|
172 |
if (params.data.available() != 0) { |
|
173 |
throw new InvalidKeyException("Extra parameter data"); |
|
174 |
} |
|
175 |
||
176 |
// |
|
177 |
// privateKey |
|
178 |
// |
|
179 |
this.key = val.data.getOctetString(); |
|
180 |
parseKeyBits(); |
|
181 |
||
182 |
// ignore OPTIONAL attributes |
|
183 |
||
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7043
diff
changeset
|
184 |
this.encodedKey = encodedKey.clone(); |
2 | 185 |
|
186 |
} catch (NumberFormatException e) { |
|
187 |
InvalidKeyException ike = new InvalidKeyException( |
|
188 |
"Private-value length too big"); |
|
189 |
ike.initCause(e); |
|
190 |
throw ike; |
|
191 |
} catch (IOException e) { |
|
7043 | 192 |
InvalidKeyException ike = new InvalidKeyException( |
193 |
"Error parsing key encoding: " + e.getMessage()); |
|
2 | 194 |
ike.initCause(e); |
195 |
throw ike; |
|
196 |
} |
|
197 |
} |
|
198 |
||
199 |
/** |
|
200 |
* Returns the encoding format of this key: "PKCS#8" |
|
201 |
*/ |
|
202 |
public String getFormat() { |
|
203 |
return "PKCS#8"; |
|
204 |
} |
|
205 |
||
206 |
/** |
|
207 |
* Returns the name of the algorithm associated with this key: "DH" |
|
208 |
*/ |
|
209 |
public String getAlgorithm() { |
|
210 |
return "DH"; |
|
211 |
} |
|
212 |
||
213 |
/** |
|
214 |
* Get the encoding of the key. |
|
215 |
*/ |
|
216 |
public synchronized byte[] getEncoded() { |
|
217 |
if (this.encodedKey == null) { |
|
218 |
try { |
|
219 |
DerOutputStream tmp = new DerOutputStream(); |
|
220 |
||
221 |
// |
|
222 |
// version |
|
223 |
// |
|
224 |
tmp.putInteger(PKCS8_VERSION); |
|
225 |
||
226 |
// |
|
227 |
// privateKeyAlgorithm |
|
228 |
// |
|
229 |
DerOutputStream algid = new DerOutputStream(); |
|
230 |
||
231 |
// store OID |
|
232 |
algid.putOID(new ObjectIdentifier(DH_data)); |
|
233 |
// encode parameters |
|
234 |
DerOutputStream params = new DerOutputStream(); |
|
235 |
params.putInteger(this.p); |
|
236 |
params.putInteger(this.g); |
|
237 |
if (this.l != 0) |
|
238 |
params.putInteger(this.l); |
|
239 |
// wrap parameters into SEQUENCE |
|
240 |
DerValue paramSequence = new DerValue(DerValue.tag_Sequence, |
|
241 |
params.toByteArray()); |
|
242 |
// store parameter SEQUENCE in algid |
|
243 |
algid.putDerValue(paramSequence); |
|
244 |
// wrap algid into SEQUENCE |
|
245 |
tmp.write(DerValue.tag_Sequence, algid); |
|
246 |
||
247 |
// privateKey |
|
248 |
tmp.putOctetString(this.key); |
|
249 |
||
250 |
// make it a SEQUENCE |
|
251 |
DerOutputStream derKey = new DerOutputStream(); |
|
252 |
derKey.write(DerValue.tag_Sequence, tmp); |
|
253 |
this.encodedKey = derKey.toByteArray(); |
|
254 |
} catch (IOException e) { |
|
255 |
return null; |
|
256 |
} |
|
257 |
} |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7043
diff
changeset
|
258 |
return this.encodedKey.clone(); |
2 | 259 |
} |
260 |
||
261 |
/** |
|
262 |
* Returns the private value, <code>x</code>. |
|
263 |
* |
|
264 |
* @return the private value, <code>x</code> |
|
265 |
*/ |
|
266 |
public BigInteger getX() { |
|
267 |
return this.x; |
|
268 |
} |
|
269 |
||
270 |
/** |
|
271 |
* Returns the key parameters. |
|
272 |
* |
|
273 |
* @return the key parameters |
|
274 |
*/ |
|
275 |
public DHParameterSpec getParams() { |
|
276 |
if (this.l != 0) |
|
277 |
return new DHParameterSpec(this.p, this.g, this.l); |
|
278 |
else |
|
279 |
return new DHParameterSpec(this.p, this.g); |
|
280 |
} |
|
281 |
||
282 |
public String toString() { |
|
283 |
String LINE_SEP = System.getProperty("line.separator"); |
|
284 |
||
285 |
StringBuffer strbuf |
|
286 |
= new StringBuffer("SunJCE Diffie-Hellman Private Key:" |
|
287 |
+ LINE_SEP + "x:" + LINE_SEP |
|
288 |
+ Debug.toHexString(this.x) |
|
289 |
+ LINE_SEP + "p:" + LINE_SEP |
|
290 |
+ Debug.toHexString(this.p) |
|
291 |
+ LINE_SEP + "g:" + LINE_SEP |
|
292 |
+ Debug.toHexString(this.g)); |
|
293 |
if (this.l != 0) |
|
294 |
strbuf.append(LINE_SEP + "l:" + LINE_SEP + " " + this.l); |
|
295 |
return strbuf.toString(); |
|
296 |
} |
|
297 |
||
298 |
private void parseKeyBits() throws InvalidKeyException { |
|
299 |
try { |
|
300 |
DerInputStream in = new DerInputStream(this.key); |
|
301 |
this.x = in.getBigInteger(); |
|
302 |
} catch (IOException e) { |
|
7043 | 303 |
InvalidKeyException ike = new InvalidKeyException( |
304 |
"Error parsing key encoding: " + e.getMessage()); |
|
2 | 305 |
ike.initCause(e); |
306 |
throw ike; |
|
307 |
} |
|
308 |
} |
|
309 |
||
310 |
/** |
|
311 |
* Calculates a hash code value for the object. |
|
312 |
* Objects that are equal will also have the same hashcode. |
|
313 |
*/ |
|
314 |
public int hashCode() { |
|
315 |
int retval = 0; |
|
316 |
byte[] enc = getEncoded(); |
|
317 |
||
318 |
for (int i = 1; i < enc.length; i++) { |
|
319 |
retval += enc[i] * i; |
|
320 |
} |
|
321 |
return(retval); |
|
322 |
} |
|
323 |
||
324 |
public boolean equals(Object obj) { |
|
325 |
if (this == obj) |
|
326 |
return true; |
|
327 |
||
328 |
if (!(obj instanceof PrivateKey)) |
|
329 |
return false; |
|
330 |
||
331 |
byte[] thisEncoded = this.getEncoded(); |
|
332 |
byte[] thatEncoded = ((PrivateKey)obj).getEncoded(); |
|
333 |
||
334 |
return java.util.Arrays.equals(thisEncoded, thatEncoded); |
|
335 |
} |
|
336 |
||
337 |
/** |
|
338 |
* Replace the DH private key to be serialized. |
|
339 |
* |
|
340 |
* @return the standard KeyRep object to be serialized |
|
341 |
* |
|
342 |
* @throws java.io.ObjectStreamException if a new object representing |
|
343 |
* this DH private key could not be created |
|
344 |
*/ |
|
345 |
private Object writeReplace() throws java.io.ObjectStreamException { |
|
346 |
return new KeyRep(KeyRep.Type.PRIVATE, |
|
347 |
getAlgorithm(), |
|
348 |
getFormat(), |
|
349 |
getEncoded()); |
|
350 |
} |
|
351 |
} |