Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/crypto/provider/TLS/TestKeyMaterial.java
38867 views
1
/*
2
* Copyright (c) 2005, 2010, 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 6313661
27
* @summary Known-answer-test for TlsKeyMaterial generator
28
* @author Andreas Sterbenz
29
*/
30
31
import java.io.*;
32
import java.util.*;
33
34
import java.security.Security;
35
import java.security.Provider;
36
37
import javax.crypto.KeyGenerator;
38
import javax.crypto.SecretKey;
39
40
import javax.crypto.spec.*;
41
42
import sun.security.internal.spec.*;
43
44
public class TestKeyMaterial extends Utils {
45
46
private static int PREFIX_LENGTH = "km-master: ".length();
47
48
public static void main(String[] args) throws Exception {
49
Provider provider = Security.getProvider("SunJCE");
50
51
InputStream in = new FileInputStream(new File(BASE, "keymatdata.txt"));
52
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
53
54
int n = 0;
55
int lineNumber = 0;
56
57
byte[] master = null;
58
int major = 0;
59
int minor = 0;
60
byte[] clientRandom = null;
61
byte[] serverRandom = null;
62
String cipherAlgorithm = null;
63
int keyLength = 0;
64
int expandedKeyLength = 0;
65
int ivLength = 0;
66
int macLength = 0;
67
byte[] clientCipherBytes = null;
68
byte[] serverCipherBytes = null;
69
byte[] clientIv = null;
70
byte[] serverIv = null;
71
byte[] clientMacBytes = null;
72
byte[] serverMacBytes = null;
73
74
while (true) {
75
String line = reader.readLine();
76
lineNumber++;
77
if (line == null) {
78
break;
79
}
80
if (line.startsWith("km-") == false) {
81
continue;
82
}
83
String data = line.substring(PREFIX_LENGTH);
84
if (line.startsWith("km-master:")) {
85
master = parse(data);
86
} else if (line.startsWith("km-major:")) {
87
major = Integer.parseInt(data);
88
} else if (line.startsWith("km-minor:")) {
89
minor = Integer.parseInt(data);
90
} else if (line.startsWith("km-crandom:")) {
91
clientRandom = parse(data);
92
} else if (line.startsWith("km-srandom:")) {
93
serverRandom = parse(data);
94
} else if (line.startsWith("km-cipalg:")) {
95
cipherAlgorithm = data;
96
} else if (line.startsWith("km-keylen:")) {
97
keyLength = Integer.parseInt(data);
98
} else if (line.startsWith("km-explen:")) {
99
expandedKeyLength = Integer.parseInt(data);
100
} else if (line.startsWith("km-ivlen:")) {
101
ivLength = Integer.parseInt(data);
102
} else if (line.startsWith("km-maclen:")) {
103
macLength = Integer.parseInt(data);
104
} else if (line.startsWith("km-ccipkey:")) {
105
clientCipherBytes = parse(data);
106
} else if (line.startsWith("km-scipkey:")) {
107
serverCipherBytes = parse(data);
108
} else if (line.startsWith("km-civ:")) {
109
clientIv = parse(data);
110
} else if (line.startsWith("km-siv:")) {
111
serverIv = parse(data);
112
} else if (line.startsWith("km-cmackey:")) {
113
clientMacBytes = parse(data);
114
} else if (line.startsWith("km-smackey:")) {
115
serverMacBytes = parse(data);
116
117
System.out.print(".");
118
n++;
119
120
KeyGenerator kg =
121
KeyGenerator.getInstance("SunTlsKeyMaterial", provider);
122
SecretKey masterKey =
123
new SecretKeySpec(master, "TlsMasterSecret");
124
TlsKeyMaterialParameterSpec spec =
125
new TlsKeyMaterialParameterSpec(masterKey, major, minor,
126
clientRandom, serverRandom, cipherAlgorithm,
127
keyLength, expandedKeyLength, ivLength, macLength,
128
null, -1, -1);
129
130
kg.init(spec);
131
TlsKeyMaterialSpec result =
132
(TlsKeyMaterialSpec)kg.generateKey();
133
match(lineNumber, clientCipherBytes,
134
result.getClientCipherKey());
135
match(lineNumber, serverCipherBytes,
136
result.getServerCipherKey());
137
match(lineNumber, clientIv, result.getClientIv());
138
match(lineNumber, serverIv, result.getServerIv());
139
match(lineNumber, clientMacBytes, result.getClientMacKey());
140
match(lineNumber, serverMacBytes, result.getServerMacKey());
141
142
} else {
143
throw new Exception("Unknown line: " + line);
144
}
145
}
146
if (n == 0) {
147
throw new Exception("no tests");
148
}
149
in.close();
150
System.out.println();
151
System.out.println("OK: " + n + " tests");
152
}
153
154
private static void match(int lineNumber, byte[] out, Object res)
155
throws Exception {
156
if ((out == null) || (res == null)) {
157
if (out != res) {
158
throw new Exception("null mismatch line " + lineNumber);
159
} else {
160
return;
161
}
162
}
163
byte[] b;
164
if (res instanceof SecretKey) {
165
b = ((SecretKey)res).getEncoded();
166
} else if (res instanceof IvParameterSpec) {
167
b = ((IvParameterSpec)res).getIV();
168
} else {
169
throw new Exception(res.getClass().getName());
170
}
171
if (Arrays.equals(out, b) == false) {
172
throw new Exception("mismatch line " + lineNumber);
173
}
174
}
175
176
}
177
178