@@ -450,61 +450,7 @@ public ClientStream newStream(
450450 Preconditions .checkNotNull (headers , "headers" );
451451 StatsTraceContext statsTraceContext =
452452 StatsTraceContext .newClientContext (tracers , getAttributes (), headers );
453- if (hostnameVerifier != null && socket instanceof SSLSocket
454- && !hostnameVerifier .verify (callOptions .getAuthority (),
455- ((SSLSocket ) socket ).getSession ())) {
456- if (enablePerRpcAuthorityCheck ) {
457- return new FailingClientStream (Status .UNAVAILABLE .withDescription (
458- String .format ("HostNameVerifier verification failed for authority '%s'" ,
459- callOptions .getAuthority ())), tracers );
460- }
461- }
462- if (socket instanceof SSLSocket && callOptions .getAuthority () != null
463- && channelCredentials != null && channelCredentials instanceof TlsChannelCredentials ) {
464- Status peerVerificationStatus = null ;
465- if (peerVerificationResults .containsKey (callOptions .getAuthority ())) {
466- peerVerificationStatus = peerVerificationResults .get (callOptions .getAuthority ());
467- } else {
468- TrustManager x509ExtendedTrustManager ;
469- try {
470- x509ExtendedTrustManager = x509ExtendedTrustManagerClass != null
471- ? getX509ExtendedTrustManager ((TlsChannelCredentials ) channelCredentials ) : null ;
472- if (x509ExtendedTrustManager == null ) {
473- if (GrpcUtil .getFlag (GRPC_ENABLE_PER_RPC_AUTHORITY_CHECK , false )) {
474- return new FailingClientStream (Status .UNAVAILABLE .withDescription (
475- "Can't allow authority override in rpc when X509ExtendedTrustManager is not "
476- + "available" ), tracers );
477- }
478- } else {
479- try {
480- Certificate [] peerCertificates = sslSession .getPeerCertificates ();
481- X509Certificate [] x509PeerCertificates = new X509Certificate [peerCertificates .length ];
482- for (int i = 0 ; i < peerCertificates .length ; i ++) {
483- x509PeerCertificates [i ] = (X509Certificate ) peerCertificates [i ];
484- }
485- checkServerTrustedMethod .invoke (x509ExtendedTrustManager , x509PeerCertificates ,
486- "RSA" , new SslSocketWrapper ((SSLSocket ) socket , callOptions .getAuthority ()));
487- peerVerificationStatus = Status .OK ;
488- } catch (SSLPeerUnverifiedException | InvocationTargetException
489- | IllegalAccessException e ) {
490- peerVerificationStatus = Status .UNAVAILABLE .withDescription (
491- String .format ("Failure in verifying authority '%s' against peer during rpc" ,
492- callOptions .getAuthority ())).withCause (e );
493- }
494- peerVerificationResults .put (callOptions .getAuthority (), peerVerificationStatus );
495- }
496- } catch (GeneralSecurityException e ) {
497- if (GrpcUtil .getFlag (GRPC_ENABLE_PER_RPC_AUTHORITY_CHECK , false )) {
498- return new FailingClientStream (Status .UNAVAILABLE .withDescription (
499- "Failure getting X509ExtendedTrustManager from TlsCredentials" ).withCause (e ),
500- tracers );
501- }
502- }
503- }
504- if (peerVerificationStatus != null && !peerVerificationStatus .isOk ()) {
505- return new FailingClientStream (peerVerificationStatus , tracers );
506- }
507- }
453+
508454 // FIXME: it is likely wrong to pass the transportTracer here as it'll exit the lock's scope
509455 synchronized (lock ) { // to make @GuardedBy linter happy
510456 return new OkHttpClientStream (
@@ -548,14 +494,87 @@ private TrustManager getX509ExtendedTrustManager(TlsChannelCredentials tlsCreds)
548494 }
549495
550496 @ GuardedBy ("lock" )
551- void streamReadyToStart (OkHttpClientStream clientStream ) {
497+ void streamReadyToStart (OkHttpClientStream clientStream , String authority ) {
552498 if (goAwayStatus != null ) {
553499 clientStream .transportState ().transportReportStatus (
554500 goAwayStatus , RpcProgress .MISCARRIED , true , new Metadata ());
555501 } else if (streams .size () >= maxConcurrentStreams ) {
556502 pendingStreams .add (clientStream );
557503 setInUse (clientStream );
558504 } else {
505+ if (hostnameVerifier != null
506+ && socket instanceof SSLSocket
507+ && !hostnameVerifier .verify (authority ,
508+ ((SSLSocket ) socket ).getSession ())) {
509+ if (enablePerRpcAuthorityCheck ) {
510+ clientStream .transportState ().transportReportStatus (
511+ Status .UNAVAILABLE .withDescription (
512+ String .format ("HostNameVerifier verification failed for authority '%s'" ,
513+ authority )),
514+ RpcProgress .DROPPED , true , new Metadata ());
515+ return ;
516+ } else {
517+ log .log (Level .WARNING , String .format ("HostNameVerifier verification failed for authority "
518+ + "'%s'. This will be an error in the future." ,
519+ authority ));
520+ }
521+ }
522+ if (socket instanceof SSLSocket && authority != null
523+ && channelCredentials != null && channelCredentials instanceof TlsChannelCredentials ) {
524+ Status peerVerificationStatus = null ;
525+ if (peerVerificationResults .containsKey (authority )) {
526+ peerVerificationStatus = peerVerificationResults .get (authority );
527+ } else {
528+ TrustManager x509ExtendedTrustManager = null ;
529+ try {
530+ x509ExtendedTrustManager = x509ExtendedTrustManagerClass != null
531+ ? getX509ExtendedTrustManager ((TlsChannelCredentials ) channelCredentials ) : null ;
532+ } catch (GeneralSecurityException e ) {
533+ peerVerificationStatus = Status .UNAVAILABLE .withCause (e )
534+ .withDescription ("Could not verify authority due to failure getting "
535+ + "X509ExtendedTrustManager from TlsCredentials" );
536+ }
537+ if (x509ExtendedTrustManager == null ) {
538+ peerVerificationStatus = Status .UNAVAILABLE .withDescription (String .format ("Could not verify authority '%s' for "
539+ + "the rpc with no X509ExtendedTrustManager available" ,
540+ authority ));
541+ }
542+ if (x509ExtendedTrustManager != null ) {
543+ try {
544+ Certificate [] peerCertificates = sslSession .getPeerCertificates ();
545+ X509Certificate [] x509PeerCertificates = new X509Certificate [peerCertificates .length ];
546+ for (int i = 0 ; i < peerCertificates .length ; i ++) {
547+ x509PeerCertificates [i ] = (X509Certificate ) peerCertificates [i ];
548+ }
549+ checkServerTrustedMethod .invoke (x509ExtendedTrustManager , x509PeerCertificates ,
550+ "RSA" , new SslSocketWrapper ((SSLSocket ) socket , authority ));
551+ peerVerificationStatus = Status .OK ;
552+ } catch (SSLPeerUnverifiedException | InvocationTargetException
553+ | IllegalAccessException e ) {
554+ peerVerificationStatus = Status .UNAVAILABLE .withCause (e ).withDescription (
555+ "Peer verification failed" );
556+ }
557+ if (peerVerificationStatus != null ) {
558+ peerVerificationResults .put (authority , peerVerificationStatus );
559+ }
560+ }
561+ }
562+ if (peerVerificationStatus != null && !peerVerificationStatus .isOk ()) {
563+ if (enablePerRpcAuthorityCheck ) {
564+ clientStream .transportState ().transportReportStatus (
565+ peerVerificationStatus , RpcProgress .DROPPED , true , new Metadata ());
566+ return ;
567+ } else {
568+ if (peerVerificationStatus .getCause () != null ) {
569+ log .log (Level .WARNING , peerVerificationStatus .getDescription ()
570+ + ". This will be an error in the future." , peerVerificationStatus .getCause ());
571+ } else {
572+ log .log (Level .WARNING , peerVerificationStatus .getDescription ()
573+ + ". This will be an error in the future." );
574+ }
575+ }
576+ }
577+ }
559578 startStream (clientStream );
560579 }
561580 }
0 commit comments