TestPRF.java revision 16554:ccf1ccb7adf9
155682Smarkm/* 255682Smarkm * Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved. 355682Smarkm * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 455682Smarkm * 555682Smarkm * This code is free software; you can redistribute it and/or modify it 655682Smarkm * under the terms of the GNU General Public License version 2 only, as 755682Smarkm * published by the Free Software Foundation. 855682Smarkm * 955682Smarkm * This code is distributed in the hope that it will be useful, but WITHOUT 1055682Smarkm * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1155682Smarkm * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1255682Smarkm * version 2 for more details (a copy is included in the LICENSE file that 13178825Sdfr * accompanied this code). 1455682Smarkm * 1555682Smarkm * You should have received a copy of the GNU General Public License version 1655682Smarkm * 2 along with this work; if not, write to the Free Software Foundation, 1755682Smarkm * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1855682Smarkm * 1955682Smarkm * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2055682Smarkm * or visit www.oracle.com if you need additional information or have any 2155682Smarkm * questions. 2255682Smarkm */ 2355682Smarkm 2455682Smarkm/* 2555682Smarkm * @test 2655682Smarkm * @bug 6316539 6345251 2755682Smarkm * @summary Basic known-answer-test for TlsPrf 2855682Smarkm * @author Andreas Sterbenz 2955682Smarkm * @library .. 3055682Smarkm * @modules java.base/sun.security.internal.spec 3155682Smarkm * jdk.crypto.cryptoki 3255682Smarkm * @run main/othervm TestPRF 3355682Smarkm * @run main/othervm TestPRF sm policy 3455682Smarkm */ 3555682Smarkm 3655682Smarkmimport java.io.BufferedReader; 3755682Smarkmimport java.nio.file.Files; 38178825Sdfrimport java.nio.file.Paths; 3955682Smarkmimport java.security.Provider; 4055682Smarkmimport java.util.Arrays; 4155682Smarkmimport javax.crypto.KeyGenerator; 4255682Smarkmimport javax.crypto.SecretKey; 4355682Smarkmimport javax.crypto.spec.SecretKeySpec; 4455682Smarkmimport sun.security.internal.spec.TlsPrfParameterSpec; 4555682Smarkm 4655682Smarkmpublic class TestPRF extends PKCS11Test { 4755682Smarkm 4855682Smarkm private static final int PREFIX_LENGTH = "prf-output: ".length(); 4955682Smarkm 5055682Smarkm public static void main(String[] args) throws Exception { 5155682Smarkm main(new TestPRF(), args); 5255682Smarkm } 5355682Smarkm 54178825Sdfr @Override 5555682Smarkm public void main(Provider provider) throws Exception { 5655682Smarkm if (provider.getService("KeyGenerator", "SunTlsPrf") == null) { 5755682Smarkm System.out.println("Provider does not support algorithm, skipping"); 5855682Smarkm return; 5955682Smarkm } 6055682Smarkm 6155682Smarkm try (BufferedReader reader = Files.newBufferedReader( 6255682Smarkm Paths.get(BASE, "prfdata.txt"))) { 6355682Smarkm 6455682Smarkm int n = 0; 6555682Smarkm int lineNumber = 0; 6655682Smarkm 6755682Smarkm byte[] secret = null; 6855682Smarkm String label = null; 6955682Smarkm byte[] seed = null; 7055682Smarkm int length = 0; 7155682Smarkm byte[] output = null; 7255682Smarkm 7355682Smarkm while (true) { 7455682Smarkm String line = reader.readLine(); 7555682Smarkm lineNumber++; 7655682Smarkm if (line == null) { 7755682Smarkm break; 7855682Smarkm } 7955682Smarkm if (line.startsWith("prf-") == false) { 8055682Smarkm continue; 8155682Smarkm } 8255682Smarkm 8355682Smarkm String data = line.substring(PREFIX_LENGTH); 8455682Smarkm if (line.startsWith("prf-secret:")) { 85 secret = parse(data); 86 } else if (line.startsWith("prf-label:")) { 87 label = data; 88 } else if (line.startsWith("prf-seed:")) { 89 seed = parse(data); 90 } else if (line.startsWith("prf-length:")) { 91 length = Integer.parseInt(data); 92 } else if (line.startsWith("prf-output:")) { 93 output = parse(data); 94 95 System.out.print("."); 96 n++; 97 98 KeyGenerator kg = 99 KeyGenerator.getInstance("SunTlsPrf", provider); 100 SecretKey inKey; 101 if (secret == null) { 102 inKey = null; 103 } else { 104 inKey = new SecretKeySpec(secret, "Generic"); 105 } 106 TlsPrfParameterSpec spec = 107 new TlsPrfParameterSpec(inKey, label, seed, length, 108 null, -1, -1); 109 SecretKey key; 110 try { 111 kg.init(spec); 112 key = kg.generateKey(); 113 } catch (Exception e) { 114 if (secret == null) { 115 // This fails on Solaris, but since we never call this 116 // API for this case in JSSE, ignore the failure. 117 // (SunJSSE uses the CKM_TLS_KEY_AND_MAC_DERIVE 118 // mechanism) 119 System.out.print("X"); 120 continue; 121 } 122 System.out.println(); 123 throw new Exception("Error on line: " + lineNumber, e); 124 } 125 byte[] enc = key.getEncoded(); 126 if (Arrays.equals(output, enc) == false) { 127 System.out.println(); 128 System.out.println("expected: " + toString(output)); 129 System.out.println("actual: " + toString(enc)); 130 throw new Exception("mismatch line: " + lineNumber); 131 } 132 } else { 133 throw new Exception("Unknown line: " + line); 134 } 135 } 136 if (n == 0) { 137 throw new Exception("no tests"); 138 } 139 System.out.println(); 140 System.out.println("OK: " + n + " tests"); 141 } 142 } 143 144} 145