| 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 |
// Copyright 2005 Nokia. All rights reserved. |
| 5 |
// |
| 6 |
// ECC cipher suite support in OpenSSL originally developed by |
| 7 |
// SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. |
| 8 |
// |
| 9 |
// The Contribution, originally written by Mika Kousa and Pasi Eronen of |
| 10 |
// Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites |
| 11 |
// support (see RFC 4279) to OpenSSL. |
| 12 |
// |
| 13 |
// SPDX-License-Identifier: Apache-2.0 |
| 14 |
|
| 15 |
#include <openssl/ssl.h> |
| 16 |
|
| 17 |
#include <assert.h> |
| 18 |
|
| 19 |
#include <openssl/asn1.h> |
| 20 |
#include <openssl/bytestring.h> |
| 21 |
#include <openssl/err.h> |
| 22 |
#include <openssl/pem.h> |
| 23 |
#include <openssl/stack.h> |
| 24 |
#include <openssl/x509.h> |
| 25 |
|
| 26 |
#include "../crypto/internal.h" |
| 27 |
#include "internal.h" |
| 28 |
|
| 29 |
|
| 30 |
BSSL_NAMESPACE_BEGIN |
| 31 |
|
| 32 |
// check_ssl_x509_method asserts that |ssl| has the X509-based method |
| 33 |
// installed. Calling an X509-based method on an |ssl| with a different method |
| 34 |
// will likely misbehave and possibly crash or leak memory. |
| 35 |
static void check_ssl_x509_method(const SSL *ssl) { |
| 36 |
assert(ssl == NULL || ssl->ctx->x509_method == &ssl_crypto_x509_method); |
| 37 |
} |
| 38 |
|
| 39 |
// check_ssl_ctx_x509_method acts like |check_ssl_x509_method|, but for an |
| 40 |
// |SSL_CTX|. |
| 41 |
static void check_ssl_ctx_x509_method(const SSL_CTX *ctx) { |
| 42 |
assert(ctx == NULL || ctx->x509_method == &ssl_crypto_x509_method); |
| 43 |
} |
| 44 |
|
| 45 |
// x509_to_buffer returns a |CRYPTO_BUFFER| that contains the serialised |
| 46 |
// contents of |x509|. |
| 47 |
UniquePtr<CRYPTO_BUFFER> x509_to_buffer(X509 *x509) { |
| 48 |
uint8_t *buf = NULL; |
| 49 |
int cert_len = i2d_X509(x509, &buf); |
| 50 |
if (cert_len <= 0) { |
| 51 |
return 0; |
| 52 |
} |
| 53 |
|
| 54 |
UniquePtr<CRYPTO_BUFFER> buffer(CRYPTO_BUFFER_new(buf, cert_len, NULL)); |
| 55 |
OPENSSL_free(buf); |
| 56 |
|
| 57 |
return buffer; |
| 58 |
} |
| 59 |
|
| 60 |
// new_leafless_chain returns a fresh stack of buffers set to {NULL}. |
| 61 |
static UniquePtr<STACK_OF(CRYPTO_BUFFER)> new_leafless_chain(void) { |
| 62 |
UniquePtr<STACK_OF(CRYPTO_BUFFER)> chain(sk_CRYPTO_BUFFER_new_null()); |
| 63 |
if (!chain || !sk_CRYPTO_BUFFER_push(chain.get(), nullptr)) { |
| 64 |
return nullptr; |
| 65 |
} |
| 66 |
|
| 67 |
return chain; |
| 68 |
} |
| 69 |
|
| 70 |
// ssl_cert_set_chain sets elements 1.. of |cert->chain| to the serialised |
| 71 |
// forms of elements of |chain|. It returns one on success or zero on error, in |
| 72 |
// which case no change to |cert->chain| is made. It preserves the existing |
| 73 |
// leaf from |cert->chain|, if any. |
| 74 |
static bool ssl_cert_set_chain(CERT *cert, STACK_OF(X509) *chain) { |
| 75 |
if (!ssl_cert_check_cert_private_keys_usage(cert)) { |
| 76 |
return false; |
| 77 |
} |
| 78 |
|
| 79 |
UniquePtr<STACK_OF(CRYPTO_BUFFER)> new_chain; |
| 80 |
UniquePtr<STACK_OF(CRYPTO_BUFFER)> &old_chain = |
| 81 |
cert->cert_private_keys[cert->cert_private_key_idx].chain; |
| 82 |
|
| 83 |
|
| 84 |
if (old_chain != nullptr) { |
| 85 |
new_chain.reset(sk_CRYPTO_BUFFER_new_null()); |
| 86 |
if (!new_chain) { |
| 87 |
return false; |
| 88 |
} |
| 89 |
|
| 90 |
// |leaf| might be NULL if it's a “leafless” chain. |
| 91 |
CRYPTO_BUFFER *leaf = sk_CRYPTO_BUFFER_value(old_chain.get(), 0); |
| 92 |
if (!PushToStack(new_chain.get(), UpRef(leaf))) { |
| 93 |
return false; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
for (X509 *x509 : chain) { |
| 98 |
if (!new_chain) { |
| 99 |
new_chain = new_leafless_chain(); |
| 100 |
if (!new_chain) { |
| 101 |
return false; |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
UniquePtr<CRYPTO_BUFFER> buffer = x509_to_buffer(x509); |
| 106 |
if (!buffer || !PushToStack(new_chain.get(), std::move(buffer))) { |
| 107 |
return false; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
old_chain = std::move(new_chain); |
| 112 |
return true; |
| 113 |
} |
| 114 |
|
| 115 |
static void ssl_crypto_x509_cert_flush_leaf(CERT *cert) { |
| 116 |
for (auto &cert_privatekey : cert->cert_private_keys) { |
| 117 |
X509_free(cert_privatekey.x509_leaf); |
| 118 |
cert_privatekey.x509_leaf = nullptr; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
static void ssl_crypto_x509_cert_flush_cached_chain(CERT *cert) { |
| 123 |
for (auto &cert_privatekey : cert->cert_private_keys) { |
| 124 |
sk_X509_pop_free(cert_privatekey.x509_chain, X509_free); |
| 125 |
cert_privatekey.x509_chain = nullptr; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
static bool ssl_crypto_x509_check_client_CA_list( |
| 130 |
STACK_OF(CRYPTO_BUFFER) *names) { |
| 131 |
for (const CRYPTO_BUFFER *buffer : names) { |
| 132 |
const uint8_t *inp = CRYPTO_BUFFER_data(buffer); |
| 133 |
UniquePtr<X509_NAME> name( |
| 134 |
d2i_X509_NAME(nullptr, &inp, CRYPTO_BUFFER_len(buffer))); |
| 135 |
if (name == nullptr || |
| 136 |
inp != CRYPTO_BUFFER_data(buffer) + CRYPTO_BUFFER_len(buffer)) { |
| 137 |
return false; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
return true; |
| 142 |
} |
| 143 |
|
| 144 |
static void ssl_crypto_x509_cert_clear(CERT *cert) { |
| 145 |
ssl_crypto_x509_cert_flush_leaf(cert); |
| 146 |
ssl_crypto_x509_cert_flush_cached_chain(cert); |
| 147 |
|
| 148 |
X509_free(cert->x509_stash); |
| 149 |
cert->x509_stash = nullptr; |
| 150 |
} |
| 151 |
|
| 152 |
static void ssl_crypto_x509_cert_free(CERT *cert) { |
| 153 |
ssl_crypto_x509_cert_clear(cert); |
| 154 |
X509_STORE_free(cert->verify_store); |
| 155 |
} |
| 156 |
|
| 157 |
static void ssl_crypto_x509_cert_dup(CERT *new_cert, const CERT *cert) { |
| 158 |
if (cert->verify_store != nullptr) { |
| 159 |
X509_STORE_up_ref(cert->verify_store); |
| 160 |
new_cert->verify_store = cert->verify_store; |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
static bool ssl_crypto_x509_session_cache_objects(SSL_SESSION *sess) { |
| 165 |
bssl::UniquePtr<STACK_OF(X509)> chain, chain_without_leaf; |
| 166 |
if (sk_CRYPTO_BUFFER_num(sess->certs.get()) > 0) { |
| 167 |
chain.reset(sk_X509_new_null()); |
| 168 |
if (!chain) { |
| 169 |
return false; |
| 170 |
} |
| 171 |
if (sess->is_server) { |
| 172 |
// chain_without_leaf is only needed for server sessions. See |
| 173 |
// |SSL_get_peer_cert_chain|. |
| 174 |
chain_without_leaf.reset(sk_X509_new_null()); |
| 175 |
if (!chain_without_leaf) { |
| 176 |
return false; |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
bssl::UniquePtr<X509> leaf; |
| 182 |
for (CRYPTO_BUFFER *cert : sess->certs.get()) { |
| 183 |
UniquePtr<X509> x509(X509_parse_from_buffer(cert)); |
| 184 |
if (!x509) { |
| 185 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 186 |
return false; |
| 187 |
} |
| 188 |
if (leaf == nullptr) { |
| 189 |
leaf = UpRef(x509); |
| 190 |
} else if (chain_without_leaf && |
| 191 |
!PushToStack(chain_without_leaf.get(), UpRef(x509))) { |
| 192 |
return false; |
| 193 |
} |
| 194 |
if (!PushToStack(chain.get(), std::move(x509))) { |
| 195 |
return false; |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
sk_X509_pop_free(sess->x509_chain, X509_free); |
| 200 |
sess->x509_chain = chain.release(); |
| 201 |
|
| 202 |
sk_X509_pop_free(sess->x509_chain_without_leaf, X509_free); |
| 203 |
sess->x509_chain_without_leaf = chain_without_leaf.release(); |
| 204 |
|
| 205 |
X509_free(sess->x509_peer); |
| 206 |
sess->x509_peer = leaf.release(); |
| 207 |
|
| 208 |
sk_X509_pop_free(sess->x509_verified_chain, X509_free); |
| 209 |
sess->x509_verified_chain = nullptr; |
| 210 |
|
| 211 |
return true; |
| 212 |
} |
| 213 |
|
| 214 |
static bool ssl_crypto_x509_session_dup(SSL_SESSION *new_session, |
| 215 |
const SSL_SESSION *session) { |
| 216 |
new_session->x509_peer = UpRef(session->x509_peer).release(); |
| 217 |
if (session->x509_chain != nullptr) { |
| 218 |
new_session->x509_chain = X509_chain_up_ref(session->x509_chain); |
| 219 |
if (new_session->x509_chain == nullptr) { |
| 220 |
return false; |
| 221 |
} |
| 222 |
} |
| 223 |
if (session->x509_chain_without_leaf != nullptr) { |
| 224 |
new_session->x509_chain_without_leaf = |
| 225 |
X509_chain_up_ref(session->x509_chain_without_leaf); |
| 226 |
if (new_session->x509_chain_without_leaf == nullptr) { |
| 227 |
return false; |
| 228 |
} |
| 229 |
} |
| 230 |
if (session->x509_verified_chain != nullptr) { |
| 231 |
new_session->x509_verified_chain = |
| 232 |
X509_chain_up_ref(session->x509_verified_chain); |
| 233 |
if (new_session->x509_verified_chain == nullptr) { |
| 234 |
return false; |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
return true; |
| 239 |
} |
| 240 |
|
| 241 |
static void ssl_crypto_x509_session_clear(SSL_SESSION *session) { |
| 242 |
X509_free(session->x509_peer); |
| 243 |
session->x509_peer = nullptr; |
| 244 |
sk_X509_pop_free(session->x509_chain, X509_free); |
| 245 |
session->x509_chain = nullptr; |
| 246 |
sk_X509_pop_free(session->x509_chain_without_leaf, X509_free); |
| 247 |
session->x509_chain_without_leaf = nullptr; |
| 248 |
sk_X509_pop_free(session->x509_verified_chain, X509_free); |
| 249 |
session->x509_verified_chain = nullptr; |
| 250 |
} |
| 251 |
|
| 252 |
static bool ssl_crypto_x509_session_verify_cert_chain(SSL_SESSION *session, |
| 253 |
SSL_HANDSHAKE *hs, |
| 254 |
uint8_t *out_alert) { |
| 255 |
*out_alert = SSL_AD_INTERNAL_ERROR; |
| 256 |
STACK_OF(X509) *const cert_chain = session->x509_chain; |
| 257 |
if (cert_chain == nullptr || sk_X509_num(cert_chain) == 0) { |
| 258 |
return false; |
| 259 |
} |
| 260 |
|
| 261 |
SSL *const ssl = hs->ssl; |
| 262 |
SSL_CTX *ssl_ctx = ssl->ctx.get(); |
| 263 |
X509_STORE *verify_store = ssl_ctx->cert_store; |
| 264 |
if (hs->config->cert->verify_store != nullptr) { |
| 265 |
verify_store = hs->config->cert->verify_store; |
| 266 |
} |
| 267 |
|
| 268 |
X509 *leaf = sk_X509_value(cert_chain, 0); |
| 269 |
const char *name; |
| 270 |
size_t name_len; |
| 271 |
SSL_get0_ech_name_override(ssl, &name, &name_len); |
| 272 |
UniquePtr<X509_STORE_CTX> ctx(X509_STORE_CTX_new()); |
| 273 |
if (!ctx || !X509_STORE_CTX_init(ctx.get(), verify_store, leaf, cert_chain) || |
| 274 |
!X509_STORE_CTX_set_ex_data(ctx.get(), |
| 275 |
SSL_get_ex_data_X509_STORE_CTX_idx(), ssl) || |
| 276 |
// We need to inherit the verify parameters. These can be determined by |
| 277 |
// the context: if its a server it will verify SSL client certificates or |
| 278 |
// vice versa. |
| 279 |
!X509_STORE_CTX_set_default(ctx.get(), |
| 280 |
ssl->server ? "ssl_client" : "ssl_server") || |
| 281 |
// Anything non-default in "param" should overwrite anything in the ctx. |
| 282 |
!X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(ctx.get()), |
| 283 |
hs->config->param) || |
| 284 |
// ClientHelloOuter connections use a different name. |
| 285 |
(name_len != 0 && |
| 286 |
!X509_VERIFY_PARAM_set1_host(X509_STORE_CTX_get0_param(ctx.get()), name, |
| 287 |
name_len))) { |
| 288 |
OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB); |
| 289 |
return false; |
| 290 |
} |
| 291 |
|
| 292 |
if (hs->config->verify_callback) { |
| 293 |
X509_STORE_CTX_set_verify_cb(ctx.get(), hs->config->verify_callback); |
| 294 |
} |
| 295 |
|
| 296 |
int verify_ret; |
| 297 |
if (ssl_ctx->app_verify_callback != nullptr) { |
| 298 |
verify_ret = |
| 299 |
ssl_ctx->app_verify_callback(ctx.get(), ssl_ctx->app_verify_arg); |
| 300 |
} else { |
| 301 |
verify_ret = X509_verify_cert(ctx.get()); |
| 302 |
} |
| 303 |
|
| 304 |
session->verify_result = X509_STORE_CTX_get_error(ctx.get()); |
| 305 |
|
| 306 |
// If |SSL_VERIFY_NONE|, the error is non-fatal, but we keep the result. |
| 307 |
if (verify_ret <= 0 && hs->config->verify_mode != SSL_VERIFY_NONE) { |
| 308 |
*out_alert = SSL_alert_from_verify_result(session->verify_result); |
| 309 |
return false; |
| 310 |
} |
| 311 |
|
| 312 |
sk_X509_pop_free(session->x509_verified_chain, X509_free); |
| 313 |
session->x509_verified_chain = X509_STORE_CTX_get1_chain(ctx.get()); |
| 314 |
|
| 315 |
ERR_clear_error(); |
| 316 |
return true; |
| 317 |
} |
| 318 |
|
| 319 |
static void ssl_crypto_x509_hs_flush_cached_ca_names(SSL_HANDSHAKE *hs) { |
| 320 |
sk_X509_NAME_pop_free(hs->ssl->s3->cached_x509_peer_ca_names, |
| 321 |
X509_NAME_free); |
| 322 |
hs->ssl->s3->cached_x509_peer_ca_names = nullptr; |
| 323 |
} |
| 324 |
|
| 325 |
static bool ssl_crypto_x509_ssl_new(SSL_HANDSHAKE *hs) { |
| 326 |
hs->config->param = X509_VERIFY_PARAM_new(); |
| 327 |
if (hs->config->param == nullptr) { |
| 328 |
return false; |
| 329 |
} |
| 330 |
X509_VERIFY_PARAM_inherit(hs->config->param, hs->ssl->ctx->param); |
| 331 |
return true; |
| 332 |
} |
| 333 |
|
| 334 |
static void ssl_crypto_x509_ssl_flush_cached_client_CA(SSL_CONFIG *cfg) { |
| 335 |
sk_X509_NAME_pop_free(cfg->cached_x509_client_CA, X509_NAME_free); |
| 336 |
cfg->cached_x509_client_CA = nullptr; |
| 337 |
} |
| 338 |
|
| 339 |
static void ssl_crypto_x509_ssl_config_free(SSL_CONFIG *cfg) { |
| 340 |
sk_X509_NAME_pop_free(cfg->cached_x509_client_CA, X509_NAME_free); |
| 341 |
cfg->cached_x509_client_CA = nullptr; |
| 342 |
X509_VERIFY_PARAM_free(cfg->param); |
| 343 |
} |
| 344 |
|
| 345 |
static bool ssl_crypto_x509_ssl_auto_chain_if_needed(SSL_HANDSHAKE *hs) { |
| 346 |
if (!ssl_cert_check_cert_private_keys_usage(hs->config->cert.get())) { |
| 347 |
return false; |
| 348 |
} |
| 349 |
|
| 350 |
// Only build a chain if there are no intermediates configured and the feature |
| 351 |
// isn't disabled. |
| 352 |
UniquePtr<STACK_OF(CRYPTO_BUFFER)> &cert_chain = |
| 353 |
hs->config->cert |
| 354 |
->cert_private_keys[hs->config->cert->cert_private_key_idx] |
| 355 |
.chain; |
| 356 |
if ((hs->ssl->mode & SSL_MODE_NO_AUTO_CHAIN) || !ssl_has_certificate(hs) || |
| 357 |
cert_chain == nullptr || sk_CRYPTO_BUFFER_num(cert_chain.get()) > 1) { |
| 358 |
return true; |
| 359 |
} |
| 360 |
|
| 361 |
UniquePtr<X509> leaf( |
| 362 |
X509_parse_from_buffer(sk_CRYPTO_BUFFER_value(cert_chain.get(), 0))); |
| 363 |
if (!leaf) { |
| 364 |
OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB); |
| 365 |
return false; |
| 366 |
} |
| 367 |
|
| 368 |
UniquePtr<X509_STORE_CTX> ctx(X509_STORE_CTX_new()); |
| 369 |
if (!ctx || !X509_STORE_CTX_init(ctx.get(), hs->ssl->ctx->cert_store, |
| 370 |
leaf.get(), nullptr)) { |
| 371 |
OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB); |
| 372 |
return false; |
| 373 |
} |
| 374 |
|
| 375 |
// Attempt to build a chain, ignoring the result. |
| 376 |
X509_verify_cert(ctx.get()); |
| 377 |
ERR_clear_error(); |
| 378 |
|
| 379 |
// Remove the leaf from the generated chain. |
| 380 |
UniquePtr<STACK_OF(X509)> chain(X509_STORE_CTX_get1_chain(ctx.get())); |
| 381 |
if (!chain) { |
| 382 |
return false; |
| 383 |
} |
| 384 |
X509_free(sk_X509_shift(chain.get())); |
| 385 |
|
| 386 |
if (!ssl_cert_set_chain(hs->config->cert.get(), chain.get())) { |
| 387 |
return false; |
| 388 |
} |
| 389 |
|
| 390 |
ssl_crypto_x509_cert_flush_cached_chain(hs->config->cert.get()); |
| 391 |
|
| 392 |
return true; |
| 393 |
} |
| 394 |
|
| 395 |
static void ssl_crypto_x509_ssl_ctx_flush_cached_client_CA(SSL_CTX *ctx) { |
| 396 |
sk_X509_NAME_pop_free(ctx->cached_x509_client_CA, X509_NAME_free); |
| 397 |
ctx->cached_x509_client_CA = nullptr; |
| 398 |
} |
| 399 |
|
| 400 |
static bool ssl_crypto_x509_ssl_ctx_new(SSL_CTX *ctx) { |
| 401 |
ctx->cert_store = X509_STORE_new(); |
| 402 |
ctx->param = X509_VERIFY_PARAM_new(); |
| 403 |
return (ctx->cert_store != nullptr && ctx->param != nullptr); |
| 404 |
} |
| 405 |
|
| 406 |
static void ssl_crypto_x509_ssl_ctx_free(SSL_CTX *ctx) { |
| 407 |
ssl_crypto_x509_ssl_ctx_flush_cached_client_CA(ctx); |
| 408 |
X509_VERIFY_PARAM_free(ctx->param); |
| 409 |
X509_STORE_free(ctx->cert_store); |
| 410 |
} |
| 411 |
|
| 412 |
const SSL_X509_METHOD ssl_crypto_x509_method = { |
| 413 |
ssl_crypto_x509_check_client_CA_list, |
| 414 |
ssl_crypto_x509_cert_clear, |
| 415 |
ssl_crypto_x509_cert_free, |
| 416 |
ssl_crypto_x509_cert_dup, |
| 417 |
ssl_crypto_x509_cert_flush_cached_chain, |
| 418 |
ssl_crypto_x509_cert_flush_leaf, |
| 419 |
ssl_crypto_x509_session_cache_objects, |
| 420 |
ssl_crypto_x509_session_dup, |
| 421 |
ssl_crypto_x509_session_clear, |
| 422 |
ssl_crypto_x509_session_verify_cert_chain, |
| 423 |
ssl_crypto_x509_hs_flush_cached_ca_names, |
| 424 |
ssl_crypto_x509_ssl_new, |
| 425 |
ssl_crypto_x509_ssl_config_free, |
| 426 |
ssl_crypto_x509_ssl_flush_cached_client_CA, |
| 427 |
ssl_crypto_x509_ssl_auto_chain_if_needed, |
| 428 |
ssl_crypto_x509_ssl_ctx_new, |
| 429 |
ssl_crypto_x509_ssl_ctx_free, |
| 430 |
ssl_crypto_x509_ssl_ctx_flush_cached_client_CA, |
| 431 |
}; |
| 432 |
|
| 433 |
BSSL_NAMESPACE_END |
| 434 |
|
| 435 |
using namespace bssl; |
| 436 |
|
| 437 |
X509 *SSL_get_peer_certificate(const SSL *ssl) { |
| 438 |
check_ssl_x509_method(ssl); |
| 439 |
if (ssl == NULL) { |
| 440 |
return NULL; |
| 441 |
} |
| 442 |
SSL_SESSION *session = SSL_get_session(ssl); |
| 443 |
if (session == NULL || session->x509_peer == NULL) { |
| 444 |
return NULL; |
| 445 |
} |
| 446 |
X509_up_ref(session->x509_peer); |
| 447 |
return session->x509_peer; |
| 448 |
} |
| 449 |
|
| 450 |
STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *ssl) { |
| 451 |
check_ssl_x509_method(ssl); |
| 452 |
if (ssl == nullptr) { |
| 453 |
return nullptr; |
| 454 |
} |
| 455 |
SSL_SESSION *session = SSL_get_session(ssl); |
| 456 |
if (session == nullptr) { |
| 457 |
return nullptr; |
| 458 |
} |
| 459 |
|
| 460 |
// OpenSSL historically didn't include the leaf certificate in the returned |
| 461 |
// certificate chain, but only for servers. |
| 462 |
return ssl->server ? session->x509_chain_without_leaf : session->x509_chain; |
| 463 |
} |
| 464 |
|
| 465 |
STACK_OF(X509) *SSL_get_peer_full_cert_chain(const SSL *ssl) { |
| 466 |
check_ssl_x509_method(ssl); |
| 467 |
SSL_SESSION *session = SSL_get_session(ssl); |
| 468 |
if (session == NULL) { |
| 469 |
return NULL; |
| 470 |
} |
| 471 |
|
| 472 |
return session->x509_chain; |
| 473 |
} |
| 474 |
|
| 475 |
STACK_OF(X509) *SSL_get0_verified_chain(const SSL *ssl) { |
| 476 |
check_ssl_x509_method(ssl); |
| 477 |
SSL_SESSION *session = SSL_get_session(ssl); |
| 478 |
if (session == NULL || SSL_get_verify_result(ssl) != X509_V_OK) { |
| 479 |
return NULL; |
| 480 |
} |
| 481 |
|
| 482 |
return session->x509_verified_chain; |
| 483 |
} |
| 484 |
|
| 485 |
int SSL_CTX_set_purpose(SSL_CTX *ctx, int purpose) { |
| 486 |
check_ssl_ctx_x509_method(ctx); |
| 487 |
return X509_VERIFY_PARAM_set_purpose(ctx->param, purpose); |
| 488 |
} |
| 489 |
|
| 490 |
int SSL_set_purpose(SSL *ssl, int purpose) { |
| 491 |
check_ssl_x509_method(ssl); |
| 492 |
if (!ssl->config) { |
| 493 |
return 0; |
| 494 |
} |
| 495 |
return X509_VERIFY_PARAM_set_purpose(ssl->config->param, purpose); |
| 496 |
} |
| 497 |
|
| 498 |
int SSL_CTX_set_trust(SSL_CTX *ctx, int trust) { |
| 499 |
check_ssl_ctx_x509_method(ctx); |
| 500 |
return X509_VERIFY_PARAM_set_trust(ctx->param, trust); |
| 501 |
} |
| 502 |
|
| 503 |
int SSL_set_trust(SSL *ssl, int trust) { |
| 504 |
check_ssl_x509_method(ssl); |
| 505 |
if (!ssl->config) { |
| 506 |
return 0; |
| 507 |
} |
| 508 |
return X509_VERIFY_PARAM_set_trust(ssl->config->param, trust); |
| 509 |
} |
| 510 |
|
| 511 |
int SSL_CTX_set1_param(SSL_CTX *ctx, const X509_VERIFY_PARAM *param) { |
| 512 |
check_ssl_ctx_x509_method(ctx); |
| 513 |
return X509_VERIFY_PARAM_set1(ctx->param, param); |
| 514 |
} |
| 515 |
|
| 516 |
int SSL_set1_param(SSL *ssl, const X509_VERIFY_PARAM *param) { |
| 517 |
check_ssl_x509_method(ssl); |
| 518 |
if (!ssl->config) { |
| 519 |
return 0; |
| 520 |
} |
| 521 |
return X509_VERIFY_PARAM_set1(ssl->config->param, param); |
| 522 |
} |
| 523 |
|
| 524 |
X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) { |
| 525 |
check_ssl_ctx_x509_method(ctx); |
| 526 |
return ctx->param; |
| 527 |
} |
| 528 |
|
| 529 |
X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) { |
| 530 |
check_ssl_x509_method(ssl); |
| 531 |
if (!ssl->config) { |
| 532 |
assert(ssl->config); |
| 533 |
return 0; |
| 534 |
} |
| 535 |
return ssl->config->param; |
| 536 |
} |
| 537 |
|
| 538 |
int SSL_get_verify_depth(const SSL *ssl) { |
| 539 |
check_ssl_x509_method(ssl); |
| 540 |
if (!ssl->config) { |
| 541 |
assert(ssl->config); |
| 542 |
return 0; |
| 543 |
} |
| 544 |
return X509_VERIFY_PARAM_get_depth(ssl->config->param); |
| 545 |
} |
| 546 |
|
| 547 |
int (*SSL_get_verify_callback(const SSL *ssl))(int, X509_STORE_CTX *) { |
| 548 |
check_ssl_x509_method(ssl); |
| 549 |
if (!ssl->config) { |
| 550 |
assert(ssl->config); |
| 551 |
return 0; |
| 552 |
} |
| 553 |
return ssl->config->verify_callback; |
| 554 |
} |
| 555 |
|
| 556 |
int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) { |
| 557 |
check_ssl_ctx_x509_method(ctx); |
| 558 |
return ctx->verify_mode; |
| 559 |
} |
| 560 |
|
| 561 |
int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) { |
| 562 |
check_ssl_ctx_x509_method(ctx); |
| 563 |
return X509_VERIFY_PARAM_get_depth(ctx->param); |
| 564 |
} |
| 565 |
|
| 566 |
int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))( |
| 567 |
int ok, X509_STORE_CTX *store_ctx) { |
| 568 |
check_ssl_ctx_x509_method(ctx); |
| 569 |
return ctx->default_verify_callback; |
| 570 |
} |
| 571 |
|
| 572 |
void SSL_set_verify(SSL *ssl, int mode, |
| 573 |
int (*callback)(int ok, X509_STORE_CTX *store_ctx)) { |
| 574 |
check_ssl_x509_method(ssl); |
| 575 |
if (!ssl->config) { |
| 576 |
return; |
| 577 |
} |
| 578 |
ssl->config->verify_mode = mode; |
| 579 |
if (callback != NULL) { |
| 580 |
ssl->config->verify_callback = callback; |
| 581 |
} |
| 582 |
} |
| 583 |
|
| 584 |
void SSL_set_verify_depth(SSL *ssl, int depth) { |
| 585 |
check_ssl_x509_method(ssl); |
| 586 |
if (!ssl->config) { |
| 587 |
return; |
| 588 |
} |
| 589 |
X509_VERIFY_PARAM_set_depth(ssl->config->param, depth); |
| 590 |
} |
| 591 |
|
| 592 |
void SSL_CTX_set_cert_verify_callback( |
| 593 |
SSL_CTX *ctx, int (*cb)(X509_STORE_CTX *store_ctx, void *arg), void *arg) { |
| 594 |
check_ssl_ctx_x509_method(ctx); |
| 595 |
ctx->app_verify_callback = cb; |
| 596 |
ctx->app_verify_arg = arg; |
| 597 |
} |
| 598 |
|
| 599 |
void SSL_CTX_set_verify(SSL_CTX *ctx, int mode, |
| 600 |
int (*cb)(int, X509_STORE_CTX *)) { |
| 601 |
check_ssl_ctx_x509_method(ctx); |
| 602 |
ctx->verify_mode = mode; |
| 603 |
ctx->default_verify_callback = cb; |
| 604 |
} |
| 605 |
|
| 606 |
void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) { |
| 607 |
check_ssl_ctx_x509_method(ctx); |
| 608 |
X509_VERIFY_PARAM_set_depth(ctx->param, depth); |
| 609 |
} |
| 610 |
|
| 611 |
int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) { |
| 612 |
check_ssl_ctx_x509_method(ctx); |
| 613 |
return X509_STORE_set_default_paths(ctx->cert_store); |
| 614 |
} |
| 615 |
|
| 616 |
int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *ca_file, |
| 617 |
const char *ca_dir) { |
| 618 |
check_ssl_ctx_x509_method(ctx); |
| 619 |
return X509_STORE_load_locations(ctx->cert_store, ca_file, ca_dir); |
| 620 |
} |
| 621 |
|
| 622 |
void SSL_set_verify_result(SSL *ssl, long arg) { |
| 623 |
check_ssl_x509_method(ssl); |
| 624 |
ssl->verify_result = arg; |
| 625 |
} |
| 626 |
|
| 627 |
long SSL_get_verify_result(const SSL *ssl) { |
| 628 |
check_ssl_x509_method(ssl); |
| 629 |
if (SSL_get_session(ssl) == nullptr) { |
| 630 |
return X509_V_ERR_INVALID_CALL; |
| 631 |
} |
| 632 |
return ssl->verify_result; |
| 633 |
} |
| 634 |
|
| 635 |
X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) { |
| 636 |
check_ssl_ctx_x509_method(ctx); |
| 637 |
return ctx->cert_store; |
| 638 |
} |
| 639 |
|
| 640 |
void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) { |
| 641 |
assert(ctx != nullptr); |
| 642 |
check_ssl_ctx_x509_method(ctx); |
| 643 |
X509_STORE_free(ctx->cert_store); |
| 644 |
ctx->cert_store = store; |
| 645 |
} |
| 646 |
|
| 647 |
void SSL_CTX_set1_cert_store(SSL_CTX *ctx, X509_STORE *store) { |
| 648 |
assert(ctx != nullptr); |
| 649 |
check_ssl_ctx_x509_method(ctx); |
| 650 |
if (store != nullptr) { |
| 651 |
X509_STORE_up_ref(store); |
| 652 |
} |
| 653 |
SSL_CTX_set_cert_store(ctx, store); |
| 654 |
} |
| 655 |
|
| 656 |
static int ssl_use_certificate(CERT *cert, X509 *x) { |
| 657 |
if (x == nullptr) { |
| 658 |
OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER); |
| 659 |
return 0; |
| 660 |
} |
| 661 |
|
| 662 |
if (!ssl_cert_check_cert_private_keys_usage(cert)) { |
| 663 |
return 0; |
| 664 |
} |
| 665 |
|
| 666 |
UniquePtr<CRYPTO_BUFFER> buffer = x509_to_buffer(x); |
| 667 |
if (!buffer || !ssl_set_cert(cert, std::move(buffer))) { |
| 668 |
return 0; |
| 669 |
} |
| 670 |
|
| 671 |
// We set the |x509_leaf| here to prevent any external data set from being |
| 672 |
// lost. The rest of the chain still uses |CRYPTO_BUFFER|s. |
| 673 |
X509 *&x509_leaf = |
| 674 |
cert->cert_private_keys[cert->cert_private_key_idx].x509_leaf; |
| 675 |
X509_free(x509_leaf); |
| 676 |
X509_up_ref(x); |
| 677 |
x509_leaf = x; |
| 678 |
return 1; |
| 679 |
} |
| 680 |
|
| 681 |
int SSL_use_certificate(SSL *ssl, X509 *x) { |
| 682 |
check_ssl_x509_method(ssl); |
| 683 |
if (!ssl->config) { |
| 684 |
return 0; |
| 685 |
} |
| 686 |
return ssl_use_certificate(ssl->config->cert.get(), x); |
| 687 |
} |
| 688 |
|
| 689 |
int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x) { |
| 690 |
check_ssl_ctx_x509_method(ctx); |
| 691 |
return ssl_use_certificate(ctx->cert.get(), x); |
| 692 |
} |
| 693 |
|
| 694 |
// ssl_cert_cache_leaf_cert sets |cert->x509_leaf|, if currently NULL, from the |
| 695 |
// first element of |cert->chain|. This is the case when certs are set with |
| 696 |
// |SSL_CTX_use_certificate_ASN1| or |SSL_use_certificate_ASN1| in AWS-LC. |
| 697 |
static int ssl_cert_cache_leaf_cert(CERT *cert) { |
| 698 |
assert(cert->x509_method); |
| 699 |
if (!ssl_cert_check_cert_private_keys_usage(cert)) { |
| 700 |
return 0; |
| 701 |
} |
| 702 |
|
| 703 |
X509 *&x509_leaf = |
| 704 |
cert->cert_private_keys[cert->cert_private_key_idx].x509_leaf; |
| 705 |
UniquePtr<STACK_OF(CRYPTO_BUFFER)> &chain = |
| 706 |
cert->cert_private_keys[cert->cert_private_key_idx].chain; |
| 707 |
|
| 708 |
if (x509_leaf != nullptr || chain == nullptr) { |
| 709 |
return 1; |
| 710 |
} |
| 711 |
|
| 712 |
CRYPTO_BUFFER *leaf = sk_CRYPTO_BUFFER_value(chain.get(), 0); |
| 713 |
if (!leaf) { |
| 714 |
return 1; |
| 715 |
} |
| 716 |
|
| 717 |
x509_leaf = X509_parse_from_buffer(leaf); |
| 718 |
return x509_leaf != nullptr; |
| 719 |
} |
| 720 |
|
| 721 |
static X509 *ssl_cert_get0_leaf(CERT *cert) { |
| 722 |
if (!ssl_cert_check_cert_private_keys_usage(cert)) { |
| 723 |
return nullptr; |
| 724 |
} |
| 725 |
|
| 726 |
X509 *&x509_leaf = |
| 727 |
cert->cert_private_keys[cert->cert_private_key_idx].x509_leaf; |
| 728 |
if (x509_leaf == nullptr && !ssl_cert_cache_leaf_cert(cert)) { |
| 729 |
return nullptr; |
| 730 |
} |
| 731 |
|
| 732 |
return x509_leaf; |
| 733 |
} |
| 734 |
|
| 735 |
X509 *SSL_get_certificate(const SSL *ssl) { |
| 736 |
check_ssl_x509_method(ssl); |
| 737 |
if (!ssl->config) { |
| 738 |
assert(ssl->config); |
| 739 |
return 0; |
| 740 |
} |
| 741 |
return ssl_cert_get0_leaf(ssl->config->cert.get()); |
| 742 |
} |
| 743 |
|
| 744 |
X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) { |
| 745 |
check_ssl_ctx_x509_method(ctx); |
| 746 |
MutexWriteLock lock(const_cast<CRYPTO_MUTEX *>(&ctx->lock)); |
| 747 |
return ssl_cert_get0_leaf(ctx->cert.get()); |
| 748 |
} |
| 749 |
|
| 750 |
static int ssl_cert_set0_chain(CERT *cert, STACK_OF(X509) *chain) { |
| 751 |
if (!ssl_cert_set_chain(cert, chain)) { |
| 752 |
return 0; |
| 753 |
} |
| 754 |
|
| 755 |
sk_X509_pop_free(chain, X509_free); |
| 756 |
ssl_crypto_x509_cert_flush_cached_chain(cert); |
| 757 |
return 1; |
| 758 |
} |
| 759 |
|
| 760 |
static int ssl_cert_set1_chain(CERT *cert, STACK_OF(X509) *chain) { |
| 761 |
if (!ssl_cert_set_chain(cert, chain)) { |
| 762 |
return 0; |
| 763 |
} |
| 764 |
|
| 765 |
ssl_crypto_x509_cert_flush_cached_chain(cert); |
| 766 |
return 1; |
| 767 |
} |
| 768 |
|
| 769 |
static int ssl_cert_append_cert(CERT *cert, X509 *x509) { |
| 770 |
assert(cert->x509_method); |
| 771 |
if (!ssl_cert_check_cert_private_keys_usage(cert)) { |
| 772 |
return 0; |
| 773 |
} |
| 774 |
|
| 775 |
UniquePtr<CRYPTO_BUFFER> buffer = x509_to_buffer(x509); |
| 776 |
if (!buffer) { |
| 777 |
return 0; |
| 778 |
} |
| 779 |
|
| 780 |
UniquePtr<STACK_OF(CRYPTO_BUFFER)> &chain = |
| 781 |
cert->cert_private_keys[cert->cert_private_key_idx].chain; |
| 782 |
if (chain != nullptr) { |
| 783 |
return PushToStack(chain.get(), std::move(buffer)); |
| 784 |
} |
| 785 |
|
| 786 |
chain = new_leafless_chain(); |
| 787 |
if (!chain || !PushToStack(chain.get(), std::move(buffer))) { |
| 788 |
chain.reset(); |
| 789 |
return 0; |
| 790 |
} |
| 791 |
|
| 792 |
return 1; |
| 793 |
} |
| 794 |
|
| 795 |
static int ssl_cert_add0_chain_cert(CERT *cert, X509 *x509) { |
| 796 |
if (!ssl_cert_append_cert(cert, x509)) { |
| 797 |
return 0; |
| 798 |
} |
| 799 |
|
| 800 |
X509_free(cert->x509_stash); |
| 801 |
cert->x509_stash = x509; |
| 802 |
ssl_crypto_x509_cert_flush_cached_chain(cert); |
| 803 |
return 1; |
| 804 |
} |
| 805 |
|
| 806 |
static int ssl_cert_add1_chain_cert(CERT *cert, X509 *x509) { |
| 807 |
if (!ssl_cert_append_cert(cert, x509)) { |
| 808 |
return 0; |
| 809 |
} |
| 810 |
|
| 811 |
ssl_crypto_x509_cert_flush_cached_chain(cert); |
| 812 |
return 1; |
| 813 |
} |
| 814 |
|
| 815 |
int SSL_CTX_set0_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) { |
| 816 |
check_ssl_ctx_x509_method(ctx); |
| 817 |
return ssl_cert_set0_chain(ctx->cert.get(), chain); |
| 818 |
} |
| 819 |
|
| 820 |
int SSL_CTX_set1_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) { |
| 821 |
check_ssl_ctx_x509_method(ctx); |
| 822 |
return ssl_cert_set1_chain(ctx->cert.get(), chain); |
| 823 |
} |
| 824 |
|
| 825 |
int SSL_set0_chain(SSL *ssl, STACK_OF(X509) *chain) { |
| 826 |
check_ssl_x509_method(ssl); |
| 827 |
if (!ssl->config) { |
| 828 |
return 0; |
| 829 |
} |
| 830 |
return ssl_cert_set0_chain(ssl->config->cert.get(), chain); |
| 831 |
} |
| 832 |
|
| 833 |
int SSL_set1_chain(SSL *ssl, STACK_OF(X509) *chain) { |
| 834 |
check_ssl_x509_method(ssl); |
| 835 |
if (!ssl->config) { |
| 836 |
return 0; |
| 837 |
} |
| 838 |
return ssl_cert_set1_chain(ssl->config->cert.get(), chain); |
| 839 |
} |
| 840 |
|
| 841 |
int SSL_CTX_add0_chain_cert(SSL_CTX *ctx, X509 *x509) { |
| 842 |
check_ssl_ctx_x509_method(ctx); |
| 843 |
return ssl_cert_add0_chain_cert(ctx->cert.get(), x509); |
| 844 |
} |
| 845 |
|
| 846 |
int SSL_CTX_add1_chain_cert(SSL_CTX *ctx, X509 *x509) { |
| 847 |
check_ssl_ctx_x509_method(ctx); |
| 848 |
return ssl_cert_add1_chain_cert(ctx->cert.get(), x509); |
| 849 |
} |
| 850 |
|
| 851 |
int SSL_CTX_add_extra_chain_cert(SSL_CTX *ctx, X509 *x509) { |
| 852 |
check_ssl_ctx_x509_method(ctx); |
| 853 |
return SSL_CTX_add0_chain_cert(ctx, x509); |
| 854 |
} |
| 855 |
|
| 856 |
int SSL_add0_chain_cert(SSL *ssl, X509 *x509) { |
| 857 |
check_ssl_x509_method(ssl); |
| 858 |
if (!ssl->config) { |
| 859 |
return 0; |
| 860 |
} |
| 861 |
return ssl_cert_add0_chain_cert(ssl->config->cert.get(), x509); |
| 862 |
} |
| 863 |
|
| 864 |
int SSL_add1_chain_cert(SSL *ssl, X509 *x509) { |
| 865 |
check_ssl_x509_method(ssl); |
| 866 |
if (!ssl->config) { |
| 867 |
return 0; |
| 868 |
} |
| 869 |
return ssl_cert_add1_chain_cert(ssl->config->cert.get(), x509); |
| 870 |
} |
| 871 |
|
| 872 |
#define is_flag_set(flags, query) (flags & query) |
| 873 |
|
| 874 |
static int ssl_build_cert_chain(CERT *cert, X509_STORE *cert_store, int flags) { |
| 875 |
assert(cert_store); |
| 876 |
if (!ssl_cert_check_cert_private_keys_usage(cert)) { |
| 877 |
return 0; |
| 878 |
} |
| 879 |
|
| 880 |
CERT_PKEY *cert_pkey = &cert->cert_private_keys[cert->cert_private_key_idx]; |
| 881 |
CRYPTO_BUFFER *leaf_buffer = |
| 882 |
sk_CRYPTO_BUFFER_value(cert_pkey->chain.get(), 0); |
| 883 |
if (leaf_buffer == nullptr) { |
| 884 |
OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET); |
| 885 |
return 0; |
| 886 |
} |
| 887 |
UniquePtr<X509> leaf(X509_parse_from_buffer(leaf_buffer)); |
| 888 |
if (leaf == nullptr) { |
| 889 |
OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB); |
| 890 |
return 0; |
| 891 |
} |
| 892 |
|
| 893 |
UniquePtr<X509_STORE> store(X509_STORE_new()); |
| 894 |
UniquePtr<X509_STORE_CTX> store_ctx(X509_STORE_CTX_new()); |
| 895 |
if (store == nullptr || store_ctx == nullptr) { |
| 896 |
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 897 |
return 0; |
| 898 |
} |
| 899 |
|
| 900 |
// Rearranging and check the chain: add everything to a store |
| 901 |
UniquePtr<STACK_OF(X509)> untrusted(sk_X509_new_null()); |
| 902 |
if (is_flag_set(flags, SSL_BUILD_CHAIN_FLAG_CHECK)) { |
| 903 |
// Push certs onto |X509_STORE|. |
| 904 |
for (size_t i = 1; i < sk_CRYPTO_BUFFER_num(cert_pkey->chain.get()); i++) { |
| 905 |
CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(cert_pkey->chain.get(), i); |
| 906 |
UniquePtr<X509> x509(X509_parse_from_buffer(buffer)); |
| 907 |
if (!x509 || !X509_STORE_add_cert(store.get(), x509.get())) { |
| 908 |
return 0; |
| 909 |
} |
| 910 |
} |
| 911 |
// Add end-entity certificate too: it might be self-signed. |
| 912 |
if (!X509_STORE_add_cert(store.get(), leaf.get())) { |
| 913 |
return 0; |
| 914 |
} |
| 915 |
} else { |
| 916 |
// Use associated |cert_store| from |SSL_CTX|. Reference count added to |
| 917 |
// avoid double freeing of |X509_STORE|. |
| 918 |
store.reset(cert_store); |
| 919 |
X509_STORE_up_ref(cert_store); |
| 920 |
|
| 921 |
if (is_flag_set(flags, SSL_BUILD_CHAIN_FLAG_UNTRUSTED)) { |
| 922 |
// Push certs onto untrusted stack. |
| 923 |
for (size_t i = 1; i < sk_CRYPTO_BUFFER_num(cert_pkey->chain.get()); |
| 924 |
i++) { |
| 925 |
CRYPTO_BUFFER *buffer = |
| 926 |
sk_CRYPTO_BUFFER_value(cert_pkey->chain.get(), i); |
| 927 |
UniquePtr<X509> x509(X509_parse_from_buffer(buffer)); |
| 928 |
if (!x509 || !PushToStack(untrusted.get(), std::move(x509))) { |
| 929 |
return 0; |
| 930 |
} |
| 931 |
} |
| 932 |
} |
| 933 |
} |
| 934 |
|
| 935 |
if (!X509_STORE_CTX_init(store_ctx.get(), store.get(), leaf.get(), |
| 936 |
untrusted.get())) { |
| 937 |
OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB); |
| 938 |
return 0; |
| 939 |
} |
| 940 |
|
| 941 |
bool ignore_error = false; |
| 942 |
if (X509_verify_cert(store_ctx.get()) <= 0) { |
| 943 |
OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_VERIFY_FAILED); |
| 944 |
ERR_add_error_data(2, "Verify error:", |
| 945 |
X509_verify_cert_error_string( |
| 946 |
X509_STORE_CTX_get_error(store_ctx.get()))); |
| 947 |
|
| 948 |
// Fail if |SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR| is not set. |
| 949 |
if (!is_flag_set(flags, SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR)) { |
| 950 |
return 0; |
| 951 |
} |
| 952 |
|
| 953 |
if (is_flag_set(flags, SSL_BUILD_CHAIN_FLAG_CLEAR_ERROR)) { |
| 954 |
ERR_clear_error(); |
| 955 |
} |
| 956 |
ignore_error = true; |
| 957 |
} |
| 958 |
|
| 959 |
UniquePtr<STACK_OF(X509)> built_chain( |
| 960 |
X509_STORE_CTX_get1_chain(store_ctx.get())); |
| 961 |
// Remove EE certificate from chain. |
| 962 |
X509_free(sk_X509_shift(built_chain.get())); |
| 963 |
|
| 964 |
if (is_flag_set(flags, SSL_BUILD_CHAIN_FLAG_NO_ROOT)) { |
| 965 |
if (sk_X509_num(built_chain.get()) > 0) { |
| 966 |
// See if last cert is self-signed. |
| 967 |
if (X509_get_extension_flags(sk_X509_value( |
| 968 |
built_chain.get(), sk_X509_num(built_chain.get()) - 1)) & |
| 969 |
EXFLAG_SS) { |
| 970 |
X509_free(sk_X509_pop(built_chain.get())); |
| 971 |
} |
| 972 |
} |
| 973 |
} |
| 974 |
if (!ssl_cert_set_chain(cert, built_chain.get())) { |
| 975 |
return 0; |
| 976 |
} |
| 977 |
|
| 978 |
// Anything that has passed successfully up to here is valid. |
| 979 |
// 2 is used to indicate a verification error has happened, but was ignored |
| 980 |
// because |SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR| was set. |
| 981 |
if (ignore_error) { |
| 982 |
return 2; |
| 983 |
} |
| 984 |
return 1; |
| 985 |
} |
| 986 |
|
| 987 |
int SSL_CTX_build_cert_chain(SSL_CTX *ctx, int flags) { |
| 988 |
check_ssl_ctx_x509_method(ctx); |
| 989 |
return ssl_build_cert_chain(ctx->cert.get(), ctx->cert_store, flags); |
| 990 |
} |
| 991 |
|
| 992 |
int SSL_build_cert_chain(SSL *ssl, int flags) { |
| 993 |
check_ssl_x509_method(ssl); |
| 994 |
if (!ssl->config) { |
| 995 |
return 0; |
| 996 |
} |
| 997 |
return ssl_build_cert_chain(ssl->config->cert.get(), ssl->ctx->cert_store, |
| 998 |
flags); |
| 999 |
} |
| 1000 |
|
| 1001 |
int SSL_CTX_clear_chain_certs(SSL_CTX *ctx) { |
| 1002 |
check_ssl_ctx_x509_method(ctx); |
| 1003 |
return SSL_CTX_set0_chain(ctx, NULL); |
| 1004 |
} |
| 1005 |
|
| 1006 |
int SSL_CTX_clear_extra_chain_certs(SSL_CTX *ctx) { |
| 1007 |
check_ssl_ctx_x509_method(ctx); |
| 1008 |
return SSL_CTX_clear_chain_certs(ctx); |
| 1009 |
} |
| 1010 |
|
| 1011 |
int SSL_clear_chain_certs(SSL *ssl) { |
| 1012 |
check_ssl_x509_method(ssl); |
| 1013 |
return SSL_set0_chain(ssl, NULL); |
| 1014 |
} |
| 1015 |
|
| 1016 |
// ssl_cert_cache_chain_certs fills in |cert->x509_chain| from elements 1.. of |
| 1017 |
// |cert->chain|. |
| 1018 |
static int ssl_cert_cache_chain_certs(CERT *cert) { |
| 1019 |
assert(cert->x509_method); |
| 1020 |
if (!ssl_cert_check_cert_private_keys_usage(cert)) { |
| 1021 |
return 0; |
| 1022 |
} |
| 1023 |
|
| 1024 |
STACK_OF(X509) *&x509_chain = |
| 1025 |
cert->cert_private_keys[cert->cert_private_key_idx].x509_chain; |
| 1026 |
UniquePtr<STACK_OF(CRYPTO_BUFFER)> &chain = |
| 1027 |
cert->cert_private_keys[cert->cert_private_key_idx].chain; |
| 1028 |
|
| 1029 |
if (x509_chain != nullptr || chain == nullptr || |
| 1030 |
sk_CRYPTO_BUFFER_num(chain.get()) < 2) { |
| 1031 |
return 1; |
| 1032 |
} |
| 1033 |
|
| 1034 |
UniquePtr<STACK_OF(X509)> new_x509_chain(sk_X509_new_null()); |
| 1035 |
if (!new_x509_chain) { |
| 1036 |
return 0; |
| 1037 |
} |
| 1038 |
|
| 1039 |
for (size_t i = 1; i < sk_CRYPTO_BUFFER_num(chain.get()); i++) { |
| 1040 |
CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(chain.get(), i); |
| 1041 |
UniquePtr<X509> x509(X509_parse_from_buffer(buffer)); |
| 1042 |
if (!x509 || !PushToStack(new_x509_chain.get(), std::move(x509))) { |
| 1043 |
return 0; |
| 1044 |
} |
| 1045 |
} |
| 1046 |
|
| 1047 |
x509_chain = new_x509_chain.release(); |
| 1048 |
return 1; |
| 1049 |
} |
| 1050 |
|
| 1051 |
int SSL_CTX_get0_chain_certs(const SSL_CTX *ctx, STACK_OF(X509) **out_chain) { |
| 1052 |
check_ssl_ctx_x509_method(ctx); |
| 1053 |
MutexWriteLock lock(const_cast<CRYPTO_MUTEX *>(&ctx->lock)); |
| 1054 |
if (!ssl_cert_cache_chain_certs(ctx->cert.get())) { |
| 1055 |
*out_chain = NULL; |
| 1056 |
return 0; |
| 1057 |
} |
| 1058 |
|
| 1059 |
// |cert_private_keys| already checked above in |ssl_cert_cache_chain_certs|. |
| 1060 |
*out_chain = |
| 1061 |
ctx->cert->cert_private_keys[ctx->cert->cert_private_key_idx].x509_chain; |
| 1062 |
return 1; |
| 1063 |
} |
| 1064 |
|
| 1065 |
int SSL_CTX_get_extra_chain_certs(const SSL_CTX *ctx, |
| 1066 |
STACK_OF(X509) **out_chain) { |
| 1067 |
return SSL_CTX_get0_chain_certs(ctx, out_chain); |
| 1068 |
} |
| 1069 |
|
| 1070 |
int SSL_get0_chain_certs(const SSL *ssl, STACK_OF(X509) **out_chain) { |
| 1071 |
check_ssl_x509_method(ssl); |
| 1072 |
if (!ssl->config) { |
| 1073 |
assert(ssl->config); |
| 1074 |
return 0; |
| 1075 |
} |
| 1076 |
if (!ssl_cert_cache_chain_certs(ssl->config->cert.get())) { |
| 1077 |
*out_chain = NULL; |
| 1078 |
return 0; |
| 1079 |
} |
| 1080 |
|
| 1081 |
// |cert_private_keys| already checked above in |ssl_cert_cache_chain_certs|. |
| 1082 |
*out_chain = ssl->config->cert |
| 1083 |
->cert_private_keys[ssl->config->cert->cert_private_key_idx] |
| 1084 |
.x509_chain; |
| 1085 |
return 1; |
| 1086 |
} |
| 1087 |
|
| 1088 |
SSL_SESSION *d2i_SSL_SESSION_bio(BIO *bio, SSL_SESSION **out) { |
| 1089 |
uint8_t *data; |
| 1090 |
size_t len; |
| 1091 |
if (!BIO_read_asn1(bio, &data, &len, 1024 * 1024)) { |
| 1092 |
return 0; |
| 1093 |
} |
| 1094 |
bssl::UniquePtr<uint8_t> free_data(data); |
| 1095 |
const uint8_t *ptr = data; |
| 1096 |
return d2i_SSL_SESSION(out, &ptr, static_cast<long>(len)); |
| 1097 |
} |
| 1098 |
|
| 1099 |
int i2d_SSL_SESSION_bio(BIO *bio, const SSL_SESSION *session) { |
| 1100 |
uint8_t *data; |
| 1101 |
size_t len; |
| 1102 |
if (!SSL_SESSION_to_bytes(session, &data, &len)) { |
| 1103 |
return 0; |
| 1104 |
} |
| 1105 |
bssl::UniquePtr<uint8_t> free_data(data); |
| 1106 |
return BIO_write_all(bio, data, len); |
| 1107 |
} |
| 1108 |
|
| 1109 |
IMPLEMENT_PEM_rw(SSL_SESSION, SSL_SESSION, PEM_STRING_SSL_SESSION, SSL_SESSION) |
| 1110 |
|
| 1111 |
SSL_SESSION *d2i_SSL_SESSION(SSL_SESSION **a, const uint8_t **pp, long length) { |
| 1112 |
if (length < 0) { |
| 1113 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1114 |
return NULL; |
| 1115 |
} |
| 1116 |
|
| 1117 |
CBS cbs; |
| 1118 |
CBS_init(&cbs, *pp, length); |
| 1119 |
|
| 1120 |
UniquePtr<SSL_SESSION> ret = SSL_SESSION_parse(&cbs, &ssl_crypto_x509_method, |
| 1121 |
NULL /* no buffer pool */); |
| 1122 |
if (!ret) { |
| 1123 |
return NULL; |
| 1124 |
} |
| 1125 |
|
| 1126 |
if (a) { |
| 1127 |
SSL_SESSION_free(*a); |
| 1128 |
*a = ret.get(); |
| 1129 |
} |
| 1130 |
*pp = CBS_data(&cbs); |
| 1131 |
return ret.release(); |
| 1132 |
} |
| 1133 |
|
| 1134 |
STACK_OF(X509_NAME) *SSL_dup_CA_list(STACK_OF(X509_NAME) *list) { |
| 1135 |
// TODO(https://crbug.com/boringssl/407): |X509_NAME_dup| should be const. |
| 1136 |
auto name_dup = [](const X509_NAME *name) { |
| 1137 |
return X509_NAME_dup(const_cast<X509_NAME *>(name)); |
| 1138 |
}; |
| 1139 |
return sk_X509_NAME_deep_copy(list, name_dup, X509_NAME_free); |
| 1140 |
} |
| 1141 |
|
| 1142 |
static void set_client_CA_list(UniquePtr<STACK_OF(CRYPTO_BUFFER)> *ca_list, |
| 1143 |
const STACK_OF(X509_NAME) *name_list, |
| 1144 |
CRYPTO_BUFFER_POOL *pool) { |
| 1145 |
UniquePtr<STACK_OF(CRYPTO_BUFFER)> buffers(sk_CRYPTO_BUFFER_new_null()); |
| 1146 |
if (!buffers) { |
| 1147 |
return; |
| 1148 |
} |
| 1149 |
|
| 1150 |
for (X509_NAME *name : name_list) { |
| 1151 |
uint8_t *outp = NULL; |
| 1152 |
int len = i2d_X509_NAME(name, &outp); |
| 1153 |
if (len < 0) { |
| 1154 |
return; |
| 1155 |
} |
| 1156 |
|
| 1157 |
UniquePtr<CRYPTO_BUFFER> buffer(CRYPTO_BUFFER_new(outp, len, pool)); |
| 1158 |
OPENSSL_free(outp); |
| 1159 |
if (!buffer || !PushToStack(buffers.get(), std::move(buffer))) { |
| 1160 |
return; |
| 1161 |
} |
| 1162 |
} |
| 1163 |
|
| 1164 |
*ca_list = std::move(buffers); |
| 1165 |
} |
| 1166 |
|
| 1167 |
void SSL_set_client_CA_list(SSL *ssl, STACK_OF(X509_NAME) *name_list) { |
| 1168 |
check_ssl_x509_method(ssl); |
| 1169 |
if (!ssl->config) { |
| 1170 |
return; |
| 1171 |
} |
| 1172 |
ssl->ctx->x509_method->ssl_flush_cached_client_CA(ssl->config.get()); |
| 1173 |
set_client_CA_list(&ssl->config->client_CA, name_list, ssl->ctx->pool); |
| 1174 |
sk_X509_NAME_pop_free(name_list, X509_NAME_free); |
| 1175 |
} |
| 1176 |
|
| 1177 |
void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list) { |
| 1178 |
check_ssl_ctx_x509_method(ctx); |
| 1179 |
ctx->x509_method->ssl_ctx_flush_cached_client_CA(ctx); |
| 1180 |
set_client_CA_list(&ctx->client_CA, name_list, ctx->pool); |
| 1181 |
sk_X509_NAME_pop_free(name_list, X509_NAME_free); |
| 1182 |
} |
| 1183 |
|
| 1184 |
static STACK_OF(X509_NAME) *buffer_names_to_x509( |
| 1185 |
const STACK_OF(CRYPTO_BUFFER) *names, STACK_OF(X509_NAME) **cached) { |
| 1186 |
if (names == NULL) { |
| 1187 |
return NULL; |
| 1188 |
} |
| 1189 |
|
| 1190 |
if (*cached != NULL) { |
| 1191 |
return *cached; |
| 1192 |
} |
| 1193 |
|
| 1194 |
UniquePtr<STACK_OF(X509_NAME)> new_cache(sk_X509_NAME_new_null()); |
| 1195 |
if (!new_cache) { |
| 1196 |
return NULL; |
| 1197 |
} |
| 1198 |
|
| 1199 |
for (const CRYPTO_BUFFER *buffer : names) { |
| 1200 |
const uint8_t *inp = CRYPTO_BUFFER_data(buffer); |
| 1201 |
UniquePtr<X509_NAME> name( |
| 1202 |
d2i_X509_NAME(nullptr, &inp, CRYPTO_BUFFER_len(buffer))); |
| 1203 |
if (!name || |
| 1204 |
inp != CRYPTO_BUFFER_data(buffer) + CRYPTO_BUFFER_len(buffer) || |
| 1205 |
!PushToStack(new_cache.get(), std::move(name))) { |
| 1206 |
return NULL; |
| 1207 |
} |
| 1208 |
} |
| 1209 |
|
| 1210 |
*cached = new_cache.release(); |
| 1211 |
return *cached; |
| 1212 |
} |
| 1213 |
|
| 1214 |
bool ssl_x509_persist_peer_ca_names(SSL *ssl) { |
| 1215 |
if (!ssl->s3->hs || !ssl->s3->hs->ca_names) { |
| 1216 |
return true; |
| 1217 |
} |
| 1218 |
// Eagerly populate the X509_NAME cache on |s3| from the raw CRYPTO_BUFFERs |
| 1219 |
// so that the names survive after |hs| is destroyed. |
| 1220 |
return buffer_names_to_x509(ssl->s3->hs->ca_names.get(), |
| 1221 |
&ssl->s3->cached_x509_peer_ca_names) != NULL; |
| 1222 |
} |
| 1223 |
|
| 1224 |
STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *ssl) { |
| 1225 |
check_ssl_x509_method(ssl); |
| 1226 |
// For historical reasons, this function is used both to query configuration |
| 1227 |
// state on a server as well as handshake state on a client. However, whether |
| 1228 |
// |ssl| is a client or server is not known until explicitly configured with |
| 1229 |
// |SSL_set_connect_state|. If |do_handshake| is NULL, |ssl| is in an |
| 1230 |
// indeterminate mode and |ssl->server| is unset. |
| 1231 |
if (ssl->do_handshake != NULL && !ssl->server) { |
| 1232 |
if (ssl->s3->hs != NULL) { |
| 1233 |
return buffer_names_to_x509( |
| 1234 |
ssl->s3->hs->ca_names.get(), |
| 1235 |
&ssl->s3->cached_x509_peer_ca_names); |
| 1236 |
} |
| 1237 |
// After the handshake completes, |hs| is destroyed. The cached |
| 1238 |
// X509_NAMEs were eagerly populated before |hs| was destroyed. |
| 1239 |
return ssl->s3->cached_x509_peer_ca_names; |
| 1240 |
} |
| 1241 |
|
| 1242 |
if (!ssl->config) { |
| 1243 |
assert(ssl->config); |
| 1244 |
return NULL; |
| 1245 |
} |
| 1246 |
|
| 1247 |
if (ssl->config->client_CA != NULL) { |
| 1248 |
return buffer_names_to_x509( |
| 1249 |
ssl->config->client_CA.get(), |
| 1250 |
(STACK_OF(X509_NAME) **)&ssl->config->cached_x509_client_CA); |
| 1251 |
} |
| 1252 |
return SSL_CTX_get_client_CA_list(ssl->ctx.get()); |
| 1253 |
} |
| 1254 |
|
| 1255 |
STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx) { |
| 1256 |
check_ssl_ctx_x509_method(ctx); |
| 1257 |
// This is a logically const operation that may be called on multiple threads, |
| 1258 |
// so it needs to lock around updating |cached_x509_client_CA|. |
| 1259 |
MutexWriteLock lock(const_cast<CRYPTO_MUTEX *>(&ctx->lock)); |
| 1260 |
return buffer_names_to_x509( |
| 1261 |
ctx->client_CA.get(), |
| 1262 |
const_cast<STACK_OF(X509_NAME) **>(&ctx->cached_x509_client_CA)); |
| 1263 |
} |
| 1264 |
|
| 1265 |
static int add_client_CA(UniquePtr<STACK_OF(CRYPTO_BUFFER)> *names, X509 *x509, |
| 1266 |
CRYPTO_BUFFER_POOL *pool) { |
| 1267 |
if (x509 == NULL) { |
| 1268 |
return 0; |
| 1269 |
} |
| 1270 |
|
| 1271 |
uint8_t *outp = NULL; |
| 1272 |
int len = i2d_X509_NAME(X509_get_subject_name(x509), &outp); |
| 1273 |
if (len < 0) { |
| 1274 |
return 0; |
| 1275 |
} |
| 1276 |
|
| 1277 |
UniquePtr<CRYPTO_BUFFER> buffer(CRYPTO_BUFFER_new(outp, len, pool)); |
| 1278 |
OPENSSL_free(outp); |
| 1279 |
if (!buffer) { |
| 1280 |
return 0; |
| 1281 |
} |
| 1282 |
|
| 1283 |
int alloced = 0; |
| 1284 |
if (*names == nullptr) { |
| 1285 |
names->reset(sk_CRYPTO_BUFFER_new_null()); |
| 1286 |
alloced = 1; |
| 1287 |
|
| 1288 |
if (*names == NULL) { |
| 1289 |
return 0; |
| 1290 |
} |
| 1291 |
} |
| 1292 |
|
| 1293 |
if (!PushToStack(names->get(), std::move(buffer))) { |
| 1294 |
if (alloced) { |
| 1295 |
names->reset(); |
| 1296 |
} |
| 1297 |
return 0; |
| 1298 |
} |
| 1299 |
|
| 1300 |
return 1; |
| 1301 |
} |
| 1302 |
|
| 1303 |
int SSL_add_client_CA(SSL *ssl, X509 *x509) { |
| 1304 |
check_ssl_x509_method(ssl); |
| 1305 |
if (!ssl->config) { |
| 1306 |
return 0; |
| 1307 |
} |
| 1308 |
if (!add_client_CA(&ssl->config->client_CA, x509, ssl->ctx->pool)) { |
| 1309 |
return 0; |
| 1310 |
} |
| 1311 |
|
| 1312 |
ssl_crypto_x509_ssl_flush_cached_client_CA(ssl->config.get()); |
| 1313 |
return 1; |
| 1314 |
} |
| 1315 |
|
| 1316 |
int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x509) { |
| 1317 |
check_ssl_ctx_x509_method(ctx); |
| 1318 |
if (!add_client_CA(&ctx->client_CA, x509, ctx->pool)) { |
| 1319 |
return 0; |
| 1320 |
} |
| 1321 |
|
| 1322 |
ssl_crypto_x509_ssl_ctx_flush_cached_client_CA(ctx); |
| 1323 |
return 1; |
| 1324 |
} |
| 1325 |
|
| 1326 |
static int do_client_cert_cb(SSL *ssl, void *arg) { |
| 1327 |
// Should only be called during handshake, but check to be sure. |
| 1328 |
if (!ssl->config) { |
| 1329 |
assert(ssl->config); |
| 1330 |
return -1; |
| 1331 |
} |
| 1332 |
|
| 1333 |
if (ssl_has_certificate(ssl->s3->hs.get()) || |
| 1334 |
ssl->ctx->client_cert_cb == NULL) { |
| 1335 |
return 1; |
| 1336 |
} |
| 1337 |
|
| 1338 |
X509 *x509 = NULL; |
| 1339 |
EVP_PKEY *pkey = NULL; |
| 1340 |
int ret = ssl->ctx->client_cert_cb(ssl, &x509, &pkey); |
| 1341 |
if (ret < 0) { |
| 1342 |
return -1; |
| 1343 |
} |
| 1344 |
UniquePtr<X509> free_x509(x509); |
| 1345 |
UniquePtr<EVP_PKEY> free_pkey(pkey); |
| 1346 |
|
| 1347 |
if (ret != 0) { |
| 1348 |
if (!SSL_use_certificate(ssl, x509) || !SSL_use_PrivateKey(ssl, pkey)) { |
| 1349 |
return 0; |
| 1350 |
} |
| 1351 |
} |
| 1352 |
|
| 1353 |
return 1; |
| 1354 |
} |
| 1355 |
|
| 1356 |
void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx, |
| 1357 |
int (*cb)(SSL *ssl, X509 **out_x509, |
| 1358 |
EVP_PKEY **out_pkey)) { |
| 1359 |
check_ssl_ctx_x509_method(ctx); |
| 1360 |
// Emulate the old client certificate callback with the new one. |
| 1361 |
SSL_CTX_set_cert_cb(ctx, do_client_cert_cb, NULL); |
| 1362 |
ctx->client_cert_cb = cb; |
| 1363 |
} |
| 1364 |
|
| 1365 |
static int set_cert_store(X509_STORE **store_ptr, X509_STORE *new_store, |
| 1366 |
int take_ref) { |
| 1367 |
X509_STORE_free(*store_ptr); |
| 1368 |
*store_ptr = new_store; |
| 1369 |
|
| 1370 |
if (new_store != NULL && take_ref) { |
| 1371 |
X509_STORE_up_ref(new_store); |
| 1372 |
} |
| 1373 |
|
| 1374 |
return 1; |
| 1375 |
} |
| 1376 |
|
| 1377 |
int SSL_get_ex_data_X509_STORE_CTX_idx(void) { |
| 1378 |
// The ex_data index to go from |X509_STORE_CTX| to |SSL| always uses the |
| 1379 |
// reserved app_data slot. Before ex_data was introduced, app_data was used. |
| 1380 |
// Avoid breaking any software which assumes |X509_STORE_CTX_get_app_data| |
| 1381 |
// works. |
| 1382 |
return 0; |
| 1383 |
} |
| 1384 |
|
| 1385 |
int SSL_CTX_set0_verify_cert_store(SSL_CTX *ctx, X509_STORE *store) { |
| 1386 |
check_ssl_ctx_x509_method(ctx); |
| 1387 |
return set_cert_store(&ctx->cert->verify_store, store, 0); |
| 1388 |
} |
| 1389 |
|
| 1390 |
int SSL_CTX_set1_verify_cert_store(SSL_CTX *ctx, X509_STORE *store) { |
| 1391 |
check_ssl_ctx_x509_method(ctx); |
| 1392 |
return set_cert_store(&ctx->cert->verify_store, store, 1); |
| 1393 |
} |
| 1394 |
|
| 1395 |
int SSL_set0_verify_cert_store(SSL *ssl, X509_STORE *store) { |
| 1396 |
check_ssl_x509_method(ssl); |
| 1397 |
if (!ssl->config) { |
| 1398 |
return 0; |
| 1399 |
} |
| 1400 |
return set_cert_store(&ssl->config->cert->verify_store, store, 0); |
| 1401 |
} |
| 1402 |
|
| 1403 |
int SSL_set1_verify_cert_store(SSL *ssl, X509_STORE *store) { |
| 1404 |
check_ssl_x509_method(ssl); |
| 1405 |
if (!ssl->config) { |
| 1406 |
return 0; |
| 1407 |
} |
| 1408 |
return set_cert_store(&ssl->config->cert->verify_store, store, 1); |
| 1409 |
} |
| 1410 |
|
| 1411 |
int SSL_set1_host(SSL *ssl, const char *hostname) { |
| 1412 |
check_ssl_x509_method(ssl); |
| 1413 |
if (!ssl->config) { |
| 1414 |
return 0; |
| 1415 |
} |
| 1416 |
return X509_VERIFY_PARAM_set1_host(ssl->config->param, hostname, |
| 1417 |
strlen(hostname)); |
| 1418 |
} |
| 1419 |
|
| 1420 |
void SSL_set_hostflags(SSL *ssl, unsigned flags) { |
| 1421 |
check_ssl_x509_method(ssl); |
| 1422 |
if (!ssl->config) { |
| 1423 |
return; |
| 1424 |
} |
| 1425 |
X509_VERIFY_PARAM_set_hostflags(ssl->config->param, flags); |
| 1426 |
} |
| 1427 |
|
| 1428 |
int SSL_alert_from_verify_result(long result) { |
| 1429 |
switch (result) { |
| 1430 |
case X509_V_ERR_CERT_CHAIN_TOO_LONG: |
| 1431 |
case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT: |
| 1432 |
case X509_V_ERR_INVALID_CA: |
| 1433 |
case X509_V_ERR_PATH_LENGTH_EXCEEDED: |
| 1434 |
case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN: |
| 1435 |
case X509_V_ERR_UNABLE_TO_GET_CRL: |
| 1436 |
case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER: |
| 1437 |
case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT: |
| 1438 |
case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY: |
| 1439 |
case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE: |
| 1440 |
return SSL_AD_UNKNOWN_CA; |
| 1441 |
|
| 1442 |
case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE: |
| 1443 |
case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE: |
| 1444 |
case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY: |
| 1445 |
case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD: |
| 1446 |
case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD: |
| 1447 |
case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD: |
| 1448 |
case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD: |
| 1449 |
case X509_V_ERR_CERT_UNTRUSTED: |
| 1450 |
case X509_V_ERR_CERT_REJECTED: |
| 1451 |
case X509_V_ERR_HOSTNAME_MISMATCH: |
| 1452 |
case X509_V_ERR_EMAIL_MISMATCH: |
| 1453 |
case X509_V_ERR_IP_ADDRESS_MISMATCH: |
| 1454 |
return SSL_AD_BAD_CERTIFICATE; |
| 1455 |
|
| 1456 |
case X509_V_ERR_CERT_SIGNATURE_FAILURE: |
| 1457 |
case X509_V_ERR_CRL_SIGNATURE_FAILURE: |
| 1458 |
return SSL_AD_DECRYPT_ERROR; |
| 1459 |
|
| 1460 |
case X509_V_ERR_CERT_HAS_EXPIRED: |
| 1461 |
case X509_V_ERR_CERT_NOT_YET_VALID: |
| 1462 |
case X509_V_ERR_CRL_HAS_EXPIRED: |
| 1463 |
case X509_V_ERR_CRL_NOT_YET_VALID: |
| 1464 |
return SSL_AD_CERTIFICATE_EXPIRED; |
| 1465 |
|
| 1466 |
case X509_V_ERR_CERT_REVOKED: |
| 1467 |
return SSL_AD_CERTIFICATE_REVOKED; |
| 1468 |
|
| 1469 |
case X509_V_ERR_UNSPECIFIED: |
| 1470 |
case X509_V_ERR_OUT_OF_MEM: |
| 1471 |
case X509_V_ERR_INVALID_CALL: |
| 1472 |
case X509_V_ERR_STORE_LOOKUP: |
| 1473 |
return SSL_AD_INTERNAL_ERROR; |
| 1474 |
|
| 1475 |
case X509_V_ERR_APPLICATION_VERIFICATION: |
| 1476 |
return SSL_AD_HANDSHAKE_FAILURE; |
| 1477 |
|
| 1478 |
case X509_V_ERR_INVALID_PURPOSE: |
| 1479 |
case X509_V_UNABLE_TO_GET_CERTS_PUBLIC_KEY: |
| 1480 |
return SSL_AD_UNSUPPORTED_CERTIFICATE; |
| 1481 |
|
| 1482 |
default: |
| 1483 |
return SSL_AD_CERTIFICATE_UNKNOWN; |
| 1484 |
} |
| 1485 |
} |