Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/crypto/provider/TLS/TestMasterSecret.java
38867 views
/*1* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 631366126* @summary Known-answer-test for TlsMasterSecret generator27* @author Andreas Sterbenz28*/2930import java.io.*;31import java.util.*;3233import java.security.Security;34import java.security.Provider;3536import javax.crypto.KeyGenerator;37import javax.crypto.SecretKey;3839import javax.crypto.spec.*;4041import sun.security.internal.spec.*;42import sun.security.internal.interfaces.TlsMasterSecret;4344public class TestMasterSecret extends Utils {4546private static int PREFIX_LENGTH = "m-premaster: ".length();4748public static void main(String[] args) throws Exception {49Provider provider = Security.getProvider("SunJCE");5051InputStream in = new FileInputStream(new File(BASE, "masterdata.txt"));52BufferedReader reader = new BufferedReader(new InputStreamReader(in));5354int n = 0;55int lineNumber = 0;5657String algorithm = null;58byte[] premaster = null;59byte[] clientRandom = null;60byte[] serverRandom = null;61int protoMajor = 0;62int protoMinor = 0;63int preMajor = 0;64int preMinor = 0;65byte[] master = null;6667while (true) {68String line = reader.readLine();69lineNumber++;70if (line == null) {71break;72}73if (line.startsWith("m-") == false) {74continue;75}76String data = line.substring(PREFIX_LENGTH);77if (line.startsWith("m-algorithm:")) {78algorithm = data;79} else if (line.startsWith("m-premaster:")) {80premaster = parse(data);81} else if (line.startsWith("m-crandom:")) {82clientRandom = parse(data);83} else if (line.startsWith("m-srandom:")) {84serverRandom = parse(data);85} else if (line.startsWith("m-protomajor:")) {86protoMajor = Integer.parseInt(data);87} else if (line.startsWith("m-protominor:")) {88protoMinor = Integer.parseInt(data);89} else if (line.startsWith("m-premajor:")) {90preMajor = Integer.parseInt(data);91} else if (line.startsWith("m-preminor:")) {92preMinor = Integer.parseInt(data);93} else if (line.startsWith("m-master:")) {94master = parse(data);9596System.out.print(".");97n++;9899KeyGenerator kg =100KeyGenerator.getInstance("SunTlsMasterSecret", provider);101SecretKey premasterKey =102new SecretKeySpec(premaster, algorithm);103TlsMasterSecretParameterSpec spec =104new TlsMasterSecretParameterSpec(premasterKey, protoMajor,105protoMinor, clientRandom, serverRandom,106null, -1, -1);107kg.init(spec);108TlsMasterSecret key = (TlsMasterSecret)kg.generateKey();109byte[] enc = key.getEncoded();110if (Arrays.equals(master, enc) == false) {111throw new Exception("mismatch line: " + lineNumber);112}113if ((preMajor != key.getMajorVersion()) ||114(preMinor != key.getMinorVersion())) {115throw new Exception("version mismatch line: " + lineNumber);116}117} else {118throw new Exception("Unknown line: " + line);119}120}121if (n == 0) {122throw new Exception("no tests");123}124in.close();125System.out.println();126System.out.println("OK: " + n + " tests");127}128129}130131132