Path: blob/master/test/jdk/javax/net/ssl/ciphersuites/ECCurvesconstraints.java
41152 views
/*1* Copyright (c) 2016, 2017, 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 814851633* @summary Improve the default strength of EC in JDK34* @modules jdk.crypto.ec35* @run main/othervm ECCurvesconstraints PKIX36* @run main/othervm ECCurvesconstraints SunX50937*/3839import java.io.ByteArrayInputStream;40import java.io.InputStream;41import java.io.OutputStream;42import java.io.IOException;43import java.security.KeyStore;44import java.security.KeyFactory;45import java.security.cert.Certificate;46import java.security.cert.CertificateFactory;47import java.security.interfaces.ECPrivateKey;48import java.security.spec.PKCS8EncodedKeySpec;49import java.util.Base64;50import javax.net.ssl.KeyManagerFactory;51import javax.net.ssl.SSLContext;52import javax.net.ssl.SSLServerSocket;53import javax.net.ssl.SSLServerSocketFactory;54import javax.net.ssl.SSLSocket;55import javax.net.ssl.SSLSocketFactory;56import javax.net.ssl.TrustManagerFactory;5758public class ECCurvesconstraints {5960/*61* =============================================================62* Set the various variables needed for the tests, then63* specify what tests to run on each side.64*/6566/*67* Should we run the client or server in a separate thread?68* Both sides can throw exceptions, but do you have a preference69* as to which side should be the main thread.70*/71static boolean separateServerThread = false;7273/*74* Where do we find the keystores?75*/76// Certificates and key used in the test.77//78// EC curve: secp224k179static String trustedCertStr =80"-----BEGIN CERTIFICATE-----\n" +81"MIIBCzCBugIEVz2lcjAKBggqhkjOPQQDAjAaMRgwFgYDVQQDDA93d3cuZXhhbXBs\n" +82"ZS5vcmcwHhcNMTYwNTE5MTEzNzM5WhcNMTcwNTE5MTEzNzM5WjAaMRgwFgYDVQQD\n" +83"DA93d3cuZXhhbXBsZS5vcmcwTjAQBgcqhkjOPQIBBgUrgQQAIAM6AAT68uovMZ8f\n" +84"KARn5NOjvieJaq6h8zHYkM9w5DuN0kkOo4KBhke06EkQj0nvQQcSvppTV6RoDLY4\n" +85"djAKBggqhkjOPQQDAgNAADA9AhwMNIujM0R0llpPH6d89d1S3VRGH/78ovc+zw51\n" +86"Ah0AuZ1YlQkUbrJIzkuPSICxz5UfCWPe+7w4as+wiA==\n" +87"-----END CERTIFICATE-----";8889// Private key in the format of PKCS#890static String targetPrivateKey =91"MIGCAgEAMBAGByqGSM49AgEGBSuBBAAgBGswaQIBAQQdAPbckc86mgW/zexB1Ajq\n" +92"38HntWOjdxL6XSoiAsWgBwYFK4EEACChPAM6AAT68uovMZ8fKARn5NOjvieJaq6h\n" +93"8zHYkM9w5DuN0kkOo4KBhke06EkQj0nvQQcSvppTV6RoDLY4dg==";9495static String[] serverCerts = {trustedCertStr};96static String[] serverKeys = {targetPrivateKey};97static String[] clientCerts = {trustedCertStr};98static String[] clientKeys = {targetPrivateKey};99100static char passphrase[] = "passphrase".toCharArray();101102/*103* Is the server ready to serve?104*/105volatile static boolean serverReady = false;106107/*108* Turn on SSL debugging?109*/110static boolean debug = false;111112/*113* Define the server side of the test.114*115* If the server prematurely exits, serverReady will be set to true116* to avoid infinite hangs.117*/118void doServerSide() throws Exception {119SSLContext context = generateSSLContext(false);120SSLServerSocketFactory sslssf = context.getServerSocketFactory();121SSLServerSocket sslServerSocket =122(SSLServerSocket)sslssf.createServerSocket(serverPort);123serverPort = sslServerSocket.getLocalPort();124125/*126* Signal Client, we're ready for his connect.127*/128serverReady = true;129130SSLSocket sslSocket = (SSLSocket)sslServerSocket.accept();131try {132sslSocket.setSoTimeout(5000);133sslSocket.setSoLinger(true, 5);134135InputStream sslIS = sslSocket.getInputStream();136OutputStream sslOS = sslSocket.getOutputStream();137138sslIS.read();139sslOS.write('A');140sslOS.flush();141142throw new Exception("EC curve secp224k1 should be disabled");143} catch (IOException she) {144// expected exception: no cipher suites in common145System.out.println("Expected exception: " + she);146} finally {147sslSocket.close();148sslServerSocket.close();149}150}151152/*153* Define the client side of the test.154*155* If the server prematurely exits, serverReady will be set to true156* to avoid infinite hangs.157*/158void doClientSide() throws Exception {159160/*161* Wait for server to get started.162*/163while (!serverReady) {164Thread.sleep(50);165}166167SSLContext context = generateSSLContext(true);168SSLSocketFactory sslsf = context.getSocketFactory();169170SSLSocket sslSocket =171(SSLSocket)sslsf.createSocket("localhost", serverPort);172173try {174sslSocket.setSoTimeout(5000);175sslSocket.setSoLinger(true, 5);176177InputStream sslIS = sslSocket.getInputStream();178OutputStream sslOS = sslSocket.getOutputStream();179180sslOS.write('B');181sslOS.flush();182sslIS.read();183184throw new Exception("EC curve secp224k1 should be disabled");185} catch (IOException she) {186// expected exception: Received fatal alert187System.out.println("Expected exception: " + she);188} finally {189sslSocket.close();190}191}192193/*194* =============================================================195* The remainder is just support stuff196*/197private static String tmAlgorithm; // trust manager198199private static void parseArguments(String[] args) {200tmAlgorithm = args[0];201}202203private static SSLContext generateSSLContext(boolean isClient)204throws Exception {205206// generate certificate from cert string207CertificateFactory cf = CertificateFactory.getInstance("X.509");208209// create a key store210KeyStore ks = KeyStore.getInstance("JKS");211ks.load(null, null);212213// import the trused cert214ByteArrayInputStream is =215new ByteArrayInputStream(trustedCertStr.getBytes());216Certificate trusedCert = cf.generateCertificate(is);217is.close();218219ks.setCertificateEntry("Export Signer", trusedCert);220221String[] certStrs = null;222String[] keyStrs = null;223if (isClient) {224certStrs = clientCerts;225keyStrs = clientKeys;226} else {227certStrs = serverCerts;228keyStrs = serverKeys;229}230231for (int i = 0; i < certStrs.length; i++) {232// generate the private key.233String keySpecStr = keyStrs[i];234PKCS8EncodedKeySpec priKeySpec = new PKCS8EncodedKeySpec(235Base64.getMimeDecoder().decode(keySpecStr));236KeyFactory kf = KeyFactory.getInstance("EC");237ECPrivateKey priKey =238(ECPrivateKey)kf.generatePrivate(priKeySpec);239240// generate certificate chain241String keyCertStr = certStrs[i];242is = new ByteArrayInputStream(keyCertStr.getBytes());243Certificate keyCert = cf.generateCertificate(is);244is.close();245246Certificate[] chain = new Certificate[2];247chain[0] = keyCert;248chain[1] = trusedCert;249250// import the key entry.251ks.setKeyEntry("key-entry-" + i, priKey, passphrase, chain);252}253254// create SSL context255TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmAlgorithm);256tmf.init(ks);257258SSLContext ctx = SSLContext.getInstance("TLS");259KeyManagerFactory kmf = KeyManagerFactory.getInstance("NewSunX509");260kmf.init(ks, passphrase);261262ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);263ks = null;264265return ctx;266}267268// use any free port by default269volatile int serverPort = 0;270271volatile Exception serverException = null;272volatile Exception clientException = null;273274public static void main(String[] args) throws Exception {275if (debug) {276System.setProperty("javax.net.debug", "all");277}278279/*280* Get the customized arguments.281*/282parseArguments(args);283284/*285* Start the tests.286*/287new ECCurvesconstraints();288}289290Thread clientThread = null;291Thread serverThread = null;292293/*294* Primary constructor, used to drive remainder of the test.295*296* Fork off the other side, then do your work.297*/298ECCurvesconstraints() throws Exception {299try {300if (separateServerThread) {301startServer(true);302startClient(false);303} else {304startClient(true);305startServer(false);306}307} catch (Exception e) {308// swallow for now. Show later309}310311/*312* Wait for other side to close down.313*/314if (separateServerThread) {315serverThread.join();316} else {317clientThread.join();318}319320/*321* When we get here, the test is pretty much over.322* Which side threw the error?323*/324Exception local;325Exception remote;326String whichRemote;327328if (separateServerThread) {329remote = serverException;330local = clientException;331whichRemote = "server";332} else {333remote = clientException;334local = serverException;335whichRemote = "client";336}337338/*339* If both failed, return the curthread's exception, but also340* print the remote side Exception341*/342if ((local != null) && (remote != null)) {343System.out.println(whichRemote + " also threw:");344remote.printStackTrace();345System.out.println();346throw local;347}348349if (remote != null) {350throw remote;351}352353if (local != null) {354throw local;355}356}357358void startServer(boolean newThread) throws Exception {359if (newThread) {360serverThread = new Thread() {361public void run() {362try {363doServerSide();364} catch (Exception e) {365/*366* Our server thread just died.367*368* Release the client, if not active already...369*/370System.err.println("Server died, because of " + e);371serverReady = true;372serverException = e;373}374}375};376serverThread.start();377} else {378try {379doServerSide();380} catch (Exception e) {381serverException = e;382} finally {383serverReady = true;384}385}386}387388void startClient(boolean newThread) throws Exception {389if (newThread) {390clientThread = new Thread() {391public void run() {392try {393doClientSide();394} catch (Exception e) {395/*396* Our client thread just died.397*/398System.err.println("Client died, because of " + e);399clientException = e;400}401}402};403clientThread.start();404} else {405try {406doClientSide();407} catch (Exception e) {408clientException = e;409}410}411}412}413414415