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   bssl::UniquePtr<STACK_OF(X509)> chain;
    498   const size_t num_certs = sk_CRYPTO_BUFFER_num(sess->certs);
    499   if (num_certs > 0) {
    500     chain.reset(sk_X509_new_null());
    501     if (!chain) {
    502       OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
    503       return 0;
    504     }
    505   }
    506 
    507   X509 *leaf = NULL;
    508   for (size_t i = 0; i < num_certs; i++) {
    509     X509 *x509 = X509_parse_from_buffer(sk_CRYPTO_BUFFER_value(sess->certs, i));
    510     if (x509 == NULL) {
    511       OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
    512       return 0;
    513     }
    514     if (!sk_X509_push(chain.get(), x509)) {
    515       OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
    516       X509_free(x509);
    517       return 0;
    518     }
    519     if (i == 0) {
    520       leaf = x509;
    521     }
    522   }
    523 
    524   sk_X509_pop_free(sess->x509_chain, X509_free);
    525   sess->x509_chain = chain.release();
    526   sk_X509_pop_free(sess->x509_chain_without_leaf, X509_free);
    527   sess->x509_chain_without_leaf = NULL;
    528 
    529   X509_free(sess->x509_peer);
    530   if (leaf != NULL) {
    531     X509_up_ref(leaf);
    532   }
    533   sess->x509_peer = leaf;
    534   return 1;
    535 }
    536 
    537 static int ssl_crypto_x509_session_dup(SSL_SESSION *new_session,
    538                                        const SSL_SESSION *session) {
    539   if (session->x509_peer != NULL) {
    540     X509_up_ref(session->x509_peer);
    541     new_session->x509_peer = session->x509_peer;
    542   }
    543   if (session->x509_chain != NULL) {
    544     new_session->x509_chain = X509_chain_up_ref(session->x509_chain);
    545     if (new_session->x509_chain == NULL) {
    546       return 0;
    547     }
    548   }
    549 
    550   return 1;
    551 }
    552 
    553 static void ssl_crypto_x509_session_clear(SSL_SESSION *session) {
    554   X509_free(session->x509_peer);
    555   session->x509_peer = NULL;
    556   sk_X509_pop_free(session->x509_chain, X509_free);
    557   session->x509_chain = NULL;
    558   sk_X509_pop_free(session->x509_chain_without_leaf, X509_free);
    559   session->x509_chain_without_leaf = NULL;
    560 }
    561 
    562 static int ssl_verify_alarm_type(long type) {
    563   switch (type) {
    564     case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
    565     case X509_V_ERR_UNABLE_TO_GET_CRL:
    566     case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER:
    567       return SSL_AD_UNKNOWN_CA;
    568 
    569     case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
    570     case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE:
    571     case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
    572     case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
    573     case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
    574     case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD:
    575     case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD:
    576     case X509_V_ERR_CERT_NOT_YET_VALID:
    577     case X509_V_ERR_CRL_NOT_YET_VALID:
    578     case X509_V_ERR_CERT_UNTRUSTED:
    579     case X509_V_ERR_CERT_REJECTED:
    580     case X509_V_ERR_HOSTNAME_MISMATCH:
    581     case X509_V_ERR_EMAIL_MISMATCH:
    582     case X509_V_ERR_IP_ADDRESS_MISMATCH:
    583       return SSL_AD_BAD_CERTIFICATE;
    584 
    585     case X509_V_ERR_CERT_SIGNATURE_FAILURE:
    586     case X509_V_ERR_CRL_SIGNATURE_FAILURE:
    587       return SSL_AD_DECRYPT_ERROR;
    588 
    589     case X509_V_ERR_CERT_HAS_EXPIRED:
    590     case X509_V_ERR_CRL_HAS_EXPIRED:
    591       return SSL_AD_CERTIFICATE_EXPIRED;
    592 
    593     case X509_V_ERR_CERT_REVOKED:
    594       return SSL_AD_CERTIFICATE_REVOKED;
    595 
    596     case X509_V_ERR_UNSPECIFIED:
    597     case X509_V_ERR_OUT_OF_MEM:
    598     case X509_V_ERR_INVALID_CALL:
    599     case X509_V_ERR_STORE_LOOKUP:
    600       return SSL_AD_INTERNAL_ERROR;
    601 
    602     case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
    603     case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
    604     case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
    605     case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
    606     case X509_V_ERR_CERT_CHAIN_TOO_LONG:
    607     case X509_V_ERR_PATH_LENGTH_EXCEEDED:
    608     case X509_V_ERR_INVALID_CA:
    609       return SSL_AD_UNKNOWN_CA;
    610 
    611     case X509_V_ERR_APPLICATION_VERIFICATION:
    612       return SSL_AD_HANDSHAKE_FAILURE;
    613 
    614     case X509_V_ERR_INVALID_PURPOSE:
    615       return SSL_AD_UNSUPPORTED_CERTIFICATE;
    616 
    617     default:
    618       return SSL_AD_CERTIFICATE_UNKNOWN;
    619   }
    620 }
    621 
    622 static int ssl_crypto_x509_session_verify_cert_chain(SSL_SESSION *session,
    623                                                      SSL *ssl) {
    624   STACK_OF(X509) *const cert_chain = session->x509_chain;
    625   if (cert_chain == NULL || sk_X509_num(cert_chain) == 0) {
    626     return 0;
    627   }
    628 
    629   X509_STORE *verify_store = ssl->ctx->cert_store;
    630   if (ssl->cert->verify_store != NULL) {
    631     verify_store = ssl->cert->verify_store;
    632   }
    633 
    634   X509 *leaf = sk_X509_value(cert_chain, 0);
    635   int ret = 0;
    636   X509_STORE_CTX ctx;
    637   if (!X509_STORE_CTX_init(&ctx, verify_store, leaf, cert_chain)) {
    638     OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB);
    639     return 0;
    640   }
    641   if (!X509_STORE_CTX_set_ex_data(&ctx, SSL_get_ex_data_X509_STORE_CTX_idx(),
    642                                   ssl)) {
    643     goto err;
    644   }
    645 
    646   /* We need to inherit the verify parameters. These can be determined by the
    647    * context: if its a server it will verify SSL client certificates or vice
    648    * versa. */
    649   X509_STORE_CTX_set_default(&ctx, ssl->server ? "ssl_client" : "ssl_server");
    650 
    651   /* Anything non-default in "param" should overwrite anything in the ctx. */
    652   X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(&ctx), ssl->param);
    653 
    654   if (ssl->verify_callback) {
    655     X509_STORE_CTX_set_verify_cb(&ctx, ssl->verify_callback);
    656   }
    657 
    658   int verify_ret;
    659   if (ssl->ctx->app_verify_callback != NULL) {
    660     verify_ret = ssl->ctx->app_verify_callback(&ctx, ssl->ctx->app_verify_arg);
    661   } else {
    662     verify_ret = X509_verify_cert(&ctx);
    663   }
    664 
    665   session->verify_result = ctx.error;
    666 
    667   /* If |SSL_VERIFY_NONE|, the error is non-fatal, but we keep the result. */
    668   if (verify_ret <= 0 && ssl->verify_mode != SSL_VERIFY_NONE) {
    669     ssl3_send_alert(ssl, SSL3_AL_FATAL, ssl_verify_alarm_type(ctx.error));
    670     OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_VERIFY_FAILED);
    671     goto err;
    672   }
    673 
    674   ERR_clear_error();
    675   ret = 1;
    676 
    677 err:
    678   X509_STORE_CTX_cleanup(&ctx);
    679   return ret;
    680 }
    681 
    682 static void ssl_crypto_x509_hs_flush_cached_ca_names(SSL_HANDSHAKE *hs) {
    683   sk_X509_NAME_pop_free(hs->cached_x509_ca_names, X509_NAME_free);
    684   hs->cached_x509_ca_names = NULL;
    685 }
    686 
    687 static int ssl_crypto_x509_ssl_new(SSL *ssl) {
    688   ssl->param = X509_VERIFY_PARAM_new();
    689   if (ssl->param == NULL) {
    690     return 0;
    691   }
    692   X509_VERIFY_PARAM_inherit(ssl->param, ssl->ctx->param);
    693   return 1;
    694 }
    695 
    696 static void ssl_crypto_x509_ssl_flush_cached_client_CA(SSL *ssl) {
    697   sk_X509_NAME_pop_free(ssl->cached_x509_client_CA, X509_NAME_free);
    698   ssl->cached_x509_client_CA = NULL;
    699 }
    700 
    701 static void ssl_crypto_x509_ssl_free(SSL *ssl) {
    702   ssl_crypto_x509_ssl_flush_cached_client_CA(ssl);
    703   X509_VERIFY_PARAM_free(ssl->param);
    704 }
    705 
    706 static int ssl_crypto_x509_ssl_auto_chain_if_needed(SSL *ssl) {
    707   /* Only build a chain if there are no intermediates configured and the feature
    708    * isn't disabled. */
    709   if ((ssl->mode & SSL_MODE_NO_AUTO_CHAIN) ||
    710       !ssl_has_certificate(ssl) ||
    711       ssl->cert->chain == NULL ||
    712       sk_CRYPTO_BUFFER_num(ssl->cert->chain) > 1) {
    713     return 1;
    714   }
    715 
    716   X509 *leaf =
    717       X509_parse_from_buffer(sk_CRYPTO_BUFFER_value(ssl->cert->chain, 0));
    718   if (!leaf) {
    719     OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB);
    720     return 0;
    721   }
    722 
    723   X509_STORE_CTX ctx;
    724   if (!X509_STORE_CTX_init(&ctx, ssl->ctx->cert_store, leaf, NULL)) {
    725     X509_free(leaf);
    726     OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB);
    727     return 0;
    728   }
    729 
    730   /* Attempt to build a chain, ignoring the result. */
    731   X509_verify_cert(&ctx);
    732   X509_free(leaf);
    733   ERR_clear_error();
    734 
    735   /* Remove the leaf from the generated chain. */
    736   X509_free(sk_X509_shift(ctx.chain));
    737 
    738   const int ok = ssl_cert_set_chain(ssl->cert, ctx.chain);
    739   X509_STORE_CTX_cleanup(&ctx);
    740   if (!ok) {
    741     return 0;
    742   }
    743 
    744   ssl_crypto_x509_cert_flush_cached_chain(ssl->cert);
    745 
    746   return 1;
    747 }
    748 
    749 static void ssl_crypto_x509_ssl_ctx_flush_cached_client_CA(SSL_CTX *ctx) {
    750   sk_X509_NAME_pop_free(ctx->cached_x509_client_CA, X509_NAME_free);
    751   ctx->cached_x509_client_CA = NULL;
    752 }
    753 
    754 static int ssl_crypto_x509_ssl_ctx_new(SSL_CTX *ctx) {
    755   ctx->cert_store = X509_STORE_new();
    756   ctx->param = X509_VERIFY_PARAM_new();
    757   return (ctx->cert_store != NULL && ctx->param != NULL);
    758 }
    759 
    760 static void ssl_crypto_x509_ssl_ctx_free(SSL_CTX *ctx) {
    761   ssl_crypto_x509_ssl_ctx_flush_cached_client_CA(ctx);
    762   X509_VERIFY_PARAM_free(ctx->param);
    763   X509_STORE_free(ctx->cert_store);
    764 }
    765 
    766 const SSL_X509_METHOD ssl_crypto_x509_method = {
    767   ssl_crypto_x509_check_client_CA_list,
    768   ssl_crypto_x509_cert_clear,
    769   ssl_crypto_x509_cert_free,
    770   ssl_crypto_x509_cert_dup,
    771   ssl_crypto_x509_cert_flush_cached_chain,
    772   ssl_crypto_x509_cert_flush_cached_leaf,
    773   ssl_crypto_x509_session_cache_objects,
    774   ssl_crypto_x509_session_dup,
    775   ssl_crypto_x509_session_clear,
    776   ssl_crypto_x509_session_verify_cert_chain,
    777   ssl_crypto_x509_hs_flush_cached_ca_names,
    778   ssl_crypto_x509_ssl_new,
    779   ssl_crypto_x509_ssl_free,
    780   ssl_crypto_x509_ssl_flush_cached_client_CA,
    781   ssl_crypto_x509_ssl_auto_chain_if_needed,
    782   ssl_crypto_x509_ssl_ctx_new,
    783   ssl_crypto_x509_ssl_ctx_free,
    784   ssl_crypto_x509_ssl_ctx_flush_cached_client_CA,
    785 };
    786 
    787 static int ssl_use_certificate(CERT *cert, X509 *x) {
    788   if (x == NULL) {
    789     OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
    790     return 0;
    791   }
    792 
    793   CRYPTO_BUFFER *buffer = x509_to_buffer(x);
    794   if (buffer == NULL) {
    795     return 0;
    796   }
    797 
    798   const int ok = ssl_set_cert(cert, buffer);
    799   CRYPTO_BUFFER_free(buffer);
    800   return ok;
    801 }
    802 
    803 int SSL_use_certificate(SSL *ssl, X509 *x) {
    804   check_ssl_x509_method(ssl);
    805   return ssl_use_certificate(ssl->cert, x);
    806 }
    807 
    808 int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x) {
    809   check_ssl_ctx_x509_method(ctx);
    810   return ssl_use_certificate(ctx->cert, x);
    811 }
    812 
    813 /* ssl_cert_cache_leaf_cert sets |cert->x509_leaf|, if currently NULL, from the
    814  * first element of |cert->chain|. */
    815 static int ssl_cert_cache_leaf_cert(CERT *cert) {
    816   assert(cert->x509_method);
    817 
    818   if (cert->x509_leaf != NULL ||
    819       cert->chain == NULL) {
    820     return 1;
    821   }
    822 
    823   CRYPTO_BUFFER *leaf = sk_CRYPTO_BUFFER_value(cert->chain, 0);
    824   if (!leaf) {
    825     return 1;
    826   }
    827 
    828   cert->x509_leaf = X509_parse_from_buffer(leaf);
    829   return cert->x509_leaf != NULL;
    830 }
    831 
    832 static X509 *ssl_cert_get0_leaf(CERT *cert) {
    833   if (cert->x509_leaf == NULL &&
    834       !ssl_cert_cache_leaf_cert(cert)) {
    835     return NULL;
    836   }
    837 
    838   return cert->x509_leaf;
    839 }
    840 
    841 X509 *SSL_get_certificate(const SSL *ssl) {
    842   check_ssl_x509_method(ssl);
    843   return ssl_cert_get0_leaf(ssl->cert);
    844 }
    845 
    846 X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
    847   check_ssl_ctx_x509_method(ctx);
    848   CRYPTO_MUTEX_lock_write((CRYPTO_MUTEX *) &ctx->lock);
    849   X509 *ret = ssl_cert_get0_leaf(ctx->cert);
    850   CRYPTO_MUTEX_unlock_write((CRYPTO_MUTEX *) &ctx->lock);
    851   return ret;
    852 }
    853 
    854 static int ssl_cert_set0_chain(CERT *cert, STACK_OF(X509) *chain) {
    855   if (!ssl_cert_set_chain(cert, chain)) {
    856     return 0;
    857   }
    858 
    859   sk_X509_pop_free(chain, X509_free);
    860   ssl_crypto_x509_cert_flush_cached_chain(cert);
    861   return 1;
    862 }
    863 
    864 static int ssl_cert_set1_chain(CERT *cert, STACK_OF(X509) *chain) {
    865   if (!ssl_cert_set_chain(cert, chain)) {
    866     return 0;
    867   }
    868 
    869   ssl_crypto_x509_cert_flush_cached_chain(cert);
    870   return 1;
    871 }
    872 
    873 static int ssl_cert_append_cert(CERT *cert, X509 *x509) {
    874   assert(cert->x509_method);
    875 
    876   CRYPTO_BUFFER *buffer = x509_to_buffer(x509);
    877   if (buffer == NULL) {
    878     return 0;
    879   }
    880 
    881   if (cert->chain != NULL) {
    882     if (!sk_CRYPTO_BUFFER_push(cert->chain, buffer)) {
    883       CRYPTO_BUFFER_free(buffer);
    884       return 0;
    885     }
    886 
    887     return 1;
    888   }
    889 
    890   cert->chain = new_leafless_chain();
    891   if (cert->chain == NULL ||
    892       !sk_CRYPTO_BUFFER_push(cert->chain, buffer)) {
    893     CRYPTO_BUFFER_free(buffer);
    894     sk_CRYPTO_BUFFER_free(cert->chain);
    895     cert->chain = NULL;
    896     return 0;
    897   }
    898 
    899   return 1;
    900 }
    901 
    902 static int ssl_cert_add0_chain_cert(CERT *cert, X509 *x509) {
    903   if (!ssl_cert_append_cert(cert, x509)) {
    904     return 0;
    905   }
    906 
    907   X509_free(cert->x509_stash);
    908   cert->x509_stash = x509;
    909   ssl_crypto_x509_cert_flush_cached_chain(cert);
    910   return 1;
    911 }
    912 
    913 static int ssl_cert_add1_chain_cert(CERT *cert, X509 *x509) {
    914   if (!ssl_cert_append_cert(cert, x509)) {
    915     return 0;
    916   }
    917 
    918   ssl_crypto_x509_cert_flush_cached_chain(cert);
    919   return 1;
    920 }
    921 
    922 int SSL_CTX_set0_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) {
    923   check_ssl_ctx_x509_method(ctx);
    924   return ssl_cert_set0_chain(ctx->cert, chain);
    925 }
    926 
    927 int SSL_CTX_set1_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) {
    928   check_ssl_ctx_x509_method(ctx);
    929   return ssl_cert_set1_chain(ctx->cert, chain);
    930 }
    931 
    932 int SSL_set0_chain(SSL *ssl, STACK_OF(X509) *chain) {
    933   check_ssl_x509_method(ssl);
    934   return ssl_cert_set0_chain(ssl->cert, chain);
    935 }
    936 
    937 int SSL_set1_chain(SSL *ssl, STACK_OF(X509) *chain) {
    938   check_ssl_x509_method(ssl);
    939   return ssl_cert_set1_chain(ssl->cert, chain);
    940 }
    941 
    942 int SSL_CTX_add0_chain_cert(SSL_CTX *ctx, X509 *x509) {
    943   check_ssl_ctx_x509_method(ctx);
    944   return ssl_cert_add0_chain_cert(ctx->cert, x509);
    945 }
    946 
    947 int SSL_CTX_add1_chain_cert(SSL_CTX *ctx, X509 *x509) {
    948   check_ssl_ctx_x509_method(ctx);
    949   return ssl_cert_add1_chain_cert(ctx->cert, x509);
    950 }
    951 
    952 int SSL_CTX_add_extra_chain_cert(SSL_CTX *ctx, X509 *x509) {
    953   check_ssl_ctx_x509_method(ctx);
    954   return SSL_CTX_add0_chain_cert(ctx, x509);
    955 }
    956 
    957 int SSL_add0_chain_cert(SSL *ssl, X509 *x509) {
    958   check_ssl_x509_method(ssl);
    959   return ssl_cert_add0_chain_cert(ssl->cert, x509);
    960 }
    961 
    962 int SSL_add1_chain_cert(SSL *ssl, X509 *x509) {
    963   check_ssl_x509_method(ssl);
    964   return ssl_cert_add1_chain_cert(ssl->cert, x509);
    965 }
    966 
    967 int SSL_CTX_clear_chain_certs(SSL_CTX *ctx) {
    968   check_ssl_ctx_x509_method(ctx);
    969   return SSL_CTX_set0_chain(ctx, NULL);
    970 }
    971 
    972 int SSL_CTX_clear_extra_chain_certs(SSL_CTX *ctx) {
    973   check_ssl_ctx_x509_method(ctx);
    974   return SSL_CTX_clear_chain_certs(ctx);
    975 }
    976 
    977 int SSL_clear_chain_certs(SSL *ssl) {
    978   check_ssl_x509_method(ssl);
    979   return SSL_set0_chain(ssl, NULL);
    980 }
    981 
    982 /* ssl_cert_cache_chain_certs fills in |cert->x509_chain| from elements 1.. of
    983  * |cert->chain|. */
    984 static int ssl_cert_cache_chain_certs(CERT *cert) {
    985   assert(cert->x509_method);
    986 
    987   if (cert->x509_chain != NULL ||
    988       cert->chain == NULL ||
    989       sk_CRYPTO_BUFFER_num(cert->chain) < 2) {
    990     return 1;
    991   }
    992 
    993   STACK_OF(X509) *chain = sk_X509_new_null();
    994   if (chain == NULL) {
    995     return 0;
    996   }
    997 
    998   for (size_t i = 1; i < sk_CRYPTO_BUFFER_num(cert->chain); i++) {
    999     CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(cert->chain, i);
   1000     X509 *x509 = X509_parse_from_buffer(buffer);
   1001     if (x509 == NULL ||
   1002         !sk_X509_push(chain, x509)) {
   1003       X509_free(x509);
   1004       goto err;
   1005     }
   1006   }
   1007 
   1008   cert->x509_chain = chain;
   1009   return 1;
   1010 
   1011 err:
   1012   sk_X509_pop_free(chain, X509_free);
   1013   return 0;
   1014 }
   1015 
   1016 int SSL_CTX_get0_chain_certs(const SSL_CTX *ctx, STACK_OF(X509) **out_chain) {
   1017   check_ssl_ctx_x509_method(ctx);
   1018   CRYPTO_MUTEX_lock_write((CRYPTO_MUTEX *) &ctx->lock);
   1019   const int ret = ssl_cert_cache_chain_certs(ctx->cert);
   1020   CRYPTO_MUTEX_unlock_write((CRYPTO_MUTEX *) &ctx->lock);
   1021 
   1022   if (!ret) {
   1023     *out_chain = NULL;
   1024     return 0;
   1025   }
   1026 
   1027   *out_chain = ctx->cert->x509_chain;
   1028   return 1;
   1029 }
   1030 
   1031 int SSL_CTX_get_extra_chain_certs(const SSL_CTX *ctx,
   1032                                   STACK_OF(X509) **out_chain) {
   1033   return SSL_CTX_get0_chain_certs(ctx, out_chain);
   1034 }
   1035 
   1036 int SSL_get0_chain_certs(const SSL *ssl, STACK_OF(X509) **out_chain) {
   1037   check_ssl_x509_method(ssl);
   1038   if (!ssl_cert_cache_chain_certs(ssl->cert)) {
   1039     *out_chain = NULL;
   1040     return 0;
   1041   }
   1042 
   1043   *out_chain = ssl->cert->x509_chain;
   1044   return 1;
   1045 }
   1046 
   1047 static SSL_SESSION *ssl_session_new_with_crypto_x509(void) {
   1048   return ssl_session_new(&ssl_crypto_x509_method);
   1049 }
   1050 
   1051 SSL_SESSION *d2i_SSL_SESSION_bio(BIO *bio, SSL_SESSION **out) {
   1052   return ASN1_d2i_bio_of(SSL_SESSION, ssl_session_new_with_crypto_x509,
   1053                          d2i_SSL_SESSION, bio, out);
   1054 }
   1055 
   1056 int i2d_SSL_SESSION_bio(BIO *bio, const SSL_SESSION *session) {
   1057   return ASN1_i2d_bio_of(SSL_SESSION, i2d_SSL_SESSION, bio, session);
   1058 }
   1059 
   1060 IMPLEMENT_PEM_rw(SSL_SESSION, SSL_SESSION, PEM_STRING_SSL_SESSION, SSL_SESSION)
   1061 
   1062 SSL_SESSION *d2i_SSL_SESSION(SSL_SESSION **a, const uint8_t **pp, long length) {
   1063   if (length < 0) {
   1064     OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
   1065     return NULL;
   1066   }
   1067 
   1068   CBS cbs;
   1069   CBS_init(&cbs, *pp, length);
   1070 
   1071   SSL_SESSION *ret = SSL_SESSION_parse(&cbs, &ssl_crypto_x509_method,
   1072                                        NULL /* no buffer pool */);
   1073   if (ret == NULL) {
   1074     return NULL;
   1075   }
   1076 
   1077   if (a) {
   1078     SSL_SESSION_free(*a);
   1079     *a = ret;
   1080   }
   1081   *pp = CBS_data(&cbs);
   1082   return ret;
   1083 }
   1084 
   1085 STACK_OF(X509_NAME) *SSL_dup_CA_list(STACK_OF(X509_NAME) *list) {
   1086   return sk_X509_NAME_deep_copy(list, X509_NAME_dup, X509_NAME_free);
   1087 }
   1088 
   1089 static void set_client_CA_list(STACK_OF(CRYPTO_BUFFER) **ca_list,
   1090                                const STACK_OF(X509_NAME) *name_list,
   1091                                CRYPTO_BUFFER_POOL *pool) {
   1092   STACK_OF(CRYPTO_BUFFER) *buffers = sk_CRYPTO_BUFFER_new_null();
   1093   if (buffers == NULL) {
   1094     return;
   1095   }
   1096 
   1097   for (size_t i = 0; i < sk_X509_NAME_num(name_list); i++) {
   1098     X509_NAME *name = sk_X509_NAME_value(name_list, i);
   1099     uint8_t *outp = NULL;
   1100     int len = i2d_X509_NAME(name, &outp);
   1101     if (len < 0) {
   1102       goto err;
   1103     }
   1104 
   1105     CRYPTO_BUFFER *buffer = CRYPTO_BUFFER_new(outp, len, pool);
   1106     OPENSSL_free(outp);
   1107     if (buffer == NULL ||
   1108         !sk_CRYPTO_BUFFER_push(buffers, buffer)) {
   1109       CRYPTO_BUFFER_free(buffer);
   1110       goto err;
   1111     }
   1112   }
   1113 
   1114   sk_CRYPTO_BUFFER_pop_free(*ca_list, CRYPTO_BUFFER_free);
   1115   *ca_list = buffers;
   1116   return;
   1117 
   1118 err:
   1119   sk_CRYPTO_BUFFER_pop_free(buffers, CRYPTO_BUFFER_free);
   1120 }
   1121 
   1122 void SSL_set_client_CA_list(SSL *ssl, STACK_OF(X509_NAME) *name_list) {
   1123   check_ssl_x509_method(ssl);
   1124   ssl->ctx->x509_method->ssl_flush_cached_client_CA(ssl);
   1125   set_client_CA_list(&ssl->client_CA, name_list, ssl->ctx->pool);
   1126   sk_X509_NAME_pop_free(name_list, X509_NAME_free);
   1127 }
   1128 
   1129 void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list) {
   1130   check_ssl_ctx_x509_method(ctx);
   1131   ctx->x509_method->ssl_ctx_flush_cached_client_CA(ctx);
   1132   set_client_CA_list(&ctx->client_CA, name_list, ctx->pool);
   1133   sk_X509_NAME_pop_free(name_list, X509_NAME_free);
   1134 }
   1135 
   1136 static STACK_OF(X509_NAME) *
   1137     buffer_names_to_x509(const STACK_OF(CRYPTO_BUFFER) *names,
   1138                          STACK_OF(X509_NAME) **cached) {
   1139   if (names == NULL) {
   1140     return NULL;
   1141   }
   1142 
   1143   if (*cached != NULL) {
   1144     return *cached;
   1145   }
   1146 
   1147   STACK_OF(X509_NAME) *new_cache = sk_X509_NAME_new_null();
   1148   if (new_cache == NULL) {
   1149     OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
   1150     return NULL;
   1151   }
   1152 
   1153   for (size_t i = 0; i < sk_CRYPTO_BUFFER_num(names); i++) {
   1154     const CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(names, i);
   1155     const uint8_t *inp = CRYPTO_BUFFER_data(buffer);
   1156     X509_NAME *name = d2i_X509_NAME(NULL, &inp, CRYPTO_BUFFER_len(buffer));
   1157     if (name == NULL ||
   1158         inp != CRYPTO_BUFFER_data(buffer) + CRYPTO_BUFFER_len(buffer) ||
   1159         !sk_X509_NAME_push(new_cache, name)) {
   1160       X509_NAME_free(name);
   1161       goto err;
   1162     }
   1163   }
   1164 
   1165   *cached = new_cache;
   1166   return new_cache;
   1167 
   1168 err:
   1169   sk_X509_NAME_pop_free(new_cache, X509_NAME_free);
   1170   return NULL;
   1171 }
   1172 
   1173 STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *ssl) {
   1174   check_ssl_x509_method(ssl);
   1175   /* For historical reasons, this function is used both to query configuration
   1176    * state on a server as well as handshake state on a client. However, whether
   1177    * |ssl| is a client or server is not known until explicitly configured with
   1178    * |SSL_set_connect_state|. If |handshake_func| is NULL, |ssl| is in an
   1179    * indeterminate mode and |ssl->server| is unset. */
   1180   if (ssl->handshake_func != NULL && !ssl->server) {
   1181     if (ssl->s3->hs != NULL) {
   1182       return buffer_names_to_x509(ssl->s3->hs->ca_names,
   1183                                   &ssl->s3->hs->cached_x509_ca_names);
   1184     }
   1185 
   1186     return NULL;
   1187   }
   1188 
   1189   if (ssl->client_CA != NULL) {
   1190     return buffer_names_to_x509(
   1191         ssl->client_CA, (STACK_OF(X509_NAME) **)&ssl->cached_x509_client_CA);
   1192   }
   1193   return buffer_names_to_x509(ssl->ctx->client_CA,
   1194                               &ssl->ctx->cached_x509_client_CA);
   1195 }
   1196 
   1197 STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx) {
   1198   check_ssl_ctx_x509_method(ctx);
   1199   CRYPTO_MUTEX_lock_write((CRYPTO_MUTEX *) &ctx->lock);
   1200   STACK_OF(X509_NAME) *ret = buffer_names_to_x509(
   1201       ctx->client_CA, (STACK_OF(X509_NAME) **)&ctx->cached_x509_client_CA);
   1202   CRYPTO_MUTEX_unlock_write((CRYPTO_MUTEX *) &ctx->lock);
   1203   return ret;
   1204 }
   1205 
   1206 static int add_client_CA(STACK_OF(CRYPTO_BUFFER) **names, X509 *x509,
   1207                          CRYPTO_BUFFER_POOL *pool) {
   1208   if (x509 == NULL) {
   1209     return 0;
   1210   }
   1211 
   1212   uint8_t *outp = NULL;
   1213   int len = i2d_X509_NAME(X509_get_subject_name(x509), &outp);
   1214   if (len < 0) {
   1215     return 0;
   1216   }
   1217 
   1218   CRYPTO_BUFFER *buffer = CRYPTO_BUFFER_new(outp, len, pool);
   1219   OPENSSL_free(outp);
   1220   if (buffer == NULL) {
   1221     return 0;
   1222   }
   1223 
   1224   int alloced = 0;
   1225   if (*names == NULL) {
   1226     *names = sk_CRYPTO_BUFFER_new_null();
   1227     alloced = 1;
   1228 
   1229     if (*names == NULL) {
   1230       CRYPTO_BUFFER_free(buffer);
   1231       return 0;
   1232     }
   1233   }
   1234 
   1235   if (!sk_CRYPTO_BUFFER_push(*names, buffer)) {
   1236     CRYPTO_BUFFER_free(buffer);
   1237     if (alloced) {
   1238       sk_CRYPTO_BUFFER_pop_free(*names, CRYPTO_BUFFER_free);
   1239       *names = NULL;
   1240     }
   1241     return 0;
   1242   }
   1243 
   1244   return 1;
   1245 }
   1246 
   1247 int SSL_add_client_CA(SSL *ssl, X509 *x509) {
   1248   check_ssl_x509_method(ssl);
   1249   if (!add_client_CA(&ssl->client_CA, x509, ssl->ctx->pool)) {
   1250     return 0;
   1251   }
   1252 
   1253   ssl_crypto_x509_ssl_flush_cached_client_CA(ssl);
   1254   return 1;
   1255 }
   1256 
   1257 int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x509) {
   1258   check_ssl_ctx_x509_method(ctx);
   1259   if (!add_client_CA(&ctx->client_CA, x509, ctx->pool)) {
   1260     return 0;
   1261   }
   1262 
   1263   ssl_crypto_x509_ssl_ctx_flush_cached_client_CA(ctx);
   1264   return 1;
   1265 }
   1266 
   1267 static int do_client_cert_cb(SSL *ssl, void *arg) {
   1268   if (ssl_has_certificate(ssl) || ssl->ctx->client_cert_cb == NULL) {
   1269     return 1;
   1270   }
   1271 
   1272   X509 *x509 = NULL;
   1273   EVP_PKEY *pkey = NULL;
   1274   int ret = ssl->ctx->client_cert_cb(ssl, &x509, &pkey);
   1275   if (ret < 0) {
   1276     return -1;
   1277   }
   1278 
   1279   if (ret != 0) {
   1280     if (!SSL_use_certificate(ssl, x509) ||
   1281         !SSL_use_PrivateKey(ssl, pkey)) {
   1282       return 0;
   1283     }
   1284   }
   1285 
   1286   X509_free(x509);
   1287   EVP_PKEY_free(pkey);
   1288   return 1;
   1289 }
   1290 
   1291 void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl,
   1292                                                         X509 **out_x509,
   1293                                                         EVP_PKEY **out_pkey)) {
   1294   check_ssl_ctx_x509_method(ctx);
   1295   /* Emulate the old client certificate callback with the new one. */
   1296   SSL_CTX_set_cert_cb(ctx, do_client_cert_cb, NULL);
   1297   ctx->client_cert_cb = cb;
   1298 }
   1299 
   1300 static int set_cert_store(X509_STORE **store_ptr, X509_STORE *new_store,
   1301                           int take_ref) {
   1302   X509_STORE_free(*store_ptr);
   1303   *store_ptr = new_store;
   1304 
   1305   if (new_store != NULL && take_ref) {
   1306     X509_STORE_up_ref(new_store);
   1307   }
   1308 
   1309   return 1;
   1310 }
   1311 
   1312 int SSL_get_ex_data_X509_STORE_CTX_idx(void) {
   1313   /* The ex_data index to go from |X509_STORE_CTX| to |SSL| always uses the
   1314    * reserved app_data slot. Before ex_data was introduced, app_data was used.
   1315    * Avoid breaking any software which assumes |X509_STORE_CTX_get_app_data|
   1316    * works. */
   1317   return 0;
   1318 }
   1319 
   1320 int SSL_CTX_set0_verify_cert_store(SSL_CTX *ctx, X509_STORE *store) {
   1321   check_ssl_ctx_x509_method(ctx);
   1322   return set_cert_store(&ctx->cert->verify_store, store, 0);
   1323 }
   1324 
   1325 int SSL_CTX_set1_verify_cert_store(SSL_CTX *ctx, X509_STORE *store) {
   1326   check_ssl_ctx_x509_method(ctx);
   1327   return set_cert_store(&ctx->cert->verify_store, store, 1);
   1328 }
   1329 
   1330 int SSL_set0_verify_cert_store(SSL *ssl, X509_STORE *store) {
   1331   check_ssl_x509_method(ssl);
   1332   return set_cert_store(&ssl->cert->verify_store, store, 0);
   1333 }
   1334 
   1335 int SSL_set1_verify_cert_store(SSL *ssl, X509_STORE *store) {
   1336   check_ssl_x509_method(ssl);
   1337   return set_cert_store(&ssl->cert->verify_store, store, 1);
   1338 }
   1339