Adam Langley | d1515a3 | 2017-01-23 16:46:16 -0800 | [diff] [blame] | 1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 2 | * All rights reserved. |
| 3 | * |
| 4 | * This package is an SSL implementation written |
| 5 | * by Eric Young (eay@cryptsoft.com). |
| 6 | * The implementation was written so as to conform with Netscapes SSL. |
| 7 | * |
| 8 | * This library is free for commercial and non-commercial use as long as |
| 9 | * the following conditions are aheared to. The following conditions |
| 10 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 12 | * included with this distribution is covered by the same copyright terms |
| 13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 14 | * |
| 15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 16 | * the code are not to be removed. |
| 17 | * If this package is used in a product, Eric Young should be given attribution |
| 18 | * as the author of the parts of the library used. |
| 19 | * This can be in the form of a textual message at program startup or |
| 20 | * in documentation (online or textual) provided with the package. |
| 21 | * |
| 22 | * Redistribution and use in source and binary forms, with or without |
| 23 | * modification, are permitted provided that the following conditions |
| 24 | * are met: |
| 25 | * 1. Redistributions of source code must retain the copyright |
| 26 | * notice, this list of conditions and the following disclaimer. |
| 27 | * 2. Redistributions in binary form must reproduce the above copyright |
| 28 | * notice, this list of conditions and the following disclaimer in the |
| 29 | * documentation and/or other materials provided with the distribution. |
| 30 | * 3. All advertising materials mentioning features or use of this software |
| 31 | * must display the following acknowledgement: |
| 32 | * "This product includes cryptographic software written by |
| 33 | * Eric Young (eay@cryptsoft.com)" |
| 34 | * The word 'cryptographic' can be left out if the rouines from the library |
| 35 | * being used are not cryptographic related :-). |
| 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 37 | * the apps directory (application code) you must include an acknowledgement: |
| 38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 39 | * |
| 40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 50 | * SUCH DAMAGE. |
| 51 | * |
| 52 | * The licence and distribution terms for any publically available version or |
| 53 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 54 | * copied and put under another distribution licence |
| 55 | * [including the GNU Public Licence.] |
| 56 | */ |
| 57 | /* ==================================================================== |
| 58 | * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. |
| 59 | * |
| 60 | * Redistribution and use in source and binary forms, with or without |
| 61 | * modification, are permitted provided that the following conditions |
| 62 | * are met: |
| 63 | * |
| 64 | * 1. Redistributions of source code must retain the above copyright |
| 65 | * notice, this list of conditions and the following disclaimer. |
| 66 | * |
| 67 | * 2. Redistributions in binary form must reproduce the above copyright |
| 68 | * notice, this list of conditions and the following disclaimer in |
| 69 | * the documentation and/or other materials provided with the |
| 70 | * distribution. |
| 71 | * |
| 72 | * 3. All advertising materials mentioning features or use of this |
| 73 | * software must display the following acknowledgment: |
| 74 | * "This product includes software developed by the OpenSSL Project |
| 75 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
| 76 | * |
| 77 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 78 | * endorse or promote products derived from this software without |
| 79 | * prior written permission. For written permission, please contact |
| 80 | * openssl-core@openssl.org. |
| 81 | * |
| 82 | * 5. Products derived from this software may not be called "OpenSSL" |
| 83 | * nor may "OpenSSL" appear in their names without prior written |
| 84 | * permission of the OpenSSL Project. |
| 85 | * |
| 86 | * 6. Redistributions of any form whatsoever must retain the following |
| 87 | * acknowledgment: |
| 88 | * "This product includes software developed by the OpenSSL Project |
| 89 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
| 90 | * |
| 91 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 92 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 93 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 94 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 95 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 96 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 97 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 98 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 99 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 100 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 101 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 102 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 103 | * ==================================================================== |
| 104 | * |
| 105 | * This product includes cryptographic software written by Eric Young |
| 106 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 107 | * Hudson (tjh@cryptsoft.com). |
| 108 | * |
| 109 | */ |
| 110 | /* ==================================================================== |
| 111 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. |
| 112 | * ECC cipher suite support in OpenSSL originally developed by |
| 113 | * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. |
| 114 | */ |
| 115 | /* ==================================================================== |
| 116 | * Copyright 2005 Nokia. All rights reserved. |
| 117 | * |
| 118 | * The portions of the attached software ("Contribution") is developed by |
| 119 | * Nokia Corporation and is licensed pursuant to the OpenSSL open source |
| 120 | * license. |
| 121 | * |
| 122 | * The Contribution, originally written by Mika Kousa and Pasi Eronen of |
| 123 | * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites |
| 124 | * support (see RFC 4279) to OpenSSL. |
| 125 | * |
| 126 | * No patent licenses or other rights except those expressly stated in |
| 127 | * the OpenSSL open source license shall be deemed granted or received |
| 128 | * expressly, by implication, estoppel, or otherwise. |
| 129 | * |
| 130 | * No assurances are provided by Nokia that the Contribution does not |
| 131 | * infringe the patent or other intellectual property rights of any third |
| 132 | * party or that the license provides you with all the necessary rights |
| 133 | * to make use of the Contribution. |
| 134 | * |
| 135 | * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN |
| 136 | * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA |
| 137 | * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY |
| 138 | * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR |
| 139 | * OTHERWISE. */ |
| 140 | |
| 141 | #include <openssl/ssl.h> |
| 142 | |
Adam Langley | 3509dac | 2017-02-01 11:59:18 -0800 | [diff] [blame^] | 143 | #include <assert.h> |
| 144 | |
Adam Langley | d1515a3 | 2017-01-23 16:46:16 -0800 | [diff] [blame] | 145 | #include <openssl/stack.h> |
| 146 | #include <openssl/x509.h> |
Adam Langley | 3509dac | 2017-02-01 11:59:18 -0800 | [diff] [blame^] | 147 | #include <openssl/x509v3.h> |
Adam Langley | d1515a3 | 2017-01-23 16:46:16 -0800 | [diff] [blame] | 148 | #include <openssl/x509_vfy.h> |
| 149 | |
| 150 | #include "internal.h" |
| 151 | |
| 152 | |
| 153 | X509 *SSL_get_peer_certificate(const SSL *ssl) { |
| 154 | if (ssl == NULL) { |
| 155 | return NULL; |
| 156 | } |
| 157 | SSL_SESSION *session = SSL_get_session(ssl); |
| 158 | if (session == NULL || session->x509_peer == NULL) { |
| 159 | return NULL; |
| 160 | } |
| 161 | X509_up_ref(session->x509_peer); |
| 162 | return session->x509_peer; |
| 163 | } |
| 164 | |
| 165 | STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *ssl) { |
| 166 | if (ssl == NULL) { |
| 167 | return NULL; |
| 168 | } |
| 169 | SSL_SESSION *session = SSL_get_session(ssl); |
| 170 | if (session == NULL || |
| 171 | session->x509_chain == NULL) { |
| 172 | return NULL; |
| 173 | } |
| 174 | |
| 175 | if (!ssl->server) { |
| 176 | return session->x509_chain; |
| 177 | } |
| 178 | |
| 179 | /* OpenSSL historically didn't include the leaf certificate in the returned |
| 180 | * certificate chain, but only for servers. */ |
| 181 | if (session->x509_chain_without_leaf == NULL) { |
| 182 | session->x509_chain_without_leaf = sk_X509_new_null(); |
| 183 | if (session->x509_chain_without_leaf == NULL) { |
| 184 | return NULL; |
| 185 | } |
| 186 | |
| 187 | for (size_t i = 1; i < sk_X509_num(session->x509_chain); i++) { |
| 188 | X509 *cert = sk_X509_value(session->x509_chain, i); |
| 189 | if (!sk_X509_push(session->x509_chain_without_leaf, cert)) { |
| 190 | sk_X509_pop_free(session->x509_chain_without_leaf, X509_free); |
| 191 | session->x509_chain_without_leaf = NULL; |
| 192 | return NULL; |
| 193 | } |
| 194 | X509_up_ref(cert); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | return session->x509_chain_without_leaf; |
| 199 | } |
| 200 | |
| 201 | STACK_OF(X509) *SSL_get_peer_full_cert_chain(const SSL *ssl) { |
| 202 | SSL_SESSION *session = SSL_get_session(ssl); |
| 203 | if (session == NULL) { |
| 204 | return NULL; |
| 205 | } |
| 206 | |
| 207 | return session->x509_chain; |
| 208 | } |
| 209 | |
| 210 | int SSL_CTX_set_purpose(SSL_CTX *ctx, int purpose) { |
| 211 | return X509_VERIFY_PARAM_set_purpose(ctx->param, purpose); |
| 212 | } |
| 213 | |
| 214 | int SSL_set_purpose(SSL *ssl, int purpose) { |
| 215 | return X509_VERIFY_PARAM_set_purpose(ssl->param, purpose); |
| 216 | } |
| 217 | |
| 218 | int SSL_CTX_set_trust(SSL_CTX *ctx, int trust) { |
| 219 | return X509_VERIFY_PARAM_set_trust(ctx->param, trust); |
| 220 | } |
| 221 | |
| 222 | int SSL_set_trust(SSL *ssl, int trust) { |
| 223 | return X509_VERIFY_PARAM_set_trust(ssl->param, trust); |
| 224 | } |
| 225 | |
| 226 | int SSL_CTX_set1_param(SSL_CTX *ctx, const X509_VERIFY_PARAM *param) { |
| 227 | return X509_VERIFY_PARAM_set1(ctx->param, param); |
| 228 | } |
| 229 | |
| 230 | int SSL_set1_param(SSL *ssl, const X509_VERIFY_PARAM *param) { |
| 231 | return X509_VERIFY_PARAM_set1(ssl->param, param); |
| 232 | } |
| 233 | |
| 234 | X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) { return ctx->param; } |
| 235 | |
| 236 | X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) { return ssl->param; } |
| 237 | |
| 238 | int SSL_get_verify_depth(const SSL *ssl) { |
| 239 | return X509_VERIFY_PARAM_get_depth(ssl->param); |
| 240 | } |
| 241 | |
| 242 | int (*SSL_get_verify_callback(const SSL *ssl))(int, X509_STORE_CTX *) { |
| 243 | return ssl->verify_callback; |
| 244 | } |
| 245 | |
| 246 | int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) { return ctx->verify_mode; } |
| 247 | |
| 248 | int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) { |
| 249 | return X509_VERIFY_PARAM_get_depth(ctx->param); |
| 250 | } |
| 251 | |
| 252 | int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))( |
| 253 | int ok, X509_STORE_CTX *store_ctx) { |
| 254 | return ctx->default_verify_callback; |
| 255 | } |
| 256 | |
| 257 | void SSL_set_verify(SSL *ssl, int mode, |
| 258 | int (*callback)(int ok, X509_STORE_CTX *store_ctx)) { |
| 259 | ssl->verify_mode = mode; |
| 260 | if (callback != NULL) { |
| 261 | ssl->verify_callback = callback; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | void SSL_set_verify_depth(SSL *ssl, int depth) { |
| 266 | X509_VERIFY_PARAM_set_depth(ssl->param, depth); |
| 267 | } |
| 268 | |
| 269 | void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx, |
| 270 | int (*cb)(X509_STORE_CTX *store_ctx, |
| 271 | void *arg), |
| 272 | void *arg) { |
| 273 | ctx->app_verify_callback = cb; |
| 274 | ctx->app_verify_arg = arg; |
| 275 | } |
| 276 | |
| 277 | void SSL_CTX_set_verify(SSL_CTX *ctx, int mode, |
| 278 | int (*cb)(int, X509_STORE_CTX *)) { |
| 279 | ctx->verify_mode = mode; |
| 280 | ctx->default_verify_callback = cb; |
| 281 | } |
| 282 | |
| 283 | void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) { |
| 284 | X509_VERIFY_PARAM_set_depth(ctx->param, depth); |
| 285 | } |
| 286 | |
Adam Langley | d1515a3 | 2017-01-23 16:46:16 -0800 | [diff] [blame] | 287 | int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) { |
| 288 | return X509_STORE_set_default_paths(ctx->cert_store); |
| 289 | } |
| 290 | |
| 291 | int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *ca_file, |
| 292 | const char *ca_dir) { |
| 293 | return X509_STORE_load_locations(ctx->cert_store, ca_file, ca_dir); |
| 294 | } |
| 295 | |
| 296 | void SSL_set_verify_result(SSL *ssl, long result) { |
| 297 | if (result != X509_V_OK) { |
| 298 | abort(); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | long SSL_get_verify_result(const SSL *ssl) { |
| 303 | SSL_SESSION *session = SSL_get_session(ssl); |
| 304 | if (session == NULL) { |
| 305 | return X509_V_ERR_INVALID_CALL; |
| 306 | } |
| 307 | return session->verify_result; |
| 308 | } |
| 309 | |
| 310 | X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) { |
| 311 | return ctx->cert_store; |
| 312 | } |
| 313 | |
| 314 | void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) { |
| 315 | X509_STORE_free(ctx->cert_store); |
| 316 | ctx->cert_store = store; |
| 317 | } |
Adam Langley | 3509dac | 2017-02-01 11:59:18 -0800 | [diff] [blame^] | 318 | |
| 319 | static void ssl_crypto_x509_flush_cached_leaf(CERT *cert) { |
| 320 | X509_free(cert->x509_leaf); |
| 321 | cert->x509_leaf = NULL; |
| 322 | } |
| 323 | |
| 324 | static void ssl_crypto_x509_flush_cached_chain(CERT *cert) { |
| 325 | sk_X509_pop_free(cert->x509_chain, X509_free); |
| 326 | cert->x509_chain = NULL; |
| 327 | } |
| 328 | |
| 329 | static void ssl_crypto_x509_clear(CERT *cert) { |
| 330 | ssl_crypto_x509_flush_cached_leaf(cert); |
| 331 | ssl_crypto_x509_flush_cached_chain(cert); |
| 332 | |
| 333 | X509_free(cert->x509_stash); |
| 334 | cert->x509_stash = NULL; |
| 335 | } |
| 336 | |
| 337 | const SSL_X509_METHOD ssl_crypto_x509_method = { |
| 338 | ssl_crypto_x509_clear, |
| 339 | ssl_crypto_x509_flush_cached_chain, |
| 340 | ssl_crypto_x509_flush_cached_leaf, |
| 341 | }; |
| 342 | |
| 343 | /* x509_to_buffer returns a |CRYPTO_BUFFER| that contains the serialised |
| 344 | * contents of |x509|. */ |
| 345 | static CRYPTO_BUFFER *x509_to_buffer(X509 *x509) { |
| 346 | uint8_t *buf = NULL; |
| 347 | int cert_len = i2d_X509(x509, &buf); |
| 348 | if (cert_len <= 0) { |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | CRYPTO_BUFFER *buffer = CRYPTO_BUFFER_new(buf, cert_len, NULL); |
| 353 | OPENSSL_free(buf); |
| 354 | |
| 355 | return buffer; |
| 356 | } |
| 357 | |
| 358 | static int ssl_use_certificate(CERT *cert, X509 *x) { |
| 359 | if (x == NULL) { |
| 360 | OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER); |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | CRYPTO_BUFFER *buffer = x509_to_buffer(x); |
| 365 | if (buffer == NULL) { |
| 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | const int ok = ssl_set_cert(cert, buffer); |
| 370 | CRYPTO_BUFFER_free(buffer); |
| 371 | return ok; |
| 372 | } |
| 373 | |
| 374 | int SSL_use_certificate(SSL *ssl, X509 *x) { |
| 375 | return ssl_use_certificate(ssl->cert, x); |
| 376 | } |
| 377 | |
| 378 | int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x) { |
| 379 | return ssl_use_certificate(ctx->cert, x); |
| 380 | } |
| 381 | |
| 382 | /* ssl_cert_cache_leaf_cert sets |cert->x509_leaf|, if currently NULL, from the |
| 383 | * first element of |cert->chain|. */ |
| 384 | static int ssl_cert_cache_leaf_cert(CERT *cert) { |
| 385 | assert(cert->x509_method); |
| 386 | |
| 387 | if (cert->x509_leaf != NULL || |
| 388 | cert->chain == NULL) { |
| 389 | return 1; |
| 390 | } |
| 391 | |
| 392 | CRYPTO_BUFFER *leaf = sk_CRYPTO_BUFFER_value(cert->chain, 0); |
| 393 | if (!leaf) { |
| 394 | return 1; |
| 395 | } |
| 396 | |
| 397 | cert->x509_leaf = X509_parse_from_buffer(leaf); |
| 398 | return cert->x509_leaf != NULL; |
| 399 | } |
| 400 | |
| 401 | static X509 *ssl_cert_get0_leaf(CERT *cert) { |
| 402 | if (cert->x509_leaf == NULL && |
| 403 | !ssl_cert_cache_leaf_cert(cert)) { |
| 404 | return NULL; |
| 405 | } |
| 406 | |
| 407 | return cert->x509_leaf; |
| 408 | } |
| 409 | |
| 410 | X509 *SSL_get_certificate(const SSL *ssl) { |
| 411 | return ssl_cert_get0_leaf(ssl->cert); |
| 412 | } |
| 413 | |
| 414 | X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) { |
| 415 | return ssl_cert_get0_leaf(ctx->cert); |
| 416 | } |
| 417 | |
| 418 | /* new_leafless_chain returns a fresh stack of buffers set to {NULL}. */ |
| 419 | static STACK_OF(CRYPTO_BUFFER) *new_leafless_chain(void) { |
| 420 | STACK_OF(CRYPTO_BUFFER) *chain = sk_CRYPTO_BUFFER_new_null(); |
| 421 | if (chain == NULL) { |
| 422 | return NULL; |
| 423 | } |
| 424 | |
| 425 | if (!sk_CRYPTO_BUFFER_push(chain, NULL)) { |
| 426 | sk_CRYPTO_BUFFER_free(chain); |
| 427 | return NULL; |
| 428 | } |
| 429 | |
| 430 | return chain; |
| 431 | } |
| 432 | |
| 433 | /* ssl_cert_set_chain sets elements 1.. of |cert->chain| to the serialised |
| 434 | * forms of elements of |chain|. It returns one on success or zero on error, in |
| 435 | * which case no change to |cert->chain| is made. It preverses the existing |
| 436 | * leaf from |cert->chain|, if any. */ |
| 437 | static int ssl_cert_set_chain(CERT *cert, STACK_OF(X509) *chain) { |
| 438 | STACK_OF(CRYPTO_BUFFER) *new_chain = NULL; |
| 439 | |
| 440 | if (cert->chain != NULL) { |
| 441 | new_chain = sk_CRYPTO_BUFFER_new_null(); |
| 442 | if (new_chain == NULL) { |
| 443 | return 0; |
| 444 | } |
| 445 | |
| 446 | CRYPTO_BUFFER *leaf = sk_CRYPTO_BUFFER_value(cert->chain, 0); |
| 447 | if (!sk_CRYPTO_BUFFER_push(new_chain, leaf)) { |
| 448 | goto err; |
| 449 | } |
| 450 | /* |leaf| might be NULL if it's a “leafless” chain. */ |
| 451 | if (leaf != NULL) { |
| 452 | CRYPTO_BUFFER_up_ref(leaf); |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | for (size_t i = 0; i < sk_X509_num(chain); i++) { |
| 457 | if (new_chain == NULL) { |
| 458 | new_chain = new_leafless_chain(); |
| 459 | if (new_chain == NULL) { |
| 460 | goto err; |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | CRYPTO_BUFFER *buffer = x509_to_buffer(sk_X509_value(chain, i)); |
| 465 | if (buffer == NULL || |
| 466 | !sk_CRYPTO_BUFFER_push(new_chain, buffer)) { |
| 467 | CRYPTO_BUFFER_free(buffer); |
| 468 | goto err; |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | sk_CRYPTO_BUFFER_pop_free(cert->chain, CRYPTO_BUFFER_free); |
| 473 | cert->chain = new_chain; |
| 474 | |
| 475 | return 1; |
| 476 | |
| 477 | err: |
| 478 | sk_CRYPTO_BUFFER_pop_free(new_chain, CRYPTO_BUFFER_free); |
| 479 | return 0; |
| 480 | } |
| 481 | |
| 482 | static int ssl_cert_set0_chain(CERT *cert, STACK_OF(X509) *chain) { |
| 483 | if (!ssl_cert_set_chain(cert, chain)) { |
| 484 | return 0; |
| 485 | } |
| 486 | |
| 487 | sk_X509_pop_free(chain, X509_free); |
| 488 | ssl_crypto_x509_flush_cached_chain(cert); |
| 489 | return 1; |
| 490 | } |
| 491 | |
| 492 | static int ssl_cert_set1_chain(CERT *cert, STACK_OF(X509) *chain) { |
| 493 | if (!ssl_cert_set_chain(cert, chain)) { |
| 494 | return 0; |
| 495 | } |
| 496 | |
| 497 | ssl_crypto_x509_flush_cached_chain(cert); |
| 498 | return 1; |
| 499 | } |
| 500 | |
| 501 | static int ssl_cert_append_cert(CERT *cert, X509 *x509) { |
| 502 | assert(cert->x509_method); |
| 503 | |
| 504 | CRYPTO_BUFFER *buffer = x509_to_buffer(x509); |
| 505 | if (buffer == NULL) { |
| 506 | return 0; |
| 507 | } |
| 508 | |
| 509 | if (cert->chain != NULL) { |
| 510 | if (!sk_CRYPTO_BUFFER_push(cert->chain, buffer)) { |
| 511 | CRYPTO_BUFFER_free(buffer); |
| 512 | return 0; |
| 513 | } |
| 514 | |
| 515 | return 1; |
| 516 | } |
| 517 | |
| 518 | cert->chain = new_leafless_chain(); |
| 519 | if (cert->chain == NULL || |
| 520 | !sk_CRYPTO_BUFFER_push(cert->chain, buffer)) { |
| 521 | CRYPTO_BUFFER_free(buffer); |
| 522 | sk_CRYPTO_BUFFER_free(cert->chain); |
| 523 | cert->chain = NULL; |
| 524 | return 0; |
| 525 | } |
| 526 | |
| 527 | return 1; |
| 528 | } |
| 529 | |
| 530 | static int ssl_cert_add0_chain_cert(CERT *cert, X509 *x509) { |
| 531 | if (!ssl_cert_append_cert(cert, x509)) { |
| 532 | return 0; |
| 533 | } |
| 534 | |
| 535 | X509_free(cert->x509_stash); |
| 536 | cert->x509_stash = x509; |
| 537 | ssl_crypto_x509_flush_cached_chain(cert); |
| 538 | return 1; |
| 539 | } |
| 540 | |
| 541 | static int ssl_cert_add1_chain_cert(CERT *cert, X509 *x509) { |
| 542 | if (!ssl_cert_append_cert(cert, x509)) { |
| 543 | return 0; |
| 544 | } |
| 545 | |
| 546 | ssl_crypto_x509_flush_cached_chain(cert); |
| 547 | return 1; |
| 548 | } |
| 549 | |
| 550 | int SSL_CTX_set0_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) { |
| 551 | return ssl_cert_set0_chain(ctx->cert, chain); |
| 552 | } |
| 553 | |
| 554 | int SSL_CTX_set1_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) { |
| 555 | return ssl_cert_set1_chain(ctx->cert, chain); |
| 556 | } |
| 557 | |
| 558 | int SSL_set0_chain(SSL *ssl, STACK_OF(X509) *chain) { |
| 559 | return ssl_cert_set0_chain(ssl->cert, chain); |
| 560 | } |
| 561 | |
| 562 | int SSL_set1_chain(SSL *ssl, STACK_OF(X509) *chain) { |
| 563 | return ssl_cert_set1_chain(ssl->cert, chain); |
| 564 | } |
| 565 | |
| 566 | int SSL_CTX_add0_chain_cert(SSL_CTX *ctx, X509 *x509) { |
| 567 | return ssl_cert_add0_chain_cert(ctx->cert, x509); |
| 568 | } |
| 569 | |
| 570 | int SSL_CTX_add1_chain_cert(SSL_CTX *ctx, X509 *x509) { |
| 571 | return ssl_cert_add1_chain_cert(ctx->cert, x509); |
| 572 | } |
| 573 | |
| 574 | int SSL_CTX_add_extra_chain_cert(SSL_CTX *ctx, X509 *x509) { |
| 575 | return SSL_CTX_add0_chain_cert(ctx, x509); |
| 576 | } |
| 577 | |
| 578 | int SSL_add0_chain_cert(SSL *ssl, X509 *x509) { |
| 579 | return ssl_cert_add0_chain_cert(ssl->cert, x509); |
| 580 | } |
| 581 | |
| 582 | int SSL_add1_chain_cert(SSL *ssl, X509 *x509) { |
| 583 | return ssl_cert_add1_chain_cert(ssl->cert, x509); |
| 584 | } |
| 585 | |
| 586 | int SSL_CTX_clear_chain_certs(SSL_CTX *ctx) { |
| 587 | return SSL_CTX_set0_chain(ctx, NULL); |
| 588 | } |
| 589 | |
| 590 | int SSL_CTX_clear_extra_chain_certs(SSL_CTX *ctx) { |
| 591 | return SSL_CTX_clear_chain_certs(ctx); |
| 592 | } |
| 593 | |
| 594 | int SSL_clear_chain_certs(SSL *ssl) { |
| 595 | return SSL_set0_chain(ssl, NULL); |
| 596 | } |
| 597 | |
| 598 | int ssl_auto_chain_if_needed(SSL *ssl) { |
| 599 | /* Only build a chain if there are no intermediates configured and the feature |
| 600 | * isn't disabled. */ |
| 601 | if ((ssl->mode & SSL_MODE_NO_AUTO_CHAIN) || |
| 602 | !ssl_has_certificate(ssl) || |
| 603 | ssl->cert->chain == NULL || |
| 604 | sk_CRYPTO_BUFFER_num(ssl->cert->chain) > 1) { |
| 605 | return 1; |
| 606 | } |
| 607 | |
| 608 | X509 *leaf = |
| 609 | X509_parse_from_buffer(sk_CRYPTO_BUFFER_value(ssl->cert->chain, 0)); |
| 610 | if (!leaf) { |
| 611 | OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB); |
| 612 | return 0; |
| 613 | } |
| 614 | |
| 615 | X509_STORE_CTX ctx; |
| 616 | if (!X509_STORE_CTX_init(&ctx, ssl->ctx->cert_store, leaf, NULL)) { |
| 617 | X509_free(leaf); |
| 618 | OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB); |
| 619 | return 0; |
| 620 | } |
| 621 | |
| 622 | /* Attempt to build a chain, ignoring the result. */ |
| 623 | X509_verify_cert(&ctx); |
| 624 | X509_free(leaf); |
| 625 | ERR_clear_error(); |
| 626 | |
| 627 | /* Remove the leaf from the generated chain. */ |
| 628 | X509_free(sk_X509_shift(ctx.chain)); |
| 629 | |
| 630 | const int ok = ssl_cert_set_chain(ssl->cert, ctx.chain); |
| 631 | X509_STORE_CTX_cleanup(&ctx); |
| 632 | if (!ok) { |
| 633 | return 0; |
| 634 | } |
| 635 | |
| 636 | ssl_crypto_x509_flush_cached_chain(ssl->cert); |
| 637 | |
| 638 | return 1; |
| 639 | } |
| 640 | |
| 641 | /* ssl_cert_cache_chain_certs fills in |cert->x509_chain| from elements 1.. of |
| 642 | * |cert->chain|. */ |
| 643 | static int ssl_cert_cache_chain_certs(CERT *cert) { |
| 644 | assert(cert->x509_method); |
| 645 | |
| 646 | if (cert->x509_chain != NULL || |
| 647 | cert->chain == NULL || |
| 648 | sk_CRYPTO_BUFFER_num(cert->chain) < 2) { |
| 649 | return 1; |
| 650 | } |
| 651 | |
| 652 | STACK_OF(X509) *chain = sk_X509_new_null(); |
| 653 | if (chain == NULL) { |
| 654 | return 0; |
| 655 | } |
| 656 | |
| 657 | for (size_t i = 1; i < sk_CRYPTO_BUFFER_num(cert->chain); i++) { |
| 658 | CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(cert->chain, i); |
| 659 | X509 *x509 = X509_parse_from_buffer(buffer); |
| 660 | if (x509 == NULL || |
| 661 | !sk_X509_push(chain, x509)) { |
| 662 | X509_free(x509); |
| 663 | goto err; |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | cert->x509_chain = chain; |
| 668 | return 1; |
| 669 | |
| 670 | err: |
| 671 | sk_X509_pop_free(chain, X509_free); |
| 672 | return 0; |
| 673 | } |
| 674 | |
| 675 | int SSL_CTX_get0_chain_certs(const SSL_CTX *ctx, STACK_OF(X509) **out_chain) { |
| 676 | if (!ssl_cert_cache_chain_certs(ctx->cert)) { |
| 677 | *out_chain = NULL; |
| 678 | return 0; |
| 679 | } |
| 680 | |
| 681 | *out_chain = ctx->cert->x509_chain; |
| 682 | return 1; |
| 683 | } |
| 684 | |
| 685 | int SSL_CTX_get_extra_chain_certs(const SSL_CTX *ctx, |
| 686 | STACK_OF(X509) **out_chain) { |
| 687 | return SSL_CTX_get0_chain_certs(ctx, out_chain); |
| 688 | } |
| 689 | |
| 690 | int SSL_get0_chain_certs(const SSL *ssl, STACK_OF(X509) **out_chain) { |
| 691 | if (!ssl_cert_cache_chain_certs(ssl->cert)) { |
| 692 | *out_chain = NULL; |
| 693 | return 0; |
| 694 | } |
| 695 | |
| 696 | *out_chain = ssl->cert->x509_chain; |
| 697 | return 1; |
| 698 | } |