Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/crypto/provider/TLS/TestPremaster.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 Basic tests for TlsRsaPremasterSecret generator27* @author Andreas Sterbenz28*/2930import java.security.Security;31import java.security.Provider;3233import javax.crypto.KeyGenerator;34import javax.crypto.SecretKey;35import java.util.Formatter;3637import sun.security.internal.spec.TlsRsaPremasterSecretParameterSpec;3839public class TestPremaster {4041public static void main(String[] args) throws Exception {42Provider provider = Security.getProvider("SunJCE");4344KeyGenerator kg;4546kg = KeyGenerator.getInstance("SunTlsRsaPremasterSecret", provider);4748try {49kg.generateKey();50throw new Exception("no exception");51} catch (IllegalStateException e) {52System.out.println("OK: " + e);53}5455int[] protocolVersions = {0x0300, 0x0301, 0x0302, 0x0400};56for (int clientVersion : protocolVersions) {57for (int serverVersion : protocolVersions) {58test(kg, clientVersion, serverVersion);59if (serverVersion >= clientVersion) {60break;61}62}63}6465System.out.println("Done.");66}6768private static void test(KeyGenerator kg,69int clientVersion, int serverVersion) throws Exception {7071System.out.printf(72"Testing RSA pre-master secret key generation between " +73"client (0x%04X) and server(0x%04X)%n",74clientVersion, serverVersion);75kg.init(new TlsRsaPremasterSecretParameterSpec(76clientVersion, serverVersion));7778SecretKey key = kg.generateKey();79byte[] encoded = key.getEncoded();80if (encoded != null) { // raw key material may be not extractable81if (encoded.length != 48) {82throw new Exception("length: " + encoded.length);83}84int v = versionOf(encoded[0], encoded[1]);85if (clientVersion != v) {86if (serverVersion != v || clientVersion >= 0x0302) {87throw new Exception(String.format(88"version mismatch: (0x%04X) rather than (0x%04X) " +89"is used in pre-master secret", v, clientVersion));90}91System.out.printf("Use compatible version (0x%04X)%n", v);92}93System.out.println("Passed, version matches!");94} else {95System.out.println("Raw key material is not extractable");96}97}9899private static int versionOf(int major, int minor) {100return ((major & 0xFF) << 8) | (minor & 0xFF);101}102}103104105