8888import java .util .logging .Logger ;
8989import javax .annotation .Nonnull ;
9090import javax .annotation .Nullable ;
91+ import javax .annotation .concurrent .GuardedBy ;
9192import javax .net .ssl .SSLEngine ;
9293import javax .net .ssl .SSLException ;
9394import javax .net .ssl .SSLParameters ;
@@ -145,10 +146,15 @@ public static FromChannelCredentialsResult from(ChannelCredentials creds) {
145146 trustManagers = Arrays .asList (tmf .getTrustManagers ());
146147 }
147148 builder .trustManager (new FixedTrustManagerFactory (trustManagers ));
148- Optional <TrustManager > x509ExtendedTrustManager = trustManagers .stream ().filter (
149- trustManager -> trustManager instanceof X509ExtendedTrustManager ).findFirst ();
149+ TrustManager x509ExtendedTrustManager = null ;
150+ for (TrustManager trustManager : trustManagers ) {
151+ if (trustManager instanceof X509ExtendedTrustManager ) {
152+ x509ExtendedTrustManager = trustManager ;
153+ break ;
154+ }
155+ }
150156 return FromChannelCredentialsResult .negotiator (tlsClientFactory (builder .build (),
151- (X509ExtendedTrustManager ) x509ExtendedTrustManager . orElse ( null ) ));
157+ (X509ExtendedTrustManager ) x509ExtendedTrustManager ));
152158 } catch (SSLException | GeneralSecurityException ex ) {
153159 log .log (Level .FINE , "Exception building SslContext" , ex );
154160 return FromChannelCredentialsResult .error (
@@ -567,6 +573,7 @@ protected void userEventTriggered0(ChannelHandlerContext ctx, Object evt) throws
567573 }
568574
569575 static final class ClientTlsProtocolNegotiator implements ProtocolNegotiator {
576+ @ GuardedBy ("this" )
570577 private final LinkedHashMap <String , Status > peerVerificationResults =
571578 new LinkedHashMap <String , Status >() {
572579 @ Override
@@ -617,10 +624,12 @@ public void close() {
617624
618625 @ Override
619626 public synchronized Status verifyAuthority (@ Nonnull String authority ) {
620- if (!canVerifyAuthorityOverride ()) {
627+ // sslEngine won't be set when creating ClientTlsHandler from InternalProtocolNegotiators
628+ // for example.
629+ if (sslEngine == null || x509ExtendedTrustManager == null ) {
621630 return Status .FAILED_PRECONDITION .withDescription (
622- "Can't allow authority override in rpc when X509ExtendedTrustManager is not "
623- + "available" );
631+ "Can't allow authority override in rpc when SslEngine or X509ExtendedTrustManager "
632+ + " is not available" );
624633 }
625634 if (peerVerificationResults .containsKey (authority )) {
626635 return peerVerificationResults .get (authority );
@@ -631,7 +640,7 @@ public synchronized Status verifyAuthority(@Nonnull String authority) {
631640 peerVerificationStatus = Status .OK ;
632641 } catch (SSLPeerUnverifiedException | CertificateException e ) {
633642 peerVerificationStatus = Status .UNAVAILABLE .withDescription (
634- String .format ("Peer hostname verification failed for authority '%s'" ,
643+ String .format ("Peer hostname verification during rpc failed for authority '%s'" ,
635644 authority )).withCause (e );
636645 }
637646 peerVerificationResults .put (authority , peerVerificationStatus );
@@ -643,17 +652,11 @@ public void setSslEngine(SSLEngine sslEngine) {
643652 this .sslEngine = sslEngine ;
644653 }
645654
646- boolean canVerifyAuthorityOverride () {
647- // sslEngine won't be set when creating ClientTlsHandlder from InternalProtocolNegotiators
648- // for example.
649- return sslEngine != null && x509ExtendedTrustManager != null ;
650- }
651-
652655 private void verifyAuthorityAllowedForPeerCert (String authority )
653656 throws SSLPeerUnverifiedException , CertificateException {
654657 SSLEngine sslEngineWrapper = new SslEngineWrapper (sslEngine , authority );
655658 // The typecasting of Certificate to X509Certificate should work because this method will only
656- // be called when there is a X509ExtendedTrustManager available .
659+ // be called when using TLS and thus X509 .
657660 Certificate [] peerCertificates = sslEngine .getSession ().getPeerCertificates ();
658661 X509Certificate [] x509PeerCertificates = new X509Certificate [peerCertificates .length ];
659662 for (int i = 0 ; i < peerCertificates .length ; i ++) {
0 commit comments