@@ -134,10 +134,11 @@ class OkHttpClientTransport implements ConnectionClientTransport, TransportExcep
134134 private static final Logger log = Logger .getLogger (OkHttpClientTransport .class .getName ());
135135 private static final String GRPC_ENABLE_PER_RPC_AUTHORITY_CHECK =
136136 "GRPC_ENABLE_PER_RPC_AUTHORITY_CHECK" ;
137+ static boolean enablePerRpcAuthorityCheck =
138+ GrpcUtil .getFlag (GRPC_ENABLE_PER_RPC_AUTHORITY_CHECK , false );
137139 private final ChannelCredentials channelCredentials ;
138140 private Socket sock ;
139141 private SSLSession sslSession ;
140- private final Logger logger = Logger .getLogger (OkHttpClientTransport .class .getName ());
141142
142143 private static Map <ErrorCode , Status > buildErrorCodeToStatusMap () {
143144 Map <ErrorCode , Status > errorToStatus = new EnumMap <>(ErrorCode .class );
@@ -179,9 +180,8 @@ private static Map<ErrorCode, Status> buildErrorCodeToStatusMap() {
179180 } catch (ClassNotFoundException e ) {
180181 // Per-rpc authority override via call options will be disallowed.
181182 } catch (NoSuchMethodException e ) {
182- // Should never happen.
183- Logger .getLogger (OkHttpClientTransport .class .getName ()).warning ("Method checkServerTrusted "
184- + "not found in javax.net.ssl.X509ExtendedTrustManager" );
183+ // Should never happen since X509ExtendedTrustManager was introduced in Android API level 24
184+ // along with checkServerTrusted.
185185 }
186186 }
187187
@@ -246,13 +246,13 @@ private static Map<ErrorCode, Status> buildErrorCodeToStatusMap() {
246246 private final boolean useGetForSafeMethods ;
247247 @ GuardedBy ("lock" )
248248 private final TransportTracer transportTracer ;
249- private final LinkedHashMap <String , Status > peerVerificationResults =
249+ private final Map <String , Status > peerVerificationResults = Collections . synchronizedMap (
250250 new LinkedHashMap <String , Status >() {
251251 @ Override
252252 protected boolean removeEldestEntry (Map .Entry <String , Status > eldest ) {
253253 return size () > 100 ;
254254 }
255- };
255+ }) ;
256256
257257 @ GuardedBy ("lock" )
258258 private final InUseStateAggregator <OkHttpClientStream > inUseState =
@@ -453,69 +453,52 @@ public ClientStream newStream(
453453 if (hostnameVerifier != null && socket instanceof SSLSocket
454454 && !hostnameVerifier .verify (callOptions .getAuthority (),
455455 ((SSLSocket ) socket ).getSession ())) {
456- if (GrpcUtil . getFlag ( GRPC_ENABLE_PER_RPC_AUTHORITY_CHECK , false ) ) {
456+ if (enablePerRpcAuthorityCheck ) {
457457 return new FailingClientStream (Status .UNAVAILABLE .withDescription (
458458 String .format ("HostNameVerifier verification failed for authority '%s'" ,
459459 callOptions .getAuthority ())), tracers );
460460 }
461- logger .warning (String .format ("HostNameVerifier verification failed for authority '%s'." ,
462- callOptions .getAuthority ()));
463461 }
464462 if (socket instanceof SSLSocket && callOptions .getAuthority () != null
465463 && channelCredentials != null && channelCredentials instanceof TlsChannelCredentials ) {
466464 Status peerVerificationStatus = null ;
467465 if (peerVerificationResults .containsKey (callOptions .getAuthority ())) {
468466 peerVerificationStatus = peerVerificationResults .get (callOptions .getAuthority ());
469467 } else {
470- TrustManager x509ExtendedTrustManager = null ;
471- boolean warningLogged = false ;
468+ TrustManager x509ExtendedTrustManager ;
472469 try {
473470 x509ExtendedTrustManager = x509ExtendedTrustManagerClass != null
474471 ? getX509ExtendedTrustManager ((TlsChannelCredentials ) channelCredentials ) : null ;
475- } catch (GeneralSecurityException e ) {
476- if (GrpcUtil .getFlag (GRPC_ENABLE_PER_RPC_AUTHORITY_CHECK , false )) {
477- return new FailingClientStream (Status .UNAVAILABLE .withDescription (
478- "Failure getting X509ExtendedTrustManager from TlsCredentials" ).withCause (e ),
479- tracers );
480- }
481- logger .warning (String .format ("Failure getting X509ExtendedTrustManager from "
482- + "TlsCredentials due to: %s" , e .getMessage ()));
483- warningLogged = true ;
484- }
485- if (x509ExtendedTrustManager == null ) {
486- if (GrpcUtil .getFlag (GRPC_ENABLE_PER_RPC_AUTHORITY_CHECK , false )) {
487- return new FailingClientStream (Status .UNAVAILABLE .withDescription (
488- "Can't allow authority override in rpc when X509ExtendedTrustManager is not "
489- + "available" ), tracers );
490- }
491- if (!warningLogged ) {
492- logger .warning ("Authority override set for rpc when X509ExtendedTrustManager is not "
493- + "available." );
494- }
495- } else {
496- try {
497- Certificate [] peerCertificates = sslSession .getPeerCertificates ();
498- X509Certificate [] x509PeerCertificates = new X509Certificate [peerCertificates .length ];
499- for (int i = 0 ; i < peerCertificates .length ; i ++) {
500- x509PeerCertificates [i ] = (X509Certificate ) peerCertificates [i ];
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 );
501477 }
502- // Should never happen
503- if (checkServerTrustedMethod == null ) {
504- peerVerificationStatus = Status .UNAVAILABLE .withDescription (
505- "Method checkServerTrusted not found in "
506- + "javax.net.ssl.X509ExtendedTrustManager" );
507- } else {
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+ }
508485 checkServerTrustedMethod .invoke (x509ExtendedTrustManager , x509PeerCertificates ,
509486 "RSA" , new SslSocketWrapper ((SSLSocket ) socket , callOptions .getAuthority ()));
510487 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 );
511493 }
512- } catch (SSLPeerUnverifiedException | InvocationTargetException
513- | IllegalAccessException e ) {
514- peerVerificationStatus = Status .UNAVAILABLE .withDescription (
515- String .format ("Failure in verifying authority '%s' against peer during rpc" ,
516- callOptions .getAuthority ())).withCause (e );
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 );
517501 }
518- peerVerificationResults .put (callOptions .getAuthority (), peerVerificationStatus );
519502 }
520503 }
521504 if (peerVerificationStatus != null && !peerVerificationStatus .isOk ()) {
@@ -1610,7 +1593,7 @@ public void alternateService(int streamId, String origin, ByteString protocol, S
16101593 /**
16111594 * SSLSocket wrapper that provides a fake SSLSession for handshake session.
16121595 */
1613- static class SslSocketWrapper extends NoopSslSocket {
1596+ static final class SslSocketWrapper extends NoopSslSocket {
16141597
16151598 private final SSLSession sslSession ;
16161599 private final SSLSocket sslSocket ;
0 commit comments