1 /*
   2  * Copyright (c) 2017, 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 /*
  25  * It represents a JDK with some specific attributes.
  26  * If two JdkInfo instances have the same version value, the instances are
  27  * regarded as equivalent.
  28  */
  29 public class JdkInfo {
  30 
  31     public final String jdkPath;
  32 
  33     public final String version;
  34     public final boolean supportsECKey;
  35     public final boolean supportsSNI;
  36     public final boolean supportsALPN;
  37 
  38     public JdkInfo(String jdkPath) throws Throwable {
  39         this.jdkPath = jdkPath;
  40 
  41         String output = jdkAttributes(jdkPath);
  42         if (output == null || output.trim().isEmpty()) {
  43             throw new RuntimeException(
  44                     "Cannot determine the JDK attributes: " + jdkPath);
  45         }
  46 
  47         String[] attributes = Utils.split(output, Utils.PARAM_DELIMITER);
  48         version = attributes[0].replaceAll(".*=", "");
  49         supportsECKey = Boolean.valueOf(attributes[1].replaceAll(".*=", ""));
  50         supportsSNI = Boolean.valueOf(attributes[2].replaceAll(".*=", ""));
  51         supportsALPN = Boolean.valueOf(attributes[3].replaceAll(".*=", ""));
  52     }
  53 
  54     // Determines the specific attributes for the specified JDK.
  55     private static String jdkAttributes(String jdkPath) throws Throwable {
  56         return ProcessUtils.java(jdkPath, null, JdkUtils.class).getOutput();
  57     }
  58 
  59     @Override
  60     public int hashCode() {
  61         return version == null ? 0 : version.hashCode();
  62     }
  63 
  64     @Override
  65     public boolean equals(Object obj) {
  66         if (this == obj) {
  67             return true;
  68         }
  69         if (obj == null) {
  70             return false;
  71         }
  72         if (getClass() != obj.getClass()) {
  73             return false;
  74         }
  75         JdkInfo other = (JdkInfo) obj;
  76         if (version == null) {
  77             if (other.version != null) {
  78                 return false;
  79             }
  80         } else if (!version.equals(other.version)) {
  81             return false;
  82         }
  83         return true;
  84     }
  85 
  86     public boolean supportsCipherSuite(CipherSuite cipherSuite) {
  87         JdkRelease jdkRelease = JdkRelease.getRelease(version);
  88         return cipherSuite.startJdk.sequence <= jdkRelease.sequence
  89                 && (cipherSuite.endJdk == null
  90                         || cipherSuite.endJdk.sequence >= jdkRelease.sequence);
  91     }
  92 }