1/* 2 * Copyright (c) 1996, 2016, 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 27package sun.security.ssl; 28 29import java.util.Map; 30import java.util.HashMap; 31import java.util.Collections; 32import java.util.regex.Pattern; 33import java.util.regex.Matcher; 34import java.math.BigInteger; 35import java.security.*; 36import java.io.IOException; 37import javax.net.ssl.SSLHandshakeException; 38import javax.crypto.SecretKey; 39import javax.crypto.KeyAgreement; 40import javax.crypto.interfaces.DHPublicKey; 41import javax.crypto.spec.*; 42import java.util.EnumSet; 43 44import sun.security.util.KeyUtil; 45 46/** 47 * This class implements the Diffie-Hellman key exchange algorithm. 48 * D-H means combining your private key with your partners public key to 49 * generate a number. The peer does the same with its private key and our 50 * public key. Through the magic of Diffie-Hellman we both come up with the 51 * same number. This number is secret (discounting MITM attacks) and hence 52 * called the shared secret. It has the same length as the modulus, e.g. 512 53 * or 1024 bit. Man-in-the-middle attacks are typically countered by an 54 * independent authentication step using certificates (RSA, DSA, etc.). 55 * 56 * The thing to note is that the shared secret is constant for two partners 57 * with constant private keys. This is often not what we want, which is why 58 * it is generally a good idea to create a new private key for each session. 59 * Generating a private key involves one modular exponentiation assuming 60 * suitable D-H parameters are available. 61 * 62 * General usage of this class (TLS DHE case): 63 * . if we are server, call DHCrypt(keyLength,random). This generates 64 * an ephemeral keypair of the request length. 65 * . if we are client, call DHCrypt(modulus, base, random). This 66 * generates an ephemeral keypair using the parameters specified by 67 * the server. 68 * . send parameters and public value to remote peer 69 * . receive peers ephemeral public key 70 * . call getAgreedSecret() to calculate the shared secret 71 * 72 * In TLS the server chooses the parameter values itself, the client must use 73 * those sent to it by the server. 74 * 75 * The use of ephemeral keys as described above also achieves what is called 76 * "forward secrecy". This means that even if the authentication keys are 77 * broken at a later date, the shared secret remains secure. The session is 78 * compromised only if the authentication keys are already broken at the 79 * time the key exchange takes place and an active MITM attack is used. 80 * This is in contrast to straightforward encrypting RSA key exchanges. 81 * 82 * @author David Brownell 83 */ 84final class DHCrypt { 85 86 // group parameters (prime modulus and generator) 87 private BigInteger modulus; // P (aka N) 88 private BigInteger base; // G (aka alpha) 89 90 // our private key (including private component x) 91 private PrivateKey privateKey; 92 93 // public component of our key, X = (g ^ x) mod p 94 private BigInteger publicValue; // X (aka y) 95 96 // the times to recove from failure if public key validation 97 private static int MAX_FAILOVER_TIMES = 2; 98 99 /** 100 * Generate a Diffie-Hellman keypair of the specified size. 101 */ 102 DHCrypt(int keyLength, SecureRandom random) { 103 this(keyLength, 104 ParametersHolder.definedParams.get(keyLength), random); 105 } 106 107 /** 108 * Generate a Diffie-Hellman keypair using the specified parameters. 109 * 110 * @param modulus the Diffie-Hellman modulus P 111 * @param base the Diffie-Hellman base G 112 */ 113 DHCrypt(BigInteger modulus, BigInteger base, SecureRandom random) { 114 this(modulus.bitLength(), 115 new DHParameterSpec(modulus, base), random); 116 } 117 118 /** 119 * Generate a Diffie-Hellman keypair using the specified size and 120 * parameters. 121 */ 122 private DHCrypt(int keyLength, 123 DHParameterSpec params, SecureRandom random) { 124 125 try { 126 KeyPairGenerator kpg = JsseJce.getKeyPairGenerator("DiffieHellman"); 127 if (params != null) { 128 kpg.initialize(params, random); 129 } else { 130 kpg.initialize(keyLength, random); 131 } 132 133 DHPublicKeySpec spec = generateDHPublicKeySpec(kpg); 134 if (spec == null) { 135 throw new RuntimeException("Could not generate DH keypair"); 136 } 137 138 publicValue = spec.getY(); 139 modulus = spec.getP(); 140 base = spec.getG(); 141 } catch (GeneralSecurityException e) { 142 throw new RuntimeException("Could not generate DH keypair", e); 143 } 144 } 145 146 static DHPublicKeySpec getDHPublicKeySpec(PublicKey key) { 147 if (key instanceof DHPublicKey) { 148 DHPublicKey dhKey = (DHPublicKey)key; 149 DHParameterSpec params = dhKey.getParams(); 150 return new DHPublicKeySpec(dhKey.getY(), 151 params.getP(), params.getG()); 152 } 153 try { 154 KeyFactory factory = JsseJce.getKeyFactory("DiffieHellman"); 155 return factory.getKeySpec(key, DHPublicKeySpec.class); 156 } catch (Exception e) { 157 throw new RuntimeException(e); 158 } 159 } 160 161 162 /** Returns the Diffie-Hellman modulus. */ 163 BigInteger getModulus() { 164 return modulus; 165 } 166 167 /** Returns the Diffie-Hellman base (generator). */ 168 BigInteger getBase() { 169 return base; 170 } 171 172 /** 173 * Gets the public key of this end of the key exchange. 174 */ 175 BigInteger getPublicKey() { 176 return publicValue; 177 } 178 179 /** 180 * Get the secret data that has been agreed on through Diffie-Hellman 181 * key agreement protocol. Note that in the two party protocol, if 182 * the peer keys are already known, no other data needs to be sent in 183 * order to agree on a secret. That is, a secured message may be 184 * sent without any mandatory round-trip overheads. 185 * 186 * <P>It is illegal to call this member function if the private key 187 * has not been set (or generated). 188 * 189 * @param peerPublicKey the peer's public key. 190 * @param keyIsValidated whether the {@code peerPublicKey} has beed 191 * validated 192 * @return the secret, which is an unsigned big-endian integer 193 * the same size as the Diffie-Hellman modulus. 194 */ 195 SecretKey getAgreedSecret(BigInteger peerPublicValue, 196 boolean keyIsValidated) throws SSLHandshakeException { 197 try { 198 KeyFactory kf = JsseJce.getKeyFactory("DiffieHellman"); 199 DHPublicKeySpec spec = 200 new DHPublicKeySpec(peerPublicValue, modulus, base); 201 PublicKey publicKey = kf.generatePublic(spec); 202 KeyAgreement ka = JsseJce.getKeyAgreement("DiffieHellman"); 203 204 // validate the Diffie-Hellman public key 205 if (!keyIsValidated && 206 !KeyUtil.isOracleJCEProvider(ka.getProvider().getName())) { 207 try { 208 KeyUtil.validate(spec); 209 } catch (InvalidKeyException ike) { 210 // prefer handshake_failure alert to internal_error alert 211 throw new SSLHandshakeException(ike.getMessage()); 212 } 213 } 214 215 ka.init(privateKey); 216 ka.doPhase(publicKey, true); 217 return ka.generateSecret("TlsPremasterSecret"); 218 } catch (GeneralSecurityException e) { 219 throw (SSLHandshakeException) new SSLHandshakeException( 220 "Could not generate secret").initCause(e); 221 } 222 } 223 224 // Check constraints of the specified DH public key. 225 void checkConstraints(AlgorithmConstraints constraints, 226 BigInteger peerPublicValue) throws SSLHandshakeException { 227 228 try { 229 KeyFactory kf = JsseJce.getKeyFactory("DiffieHellman"); 230 DHPublicKeySpec spec = 231 new DHPublicKeySpec(peerPublicValue, modulus, base); 232 DHPublicKey publicKey = (DHPublicKey)kf.generatePublic(spec); 233 234 // check constraints of DHPublicKey 235 if (!constraints.permits( 236 EnumSet.of(CryptoPrimitive.KEY_AGREEMENT), publicKey)) { 237 throw new SSLHandshakeException( 238 "DHPublicKey does not comply to algorithm constraints"); 239 } 240 } catch (GeneralSecurityException gse) { 241 throw (SSLHandshakeException) new SSLHandshakeException( 242 "Could not generate DHPublicKey").initCause(gse); 243 } 244 } 245 246 // Generate and validate DHPublicKeySpec 247 private DHPublicKeySpec generateDHPublicKeySpec(KeyPairGenerator kpg) 248 throws GeneralSecurityException { 249 250 boolean doExtraValiadtion = 251 (!KeyUtil.isOracleJCEProvider(kpg.getProvider().getName())); 252 for (int i = 0; i <= MAX_FAILOVER_TIMES; i++) { 253 KeyPair kp = kpg.generateKeyPair(); 254 privateKey = kp.getPrivate(); 255 DHPublicKeySpec spec = getDHPublicKeySpec(kp.getPublic()); 256 257 // validate the Diffie-Hellman public key 258 if (doExtraValiadtion) { 259 try { 260 KeyUtil.validate(spec); 261 } catch (InvalidKeyException ivke) { 262 if (i == MAX_FAILOVER_TIMES) { 263 throw ivke; 264 } 265 // otherwise, ignore the exception and try the next one 266 continue; 267 } 268 } 269 270 return spec; 271 } 272 273 return null; 274 } 275 276 // lazy initialization holder class idiom for static default parameters 277 // 278 // See Effective Java Second Edition: Item 71. 279 private static class ParametersHolder { 280 private final static boolean debugIsOn = 281 (Debug.getInstance("ssl") != null) && Debug.isOn("sslctx"); 282 283 // 284 // Default DH ephemeral parameters 285 // 286 private static final BigInteger p512 = new BigInteger( // generated 287 "D87780E15FF50B4ABBE89870188B049406B5BEA98AB23A02" + 288 "41D88EA75B7755E669C08093D3F0CA7FC3A5A25CF067DCB9" + 289 "A43DD89D1D90921C6328884461E0B6D3", 16); 290 private static final BigInteger p768 = new BigInteger( // RFC 2409 291 "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" + 292 "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" + 293 "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" + 294 "E485B576625E7EC6F44C42E9A63A3620FFFFFFFFFFFFFFFF", 16); 295 296 private static final BigInteger p1024 = new BigInteger( // RFC 2409 297 "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" + 298 "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" + 299 "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" + 300 "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" + 301 "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381" + 302 "FFFFFFFFFFFFFFFF", 16); 303 private static final BigInteger p1536 = new BigInteger( // RFC 3526 304 "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" + 305 "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" + 306 "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" + 307 "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" + 308 "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" + 309 "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" + 310 "83655D23DCA3AD961C62F356208552BB9ED529077096966D" + 311 "670C354E4ABC9804F1746C08CA237327FFFFFFFFFFFFFFFF", 16); 312 private static final BigInteger p2048 = new BigInteger( // TLS FFDHE 313 "FFFFFFFFFFFFFFFFADF85458A2BB4A9AAFDC5620273D3CF1" + 314 "D8B9C583CE2D3695A9E13641146433FBCC939DCE249B3EF9" + 315 "7D2FE363630C75D8F681B202AEC4617AD3DF1ED5D5FD6561" + 316 "2433F51F5F066ED0856365553DED1AF3B557135E7F57C935" + 317 "984F0C70E0E68B77E2A689DAF3EFE8721DF158A136ADE735" + 318 "30ACCA4F483A797ABC0AB182B324FB61D108A94BB2C8E3FB" + 319 "B96ADAB760D7F4681D4F42A3DE394DF4AE56EDE76372BB19" + 320 "0B07A7C8EE0A6D709E02FCE1CDF7E2ECC03404CD28342F61" + 321 "9172FE9CE98583FF8E4F1232EEF28183C3FE3B1B4C6FAD73" + 322 "3BB5FCBC2EC22005C58EF1837D1683B2C6F34A26C1B2EFFA" + 323 "886B423861285C97FFFFFFFFFFFFFFFF", 16); 324 private static final BigInteger p3072 = new BigInteger( // TLS FFDHE 325 "FFFFFFFFFFFFFFFFADF85458A2BB4A9AAFDC5620273D3CF1" + 326 "D8B9C583CE2D3695A9E13641146433FBCC939DCE249B3EF9" + 327 "7D2FE363630C75D8F681B202AEC4617AD3DF1ED5D5FD6561" + 328 "2433F51F5F066ED0856365553DED1AF3B557135E7F57C935" + 329 "984F0C70E0E68B77E2A689DAF3EFE8721DF158A136ADE735" + 330 "30ACCA4F483A797ABC0AB182B324FB61D108A94BB2C8E3FB" + 331 "B96ADAB760D7F4681D4F42A3DE394DF4AE56EDE76372BB19" + 332 "0B07A7C8EE0A6D709E02FCE1CDF7E2ECC03404CD28342F61" + 333 "9172FE9CE98583FF8E4F1232EEF28183C3FE3B1B4C6FAD73" + 334 "3BB5FCBC2EC22005C58EF1837D1683B2C6F34A26C1B2EFFA" + 335 "886B4238611FCFDCDE355B3B6519035BBC34F4DEF99C0238" + 336 "61B46FC9D6E6C9077AD91D2691F7F7EE598CB0FAC186D91C" + 337 "AEFE130985139270B4130C93BC437944F4FD4452E2D74DD3" + 338 "64F2E21E71F54BFF5CAE82AB9C9DF69EE86D2BC522363A0D" + 339 "ABC521979B0DEADA1DBF9A42D5C4484E0ABCD06BFA53DDEF" + 340 "3C1B20EE3FD59D7C25E41D2B66C62E37FFFFFFFFFFFFFFFF", 16); 341 private static final BigInteger p4096 = new BigInteger( // TLS FFDHE 342 "FFFFFFFFFFFFFFFFADF85458A2BB4A9AAFDC5620273D3CF1" + 343 "D8B9C583CE2D3695A9E13641146433FBCC939DCE249B3EF9" + 344 "7D2FE363630C75D8F681B202AEC4617AD3DF1ED5D5FD6561" + 345 "2433F51F5F066ED0856365553DED1AF3B557135E7F57C935" + 346 "984F0C70E0E68B77E2A689DAF3EFE8721DF158A136ADE735" + 347 "30ACCA4F483A797ABC0AB182B324FB61D108A94BB2C8E3FB" + 348 "B96ADAB760D7F4681D4F42A3DE394DF4AE56EDE76372BB19" + 349 "0B07A7C8EE0A6D709E02FCE1CDF7E2ECC03404CD28342F61" + 350 "9172FE9CE98583FF8E4F1232EEF28183C3FE3B1B4C6FAD73" + 351 "3BB5FCBC2EC22005C58EF1837D1683B2C6F34A26C1B2EFFA" + 352 "886B4238611FCFDCDE355B3B6519035BBC34F4DEF99C0238" + 353 "61B46FC9D6E6C9077AD91D2691F7F7EE598CB0FAC186D91C" + 354 "AEFE130985139270B4130C93BC437944F4FD4452E2D74DD3" + 355 "64F2E21E71F54BFF5CAE82AB9C9DF69EE86D2BC522363A0D" + 356 "ABC521979B0DEADA1DBF9A42D5C4484E0ABCD06BFA53DDEF" + 357 "3C1B20EE3FD59D7C25E41D2B669E1EF16E6F52C3164DF4FB" + 358 "7930E9E4E58857B6AC7D5F42D69F6D187763CF1D55034004" + 359 "87F55BA57E31CC7A7135C886EFB4318AED6A1E012D9E6832" + 360 "A907600A918130C46DC778F971AD0038092999A333CB8B7A" + 361 "1A1DB93D7140003C2A4ECEA9F98D0ACC0A8291CDCEC97DCF" + 362 "8EC9B55A7F88A46B4DB5A851F44182E1C68A007E5E655F6A" + 363 "FFFFFFFFFFFFFFFF", 16); 364 private static final BigInteger p6144 = new BigInteger( // TLS FFDHE 365 "FFFFFFFFFFFFFFFFADF85458A2BB4A9AAFDC5620273D3CF1" + 366 "D8B9C583CE2D3695A9E13641146433FBCC939DCE249B3EF9" + 367 "7D2FE363630C75D8F681B202AEC4617AD3DF1ED5D5FD6561" + 368 "2433F51F5F066ED0856365553DED1AF3B557135E7F57C935" + 369 "984F0C70E0E68B77E2A689DAF3EFE8721DF158A136ADE735" + 370 "30ACCA4F483A797ABC0AB182B324FB61D108A94BB2C8E3FB" + 371 "B96ADAB760D7F4681D4F42A3DE394DF4AE56EDE76372BB19" + 372 "0B07A7C8EE0A6D709E02FCE1CDF7E2ECC03404CD28342F61" + 373 "9172FE9CE98583FF8E4F1232EEF28183C3FE3B1B4C6FAD73" + 374 "3BB5FCBC2EC22005C58EF1837D1683B2C6F34A26C1B2EFFA" + 375 "886B4238611FCFDCDE355B3B6519035BBC34F4DEF99C0238" + 376 "61B46FC9D6E6C9077AD91D2691F7F7EE598CB0FAC186D91C" + 377 "AEFE130985139270B4130C93BC437944F4FD4452E2D74DD3" + 378 "64F2E21E71F54BFF5CAE82AB9C9DF69EE86D2BC522363A0D" + 379 "ABC521979B0DEADA1DBF9A42D5C4484E0ABCD06BFA53DDEF" + 380 "3C1B20EE3FD59D7C25E41D2B669E1EF16E6F52C3164DF4FB" + 381 "7930E9E4E58857B6AC7D5F42D69F6D187763CF1D55034004" + 382 "87F55BA57E31CC7A7135C886EFB4318AED6A1E012D9E6832" + 383 "A907600A918130C46DC778F971AD0038092999A333CB8B7A" + 384 "1A1DB93D7140003C2A4ECEA9F98D0ACC0A8291CDCEC97DCF" + 385 "8EC9B55A7F88A46B4DB5A851F44182E1C68A007E5E0DD902" + 386 "0BFD64B645036C7A4E677D2C38532A3A23BA4442CAF53EA6" + 387 "3BB454329B7624C8917BDD64B1C0FD4CB38E8C334C701C3A" + 388 "CDAD0657FCCFEC719B1F5C3E4E46041F388147FB4CFDB477" + 389 "A52471F7A9A96910B855322EDB6340D8A00EF092350511E3" + 390 "0ABEC1FFF9E3A26E7FB29F8C183023C3587E38DA0077D9B4" + 391 "763E4E4B94B2BBC194C6651E77CAF992EEAAC0232A281BF6" + 392 "B3A739C1226116820AE8DB5847A67CBEF9C9091B462D538C" + 393 "D72B03746AE77F5E62292C311562A846505DC82DB854338A" + 394 "E49F5235C95B91178CCF2DD5CACEF403EC9D1810C6272B04" + 395 "5B3B71F9DC6B80D63FDD4A8E9ADB1E6962A69526D43161C1" + 396 "A41D570D7938DAD4A40E329CD0E40E65FFFFFFFFFFFFFFFF", 16); 397 private static final BigInteger p8192 = new BigInteger( // TLS FFDHE 398 "FFFFFFFFFFFFFFFFADF85458A2BB4A9AAFDC5620273D3CF1" + 399 "D8B9C583CE2D3695A9E13641146433FBCC939DCE249B3EF9" + 400 "7D2FE363630C75D8F681B202AEC4617AD3DF1ED5D5FD6561" + 401 "2433F51F5F066ED0856365553DED1AF3B557135E7F57C935" + 402 "984F0C70E0E68B77E2A689DAF3EFE8721DF158A136ADE735" + 403 "30ACCA4F483A797ABC0AB182B324FB61D108A94BB2C8E3FB" + 404 "B96ADAB760D7F4681D4F42A3DE394DF4AE56EDE76372BB19" + 405 "0B07A7C8EE0A6D709E02FCE1CDF7E2ECC03404CD28342F61" + 406 "9172FE9CE98583FF8E4F1232EEF28183C3FE3B1B4C6FAD73" + 407 "3BB5FCBC2EC22005C58EF1837D1683B2C6F34A26C1B2EFFA" + 408 "886B4238611FCFDCDE355B3B6519035BBC34F4DEF99C0238" + 409 "61B46FC9D6E6C9077AD91D2691F7F7EE598CB0FAC186D91C" + 410 "AEFE130985139270B4130C93BC437944F4FD4452E2D74DD3" + 411 "64F2E21E71F54BFF5CAE82AB9C9DF69EE86D2BC522363A0D" + 412 "ABC521979B0DEADA1DBF9A42D5C4484E0ABCD06BFA53DDEF" + 413 "3C1B20EE3FD59D7C25E41D2B669E1EF16E6F52C3164DF4FB" + 414 "7930E9E4E58857B6AC7D5F42D69F6D187763CF1D55034004" + 415 "87F55BA57E31CC7A7135C886EFB4318AED6A1E012D9E6832" + 416 "A907600A918130C46DC778F971AD0038092999A333CB8B7A" + 417 "1A1DB93D7140003C2A4ECEA9F98D0ACC0A8291CDCEC97DCF" + 418 "8EC9B55A7F88A46B4DB5A851F44182E1C68A007E5E0DD902" + 419 "0BFD64B645036C7A4E677D2C38532A3A23BA4442CAF53EA6" + 420 "3BB454329B7624C8917BDD64B1C0FD4CB38E8C334C701C3A" + 421 "CDAD0657FCCFEC719B1F5C3E4E46041F388147FB4CFDB477" + 422 "A52471F7A9A96910B855322EDB6340D8A00EF092350511E3" + 423 "0ABEC1FFF9E3A26E7FB29F8C183023C3587E38DA0077D9B4" + 424 "763E4E4B94B2BBC194C6651E77CAF992EEAAC0232A281BF6" + 425 "B3A739C1226116820AE8DB5847A67CBEF9C9091B462D538C" + 426 "D72B03746AE77F5E62292C311562A846505DC82DB854338A" + 427 "E49F5235C95B91178CCF2DD5CACEF403EC9D1810C6272B04" + 428 "5B3B71F9DC6B80D63FDD4A8E9ADB1E6962A69526D43161C1" + 429 "A41D570D7938DAD4A40E329CCFF46AAA36AD004CF600C838" + 430 "1E425A31D951AE64FDB23FCEC9509D43687FEB69EDD1CC5E" + 431 "0B8CC3BDF64B10EF86B63142A3AB8829555B2F747C932665" + 432 "CB2C0F1CC01BD70229388839D2AF05E454504AC78B758282" + 433 "2846C0BA35C35F5C59160CC046FD8251541FC68C9C86B022" + 434 "BB7099876A460E7451A8A93109703FEE1C217E6C3826E52C" + 435 "51AA691E0E423CFC99E9E31650C1217B624816CDAD9A95F9" + 436 "D5B8019488D9C0A0A1FE3075A577E23183F81D4A3F2FA457" + 437 "1EFC8CE0BA8A4FE8B6855DFE72B0A66EDED2FBABFBE58A30" + 438 "FAFABE1C5D71A87E2F741EF8C1FE86FEA6BBFDE530677F0D" + 439 "97D11D49F7A8443D0822E506A9F4614E011E2A94838FF88C" + 440 "D68C8BB7C5C6424CFFFFFFFFFFFFFFFF", 16); 441 442 private static final BigInteger[] supportedPrimes = { 443 p512, p768, p1024, p1536, p2048, p3072, p4096, p6144, p8192}; 444 445 // a measure of the uncertainty that prime modulus p is not a prime 446 // 447 // see BigInteger.isProbablePrime(int certainty) 448 private final static int PRIME_CERTAINTY = 120; 449 450 // the known security property, jdk.tls.server.defaultDHEParameters 451 private final static String PROPERTY_NAME = 452 "jdk.tls.server.defaultDHEParameters"; 453 454 private static final Pattern spacesPattern = Pattern.compile("\\s+"); 455 456 private final static Pattern syntaxPattern = Pattern.compile( 457 "(\\{[0-9A-Fa-f]+,[0-9A-Fa-f]+\\})" + 458 "(,\\{[0-9A-Fa-f]+,[0-9A-Fa-f]+\\})*"); 459 460 private static final Pattern paramsPattern = Pattern.compile( 461 "\\{([0-9A-Fa-f]+),([0-9A-Fa-f]+)\\}"); 462 463 // cache of predefined default DH ephemeral parameters 464 private final static Map<Integer,DHParameterSpec> definedParams; 465 466 static { 467 String property = AccessController.doPrivileged( 468 new PrivilegedAction<String>() { 469 public String run() { 470 return Security.getProperty(PROPERTY_NAME); 471 } 472 }); 473 474 if (property != null && !property.isEmpty()) { 475 // remove double quote marks from beginning/end of the property 476 if (property.length() >= 2 && property.charAt(0) == '"' && 477 property.charAt(property.length() - 1) == '"') { 478 property = property.substring(1, property.length() - 1); 479 } 480 481 property = property.trim(); 482 } 483 484 if (property != null && !property.isEmpty()) { 485 Matcher spacesMatcher = spacesPattern.matcher(property); 486 property = spacesMatcher.replaceAll(""); 487 488 if (debugIsOn) { 489 System.out.println("The Security Property " + 490 PROPERTY_NAME + ": " + property); 491 } 492 } 493 494 Map<Integer,DHParameterSpec> defaultParams = new HashMap<>(); 495 if (property != null && !property.isEmpty()) { 496 Matcher syntaxMatcher = syntaxPattern.matcher(property); 497 if (syntaxMatcher.matches()) { 498 Matcher paramsFinder = paramsPattern.matcher(property); 499 while(paramsFinder.find()) { 500 String primeModulus = paramsFinder.group(1); 501 BigInteger p = new BigInteger(primeModulus, 16); 502 if (!p.isProbablePrime(PRIME_CERTAINTY)) { 503 if (debugIsOn) { 504 System.out.println( 505 "Prime modulus p in Security Property, " + 506 PROPERTY_NAME + ", is not a prime: " + 507 primeModulus); 508 } 509 510 continue; 511 } 512 513 String baseGenerator = paramsFinder.group(2); 514 BigInteger g = new BigInteger(baseGenerator, 16); 515 516 DHParameterSpec spec = new DHParameterSpec(p, g); 517 int primeLen = p.bitLength(); 518 defaultParams.put(primeLen, spec); 519 } 520 } else if (debugIsOn) { 521 System.out.println("Invalid Security Property, " + 522 PROPERTY_NAME + ", definition"); 523 } 524 } 525 526 for (BigInteger p : supportedPrimes) { 527 int primeLen = p.bitLength(); 528 defaultParams.putIfAbsent(primeLen, 529 new DHParameterSpec(p, BigInteger.TWO)); 530 } 531 532 definedParams = 533 Collections.<Integer,DHParameterSpec>unmodifiableMap( 534 defaultParams); 535 } 536 } 537} 538