|
| 1 | +/* |
| 2 | + * Copyright 2024 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.sql.core; |
| 18 | + |
| 19 | +import java.net.Socket; |
| 20 | +import java.security.cert.CertificateException; |
| 21 | +import java.security.cert.X509Certificate; |
| 22 | +import javax.naming.InvalidNameException; |
| 23 | +import javax.naming.ldap.LdapName; |
| 24 | +import javax.naming.ldap.Rdn; |
| 25 | +import javax.net.ssl.SSLEngine; |
| 26 | +import javax.net.ssl.X509ExtendedTrustManager; |
| 27 | + |
| 28 | +/** |
| 29 | + * This is a workaround for a known bug in Conscrypt crypto in how it handles X509 auth type. |
| 30 | + * OpenJDK interpres the X509 certificate auth type as "UNKNOWN" while Conscrypt interpret the same |
| 31 | + * certificate as auth type "GENERIC". This incompatibility causes problems in the JDK. |
| 32 | + * |
| 33 | + * <p>This adapter works around the issue by creating wrappers around all TrustManager instances. It |
| 34 | + * replaces "GENERIC" auth type with "UNKNOWN" auth type before delegating calls. |
| 35 | + * |
| 36 | + * <p>See https://github.com/google/conscrypt/issues/1033#issuecomment-982701272 |
| 37 | + */ |
| 38 | +class InstanceCheckingTrustManger extends X509ExtendedTrustManager { |
| 39 | + private final X509ExtendedTrustManager tm; |
| 40 | + private final CloudSqlInstanceName instanceName; |
| 41 | + |
| 42 | + public InstanceCheckingTrustManger( |
| 43 | + CloudSqlInstanceName instanceName, X509ExtendedTrustManager tm) { |
| 44 | + this.instanceName = instanceName; |
| 45 | + this.tm = tm; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public void checkClientTrusted(X509Certificate[] chain, String authType, Socket socket) |
| 50 | + throws CertificateException { |
| 51 | + tm.checkClientTrusted(chain, authType, socket); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void checkClientTrusted(X509Certificate[] chain, String authType, SSLEngine engine) |
| 56 | + throws CertificateException { |
| 57 | + tm.checkClientTrusted(chain, authType, engine); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public void checkClientTrusted(X509Certificate[] chain, String authType) |
| 62 | + throws CertificateException { |
| 63 | + tm.checkClientTrusted(chain, authType); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public void checkServerTrusted(X509Certificate[] chain, String authType, Socket socket) |
| 68 | + throws CertificateException { |
| 69 | + tm.checkServerTrusted(chain, authType, socket); |
| 70 | + checkCertificateChain(chain); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public void checkServerTrusted(X509Certificate[] chain, String authType, SSLEngine engine) |
| 75 | + throws CertificateException { |
| 76 | + tm.checkServerTrusted(chain, authType, engine); |
| 77 | + checkCertificateChain(chain); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public void checkServerTrusted(X509Certificate[] chain, String authType) |
| 82 | + throws CertificateException { |
| 83 | + tm.checkServerTrusted(chain, authType); |
| 84 | + checkCertificateChain(chain); |
| 85 | + } |
| 86 | + |
| 87 | + private void checkCertificateChain(X509Certificate[] chain) throws CertificateException { |
| 88 | + if (chain.length == 0) { |
| 89 | + throw new CertificateException("No server certificates in chain"); |
| 90 | + } |
| 91 | + if (chain[0].getSubjectX500Principal() == null) { |
| 92 | + throw new CertificateException("Subject is missing"); |
| 93 | + } |
| 94 | + |
| 95 | + String cn = null; |
| 96 | + |
| 97 | + try { |
| 98 | + String subject = chain[0].getSubjectX500Principal().getName(); |
| 99 | + LdapName subjectName = new LdapName(subject); |
| 100 | + for (Rdn rdn : subjectName.getRdns()) { |
| 101 | + if ("CN".equals(rdn.getType())) { |
| 102 | + cn = (String) rdn.getValue(); |
| 103 | + } |
| 104 | + } |
| 105 | + } catch (InvalidNameException e) { |
| 106 | + throw new CertificateException("Exception parsing the server certificate subject field", e); |
| 107 | + } |
| 108 | + |
| 109 | + if (cn == null) { |
| 110 | + throw new CertificateException("Server certificate subject does not contain a value for CN"); |
| 111 | + } |
| 112 | + |
| 113 | + // parse CN from subject. CN always comes last in the list. |
| 114 | + String instName = this.instanceName.getProjectId() + ":" + this.instanceName.getInstanceId(); |
| 115 | + if (!instName.equals(cn)) { |
| 116 | + throw new CertificateException( |
| 117 | + "Server certificate CN does not match instance name. Server certificate CN=" |
| 118 | + + cn |
| 119 | + + " Expected instance name: " |
| 120 | + + instName); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public X509Certificate[] getAcceptedIssuers() { |
| 126 | + return tm.getAcceptedIssuers(); |
| 127 | + } |
| 128 | +} |
0 commit comments