|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package cloudsql |
| 16 | + |
| 17 | +import ( |
| 18 | + "crypto/tls" |
| 19 | + "crypto/x509" |
| 20 | + "fmt" |
| 21 | + |
| 22 | + "cloud.google.com/go/cloudsqlconn/errtype" |
| 23 | + "cloud.google.com/go/cloudsqlconn/instance" |
| 24 | +) |
| 25 | + |
| 26 | +// verifyPeerCertificateFunc creates a VerifyPeerCertificate function with the |
| 27 | +// custom TLS verification logic to gracefully and securely handle deviations |
| 28 | +// from standard TLS hostname verification in existing Cloud SQL instance |
| 29 | +// server certificates. |
| 30 | +// |
| 31 | +// This is the verification algorithm: |
| 32 | +// |
| 33 | +// 1. Verify the server cert CA, using the CA certs from the instance metadata. |
| 34 | +// Reject the certificate if the CA is invalid. |
| 35 | +// |
| 36 | +// 2. Check that the server cert contains a SubjectAlternativeName matching the |
| 37 | +// DNS name in the connector configuration OR the DNS Name from the instance |
| 38 | +// metadata |
| 39 | +// |
| 40 | +// 3. If the SubjectAlternativeName does not match, and if the server cert |
| 41 | +// Subject.CN field is not empty, check that the Subject.CN field contains |
| 42 | +// the instance name. |
| 43 | +// |
| 44 | +// Reject the certificate if both the #2 SAN check and #3 CN checks fail. |
| 45 | +// |
| 46 | +// To summarize the deviations from standard TLS hostname verification: |
| 47 | +// |
| 48 | +// Historically, Cloud SQL creates server certificates with the instance name in |
| 49 | +// the Subject.CN field in the format "my-project:my-instance". The connector is |
| 50 | +// expected to check that the instance name that the connector was configured to |
| 51 | +// dial matches the server certificate Subject.CN field. Thus, the Subject.CN |
| 52 | +// field for most Cloud SQL instances does not contain a well-formed DNS Name. |
| 53 | +// |
| 54 | +// The default Go TLS hostname verification TLSConfig.serverName may be compared |
| 55 | +// with the Subject.CN field if Subject.CN contains a well-formed DNS name. |
| 56 | +// So the Cloud SQL server certs break the standard hostname verification in Go. |
| 57 | +// See: |
| 58 | +// - https://github.com/GoogleCloudPlatform/cloudsql-proxy/issues/194 |
| 59 | +// - https://tip.golang.org/doc/go1.11#crypto/x509 |
| 60 | +// |
| 61 | +// Also, there are times when the instance metadata reports that an instance has |
| 62 | +// a DNS name, but that DNS name does not yet appear in the SAN records of the |
| 63 | +// server certificate. The client should fall back to validating the hostname |
| 64 | +// using the instance name in the Subject.CN field. |
| 65 | +func verifyPeerCertificateFunc( |
| 66 | + serverName string, cn instance.ConnName, roots *x509.CertPool, |
| 67 | +) func(certs [][]byte, chain [][]*x509.Certificate) error { |
| 68 | + return func(rawCerts [][]byte, _ [][]*x509.Certificate) error { |
| 69 | + if len(rawCerts) == 0 { |
| 70 | + return errtype.NewDialError( |
| 71 | + "no certificate to verify", cn.String(), nil, |
| 72 | + ) |
| 73 | + } |
| 74 | + // Parse the raw certificates |
| 75 | + certs := make([]*x509.Certificate, 0, len(rawCerts)) |
| 76 | + var err error |
| 77 | + for _, certBytes := range rawCerts { |
| 78 | + cert, err := x509.ParseCertificate(certBytes) |
| 79 | + if err != nil { |
| 80 | + return errtype.NewDialError( |
| 81 | + "failed to parse X.509 certificate", cn.String(), err, |
| 82 | + ) |
| 83 | + } |
| 84 | + certs = append(certs, cert) |
| 85 | + } |
| 86 | + serverCert := certs[0] |
| 87 | + |
| 88 | + // Verify the validity of the certificate chain |
| 89 | + _, err = serverCert.Verify(x509.VerifyOptions{ |
| 90 | + Roots: roots, |
| 91 | + }) |
| 92 | + if err != nil { |
| 93 | + err = &tls.CertificateVerificationError{ |
| 94 | + UnverifiedCertificates: certs, |
| 95 | + Err: err, |
| 96 | + } |
| 97 | + return errtype.NewDialError( |
| 98 | + "failed to verify certificate", cn.String(), err, |
| 99 | + ) |
| 100 | + } |
| 101 | + |
| 102 | + var serverNameErr error |
| 103 | + |
| 104 | + if serverName == "" { |
| 105 | + // The instance has no DNS name. |
| 106 | + // Verify only the CN |
| 107 | + return verifyCn(cn, serverCert) |
| 108 | + } |
| 109 | + |
| 110 | + // The instance has a DNS name. |
| 111 | + // First, verify the server hostname |
| 112 | + serverNameErr = serverCert.VerifyHostname(serverName) |
| 113 | + if serverNameErr != nil { |
| 114 | + // If that failed, verify the CN field. |
| 115 | + cnErr := verifyCn(cn, serverCert) |
| 116 | + if cnErr != nil { |
| 117 | + // If both failed, return the server hostname error. |
| 118 | + serverNameErr = &tls.CertificateVerificationError{ |
| 119 | + UnverifiedCertificates: certs, |
| 120 | + Err: serverNameErr, |
| 121 | + } |
| 122 | + return serverNameErr |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + // All checks passed |
| 127 | + return nil |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func verifyCn(cn instance.ConnName, cert *x509.Certificate) error { |
| 132 | + // Reject CN check if the certificate CN field is empty |
| 133 | + if cert.Subject.CommonName == "" { |
| 134 | + return errtype.NewDialError( |
| 135 | + fmt.Sprintf( |
| 136 | + "certificate CN was empty, expected %q", |
| 137 | + cert.Subject.CommonName, |
| 138 | + ), |
| 139 | + cn.String(), |
| 140 | + nil, |
| 141 | + ) |
| 142 | + } |
| 143 | + |
| 144 | + // Verify the CN field matches the instance name |
| 145 | + certInstanceName := fmt.Sprintf("%s:%s", cn.Project(), cn.Name()) |
| 146 | + if cert.Subject.CommonName != certInstanceName { |
| 147 | + return errtype.NewDialError( |
| 148 | + fmt.Sprintf( |
| 149 | + "certificate had CN %q, expected %q", |
| 150 | + cert.Subject.CommonName, certInstanceName, |
| 151 | + ), |
| 152 | + cn.String(), |
| 153 | + nil, |
| 154 | + ) |
| 155 | + } |
| 156 | + return nil |
| 157 | +} |
0 commit comments