Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [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 | #include <stdio.h> |
| 116 | |
| 117 | #include <openssl/bio.h> |
| 118 | #include <openssl/bn.h> |
David Benjamin | 676d1e7 | 2014-07-08 14:34:10 -0400 | [diff] [blame] | 119 | #include <openssl/buf.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 120 | #include <openssl/dh.h> |
| 121 | #include <openssl/err.h> |
| 122 | #include <openssl/mem.h> |
| 123 | #include <openssl/obj.h> |
| 124 | #include <openssl/pem.h> |
| 125 | #include <openssl/x509v3.h> |
| 126 | |
| 127 | #include "../crypto/dh/internal.h" |
| 128 | #include "../crypto/directory.h" |
| 129 | #include "ssl_locl.h" |
| 130 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 131 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 132 | int SSL_get_ex_data_X509_STORE_CTX_idx(void) { |
| 133 | static int ssl_x509_store_ctx_idx = -1; |
| 134 | int got_write_lock = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 135 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 136 | CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 137 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 138 | if (ssl_x509_store_ctx_idx < 0) { |
| 139 | CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX); |
| 140 | CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX); |
| 141 | got_write_lock = 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 142 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 143 | if (ssl_x509_store_ctx_idx < 0) { |
| 144 | ssl_x509_store_ctx_idx = X509_STORE_CTX_get_ex_new_index( |
| 145 | 0, "SSL for verify callback", NULL, NULL, NULL); |
| 146 | } |
| 147 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 148 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 149 | if (got_write_lock) { |
| 150 | CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX); |
| 151 | } else { |
| 152 | CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX); |
| 153 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 154 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 155 | return ssl_x509_store_ctx_idx; |
| 156 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 157 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 158 | CERT *ssl_cert_new(void) { |
| 159 | CERT *ret; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 160 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 161 | ret = (CERT *)OPENSSL_malloc(sizeof(CERT)); |
| 162 | if (ret == NULL) { |
| 163 | OPENSSL_PUT_ERROR(SSL, ssl_cert_new, ERR_R_MALLOC_FAILURE); |
| 164 | return NULL; |
| 165 | } |
| 166 | memset(ret, 0, sizeof(CERT)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 167 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 168 | ret->key = &ret->pkeys[SSL_PKEY_RSA_ENC]; |
| 169 | return ret; |
| 170 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 171 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 172 | CERT *ssl_cert_dup(CERT *cert) { |
| 173 | CERT *ret; |
| 174 | int i; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 175 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 176 | ret = (CERT *)OPENSSL_malloc(sizeof(CERT)); |
| 177 | if (ret == NULL) { |
| 178 | OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_MALLOC_FAILURE); |
| 179 | return NULL; |
| 180 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 181 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 182 | memset(ret, 0, sizeof(CERT)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 183 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 184 | ret->key = &ret->pkeys[cert->key - &cert->pkeys[0]]; |
| 185 | /* or ret->key = ret->pkeys + (cert->key - cert->pkeys), if you find that |
| 186 | * more readable */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 187 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 188 | ret->mask_k = cert->mask_k; |
| 189 | ret->mask_a = cert->mask_a; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 190 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 191 | if (cert->dh_tmp != NULL) { |
| 192 | ret->dh_tmp = DHparams_dup(cert->dh_tmp); |
| 193 | if (ret->dh_tmp == NULL) { |
| 194 | OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_DH_LIB); |
| 195 | goto err; |
| 196 | } |
| 197 | if (cert->dh_tmp->priv_key) { |
| 198 | BIGNUM *b = BN_dup(cert->dh_tmp->priv_key); |
| 199 | if (!b) { |
| 200 | OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_BN_LIB); |
| 201 | goto err; |
| 202 | } |
| 203 | ret->dh_tmp->priv_key = b; |
| 204 | } |
| 205 | if (cert->dh_tmp->pub_key) { |
| 206 | BIGNUM *b = BN_dup(cert->dh_tmp->pub_key); |
| 207 | if (!b) { |
| 208 | OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_BN_LIB); |
| 209 | goto err; |
| 210 | } |
| 211 | ret->dh_tmp->pub_key = b; |
| 212 | } |
| 213 | } |
| 214 | ret->dh_tmp_cb = cert->dh_tmp_cb; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 215 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 216 | if (cert->ecdh_tmp) { |
| 217 | ret->ecdh_tmp = EC_KEY_dup(cert->ecdh_tmp); |
| 218 | if (ret->ecdh_tmp == NULL) { |
| 219 | OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_EC_LIB); |
| 220 | goto err; |
| 221 | } |
| 222 | } |
| 223 | ret->ecdh_tmp_cb = cert->ecdh_tmp_cb; |
| 224 | ret->ecdh_tmp_auto = cert->ecdh_tmp_auto; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 225 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 226 | for (i = 0; i < SSL_PKEY_NUM; i++) { |
| 227 | CERT_PKEY *cpk = cert->pkeys + i; |
| 228 | CERT_PKEY *rpk = ret->pkeys + i; |
| 229 | if (cpk->x509 != NULL) { |
| 230 | rpk->x509 = X509_up_ref(cpk->x509); |
| 231 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 232 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 233 | if (cpk->privatekey != NULL) { |
| 234 | rpk->privatekey = EVP_PKEY_dup(cpk->privatekey); |
| 235 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 236 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 237 | if (cpk->chain) { |
| 238 | rpk->chain = X509_chain_up_ref(cpk->chain); |
| 239 | if (!rpk->chain) { |
| 240 | OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_MALLOC_FAILURE); |
| 241 | goto err; |
| 242 | } |
| 243 | } |
| 244 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 245 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 246 | /* Peer sigalgs set to NULL as we get these from handshake too */ |
| 247 | ret->peer_sigalgs = NULL; |
| 248 | ret->peer_sigalgslen = 0; |
| 249 | /* Configured sigalgs however we copy across */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 250 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 251 | if (cert->conf_sigalgs) { |
| 252 | ret->conf_sigalgs = OPENSSL_malloc(cert->conf_sigalgslen); |
| 253 | if (!ret->conf_sigalgs) { |
| 254 | goto err; |
| 255 | } |
| 256 | memcpy(ret->conf_sigalgs, cert->conf_sigalgs, cert->conf_sigalgslen); |
| 257 | ret->conf_sigalgslen = cert->conf_sigalgslen; |
| 258 | } else { |
| 259 | ret->conf_sigalgs = NULL; |
| 260 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 261 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 262 | if (cert->client_sigalgs) { |
| 263 | ret->client_sigalgs = OPENSSL_malloc(cert->client_sigalgslen); |
| 264 | if (!ret->client_sigalgs) { |
| 265 | goto err; |
| 266 | } |
| 267 | memcpy(ret->client_sigalgs, cert->client_sigalgs, cert->client_sigalgslen); |
| 268 | ret->client_sigalgslen = cert->client_sigalgslen; |
| 269 | } else { |
| 270 | ret->client_sigalgs = NULL; |
| 271 | } |
| 272 | /* Shared sigalgs also NULL */ |
| 273 | ret->shared_sigalgs = NULL; |
| 274 | /* Copy any custom client certificate types */ |
| 275 | if (cert->client_certificate_types) { |
| 276 | ret->client_certificate_types = BUF_memdup( |
| 277 | cert->client_certificate_types, cert->num_client_certificate_types); |
| 278 | if (!ret->client_certificate_types) { |
| 279 | goto err; |
| 280 | } |
| 281 | ret->num_client_certificate_types = cert->num_client_certificate_types; |
| 282 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 283 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 284 | ret->cert_flags = cert->cert_flags; |
| 285 | |
| 286 | ret->cert_cb = cert->cert_cb; |
| 287 | ret->cert_cb_arg = cert->cert_cb_arg; |
| 288 | |
| 289 | if (cert->verify_store) { |
| 290 | CRYPTO_add(&cert->verify_store->references, 1, CRYPTO_LOCK_X509_STORE); |
| 291 | ret->verify_store = cert->verify_store; |
| 292 | } |
| 293 | |
| 294 | if (cert->chain_store) { |
| 295 | CRYPTO_add(&cert->chain_store->references, 1, CRYPTO_LOCK_X509_STORE); |
| 296 | ret->chain_store = cert->chain_store; |
| 297 | } |
| 298 | |
| 299 | ret->ciphers_raw = NULL; |
| 300 | |
| 301 | return ret; |
| 302 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 303 | err: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 304 | ssl_cert_free(ret); |
| 305 | return NULL; |
| 306 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 307 | |
| 308 | /* Free up and clear all certificates and chains */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 309 | void ssl_cert_clear_certs(CERT *c) { |
| 310 | int i; |
| 311 | if (c == NULL) { |
| 312 | return; |
| 313 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 314 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 315 | for (i = 0; i < SSL_PKEY_NUM; i++) { |
| 316 | CERT_PKEY *cpk = c->pkeys + i; |
| 317 | if (cpk->x509) { |
| 318 | X509_free(cpk->x509); |
| 319 | cpk->x509 = NULL; |
| 320 | } |
| 321 | if (cpk->privatekey) { |
| 322 | EVP_PKEY_free(cpk->privatekey); |
| 323 | cpk->privatekey = NULL; |
| 324 | } |
| 325 | if (cpk->chain) { |
| 326 | sk_X509_pop_free(cpk->chain, X509_free); |
| 327 | cpk->chain = NULL; |
| 328 | } |
| 329 | } |
| 330 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 331 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 332 | void ssl_cert_free(CERT *c) { |
| 333 | if (c == NULL) { |
| 334 | return; |
| 335 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 336 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 337 | if (c->dh_tmp) { |
| 338 | DH_free(c->dh_tmp); |
| 339 | } |
| 340 | if (c->ecdh_tmp) { |
| 341 | EC_KEY_free(c->ecdh_tmp); |
| 342 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 343 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 344 | ssl_cert_clear_certs(c); |
| 345 | if (c->peer_sigalgs) { |
| 346 | OPENSSL_free(c->peer_sigalgs); |
| 347 | } |
| 348 | if (c->conf_sigalgs) { |
| 349 | OPENSSL_free(c->conf_sigalgs); |
| 350 | } |
| 351 | if (c->client_sigalgs) { |
| 352 | OPENSSL_free(c->client_sigalgs); |
| 353 | } |
| 354 | if (c->shared_sigalgs) { |
| 355 | OPENSSL_free(c->shared_sigalgs); |
| 356 | } |
| 357 | if (c->client_certificate_types) { |
| 358 | OPENSSL_free(c->client_certificate_types); |
| 359 | } |
| 360 | if (c->verify_store) { |
| 361 | X509_STORE_free(c->verify_store); |
| 362 | } |
| 363 | if (c->chain_store) { |
| 364 | X509_STORE_free(c->chain_store); |
| 365 | } |
| 366 | if (c->ciphers_raw) { |
| 367 | OPENSSL_free(c->ciphers_raw); |
| 368 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 369 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 370 | OPENSSL_free(c); |
| 371 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 372 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 373 | int ssl_cert_inst(CERT **o) { |
| 374 | /* Create a CERT if there isn't already one (which cannot really happen, as |
| 375 | * it is initially created in SSL_CTX_new; but the earlier code usually |
| 376 | * allows for that one being non-existant, so we follow that behaviour, as it |
| 377 | * might turn out that there actually is a reason for it -- but I'm not sure |
| 378 | * that *all* of the existing code could cope with s->cert being NULL, |
| 379 | * otherwise we could do without the initialization in SSL_CTX_new). */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 380 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 381 | if (o == NULL) { |
| 382 | OPENSSL_PUT_ERROR(SSL, ssl_cert_inst, ERR_R_PASSED_NULL_PARAMETER); |
| 383 | return 0; |
| 384 | } |
| 385 | if (*o == NULL) { |
| 386 | *o = ssl_cert_new(); |
| 387 | if (*o == NULL) { |
| 388 | OPENSSL_PUT_ERROR(SSL, ssl_cert_new, ERR_R_MALLOC_FAILURE); |
| 389 | return 0; |
| 390 | } |
| 391 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 392 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 393 | return 1; |
| 394 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 395 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 396 | int ssl_cert_set0_chain(CERT *c, STACK_OF(X509) * chain) { |
| 397 | CERT_PKEY *cpk = c->key; |
| 398 | if (!cpk) { |
| 399 | return 0; |
| 400 | } |
| 401 | if (cpk->chain) { |
| 402 | sk_X509_pop_free(cpk->chain, X509_free); |
| 403 | } |
| 404 | cpk->chain = chain; |
| 405 | return 1; |
| 406 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 407 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 408 | int ssl_cert_set1_chain(CERT *c, STACK_OF(X509) * chain) { |
| 409 | STACK_OF(X509) * dchain; |
| 410 | if (!chain) { |
| 411 | return ssl_cert_set0_chain(c, NULL); |
| 412 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 413 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 414 | dchain = X509_chain_up_ref(chain); |
| 415 | if (!dchain) { |
| 416 | return 0; |
| 417 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 418 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 419 | if (!ssl_cert_set0_chain(c, dchain)) { |
| 420 | sk_X509_pop_free(dchain, X509_free); |
| 421 | return 0; |
| 422 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 423 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 424 | return 1; |
| 425 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 426 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 427 | int ssl_cert_add0_chain_cert(CERT *c, X509 *x) { |
| 428 | CERT_PKEY *cpk = c->key; |
| 429 | if (!cpk) { |
| 430 | return 0; |
| 431 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 432 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 433 | if (!cpk->chain) { |
| 434 | cpk->chain = sk_X509_new_null(); |
| 435 | } |
| 436 | if (!cpk->chain || !sk_X509_push(cpk->chain, x)) { |
| 437 | return 0; |
| 438 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 439 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 440 | return 1; |
| 441 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 442 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 443 | int ssl_cert_add1_chain_cert(CERT *c, X509 *x) { |
| 444 | if (!ssl_cert_add0_chain_cert(c, x)) { |
| 445 | return 0; |
| 446 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 447 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 448 | X509_up_ref(x); |
| 449 | return 1; |
| 450 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 451 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 452 | int ssl_cert_select_current(CERT *c, X509 *x) { |
| 453 | int i; |
| 454 | if (x == NULL) { |
| 455 | return 0; |
| 456 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 457 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 458 | for (i = 0; i < SSL_PKEY_NUM; i++) { |
| 459 | if (c->pkeys[i].x509 == x) { |
| 460 | c->key = &c->pkeys[i]; |
| 461 | return 1; |
| 462 | } |
| 463 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 464 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 465 | for (i = 0; i < SSL_PKEY_NUM; i++) { |
| 466 | if (c->pkeys[i].x509 && !X509_cmp(c->pkeys[i].x509, x)) { |
| 467 | c->key = &c->pkeys[i]; |
| 468 | return 1; |
| 469 | } |
| 470 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 471 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 472 | return 0; |
| 473 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 474 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 475 | void ssl_cert_set_cert_cb(CERT *c, int (*cb)(SSL *ssl, void *arg), void *arg) { |
| 476 | c->cert_cb = cb; |
| 477 | c->cert_cb_arg = arg; |
| 478 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 479 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 480 | SESS_CERT *ssl_sess_cert_new(void) { |
| 481 | SESS_CERT *ret; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 482 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 483 | ret = OPENSSL_malloc(sizeof *ret); |
| 484 | if (ret == NULL) { |
| 485 | OPENSSL_PUT_ERROR(SSL, ssl_sess_cert_new, ERR_R_MALLOC_FAILURE); |
| 486 | return NULL; |
| 487 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 488 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 489 | memset(ret, 0, sizeof *ret); |
| 490 | ret->peer_key = &(ret->peer_pkeys[SSL_PKEY_RSA_ENC]); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 491 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 492 | return ret; |
| 493 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 494 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 495 | void ssl_sess_cert_free(SESS_CERT *sc) { |
| 496 | int i; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 497 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 498 | if (sc == NULL) { |
| 499 | return; |
| 500 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 501 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 502 | if (sc->cert_chain != NULL) { |
| 503 | sk_X509_pop_free(sc->cert_chain, X509_free); |
| 504 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 505 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 506 | for (i = 0; i < SSL_PKEY_NUM; i++) { |
| 507 | if (sc->peer_pkeys[i].x509 != NULL) { |
| 508 | X509_free(sc->peer_pkeys[i].x509); |
| 509 | } |
| 510 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 511 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 512 | if (sc->peer_dh_tmp != NULL) { |
| 513 | DH_free(sc->peer_dh_tmp); |
| 514 | } |
| 515 | if (sc->peer_ecdh_tmp != NULL) { |
| 516 | EC_KEY_free(sc->peer_ecdh_tmp); |
| 517 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 518 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 519 | OPENSSL_free(sc); |
| 520 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 521 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 522 | int ssl_set_peer_cert_type(SESS_CERT *sc, int type) { |
| 523 | sc->peer_cert_type = type; |
| 524 | return 1; |
| 525 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 526 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 527 | int ssl_verify_cert_chain(SSL *s, STACK_OF(X509) * sk) { |
| 528 | X509 *x; |
| 529 | int i; |
| 530 | X509_STORE *verify_store; |
| 531 | X509_STORE_CTX ctx; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 532 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 533 | if (s->cert->verify_store) { |
| 534 | verify_store = s->cert->verify_store; |
| 535 | } else { |
| 536 | verify_store = s->ctx->cert_store; |
| 537 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 538 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 539 | if (sk == NULL || sk_X509_num(sk) == 0) { |
| 540 | return 0; |
| 541 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 542 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 543 | x = sk_X509_value(sk, 0); |
| 544 | if (!X509_STORE_CTX_init(&ctx, verify_store, x, sk)) { |
| 545 | OPENSSL_PUT_ERROR(SSL, ssl_verify_cert_chain, ERR_R_X509_LIB); |
| 546 | return 0; |
| 547 | } |
| 548 | X509_STORE_CTX_set_ex_data(&ctx, SSL_get_ex_data_X509_STORE_CTX_idx(), s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 549 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 550 | /* We need to inherit the verify parameters. These can be determined by the |
| 551 | * context: if its a server it will verify SSL client certificates or vice |
| 552 | * versa. */ |
| 553 | X509_STORE_CTX_set_default(&ctx, s->server ? "ssl_client" : "ssl_server"); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 554 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 555 | /* Anything non-default in "param" should overwrite anything in the ctx. */ |
| 556 | X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(&ctx), s->param); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 557 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 558 | if (s->verify_callback) { |
| 559 | X509_STORE_CTX_set_verify_cb(&ctx, s->verify_callback); |
| 560 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 561 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 562 | if (s->ctx->app_verify_callback != NULL) { |
| 563 | i = s->ctx->app_verify_callback(&ctx, s->ctx->app_verify_arg); |
| 564 | } else { |
| 565 | i = X509_verify_cert(&ctx); |
| 566 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 567 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 568 | s->verify_result = ctx.error; |
| 569 | X509_STORE_CTX_cleanup(&ctx); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 570 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 571 | return i; |
| 572 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 573 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 574 | static void set_client_CA_list(STACK_OF(X509_NAME) * *ca_list, |
| 575 | STACK_OF(X509_NAME) * name_list) { |
| 576 | if (*ca_list != NULL) { |
| 577 | sk_X509_NAME_pop_free(*ca_list, X509_NAME_free); |
| 578 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 579 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 580 | *ca_list = name_list; |
| 581 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 582 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 583 | STACK_OF(X509_NAME) * SSL_dup_CA_list(STACK_OF(X509_NAME) * sk) { |
| 584 | size_t i; |
| 585 | STACK_OF(X509_NAME) * ret; |
| 586 | X509_NAME *name; |
| 587 | |
| 588 | ret = sk_X509_NAME_new_null(); |
| 589 | for (i = 0; i < sk_X509_NAME_num(sk); i++) { |
| 590 | name = X509_NAME_dup(sk_X509_NAME_value(sk, i)); |
| 591 | if (name == NULL || !sk_X509_NAME_push(ret, name)) { |
| 592 | sk_X509_NAME_pop_free(ret, X509_NAME_free); |
| 593 | return NULL; |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | return ret; |
| 598 | } |
| 599 | |
| 600 | void SSL_set_client_CA_list(SSL *s, STACK_OF(X509_NAME) * name_list) { |
| 601 | set_client_CA_list(&(s->client_CA), name_list); |
| 602 | } |
| 603 | |
| 604 | void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) * name_list) { |
| 605 | set_client_CA_list(&(ctx->client_CA), name_list); |
| 606 | } |
| 607 | |
| 608 | STACK_OF(X509_NAME) * SSL_CTX_get_client_CA_list(const SSL_CTX *ctx) { |
| 609 | return ctx->client_CA; |
| 610 | } |
| 611 | |
| 612 | STACK_OF(X509_NAME) * SSL_get_client_CA_list(const SSL *s) { |
| 613 | if (s->server) { |
| 614 | if (s->client_CA != NULL) { |
| 615 | return s->client_CA; |
| 616 | } else { |
| 617 | return s->ctx->client_CA; |
| 618 | } |
| 619 | } else { |
| 620 | if ((s->version >> 8) == SSL3_VERSION_MAJOR && s->s3 != NULL) { |
| 621 | return s->s3->tmp.ca_names; |
| 622 | } else { |
| 623 | return NULL; |
| 624 | } |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | static int add_client_CA(STACK_OF(X509_NAME) * *sk, X509 *x) { |
| 629 | X509_NAME *name; |
| 630 | |
| 631 | if (x == NULL) { |
| 632 | return 0; |
| 633 | } |
| 634 | if (*sk == NULL) { |
| 635 | *sk = sk_X509_NAME_new_null(); |
| 636 | if (*sk == NULL) { |
| 637 | return 0; |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | name = X509_NAME_dup(X509_get_subject_name(x)); |
| 642 | if (name == NULL) { |
| 643 | return 0; |
| 644 | } |
| 645 | |
| 646 | if (!sk_X509_NAME_push(*sk, name)) { |
| 647 | X509_NAME_free(name); |
| 648 | return 0; |
| 649 | } |
| 650 | |
| 651 | return 1; |
| 652 | } |
| 653 | |
| 654 | int SSL_add_client_CA(SSL *ssl, X509 *x) { |
| 655 | return add_client_CA(&(ssl->client_CA), x); |
| 656 | } |
| 657 | |
| 658 | int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x) { |
| 659 | return add_client_CA(&(ctx->client_CA), x); |
| 660 | } |
| 661 | |
| 662 | static int xname_cmp(const X509_NAME **a, const X509_NAME **b) { |
| 663 | return X509_NAME_cmp(*a, *b); |
| 664 | } |
| 665 | |
| 666 | /* Load CA certs from a file into a STACK. Note that it is somewhat misnamed; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 667 | * it doesn't really have anything to do with clients (except that a common use |
| 668 | * for a stack of CAs is to send it to the client). Actually, it doesn't have |
| 669 | * much to do with CAs, either, since it will load any old cert. |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 670 | * |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 671 | * \param file the file containing one or more certs. |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 672 | * \return a ::STACK containing the certs. */ |
| 673 | STACK_OF(X509_NAME) * SSL_load_client_CA_file(const char *file) { |
| 674 | BIO *in; |
| 675 | X509 *x = NULL; |
| 676 | X509_NAME *xn = NULL; |
| 677 | STACK_OF(X509_NAME) *ret = NULL, *sk; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 678 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 679 | sk = sk_X509_NAME_new(xname_cmp); |
| 680 | in = BIO_new(BIO_s_file()); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 681 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 682 | if (sk == NULL || in == NULL) { |
| 683 | OPENSSL_PUT_ERROR(SSL, SSL_load_client_CA_file, ERR_R_MALLOC_FAILURE); |
| 684 | goto err; |
| 685 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 686 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 687 | if (!BIO_read_filename(in, file)) { |
| 688 | goto err; |
| 689 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 690 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 691 | for (;;) { |
| 692 | if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL) { |
| 693 | break; |
| 694 | } |
| 695 | if (ret == NULL) { |
| 696 | ret = sk_X509_NAME_new_null(); |
| 697 | if (ret == NULL) { |
| 698 | OPENSSL_PUT_ERROR(SSL, SSL_load_client_CA_file, ERR_R_MALLOC_FAILURE); |
| 699 | goto err; |
| 700 | } |
| 701 | } |
| 702 | xn = X509_get_subject_name(x); |
| 703 | if (xn == NULL) { |
| 704 | goto err; |
| 705 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 706 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 707 | /* check for duplicates */ |
| 708 | xn = X509_NAME_dup(xn); |
| 709 | if (xn == NULL) { |
| 710 | goto err; |
| 711 | } |
| 712 | if (sk_X509_NAME_find(sk, NULL, xn)) { |
| 713 | X509_NAME_free(xn); |
| 714 | } else { |
| 715 | sk_X509_NAME_push(sk, xn); |
| 716 | sk_X509_NAME_push(ret, xn); |
| 717 | } |
| 718 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 719 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 720 | if (0) { |
| 721 | err: |
| 722 | if (ret != NULL) { |
| 723 | sk_X509_NAME_pop_free(ret, X509_NAME_free); |
| 724 | } |
| 725 | ret = NULL; |
| 726 | } |
| 727 | |
| 728 | if (sk != NULL) { |
| 729 | sk_X509_NAME_free(sk); |
| 730 | } |
| 731 | if (in != NULL) { |
| 732 | BIO_free(in); |
| 733 | } |
| 734 | if (x != NULL) { |
| 735 | X509_free(x); |
| 736 | } |
| 737 | if (ret != NULL) { |
| 738 | ERR_clear_error(); |
| 739 | } |
| 740 | return ret; |
| 741 | } |
| 742 | |
| 743 | /* Add a file of certs to a stack. |
| 744 | * |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 745 | * \param stack the stack to add to. |
| 746 | * \param file the file to add from. All certs in this file that are not |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 747 | * already in the stack will be added. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 748 | * \return 1 for success, 0 for failure. Note that in the case of failure some |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 749 | * certs may have been added to \c stack. */ |
| 750 | int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) * stack, |
| 751 | const char *file) { |
| 752 | BIO *in; |
| 753 | X509 *x = NULL; |
| 754 | X509_NAME *xn = NULL; |
| 755 | int ret = 1; |
| 756 | int (*oldcmp)(const X509_NAME **a, const X509_NAME **b); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 757 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 758 | oldcmp = sk_X509_NAME_set_cmp_func(stack, xname_cmp); |
| 759 | in = BIO_new(BIO_s_file()); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 760 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 761 | if (in == NULL) { |
| 762 | OPENSSL_PUT_ERROR(SSL, SSL_add_file_cert_subjects_to_stack, |
| 763 | ERR_R_MALLOC_FAILURE); |
| 764 | goto err; |
| 765 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 766 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 767 | if (!BIO_read_filename(in, file)) { |
| 768 | goto err; |
| 769 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 770 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 771 | for (;;) { |
| 772 | if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL) { |
| 773 | break; |
| 774 | } |
| 775 | xn = X509_get_subject_name(x); |
| 776 | if (xn == NULL) { |
| 777 | goto err; |
| 778 | } |
| 779 | xn = X509_NAME_dup(xn); |
| 780 | if (xn == NULL) { |
| 781 | goto err; |
| 782 | } |
| 783 | if (sk_X509_NAME_find(stack, NULL, xn)) { |
| 784 | X509_NAME_free(xn); |
| 785 | } else { |
| 786 | sk_X509_NAME_push(stack, xn); |
| 787 | } |
| 788 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 789 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 790 | ERR_clear_error(); |
| 791 | |
| 792 | if (0) { |
| 793 | err: |
| 794 | ret = 0; |
| 795 | } |
| 796 | |
| 797 | if (in != NULL) { |
| 798 | BIO_free(in); |
| 799 | } |
| 800 | if (x != NULL) { |
| 801 | X509_free(x); |
| 802 | } |
| 803 | |
Adam Langley | a307dfd | 2015-01-09 15:42:58 -0800 | [diff] [blame] | 804 | (void) sk_X509_NAME_set_cmp_func(stack, oldcmp); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 805 | |
| 806 | return ret; |
| 807 | } |
| 808 | |
| 809 | /* Add a directory of certs to a stack. |
| 810 | * |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 811 | * \param stack the stack to append to. |
| 812 | * \param dir the directory to append from. All files in this directory will be |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 813 | * examined as potential certs. Any that are acceptable to |
| 814 | * SSL_add_dir_cert_subjects_to_stack() that are not already in the stack will |
| 815 | * be included. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 816 | * \return 1 for success, 0 for failure. Note that in the case of failure some |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 817 | * certs may have been added to \c stack. */ |
| 818 | int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) * stack, |
| 819 | const char *dir) { |
| 820 | OPENSSL_DIR_CTX *d = NULL; |
| 821 | const char *filename; |
| 822 | int ret = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 823 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 824 | CRYPTO_w_lock(CRYPTO_LOCK_READDIR); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 825 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 826 | /* Note that a side effect is that the CAs will be sorted by name */ |
| 827 | while ((filename = OPENSSL_DIR_read(&d, dir))) { |
| 828 | char buf[1024]; |
| 829 | int r; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 830 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 831 | if (strlen(dir) + strlen(filename) + 2 > sizeof(buf)) { |
| 832 | OPENSSL_PUT_ERROR(SSL, SSL_add_dir_cert_subjects_to_stack, |
| 833 | SSL_R_PATH_TOO_LONG); |
| 834 | goto err; |
| 835 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 836 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 837 | r = BIO_snprintf(buf, sizeof buf, "%s/%s", dir, filename); |
| 838 | if (r <= 0 || r >= (int)sizeof(buf) || |
| 839 | !SSL_add_file_cert_subjects_to_stack(stack, buf)) { |
| 840 | goto err; |
| 841 | } |
| 842 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 843 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 844 | if (errno) { |
| 845 | OPENSSL_PUT_ERROR(SSL, SSL_add_file_cert_subjects_to_stack, ERR_R_SYS_LIB); |
| 846 | ERR_add_error_data(3, "OPENSSL_DIR_read(&ctx, '", dir, "')"); |
| 847 | goto err; |
| 848 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 849 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 850 | ret = 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 851 | |
| 852 | err: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 853 | if (d) { |
| 854 | OPENSSL_DIR_end(&d); |
| 855 | } |
| 856 | CRYPTO_w_unlock(CRYPTO_LOCK_READDIR); |
| 857 | return ret; |
| 858 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 859 | |
| 860 | /* Add a certificate to a BUF_MEM structure */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 861 | static int ssl_add_cert_to_buf(BUF_MEM *buf, unsigned long *l, X509 *x) { |
| 862 | int n; |
| 863 | uint8_t *p; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 864 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 865 | n = i2d_X509(x, NULL); |
| 866 | if (!BUF_MEM_grow_clean(buf, (int)(n + (*l) + 3))) { |
| 867 | OPENSSL_PUT_ERROR(SSL, ssl_add_cert_to_buf, ERR_R_BUF_LIB); |
| 868 | return 0; |
| 869 | } |
| 870 | p = (uint8_t *)&(buf->data[*l]); |
| 871 | l2n3(n, p); |
| 872 | i2d_X509(x, &p); |
| 873 | *l += n + 3; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 874 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 875 | return 1; |
| 876 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 877 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 878 | /* Add certificate chain to internal SSL BUF_MEM structure. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 879 | int ssl_add_cert_chain(SSL *s, CERT_PKEY *cpk, unsigned long *l) { |
| 880 | BUF_MEM *buf = s->init_buf; |
| 881 | int no_chain = 0; |
| 882 | size_t i; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 883 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 884 | X509 *x = NULL; |
| 885 | STACK_OF(X509) * extra_certs; |
| 886 | X509_STORE *chain_store; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 887 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 888 | if (cpk) { |
| 889 | x = cpk->x509; |
| 890 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 891 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 892 | if (s->cert->chain_store) { |
| 893 | chain_store = s->cert->chain_store; |
| 894 | } else { |
| 895 | chain_store = s->ctx->cert_store; |
| 896 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 897 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 898 | /* If we have a certificate specific chain use it, else use parent ctx. */ |
| 899 | if (cpk && cpk->chain) { |
| 900 | extra_certs = cpk->chain; |
| 901 | } else { |
| 902 | extra_certs = s->ctx->extra_certs; |
| 903 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 904 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 905 | if ((s->mode & SSL_MODE_NO_AUTO_CHAIN) || extra_certs) { |
| 906 | no_chain = 1; |
| 907 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 908 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 909 | /* TLSv1 sends a chain with nothing in it, instead of an alert. */ |
| 910 | if (!BUF_MEM_grow_clean(buf, 10)) { |
| 911 | OPENSSL_PUT_ERROR(SSL, ssl_add_cert_chain, ERR_R_BUF_LIB); |
| 912 | return 0; |
| 913 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 914 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 915 | if (x != NULL) { |
| 916 | if (no_chain) { |
| 917 | if (!ssl_add_cert_to_buf(buf, l, x)) { |
| 918 | return 0; |
| 919 | } |
| 920 | } else { |
| 921 | X509_STORE_CTX xs_ctx; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 922 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 923 | if (!X509_STORE_CTX_init(&xs_ctx, chain_store, x, NULL)) { |
| 924 | OPENSSL_PUT_ERROR(SSL, ssl_add_cert_chain, ERR_R_X509_LIB); |
| 925 | return 0; |
| 926 | } |
| 927 | X509_verify_cert(&xs_ctx); |
| 928 | /* Don't leave errors in the queue */ |
| 929 | ERR_clear_error(); |
| 930 | for (i = 0; i < sk_X509_num(xs_ctx.chain); i++) { |
| 931 | x = sk_X509_value(xs_ctx.chain, i); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 932 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 933 | if (!ssl_add_cert_to_buf(buf, l, x)) { |
| 934 | X509_STORE_CTX_cleanup(&xs_ctx); |
| 935 | return 0; |
| 936 | } |
| 937 | } |
| 938 | X509_STORE_CTX_cleanup(&xs_ctx); |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | for (i = 0; i < sk_X509_num(extra_certs); i++) { |
| 943 | x = sk_X509_value(extra_certs, i); |
| 944 | if (!ssl_add_cert_to_buf(buf, l, x)) { |
| 945 | return 0; |
| 946 | } |
| 947 | } |
| 948 | |
| 949 | return 1; |
| 950 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 951 | |
| 952 | /* Build a certificate chain for current certificate */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 953 | int ssl_build_cert_chain(CERT *c, X509_STORE *chain_store, int flags) { |
| 954 | CERT_PKEY *cpk = c->key; |
| 955 | X509_STORE_CTX xs_ctx; |
| 956 | STACK_OF(X509) *chain = NULL, *untrusted = NULL; |
| 957 | X509 *x; |
| 958 | int i, rv = 0; |
| 959 | unsigned long error; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 960 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 961 | if (!cpk->x509) { |
| 962 | OPENSSL_PUT_ERROR(SSL, ssl_build_cert_chain, SSL_R_NO_CERTIFICATE_SET); |
| 963 | goto err; |
| 964 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 965 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 966 | /* Rearranging and check the chain: add everything to a store */ |
| 967 | if (flags & SSL_BUILD_CHAIN_FLAG_CHECK) { |
| 968 | size_t j; |
| 969 | chain_store = X509_STORE_new(); |
| 970 | if (!chain_store) { |
| 971 | goto err; |
| 972 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 973 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 974 | for (j = 0; j < sk_X509_num(cpk->chain); j++) { |
| 975 | x = sk_X509_value(cpk->chain, j); |
| 976 | if (!X509_STORE_add_cert(chain_store, x)) { |
| 977 | error = ERR_peek_last_error(); |
| 978 | if (ERR_GET_LIB(error) != ERR_LIB_X509 || |
| 979 | ERR_GET_REASON(error) != X509_R_CERT_ALREADY_IN_HASH_TABLE) { |
| 980 | goto err; |
| 981 | } |
| 982 | ERR_clear_error(); |
| 983 | } |
| 984 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 985 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 986 | /* Add EE cert too: it might be self signed */ |
| 987 | if (!X509_STORE_add_cert(chain_store, cpk->x509)) { |
| 988 | error = ERR_peek_last_error(); |
| 989 | if (ERR_GET_LIB(error) != ERR_LIB_X509 || |
| 990 | ERR_GET_REASON(error) != X509_R_CERT_ALREADY_IN_HASH_TABLE) { |
| 991 | goto err; |
| 992 | } |
| 993 | ERR_clear_error(); |
| 994 | } |
| 995 | } else { |
| 996 | if (c->chain_store) { |
| 997 | chain_store = c->chain_store; |
| 998 | } |
Adam Langley | f669c2d | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 999 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1000 | if (flags & SSL_BUILD_CHAIN_FLAG_UNTRUSTED) { |
| 1001 | untrusted = cpk->chain; |
| 1002 | } |
| 1003 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1004 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1005 | if (!X509_STORE_CTX_init(&xs_ctx, chain_store, cpk->x509, untrusted)) { |
| 1006 | OPENSSL_PUT_ERROR(SSL, ssl_build_cert_chain, ERR_R_X509_LIB); |
| 1007 | goto err; |
| 1008 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1009 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1010 | i = X509_verify_cert(&xs_ctx); |
| 1011 | if (i <= 0 && flags & SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR) { |
| 1012 | if (flags & SSL_BUILD_CHAIN_FLAG_CLEAR_ERROR) { |
| 1013 | ERR_clear_error(); |
| 1014 | } |
| 1015 | i = 1; |
| 1016 | rv = 2; |
| 1017 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1018 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1019 | if (i > 0) { |
| 1020 | chain = X509_STORE_CTX_get1_chain(&xs_ctx); |
| 1021 | } |
| 1022 | if (i <= 0) { |
| 1023 | OPENSSL_PUT_ERROR(SSL, ssl_build_cert_chain, |
| 1024 | SSL_R_CERTIFICATE_VERIFY_FAILED); |
| 1025 | i = X509_STORE_CTX_get_error(&xs_ctx); |
| 1026 | ERR_add_error_data(2, "Verify error:", X509_verify_cert_error_string(i)); |
| 1027 | |
| 1028 | X509_STORE_CTX_cleanup(&xs_ctx); |
| 1029 | goto err; |
| 1030 | } |
| 1031 | |
| 1032 | X509_STORE_CTX_cleanup(&xs_ctx); |
| 1033 | if (cpk->chain) { |
| 1034 | sk_X509_pop_free(cpk->chain, X509_free); |
| 1035 | } |
| 1036 | |
| 1037 | /* Remove EE certificate from chain */ |
| 1038 | x = sk_X509_shift(chain); |
| 1039 | X509_free(x); |
| 1040 | if (flags & SSL_BUILD_CHAIN_FLAG_NO_ROOT) { |
| 1041 | if (sk_X509_num(chain) > 0) { |
| 1042 | /* See if last cert is self signed */ |
| 1043 | x = sk_X509_value(chain, sk_X509_num(chain) - 1); |
| 1044 | X509_check_purpose(x, -1, 0); |
| 1045 | if (x->ex_flags & EXFLAG_SS) { |
| 1046 | x = sk_X509_pop(chain); |
| 1047 | X509_free(x); |
| 1048 | } |
| 1049 | } |
| 1050 | } |
| 1051 | |
| 1052 | cpk->chain = chain; |
David Benjamin | 6eb000d | 2015-02-11 01:17:41 -0500 | [diff] [blame] | 1053 | if (rv == 0) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1054 | rv = 1; |
David Benjamin | 6eb000d | 2015-02-11 01:17:41 -0500 | [diff] [blame] | 1055 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1056 | |
| 1057 | err: |
| 1058 | if (flags & SSL_BUILD_CHAIN_FLAG_CHECK) { |
| 1059 | X509_STORE_free(chain_store); |
| 1060 | } |
| 1061 | |
| 1062 | return rv; |
| 1063 | } |
| 1064 | |
| 1065 | int ssl_cert_set_cert_store(CERT *c, X509_STORE *store, int chain, int ref) { |
| 1066 | X509_STORE **pstore; |
| 1067 | if (chain) { |
| 1068 | pstore = &c->chain_store; |
| 1069 | } else { |
| 1070 | pstore = &c->verify_store; |
| 1071 | } |
| 1072 | |
| 1073 | if (*pstore) { |
| 1074 | X509_STORE_free(*pstore); |
| 1075 | } |
| 1076 | *pstore = store; |
| 1077 | |
| 1078 | if (ref && store) { |
| 1079 | CRYPTO_add(&store->references, 1, CRYPTO_LOCK_X509_STORE); |
| 1080 | } |
| 1081 | return 1; |
| 1082 | } |