author | prr |
Fri, 25 May 2018 12:12:24 -0700 | |
changeset 50347 | b2f046ae8eb6 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
15298 | 1 |
/* |
2 |
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. |
|
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 |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
22 |
*/ |
|
23 |
||
24 |
/* |
|
25 |
* @test |
|
26 |
* @bug 8005408 |
|
27 |
* @summary KeyStore API enhancements |
|
28 |
*/ |
|
29 |
||
30 |
import java.io.*; |
|
31 |
import java.security.*; |
|
32 |
import java.security.cert.*; |
|
33 |
import java.util.*; |
|
34 |
import java.security.cert.Certificate; |
|
35 |
import javax.crypto.*; |
|
36 |
import javax.crypto.spec.*; |
|
37 |
||
38 |
// Store a trusted certificate in a keystore and retrieve it again. |
|
39 |
||
40 |
public class StoreTrustedCertTest { |
|
41 |
private final static String DIR = System.getProperty("test.src", "."); |
|
42 |
private static final char[] PASSWORD = "passphrase".toCharArray(); |
|
43 |
private static final String KEYSTORE = "truststore.p12"; |
|
44 |
private static final String CERT = DIR + "/trusted.pem"; |
|
45 |
private static final String ALIAS = "my trustedcert"; |
|
46 |
private static final String ALIAS2 = "my trustedcert with attributes"; |
|
47 |
||
48 |
public static void main(String[] args) throws Exception { |
|
49 |
||
50 |
new File(KEYSTORE).delete(); |
|
51 |
||
15532
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
52 |
KeyStore keystore = KeyStore.getInstance("PKCS12"); |
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
53 |
keystore.load(null, null); |
15298 | 54 |
|
15532
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
55 |
Certificate cert = loadCertificate(CERT); |
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
56 |
Set<KeyStore.Entry.Attribute> attributes = new HashSet<>(); |
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
57 |
attributes.add(new PKCS12Attribute("1.3.5.7.9", "that's odd")); |
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
58 |
attributes.add(new PKCS12Attribute("2.4.6.8.10", "that's even")); |
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
59 |
|
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
60 |
// Set trusted certificate entry |
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
61 |
keystore.setEntry(ALIAS, |
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
62 |
new KeyStore.TrustedCertificateEntry(cert), null); |
15298 | 63 |
|
15532
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
64 |
// Set trusted certificate entry with attributes |
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
65 |
keystore.setEntry(ALIAS2, |
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
66 |
new KeyStore.TrustedCertificateEntry(cert, attributes), null); |
15298 | 67 |
|
15532
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
68 |
try (FileOutputStream outStream = new FileOutputStream(KEYSTORE)) { |
15298 | 69 |
System.out.println("Storing keystore to: " + KEYSTORE); |
15532
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
70 |
keystore.store(outStream, PASSWORD); |
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
71 |
} |
15298 | 72 |
|
15532
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
73 |
try (FileInputStream inStream = new FileInputStream(KEYSTORE)) { |
15298 | 74 |
System.out.println("Loading keystore from: " + KEYSTORE); |
15532
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
75 |
keystore.load(inStream, PASSWORD); |
15298 | 76 |
System.out.println("Loaded keystore with " + keystore.size() + |
77 |
" entries"); |
|
15532
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
78 |
} |
15298 | 79 |
|
15532
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
80 |
KeyStore.Entry entry = keystore.getEntry(ALIAS, null); |
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
81 |
if (entry instanceof KeyStore.TrustedCertificateEntry) { |
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
82 |
System.out.println("Retrieved trusted certificate entry: " + entry); |
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
83 |
} else { |
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
84 |
throw new Exception("Not a trusted certificate entry"); |
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
85 |
} |
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
86 |
System.out.println(); |
15298 | 87 |
|
15532
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
88 |
entry = keystore.getEntry(ALIAS2, null); |
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
89 |
if (entry instanceof KeyStore.TrustedCertificateEntry) { |
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
90 |
KeyStore.TrustedCertificateEntry trustedEntry = |
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
91 |
(KeyStore.TrustedCertificateEntry) entry; |
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
92 |
Set<KeyStore.Entry.Attribute> entryAttributes = |
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
93 |
trustedEntry.getAttributes(); |
15298 | 94 |
|
15532
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
95 |
if (entryAttributes.containsAll(attributes)) { |
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
96 |
System.out.println("Retrieved trusted certificate entry " + |
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
97 |
"with attributes: " + entry); |
15298 | 98 |
} else { |
15532
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
99 |
throw new Exception("Failed to retrieve entry attributes"); |
15298 | 100 |
} |
15532
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
101 |
} else { |
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
102 |
throw new Exception("Not a trusted certificate entry"); |
15298 | 103 |
} |
104 |
} |
|
105 |
||
106 |
private static Certificate loadCertificate(String certFile) |
|
107 |
throws Exception { |
|
108 |
X509Certificate cert = null; |
|
109 |
try (FileInputStream certStream = new FileInputStream(certFile)) { |
|
110 |
CertificateFactory factory = |
|
111 |
CertificateFactory.getInstance("X.509"); |
|
112 |
return factory.generateCertificate(certStream); |
|
113 |
} |
|
114 |
} |
|
115 |
} |