2
|
1 |
/*
|
|
2 |
* Copyright 2005 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 |
* have any questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
/**
|
|
25 |
* @test
|
|
26 |
* @bug 6316539
|
|
27 |
* @summary Known-answer-test for TlsKeyMaterial generator
|
|
28 |
* @author Andreas Sterbenz
|
|
29 |
* @library ..
|
|
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 PKCS11Test {
|
|
46 |
|
|
47 |
private static int PREFIX_LENGTH = "km-master: ".length();
|
|
48 |
|
|
49 |
public static void main(String[] args) throws Exception {
|
|
50 |
main(new TestKeyMaterial());
|
|
51 |
}
|
|
52 |
|
|
53 |
public void main(Provider provider) throws Exception {
|
|
54 |
if (provider.getService("KeyGenerator", "SunTlsKeyMaterial") == null) {
|
|
55 |
System.out.println("Provider does not support algorithm, skipping");
|
|
56 |
return;
|
|
57 |
}
|
|
58 |
|
|
59 |
InputStream in = new FileInputStream(new File(BASE, "keymatdata.txt"));
|
|
60 |
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
|
|
61 |
|
|
62 |
int n = 0;
|
|
63 |
int lineNumber = 0;
|
|
64 |
|
|
65 |
byte[] master = null;
|
|
66 |
int major = 0;
|
|
67 |
int minor = 0;
|
|
68 |
byte[] clientRandom = null;
|
|
69 |
byte[] serverRandom = null;
|
|
70 |
String cipherAlgorithm = null;
|
|
71 |
int keyLength = 0;
|
|
72 |
int expandedKeyLength = 0;
|
|
73 |
int ivLength = 0;
|
|
74 |
int macLength = 0;
|
|
75 |
byte[] clientCipherBytes = null;
|
|
76 |
byte[] serverCipherBytes = null;
|
|
77 |
byte[] clientIv = null;
|
|
78 |
byte[] serverIv = null;
|
|
79 |
byte[] clientMacBytes = null;
|
|
80 |
byte[] serverMacBytes = null;
|
|
81 |
|
|
82 |
while (true) {
|
|
83 |
String line = reader.readLine();
|
|
84 |
lineNumber++;
|
|
85 |
if (line == null) {
|
|
86 |
break;
|
|
87 |
}
|
|
88 |
if (line.startsWith("km-") == false) {
|
|
89 |
continue;
|
|
90 |
}
|
|
91 |
String data = line.substring(PREFIX_LENGTH);
|
|
92 |
if (line.startsWith("km-master:")) {
|
|
93 |
master = parse(data);
|
|
94 |
} else if (line.startsWith("km-major:")) {
|
|
95 |
major = Integer.parseInt(data);
|
|
96 |
} else if (line.startsWith("km-minor:")) {
|
|
97 |
minor = Integer.parseInt(data);
|
|
98 |
} else if (line.startsWith("km-crandom:")) {
|
|
99 |
clientRandom = parse(data);
|
|
100 |
} else if (line.startsWith("km-srandom:")) {
|
|
101 |
serverRandom = parse(data);
|
|
102 |
} else if (line.startsWith("km-cipalg:")) {
|
|
103 |
cipherAlgorithm = data;
|
|
104 |
} else if (line.startsWith("km-keylen:")) {
|
|
105 |
keyLength = Integer.parseInt(data);
|
|
106 |
} else if (line.startsWith("km-explen:")) {
|
|
107 |
expandedKeyLength = Integer.parseInt(data);
|
|
108 |
} else if (line.startsWith("km-ivlen:")) {
|
|
109 |
ivLength = Integer.parseInt(data);
|
|
110 |
} else if (line.startsWith("km-maclen:")) {
|
|
111 |
macLength = Integer.parseInt(data);
|
|
112 |
} else if (line.startsWith("km-ccipkey:")) {
|
|
113 |
clientCipherBytes = parse(data);
|
|
114 |
} else if (line.startsWith("km-scipkey:")) {
|
|
115 |
serverCipherBytes = parse(data);
|
|
116 |
} else if (line.startsWith("km-civ:")) {
|
|
117 |
clientIv = parse(data);
|
|
118 |
} else if (line.startsWith("km-siv:")) {
|
|
119 |
serverIv = parse(data);
|
|
120 |
} else if (line.startsWith("km-cmackey:")) {
|
|
121 |
clientMacBytes = parse(data);
|
|
122 |
} else if (line.startsWith("km-smackey:")) {
|
|
123 |
serverMacBytes = parse(data);
|
|
124 |
|
|
125 |
System.out.print(".");
|
|
126 |
n++;
|
|
127 |
|
|
128 |
KeyGenerator kg = KeyGenerator.getInstance("SunTlsKeyMaterial", provider);
|
|
129 |
SecretKey masterKey = new SecretKeySpec(master, "TlsMasterSecret");
|
|
130 |
TlsKeyMaterialParameterSpec spec = new TlsKeyMaterialParameterSpec
|
|
131 |
(masterKey, major, minor, clientRandom, serverRandom, cipherAlgorithm,
|
|
132 |
keyLength, expandedKeyLength, ivLength, macLength);
|
|
133 |
|
|
134 |
kg.init(spec);
|
|
135 |
TlsKeyMaterialSpec result = (TlsKeyMaterialSpec)kg.generateKey();
|
|
136 |
match(lineNumber, clientCipherBytes, result.getClientCipherKey(), cipherAlgorithm);
|
|
137 |
match(lineNumber, serverCipherBytes, result.getServerCipherKey(), cipherAlgorithm);
|
|
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 stripParity(byte[] b) {
|
|
156 |
for (int i = 0; i < b.length; i++) {
|
|
157 |
b[i] &= 0xfe;
|
|
158 |
}
|
|
159 |
}
|
|
160 |
|
|
161 |
private static void match(int lineNumber, byte[] out, Object res, String cipherAlgorithm) throws Exception {
|
|
162 |
if ((out == null) || (res == null)) {
|
|
163 |
if (out != res) {
|
|
164 |
throw new Exception("null mismatch line " + lineNumber);
|
|
165 |
} else {
|
|
166 |
return;
|
|
167 |
}
|
|
168 |
}
|
|
169 |
byte[] b;
|
|
170 |
if (res instanceof SecretKey) {
|
|
171 |
b = ((SecretKey)res).getEncoded();
|
|
172 |
if (cipherAlgorithm.equalsIgnoreCase("DES") || cipherAlgorithm.equalsIgnoreCase("DESede")) {
|
|
173 |
// strip DES parity bits before comparision
|
|
174 |
stripParity(out);
|
|
175 |
stripParity(b);
|
|
176 |
}
|
|
177 |
} else if (res instanceof IvParameterSpec) {
|
|
178 |
b = ((IvParameterSpec)res).getIV();
|
|
179 |
} else {
|
|
180 |
throw new Exception(res.getClass().getName());
|
|
181 |
}
|
|
182 |
if (Arrays.equals(out, b) == false) {
|
|
183 |
System.out.println();
|
|
184 |
System.out.println("out: " + toString(out));
|
|
185 |
System.out.println("b: " + toString(b));
|
|
186 |
throw new Exception("mismatch line " + lineNumber);
|
|
187 |
}
|
|
188 |
}
|
|
189 |
|
|
190 |
}
|