Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/tls/TestPremaster.java
38855 views
/*1* Copyright (c) 2005, 2016, 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 631653926* @summary Basic tests for TlsRsaPremasterSecret generator27* @author Andreas Sterbenz28* @library ..29* @run main/othervm TestPremaster30* @run main/othervm TestPremaster sm policy31*/3233import java.security.Provider;34import javax.crypto.KeyGenerator;35import javax.crypto.SecretKey;36import sun.security.internal.spec.TlsRsaPremasterSecretParameterSpec;3738public class TestPremaster extends PKCS11Test {3940public static void main(String[] args) throws Exception {41main(new TestPremaster(), args);42}4344@Override45public void main(Provider provider) throws Exception {46if (provider.getService(47"KeyGenerator", "SunTlsRsaPremasterSecret") == null) {48System.out.println("Not supported by provider, skipping");49return;50}51KeyGenerator kg;52kg = KeyGenerator.getInstance("SunTlsRsaPremasterSecret", provider);5354try {55kg.generateKey();56throw new Exception("no exception");57} catch (IllegalStateException e) {58System.out.println("OK: " + e);59}6061int[] protocolVersions = {0x0300, 0x0301, 0x0302, 0x0400};62for (int clientVersion : protocolVersions) {63for (int serverVersion : protocolVersions) {64test(kg, clientVersion, serverVersion);65if (serverVersion >= clientVersion) {66break;67}68}69}7071System.out.println("Done.");72}7374private static void test(KeyGenerator kg,75int clientVersion, int serverVersion) throws Exception {7677System.out.printf(78"Testing RSA pre-master secret key generation between " +79"client (0x%04X) and server(0x%04X)%n",80clientVersion, serverVersion);81kg.init(new TlsRsaPremasterSecretParameterSpec(82clientVersion, serverVersion));83SecretKey key = kg.generateKey();84byte[] encoded = key.getEncoded();85if (encoded != null) { // raw key material may be not extractable86if (encoded.length != 48) {87throw new Exception("length: " + encoded.length);88}89int v = versionOf(encoded[0], encoded[1]);90if (clientVersion != v) {91if (serverVersion != v || clientVersion >= 0x0302) {92throw new Exception(String.format(93"version mismatch: (0x%04X) rather than (0x%04X) " +94"is used in pre-master secret", v, clientVersion));95}96System.out.printf("Use compatible version (0x%04X)%n", v);97}98System.out.println("Passed, version matches!");99} else {100System.out.println("Raw key material is not extractable");101}102}103104private static int versionOf(int major, int minor) {105return ((major & 0xFF) << 8) | (minor & 0xFF);106}107108}109110111