Path: blob/master/test/jdk/javax/net/ssl/TLSv12/ShortRSAKeyGCM.java
41152 views
/*1* Copyright (c) 2012, 2015, 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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425//26// SunJSSE does not support dynamic system properties, no way to re-use27// system properties in samevm/agentvm mode.28//2930/*31* @test32* @bug 703096633* @summary Support AEAD CipherSuites34* @run main/othervm ShortRSAKeyGCM PKIX TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA25635* @run main/othervm ShortRSAKeyGCM PKIX TLS_RSA_WITH_AES_128_GCM_SHA25636* @run main/othervm ShortRSAKeyGCM PKIX TLS_DHE_RSA_WITH_AES_128_GCM_SHA25637* @run main/othervm ShortRSAKeyGCM PKIX TLS_DH_anon_WITH_AES_128_GCM_SHA25638*/3940/*41* Need additional key materials to run the following cases.42*43* @run main/othervm ShortRSAKeyGCM PKIX TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA25644* @run main/othervm ShortRSAKeyGCM PKIX TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA25645* @run main/othervm ShortRSAKeyGCM PKIX TLS_ECDH_RSA_WITH_AES_128_GCM_SHA25646*47* Need unlimited JCE Unlimited Strength Jurisdiction Policy to run the48* following cases.49*50* @run main/othervm ShortRSAKeyGCM PKIX TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA38451* @run main/othervm ShortRSAKeyGCM PKIX TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA38452* @run main/othervm ShortRSAKeyGCM PKIX TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA38453* @run main/othervm ShortRSAKeyGCM PKIX TLS_ECDH_RSA_WITH_AES_256_GCM_SHA38454* @run main/othervm ShortRSAKeyGCM PKIX TLS_RSA_WITH_AES_256_GCM_SHA38455* @run main/othervm ShortRSAKeyGCM PKIX TLS_DHE_RSA_WITH_AES_256_GCM_SHA38456* @run main/othervm ShortRSAKeyGCM PKIX TLS_DH_anon_WITH_AES_256_GCM_SHA38457*/5859import java.net.*;60import java.util.*;61import java.io.*;62import javax.net.ssl.*;63import java.security.Security;64import java.security.KeyStore;65import java.security.KeyFactory;66import java.security.cert.Certificate;67import java.security.cert.CertificateFactory;68import java.security.spec.*;69import java.security.interfaces.*;707172public class ShortRSAKeyGCM {7374/*75* =============================================================76* Set the various variables needed for the tests, then77* specify what tests to run on each side.78*/7980/*81* Should we run the client or server in a separate thread?82* Both sides can throw exceptions, but do you have a preference83* as to which side should be the main thread.84*/85static boolean separateServerThread = true;8687/*88* Where do we find the keystores?89*/90// Certificates and key used in the test.91static String trustedCertStr =92"-----BEGIN CERTIFICATE-----\n" +93"MIICkjCCAfugAwIBAgIBADANBgkqhkiG9w0BAQQFADA7MQswCQYDVQQGEwJVUzEN\n" +94"MAsGA1UEChMESmF2YTEdMBsGA1UECxMUU3VuSlNTRSBUZXN0IFNlcml2Y2UwHhcN\n" +95"MTEwODE5MDE1MjE5WhcNMzIwNzI5MDE1MjE5WjA7MQswCQYDVQQGEwJVUzENMAsG\n" +96"A1UEChMESmF2YTEdMBsGA1UECxMUU3VuSlNTRSBUZXN0IFNlcml2Y2UwgZ8wDQYJ\n" +97"KoZIhvcNAQEBBQADgY0AMIGJAoGBAM8orG08DtF98TMSscjGsidd1ZoN4jiDpi8U\n" +98"ICz+9dMm1qM1d7O2T+KH3/mxyox7Rc2ZVSCaUD0a3CkhPMnlAx8V4u0H+E9sqso6\n" +99"iDW3JpOyzMExvZiRgRG/3nvp55RMIUV4vEHOZ1QbhuqG4ebN0Vz2DkRft7+flthf\n" +100"vDld6f5JAgMBAAGjgaUwgaIwHQYDVR0OBBYEFLl81dnfp0wDrv0OJ1sxlWzH83Xh\n" +101"MGMGA1UdIwRcMFqAFLl81dnfp0wDrv0OJ1sxlWzH83XhoT+kPTA7MQswCQYDVQQG\n" +102"EwJVUzENMAsGA1UEChMESmF2YTEdMBsGA1UECxMUU3VuSlNTRSBUZXN0IFNlcml2\n" +103"Y2WCAQAwDwYDVR0TAQH/BAUwAwEB/zALBgNVHQ8EBAMCAQYwDQYJKoZIhvcNAQEE\n" +104"BQADgYEALlgaH1gWtoBZ84EW8Hu6YtGLQ/L9zIFmHonUPZwn3Pr//icR9Sqhc3/l\n" +105"pVTxOINuFHLRz4BBtEylzRIOPzK3tg8XwuLb1zd0db90x3KBCiAL6E6cklGEPwLe\n" +106"XYMHDn9eDsaq861Tzn6ZwzMgw04zotPMoZN0mVd/3Qca8UJFucE=\n" +107"-----END CERTIFICATE-----";108109static String targetCertStr =110"-----BEGIN CERTIFICATE-----\n" +111"MIICNDCCAZ2gAwIBAgIBDDANBgkqhkiG9w0BAQQFADA7MQswCQYDVQQGEwJVUzEN\n" +112"MAsGA1UEChMESmF2YTEdMBsGA1UECxMUU3VuSlNTRSBUZXN0IFNlcml2Y2UwHhcN\n" +113"MTExMTA3MTM1NTUyWhcNMzEwNzI1MTM1NTUyWjBPMQswCQYDVQQGEwJVUzENMAsG\n" +114"A1UEChMESmF2YTEdMBsGA1UECxMUU3VuSlNTRSBUZXN0IFNlcml2Y2UxEjAQBgNV\n" +115"BAMTCWxvY2FsaG9zdDBcMA0GCSqGSIb3DQEBAQUAA0sAMEgCQQC3Pb49OSPfOD2G\n" +116"HSXFCFx1GJEZfqG9ZUf7xuIi/ra5dLjPGAaoY5QF2QOa8VnOriQCXDfyXHxsuRnE\n" +117"OomxL7EVAgMBAAGjeDB2MAsGA1UdDwQEAwID6DAdBgNVHQ4EFgQUXNCJK3/dtCIc\n" +118"xb+zlA/JINlvs/MwHwYDVR0jBBgwFoAUuXzV2d+nTAOu/Q4nWzGVbMfzdeEwJwYD\n" +119"VR0lBCAwHgYIKwYBBQUHAwEGCCsGAQUFBwMCBggrBgEFBQcDAzANBgkqhkiG9w0B\n" +120"AQQFAAOBgQB2qIDUxA2caMPpGtUACZAPRUtrGssCINIfItETXJZCx/cRuZ5sP4D9\n" +121"N1acoNDn0hCULe3lhXAeTC9NZ97680yJzregQMV5wATjo1FGsKY30Ma+sc/nfzQW\n" +122"+h/7RhYtoG0OTsiaDCvyhI6swkNJzSzrAccPY4+ZgU8HiDLzZTmM3Q==\n" +123"-----END CERTIFICATE-----";124125// Private key in the format of PKCS#8, key size is 512 bits.126static String targetPrivateKey =127"MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAtz2+PTkj3zg9hh0l\n" +128"xQhcdRiRGX6hvWVH+8biIv62uXS4zxgGqGOUBdkDmvFZzq4kAlw38lx8bLkZxDqJ\n" +129"sS+xFQIDAQABAkByx/5Oo2hQ/w2q4L8z+NTRlJ3vdl8iIDtC/4XPnfYfnGptnpG6\n" +130"ZThQRvbMZiai0xHQPQMszvAHjZVme1eDl3EBAiEA3aKJHynPVCEJhpfCLWuMwX5J\n" +131"1LntwJO7NTOyU5m8rPECIQDTpzn5X44r2rzWBDna/Sx7HW9IWCxNgUD2Eyi2nA7W\n" +132"ZQIgJerEorw4aCAuzQPxiGu57PB6GRamAihEAtoRTBQlH0ECIQDN08FgTtnesgCU\n" +133"DFYLLcw1CiHvc7fZw4neBDHCrC8NtQIgA8TOUkGnpCZlQ0KaI8KfKWI+vxFcgFnH\n" +134"3fnqsTgaUs4=";135136static char passphrase[] = "passphrase".toCharArray();137138/*139* Is the server ready to serve?140*/141volatile static boolean serverReady = false;142143/*144* Turn on SSL debugging?145*/146static boolean debug = false;147148/*149* Define the server side of the test.150*151* If the server prematurely exits, serverReady will be set to true152* to avoid infinite hangs.153*/154void doServerSide() throws Exception {155SSLContext context = generateSSLContext(null, targetCertStr,156targetPrivateKey);157SSLServerSocketFactory sslssf = context.getServerSocketFactory();158SSLServerSocket sslServerSocket =159(SSLServerSocket)sslssf.createServerSocket(serverPort);160serverPort = sslServerSocket.getLocalPort();161162/*163* Signal Client, we're ready for his connect.164*/165serverReady = true;166167SSLSocket sslSocket = (SSLSocket)sslServerSocket.accept();168sslSocket.setEnabledCipherSuites(sslSocket.getSupportedCipherSuites());169InputStream sslIS = sslSocket.getInputStream();170OutputStream sslOS = sslSocket.getOutputStream();171172sslIS.read();173sslOS.write('A');174sslOS.flush();175176sslSocket.close();177}178179/*180* Define the client side of the test.181*182* If the server prematurely exits, serverReady will be set to true183* to avoid infinite hangs.184*/185void doClientSide() throws Exception {186187/*188* Wait for server to get started.189*/190while (!serverReady) {191Thread.sleep(50);192}193194SSLContext context = generateSSLContext(trustedCertStr, null, null);195SSLSocketFactory sslsf = context.getSocketFactory();196197SSLSocket sslSocket =198(SSLSocket)sslsf.createSocket("localhost", serverPort);199200// enable TLSv1.2 only201sslSocket.setEnabledProtocols(new String[] {"TLSv1.2"});202203// enable a block cipher204sslSocket.setEnabledCipherSuites(new String[] {cipherSuite});205206InputStream sslIS = sslSocket.getInputStream();207OutputStream sslOS = sslSocket.getOutputStream();208209sslOS.write('B');210sslOS.flush();211sslIS.read();212213sslSocket.close();214}215216/*217* =============================================================218* The remainder is just support stuff219*/220private static String tmAlgorithm; // trust manager221private static String cipherSuite; // cipher suite222223private static void parseArguments(String[] args) {224tmAlgorithm = args[0];225cipherSuite = args[1];226}227228private static SSLContext generateSSLContext(String trustedCertStr,229String keyCertStr, String keySpecStr) throws Exception {230231// generate certificate from cert string232CertificateFactory cf = CertificateFactory.getInstance("X.509");233234// create a key store235KeyStore ks = KeyStore.getInstance("JKS");236ks.load(null, null);237238// import the trused cert239Certificate trusedCert = null;240ByteArrayInputStream is = null;241if (trustedCertStr != null) {242is = new ByteArrayInputStream(trustedCertStr.getBytes());243trusedCert = cf.generateCertificate(is);244is.close();245246ks.setCertificateEntry("RSA Export Signer", trusedCert);247}248249if (keyCertStr != null) {250// generate the private key.251PKCS8EncodedKeySpec priKeySpec = new PKCS8EncodedKeySpec(252Base64.getMimeDecoder().decode(keySpecStr));253KeyFactory kf = KeyFactory.getInstance("RSA");254RSAPrivateKey priKey =255(RSAPrivateKey)kf.generatePrivate(priKeySpec);256257// generate certificate chain258is = new ByteArrayInputStream(keyCertStr.getBytes());259Certificate keyCert = cf.generateCertificate(is);260is.close();261262Certificate[] chain = null;263if (trusedCert != null) {264chain = new Certificate[2];265chain[0] = keyCert;266chain[1] = trusedCert;267} else {268chain = new Certificate[1];269chain[0] = keyCert;270}271272// import the key entry.273ks.setKeyEntry("Whatever", priKey, passphrase, chain);274}275276// create SSL context277TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmAlgorithm);278tmf.init(ks);279280SSLContext ctx = SSLContext.getInstance("TLS");281if (keyCertStr != null && !keyCertStr.isEmpty()) {282KeyManagerFactory kmf = KeyManagerFactory.getInstance("NewSunX509");283kmf.init(ks, passphrase);284285ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);286ks = null;287} else {288ctx.init(null, tmf.getTrustManagers(), null);289}290291return ctx;292}293294295// use any free port by default296volatile int serverPort = 0;297298volatile Exception serverException = null;299volatile Exception clientException = null;300301public static void main(String[] args) throws Exception {302// reset the security property to make sure that the algorithms303// and keys used in this test are not disabled.304Security.setProperty("jdk.certpath.disabledAlgorithms", "MD2");305Security.setProperty("jdk.tls.disabledAlgorithms",306"SSLv3, RC4, DH keySize < 768");307308if (debug) {309System.setProperty("javax.net.debug", "all");310}311312/*313* Get the customized arguments.314*/315parseArguments(args);316317/*318* Start the tests.319*/320new ShortRSAKeyGCM();321}322323Thread clientThread = null;324Thread serverThread = null;325326/*327* Primary constructor, used to drive remainder of the test.328*329* Fork off the other side, then do your work.330*/331ShortRSAKeyGCM() throws Exception {332try {333if (separateServerThread) {334startServer(true);335startClient(false);336} else {337startClient(true);338startServer(false);339}340} catch (Exception e) {341// swallow for now. Show later342}343344/*345* Wait for other side to close down.346*/347if (separateServerThread) {348serverThread.join();349} else {350clientThread.join();351}352353/*354* When we get here, the test is pretty much over.355* Which side threw the error?356*/357Exception local;358Exception remote;359String whichRemote;360361if (separateServerThread) {362remote = serverException;363local = clientException;364whichRemote = "server";365} else {366remote = clientException;367local = serverException;368whichRemote = "client";369}370371/*372* If both failed, return the curthread's exception, but also373* print the remote side Exception374*/375if ((local != null) && (remote != null)) {376System.out.println(whichRemote + " also threw:");377remote.printStackTrace();378System.out.println();379throw local;380}381382if (remote != null) {383throw remote;384}385386if (local != null) {387throw local;388}389}390391void startServer(boolean newThread) throws Exception {392if (newThread) {393serverThread = new Thread() {394public void run() {395try {396doServerSide();397} catch (Exception e) {398/*399* Our server thread just died.400*401* Release the client, if not active already...402*/403System.err.println("Server died..." + e);404serverReady = true;405serverException = e;406}407}408};409serverThread.start();410} else {411try {412doServerSide();413} catch (Exception e) {414serverException = e;415} finally {416serverReady = true;417}418}419}420421void startClient(boolean newThread) throws Exception {422if (newThread) {423clientThread = new Thread() {424public void run() {425try {426doClientSide();427} catch (Exception e) {428/*429* Our client thread just died.430*/431System.err.println("Client died..." + e);432clientException = e;433}434}435};436clientThread.start();437} else {438try {439doClientSide();440} catch (Exception e) {441clientException = e;442}443}444}445}446447448