|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Redistribution and use in source and binary forms, with or without |
| 5 | + * modification, are permitted provided that the following conditions are |
| 6 | + * met: |
| 7 | + * |
| 8 | + * * Redistributions of source code must retain the above copyright |
| 9 | + * notice, this list of conditions and the following disclaimer. |
| 10 | + * * Redistributions in binary form must reproduce the above |
| 11 | + * copyright notice, this list of conditions and the following disclaimer |
| 12 | + * in the documentation and/or other materials provided with the |
| 13 | + * distribution. |
| 14 | + * |
| 15 | + * * Neither the name of Google LLC nor the names of its |
| 16 | + * contributors may be used to endorse or promote products derived from |
| 17 | + * this software without specific prior written permission. |
| 18 | + * |
| 19 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | + */ |
| 31 | + |
| 32 | +package com.google.auth.oauth2; |
| 33 | + |
| 34 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 35 | + |
| 36 | +import com.google.common.annotations.VisibleForTesting; |
| 37 | +import java.io.ByteArrayInputStream; |
| 38 | +import java.io.IOException; |
| 39 | +import java.io.InputStream; |
| 40 | +import java.nio.file.Files; |
| 41 | +import java.nio.file.Paths; |
| 42 | +import java.security.cert.CertificateEncodingException; |
| 43 | +import java.security.cert.CertificateException; |
| 44 | +import java.security.cert.CertificateFactory; |
| 45 | +import java.security.cert.X509Certificate; |
| 46 | +import java.util.Base64; |
| 47 | + |
| 48 | +/** |
| 49 | + * Provider for retrieving the subject tokens for {@link IdentityPoolCredentials} by reading an |
| 50 | + * X.509 certificate from the filesystem. The certificate file (e.g., PEM or DER encoded) is read, |
| 51 | + * the leaf certificate is base64-encoded (DER format), wrapped in a JSON array, and used as the |
| 52 | + * subject token for STS exchange. |
| 53 | + */ |
| 54 | +public class CertificateIdentityPoolSubjectTokenSupplier |
| 55 | + implements IdentityPoolSubjectTokenSupplier { |
| 56 | + |
| 57 | + private final IdentityPoolCredentialSource credentialSource; |
| 58 | + |
| 59 | + CertificateIdentityPoolSubjectTokenSupplier(IdentityPoolCredentialSource credentialSource) { |
| 60 | + this.credentialSource = checkNotNull(credentialSource, "credentialSource cannot be null"); |
| 61 | + // This check ensures that the credential source was intended for certificate usage. |
| 62 | + // IdentityPoolCredentials logic should guarantee credentialLocation is set in this case. |
| 63 | + checkNotNull( |
| 64 | + credentialSource.getCertificateConfig(), |
| 65 | + "credentialSource.certificateConfig cannot be null when creating" |
| 66 | + + " CertificateIdentityPoolSubjectTokenSupplier"); |
| 67 | + } |
| 68 | + |
| 69 | + private static X509Certificate loadLeafCertificate(String path) |
| 70 | + throws IOException, CertificateException { |
| 71 | + byte[] leafCertBytes = Files.readAllBytes(Paths.get(path)); |
| 72 | + return parseCertificate(leafCertBytes); |
| 73 | + } |
| 74 | + |
| 75 | + @VisibleForTesting |
| 76 | + static X509Certificate parseCertificate(byte[] certData) throws CertificateException { |
| 77 | + if (certData == null || certData.length == 0) { |
| 78 | + throw new IllegalArgumentException( |
| 79 | + "Invalid certificate data: Certificate file is empty or null."); |
| 80 | + } |
| 81 | + |
| 82 | + try { |
| 83 | + CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); |
| 84 | + InputStream certificateStream = new ByteArrayInputStream(certData); |
| 85 | + return (X509Certificate) certificateFactory.generateCertificate(certificateStream); |
| 86 | + } catch (CertificateException e) { |
| 87 | + // Catch the original exception to add context about the operation being performed. |
| 88 | + // This helps pinpoint the failure point during debugging. |
| 89 | + throw new CertificateException("Failed to parse X.509 certificate data.", e); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private static String encodeCert(X509Certificate certificate) |
| 94 | + throws CertificateEncodingException { |
| 95 | + return Base64.getEncoder().encodeToString(certificate.getEncoded()); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Retrieves the X509 subject token. This method loads the leaf certificate specified by the |
| 100 | + * {@code credentialSource.credentialLocation}. The subject token is constructed as a JSON array |
| 101 | + * containing the base64-encoded (DER format) leaf certificate. This JSON array serves as the |
| 102 | + * subject token for mTLS authentication. |
| 103 | + * |
| 104 | + * @param context The external account supplier context. This parameter is currently not used in |
| 105 | + * this implementation. |
| 106 | + * @return The JSON string representation of the base64-encoded leaf certificate in a JSON array. |
| 107 | + * @throws IOException If an I/O error occurs while reading the certificate file. |
| 108 | + */ |
| 109 | + @Override |
| 110 | + public String getSubjectToken(ExternalAccountSupplierContext context) throws IOException { |
| 111 | + try { |
| 112 | + // credentialSource.credentialLocation is expected to be non-null here, |
| 113 | + // set during IdentityPoolCredentials construction for certificate type. |
| 114 | + X509Certificate leafCert = loadLeafCertificate(credentialSource.getCredentialLocation()); |
| 115 | + String encodedLeafCert = encodeCert(leafCert); |
| 116 | + |
| 117 | + java.util.List<String> certChain = new java.util.ArrayList<>(); |
| 118 | + certChain.add(encodedLeafCert); |
| 119 | + |
| 120 | + return OAuth2Utils.JSON_FACTORY.toString(certChain); |
| 121 | + } catch (CertificateException e) { |
| 122 | + // Catch CertificateException to provide a more specific error message including |
| 123 | + // the path of the file that failed to parse, and re-throw as IOException |
| 124 | + // as expected by the getSubjectToken method signature for I/O related issues. |
| 125 | + throw new IOException( |
| 126 | + "Failed to parse certificate(s) from: " + credentialSource.getCredentialLocation(), e); |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments