Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | * ! \file ssl/ssl_cert.c |
| 3 | */ |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 4 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 5 | * All rights reserved. |
| 6 | * |
| 7 | * This package is an SSL implementation written |
| 8 | * by Eric Young (eay@cryptsoft.com). |
| 9 | * The implementation was written so as to conform with Netscapes SSL. |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 10 | * |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 11 | * This library is free for commercial and non-commercial use as long as |
| 12 | * the following conditions are aheared to. The following conditions |
| 13 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 14 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 15 | * included with this distribution is covered by the same copyright terms |
| 16 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 17 | * |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 18 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 19 | * the code are not to be removed. |
| 20 | * If this package is used in a product, Eric Young should be given attribution |
| 21 | * as the author of the parts of the library used. |
| 22 | * This can be in the form of a textual message at program startup or |
| 23 | * in documentation (online or textual) provided with the package. |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 24 | * |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 25 | * Redistribution and use in source and binary forms, with or without |
| 26 | * modification, are permitted provided that the following conditions |
| 27 | * are met: |
| 28 | * 1. Redistributions of source code must retain the copyright |
| 29 | * notice, this list of conditions and the following disclaimer. |
| 30 | * 2. Redistributions in binary form must reproduce the above copyright |
| 31 | * notice, this list of conditions and the following disclaimer in the |
| 32 | * documentation and/or other materials provided with the distribution. |
| 33 | * 3. All advertising materials mentioning features or use of this software |
| 34 | * must display the following acknowledgement: |
| 35 | * "This product includes cryptographic software written by |
| 36 | * Eric Young (eay@cryptsoft.com)" |
| 37 | * The word 'cryptographic' can be left out if the rouines from the library |
| 38 | * being used are not cryptographic related :-). |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 39 | * 4. If you include any Windows specific code (or a derivative thereof) from |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 40 | * the apps directory (application code) you must include an acknowledgement: |
| 41 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 42 | * |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 43 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 44 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 45 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 46 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 47 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 48 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 49 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 50 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 51 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 52 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 53 | * SUCH DAMAGE. |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 54 | * |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 55 | * The licence and distribution terms for any publically available version or |
| 56 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 57 | * copied and put under another distribution licence |
| 58 | * [including the GNU Public Licence.] |
| 59 | */ |
Bodo Möller | ca8e5b9 | 1999-05-09 20:12:44 +0000 | [diff] [blame] | 60 | /* ==================================================================== |
Bodo Möller | 52b8dad | 2007-02-17 06:45:38 +0000 | [diff] [blame] | 61 | * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. |
Bodo Möller | ca8e5b9 | 1999-05-09 20:12:44 +0000 | [diff] [blame] | 62 | * |
| 63 | * Redistribution and use in source and binary forms, with or without |
| 64 | * modification, are permitted provided that the following conditions |
| 65 | * are met: |
| 66 | * |
| 67 | * 1. Redistributions of source code must retain the above copyright |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 68 | * notice, this list of conditions and the following disclaimer. |
Bodo Möller | ca8e5b9 | 1999-05-09 20:12:44 +0000 | [diff] [blame] | 69 | * |
| 70 | * 2. Redistributions in binary form must reproduce the above copyright |
| 71 | * notice, this list of conditions and the following disclaimer in |
| 72 | * the documentation and/or other materials provided with the |
| 73 | * distribution. |
| 74 | * |
| 75 | * 3. All advertising materials mentioning features or use of this |
| 76 | * software must display the following acknowledgment: |
| 77 | * "This product includes software developed by the OpenSSL Project |
Bodo Möller | 675f605 | 2006-06-14 08:55:23 +0000 | [diff] [blame] | 78 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
Bodo Möller | ca8e5b9 | 1999-05-09 20:12:44 +0000 | [diff] [blame] | 79 | * |
| 80 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 81 | * endorse or promote products derived from this software without |
| 82 | * prior written permission. For written permission, please contact |
Bodo Möller | 675f605 | 2006-06-14 08:55:23 +0000 | [diff] [blame] | 83 | * openssl-core@openssl.org. |
Bodo Möller | ca8e5b9 | 1999-05-09 20:12:44 +0000 | [diff] [blame] | 84 | * |
| 85 | * 5. Products derived from this software may not be called "OpenSSL" |
| 86 | * nor may "OpenSSL" appear in their names without prior written |
| 87 | * permission of the OpenSSL Project. |
| 88 | * |
| 89 | * 6. Redistributions of any form whatsoever must retain the following |
| 90 | * acknowledgment: |
| 91 | * "This product includes software developed by the OpenSSL Project |
Bodo Möller | 675f605 | 2006-06-14 08:55:23 +0000 | [diff] [blame] | 92 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
Bodo Möller | ca8e5b9 | 1999-05-09 20:12:44 +0000 | [diff] [blame] | 93 | * |
| 94 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 95 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 96 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 97 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 98 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 99 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 100 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 101 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 102 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 103 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 104 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 105 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 106 | * ==================================================================== |
Bodo Möller | 675f605 | 2006-06-14 08:55:23 +0000 | [diff] [blame] | 107 | * |
| 108 | * This product includes cryptographic software written by Eric Young |
| 109 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 110 | * Hudson (tjh@cryptsoft.com). |
| 111 | * |
Bodo Möller | ca8e5b9 | 1999-05-09 20:12:44 +0000 | [diff] [blame] | 112 | */ |
Bodo Möller | ea26226 | 2002-08-09 08:56:08 +0000 | [diff] [blame] | 113 | /* ==================================================================== |
| 114 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 115 | * ECC cipher suite support in OpenSSL originally developed by |
Bodo Möller | ea26226 | 2002-08-09 08:56:08 +0000 | [diff] [blame] | 116 | * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. |
| 117 | */ |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 118 | |
| 119 | #include <stdio.h> |
Andy Polyakov | 17f389b | 1999-09-11 17:54:18 +0000 | [diff] [blame] | 120 | |
Richard Levitte | 41d2a33 | 2001-02-22 14:45:02 +0000 | [diff] [blame] | 121 | #include "e_os.h" |
Andy Polyakov | 17f389b | 1999-09-11 17:54:18 +0000 | [diff] [blame] | 122 | #ifndef NO_SYS_TYPES_H |
| 123 | # include <sys/types.h> |
| 124 | #endif |
| 125 | |
Richard Levitte | 4083a22 | 2004-07-10 13:17:16 +0000 | [diff] [blame] | 126 | #include "o_dir.h" |
Bodo Möller | ec57782 | 1999-04-23 22:13:45 +0000 | [diff] [blame] | 127 | #include <openssl/objects.h> |
| 128 | #include <openssl/bio.h> |
| 129 | #include <openssl/pem.h> |
Dr. Stephen Henson | bb7cd4e | 1999-11-29 22:35:00 +0000 | [diff] [blame] | 130 | #include <openssl/x509v3.h> |
Nils Larsch | 3eeaab4 | 2005-07-16 12:37:36 +0000 | [diff] [blame] | 131 | #ifndef OPENSSL_NO_DH |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 132 | # include <openssl/dh.h> |
Nils Larsch | 3eeaab4 | 2005-07-16 12:37:36 +0000 | [diff] [blame] | 133 | #endif |
Geoff Thorpe | d095b68 | 2004-05-17 18:53:47 +0000 | [diff] [blame] | 134 | #include <openssl/bn.h> |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 135 | #include "ssl_locl.h" |
| 136 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 137 | static int ssl_security_default_callback(SSL *s, SSL_CTX *ctx, int op, |
| 138 | int bits, int nid, void *other, |
| 139 | void *ex); |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 140 | |
Ulf Möller | 6b691a5 | 1999-04-19 21:31:43 +0000 | [diff] [blame] | 141 | int SSL_get_ex_data_X509_STORE_CTX_idx(void) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 142 | { |
| 143 | static volatile int ssl_x509_store_ctx_idx = -1; |
| 144 | int got_write_lock = 0; |
Bodo Möller | 675f605 | 2006-06-14 08:55:23 +0000 | [diff] [blame] | 145 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 146 | CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX); |
Ralf S. Engelschall | dfeab06 | 1998-12-21 11:00:56 +0000 | [diff] [blame] | 147 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 148 | if (ssl_x509_store_ctx_idx < 0) { |
| 149 | CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX); |
| 150 | CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX); |
| 151 | got_write_lock = 1; |
Bodo Möller | 675f605 | 2006-06-14 08:55:23 +0000 | [diff] [blame] | 152 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 153 | if (ssl_x509_store_ctx_idx < 0) { |
| 154 | ssl_x509_store_ctx_idx = |
| 155 | X509_STORE_CTX_get_ex_new_index(0, "SSL for verify callback", |
| 156 | NULL, NULL, NULL); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | if (got_write_lock) |
| 161 | CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX); |
| 162 | else |
| 163 | CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX); |
| 164 | |
| 165 | return ssl_x509_store_ctx_idx; |
| 166 | } |
Ralf S. Engelschall | dfeab06 | 1998-12-21 11:00:56 +0000 | [diff] [blame] | 167 | |
Dr. Stephen Henson | 4453cd8 | 2012-06-25 14:32:30 +0000 | [diff] [blame] | 168 | void ssl_cert_set_default_md(CERT *cert) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 169 | { |
| 170 | /* Set digest values to defaults */ |
Dr. Stephen Henson | 6b7be58 | 2011-05-06 13:00:07 +0000 | [diff] [blame] | 171 | #ifndef OPENSSL_NO_DSA |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 172 | cert->pkeys[SSL_PKEY_DSA_SIGN].digest = EVP_sha1(); |
Dr. Stephen Henson | 6b7be58 | 2011-05-06 13:00:07 +0000 | [diff] [blame] | 173 | #endif |
| 174 | #ifndef OPENSSL_NO_RSA |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 175 | cert->pkeys[SSL_PKEY_RSA_SIGN].digest = EVP_sha1(); |
| 176 | cert->pkeys[SSL_PKEY_RSA_ENC].digest = EVP_sha1(); |
Dr. Stephen Henson | 6b7be58 | 2011-05-06 13:00:07 +0000 | [diff] [blame] | 177 | #endif |
Rich Salz | 10bf4fc | 2015-03-10 19:09:27 -0400 | [diff] [blame] | 178 | #ifndef OPENSSL_NO_EC |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 179 | cert->pkeys[SSL_PKEY_ECC].digest = EVP_sha1(); |
Dr. Stephen Henson | 6b7be58 | 2011-05-06 13:00:07 +0000 | [diff] [blame] | 180 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 181 | } |
Dr. Stephen Henson | 6b7be58 | 2011-05-06 13:00:07 +0000 | [diff] [blame] | 182 | |
Ulf Möller | 6b691a5 | 1999-04-19 21:31:43 +0000 | [diff] [blame] | 183 | CERT *ssl_cert_new(void) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 184 | { |
Rich Salz | b4faea5 | 2015-05-01 23:10:31 -0400 | [diff] [blame] | 185 | CERT *ret = OPENSSL_malloc(sizeof(*ret)); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 186 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 187 | if (ret == NULL) { |
| 188 | SSLerr(SSL_F_SSL_CERT_NEW, ERR_R_MALLOC_FAILURE); |
| 189 | return (NULL); |
| 190 | } |
Rich Salz | 16f8d4e | 2015-05-04 18:00:15 -0400 | [diff] [blame^] | 191 | memset(ret, 0, sizeof(*ret)); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 192 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 193 | ret->key = &(ret->pkeys[SSL_PKEY_RSA_ENC]); |
| 194 | ret->references = 1; |
| 195 | ssl_cert_set_default_md(ret); |
| 196 | ret->sec_cb = ssl_security_default_callback; |
| 197 | ret->sec_level = OPENSSL_TLS_SECURITY_LEVEL; |
| 198 | ret->sec_ex = NULL; |
| 199 | return (ret); |
| 200 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 201 | |
Bodo Möller | ca8e5b9 | 1999-05-09 20:12:44 +0000 | [diff] [blame] | 202 | CERT *ssl_cert_dup(CERT *cert) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 203 | { |
Rich Salz | b4faea5 | 2015-05-01 23:10:31 -0400 | [diff] [blame] | 204 | CERT *ret = OPENSSL_malloc(sizeof(*ret)); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 205 | int i; |
Bodo Möller | ca8e5b9 | 1999-05-09 20:12:44 +0000 | [diff] [blame] | 206 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 207 | if (ret == NULL) { |
| 208 | SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_MALLOC_FAILURE); |
| 209 | return (NULL); |
| 210 | } |
Bodo Möller | ca8e5b9 | 1999-05-09 20:12:44 +0000 | [diff] [blame] | 211 | |
Rich Salz | 16f8d4e | 2015-05-04 18:00:15 -0400 | [diff] [blame^] | 212 | memset(ret, 0, sizeof(*ret)); |
Bodo Möller | ca8e5b9 | 1999-05-09 20:12:44 +0000 | [diff] [blame] | 213 | |
Rich Salz | 16f8d4e | 2015-05-04 18:00:15 -0400 | [diff] [blame^] | 214 | ret->key = &ret->pkeys[cert->key - cert->pkeys]; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 215 | ret->valid = cert->valid; |
| 216 | ret->mask_k = cert->mask_k; |
| 217 | ret->mask_a = cert->mask_a; |
| 218 | ret->export_mask_k = cert->export_mask_k; |
| 219 | ret->export_mask_a = cert->export_mask_a; |
Bodo Möller | ca8e5b9 | 1999-05-09 20:12:44 +0000 | [diff] [blame] | 220 | |
Richard Levitte | bc36ee6 | 2001-02-20 08:13:47 +0000 | [diff] [blame] | 221 | #ifndef OPENSSL_NO_RSA |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 222 | if (cert->rsa_tmp != NULL) { |
| 223 | RSA_up_ref(cert->rsa_tmp); |
| 224 | ret->rsa_tmp = cert->rsa_tmp; |
| 225 | } |
| 226 | ret->rsa_tmp_cb = cert->rsa_tmp_cb; |
Bodo Möller | ca8e5b9 | 1999-05-09 20:12:44 +0000 | [diff] [blame] | 227 | #endif |
| 228 | |
Richard Levitte | bc36ee6 | 2001-02-20 08:13:47 +0000 | [diff] [blame] | 229 | #ifndef OPENSSL_NO_DH |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 230 | if (cert->dh_tmp != NULL) { |
| 231 | ret->dh_tmp = DHparams_dup(cert->dh_tmp); |
| 232 | if (ret->dh_tmp == NULL) { |
| 233 | SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_DH_LIB); |
| 234 | goto err; |
| 235 | } |
| 236 | if (cert->dh_tmp->priv_key) { |
| 237 | BIGNUM *b = BN_dup(cert->dh_tmp->priv_key); |
| 238 | if (!b) { |
| 239 | SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_BN_LIB); |
| 240 | goto err; |
| 241 | } |
| 242 | ret->dh_tmp->priv_key = b; |
| 243 | } |
| 244 | if (cert->dh_tmp->pub_key) { |
| 245 | BIGNUM *b = BN_dup(cert->dh_tmp->pub_key); |
| 246 | if (!b) { |
| 247 | SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_BN_LIB); |
| 248 | goto err; |
| 249 | } |
| 250 | ret->dh_tmp->pub_key = b; |
| 251 | } |
| 252 | } |
| 253 | ret->dh_tmp_cb = cert->dh_tmp_cb; |
| 254 | ret->dh_tmp_auto = cert->dh_tmp_auto; |
Bodo Möller | ca8e5b9 | 1999-05-09 20:12:44 +0000 | [diff] [blame] | 255 | #endif |
| 256 | |
Rich Salz | 10bf4fc | 2015-03-10 19:09:27 -0400 | [diff] [blame] | 257 | #ifndef OPENSSL_NO_EC |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 258 | if (cert->ecdh_tmp) { |
| 259 | ret->ecdh_tmp = EC_KEY_dup(cert->ecdh_tmp); |
| 260 | if (ret->ecdh_tmp == NULL) { |
| 261 | SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_EC_LIB); |
| 262 | goto err; |
| 263 | } |
| 264 | } |
| 265 | ret->ecdh_tmp_cb = cert->ecdh_tmp_cb; |
| 266 | ret->ecdh_tmp_auto = cert->ecdh_tmp_auto; |
Bodo Möller | ea26226 | 2002-08-09 08:56:08 +0000 | [diff] [blame] | 267 | #endif |
| 268 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 269 | for (i = 0; i < SSL_PKEY_NUM; i++) { |
| 270 | CERT_PKEY *cpk = cert->pkeys + i; |
| 271 | CERT_PKEY *rpk = ret->pkeys + i; |
| 272 | if (cpk->x509 != NULL) { |
| 273 | rpk->x509 = cpk->x509; |
| 274 | CRYPTO_add(&rpk->x509->references, 1, CRYPTO_LOCK_X509); |
| 275 | } |
Dr. Stephen Henson | f71c6e5 | 2012-01-31 14:00:10 +0000 | [diff] [blame] | 276 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 277 | if (cpk->privatekey != NULL) { |
| 278 | rpk->privatekey = cpk->privatekey; |
| 279 | CRYPTO_add(&cpk->privatekey->references, 1, CRYPTO_LOCK_EVP_PKEY); |
| 280 | } |
| 281 | |
| 282 | if (cpk->chain) { |
| 283 | rpk->chain = X509_chain_up_ref(cpk->chain); |
| 284 | if (!rpk->chain) { |
| 285 | SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_MALLOC_FAILURE); |
| 286 | goto err; |
| 287 | } |
| 288 | } |
| 289 | rpk->valid_flags = 0; |
Trevor | a398f82 | 2013-05-12 18:55:27 -0700 | [diff] [blame] | 290 | #ifndef OPENSSL_NO_TLSEXT |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 291 | if (cert->pkeys[i].serverinfo != NULL) { |
| 292 | /* Just copy everything. */ |
| 293 | ret->pkeys[i].serverinfo = |
| 294 | OPENSSL_malloc(cert->pkeys[i].serverinfo_length); |
| 295 | if (ret->pkeys[i].serverinfo == NULL) { |
| 296 | SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_MALLOC_FAILURE); |
| 297 | goto err; |
| 298 | } |
| 299 | ret->pkeys[i].serverinfo_length = |
| 300 | cert->pkeys[i].serverinfo_length; |
| 301 | memcpy(ret->pkeys[i].serverinfo, |
| 302 | cert->pkeys[i].serverinfo, |
| 303 | cert->pkeys[i].serverinfo_length); |
| 304 | } |
Trevor | a398f82 | 2013-05-12 18:55:27 -0700 | [diff] [blame] | 305 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 306 | } |
Dr. Stephen Henson | 3dbc46d | 2012-07-03 12:51:14 +0000 | [diff] [blame] | 307 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 308 | ret->references = 1; |
| 309 | /* |
| 310 | * Set digests to defaults. NB: we don't copy existing values as they |
| 311 | * will be set during handshake. |
| 312 | */ |
| 313 | ssl_cert_set_default_md(ret); |
| 314 | /* Peer sigalgs set to NULL as we get these from handshake too */ |
| 315 | ret->peer_sigalgs = NULL; |
| 316 | ret->peer_sigalgslen = 0; |
| 317 | /* Configured sigalgs however we copy across */ |
Dr. Stephen Henson | 3dbc46d | 2012-07-03 12:51:14 +0000 | [diff] [blame] | 318 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 319 | if (cert->conf_sigalgs) { |
| 320 | ret->conf_sigalgs = OPENSSL_malloc(cert->conf_sigalgslen); |
| 321 | if (!ret->conf_sigalgs) |
| 322 | goto err; |
| 323 | memcpy(ret->conf_sigalgs, cert->conf_sigalgs, cert->conf_sigalgslen); |
| 324 | ret->conf_sigalgslen = cert->conf_sigalgslen; |
| 325 | } else |
| 326 | ret->conf_sigalgs = NULL; |
Bodo Möller | ca8e5b9 | 1999-05-09 20:12:44 +0000 | [diff] [blame] | 327 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 328 | if (cert->client_sigalgs) { |
| 329 | ret->client_sigalgs = OPENSSL_malloc(cert->client_sigalgslen); |
| 330 | if (!ret->client_sigalgs) |
| 331 | goto err; |
| 332 | memcpy(ret->client_sigalgs, cert->client_sigalgs, |
| 333 | cert->client_sigalgslen); |
| 334 | ret->client_sigalgslen = cert->client_sigalgslen; |
| 335 | } else |
| 336 | ret->client_sigalgs = NULL; |
| 337 | /* Shared sigalgs also NULL */ |
| 338 | ret->shared_sigalgs = NULL; |
| 339 | /* Copy any custom client certificate types */ |
| 340 | if (cert->ctypes) { |
| 341 | ret->ctypes = OPENSSL_malloc(cert->ctype_num); |
| 342 | if (!ret->ctypes) |
| 343 | goto err; |
| 344 | memcpy(ret->ctypes, cert->ctypes, cert->ctype_num); |
| 345 | ret->ctype_num = cert->ctype_num; |
| 346 | } |
Dr. Stephen Henson | d61ff83 | 2012-06-28 12:45:49 +0000 | [diff] [blame] | 347 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 348 | ret->cert_flags = cert->cert_flags; |
Dr. Stephen Henson | 18d7158 | 2012-06-29 14:24:42 +0000 | [diff] [blame] | 349 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 350 | ret->cert_cb = cert->cert_cb; |
| 351 | ret->cert_cb_arg = cert->cert_cb_arg; |
Dr. Stephen Henson | 74ecfab | 2012-07-23 23:34:28 +0000 | [diff] [blame] | 352 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 353 | if (cert->verify_store) { |
| 354 | CRYPTO_add(&cert->verify_store->references, 1, |
| 355 | CRYPTO_LOCK_X509_STORE); |
| 356 | ret->verify_store = cert->verify_store; |
| 357 | } |
Dr. Stephen Henson | 74ecfab | 2012-07-23 23:34:28 +0000 | [diff] [blame] | 358 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 359 | if (cert->chain_store) { |
| 360 | CRYPTO_add(&cert->chain_store->references, 1, CRYPTO_LOCK_X509_STORE); |
| 361 | ret->chain_store = cert->chain_store; |
| 362 | } |
Dr. Stephen Henson | 94a209d | 2012-09-12 13:57:48 +0000 | [diff] [blame] | 363 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 364 | ret->ciphers_raw = NULL; |
| 365 | |
| 366 | ret->sec_cb = cert->sec_cb; |
| 367 | ret->sec_level = cert->sec_level; |
| 368 | ret->sec_ex = cert->sec_ex; |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 369 | |
Dr. Stephen Henson | b83294f | 2014-08-05 15:21:36 +0100 | [diff] [blame] | 370 | #ifndef OPENSSL_NO_TLSEXT |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 371 | if (!custom_exts_copy(&ret->cli_ext, &cert->cli_ext)) |
| 372 | goto err; |
| 373 | if (!custom_exts_copy(&ret->srv_ext, &cert->srv_ext)) |
| 374 | goto err; |
Dr. Stephen Henson | b83294f | 2014-08-05 15:21:36 +0100 | [diff] [blame] | 375 | #endif |
| 376 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 377 | return (ret); |
Dr. Stephen Henson | 9ade64d | 2012-01-27 14:21:38 +0000 | [diff] [blame] | 378 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 379 | err: |
| 380 | ssl_cert_free(ret); |
| 381 | |
| 382 | return NULL; |
| 383 | } |
Bodo Möller | ca8e5b9 | 1999-05-09 20:12:44 +0000 | [diff] [blame] | 384 | |
Dr. Stephen Henson | a5ee80b | 2012-06-18 12:56:59 +0000 | [diff] [blame] | 385 | /* Free up and clear all certificates and chains */ |
| 386 | |
| 387 | void ssl_cert_clear_certs(CERT *c) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 388 | { |
| 389 | int i; |
| 390 | if (c == NULL) |
| 391 | return; |
| 392 | for (i = 0; i < SSL_PKEY_NUM; i++) { |
| 393 | CERT_PKEY *cpk = c->pkeys + i; |
Rich Salz | 222561f | 2015-04-30 17:33:59 -0400 | [diff] [blame] | 394 | X509_free(cpk->x509); |
| 395 | cpk->x509 = NULL; |
Rich Salz | c5ba2d9 | 2015-03-28 10:54:15 -0400 | [diff] [blame] | 396 | EVP_PKEY_free(cpk->privatekey); |
| 397 | cpk->privatekey = NULL; |
Rich Salz | 222561f | 2015-04-30 17:33:59 -0400 | [diff] [blame] | 398 | sk_X509_pop_free(cpk->chain, X509_free); |
| 399 | cpk->chain = NULL; |
Dr. Stephen Henson | a5ee80b | 2012-06-18 12:56:59 +0000 | [diff] [blame] | 400 | #ifndef OPENSSL_NO_TLSEXT |
Rich Salz | 25aaa98 | 2015-05-01 14:37:16 -0400 | [diff] [blame] | 401 | OPENSSL_free(cpk->serverinfo); |
| 402 | cpk->serverinfo = NULL; |
| 403 | cpk->serverinfo_length = 0; |
Dr. Stephen Henson | a5ee80b | 2012-06-18 12:56:59 +0000 | [diff] [blame] | 404 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 405 | /* Clear all flags apart from explicit sign */ |
| 406 | cpk->valid_flags &= CERT_PKEY_EXPLICIT_SIGN; |
| 407 | } |
| 408 | } |
Bodo Möller | ca8e5b9 | 1999-05-09 20:12:44 +0000 | [diff] [blame] | 409 | |
Ben Laurie | eb90a48 | 1999-02-28 17:41:55 +0000 | [diff] [blame] | 410 | void ssl_cert_free(CERT *c) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 411 | { |
| 412 | int i; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 413 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 414 | if (c == NULL) |
| 415 | return; |
Ben Laurie | e03ddfa | 1999-01-07 19:15:59 +0000 | [diff] [blame] | 416 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 417 | i = CRYPTO_add(&c->references, -1, CRYPTO_LOCK_SSL_CERT); |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 418 | #ifdef REF_PRINT |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 419 | REF_PRINT("CERT", c); |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 420 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 421 | if (i > 0) |
| 422 | return; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 423 | #ifdef REF_CHECK |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 424 | if (i < 0) { |
| 425 | fprintf(stderr, "ssl_cert_free, bad reference count\n"); |
| 426 | abort(); /* ok */ |
| 427 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 428 | #endif |
| 429 | |
Richard Levitte | bc36ee6 | 2001-02-20 08:13:47 +0000 | [diff] [blame] | 430 | #ifndef OPENSSL_NO_RSA |
Rich Salz | d640708 | 2015-03-24 10:17:37 -0400 | [diff] [blame] | 431 | RSA_free(c->rsa_tmp); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 432 | #endif |
Richard Levitte | bc36ee6 | 2001-02-20 08:13:47 +0000 | [diff] [blame] | 433 | #ifndef OPENSSL_NO_DH |
Rich Salz | d640708 | 2015-03-24 10:17:37 -0400 | [diff] [blame] | 434 | DH_free(c->dh_tmp); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 435 | #endif |
Rich Salz | 10bf4fc | 2015-03-10 19:09:27 -0400 | [diff] [blame] | 436 | #ifndef OPENSSL_NO_EC |
Rich Salz | 8fdc373 | 2015-03-25 18:35:24 -0400 | [diff] [blame] | 437 | EC_KEY_free(c->ecdh_tmp); |
Bodo Möller | ea26226 | 2002-08-09 08:56:08 +0000 | [diff] [blame] | 438 | #endif |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 439 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 440 | ssl_cert_clear_certs(c); |
Rich Salz | 25aaa98 | 2015-05-01 14:37:16 -0400 | [diff] [blame] | 441 | OPENSSL_free(c->peer_sigalgs); |
| 442 | OPENSSL_free(c->conf_sigalgs); |
| 443 | OPENSSL_free(c->client_sigalgs); |
| 444 | OPENSSL_free(c->shared_sigalgs); |
| 445 | OPENSSL_free(c->ctypes); |
Rich Salz | 222561f | 2015-04-30 17:33:59 -0400 | [diff] [blame] | 446 | X509_STORE_free(c->verify_store); |
| 447 | X509_STORE_free(c->chain_store); |
Rich Salz | 25aaa98 | 2015-05-01 14:37:16 -0400 | [diff] [blame] | 448 | OPENSSL_free(c->ciphers_raw); |
Dr. Stephen Henson | b83294f | 2014-08-05 15:21:36 +0100 | [diff] [blame] | 449 | #ifndef OPENSSL_NO_TLSEXT |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 450 | custom_exts_free(&c->cli_ext); |
| 451 | custom_exts_free(&c->srv_ext); |
Dr. Stephen Henson | b83294f | 2014-08-05 15:21:36 +0100 | [diff] [blame] | 452 | #endif |
Rich Salz | 4b45c6e | 2015-04-30 17:57:32 -0400 | [diff] [blame] | 453 | OPENSSL_clear_free(c->pms, c->pmslen); |
| 454 | c->pms = NULL; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 455 | OPENSSL_free(c); |
| 456 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 457 | |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 458 | int ssl_cert_set0_chain(SSL *s, SSL_CTX *ctx, STACK_OF(X509) *chain) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 459 | { |
| 460 | int i, r; |
| 461 | CERT_PKEY *cpk = s ? s->cert->key : ctx->cert->key; |
| 462 | if (!cpk) |
| 463 | return 0; |
Rich Salz | 222561f | 2015-04-30 17:33:59 -0400 | [diff] [blame] | 464 | sk_X509_pop_free(cpk->chain, X509_free); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 465 | for (i = 0; i < sk_X509_num(chain); i++) { |
| 466 | r = ssl_security_cert(s, ctx, sk_X509_value(chain, i), 0, 0); |
| 467 | if (r != 1) { |
| 468 | SSLerr(SSL_F_SSL_CERT_SET0_CHAIN, r); |
| 469 | return 0; |
| 470 | } |
| 471 | } |
| 472 | cpk->chain = chain; |
| 473 | return 1; |
| 474 | } |
Dr. Stephen Henson | f71c6e5 | 2012-01-31 14:00:10 +0000 | [diff] [blame] | 475 | |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 476 | int ssl_cert_set1_chain(SSL *s, SSL_CTX *ctx, STACK_OF(X509) *chain) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 477 | { |
| 478 | STACK_OF(X509) *dchain; |
| 479 | if (!chain) |
| 480 | return ssl_cert_set0_chain(s, ctx, NULL); |
| 481 | dchain = X509_chain_up_ref(chain); |
| 482 | if (!dchain) |
| 483 | return 0; |
| 484 | if (!ssl_cert_set0_chain(s, ctx, dchain)) { |
| 485 | sk_X509_pop_free(dchain, X509_free); |
| 486 | return 0; |
| 487 | } |
| 488 | return 1; |
| 489 | } |
Dr. Stephen Henson | f71c6e5 | 2012-01-31 14:00:10 +0000 | [diff] [blame] | 490 | |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 491 | int ssl_cert_add0_chain_cert(SSL *s, SSL_CTX *ctx, X509 *x) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 492 | { |
| 493 | int r; |
| 494 | CERT_PKEY *cpk = s ? s->cert->key : ctx->cert->key; |
| 495 | if (!cpk) |
| 496 | return 0; |
| 497 | r = ssl_security_cert(s, ctx, x, 0, 0); |
| 498 | if (r != 1) { |
| 499 | SSLerr(SSL_F_SSL_CERT_ADD0_CHAIN_CERT, r); |
| 500 | return 0; |
| 501 | } |
| 502 | if (!cpk->chain) |
| 503 | cpk->chain = sk_X509_new_null(); |
| 504 | if (!cpk->chain || !sk_X509_push(cpk->chain, x)) |
| 505 | return 0; |
| 506 | return 1; |
| 507 | } |
Dr. Stephen Henson | f71c6e5 | 2012-01-31 14:00:10 +0000 | [diff] [blame] | 508 | |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 509 | int ssl_cert_add1_chain_cert(SSL *s, SSL_CTX *ctx, X509 *x) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 510 | { |
| 511 | if (!ssl_cert_add0_chain_cert(s, ctx, x)) |
| 512 | return 0; |
| 513 | CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509); |
| 514 | return 1; |
| 515 | } |
Bodo Möller | b56bce4 | 1999-05-13 15:09:38 +0000 | [diff] [blame] | 516 | |
Rob Stradling | 7b6b246 | 2013-11-11 18:04:24 +0100 | [diff] [blame] | 517 | int ssl_cert_select_current(CERT *c, X509 *x) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 518 | { |
| 519 | int i; |
| 520 | if (x == NULL) |
| 521 | return 0; |
| 522 | for (i = 0; i < SSL_PKEY_NUM; i++) { |
| 523 | CERT_PKEY *cpk = c->pkeys + i; |
| 524 | if (cpk->x509 == x && cpk->privatekey) { |
| 525 | c->key = cpk; |
| 526 | return 1; |
| 527 | } |
| 528 | } |
Dr. Stephen Henson | 629b640 | 2013-11-13 22:57:11 +0000 | [diff] [blame] | 529 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 530 | for (i = 0; i < SSL_PKEY_NUM; i++) { |
| 531 | CERT_PKEY *cpk = c->pkeys + i; |
| 532 | if (cpk->privatekey && cpk->x509 && !X509_cmp(cpk->x509, x)) { |
| 533 | c->key = cpk; |
| 534 | return 1; |
| 535 | } |
| 536 | } |
| 537 | return 0; |
| 538 | } |
Rob Stradling | 7b6b246 | 2013-11-11 18:04:24 +0100 | [diff] [blame] | 539 | |
Dr. Stephen Henson | 0f78819 | 2014-02-02 02:51:30 +0000 | [diff] [blame] | 540 | int ssl_cert_set_current(CERT *c, long op) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 541 | { |
| 542 | int i, idx; |
| 543 | if (!c) |
| 544 | return 0; |
| 545 | if (op == SSL_CERT_SET_FIRST) |
| 546 | idx = 0; |
| 547 | else if (op == SSL_CERT_SET_NEXT) { |
| 548 | idx = (int)(c->key - c->pkeys + 1); |
| 549 | if (idx >= SSL_PKEY_NUM) |
| 550 | return 0; |
| 551 | } else |
| 552 | return 0; |
| 553 | for (i = idx; i < SSL_PKEY_NUM; i++) { |
| 554 | CERT_PKEY *cpk = c->pkeys + i; |
| 555 | if (cpk->x509 && cpk->privatekey) { |
| 556 | c->key = cpk; |
| 557 | return 1; |
| 558 | } |
| 559 | } |
| 560 | return 0; |
| 561 | } |
Dr. Stephen Henson | 0f78819 | 2014-02-02 02:51:30 +0000 | [diff] [blame] | 562 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 563 | void ssl_cert_set_cert_cb(CERT *c, int (*cb) (SSL *ssl, void *arg), void *arg) |
| 564 | { |
| 565 | c->cert_cb = cb; |
| 566 | c->cert_cb_arg = arg; |
| 567 | } |
Dr. Stephen Henson | 18d7158 | 2012-06-29 14:24:42 +0000 | [diff] [blame] | 568 | |
Bodo Möller | b56bce4 | 1999-05-13 15:09:38 +0000 | [diff] [blame] | 569 | SESS_CERT *ssl_sess_cert_new(void) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 570 | { |
| 571 | SESS_CERT *ret; |
Bodo Möller | b56bce4 | 1999-05-13 15:09:38 +0000 | [diff] [blame] | 572 | |
Rich Salz | b4faea5 | 2015-05-01 23:10:31 -0400 | [diff] [blame] | 573 | ret = OPENSSL_malloc(sizeof(*ret)); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 574 | if (ret == NULL) { |
| 575 | SSLerr(SSL_F_SSL_SESS_CERT_NEW, ERR_R_MALLOC_FAILURE); |
| 576 | return NULL; |
| 577 | } |
Bodo Möller | b56bce4 | 1999-05-13 15:09:38 +0000 | [diff] [blame] | 578 | |
Rich Salz | b4faea5 | 2015-05-01 23:10:31 -0400 | [diff] [blame] | 579 | memset(ret, 0, sizeof(*ret)); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 580 | ret->peer_key = &(ret->peer_pkeys[SSL_PKEY_RSA_ENC]); |
| 581 | ret->references = 1; |
Bodo Möller | b56bce4 | 1999-05-13 15:09:38 +0000 | [diff] [blame] | 582 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 583 | return ret; |
| 584 | } |
Bodo Möller | b56bce4 | 1999-05-13 15:09:38 +0000 | [diff] [blame] | 585 | |
| 586 | void ssl_sess_cert_free(SESS_CERT *sc) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 587 | { |
| 588 | int i; |
Bodo Möller | b56bce4 | 1999-05-13 15:09:38 +0000 | [diff] [blame] | 589 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 590 | if (sc == NULL) |
| 591 | return; |
Bodo Möller | b56bce4 | 1999-05-13 15:09:38 +0000 | [diff] [blame] | 592 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 593 | i = CRYPTO_add(&sc->references, -1, CRYPTO_LOCK_SSL_SESS_CERT); |
Bodo Möller | b56bce4 | 1999-05-13 15:09:38 +0000 | [diff] [blame] | 594 | #ifdef REF_PRINT |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 595 | REF_PRINT("SESS_CERT", sc); |
Bodo Möller | b56bce4 | 1999-05-13 15:09:38 +0000 | [diff] [blame] | 596 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 597 | if (i > 0) |
| 598 | return; |
Bodo Möller | b56bce4 | 1999-05-13 15:09:38 +0000 | [diff] [blame] | 599 | #ifdef REF_CHECK |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 600 | if (i < 0) { |
| 601 | fprintf(stderr, "ssl_sess_cert_free, bad reference count\n"); |
| 602 | abort(); /* ok */ |
| 603 | } |
Bodo Möller | ca8e5b9 | 1999-05-09 20:12:44 +0000 | [diff] [blame] | 604 | #endif |
Ralf S. Engelschall | 15d21c2 | 1999-02-25 14:40:29 +0000 | [diff] [blame] | 605 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 606 | /* i == 0 */ |
Rich Salz | 222561f | 2015-04-30 17:33:59 -0400 | [diff] [blame] | 607 | sk_X509_pop_free(sc->cert_chain, X509_free); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 608 | for (i = 0; i < SSL_PKEY_NUM; i++) { |
Rich Salz | 222561f | 2015-04-30 17:33:59 -0400 | [diff] [blame] | 609 | X509_free(sc->peer_pkeys[i].x509); |
Rich Salz | 9e9858d | 2015-02-06 10:52:12 -0500 | [diff] [blame] | 610 | #if 0 |
| 611 | /* |
Rich Salz | 25aaa98 | 2015-05-01 14:37:16 -0400 | [diff] [blame] | 612 | * We don't have the peer's private key. This line is just |
Rich Salz | 9e9858d | 2015-02-06 10:52:12 -0500 | [diff] [blame] | 613 | * here as a reminder that we're still using a not-quite-appropriate |
| 614 | * data structure. |
| 615 | */ |
Rich Salz | 25aaa98 | 2015-05-01 14:37:16 -0400 | [diff] [blame] | 616 | EVP_PKEY_free(sc->peer_pkeys[i].privatekey); |
Bodo Möller | b56bce4 | 1999-05-13 15:09:38 +0000 | [diff] [blame] | 617 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 618 | } |
Bodo Möller | b56bce4 | 1999-05-13 15:09:38 +0000 | [diff] [blame] | 619 | |
Richard Levitte | bc36ee6 | 2001-02-20 08:13:47 +0000 | [diff] [blame] | 620 | #ifndef OPENSSL_NO_RSA |
Rich Salz | d640708 | 2015-03-24 10:17:37 -0400 | [diff] [blame] | 621 | RSA_free(sc->peer_rsa_tmp); |
Bodo Möller | b56bce4 | 1999-05-13 15:09:38 +0000 | [diff] [blame] | 622 | #endif |
Richard Levitte | bc36ee6 | 2001-02-20 08:13:47 +0000 | [diff] [blame] | 623 | #ifndef OPENSSL_NO_DH |
Rich Salz | d640708 | 2015-03-24 10:17:37 -0400 | [diff] [blame] | 624 | DH_free(sc->peer_dh_tmp); |
Bodo Möller | b56bce4 | 1999-05-13 15:09:38 +0000 | [diff] [blame] | 625 | #endif |
Rich Salz | 10bf4fc | 2015-03-10 19:09:27 -0400 | [diff] [blame] | 626 | #ifndef OPENSSL_NO_EC |
Rich Salz | 8fdc373 | 2015-03-25 18:35:24 -0400 | [diff] [blame] | 627 | EC_KEY_free(sc->peer_ecdh_tmp); |
Bodo Möller | ea26226 | 2002-08-09 08:56:08 +0000 | [diff] [blame] | 628 | #endif |
Bodo Möller | b56bce4 | 1999-05-13 15:09:38 +0000 | [diff] [blame] | 629 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 630 | OPENSSL_free(sc); |
| 631 | } |
Bodo Möller | b56bce4 | 1999-05-13 15:09:38 +0000 | [diff] [blame] | 632 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 633 | int ssl_set_peer_cert_type(SESS_CERT *sc, int type) |
| 634 | { |
| 635 | sc->peer_cert_type = type; |
| 636 | return (1); |
| 637 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 638 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 639 | int ssl_verify_cert_chain(SSL *s, STACK_OF(X509) *sk) |
| 640 | { |
| 641 | X509 *x; |
| 642 | int i; |
| 643 | X509_STORE *verify_store; |
| 644 | X509_STORE_CTX ctx; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 645 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 646 | if (s->cert->verify_store) |
| 647 | verify_store = s->cert->verify_store; |
| 648 | else |
| 649 | verify_store = s->ctx->cert_store; |
Dr. Stephen Henson | 74ecfab | 2012-07-23 23:34:28 +0000 | [diff] [blame] | 650 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 651 | if ((sk == NULL) || (sk_X509_num(sk) == 0)) |
| 652 | return (0); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 653 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 654 | x = sk_X509_value(sk, 0); |
| 655 | if (!X509_STORE_CTX_init(&ctx, verify_store, x, sk)) { |
| 656 | SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN, ERR_R_X509_LIB); |
| 657 | return (0); |
| 658 | } |
| 659 | /* Set suite B flags if needed */ |
| 660 | X509_STORE_CTX_set_flags(&ctx, tls1_suiteb(s)); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 661 | X509_STORE_CTX_set_ex_data(&ctx, SSL_get_ex_data_X509_STORE_CTX_idx(), s); |
Bodo Möller | 3ac82fa | 2000-12-15 16:40:35 +0000 | [diff] [blame] | 662 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 663 | /* |
| 664 | * We need to inherit the verify parameters. These can be determined by |
| 665 | * the context: if its a server it will verify SSL client certificates or |
| 666 | * vice versa. |
| 667 | */ |
Dr. Stephen Henson | bb7cd4e | 1999-11-29 22:35:00 +0000 | [diff] [blame] | 668 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 669 | X509_STORE_CTX_set_default(&ctx, s->server ? "ssl_client" : "ssl_server"); |
| 670 | /* |
| 671 | * Anything non-default in "param" should overwrite anything in the ctx. |
| 672 | */ |
| 673 | X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(&ctx), s->param); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 674 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 675 | if (s->verify_callback) |
| 676 | X509_STORE_CTX_set_verify_cb(&ctx, s->verify_callback); |
Lutz Jänicke | 1f0c9ad | 2001-07-30 11:45:34 +0000 | [diff] [blame] | 677 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 678 | if (s->ctx->app_verify_callback != NULL) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 679 | i = s->ctx->app_verify_callback(&ctx, s->ctx->app_verify_arg); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 680 | else { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 681 | i = X509_verify_cert(&ctx); |
| 682 | # if 0 |
| 683 | /* Dummy error calls so mkerr generates them */ |
| 684 | SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN, SSL_R_EE_KEY_TOO_SMALL); |
| 685 | SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN, SSL_R_CA_KEY_TOO_SMALL); |
| 686 | SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN, SSL_R_CA_MD_TOO_WEAK); |
| 687 | # endif |
| 688 | if (i > 0) |
| 689 | i = ssl_security_cert_chain(s, ctx.chain, NULL, 1); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 690 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 691 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 692 | s->verify_result = ctx.error; |
| 693 | X509_STORE_CTX_cleanup(&ctx); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 694 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 695 | return (i); |
| 696 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 697 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 698 | static void set_client_CA_list(STACK_OF(X509_NAME) **ca_list, |
| 699 | STACK_OF(X509_NAME) *name_list) |
| 700 | { |
Rich Salz | 222561f | 2015-04-30 17:33:59 -0400 | [diff] [blame] | 701 | sk_X509_NAME_pop_free(*ca_list, X509_NAME_free); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 702 | *ca_list = name_list; |
| 703 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 704 | |
Ben Laurie | 838d25a | 1999-05-30 14:13:19 +0000 | [diff] [blame] | 705 | STACK_OF(X509_NAME) *SSL_dup_CA_list(STACK_OF(X509_NAME) *sk) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 706 | { |
| 707 | int i; |
| 708 | STACK_OF(X509_NAME) *ret; |
| 709 | X509_NAME *name; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 710 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 711 | ret = sk_X509_NAME_new_null(); |
| 712 | for (i = 0; i < sk_X509_NAME_num(sk); i++) { |
| 713 | name = X509_NAME_dup(sk_X509_NAME_value(sk, i)); |
| 714 | if ((name == NULL) || !sk_X509_NAME_push(ret, name)) { |
| 715 | sk_X509_NAME_pop_free(ret, X509_NAME_free); |
| 716 | return (NULL); |
| 717 | } |
| 718 | } |
| 719 | return (ret); |
| 720 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 721 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 722 | void SSL_set_client_CA_list(SSL *s, STACK_OF(X509_NAME) *name_list) |
| 723 | { |
| 724 | set_client_CA_list(&(s->client_CA), name_list); |
| 725 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 726 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 727 | void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list) |
| 728 | { |
| 729 | set_client_CA_list(&(ctx->client_CA), name_list); |
| 730 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 731 | |
Ben Laurie | 0821bcd | 2005-03-30 10:26:02 +0000 | [diff] [blame] | 732 | STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 733 | { |
| 734 | return (ctx->client_CA); |
| 735 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 736 | |
Ben Laurie | 0821bcd | 2005-03-30 10:26:02 +0000 | [diff] [blame] | 737 | STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 738 | { |
| 739 | if (s->type == SSL_ST_CONNECT) { /* we are in the client */ |
| 740 | if (((s->version >> 8) == SSL3_VERSION_MAJOR) && (s->s3 != NULL)) |
| 741 | return (s->s3->tmp.ca_names); |
| 742 | else |
| 743 | return (NULL); |
| 744 | } else { |
| 745 | if (s->client_CA != NULL) |
| 746 | return (s->client_CA); |
| 747 | else |
| 748 | return (s->ctx->client_CA); |
| 749 | } |
| 750 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 751 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 752 | static int add_client_CA(STACK_OF(X509_NAME) **sk, X509 *x) |
| 753 | { |
| 754 | X509_NAME *name; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 755 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 756 | if (x == NULL) |
| 757 | return (0); |
| 758 | if ((*sk == NULL) && ((*sk = sk_X509_NAME_new_null()) == NULL)) |
| 759 | return (0); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 760 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 761 | if ((name = X509_NAME_dup(X509_get_subject_name(x))) == NULL) |
| 762 | return (0); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 763 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 764 | if (!sk_X509_NAME_push(*sk, name)) { |
| 765 | X509_NAME_free(name); |
| 766 | return (0); |
| 767 | } |
| 768 | return (1); |
| 769 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 770 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 771 | int SSL_add_client_CA(SSL *ssl, X509 *x) |
| 772 | { |
| 773 | return (add_client_CA(&(ssl->client_CA), x)); |
| 774 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 775 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 776 | int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x) |
| 777 | { |
| 778 | return (add_client_CA(&(ctx->client_CA), x)); |
| 779 | } |
| 780 | |
| 781 | static int xname_cmp(const X509_NAME *const *a, const X509_NAME *const *b) |
| 782 | { |
| 783 | return (X509_NAME_cmp(*a, *b)); |
| 784 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 785 | |
Richard Levitte | bc36ee6 | 2001-02-20 08:13:47 +0000 | [diff] [blame] | 786 | #ifndef OPENSSL_NO_STDIO |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 787 | /** |
Ben Laurie | eb90a48 | 1999-02-28 17:41:55 +0000 | [diff] [blame] | 788 | * Load CA certs from a file into a ::STACK. Note that it is somewhat misnamed; |
| 789 | * it doesn't really have anything to do with clients (except that a common use |
| 790 | * for a stack of CAs is to send it to the client). Actually, it doesn't have |
| 791 | * much to do with CAs, either, since it will load any old cert. |
| 792 | * \param file the file containing one or more certs. |
| 793 | * \return a ::STACK containing the certs. |
| 794 | */ |
Ben Laurie | f73e07c | 1999-04-12 17:23:57 +0000 | [diff] [blame] | 795 | STACK_OF(X509_NAME) *SSL_load_client_CA_file(const char *file) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 796 | { |
| 797 | BIO *in; |
| 798 | X509 *x = NULL; |
| 799 | X509_NAME *xn = NULL; |
| 800 | STACK_OF(X509_NAME) *ret = NULL, *sk; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 801 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 802 | sk = sk_X509_NAME_new(xname_cmp); |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 803 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 804 | in = BIO_new(BIO_s_file_internal()); |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 805 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 806 | if ((sk == NULL) || (in == NULL)) { |
| 807 | SSLerr(SSL_F_SSL_LOAD_CLIENT_CA_FILE, ERR_R_MALLOC_FAILURE); |
| 808 | goto err; |
| 809 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 810 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 811 | if (!BIO_read_filename(in, file)) |
| 812 | goto err; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 813 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 814 | for (;;) { |
| 815 | if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL) |
| 816 | break; |
| 817 | if (ret == NULL) { |
| 818 | ret = sk_X509_NAME_new_null(); |
| 819 | if (ret == NULL) { |
| 820 | SSLerr(SSL_F_SSL_LOAD_CLIENT_CA_FILE, ERR_R_MALLOC_FAILURE); |
| 821 | goto err; |
| 822 | } |
| 823 | } |
| 824 | if ((xn = X509_get_subject_name(x)) == NULL) |
| 825 | goto err; |
| 826 | /* check for duplicates */ |
| 827 | xn = X509_NAME_dup(xn); |
| 828 | if (xn == NULL) |
| 829 | goto err; |
| 830 | if (sk_X509_NAME_find(sk, xn) >= 0) |
| 831 | X509_NAME_free(xn); |
| 832 | else { |
| 833 | sk_X509_NAME_push(sk, xn); |
| 834 | sk_X509_NAME_push(ret, xn); |
| 835 | } |
| 836 | } |
Rich Salz | 6669647 | 2015-05-01 14:29:48 -0400 | [diff] [blame] | 837 | goto done; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 838 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 839 | err: |
Rich Salz | 6669647 | 2015-05-01 14:29:48 -0400 | [diff] [blame] | 840 | sk_X509_NAME_pop_free(ret, X509_NAME_free); |
| 841 | ret = NULL; |
| 842 | done: |
Rich Salz | 222561f | 2015-04-30 17:33:59 -0400 | [diff] [blame] | 843 | sk_X509_NAME_free(sk); |
Rich Salz | ca3a82c | 2015-03-25 11:31:18 -0400 | [diff] [blame] | 844 | BIO_free(in); |
Rich Salz | 222561f | 2015-04-30 17:33:59 -0400 | [diff] [blame] | 845 | X509_free(x); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 846 | if (ret != NULL) |
| 847 | ERR_clear_error(); |
| 848 | return (ret); |
| 849 | } |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 850 | #endif |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 851 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 852 | /** |
Ben Laurie | eb90a48 | 1999-02-28 17:41:55 +0000 | [diff] [blame] | 853 | * Add a file of certs to a stack. |
| 854 | * \param stack the stack to add to. |
| 855 | * \param file the file to add from. All certs in this file that are not |
| 856 | * already in the stack will be added. |
| 857 | * \return 1 for success, 0 for failure. Note that in the case of failure some |
| 858 | * certs may have been added to \c stack. |
| 859 | */ |
| 860 | |
Ben Laurie | 661b361 | 1999-05-03 19:55:00 +0000 | [diff] [blame] | 861 | int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 862 | const char *file) |
| 863 | { |
| 864 | BIO *in; |
| 865 | X509 *x = NULL; |
| 866 | X509_NAME *xn = NULL; |
| 867 | int ret = 1; |
| 868 | int (*oldcmp) (const X509_NAME *const *a, const X509_NAME *const *b); |
Ben Laurie | eb90a48 | 1999-02-28 17:41:55 +0000 | [diff] [blame] | 869 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 870 | oldcmp = sk_X509_NAME_set_cmp_func(stack, xname_cmp); |
Dr. Stephen Henson | a3a06e6 | 2010-03-24 23:17:15 +0000 | [diff] [blame] | 871 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 872 | in = BIO_new(BIO_s_file_internal()); |
Bodo Möller | 2ea0910 | 2000-05-21 14:14:30 +0000 | [diff] [blame] | 873 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 874 | if (in == NULL) { |
| 875 | SSLerr(SSL_F_SSL_ADD_FILE_CERT_SUBJECTS_TO_STACK, |
| 876 | ERR_R_MALLOC_FAILURE); |
| 877 | goto err; |
| 878 | } |
Ben Laurie | eb90a48 | 1999-02-28 17:41:55 +0000 | [diff] [blame] | 879 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 880 | if (!BIO_read_filename(in, file)) |
| 881 | goto err; |
| 882 | |
| 883 | for (;;) { |
| 884 | if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL) |
| 885 | break; |
| 886 | if ((xn = X509_get_subject_name(x)) == NULL) |
| 887 | goto err; |
| 888 | xn = X509_NAME_dup(xn); |
| 889 | if (xn == NULL) |
| 890 | goto err; |
| 891 | if (sk_X509_NAME_find(stack, xn) >= 0) |
| 892 | X509_NAME_free(xn); |
| 893 | else |
| 894 | sk_X509_NAME_push(stack, xn); |
| 895 | } |
| 896 | |
| 897 | ERR_clear_error(); |
Rich Salz | 6669647 | 2015-05-01 14:29:48 -0400 | [diff] [blame] | 898 | goto done; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 899 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 900 | err: |
| 901 | ret = 0; |
Rich Salz | 6669647 | 2015-05-01 14:29:48 -0400 | [diff] [blame] | 902 | done: |
Rich Salz | ca3a82c | 2015-03-25 11:31:18 -0400 | [diff] [blame] | 903 | BIO_free(in); |
Rich Salz | 25aaa98 | 2015-05-01 14:37:16 -0400 | [diff] [blame] | 904 | X509_free(x); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 905 | (void)sk_X509_NAME_set_cmp_func(stack, oldcmp); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 906 | return ret; |
| 907 | } |
| 908 | |
| 909 | /** |
Ben Laurie | eb90a48 | 1999-02-28 17:41:55 +0000 | [diff] [blame] | 910 | * Add a directory of certs to a stack. |
| 911 | * \param stack the stack to append to. |
| 912 | * \param dir the directory to append from. All files in this directory will be |
| 913 | * examined as potential certs. Any that are acceptable to |
Ralf S. Engelschall | 72e442a | 1999-03-22 15:50:34 +0000 | [diff] [blame] | 914 | * SSL_add_dir_cert_subjects_to_stack() that are not already in the stack will be |
Ben Laurie | eb90a48 | 1999-02-28 17:41:55 +0000 | [diff] [blame] | 915 | * included. |
| 916 | * \return 1 for success, 0 for failure. Note that in the case of failure some |
| 917 | * certs may have been added to \c stack. |
| 918 | */ |
| 919 | |
Ben Laurie | 661b361 | 1999-05-03 19:55:00 +0000 | [diff] [blame] | 920 | int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 921 | const char *dir) |
| 922 | { |
| 923 | OPENSSL_DIR_CTX *d = NULL; |
| 924 | const char *filename; |
| 925 | int ret = 0; |
Ben Laurie | eb90a48 | 1999-02-28 17:41:55 +0000 | [diff] [blame] | 926 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 927 | CRYPTO_w_lock(CRYPTO_LOCK_READDIR); |
Ben Laurie | eb90a48 | 1999-02-28 17:41:55 +0000 | [diff] [blame] | 928 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 929 | /* Note that a side effect is that the CAs will be sorted by name */ |
Richard Levitte | 4083a22 | 2004-07-10 13:17:16 +0000 | [diff] [blame] | 930 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 931 | while ((filename = OPENSSL_DIR_read(&d, dir))) { |
| 932 | char buf[1024]; |
| 933 | int r; |
Richard Levitte | 4083a22 | 2004-07-10 13:17:16 +0000 | [diff] [blame] | 934 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 935 | if (strlen(dir) + strlen(filename) + 2 > sizeof buf) { |
| 936 | SSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK, |
| 937 | SSL_R_PATH_TOO_LONG); |
| 938 | goto err; |
| 939 | } |
Richard Levitte | 4083a22 | 2004-07-10 13:17:16 +0000 | [diff] [blame] | 940 | #ifdef OPENSSL_SYS_VMS |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 941 | r = BIO_snprintf(buf, sizeof buf, "%s%s", dir, filename); |
Richard Levitte | 4083a22 | 2004-07-10 13:17:16 +0000 | [diff] [blame] | 942 | #else |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 943 | r = BIO_snprintf(buf, sizeof buf, "%s/%s", dir, filename); |
Richard Levitte | 4083a22 | 2004-07-10 13:17:16 +0000 | [diff] [blame] | 944 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 945 | if (r <= 0 || r >= (int)sizeof(buf)) |
| 946 | goto err; |
| 947 | if (!SSL_add_file_cert_subjects_to_stack(stack, buf)) |
| 948 | goto err; |
| 949 | } |
Ben Laurie | eb90a48 | 1999-02-28 17:41:55 +0000 | [diff] [blame] | 950 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 951 | if (errno) { |
| 952 | SYSerr(SYS_F_OPENDIR, get_last_sys_error()); |
| 953 | ERR_add_error_data(3, "OPENSSL_DIR_read(&ctx, '", dir, "')"); |
| 954 | SSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK, ERR_R_SYS_LIB); |
| 955 | goto err; |
| 956 | } |
Richard Levitte | 4083a22 | 2004-07-10 13:17:16 +0000 | [diff] [blame] | 957 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 958 | ret = 1; |
Richard Levitte | 285046e | 2001-10-04 12:27:39 +0000 | [diff] [blame] | 959 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 960 | err: |
| 961 | if (d) |
| 962 | OPENSSL_DIR_end(&d); |
| 963 | CRYPTO_w_unlock(CRYPTO_LOCK_READDIR); |
| 964 | return ret; |
| 965 | } |
Richard Levitte | 285046e | 2001-10-04 12:27:39 +0000 | [diff] [blame] | 966 | |
Dr. Stephen Henson | 4379d0e | 2012-01-26 15:47:32 +0000 | [diff] [blame] | 967 | /* Add a certificate to a BUF_MEM structure */ |
| 968 | |
| 969 | static int ssl_add_cert_to_buf(BUF_MEM *buf, unsigned long *l, X509 *x) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 970 | { |
| 971 | int n; |
| 972 | unsigned char *p; |
Dr. Stephen Henson | 4379d0e | 2012-01-26 15:47:32 +0000 | [diff] [blame] | 973 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 974 | n = i2d_X509(x, NULL); |
| 975 | if (!BUF_MEM_grow_clean(buf, (int)(n + (*l) + 3))) { |
| 976 | SSLerr(SSL_F_SSL_ADD_CERT_TO_BUF, ERR_R_BUF_LIB); |
| 977 | return 0; |
| 978 | } |
| 979 | p = (unsigned char *)&(buf->data[*l]); |
| 980 | l2n3(n, p); |
| 981 | i2d_X509(x, &p); |
| 982 | *l += n + 3; |
Dr. Stephen Henson | 4379d0e | 2012-01-26 15:47:32 +0000 | [diff] [blame] | 983 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 984 | return 1; |
| 985 | } |
Dr. Stephen Henson | 4379d0e | 2012-01-26 15:47:32 +0000 | [diff] [blame] | 986 | |
| 987 | /* Add certificate chain to internal SSL BUF_MEM strcuture */ |
Dr. Stephen Henson | c526ed4 | 2012-01-26 16:00:34 +0000 | [diff] [blame] | 988 | int ssl_add_cert_chain(SSL *s, CERT_PKEY *cpk, unsigned long *l) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 989 | { |
| 990 | BUF_MEM *buf = s->init_buf; |
| 991 | int i; |
Dr. Stephen Henson | 4379d0e | 2012-01-26 15:47:32 +0000 | [diff] [blame] | 992 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 993 | X509 *x; |
| 994 | STACK_OF(X509) *extra_certs; |
| 995 | X509_STORE *chain_store; |
Dr. Stephen Henson | c526ed4 | 2012-01-26 16:00:34 +0000 | [diff] [blame] | 996 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 997 | /* TLSv1 sends a chain with nothing in it, instead of an alert */ |
| 998 | if (!BUF_MEM_grow_clean(buf, 10)) { |
| 999 | SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, ERR_R_BUF_LIB); |
| 1000 | return 0; |
| 1001 | } |
Dr. Stephen Henson | d628885 | 2014-03-09 16:12:20 +0000 | [diff] [blame] | 1002 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1003 | if (!cpk || !cpk->x509) |
| 1004 | return 1; |
Dr. Stephen Henson | d628885 | 2014-03-09 16:12:20 +0000 | [diff] [blame] | 1005 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1006 | x = cpk->x509; |
Dr. Stephen Henson | d628885 | 2014-03-09 16:12:20 +0000 | [diff] [blame] | 1007 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1008 | /* |
| 1009 | * If we have a certificate specific chain use it, else use parent ctx. |
| 1010 | */ |
| 1011 | if (cpk->chain) |
| 1012 | extra_certs = cpk->chain; |
| 1013 | else |
| 1014 | extra_certs = s->ctx->extra_certs; |
Dr. Stephen Henson | d628885 | 2014-03-09 16:12:20 +0000 | [diff] [blame] | 1015 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1016 | if ((s->mode & SSL_MODE_NO_AUTO_CHAIN) || extra_certs) |
| 1017 | chain_store = NULL; |
| 1018 | else if (s->cert->chain_store) |
| 1019 | chain_store = s->cert->chain_store; |
| 1020 | else |
| 1021 | chain_store = s->ctx->cert_store; |
Dr. Stephen Henson | d628885 | 2014-03-09 16:12:20 +0000 | [diff] [blame] | 1022 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1023 | if (chain_store) { |
| 1024 | X509_STORE_CTX xs_ctx; |
Dr. Stephen Henson | d628885 | 2014-03-09 16:12:20 +0000 | [diff] [blame] | 1025 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1026 | if (!X509_STORE_CTX_init(&xs_ctx, chain_store, x, NULL)) { |
| 1027 | SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, ERR_R_X509_LIB); |
| 1028 | return (0); |
| 1029 | } |
| 1030 | X509_verify_cert(&xs_ctx); |
| 1031 | /* Don't leave errors in the queue */ |
| 1032 | ERR_clear_error(); |
| 1033 | i = ssl_security_cert_chain(s, xs_ctx.chain, NULL, 0); |
| 1034 | if (i != 1) { |
| 1035 | X509_STORE_CTX_cleanup(&xs_ctx); |
| 1036 | SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, i); |
| 1037 | return 0; |
| 1038 | } |
| 1039 | for (i = 0; i < sk_X509_num(xs_ctx.chain); i++) { |
| 1040 | x = sk_X509_value(xs_ctx.chain, i); |
Dr. Stephen Henson | d628885 | 2014-03-09 16:12:20 +0000 | [diff] [blame] | 1041 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1042 | if (!ssl_add_cert_to_buf(buf, l, x)) { |
| 1043 | X509_STORE_CTX_cleanup(&xs_ctx); |
| 1044 | return 0; |
| 1045 | } |
| 1046 | } |
| 1047 | X509_STORE_CTX_cleanup(&xs_ctx); |
| 1048 | } else { |
| 1049 | i = ssl_security_cert_chain(s, extra_certs, x, 0); |
| 1050 | if (i != 1) { |
| 1051 | SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, i); |
| 1052 | return 0; |
| 1053 | } |
| 1054 | if (!ssl_add_cert_to_buf(buf, l, x)) |
| 1055 | return 0; |
| 1056 | for (i = 0; i < sk_X509_num(extra_certs); i++) { |
| 1057 | x = sk_X509_value(extra_certs, i); |
| 1058 | if (!ssl_add_cert_to_buf(buf, l, x)) |
| 1059 | return 0; |
| 1060 | } |
| 1061 | } |
| 1062 | return 1; |
| 1063 | } |
Dr. Stephen Henson | 4379d0e | 2012-01-26 15:47:32 +0000 | [diff] [blame] | 1064 | |
Dr. Stephen Henson | 74ecfab | 2012-07-23 23:34:28 +0000 | [diff] [blame] | 1065 | /* Build a certificate chain for current certificate */ |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 1066 | int ssl_build_cert_chain(SSL *s, SSL_CTX *ctx, int flags) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1067 | { |
| 1068 | CERT *c = s ? s->cert : ctx->cert; |
| 1069 | CERT_PKEY *cpk = c->key; |
| 1070 | X509_STORE *chain_store = NULL; |
| 1071 | X509_STORE_CTX xs_ctx; |
| 1072 | STACK_OF(X509) *chain = NULL, *untrusted = NULL; |
| 1073 | X509 *x; |
| 1074 | int i, rv = 0; |
| 1075 | unsigned long error; |
Dr. Stephen Henson | 74ecfab | 2012-07-23 23:34:28 +0000 | [diff] [blame] | 1076 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1077 | if (!cpk->x509) { |
| 1078 | SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, SSL_R_NO_CERTIFICATE_SET); |
| 1079 | goto err; |
| 1080 | } |
| 1081 | /* Rearranging and check the chain: add everything to a store */ |
| 1082 | if (flags & SSL_BUILD_CHAIN_FLAG_CHECK) { |
| 1083 | chain_store = X509_STORE_new(); |
| 1084 | if (!chain_store) |
| 1085 | goto err; |
| 1086 | for (i = 0; i < sk_X509_num(cpk->chain); i++) { |
| 1087 | x = sk_X509_value(cpk->chain, i); |
| 1088 | if (!X509_STORE_add_cert(chain_store, x)) { |
| 1089 | error = ERR_peek_last_error(); |
| 1090 | if (ERR_GET_LIB(error) != ERR_LIB_X509 || |
| 1091 | ERR_GET_REASON(error) != |
| 1092 | X509_R_CERT_ALREADY_IN_HASH_TABLE) |
| 1093 | goto err; |
| 1094 | ERR_clear_error(); |
| 1095 | } |
| 1096 | } |
| 1097 | /* Add EE cert too: it might be self signed */ |
| 1098 | if (!X509_STORE_add_cert(chain_store, cpk->x509)) { |
| 1099 | error = ERR_peek_last_error(); |
| 1100 | if (ERR_GET_LIB(error) != ERR_LIB_X509 || |
| 1101 | ERR_GET_REASON(error) != X509_R_CERT_ALREADY_IN_HASH_TABLE) |
| 1102 | goto err; |
| 1103 | ERR_clear_error(); |
| 1104 | } |
| 1105 | } else { |
| 1106 | if (c->chain_store) |
| 1107 | chain_store = c->chain_store; |
| 1108 | else if (s) |
| 1109 | chain_store = s->ctx->cert_store; |
| 1110 | else |
| 1111 | chain_store = ctx->cert_store; |
Dr. Stephen Henson | 74ecfab | 2012-07-23 23:34:28 +0000 | [diff] [blame] | 1112 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1113 | if (flags & SSL_BUILD_CHAIN_FLAG_UNTRUSTED) |
| 1114 | untrusted = cpk->chain; |
| 1115 | } |
Dr. Stephen Henson | 74ecfab | 2012-07-23 23:34:28 +0000 | [diff] [blame] | 1116 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1117 | if (!X509_STORE_CTX_init(&xs_ctx, chain_store, cpk->x509, untrusted)) { |
| 1118 | SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, ERR_R_X509_LIB); |
| 1119 | goto err; |
| 1120 | } |
| 1121 | /* Set suite B flags if needed */ |
| 1122 | X509_STORE_CTX_set_flags(&xs_ctx, |
| 1123 | c->cert_flags & SSL_CERT_FLAG_SUITEB_128_LOS); |
Dr. Stephen Henson | 74ecfab | 2012-07-23 23:34:28 +0000 | [diff] [blame] | 1124 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1125 | i = X509_verify_cert(&xs_ctx); |
| 1126 | if (i <= 0 && flags & SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR) { |
| 1127 | if (flags & SSL_BUILD_CHAIN_FLAG_CLEAR_ERROR) |
| 1128 | ERR_clear_error(); |
| 1129 | i = 1; |
| 1130 | rv = 2; |
| 1131 | } |
| 1132 | if (i > 0) |
| 1133 | chain = X509_STORE_CTX_get1_chain(&xs_ctx); |
| 1134 | if (i <= 0) { |
| 1135 | SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, SSL_R_CERTIFICATE_VERIFY_FAILED); |
| 1136 | i = X509_STORE_CTX_get_error(&xs_ctx); |
| 1137 | ERR_add_error_data(2, "Verify error:", |
| 1138 | X509_verify_cert_error_string(i)); |
Dr. Stephen Henson | 13dc3ce | 2014-02-23 12:00:18 +0000 | [diff] [blame] | 1139 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1140 | X509_STORE_CTX_cleanup(&xs_ctx); |
| 1141 | goto err; |
| 1142 | } |
| 1143 | X509_STORE_CTX_cleanup(&xs_ctx); |
| 1144 | /* Remove EE certificate from chain */ |
| 1145 | x = sk_X509_shift(chain); |
| 1146 | X509_free(x); |
| 1147 | if (flags & SSL_BUILD_CHAIN_FLAG_NO_ROOT) { |
| 1148 | if (sk_X509_num(chain) > 0) { |
| 1149 | /* See if last cert is self signed */ |
| 1150 | x = sk_X509_value(chain, sk_X509_num(chain) - 1); |
| 1151 | X509_check_purpose(x, -1, 0); |
| 1152 | if (x->ex_flags & EXFLAG_SS) { |
| 1153 | x = sk_X509_pop(chain); |
| 1154 | X509_free(x); |
| 1155 | } |
| 1156 | } |
| 1157 | } |
| 1158 | /* |
| 1159 | * Check security level of all CA certificates: EE will have been checked |
| 1160 | * already. |
| 1161 | */ |
| 1162 | for (i = 0; i < sk_X509_num(chain); i++) { |
| 1163 | x = sk_X509_value(chain, i); |
| 1164 | rv = ssl_security_cert(s, ctx, x, 0, 0); |
| 1165 | if (rv != 1) { |
| 1166 | SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, rv); |
| 1167 | sk_X509_pop_free(chain, X509_free); |
| 1168 | rv = 0; |
| 1169 | goto err; |
| 1170 | } |
| 1171 | } |
Rich Salz | 222561f | 2015-04-30 17:33:59 -0400 | [diff] [blame] | 1172 | sk_X509_pop_free(cpk->chain, X509_free); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1173 | cpk->chain = chain; |
| 1174 | if (rv == 0) |
| 1175 | rv = 1; |
| 1176 | err: |
| 1177 | if (flags & SSL_BUILD_CHAIN_FLAG_CHECK) |
| 1178 | X509_STORE_free(chain_store); |
Dr. Stephen Henson | 74ecfab | 2012-07-23 23:34:28 +0000 | [diff] [blame] | 1179 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1180 | return rv; |
| 1181 | } |
Dr. Stephen Henson | 74ecfab | 2012-07-23 23:34:28 +0000 | [diff] [blame] | 1182 | |
| 1183 | int ssl_cert_set_cert_store(CERT *c, X509_STORE *store, int chain, int ref) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1184 | { |
| 1185 | X509_STORE **pstore; |
| 1186 | if (chain) |
| 1187 | pstore = &c->chain_store; |
| 1188 | else |
| 1189 | pstore = &c->verify_store; |
Rich Salz | 222561f | 2015-04-30 17:33:59 -0400 | [diff] [blame] | 1190 | X509_STORE_free(*pstore); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1191 | *pstore = store; |
| 1192 | if (ref && store) |
| 1193 | CRYPTO_add(&store->references, 1, CRYPTO_LOCK_X509_STORE); |
| 1194 | return 1; |
| 1195 | } |
Dr. Stephen Henson | 74ecfab | 2012-07-23 23:34:28 +0000 | [diff] [blame] | 1196 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1197 | static int ssl_security_default_callback(SSL *s, SSL_CTX *ctx, int op, |
| 1198 | int bits, int nid, void *other, |
| 1199 | void *ex) |
| 1200 | { |
| 1201 | int level, minbits; |
| 1202 | static const int minbits_table[5] = { 80, 112, 128, 192, 256 }; |
| 1203 | if (ctx) |
| 1204 | level = SSL_CTX_get_security_level(ctx); |
| 1205 | else |
| 1206 | level = SSL_get_security_level(s); |
| 1207 | /* Level 0: anything goes */ |
| 1208 | if (level <= 0) |
| 1209 | return 1; |
| 1210 | if (level > 5) |
| 1211 | level = 5; |
| 1212 | minbits = minbits_table[level - 1]; |
| 1213 | switch (op) { |
| 1214 | case SSL_SECOP_CIPHER_SUPPORTED: |
| 1215 | case SSL_SECOP_CIPHER_SHARED: |
| 1216 | case SSL_SECOP_CIPHER_CHECK: |
| 1217 | { |
| 1218 | const SSL_CIPHER *c = other; |
| 1219 | /* No ciphers below security level */ |
| 1220 | if (bits < minbits) |
| 1221 | return 0; |
| 1222 | /* No unauthenticated ciphersuites */ |
| 1223 | if (c->algorithm_auth & SSL_aNULL) |
| 1224 | return 0; |
| 1225 | /* No MD5 mac ciphersuites */ |
| 1226 | if (c->algorithm_mac & SSL_MD5) |
| 1227 | return 0; |
| 1228 | /* SHA1 HMAC is 160 bits of security */ |
| 1229 | if (minbits > 160 && c->algorithm_mac & SSL_SHA1) |
| 1230 | return 0; |
| 1231 | /* Level 2: no RC4 */ |
| 1232 | if (level >= 2 && c->algorithm_enc == SSL_RC4) |
| 1233 | return 0; |
| 1234 | /* Level 3: forward secure ciphersuites only */ |
| 1235 | if (level >= 3 && !(c->algorithm_mkey & (SSL_kEDH | SSL_kEECDH))) |
| 1236 | return 0; |
| 1237 | break; |
| 1238 | } |
| 1239 | case SSL_SECOP_VERSION: |
| 1240 | /* SSLv3 not allowed on level 2 */ |
| 1241 | if (nid <= SSL3_VERSION && level >= 2) |
| 1242 | return 0; |
| 1243 | /* TLS v1.1 and above only for level 3 */ |
| 1244 | if (nid <= TLS1_VERSION && level >= 3) |
| 1245 | return 0; |
| 1246 | /* TLS v1.2 only for level 4 and above */ |
| 1247 | if (nid <= TLS1_1_VERSION && level >= 4) |
| 1248 | return 0; |
| 1249 | break; |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 1250 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1251 | case SSL_SECOP_COMPRESSION: |
| 1252 | if (level >= 2) |
| 1253 | return 0; |
| 1254 | break; |
| 1255 | case SSL_SECOP_TICKET: |
| 1256 | if (level >= 3) |
| 1257 | return 0; |
| 1258 | break; |
| 1259 | default: |
| 1260 | if (bits < minbits) |
| 1261 | return 0; |
| 1262 | } |
| 1263 | return 1; |
| 1264 | } |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 1265 | |
| 1266 | int ssl_security(SSL *s, int op, int bits, int nid, void *other) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1267 | { |
| 1268 | return s->cert->sec_cb(s, NULL, op, bits, nid, other, s->cert->sec_ex); |
| 1269 | } |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 1270 | |
| 1271 | int ssl_ctx_security(SSL_CTX *ctx, int op, int bits, int nid, void *other) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1272 | { |
| 1273 | return ctx->cert->sec_cb(NULL, ctx, op, bits, nid, other, |
| 1274 | ctx->cert->sec_ex); |
| 1275 | } |