Adam Langley | d1515a3 | 2017-01-23 16:46:16 -0800 | [diff] [blame] | 1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 2 | * All rights reserved. |
| 3 | * |
| 4 | * This package is an SSL implementation written |
| 5 | * by Eric Young (eay@cryptsoft.com). |
| 6 | * The implementation was written so as to conform with Netscapes SSL. |
| 7 | * |
| 8 | * This library is free for commercial and non-commercial use as long as |
| 9 | * the following conditions are aheared to. The following conditions |
| 10 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 12 | * included with this distribution is covered by the same copyright terms |
| 13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 14 | * |
| 15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 16 | * the code are not to be removed. |
| 17 | * If this package is used in a product, Eric Young should be given attribution |
| 18 | * as the author of the parts of the library used. |
| 19 | * This can be in the form of a textual message at program startup or |
| 20 | * in documentation (online or textual) provided with the package. |
| 21 | * |
| 22 | * Redistribution and use in source and binary forms, with or without |
| 23 | * modification, are permitted provided that the following conditions |
| 24 | * are met: |
| 25 | * 1. Redistributions of source code must retain the copyright |
| 26 | * notice, this list of conditions and the following disclaimer. |
| 27 | * 2. Redistributions in binary form must reproduce the above copyright |
| 28 | * notice, this list of conditions and the following disclaimer in the |
| 29 | * documentation and/or other materials provided with the distribution. |
| 30 | * 3. All advertising materials mentioning features or use of this software |
| 31 | * must display the following acknowledgement: |
| 32 | * "This product includes cryptographic software written by |
| 33 | * Eric Young (eay@cryptsoft.com)" |
| 34 | * The word 'cryptographic' can be left out if the rouines from the library |
| 35 | * being used are not cryptographic related :-). |
| 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 37 | * the apps directory (application code) you must include an acknowledgement: |
| 38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 39 | * |
| 40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 50 | * SUCH DAMAGE. |
| 51 | * |
| 52 | * The licence and distribution terms for any publically available version or |
| 53 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 54 | * copied and put under another distribution licence |
| 55 | * [including the GNU Public Licence.] |
| 56 | */ |
| 57 | /* ==================================================================== |
| 58 | * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. |
| 59 | * |
| 60 | * Redistribution and use in source and binary forms, with or without |
| 61 | * modification, are permitted provided that the following conditions |
| 62 | * are met: |
| 63 | * |
| 64 | * 1. Redistributions of source code must retain the above copyright |
| 65 | * notice, this list of conditions and the following disclaimer. |
| 66 | * |
| 67 | * 2. Redistributions in binary form must reproduce the above copyright |
| 68 | * notice, this list of conditions and the following disclaimer in |
| 69 | * the documentation and/or other materials provided with the |
| 70 | * distribution. |
| 71 | * |
| 72 | * 3. All advertising materials mentioning features or use of this |
| 73 | * software must display the following acknowledgment: |
| 74 | * "This product includes software developed by the OpenSSL Project |
| 75 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
| 76 | * |
| 77 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 78 | * endorse or promote products derived from this software without |
| 79 | * prior written permission. For written permission, please contact |
| 80 | * openssl-core@openssl.org. |
| 81 | * |
| 82 | * 5. Products derived from this software may not be called "OpenSSL" |
| 83 | * nor may "OpenSSL" appear in their names without prior written |
| 84 | * permission of the OpenSSL Project. |
| 85 | * |
| 86 | * 6. Redistributions of any form whatsoever must retain the following |
| 87 | * acknowledgment: |
| 88 | * "This product includes software developed by the OpenSSL Project |
| 89 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
| 90 | * |
| 91 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 92 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 93 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 94 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 95 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 96 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 97 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 98 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 99 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 100 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 101 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 102 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 103 | * ==================================================================== |
| 104 | * |
| 105 | * This product includes cryptographic software written by Eric Young |
| 106 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 107 | * Hudson (tjh@cryptsoft.com). |
| 108 | * |
| 109 | */ |
| 110 | /* ==================================================================== |
| 111 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. |
| 112 | * ECC cipher suite support in OpenSSL originally developed by |
| 113 | * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. |
| 114 | */ |
| 115 | /* ==================================================================== |
| 116 | * Copyright 2005 Nokia. All rights reserved. |
| 117 | * |
| 118 | * The portions of the attached software ("Contribution") is developed by |
| 119 | * Nokia Corporation and is licensed pursuant to the OpenSSL open source |
| 120 | * license. |
| 121 | * |
| 122 | * The Contribution, originally written by Mika Kousa and Pasi Eronen of |
| 123 | * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites |
| 124 | * support (see RFC 4279) to OpenSSL. |
| 125 | * |
| 126 | * No patent licenses or other rights except those expressly stated in |
| 127 | * the OpenSSL open source license shall be deemed granted or received |
| 128 | * expressly, by implication, estoppel, or otherwise. |
| 129 | * |
| 130 | * No assurances are provided by Nokia that the Contribution does not |
| 131 | * infringe the patent or other intellectual property rights of any third |
| 132 | * party or that the license provides you with all the necessary rights |
| 133 | * to make use of the Contribution. |
| 134 | * |
| 135 | * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN |
| 136 | * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA |
| 137 | * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY |
| 138 | * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR |
| 139 | * OTHERWISE. */ |
| 140 | |
| 141 | #include <openssl/ssl.h> |
| 142 | |
Adam Langley | 3509dac | 2017-02-01 11:59:18 -0800 | [diff] [blame] | 143 | #include <assert.h> |
| 144 | |
Adam Langley | 46db7af | 2017-02-01 15:49:37 -0800 | [diff] [blame] | 145 | #include <openssl/asn1.h> |
| 146 | #include <openssl/bytestring.h> |
| 147 | #include <openssl/err.h> |
| 148 | #include <openssl/pem.h> |
Adam Langley | d1515a3 | 2017-01-23 16:46:16 -0800 | [diff] [blame] | 149 | #include <openssl/stack.h> |
| 150 | #include <openssl/x509.h> |
Adam Langley | 3509dac | 2017-02-01 11:59:18 -0800 | [diff] [blame] | 151 | #include <openssl/x509v3.h> |
Adam Langley | d1515a3 | 2017-01-23 16:46:16 -0800 | [diff] [blame] | 152 | #include <openssl/x509_vfy.h> |
| 153 | |
| 154 | #include "internal.h" |
Adam Langley | 34b4c82 | 2017-02-02 10:57:17 -0800 | [diff] [blame^] | 155 | #include "../crypto/internal.h" |
Adam Langley | d1515a3 | 2017-01-23 16:46:16 -0800 | [diff] [blame] | 156 | |
| 157 | |
| 158 | X509 *SSL_get_peer_certificate(const SSL *ssl) { |
| 159 | if (ssl == NULL) { |
| 160 | return NULL; |
| 161 | } |
| 162 | SSL_SESSION *session = SSL_get_session(ssl); |
| 163 | if (session == NULL || session->x509_peer == NULL) { |
| 164 | return NULL; |
| 165 | } |
| 166 | X509_up_ref(session->x509_peer); |
| 167 | return session->x509_peer; |
| 168 | } |
| 169 | |
| 170 | STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *ssl) { |
| 171 | if (ssl == NULL) { |
| 172 | return NULL; |
| 173 | } |
| 174 | SSL_SESSION *session = SSL_get_session(ssl); |
| 175 | if (session == NULL || |
| 176 | session->x509_chain == NULL) { |
| 177 | return NULL; |
| 178 | } |
| 179 | |
| 180 | if (!ssl->server) { |
| 181 | return session->x509_chain; |
| 182 | } |
| 183 | |
| 184 | /* OpenSSL historically didn't include the leaf certificate in the returned |
| 185 | * certificate chain, but only for servers. */ |
| 186 | if (session->x509_chain_without_leaf == NULL) { |
| 187 | session->x509_chain_without_leaf = sk_X509_new_null(); |
| 188 | if (session->x509_chain_without_leaf == NULL) { |
| 189 | return NULL; |
| 190 | } |
| 191 | |
| 192 | for (size_t i = 1; i < sk_X509_num(session->x509_chain); i++) { |
| 193 | X509 *cert = sk_X509_value(session->x509_chain, i); |
| 194 | if (!sk_X509_push(session->x509_chain_without_leaf, cert)) { |
| 195 | sk_X509_pop_free(session->x509_chain_without_leaf, X509_free); |
| 196 | session->x509_chain_without_leaf = NULL; |
| 197 | return NULL; |
| 198 | } |
| 199 | X509_up_ref(cert); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | return session->x509_chain_without_leaf; |
| 204 | } |
| 205 | |
| 206 | STACK_OF(X509) *SSL_get_peer_full_cert_chain(const SSL *ssl) { |
| 207 | SSL_SESSION *session = SSL_get_session(ssl); |
| 208 | if (session == NULL) { |
| 209 | return NULL; |
| 210 | } |
| 211 | |
| 212 | return session->x509_chain; |
| 213 | } |
| 214 | |
| 215 | int SSL_CTX_set_purpose(SSL_CTX *ctx, int purpose) { |
| 216 | return X509_VERIFY_PARAM_set_purpose(ctx->param, purpose); |
| 217 | } |
| 218 | |
| 219 | int SSL_set_purpose(SSL *ssl, int purpose) { |
| 220 | return X509_VERIFY_PARAM_set_purpose(ssl->param, purpose); |
| 221 | } |
| 222 | |
| 223 | int SSL_CTX_set_trust(SSL_CTX *ctx, int trust) { |
| 224 | return X509_VERIFY_PARAM_set_trust(ctx->param, trust); |
| 225 | } |
| 226 | |
| 227 | int SSL_set_trust(SSL *ssl, int trust) { |
| 228 | return X509_VERIFY_PARAM_set_trust(ssl->param, trust); |
| 229 | } |
| 230 | |
| 231 | int SSL_CTX_set1_param(SSL_CTX *ctx, const X509_VERIFY_PARAM *param) { |
| 232 | return X509_VERIFY_PARAM_set1(ctx->param, param); |
| 233 | } |
| 234 | |
| 235 | int SSL_set1_param(SSL *ssl, const X509_VERIFY_PARAM *param) { |
| 236 | return X509_VERIFY_PARAM_set1(ssl->param, param); |
| 237 | } |
| 238 | |
| 239 | X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) { return ctx->param; } |
| 240 | |
| 241 | X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) { return ssl->param; } |
| 242 | |
| 243 | int SSL_get_verify_depth(const SSL *ssl) { |
| 244 | return X509_VERIFY_PARAM_get_depth(ssl->param); |
| 245 | } |
| 246 | |
| 247 | int (*SSL_get_verify_callback(const SSL *ssl))(int, X509_STORE_CTX *) { |
| 248 | return ssl->verify_callback; |
| 249 | } |
| 250 | |
| 251 | int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) { return ctx->verify_mode; } |
| 252 | |
| 253 | int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) { |
| 254 | return X509_VERIFY_PARAM_get_depth(ctx->param); |
| 255 | } |
| 256 | |
| 257 | int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))( |
| 258 | int ok, X509_STORE_CTX *store_ctx) { |
| 259 | return ctx->default_verify_callback; |
| 260 | } |
| 261 | |
| 262 | void SSL_set_verify(SSL *ssl, int mode, |
| 263 | int (*callback)(int ok, X509_STORE_CTX *store_ctx)) { |
| 264 | ssl->verify_mode = mode; |
| 265 | if (callback != NULL) { |
| 266 | ssl->verify_callback = callback; |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | void SSL_set_verify_depth(SSL *ssl, int depth) { |
| 271 | X509_VERIFY_PARAM_set_depth(ssl->param, depth); |
| 272 | } |
| 273 | |
| 274 | void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx, |
| 275 | int (*cb)(X509_STORE_CTX *store_ctx, |
| 276 | void *arg), |
| 277 | void *arg) { |
| 278 | ctx->app_verify_callback = cb; |
| 279 | ctx->app_verify_arg = arg; |
| 280 | } |
| 281 | |
| 282 | void SSL_CTX_set_verify(SSL_CTX *ctx, int mode, |
| 283 | int (*cb)(int, X509_STORE_CTX *)) { |
| 284 | ctx->verify_mode = mode; |
| 285 | ctx->default_verify_callback = cb; |
| 286 | } |
| 287 | |
| 288 | void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) { |
| 289 | X509_VERIFY_PARAM_set_depth(ctx->param, depth); |
| 290 | } |
| 291 | |
Adam Langley | d1515a3 | 2017-01-23 16:46:16 -0800 | [diff] [blame] | 292 | int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) { |
| 293 | return X509_STORE_set_default_paths(ctx->cert_store); |
| 294 | } |
| 295 | |
| 296 | int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *ca_file, |
| 297 | const char *ca_dir) { |
| 298 | return X509_STORE_load_locations(ctx->cert_store, ca_file, ca_dir); |
| 299 | } |
| 300 | |
| 301 | void SSL_set_verify_result(SSL *ssl, long result) { |
| 302 | if (result != X509_V_OK) { |
| 303 | abort(); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | long SSL_get_verify_result(const SSL *ssl) { |
| 308 | SSL_SESSION *session = SSL_get_session(ssl); |
| 309 | if (session == NULL) { |
| 310 | return X509_V_ERR_INVALID_CALL; |
| 311 | } |
| 312 | return session->verify_result; |
| 313 | } |
| 314 | |
| 315 | X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) { |
| 316 | return ctx->cert_store; |
| 317 | } |
| 318 | |
| 319 | void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) { |
| 320 | X509_STORE_free(ctx->cert_store); |
| 321 | ctx->cert_store = store; |
| 322 | } |
Adam Langley | 3509dac | 2017-02-01 11:59:18 -0800 | [diff] [blame] | 323 | |
| 324 | static void ssl_crypto_x509_flush_cached_leaf(CERT *cert) { |
| 325 | X509_free(cert->x509_leaf); |
| 326 | cert->x509_leaf = NULL; |
| 327 | } |
| 328 | |
| 329 | static void ssl_crypto_x509_flush_cached_chain(CERT *cert) { |
| 330 | sk_X509_pop_free(cert->x509_chain, X509_free); |
| 331 | cert->x509_chain = NULL; |
| 332 | } |
| 333 | |
| 334 | static void ssl_crypto_x509_clear(CERT *cert) { |
| 335 | ssl_crypto_x509_flush_cached_leaf(cert); |
| 336 | ssl_crypto_x509_flush_cached_chain(cert); |
| 337 | |
| 338 | X509_free(cert->x509_stash); |
| 339 | cert->x509_stash = NULL; |
| 340 | } |
| 341 | |
Adam Langley | 46db7af | 2017-02-01 15:49:37 -0800 | [diff] [blame] | 342 | static int ssl_crypto_x509_session_cache_objects(SSL_SESSION *sess) { |
| 343 | STACK_OF(X509) *chain = NULL; |
| 344 | const size_t num_certs = sk_CRYPTO_BUFFER_num(sess->certs); |
| 345 | |
| 346 | if (num_certs > 0) { |
| 347 | chain = sk_X509_new_null(); |
| 348 | if (chain == NULL) { |
| 349 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 350 | goto err; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | X509 *leaf = NULL; |
| 355 | for (size_t i = 0; i < num_certs; i++) { |
| 356 | X509 *x509 = X509_parse_from_buffer(sk_CRYPTO_BUFFER_value(sess->certs, i)); |
| 357 | if (x509 == NULL) { |
| 358 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 359 | goto err; |
| 360 | } |
| 361 | if (!sk_X509_push(chain, x509)) { |
| 362 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 363 | X509_free(x509); |
| 364 | goto err; |
| 365 | } |
| 366 | if (i == 0) { |
| 367 | leaf = x509; |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | sk_X509_pop_free(sess->x509_chain, X509_free); |
| 372 | sess->x509_chain = chain; |
| 373 | sk_X509_pop_free(sess->x509_chain_without_leaf, X509_free); |
| 374 | sess->x509_chain_without_leaf = NULL; |
| 375 | |
| 376 | X509_free(sess->x509_peer); |
| 377 | if (leaf != NULL) { |
| 378 | X509_up_ref(leaf); |
| 379 | } |
| 380 | sess->x509_peer = leaf; |
| 381 | |
| 382 | return 1; |
| 383 | |
| 384 | err: |
| 385 | sk_X509_pop_free(chain, X509_free); |
| 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | static int ssl_crypto_x509_session_dup(SSL_SESSION *new_session, |
| 390 | const SSL_SESSION *session) { |
| 391 | if (session->x509_peer != NULL) { |
| 392 | X509_up_ref(session->x509_peer); |
| 393 | new_session->x509_peer = session->x509_peer; |
| 394 | } |
| 395 | if (session->x509_chain != NULL) { |
| 396 | new_session->x509_chain = X509_chain_up_ref(session->x509_chain); |
| 397 | if (new_session->x509_chain == NULL) { |
| 398 | return 0; |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | return 1; |
| 403 | } |
| 404 | |
| 405 | static void ssl_crypto_x509_session_clear(SSL_SESSION *session) { |
| 406 | X509_free(session->x509_peer); |
| 407 | session->x509_peer = NULL; |
| 408 | sk_X509_pop_free(session->x509_chain, X509_free); |
| 409 | session->x509_chain = NULL; |
| 410 | sk_X509_pop_free(session->x509_chain_without_leaf, X509_free); |
| 411 | session->x509_chain_without_leaf = NULL; |
| 412 | } |
| 413 | |
Adam Langley | 34b4c82 | 2017-02-02 10:57:17 -0800 | [diff] [blame^] | 414 | static void ssl_crypto_x509_hs_flush_cached_ca_names(SSL_HANDSHAKE *hs) { |
| 415 | sk_X509_NAME_pop_free(hs->cached_x509_ca_names, X509_NAME_free); |
| 416 | hs->cached_x509_ca_names = NULL; |
| 417 | } |
| 418 | |
| 419 | static void ssl_crypto_x509_ssl_flush_cached_client_CA(SSL *ssl) { |
| 420 | sk_X509_NAME_pop_free(ssl->cached_x509_client_CA, X509_NAME_free); |
| 421 | ssl->cached_x509_client_CA = NULL; |
| 422 | } |
| 423 | |
| 424 | static void ssl_crypto_x509_ssl_ctx_flush_cached_client_CA(SSL_CTX *ctx) { |
| 425 | sk_X509_NAME_pop_free(ctx->cached_x509_client_CA, X509_NAME_free); |
| 426 | ctx->cached_x509_client_CA = NULL; |
| 427 | } |
| 428 | |
Adam Langley | 3509dac | 2017-02-01 11:59:18 -0800 | [diff] [blame] | 429 | const SSL_X509_METHOD ssl_crypto_x509_method = { |
| 430 | ssl_crypto_x509_clear, |
| 431 | ssl_crypto_x509_flush_cached_chain, |
| 432 | ssl_crypto_x509_flush_cached_leaf, |
Adam Langley | 46db7af | 2017-02-01 15:49:37 -0800 | [diff] [blame] | 433 | ssl_crypto_x509_session_cache_objects, |
| 434 | ssl_crypto_x509_session_dup, |
| 435 | ssl_crypto_x509_session_clear, |
Adam Langley | 34b4c82 | 2017-02-02 10:57:17 -0800 | [diff] [blame^] | 436 | ssl_crypto_x509_hs_flush_cached_ca_names, |
| 437 | ssl_crypto_x509_ssl_flush_cached_client_CA, |
| 438 | ssl_crypto_x509_ssl_ctx_flush_cached_client_CA, |
Adam Langley | 3509dac | 2017-02-01 11:59:18 -0800 | [diff] [blame] | 439 | }; |
| 440 | |
| 441 | /* x509_to_buffer returns a |CRYPTO_BUFFER| that contains the serialised |
| 442 | * contents of |x509|. */ |
| 443 | static CRYPTO_BUFFER *x509_to_buffer(X509 *x509) { |
| 444 | uint8_t *buf = NULL; |
| 445 | int cert_len = i2d_X509(x509, &buf); |
| 446 | if (cert_len <= 0) { |
| 447 | return 0; |
| 448 | } |
| 449 | |
| 450 | CRYPTO_BUFFER *buffer = CRYPTO_BUFFER_new(buf, cert_len, NULL); |
| 451 | OPENSSL_free(buf); |
| 452 | |
| 453 | return buffer; |
| 454 | } |
| 455 | |
| 456 | static int ssl_use_certificate(CERT *cert, X509 *x) { |
| 457 | if (x == NULL) { |
| 458 | OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER); |
| 459 | return 0; |
| 460 | } |
| 461 | |
| 462 | CRYPTO_BUFFER *buffer = x509_to_buffer(x); |
| 463 | if (buffer == NULL) { |
| 464 | return 0; |
| 465 | } |
| 466 | |
| 467 | const int ok = ssl_set_cert(cert, buffer); |
| 468 | CRYPTO_BUFFER_free(buffer); |
| 469 | return ok; |
| 470 | } |
| 471 | |
| 472 | int SSL_use_certificate(SSL *ssl, X509 *x) { |
| 473 | return ssl_use_certificate(ssl->cert, x); |
| 474 | } |
| 475 | |
| 476 | int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x) { |
| 477 | return ssl_use_certificate(ctx->cert, x); |
| 478 | } |
| 479 | |
| 480 | /* ssl_cert_cache_leaf_cert sets |cert->x509_leaf|, if currently NULL, from the |
| 481 | * first element of |cert->chain|. */ |
| 482 | static int ssl_cert_cache_leaf_cert(CERT *cert) { |
| 483 | assert(cert->x509_method); |
| 484 | |
| 485 | if (cert->x509_leaf != NULL || |
| 486 | cert->chain == NULL) { |
| 487 | return 1; |
| 488 | } |
| 489 | |
| 490 | CRYPTO_BUFFER *leaf = sk_CRYPTO_BUFFER_value(cert->chain, 0); |
| 491 | if (!leaf) { |
| 492 | return 1; |
| 493 | } |
| 494 | |
| 495 | cert->x509_leaf = X509_parse_from_buffer(leaf); |
| 496 | return cert->x509_leaf != NULL; |
| 497 | } |
| 498 | |
| 499 | static X509 *ssl_cert_get0_leaf(CERT *cert) { |
| 500 | if (cert->x509_leaf == NULL && |
| 501 | !ssl_cert_cache_leaf_cert(cert)) { |
| 502 | return NULL; |
| 503 | } |
| 504 | |
| 505 | return cert->x509_leaf; |
| 506 | } |
| 507 | |
| 508 | X509 *SSL_get_certificate(const SSL *ssl) { |
| 509 | return ssl_cert_get0_leaf(ssl->cert); |
| 510 | } |
| 511 | |
| 512 | X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) { |
Adam Langley | 34b4c82 | 2017-02-02 10:57:17 -0800 | [diff] [blame^] | 513 | CRYPTO_MUTEX_lock_write((CRYPTO_MUTEX *) &ctx->lock); |
| 514 | X509 *ret = ssl_cert_get0_leaf(ctx->cert); |
| 515 | CRYPTO_MUTEX_unlock_write((CRYPTO_MUTEX *) &ctx->lock); |
| 516 | return ret; |
Adam Langley | 3509dac | 2017-02-01 11:59:18 -0800 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | /* new_leafless_chain returns a fresh stack of buffers set to {NULL}. */ |
| 520 | static STACK_OF(CRYPTO_BUFFER) *new_leafless_chain(void) { |
| 521 | STACK_OF(CRYPTO_BUFFER) *chain = sk_CRYPTO_BUFFER_new_null(); |
| 522 | if (chain == NULL) { |
| 523 | return NULL; |
| 524 | } |
| 525 | |
| 526 | if (!sk_CRYPTO_BUFFER_push(chain, NULL)) { |
| 527 | sk_CRYPTO_BUFFER_free(chain); |
| 528 | return NULL; |
| 529 | } |
| 530 | |
| 531 | return chain; |
| 532 | } |
| 533 | |
| 534 | /* ssl_cert_set_chain sets elements 1.. of |cert->chain| to the serialised |
| 535 | * forms of elements of |chain|. It returns one on success or zero on error, in |
| 536 | * which case no change to |cert->chain| is made. It preverses the existing |
| 537 | * leaf from |cert->chain|, if any. */ |
| 538 | static int ssl_cert_set_chain(CERT *cert, STACK_OF(X509) *chain) { |
| 539 | STACK_OF(CRYPTO_BUFFER) *new_chain = NULL; |
| 540 | |
| 541 | if (cert->chain != NULL) { |
| 542 | new_chain = sk_CRYPTO_BUFFER_new_null(); |
| 543 | if (new_chain == NULL) { |
| 544 | return 0; |
| 545 | } |
| 546 | |
| 547 | CRYPTO_BUFFER *leaf = sk_CRYPTO_BUFFER_value(cert->chain, 0); |
| 548 | if (!sk_CRYPTO_BUFFER_push(new_chain, leaf)) { |
| 549 | goto err; |
| 550 | } |
| 551 | /* |leaf| might be NULL if it's a “leafless” chain. */ |
| 552 | if (leaf != NULL) { |
| 553 | CRYPTO_BUFFER_up_ref(leaf); |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | for (size_t i = 0; i < sk_X509_num(chain); i++) { |
| 558 | if (new_chain == NULL) { |
| 559 | new_chain = new_leafless_chain(); |
| 560 | if (new_chain == NULL) { |
| 561 | goto err; |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | CRYPTO_BUFFER *buffer = x509_to_buffer(sk_X509_value(chain, i)); |
| 566 | if (buffer == NULL || |
| 567 | !sk_CRYPTO_BUFFER_push(new_chain, buffer)) { |
| 568 | CRYPTO_BUFFER_free(buffer); |
| 569 | goto err; |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | sk_CRYPTO_BUFFER_pop_free(cert->chain, CRYPTO_BUFFER_free); |
| 574 | cert->chain = new_chain; |
| 575 | |
| 576 | return 1; |
| 577 | |
| 578 | err: |
| 579 | sk_CRYPTO_BUFFER_pop_free(new_chain, CRYPTO_BUFFER_free); |
| 580 | return 0; |
| 581 | } |
| 582 | |
| 583 | static int ssl_cert_set0_chain(CERT *cert, STACK_OF(X509) *chain) { |
| 584 | if (!ssl_cert_set_chain(cert, chain)) { |
| 585 | return 0; |
| 586 | } |
| 587 | |
| 588 | sk_X509_pop_free(chain, X509_free); |
| 589 | ssl_crypto_x509_flush_cached_chain(cert); |
| 590 | return 1; |
| 591 | } |
| 592 | |
| 593 | static int ssl_cert_set1_chain(CERT *cert, STACK_OF(X509) *chain) { |
| 594 | if (!ssl_cert_set_chain(cert, chain)) { |
| 595 | return 0; |
| 596 | } |
| 597 | |
| 598 | ssl_crypto_x509_flush_cached_chain(cert); |
| 599 | return 1; |
| 600 | } |
| 601 | |
| 602 | static int ssl_cert_append_cert(CERT *cert, X509 *x509) { |
| 603 | assert(cert->x509_method); |
| 604 | |
| 605 | CRYPTO_BUFFER *buffer = x509_to_buffer(x509); |
| 606 | if (buffer == NULL) { |
| 607 | return 0; |
| 608 | } |
| 609 | |
| 610 | if (cert->chain != NULL) { |
| 611 | if (!sk_CRYPTO_BUFFER_push(cert->chain, buffer)) { |
| 612 | CRYPTO_BUFFER_free(buffer); |
| 613 | return 0; |
| 614 | } |
| 615 | |
| 616 | return 1; |
| 617 | } |
| 618 | |
| 619 | cert->chain = new_leafless_chain(); |
| 620 | if (cert->chain == NULL || |
| 621 | !sk_CRYPTO_BUFFER_push(cert->chain, buffer)) { |
| 622 | CRYPTO_BUFFER_free(buffer); |
| 623 | sk_CRYPTO_BUFFER_free(cert->chain); |
| 624 | cert->chain = NULL; |
| 625 | return 0; |
| 626 | } |
| 627 | |
| 628 | return 1; |
| 629 | } |
| 630 | |
| 631 | static int ssl_cert_add0_chain_cert(CERT *cert, X509 *x509) { |
| 632 | if (!ssl_cert_append_cert(cert, x509)) { |
| 633 | return 0; |
| 634 | } |
| 635 | |
| 636 | X509_free(cert->x509_stash); |
| 637 | cert->x509_stash = x509; |
| 638 | ssl_crypto_x509_flush_cached_chain(cert); |
| 639 | return 1; |
| 640 | } |
| 641 | |
| 642 | static int ssl_cert_add1_chain_cert(CERT *cert, X509 *x509) { |
| 643 | if (!ssl_cert_append_cert(cert, x509)) { |
| 644 | return 0; |
| 645 | } |
| 646 | |
| 647 | ssl_crypto_x509_flush_cached_chain(cert); |
| 648 | return 1; |
| 649 | } |
| 650 | |
| 651 | int SSL_CTX_set0_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) { |
| 652 | return ssl_cert_set0_chain(ctx->cert, chain); |
| 653 | } |
| 654 | |
| 655 | int SSL_CTX_set1_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) { |
| 656 | return ssl_cert_set1_chain(ctx->cert, chain); |
| 657 | } |
| 658 | |
| 659 | int SSL_set0_chain(SSL *ssl, STACK_OF(X509) *chain) { |
| 660 | return ssl_cert_set0_chain(ssl->cert, chain); |
| 661 | } |
| 662 | |
| 663 | int SSL_set1_chain(SSL *ssl, STACK_OF(X509) *chain) { |
| 664 | return ssl_cert_set1_chain(ssl->cert, chain); |
| 665 | } |
| 666 | |
| 667 | int SSL_CTX_add0_chain_cert(SSL_CTX *ctx, X509 *x509) { |
| 668 | return ssl_cert_add0_chain_cert(ctx->cert, x509); |
| 669 | } |
| 670 | |
| 671 | int SSL_CTX_add1_chain_cert(SSL_CTX *ctx, X509 *x509) { |
| 672 | return ssl_cert_add1_chain_cert(ctx->cert, x509); |
| 673 | } |
| 674 | |
| 675 | int SSL_CTX_add_extra_chain_cert(SSL_CTX *ctx, X509 *x509) { |
| 676 | return SSL_CTX_add0_chain_cert(ctx, x509); |
| 677 | } |
| 678 | |
| 679 | int SSL_add0_chain_cert(SSL *ssl, X509 *x509) { |
| 680 | return ssl_cert_add0_chain_cert(ssl->cert, x509); |
| 681 | } |
| 682 | |
| 683 | int SSL_add1_chain_cert(SSL *ssl, X509 *x509) { |
| 684 | return ssl_cert_add1_chain_cert(ssl->cert, x509); |
| 685 | } |
| 686 | |
| 687 | int SSL_CTX_clear_chain_certs(SSL_CTX *ctx) { |
| 688 | return SSL_CTX_set0_chain(ctx, NULL); |
| 689 | } |
| 690 | |
| 691 | int SSL_CTX_clear_extra_chain_certs(SSL_CTX *ctx) { |
| 692 | return SSL_CTX_clear_chain_certs(ctx); |
| 693 | } |
| 694 | |
| 695 | int SSL_clear_chain_certs(SSL *ssl) { |
| 696 | return SSL_set0_chain(ssl, NULL); |
| 697 | } |
| 698 | |
| 699 | int ssl_auto_chain_if_needed(SSL *ssl) { |
| 700 | /* Only build a chain if there are no intermediates configured and the feature |
| 701 | * isn't disabled. */ |
| 702 | if ((ssl->mode & SSL_MODE_NO_AUTO_CHAIN) || |
| 703 | !ssl_has_certificate(ssl) || |
| 704 | ssl->cert->chain == NULL || |
| 705 | sk_CRYPTO_BUFFER_num(ssl->cert->chain) > 1) { |
| 706 | return 1; |
| 707 | } |
| 708 | |
| 709 | X509 *leaf = |
| 710 | X509_parse_from_buffer(sk_CRYPTO_BUFFER_value(ssl->cert->chain, 0)); |
| 711 | if (!leaf) { |
| 712 | OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB); |
| 713 | return 0; |
| 714 | } |
| 715 | |
| 716 | X509_STORE_CTX ctx; |
| 717 | if (!X509_STORE_CTX_init(&ctx, ssl->ctx->cert_store, leaf, NULL)) { |
| 718 | X509_free(leaf); |
| 719 | OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB); |
| 720 | return 0; |
| 721 | } |
| 722 | |
| 723 | /* Attempt to build a chain, ignoring the result. */ |
| 724 | X509_verify_cert(&ctx); |
| 725 | X509_free(leaf); |
| 726 | ERR_clear_error(); |
| 727 | |
| 728 | /* Remove the leaf from the generated chain. */ |
| 729 | X509_free(sk_X509_shift(ctx.chain)); |
| 730 | |
| 731 | const int ok = ssl_cert_set_chain(ssl->cert, ctx.chain); |
| 732 | X509_STORE_CTX_cleanup(&ctx); |
| 733 | if (!ok) { |
| 734 | return 0; |
| 735 | } |
| 736 | |
| 737 | ssl_crypto_x509_flush_cached_chain(ssl->cert); |
| 738 | |
| 739 | return 1; |
| 740 | } |
| 741 | |
| 742 | /* ssl_cert_cache_chain_certs fills in |cert->x509_chain| from elements 1.. of |
| 743 | * |cert->chain|. */ |
| 744 | static int ssl_cert_cache_chain_certs(CERT *cert) { |
| 745 | assert(cert->x509_method); |
| 746 | |
| 747 | if (cert->x509_chain != NULL || |
| 748 | cert->chain == NULL || |
| 749 | sk_CRYPTO_BUFFER_num(cert->chain) < 2) { |
| 750 | return 1; |
| 751 | } |
| 752 | |
| 753 | STACK_OF(X509) *chain = sk_X509_new_null(); |
| 754 | if (chain == NULL) { |
| 755 | return 0; |
| 756 | } |
| 757 | |
| 758 | for (size_t i = 1; i < sk_CRYPTO_BUFFER_num(cert->chain); i++) { |
| 759 | CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(cert->chain, i); |
| 760 | X509 *x509 = X509_parse_from_buffer(buffer); |
| 761 | if (x509 == NULL || |
| 762 | !sk_X509_push(chain, x509)) { |
| 763 | X509_free(x509); |
| 764 | goto err; |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | cert->x509_chain = chain; |
| 769 | return 1; |
| 770 | |
| 771 | err: |
| 772 | sk_X509_pop_free(chain, X509_free); |
| 773 | return 0; |
| 774 | } |
| 775 | |
| 776 | int SSL_CTX_get0_chain_certs(const SSL_CTX *ctx, STACK_OF(X509) **out_chain) { |
Adam Langley | 34b4c82 | 2017-02-02 10:57:17 -0800 | [diff] [blame^] | 777 | CRYPTO_MUTEX_lock_write((CRYPTO_MUTEX *) &ctx->lock); |
| 778 | const int ret = ssl_cert_cache_chain_certs(ctx->cert); |
| 779 | CRYPTO_MUTEX_unlock_write((CRYPTO_MUTEX *) &ctx->lock); |
| 780 | |
| 781 | if (!ret) { |
Adam Langley | 3509dac | 2017-02-01 11:59:18 -0800 | [diff] [blame] | 782 | *out_chain = NULL; |
| 783 | return 0; |
| 784 | } |
| 785 | |
| 786 | *out_chain = ctx->cert->x509_chain; |
| 787 | return 1; |
| 788 | } |
| 789 | |
| 790 | int SSL_CTX_get_extra_chain_certs(const SSL_CTX *ctx, |
| 791 | STACK_OF(X509) **out_chain) { |
| 792 | return SSL_CTX_get0_chain_certs(ctx, out_chain); |
| 793 | } |
| 794 | |
| 795 | int SSL_get0_chain_certs(const SSL *ssl, STACK_OF(X509) **out_chain) { |
| 796 | if (!ssl_cert_cache_chain_certs(ssl->cert)) { |
| 797 | *out_chain = NULL; |
| 798 | return 0; |
| 799 | } |
| 800 | |
| 801 | *out_chain = ssl->cert->x509_chain; |
| 802 | return 1; |
| 803 | } |
Adam Langley | 46db7af | 2017-02-01 15:49:37 -0800 | [diff] [blame] | 804 | |
| 805 | static SSL_SESSION *ssl_session_new_with_crypto_x509(void) { |
| 806 | return ssl_session_new(&ssl_crypto_x509_method); |
| 807 | } |
| 808 | |
| 809 | SSL_SESSION *d2i_SSL_SESSION_bio(BIO *bio, SSL_SESSION **out) { |
| 810 | return ASN1_d2i_bio_of(SSL_SESSION, ssl_session_new_with_crypto_x509, |
| 811 | d2i_SSL_SESSION, bio, out); |
| 812 | } |
| 813 | |
| 814 | int i2d_SSL_SESSION_bio(BIO *bio, const SSL_SESSION *session) { |
| 815 | return ASN1_i2d_bio_of(SSL_SESSION, i2d_SSL_SESSION, bio, session); |
| 816 | } |
| 817 | |
| 818 | IMPLEMENT_PEM_rw(SSL_SESSION, SSL_SESSION, PEM_STRING_SSL_SESSION, SSL_SESSION) |
| 819 | |
| 820 | SSL_SESSION *d2i_SSL_SESSION(SSL_SESSION **a, const uint8_t **pp, long length) { |
| 821 | if (length < 0) { |
| 822 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 823 | return NULL; |
| 824 | } |
| 825 | |
| 826 | CBS cbs; |
| 827 | CBS_init(&cbs, *pp, length); |
| 828 | |
| 829 | SSL_SESSION *ret = SSL_SESSION_parse(&cbs, &ssl_crypto_x509_method, |
| 830 | NULL /* no buffer pool */); |
| 831 | if (ret == NULL) { |
| 832 | return NULL; |
| 833 | } |
| 834 | |
| 835 | if (a) { |
| 836 | SSL_SESSION_free(*a); |
| 837 | *a = ret; |
| 838 | } |
| 839 | *pp = CBS_data(&cbs); |
| 840 | return ret; |
| 841 | } |
Adam Langley | 34b4c82 | 2017-02-02 10:57:17 -0800 | [diff] [blame^] | 842 | |
| 843 | STACK_OF(X509_NAME) *SSL_dup_CA_list(STACK_OF(X509_NAME) *list) { |
| 844 | return sk_X509_NAME_deep_copy(list, X509_NAME_dup, X509_NAME_free); |
| 845 | } |
| 846 | |
| 847 | static void set_client_CA_list(STACK_OF(CRYPTO_BUFFER) **ca_list, |
| 848 | const STACK_OF(X509_NAME) *name_list, |
| 849 | CRYPTO_BUFFER_POOL *pool) { |
| 850 | STACK_OF(CRYPTO_BUFFER) *buffers = sk_CRYPTO_BUFFER_new_null(); |
| 851 | if (buffers == NULL) { |
| 852 | return; |
| 853 | } |
| 854 | |
| 855 | for (size_t i = 0; i < sk_X509_NAME_num(name_list); i++) { |
| 856 | X509_NAME *name = sk_X509_NAME_value(name_list, i); |
| 857 | uint8_t *outp = NULL; |
| 858 | int len = i2d_X509_NAME(name, &outp); |
| 859 | if (len < 0) { |
| 860 | goto err; |
| 861 | } |
| 862 | |
| 863 | CRYPTO_BUFFER *buffer = CRYPTO_BUFFER_new(outp, len, pool); |
| 864 | OPENSSL_free(outp); |
| 865 | if (buffer == NULL || |
| 866 | !sk_CRYPTO_BUFFER_push(buffers, buffer)) { |
| 867 | CRYPTO_BUFFER_free(buffer); |
| 868 | goto err; |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | sk_CRYPTO_BUFFER_pop_free(*ca_list, CRYPTO_BUFFER_free); |
| 873 | *ca_list = buffers; |
| 874 | return; |
| 875 | |
| 876 | err: |
| 877 | sk_CRYPTO_BUFFER_pop_free(buffers, CRYPTO_BUFFER_free); |
| 878 | } |
| 879 | |
| 880 | void SSL_set_client_CA_list(SSL *ssl, STACK_OF(X509_NAME) *name_list) { |
| 881 | ssl->ctx->x509_method->ssl_flush_cached_client_CA(ssl); |
| 882 | set_client_CA_list(&ssl->client_CA, name_list, ssl->ctx->pool); |
| 883 | sk_X509_NAME_pop_free(name_list, X509_NAME_free); |
| 884 | } |
| 885 | |
| 886 | void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list) { |
| 887 | ctx->x509_method->ssl_ctx_flush_cached_client_CA(ctx); |
| 888 | set_client_CA_list(&ctx->client_CA, name_list, ctx->pool); |
| 889 | sk_X509_NAME_pop_free(name_list, X509_NAME_free); |
| 890 | } |
| 891 | |
| 892 | static STACK_OF(X509_NAME) * |
| 893 | buffer_names_to_x509(const STACK_OF(CRYPTO_BUFFER) *names, |
| 894 | STACK_OF(X509_NAME) **cached) { |
| 895 | if (names == NULL) { |
| 896 | return NULL; |
| 897 | } |
| 898 | |
| 899 | if (*cached != NULL) { |
| 900 | return *cached; |
| 901 | } |
| 902 | |
| 903 | STACK_OF(X509_NAME) *new_cache = sk_X509_NAME_new_null(); |
| 904 | if (new_cache == NULL) { |
| 905 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 906 | return NULL; |
| 907 | } |
| 908 | |
| 909 | for (size_t i = 0; i < sk_CRYPTO_BUFFER_num(names); i++) { |
| 910 | const CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(names, i); |
| 911 | const uint8_t *inp = CRYPTO_BUFFER_data(buffer); |
| 912 | X509_NAME *name = d2i_X509_NAME(NULL, &inp, CRYPTO_BUFFER_len(buffer)); |
| 913 | if (name == NULL || |
| 914 | inp != CRYPTO_BUFFER_data(buffer) + CRYPTO_BUFFER_len(buffer) || |
| 915 | !sk_X509_NAME_push(new_cache, name)) { |
| 916 | X509_NAME_free(name); |
| 917 | goto err; |
| 918 | } |
| 919 | } |
| 920 | |
| 921 | *cached = new_cache; |
| 922 | return new_cache; |
| 923 | |
| 924 | err: |
| 925 | sk_X509_NAME_pop_free(new_cache, X509_NAME_free); |
| 926 | return NULL; |
| 927 | } |
| 928 | |
| 929 | STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *ssl) { |
| 930 | /* For historical reasons, this function is used both to query configuration |
| 931 | * state on a server as well as handshake state on a client. However, whether |
| 932 | * |ssl| is a client or server is not known until explicitly configured with |
| 933 | * |SSL_set_connect_state|. If |handshake_func| is NULL, |ssl| is in an |
| 934 | * indeterminate mode and |ssl->server| is unset. */ |
| 935 | if (ssl->handshake_func != NULL && !ssl->server) { |
| 936 | if (ssl->s3->hs != NULL) { |
| 937 | return buffer_names_to_x509(ssl->s3->hs->ca_names, |
| 938 | &ssl->s3->hs->cached_x509_ca_names); |
| 939 | } |
| 940 | |
| 941 | return NULL; |
| 942 | } |
| 943 | |
| 944 | if (ssl->client_CA != NULL) { |
| 945 | return buffer_names_to_x509( |
| 946 | ssl->client_CA, (STACK_OF(X509_NAME) **)&ssl->cached_x509_client_CA); |
| 947 | } |
| 948 | return buffer_names_to_x509(ssl->ctx->client_CA, |
| 949 | &ssl->ctx->cached_x509_client_CA); |
| 950 | } |
| 951 | |
| 952 | STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx) { |
| 953 | CRYPTO_MUTEX_lock_write((CRYPTO_MUTEX *) &ctx->lock); |
| 954 | STACK_OF(X509_NAME) *ret = buffer_names_to_x509( |
| 955 | ctx->client_CA, (STACK_OF(X509_NAME) **)&ctx->cached_x509_client_CA); |
| 956 | CRYPTO_MUTEX_unlock_write((CRYPTO_MUTEX *) &ctx->lock); |
| 957 | return ret; |
| 958 | } |
| 959 | |
| 960 | static int add_client_CA(STACK_OF(CRYPTO_BUFFER) **names, X509 *x509, |
| 961 | CRYPTO_BUFFER_POOL *pool) { |
| 962 | if (x509 == NULL) { |
| 963 | return 0; |
| 964 | } |
| 965 | |
| 966 | uint8_t *outp = NULL; |
| 967 | int len = i2d_X509_NAME(X509_get_subject_name(x509), &outp); |
| 968 | if (len < 0) { |
| 969 | return 0; |
| 970 | } |
| 971 | |
| 972 | CRYPTO_BUFFER *buffer = CRYPTO_BUFFER_new(outp, len, pool); |
| 973 | OPENSSL_free(outp); |
| 974 | if (buffer == NULL) { |
| 975 | return 0; |
| 976 | } |
| 977 | |
| 978 | int alloced = 0; |
| 979 | if (*names == NULL) { |
| 980 | *names = sk_CRYPTO_BUFFER_new_null(); |
| 981 | alloced = 1; |
| 982 | |
| 983 | if (*names == NULL) { |
| 984 | CRYPTO_BUFFER_free(buffer); |
| 985 | return 0; |
| 986 | } |
| 987 | } |
| 988 | |
| 989 | if (!sk_CRYPTO_BUFFER_push(*names, buffer)) { |
| 990 | CRYPTO_BUFFER_free(buffer); |
| 991 | if (alloced) { |
| 992 | sk_CRYPTO_BUFFER_pop_free(*names, CRYPTO_BUFFER_free); |
| 993 | *names = NULL; |
| 994 | } |
| 995 | return 0; |
| 996 | } |
| 997 | |
| 998 | return 1; |
| 999 | } |
| 1000 | |
| 1001 | int SSL_add_client_CA(SSL *ssl, X509 *x509) { |
| 1002 | if (!add_client_CA(&ssl->client_CA, x509, ssl->ctx->pool)) { |
| 1003 | return 0; |
| 1004 | } |
| 1005 | |
| 1006 | ssl_crypto_x509_ssl_flush_cached_client_CA(ssl); |
| 1007 | return 1; |
| 1008 | } |
| 1009 | |
| 1010 | int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x509) { |
| 1011 | if (!add_client_CA(&ctx->client_CA, x509, ctx->pool)) { |
| 1012 | return 0; |
| 1013 | } |
| 1014 | |
| 1015 | ssl_crypto_x509_ssl_ctx_flush_cached_client_CA(ctx); |
| 1016 | return 1; |
| 1017 | } |