@@ -585,25 +585,6 @@ protected void userEventTriggered0(ChannelHandlerContext ctx, Object evt) throws
585585 }
586586
587587 static final class ClientTlsProtocolNegotiator implements ProtocolNegotiator {
588- private static final Method checkServerTrustedMethod ;
589-
590- static {
591- Method method = null ;
592- try {
593- Class <?> x509ExtendedTrustManagerClass =
594- Class .forName ("javax.net.ssl.X509ExtendedTrustManager" );
595- method = x509ExtendedTrustManagerClass .getMethod ("checkServerTrusted" ,
596- X509Certificate [].class , String .class , SSLEngine .class );
597- } catch (ClassNotFoundException e ) {
598- // Per-rpc authority overriding via call options will be disallowed.
599- } catch (NoSuchMethodException e ) {
600- // Should never happen since X509ExtendedTrustManager was introduced in Android API level 24
601- // along with checkServerTrusted.
602- }
603- checkServerTrustedMethod = method ;
604- }
605-
606- private SSLEngine sslEngine ;
607588
608589 public ClientTlsProtocolNegotiator (SslContext sslContext ,
609590 ObjectPool <? extends Executor > executorPool , Optional <Runnable > handshakeCompleteRunnable ,
@@ -633,7 +614,8 @@ public ChannelHandler newHandler(GrpcHttp2ConnectionHandler grpcHandler) {
633614 ChannelHandler gnh = new GrpcNegotiationHandler (grpcHandler );
634615 ChannelLogger negotiationLogger = grpcHandler .getNegotiationLogger ();
635616 ChannelHandler cth = new ClientTlsHandler (gnh , sslContext , grpcHandler .getAuthority (),
636- this .executor , negotiationLogger , handshakeCompleteRunnable , this );
617+ this .executor , negotiationLogger , handshakeCompleteRunnable , this ,
618+ x509ExtendedTrustManager );
637619 return new WaitUntilActiveHandler (cth , negotiationLogger );
638620 }
639621
@@ -644,47 +626,6 @@ public void close() {
644626 }
645627 }
646628
647- @ Override
648- public Status verifyAuthority (@ Nonnull String authority ) {
649- // sslEngine won't be set when creating ClientTlsHandler from InternalProtocolNegotiators
650- // for example.
651- if (sslEngine == null || x509ExtendedTrustManager == null ) {
652- return Status .FAILED_PRECONDITION .withDescription (
653- "Can't allow authority override in rpc when SslEngine or X509ExtendedTrustManager"
654- + " is not available" );
655- }
656- Status peerVerificationStatus ;
657- try {
658- verifyAuthorityAllowedForPeerCert (authority );
659- peerVerificationStatus = Status .OK ;
660- } catch (SSLPeerUnverifiedException | CertificateException | InvocationTargetException
661- | IllegalAccessException | IllegalStateException e ) {
662- peerVerificationStatus = Status .UNAVAILABLE .withDescription (
663- String .format ("Peer hostname verification during rpc failed for authority '%s'" ,
664- authority )).withCause (e );
665- }
666- return peerVerificationStatus ;
667- }
668-
669- public void setSslEngine (SSLEngine sslEngine ) {
670- this .sslEngine = sslEngine ;
671- }
672-
673- private void verifyAuthorityAllowedForPeerCert (String authority )
674- throws SSLPeerUnverifiedException , CertificateException , InvocationTargetException ,
675- IllegalAccessException {
676- SSLEngine sslEngineWrapper = new SslEngineWrapper (sslEngine , authority );
677- // The typecasting of Certificate to X509Certificate should work because this method will only
678- // be called when using TLS and thus X509.
679- Certificate [] peerCertificates = sslEngine .getSession ().getPeerCertificates ();
680- X509Certificate [] x509PeerCertificates = new X509Certificate [peerCertificates .length ];
681- for (int i = 0 ; i < peerCertificates .length ; i ++) {
682- x509PeerCertificates [i ] = (X509Certificate ) peerCertificates [i ];
683- }
684- checkServerTrustedMethod .invoke (
685- x509ExtendedTrustManager , x509PeerCertificates , "RSA" , sslEngineWrapper );
686- }
687-
688629 @ VisibleForTesting
689630 boolean hasX509ExtendedTrustManager () {
690631 return x509ExtendedTrustManager != null ;
@@ -699,11 +640,13 @@ static final class ClientTlsHandler extends ProtocolNegotiationHandler {
699640 private final ClientTlsProtocolNegotiator clientTlsProtocolNegotiator ;
700641 private Executor executor ;
701642 private final Optional <Runnable > handshakeCompleteRunnable ;
643+ private final X509TrustManager x509ExtendedTrustManager ;
702644
703645 ClientTlsHandler (ChannelHandler next , SslContext sslContext , String authority ,
704646 Executor executor , ChannelLogger negotiationLogger ,
705647 Optional <Runnable > handshakeCompleteRunnable ,
706- ClientTlsProtocolNegotiator clientTlsProtocolNegotiator ) {
648+ ClientTlsProtocolNegotiator clientTlsProtocolNegotiator ,
649+ X509TrustManager x509ExtendedTrustManager ) {
707650 super (next , negotiationLogger );
708651 this .sslContext = checkNotNull (sslContext , "sslContext" );
709652 HostPort hostPort = parseAuthority (authority );
@@ -712,6 +655,7 @@ static final class ClientTlsHandler extends ProtocolNegotiationHandler {
712655 this .executor = executor ;
713656 this .handshakeCompleteRunnable = handshakeCompleteRunnable ;
714657 this .clientTlsProtocolNegotiator = clientTlsProtocolNegotiator ;
658+ this .x509ExtendedTrustManager = x509ExtendedTrustManager ;
715659 }
716660
717661 @ Override
@@ -724,7 +668,12 @@ protected void handlerAdded0(ChannelHandlerContext ctx) {
724668 ctx .pipeline ().addBefore (ctx .name (), /* name= */ null , this .executor != null
725669 ? new SslHandler (sslEngine , false , this .executor )
726670 : new SslHandler (sslEngine , false ));
727- clientTlsProtocolNegotiator .setSslEngine (sslEngine );
671+ ProtocolNegotiationEvent existingPne = getProtocolNegotiationEvent ();
672+ Attributes attrs = existingPne .getAttributes ().toBuilder ()
673+ .set (GrpcAttributes .ATTR_AUTHORITY_VERIFIER , new X509AuthorityVerifier (
674+ sslEngine , x509ExtendedTrustManager ))
675+ .build ();
676+ replaceProtocolNegotiationEvent (existingPne .withAttributes (attrs ));
728677 }
729678
730679 @ Override
0 commit comments