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