1 /*
   2  * Copyright (c) 2011, 2020, 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  * @bug 6976117
  30  * @summary SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets
  31  *          without TLSv1.1 enabled
  32  * @library /lib/security
  33  * @run main/othervm SSLContextVersion
  34  */
  35 
  36 import javax.net.ssl.*;
  37 
  38 public class SSLContextVersion {
  39     static enum ContextVersion {
  40         TLS_CV_01("SSL", "TLSv1.2", "TLSv1.2"),
  41         TLS_CV_02("TLS", "TLSv1.2", "TLSv1.2"),
  42         TLS_CV_03("SSLv3", "TLSv1", "TLSv1.2"),
  43         TLS_CV_04("TLSv1", "TLSv1", "TLSv1.2"),
  44         TLS_CV_05("TLSv1.1", "TLSv1.1", "TLSv1.2"),
  45         TLS_CV_06("TLSv1.2", "TLSv1.2", "TLSv1.2"),
  46         TLS_CV_07("Default", "TLSv1.2", "TLSv1.2");
  47 
  48         final String contextVersion;
  49         final String defaultProtocolVersion;
  50         final String supportedProtocolVersion;
  51 
  52         ContextVersion(String contextVersion, String defaultProtocolVersion,
  53                 String supportedProtocolVersion) {
  54             this.contextVersion = contextVersion;
  55             this.defaultProtocolVersion = defaultProtocolVersion;
  56             this.supportedProtocolVersion = supportedProtocolVersion;
  57         }
  58     }
  59 
  60     public static void main(String[] args) throws Exception {
  61         // Re-enable TLSv1 and TLSv1.1 since test depends on them.
  62         SecurityUtils.removeFromDisabledTlsAlgs("TLSv1", "TLSv1.1");
  63 
  64         for (ContextVersion cv : ContextVersion.values()) {
  65             System.out.println("Checking SSLContext of " + cv.contextVersion);
  66             SSLContext context = SSLContext.getInstance(cv.contextVersion);
  67 
  68             // Default SSLContext is initialized automatically.
  69             if (!cv.contextVersion.equals("Default")) {
  70                 // Use default TK, KM and random.
  71                 context.init((KeyManager[])null, (TrustManager[])null, null);
  72             }
  73 
  74             SSLParameters parameters = context.getDefaultSSLParameters();
  75 
  76             String[] protocols = parameters.getProtocols();
  77             String[] ciphers = parameters.getCipherSuites();
  78 
  79             if (protocols.length == 0 || ciphers.length == 0) {
  80                 throw new Exception("No default protocols or cipher suites");
  81             }
  82 
  83             boolean isMatch = false;
  84             for (String protocol : protocols) {
  85                 System.out.println("\tdefault protocol version " + protocol);
  86                 if (protocol.equals(cv.defaultProtocolVersion)) {
  87                     isMatch = true;
  88                     break;
  89                 }
  90             }
  91 
  92             if (!isMatch) {
  93                 throw new Exception("No matched default protocol");
  94             }
  95 
  96             parameters = context.getSupportedSSLParameters();
  97 
  98             protocols = parameters.getProtocols();
  99             ciphers = parameters.getCipherSuites();
 100 
 101             if (protocols.length == 0 || ciphers.length == 0) {
 102                 throw new Exception("No supported protocols or cipher suites");
 103             }
 104 
 105             isMatch = false;
 106             for (String protocol : protocols) {
 107                 System.out.println("\tsupported protocol version " + protocol);
 108                 if (protocol.equals(cv.supportedProtocolVersion)) {
 109                     isMatch = true;
 110                     break;
 111                 }
 112             }
 113 
 114             if (!isMatch) {
 115                 throw new Exception("No matched supported protocol");
 116             }
 117             System.out.println("\t... Success");
 118         }
 119     }
 120 }