2
|
1 |
/*
|
7043
|
2 |
* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
|
2
|
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 |
*
|
5506
|
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.
|
2
|
22 |
*/
|
|
23 |
|
|
24 |
/**
|
|
25 |
* @test
|
|
26 |
* @bug 6316539
|
|
27 |
* @summary Known-answer-test for TlsMasterSecret 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 |
import sun.security.internal.interfaces.TlsMasterSecret;
|
|
45 |
|
|
46 |
public class TestMasterSecret extends PKCS11Test {
|
|
47 |
|
|
48 |
private static int PREFIX_LENGTH = "m-premaster: ".length();
|
|
49 |
|
|
50 |
public static void main(String[] args) throws Exception {
|
|
51 |
main(new TestMasterSecret());
|
|
52 |
}
|
|
53 |
|
|
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 |
InputStream in = new FileInputStream(new File(BASE, "masterdata.txt"));
|
|
60 |
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
|
|
61 |
|
|
62 |
int n = 0;
|
|
63 |
int lineNumber = 0;
|
|
64 |
|
|
65 |
String algorithm = null;
|
|
66 |
byte[] premaster = null;
|
|
67 |
byte[] clientRandom = null;
|
|
68 |
byte[] serverRandom = null;
|
|
69 |
int protoMajor = 0;
|
|
70 |
int protoMinor = 0;
|
|
71 |
int preMajor = 0;
|
|
72 |
int preMinor = 0;
|
|
73 |
byte[] master = null;
|
|
74 |
|
|
75 |
while (true) {
|
|
76 |
String line = reader.readLine();
|
|
77 |
lineNumber++;
|
|
78 |
if (line == null) {
|
|
79 |
break;
|
|
80 |
}
|
|
81 |
if (line.startsWith("m-") == false) {
|
|
82 |
continue;
|
|
83 |
}
|
|
84 |
String data = line.substring(PREFIX_LENGTH);
|
|
85 |
if (line.startsWith("m-algorithm:")) {
|
|
86 |
algorithm = data;
|
|
87 |
} else if (line.startsWith("m-premaster:")) {
|
|
88 |
premaster = parse(data);
|
|
89 |
} else if (line.startsWith("m-crandom:")) {
|
|
90 |
clientRandom = parse(data);
|
|
91 |
} else if (line.startsWith("m-srandom:")) {
|
|
92 |
serverRandom = parse(data);
|
|
93 |
} else if (line.startsWith("m-protomajor:")) {
|
|
94 |
protoMajor = Integer.parseInt(data);
|
|
95 |
} else if (line.startsWith("m-protominor:")) {
|
|
96 |
protoMinor = Integer.parseInt(data);
|
|
97 |
} else if (line.startsWith("m-premajor:")) {
|
|
98 |
preMajor = Integer.parseInt(data);
|
|
99 |
} else if (line.startsWith("m-preminor:")) {
|
|
100 |
preMinor = Integer.parseInt(data);
|
|
101 |
} else if (line.startsWith("m-master:")) {
|
|
102 |
master = parse(data);
|
|
103 |
|
|
104 |
System.out.print(".");
|
|
105 |
n++;
|
|
106 |
|
7043
|
107 |
KeyGenerator kg =
|
|
108 |
KeyGenerator.getInstance("SunTlsMasterSecret", provider);
|
|
109 |
SecretKey premasterKey =
|
|
110 |
new SecretKeySpec(premaster, algorithm);
|
|
111 |
TlsMasterSecretParameterSpec spec =
|
|
112 |
new TlsMasterSecretParameterSpec(premasterKey,
|
|
113 |
protoMajor, protoMinor, clientRandom, serverRandom,
|
|
114 |
null, -1, -1);
|
2
|
115 |
kg.init(spec);
|
|
116 |
TlsMasterSecret key = (TlsMasterSecret)kg.generateKey();
|
|
117 |
byte[] enc = key.getEncoded();
|
|
118 |
if (Arrays.equals(master, enc) == false) {
|
|
119 |
throw new Exception("mismatch line: " + lineNumber);
|
|
120 |
}
|
7043
|
121 |
if ((preMajor != key.getMajorVersion()) ||
|
|
122 |
(preMinor != key.getMinorVersion())) {
|
2
|
123 |
throw new Exception("version mismatch line: " + lineNumber);
|
|
124 |
}
|
|
125 |
} else {
|
|
126 |
throw new Exception("Unknown line: " + line);
|
|
127 |
}
|
|
128 |
}
|
|
129 |
if (n == 0) {
|
|
130 |
throw new Exception("no tests");
|
|
131 |
}
|
|
132 |
in.close();
|
|
133 |
System.out.println();
|
|
134 |
System.out.println("OK: " + n + " tests");
|
|
135 |
}
|
|
136 |
|
|
137 |
}
|