-
Notifications
You must be signed in to change notification settings - Fork 119
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
fix: Add TrustManagerFactory workaround for Conscrypt bug #1993
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* | ||
* 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 com.google.cloud.sql.core; | ||
|
||
import java.net.Socket; | ||
import java.security.cert.CertificateException; | ||
import java.security.cert.X509Certificate; | ||
import javax.net.ssl.SSLEngine; | ||
import javax.net.ssl.X509ExtendedTrustManager; | ||
|
||
/** | ||
* This is a workaround for a known bug in Conscrypt crypto in how it handles X509 auth type. | ||
* OpenJDK interpres the X509 certificate auth type as "UNKNOWN" while Conscrypt interpret the same | ||
* certificate as auth type "GENERIC". This incompatibility causes problems in the JDK. | ||
* | ||
* <p>This adapter works around the issue by creating wrappers around all TrustManager instances. It | ||
* replaces "GENERIC" auth type with "UNKNOWN" auth type before delegating calls. | ||
* | ||
* <p>See https://github.com/google/conscrypt/issues/1033#issuecomment-982701272 | ||
*/ | ||
class ConscryptWorkaroundDelegatingTrustManger extends X509ExtendedTrustManager { | ||
private final X509ExtendedTrustManager tm; | ||
|
||
ConscryptWorkaroundDelegatingTrustManger(X509ExtendedTrustManager tm) { | ||
this.tm = tm; | ||
} | ||
|
||
@Override | ||
public void checkClientTrusted(X509Certificate[] chain, String authType, Socket socket) | ||
throws CertificateException { | ||
tm.checkClientTrusted(chain, fixAuthType(authType), socket); | ||
} | ||
|
||
@Override | ||
public void checkClientTrusted(X509Certificate[] chain, String authType, SSLEngine engine) | ||
throws CertificateException { | ||
tm.checkClientTrusted(chain, fixAuthType(authType), engine); | ||
} | ||
|
||
@Override | ||
public void checkClientTrusted(X509Certificate[] chain, String authType) | ||
throws CertificateException { | ||
tm.checkClientTrusted(chain, fixAuthType(authType)); | ||
} | ||
|
||
@Override | ||
public void checkServerTrusted(X509Certificate[] chain, String authType, Socket socket) | ||
throws CertificateException { | ||
tm.checkServerTrusted(chain, fixAuthType(authType), socket); | ||
} | ||
|
||
@Override | ||
public void checkServerTrusted(X509Certificate[] chain, String authType, SSLEngine engine) | ||
throws CertificateException { | ||
tm.checkServerTrusted(chain, fixAuthType(authType), engine); | ||
} | ||
|
||
@Override | ||
public void checkServerTrusted(X509Certificate[] chain, String authType) | ||
throws CertificateException { | ||
tm.checkServerTrusted(chain, fixAuthType(authType)); | ||
} | ||
|
||
@Override | ||
public X509Certificate[] getAcceptedIssuers() { | ||
return tm.getAcceptedIssuers(); | ||
} | ||
|
||
private String fixAuthType(String authType) { | ||
if ("GENERIC".equals(authType)) { | ||
return "UNKNOWN"; | ||
} | ||
return authType; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* | ||
* 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 com.google.cloud.sql.core; | ||
|
||
import java.security.NoSuchAlgorithmException; | ||
import java.security.Provider; | ||
import java.security.Security; | ||
import javax.net.ssl.SSLContext; | ||
import javax.net.ssl.TrustManagerFactory; | ||
|
||
/** | ||
* This is a workaround for a known bug in Conscrypt crypto in how it handles X509 auth type. | ||
* OpenJDK interpres the X509 certificate auth type as "UNKNOWN" while Conscrypt interpret the same | ||
* certificate as auth type "GENERIC". This incompatibility causes problems in the JDK. | ||
* | ||
* <p>This adapter works around the issue by creating wrappers around all TrustManager instances. It | ||
* replaces "GENERIC" auth type with "UNKNOWN" auth type before delegating calls. | ||
* | ||
* <p>In the JVM, we need to implement 3 classes to make sure that we are capturing all of the | ||
* TrustManager instances created when Conscrypt is the Java Crypto provider so that we can wrap | ||
* them with the ConscryptWorkaroundTrustManager: | ||
* | ||
* <p>class ConscryptWorkaroundTrustManagerFactory extends TrustManagerFactory - has a bunch of | ||
* final methods that delegate to a TrustManagerFactorySpi. class | ||
* ConscryptWorkaroundTrustManagerFactorySpi implements TrustManagerFactorySpi - can actually | ||
* intercept and delegate calls related to trust managers and wrap them with | ||
* ConscryptWorkaroundTrustManager ConscryptWorkaroundTrustManager - the workaround for the | ||
* Conscrypt bug. | ||
* | ||
* <p>See https://github.com/google/conscrypt/issues/1033#issuecomment-982701272 | ||
*/ | ||
class ConscryptWorkaroundTrustManagerFactory extends TrustManagerFactory { | ||
private static final boolean CONSCRYPT_TLS; | ||
|
||
static { | ||
// Provider name is "Conscrypt", hardcoded string in the library source: | ||
// https://github.com/google/conscrypt/blob/655ad5069e1cb4d1989b8117eaf090371885af99/openjdk/src/main/java/org/conscrypt/Platform.java#L149 | ||
Provider p = Security.getProvider("Conscrypt"); | ||
enocom marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (p != null) { | ||
try { | ||
SSLContext ctx = SSLContext.getInstance("TLS"); | ||
Provider prov = ctx.getProvider(); | ||
CONSCRYPT_TLS = "Conscrypt".equals(prov.getName()); | ||
} catch (NoSuchAlgorithmException e) { | ||
throw new RuntimeException("Unable to load algorithm TLS", e); | ||
} | ||
} else { | ||
CONSCRYPT_TLS = false; | ||
} | ||
} | ||
|
||
/** Returns true if the Conscrypt Java Crypto Extension is installed. */ | ||
static boolean isWorkaroundNeeded() { | ||
return CONSCRYPT_TLS; | ||
} | ||
|
||
static ConscryptWorkaroundTrustManagerFactory newInstance() throws NoSuchAlgorithmException { | ||
TrustManagerFactory delegate = TrustManagerFactory.getInstance("X.509"); | ||
return new ConscryptWorkaroundTrustManagerFactory(delegate); | ||
} | ||
|
||
private ConscryptWorkaroundTrustManagerFactory(TrustManagerFactory delegate) { | ||
super( | ||
new ConscryptWorkaroundTrustManagerFactorySpi(delegate), | ||
delegate.getProvider(), | ||
delegate.getAlgorithm()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* | ||
* 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 com.google.cloud.sql.core; | ||
|
||
import java.security.InvalidAlgorithmParameterException; | ||
import java.security.KeyStore; | ||
import java.security.KeyStoreException; | ||
import javax.net.ssl.ManagerFactoryParameters; | ||
import javax.net.ssl.TrustManager; | ||
import javax.net.ssl.TrustManagerFactory; | ||
import javax.net.ssl.TrustManagerFactorySpi; | ||
import javax.net.ssl.X509ExtendedTrustManager; | ||
|
||
/** | ||
* This is a workaround for a known bug in Conscrypt crypto in how it handles X509 auth type. | ||
* OpenJDK interpres the X509 certificate auth type as "UNKNOWN" while Conscrypt interpret the same | ||
* certificate as auth type "GENERIC". This incompatibility causes problems in the JDK. | ||
* | ||
* <p>This adapter works around the issue by creating wrappers around all TrustManager instances. It | ||
* replaces "GENERIC" auth type with "UNKNOWN" auth type before delegating calls. | ||
* | ||
* <p>See https://github.com/google/conscrypt/issues/1033#issuecomment-982701272 | ||
*/ | ||
class ConscryptWorkaroundTrustManagerFactorySpi extends TrustManagerFactorySpi { | ||
private final TrustManagerFactory delegate; | ||
|
||
ConscryptWorkaroundTrustManagerFactorySpi(TrustManagerFactory delegate) { | ||
this.delegate = delegate; | ||
} | ||
|
||
@Override | ||
protected void engineInit(KeyStore ks) throws KeyStoreException { | ||
delegate.init(ks); | ||
} | ||
|
||
@Override | ||
protected void engineInit(ManagerFactoryParameters spec) | ||
throws InvalidAlgorithmParameterException { | ||
delegate.init(spec); | ||
} | ||
|
||
@Override | ||
protected TrustManager[] engineGetTrustManagers() { | ||
TrustManager[] tms = delegate.getTrustManagers(); | ||
TrustManager[] delegates = new TrustManager[tms.length]; | ||
for (int i = 0; i < tms.length; i++) { | ||
if (tms[i] instanceof X509ExtendedTrustManager) { | ||
delegates[i] = | ||
new ConscryptWorkaroundDelegatingTrustManger((X509ExtendedTrustManager) tms[i]); | ||
} else { | ||
|
||
delegates[i] = tms[i]; | ||
} | ||
} | ||
return delegates; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -382,8 +382,20 @@ private SslData createSslData( | |
KeyStore trustedKeyStore = KeyStore.getInstance(KeyStore.getDefaultType()); | ||
trustedKeyStore.load(null, null); | ||
trustedKeyStore.setCertificateEntry("instance", instanceMetadata.getInstanceCaCertificate()); | ||
TrustManagerFactory tmf = TrustManagerFactory.getInstance("X.509"); | ||
TrustManagerFactory tmf; | ||
|
||
// Note: This is a workaround for Conscrypt bug #1033 | ||
// Conscrypt is the JCE provider on some Google Cloud runtimes like DataProc. | ||
// https://github.com/google/conscrypt/issues/1033 | ||
// | ||
if (ConscryptWorkaroundTrustManagerFactory.isWorkaroundNeeded()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How do these lines relate to ConscryptWorkaroundTrustManagerFactorySpi? Do we need both? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the JVM, we need to implement 3 classes to make sure that we are capturing all of the TrustManager instances created by the default Java Crypto provider and wrapping them with the ConscryptWorkaroundTrustManager:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got it. Thanks. Personally, this context would be useful to me in the future so I'd love to see this capture in the commit message or in a comment somewhere. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added this extra info to the ConscryptWorkaroundTrustManagerFactory class javadoc. |
||
tmf = ConscryptWorkaroundTrustManagerFactory.newInstance(); | ||
} else { | ||
tmf = TrustManagerFactory.getInstance("X.509"); | ||
} | ||
|
||
tmf.init(trustedKeyStore); | ||
|
||
SSLContext sslContext; | ||
|
||
try { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is this profile for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This profile allows you to run the Postgres JDBC integration tests using Conscrypt crypto.