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 TlsKeyMaterial 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/A
0N/Apublic class TestKeyMaterial extends Utils {
0N/A
0N/A private static int PREFIX_LENGTH = "km-master: ".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, "keymatdata.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 byte[] master = null;
0N/A int major = 0;
0N/A int minor = 0;
0N/A byte[] clientRandom = null;
0N/A byte[] serverRandom = null;
0N/A String cipherAlgorithm = null;
0N/A int keyLength = 0;
0N/A int expandedKeyLength = 0;
0N/A int ivLength = 0;
0N/A int macLength = 0;
0N/A byte[] clientCipherBytes = null;
0N/A byte[] serverCipherBytes = null;
0N/A byte[] clientIv = null;
0N/A byte[] serverIv = null;
0N/A byte[] clientMacBytes = null;
0N/A byte[] serverMacBytes = 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("km-") == false) {
0N/A continue;
0N/A }
0N/A String data = line.substring(PREFIX_LENGTH);
0N/A if (line.startsWith("km-master:")) {
0N/A master = parse(data);
0N/A } else if (line.startsWith("km-major:")) {
0N/A major = Integer.parseInt(data);
0N/A } else if (line.startsWith("km-minor:")) {
0N/A minor = Integer.parseInt(data);
0N/A } else if (line.startsWith("km-crandom:")) {
0N/A clientRandom = parse(data);
0N/A } else if (line.startsWith("km-srandom:")) {
0N/A serverRandom = parse(data);
0N/A } else if (line.startsWith("km-cipalg:")) {
0N/A cipherAlgorithm = data;
0N/A } else if (line.startsWith("km-keylen:")) {
0N/A keyLength = Integer.parseInt(data);
0N/A } else if (line.startsWith("km-explen:")) {
0N/A expandedKeyLength = Integer.parseInt(data);
0N/A } else if (line.startsWith("km-ivlen:")) {
0N/A ivLength = Integer.parseInt(data);
0N/A } else if (line.startsWith("km-maclen:")) {
0N/A macLength = Integer.parseInt(data);
0N/A } else if (line.startsWith("km-ccipkey:")) {
0N/A clientCipherBytes = parse(data);
0N/A } else if (line.startsWith("km-scipkey:")) {
0N/A serverCipherBytes = parse(data);
0N/A } else if (line.startsWith("km-civ:")) {
0N/A clientIv = parse(data);
0N/A } else if (line.startsWith("km-siv:")) {
0N/A serverIv = parse(data);
0N/A } else if (line.startsWith("km-cmackey:")) {
0N/A clientMacBytes = parse(data);
0N/A } else if (line.startsWith("km-smackey:")) {
0N/A serverMacBytes = parse(data);
0N/A
0N/A System.out.print(".");
0N/A n++;
0N/A
3002N/A KeyGenerator kg =
3002N/A KeyGenerator.getInstance("SunTlsKeyMaterial", provider);
3002N/A SecretKey masterKey =
3002N/A new SecretKeySpec(master, "TlsMasterSecret");
3002N/A TlsKeyMaterialParameterSpec spec =
3002N/A new TlsKeyMaterialParameterSpec(masterKey, major, minor,
3002N/A clientRandom, serverRandom, cipherAlgorithm,
3002N/A keyLength, expandedKeyLength, ivLength, macLength,
3002N/A null, -1, -1);
0N/A
0N/A kg.init(spec);
3002N/A TlsKeyMaterialSpec result =
3002N/A (TlsKeyMaterialSpec)kg.generateKey();
3002N/A match(lineNumber, clientCipherBytes,
3002N/A result.getClientCipherKey());
3002N/A match(lineNumber, serverCipherBytes,
3002N/A result.getServerCipherKey());
0N/A match(lineNumber, clientIv, result.getClientIv());
0N/A match(lineNumber, serverIv, result.getServerIv());
0N/A match(lineNumber, clientMacBytes, result.getClientMacKey());
0N/A match(lineNumber, serverMacBytes, result.getServerMacKey());
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
3002N/A private static void match(int lineNumber, byte[] out, Object res)
3002N/A throws Exception {
0N/A if ((out == null) || (res == null)) {
0N/A if (out != res) {
0N/A throw new Exception("null mismatch line " + lineNumber);
0N/A } else {
0N/A return;
0N/A }
0N/A }
0N/A byte[] b;
0N/A if (res instanceof SecretKey) {
0N/A b = ((SecretKey)res).getEncoded();
0N/A } else if (res instanceof IvParameterSpec) {
0N/A b = ((IvParameterSpec)res).getIV();
0N/A } else {
0N/A throw new Exception(res.getClass().getName());
0N/A }
0N/A if (Arrays.equals(out, b) == false) {
0N/A throw new Exception("mismatch line " + lineNumber);
0N/A }
0N/A }
0N/A
0N/A}