Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/pkcs/pkcs8/PKCS8Test.java
41153 views
1
/*
2
* Copyright (c) 2015, 2021, 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 8048357 8244565
27
* @summary PKCS8 Standards Conformance Tests
28
* @library /test/lib
29
* @modules java.base/sun.security.pkcs
30
* java.base/sun.security.util
31
* java.base/sun.security.provider
32
* java.base/sun.security.x509
33
* @compile -XDignore.symbol.file PKCS8Test.java
34
* @run testng PKCS8Test
35
*/
36
37
import java.io.IOException;
38
import java.math.BigInteger;
39
import java.util.Arrays;
40
import java.util.HexFormat;
41
42
import jdk.test.lib.hexdump.ASN1Formatter;
43
import jdk.test.lib.hexdump.HexPrinter;
44
import org.testng.Assert;
45
import org.testng.annotations.Test;
46
import sun.security.pkcs.PKCS8Key;
47
import sun.security.provider.DSAPrivateKey;
48
import sun.security.util.DerValue;
49
50
public class PKCS8Test {
51
52
static final String FORMAT = "PKCS#8";
53
static final String EXPECTED_ALG_ID_CHRS = "DSA, \n" +
54
"\tp: 02\n\tq: 03\n\tg: 04\n";
55
static final String ALGORITHM = "DSA";
56
57
static final byte[] EXPECTED = HexFormat.of().parseHex(
58
"301e" + // SEQUENCE
59
"020100" + // Version int 0
60
"3014" + // PrivateKeyAlgorithmIdentifier
61
"06072a8648ce380401" + // OID DSA 1.2.840.10040.4.1
62
"3009020102020103020104" + // p=2, q=3, g=4
63
"0403020101"); // PrivateKey OCTET int x = 1
64
65
@Test
66
public void test() throws IOException {
67
68
byte[] encodedKey = new DSAPrivateKey(
69
BigInteger.valueOf(1),
70
BigInteger.valueOf(2),
71
BigInteger.valueOf(3),
72
BigInteger.valueOf(4)).getEncoded();
73
74
Assert.assertTrue(Arrays.equals(encodedKey, EXPECTED),
75
HexPrinter.simple()
76
.formatter(ASN1Formatter.formatter())
77
.toString(encodedKey));
78
79
PKCS8Key decodedKey = (PKCS8Key)PKCS8Key.parseKey(encodedKey);
80
81
Assert.assertEquals(decodedKey.getAlgorithm(), ALGORITHM);
82
Assert.assertEquals(decodedKey.getFormat(), FORMAT);
83
Assert.assertEquals(decodedKey.getAlgorithmId().toString(),
84
EXPECTED_ALG_ID_CHRS);
85
86
byte[] encodedOutput = decodedKey.getEncoded();
87
Assert.assertTrue(Arrays.equals(encodedOutput, EXPECTED),
88
HexPrinter.simple()
89
.formatter(ASN1Formatter.formatter())
90
.toString(encodedOutput));
91
92
// Test additional fields
93
enlarge(0, "8000"); // attributes
94
enlarge(1, "810100"); // public key for v2
95
enlarge(1, "8000", "810100"); // both
96
97
Assert.assertThrows(() -> enlarge(2)); // bad ver
98
Assert.assertThrows(() -> enlarge(0, "8000", "8000")); // no dup
99
Assert.assertThrows(() -> enlarge(0, "810100")); // no public in v1
100
Assert.assertThrows(() -> enlarge(1, "810100", "8000")); // bad order
101
Assert.assertThrows(() -> enlarge(1, "820100")); // bad tag
102
}
103
104
/**
105
* Add more fields to EXPECTED and see if it's still valid PKCS8.
106
*
107
* @param newVersion new version
108
* @param fields extra fields to add, in hex
109
*/
110
static void enlarge(int newVersion, String... fields) throws IOException {
111
byte[] original = EXPECTED.clone();
112
int length = original.length;
113
for (String field : fields) { // append fields
114
byte[] add = HexFormat.of().parseHex(field);
115
original = Arrays.copyOf(original, length + add.length);
116
System.arraycopy(add, 0, original, length, add.length);
117
length += add.length;
118
}
119
Assert.assertTrue(length < 127);
120
original[1] = (byte)(length - 2); // the length field inside DER
121
original[4] = (byte)newVersion; // the version inside DER
122
PKCS8Key.parseKey(original);
123
}
124
}
125
126