TestKeyMaterial.java revision 0
0N/A/*
0N/A * Copyright 2005 Sun Microsystems, Inc. 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 *
0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
0N/A * CA 95054 USA or visit www.sun.com if you need additional information or
0N/A * have any questions.
0N/A */
0N/A
0N/A/**
0N/A * @test
0N/A * @bug 6316539
0N/A * @summary Known-answer-test for TlsKeyMaterial generator
0N/A * @author Andreas Sterbenz
0N/A * @library ..
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 PKCS11Test {
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 main(new TestKeyMaterial());
0N/A }
0N/A
0N/A public void main(Provider provider) throws Exception {
0N/A if (provider.getService("KeyGenerator", "SunTlsKeyMaterial") == null) {
0N/A System.out.println("Provider does not support algorithm, skipping");
0N/A return;
0N/A }
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
0N/A KeyGenerator kg = KeyGenerator.getInstance("SunTlsKeyMaterial", provider);
0N/A SecretKey masterKey = new SecretKeySpec(master, "TlsMasterSecret");
0N/A TlsKeyMaterialParameterSpec spec = new TlsKeyMaterialParameterSpec
0N/A (masterKey, major, minor, clientRandom, serverRandom, cipherAlgorithm,
0N/A keyLength, expandedKeyLength, ivLength, macLength);
0N/A
0N/A kg.init(spec);
0N/A TlsKeyMaterialSpec result = (TlsKeyMaterialSpec)kg.generateKey();
0N/A match(lineNumber, clientCipherBytes, result.getClientCipherKey(), cipherAlgorithm);
0N/A match(lineNumber, serverCipherBytes, result.getServerCipherKey(), cipherAlgorithm);
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
0N/A private static void stripParity(byte[] b) {
0N/A for (int i = 0; i < b.length; i++) {
0N/A b[i] &= 0xfe;
0N/A }
0N/A }
0N/A
0N/A private static void match(int lineNumber, byte[] out, Object res, String cipherAlgorithm) 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 if (cipherAlgorithm.equalsIgnoreCase("DES") || cipherAlgorithm.equalsIgnoreCase("DESede")) {
0N/A // strip DES parity bits before comparision
0N/A stripParity(out);
0N/A stripParity(b);
0N/A }
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 System.out.println();
0N/A System.out.println("out: " + toString(out));
0N/A System.out.println("b: " + toString(b));
0N/A throw new Exception("mismatch line " + lineNumber);
0N/A }
0N/A }
0N/A
0N/A}