TestMasterSecret.java revision 13549:88a7d9ea4ae2
1/*
2 * Copyright (c) 2005, 2016, 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 6316539
27 * @summary Known-answer-test for TlsMasterSecret generator
28 * @author Andreas Sterbenz
29 * @library ..
30 * @modules java.base/sun.security.internal.interfaces
31 *          java.base/sun.security.internal.spec
32 * @run main/othervm TestMasterSecret
33 * @run main/othervm TestMasterSecret sm TestMasterSecret.policy
34 */
35
36import java.io.BufferedReader;
37import java.nio.file.Files;
38import java.nio.file.Paths;
39import java.security.Provider;
40import java.util.Arrays;
41import javax.crypto.KeyGenerator;
42import javax.crypto.SecretKey;
43import javax.crypto.spec.SecretKeySpec;
44import sun.security.internal.interfaces.TlsMasterSecret;
45import sun.security.internal.spec.TlsMasterSecretParameterSpec;
46
47public class TestMasterSecret extends PKCS11Test {
48
49    private static final int PREFIX_LENGTH = "m-premaster:  ".length();
50
51    public static void main(String[] args) throws Exception {
52        main(new TestMasterSecret(), args);
53    }
54
55    @Override
56    public void main(Provider provider) throws Exception {
57        if (provider.getService("KeyGenerator", "SunTlsMasterSecret") == null) {
58            System.out.println("Not supported by provider, skipping");
59            return;
60        }
61
62        try (BufferedReader reader = Files.newBufferedReader(
63                Paths.get(BASE, "masterdata.txt"))) {
64
65            int n = 0;
66            int lineNumber = 0;
67
68            String algorithm = null;
69            byte[] premaster = null;
70            byte[] clientRandom = null;
71            byte[] serverRandom = null;
72            int protoMajor = 0;
73            int protoMinor = 0;
74            int preMajor = 0;
75            int preMinor = 0;
76            byte[] master = null;
77
78            while (true) {
79                String line = reader.readLine();
80                lineNumber++;
81                if (line == null) {
82                    break;
83                }
84                if (line.startsWith("m-") == false) {
85                    continue;
86                }
87                String data = line.substring(PREFIX_LENGTH);
88                if (line.startsWith("m-algorithm:")) {
89                    algorithm = data;
90                } else if (line.startsWith("m-premaster:")) {
91                    premaster = parse(data);
92                } else if (line.startsWith("m-crandom:")) {
93                    clientRandom = parse(data);
94                } else if (line.startsWith("m-srandom:")) {
95                    serverRandom = parse(data);
96                } else if (line.startsWith("m-protomajor:")) {
97                    protoMajor = Integer.parseInt(data);
98                } else if (line.startsWith("m-protominor:")) {
99                    protoMinor = Integer.parseInt(data);
100                } else if (line.startsWith("m-premajor:")) {
101                    preMajor = Integer.parseInt(data);
102                } else if (line.startsWith("m-preminor:")) {
103                    preMinor = Integer.parseInt(data);
104                } else if (line.startsWith("m-master:")) {
105                    master = parse(data);
106
107                    System.out.print(".");
108                    n++;
109
110                    KeyGenerator kg =
111                        KeyGenerator.getInstance("SunTlsMasterSecret", provider);
112                    SecretKey premasterKey =
113                        new SecretKeySpec(premaster, algorithm);
114                    TlsMasterSecretParameterSpec spec =
115                        new TlsMasterSecretParameterSpec(premasterKey,
116                            protoMajor, protoMinor, clientRandom, serverRandom,
117                            null, -1, -1);
118                    kg.init(spec);
119                    TlsMasterSecret key = (TlsMasterSecret)kg.generateKey();
120                    byte[] enc = key.getEncoded();
121                    if (Arrays.equals(master, enc) == false) {
122                        throw new Exception("mismatch line: " + lineNumber);
123                    }
124                    if ((preMajor != key.getMajorVersion()) ||
125                            (preMinor != key.getMinorVersion())) {
126                        throw new Exception("version mismatch line: " + lineNumber);
127                    }
128                } else {
129                    throw new Exception("Unknown line: " + line);
130                }
131            }
132            if (n == 0) {
133                throw new Exception("no tests");
134            }
135            System.out.println();
136            System.out.println("OK: " + n + " tests");
137        }
138    }
139
140}
141