Path: blob/master/test/jdk/javax/net/ssl/TLS/JSSEClient.java
41152 views
/*1* Copyright (c) 2010, 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.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*/2223import java.io.InputStream;24import java.io.OutputStream;25import java.security.cert.Certificate;26import javax.net.ssl.KeyManager;27import javax.net.ssl.SSLContext;28import javax.net.ssl.SSLSession;29import javax.net.ssl.SSLSocket;30import javax.net.ssl.SSLSocketFactory;31import javax.net.ssl.TrustManager;3233class JSSEClient extends CipherTestUtils.Client {3435private static final String DEFAULT = "DEFAULT";36private static final String TLS = "TLS";3738private final SSLContext context;39private final MyX509KeyManager keyManager;40private final int port;41private final String host;42private final String protocol;4344JSSEClient(CipherTestUtils cipherTest, String host, int port,45String protocols, String ciphersuite) throws Exception {46super(cipherTest, ciphersuite);47this.host = host;48this.port = port;49this.protocol = protocols;50this.keyManager = new MyX509KeyManager(51cipherTest.getClientKeyManager());52context = SSLContext.getInstance(TLS);53}5455@Override56void runTest(CipherTestUtils.TestParameters params) throws Exception {57keyManager.setAuthType(params.clientAuth);58context.init(59new KeyManager[]{ keyManager },60new TrustManager[]{ cipherTest.getClientTrustManager() },61CipherTestUtils.secureRandom);62SSLSocketFactory factory = (SSLSocketFactory)context.getSocketFactory();6364System.out.println("Connecting to server...");65try (SSLSocket socket = (SSLSocket) factory.createSocket(host, port)) {66socket.setSoTimeout(CipherTestUtils.TIMEOUT);67socket.setEnabledCipherSuites(params.cipherSuite.split(","));68if (params.protocol != null && !params.protocol.trim().isEmpty()69&& !params.protocol.trim().equals(DEFAULT)) {70socket.setEnabledProtocols(params.protocol.split(","));71}72CipherTestUtils.printInfo(socket);73InputStream in = socket.getInputStream();74OutputStream out = socket.getOutputStream();75sendRequest(in, out);76SSLSession session = socket.getSession();77session.invalidate();78String cipherSuite = session.getCipherSuite();79if (params.cipherSuite.equals(cipherSuite) == false) {80throw new RuntimeException("Negotiated ciphersuite mismatch: "81+ cipherSuite + " != " + params.cipherSuite);82}83String protocol = session.getProtocol();84if (!DEFAULT.equals(params.protocol)85&& !params.protocol.contains(protocol)) {86throw new RuntimeException("Negotiated protocol mismatch: "87+ protocol + " != " + params.protocol);88}89if (!cipherSuite.contains("DH_anon")) {90session.getPeerCertificates();91}92Certificate[] certificates = session.getLocalCertificates();93if (params.clientAuth == null) {94if (certificates != null) {95throw new RuntimeException("Local certificates "96+ "should be null");97}98} else {99if ((certificates == null) || (certificates.length == 0)) {100throw new RuntimeException("Certificates missing");101}102String keyAlg = certificates[0].getPublicKey().getAlgorithm();103if ("EC".equals(keyAlg)) {104keyAlg = "ECDSA";105}106if (!params.clientAuth.equals(keyAlg)) {107throw new RuntimeException("Certificate type mismatch: "108+ keyAlg + " != " + params.clientAuth);109}110}111}112}113}114115116