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> |
| 119 | #include <openssl/dh.h> |
| 120 | #include <openssl/err.h> |
| 121 | #include <openssl/mem.h> |
| 122 | #include <openssl/obj.h> |
| 123 | #include <openssl/pem.h> |
| 124 | #include <openssl/x509v3.h> |
| 125 | |
| 126 | #include "../crypto/dh/internal.h" |
| 127 | #include "../crypto/directory.h" |
| 128 | #include "ssl_locl.h" |
| 129 | |
| 130 | int SSL_get_ex_data_X509_STORE_CTX_idx(void) |
| 131 | { |
| 132 | static volatile int ssl_x509_store_ctx_idx= -1; |
| 133 | int got_write_lock = 0; |
| 134 | |
| 135 | CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX); |
| 136 | |
| 137 | if (ssl_x509_store_ctx_idx < 0) |
| 138 | { |
| 139 | CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX); |
| 140 | CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX); |
| 141 | got_write_lock = 1; |
| 142 | |
| 143 | if (ssl_x509_store_ctx_idx < 0) |
| 144 | { |
| 145 | ssl_x509_store_ctx_idx=X509_STORE_CTX_get_ex_new_index( |
| 146 | 0,"SSL for verify callback",NULL,NULL,NULL); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | if (got_write_lock) |
| 151 | CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX); |
| 152 | else |
| 153 | CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX); |
| 154 | |
| 155 | return ssl_x509_store_ctx_idx; |
| 156 | } |
| 157 | |
| 158 | void ssl_cert_set_default_md(CERT *cert) |
| 159 | { |
| 160 | /* Set digest values to defaults */ |
| 161 | #ifndef OPENSSL_NO_DSA |
| 162 | cert->pkeys[SSL_PKEY_DSA_SIGN].digest = EVP_sha1(); |
| 163 | #endif |
| 164 | #ifndef OPENSSL_NO_RSA |
| 165 | cert->pkeys[SSL_PKEY_RSA_SIGN].digest = EVP_sha1(); |
| 166 | cert->pkeys[SSL_PKEY_RSA_ENC].digest = EVP_sha1(); |
| 167 | #endif |
| 168 | #ifndef OPENSSL_NO_ECDSA |
| 169 | cert->pkeys[SSL_PKEY_ECC].digest = EVP_sha1(); |
| 170 | #endif |
| 171 | } |
| 172 | |
| 173 | CERT *ssl_cert_new(void) |
| 174 | { |
| 175 | CERT *ret; |
| 176 | |
| 177 | ret=(CERT *)OPENSSL_malloc(sizeof(CERT)); |
| 178 | if (ret == NULL) |
| 179 | { |
| 180 | OPENSSL_PUT_ERROR(SSL, ssl_cert_new, ERR_R_MALLOC_FAILURE); |
| 181 | return(NULL); |
| 182 | } |
| 183 | memset(ret,0,sizeof(CERT)); |
| 184 | |
| 185 | ret->key= &(ret->pkeys[SSL_PKEY_RSA_ENC]); |
| 186 | ret->references=1; |
| 187 | ssl_cert_set_default_md(ret); |
| 188 | return(ret); |
| 189 | } |
| 190 | |
| 191 | CERT *ssl_cert_dup(CERT *cert) |
| 192 | { |
| 193 | CERT *ret; |
| 194 | int i; |
| 195 | |
| 196 | ret = (CERT *)OPENSSL_malloc(sizeof(CERT)); |
| 197 | if (ret == NULL) |
| 198 | { |
| 199 | OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_MALLOC_FAILURE); |
| 200 | return(NULL); |
| 201 | } |
| 202 | |
| 203 | memset(ret, 0, sizeof(CERT)); |
| 204 | |
| 205 | ret->key = &ret->pkeys[cert->key - &cert->pkeys[0]]; |
| 206 | /* or ret->key = ret->pkeys + (cert->key - cert->pkeys), |
| 207 | * if you find that more readable */ |
| 208 | |
| 209 | ret->valid = cert->valid; |
| 210 | ret->mask_k = cert->mask_k; |
| 211 | ret->mask_a = cert->mask_a; |
| 212 | ret->export_mask_k = cert->export_mask_k; |
| 213 | ret->export_mask_a = cert->export_mask_a; |
| 214 | |
| 215 | #ifndef OPENSSL_NO_RSA |
| 216 | if (cert->rsa_tmp != NULL) |
| 217 | { |
| 218 | RSA_up_ref(cert->rsa_tmp); |
| 219 | ret->rsa_tmp = cert->rsa_tmp; |
| 220 | } |
| 221 | ret->rsa_tmp_cb = cert->rsa_tmp_cb; |
| 222 | #endif |
| 223 | |
| 224 | #ifndef OPENSSL_NO_DH |
| 225 | if (cert->dh_tmp != NULL) |
| 226 | { |
| 227 | ret->dh_tmp = DHparams_dup(cert->dh_tmp); |
| 228 | if (ret->dh_tmp == NULL) |
| 229 | { |
| 230 | OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_DH_LIB); |
| 231 | goto err; |
| 232 | } |
| 233 | if (cert->dh_tmp->priv_key) |
| 234 | { |
| 235 | BIGNUM *b = BN_dup(cert->dh_tmp->priv_key); |
| 236 | if (!b) |
| 237 | { |
| 238 | OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_BN_LIB); |
| 239 | goto err; |
| 240 | } |
| 241 | ret->dh_tmp->priv_key = b; |
| 242 | } |
| 243 | if (cert->dh_tmp->pub_key) |
| 244 | { |
| 245 | BIGNUM *b = BN_dup(cert->dh_tmp->pub_key); |
| 246 | if (!b) |
| 247 | { |
| 248 | OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_BN_LIB); |
| 249 | goto err; |
| 250 | } |
| 251 | ret->dh_tmp->pub_key = b; |
| 252 | } |
| 253 | } |
| 254 | ret->dh_tmp_cb = cert->dh_tmp_cb; |
| 255 | #endif |
| 256 | |
| 257 | #ifndef OPENSSL_NO_ECDH |
| 258 | if (cert->ecdh_tmp) |
| 259 | { |
| 260 | ret->ecdh_tmp = EC_KEY_dup(cert->ecdh_tmp); |
| 261 | if (ret->ecdh_tmp == NULL) |
| 262 | { |
| 263 | OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_EC_LIB); |
| 264 | goto err; |
| 265 | } |
| 266 | } |
| 267 | ret->ecdh_tmp_cb = cert->ecdh_tmp_cb; |
| 268 | ret->ecdh_tmp_auto = cert->ecdh_tmp_auto; |
| 269 | #endif |
| 270 | |
| 271 | for (i = 0; i < SSL_PKEY_NUM; i++) |
| 272 | { |
| 273 | CERT_PKEY *cpk = cert->pkeys + i; |
| 274 | CERT_PKEY *rpk = ret->pkeys + i; |
| 275 | if (cpk->x509 != NULL) |
| 276 | { |
| 277 | rpk->x509 = cpk->x509; |
| 278 | CRYPTO_add(&rpk->x509->references, 1, CRYPTO_LOCK_X509); |
| 279 | } |
| 280 | |
| 281 | if (cpk->privatekey != NULL) |
| 282 | { |
| 283 | rpk->privatekey = cpk->privatekey; |
| 284 | CRYPTO_add(&cpk->privatekey->references, 1, |
| 285 | CRYPTO_LOCK_EVP_PKEY); |
| 286 | |
| 287 | switch(i) |
| 288 | { |
| 289 | /* If there was anything special to do for |
| 290 | * certain types of keys, we'd do it here. |
| 291 | * (Nothing at the moment, I think.) */ |
| 292 | |
| 293 | case SSL_PKEY_RSA_ENC: |
| 294 | case SSL_PKEY_RSA_SIGN: |
| 295 | /* We have an RSA key. */ |
| 296 | break; |
| 297 | |
| 298 | case SSL_PKEY_DSA_SIGN: |
| 299 | /* We have a DSA key. */ |
| 300 | break; |
| 301 | |
| 302 | case SSL_PKEY_DH_RSA: |
| 303 | case SSL_PKEY_DH_DSA: |
| 304 | /* We have a DH key. */ |
| 305 | break; |
| 306 | |
| 307 | case SSL_PKEY_ECC: |
| 308 | /* We have an ECC key */ |
| 309 | break; |
| 310 | |
| 311 | default: |
| 312 | /* Can't happen. */ |
| 313 | OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, SSL_R_LIBRARY_BUG); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | if (cpk->chain) |
| 318 | { |
| 319 | rpk->chain = X509_chain_up_ref(cpk->chain); |
| 320 | if (!rpk->chain) |
| 321 | { |
| 322 | OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_MALLOC_FAILURE); |
| 323 | goto err; |
| 324 | } |
| 325 | } |
| 326 | rpk->valid_flags = 0; |
| 327 | #ifndef OPENSSL_NO_TLSEXT |
| 328 | if (cert->pkeys[i].authz != NULL) |
| 329 | { |
| 330 | /* Just copy everything. */ |
| 331 | ret->pkeys[i].authz_length = |
| 332 | cert->pkeys[i].authz_length; |
| 333 | ret->pkeys[i].authz = |
| 334 | OPENSSL_malloc(ret->pkeys[i].authz_length); |
| 335 | if (ret->pkeys[i].authz == NULL) |
| 336 | { |
| 337 | OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_MALLOC_FAILURE); |
| 338 | return NULL; |
| 339 | } |
| 340 | memcpy(ret->pkeys[i].authz, |
| 341 | cert->pkeys[i].authz, |
| 342 | cert->pkeys[i].authz_length); |
| 343 | } |
| 344 | |
| 345 | if (cert->pkeys[i].serverinfo != NULL) |
| 346 | { |
| 347 | /* Just copy everything. */ |
| 348 | ret->pkeys[i].serverinfo = |
| 349 | OPENSSL_malloc(cert->pkeys[i].serverinfo_length); |
| 350 | if (ret->pkeys[i].serverinfo == NULL) |
| 351 | { |
| 352 | OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_MALLOC_FAILURE); |
| 353 | return NULL; |
| 354 | } |
| 355 | ret->pkeys[i].serverinfo_length = |
| 356 | cert->pkeys[i].serverinfo_length; |
| 357 | memcpy(ret->pkeys[i].serverinfo, |
| 358 | cert->pkeys[i].serverinfo, |
| 359 | cert->pkeys[i].serverinfo_length); |
| 360 | } |
| 361 | #endif |
| 362 | } |
| 363 | |
| 364 | ret->references=1; |
| 365 | /* Set digests to defaults. NB: we don't copy existing values as they |
| 366 | * will be set during handshake. |
| 367 | */ |
| 368 | ssl_cert_set_default_md(ret); |
| 369 | /* Peer sigalgs set to NULL as we get these from handshake too */ |
| 370 | ret->peer_sigalgs = NULL; |
| 371 | ret->peer_sigalgslen = 0; |
| 372 | /* Configured sigalgs however we copy across */ |
| 373 | |
| 374 | if (cert->conf_sigalgs) |
| 375 | { |
| 376 | ret->conf_sigalgs = OPENSSL_malloc(cert->conf_sigalgslen); |
| 377 | if (!ret->conf_sigalgs) |
| 378 | goto err; |
| 379 | memcpy(ret->conf_sigalgs, cert->conf_sigalgs, |
| 380 | cert->conf_sigalgslen); |
| 381 | ret->conf_sigalgslen = cert->conf_sigalgslen; |
| 382 | } |
| 383 | else |
| 384 | ret->conf_sigalgs = NULL; |
| 385 | |
| 386 | if (cert->client_sigalgs) |
| 387 | { |
| 388 | ret->client_sigalgs = OPENSSL_malloc(cert->client_sigalgslen); |
| 389 | if (!ret->client_sigalgs) |
| 390 | goto err; |
| 391 | memcpy(ret->client_sigalgs, cert->client_sigalgs, |
| 392 | cert->client_sigalgslen); |
| 393 | ret->client_sigalgslen = cert->client_sigalgslen; |
| 394 | } |
| 395 | else |
| 396 | ret->client_sigalgs = NULL; |
| 397 | /* Shared sigalgs also NULL */ |
| 398 | ret->shared_sigalgs = NULL; |
| 399 | /* Copy any custom client certificate types */ |
| 400 | if (cert->ctypes) |
| 401 | { |
| 402 | ret->ctypes = OPENSSL_malloc(cert->ctype_num); |
| 403 | if (!ret->ctypes) |
| 404 | goto err; |
| 405 | memcpy(ret->ctypes, cert->ctypes, cert->ctype_num); |
| 406 | ret->ctype_num = cert->ctype_num; |
| 407 | } |
| 408 | |
| 409 | ret->cert_flags = cert->cert_flags; |
| 410 | |
| 411 | ret->cert_cb = cert->cert_cb; |
| 412 | ret->cert_cb_arg = cert->cert_cb_arg; |
| 413 | |
| 414 | if (cert->verify_store) |
| 415 | { |
| 416 | CRYPTO_add(&cert->verify_store->references, 1, CRYPTO_LOCK_X509_STORE); |
| 417 | ret->verify_store = cert->verify_store; |
| 418 | } |
| 419 | |
| 420 | if (cert->chain_store) |
| 421 | { |
| 422 | CRYPTO_add(&cert->chain_store->references, 1, CRYPTO_LOCK_X509_STORE); |
| 423 | ret->chain_store = cert->chain_store; |
| 424 | } |
| 425 | |
| 426 | ret->ciphers_raw = NULL; |
| 427 | |
| 428 | return(ret); |
| 429 | |
| 430 | #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_ECDH) |
| 431 | err: |
| 432 | #endif |
| 433 | #ifndef OPENSSL_NO_RSA |
| 434 | if (ret->rsa_tmp != NULL) |
| 435 | RSA_free(ret->rsa_tmp); |
| 436 | #endif |
| 437 | #ifndef OPENSSL_NO_DH |
| 438 | if (ret->dh_tmp != NULL) |
| 439 | DH_free(ret->dh_tmp); |
| 440 | #endif |
| 441 | #ifndef OPENSSL_NO_ECDH |
| 442 | if (ret->ecdh_tmp != NULL) |
| 443 | EC_KEY_free(ret->ecdh_tmp); |
| 444 | #endif |
| 445 | |
| 446 | ssl_cert_clear_certs(ret); |
| 447 | |
| 448 | return NULL; |
| 449 | } |
| 450 | |
| 451 | /* Free up and clear all certificates and chains */ |
| 452 | |
| 453 | void ssl_cert_clear_certs(CERT *c) |
| 454 | { |
| 455 | int i; |
| 456 | if (c == NULL) |
| 457 | return; |
| 458 | for (i = 0; i<SSL_PKEY_NUM; i++) |
| 459 | { |
| 460 | CERT_PKEY *cpk = c->pkeys + i; |
| 461 | if (cpk->x509) |
| 462 | { |
| 463 | X509_free(cpk->x509); |
| 464 | cpk->x509 = NULL; |
| 465 | } |
| 466 | if (cpk->privatekey) |
| 467 | { |
| 468 | EVP_PKEY_free(cpk->privatekey); |
| 469 | cpk->privatekey = NULL; |
| 470 | } |
| 471 | if (cpk->chain) |
| 472 | { |
| 473 | sk_X509_pop_free(cpk->chain, X509_free); |
| 474 | cpk->chain = NULL; |
| 475 | } |
| 476 | #ifndef OPENSSL_NO_TLSEXT |
| 477 | if (cpk->authz) |
| 478 | { |
| 479 | OPENSSL_free(cpk->authz); |
| 480 | cpk->authz = NULL; |
| 481 | } |
| 482 | if (cpk->serverinfo) |
| 483 | { |
| 484 | OPENSSL_free(cpk->serverinfo); |
| 485 | cpk->serverinfo = NULL; |
| 486 | cpk->serverinfo_length = 0; |
| 487 | } |
| 488 | #endif |
| 489 | /* Clear all flags apart from explicit sign */ |
| 490 | cpk->valid_flags &= CERT_PKEY_EXPLICIT_SIGN; |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | void ssl_cert_free(CERT *c) |
| 495 | { |
| 496 | int i; |
| 497 | |
| 498 | if(c == NULL) |
| 499 | return; |
| 500 | |
| 501 | i=CRYPTO_add(&c->references,-1,CRYPTO_LOCK_SSL_CERT); |
| 502 | #ifdef REF_PRINT |
| 503 | REF_PRINT("CERT",c); |
| 504 | #endif |
| 505 | if (i > 0) return; |
| 506 | #ifdef REF_CHECK |
| 507 | if (i < 0) |
| 508 | { |
| 509 | fprintf(stderr,"ssl_cert_free, bad reference count\n"); |
| 510 | abort(); /* ok */ |
| 511 | } |
| 512 | #endif |
| 513 | |
| 514 | #ifndef OPENSSL_NO_RSA |
| 515 | if (c->rsa_tmp) RSA_free(c->rsa_tmp); |
| 516 | #endif |
| 517 | #ifndef OPENSSL_NO_DH |
| 518 | if (c->dh_tmp) DH_free(c->dh_tmp); |
| 519 | #endif |
| 520 | #ifndef OPENSSL_NO_ECDH |
| 521 | if (c->ecdh_tmp) EC_KEY_free(c->ecdh_tmp); |
| 522 | #endif |
| 523 | |
| 524 | ssl_cert_clear_certs(c); |
| 525 | if (c->peer_sigalgs) |
| 526 | OPENSSL_free(c->peer_sigalgs); |
| 527 | if (c->conf_sigalgs) |
| 528 | OPENSSL_free(c->conf_sigalgs); |
| 529 | if (c->client_sigalgs) |
| 530 | OPENSSL_free(c->client_sigalgs); |
| 531 | if (c->shared_sigalgs) |
| 532 | OPENSSL_free(c->shared_sigalgs); |
| 533 | if (c->ctypes) |
| 534 | OPENSSL_free(c->ctypes); |
| 535 | if (c->verify_store) |
| 536 | X509_STORE_free(c->verify_store); |
| 537 | if (c->chain_store) |
| 538 | X509_STORE_free(c->chain_store); |
| 539 | if (c->ciphers_raw) |
| 540 | OPENSSL_free(c->ciphers_raw); |
| 541 | OPENSSL_free(c); |
| 542 | } |
| 543 | |
| 544 | int ssl_cert_inst(CERT **o) |
| 545 | { |
| 546 | /* Create a CERT if there isn't already one |
| 547 | * (which cannot really happen, as it is initially created in |
| 548 | * SSL_CTX_new; but the earlier code usually allows for that one |
| 549 | * being non-existant, so we follow that behaviour, as it might |
| 550 | * turn out that there actually is a reason for it -- but I'm |
| 551 | * not sure that *all* of the existing code could cope with |
| 552 | * s->cert being NULL, otherwise we could do without the |
| 553 | * initialization in SSL_CTX_new). |
| 554 | */ |
| 555 | |
| 556 | if (o == NULL) |
| 557 | { |
| 558 | OPENSSL_PUT_ERROR(SSL, ssl_cert_inst, ERR_R_PASSED_NULL_PARAMETER); |
| 559 | return(0); |
| 560 | } |
| 561 | if (*o == NULL) |
| 562 | { |
| 563 | if ((*o = ssl_cert_new()) == NULL) |
| 564 | { |
| 565 | OPENSSL_PUT_ERROR(SSL, ssl_cert_new, ERR_R_MALLOC_FAILURE); |
| 566 | return(0); |
| 567 | } |
| 568 | } |
| 569 | return(1); |
| 570 | } |
| 571 | |
| 572 | int ssl_cert_set0_chain(CERT *c, STACK_OF(X509) *chain) |
| 573 | { |
| 574 | CERT_PKEY *cpk = c->key; |
| 575 | if (!cpk) |
| 576 | return 0; |
| 577 | if (cpk->chain) |
| 578 | sk_X509_pop_free(cpk->chain, X509_free); |
| 579 | cpk->chain = chain; |
| 580 | return 1; |
| 581 | } |
| 582 | |
| 583 | int ssl_cert_set1_chain(CERT *c, STACK_OF(X509) *chain) |
| 584 | { |
| 585 | STACK_OF(X509) *dchain; |
| 586 | if (!chain) |
| 587 | return ssl_cert_set0_chain(c, NULL); |
| 588 | dchain = X509_chain_up_ref(chain); |
| 589 | if (!dchain) |
| 590 | return 0; |
| 591 | if (!ssl_cert_set0_chain(c, dchain)) |
| 592 | { |
| 593 | sk_X509_pop_free(dchain, X509_free); |
| 594 | return 0; |
| 595 | } |
| 596 | return 1; |
| 597 | } |
| 598 | |
| 599 | int ssl_cert_add0_chain_cert(CERT *c, X509 *x) |
| 600 | { |
| 601 | CERT_PKEY *cpk = c->key; |
| 602 | if (!cpk) |
| 603 | return 0; |
| 604 | if (!cpk->chain) |
| 605 | cpk->chain = sk_X509_new_null(); |
| 606 | if (!cpk->chain || !sk_X509_push(cpk->chain, x)) |
| 607 | return 0; |
| 608 | return 1; |
| 609 | } |
| 610 | |
| 611 | int ssl_cert_add1_chain_cert(CERT *c, X509 *x) |
| 612 | { |
| 613 | if (!ssl_cert_add0_chain_cert(c, x)) |
| 614 | return 0; |
| 615 | CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509); |
| 616 | return 1; |
| 617 | } |
| 618 | |
| 619 | int ssl_cert_select_current(CERT *c, X509 *x) |
| 620 | { |
| 621 | int i; |
| 622 | if (x == NULL) |
| 623 | return 0; |
| 624 | for (i = 0; i < SSL_PKEY_NUM; i++) |
| 625 | { |
| 626 | if (c->pkeys[i].x509 == x) |
| 627 | { |
| 628 | c->key = &c->pkeys[i]; |
| 629 | return 1; |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | for (i = 0; i < SSL_PKEY_NUM; i++) |
| 634 | { |
| 635 | if (c->pkeys[i].x509 && !X509_cmp(c->pkeys[i].x509, x)) |
| 636 | { |
| 637 | c->key = &c->pkeys[i]; |
| 638 | return 1; |
| 639 | } |
| 640 | } |
| 641 | return 0; |
| 642 | } |
| 643 | |
| 644 | void ssl_cert_set_cert_cb(CERT *c, int (*cb)(SSL *ssl, void *arg), void *arg) |
| 645 | { |
| 646 | c->cert_cb = cb; |
| 647 | c->cert_cb_arg = arg; |
| 648 | } |
| 649 | |
| 650 | SESS_CERT *ssl_sess_cert_new(void) |
| 651 | { |
| 652 | SESS_CERT *ret; |
| 653 | |
| 654 | ret = OPENSSL_malloc(sizeof *ret); |
| 655 | if (ret == NULL) |
| 656 | { |
| 657 | OPENSSL_PUT_ERROR(SSL, ssl_sess_cert_new, ERR_R_MALLOC_FAILURE); |
| 658 | return NULL; |
| 659 | } |
| 660 | |
| 661 | memset(ret, 0 ,sizeof *ret); |
| 662 | ret->peer_key = &(ret->peer_pkeys[SSL_PKEY_RSA_ENC]); |
| 663 | ret->references = 1; |
| 664 | |
| 665 | return ret; |
| 666 | } |
| 667 | |
| 668 | void ssl_sess_cert_free(SESS_CERT *sc) |
| 669 | { |
| 670 | int i; |
| 671 | |
| 672 | if (sc == NULL) |
| 673 | return; |
| 674 | |
| 675 | i = CRYPTO_add(&sc->references, -1, CRYPTO_LOCK_SSL_SESS_CERT); |
| 676 | #ifdef REF_PRINT |
| 677 | REF_PRINT("SESS_CERT", sc); |
| 678 | #endif |
| 679 | if (i > 0) |
| 680 | return; |
| 681 | #ifdef REF_CHECK |
| 682 | if (i < 0) |
| 683 | { |
| 684 | fprintf(stderr,"ssl_sess_cert_free, bad reference count\n"); |
| 685 | abort(); /* ok */ |
| 686 | } |
| 687 | #endif |
| 688 | |
| 689 | /* i == 0 */ |
| 690 | if (sc->cert_chain != NULL) |
| 691 | sk_X509_pop_free(sc->cert_chain, X509_free); |
| 692 | for (i = 0; i < SSL_PKEY_NUM; i++) |
| 693 | { |
| 694 | if (sc->peer_pkeys[i].x509 != NULL) |
| 695 | X509_free(sc->peer_pkeys[i].x509); |
| 696 | #if 0 /* We don't have the peer's private key. These lines are just |
| 697 | * here as a reminder that we're still using a not-quite-appropriate |
| 698 | * data structure. */ |
| 699 | if (sc->peer_pkeys[i].privatekey != NULL) |
| 700 | EVP_PKEY_free(sc->peer_pkeys[i].privatekey); |
| 701 | #endif |
| 702 | } |
| 703 | |
| 704 | #ifndef OPENSSL_NO_RSA |
| 705 | if (sc->peer_rsa_tmp != NULL) |
| 706 | RSA_free(sc->peer_rsa_tmp); |
| 707 | #endif |
| 708 | #ifndef OPENSSL_NO_DH |
| 709 | if (sc->peer_dh_tmp != NULL) |
| 710 | DH_free(sc->peer_dh_tmp); |
| 711 | #endif |
| 712 | #ifndef OPENSSL_NO_ECDH |
| 713 | if (sc->peer_ecdh_tmp != NULL) |
| 714 | EC_KEY_free(sc->peer_ecdh_tmp); |
| 715 | #endif |
| 716 | |
| 717 | OPENSSL_free(sc); |
| 718 | } |
| 719 | |
| 720 | int ssl_set_peer_cert_type(SESS_CERT *sc,int type) |
| 721 | { |
| 722 | sc->peer_cert_type = type; |
| 723 | return(1); |
| 724 | } |
| 725 | |
| 726 | #ifndef OPENSSL_NO_DANE |
| 727 | /* |
| 728 | * return value: |
| 729 | * -1: format or digest error |
| 730 | * 0: match |
| 731 | * 1: no match |
| 732 | */ |
| 733 | int tlsa_cmp(const X509 *cert, const unsigned char *tlsa_record, unsigned int reclen) |
| 734 | { |
| 735 | const EVP_MD *md; |
| 736 | unsigned char digest[EVP_MAX_MD_SIZE]; |
| 737 | unsigned int len, selector, matching_type; |
| 738 | int ret; |
| 739 | |
| 740 | if (reclen<3) return -1; |
| 741 | |
| 742 | selector = tlsa_record[1]; |
| 743 | matching_type = tlsa_record[2]; |
| 744 | tlsa_record += 3; |
| 745 | reclen -= 3; |
| 746 | |
| 747 | switch (matching_type) { |
| 748 | case 0: /* exact match */ |
| 749 | if (selector==0) { /* full certificate */ |
| 750 | ret = EVP_Digest(tlsa_record,reclen,digest,&len,EVP_sha1(),NULL); |
| 751 | return ret ? memcmp(cert->sha1_hash,digest,len)!=0 : -1; |
| 752 | } |
| 753 | else if (selector==1) { /* SubjectPublicKeyInfo */ |
| 754 | ASN1_BIT_STRING *key = X509_get0_pubkey_bitstr(cert); |
| 755 | |
| 756 | if (key == NULL) return -1; |
| 757 | if (key->length != reclen) return 1; |
| 758 | |
| 759 | return memcmp(key->data,tlsa_record,reclen)!=0; |
| 760 | } |
| 761 | return -1; |
| 762 | |
| 763 | case 1: /* SHA256 */ |
| 764 | case 2: /* SHA512 */ |
| 765 | md = matching_type==1 ? EVP_sha256() : EVP_sha512(); |
| 766 | |
| 767 | if (reclen!=EVP_MD_size(md)) return -1; |
| 768 | |
| 769 | if (selector==0) { /* full certificate */ |
| 770 | ret = X509_digest(cert,md,digest,&len); |
| 771 | } |
| 772 | else if (selector==1) { /* SubjectPublicKeyInfo */ |
| 773 | ret = X509_pubkey_digest(cert,md,digest,&len); |
| 774 | } |
| 775 | else |
| 776 | return -1; |
| 777 | |
| 778 | return ret ? memcmp(tlsa_record,digest,len)!=0 : -1; |
| 779 | default: |
| 780 | return -1; |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | int dane_verify_callback(int ok, X509_STORE_CTX *ctx) |
| 785 | { |
| 786 | SSL *s = X509_STORE_CTX_get_ex_data(ctx,SSL_get_ex_data_X509_STORE_CTX_idx()); |
| 787 | int depth=X509_STORE_CTX_get_error_depth(ctx); |
| 788 | X509 *cert = sk_X509_value(ctx->chain,depth); |
| 789 | unsigned int reclen, certificate_usage; |
| 790 | const unsigned char *tlsa_record = s->tlsa_record; |
| 791 | int tlsa_ret = -1; |
| 792 | |
| 793 | if (s->verify_callback) ok = s->verify_callback(ok,ctx); |
| 794 | |
| 795 | if (tlsa_record == NULL) return ok; |
| 796 | |
| 797 | if (tlsa_record == (void*)-1) { |
| 798 | ctx->error = X509_V_ERR_INVALID_CA; /* temporary code? */ |
| 799 | return 0; |
| 800 | } |
| 801 | |
| 802 | while ((reclen = *(unsigned int *)tlsa_record)) { |
| 803 | tlsa_record += sizeof(unsigned int); |
| 804 | |
| 805 | /* |
| 806 | * tlsa_record[0] Certificate Usage field |
| 807 | * tlsa_record[1] Selector field |
| 808 | * tlsa_record[2] Matching Type Field |
| 809 | * tlsa_record+3 Certificate Association data |
| 810 | */ |
| 811 | certificate_usage = tlsa_record[0]; |
| 812 | |
| 813 | if (depth==0 || certificate_usage==0 || certificate_usage==2) { |
| 814 | tlsa_ret = tlsa_cmp(cert,tlsa_record,reclen); |
| 815 | if (tlsa_ret==0) { |
| 816 | s->tlsa_witness = depth<<8|certificate_usage; |
| 817 | break; |
| 818 | } |
| 819 | else if (tlsa_ret==-1) |
| 820 | s->tlsa_witness = -1; /* something phishy? */ |
| 821 | } |
| 822 | |
| 823 | tlsa_record += reclen; |
| 824 | } |
| 825 | |
| 826 | if (depth==0) { |
| 827 | switch (s->tlsa_witness&0xff) { /* witnessed usage */ |
| 828 | case 0: /* CA constraint */ |
| 829 | if (s->tlsa_witness<0 && ctx->error==X509_V_OK) |
| 830 | ctx->error = X509_V_ERR_INVALID_CA; |
| 831 | return 0; |
| 832 | case 1: /* service certificate constraint */ |
| 833 | if (tlsa_ret!=0 && ctx->error==X509_V_OK) |
| 834 | ctx->error = X509_V_ERR_CERT_UNTRUSTED; |
| 835 | return 0; |
| 836 | case 2: /* trust anchor assertion */ |
| 837 | if ((s->tlsa_witness>>8)>0 && ctx->error==X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY) |
| 838 | ctx->error = X509_V_OK; |
| 839 | break; |
| 840 | case 3: /* domain-issued certificate */ |
| 841 | if (tlsa_ret==0) |
| 842 | ctx->error = X509_V_OK; /* override all errors? */ |
| 843 | break; |
| 844 | default:/* there were TLSA records, but something phishy happened */ |
| 845 | ctx->error = X509_V_ERR_CERT_UNTRUSTED; |
| 846 | return ok; |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | /* |
| 851 | * returning 1 makes verify procedure traverse the whole chain, |
| 852 | * not actually approve it... |
| 853 | */ |
| 854 | return 1; |
| 855 | } |
| 856 | #endif |
| 857 | |
| 858 | int ssl_verify_cert_chain(SSL *s,STACK_OF(X509) *sk) |
| 859 | { |
| 860 | X509 *x; |
| 861 | int i; |
| 862 | X509_STORE *verify_store; |
| 863 | X509_STORE_CTX ctx; |
| 864 | |
| 865 | if (s->cert->verify_store) |
| 866 | verify_store = s->cert->verify_store; |
| 867 | else |
| 868 | verify_store = s->ctx->cert_store; |
| 869 | |
| 870 | if ((sk == NULL) || (sk_X509_num(sk) == 0)) |
| 871 | return(0); |
| 872 | |
| 873 | x=sk_X509_value(sk,0); |
| 874 | if(!X509_STORE_CTX_init(&ctx,verify_store,x,sk)) |
| 875 | { |
| 876 | OPENSSL_PUT_ERROR(SSL, ssl_verify_cert_chain, ERR_R_X509_LIB); |
| 877 | return(0); |
| 878 | } |
| 879 | /* Set suite B flags if needed */ |
| 880 | X509_STORE_CTX_set_flags(&ctx, tls1_suiteb(s)); |
| 881 | #if 0 |
| 882 | if (SSL_get_verify_depth(s) >= 0) |
| 883 | X509_STORE_CTX_set_depth(&ctx, SSL_get_verify_depth(s)); |
| 884 | #endif |
| 885 | X509_STORE_CTX_set_ex_data(&ctx,SSL_get_ex_data_X509_STORE_CTX_idx(),s); |
| 886 | |
| 887 | /* We need to inherit the verify parameters. These can be determined by |
| 888 | * the context: if its a server it will verify SSL client certificates |
| 889 | * or vice versa. |
| 890 | */ |
| 891 | |
| 892 | X509_STORE_CTX_set_default(&ctx, |
| 893 | s->server ? "ssl_client" : "ssl_server"); |
| 894 | /* Anything non-default in "param" should overwrite anything in the |
| 895 | * ctx. |
| 896 | */ |
| 897 | X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(&ctx), s->param); |
| 898 | |
| 899 | #ifndef OPENSSL_NO_DANE |
| 900 | X509_STORE_CTX_set_verify_cb(&ctx, dane_verify_callback); |
| 901 | s->tlsa_witness = -1; |
| 902 | #else |
| 903 | if (s->verify_callback) |
| 904 | X509_STORE_CTX_set_verify_cb(&ctx, s->verify_callback); |
| 905 | #endif |
| 906 | |
| 907 | if (s->ctx->app_verify_callback != NULL) |
| 908 | #if 1 /* new with OpenSSL 0.9.7 */ |
| 909 | i=s->ctx->app_verify_callback(&ctx, s->ctx->app_verify_arg); |
| 910 | #else |
| 911 | i=s->ctx->app_verify_callback(&ctx); /* should pass app_verify_arg */ |
| 912 | #endif |
| 913 | else |
| 914 | { |
| 915 | #ifndef OPENSSL_NO_X509_VERIFY |
| 916 | i=X509_verify_cert(&ctx); |
| 917 | #else |
| 918 | i=0; |
| 919 | ctx.error=X509_V_ERR_APPLICATION_VERIFICATION; |
| 920 | OPENSSL_PUT_ERROR(SSL, ssl_verify_cert_chain, SSL_R_NO_VERIFY_CALLBACK); |
| 921 | #endif |
| 922 | } |
| 923 | |
| 924 | s->verify_result=ctx.error; |
| 925 | X509_STORE_CTX_cleanup(&ctx); |
| 926 | |
| 927 | return(i); |
| 928 | } |
| 929 | |
| 930 | static void set_client_CA_list(STACK_OF(X509_NAME) **ca_list,STACK_OF(X509_NAME) *name_list) |
| 931 | { |
| 932 | if (*ca_list != NULL) |
| 933 | sk_X509_NAME_pop_free(*ca_list,X509_NAME_free); |
| 934 | |
| 935 | *ca_list=name_list; |
| 936 | } |
| 937 | |
| 938 | STACK_OF(X509_NAME) *SSL_dup_CA_list(STACK_OF(X509_NAME) *sk) |
| 939 | { |
| 940 | int i; |
| 941 | STACK_OF(X509_NAME) *ret; |
| 942 | X509_NAME *name; |
| 943 | |
| 944 | ret=sk_X509_NAME_new_null(); |
| 945 | for (i=0; i<sk_X509_NAME_num(sk); i++) |
| 946 | { |
| 947 | name=X509_NAME_dup(sk_X509_NAME_value(sk,i)); |
| 948 | if ((name == NULL) || !sk_X509_NAME_push(ret,name)) |
| 949 | { |
| 950 | sk_X509_NAME_pop_free(ret,X509_NAME_free); |
| 951 | return(NULL); |
| 952 | } |
| 953 | } |
| 954 | return(ret); |
| 955 | } |
| 956 | |
| 957 | void SSL_set_client_CA_list(SSL *s,STACK_OF(X509_NAME) *name_list) |
| 958 | { |
| 959 | set_client_CA_list(&(s->client_CA),name_list); |
| 960 | } |
| 961 | |
| 962 | void SSL_CTX_set_client_CA_list(SSL_CTX *ctx,STACK_OF(X509_NAME) *name_list) |
| 963 | { |
| 964 | set_client_CA_list(&(ctx->client_CA),name_list); |
| 965 | } |
| 966 | |
| 967 | STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx) |
| 968 | { |
| 969 | return(ctx->client_CA); |
| 970 | } |
| 971 | |
| 972 | STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *s) |
| 973 | { |
| 974 | if (s->type == SSL_ST_CONNECT) |
| 975 | { /* we are in the client */ |
| 976 | if (((s->version>>8) == SSL3_VERSION_MAJOR) && |
| 977 | (s->s3 != NULL)) |
| 978 | return(s->s3->tmp.ca_names); |
| 979 | else |
| 980 | return(NULL); |
| 981 | } |
| 982 | else |
| 983 | { |
| 984 | if (s->client_CA != NULL) |
| 985 | return(s->client_CA); |
| 986 | else |
| 987 | return(s->ctx->client_CA); |
| 988 | } |
| 989 | } |
| 990 | |
| 991 | static int add_client_CA(STACK_OF(X509_NAME) **sk,X509 *x) |
| 992 | { |
| 993 | X509_NAME *name; |
| 994 | |
| 995 | if (x == NULL) return(0); |
| 996 | if ((*sk == NULL) && ((*sk=sk_X509_NAME_new_null()) == NULL)) |
| 997 | return(0); |
| 998 | |
| 999 | if ((name=X509_NAME_dup(X509_get_subject_name(x))) == NULL) |
| 1000 | return(0); |
| 1001 | |
| 1002 | if (!sk_X509_NAME_push(*sk,name)) |
| 1003 | { |
| 1004 | X509_NAME_free(name); |
| 1005 | return(0); |
| 1006 | } |
| 1007 | return(1); |
| 1008 | } |
| 1009 | |
| 1010 | int SSL_add_client_CA(SSL *ssl,X509 *x) |
| 1011 | { |
| 1012 | return(add_client_CA(&(ssl->client_CA),x)); |
| 1013 | } |
| 1014 | |
| 1015 | int SSL_CTX_add_client_CA(SSL_CTX *ctx,X509 *x) |
| 1016 | { |
| 1017 | return(add_client_CA(&(ctx->client_CA),x)); |
| 1018 | } |
| 1019 | |
| 1020 | static int xname_cmp(const X509_NAME **a, const X509_NAME **b) |
| 1021 | { |
| 1022 | return(X509_NAME_cmp(*a,*b)); |
| 1023 | } |
| 1024 | |
| 1025 | #ifndef OPENSSL_NO_STDIO |
| 1026 | /*! |
| 1027 | * Load CA certs from a file into a ::STACK. Note that it is somewhat misnamed; |
| 1028 | * it doesn't really have anything to do with clients (except that a common use |
| 1029 | * for a stack of CAs is to send it to the client). Actually, it doesn't have |
| 1030 | * much to do with CAs, either, since it will load any old cert. |
| 1031 | * \param file the file containing one or more certs. |
| 1032 | * \return a ::STACK containing the certs. |
| 1033 | */ |
| 1034 | STACK_OF(X509_NAME) *SSL_load_client_CA_file(const char *file) |
| 1035 | { |
| 1036 | BIO *in; |
| 1037 | X509 *x=NULL; |
| 1038 | X509_NAME *xn=NULL; |
| 1039 | STACK_OF(X509_NAME) *ret = NULL,*sk; |
| 1040 | |
| 1041 | sk=sk_X509_NAME_new(xname_cmp); |
| 1042 | |
| 1043 | in=BIO_new(BIO_s_file()); |
| 1044 | |
| 1045 | if ((sk == NULL) || (in == NULL)) |
| 1046 | { |
| 1047 | OPENSSL_PUT_ERROR(SSL, SSL_load_client_CA_file, ERR_R_MALLOC_FAILURE); |
| 1048 | goto err; |
| 1049 | } |
| 1050 | |
| 1051 | if (!BIO_read_filename(in,file)) |
| 1052 | goto err; |
| 1053 | |
| 1054 | for (;;) |
| 1055 | { |
| 1056 | if (PEM_read_bio_X509(in,&x,NULL,NULL) == NULL) |
| 1057 | break; |
| 1058 | if (ret == NULL) |
| 1059 | { |
| 1060 | ret = sk_X509_NAME_new_null(); |
| 1061 | if (ret == NULL) |
| 1062 | { |
| 1063 | OPENSSL_PUT_ERROR(SSL, SSL_load_client_CA_file, ERR_R_MALLOC_FAILURE); |
| 1064 | goto err; |
| 1065 | } |
| 1066 | } |
| 1067 | if ((xn=X509_get_subject_name(x)) == NULL) goto err; |
| 1068 | /* check for duplicates */ |
| 1069 | xn=X509_NAME_dup(xn); |
| 1070 | if (xn == NULL) goto err; |
| 1071 | if (sk_X509_NAME_find(sk, NULL, xn)) |
| 1072 | X509_NAME_free(xn); |
| 1073 | else |
| 1074 | { |
| 1075 | sk_X509_NAME_push(sk,xn); |
| 1076 | sk_X509_NAME_push(ret,xn); |
| 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | if (0) |
| 1081 | { |
| 1082 | err: |
| 1083 | if (ret != NULL) sk_X509_NAME_pop_free(ret,X509_NAME_free); |
| 1084 | ret=NULL; |
| 1085 | } |
| 1086 | if (sk != NULL) sk_X509_NAME_free(sk); |
| 1087 | if (in != NULL) BIO_free(in); |
| 1088 | if (x != NULL) X509_free(x); |
| 1089 | if (ret != NULL) |
| 1090 | ERR_clear_error(); |
| 1091 | return(ret); |
| 1092 | } |
| 1093 | #endif |
| 1094 | |
| 1095 | /*! |
| 1096 | * Add a file of certs to a stack. |
| 1097 | * \param stack the stack to add to. |
| 1098 | * \param file the file to add from. All certs in this file that are not |
| 1099 | * already in the stack will be added. |
| 1100 | * \return 1 for success, 0 for failure. Note that in the case of failure some |
| 1101 | * certs may have been added to \c stack. |
| 1102 | */ |
| 1103 | |
| 1104 | int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack, |
| 1105 | const char *file) |
| 1106 | { |
| 1107 | BIO *in; |
| 1108 | X509 *x=NULL; |
| 1109 | X509_NAME *xn=NULL; |
| 1110 | int ret=1; |
| 1111 | int (*oldcmp)(const X509_NAME **a, const X509_NAME **b); |
| 1112 | |
| 1113 | oldcmp=sk_X509_NAME_set_cmp_func(stack,xname_cmp); |
| 1114 | |
| 1115 | in=BIO_new(BIO_s_file()); |
| 1116 | |
| 1117 | if (in == NULL) |
| 1118 | { |
| 1119 | OPENSSL_PUT_ERROR(SSL, SSL_add_file_cert_subjects_to_stack, ERR_R_MALLOC_FAILURE); |
| 1120 | goto err; |
| 1121 | } |
| 1122 | |
| 1123 | if (!BIO_read_filename(in,file)) |
| 1124 | goto err; |
| 1125 | |
| 1126 | for (;;) |
| 1127 | { |
| 1128 | if (PEM_read_bio_X509(in,&x,NULL,NULL) == NULL) |
| 1129 | break; |
| 1130 | if ((xn=X509_get_subject_name(x)) == NULL) goto err; |
| 1131 | xn=X509_NAME_dup(xn); |
| 1132 | if (xn == NULL) goto err; |
| 1133 | if (sk_X509_NAME_find(stack, NULL, xn)) |
| 1134 | X509_NAME_free(xn); |
| 1135 | else |
| 1136 | sk_X509_NAME_push(stack,xn); |
| 1137 | } |
| 1138 | |
| 1139 | ERR_clear_error(); |
| 1140 | |
| 1141 | if (0) |
| 1142 | { |
| 1143 | err: |
| 1144 | ret=0; |
| 1145 | } |
| 1146 | if(in != NULL) |
| 1147 | BIO_free(in); |
| 1148 | if(x != NULL) |
| 1149 | X509_free(x); |
| 1150 | |
| 1151 | (void)sk_X509_NAME_set_cmp_func(stack,oldcmp); |
| 1152 | |
| 1153 | return ret; |
| 1154 | } |
| 1155 | |
| 1156 | /*! |
| 1157 | * Add a directory of certs to a stack. |
| 1158 | * \param stack the stack to append to. |
| 1159 | * \param dir the directory to append from. All files in this directory will be |
| 1160 | * examined as potential certs. Any that are acceptable to |
| 1161 | * SSL_add_dir_cert_subjects_to_stack() that are not already in the stack will be |
| 1162 | * included. |
| 1163 | * \return 1 for success, 0 for failure. Note that in the case of failure some |
| 1164 | * certs may have been added to \c stack. |
| 1165 | */ |
| 1166 | |
| 1167 | int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack, |
| 1168 | const char *dir) |
| 1169 | { |
| 1170 | OPENSSL_DIR_CTX *d = NULL; |
| 1171 | const char *filename; |
| 1172 | int ret = 0; |
| 1173 | |
| 1174 | CRYPTO_w_lock(CRYPTO_LOCK_READDIR); |
| 1175 | |
| 1176 | /* Note that a side effect is that the CAs will be sorted by name */ |
| 1177 | |
| 1178 | while((filename = OPENSSL_DIR_read(&d, dir))) |
| 1179 | { |
| 1180 | char buf[1024]; |
| 1181 | int r; |
| 1182 | |
| 1183 | if(strlen(dir)+strlen(filename)+2 > sizeof buf) |
| 1184 | { |
| 1185 | OPENSSL_PUT_ERROR(SSL, SSL_add_dir_cert_subjects_to_stack, SSL_R_PATH_TOO_LONG); |
| 1186 | goto err; |
| 1187 | } |
| 1188 | |
| 1189 | #ifdef OPENSSL_SYS_VMS |
| 1190 | r = BIO_snprintf(buf,sizeof buf,"%s%s",dir,filename); |
| 1191 | #else |
| 1192 | r = BIO_snprintf(buf,sizeof buf,"%s/%s",dir,filename); |
| 1193 | #endif |
| 1194 | if (r <= 0 || r >= (int)sizeof(buf)) |
| 1195 | goto err; |
| 1196 | if(!SSL_add_file_cert_subjects_to_stack(stack,buf)) |
| 1197 | goto err; |
| 1198 | } |
| 1199 | |
| 1200 | if (errno) |
| 1201 | { |
| 1202 | OPENSSL_PUT_ERROR(SSL, SSL_add_file_cert_subjects_to_stack, ERR_R_SYS_LIB); |
| 1203 | ERR_add_error_data(3, "OPENSSL_DIR_read(&ctx, '", dir, "')"); |
| 1204 | goto err; |
| 1205 | } |
| 1206 | |
| 1207 | ret = 1; |
| 1208 | |
| 1209 | err: |
| 1210 | if (d) OPENSSL_DIR_end(&d); |
| 1211 | CRYPTO_w_unlock(CRYPTO_LOCK_READDIR); |
| 1212 | return ret; |
| 1213 | } |
| 1214 | |
| 1215 | /* Add a certificate to a BUF_MEM structure */ |
| 1216 | |
| 1217 | static int ssl_add_cert_to_buf(BUF_MEM *buf, unsigned long *l, X509 *x) |
| 1218 | { |
| 1219 | int n; |
| 1220 | unsigned char *p; |
| 1221 | |
| 1222 | n=i2d_X509(x,NULL); |
| 1223 | if (!BUF_MEM_grow_clean(buf,(int)(n+(*l)+3))) |
| 1224 | { |
| 1225 | OPENSSL_PUT_ERROR(SSL, ssl_add_cert_to_buf, ERR_R_BUF_LIB); |
| 1226 | return 0; |
| 1227 | } |
| 1228 | p=(unsigned char *)&(buf->data[*l]); |
| 1229 | l2n3(n,p); |
| 1230 | i2d_X509(x,&p); |
| 1231 | *l+=n+3; |
| 1232 | |
| 1233 | return 1; |
| 1234 | } |
| 1235 | |
| 1236 | /* Add certificate chain to internal SSL BUF_MEM strcuture */ |
| 1237 | int ssl_add_cert_chain(SSL *s, CERT_PKEY *cpk, unsigned long *l) |
| 1238 | { |
| 1239 | BUF_MEM *buf = s->init_buf; |
| 1240 | int no_chain; |
| 1241 | int i; |
| 1242 | |
| 1243 | X509 *x; |
| 1244 | STACK_OF(X509) *extra_certs; |
| 1245 | X509_STORE *chain_store; |
| 1246 | |
| 1247 | if (cpk) |
| 1248 | x = cpk->x509; |
| 1249 | else |
| 1250 | x = NULL; |
| 1251 | |
| 1252 | if (s->cert->chain_store) |
| 1253 | chain_store = s->cert->chain_store; |
| 1254 | else |
| 1255 | chain_store = s->ctx->cert_store; |
| 1256 | |
| 1257 | /* If we have a certificate specific chain use it, else use |
| 1258 | * parent ctx. |
| 1259 | */ |
| 1260 | if (cpk && cpk->chain) |
| 1261 | extra_certs = cpk->chain; |
| 1262 | else |
| 1263 | extra_certs = s->ctx->extra_certs; |
| 1264 | |
| 1265 | if ((s->mode & SSL_MODE_NO_AUTO_CHAIN) || extra_certs) |
| 1266 | no_chain = 1; |
| 1267 | else |
| 1268 | no_chain = 0; |
| 1269 | |
| 1270 | /* TLSv1 sends a chain with nothing in it, instead of an alert */ |
| 1271 | if (!BUF_MEM_grow_clean(buf,10)) |
| 1272 | { |
| 1273 | OPENSSL_PUT_ERROR(SSL, ssl_add_cert_chain, ERR_R_BUF_LIB); |
| 1274 | return 0; |
| 1275 | } |
| 1276 | if (x != NULL) |
| 1277 | { |
| 1278 | if (no_chain) |
| 1279 | { |
| 1280 | if (!ssl_add_cert_to_buf(buf, l, x)) |
| 1281 | return 0; |
| 1282 | } |
| 1283 | else |
| 1284 | { |
| 1285 | X509_STORE_CTX xs_ctx; |
| 1286 | |
| 1287 | if (!X509_STORE_CTX_init(&xs_ctx,chain_store,x,NULL)) |
| 1288 | { |
| 1289 | OPENSSL_PUT_ERROR(SSL, ssl_add_cert_chain, ERR_R_X509_LIB); |
| 1290 | return(0); |
| 1291 | } |
| 1292 | X509_verify_cert(&xs_ctx); |
| 1293 | /* Don't leave errors in the queue */ |
| 1294 | ERR_clear_error(); |
| 1295 | for (i=0; i < sk_X509_num(xs_ctx.chain); i++) |
| 1296 | { |
| 1297 | x = sk_X509_value(xs_ctx.chain, i); |
| 1298 | |
| 1299 | if (!ssl_add_cert_to_buf(buf, l, x)) |
| 1300 | { |
| 1301 | X509_STORE_CTX_cleanup(&xs_ctx); |
| 1302 | return 0; |
| 1303 | } |
| 1304 | } |
| 1305 | X509_STORE_CTX_cleanup(&xs_ctx); |
| 1306 | } |
| 1307 | } |
| 1308 | for (i=0; i<sk_X509_num(extra_certs); i++) |
| 1309 | { |
| 1310 | x=sk_X509_value(extra_certs,i); |
| 1311 | if (!ssl_add_cert_to_buf(buf, l, x)) |
| 1312 | return 0; |
| 1313 | } |
| 1314 | |
| 1315 | return 1; |
| 1316 | } |
| 1317 | |
| 1318 | /* Build a certificate chain for current certificate */ |
| 1319 | int ssl_build_cert_chain(CERT *c, X509_STORE *chain_store, int flags) |
| 1320 | { |
| 1321 | CERT_PKEY *cpk = c->key; |
| 1322 | X509_STORE_CTX xs_ctx; |
| 1323 | STACK_OF(X509) *chain = NULL, *untrusted = NULL; |
| 1324 | X509 *x; |
| 1325 | int i; |
| 1326 | |
| 1327 | if (!cpk->x509) |
| 1328 | { |
| 1329 | OPENSSL_PUT_ERROR(SSL, ssl_build_cert_chain, SSL_R_NO_CERTIFICATE_SET); |
| 1330 | return 0; |
| 1331 | } |
| 1332 | |
| 1333 | if (c->chain_store) |
| 1334 | chain_store = c->chain_store; |
| 1335 | |
| 1336 | if (flags & SSL_BUILD_CHAIN_FLAG_UNTRUSTED) |
| 1337 | untrusted = cpk->chain; |
| 1338 | |
| 1339 | if (!X509_STORE_CTX_init(&xs_ctx, chain_store, cpk->x509, untrusted)) |
| 1340 | { |
| 1341 | OPENSSL_PUT_ERROR(SSL, ssl_build_cert_chain, ERR_R_X509_LIB); |
| 1342 | return 0; |
| 1343 | } |
| 1344 | /* Set suite B flags if needed */ |
| 1345 | X509_STORE_CTX_set_flags(&xs_ctx, c->cert_flags & SSL_CERT_FLAG_SUITEB_128_LOS); |
| 1346 | |
| 1347 | i = X509_verify_cert(&xs_ctx); |
| 1348 | if (i > 0) |
| 1349 | chain = X509_STORE_CTX_get1_chain(&xs_ctx); |
| 1350 | X509_STORE_CTX_cleanup(&xs_ctx); |
| 1351 | if (i <= 0) |
| 1352 | { |
| 1353 | OPENSSL_PUT_ERROR(SSL, ssl_build_cert_chain, SSL_R_CERTIFICATE_VERIFY_FAILED); |
| 1354 | return 0; |
| 1355 | } |
| 1356 | if (cpk->chain) |
| 1357 | sk_X509_pop_free(cpk->chain, X509_free); |
| 1358 | /* Remove EE certificate from chain */ |
| 1359 | x = sk_X509_shift(chain); |
| 1360 | X509_free(x); |
| 1361 | if (flags & SSL_BUILD_CHAIN_FLAG_NO_ROOT) |
| 1362 | { |
| 1363 | x = sk_X509_pop(chain); |
| 1364 | X509_free(x); |
| 1365 | } |
| 1366 | cpk->chain = chain; |
| 1367 | |
| 1368 | return 1; |
| 1369 | } |
| 1370 | |
| 1371 | int ssl_cert_set_cert_store(CERT *c, X509_STORE *store, int chain, int ref) |
| 1372 | { |
| 1373 | X509_STORE **pstore; |
| 1374 | if (chain) |
| 1375 | pstore = &c->chain_store; |
| 1376 | else |
| 1377 | pstore = &c->verify_store; |
| 1378 | if (*pstore) |
| 1379 | X509_STORE_free(*pstore); |
| 1380 | *pstore = store; |
| 1381 | if (ref && store) |
| 1382 | CRYPTO_add(&store->references, 1, CRYPTO_LOCK_X509_STORE); |
| 1383 | return 1; |
| 1384 | } |
| 1385 | |