TestMasterSecret.java revision 0:37a05a11f281
117680Spst/* 239297Sfenner * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. 317680Spst * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 417680Spst * 517680Spst * This code is free software; you can redistribute it and/or modify it 617680Spst * under the terms of the GNU General Public License version 2 only, as 717680Spst * published by the Free Software Foundation. 817680Spst * 917680Spst * This code is distributed in the hope that it will be useful, but WITHOUT 1017680Spst * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1117680Spst * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1217680Spst * version 2 for more details (a copy is included in the LICENSE file that 1317680Spst * accompanied this code). 1417680Spst * 1517680Spst * You should have received a copy of the GNU General Public License version 1617680Spst * 2 along with this work; if not, write to the Free Software Foundation, 1717680Spst * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1817680Spst * 1917680Spst * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 2017680Spst * CA 95054 USA or visit www.sun.com if you need additional information or 2117680Spst * have any questions. 2217680Spst */ 23127668Sbms 24127668Sbms/** 2517680Spst * @test 2617680Spst * @bug 6316539 2756893Sfenner * @summary Known-answer-test for TlsMasterSecret generator 2856893Sfenner * @author Andreas Sterbenz 2956893Sfenner * @library .. 3056893Sfenner */ 31127668Sbms 32127668Sbmsimport java.io.*; 3317680Spstimport java.util.*; 3417680Spst 3517680Spstimport java.security.Security; 3617680Spstimport java.security.Provider; 3717680Spst 3817680Spstimport javax.crypto.KeyGenerator; 3917680Spstimport javax.crypto.SecretKey; 4017680Spst 4117680Spstimport javax.crypto.spec.*; 4217680Spst 4317680Spstimport sun.security.internal.spec.*; 4417680Spstimport sun.security.internal.interfaces.TlsMasterSecret; 4517680Spst 4617680Spstpublic class TestMasterSecret extends PKCS11Test { 4717680Spst 4817680Spst private static int PREFIX_LENGTH = "m-premaster: ".length(); 4917680Spst 5017680Spst public static void main(String[] args) throws Exception { 5117680Spst main(new TestMasterSecret()); 5217680Spst } 5317680Spst 5417680Spst public void main(Provider provider) throws Exception { 5517680Spst if (provider.getService("KeyGenerator", "SunTlsMasterSecret") == null) { 5617680Spst System.out.println("Not supported by provider, skipping"); 5717680Spst return; 5817680Spst } 5917680Spst InputStream in = new FileInputStream(new File(BASE, "masterdata.txt")); 6017680Spst BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 6117680Spst 6217680Spst int n = 0; 6317680Spst int lineNumber = 0; 6417680Spst 6517680Spst String algorithm = null; 6617680Spst byte[] premaster = null; 6717680Spst byte[] clientRandom = null; 6817680Spst byte[] serverRandom = null; 6917680Spst int protoMajor = 0; 7017680Spst int protoMinor = 0; 7117680Spst int preMajor = 0; 7217680Spst int preMinor = 0; 7317680Spst byte[] master = null; 7417680Spst 7517680Spst while (true) { 7617680Spst String line = reader.readLine(); 7717680Spst lineNumber++; 7817680Spst if (line == null) { 7917680Spst break; 8017680Spst } 8117680Spst if (line.startsWith("m-") == false) { 8217680Spst continue; 8317680Spst } 8417680Spst String data = line.substring(PREFIX_LENGTH); 8517680Spst if (line.startsWith("m-algorithm:")) { 8617680Spst algorithm = data; 8717680Spst } else if (line.startsWith("m-premaster:")) { 8817680Spst premaster = parse(data); 8917680Spst } else if (line.startsWith("m-crandom:")) { 90127668Sbms clientRandom = parse(data); 91127668Sbms } else if (line.startsWith("m-srandom:")) { 9217680Spst serverRandom = parse(data); 9317680Spst } else if (line.startsWith("m-protomajor:")) { 9417680Spst protoMajor = Integer.parseInt(data); 9517680Spst } else if (line.startsWith("m-protominor:")) { 9617680Spst protoMinor = Integer.parseInt(data); 9717680Spst } else if (line.startsWith("m-premajor:")) { 9817680Spst preMajor = Integer.parseInt(data); 9917680Spst } else if (line.startsWith("m-preminor:")) { 10017680Spst preMinor = Integer.parseInt(data); 10117680Spst } else if (line.startsWith("m-master:")) { 10217680Spst master = parse(data); 10317680Spst 104127668Sbms System.out.print("."); 10517680Spst n++; 10617680Spst 10717680Spst KeyGenerator kg = KeyGenerator.getInstance("SunTlsMasterSecret", provider); 10817680Spst SecretKey premasterKey = new SecretKeySpec(premaster, algorithm); 10917680Spst TlsMasterSecretParameterSpec spec = new TlsMasterSecretParameterSpec 11017680Spst (premasterKey, protoMajor, protoMinor, clientRandom, serverRandom); 11117680Spst kg.init(spec); 11217680Spst TlsMasterSecret key = (TlsMasterSecret)kg.generateKey(); 11317680Spst byte[] enc = key.getEncoded(); 11475115Sfenner if (Arrays.equals(master, enc) == false) { 11575115Sfenner throw new Exception("mismatch line: " + lineNumber); 11675115Sfenner } 11775115Sfenner if ((preMajor != key.getMajorVersion()) || (preMinor != key.getMinorVersion())) { 11817680Spst throw new Exception("version mismatch line: " + lineNumber); 11975115Sfenner } 12075115Sfenner } else { 12117680Spst throw new Exception("Unknown line: " + line); 12217680Spst } 12375115Sfenner } 12475115Sfenner if (n == 0) { 12575115Sfenner throw new Exception("no tests"); 12675115Sfenner } 12775115Sfenner in.close(); 12875115Sfenner System.out.println(); 12975115Sfenner System.out.println("OK: " + n + " tests"); 13075115Sfenner } 13175115Sfenner 13275115Sfenner} 13375115Sfenner