< prev index next >

src/java.base/share/classes/sun/security/ssl/PredefinedDHParameterSpecs.java

Print this page


   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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.security.ssl;
  27 
  28 import java.security.*;
  29 import java.math.BigInteger;
  30 import java.util.regex.Pattern;
  31 import java.util.regex.Matcher;
  32 import java.util.Map;
  33 import java.util.HashMap;
  34 import java.util.Collections;




  35 import javax.crypto.spec.DHParameterSpec;
  36 
  37 /**
  38  * Predefined default DH ephemeral parameters.
  39  */
  40 final class PredefinedDHParameterSpecs {
  41     private final static boolean debugIsOn =
  42             (Debug.getInstance("ssl") != null) && Debug.isOn("sslctx");
  43 
  44     //
  45     // Default DH ephemeral parameters
  46     //
  47     private static final BigInteger p512 = new BigInteger(       // generated
  48             "D87780E15FF50B4ABBE89870188B049406B5BEA98AB23A02" +
  49             "41D88EA75B7755E669C08093D3F0CA7FC3A5A25CF067DCB9" +
  50             "A43DD89D1D90921C6328884461E0B6D3", 16);
  51     private static final BigInteger p768 = new BigInteger(       // RFC 2409
  52             "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" +
  53             "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" +
  54             "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" +
  55             "E485B576625E7EC6F44C42E9A63A3620FFFFFFFFFFFFFFFF", 16);
  56 
  57     private static final BigInteger p1024 = new BigInteger(      // RFC 2409
  58             "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" +
  59             "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" +
  60             "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" +
  61             "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" +
  62             "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381" +


 192             "0B8CC3BDF64B10EF86B63142A3AB8829555B2F747C932665" +
 193             "CB2C0F1CC01BD70229388839D2AF05E454504AC78B758282" +
 194             "2846C0BA35C35F5C59160CC046FD8251541FC68C9C86B022" +
 195             "BB7099876A460E7451A8A93109703FEE1C217E6C3826E52C" +
 196             "51AA691E0E423CFC99E9E31650C1217B624816CDAD9A95F9" +
 197             "D5B8019488D9C0A0A1FE3075A577E23183F81D4A3F2FA457" +
 198             "1EFC8CE0BA8A4FE8B6855DFE72B0A66EDED2FBABFBE58A30" +
 199             "FAFABE1C5D71A87E2F741EF8C1FE86FEA6BBFDE530677F0D" +
 200             "97D11D49F7A8443D0822E506A9F4614E011E2A94838FF88C" +
 201             "D68C8BB7C5C6424CFFFFFFFFFFFFFFFF", 16);
 202 
 203     private static final BigInteger[] supportedPrimes = {
 204             p512, p768, p1024, p1536, p2048, p3072, p4096, p6144, p8192};
 205 
 206     private static final BigInteger[] ffdhePrimes = {
 207             p2048, p3072, p4096, p6144, p8192};
 208 
 209     // a measure of the uncertainty that prime modulus p is not a prime
 210     //
 211     // see BigInteger.isProbablePrime(int certainty)
 212     private final static int PRIME_CERTAINTY = 120;
 213 
 214     // the known security property, jdk.tls.server.defaultDHEParameters
 215     private final static String PROPERTY_NAME =
 216             "jdk.tls.server.defaultDHEParameters";
 217 
 218     private static final Pattern spacesPattern = Pattern.compile("\\s+");
 219 
 220     private final static Pattern syntaxPattern = Pattern.compile(
 221             "(\\{[0-9A-Fa-f]+,[0-9A-Fa-f]+\\})" +
 222             "(,\\{[0-9A-Fa-f]+,[0-9A-Fa-f]+\\})*");
 223 
 224     private static final Pattern paramsPattern = Pattern.compile(
 225             "\\{([0-9A-Fa-f]+),([0-9A-Fa-f]+)\\}");
 226 
 227     // cache of predefined default DH ephemeral parameters
 228     final static Map<Integer, DHParameterSpec> definedParams;
 229 
 230     // cache of Finite Field DH Ephemeral parameters (RFC 7919/FFDHE)
 231     final static Map<Integer, DHParameterSpec> ffdheParams;
 232 
 233     static {
 234         String property = AccessController.doPrivileged(
 235             new PrivilegedAction<String>() {
 236                 public String run() {
 237                     return Security.getProperty(PROPERTY_NAME);
 238                 }
 239             });
 240 
 241         if (property != null && !property.isEmpty()) {
 242             // remove double quote marks from beginning/end of the property
 243             if (property.length() >= 2 && property.charAt(0) == '"' &&
 244                     property.charAt(property.length() - 1) == '"') {
 245                 property = property.substring(1, property.length() - 1);
 246             }
 247 
 248             property = property.trim();
 249         }
 250 
 251         if (property != null && !property.isEmpty()) {
 252             Matcher spacesMatcher = spacesPattern.matcher(property);
 253             property = spacesMatcher.replaceAll("");
 254 
 255             if (debugIsOn) {
 256                 System.out.println("The Security Property " +

 257                         PROPERTY_NAME + ": " + property);
 258             }
 259         }
 260 
 261         Map<Integer,DHParameterSpec> defaultParams = new HashMap<>();
 262         if (property != null && !property.isEmpty()) {
 263             Matcher syntaxMatcher = syntaxPattern.matcher(property);
 264             if (syntaxMatcher.matches()) {
 265                 Matcher paramsFinder = paramsPattern.matcher(property);
 266                 while(paramsFinder.find()) {
 267                     String primeModulus = paramsFinder.group(1);
 268                     BigInteger p = new BigInteger(primeModulus, 16);
 269                     if (!p.isProbablePrime(PRIME_CERTAINTY)) {
 270                         if (debugIsOn) {
 271                             System.out.println(
 272                                 "Prime modulus p in Security Property, " +
 273                                 PROPERTY_NAME + ", is not a prime: " +
 274                                 primeModulus);
 275                         }
 276 
 277                         continue;
 278                     }
 279 
 280                     String baseGenerator = paramsFinder.group(2);
 281                     BigInteger g = new BigInteger(baseGenerator, 16);
 282 
 283                     DHParameterSpec spec = new DHParameterSpec(p, g);
 284                     int primeLen = p.bitLength();
 285                     defaultParams.put(primeLen, spec);
 286                 }
 287             } else if (debugIsOn) {
 288                 System.out.println("Invalid Security Property, " +
 289                         PROPERTY_NAME + ", definition");
 290             }
 291         }
 292 
 293         Map<Integer,DHParameterSpec> tempFFDHEs = new HashMap<>();
 294         for (BigInteger p : ffdhePrimes) {
 295             int primeLen = p.bitLength();
 296             DHParameterSpec dhps = new DHParameterSpec(p, BigInteger.TWO);
 297             tempFFDHEs.put(primeLen, dhps);
 298             defaultParams.putIfAbsent(primeLen, dhps);
 299         }
 300 
 301         for (BigInteger p : supportedPrimes) {
 302             int primeLen = p.bitLength();
 303             if (defaultParams.get(primeLen) == null) {
 304                 defaultParams.put(primeLen,
 305                     new DHParameterSpec(p, BigInteger.TWO));
 306             }
 307         }
 308 
   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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.security.ssl;
  27 

  28 import java.math.BigInteger;
  29 import java.security.*;



  30 import java.util.Collections;
  31 import java.util.HashMap;
  32 import java.util.Map;
  33 import java.util.regex.Matcher;
  34 import java.util.regex.Pattern;
  35 import javax.crypto.spec.DHParameterSpec;
  36 
  37 /**
  38  * Predefined default DH ephemeral parameters.
  39  */
  40 final class PredefinedDHParameterSpecs {


  41 
  42     //
  43     // Default DH ephemeral parameters
  44     //
  45     private static final BigInteger p512 = new BigInteger(       // generated
  46             "D87780E15FF50B4ABBE89870188B049406B5BEA98AB23A02" +
  47             "41D88EA75B7755E669C08093D3F0CA7FC3A5A25CF067DCB9" +
  48             "A43DD89D1D90921C6328884461E0B6D3", 16);
  49     private static final BigInteger p768 = new BigInteger(       // RFC 2409
  50             "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" +
  51             "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" +
  52             "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" +
  53             "E485B576625E7EC6F44C42E9A63A3620FFFFFFFFFFFFFFFF", 16);
  54 
  55     private static final BigInteger p1024 = new BigInteger(      // RFC 2409
  56             "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" +
  57             "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" +
  58             "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" +
  59             "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" +
  60             "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381" +


 190             "0B8CC3BDF64B10EF86B63142A3AB8829555B2F747C932665" +
 191             "CB2C0F1CC01BD70229388839D2AF05E454504AC78B758282" +
 192             "2846C0BA35C35F5C59160CC046FD8251541FC68C9C86B022" +
 193             "BB7099876A460E7451A8A93109703FEE1C217E6C3826E52C" +
 194             "51AA691E0E423CFC99E9E31650C1217B624816CDAD9A95F9" +
 195             "D5B8019488D9C0A0A1FE3075A577E23183F81D4A3F2FA457" +
 196             "1EFC8CE0BA8A4FE8B6855DFE72B0A66EDED2FBABFBE58A30" +
 197             "FAFABE1C5D71A87E2F741EF8C1FE86FEA6BBFDE530677F0D" +
 198             "97D11D49F7A8443D0822E506A9F4614E011E2A94838FF88C" +
 199             "D68C8BB7C5C6424CFFFFFFFFFFFFFFFF", 16);
 200 
 201     private static final BigInteger[] supportedPrimes = {
 202             p512, p768, p1024, p1536, p2048, p3072, p4096, p6144, p8192};
 203 
 204     private static final BigInteger[] ffdhePrimes = {
 205             p2048, p3072, p4096, p6144, p8192};
 206 
 207     // a measure of the uncertainty that prime modulus p is not a prime
 208     //
 209     // see BigInteger.isProbablePrime(int certainty)
 210     private static final int PRIME_CERTAINTY = 120;
 211 
 212     // the known security property, jdk.tls.server.defaultDHEParameters
 213     private static final String PROPERTY_NAME =
 214             "jdk.tls.server.defaultDHEParameters";
 215 
 216     private static final Pattern spacesPattern = Pattern.compile("\\s+");
 217 
 218     private static final Pattern syntaxPattern = Pattern.compile(
 219             "(\\{[0-9A-Fa-f]+,[0-9A-Fa-f]+\\})" +
 220             "(,\\{[0-9A-Fa-f]+,[0-9A-Fa-f]+\\})*");
 221 
 222     private static final Pattern paramsPattern = Pattern.compile(
 223             "\\{([0-9A-Fa-f]+),([0-9A-Fa-f]+)\\}");
 224 
 225     // cache of predefined default DH ephemeral parameters
 226     static final Map<Integer, DHParameterSpec> definedParams;
 227 
 228     // cache of Finite Field DH Ephemeral parameters (RFC 7919/FFDHE)
 229     static final Map<Integer, DHParameterSpec> ffdheParams;
 230 
 231     static {
 232         String property = AccessController.doPrivileged(
 233             new PrivilegedAction<String>() {
 234                 public String run() {
 235                     return Security.getProperty(PROPERTY_NAME);
 236                 }
 237             });
 238 
 239         if (property != null && !property.isEmpty()) {
 240             // remove double quote marks from beginning/end of the property
 241             if (property.length() >= 2 && property.charAt(0) == '"' &&
 242                     property.charAt(property.length() - 1) == '"') {
 243                 property = property.substring(1, property.length() - 1);
 244             }
 245 
 246             property = property.trim();
 247         }
 248 
 249         if (property != null && !property.isEmpty()) {
 250             Matcher spacesMatcher = spacesPattern.matcher(property);
 251             property = spacesMatcher.replaceAll("");
 252 
 253             if (SSLLogger.isOn && SSLLogger.isOn("sslctx")) {
 254                 SSLLogger.fine(
 255                         "The Security Property " +
 256                         PROPERTY_NAME + ": " + property);
 257             }
 258         }
 259 
 260         Map<Integer,DHParameterSpec> defaultParams = new HashMap<>();
 261         if (property != null && !property.isEmpty()) {
 262             Matcher syntaxMatcher = syntaxPattern.matcher(property);
 263             if (syntaxMatcher.matches()) {
 264                 Matcher paramsFinder = paramsPattern.matcher(property);
 265                 while(paramsFinder.find()) {
 266                     String primeModulus = paramsFinder.group(1);
 267                     BigInteger p = new BigInteger(primeModulus, 16);
 268                     if (!p.isProbablePrime(PRIME_CERTAINTY)) {
 269                         if (SSLLogger.isOn && SSLLogger.isOn("sslctx")) {
 270                             SSLLogger.fine(
 271                                 "Prime modulus p in Security Property, " +
 272                                 PROPERTY_NAME + ", is not a prime: " +
 273                                 primeModulus);
 274                         }
 275 
 276                         continue;
 277                     }
 278 
 279                     String baseGenerator = paramsFinder.group(2);
 280                     BigInteger g = new BigInteger(baseGenerator, 16);
 281 
 282                     DHParameterSpec spec = new DHParameterSpec(p, g);
 283                     int primeLen = p.bitLength();
 284                     defaultParams.put(primeLen, spec);
 285                 }
 286             } else if (SSLLogger.isOn && SSLLogger.isOn("sslctx")) {
 287                 SSLLogger.fine("Invalid Security Property, " +
 288                         PROPERTY_NAME + ", definition");
 289             }
 290         }
 291 
 292         Map<Integer,DHParameterSpec> tempFFDHEs = new HashMap<>();
 293         for (BigInteger p : ffdhePrimes) {
 294             int primeLen = p.bitLength();
 295             DHParameterSpec dhps = new DHParameterSpec(p, BigInteger.TWO);
 296             tempFFDHEs.put(primeLen, dhps);
 297             defaultParams.putIfAbsent(primeLen, dhps);
 298         }
 299 
 300         for (BigInteger p : supportedPrimes) {
 301             int primeLen = p.bitLength();
 302             if (defaultParams.get(primeLen) == null) {
 303                 defaultParams.put(primeLen,
 304                     new DHParameterSpec(p, BigInteger.TWO));
 305             }
 306         }
 307 
< prev index next >