Home | History | Annotate | Download | only in ssl
      1 /* Copyright (C) 1995-1998 Eric Young (eay (at) cryptsoft.com)
      2  * All rights reserved.
      3  *
      4  * This package is an SSL implementation written
      5  * by Eric Young (eay (at) 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 (at) 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 (at) 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 (at) 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 (at) 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 (at) cryptsoft.com).  This product includes software written by Tim
    107  * Hudson (tjh (at) 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 
    143 #include <assert.h>
    144 
    145 #include <openssl/asn1.h>
    146 #include <openssl/bytestring.h>
    147 #include <openssl/err.h>
    148 #include <openssl/pem.h>
    149 #include <openssl/stack.h>
    150 #include <openssl/x509.h>
    151 #include <openssl/x509v3.h>
    152 #include <openssl/x509_vfy.h>
    153 
    154 #include "internal.h"
    155 #include "../crypto/internal.h"
    156 
    157 
    158 /* check_ssl_x509_method asserts that |ssl| has the X509-based method
    159  * installed. Calling an X509-based method on an |ssl| with a different method
    160  * will likely misbehave and possibly crash or leak memory. */
    161 static void check_ssl_x509_method(const SSL *ssl) {
    162   assert(ssl == NULL || ssl->ctx->x509_method == &ssl_crypto_x509_method);
    163 }
    164 
    165 /* check_ssl_ctx_x509_method acts like |check_ssl_x509_method|, but for an
    166  * |SSL_CTX|. */
    167 static void check_ssl_ctx_x509_method(const SSL_CTX *ctx) {
    168   assert(ctx == NULL || ctx->x509_method == &ssl_crypto_x509_method);
    169 }
    170 
    171 X509 *SSL_get_peer_certificate(const SSL *ssl) {
    172   check_ssl_x509_method(ssl);
    173   if (ssl == NULL) {
    174     return NULL;
    175   }
    176   SSL_SESSION *session = SSL_get_session(ssl);
    177   if (session == NULL || session->x509_peer == NULL) {
    178     return NULL;
    179   }
    180   X509_up_ref(session->x509_peer);
    181   return session->x509_peer;
    182 }
    183 
    184 STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *ssl) {
    185   check_ssl_x509_method(ssl);
    186   if (ssl == NULL) {
    187     return NULL;
    188   }
    189   SSL_SESSION *session = SSL_get_session(ssl);
    190   if (session == NULL ||
    191       session->x509_chain == NULL) {
    192     return NULL;
    193   }
    194 
    195   if (!ssl->server) {
    196     return session->x509_chain;
    197   }
    198 
    199   /* OpenSSL historically didn't include the leaf certificate in the returned
    200    * certificate chain, but only for servers. */
    201   if (session->x509_chain_without_leaf == NULL) {
    202     session->x509_chain_without_leaf = sk_X509_new_null();
    203     if (session->x509_chain_without_leaf == NULL) {
    204       return NULL;
    205     }
    206 
    207     for (size_t i = 1; i < sk_X509_num(session->x509_chain); i++) {
    208       X509 *cert = sk_X509_value(session->x509_chain, i);
    209       if (!sk_X509_push(session->x509_chain_without_leaf, cert)) {
    210         sk_X509_pop_free(session->x509_chain_without_leaf, X509_free);
    211         session->x509_chain_without_leaf = NULL;
    212         return NULL;
    213       }
    214       X509_up_ref(cert);
    215     }
    216   }
    217 
    218   return session->x509_chain_without_leaf;
    219 }
    220 
    221 STACK_OF(X509) *SSL_get_peer_full_cert_chain(const SSL *ssl) {
    222   check_ssl_x509_method(ssl);
    223   SSL_SESSION *session = SSL_get_session(ssl);
    224   if (session == NULL) {
    225     return NULL;
    226   }
    227 
    228   return session->x509_chain;
    229 }
    230 
    231 int SSL_CTX_set_purpose(SSL_CTX *ctx, int purpose) {
    232   check_ssl_ctx_x509_method(ctx);
    233   return X509_VERIFY_PARAM_set_purpose(ctx->param, purpose);
    234 }
    235 
    236 int SSL_set_purpose(SSL *ssl, int purpose) {
    237   check_ssl_x509_method(ssl);
    238   return X509_VERIFY_PARAM_set_purpose(ssl->param, purpose);
    239 }
    240 
    241 int SSL_CTX_set_trust(SSL_CTX *ctx, int trust) {
    242   check_ssl_ctx_x509_method(ctx);
    243   return X509_VERIFY_PARAM_set_trust(ctx->param, trust);
    244 }
    245 
    246 int SSL_set_trust(SSL *ssl, int trust) {
    247   check_ssl_x509_method(ssl);
    248   return X509_VERIFY_PARAM_set_trust(ssl->param, trust);
    249 }
    250 
    251 int SSL_CTX_set1_param(SSL_CTX *ctx, const X509_VERIFY_PARAM *param) {
    252   check_ssl_ctx_x509_method(ctx);
    253   return X509_VERIFY_PARAM_set1(ctx->param, param);
    254 }
    255 
    256 int SSL_set1_param(SSL *ssl, const X509_VERIFY_PARAM *param) {
    257   check_ssl_x509_method(ssl);
    258   return X509_VERIFY_PARAM_set1(ssl->param, param);
    259 }
    260 
    261 X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) {
    262   check_ssl_ctx_x509_method(ctx);
    263   return ctx->param;
    264 }
    265 
    266 X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) {
    267   check_ssl_x509_method(ssl);
    268   return ssl->param;
    269 }
    270 
    271 int SSL_get_verify_depth(const SSL *ssl) {
    272   check_ssl_x509_method(ssl);
    273   return X509_VERIFY_PARAM_get_depth(ssl->param);
    274 }
    275 
    276 int (*SSL_get_verify_callback(const SSL *ssl))(int, X509_STORE_CTX *) {
    277   check_ssl_x509_method(ssl);
    278   return ssl->verify_callback;
    279 }
    280 
    281 int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) {
    282   check_ssl_ctx_x509_method(ctx);
    283   return ctx->verify_mode;
    284 }
    285 
    286 int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) {
    287   check_ssl_ctx_x509_method(ctx);
    288   return X509_VERIFY_PARAM_get_depth(ctx->param);
    289 }
    290 
    291 int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(
    292     int ok, X509_STORE_CTX *store_ctx) {
    293   check_ssl_ctx_x509_method(ctx);
    294   return ctx->default_verify_callback;
    295 }
    296 
    297 void SSL_set_verify(SSL *ssl, int mode,
    298                     int (*callback)(int ok, X509_STORE_CTX *store_ctx)) {
    299   check_ssl_x509_method(ssl);
    300   ssl->verify_mode = mode;
    301   if (callback != NULL) {
    302     ssl->verify_callback = callback;
    303   }
    304 }
    305 
    306 void SSL_set_verify_depth(SSL *ssl, int depth) {
    307   check_ssl_x509_method(ssl);
    308   X509_VERIFY_PARAM_set_depth(ssl->param, depth);
    309 }
    310 
    311 void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
    312                                       int (*cb)(X509_STORE_CTX *store_ctx,
    313                                                 void *arg),
    314                                       void *arg) {
    315   check_ssl_ctx_x509_method(ctx);
    316   ctx->app_verify_callback = cb;
    317   ctx->app_verify_arg = arg;
    318 }
    319 
    320 void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
    321                         int (*cb)(int, X509_STORE_CTX *)) {
    322   check_ssl_ctx_x509_method(ctx);
    323   ctx->verify_mode = mode;
    324   ctx->default_verify_callback = cb;
    325 }
    326 
    327 void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
    328   check_ssl_ctx_x509_method(ctx);
    329   X509_VERIFY_PARAM_set_depth(ctx->param, depth);
    330 }
    331 
    332 int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
    333   check_ssl_ctx_x509_method(ctx);
    334   return X509_STORE_set_default_paths(ctx->cert_store);
    335 }
    336 
    337 int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *ca_file,
    338                                   const char *ca_dir) {
    339   check_ssl_ctx_x509_method(ctx);
    340   return X509_STORE_load_locations(ctx->cert_store, ca_file, ca_dir);
    341 }
    342 
    343 void SSL_set_verify_result(SSL *ssl, long result) {
    344   check_ssl_x509_method(ssl);
    345   if (result != X509_V_OK) {
    346     abort();
    347   }
    348 }
    349 
    350 long SSL_get_verify_result(const SSL *ssl) {
    351   check_ssl_x509_method(ssl);
    352   SSL_SESSION *session = SSL_get_session(ssl);
    353   if (session == NULL) {
    354     return X509_V_ERR_INVALID_CALL;
    355   }
    356   return session->verify_result;
    357 }
    358 
    359 X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
    360   check_ssl_ctx_x509_method(ctx);
    361   return ctx->cert_store;
    362 }
    363 
    364 void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
    365   check_ssl_ctx_x509_method(ctx);
    366   X509_STORE_free(ctx->cert_store);
    367   ctx->cert_store = store;
    368 }
    369 
    370 /* x509_to_buffer returns a |CRYPTO_BUFFER| that contains the serialised
    371  * contents of |x509|. */
    372 static CRYPTO_BUFFER *x509_to_buffer(X509 *x509) {
    373   uint8_t *buf = NULL;
    374   int cert_len = i2d_X509(x509, &buf);
    375   if (cert_len <= 0) {
    376     return 0;
    377   }
    378 
    379   CRYPTO_BUFFER *buffer = CRYPTO_BUFFER_new(buf, cert_len, NULL);
    380   OPENSSL_free(buf);
    381 
    382   return buffer;
    383 }
    384 
    385 /* new_leafless_chain returns a fresh stack of buffers set to {NULL}. */
    386 static STACK_OF(CRYPTO_BUFFER) *new_leafless_chain(void) {
    387   STACK_OF(CRYPTO_BUFFER) *chain = sk_CRYPTO_BUFFER_new_null();
    388   if (chain == NULL) {
    389     return NULL;
    390   }
    391 
    392   if (!sk_CRYPTO_BUFFER_push(chain, NULL)) {
    393     sk_CRYPTO_BUFFER_free(chain);
    394     return NULL;
    395   }
    396 
    397   return chain;
    398 }
    399 
    400 /* ssl_cert_set_chain sets elements 1.. of |cert->chain| to the serialised
    401  * forms of elements of |chain|. It returns one on success or zero on error, in
    402  * which case no change to |cert->chain| is made. It preverses the existing
    403  * leaf from |cert->chain|, if any. */
    404 static int ssl_cert_set_chain(CERT *cert, STACK_OF(X509) *chain) {
    405   STACK_OF(CRYPTO_BUFFER) *new_chain = NULL;
    406 
    407   if (cert->chain != NULL) {
    408     new_chain = sk_CRYPTO_BUFFER_new_null();
    409     if (new_chain == NULL) {
    410       return 0;
    411     }
    412 
    413     CRYPTO_BUFFER *leaf = sk_CRYPTO_BUFFER_value(cert->chain, 0);
    414     if (!sk_CRYPTO_BUFFER_push(new_chain, leaf)) {
    415       goto err;
    416     }
    417     /* |leaf| might be NULL if it's a leafless chain. */
    418     if (leaf != NULL) {
    419       CRYPTO_BUFFER_up_ref(leaf);
    420     }
    421   }
    422 
    423   for (size_t i = 0; i < sk_X509_num(chain); i++) {
    424     if (new_chain == NULL) {
    425       new_chain = new_leafless_chain();
    426       if (new_chain == NULL) {
    427         goto err;
    428       }
    429     }
    430 
    431     CRYPTO_BUFFER *buffer = x509_to_buffer(sk_X509_value(chain, i));
    432     if (buffer == NULL ||
    433         !sk_CRYPTO_BUFFER_push(new_chain, buffer)) {
    434       CRYPTO_BUFFER_free(buffer);
    435       goto err;
    436     }
    437   }
    438 
    439   sk_CRYPTO_BUFFER_pop_free(cert->chain, CRYPTO_BUFFER_free);
    440   cert->chain = new_chain;
    441 
    442   return 1;
    443 
    444 err:
    445   sk_CRYPTO_BUFFER_pop_free(new_chain, CRYPTO_BUFFER_free);
    446   return 0;
    447 }
    448 
    449 static void ssl_crypto_x509_cert_flush_cached_leaf(CERT *cert) {
    450   X509_free(cert->x509_leaf);
    451   cert->x509_leaf = NULL;
    452 }
    453 
    454 static void ssl_crypto_x509_cert_flush_cached_chain(CERT *cert) {
    455   sk_X509_pop_free(cert->x509_chain, X509_free);
    456   cert->x509_chain = NULL;
    457 }
    458 
    459 static int ssl_crypto_x509_check_client_CA_list(
    460     STACK_OF(CRYPTO_BUFFER) *names) {
    461   for (size_t i = 0; i < sk_CRYPTO_BUFFER_num(names); i++) {
    462     const CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(names, i);
    463     const uint8_t *inp = CRYPTO_BUFFER_data(buffer);
    464     X509_NAME *name = d2i_X509_NAME(NULL, &inp, CRYPTO_BUFFER_len(buffer));
    465     const int ok = name != NULL && inp == CRYPTO_BUFFER_data(buffer) +
    466                                               CRYPTO_BUFFER_len(buffer);
    467     X509_NAME_free(name);
    468     if (!ok) {
    469       return 0;
    470     }
    471   }
    472 
    473   return 1;
    474 }
    475 
    476 static void ssl_crypto_x509_cert_clear(CERT *cert) {
    477   ssl_crypto_x509_cert_flush_cached_leaf(cert);
    478   ssl_crypto_x509_cert_flush_cached_chain(cert);
    479 
    480   X509_free(cert->x509_stash);
    481   cert->x509_stash = NULL;
    482 }
    483 
    484 static void ssl_crypto_x509_cert_free(CERT *cert) {
    485   ssl_crypto_x509_cert_clear(cert);
    486   X509_STORE_free(cert->verify_store);
    487 }
    488 
    489 static void ssl_crypto_x509_cert_dup(CERT *new_cert, const CERT *cert) {
    490   if (cert->verify_store != NULL) {
    491     X509_STORE_up_ref(cert->verify_store);
    492     new_cert->verify_store = cert->verify_store;
    493   }
    494 }
    495 
    496 static int ssl_crypto_x509_session_cache_objects(SSL_SESSION *sess) {
    497   STACK_OF(X509) *chain = NULL;
    498   const size_t num_certs = sk_CRYPTO_BUFFER_num(sess->certs);
    499 
    500   if (num_certs > 0) {
    501     chain = sk_X509_new_null();
    502     if (chain == NULL) {
    503       OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
    504       goto err;
    505     }
    506   }
    507 
    508   X509 *leaf = NULL;
    509   for (size_t i = 0; i < num_certs; i++) {
    510     X509 *x509 = X509_parse_from_buffer(sk_CRYPTO_BUFFER_value(sess->certs, i));
    511     if (x509 == NULL) {
    512       OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
    513       goto err;
    514     }
    515     if (!sk_X509_push(chain, x509)) {
    516       OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
    517       X509_free(x509);
    518       goto err;
    519     }
    520     if (i == 0) {
    521       leaf = x509;
    522     }
    523   }
    524 
    525   sk_X509_pop_free(sess->x509_chain, X509_free);
    526   sess->x509_chain = chain;
    527   sk_X509_pop_free(sess->x509_chain_without_leaf, X509_free);
    528   sess->x509_chain_without_leaf = NULL;
    529 
    530   X509_free(sess->x509_peer);
    531   if (leaf != NULL) {
    532     X509_up_ref(leaf);
    533   }
    534   sess->x509_peer = leaf;
    535 
    536   return 1;
    537 
    538 err:
    539   sk_X509_pop_free(chain, X509_free);
    540   return 0;
    541 }
    542 
    543 static int ssl_crypto_x509_session_dup(SSL_SESSION *new_session,
    544                                        const SSL_SESSION *session) {
    545   if (session->x509_peer != NULL) {
    546     X509_up_ref(session->x509_peer);
    547     new_session->x509_peer = session->x509_peer;
    548   }
    549   if (session->x509_chain != NULL) {
    550     new_session->x509_chain = X509_chain_up_ref(session->x509_chain);
    551     if (new_session->x509_chain == NULL) {
    552       return 0;
    553     }
    554   }
    555 
    556   return 1;
    557 }
    558 
    559 static void ssl_crypto_x509_session_clear(SSL_SESSION *session) {
    560   X509_free(session->x509_peer);
    561   session->x509_peer = NULL;
    562   sk_X509_pop_free(session->x509_chain, X509_free);
    563   session->x509_chain = NULL;
    564   sk_X509_pop_free(session->x509_chain_without_leaf, X509_free);
    565   session->x509_chain_without_leaf = NULL;
    566 }
    567 
    568 static int ssl_verify_alarm_type(long type) {
    569   switch (type) {
    570     case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
    571     case X509_V_ERR_UNABLE_TO_GET_CRL:
    572     case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER:
    573       return SSL_AD_UNKNOWN_CA;
    574 
    575     case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
    576     case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE:
    577     case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
    578     case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
    579     case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
    580     case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD:
    581     case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD:
    582     case X509_V_ERR_CERT_NOT_YET_VALID:
    583     case X509_V_ERR_CRL_NOT_YET_VALID:
    584     case X509_V_ERR_CERT_UNTRUSTED:
    585     case X509_V_ERR_CERT_REJECTED:
    586     case X509_V_ERR_HOSTNAME_MISMATCH:
    587     case X509_V_ERR_EMAIL_MISMATCH:
    588     case X509_V_ERR_IP_ADDRESS_MISMATCH:
    589       return SSL_AD_BAD_CERTIFICATE;
    590 
    591     case X509_V_ERR_CERT_SIGNATURE_FAILURE:
    592     case X509_V_ERR_CRL_SIGNATURE_FAILURE:
    593       return SSL_AD_DECRYPT_ERROR;
    594 
    595     case X509_V_ERR_CERT_HAS_EXPIRED:
    596     case X509_V_ERR_CRL_HAS_EXPIRED:
    597       return SSL_AD_CERTIFICATE_EXPIRED;
    598 
    599     case X509_V_ERR_CERT_REVOKED:
    600       return SSL_AD_CERTIFICATE_REVOKED;
    601 
    602     case X509_V_ERR_UNSPECIFIED:
    603     case X509_V_ERR_OUT_OF_MEM:
    604     case X509_V_ERR_INVALID_CALL:
    605     case X509_V_ERR_STORE_LOOKUP:
    606       return SSL_AD_INTERNAL_ERROR;
    607 
    608     case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
    609     case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
    610     case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
    611     case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
    612     case X509_V_ERR_CERT_CHAIN_TOO_LONG:
    613     case X509_V_ERR_PATH_LENGTH_EXCEEDED:
    614     case X509_V_ERR_INVALID_CA:
    615       return SSL_AD_UNKNOWN_CA;
    616 
    617     case X509_V_ERR_APPLICATION_VERIFICATION:
    618       return SSL_AD_HANDSHAKE_FAILURE;
    619 
    620     case X509_V_ERR_INVALID_PURPOSE:
    621       return SSL_AD_UNSUPPORTED_CERTIFICATE;
    622 
    623     default:
    624       return SSL_AD_CERTIFICATE_UNKNOWN;
    625   }
    626 }
    627 
    628 static int ssl_crypto_x509_session_verify_cert_chain(SSL_SESSION *session,
    629                                                      SSL *ssl) {
    630   STACK_OF(X509) *const cert_chain = session->x509_chain;
    631   if (cert_chain == NULL || sk_X509_num(cert_chain) == 0) {
    632     return 0;
    633   }
    634 
    635   X509_STORE *verify_store = ssl->ctx->cert_store;
    636   if (ssl->cert->verify_store != NULL) {
    637     verify_store = ssl->cert->verify_store;
    638   }
    639 
    640   X509 *leaf = sk_X509_value(cert_chain, 0);
    641   int ret = 0;
    642   X509_STORE_CTX ctx;
    643   if (!X509_STORE_CTX_init(&ctx, verify_store, leaf, cert_chain)) {
    644     OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB);
    645     return 0;
    646   }
    647   if (!X509_STORE_CTX_set_ex_data(&ctx, SSL_get_ex_data_X509_STORE_CTX_idx(),
    648                                   ssl)) {
    649     goto err;
    650   }
    651 
    652   /* We need to inherit the verify parameters. These can be determined by the
    653    * context: if its a server it will verify SSL client certificates or vice
    654    * versa. */
    655   X509_STORE_CTX_set_default(&ctx, ssl->server ? "ssl_client" : "ssl_server");
    656 
    657   /* Anything non-default in "param" should overwrite anything in the ctx. */
    658   X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(&ctx), ssl->param);
    659 
    660   if (ssl->verify_callback) {
    661     X509_STORE_CTX_set_verify_cb(&ctx, ssl->verify_callback);
    662   }
    663 
    664   int verify_ret;
    665   if (ssl->ctx->app_verify_callback != NULL) {
    666     verify_ret = ssl->ctx->app_verify_callback(&ctx, ssl->ctx->app_verify_arg);
    667   } else {
    668     verify_ret = X509_verify_cert(&ctx);
    669   }
    670 
    671   session->verify_result = ctx.error;
    672 
    673   /* If |SSL_VERIFY_NONE|, the error is non-fatal, but we keep the result. */
    674   if (verify_ret <= 0 && ssl->verify_mode != SSL_VERIFY_NONE) {
    675     ssl3_send_alert(ssl, SSL3_AL_FATAL, ssl_verify_alarm_type(ctx.error));
    676     OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_VERIFY_FAILED);
    677     goto err;
    678   }
    679 
    680   ERR_clear_error();
    681   ret = 1;
    682 
    683 err:
    684   X509_STORE_CTX_cleanup(&ctx);
    685   return ret;
    686 }
    687 
    688 static void ssl_crypto_x509_hs_flush_cached_ca_names(SSL_HANDSHAKE *hs) {
    689   sk_X509_NAME_pop_free(hs->cached_x509_ca_names, X509_NAME_free);
    690   hs->cached_x509_ca_names = NULL;
    691 }
    692 
    693 static int ssl_crypto_x509_ssl_new(SSL *ssl) {
    694   ssl->param = X509_VERIFY_PARAM_new();
    695   if (ssl->param == NULL) {
    696     return 0;
    697   }
    698   X509_VERIFY_PARAM_inherit(ssl->param, ssl->ctx->param);
    699   return 1;
    700 }
    701 
    702 static void ssl_crypto_x509_ssl_flush_cached_client_CA(SSL *ssl) {
    703   sk_X509_NAME_pop_free(ssl->cached_x509_client_CA, X509_NAME_free);
    704   ssl->cached_x509_client_CA = NULL;
    705 }
    706 
    707 static void ssl_crypto_x509_ssl_free(SSL *ssl) {
    708   ssl_crypto_x509_ssl_flush_cached_client_CA(ssl);
    709   X509_VERIFY_PARAM_free(ssl->param);
    710 }
    711 
    712 static int ssl_crypto_x509_ssl_auto_chain_if_needed(SSL *ssl) {
    713   /* Only build a chain if there are no intermediates configured and the feature
    714    * isn't disabled. */
    715   if ((ssl->mode & SSL_MODE_NO_AUTO_CHAIN) ||
    716       !ssl_has_certificate(ssl) ||
    717       ssl->cert->chain == NULL ||
    718       sk_CRYPTO_BUFFER_num(ssl->cert->chain) > 1) {
    719     return 1;
    720   }
    721 
    722   X509 *leaf =
    723       X509_parse_from_buffer(sk_CRYPTO_BUFFER_value(ssl->cert->chain, 0));
    724   if (!leaf) {
    725     OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB);
    726     return 0;
    727   }
    728 
    729   X509_STORE_CTX ctx;
    730   if (!X509_STORE_CTX_init(&ctx, ssl->ctx->cert_store, leaf, NULL)) {
    731     X509_free(leaf);
    732     OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB);
    733     return 0;
    734   }
    735 
    736   /* Attempt to build a chain, ignoring the result. */
    737   X509_verify_cert(&ctx);
    738   X509_free(leaf);
    739   ERR_clear_error();
    740 
    741   /* Remove the leaf from the generated chain. */
    742   X509_free(sk_X509_shift(ctx.chain));
    743 
    744   const int ok = ssl_cert_set_chain(ssl->cert, ctx.chain);
    745   X509_STORE_CTX_cleanup(&ctx);
    746   if (!ok) {
    747     return 0;
    748   }
    749 
    750   ssl_crypto_x509_cert_flush_cached_chain(ssl->cert);
    751 
    752   return 1;
    753 }
    754 
    755 static void ssl_crypto_x509_ssl_ctx_flush_cached_client_CA(SSL_CTX *ctx) {
    756   sk_X509_NAME_pop_free(ctx->cached_x509_client_CA, X509_NAME_free);
    757   ctx->cached_x509_client_CA = NULL;
    758 }
    759 
    760 static int ssl_crypto_x509_ssl_ctx_new(SSL_CTX *ctx) {
    761   ctx->cert_store = X509_STORE_new();
    762   ctx->param = X509_VERIFY_PARAM_new();
    763   return (ctx->cert_store != NULL && ctx->param != NULL);
    764 }
    765 
    766 static void ssl_crypto_x509_ssl_ctx_free(SSL_CTX *ctx) {
    767   ssl_crypto_x509_ssl_ctx_flush_cached_client_CA(ctx);
    768   X509_VERIFY_PARAM_free(ctx->param);
    769   X509_STORE_free(ctx->cert_store);
    770 }
    771 
    772 const SSL_X509_METHOD ssl_crypto_x509_method = {
    773   ssl_crypto_x509_check_client_CA_list,
    774   ssl_crypto_x509_cert_clear,
    775   ssl_crypto_x509_cert_free,
    776   ssl_crypto_x509_cert_dup,
    777   ssl_crypto_x509_cert_flush_cached_chain,
    778   ssl_crypto_x509_cert_flush_cached_leaf,
    779   ssl_crypto_x509_session_cache_objects,
    780   ssl_crypto_x509_session_dup,
    781   ssl_crypto_x509_session_clear,
    782   ssl_crypto_x509_session_verify_cert_chain,
    783   ssl_crypto_x509_hs_flush_cached_ca_names,
    784   ssl_crypto_x509_ssl_new,
    785   ssl_crypto_x509_ssl_free,
    786   ssl_crypto_x509_ssl_flush_cached_client_CA,
    787   ssl_crypto_x509_ssl_auto_chain_if_needed,
    788   ssl_crypto_x509_ssl_ctx_new,
    789   ssl_crypto_x509_ssl_ctx_free,
    790   ssl_crypto_x509_ssl_ctx_flush_cached_client_CA,
    791 };
    792 
    793 static int ssl_use_certificate(CERT *cert, X509 *x) {
    794   if (x == NULL) {
    795     OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
    796     return 0;
    797   }
    798 
    799   CRYPTO_BUFFER *buffer = x509_to_buffer(x);
    800   if (buffer == NULL) {
    801     return 0;
    802   }
    803 
    804   const int ok = ssl_set_cert(cert, buffer);
    805   CRYPTO_BUFFER_free(buffer);
    806   return ok;
    807 }
    808 
    809 int SSL_use_certificate(SSL *ssl, X509 *x) {
    810   check_ssl_x509_method(ssl);
    811   return ssl_use_certificate(ssl->cert, x);
    812 }
    813 
    814 int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x) {
    815   check_ssl_ctx_x509_method(ctx);
    816   return ssl_use_certificate(ctx->cert, x);
    817 }
    818 
    819 /* ssl_cert_cache_leaf_cert sets |cert->x509_leaf|, if currently NULL, from the
    820  * first element of |cert->chain|. */
    821 static int ssl_cert_cache_leaf_cert(CERT *cert) {
    822   assert(cert->x509_method);
    823 
    824   if (cert->x509_leaf != NULL ||
    825       cert->chain == NULL) {
    826     return 1;
    827   }
    828 
    829   CRYPTO_BUFFER *leaf = sk_CRYPTO_BUFFER_value(cert->chain, 0);
    830   if (!leaf) {
    831     return 1;
    832   }
    833 
    834   cert->x509_leaf = X509_parse_from_buffer(leaf);
    835   return cert->x509_leaf != NULL;
    836 }
    837 
    838 static X509 *ssl_cert_get0_leaf(CERT *cert) {
    839   if (cert->x509_leaf == NULL &&
    840       !ssl_cert_cache_leaf_cert(cert)) {
    841     return NULL;
    842   }
    843 
    844   return cert->x509_leaf;
    845 }
    846 
    847 X509 *SSL_get_certificate(const SSL *ssl) {
    848   check_ssl_x509_method(ssl);
    849   return ssl_cert_get0_leaf(ssl->cert);
    850 }
    851 
    852 X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
    853   check_ssl_ctx_x509_method(ctx);
    854   CRYPTO_MUTEX_lock_write((CRYPTO_MUTEX *) &ctx->lock);
    855   X509 *ret = ssl_cert_get0_leaf(ctx->cert);
    856   CRYPTO_MUTEX_unlock_write((CRYPTO_MUTEX *) &ctx->lock);
    857   return ret;
    858 }
    859 
    860 static int ssl_cert_set0_chain(CERT *cert, STACK_OF(X509) *chain) {
    861   if (!ssl_cert_set_chain(cert, chain)) {
    862     return 0;
    863   }
    864 
    865   sk_X509_pop_free(chain, X509_free);
    866   ssl_crypto_x509_cert_flush_cached_chain(cert);
    867   return 1;
    868 }
    869 
    870 static int ssl_cert_set1_chain(CERT *cert, STACK_OF(X509) *chain) {
    871   if (!ssl_cert_set_chain(cert, chain)) {
    872     return 0;
    873   }
    874 
    875   ssl_crypto_x509_cert_flush_cached_chain(cert);
    876   return 1;
    877 }
    878 
    879 static int ssl_cert_append_cert(CERT *cert, X509 *x509) {
    880   assert(cert->x509_method);
    881 
    882   CRYPTO_BUFFER *buffer = x509_to_buffer(x509);
    883   if (buffer == NULL) {
    884     return 0;
    885   }
    886 
    887   if (cert->chain != NULL) {
    888     if (!sk_CRYPTO_BUFFER_push(cert->chain, buffer)) {
    889       CRYPTO_BUFFER_free(buffer);
    890       return 0;
    891     }
    892 
    893     return 1;
    894   }
    895 
    896   cert->chain = new_leafless_chain();
    897   if (cert->chain == NULL ||
    898       !sk_CRYPTO_BUFFER_push(cert->chain, buffer)) {
    899     CRYPTO_BUFFER_free(buffer);
    900     sk_CRYPTO_BUFFER_free(cert->chain);
    901     cert->chain = NULL;
    902     return 0;
    903   }
    904 
    905   return 1;
    906 }
    907 
    908 static int ssl_cert_add0_chain_cert(CERT *cert, X509 *x509) {
    909   if (!ssl_cert_append_cert(cert, x509)) {
    910     return 0;
    911   }
    912 
    913   X509_free(cert->x509_stash);
    914   cert->x509_stash = x509;
    915   ssl_crypto_x509_cert_flush_cached_chain(cert);
    916   return 1;
    917 }
    918 
    919 static int ssl_cert_add1_chain_cert(CERT *cert, X509 *x509) {
    920   if (!ssl_cert_append_cert(cert, x509)) {
    921     return 0;
    922   }
    923 
    924   ssl_crypto_x509_cert_flush_cached_chain(cert);
    925   return 1;
    926 }
    927 
    928 int SSL_CTX_set0_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) {
    929   check_ssl_ctx_x509_method(ctx);
    930   return ssl_cert_set0_chain(ctx->cert, chain);
    931 }
    932 
    933 int SSL_CTX_set1_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) {
    934   check_ssl_ctx_x509_method(ctx);
    935   return ssl_cert_set1_chain(ctx->cert, chain);
    936 }
    937 
    938 int SSL_set0_chain(SSL *ssl, STACK_OF(X509) *chain) {
    939   check_ssl_x509_method(ssl);
    940   return ssl_cert_set0_chain(ssl->cert, chain);
    941 }
    942 
    943 int SSL_set1_chain(SSL *ssl, STACK_OF(X509) *chain) {
    944   check_ssl_x509_method(ssl);
    945   return ssl_cert_set1_chain(ssl->cert, chain);
    946 }
    947 
    948 int SSL_CTX_add0_chain_cert(SSL_CTX *ctx, X509 *x509) {
    949   check_ssl_ctx_x509_method(ctx);
    950   return ssl_cert_add0_chain_cert(ctx->cert, x509);
    951 }
    952 
    953 int SSL_CTX_add1_chain_cert(SSL_CTX *ctx, X509 *x509) {
    954   check_ssl_ctx_x509_method(ctx);
    955   return ssl_cert_add1_chain_cert(ctx->cert, x509);
    956 }
    957 
    958 int SSL_CTX_add_extra_chain_cert(SSL_CTX *ctx, X509 *x509) {
    959   check_ssl_ctx_x509_method(ctx);
    960   return SSL_CTX_add0_chain_cert(ctx, x509);
    961 }
    962 
    963 int SSL_add0_chain_cert(SSL *ssl, X509 *x509) {
    964   check_ssl_x509_method(ssl);
    965   return ssl_cert_add0_chain_cert(ssl->cert, x509);
    966 }
    967 
    968 int SSL_add1_chain_cert(SSL *ssl, X509 *x509) {
    969   check_ssl_x509_method(ssl);
    970   return ssl_cert_add1_chain_cert(ssl->cert, x509);
    971 }
    972 
    973 int SSL_CTX_clear_chain_certs(SSL_CTX *ctx) {
    974   check_ssl_ctx_x509_method(ctx);
    975   return SSL_CTX_set0_chain(ctx, NULL);
    976 }
    977 
    978 int SSL_CTX_clear_extra_chain_certs(SSL_CTX *ctx) {
    979   check_ssl_ctx_x509_method(ctx);
    980   return SSL_CTX_clear_chain_certs(ctx);
    981 }
    982 
    983 int SSL_clear_chain_certs(SSL *ssl) {
    984   check_ssl_x509_method(ssl);
    985   return SSL_set0_chain(ssl, NULL);
    986 }
    987 
    988 /* ssl_cert_cache_chain_certs fills in |cert->x509_chain| from elements 1.. of
    989  * |cert->chain|. */
    990 static int ssl_cert_cache_chain_certs(CERT *cert) {
    991   assert(cert->x509_method);
    992 
    993   if (cert->x509_chain != NULL ||
    994       cert->chain == NULL ||
    995       sk_CRYPTO_BUFFER_num(cert->chain) < 2) {
    996     return 1;
    997   }
    998 
    999   STACK_OF(X509) *chain = sk_X509_new_null();
   1000   if (chain == NULL) {
   1001     return 0;
   1002   }
   1003 
   1004   for (size_t i = 1; i < sk_CRYPTO_BUFFER_num(cert->chain); i++) {
   1005     CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(cert->chain, i);
   1006     X509 *x509 = X509_parse_from_buffer(buffer);
   1007     if (x509 == NULL ||
   1008         !sk_X509_push(chain, x509)) {
   1009       X509_free(x509);
   1010       goto err;
   1011     }
   1012   }
   1013 
   1014   cert->x509_chain = chain;
   1015   return 1;
   1016 
   1017 err:
   1018   sk_X509_pop_free(chain, X509_free);
   1019   return 0;
   1020 }
   1021 
   1022 int SSL_CTX_get0_chain_certs(const SSL_CTX *ctx, STACK_OF(X509) **out_chain) {
   1023   check_ssl_ctx_x509_method(ctx);
   1024   CRYPTO_MUTEX_lock_write((CRYPTO_MUTEX *) &ctx->lock);
   1025   const int ret = ssl_cert_cache_chain_certs(ctx->cert);
   1026   CRYPTO_MUTEX_unlock_write((CRYPTO_MUTEX *) &ctx->lock);
   1027 
   1028   if (!ret) {
   1029     *out_chain = NULL;
   1030     return 0;
   1031   }
   1032 
   1033   *out_chain = ctx->cert->x509_chain;
   1034   return 1;
   1035 }
   1036 
   1037 int SSL_CTX_get_extra_chain_certs(const SSL_CTX *ctx,
   1038                                   STACK_OF(X509) **out_chain) {
   1039   return SSL_CTX_get0_chain_certs(ctx, out_chain);
   1040 }
   1041 
   1042 int SSL_get0_chain_certs(const SSL *ssl, STACK_OF(X509) **out_chain) {
   1043   check_ssl_x509_method(ssl);
   1044   if (!ssl_cert_cache_chain_certs(ssl->cert)) {
   1045     *out_chain = NULL;
   1046     return 0;
   1047   }
   1048 
   1049   *out_chain = ssl->cert->x509_chain;
   1050   return 1;
   1051 }
   1052 
   1053 static SSL_SESSION *ssl_session_new_with_crypto_x509(void) {
   1054   return ssl_session_new(&ssl_crypto_x509_method);
   1055 }
   1056 
   1057 SSL_SESSION *d2i_SSL_SESSION_bio(BIO *bio, SSL_SESSION **out) {
   1058   return ASN1_d2i_bio_of(SSL_SESSION, ssl_session_new_with_crypto_x509,
   1059                          d2i_SSL_SESSION, bio, out);
   1060 }
   1061 
   1062 int i2d_SSL_SESSION_bio(BIO *bio, const SSL_SESSION *session) {
   1063   return ASN1_i2d_bio_of(SSL_SESSION, i2d_SSL_SESSION, bio, session);
   1064 }
   1065 
   1066 IMPLEMENT_PEM_rw(SSL_SESSION, SSL_SESSION, PEM_STRING_SSL_SESSION, SSL_SESSION)
   1067 
   1068 SSL_SESSION *d2i_SSL_SESSION(SSL_SESSION **a, const uint8_t **pp, long length) {
   1069   if (length < 0) {
   1070     OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
   1071     return NULL;
   1072   }
   1073 
   1074   CBS cbs;
   1075   CBS_init(&cbs, *pp, length);
   1076 
   1077   SSL_SESSION *ret = SSL_SESSION_parse(&cbs, &ssl_crypto_x509_method,
   1078                                        NULL /* no buffer pool */);
   1079   if (ret == NULL) {
   1080     return NULL;
   1081   }
   1082 
   1083   if (a) {
   1084     SSL_SESSION_free(*a);
   1085     *a = ret;
   1086   }
   1087   *pp = CBS_data(&cbs);
   1088   return ret;
   1089 }
   1090 
   1091 STACK_OF(X509_NAME) *SSL_dup_CA_list(STACK_OF(X509_NAME) *list) {
   1092   return sk_X509_NAME_deep_copy(list, X509_NAME_dup, X509_NAME_free);
   1093 }
   1094 
   1095 static void set_client_CA_list(STACK_OF(CRYPTO_BUFFER) **ca_list,
   1096                                const STACK_OF(X509_NAME) *name_list,
   1097                                CRYPTO_BUFFER_POOL *pool) {
   1098   STACK_OF(CRYPTO_BUFFER) *buffers = sk_CRYPTO_BUFFER_new_null();
   1099   if (buffers == NULL) {
   1100     return;
   1101   }
   1102 
   1103   for (size_t i = 0; i < sk_X509_NAME_num(name_list); i++) {
   1104     X509_NAME *name = sk_X509_NAME_value(name_list, i);
   1105     uint8_t *outp = NULL;
   1106     int len = i2d_X509_NAME(name, &outp);
   1107     if (len < 0) {
   1108       goto err;
   1109     }
   1110 
   1111     CRYPTO_BUFFER *buffer = CRYPTO_BUFFER_new(outp, len, pool);
   1112     OPENSSL_free(outp);
   1113     if (buffer == NULL ||
   1114         !sk_CRYPTO_BUFFER_push(buffers, buffer)) {
   1115       CRYPTO_BUFFER_free(buffer);
   1116       goto err;
   1117     }
   1118   }
   1119 
   1120   sk_CRYPTO_BUFFER_pop_free(*ca_list, CRYPTO_BUFFER_free);
   1121   *ca_list = buffers;
   1122   return;
   1123 
   1124 err:
   1125   sk_CRYPTO_BUFFER_pop_free(buffers, CRYPTO_BUFFER_free);
   1126 }
   1127 
   1128 void SSL_set_client_CA_list(SSL *ssl, STACK_OF(X509_NAME) *name_list) {
   1129   check_ssl_x509_method(ssl);
   1130   ssl->ctx->x509_method->ssl_flush_cached_client_CA(ssl);
   1131   set_client_CA_list(&ssl->client_CA, name_list, ssl->ctx->pool);
   1132   sk_X509_NAME_pop_free(name_list, X509_NAME_free);
   1133 }
   1134 
   1135 void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list) {
   1136   check_ssl_ctx_x509_method(ctx);
   1137   ctx->x509_method->ssl_ctx_flush_cached_client_CA(ctx);
   1138   set_client_CA_list(&ctx->client_CA, name_list, ctx->pool);
   1139   sk_X509_NAME_pop_free(name_list, X509_NAME_free);
   1140 }
   1141 
   1142 static STACK_OF(X509_NAME) *
   1143     buffer_names_to_x509(const STACK_OF(CRYPTO_BUFFER) *names,
   1144                          STACK_OF(X509_NAME) **cached) {
   1145   if (names == NULL) {
   1146     return NULL;
   1147   }
   1148 
   1149   if (*cached != NULL) {
   1150     return *cached;
   1151   }
   1152 
   1153   STACK_OF(X509_NAME) *new_cache = sk_X509_NAME_new_null();
   1154   if (new_cache == NULL) {
   1155     OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
   1156     return NULL;
   1157   }
   1158 
   1159   for (size_t i = 0; i < sk_CRYPTO_BUFFER_num(names); i++) {
   1160     const CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(names, i);
   1161     const uint8_t *inp = CRYPTO_BUFFER_data(buffer);
   1162     X509_NAME *name = d2i_X509_NAME(NULL, &inp, CRYPTO_BUFFER_len(buffer));
   1163     if (name == NULL ||
   1164         inp != CRYPTO_BUFFER_data(buffer) + CRYPTO_BUFFER_len(buffer) ||
   1165         !sk_X509_NAME_push(new_cache, name)) {
   1166       X509_NAME_free(name);
   1167       goto err;
   1168     }
   1169   }
   1170 
   1171   *cached = new_cache;
   1172   return new_cache;
   1173 
   1174 err:
   1175   sk_X509_NAME_pop_free(new_cache, X509_NAME_free);
   1176   return NULL;
   1177 }
   1178 
   1179 STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *ssl) {
   1180   check_ssl_x509_method(ssl);
   1181   /* For historical reasons, this function is used both to query configuration
   1182    * state on a server as well as handshake state on a client. However, whether
   1183    * |ssl| is a client or server is not known until explicitly configured with
   1184    * |SSL_set_connect_state|. If |handshake_func| is NULL, |ssl| is in an
   1185    * indeterminate mode and |ssl->server| is unset. */
   1186   if (ssl->handshake_func != NULL && !ssl->server) {
   1187     if (ssl->s3->hs != NULL) {
   1188       return buffer_names_to_x509(ssl->s3->hs->ca_names,
   1189                                   &ssl->s3->hs->cached_x509_ca_names);
   1190     }
   1191 
   1192     return NULL;
   1193   }
   1194 
   1195   if (ssl->client_CA != NULL) {
   1196     return buffer_names_to_x509(
   1197         ssl->client_CA, (STACK_OF(X509_NAME) **)&ssl->cached_x509_client_CA);
   1198   }
   1199   return buffer_names_to_x509(ssl->ctx->client_CA,
   1200                               &ssl->ctx->cached_x509_client_CA);
   1201 }
   1202 
   1203 STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx) {
   1204   check_ssl_ctx_x509_method(ctx);
   1205   CRYPTO_MUTEX_lock_write((CRYPTO_MUTEX *) &ctx->lock);
   1206   STACK_OF(X509_NAME) *ret = buffer_names_to_x509(
   1207       ctx->client_CA, (STACK_OF(X509_NAME) **)&ctx->cached_x509_client_CA);
   1208   CRYPTO_MUTEX_unlock_write((CRYPTO_MUTEX *) &ctx->lock);
   1209   return ret;
   1210 }
   1211 
   1212 static int add_client_CA(STACK_OF(CRYPTO_BUFFER) **names, X509 *x509,
   1213                          CRYPTO_BUFFER_POOL *pool) {
   1214   if (x509 == NULL) {
   1215     return 0;
   1216   }
   1217 
   1218   uint8_t *outp = NULL;
   1219   int len = i2d_X509_NAME(X509_get_subject_name(x509), &outp);
   1220   if (len < 0) {
   1221     return 0;
   1222   }
   1223 
   1224   CRYPTO_BUFFER *buffer = CRYPTO_BUFFER_new(outp, len, pool);
   1225   OPENSSL_free(outp);
   1226   if (buffer == NULL) {
   1227     return 0;
   1228   }
   1229 
   1230   int alloced = 0;
   1231   if (*names == NULL) {
   1232     *names = sk_CRYPTO_BUFFER_new_null();
   1233     alloced = 1;
   1234 
   1235     if (*names == NULL) {
   1236       CRYPTO_BUFFER_free(buffer);
   1237       return 0;
   1238     }
   1239   }
   1240 
   1241   if (!sk_CRYPTO_BUFFER_push(*names, buffer)) {
   1242     CRYPTO_BUFFER_free(buffer);
   1243     if (alloced) {
   1244       sk_CRYPTO_BUFFER_pop_free(*names, CRYPTO_BUFFER_free);
   1245       *names = NULL;
   1246     }
   1247     return 0;
   1248   }
   1249 
   1250   return 1;
   1251 }
   1252 
   1253 int SSL_add_client_CA(SSL *ssl, X509 *x509) {
   1254   check_ssl_x509_method(ssl);
   1255   if (!add_client_CA(&ssl->client_CA, x509, ssl->ctx->pool)) {
   1256     return 0;
   1257   }
   1258 
   1259   ssl_crypto_x509_ssl_flush_cached_client_CA(ssl);
   1260   return 1;
   1261 }
   1262 
   1263 int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x509) {
   1264   check_ssl_ctx_x509_method(ctx);
   1265   if (!add_client_CA(&ctx->client_CA, x509, ctx->pool)) {
   1266     return 0;
   1267   }
   1268 
   1269   ssl_crypto_x509_ssl_ctx_flush_cached_client_CA(ctx);
   1270   return 1;
   1271 }
   1272 
   1273 static int do_client_cert_cb(SSL *ssl, void *arg) {
   1274   if (ssl_has_certificate(ssl) || ssl->ctx->client_cert_cb == NULL) {
   1275     return 1;
   1276   }
   1277 
   1278   X509 *x509 = NULL;
   1279   EVP_PKEY *pkey = NULL;
   1280   int ret = ssl->ctx->client_cert_cb(ssl, &x509, &pkey);
   1281   if (ret < 0) {
   1282     return -1;
   1283   }
   1284 
   1285   if (ret != 0) {
   1286     if (!SSL_use_certificate(ssl, x509) ||
   1287         !SSL_use_PrivateKey(ssl, pkey)) {
   1288       return 0;
   1289     }
   1290   }
   1291 
   1292   X509_free(x509);
   1293   EVP_PKEY_free(pkey);
   1294   return 1;
   1295 }
   1296 
   1297 void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl,
   1298                                                         X509 **out_x509,
   1299                                                         EVP_PKEY **out_pkey)) {
   1300   check_ssl_ctx_x509_method(ctx);
   1301   /* Emulate the old client certificate callback with the new one. */
   1302   SSL_CTX_set_cert_cb(ctx, do_client_cert_cb, NULL);
   1303   ctx->client_cert_cb = cb;
   1304 }
   1305 
   1306 static int set_cert_store(X509_STORE **store_ptr, X509_STORE *new_store,
   1307                           int take_ref) {
   1308   X509_STORE_free(*store_ptr);
   1309   *store_ptr = new_store;
   1310 
   1311   if (new_store != NULL && take_ref) {
   1312     X509_STORE_up_ref(new_store);
   1313   }
   1314 
   1315   return 1;
   1316 }
   1317 
   1318 int SSL_get_ex_data_X509_STORE_CTX_idx(void) {
   1319   /* The ex_data index to go from |X509_STORE_CTX| to |SSL| always uses the
   1320    * reserved app_data slot. Before ex_data was introduced, app_data was used.
   1321    * Avoid breaking any software which assumes |X509_STORE_CTX_get_app_data|
   1322    * works. */
   1323   return 0;
   1324 }
   1325 
   1326 int SSL_CTX_set0_verify_cert_store(SSL_CTX *ctx, X509_STORE *store) {
   1327   check_ssl_ctx_x509_method(ctx);
   1328   return set_cert_store(&ctx->cert->verify_store, store, 0);
   1329 }
   1330 
   1331 int SSL_CTX_set1_verify_cert_store(SSL_CTX *ctx, X509_STORE *store) {
   1332   check_ssl_ctx_x509_method(ctx);
   1333   return set_cert_store(&ctx->cert->verify_store, store, 1);
   1334 }
   1335 
   1336 int SSL_set0_verify_cert_store(SSL *ssl, X509_STORE *store) {
   1337   check_ssl_x509_method(ssl);
   1338   return set_cert_store(&ssl->cert->verify_store, store, 0);
   1339 }
   1340 
   1341 int SSL_set1_verify_cert_store(SSL *ssl, X509_STORE *store) {
   1342   check_ssl_x509_method(ssl);
   1343   return set_cert_store(&ssl->cert->verify_store, store, 1);
   1344 }
   1345