API Overview API Index Package Overview Direct link to this page
JDK 1.6
  java.security. KeyStoreSpi View Javadoc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576

/*
 * @(#)KeyStoreSpi.java	1.22 06/04/07
 *
 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package java.security;

import java.io.*;
import java.util.*;

import java.security.KeyStore.*;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;

import javax.crypto.SecretKey;

import javax.security.auth.callback.*;

/**
 * This class defines the <i>Service Provider Interface</i> (<b>SPI</b>)
 * for the <code>KeyStore</code> class.
 * All the abstract methods in this class must be implemented by each
 * cryptographic service provider who wishes to supply the implementation
 * of a keystore for a particular keystore type.
 *
 * @author Jan Luehe
 *
 * @version 1.22, 04/07/06
 *
 * @see KeyStore
 *
 * @since 1.2
 */

public abstract class KeyStoreSpi {

    /**
     * Returns the key associated with the given alias, using the given
     * password to recover it.  The key must have been associated with
     * the alias by a call to <code>setKeyEntry</code>,
     * or by a call to <code>setEntry</code> with a
     * <code>PrivateKeyEntry</code> or <code>SecretKeyEntry</code>.
     *
     * @param alias the alias name
     * @param password the password for recovering the key
     *
     * @return the requested key, or null if the given alias does not exist
     * or does not identify a key-related entry.
     *
     * @exception NoSuchAlgorithmException if the algorithm for recovering the
     * key cannot be found
     * @exception UnrecoverableKeyException if the key cannot be recovered
     * (e.g., the given password is wrong).
     */
    public abstract Key engineGetKey(String alias, char[] password)
	throws NoSuchAlgorithmException, UnrecoverableKeyException;

    /**
     * Returns the certificate chain associated with the given alias.
     * The certificate chain must have been associated with the alias
     * by a call to <code>setKeyEntry</code>,
     * or by a call to <code>setEntry</code> with a
     * <code>PrivateKeyEntry</code>.
     *
     * @param alias the alias name
     *
     * @return the certificate chain (ordered with the user's certificate first
     * and the root certificate authority last), or null if the given alias
     * does not exist or does not contain a certificate chain
     */
    public abstract Certificate[] engineGetCertificateChain(String alias);

    /**
     * Returns the certificate associated with the given alias.
     *
     * <p> If the given alias name identifies an entry
     * created by a call to <code>setCertificateEntry</code>,
     * or created by a call to <code>setEntry</code> with a
     * <code>TrustedCertificateEntry</code>,
     * then the trusted certificate contained in that entry is returned.
     *
     * <p> If the given alias name identifies an entry
     * created by a call to <code>setKeyEntry</code>,
     * or created by a call to <code>setEntry</code> with a
     * <code>PrivateKeyEntry</code>,
     * then the first element of the certificate chain in that entry
     * (if a chain exists) is returned.
     *
     * @param alias the alias name
     *
     * @return the certificate, or null if the given alias does not exist or
     * does not contain a certificate.
     */
    public abstract Certificate engineGetCertificate(String alias);

    /**
     * Returns the creation date of the entry identified by the given alias.
     *
     * @param alias the alias name
     *
     * @return the creation date of this entry, or null if the given alias does
     * not exist
     */
    public abstract Date engineGetCreationDate(String alias);

    /**
     * Assigns the given key to the given alias, protecting it with the given
     * password.
     *
     * <p>If the given key is of type <code>java.security.PrivateKey</code>,
     * it must be accompanied by a certificate chain certifying the
     * corresponding public key.
     *
     * <p>If the given alias already exists, the keystore information
     * associated with it is overridden by the given key (and possibly
     * certificate chain).
     *
     * @param alias the alias name
     * @param key the key to be associated with the alias
     * @param password the password to protect the key
     * @param chain the certificate chain for the corresponding public
     * key (only required if the given key is of type
     * <code>java.security.PrivateKey</code>).
     *
     * @exception KeyStoreException if the given key cannot be protected, or
     * this operation fails for some other reason
     */
    public abstract void engineSetKeyEntry(String alias, Key key,
					   char[] password,
					   Certificate[] chain)
	throws KeyStoreException;

    /**
     * Assigns the given key (that has already been protected) to the given
     * alias.
     *
     * <p>If the protected key is of type
     * <code>java.security.PrivateKey</code>,
     * it must be accompanied by a certificate chain certifying the
     * corresponding public key.
     *
     * <p>If the given alias already exists, the keystore information
     * associated with it is overridden by the given key (and possibly
     * certificate chain).
     *
     * @param alias the alias name
     * @param key the key (in protected format) to be associated with the alias
     * @param chain the certificate chain for the corresponding public
     * key (only useful if the protected key is of type
     * <code>java.security.PrivateKey</code>).
     *
     * @exception KeyStoreException if this operation fails.
     */
    public abstract void engineSetKeyEntry(String alias, byte[] key,
					   Certificate[] chain)
	throws KeyStoreException;

    /**
     * Assigns the given certificate to the given alias.
     *
     * <p> If the given alias identifies an existing entry
     * created by a call to <code>setCertificateEntry</code>,
     * or created by a call to <code>setEntry</code> with a
     * <code>TrustedCertificateEntry</code>,
     * the trusted certificate in the existing entry
     * is overridden by the given certificate.
     *
     * @param alias the alias name
     * @param cert the certificate
     *
     * @exception KeyStoreException if the given alias already exists and does
     * not identify an entry containing a trusted certificate,
     * or this operation fails for some other reason.
     */
    public abstract void engineSetCertificateEntry(String alias,
						   Certificate cert)
	throws KeyStoreException;

    /**
     * Deletes the entry identified by the given alias from this keystore.
     *
     * @param alias the alias name
     *
     * @exception KeyStoreException if the entry cannot be removed.
     */
    public abstract void engineDeleteEntry(String alias)
	throws KeyStoreException;

    /**
     * Lists all the alias names of this keystore.
     *
     * @return enumeration of the alias names
     */
    public abstract Enumeration<String> engineAliases();

    /**
     * Checks if the given alias exists in this keystore.
     *
     * @param alias the alias name
     *
     * @return true if the alias exists, false otherwise
     */
    public abstract boolean engineContainsAlias(String alias);

    /**
     * Retrieves the number of entries in this keystore.
     *
     * @return the number of entries in this keystore
     */
    public abstract int engineSize();

    /**
     * Returns true if the entry identified by the given alias
     * was created by a call to <code>setKeyEntry</code>,
     * or created by a call to <code>setEntry</code> with a
     * <code>PrivateKeyEntry</code> or a <code>SecretKeyEntry</code>.
     *
     * @param alias the alias for the keystore entry to be checked
     *
     * @return true if the entry identified by the given alias is a
     * key-related, false otherwise.
     */
    public abstract boolean engineIsKeyEntry(String alias);

    /**
     * Returns true if the entry identified by the given alias
     * was created by a call to <code>setCertificateEntry</code>,
     * or created by a call to <code>setEntry</code> with a
     * <code>TrustedCertificateEntry</code>.
     *
     * @param alias the alias for the keystore entry to be checked
     *
     * @return true if the entry identified by the given alias contains a
     * trusted certificate, false otherwise.
     */
    public abstract boolean engineIsCertificateEntry(String alias);

    /**
     * Returns the (alias) name of the first keystore entry whose certificate
     * matches the given certificate.
     *
     * <p>This method attempts to match the given certificate with each
     * keystore entry. If the entry being considered was
     * created by a call to <code>setCertificateEntry</code>,
     * or created by a call to <code>setEntry</code> with a
     * <code>TrustedCertificateEntry</code>,
     * then the given certificate is compared to that entry's certificate.
     *
     * <p> If the entry being considered was
     * created by a call to <code>setKeyEntry</code>,
     * or created by a call to <code>setEntry</code> with a
     * <code>PrivateKeyEntry</code>,
     * then the given certificate is compared to the first
     * element of that entry's certificate chain.
     *
     * @param cert the certificate to match with.
     *
     * @return the alias name of the first entry with matching certificate,
     * or null if no such entry exists in this keystore.
     */
    public abstract String engineGetCertificateAlias(Certificate cert);

    /**
     * Stores this keystore to the given output stream, and protects its
     * integrity with the given password.
     *
     * @param stream the output stream to which this keystore is written.
     * @param password the password to generate the keystore integrity check
     *
     * @exception IOException if there was an I/O problem with data
     * @exception NoSuchAlgorithmException if the appropriate data integrity
     * algorithm could not be found
     * @exception CertificateException if any of the certificates included in
     * the keystore data could not be stored
     */
    public abstract void engineStore(OutputStream stream, char[] password)
	throws IOException, NoSuchAlgorithmException, CertificateException;

    /**
     * Stores this keystore using the given
     * <code>KeyStore.LoadStoreParmeter</code>.
     *
     * @param param the <code>KeyStore.LoadStoreParmeter</code>
     *		that specifies how to store the keystore,
     *		which may be <code>null</code>
     *
     * @exception IllegalArgumentException if the given
     *		<code>KeyStore.LoadStoreParmeter</code>
     *		input is not recognized
     * @exception IOException if there was an I/O problem with data
     * @exception NoSuchAlgorithmException if the appropriate data integrity
     *		algorithm could not be found
     * @exception CertificateException if any of the certificates included in
     *		the keystore data could not be stored
     *
     * @since 1.5
     */
    public void engineStore(KeyStore.LoadStoreParameter param)
		throws IOException, NoSuchAlgorithmException,
		CertificateException {
	throw new UnsupportedOperationException();
    }

    /**
     * Loads the keystore from the given input stream.
     *
     * <p>A password may be given to unlock the keystore
     * (e.g. the keystore resides on a hardware token device),
     * or to check the integrity of the keystore data.
     * If a password is not given for integrity checking,
     * then integrity checking is not performed.
     *
     * @param stream the input stream from which the keystore is loaded,
     * or <code>null</code>
     * @param password the password used to check the integrity of
     * the keystore, the password used to unlock the keystore,
     * or <code>null</code>
     *
     * @exception IOException if there is an I/O or format problem with the
     * keystore data, if a password is required but not given,
     * or if the given password was incorrect. If the error is due to a
     * wrong password, the {@link Throwable#getCause cause} of the 
     * <code>IOException</code> should be an 
     * <code>UnrecoverableKeyException</code>
     * @exception NoSuchAlgorithmException if the algorithm used to check
     * the integrity of the keystore cannot be found
     * @exception CertificateException if any of the certificates in the
     * keystore could not be loaded
     */
    public abstract void engineLoad(InputStream stream, char[] password)
	throws IOException, NoSuchAlgorithmException, CertificateException;

    /**
     * Loads the keystore using the given
     * <code>KeyStore.LoadStoreParameter</code>.
     *
     * <p> Note that if this KeyStore has already been loaded, it is
     * reinitialized and loaded again from the given parameter.
     *
     * @param param the <code>KeyStore.LoadStoreParameter</code>
     *		that specifies how to load the keystore,
     *		which may be <code>null</code>
     *
     * @exception IllegalArgumentException if the given
     *		<code>KeyStore.LoadStoreParameter</code>
     *		input is not recognized
     * @exception IOException if there is an I/O or format problem with the
     *		keystore data. If the error is due to an incorrect 
     *         <code>ProtectionParameter</code> (e.g. wrong password)
     *         the {@link Throwable#getCause cause} of the 
     *         <code>IOException</code> should be an 
     *         <code>UnrecoverableKeyException</code>
     * @exception NoSuchAlgorithmException if the algorithm used to check
     *		the integrity of the keystore cannot be found
     * @exception CertificateException if any of the certificates in the
     *		keystore could not be loaded
     *
     * @since 1.5
     */
    public void engineLoad(KeyStore.LoadStoreParameter param)
		throws IOException, NoSuchAlgorithmException,
		CertificateException {

	if (param == null) {
	    engineLoad((InputStream)null, (char[])null);
	    return;
	}
	
	if (param instanceof KeyStore.SimpleLoadStoreParameter) {
	    ProtectionParameter protection = param.getProtectionParameter();
	    char[] password;
	    if (protection instanceof PasswordProtection) {
		password = ((PasswordProtection)protection).getPassword();
	    } else if (protection instanceof CallbackHandlerProtection) {
		CallbackHandler handler = 
		    ((CallbackHandlerProtection)protection).getCallbackHandler();
		PasswordCallback callback = 
		    new PasswordCallback("Password: ", false);
		try {
		    handler.handle(new Callback[] {callback});
		} catch (UnsupportedCallbackException e) {
		    throw new NoSuchAlgorithmException
			("Could not obtain password", e);
		}
		password = callback.getPassword();
		callback.clearPassword();
		if (password == null) {
		    throw new NoSuchAlgorithmException
			("No password provided");
		}
	    } else {
		throw new NoSuchAlgorithmException("ProtectionParameter must"
		    + " be PasswordProtection or CallbackHandlerProtection");
	    }
	    engineLoad(null, password);
	    return;
	}

	throw new UnsupportedOperationException();
    }

    /**
     * Gets a <code>KeyStore.Entry</code> for the specified alias
     * with the specified protection parameter.
     *
     * @param alias get the <code>KeyStore.Entry</code> for this alias
     * @param protParam the <code>ProtectionParameter</code>
     *		used to protect the <code>Entry</code>,
     *		which may be <code>null</code>
     *
     * @return the <code>KeyStore.Entry</code> for the specified alias,
     *		or <code>null</code> if there is no such entry
     *
     * @exception KeyStoreException if the operation failed
     * @exception NoSuchAlgorithmException if the algorithm for recovering the
     *		entry cannot be found
     * @exception UnrecoverableEntryException if the specified
     *		<code>protParam</code> were insufficient or invalid
     * @exception UnrecoverableKeyException if the entry is a 
     *          <code>PrivateKeyEntry</code> or <code>SecretKeyEntry</code>
     *          and the specified <code>protParam</code> does not contain
     *          the information needed to recover the key (e.g. wrong password)
     *
     * @since 1.5
     */
    public KeyStore.Entry engineGetEntry(String alias,
			KeyStore.ProtectionParameter protParam)
		throws KeyStoreException, NoSuchAlgorithmException,
		UnrecoverableEntryException {

	if (!engineContainsAlias(alias)) {
	    return null;
	}

	if (protParam == null) {
	    if (engineIsCertificateEntry(alias)) {
		return new KeyStore.TrustedCertificateEntry
				(engineGetCertificate(alias));
	    } else {
		throw new UnrecoverableKeyException
			("requested entry requires a password");
	    }
	}

	if (protParam instanceof KeyStore.PasswordProtection) {
	    if (engineIsCertificateEntry(alias)) {
		throw new UnsupportedOperationException
		    ("trusted certificate entries are not password-protected");
	    } else if (engineIsKeyEntry(alias)) {
		KeyStore.PasswordProtection pp =
			(KeyStore.PasswordProtection)protParam;
		char[] password = pp.getPassword();
		
		Key key = engineGetKey(alias, password);
		if (key instanceof PrivateKey) {
		    Certificate[] chain = engineGetCertificateChain(alias);
		    return new KeyStore.PrivateKeyEntry((PrivateKey)key, chain);
		} else if (key instanceof SecretKey) {
		    return new KeyStore.SecretKeyEntry((SecretKey)key);
		}
	    }
	}

	throw new UnsupportedOperationException();
    }

    /**
     * Saves a <code>KeyStore.Entry</code> under the specified alias.
     * The specified protection parameter is used to protect the
     * <code>Entry</code>.
     *
     * <p> If an entry already exists for the specified alias,
     * it is overridden.
     *
     * @param alias save the <code>KeyStore.Entry</code> under this alias
     * @param entry the <code>Entry</code> to save
     * @param protParam the <code>ProtectionParameter</code>
     *		used to protect the <code>Entry</code>,
     *		which may be <code>null</code>
     *
     * @exception KeyStoreException if this operation fails
     *
     * @since 1.5
     */
    public void engineSetEntry(String alias, KeyStore.Entry entry,
			KeyStore.ProtectionParameter protParam)
		throws KeyStoreException {

	// get password
	if (protParam != null &&
	    !(protParam instanceof KeyStore.PasswordProtection)) {
	    throw new KeyStoreException("unsupported protection parameter");
	}
	KeyStore.PasswordProtection pProtect = null;
	if (protParam != null) {
	    pProtect = (KeyStore.PasswordProtection)protParam;
	}

	// set entry
	if (entry instanceof KeyStore.TrustedCertificateEntry) {
	    if (protParam != null && pProtect.getPassword() != null) {
		// pre-1.5 style setCertificateEntry did not allow password
		throw new KeyStoreException
		    ("trusted certificate entries are not password-protected");
	    } else {
		KeyStore.TrustedCertificateEntry tce =
			(KeyStore.TrustedCertificateEntry)entry;
		engineSetCertificateEntry(alias, tce.getTrustedCertificate());
		return;
	    }
	} else if (entry instanceof KeyStore.PrivateKeyEntry) {
	    if (pProtect == null || pProtect.getPassword() == null) {
		// pre-1.5 style setKeyEntry required password
		throw new KeyStoreException
		    ("non-null password required to create PrivateKeyEntry");
	    } else {
		engineSetKeyEntry
		    (alias,
		    ((KeyStore.PrivateKeyEntry)entry).getPrivateKey(),
		    pProtect.getPassword(),
		    ((KeyStore.PrivateKeyEntry)entry).getCertificateChain());
		return;
	    }
	} else if (entry instanceof KeyStore.SecretKeyEntry) {
	    if (pProtect == null || pProtect.getPassword() == null) {
		// pre-1.5 style setKeyEntry required password
		throw new KeyStoreException
		    ("non-null password required to create SecretKeyEntry");
	    } else {
		engineSetKeyEntry
		    (alias,
		    ((KeyStore.SecretKeyEntry)entry).getSecretKey(),
		    pProtect.getPassword(),
		    (Certificate[])null);
		return;
	    }
	}

	throw new KeyStoreException
		("unsupported entry type: " + entry.getClass().getName());
    }

    /**
     * Determines if the keystore <code>Entry</code> for the specified
     * <code>alias</code> is an instance or subclass of the specified
     * <code>entryClass</code>.
     *
     * @param alias the alias name
     * @param entryClass the entry class
     *
     * @return true if the keystore <code>Entry</code> for the specified
     *		<code>alias</code> is an instance or subclass of the
     *		specified <code>entryClass</code>, false otherwise
     *
     * @since 1.5
     */
    public boolean
	engineEntryInstanceOf(String alias,
			      Class<? extends KeyStore.Entry> entryClass)
    {
	if (entryClass == KeyStore.TrustedCertificateEntry.class) {
	    return engineIsCertificateEntry(alias);
	}
	if (entryClass == KeyStore.PrivateKeyEntry.class) {
	    return engineIsKeyEntry(alias) &&
			engineGetCertificate(alias) != null;
	} 
	if (entryClass == KeyStore.SecretKeyEntry.class) {
	    return engineIsKeyEntry(alias) &&
			engineGetCertificate(alias) == null;
	}
	return false;
    }
}

Generated By: JavaOnTracks Doclet 0.1.4     ©Thibaut Colar