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