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 6316539 6345251
0N/A * @summary Basic known-answer-test for TlsPrf
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 TestPRF extends PKCS11Test {
0N/A
0N/A private static int PREFIX_LENGTH = "prf-output: ".length();
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A main(new TestPRF());
0N/A }
0N/A
0N/A public void main(Provider provider) throws Exception {
0N/A if (provider.getService("KeyGenerator", "SunTlsPrf") == 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, "prfdata.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[] secret = null;
0N/A String label = null;
0N/A byte[] seed = null;
0N/A int length = 0;
0N/A byte[] output = 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("prf-") == false) {
0N/A continue;
0N/A }
0N/A
0N/A String data = line.substring(PREFIX_LENGTH);
0N/A if (line.startsWith("prf-secret:")) {
0N/A secret = parse(data);
0N/A } else if (line.startsWith("prf-label:")) {
0N/A label = data;
0N/A } else if (line.startsWith("prf-seed:")) {
0N/A seed = parse(data);
0N/A } else if (line.startsWith("prf-length:")) {
0N/A length = Integer.parseInt(data);
0N/A } else if (line.startsWith("prf-output:")) {
0N/A output = parse(data);
0N/A
0N/A System.out.print(".");
0N/A n++;
0N/A
3002N/A KeyGenerator kg =
3002N/A KeyGenerator.getInstance("SunTlsPrf", provider);
0N/A SecretKey inKey;
0N/A if (secret == null) {
0N/A inKey = null;
0N/A } else {
0N/A inKey = new SecretKeySpec(secret, "Generic");
0N/A }
3002N/A TlsPrfParameterSpec spec =
3002N/A new TlsPrfParameterSpec(inKey, label, seed, length,
3002N/A null, -1, -1);
0N/A SecretKey key;
0N/A try {
0N/A kg.init(spec);
0N/A key = kg.generateKey();
0N/A } catch (Exception e) {
0N/A if (secret == null) {
0N/A // This fails on Solaris, but since we never call this
0N/A // API for this case in JSSE, ignore the failure.
3002N/A // (SunJSSE uses the CKM_TLS_KEY_AND_MAC_DERIVE
3002N/A // mechanism)
0N/A System.out.print("X");
0N/A continue;
0N/A }
0N/A System.out.println();
0N/A throw new Exception("Error on line: " + lineNumber, e);
0N/A }
0N/A byte[] enc = key.getEncoded();
0N/A if (Arrays.equals(output, enc) == false) {
0N/A System.out.println();
0N/A System.out.println("expected: " + toString(output));
0N/A System.out.println("actual: " + toString(enc));
0N/A throw new Exception("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}