Path: blob/master/test/jdk/javax/net/ssl/TLSv12/DisabledShortRSAKeys.java
41152 views
/*1* Copyright (c) 2012, 2013, 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// SunJSSE does not support dynamic system properties, no way to re-use26// system properties in samevm/agentvm mode.2728/*29* @test30* @bug 710927431* @summary Consider disabling support for X.509 certificates with RSA keys32* less than 1024 bits33* @library /javax/net/ssl/templates34* @run main/othervm DisabledShortRSAKeys PKIX TLSv1.235* @run main/othervm DisabledShortRSAKeys SunX509 TLSv1.236* @run main/othervm DisabledShortRSAKeys PKIX TLSv1.137* @run main/othervm DisabledShortRSAKeys SunX509 TLSv1.138* @run main/othervm DisabledShortRSAKeys PKIX TLSv139* @run main/othervm DisabledShortRSAKeys SunX509 TLSv140* @run main/othervm DisabledShortRSAKeys PKIX SSLv341* @run main/othervm DisabledShortRSAKeys SunX509 SSLv342*/4344import java.net.*;45import java.util.*;46import java.io.*;47import javax.net.ssl.*;48import java.security.Security;49import java.security.KeyStore;50import java.security.KeyFactory;51import java.security.cert.Certificate;52import java.security.cert.CertificateFactory;53import java.security.spec.*;54import java.security.interfaces.*;55import java.util.Base64;565758public class DisabledShortRSAKeys extends SSLSocketTemplate {5960/*61* Where do we find the keystores?62*/63// Certificates and key used in the test.64static String trustedCertStr =65"-----BEGIN CERTIFICATE-----\n" +66"MIICkjCCAfugAwIBAgIBADANBgkqhkiG9w0BAQQFADA7MQswCQYDVQQGEwJVUzEN\n" +67"MAsGA1UEChMESmF2YTEdMBsGA1UECxMUU3VuSlNTRSBUZXN0IFNlcml2Y2UwHhcN\n" +68"MTEwODE5MDE1MjE5WhcNMzIwNzI5MDE1MjE5WjA7MQswCQYDVQQGEwJVUzENMAsG\n" +69"A1UEChMESmF2YTEdMBsGA1UECxMUU3VuSlNTRSBUZXN0IFNlcml2Y2UwgZ8wDQYJ\n" +70"KoZIhvcNAQEBBQADgY0AMIGJAoGBAM8orG08DtF98TMSscjGsidd1ZoN4jiDpi8U\n" +71"ICz+9dMm1qM1d7O2T+KH3/mxyox7Rc2ZVSCaUD0a3CkhPMnlAx8V4u0H+E9sqso6\n" +72"iDW3JpOyzMExvZiRgRG/3nvp55RMIUV4vEHOZ1QbhuqG4ebN0Vz2DkRft7+flthf\n" +73"vDld6f5JAgMBAAGjgaUwgaIwHQYDVR0OBBYEFLl81dnfp0wDrv0OJ1sxlWzH83Xh\n" +74"MGMGA1UdIwRcMFqAFLl81dnfp0wDrv0OJ1sxlWzH83XhoT+kPTA7MQswCQYDVQQG\n" +75"EwJVUzENMAsGA1UEChMESmF2YTEdMBsGA1UECxMUU3VuSlNTRSBUZXN0IFNlcml2\n" +76"Y2WCAQAwDwYDVR0TAQH/BAUwAwEB/zALBgNVHQ8EBAMCAQYwDQYJKoZIhvcNAQEE\n" +77"BQADgYEALlgaH1gWtoBZ84EW8Hu6YtGLQ/L9zIFmHonUPZwn3Pr//icR9Sqhc3/l\n" +78"pVTxOINuFHLRz4BBtEylzRIOPzK3tg8XwuLb1zd0db90x3KBCiAL6E6cklGEPwLe\n" +79"XYMHDn9eDsaq861Tzn6ZwzMgw04zotPMoZN0mVd/3Qca8UJFucE=\n" +80"-----END CERTIFICATE-----";8182static String targetCertStr =83"-----BEGIN CERTIFICATE-----\n" +84"MIICNDCCAZ2gAwIBAgIBDDANBgkqhkiG9w0BAQQFADA7MQswCQYDVQQGEwJVUzEN\n" +85"MAsGA1UEChMESmF2YTEdMBsGA1UECxMUU3VuSlNTRSBUZXN0IFNlcml2Y2UwHhcN\n" +86"MTExMTA3MTM1NTUyWhcNMzEwNzI1MTM1NTUyWjBPMQswCQYDVQQGEwJVUzENMAsG\n" +87"A1UEChMESmF2YTEdMBsGA1UECxMUU3VuSlNTRSBUZXN0IFNlcml2Y2UxEjAQBgNV\n" +88"BAMTCWxvY2FsaG9zdDBcMA0GCSqGSIb3DQEBAQUAA0sAMEgCQQC3Pb49OSPfOD2G\n" +89"HSXFCFx1GJEZfqG9ZUf7xuIi/ra5dLjPGAaoY5QF2QOa8VnOriQCXDfyXHxsuRnE\n" +90"OomxL7EVAgMBAAGjeDB2MAsGA1UdDwQEAwID6DAdBgNVHQ4EFgQUXNCJK3/dtCIc\n" +91"xb+zlA/JINlvs/MwHwYDVR0jBBgwFoAUuXzV2d+nTAOu/Q4nWzGVbMfzdeEwJwYD\n" +92"VR0lBCAwHgYIKwYBBQUHAwEGCCsGAQUFBwMCBggrBgEFBQcDAzANBgkqhkiG9w0B\n" +93"AQQFAAOBgQB2qIDUxA2caMPpGtUACZAPRUtrGssCINIfItETXJZCx/cRuZ5sP4D9\n" +94"N1acoNDn0hCULe3lhXAeTC9NZ97680yJzregQMV5wATjo1FGsKY30Ma+sc/nfzQW\n" +95"+h/7RhYtoG0OTsiaDCvyhI6swkNJzSzrAccPY4+ZgU8HiDLzZTmM3Q==\n" +96"-----END CERTIFICATE-----";9798// Private key in the format of PKCS#8, key size is 512 bits.99static String targetPrivateKey =100"MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAtz2+PTkj3zg9hh0l\n" +101"xQhcdRiRGX6hvWVH+8biIv62uXS4zxgGqGOUBdkDmvFZzq4kAlw38lx8bLkZxDqJ\n" +102"sS+xFQIDAQABAkByx/5Oo2hQ/w2q4L8z+NTRlJ3vdl8iIDtC/4XPnfYfnGptnpG6\n" +103"ZThQRvbMZiai0xHQPQMszvAHjZVme1eDl3EBAiEA3aKJHynPVCEJhpfCLWuMwX5J\n" +104"1LntwJO7NTOyU5m8rPECIQDTpzn5X44r2rzWBDna/Sx7HW9IWCxNgUD2Eyi2nA7W\n" +105"ZQIgJerEorw4aCAuzQPxiGu57PB6GRamAihEAtoRTBQlH0ECIQDN08FgTtnesgCU\n" +106"DFYLLcw1CiHvc7fZw4neBDHCrC8NtQIgA8TOUkGnpCZlQ0KaI8KfKWI+vxFcgFnH\n" +107"3fnqsTgaUs4=";108109static char passphrase[] = "passphrase".toCharArray();110111/*112* Turn on SSL debugging?113*/114static boolean debug = false;115116@Override117protected SSLContext createClientSSLContext() throws Exception {118return generateSSLContext(trustedCertStr, null, null);119}120121@Override122protected SSLContext createServerSSLContext() throws Exception {123return generateSSLContext(null, targetCertStr, targetPrivateKey);124}125126@Override127protected void runServerApplication(SSLSocket socket) throws Exception {128try {129try (InputStream sslIS = socket.getInputStream()) {130sslIS.read();131}132throw new Exception("RSA keys shorter than 1024 bits should be disabled");133} catch (SSLHandshakeException sslhe) {134// the expected exception, ignore135}136137}138139@Override140protected void runClientApplication(SSLSocket socket) throws Exception {141142try {143144// only enable the target protocol145socket.setEnabledProtocols(new String[] { enabledProtocol });146// enable a block cipher147socket.setEnabledCipherSuites(148new String[] { "TLS_DHE_RSA_WITH_AES_128_CBC_SHA" });149150try (OutputStream sslOS = socket.getOutputStream()) {151sslOS.write('B');152sslOS.flush();153}154throw new Exception(155"RSA keys shorter than 1024 bits should be disabled");156} catch (SSLHandshakeException sslhe) {157// the expected exception, ignore158}159}160161/*162* =============================================================163* The remainder is just support stuff164*/165private static String tmAlgorithm; // trust manager166private static String enabledProtocol; // the target protocol167168private static void parseArguments(String[] args) {169tmAlgorithm = args[0];170enabledProtocol = args[1];171}172173private static SSLContext generateSSLContext(String trustedCertStr,174String keyCertStr, String keySpecStr) throws Exception {175176// generate certificate from cert string177CertificateFactory cf = CertificateFactory.getInstance("X.509");178179// create a key store180KeyStore ks = KeyStore.getInstance("JKS");181ks.load(null, null);182183// import the trused cert184Certificate trusedCert = null;185ByteArrayInputStream is = null;186if (trustedCertStr != null) {187is = new ByteArrayInputStream(trustedCertStr.getBytes());188trusedCert = cf.generateCertificate(is);189is.close();190191ks.setCertificateEntry("RSA Export Signer", trusedCert);192}193194if (keyCertStr != null) {195// generate the private key.196PKCS8EncodedKeySpec priKeySpec = new PKCS8EncodedKeySpec(197Base64.getMimeDecoder().decode(keySpecStr));198KeyFactory kf = KeyFactory.getInstance("RSA");199RSAPrivateKey priKey =200(RSAPrivateKey)kf.generatePrivate(priKeySpec);201202// generate certificate chain203is = new ByteArrayInputStream(keyCertStr.getBytes());204Certificate keyCert = cf.generateCertificate(is);205is.close();206207Certificate[] chain = null;208if (trusedCert != null) {209chain = new Certificate[2];210chain[0] = keyCert;211chain[1] = trusedCert;212} else {213chain = new Certificate[1];214chain[0] = keyCert;215}216217// import the key entry.218ks.setKeyEntry("Whatever", priKey, passphrase, chain);219}220221// create SSL context222TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmAlgorithm);223tmf.init(ks);224225SSLContext ctx = SSLContext.getInstance("TLS");226if (keyCertStr != null && !keyCertStr.isEmpty()) {227KeyManagerFactory kmf = KeyManagerFactory.getInstance("NewSunX509");228kmf.init(ks, passphrase);229230ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);231ks = null;232} else {233ctx.init(null, tmf.getTrustManagers(), null);234}235236return ctx;237}238239public static void main(String[] args) throws Exception {240Security.setProperty("jdk.certpath.disabledAlgorithms",241"RSA keySize < 1024");242Security.setProperty("jdk.tls.disabledAlgorithms",243"RSA keySize < 1024");244245if (debug) {246System.setProperty("javax.net.debug", "all");247}248249/*250* Get the customized arguments.251*/252parseArguments(args);253254/*255* Start the tests.256*/257new DisabledShortRSAKeys().run();258}259260}261262263