jdk/test/javax/net/ssl/TLS/JSSEClient.java
changeset 39140 86f21a96d0ab
parent 26337 39f3ee5364e5
child 45028 b0ea3c0bfb81
equal deleted inserted replaced
39139:8c8f2162a4bc 39140:86f21a96d0ab
     1 /**
     1 /*
     2  * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it under
     5  * This code is free software; you can redistribute it and/or modify it under
     6  * the terms of the GNU General Public License version 2 only, as published by
     6  * the terms of the GNU General Public License version 2 only, as published by
     7  * the Free Software Foundation.
     7  * the Free Software Foundation.
    33 class JSSEClient extends CipherTestUtils.Client {
    33 class JSSEClient extends CipherTestUtils.Client {
    34 
    34 
    35     private static final String DEFAULT = "DEFAULT";
    35     private static final String DEFAULT = "DEFAULT";
    36     private static final String TLS = "TLS";
    36     private static final String TLS = "TLS";
    37 
    37 
    38     private final SSLContext sslContext;
    38     private final SSLContext context;
    39     private final MyX509KeyManager keyManager;
    39     private final MyX509KeyManager keyManager;
    40     private final int serverPort;
    40     private final int port;
    41     private final String serverHost;
    41     private final String host;
    42     private final String testedProtocol;
    42     private final String protocol;
    43 
    43 
    44     JSSEClient(CipherTestUtils cipherTest, String serverHost, int serverPort,
    44     JSSEClient(CipherTestUtils cipherTest, String host, int port,
    45             String testedProtocols, String testedCipherSuite) throws Exception {
    45             String protocols, String ciphersuite) throws Exception {
    46         super(cipherTest, testedCipherSuite);
    46         super(cipherTest, ciphersuite);
    47         this.serverHost = serverHost;
    47         this.host = host;
    48         this.serverPort = serverPort;
    48         this.port = port;
    49         this.testedProtocol = testedProtocols;
    49         this.protocol = protocols;
    50         this.keyManager =
    50         this.keyManager = new MyX509KeyManager(
    51                 new MyX509KeyManager(cipherTest.getClientKeyManager());
    51                                     cipherTest.getClientKeyManager());
    52         sslContext = SSLContext.getInstance(TLS);
    52         context = SSLContext.getInstance(TLS);
    53     }
    53     }
    54 
    54 
    55     @Override
    55     @Override
    56     void runTest(CipherTestUtils.TestParameters params) throws Exception {
    56     void runTest(CipherTestUtils.TestParameters params) throws Exception {
    57         SSLSocket socket = null;
    57         keyManager.setAuthType(params.clientAuth);
    58         try {
    58         context.init(
    59             System.out.println("Connecting to server...");
    59                 new KeyManager[]{ keyManager },
    60             keyManager.setAuthType(params.clientAuth);
    60                 new TrustManager[]{ cipherTest.getClientTrustManager() },
    61             sslContext.init(new KeyManager[]{keyManager},
    61                 CipherTestUtils.secureRandom);
    62                     new TrustManager[]{cipherTest.getClientTrustManager()},
    62         SSLSocketFactory factory = (SSLSocketFactory)context.getSocketFactory();
    63                     CipherTestUtils.secureRandom);
    63 
    64             SSLSocketFactory factory = (SSLSocketFactory) sslContext.
    64         System.out.println("Connecting to server...");
    65                     getSocketFactory();
    65         try (SSLSocket socket = (SSLSocket) factory.createSocket(host, port)) {
    66             socket = (SSLSocket) factory.createSocket(serverHost,
       
    67                     serverPort);
       
    68             socket.setSoTimeout(CipherTestUtils.TIMEOUT);
    66             socket.setSoTimeout(CipherTestUtils.TIMEOUT);
    69             socket.setEnabledCipherSuites(params.cipherSuite.split(","));
    67             socket.setEnabledCipherSuites(params.cipherSuite.split(","));
    70             if (params.protocol != null && !params.protocol.trim().equals("")
    68             if (params.protocol != null && !params.protocol.trim().isEmpty()
    71                     && !params.protocol.trim().equals(DEFAULT)) {
    69                     && !params.protocol.trim().equals(DEFAULT)) {
    72                 socket.setEnabledProtocols(params.protocol.split(","));
    70                 socket.setEnabledProtocols(params.protocol.split(","));
    73             }
    71             }
    74             CipherTestUtils.printInfo(socket);
    72             CipherTestUtils.printInfo(socket);
    75             InputStream in = socket.getInputStream();
    73             InputStream in = socket.getInputStream();
   103                 }
   101                 }
   104                 String keyAlg = certificates[0].getPublicKey().getAlgorithm();
   102                 String keyAlg = certificates[0].getPublicKey().getAlgorithm();
   105                 if ("EC".equals(keyAlg)) {
   103                 if ("EC".equals(keyAlg)) {
   106                     keyAlg = "ECDSA";
   104                     keyAlg = "ECDSA";
   107                 }
   105                 }
   108                 if (params.clientAuth == null ? keyAlg != null
   106                 if (!params.clientAuth.equals(keyAlg)) {
   109                         : !params.clientAuth.equals(keyAlg)) {
       
   110                     throw new RuntimeException("Certificate type mismatch: "
   107                     throw new RuntimeException("Certificate type mismatch: "
   111                             + keyAlg + " != " + params.clientAuth);
   108                             + keyAlg + " != " + params.clientAuth);
   112                 }
   109                 }
   113             }
   110             }
   114         } finally {
       
   115             if (socket != null) {
       
   116                 socket.close();
       
   117             }
       
   118         }
   111         }
   119     }
   112     }
   120 }
   113 }