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  */
  30 
  31 import java.io.*;
  32 import java.util.*;
  33 
  34 import java.security.Security;
  35 import java.security.Provider;
  36 
  37 import javax.crypto.KeyGenerator;
  38 import javax.crypto.SecretKey;
  39 
  40 import javax.crypto.spec.*;
  41 
  42 import sun.security.internal.spec.*;
  43 import sun.security.internal.interfaces.TlsMasterSecret;
  44 
  45 public class TestMasterSecret extends Utils {
  46 
  47     private static int PREFIX_LENGTH = "m-premaster:  ".length();
  48 
  49     public static void main(String[] args) throws Exception {
  50         Provider provider = Security.getProvider("SunJCE");
  51 
  52         InputStream in = new FileInputStream(new File(BASE, "masterdata.txt"));
  53         BufferedReader reader = new BufferedReader(new InputStreamReader(in));
  54 
  55         int n = 0;
  56         int lineNumber = 0;
  57 
  58         String algorithm = null;
  59         byte[] premaster = null;
  60         byte[] clientRandom = null;
  61         byte[] serverRandom = null;
  62         int protoMajor = 0;
  63         int protoMinor = 0;
  64         int preMajor = 0;
  65         int preMinor = 0;
  66         byte[] master = null;
  67 
  68         while (true) {
  69             String line = reader.readLine();
  70             lineNumber++;
  71             if (line == null) {
  72                 break;
  73             }
  74             if (line.startsWith("m-") == false) {
  75                 continue;
  76             }
  77             String data = line.substring(PREFIX_LENGTH);
  78             if (line.startsWith("m-algorithm:")) {
  79                 algorithm = data;
  80             } else if (line.startsWith("m-premaster:")) {
  81                 premaster = parse(data);
  82             } else if (line.startsWith("m-crandom:")) {
  83                 clientRandom = parse(data);
  84             } else if (line.startsWith("m-srandom:")) {
  85                 serverRandom = parse(data);
  86             } else if (line.startsWith("m-protomajor:")) {
  87                 protoMajor = Integer.parseInt(data);
  88             } else if (line.startsWith("m-protominor:")) {
  89                 protoMinor = Integer.parseInt(data);
  90             } else if (line.startsWith("m-premajor:")) {
  91                 preMajor = Integer.parseInt(data);
  92             } else if (line.startsWith("m-preminor:")) {
  93                 preMinor = Integer.parseInt(data);
  94             } else if (line.startsWith("m-master:")) {
  95                 master = parse(data);
  96 
  97                 System.out.print(".");
  98                 n++;
  99 
 100                 KeyGenerator kg =
 101                     KeyGenerator.getInstance("SunTlsMasterSecret", provider);
 102                 SecretKey premasterKey =
 103                     new SecretKeySpec(premaster, algorithm);
 104                 TlsMasterSecretParameterSpec spec =
 105                     new TlsMasterSecretParameterSpec(premasterKey, protoMajor,
 106                         protoMinor, clientRandom, serverRandom,
 107                         null, -1, -1);
 108                 kg.init(spec);
 109                 TlsMasterSecret key = (TlsMasterSecret)kg.generateKey();
 110                 byte[] enc = key.getEncoded();
 111                 if (Arrays.equals(master, enc) == false) {
 112                     throw new Exception("mismatch line: " + lineNumber);
 113                 }
 114                 if ((preMajor != key.getMajorVersion()) ||
 115                         (preMinor != key.getMinorVersion())) {
 116                     throw new Exception("version mismatch line: " + lineNumber);
 117                 }
 118             } else {
 119                 throw new Exception("Unknown line: " + line);
 120             }
 121         }
 122         if (n == 0) {
 123             throw new Exception("no tests");
 124         }
 125         in.close();
 126         System.out.println();
 127         System.out.println("OK: " + n + " tests");
 128     }
 129 
 130 }