test/jdk/sun/security/ssl/SSLContextImpl/CustomizedDTLSServerDefaultProtocols.java
changeset 50768 68fa3d4026ea
child 51771 1f805481d8de
equal deleted inserted replaced
50767:356eaea05bf0 50768:68fa3d4026ea
       
     1 /*
       
     2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 // SunJSSE does not support dynamic system properties, no way to re-use
       
    25 // system properties in samevm/agentvm mode.
       
    26 
       
    27 /*
       
    28  * @test
       
    29  * @summary Test jdk.tls.server.protocols with DTLS
       
    30  * @run main/othervm -Djdk.tls.server.protocols="DTLSv1.0"
       
    31  *      CustomizedDTLSServerDefaultProtocols
       
    32  */
       
    33 
       
    34 import java.security.NoSuchAlgorithmException;
       
    35 import java.security.Security;
       
    36 import java.util.Arrays;
       
    37 import java.util.HashSet;
       
    38 import java.util.Set;
       
    39 
       
    40 import javax.net.SocketFactory;
       
    41 import javax.net.ssl.SSLContext;
       
    42 import javax.net.ssl.SSLEngine;
       
    43 import javax.net.ssl.SSLParameters;
       
    44 import javax.net.ssl.SSLServerSocket;
       
    45 import javax.net.ssl.SSLServerSocketFactory;
       
    46 import javax.net.ssl.SSLSocket;
       
    47 
       
    48 public class CustomizedDTLSServerDefaultProtocols {
       
    49 
       
    50     final static String[] supportedProtocols = new String[]{
       
    51             "DTLSv1.0", "DTLSv1.2"};
       
    52 
       
    53     enum ContextVersion {
       
    54         TLS_CV_01("DTLS",
       
    55                 new String[]{"DTLSv1.0"},
       
    56                 supportedProtocols),
       
    57         TLS_CV_02("DTLSv1.0",
       
    58                 supportedProtocols,
       
    59                 new String[]{"DTLSv1.0"}),
       
    60         TLS_CV_03("DTLS1.2",
       
    61                 supportedProtocols,
       
    62                 supportedProtocols);
       
    63 
       
    64         final String contextVersion;
       
    65         final String[] serverEnabledProtocols;
       
    66         final String[] clientEnabledProtocols;
       
    67 
       
    68         ContextVersion(String contextVersion, String[] serverEnabledProtocols,
       
    69                 String[] clientEnabledProtocols) {
       
    70             this.contextVersion = contextVersion;
       
    71             this.serverEnabledProtocols = serverEnabledProtocols;
       
    72             this.clientEnabledProtocols = clientEnabledProtocols;
       
    73         }
       
    74     }
       
    75 
       
    76     private static boolean checkProtocols(String[] target, String[] expected) {
       
    77         boolean success = true;
       
    78         if (target.length == 0) {
       
    79             System.out.println("\tError: No protocols");
       
    80             success = false;
       
    81         }
       
    82 
       
    83         if (!protocolEquals(target, expected)) {
       
    84             System.out.println("\tError: Expected to get protocols " +
       
    85                     Arrays.toString(expected));
       
    86             success = false;
       
    87         }
       
    88         System.out.println("\t  Protocols found " + Arrays.toString(target));
       
    89         return success;
       
    90     }
       
    91 
       
    92     private static boolean protocolEquals(
       
    93             String[] actualProtocols,
       
    94             String[] expectedProtocols) {
       
    95         if (actualProtocols.length != expectedProtocols.length) {
       
    96             return false;
       
    97         }
       
    98 
       
    99         Set<String> set = new HashSet<>(Arrays.asList(expectedProtocols));
       
   100         for (String actual : actualProtocols) {
       
   101             if (set.add(actual)) {
       
   102                 return false;
       
   103             }
       
   104         }
       
   105 
       
   106         return true;
       
   107     }
       
   108 
       
   109     private static boolean checkCipherSuites(String[] target) {
       
   110         boolean success = true;
       
   111         if (target.length == 0) {
       
   112             System.out.println("\tError: No cipher suites");
       
   113             success = false;
       
   114         }
       
   115 
       
   116         return success;
       
   117     }
       
   118 
       
   119     public static void main(String[] args) throws Exception {
       
   120         // reset the security property to make sure that the algorithms
       
   121         // and keys used in this test are not disabled.
       
   122         Security.setProperty("jdk.tls.disabledAlgorithms", "");
       
   123         System.out.println("jdk.tls.client.protocols = " +
       
   124                 System.getProperty("jdk.tls.client.protocols"));
       
   125         System.out.println("jdk.tls.server.protocols = "+
       
   126                 System.getProperty("jdk.tls.server.protocols"));
       
   127         Test();
       
   128     }
       
   129 
       
   130     static void Test() throws Exception {
       
   131         boolean failed = false;
       
   132 
       
   133         SSLContext context;
       
   134         for (ContextVersion cv : ContextVersion.values()) {
       
   135             System.out.println("Checking SSLContext of " + cv.contextVersion);
       
   136             try {
       
   137                 context = SSLContext.getInstance(cv.contextVersion);
       
   138             } catch (NoSuchAlgorithmException e) {
       
   139                 if (cv.contextVersion.compareToIgnoreCase("DTLS1.2") == 0) {
       
   140                     System.out.println("Exception expected: " + e.getMessage());
       
   141                     continue;
       
   142                 }
       
   143                 throw e;
       
   144             }
       
   145             // Default SSLContext is initialized automatically.
       
   146             if (!cv.contextVersion.equals("Default")) {
       
   147                 // Use default TK, KM and random.
       
   148                 context.init(null, null, null);
       
   149             }
       
   150 
       
   151             //
       
   152             // Check SSLContext
       
   153             //
       
   154             // Check default SSLParameters of SSLContext
       
   155             System.out.println("\tChecking default SSLParameters");
       
   156             SSLParameters parameters = context.getDefaultSSLParameters();
       
   157 
       
   158             String[] protocols = parameters.getProtocols();
       
   159             failed |= !checkProtocols(protocols, cv.clientEnabledProtocols);
       
   160 
       
   161             String[] ciphers = parameters.getCipherSuites();
       
   162             failed |= !checkCipherSuites(ciphers);
       
   163 
       
   164             // Check supported SSLParameters of SSLContext
       
   165             System.out.println("\tChecking supported SSLParameters");
       
   166             parameters = context.getSupportedSSLParameters();
       
   167 
       
   168             protocols = parameters.getProtocols();
       
   169             failed |= !checkProtocols(protocols, supportedProtocols);
       
   170 
       
   171             ciphers = parameters.getCipherSuites();
       
   172             failed |= !checkCipherSuites(ciphers);
       
   173 
       
   174             //
       
   175             // Check SSLEngine
       
   176             //
       
   177             // Check SSLParameters of SSLEngine
       
   178             System.out.println();
       
   179             System.out.println("\tChecking SSLEngine of this SSLContext");
       
   180             System.out.println("\tChecking SSLEngine.getSSLParameters()");
       
   181             SSLEngine engine = context.createSSLEngine();
       
   182             engine.setUseClientMode(true);
       
   183             parameters = engine.getSSLParameters();
       
   184 
       
   185             protocols = parameters.getProtocols();
       
   186             failed |= !checkProtocols(protocols, cv.clientEnabledProtocols);
       
   187 
       
   188             ciphers = parameters.getCipherSuites();
       
   189             failed |= !checkCipherSuites(ciphers);
       
   190 
       
   191             System.out.println("\tChecking SSLEngine.getEnabledProtocols()");
       
   192             protocols = engine.getEnabledProtocols();
       
   193             failed |= !checkProtocols(protocols, cv.clientEnabledProtocols);
       
   194 
       
   195             System.out.println("\tChecking SSLEngine.getEnabledCipherSuites()");
       
   196             ciphers = engine.getEnabledCipherSuites();
       
   197             failed |= !checkCipherSuites(ciphers);
       
   198 
       
   199             System.out.println("\tChecking SSLEngine.getSupportedProtocols()");
       
   200             protocols = engine.getSupportedProtocols();
       
   201             failed |= !checkProtocols(protocols, supportedProtocols);
       
   202 
       
   203             System.out.println(
       
   204                     "\tChecking SSLEngine.getSupportedCipherSuites()");
       
   205             ciphers = engine.getSupportedCipherSuites();
       
   206             failed |= !checkCipherSuites(ciphers);
       
   207 
       
   208             //
       
   209             // Check SSLSocket
       
   210             //
       
   211             // Check SSLParameters of SSLSocket
       
   212             System.out.println();
       
   213             System.out.println("\tChecking SSLSocket of this SSLContext");
       
   214             System.out.println("\tChecking SSLSocket.getSSLParameters()");
       
   215             SocketFactory fac = context.getSocketFactory();
       
   216             SSLSocket socket = (SSLSocket) fac.createSocket();
       
   217             parameters = socket.getSSLParameters();
       
   218 
       
   219             protocols = parameters.getProtocols();
       
   220             failed |= !checkProtocols(protocols, cv.clientEnabledProtocols);
       
   221 
       
   222             ciphers = parameters.getCipherSuites();
       
   223             failed |= !checkCipherSuites(ciphers);
       
   224 
       
   225             System.out.println("\tChecking SSLSocket.getEnabledProtocols()");
       
   226             protocols = socket.getEnabledProtocols();
       
   227             failed |= !checkProtocols(protocols, cv.clientEnabledProtocols);
       
   228 
       
   229             System.out.println("\tChecking SSLSocket.getEnabledCipherSuites()");
       
   230             ciphers = socket.getEnabledCipherSuites();
       
   231             failed |= !checkCipherSuites(ciphers);
       
   232 
       
   233             System.out.println("\tChecking SSLSocket.getSupportedProtocols()");
       
   234             protocols = socket.getSupportedProtocols();
       
   235             failed |= !checkProtocols(protocols, supportedProtocols);
       
   236 
       
   237             System.out.println(
       
   238                     "\tChecking SSLSocket.getSupportedCipherSuites()");
       
   239             ciphers = socket.getSupportedCipherSuites();
       
   240             failed |= !checkCipherSuites(ciphers);
       
   241 
       
   242             //
       
   243             // Check SSLServerSocket
       
   244             //
       
   245             // Check SSLParameters of SSLServerSocket
       
   246             System.out.println();
       
   247             System.out.println("\tChecking SSLServerSocket of this SSLContext");
       
   248             System.out.println("\tChecking SSLServerSocket.getSSLParameters()");
       
   249             SSLServerSocketFactory sf = context.getServerSocketFactory();
       
   250             SSLServerSocket ssocket = (SSLServerSocket) sf.createServerSocket();
       
   251             parameters = ssocket.getSSLParameters();
       
   252 
       
   253             protocols = parameters.getProtocols();
       
   254             failed |= !checkProtocols(protocols, cv.serverEnabledProtocols);
       
   255 
       
   256             ciphers = parameters.getCipherSuites();
       
   257             failed |= !checkCipherSuites(ciphers);
       
   258 
       
   259             System.out.println("\tChecking SSLEngine.getEnabledProtocols()");
       
   260             protocols = ssocket.getEnabledProtocols();
       
   261             failed |= !checkProtocols(protocols, cv.serverEnabledProtocols);
       
   262 
       
   263             System.out.println("\tChecking SSLEngine.getEnabledCipherSuites()");
       
   264             ciphers = ssocket.getEnabledCipherSuites();
       
   265             failed |= !checkCipherSuites(ciphers);
       
   266 
       
   267             System.out.println("\tChecking SSLEngine.getSupportedProtocols()");
       
   268             protocols = ssocket.getSupportedProtocols();
       
   269             failed |= !checkProtocols(protocols, supportedProtocols);
       
   270 
       
   271             System.out.println(
       
   272                     "\tChecking SSLEngine.getSupportedCipherSuites()");
       
   273             ciphers = ssocket.getSupportedCipherSuites();
       
   274             failed |= !checkCipherSuites(ciphers);
       
   275 
       
   276             if (failed) {
       
   277                 throw new Exception("Run into problems, see log for more details");
       
   278             } else {
       
   279                 System.out.println("\t... Success");
       
   280             }
       
   281         }
       
   282     }
       
   283 }