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

Print this page




  45     private final EphemeralKeyManager ephemeralKeyManager;
  46     private final SSLSessionContextImpl clientCache;
  47     private final SSLSessionContextImpl serverCache;
  48 
  49     private boolean isInitialized;
  50 
  51     private X509ExtendedKeyManager keyManager;
  52     private X509TrustManager trustManager;
  53     private SecureRandom secureRandom;
  54 
  55     // supported and default protocols
  56     private ProtocolList defaultServerProtocolList;
  57     private ProtocolList defaultClientProtocolList;
  58     private ProtocolList supportedProtocolList;
  59 
  60     // supported and default cipher suites
  61     private CipherSuiteList defaultServerCipherSuiteList;
  62     private CipherSuiteList defaultClientCipherSuiteList;
  63     private CipherSuiteList supportedCipherSuiteList;
  64 



  65     SSLContextImpl() {
  66         ephemeralKeyManager = new EphemeralKeyManager();
  67         clientCache = new SSLSessionContextImpl();
  68         serverCache = new SSLSessionContextImpl();
  69     }
  70 
  71     @Override
  72     protected void engineInit(KeyManager[] km, TrustManager[] tm,
  73                                 SecureRandom sr) throws KeyManagementException {
  74         isInitialized = false;
  75         keyManager = chooseKeyManager(km);
  76 
  77         if (tm == null) {
  78             try {
  79                 TrustManagerFactory tmf = TrustManagerFactory.getInstance(
  80                         TrustManagerFactory.getDefaultAlgorithm());
  81                 tmf.init((KeyStore)null);
  82                 tm = tmf.getTrustManagers();
  83             } catch (Exception e) {
  84                 // eat


 158                     throw new KeyManagementException
 159                         ("FIPS mode: only SunJSSE KeyManagers may be used");
 160                 }
 161             }
 162             if (km instanceof X509ExtendedKeyManager) {
 163                 return (X509ExtendedKeyManager)km;
 164             }
 165             if (debug != null && Debug.isOn("sslctx")) {
 166                 System.out.println(
 167                     "X509KeyManager passed to " +
 168                     "SSLContext.init():  need an " +
 169                     "X509ExtendedKeyManager for SSLEngine use");
 170             }
 171             return new AbstractKeyManagerWrapper((X509KeyManager)km);
 172         }
 173 
 174         // nothing found, return a dummy X509ExtendedKeyManager
 175         return DummyX509KeyManager.INSTANCE;
 176     }
 177 



 178     @Override
 179     protected SSLSocketFactory engineGetSocketFactory() {
 180         if (!isInitialized) {
 181             throw new IllegalStateException(
 182                 "SSLContextImpl is not initialized");
 183         }
 184        return new SSLSocketFactoryImpl(this);
 185     }
 186 
 187     @Override
 188     protected SSLServerSocketFactory engineGetServerSocketFactory() {
 189         if (!isInitialized) {
 190             throw new IllegalStateException("SSLContext is not initialized");
 191         }
 192         return new SSLServerSocketFactoryImpl(this);
 193     }
 194 
 195     @Override
 196     protected SSLEngine engineCreateSSLEngine() {
 197         if (!isInitialized) {
 198             throw new IllegalStateException(
 199                 "SSLContextImpl is not initialized");
 200         }
 201         return new SSLEngineImpl(this);
 202     }
 203 
 204     @Override
 205     protected SSLEngine engineCreateSSLEngine(String host, int port) {
 206         if (!isInitialized) {
 207             throw new IllegalStateException(
 208                 "SSLContextImpl is not initialized");
 209         }
 210         return new SSLEngineImpl(this, host, port);
 211     }
 212 
 213     @Override
 214     protected SSLSessionContext engineGetClientSessionContext() {
 215         return clientCache;
 216     }
 217 
 218     @Override
 219     protected SSLSessionContext engineGetServerSessionContext() {
 220         return serverCache;
 221     }
 222 
 223     SecureRandom getSecureRandom() {
 224         return secureRandom;
 225     }
 226 
 227     X509ExtendedKeyManager getX509KeyManager() {
 228         return keyManager;
 229     }
 230 
 231     X509TrustManager getX509TrustManager() {
 232         return trustManager;
 233     }
 234 
 235     EphemeralKeyManager getEphemeralKeyManager() {
 236         return ephemeralKeyManager;
 237     }
 238 

















 239     abstract SSLParameters getDefaultServerSSLParams();
 240     abstract SSLParameters getDefaultClientSSLParams();
 241     abstract SSLParameters getSupportedSSLParams();
 242 
 243     // Get supported ProtocolList.
 244     ProtocolList getSuportedProtocolList() {
 245         if (supportedProtocolList == null) {
 246             supportedProtocolList =
 247                 new ProtocolList(getSupportedSSLParams().getProtocols());
 248         }
 249 
 250         return supportedProtocolList;
 251     }
 252 
 253     // Get default ProtocolList.
 254     ProtocolList getDefaultProtocolList(boolean roleIsServer) {
 255         if (roleIsServer) {
 256             if (defaultServerProtocolList == null) {
 257                 defaultServerProtocolList = new ProtocolList(
 258                         getDefaultServerSSLParams().getProtocols());


 302             } else {
 303                 if (defaultClientCipherSuiteList == null) {
 304                     defaultClientCipherSuiteList = getApplicableCipherSuiteList(
 305                         getDefaultProtocolList(false), true);
 306                 }
 307 
 308                 return defaultClientCipherSuiteList;
 309             }
 310         }
 311     }
 312 
 313     /**
 314      * Return whether a protocol list is the original default enabled
 315      * protocols.  See: SSLSocket/SSLEngine.setEnabledProtocols()
 316      */
 317     boolean isDefaultProtocolList(ProtocolList protocols) {
 318         return (protocols == defaultServerProtocolList) ||
 319                (protocols == defaultClientProtocolList);
 320     }
 321 








 322 
 323     /*
 324      * Return the list of all available CipherSuites with a priority of
 325      * minPriority or above.
 326      */
 327     private CipherSuiteList getApplicableCipherSuiteList(
 328             ProtocolList protocols, boolean onlyEnabled) {
 329 
 330         int minPriority = CipherSuite.SUPPORTED_SUITES_PRIORITY;
 331         if (onlyEnabled) {
 332             minPriority = CipherSuite.DEFAULT_SUITES_PRIORITY;
 333         }
 334 
 335         Collection<CipherSuite> allowedCipherSuites =
 336                                     CipherSuite.allowedCipherSuites();
 337 
 338         TreeSet<CipherSuite> suites = new TreeSet<>();
 339         if (!(protocols.collection().isEmpty()) &&
 340                 protocols.min.v != ProtocolVersion.NONE.v) {
 341             for (CipherSuite suite : allowedCipherSuites) {
 342                 if (!suite.allowed || suite.priority < minPriority) {
 343                     continue;
 344                 }
 345 
 346                 if (suite.isAvailable() &&
 347                         suite.obsoleted > protocols.min.v &&
 348                         suite.supported <= protocols.max.v) {
 349                     if (SSLAlgorithmConstraints.DEFAULT.permits(
 350                             EnumSet.of(CryptoPrimitive.KEY_AGREEMENT),
 351                             suite.name, null)) {
 352                         suites.add(suite);
 353                     }
 354                 } else if (debug != null &&
 355                         Debug.isOn("sslctx") && Debug.isOn("verbose")) {
 356                     if (suite.obsoleted <= protocols.min.v) {
 357                         System.out.println(
 358                             "Ignoring obsoleted cipher suite: " + suite);
 359                     } else if (suite.supported > protocols.max.v) {
 360                         System.out.println(
 361                             "Ignoring unsupported cipher suite: " + suite);
 362                     } else {
 363                         System.out.println(
 364                             "Ignoring unavailable cipher suite: " + suite);
 365                     }
 366                 }
 367             }
 368         }
 369 
 370         return new CipherSuiteList(suites);
 371     }
 372 
 373     /**
 374      * Clear cache of available ciphersuites. If we support all ciphers
 375      * internally, there is no need to clear the cache and calling this
 376      * method has no effect.
 377      *
 378      * Note that every call to clearAvailableCache() and the maintenance of
 379      * cipher suites need to be synchronized with this instance.
 380      */
 381     private void clearAvailableCache() {
 382         if (CipherSuite.DYNAMIC_AVAILABILITY) {
 383             supportedCipherSuiteList = null;
 384             defaultServerCipherSuiteList = null;
 385             defaultClientCipherSuiteList = null;
 386             CipherSuite.BulkCipher.clearAvailableCache();
 387             JsseJce.clearEcAvailable();
 388         }
 389     }
 390 

















 391     /*
 392      * The SSLContext implementation for TLS/SSL algorithm
 393      *
 394      * SSL/TLS protocols specify the forward compatibility and version
 395      * roll-back attack protections, however, a number of SSL/TLS server
 396      * vendors did not implement these aspects properly, and some current
 397      * SSL/TLS servers may refuse to talk to a TLS 1.1 or later client.
 398      *
 399      * Considering above interoperability issues, SunJSSE will not set
 400      * TLS 1.1 and TLS 1.2 as the enabled protocols for client by default.
 401      *
 402      * For SSL/TLS servers, there is no such interoperability issues as
 403      * SSL/TLS clients. In SunJSSE, TLS 1.1 or later version will be the
 404      * enabled protocols for server by default.
 405      *
 406      * We may change the behavior when popular TLS/SSL vendors support TLS
 407      * forward compatibility properly.
 408      *
 409      * SSLv2Hello is no longer necessary.  This interoperability option was
 410      * put in place in the late 90's when SSLv3/TLS1.0 were relatively new
 411      * and there were a fair number of SSLv2-only servers deployed.  Because
 412      * of the security issues in SSLv2, it is rarely (if ever) used, as
 413      * deployments should now be using SSLv3 and TLSv1.
 414      *
 415      * Considering the issues of SSLv2Hello, we should not enable SSLv2Hello
 416      * by default. Applications still can use it by enabling SSLv2Hello with
 417      * the series of setEnabledProtocols APIs.
 418      */
 419 
 420     /*
 421      * The base abstract SSLContext implementation.

 422      *
 423      * This abstract class encapsulates supported and the default server
 424      * SSL parameters.
 425      *
 426      * @see SSLContext
 427      */
 428     private abstract static class AbstractSSLContext extends SSLContextImpl {
 429         // parameters
 430         private static final SSLParameters defaultServerSSLParams;
 431         private static final SSLParameters supportedSSLParams;
 432 
 433         static {
 434             // supported SSL parameters
 435             supportedSSLParams = new SSLParameters();
 436 
 437             // candidates for available protocols
 438             ProtocolVersion[] candidates;
 439 
 440             if (SunJSSE.isFIPS()) {
 441                 supportedSSLParams.setProtocols(new String[] {
 442                     ProtocolVersion.TLS10.name,
 443                     ProtocolVersion.TLS11.name,
 444                     ProtocolVersion.TLS12.name
 445                 });
 446 
 447                 candidates = new ProtocolVersion[] {
 448                     ProtocolVersion.TLS10,


 465                     ProtocolVersion.TLS11,
 466                     ProtocolVersion.TLS12
 467                 };
 468             }
 469 
 470             defaultServerSSLParams = new SSLParameters();
 471             defaultServerSSLParams.setProtocols(
 472                     getAvailableProtocols(candidates));
 473         }
 474 
 475         @Override
 476         SSLParameters getDefaultServerSSLParams() {
 477             return defaultServerSSLParams;
 478         }
 479 
 480         @Override
 481         SSLParameters getSupportedSSLParams() {
 482             return supportedSSLParams;
 483         }
 484 
 485         static String[] getAvailableProtocols(
 486                 ProtocolVersion[] protocolCandidates) {
 487 
 488             List<String> availableProtocols = Collections.<String>emptyList();
 489             if (protocolCandidates !=  null && protocolCandidates.length != 0) {
 490                 availableProtocols = new ArrayList<>(protocolCandidates.length);
 491                 for (ProtocolVersion p : protocolCandidates) {
 492                     if (ProtocolVersion.availableProtocols.contains(p)) {
 493                         availableProtocols.add(p.name);
 494                     }
 495                 }
 496             }
 497 
 498             return availableProtocols.toArray(new String[0]);


 499         }
 500     }
 501 
 502     /*
 503      * The SSLContext implementation for SSLv3 and TLS10 algorithm
 504      *
 505      * @see SSLContext
 506      */
 507     public static final class TLS10Context extends AbstractSSLContext {
 508         private static final SSLParameters defaultClientSSLParams;
 509 
 510         static {
 511             // candidates for available protocols
 512             ProtocolVersion[] candidates;
 513             if (SunJSSE.isFIPS()) {
 514                 candidates = new ProtocolVersion[] {
 515                     ProtocolVersion.TLS10
 516                 };
 517             } else {
 518                 candidates = new ProtocolVersion[] {
 519                     ProtocolVersion.SSL30,
 520                     ProtocolVersion.TLS10
 521                 };
 522             }
 523 
 524             defaultClientSSLParams = new SSLParameters();
 525             defaultClientSSLParams.setProtocols(
 526                     getAvailableProtocols(candidates));
 527         }
 528 
 529         @Override
 530         SSLParameters getDefaultClientSSLParams() {
 531             return defaultClientSSLParams;
 532         }
 533     }
 534 
 535     /*
 536      * The SSLContext implementation for TLS11 algorithm
 537      *
 538      * @see SSLContext
 539      */
 540     public static final class TLS11Context extends AbstractSSLContext {
 541         private static final SSLParameters defaultClientSSLParams;
 542 
 543         static {
 544             // candidates for available protocols
 545             ProtocolVersion[] candidates;
 546             if (SunJSSE.isFIPS()) {
 547                 candidates = new ProtocolVersion[] {
 548                     ProtocolVersion.TLS10,
 549                     ProtocolVersion.TLS11
 550                 };
 551             } else {
 552                 candidates = new ProtocolVersion[] {
 553                     ProtocolVersion.SSL30,
 554                     ProtocolVersion.TLS10,
 555                     ProtocolVersion.TLS11
 556                 };
 557             }
 558 
 559             defaultClientSSLParams = new SSLParameters();
 560             defaultClientSSLParams.setProtocols(
 561                     getAvailableProtocols(candidates));
 562         }
 563 
 564         @Override
 565         SSLParameters getDefaultClientSSLParams() {
 566             return defaultClientSSLParams;
 567         }
 568     }
 569 
 570     /*
 571      * The SSLContext implementation for TLS12 algorithm
 572      *
 573      * @see SSLContext
 574      */
 575     public static final class TLS12Context extends AbstractSSLContext {
 576         private static final SSLParameters defaultClientSSLParams;
 577 
 578         static {
 579             // candidates for available protocols
 580             ProtocolVersion[] candidates;
 581             if (SunJSSE.isFIPS()) {
 582                 candidates = new ProtocolVersion[] {
 583                     ProtocolVersion.TLS10,
 584                     ProtocolVersion.TLS11,
 585                     ProtocolVersion.TLS12
 586                 };
 587             } else {
 588                 candidates = new ProtocolVersion[] {
 589                     ProtocolVersion.SSL30,
 590                     ProtocolVersion.TLS10,
 591                     ProtocolVersion.TLS11,
 592                     ProtocolVersion.TLS12
 593                 };
 594             }
 595 
 596             defaultClientSSLParams = new SSLParameters();
 597             defaultClientSSLParams.setProtocols(
 598                     getAvailableProtocols(candidates));
 599         }
 600 
 601         @Override
 602         SSLParameters getDefaultClientSSLParams() {
 603             return defaultClientSSLParams;
 604         }
 605     }
 606 
 607     /*





























































 608      * The SSLContext implementation for customized TLS protocols
 609      *
 610      * @see SSLContext
 611      */
 612     private static class CustomizedSSLContext extends AbstractSSLContext {
 613         private static final String PROPERTY_NAME = "jdk.tls.client.protocols";
 614         private static final SSLParameters defaultClientSSLParams;
 615         private static IllegalArgumentException reservedException = null;
 616 
 617         // Don't want a java.lang.LinkageError for illegal system property.
 618         //
 619         // Please don't throw exception in this static block.  Otherwise,
 620         // java.lang.LinkageError may be thrown during the instantiation of
 621         // the provider service. Instead, let's handle the initialization
 622         // exception in constructor.
 623         static {











 624             // candidates for available protocols
 625             ProtocolVersion[] candidates;
 626 
 627             String property = AccessController.doPrivileged(
 628                     new GetPropertyAction(PROPERTY_NAME));
 629             if (property == null || property.length() == 0) {
 630                 // the default enabled client TLS protocols
 631                 if (SunJSSE.isFIPS()) {
 632                     candidates = new ProtocolVersion[] {
 633                         ProtocolVersion.TLS10,
 634                         ProtocolVersion.TLS11,
 635                         ProtocolVersion.TLS12
 636                     };
 637                 } else {
 638                     candidates = new ProtocolVersion[] {
 639                         ProtocolVersion.SSL30,
 640                         ProtocolVersion.TLS10,
 641                         ProtocolVersion.TLS11,
 642                         ProtocolVersion.TLS12
 643                     };
 644                 }
 645             } else {
 646                 // remove double quote marks from beginning/end of the property
 647                 if (property.length() > 1 && property.charAt(0) == '"' &&
 648                         property.charAt(property.length() - 1) == '"') {
 649                     property = property.substring(1, property.length() - 1);
 650                 }
 651 
 652                 String[] protocols = null;
 653                 if (property != null && property.length() != 0) {
 654                     protocols = property.split(",");
 655                 } else {
 656                     reservedException = new IllegalArgumentException(
 657                         "No protocol specified in " +
 658                         PROPERTY_NAME + " system property");
 659                     protocols = new String[0];
 660                 }
 661 
 662                 candidates = new ProtocolVersion[protocols.length];
 663                 for (int i = 0; i < protocols.length; i++) {
 664                     protocols[i] = protocols[i].trim();
 665                     // Is it a supported protocol name?
 666                     try {
 667                         candidates[i] = ProtocolVersion.valueOf(protocols[i]);
 668                     } catch (IllegalArgumentException iae) {
 669                         reservedException = new IllegalArgumentException(
 670                             PROPERTY_NAME + ": " + protocols[i] +
 671                             " is not a standard SSL/TLS protocol name", iae);
 672                         break;
 673                     }
 674                 }
 675 
 676                 if ((reservedException == null) && SunJSSE.isFIPS()) {
 677                     for (ProtocolVersion protocolVersion : candidates) {
 678                         if (ProtocolVersion.SSL20Hello.v == protocolVersion.v ||
 679                                 ProtocolVersion.SSL30.v == protocolVersion.v) {
 680                             reservedException = new IllegalArgumentException(
 681                                     PROPERTY_NAME + ": " + protocolVersion +
 682                                     " is not FIPS compliant");
 683                         }
 684                     }
 685                 }
 686             }
 687 
 688             defaultClientSSLParams = new SSLParameters();
 689             if (reservedException == null) {
 690                 defaultClientSSLParams.setProtocols(
 691                         getAvailableProtocols(candidates));


 692             }
 693         }
 694 
 695         protected CustomizedSSLContext() {
 696             if (reservedException != null) {
 697                 throw reservedException;
 698             }
 699         }
 700 
 701         @Override
 702         SSLParameters getDefaultClientSSLParams() {
 703             return defaultClientSSLParams;
 704         }
 705     }
 706 
 707     /*
 708      * The SSLContext implementation for default "TLS" algorithm
 709      *
 710      * @see SSLContext
 711      */
 712     public static final class TLSContext extends CustomizedSSLContext {
 713         // use the default constructor and methods
 714     }
 715 
 716     /*
 717      * The SSLContext implementation for default "Default" algorithm
 718      *
 719      * @see SSLContext
 720      */
 721     public static final class DefaultSSLContext extends CustomizedSSLContext {
 722         private static final String NONE = "NONE";
 723         private static final String P11KEYSTORE = "PKCS11";
 724 
 725         private static volatile SSLContextImpl defaultImpl;
 726 
 727         private static TrustManager[] defaultTrustManagers;
 728         private static KeyManager[] defaultKeyManagers;
 729 
 730         public DefaultSSLContext() throws Exception {
 731             try {
 732                 super.engineInit(getDefaultKeyManager(),
 733                         getDefaultTrustManager(), null);
 734             } catch (Exception e) {
 735                 if (debug != null && Debug.isOn("defaultctx")) {
 736                     System.out.println("default context init failed: " + e);
 737                 }
 738                 throw e;
 739             }
 740 
 741             if (defaultImpl == null) {


 862              * Try to initialize key manager.
 863              */
 864             if (debug != null && Debug.isOn("defaultctx")) {
 865                 System.out.println("init keymanager of type " +
 866                     KeyManagerFactory.getDefaultAlgorithm());
 867             }
 868             KeyManagerFactory kmf = KeyManagerFactory.getInstance(
 869                 KeyManagerFactory.getDefaultAlgorithm());
 870 
 871             if (P11KEYSTORE.equals(defaultKeyStoreType)) {
 872                 kmf.init(ks, null); // do not pass key passwd if using token
 873             } else {
 874                 kmf.init(ks, passwd);
 875             }
 876 
 877             defaultKeyManagers = kmf.getKeyManagers();
 878             return defaultKeyManagers;
 879         }
 880     }
 881 



























































































































































































 882 }
 883 
 884 
 885 final class AbstractTrustManagerWrapper extends X509ExtendedTrustManager
 886             implements X509TrustManager {
 887 
 888     // the delegated trust manager
 889     private final X509TrustManager tm;
 890 
 891     AbstractTrustManagerWrapper(X509TrustManager tm) {
 892         this.tm = tm;
 893     }
 894 
 895     @Override
 896     public void checkClientTrusted(X509Certificate[] chain, String authType)
 897         throws CertificateException {
 898         tm.checkClientTrusted(chain, authType);
 899     }
 900 
 901     @Override


 944 
 945             SSLSocket sslSocket = (SSLSocket)socket;
 946             SSLSession session = sslSocket.getHandshakeSession();
 947             if (session == null) {
 948                 throw new CertificateException("No handshake session");
 949             }
 950 
 951             // check endpoint identity
 952             String identityAlg = sslSocket.getSSLParameters().
 953                                         getEndpointIdentificationAlgorithm();
 954             if (identityAlg != null && identityAlg.length() != 0) {
 955                 String hostname = session.getPeerHost();
 956                 X509TrustManagerImpl.checkIdentity(
 957                                     hostname, chain[0], identityAlg);
 958             }
 959 
 960             // try the best to check the algorithm constraints
 961             ProtocolVersion protocolVersion =
 962                 ProtocolVersion.valueOf(session.getProtocol());
 963             AlgorithmConstraints constraints = null;
 964             if (protocolVersion.v >= ProtocolVersion.TLS12.v) {
 965                 if (session instanceof ExtendedSSLSession) {
 966                     ExtendedSSLSession extSession =
 967                                     (ExtendedSSLSession)session;
 968                     String[] peerSupportedSignAlgs =
 969                             extSession.getLocalSupportedSignatureAlgorithms();
 970 
 971                     constraints = new SSLAlgorithmConstraints(
 972                                     sslSocket, peerSupportedSignAlgs, true);
 973                 } else {
 974                     constraints =
 975                             new SSLAlgorithmConstraints(sslSocket, true);
 976                 }
 977             } else {
 978                 constraints = new SSLAlgorithmConstraints(sslSocket, true);
 979             }
 980 
 981             checkAlgorithmConstraints(chain, constraints);
 982         }
 983     }
 984 


 986             SSLEngine engine, boolean isClient) throws CertificateException {
 987         if (engine != null) {
 988             SSLSession session = engine.getHandshakeSession();
 989             if (session == null) {
 990                 throw new CertificateException("No handshake session");
 991             }
 992 
 993             // check endpoint identity
 994             String identityAlg = engine.getSSLParameters().
 995                                         getEndpointIdentificationAlgorithm();
 996             if (identityAlg != null && identityAlg.length() != 0) {
 997                 String hostname = session.getPeerHost();
 998                 X509TrustManagerImpl.checkIdentity(
 999                                     hostname, chain[0], identityAlg);
1000             }
1001 
1002             // try the best to check the algorithm constraints
1003             ProtocolVersion protocolVersion =
1004                 ProtocolVersion.valueOf(session.getProtocol());
1005             AlgorithmConstraints constraints = null;
1006             if (protocolVersion.v >= ProtocolVersion.TLS12.v) {
1007                 if (session instanceof ExtendedSSLSession) {
1008                     ExtendedSSLSession extSession =
1009                                     (ExtendedSSLSession)session;
1010                     String[] peerSupportedSignAlgs =
1011                             extSession.getLocalSupportedSignatureAlgorithms();
1012 
1013                     constraints = new SSLAlgorithmConstraints(
1014                                     engine, peerSupportedSignAlgs, true);
1015                 } else {
1016                     constraints =
1017                             new SSLAlgorithmConstraints(engine, true);
1018                 }
1019             } else {
1020                 constraints = new SSLAlgorithmConstraints(engine, true);
1021             }
1022 
1023             checkAlgorithmConstraints(chain, constraints);
1024         }
1025     }
1026 




  45     private final EphemeralKeyManager ephemeralKeyManager;
  46     private final SSLSessionContextImpl clientCache;
  47     private final SSLSessionContextImpl serverCache;
  48 
  49     private boolean isInitialized;
  50 
  51     private X509ExtendedKeyManager keyManager;
  52     private X509TrustManager trustManager;
  53     private SecureRandom secureRandom;
  54 
  55     // supported and default protocols
  56     private ProtocolList defaultServerProtocolList;
  57     private ProtocolList defaultClientProtocolList;
  58     private ProtocolList supportedProtocolList;
  59 
  60     // supported and default cipher suites
  61     private CipherSuiteList defaultServerCipherSuiteList;
  62     private CipherSuiteList defaultClientCipherSuiteList;
  63     private CipherSuiteList supportedCipherSuiteList;
  64 
  65     // DTLS cookie exchange manager
  66     private HelloCookieManager helloCookieManager;
  67 
  68     SSLContextImpl() {
  69         ephemeralKeyManager = new EphemeralKeyManager();
  70         clientCache = new SSLSessionContextImpl();
  71         serverCache = new SSLSessionContextImpl();
  72     }
  73 
  74     @Override
  75     protected void engineInit(KeyManager[] km, TrustManager[] tm,
  76                                 SecureRandom sr) throws KeyManagementException {
  77         isInitialized = false;
  78         keyManager = chooseKeyManager(km);
  79 
  80         if (tm == null) {
  81             try {
  82                 TrustManagerFactory tmf = TrustManagerFactory.getInstance(
  83                         TrustManagerFactory.getDefaultAlgorithm());
  84                 tmf.init((KeyStore)null);
  85                 tm = tmf.getTrustManagers();
  86             } catch (Exception e) {
  87                 // eat


 161                     throw new KeyManagementException
 162                         ("FIPS mode: only SunJSSE KeyManagers may be used");
 163                 }
 164             }
 165             if (km instanceof X509ExtendedKeyManager) {
 166                 return (X509ExtendedKeyManager)km;
 167             }
 168             if (debug != null && Debug.isOn("sslctx")) {
 169                 System.out.println(
 170                     "X509KeyManager passed to " +
 171                     "SSLContext.init():  need an " +
 172                     "X509ExtendedKeyManager for SSLEngine use");
 173             }
 174             return new AbstractKeyManagerWrapper((X509KeyManager)km);
 175         }
 176 
 177         // nothing found, return a dummy X509ExtendedKeyManager
 178         return DummyX509KeyManager.INSTANCE;
 179     }
 180 
 181     abstract SSLEngine createSSLEngineImpl();
 182     abstract SSLEngine createSSLEngineImpl(String host, int port);
 183 
 184     @Override
 185     protected SSLEngine engineCreateSSLEngine() {
 186         if (!isInitialized) {
 187             throw new IllegalStateException("SSLContext is not initialized");

 188         }
 189         return createSSLEngineImpl();
 190     }
 191 
 192     @Override
 193     protected SSLEngine engineCreateSSLEngine(String host, int port) {
 194         if (!isInitialized) {
 195             throw new IllegalStateException("SSLContext is not initialized");
 196         }
 197         return createSSLEngineImpl(host, port);
 198     }
 199 
 200     @Override
 201     protected SSLSocketFactory engineGetSocketFactory() {
 202         if (!isInitialized) {
 203             throw new IllegalStateException("SSLContext is not initialized");

 204         }
 205        return new SSLSocketFactoryImpl(this);
 206     }
 207 
 208     @Override
 209     protected SSLServerSocketFactory engineGetServerSocketFactory() {
 210         if (!isInitialized) {
 211             throw new IllegalStateException("SSLContext is not initialized");

 212         }
 213         return new SSLServerSocketFactoryImpl(this);
 214     }
 215 
 216     @Override
 217     protected SSLSessionContext engineGetClientSessionContext() {
 218         return clientCache;
 219     }
 220 
 221     @Override
 222     protected SSLSessionContext engineGetServerSessionContext() {
 223         return serverCache;
 224     }
 225 
 226     SecureRandom getSecureRandom() {
 227         return secureRandom;
 228     }
 229 
 230     X509ExtendedKeyManager getX509KeyManager() {
 231         return keyManager;
 232     }
 233 
 234     X509TrustManager getX509TrustManager() {
 235         return trustManager;
 236     }
 237 
 238     EphemeralKeyManager getEphemeralKeyManager() {
 239         return ephemeralKeyManager;
 240     }
 241 
 242     HelloCookieManager getHelloCookieManager() {
 243         if (!isInitialized) {
 244             throw new IllegalStateException("SSLContext is not initialized");
 245         }
 246 
 247         if (helloCookieManager == null) {
 248             helloCookieManager = getHelloCookieManager(secureRandom);
 249         }
 250 
 251         return helloCookieManager;
 252     }
 253 
 254     HelloCookieManager getHelloCookieManager(SecureRandom secureRandom) {
 255         throw new UnsupportedOperationException(
 256                 "Cookie exchange applies to DTLS only");
 257     }
 258 
 259     abstract SSLParameters getDefaultServerSSLParams();
 260     abstract SSLParameters getDefaultClientSSLParams();
 261     abstract SSLParameters getSupportedSSLParams();
 262 
 263     // Get supported ProtocolList.
 264     ProtocolList getSuportedProtocolList() {
 265         if (supportedProtocolList == null) {
 266             supportedProtocolList =
 267                 new ProtocolList(getSupportedSSLParams().getProtocols());
 268         }
 269 
 270         return supportedProtocolList;
 271     }
 272 
 273     // Get default ProtocolList.
 274     ProtocolList getDefaultProtocolList(boolean roleIsServer) {
 275         if (roleIsServer) {
 276             if (defaultServerProtocolList == null) {
 277                 defaultServerProtocolList = new ProtocolList(
 278                         getDefaultServerSSLParams().getProtocols());


 322             } else {
 323                 if (defaultClientCipherSuiteList == null) {
 324                     defaultClientCipherSuiteList = getApplicableCipherSuiteList(
 325                         getDefaultProtocolList(false), true);
 326                 }
 327 
 328                 return defaultClientCipherSuiteList;
 329             }
 330         }
 331     }
 332 
 333     /**
 334      * Return whether a protocol list is the original default enabled
 335      * protocols.  See: SSLSocket/SSLEngine.setEnabledProtocols()
 336      */
 337     boolean isDefaultProtocolList(ProtocolList protocols) {
 338         return (protocols == defaultServerProtocolList) ||
 339                (protocols == defaultClientProtocolList);
 340     }
 341 
 342     /**
 343      * Return whether a protocol list is the original default enabled
 344      * protocols.  See: SSLSocket/SSLEngine.setEnabledProtocols()
 345      */
 346     boolean isDefaultCipherSuiteList(CipherSuiteList cipherSuites) {
 347         return (cipherSuites == defaultClientCipherSuiteList) ||
 348                (cipherSuites == defaultServerCipherSuiteList);
 349     }
 350 
 351     /*
 352      * Return the list of all available CipherSuites with a priority of
 353      * minPriority or above.
 354      */
 355     private static CipherSuiteList getApplicableCipherSuiteList(
 356             ProtocolList protocols, boolean onlyEnabled) {
 357 
 358         int minPriority = CipherSuite.SUPPORTED_SUITES_PRIORITY;
 359         if (onlyEnabled) {
 360             minPriority = CipherSuite.DEFAULT_SUITES_PRIORITY;
 361         }
 362 
 363         Collection<CipherSuite> allowedCipherSuites =
 364                                     CipherSuite.allowedCipherSuites();
 365 
 366         TreeSet<CipherSuite> suites = new TreeSet<>();
 367         if (!(protocols.collection().isEmpty()) &&
 368                 protocols.min.v != ProtocolVersion.NONE.v) {
 369             for (CipherSuite suite : allowedCipherSuites) {
 370                 if (!suite.allowed || suite.priority < minPriority) {
 371                     continue;
 372                 }
 373 
 374                 if (suite.isAvailable() &&
 375                         !protocols.min.obsoletes(suite) &&
 376                         protocols.max.supports(suite)) {
 377                     if (SSLAlgorithmConstraints.DEFAULT.permits(
 378                             EnumSet.of(CryptoPrimitive.KEY_AGREEMENT),
 379                             suite.name, null)) {
 380                         suites.add(suite);
 381                     }
 382                 } else if (debug != null &&
 383                         Debug.isOn("sslctx") && Debug.isOn("verbose")) {
 384                     if (protocols.min.obsoletes(suite)) {
 385                         System.out.println(
 386                             "Ignoring obsoleted cipher suite: " + suite);
 387                     } else if (!protocols.max.supports(suite)) {
 388                         System.out.println(
 389                             "Ignoring unsupported cipher suite: " + suite);
 390                     } else {
 391                         System.out.println(
 392                             "Ignoring unavailable cipher suite: " + suite);
 393                     }
 394                 }
 395             }
 396         }
 397 
 398         return new CipherSuiteList(suites);
 399     }
 400 
 401     /**
 402      * Clear cache of available ciphersuites. If we support all ciphers
 403      * internally, there is no need to clear the cache and calling this
 404      * method has no effect.
 405      *
 406      * Note that every call to clearAvailableCache() and the maintenance of
 407      * cipher suites need to be synchronized with this instance.
 408      */
 409     private void clearAvailableCache() {
 410         if (CipherSuite.DYNAMIC_AVAILABILITY) {
 411             supportedCipherSuiteList = null;
 412             defaultServerCipherSuiteList = null;
 413             defaultClientCipherSuiteList = null;
 414             CipherSuite.BulkCipher.clearAvailableCache();
 415             JsseJce.clearEcAvailable();
 416         }
 417     }
 418 
 419     private static String[] getAvailableProtocols(
 420             ProtocolVersion[] protocolCandidates) {
 421 
 422         List<String> availableProtocols = Collections.<String>emptyList();
 423         if (protocolCandidates !=  null && protocolCandidates.length != 0) {
 424             availableProtocols = new ArrayList<>(protocolCandidates.length);
 425             for (ProtocolVersion p : protocolCandidates) {
 426                 if (ProtocolVersion.availableProtocols.contains(p)) {
 427                     availableProtocols.add(p.name);
 428                 }
 429             }
 430         }
 431 
 432         return availableProtocols.toArray(new String[0]);
 433     }
 434 
 435 
 436     /*
 437      * The SSLContext implementation for SSL/(D)TLS algorithm
 438      *
 439      * SSL/TLS protocols specify the forward compatibility and version
 440      * roll-back attack protections, however, a number of SSL/TLS server
 441      * vendors did not implement these aspects properly, and some current
 442      * SSL/TLS servers may refuse to talk to a TLS 1.1 or later client.
 443      *
 444      * Considering above interoperability issues, SunJSSE will not set
 445      * TLS 1.1 and TLS 1.2 as the enabled protocols for client by default.
 446      *
 447      * For SSL/TLS servers, there is no such interoperability issues as
 448      * SSL/TLS clients. In SunJSSE, TLS 1.1 or later version will be the
 449      * enabled protocols for server by default.
 450      *
 451      * We may change the behavior when popular TLS/SSL vendors support TLS
 452      * forward compatibility properly.
 453      *
 454      * SSLv2Hello is no longer necessary.  This interoperability option was
 455      * put in place in the late 90's when SSLv3/TLS1.0 were relatively new
 456      * and there were a fair number of SSLv2-only servers deployed.  Because
 457      * of the security issues in SSLv2, it is rarely (if ever) used, as
 458      * deployments should now be using SSLv3 and TLSv1.
 459      *
 460      * Considering the issues of SSLv2Hello, we should not enable SSLv2Hello
 461      * by default. Applications still can use it by enabling SSLv2Hello with
 462      * the series of setEnabledProtocols APIs.
 463      */
 464 
 465     /*
 466      * The base abstract SSLContext implementation for the Transport Layer
 467      * Security (TLS) protocols.
 468      *
 469      * This abstract class encapsulates supported and the default server
 470      * SSL/TLS parameters.
 471      *
 472      * @see SSLContext
 473      */
 474     private abstract static class AbstractTLSContext extends SSLContextImpl {
 475         // parameters
 476         private static final SSLParameters defaultServerSSLParams;
 477         private static final SSLParameters supportedSSLParams;
 478 
 479         static {
 480             // supported SSL parameters
 481             supportedSSLParams = new SSLParameters();
 482 
 483             // candidates for available protocols
 484             ProtocolVersion[] candidates;
 485 
 486             if (SunJSSE.isFIPS()) {
 487                 supportedSSLParams.setProtocols(new String[] {
 488                     ProtocolVersion.TLS10.name,
 489                     ProtocolVersion.TLS11.name,
 490                     ProtocolVersion.TLS12.name
 491                 });
 492 
 493                 candidates = new ProtocolVersion[] {
 494                     ProtocolVersion.TLS10,


 511                     ProtocolVersion.TLS11,
 512                     ProtocolVersion.TLS12
 513                 };
 514             }
 515 
 516             defaultServerSSLParams = new SSLParameters();
 517             defaultServerSSLParams.setProtocols(
 518                     getAvailableProtocols(candidates));
 519         }
 520 
 521         @Override
 522         SSLParameters getDefaultServerSSLParams() {
 523             return defaultServerSSLParams;
 524         }
 525 
 526         @Override
 527         SSLParameters getSupportedSSLParams() {
 528             return supportedSSLParams;
 529         }
 530 
 531         @Override
 532         SSLEngine createSSLEngineImpl() {
 533             return new SSLEngineImpl(this, false);






 534         }


 535 
 536         @Override
 537         SSLEngine createSSLEngineImpl(String host, int port) {
 538             return new SSLEngineImpl(this, host, port, false);
 539         }
 540     }
 541 
 542     /*
 543      * The SSLContext implementation for SSLv3 and TLS10 algorithm
 544      *
 545      * @see SSLContext
 546      */
 547     public static final class TLS10Context extends AbstractTLSContext {
 548         private static final SSLParameters defaultClientSSLParams;
 549 
 550         static {
 551             // candidates for available protocols
 552             ProtocolVersion[] candidates;
 553             if (SunJSSE.isFIPS()) {
 554                 candidates = new ProtocolVersion[] {
 555                     ProtocolVersion.TLS10
 556                 };
 557             } else {
 558                 candidates = new ProtocolVersion[] {
 559                     ProtocolVersion.SSL30,
 560                     ProtocolVersion.TLS10
 561                 };
 562             }
 563 
 564             defaultClientSSLParams = new SSLParameters();
 565             defaultClientSSLParams.setProtocols(
 566                     getAvailableProtocols(candidates));
 567         }
 568 
 569         @Override
 570         SSLParameters getDefaultClientSSLParams() {
 571             return defaultClientSSLParams;
 572         }
 573     }
 574 
 575     /*
 576      * The SSLContext implementation for TLS11 algorithm
 577      *
 578      * @see SSLContext
 579      */
 580     public static final class TLS11Context extends AbstractTLSContext {
 581         private static final SSLParameters defaultClientSSLParams;
 582 
 583         static {
 584             // candidates for available protocols
 585             ProtocolVersion[] candidates;
 586             if (SunJSSE.isFIPS()) {
 587                 candidates = new ProtocolVersion[] {
 588                     ProtocolVersion.TLS10,
 589                     ProtocolVersion.TLS11
 590                 };
 591             } else {
 592                 candidates = new ProtocolVersion[] {
 593                     ProtocolVersion.SSL30,
 594                     ProtocolVersion.TLS10,
 595                     ProtocolVersion.TLS11
 596                 };
 597             }
 598 
 599             defaultClientSSLParams = new SSLParameters();
 600             defaultClientSSLParams.setProtocols(
 601                     getAvailableProtocols(candidates));
 602         }
 603 
 604         @Override
 605         SSLParameters getDefaultClientSSLParams() {
 606             return defaultClientSSLParams;
 607         }
 608     }
 609 
 610     /*
 611      * The SSLContext implementation for TLS12 algorithm
 612      *
 613      * @see SSLContext
 614      */
 615     public static final class TLS12Context extends AbstractTLSContext {
 616         private static final SSLParameters defaultClientSSLParams;
 617 
 618         static {
 619             // candidates for available protocols
 620             ProtocolVersion[] candidates;
 621             if (SunJSSE.isFIPS()) {
 622                 candidates = new ProtocolVersion[] {
 623                     ProtocolVersion.TLS10,
 624                     ProtocolVersion.TLS11,
 625                     ProtocolVersion.TLS12
 626                 };
 627             } else {
 628                 candidates = new ProtocolVersion[] {
 629                     ProtocolVersion.SSL30,
 630                     ProtocolVersion.TLS10,
 631                     ProtocolVersion.TLS11,
 632                     ProtocolVersion.TLS12
 633                 };
 634             }
 635 
 636             defaultClientSSLParams = new SSLParameters();
 637             defaultClientSSLParams.setProtocols(
 638                     getAvailableProtocols(candidates));
 639         }
 640 
 641         @Override
 642         SSLParameters getDefaultClientSSLParams() {
 643             return defaultClientSSLParams;
 644         }
 645     }
 646 
 647     /*
 648      * The interface for the customized SSL/(D)TLS SSLContext.
 649      *
 650      * @see SSLContext
 651      */
 652     private static class CustomizedSSLProtocols {
 653         private final static String PROPERTY_NAME = "jdk.tls.client.protocols";
 654         static IllegalArgumentException reservedException = null;
 655         static ArrayList<ProtocolVersion>
 656                                 customizedProtocols = new ArrayList<>();
 657 
 658         // Don't want a java.lang.LinkageError for illegal system property.
 659         //
 660         // Please don't throw exception in this static block.  Otherwise,
 661         // java.lang.LinkageError may be thrown during the instantiation of
 662         // the provider service. Instead, please handle the initialization
 663         // exception in the caller's constructor.
 664         static {
 665             String property = AccessController.doPrivileged(
 666                     new GetPropertyAction(PROPERTY_NAME));
 667             if (property != null && property.length() != 0) {
 668                 // remove double quote marks from beginning/end of the property
 669                 if (property.length() > 1 && property.charAt(0) == '"' &&
 670                         property.charAt(property.length() - 1) == '"') {
 671                     property = property.substring(1, property.length() - 1);
 672                 }
 673             }
 674 
 675             if (property != null && property.length() != 0) {
 676                 String[] protocols = property.split(",");
 677                 for (int i = 0; i < protocols.length; i++) {
 678                     protocols[i] = protocols[i].trim();
 679                     // Is it a supported protocol name?
 680                     try {
 681                         ProtocolVersion pro =
 682                                 ProtocolVersion.valueOf(protocols[i]);
 683 
 684                         if (SunJSSE.isFIPS() &&
 685                                 ((pro.v == ProtocolVersion.SSL30.v) ||
 686                                  (pro.v == ProtocolVersion.SSL20Hello.v))) {
 687                             reservedException = new IllegalArgumentException(
 688                                     PROPERTY_NAME + ": " + pro +
 689                                     " is not FIPS compliant");
 690 
 691                             break;
 692                         }
 693 
 694                         // ignore duplicated protocols
 695                         if (!customizedProtocols.contains(pro)) {
 696                             customizedProtocols.add(pro);
 697                         }
 698                     } catch (IllegalArgumentException iae) {
 699                         reservedException = new IllegalArgumentException(
 700                                 PROPERTY_NAME + ": " + protocols[i] +
 701                                 " is not a standard SSL protocol name", iae);
 702                     }
 703                 }
 704             }
 705         }
 706     }
 707 
 708     /*
 709      * The SSLContext implementation for customized TLS protocols
 710      *
 711      * @see SSLContext
 712      */
 713     private static class CustomizedTLSContext extends AbstractTLSContext {
 714 
 715         private static final SSLParameters defaultClientSSLParams;
 716         private static IllegalArgumentException reservedException = null;
 717 
 718         // Don't want a java.lang.LinkageError for illegal system property.
 719         //
 720         // Please don't throw exception in this static block.  Otherwise,
 721         // java.lang.LinkageError may be thrown during the instantiation of
 722         // the provider service. Instead, let's handle the initialization
 723         // exception in constructor.
 724         static {
 725             reservedException = CustomizedSSLProtocols.reservedException;
 726             if (reservedException == null) {
 727                 ArrayList<ProtocolVersion>
 728                         customizedTLSProtocols = new ArrayList<>();
 729                 for (ProtocolVersion protocol :
 730                         CustomizedSSLProtocols.customizedProtocols) {
 731                     if (!protocol.isDTLSProtocol()) {
 732                         customizedTLSProtocols.add(protocol);
 733                     }
 734                 }
 735 
 736                 // candidates for available protocols
 737                 ProtocolVersion[] candidates;
 738                 if (customizedTLSProtocols.isEmpty()) {
 739                     // Use the default enabled client protocols if no
 740                     // customized TLS protocols.


 741                     if (SunJSSE.isFIPS()) {
 742                         candidates = new ProtocolVersion[] {
 743                             ProtocolVersion.TLS10,
 744                             ProtocolVersion.TLS11,
 745                             ProtocolVersion.TLS12
 746                         };
 747                     } else {
 748                         candidates = new ProtocolVersion[] {
 749                             ProtocolVersion.SSL30,
 750                             ProtocolVersion.TLS10,
 751                             ProtocolVersion.TLS11,
 752                             ProtocolVersion.TLS12
 753                         };
 754                     }
 755                 } else {
 756                     // Use the customized TLS protocols.
 757                     candidates =
 758                             new ProtocolVersion[customizedTLSProtocols.size()];
 759                     candidates = customizedTLSProtocols.toArray(candidates);
 760                 }
 761 




































 762                 defaultClientSSLParams = new SSLParameters();

 763                 defaultClientSSLParams.setProtocols(
 764                         getAvailableProtocols(candidates));
 765             } else {
 766                 defaultClientSSLParams = null;  // unlikely to be used
 767             }
 768         }
 769 
 770         protected CustomizedTLSContext() {
 771             if (reservedException != null) {
 772                 throw reservedException;
 773             }
 774         }
 775 
 776         @Override
 777         SSLParameters getDefaultClientSSLParams() {
 778             return defaultClientSSLParams;
 779         }
 780     }
 781 
 782     /*
 783      * The SSLContext implementation for default "TLS" algorithm
 784      *
 785      * @see SSLContext
 786      */
 787     public static final class TLSContext extends CustomizedTLSContext {
 788         // use the default constructor and methods
 789     }
 790 
 791     /*
 792      * The SSLContext implementation for default "Default" algorithm
 793      *
 794      * @see SSLContext
 795      */
 796     public static final class DefaultSSLContext extends CustomizedTLSContext {
 797         private static final String NONE = "NONE";
 798         private static final String P11KEYSTORE = "PKCS11";
 799 
 800         private static volatile SSLContextImpl defaultImpl;
 801 
 802         private static TrustManager[] defaultTrustManagers;
 803         private static KeyManager[] defaultKeyManagers;
 804 
 805         public DefaultSSLContext() throws Exception {
 806             try {
 807                 super.engineInit(getDefaultKeyManager(),
 808                         getDefaultTrustManager(), null);
 809             } catch (Exception e) {
 810                 if (debug != null && Debug.isOn("defaultctx")) {
 811                     System.out.println("default context init failed: " + e);
 812                 }
 813                 throw e;
 814             }
 815 
 816             if (defaultImpl == null) {


 937              * Try to initialize key manager.
 938              */
 939             if (debug != null && Debug.isOn("defaultctx")) {
 940                 System.out.println("init keymanager of type " +
 941                     KeyManagerFactory.getDefaultAlgorithm());
 942             }
 943             KeyManagerFactory kmf = KeyManagerFactory.getInstance(
 944                 KeyManagerFactory.getDefaultAlgorithm());
 945 
 946             if (P11KEYSTORE.equals(defaultKeyStoreType)) {
 947                 kmf.init(ks, null); // do not pass key passwd if using token
 948             } else {
 949                 kmf.init(ks, passwd);
 950             }
 951 
 952             defaultKeyManagers = kmf.getKeyManagers();
 953             return defaultKeyManagers;
 954         }
 955     }
 956 
 957     /*
 958      * The base abstract SSLContext implementation for the Datagram Transport
 959      * Layer Security (DTLS) protocols.
 960      *
 961      * This abstract class encapsulates supported and the default server DTLS
 962      * parameters.
 963      *
 964      * @see SSLContext
 965      */
 966     private abstract static class AbstractDTLSContext extends SSLContextImpl {
 967         // parameters
 968         private static final SSLParameters defaultServerSSLParams;
 969         private static final SSLParameters supportedSSLParams;
 970 
 971         static {
 972             // supported SSL parameters
 973             supportedSSLParams = new SSLParameters();
 974 
 975             // Both DTLSv1.0 and DTLSv1.2 can be used in FIPS mode.
 976             supportedSSLParams.setProtocols(new String[] {
 977                 ProtocolVersion.DTLS10.name,
 978                 ProtocolVersion.DTLS12.name
 979             });
 980 
 981             // candidates for available protocols
 982             ProtocolVersion[] candidates = new ProtocolVersion[] {
 983                 ProtocolVersion.DTLS10,
 984                 ProtocolVersion.DTLS12
 985             };
 986 
 987             defaultServerSSLParams = new SSLParameters();
 988             defaultServerSSLParams.setProtocols(
 989                     getAvailableProtocols(candidates));
 990         }
 991 
 992         @Override
 993         SSLParameters getDefaultServerSSLParams() {
 994             return defaultServerSSLParams;
 995         }
 996 
 997         @Override
 998         SSLParameters getSupportedSSLParams() {
 999             return supportedSSLParams;
1000         }
1001 
1002         @Override
1003         SSLEngine createSSLEngineImpl() {
1004             return new SSLEngineImpl(this, true);
1005         }
1006 
1007         @Override
1008         SSLEngine createSSLEngineImpl(String host, int port) {
1009             return new SSLEngineImpl(this, host, port, true);
1010         }
1011 
1012         @Override
1013         HelloCookieManager getHelloCookieManager(SecureRandom secureRandom) {
1014             return new HelloCookieManager(secureRandom);
1015         }
1016     }
1017 
1018     /*
1019      * The SSLContext implementation for DTLSv1.0 algorithm.
1020      *
1021      * @see SSLContext
1022      */
1023     public static final class DTLS10Context extends AbstractDTLSContext {
1024         private final static SSLParameters defaultClientSSLParams;
1025 
1026         static {
1027             // candidates for available protocols
1028             ProtocolVersion[] candidates = new ProtocolVersion[] {
1029                 ProtocolVersion.DTLS10
1030             };
1031 
1032             defaultClientSSLParams = new SSLParameters();
1033             defaultClientSSLParams.setProtocols(
1034                     getAvailableProtocols(candidates));
1035         }
1036 
1037         @Override
1038         SSLParameters getDefaultClientSSLParams() {
1039             return defaultClientSSLParams;
1040         }
1041     }
1042 
1043     /*
1044      * The SSLContext implementation for DTLSv1.2 algorithm.
1045      *
1046      * @see SSLContext
1047      */
1048     public static final class DTLS12Context extends AbstractDTLSContext {
1049         private final static SSLParameters defaultClientSSLParams;
1050 
1051         static {
1052             // candidates for available protocols
1053             ProtocolVersion[] candidates = new ProtocolVersion[] {
1054                 ProtocolVersion.DTLS10,
1055                 ProtocolVersion.DTLS12
1056             };
1057 
1058             defaultClientSSLParams = new SSLParameters();
1059             defaultClientSSLParams.setProtocols(
1060                     getAvailableProtocols(candidates));
1061         }
1062 
1063         @Override
1064         SSLParameters getDefaultClientSSLParams() {
1065             return defaultClientSSLParams;
1066         }
1067     }
1068 
1069     /*
1070      * The SSLContext implementation for customized TLS protocols
1071      *
1072      * @see SSLContext
1073      */
1074     private static class CustomizedDTLSContext extends AbstractDTLSContext {
1075         private final static SSLParameters defaultClientSSLParams;
1076         private static IllegalArgumentException reservedException = null;
1077 
1078         // Don't want a java.lang.LinkageError for illegal system property.
1079         //
1080         // Please don't throw exception in this static block.  Otherwise,
1081         // java.lang.LinkageError may be thrown during the instantiation of
1082         // the provider service. Instead, let's handle the initialization
1083         // exception in constructor.
1084         static {
1085             reservedException = CustomizedSSLProtocols.reservedException;
1086             if (reservedException == null) {
1087                 ArrayList<ProtocolVersion>
1088                         customizedDTLSProtocols = new ArrayList<>();
1089                 for (ProtocolVersion protocol :
1090                         CustomizedSSLProtocols.customizedProtocols) {
1091                     if (protocol.isDTLSProtocol()) {
1092                         customizedDTLSProtocols.add(protocol);
1093                     }
1094                 }
1095 
1096                 // candidates for available protocols
1097                 ProtocolVersion[] candidates;
1098                 if (customizedDTLSProtocols.isEmpty()) {
1099                     // Use the default enabled client protocols if no
1100                     // customized TLS protocols.
1101                     //
1102                     // Both DTLSv1.0 and DTLSv1.2 can be used in FIPS mode.
1103                     candidates = new ProtocolVersion[] {
1104                         ProtocolVersion.DTLS10,
1105                         ProtocolVersion.DTLS12
1106                     };
1107 
1108                 } else {
1109                     // Use the customized TLS protocols.
1110                     candidates =
1111                             new ProtocolVersion[customizedDTLSProtocols.size()];
1112                     candidates = customizedDTLSProtocols.toArray(candidates);
1113                 }
1114 
1115                 defaultClientSSLParams = new SSLParameters();
1116                 defaultClientSSLParams.setProtocols(
1117                         getAvailableProtocols(candidates));
1118             } else {
1119                 defaultClientSSLParams = null;   // unlikely to be used
1120             }
1121         }
1122 
1123         protected CustomizedDTLSContext() {
1124             if (reservedException != null) {
1125                 throw reservedException;
1126             }
1127         }
1128 
1129         @Override
1130         SSLParameters getDefaultClientSSLParams() {
1131             return defaultClientSSLParams;
1132         }
1133     }
1134 
1135     /*
1136      * The SSLContext implementation for default "DTLS" algorithm
1137      *
1138      * @see SSLContext
1139      */
1140     public static final class DTLSContext extends CustomizedDTLSContext {
1141         // use the default constructor and methods
1142     }
1143 
1144 }
1145 
1146 
1147 final class AbstractTrustManagerWrapper extends X509ExtendedTrustManager
1148             implements X509TrustManager {
1149 
1150     // the delegated trust manager
1151     private final X509TrustManager tm;
1152 
1153     AbstractTrustManagerWrapper(X509TrustManager tm) {
1154         this.tm = tm;
1155     }
1156 
1157     @Override
1158     public void checkClientTrusted(X509Certificate[] chain, String authType)
1159         throws CertificateException {
1160         tm.checkClientTrusted(chain, authType);
1161     }
1162 
1163     @Override


1206 
1207             SSLSocket sslSocket = (SSLSocket)socket;
1208             SSLSession session = sslSocket.getHandshakeSession();
1209             if (session == null) {
1210                 throw new CertificateException("No handshake session");
1211             }
1212 
1213             // check endpoint identity
1214             String identityAlg = sslSocket.getSSLParameters().
1215                                         getEndpointIdentificationAlgorithm();
1216             if (identityAlg != null && identityAlg.length() != 0) {
1217                 String hostname = session.getPeerHost();
1218                 X509TrustManagerImpl.checkIdentity(
1219                                     hostname, chain[0], identityAlg);
1220             }
1221 
1222             // try the best to check the algorithm constraints
1223             ProtocolVersion protocolVersion =
1224                 ProtocolVersion.valueOf(session.getProtocol());
1225             AlgorithmConstraints constraints = null;
1226             if (protocolVersion.useTLS12PlusSpec()) {
1227                 if (session instanceof ExtendedSSLSession) {
1228                     ExtendedSSLSession extSession =
1229                                     (ExtendedSSLSession)session;
1230                     String[] peerSupportedSignAlgs =
1231                             extSession.getLocalSupportedSignatureAlgorithms();
1232 
1233                     constraints = new SSLAlgorithmConstraints(
1234                                     sslSocket, peerSupportedSignAlgs, true);
1235                 } else {
1236                     constraints =
1237                             new SSLAlgorithmConstraints(sslSocket, true);
1238                 }
1239             } else {
1240                 constraints = new SSLAlgorithmConstraints(sslSocket, true);
1241             }
1242 
1243             checkAlgorithmConstraints(chain, constraints);
1244         }
1245     }
1246 


1248             SSLEngine engine, boolean isClient) throws CertificateException {
1249         if (engine != null) {
1250             SSLSession session = engine.getHandshakeSession();
1251             if (session == null) {
1252                 throw new CertificateException("No handshake session");
1253             }
1254 
1255             // check endpoint identity
1256             String identityAlg = engine.getSSLParameters().
1257                                         getEndpointIdentificationAlgorithm();
1258             if (identityAlg != null && identityAlg.length() != 0) {
1259                 String hostname = session.getPeerHost();
1260                 X509TrustManagerImpl.checkIdentity(
1261                                     hostname, chain[0], identityAlg);
1262             }
1263 
1264             // try the best to check the algorithm constraints
1265             ProtocolVersion protocolVersion =
1266                 ProtocolVersion.valueOf(session.getProtocol());
1267             AlgorithmConstraints constraints = null;
1268             if (protocolVersion.useTLS12PlusSpec()) {
1269                 if (session instanceof ExtendedSSLSession) {
1270                     ExtendedSSLSession extSession =
1271                                     (ExtendedSSLSession)session;
1272                     String[] peerSupportedSignAlgs =
1273                             extSession.getLocalSupportedSignatureAlgorithms();
1274 
1275                     constraints = new SSLAlgorithmConstraints(
1276                                     engine, peerSupportedSignAlgs, true);
1277                 } else {
1278                     constraints =
1279                             new SSLAlgorithmConstraints(engine, true);
1280                 }
1281             } else {
1282                 constraints = new SSLAlgorithmConstraints(engine, true);
1283             }
1284 
1285             checkAlgorithmConstraints(chain, constraints);
1286         }
1287     }
1288