author | erikj |
Fri, 01 Apr 2016 17:08:43 +0200 | |
changeset 36725 | f458544b0d76 |
parent 31718 | c10ac6f1e391 |
child 45028 | b0ea3c0bfb81 |
permissions | -rw-r--r-- |
15298 | 1 |
/* |
30368
60f02327d396
8079129: NullPointerException in PKCS#12 Keystore in PKCS12KeyStore.java
vinnie
parents:
15532
diff
changeset
|
2 |
* Copyright (c) 2013, 2015 Oracle and/or its affiliates. All rights reserved. |
15298 | 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 |
|
31718
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
26 |
* @bug 8005408 8079129 8048830 |
15298 | 27 |
* @summary KeyStore API enhancements |
31718
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
28 |
* @run main StoreSecretKeyTest |
15298 | 29 |
*/ |
30 |
||
31 |
import java.io.*; |
|
32 |
import java.security.*; |
|
30368
60f02327d396
8079129: NullPointerException in PKCS#12 Keystore in PKCS12KeyStore.java
vinnie
parents:
15532
diff
changeset
|
33 |
import java.security.cert.*; |
60f02327d396
8079129: NullPointerException in PKCS#12 Keystore in PKCS12KeyStore.java
vinnie
parents:
15532
diff
changeset
|
34 |
import java.security.cert.Certificate; |
15298 | 35 |
import java.util.*; |
36 |
import javax.crypto.*; |
|
37 |
import javax.crypto.spec.*; |
|
38 |
||
39 |
// Store a secret key in a keystore and retrieve it again. |
|
40 |
||
41 |
public class StoreSecretKeyTest { |
|
42 |
private final static String DIR = System.getProperty("test.src", "."); |
|
43 |
private static final char[] PASSWORD = "passphrase".toCharArray(); |
|
44 |
private static final String KEYSTORE = "keystore.p12"; |
|
30368
60f02327d396
8079129: NullPointerException in PKCS#12 Keystore in PKCS12KeyStore.java
vinnie
parents:
15532
diff
changeset
|
45 |
private static final String CERT = DIR + "/trusted.pem"; |
60f02327d396
8079129: NullPointerException in PKCS#12 Keystore in PKCS12KeyStore.java
vinnie
parents:
15532
diff
changeset
|
46 |
private static final String ALIAS = "my trusted cert"; |
60f02327d396
8079129: NullPointerException in PKCS#12 Keystore in PKCS12KeyStore.java
vinnie
parents:
15532
diff
changeset
|
47 |
private static final String ALIAS2 = "my secret key"; |
31718
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
48 |
private enum ALGORITHM { |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
49 |
DES(56), |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
50 |
DESede(168), |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
51 |
AES(128); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
52 |
final int len; |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
53 |
ALGORITHM(int l) { |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
54 |
len = l; |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
55 |
} |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
56 |
final int getLength() { |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
57 |
return len; |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
58 |
} |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
59 |
} |
15298 | 60 |
public static void main(String[] args) throws Exception { |
31718
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
61 |
boolean isSecretkeyAlgSupported = false; |
15301
215128369cab
8006855: PKCS12 test failures due to unsupported algorithm
vinnie
parents:
15298
diff
changeset
|
62 |
// Skip test if AES is unavailable |
215128369cab
8006855: PKCS12 test failures due to unsupported algorithm
vinnie
parents:
15298
diff
changeset
|
63 |
try { |
215128369cab
8006855: PKCS12 test failures due to unsupported algorithm
vinnie
parents:
15298
diff
changeset
|
64 |
SecretKeyFactory.getInstance("AES"); |
215128369cab
8006855: PKCS12 test failures due to unsupported algorithm
vinnie
parents:
15298
diff
changeset
|
65 |
} catch (NoSuchAlgorithmException nsae) { |
215128369cab
8006855: PKCS12 test failures due to unsupported algorithm
vinnie
parents:
15298
diff
changeset
|
66 |
System.out.println("AES is unavailable. Skipping test..."); |
215128369cab
8006855: PKCS12 test failures due to unsupported algorithm
vinnie
parents:
15298
diff
changeset
|
67 |
return; |
215128369cab
8006855: PKCS12 test failures due to unsupported algorithm
vinnie
parents:
15298
diff
changeset
|
68 |
} |
215128369cab
8006855: PKCS12 test failures due to unsupported algorithm
vinnie
parents:
15298
diff
changeset
|
69 |
|
31718
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
70 |
for (ALGORITHM alg : ALGORITHM.values()) { |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
71 |
isSecretkeyAlgSupported |= testSecretKeyAlgorithm(alg); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
72 |
} |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
73 |
if (!isSecretkeyAlgSupported) { |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
74 |
throw new Exception("None of the SecretKey algorithms is " |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
75 |
+ "supported"); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
76 |
} |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
77 |
} |
15298 | 78 |
|
31718
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
79 |
private static boolean testSecretKeyAlgorithm(ALGORITHM algorithm) throws |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
80 |
Exception { |
15298 | 81 |
|
31718
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
82 |
System.out.println("Testing algorithm : " + algorithm.name()); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
83 |
new File(KEYSTORE).delete(); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
84 |
try { |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
85 |
KeyStore keystore = KeyStore.getInstance("PKCS12"); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
86 |
keystore.load(null, null); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
87 |
|
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
88 |
// Set trusted certificate entry |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
89 |
Certificate cert = loadCertificate(CERT); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
90 |
keystore.setEntry(ALIAS, |
30368
60f02327d396
8079129: NullPointerException in PKCS#12 Keystore in PKCS12KeyStore.java
vinnie
parents:
15532
diff
changeset
|
91 |
new KeyStore.TrustedCertificateEntry(cert), null); |
31718
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
92 |
// Set secret key entry |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
93 |
SecretKey secretKey = generateSecretKey(algorithm.name(), |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
94 |
algorithm.len); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
95 |
if(secretKey == null) { |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
96 |
return false; |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
97 |
} |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
98 |
keystore.setEntry(ALIAS2, |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
99 |
new KeyStore.SecretKeyEntry(secretKey), |
15532
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15301
diff
changeset
|
100 |
new KeyStore.PasswordProtection(PASSWORD)); |
15298 | 101 |
|
31718
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
102 |
try (FileOutputStream outStream = new FileOutputStream(KEYSTORE)) { |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
103 |
System.out.println("Storing keystore to: " + KEYSTORE); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
104 |
keystore.store(outStream, PASSWORD); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
105 |
} |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
106 |
|
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
107 |
try (FileInputStream inStream = new FileInputStream(KEYSTORE)) { |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
108 |
System.out.println("Loading keystore from: " + KEYSTORE); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
109 |
keystore.load(inStream, PASSWORD); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
110 |
System.out.println("Loaded keystore with " + keystore.size() + |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
111 |
" entries"); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
112 |
} |
15298 | 113 |
|
31718
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
114 |
KeyStore.Entry entry = keystore.getEntry(ALIAS2, |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
115 |
new KeyStore.PasswordProtection(PASSWORD)); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
116 |
System.out.println("Retrieved entry: " + entry); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
117 |
|
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
118 |
if (entry instanceof KeyStore.SecretKeyEntry) { |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
119 |
System.out.println("Retrieved secret key entry: " + entry); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
120 |
} else { |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
121 |
throw new Exception("Not a secret key entry"); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
122 |
} |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
123 |
} catch (KeyStoreException | UnrecoverableKeyException ex) { |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
124 |
System.out.println("Unable to check SecretKey algorithm due to " |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
125 |
+ "exception: " + ex.getMessage()); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
126 |
return false; |
15532
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15301
diff
changeset
|
127 |
} |
31718
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
30368
diff
changeset
|
128 |
return true; |
15298 | 129 |
} |
130 |
||
131 |
private static SecretKey generateSecretKey(String algorithm, int size) |
|
132 |
throws NoSuchAlgorithmException { |
|
133 |
KeyGenerator generator = KeyGenerator.getInstance(algorithm); |
|
134 |
generator.init(size); |
|
135 |
return generator.generateKey(); |
|
136 |
} |
|
30368
60f02327d396
8079129: NullPointerException in PKCS#12 Keystore in PKCS12KeyStore.java
vinnie
parents:
15532
diff
changeset
|
137 |
|
60f02327d396
8079129: NullPointerException in PKCS#12 Keystore in PKCS12KeyStore.java
vinnie
parents:
15532
diff
changeset
|
138 |
private static Certificate loadCertificate(String certFile) |
60f02327d396
8079129: NullPointerException in PKCS#12 Keystore in PKCS12KeyStore.java
vinnie
parents:
15532
diff
changeset
|
139 |
throws Exception { |
60f02327d396
8079129: NullPointerException in PKCS#12 Keystore in PKCS12KeyStore.java
vinnie
parents:
15532
diff
changeset
|
140 |
X509Certificate cert = null; |
60f02327d396
8079129: NullPointerException in PKCS#12 Keystore in PKCS12KeyStore.java
vinnie
parents:
15532
diff
changeset
|
141 |
try (FileInputStream certStream = new FileInputStream(certFile)) { |
60f02327d396
8079129: NullPointerException in PKCS#12 Keystore in PKCS12KeyStore.java
vinnie
parents:
15532
diff
changeset
|
142 |
CertificateFactory factory = |
60f02327d396
8079129: NullPointerException in PKCS#12 Keystore in PKCS12KeyStore.java
vinnie
parents:
15532
diff
changeset
|
143 |
CertificateFactory.getInstance("X.509"); |
60f02327d396
8079129: NullPointerException in PKCS#12 Keystore in PKCS12KeyStore.java
vinnie
parents:
15532
diff
changeset
|
144 |
return factory.generateCertificate(certStream); |
60f02327d396
8079129: NullPointerException in PKCS#12 Keystore in PKCS12KeyStore.java
vinnie
parents:
15532
diff
changeset
|
145 |
} |
60f02327d396
8079129: NullPointerException in PKCS#12 Keystore in PKCS12KeyStore.java
vinnie
parents:
15532
diff
changeset
|
146 |
} |
15298 | 147 |
} |