author | chegar |
Thu, 17 Oct 2019 20:54:25 +0100 | |
branch | datagramsocketimpl-branch |
changeset 58679 | 9c3209ff7550 |
parent 58678 | 9cf78a70fa4f |
parent 58519 | 6e017b301287 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
57950
4612a3cfb927
8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents:
48080
diff
changeset
|
2 |
* Copyright (c) 1996, 2019, 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 sun.security.x509; |
|
27 |
||
28 |
import java.io.*; |
|
29 |
import java.util.Arrays; |
|
30 |
import java.util.Properties; |
|
31 |
import java.security.Key; |
|
32 |
import java.security.PublicKey; |
|
33 |
import java.security.KeyFactory; |
|
34 |
import java.security.Security; |
|
35 |
import java.security.Provider; |
|
36 |
import java.security.InvalidKeyException; |
|
37 |
import java.security.NoSuchAlgorithmException; |
|
38 |
import java.security.spec.InvalidKeySpecException; |
|
39 |
import java.security.spec.X509EncodedKeySpec; |
|
40 |
||
34687
d302ed125dc9
8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents:
25859
diff
changeset
|
41 |
import sun.security.util.HexDumpEncoder; |
2 | 42 |
import sun.security.util.*; |
43 |
||
44 |
/** |
|
45 |
* Holds an X.509 key, for example a public key found in an X.509 |
|
46 |
* certificate. Includes a description of the algorithm to be used |
|
47 |
* with the key; these keys normally are used as |
|
48 |
* "SubjectPublicKeyInfo". |
|
49 |
* |
|
50 |
* <P>While this class can represent any kind of X.509 key, it may be |
|
51 |
* desirable to provide subclasses which understand how to parse keying |
|
52 |
* data. For example, RSA public keys have two members, one for the |
|
53 |
* public modulus and one for the prime exponent. If such a class is |
|
54 |
* provided, it is used when parsing X.509 keys. If one is not provided, |
|
55 |
* the key still parses correctly. |
|
56 |
* |
|
57 |
* @author David Brownell |
|
58 |
*/ |
|
59 |
public class X509Key implements PublicKey { |
|
60 |
||
61 |
/** use serialVersionUID from JDK 1.1. for interoperability */ |
|
57950
4612a3cfb927
8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents:
48080
diff
changeset
|
62 |
@java.io.Serial |
2 | 63 |
private static final long serialVersionUID = -5359250853002055002L; |
64 |
||
65 |
/* The algorithm information (name, parameters, etc). */ |
|
66 |
protected AlgorithmId algid; |
|
67 |
||
68 |
/** |
|
69 |
* The key bytes, without the algorithm information. |
|
70 |
* @deprecated Use the BitArray form which does not require keys to |
|
71 |
* be byte aligned. |
|
72 |
* @see sun.security.x509.X509Key#setKey(BitArray) |
|
73 |
* @see sun.security.x509.X509Key#getKey() |
|
74 |
*/ |
|
75 |
@Deprecated |
|
76 |
protected byte[] key = null; |
|
77 |
||
78 |
/* |
|
79 |
* The number of bits unused in the last byte of the key. |
|
80 |
* Added to keep the byte[] key form consistent with the BitArray |
|
81 |
* form. Can de deleted when byte[] key is deleted. |
|
82 |
*/ |
|
19375
cdfdb9c0590e
8022461: Fix lint warnings in sun.security.{provider,rsa,x509}
juh
parents:
10336
diff
changeset
|
83 |
@Deprecated |
2 | 84 |
private int unusedBits = 0; |
85 |
||
86 |
/* BitArray form of key */ |
|
58519
6e017b301287
8231262: Suppress warnings on non-serializable instance fields in security libs serializable classes
darcy
parents:
57950
diff
changeset
|
87 |
private transient BitArray bitStringKey = null; |
2 | 88 |
|
89 |
/* The encoding for the key. */ |
|
90 |
protected byte[] encodedKey; |
|
91 |
||
92 |
/** |
|
93 |
* Default constructor. The key constructed must have its key |
|
94 |
* and algorithm initialized before it may be used, for example |
|
95 |
* by using <code>decode</code>. |
|
96 |
*/ |
|
97 |
public X509Key() { } |
|
98 |
||
99 |
/* |
|
100 |
* Build and initialize as a "default" key. All X.509 key |
|
101 |
* data is stored and transmitted losslessly, but no knowledge |
|
102 |
* about this particular algorithm is available. |
|
103 |
*/ |
|
104 |
private X509Key(AlgorithmId algid, BitArray key) |
|
105 |
throws InvalidKeyException { |
|
106 |
this.algid = algid; |
|
107 |
setKey(key); |
|
108 |
encode(); |
|
109 |
} |
|
110 |
||
111 |
/** |
|
112 |
* Sets the key in the BitArray form. |
|
113 |
*/ |
|
114 |
protected void setKey(BitArray key) { |
|
115 |
this.bitStringKey = (BitArray)key.clone(); |
|
116 |
||
117 |
/* |
|
118 |
* Do this to keep the byte array form consistent with |
|
119 |
* this. Can delete when byte[] key is deleted. |
|
120 |
*/ |
|
121 |
this.key = key.toByteArray(); |
|
122 |
int remaining = key.length() % 8; |
|
123 |
this.unusedBits = |
|
124 |
((remaining == 0) ? 0 : 8 - remaining); |
|
125 |
} |
|
126 |
||
127 |
/** |
|
128 |
* Gets the key. The key may or may not be byte aligned. |
|
129 |
* @return a BitArray containing the key. |
|
130 |
*/ |
|
131 |
protected BitArray getKey() { |
|
132 |
/* |
|
133 |
* Do this for consistency in case a subclass |
|
134 |
* modifies byte[] key directly. Remove when |
|
135 |
* byte[] key is deleted. |
|
136 |
* Note: the consistency checks fail when the subclass |
|
137 |
* modifies a non byte-aligned key (into a byte-aligned key) |
|
138 |
* using the deprecated byte[] key field. |
|
139 |
*/ |
|
140 |
this.bitStringKey = new BitArray( |
|
141 |
this.key.length * 8 - this.unusedBits, |
|
142 |
this.key); |
|
143 |
||
144 |
return (BitArray)bitStringKey.clone(); |
|
145 |
} |
|
146 |
||
147 |
/** |
|
148 |
* Construct X.509 subject public key from a DER value. If |
|
149 |
* the runtime environment is configured with a specific class for |
|
150 |
* this kind of key, a subclass is returned. Otherwise, a generic |
|
151 |
* X509Key object is returned. |
|
152 |
* |
|
153 |
* <P>This mechanism gurantees that keys (and algorithms) may be |
|
154 |
* freely manipulated and transferred, without risk of losing |
|
155 |
* information. Also, when a key (or algorithm) needs some special |
|
156 |
* handling, that specific need can be accomodated. |
|
157 |
* |
|
158 |
* @param in the DER-encoded SubjectPublicKeyInfo value |
|
159 |
* @exception IOException on data format errors |
|
160 |
*/ |
|
161 |
public static PublicKey parse(DerValue in) throws IOException |
|
162 |
{ |
|
163 |
AlgorithmId algorithm; |
|
164 |
PublicKey subjectKey; |
|
165 |
||
166 |
if (in.tag != DerValue.tag_Sequence) |
|
167 |
throw new IOException("corrupt subject key"); |
|
168 |
||
169 |
algorithm = AlgorithmId.parse(in.data.getDerValue()); |
|
170 |
try { |
|
171 |
subjectKey = buildX509Key(algorithm, |
|
172 |
in.data.getUnalignedBitString()); |
|
173 |
||
174 |
} catch (InvalidKeyException e) { |
|
5298 | 175 |
throw new IOException("subject key, " + e.getMessage(), e); |
2 | 176 |
} |
177 |
||
178 |
if (in.data.available() != 0) |
|
179 |
throw new IOException("excess subject key"); |
|
180 |
return subjectKey; |
|
181 |
} |
|
182 |
||
183 |
/** |
|
184 |
* Parse the key bits. This may be redefined by subclasses to take |
|
185 |
* advantage of structure within the key. For example, RSA public |
|
186 |
* keys encapsulate two unsigned integers (modulus and exponent) as |
|
187 |
* DER values within the <code>key</code> bits; Diffie-Hellman and |
|
188 |
* DSS/DSA keys encapsulate a single unsigned integer. |
|
189 |
* |
|
190 |
* <P>This function is called when creating X.509 SubjectPublicKeyInfo |
|
191 |
* values using the X509Key member functions, such as <code>parse</code> |
|
192 |
* and <code>decode</code>. |
|
193 |
* |
|
194 |
* @exception IOException on parsing errors. |
|
195 |
* @exception InvalidKeyException on invalid key encodings. |
|
196 |
*/ |
|
197 |
protected void parseKeyBits() throws IOException, InvalidKeyException { |
|
198 |
encode(); |
|
199 |
} |
|
200 |
||
201 |
/* |
|
202 |
* Factory interface, building the kind of key associated with this |
|
203 |
* specific algorithm ID or else returning this generic base class. |
|
204 |
* See the description above. |
|
205 |
*/ |
|
206 |
static PublicKey buildX509Key(AlgorithmId algid, BitArray key) |
|
207 |
throws IOException, InvalidKeyException |
|
208 |
{ |
|
209 |
/* |
|
210 |
* Use the algid and key parameters to produce the ASN.1 encoding |
|
211 |
* of the key, which will then be used as the input to the |
|
212 |
* key factory. |
|
213 |
*/ |
|
214 |
DerOutputStream x509EncodedKeyStream = new DerOutputStream(); |
|
215 |
encode(x509EncodedKeyStream, algid, key); |
|
216 |
X509EncodedKeySpec x509KeySpec |
|
217 |
= new X509EncodedKeySpec(x509EncodedKeyStream.toByteArray()); |
|
218 |
||
219 |
try { |
|
220 |
// Instantiate the key factory of the appropriate algorithm |
|
221 |
KeyFactory keyFac = KeyFactory.getInstance(algid.getName()); |
|
222 |
||
223 |
// Generate the public key |
|
224 |
return keyFac.generatePublic(x509KeySpec); |
|
225 |
} catch (NoSuchAlgorithmException e) { |
|
226 |
// Return generic X509Key with opaque key data (see below) |
|
227 |
} catch (InvalidKeySpecException e) { |
|
5298 | 228 |
throw new InvalidKeyException(e.getMessage(), e); |
2 | 229 |
} |
230 |
||
231 |
/* |
|
232 |
* Try again using JDK1.1-style for backwards compatibility. |
|
233 |
*/ |
|
234 |
String classname = ""; |
|
235 |
try { |
|
236 |
Properties props; |
|
237 |
String keytype; |
|
238 |
Provider sunProvider; |
|
239 |
||
240 |
sunProvider = Security.getProvider("SUN"); |
|
241 |
if (sunProvider == null) |
|
242 |
throw new InstantiationException(); |
|
243 |
classname = sunProvider.getProperty("PublicKey.X.509." + |
|
244 |
algid.getName()); |
|
245 |
if (classname == null) { |
|
246 |
throw new InstantiationException(); |
|
247 |
} |
|
248 |
||
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7668
diff
changeset
|
249 |
Class<?> keyClass = null; |
2 | 250 |
try { |
251 |
keyClass = Class.forName(classname); |
|
252 |
} catch (ClassNotFoundException e) { |
|
253 |
ClassLoader cl = ClassLoader.getSystemClassLoader(); |
|
254 |
if (cl != null) { |
|
255 |
keyClass = cl.loadClass(classname); |
|
256 |
} |
|
257 |
} |
|
258 |
||
37782
ad8fe7507ecc
6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents:
34687
diff
changeset
|
259 |
@SuppressWarnings("deprecation") |
ad8fe7507ecc
6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents:
34687
diff
changeset
|
260 |
Object inst = (keyClass != null) ? keyClass.newInstance() : null; |
2 | 261 |
X509Key result; |
262 |
||
263 |
if (inst instanceof X509Key) { |
|
264 |
result = (X509Key) inst; |
|
265 |
result.algid = algid; |
|
266 |
result.setKey(key); |
|
267 |
result.parseKeyBits(); |
|
268 |
return result; |
|
269 |
} |
|
270 |
} catch (ClassNotFoundException e) { |
|
271 |
} catch (InstantiationException e) { |
|
272 |
} catch (IllegalAccessException e) { |
|
273 |
// this should not happen. |
|
274 |
throw new IOException (classname + " [internal error]"); |
|
275 |
} |
|
276 |
||
277 |
X509Key result = new X509Key(algid, key); |
|
278 |
return result; |
|
279 |
} |
|
280 |
||
281 |
/** |
|
282 |
* Returns the algorithm to be used with this key. |
|
283 |
*/ |
|
284 |
public String getAlgorithm() { |
|
285 |
return algid.getName(); |
|
286 |
} |
|
287 |
||
288 |
/** |
|
289 |
* Returns the algorithm ID to be used with this key. |
|
290 |
*/ |
|
291 |
public AlgorithmId getAlgorithmId() { return algid; } |
|
292 |
||
293 |
/** |
|
294 |
* Encode SubjectPublicKeyInfo sequence on the DER output stream. |
|
295 |
* |
|
296 |
* @exception IOException on encoding errors. |
|
297 |
*/ |
|
298 |
public final void encode(DerOutputStream out) throws IOException |
|
299 |
{ |
|
300 |
encode(out, this.algid, getKey()); |
|
301 |
} |
|
302 |
||
303 |
/** |
|
304 |
* Returns the DER-encoded form of the key as a byte array. |
|
305 |
*/ |
|
306 |
public byte[] getEncoded() { |
|
307 |
try { |
|
308 |
return getEncodedInternal().clone(); |
|
309 |
} catch (InvalidKeyException e) { |
|
310 |
// XXX |
|
311 |
} |
|
312 |
return null; |
|
313 |
} |
|
314 |
||
315 |
public byte[] getEncodedInternal() throws InvalidKeyException { |
|
316 |
byte[] encoded = encodedKey; |
|
317 |
if (encoded == null) { |
|
318 |
try { |
|
319 |
DerOutputStream out = new DerOutputStream(); |
|
320 |
encode(out); |
|
321 |
encoded = out.toByteArray(); |
|
322 |
} catch (IOException e) { |
|
323 |
throw new InvalidKeyException("IOException : " + |
|
324 |
e.getMessage()); |
|
325 |
} |
|
326 |
encodedKey = encoded; |
|
327 |
} |
|
328 |
return encoded; |
|
329 |
} |
|
330 |
||
331 |
/** |
|
332 |
* Returns the format for this key: "X.509" |
|
333 |
*/ |
|
334 |
public String getFormat() { |
|
335 |
return "X.509"; |
|
336 |
} |
|
337 |
||
338 |
/** |
|
339 |
* Returns the DER-encoded form of the key as a byte array. |
|
340 |
* |
|
341 |
* @exception InvalidKeyException on encoding errors. |
|
342 |
*/ |
|
343 |
public byte[] encode() throws InvalidKeyException { |
|
344 |
return getEncodedInternal().clone(); |
|
345 |
} |
|
346 |
||
347 |
/* |
|
348 |
* Returns a printable representation of the key |
|
349 |
*/ |
|
350 |
public String toString() |
|
351 |
{ |
|
352 |
HexDumpEncoder encoder = new HexDumpEncoder(); |
|
353 |
||
354 |
return "algorithm = " + algid.toString() |
|
355 |
+ ", unparsed keybits = \n" + encoder.encodeBuffer(key); |
|
356 |
} |
|
357 |
||
358 |
/** |
|
359 |
* Initialize an X509Key object from an input stream. The data on that |
|
360 |
* input stream must be encoded using DER, obeying the X.509 |
|
361 |
* <code>SubjectPublicKeyInfo</code> format. That is, the data is a |
|
362 |
* sequence consisting of an algorithm ID and a bit string which holds |
|
363 |
* the key. (That bit string is often used to encapsulate another DER |
|
364 |
* encoded sequence.) |
|
365 |
* |
|
366 |
* <P>Subclasses should not normally redefine this method; they should |
|
367 |
* instead provide a <code>parseKeyBits</code> method to parse any |
|
368 |
* fields inside the <code>key</code> member. |
|
369 |
* |
|
370 |
* <P>The exception to this rule is that since private keys need not |
|
371 |
* be encoded using the X.509 <code>SubjectPublicKeyInfo</code> format, |
|
372 |
* private keys may override this method, <code>encode</code>, and |
|
373 |
* of course <code>getFormat</code>. |
|
374 |
* |
|
375 |
* @param in an input stream with a DER-encoded X.509 |
|
376 |
* SubjectPublicKeyInfo value |
|
377 |
* @exception InvalidKeyException on parsing errors. |
|
378 |
*/ |
|
379 |
public void decode(InputStream in) |
|
380 |
throws InvalidKeyException |
|
381 |
{ |
|
382 |
DerValue val; |
|
383 |
||
384 |
try { |
|
385 |
val = new DerValue(in); |
|
386 |
if (val.tag != DerValue.tag_Sequence) |
|
387 |
throw new InvalidKeyException("invalid key format"); |
|
388 |
||
389 |
algid = AlgorithmId.parse(val.data.getDerValue()); |
|
390 |
setKey(val.data.getUnalignedBitString()); |
|
391 |
parseKeyBits(); |
|
392 |
if (val.data.available() != 0) |
|
393 |
throw new InvalidKeyException ("excess key data"); |
|
394 |
||
395 |
} catch (IOException e) { |
|
396 |
throw new InvalidKeyException("IOException: " + |
|
397 |
e.getMessage()); |
|
398 |
} |
|
399 |
} |
|
400 |
||
401 |
public void decode(byte[] encodedKey) throws InvalidKeyException { |
|
402 |
decode(new ByteArrayInputStream(encodedKey)); |
|
403 |
} |
|
404 |
||
405 |
/** |
|
406 |
* Serialization write ... X.509 keys serialize as |
|
407 |
* themselves, and they're parsed when they get read back. |
|
408 |
*/ |
|
57950
4612a3cfb927
8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents:
48080
diff
changeset
|
409 |
@java.io.Serial |
2 | 410 |
private void writeObject(ObjectOutputStream stream) throws IOException { |
411 |
stream.write(getEncoded()); |
|
412 |
} |
|
413 |
||
414 |
/** |
|
415 |
* Serialization read ... X.509 keys serialize as |
|
416 |
* themselves, and they're parsed when they get read back. |
|
417 |
*/ |
|
57950
4612a3cfb927
8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents:
48080
diff
changeset
|
418 |
@java.io.Serial |
2 | 419 |
private void readObject(ObjectInputStream stream) throws IOException { |
420 |
try { |
|
421 |
decode(stream); |
|
422 |
} catch (InvalidKeyException e) { |
|
423 |
e.printStackTrace(); |
|
424 |
throw new IOException("deserialized key is invalid: " + |
|
425 |
e.getMessage()); |
|
426 |
} |
|
427 |
} |
|
428 |
||
429 |
public boolean equals(Object obj) { |
|
430 |
if (this == obj) { |
|
431 |
return true; |
|
432 |
} |
|
433 |
if (obj instanceof Key == false) { |
|
434 |
return false; |
|
435 |
} |
|
436 |
try { |
|
437 |
byte[] thisEncoded = this.getEncodedInternal(); |
|
438 |
byte[] otherEncoded; |
|
439 |
if (obj instanceof X509Key) { |
|
440 |
otherEncoded = ((X509Key)obj).getEncodedInternal(); |
|
441 |
} else { |
|
442 |
otherEncoded = ((Key)obj).getEncoded(); |
|
443 |
} |
|
444 |
return Arrays.equals(thisEncoded, otherEncoded); |
|
445 |
} catch (InvalidKeyException e) { |
|
446 |
return false; |
|
447 |
} |
|
448 |
} |
|
449 |
||
450 |
/** |
|
451 |
* Calculates a hash code value for the object. Objects |
|
452 |
* which are equal will also have the same hashcode. |
|
453 |
*/ |
|
454 |
public int hashCode() { |
|
455 |
try { |
|
456 |
byte[] b1 = getEncodedInternal(); |
|
457 |
int r = b1.length; |
|
458 |
for (int i = 0; i < b1.length; i++) { |
|
459 |
r += (b1[i] & 0xff) * 37; |
|
460 |
} |
|
461 |
return r; |
|
462 |
} catch (InvalidKeyException e) { |
|
463 |
// should not happen |
|
464 |
return 0; |
|
465 |
} |
|
466 |
} |
|
467 |
||
468 |
/* |
|
469 |
* Produce SubjectPublicKey encoding from algorithm id and key material. |
|
470 |
*/ |
|
471 |
static void encode(DerOutputStream out, AlgorithmId algid, BitArray key) |
|
472 |
throws IOException { |
|
473 |
DerOutputStream tmp = new DerOutputStream(); |
|
474 |
algid.encode(tmp); |
|
475 |
tmp.putUnalignedBitString(key); |
|
476 |
out.write(DerValue.tag_Sequence, tmp); |
|
477 |
} |
|
478 |
} |