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/TestKeyMaterial.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 TlsKeyMaterial generator
28
* @author Andreas Sterbenz
29
* @library ..
30
* @run main/othervm TestKeyMaterial
31
* @run main/othervm TestKeyMaterial sm 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.IvParameterSpec;
42
import javax.crypto.spec.SecretKeySpec;
43
import sun.security.internal.spec.TlsKeyMaterialParameterSpec;
44
import sun.security.internal.spec.TlsKeyMaterialSpec;
45
46
public class TestKeyMaterial extends PKCS11Test {
47
48
private static final int PREFIX_LENGTH = "km-master: ".length();
49
50
public static void main(String[] args) throws Exception {
51
main(new TestKeyMaterial(), args);
52
}
53
54
@Override
55
public void main(Provider provider) throws Exception {
56
if (provider.getService("KeyGenerator", "SunTlsKeyMaterial") == null) {
57
System.out.println("Provider does not support algorithm, skipping");
58
return;
59
}
60
61
try (BufferedReader reader = Files.newBufferedReader(
62
Paths.get(BASE, "keymatdata.txt"))) {
63
64
int n = 0;
65
int lineNumber = 0;
66
67
byte[] master = null;
68
int major = 0;
69
int minor = 0;
70
byte[] clientRandom = null;
71
byte[] serverRandom = null;
72
String cipherAlgorithm = null;
73
int keyLength = 0;
74
int expandedKeyLength = 0;
75
int ivLength = 0;
76
int macLength = 0;
77
byte[] clientCipherBytes = null;
78
byte[] serverCipherBytes = null;
79
byte[] clientIv = null;
80
byte[] serverIv = null;
81
byte[] clientMacBytes = null;
82
byte[] serverMacBytes = null;
83
84
while (true) {
85
String line = reader.readLine();
86
lineNumber++;
87
if (line == null) {
88
break;
89
}
90
if (line.startsWith("km-") == false) {
91
continue;
92
}
93
String data = line.substring(PREFIX_LENGTH);
94
if (line.startsWith("km-master:")) {
95
master = parse(data);
96
} else if (line.startsWith("km-major:")) {
97
major = Integer.parseInt(data);
98
} else if (line.startsWith("km-minor:")) {
99
minor = Integer.parseInt(data);
100
} else if (line.startsWith("km-crandom:")) {
101
clientRandom = parse(data);
102
} else if (line.startsWith("km-srandom:")) {
103
serverRandom = parse(data);
104
} else if (line.startsWith("km-cipalg:")) {
105
cipherAlgorithm = data;
106
} else if (line.startsWith("km-keylen:")) {
107
keyLength = Integer.parseInt(data);
108
} else if (line.startsWith("km-explen:")) {
109
expandedKeyLength = Integer.parseInt(data);
110
} else if (line.startsWith("km-ivlen:")) {
111
ivLength = Integer.parseInt(data);
112
} else if (line.startsWith("km-maclen:")) {
113
macLength = Integer.parseInt(data);
114
} else if (line.startsWith("km-ccipkey:")) {
115
clientCipherBytes = parse(data);
116
} else if (line.startsWith("km-scipkey:")) {
117
serverCipherBytes = parse(data);
118
} else if (line.startsWith("km-civ:")) {
119
clientIv = parse(data);
120
} else if (line.startsWith("km-siv:")) {
121
serverIv = parse(data);
122
} else if (line.startsWith("km-cmackey:")) {
123
clientMacBytes = parse(data);
124
} else if (line.startsWith("km-smackey:")) {
125
serverMacBytes = parse(data);
126
127
System.out.print(".");
128
n++;
129
130
KeyGenerator kg =
131
KeyGenerator.getInstance("SunTlsKeyMaterial", provider);
132
SecretKey masterKey =
133
new SecretKeySpec(master, "TlsMasterSecret");
134
TlsKeyMaterialParameterSpec spec =
135
new TlsKeyMaterialParameterSpec(masterKey, major, minor,
136
clientRandom, serverRandom, cipherAlgorithm,
137
keyLength, expandedKeyLength, ivLength, macLength,
138
null, -1, -1);
139
140
kg.init(spec);
141
TlsKeyMaterialSpec result =
142
(TlsKeyMaterialSpec)kg.generateKey();
143
match(lineNumber, clientCipherBytes,
144
result.getClientCipherKey(), cipherAlgorithm);
145
match(lineNumber, serverCipherBytes,
146
result.getServerCipherKey(), cipherAlgorithm);
147
match(lineNumber, clientIv, result.getClientIv(), "");
148
match(lineNumber, serverIv, result.getServerIv(), "");
149
match(lineNumber, clientMacBytes, result.getClientMacKey(), "");
150
match(lineNumber, serverMacBytes, result.getServerMacKey(), "");
151
152
} else {
153
throw new Exception("Unknown line: " + line);
154
}
155
}
156
if (n == 0) {
157
throw new Exception("no tests");
158
}
159
System.out.println();
160
System.out.println("OK: " + n + " tests");
161
}
162
}
163
164
private static void stripParity(byte[] b) {
165
for (int i = 0; i < b.length; i++) {
166
b[i] &= 0xfe;
167
}
168
}
169
170
private static void match(int lineNumber, byte[] out, Object res,
171
String cipherAlgorithm) throws Exception {
172
if ((out == null) || (res == null)) {
173
if (out != res) {
174
throw new Exception("null mismatch line " + lineNumber);
175
} else {
176
return;
177
}
178
}
179
byte[] b;
180
if (res instanceof SecretKey) {
181
b = ((SecretKey)res).getEncoded();
182
if (cipherAlgorithm.equalsIgnoreCase("DES") ||
183
cipherAlgorithm.equalsIgnoreCase("DESede")) {
184
// strip DES parity bits before comparision
185
stripParity(out);
186
stripParity(b);
187
}
188
} else if (res instanceof IvParameterSpec) {
189
b = ((IvParameterSpec)res).getIV();
190
} else {
191
throw new Exception(res.getClass().getName());
192
}
193
if (Arrays.equals(out, b) == false) {
194
System.out.println();
195
System.out.println("out: " + toString(out));
196
System.out.println("b: " + toString(b));
197
throw new Exception("mismatch line " + lineNumber);
198
}
199
}
200
201
}
202
203