0N/A/*
3002N/A * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A * @test
0N/A * @bug 6313661
0N/A * @summary Known-answer-test for TlsMasterSecret generator
0N/A * @author Andreas Sterbenz
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.util.*;
0N/A
0N/Aimport java.security.Security;
0N/Aimport java.security.Provider;
0N/A
0N/Aimport javax.crypto.KeyGenerator;
0N/Aimport javax.crypto.SecretKey;
0N/A
0N/Aimport javax.crypto.spec.*;
0N/A
0N/Aimport sun.security.internal.spec.*;
0N/Aimport sun.security.internal.interfaces.TlsMasterSecret;
0N/A
0N/Apublic class TestMasterSecret extends Utils {
0N/A
0N/A private static int PREFIX_LENGTH = "m-premaster: ".length();
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A Provider provider = Security.getProvider("SunJCE");
0N/A
0N/A InputStream in = new FileInputStream(new File(BASE, "masterdata.txt"));
0N/A BufferedReader reader = new BufferedReader(new InputStreamReader(in));
0N/A
0N/A int n = 0;
0N/A int lineNumber = 0;
0N/A
0N/A String algorithm = null;
0N/A byte[] premaster = null;
0N/A byte[] clientRandom = null;
0N/A byte[] serverRandom = null;
0N/A int protoMajor = 0;
0N/A int protoMinor = 0;
0N/A int preMajor = 0;
0N/A int preMinor = 0;
0N/A byte[] master = null;
0N/A
0N/A while (true) {
0N/A String line = reader.readLine();
0N/A lineNumber++;
0N/A if (line == null) {
0N/A break;
0N/A }
0N/A if (line.startsWith("m-") == false) {
0N/A continue;
0N/A }
0N/A String data = line.substring(PREFIX_LENGTH);
0N/A if (line.startsWith("m-algorithm:")) {
0N/A algorithm = data;
0N/A } else if (line.startsWith("m-premaster:")) {
0N/A premaster = parse(data);
0N/A } else if (line.startsWith("m-crandom:")) {
0N/A clientRandom = parse(data);
0N/A } else if (line.startsWith("m-srandom:")) {
0N/A serverRandom = parse(data);
0N/A } else if (line.startsWith("m-protomajor:")) {
0N/A protoMajor = Integer.parseInt(data);
0N/A } else if (line.startsWith("m-protominor:")) {
0N/A protoMinor = Integer.parseInt(data);
0N/A } else if (line.startsWith("m-premajor:")) {
0N/A preMajor = Integer.parseInt(data);
0N/A } else if (line.startsWith("m-preminor:")) {
0N/A preMinor = Integer.parseInt(data);
0N/A } else if (line.startsWith("m-master:")) {
0N/A master = parse(data);
0N/A
0N/A System.out.print(".");
0N/A n++;
0N/A
3002N/A KeyGenerator kg =
3002N/A KeyGenerator.getInstance("SunTlsMasterSecret", provider);
3002N/A SecretKey premasterKey =
3002N/A new SecretKeySpec(premaster, algorithm);
3002N/A TlsMasterSecretParameterSpec spec =
3002N/A new TlsMasterSecretParameterSpec(premasterKey, protoMajor,
3002N/A protoMinor, clientRandom, serverRandom,
3002N/A null, -1, -1);
0N/A kg.init(spec);
0N/A TlsMasterSecret key = (TlsMasterSecret)kg.generateKey();
0N/A byte[] enc = key.getEncoded();
0N/A if (Arrays.equals(master, enc) == false) {
0N/A throw new Exception("mismatch line: " + lineNumber);
0N/A }
3002N/A if ((preMajor != key.getMajorVersion()) ||
3002N/A (preMinor != key.getMinorVersion())) {
0N/A throw new Exception("version mismatch line: " + lineNumber);
0N/A }
0N/A } else {
0N/A throw new Exception("Unknown line: " + line);
0N/A }
0N/A }
0N/A if (n == 0) {
0N/A throw new Exception("no tests");
0N/A }
0N/A in.close();
0N/A System.out.println();
0N/A System.out.println("OK: " + n + " tests");
0N/A }
0N/A
0N/A}