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 | |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 121 | #include <utility> |
| 122 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 123 | #include <openssl/bn.h> |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 124 | #include <openssl/bytestring.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 | |
Adam Langley | 0da323a | 2015-05-15 12:49:30 -0700 | [diff] [blame] | 131 | #include "../crypto/internal.h" |
David Benjamin | 2ee94aa | 2015-04-07 22:38:30 -0400 | [diff] [blame] | 132 | #include "internal.h" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 133 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 134 | |
Joshua Liebow-Feeser | 8c7c635 | 2018-08-26 18:53:36 -0700 | [diff] [blame] | 135 | BSSL_NAMESPACE_BEGIN |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 136 | |
David Benjamin | e325c3f | 2018-04-12 16:11:15 -0400 | [diff] [blame] | 137 | CERT::CERT(const SSL_X509_METHOD *x509_method_arg) |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 138 | : x509_method(x509_method_arg) {} |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 139 | |
David Benjamin | e325c3f | 2018-04-12 16:11:15 -0400 | [diff] [blame] | 140 | CERT::~CERT() { |
| 141 | ssl_cert_clear_certs(this); |
| 142 | x509_method->cert_free(this); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 143 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 144 | |
Adam Langley | 3a2b47a | 2017-01-24 13:59:42 -0800 | [diff] [blame] | 145 | static CRYPTO_BUFFER *buffer_up_ref(CRYPTO_BUFFER *buffer) { |
| 146 | CRYPTO_BUFFER_up_ref(buffer); |
| 147 | return buffer; |
| 148 | } |
| 149 | |
David Benjamin | e325c3f | 2018-04-12 16:11:15 -0400 | [diff] [blame] | 150 | UniquePtr<CERT> ssl_cert_dup(CERT *cert) { |
| 151 | UniquePtr<CERT> ret = MakeUnique<CERT>(cert->x509_method); |
| 152 | if (!ret) { |
| 153 | return nullptr; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 154 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 155 | |
David Benjamin | e325c3f | 2018-04-12 16:11:15 -0400 | [diff] [blame] | 156 | if (cert->chain) { |
| 157 | ret->chain.reset(sk_CRYPTO_BUFFER_deep_copy( |
| 158 | cert->chain.get(), buffer_up_ref, CRYPTO_BUFFER_free)); |
| 159 | if (!ret->chain) { |
| 160 | return nullptr; |
| 161 | } |
| 162 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 163 | |
David Benjamin | 2908dd1 | 2018-06-29 17:46:42 -0400 | [diff] [blame] | 164 | ret->privatekey = UpRef(cert->privatekey); |
Tom Thorogood | 66b2fe8 | 2016-03-06 20:08:38 +1030 | [diff] [blame] | 165 | ret->key_method = cert->key_method; |
| 166 | |
David Benjamin | e325c3f | 2018-04-12 16:11:15 -0400 | [diff] [blame] | 167 | if (!ret->sigalgs.CopyFrom(cert->sigalgs)) { |
| 168 | return nullptr; |
David Benjamin | 0fc37ef | 2016-08-17 15:29:46 -0400 | [diff] [blame] | 169 | } |
David Benjamin | 0fc37ef | 2016-08-17 15:29:46 -0400 | [diff] [blame] | 170 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 171 | ret->cert_cb = cert->cert_cb; |
| 172 | ret->cert_cb_arg = cert->cert_cb_arg; |
| 173 | |
David Benjamin | e325c3f | 2018-04-12 16:11:15 -0400 | [diff] [blame] | 174 | ret->x509_method->cert_dup(ret.get(), cert); |
Adam Langley | d323f4b | 2016-03-01 15:58:14 -0800 | [diff] [blame] | 175 | |
David Benjamin | 2908dd1 | 2018-06-29 17:46:42 -0400 | [diff] [blame] | 176 | ret->signed_cert_timestamp_list = UpRef(cert->signed_cert_timestamp_list); |
| 177 | ret->ocsp_response = UpRef(cert->ocsp_response); |
David Benjamin | 83a3212 | 2017-02-14 18:34:54 -0500 | [diff] [blame] | 178 | |
David Benjamin | 5960a90 | 2017-02-14 20:07:11 -0500 | [diff] [blame] | 179 | ret->sid_ctx_length = cert->sid_ctx_length; |
| 180 | OPENSSL_memcpy(ret->sid_ctx, cert->sid_ctx, sizeof(ret->sid_ctx)); |
| 181 | |
Christopher Patton | 6c1b376 | 2018-07-17 12:49:41 -0700 | [diff] [blame] | 182 | if (cert->dc) { |
| 183 | ret->dc = cert->dc->Dup(); |
| 184 | if (!ret->dc) { |
| 185 | return nullptr; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | ret->dc_privatekey = UpRef(cert->dc_privatekey); |
| 190 | ret->dc_key_method = cert->dc_key_method; |
| 191 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 192 | return ret; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 193 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 194 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 195 | // Free up and clear all certificates and chains |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 196 | void ssl_cert_clear_certs(CERT *cert) { |
| 197 | if (cert == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 198 | return; |
| 199 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 200 | |
Adam Langley | 3509dac | 2017-02-01 11:59:18 -0800 | [diff] [blame] | 201 | cert->x509_method->cert_clear(cert); |
Adam Langley | e1e7813 | 2017-01-31 15:24:31 -0800 | [diff] [blame] | 202 | |
David Benjamin | e325c3f | 2018-04-12 16:11:15 -0400 | [diff] [blame] | 203 | cert->chain.reset(); |
| 204 | cert->privatekey.reset(); |
| 205 | cert->key_method = nullptr; |
Christopher Patton | 6c1b376 | 2018-07-17 12:49:41 -0700 | [diff] [blame] | 206 | |
| 207 | cert->dc.reset(); |
| 208 | cert->dc_privatekey.reset(); |
| 209 | cert->dc_key_method = nullptr; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 210 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 211 | |
David Benjamin | 27e377e | 2017-07-31 19:09:42 -0400 | [diff] [blame] | 212 | static void ssl_cert_set_cert_cb(CERT *cert, int (*cb)(SSL *ssl, void *arg), |
David Benjamin | a2bda9f | 2016-12-03 23:29:05 -0500 | [diff] [blame] | 213 | void *arg) { |
David Benjamin | 27e377e | 2017-07-31 19:09:42 -0400 | [diff] [blame] | 214 | cert->cert_cb = cb; |
| 215 | cert->cert_cb_arg = arg; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 216 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 217 | |
Adam Langley | d04ca95 | 2017-02-28 11:26:51 -0800 | [diff] [blame] | 218 | enum leaf_cert_and_privkey_result_t { |
| 219 | leaf_cert_and_privkey_error, |
| 220 | leaf_cert_and_privkey_ok, |
| 221 | leaf_cert_and_privkey_mismatch, |
| 222 | }; |
| 223 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 224 | // check_leaf_cert_and_privkey checks whether the certificate in |leaf_buffer| |
| 225 | // and the private key in |privkey| are suitable and coherent. It returns |
| 226 | // |leaf_cert_and_privkey_error| and pushes to the error queue if a problem is |
| 227 | // found. If the certificate and private key are valid, but incoherent, it |
| 228 | // returns |leaf_cert_and_privkey_mismatch|. Otherwise it returns |
| 229 | // |leaf_cert_and_privkey_ok|. |
Adam Langley | d04ca95 | 2017-02-28 11:26:51 -0800 | [diff] [blame] | 230 | static enum leaf_cert_and_privkey_result_t check_leaf_cert_and_privkey( |
| 231 | CRYPTO_BUFFER *leaf_buffer, EVP_PKEY *privkey) { |
Adam Langley | 52940c4 | 2017-02-01 12:40:31 -0800 | [diff] [blame] | 232 | CBS cert_cbs; |
Adam Langley | d04ca95 | 2017-02-28 11:26:51 -0800 | [diff] [blame] | 233 | CRYPTO_BUFFER_init_CBS(leaf_buffer, &cert_cbs); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 234 | UniquePtr<EVP_PKEY> pubkey = ssl_cert_parse_pubkey(&cert_cbs); |
| 235 | if (!pubkey) { |
Adam Langley | d04ca95 | 2017-02-28 11:26:51 -0800 | [diff] [blame] | 236 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 237 | return leaf_cert_and_privkey_error; |
Adam Langley | 52940c4 | 2017-02-01 12:40:31 -0800 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | if (!ssl_is_key_type_supported(pubkey->type)) { |
| 241 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 242 | return leaf_cert_and_privkey_error; |
Adam Langley | 52940c4 | 2017-02-01 12:40:31 -0800 | [diff] [blame] | 243 | } |
| 244 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 245 | // An ECC certificate may be usable for ECDH or ECDSA. We only support ECDSA |
| 246 | // certificates, so sanity-check the key usage extension. |
Adam Langley | 52940c4 | 2017-02-01 12:40:31 -0800 | [diff] [blame] | 247 | if (pubkey->type == EVP_PKEY_EC && |
Jesse Selover | d7266ec | 2019-01-30 16:06:10 -0500 | [diff] [blame] | 248 | !ssl_cert_check_key_usage(&cert_cbs, key_usage_digital_signature)) { |
Adam Langley | 52940c4 | 2017-02-01 12:40:31 -0800 | [diff] [blame] | 249 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 250 | return leaf_cert_and_privkey_error; |
Adam Langley | d04ca95 | 2017-02-28 11:26:51 -0800 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | if (privkey != NULL && |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 254 | // Sanity-check that the private key and the certificate match. |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 255 | !ssl_compare_public_and_private_key(pubkey.get(), privkey)) { |
Adam Langley | d04ca95 | 2017-02-28 11:26:51 -0800 | [diff] [blame] | 256 | ERR_clear_error(); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 257 | return leaf_cert_and_privkey_mismatch; |
Adam Langley | d04ca95 | 2017-02-28 11:26:51 -0800 | [diff] [blame] | 258 | } |
| 259 | |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 260 | return leaf_cert_and_privkey_ok; |
Adam Langley | d04ca95 | 2017-02-28 11:26:51 -0800 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | static int cert_set_chain_and_key( |
| 264 | CERT *cert, CRYPTO_BUFFER *const *certs, size_t num_certs, |
| 265 | EVP_PKEY *privkey, const SSL_PRIVATE_KEY_METHOD *privkey_method) { |
| 266 | if (num_certs == 0 || |
| 267 | (privkey == NULL && privkey_method == NULL)) { |
| 268 | OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER); |
Adam Langley | 52940c4 | 2017-02-01 12:40:31 -0800 | [diff] [blame] | 269 | return 0; |
| 270 | } |
| 271 | |
Adam Langley | d04ca95 | 2017-02-28 11:26:51 -0800 | [diff] [blame] | 272 | if (privkey != NULL && privkey_method != NULL) { |
| 273 | OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_HAVE_BOTH_PRIVKEY_AND_METHOD); |
| 274 | return 0; |
Adam Langley | 52940c4 | 2017-02-01 12:40:31 -0800 | [diff] [blame] | 275 | } |
| 276 | |
Adam Langley | d04ca95 | 2017-02-28 11:26:51 -0800 | [diff] [blame] | 277 | switch (check_leaf_cert_and_privkey(certs[0], privkey)) { |
| 278 | case leaf_cert_and_privkey_error: |
| 279 | return 0; |
| 280 | case leaf_cert_and_privkey_mismatch: |
| 281 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_AND_PRIVATE_KEY_MISMATCH); |
| 282 | return 0; |
| 283 | case leaf_cert_and_privkey_ok: |
| 284 | break; |
| 285 | } |
| 286 | |
David Benjamin | e325c3f | 2018-04-12 16:11:15 -0400 | [diff] [blame] | 287 | UniquePtr<STACK_OF(CRYPTO_BUFFER)> certs_sk(sk_CRYPTO_BUFFER_new_null()); |
| 288 | if (!certs_sk) { |
Adam Langley | d04ca95 | 2017-02-28 11:26:51 -0800 | [diff] [blame] | 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | for (size_t i = 0; i < num_certs; i++) { |
David Benjamin | 2908dd1 | 2018-06-29 17:46:42 -0400 | [diff] [blame] | 293 | if (!PushToStack(certs_sk.get(), UpRef(certs[i]))) { |
Adam Langley | d04ca95 | 2017-02-28 11:26:51 -0800 | [diff] [blame] | 294 | return 0; |
| 295 | } |
Adam Langley | d04ca95 | 2017-02-28 11:26:51 -0800 | [diff] [blame] | 296 | } |
| 297 | |
David Benjamin | 2908dd1 | 2018-06-29 17:46:42 -0400 | [diff] [blame] | 298 | cert->privatekey = UpRef(privkey); |
Adam Langley | d04ca95 | 2017-02-28 11:26:51 -0800 | [diff] [blame] | 299 | cert->key_method = privkey_method; |
| 300 | |
David Benjamin | e325c3f | 2018-04-12 16:11:15 -0400 | [diff] [blame] | 301 | cert->chain = std::move(certs_sk); |
Adam Langley | d04ca95 | 2017-02-28 11:26:51 -0800 | [diff] [blame] | 302 | return 1; |
| 303 | } |
| 304 | |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 305 | bool ssl_set_cert(CERT *cert, UniquePtr<CRYPTO_BUFFER> buffer) { |
David Benjamin | e325c3f | 2018-04-12 16:11:15 -0400 | [diff] [blame] | 306 | switch (check_leaf_cert_and_privkey(buffer.get(), cert->privatekey.get())) { |
Adam Langley | d04ca95 | 2017-02-28 11:26:51 -0800 | [diff] [blame] | 307 | case leaf_cert_and_privkey_error: |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 308 | return false; |
Adam Langley | d04ca95 | 2017-02-28 11:26:51 -0800 | [diff] [blame] | 309 | case leaf_cert_and_privkey_mismatch: |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 310 | // don't fail for a cert/key mismatch, just free current private key |
| 311 | // (when switching to a different cert & key, first this function should |
| 312 | // be used, then |ssl_set_pkey|. |
David Benjamin | e325c3f | 2018-04-12 16:11:15 -0400 | [diff] [blame] | 313 | cert->privatekey.reset(); |
Adam Langley | d04ca95 | 2017-02-28 11:26:51 -0800 | [diff] [blame] | 314 | break; |
| 315 | case leaf_cert_and_privkey_ok: |
| 316 | break; |
| 317 | } |
Adam Langley | 52940c4 | 2017-02-01 12:40:31 -0800 | [diff] [blame] | 318 | |
| 319 | cert->x509_method->cert_flush_cached_leaf(cert); |
| 320 | |
David Benjamin | e325c3f | 2018-04-12 16:11:15 -0400 | [diff] [blame] | 321 | if (cert->chain != nullptr) { |
| 322 | CRYPTO_BUFFER_free(sk_CRYPTO_BUFFER_value(cert->chain.get(), 0)); |
| 323 | sk_CRYPTO_BUFFER_set(cert->chain.get(), 0, buffer.release()); |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 324 | return true; |
Adam Langley | 52940c4 | 2017-02-01 12:40:31 -0800 | [diff] [blame] | 325 | } |
| 326 | |
David Benjamin | e325c3f | 2018-04-12 16:11:15 -0400 | [diff] [blame] | 327 | cert->chain.reset(sk_CRYPTO_BUFFER_new_null()); |
| 328 | if (cert->chain == nullptr) { |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 329 | return false; |
Adam Langley | 52940c4 | 2017-02-01 12:40:31 -0800 | [diff] [blame] | 330 | } |
| 331 | |
David Benjamin | e325c3f | 2018-04-12 16:11:15 -0400 | [diff] [blame] | 332 | if (!PushToStack(cert->chain.get(), std::move(buffer))) { |
| 333 | cert->chain.reset(); |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 334 | return false; |
Adam Langley | 52940c4 | 2017-02-01 12:40:31 -0800 | [diff] [blame] | 335 | } |
Adam Langley | 52940c4 | 2017-02-01 12:40:31 -0800 | [diff] [blame] | 336 | |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 337 | return true; |
Adam Langley | 52940c4 | 2017-02-01 12:40:31 -0800 | [diff] [blame] | 338 | } |
| 339 | |
Christopher Patton | 9cde848 | 2018-07-17 11:36:36 -0700 | [diff] [blame] | 340 | bool ssl_has_certificate(const SSL_HANDSHAKE *hs) { |
| 341 | return hs->config->cert->chain != nullptr && |
| 342 | sk_CRYPTO_BUFFER_value(hs->config->cert->chain.get(), 0) != nullptr && |
| 343 | ssl_has_private_key(hs); |
David Benjamin | 32a66d5 | 2016-07-13 22:03:11 -0400 | [diff] [blame] | 344 | } |
| 345 | |
David Benjamin | e664a53 | 2017-07-20 20:19:36 -0400 | [diff] [blame] | 346 | bool ssl_parse_cert_chain(uint8_t *out_alert, |
| 347 | UniquePtr<STACK_OF(CRYPTO_BUFFER)> *out_chain, |
| 348 | UniquePtr<EVP_PKEY> *out_pubkey, |
| 349 | uint8_t *out_leaf_sha256, CBS *cbs, |
| 350 | CRYPTO_BUFFER_POOL *pool) { |
| 351 | out_chain->reset(); |
| 352 | out_pubkey->reset(); |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 353 | |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 354 | CBS certificate_list; |
| 355 | if (!CBS_get_u24_length_prefixed(cbs, &certificate_list)) { |
| 356 | *out_alert = SSL_AD_DECODE_ERROR; |
| 357 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | e664a53 | 2017-07-20 20:19:36 -0400 | [diff] [blame] | 358 | return false; |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 359 | } |
| 360 | |
David Benjamin | e664a53 | 2017-07-20 20:19:36 -0400 | [diff] [blame] | 361 | if (CBS_len(&certificate_list) == 0) { |
| 362 | return true; |
| 363 | } |
| 364 | |
| 365 | UniquePtr<STACK_OF(CRYPTO_BUFFER)> chain(sk_CRYPTO_BUFFER_new_null()); |
| 366 | if (!chain) { |
| 367 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 368 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 369 | return false; |
| 370 | } |
| 371 | |
| 372 | UniquePtr<EVP_PKEY> pubkey; |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 373 | while (CBS_len(&certificate_list) > 0) { |
| 374 | CBS certificate; |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 375 | if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate) || |
| 376 | CBS_len(&certificate) == 0) { |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 377 | *out_alert = SSL_AD_DECODE_ERROR; |
| 378 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH); |
David Benjamin | e664a53 | 2017-07-20 20:19:36 -0400 | [diff] [blame] | 379 | return false; |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 380 | } |
| 381 | |
David Benjamin | e664a53 | 2017-07-20 20:19:36 -0400 | [diff] [blame] | 382 | if (sk_CRYPTO_BUFFER_num(chain.get()) == 0) { |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 383 | pubkey = ssl_cert_parse_pubkey(&certificate); |
| 384 | if (!pubkey) { |
Adam Langley | 7dccc71 | 2017-02-08 08:51:25 -0800 | [diff] [blame] | 385 | *out_alert = SSL_AD_DECODE_ERROR; |
David Benjamin | e664a53 | 2017-07-20 20:19:36 -0400 | [diff] [blame] | 386 | return false; |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 387 | } |
| 388 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 389 | // Retain the hash of the leaf certificate if requested. |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 390 | if (out_leaf_sha256 != NULL) { |
| 391 | SHA256(CBS_data(&certificate), CBS_len(&certificate), out_leaf_sha256); |
| 392 | } |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 393 | } |
| 394 | |
David Benjamin | ee910bf | 2017-07-25 22:36:00 -0400 | [diff] [blame] | 395 | UniquePtr<CRYPTO_BUFFER> buf( |
| 396 | CRYPTO_BUFFER_new_from_CBS(&certificate, pool)); |
| 397 | if (!buf || |
| 398 | !PushToStack(chain.get(), std::move(buf))) { |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 399 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 400 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | e664a53 | 2017-07-20 20:19:36 -0400 | [diff] [blame] | 401 | return false; |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 402 | } |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 403 | } |
| 404 | |
David Benjamin | e664a53 | 2017-07-20 20:19:36 -0400 | [diff] [blame] | 405 | *out_chain = std::move(chain); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 406 | *out_pubkey = std::move(pubkey); |
David Benjamin | e664a53 | 2017-07-20 20:19:36 -0400 | [diff] [blame] | 407 | return true; |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 408 | } |
| 409 | |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 410 | bool ssl_add_cert_chain(SSL_HANDSHAKE *hs, CBB *cbb) { |
Christopher Patton | 9cde848 | 2018-07-17 11:36:36 -0700 | [diff] [blame] | 411 | if (!ssl_has_certificate(hs)) { |
David Benjamin | 32a66d5 | 2016-07-13 22:03:11 -0400 | [diff] [blame] | 412 | return CBB_add_u24(cbb, 0); |
| 413 | } |
| 414 | |
Adam Langley | 3a2b47a | 2017-01-24 13:59:42 -0800 | [diff] [blame] | 415 | CBB certs; |
| 416 | if (!CBB_add_u24_length_prefixed(cbb, &certs)) { |
David Benjamin | e64d2c7 | 2017-07-12 16:31:08 -0400 | [diff] [blame] | 417 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 418 | return false; |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 419 | } |
| 420 | |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 421 | STACK_OF(CRYPTO_BUFFER) *chain = hs->config->cert->chain.get(); |
Adam Langley | 3a2b47a | 2017-01-24 13:59:42 -0800 | [diff] [blame] | 422 | for (size_t i = 0; i < sk_CRYPTO_BUFFER_num(chain); i++) { |
| 423 | CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(chain, i); |
| 424 | CBB child; |
| 425 | if (!CBB_add_u24_length_prefixed(&certs, &child) || |
| 426 | !CBB_add_bytes(&child, CRYPTO_BUFFER_data(buffer), |
| 427 | CRYPTO_BUFFER_len(buffer)) || |
| 428 | !CBB_flush(&certs)) { |
David Benjamin | e64d2c7 | 2017-07-12 16:31:08 -0400 | [diff] [blame] | 429 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 430 | return false; |
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 | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 435 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 436 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 437 | // ssl_cert_skip_to_spki parses a DER-encoded, X.509 certificate from |in| and |
| 438 | // positions |*out_tbs_cert| to cover the TBSCertificate, starting at the |
| 439 | // subjectPublicKeyInfo. |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 440 | static bool ssl_cert_skip_to_spki(const CBS *in, CBS *out_tbs_cert) { |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 441 | /* From RFC 5280, section 4.1 |
| 442 | * Certificate ::= SEQUENCE { |
| 443 | * tbsCertificate TBSCertificate, |
| 444 | * signatureAlgorithm AlgorithmIdentifier, |
| 445 | * signatureValue BIT STRING } |
| 446 | |
| 447 | * TBSCertificate ::= SEQUENCE { |
| 448 | * version [0] EXPLICIT Version DEFAULT v1, |
| 449 | * serialNumber CertificateSerialNumber, |
| 450 | * signature AlgorithmIdentifier, |
| 451 | * issuer Name, |
| 452 | * validity Validity, |
| 453 | * subject Name, |
| 454 | * subjectPublicKeyInfo SubjectPublicKeyInfo, |
| 455 | * ... } */ |
| 456 | CBS buf = *in; |
| 457 | |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 458 | CBS toplevel; |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 459 | if (!CBS_get_asn1(&buf, &toplevel, CBS_ASN1_SEQUENCE) || |
| 460 | CBS_len(&buf) != 0 || |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 461 | !CBS_get_asn1(&toplevel, out_tbs_cert, CBS_ASN1_SEQUENCE) || |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 462 | // version |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 463 | !CBS_get_optional_asn1( |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 464 | out_tbs_cert, NULL, NULL, |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 465 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 0) || |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 466 | // serialNumber |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 467 | !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_INTEGER) || |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 468 | // signature algorithm |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 469 | !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) || |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 470 | // issuer |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 471 | !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) || |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 472 | // validity |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 473 | !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) || |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 474 | // subject |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 475 | !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE)) { |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 476 | return false; |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 477 | } |
| 478 | |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 479 | return true; |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 480 | } |
| 481 | |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 482 | UniquePtr<EVP_PKEY> ssl_cert_parse_pubkey(const CBS *in) { |
David Benjamin | 5b410b6 | 2017-01-03 08:08:57 -0500 | [diff] [blame] | 483 | CBS buf = *in, tbs_cert; |
| 484 | if (!ssl_cert_skip_to_spki(&buf, &tbs_cert)) { |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 485 | OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 486 | return nullptr; |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 487 | } |
| 488 | |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 489 | return UniquePtr<EVP_PKEY>(EVP_parse_public_key(&tbs_cert)); |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 490 | } |
| 491 | |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 492 | bool ssl_compare_public_and_private_key(const EVP_PKEY *pubkey, |
| 493 | const EVP_PKEY *privkey) { |
Adam Langley | d04ca95 | 2017-02-28 11:26:51 -0800 | [diff] [blame] | 494 | if (EVP_PKEY_is_opaque(privkey)) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 495 | // We cannot check an opaque private key and have to trust that it |
| 496 | // matches. |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 497 | return true; |
Adam Langley | d04ca95 | 2017-02-28 11:26:51 -0800 | [diff] [blame] | 498 | } |
| 499 | |
Adam Langley | 3a2b47a | 2017-01-24 13:59:42 -0800 | [diff] [blame] | 500 | switch (EVP_PKEY_cmp(pubkey, privkey)) { |
| 501 | case 1: |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 502 | return true; |
Adam Langley | 3a2b47a | 2017-01-24 13:59:42 -0800 | [diff] [blame] | 503 | case 0: |
| 504 | OPENSSL_PUT_ERROR(X509, X509_R_KEY_VALUES_MISMATCH); |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 505 | return false; |
Adam Langley | 3a2b47a | 2017-01-24 13:59:42 -0800 | [diff] [blame] | 506 | case -1: |
| 507 | OPENSSL_PUT_ERROR(X509, X509_R_KEY_TYPE_MISMATCH); |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 508 | return false; |
Adam Langley | 3a2b47a | 2017-01-24 13:59:42 -0800 | [diff] [blame] | 509 | case -2: |
| 510 | OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_KEY_TYPE); |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 511 | return false; |
Adam Langley | 3a2b47a | 2017-01-24 13:59:42 -0800 | [diff] [blame] | 512 | } |
| 513 | |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 514 | assert(0); |
| 515 | return false; |
Adam Langley | 3a2b47a | 2017-01-24 13:59:42 -0800 | [diff] [blame] | 516 | } |
| 517 | |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 518 | bool ssl_cert_check_private_key(const CERT *cert, const EVP_PKEY *privkey) { |
David Benjamin | e325c3f | 2018-04-12 16:11:15 -0400 | [diff] [blame] | 519 | if (privkey == nullptr) { |
Adam Langley | 3a2b47a | 2017-01-24 13:59:42 -0800 | [diff] [blame] | 520 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED); |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 521 | return false; |
Adam Langley | 3a2b47a | 2017-01-24 13:59:42 -0800 | [diff] [blame] | 522 | } |
| 523 | |
David Benjamin | e325c3f | 2018-04-12 16:11:15 -0400 | [diff] [blame] | 524 | if (cert->chain == nullptr || |
| 525 | sk_CRYPTO_BUFFER_value(cert->chain.get(), 0) == nullptr) { |
Adam Langley | 3a2b47a | 2017-01-24 13:59:42 -0800 | [diff] [blame] | 526 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED); |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 527 | return false; |
Adam Langley | 3a2b47a | 2017-01-24 13:59:42 -0800 | [diff] [blame] | 528 | } |
| 529 | |
Adam Langley | c26692c | 2017-01-25 09:34:42 -0800 | [diff] [blame] | 530 | CBS cert_cbs; |
David Benjamin | e325c3f | 2018-04-12 16:11:15 -0400 | [diff] [blame] | 531 | CRYPTO_BUFFER_init_CBS(sk_CRYPTO_BUFFER_value(cert->chain.get(), 0), |
| 532 | &cert_cbs); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 533 | UniquePtr<EVP_PKEY> pubkey = ssl_cert_parse_pubkey(&cert_cbs); |
Adam Langley | c26692c | 2017-01-25 09:34:42 -0800 | [diff] [blame] | 534 | if (!pubkey) { |
| 535 | OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_KEY_TYPE); |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 536 | return false; |
Adam Langley | c26692c | 2017-01-25 09:34:42 -0800 | [diff] [blame] | 537 | } |
| 538 | |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 539 | return ssl_compare_public_and_private_key(pubkey.get(), privkey); |
Adam Langley | 3a2b47a | 2017-01-24 13:59:42 -0800 | [diff] [blame] | 540 | } |
| 541 | |
Jesse Selover | d7266ec | 2019-01-30 16:06:10 -0500 | [diff] [blame] | 542 | bool ssl_cert_check_key_usage(const CBS *in, enum ssl_key_usage_t bit) { |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 543 | CBS buf = *in; |
| 544 | |
| 545 | CBS tbs_cert, outer_extensions; |
| 546 | int has_extensions; |
| 547 | if (!ssl_cert_skip_to_spki(&buf, &tbs_cert) || |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 548 | // subjectPublicKeyInfo |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 549 | !CBS_get_asn1(&tbs_cert, NULL, CBS_ASN1_SEQUENCE) || |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 550 | // issuerUniqueID |
David Benjamin | 2f3958a | 2021-04-16 11:55:23 -0400 | [diff] [blame] | 551 | !CBS_get_optional_asn1(&tbs_cert, NULL, NULL, |
| 552 | CBS_ASN1_CONTEXT_SPECIFIC | 1) || |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 553 | // subjectUniqueID |
David Benjamin | 2f3958a | 2021-04-16 11:55:23 -0400 | [diff] [blame] | 554 | !CBS_get_optional_asn1(&tbs_cert, NULL, NULL, |
| 555 | CBS_ASN1_CONTEXT_SPECIFIC | 2) || |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 556 | !CBS_get_optional_asn1( |
| 557 | &tbs_cert, &outer_extensions, &has_extensions, |
| 558 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 3)) { |
David Benjamin | c64d123 | 2017-10-04 18:14:28 -0400 | [diff] [blame] | 559 | OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT); |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 560 | return false; |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | if (!has_extensions) { |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 564 | return true; |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | CBS extensions; |
| 568 | if (!CBS_get_asn1(&outer_extensions, &extensions, CBS_ASN1_SEQUENCE)) { |
David Benjamin | c64d123 | 2017-10-04 18:14:28 -0400 | [diff] [blame] | 569 | OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT); |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 570 | return false; |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | while (CBS_len(&extensions) > 0) { |
| 574 | CBS extension, oid, contents; |
| 575 | if (!CBS_get_asn1(&extensions, &extension, CBS_ASN1_SEQUENCE) || |
| 576 | !CBS_get_asn1(&extension, &oid, CBS_ASN1_OBJECT) || |
| 577 | (CBS_peek_asn1_tag(&extension, CBS_ASN1_BOOLEAN) && |
| 578 | !CBS_get_asn1(&extension, NULL, CBS_ASN1_BOOLEAN)) || |
| 579 | !CBS_get_asn1(&extension, &contents, CBS_ASN1_OCTETSTRING) || |
| 580 | CBS_len(&extension) != 0) { |
David Benjamin | c64d123 | 2017-10-04 18:14:28 -0400 | [diff] [blame] | 581 | OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT); |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 582 | return false; |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | static const uint8_t kKeyUsageOID[3] = {0x55, 0x1d, 0x0f}; |
| 586 | if (CBS_len(&oid) != sizeof(kKeyUsageOID) || |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 587 | OPENSSL_memcmp(CBS_data(&oid), kKeyUsageOID, sizeof(kKeyUsageOID)) != |
| 588 | 0) { |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 589 | continue; |
| 590 | } |
| 591 | |
| 592 | CBS bit_string; |
| 593 | if (!CBS_get_asn1(&contents, &bit_string, CBS_ASN1_BITSTRING) || |
| 594 | CBS_len(&contents) != 0) { |
David Benjamin | c64d123 | 2017-10-04 18:14:28 -0400 | [diff] [blame] | 595 | OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT); |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 596 | return false; |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 597 | } |
| 598 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 599 | // This is the KeyUsage extension. See |
| 600 | // https://tools.ietf.org/html/rfc5280#section-4.2.1.3 |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 601 | if (!CBS_is_valid_asn1_bitstring(&bit_string)) { |
David Benjamin | c64d123 | 2017-10-04 18:14:28 -0400 | [diff] [blame] | 602 | OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT); |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 603 | return false; |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 604 | } |
| 605 | |
Jesse Selover | d7266ec | 2019-01-30 16:06:10 -0500 | [diff] [blame] | 606 | if (!CBS_asn1_bitstring_has_bit(&bit_string, bit)) { |
| 607 | OPENSSL_PUT_ERROR(SSL, SSL_R_KEY_USAGE_BIT_INCORRECT); |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 608 | return false; |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 609 | } |
| 610 | |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 611 | return true; |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 612 | } |
| 613 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 614 | // No KeyUsage extension found. |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 615 | return true; |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 616 | } |
| 617 | |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 618 | UniquePtr<STACK_OF(CRYPTO_BUFFER)> ssl_parse_client_CA_list(SSL *ssl, |
| 619 | uint8_t *out_alert, |
| 620 | CBS *cbs) { |
Adam Langley | 34b4c82 | 2017-02-02 10:57:17 -0800 | [diff] [blame] | 621 | CRYPTO_BUFFER_POOL *const pool = ssl->ctx->pool; |
| 622 | |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 623 | UniquePtr<STACK_OF(CRYPTO_BUFFER)> ret(sk_CRYPTO_BUFFER_new_null()); |
| 624 | if (!ret) { |
David Benjamin | e0332e8 | 2016-07-13 22:40:36 -0400 | [diff] [blame] | 625 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 626 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 627 | return nullptr; |
David Benjamin | e0332e8 | 2016-07-13 22:40:36 -0400 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | CBS child; |
| 631 | if (!CBS_get_u16_length_prefixed(cbs, &child)) { |
| 632 | *out_alert = SSL_AD_DECODE_ERROR; |
| 633 | OPENSSL_PUT_ERROR(SSL, SSL_R_LENGTH_MISMATCH); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 634 | return nullptr; |
David Benjamin | e0332e8 | 2016-07-13 22:40:36 -0400 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | while (CBS_len(&child) > 0) { |
| 638 | CBS distinguished_name; |
| 639 | if (!CBS_get_u16_length_prefixed(&child, &distinguished_name)) { |
| 640 | *out_alert = SSL_AD_DECODE_ERROR; |
| 641 | OPENSSL_PUT_ERROR(SSL, SSL_R_CA_DN_TOO_LONG); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 642 | return nullptr; |
David Benjamin | e0332e8 | 2016-07-13 22:40:36 -0400 | [diff] [blame] | 643 | } |
| 644 | |
David Benjamin | ee910bf | 2017-07-25 22:36:00 -0400 | [diff] [blame] | 645 | UniquePtr<CRYPTO_BUFFER> buffer( |
| 646 | CRYPTO_BUFFER_new_from_CBS(&distinguished_name, pool)); |
| 647 | if (!buffer || |
| 648 | !PushToStack(ret.get(), std::move(buffer))) { |
David Benjamin | e0332e8 | 2016-07-13 22:40:36 -0400 | [diff] [blame] | 649 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 650 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 651 | return nullptr; |
David Benjamin | e0332e8 | 2016-07-13 22:40:36 -0400 | [diff] [blame] | 652 | } |
David Benjamin | e0332e8 | 2016-07-13 22:40:36 -0400 | [diff] [blame] | 653 | } |
| 654 | |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 655 | if (!ssl->ctx->x509_method->check_client_CA_list(ret.get())) { |
David Benjamin | 418cdc4 | 2018-05-09 13:56:09 -0400 | [diff] [blame] | 656 | *out_alert = SSL_AD_DECODE_ERROR; |
Adam Langley | 0bdef09 | 2017-02-23 15:02:58 -0800 | [diff] [blame] | 657 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 658 | return nullptr; |
Adam Langley | 0bdef09 | 2017-02-23 15:02:58 -0800 | [diff] [blame] | 659 | } |
| 660 | |
David Benjamin | e0332e8 | 2016-07-13 22:40:36 -0400 | [diff] [blame] | 661 | return ret; |
David Benjamin | e0332e8 | 2016-07-13 22:40:36 -0400 | [diff] [blame] | 662 | } |
| 663 | |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 664 | bool ssl_has_client_CAs(const SSL_CONFIG *cfg) { |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 665 | const STACK_OF(CRYPTO_BUFFER) *names = cfg->client_CA.get(); |
| 666 | if (names == nullptr) { |
| 667 | names = cfg->ssl->ctx->client_CA.get(); |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 668 | } |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 669 | if (names == nullptr) { |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 670 | return false; |
| 671 | } |
| 672 | return sk_CRYPTO_BUFFER_num(names) > 0; |
| 673 | } |
| 674 | |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 675 | bool ssl_add_client_CA_list(SSL_HANDSHAKE *hs, CBB *cbb) { |
David Benjamin | 32a66d5 | 2016-07-13 22:03:11 -0400 | [diff] [blame] | 676 | CBB child, name_cbb; |
| 677 | if (!CBB_add_u16_length_prefixed(cbb, &child)) { |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 678 | return false; |
David Benjamin | 32a66d5 | 2016-07-13 22:03:11 -0400 | [diff] [blame] | 679 | } |
| 680 | |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 681 | const STACK_OF(CRYPTO_BUFFER) *names = hs->config->client_CA.get(); |
Adam Langley | 34b4c82 | 2017-02-02 10:57:17 -0800 | [diff] [blame] | 682 | if (names == NULL) { |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 683 | names = hs->ssl->ctx->client_CA.get(); |
Adam Langley | 34b4c82 | 2017-02-02 10:57:17 -0800 | [diff] [blame] | 684 | } |
| 685 | if (names == NULL) { |
David Benjamin | 32a66d5 | 2016-07-13 22:03:11 -0400 | [diff] [blame] | 686 | return CBB_flush(cbb); |
| 687 | } |
| 688 | |
David Benjamin | ec78383 | 2017-07-25 23:23:03 -0400 | [diff] [blame] | 689 | for (const CRYPTO_BUFFER *name : names) { |
David Benjamin | 32a66d5 | 2016-07-13 22:03:11 -0400 | [diff] [blame] | 690 | if (!CBB_add_u16_length_prefixed(&child, &name_cbb) || |
Adam Langley | 34b4c82 | 2017-02-02 10:57:17 -0800 | [diff] [blame] | 691 | !CBB_add_bytes(&name_cbb, CRYPTO_BUFFER_data(name), |
| 692 | CRYPTO_BUFFER_len(name))) { |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 693 | return false; |
David Benjamin | 32a66d5 | 2016-07-13 22:03:11 -0400 | [diff] [blame] | 694 | } |
| 695 | } |
| 696 | |
| 697 | return CBB_flush(cbb); |
| 698 | } |
| 699 | |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 700 | bool ssl_check_leaf_certificate(SSL_HANDSHAKE *hs, EVP_PKEY *pkey, |
| 701 | const CRYPTO_BUFFER *leaf) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 702 | assert(ssl_protocol_version(hs->ssl) < TLS1_3_VERSION); |
David Benjamin | 938fa7c | 2016-10-07 00:27:05 -0400 | [diff] [blame] | 703 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 704 | // Check the certificate's type matches the cipher. |
David Benjamin | 6952211 | 2017-03-28 15:38:29 -0500 | [diff] [blame] | 705 | if (!(hs->new_cipher->algorithm_auth & ssl_cipher_auth_mask_for_key(pkey))) { |
Steven Valdez | bf5aa84 | 2016-07-15 07:07:40 -0400 | [diff] [blame] | 706 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CERTIFICATE_TYPE); |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 707 | return false; |
Steven Valdez | bf5aa84 | 2016-07-15 07:07:40 -0400 | [diff] [blame] | 708 | } |
| 709 | |
David Benjamin | 6952211 | 2017-03-28 15:38:29 -0500 | [diff] [blame] | 710 | if (EVP_PKEY_id(pkey) == EVP_PKEY_EC) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 711 | // Check the key's group and point format are acceptable. |
David Benjamin | 6952211 | 2017-03-28 15:38:29 -0500 | [diff] [blame] | 712 | EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey); |
David Benjamin | 938fa7c | 2016-10-07 00:27:05 -0400 | [diff] [blame] | 713 | uint16_t group_id; |
| 714 | if (!ssl_nid_to_group_id( |
| 715 | &group_id, EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key))) || |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 716 | !tls1_check_group_id(hs, group_id) || |
David Benjamin | 938fa7c | 2016-10-07 00:27:05 -0400 | [diff] [blame] | 717 | EC_KEY_get_conv_form(ec_key) != POINT_CONVERSION_UNCOMPRESSED) { |
Steven Valdez | bf5aa84 | 2016-07-15 07:07:40 -0400 | [diff] [blame] | 718 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECC_CERT); |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 719 | return false; |
Steven Valdez | bf5aa84 | 2016-07-15 07:07:40 -0400 | [diff] [blame] | 720 | } |
| 721 | } |
| 722 | |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 723 | return true; |
Steven Valdez | bf5aa84 | 2016-07-15 07:07:40 -0400 | [diff] [blame] | 724 | } |
David Benjamin | 5edfc8c | 2016-12-10 15:46:58 -0500 | [diff] [blame] | 725 | |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 726 | bool ssl_on_certificate_selected(SSL_HANDSHAKE *hs) { |
David Benjamin | a232a71 | 2017-03-30 15:51:53 -0500 | [diff] [blame] | 727 | SSL *const ssl = hs->ssl; |
Christopher Patton | 9cde848 | 2018-07-17 11:36:36 -0700 | [diff] [blame] | 728 | if (!ssl_has_certificate(hs)) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 729 | // Nothing to do. |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 730 | return true; |
David Benjamin | a232a71 | 2017-03-30 15:51:53 -0500 | [diff] [blame] | 731 | } |
| 732 | |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 733 | if (!ssl->ctx->x509_method->ssl_auto_chain_if_needed(hs)) { |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 734 | return false; |
David Benjamin | a232a71 | 2017-03-30 15:51:53 -0500 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | CBS leaf; |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 738 | CRYPTO_BUFFER_init_CBS( |
| 739 | sk_CRYPTO_BUFFER_value(hs->config->cert->chain.get(), 0), &leaf); |
David Benjamin | a232a71 | 2017-03-30 15:51:53 -0500 | [diff] [blame] | 740 | |
Christopher Patton | 6c1b376 | 2018-07-17 12:49:41 -0700 | [diff] [blame] | 741 | if (ssl_signing_with_dc(hs)) { |
| 742 | hs->local_pubkey = UpRef(hs->config->cert->dc->pkey); |
| 743 | } else { |
| 744 | hs->local_pubkey = ssl_cert_parse_pubkey(&leaf); |
| 745 | } |
David Benjamin | a232a71 | 2017-03-30 15:51:53 -0500 | [diff] [blame] | 746 | return hs->local_pubkey != NULL; |
| 747 | } |
| 748 | |
Christopher Patton | 6c1b376 | 2018-07-17 12:49:41 -0700 | [diff] [blame] | 749 | |
| 750 | // Delegated credentials. |
| 751 | |
| 752 | DC::DC() = default; |
| 753 | DC::~DC() = default; |
| 754 | |
| 755 | UniquePtr<DC> DC::Dup() { |
| 756 | bssl::UniquePtr<DC> ret = MakeUnique<DC>(); |
| 757 | if (!ret) { |
| 758 | return nullptr; |
| 759 | } |
| 760 | |
| 761 | ret->raw = UpRef(raw); |
| 762 | ret->expected_cert_verify_algorithm = expected_cert_verify_algorithm; |
Christopher Patton | 6c1b376 | 2018-07-17 12:49:41 -0700 | [diff] [blame] | 763 | ret->pkey = UpRef(pkey); |
| 764 | return ret; |
| 765 | } |
| 766 | |
| 767 | // static |
| 768 | UniquePtr<DC> DC::Parse(CRYPTO_BUFFER *in, uint8_t *out_alert) { |
| 769 | UniquePtr<DC> dc = MakeUnique<DC>(); |
| 770 | if (!dc) { |
| 771 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 772 | return nullptr; |
| 773 | } |
| 774 | |
| 775 | dc->raw = UpRef(in); |
| 776 | |
| 777 | CBS pubkey, deleg, sig; |
| 778 | uint32_t valid_time; |
| 779 | uint16_t algorithm; |
| 780 | CRYPTO_BUFFER_init_CBS(dc->raw.get(), &deleg); |
| 781 | if (!CBS_get_u32(&deleg, &valid_time) || |
| 782 | !CBS_get_u16(&deleg, &dc->expected_cert_verify_algorithm) || |
Christopher Patton | 6c1b376 | 2018-07-17 12:49:41 -0700 | [diff] [blame] | 783 | !CBS_get_u24_length_prefixed(&deleg, &pubkey) || |
| 784 | !CBS_get_u16(&deleg, &algorithm) || |
| 785 | !CBS_get_u16_length_prefixed(&deleg, &sig) || |
| 786 | CBS_len(&deleg) != 0) { |
| 787 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 788 | *out_alert = SSL_AD_DECODE_ERROR; |
| 789 | return nullptr; |
| 790 | } |
| 791 | |
| 792 | dc->pkey.reset(EVP_parse_public_key(&pubkey)); |
| 793 | if (dc->pkey == nullptr) { |
| 794 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 795 | *out_alert = SSL_AD_DECODE_ERROR; |
| 796 | return nullptr; |
| 797 | } |
| 798 | |
| 799 | return dc; |
| 800 | } |
| 801 | |
| 802 | // ssl_can_serve_dc returns true if the host has configured a DC that it can |
| 803 | // serve in the handshake. Specifically, it checks that a DC has been |
Watson Ladd | 3390fd8 | 2019-03-07 13:06:00 -0800 | [diff] [blame] | 804 | // configured and that the DC signature algorithm is supported by the peer. |
Christopher Patton | 6c1b376 | 2018-07-17 12:49:41 -0700 | [diff] [blame] | 805 | static bool ssl_can_serve_dc(const SSL_HANDSHAKE *hs) { |
| 806 | // Check that a DC has been configured. |
| 807 | const CERT *cert = hs->config->cert.get(); |
| 808 | if (cert->dc == nullptr || |
| 809 | cert->dc->raw == nullptr || |
| 810 | (cert->dc_privatekey == nullptr && cert->dc_key_method == nullptr)) { |
| 811 | return false; |
| 812 | } |
| 813 | |
Watson Ladd | 2f213f6 | 2019-02-12 16:59:54 -0800 | [diff] [blame] | 814 | // Check that 1.3 or higher has been negotiated. |
Christopher Patton | 6c1b376 | 2018-07-17 12:49:41 -0700 | [diff] [blame] | 815 | const DC *dc = cert->dc.get(); |
| 816 | assert(hs->ssl->s3->have_version); |
Watson Ladd | 2f213f6 | 2019-02-12 16:59:54 -0800 | [diff] [blame] | 817 | if (ssl_protocol_version(hs->ssl) < TLS1_3_VERSION) { |
Christopher Patton | 6c1b376 | 2018-07-17 12:49:41 -0700 | [diff] [blame] | 818 | return false; |
| 819 | } |
| 820 | |
| 821 | // Check that the DC signature algorithm is supported by the peer. |
Watson Ladd | dcd6e44 | 2020-08-10 15:12:45 -0400 | [diff] [blame] | 822 | Span<const uint16_t> peer_sigalgs = hs->peer_delegated_credential_sigalgs; |
Christopher Patton | 6c1b376 | 2018-07-17 12:49:41 -0700 | [diff] [blame] | 823 | for (uint16_t peer_sigalg : peer_sigalgs) { |
| 824 | if (dc->expected_cert_verify_algorithm == peer_sigalg) { |
Watson Ladd | dcd6e44 | 2020-08-10 15:12:45 -0400 | [diff] [blame] | 825 | return true; |
Christopher Patton | 6c1b376 | 2018-07-17 12:49:41 -0700 | [diff] [blame] | 826 | } |
| 827 | } |
Watson Ladd | dcd6e44 | 2020-08-10 15:12:45 -0400 | [diff] [blame] | 828 | return false; |
Christopher Patton | 6c1b376 | 2018-07-17 12:49:41 -0700 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | bool ssl_signing_with_dc(const SSL_HANDSHAKE *hs) { |
Watson Ladd | 2f213f6 | 2019-02-12 16:59:54 -0800 | [diff] [blame] | 832 | // As of draft-ietf-tls-subcert-03, only the server may use delegated |
Christopher Patton | 6c1b376 | 2018-07-17 12:49:41 -0700 | [diff] [blame] | 833 | // credentials to authenticate itself. |
| 834 | return hs->ssl->server && |
| 835 | hs->delegated_credential_requested && |
| 836 | ssl_can_serve_dc(hs); |
| 837 | } |
| 838 | |
| 839 | static int cert_set_dc(CERT *cert, CRYPTO_BUFFER *const raw, EVP_PKEY *privkey, |
| 840 | const SSL_PRIVATE_KEY_METHOD *key_method) { |
| 841 | if (privkey == nullptr && key_method == nullptr) { |
| 842 | OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER); |
| 843 | return 0; |
| 844 | } |
| 845 | |
| 846 | if (privkey != nullptr && key_method != nullptr) { |
| 847 | OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_HAVE_BOTH_PRIVKEY_AND_METHOD); |
| 848 | return 0; |
| 849 | } |
| 850 | |
| 851 | uint8_t alert; |
| 852 | UniquePtr<DC> dc = DC::Parse(raw, &alert); |
| 853 | if (dc == nullptr) { |
| 854 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_DELEGATED_CREDENTIAL); |
| 855 | return 0; |
| 856 | } |
| 857 | |
| 858 | if (privkey) { |
| 859 | // Check that the public and private keys match. |
| 860 | if (!ssl_compare_public_and_private_key(dc->pkey.get(), privkey)) { |
| 861 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_AND_PRIVATE_KEY_MISMATCH); |
| 862 | return 0; |
| 863 | } |
| 864 | } |
| 865 | |
| 866 | cert->dc = std::move(dc); |
| 867 | cert->dc_privatekey = UpRef(privkey); |
| 868 | cert->dc_key_method = key_method; |
| 869 | |
| 870 | return 1; |
| 871 | } |
| 872 | |
Joshua Liebow-Feeser | 8c7c635 | 2018-08-26 18:53:36 -0700 | [diff] [blame] | 873 | BSSL_NAMESPACE_END |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 874 | |
| 875 | using namespace bssl; |
| 876 | |
| 877 | int SSL_set_chain_and_key(SSL *ssl, CRYPTO_BUFFER *const *certs, |
| 878 | size_t num_certs, EVP_PKEY *privkey, |
| 879 | const SSL_PRIVATE_KEY_METHOD *privkey_method) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 880 | if (!ssl->config) { |
| 881 | return 0; |
| 882 | } |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 883 | return cert_set_chain_and_key(ssl->config->cert.get(), certs, num_certs, |
| 884 | privkey, privkey_method); |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | int SSL_CTX_set_chain_and_key(SSL_CTX *ctx, CRYPTO_BUFFER *const *certs, |
| 888 | size_t num_certs, EVP_PKEY *privkey, |
| 889 | const SSL_PRIVATE_KEY_METHOD *privkey_method) { |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 890 | return cert_set_chain_and_key(ctx->cert.get(), certs, num_certs, privkey, |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 891 | privkey_method); |
| 892 | } |
| 893 | |
Adam Langley | 964256d | 2020-03-19 11:57:12 -0700 | [diff] [blame] | 894 | const STACK_OF(CRYPTO_BUFFER)* SSL_CTX_get0_chain(const SSL_CTX *ctx) { |
| 895 | return ctx->cert->chain.get(); |
| 896 | } |
| 897 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 898 | int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, size_t der_len, |
| 899 | const uint8_t *der) { |
David Benjamin | ee910bf | 2017-07-25 22:36:00 -0400 | [diff] [blame] | 900 | UniquePtr<CRYPTO_BUFFER> buffer(CRYPTO_BUFFER_new(der, der_len, NULL)); |
| 901 | if (!buffer) { |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 902 | return 0; |
| 903 | } |
| 904 | |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 905 | return ssl_set_cert(ctx->cert.get(), std::move(buffer)); |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | int SSL_use_certificate_ASN1(SSL *ssl, const uint8_t *der, size_t der_len) { |
David Benjamin | ee910bf | 2017-07-25 22:36:00 -0400 | [diff] [blame] | 909 | UniquePtr<CRYPTO_BUFFER> buffer(CRYPTO_BUFFER_new(der, der_len, NULL)); |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 910 | if (!buffer || !ssl->config) { |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 911 | return 0; |
| 912 | } |
| 913 | |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 914 | return ssl_set_cert(ssl->config->cert.get(), std::move(buffer)); |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | void SSL_CTX_set_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl, void *arg), |
| 918 | void *arg) { |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 919 | ssl_cert_set_cert_cb(ctx->cert.get(), cb, arg); |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | void SSL_set_cert_cb(SSL *ssl, int (*cb)(SSL *ssl, void *arg), void *arg) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 923 | if (!ssl->config) { |
| 924 | return; |
| 925 | } |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 926 | ssl_cert_set_cert_cb(ssl->config->cert.get(), cb, arg); |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 927 | } |
| 928 | |
David Benjamin | 5f001d1 | 2018-05-08 16:46:48 -0400 | [diff] [blame] | 929 | const STACK_OF(CRYPTO_BUFFER) *SSL_get0_peer_certificates(const SSL *ssl) { |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 930 | SSL_SESSION *session = SSL_get_session(ssl); |
| 931 | if (session == NULL) { |
| 932 | return NULL; |
| 933 | } |
| 934 | |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 935 | return session->certs.get(); |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 936 | } |
| 937 | |
David Benjamin | 5f001d1 | 2018-05-08 16:46:48 -0400 | [diff] [blame] | 938 | const STACK_OF(CRYPTO_BUFFER) *SSL_get0_server_requested_CAs(const SSL *ssl) { |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 939 | if (ssl->s3->hs == NULL) { |
| 940 | return NULL; |
| 941 | } |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 942 | return ssl->s3->hs->ca_names.get(); |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 943 | } |
| 944 | |
David Benjamin | 83a3212 | 2017-02-14 18:34:54 -0500 | [diff] [blame] | 945 | static int set_signed_cert_timestamp_list(CERT *cert, const uint8_t *list, |
David Benjamin | e325c3f | 2018-04-12 16:11:15 -0400 | [diff] [blame] | 946 | size_t list_len) { |
David Benjamin | 83a3212 | 2017-02-14 18:34:54 -0500 | [diff] [blame] | 947 | CBS sct_list; |
| 948 | CBS_init(&sct_list, list, list_len); |
| 949 | if (!ssl_is_sct_list_valid(&sct_list)) { |
| 950 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SCT_LIST); |
| 951 | return 0; |
| 952 | } |
| 953 | |
David Benjamin | e325c3f | 2018-04-12 16:11:15 -0400 | [diff] [blame] | 954 | cert->signed_cert_timestamp_list.reset( |
| 955 | CRYPTO_BUFFER_new(CBS_data(&sct_list), CBS_len(&sct_list), nullptr)); |
| 956 | return cert->signed_cert_timestamp_list != nullptr; |
David Benjamin | 83a3212 | 2017-02-14 18:34:54 -0500 | [diff] [blame] | 957 | } |
| 958 | |
| 959 | int SSL_CTX_set_signed_cert_timestamp_list(SSL_CTX *ctx, const uint8_t *list, |
| 960 | size_t list_len) { |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 961 | return set_signed_cert_timestamp_list(ctx->cert.get(), list, list_len); |
David Benjamin | 83a3212 | 2017-02-14 18:34:54 -0500 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | int SSL_set_signed_cert_timestamp_list(SSL *ssl, const uint8_t *list, |
| 965 | size_t list_len) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 966 | if (!ssl->config) { |
| 967 | return 0; |
| 968 | } |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 969 | return set_signed_cert_timestamp_list(ssl->config->cert.get(), list, |
| 970 | list_len); |
David Benjamin | 83a3212 | 2017-02-14 18:34:54 -0500 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | int SSL_CTX_set_ocsp_response(SSL_CTX *ctx, const uint8_t *response, |
| 974 | size_t response_len) { |
David Benjamin | e325c3f | 2018-04-12 16:11:15 -0400 | [diff] [blame] | 975 | ctx->cert->ocsp_response.reset( |
| 976 | CRYPTO_BUFFER_new(response, response_len, nullptr)); |
| 977 | return ctx->cert->ocsp_response != nullptr; |
David Benjamin | 83a3212 | 2017-02-14 18:34:54 -0500 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | int SSL_set_ocsp_response(SSL *ssl, const uint8_t *response, |
| 981 | size_t response_len) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 982 | if (!ssl->config) { |
| 983 | return 0; |
| 984 | } |
| 985 | ssl->config->cert->ocsp_response.reset( |
David Benjamin | e325c3f | 2018-04-12 16:11:15 -0400 | [diff] [blame] | 986 | CRYPTO_BUFFER_new(response, response_len, nullptr)); |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 987 | return ssl->config->cert->ocsp_response != nullptr; |
David Benjamin | 83a3212 | 2017-02-14 18:34:54 -0500 | [diff] [blame] | 988 | } |
David Benjamin | 71dfad4 | 2017-07-16 17:27:39 -0400 | [diff] [blame] | 989 | |
| 990 | void SSL_CTX_set0_client_CAs(SSL_CTX *ctx, STACK_OF(CRYPTO_BUFFER) *name_list) { |
| 991 | ctx->x509_method->ssl_ctx_flush_cached_client_CA(ctx); |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 992 | ctx->client_CA.reset(name_list); |
David Benjamin | 71dfad4 | 2017-07-16 17:27:39 -0400 | [diff] [blame] | 993 | } |
| 994 | |
| 995 | void SSL_set0_client_CAs(SSL *ssl, STACK_OF(CRYPTO_BUFFER) *name_list) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 996 | if (!ssl->config) { |
| 997 | return; |
| 998 | } |
David Benjamin | 50596f8 | 2018-07-02 19:47:27 -0400 | [diff] [blame] | 999 | ssl->ctx->x509_method->ssl_flush_cached_client_CA(ssl->config.get()); |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 1000 | ssl->config->client_CA.reset(name_list); |
David Benjamin | 71dfad4 | 2017-07-16 17:27:39 -0400 | [diff] [blame] | 1001 | } |
Christopher Patton | 6c1b376 | 2018-07-17 12:49:41 -0700 | [diff] [blame] | 1002 | |
| 1003 | int SSL_set1_delegated_credential(SSL *ssl, CRYPTO_BUFFER *dc, EVP_PKEY *pkey, |
| 1004 | const SSL_PRIVATE_KEY_METHOD *key_method) { |
| 1005 | if (!ssl->config) { |
| 1006 | return 0; |
| 1007 | } |
| 1008 | |
| 1009 | return cert_set_dc(ssl->config->cert.get(), dc, pkey, key_method); |
| 1010 | } |
Watson Ladd | 629f321 | 2019-06-11 17:46:52 -0700 | [diff] [blame] | 1011 | |
| 1012 | int SSL_delegated_credential_used(const SSL *ssl) { |
| 1013 | return ssl->s3->delegated_credential_used; |
| 1014 | } |