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