Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 2 | * All rights reserved. |
| 3 | * |
| 4 | * This package is an SSL implementation written |
| 5 | * by Eric Young (eay@cryptsoft.com). |
| 6 | * The implementation was written so as to conform with Netscapes SSL. |
| 7 | * |
| 8 | * This library is free for commercial and non-commercial use as long as |
| 9 | * the following conditions are aheared to. The following conditions |
| 10 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 12 | * included with this distribution is covered by the same copyright terms |
| 13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 14 | * |
| 15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 16 | * the code are not to be removed. |
| 17 | * If this package is used in a product, Eric Young should be given attribution |
| 18 | * as the author of the parts of the library used. |
| 19 | * This can be in the form of a textual message at program startup or |
| 20 | * in documentation (online or textual) provided with the package. |
| 21 | * |
| 22 | * Redistribution and use in source and binary forms, with or without |
| 23 | * modification, are permitted provided that the following conditions |
| 24 | * are met: |
| 25 | * 1. Redistributions of source code must retain the copyright |
| 26 | * notice, this list of conditions and the following disclaimer. |
| 27 | * 2. Redistributions in binary form must reproduce the above copyright |
| 28 | * notice, this list of conditions and the following disclaimer in the |
| 29 | * documentation and/or other materials provided with the distribution. |
| 30 | * 3. All advertising materials mentioning features or use of this software |
| 31 | * must display the following acknowledgement: |
| 32 | * "This product includes cryptographic software written by |
| 33 | * Eric Young (eay@cryptsoft.com)" |
| 34 | * The word 'cryptographic' can be left out if the rouines from the library |
| 35 | * being used are not cryptographic related :-). |
| 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 37 | * the apps directory (application code) you must include an acknowledgement: |
| 38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 39 | * |
| 40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 50 | * SUCH DAMAGE. |
| 51 | * |
| 52 | * The licence and distribution terms for any publically available version or |
| 53 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 54 | * copied and put under another distribution licence |
| 55 | * [including the GNU Public Licence.] |
| 56 | */ |
| 57 | /* ==================================================================== |
| 58 | * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. |
| 59 | * |
| 60 | * Redistribution and use in source and binary forms, with or without |
| 61 | * modification, are permitted provided that the following conditions |
| 62 | * are met: |
| 63 | * |
| 64 | * 1. Redistributions of source code must retain the above copyright |
| 65 | * notice, this list of conditions and the following disclaimer. |
| 66 | * |
| 67 | * 2. Redistributions in binary form must reproduce the above copyright |
| 68 | * notice, this list of conditions and the following disclaimer in |
| 69 | * the documentation and/or other materials provided with the |
| 70 | * distribution. |
| 71 | * |
| 72 | * 3. All advertising materials mentioning features or use of this |
| 73 | * software must display the following acknowledgment: |
| 74 | * "This product includes software developed by the OpenSSL Project |
| 75 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
| 76 | * |
| 77 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 78 | * endorse or promote products derived from this software without |
| 79 | * prior written permission. For written permission, please contact |
| 80 | * openssl-core@openssl.org. |
| 81 | * |
| 82 | * 5. Products derived from this software may not be called "OpenSSL" |
| 83 | * nor may "OpenSSL" appear in their names without prior written |
| 84 | * permission of the OpenSSL Project. |
| 85 | * |
| 86 | * 6. Redistributions of any form whatsoever must retain the following |
| 87 | * acknowledgment: |
| 88 | * "This product includes software developed by the OpenSSL Project |
| 89 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
| 90 | * |
| 91 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 92 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 93 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 94 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 95 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 96 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 97 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 98 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 99 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 100 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 101 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 102 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 103 | * ==================================================================== |
| 104 | * |
| 105 | * This product includes cryptographic software written by Eric Young |
| 106 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 107 | * Hudson (tjh@cryptsoft.com). |
| 108 | * |
| 109 | */ |
| 110 | /* ==================================================================== |
| 111 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. |
| 112 | * ECC cipher suite support in OpenSSL originally developed by |
| 113 | * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. */ |
| 114 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 115 | #include <openssl/ssl.h> |
| 116 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 117 | #include <assert.h> |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 118 | #include <string.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 119 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 120 | #include <openssl/bn.h> |
| 121 | #include <openssl/buf.h> |
Adam Langley | 56d2503 | 2015-06-23 16:20:13 -0700 | [diff] [blame] | 122 | #include <openssl/ec_key.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 123 | #include <openssl/dh.h> |
| 124 | #include <openssl/err.h> |
| 125 | #include <openssl/mem.h> |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 126 | #include <openssl/sha.h> |
Adam Langley | 56d2503 | 2015-06-23 16:20:13 -0700 | [diff] [blame] | 127 | #include <openssl/x509.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 128 | #include <openssl/x509v3.h> |
| 129 | |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 130 | #include "../crypto/internal.h" |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 131 | #include "internal.h" |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 132 | |
| 133 | |
| 134 | int SSL_get_ex_data_X509_STORE_CTX_idx(void) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 135 | /* The ex_data index to go from |X509_STORE_CTX| to |SSL| always uses the |
| 136 | * reserved app_data slot. Before ex_data was introduced, app_data was used. |
| 137 | * Avoid breaking any software which assumes |X509_STORE_CTX_get_app_data| |
| 138 | * works. */ |
| 139 | return 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | CERT *ssl_cert_new(void) { |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 143 | CERT *ret = OPENSSL_malloc(sizeof(CERT)); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 144 | if (ret == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 145 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 146 | return NULL; |
| 147 | } |
| 148 | memset(ret, 0, sizeof(CERT)); |
| 149 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 150 | return ret; |
| 151 | } |
| 152 | |
| 153 | CERT *ssl_cert_dup(CERT *cert) { |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 154 | CERT *ret = OPENSSL_malloc(sizeof(CERT)); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 155 | if (ret == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 156 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 157 | return NULL; |
| 158 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 159 | memset(ret, 0, sizeof(CERT)); |
| 160 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 161 | if (cert->x509 != NULL) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 162 | X509_up_ref(cert->x509); |
| 163 | ret->x509 = cert->x509; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 164 | } |
| 165 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 166 | if (cert->privatekey != NULL) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 167 | EVP_PKEY_up_ref(cert->privatekey); |
| 168 | ret->privatekey = cert->privatekey; |
Kenny Root | a04d78d | 2015-09-25 00:26:37 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 171 | if (cert->chain) { |
| 172 | ret->chain = X509_chain_up_ref(cert->chain); |
| 173 | if (!ret->chain) { |
| 174 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Kenny Root | a04d78d | 2015-09-25 00:26:37 +0000 | [diff] [blame] | 175 | goto err; |
| 176 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 177 | } |
| 178 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 179 | ret->key_method = cert->key_method; |
| 180 | |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame^] | 181 | ret->mask_k = cert->mask_k; |
| 182 | ret->mask_a = cert->mask_a; |
| 183 | |
| 184 | if (cert->dh_tmp != NULL) { |
| 185 | ret->dh_tmp = DHparams_dup(cert->dh_tmp); |
| 186 | if (ret->dh_tmp == NULL) { |
| 187 | OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB); |
| 188 | goto err; |
| 189 | } |
| 190 | } |
| 191 | ret->dh_tmp_cb = cert->dh_tmp_cb; |
| 192 | |
| 193 | if (cert->sigalgs != NULL) { |
| 194 | ret->sigalgs = |
| 195 | BUF_memdup(cert->sigalgs, cert->num_sigalgs * sizeof(cert->sigalgs[0])); |
| 196 | if (ret->sigalgs == NULL) { |
| 197 | goto err; |
| 198 | } |
| 199 | } |
| 200 | ret->num_sigalgs = cert->num_sigalgs; |
| 201 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 202 | ret->cert_cb = cert->cert_cb; |
| 203 | ret->cert_cb_arg = cert->cert_cb_arg; |
| 204 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 205 | if (cert->verify_store != NULL) { |
| 206 | X509_STORE_up_ref(cert->verify_store); |
| 207 | ret->verify_store = cert->verify_store; |
| 208 | } |
| 209 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 210 | return ret; |
| 211 | |
| 212 | err: |
| 213 | ssl_cert_free(ret); |
| 214 | return NULL; |
| 215 | } |
| 216 | |
| 217 | /* Free up and clear all certificates and chains */ |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 218 | void ssl_cert_clear_certs(CERT *cert) { |
| 219 | if (cert == NULL) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 220 | return; |
| 221 | } |
| 222 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 223 | X509_free(cert->x509); |
| 224 | cert->x509 = NULL; |
| 225 | EVP_PKEY_free(cert->privatekey); |
| 226 | cert->privatekey = NULL; |
| 227 | sk_X509_pop_free(cert->chain, X509_free); |
| 228 | cert->chain = NULL; |
| 229 | cert->key_method = NULL; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | void ssl_cert_free(CERT *c) { |
| 233 | if (c == NULL) { |
| 234 | return; |
| 235 | } |
| 236 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 237 | DH_free(c->dh_tmp); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 238 | |
| 239 | ssl_cert_clear_certs(c); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 240 | OPENSSL_free(c->sigalgs); |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 241 | X509_STORE_free(c->verify_store); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 242 | |
| 243 | OPENSSL_free(c); |
| 244 | } |
| 245 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 246 | int ssl_cert_set0_chain(CERT *cert, STACK_OF(X509) *chain) { |
| 247 | sk_X509_pop_free(cert->chain, X509_free); |
| 248 | cert->chain = chain; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 249 | return 1; |
| 250 | } |
| 251 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 252 | int ssl_cert_set1_chain(CERT *cert, STACK_OF(X509) *chain) { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 253 | STACK_OF(X509) *dchain; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 254 | if (chain == NULL) { |
| 255 | return ssl_cert_set0_chain(cert, NULL); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | dchain = X509_chain_up_ref(chain); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 259 | if (dchain == NULL) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 260 | return 0; |
| 261 | } |
| 262 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 263 | if (!ssl_cert_set0_chain(cert, dchain)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 264 | sk_X509_pop_free(dchain, X509_free); |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | return 1; |
| 269 | } |
| 270 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 271 | int ssl_cert_add0_chain_cert(CERT *cert, X509 *x509) { |
| 272 | if (cert->chain == NULL) { |
| 273 | cert->chain = sk_X509_new_null(); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 274 | } |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 275 | if (cert->chain == NULL || !sk_X509_push(cert->chain, x509)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | return 1; |
| 280 | } |
| 281 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 282 | int ssl_cert_add1_chain_cert(CERT *cert, X509 *x509) { |
| 283 | if (!ssl_cert_add0_chain_cert(cert, x509)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 284 | return 0; |
| 285 | } |
| 286 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 287 | X509_up_ref(x509); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 288 | return 1; |
| 289 | } |
| 290 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 291 | void ssl_cert_set_cert_cb(CERT *c, int (*cb)(SSL *ssl, void *arg), void *arg) { |
| 292 | c->cert_cb = cb; |
| 293 | c->cert_cb_arg = arg; |
| 294 | } |
| 295 | |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame^] | 296 | int ssl_verify_cert_chain(SSL *ssl, long *out_verify_result, |
| 297 | STACK_OF(X509) *cert_chain) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 298 | if (cert_chain == NULL || sk_X509_num(cert_chain) == 0) { |
| 299 | return 0; |
Kenny Root | a04d78d | 2015-09-25 00:26:37 +0000 | [diff] [blame] | 300 | } |
| 301 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 302 | X509_STORE *verify_store = ssl->ctx->cert_store; |
| 303 | if (ssl->cert->verify_store != NULL) { |
| 304 | verify_store = ssl->cert->verify_store; |
| 305 | } |
| 306 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 307 | X509 *leaf = sk_X509_value(cert_chain, 0); |
| 308 | int ret = 0; |
Kenny Root | a04d78d | 2015-09-25 00:26:37 +0000 | [diff] [blame] | 309 | X509_STORE_CTX ctx; |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 310 | if (!X509_STORE_CTX_init(&ctx, verify_store, leaf, cert_chain)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 311 | OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB); |
Adam Langley | 1e4884f | 2015-09-24 10:57:52 -0700 | [diff] [blame] | 312 | return 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 313 | } |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 314 | if (!X509_STORE_CTX_set_ex_data(&ctx, SSL_get_ex_data_X509_STORE_CTX_idx(), |
| 315 | ssl)) { |
| 316 | goto err; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 317 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 318 | |
| 319 | /* We need to inherit the verify parameters. These can be determined by the |
| 320 | * context: if its a server it will verify SSL client certificates or vice |
| 321 | * versa. */ |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 322 | X509_STORE_CTX_set_default(&ctx, ssl->server ? "ssl_client" : "ssl_server"); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 323 | |
| 324 | /* Anything non-default in "param" should overwrite anything in the ctx. */ |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 325 | X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(&ctx), ssl->param); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 326 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 327 | if (ssl->verify_callback) { |
| 328 | X509_STORE_CTX_set_verify_cb(&ctx, ssl->verify_callback); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 329 | } |
| 330 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 331 | int verify_ret; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 332 | if (ssl->ctx->app_verify_callback != NULL) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 333 | verify_ret = ssl->ctx->app_verify_callback(&ctx, ssl->ctx->app_verify_arg); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 334 | } else { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 335 | verify_ret = X509_verify_cert(&ctx); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 336 | } |
| 337 | |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame^] | 338 | *out_verify_result = ctx.error; |
Kenny Root | a04d78d | 2015-09-25 00:26:37 +0000 | [diff] [blame] | 339 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 340 | /* If |SSL_VERIFY_NONE|, the error is non-fatal, but we keep the result. */ |
| 341 | if (verify_ret <= 0 && ssl->verify_mode != SSL_VERIFY_NONE) { |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame^] | 342 | ssl3_send_alert(ssl, SSL3_AL_FATAL, ssl_verify_alarm_type(ctx.error)); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 343 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_VERIFY_FAILED); |
| 344 | goto err; |
| 345 | } |
| 346 | |
| 347 | ERR_clear_error(); |
| 348 | ret = 1; |
| 349 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 350 | err: |
| 351 | X509_STORE_CTX_cleanup(&ctx); |
| 352 | return ret; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 353 | } |
| 354 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 355 | static void set_client_CA_list(STACK_OF(X509_NAME) **ca_list, |
| 356 | STACK_OF(X509_NAME) *name_list) { |
| 357 | sk_X509_NAME_pop_free(*ca_list, X509_NAME_free); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 358 | *ca_list = name_list; |
| 359 | } |
| 360 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 361 | STACK_OF(X509_NAME) *SSL_dup_CA_list(STACK_OF(X509_NAME) *list) { |
| 362 | STACK_OF(X509_NAME) *ret = sk_X509_NAME_new_null(); |
| 363 | if (ret == NULL) { |
| 364 | return NULL; |
| 365 | } |
Kenny Root | a04d78d | 2015-09-25 00:26:37 +0000 | [diff] [blame] | 366 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 367 | size_t i; |
| 368 | for (i = 0; i < sk_X509_NAME_num(list); i++) { |
| 369 | X509_NAME *name = X509_NAME_dup(sk_X509_NAME_value(list, i)); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 370 | if (name == NULL || !sk_X509_NAME_push(ret, name)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 371 | X509_NAME_free(name); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 372 | sk_X509_NAME_pop_free(ret, X509_NAME_free); |
| 373 | return NULL; |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | return ret; |
| 378 | } |
| 379 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 380 | void SSL_set_client_CA_list(SSL *ssl, STACK_OF(X509_NAME) *name_list) { |
| 381 | set_client_CA_list(&ssl->client_CA, name_list); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 382 | } |
| 383 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 384 | void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 385 | set_client_CA_list(&ctx->client_CA, name_list); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 386 | } |
| 387 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 388 | STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 389 | return ctx->client_CA; |
| 390 | } |
| 391 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 392 | STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *ssl) { |
| 393 | /* For historical reasons, this function is used both to query configuration |
| 394 | * state on a server as well as handshake state on a client. However, whether |
| 395 | * |ssl| is a client or server is not known until explicitly configured with |
| 396 | * |SSL_set_connect_state|. If |handshake_func| is NULL, |ssl| is in an |
| 397 | * indeterminate mode and |ssl->server| is unset. */ |
| 398 | if (ssl->handshake_func != NULL && !ssl->server) { |
| 399 | return ssl->s3->tmp.ca_names; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 400 | } |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 401 | |
| 402 | if (ssl->client_CA != NULL) { |
| 403 | return ssl->client_CA; |
| 404 | } |
| 405 | return ssl->ctx->client_CA; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 406 | } |
| 407 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 408 | static int add_client_CA(STACK_OF(X509_NAME) **sk, X509 *x509) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 409 | X509_NAME *name; |
| 410 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 411 | if (x509 == NULL) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 412 | return 0; |
| 413 | } |
| 414 | if (*sk == NULL) { |
| 415 | *sk = sk_X509_NAME_new_null(); |
| 416 | if (*sk == NULL) { |
| 417 | return 0; |
| 418 | } |
| 419 | } |
| 420 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 421 | name = X509_NAME_dup(X509_get_subject_name(x509)); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 422 | if (name == NULL) { |
| 423 | return 0; |
| 424 | } |
| 425 | |
| 426 | if (!sk_X509_NAME_push(*sk, name)) { |
| 427 | X509_NAME_free(name); |
| 428 | return 0; |
| 429 | } |
| 430 | |
| 431 | return 1; |
| 432 | } |
| 433 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 434 | int SSL_add_client_CA(SSL *ssl, X509 *x509) { |
| 435 | return add_client_CA(&ssl->client_CA, x509); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 436 | } |
| 437 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 438 | int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x509) { |
| 439 | return add_client_CA(&ctx->client_CA, x509); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 440 | } |
| 441 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 442 | int ssl_has_certificate(const SSL *ssl) { |
| 443 | return ssl->cert->x509 != NULL && ssl_has_private_key(ssl); |
| 444 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 445 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 446 | STACK_OF(X509) *ssl_parse_cert_chain(SSL *ssl, uint8_t *out_alert, |
| 447 | uint8_t *out_leaf_sha256, CBS *cbs) { |
| 448 | STACK_OF(X509) *ret = sk_X509_new_null(); |
| 449 | if (ret == NULL) { |
| 450 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 451 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 452 | return NULL; |
| 453 | } |
| 454 | |
| 455 | X509 *x = NULL; |
| 456 | CBS certificate_list; |
| 457 | if (!CBS_get_u24_length_prefixed(cbs, &certificate_list)) { |
| 458 | *out_alert = SSL_AD_DECODE_ERROR; |
| 459 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 460 | goto err; |
| 461 | } |
| 462 | |
| 463 | while (CBS_len(&certificate_list) > 0) { |
| 464 | CBS certificate; |
| 465 | if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) { |
| 466 | *out_alert = SSL_AD_DECODE_ERROR; |
| 467 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH); |
| 468 | goto err; |
| 469 | } |
| 470 | |
| 471 | /* Retain the hash of the leaf certificate if requested. */ |
| 472 | if (sk_X509_num(ret) == 0 && out_leaf_sha256 != NULL) { |
| 473 | SHA256(CBS_data(&certificate), CBS_len(&certificate), out_leaf_sha256); |
| 474 | } |
| 475 | |
| 476 | /* A u24 length cannot overflow a long. */ |
| 477 | const uint8_t *data = CBS_data(&certificate); |
| 478 | x = d2i_X509(NULL, &data, (long)CBS_len(&certificate)); |
| 479 | if (x == NULL || data != CBS_data(&certificate) + CBS_len(&certificate)) { |
| 480 | *out_alert = SSL_AD_DECODE_ERROR; |
| 481 | goto err; |
| 482 | } |
| 483 | if (!sk_X509_push(ret, x)) { |
| 484 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 485 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 486 | goto err; |
| 487 | } |
| 488 | x = NULL; |
| 489 | } |
| 490 | |
| 491 | return ret; |
| 492 | |
| 493 | err: |
| 494 | X509_free(x); |
| 495 | sk_X509_pop_free(ret, X509_free); |
| 496 | return NULL; |
| 497 | } |
| 498 | |
| 499 | int ssl_add_cert_to_cbb(CBB *cbb, X509 *x509) { |
| 500 | int len = i2d_X509(x509, NULL); |
| 501 | if (len < 0) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 502 | return 0; |
| 503 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 504 | uint8_t *buf; |
| 505 | if (!CBB_add_space(cbb, &buf, len)) { |
| 506 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 507 | return 0; |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 508 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 509 | if (buf != NULL && i2d_X509(x509, &buf) < 0) { |
| 510 | return 0; |
| 511 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 512 | return 1; |
| 513 | } |
| 514 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 515 | static int ssl_add_cert_with_length(CBB *cbb, X509 *x509) { |
| 516 | CBB child; |
| 517 | return CBB_add_u24_length_prefixed(cbb, &child) && |
| 518 | ssl_add_cert_to_cbb(&child, x509) && |
| 519 | CBB_flush(cbb); |
| 520 | } |
| 521 | |
| 522 | int ssl_add_cert_chain(SSL *ssl, CBB *cbb) { |
| 523 | if (!ssl_has_certificate(ssl)) { |
| 524 | return CBB_add_u24(cbb, 0); |
| 525 | } |
| 526 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 527 | CERT *cert = ssl->cert; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 528 | X509 *x = cert->x509; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 529 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 530 | CBB child; |
| 531 | if (!CBB_add_u24_length_prefixed(cbb, &child)) { |
| 532 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 533 | return 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 534 | } |
| 535 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 536 | int no_chain = 0; |
| 537 | STACK_OF(X509) *chain = cert->chain; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 538 | if ((ssl->mode & SSL_MODE_NO_AUTO_CHAIN) || chain != NULL) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 539 | no_chain = 1; |
| 540 | } |
| 541 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 542 | if (no_chain) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 543 | if (!ssl_add_cert_with_length(&child, x)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 544 | return 0; |
| 545 | } |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 546 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 547 | size_t i; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 548 | for (i = 0; i < sk_X509_num(chain); i++) { |
| 549 | x = sk_X509_value(chain, i); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 550 | if (!ssl_add_cert_with_length(&child, x)) { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 551 | return 0; |
| 552 | } |
| 553 | } |
| 554 | } else { |
| 555 | X509_STORE_CTX xs_ctx; |
| 556 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 557 | if (!X509_STORE_CTX_init(&xs_ctx, ssl->ctx->cert_store, x, NULL)) { |
| 558 | OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 559 | return 0; |
| 560 | } |
| 561 | X509_verify_cert(&xs_ctx); |
| 562 | /* Don't leave errors in the queue */ |
| 563 | ERR_clear_error(); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 564 | |
| 565 | size_t i; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 566 | for (i = 0; i < sk_X509_num(xs_ctx.chain); i++) { |
| 567 | x = sk_X509_value(xs_ctx.chain, i); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 568 | if (!ssl_add_cert_with_length(&child, x)) { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 569 | X509_STORE_CTX_cleanup(&xs_ctx); |
| 570 | return 0; |
| 571 | } |
| 572 | } |
| 573 | X509_STORE_CTX_cleanup(&xs_ctx); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 574 | } |
| 575 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 576 | return CBB_flush(cbb); |
| 577 | } |
| 578 | |
| 579 | static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b) { |
| 580 | return X509_NAME_cmp(*a, *b); |
| 581 | } |
| 582 | |
| 583 | STACK_OF(X509_NAME) * |
| 584 | ssl_parse_client_CA_list(SSL *ssl, uint8_t *out_alert, CBS *cbs) { |
| 585 | STACK_OF(X509_NAME) *ret = sk_X509_NAME_new(ca_dn_cmp); |
| 586 | X509_NAME *name = NULL; |
| 587 | if (ret == NULL) { |
| 588 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 589 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 590 | return NULL; |
| 591 | } |
| 592 | |
| 593 | CBS child; |
| 594 | if (!CBS_get_u16_length_prefixed(cbs, &child)) { |
| 595 | *out_alert = SSL_AD_DECODE_ERROR; |
| 596 | OPENSSL_PUT_ERROR(SSL, SSL_R_LENGTH_MISMATCH); |
| 597 | goto err; |
| 598 | } |
| 599 | |
| 600 | while (CBS_len(&child) > 0) { |
| 601 | CBS distinguished_name; |
| 602 | if (!CBS_get_u16_length_prefixed(&child, &distinguished_name)) { |
| 603 | *out_alert = SSL_AD_DECODE_ERROR; |
| 604 | OPENSSL_PUT_ERROR(SSL, SSL_R_CA_DN_TOO_LONG); |
| 605 | goto err; |
| 606 | } |
| 607 | |
| 608 | const uint8_t *ptr = CBS_data(&distinguished_name); |
| 609 | /* A u16 length cannot overflow a long. */ |
| 610 | name = d2i_X509_NAME(NULL, &ptr, (long)CBS_len(&distinguished_name)); |
| 611 | if (name == NULL || |
| 612 | ptr != CBS_data(&distinguished_name) + CBS_len(&distinguished_name)) { |
| 613 | *out_alert = SSL_AD_DECODE_ERROR; |
| 614 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 615 | goto err; |
| 616 | } |
| 617 | |
| 618 | if (!sk_X509_NAME_push(ret, name)) { |
| 619 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 620 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 621 | goto err; |
| 622 | } |
| 623 | name = NULL; |
| 624 | } |
| 625 | |
| 626 | return ret; |
| 627 | |
| 628 | err: |
| 629 | X509_NAME_free(name); |
| 630 | sk_X509_NAME_pop_free(ret, X509_NAME_free); |
| 631 | return NULL; |
| 632 | } |
| 633 | |
| 634 | int ssl_add_client_CA_list(SSL *ssl, CBB *cbb) { |
| 635 | CBB child, name_cbb; |
| 636 | if (!CBB_add_u16_length_prefixed(cbb, &child)) { |
| 637 | return 0; |
| 638 | } |
| 639 | |
| 640 | STACK_OF(X509_NAME) *sk = SSL_get_client_CA_list(ssl); |
| 641 | if (sk == NULL) { |
| 642 | return CBB_flush(cbb); |
| 643 | } |
| 644 | |
| 645 | for (size_t i = 0; i < sk_X509_NAME_num(sk); i++) { |
| 646 | X509_NAME *name = sk_X509_NAME_value(sk, i); |
| 647 | int len = i2d_X509_NAME(name, NULL); |
| 648 | if (len < 0) { |
| 649 | return 0; |
| 650 | } |
| 651 | uint8_t *ptr; |
| 652 | if (!CBB_add_u16_length_prefixed(&child, &name_cbb) || |
| 653 | !CBB_add_space(&name_cbb, &ptr, (size_t)len) || |
| 654 | (len > 0 && i2d_X509_NAME(name, &ptr) < 0)) { |
| 655 | return 0; |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | return CBB_flush(cbb); |
| 660 | } |
| 661 | |
| 662 | int ssl_do_client_cert_cb(SSL *ssl, int *out_should_retry) { |
| 663 | if (ssl_has_certificate(ssl) || ssl->ctx->client_cert_cb == NULL) { |
| 664 | return 1; |
| 665 | } |
| 666 | |
| 667 | X509 *x509 = NULL; |
| 668 | EVP_PKEY *pkey = NULL; |
| 669 | int ret = ssl->ctx->client_cert_cb(ssl, &x509, &pkey); |
| 670 | if (ret < 0) { |
| 671 | *out_should_retry = 1; |
| 672 | return 0; |
| 673 | } |
| 674 | |
| 675 | if (ret != 0) { |
| 676 | if (!SSL_use_certificate(ssl, x509) || |
| 677 | !SSL_use_PrivateKey(ssl, pkey)) { |
| 678 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 679 | *out_should_retry = 0; |
| 680 | return 0; |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | X509_free(x509); |
| 685 | EVP_PKEY_free(pkey); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 686 | return 1; |
| 687 | } |
| 688 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 689 | static int set_cert_store(X509_STORE **store_ptr, X509_STORE *new_store, int take_ref) { |
| 690 | X509_STORE_free(*store_ptr); |
| 691 | *store_ptr = new_store; |
| 692 | |
| 693 | if (new_store != NULL && take_ref) { |
| 694 | X509_STORE_up_ref(new_store); |
| 695 | } |
| 696 | |
| 697 | return 1; |
| 698 | } |
| 699 | |
| 700 | int SSL_CTX_set0_verify_cert_store(SSL_CTX *ctx, X509_STORE *store) { |
| 701 | return set_cert_store(&ctx->cert->verify_store, store, 0); |
| 702 | } |
| 703 | |
| 704 | int SSL_CTX_set1_verify_cert_store(SSL_CTX *ctx, X509_STORE *store) { |
| 705 | return set_cert_store(&ctx->cert->verify_store, store, 1); |
| 706 | } |
| 707 | |
| 708 | int SSL_set0_verify_cert_store(SSL *ssl, X509_STORE *store) { |
| 709 | return set_cert_store(&ssl->cert->verify_store, store, 0); |
| 710 | } |
| 711 | |
| 712 | int SSL_set1_verify_cert_store(SSL *ssl, X509_STORE *store) { |
| 713 | return set_cert_store(&ssl->cert->verify_store, store, 1); |
| 714 | } |
| 715 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 716 | int SSL_CTX_set0_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) { |
| 717 | return ssl_cert_set0_chain(ctx->cert, chain); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 718 | } |
| 719 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 720 | int SSL_CTX_set1_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) { |
| 721 | return ssl_cert_set1_chain(ctx->cert, chain); |
| 722 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 723 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 724 | int SSL_set0_chain(SSL *ssl, STACK_OF(X509) *chain) { |
| 725 | return ssl_cert_set0_chain(ssl->cert, chain); |
| 726 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 727 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 728 | int SSL_set1_chain(SSL *ssl, STACK_OF(X509) *chain) { |
| 729 | return ssl_cert_set1_chain(ssl->cert, chain); |
| 730 | } |
| 731 | |
| 732 | int SSL_CTX_add0_chain_cert(SSL_CTX *ctx, X509 *x509) { |
| 733 | return ssl_cert_add0_chain_cert(ctx->cert, x509); |
| 734 | } |
| 735 | |
| 736 | int SSL_CTX_add1_chain_cert(SSL_CTX *ctx, X509 *x509) { |
| 737 | return ssl_cert_add1_chain_cert(ctx->cert, x509); |
| 738 | } |
| 739 | |
| 740 | int SSL_CTX_add_extra_chain_cert(SSL_CTX *ctx, X509 *x509) { |
| 741 | return SSL_CTX_add0_chain_cert(ctx, x509); |
| 742 | } |
| 743 | |
| 744 | int SSL_add0_chain_cert(SSL *ssl, X509 *x509) { |
| 745 | return ssl_cert_add0_chain_cert(ssl->cert, x509); |
| 746 | } |
| 747 | |
| 748 | int SSL_add1_chain_cert(SSL *ssl, X509 *x509) { |
| 749 | return ssl_cert_add1_chain_cert(ssl->cert, x509); |
| 750 | } |
| 751 | |
| 752 | int SSL_CTX_clear_chain_certs(SSL_CTX *ctx) { |
| 753 | return SSL_CTX_set0_chain(ctx, NULL); |
| 754 | } |
| 755 | |
| 756 | int SSL_CTX_clear_extra_chain_certs(SSL_CTX *ctx) { |
| 757 | return SSL_CTX_clear_chain_certs(ctx); |
| 758 | } |
| 759 | |
| 760 | int SSL_clear_chain_certs(SSL *ssl) { |
| 761 | return SSL_set0_chain(ssl, NULL); |
| 762 | } |
| 763 | |
| 764 | int SSL_CTX_get0_chain_certs(const SSL_CTX *ctx, STACK_OF(X509) **out_chain) { |
| 765 | *out_chain = ctx->cert->chain; |
| 766 | return 1; |
| 767 | } |
| 768 | |
| 769 | int SSL_CTX_get_extra_chain_certs(const SSL_CTX *ctx, |
| 770 | STACK_OF(X509) **out_chain) { |
| 771 | return SSL_CTX_get0_chain_certs(ctx, out_chain); |
| 772 | } |
| 773 | |
| 774 | int SSL_get0_chain_certs(const SSL *ssl, STACK_OF(X509) **out_chain) { |
| 775 | *out_chain = ssl->cert->chain; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 776 | return 1; |
| 777 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 778 | |
| 779 | int ssl_check_leaf_certificate(SSL *ssl, X509 *leaf) { |
| 780 | int ret = 0; |
| 781 | EVP_PKEY *pkey = X509_get_pubkey(leaf); |
| 782 | if (pkey == NULL) { |
| 783 | goto err; |
| 784 | } |
| 785 | |
| 786 | /* Check the certificate's type matches the cipher. */ |
| 787 | const SSL_CIPHER *cipher = ssl->s3->tmp.new_cipher; |
| 788 | int expected_type = ssl_cipher_get_key_type(cipher); |
| 789 | assert(expected_type != EVP_PKEY_NONE); |
| 790 | if (pkey->type != expected_type) { |
| 791 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CERTIFICATE_TYPE); |
| 792 | goto err; |
| 793 | } |
| 794 | |
| 795 | if (cipher->algorithm_auth & SSL_aECDSA) { |
| 796 | /* TODO(davidben): This behavior is preserved from upstream. Should key |
| 797 | * usages be checked in other cases as well? */ |
| 798 | /* This call populates the ex_flags field correctly */ |
| 799 | X509_check_purpose(leaf, -1, 0); |
| 800 | if ((leaf->ex_flags & EXFLAG_KUSAGE) && |
| 801 | !(leaf->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)) { |
| 802 | OPENSSL_PUT_ERROR(SSL, SSL_R_ECC_CERT_NOT_FOR_SIGNING); |
| 803 | goto err; |
| 804 | } |
| 805 | |
| 806 | if (!tls1_check_ec_cert(ssl, leaf)) { |
| 807 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECC_CERT); |
| 808 | goto err; |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | ret = 1; |
| 813 | |
| 814 | err: |
| 815 | EVP_PKEY_free(pkey); |
| 816 | return ret; |
| 817 | } |