| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
| 8 | // |
| 9 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
| 16 |
| 17 | #include <openssl/ssl.h> |
| 18 |
| 19 | #include <assert.h> |
| 20 |
| 21 | #include <openssl/asn1.h> |
| 22 | #include <openssl/bytestring.h> |
| 23 | #include <openssl/err.h> |
| 24 | #include <openssl/pem.h> |
| 25 | #include <openssl/stack.h> |
| 26 | #include <openssl/x509.h> |
| 27 |
| 28 | #include "../crypto/internal.h" |
| 29 | #include "internal.h" |
| 30 |
| 31 |
| 32 | BSSL_NAMESPACE_BEGIN |
| 33 |
| 34 | // check_ssl_x509_method asserts that |ssl| has the X509-based method |
| 35 | // installed. Calling an X509-based method on an |ssl| with a different method |
| 36 | // will likely misbehave and possibly crash or leak memory. |
| 37 | static void check_ssl_x509_method(const SSL *ssl) { |
| 38 | assert(ssl == NULL || ssl->ctx->x509_method == &ssl_crypto_x509_method); |
| 39 | } |
| 40 |
| 41 | // check_ssl_ctx_x509_method acts like |check_ssl_x509_method|, but for an |
| 42 | // |SSL_CTX|. |
| 43 | static void check_ssl_ctx_x509_method(const SSL_CTX *ctx) { |
| 44 | assert(ctx == NULL || ctx->x509_method == &ssl_crypto_x509_method); |
| 45 | } |
| 46 |
| 47 | // x509_to_buffer returns a |CRYPTO_BUFFER| that contains the serialised |
| 48 | // contents of |x509|. |
| 49 | static UniquePtr<CRYPTO_BUFFER> x509_to_buffer(X509 *x509) { |
| 50 | uint8_t *buf = NULL; |
| 51 | int cert_len = i2d_X509(x509, &buf); |
| 52 | if (cert_len <= 0) { |
| 53 | return 0; |
| 54 | } |
| 55 |
| 56 | UniquePtr<CRYPTO_BUFFER> buffer(CRYPTO_BUFFER_new(buf, cert_len, NULL)); |
| 57 | OPENSSL_free(buf); |
| 58 |
| 59 | return buffer; |
| 60 | } |
| 61 |
| 62 | static void ssl_crypto_x509_cert_flush_cached_leaf(CERT *cert) { |
| 63 | X509_free(cert->x509_leaf); |
| 64 | cert->x509_leaf = nullptr; |
| 65 | } |
| 66 |
| 67 | static void ssl_crypto_x509_cert_flush_cached_chain(CERT *cert) { |
| 68 | sk_X509_pop_free(cert->x509_chain, X509_free); |
| 69 | cert->x509_chain = nullptr; |
| 70 | } |
| 71 |
| 72 | // ssl_cert_set1_chain sets elements 1.. of |cert->chain| to the serialised |
| 73 | // forms of elements of |chain|. It returns one on success or zero on error, in |
| 74 | // which case no change to |cert->chain| is made. It preverses the existing |
| 75 | // leaf from |cert->chain|, if any. |
| 76 | static bool ssl_cert_set1_chain(CERT *cert, STACK_OF(X509) *chain) { |
| 77 | cert->legacy_credential->ClearIntermediateCerts(); |
| 78 | for (X509 *x509 : chain) { |
| 79 | UniquePtr<CRYPTO_BUFFER> buffer = x509_to_buffer(x509); |
| 80 | if (!buffer || |
| 81 | !cert->legacy_credential->AppendIntermediateCert(std::move(buffer))) { |
| 82 | return false; |
| 83 | } |
| 84 | } |
| 85 |
| 86 | ssl_crypto_x509_cert_flush_cached_chain(cert); |
| 87 | return true; |
| 88 | } |
| 89 |
| 90 | static bool ssl_crypto_x509_check_client_CA_list( |
| 91 | STACK_OF(CRYPTO_BUFFER) *names) { |
| 92 | for (const CRYPTO_BUFFER *buffer : names) { |
| 93 | const uint8_t *inp = CRYPTO_BUFFER_data(buffer); |
| 94 | UniquePtr<X509_NAME> name( |
| 95 | d2i_X509_NAME(nullptr, &inp, CRYPTO_BUFFER_len(buffer))); |
| 96 | if (name == nullptr || |
| 97 | inp != CRYPTO_BUFFER_data(buffer) + CRYPTO_BUFFER_len(buffer)) { |
| 98 | return false; |
| 99 | } |
| 100 | } |
| 101 |
| 102 | return true; |
| 103 | } |
| 104 |
| 105 | static void ssl_crypto_x509_cert_clear(CERT *cert) { |
| 106 | ssl_crypto_x509_cert_flush_cached_leaf(cert); |
| 107 | ssl_crypto_x509_cert_flush_cached_chain(cert); |
| 108 |
| 109 | X509_free(cert->x509_stash); |
| 110 | cert->x509_stash = nullptr; |
| 111 | } |
| 112 |
| 113 | static void ssl_crypto_x509_cert_free(CERT *cert) { |
| 114 | ssl_crypto_x509_cert_clear(cert); |
| 115 | X509_STORE_free(cert->verify_store); |
| 116 | } |
| 117 |
| 118 | static void ssl_crypto_x509_cert_dup(CERT *new_cert, const CERT *cert) { |
| 119 | if (cert->verify_store != nullptr) { |
| 120 | X509_STORE_up_ref(cert->verify_store); |
| 121 | new_cert->verify_store = cert->verify_store; |
| 122 | } |
| 123 | } |
| 124 |
| 125 | static bool ssl_crypto_x509_session_cache_objects(SSL_SESSION *sess) { |
| 126 | bssl::UniquePtr<STACK_OF(X509)> chain, chain_without_leaf; |
| 127 | if (sk_CRYPTO_BUFFER_num(sess->certs.get()) > 0) { |
| 128 | chain.reset(sk_X509_new_null()); |
| 129 | if (!chain) { |
| 130 | return false; |
| 131 | } |
| 132 | if (sess->is_server) { |
| 133 | // chain_without_leaf is only needed for server sessions. See |
| 134 | // |SSL_get_peer_cert_chain|. |
| 135 | chain_without_leaf.reset(sk_X509_new_null()); |
| 136 | if (!chain_without_leaf) { |
| 137 | return false; |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 |
| 142 | bssl::UniquePtr<X509> leaf; |
| 143 | for (CRYPTO_BUFFER *cert : sess->certs.get()) { |
| 144 | UniquePtr<X509> x509(X509_parse_from_buffer(cert)); |
| 145 | if (!x509) { |
| 146 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 147 | return false; |
| 148 | } |
| 149 | if (leaf == nullptr) { |
| 150 | leaf = UpRef(x509); |
| 151 | } else if (chain_without_leaf && |
| 152 | !PushToStack(chain_without_leaf.get(), UpRef(x509))) { |
| 153 | return false; |
| 154 | } |
| 155 | if (!PushToStack(chain.get(), std::move(x509))) { |
| 156 | return false; |
| 157 | } |
| 158 | } |
| 159 |
| 160 | sk_X509_pop_free(sess->x509_chain, X509_free); |
| 161 | sess->x509_chain = chain.release(); |
| 162 |
| 163 | sk_X509_pop_free(sess->x509_chain_without_leaf, X509_free); |
| 164 | sess->x509_chain_without_leaf = chain_without_leaf.release(); |
| 165 |
| 166 | X509_free(sess->x509_peer); |
| 167 | sess->x509_peer = leaf.release(); |
| 168 | return true; |
| 169 | } |
| 170 |
| 171 | static bool ssl_crypto_x509_session_dup(SSL_SESSION *new_session, |
| 172 | const SSL_SESSION *session) { |
| 173 | new_session->x509_peer = UpRef(session->x509_peer).release(); |
| 174 | if (session->x509_chain != nullptr) { |
| 175 | new_session->x509_chain = X509_chain_up_ref(session->x509_chain); |
| 176 | if (new_session->x509_chain == nullptr) { |
| 177 | return false; |
| 178 | } |
| 179 | } |
| 180 | if (session->x509_chain_without_leaf != nullptr) { |
| 181 | new_session->x509_chain_without_leaf = |
| 182 | X509_chain_up_ref(session->x509_chain_without_leaf); |
| 183 | if (new_session->x509_chain_without_leaf == nullptr) { |
| 184 | return false; |
| 185 | } |
| 186 | } |
| 187 |
| 188 | return true; |
| 189 | } |
| 190 |
| 191 | static void ssl_crypto_x509_session_clear(SSL_SESSION *session) { |
| 192 | X509_free(session->x509_peer); |
| 193 | session->x509_peer = nullptr; |
| 194 | sk_X509_pop_free(session->x509_chain, X509_free); |
| 195 | session->x509_chain = nullptr; |
| 196 | sk_X509_pop_free(session->x509_chain_without_leaf, X509_free); |
| 197 | session->x509_chain_without_leaf = nullptr; |
| 198 | } |
| 199 |
| 200 | static bool ssl_crypto_x509_session_verify_cert_chain(SSL_SESSION *session, |
| 201 | SSL_HANDSHAKE *hs, |
| 202 | uint8_t *out_alert) { |
| 203 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 204 | STACK_OF(X509) *const cert_chain = session->x509_chain; |
| 205 | if (cert_chain == nullptr || sk_X509_num(cert_chain) == 0) { |
| 206 | return false; |
| 207 | } |
| 208 |
| 209 | SSL *const ssl = hs->ssl; |
| 210 | SSL_CTX *ssl_ctx = ssl->ctx.get(); |
| 211 | X509_STORE *verify_store = ssl_ctx->cert_store; |
| 212 | if (hs->config->cert->verify_store != nullptr) { |
| 213 | verify_store = hs->config->cert->verify_store; |
| 214 | } |
| 215 |
| 216 | X509 *leaf = sk_X509_value(cert_chain, 0); |
| 217 | const char *name; |
| 218 | size_t name_len; |
| 219 | SSL_get0_ech_name_override(ssl, &name, &name_len); |
| 220 | UniquePtr<X509_STORE_CTX> ctx(X509_STORE_CTX_new()); |
| 221 | if (!ctx || // |
| 222 | !X509_STORE_CTX_init(ctx.get(), verify_store, leaf, cert_chain) || // |
| 223 | !X509_STORE_CTX_set_ex_data( |
| 224 | ctx.get(), SSL_get_ex_data_X509_STORE_CTX_idx(), ssl) || // |
| 225 | // We need to inherit the verify parameters. These can be determined by |
| 226 | // the context: if its a server it will verify SSL client certificates or |
| 227 | // vice versa. |
| 228 | !X509_STORE_CTX_set_default( |
| 229 | ctx.get(), |
| 230 | ssl->server ? "ssl_client" : "ssl_server") || // |
| 231 | // Anything non-default in "param" should overwrite anything in the ctx. |
| 232 | !X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(ctx.get()), |
| 233 | hs->config->param) || // |
| 234 | // ClientHelloOuter connections use a different name. |
| 235 | (name_len != 0 && // |
| 236 | !X509_VERIFY_PARAM_set1_host(X509_STORE_CTX_get0_param(ctx.get()), name, |
| 237 | name_len))) { |
| 238 | OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB); |
| 239 | return false; |
| 240 | } |
| 241 |
| 242 | if (hs->config->verify_callback) { |
| 243 | X509_STORE_CTX_set_verify_cb(ctx.get(), hs->config->verify_callback); |
| 244 | } |
| 245 |
| 246 | int verify_ret; |
| 247 | if (ssl_ctx->app_verify_callback != nullptr) { |
| 248 | verify_ret = |
| 249 | ssl_ctx->app_verify_callback(ctx.get(), ssl_ctx->app_verify_arg); |
| 250 | } else { |
| 251 | verify_ret = X509_verify_cert(ctx.get()); |
| 252 | } |
| 253 |
| 254 | session->verify_result = X509_STORE_CTX_get_error(ctx.get()); |
| 255 |
| 256 | // If |SSL_VERIFY_NONE|, the error is non-fatal, but we keep the result. |
| 257 | if (verify_ret <= 0 && hs->config->verify_mode != SSL_VERIFY_NONE) { |
| 258 | *out_alert = SSL_alert_from_verify_result(session->verify_result); |
| 259 | return false; |
| 260 | } |
| 261 |
| 262 | ERR_clear_error(); |
| 263 | return true; |
| 264 | } |
| 265 |
| 266 | static void ssl_crypto_x509_hs_flush_cached_ca_names(SSL_HANDSHAKE *hs) { |
| 267 | sk_X509_NAME_pop_free(hs->cached_x509_ca_names, X509_NAME_free); |
| 268 | hs->cached_x509_ca_names = nullptr; |
| 269 | } |
| 270 |
| 271 | static bool ssl_crypto_x509_ssl_new(SSL_HANDSHAKE *hs) { |
| 272 | hs->config->param = X509_VERIFY_PARAM_new(); |
| 273 | if (hs->config->param == nullptr) { |
| 274 | return false; |
| 275 | } |
| 276 | X509_VERIFY_PARAM_inherit(hs->config->param, hs->ssl->ctx->param); |
| 277 | return true; |
| 278 | } |
| 279 |
| 280 | static void ssl_crypto_x509_ssl_flush_cached_client_CA(SSL_CONFIG *cfg) { |
| 281 | sk_X509_NAME_pop_free(cfg->cached_x509_client_CA, X509_NAME_free); |
| 282 | cfg->cached_x509_client_CA = nullptr; |
| 283 | } |
| 284 |
| 285 | static void ssl_crypto_x509_ssl_config_free(SSL_CONFIG *cfg) { |
| 286 | sk_X509_NAME_pop_free(cfg->cached_x509_client_CA, X509_NAME_free); |
| 287 | cfg->cached_x509_client_CA = nullptr; |
| 288 | X509_VERIFY_PARAM_free(cfg->param); |
| 289 | } |
| 290 |
| 291 | static bool ssl_crypto_x509_ssl_auto_chain_if_needed(SSL_HANDSHAKE *hs) { |
| 292 | // Only build a chain if the feature isn't disabled, the legacy credential |
| 293 | // exists but has no intermediates configured. |
| 294 | SSL *ssl = hs->ssl; |
| 295 | SSL_CREDENTIAL *cred = hs->config->cert->legacy_credential.get(); |
| 296 | if ((ssl->mode & SSL_MODE_NO_AUTO_CHAIN) || !cred->IsComplete() || |
| 297 | sk_CRYPTO_BUFFER_num(cred->chain.get()) != 1) { |
| 298 | return true; |
| 299 | } |
| 300 |
| 301 | UniquePtr<X509> leaf( |
| 302 | X509_parse_from_buffer(sk_CRYPTO_BUFFER_value(cred->chain.get(), 0))); |
| 303 | if (!leaf) { |
| 304 | OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB); |
| 305 | return false; |
| 306 | } |
| 307 |
| 308 | UniquePtr<X509_STORE_CTX> ctx(X509_STORE_CTX_new()); |
| 309 | if (!ctx || !X509_STORE_CTX_init(ctx.get(), ssl->ctx->cert_store, leaf.get(), |
| 310 | nullptr)) { |
| 311 | OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB); |
| 312 | return false; |
| 313 | } |
| 314 |
| 315 | // Attempt to build a chain, ignoring the result. |
| 316 | X509_verify_cert(ctx.get()); |
| 317 | ERR_clear_error(); |
| 318 |
| 319 | // Remove the leaf from the generated chain. |
| 320 | UniquePtr<STACK_OF(X509)> chain(X509_STORE_CTX_get1_chain(ctx.get())); |
| 321 | if (!chain) { |
| 322 | return false; |
| 323 | } |
| 324 | X509_free(sk_X509_shift(chain.get())); |
| 325 |
| 326 | return SSL_set1_chain(ssl, chain.get()); |
| 327 | } |
| 328 |
| 329 | static void ssl_crypto_x509_ssl_ctx_flush_cached_client_CA(SSL_CTX *ctx) { |
| 330 | sk_X509_NAME_pop_free(ctx->cached_x509_client_CA, X509_NAME_free); |
| 331 | ctx->cached_x509_client_CA = nullptr; |
| 332 | } |
| 333 |
| 334 | static bool ssl_crypto_x509_ssl_ctx_new(SSL_CTX *ctx) { |
| 335 | ctx->cert_store = X509_STORE_new(); |
| 336 | ctx->param = X509_VERIFY_PARAM_new(); |
| 337 | return (ctx->cert_store != nullptr && ctx->param != nullptr); |
| 338 | } |
| 339 |
| 340 | static void ssl_crypto_x509_ssl_ctx_free(SSL_CTX *ctx) { |
| 341 | ssl_crypto_x509_ssl_ctx_flush_cached_client_CA(ctx); |
| 342 | X509_VERIFY_PARAM_free(ctx->param); |
| 343 | X509_STORE_free(ctx->cert_store); |
| 344 | } |
| 345 |
| 346 | const SSL_X509_METHOD ssl_crypto_x509_method = { |
| 347 | ssl_crypto_x509_check_client_CA_list, |
| 348 | ssl_crypto_x509_cert_clear, |
| 349 | ssl_crypto_x509_cert_free, |
| 350 | ssl_crypto_x509_cert_dup, |
| 351 | ssl_crypto_x509_cert_flush_cached_chain, |
| 352 | ssl_crypto_x509_cert_flush_cached_leaf, |
| 353 | ssl_crypto_x509_session_cache_objects, |
| 354 | ssl_crypto_x509_session_dup, |
| 355 | ssl_crypto_x509_session_clear, |
| 356 | ssl_crypto_x509_session_verify_cert_chain, |
| 357 | ssl_crypto_x509_hs_flush_cached_ca_names, |
| 358 | ssl_crypto_x509_ssl_new, |
| 359 | ssl_crypto_x509_ssl_config_free, |
| 360 | ssl_crypto_x509_ssl_flush_cached_client_CA, |
| 361 | ssl_crypto_x509_ssl_auto_chain_if_needed, |
| 362 | ssl_crypto_x509_ssl_ctx_new, |
| 363 | ssl_crypto_x509_ssl_ctx_free, |
| 364 | ssl_crypto_x509_ssl_ctx_flush_cached_client_CA, |
| 365 | }; |
| 366 |
| 367 | BSSL_NAMESPACE_END |
| 368 |
| 369 | using namespace bssl; |
| 370 |
| 371 | X509 *SSL_get_peer_certificate(const SSL *ssl) { |
| 372 | check_ssl_x509_method(ssl); |
| 373 | if (ssl == NULL) { |
| 374 | return NULL; |
| 375 | } |
| 376 | SSL_SESSION *session = SSL_get_session(ssl); |
| 377 | if (session == NULL || session->x509_peer == NULL) { |
| 378 | return NULL; |
| 379 | } |
| 380 | X509_up_ref(session->x509_peer); |
| 381 | return session->x509_peer; |
| 382 | } |
| 383 |
| 384 | STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *ssl) { |
| 385 | check_ssl_x509_method(ssl); |
| 386 | if (ssl == nullptr) { |
| 387 | return nullptr; |
| 388 | } |
| 389 | SSL_SESSION *session = SSL_get_session(ssl); |
| 390 | if (session == nullptr) { |
| 391 | return nullptr; |
| 392 | } |
| 393 |
| 394 | // OpenSSL historically didn't include the leaf certificate in the returned |
| 395 | // certificate chain, but only for servers. |
| 396 | return ssl->server ? session->x509_chain_without_leaf : session->x509_chain; |
| 397 | } |
| 398 |
| 399 | STACK_OF(X509) *SSL_get_peer_full_cert_chain(const SSL *ssl) { |
| 400 | check_ssl_x509_method(ssl); |
| 401 | SSL_SESSION *session = SSL_get_session(ssl); |
| 402 | if (session == NULL) { |
| 403 | return NULL; |
| 404 | } |
| 405 |
| 406 | return session->x509_chain; |
| 407 | } |
| 408 |
| 409 | int SSL_CTX_set_purpose(SSL_CTX *ctx, int purpose) { |
| 410 | check_ssl_ctx_x509_method(ctx); |
| 411 | return X509_VERIFY_PARAM_set_purpose(ctx->param, purpose); |
| 412 | } |
| 413 |
| 414 | int SSL_set_purpose(SSL *ssl, int purpose) { |
| 415 | check_ssl_x509_method(ssl); |
| 416 | if (!ssl->config) { |
| 417 | return 0; |
| 418 | } |
| 419 | return X509_VERIFY_PARAM_set_purpose(ssl->config->param, purpose); |
| 420 | } |
| 421 |
| 422 | int SSL_CTX_set_trust(SSL_CTX *ctx, int trust) { |
| 423 | check_ssl_ctx_x509_method(ctx); |
| 424 | return X509_VERIFY_PARAM_set_trust(ctx->param, trust); |
| 425 | } |
| 426 |
| 427 | int SSL_set_trust(SSL *ssl, int trust) { |
| 428 | check_ssl_x509_method(ssl); |
| 429 | if (!ssl->config) { |
| 430 | return 0; |
| 431 | } |
| 432 | return X509_VERIFY_PARAM_set_trust(ssl->config->param, trust); |
| 433 | } |
| 434 |
| 435 | int SSL_CTX_set1_param(SSL_CTX *ctx, const X509_VERIFY_PARAM *param) { |
| 436 | check_ssl_ctx_x509_method(ctx); |
| 437 | return X509_VERIFY_PARAM_set1(ctx->param, param); |
| 438 | } |
| 439 |
| 440 | int SSL_set1_param(SSL *ssl, const X509_VERIFY_PARAM *param) { |
| 441 | check_ssl_x509_method(ssl); |
| 442 | if (!ssl->config) { |
| 443 | return 0; |
| 444 | } |
| 445 | return X509_VERIFY_PARAM_set1(ssl->config->param, param); |
| 446 | } |
| 447 |
| 448 | X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) { |
| 449 | check_ssl_ctx_x509_method(ctx); |
| 450 | return ctx->param; |
| 451 | } |
| 452 |
| 453 | X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) { |
| 454 | check_ssl_x509_method(ssl); |
| 455 | if (!ssl->config) { |
| 456 | assert(ssl->config); |
| 457 | return 0; |
| 458 | } |
| 459 | return ssl->config->param; |
| 460 | } |
| 461 |
| 462 | int SSL_get_verify_depth(const SSL *ssl) { |
| 463 | check_ssl_x509_method(ssl); |
| 464 | if (!ssl->config) { |
| 465 | assert(ssl->config); |
| 466 | return 0; |
| 467 | } |
| 468 | return X509_VERIFY_PARAM_get_depth(ssl->config->param); |
| 469 | } |
| 470 |
| 471 | int (*SSL_get_verify_callback(const SSL *ssl))(int, X509_STORE_CTX *) { |
| 472 | check_ssl_x509_method(ssl); |
| 473 | if (!ssl->config) { |
| 474 | assert(ssl->config); |
| 475 | return 0; |
| 476 | } |
| 477 | return ssl->config->verify_callback; |
| 478 | } |
| 479 |
| 480 | int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) { |
| 481 | check_ssl_ctx_x509_method(ctx); |
| 482 | return ctx->verify_mode; |
| 483 | } |
| 484 |
| 485 | int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) { |
| 486 | check_ssl_ctx_x509_method(ctx); |
| 487 | return X509_VERIFY_PARAM_get_depth(ctx->param); |
| 488 | } |
| 489 |
| 490 | int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))( |
| 491 | int ok, X509_STORE_CTX *store_ctx) { |
| 492 | check_ssl_ctx_x509_method(ctx); |
| 493 | return ctx->default_verify_callback; |
| 494 | } |
| 495 |
| 496 | void SSL_set_verify(SSL *ssl, int mode, |
| 497 | int (*callback)(int ok, X509_STORE_CTX *store_ctx)) { |
| 498 | check_ssl_x509_method(ssl); |
| 499 | if (!ssl->config) { |
| 500 | return; |
| 501 | } |
| 502 | ssl->config->verify_mode = mode; |
| 503 | if (callback != NULL) { |
| 504 | ssl->config->verify_callback = callback; |
| 505 | } |
| 506 | } |
| 507 |
| 508 | void SSL_set_verify_depth(SSL *ssl, int depth) { |
| 509 | check_ssl_x509_method(ssl); |
| 510 | if (!ssl->config) { |
| 511 | return; |
| 512 | } |
| 513 | X509_VERIFY_PARAM_set_depth(ssl->config->param, depth); |
| 514 | } |
| 515 |
| 516 | void SSL_CTX_set_cert_verify_callback( |
| 517 | SSL_CTX *ctx, int (*cb)(X509_STORE_CTX *store_ctx, void *arg), void *arg) { |
| 518 | check_ssl_ctx_x509_method(ctx); |
| 519 | ctx->app_verify_callback = cb; |
| 520 | ctx->app_verify_arg = arg; |
| 521 | } |
| 522 |
| 523 | void SSL_CTX_set_verify(SSL_CTX *ctx, int mode, |
| 524 | int (*cb)(int, X509_STORE_CTX *)) { |
| 525 | check_ssl_ctx_x509_method(ctx); |
| 526 | ctx->verify_mode = mode; |
| 527 | ctx->default_verify_callback = cb; |
| 528 | } |
| 529 |
| 530 | void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) { |
| 531 | check_ssl_ctx_x509_method(ctx); |
| 532 | X509_VERIFY_PARAM_set_depth(ctx->param, depth); |
| 533 | } |
| 534 |
| 535 | int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) { |
| 536 | check_ssl_ctx_x509_method(ctx); |
| 537 | return X509_STORE_set_default_paths(ctx->cert_store); |
| 538 | } |
| 539 |
| 540 | int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *ca_file, |
| 541 | const char *ca_dir) { |
| 542 | check_ssl_ctx_x509_method(ctx); |
| 543 | return X509_STORE_load_locations(ctx->cert_store, ca_file, ca_dir); |
| 544 | } |
| 545 |
| 546 | long SSL_get_verify_result(const SSL *ssl) { |
| 547 | check_ssl_x509_method(ssl); |
| 548 | SSL_SESSION *session = SSL_get_session(ssl); |
| 549 | if (session == NULL) { |
| 550 | return X509_V_ERR_INVALID_CALL; |
| 551 | } |
| 552 | return session->verify_result; |
| 553 | } |
| 554 |
| 555 | X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) { |
| 556 | check_ssl_ctx_x509_method(ctx); |
| 557 | return ctx->cert_store; |
| 558 | } |
| 559 |
| 560 | void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) { |
| 561 | check_ssl_ctx_x509_method(ctx); |
| 562 | X509_STORE_free(ctx->cert_store); |
| 563 | ctx->cert_store = store; |
| 564 | } |
| 565 |
| 566 | static int ssl_use_certificate(CERT *cert, X509 *x) { |
| 567 | if (x == NULL) { |
| 568 | OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER); |
| 569 | return 0; |
| 570 | } |
| 571 |
| 572 | UniquePtr<CRYPTO_BUFFER> buffer = x509_to_buffer(x); |
| 573 | if (!buffer) { |
| 574 | return 0; |
| 575 | } |
| 576 |
| 577 | return ssl_set_cert(cert, std::move(buffer)); |
| 578 | } |
| 579 |
| 580 | int SSL_use_certificate(SSL *ssl, X509 *x) { |
| 581 | check_ssl_x509_method(ssl); |
| 582 | if (!ssl->config) { |
| 583 | return 0; |
| 584 | } |
| 585 | return ssl_use_certificate(ssl->config->cert.get(), x); |
| 586 | } |
| 587 |
| 588 | int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x) { |
| 589 | check_ssl_ctx_x509_method(ctx); |
| 590 | return ssl_use_certificate(ctx->cert.get(), x); |
| 591 | } |
| 592 |
| 593 | // ssl_cert_cache_leaf_cert sets |cert->x509_leaf|, if currently NULL, from the |
| 594 | // first element of |cert->chain|. |
| 595 | static int ssl_cert_cache_leaf_cert(CERT *cert) { |
| 596 | assert(cert->x509_method); |
| 597 |
| 598 | const SSL_CREDENTIAL *cred = cert->legacy_credential.get(); |
| 599 | if (cert->x509_leaf != NULL || cred->chain == NULL) { |
| 600 | return 1; |
| 601 | } |
| 602 |
| 603 | CRYPTO_BUFFER *leaf = sk_CRYPTO_BUFFER_value(cred->chain.get(), 0); |
| 604 | if (!leaf) { |
| 605 | return 1; |
| 606 | } |
| 607 |
| 608 | cert->x509_leaf = X509_parse_from_buffer(leaf); |
| 609 | return cert->x509_leaf != NULL; |
| 610 | } |
| 611 |
| 612 | static X509 *ssl_cert_get0_leaf(CERT *cert) { |
| 613 | if (cert->x509_leaf == NULL && // |
| 614 | !ssl_cert_cache_leaf_cert(cert)) { |
| 615 | return NULL; |
| 616 | } |
| 617 |
| 618 | return cert->x509_leaf; |
| 619 | } |
| 620 |
| 621 | X509 *SSL_get_certificate(const SSL *ssl) { |
| 622 | check_ssl_x509_method(ssl); |
| 623 | if (!ssl->config) { |
| 624 | assert(ssl->config); |
| 625 | return 0; |
| 626 | } |
| 627 | return ssl_cert_get0_leaf(ssl->config->cert.get()); |
| 628 | } |
| 629 |
| 630 | X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) { |
| 631 | check_ssl_ctx_x509_method(ctx); |
| 632 | MutexWriteLock lock(const_cast<CRYPTO_MUTEX *>(&ctx->lock)); |
| 633 | return ssl_cert_get0_leaf(ctx->cert.get()); |
| 634 | } |
| 635 |
| 636 | static int ssl_cert_add1_chain_cert(CERT *cert, X509 *x509) { |
| 637 | assert(cert->x509_method); |
| 638 |
| 639 | UniquePtr<CRYPTO_BUFFER> buffer = x509_to_buffer(x509); |
| 640 | if (!buffer || |
| 641 | !cert->legacy_credential->AppendIntermediateCert(std::move(buffer))) { |
| 642 | return 0; |
| 643 | } |
| 644 |
| 645 | ssl_crypto_x509_cert_flush_cached_chain(cert); |
| 646 | return 1; |
| 647 | } |
| 648 |
| 649 | static int ssl_cert_add0_chain_cert(CERT *cert, X509 *x509) { |
| 650 | if (!ssl_cert_add1_chain_cert(cert, x509)) { |
| 651 | return 0; |
| 652 | } |
| 653 |
| 654 | X509_free(cert->x509_stash); |
| 655 | cert->x509_stash = x509; |
| 656 | return 1; |
| 657 | } |
| 658 |
| 659 | int SSL_CTX_set0_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) { |
| 660 | check_ssl_ctx_x509_method(ctx); |
| 661 | if (!ssl_cert_set1_chain(ctx->cert.get(), chain)) { |
| 662 | return 0; |
| 663 | } |
| 664 | sk_X509_pop_free(chain, X509_free); |
| 665 | return 1; |
| 666 | } |
| 667 |
| 668 | int SSL_CTX_set1_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) { |
| 669 | check_ssl_ctx_x509_method(ctx); |
| 670 | return ssl_cert_set1_chain(ctx->cert.get(), chain); |
| 671 | } |
| 672 |
| 673 | int SSL_set0_chain(SSL *ssl, STACK_OF(X509) *chain) { |
| 674 | check_ssl_x509_method(ssl); |
| 675 | if (!ssl->config) { |
| 676 | return 0; |
| 677 | } |
| 678 | if (!ssl_cert_set1_chain(ssl->config->cert.get(), chain)) { |
| 679 | return 0; |
| 680 | } |
| 681 | sk_X509_pop_free(chain, X509_free); |
| 682 | return 1; |
| 683 | } |
| 684 |
| 685 | int SSL_set1_chain(SSL *ssl, STACK_OF(X509) *chain) { |
| 686 | check_ssl_x509_method(ssl); |
| 687 | if (!ssl->config) { |
| 688 | return 0; |
| 689 | } |
| 690 | return ssl_cert_set1_chain(ssl->config->cert.get(), chain); |
| 691 | } |
| 692 |
| 693 | int SSL_CTX_add0_chain_cert(SSL_CTX *ctx, X509 *x509) { |
| 694 | check_ssl_ctx_x509_method(ctx); |
| 695 | return ssl_cert_add0_chain_cert(ctx->cert.get(), x509); |
| 696 | } |
| 697 |
| 698 | int SSL_CTX_add1_chain_cert(SSL_CTX *ctx, X509 *x509) { |
| 699 | check_ssl_ctx_x509_method(ctx); |
| 700 | return ssl_cert_add1_chain_cert(ctx->cert.get(), x509); |
| 701 | } |
| 702 |
| 703 | int SSL_CTX_add_extra_chain_cert(SSL_CTX *ctx, X509 *x509) { |
| 704 | check_ssl_ctx_x509_method(ctx); |
| 705 | return SSL_CTX_add0_chain_cert(ctx, x509); |
| 706 | } |
| 707 |
| 708 | int SSL_add0_chain_cert(SSL *ssl, X509 *x509) { |
| 709 | check_ssl_x509_method(ssl); |
| 710 | if (!ssl->config) { |
| 711 | return 0; |
| 712 | } |
| 713 | return ssl_cert_add0_chain_cert(ssl->config->cert.get(), x509); |
| 714 | } |
| 715 |
| 716 | int SSL_add1_chain_cert(SSL *ssl, X509 *x509) { |
| 717 | check_ssl_x509_method(ssl); |
| 718 | if (!ssl->config) { |
| 719 | return 0; |
| 720 | } |
| 721 | return ssl_cert_add1_chain_cert(ssl->config->cert.get(), x509); |
| 722 | } |
| 723 |
| 724 | int SSL_CTX_clear_chain_certs(SSL_CTX *ctx) { |
| 725 | check_ssl_ctx_x509_method(ctx); |
| 726 | return SSL_CTX_set0_chain(ctx, NULL); |
| 727 | } |
| 728 |
| 729 | int SSL_CTX_clear_extra_chain_certs(SSL_CTX *ctx) { |
| 730 | check_ssl_ctx_x509_method(ctx); |
| 731 | return SSL_CTX_clear_chain_certs(ctx); |
| 732 | } |
| 733 |
| 734 | int SSL_clear_chain_certs(SSL *ssl) { |
| 735 | check_ssl_x509_method(ssl); |
| 736 | return SSL_set0_chain(ssl, NULL); |
| 737 | } |
| 738 |
| 739 | // ssl_cert_cache_chain_certs fills in |cert->x509_chain| from elements 1.. of |
| 740 | // |cert->chain|. |
| 741 | static int ssl_cert_cache_chain_certs(CERT *cert) { |
| 742 | assert(cert->x509_method); |
| 743 |
| 744 | const SSL_CREDENTIAL *cred = cert->legacy_credential.get(); |
| 745 | if (cert->x509_chain != nullptr || cred->chain == nullptr || |
| 746 | sk_CRYPTO_BUFFER_num(cred->chain.get()) < 2) { |
| 747 | return 1; |
| 748 | } |
| 749 |
| 750 | UniquePtr<STACK_OF(X509)> chain(sk_X509_new_null()); |
| 751 | if (!chain) { |
| 752 | return 0; |
| 753 | } |
| 754 |
| 755 | for (size_t i = 1; i < sk_CRYPTO_BUFFER_num(cred->chain.get()); i++) { |
| 756 | CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(cred->chain.get(), i); |
| 757 | UniquePtr<X509> x509(X509_parse_from_buffer(buffer)); |
| 758 | if (!x509 || // |
| 759 | !PushToStack(chain.get(), std::move(x509))) { |
| 760 | return 0; |
| 761 | } |
| 762 | } |
| 763 |
| 764 | cert->x509_chain = chain.release(); |
| 765 | return 1; |
| 766 | } |
| 767 |
| 768 | int SSL_CTX_get0_chain_certs(const SSL_CTX *ctx, STACK_OF(X509) **out_chain) { |
| 769 | check_ssl_ctx_x509_method(ctx); |
| 770 | MutexWriteLock lock(const_cast<CRYPTO_MUTEX *>(&ctx->lock)); |
| 771 | if (!ssl_cert_cache_chain_certs(ctx->cert.get())) { |
| 772 | *out_chain = NULL; |
| 773 | return 0; |
| 774 | } |
| 775 |
| 776 | *out_chain = ctx->cert->x509_chain; |
| 777 | return 1; |
| 778 | } |
| 779 |
| 780 | int SSL_CTX_get_extra_chain_certs(const SSL_CTX *ctx, |
| 781 | STACK_OF(X509) **out_chain) { |
| 782 | return SSL_CTX_get0_chain_certs(ctx, out_chain); |
| 783 | } |
| 784 |
| 785 | int SSL_get0_chain_certs(const SSL *ssl, STACK_OF(X509) **out_chain) { |
| 786 | check_ssl_x509_method(ssl); |
| 787 | if (!ssl->config) { |
| 788 | assert(ssl->config); |
| 789 | return 0; |
| 790 | } |
| 791 | if (!ssl_cert_cache_chain_certs(ssl->config->cert.get())) { |
| 792 | *out_chain = NULL; |
| 793 | return 0; |
| 794 | } |
| 795 |
| 796 | *out_chain = ssl->config->cert->x509_chain; |
| 797 | return 1; |
| 798 | } |
| 799 |
| 800 | SSL_SESSION *d2i_SSL_SESSION_bio(BIO *bio, SSL_SESSION **out) { |
| 801 | uint8_t *data; |
| 802 | size_t len; |
| 803 | if (!BIO_read_asn1(bio, &data, &len, 1024 * 1024)) { |
| 804 | return 0; |
| 805 | } |
| 806 | bssl::UniquePtr<uint8_t> free_data(data); |
| 807 | const uint8_t *ptr = data; |
| 808 | return d2i_SSL_SESSION(out, &ptr, static_cast<long>(len)); |
| 809 | } |
| 810 |
| 811 | int i2d_SSL_SESSION_bio(BIO *bio, const SSL_SESSION *session) { |
| 812 | uint8_t *data; |
| 813 | size_t len; |
| 814 | if (!SSL_SESSION_to_bytes(session, &data, &len)) { |
| 815 | return 0; |
| 816 | } |
| 817 | bssl::UniquePtr<uint8_t> free_data(data); |
| 818 | return BIO_write_all(bio, data, len); |
| 819 | } |
| 820 |
| 821 | IMPLEMENT_PEM_rw(SSL_SESSION, SSL_SESSION, PEM_STRING_SSL_SESSION, SSL_SESSION) |
| 822 |
| 823 | SSL_SESSION *d2i_SSL_SESSION(SSL_SESSION **a, const uint8_t **pp, long length) { |
| 824 | if (length < 0) { |
| 825 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 826 | return NULL; |
| 827 | } |
| 828 |
| 829 | CBS cbs; |
| 830 | CBS_init(&cbs, *pp, length); |
| 831 |
| 832 | UniquePtr<SSL_SESSION> ret = SSL_SESSION_parse(&cbs, &ssl_crypto_x509_method, |
| 833 | NULL /* no buffer pool */); |
| 834 | if (!ret) { |
| 835 | return NULL; |
| 836 | } |
| 837 |
| 838 | if (a) { |
| 839 | SSL_SESSION_free(*a); |
| 840 | *a = ret.get(); |
| 841 | } |
| 842 | *pp = CBS_data(&cbs); |
| 843 | return ret.release(); |
| 844 | } |
| 845 |
| 846 | STACK_OF(X509_NAME) *SSL_dup_CA_list(STACK_OF(X509_NAME) *list) { |
| 847 | // TODO(https://crbug.com/boringssl/407): |X509_NAME_dup| should be const. |
| 848 | auto name_dup = [](const X509_NAME *name) { |
| 849 | return X509_NAME_dup(const_cast<X509_NAME *>(name)); |
| 850 | }; |
| 851 | return sk_X509_NAME_deep_copy(list, name_dup, X509_NAME_free); |
| 852 | } |
| 853 |
| 854 | static void set_client_CA_list(UniquePtr<STACK_OF(CRYPTO_BUFFER)> *ca_list, |
| 855 | const STACK_OF(X509_NAME) *name_list, |
| 856 | CRYPTO_BUFFER_POOL *pool) { |
| 857 | UniquePtr<STACK_OF(CRYPTO_BUFFER)> buffers(sk_CRYPTO_BUFFER_new_null()); |
| 858 | if (!buffers) { |
| 859 | return; |
| 860 | } |
| 861 |
| 862 | for (X509_NAME *name : name_list) { |
| 863 | uint8_t *outp = NULL; |
| 864 | int len = i2d_X509_NAME(name, &outp); |
| 865 | if (len < 0) { |
| 866 | return; |
| 867 | } |
| 868 |
| 869 | UniquePtr<CRYPTO_BUFFER> buffer(CRYPTO_BUFFER_new(outp, len, pool)); |
| 870 | OPENSSL_free(outp); |
| 871 | if (!buffer || !PushToStack(buffers.get(), std::move(buffer))) { |
| 872 | return; |
| 873 | } |
| 874 | } |
| 875 |
| 876 | *ca_list = std::move(buffers); |
| 877 | } |
| 878 |
| 879 | void SSL_set_client_CA_list(SSL *ssl, STACK_OF(X509_NAME) *name_list) { |
| 880 | check_ssl_x509_method(ssl); |
| 881 | if (!ssl->config) { |
| 882 | return; |
| 883 | } |
| 884 | ssl->ctx->x509_method->ssl_flush_cached_client_CA(ssl->config.get()); |
| 885 | set_client_CA_list(&ssl->config->client_CA, name_list, ssl->ctx->pool); |
| 886 | sk_X509_NAME_pop_free(name_list, X509_NAME_free); |
| 887 | } |
| 888 |
| 889 | void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list) { |
| 890 | check_ssl_ctx_x509_method(ctx); |
| 891 | ctx->x509_method->ssl_ctx_flush_cached_client_CA(ctx); |
| 892 | set_client_CA_list(&ctx->client_CA, name_list, ctx->pool); |
| 893 | sk_X509_NAME_pop_free(name_list, X509_NAME_free); |
| 894 | } |
| 895 |
| 896 | static STACK_OF(X509_NAME) *buffer_names_to_x509( |
| 897 | const STACK_OF(CRYPTO_BUFFER) *names, STACK_OF(X509_NAME) **cached) { |
| 898 | if (names == NULL) { |
| 899 | return NULL; |
| 900 | } |
| 901 |
| 902 | if (*cached != NULL) { |
| 903 | return *cached; |
| 904 | } |
| 905 |
| 906 | UniquePtr<STACK_OF(X509_NAME)> new_cache(sk_X509_NAME_new_null()); |
| 907 | if (!new_cache) { |
| 908 | return NULL; |
| 909 | } |
| 910 |
| 911 | for (const CRYPTO_BUFFER *buffer : names) { |
| 912 | const uint8_t *inp = CRYPTO_BUFFER_data(buffer); |
| 913 | UniquePtr<X509_NAME> name( |
| 914 | d2i_X509_NAME(nullptr, &inp, CRYPTO_BUFFER_len(buffer))); |
| 915 | if (!name || |
| 916 | inp != CRYPTO_BUFFER_data(buffer) + CRYPTO_BUFFER_len(buffer) || |
| 917 | !PushToStack(new_cache.get(), std::move(name))) { |
| 918 | return NULL; |
| 919 | } |
| 920 | } |
| 921 |
| 922 | *cached = new_cache.release(); |
| 923 | return *cached; |
| 924 | } |
| 925 |
| 926 | STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *ssl) { |
| 927 | check_ssl_x509_method(ssl); |
| 928 | if (!ssl->config) { |
| 929 | assert(ssl->config); |
| 930 | return NULL; |
| 931 | } |
| 932 | // For historical reasons, this function is used both to query configuration |
| 933 | // state on a server as well as handshake state on a client. However, whether |
| 934 | // |ssl| is a client or server is not known until explicitly configured with |
| 935 | // |SSL_set_connect_state|. If |do_handshake| is NULL, |ssl| is in an |
| 936 | // indeterminate mode and |ssl->server| is unset. |
| 937 | if (ssl->do_handshake != NULL && !ssl->server) { |
| 938 | if (ssl->s3->hs != NULL) { |
| 939 | return buffer_names_to_x509(ssl->s3->hs->ca_names.get(), |
| 940 | &ssl->s3->hs->cached_x509_ca_names); |
| 941 | } |
| 942 |
| 943 | return NULL; |
| 944 | } |
| 945 |
| 946 | if (ssl->config->client_CA != NULL) { |
| 947 | return buffer_names_to_x509( |
| 948 | ssl->config->client_CA.get(), |
| 949 | (STACK_OF(X509_NAME) **)&ssl->config->cached_x509_client_CA); |
| 950 | } |
| 951 | return SSL_CTX_get_client_CA_list(ssl->ctx.get()); |
| 952 | } |
| 953 |
| 954 | STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx) { |
| 955 | check_ssl_ctx_x509_method(ctx); |
| 956 | // This is a logically const operation that may be called on multiple threads, |
| 957 | // so it needs to lock around updating |cached_x509_client_CA|. |
| 958 | MutexWriteLock lock(const_cast<CRYPTO_MUTEX *>(&ctx->lock)); |
| 959 | return buffer_names_to_x509( |
| 960 | ctx->client_CA.get(), |
| 961 | const_cast<STACK_OF(X509_NAME) **>(&ctx->cached_x509_client_CA)); |
| 962 | } |
| 963 |
| 964 | static int add_client_CA(UniquePtr<STACK_OF(CRYPTO_BUFFER)> *names, X509 *x509, |
| 965 | CRYPTO_BUFFER_POOL *pool) { |
| 966 | if (x509 == NULL) { |
| 967 | return 0; |
| 968 | } |
| 969 |
| 970 | uint8_t *outp = NULL; |
| 971 | int len = i2d_X509_NAME(X509_get_subject_name(x509), &outp); |
| 972 | if (len < 0) { |
| 973 | return 0; |
| 974 | } |
| 975 |
| 976 | UniquePtr<CRYPTO_BUFFER> buffer(CRYPTO_BUFFER_new(outp, len, pool)); |
| 977 | OPENSSL_free(outp); |
| 978 | if (!buffer) { |
| 979 | return 0; |
| 980 | } |
| 981 |
| 982 | int alloced = 0; |
| 983 | if (*names == nullptr) { |
| 984 | names->reset(sk_CRYPTO_BUFFER_new_null()); |
| 985 | alloced = 1; |
| 986 |
| 987 | if (*names == NULL) { |
| 988 | return 0; |
| 989 | } |
| 990 | } |
| 991 |
| 992 | if (!PushToStack(names->get(), std::move(buffer))) { |
| 993 | if (alloced) { |
| 994 | names->reset(); |
| 995 | } |
| 996 | return 0; |
| 997 | } |
| 998 |
| 999 | return 1; |
| 1000 | } |
| 1001 |
| 1002 | int SSL_add_client_CA(SSL *ssl, X509 *x509) { |
| 1003 | check_ssl_x509_method(ssl); |
| 1004 | if (!ssl->config) { |
| 1005 | return 0; |
| 1006 | } |
| 1007 | if (!add_client_CA(&ssl->config->client_CA, x509, ssl->ctx->pool)) { |
| 1008 | return 0; |
| 1009 | } |
| 1010 |
| 1011 | ssl_crypto_x509_ssl_flush_cached_client_CA(ssl->config.get()); |
| 1012 | return 1; |
| 1013 | } |
| 1014 |
| 1015 | int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x509) { |
| 1016 | check_ssl_ctx_x509_method(ctx); |
| 1017 | if (!add_client_CA(&ctx->client_CA, x509, ctx->pool)) { |
| 1018 | return 0; |
| 1019 | } |
| 1020 |
| 1021 | ssl_crypto_x509_ssl_ctx_flush_cached_client_CA(ctx); |
| 1022 | return 1; |
| 1023 | } |
| 1024 |
| 1025 | static int do_client_cert_cb(SSL *ssl, void *arg) { |
| 1026 | // Should only be called during handshake, but check to be sure. |
| 1027 | BSSL_CHECK(ssl->config); |
| 1028 |
| 1029 | if (ssl->config->cert->legacy_credential->IsComplete() || |
| 1030 | ssl->ctx->client_cert_cb == nullptr) { |
| 1031 | return 1; |
| 1032 | } |
| 1033 |
| 1034 | X509 *x509 = NULL; |
| 1035 | EVP_PKEY *pkey = NULL; |
| 1036 | int ret = ssl->ctx->client_cert_cb(ssl, &x509, &pkey); |
| 1037 | if (ret < 0) { |
| 1038 | return -1; |
| 1039 | } |
| 1040 | UniquePtr<X509> free_x509(x509); |
| 1041 | UniquePtr<EVP_PKEY> free_pkey(pkey); |
| 1042 |
| 1043 | if (ret != 0) { |
| 1044 | if (!SSL_use_certificate(ssl, x509) || // |
| 1045 | !SSL_use_PrivateKey(ssl, pkey)) { |
| 1046 | return 0; |
| 1047 | } |
| 1048 | } |
| 1049 |
| 1050 | return 1; |
| 1051 | } |
| 1052 |
| 1053 | void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx, |
| 1054 | int (*cb)(SSL *ssl, X509 **out_x509, |
| 1055 | EVP_PKEY **out_pkey)) { |
| 1056 | check_ssl_ctx_x509_method(ctx); |
| 1057 | // Emulate the old client certificate callback with the new one. |
| 1058 | SSL_CTX_set_cert_cb(ctx, do_client_cert_cb, NULL); |
| 1059 | ctx->client_cert_cb = cb; |
| 1060 | } |
| 1061 |
| 1062 | static int set_cert_store(X509_STORE **store_ptr, X509_STORE *new_store, |
| 1063 | int take_ref) { |
| 1064 | X509_STORE_free(*store_ptr); |
| 1065 | *store_ptr = new_store; |
| 1066 |
| 1067 | if (new_store != NULL && take_ref) { |
| 1068 | X509_STORE_up_ref(new_store); |
| 1069 | } |
| 1070 |
| 1071 | return 1; |
| 1072 | } |
| 1073 |
| 1074 | int SSL_get_ex_data_X509_STORE_CTX_idx(void) { |
| 1075 | // The ex_data index to go from |X509_STORE_CTX| to |SSL| always uses the |
| 1076 | // reserved app_data slot. Before ex_data was introduced, app_data was used. |
| 1077 | // Avoid breaking any software which assumes |X509_STORE_CTX_get_app_data| |
| 1078 | // works. |
| 1079 | return 0; |
| 1080 | } |
| 1081 |
| 1082 | int SSL_CTX_set0_verify_cert_store(SSL_CTX *ctx, X509_STORE *store) { |
| 1083 | check_ssl_ctx_x509_method(ctx); |
| 1084 | return set_cert_store(&ctx->cert->verify_store, store, 0); |
| 1085 | } |
| 1086 |
| 1087 | int SSL_CTX_set1_verify_cert_store(SSL_CTX *ctx, X509_STORE *store) { |
| 1088 | check_ssl_ctx_x509_method(ctx); |
| 1089 | return set_cert_store(&ctx->cert->verify_store, store, 1); |
| 1090 | } |
| 1091 |
| 1092 | int SSL_set0_verify_cert_store(SSL *ssl, X509_STORE *store) { |
| 1093 | check_ssl_x509_method(ssl); |
| 1094 | if (!ssl->config) { |
| 1095 | return 0; |
| 1096 | } |
| 1097 | return set_cert_store(&ssl->config->cert->verify_store, store, 0); |
| 1098 | } |
| 1099 |
| 1100 | int SSL_set1_verify_cert_store(SSL *ssl, X509_STORE *store) { |
| 1101 | check_ssl_x509_method(ssl); |
| 1102 | if (!ssl->config) { |
| 1103 | return 0; |
| 1104 | } |
| 1105 | return set_cert_store(&ssl->config->cert->verify_store, store, 1); |
| 1106 | } |
| 1107 |
| 1108 | int SSL_set1_host(SSL *ssl, const char *hostname) { |
| 1109 | check_ssl_x509_method(ssl); |
| 1110 | if (!ssl->config) { |
| 1111 | return 0; |
| 1112 | } |
| 1113 | return X509_VERIFY_PARAM_set1_host(ssl->config->param, hostname, |
| 1114 | strlen(hostname)); |
| 1115 | } |
| 1116 |
| 1117 | void SSL_set_hostflags(SSL *ssl, unsigned flags) { |
| 1118 | check_ssl_x509_method(ssl); |
| 1119 | if (!ssl->config) { |
| 1120 | return; |
| 1121 | } |
| 1122 | X509_VERIFY_PARAM_set_hostflags(ssl->config->param, flags); |
| 1123 | } |
| 1124 |
| 1125 | int SSL_alert_from_verify_result(long result) { |
| 1126 | switch (result) { |
| 1127 | case X509_V_ERR_CERT_CHAIN_TOO_LONG: |
| 1128 | case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT: |
| 1129 | case X509_V_ERR_INVALID_CA: |
| 1130 | case X509_V_ERR_PATH_LENGTH_EXCEEDED: |
| 1131 | case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN: |
| 1132 | case X509_V_ERR_UNABLE_TO_GET_CRL: |
| 1133 | case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER: |
| 1134 | case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT: |
| 1135 | case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY: |
| 1136 | case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE: |
| 1137 | return SSL_AD_UNKNOWN_CA; |
| 1138 |
| 1139 | case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE: |
| 1140 | case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE: |
| 1141 | case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY: |
| 1142 | case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD: |
| 1143 | case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD: |
| 1144 | case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD: |
| 1145 | case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD: |
| 1146 | case X509_V_ERR_CERT_UNTRUSTED: |
| 1147 | case X509_V_ERR_CERT_REJECTED: |
| 1148 | case X509_V_ERR_HOSTNAME_MISMATCH: |
| 1149 | case X509_V_ERR_EMAIL_MISMATCH: |
| 1150 | case X509_V_ERR_IP_ADDRESS_MISMATCH: |
| 1151 | return SSL_AD_BAD_CERTIFICATE; |
| 1152 |
| 1153 | case X509_V_ERR_CERT_SIGNATURE_FAILURE: |
| 1154 | case X509_V_ERR_CRL_SIGNATURE_FAILURE: |
| 1155 | return SSL_AD_DECRYPT_ERROR; |
| 1156 |
| 1157 | case X509_V_ERR_CERT_HAS_EXPIRED: |
| 1158 | case X509_V_ERR_CERT_NOT_YET_VALID: |
| 1159 | case X509_V_ERR_CRL_HAS_EXPIRED: |
| 1160 | case X509_V_ERR_CRL_NOT_YET_VALID: |
| 1161 | return SSL_AD_CERTIFICATE_EXPIRED; |
| 1162 |
| 1163 | case X509_V_ERR_CERT_REVOKED: |
| 1164 | return SSL_AD_CERTIFICATE_REVOKED; |
| 1165 |
| 1166 | case X509_V_ERR_UNSPECIFIED: |
| 1167 | case X509_V_ERR_OUT_OF_MEM: |
| 1168 | case X509_V_ERR_INVALID_CALL: |
| 1169 | case X509_V_ERR_STORE_LOOKUP: |
| 1170 | return SSL_AD_INTERNAL_ERROR; |
| 1171 |
| 1172 | case X509_V_ERR_APPLICATION_VERIFICATION: |
| 1173 | return SSL_AD_HANDSHAKE_FAILURE; |
| 1174 |
| 1175 | case X509_V_ERR_INVALID_PURPOSE: |
| 1176 | return SSL_AD_UNSUPPORTED_CERTIFICATE; |
| 1177 |
| 1178 | default: |
| 1179 | return SSL_AD_CERTIFICATE_UNKNOWN; |
| 1180 | } |
| 1181 | } |
| 1182 |