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