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 | 3509dac | 2017-02-01 11:59:18 -0800 | [diff] [blame] | 136 | CERT *ssl_cert_new(const SSL_X509_METHOD *x509_method) { |
Brian Smith | 5ba0689 | 2016-02-07 09:36:04 -1000 | [diff] [blame] | 137 | CERT *ret = OPENSSL_malloc(sizeof(CERT)); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 138 | if (ret == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 139 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 140 | return NULL; |
| 141 | } |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 142 | OPENSSL_memset(ret, 0, sizeof(CERT)); |
Adam Langley | 3509dac | 2017-02-01 11:59:18 -0800 | [diff] [blame] | 143 | ret->x509_method = x509_method; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 144 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 145 | return ret; |
| 146 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 147 | |
Adam Langley | 3a2b47a | 2017-01-24 13:59:42 -0800 | [diff] [blame] | 148 | static CRYPTO_BUFFER *buffer_up_ref(CRYPTO_BUFFER *buffer) { |
| 149 | CRYPTO_BUFFER_up_ref(buffer); |
| 150 | return buffer; |
| 151 | } |
| 152 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 153 | CERT *ssl_cert_dup(CERT *cert) { |
Brian Smith | 5ba0689 | 2016-02-07 09:36:04 -1000 | [diff] [blame] | 154 | CERT *ret = OPENSSL_malloc(sizeof(CERT)); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 155 | if (ret == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 156 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 157 | return NULL; |
| 158 | } |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 159 | OPENSSL_memset(ret, 0, sizeof(CERT)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 160 | |
Adam Langley | 3a2b47a | 2017-01-24 13:59:42 -0800 | [diff] [blame] | 161 | ret->chain = sk_CRYPTO_BUFFER_deep_copy(cert->chain, buffer_up_ref, |
| 162 | CRYPTO_BUFFER_free); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 163 | |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 164 | if (cert->privatekey != NULL) { |
Adam Langley | 310d3f6 | 2016-07-12 10:39:20 -0700 | [diff] [blame] | 165 | EVP_PKEY_up_ref(cert->privatekey); |
| 166 | ret->privatekey = cert->privatekey; |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 167 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 168 | |
Tom Thorogood | 66b2fe8 | 2016-03-06 20:08:38 +1030 | [diff] [blame] | 169 | ret->key_method = cert->key_method; |
Adam Langley | 3509dac | 2017-02-01 11:59:18 -0800 | [diff] [blame] | 170 | ret->x509_method = cert->x509_method; |
Tom Thorogood | 66b2fe8 | 2016-03-06 20:08:38 +1030 | [diff] [blame] | 171 | |
David Benjamin | 0fc37ef | 2016-08-17 15:29:46 -0400 | [diff] [blame] | 172 | if (cert->dh_tmp != NULL) { |
| 173 | ret->dh_tmp = DHparams_dup(cert->dh_tmp); |
| 174 | if (ret->dh_tmp == NULL) { |
| 175 | OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB); |
| 176 | goto err; |
| 177 | } |
| 178 | } |
| 179 | ret->dh_tmp_cb = cert->dh_tmp_cb; |
| 180 | |
| 181 | if (cert->sigalgs != NULL) { |
| 182 | ret->sigalgs = |
| 183 | BUF_memdup(cert->sigalgs, cert->num_sigalgs * sizeof(cert->sigalgs[0])); |
| 184 | if (ret->sigalgs == NULL) { |
| 185 | goto err; |
| 186 | } |
| 187 | } |
| 188 | ret->num_sigalgs = cert->num_sigalgs; |
| 189 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 190 | ret->cert_cb = cert->cert_cb; |
| 191 | ret->cert_cb_arg = cert->cert_cb_arg; |
| 192 | |
Adam Langley | 2a3b343 | 2017-02-10 13:56:48 -0800 | [diff] [blame] | 193 | ret->x509_method->cert_dup(ret, cert); |
Adam Langley | d323f4b | 2016-03-01 15:58:14 -0800 | [diff] [blame] | 194 | |
David Benjamin | 83a3212 | 2017-02-14 18:34:54 -0500 | [diff] [blame] | 195 | if (cert->signed_cert_timestamp_list != NULL) { |
| 196 | CRYPTO_BUFFER_up_ref(cert->signed_cert_timestamp_list); |
| 197 | ret->signed_cert_timestamp_list = cert->signed_cert_timestamp_list; |
| 198 | } |
| 199 | |
| 200 | if (cert->ocsp_response != NULL) { |
| 201 | CRYPTO_BUFFER_up_ref(cert->ocsp_response); |
| 202 | ret->ocsp_response = cert->ocsp_response; |
| 203 | } |
| 204 | |
David Benjamin | 5960a90 | 2017-02-14 20:07:11 -0500 | [diff] [blame] | 205 | ret->sid_ctx_length = cert->sid_ctx_length; |
| 206 | OPENSSL_memcpy(ret->sid_ctx, cert->sid_ctx, sizeof(ret->sid_ctx)); |
| 207 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 208 | return ret; |
| 209 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 210 | err: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 211 | ssl_cert_free(ret); |
| 212 | return NULL; |
| 213 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 214 | |
| 215 | /* Free up and clear all certificates and chains */ |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 216 | void ssl_cert_clear_certs(CERT *cert) { |
| 217 | if (cert == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 218 | return; |
| 219 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 220 | |
Adam Langley | 3509dac | 2017-02-01 11:59:18 -0800 | [diff] [blame] | 221 | cert->x509_method->cert_clear(cert); |
Adam Langley | e1e7813 | 2017-01-31 15:24:31 -0800 | [diff] [blame] | 222 | |
Adam Langley | 3a2b47a | 2017-01-24 13:59:42 -0800 | [diff] [blame] | 223 | sk_CRYPTO_BUFFER_pop_free(cert->chain, CRYPTO_BUFFER_free); |
| 224 | cert->chain = NULL; |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 225 | EVP_PKEY_free(cert->privatekey); |
| 226 | cert->privatekey = NULL; |
David Benjamin | 71d2e54 | 2015-07-06 00:30:25 -0400 | [diff] [blame] | 227 | cert->key_method = NULL; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 228 | } |
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 | void ssl_cert_free(CERT *c) { |
| 231 | if (c == NULL) { |
| 232 | return; |
| 233 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 234 | |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 235 | DH_free(c->dh_tmp); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 236 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 237 | ssl_cert_clear_certs(c); |
Adam Langley | 2a3b343 | 2017-02-10 13:56:48 -0800 | [diff] [blame] | 238 | c->x509_method->cert_free(c); |
David Benjamin | d246b81 | 2016-07-08 15:07:02 -0700 | [diff] [blame] | 239 | OPENSSL_free(c->sigalgs); |
David Benjamin | 83a3212 | 2017-02-14 18:34:54 -0500 | [diff] [blame] | 240 | CRYPTO_BUFFER_free(c->signed_cert_timestamp_list); |
| 241 | CRYPTO_BUFFER_free(c->ocsp_response); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 242 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 243 | OPENSSL_free(c); |
| 244 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 245 | |
David Benjamin | a2bda9f | 2016-12-03 23:29:05 -0500 | [diff] [blame] | 246 | static void ssl_cert_set_cert_cb(CERT *c, int (*cb)(SSL *ssl, void *arg), |
| 247 | void *arg) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 248 | c->cert_cb = cb; |
| 249 | c->cert_cb_arg = arg; |
| 250 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 251 | |
Adam Langley | 52940c4 | 2017-02-01 12:40:31 -0800 | [diff] [blame] | 252 | int ssl_set_cert(CERT *cert, CRYPTO_BUFFER *buffer) { |
| 253 | CBS cert_cbs; |
| 254 | CRYPTO_BUFFER_init_CBS(buffer, &cert_cbs); |
| 255 | EVP_PKEY *pubkey = ssl_cert_parse_pubkey(&cert_cbs); |
| 256 | if (pubkey == NULL) { |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | if (!ssl_is_key_type_supported(pubkey->type)) { |
| 261 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE); |
| 262 | EVP_PKEY_free(pubkey); |
| 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | /* An ECC certificate may be usable for ECDH or ECDSA. We only support ECDSA |
| 267 | * certificates, so sanity-check the key usage extension. */ |
| 268 | if (pubkey->type == EVP_PKEY_EC && |
| 269 | !ssl_cert_check_digital_signature_key_usage(&cert_cbs)) { |
| 270 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE); |
| 271 | EVP_PKEY_free(pubkey); |
| 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | if (cert->privatekey != NULL) { |
| 276 | /* Sanity-check that the private key and the certificate match, unless the |
| 277 | * key is opaque (in case of, say, a smartcard). */ |
| 278 | if (!EVP_PKEY_is_opaque(cert->privatekey) && |
| 279 | !ssl_compare_public_and_private_key(pubkey, cert->privatekey)) { |
| 280 | /* don't fail for a cert/key mismatch, just free current private key |
| 281 | * (when switching to a different cert & key, first this function should |
| 282 | * be used, then ssl_set_pkey */ |
| 283 | EVP_PKEY_free(cert->privatekey); |
| 284 | cert->privatekey = NULL; |
| 285 | /* clear error queue */ |
| 286 | ERR_clear_error(); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | EVP_PKEY_free(pubkey); |
| 291 | |
| 292 | cert->x509_method->cert_flush_cached_leaf(cert); |
| 293 | |
| 294 | if (cert->chain != NULL) { |
| 295 | CRYPTO_BUFFER_free(sk_CRYPTO_BUFFER_value(cert->chain, 0)); |
| 296 | sk_CRYPTO_BUFFER_set(cert->chain, 0, buffer); |
| 297 | CRYPTO_BUFFER_up_ref(buffer); |
| 298 | return 1; |
| 299 | } |
| 300 | |
| 301 | cert->chain = sk_CRYPTO_BUFFER_new_null(); |
| 302 | if (cert->chain == NULL) { |
| 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | if (!sk_CRYPTO_BUFFER_push(cert->chain, buffer)) { |
| 307 | sk_CRYPTO_BUFFER_free(cert->chain); |
| 308 | cert->chain = NULL; |
| 309 | return 0; |
| 310 | } |
| 311 | CRYPTO_BUFFER_up_ref(buffer); |
| 312 | |
| 313 | return 1; |
| 314 | } |
| 315 | |
| 316 | int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, size_t der_len, |
| 317 | const uint8_t *der) { |
| 318 | CRYPTO_BUFFER *buffer = CRYPTO_BUFFER_new(der, der_len, NULL); |
| 319 | if (buffer == NULL) { |
| 320 | return 0; |
| 321 | } |
| 322 | |
| 323 | const int ok = ssl_set_cert(ctx->cert, buffer); |
| 324 | CRYPTO_BUFFER_free(buffer); |
| 325 | return ok; |
| 326 | } |
| 327 | |
| 328 | int SSL_use_certificate_ASN1(SSL *ssl, const uint8_t *der, size_t der_len) { |
| 329 | CRYPTO_BUFFER *buffer = CRYPTO_BUFFER_new(der, der_len, NULL); |
| 330 | if (buffer == NULL) { |
| 331 | return 0; |
| 332 | } |
| 333 | |
| 334 | const int ok = ssl_set_cert(ssl->cert, buffer); |
| 335 | CRYPTO_BUFFER_free(buffer); |
| 336 | return ok; |
| 337 | } |
| 338 | |
David Benjamin | 32a66d5 | 2016-07-13 22:03:11 -0400 | [diff] [blame] | 339 | int ssl_has_certificate(const SSL *ssl) { |
Adam Langley | 3a2b47a | 2017-01-24 13:59:42 -0800 | [diff] [blame] | 340 | return ssl->cert->chain != NULL && |
| 341 | sk_CRYPTO_BUFFER_value(ssl->cert->chain, 0) != NULL && |
| 342 | ssl_has_private_key(ssl); |
David Benjamin | 32a66d5 | 2016-07-13 22:03:11 -0400 | [diff] [blame] | 343 | } |
| 344 | |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 345 | STACK_OF(CRYPTO_BUFFER) *ssl_parse_cert_chain(uint8_t *out_alert, |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 346 | EVP_PKEY **out_pubkey, |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 347 | uint8_t *out_leaf_sha256, |
Adam Langley | d519bf6 | 2016-12-12 11:16:44 -0800 | [diff] [blame] | 348 | CBS *cbs, |
| 349 | CRYPTO_BUFFER_POOL *pool) { |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 350 | *out_pubkey = NULL; |
| 351 | |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 352 | STACK_OF(CRYPTO_BUFFER) *ret = sk_CRYPTO_BUFFER_new_null(); |
Adam Langley | c0fc7a1 | 2016-12-09 15:05:34 -0800 | [diff] [blame] | 353 | if (ret == NULL) { |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 354 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 355 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | c0fc7a1 | 2016-12-09 15:05:34 -0800 | [diff] [blame] | 356 | return NULL; |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 357 | } |
| 358 | |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 359 | CBS certificate_list; |
| 360 | if (!CBS_get_u24_length_prefixed(cbs, &certificate_list)) { |
| 361 | *out_alert = SSL_AD_DECODE_ERROR; |
| 362 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 363 | goto err; |
| 364 | } |
| 365 | |
| 366 | while (CBS_len(&certificate_list) > 0) { |
| 367 | CBS certificate; |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 368 | if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate) || |
| 369 | CBS_len(&certificate) == 0) { |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 370 | *out_alert = SSL_AD_DECODE_ERROR; |
| 371 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH); |
| 372 | goto err; |
| 373 | } |
| 374 | |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 375 | if (sk_CRYPTO_BUFFER_num(ret) == 0) { |
| 376 | *out_pubkey = ssl_cert_parse_pubkey(&certificate); |
| 377 | if (*out_pubkey == NULL) { |
Adam Langley | 7dccc71 | 2017-02-08 08:51:25 -0800 | [diff] [blame] | 378 | *out_alert = SSL_AD_DECODE_ERROR; |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 379 | goto err; |
| 380 | } |
| 381 | |
| 382 | /* Retain the hash of the leaf certificate if requested. */ |
| 383 | if (out_leaf_sha256 != NULL) { |
| 384 | SHA256(CBS_data(&certificate), CBS_len(&certificate), out_leaf_sha256); |
| 385 | } |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 386 | } |
| 387 | |
Adam Langley | d519bf6 | 2016-12-12 11:16:44 -0800 | [diff] [blame] | 388 | CRYPTO_BUFFER *buf = |
| 389 | CRYPTO_BUFFER_new_from_CBS(&certificate, pool); |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 390 | if (buf == NULL) { |
Adam Langley | c0fc7a1 | 2016-12-09 15:05:34 -0800 | [diff] [blame] | 391 | *out_alert = SSL_AD_DECODE_ERROR; |
| 392 | goto err; |
| 393 | } |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 394 | |
| 395 | if (!sk_CRYPTO_BUFFER_push(ret, buf)) { |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 396 | *out_alert = SSL_AD_INTERNAL_ERROR; |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 397 | CRYPTO_BUFFER_free(buf); |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 398 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 399 | goto err; |
| 400 | } |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 401 | } |
| 402 | |
Adam Langley | c0fc7a1 | 2016-12-09 15:05:34 -0800 | [diff] [blame] | 403 | return ret; |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 404 | |
| 405 | err: |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 406 | EVP_PKEY_free(*out_pubkey); |
| 407 | *out_pubkey = NULL; |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 408 | sk_CRYPTO_BUFFER_pop_free(ret, CRYPTO_BUFFER_free); |
Adam Langley | c0fc7a1 | 2016-12-09 15:05:34 -0800 | [diff] [blame] | 409 | return NULL; |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 410 | } |
| 411 | |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 412 | int ssl_add_cert_chain(SSL *ssl, CBB *cbb) { |
David Benjamin | 32a66d5 | 2016-07-13 22:03:11 -0400 | [diff] [blame] | 413 | if (!ssl_has_certificate(ssl)) { |
| 414 | return CBB_add_u24(cbb, 0); |
| 415 | } |
| 416 | |
Adam Langley | 3a2b47a | 2017-01-24 13:59:42 -0800 | [diff] [blame] | 417 | CBB certs; |
| 418 | if (!CBB_add_u24_length_prefixed(cbb, &certs)) { |
| 419 | goto err; |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 420 | } |
| 421 | |
Adam Langley | 3a2b47a | 2017-01-24 13:59:42 -0800 | [diff] [blame] | 422 | STACK_OF(CRYPTO_BUFFER) *chain = ssl->cert->chain; |
| 423 | for (size_t i = 0; i < sk_CRYPTO_BUFFER_num(chain); i++) { |
| 424 | CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(chain, i); |
| 425 | CBB child; |
| 426 | if (!CBB_add_u24_length_prefixed(&certs, &child) || |
| 427 | !CBB_add_bytes(&child, CRYPTO_BUFFER_data(buffer), |
| 428 | CRYPTO_BUFFER_len(buffer)) || |
| 429 | !CBB_flush(&certs)) { |
| 430 | goto err; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 434 | return CBB_flush(cbb); |
Adam Langley | 3a2b47a | 2017-01-24 13:59:42 -0800 | [diff] [blame] | 435 | |
| 436 | err: |
| 437 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 438 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 439 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 440 | |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 441 | /* ssl_cert_skip_to_spki parses a DER-encoded, X.509 certificate from |in| and |
| 442 | * positions |*out_tbs_cert| to cover the TBSCertificate, starting at the |
| 443 | * subjectPublicKeyInfo. */ |
| 444 | 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] | 445 | /* From RFC 5280, section 4.1 |
| 446 | * Certificate ::= SEQUENCE { |
| 447 | * tbsCertificate TBSCertificate, |
| 448 | * signatureAlgorithm AlgorithmIdentifier, |
| 449 | * signatureValue BIT STRING } |
| 450 | |
| 451 | * TBSCertificate ::= SEQUENCE { |
| 452 | * version [0] EXPLICIT Version DEFAULT v1, |
| 453 | * serialNumber CertificateSerialNumber, |
| 454 | * signature AlgorithmIdentifier, |
| 455 | * issuer Name, |
| 456 | * validity Validity, |
| 457 | * subject Name, |
| 458 | * subjectPublicKeyInfo SubjectPublicKeyInfo, |
| 459 | * ... } */ |
| 460 | CBS buf = *in; |
| 461 | |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 462 | CBS toplevel; |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 463 | if (!CBS_get_asn1(&buf, &toplevel, CBS_ASN1_SEQUENCE) || |
| 464 | CBS_len(&buf) != 0 || |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 465 | !CBS_get_asn1(&toplevel, out_tbs_cert, CBS_ASN1_SEQUENCE) || |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 466 | /* version */ |
| 467 | !CBS_get_optional_asn1( |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 468 | out_tbs_cert, NULL, NULL, |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 469 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 0) || |
| 470 | /* serialNumber */ |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 471 | !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_INTEGER) || |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 472 | /* signature algorithm */ |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 473 | !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) || |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 474 | /* issuer */ |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 475 | !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) || |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 476 | /* validity */ |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 477 | !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) || |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 478 | /* subject */ |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 479 | !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE)) { |
| 480 | return 0; |
| 481 | } |
| 482 | |
| 483 | return 1; |
| 484 | } |
| 485 | |
| 486 | EVP_PKEY *ssl_cert_parse_pubkey(const CBS *in) { |
David Benjamin | 5b410b6 | 2017-01-03 08:08:57 -0500 | [diff] [blame] | 487 | CBS buf = *in, tbs_cert; |
| 488 | if (!ssl_cert_skip_to_spki(&buf, &tbs_cert)) { |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 489 | OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT); |
| 490 | return NULL; |
| 491 | } |
| 492 | |
David Benjamin | 5b410b6 | 2017-01-03 08:08:57 -0500 | [diff] [blame] | 493 | return EVP_parse_public_key(&tbs_cert); |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 494 | } |
| 495 | |
Adam Langley | c26692c | 2017-01-25 09:34:42 -0800 | [diff] [blame] | 496 | int ssl_compare_public_and_private_key(const EVP_PKEY *pubkey, |
| 497 | const EVP_PKEY *privkey) { |
Adam Langley | 3a2b47a | 2017-01-24 13:59:42 -0800 | [diff] [blame] | 498 | int ret = 0; |
| 499 | |
| 500 | switch (EVP_PKEY_cmp(pubkey, privkey)) { |
| 501 | case 1: |
| 502 | ret = 1; |
| 503 | break; |
| 504 | case 0: |
| 505 | OPENSSL_PUT_ERROR(X509, X509_R_KEY_VALUES_MISMATCH); |
| 506 | break; |
| 507 | case -1: |
| 508 | OPENSSL_PUT_ERROR(X509, X509_R_KEY_TYPE_MISMATCH); |
| 509 | break; |
| 510 | case -2: |
| 511 | OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_KEY_TYPE); |
| 512 | default: |
| 513 | assert(0); |
| 514 | break; |
| 515 | } |
| 516 | |
Adam Langley | 3a2b47a | 2017-01-24 13:59:42 -0800 | [diff] [blame] | 517 | return ret; |
| 518 | } |
| 519 | |
| 520 | int ssl_cert_check_private_key(const CERT *cert, const EVP_PKEY *privkey) { |
| 521 | if (privkey == NULL) { |
| 522 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED); |
| 523 | return 0; |
| 524 | } |
| 525 | |
| 526 | if (cert->chain == NULL || |
| 527 | sk_CRYPTO_BUFFER_value(cert->chain, 0) == NULL) { |
| 528 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED); |
| 529 | return 0; |
| 530 | } |
| 531 | |
Adam Langley | c26692c | 2017-01-25 09:34:42 -0800 | [diff] [blame] | 532 | CBS cert_cbs; |
| 533 | CRYPTO_BUFFER_init_CBS(sk_CRYPTO_BUFFER_value(cert->chain, 0), &cert_cbs); |
| 534 | EVP_PKEY *pubkey = ssl_cert_parse_pubkey(&cert_cbs); |
| 535 | if (!pubkey) { |
| 536 | OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_KEY_TYPE); |
| 537 | return 0; |
| 538 | } |
| 539 | |
| 540 | const int ok = ssl_compare_public_and_private_key(pubkey, privkey); |
| 541 | EVP_PKEY_free(pubkey); |
| 542 | return ok; |
Adam Langley | 3a2b47a | 2017-01-24 13:59:42 -0800 | [diff] [blame] | 543 | } |
| 544 | |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 545 | int ssl_cert_check_digital_signature_key_usage(const CBS *in) { |
| 546 | CBS buf = *in; |
| 547 | |
| 548 | CBS tbs_cert, outer_extensions; |
| 549 | int has_extensions; |
| 550 | if (!ssl_cert_skip_to_spki(&buf, &tbs_cert) || |
| 551 | /* subjectPublicKeyInfo */ |
| 552 | !CBS_get_asn1(&tbs_cert, NULL, CBS_ASN1_SEQUENCE) || |
| 553 | /* issuerUniqueID */ |
| 554 | !CBS_get_optional_asn1( |
| 555 | &tbs_cert, NULL, NULL, |
| 556 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1) || |
| 557 | /* subjectUniqueID */ |
| 558 | !CBS_get_optional_asn1( |
| 559 | &tbs_cert, NULL, NULL, |
| 560 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 2) || |
| 561 | !CBS_get_optional_asn1( |
| 562 | &tbs_cert, &outer_extensions, &has_extensions, |
| 563 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 3)) { |
| 564 | goto parse_err; |
| 565 | } |
| 566 | |
| 567 | if (!has_extensions) { |
| 568 | return 1; |
| 569 | } |
| 570 | |
| 571 | CBS extensions; |
| 572 | if (!CBS_get_asn1(&outer_extensions, &extensions, CBS_ASN1_SEQUENCE)) { |
| 573 | goto parse_err; |
| 574 | } |
| 575 | |
| 576 | while (CBS_len(&extensions) > 0) { |
| 577 | CBS extension, oid, contents; |
| 578 | if (!CBS_get_asn1(&extensions, &extension, CBS_ASN1_SEQUENCE) || |
| 579 | !CBS_get_asn1(&extension, &oid, CBS_ASN1_OBJECT) || |
| 580 | (CBS_peek_asn1_tag(&extension, CBS_ASN1_BOOLEAN) && |
| 581 | !CBS_get_asn1(&extension, NULL, CBS_ASN1_BOOLEAN)) || |
| 582 | !CBS_get_asn1(&extension, &contents, CBS_ASN1_OCTETSTRING) || |
| 583 | CBS_len(&extension) != 0) { |
| 584 | goto parse_err; |
| 585 | } |
| 586 | |
| 587 | static const uint8_t kKeyUsageOID[3] = {0x55, 0x1d, 0x0f}; |
| 588 | if (CBS_len(&oid) != sizeof(kKeyUsageOID) || |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 589 | OPENSSL_memcmp(CBS_data(&oid), kKeyUsageOID, sizeof(kKeyUsageOID)) != |
| 590 | 0) { |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 591 | continue; |
| 592 | } |
| 593 | |
| 594 | CBS bit_string; |
| 595 | if (!CBS_get_asn1(&contents, &bit_string, CBS_ASN1_BITSTRING) || |
| 596 | CBS_len(&contents) != 0) { |
| 597 | goto parse_err; |
| 598 | } |
| 599 | |
| 600 | /* This is the KeyUsage extension. See |
| 601 | * https://tools.ietf.org/html/rfc5280#section-4.2.1.3 */ |
| 602 | if (!CBS_is_valid_asn1_bitstring(&bit_string)) { |
| 603 | goto parse_err; |
| 604 | } |
| 605 | |
| 606 | if (!CBS_asn1_bitstring_has_bit(&bit_string, 0)) { |
| 607 | OPENSSL_PUT_ERROR(SSL, SSL_R_ECC_CERT_NOT_FOR_SIGNING); |
| 608 | return 0; |
| 609 | } |
| 610 | |
| 611 | return 1; |
| 612 | } |
| 613 | |
| 614 | /* No KeyUsage extension found. */ |
| 615 | return 1; |
| 616 | |
| 617 | parse_err: |
| 618 | OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT); |
| 619 | return 0; |
| 620 | } |
| 621 | |
Adam Langley | 34b4c82 | 2017-02-02 10:57:17 -0800 | [diff] [blame] | 622 | STACK_OF(CRYPTO_BUFFER) * |
David Benjamin | e0332e8 | 2016-07-13 22:40:36 -0400 | [diff] [blame] | 623 | ssl_parse_client_CA_list(SSL *ssl, uint8_t *out_alert, CBS *cbs) { |
Adam Langley | 34b4c82 | 2017-02-02 10:57:17 -0800 | [diff] [blame] | 624 | CRYPTO_BUFFER_POOL *const pool = ssl->ctx->pool; |
| 625 | |
| 626 | STACK_OF(CRYPTO_BUFFER) *ret = sk_CRYPTO_BUFFER_new_null(); |
David Benjamin | e0332e8 | 2016-07-13 22:40:36 -0400 | [diff] [blame] | 627 | if (ret == NULL) { |
| 628 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 629 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 630 | return NULL; |
| 631 | } |
| 632 | |
| 633 | CBS child; |
| 634 | if (!CBS_get_u16_length_prefixed(cbs, &child)) { |
| 635 | *out_alert = SSL_AD_DECODE_ERROR; |
| 636 | OPENSSL_PUT_ERROR(SSL, SSL_R_LENGTH_MISMATCH); |
| 637 | goto err; |
| 638 | } |
| 639 | |
| 640 | while (CBS_len(&child) > 0) { |
| 641 | CBS distinguished_name; |
| 642 | if (!CBS_get_u16_length_prefixed(&child, &distinguished_name)) { |
| 643 | *out_alert = SSL_AD_DECODE_ERROR; |
| 644 | OPENSSL_PUT_ERROR(SSL, SSL_R_CA_DN_TOO_LONG); |
| 645 | goto err; |
| 646 | } |
| 647 | |
Adam Langley | 34b4c82 | 2017-02-02 10:57:17 -0800 | [diff] [blame] | 648 | CRYPTO_BUFFER *buffer = |
| 649 | CRYPTO_BUFFER_new_from_CBS(&distinguished_name, pool); |
| 650 | if (buffer == NULL || |
| 651 | !sk_CRYPTO_BUFFER_push(ret, buffer)) { |
| 652 | CRYPTO_BUFFER_free(buffer); |
David Benjamin | e0332e8 | 2016-07-13 22:40:36 -0400 | [diff] [blame] | 653 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 654 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 655 | goto err; |
| 656 | } |
David Benjamin | e0332e8 | 2016-07-13 22:40:36 -0400 | [diff] [blame] | 657 | } |
| 658 | |
Adam Langley | 0bdef09 | 2017-02-23 15:02:58 -0800 | [diff] [blame] | 659 | if (!ssl->ctx->x509_method->check_client_CA_list(ret)) { |
| 660 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 661 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 662 | goto err; |
| 663 | } |
| 664 | |
David Benjamin | e0332e8 | 2016-07-13 22:40:36 -0400 | [diff] [blame] | 665 | return ret; |
| 666 | |
| 667 | err: |
Adam Langley | 34b4c82 | 2017-02-02 10:57:17 -0800 | [diff] [blame] | 668 | sk_CRYPTO_BUFFER_pop_free(ret, CRYPTO_BUFFER_free); |
David Benjamin | e0332e8 | 2016-07-13 22:40:36 -0400 | [diff] [blame] | 669 | return NULL; |
| 670 | } |
| 671 | |
David Benjamin | 32a66d5 | 2016-07-13 22:03:11 -0400 | [diff] [blame] | 672 | int ssl_add_client_CA_list(SSL *ssl, CBB *cbb) { |
| 673 | CBB child, name_cbb; |
| 674 | if (!CBB_add_u16_length_prefixed(cbb, &child)) { |
| 675 | return 0; |
| 676 | } |
| 677 | |
Adam Langley | 34b4c82 | 2017-02-02 10:57:17 -0800 | [diff] [blame] | 678 | STACK_OF(CRYPTO_BUFFER) *names = ssl->client_CA; |
| 679 | if (names == NULL) { |
| 680 | names = ssl->ctx->client_CA; |
| 681 | } |
| 682 | if (names == NULL) { |
David Benjamin | 32a66d5 | 2016-07-13 22:03:11 -0400 | [diff] [blame] | 683 | return CBB_flush(cbb); |
| 684 | } |
| 685 | |
Adam Langley | 34b4c82 | 2017-02-02 10:57:17 -0800 | [diff] [blame] | 686 | for (size_t i = 0; i < sk_CRYPTO_BUFFER_num(names); i++) { |
| 687 | const CRYPTO_BUFFER *name = sk_CRYPTO_BUFFER_value(names, i); |
| 688 | |
David Benjamin | 32a66d5 | 2016-07-13 22:03:11 -0400 | [diff] [blame] | 689 | if (!CBB_add_u16_length_prefixed(&child, &name_cbb) || |
Adam Langley | 34b4c82 | 2017-02-02 10:57:17 -0800 | [diff] [blame] | 690 | !CBB_add_bytes(&name_cbb, CRYPTO_BUFFER_data(name), |
| 691 | CRYPTO_BUFFER_len(name))) { |
David Benjamin | 32a66d5 | 2016-07-13 22:03:11 -0400 | [diff] [blame] | 692 | return 0; |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | return CBB_flush(cbb); |
| 697 | } |
| 698 | |
David Benjamin | a2bda9f | 2016-12-03 23:29:05 -0500 | [diff] [blame] | 699 | void SSL_CTX_set_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl, void *arg), |
| 700 | void *arg) { |
| 701 | ssl_cert_set_cert_cb(ctx->cert, cb, arg); |
| 702 | } |
| 703 | |
| 704 | void SSL_set_cert_cb(SSL *ssl, int (*cb)(SSL *ssl, void *arg), void *arg) { |
| 705 | ssl_cert_set_cert_cb(ssl->cert, cb, arg); |
| 706 | } |
| 707 | |
David Benjamin | f465461 | 2017-03-02 18:47:12 -0500 | [diff] [blame^] | 708 | STACK_OF(CRYPTO_BUFFER) *SSL_get0_peer_certificates(const SSL *ssl) { |
| 709 | SSL_SESSION *session = SSL_get_session(ssl); |
| 710 | if (session == NULL) { |
| 711 | return NULL; |
| 712 | } |
| 713 | |
| 714 | return session->certs; |
| 715 | } |
| 716 | |
Adam Langley | d6c22ee | 2017-03-02 12:56:32 -0800 | [diff] [blame] | 717 | STACK_OF(CRYPTO_BUFFER) *SSL_get0_server_requested_CAs(const SSL *ssl) { |
| 718 | if (ssl->s3->hs == NULL) { |
| 719 | return NULL; |
| 720 | } |
| 721 | return ssl->s3->hs->ca_names; |
| 722 | } |
| 723 | |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 724 | int ssl_check_leaf_certificate(SSL_HANDSHAKE *hs, EVP_PKEY *pkey, |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 725 | const CRYPTO_BUFFER *leaf) { |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 726 | SSL *const ssl = hs->ssl; |
David Benjamin | 938fa7c | 2016-10-07 00:27:05 -0400 | [diff] [blame] | 727 | assert(ssl3_protocol_version(ssl) < TLS1_3_VERSION); |
| 728 | |
Steven Valdez | bf5aa84 | 2016-07-15 07:07:40 -0400 | [diff] [blame] | 729 | /* Check the certificate's type matches the cipher. */ |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 730 | int expected_type = ssl_cipher_get_key_type(hs->new_cipher); |
Steven Valdez | bf5aa84 | 2016-07-15 07:07:40 -0400 | [diff] [blame] | 731 | assert(expected_type != EVP_PKEY_NONE); |
| 732 | if (pkey->type != expected_type) { |
| 733 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CERTIFICATE_TYPE); |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 734 | return 0; |
Steven Valdez | bf5aa84 | 2016-07-15 07:07:40 -0400 | [diff] [blame] | 735 | } |
| 736 | |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 737 | if (hs->new_cipher->algorithm_auth & SSL_aECDSA) { |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 738 | CBS leaf_cbs; |
| 739 | CBS_init(&leaf_cbs, CRYPTO_BUFFER_data(leaf), CRYPTO_BUFFER_len(leaf)); |
| 740 | /* ECDSA and ECDH certificates use the same public key format. Instead, |
| 741 | * they are distinguished by the key usage extension in the certificate. */ |
| 742 | if (!ssl_cert_check_digital_signature_key_usage(&leaf_cbs)) { |
| 743 | return 0; |
Steven Valdez | bf5aa84 | 2016-07-15 07:07:40 -0400 | [diff] [blame] | 744 | } |
| 745 | |
David Benjamin | 938fa7c | 2016-10-07 00:27:05 -0400 | [diff] [blame] | 746 | EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey); |
| 747 | if (ec_key == NULL) { |
| 748 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECC_CERT); |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 749 | return 0; |
David Benjamin | 938fa7c | 2016-10-07 00:27:05 -0400 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | /* Check the key's group and point format are acceptable. */ |
| 753 | uint16_t group_id; |
| 754 | if (!ssl_nid_to_group_id( |
| 755 | &group_id, EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key))) || |
| 756 | !tls1_check_group_id(ssl, group_id) || |
| 757 | EC_KEY_get_conv_form(ec_key) != POINT_CONVERSION_UNCOMPRESSED) { |
Steven Valdez | bf5aa84 | 2016-07-15 07:07:40 -0400 | [diff] [blame] | 758 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECC_CERT); |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 759 | return 0; |
Steven Valdez | bf5aa84 | 2016-07-15 07:07:40 -0400 | [diff] [blame] | 760 | } |
| 761 | } |
| 762 | |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 763 | return 1; |
Steven Valdez | bf5aa84 | 2016-07-15 07:07:40 -0400 | [diff] [blame] | 764 | } |
David Benjamin | 5edfc8c | 2016-12-10 15:46:58 -0500 | [diff] [blame] | 765 | |
David Benjamin | 83a3212 | 2017-02-14 18:34:54 -0500 | [diff] [blame] | 766 | static int set_signed_cert_timestamp_list(CERT *cert, const uint8_t *list, |
| 767 | size_t list_len) { |
| 768 | CBS sct_list; |
| 769 | CBS_init(&sct_list, list, list_len); |
| 770 | if (!ssl_is_sct_list_valid(&sct_list)) { |
| 771 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SCT_LIST); |
| 772 | return 0; |
| 773 | } |
| 774 | |
| 775 | CRYPTO_BUFFER_free(cert->signed_cert_timestamp_list); |
| 776 | cert->signed_cert_timestamp_list = |
| 777 | CRYPTO_BUFFER_new(CBS_data(&sct_list), CBS_len(&sct_list), NULL); |
| 778 | return cert->signed_cert_timestamp_list != NULL; |
| 779 | } |
| 780 | |
| 781 | int SSL_CTX_set_signed_cert_timestamp_list(SSL_CTX *ctx, const uint8_t *list, |
| 782 | size_t list_len) { |
| 783 | return set_signed_cert_timestamp_list(ctx->cert, list, list_len); |
| 784 | } |
| 785 | |
| 786 | int SSL_set_signed_cert_timestamp_list(SSL *ssl, const uint8_t *list, |
| 787 | size_t list_len) { |
| 788 | return set_signed_cert_timestamp_list(ssl->cert, list, list_len); |
| 789 | } |
| 790 | |
| 791 | int SSL_CTX_set_ocsp_response(SSL_CTX *ctx, const uint8_t *response, |
| 792 | size_t response_len) { |
| 793 | CRYPTO_BUFFER_free(ctx->cert->ocsp_response); |
| 794 | ctx->cert->ocsp_response = CRYPTO_BUFFER_new(response, response_len, NULL); |
| 795 | return ctx->cert->ocsp_response != NULL; |
| 796 | } |
| 797 | |
| 798 | int SSL_set_ocsp_response(SSL *ssl, const uint8_t *response, |
| 799 | size_t response_len) { |
| 800 | CRYPTO_BUFFER_free(ssl->cert->ocsp_response); |
| 801 | ssl->cert->ocsp_response = CRYPTO_BUFFER_new(response, response_len, NULL); |
| 802 | return ssl->cert->ocsp_response != NULL; |
| 803 | } |