| 1 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) |
| 2 |
// Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. |
| 3 |
// Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. |
| 4 |
// |
| 5 |
// ECC cipher suite support in OpenSSL originally developed by |
| 6 |
// SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. |
| 7 |
// |
| 8 |
// SPDX-License-Identifier: Apache-2.0 |
| 9 |
|
| 10 |
#include <openssl/ssl.h> |
| 11 |
|
| 12 |
#include <assert.h> |
| 13 |
#include <limits.h> |
| 14 |
#include <string.h> |
| 15 |
|
| 16 |
#include <utility> |
| 17 |
|
| 18 |
#include <openssl/bn.h> |
| 19 |
#include <openssl/bytestring.h> |
| 20 |
#include <openssl/ec_key.h> |
| 21 |
#include <openssl/err.h> |
| 22 |
#include <openssl/mem.h> |
| 23 |
#include <openssl/sha.h> |
| 24 |
#include <openssl/x509.h> |
| 25 |
|
| 26 |
#include "../crypto/internal.h" |
| 27 |
#include "internal.h" |
| 28 |
|
| 29 |
|
| 30 |
BSSL_NAMESPACE_BEGIN |
| 31 |
|
| 32 |
CERT::CERT(const SSL_X509_METHOD *x509_method_arg) |
| 33 |
: x509_method(x509_method_arg) { |
| 34 |
this->cert_private_key_idx = SSL_PKEY_RSA; |
| 35 |
if (!this->cert_private_keys.Init(SSL_PKEY_SIZE)) { |
| 36 |
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 37 |
return; |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
CERT::~CERT() { x509_method->cert_free(this); } |
| 42 |
|
| 43 |
bool CERT::SetKeyMethod(const SSL_PRIVATE_KEY_METHOD *method, int slot_idx) { |
| 44 |
key_method = method; |
| 45 |
if (slot_idx >= 0 && |
| 46 |
slot_idx < static_cast<int>(cert_private_keys.size())) { |
| 47 |
cert_private_keys[slot_idx].privatekey.reset(); |
| 48 |
return true; |
| 49 |
} |
| 50 |
// If |method| is non-NULL and |slot_idx| is out of range, the mutual |
| 51 |
// exclusivity invariant with per-slot |privatekey| cannot be enforced. |
| 52 |
return method == nullptr; |
| 53 |
} |
| 54 |
|
| 55 |
bool CERT::SetSlotPrivateKey(int slot_idx, EVP_PKEY *pkey) { |
| 56 |
if (slot_idx < 0 || |
| 57 |
slot_idx >= static_cast<int>(cert_private_keys.size())) { |
| 58 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 59 |
return false; |
| 60 |
} |
| 61 |
cert_private_keys[slot_idx].privatekey = UpRef(pkey); |
| 62 |
key_method = nullptr; |
| 63 |
return true; |
| 64 |
} |
| 65 |
|
| 66 |
CRYPTO_BUFFER *buffer_up_ref(const CRYPTO_BUFFER *buffer) { |
| 67 |
CRYPTO_BUFFER_up_ref(const_cast<CRYPTO_BUFFER *>(buffer)); |
| 68 |
return const_cast<CRYPTO_BUFFER *>(buffer); |
| 69 |
} |
| 70 |
|
| 71 |
UniquePtr<CERT> ssl_cert_dup(CERT *cert) { |
| 72 |
if (cert == nullptr) { |
| 73 |
return nullptr; |
| 74 |
} |
| 75 |
|
| 76 |
UniquePtr<CERT> ret = MakeUnique<CERT>(cert->x509_method); |
| 77 |
if (!ret) { |
| 78 |
return nullptr; |
| 79 |
} |
| 80 |
|
| 81 |
ret->cert_private_key_idx = cert->cert_private_key_idx; |
| 82 |
if (!ssl_cert_check_cert_private_keys_usage(cert) || |
| 83 |
!ssl_cert_check_cert_private_keys_usage(ret.get())) { |
| 84 |
return nullptr; |
| 85 |
} |
| 86 |
for (int i = 0; i < SSL_PKEY_SIZE; i++) { |
| 87 |
CERT_PKEY &cert_pkey = cert->cert_private_keys[i]; |
| 88 |
CERT_PKEY &ret_pkey = ret->cert_private_keys[i]; |
| 89 |
|
| 90 |
if (cert_pkey.chain) { |
| 91 |
ret_pkey.chain.reset(sk_CRYPTO_BUFFER_deep_copy( |
| 92 |
cert_pkey.chain.get(), buffer_up_ref, CRYPTO_BUFFER_free)); |
| 93 |
if (!ret_pkey.chain) { |
| 94 |
return nullptr; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
if (cert_pkey.x509_leaf != nullptr) { |
| 99 |
X509_up_ref(cert_pkey.x509_leaf); |
| 100 |
ret_pkey.x509_leaf = cert_pkey.x509_leaf; |
| 101 |
} |
| 102 |
|
| 103 |
ret_pkey.privatekey = UpRef(cert_pkey.privatekey); |
| 104 |
} |
| 105 |
|
| 106 |
ret->key_method = cert->key_method; |
| 107 |
|
| 108 |
if (!ret->sigalgs.CopyFrom(cert->sigalgs)) { |
| 109 |
return nullptr; |
| 110 |
} |
| 111 |
|
| 112 |
ret->cert_cb = cert->cert_cb; |
| 113 |
ret->cert_cb_arg = cert->cert_cb_arg; |
| 114 |
|
| 115 |
ret->x509_method->cert_dup(ret.get(), cert); |
| 116 |
|
| 117 |
ret->signed_cert_timestamp_list = UpRef(cert->signed_cert_timestamp_list); |
| 118 |
ret->ocsp_response = UpRef(cert->ocsp_response); |
| 119 |
|
| 120 |
ret->sid_ctx_length = cert->sid_ctx_length; |
| 121 |
OPENSSL_memcpy(ret->sid_ctx, cert->sid_ctx, sizeof(ret->sid_ctx)); |
| 122 |
|
| 123 |
if (cert->dc) { |
| 124 |
ret->dc = cert->dc->Dup(); |
| 125 |
if (!ret->dc) { |
| 126 |
return nullptr; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
ret->dc_privatekey = UpRef(cert->dc_privatekey); |
| 131 |
ret->dc_key_method = cert->dc_key_method; |
| 132 |
|
| 133 |
return ret; |
| 134 |
} |
| 135 |
|
| 136 |
static void ssl_cert_set_cert_cb(CERT *cert, int (*cb)(SSL *ssl, void *arg), |
| 137 |
void *arg) { |
| 138 |
cert->cert_cb = cb; |
| 139 |
cert->cert_cb_arg = arg; |
| 140 |
} |
| 141 |
|
| 142 |
enum leaf_cert_and_privkey_result_t { |
| 143 |
leaf_cert_and_privkey_error, |
| 144 |
leaf_cert_and_privkey_ok, |
| 145 |
leaf_cert_and_privkey_mismatch, |
| 146 |
}; |
| 147 |
|
| 148 |
// do_leaf_cert_and_privkey_checks does the necessary checks against |pubkey| |
| 149 |
// and |privkey|. It's expected that the |pubkey| has been parsed from |
| 150 |
// |cert_cbs|. |
| 151 |
static enum leaf_cert_and_privkey_result_t do_leaf_cert_and_privkey_checks( |
| 152 |
const CBS *cert_cbs, EVP_PKEY *pubkey, EVP_PKEY *privkey) { |
| 153 |
if (!ssl_is_key_type_supported(EVP_PKEY_id(pubkey))) { |
| 154 |
OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE); |
| 155 |
return leaf_cert_and_privkey_error; |
| 156 |
} |
| 157 |
|
| 158 |
// An ECC certificate may be usable for ECDH or ECDSA. We only support ECDSA |
| 159 |
// certificates, so sanity-check the key usage extension. |
| 160 |
if (EVP_PKEY_id(pubkey) == EVP_PKEY_EC && |
| 161 |
!ssl_cert_check_key_usage(cert_cbs, key_usage_digital_signature)) { |
| 162 |
OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE); |
| 163 |
return leaf_cert_and_privkey_error; |
| 164 |
} |
| 165 |
|
| 166 |
if (privkey != nullptr && |
| 167 |
// Sanity-check that the private key and the certificate match. |
| 168 |
!ssl_compare_public_and_private_key(pubkey, privkey)) { |
| 169 |
ERR_clear_error(); |
| 170 |
return leaf_cert_and_privkey_mismatch; |
| 171 |
} |
| 172 |
|
| 173 |
return leaf_cert_and_privkey_ok; |
| 174 |
} |
| 175 |
|
| 176 |
// check_leaf_cert_and_privkey checks whether the certificate in |leaf_buffer| |
| 177 |
// and the private key in |privkey| are suitable and coherent. It returns |
| 178 |
// |leaf_cert_and_privkey_error| and pushes to the error queue if a problem is |
| 179 |
// found. If the certificate and private key are valid, but incoherent, it |
| 180 |
// returns |leaf_cert_and_privkey_mismatch|. Otherwise it returns |
| 181 |
// |leaf_cert_and_privkey_ok|. |
| 182 |
static enum leaf_cert_and_privkey_result_t check_leaf_cert_and_privkey( |
| 183 |
CRYPTO_BUFFER *leaf_buffer, EVP_PKEY *privkey, int *out_pubslot_idx) { |
| 184 |
CBS cert_cbs; |
| 185 |
CRYPTO_BUFFER_init_CBS(leaf_buffer, &cert_cbs); |
| 186 |
UniquePtr<EVP_PKEY> pubkey = ssl_cert_parse_pubkey(&cert_cbs); |
| 187 |
if (pubkey == nullptr) { |
| 188 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 189 |
return leaf_cert_and_privkey_error; |
| 190 |
} |
| 191 |
enum leaf_cert_and_privkey_result_t result = do_leaf_cert_and_privkey_checks( |
| 192 |
&cert_cbs, pubkey.get(), privkey); |
| 193 |
if(out_pubslot_idx != nullptr) { |
| 194 |
if(result == leaf_cert_and_privkey_ok) { |
| 195 |
*out_pubslot_idx = ssl_get_certificate_slot_index(pubkey.get()); |
| 196 |
} else { |
| 197 |
*out_pubslot_idx = -1; |
| 198 |
} |
| 199 |
} |
| 200 |
return result; |
| 201 |
} |
| 202 |
|
| 203 |
static int cert_set_chain_and_key( |
| 204 |
CERT *cert, UniquePtr<STACK_OF(CRYPTO_BUFFER)> &certs, size_t num_certs, |
| 205 |
EVP_PKEY *privkey, const SSL_PRIVATE_KEY_METHOD *privkey_method) { |
| 206 |
if (num_certs == 0 || (privkey == NULL && privkey_method == NULL)) { |
| 207 |
OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER); |
| 208 |
return 0; |
| 209 |
} |
| 210 |
|
| 211 |
if (privkey != NULL && privkey_method != NULL) { |
| 212 |
OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_HAVE_BOTH_PRIVKEY_AND_METHOD); |
| 213 |
return 0; |
| 214 |
} |
| 215 |
|
| 216 |
CRYPTO_BUFFER *leaf_buf = sk_CRYPTO_BUFFER_value(certs.get(), 0); |
| 217 |
if (leaf_buf == nullptr) { |
| 218 |
return 0; |
| 219 |
} |
| 220 |
|
| 221 |
int slot_idx = -1; |
| 222 |
|
| 223 |
switch (check_leaf_cert_and_privkey(leaf_buf, privkey, &slot_idx)) { |
| 224 |
case leaf_cert_and_privkey_error: |
| 225 |
return 0; |
| 226 |
case leaf_cert_and_privkey_mismatch: |
| 227 |
OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_AND_PRIVATE_KEY_MISMATCH); |
| 228 |
return 0; |
| 229 |
case leaf_cert_and_privkey_ok: |
| 230 |
break; |
| 231 |
} |
| 232 |
assert(slot_idx >= 0); |
| 233 |
|
| 234 |
if (!ssl_cert_check_cert_private_keys_usage(cert)) { |
| 235 |
return 0; |
| 236 |
} |
| 237 |
|
| 238 |
// Certificate slot validity already checked and set by |
| 239 |
// |check_leaf_cert_and_privkey|. |
| 240 |
CERT_PKEY *cert_pkey = &cert->cert_private_keys[slot_idx]; |
| 241 |
|
| 242 |
if(privkey != nullptr) { |
| 243 |
if (!cert->SetSlotPrivateKey(slot_idx, privkey)) { |
| 244 |
return 0; |
| 245 |
} |
| 246 |
} else { |
| 247 |
if (!cert->SetKeyMethod(privkey_method, slot_idx)) { |
| 248 |
return 0; |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
if (cert_pkey->chain) { |
| 253 |
cert_pkey->chain.reset(); |
| 254 |
} |
| 255 |
cert_pkey->chain = std::move(certs); |
| 256 |
|
| 257 |
// Invalidate the parsed X509 leaf and chain caches since the backing |
| 258 |
// CRYPTO_BUFFER chain was just replaced. Dispatch through |x509_method| to |
| 259 |
// respect the abstraction boundary and to correctly handle the noop case |
| 260 |
// (e.g. TLS_with_buffers_method) where these are no-ops. |
| 261 |
cert->x509_method->cert_flush_cached_chain(cert); |
| 262 |
cert->x509_method->cert_flush_leaf(cert); |
| 263 |
|
| 264 |
cert->cert_private_key_idx = slot_idx; |
| 265 |
|
| 266 |
return 1; |
| 267 |
} |
| 268 |
|
| 269 |
bool ssl_set_cert(CERT *cert, UniquePtr<CRYPTO_BUFFER> buffer) { |
| 270 |
if (!ssl_cert_check_cert_private_keys_usage(cert)) { |
| 271 |
return false; |
| 272 |
} |
| 273 |
|
| 274 |
// Parse the right certificate slot index from |buffer|. |
| 275 |
CBS cert_cbs; |
| 276 |
CRYPTO_BUFFER_init_CBS(buffer.get(), &cert_cbs); |
| 277 |
UniquePtr<EVP_PKEY> pubkey = ssl_cert_parse_pubkey(&cert_cbs); |
| 278 |
if (!pubkey) { |
| 279 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 280 |
return false; |
| 281 |
} |
| 282 |
int slot_index = ssl_get_certificate_slot_index(pubkey.get()); |
| 283 |
if (slot_index < 0) { |
| 284 |
OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE); |
| 285 |
return false; |
| 286 |
} |
| 287 |
|
| 288 |
CERT_PKEY &cert_pkey = cert->cert_private_keys[slot_index]; |
| 289 |
switch (do_leaf_cert_and_privkey_checks(&cert_cbs, pubkey.get(), |
| 290 |
cert_pkey.privatekey.get())) { |
| 291 |
case leaf_cert_and_privkey_error: |
| 292 |
return false; |
| 293 |
case leaf_cert_and_privkey_mismatch: |
| 294 |
// don't fail for a cert/key mismatch, just free current private key |
| 295 |
// (when switching to a different cert & key, first this function should |
| 296 |
// be used, then |ssl_set_pkey|. |
| 297 |
cert_pkey.privatekey.reset(); |
| 298 |
break; |
| 299 |
case leaf_cert_and_privkey_ok: |
| 300 |
break; |
| 301 |
} |
| 302 |
|
| 303 |
if (cert_pkey.chain != nullptr) { |
| 304 |
CRYPTO_BUFFER_free(sk_CRYPTO_BUFFER_value(cert_pkey.chain.get(), 0)); |
| 305 |
sk_CRYPTO_BUFFER_set(cert_pkey.chain.get(), 0, buffer.release()); |
| 306 |
|
| 307 |
// The backing CRYPTO_BUFFER leaf was just replaced; invalidate the cached |
| 308 |
// X509 leaf to prevent stale data from being returned to callers. |
| 309 |
cert->x509_method->cert_flush_leaf(cert); |
| 310 |
|
| 311 |
// Update certificate slot index if all checks have passed. |
| 312 |
cert->cert_private_key_idx = slot_index; |
| 313 |
return true; |
| 314 |
} |
| 315 |
|
| 316 |
cert_pkey.chain.reset(sk_CRYPTO_BUFFER_new_null()); |
| 317 |
if (cert_pkey.chain == nullptr) { |
| 318 |
return false; |
| 319 |
} |
| 320 |
|
| 321 |
if (!PushToStack(cert_pkey.chain.get(), std::move(buffer))) { |
| 322 |
cert_pkey.chain.reset(); |
| 323 |
return false; |
| 324 |
} |
| 325 |
|
| 326 |
// Update certificate slot index if all checks have passed. |
| 327 |
cert->cert_private_key_idx = slot_index; |
| 328 |
return true; |
| 329 |
} |
| 330 |
|
| 331 |
bool ssl_has_certificate(const SSL_HANDSHAKE *hs) { |
| 332 |
if (!ssl_cert_check_cert_private_keys_usage(hs->config->cert.get())) { |
| 333 |
return false; |
| 334 |
} |
| 335 |
|
| 336 |
CERT_PKEY &cert_pkey = |
| 337 |
hs->config->cert |
| 338 |
->cert_private_keys[hs->config->cert->cert_private_key_idx]; |
| 339 |
return cert_pkey.chain != nullptr && |
| 340 |
sk_CRYPTO_BUFFER_value(cert_pkey.chain.get(), 0) != nullptr && |
| 341 |
ssl_has_private_key(hs); |
| 342 |
} |
| 343 |
|
| 344 |
bool ssl_parse_cert_chain(uint8_t *out_alert, |
| 345 |
UniquePtr<STACK_OF(CRYPTO_BUFFER)> *out_chain, |
| 346 |
UniquePtr<EVP_PKEY> *out_pubkey, |
| 347 |
uint8_t *out_leaf_sha256, CBS *cbs, |
| 348 |
CRYPTO_BUFFER_POOL *pool) { |
| 349 |
out_chain->reset(); |
| 350 |
out_pubkey->reset(); |
| 351 |
|
| 352 |
CBS certificate_list; |
| 353 |
if (!CBS_get_u24_length_prefixed(cbs, &certificate_list)) { |
| 354 |
*out_alert = SSL_AD_DECODE_ERROR; |
| 355 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 356 |
return false; |
| 357 |
} |
| 358 |
|
| 359 |
if (CBS_len(&certificate_list) == 0) { |
| 360 |
return true; |
| 361 |
} |
| 362 |
|
| 363 |
UniquePtr<STACK_OF(CRYPTO_BUFFER)> chain(sk_CRYPTO_BUFFER_new_null()); |
| 364 |
if (!chain) { |
| 365 |
*out_alert = SSL_AD_INTERNAL_ERROR; |
| 366 |
return false; |
| 367 |
} |
| 368 |
|
| 369 |
UniquePtr<EVP_PKEY> pubkey; |
| 370 |
while (CBS_len(&certificate_list) > 0) { |
| 371 |
CBS certificate; |
| 372 |
if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate) || |
| 373 |
CBS_len(&certificate) == 0) { |
| 374 |
*out_alert = SSL_AD_DECODE_ERROR; |
| 375 |
OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH); |
| 376 |
return false; |
| 377 |
} |
| 378 |
|
| 379 |
if (sk_CRYPTO_BUFFER_num(chain.get()) == 0) { |
| 380 |
pubkey = ssl_cert_parse_pubkey(&certificate); |
| 381 |
if (!pubkey) { |
| 382 |
*out_alert = SSL_AD_DECODE_ERROR; |
| 383 |
return false; |
| 384 |
} |
| 385 |
|
| 386 |
// Retain the hash of the leaf certificate if requested. |
| 387 |
if (out_leaf_sha256 != NULL) { |
| 388 |
SHA256(CBS_data(&certificate), CBS_len(&certificate), out_leaf_sha256); |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
UniquePtr<CRYPTO_BUFFER> buf( |
| 393 |
CRYPTO_BUFFER_new_from_CBS(&certificate, pool)); |
| 394 |
if (!buf || !PushToStack(chain.get(), std::move(buf))) { |
| 395 |
*out_alert = SSL_AD_INTERNAL_ERROR; |
| 396 |
return false; |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
*out_chain = std::move(chain); |
| 401 |
*out_pubkey = std::move(pubkey); |
| 402 |
return true; |
| 403 |
} |
| 404 |
|
| 405 |
bool ssl_add_cert_chain(SSL_HANDSHAKE *hs, CBB *cbb) { |
| 406 |
if (!ssl_has_certificate(hs)) { |
| 407 |
return CBB_add_u24(cbb, 0); |
| 408 |
} |
| 409 |
|
| 410 |
CBB certs; |
| 411 |
if (!CBB_add_u24_length_prefixed(cbb, &certs)) { |
| 412 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 413 |
return false; |
| 414 |
} |
| 415 |
|
| 416 |
// |cert_private_keys| already checked above in |ssl_has_certificate|. |
| 417 |
int idx = hs->config->cert->cert_private_key_idx; |
| 418 |
STACK_OF(CRYPTO_BUFFER) *chain = |
| 419 |
hs->config->cert->cert_private_keys[idx].chain.get(); |
| 420 |
for (size_t i = 0; i < sk_CRYPTO_BUFFER_num(chain); i++) { |
| 421 |
CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(chain, i); |
| 422 |
CBB child; |
| 423 |
if (!CBB_add_u24_length_prefixed(&certs, &child) || |
| 424 |
!CBB_add_bytes(&child, CRYPTO_BUFFER_data(buffer), |
| 425 |
CRYPTO_BUFFER_len(buffer)) || |
| 426 |
!CBB_flush(&certs)) { |
| 427 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 428 |
return false; |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
return CBB_flush(cbb); |
| 433 |
} |
| 434 |
|
| 435 |
// ssl_cert_skip_to_spki parses a DER-encoded, X.509 certificate from |in| and |
| 436 |
// positions |*out_tbs_cert| to cover the TBSCertificate, starting at the |
| 437 |
// subjectPublicKeyInfo. |
| 438 |
static bool ssl_cert_skip_to_spki(const CBS *in, CBS *out_tbs_cert) { |
| 439 |
/* From RFC 5280, section 4.1 |
| 440 |
* Certificate ::= SEQUENCE { |
| 441 |
* tbsCertificate TBSCertificate, |
| 442 |
* signatureAlgorithm AlgorithmIdentifier, |
| 443 |
* signatureValue BIT STRING } |
| 444 |
|
| 445 |
* TBSCertificate ::= SEQUENCE { |
| 446 |
* version [0] EXPLICIT Version DEFAULT v1, |
| 447 |
* serialNumber CertificateSerialNumber, |
| 448 |
* signature AlgorithmIdentifier, |
| 449 |
* issuer Name, |
| 450 |
* validity Validity, |
| 451 |
* subject Name, |
| 452 |
* subjectPublicKeyInfo SubjectPublicKeyInfo, |
| 453 |
* ... } */ |
| 454 |
CBS buf = *in; |
| 455 |
|
| 456 |
CBS toplevel; |
| 457 |
if (!CBS_get_asn1(&buf, &toplevel, CBS_ASN1_SEQUENCE) || CBS_len(&buf) != 0 || |
| 458 |
!CBS_get_asn1(&toplevel, out_tbs_cert, CBS_ASN1_SEQUENCE) || |
| 459 |
// version |
| 460 |
!CBS_get_optional_asn1( |
| 461 |
out_tbs_cert, NULL, NULL, |
| 462 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 0) || |
| 463 |
// serialNumber |
| 464 |
!CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_INTEGER) || |
| 465 |
// signature algorithm |
| 466 |
!CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) || |
| 467 |
// issuer |
| 468 |
!CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) || |
| 469 |
// validity |
| 470 |
!CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) || |
| 471 |
// subject |
| 472 |
!CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE)) { |
| 473 |
return false; |
| 474 |
} |
| 475 |
|
| 476 |
return true; |
| 477 |
} |
| 478 |
|
| 479 |
UniquePtr<EVP_PKEY> ssl_cert_parse_pubkey(const CBS *in) { |
| 480 |
CBS buf = *in, tbs_cert; |
| 481 |
if (!ssl_cert_skip_to_spki(&buf, &tbs_cert)) { |
| 482 |
OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT); |
| 483 |
return nullptr; |
| 484 |
} |
| 485 |
|
| 486 |
return UniquePtr<EVP_PKEY>(EVP_parse_public_key(&tbs_cert)); |
| 487 |
} |
| 488 |
|
| 489 |
bool ssl_compare_public_and_private_key(const EVP_PKEY *pubkey, |
| 490 |
const EVP_PKEY *privkey) { |
| 491 |
if (EVP_PKEY_is_opaque(privkey)) { |
| 492 |
// We cannot check an opaque private key and have to trust that it |
| 493 |
// matches. |
| 494 |
return true; |
| 495 |
} |
| 496 |
|
| 497 |
switch (EVP_PKEY_cmp(pubkey, privkey)) { |
| 498 |
case 1: |
| 499 |
return true; |
| 500 |
case 0: |
| 501 |
OPENSSL_PUT_ERROR(X509, X509_R_KEY_VALUES_MISMATCH); |
| 502 |
return false; |
| 503 |
case -1: |
| 504 |
OPENSSL_PUT_ERROR(X509, X509_R_KEY_TYPE_MISMATCH); |
| 505 |
return false; |
| 506 |
case -2: |
| 507 |
OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_KEY_TYPE); |
| 508 |
return false; |
| 509 |
} |
| 510 |
|
| 511 |
assert(0); |
| 512 |
return false; |
| 513 |
} |
| 514 |
|
| 515 |
bool ssl_cert_check_private_key(const CERT *cert, const EVP_PKEY *privkey) { |
| 516 |
if (privkey == nullptr) { |
| 517 |
OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED); |
| 518 |
return false; |
| 519 |
} |
| 520 |
|
| 521 |
// |cert_private_keys| already checked before usages of |
| 522 |
// |ssl_cert_check_private_key|. |
| 523 |
STACK_OF(CRYPTO_BUFFER) *chain = |
| 524 |
cert->cert_private_keys[cert->cert_private_key_idx].chain.get(); |
| 525 |
|
| 526 |
if (chain == nullptr || sk_CRYPTO_BUFFER_value(chain, 0) == nullptr) { |
| 527 |
OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED); |
| 528 |
return false; |
| 529 |
} |
| 530 |
|
| 531 |
CBS cert_cbs; |
| 532 |
CRYPTO_BUFFER_init_CBS(sk_CRYPTO_BUFFER_value(chain, 0), &cert_cbs); |
| 533 |
UniquePtr<EVP_PKEY> pubkey = ssl_cert_parse_pubkey(&cert_cbs); |
| 534 |
if (!pubkey) { |
| 535 |
OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_KEY_TYPE); |
| 536 |
return false; |
| 537 |
} |
| 538 |
|
| 539 |
return ssl_compare_public_and_private_key(pubkey.get(), privkey); |
| 540 |
} |
| 541 |
|
| 542 |
bool ssl_cert_check_key_usage(const CBS *in, enum ssl_key_usage_t bit) { |
| 543 |
CBS buf = *in; |
| 544 |
|
| 545 |
CBS tbs_cert, outer_extensions; |
| 546 |
int has_extensions; |
| 547 |
if (!ssl_cert_skip_to_spki(&buf, &tbs_cert) || |
| 548 |
// subjectPublicKeyInfo |
| 549 |
!CBS_get_asn1(&tbs_cert, NULL, CBS_ASN1_SEQUENCE) || |
| 550 |
// issuerUniqueID |
| 551 |
!CBS_get_optional_asn1(&tbs_cert, NULL, NULL, |
| 552 |
CBS_ASN1_CONTEXT_SPECIFIC | 1) || |
| 553 |
// subjectUniqueID |
| 554 |
!CBS_get_optional_asn1(&tbs_cert, NULL, NULL, |
| 555 |
CBS_ASN1_CONTEXT_SPECIFIC | 2) || |
| 556 |
!CBS_get_optional_asn1( |
| 557 |
&tbs_cert, &outer_extensions, &has_extensions, |
| 558 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 3)) { |
| 559 |
OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT); |
| 560 |
return false; |
| 561 |
} |
| 562 |
|
| 563 |
if (!has_extensions) { |
| 564 |
return true; |
| 565 |
} |
| 566 |
|
| 567 |
CBS extensions; |
| 568 |
if (!CBS_get_asn1(&outer_extensions, &extensions, CBS_ASN1_SEQUENCE)) { |
| 569 |
OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT); |
| 570 |
return false; |
| 571 |
} |
| 572 |
|
| 573 |
while (CBS_len(&extensions) > 0) { |
| 574 |
CBS extension, oid, contents; |
| 575 |
if (!CBS_get_asn1(&extensions, &extension, CBS_ASN1_SEQUENCE) || |
| 576 |
!CBS_get_asn1(&extension, &oid, CBS_ASN1_OBJECT) || |
| 577 |
(CBS_peek_asn1_tag(&extension, CBS_ASN1_BOOLEAN) && |
| 578 |
!CBS_get_asn1(&extension, NULL, CBS_ASN1_BOOLEAN)) || |
| 579 |
!CBS_get_asn1(&extension, &contents, CBS_ASN1_OCTETSTRING) || |
| 580 |
CBS_len(&extension) != 0) { |
| 581 |
OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT); |
| 582 |
return false; |
| 583 |
} |
| 584 |
|
| 585 |
static const uint8_t kKeyUsageOID[3] = {0x55, 0x1d, 0x0f}; |
| 586 |
if (CBS_len(&oid) != sizeof(kKeyUsageOID) || |
| 587 |
OPENSSL_memcmp(CBS_data(&oid), kKeyUsageOID, sizeof(kKeyUsageOID)) != |
| 588 |
0) { |
| 589 |
continue; |
| 590 |
} |
| 591 |
|
| 592 |
CBS bit_string; |
| 593 |
if (!CBS_get_asn1(&contents, &bit_string, CBS_ASN1_BITSTRING) || |
| 594 |
CBS_len(&contents) != 0) { |
| 595 |
OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT); |
| 596 |
return false; |
| 597 |
} |
| 598 |
|
| 599 |
// This is the KeyUsage extension. See |
| 600 |
// https://tools.ietf.org/html/rfc5280#section-4.2.1.3 |
| 601 |
if (!CBS_is_valid_asn1_bitstring(&bit_string)) { |
| 602 |
OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT); |
| 603 |
return false; |
| 604 |
} |
| 605 |
|
| 606 |
if (!CBS_asn1_bitstring_has_bit(&bit_string, bit)) { |
| 607 |
OPENSSL_PUT_ERROR(SSL, SSL_R_KEY_USAGE_BIT_INCORRECT); |
| 608 |
return false; |
| 609 |
} |
| 610 |
|
| 611 |
return true; |
| 612 |
} |
| 613 |
|
| 614 |
// No KeyUsage extension found. |
| 615 |
return true; |
| 616 |
} |
| 617 |
|
| 618 |
UniquePtr<STACK_OF(CRYPTO_BUFFER)> ssl_parse_client_CA_list(SSL *ssl, |
| 619 |
uint8_t *out_alert, |
| 620 |
CBS *cbs) { |
| 621 |
CRYPTO_BUFFER_POOL *const pool = ssl->ctx->pool; |
| 622 |
|
| 623 |
UniquePtr<STACK_OF(CRYPTO_BUFFER)> ret(sk_CRYPTO_BUFFER_new_null()); |
| 624 |
if (!ret) { |
| 625 |
*out_alert = SSL_AD_INTERNAL_ERROR; |
| 626 |
return nullptr; |
| 627 |
} |
| 628 |
|
| 629 |
CBS child; |
| 630 |
if (!CBS_get_u16_length_prefixed(cbs, &child)) { |
| 631 |
*out_alert = SSL_AD_DECODE_ERROR; |
| 632 |
OPENSSL_PUT_ERROR(SSL, SSL_R_LENGTH_MISMATCH); |
| 633 |
return nullptr; |
| 634 |
} |
| 635 |
|
| 636 |
while (CBS_len(&child) > 0) { |
| 637 |
CBS distinguished_name; |
| 638 |
if (!CBS_get_u16_length_prefixed(&child, &distinguished_name)) { |
| 639 |
*out_alert = SSL_AD_DECODE_ERROR; |
| 640 |
OPENSSL_PUT_ERROR(SSL, SSL_R_CA_DN_TOO_LONG); |
| 641 |
return nullptr; |
| 642 |
} |
| 643 |
|
| 644 |
UniquePtr<CRYPTO_BUFFER> buffer( |
| 645 |
CRYPTO_BUFFER_new_from_CBS(&distinguished_name, pool)); |
| 646 |
if (!buffer || !PushToStack(ret.get(), std::move(buffer))) { |
| 647 |
*out_alert = SSL_AD_INTERNAL_ERROR; |
| 648 |
return nullptr; |
| 649 |
} |
| 650 |
} |
| 651 |
|
| 652 |
if (!ssl->ctx->x509_method->check_client_CA_list(ret.get())) { |
| 653 |
*out_alert = SSL_AD_DECODE_ERROR; |
| 654 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 655 |
return nullptr; |
| 656 |
} |
| 657 |
|
| 658 |
return ret; |
| 659 |
} |
| 660 |
|
| 661 |
bool ssl_has_client_CAs(const SSL_CONFIG *cfg) { |
| 662 |
const STACK_OF(CRYPTO_BUFFER) *names = cfg->client_CA.get(); |
| 663 |
if (names == nullptr) { |
| 664 |
names = cfg->ssl->ctx->client_CA.get(); |
| 665 |
} |
| 666 |
if (names == nullptr) { |
| 667 |
return false; |
| 668 |
} |
| 669 |
return sk_CRYPTO_BUFFER_num(names) > 0; |
| 670 |
} |
| 671 |
|
| 672 |
bool ssl_add_client_CA_list(SSL_HANDSHAKE *hs, CBB *cbb) { |
| 673 |
CBB child, name_cbb; |
| 674 |
if (!CBB_add_u16_length_prefixed(cbb, &child)) { |
| 675 |
return false; |
| 676 |
} |
| 677 |
|
| 678 |
const STACK_OF(CRYPTO_BUFFER) *names = hs->config->client_CA.get(); |
| 679 |
if (names == NULL) { |
| 680 |
names = hs->ssl->ctx->client_CA.get(); |
| 681 |
} |
| 682 |
if (names == NULL) { |
| 683 |
return CBB_flush(cbb); |
| 684 |
} |
| 685 |
|
| 686 |
for (const CRYPTO_BUFFER *name : names) { |
| 687 |
if (!CBB_add_u16_length_prefixed(&child, &name_cbb) || |
| 688 |
!CBB_add_bytes(&name_cbb, CRYPTO_BUFFER_data(name), |
| 689 |
CRYPTO_BUFFER_len(name))) { |
| 690 |
return false; |
| 691 |
} |
| 692 |
} |
| 693 |
|
| 694 |
return CBB_flush(cbb); |
| 695 |
} |
| 696 |
|
| 697 |
bool ssl_check_leaf_certificate(SSL_HANDSHAKE *hs, EVP_PKEY *pkey, |
| 698 |
const CRYPTO_BUFFER *leaf) { |
| 699 |
assert(ssl_protocol_version(hs->ssl) < TLS1_3_VERSION); |
| 700 |
|
| 701 |
// Check the certificate's type matches the cipher. |
| 702 |
if (!(hs->new_cipher->algorithm_auth & ssl_cipher_auth_mask_for_key(pkey))) { |
| 703 |
OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CERTIFICATE_TYPE); |
| 704 |
return false; |
| 705 |
} |
| 706 |
|
| 707 |
if (EVP_PKEY_id(pkey) == EVP_PKEY_EC) { |
| 708 |
// Check the key's group and point format are acceptable. |
| 709 |
EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey); |
| 710 |
uint16_t group_id; |
| 711 |
if (!ssl_nid_to_group_id( |
| 712 |
&group_id, EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key))) || |
| 713 |
!tls1_check_group_id(hs, group_id) || |
| 714 |
EC_KEY_get_conv_form(ec_key) != POINT_CONVERSION_UNCOMPRESSED) { |
| 715 |
OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECC_CERT); |
| 716 |
return false; |
| 717 |
} |
| 718 |
} |
| 719 |
|
| 720 |
return true; |
| 721 |
} |
| 722 |
|
| 723 |
bool ssl_on_certificate_selected(SSL_HANDSHAKE *hs) { |
| 724 |
SSL *const ssl = hs->ssl; |
| 725 |
if (!ssl_has_certificate(hs)) { |
| 726 |
// Nothing to do. |
| 727 |
return true; |
| 728 |
} |
| 729 |
|
| 730 |
if (!ssl->ctx->x509_method->ssl_auto_chain_if_needed(hs) || |
| 731 |
!ssl_handshake_load_local_pubkey(hs)) { |
| 732 |
return false; |
| 733 |
} |
| 734 |
|
| 735 |
// Sanity check that cached certificate public key type matches the chosen |
| 736 |
// certificate slot index type. |
| 737 |
assert(ssl_signing_with_dc(hs) || |
| 738 |
(ssl_get_certificate_slot_index(hs->local_pubkey.get()) == |
| 739 |
hs->config->cert->cert_private_key_idx)); |
| 740 |
|
| 741 |
return true; |
| 742 |
} |
| 743 |
|
| 744 |
bool ssl_handshake_load_local_pubkey(SSL_HANDSHAKE *hs) { |
| 745 |
if (!ssl_cert_check_cert_private_keys_usage(hs->config->cert.get())) { |
| 746 |
return false; |
| 747 |
} |
| 748 |
|
| 749 |
STACK_OF(CRYPTO_BUFFER) *chain = |
| 750 |
hs->config->cert |
| 751 |
->cert_private_keys[hs->config->cert->cert_private_key_idx] |
| 752 |
.chain.get(); |
| 753 |
|
| 754 |
if (ssl_signing_with_dc(hs)) { |
| 755 |
hs->local_pubkey = UpRef(hs->config->cert->dc->pkey); |
| 756 |
} else { |
| 757 |
hs->local_pubkey = ssl_cert_parse_leaf_pubkey(chain); |
| 758 |
} |
| 759 |
return hs->local_pubkey != nullptr; |
| 760 |
} |
| 761 |
|
| 762 |
|
| 763 |
// Delegated credentials. |
| 764 |
|
| 765 |
DC::DC() = default; |
| 766 |
DC::~DC() = default; |
| 767 |
|
| 768 |
UniquePtr<DC> DC::Dup() { |
| 769 |
bssl::UniquePtr<DC> ret = MakeUnique<DC>(); |
| 770 |
if (!ret) { |
| 771 |
return nullptr; |
| 772 |
} |
| 773 |
|
| 774 |
ret->raw = UpRef(raw); |
| 775 |
ret->dc_cert_verify_algorithm = dc_cert_verify_algorithm; |
| 776 |
ret->pkey = UpRef(pkey); |
| 777 |
return ret; |
| 778 |
} |
| 779 |
|
| 780 |
// static |
| 781 |
UniquePtr<DC> DC::Parse(CRYPTO_BUFFER *in, uint8_t *out_alert) { |
| 782 |
UniquePtr<DC> dc = MakeUnique<DC>(); |
| 783 |
if (!dc) { |
| 784 |
*out_alert = SSL_AD_INTERNAL_ERROR; |
| 785 |
return nullptr; |
| 786 |
} |
| 787 |
|
| 788 |
dc->raw = UpRef(in); |
| 789 |
|
| 790 |
CBS pubkey, deleg, sig; |
| 791 |
uint32_t valid_time; |
| 792 |
uint16_t algorithm; |
| 793 |
CRYPTO_BUFFER_init_CBS(dc->raw.get(), &deleg); |
| 794 |
if (!CBS_get_u32(&deleg, &valid_time) || |
| 795 |
!CBS_get_u16(&deleg, &dc->dc_cert_verify_algorithm) || |
| 796 |
!CBS_get_u24_length_prefixed(&deleg, &pubkey) || |
| 797 |
!CBS_get_u16(&deleg, &algorithm) || |
| 798 |
!CBS_get_u16_length_prefixed(&deleg, &sig) || CBS_len(&deleg) != 0) { |
| 799 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 800 |
*out_alert = SSL_AD_DECODE_ERROR; |
| 801 |
return nullptr; |
| 802 |
} |
| 803 |
|
| 804 |
dc->pkey.reset(EVP_parse_public_key(&pubkey)); |
| 805 |
if (dc->pkey == nullptr) { |
| 806 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 807 |
*out_alert = SSL_AD_DECODE_ERROR; |
| 808 |
return nullptr; |
| 809 |
} |
| 810 |
|
| 811 |
// RFC 9345 forbids algorithms that use the rsaEncryption OID. As the |
| 812 |
// RSASSA-PSS OID is unusably complicated, this effectively means we will not |
| 813 |
// support RSA delegated credentials. |
| 814 |
if (SSL_get_signature_algorithm_key_type(dc->dc_cert_verify_algorithm) == |
| 815 |
EVP_PKEY_RSA) { |
| 816 |
OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM); |
| 817 |
*out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 818 |
return nullptr; |
| 819 |
} |
| 820 |
|
| 821 |
return dc; |
| 822 |
} |
| 823 |
|
| 824 |
// ssl_can_serve_dc returns true if the host has configured a DC that it can |
| 825 |
// serve in the handshake. Specifically, it checks that a DC has been |
| 826 |
// configured and that the DC signature algorithm is supported by the peer. |
| 827 |
static bool ssl_can_serve_dc(const SSL_HANDSHAKE *hs) { |
| 828 |
// Check that a DC has been configured. |
| 829 |
const CERT *cert = hs->config->cert.get(); |
| 830 |
if (cert->dc == nullptr || cert->dc->raw == nullptr || |
| 831 |
(cert->dc_privatekey == nullptr && cert->dc_key_method == nullptr)) { |
| 832 |
return false; |
| 833 |
} |
| 834 |
|
| 835 |
// Check that 1.3 or higher has been negotiated. |
| 836 |
const DC *dc = cert->dc.get(); |
| 837 |
assert(hs->ssl->s3->have_version); |
| 838 |
if (ssl_protocol_version(hs->ssl) < TLS1_3_VERSION) { |
| 839 |
return false; |
| 840 |
} |
| 841 |
|
| 842 |
// Check that the DC signature algorithm is supported by the peer. |
| 843 |
Span<const uint16_t> peer_sigalgs = hs->peer_delegated_credential_sigalgs; |
| 844 |
for (uint16_t peer_sigalg : peer_sigalgs) { |
| 845 |
if (dc->dc_cert_verify_algorithm == peer_sigalg) { |
| 846 |
return true; |
| 847 |
} |
| 848 |
} |
| 849 |
return false; |
| 850 |
} |
| 851 |
|
| 852 |
bool ssl_signing_with_dc(const SSL_HANDSHAKE *hs) { |
| 853 |
// We only support delegated credentials as a server. |
| 854 |
return hs->ssl->server && ssl_can_serve_dc(hs); |
| 855 |
} |
| 856 |
|
| 857 |
static int cert_set_dc(CERT *cert, CRYPTO_BUFFER *const raw, EVP_PKEY *privkey, |
| 858 |
const SSL_PRIVATE_KEY_METHOD *key_method) { |
| 859 |
if (privkey == nullptr && key_method == nullptr) { |
| 860 |
OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER); |
| 861 |
return 0; |
| 862 |
} |
| 863 |
|
| 864 |
if (privkey != nullptr && key_method != nullptr) { |
| 865 |
OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_HAVE_BOTH_PRIVKEY_AND_METHOD); |
| 866 |
return 0; |
| 867 |
} |
| 868 |
|
| 869 |
uint8_t alert; |
| 870 |
UniquePtr<DC> dc = DC::Parse(raw, &alert); |
| 871 |
if (dc == nullptr) { |
| 872 |
OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_DELEGATED_CREDENTIAL); |
| 873 |
return 0; |
| 874 |
} |
| 875 |
|
| 876 |
if (privkey) { |
| 877 |
// Check that the public and private keys match. |
| 878 |
if (!ssl_compare_public_and_private_key(dc->pkey.get(), privkey)) { |
| 879 |
OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_AND_PRIVATE_KEY_MISMATCH); |
| 880 |
return 0; |
| 881 |
} |
| 882 |
} |
| 883 |
|
| 884 |
cert->dc = std::move(dc); |
| 885 |
cert->dc_privatekey = UpRef(privkey); |
| 886 |
cert->dc_key_method = key_method; |
| 887 |
|
| 888 |
return 1; |
| 889 |
} |
| 890 |
|
| 891 |
bool ssl_cert_check_cert_private_keys_usage(const CERT *cert) { |
| 892 |
if (cert == nullptr || cert->cert_private_keys.size() != SSL_PKEY_SIZE || |
| 893 |
cert->cert_private_key_idx < 0 || |
| 894 |
cert->cert_private_key_idx >= SSL_PKEY_SIZE) { |
| 895 |
return false; |
| 896 |
} |
| 897 |
return true; |
| 898 |
} |
| 899 |
|
| 900 |
BSSL_NAMESPACE_END |
| 901 |
|
| 902 |
using namespace bssl; |
| 903 |
|
| 904 |
static int cert_array_to_stack(CRYPTO_BUFFER *const *from, |
| 905 |
UniquePtr<STACK_OF(CRYPTO_BUFFER)> *to, |
| 906 |
size_t num_certs) { |
| 907 |
for (size_t i = 0; i < num_certs; i++) { |
| 908 |
if (!PushToStack(to->get(), UpRef(from[i]))) { |
| 909 |
return 0; |
| 910 |
} |
| 911 |
} |
| 912 |
return 1; |
| 913 |
} |
| 914 |
|
| 915 |
int SSL_set_chain_and_key(SSL *ssl, CRYPTO_BUFFER *const *certs, |
| 916 |
size_t num_certs, EVP_PKEY *privkey, |
| 917 |
const SSL_PRIVATE_KEY_METHOD *privkey_method) { |
| 918 |
if (!ssl->config) { |
| 919 |
return 0; |
| 920 |
} |
| 921 |
UniquePtr<STACK_OF(CRYPTO_BUFFER)> crypto_certs(sk_CRYPTO_BUFFER_new_null()); |
| 922 |
if (!cert_array_to_stack(certs, &crypto_certs, num_certs)) { |
| 923 |
return 0; |
| 924 |
} |
| 925 |
return cert_set_chain_and_key(ssl->config->cert.get(), crypto_certs, num_certs, |
| 926 |
privkey, privkey_method); |
| 927 |
} |
| 928 |
|
| 929 |
int SSL_CTX_set_chain_and_key(SSL_CTX *ctx, CRYPTO_BUFFER *const *certs, |
| 930 |
size_t num_certs, EVP_PKEY *privkey, |
| 931 |
const SSL_PRIVATE_KEY_METHOD *privkey_method) { |
| 932 |
UniquePtr<STACK_OF(CRYPTO_BUFFER)> crypto_certs(sk_CRYPTO_BUFFER_new_null()); |
| 933 |
if (!cert_array_to_stack(certs, &crypto_certs, num_certs)) { |
| 934 |
return 0; |
| 935 |
} |
| 936 |
return cert_set_chain_and_key(ctx->cert.get(), crypto_certs, num_certs, privkey, |
| 937 |
privkey_method); |
| 938 |
} |
| 939 |
|
| 940 |
static int cert_use_cert_and_key(CERT *cert, X509 *x509, EVP_PKEY *privatekey, |
| 941 |
STACK_OF(X509) *chain, int override) { |
| 942 |
if (privatekey == nullptr || x509 == nullptr) { |
| 943 |
OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER); |
| 944 |
return 0; |
| 945 |
} |
| 946 |
|
| 947 |
// Check override value |
| 948 |
int idx = ssl_get_certificate_slot_index(privatekey); |
| 949 |
if (idx < 0) { |
| 950 |
OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE); |
| 951 |
return 0; |
| 952 |
} |
| 953 |
CERT_PKEY *cert_pkey = &cert->cert_private_keys[idx]; |
| 954 |
if (!override && (cert_pkey->privatekey != NULL || |
| 955 |
cert_pkey->x509_leaf != NULL || |
| 956 |
cert_pkey->chain != NULL)) { |
| 957 |
OPENSSL_PUT_ERROR(SSL, SSL_R_NOT_REPLACING_CERTIFICATE); |
| 958 |
return 0; |
| 959 |
} |
| 960 |
|
| 961 |
UniquePtr<STACK_OF(CRYPTO_BUFFER)> leaf_and_chain(sk_CRYPTO_BUFFER_new_null()); |
| 962 |
|
| 963 |
// Convert leaf certificate |x509| to CRYPTO_BUFFER |
| 964 |
uint8_t *buf = nullptr; |
| 965 |
int cert_len = i2d_X509(x509, &buf); |
| 966 |
if (cert_len <= 0) { |
| 967 |
OPENSSL_free(buf); |
| 968 |
return 0; |
| 969 |
} |
| 970 |
|
| 971 |
// Add leaf cert first to the chain |
| 972 |
UniquePtr<CRYPTO_BUFFER> leaf_buf(CRYPTO_BUFFER_new(buf, cert_len, nullptr)); |
| 973 |
OPENSSL_free(buf); |
| 974 |
if (!leaf_buf || |
| 975 |
!PushToStack(leaf_and_chain.get(), std::move(leaf_buf))) { |
| 976 |
return 0; |
| 977 |
} |
| 978 |
|
| 979 |
// Convert other chain certificates to CRYPTO_BUFFER objects |
| 980 |
if (chain != nullptr) { |
| 981 |
for (size_t i = 0; i < sk_X509_num(chain); i++) { |
| 982 |
buf = nullptr; |
| 983 |
cert_len = i2d_X509(sk_X509_value(chain, i), &buf); |
| 984 |
if (cert_len <= 0) { |
| 985 |
OPENSSL_free(buf); |
| 986 |
return 0; |
| 987 |
} |
| 988 |
|
| 989 |
UniquePtr<CRYPTO_BUFFER> chain_buf(CRYPTO_BUFFER_new(buf, cert_len, nullptr)); |
| 990 |
OPENSSL_free(buf); |
| 991 |
if (!chain_buf || |
| 992 |
!PushToStack(leaf_and_chain.get(), std::move(chain_buf))) { |
| 993 |
return 0; |
| 994 |
} |
| 995 |
} |
| 996 |
} |
| 997 |
|
| 998 |
// Set the chain and key |
| 999 |
if (!cert_set_chain_and_key(cert, leaf_and_chain, |
| 1000 |
sk_CRYPTO_BUFFER_num(leaf_and_chain.get()), |
| 1001 |
privatekey, nullptr)) { |
| 1002 |
return 0; |
| 1003 |
} |
| 1004 |
|
| 1005 |
// Update the leaf certificate |
| 1006 |
if (cert_pkey->x509_leaf) { |
| 1007 |
X509_free(cert_pkey->x509_leaf); |
| 1008 |
} |
| 1009 |
X509_up_ref(x509); |
| 1010 |
cert_pkey->x509_leaf = x509; |
| 1011 |
return 1; |
| 1012 |
} |
| 1013 |
|
| 1014 |
int SSL_CTX_use_cert_and_key(SSL_CTX *ctx, X509 *x509, EVP_PKEY *privatekey, |
| 1015 |
STACK_OF(X509) *chain, int override) { |
| 1016 |
return cert_use_cert_and_key(ctx->cert.get(), x509, privatekey, chain, |
| 1017 |
override); |
| 1018 |
} |
| 1019 |
|
| 1020 |
int SSL_use_cert_and_key(SSL *ssl, X509 *x509, EVP_PKEY *privatekey, |
| 1021 |
STACK_OF(X509) *chain, int override) { |
| 1022 |
if (!ssl->config) { |
| 1023 |
return 0; |
| 1024 |
} |
| 1025 |
return cert_use_cert_and_key(ssl->config->cert.get(), x509, privatekey, |
| 1026 |
chain, override); |
| 1027 |
} |
| 1028 |
|
| 1029 |
void SSL_certs_clear(SSL *ssl) { |
| 1030 |
if (!ssl->config) { |
| 1031 |
return; |
| 1032 |
} |
| 1033 |
|
| 1034 |
CERT *cert = ssl->config->cert.get(); |
| 1035 |
cert->x509_method->cert_clear(cert); |
| 1036 |
|
| 1037 |
cert->cert_private_key_idx = -1; |
| 1038 |
for (auto &cert_private_key : cert->cert_private_keys) { |
| 1039 |
cert_private_key.chain.reset(); |
| 1040 |
cert_private_key.privatekey.reset(); |
| 1041 |
} |
| 1042 |
cert->key_method = nullptr; |
| 1043 |
|
| 1044 |
cert->dc.reset(); |
| 1045 |
cert->dc_privatekey.reset(); |
| 1046 |
cert->dc_key_method = nullptr; |
| 1047 |
} |
| 1048 |
|
| 1049 |
const STACK_OF(CRYPTO_BUFFER) *SSL_CTX_get0_chain(const SSL_CTX *ctx) { |
| 1050 |
if (!ssl_cert_check_cert_private_keys_usage(ctx->cert.get())) { |
| 1051 |
return nullptr; |
| 1052 |
} |
| 1053 |
return ctx->cert->cert_private_keys[ctx->cert->cert_private_key_idx] |
| 1054 |
.chain.get(); |
| 1055 |
} |
| 1056 |
|
| 1057 |
int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, size_t der_len, |
| 1058 |
const uint8_t *der) { |
| 1059 |
UniquePtr<CRYPTO_BUFFER> buffer(CRYPTO_BUFFER_new(der, der_len, NULL)); |
| 1060 |
if (!buffer) { |
| 1061 |
return 0; |
| 1062 |
} |
| 1063 |
|
| 1064 |
return ssl_set_cert(ctx->cert.get(), std::move(buffer)); |
| 1065 |
} |
| 1066 |
|
| 1067 |
int SSL_use_certificate_ASN1(SSL *ssl, const uint8_t *der, size_t der_len) { |
| 1068 |
UniquePtr<CRYPTO_BUFFER> buffer(CRYPTO_BUFFER_new(der, der_len, NULL)); |
| 1069 |
if (!buffer || !ssl->config) { |
| 1070 |
return 0; |
| 1071 |
} |
| 1072 |
|
| 1073 |
return ssl_set_cert(ssl->config->cert.get(), std::move(buffer)); |
| 1074 |
} |
| 1075 |
|
| 1076 |
void SSL_CTX_set_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl, void *arg), |
| 1077 |
void *arg) { |
| 1078 |
ssl_cert_set_cert_cb(ctx->cert.get(), cb, arg); |
| 1079 |
} |
| 1080 |
|
| 1081 |
void SSL_set_cert_cb(SSL *ssl, int (*cb)(SSL *ssl, void *arg), void *arg) { |
| 1082 |
if (!ssl->config) { |
| 1083 |
return; |
| 1084 |
} |
| 1085 |
ssl_cert_set_cert_cb(ssl->config->cert.get(), cb, arg); |
| 1086 |
} |
| 1087 |
|
| 1088 |
const STACK_OF(CRYPTO_BUFFER) *SSL_get0_peer_certificates(const SSL *ssl) { |
| 1089 |
SSL_SESSION *session = SSL_get_session(ssl); |
| 1090 |
if (session == NULL) { |
| 1091 |
return NULL; |
| 1092 |
} |
| 1093 |
|
| 1094 |
return session->certs.get(); |
| 1095 |
} |
| 1096 |
|
| 1097 |
const STACK_OF(CRYPTO_BUFFER) *SSL_get0_server_requested_CAs(const SSL *ssl) { |
| 1098 |
if (ssl->s3->hs != NULL) { |
| 1099 |
return ssl->s3->hs->ca_names.get(); |
| 1100 |
} |
| 1101 |
return NULL; |
| 1102 |
} |
| 1103 |
|
| 1104 |
static int set_signed_cert_timestamp_list(CERT *cert, const uint8_t *list, |
| 1105 |
size_t list_len) { |
| 1106 |
CBS sct_list; |
| 1107 |
CBS_init(&sct_list, list, list_len); |
| 1108 |
if (!ssl_is_sct_list_valid(&sct_list)) { |
| 1109 |
OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SCT_LIST); |
| 1110 |
return 0; |
| 1111 |
} |
| 1112 |
|
| 1113 |
cert->signed_cert_timestamp_list.reset( |
| 1114 |
CRYPTO_BUFFER_new(CBS_data(&sct_list), CBS_len(&sct_list), nullptr)); |
| 1115 |
return cert->signed_cert_timestamp_list != nullptr; |
| 1116 |
} |
| 1117 |
|
| 1118 |
int SSL_CTX_set_signed_cert_timestamp_list(SSL_CTX *ctx, const uint8_t *list, |
| 1119 |
size_t list_len) { |
| 1120 |
return set_signed_cert_timestamp_list(ctx->cert.get(), list, list_len); |
| 1121 |
} |
| 1122 |
|
| 1123 |
int SSL_set_signed_cert_timestamp_list(SSL *ssl, const uint8_t *list, |
| 1124 |
size_t list_len) { |
| 1125 |
if (!ssl->config) { |
| 1126 |
return 0; |
| 1127 |
} |
| 1128 |
return set_signed_cert_timestamp_list(ssl->config->cert.get(), list, |
| 1129 |
list_len); |
| 1130 |
} |
| 1131 |
|
| 1132 |
int SSL_CTX_set_ocsp_response(SSL_CTX *ctx, const uint8_t *response, |
| 1133 |
size_t response_len) { |
| 1134 |
ctx->cert->ocsp_response.reset( |
| 1135 |
CRYPTO_BUFFER_new(response, response_len, nullptr)); |
| 1136 |
return ctx->cert->ocsp_response != nullptr; |
| 1137 |
} |
| 1138 |
|
| 1139 |
int SSL_set_ocsp_response(SSL *ssl, const uint8_t *response, |
| 1140 |
size_t response_len) { |
| 1141 |
if (!ssl->config) { |
| 1142 |
return 0; |
| 1143 |
} |
| 1144 |
ssl->config->cert->ocsp_response.reset( |
| 1145 |
CRYPTO_BUFFER_new(response, response_len, nullptr)); |
| 1146 |
return ssl->config->cert->ocsp_response != nullptr; |
| 1147 |
} |
| 1148 |
|
| 1149 |
void SSL_CTX_set0_client_CAs(SSL_CTX *ctx, STACK_OF(CRYPTO_BUFFER) *name_list) { |
| 1150 |
ctx->x509_method->ssl_ctx_flush_cached_client_CA(ctx); |
| 1151 |
ctx->client_CA.reset(name_list); |
| 1152 |
} |
| 1153 |
|
| 1154 |
void SSL_set0_client_CAs(SSL *ssl, STACK_OF(CRYPTO_BUFFER) *name_list) { |
| 1155 |
if (!ssl->config) { |
| 1156 |
return; |
| 1157 |
} |
| 1158 |
ssl->ctx->x509_method->ssl_flush_cached_client_CA(ssl->config.get()); |
| 1159 |
ssl->config->client_CA.reset(name_list); |
| 1160 |
} |
| 1161 |
|
| 1162 |
int SSL_set1_delegated_credential(SSL *ssl, CRYPTO_BUFFER *dc, EVP_PKEY *pkey, |
| 1163 |
const SSL_PRIVATE_KEY_METHOD *key_method) { |
| 1164 |
if (!ssl->config) { |
| 1165 |
return 0; |
| 1166 |
} |
| 1167 |
|
| 1168 |
return cert_set_dc(ssl->config->cert.get(), dc, pkey, key_method); |
| 1169 |
} |
| 1170 |
|
| 1171 |
int SSL_delegated_credential_used(const SSL *ssl) { |
| 1172 |
return ssl->s3->delegated_credential_used; |
| 1173 |
} |
| 1174 |
|
| 1175 |
int SSL_CTX_get_security_level(const SSL_CTX *ctx) { return 0; } |
| 1176 |
|
| 1177 |
void SSL_CTX_set_security_level(const SSL_CTX *ctx, int level) {} |