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/TestMasterSecret.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 Known-answer-test for TlsMasterSecret generator
28
* @author Andreas Sterbenz
29
* @library ..
30
* @run main/othervm TestMasterSecret
31
* @run main/othervm TestMasterSecret sm TestMasterSecret.policy
32
*/
33
34
import java.io.BufferedReader;
35
import java.nio.file.Files;
36
import java.nio.file.Paths;
37
import java.security.Provider;
38
import java.util.Arrays;
39
import javax.crypto.KeyGenerator;
40
import javax.crypto.SecretKey;
41
import javax.crypto.spec.SecretKeySpec;
42
import sun.security.internal.interfaces.TlsMasterSecret;
43
import sun.security.internal.spec.TlsMasterSecretParameterSpec;
44
45
public class TestMasterSecret extends PKCS11Test {
46
47
private static final int PREFIX_LENGTH = "m-premaster: ".length();
48
49
public static void main(String[] args) throws Exception {
50
main(new TestMasterSecret(), args);
51
}
52
53
@Override
54
public void main(Provider provider) throws Exception {
55
if (provider.getService("KeyGenerator", "SunTlsMasterSecret") == null) {
56
System.out.println("Not supported by provider, skipping");
57
return;
58
}
59
60
try (BufferedReader reader = Files.newBufferedReader(
61
Paths.get(BASE, "masterdata.txt"))) {
62
63
int n = 0;
64
int lineNumber = 0;
65
66
String algorithm = null;
67
byte[] premaster = null;
68
byte[] clientRandom = null;
69
byte[] serverRandom = null;
70
int protoMajor = 0;
71
int protoMinor = 0;
72
int preMajor = 0;
73
int preMinor = 0;
74
byte[] master = null;
75
76
while (true) {
77
String line = reader.readLine();
78
lineNumber++;
79
if (line == null) {
80
break;
81
}
82
if (line.startsWith("m-") == false) {
83
continue;
84
}
85
String data = line.substring(PREFIX_LENGTH);
86
if (line.startsWith("m-algorithm:")) {
87
algorithm = data;
88
} else if (line.startsWith("m-premaster:")) {
89
premaster = parse(data);
90
} else if (line.startsWith("m-crandom:")) {
91
clientRandom = parse(data);
92
} else if (line.startsWith("m-srandom:")) {
93
serverRandom = parse(data);
94
} else if (line.startsWith("m-protomajor:")) {
95
protoMajor = Integer.parseInt(data);
96
} else if (line.startsWith("m-protominor:")) {
97
protoMinor = Integer.parseInt(data);
98
} else if (line.startsWith("m-premajor:")) {
99
preMajor = Integer.parseInt(data);
100
} else if (line.startsWith("m-preminor:")) {
101
preMinor = Integer.parseInt(data);
102
} else if (line.startsWith("m-master:")) {
103
master = parse(data);
104
105
System.out.print(".");
106
n++;
107
108
KeyGenerator kg =
109
KeyGenerator.getInstance("SunTlsMasterSecret", provider);
110
SecretKey premasterKey =
111
new SecretKeySpec(premaster, algorithm);
112
TlsMasterSecretParameterSpec spec =
113
new TlsMasterSecretParameterSpec(premasterKey,
114
protoMajor, protoMinor, clientRandom, serverRandom,
115
null, -1, -1);
116
kg.init(spec);
117
TlsMasterSecret key = (TlsMasterSecret)kg.generateKey();
118
byte[] enc = key.getEncoded();
119
if (Arrays.equals(master, enc) == false) {
120
throw new Exception("mismatch line: " + lineNumber);
121
}
122
if ((preMajor != key.getMajorVersion()) ||
123
(preMinor != key.getMinorVersion())) {
124
throw new Exception("version mismatch line: " + lineNumber);
125
}
126
} else {
127
throw new Exception("Unknown line: " + line);
128
}
129
}
130
if (n == 0) {
131
throw new Exception("no tests");
132
}
133
System.out.println();
134
System.out.println("OK: " + n + " tests");
135
}
136
}
137
138
}
139
140