author | kvn |
Thu, 16 Apr 2015 14:01:18 -0700 | |
changeset 30083 | 385348cd698e |
parent 25859 | 3317bb8137f4 |
child 31264 | 896105040033 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
9508
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 2005, 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 sun.security.mscapi; |
|
27 |
||
28 |
import java.io.ByteArrayInputStream; |
|
29 |
import java.io.IOException; |
|
30 |
import java.io.InputStream; |
|
31 |
import java.io.OutputStream; |
|
32 |
import java.security.AccessController; |
|
9508
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
5506
diff
changeset
|
33 |
import java.security.InvalidKeyException; |
2 | 34 |
import java.security.KeyStoreSpi; |
35 |
import java.security.KeyStoreException; |
|
23582
d5fa3327ab3a
8038177: Eliminate unnecessary dependency to sun.security.action
mchung
parents:
10336
diff
changeset
|
36 |
import java.security.PrivilegedAction; |
2 | 37 |
import java.security.UnrecoverableKeyException; |
38 |
import java.security.NoSuchAlgorithmException; |
|
39 |
import java.security.SecurityPermission; |
|
40 |
import java.security.cert.X509Certificate; |
|
41 |
import java.security.cert.Certificate; |
|
42 |
import java.security.cert.CertificateException; |
|
43 |
import java.security.cert.CertificateFactory; |
|
44 |
import java.security.interfaces.RSAPrivateCrtKey; |
|
45 |
import java.util.ArrayList; |
|
46 |
import java.util.Collection; |
|
47 |
import java.util.Date; |
|
48 |
import java.util.Enumeration; |
|
49 |
import java.util.Iterator; |
|
50 |
import java.util.UUID; |
|
51 |
||
52 |
/** |
|
53 |
* Implementation of key store for Windows using the Microsoft Crypto API. |
|
54 |
* |
|
55 |
* @since 1.6 |
|
56 |
*/ |
|
57 |
abstract class KeyStore extends KeyStoreSpi { |
|
58 |
||
59 |
public static final class MY extends KeyStore { |
|
60 |
public MY() { |
|
61 |
super("MY"); |
|
62 |
} |
|
63 |
} |
|
64 |
||
65 |
public static final class ROOT extends KeyStore { |
|
66 |
public ROOT() { |
|
67 |
super("ROOT"); |
|
68 |
} |
|
69 |
} |
|
70 |
||
71 |
class KeyEntry |
|
72 |
{ |
|
73 |
private Key privateKey; |
|
74 |
private X509Certificate certChain[]; |
|
75 |
private String alias; |
|
76 |
||
77 |
KeyEntry(Key key, X509Certificate[] chain) { |
|
78 |
this(null, key, chain); |
|
79 |
} |
|
80 |
||
81 |
KeyEntry(String alias, Key key, X509Certificate[] chain) { |
|
82 |
this.privateKey = key; |
|
83 |
this.certChain = chain; |
|
84 |
/* |
|
85 |
* The default alias for both entry types is derived from a |
|
86 |
* hash value intrinsic to the first certificate in the chain. |
|
87 |
*/ |
|
88 |
if (alias == null) { |
|
89 |
this.alias = Integer.toString(chain[0].hashCode()); |
|
90 |
} else { |
|
91 |
this.alias = alias; |
|
92 |
} |
|
93 |
} |
|
94 |
||
95 |
/** |
|
96 |
* Gets the alias for the keystore entry. |
|
97 |
*/ |
|
98 |
String getAlias() |
|
99 |
{ |
|
100 |
return alias; |
|
101 |
} |
|
102 |
||
103 |
/** |
|
104 |
* Sets the alias for the keystore entry. |
|
105 |
*/ |
|
106 |
void setAlias(String alias) |
|
107 |
{ |
|
108 |
// TODO - set friendly name prop in cert store |
|
109 |
this.alias = alias; |
|
110 |
} |
|
111 |
||
112 |
/** |
|
113 |
* Gets the private key for the keystore entry. |
|
114 |
*/ |
|
115 |
Key getPrivateKey() |
|
116 |
{ |
|
117 |
return privateKey; |
|
118 |
} |
|
119 |
||
120 |
/** |
|
121 |
* Sets the private key for the keystore entry. |
|
122 |
*/ |
|
123 |
void setPrivateKey(RSAPrivateCrtKey key) |
|
9508
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
5506
diff
changeset
|
124 |
throws InvalidKeyException, KeyStoreException |
2 | 125 |
{ |
126 |
byte[] modulusBytes = key.getModulus().toByteArray(); |
|
127 |
||
128 |
// Adjust key length due to sign bit |
|
129 |
int keyBitLength = (modulusBytes[0] == 0) |
|
130 |
? (modulusBytes.length - 1) * 8 |
|
131 |
: modulusBytes.length * 8; |
|
132 |
||
133 |
byte[] keyBlob = generatePrivateKeyBlob( |
|
134 |
keyBitLength, |
|
135 |
modulusBytes, |
|
136 |
key.getPublicExponent().toByteArray(), |
|
137 |
key.getPrivateExponent().toByteArray(), |
|
138 |
key.getPrimeP().toByteArray(), |
|
139 |
key.getPrimeQ().toByteArray(), |
|
140 |
key.getPrimeExponentP().toByteArray(), |
|
141 |
key.getPrimeExponentQ().toByteArray(), |
|
142 |
key.getCrtCoefficient().toByteArray()); |
|
143 |
||
144 |
privateKey = storePrivateKey(keyBlob, |
|
145 |
"{" + UUID.randomUUID().toString() + "}", keyBitLength); |
|
146 |
} |
|
147 |
||
148 |
/** |
|
149 |
* Gets the certificate chain for the keystore entry. |
|
150 |
*/ |
|
151 |
X509Certificate[] getCertificateChain() |
|
152 |
{ |
|
153 |
return certChain; |
|
154 |
} |
|
155 |
||
156 |
/** |
|
157 |
* Sets the certificate chain for the keystore entry. |
|
158 |
*/ |
|
159 |
void setCertificateChain(X509Certificate[] chain) |
|
9508
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
5506
diff
changeset
|
160 |
throws CertificateException, KeyStoreException |
2 | 161 |
{ |
162 |
for (int i = 0; i < chain.length; i++) { |
|
163 |
byte[] encoding = chain[i].getEncoded(); |
|
164 |
if (i == 0 && privateKey != null) { |
|
165 |
storeCertificate(getName(), alias, encoding, |
|
166 |
encoding.length, privateKey.getHCryptProvider(), |
|
167 |
privateKey.getHCryptKey()); |
|
168 |
||
169 |
} else { |
|
170 |
storeCertificate(getName(), alias, encoding, |
|
171 |
encoding.length, 0L, 0L); // no private key to attach |
|
172 |
} |
|
173 |
} |
|
174 |
certChain = chain; |
|
175 |
} |
|
176 |
}; |
|
177 |
||
178 |
/* |
|
179 |
* An X.509 certificate factory. |
|
180 |
* Used to create an X.509 certificate from its DER-encoding. |
|
181 |
*/ |
|
182 |
private CertificateFactory certificateFactory = null; |
|
183 |
||
184 |
/* |
|
185 |
* Compatibility mode: for applications that assume keystores are |
|
186 |
* stream-based this mode tolerates (but ignores) a non-null stream |
|
187 |
* or password parameter when passed to the load or store methods. |
|
188 |
* The mode is enabled by default. |
|
189 |
*/ |
|
190 |
private static final String KEYSTORE_COMPATIBILITY_MODE_PROP = |
|
191 |
"sun.security.mscapi.keyStoreCompatibilityMode"; |
|
192 |
private final boolean keyStoreCompatibilityMode; |
|
193 |
||
194 |
/* |
|
195 |
* The keystore entries. |
|
196 |
*/ |
|
197 |
private Collection<KeyEntry> entries = new ArrayList<KeyEntry>(); |
|
198 |
||
199 |
/* |
|
200 |
* The keystore name. |
|
201 |
* Case is not significant. |
|
202 |
*/ |
|
203 |
private final String storeName; |
|
204 |
||
205 |
KeyStore(String storeName) { |
|
206 |
// Get the compatibility mode |
|
23582
d5fa3327ab3a
8038177: Eliminate unnecessary dependency to sun.security.action
mchung
parents:
10336
diff
changeset
|
207 |
String prop = AccessController.doPrivileged( |
d5fa3327ab3a
8038177: Eliminate unnecessary dependency to sun.security.action
mchung
parents:
10336
diff
changeset
|
208 |
(PrivilegedAction<String>) () -> System.getProperty(KEYSTORE_COMPATIBILITY_MODE_PROP)); |
2 | 209 |
|
210 |
if ("false".equalsIgnoreCase(prop)) { |
|
211 |
keyStoreCompatibilityMode = false; |
|
212 |
} else { |
|
213 |
keyStoreCompatibilityMode = true; |
|
214 |
} |
|
215 |
||
216 |
this.storeName = storeName; |
|
217 |
} |
|
218 |
||
219 |
/** |
|
220 |
* Returns the key associated with the given alias. |
|
221 |
* <p> |
|
222 |
* A compatibility mode is supported for applications that assume |
|
223 |
* a password must be supplied. It permits (but ignores) a non-null |
|
224 |
* <code>password</code>. The mode is enabled by default. |
|
225 |
* Set the |
|
226 |
* <code>sun.security.mscapi.keyStoreCompatibilityMode</code> |
|
227 |
* system property to <code>false</code> to disable compatibility mode |
|
228 |
* and reject a non-null <code>password</code>. |
|
229 |
* |
|
230 |
* @param alias the alias name |
|
231 |
* @param password the password, which should be <code>null</code> |
|
232 |
* |
|
233 |
* @return the requested key, or null if the given alias does not exist |
|
234 |
* or does not identify a <i>key entry</i>. |
|
235 |
* |
|
236 |
* @exception NoSuchAlgorithmException if the algorithm for recovering the |
|
237 |
* key cannot be found, |
|
238 |
* or if compatibility mode is disabled and <code>password</code> is |
|
239 |
* non-null. |
|
240 |
* @exception UnrecoverableKeyException if the key cannot be recovered. |
|
241 |
*/ |
|
242 |
public java.security.Key engineGetKey(String alias, char[] password) |
|
243 |
throws NoSuchAlgorithmException, UnrecoverableKeyException |
|
244 |
{ |
|
245 |
if (alias == null) { |
|
246 |
return null; |
|
247 |
} |
|
248 |
||
249 |
if (password != null && !keyStoreCompatibilityMode) { |
|
250 |
throw new UnrecoverableKeyException("Password must be null"); |
|
251 |
} |
|
252 |
||
253 |
if (engineIsKeyEntry(alias) == false) |
|
254 |
return null; |
|
255 |
||
256 |
for (KeyEntry entry : entries) { |
|
257 |
if (alias.equals(entry.getAlias())) { |
|
258 |
return entry.getPrivateKey(); |
|
259 |
} |
|
260 |
} |
|
261 |
||
262 |
return null; |
|
263 |
} |
|
264 |
||
265 |
/** |
|
266 |
* Returns the certificate chain associated with the given alias. |
|
267 |
* |
|
268 |
* @param alias the alias name |
|
269 |
* |
|
270 |
* @return the certificate chain (ordered with the user's certificate first |
|
271 |
* and the root certificate authority last), or null if the given alias |
|
272 |
* does not exist or does not contain a certificate chain (i.e., the given |
|
273 |
* alias identifies either a <i>trusted certificate entry</i> or a |
|
274 |
* <i>key entry</i> without a certificate chain). |
|
275 |
*/ |
|
276 |
public Certificate[] engineGetCertificateChain(String alias) |
|
277 |
{ |
|
278 |
if (alias == null) { |
|
279 |
return null; |
|
280 |
} |
|
281 |
||
282 |
for (KeyEntry entry : entries) { |
|
283 |
if (alias.equals(entry.getAlias())) { |
|
284 |
X509Certificate[] certChain = entry.getCertificateChain(); |
|
285 |
||
286 |
return certChain.clone(); |
|
287 |
} |
|
288 |
} |
|
289 |
||
290 |
return null; |
|
291 |
} |
|
292 |
||
293 |
/** |
|
294 |
* Returns the certificate associated with the given alias. |
|
295 |
* |
|
296 |
* <p>If the given alias name identifies a |
|
297 |
* <i>trusted certificate entry</i>, the certificate associated with that |
|
298 |
* entry is returned. If the given alias name identifies a |
|
299 |
* <i>key entry</i>, the first element of the certificate chain of that |
|
300 |
* entry is returned, or null if that entry does not have a certificate |
|
301 |
* chain. |
|
302 |
* |
|
303 |
* @param alias the alias name |
|
304 |
* |
|
305 |
* @return the certificate, or null if the given alias does not exist or |
|
306 |
* does not contain a certificate. |
|
307 |
*/ |
|
308 |
public Certificate engineGetCertificate(String alias) |
|
309 |
{ |
|
310 |
if (alias == null) { |
|
311 |
return null; |
|
312 |
} |
|
313 |
||
314 |
for (KeyEntry entry : entries) { |
|
315 |
if (alias.equals(entry.getAlias())) |
|
316 |
{ |
|
317 |
X509Certificate[] certChain = entry.getCertificateChain(); |
|
318 |
return certChain[0]; |
|
319 |
} |
|
320 |
} |
|
321 |
||
322 |
return null; |
|
323 |
} |
|
324 |
||
325 |
/** |
|
326 |
* Returns the creation date of the entry identified by the given alias. |
|
327 |
* |
|
328 |
* @param alias the alias name |
|
329 |
* |
|
330 |
* @return the creation date of this entry, or null if the given alias does |
|
331 |
* not exist |
|
332 |
*/ |
|
333 |
public Date engineGetCreationDate(String alias) { |
|
334 |
if (alias == null) { |
|
335 |
return null; |
|
336 |
} |
|
337 |
return new Date(); |
|
338 |
} |
|
339 |
||
340 |
/** |
|
341 |
* Stores the given private key and associated certificate chain in the |
|
342 |
* keystore. |
|
343 |
* |
|
344 |
* <p>The given java.security.PrivateKey <code>key</code> must |
|
345 |
* be accompanied by a certificate chain certifying the |
|
346 |
* corresponding public key. |
|
347 |
* |
|
348 |
* <p>If the given alias already exists, the keystore information |
|
349 |
* associated with it is overridden by the given key and certificate |
|
350 |
* chain. Otherwise, a new entry is created. |
|
351 |
* |
|
352 |
* <p> |
|
353 |
* A compatibility mode is supported for applications that assume |
|
354 |
* a password must be supplied. It permits (but ignores) a non-null |
|
355 |
* <code>password</code>. The mode is enabled by default. |
|
356 |
* Set the |
|
357 |
* <code>sun.security.mscapi.keyStoreCompatibilityMode</code> |
|
358 |
* system property to <code>false</code> to disable compatibility mode |
|
359 |
* and reject a non-null <code>password</code>. |
|
360 |
* |
|
361 |
* @param alias the alias name |
|
362 |
* @param key the private key to be associated with the alias |
|
363 |
* @param password the password, which should be <code>null</code> |
|
364 |
* @param chain the certificate chain for the corresponding public |
|
365 |
* key (only required if the given key is of type |
|
366 |
* <code>java.security.PrivateKey</code>). |
|
367 |
* |
|
368 |
* @exception KeyStoreException if the given key is not a private key, |
|
369 |
* cannot be protected, or if compatibility mode is disabled and |
|
370 |
* <code>password</code> is non-null, or if this operation fails for |
|
371 |
* some other reason. |
|
372 |
*/ |
|
373 |
public void engineSetKeyEntry(String alias, java.security.Key key, |
|
374 |
char[] password, Certificate[] chain) throws KeyStoreException |
|
375 |
{ |
|
376 |
if (alias == null) { |
|
377 |
throw new KeyStoreException("alias must not be null"); |
|
378 |
} |
|
379 |
||
380 |
if (password != null && !keyStoreCompatibilityMode) { |
|
381 |
throw new KeyStoreException("Password must be null"); |
|
382 |
} |
|
383 |
||
384 |
if (key instanceof RSAPrivateCrtKey) { |
|
385 |
||
386 |
KeyEntry entry = null; |
|
387 |
boolean found = false; |
|
388 |
||
389 |
for (KeyEntry e : entries) { |
|
390 |
if (alias.equals(e.getAlias())) { |
|
391 |
found = true; |
|
392 |
entry = e; |
|
393 |
break; |
|
394 |
} |
|
395 |
} |
|
396 |
||
397 |
if (! found) { |
|
398 |
entry = |
|
399 |
//TODO new KeyEntry(alias, key, (X509Certificate[]) chain); |
|
400 |
new KeyEntry(alias, null, (X509Certificate[]) chain); |
|
401 |
entries.add(entry); |
|
402 |
} |
|
403 |
||
404 |
entry.setAlias(alias); |
|
405 |
||
406 |
try { |
|
9508
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
5506
diff
changeset
|
407 |
entry.setPrivateKey((RSAPrivateCrtKey) key); |
2 | 408 |
entry.setCertificateChain((X509Certificate[]) chain); |
409 |
||
410 |
} catch (CertificateException ce) { |
|
411 |
throw new KeyStoreException(ce); |
|
9508
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
5506
diff
changeset
|
412 |
|
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
5506
diff
changeset
|
413 |
} catch (InvalidKeyException ike) { |
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
5506
diff
changeset
|
414 |
throw new KeyStoreException(ike); |
2 | 415 |
} |
416 |
||
417 |
} else { |
|
418 |
throw new UnsupportedOperationException( |
|
419 |
"Cannot assign the key to the given alias."); |
|
420 |
} |
|
421 |
} |
|
422 |
||
423 |
/** |
|
424 |
* Assigns the given key (that has already been protected) to the given |
|
425 |
* alias. |
|
426 |
* |
|
427 |
* <p>If the protected key is of type |
|
428 |
* <code>java.security.PrivateKey</code>, it must be accompanied by a |
|
429 |
* certificate chain certifying the corresponding public key. If the |
|
430 |
* underlying keystore implementation is of type <code>jks</code>, |
|
431 |
* <code>key</code> must be encoded as an |
|
432 |
* <code>EncryptedPrivateKeyInfo</code> as defined in the PKCS #8 standard. |
|
433 |
* |
|
434 |
* <p>If the given alias already exists, the keystore information |
|
435 |
* associated with it is overridden by the given key (and possibly |
|
436 |
* certificate chain). |
|
437 |
* |
|
438 |
* @param alias the alias name |
|
439 |
* @param key the key (in protected format) to be associated with the alias |
|
440 |
* @param chain the certificate chain for the corresponding public |
|
441 |
* key (only useful if the protected key is of type |
|
442 |
* <code>java.security.PrivateKey</code>). |
|
443 |
* |
|
444 |
* @exception KeyStoreException if this operation fails. |
|
445 |
*/ |
|
446 |
public void engineSetKeyEntry(String alias, byte[] key, |
|
447 |
Certificate[] chain) |
|
448 |
throws KeyStoreException |
|
449 |
{ |
|
450 |
throw new UnsupportedOperationException( |
|
451 |
"Cannot assign the encoded key to the given alias."); |
|
452 |
} |
|
453 |
||
454 |
/** |
|
455 |
* Assigns the given certificate to the given alias. |
|
456 |
* |
|
457 |
* <p>If the given alias already exists in this keystore and identifies a |
|
458 |
* <i>trusted certificate entry</i>, the certificate associated with it is |
|
459 |
* overridden by the given certificate. |
|
460 |
* |
|
461 |
* @param alias the alias name |
|
462 |
* @param cert the certificate |
|
463 |
* |
|
464 |
* @exception KeyStoreException if the given alias already exists and does |
|
465 |
* not identify a <i>trusted certificate entry</i>, or this operation |
|
466 |
* fails for some other reason. |
|
467 |
*/ |
|
468 |
public void engineSetCertificateEntry(String alias, Certificate cert) |
|
469 |
throws KeyStoreException |
|
470 |
{ |
|
471 |
if (alias == null) { |
|
472 |
throw new KeyStoreException("alias must not be null"); |
|
473 |
} |
|
474 |
||
475 |
if (cert instanceof X509Certificate) { |
|
476 |
||
477 |
// TODO - build CryptoAPI chain? |
|
478 |
X509Certificate[] chain = |
|
479 |
new X509Certificate[]{ (X509Certificate) cert }; |
|
480 |
KeyEntry entry = null; |
|
481 |
boolean found = false; |
|
482 |
||
483 |
for (KeyEntry e : entries) { |
|
484 |
if (alias.equals(e.getAlias())) { |
|
485 |
found = true; |
|
486 |
entry = e; |
|
487 |
break; |
|
488 |
} |
|
489 |
} |
|
490 |
||
491 |
if (! found) { |
|
492 |
entry = |
|
493 |
new KeyEntry(alias, null, chain); |
|
494 |
entries.add(entry); |
|
495 |
||
496 |
} |
|
497 |
if (entry.getPrivateKey() == null) { // trusted-cert entry |
|
498 |
entry.setAlias(alias); |
|
499 |
||
500 |
try { |
|
501 |
entry.setCertificateChain(chain); |
|
502 |
||
503 |
} catch (CertificateException ce) { |
|
504 |
throw new KeyStoreException(ce); |
|
505 |
} |
|
506 |
} |
|
507 |
||
508 |
} else { |
|
509 |
throw new UnsupportedOperationException( |
|
510 |
"Cannot assign the certificate to the given alias."); |
|
511 |
} |
|
512 |
} |
|
513 |
||
514 |
/** |
|
515 |
* Deletes the entry identified by the given alias from this keystore. |
|
516 |
* |
|
517 |
* @param alias the alias name |
|
518 |
* |
|
519 |
* @exception KeyStoreException if the entry cannot be removed. |
|
520 |
*/ |
|
521 |
public void engineDeleteEntry(String alias) |
|
522 |
throws KeyStoreException |
|
523 |
{ |
|
524 |
if (alias == null) { |
|
525 |
throw new KeyStoreException("alias must not be null"); |
|
526 |
} |
|
527 |
||
528 |
for (KeyEntry entry : entries) { |
|
529 |
if (alias.equals(entry.getAlias())) { |
|
530 |
||
531 |
// Get end-entity certificate and remove from system cert store |
|
532 |
X509Certificate[] certChain = entry.getCertificateChain(); |
|
533 |
if (certChain != null) { |
|
534 |
||
535 |
try { |
|
536 |
||
537 |
byte[] encoding = certChain[0].getEncoded(); |
|
538 |
removeCertificate(getName(), alias, encoding, |
|
539 |
encoding.length); |
|
540 |
||
9508
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
5506
diff
changeset
|
541 |
} catch (CertificateException e) { |
2 | 542 |
throw new KeyStoreException("Cannot remove entry: " + |
543 |
e); |
|
544 |
} |
|
545 |
} |
|
546 |
Key privateKey = entry.getPrivateKey(); |
|
547 |
if (privateKey != null) { |
|
548 |
destroyKeyContainer( |
|
549 |
Key.getContainerName(privateKey.getHCryptProvider())); |
|
550 |
} |
|
551 |
||
552 |
entries.remove(entry); |
|
553 |
break; |
|
554 |
} |
|
555 |
} |
|
556 |
} |
|
557 |
||
558 |
/** |
|
559 |
* Lists all the alias names of this keystore. |
|
560 |
* |
|
561 |
* @return enumeration of the alias names |
|
562 |
*/ |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9508
diff
changeset
|
563 |
public Enumeration<String> engineAliases() { |
2 | 564 |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9508
diff
changeset
|
565 |
final Iterator<KeyEntry> iter = entries.iterator(); |
2 | 566 |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9508
diff
changeset
|
567 |
return new Enumeration<String>() |
2 | 568 |
{ |
569 |
public boolean hasMoreElements() |
|
570 |
{ |
|
571 |
return iter.hasNext(); |
|
572 |
} |
|
573 |
||
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9508
diff
changeset
|
574 |
public String nextElement() |
2 | 575 |
{ |
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9508
diff
changeset
|
576 |
KeyEntry entry = iter.next(); |
2 | 577 |
return entry.getAlias(); |
578 |
} |
|
579 |
}; |
|
580 |
} |
|
581 |
||
582 |
/** |
|
583 |
* Checks if the given alias exists in this keystore. |
|
584 |
* |
|
585 |
* @param alias the alias name |
|
586 |
* |
|
587 |
* @return true if the alias exists, false otherwise |
|
588 |
*/ |
|
589 |
public boolean engineContainsAlias(String alias) { |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9508
diff
changeset
|
590 |
for (Enumeration<String> enumerator = engineAliases(); |
2 | 591 |
enumerator.hasMoreElements();) |
592 |
{ |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9508
diff
changeset
|
593 |
String a = enumerator.nextElement(); |
2 | 594 |
|
595 |
if (a.equals(alias)) |
|
596 |
return true; |
|
597 |
} |
|
598 |
return false; |
|
599 |
} |
|
600 |
||
601 |
/** |
|
602 |
* Retrieves the number of entries in this keystore. |
|
603 |
* |
|
604 |
* @return the number of entries in this keystore |
|
605 |
*/ |
|
606 |
public int engineSize() { |
|
607 |
return entries.size(); |
|
608 |
} |
|
609 |
||
610 |
/** |
|
611 |
* Returns true if the entry identified by the given alias is a |
|
612 |
* <i>key entry</i>, and false otherwise. |
|
613 |
* |
|
614 |
* @return true if the entry identified by the given alias is a |
|
615 |
* <i>key entry</i>, false otherwise. |
|
616 |
*/ |
|
617 |
public boolean engineIsKeyEntry(String alias) { |
|
618 |
||
619 |
if (alias == null) { |
|
620 |
return false; |
|
621 |
} |
|
622 |
||
623 |
for (KeyEntry entry : entries) { |
|
624 |
if (alias.equals(entry.getAlias())) { |
|
625 |
return entry.getPrivateKey() != null; |
|
626 |
} |
|
627 |
} |
|
628 |
||
629 |
return false; |
|
630 |
} |
|
631 |
||
632 |
/** |
|
633 |
* Returns true if the entry identified by the given alias is a |
|
634 |
* <i>trusted certificate entry</i>, and false otherwise. |
|
635 |
* |
|
636 |
* @return true if the entry identified by the given alias is a |
|
637 |
* <i>trusted certificate entry</i>, false otherwise. |
|
638 |
*/ |
|
639 |
public boolean engineIsCertificateEntry(String alias) |
|
640 |
{ |
|
641 |
for (KeyEntry entry : entries) { |
|
642 |
if (alias.equals(entry.getAlias())) { |
|
643 |
return entry.getPrivateKey() == null; |
|
644 |
} |
|
645 |
} |
|
646 |
||
647 |
return false; |
|
648 |
} |
|
649 |
||
650 |
/** |
|
651 |
* Returns the (alias) name of the first keystore entry whose certificate |
|
652 |
* matches the given certificate. |
|
653 |
* |
|
654 |
* <p>This method attempts to match the given certificate with each |
|
655 |
* keystore entry. If the entry being considered |
|
656 |
* is a <i>trusted certificate entry</i>, the given certificate is |
|
657 |
* compared to that entry's certificate. If the entry being considered is |
|
658 |
* a <i>key entry</i>, the given certificate is compared to the first |
|
659 |
* element of that entry's certificate chain (if a chain exists). |
|
660 |
* |
|
661 |
* @param cert the certificate to match with. |
|
662 |
* |
|
663 |
* @return the (alias) name of the first entry with matching certificate, |
|
664 |
* or null if no such entry exists in this keystore. |
|
665 |
*/ |
|
666 |
public String engineGetCertificateAlias(Certificate cert) |
|
667 |
{ |
|
668 |
for (KeyEntry entry : entries) { |
|
669 |
if (entry.certChain != null && entry.certChain[0].equals(cert)) { |
|
670 |
return entry.getAlias(); |
|
671 |
} |
|
672 |
} |
|
673 |
||
674 |
return null; |
|
675 |
} |
|
676 |
||
677 |
/** |
|
678 |
* engineStore is currently a no-op. |
|
679 |
* Entries are stored during engineSetEntry. |
|
680 |
* |
|
681 |
* A compatibility mode is supported for applications that assume |
|
682 |
* keystores are stream-based. It permits (but ignores) a non-null |
|
683 |
* <code>stream</code> or <code>password</code>. |
|
684 |
* The mode is enabled by default. |
|
685 |
* Set the |
|
686 |
* <code>sun.security.mscapi.keyStoreCompatibilityMode</code> |
|
687 |
* system property to <code>false</code> to disable compatibility mode |
|
688 |
* and reject a non-null <code>stream</code> or <code>password</code>. |
|
689 |
* |
|
690 |
* @param stream the output stream, which should be <code>null</code> |
|
691 |
* @param password the password, which should be <code>null</code> |
|
692 |
* |
|
693 |
* @exception IOException if compatibility mode is disabled and either |
|
694 |
* parameter is non-null. |
|
695 |
*/ |
|
696 |
public void engineStore(OutputStream stream, char[] password) |
|
697 |
throws IOException, NoSuchAlgorithmException, CertificateException |
|
698 |
{ |
|
699 |
if (stream != null && !keyStoreCompatibilityMode) { |
|
700 |
throw new IOException("Keystore output stream must be null"); |
|
701 |
} |
|
702 |
||
703 |
if (password != null && !keyStoreCompatibilityMode) { |
|
704 |
throw new IOException("Keystore password must be null"); |
|
705 |
} |
|
706 |
} |
|
707 |
||
708 |
/** |
|
709 |
* Loads the keystore. |
|
710 |
* |
|
711 |
* A compatibility mode is supported for applications that assume |
|
712 |
* keystores are stream-based. It permits (but ignores) a non-null |
|
713 |
* <code>stream</code> or <code>password</code>. |
|
714 |
* The mode is enabled by default. |
|
715 |
* Set the |
|
716 |
* <code>sun.security.mscapi.keyStoreCompatibilityMode</code> |
|
717 |
* system property to <code>false</code> to disable compatibility mode |
|
718 |
* and reject a non-null <code>stream</code> or <code>password</code>. |
|
719 |
* |
|
720 |
* @param stream the input stream, which should be <code>null</code>. |
|
721 |
* @param password the password, which should be <code>null</code>. |
|
722 |
* |
|
723 |
* @exception IOException if there is an I/O or format problem with the |
|
724 |
* keystore data. Or if compatibility mode is disabled and either |
|
725 |
* parameter is non-null. |
|
726 |
* @exception NoSuchAlgorithmException if the algorithm used to check |
|
727 |
* the integrity of the keystore cannot be found |
|
728 |
* @exception CertificateException if any of the certificates in the |
|
729 |
* keystore could not be loaded |
|
730 |
* @exception SecurityException if the security check for |
|
731 |
* <code>SecurityPermission("authProvider.<i>name</i>")</code> does not |
|
732 |
* pass, where <i>name</i> is the value returned by |
|
733 |
* this provider's <code>getName</code> method. |
|
734 |
*/ |
|
735 |
public void engineLoad(InputStream stream, char[] password) |
|
736 |
throws IOException, NoSuchAlgorithmException, CertificateException |
|
737 |
{ |
|
738 |
if (stream != null && !keyStoreCompatibilityMode) { |
|
739 |
throw new IOException("Keystore input stream must be null"); |
|
740 |
} |
|
741 |
||
742 |
if (password != null && !keyStoreCompatibilityMode) { |
|
743 |
throw new IOException("Keystore password must be null"); |
|
744 |
} |
|
745 |
||
746 |
/* |
|
747 |
* Use the same security check as AuthProvider.login |
|
748 |
*/ |
|
749 |
SecurityManager sm = System.getSecurityManager(); |
|
750 |
if (sm != null) { |
|
751 |
sm.checkPermission(new SecurityPermission( |
|
752 |
"authProvider.SunMSCAPI")); |
|
753 |
} |
|
754 |
||
755 |
// Clear all key entries |
|
756 |
entries.clear(); |
|
757 |
||
9508
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
5506
diff
changeset
|
758 |
try { |
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
5506
diff
changeset
|
759 |
|
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
5506
diff
changeset
|
760 |
// Load keys and/or certificate chains |
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
5506
diff
changeset
|
761 |
loadKeysOrCertificateChains(getName(), entries); |
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
5506
diff
changeset
|
762 |
|
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
5506
diff
changeset
|
763 |
} catch (KeyStoreException e) { |
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
5506
diff
changeset
|
764 |
throw new IOException(e); |
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
5506
diff
changeset
|
765 |
} |
2 | 766 |
} |
767 |
||
768 |
/** |
|
769 |
* Generates a certificate chain from the collection of |
|
770 |
* certificates and stores the result into a key entry. |
|
771 |
*/ |
|
772 |
private void generateCertificateChain(String alias, |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9508
diff
changeset
|
773 |
Collection<? extends Certificate> certCollection, |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9508
diff
changeset
|
774 |
Collection<KeyEntry> entries) |
2 | 775 |
{ |
776 |
try |
|
777 |
{ |
|
778 |
X509Certificate[] certChain = |
|
779 |
new X509Certificate[certCollection.size()]; |
|
780 |
||
781 |
int i = 0; |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9508
diff
changeset
|
782 |
for (Iterator<? extends Certificate> iter = |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9508
diff
changeset
|
783 |
certCollection.iterator(); iter.hasNext(); i++) |
2 | 784 |
{ |
785 |
certChain[i] = (X509Certificate) iter.next(); |
|
786 |
} |
|
787 |
||
788 |
KeyEntry entry = new KeyEntry(alias, null, certChain); |
|
789 |
||
790 |
// Add cert chain |
|
791 |
entries.add(entry); |
|
792 |
} |
|
793 |
catch (Throwable e) |
|
794 |
{ |
|
795 |
// Ignore the exception and skip this entry |
|
796 |
// TODO - throw CertificateException? |
|
797 |
} |
|
798 |
} |
|
799 |
||
800 |
/** |
|
801 |
* Generates RSA key and certificate chain from the private key handle, |
|
802 |
* collection of certificates and stores the result into key entries. |
|
803 |
*/ |
|
804 |
private void generateRSAKeyAndCertificateChain(String alias, |
|
805 |
long hCryptProv, long hCryptKey, int keyLength, |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9508
diff
changeset
|
806 |
Collection<? extends Certificate> certCollection, |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9508
diff
changeset
|
807 |
Collection<KeyEntry> entries) |
2 | 808 |
{ |
809 |
try |
|
810 |
{ |
|
811 |
X509Certificate[] certChain = |
|
812 |
new X509Certificate[certCollection.size()]; |
|
813 |
||
814 |
int i = 0; |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9508
diff
changeset
|
815 |
for (Iterator<? extends Certificate> iter = |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9508
diff
changeset
|
816 |
certCollection.iterator(); iter.hasNext(); i++) |
2 | 817 |
{ |
818 |
certChain[i] = (X509Certificate) iter.next(); |
|
819 |
} |
|
820 |
||
821 |
KeyEntry entry = new KeyEntry(alias, new RSAPrivateKey(hCryptProv, |
|
822 |
hCryptKey, keyLength), certChain); |
|
823 |
||
824 |
// Add cert chain |
|
825 |
entries.add(entry); |
|
826 |
} |
|
827 |
catch (Throwable e) |
|
828 |
{ |
|
829 |
// Ignore the exception and skip this entry |
|
830 |
// TODO - throw CertificateException? |
|
831 |
} |
|
832 |
} |
|
833 |
||
834 |
/** |
|
835 |
* Generates certificates from byte data and stores into cert collection. |
|
836 |
* |
|
837 |
* @param data Byte data. |
|
838 |
* @param certCollection Collection of certificates. |
|
839 |
*/ |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9508
diff
changeset
|
840 |
private void generateCertificate(byte[] data, |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9508
diff
changeset
|
841 |
Collection<Certificate> certCollection) { |
2 | 842 |
try |
843 |
{ |
|
844 |
ByteArrayInputStream bis = new ByteArrayInputStream(data); |
|
845 |
||
846 |
// Obtain certificate factory |
|
847 |
if (certificateFactory == null) { |
|
848 |
certificateFactory = CertificateFactory.getInstance("X.509"); |
|
849 |
} |
|
850 |
||
851 |
// Generate certificate |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9508
diff
changeset
|
852 |
Collection<? extends Certificate> c = |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9508
diff
changeset
|
853 |
certificateFactory.generateCertificates(bis); |
2 | 854 |
certCollection.addAll(c); |
855 |
} |
|
856 |
catch (CertificateException e) |
|
857 |
{ |
|
858 |
// Ignore the exception and skip this certificate |
|
859 |
// TODO - throw CertificateException? |
|
860 |
} |
|
861 |
catch (Throwable te) |
|
862 |
{ |
|
863 |
// Ignore the exception and skip this certificate |
|
864 |
// TODO - throw CertificateException? |
|
865 |
} |
|
866 |
} |
|
867 |
||
868 |
/** |
|
869 |
* Returns the name of the keystore. |
|
870 |
*/ |
|
871 |
private String getName() |
|
872 |
{ |
|
873 |
return storeName; |
|
874 |
} |
|
875 |
||
876 |
/** |
|
877 |
* Load keys and/or certificates from keystore into Collection. |
|
878 |
* |
|
879 |
* @param name Name of keystore. |
|
880 |
* @param entries Collection of key/certificate. |
|
881 |
*/ |
|
882 |
private native void loadKeysOrCertificateChains(String name, |
|
9508
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
5506
diff
changeset
|
883 |
Collection<KeyEntry> entries) throws KeyStoreException; |
2 | 884 |
|
885 |
/** |
|
886 |
* Stores a DER-encoded certificate into the certificate store |
|
887 |
* |
|
888 |
* @param name Name of the keystore. |
|
889 |
* @param alias Name of the certificate. |
|
890 |
* @param encoding DER-encoded certificate. |
|
891 |
*/ |
|
892 |
private native void storeCertificate(String name, String alias, |
|
893 |
byte[] encoding, int encodingLength, long hCryptProvider, |
|
9508
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
5506
diff
changeset
|
894 |
long hCryptKey) throws CertificateException, KeyStoreException; |
2 | 895 |
|
896 |
/** |
|
897 |
* Removes the certificate from the certificate store |
|
898 |
* |
|
899 |
* @param name Name of the keystore. |
|
900 |
* @param alias Name of the certificate. |
|
901 |
* @param encoding DER-encoded certificate. |
|
902 |
*/ |
|
903 |
private native void removeCertificate(String name, String alias, |
|
9508
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
5506
diff
changeset
|
904 |
byte[] encoding, int encodingLength) |
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
5506
diff
changeset
|
905 |
throws CertificateException, KeyStoreException; |
2 | 906 |
|
907 |
/** |
|
908 |
* Destroys the key container. |
|
909 |
* |
|
910 |
* @param keyContainerName The name of the key container. |
|
911 |
*/ |
|
9508
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
5506
diff
changeset
|
912 |
private native void destroyKeyContainer(String keyContainerName) |
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
5506
diff
changeset
|
913 |
throws KeyStoreException; |
2 | 914 |
|
915 |
/** |
|
916 |
* Generates a private-key BLOB from a key's components. |
|
917 |
*/ |
|
918 |
private native byte[] generatePrivateKeyBlob( |
|
919 |
int keyBitLength, |
|
920 |
byte[] modulus, |
|
921 |
byte[] publicExponent, |
|
922 |
byte[] privateExponent, |
|
923 |
byte[] primeP, |
|
924 |
byte[] primeQ, |
|
925 |
byte[] exponentP, |
|
926 |
byte[] exponentQ, |
|
9508
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
5506
diff
changeset
|
927 |
byte[] crtCoefficient) throws InvalidKeyException; |
2 | 928 |
|
929 |
private native RSAPrivateKey storePrivateKey(byte[] keyBlob, |
|
9508
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
5506
diff
changeset
|
930 |
String keyContainerName, int keySize) throws KeyStoreException; |
2 | 931 |
} |