@@ -487,13 +487,77 @@ protected static synchronized String[] getAllCiphers() {
487487 }
488488
489489 /**
490- * Get all enabled cipher suites, and allowed via
491- * wolfjsse.enabledCipherSuites system Security property (if set).
490+ * Get all enabled cipher suites, filtered by enabled protocols and
491+ * allowed via wolfjsse.enabledCipherSuites system Security property
492+ * (if set).
492493 *
493- * @return String array of all enabled cipher suites
494+ * TLS 1.3 uses different cipher suite names than TLS 1.2 and earlier.
495+ * TLS 1.3 suites do not contain "_WITH_" (e.g., TLS_AES_256_GCM_SHA384),
496+ * while TLS 1.2 and earlier suites do (e.g.,
497+ * TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384).
498+ *
499+ * Filtering is only applied when it results in a non-empty list. If
500+ * filtering would remove all cipher suites (e.g., user explicitly set
501+ * incompatible cipher suites for the protocol), the original list is
502+ * returned to preserve user settings and let the handshake fail with
503+ * a proper error.
504+ *
505+ * @return String array of all enabled cipher suites compatible with
506+ * enabled protocols
494507 */
495508 protected synchronized String [] getCiphers () {
496- return WolfSSLUtil .sanitizeSuites (this .params .getCipherSuites ());
509+ String [] suites = WolfSSLUtil .sanitizeSuites (
510+ this .params .getCipherSuites ());
511+ String [] protocols = null ;
512+ if (this .params != null ) {
513+ protocols = this .params .getProtocols ();
514+ }
515+
516+ if (suites == null || suites .length == 0 ||
517+ protocols == null || protocols .length == 0 ) {
518+ return suites ;
519+ }
520+
521+ boolean tls13Enabled = false ;
522+ boolean preTls13Enabled = false ;
523+
524+ for (String protocol : protocols ) {
525+ if (protocol .equals ("TLSv1.3" ) || protocol .equals ("DTLSv1.3" )) {
526+ tls13Enabled = true ;
527+ } else if (protocol .startsWith ("TLS" ) ||
528+ protocol .startsWith ("SSL" ) ||
529+ protocol .startsWith ("DTLS" )) {
530+ preTls13Enabled = true ;
531+ }
532+ }
533+
534+ /* If both TLS 1.3 and pre-TLS 1.3 protocols enabled, return all */
535+ if (tls13Enabled && preTls13Enabled ) {
536+ return suites ;
537+ }
538+
539+ ArrayList <String > filtered = new ArrayList <>();
540+
541+ for (String suite : suites ) {
542+ /* TLS 1.3 cipher suites do NOT contain "_WITH_" in their names */
543+ boolean isTls13Suite = !suite .contains ("_WITH_" );
544+
545+ if (tls13Enabled && isTls13Suite ) {
546+ /* Only TLS 1.3 enabled - include only TLS 1.3 suites */
547+ filtered .add (suite );
548+ } else if (preTls13Enabled && !isTls13Suite ) {
549+ /* Only pre-TLS 1.3 enabled - include only pre-TLS 1.3 suites */
550+ filtered .add (suite );
551+ }
552+ }
553+
554+ /* If filtering removed all suites, return original list to preserve
555+ * user settings and allow handshake to fail with a proper error. */
556+ if (filtered .isEmpty ()) {
557+ return suites ;
558+ }
559+
560+ return filtered .toArray (new String [0 ]);
497561 }
498562
499563 /**
0 commit comments