|
| 1 | +// Copyright 2025 The TensorStore Authors |
| 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 | +// http://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 | +#include "tensorstore/internal/http/self_signed_cert.h" |
| 16 | + |
| 17 | +#include <stdint.h> |
| 18 | + |
| 19 | +#include <memory> |
| 20 | +#include <string> |
| 21 | +#include <utility> |
| 22 | +#include <vector> |
| 23 | + |
| 24 | +#include "absl/log/absl_check.h" |
| 25 | +#include "absl/status/status.h" |
| 26 | +#include "absl/strings/str_cat.h" |
| 27 | +#include <openssl/asn1.h> |
| 28 | +#include <openssl/bio.h> |
| 29 | +#include <openssl/bn.h> |
| 30 | +#include <openssl/err.h> |
| 31 | +#include <openssl/evp.h> |
| 32 | +#include <openssl/pem.h> |
| 33 | +#include <openssl/rsa.h> |
| 34 | +#include <openssl/x509.h> |
| 35 | +#include "tensorstore/internal/source_location.h" |
| 36 | +#include "tensorstore/util/result.h" |
| 37 | +#include "tensorstore/util/status.h" |
| 38 | + |
| 39 | +namespace tensorstore { |
| 40 | +namespace internal_http { |
| 41 | +namespace { |
| 42 | + |
| 43 | +struct EVP_PKEYFree { |
| 44 | + void operator()(EVP_PKEY* pkey) { EVP_PKEY_free(pkey); } |
| 45 | +}; |
| 46 | + |
| 47 | +struct X509Free { |
| 48 | + void operator()(X509* x509) { X509_free(x509); } |
| 49 | +}; |
| 50 | + |
| 51 | +struct X509NameFree { |
| 52 | + void operator()(X509_NAME* name) { X509_NAME_free(name); } |
| 53 | +}; |
| 54 | + |
| 55 | +struct BIOFree { |
| 56 | + void operator()(BIO* bio) { BIO_free(bio); } |
| 57 | +}; |
| 58 | + |
| 59 | +struct BIGNUMFree { |
| 60 | + void operator()(BIGNUM* bn) { BN_free(bn); } |
| 61 | +}; |
| 62 | + |
| 63 | +struct RSAFree { |
| 64 | + void operator()(RSA* rsa) { RSA_free(rsa); } |
| 65 | +}; |
| 66 | + |
| 67 | +std::unique_ptr<X509_NAME, X509NameFree> CreateName( |
| 68 | + const std::vector<std::pair<std::string, std::string>>& name_parts) { |
| 69 | + std::unique_ptr<X509_NAME, X509NameFree> name(X509_NAME_new()); |
| 70 | + for (const auto& part : name_parts) { |
| 71 | + X509_NAME_add_entry_by_txt(name.get(), part.first.c_str(), MBSTRING_ASC, |
| 72 | + (unsigned char*)part.second.c_str(), -1, -1, 0); |
| 73 | + } |
| 74 | + return name; |
| 75 | +} |
| 76 | + |
| 77 | +// Append the OpenSSL error strings to the status message. |
| 78 | +absl::Status OpenSslError(std::string message, |
| 79 | + SourceLocation loc = SourceLocation::current()) { |
| 80 | + for (int line = 0; line < 4; ++line) { |
| 81 | + absl::StrAppend(&message, "\n"); |
| 82 | + const char* extra_data = nullptr; |
| 83 | + const char* file_name = nullptr; |
| 84 | + int line_number = 0; |
| 85 | + int flags = 0; |
| 86 | + // This extracts the top error code from the error codes stack. |
| 87 | + const uint32_t error_code = |
| 88 | + ERR_get_error_line_data(&file_name, &line_number, &extra_data, &flags); |
| 89 | + if (error_code == 0) { // No more error codes. |
| 90 | + break; |
| 91 | + } |
| 92 | + if (file_name != nullptr) { |
| 93 | + absl::StrAppend(&message, file_name, ":", line_number, " "); |
| 94 | + } |
| 95 | + const char* reason_error_string = ERR_reason_error_string(error_code); |
| 96 | + if (reason_error_string != nullptr) { |
| 97 | + absl::StrAppend(&message, reason_error_string); |
| 98 | + } else { |
| 99 | + absl::StrAppend(&message, error_code); |
| 100 | + } |
| 101 | + if (extra_data != nullptr && (flags & ERR_TXT_STRING)) { |
| 102 | + absl::StrAppend(&message, " - ", extra_data); |
| 103 | + } |
| 104 | + } |
| 105 | + auto status = absl::InternalError(message); |
| 106 | + MaybeAddSourceLocation(status, loc); |
| 107 | + return status; |
| 108 | +} |
| 109 | + |
| 110 | +} // namespace |
| 111 | + |
| 112 | +Result<SelfSignedCertificate> GenerateSelfSignedCerts() { |
| 113 | + std::unique_ptr<EVP_PKEY, EVP_PKEYFree> pkey(EVP_PKEY_new()); |
| 114 | + |
| 115 | + { |
| 116 | + std::unique_ptr<BIGNUM, BIGNUMFree> bn(BN_new()); |
| 117 | + std::unique_ptr<RSA, RSAFree> rsa(RSA_new()); |
| 118 | + |
| 119 | + ABSL_CHECK(BN_set_word(bn.get(), RSA_F4)); |
| 120 | + ABSL_CHECK(RSA_generate_key_ex(rsa.get(), 2048, bn.get(), nullptr)); |
| 121 | + ABSL_CHECK(EVP_PKEY_assign_RSA(pkey.get(), rsa.release())); |
| 122 | + } |
| 123 | + |
| 124 | + std::unique_ptr<X509, X509Free> x509(X509_new()); |
| 125 | + |
| 126 | + // TODO: Random serial number? |
| 127 | + ABSL_CHECK(ASN1_INTEGER_set(X509_get_serialNumber(x509.get()), 1)); |
| 128 | + ABSL_CHECK(X509_gmtime_adj(X509_get_notBefore(x509.get()), 0)); |
| 129 | + ABSL_CHECK(X509_gmtime_adj(X509_get_notAfter(x509.get()), 3600L)); // 1 hour |
| 130 | + |
| 131 | + if (!X509_set_pubkey(x509.get(), pkey.get())) { |
| 132 | + return OpenSslError("Failed to set public key on certificate"); |
| 133 | + } |
| 134 | + |
| 135 | + auto name = CreateName({ |
| 136 | + {"C", "CA"}, |
| 137 | + {"O", "Tensorstore Test"}, |
| 138 | + {"CN", "localhost"}, |
| 139 | + }); |
| 140 | + if (!X509_set_issuer_name(x509.get(), name.get())) { |
| 141 | + return OpenSslError("Failed to set issuer name on certificate"); |
| 142 | + } |
| 143 | + |
| 144 | + // Sign the certificate. |
| 145 | + if (!X509_sign(x509.get(), pkey.get(), EVP_sha256())) { |
| 146 | + return OpenSslError("Failed to sign x509 certificate"); |
| 147 | + } |
| 148 | + |
| 149 | + // Output the certificate and private key to PEM. |
| 150 | + std::unique_ptr<BIO, BIOFree> bio_pem(BIO_new(BIO_s_mem())); |
| 151 | + if (!PEM_write_bio_PrivateKey(bio_pem.get(), pkey.get(), nullptr, nullptr, 0, |
| 152 | + 0, nullptr)) { |
| 153 | + return OpenSslError("Failed to generate certificate"); |
| 154 | + } |
| 155 | + |
| 156 | + SelfSignedCertificate result; |
| 157 | + auto& key_pem = result.key_pem; |
| 158 | + |
| 159 | + key_pem.resize(BIO_pending(bio_pem.get())); |
| 160 | + if (BIO_read(bio_pem.get(), key_pem.data(), key_pem.size()) != |
| 161 | + key_pem.size()) { |
| 162 | + return OpenSslError("Failed to generate certificate"); |
| 163 | + } |
| 164 | + |
| 165 | + std::unique_ptr<BIO, BIOFree> bio_cert(BIO_new(BIO_s_mem())); |
| 166 | + if (!PEM_write_bio_X509(bio_cert.get(), x509.get())) { |
| 167 | + return OpenSslError("Failed to generate certificate"); |
| 168 | + } |
| 169 | + |
| 170 | + auto& cert_pem = result.cert_pem; |
| 171 | + cert_pem.resize(BIO_pending(bio_cert.get())); |
| 172 | + if (BIO_read(bio_cert.get(), cert_pem.data(), cert_pem.size()) != |
| 173 | + cert_pem.size()) { |
| 174 | + return OpenSslError("Failed to generate certificate"); |
| 175 | + } |
| 176 | + return result; |
| 177 | +} |
| 178 | + |
| 179 | +} // namespace internal_http |
| 180 | +} // namespace tensorstore |
0 commit comments