Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs12/P12SecretKey.java
38840 views
1
/*
2
* Copyright (c) 2016, 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 8149411
27
* @summary Get AES key from keystore (uses SecretKeySpec not SecretKeyFactory)
28
*/
29
30
import java.io.File;
31
import java.io.FileInputStream;
32
import java.io.FileOutputStream;
33
import java.security.KeyStore;
34
import java.security.cert.CertificateException;
35
import java.util.Arrays;
36
37
import javax.crypto.KeyGenerator;
38
import javax.crypto.SecretKey;
39
40
public class P12SecretKey {
41
42
private static final String ALIAS = "alias";
43
44
public static void main(String[] args) throws Exception {
45
P12SecretKey testp12 = new P12SecretKey();
46
String keystoreType = "pkcs12";
47
if (args != null && args.length > 0) {
48
keystoreType = args[0];
49
}
50
testp12.run(keystoreType);
51
}
52
53
private void run(String keystoreType) throws Exception {
54
char[] pw = "password".toCharArray();
55
KeyStore ks = KeyStore.getInstance(keystoreType);
56
ks.load(null, pw);
57
58
KeyGenerator kg = KeyGenerator.getInstance("AES");
59
kg.init(128);
60
SecretKey key = kg.generateKey();
61
62
KeyStore.SecretKeyEntry ske = new KeyStore.SecretKeyEntry(key);
63
KeyStore.ProtectionParameter kspp = new KeyStore.PasswordProtection(pw);
64
ks.setEntry(ALIAS, ske, kspp);
65
66
File ksFile = File.createTempFile("test", ".test");
67
try (FileOutputStream fos = new FileOutputStream(ksFile)) {
68
ks.store(fos, pw);
69
fos.flush();
70
}
71
72
// now see if we can get it back
73
try (FileInputStream fis = new FileInputStream(ksFile)) {
74
KeyStore ks2 = KeyStore.getInstance(keystoreType);
75
ks2.load(fis, pw);
76
KeyStore.Entry entry = ks2.getEntry(ALIAS, kspp);
77
SecretKey keyIn = ((KeyStore.SecretKeyEntry)entry).getSecretKey();
78
if (Arrays.equals(key.getEncoded(), keyIn.getEncoded())) {
79
System.err.println("OK: worked just fine with " + keystoreType +
80
" keystore");
81
} else {
82
System.err.println("ERROR: keys are NOT equal after storing in "
83
+ keystoreType + " keystore");
84
}
85
}
86
}
87
}
88
89