@@ -73,6 +73,9 @@ public class WolfSSLImplementSSLSession extends ExtendedSSLSession {
7373 byte [] pseudoSessionID = null ; /* used with TLS 1.3*/
7474 private int side = 0 ;
7575
76+ /* Track if client auth was requested, for getPeerCertificates() behavior */
77+ private volatile boolean clientAuthRequested = false ;
78+
7679 /* Cache peer certificates after received. Applications assume that
7780 * SSLSocket.getSession().getPeerCertificates() will return the peer
7881 * certificate even on a resumed connection where the cert has not been
@@ -260,6 +263,7 @@ public WolfSSLImplementSSLSession (WolfSSLImplementSSLSession orig) {
260263 this .pseudoSessionID = orig .pseudoSessionID .clone ();
261264 }
262265 this .side = orig .side ;
266+ this .clientAuthRequested = orig .clientAuthRequested ;
263267 if (orig .peerCerts != null ) {
264268 this .peerCerts = orig .peerCerts .clone ();
265269 }
@@ -519,6 +523,15 @@ public synchronized Certificate[] getPeerCertificates()
519523 "SSLSocket/Engine closed" );
520524 }
521525
526+ /* Throw if server side with no client auth requested */
527+ if (this .side == WolfSSL .WOLFSSL_SERVER_END &&
528+ !this .clientAuthRequested ) {
529+ WolfSSLDebug .log (getClass (), WolfSSLDebug .INFO ,
530+ () -> "Server side, no client auth, throwing exception" );
531+ throw new SSLPeerUnverifiedException (
532+ "peer not authenticated (no client auth requested)" );
533+ }
534+
522535 try {
523536 x509 = this .ssl .getPeerCertificate ();
524537 } catch (IllegalStateException | WolfSSLJNIException ex ) {
@@ -605,8 +618,8 @@ public Certificate[] getLocalCertificates() {
605618 }
606619
607620 @ Override
608- public synchronized javax .security .cert .X509Certificate [] getPeerCertificateChain ()
609- throws SSLPeerUnverifiedException {
621+ public synchronized javax .security .cert .X509Certificate []
622+ getPeerCertificateChain () throws SSLPeerUnverifiedException {
610623
611624 long peerX509 = 0 ;
612625 WolfSSLX509X x509 ;
@@ -615,10 +628,17 @@ public synchronized javax.security.cert.X509Certificate[] getPeerCertificateChai
615628 throw new SSLPeerUnverifiedException ("handshake not done" );
616629 }
617630
631+ /* Throw if server side with no client auth requested */
632+ if (this .side == WolfSSL .WOLFSSL_SERVER_END &&
633+ !this .clientAuthRequested ) {
634+ throw new SSLPeerUnverifiedException (
635+ "peer not authenticated (no client auth requested)" );
636+ }
637+
618638 try {
619639 peerX509 = this .ssl .getPeerCertificate ();
620640 if (peerX509 == 0 ) {
621- return null ;
641+ throw new SSLPeerUnverifiedException ( "No peer certificate" ) ;
622642 }
623643
624644 /* wolfSSL starting with 5.3.0 returns a new WOLFSSL_X509
@@ -657,10 +677,17 @@ public synchronized Principal getPeerPrincipal()
657677 throw new SSLPeerUnverifiedException ("handshake not done" );
658678 }
659679
680+ /* Throw if server side with no client auth requested */
681+ if (this .side == WolfSSL .WOLFSSL_SERVER_END &&
682+ !this .clientAuthRequested ) {
683+ throw new SSLPeerUnverifiedException (
684+ "peer not authenticated (no client auth requested)" );
685+ }
686+
660687 try {
661688 peerX509 = this .ssl .getPeerCertificate ();
662689 if (peerX509 == 0 ) {
663- return null ;
690+ throw new SSLPeerUnverifiedException ( "No peer certificate" ) ;
664691 }
665692
666693 /* wolfSSL starting with 5.3.0 returns a new WOLFSSL_X509
@@ -1039,6 +1066,16 @@ protected int getSide() {
10391066 return this .side ;
10401067 }
10411068
1069+ /**
1070+ * Set whether client auth was requested.
1071+ * Used for getPeerCertificates() behavior.
1072+ *
1073+ * @param requested true if client auth was requested, false otherwise
1074+ */
1075+ protected void setClientAuthRequested (boolean requested ) {
1076+ this .clientAuthRequested = requested ;
1077+ }
1078+
10421079 /**
10431080 * Return the side session is on (server/client) as a String
10441081 * @return "client" or "server" representing the side of this session
0 commit comments