| /** @file | |
| @par License | |
| Licensed to the Apache Software Foundation (ASF) under one | |
| or more contributor license agreements. See the NOTICE file | |
| distributed with this work for additional information | |
| regarding copyright ownership. The ASF licenses this file | |
| to you 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. | |
| */ | |
| #include "SSLRPKUtils.h" | |
| #include "P_SSLUtils.h" | |
| #include "iocore/net/SSLDiags.h" | |
| #include <openssl/err.h> | |
| #include <openssl/pem.h> | |
| #include <openssl/x509.h> | |
| #include <cstring> | |
| #include <memory> | |
| #include <utility> | |
| namespace SSLRPKUtils | |
| { | |
| bool | |
| loadTrustedKeys(const char *path, TrustedKeySet &out) | |
| { | |
| scoped_BIO bio(BIO_new_file(path, "r")); | |
| if (!bio) { | |
| SSLError("SSLRPKUtils: failed to open trusted raw public key file %s", path); | |
| return false; | |
| } | |
| // Discard anything already on this thread's error queue so the PEM_R_NO_START_LINE check | |
| // below on the first iteration can't be confused by an unrelated error left over from earlier, | |
| // unrelated OpenSSL/BoringSSL calls on this thread. | |
| ERR_clear_error(); | |
| for (;;) { | |
| // Read the PEM envelope first and decode it separately. Asking PEM_read_bio_PUBKEY() to do | |
| // both makes end-of-file indistinguishable from a decode failure: on OpenSSL 3 it runs the | |
| // decoder framework, so the error left on the queue at EOF is the decoder's "unsupported" | |
| // rather than PEM_R_NO_START_LINE, and a "no more keys" stop looks exactly like a malformed | |
| // key. PEM_read_bio() reports EOF unambiguously via PEM_R_NO_START_LINE. | |
| char *name = nullptr; | |
| char *header = nullptr; | |
| unsigned char *data = nullptr; | |
| long len = 0; | |
| if (PEM_read_bio(bio.get(), &name, &header, &data, &len) != 1) { | |
| unsigned long err = ERR_peek_last_error(); | |
| // Reason codes 100-255 are per-sub-library, so the library has to be checked too. | |
| bool const at_eof = ERR_GET_LIB(err) == ERR_LIB_PEM && ERR_GET_REASON(err) == PEM_R_NO_START_LINE; | |
| ERR_clear_error(); | |
| if (at_eof && !out.empty()) { | |
| break; | |
| } | |
| SSLError("SSLRPKUtils: failed to read a PEM block from %s", path); | |
| return false; | |
| } | |
| bool const is_pubkey = name != nullptr && strcmp(name, PEM_STRING_PUBLIC) == 0; | |
| if (!is_pubkey) { | |
| SSLError("SSLRPKUtils: %s contains a '%s' block; only bare public keys are supported", path, | |
| name != nullptr ? name : "(unnamed)"); | |
| } | |
| EVP_PKEY *pkey = nullptr; | |
| if (is_pubkey) { | |
| const unsigned char *p = data; | |
| // Decode the PEM payload so a corrupt key is rejected at config load rather than silently | |
| // pinned as opaque bytes, and so the pin is the canonical re-encoding of the key rather than | |
| // the payload as received, which a peer's cleanly-encoded SPKI could fail to match. | |
| pkey = d2i_PUBKEY(nullptr, &p, len); | |
| if (pkey == nullptr) { | |
| SSLError("SSLRPKUtils: failed to parse a raw public key from %s", path); | |
| } else if (p != data + len) { | |
| // d2i_PUBKEY() stops after the first DER structure, so whatever follows has to be rejected | |
| // here or it would be dropped without a word. | |
| SSLError("SSLRPKUtils: a raw public key in %s is followed by trailing data", path); | |
| EVP_PKEY_free(pkey); | |
| pkey = nullptr; | |
| } | |
| } | |
| OPENSSL_free(name); | |
| OPENSSL_free(header); | |
| if (pkey == nullptr) { | |
| OPENSSL_free(data); | |
| return false; | |
| } | |
| OPENSSL_free(data); | |
| int canonical_len = i2d_PUBKEY(pkey, nullptr); | |
| if (canonical_len <= 0) { | |
| SSLError("SSLRPKUtils: failed to re-encode a raw public key from %s", path); | |
| EVP_PKEY_free(pkey); | |
| return false; | |
| } | |
| TrustedKey canonical(canonical_len); | |
| unsigned char *cp = canonical.data(); | |
| int const written = i2d_PUBKEY(pkey, &cp); | |
| EVP_PKEY_free(pkey); | |
| if (written != canonical_len) { | |
| // A short write would leave the pin zero padded, so it could never match a peer's key -- | |
| // presenting as a mismatch against the peer rather than as a bad pin file. | |
| SSLError("SSLRPKUtils: inconsistent raw public key encoding from %s", path); | |
| return false; | |
| } | |
| out.push_back(std::move(canonical)); | |
| } | |
| return true; | |
| } | |
| bool | |
| pinnedKeyMatches(const unsigned char *peer_spki_der, int peer_spki_len, const TrustedKeySet &trusted) | |
| { | |
| if (peer_spki_der == nullptr || peer_spki_len <= 0) { | |
| return false; | |
| } | |
| for (auto const &key : trusted) { | |
| if (key.size() == static_cast<size_t>(peer_spki_len) && memcmp(key.data(), peer_spki_der, key.size()) == 0) { | |
| return true; | |
| } | |
| } | |
| return false; | |
| } | |
| bool | |
| pinnedKeyMatches(EVP_PKEY *pkey, const TrustedKeySet &trusted) | |
| { | |
| if (pkey == nullptr) { | |
| return false; | |
| } | |
| unsigned char *der = nullptr; | |
| int der_len = i2d_PUBKEY(pkey, &der); | |
| if (der_len <= 0) { | |
| return false; | |
| } | |
| bool matched = pinnedKeyMatches(der, der_len, trusted); | |
| OPENSSL_free(der); | |
| return matched; | |
| } | |
| std::string | |
| digestTrustedKeys(const TrustedKeySet &keys) | |
| { | |
| if (keys.empty()) { | |
| return {}; | |
| } | |
| struct MDCtxDeleter { | |
| void | |
| operator()(EVP_MD_CTX *c) const | |
| { | |
| EVP_MD_CTX_free(c); | |
| } | |
| }; | |
| std::unique_ptr<EVP_MD_CTX, MDCtxDeleter> ctx(EVP_MD_CTX_new()); | |
| if (!ctx || EVP_DigestInit_ex(ctx.get(), EVP_sha256(), nullptr) != 1) { | |
| SSLError("SSLRPKUtils: failed to start a digest over the trusted key set"); | |
| return {}; | |
| } | |
| for (auto const &key : keys) { | |
| // Length-prefix each entry, big endian, so that two sets cannot collide with a single set | |
| // holding their joined bytes. | |
| size_t const size = key.size(); | |
| unsigned char const len[4]{static_cast<unsigned char>((size >> 24) & 0xff), static_cast<unsigned char>((size >> 16) & 0xff), | |
| static_cast<unsigned char>((size >> 8) & 0xff), static_cast<unsigned char>(size & 0xff)}; | |
| if (EVP_DigestUpdate(ctx.get(), len, sizeof(len)) != 1 || EVP_DigestUpdate(ctx.get(), key.data(), size) != 1) { | |
| SSLError("SSLRPKUtils: failed to digest the trusted key set"); | |
| return {}; | |
| } | |
| } | |
| unsigned char hash[EVP_MAX_MD_SIZE]; | |
| unsigned int hash_len = 0; | |
| if (EVP_DigestFinal_ex(ctx.get(), hash, &hash_len) != 1) { | |
| SSLError("SSLRPKUtils: failed to finish the digest over the trusted key set"); | |
| return {}; | |
| } | |
| static char const digits[] = "0123456789abcdef"; | |
| std::string hex; | |
| hex.reserve(static_cast<size_t>(hash_len) * 2); | |
| for (unsigned int i = 0; i < hash_len; ++i) { | |
| hex.push_back(digits[hash[i] >> 4]); | |
| hex.push_back(digits[hash[i] & 0x0f]); | |
| } | |
| return hex; | |
| } | |
| bool | |
| loadTrustedKeyFile(const char *path, std::shared_ptr<const TrustedKeySet> &keys, std::string &digest) | |
| { | |
| auto loaded = std::make_shared<TrustedKeySet>(); | |
| if (!loadTrustedKeys(path, *loaded)) { | |
| return false; | |
| } | |
| std::string loaded_digest = digestTrustedKeys(*loaded); | |
| if (loaded_digest.empty()) { | |
| SSLError("SSLRPKUtils: failed to digest the trusted raw public keys from %s", path); | |
| return false; | |
| } | |
| keys = std::move(loaded); | |
| digest = std::move(loaded_digest); | |
| return true; | |
| } | |
| } // namespace SSLRPKUtils |