Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/security/KeyStore/PKCS12/MetadataStoreLoadTest.java
41153 views
1
/*
2
* Copyright (c) 2012, 2018, 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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
import java.io.File;
27
import java.io.IOException;
28
import java.security.GeneralSecurityException;
29
import java.security.Key;
30
import java.security.KeyStore;
31
import java.security.KeyStoreException;
32
import java.security.NoSuchAlgorithmException;
33
import java.security.PKCS12Attribute;
34
import java.security.PrivateKey;
35
import java.security.UnrecoverableEntryException;
36
import java.security.cert.Certificate;
37
import java.util.Arrays;
38
import java.util.Set;
39
import static java.lang.System.out;
40
import java.util.HashSet;
41
42
/**
43
* @test
44
* @bug 8048830
45
* @summary Test store metadata attributes to PKCS12 keystore.
46
* @library ../
47
* @library /test/lib
48
* @run main MetadataStoreLoadTest
49
*/
50
public class MetadataStoreLoadTest {
51
private static final char[] PASSWORD = "passwd".toCharArray();
52
private static final char[] KEY_PASSWORD = "keypasswd".toCharArray();
53
private static final String ALIAS = "testkey_metadata";
54
private static final String KEYSTORE = "ks.pkcs12";
55
private static final String KESTORE_NEW = "ks-attr.pkcs12";
56
private static final int MAX_HUGE_SIZE = 2000000;
57
private static final String WORKING_DIRECTORY = System.getProperty(
58
"test.classes", "." + File.separator);
59
private static final String KEYSTORE_PATH = WORKING_DIRECTORY
60
+ File.separator + KEYSTORE;
61
private static KeyStore.Entry.Attribute[] ATTR_SET;
62
63
private void runTest() throws GeneralSecurityException,
64
UnrecoverableEntryException, NoSuchAlgorithmException,
65
KeyStoreException, IOException {
66
storeAttrs();
67
checkAttrs();
68
}
69
70
private void storeAttrs() throws UnrecoverableEntryException,
71
GeneralSecurityException, NoSuchAlgorithmException,
72
KeyStoreException, IOException {
73
KeyStore ksIn = Utils.loadKeyStore(KEYSTORE_PATH,
74
Utils.KeyStoreType.pkcs12, PASSWORD);
75
KeyStore ksAttr = KeyStore
76
.getInstance(Utils.KeyStoreType.pkcs12.name());
77
ksAttr.load(null);
78
Key key = ksIn.getKey(ALIAS, PASSWORD);
79
Certificate cert = ksIn.getCertificate(ALIAS);
80
Set<KeyStore.Entry.Attribute> attrs =
81
new HashSet<>(Arrays.asList(ATTR_SET));
82
KeyStore.Entry e = new KeyStore.PrivateKeyEntry((PrivateKey) key,
83
new Certificate[]{cert}, attrs);
84
ksAttr.setEntry(ALIAS, e, new KeyStore.PasswordProtection(
85
KEY_PASSWORD));
86
87
out.println("Attributes before store:");
88
e.getAttributes().stream().forEach((attr) -> {
89
out.println(attr.getName() + ", '" + attr.getValue() + "'");
90
});
91
Utils.saveKeyStore(ksAttr, WORKING_DIRECTORY + File.separator
92
+ KESTORE_NEW, PASSWORD);
93
}
94
95
private void checkAttrs() throws UnrecoverableEntryException,
96
GeneralSecurityException, NoSuchAlgorithmException,
97
KeyStoreException, IOException {
98
KeyStore ks = Utils.loadKeyStore(WORKING_DIRECTORY
99
+ File.separator
100
+ KESTORE_NEW, Utils.KeyStoreType.pkcs12, PASSWORD);
101
KeyStore.Entry keyStoreEntry = ks.getEntry(ALIAS,
102
new KeyStore.PasswordProtection(KEY_PASSWORD));
103
out.println("Attributes after store:");
104
//print attribute values
105
keyStoreEntry.getAttributes().stream().forEach((attr) -> {
106
out.println(attr.getName() + ", '" + attr.getValue() + "'");
107
});
108
Arrays.stream(ATTR_SET).forEach((attr) -> {
109
if (!keyStoreEntry.getAttributes().contains(attr)) {
110
throw new RuntimeException("Entry doesn't contain attribute: ("
111
+ attr.getName() + ", '" + attr.getValue() + "')");
112
}
113
});
114
}
115
116
public static void main(String[] args) throws Exception {
117
MetadataStoreLoadTest test = new MetadataStoreLoadTest();
118
test.setUp();
119
test.runTest();
120
out.println("Test Passed");
121
}
122
123
private void setUp() {
124
Utils.createKeyStore(Utils.KeyStoreType.pkcs12, KEYSTORE_PATH, ALIAS);
125
final String allCharsString = "`1234567890-=qwertyuiop[]asdfghjkl;'\\zx"
126
+ "cvbnm,./!@#$%^&*()_+QWERTYUIOP{}ASDFGHJKL:|>ZXCVBNM<>?\"";
127
StringBuilder sbPrintable = new StringBuilder();
128
while (sbPrintable.length() < MAX_HUGE_SIZE) {
129
sbPrintable.append(allCharsString);
130
}
131
final String hugePrintable = sbPrintable.toString();
132
final String binaryString = "00:11:22:33:44:55:66:77:88:99:AA:BB:DD:"
133
+ "EE:FF:";
134
StringBuilder sbBinary = new StringBuilder();
135
sbBinary.append(binaryString);
136
while (sbBinary.length() < MAX_HUGE_SIZE) {
137
sbBinary.append(":").append(binaryString);
138
}
139
sbBinary.insert(0, "[").append("]");
140
final String hugeBinary = sbBinary.toString();
141
ATTR_SET = new PKCS12Attribute[5];
142
ATTR_SET[0] = new PKCS12Attribute("1.2.840.113549.1.9.1",
143
"Test email addres attr <[email protected]>");
144
ATTR_SET[1] = new PKCS12Attribute("1.2.110.1", "not registered attr");
145
ATTR_SET[2] = new PKCS12Attribute("1.2.110.2", hugePrintable);
146
ATTR_SET[3] = new PKCS12Attribute("1.2.110.3", hugeBinary);
147
ATTR_SET[4] = new PKCS12Attribute("1.2.110.2", " ");
148
}
149
}
150
151