Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ok-http: Per-rpc call option authority verification #11754

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/src/main/java/io/grpc/ManagedChannelRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,4 @@ public ProviderNotFoundException(String msg) {
super(msg);
}
}
}
}
80 changes: 80 additions & 0 deletions core/src/main/java/io/grpc/internal/CertificateUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright 2024 The gRPC Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.grpc.internal;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.Collection;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.security.auth.x500.X500Principal;

/**
* Contains certificate/key PEM file utility method(s) for internal usage.
*/
public class CertificateUtils {
/**
* Creates X509TrustManagers using the provided CA certs.
*/
public static TrustManager[] createTrustManager(byte[] rootCerts)
throws GeneralSecurityException {
InputStream rootCertsStream = new ByteArrayInputStream(rootCerts);
try {
return io.grpc.internal.CertificateUtils.createTrustManager(rootCertsStream);
} finally {
GrpcUtil.closeQuietly(rootCertsStream);
}
}

/**
* Creates X509TrustManagers using the provided input stream of CA certs.
*/
public static TrustManager[] createTrustManager(InputStream rootCerts)
throws GeneralSecurityException {
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
try {
ks.load(null, null);
} catch (IOException ex) {
// Shouldn't really happen, as we're not loading any data.
throw new GeneralSecurityException(ex);
}
X509Certificate[] certs = CertificateUtils.getX509Certificates(rootCerts);
for (X509Certificate cert : certs) {
X500Principal principal = cert.getSubjectX500Principal();
ks.setCertificateEntry(principal.getName("RFC2253"), cert);
}

TrustManagerFactory trustManagerFactory =
TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(ks);
return trustManagerFactory.getTrustManagers();
}

private static X509Certificate[] getX509Certificates(InputStream inputStream)
throws CertificateException {
CertificateFactory factory = CertificateFactory.getInstance("X.509");
Collection<? extends Certificate> certs = factory.generateCertificates(inputStream);
return certs.toArray(new X509Certificate[0]);
}
}
132 changes: 132 additions & 0 deletions core/src/main/java/io/grpc/internal/NoopSslSession.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* Copyright 2024 The gRPC Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.grpc.internal;

import java.security.Principal;
import java.security.cert.Certificate;
import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSessionContext;

/** A no-op ssl session, to facilitate overriding only the required methods in specific
* implementations.
*/
public class NoopSslSession implements SSLSession {
@Override
public byte[] getId() {
return new byte[0];
}

@Override
public SSLSessionContext getSessionContext() {
return null;
}

@Override
@SuppressWarnings("deprecation")
public javax.security.cert.X509Certificate[] getPeerCertificateChain() {
throw new UnsupportedOperationException("This method is deprecated and marked for removal. "
+ "Use the getPeerCertificates() method instead.");
}

@Override
public long getCreationTime() {
return 0;
}

@Override
public long getLastAccessedTime() {
return 0;
}

@Override
public void invalidate() {
}

@Override
public boolean isValid() {
return false;
}

@Override
public void putValue(String s, Object o) {
}

@Override
public Object getValue(String s) {
return null;
}

@Override
public void removeValue(String s) {
}

@Override
public String[] getValueNames() {
return new String[0];
}

@Override
public Certificate[] getPeerCertificates() throws SSLPeerUnverifiedException {
return new Certificate[0];
}

@Override
public Certificate[] getLocalCertificates() {
return new Certificate[0];
}

@Override
public Principal getPeerPrincipal() throws SSLPeerUnverifiedException {
return null;
}

@Override
public Principal getLocalPrincipal() {
return null;
}

@Override
public String getCipherSuite() {
return null;
}

@Override
public String getProtocol() {
return null;
}

@Override
public String getPeerHost() {
return null;
}

@Override
public int getPeerPort() {
return 0;
}

@Override
public int getPacketBufferSize() {
return 0;
}

@Override
public int getApplicationBufferSize() {
return 0;
}
}
117 changes: 117 additions & 0 deletions okhttp/src/main/java/io/grpc/okhttp/NoopSslSocket.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Copyright 2024 The gRPC Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.grpc.okhttp;

import java.io.IOException;
import javax.net.ssl.HandshakeCompletedListener;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;

/** A no-op ssl socket, to facilitate overriding only the required methods in specific
* implementations.
*/
class NoopSslSocket extends SSLSocket {
@Override
public String[] getSupportedCipherSuites() {
return new String[0];
}

@Override
public String[] getEnabledCipherSuites() {
return new String[0];
}

@Override
public void setEnabledCipherSuites(String[] suites) {

}

@Override
public String[] getSupportedProtocols() {
return new String[0];
}

@Override
public String[] getEnabledProtocols() {
return new String[0];
}

@Override
public void setEnabledProtocols(String[] protocols) {

}

@Override
public SSLSession getSession() {
return null;
}

@Override
public void addHandshakeCompletedListener(HandshakeCompletedListener listener) {

}

@Override
public void removeHandshakeCompletedListener(HandshakeCompletedListener listener) {

}

@Override
public void startHandshake() throws IOException {

}

@Override
public void setUseClientMode(boolean mode) {

}

@Override
public boolean getUseClientMode() {
return false;
}

@Override
public void setNeedClientAuth(boolean need) {

}

@Override
public boolean getNeedClientAuth() {
return false;
}

@Override
public void setWantClientAuth(boolean want) {

}

@Override
public boolean getWantClientAuth() {
return false;
}

@Override
public void setEnableSessionCreation(boolean flag) {

}

@Override
public boolean getEnableSessionCreation() {
return false;
}
}
Loading
Loading