3838import javax .net .ssl .X509ExtendedTrustManager ;
3939import java .io .IOException ;
4040import java .lang .ref .WeakReference ;
41+ import java .nio .charset .StandardCharsets ;
42+ import java .util .List ;
4143
4244/**
4345 * Internal verify callback.
@@ -132,33 +134,31 @@ private int verifyHostnameOnly(X509Certificate peer) {
132134 WolfSSLTrustX509 wolfTM = (WolfSSLTrustX509 )tm ;
133135
134136 try {
135- if (this .callingSocket != null &&
136- this .callingSocket .get () != null ) {
137+ SSLSocket sock = (this .callingSocket != null ) ?
138+ this .callingSocket .get () : null ;
139+ SSLEngine eng = (this .callingEngine != null ) ?
140+ this .callingEngine .get () : null ;
137141
142+ if (sock != null ) {
138143 WolfSSLDebug .log (getClass (), WolfSSLDebug .INFO ,
139144 () -> "checking hostname verification using SSLSocket" );
140145
141146 /* Throws CertificateException when verify fails */
142- wolfTM .verifyHostname (peer , this .callingSocket .get (),
143- null , clientMode );
147+ wolfTM .verifyHostname (peer , sock , null , clientMode );
144148 }
145- else if (this .callingEngine != null &&
146- this .callingEngine .get () != null ) {
147-
149+ else if (eng != null ) {
148150 WolfSSLDebug .log (getClass (), WolfSSLDebug .INFO ,
149151 () -> "checking hostname verification using SSLEngine" );
150152
151153 /* Throws CertificateException when verify fails */
152- wolfTM .verifyHostname (peer , null ,
153- this .callingEngine .get (), clientMode );
154+ wolfTM .verifyHostname (peer , null , eng , clientMode );
154155 }
155156 else {
156157 /* SSLSocket/SSLEngine null. Fail if endpoint ID
157158 * is set, otherwise skip hostname verification. */
158159 String eia = null ;
159160 if (this .params != null ) {
160- eia = this .params
161- .getEndpointIdentificationAlgorithm ();
161+ eia = this .params .getEndpointIdentificationAlgorithm ();
162162 }
163163 if (eia != null && !eia .isEmpty ()) {
164164 throw new CertificateException (
@@ -199,8 +199,7 @@ private int verifyHostnameForExternalTM(X509Certificate peer) {
199199
200200 /* Get endpoint identification algorithm from params */
201201 if (this .params != null ) {
202- endpointIdAlgo =
203- this .params .getEndpointIdentificationAlgorithm ();
202+ endpointIdAlgo = this .params .getEndpointIdentificationAlgorithm ();
204203 }
205204
206205 /* If no endpoint identification algorithm set, skip hostname
@@ -209,10 +208,18 @@ private int verifyHostnameForExternalTM(X509Certificate peer) {
209208 return 1 ;
210209 }
211210
212- /* Only HTTPS and LDAPS are supported */
211+ /* Only HTTPS and LDAPS are supported. Fail if endpoint ID was
212+ * explicitly set to something else (typo or unsupported algo). */
213213 if (!endpointIdAlgo .equals ("HTTPS" ) &&
214214 !endpointIdAlgo .equals ("LDAPS" )) {
215- return 1 ;
215+ final String tmpAlgoUnsup = endpointIdAlgo ;
216+ this .verifyException = new CertificateException (
217+ "Unsupported endpoint identification algorithm: " +
218+ endpointIdAlgo );
219+ WolfSSLDebug .log (getClass (), WolfSSLDebug .INFO ,
220+ () -> "Unsupported endpoint identification algorithm: "
221+ + tmpAlgoUnsup );
222+ return 0 ;
216223 }
217224
218225 final String tmpAlgo = endpointIdAlgo ;
@@ -222,21 +229,20 @@ private int verifyHostnameForExternalTM(X509Certificate peer) {
222229
223230 /* Get peer host from SSLEngine or SSLSocket handshake session */
224231 try {
225- if (this .callingEngine != null &&
226- this .callingEngine .get () != null ) {
227- session =
228- this .callingEngine .get ().getHandshakeSession ();
232+ SSLEngine eng = (this .callingEngine != null ) ?
233+ this .callingEngine .get () : null ;
234+ SSLSocket sock = (this .callingSocket != null ) ?
235+ this .callingSocket .get () : null ;
236+
237+ if (eng != null ) {
238+ session = eng .getHandshakeSession ();
229239 }
230- else if (this .callingSocket != null &&
231- this .callingSocket .get () != null ) {
232- javax .net .ssl .SSLSocket sock =
233- this .callingSocket .get ();
240+ else if (sock != null ) {
234241 session = sock .getHandshakeSession ();
235242 }
236243 } catch (UnsupportedOperationException e ) {
237244 WolfSSLDebug .log (getClass (), WolfSSLDebug .INFO ,
238- () -> "getHandshakeSession() not supported: " +
239- e .getMessage ());
245+ () -> "getHandshakeSession() not supported: " + e .getMessage ());
240246 CertificateException ce = new CertificateException (
241247 "getHandshakeSession() not supported: " + e .getMessage ());
242248 ce .initCause (e );
@@ -249,16 +255,15 @@ else if (this.callingSocket != null &&
249255 * but for SNI verification we need the logical hostname the client
250256 * requested (e.g. "something.netty.io"). */
251257 if (this .params != null ) {
252- java .util .List <WolfSSLSNIServerName > sniNames =
253- this .params .getServerNames ();
258+ List <WolfSSLSNIServerName > sniNames = this .params .getServerNames ();
254259 if (sniNames != null && !sniNames .isEmpty ()) {
255260 for (WolfSSLSNIServerName sni : sniNames ) {
256261 /* Type 0 = host_name (RFC 6066) */
257262 if (sni .getType () == 0 ) {
258263 byte [] encoded = sni .getEncoded ();
259264 if (encoded != null && encoded .length > 0 ) {
260265 peerHost = new String (encoded ,
261- java . nio . charset . StandardCharsets .US_ASCII );
266+ StandardCharsets .US_ASCII );
262267 }
263268 break ;
264269 }
@@ -292,17 +297,15 @@ else if (this.callingSocket != null &&
292297 int ret = wCert .checkHost (peerHost );
293298 if (ret == WolfSSL .SSL_SUCCESS ) {
294299 WolfSSLDebug .log (getClass (), WolfSSLDebug .INFO ,
295- () -> "Provider-level hostname " +
296- "verification passed for: " + tmpHost );
300+ () -> "Provider-level hostname verification " +
301+ " passed for: " + tmpHost );
297302 return 1 ;
298303 } else {
299304 WolfSSLDebug .log (getClass (), WolfSSLDebug .INFO ,
300- () -> "Provider-level hostname " +
301- "verification FAILED for: " + tmpHost );
302- this .verifyException =
303- new CertificateException (
304- "Hostname verification failed for: "
305- + tmpHost );
305+ () -> "Provider-level hostname verification " +
306+ "FAILED for: " + tmpHost );
307+ this .verifyException = new CertificateException (
308+ "Hostname verification failed for: " + tmpHost );
306309 return 0 ;
307310 }
308311 } catch (Exception e ) {
@@ -344,30 +347,29 @@ private boolean VerifyCertChainWithTrustManager(X509Certificate[] certs,
344347 try {
345348 /* Call TrustManager to do cert verification, should throw
346349 * CertificateException if verification fails */
350+ SSLSocket sock = (this .callingSocket != null ) ?
351+ this .callingSocket .get () : null ;
352+ SSLEngine eng = (this .callingEngine != null ) ?
353+ this .callingEngine .get () : null ;
354+
347355 if (this .clientMode ) {
348356 if (this .tm instanceof X509ExtendedTrustManager ) {
349357 X509ExtendedTrustManager xtm =
350358 (X509ExtendedTrustManager )this .tm ;
351359
352- if (this .callingSocket != null &&
353- this .callingSocket .get () != null ) {
354-
360+ if (sock != null ) {
355361 WolfSSLDebug .log (getClass (), WolfSSLDebug .INFO ,
356362 () -> "Calling TrustManager.checkServerTrusted(" +
357363 "SSLSocket)" );
358364
359- xtm .checkServerTrusted (certs , authType ,
360- this .callingSocket .get ());
365+ xtm .checkServerTrusted (certs , authType , sock );
361366 }
362- else if (this .callingEngine != null &&
363- this .callingEngine .get () != null ) {
364-
367+ else if (eng != null ) {
365368 WolfSSLDebug .log (getClass (), WolfSSLDebug .INFO ,
366369 () -> "Calling TrustManager.checkServerTrusted(" +
367370 "SSLEngine)" );
368371
369- xtm .checkServerTrusted (certs , authType ,
370- this .callingEngine .get ());
372+ xtm .checkServerTrusted (certs , authType , eng );
371373 }
372374 else {
373375 /* If we do have access to X509ExtendedTrustManager,
@@ -391,25 +393,19 @@ else if (this.callingEngine != null &&
391393 X509ExtendedTrustManager xtm =
392394 (X509ExtendedTrustManager )this .tm ;
393395
394- if (this .callingSocket != null &&
395- this .callingSocket .get () != null ) {
396-
396+ if (sock != null ) {
397397 WolfSSLDebug .log (getClass (), WolfSSLDebug .INFO ,
398398 () -> "Calling TrustManager.checkClientTrusted(" +
399399 "SSLSocket)" );
400400
401- xtm .checkClientTrusted (certs , authType ,
402- this .callingSocket .get ());
401+ xtm .checkClientTrusted (certs , authType , sock );
403402 }
404- else if (this .callingEngine != null &&
405- this .callingEngine .get () != null ) {
406-
403+ else if (eng != null ) {
407404 WolfSSLDebug .log (getClass (), WolfSSLDebug .INFO ,
408405 () -> "Calling TrustManager.checkClientTrusted(" +
409406 "SSLEngine)" );
410407
411- xtm .checkClientTrusted (certs , authType ,
412- this .callingEngine .get ());
408+ xtm .checkClientTrusted (certs , authType , eng );
413409 }
414410 else {
415411 /* If we do have access to X509ExtendedTrustManager,
0 commit comments