1 /* 2 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /** 25 * @test 26 * @bug 6313661 27 * @summary Known-answer-test for TlsMasterSecret generator 28 * @author Andreas Sterbenz 29 * @modules java.base/sun.security.internal.interfaces 30 * java.base/sun.security.internal.spec 31 */ 32 33 import java.io.*; 34 import java.util.*; 35 36 import java.security.Security; 37 import java.security.Provider; 38 39 import javax.crypto.KeyGenerator; 40 import javax.crypto.SecretKey; 41 42 import javax.crypto.spec.*; 43 44 import sun.security.internal.spec.*; 45 import sun.security.internal.interfaces.TlsMasterSecret; 46 47 public class TestMasterSecret extends Utils { 48 49 private static int PREFIX_LENGTH = "m-premaster: ".length(); 50 51 public static void main(String[] args) throws Exception { 52 Provider provider = Security.getProvider("SunJCE"); 53 54 InputStream in = new FileInputStream(new File(BASE, "masterdata.txt")); 55 BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 56 57 int n = 0; 58 int lineNumber = 0; 59 60 String algorithm = null; 61 byte[] premaster = null; 62 byte[] clientRandom = null; 63 byte[] serverRandom = null; 64 int protoMajor = 0; 65 int protoMinor = 0; 66 int preMajor = 0; 67 int preMinor = 0; 68 byte[] master = null; 69 70 while (true) { 71 String line = reader.readLine(); 72 lineNumber++; 73 if (line == null) { 74 break; 75 } 76 if (line.startsWith("m-") == false) { 77 continue; 78 } 79 String data = line.substring(PREFIX_LENGTH); 80 if (line.startsWith("m-algorithm:")) { 81 algorithm = data; 82 } else if (line.startsWith("m-premaster:")) { 83 premaster = parse(data); 84 } else if (line.startsWith("m-crandom:")) { 85 clientRandom = parse(data); 86 } else if (line.startsWith("m-srandom:")) { 87 serverRandom = parse(data); 88 } else if (line.startsWith("m-protomajor:")) { 89 protoMajor = Integer.parseInt(data); 90 } else if (line.startsWith("m-protominor:")) { 91 protoMinor = Integer.parseInt(data); 92 } else if (line.startsWith("m-premajor:")) { 93 preMajor = Integer.parseInt(data); 94 } else if (line.startsWith("m-preminor:")) { 95 preMinor = Integer.parseInt(data); 96 } else if (line.startsWith("m-master:")) { 97 master = parse(data); 98 99 System.out.print("."); 100 n++; 101 102 KeyGenerator kg = 103 KeyGenerator.getInstance("SunTlsMasterSecret", provider); 104 SecretKey premasterKey = 105 new SecretKeySpec(premaster, algorithm); 106 TlsMasterSecretParameterSpec spec = 107 new TlsMasterSecretParameterSpec(premasterKey, protoMajor, 108 protoMinor, clientRandom, serverRandom, 109 null, -1, -1); 110 kg.init(spec); 111 TlsMasterSecret key = (TlsMasterSecret)kg.generateKey(); 112 byte[] enc = key.getEncoded(); 113 if (Arrays.equals(master, enc) == false) { 114 throw new Exception("mismatch line: " + lineNumber); 115 } 116 if ((preMajor != key.getMajorVersion()) || 117 (preMinor != key.getMinorVersion())) { 118 throw new Exception("version mismatch line: " + lineNumber); 119 } 120 } else { 121 throw new Exception("Unknown line: " + line); 122 } 123 } 124 if (n == 0) { 125 throw new Exception("no tests"); 126 } 127 in.close(); 128 System.out.println(); 129 System.out.println("OK: " + n + " tests"); 130 } 131 132 }