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> |
David Benjamin | f0ae170 | 2015-04-07 23:05:04 -0400 | [diff] [blame] | 118 | #include <string.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 119 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 120 | #include <openssl/bn.h> |
David Benjamin | 676d1e7 | 2014-07-08 14:34:10 -0400 | [diff] [blame] | 121 | #include <openssl/buf.h> |
David Benjamin | 680ca96 | 2015-06-18 12:37:23 -0400 | [diff] [blame] | 122 | #include <openssl/ec_key.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 123 | #include <openssl/dh.h> |
| 124 | #include <openssl/err.h> |
| 125 | #include <openssl/mem.h> |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 126 | #include <openssl/sha.h> |
David Benjamin | 680ca96 | 2015-06-18 12:37:23 -0400 | [diff] [blame] | 127 | #include <openssl/x509.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 128 | #include <openssl/x509v3.h> |
| 129 | |
| 130 | #include "../crypto/dh/internal.h" |
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 | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 162 | ret->mask_k = cert->mask_k; |
| 163 | ret->mask_a = cert->mask_a; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 164 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 165 | if (cert->dh_tmp != NULL) { |
| 166 | ret->dh_tmp = DHparams_dup(cert->dh_tmp); |
| 167 | if (ret->dh_tmp == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 168 | OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 169 | goto err; |
| 170 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 171 | } |
| 172 | ret->dh_tmp_cb = cert->dh_tmp_cb; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 173 | |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 174 | if (cert->x509 != NULL) { |
| 175 | ret->x509 = X509_up_ref(cert->x509); |
| 176 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 177 | |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 178 | if (cert->privatekey != NULL) { |
Adam Langley | 310d3f6 | 2016-07-12 10:39:20 -0700 | [diff] [blame] | 179 | EVP_PKEY_up_ref(cert->privatekey); |
| 180 | ret->privatekey = cert->privatekey; |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 181 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 182 | |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 183 | if (cert->chain) { |
| 184 | ret->chain = X509_chain_up_ref(cert->chain); |
| 185 | if (!ret->chain) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 186 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 187 | goto err; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 188 | } |
| 189 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 190 | |
Tom Thorogood | 66b2fe8 | 2016-03-06 20:08:38 +1030 | [diff] [blame] | 191 | ret->key_method = cert->key_method; |
| 192 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 193 | ret->cert_cb = cert->cert_cb; |
| 194 | ret->cert_cb_arg = cert->cert_cb_arg; |
| 195 | |
Adam Langley | d323f4b | 2016-03-01 15:58:14 -0800 | [diff] [blame] | 196 | if (cert->verify_store != NULL) { |
| 197 | X509_STORE_up_ref(cert->verify_store); |
| 198 | ret->verify_store = cert->verify_store; |
| 199 | } |
| 200 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 201 | return ret; |
| 202 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 203 | err: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 204 | ssl_cert_free(ret); |
| 205 | return NULL; |
| 206 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 207 | |
| 208 | /* Free up and clear all certificates and chains */ |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 209 | void ssl_cert_clear_certs(CERT *cert) { |
| 210 | if (cert == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 211 | return; |
| 212 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 213 | |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 214 | X509_free(cert->x509); |
| 215 | cert->x509 = NULL; |
| 216 | EVP_PKEY_free(cert->privatekey); |
| 217 | cert->privatekey = NULL; |
| 218 | sk_X509_pop_free(cert->chain, X509_free); |
| 219 | cert->chain = NULL; |
David Benjamin | 71d2e54 | 2015-07-06 00:30:25 -0400 | [diff] [blame] | 220 | cert->key_method = NULL; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 221 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 222 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 223 | void ssl_cert_free(CERT *c) { |
| 224 | if (c == NULL) { |
| 225 | return; |
| 226 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 227 | |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 228 | DH_free(c->dh_tmp); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 229 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 230 | ssl_cert_clear_certs(c); |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 231 | OPENSSL_free(c->peer_sigalgs); |
David Benjamin | d246b81 | 2016-07-08 15:07:02 -0700 | [diff] [blame] | 232 | OPENSSL_free(c->sigalgs); |
Adam Langley | d323f4b | 2016-03-01 15:58:14 -0800 | [diff] [blame] | 233 | X509_STORE_free(c->verify_store); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 234 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 235 | OPENSSL_free(c); |
| 236 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 237 | |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 238 | int ssl_cert_set0_chain(CERT *cert, STACK_OF(X509) *chain) { |
| 239 | sk_X509_pop_free(cert->chain, X509_free); |
| 240 | cert->chain = chain; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 241 | return 1; |
| 242 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 243 | |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 244 | int ssl_cert_set1_chain(CERT *cert, STACK_OF(X509) *chain) { |
David Benjamin | 60da0cd | 2015-05-03 15:21:28 -0400 | [diff] [blame] | 245 | STACK_OF(X509) *dchain; |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 246 | if (chain == NULL) { |
| 247 | return ssl_cert_set0_chain(cert, NULL); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 248 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 249 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 250 | dchain = X509_chain_up_ref(chain); |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 251 | if (dchain == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 252 | return 0; |
| 253 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 254 | |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 255 | if (!ssl_cert_set0_chain(cert, dchain)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 256 | sk_X509_pop_free(dchain, X509_free); |
| 257 | return 0; |
| 258 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 259 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 260 | return 1; |
| 261 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 262 | |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 263 | int ssl_cert_add0_chain_cert(CERT *cert, X509 *x509) { |
| 264 | if (cert->chain == NULL) { |
| 265 | cert->chain = sk_X509_new_null(); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 266 | } |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 267 | if (cert->chain == NULL || !sk_X509_push(cert->chain, x509)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 268 | return 0; |
| 269 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 270 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 271 | return 1; |
| 272 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 273 | |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 274 | int ssl_cert_add1_chain_cert(CERT *cert, X509 *x509) { |
| 275 | if (!ssl_cert_add0_chain_cert(cert, x509)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 276 | return 0; |
| 277 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 278 | |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 279 | X509_up_ref(x509); |
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 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 283 | void ssl_cert_set_cert_cb(CERT *c, int (*cb)(SSL *ssl, void *arg), void *arg) { |
| 284 | c->cert_cb = cb; |
| 285 | c->cert_cb_arg = arg; |
| 286 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 287 | |
David Benjamin | 306ece3 | 2015-09-17 13:46:22 -0400 | [diff] [blame] | 288 | int ssl_verify_cert_chain(SSL *ssl, STACK_OF(X509) *cert_chain) { |
| 289 | if (cert_chain == NULL || sk_X509_num(cert_chain) == 0) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 290 | return 0; |
| 291 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 292 | |
Adam Langley | d323f4b | 2016-03-01 15:58:14 -0800 | [diff] [blame] | 293 | X509_STORE *verify_store = ssl->ctx->cert_store; |
| 294 | if (ssl->cert->verify_store != NULL) { |
| 295 | verify_store = ssl->cert->verify_store; |
| 296 | } |
| 297 | |
David Benjamin | 306ece3 | 2015-09-17 13:46:22 -0400 | [diff] [blame] | 298 | X509 *leaf = sk_X509_value(cert_chain, 0); |
| 299 | int ret = 0; |
| 300 | X509_STORE_CTX ctx; |
Adam Langley | d323f4b | 2016-03-01 15:58:14 -0800 | [diff] [blame] | 301 | if (!X509_STORE_CTX_init(&ctx, verify_store, leaf, cert_chain)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 302 | OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 303 | return 0; |
| 304 | } |
David Benjamin | 306ece3 | 2015-09-17 13:46:22 -0400 | [diff] [blame] | 305 | if (!X509_STORE_CTX_set_ex_data(&ctx, SSL_get_ex_data_X509_STORE_CTX_idx(), |
| 306 | ssl)) { |
| 307 | goto err; |
| 308 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 309 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 310 | /* We need to inherit the verify parameters. These can be determined by the |
| 311 | * context: if its a server it will verify SSL client certificates or vice |
| 312 | * versa. */ |
David Benjamin | 306ece3 | 2015-09-17 13:46:22 -0400 | [diff] [blame] | 313 | X509_STORE_CTX_set_default(&ctx, ssl->server ? "ssl_client" : "ssl_server"); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 314 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 315 | /* Anything non-default in "param" should overwrite anything in the ctx. */ |
David Benjamin | 306ece3 | 2015-09-17 13:46:22 -0400 | [diff] [blame] | 316 | X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(&ctx), ssl->param); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 317 | |
David Benjamin | 306ece3 | 2015-09-17 13:46:22 -0400 | [diff] [blame] | 318 | if (ssl->verify_callback) { |
| 319 | X509_STORE_CTX_set_verify_cb(&ctx, ssl->verify_callback); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 320 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 321 | |
David Benjamin | 306ece3 | 2015-09-17 13:46:22 -0400 | [diff] [blame] | 322 | if (ssl->ctx->app_verify_callback != NULL) { |
| 323 | ret = ssl->ctx->app_verify_callback(&ctx, ssl->ctx->app_verify_arg); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 324 | } else { |
David Benjamin | 306ece3 | 2015-09-17 13:46:22 -0400 | [diff] [blame] | 325 | ret = X509_verify_cert(&ctx); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 326 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 327 | |
David Benjamin | 306ece3 | 2015-09-17 13:46:22 -0400 | [diff] [blame] | 328 | ssl->verify_result = ctx.error; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 329 | |
David Benjamin | 306ece3 | 2015-09-17 13:46:22 -0400 | [diff] [blame] | 330 | err: |
| 331 | X509_STORE_CTX_cleanup(&ctx); |
| 332 | return ret; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 333 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 334 | |
David Benjamin | 60da0cd | 2015-05-03 15:21:28 -0400 | [diff] [blame] | 335 | static void set_client_CA_list(STACK_OF(X509_NAME) **ca_list, |
| 336 | STACK_OF(X509_NAME) *name_list) { |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 337 | sk_X509_NAME_pop_free(*ca_list, X509_NAME_free); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 338 | *ca_list = name_list; |
| 339 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 340 | |
David Benjamin | 5993704 | 2015-09-19 13:04:22 -0400 | [diff] [blame] | 341 | STACK_OF(X509_NAME) *SSL_dup_CA_list(STACK_OF(X509_NAME) *list) { |
| 342 | STACK_OF(X509_NAME) *ret = sk_X509_NAME_new_null(); |
| 343 | if (ret == NULL) { |
| 344 | return NULL; |
| 345 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 346 | |
David Benjamin | 5993704 | 2015-09-19 13:04:22 -0400 | [diff] [blame] | 347 | size_t i; |
| 348 | for (i = 0; i < sk_X509_NAME_num(list); i++) { |
| 349 | X509_NAME *name = X509_NAME_dup(sk_X509_NAME_value(list, i)); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 350 | if (name == NULL || !sk_X509_NAME_push(ret, name)) { |
David Benjamin | 5993704 | 2015-09-19 13:04:22 -0400 | [diff] [blame] | 351 | X509_NAME_free(name); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 352 | sk_X509_NAME_pop_free(ret, X509_NAME_free); |
| 353 | return NULL; |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | return ret; |
| 358 | } |
| 359 | |
David Benjamin | 5993704 | 2015-09-19 13:04:22 -0400 | [diff] [blame] | 360 | void SSL_set_client_CA_list(SSL *ssl, STACK_OF(X509_NAME) *name_list) { |
| 361 | set_client_CA_list(&ssl->client_CA, name_list); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 362 | } |
| 363 | |
David Benjamin | 60da0cd | 2015-05-03 15:21:28 -0400 | [diff] [blame] | 364 | 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] | 365 | set_client_CA_list(&ctx->client_CA, name_list); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 366 | } |
| 367 | |
David Benjamin | 60da0cd | 2015-05-03 15:21:28 -0400 | [diff] [blame] | 368 | 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] | 369 | return ctx->client_CA; |
| 370 | } |
| 371 | |
David Benjamin | 1d128f3 | 2015-09-08 17:41:40 -0400 | [diff] [blame] | 372 | STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *ssl) { |
| 373 | /* For historical reasons, this function is used both to query configuration |
| 374 | * state on a server as well as handshake state on a client. However, whether |
| 375 | * |ssl| is a client or server is not known until explicitly configured with |
| 376 | * |SSL_set_connect_state|. If |handshake_func| is NULL, |ssl| is in an |
| 377 | * indeterminate mode and |ssl->server| is unset. */ |
| 378 | if (ssl->handshake_func != NULL && !ssl->server) { |
| 379 | return ssl->s3->tmp.ca_names; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 380 | } |
David Benjamin | 1d128f3 | 2015-09-08 17:41:40 -0400 | [diff] [blame] | 381 | |
| 382 | if (ssl->client_CA != NULL) { |
| 383 | return ssl->client_CA; |
| 384 | } |
| 385 | return ssl->ctx->client_CA; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 386 | } |
| 387 | |
David Benjamin | 5993704 | 2015-09-19 13:04:22 -0400 | [diff] [blame] | 388 | static int add_client_CA(STACK_OF(X509_NAME) **sk, X509 *x509) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 389 | X509_NAME *name; |
| 390 | |
David Benjamin | 5993704 | 2015-09-19 13:04:22 -0400 | [diff] [blame] | 391 | if (x509 == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 392 | return 0; |
| 393 | } |
| 394 | if (*sk == NULL) { |
| 395 | *sk = sk_X509_NAME_new_null(); |
| 396 | if (*sk == NULL) { |
| 397 | return 0; |
| 398 | } |
| 399 | } |
| 400 | |
David Benjamin | 5993704 | 2015-09-19 13:04:22 -0400 | [diff] [blame] | 401 | name = X509_NAME_dup(X509_get_subject_name(x509)); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 402 | if (name == NULL) { |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | if (!sk_X509_NAME_push(*sk, name)) { |
| 407 | X509_NAME_free(name); |
| 408 | return 0; |
| 409 | } |
| 410 | |
| 411 | return 1; |
| 412 | } |
| 413 | |
David Benjamin | 5993704 | 2015-09-19 13:04:22 -0400 | [diff] [blame] | 414 | int SSL_add_client_CA(SSL *ssl, X509 *x509) { |
| 415 | return add_client_CA(&ssl->client_CA, x509); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 416 | } |
| 417 | |
David Benjamin | 5993704 | 2015-09-19 13:04:22 -0400 | [diff] [blame] | 418 | int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x509) { |
| 419 | return add_client_CA(&ctx->client_CA, x509); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 420 | } |
| 421 | |
David Benjamin | 32a66d5 | 2016-07-13 22:03:11 -0400 | [diff] [blame] | 422 | int ssl_has_certificate(const SSL *ssl) { |
| 423 | return ssl->cert->x509 != NULL && ssl_has_private_key(ssl); |
| 424 | } |
| 425 | |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 426 | STACK_OF(X509) *ssl_parse_cert_chain(SSL *ssl, uint8_t *out_alert, |
| 427 | uint8_t *out_leaf_sha256, CBS *cbs) { |
| 428 | STACK_OF(X509) *ret = sk_X509_new_null(); |
| 429 | if (ret == NULL) { |
| 430 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 431 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 432 | return NULL; |
| 433 | } |
| 434 | |
| 435 | X509 *x = NULL; |
| 436 | CBS certificate_list; |
| 437 | if (!CBS_get_u24_length_prefixed(cbs, &certificate_list)) { |
| 438 | *out_alert = SSL_AD_DECODE_ERROR; |
| 439 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 440 | goto err; |
| 441 | } |
| 442 | |
| 443 | while (CBS_len(&certificate_list) > 0) { |
| 444 | CBS certificate; |
| 445 | if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) { |
| 446 | *out_alert = SSL_AD_DECODE_ERROR; |
| 447 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH); |
| 448 | goto err; |
| 449 | } |
| 450 | |
| 451 | /* Retain the hash of the leaf certificate if requested. */ |
| 452 | if (sk_X509_num(ret) == 0 && out_leaf_sha256 != NULL) { |
| 453 | SHA256(CBS_data(&certificate), CBS_len(&certificate), out_leaf_sha256); |
| 454 | } |
| 455 | |
| 456 | /* A u24 length cannot overflow a long. */ |
| 457 | const uint8_t *data = CBS_data(&certificate); |
| 458 | x = d2i_X509(NULL, &data, (long)CBS_len(&certificate)); |
| 459 | if (x == NULL || data != CBS_data(&certificate) + CBS_len(&certificate)) { |
| 460 | *out_alert = SSL_AD_DECODE_ERROR; |
| 461 | goto err; |
| 462 | } |
| 463 | if (!sk_X509_push(ret, x)) { |
| 464 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 465 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 466 | goto err; |
| 467 | } |
| 468 | x = NULL; |
| 469 | } |
| 470 | |
| 471 | return ret; |
| 472 | |
| 473 | err: |
| 474 | X509_free(x); |
| 475 | sk_X509_pop_free(ret, X509_free); |
| 476 | return NULL; |
| 477 | } |
| 478 | |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 479 | int ssl_add_cert_to_cbb(CBB *cbb, X509 *x509) { |
| 480 | int len = i2d_X509(x509, NULL); |
| 481 | if (len < 0) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 482 | return 0; |
| 483 | } |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 484 | uint8_t *buf; |
| 485 | if (!CBB_add_space(cbb, &buf, len)) { |
| 486 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 487 | return 0; |
Steven Valdez | b32a915 | 2016-04-26 12:57:22 -0400 | [diff] [blame] | 488 | } |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 489 | if (buf != NULL && i2d_X509(x509, &buf) < 0) { |
| 490 | return 0; |
| 491 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 492 | return 1; |
| 493 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 494 | |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 495 | static int ssl_add_cert_with_length(CBB *cbb, X509 *x509) { |
| 496 | CBB child; |
| 497 | return CBB_add_u24_length_prefixed(cbb, &child) && |
| 498 | ssl_add_cert_to_cbb(&child, x509) && |
| 499 | CBB_flush(cbb); |
| 500 | } |
| 501 | |
| 502 | int ssl_add_cert_chain(SSL *ssl, CBB *cbb) { |
David Benjamin | 32a66d5 | 2016-07-13 22:03:11 -0400 | [diff] [blame] | 503 | if (!ssl_has_certificate(ssl)) { |
| 504 | return CBB_add_u24(cbb, 0); |
| 505 | } |
| 506 | |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 507 | CERT *cert = ssl->cert; |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 508 | X509 *x = cert->x509; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 509 | |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 510 | CBB child; |
| 511 | if (!CBB_add_u24_length_prefixed(cbb, &child)) { |
| 512 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 513 | return 0; |
| 514 | } |
| 515 | |
| 516 | int no_chain = 0; |
| 517 | STACK_OF(X509) *chain = cert->chain; |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 518 | if ((ssl->mode & SSL_MODE_NO_AUTO_CHAIN) || chain != NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 519 | no_chain = 1; |
| 520 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 521 | |
David Benjamin | 605641e | 2015-05-03 15:14:04 -0400 | [diff] [blame] | 522 | if (no_chain) { |
Steven Valdez | 57a6f3c | 2016-06-28 15:37:37 -0400 | [diff] [blame] | 523 | if (!ssl_add_cert_with_length(&child, x)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 524 | return 0; |
| 525 | } |
David Benjamin | 605641e | 2015-05-03 15:14:04 -0400 | [diff] [blame] | 526 | |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 527 | size_t i; |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 528 | for (i = 0; i < sk_X509_num(chain); i++) { |
| 529 | x = sk_X509_value(chain, i); |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 530 | if (!ssl_add_cert_with_length(&child, x)) { |
David Benjamin | 605641e | 2015-05-03 15:14:04 -0400 | [diff] [blame] | 531 | return 0; |
| 532 | } |
| 533 | } |
| 534 | } else { |
| 535 | X509_STORE_CTX xs_ctx; |
| 536 | |
David Benjamin | d27441a | 2015-08-09 11:05:17 -0400 | [diff] [blame] | 537 | 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] | 538 | OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB); |
David Benjamin | 605641e | 2015-05-03 15:14:04 -0400 | [diff] [blame] | 539 | return 0; |
| 540 | } |
| 541 | X509_verify_cert(&xs_ctx); |
| 542 | /* Don't leave errors in the queue */ |
| 543 | ERR_clear_error(); |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 544 | |
| 545 | size_t i; |
David Benjamin | 605641e | 2015-05-03 15:14:04 -0400 | [diff] [blame] | 546 | for (i = 0; i < sk_X509_num(xs_ctx.chain); i++) { |
| 547 | x = sk_X509_value(xs_ctx.chain, i); |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 548 | if (!ssl_add_cert_with_length(&child, x)) { |
David Benjamin | 605641e | 2015-05-03 15:14:04 -0400 | [diff] [blame] | 549 | X509_STORE_CTX_cleanup(&xs_ctx); |
| 550 | return 0; |
| 551 | } |
| 552 | } |
| 553 | X509_STORE_CTX_cleanup(&xs_ctx); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 554 | } |
| 555 | |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 556 | return CBB_flush(cbb); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 557 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 558 | |
David Benjamin | e0332e8 | 2016-07-13 22:40:36 -0400 | [diff] [blame] | 559 | static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b) { |
| 560 | return X509_NAME_cmp(*a, *b); |
| 561 | } |
| 562 | |
| 563 | STACK_OF(X509_NAME) * |
| 564 | ssl_parse_client_CA_list(SSL *ssl, uint8_t *out_alert, CBS *cbs) { |
| 565 | STACK_OF(X509_NAME) *ret = sk_X509_NAME_new(ca_dn_cmp); |
| 566 | X509_NAME *name = NULL; |
| 567 | if (ret == NULL) { |
| 568 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 569 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 570 | return NULL; |
| 571 | } |
| 572 | |
| 573 | CBS child; |
| 574 | if (!CBS_get_u16_length_prefixed(cbs, &child)) { |
| 575 | *out_alert = SSL_AD_DECODE_ERROR; |
| 576 | OPENSSL_PUT_ERROR(SSL, SSL_R_LENGTH_MISMATCH); |
| 577 | goto err; |
| 578 | } |
| 579 | |
| 580 | while (CBS_len(&child) > 0) { |
| 581 | CBS distinguished_name; |
| 582 | if (!CBS_get_u16_length_prefixed(&child, &distinguished_name)) { |
| 583 | *out_alert = SSL_AD_DECODE_ERROR; |
| 584 | OPENSSL_PUT_ERROR(SSL, SSL_R_CA_DN_TOO_LONG); |
| 585 | goto err; |
| 586 | } |
| 587 | |
| 588 | const uint8_t *ptr = CBS_data(&distinguished_name); |
| 589 | /* A u16 length cannot overflow a long. */ |
| 590 | name = d2i_X509_NAME(NULL, &ptr, (long)CBS_len(&distinguished_name)); |
| 591 | if (name == NULL || |
| 592 | ptr != CBS_data(&distinguished_name) + CBS_len(&distinguished_name)) { |
| 593 | *out_alert = SSL_AD_DECODE_ERROR; |
| 594 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 595 | goto err; |
| 596 | } |
| 597 | |
| 598 | if (!sk_X509_NAME_push(ret, name)) { |
| 599 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 600 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 601 | goto err; |
| 602 | } |
| 603 | name = NULL; |
| 604 | } |
| 605 | |
| 606 | return ret; |
| 607 | |
| 608 | err: |
| 609 | X509_NAME_free(name); |
| 610 | sk_X509_NAME_pop_free(ret, X509_NAME_free); |
| 611 | return NULL; |
| 612 | } |
| 613 | |
David Benjamin | 32a66d5 | 2016-07-13 22:03:11 -0400 | [diff] [blame] | 614 | int ssl_add_client_CA_list(SSL *ssl, CBB *cbb) { |
| 615 | CBB child, name_cbb; |
| 616 | if (!CBB_add_u16_length_prefixed(cbb, &child)) { |
| 617 | return 0; |
| 618 | } |
| 619 | |
| 620 | STACK_OF(X509_NAME) *sk = SSL_get_client_CA_list(ssl); |
| 621 | if (sk == NULL) { |
| 622 | return CBB_flush(cbb); |
| 623 | } |
| 624 | |
| 625 | for (size_t i = 0; i < sk_X509_NAME_num(sk); i++) { |
| 626 | X509_NAME *name = sk_X509_NAME_value(sk, i); |
| 627 | int len = i2d_X509_NAME(name, NULL); |
| 628 | if (len < 0) { |
| 629 | return 0; |
| 630 | } |
| 631 | uint8_t *ptr; |
| 632 | if (!CBB_add_u16_length_prefixed(&child, &name_cbb) || |
| 633 | !CBB_add_space(&name_cbb, &ptr, (size_t)len) || |
| 634 | (len > 0 && i2d_X509_NAME(name, &ptr) < 0)) { |
| 635 | return 0; |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | return CBB_flush(cbb); |
| 640 | } |
| 641 | |
Adam Langley | d323f4b | 2016-03-01 15:58:14 -0800 | [diff] [blame] | 642 | static int set_cert_store(X509_STORE **store_ptr, X509_STORE *new_store, int take_ref) { |
| 643 | X509_STORE_free(*store_ptr); |
| 644 | *store_ptr = new_store; |
| 645 | |
| 646 | if (new_store != NULL && take_ref) { |
| 647 | X509_STORE_up_ref(new_store); |
| 648 | } |
| 649 | |
| 650 | return 1; |
| 651 | } |
| 652 | |
| 653 | int SSL_CTX_set0_verify_cert_store(SSL_CTX *ctx, X509_STORE *store) { |
| 654 | return set_cert_store(&ctx->cert->verify_store, store, 0); |
| 655 | } |
| 656 | |
| 657 | int SSL_CTX_set1_verify_cert_store(SSL_CTX *ctx, X509_STORE *store) { |
| 658 | return set_cert_store(&ctx->cert->verify_store, store, 1); |
| 659 | } |
| 660 | |
| 661 | int SSL_set0_verify_cert_store(SSL *ssl, X509_STORE *store) { |
| 662 | return set_cert_store(&ssl->cert->verify_store, store, 0); |
| 663 | } |
| 664 | |
| 665 | int SSL_set1_verify_cert_store(SSL *ssl, X509_STORE *store) { |
| 666 | return set_cert_store(&ssl->cert->verify_store, store, 1); |
| 667 | } |
| 668 | |
David Benjamin | 11c0f8e | 2015-07-06 00:18:15 -0400 | [diff] [blame] | 669 | int SSL_CTX_set0_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) { |
| 670 | return ssl_cert_set0_chain(ctx->cert, chain); |
| 671 | } |
| 672 | |
| 673 | int SSL_CTX_set1_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) { |
| 674 | return ssl_cert_set1_chain(ctx->cert, chain); |
| 675 | } |
| 676 | |
| 677 | int SSL_set0_chain(SSL *ssl, STACK_OF(X509) *chain) { |
| 678 | return ssl_cert_set0_chain(ssl->cert, chain); |
| 679 | } |
| 680 | |
| 681 | int SSL_set1_chain(SSL *ssl, STACK_OF(X509) *chain) { |
| 682 | return ssl_cert_set1_chain(ssl->cert, chain); |
| 683 | } |
| 684 | |
| 685 | int SSL_CTX_add0_chain_cert(SSL_CTX *ctx, X509 *x509) { |
| 686 | return ssl_cert_add0_chain_cert(ctx->cert, x509); |
| 687 | } |
| 688 | |
| 689 | int SSL_CTX_add1_chain_cert(SSL_CTX *ctx, X509 *x509) { |
| 690 | return ssl_cert_add1_chain_cert(ctx->cert, x509); |
| 691 | } |
| 692 | |
| 693 | int SSL_CTX_add_extra_chain_cert(SSL_CTX *ctx, X509 *x509) { |
| 694 | return SSL_CTX_add0_chain_cert(ctx, x509); |
| 695 | } |
| 696 | |
| 697 | int SSL_add0_chain_cert(SSL *ssl, X509 *x509) { |
| 698 | return ssl_cert_add0_chain_cert(ssl->cert, x509); |
| 699 | } |
| 700 | |
| 701 | int SSL_add1_chain_cert(SSL *ssl, X509 *x509) { |
| 702 | return ssl_cert_add1_chain_cert(ssl->cert, x509); |
| 703 | } |
| 704 | |
| 705 | int SSL_CTX_clear_chain_certs(SSL_CTX *ctx) { |
| 706 | return SSL_CTX_set0_chain(ctx, NULL); |
| 707 | } |
| 708 | |
| 709 | int SSL_CTX_clear_extra_chain_certs(SSL_CTX *ctx) { |
| 710 | return SSL_CTX_clear_chain_certs(ctx); |
| 711 | } |
| 712 | |
| 713 | int SSL_clear_chain_certs(SSL *ssl) { |
| 714 | return SSL_set0_chain(ssl, NULL); |
| 715 | } |
| 716 | |
| 717 | int SSL_CTX_get0_chain_certs(const SSL_CTX *ctx, STACK_OF(X509) **out_chain) { |
| 718 | *out_chain = ctx->cert->chain; |
| 719 | return 1; |
| 720 | } |
| 721 | |
| 722 | int SSL_CTX_get_extra_chain_certs(const SSL_CTX *ctx, |
| 723 | STACK_OF(X509) **out_chain) { |
| 724 | return SSL_CTX_get0_chain_certs(ctx, out_chain); |
| 725 | } |
| 726 | |
| 727 | int SSL_get0_chain_certs(const SSL *ssl, STACK_OF(X509) **out_chain) { |
| 728 | *out_chain = ssl->cert->chain; |
| 729 | return 1; |
| 730 | } |
Steven Valdez | bf5aa84 | 2016-07-15 07:07:40 -0400 | [diff] [blame^] | 731 | |
| 732 | int ssl_check_leaf_certificate(SSL *ssl, X509 *leaf) { |
| 733 | int ret = 0; |
| 734 | EVP_PKEY *pkey = X509_get_pubkey(leaf); |
| 735 | if (pkey == NULL) { |
| 736 | goto err; |
| 737 | } |
| 738 | |
| 739 | /* Check the certificate's type matches the cipher. */ |
| 740 | const SSL_CIPHER *cipher = ssl->s3->tmp.new_cipher; |
| 741 | int expected_type = ssl_cipher_get_key_type(cipher); |
| 742 | assert(expected_type != EVP_PKEY_NONE); |
| 743 | if (pkey->type != expected_type) { |
| 744 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CERTIFICATE_TYPE); |
| 745 | goto err; |
| 746 | } |
| 747 | |
| 748 | if (cipher->algorithm_auth & SSL_aECDSA) { |
| 749 | /* TODO(davidben): This behavior is preserved from upstream. Should key |
| 750 | * usages be checked in other cases as well? */ |
| 751 | /* This call populates the ex_flags field correctly */ |
| 752 | X509_check_purpose(leaf, -1, 0); |
| 753 | if ((leaf->ex_flags & EXFLAG_KUSAGE) && |
| 754 | !(leaf->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)) { |
| 755 | OPENSSL_PUT_ERROR(SSL, SSL_R_ECC_CERT_NOT_FOR_SIGNING); |
| 756 | goto err; |
| 757 | } |
| 758 | |
| 759 | if (!tls1_check_ec_cert(ssl, leaf)) { |
| 760 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECC_CERT); |
| 761 | goto err; |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | ret = 1; |
| 766 | |
| 767 | err: |
| 768 | EVP_PKEY_free(pkey); |
| 769 | return ret; |
| 770 | } |