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