Path: blob/master/test/jdk/com/sun/crypto/provider/TLS/TestMasterSecret.java
41155 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* @modules java.base/sun.security.internal.interfaces29* java.base/sun.security.internal.spec30*/3132import java.io.*;33import java.util.*;3435import java.security.Security;36import java.security.Provider;3738import javax.crypto.KeyGenerator;39import javax.crypto.SecretKey;4041import javax.crypto.spec.*;4243import sun.security.internal.spec.*;44import sun.security.internal.interfaces.TlsMasterSecret;4546public class TestMasterSecret extends Utils {4748private static int PREFIX_LENGTH = "m-premaster: ".length();4950public static void main(String[] args) throws Exception {51Provider provider = Security.getProvider("SunJCE");5253InputStream in = new FileInputStream(new File(BASE, "masterdata.txt"));54BufferedReader reader = new BufferedReader(new InputStreamReader(in));5556int n = 0;57int lineNumber = 0;5859String algorithm = null;60byte[] premaster = null;61byte[] clientRandom = null;62byte[] serverRandom = null;63int protoMajor = 0;64int protoMinor = 0;65int preMajor = 0;66int preMinor = 0;67byte[] master = null;6869while (true) {70String line = reader.readLine();71lineNumber++;72if (line == null) {73break;74}75if (line.startsWith("m-") == false) {76continue;77}78String data = line.substring(PREFIX_LENGTH);79if (line.startsWith("m-algorithm:")) {80algorithm = data;81} else if (line.startsWith("m-premaster:")) {82premaster = parse(data);83} else if (line.startsWith("m-crandom:")) {84clientRandom = parse(data);85} else if (line.startsWith("m-srandom:")) {86serverRandom = parse(data);87} else if (line.startsWith("m-protomajor:")) {88protoMajor = Integer.parseInt(data);89} else if (line.startsWith("m-protominor:")) {90protoMinor = Integer.parseInt(data);91} else if (line.startsWith("m-premajor:")) {92preMajor = Integer.parseInt(data);93} else if (line.startsWith("m-preminor:")) {94preMinor = Integer.parseInt(data);95} else if (line.startsWith("m-master:")) {96master = parse(data);9798System.out.print(".");99n++;100101KeyGenerator kg =102KeyGenerator.getInstance("SunTlsMasterSecret", provider);103SecretKey premasterKey =104new SecretKeySpec(premaster, algorithm);105TlsMasterSecretParameterSpec spec =106new TlsMasterSecretParameterSpec(premasterKey, protoMajor,107protoMinor, clientRandom, serverRandom,108null, -1, -1);109kg.init(spec);110TlsMasterSecret key = (TlsMasterSecret)kg.generateKey();111byte[] enc = key.getEncoded();112if (Arrays.equals(master, enc) == false) {113throw new Exception("mismatch line: " + lineNumber);114}115if ((preMajor != key.getMajorVersion()) ||116(preMinor != key.getMinorVersion())) {117throw new Exception("version mismatch line: " + lineNumber);118}119} else {120throw new Exception("Unknown line: " + line);121}122}123if (n == 0) {124throw new Exception("no tests");125}126in.close();127System.out.println();128System.out.println("OK: " + n + " tests");129}130131}132133134