blob: b06e16469bca3aca40cee7442b67a1cbedbae1a7 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
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 TlsMasterSecret generator
28 * @author Andreas Sterbenz
29 * @library ..
30 */
31
32import java.io.*;
33import java.util.*;
34
35import java.security.Security;
36import java.security.Provider;
37
38import javax.crypto.KeyGenerator;
39import javax.crypto.SecretKey;
40
41import javax.crypto.spec.*;
42
43import sun.security.internal.spec.*;
44import sun.security.internal.interfaces.TlsMasterSecret;
45
46public 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
107 KeyGenerator kg = KeyGenerator.getInstance("SunTlsMasterSecret", provider);
108 SecretKey premasterKey = new SecretKeySpec(premaster, algorithm);
109 TlsMasterSecretParameterSpec spec = new TlsMasterSecretParameterSpec
110 (premasterKey, protoMajor, protoMinor, clientRandom, serverRandom);
111 kg.init(spec);
112 TlsMasterSecret key = (TlsMasterSecret)kg.generateKey();
113 byte[] enc = key.getEncoded();
114 if (Arrays.equals(master, enc) == false) {
115 throw new Exception("mismatch line: " + lineNumber);
116 }
117 if ((preMajor != key.getMajorVersion()) || (preMinor != key.getMinorVersion())) {
118 throw new Exception("version mismatch line: " + lineNumber);
119 }
120 } else {
121 throw new Exception("Unknown line: " + line);
122 }
123 }
124 if (n == 0) {
125 throw new Exception("no tests");
126 }
127 in.close();
128 System.out.println();
129 System.out.println("OK: " + n + " tests");
130 }
131
132}