API Overview API Index Package Overview Direct link to this page
JDK 1.6
  java.security. KeyStore 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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741

/*
 * @(#)KeyStore.java	1.53 06/07/28
 *
 * 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.security.cert.Certificate;
import java.security.cert.X509Certificate;
import java.security.cert.CertificateException;
import java.util.*;
import javax.crypto.SecretKey;

import javax.security.auth.callback.*;

/**
 * This class represents a storage facility for cryptographic
 * keys and certificates.
 *
 * <p> A <code>KeyStore</code> manages different types of entries.
 * Each type of entry implements the <code>KeyStore.Entry</code> interface.
 * Three basic <code>KeyStore.Entry</code> implementations are provided:
 *
 * <ul>
 * <li><b>KeyStore.PrivateKeyEntry</b>
 * <p> This type of entry holds a cryptographic <code>PrivateKey</code>,
 * which is optionally stored in a protected format to prevent
 * unauthorized access.  It is also accompanied by a certificate chain
 * for the corresponding public key.
 *
 * <p> Private keys and certificate chains are used by a given entity for
 * self-authentication. Applications for this authentication include software
 * distribution organizations which sign JAR files as part of releasing
 * and/or licensing software.
 *
 * <li><b>KeyStore.SecretKeyEntry</b>
 * <p> This type of entry holds a cryptographic <code>SecretKey</code>,
 * which is optionally stored in a protected format to prevent
 * unauthorized access.
 *
 * <li><b>KeyStore.TrustedCertificateEntry</b>
 * <p> This type of entry contains a single public key <code>Certificate</code>
 * belonging to another party. It is called a <i>trusted certificate</i>
 * because the keystore owner trusts that the public key in the certificate
 * indeed belongs to the identity identified by the <i>subject</i> (owner)
 * of the certificate.
 *
 * <p>This type of entry can be used to authenticate other parties.
 * </ul>
 *
 * <p> Each entry in a keystore is identified by an "alias" string. In the
 * case of private keys and their associated certificate chains, these strings
 * distinguish among the different ways in which the entity may authenticate
 * itself. For example, the entity may authenticate itself using different
 * certificate authorities, or using different public key algorithms.
 *
 * <p> Whether aliases are case sensitive is implementation dependent. In order
 * to avoid problems, it is recommended not to use aliases in a KeyStore that
 * only differ in case.
 *
 * <p> Whether keystores are persistent, and the mechanisms used by the
 * keystore if it is persistent, are not specified here. This allows
 * use of a variety of techniques for protecting sensitive (e.g., private or
 * secret) keys. Smart cards or other integrated cryptographic engines
 * (SafeKeyper) are one option, and simpler mechanisms such as files may also
 * be used (in a variety of formats).
 *
 * <p> Typical ways to request a KeyStore object include
 * relying on the default type and providing a specific keystore type.
 *
 * <ul>
 * <li>To rely on the default type:
 * <pre>
 *    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
 * </pre>
 * The system will return a keystore implementation for the default type.
 * <p>
 *
 * <li>To provide a specific keystore type:
 * <pre>
 *      KeyStore ks = KeyStore.getInstance("JKS");
 * </pre>
 * The system will return the most preferred implementation of the
 * specified keystore type available in the environment. <p>
 * </ul>
 *
 * <p> Before a keystore can be accessed, it must be
 * {@link #load(java.io.InputStream, char[]) loaded}.
 * <pre>
 *    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
 *
 *    // get user password and file input stream
 *    char[] password = getPassword();
 *
 *    java.io.FileInputStream fis = null;
 *    try {
 *        fis = new java.io.FileInputStream("keyStoreName");
 *        ks.load(fis, password);
 *    } finally {
 *        if (fis != null) {
 *            fis.close();
 *        }
 *    }
 * </pre>
 *
 * To create an empty keystore using the above <code>load</code> method,
 * pass <code>null</code> as the <code>InputStream</code> argument.
 *
 * <p> Once the keystore has been loaded, it is possible
 * to read existing entries from the keystore, or to write new entries
 * into the keystore:
 * <pre>
 *    // get my private key
 *    KeyStore.PrivateKeyEntry pkEntry = (KeyStore.PrivateKeyEntry)
 *        ks.getEntry("privateKeyAlias", password);
 *    PrivateKey myPrivateKey = pkEntry.getPrivateKey();
 *
 *    // save my secret key
 *    javax.crypto.SecretKey mySecretKey;
 *    KeyStore.SecretKeyEntry skEntry =
 *        new KeyStore.SecretKeyEntry(mySecretKey);
 *    ks.setEntry("secretKeyAlias", skEntry, 
 *        new KeyStore.PasswordProtection(password));
 *
 *    // store away the keystore
 *    java.io.FileOutputStream fos = null;
 *    try {
 *        fos = new java.io.FileOutputStream("newKeyStoreName");
 *        ks.store(fos, password);
 *    } finally {
 *        if (fos != null) {
 *            fos.close();
 *        }
 *    }
 * </pre>
 *
 * Note that although the same password may be used to
 * load the keystore, to protect the private key entry,
 * to protect the secret key entry, and to store the keystore
 * (as is shown in the sample code above),
 * different passwords or other protection parameters
 * may also be used.
 *
 * @author Jan Luehe
 *
 * @version 1.53, 07/28/06
 *
 * @see java.security.PrivateKey
 * @see javax.crypto.SecretKey
 * @see java.security.cert.Certificate
 *
 * @since 1.2
 */

public class KeyStore {

    /*
     * Constant to lookup in the Security properties file to determine
     * the default keystore type.
     * In the Security properties file, the default keystore type is given as:
     * <pre>
     * keystore.type=jks
     * </pre>
     */  
    private static final String KEYSTORE_TYPE = "keystore.type";

    // The keystore type
    private String type;

    // The provider
    private Provider provider;

    // The provider implementation
    private KeyStoreSpi keyStoreSpi;

    // Has this keystore been initialized (loaded)?
    private boolean initialized = false;

    /**
     * A marker interface for <code>KeyStore</code>
     * {@link #load(KeyStore.LoadStoreParameter) load}
     * and
     * {@link #store(KeyStore.LoadStoreParameter) store}
     * parameters.
     *
     * @since 1.5
     */
    public static interface LoadStoreParameter {
	/**
	 * Gets the parameter used to protect keystore data.
	 *
	 * @return the parameter used to protect keystore data, or null
	 */
	public ProtectionParameter getProtectionParameter();
    }

    /**
     * A marker interface for keystore protection parameters.
     *
     * <p> The information stored in a <code>ProtectionParameter</code>
     * object protects the contents of a keystore.
     * For example, protection parameters may be used to check
     * the integrity of keystore data, or to protect the
     * confidentiality of sensitive keystore data
     * (such as a <code>PrivateKey</code>).
     *
     * @since 1.5
     */
    public static interface ProtectionParameter { }

    /**
     * A password-based implementation of <code>ProtectionParameter</code>.
     *
     * @since 1.5
     */
    public static class PasswordProtection implements
		ProtectionParameter, javax.security.auth.Destroyable {

	private final char[] password;
	private volatile boolean destroyed = false;

	/**
	 * Creates a password parameter.
	 *
	 * <p> The specified <code>password</code> is cloned before it is stored
	 * in the new <code>PasswordProtection</code> object.
	 *
	 * @param password the password, which may be <code>null</code>
	 */
	public PasswordProtection(char[] password) {
	    this.password = (password == null) ?
				null : (char[])password.clone();
	}

	/**
	 * Gets the password.
	 *
	 * <p>Note that this method returns a reference to the password.
	 * If a clone of the array is created it is the caller's
	 * responsibility to zero out the password information
	 * after it is no longer needed.
	 *
	 * @see #destroy()
	 * @return the password, which may be <code>null</code>
	 * @exception IllegalStateException if the password has
	 *		been cleared (destroyed)
	 */
	public synchronized char[] getPassword() {
	    if (destroyed) {
		throw new IllegalStateException("password has been cleared");
	    }
	    return password;
	}

	/**
	 * Clears the password.
	 *
	 * @exception DestroyFailedException if this method was unable
	 *	to clear the password
	 */
	public synchronized void destroy()
		throws javax.security.auth.DestroyFailedException {
	    destroyed = true;
	    if (password != null) {
		Arrays.fill(password, ' ');
	    }
	}

	/**
	 * Determines if password has been cleared.
	 *
	 * @return true if the password has been cleared, false otherwise
	 */
	public synchronized boolean isDestroyed() {
	    return destroyed;
	}
    }

    /**
     * A ProtectionParameter encapsulating a CallbackHandler.
     *
     * @since 1.5
     */
    public static class CallbackHandlerProtection 
	    implements ProtectionParameter {
		
	private final CallbackHandler handler;
	
	/**
	 * Constructs a new CallbackHandlerProtection from a
	 * CallbackHandler.
	 *
	 * @param handler the CallbackHandler
	 * @exception NullPointerException if handler is null
	 */
	public CallbackHandlerProtection(CallbackHandler handler) {
	    if (handler == null) {
		throw new NullPointerException("handler must not be null");
	    }
	    this.handler = handler;
	}
	
	/**
	 * Returns the CallbackHandler.
	 *
	 * @return the CallbackHandler.
	 */
	public CallbackHandler getCallbackHandler() {
	    return handler;
	}
	
    }

    /**
     * A marker interface for <code>KeyStore</code> entry types.
     *
     * @since 1.5
     */
    public static interface Entry { }

    /**
     * A <code>KeyStore</code> entry that holds a <code>PrivateKey</code>
     * and corresponding certificate chain.
     *
     * @since 1.5
     */
    public static final class PrivateKeyEntry implements Entry {

	private final PrivateKey privKey;
	private final Certificate[] chain;

	/**
	 * Constructs a <code>PrivateKeyEntry</code> with a
	 * <code>PrivateKey</code> and corresponding certificate chain.
	 *
	 * <p> The specified <code>chain</code> is cloned before it is stored
	 * in the new <code>PrivateKeyEntry</code> object.
	 *
	 * @param privateKey the <code>PrivateKey</code>
	 * @param chain an array of <code>Certificate</code>s
	 *	representing the certificate chain.
	 *	The chain must be ordered and contain a
	 *	<code>Certificate</code> at index 0
	 *	corresponding to the private key.
	 *
	 * @exception NullPointerException if
	 *	<code>privateKey</code> or <code>chain</code>
	 *	is <code>null</code>
	 * @exception IllegalArgumentException if the specified chain has a
	 *	length of 0, if the specified chain does not contain
	 *	<code>Certificate</code>s of the same type,
	 *	or if the <code>PrivateKey</code> algorithm
	 *	does not match the algorithm of the <code>PublicKey</code>
	 *	in the end entity <code>Certificate</code> (at index 0)
	 */
	public PrivateKeyEntry(PrivateKey privateKey, Certificate[] chain) {
	    if (privateKey == null || chain == null) {
		throw new NullPointerException("invalid null input");
	    }
	    if (chain.length == 0) {
		throw new IllegalArgumentException
				("invalid zero-length input chain");
	    }

	    Certificate[] clonedChain = (Certificate[])chain.clone();
	    String certType = clonedChain[0].getType();
	    for (int i = 1; i < clonedChain.length; i++) {
		if (!certType.equals(clonedChain[i].getType())) {
		    throw new IllegalArgumentException
				("chain does not contain certificates " +
				"of the same type");
		}
	    }
	    if (!privateKey.getAlgorithm().equals
			(clonedChain[0].getPublicKey().getAlgorithm())) {
		throw new IllegalArgumentException
				("private key algorithm does not match " +
				"algorithm of public key in end entity " +
				"certificate (at index 0)");
	    }
	    this.privKey = privateKey;

	    if (clonedChain[0] instanceof X509Certificate &&
		!(clonedChain instanceof X509Certificate[])) {

		this.chain = new X509Certificate[clonedChain.length];
		System.arraycopy(clonedChain, 0,
				this.chain, 0, clonedChain.length);
	    } else {
		this.chain = clonedChain;
	    }
	}

	/**
	 * Gets the <code>PrivateKey</code> from this entry.
	 *
	 * @return the <code>PrivateKey</code> from this entry
	 */
	public PrivateKey getPrivateKey() {
	    return privKey;
	}

	/**
	 * Gets the <code>Certificate</code> chain from this entry.
	 *
	 * <p> The stored chain is cloned before being returned.
	 *
	 * @return an array of <code>Certificate</code>s corresponding
	 *	to the certificate chain for the public key.
	 *	If the certificates are of type X.509,
	 *	the runtime type of the returned array is
	 *	<code>X509Certificate[]</code>.
	 */
	public Certificate[] getCertificateChain() {
	    return (Certificate[])chain.clone();
	}

	/**
	 * Gets the end entity <code>Certificate</code>
	 * from the certificate chain in this entry.
	 *
	 * @return the end entity <code>Certificate</code> (at index 0)
	 *	from the certificate chain in this entry.
	 *	If the certificate is of type X.509,
	 *	the runtime type of the returned certificate is
	 *	<code>X509Certificate</code>.
	 */
	public Certificate getCertificate() {
	    return chain[0];
	}

	/**
	 * Returns a string representation of this PrivateKeyEntry.
	 * @return a string representation of this PrivateKeyEntry.
	 */	
	public String toString() {
	    StringBuilder sb = new StringBuilder();
	    sb.append("Private key entry and certificate chain with "
		+ chain.length + " elements:\r\n");
	    for (Certificate cert : chain) {
		sb.append(cert);
		sb.append("\r\n");
	    }
	    return sb.toString();
	}

    }

    /**
     * A <code>KeyStore</code> entry that holds a <code>SecretKey</code>.
     *
     * @since 1.5
     */
    public static final class SecretKeyEntry implements Entry {

	private final SecretKey sKey;

	/**
	 * Constructs a <code>SecretKeyEntry</code> with a
	 * <code>SecretKey</code>.
	 *
	 * @param secretKey the <code>SecretKey</code>
	 *
	 * @exception NullPointerException if <code>secretKey</code>
	 *	is <code>null</code>
	 */
	public SecretKeyEntry(SecretKey secretKey) {
	    if (secretKey == null) {
		throw new NullPointerException("invalid null input");
	    }
	    this.sKey = secretKey;
	}

	/**
	 * Gets the <code>SecretKey</code> from this entry.
	 *
	 * @return the <code>SecretKey</code> from this entry
	 */
	public SecretKey getSecretKey() {
	    return sKey;
	}
	
	/**
	 * Returns a string representation of this SecretKeyEntry.
	 * @return a string representation of this SecretKeyEntry.
	 */	
	public String toString() {
	    return "Secret key entry with algorithm " + sKey.getAlgorithm();
	}
    }

    /**
     * A <code>KeyStore</code> entry that holds a trusted
     * <code>Certificate</code>.
     *
     * @since 1.5
     */
    public static final class TrustedCertificateEntry implements Entry {

	private final Certificate cert;

	/**
	 * Constructs a <code>TrustedCertificateEntry</code> with a
	 * trusted <code>Certificate</code>.
	 *
	 * @param trustedCert the trusted <code>Certificate</code>
	 *
	 * @exception NullPointerException if
	 *	<code>trustedCert</code> is <code>null</code>
	 */
	public TrustedCertificateEntry(Certificate trustedCert) {
	    if (trustedCert == null) {
		throw new NullPointerException("invalid null input");
	    }
	    this.cert = trustedCert;
	}

	/**
	 * Gets the trusted <code>Certficate</code> from this entry.
	 *
	 * @return the trusted <code>Certificate</code> from this entry
	 */
	public Certificate getTrustedCertificate() {
	    return cert;
	}
	
	/**
	 * Returns a string representation of this TrustedCertificateEntry.
	 * @return a string representation of this TrustedCertificateEntry.
	 */	
	public String toString() {
	    return "Trusted certificate entry:\r\n" + cert.toString();
	}
    }

    /**
     * Creates a KeyStore object of the given type, and encapsulates the given
     * provider implementation (SPI object) in it.
     *
     * @param keyStoreSpi the provider implementation.
     * @param provider the provider.
     * @param type the keystore type.
     */
    protected KeyStore(KeyStoreSpi keyStoreSpi, Provider provider, String type)
    {
	this.keyStoreSpi = keyStoreSpi;
	this.provider = provider;
	this.type = type;
    }

    /**
     * Returns a keystore object of the specified type.
     * 
     * <p> This method traverses the list of registered security Providers,
     * starting with the most preferred Provider.
     * A new KeyStore object encapsulating the
     * KeyStoreSpi implementation from the first
     * Provider that supports the specified type is returned.
     *
     * <p> Note that the list of registered providers may be retrieved via
     * the {@link Security#getProviders() Security.getProviders()} method.
     *
     * @param type the type of keystore. 
     * See Appendix A in the <a href=
     * "../../../technotes/guides/security/crypto/CryptoSpec.html#AppA">
     * Java Cryptography Architecture API Specification &amp; Reference </a> 
     * for information about standard keystore types.
     *
     * @return a keystore object of the specified type.
     *
     * @exception KeyStoreException if no Provider supports a
     *          KeyStoreSpi implementation for the
     *          specified type.
     *
     * @see Provider
     */
    public static KeyStore getInstance(String type) 
	throws KeyStoreException
    {
	try {
	    Object[] objs = Security.getImpl(type, "KeyStore", (String)null);
	    return new KeyStore((KeyStoreSpi)objs[0], (Provider)objs[1], type);
	} catch (NoSuchAlgorithmException nsae) {
	    throw new KeyStoreException(type + " not found", nsae);
	} catch (NoSuchProviderException nspe) {
	    throw new KeyStoreException(type + " not found", nspe);
	}
    }

    /**
     * Returns a keystore object of the specified type.
     * 
     * <p> A new KeyStore object encapsulating the
     * KeyStoreSpi implementation from the specified provider
     * is returned.  The specified provider must be registered
     * in the security provider list.
     *
     * <p> Note that the list of registered providers may be retrieved via
     * the {@link Security#getProviders() Security.getProviders()} method.
     *
     * @param type the type of keystore.
     * See Appendix A in the <a href=
     * "../../../technotes/guides/security/crypto/CryptoSpec.html#AppA">
     * Java Cryptography Architecture API Specification &amp; Reference </a> 
     * for information about standard keystore types.
     *
     * @param provider the name of the provider.
     *
     * @return a keystore object of the specified type.
     *
     * @exception KeyStoreException if a KeyStoreSpi
     *          implementation for the specified type is not
     *          available from the specified provider.
     * 
     * @exception NoSuchProviderException if the specified provider is not
     *          registered in the security provider list.
     *
     * @exception IllegalArgumentException if the provider name is null
     *		or empty.
     *
     * @see Provider
     */
    public static KeyStore getInstance(String type, String provider)
	throws KeyStoreException, NoSuchProviderException
    {
	if (provider == null || provider.length() == 0)
	    throw new IllegalArgumentException("missing provider");
	try {
	    Object[] objs = Security.getImpl(type, "KeyStore", provider);
	    return new KeyStore((KeyStoreSpi)objs[0], (Provider)objs[1], type);
	} catch (NoSuchAlgorithmException nsae) {
	    throw new KeyStoreException(type + " not found", nsae);
	}
    }

    /**
     * Returns a keystore object of the specified type.
     * 
     * <p> A new KeyStore object encapsulating the
     * KeyStoreSpi implementation from the specified Provider
     * object is returned.  Note that the specified Provider object
     * does not have to be registered in the provider list.
     *
     * @param type the type of keystore.
     * See Appendix A in the <a href=
     * "../../../technotes/guides/security/crypto/CryptoSpec.html#AppA">
     * Java Cryptography Architecture API Specification &amp; Reference </a> 
     * for information about standard keystore types.
     *
     * @param provider the provider.
     *
     * @return a keystore object of the specified type.
     *
     * @exception KeyStoreException if KeyStoreSpi
     *          implementation for the specified type is not available
     *          from the specified Provider object.
     *
     * @exception IllegalArgumentException if the specified provider is null.
     *
     * @see Provider
     *
     * @since 1.4
     */
    public static KeyStore getInstance(String type, Provider provider)
	throws KeyStoreException
    {
	if (provider == null)
	    throw new IllegalArgumentException("missing provider");
	try {
	    Object[] objs = Security.getImpl(type, "KeyStore", provider);
	    return new KeyStore((KeyStoreSpi)objs[0], (Provider)objs[1], type);
	} catch (NoSuchAlgorithmException nsae) {
	    throw new KeyStoreException(type + " not found", nsae);
	}
    }

    /**
     * Returns the default keystore type as specified in the Java security
     * properties file, or the string
     * &quot;jks&quot; (acronym for &quot;Java keystore&quot;)
     * if no such property exists.
     * The Java security properties file is located in the file named
     * &lt;JAVA_HOME&gt;/lib/security/java.security.
     * &lt;JAVA_HOME&gt; refers to the value of the java.home system property,
     * and specifies the directory where the JRE is installed.
     *
     * <p>The default keystore type can be used by applications that do not
     * want to use a hard-coded keystore type when calling one of the
     * <code>getInstance</code> methods, and want to provide a default keystore
     * type in case a user does not specify its own.
     *
     * <p>The default keystore type can be changed by setting the value of the
     * "keystore.type" security property (in the Java security properties
     * file) to the desired keystore type.
     *
     * @return the default keystore type as specified in the 
     * Java security properties file, or the string &quot;jks&quot;
     * if no such property exists.
     */
    public final static String getDefaultType() {
	String kstype;
	kstype = (String)AccessController.doPrivileged(new PrivilegedAction() {
	    public Object run() {
		return Security.getProperty(KEYSTORE_TYPE);
	    }
	});
	if (kstype == null) {
	    kstype = "jks";
	}
	return kstype;
    }

    /** 
     * Returns the provider of this keystore.
     * 
     * @return the provider of this keystore.
     */
    public final Provider getProvider()
    {
	return this.provider;
    }

    /**
     * Returns the type of this keystore.
     *
     * @return the type of this keystore.
     */
    public final String getType()
    {
	return this.type;
    }

    /**
     * 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 KeyStoreException if the keystore has not been initialized
     * (loaded).
     * @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 final Key getKey(String alias, char[] password)
	throws KeyStoreException, NoSuchAlgorithmException,
	    UnrecoverableKeyException
    {
	if (!initialized) {
	    throw new KeyStoreException("Uninitialized keystore");
	}
	return keyStoreSpi.engineGetKey(alias, password);
    }

    /**
     * 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
     *
     * @exception KeyStoreException if the keystore has not been initialized
     * (loaded).
     */
    public final Certificate[] getCertificateChain(String alias)
	throws KeyStoreException
    {
	if (!initialized) {
	    throw new KeyStoreException("Uninitialized keystore");
	}
	return keyStoreSpi.engineGetCertificateChain(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
     * 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.
     *
     * @exception KeyStoreException if the keystore has not been initialized
     * (loaded).
     */
    public final Certificate getCertificate(String alias)
	throws KeyStoreException
    {
	if (!initialized) {
	    throw new KeyStoreException("Uninitialized keystore");
	}
	return keyStoreSpi.engineGetCertificate(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
     *
     * @exception KeyStoreException if the keystore has not been initialized
     * (loaded).
     */
    public final Date getCreationDate(String alias)
	throws KeyStoreException
    {
	if (!initialized) {
	    throw new KeyStoreException("Uninitialized keystore");
	}
	return keyStoreSpi.engineGetCreationDate(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 keystore has not been initialized
     * (loaded), the given key cannot be protected, or this operation fails
     * for some other reason
     */
    public final void setKeyEntry(String alias, Key key, char[] password,
				  Certificate[] chain)
	throws KeyStoreException
    {
	if (!initialized) {
	    throw new KeyStoreException("Uninitialized keystore");
	}
	if ((key instanceof PrivateKey) && 
	    (chain == null || chain.length == 0)) {
	    throw new IllegalArgumentException("Private key must be "
					       + "accompanied by certificate "
					       + "chain");
	}
	keyStoreSpi.engineSetKeyEntry(alias, key, password, chain);
    }

    /**
     * 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. If the
     * underlying keystore implementation is of type <code>jks</code>,
     * <code>key</code> must be encoded as an
     * <code>EncryptedPrivateKeyInfo</code> as defined in the PKCS #8 standard.
     *
     * <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 the keystore has not been initialized
     * (loaded), or if this operation fails for some other reason.
     */
    public final void setKeyEntry(String alias, byte[] key,
				  Certificate[] chain)
	throws KeyStoreException
    {
	if (!initialized) {
	    throw new KeyStoreException("Uninitialized keystore");
	}
	keyStoreSpi.engineSetKeyEntry(alias, key, chain);
    }

    /**
     * Assigns the given trusted 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 keystore has not been initialized,
     * or the given alias already exists and does not identify an
     * entry containing a trusted certificate,
     * or this operation fails for some other reason.
     */
    public final void setCertificateEntry(String alias, Certificate cert)
	throws KeyStoreException
    {
	if (!initialized) {
	    throw new KeyStoreException("Uninitialized keystore");
	}
	keyStoreSpi.engineSetCertificateEntry(alias, cert);
    }

    /**
     * Deletes the entry identified by the given alias from this keystore.
     *
     * @param alias the alias name
     *
     * @exception KeyStoreException if the keystore has not been initialized,
     * or if the entry cannot be removed.
     */
    public final void deleteEntry(String alias)
	throws KeyStoreException
    {
	if (!initialized) {
	    throw new KeyStoreException("Uninitialized keystore");
	}
	keyStoreSpi.engineDeleteEntry(alias);
    }

    /**
     * Lists all the alias names of this keystore.
     *
     * @return enumeration of the alias names
     *
     * @exception KeyStoreException if the keystore has not been initialized
     * (loaded).
     */
    public final Enumeration<String> aliases()
	throws KeyStoreException
    {
	if (!initialized) {
	    throw new KeyStoreException("Uninitialized keystore");
	}
	return keyStoreSpi.engineAliases();
    }

    /**
     * Checks if the given alias exists in this keystore.
     *
     * @param alias the alias name
     *
     * @return true if the alias exists, false otherwise
     *
     * @exception KeyStoreException if the keystore has not been initialized
     * (loaded).
     */
    public final boolean containsAlias(String alias)
	throws KeyStoreException
    {
	if (!initialized) {
	    throw new KeyStoreException("Uninitialized keystore");
	}
	return keyStoreSpi.engineContainsAlias(alias);
    }

    /**
     * Retrieves the number of entries in this keystore.
     *
     * @return the number of entries in this keystore
     *
     * @exception KeyStoreException if the keystore has not been initialized
     * (loaded).
     */
    public final int size()
	throws KeyStoreException
    {
	if (!initialized) {
	    throw new KeyStoreException("Uninitialized keystore");
	}
	return keyStoreSpi.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 entry, false otherwise.
     *
     * @exception KeyStoreException if the keystore has not been initialized
     * (loaded).
     */
    public final boolean isKeyEntry(String alias)
	throws KeyStoreException
    {
	if (!initialized) {
	    throw new KeyStoreException("Uninitialized keystore");
	}
	return keyStoreSpi.engineIsKeyEntry(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.
     *
     * @exception KeyStoreException if the keystore has not been initialized
     * (loaded).
     */
    public final boolean isCertificateEntry(String alias)
	throws KeyStoreException
    {
	if (!initialized) {
	    throw new KeyStoreException("Uninitialized keystore");
	}
	return keyStoreSpi.engineIsCertificateEntry(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 a matching certificate,
     * or null if no such entry exists in this keystore.
     *
     * @exception KeyStoreException if the keystore has not been initialized
     * (loaded).
     */
    public final String getCertificateAlias(Certificate cert)
	throws KeyStoreException
    {
	if (!initialized) {
	    throw new KeyStoreException("Uninitialized keystore");
	}
	return keyStoreSpi.engineGetCertificateAlias(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 KeyStoreException if the keystore has not been initialized
     * (loaded).
     * @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 final void store(OutputStream stream, char[] password)
	throws KeyStoreException, IOException, NoSuchAlgorithmException,
	    CertificateException
    {
	if (!initialized) {
	    throw new KeyStoreException("Uninitialized keystore");
	}
	keyStoreSpi.engineStore(stream, password);
    }

    /**
     * Stores this keystore using the given <code>LoadStoreParameter</code>.
     *
     * @param param the <code>LoadStoreParameter</code>
     *		that specifies how to store the keystore,
     *		which may be <code>null</code>
     *
     * @exception IllegalArgumentException if the given
     *		<code>LoadStoreParameter</code>
     *		input is not recognized
     * @exception KeyStoreException if the keystore has not been initialized
     *		(loaded)
     * @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 final void store(LoadStoreParameter param)
		throws KeyStoreException, IOException,
		NoSuchAlgorithmException, CertificateException {
	if (!initialized) {
	    throw new KeyStoreException("Uninitialized keystore");
	}
	keyStoreSpi.engineStore(param);
    }

    /**
     * Loads this 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.
     *
     * <p>In order to create an empty keystore, or if the keystore cannot
     * be initialized from a stream, pass <code>null</code>
     * as the <code>stream</code> argument.
     *
     * <p> Note that if this keystore has already been loaded, it is
     * reinitialized and loaded again from the given input stream.
     *
     * @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 final void load(InputStream stream, char[] password)
	throws IOException, NoSuchAlgorithmException, CertificateException
    {
	keyStoreSpi.engineLoad(stream, password);
	initialized = true;
    }

    /**
     * Loads this keystore using the given <code>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>LoadStoreParameter</code>
     *		that specifies how to load the keystore,
     *		which may be <code>null</code>
     *
     * @exception IllegalArgumentException if the given
     *		<code>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 final void load(LoadStoreParameter param)
		throws IOException, NoSuchAlgorithmException,
		CertificateException {

	keyStoreSpi.engineLoad(param);
	initialized = true;
    }

    /**
     * Gets a keystore <code>Entry</code> for the specified alias
     * with the specified protection parameter.
     *
     * @param alias get the keystore <code>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 keystore <code>Entry</code> for the specified alias,
     *		or <code>null</code> if there is no such entry
     *
     * @exception NullPointerException if
     *		<code>alias</code> is <code>null</code>
     * @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)
     * @exception KeyStoreException if the keystore has not been initialized
     *		(loaded).
     * @see #setEntry(String, KeyStore.Entry, KeyStore.ProtectionParameter)
     *
     * @since 1.5
     */
    public final Entry getEntry(String alias, ProtectionParameter protParam)
    		throws NoSuchAlgorithmException, UnrecoverableEntryException,
		KeyStoreException {

	if (alias == null) {
	    throw new NullPointerException("invalid null input");
	}
	if (!initialized) {
	    throw new KeyStoreException("Uninitialized keystore");
	}
	return keyStoreSpi.engineGetEntry(alias, protParam);
    }

    /**
     * Saves a keystore <code>Entry</code> under the specified alias.
     * The 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 keystore <code>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 NullPointerException if
     *		<code>alias</code> or <code>entry</code>
     *		is <code>null</code>
     * @exception KeyStoreException if the keystore has not been initialized
     *		(loaded), or if this operation fails for some other reason
     *
     * @see #getEntry(String, KeyStore.ProtectionParameter)
     *
     * @since 1.5
     */
    public final void setEntry(String alias, Entry entry,
			ProtectionParameter protParam)
		throws KeyStoreException {
	if (alias == null || entry == null) {
	    throw new NullPointerException("invalid null input");
	}
	if (!initialized) {
	    throw new KeyStoreException("Uninitialized keystore");
	}
	keyStoreSpi.engineSetEntry(alias, entry, protParam);
    }

    /**
     * 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
     *
     * @exception NullPointerException if
     *		<code>alias</code> or <code>entryClass</code>
     *		is <code>null</code>
     * @exception KeyStoreException if the keystore has not been
     *		initialized (loaded)
     *
     * @since 1.5
     */
    public final boolean
	entryInstanceOf(String alias,
			Class<? extends KeyStore.Entry> entryClass)
	throws KeyStoreException
    {

	if (alias == null || entryClass == null) {
	    throw new NullPointerException("invalid null input");
	}
	if (!initialized) {
	    throw new KeyStoreException("Uninitialized keystore");
	}
	return keyStoreSpi.engineEntryInstanceOf(alias, entryClass);
    }
    
    /**
     * A description of a to-be-instantiated KeyStore object.
     *
     * <p>An instance of this class encapsulates the information needed to
     * instantiate and initialize a KeyStore object. That process is
     * triggered when the {@linkplain #getKeyStore} method is called.
     *
     * <p>This makes it possible to decouple configuration from KeyStore
     * object creation and e.g. delay a password prompt until it is
     * needed.
     *
     * @see KeyStore
     * @see javax.net.ssl.KeyStoreBuilderParameters
     * @since 1.5
     */
    public static abstract class Builder {

	// maximum times to try the callbackhandler if the password is wrong
	static final int MAX_CALLBACK_TRIES = 3;
	
	/**
	 * Construct a new Builder.
	 */
	protected Builder() {
	    // empty
	}

	/**
	 * Returns the KeyStore described by this object.
	 *
	 * @exception KeyStoreException if an error occured during the
	 *   operation, for example if the KeyStore could not be
	 *   instantiated or loaded
	 */
	public abstract KeyStore getKeyStore() throws KeyStoreException;

	/**
	 * Returns the ProtectionParameters that should be used to obtain
	 * the {@link KeyStore.Entry Entry} with the given alias.
	 * The <code>getKeyStore</code> method must be invoked before this
	 * method may be called.
	 *
	 * @return the ProtectionParameters that should be used to obtain
	 *   the {@link KeyStore.Entry Entry} with the given alias.
	 * @param alias the alias of the KeyStore entry
	 * @throws NullPointerException if alias is null
	 * @throws KeyStoreException if an error occured during the
	 *   operation
	 * @throws IllegalStateException if the getKeyStore method has
	 *   not been invoked prior to calling this method
	 */
	public abstract ProtectionParameter getProtectionParameter(String alias)
	    throws KeyStoreException;

	/**
	 * Returns a new Builder that encapsulates the given KeyStore.
	 * The {@linkplain #getKeyStore} method of the returned object 
	 * will return <code>keyStore</code>, the {@linkplain 
	 * #getProtectionParameter getProtectionParameter()} method will 
	 * return <code>protectionParameters</code>.
         *
	 * <p> This is useful if an existing KeyStore object needs to be
	 * used with Builder-based APIs.
	 *
	 * @return a new Builder object
	 * @param keyStore the KeyStore to be encapsulated
	 * @param protectionParameter the ProtectionParameter used to
	 *   protect the KeyStore entries
	 * @throws NullPointerException if keyStore or
	 *   protectionParameters is null
	 * @throws IllegalArgumentException if the keyStore has not been
	 *   initialized
	 */
	public static Builder newInstance(final KeyStore keyStore,
		final ProtectionParameter protectionParameter) {
	    if ((keyStore == null) || (protectionParameter == null)) {
		throw new NullPointerException();
	    }
	    if (keyStore.initialized == false) {
		throw new IllegalArgumentException("KeyStore not initialized");
	    }
	    return new Builder() {
		private volatile boolean getCalled;
		
		public KeyStore getKeyStore() {
		    getCalled = true;
		    return keyStore;
		}
		
		public ProtectionParameter getProtectionParameter(String alias)
		{
		    if (alias == null) {
			throw new NullPointerException();
		    }
		    if (getCalled == false) {
			throw new IllegalStateException
			    ("getKeyStore() must be called first");
		    }
		    return protectionParameter;
		}
	    };
	}

	/**
	 * Returns a new Builder object.
	 *
	 * <p>The first call to the {@link #getKeyStore} method on the returned
	 * builder will create a KeyStore of type <code>type</code> and call
	 * its {@link KeyStore#load load()} method. 
	 * The <code>inputStream</code> argument is constructed from
	 * <code>file</code>. 
	 * If <code>protection</code> is a
	 * <code>PasswordProtection</code>, the password is obtained by
	 * calling the <code>getPassword</code> method.
	 * Otherwise, if <code>protection</code> is a 
	 * <code>CallbackHandlerProtection</code>, the password is obtained
	 * by invoking the CallbackHandler.
	 *
	 * <p>Subsequent calls to {@link #getKeyStore} return the same object 
	 * as the initial call. If the initial call to failed with a
	 * KeyStoreException, subsequent calls also throw a 
	 * KeyStoreException.
	 *
	 * <p>The KeyStore is instantiated from <code>provider</code> if
	 * non-null. Otherwise, all installed providers are searched.
	 *
	 * <p>Calls to {@link #getProtectionParameter getProtectionParameter()}
	 * will return a {@link KeyStore.PasswordProtection PasswordProtection}
	 * object encapsulating the password that was used to invoke the
	 * <code>load</code> method.
	 *
	 * <p><em>Note</em> that the {@link #getKeyStore} method is executed 
	 * within the {@link AccessControlContext} of the code invoking this 
	 * method.
	 *
	 * @return a new Builder object
	 * @param type the type of KeyStore to be constructed
	 * @param provider the provider from which the KeyStore is to
	 *   be instantiated (or null)
	 * @param file the File that contains the KeyStore data
	 * @param protection the ProtectionParameter securing the KeyStore data
	 * @throws NullPointerException if type, file or protection is null
	 * @throws IllegalArgumentException if protection is not an instance
	 *   of either PasswordProtection or CallbackHandlerProtection; or
	 *   if file does not exist or does not refer to a normal file
	 */
	public static Builder newInstance(String type, Provider provider, 
		File file, ProtectionParameter protection) {
	    if ((type == null) || (file == null) || (protection == null)) {
		throw new NullPointerException();
	    }
	    if ((protection instanceof PasswordProtection == false) &&
		(protection instanceof CallbackHandlerProtection == false)) {
		throw new IllegalArgumentException
		("Protection must be PasswordProtection or " +
		 "CallbackHandlerProtection");
	    }
	    if (file.isFile() == false) {
		throw new IllegalArgumentException
		    ("File does not exist or it does not refer " +
		     "to a normal file: " + file);
	    }
	    return new FileBuilder(type, provider, file, protection, 
		AccessController.getContext());
	}
	
	private static final class FileBuilder extends Builder {
	    
	    private final String type;
	    private final Provider provider;
	    private final File file;
	    private ProtectionParameter protection;
	    private ProtectionParameter keyProtection;
	    private final AccessControlContext context;
	    
	    private KeyStore keyStore;
	    
	    private Throwable oldException;
	    
	    FileBuilder(String type, Provider provider, File file, 
		    ProtectionParameter protection, 
		    AccessControlContext context) {
		this.type = type;
		this.provider = provider;
		this.file = file;
		this.protection = protection;
		this.context = context;
	    }
	    
	    public synchronized KeyStore getKeyStore() throws KeyStoreException
	    {
		if (keyStore != null) {
		    return keyStore;
		}
		if (oldException != null) {
		    throw new KeyStoreException
			("Previous KeyStore instantiation failed",
			 oldException);
		}
		PrivilegedExceptionAction action = 
			new PrivilegedExceptionAction() {
		    public Object run() throws Exception {
			if (protection instanceof CallbackHandlerProtection == false) {
			    return run0();
			}
			// when using a CallbackHandler,
			// reprompt if the password is wrong
			int tries = 0;
			while (true) {
			    tries++;
			    try {
				return run0();
			    } catch (IOException e) {
				if ((tries < MAX_CALLBACK_TRIES)
					&& (e.getCause() instanceof UnrecoverableKeyException)) {
				    continue;
				}
				throw e;
			    }
			}
		    }
		    public Object run0() throws Exception {
			KeyStore ks;
			if (provider == null) {
			    ks = KeyStore.getInstance(type);
			} else {
			    ks = KeyStore.getInstance(type, provider);
			}
			InputStream in = null;
			char[] password = null;
			try {
			    in = new FileInputStream(file);
			    if (protection instanceof PasswordProtection) {
				password = 
				((PasswordProtection)protection).getPassword();
				keyProtection = protection;
			    } else {
				CallbackHandler handler = 
				    ((CallbackHandlerProtection)protection)
				    .getCallbackHandler();
				PasswordCallback callback = new PasswordCallback
				    ("Password for keystore " + file.getName(), 
				    false);
				handler.handle(new Callback[] {callback});
				password = callback.getPassword();
				if (password == null) {
				    throw new KeyStoreException("No password" +
								" provided");
				}
				callback.clearPassword();
				keyProtection = new PasswordProtection(password);
			    }
			    ks.load(in, password);
			    return ks;
			} finally {
			    if (in != null) {
				in.close();
			    }
			}
		    }
		};
		try {
		    keyStore = (KeyStore)AccessController.doPrivileged
							(action, context);
		    return keyStore;
		} catch (PrivilegedActionException e) {
		    oldException = e.getCause();
		    throw new KeyStoreException
			("KeyStore instantiation failed", oldException);
		}
	    }
	    
	    public synchronized ProtectionParameter 
			getProtectionParameter(String alias) {
		if (alias == null) {
		    throw new NullPointerException();
		}
		if (keyStore == null) {
		    throw new IllegalStateException
			("getKeyStore() must be called first");
		}
		return keyProtection;
	    }
	}
	
	/**
	 * Returns a new Builder object.
	 *
	 * <p>Each call to the {@link #getKeyStore} method on the returned
	 * builder will return a new KeyStore object of type <code>type</code>.
	 * Its {@link KeyStore#load(KeyStore.LoadStoreParameter) load()} 
	 * method is invoked using a
	 * <code>LoadStoreParameter</code> that encapsulates 
	 * <code>protection</code>.
	 *
	 * <p>The KeyStore is instantiated from <code>provider</code> if
	 * non-null. Otherwise, all installed providers are searched.
	 *
	 * <p>Calls to {@link #getProtectionParameter getProtectionParameter()}
	 * will return <code>protection</code>.
	 *
	 * <p><em>Note</em> that the {@link #getKeyStore} method is executed 
	 * within the {@link AccessControlContext} of the code invoking this 
	 * method.
	 *
	 * @return a new Builder object
	 * @param type the type of KeyStore to be constructed
	 * @param provider the provider from which the KeyStore is to
	 *   be instantiated (or null)
	 * @param protection the ProtectionParameter securing the Keystore
	 * @throws NullPointerException if type or protection is null
	 */
	public static Builder newInstance(final String type, 
		final Provider provider, final ProtectionParameter protection) {
	    if ((type == null) || (protection == null)) {
		throw new NullPointerException();
	    }
	    final AccessControlContext context = AccessController.getContext();
	    return new Builder() {
		private volatile boolean getCalled;
		private IOException oldException;
		
		private final PrivilegedExceptionAction action
		= new PrivilegedExceptionAction() {
		    
		    public Object run() throws Exception {
			KeyStore ks;
			if (provider == null) {
			    ks = KeyStore.getInstance(type);
			} else {
			    ks = KeyStore.getInstance(type, provider);
			}
			LoadStoreParameter param = new SimpleLoadStoreParameter(protection);
			if (protection instanceof CallbackHandlerProtection == false) {
			    ks.load(param);
			} else {
			    // when using a CallbackHandler,
			    // reprompt if the password is wrong
			    int tries = 0;
			    while (true) {
				tries++;
				try {
				    ks.load(param);
				    break;
				} catch (IOException e) {
				    if (e.getCause() instanceof UnrecoverableKeyException) {
					if (tries < MAX_CALLBACK_TRIES) {
					    continue;
					} else {
					    oldException = e;
					}
				    }
				    throw e;
				}
			    }
			}
			getCalled = true;
			return ks;
		    }
		};
		
		public synchronized KeyStore getKeyStore()
			throws KeyStoreException {
		    if (oldException != null) {
			throw new KeyStoreException
			    ("Previous KeyStore instantiation failed",
			     oldException);
		    }
		    try {
			return (KeyStore)AccessController.doPrivileged(action);
		    } catch (PrivilegedActionException e) {
			Throwable cause = e.getCause();
			throw new KeyStoreException
			    ("KeyStore instantiation failed", cause);
		    }
		}
		
		public ProtectionParameter getProtectionParameter(String alias)
		{
		    if (alias == null) {
			throw new NullPointerException();
		    }
		    if (getCalled == false) {
			throw new IllegalStateException
			    ("getKeyStore() must be called first");
		    }
		    return protection;
		}
	    };
	}

    }
    
    static class SimpleLoadStoreParameter implements LoadStoreParameter {
	
	private final ProtectionParameter protection;
	
	SimpleLoadStoreParameter(ProtectionParameter protection) {
	    this.protection = protection;
	}
	
	public ProtectionParameter getProtectionParameter() {
	    return protection;
	}
    }
    
}

Generated By: JavaOnTracks Doclet 0.1.4     ©Thibaut Colar