@@ -79,32 +79,35 @@ 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+ return ;
8790 }
88- final X509Certificate primaryClientCertificate = certificates [0 ];
8991
9092 // Revocation check
9193 final BigInteger serialNumber = primaryClientCertificate .getSerialNumber ();
9294 if (serialNumber == null || crlDao .findBySerial (serialNumber ) != null ) {
9395 final String errorMsg = String .format ("Client is using revoked certificate of serial=%x, subject=%s from address=%s" ,
9496 primaryClientCertificate .getSerialNumber (), primaryClientCertificate .getSubjectDN (), clientAddress );
9597 LOG .error (errorMsg );
96- throw new CertificateException ( errorMsg );
98+ exceptionMsg = ( Strings . isNullOrEmpty ( exceptionMsg )) ? errorMsg : ( exceptionMsg + ". " + errorMsg );
9799 }
98100
99101 // 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 ); }
102+ try {
103+ primaryClientCertificate .checkValidity ();
104+ } catch (final CertificateExpiredException | CertificateNotYetValidException e ) {
105+ final String errorMsg = String .format ("Client certificate has expired with serial=%x, subject=%s from address=%s" ,
106+ primaryClientCertificate .getSerialNumber (), primaryClientCertificate .getSubjectDN (), clientAddress );
107+ LOG .error (errorMsg );
108+ if (!allowExpiredCertificate ) {
109+ throw new CertificateException (errorMsg );
110+ }
108111 }
109112
110113 // Ownership check
@@ -122,13 +125,21 @@ public void checkClientTrusted(final X509Certificate[] certificates, final Strin
122125 if (!certMatchesOwnership ) {
123126 final String errorMsg = "Certificate ownership verification failed for client: " + clientAddress ;
124127 LOG .error (errorMsg );
125- throw new CertificateException ( errorMsg );
128+ exceptionMsg = ( Strings . isNullOrEmpty ( exceptionMsg )) ? errorMsg : ( exceptionMsg + ". " + errorMsg );
126129 }
127- if (activeCertMap != null && !Strings .isNullOrEmpty (clientAddress )) {
128- activeCertMap . put ( clientAddress , primaryClientCertificate );
130+ if (authStrictness && !Strings .isNullOrEmpty (exceptionMsg )) {
131+ throw new CertificateException ( exceptionMsg );
129132 }
130133 if (LOG .isDebugEnabled ()) {
131- LOG .debug ("Client/agent connection from ip=" + clientAddress + " has been validated and trusted." );
134+ if (authStrictness ) {
135+ LOG .debug ("Client/agent connection from ip=" + clientAddress + " has been validated and trusted." );
136+ } else {
137+ LOG .debug ("Client/agent connection from ip=" + clientAddress + " accepted without certificate validation." );
138+ }
139+ }
140+
141+ if (primaryClientCertificate != null && activeCertMap != null && !Strings .isNullOrEmpty (clientAddress )) {
142+ activeCertMap .put (clientAddress , primaryClientCertificate );
132143 }
133144 }
134145
@@ -138,9 +149,6 @@ public void checkServerTrusted(X509Certificate[] x509Certificates, String s) thr
138149
139150 @ Override
140151 public X509Certificate [] getAcceptedIssuers () {
141- if (!authStrictness ) {
142- return null ;
143- }
144152 return new X509Certificate []{caCertificate };
145153 }
146154}
0 commit comments