Path: blob/master/test/jdk/com/sun/crypto/provider/TLS/TestPremaster.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 Basic tests for TlsRsaPremasterSecret generator27* @author Andreas Sterbenz28* @modules java.base/sun.security.internal.spec29*/3031import java.security.Security;32import java.security.Provider;3334import javax.crypto.KeyGenerator;35import javax.crypto.SecretKey;36import java.util.Formatter;3738import sun.security.internal.spec.TlsRsaPremasterSecretParameterSpec;3940public class TestPremaster {4142public static void main(String[] args) throws Exception {43Provider provider = Security.getProvider("SunJCE");4445KeyGenerator kg;4647kg = KeyGenerator.getInstance("SunTlsRsaPremasterSecret", provider);4849try {50kg.generateKey();51throw new Exception("no exception");52} catch (IllegalStateException e) {53System.out.println("OK: " + e);54}5556int[] protocolVersions = {0x0300, 0x0301, 0x0302, 0x0400};57for (int clientVersion : protocolVersions) {58for (int serverVersion : protocolVersions) {59test(kg, clientVersion, serverVersion);60if (serverVersion >= clientVersion) {61break;62}63}64}6566System.out.println("Done.");67}6869private static void test(KeyGenerator kg,70int clientVersion, int serverVersion) throws Exception {7172System.out.printf(73"Testing RSA pre-master secret key generation between " +74"client (0x%04X) and server(0x%04X)%n",75clientVersion, serverVersion);76kg.init(new TlsRsaPremasterSecretParameterSpec(77clientVersion, serverVersion));7879SecretKey key = kg.generateKey();80byte[] encoded = key.getEncoded();81if (encoded != null) { // raw key material may be not extractable82if (encoded.length != 48) {83throw new Exception("length: " + encoded.length);84}85int v = versionOf(encoded[0], encoded[1]);86if (clientVersion != v) {87if (serverVersion != v || clientVersion >= 0x0302) {88throw new Exception(String.format(89"version mismatch: (0x%04X) rather than (0x%04X) " +90"is used in pre-master secret", v, clientVersion));91}92System.out.printf("Use compatible version (0x%04X)%n", v);93}94System.out.println("Passed, version matches!");95} else {96System.out.println("Raw key material is not extractable");97}98}99100private static int versionOf(int major, int minor) {101return ((major & 0xFF) << 8) | (minor & 0xFF);102}103}104105106