Skip to content

Commit 909b863

Browse files
committed
Fixes.
1 parent 4d71cce commit 909b863

File tree

1 file changed

+32
-19
lines changed

1 file changed

+32
-19
lines changed

okhttp/src/main/java/io/grpc/okhttp/OkHttpClientTransport.java

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@
129129
import okio.Okio;
130130
import okio.Source;
131131
import okio.Timeout;
132-
import sun.security.ssl.SSLSocketImpl;
133132

134133
/**
135134
* A okhttp-based {@link ConnectionClientTransport} implementation.
@@ -431,8 +430,12 @@ public ClientStream newStream(
431430
StatsTraceContext statsTraceContext =
432431
StatsTraceContext.newClientContext(tracers, getAttributes(), headers);
433432
if (socket instanceof SSLSocket && callOptions.getAuthority() != null && channelCredentials != null && channelCredentials instanceof TlsChannelCredentials) {
434-
Optional<TrustManager> x509ExtendedTrustManager;
435-
try {
433+
boolean isAuthorityValid;
434+
if (authoritiesAllowedForPeer.containsKey(callOptions.getAuthority())) {
435+
isAuthorityValid = authoritiesAllowedForPeer.get(callOptions.getAuthority());
436+
} else {
437+
Optional<TrustManager> x509ExtendedTrustManager;
438+
try {
436439
x509ExtendedTrustManager = getX509ExtendedTrustManager(
437440
(TlsChannelCredentials) channelCredentials);
438441
} catch (GeneralSecurityException e) {
@@ -441,23 +444,28 @@ public ClientStream newStream(
441444
"Failure getting X509ExtendedTrustManager from TlsCredentials"),
442445
tracers);
443446
}
444-
if (!x509ExtendedTrustManager.isPresent()) {
445-
return new FailingClientStream(Status.INTERNAL.withDescription(
446-
"Can't allow authority override in rpc when X509ExtendedTrustManager is not available"),
447-
tracers);
448-
}
449-
try {
450-
Certificate[] peerCertificates = sslSession.getPeerCertificates();
451-
X509Certificate[] x509PeerCertificates = new X509Certificate[peerCertificates.length];
452-
for (int i = 0; i < peerCertificates.length; i++) {
453-
x509PeerCertificates[i] = (X509Certificate) peerCertificates[i];
447+
if (!x509ExtendedTrustManager.isPresent()) {
448+
return new FailingClientStream(Status.INTERNAL.withDescription(
449+
"Can't allow authority override in rpc when X509ExtendedTrustManager is not available"),
450+
tracers);
451+
}
452+
try {
453+
Certificate[] peerCertificates = sslSession.getPeerCertificates();
454+
X509Certificate[] x509PeerCertificates = new X509Certificate[peerCertificates.length];
455+
for (int i = 0; i < peerCertificates.length; i++) {
456+
x509PeerCertificates[i] = (X509Certificate) peerCertificates[i];
457+
}
458+
((X509ExtendedTrustManager) x509ExtendedTrustManager.get()).checkServerTrusted(
459+
x509PeerCertificates, "RSA",
460+
new SslSocketWrapper((SSLSocket) socket, callOptions.getAuthority()));
461+
authoritiesAllowedForPeer.put(callOptions.getAuthority(), true);
462+
} catch (SSLPeerUnverifiedException | CertificateException e) {
463+
log.log(Level.FINE, "Failure in verifying authority against peer", e);
464+
authoritiesAllowedForPeer.put(callOptions.getAuthority(), false);
465+
return new FailingClientStream(Status.INTERNAL.withDescription(
466+
"Failure in verifying authority against peer"),
467+
tracers);
454468
}
455-
((X509ExtendedTrustManager) x509ExtendedTrustManager.get()).checkServerTrusted(x509PeerCertificates, "RSA", new SslSocketWrapper((SSLSocket) socket, callOptions.getAuthority()));
456-
} catch (SSLPeerUnverifiedException | CertificateException e) {
457-
log.log(Level.FINE, "Failure in verifying authority against peer", e);
458-
return new FailingClientStream(Status.INTERNAL.withDescription(
459-
"Failure in verifying authority against peer"),
460-
tracers);
461469
}
462470
}
463471
// FIXME: it is likely wrong to pass the transportTracer here as it'll exit the lock's scope
@@ -1674,6 +1682,11 @@ public String getPeerHost() {
16741682
return peerHost;
16751683
}
16761684

1685+
@SuppressWarnings("deprecation")
1686+
public javax.security.cert.X509Certificate[] getPeerCertificateChain() throws SSLPeerUnverifiedException {
1687+
throw new UnsupportedOperationException("This method is deprecated and marked for removal. Use the getPeerCertificates() method instead.");
1688+
}
1689+
16771690
@Override
16781691
public byte[] getId() {
16791692
return new byte[0];

0 commit comments

Comments
 (0)