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/pkcs11/tls/TestPremaster.java
38855 views
1
/*
2
* Copyright (c) 2005, 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 6316539
27
* @summary Basic tests for TlsRsaPremasterSecret generator
28
* @author Andreas Sterbenz
29
* @library ..
30
* @run main/othervm TestPremaster
31
* @run main/othervm TestPremaster sm policy
32
*/
33
34
import java.security.Provider;
35
import javax.crypto.KeyGenerator;
36
import javax.crypto.SecretKey;
37
import sun.security.internal.spec.TlsRsaPremasterSecretParameterSpec;
38
39
public class TestPremaster extends PKCS11Test {
40
41
public static void main(String[] args) throws Exception {
42
main(new TestPremaster(), args);
43
}
44
45
@Override
46
public void main(Provider provider) throws Exception {
47
if (provider.getService(
48
"KeyGenerator", "SunTlsRsaPremasterSecret") == null) {
49
System.out.println("Not supported by provider, skipping");
50
return;
51
}
52
KeyGenerator kg;
53
kg = KeyGenerator.getInstance("SunTlsRsaPremasterSecret", provider);
54
55
try {
56
kg.generateKey();
57
throw new Exception("no exception");
58
} catch (IllegalStateException e) {
59
System.out.println("OK: " + e);
60
}
61
62
int[] protocolVersions = {0x0300, 0x0301, 0x0302, 0x0400};
63
for (int clientVersion : protocolVersions) {
64
for (int serverVersion : protocolVersions) {
65
test(kg, clientVersion, serverVersion);
66
if (serverVersion >= clientVersion) {
67
break;
68
}
69
}
70
}
71
72
System.out.println("Done.");
73
}
74
75
private static void test(KeyGenerator kg,
76
int clientVersion, int serverVersion) throws Exception {
77
78
System.out.printf(
79
"Testing RSA pre-master secret key generation between " +
80
"client (0x%04X) and server(0x%04X)%n",
81
clientVersion, serverVersion);
82
kg.init(new TlsRsaPremasterSecretParameterSpec(
83
clientVersion, serverVersion));
84
SecretKey key = kg.generateKey();
85
byte[] encoded = key.getEncoded();
86
if (encoded != null) { // raw key material may be not extractable
87
if (encoded.length != 48) {
88
throw new Exception("length: " + encoded.length);
89
}
90
int v = versionOf(encoded[0], encoded[1]);
91
if (clientVersion != v) {
92
if (serverVersion != v || clientVersion >= 0x0302) {
93
throw new Exception(String.format(
94
"version mismatch: (0x%04X) rather than (0x%04X) " +
95
"is used in pre-master secret", v, clientVersion));
96
}
97
System.out.printf("Use compatible version (0x%04X)%n", v);
98
}
99
System.out.println("Passed, version matches!");
100
} else {
101
System.out.println("Raw key material is not extractable");
102
}
103
}
104
105
private static int versionOf(int major, int minor) {
106
return ((major & 0xFF) << 8) | (minor & 0xFF);
107
}
108
109
}
110
111