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 | 6857079 | 2015-05-14 14:54:49 +0200 | [diff] [blame] | 126 | #include "internal/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 | |
Ulf Möller | 6b691a5 | 1999-04-19 21:31:43 +0000 | [diff] [blame] | 168 | CERT *ssl_cert_new(void) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 169 | { |
Rich Salz | b51bce9 | 2015-08-25 13:25:58 -0400 | [diff] [blame] | 170 | CERT *ret = OPENSSL_zalloc(sizeof(*ret)); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 171 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 172 | if (ret == NULL) { |
| 173 | SSLerr(SSL_F_SSL_CERT_NEW, ERR_R_MALLOC_FAILURE); |
| 174 | return (NULL); |
| 175 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 176 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 177 | ret->key = &(ret->pkeys[SSL_PKEY_RSA_ENC]); |
| 178 | ret->references = 1; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 179 | ret->sec_cb = ssl_security_default_callback; |
| 180 | ret->sec_level = OPENSSL_TLS_SECURITY_LEVEL; |
| 181 | ret->sec_ex = NULL; |
| 182 | return (ret); |
| 183 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 184 | |
Bodo Möller | ca8e5b9 | 1999-05-09 20:12:44 +0000 | [diff] [blame] | 185 | CERT *ssl_cert_dup(CERT *cert) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 186 | { |
Rich Salz | b51bce9 | 2015-08-25 13:25:58 -0400 | [diff] [blame] | 187 | CERT *ret = OPENSSL_zalloc(sizeof(*ret)); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 188 | int i; |
Bodo Möller | ca8e5b9 | 1999-05-09 20:12:44 +0000 | [diff] [blame] | 189 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 190 | if (ret == NULL) { |
| 191 | SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_MALLOC_FAILURE); |
| 192 | return (NULL); |
| 193 | } |
Bodo Möller | ca8e5b9 | 1999-05-09 20:12:44 +0000 | [diff] [blame] | 194 | |
Filipe DA SILVA | 0e04674 | 2015-09-16 22:25:31 -0400 | [diff] [blame] | 195 | ret->references = 1; |
Rich Salz | 16f8d4e | 2015-05-04 18:00:15 -0400 | [diff] [blame] | 196 | ret->key = &ret->pkeys[cert->key - cert->pkeys]; |
Bodo Möller | ca8e5b9 | 1999-05-09 20:12:44 +0000 | [diff] [blame] | 197 | |
Richard Levitte | bc36ee6 | 2001-02-20 08:13:47 +0000 | [diff] [blame] | 198 | #ifndef OPENSSL_NO_DH |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 199 | if (cert->dh_tmp != NULL) { |
Dr. Stephen Henson | e2b420f | 2015-12-17 00:05:26 +0000 | [diff] [blame] | 200 | ret->dh_tmp = cert->dh_tmp; |
| 201 | EVP_PKEY_up_ref(ret->dh_tmp); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 202 | } |
| 203 | ret->dh_tmp_cb = cert->dh_tmp_cb; |
| 204 | ret->dh_tmp_auto = cert->dh_tmp_auto; |
Bodo Möller | ca8e5b9 | 1999-05-09 20:12:44 +0000 | [diff] [blame] | 205 | #endif |
| 206 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 207 | for (i = 0; i < SSL_PKEY_NUM; i++) { |
| 208 | CERT_PKEY *cpk = cert->pkeys + i; |
| 209 | CERT_PKEY *rpk = ret->pkeys + i; |
| 210 | if (cpk->x509 != NULL) { |
| 211 | rpk->x509 = cpk->x509; |
Dr. Stephen Henson | 05f0fb9 | 2015-08-31 20:29:57 +0100 | [diff] [blame] | 212 | X509_up_ref(rpk->x509); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 213 | } |
Dr. Stephen Henson | f71c6e5 | 2012-01-31 14:00:10 +0000 | [diff] [blame] | 214 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 215 | if (cpk->privatekey != NULL) { |
| 216 | rpk->privatekey = cpk->privatekey; |
Dr. Stephen Henson | 3aeb934 | 2016-01-19 00:21:12 +0000 | [diff] [blame^] | 217 | EVP_PKEY_up_ref(cpk->privatekey); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | if (cpk->chain) { |
| 221 | rpk->chain = X509_chain_up_ref(cpk->chain); |
| 222 | if (!rpk->chain) { |
| 223 | SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_MALLOC_FAILURE); |
| 224 | goto err; |
| 225 | } |
| 226 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 227 | if (cert->pkeys[i].serverinfo != NULL) { |
| 228 | /* Just copy everything. */ |
| 229 | ret->pkeys[i].serverinfo = |
| 230 | OPENSSL_malloc(cert->pkeys[i].serverinfo_length); |
| 231 | if (ret->pkeys[i].serverinfo == NULL) { |
| 232 | SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_MALLOC_FAILURE); |
| 233 | goto err; |
| 234 | } |
| 235 | ret->pkeys[i].serverinfo_length = |
| 236 | cert->pkeys[i].serverinfo_length; |
| 237 | memcpy(ret->pkeys[i].serverinfo, |
| 238 | cert->pkeys[i].serverinfo, |
| 239 | cert->pkeys[i].serverinfo_length); |
| 240 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 241 | } |
Dr. Stephen Henson | 3dbc46d | 2012-07-03 12:51:14 +0000 | [diff] [blame] | 242 | |
Dr. Stephen Henson | 76106e6 | 2015-05-12 17:17:37 +0100 | [diff] [blame] | 243 | /* Configured sigalgs copied across */ |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 244 | if (cert->conf_sigalgs) { |
| 245 | ret->conf_sigalgs = OPENSSL_malloc(cert->conf_sigalgslen); |
Matt Caswell | a71edf3 | 2015-10-30 10:05:53 +0000 | [diff] [blame] | 246 | if (ret->conf_sigalgs == NULL) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 247 | goto err; |
| 248 | memcpy(ret->conf_sigalgs, cert->conf_sigalgs, cert->conf_sigalgslen); |
| 249 | ret->conf_sigalgslen = cert->conf_sigalgslen; |
| 250 | } else |
| 251 | ret->conf_sigalgs = NULL; |
Bodo Möller | ca8e5b9 | 1999-05-09 20:12:44 +0000 | [diff] [blame] | 252 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 253 | if (cert->client_sigalgs) { |
| 254 | ret->client_sigalgs = OPENSSL_malloc(cert->client_sigalgslen); |
Matt Caswell | a71edf3 | 2015-10-30 10:05:53 +0000 | [diff] [blame] | 255 | if (ret->client_sigalgs == NULL) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 256 | goto err; |
| 257 | memcpy(ret->client_sigalgs, cert->client_sigalgs, |
| 258 | cert->client_sigalgslen); |
| 259 | ret->client_sigalgslen = cert->client_sigalgslen; |
| 260 | } else |
| 261 | ret->client_sigalgs = NULL; |
| 262 | /* Shared sigalgs also NULL */ |
| 263 | ret->shared_sigalgs = NULL; |
| 264 | /* Copy any custom client certificate types */ |
| 265 | if (cert->ctypes) { |
| 266 | ret->ctypes = OPENSSL_malloc(cert->ctype_num); |
Matt Caswell | a71edf3 | 2015-10-30 10:05:53 +0000 | [diff] [blame] | 267 | if (ret->ctypes == NULL) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 268 | goto err; |
| 269 | memcpy(ret->ctypes, cert->ctypes, cert->ctype_num); |
| 270 | ret->ctype_num = cert->ctype_num; |
| 271 | } |
Dr. Stephen Henson | d61ff83 | 2012-06-28 12:45:49 +0000 | [diff] [blame] | 272 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 273 | ret->cert_flags = cert->cert_flags; |
Dr. Stephen Henson | 18d7158 | 2012-06-29 14:24:42 +0000 | [diff] [blame] | 274 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 275 | ret->cert_cb = cert->cert_cb; |
| 276 | ret->cert_cb_arg = cert->cert_cb_arg; |
Dr. Stephen Henson | 74ecfab | 2012-07-23 23:34:28 +0000 | [diff] [blame] | 277 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 278 | if (cert->verify_store) { |
| 279 | CRYPTO_add(&cert->verify_store->references, 1, |
| 280 | CRYPTO_LOCK_X509_STORE); |
| 281 | ret->verify_store = cert->verify_store; |
| 282 | } |
Dr. Stephen Henson | 74ecfab | 2012-07-23 23:34:28 +0000 | [diff] [blame] | 283 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 284 | if (cert->chain_store) { |
| 285 | CRYPTO_add(&cert->chain_store->references, 1, CRYPTO_LOCK_X509_STORE); |
| 286 | ret->chain_store = cert->chain_store; |
| 287 | } |
Dr. Stephen Henson | 94a209d | 2012-09-12 13:57:48 +0000 | [diff] [blame] | 288 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 289 | ret->sec_cb = cert->sec_cb; |
| 290 | ret->sec_level = cert->sec_level; |
| 291 | ret->sec_ex = cert->sec_ex; |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 292 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 293 | if (!custom_exts_copy(&ret->cli_ext, &cert->cli_ext)) |
| 294 | goto err; |
| 295 | if (!custom_exts_copy(&ret->srv_ext, &cert->srv_ext)) |
| 296 | goto err; |
Dr. Stephen Henson | 9076bd2 | 2015-09-11 17:08:11 +0100 | [diff] [blame] | 297 | #ifndef OPENSSL_NO_PSK |
Dr. Stephen Henson | df6da24 | 2015-09-14 17:58:04 +0100 | [diff] [blame] | 298 | if (cert->psk_identity_hint) { |
Rich Salz | 7644a9a | 2015-12-16 16:12:24 -0500 | [diff] [blame] | 299 | ret->psk_identity_hint = OPENSSL_strdup(cert->psk_identity_hint); |
Dr. Stephen Henson | df6da24 | 2015-09-14 17:58:04 +0100 | [diff] [blame] | 300 | if (ret->psk_identity_hint == NULL) |
| 301 | goto err; |
| 302 | } |
Dr. Stephen Henson | 9076bd2 | 2015-09-11 17:08:11 +0100 | [diff] [blame] | 303 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 304 | return (ret); |
Dr. Stephen Henson | 9ade64d | 2012-01-27 14:21:38 +0000 | [diff] [blame] | 305 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 306 | err: |
| 307 | ssl_cert_free(ret); |
| 308 | |
| 309 | return NULL; |
| 310 | } |
Bodo Möller | ca8e5b9 | 1999-05-09 20:12:44 +0000 | [diff] [blame] | 311 | |
Dr. Stephen Henson | a5ee80b | 2012-06-18 12:56:59 +0000 | [diff] [blame] | 312 | /* Free up and clear all certificates and chains */ |
| 313 | |
| 314 | void ssl_cert_clear_certs(CERT *c) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 315 | { |
| 316 | int i; |
| 317 | if (c == NULL) |
| 318 | return; |
| 319 | for (i = 0; i < SSL_PKEY_NUM; i++) { |
| 320 | CERT_PKEY *cpk = c->pkeys + i; |
Rich Salz | 222561f | 2015-04-30 17:33:59 -0400 | [diff] [blame] | 321 | X509_free(cpk->x509); |
| 322 | cpk->x509 = NULL; |
Rich Salz | c5ba2d9 | 2015-03-28 10:54:15 -0400 | [diff] [blame] | 323 | EVP_PKEY_free(cpk->privatekey); |
| 324 | cpk->privatekey = NULL; |
Rich Salz | 222561f | 2015-04-30 17:33:59 -0400 | [diff] [blame] | 325 | sk_X509_pop_free(cpk->chain, X509_free); |
| 326 | cpk->chain = NULL; |
Rich Salz | 25aaa98 | 2015-05-01 14:37:16 -0400 | [diff] [blame] | 327 | OPENSSL_free(cpk->serverinfo); |
| 328 | cpk->serverinfo = NULL; |
| 329 | cpk->serverinfo_length = 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 330 | } |
| 331 | } |
Bodo Möller | ca8e5b9 | 1999-05-09 20:12:44 +0000 | [diff] [blame] | 332 | |
Ben Laurie | eb90a48 | 1999-02-28 17:41:55 +0000 | [diff] [blame] | 333 | void ssl_cert_free(CERT *c) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 334 | { |
| 335 | int i; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 336 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 337 | if (c == NULL) |
| 338 | return; |
Ben Laurie | e03ddfa | 1999-01-07 19:15:59 +0000 | [diff] [blame] | 339 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 340 | i = CRYPTO_add(&c->references, -1, CRYPTO_LOCK_SSL_CERT); |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 341 | #ifdef REF_PRINT |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 342 | REF_PRINT("CERT", c); |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 343 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 344 | if (i > 0) |
| 345 | return; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 346 | #ifdef REF_CHECK |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 347 | if (i < 0) { |
| 348 | fprintf(stderr, "ssl_cert_free, bad reference count\n"); |
| 349 | abort(); /* ok */ |
| 350 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 351 | #endif |
| 352 | |
Richard Levitte | bc36ee6 | 2001-02-20 08:13:47 +0000 | [diff] [blame] | 353 | #ifndef OPENSSL_NO_DH |
Dr. Stephen Henson | e2b420f | 2015-12-17 00:05:26 +0000 | [diff] [blame] | 354 | EVP_PKEY_free(c->dh_tmp); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 355 | #endif |
| 356 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 357 | ssl_cert_clear_certs(c); |
Rich Salz | 25aaa98 | 2015-05-01 14:37:16 -0400 | [diff] [blame] | 358 | OPENSSL_free(c->conf_sigalgs); |
| 359 | OPENSSL_free(c->client_sigalgs); |
| 360 | OPENSSL_free(c->shared_sigalgs); |
| 361 | OPENSSL_free(c->ctypes); |
Rich Salz | 222561f | 2015-04-30 17:33:59 -0400 | [diff] [blame] | 362 | X509_STORE_free(c->verify_store); |
| 363 | X509_STORE_free(c->chain_store); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 364 | custom_exts_free(&c->cli_ext); |
| 365 | custom_exts_free(&c->srv_ext); |
Dr. Stephen Henson | df6da24 | 2015-09-14 17:58:04 +0100 | [diff] [blame] | 366 | #ifndef OPENSSL_NO_PSK |
| 367 | OPENSSL_free(c->psk_identity_hint); |
| 368 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 369 | OPENSSL_free(c); |
| 370 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 371 | |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 372 | 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] | 373 | { |
| 374 | int i, r; |
| 375 | CERT_PKEY *cpk = s ? s->cert->key : ctx->cert->key; |
| 376 | if (!cpk) |
| 377 | return 0; |
Rich Salz | 222561f | 2015-04-30 17:33:59 -0400 | [diff] [blame] | 378 | sk_X509_pop_free(cpk->chain, X509_free); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 379 | for (i = 0; i < sk_X509_num(chain); i++) { |
| 380 | r = ssl_security_cert(s, ctx, sk_X509_value(chain, i), 0, 0); |
| 381 | if (r != 1) { |
| 382 | SSLerr(SSL_F_SSL_CERT_SET0_CHAIN, r); |
| 383 | return 0; |
| 384 | } |
| 385 | } |
| 386 | cpk->chain = chain; |
| 387 | return 1; |
| 388 | } |
Dr. Stephen Henson | f71c6e5 | 2012-01-31 14:00:10 +0000 | [diff] [blame] | 389 | |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 390 | 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] | 391 | { |
| 392 | STACK_OF(X509) *dchain; |
| 393 | if (!chain) |
| 394 | return ssl_cert_set0_chain(s, ctx, NULL); |
| 395 | dchain = X509_chain_up_ref(chain); |
| 396 | if (!dchain) |
| 397 | return 0; |
| 398 | if (!ssl_cert_set0_chain(s, ctx, dchain)) { |
| 399 | sk_X509_pop_free(dchain, X509_free); |
| 400 | return 0; |
| 401 | } |
| 402 | return 1; |
| 403 | } |
Dr. Stephen Henson | f71c6e5 | 2012-01-31 14:00:10 +0000 | [diff] [blame] | 404 | |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 405 | 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] | 406 | { |
| 407 | int r; |
| 408 | CERT_PKEY *cpk = s ? s->cert->key : ctx->cert->key; |
| 409 | if (!cpk) |
| 410 | return 0; |
| 411 | r = ssl_security_cert(s, ctx, x, 0, 0); |
| 412 | if (r != 1) { |
| 413 | SSLerr(SSL_F_SSL_CERT_ADD0_CHAIN_CERT, r); |
| 414 | return 0; |
| 415 | } |
| 416 | if (!cpk->chain) |
| 417 | cpk->chain = sk_X509_new_null(); |
| 418 | if (!cpk->chain || !sk_X509_push(cpk->chain, x)) |
| 419 | return 0; |
| 420 | return 1; |
| 421 | } |
Dr. Stephen Henson | f71c6e5 | 2012-01-31 14:00:10 +0000 | [diff] [blame] | 422 | |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 423 | 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] | 424 | { |
| 425 | if (!ssl_cert_add0_chain_cert(s, ctx, x)) |
| 426 | return 0; |
Dr. Stephen Henson | 05f0fb9 | 2015-08-31 20:29:57 +0100 | [diff] [blame] | 427 | X509_up_ref(x); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 428 | return 1; |
| 429 | } |
Bodo Möller | b56bce4 | 1999-05-13 15:09:38 +0000 | [diff] [blame] | 430 | |
Rob Stradling | 7b6b246 | 2013-11-11 18:04:24 +0100 | [diff] [blame] | 431 | int ssl_cert_select_current(CERT *c, X509 *x) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 432 | { |
| 433 | int i; |
| 434 | if (x == NULL) |
| 435 | return 0; |
| 436 | for (i = 0; i < SSL_PKEY_NUM; i++) { |
| 437 | CERT_PKEY *cpk = c->pkeys + i; |
| 438 | if (cpk->x509 == x && cpk->privatekey) { |
| 439 | c->key = cpk; |
| 440 | return 1; |
| 441 | } |
| 442 | } |
Dr. Stephen Henson | 629b640 | 2013-11-13 22:57:11 +0000 | [diff] [blame] | 443 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 444 | for (i = 0; i < SSL_PKEY_NUM; i++) { |
| 445 | CERT_PKEY *cpk = c->pkeys + i; |
| 446 | if (cpk->privatekey && cpk->x509 && !X509_cmp(cpk->x509, x)) { |
| 447 | c->key = cpk; |
| 448 | return 1; |
| 449 | } |
| 450 | } |
| 451 | return 0; |
| 452 | } |
Rob Stradling | 7b6b246 | 2013-11-11 18:04:24 +0100 | [diff] [blame] | 453 | |
Dr. Stephen Henson | 0f78819 | 2014-02-02 02:51:30 +0000 | [diff] [blame] | 454 | int ssl_cert_set_current(CERT *c, long op) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 455 | { |
| 456 | int i, idx; |
| 457 | if (!c) |
| 458 | return 0; |
| 459 | if (op == SSL_CERT_SET_FIRST) |
| 460 | idx = 0; |
| 461 | else if (op == SSL_CERT_SET_NEXT) { |
| 462 | idx = (int)(c->key - c->pkeys + 1); |
| 463 | if (idx >= SSL_PKEY_NUM) |
| 464 | return 0; |
| 465 | } else |
| 466 | return 0; |
| 467 | for (i = idx; i < SSL_PKEY_NUM; i++) { |
| 468 | CERT_PKEY *cpk = c->pkeys + i; |
| 469 | if (cpk->x509 && cpk->privatekey) { |
| 470 | c->key = cpk; |
| 471 | return 1; |
| 472 | } |
| 473 | } |
| 474 | return 0; |
| 475 | } |
Dr. Stephen Henson | 0f78819 | 2014-02-02 02:51:30 +0000 | [diff] [blame] | 476 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 477 | void ssl_cert_set_cert_cb(CERT *c, int (*cb) (SSL *ssl, void *arg), void *arg) |
| 478 | { |
| 479 | c->cert_cb = cb; |
| 480 | c->cert_cb_arg = arg; |
| 481 | } |
Dr. Stephen Henson | 18d7158 | 2012-06-29 14:24:42 +0000 | [diff] [blame] | 482 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 483 | int ssl_verify_cert_chain(SSL *s, STACK_OF(X509) *sk) |
| 484 | { |
| 485 | X509 *x; |
| 486 | int i; |
| 487 | X509_STORE *verify_store; |
| 488 | X509_STORE_CTX ctx; |
Viktor Dukhovni | 919ba00 | 2015-12-29 13:28:28 -0500 | [diff] [blame] | 489 | X509_VERIFY_PARAM *param; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 490 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 491 | if (s->cert->verify_store) |
| 492 | verify_store = s->cert->verify_store; |
| 493 | else |
| 494 | verify_store = s->ctx->cert_store; |
Dr. Stephen Henson | 74ecfab | 2012-07-23 23:34:28 +0000 | [diff] [blame] | 495 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 496 | if ((sk == NULL) || (sk_X509_num(sk) == 0)) |
| 497 | return (0); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 498 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 499 | x = sk_X509_value(sk, 0); |
| 500 | if (!X509_STORE_CTX_init(&ctx, verify_store, x, sk)) { |
| 501 | SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN, ERR_R_X509_LIB); |
| 502 | return (0); |
| 503 | } |
Viktor Dukhovni | 919ba00 | 2015-12-29 13:28:28 -0500 | [diff] [blame] | 504 | param = X509_STORE_CTX_get0_param(&ctx); |
| 505 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 506 | /* Set suite B flags if needed */ |
| 507 | X509_STORE_CTX_set_flags(&ctx, tls1_suiteb(s)); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 508 | 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] | 509 | |
Viktor Dukhovni | 919ba00 | 2015-12-29 13:28:28 -0500 | [diff] [blame] | 510 | /* Verify via DANE if enabled */ |
| 511 | if (DANETLS_ENABLED(&s->dane)) |
| 512 | X509_STORE_CTX_set0_dane(&ctx, &s->dane); |
| 513 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 514 | /* |
| 515 | * We need to inherit the verify parameters. These can be determined by |
| 516 | * the context: if its a server it will verify SSL client certificates or |
| 517 | * vice versa. |
| 518 | */ |
Dr. Stephen Henson | bb7cd4e | 1999-11-29 22:35:00 +0000 | [diff] [blame] | 519 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 520 | X509_STORE_CTX_set_default(&ctx, s->server ? "ssl_client" : "ssl_server"); |
| 521 | /* |
Viktor Dukhovni | 919ba00 | 2015-12-29 13:28:28 -0500 | [diff] [blame] | 522 | * Anything non-default in "s->param" should overwrite anything in the ctx. |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 523 | */ |
Viktor Dukhovni | 919ba00 | 2015-12-29 13:28:28 -0500 | [diff] [blame] | 524 | X509_VERIFY_PARAM_set1(param, s->param); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 525 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 526 | if (s->verify_callback) |
| 527 | X509_STORE_CTX_set_verify_cb(&ctx, s->verify_callback); |
Lutz Jänicke | 1f0c9ad | 2001-07-30 11:45:34 +0000 | [diff] [blame] | 528 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 529 | if (s->ctx->app_verify_callback != NULL) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 530 | i = s->ctx->app_verify_callback(&ctx, s->ctx->app_verify_arg); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 531 | else { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 532 | i = X509_verify_cert(&ctx); |
| 533 | # if 0 |
| 534 | /* Dummy error calls so mkerr generates them */ |
| 535 | SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN, SSL_R_EE_KEY_TOO_SMALL); |
| 536 | SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN, SSL_R_CA_KEY_TOO_SMALL); |
| 537 | SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN, SSL_R_CA_MD_TOO_WEAK); |
| 538 | # endif |
| 539 | if (i > 0) |
| 540 | i = ssl_security_cert_chain(s, ctx.chain, NULL, 1); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 541 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 542 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 543 | s->verify_result = ctx.error; |
Viktor Dukhovni | 919ba00 | 2015-12-29 13:28:28 -0500 | [diff] [blame] | 544 | |
| 545 | /* Move peername from the store context params to the SSL handle's */ |
| 546 | X509_VERIFY_PARAM_move_peername(s->param, param); |
| 547 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 548 | X509_STORE_CTX_cleanup(&ctx); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 549 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 550 | return (i); |
| 551 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 552 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 553 | static void set_client_CA_list(STACK_OF(X509_NAME) **ca_list, |
| 554 | STACK_OF(X509_NAME) *name_list) |
| 555 | { |
Rich Salz | 222561f | 2015-04-30 17:33:59 -0400 | [diff] [blame] | 556 | sk_X509_NAME_pop_free(*ca_list, X509_NAME_free); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 557 | *ca_list = name_list; |
| 558 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 559 | |
Ben Laurie | 838d25a | 1999-05-30 14:13:19 +0000 | [diff] [blame] | 560 | 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] | 561 | { |
| 562 | int i; |
| 563 | STACK_OF(X509_NAME) *ret; |
| 564 | X509_NAME *name; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 565 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 566 | ret = sk_X509_NAME_new_null(); |
| 567 | for (i = 0; i < sk_X509_NAME_num(sk); i++) { |
| 568 | name = X509_NAME_dup(sk_X509_NAME_value(sk, i)); |
| 569 | if ((name == NULL) || !sk_X509_NAME_push(ret, name)) { |
| 570 | sk_X509_NAME_pop_free(ret, X509_NAME_free); |
| 571 | return (NULL); |
| 572 | } |
| 573 | } |
| 574 | return (ret); |
| 575 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 576 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 577 | void SSL_set_client_CA_list(SSL *s, STACK_OF(X509_NAME) *name_list) |
| 578 | { |
| 579 | set_client_CA_list(&(s->client_CA), name_list); |
| 580 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 581 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 582 | void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list) |
| 583 | { |
| 584 | set_client_CA_list(&(ctx->client_CA), name_list); |
| 585 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 586 | |
Ben Laurie | 0821bcd | 2005-03-30 10:26:02 +0000 | [diff] [blame] | 587 | 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] | 588 | { |
| 589 | return (ctx->client_CA); |
| 590 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 591 | |
Ben Laurie | 0821bcd | 2005-03-30 10:26:02 +0000 | [diff] [blame] | 592 | STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 593 | { |
Matt Caswell | 23a635c | 2015-09-10 09:19:53 +0100 | [diff] [blame] | 594 | if (!s->server) { /* we are in the client */ |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 595 | if (((s->version >> 8) == SSL3_VERSION_MAJOR) && (s->s3 != NULL)) |
| 596 | return (s->s3->tmp.ca_names); |
| 597 | else |
| 598 | return (NULL); |
| 599 | } else { |
| 600 | if (s->client_CA != NULL) |
| 601 | return (s->client_CA); |
| 602 | else |
| 603 | return (s->ctx->client_CA); |
| 604 | } |
| 605 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 606 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 607 | static int add_client_CA(STACK_OF(X509_NAME) **sk, X509 *x) |
| 608 | { |
| 609 | X509_NAME *name; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 610 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 611 | if (x == NULL) |
| 612 | return (0); |
| 613 | if ((*sk == NULL) && ((*sk = sk_X509_NAME_new_null()) == NULL)) |
| 614 | return (0); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 615 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 616 | if ((name = X509_NAME_dup(X509_get_subject_name(x))) == NULL) |
| 617 | return (0); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 618 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 619 | if (!sk_X509_NAME_push(*sk, name)) { |
| 620 | X509_NAME_free(name); |
| 621 | return (0); |
| 622 | } |
| 623 | return (1); |
| 624 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 625 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 626 | int SSL_add_client_CA(SSL *ssl, X509 *x) |
| 627 | { |
| 628 | return (add_client_CA(&(ssl->client_CA), x)); |
| 629 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 630 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 631 | int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x) |
| 632 | { |
| 633 | return (add_client_CA(&(ctx->client_CA), x)); |
| 634 | } |
| 635 | |
| 636 | static int xname_cmp(const X509_NAME *const *a, const X509_NAME *const *b) |
| 637 | { |
| 638 | return (X509_NAME_cmp(*a, *b)); |
| 639 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 640 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 641 | /** |
Ben Laurie | eb90a48 | 1999-02-28 17:41:55 +0000 | [diff] [blame] | 642 | * Load CA certs from a file into a ::STACK. Note that it is somewhat misnamed; |
| 643 | * it doesn't really have anything to do with clients (except that a common use |
| 644 | * for a stack of CAs is to send it to the client). Actually, it doesn't have |
| 645 | * much to do with CAs, either, since it will load any old cert. |
| 646 | * \param file the file containing one or more certs. |
| 647 | * \return a ::STACK containing the certs. |
| 648 | */ |
Ben Laurie | f73e07c | 1999-04-12 17:23:57 +0000 | [diff] [blame] | 649 | STACK_OF(X509_NAME) *SSL_load_client_CA_file(const char *file) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 650 | { |
| 651 | BIO *in; |
| 652 | X509 *x = NULL; |
| 653 | X509_NAME *xn = NULL; |
| 654 | STACK_OF(X509_NAME) *ret = NULL, *sk; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 655 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 656 | sk = sk_X509_NAME_new(xname_cmp); |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 657 | |
Rich Salz | 9982cbb | 2015-09-30 14:32:49 -0400 | [diff] [blame] | 658 | in = BIO_new(BIO_s_file()); |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 659 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 660 | if ((sk == NULL) || (in == NULL)) { |
| 661 | SSLerr(SSL_F_SSL_LOAD_CLIENT_CA_FILE, ERR_R_MALLOC_FAILURE); |
| 662 | goto err; |
| 663 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 664 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 665 | if (!BIO_read_filename(in, file)) |
| 666 | goto err; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 667 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 668 | for (;;) { |
| 669 | if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL) |
| 670 | break; |
| 671 | if (ret == NULL) { |
| 672 | ret = sk_X509_NAME_new_null(); |
| 673 | if (ret == NULL) { |
| 674 | SSLerr(SSL_F_SSL_LOAD_CLIENT_CA_FILE, ERR_R_MALLOC_FAILURE); |
| 675 | goto err; |
| 676 | } |
| 677 | } |
| 678 | if ((xn = X509_get_subject_name(x)) == NULL) |
| 679 | goto err; |
| 680 | /* check for duplicates */ |
| 681 | xn = X509_NAME_dup(xn); |
| 682 | if (xn == NULL) |
| 683 | goto err; |
| 684 | if (sk_X509_NAME_find(sk, xn) >= 0) |
| 685 | X509_NAME_free(xn); |
| 686 | else { |
| 687 | sk_X509_NAME_push(sk, xn); |
| 688 | sk_X509_NAME_push(ret, xn); |
| 689 | } |
| 690 | } |
Rich Salz | 6669647 | 2015-05-01 14:29:48 -0400 | [diff] [blame] | 691 | goto done; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 692 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 693 | err: |
Rich Salz | 6669647 | 2015-05-01 14:29:48 -0400 | [diff] [blame] | 694 | sk_X509_NAME_pop_free(ret, X509_NAME_free); |
| 695 | ret = NULL; |
| 696 | done: |
Rich Salz | 222561f | 2015-04-30 17:33:59 -0400 | [diff] [blame] | 697 | sk_X509_NAME_free(sk); |
Rich Salz | ca3a82c | 2015-03-25 11:31:18 -0400 | [diff] [blame] | 698 | BIO_free(in); |
Rich Salz | 222561f | 2015-04-30 17:33:59 -0400 | [diff] [blame] | 699 | X509_free(x); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 700 | if (ret != NULL) |
| 701 | ERR_clear_error(); |
| 702 | return (ret); |
| 703 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 704 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 705 | /** |
Ben Laurie | eb90a48 | 1999-02-28 17:41:55 +0000 | [diff] [blame] | 706 | * Add a file of certs to a stack. |
| 707 | * \param stack the stack to add to. |
| 708 | * \param file the file to add from. All certs in this file that are not |
| 709 | * already in the stack will be added. |
| 710 | * \return 1 for success, 0 for failure. Note that in the case of failure some |
| 711 | * certs may have been added to \c stack. |
| 712 | */ |
| 713 | |
Ben Laurie | 661b361 | 1999-05-03 19:55:00 +0000 | [diff] [blame] | 714 | 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] | 715 | const char *file) |
| 716 | { |
| 717 | BIO *in; |
| 718 | X509 *x = NULL; |
| 719 | X509_NAME *xn = NULL; |
| 720 | int ret = 1; |
| 721 | int (*oldcmp) (const X509_NAME *const *a, const X509_NAME *const *b); |
Ben Laurie | eb90a48 | 1999-02-28 17:41:55 +0000 | [diff] [blame] | 722 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 723 | oldcmp = sk_X509_NAME_set_cmp_func(stack, xname_cmp); |
Dr. Stephen Henson | a3a06e6 | 2010-03-24 23:17:15 +0000 | [diff] [blame] | 724 | |
Rich Salz | 9982cbb | 2015-09-30 14:32:49 -0400 | [diff] [blame] | 725 | in = BIO_new(BIO_s_file()); |
Bodo Möller | 2ea0910 | 2000-05-21 14:14:30 +0000 | [diff] [blame] | 726 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 727 | if (in == NULL) { |
| 728 | SSLerr(SSL_F_SSL_ADD_FILE_CERT_SUBJECTS_TO_STACK, |
| 729 | ERR_R_MALLOC_FAILURE); |
| 730 | goto err; |
| 731 | } |
Ben Laurie | eb90a48 | 1999-02-28 17:41:55 +0000 | [diff] [blame] | 732 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 733 | if (!BIO_read_filename(in, file)) |
| 734 | goto err; |
| 735 | |
| 736 | for (;;) { |
| 737 | if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL) |
| 738 | break; |
| 739 | if ((xn = X509_get_subject_name(x)) == NULL) |
| 740 | goto err; |
| 741 | xn = X509_NAME_dup(xn); |
| 742 | if (xn == NULL) |
| 743 | goto err; |
| 744 | if (sk_X509_NAME_find(stack, xn) >= 0) |
| 745 | X509_NAME_free(xn); |
| 746 | else |
| 747 | sk_X509_NAME_push(stack, xn); |
| 748 | } |
| 749 | |
| 750 | ERR_clear_error(); |
Rich Salz | 6669647 | 2015-05-01 14:29:48 -0400 | [diff] [blame] | 751 | goto done; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 752 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 753 | err: |
| 754 | ret = 0; |
Rich Salz | 6669647 | 2015-05-01 14:29:48 -0400 | [diff] [blame] | 755 | done: |
Rich Salz | ca3a82c | 2015-03-25 11:31:18 -0400 | [diff] [blame] | 756 | BIO_free(in); |
Rich Salz | 25aaa98 | 2015-05-01 14:37:16 -0400 | [diff] [blame] | 757 | X509_free(x); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 758 | (void)sk_X509_NAME_set_cmp_func(stack, oldcmp); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 759 | return ret; |
| 760 | } |
| 761 | |
| 762 | /** |
Ben Laurie | eb90a48 | 1999-02-28 17:41:55 +0000 | [diff] [blame] | 763 | * Add a directory of certs to a stack. |
| 764 | * \param stack the stack to append to. |
| 765 | * \param dir the directory to append from. All files in this directory will be |
| 766 | * examined as potential certs. Any that are acceptable to |
Ralf S. Engelschall | 72e442a | 1999-03-22 15:50:34 +0000 | [diff] [blame] | 767 | * 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] | 768 | * included. |
| 769 | * \return 1 for success, 0 for failure. Note that in the case of failure some |
| 770 | * certs may have been added to \c stack. |
| 771 | */ |
| 772 | |
Ben Laurie | 661b361 | 1999-05-03 19:55:00 +0000 | [diff] [blame] | 773 | 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] | 774 | const char *dir) |
| 775 | { |
| 776 | OPENSSL_DIR_CTX *d = NULL; |
| 777 | const char *filename; |
| 778 | int ret = 0; |
Ben Laurie | eb90a48 | 1999-02-28 17:41:55 +0000 | [diff] [blame] | 779 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 780 | CRYPTO_w_lock(CRYPTO_LOCK_READDIR); |
Ben Laurie | eb90a48 | 1999-02-28 17:41:55 +0000 | [diff] [blame] | 781 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 782 | /* 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] | 783 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 784 | while ((filename = OPENSSL_DIR_read(&d, dir))) { |
| 785 | char buf[1024]; |
| 786 | int r; |
Richard Levitte | 4083a22 | 2004-07-10 13:17:16 +0000 | [diff] [blame] | 787 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 788 | if (strlen(dir) + strlen(filename) + 2 > sizeof buf) { |
| 789 | SSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK, |
| 790 | SSL_R_PATH_TOO_LONG); |
| 791 | goto err; |
| 792 | } |
Richard Levitte | 4083a22 | 2004-07-10 13:17:16 +0000 | [diff] [blame] | 793 | #ifdef OPENSSL_SYS_VMS |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 794 | r = BIO_snprintf(buf, sizeof buf, "%s%s", dir, filename); |
Richard Levitte | 4083a22 | 2004-07-10 13:17:16 +0000 | [diff] [blame] | 795 | #else |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 796 | r = BIO_snprintf(buf, sizeof buf, "%s/%s", dir, filename); |
Richard Levitte | 4083a22 | 2004-07-10 13:17:16 +0000 | [diff] [blame] | 797 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 798 | if (r <= 0 || r >= (int)sizeof(buf)) |
| 799 | goto err; |
| 800 | if (!SSL_add_file_cert_subjects_to_stack(stack, buf)) |
| 801 | goto err; |
| 802 | } |
Ben Laurie | eb90a48 | 1999-02-28 17:41:55 +0000 | [diff] [blame] | 803 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 804 | if (errno) { |
| 805 | SYSerr(SYS_F_OPENDIR, get_last_sys_error()); |
| 806 | ERR_add_error_data(3, "OPENSSL_DIR_read(&ctx, '", dir, "')"); |
| 807 | SSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK, ERR_R_SYS_LIB); |
| 808 | goto err; |
| 809 | } |
Richard Levitte | 4083a22 | 2004-07-10 13:17:16 +0000 | [diff] [blame] | 810 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 811 | ret = 1; |
Richard Levitte | 285046e | 2001-10-04 12:27:39 +0000 | [diff] [blame] | 812 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 813 | err: |
| 814 | if (d) |
| 815 | OPENSSL_DIR_end(&d); |
| 816 | CRYPTO_w_unlock(CRYPTO_LOCK_READDIR); |
| 817 | return ret; |
| 818 | } |
Richard Levitte | 285046e | 2001-10-04 12:27:39 +0000 | [diff] [blame] | 819 | |
Dr. Stephen Henson | 4379d0e | 2012-01-26 15:47:32 +0000 | [diff] [blame] | 820 | /* Add a certificate to a BUF_MEM structure */ |
| 821 | |
| 822 | 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] | 823 | { |
| 824 | int n; |
| 825 | unsigned char *p; |
Dr. Stephen Henson | 4379d0e | 2012-01-26 15:47:32 +0000 | [diff] [blame] | 826 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 827 | n = i2d_X509(x, NULL); |
| 828 | if (!BUF_MEM_grow_clean(buf, (int)(n + (*l) + 3))) { |
| 829 | SSLerr(SSL_F_SSL_ADD_CERT_TO_BUF, ERR_R_BUF_LIB); |
| 830 | return 0; |
| 831 | } |
| 832 | p = (unsigned char *)&(buf->data[*l]); |
| 833 | l2n3(n, p); |
| 834 | i2d_X509(x, &p); |
| 835 | *l += n + 3; |
Dr. Stephen Henson | 4379d0e | 2012-01-26 15:47:32 +0000 | [diff] [blame] | 836 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 837 | return 1; |
| 838 | } |
Dr. Stephen Henson | 4379d0e | 2012-01-26 15:47:32 +0000 | [diff] [blame] | 839 | |
| 840 | /* Add certificate chain to internal SSL BUF_MEM strcuture */ |
Dr. Stephen Henson | c526ed4 | 2012-01-26 16:00:34 +0000 | [diff] [blame] | 841 | 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] | 842 | { |
| 843 | BUF_MEM *buf = s->init_buf; |
| 844 | int i; |
Dr. Stephen Henson | 4379d0e | 2012-01-26 15:47:32 +0000 | [diff] [blame] | 845 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 846 | X509 *x; |
| 847 | STACK_OF(X509) *extra_certs; |
| 848 | X509_STORE *chain_store; |
Dr. Stephen Henson | c526ed4 | 2012-01-26 16:00:34 +0000 | [diff] [blame] | 849 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 850 | /* TLSv1 sends a chain with nothing in it, instead of an alert */ |
| 851 | if (!BUF_MEM_grow_clean(buf, 10)) { |
| 852 | SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, ERR_R_BUF_LIB); |
| 853 | return 0; |
| 854 | } |
Dr. Stephen Henson | d628885 | 2014-03-09 16:12:20 +0000 | [diff] [blame] | 855 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 856 | if (!cpk || !cpk->x509) |
| 857 | return 1; |
Dr. Stephen Henson | d628885 | 2014-03-09 16:12:20 +0000 | [diff] [blame] | 858 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 859 | x = cpk->x509; |
Dr. Stephen Henson | d628885 | 2014-03-09 16:12:20 +0000 | [diff] [blame] | 860 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 861 | /* |
| 862 | * If we have a certificate specific chain use it, else use parent ctx. |
| 863 | */ |
| 864 | if (cpk->chain) |
| 865 | extra_certs = cpk->chain; |
| 866 | else |
| 867 | extra_certs = s->ctx->extra_certs; |
Dr. Stephen Henson | d628885 | 2014-03-09 16:12:20 +0000 | [diff] [blame] | 868 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 869 | if ((s->mode & SSL_MODE_NO_AUTO_CHAIN) || extra_certs) |
| 870 | chain_store = NULL; |
| 871 | else if (s->cert->chain_store) |
| 872 | chain_store = s->cert->chain_store; |
| 873 | else |
| 874 | chain_store = s->ctx->cert_store; |
Dr. Stephen Henson | d628885 | 2014-03-09 16:12:20 +0000 | [diff] [blame] | 875 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 876 | if (chain_store) { |
| 877 | X509_STORE_CTX xs_ctx; |
Dr. Stephen Henson | d628885 | 2014-03-09 16:12:20 +0000 | [diff] [blame] | 878 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 879 | if (!X509_STORE_CTX_init(&xs_ctx, chain_store, x, NULL)) { |
| 880 | SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, ERR_R_X509_LIB); |
| 881 | return (0); |
| 882 | } |
Matt Caswell | ae4d0c8 | 2015-11-11 10:17:22 +0000 | [diff] [blame] | 883 | /* |
| 884 | * It is valid for the chain not to be complete (because normally we |
| 885 | * don't include the root cert in the chain). Therefore we deliberately |
| 886 | * ignore the error return from this call. We're not actually verifying |
| 887 | * the cert - we're just building as much of the chain as we can |
| 888 | */ |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 889 | X509_verify_cert(&xs_ctx); |
| 890 | /* Don't leave errors in the queue */ |
| 891 | ERR_clear_error(); |
| 892 | i = ssl_security_cert_chain(s, xs_ctx.chain, NULL, 0); |
| 893 | if (i != 1) { |
| 894 | X509_STORE_CTX_cleanup(&xs_ctx); |
| 895 | SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, i); |
| 896 | return 0; |
| 897 | } |
| 898 | for (i = 0; i < sk_X509_num(xs_ctx.chain); i++) { |
| 899 | x = sk_X509_value(xs_ctx.chain, i); |
Dr. Stephen Henson | d628885 | 2014-03-09 16:12:20 +0000 | [diff] [blame] | 900 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 901 | if (!ssl_add_cert_to_buf(buf, l, x)) { |
| 902 | X509_STORE_CTX_cleanup(&xs_ctx); |
| 903 | return 0; |
| 904 | } |
| 905 | } |
| 906 | X509_STORE_CTX_cleanup(&xs_ctx); |
| 907 | } else { |
| 908 | i = ssl_security_cert_chain(s, extra_certs, x, 0); |
| 909 | if (i != 1) { |
| 910 | SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, i); |
| 911 | return 0; |
| 912 | } |
| 913 | if (!ssl_add_cert_to_buf(buf, l, x)) |
| 914 | return 0; |
| 915 | for (i = 0; i < sk_X509_num(extra_certs); i++) { |
| 916 | x = sk_X509_value(extra_certs, i); |
| 917 | if (!ssl_add_cert_to_buf(buf, l, x)) |
| 918 | return 0; |
| 919 | } |
| 920 | } |
| 921 | return 1; |
| 922 | } |
Dr. Stephen Henson | 4379d0e | 2012-01-26 15:47:32 +0000 | [diff] [blame] | 923 | |
Dr. Stephen Henson | 74ecfab | 2012-07-23 23:34:28 +0000 | [diff] [blame] | 924 | /* Build a certificate chain for current certificate */ |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 925 | int ssl_build_cert_chain(SSL *s, SSL_CTX *ctx, int flags) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 926 | { |
| 927 | CERT *c = s ? s->cert : ctx->cert; |
| 928 | CERT_PKEY *cpk = c->key; |
| 929 | X509_STORE *chain_store = NULL; |
| 930 | X509_STORE_CTX xs_ctx; |
| 931 | STACK_OF(X509) *chain = NULL, *untrusted = NULL; |
| 932 | X509 *x; |
| 933 | int i, rv = 0; |
| 934 | unsigned long error; |
Dr. Stephen Henson | 74ecfab | 2012-07-23 23:34:28 +0000 | [diff] [blame] | 935 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 936 | if (!cpk->x509) { |
| 937 | SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, SSL_R_NO_CERTIFICATE_SET); |
| 938 | goto err; |
| 939 | } |
| 940 | /* Rearranging and check the chain: add everything to a store */ |
| 941 | if (flags & SSL_BUILD_CHAIN_FLAG_CHECK) { |
| 942 | chain_store = X509_STORE_new(); |
Matt Caswell | a71edf3 | 2015-10-30 10:05:53 +0000 | [diff] [blame] | 943 | if (chain_store == NULL) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 944 | goto err; |
| 945 | for (i = 0; i < sk_X509_num(cpk->chain); i++) { |
| 946 | x = sk_X509_value(cpk->chain, i); |
| 947 | if (!X509_STORE_add_cert(chain_store, x)) { |
| 948 | error = ERR_peek_last_error(); |
| 949 | if (ERR_GET_LIB(error) != ERR_LIB_X509 || |
| 950 | ERR_GET_REASON(error) != |
| 951 | X509_R_CERT_ALREADY_IN_HASH_TABLE) |
| 952 | goto err; |
| 953 | ERR_clear_error(); |
| 954 | } |
| 955 | } |
| 956 | /* Add EE cert too: it might be self signed */ |
| 957 | if (!X509_STORE_add_cert(chain_store, cpk->x509)) { |
| 958 | error = ERR_peek_last_error(); |
| 959 | if (ERR_GET_LIB(error) != ERR_LIB_X509 || |
| 960 | ERR_GET_REASON(error) != X509_R_CERT_ALREADY_IN_HASH_TABLE) |
| 961 | goto err; |
| 962 | ERR_clear_error(); |
| 963 | } |
| 964 | } else { |
| 965 | if (c->chain_store) |
| 966 | chain_store = c->chain_store; |
| 967 | else if (s) |
| 968 | chain_store = s->ctx->cert_store; |
| 969 | else |
| 970 | chain_store = ctx->cert_store; |
Dr. Stephen Henson | 74ecfab | 2012-07-23 23:34:28 +0000 | [diff] [blame] | 971 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 972 | if (flags & SSL_BUILD_CHAIN_FLAG_UNTRUSTED) |
| 973 | untrusted = cpk->chain; |
| 974 | } |
Dr. Stephen Henson | 74ecfab | 2012-07-23 23:34:28 +0000 | [diff] [blame] | 975 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 976 | if (!X509_STORE_CTX_init(&xs_ctx, chain_store, cpk->x509, untrusted)) { |
| 977 | SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, ERR_R_X509_LIB); |
| 978 | goto err; |
| 979 | } |
| 980 | /* Set suite B flags if needed */ |
| 981 | X509_STORE_CTX_set_flags(&xs_ctx, |
| 982 | c->cert_flags & SSL_CERT_FLAG_SUITEB_128_LOS); |
Dr. Stephen Henson | 74ecfab | 2012-07-23 23:34:28 +0000 | [diff] [blame] | 983 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 984 | i = X509_verify_cert(&xs_ctx); |
| 985 | if (i <= 0 && flags & SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR) { |
| 986 | if (flags & SSL_BUILD_CHAIN_FLAG_CLEAR_ERROR) |
| 987 | ERR_clear_error(); |
| 988 | i = 1; |
| 989 | rv = 2; |
| 990 | } |
| 991 | if (i > 0) |
| 992 | chain = X509_STORE_CTX_get1_chain(&xs_ctx); |
| 993 | if (i <= 0) { |
| 994 | SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, SSL_R_CERTIFICATE_VERIFY_FAILED); |
| 995 | i = X509_STORE_CTX_get_error(&xs_ctx); |
| 996 | ERR_add_error_data(2, "Verify error:", |
| 997 | X509_verify_cert_error_string(i)); |
Dr. Stephen Henson | 13dc3ce | 2014-02-23 12:00:18 +0000 | [diff] [blame] | 998 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 999 | X509_STORE_CTX_cleanup(&xs_ctx); |
| 1000 | goto err; |
| 1001 | } |
| 1002 | X509_STORE_CTX_cleanup(&xs_ctx); |
| 1003 | /* Remove EE certificate from chain */ |
| 1004 | x = sk_X509_shift(chain); |
| 1005 | X509_free(x); |
| 1006 | if (flags & SSL_BUILD_CHAIN_FLAG_NO_ROOT) { |
| 1007 | if (sk_X509_num(chain) > 0) { |
| 1008 | /* See if last cert is self signed */ |
| 1009 | x = sk_X509_value(chain, sk_X509_num(chain) - 1); |
Dr. Stephen Henson | a8d8e06 | 2015-09-02 22:01:18 +0100 | [diff] [blame] | 1010 | if (X509_get_extension_flags(x) & EXFLAG_SS) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1011 | x = sk_X509_pop(chain); |
| 1012 | X509_free(x); |
| 1013 | } |
| 1014 | } |
| 1015 | } |
| 1016 | /* |
| 1017 | * Check security level of all CA certificates: EE will have been checked |
| 1018 | * already. |
| 1019 | */ |
| 1020 | for (i = 0; i < sk_X509_num(chain); i++) { |
| 1021 | x = sk_X509_value(chain, i); |
| 1022 | rv = ssl_security_cert(s, ctx, x, 0, 0); |
| 1023 | if (rv != 1) { |
| 1024 | SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, rv); |
| 1025 | sk_X509_pop_free(chain, X509_free); |
| 1026 | rv = 0; |
| 1027 | goto err; |
| 1028 | } |
| 1029 | } |
Rich Salz | 222561f | 2015-04-30 17:33:59 -0400 | [diff] [blame] | 1030 | sk_X509_pop_free(cpk->chain, X509_free); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1031 | cpk->chain = chain; |
| 1032 | if (rv == 0) |
| 1033 | rv = 1; |
| 1034 | err: |
| 1035 | if (flags & SSL_BUILD_CHAIN_FLAG_CHECK) |
| 1036 | X509_STORE_free(chain_store); |
Dr. Stephen Henson | 74ecfab | 2012-07-23 23:34:28 +0000 | [diff] [blame] | 1037 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1038 | return rv; |
| 1039 | } |
Dr. Stephen Henson | 74ecfab | 2012-07-23 23:34:28 +0000 | [diff] [blame] | 1040 | |
| 1041 | 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] | 1042 | { |
| 1043 | X509_STORE **pstore; |
| 1044 | if (chain) |
| 1045 | pstore = &c->chain_store; |
| 1046 | else |
| 1047 | pstore = &c->verify_store; |
Rich Salz | 222561f | 2015-04-30 17:33:59 -0400 | [diff] [blame] | 1048 | X509_STORE_free(*pstore); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1049 | *pstore = store; |
| 1050 | if (ref && store) |
| 1051 | CRYPTO_add(&store->references, 1, CRYPTO_LOCK_X509_STORE); |
| 1052 | return 1; |
| 1053 | } |
Dr. Stephen Henson | 74ecfab | 2012-07-23 23:34:28 +0000 | [diff] [blame] | 1054 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1055 | static int ssl_security_default_callback(SSL *s, SSL_CTX *ctx, int op, |
| 1056 | int bits, int nid, void *other, |
| 1057 | void *ex) |
| 1058 | { |
| 1059 | int level, minbits; |
| 1060 | static const int minbits_table[5] = { 80, 112, 128, 192, 256 }; |
| 1061 | if (ctx) |
| 1062 | level = SSL_CTX_get_security_level(ctx); |
| 1063 | else |
| 1064 | level = SSL_get_security_level(s); |
Viktor Dukhovni | a7cf07b | 2016-01-14 01:16:16 -0500 | [diff] [blame] | 1065 | |
| 1066 | if (level <= 0) { |
| 1067 | /* |
| 1068 | * No EDH keys weaker than 1024-bits even at level 0, otherwise, |
| 1069 | * anything goes. |
| 1070 | */ |
| 1071 | if (op == SSL_SECOP_TMP_DH && bits < 80) |
| 1072 | return 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1073 | return 1; |
Viktor Dukhovni | a7cf07b | 2016-01-14 01:16:16 -0500 | [diff] [blame] | 1074 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1075 | if (level > 5) |
| 1076 | level = 5; |
| 1077 | minbits = minbits_table[level - 1]; |
| 1078 | switch (op) { |
| 1079 | case SSL_SECOP_CIPHER_SUPPORTED: |
| 1080 | case SSL_SECOP_CIPHER_SHARED: |
| 1081 | case SSL_SECOP_CIPHER_CHECK: |
| 1082 | { |
| 1083 | const SSL_CIPHER *c = other; |
| 1084 | /* No ciphers below security level */ |
| 1085 | if (bits < minbits) |
| 1086 | return 0; |
| 1087 | /* No unauthenticated ciphersuites */ |
| 1088 | if (c->algorithm_auth & SSL_aNULL) |
| 1089 | return 0; |
| 1090 | /* No MD5 mac ciphersuites */ |
| 1091 | if (c->algorithm_mac & SSL_MD5) |
| 1092 | return 0; |
| 1093 | /* SHA1 HMAC is 160 bits of security */ |
| 1094 | if (minbits > 160 && c->algorithm_mac & SSL_SHA1) |
| 1095 | return 0; |
| 1096 | /* Level 2: no RC4 */ |
| 1097 | if (level >= 2 && c->algorithm_enc == SSL_RC4) |
| 1098 | return 0; |
| 1099 | /* Level 3: forward secure ciphersuites only */ |
| 1100 | if (level >= 3 && !(c->algorithm_mkey & (SSL_kEDH | SSL_kEECDH))) |
| 1101 | return 0; |
| 1102 | break; |
| 1103 | } |
| 1104 | case SSL_SECOP_VERSION: |
Viktor Dukhovni | 4fa5214 | 2015-12-29 03:24:17 -0500 | [diff] [blame] | 1105 | if (!SSL_IS_DTLS(s)) { |
| 1106 | /* SSLv3 not allowed at level 2 */ |
| 1107 | if (nid <= SSL3_VERSION && level >= 2) |
| 1108 | return 0; |
| 1109 | /* TLS v1.1 and above only for level 3 */ |
| 1110 | if (nid <= TLS1_VERSION && level >= 3) |
| 1111 | return 0; |
| 1112 | /* TLS v1.2 only for level 4 and above */ |
| 1113 | if (nid <= TLS1_1_VERSION && level >= 4) |
| 1114 | return 0; |
| 1115 | } else { |
| 1116 | /* DTLS v1.2 only for level 4 and above */ |
| 1117 | if (DTLS_VERSION_LT(nid, DTLS1_2_VERSION) && level >= 4) |
| 1118 | return 0; |
| 1119 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1120 | break; |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 1121 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1122 | case SSL_SECOP_COMPRESSION: |
| 1123 | if (level >= 2) |
| 1124 | return 0; |
| 1125 | break; |
| 1126 | case SSL_SECOP_TICKET: |
| 1127 | if (level >= 3) |
| 1128 | return 0; |
| 1129 | break; |
| 1130 | default: |
| 1131 | if (bits < minbits) |
| 1132 | return 0; |
| 1133 | } |
| 1134 | return 1; |
| 1135 | } |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 1136 | |
| 1137 | 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] | 1138 | { |
| 1139 | return s->cert->sec_cb(s, NULL, op, bits, nid, other, s->cert->sec_ex); |
| 1140 | } |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 1141 | |
| 1142 | 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] | 1143 | { |
| 1144 | return ctx->cert->sec_cb(NULL, ctx, op, bits, nid, other, |
| 1145 | ctx->cert->sec_ex); |
| 1146 | } |