@@ -79,32 +79,36 @@ public void checkClientTrusted(final X509Certificate[] certificates, final Strin
7979 if (LOG .isDebugEnabled ()) {
8080 printCertificateChain (certificates , s );
8181 }
82- if (!authStrictness ) {
83- return ;
84- }
85- if (certificates == null || certificates .length < 1 || certificates [0 ] == null ) {
82+
83+ final X509Certificate primaryClientCertificate = (certificates != null && certificates .length > 0 && certificates [0 ] != null ) ? certificates [0 ] : null ;
84+ String exceptionMsg = "" ;
85+
86+ if (authStrictness && primaryClientCertificate == null ) {
8687 throw new CertificateException ("In strict auth mode, certificate(s) are expected from client:" + clientAddress );
88+ } else if (primaryClientCertificate == null ) {
89+ LOG .info ("No certificate was received from client, but continuing since strict auth mode is disabled" );
90+ return ;
8791 }
88- final X509Certificate primaryClientCertificate = certificates [0 ];
8992
9093 // Revocation check
9194 final BigInteger serialNumber = primaryClientCertificate .getSerialNumber ();
9295 if (serialNumber == null || crlDao .findBySerial (serialNumber ) != null ) {
9396 final String errorMsg = String .format ("Client is using revoked certificate of serial=%x, subject=%s from address=%s" ,
9497 primaryClientCertificate .getSerialNumber (), primaryClientCertificate .getSubjectDN (), clientAddress );
9598 LOG .error (errorMsg );
96- throw new CertificateException ( errorMsg );
99+ exceptionMsg = ( Strings . isNullOrEmpty ( exceptionMsg )) ? errorMsg : ( exceptionMsg + ". " + errorMsg );
97100 }
98101
99102 // Validity check
100- if (!allowExpiredCertificate ) {
101- try {
102- primaryClientCertificate .checkValidity ();
103- } catch (final CertificateExpiredException | CertificateNotYetValidException e ) {
104- final String errorMsg = String .format ("Client certificate has expired with serial=%x, subject=%s from address=%s" ,
105- primaryClientCertificate .getSerialNumber (), primaryClientCertificate .getSubjectDN (), clientAddress );
106- LOG .error (errorMsg );
107- throw new CertificateException (errorMsg ); }
103+ try {
104+ primaryClientCertificate .checkValidity ();
105+ } catch (final CertificateExpiredException | CertificateNotYetValidException e ) {
106+ final String errorMsg = String .format ("Client certificate has expired with serial=%x, subject=%s from address=%s" ,
107+ primaryClientCertificate .getSerialNumber (), primaryClientCertificate .getSubjectDN (), clientAddress );
108+ LOG .error (errorMsg );
109+ if (!allowExpiredCertificate ) {
110+ throw new CertificateException (errorMsg );
111+ }
108112 }
109113
110114 // Ownership check
@@ -122,13 +126,21 @@ public void checkClientTrusted(final X509Certificate[] certificates, final Strin
122126 if (!certMatchesOwnership ) {
123127 final String errorMsg = "Certificate ownership verification failed for client: " + clientAddress ;
124128 LOG .error (errorMsg );
125- throw new CertificateException ( errorMsg );
129+ exceptionMsg = ( Strings . isNullOrEmpty ( exceptionMsg )) ? errorMsg : ( exceptionMsg + ". " + errorMsg );
126130 }
127- if (activeCertMap != null && !Strings .isNullOrEmpty (clientAddress )) {
128- activeCertMap . put ( clientAddress , primaryClientCertificate );
131+ if (authStrictness && !Strings .isNullOrEmpty (exceptionMsg )) {
132+ throw new CertificateException ( exceptionMsg );
129133 }
130134 if (LOG .isDebugEnabled ()) {
131- LOG .debug ("Client/agent connection from ip=" + clientAddress + " has been validated and trusted." );
135+ if (authStrictness ) {
136+ LOG .debug ("Client/agent connection from ip=" + clientAddress + " has been validated and trusted." );
137+ } else {
138+ LOG .debug ("Client/agent connection from ip=" + clientAddress + " accepted without certificate validation." );
139+ }
140+ }
141+
142+ if (primaryClientCertificate != null && activeCertMap != null && !Strings .isNullOrEmpty (clientAddress )) {
143+ activeCertMap .put (clientAddress , primaryClientCertificate );
132144 }
133145 }
134146
@@ -138,9 +150,6 @@ public void checkServerTrusted(X509Certificate[] x509Certificates, String s) thr
138150
139151 @ Override
140152 public X509Certificate [] getAcceptedIssuers () {
141- if (!authStrictness ) {
142- return null ;
143- }
144153 return new X509Certificate []{caCertificate };
145154 }
146155}
0 commit comments