Path: blob/master/test/jdk/sun/security/pkcs11/tls/TestMasterSecret.java
41155 views
/*1* Copyright (c) 2005, 2018, 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 6316539 813635526* @summary Known-answer-test for TlsMasterSecret generator27* @author Andreas Sterbenz28* @library /test/lib ..29* @modules java.base/sun.security.internal.interfaces30* java.base/sun.security.internal.spec31* jdk.crypto.cryptoki32* @run main/othervm TestMasterSecret33* @run main/othervm -Djava.security.manager=allow TestMasterSecret sm TestMasterSecret.policy34*/3536import java.io.BufferedReader;37import java.nio.file.Files;38import java.nio.file.Paths;39import java.security.Provider;40import java.security.InvalidAlgorithmParameterException;41import java.util.Arrays;42import javax.crypto.KeyGenerator;43import javax.crypto.SecretKey;44import javax.crypto.spec.SecretKeySpec;45import sun.security.internal.interfaces.TlsMasterSecret;46import sun.security.internal.spec.TlsMasterSecretParameterSpec;4748public class TestMasterSecret extends PKCS11Test {4950private static final int PREFIX_LENGTH = "m-premaster: ".length();5152public static void main(String[] args) throws Exception {53main(new TestMasterSecret(), args);54}5556@Override57public void main(Provider provider) throws Exception {58if (provider.getService("KeyGenerator", "SunTlsMasterSecret") == null) {59System.out.println("Not supported by provider, skipping");60return;61}6263try (BufferedReader reader = Files.newBufferedReader(64Paths.get(BASE, "masterdata.txt"))) {6566int n = 0;67int lineNumber = 0;6869String algorithm = null;70byte[] premaster = null;71byte[] clientRandom = null;72byte[] serverRandom = null;73int protoMajor = 0;74int protoMinor = 0;75int preMajor = 0;76int preMinor = 0;77byte[] master = null;7879while (true) {80String line = reader.readLine();81lineNumber++;82if (line == null) {83break;84}85if (line.startsWith("m-") == false) {86continue;87}88String data = line.substring(PREFIX_LENGTH);89if (line.startsWith("m-algorithm:")) {90algorithm = data;91} else if (line.startsWith("m-premaster:")) {92premaster = parse(data);93} else if (line.startsWith("m-crandom:")) {94clientRandom = parse(data);95} else if (line.startsWith("m-srandom:")) {96serverRandom = parse(data);97} else if (line.startsWith("m-protomajor:")) {98protoMajor = Integer.parseInt(data);99} else if (line.startsWith("m-protominor:")) {100protoMinor = Integer.parseInt(data);101} else if (line.startsWith("m-premajor:")) {102preMajor = Integer.parseInt(data);103} else if (line.startsWith("m-preminor:")) {104preMinor = Integer.parseInt(data);105} else if (line.startsWith("m-master:")) {106master = parse(data);107108System.out.print(".");109n++;110111KeyGenerator kg =112KeyGenerator.getInstance("SunTlsMasterSecret", provider);113SecretKey premasterKey =114new SecretKeySpec(premaster, algorithm);115TlsMasterSecretParameterSpec spec =116new TlsMasterSecretParameterSpec(premasterKey,117protoMajor, protoMinor, clientRandom, serverRandom,118null, -1, -1);119120try {121kg.init(spec);122TlsMasterSecret key = (TlsMasterSecret)kg.generateKey();123byte[] enc = key.getEncoded();124if (Arrays.equals(master, enc) == false) {125throw new Exception("mismatch line: " + lineNumber);126}127if ((preMajor != key.getMajorVersion()) ||128(preMinor != key.getMinorVersion())) {129throw new Exception("version mismatch line: " + lineNumber);130}131} catch (InvalidAlgorithmParameterException iape) {132// SSLv3 support is removed in S12133if (preMajor == 3 && preMinor == 0) {134System.out.println("Skip testing SSLv3");135continue;136}137}138} else {139throw new Exception("Unknown line: " + line);140}141}142if (n == 0) {143throw new Exception("no tests");144}145System.out.println();146System.out.println("OK: " + n + " tests");147}148}149150}151152153