Path: blob/master/test/jdk/javax/net/ssl/TLSv12/ShortRSAKey512.java
41152 views
/*1* Copyright (c) 2012, 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. 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// This test case relies on updated static security property, no way to re-use26// security property in samevm/agentvm mode.2728/*29* @test30* @bug 710677331* @summary 512 bits RSA key cannot work with SHA384 and SHA51232*33* SunJSSE does not support dynamic system properties, no way to re-use34* system properties in samevm/agentvm mode.35* @run main/othervm ShortRSAKey512 PKIX36* @run main/othervm ShortRSAKey512 SunX50937*/3839import java.net.*;40import java.util.*;41import java.io.*;42import javax.net.ssl.*;43import java.security.Security;44import java.security.KeyStore;45import java.security.KeyFactory;46import java.security.cert.Certificate;47import java.security.cert.CertificateFactory;48import java.security.spec.*;49import java.security.interfaces.*;50import java.util.Base64;515253public class ShortRSAKey512 {5455/*56* =============================================================57* Set the various variables needed for the tests, then58* specify what tests to run on each side.59*/6061/*62* Should we run the client or server in a separate thread?63* Both sides can throw exceptions, but do you have a preference64* as to which side should be the main thread.65*/66static boolean separateServerThread = false;6768/*69* Where do we find the keystores?70*/71// Certificates and key used in the test.72static String trustedCertStr =73"-----BEGIN CERTIFICATE-----\n" +74"MIICkjCCAfugAwIBAgIBADANBgkqhkiG9w0BAQQFADA7MQswCQYDVQQGEwJVUzEN\n" +75"MAsGA1UEChMESmF2YTEdMBsGA1UECxMUU3VuSlNTRSBUZXN0IFNlcml2Y2UwHhcN\n" +76"MTEwODE5MDE1MjE5WhcNMzIwNzI5MDE1MjE5WjA7MQswCQYDVQQGEwJVUzENMAsG\n" +77"A1UEChMESmF2YTEdMBsGA1UECxMUU3VuSlNTRSBUZXN0IFNlcml2Y2UwgZ8wDQYJ\n" +78"KoZIhvcNAQEBBQADgY0AMIGJAoGBAM8orG08DtF98TMSscjGsidd1ZoN4jiDpi8U\n" +79"ICz+9dMm1qM1d7O2T+KH3/mxyox7Rc2ZVSCaUD0a3CkhPMnlAx8V4u0H+E9sqso6\n" +80"iDW3JpOyzMExvZiRgRG/3nvp55RMIUV4vEHOZ1QbhuqG4ebN0Vz2DkRft7+flthf\n" +81"vDld6f5JAgMBAAGjgaUwgaIwHQYDVR0OBBYEFLl81dnfp0wDrv0OJ1sxlWzH83Xh\n" +82"MGMGA1UdIwRcMFqAFLl81dnfp0wDrv0OJ1sxlWzH83XhoT+kPTA7MQswCQYDVQQG\n" +83"EwJVUzENMAsGA1UEChMESmF2YTEdMBsGA1UECxMUU3VuSlNTRSBUZXN0IFNlcml2\n" +84"Y2WCAQAwDwYDVR0TAQH/BAUwAwEB/zALBgNVHQ8EBAMCAQYwDQYJKoZIhvcNAQEE\n" +85"BQADgYEALlgaH1gWtoBZ84EW8Hu6YtGLQ/L9zIFmHonUPZwn3Pr//icR9Sqhc3/l\n" +86"pVTxOINuFHLRz4BBtEylzRIOPzK3tg8XwuLb1zd0db90x3KBCiAL6E6cklGEPwLe\n" +87"XYMHDn9eDsaq861Tzn6ZwzMgw04zotPMoZN0mVd/3Qca8UJFucE=\n" +88"-----END CERTIFICATE-----";8990static String targetCertStr =91"-----BEGIN CERTIFICATE-----\n" +92"MIICNDCCAZ2gAwIBAgIBDDANBgkqhkiG9w0BAQQFADA7MQswCQYDVQQGEwJVUzEN\n" +93"MAsGA1UEChMESmF2YTEdMBsGA1UECxMUU3VuSlNTRSBUZXN0IFNlcml2Y2UwHhcN\n" +94"MTExMTA3MTM1NTUyWhcNMzEwNzI1MTM1NTUyWjBPMQswCQYDVQQGEwJVUzENMAsG\n" +95"A1UEChMESmF2YTEdMBsGA1UECxMUU3VuSlNTRSBUZXN0IFNlcml2Y2UxEjAQBgNV\n" +96"BAMTCWxvY2FsaG9zdDBcMA0GCSqGSIb3DQEBAQUAA0sAMEgCQQC3Pb49OSPfOD2G\n" +97"HSXFCFx1GJEZfqG9ZUf7xuIi/ra5dLjPGAaoY5QF2QOa8VnOriQCXDfyXHxsuRnE\n" +98"OomxL7EVAgMBAAGjeDB2MAsGA1UdDwQEAwID6DAdBgNVHQ4EFgQUXNCJK3/dtCIc\n" +99"xb+zlA/JINlvs/MwHwYDVR0jBBgwFoAUuXzV2d+nTAOu/Q4nWzGVbMfzdeEwJwYD\n" +100"VR0lBCAwHgYIKwYBBQUHAwEGCCsGAQUFBwMCBggrBgEFBQcDAzANBgkqhkiG9w0B\n" +101"AQQFAAOBgQB2qIDUxA2caMPpGtUACZAPRUtrGssCINIfItETXJZCx/cRuZ5sP4D9\n" +102"N1acoNDn0hCULe3lhXAeTC9NZ97680yJzregQMV5wATjo1FGsKY30Ma+sc/nfzQW\n" +103"+h/7RhYtoG0OTsiaDCvyhI6swkNJzSzrAccPY4+ZgU8HiDLzZTmM3Q==\n" +104"-----END CERTIFICATE-----";105106// Private key in the format of PKCS#8, key size is 512 bits.107static String targetPrivateKey =108"MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAtz2+PTkj3zg9hh0l\n" +109"xQhcdRiRGX6hvWVH+8biIv62uXS4zxgGqGOUBdkDmvFZzq4kAlw38lx8bLkZxDqJ\n" +110"sS+xFQIDAQABAkByx/5Oo2hQ/w2q4L8z+NTRlJ3vdl8iIDtC/4XPnfYfnGptnpG6\n" +111"ZThQRvbMZiai0xHQPQMszvAHjZVme1eDl3EBAiEA3aKJHynPVCEJhpfCLWuMwX5J\n" +112"1LntwJO7NTOyU5m8rPECIQDTpzn5X44r2rzWBDna/Sx7HW9IWCxNgUD2Eyi2nA7W\n" +113"ZQIgJerEorw4aCAuzQPxiGu57PB6GRamAihEAtoRTBQlH0ECIQDN08FgTtnesgCU\n" +114"DFYLLcw1CiHvc7fZw4neBDHCrC8NtQIgA8TOUkGnpCZlQ0KaI8KfKWI+vxFcgFnH\n" +115"3fnqsTgaUs4=";116117static char passphrase[] = "passphrase".toCharArray();118119/*120* Is the server ready to serve?121*/122volatile static boolean serverReady = false;123124/*125* Turn on SSL debugging?126*/127static boolean debug = true;128129/*130* Define the server side of the test.131*132* If the server prematurely exits, serverReady will be set to true133* to avoid infinite hangs.134*/135void doServerSide() throws Exception {136SSLContext context = generateSSLContext(null, targetCertStr,137targetPrivateKey);138SSLServerSocketFactory sslssf = context.getServerSocketFactory();139try (SSLServerSocket sslServerSocket =140(SSLServerSocket) sslssf.createServerSocket(serverPort)) {141142serverPort = sslServerSocket.getLocalPort();143System.out.println("Start server on port " + serverPort);144145/*146* Signal Client, we're ready for his connect.147*/148serverReady = true;149150try (SSLSocket sslSocket = (SSLSocket)sslServerSocket.accept()) {151InputStream sslIS = sslSocket.getInputStream();152OutputStream sslOS = sslSocket.getOutputStream();153154sslIS.read();155sslOS.write('A');156sslOS.flush();157}158}159}160161/*162* Define the client side of the test.163*164* If the server prematurely exits, serverReady will be set to true165* to avoid infinite hangs.166*/167void doClientSide() throws Exception {168169/*170* Wait for server to get started.171*/172while (!serverReady) {173Thread.sleep(50);174}175176SSLContext context = generateSSLContext(trustedCertStr, null, null);177SSLSocketFactory sslsf = context.getSocketFactory();178179System.out.println("Client connects to port " + serverPort);180try (SSLSocket sslSocket =181(SSLSocket) sslsf.createSocket("localhost", serverPort)) {182183// enable TLSv1.2 only184sslSocket.setEnabledProtocols(new String[] {"TLSv1.2"});185186// enable a block cipher187sslSocket.setEnabledCipherSuites(188new String[] {"TLS_DHE_RSA_WITH_AES_128_CBC_SHA"});189190InputStream sslIS = sslSocket.getInputStream();191OutputStream sslOS = sslSocket.getOutputStream();192193sslOS.write('B');194sslOS.flush();195sslIS.read();196}197}198199/*200* =============================================================201* The remainder is just support stuff202*/203private static String tmAlgorithm; // trust manager204205private static void parseArguments(String[] args) {206tmAlgorithm = args[0];207}208209private static SSLContext generateSSLContext(String trustedCertStr,210String keyCertStr, String keySpecStr) throws Exception {211212// generate certificate from cert string213CertificateFactory cf = CertificateFactory.getInstance("X.509");214215// create a key store216KeyStore ks = KeyStore.getInstance("JKS");217ks.load(null, null);218219// import the trused cert220Certificate trusedCert = null;221ByteArrayInputStream is = null;222if (trustedCertStr != null) {223is = new ByteArrayInputStream(trustedCertStr.getBytes());224trusedCert = cf.generateCertificate(is);225is.close();226227ks.setCertificateEntry("RSA Export Signer", trusedCert);228}229230if (keyCertStr != null) {231// generate the private key.232PKCS8EncodedKeySpec priKeySpec = new PKCS8EncodedKeySpec(233Base64.getMimeDecoder().decode(keySpecStr));234KeyFactory kf = KeyFactory.getInstance("RSA");235RSAPrivateKey priKey =236(RSAPrivateKey)kf.generatePrivate(priKeySpec);237238// generate certificate chain239is = new ByteArrayInputStream(keyCertStr.getBytes());240Certificate keyCert = cf.generateCertificate(is);241is.close();242243Certificate[] chain = null;244if (trusedCert != null) {245chain = new Certificate[2];246chain[0] = keyCert;247chain[1] = trusedCert;248} else {249chain = new Certificate[1];250chain[0] = keyCert;251}252253// import the key entry.254ks.setKeyEntry("Whatever", priKey, passphrase, chain);255}256257// create SSL context258TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmAlgorithm);259tmf.init(ks);260261SSLContext ctx = SSLContext.getInstance("TLS");262if (keyCertStr != null && !keyCertStr.isEmpty()) {263KeyManagerFactory kmf = KeyManagerFactory.getInstance("NewSunX509");264kmf.init(ks, passphrase);265266ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);267ks = null;268} else {269ctx.init(null, tmf.getTrustManagers(), null);270}271272return ctx;273}274275276// use any free port by default277volatile int serverPort = 0;278279volatile Exception serverException = null;280volatile Exception clientException = null;281282public static void main(String[] args) throws Exception {283// reset the security property to make sure that the algorithms284// and keys used in this test are not disabled.285Security.setProperty("jdk.certpath.disabledAlgorithms", "MD2");286Security.setProperty("jdk.tls.disabledAlgorithms",287"SSLv3, RC4, DH keySize < 768");288289if (debug)290System.setProperty("javax.net.debug", "all");291292/*293* Get the customized arguments.294*/295parseArguments(args);296297/*298* Start the tests.299*/300new ShortRSAKey512();301}302303Thread clientThread = null;304Thread serverThread = null;305306/*307* Primary constructor, used to drive remainder of the test.308*309* Fork off the other side, then do your work.310*/311ShortRSAKey512() throws Exception {312if (separateServerThread) {313startServer(true);314startClient(false);315} else {316startClient(true);317startServer(false);318}319320/*321* Wait for other side to close down.322*/323if (separateServerThread) {324serverThread.join();325} else {326clientThread.join();327}328329/*330* When we get here, the test is pretty much over.331* Which side threw the error?332*/333Exception local;334Exception remote;335336if (separateServerThread) {337remote = serverException;338local = clientException;339} else {340remote = clientException;341local = serverException;342}343344/*345* If both failed, return the curthread's exception, but also346* print the remote side Exception347*/348if ((local != null) && (remote != null)) {349throw local;350}351352if (remote != null) {353throw remote;354}355356if (local != null) {357throw local;358}359}360361void startServer(boolean newThread) {362if (newThread) {363serverThread = new Thread() {364public void run() {365try {366doServerSide();367} catch (Exception e) {368/*369* Our server thread just died.370*371* Release the client, if not active already...372*/373System.err.println("Server died...");374e.printStackTrace(System.err);375serverReady = true;376serverException = e;377}378}379};380serverThread.setDaemon(true);381serverThread.start();382} else {383try {384doServerSide();385} catch (Exception e) {386serverException = e;387} finally {388serverReady = true;389}390}391}392393void startClient(boolean newThread) {394if (newThread) {395clientThread = new Thread() {396public void run() {397try {398doClientSide();399} catch (Exception e) {400/*401* Our client thread just died.402*/403System.err.println("Client died...");404e.printStackTrace(System.err);405clientException = e;406}407}408};409clientThread.setDaemon(true);410clientThread.start();411} else {412try {413doClientSide();414} catch (Exception e) {415clientException = e;416}417}418}419}420421422