author | erikj |
Fri, 01 Apr 2016 17:08:43 +0200 | |
changeset 36725 | f458544b0d76 |
parent 15538 | 02e547c0b530 |
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.util.*; |
|
33 |
import javax.crypto.*; |
|
34 |
import javax.crypto.spec.*; |
|
35 |
import java.security.spec.InvalidKeySpecException; |
|
36 |
||
37 |
// Store a password in a keystore and retrieve it again. |
|
38 |
||
39 |
public class StorePasswordTest { |
|
40 |
private final static String DIR = System.getProperty("test.src", "."); |
|
41 |
private static final char[] PASSWORD = "passphrase".toCharArray(); |
|
42 |
private static final String KEYSTORE = "pwdstore.p12"; |
|
43 |
private static final String ALIAS = "my password"; |
|
44 |
private static final String USER_PASSWORD = "hello1"; |
|
45 |
||
46 |
public static void main(String[] args) throws Exception { |
|
47 |
||
48 |
new File(KEYSTORE).delete(); |
|
49 |
||
15532
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
50 |
KeyStore keystore = KeyStore.getInstance("PKCS12"); |
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
51 |
keystore.load(null, null); |
15298 | 52 |
|
15532
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
53 |
// Set entry |
15538
02e547c0b530
8007483: attributes are ignored when loading keys from a PKCS12 keystore
vinnie
parents:
15532
diff
changeset
|
54 |
Set<KeyStore.Entry.Attribute> attrs = new HashSet<>(); |
02e547c0b530
8007483: attributes are ignored when loading keys from a PKCS12 keystore
vinnie
parents:
15532
diff
changeset
|
55 |
attrs.add(new PKCS12Attribute("1.3.5.7.9", "printable1")); |
02e547c0b530
8007483: attributes are ignored when loading keys from a PKCS12 keystore
vinnie
parents:
15532
diff
changeset
|
56 |
attrs.add(new PKCS12Attribute("2.4.6.8.10", "1F:2F:3F:4F:5F")); |
02e547c0b530
8007483: attributes are ignored when loading keys from a PKCS12 keystore
vinnie
parents:
15532
diff
changeset
|
57 |
int originalAttrCount = attrs.size() + 2; |
15532
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
58 |
keystore.setEntry(ALIAS, |
15538
02e547c0b530
8007483: attributes are ignored when loading keys from a PKCS12 keystore
vinnie
parents:
15532
diff
changeset
|
59 |
new KeyStore.SecretKeyEntry(convertPassword(USER_PASSWORD), attrs), |
15532
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
60 |
new KeyStore.PasswordProtection(PASSWORD)); |
15298 | 61 |
|
15532
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
62 |
try (FileOutputStream outStream = new FileOutputStream(KEYSTORE)) { |
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
63 |
System.out.println("Storing keystore to: " + KEYSTORE); |
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
64 |
keystore.store(outStream, PASSWORD); |
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
65 |
} |
15298 | 66 |
|
15532
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
67 |
try (FileInputStream inStream = new FileInputStream(KEYSTORE)) { |
15298 | 68 |
System.out.println("Loading keystore from: " + KEYSTORE); |
15532
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
69 |
keystore.load(inStream, PASSWORD); |
15298 | 70 |
System.out.println("Loaded keystore with " + keystore.size() + |
71 |
" entries"); |
|
15532
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
72 |
} |
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
73 |
|
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
74 |
KeyStore.Entry entry = keystore.getEntry(ALIAS, |
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
75 |
new KeyStore.PasswordProtection(PASSWORD)); |
15538
02e547c0b530
8007483: attributes are ignored when loading keys from a PKCS12 keystore
vinnie
parents:
15532
diff
changeset
|
76 |
int attrCount = entry.getAttributes().size(); |
02e547c0b530
8007483: attributes are ignored when loading keys from a PKCS12 keystore
vinnie
parents:
15532
diff
changeset
|
77 |
System.out.println("Retrieved entry with " + attrCount + " attrs: " + |
02e547c0b530
8007483: attributes are ignored when loading keys from a PKCS12 keystore
vinnie
parents:
15532
diff
changeset
|
78 |
entry); |
02e547c0b530
8007483: attributes are ignored when loading keys from a PKCS12 keystore
vinnie
parents:
15532
diff
changeset
|
79 |
if (attrCount != originalAttrCount) { |
02e547c0b530
8007483: attributes are ignored when loading keys from a PKCS12 keystore
vinnie
parents:
15532
diff
changeset
|
80 |
throw new Exception("Failed to recover all the entry attributes"); |
02e547c0b530
8007483: attributes are ignored when loading keys from a PKCS12 keystore
vinnie
parents:
15532
diff
changeset
|
81 |
} |
15298 | 82 |
|
15532
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
83 |
SecretKey key = (SecretKey) keystore.getKey(ALIAS, PASSWORD); |
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
84 |
SecretKeyFactory factory = |
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
85 |
SecretKeyFactory.getInstance(key.getAlgorithm()); |
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
86 |
PBEKeySpec keySpec = |
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
87 |
(PBEKeySpec) factory.getKeySpec(key, PBEKeySpec.class); |
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
88 |
char[] pwd = keySpec.getPassword(); |
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
89 |
System.out.println("Recovered credential: " + new String(pwd)); |
15298 | 90 |
|
15532
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
91 |
if (!Arrays.equals(USER_PASSWORD.toCharArray(), pwd)) { |
859facd70580
8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents:
15298
diff
changeset
|
92 |
throw new Exception("Failed to recover the stored password"); |
15298 | 93 |
} |
94 |
} |
|
95 |
||
96 |
private static SecretKey convertPassword(String password) |
|
97 |
throws NoSuchAlgorithmException, InvalidKeySpecException { |
|
98 |
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBE"); |
|
99 |
return factory.generateSecret(new PBEKeySpec(password.toCharArray())); |
|
100 |
} |
|
101 |
} |