4747import java .util .Collections ;
4848import java .util .Comparator ;
4949import java .util .HashMap ;
50- import java .util .Iterator ;
5150import java .util .LinkedHashSet ;
5251import java .util .List ;
5352import java .util .Map ;
5453import java .util .Objects ;
5554import java .util .Set ;
5655import java .util .function .Consumer ;
57- import java .util .regex .Matcher ;
5856import java .util .regex .Pattern ;
5957import javax .net .ssl .CertPathTrustManagerParameters ;
6058import javax .net .ssl .HostnameVerifier ;
@@ -140,7 +138,7 @@ public class SslContextFactory extends AbstractLifeCycle implements Dumpable
140138 private final Set <String > _excludeProtocols = new LinkedHashSet <>();
141139 private final Set <String > _includeProtocols = new LinkedHashSet <>();
142140 private final Set <String > _excludeCipherSuites = new LinkedHashSet <>();
143- private final List <String > _includeCipherSuites = new ArrayList <>();
141+ private final Set <String > _includeCipherSuites = new LinkedHashSet <>();
144142 private final Map <String , X509 > _aliasX509 = new HashMap <>();
145143 private final Map <String , X509 > _certHosts = new HashMap <>();
146144 private final Map <String , X509 > _certWilds = new HashMap <>();
@@ -526,6 +524,8 @@ public String[] getExcludeProtocols()
526524 }
527525
528526 /**
527+ * You can either use the exact Protocol name or a a regular expression.
528+ *
529529 * @param protocols The array of protocol names to exclude from
530530 * {@link SSLEngine#setEnabledProtocols(String[])}
531531 */
@@ -536,15 +536,17 @@ public void setExcludeProtocols(String... protocols)
536536 }
537537
538538 /**
539- * @param protocol Protocol names to add to {@link SSLEngine#setEnabledProtocols(String[])}
539+ * You can either use the exact Protocol name or a a regular expression.
540+ *
541+ * @param protocol Protocol name patterns to add to {@link SSLEngine#setEnabledProtocols(String[])}
540542 */
541543 public void addExcludeProtocols (String ... protocol )
542544 {
543545 _excludeProtocols .addAll (Arrays .asList (protocol ));
544546 }
545547
546548 /**
547- * @return The array of protocol names to include in
549+ * @return The array of protocol name patterns to include in
548550 * {@link SSLEngine#setEnabledProtocols(String[])}
549551 */
550552 @ ManagedAttribute ("The included TLS protocols" )
@@ -554,7 +556,9 @@ public String[] getIncludeProtocols()
554556 }
555557
556558 /**
557- * @param protocols The array of protocol names to include in
559+ * You can either use the exact Protocol name or a a regular expression.
560+ *
561+ * @param protocols The array of protocol name patterns to include in
558562 * {@link SSLEngine#setEnabledProtocols(String[])}
559563 */
560564 public void setIncludeProtocols (String ... protocols )
@@ -564,7 +568,7 @@ public void setIncludeProtocols(String... protocols)
564568 }
565569
566570 /**
567- * @return The array of cipher suite names to exclude from
571+ * @return The array of cipher suite name patterns to exclude from
568572 * {@link SSLEngine#setEnabledCipherSuites(String[])}
569573 */
570574 @ ManagedAttribute ("The excluded cipher suites" )
@@ -574,7 +578,7 @@ public String[] getExcludeCipherSuites()
574578 }
575579
576580 /**
577- * You can either use the exact cipher suite name or a a regular expression.
581+ * You can either use the exact Cipher suite name or a a regular expression.
578582 *
579583 * @param cipherSuites The array of cipher suite names to exclude from
580584 * {@link SSLEngine#setEnabledCipherSuites(String[])}
@@ -586,6 +590,8 @@ public void setExcludeCipherSuites(String... cipherSuites)
586590 }
587591
588592 /**
593+ * You can either use the exact Cipher suite name or a a regular expression.
594+ *
589595 * @param cipher Cipher names to add to {@link SSLEngine#setEnabledCipherSuites(String[])}
590596 */
591597 public void addExcludeCipherSuites (String ... cipher )
@@ -594,7 +600,7 @@ public void addExcludeCipherSuites(String... cipher)
594600 }
595601
596602 /**
597- * @return The array of cipher suite names to include in
603+ * @return The array of Cipher suite names to include in
598604 * {@link SSLEngine#setEnabledCipherSuites(String[])}
599605 */
600606 @ ManagedAttribute ("The included cipher suites" )
@@ -604,7 +610,7 @@ public String[] getIncludeCipherSuites()
604610 }
605611
606612 /**
607- * You can either use the exact cipher suite name or a a regular expression.
613+ * You can either use the exact Cipher suite name or a a regular expression.
608614 *
609615 * @param cipherSuites The array of cipher suite names to include in
610616 * {@link SSLEngine#setEnabledCipherSuites(String[])}
@@ -1357,28 +1363,10 @@ protected PKIXBuilderParameters newPKIXBuilderParameters(KeyStore trustStore, Co
13571363 */
13581364 public void selectProtocols (String [] enabledProtocols , String [] supportedProtocols )
13591365 {
1360- Set <String > selectedProtocols = new LinkedHashSet <>();
1361-
1362- // Set the starting protocols - either from the included or enabled list
1363- if (!_includeProtocols .isEmpty ())
1364- {
1365- // Use only the supported included protocols
1366- for (String protocol : _includeProtocols )
1367- {
1368- if (Arrays .asList (supportedProtocols ).contains (protocol ))
1369- selectedProtocols .add (protocol );
1370- else
1371- LOG .info ("Protocol {} not supported in {}" , protocol , Arrays .asList (supportedProtocols ));
1372- }
1373- }
1374- else
1375- selectedProtocols .addAll (Arrays .asList (enabledProtocols ));
1376-
1377- // Remove any excluded protocols
1378- selectedProtocols .removeAll (_excludeProtocols );
1366+ List <String > selectedProtocols = processIncludeExcludePatterns ("Protocols" , enabledProtocols , supportedProtocols , _includeProtocols , _excludeProtocols );
13791367
13801368 if (selectedProtocols .isEmpty ())
1381- LOG .warn ("No selected protocols from {}" , Arrays .asList (supportedProtocols ));
1369+ LOG .warn ("No selected Protocols from {}" , Arrays .asList (supportedProtocols ));
13821370
13831371 _selectedProtocols = selectedProtocols .toArray (new String [0 ]);
13841372 }
@@ -1393,18 +1381,10 @@ public void selectProtocols(String[] enabledProtocols, String[] supportedProtoco
13931381 */
13941382 protected void selectCipherSuites (String [] enabledCipherSuites , String [] supportedCipherSuites )
13951383 {
1396- List <String > selectedCiphers = new ArrayList <>();
1397-
1398- // Set the starting ciphers - either from the included or enabled list
1399- if (_includeCipherSuites .isEmpty ())
1400- selectedCiphers .addAll (Arrays .asList (enabledCipherSuites ));
1401- else
1402- processIncludeCipherSuites (supportedCipherSuites , selectedCiphers );
1403-
1404- removeExcludedCipherSuites (selectedCiphers );
1384+ List <String > selectedCiphers = processIncludeExcludePatterns ("Cipher Suite" , enabledCipherSuites , supportedCipherSuites , _includeCipherSuites , _excludeCipherSuites );
14051385
14061386 if (selectedCiphers .isEmpty ())
1407- LOG .warn ("No supported ciphers from {}" , Arrays .asList (supportedCipherSuites ));
1387+ LOG .warn ("No supported Cipher Suite from {}" , Arrays .asList (supportedCipherSuites ));
14081388
14091389 Comparator <String > comparator = getCipherComparator ();
14101390 if (comparator != null )
@@ -1417,39 +1397,58 @@ protected void selectCipherSuites(String[] enabledCipherSuites, String[] support
14171397 _selectedCipherSuites = selectedCiphers .toArray (new String [0 ]);
14181398 }
14191399
1420- protected void processIncludeCipherSuites (String [] supportedCipherSuites , List <String > selectedCiphers )
1400+ private List < String > processIncludeExcludePatterns (String type , String [] enabled , String [] supported , Set <String > included , Set < String > excluded )
14211401 {
1422- for (String cipherSuite : _includeCipherSuites )
1402+ List <String > selected = new ArrayList <>();
1403+ // Set the starting list - either from the included or enabled list
1404+ if (included .isEmpty ())
14231405 {
1424- Pattern p = Pattern .compile (cipherSuite );
1425- boolean added = false ;
1426- for (String supportedCipherSuite : supportedCipherSuites )
1406+ selected .addAll (Arrays .asList (enabled ));
1407+ }
1408+ else
1409+ {
1410+ // process include patterns
1411+ for (String includedItem : included )
14271412 {
1428- Matcher m = p .matcher (supportedCipherSuite );
1429- if (m .matches ())
1413+ Pattern pattern = Pattern .compile (includedItem );
1414+ boolean added = false ;
1415+ for (String supportedItem : supported )
14301416 {
1431- added = true ;
1432- selectedCiphers .add (supportedCipherSuite );
1417+ if (pattern .matcher (supportedItem ).matches ())
1418+ {
1419+ added = true ;
1420+ selected .add (supportedItem );
1421+ }
14331422 }
1423+ if (!added )
1424+ LOG .info ("No {} matching '{}' is supported" , type , includedItem );
14341425 }
1435- if (!added )
1436- LOG .info ("No Cipher matching '{}' is supported" , cipherSuite );
14371426 }
1427+
1428+ // process exclude patterns
1429+ for (String excludedItem : excluded )
1430+ {
1431+ Pattern pattern = Pattern .compile (excludedItem );
1432+ selected .removeIf (selectedItem -> pattern .matcher (selectedItem ).matches ());
1433+ }
1434+
1435+ return selected ;
14381436 }
14391437
1438+ /**
1439+ * @deprecated no replacement
1440+ */
1441+ @ Deprecated
1442+ protected void processIncludeCipherSuites (String [] supportedCipherSuites , List <String > selectedCiphers )
1443+ {
1444+ }
1445+
1446+ /**
1447+ * @deprecated no replacement
1448+ */
1449+ @ Deprecated
14401450 protected void removeExcludedCipherSuites (List <String > selectedCiphers )
14411451 {
1442- for (String excludeCipherSuite : _excludeCipherSuites )
1443- {
1444- Pattern excludeCipherPattern = Pattern .compile (excludeCipherSuite );
1445- for (Iterator <String > i = selectedCiphers .iterator (); i .hasNext (); )
1446- {
1447- String selectedCipherSuite = i .next ();
1448- Matcher m = excludeCipherPattern .matcher (selectedCipherSuite );
1449- if (m .matches ())
1450- i .remove ();
1451- }
1452- }
14531452 }
14541453
14551454 /**
0 commit comments