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> |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 123 | #include <openssl/bytestring.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 124 | #include <openssl/dh.h> |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 125 | #include <openssl/ec_key.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 126 | #include <openssl/err.h> |
| 127 | #include <openssl/mem.h> |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 128 | #include <openssl/sha.h> |
David Benjamin | 680ca96 | 2015-06-18 12:37:23 -0400 | [diff] [blame] | 129 | #include <openssl/x509.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 130 | #include <openssl/x509v3.h> |
| 131 | |
Adam Langley | 0da323a | 2015-05-15 12:49:30 -0700 | [diff] [blame] | 132 | #include "../crypto/internal.h" |
David Benjamin | 2ee94aa | 2015-04-07 22:38:30 -0400 | [diff] [blame] | 133 | #include "internal.h" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 134 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 135 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 136 | int SSL_get_ex_data_X509_STORE_CTX_idx(void) { |
David Benjamin | aa58513 | 2015-06-29 23:36:17 -0400 | [diff] [blame] | 137 | /* The ex_data index to go from |X509_STORE_CTX| to |SSL| always uses the |
| 138 | * reserved app_data slot. Before ex_data was introduced, app_data was used. |
| 139 | * Avoid breaking any software which assumes |X509_STORE_CTX_get_app_data| |
| 140 | * works. */ |
| 141 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 142 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 143 | |
Adam Langley | 3509dac | 2017-02-01 11:59:18 -0800 | [diff] [blame] | 144 | CERT *ssl_cert_new(const SSL_X509_METHOD *x509_method) { |
Brian Smith | 5ba0689 | 2016-02-07 09:36:04 -1000 | [diff] [blame] | 145 | CERT *ret = OPENSSL_malloc(sizeof(CERT)); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 146 | if (ret == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 147 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 148 | return NULL; |
| 149 | } |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 150 | OPENSSL_memset(ret, 0, sizeof(CERT)); |
Adam Langley | 3509dac | 2017-02-01 11:59:18 -0800 | [diff] [blame] | 151 | ret->x509_method = x509_method; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 152 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 153 | return ret; |
| 154 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 155 | |
Adam Langley | 3a2b47a | 2017-01-24 13:59:42 -0800 | [diff] [blame] | 156 | static CRYPTO_BUFFER *buffer_up_ref(CRYPTO_BUFFER *buffer) { |
| 157 | CRYPTO_BUFFER_up_ref(buffer); |
| 158 | return buffer; |
| 159 | } |
| 160 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 161 | CERT *ssl_cert_dup(CERT *cert) { |
Brian Smith | 5ba0689 | 2016-02-07 09:36:04 -1000 | [diff] [blame] | 162 | CERT *ret = OPENSSL_malloc(sizeof(CERT)); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 163 | if (ret == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 164 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 165 | return NULL; |
| 166 | } |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 167 | OPENSSL_memset(ret, 0, sizeof(CERT)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 168 | |
Adam Langley | 3a2b47a | 2017-01-24 13:59:42 -0800 | [diff] [blame] | 169 | ret->chain = sk_CRYPTO_BUFFER_deep_copy(cert->chain, buffer_up_ref, |
| 170 | CRYPTO_BUFFER_free); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 171 | |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 172 | if (cert->privatekey != NULL) { |
Adam Langley | 310d3f6 | 2016-07-12 10:39:20 -0700 | [diff] [blame] | 173 | EVP_PKEY_up_ref(cert->privatekey); |
| 174 | ret->privatekey = cert->privatekey; |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 175 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 176 | |
Tom Thorogood | 66b2fe8 | 2016-03-06 20:08:38 +1030 | [diff] [blame] | 177 | ret->key_method = cert->key_method; |
Adam Langley | 3509dac | 2017-02-01 11:59:18 -0800 | [diff] [blame] | 178 | ret->x509_method = cert->x509_method; |
Tom Thorogood | 66b2fe8 | 2016-03-06 20:08:38 +1030 | [diff] [blame] | 179 | |
David Benjamin | 0fc37ef | 2016-08-17 15:29:46 -0400 | [diff] [blame] | 180 | if (cert->dh_tmp != NULL) { |
| 181 | ret->dh_tmp = DHparams_dup(cert->dh_tmp); |
| 182 | if (ret->dh_tmp == NULL) { |
| 183 | OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB); |
| 184 | goto err; |
| 185 | } |
| 186 | } |
| 187 | ret->dh_tmp_cb = cert->dh_tmp_cb; |
| 188 | |
| 189 | if (cert->sigalgs != NULL) { |
| 190 | ret->sigalgs = |
| 191 | BUF_memdup(cert->sigalgs, cert->num_sigalgs * sizeof(cert->sigalgs[0])); |
| 192 | if (ret->sigalgs == NULL) { |
| 193 | goto err; |
| 194 | } |
| 195 | } |
| 196 | ret->num_sigalgs = cert->num_sigalgs; |
| 197 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 198 | ret->cert_cb = cert->cert_cb; |
| 199 | ret->cert_cb_arg = cert->cert_cb_arg; |
| 200 | |
Adam Langley | d323f4b | 2016-03-01 15:58:14 -0800 | [diff] [blame] | 201 | if (cert->verify_store != NULL) { |
| 202 | X509_STORE_up_ref(cert->verify_store); |
| 203 | ret->verify_store = cert->verify_store; |
| 204 | } |
| 205 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 206 | return ret; |
| 207 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 208 | err: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 209 | ssl_cert_free(ret); |
| 210 | return NULL; |
| 211 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 212 | |
| 213 | /* Free up and clear all certificates and chains */ |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 214 | void ssl_cert_clear_certs(CERT *cert) { |
| 215 | if (cert == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 216 | return; |
| 217 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 218 | |
Adam Langley | 3509dac | 2017-02-01 11:59:18 -0800 | [diff] [blame] | 219 | cert->x509_method->cert_clear(cert); |
Adam Langley | e1e7813 | 2017-01-31 15:24:31 -0800 | [diff] [blame] | 220 | |
Adam Langley | 3a2b47a | 2017-01-24 13:59:42 -0800 | [diff] [blame] | 221 | sk_CRYPTO_BUFFER_pop_free(cert->chain, CRYPTO_BUFFER_free); |
| 222 | cert->chain = NULL; |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 223 | EVP_PKEY_free(cert->privatekey); |
| 224 | cert->privatekey = NULL; |
David Benjamin | 71d2e54 | 2015-07-06 00:30:25 -0400 | [diff] [blame] | 225 | cert->key_method = NULL; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 226 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 227 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 228 | void ssl_cert_free(CERT *c) { |
| 229 | if (c == NULL) { |
| 230 | return; |
| 231 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 232 | |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 233 | DH_free(c->dh_tmp); |
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 | ssl_cert_clear_certs(c); |
David Benjamin | d246b81 | 2016-07-08 15:07:02 -0700 | [diff] [blame] | 236 | OPENSSL_free(c->sigalgs); |
Adam Langley | d323f4b | 2016-03-01 15:58:14 -0800 | [diff] [blame] | 237 | X509_STORE_free(c->verify_store); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 238 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 239 | OPENSSL_free(c); |
| 240 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 241 | |
David Benjamin | a2bda9f | 2016-12-03 23:29:05 -0500 | [diff] [blame] | 242 | static void ssl_cert_set_cert_cb(CERT *c, int (*cb)(SSL *ssl, void *arg), |
| 243 | void *arg) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 244 | c->cert_cb = cb; |
| 245 | c->cert_cb_arg = arg; |
| 246 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 247 | |
Adam Langley | 52940c4 | 2017-02-01 12:40:31 -0800 | [diff] [blame^] | 248 | int ssl_set_cert(CERT *cert, CRYPTO_BUFFER *buffer) { |
| 249 | CBS cert_cbs; |
| 250 | CRYPTO_BUFFER_init_CBS(buffer, &cert_cbs); |
| 251 | EVP_PKEY *pubkey = ssl_cert_parse_pubkey(&cert_cbs); |
| 252 | if (pubkey == NULL) { |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | if (!ssl_is_key_type_supported(pubkey->type)) { |
| 257 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE); |
| 258 | EVP_PKEY_free(pubkey); |
| 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | /* An ECC certificate may be usable for ECDH or ECDSA. We only support ECDSA |
| 263 | * certificates, so sanity-check the key usage extension. */ |
| 264 | if (pubkey->type == EVP_PKEY_EC && |
| 265 | !ssl_cert_check_digital_signature_key_usage(&cert_cbs)) { |
| 266 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE); |
| 267 | EVP_PKEY_free(pubkey); |
| 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | if (cert->privatekey != NULL) { |
| 272 | /* Sanity-check that the private key and the certificate match, unless the |
| 273 | * key is opaque (in case of, say, a smartcard). */ |
| 274 | if (!EVP_PKEY_is_opaque(cert->privatekey) && |
| 275 | !ssl_compare_public_and_private_key(pubkey, cert->privatekey)) { |
| 276 | /* don't fail for a cert/key mismatch, just free current private key |
| 277 | * (when switching to a different cert & key, first this function should |
| 278 | * be used, then ssl_set_pkey */ |
| 279 | EVP_PKEY_free(cert->privatekey); |
| 280 | cert->privatekey = NULL; |
| 281 | /* clear error queue */ |
| 282 | ERR_clear_error(); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | EVP_PKEY_free(pubkey); |
| 287 | |
| 288 | cert->x509_method->cert_flush_cached_leaf(cert); |
| 289 | |
| 290 | if (cert->chain != NULL) { |
| 291 | CRYPTO_BUFFER_free(sk_CRYPTO_BUFFER_value(cert->chain, 0)); |
| 292 | sk_CRYPTO_BUFFER_set(cert->chain, 0, buffer); |
| 293 | CRYPTO_BUFFER_up_ref(buffer); |
| 294 | return 1; |
| 295 | } |
| 296 | |
| 297 | cert->chain = sk_CRYPTO_BUFFER_new_null(); |
| 298 | if (cert->chain == NULL) { |
| 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | if (!sk_CRYPTO_BUFFER_push(cert->chain, buffer)) { |
| 303 | sk_CRYPTO_BUFFER_free(cert->chain); |
| 304 | cert->chain = NULL; |
| 305 | return 0; |
| 306 | } |
| 307 | CRYPTO_BUFFER_up_ref(buffer); |
| 308 | |
| 309 | return 1; |
| 310 | } |
| 311 | |
| 312 | int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, size_t der_len, |
| 313 | const uint8_t *der) { |
| 314 | CRYPTO_BUFFER *buffer = CRYPTO_BUFFER_new(der, der_len, NULL); |
| 315 | if (buffer == NULL) { |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | const int ok = ssl_set_cert(ctx->cert, buffer); |
| 320 | CRYPTO_BUFFER_free(buffer); |
| 321 | return ok; |
| 322 | } |
| 323 | |
| 324 | int SSL_use_certificate_ASN1(SSL *ssl, const uint8_t *der, size_t der_len) { |
| 325 | CRYPTO_BUFFER *buffer = CRYPTO_BUFFER_new(der, der_len, NULL); |
| 326 | if (buffer == NULL) { |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | const int ok = ssl_set_cert(ssl->cert, buffer); |
| 331 | CRYPTO_BUFFER_free(buffer); |
| 332 | return ok; |
| 333 | } |
| 334 | |
David Benjamin | 7aa31d6 | 2016-08-08 21:38:32 -0400 | [diff] [blame] | 335 | int ssl_verify_cert_chain(SSL *ssl, long *out_verify_result, |
| 336 | STACK_OF(X509) *cert_chain) { |
David Benjamin | 306ece3 | 2015-09-17 13:46:22 -0400 | [diff] [blame] | 337 | if (cert_chain == NULL || sk_X509_num(cert_chain) == 0) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 338 | return 0; |
| 339 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 340 | |
Adam Langley | d323f4b | 2016-03-01 15:58:14 -0800 | [diff] [blame] | 341 | X509_STORE *verify_store = ssl->ctx->cert_store; |
| 342 | if (ssl->cert->verify_store != NULL) { |
| 343 | verify_store = ssl->cert->verify_store; |
| 344 | } |
| 345 | |
David Benjamin | 306ece3 | 2015-09-17 13:46:22 -0400 | [diff] [blame] | 346 | X509 *leaf = sk_X509_value(cert_chain, 0); |
| 347 | int ret = 0; |
| 348 | X509_STORE_CTX ctx; |
Adam Langley | d323f4b | 2016-03-01 15:58:14 -0800 | [diff] [blame] | 349 | if (!X509_STORE_CTX_init(&ctx, verify_store, leaf, cert_chain)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 350 | OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 351 | return 0; |
| 352 | } |
David Benjamin | 306ece3 | 2015-09-17 13:46:22 -0400 | [diff] [blame] | 353 | if (!X509_STORE_CTX_set_ex_data(&ctx, SSL_get_ex_data_X509_STORE_CTX_idx(), |
| 354 | ssl)) { |
| 355 | goto err; |
| 356 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 357 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 358 | /* We need to inherit the verify parameters. These can be determined by the |
| 359 | * context: if its a server it will verify SSL client certificates or vice |
| 360 | * versa. */ |
David Benjamin | 306ece3 | 2015-09-17 13:46:22 -0400 | [diff] [blame] | 361 | X509_STORE_CTX_set_default(&ctx, ssl->server ? "ssl_client" : "ssl_server"); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 362 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 363 | /* Anything non-default in "param" should overwrite anything in the ctx. */ |
David Benjamin | 306ece3 | 2015-09-17 13:46:22 -0400 | [diff] [blame] | 364 | X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(&ctx), ssl->param); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 365 | |
David Benjamin | 306ece3 | 2015-09-17 13:46:22 -0400 | [diff] [blame] | 366 | if (ssl->verify_callback) { |
| 367 | X509_STORE_CTX_set_verify_cb(&ctx, ssl->verify_callback); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 368 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 369 | |
David Benjamin | e455e51 | 2016-08-01 20:11:13 -0400 | [diff] [blame] | 370 | int verify_ret; |
David Benjamin | 306ece3 | 2015-09-17 13:46:22 -0400 | [diff] [blame] | 371 | if (ssl->ctx->app_verify_callback != NULL) { |
David Benjamin | e455e51 | 2016-08-01 20:11:13 -0400 | [diff] [blame] | 372 | 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] | 373 | } else { |
David Benjamin | e455e51 | 2016-08-01 20:11:13 -0400 | [diff] [blame] | 374 | verify_ret = X509_verify_cert(&ctx); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 375 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 376 | |
Adam Langley | a6cd185 | 2016-08-26 09:38:13 -0700 | [diff] [blame] | 377 | *out_verify_result = ctx.error; |
| 378 | |
David Benjamin | e455e51 | 2016-08-01 20:11:13 -0400 | [diff] [blame] | 379 | /* If |SSL_VERIFY_NONE|, the error is non-fatal, but we keep the result. */ |
| 380 | if (verify_ret <= 0 && ssl->verify_mode != SSL_VERIFY_NONE) { |
David Benjamin | 7aa31d6 | 2016-08-08 21:38:32 -0400 | [diff] [blame] | 381 | 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] | 382 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_VERIFY_FAILED); |
| 383 | goto err; |
| 384 | } |
| 385 | |
| 386 | ERR_clear_error(); |
| 387 | ret = 1; |
| 388 | |
David Benjamin | 306ece3 | 2015-09-17 13:46:22 -0400 | [diff] [blame] | 389 | err: |
| 390 | X509_STORE_CTX_cleanup(&ctx); |
| 391 | return ret; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 392 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 393 | |
David Benjamin | 60da0cd | 2015-05-03 15:21:28 -0400 | [diff] [blame] | 394 | static void set_client_CA_list(STACK_OF(X509_NAME) **ca_list, |
| 395 | STACK_OF(X509_NAME) *name_list) { |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 396 | sk_X509_NAME_pop_free(*ca_list, X509_NAME_free); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 397 | *ca_list = name_list; |
| 398 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 399 | |
David Benjamin | 5993704 | 2015-09-19 13:04:22 -0400 | [diff] [blame] | 400 | STACK_OF(X509_NAME) *SSL_dup_CA_list(STACK_OF(X509_NAME) *list) { |
| 401 | STACK_OF(X509_NAME) *ret = sk_X509_NAME_new_null(); |
| 402 | if (ret == NULL) { |
| 403 | return NULL; |
| 404 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 405 | |
David Benjamin | 5409123 | 2016-09-05 12:47:25 -0400 | [diff] [blame] | 406 | for (size_t i = 0; i < sk_X509_NAME_num(list); i++) { |
David Benjamin | 5993704 | 2015-09-19 13:04:22 -0400 | [diff] [blame] | 407 | X509_NAME *name = X509_NAME_dup(sk_X509_NAME_value(list, i)); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 408 | if (name == NULL || !sk_X509_NAME_push(ret, name)) { |
David Benjamin | 5993704 | 2015-09-19 13:04:22 -0400 | [diff] [blame] | 409 | X509_NAME_free(name); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 410 | sk_X509_NAME_pop_free(ret, X509_NAME_free); |
| 411 | return NULL; |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | return ret; |
| 416 | } |
| 417 | |
David Benjamin | 5993704 | 2015-09-19 13:04:22 -0400 | [diff] [blame] | 418 | void SSL_set_client_CA_list(SSL *ssl, STACK_OF(X509_NAME) *name_list) { |
| 419 | set_client_CA_list(&ssl->client_CA, name_list); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 420 | } |
| 421 | |
David Benjamin | 60da0cd | 2015-05-03 15:21:28 -0400 | [diff] [blame] | 422 | 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] | 423 | set_client_CA_list(&ctx->client_CA, name_list); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 424 | } |
| 425 | |
David Benjamin | 60da0cd | 2015-05-03 15:21:28 -0400 | [diff] [blame] | 426 | 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] | 427 | return ctx->client_CA; |
| 428 | } |
| 429 | |
David Benjamin | 1d128f3 | 2015-09-08 17:41:40 -0400 | [diff] [blame] | 430 | STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *ssl) { |
| 431 | /* For historical reasons, this function is used both to query configuration |
| 432 | * state on a server as well as handshake state on a client. However, whether |
| 433 | * |ssl| is a client or server is not known until explicitly configured with |
| 434 | * |SSL_set_connect_state|. If |handshake_func| is NULL, |ssl| is in an |
| 435 | * indeterminate mode and |ssl->server| is unset. */ |
| 436 | if (ssl->handshake_func != NULL && !ssl->server) { |
David Benjamin | a048678 | 2016-10-06 19:11:32 -0400 | [diff] [blame] | 437 | if (ssl->s3->hs != NULL) { |
| 438 | return ssl->s3->hs->ca_names; |
| 439 | } |
| 440 | |
| 441 | return NULL; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 442 | } |
David Benjamin | 1d128f3 | 2015-09-08 17:41:40 -0400 | [diff] [blame] | 443 | |
| 444 | if (ssl->client_CA != NULL) { |
| 445 | return ssl->client_CA; |
| 446 | } |
| 447 | return ssl->ctx->client_CA; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 448 | } |
| 449 | |
David Benjamin | 5993704 | 2015-09-19 13:04:22 -0400 | [diff] [blame] | 450 | static int add_client_CA(STACK_OF(X509_NAME) **sk, X509 *x509) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 451 | X509_NAME *name; |
| 452 | |
David Benjamin | 5993704 | 2015-09-19 13:04:22 -0400 | [diff] [blame] | 453 | if (x509 == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 454 | return 0; |
| 455 | } |
| 456 | if (*sk == NULL) { |
| 457 | *sk = sk_X509_NAME_new_null(); |
| 458 | if (*sk == NULL) { |
| 459 | return 0; |
| 460 | } |
| 461 | } |
| 462 | |
David Benjamin | 5993704 | 2015-09-19 13:04:22 -0400 | [diff] [blame] | 463 | name = X509_NAME_dup(X509_get_subject_name(x509)); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 464 | if (name == NULL) { |
| 465 | return 0; |
| 466 | } |
| 467 | |
| 468 | if (!sk_X509_NAME_push(*sk, name)) { |
| 469 | X509_NAME_free(name); |
| 470 | return 0; |
| 471 | } |
| 472 | |
| 473 | return 1; |
| 474 | } |
| 475 | |
David Benjamin | 5993704 | 2015-09-19 13:04:22 -0400 | [diff] [blame] | 476 | int SSL_add_client_CA(SSL *ssl, X509 *x509) { |
| 477 | return add_client_CA(&ssl->client_CA, x509); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 478 | } |
| 479 | |
David Benjamin | 5993704 | 2015-09-19 13:04:22 -0400 | [diff] [blame] | 480 | int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x509) { |
| 481 | return add_client_CA(&ctx->client_CA, x509); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 482 | } |
| 483 | |
David Benjamin | 32a66d5 | 2016-07-13 22:03:11 -0400 | [diff] [blame] | 484 | int ssl_has_certificate(const SSL *ssl) { |
Adam Langley | 3a2b47a | 2017-01-24 13:59:42 -0800 | [diff] [blame] | 485 | return ssl->cert->chain != NULL && |
| 486 | sk_CRYPTO_BUFFER_value(ssl->cert->chain, 0) != NULL && |
| 487 | ssl_has_private_key(ssl); |
David Benjamin | 32a66d5 | 2016-07-13 22:03:11 -0400 | [diff] [blame] | 488 | } |
| 489 | |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 490 | STACK_OF(CRYPTO_BUFFER) *ssl_parse_cert_chain(uint8_t *out_alert, |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 491 | EVP_PKEY **out_pubkey, |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 492 | uint8_t *out_leaf_sha256, |
Adam Langley | d519bf6 | 2016-12-12 11:16:44 -0800 | [diff] [blame] | 493 | CBS *cbs, |
| 494 | CRYPTO_BUFFER_POOL *pool) { |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 495 | *out_pubkey = NULL; |
| 496 | |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 497 | STACK_OF(CRYPTO_BUFFER) *ret = sk_CRYPTO_BUFFER_new_null(); |
Adam Langley | c0fc7a1 | 2016-12-09 15:05:34 -0800 | [diff] [blame] | 498 | if (ret == NULL) { |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 499 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 500 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | c0fc7a1 | 2016-12-09 15:05:34 -0800 | [diff] [blame] | 501 | return NULL; |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 502 | } |
| 503 | |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 504 | CBS certificate_list; |
| 505 | if (!CBS_get_u24_length_prefixed(cbs, &certificate_list)) { |
| 506 | *out_alert = SSL_AD_DECODE_ERROR; |
| 507 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 508 | goto err; |
| 509 | } |
| 510 | |
| 511 | while (CBS_len(&certificate_list) > 0) { |
| 512 | CBS certificate; |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 513 | if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate) || |
| 514 | CBS_len(&certificate) == 0) { |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 515 | *out_alert = SSL_AD_DECODE_ERROR; |
| 516 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH); |
| 517 | goto err; |
| 518 | } |
| 519 | |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 520 | if (sk_CRYPTO_BUFFER_num(ret) == 0) { |
| 521 | *out_pubkey = ssl_cert_parse_pubkey(&certificate); |
| 522 | if (*out_pubkey == NULL) { |
Adam Langley | 7dccc71 | 2017-02-08 08:51:25 -0800 | [diff] [blame] | 523 | *out_alert = SSL_AD_DECODE_ERROR; |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 524 | goto err; |
| 525 | } |
| 526 | |
| 527 | /* Retain the hash of the leaf certificate if requested. */ |
| 528 | if (out_leaf_sha256 != NULL) { |
| 529 | SHA256(CBS_data(&certificate), CBS_len(&certificate), out_leaf_sha256); |
| 530 | } |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 531 | } |
| 532 | |
Adam Langley | d519bf6 | 2016-12-12 11:16:44 -0800 | [diff] [blame] | 533 | CRYPTO_BUFFER *buf = |
| 534 | CRYPTO_BUFFER_new_from_CBS(&certificate, pool); |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 535 | if (buf == NULL) { |
Adam Langley | c0fc7a1 | 2016-12-09 15:05:34 -0800 | [diff] [blame] | 536 | *out_alert = SSL_AD_DECODE_ERROR; |
| 537 | goto err; |
| 538 | } |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 539 | |
| 540 | if (!sk_CRYPTO_BUFFER_push(ret, buf)) { |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 541 | *out_alert = SSL_AD_INTERNAL_ERROR; |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 542 | CRYPTO_BUFFER_free(buf); |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 543 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 544 | goto err; |
| 545 | } |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 546 | } |
| 547 | |
Adam Langley | c0fc7a1 | 2016-12-09 15:05:34 -0800 | [diff] [blame] | 548 | return ret; |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 549 | |
| 550 | err: |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 551 | EVP_PKEY_free(*out_pubkey); |
| 552 | *out_pubkey = NULL; |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 553 | sk_CRYPTO_BUFFER_pop_free(ret, CRYPTO_BUFFER_free); |
Adam Langley | c0fc7a1 | 2016-12-09 15:05:34 -0800 | [diff] [blame] | 554 | return NULL; |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 555 | } |
| 556 | |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 557 | int ssl_add_cert_chain(SSL *ssl, CBB *cbb) { |
David Benjamin | 32a66d5 | 2016-07-13 22:03:11 -0400 | [diff] [blame] | 558 | if (!ssl_has_certificate(ssl)) { |
| 559 | return CBB_add_u24(cbb, 0); |
| 560 | } |
| 561 | |
Adam Langley | 3a2b47a | 2017-01-24 13:59:42 -0800 | [diff] [blame] | 562 | CBB certs; |
| 563 | if (!CBB_add_u24_length_prefixed(cbb, &certs)) { |
| 564 | goto err; |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 565 | } |
| 566 | |
Adam Langley | 3a2b47a | 2017-01-24 13:59:42 -0800 | [diff] [blame] | 567 | STACK_OF(CRYPTO_BUFFER) *chain = ssl->cert->chain; |
| 568 | for (size_t i = 0; i < sk_CRYPTO_BUFFER_num(chain); i++) { |
| 569 | CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(chain, i); |
| 570 | CBB child; |
| 571 | if (!CBB_add_u24_length_prefixed(&certs, &child) || |
| 572 | !CBB_add_bytes(&child, CRYPTO_BUFFER_data(buffer), |
| 573 | CRYPTO_BUFFER_len(buffer)) || |
| 574 | !CBB_flush(&certs)) { |
| 575 | goto err; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 576 | } |
| 577 | } |
| 578 | |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 579 | return CBB_flush(cbb); |
Adam Langley | 3a2b47a | 2017-01-24 13:59:42 -0800 | [diff] [blame] | 580 | |
| 581 | err: |
| 582 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 583 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 584 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 585 | |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 586 | /* ssl_cert_skip_to_spki parses a DER-encoded, X.509 certificate from |in| and |
| 587 | * positions |*out_tbs_cert| to cover the TBSCertificate, starting at the |
| 588 | * subjectPublicKeyInfo. */ |
| 589 | static int ssl_cert_skip_to_spki(const CBS *in, CBS *out_tbs_cert) { |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 590 | /* From RFC 5280, section 4.1 |
| 591 | * Certificate ::= SEQUENCE { |
| 592 | * tbsCertificate TBSCertificate, |
| 593 | * signatureAlgorithm AlgorithmIdentifier, |
| 594 | * signatureValue BIT STRING } |
| 595 | |
| 596 | * TBSCertificate ::= SEQUENCE { |
| 597 | * version [0] EXPLICIT Version DEFAULT v1, |
| 598 | * serialNumber CertificateSerialNumber, |
| 599 | * signature AlgorithmIdentifier, |
| 600 | * issuer Name, |
| 601 | * validity Validity, |
| 602 | * subject Name, |
| 603 | * subjectPublicKeyInfo SubjectPublicKeyInfo, |
| 604 | * ... } */ |
| 605 | CBS buf = *in; |
| 606 | |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 607 | CBS toplevel; |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 608 | if (!CBS_get_asn1(&buf, &toplevel, CBS_ASN1_SEQUENCE) || |
| 609 | CBS_len(&buf) != 0 || |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 610 | !CBS_get_asn1(&toplevel, out_tbs_cert, CBS_ASN1_SEQUENCE) || |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 611 | /* version */ |
| 612 | !CBS_get_optional_asn1( |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 613 | out_tbs_cert, NULL, NULL, |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 614 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 0) || |
| 615 | /* serialNumber */ |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 616 | !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_INTEGER) || |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 617 | /* signature algorithm */ |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 618 | !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) || |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 619 | /* issuer */ |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 620 | !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) || |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 621 | /* validity */ |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 622 | !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) || |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 623 | /* subject */ |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 624 | !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE)) { |
| 625 | return 0; |
| 626 | } |
| 627 | |
| 628 | return 1; |
| 629 | } |
| 630 | |
| 631 | EVP_PKEY *ssl_cert_parse_pubkey(const CBS *in) { |
David Benjamin | 5b410b6 | 2017-01-03 08:08:57 -0500 | [diff] [blame] | 632 | CBS buf = *in, tbs_cert; |
| 633 | if (!ssl_cert_skip_to_spki(&buf, &tbs_cert)) { |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 634 | OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT); |
| 635 | return NULL; |
| 636 | } |
| 637 | |
David Benjamin | 5b410b6 | 2017-01-03 08:08:57 -0500 | [diff] [blame] | 638 | return EVP_parse_public_key(&tbs_cert); |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 639 | } |
| 640 | |
Adam Langley | c26692c | 2017-01-25 09:34:42 -0800 | [diff] [blame] | 641 | int ssl_compare_public_and_private_key(const EVP_PKEY *pubkey, |
| 642 | const EVP_PKEY *privkey) { |
Adam Langley | 3a2b47a | 2017-01-24 13:59:42 -0800 | [diff] [blame] | 643 | int ret = 0; |
| 644 | |
| 645 | switch (EVP_PKEY_cmp(pubkey, privkey)) { |
| 646 | case 1: |
| 647 | ret = 1; |
| 648 | break; |
| 649 | case 0: |
| 650 | OPENSSL_PUT_ERROR(X509, X509_R_KEY_VALUES_MISMATCH); |
| 651 | break; |
| 652 | case -1: |
| 653 | OPENSSL_PUT_ERROR(X509, X509_R_KEY_TYPE_MISMATCH); |
| 654 | break; |
| 655 | case -2: |
| 656 | OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_KEY_TYPE); |
| 657 | default: |
| 658 | assert(0); |
| 659 | break; |
| 660 | } |
| 661 | |
Adam Langley | 3a2b47a | 2017-01-24 13:59:42 -0800 | [diff] [blame] | 662 | return ret; |
| 663 | } |
| 664 | |
| 665 | int ssl_cert_check_private_key(const CERT *cert, const EVP_PKEY *privkey) { |
| 666 | if (privkey == NULL) { |
| 667 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED); |
| 668 | return 0; |
| 669 | } |
| 670 | |
| 671 | if (cert->chain == NULL || |
| 672 | sk_CRYPTO_BUFFER_value(cert->chain, 0) == NULL) { |
| 673 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED); |
| 674 | return 0; |
| 675 | } |
| 676 | |
Adam Langley | c26692c | 2017-01-25 09:34:42 -0800 | [diff] [blame] | 677 | CBS cert_cbs; |
| 678 | CRYPTO_BUFFER_init_CBS(sk_CRYPTO_BUFFER_value(cert->chain, 0), &cert_cbs); |
| 679 | EVP_PKEY *pubkey = ssl_cert_parse_pubkey(&cert_cbs); |
| 680 | if (!pubkey) { |
| 681 | OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_KEY_TYPE); |
| 682 | return 0; |
| 683 | } |
| 684 | |
| 685 | const int ok = ssl_compare_public_and_private_key(pubkey, privkey); |
| 686 | EVP_PKEY_free(pubkey); |
| 687 | return ok; |
Adam Langley | 3a2b47a | 2017-01-24 13:59:42 -0800 | [diff] [blame] | 688 | } |
| 689 | |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 690 | int ssl_cert_check_digital_signature_key_usage(const CBS *in) { |
| 691 | CBS buf = *in; |
| 692 | |
| 693 | CBS tbs_cert, outer_extensions; |
| 694 | int has_extensions; |
| 695 | if (!ssl_cert_skip_to_spki(&buf, &tbs_cert) || |
| 696 | /* subjectPublicKeyInfo */ |
| 697 | !CBS_get_asn1(&tbs_cert, NULL, CBS_ASN1_SEQUENCE) || |
| 698 | /* issuerUniqueID */ |
| 699 | !CBS_get_optional_asn1( |
| 700 | &tbs_cert, NULL, NULL, |
| 701 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1) || |
| 702 | /* subjectUniqueID */ |
| 703 | !CBS_get_optional_asn1( |
| 704 | &tbs_cert, NULL, NULL, |
| 705 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 2) || |
| 706 | !CBS_get_optional_asn1( |
| 707 | &tbs_cert, &outer_extensions, &has_extensions, |
| 708 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 3)) { |
| 709 | goto parse_err; |
| 710 | } |
| 711 | |
| 712 | if (!has_extensions) { |
| 713 | return 1; |
| 714 | } |
| 715 | |
| 716 | CBS extensions; |
| 717 | if (!CBS_get_asn1(&outer_extensions, &extensions, CBS_ASN1_SEQUENCE)) { |
| 718 | goto parse_err; |
| 719 | } |
| 720 | |
| 721 | while (CBS_len(&extensions) > 0) { |
| 722 | CBS extension, oid, contents; |
| 723 | if (!CBS_get_asn1(&extensions, &extension, CBS_ASN1_SEQUENCE) || |
| 724 | !CBS_get_asn1(&extension, &oid, CBS_ASN1_OBJECT) || |
| 725 | (CBS_peek_asn1_tag(&extension, CBS_ASN1_BOOLEAN) && |
| 726 | !CBS_get_asn1(&extension, NULL, CBS_ASN1_BOOLEAN)) || |
| 727 | !CBS_get_asn1(&extension, &contents, CBS_ASN1_OCTETSTRING) || |
| 728 | CBS_len(&extension) != 0) { |
| 729 | goto parse_err; |
| 730 | } |
| 731 | |
| 732 | static const uint8_t kKeyUsageOID[3] = {0x55, 0x1d, 0x0f}; |
| 733 | if (CBS_len(&oid) != sizeof(kKeyUsageOID) || |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 734 | OPENSSL_memcmp(CBS_data(&oid), kKeyUsageOID, sizeof(kKeyUsageOID)) != |
| 735 | 0) { |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 736 | continue; |
| 737 | } |
| 738 | |
| 739 | CBS bit_string; |
| 740 | if (!CBS_get_asn1(&contents, &bit_string, CBS_ASN1_BITSTRING) || |
| 741 | CBS_len(&contents) != 0) { |
| 742 | goto parse_err; |
| 743 | } |
| 744 | |
| 745 | /* This is the KeyUsage extension. See |
| 746 | * https://tools.ietf.org/html/rfc5280#section-4.2.1.3 */ |
| 747 | if (!CBS_is_valid_asn1_bitstring(&bit_string)) { |
| 748 | goto parse_err; |
| 749 | } |
| 750 | |
| 751 | if (!CBS_asn1_bitstring_has_bit(&bit_string, 0)) { |
| 752 | OPENSSL_PUT_ERROR(SSL, SSL_R_ECC_CERT_NOT_FOR_SIGNING); |
| 753 | return 0; |
| 754 | } |
| 755 | |
| 756 | return 1; |
| 757 | } |
| 758 | |
| 759 | /* No KeyUsage extension found. */ |
| 760 | return 1; |
| 761 | |
| 762 | parse_err: |
| 763 | OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT); |
| 764 | return 0; |
| 765 | } |
| 766 | |
David Benjamin | e0332e8 | 2016-07-13 22:40:36 -0400 | [diff] [blame] | 767 | static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b) { |
| 768 | return X509_NAME_cmp(*a, *b); |
| 769 | } |
| 770 | |
| 771 | STACK_OF(X509_NAME) * |
| 772 | ssl_parse_client_CA_list(SSL *ssl, uint8_t *out_alert, CBS *cbs) { |
| 773 | STACK_OF(X509_NAME) *ret = sk_X509_NAME_new(ca_dn_cmp); |
| 774 | X509_NAME *name = NULL; |
| 775 | if (ret == NULL) { |
| 776 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 777 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 778 | return NULL; |
| 779 | } |
| 780 | |
| 781 | CBS child; |
| 782 | if (!CBS_get_u16_length_prefixed(cbs, &child)) { |
| 783 | *out_alert = SSL_AD_DECODE_ERROR; |
| 784 | OPENSSL_PUT_ERROR(SSL, SSL_R_LENGTH_MISMATCH); |
| 785 | goto err; |
| 786 | } |
| 787 | |
| 788 | while (CBS_len(&child) > 0) { |
| 789 | CBS distinguished_name; |
| 790 | if (!CBS_get_u16_length_prefixed(&child, &distinguished_name)) { |
| 791 | *out_alert = SSL_AD_DECODE_ERROR; |
| 792 | OPENSSL_PUT_ERROR(SSL, SSL_R_CA_DN_TOO_LONG); |
| 793 | goto err; |
| 794 | } |
| 795 | |
| 796 | const uint8_t *ptr = CBS_data(&distinguished_name); |
| 797 | /* A u16 length cannot overflow a long. */ |
| 798 | name = d2i_X509_NAME(NULL, &ptr, (long)CBS_len(&distinguished_name)); |
| 799 | if (name == NULL || |
| 800 | ptr != CBS_data(&distinguished_name) + CBS_len(&distinguished_name)) { |
| 801 | *out_alert = SSL_AD_DECODE_ERROR; |
| 802 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 803 | goto err; |
| 804 | } |
| 805 | |
| 806 | if (!sk_X509_NAME_push(ret, name)) { |
| 807 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 808 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 809 | goto err; |
| 810 | } |
| 811 | name = NULL; |
| 812 | } |
| 813 | |
| 814 | return ret; |
| 815 | |
| 816 | err: |
| 817 | X509_NAME_free(name); |
| 818 | sk_X509_NAME_pop_free(ret, X509_NAME_free); |
| 819 | return NULL; |
| 820 | } |
| 821 | |
David Benjamin | 32a66d5 | 2016-07-13 22:03:11 -0400 | [diff] [blame] | 822 | int ssl_add_client_CA_list(SSL *ssl, CBB *cbb) { |
| 823 | CBB child, name_cbb; |
| 824 | if (!CBB_add_u16_length_prefixed(cbb, &child)) { |
| 825 | return 0; |
| 826 | } |
| 827 | |
| 828 | STACK_OF(X509_NAME) *sk = SSL_get_client_CA_list(ssl); |
| 829 | if (sk == NULL) { |
| 830 | return CBB_flush(cbb); |
| 831 | } |
| 832 | |
| 833 | for (size_t i = 0; i < sk_X509_NAME_num(sk); i++) { |
| 834 | X509_NAME *name = sk_X509_NAME_value(sk, i); |
| 835 | int len = i2d_X509_NAME(name, NULL); |
| 836 | if (len < 0) { |
| 837 | return 0; |
| 838 | } |
| 839 | uint8_t *ptr; |
| 840 | if (!CBB_add_u16_length_prefixed(&child, &name_cbb) || |
| 841 | !CBB_add_space(&name_cbb, &ptr, (size_t)len) || |
| 842 | (len > 0 && i2d_X509_NAME(name, &ptr) < 0)) { |
| 843 | return 0; |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | return CBB_flush(cbb); |
| 848 | } |
| 849 | |
Adam Langley | d323f4b | 2016-03-01 15:58:14 -0800 | [diff] [blame] | 850 | static int set_cert_store(X509_STORE **store_ptr, X509_STORE *new_store, int take_ref) { |
| 851 | X509_STORE_free(*store_ptr); |
| 852 | *store_ptr = new_store; |
| 853 | |
| 854 | if (new_store != NULL && take_ref) { |
| 855 | X509_STORE_up_ref(new_store); |
| 856 | } |
| 857 | |
| 858 | return 1; |
| 859 | } |
| 860 | |
| 861 | int SSL_CTX_set0_verify_cert_store(SSL_CTX *ctx, X509_STORE *store) { |
| 862 | return set_cert_store(&ctx->cert->verify_store, store, 0); |
| 863 | } |
| 864 | |
| 865 | int SSL_CTX_set1_verify_cert_store(SSL_CTX *ctx, X509_STORE *store) { |
| 866 | return set_cert_store(&ctx->cert->verify_store, store, 1); |
| 867 | } |
| 868 | |
| 869 | int SSL_set0_verify_cert_store(SSL *ssl, X509_STORE *store) { |
| 870 | return set_cert_store(&ssl->cert->verify_store, store, 0); |
| 871 | } |
| 872 | |
| 873 | int SSL_set1_verify_cert_store(SSL *ssl, X509_STORE *store) { |
| 874 | return set_cert_store(&ssl->cert->verify_store, store, 1); |
| 875 | } |
| 876 | |
David Benjamin | a2bda9f | 2016-12-03 23:29:05 -0500 | [diff] [blame] | 877 | void SSL_CTX_set_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl, void *arg), |
| 878 | void *arg) { |
| 879 | ssl_cert_set_cert_cb(ctx->cert, cb, arg); |
| 880 | } |
| 881 | |
| 882 | void SSL_set_cert_cb(SSL *ssl, int (*cb)(SSL *ssl, void *arg), void *arg) { |
| 883 | ssl_cert_set_cert_cb(ssl->cert, cb, arg); |
| 884 | } |
| 885 | |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 886 | int ssl_check_leaf_certificate(SSL *ssl, EVP_PKEY *pkey, |
| 887 | const CRYPTO_BUFFER *leaf) { |
David Benjamin | 938fa7c | 2016-10-07 00:27:05 -0400 | [diff] [blame] | 888 | assert(ssl3_protocol_version(ssl) < TLS1_3_VERSION); |
| 889 | |
Steven Valdez | bf5aa84 | 2016-07-15 07:07:40 -0400 | [diff] [blame] | 890 | /* Check the certificate's type matches the cipher. */ |
| 891 | const SSL_CIPHER *cipher = ssl->s3->tmp.new_cipher; |
| 892 | int expected_type = ssl_cipher_get_key_type(cipher); |
| 893 | assert(expected_type != EVP_PKEY_NONE); |
| 894 | if (pkey->type != expected_type) { |
| 895 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CERTIFICATE_TYPE); |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 896 | return 0; |
Steven Valdez | bf5aa84 | 2016-07-15 07:07:40 -0400 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | if (cipher->algorithm_auth & SSL_aECDSA) { |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 900 | CBS leaf_cbs; |
| 901 | CBS_init(&leaf_cbs, CRYPTO_BUFFER_data(leaf), CRYPTO_BUFFER_len(leaf)); |
| 902 | /* ECDSA and ECDH certificates use the same public key format. Instead, |
| 903 | * they are distinguished by the key usage extension in the certificate. */ |
| 904 | if (!ssl_cert_check_digital_signature_key_usage(&leaf_cbs)) { |
| 905 | return 0; |
Steven Valdez | bf5aa84 | 2016-07-15 07:07:40 -0400 | [diff] [blame] | 906 | } |
| 907 | |
David Benjamin | 938fa7c | 2016-10-07 00:27:05 -0400 | [diff] [blame] | 908 | EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey); |
| 909 | if (ec_key == NULL) { |
| 910 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECC_CERT); |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 911 | return 0; |
David Benjamin | 938fa7c | 2016-10-07 00:27:05 -0400 | [diff] [blame] | 912 | } |
| 913 | |
| 914 | /* Check the key's group and point format are acceptable. */ |
| 915 | uint16_t group_id; |
| 916 | if (!ssl_nid_to_group_id( |
| 917 | &group_id, EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key))) || |
| 918 | !tls1_check_group_id(ssl, group_id) || |
| 919 | EC_KEY_get_conv_form(ec_key) != POINT_CONVERSION_UNCOMPRESSED) { |
Steven Valdez | bf5aa84 | 2016-07-15 07:07:40 -0400 | [diff] [blame] | 920 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECC_CERT); |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 921 | return 0; |
Steven Valdez | bf5aa84 | 2016-07-15 07:07:40 -0400 | [diff] [blame] | 922 | } |
| 923 | } |
| 924 | |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 925 | return 1; |
Steven Valdez | bf5aa84 | 2016-07-15 07:07:40 -0400 | [diff] [blame] | 926 | } |
David Benjamin | 5edfc8c | 2016-12-10 15:46:58 -0500 | [diff] [blame] | 927 | |
| 928 | static int do_client_cert_cb(SSL *ssl, void *arg) { |
| 929 | if (ssl_has_certificate(ssl) || ssl->ctx->client_cert_cb == NULL) { |
| 930 | return 1; |
| 931 | } |
| 932 | |
| 933 | X509 *x509 = NULL; |
| 934 | EVP_PKEY *pkey = NULL; |
| 935 | int ret = ssl->ctx->client_cert_cb(ssl, &x509, &pkey); |
| 936 | if (ret < 0) { |
| 937 | return -1; |
| 938 | } |
| 939 | |
| 940 | if (ret != 0) { |
| 941 | if (!SSL_use_certificate(ssl, x509) || |
| 942 | !SSL_use_PrivateKey(ssl, pkey)) { |
| 943 | return 0; |
| 944 | } |
| 945 | } |
| 946 | |
| 947 | X509_free(x509); |
| 948 | EVP_PKEY_free(pkey); |
| 949 | return 1; |
| 950 | } |
| 951 | |
| 952 | void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl, |
| 953 | X509 **out_x509, |
| 954 | EVP_PKEY **out_pkey)) { |
| 955 | /* Emulate the old client certificate callback with the new one. */ |
| 956 | SSL_CTX_set_cert_cb(ctx, do_client_cert_cb, NULL); |
| 957 | ctx->client_cert_cb = cb; |
| 958 | } |