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