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. |
Adam Langley | e745b25 | 2018-02-26 14:02:17 -0800 | [diff] [blame] | 7 | * |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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). |
Adam Langley | e745b25 | 2018-02-26 14:02:17 -0800 | [diff] [blame] | 14 | * |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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. |
Adam Langley | e745b25 | 2018-02-26 14:02:17 -0800 | [diff] [blame] | 21 | * |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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 :-). |
Adam Langley | e745b25 | 2018-02-26 14:02:17 -0800 | [diff] [blame] | 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 37 | * the apps directory (application code) you must include an acknowledgement: |
| 38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
Adam Langley | e745b25 | 2018-02-26 14:02:17 -0800 | [diff] [blame] | 39 | * |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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. |
Adam Langley | e745b25 | 2018-02-26 14:02:17 -0800 | [diff] [blame] | 51 | * |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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 |
Adam Langley | e745b25 | 2018-02-26 14:02:17 -0800 | [diff] [blame] | 65 | * notice, this list of conditions and the following disclaimer. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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. |
Adam Langley | e745b25 | 2018-02-26 14:02:17 -0800 | [diff] [blame] | 112 | * ECC cipher suite support in OpenSSL originally developed by |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 113 | * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. |
| 114 | */ |
| 115 | /* ==================================================================== |
| 116 | * Copyright 2005 Nokia. All rights reserved. |
| 117 | * |
| 118 | * The portions of the attached software ("Contribution") is developed by |
| 119 | * Nokia Corporation and is licensed pursuant to the OpenSSL open source |
| 120 | * license. |
| 121 | * |
| 122 | * The Contribution, originally written by Mika Kousa and Pasi Eronen of |
| 123 | * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites |
| 124 | * support (see RFC 4279) to OpenSSL. |
| 125 | * |
| 126 | * No patent licenses or other rights except those expressly stated in |
| 127 | * the OpenSSL open source license shall be deemed granted or received |
| 128 | * expressly, by implication, estoppel, or otherwise. |
| 129 | * |
| 130 | * No assurances are provided by Nokia that the Contribution does not |
| 131 | * infringe the patent or other intellectual property rights of any third |
| 132 | * party or that the license provides you with all the necessary rights |
| 133 | * to make use of the Contribution. |
| 134 | * |
| 135 | * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN |
| 136 | * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA |
| 137 | * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY |
| 138 | * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR |
| 139 | * OTHERWISE. */ |
| 140 | |
David Benjamin | 9e4e01e | 2015-09-15 01:48:04 -0400 | [diff] [blame] | 141 | #include <openssl/ssl.h> |
| 142 | |
David Benjamin | 3f180b8 | 2022-05-09 17:45:18 -0400 | [diff] [blame] | 143 | #include <algorithm> |
| 144 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 145 | #include <assert.h> |
David Benjamin | 7ac94aa | 2022-10-24 15:44:37 -0400 | [diff] [blame] | 146 | #include <limits.h> |
David Benjamin | 93d9743 | 2016-08-08 21:22:47 -0400 | [diff] [blame] | 147 | #include <stdlib.h> |
David Benjamin | f0ae170 | 2015-04-07 23:05:04 -0400 | [diff] [blame] | 148 | #include <string.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 149 | |
David Benjamin | 39482a1 | 2014-07-20 13:30:15 -0400 | [diff] [blame] | 150 | #include <openssl/bytestring.h> |
David Benjamin | 7c6d09b | 2015-09-05 11:00:19 -0400 | [diff] [blame] | 151 | #include <openssl/crypto.h> |
David Benjamin | f0ae170 | 2015-04-07 23:05:04 -0400 | [diff] [blame] | 152 | #include <openssl/err.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 153 | #include <openssl/lhash.h> |
| 154 | #include <openssl/mem.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 155 | #include <openssl/rand.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 156 | |
David Benjamin | 2ee94aa | 2015-04-07 22:38:30 -0400 | [diff] [blame] | 157 | #include "internal.h" |
David Benjamin | 546f1a5 | 2015-04-15 16:46:09 -0400 | [diff] [blame] | 158 | #include "../crypto/internal.h" |
| 159 | |
David Benjamin | 721e8b7 | 2016-08-03 13:13:17 -0400 | [diff] [blame] | 160 | #if defined(OPENSSL_WINDOWS) |
| 161 | #include <sys/timeb.h> |
| 162 | #else |
| 163 | #include <sys/socket.h> |
| 164 | #include <sys/time.h> |
| 165 | #endif |
| 166 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 167 | |
Joshua Liebow-Feeser | 8c7c635 | 2018-08-26 18:53:36 -0700 | [diff] [blame] | 168 | BSSL_NAMESPACE_BEGIN |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 169 | |
David Benjamin | b7d6320 | 2022-07-26 13:25:02 -0700 | [diff] [blame] | 170 | static_assert(SSL3_RT_MAX_ENCRYPTED_OVERHEAD >= |
| 171 | SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD, |
| 172 | "max overheads are inconsistent"); |
| 173 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 174 | // |SSL_R_UNKNOWN_PROTOCOL| is no longer emitted, but continue to define it |
| 175 | // to avoid downstream churn. |
David Benjamin | 76c2efc | 2015-08-31 14:24:29 -0400 | [diff] [blame] | 176 | OPENSSL_DECLARE_ERROR_REASON(SSL, UNKNOWN_PROTOCOL) |
David Benjamin | 97760d5 | 2015-07-24 23:02:49 -0400 | [diff] [blame] | 177 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 178 | // The following errors are no longer emitted, but are used in nginx without |
| 179 | // #ifdefs. |
David Benjamin | 311c257 | 2016-08-17 15:54:36 -0400 | [diff] [blame] | 180 | OPENSSL_DECLARE_ERROR_REASON(SSL, BLOCK_CIPHER_PAD_IS_WRONG) |
| 181 | OPENSSL_DECLARE_ERROR_REASON(SSL, NO_CIPHERS_SPECIFIED) |
| 182 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 183 | // Some error codes are special. Ensure the make_errors.go script never |
| 184 | // regresses this. |
David Benjamin | a3d76d0 | 2017-07-14 19:36:07 -0400 | [diff] [blame] | 185 | static_assert(SSL_R_TLSV1_ALERT_NO_RENEGOTIATION == |
| 186 | SSL_AD_NO_RENEGOTIATION + SSL_AD_REASON_OFFSET, |
| 187 | "alert reason code mismatch"); |
David Benjamin | e113608 | 2014-09-20 12:28:58 -0400 | [diff] [blame] | 188 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 189 | // kMaxHandshakeSize is the maximum size, in bytes, of a handshake message. |
David Benjamin | 1d0a194 | 2015-04-26 15:35:35 -0400 | [diff] [blame] | 190 | static const size_t kMaxHandshakeSize = (1u << 24) - 1; |
| 191 | |
David Benjamin | aa58513 | 2015-06-29 23:36:17 -0400 | [diff] [blame] | 192 | static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl = |
| 193 | CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA; |
| 194 | static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl_ctx = |
| 195 | CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA; |
David Benjamin | 9f33fc6 | 2015-04-15 17:29:53 -0400 | [diff] [blame] | 196 | |
David Benjamin | 879efc3 | 2017-09-21 11:20:53 -0400 | [diff] [blame] | 197 | bool CBBFinishArray(CBB *cbb, Array<uint8_t> *out) { |
| 198 | uint8_t *ptr; |
| 199 | size_t len; |
| 200 | if (!CBB_finish(cbb, &ptr, &len)) { |
| 201 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 202 | return false; |
| 203 | } |
| 204 | out->Reset(ptr, len); |
| 205 | return true; |
| 206 | } |
| 207 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 208 | void ssl_reset_error_state(SSL *ssl) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 209 | // Functions which use |SSL_get_error| must reset I/O and error state on |
| 210 | // entry. |
David Benjamin | f492830 | 2019-08-21 16:04:53 -0400 | [diff] [blame] | 211 | ssl->s3->rwstate = SSL_ERROR_NONE; |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 212 | ERR_clear_error(); |
| 213 | ERR_clear_system_error(); |
| 214 | } |
| 215 | |
David Benjamin | 31aad2d | 2017-10-06 16:04:06 -0400 | [diff] [blame] | 216 | void ssl_set_read_error(SSL* ssl) { |
| 217 | ssl->s3->read_shutdown = ssl_shutdown_error; |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 218 | ssl->s3->read_error.reset(ERR_save_state()); |
David Benjamin | 31aad2d | 2017-10-06 16:04:06 -0400 | [diff] [blame] | 219 | } |
| 220 | |
David Benjamin | f6632da | 2017-10-12 19:11:47 -0400 | [diff] [blame] | 221 | static bool check_read_error(const SSL *ssl) { |
| 222 | if (ssl->s3->read_shutdown == ssl_shutdown_error) { |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 223 | ERR_restore_state(ssl->s3->read_error.get()); |
David Benjamin | f6632da | 2017-10-12 19:11:47 -0400 | [diff] [blame] | 224 | return false; |
| 225 | } |
| 226 | return true; |
| 227 | } |
| 228 | |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 229 | bool ssl_can_write(const SSL *ssl) { |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 230 | return !SSL_in_init(ssl) || ssl->s3->hs->can_early_write; |
| 231 | } |
| 232 | |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 233 | bool ssl_can_read(const SSL *ssl) { |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 234 | return !SSL_in_init(ssl) || ssl->s3->hs->can_early_read; |
| 235 | } |
| 236 | |
David Benjamin | f6632da | 2017-10-12 19:11:47 -0400 | [diff] [blame] | 237 | ssl_open_record_t ssl_open_handshake(SSL *ssl, size_t *out_consumed, |
| 238 | uint8_t *out_alert, Span<uint8_t> in) { |
| 239 | *out_consumed = 0; |
| 240 | if (!check_read_error(ssl)) { |
| 241 | *out_alert = 0; |
| 242 | return ssl_open_record_error; |
| 243 | } |
| 244 | auto ret = ssl->method->open_handshake(ssl, out_consumed, out_alert, in); |
| 245 | if (ret == ssl_open_record_error) { |
| 246 | ssl_set_read_error(ssl); |
| 247 | } |
| 248 | return ret; |
| 249 | } |
| 250 | |
| 251 | ssl_open_record_t ssl_open_change_cipher_spec(SSL *ssl, size_t *out_consumed, |
| 252 | uint8_t *out_alert, |
| 253 | Span<uint8_t> in) { |
| 254 | *out_consumed = 0; |
| 255 | if (!check_read_error(ssl)) { |
| 256 | *out_alert = 0; |
| 257 | return ssl_open_record_error; |
| 258 | } |
| 259 | auto ret = |
| 260 | ssl->method->open_change_cipher_spec(ssl, out_consumed, out_alert, in); |
| 261 | if (ret == ssl_open_record_error) { |
| 262 | ssl_set_read_error(ssl); |
| 263 | } |
| 264 | return ret; |
| 265 | } |
| 266 | |
| 267 | ssl_open_record_t ssl_open_app_data(SSL *ssl, Span<uint8_t> *out, |
| 268 | size_t *out_consumed, uint8_t *out_alert, |
| 269 | Span<uint8_t> in) { |
| 270 | *out_consumed = 0; |
| 271 | if (!check_read_error(ssl)) { |
| 272 | *out_alert = 0; |
| 273 | return ssl_open_record_error; |
| 274 | } |
| 275 | auto ret = ssl->method->open_app_data(ssl, out, out_consumed, out_alert, in); |
| 276 | if (ret == ssl_open_record_error) { |
| 277 | ssl_set_read_error(ssl); |
| 278 | } |
| 279 | return ret; |
| 280 | } |
| 281 | |
David Benjamin | b244e3a | 2019-08-16 19:33:15 -0400 | [diff] [blame] | 282 | static bool cbb_add_hex(CBB *cbb, Span<const uint8_t> in) { |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 283 | static const char hextable[] = "0123456789abcdef"; |
| 284 | uint8_t *out; |
| 285 | |
David Benjamin | b244e3a | 2019-08-16 19:33:15 -0400 | [diff] [blame] | 286 | if (!CBB_add_space(cbb, &out, in.size() * 2)) { |
| 287 | return false; |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 288 | } |
| 289 | |
David Benjamin | b244e3a | 2019-08-16 19:33:15 -0400 | [diff] [blame] | 290 | for (uint8_t b : in) { |
| 291 | *(out++) = (uint8_t)hextable[b >> 4]; |
| 292 | *(out++) = (uint8_t)hextable[b & 0xf]; |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 293 | } |
| 294 | |
David Benjamin | b244e3a | 2019-08-16 19:33:15 -0400 | [diff] [blame] | 295 | return true; |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 296 | } |
| 297 | |
David Benjamin | b244e3a | 2019-08-16 19:33:15 -0400 | [diff] [blame] | 298 | bool ssl_log_secret(const SSL *ssl, const char *label, |
| 299 | Span<const uint8_t> secret) { |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 300 | if (ssl->ctx->keylog_callback == NULL) { |
David Benjamin | b244e3a | 2019-08-16 19:33:15 -0400 | [diff] [blame] | 301 | return true; |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 302 | } |
| 303 | |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 304 | ScopedCBB cbb; |
David Benjamin | b244e3a | 2019-08-16 19:33:15 -0400 | [diff] [blame] | 305 | Array<uint8_t> line; |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 306 | if (!CBB_init(cbb.get(), strlen(label) + 1 + SSL3_RANDOM_SIZE * 2 + 1 + |
David Benjamin | b244e3a | 2019-08-16 19:33:15 -0400 | [diff] [blame] | 307 | secret.size() * 2 + 1) || |
| 308 | !CBB_add_bytes(cbb.get(), reinterpret_cast<const uint8_t *>(label), |
| 309 | strlen(label)) || |
| 310 | !CBB_add_u8(cbb.get(), ' ') || |
| 311 | !cbb_add_hex(cbb.get(), ssl->s3->client_random) || |
| 312 | !CBB_add_u8(cbb.get(), ' ') || |
| 313 | !cbb_add_hex(cbb.get(), secret) || |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 314 | !CBB_add_u8(cbb.get(), 0 /* NUL */) || |
David Benjamin | b244e3a | 2019-08-16 19:33:15 -0400 | [diff] [blame] | 315 | !CBBFinishArray(cbb.get(), &line)) { |
| 316 | return false; |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 317 | } |
| 318 | |
David Benjamin | b244e3a | 2019-08-16 19:33:15 -0400 | [diff] [blame] | 319 | ssl->ctx->keylog_callback(ssl, reinterpret_cast<const char *>(line.data())); |
| 320 | return true; |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 321 | } |
| 322 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 323 | void ssl_do_info_callback(const SSL *ssl, int type, int value) { |
| 324 | void (*cb)(const SSL *ssl, int type, int value) = NULL; |
| 325 | if (ssl->info_callback != NULL) { |
| 326 | cb = ssl->info_callback; |
| 327 | } else if (ssl->ctx->info_callback != NULL) { |
| 328 | cb = ssl->ctx->info_callback; |
| 329 | } |
| 330 | |
| 331 | if (cb != NULL) { |
| 332 | cb(ssl, type, value); |
| 333 | } |
| 334 | } |
| 335 | |
Adam Langley | c9827e0 | 2019-04-12 14:46:50 -0700 | [diff] [blame] | 336 | void ssl_do_msg_callback(const SSL *ssl, int is_write, int content_type, |
David Benjamin | c64d123 | 2017-10-04 18:14:28 -0400 | [diff] [blame] | 337 | Span<const uint8_t> in) { |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 338 | if (ssl->msg_callback == NULL) { |
| 339 | return; |
| 340 | } |
| 341 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 342 | // |version| is zero when calling for |SSL3_RT_HEADER| and |SSL2_VERSION| for |
| 343 | // a V2ClientHello. |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 344 | int version; |
| 345 | switch (content_type) { |
| 346 | case 0: |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 347 | // V2ClientHello |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 348 | version = SSL2_VERSION; |
| 349 | break; |
| 350 | case SSL3_RT_HEADER: |
| 351 | version = 0; |
| 352 | break; |
| 353 | default: |
| 354 | version = SSL_version(ssl); |
| 355 | } |
| 356 | |
Adam Langley | c9827e0 | 2019-04-12 14:46:50 -0700 | [diff] [blame] | 357 | ssl->msg_callback(is_write, version, content_type, in.data(), in.size(), |
| 358 | const_cast<SSL *>(ssl), ssl->msg_callback_arg); |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | void ssl_get_current_time(const SSL *ssl, struct OPENSSL_timeval *out_clock) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 362 | // TODO(martinkr): Change callers to |ssl_ctx_get_current_time| and drop the |
| 363 | // |ssl| arg from |current_time_cb| if possible. |
David Benjamin | 50596f8 | 2018-07-02 19:47:27 -0400 | [diff] [blame] | 364 | ssl_ctx_get_current_time(ssl->ctx.get(), out_clock); |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | void ssl_ctx_get_current_time(const SSL_CTX *ctx, |
| 368 | struct OPENSSL_timeval *out_clock) { |
| 369 | if (ctx->current_time_cb != NULL) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 370 | // TODO(davidben): Update current_time_cb to use OPENSSL_timeval. See |
| 371 | // https://crbug.com/boringssl/155. |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 372 | struct timeval clock; |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 373 | ctx->current_time_cb(nullptr /* ssl */, &clock); |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 374 | if (clock.tv_sec < 0) { |
| 375 | assert(0); |
| 376 | out_clock->tv_sec = 0; |
| 377 | out_clock->tv_usec = 0; |
| 378 | } else { |
| 379 | out_clock->tv_sec = (uint64_t)clock.tv_sec; |
| 380 | out_clock->tv_usec = (uint32_t)clock.tv_usec; |
| 381 | } |
| 382 | return; |
| 383 | } |
| 384 | |
| 385 | #if defined(BORINGSSL_UNSAFE_DETERMINISTIC_MODE) |
| 386 | out_clock->tv_sec = 1234; |
| 387 | out_clock->tv_usec = 1234; |
| 388 | #elif defined(OPENSSL_WINDOWS) |
| 389 | struct _timeb time; |
| 390 | _ftime(&time); |
| 391 | if (time.time < 0) { |
| 392 | assert(0); |
| 393 | out_clock->tv_sec = 0; |
| 394 | out_clock->tv_usec = 0; |
| 395 | } else { |
| 396 | out_clock->tv_sec = time.time; |
| 397 | out_clock->tv_usec = time.millitm * 1000; |
| 398 | } |
| 399 | #else |
| 400 | struct timeval clock; |
| 401 | gettimeofday(&clock, NULL); |
| 402 | if (clock.tv_sec < 0) { |
| 403 | assert(0); |
| 404 | out_clock->tv_sec = 0; |
| 405 | out_clock->tv_usec = 0; |
| 406 | } else { |
| 407 | out_clock->tv_sec = (uint64_t)clock.tv_sec; |
| 408 | out_clock->tv_usec = (uint32_t)clock.tv_usec; |
| 409 | } |
| 410 | #endif |
| 411 | } |
| 412 | |
Adam Langley | 3fe8fa7 | 2018-01-26 09:14:30 -0800 | [diff] [blame] | 413 | void SSL_CTX_set_handoff_mode(SSL_CTX *ctx, bool on) { |
| 414 | ctx->handoff = on; |
| 415 | } |
| 416 | |
David Benjamin | 71666cb | 2018-05-01 17:14:27 -0400 | [diff] [blame] | 417 | static bool ssl_can_renegotiate(const SSL *ssl) { |
| 418 | if (ssl->server || SSL_is_dtls(ssl)) { |
| 419 | return false; |
| 420 | } |
| 421 | |
David Benjamin | 9b2cdb7 | 2021-04-01 23:21:53 -0400 | [diff] [blame] | 422 | if (ssl->s3->have_version && |
| 423 | ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
David Benjamin | 71666cb | 2018-05-01 17:14:27 -0400 | [diff] [blame] | 424 | return false; |
| 425 | } |
| 426 | |
| 427 | // The config has already been shed. |
| 428 | if (!ssl->config) { |
| 429 | return false; |
| 430 | } |
| 431 | |
| 432 | switch (ssl->renegotiate_mode) { |
| 433 | case ssl_renegotiate_ignore: |
| 434 | case ssl_renegotiate_never: |
| 435 | return false; |
| 436 | |
| 437 | case ssl_renegotiate_freely: |
David Benjamin | 0e7dbd5 | 2019-05-15 16:01:18 -0400 | [diff] [blame] | 438 | case ssl_renegotiate_explicit: |
David Benjamin | 71666cb | 2018-05-01 17:14:27 -0400 | [diff] [blame] | 439 | return true; |
| 440 | case ssl_renegotiate_once: |
| 441 | return ssl->s3->total_renegotiations == 0; |
| 442 | } |
| 443 | |
| 444 | assert(0); |
| 445 | return false; |
| 446 | } |
| 447 | |
| 448 | static void ssl_maybe_shed_handshake_config(SSL *ssl) { |
| 449 | if (ssl->s3->hs != nullptr || |
| 450 | ssl->config == nullptr || |
| 451 | !ssl->config->shed_handshake_config || |
| 452 | ssl_can_renegotiate(ssl)) { |
| 453 | return; |
| 454 | } |
| 455 | |
David Benjamin | 50596f8 | 2018-07-02 19:47:27 -0400 | [diff] [blame] | 456 | ssl->config.reset(); |
David Benjamin | 71666cb | 2018-05-01 17:14:27 -0400 | [diff] [blame] | 457 | } |
| 458 | |
Matthew Braithwaite | e30fac6 | 2018-04-05 16:50:21 -0700 | [diff] [blame] | 459 | void SSL_set_handoff_mode(SSL *ssl, bool on) { |
| 460 | if (!ssl->config) { |
| 461 | return; |
| 462 | } |
| 463 | ssl->config->handoff = on; |
| 464 | } |
| 465 | |
Adam Langley | d6e1f23 | 2018-12-12 14:11:01 -0800 | [diff] [blame] | 466 | bool SSL_get_traffic_secrets(const SSL *ssl, |
| 467 | Span<const uint8_t> *out_read_traffic_secret, |
| 468 | Span<const uint8_t> *out_write_traffic_secret) { |
| 469 | if (SSL_version(ssl) < TLS1_3_VERSION) { |
| 470 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION); |
| 471 | return false; |
| 472 | } |
| 473 | |
| 474 | if (!ssl->s3->initial_handshake_complete) { |
| 475 | OPENSSL_PUT_ERROR(SSL, SSL_R_HANDSHAKE_NOT_COMPLETE); |
| 476 | return false; |
| 477 | } |
| 478 | |
| 479 | *out_read_traffic_secret = Span<const uint8_t>( |
| 480 | ssl->s3->read_traffic_secret, ssl->s3->read_traffic_secret_len); |
| 481 | *out_write_traffic_secret = Span<const uint8_t>( |
| 482 | ssl->s3->write_traffic_secret, ssl->s3->write_traffic_secret_len); |
| 483 | |
| 484 | return true; |
| 485 | } |
| 486 | |
Bob Beck | 8cacbd9 | 2023-04-05 14:38:38 -0600 | [diff] [blame] | 487 | void SSL_CTX_set_aes_hw_override_for_testing(SSL_CTX *ctx, |
| 488 | bool override_value) { |
| 489 | ctx->aes_hw_override = true; |
| 490 | ctx->aes_hw_override_value = override_value; |
| 491 | } |
| 492 | |
| 493 | void SSL_set_aes_hw_override_for_testing(SSL *ssl, bool override_value) { |
| 494 | ssl->config->aes_hw_override = true; |
| 495 | ssl->config->aes_hw_override_value = override_value; |
| 496 | } |
| 497 | |
Joshua Liebow-Feeser | 8c7c635 | 2018-08-26 18:53:36 -0700 | [diff] [blame] | 498 | BSSL_NAMESPACE_END |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 499 | |
| 500 | using namespace bssl; |
| 501 | |
David Benjamin | 7c6d09b | 2015-09-05 11:00:19 -0400 | [diff] [blame] | 502 | int SSL_library_init(void) { |
| 503 | CRYPTO_library_init(); |
| 504 | return 1; |
| 505 | } |
| 506 | |
David Benjamin | 81f030b | 2016-08-12 14:48:19 -0400 | [diff] [blame] | 507 | int OPENSSL_init_ssl(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings) { |
| 508 | CRYPTO_library_init(); |
| 509 | return 1; |
| 510 | } |
| 511 | |
Adam Langley | e976e43 | 2016-02-15 14:01:10 -0800 | [diff] [blame] | 512 | static uint32_t ssl_session_hash(const SSL_SESSION *sess) { |
David Benjamin | 58150ed | 2018-06-29 23:58:43 -0400 | [diff] [blame] | 513 | return ssl_hash_session_id( |
| 514 | MakeConstSpan(sess->session_id, sess->session_id_length)); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 515 | } |
| 516 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 517 | static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) { |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 518 | if (a->session_id_length != b->session_id_length) { |
| 519 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 520 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 521 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 522 | return OPENSSL_memcmp(a->session_id, b->session_id, a->session_id_length); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 523 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 524 | |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 525 | ssl_ctx_st::ssl_ctx_st(const SSL_METHOD *ssl_method) |
David Benjamin | fbf10f0 | 2024-02-12 16:57:18 -0500 | [diff] [blame] | 526 | : RefCounted(CheckSubClass()), |
| 527 | method(ssl_method->method), |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 528 | x509_method(ssl_method->x509_method), |
| 529 | retain_only_sha256_of_client_certs(false), |
| 530 | quiet_shutdown(false), |
| 531 | ocsp_stapling_enabled(false), |
| 532 | signed_cert_timestamps_enabled(false), |
David Benjamin | 4685376 | 2018-07-03 14:01:26 -0400 | [diff] [blame] | 533 | channel_id_enabled(false), |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 534 | grease_enabled(false), |
David Benjamin | e9c5d72 | 2021-06-09 17:43:16 -0400 | [diff] [blame] | 535 | permute_extensions(false), |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 536 | allow_unknown_alpn_protos(false), |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 537 | false_start_allowed_without_alpn(false), |
| 538 | handoff(false), |
Adam Langley | 451ea3c | 2022-05-18 16:15:49 -0700 | [diff] [blame] | 539 | enable_early_data(false), |
Bob Beck | 8cacbd9 | 2023-04-05 14:38:38 -0600 | [diff] [blame] | 540 | aes_hw_override(false), |
| 541 | aes_hw_override_value(false) { |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 542 | CRYPTO_MUTEX_init(&lock); |
| 543 | CRYPTO_new_ex_data(&ex_data); |
| 544 | } |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 545 | |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 546 | ssl_ctx_st::~ssl_ctx_st() { |
| 547 | // Free the internal session cache. Note that this calls the caller-supplied |
| 548 | // remove callback, so we must do it before clearing ex_data. (See ticket |
| 549 | // [openssl.org #212].) |
| 550 | SSL_CTX_flush_sessions(this, 0); |
| 551 | |
| 552 | CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, this, &ex_data); |
| 553 | |
| 554 | CRYPTO_MUTEX_cleanup(&lock); |
| 555 | lh_SSL_SESSION_free(sessions); |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 556 | x509_method->ssl_ctx_free(this); |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | SSL_CTX *SSL_CTX_new(const SSL_METHOD *method) { |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 560 | if (method == NULL) { |
| 561 | OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_METHOD_PASSED); |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 562 | return nullptr; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 563 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 564 | |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 565 | UniquePtr<SSL_CTX> ret = MakeUnique<SSL_CTX>(method); |
| 566 | if (!ret) { |
| 567 | return nullptr; |
David Benjamin | 62fd162 | 2015-01-11 13:30:01 -0500 | [diff] [blame] | 568 | } |
| 569 | |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 570 | ret->cert = MakeUnique<CERT>(method->x509_method); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 571 | ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp); |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 572 | ret->client_CA.reset(sk_CRYPTO_BUFFER_new_null()); |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 573 | if (ret->cert == nullptr || |
| 574 | ret->sessions == nullptr || |
| 575 | ret->client_CA == nullptr || |
| 576 | !ret->x509_method->ssl_ctx_new(ret.get())) { |
| 577 | return nullptr; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 578 | } |
| 579 | |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 580 | if (!SSL_CTX_set_strict_cipher_list(ret.get(), SSL_DEFAULT_CIPHER_LIST) || |
| 581 | // Lock the SSL_CTX to the specified version, for compatibility with |
| 582 | // legacy uses of SSL_METHOD. |
| 583 | !SSL_CTX_set_max_proto_version(ret.get(), method->version) || |
| 584 | !SSL_CTX_set_min_proto_version(ret.get(), method->version)) { |
David Benjamin | e34bcc9 | 2016-09-21 16:53:09 -0400 | [diff] [blame] | 585 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 586 | return nullptr; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 587 | } |
| 588 | |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 589 | return ret.release(); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 590 | } |
| 591 | |
Adam Langley | 84cd159 | 2016-06-30 18:29:44 -0700 | [diff] [blame] | 592 | int SSL_CTX_up_ref(SSL_CTX *ctx) { |
David Benjamin | fbf10f0 | 2024-02-12 16:57:18 -0500 | [diff] [blame] | 593 | ctx->UpRefInternal(); |
Adam Langley | 84cd159 | 2016-06-30 18:29:44 -0700 | [diff] [blame] | 594 | return 1; |
| 595 | } |
| 596 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 597 | void SSL_CTX_free(SSL_CTX *ctx) { |
David Benjamin | fbf10f0 | 2024-02-12 16:57:18 -0500 | [diff] [blame] | 598 | if (ctx != nullptr) { |
| 599 | ctx->DecRefInternal(); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 600 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 601 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 602 | |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 603 | ssl_st::ssl_st(SSL_CTX *ctx_arg) |
| 604 | : method(ctx_arg->method), |
| 605 | max_send_fragment(ctx_arg->max_send_fragment), |
| 606 | msg_callback(ctx_arg->msg_callback), |
| 607 | msg_callback_arg(ctx_arg->msg_callback_arg), |
David Benjamin | 50596f8 | 2018-07-02 19:47:27 -0400 | [diff] [blame] | 608 | ctx(UpRef(ctx_arg)), |
| 609 | session_ctx(UpRef(ctx_arg)), |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 610 | options(ctx->options), |
| 611 | mode(ctx->mode), |
| 612 | max_cert_list(ctx->max_cert_list), |
| 613 | server(false), |
| 614 | quiet_shutdown(ctx->quiet_shutdown), |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 615 | enable_early_data(ctx->enable_early_data) { |
| 616 | CRYPTO_new_ex_data(&ex_data); |
| 617 | } |
| 618 | |
| 619 | ssl_st::~ssl_st() { |
| 620 | CRYPTO_free_ex_data(&g_ex_data_class_ssl, this, &ex_data); |
David Benjamin | 50596f8 | 2018-07-02 19:47:27 -0400 | [diff] [blame] | 621 | // |config| refers to |this|, so we must release it earlier. |
| 622 | config.reset(); |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 623 | if (method != NULL) { |
| 624 | method->ssl_free(this); |
| 625 | } |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 626 | } |
| 627 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 628 | SSL *SSL_new(SSL_CTX *ctx) { |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 629 | if (ctx == nullptr) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 630 | OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_CTX); |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 631 | return nullptr; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 632 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 633 | |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 634 | UniquePtr<SSL> ssl = MakeUnique<SSL>(ctx); |
| 635 | if (ssl == nullptr) { |
| 636 | return nullptr; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 637 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 638 | |
David Benjamin | 50596f8 | 2018-07-02 19:47:27 -0400 | [diff] [blame] | 639 | ssl->config = MakeUnique<SSL_CONFIG>(ssl.get()); |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 640 | if (ssl->config == nullptr) { |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 641 | return nullptr; |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 642 | } |
| 643 | ssl->config->conf_min_version = ctx->conf_min_version; |
| 644 | ssl->config->conf_max_version = ctx->conf_max_version; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 645 | |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 646 | ssl->config->cert = ssl_cert_dup(ctx->cert.get()); |
| 647 | if (ssl->config->cert == nullptr) { |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 648 | return nullptr; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 649 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 650 | |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 651 | ssl->config->verify_mode = ctx->verify_mode; |
| 652 | ssl->config->verify_callback = ctx->default_verify_callback; |
| 653 | ssl->config->custom_verify_callback = ctx->custom_verify_callback; |
| 654 | ssl->config->retain_only_sha256_of_client_certs = |
David Benjamin | bbaf367 | 2016-11-17 10:53:09 +0900 | [diff] [blame] | 655 | ctx->retain_only_sha256_of_client_certs; |
David Benjamin | e9c5d72 | 2021-06-09 17:43:16 -0400 | [diff] [blame] | 656 | ssl->config->permute_extensions = ctx->permute_extensions; |
Bob Beck | 8cacbd9 | 2023-04-05 14:38:38 -0600 | [diff] [blame] | 657 | ssl->config->aes_hw_override = ctx->aes_hw_override; |
| 658 | ssl->config->aes_hw_override_value = ctx->aes_hw_override_value; |
Adam Langley | 2f6409e | 2023-04-10 21:09:11 +0000 | [diff] [blame] | 659 | ssl->config->tls13_cipher_policy = ctx->tls13_cipher_policy; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 660 | |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 661 | if (!ssl->config->supported_group_list.CopyFrom(ctx->supported_group_list) || |
| 662 | !ssl->config->alpn_client_proto_list.CopyFrom( |
Adam Langley | 826ce15 | 2018-08-03 10:31:21 -0700 | [diff] [blame] | 663 | ctx->alpn_client_proto_list) || |
| 664 | !ssl->config->verify_sigalgs.CopyFrom(ctx->verify_sigalgs)) { |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 665 | return nullptr; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 666 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 667 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 668 | if (ctx->psk_identity_hint) { |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 669 | ssl->config->psk_identity_hint.reset( |
David Benjamin | 3ba9586 | 2019-10-21 16:14:33 -0400 | [diff] [blame] | 670 | OPENSSL_strdup(ctx->psk_identity_hint.get())); |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 671 | if (ssl->config->psk_identity_hint == nullptr) { |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 672 | return nullptr; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 673 | } |
| 674 | } |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 675 | ssl->config->psk_client_callback = ctx->psk_client_callback; |
| 676 | ssl->config->psk_server_callback = ctx->psk_server_callback; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 677 | |
David Benjamin | 4685376 | 2018-07-03 14:01:26 -0400 | [diff] [blame] | 678 | ssl->config->channel_id_enabled = ctx->channel_id_enabled; |
| 679 | ssl->config->channel_id_private = UpRef(ctx->channel_id_private); |
David Benjamin | 02ddbfd | 2015-01-11 13:09:11 -0500 | [diff] [blame] | 680 | |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 681 | ssl->config->signed_cert_timestamps_enabled = |
| 682 | ctx->signed_cert_timestamps_enabled; |
| 683 | ssl->config->ocsp_stapling_enabled = ctx->ocsp_stapling_enabled; |
| 684 | ssl->config->handoff = ctx->handoff; |
Alessandro Ghedini | 3cbb029 | 2018-12-13 13:53:57 +0000 | [diff] [blame] | 685 | ssl->quic_method = ctx->quic_method; |
HÃ¥vard Molland | 9169c96 | 2014-08-14 14:42:37 +0200 | [diff] [blame] | 686 | |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 687 | if (!ssl->method->ssl_new(ssl.get()) || |
| 688 | !ssl->ctx->x509_method->ssl_new(ssl->s3->hs.get())) { |
| 689 | return nullptr; |
| 690 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 691 | |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 692 | return ssl.release(); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 693 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 694 | |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 695 | SSL_CONFIG::SSL_CONFIG(SSL *ssl_arg) |
| 696 | : ssl(ssl_arg), |
Dan McArdle | 1920c6f | 2020-03-11 17:29:40 -0400 | [diff] [blame] | 697 | ech_grease_enabled(false), |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 698 | signed_cert_timestamps_enabled(false), |
| 699 | ocsp_stapling_enabled(false), |
David Benjamin | 4685376 | 2018-07-03 14:01:26 -0400 | [diff] [blame] | 700 | channel_id_enabled(false), |
David Benjamin | cee2dbb | 2023-04-14 16:47:59 -0400 | [diff] [blame] | 701 | enforce_rsa_key_usage(true), |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 702 | retain_only_sha256_of_client_certs(false), |
| 703 | handoff(false), |
David Benjamin | 2d98d49 | 2018-10-08 17:31:17 -0500 | [diff] [blame] | 704 | shed_handshake_config(false), |
David Schinazi | 3d8b8c3 | 2021-01-14 11:25:49 -0800 | [diff] [blame] | 705 | jdk11_workaround(false), |
David Benjamin | e9c5d72 | 2021-06-09 17:43:16 -0400 | [diff] [blame] | 706 | quic_use_legacy_codepoint(false), |
Victor Tan | 558960d | 2023-06-23 15:04:33 +0000 | [diff] [blame] | 707 | permute_extensions(false), |
| 708 | alps_use_new_codepoint(false) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 709 | assert(ssl); |
| 710 | } |
| 711 | |
| 712 | SSL_CONFIG::~SSL_CONFIG() { |
| 713 | if (ssl->ctx != nullptr) { |
| 714 | ssl->ctx->x509_method->ssl_config_free(this); |
| 715 | } |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 716 | } |
| 717 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 718 | void SSL_free(SSL *ssl) { |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 719 | Delete(ssl); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | void SSL_set_connect_state(SSL *ssl) { |
David Benjamin | 7e58c5e | 2017-10-11 13:01:08 -0400 | [diff] [blame] | 723 | ssl->server = false; |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 724 | ssl->do_handshake = ssl_client_handshake; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | void SSL_set_accept_state(SSL *ssl) { |
David Benjamin | 7e58c5e | 2017-10-11 13:01:08 -0400 | [diff] [blame] | 728 | ssl->server = true; |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 729 | ssl->do_handshake = ssl_server_handshake; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 730 | } |
| 731 | |
David Benjamin | 4501bd5 | 2016-08-01 13:39:41 -0400 | [diff] [blame] | 732 | void SSL_set0_rbio(SSL *ssl, BIO *rbio) { |
David Benjamin | 50596f8 | 2018-07-02 19:47:27 -0400 | [diff] [blame] | 733 | ssl->rbio.reset(rbio); |
David Benjamin | f715c42 | 2016-06-11 19:01:56 -0400 | [diff] [blame] | 734 | } |
| 735 | |
David Benjamin | 4501bd5 | 2016-08-01 13:39:41 -0400 | [diff] [blame] | 736 | void SSL_set0_wbio(SSL *ssl, BIO *wbio) { |
David Benjamin | 50596f8 | 2018-07-02 19:47:27 -0400 | [diff] [blame] | 737 | ssl->wbio.reset(wbio); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 738 | } |
| 739 | |
David Benjamin | f715c42 | 2016-06-11 19:01:56 -0400 | [diff] [blame] | 740 | void SSL_set_bio(SSL *ssl, BIO *rbio, BIO *wbio) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 741 | // For historical reasons, this function has many different cases in ownership |
| 742 | // handling. |
David Benjamin | f715c42 | 2016-06-11 19:01:56 -0400 | [diff] [blame] | 743 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 744 | // If nothing has changed, do nothing |
David Benjamin | 4501bd5 | 2016-08-01 13:39:41 -0400 | [diff] [blame] | 745 | if (rbio == SSL_get_rbio(ssl) && wbio == SSL_get_wbio(ssl)) { |
| 746 | return; |
| 747 | } |
| 748 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 749 | // If the two arguments are equal, one fewer reference is granted than |
| 750 | // taken. |
David Benjamin | f715c42 | 2016-06-11 19:01:56 -0400 | [diff] [blame] | 751 | if (rbio != NULL && rbio == wbio) { |
| 752 | BIO_up_ref(rbio); |
| 753 | } |
| 754 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 755 | // If only the wbio is changed, adopt only one reference. |
David Benjamin | f715c42 | 2016-06-11 19:01:56 -0400 | [diff] [blame] | 756 | if (rbio == SSL_get_rbio(ssl)) { |
David Benjamin | 4501bd5 | 2016-08-01 13:39:41 -0400 | [diff] [blame] | 757 | SSL_set0_wbio(ssl, wbio); |
David Benjamin | f715c42 | 2016-06-11 19:01:56 -0400 | [diff] [blame] | 758 | return; |
| 759 | } |
David Benjamin | 4501bd5 | 2016-08-01 13:39:41 -0400 | [diff] [blame] | 760 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 761 | // There is an asymmetry here for historical reasons. If only the rbio is |
| 762 | // changed AND the rbio and wbio were originally different, then we only adopt |
| 763 | // one reference. |
David Benjamin | 4501bd5 | 2016-08-01 13:39:41 -0400 | [diff] [blame] | 764 | if (wbio == SSL_get_wbio(ssl) && SSL_get_rbio(ssl) != SSL_get_wbio(ssl)) { |
| 765 | SSL_set0_rbio(ssl, rbio); |
David Benjamin | f715c42 | 2016-06-11 19:01:56 -0400 | [diff] [blame] | 766 | return; |
| 767 | } |
| 768 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 769 | // Otherwise, adopt both references. |
David Benjamin | 4501bd5 | 2016-08-01 13:39:41 -0400 | [diff] [blame] | 770 | SSL_set0_rbio(ssl, rbio); |
| 771 | SSL_set0_wbio(ssl, wbio); |
David Benjamin | f715c42 | 2016-06-11 19:01:56 -0400 | [diff] [blame] | 772 | } |
| 773 | |
David Benjamin | 50596f8 | 2018-07-02 19:47:27 -0400 | [diff] [blame] | 774 | BIO *SSL_get_rbio(const SSL *ssl) { return ssl->rbio.get(); } |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 775 | |
David Benjamin | 50596f8 | 2018-07-02 19:47:27 -0400 | [diff] [blame] | 776 | BIO *SSL_get_wbio(const SSL *ssl) { return ssl->wbio.get(); } |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 777 | |
Steven Valdez | c8e0f90 | 2018-07-14 11:23:01 -0400 | [diff] [blame] | 778 | size_t SSL_quic_max_handshake_flight_len(const SSL *ssl, |
| 779 | enum ssl_encryption_level_t level) { |
| 780 | // Limits flights to 16K by default when there are no large |
| 781 | // (certificate-carrying) messages. |
| 782 | static const size_t kDefaultLimit = 16384; |
| 783 | |
| 784 | switch (level) { |
| 785 | case ssl_encryption_initial: |
| 786 | return kDefaultLimit; |
| 787 | case ssl_encryption_early_data: |
| 788 | // QUIC does not send EndOfEarlyData. |
| 789 | return 0; |
| 790 | case ssl_encryption_handshake: |
| 791 | if (ssl->server) { |
| 792 | // Servers may receive Certificate message if configured to request |
| 793 | // client certificates. |
| 794 | if (!!(ssl->config->verify_mode & SSL_VERIFY_PEER) && |
| 795 | ssl->max_cert_list > kDefaultLimit) { |
| 796 | return ssl->max_cert_list; |
| 797 | } |
| 798 | } else { |
| 799 | // Clients may receive both Certificate message and a CertificateRequest |
| 800 | // message. |
| 801 | if (2*ssl->max_cert_list > kDefaultLimit) { |
| 802 | return 2*ssl->max_cert_list; |
| 803 | } |
| 804 | } |
| 805 | return kDefaultLimit; |
| 806 | case ssl_encryption_application: |
| 807 | // Note there is not actually a bound on the number of NewSessionTickets |
| 808 | // one may send in a row. This level may need more involved flow |
| 809 | // control. See https://github.com/quicwg/base-drafts/issues/1834. |
| 810 | return kDefaultLimit; |
| 811 | } |
| 812 | |
| 813 | return 0; |
| 814 | } |
| 815 | |
| 816 | enum ssl_encryption_level_t SSL_quic_read_level(const SSL *ssl) { |
| 817 | return ssl->s3->read_level; |
| 818 | } |
| 819 | |
| 820 | enum ssl_encryption_level_t SSL_quic_write_level(const SSL *ssl) { |
| 821 | return ssl->s3->write_level; |
| 822 | } |
| 823 | |
| 824 | int SSL_provide_quic_data(SSL *ssl, enum ssl_encryption_level_t level, |
| 825 | const uint8_t *data, size_t len) { |
Alessandro Ghedini | 3cbb029 | 2018-12-13 13:53:57 +0000 | [diff] [blame] | 826 | if (ssl->quic_method == nullptr) { |
Steven Valdez | c8e0f90 | 2018-07-14 11:23:01 -0400 | [diff] [blame] | 827 | OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 828 | return 0; |
| 829 | } |
| 830 | |
| 831 | if (level != ssl->s3->read_level) { |
| 832 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_ENCRYPTION_LEVEL_RECEIVED); |
| 833 | return 0; |
| 834 | } |
| 835 | |
| 836 | size_t new_len = (ssl->s3->hs_buf ? ssl->s3->hs_buf->length : 0) + len; |
| 837 | if (new_len < len || |
| 838 | new_len > SSL_quic_max_handshake_flight_len(ssl, level)) { |
| 839 | OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESSIVE_MESSAGE_SIZE); |
| 840 | return 0; |
| 841 | } |
| 842 | |
David Benjamin | 7d10ab5 | 2018-11-02 18:31:17 -0500 | [diff] [blame] | 843 | return tls_append_handshake_data(ssl, MakeConstSpan(data, len)); |
Steven Valdez | c8e0f90 | 2018-07-14 11:23:01 -0400 | [diff] [blame] | 844 | } |
| 845 | |
David Benjamin | 2be4aa7 | 2017-01-02 07:56:51 -0500 | [diff] [blame] | 846 | int SSL_do_handshake(SSL *ssl) { |
| 847 | ssl_reset_error_state(ssl); |
David Benjamin | 15c1488 | 2016-03-14 14:25:46 -0400 | [diff] [blame] | 848 | |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 849 | if (ssl->do_handshake == NULL) { |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 850 | OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_TYPE_NOT_SET); |
| 851 | return -1; |
| 852 | } |
| 853 | |
| 854 | if (!SSL_in_init(ssl)) { |
| 855 | return 1; |
| 856 | } |
| 857 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 858 | // Run the handshake. |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 859 | SSL_HANDSHAKE *hs = ssl->s3->hs.get(); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 860 | |
David Benjamin | 74795b3 | 2017-08-31 15:13:12 -0400 | [diff] [blame] | 861 | bool early_return = false; |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 862 | int ret = ssl_run_handshake(hs, &early_return); |
| 863 | ssl_do_info_callback( |
| 864 | ssl, ssl->server ? SSL_CB_ACCEPT_EXIT : SSL_CB_CONNECT_EXIT, ret); |
David Benjamin | ce8c9d2 | 2016-11-14 10:45:16 +0900 | [diff] [blame] | 865 | if (ret <= 0) { |
| 866 | return ret; |
| 867 | } |
| 868 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 869 | // Destroy the handshake object if the handshake has completely finished. |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 870 | if (!early_return) { |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 871 | ssl->s3->hs.reset(); |
David Benjamin | 71666cb | 2018-05-01 17:14:27 -0400 | [diff] [blame] | 872 | ssl_maybe_shed_handshake_config(ssl); |
David Benjamin | ce8c9d2 | 2016-11-14 10:45:16 +0900 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | return 1; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | int SSL_connect(SSL *ssl) { |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 879 | if (ssl->do_handshake == NULL) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 880 | // Not properly initialized yet |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 881 | SSL_set_connect_state(ssl); |
| 882 | } |
| 883 | |
David Benjamin | 15c1488 | 2016-03-14 14:25:46 -0400 | [diff] [blame] | 884 | return SSL_do_handshake(ssl); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | int SSL_accept(SSL *ssl) { |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 888 | if (ssl->do_handshake == NULL) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 889 | // Not properly initialized yet |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 890 | SSL_set_accept_state(ssl); |
| 891 | } |
| 892 | |
David Benjamin | 15c1488 | 2016-03-14 14:25:46 -0400 | [diff] [blame] | 893 | return SSL_do_handshake(ssl); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 894 | } |
| 895 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 896 | static int ssl_do_post_handshake(SSL *ssl, const SSLMessage &msg) { |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 897 | if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 898 | return tls13_post_handshake(ssl, msg); |
| 899 | } |
| 900 | |
David Benjamin | 71666cb | 2018-05-01 17:14:27 -0400 | [diff] [blame] | 901 | // Check for renegotiation on the server before parsing to use the correct |
| 902 | // error. Renegotiation is triggered by a different message for servers. |
| 903 | if (ssl->server) { |
| 904 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION); |
| 905 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_NO_RENEGOTIATION); |
| 906 | return 0; |
David Benjamin | 163f29a | 2016-07-28 11:05:58 -0400 | [diff] [blame] | 907 | } |
| 908 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 909 | if (msg.type != SSL3_MT_HELLO_REQUEST || CBS_len(&msg.body) != 0) { |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 910 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 163f29a | 2016-07-28 11:05:58 -0400 | [diff] [blame] | 911 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HELLO_REQUEST); |
| 912 | return 0; |
| 913 | } |
| 914 | |
David Benjamin | 71666cb | 2018-05-01 17:14:27 -0400 | [diff] [blame] | 915 | if (ssl->renegotiate_mode == ssl_renegotiate_ignore) { |
| 916 | return 1; // Ignore the HelloRequest. |
David Benjamin | 163f29a | 2016-07-28 11:05:58 -0400 | [diff] [blame] | 917 | } |
| 918 | |
David Benjamin | 0e7dbd5 | 2019-05-15 16:01:18 -0400 | [diff] [blame] | 919 | ssl->s3->renegotiate_pending = true; |
| 920 | if (ssl->renegotiate_mode == ssl_renegotiate_explicit) { |
| 921 | return 1; // Handle it later. |
| 922 | } |
| 923 | |
| 924 | if (!SSL_renegotiate(ssl)) { |
David Benjamin | 71666cb | 2018-05-01 17:14:27 -0400 | [diff] [blame] | 925 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_NO_RENEGOTIATION); |
| 926 | return 0; |
David Benjamin | 163f29a | 2016-07-28 11:05:58 -0400 | [diff] [blame] | 927 | } |
| 928 | |
David Benjamin | 163f29a | 2016-07-28 11:05:58 -0400 | [diff] [blame] | 929 | return 1; |
David Benjamin | 163f29a | 2016-07-28 11:05:58 -0400 | [diff] [blame] | 930 | } |
| 931 | |
Steven Valdez | e6eef1c | 2018-11-09 13:32:34 -0500 | [diff] [blame] | 932 | int SSL_process_quic_post_handshake(SSL *ssl) { |
| 933 | ssl_reset_error_state(ssl); |
| 934 | |
| 935 | if (SSL_in_init(ssl)) { |
| 936 | OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 937 | return 0; |
| 938 | } |
| 939 | |
| 940 | // Replay post-handshake message errors. |
| 941 | if (!check_read_error(ssl)) { |
| 942 | return 0; |
| 943 | } |
| 944 | |
| 945 | // Process any buffered post-handshake messages. |
| 946 | SSLMessage msg; |
| 947 | while (ssl->method->get_message(ssl, &msg)) { |
| 948 | // Handle the post-handshake message and try again. |
| 949 | if (!ssl_do_post_handshake(ssl, msg)) { |
| 950 | ssl_set_read_error(ssl); |
| 951 | return 0; |
| 952 | } |
| 953 | ssl->method->next_message(ssl); |
| 954 | } |
| 955 | |
| 956 | return 1; |
| 957 | } |
| 958 | |
David Benjamin | a031b61 | 2017-10-11 20:48:25 -0400 | [diff] [blame] | 959 | static int ssl_read_impl(SSL *ssl) { |
David Benjamin | 2be4aa7 | 2017-01-02 07:56:51 -0500 | [diff] [blame] | 960 | ssl_reset_error_state(ssl); |
David Benjamin | 15c1488 | 2016-03-14 14:25:46 -0400 | [diff] [blame] | 961 | |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 962 | if (ssl->do_handshake == NULL) { |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 963 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED); |
| 964 | return -1; |
| 965 | } |
| 966 | |
David Benjamin | f6632da | 2017-10-12 19:11:47 -0400 | [diff] [blame] | 967 | // Replay post-handshake message errors. |
| 968 | if (!check_read_error(ssl)) { |
| 969 | return -1; |
| 970 | } |
| 971 | |
David Benjamin | a031b61 | 2017-10-11 20:48:25 -0400 | [diff] [blame] | 972 | while (ssl->s3->pending_app_data.empty()) { |
David Benjamin | 0e7dbd5 | 2019-05-15 16:01:18 -0400 | [diff] [blame] | 973 | if (ssl->s3->renegotiate_pending) { |
| 974 | ssl->s3->rwstate = SSL_ERROR_WANT_RENEGOTIATE; |
| 975 | return -1; |
| 976 | } |
| 977 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 978 | // Complete the current handshake, if any. False Start will cause |
| 979 | // |SSL_do_handshake| to return mid-handshake, so this may require multiple |
| 980 | // iterations. |
Steven Valdez | 681eb6a | 2016-12-19 13:19:29 -0500 | [diff] [blame] | 981 | while (!ssl_can_read(ssl)) { |
David Benjamin | 163f29a | 2016-07-28 11:05:58 -0400 | [diff] [blame] | 982 | int ret = SSL_do_handshake(ssl); |
| 983 | if (ret < 0) { |
| 984 | return ret; |
| 985 | } |
| 986 | if (ret == 0) { |
| 987 | OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 988 | return -1; |
| 989 | } |
| 990 | } |
| 991 | |
David Benjamin | 40e9470 | 2017-10-06 18:26:36 -0400 | [diff] [blame] | 992 | // Process any buffered post-handshake messages. |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 993 | SSLMessage msg; |
David Benjamin | 40e9470 | 2017-10-06 18:26:36 -0400 | [diff] [blame] | 994 | if (ssl->method->get_message(ssl, &msg)) { |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 995 | // If we received an interrupt in early read (EndOfEarlyData), loop again |
| 996 | // for the handshake to process it. |
| 997 | if (SSL_in_init(ssl)) { |
| 998 | ssl->s3->hs->can_early_read = false; |
| 999 | continue; |
| 1000 | } |
| 1001 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1002 | // Handle the post-handshake message and try again. |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1003 | if (!ssl_do_post_handshake(ssl, msg)) { |
David Benjamin | f6632da | 2017-10-12 19:11:47 -0400 | [diff] [blame] | 1004 | ssl_set_read_error(ssl); |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1005 | return -1; |
| 1006 | } |
| 1007 | ssl->method->next_message(ssl); |
David Benjamin | 40e9470 | 2017-10-06 18:26:36 -0400 | [diff] [blame] | 1008 | continue; // Loop again. We may have begun a new handshake. |
David Benjamin | c79845c | 2016-03-10 01:28:00 -0500 | [diff] [blame] | 1009 | } |
David Benjamin | 40e9470 | 2017-10-06 18:26:36 -0400 | [diff] [blame] | 1010 | |
David Benjamin | a031b61 | 2017-10-11 20:48:25 -0400 | [diff] [blame] | 1011 | uint8_t alert = SSL_AD_DECODE_ERROR; |
| 1012 | size_t consumed = 0; |
David Benjamin | f6632da | 2017-10-12 19:11:47 -0400 | [diff] [blame] | 1013 | auto ret = ssl_open_app_data(ssl, &ssl->s3->pending_app_data, &consumed, |
David Benjamin | ea712e3 | 2017-10-13 16:50:39 -0400 | [diff] [blame] | 1014 | &alert, ssl->s3->read_buffer.span()); |
David Benjamin | a031b61 | 2017-10-11 20:48:25 -0400 | [diff] [blame] | 1015 | bool retry; |
| 1016 | int bio_ret = ssl_handle_open_record(ssl, &retry, ret, consumed, alert); |
| 1017 | if (bio_ret <= 0) { |
| 1018 | return bio_ret; |
| 1019 | } |
| 1020 | if (!retry) { |
| 1021 | assert(!ssl->s3->pending_app_data.empty()); |
David Benjamin | 40e9470 | 2017-10-06 18:26:36 -0400 | [diff] [blame] | 1022 | ssl->s3->key_update_count = 0; |
| 1023 | } |
David Benjamin | c79845c | 2016-03-10 01:28:00 -0500 | [diff] [blame] | 1024 | } |
David Benjamin | a031b61 | 2017-10-11 20:48:25 -0400 | [diff] [blame] | 1025 | |
| 1026 | return 1; |
David Benjamin | 15c1488 | 2016-03-14 14:25:46 -0400 | [diff] [blame] | 1027 | } |
| 1028 | |
| 1029 | int SSL_read(SSL *ssl, void *buf, int num) { |
David Benjamin | a031b61 | 2017-10-11 20:48:25 -0400 | [diff] [blame] | 1030 | int ret = SSL_peek(ssl, buf, num); |
| 1031 | if (ret <= 0) { |
| 1032 | return ret; |
| 1033 | } |
| 1034 | // TODO(davidben): In DTLS, should the rest of the record be discarded? DTLS |
| 1035 | // is not a stream. See https://crbug.com/boringssl/65. |
| 1036 | ssl->s3->pending_app_data = |
| 1037 | ssl->s3->pending_app_data.subspan(static_cast<size_t>(ret)); |
| 1038 | if (ssl->s3->pending_app_data.empty()) { |
David Benjamin | ea712e3 | 2017-10-13 16:50:39 -0400 | [diff] [blame] | 1039 | ssl->s3->read_buffer.DiscardConsumed(); |
David Benjamin | a031b61 | 2017-10-11 20:48:25 -0400 | [diff] [blame] | 1040 | } |
| 1041 | return ret; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | int SSL_peek(SSL *ssl, void *buf, int num) { |
Alessandro Ghedini | 3cbb029 | 2018-12-13 13:53:57 +0000 | [diff] [blame] | 1045 | if (ssl->quic_method != nullptr) { |
Steven Valdez | c8e0f90 | 2018-07-14 11:23:01 -0400 | [diff] [blame] | 1046 | OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
David Benjamin | 69ec7c8 | 2021-08-11 17:18:00 -0400 | [diff] [blame] | 1047 | return -1; |
Steven Valdez | c8e0f90 | 2018-07-14 11:23:01 -0400 | [diff] [blame] | 1048 | } |
| 1049 | |
David Benjamin | a031b61 | 2017-10-11 20:48:25 -0400 | [diff] [blame] | 1050 | int ret = ssl_read_impl(ssl); |
| 1051 | if (ret <= 0) { |
| 1052 | return ret; |
| 1053 | } |
| 1054 | if (num <= 0) { |
| 1055 | return num; |
| 1056 | } |
| 1057 | size_t todo = |
| 1058 | std::min(ssl->s3->pending_app_data.size(), static_cast<size_t>(num)); |
| 1059 | OPENSSL_memcpy(buf, ssl->s3->pending_app_data.data(), todo); |
| 1060 | return static_cast<int>(todo); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1061 | } |
| 1062 | |
| 1063 | int SSL_write(SSL *ssl, const void *buf, int num) { |
David Benjamin | 2be4aa7 | 2017-01-02 07:56:51 -0500 | [diff] [blame] | 1064 | ssl_reset_error_state(ssl); |
David Benjamin | 15c1488 | 2016-03-14 14:25:46 -0400 | [diff] [blame] | 1065 | |
Alessandro Ghedini | 3cbb029 | 2018-12-13 13:53:57 +0000 | [diff] [blame] | 1066 | if (ssl->quic_method != nullptr) { |
Steven Valdez | c8e0f90 | 2018-07-14 11:23:01 -0400 | [diff] [blame] | 1067 | OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
David Benjamin | 69ec7c8 | 2021-08-11 17:18:00 -0400 | [diff] [blame] | 1068 | return -1; |
Steven Valdez | c8e0f90 | 2018-07-14 11:23:01 -0400 | [diff] [blame] | 1069 | } |
| 1070 | |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1071 | if (ssl->do_handshake == NULL) { |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1072 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED); |
| 1073 | return -1; |
| 1074 | } |
| 1075 | |
David Benjamin | 74795b3 | 2017-08-31 15:13:12 -0400 | [diff] [blame] | 1076 | int ret = 0; |
David Benjamin | 4bd32a8 | 2021-05-14 17:06:29 -0400 | [diff] [blame] | 1077 | size_t bytes_written = 0; |
David Benjamin | 74795b3 | 2017-08-31 15:13:12 -0400 | [diff] [blame] | 1078 | bool needs_handshake = false; |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 1079 | do { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1080 | // If necessary, complete the handshake implicitly. |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 1081 | if (!ssl_can_write(ssl)) { |
| 1082 | ret = SSL_do_handshake(ssl); |
| 1083 | if (ret < 0) { |
| 1084 | return ret; |
| 1085 | } |
| 1086 | if (ret == 0) { |
| 1087 | OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 1088 | return -1; |
| 1089 | } |
David Benjamin | d7ac143 | 2016-03-10 00:41:25 -0500 | [diff] [blame] | 1090 | } |
David Benjamin | d7ac143 | 2016-03-10 00:41:25 -0500 | [diff] [blame] | 1091 | |
David Benjamin | 4bd32a8 | 2021-05-14 17:06:29 -0400 | [diff] [blame] | 1092 | if (num < 0) { |
| 1093 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_LENGTH); |
| 1094 | return -1; |
| 1095 | } |
| 1096 | ret = ssl->method->write_app_data( |
| 1097 | ssl, &needs_handshake, &bytes_written, |
| 1098 | MakeConstSpan(static_cast<const uint8_t *>(buf), |
| 1099 | static_cast<size_t>(num))); |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 1100 | } while (needs_handshake); |
David Benjamin | 4bd32a8 | 2021-05-14 17:06:29 -0400 | [diff] [blame] | 1101 | return ret <= 0 ? ret : static_cast<int>(bytes_written); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1102 | } |
| 1103 | |
Adam Langley | ba9ad66 | 2018-12-17 13:59:38 -0800 | [diff] [blame] | 1104 | int SSL_key_update(SSL *ssl, int request_type) { |
| 1105 | ssl_reset_error_state(ssl); |
| 1106 | |
| 1107 | if (ssl->do_handshake == NULL) { |
| 1108 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED); |
| 1109 | return 0; |
| 1110 | } |
| 1111 | |
| 1112 | if (ssl->ctx->quic_method != nullptr) { |
| 1113 | OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 1114 | return 0; |
| 1115 | } |
| 1116 | |
| 1117 | if (!ssl->s3->initial_handshake_complete) { |
| 1118 | OPENSSL_PUT_ERROR(SSL, SSL_R_HANDSHAKE_NOT_COMPLETE); |
| 1119 | return 0; |
| 1120 | } |
| 1121 | |
| 1122 | if (ssl_protocol_version(ssl) < TLS1_3_VERSION) { |
| 1123 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION); |
| 1124 | return 0; |
| 1125 | } |
| 1126 | |
| 1127 | if (!ssl->s3->key_update_pending && |
| 1128 | !tls13_add_key_update(ssl, request_type)) { |
| 1129 | return 0; |
| 1130 | } |
| 1131 | |
| 1132 | return 1; |
| 1133 | } |
| 1134 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1135 | int SSL_shutdown(SSL *ssl) { |
David Benjamin | 2be4aa7 | 2017-01-02 07:56:51 -0500 | [diff] [blame] | 1136 | ssl_reset_error_state(ssl); |
David Benjamin | 15c1488 | 2016-03-14 14:25:46 -0400 | [diff] [blame] | 1137 | |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1138 | if (ssl->do_handshake == NULL) { |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1139 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED); |
| 1140 | return -1; |
| 1141 | } |
| 1142 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1143 | // If we are in the middle of a handshake, silently succeed. Consumers often |
| 1144 | // call this function before |SSL_free|, whether the handshake succeeded or |
| 1145 | // not. We assume the caller has already handled failed handshakes. |
Steven Valdez | a14934f | 2016-02-29 10:05:08 -0500 | [diff] [blame] | 1146 | if (SSL_in_init(ssl)) { |
David Benjamin | 12709db | 2017-02-17 13:51:50 -0500 | [diff] [blame] | 1147 | return 1; |
Steven Valdez | a14934f | 2016-02-29 10:05:08 -0500 | [diff] [blame] | 1148 | } |
| 1149 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1150 | if (ssl->quiet_shutdown) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1151 | // Do nothing if configured not to send a close_notify. |
David Benjamin | 23c25d5 | 2017-10-06 16:02:47 -0400 | [diff] [blame] | 1152 | ssl->s3->write_shutdown = ssl_shutdown_close_notify; |
| 1153 | ssl->s3->read_shutdown = ssl_shutdown_close_notify; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1154 | return 1; |
| 1155 | } |
| 1156 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1157 | // This function completes in two stages. It sends a close_notify and then it |
| 1158 | // waits for a close_notify to come in. Perform exactly one action and return |
| 1159 | // whether or not it succeeds. |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1160 | |
David Benjamin | 23c25d5 | 2017-10-06 16:02:47 -0400 | [diff] [blame] | 1161 | if (ssl->s3->write_shutdown != ssl_shutdown_close_notify) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1162 | // Send a close_notify. |
David Benjamin | b29e1e1 | 2019-05-06 14:44:46 -0500 | [diff] [blame] | 1163 | if (ssl_send_alert_impl(ssl, SSL3_AL_WARNING, SSL_AD_CLOSE_NOTIFY) <= 0) { |
David Benjamin | fa214e4 | 2016-05-10 17:03:10 -0400 | [diff] [blame] | 1164 | return -1; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1165 | } |
| 1166 | } else if (ssl->s3->alert_dispatch) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1167 | // Finish sending the close_notify. |
David Benjamin | f0ee907 | 2016-06-15 17:44:37 -0400 | [diff] [blame] | 1168 | if (ssl->method->dispatch_alert(ssl) <= 0) { |
David Benjamin | fa214e4 | 2016-05-10 17:03:10 -0400 | [diff] [blame] | 1169 | return -1; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1170 | } |
David Benjamin | 23c25d5 | 2017-10-06 16:02:47 -0400 | [diff] [blame] | 1171 | } else if (ssl->s3->read_shutdown != ssl_shutdown_close_notify) { |
David Benjamin | a031b61 | 2017-10-11 20:48:25 -0400 | [diff] [blame] | 1172 | if (SSL_is_dtls(ssl)) { |
| 1173 | // Bidirectional shutdown doesn't make sense for an unordered |
| 1174 | // transport. DTLS alerts also aren't delivered reliably, so we may even |
| 1175 | // time out because the peer never received our close_notify. Report to |
| 1176 | // the caller that the channel has fully shut down. |
| 1177 | if (ssl->s3->read_shutdown == ssl_shutdown_error) { |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 1178 | ERR_restore_state(ssl->s3->read_error.get()); |
David Benjamin | a031b61 | 2017-10-11 20:48:25 -0400 | [diff] [blame] | 1179 | return -1; |
David Benjamin | d9229f9 | 2017-10-06 17:36:20 -0400 | [diff] [blame] | 1180 | } |
David Benjamin | a031b61 | 2017-10-11 20:48:25 -0400 | [diff] [blame] | 1181 | ssl->s3->read_shutdown = ssl_shutdown_close_notify; |
| 1182 | } else { |
David Benjamin | 5267ef7 | 2018-05-30 10:18:09 -0400 | [diff] [blame] | 1183 | // Process records until an error, close_notify, or application data. |
| 1184 | if (ssl_read_impl(ssl) > 0) { |
| 1185 | // We received some unexpected application data. |
| 1186 | OPENSSL_PUT_ERROR(SSL, SSL_R_APPLICATION_DATA_ON_SHUTDOWN); |
| 1187 | return -1; |
David Benjamin | a031b61 | 2017-10-11 20:48:25 -0400 | [diff] [blame] | 1188 | } |
| 1189 | if (ssl->s3->read_shutdown != ssl_shutdown_close_notify) { |
| 1190 | return -1; |
| 1191 | } |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1192 | } |
| 1193 | } |
| 1194 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1195 | // Return 0 for unidirectional shutdown and 1 for bidirectional shutdown. |
David Benjamin | 23c25d5 | 2017-10-06 16:02:47 -0400 | [diff] [blame] | 1196 | return ssl->s3->read_shutdown == ssl_shutdown_close_notify; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1197 | } |
| 1198 | |
David Benjamin | 1d4f4c0 | 2016-07-26 18:03:08 -0400 | [diff] [blame] | 1199 | int SSL_send_fatal_alert(SSL *ssl, uint8_t alert) { |
| 1200 | if (ssl->s3->alert_dispatch) { |
| 1201 | if (ssl->s3->send_alert[0] != SSL3_AL_FATAL || |
| 1202 | ssl->s3->send_alert[1] != alert) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1203 | // We are already attempting to write a different alert. |
David Benjamin | 1d4f4c0 | 2016-07-26 18:03:08 -0400 | [diff] [blame] | 1204 | OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN); |
| 1205 | return -1; |
| 1206 | } |
| 1207 | return ssl->method->dispatch_alert(ssl); |
| 1208 | } |
| 1209 | |
David Benjamin | b29e1e1 | 2019-05-06 14:44:46 -0500 | [diff] [blame] | 1210 | return ssl_send_alert_impl(ssl, SSL3_AL_FATAL, alert); |
David Benjamin | 1d4f4c0 | 2016-07-26 18:03:08 -0400 | [diff] [blame] | 1211 | } |
| 1212 | |
Nick Harper | 3c034b2 | 2017-12-22 15:50:43 -0800 | [diff] [blame] | 1213 | int SSL_set_quic_transport_params(SSL *ssl, const uint8_t *params, |
| 1214 | size_t params_len) { |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 1215 | return ssl->config && ssl->config->quic_transport_params.CopyFrom( |
| 1216 | MakeConstSpan(params, params_len)); |
Nick Harper | 3c034b2 | 2017-12-22 15:50:43 -0800 | [diff] [blame] | 1217 | } |
| 1218 | |
| 1219 | void SSL_get_peer_quic_transport_params(const SSL *ssl, |
| 1220 | const uint8_t **out_params, |
| 1221 | size_t *out_params_len) { |
| 1222 | *out_params = ssl->s3->peer_quic_transport_params.data(); |
| 1223 | *out_params_len = ssl->s3->peer_quic_transport_params.size(); |
| 1224 | } |
| 1225 | |
Nick Harper | 7c52299 | 2020-04-30 14:15:49 -0700 | [diff] [blame] | 1226 | int SSL_set_quic_early_data_context(SSL *ssl, const uint8_t *context, |
| 1227 | size_t context_len) { |
| 1228 | return ssl->config && ssl->config->quic_early_data_context.CopyFrom( |
| 1229 | MakeConstSpan(context, context_len)); |
| 1230 | } |
| 1231 | |
Steven Valdez | 08b65f4 | 2016-12-07 15:29:45 -0500 | [diff] [blame] | 1232 | void SSL_CTX_set_early_data_enabled(SSL_CTX *ctx, int enabled) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 1233 | ctx->enable_early_data = !!enabled; |
Alessandro Ghedini | 67bb45f | 2017-03-30 16:33:24 -0500 | [diff] [blame] | 1234 | } |
| 1235 | |
| 1236 | void SSL_set_early_data_enabled(SSL *ssl, int enabled) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 1237 | ssl->enable_early_data = !!enabled; |
Steven Valdez | 08b65f4 | 2016-12-07 15:29:45 -0500 | [diff] [blame] | 1238 | } |
| 1239 | |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 1240 | int SSL_in_early_data(const SSL *ssl) { |
| 1241 | if (ssl->s3->hs == NULL) { |
| 1242 | return 0; |
| 1243 | } |
| 1244 | return ssl->s3->hs->in_early_data; |
| 1245 | } |
| 1246 | |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 1247 | int SSL_early_data_accepted(const SSL *ssl) { |
David Benjamin | 02e6256 | 2017-12-18 18:04:01 -0500 | [diff] [blame] | 1248 | return ssl->s3->early_data_accepted; |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 1249 | } |
| 1250 | |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 1251 | void SSL_reset_early_data_reject(SSL *ssl) { |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 1252 | SSL_HANDSHAKE *hs = ssl->s3->hs.get(); |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 1253 | if (hs == NULL || |
| 1254 | hs->wait != ssl_hs_early_data_rejected) { |
| 1255 | abort(); |
| 1256 | } |
| 1257 | |
| 1258 | hs->wait = ssl_hs_ok; |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 1259 | hs->in_early_data = false; |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 1260 | hs->early_session.reset(); |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 1261 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1262 | // Discard any unfinished writes from the perspective of |SSL_write|'s |
| 1263 | // retry. The handshake will transparently flush out the pending record |
| 1264 | // (discarded by the server) to keep the framing correct. |
David Benjamin | 4bd32a8 | 2021-05-14 17:06:29 -0400 | [diff] [blame] | 1265 | ssl->s3->pending_write = {}; |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 1266 | } |
| 1267 | |
David Benjamin | 6477012 | 2019-05-04 11:00:04 -0500 | [diff] [blame] | 1268 | enum ssl_early_data_reason_t SSL_get_early_data_reason(const SSL *ssl) { |
| 1269 | return ssl->s3->early_data_reason; |
| 1270 | } |
| 1271 | |
David Benjamin | fa9796e | 2020-11-02 13:01:04 -0500 | [diff] [blame] | 1272 | const char *SSL_early_data_reason_string(enum ssl_early_data_reason_t reason) { |
| 1273 | switch (reason) { |
| 1274 | case ssl_early_data_unknown: |
| 1275 | return "unknown"; |
| 1276 | case ssl_early_data_disabled: |
| 1277 | return "disabled"; |
| 1278 | case ssl_early_data_accepted: |
| 1279 | return "accepted"; |
| 1280 | case ssl_early_data_protocol_version: |
| 1281 | return "protocol_version"; |
| 1282 | case ssl_early_data_peer_declined: |
| 1283 | return "peer_declined"; |
| 1284 | case ssl_early_data_no_session_offered: |
| 1285 | return "no_session_offered"; |
| 1286 | case ssl_early_data_session_not_resumed: |
| 1287 | return "session_not_resumed"; |
| 1288 | case ssl_early_data_unsupported_for_session: |
| 1289 | return "unsupported_for_session"; |
| 1290 | case ssl_early_data_hello_retry_request: |
| 1291 | return "hello_retry_request"; |
| 1292 | case ssl_early_data_alpn_mismatch: |
| 1293 | return "alpn_mismatch"; |
| 1294 | case ssl_early_data_channel_id: |
| 1295 | return "channel_id"; |
David Benjamin | fa9796e | 2020-11-02 13:01:04 -0500 | [diff] [blame] | 1296 | case ssl_early_data_ticket_age_skew: |
| 1297 | return "ticket_age_skew"; |
| 1298 | case ssl_early_data_quic_parameter_mismatch: |
| 1299 | return "quic_parameter_mismatch"; |
| 1300 | case ssl_early_data_alps_mismatch: |
| 1301 | return "alps_mismatch"; |
| 1302 | } |
| 1303 | |
| 1304 | return nullptr; |
| 1305 | } |
| 1306 | |
David Benjamin | 287fc4f | 2016-12-10 15:12:03 -0500 | [diff] [blame] | 1307 | static int bio_retry_reason_to_error(int reason) { |
| 1308 | switch (reason) { |
| 1309 | case BIO_RR_CONNECT: |
| 1310 | return SSL_ERROR_WANT_CONNECT; |
| 1311 | case BIO_RR_ACCEPT: |
| 1312 | return SSL_ERROR_WANT_ACCEPT; |
| 1313 | default: |
| 1314 | return SSL_ERROR_SYSCALL; |
| 1315 | } |
| 1316 | } |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1317 | |
David Benjamin | 287fc4f | 2016-12-10 15:12:03 -0500 | [diff] [blame] | 1318 | int SSL_get_error(const SSL *ssl, int ret_code) { |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1319 | if (ret_code > 0) { |
| 1320 | return SSL_ERROR_NONE; |
| 1321 | } |
| 1322 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1323 | // Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc, |
| 1324 | // where we do encode the error |
David Benjamin | 287fc4f | 2016-12-10 15:12:03 -0500 | [diff] [blame] | 1325 | uint32_t err = ERR_peek_error(); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1326 | if (err != 0) { |
| 1327 | if (ERR_GET_LIB(err) == ERR_LIB_SYS) { |
| 1328 | return SSL_ERROR_SYSCALL; |
| 1329 | } |
| 1330 | return SSL_ERROR_SSL; |
| 1331 | } |
| 1332 | |
| 1333 | if (ret_code == 0) { |
David Benjamin | ebd8b89 | 2022-07-27 18:41:51 -0700 | [diff] [blame] | 1334 | if (ssl->s3->rwstate == SSL_ERROR_ZERO_RETURN) { |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1335 | return SSL_ERROR_ZERO_RETURN; |
| 1336 | } |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1337 | // An EOF was observed which violates the protocol, and the underlying |
| 1338 | // transport does not participate in the error queue. Bubble up to the |
| 1339 | // caller. |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1340 | return SSL_ERROR_SYSCALL; |
| 1341 | } |
| 1342 | |
David Benjamin | 32ce0ac | 2017-10-13 19:17:22 -0400 | [diff] [blame] | 1343 | switch (ssl->s3->rwstate) { |
David Benjamin | f492830 | 2019-08-21 16:04:53 -0400 | [diff] [blame] | 1344 | case SSL_ERROR_PENDING_SESSION: |
| 1345 | case SSL_ERROR_PENDING_CERTIFICATE: |
| 1346 | case SSL_ERROR_HANDOFF: |
| 1347 | case SSL_ERROR_HANDBACK: |
| 1348 | case SSL_ERROR_WANT_X509_LOOKUP: |
David Benjamin | f492830 | 2019-08-21 16:04:53 -0400 | [diff] [blame] | 1349 | case SSL_ERROR_WANT_PRIVATE_KEY_OPERATION: |
| 1350 | case SSL_ERROR_PENDING_TICKET: |
| 1351 | case SSL_ERROR_EARLY_DATA_REJECTED: |
| 1352 | case SSL_ERROR_WANT_CERTIFICATE_VERIFY: |
David Benjamin | 0e7dbd5 | 2019-05-15 16:01:18 -0400 | [diff] [blame] | 1353 | case SSL_ERROR_WANT_RENEGOTIATE: |
David Benjamin | b571e77 | 2021-03-25 19:42:16 -0400 | [diff] [blame] | 1354 | case SSL_ERROR_HANDSHAKE_HINTS_READY: |
David Benjamin | f492830 | 2019-08-21 16:04:53 -0400 | [diff] [blame] | 1355 | return ssl->s3->rwstate; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1356 | |
David Benjamin | f492830 | 2019-08-21 16:04:53 -0400 | [diff] [blame] | 1357 | case SSL_ERROR_WANT_READ: { |
Alessandro Ghedini | 3cbb029 | 2018-12-13 13:53:57 +0000 | [diff] [blame] | 1358 | if (ssl->quic_method) { |
Steven Valdez | c8e0f90 | 2018-07-14 11:23:01 -0400 | [diff] [blame] | 1359 | return SSL_ERROR_WANT_READ; |
| 1360 | } |
David Benjamin | 287fc4f | 2016-12-10 15:12:03 -0500 | [diff] [blame] | 1361 | BIO *bio = SSL_get_rbio(ssl); |
| 1362 | if (BIO_should_read(bio)) { |
| 1363 | return SSL_ERROR_WANT_READ; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1364 | } |
| 1365 | |
David Benjamin | 287fc4f | 2016-12-10 15:12:03 -0500 | [diff] [blame] | 1366 | if (BIO_should_write(bio)) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1367 | // TODO(davidben): OpenSSL historically checked for writes on the read |
| 1368 | // BIO. Can this be removed? |
David Benjamin | 287fc4f | 2016-12-10 15:12:03 -0500 | [diff] [blame] | 1369 | return SSL_ERROR_WANT_WRITE; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1370 | } |
| 1371 | |
David Benjamin | 287fc4f | 2016-12-10 15:12:03 -0500 | [diff] [blame] | 1372 | if (BIO_should_io_special(bio)) { |
| 1373 | return bio_retry_reason_to_error(BIO_get_retry_reason(bio)); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1374 | } |
| 1375 | |
David Benjamin | 287fc4f | 2016-12-10 15:12:03 -0500 | [diff] [blame] | 1376 | break; |
| 1377 | } |
| 1378 | |
David Benjamin | f492830 | 2019-08-21 16:04:53 -0400 | [diff] [blame] | 1379 | case SSL_ERROR_WANT_WRITE: { |
David Benjamin | 287fc4f | 2016-12-10 15:12:03 -0500 | [diff] [blame] | 1380 | BIO *bio = SSL_get_wbio(ssl); |
| 1381 | if (BIO_should_write(bio)) { |
| 1382 | return SSL_ERROR_WANT_WRITE; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1383 | } |
| 1384 | |
David Benjamin | 287fc4f | 2016-12-10 15:12:03 -0500 | [diff] [blame] | 1385 | if (BIO_should_read(bio)) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1386 | // TODO(davidben): OpenSSL historically checked for reads on the write |
| 1387 | // BIO. Can this be removed? |
David Benjamin | 287fc4f | 2016-12-10 15:12:03 -0500 | [diff] [blame] | 1388 | return SSL_ERROR_WANT_READ; |
| 1389 | } |
| 1390 | |
| 1391 | if (BIO_should_io_special(bio)) { |
| 1392 | return bio_retry_reason_to_error(BIO_get_retry_reason(bio)); |
| 1393 | } |
| 1394 | |
| 1395 | break; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1396 | } |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1397 | } |
| 1398 | |
| 1399 | return SSL_ERROR_SYSCALL; |
| 1400 | } |
| 1401 | |
David Benjamin | bd2a8d6 | 2019-08-21 16:17:17 -0400 | [diff] [blame] | 1402 | const char *SSL_error_description(int err) { |
| 1403 | switch (err) { |
| 1404 | case SSL_ERROR_NONE: |
| 1405 | return "NONE"; |
| 1406 | case SSL_ERROR_SSL: |
| 1407 | return "SSL"; |
| 1408 | case SSL_ERROR_WANT_READ: |
| 1409 | return "WANT_READ"; |
| 1410 | case SSL_ERROR_WANT_WRITE: |
| 1411 | return "WANT_WRITE"; |
| 1412 | case SSL_ERROR_WANT_X509_LOOKUP: |
| 1413 | return "WANT_X509_LOOKUP"; |
| 1414 | case SSL_ERROR_SYSCALL: |
| 1415 | return "SYSCALL"; |
| 1416 | case SSL_ERROR_ZERO_RETURN: |
| 1417 | return "ZERO_RETURN"; |
| 1418 | case SSL_ERROR_WANT_CONNECT: |
| 1419 | return "WANT_CONNECT"; |
| 1420 | case SSL_ERROR_WANT_ACCEPT: |
| 1421 | return "WANT_ACCEPT"; |
David Benjamin | bd2a8d6 | 2019-08-21 16:17:17 -0400 | [diff] [blame] | 1422 | case SSL_ERROR_PENDING_SESSION: |
| 1423 | return "PENDING_SESSION"; |
| 1424 | case SSL_ERROR_PENDING_CERTIFICATE: |
| 1425 | return "PENDING_CERTIFICATE"; |
| 1426 | case SSL_ERROR_WANT_PRIVATE_KEY_OPERATION: |
| 1427 | return "WANT_PRIVATE_KEY_OPERATION"; |
| 1428 | case SSL_ERROR_PENDING_TICKET: |
| 1429 | return "PENDING_TICKET"; |
| 1430 | case SSL_ERROR_EARLY_DATA_REJECTED: |
| 1431 | return "EARLY_DATA_REJECTED"; |
| 1432 | case SSL_ERROR_WANT_CERTIFICATE_VERIFY: |
| 1433 | return "WANT_CERTIFICATE_VERIFY"; |
| 1434 | case SSL_ERROR_HANDOFF: |
| 1435 | return "HANDOFF"; |
| 1436 | case SSL_ERROR_HANDBACK: |
| 1437 | return "HANDBACK"; |
David Benjamin | 747229e | 2021-05-18 19:29:58 -0400 | [diff] [blame] | 1438 | case SSL_ERROR_WANT_RENEGOTIATE: |
| 1439 | return "WANT_RENEGOTIATE"; |
David Benjamin | b571e77 | 2021-03-25 19:42:16 -0400 | [diff] [blame] | 1440 | case SSL_ERROR_HANDSHAKE_HINTS_READY: |
| 1441 | return "HANDSHAKE_HINTS_READY"; |
David Benjamin | bd2a8d6 | 2019-08-21 16:17:17 -0400 | [diff] [blame] | 1442 | default: |
| 1443 | return nullptr; |
| 1444 | } |
| 1445 | } |
| 1446 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1447 | uint32_t SSL_CTX_set_options(SSL_CTX *ctx, uint32_t options) { |
| 1448 | ctx->options |= options; |
| 1449 | return ctx->options; |
| 1450 | } |
| 1451 | |
| 1452 | uint32_t SSL_CTX_clear_options(SSL_CTX *ctx, uint32_t options) { |
| 1453 | ctx->options &= ~options; |
| 1454 | return ctx->options; |
| 1455 | } |
| 1456 | |
| 1457 | uint32_t SSL_CTX_get_options(const SSL_CTX *ctx) { return ctx->options; } |
| 1458 | |
| 1459 | uint32_t SSL_set_options(SSL *ssl, uint32_t options) { |
| 1460 | ssl->options |= options; |
| 1461 | return ssl->options; |
| 1462 | } |
| 1463 | |
| 1464 | uint32_t SSL_clear_options(SSL *ssl, uint32_t options) { |
| 1465 | ssl->options &= ~options; |
| 1466 | return ssl->options; |
| 1467 | } |
| 1468 | |
| 1469 | uint32_t SSL_get_options(const SSL *ssl) { return ssl->options; } |
| 1470 | |
| 1471 | uint32_t SSL_CTX_set_mode(SSL_CTX *ctx, uint32_t mode) { |
| 1472 | ctx->mode |= mode; |
| 1473 | return ctx->mode; |
| 1474 | } |
| 1475 | |
| 1476 | uint32_t SSL_CTX_clear_mode(SSL_CTX *ctx, uint32_t mode) { |
| 1477 | ctx->mode &= ~mode; |
| 1478 | return ctx->mode; |
| 1479 | } |
| 1480 | |
| 1481 | uint32_t SSL_CTX_get_mode(const SSL_CTX *ctx) { return ctx->mode; } |
| 1482 | |
| 1483 | uint32_t SSL_set_mode(SSL *ssl, uint32_t mode) { |
| 1484 | ssl->mode |= mode; |
| 1485 | return ssl->mode; |
| 1486 | } |
| 1487 | |
| 1488 | uint32_t SSL_clear_mode(SSL *ssl, uint32_t mode) { |
| 1489 | ssl->mode &= ~mode; |
| 1490 | return ssl->mode; |
| 1491 | } |
| 1492 | |
| 1493 | uint32_t SSL_get_mode(const SSL *ssl) { return ssl->mode; } |
| 1494 | |
Adam Langley | d519bf6 | 2016-12-12 11:16:44 -0800 | [diff] [blame] | 1495 | void SSL_CTX_set0_buffer_pool(SSL_CTX *ctx, CRYPTO_BUFFER_POOL *pool) { |
| 1496 | ctx->pool = pool; |
| 1497 | } |
| 1498 | |
David Benjamin | ee0c827 | 2015-09-13 01:03:54 -0400 | [diff] [blame] | 1499 | int SSL_get_tls_unique(const SSL *ssl, uint8_t *out, size_t *out_len, |
| 1500 | size_t max_out) { |
David Benjamin | f526081 | 2017-07-12 17:16:39 -0400 | [diff] [blame] | 1501 | *out_len = 0; |
| 1502 | OPENSSL_memset(out, 0, max_out); |
| 1503 | |
David Benjamin | 9bb15f5 | 2018-06-26 00:07:40 -0400 | [diff] [blame] | 1504 | // tls-unique is not defined for TLS 1.3. |
David Benjamin | ced00b4 | 2016-10-08 11:24:09 -0400 | [diff] [blame] | 1505 | if (!ssl->s3->initial_handshake_complete || |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 1506 | ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
David Benjamin | f526081 | 2017-07-12 17:16:39 -0400 | [diff] [blame] | 1507 | return 0; |
David Benjamin | ced00b4 | 2016-10-08 11:24:09 -0400 | [diff] [blame] | 1508 | } |
| 1509 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1510 | // The tls-unique value is the first Finished message in the handshake, which |
| 1511 | // is the client's in a full handshake and the server's for a resumption. See |
| 1512 | // https://tools.ietf.org/html/rfc5929#section-3.1. |
David Benjamin | ee0c827 | 2015-09-13 01:03:54 -0400 | [diff] [blame] | 1513 | const uint8_t *finished = ssl->s3->previous_client_finished; |
| 1514 | size_t finished_len = ssl->s3->previous_client_finished_len; |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1515 | if (ssl->session != NULL) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1516 | // tls-unique is broken for resumed sessions unless EMS is used. |
David Benjamin | ee0c827 | 2015-09-13 01:03:54 -0400 | [diff] [blame] | 1517 | if (!ssl->session->extended_master_secret) { |
David Benjamin | f526081 | 2017-07-12 17:16:39 -0400 | [diff] [blame] | 1518 | return 0; |
David Benjamin | ee0c827 | 2015-09-13 01:03:54 -0400 | [diff] [blame] | 1519 | } |
| 1520 | finished = ssl->s3->previous_server_finished; |
| 1521 | finished_len = ssl->s3->previous_server_finished_len; |
| 1522 | } |
| 1523 | |
David Benjamin | ee0c827 | 2015-09-13 01:03:54 -0400 | [diff] [blame] | 1524 | *out_len = finished_len; |
| 1525 | if (finished_len > max_out) { |
| 1526 | *out_len = max_out; |
| 1527 | } |
| 1528 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 1529 | OPENSSL_memcpy(out, finished, *out_len); |
David Benjamin | ee0c827 | 2015-09-13 01:03:54 -0400 | [diff] [blame] | 1530 | return 1; |
David Benjamin | ee0c827 | 2015-09-13 01:03:54 -0400 | [diff] [blame] | 1531 | } |
| 1532 | |
David Benjamin | 5960a90 | 2017-02-14 20:07:11 -0500 | [diff] [blame] | 1533 | static int set_session_id_context(CERT *cert, const uint8_t *sid_ctx, |
David Benjamin | 30c4c30 | 2016-12-07 22:35:24 -0500 | [diff] [blame] | 1534 | size_t sid_ctx_len) { |
David Benjamin | 5960a90 | 2017-02-14 20:07:11 -0500 | [diff] [blame] | 1535 | if (sid_ctx_len > sizeof(cert->sid_ctx)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1536 | OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1537 | return 0; |
| 1538 | } |
David Benjamin | 30c4c30 | 2016-12-07 22:35:24 -0500 | [diff] [blame] | 1539 | |
David Benjamin | a3d76d0 | 2017-07-14 19:36:07 -0400 | [diff] [blame] | 1540 | static_assert(sizeof(cert->sid_ctx) < 256, "sid_ctx too large"); |
David Benjamin | 5960a90 | 2017-02-14 20:07:11 -0500 | [diff] [blame] | 1541 | cert->sid_ctx_length = (uint8_t)sid_ctx_len; |
| 1542 | OPENSSL_memcpy(cert->sid_ctx, sid_ctx, sid_ctx_len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1543 | return 1; |
| 1544 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1545 | |
David Benjamin | 5960a90 | 2017-02-14 20:07:11 -0500 | [diff] [blame] | 1546 | int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx, |
| 1547 | size_t sid_ctx_len) { |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 1548 | return set_session_id_context(ctx->cert.get(), sid_ctx, sid_ctx_len); |
David Benjamin | 5960a90 | 2017-02-14 20:07:11 -0500 | [diff] [blame] | 1549 | } |
| 1550 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1551 | int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx, |
David Benjamin | 30c4c30 | 2016-12-07 22:35:24 -0500 | [diff] [blame] | 1552 | size_t sid_ctx_len) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 1553 | if (!ssl->config) { |
| 1554 | return 0; |
| 1555 | } |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 1556 | return set_session_id_context(ssl->config->cert.get(), sid_ctx, sid_ctx_len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1557 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1558 | |
David Benjamin | 07820b5 | 2016-12-07 21:13:41 -0500 | [diff] [blame] | 1559 | const uint8_t *SSL_get0_session_id_context(const SSL *ssl, size_t *out_len) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 1560 | if (!ssl->config) { |
| 1561 | assert(ssl->config); |
| 1562 | *out_len = 0; |
| 1563 | return NULL; |
| 1564 | } |
| 1565 | *out_len = ssl->config->cert->sid_ctx_length; |
| 1566 | return ssl->config->cert->sid_ctx; |
David Benjamin | 07820b5 | 2016-12-07 21:13:41 -0500 | [diff] [blame] | 1567 | } |
| 1568 | |
David Benjamin | 066fe0a | 2015-10-17 21:11:33 -0400 | [diff] [blame] | 1569 | int SSL_get_fd(const SSL *ssl) { return SSL_get_rfd(ssl); } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1570 | |
David Benjamin | 066fe0a | 2015-10-17 21:11:33 -0400 | [diff] [blame] | 1571 | int SSL_get_rfd(const SSL *ssl) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1572 | int ret = -1; |
David Benjamin | 066fe0a | 2015-10-17 21:11:33 -0400 | [diff] [blame] | 1573 | BIO *b = BIO_find_type(SSL_get_rbio(ssl), BIO_TYPE_DESCRIPTOR); |
| 1574 | if (b != NULL) { |
| 1575 | BIO_get_fd(b, &ret); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1576 | } |
| 1577 | return ret; |
| 1578 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1579 | |
David Benjamin | 066fe0a | 2015-10-17 21:11:33 -0400 | [diff] [blame] | 1580 | int SSL_get_wfd(const SSL *ssl) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1581 | int ret = -1; |
David Benjamin | 066fe0a | 2015-10-17 21:11:33 -0400 | [diff] [blame] | 1582 | BIO *b = BIO_find_type(SSL_get_wbio(ssl), BIO_TYPE_DESCRIPTOR); |
| 1583 | if (b != NULL) { |
| 1584 | BIO_get_fd(b, &ret); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1585 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1586 | return ret; |
| 1587 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1588 | |
David Benjamin | 7cb91d2 | 2023-07-14 16:48:05 -0400 | [diff] [blame] | 1589 | #if !defined(OPENSSL_NO_SOCK) |
David Benjamin | 066fe0a | 2015-10-17 21:11:33 -0400 | [diff] [blame] | 1590 | int SSL_set_fd(SSL *ssl, int fd) { |
David Benjamin | 22ce9b2 | 2016-03-06 19:26:52 -0500 | [diff] [blame] | 1591 | BIO *bio = BIO_new(BIO_s_socket()); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1592 | if (bio == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1593 | OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB); |
David Benjamin | 066fe0a | 2015-10-17 21:11:33 -0400 | [diff] [blame] | 1594 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1595 | } |
| 1596 | BIO_set_fd(bio, fd, BIO_NOCLOSE); |
David Benjamin | 066fe0a | 2015-10-17 21:11:33 -0400 | [diff] [blame] | 1597 | SSL_set_bio(ssl, bio, bio); |
| 1598 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1599 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1600 | |
David Benjamin | 066fe0a | 2015-10-17 21:11:33 -0400 | [diff] [blame] | 1601 | int SSL_set_wfd(SSL *ssl, int fd) { |
David Benjamin | 2f87112 | 2016-05-20 14:27:17 -0400 | [diff] [blame] | 1602 | BIO *rbio = SSL_get_rbio(ssl); |
| 1603 | if (rbio == NULL || BIO_method_type(rbio) != BIO_TYPE_SOCKET || |
| 1604 | BIO_get_fd(rbio, NULL) != fd) { |
David Benjamin | 22ce9b2 | 2016-03-06 19:26:52 -0500 | [diff] [blame] | 1605 | BIO *bio = BIO_new(BIO_s_socket()); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1606 | if (bio == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1607 | OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB); |
David Benjamin | 066fe0a | 2015-10-17 21:11:33 -0400 | [diff] [blame] | 1608 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1609 | } |
| 1610 | BIO_set_fd(bio, fd, BIO_NOCLOSE); |
David Benjamin | 4501bd5 | 2016-08-01 13:39:41 -0400 | [diff] [blame] | 1611 | SSL_set0_wbio(ssl, bio); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1612 | } else { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1613 | // Copy the rbio over to the wbio. |
David Benjamin | 4501bd5 | 2016-08-01 13:39:41 -0400 | [diff] [blame] | 1614 | BIO_up_ref(rbio); |
| 1615 | SSL_set0_wbio(ssl, rbio); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1616 | } |
| 1617 | |
David Benjamin | 066fe0a | 2015-10-17 21:11:33 -0400 | [diff] [blame] | 1618 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1619 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1620 | |
David Benjamin | 066fe0a | 2015-10-17 21:11:33 -0400 | [diff] [blame] | 1621 | int SSL_set_rfd(SSL *ssl, int fd) { |
David Benjamin | 2f87112 | 2016-05-20 14:27:17 -0400 | [diff] [blame] | 1622 | BIO *wbio = SSL_get_wbio(ssl); |
| 1623 | if (wbio == NULL || BIO_method_type(wbio) != BIO_TYPE_SOCKET || |
| 1624 | BIO_get_fd(wbio, NULL) != fd) { |
David Benjamin | 22ce9b2 | 2016-03-06 19:26:52 -0500 | [diff] [blame] | 1625 | BIO *bio = BIO_new(BIO_s_socket()); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1626 | if (bio == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1627 | OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB); |
David Benjamin | 066fe0a | 2015-10-17 21:11:33 -0400 | [diff] [blame] | 1628 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1629 | } |
| 1630 | BIO_set_fd(bio, fd, BIO_NOCLOSE); |
David Benjamin | 4501bd5 | 2016-08-01 13:39:41 -0400 | [diff] [blame] | 1631 | SSL_set0_rbio(ssl, bio); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1632 | } else { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1633 | // Copy the wbio over to the rbio. |
David Benjamin | 4501bd5 | 2016-08-01 13:39:41 -0400 | [diff] [blame] | 1634 | BIO_up_ref(wbio); |
| 1635 | SSL_set0_rbio(ssl, wbio); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1636 | } |
David Benjamin | 066fe0a | 2015-10-17 21:11:33 -0400 | [diff] [blame] | 1637 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1638 | } |
David Benjamin | 7cb91d2 | 2023-07-14 16:48:05 -0400 | [diff] [blame] | 1639 | #endif // !OPENSSL_NO_SOCK |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1640 | |
David Benjamin | 49ddf41 | 2016-10-08 11:56:01 -0400 | [diff] [blame] | 1641 | static size_t copy_finished(void *out, size_t out_len, const uint8_t *in, |
| 1642 | size_t in_len) { |
| 1643 | if (out_len > in_len) { |
| 1644 | out_len = in_len; |
| 1645 | } |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 1646 | OPENSSL_memcpy(out, in, out_len); |
David Benjamin | 49ddf41 | 2016-10-08 11:56:01 -0400 | [diff] [blame] | 1647 | return in_len; |
| 1648 | } |
| 1649 | |
David Benjamin | 1a1b34d | 2015-10-17 12:51:52 -0400 | [diff] [blame] | 1650 | size_t SSL_get_finished(const SSL *ssl, void *buf, size_t count) { |
David Benjamin | ced00b4 | 2016-10-08 11:24:09 -0400 | [diff] [blame] | 1651 | if (!ssl->s3->initial_handshake_complete || |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 1652 | ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
David Benjamin | ced00b4 | 2016-10-08 11:24:09 -0400 | [diff] [blame] | 1653 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1654 | } |
| 1655 | |
David Benjamin | 49ddf41 | 2016-10-08 11:56:01 -0400 | [diff] [blame] | 1656 | if (ssl->server) { |
| 1657 | return copy_finished(buf, count, ssl->s3->previous_server_finished, |
| 1658 | ssl->s3->previous_server_finished_len); |
David Benjamin | ced00b4 | 2016-10-08 11:24:09 -0400 | [diff] [blame] | 1659 | } |
David Benjamin | 49ddf41 | 2016-10-08 11:56:01 -0400 | [diff] [blame] | 1660 | |
| 1661 | return copy_finished(buf, count, ssl->s3->previous_client_finished, |
| 1662 | ssl->s3->previous_client_finished_len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1663 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1664 | |
David Benjamin | 1a1b34d | 2015-10-17 12:51:52 -0400 | [diff] [blame] | 1665 | size_t SSL_get_peer_finished(const SSL *ssl, void *buf, size_t count) { |
David Benjamin | ced00b4 | 2016-10-08 11:24:09 -0400 | [diff] [blame] | 1666 | if (!ssl->s3->initial_handshake_complete || |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 1667 | ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
David Benjamin | ced00b4 | 2016-10-08 11:24:09 -0400 | [diff] [blame] | 1668 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1669 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1670 | |
David Benjamin | 49ddf41 | 2016-10-08 11:56:01 -0400 | [diff] [blame] | 1671 | if (ssl->server) { |
| 1672 | return copy_finished(buf, count, ssl->s3->previous_client_finished, |
| 1673 | ssl->s3->previous_client_finished_len); |
David Benjamin | ced00b4 | 2016-10-08 11:24:09 -0400 | [diff] [blame] | 1674 | } |
David Benjamin | 49ddf41 | 2016-10-08 11:56:01 -0400 | [diff] [blame] | 1675 | |
| 1676 | return copy_finished(buf, count, ssl->s3->previous_server_finished, |
| 1677 | ssl->s3->previous_server_finished_len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1678 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1679 | |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 1680 | int SSL_get_verify_mode(const SSL *ssl) { |
| 1681 | if (!ssl->config) { |
| 1682 | assert(ssl->config); |
| 1683 | return -1; |
| 1684 | } |
| 1685 | return ssl->config->verify_mode; |
| 1686 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1687 | |
David Benjamin | 42fea37 | 2015-09-19 01:22:44 -0400 | [diff] [blame] | 1688 | int SSL_get_extms_support(const SSL *ssl) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1689 | // TLS 1.3 does not require extended master secret and always reports as |
| 1690 | // supporting it. |
David Benjamin | a68c118 | 2016-07-24 00:14:33 -0400 | [diff] [blame] | 1691 | if (!ssl->s3->have_version) { |
| 1692 | return 0; |
| 1693 | } |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 1694 | if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
David Benjamin | fc02b59 | 2017-02-17 16:26:01 -0500 | [diff] [blame] | 1695 | return 1; |
| 1696 | } |
| 1697 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1698 | // If the initial handshake completed, query the established session. |
David Benjamin | fc02b59 | 2017-02-17 16:26:01 -0500 | [diff] [blame] | 1699 | if (ssl->s3->established_session != NULL) { |
| 1700 | return ssl->s3->established_session->extended_master_secret; |
| 1701 | } |
| 1702 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1703 | // Otherwise, query the in-progress handshake. |
David Benjamin | fc02b59 | 2017-02-17 16:26:01 -0500 | [diff] [blame] | 1704 | if (ssl->s3->hs != NULL) { |
| 1705 | return ssl->s3->hs->extended_master_secret; |
| 1706 | } |
| 1707 | assert(0); |
| 1708 | return 0; |
Matt Braithwaite | cd6f54b | 2015-09-17 12:54:42 -0700 | [diff] [blame] | 1709 | } |
| 1710 | |
David Benjamin | 9a41d1b | 2015-05-16 01:30:09 -0400 | [diff] [blame] | 1711 | int SSL_CTX_get_read_ahead(const SSL_CTX *ctx) { return 0; } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1712 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1713 | int SSL_get_read_ahead(const SSL *ssl) { return 0; } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1714 | |
David Benjamin | 103ed08 | 2018-05-10 19:55:02 -0400 | [diff] [blame] | 1715 | int SSL_CTX_set_read_ahead(SSL_CTX *ctx, int yes) { return 1; } |
David Benjamin | 61ecccf | 2015-05-05 09:44:51 -0400 | [diff] [blame] | 1716 | |
David Benjamin | 103ed08 | 2018-05-10 19:55:02 -0400 | [diff] [blame] | 1717 | int SSL_set_read_ahead(SSL *ssl, int yes) { return 1; } |
David Benjamin | 61ecccf | 2015-05-05 09:44:51 -0400 | [diff] [blame] | 1718 | |
David Benjamin | 9f85949 | 2015-10-03 10:44:30 -0400 | [diff] [blame] | 1719 | int SSL_pending(const SSL *ssl) { |
David Benjamin | d9229f9 | 2017-10-06 17:36:20 -0400 | [diff] [blame] | 1720 | return static_cast<int>(ssl->s3->pending_app_data.size()); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1721 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1722 | |
David Benjamin | b3ed071 | 2021-11-12 14:43:21 -0500 | [diff] [blame] | 1723 | int SSL_has_pending(const SSL *ssl) { |
| 1724 | return SSL_pending(ssl) != 0 || !ssl->s3->read_buffer.empty(); |
| 1725 | } |
| 1726 | |
David Benjamin | 5d88014 | 2024-02-20 17:34:14 -0500 | [diff] [blame] | 1727 | static bool has_cert_and_key(const CERT *cert) { |
| 1728 | // TODO(davidben): If |cert->key_method| is set, that should be fine too. |
| 1729 | if (cert->privatekey == nullptr) { |
| 1730 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED); |
| 1731 | return false; |
| 1732 | } |
| 1733 | |
| 1734 | if (cert->chain == nullptr || |
| 1735 | sk_CRYPTO_BUFFER_value(cert->chain.get(), 0) == nullptr) { |
| 1736 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED); |
| 1737 | return false; |
| 1738 | } |
| 1739 | |
| 1740 | return true; |
| 1741 | } |
| 1742 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1743 | int SSL_CTX_check_private_key(const SSL_CTX *ctx) { |
David Benjamin | 5d88014 | 2024-02-20 17:34:14 -0500 | [diff] [blame] | 1744 | // There is no need to actually check consistency because inconsistent values |
| 1745 | // can never be configured. |
| 1746 | return has_cert_and_key(ctx->cert.get()); |
Adam Langley | c5ac2b6 | 2016-11-07 12:02:35 -0800 | [diff] [blame] | 1747 | } |
| 1748 | |
Adam Langley | c5ac2b6 | 2016-11-07 12:02:35 -0800 | [diff] [blame] | 1749 | int SSL_check_private_key(const SSL *ssl) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 1750 | if (!ssl->config) { |
| 1751 | return 0; |
| 1752 | } |
David Benjamin | 5d88014 | 2024-02-20 17:34:14 -0500 | [diff] [blame] | 1753 | |
| 1754 | // There is no need to actually check consistency because inconsistent values |
| 1755 | // can never be configured. |
| 1756 | return has_cert_and_key(ssl->config->cert.get()); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1757 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1758 | |
David Benjamin | 42fea37 | 2015-09-19 01:22:44 -0400 | [diff] [blame] | 1759 | long SSL_get_default_timeout(const SSL *ssl) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1760 | return SSL_DEFAULT_SESSION_TIMEOUT; |
| 1761 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1762 | |
David Benjamin | 44d3eed | 2015-05-21 01:29:55 -0400 | [diff] [blame] | 1763 | int SSL_renegotiate(SSL *ssl) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1764 | // Caller-initiated renegotiation is not supported. |
David Benjamin | 0e7dbd5 | 2019-05-15 16:01:18 -0400 | [diff] [blame] | 1765 | if (!ssl->s3->renegotiate_pending) { |
| 1766 | OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 1767 | return 0; |
| 1768 | } |
| 1769 | |
| 1770 | if (!ssl_can_renegotiate(ssl)) { |
| 1771 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION); |
| 1772 | return 0; |
| 1773 | } |
| 1774 | |
David Benjamin | 9b2cdb7 | 2021-04-01 23:21:53 -0400 | [diff] [blame] | 1775 | // We should not have told the caller to release the private key. |
| 1776 | assert(!SSL_can_release_private_key(ssl)); |
| 1777 | |
David Benjamin | 0e7dbd5 | 2019-05-15 16:01:18 -0400 | [diff] [blame] | 1778 | // Renegotiation is only supported at quiescent points in the application |
| 1779 | // protocol, namely in HTTPS, just before reading the HTTP response. |
| 1780 | // Require the record-layer be idle and avoid complexities of sending a |
| 1781 | // handshake record while an application_data record is being written. |
| 1782 | if (!ssl->s3->write_buffer.empty() || |
| 1783 | ssl->s3->write_shutdown != ssl_shutdown_none) { |
| 1784 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION); |
| 1785 | return 0; |
| 1786 | } |
| 1787 | |
| 1788 | // Begin a new handshake. |
| 1789 | if (ssl->s3->hs != nullptr) { |
| 1790 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1791 | return 0; |
| 1792 | } |
| 1793 | ssl->s3->hs = ssl_handshake_new(ssl); |
| 1794 | if (ssl->s3->hs == nullptr) { |
| 1795 | return 0; |
| 1796 | } |
| 1797 | |
| 1798 | ssl->s3->renegotiate_pending = false; |
| 1799 | ssl->s3->total_renegotiations++; |
| 1800 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1801 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1802 | |
David Benjamin | 44d3eed | 2015-05-21 01:29:55 -0400 | [diff] [blame] | 1803 | int SSL_renegotiate_pending(SSL *ssl) { |
| 1804 | return SSL_in_init(ssl) && ssl->s3->initial_handshake_complete; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1805 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1806 | |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 1807 | int SSL_total_renegotiations(const SSL *ssl) { |
| 1808 | return ssl->s3->total_renegotiations; |
| 1809 | } |
| 1810 | |
David Benjamin | 1d0a194 | 2015-04-26 15:35:35 -0400 | [diff] [blame] | 1811 | size_t SSL_CTX_get_max_cert_list(const SSL_CTX *ctx) { |
| 1812 | return ctx->max_cert_list; |
| 1813 | } |
| 1814 | |
| 1815 | void SSL_CTX_set_max_cert_list(SSL_CTX *ctx, size_t max_cert_list) { |
| 1816 | if (max_cert_list > kMaxHandshakeSize) { |
| 1817 | max_cert_list = kMaxHandshakeSize; |
| 1818 | } |
| 1819 | ctx->max_cert_list = (uint32_t)max_cert_list; |
| 1820 | } |
| 1821 | |
| 1822 | size_t SSL_get_max_cert_list(const SSL *ssl) { |
| 1823 | return ssl->max_cert_list; |
| 1824 | } |
| 1825 | |
| 1826 | void SSL_set_max_cert_list(SSL *ssl, size_t max_cert_list) { |
| 1827 | if (max_cert_list > kMaxHandshakeSize) { |
| 1828 | max_cert_list = kMaxHandshakeSize; |
| 1829 | } |
| 1830 | ssl->max_cert_list = (uint32_t)max_cert_list; |
| 1831 | } |
| 1832 | |
Adam Langley | ce9d85e | 2016-01-24 15:58:39 -0800 | [diff] [blame] | 1833 | int SSL_CTX_set_max_send_fragment(SSL_CTX *ctx, size_t max_send_fragment) { |
David Benjamin | 1d0a194 | 2015-04-26 15:35:35 -0400 | [diff] [blame] | 1834 | if (max_send_fragment < 512) { |
| 1835 | max_send_fragment = 512; |
| 1836 | } |
| 1837 | if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) { |
| 1838 | max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH; |
| 1839 | } |
| 1840 | ctx->max_send_fragment = (uint16_t)max_send_fragment; |
Adam Langley | ce9d85e | 2016-01-24 15:58:39 -0800 | [diff] [blame] | 1841 | |
| 1842 | return 1; |
David Benjamin | 1d0a194 | 2015-04-26 15:35:35 -0400 | [diff] [blame] | 1843 | } |
| 1844 | |
Adam Langley | ce9d85e | 2016-01-24 15:58:39 -0800 | [diff] [blame] | 1845 | int SSL_set_max_send_fragment(SSL *ssl, size_t max_send_fragment) { |
David Benjamin | 1d0a194 | 2015-04-26 15:35:35 -0400 | [diff] [blame] | 1846 | if (max_send_fragment < 512) { |
| 1847 | max_send_fragment = 512; |
| 1848 | } |
| 1849 | if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) { |
| 1850 | max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH; |
| 1851 | } |
| 1852 | ssl->max_send_fragment = (uint16_t)max_send_fragment; |
Adam Langley | ce9d85e | 2016-01-24 15:58:39 -0800 | [diff] [blame] | 1853 | |
| 1854 | return 1; |
David Benjamin | 1d0a194 | 2015-04-26 15:35:35 -0400 | [diff] [blame] | 1855 | } |
| 1856 | |
David Benjamin | cb9cf79 | 2015-05-05 09:46:14 -0400 | [diff] [blame] | 1857 | int SSL_set_mtu(SSL *ssl, unsigned mtu) { |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 1858 | if (!SSL_is_dtls(ssl) || mtu < dtls1_min_mtu()) { |
David Benjamin | cb9cf79 | 2015-05-05 09:46:14 -0400 | [diff] [blame] | 1859 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1860 | } |
David Benjamin | cb9cf79 | 2015-05-05 09:46:14 -0400 | [diff] [blame] | 1861 | ssl->d1->mtu = mtu; |
| 1862 | return 1; |
| 1863 | } |
| 1864 | |
| 1865 | int SSL_get_secure_renegotiation_support(const SSL *ssl) { |
David Benjamin | d261004 | 2017-01-03 10:49:28 -0500 | [diff] [blame] | 1866 | if (!ssl->s3->have_version) { |
| 1867 | return 0; |
| 1868 | } |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 1869 | return ssl_protocol_version(ssl) >= TLS1_3_VERSION || |
David Benjamin | d261004 | 2017-01-03 10:49:28 -0500 | [diff] [blame] | 1870 | ssl->s3->send_connection_binding; |
David Benjamin | cb9cf79 | 2015-05-05 09:46:14 -0400 | [diff] [blame] | 1871 | } |
| 1872 | |
David Benjamin | 71f7d3d | 2015-05-05 09:46:38 -0400 | [diff] [blame] | 1873 | size_t SSL_CTX_sess_number(const SSL_CTX *ctx) { |
David Benjamin | 9eaa3bd | 2017-09-27 17:03:54 -0400 | [diff] [blame] | 1874 | MutexReadLock lock(const_cast<CRYPTO_MUTEX *>(&ctx->lock)); |
David Benjamin | 71f7d3d | 2015-05-05 09:46:38 -0400 | [diff] [blame] | 1875 | return lh_SSL_SESSION_num_items(ctx->sessions); |
| 1876 | } |
| 1877 | |
| 1878 | unsigned long SSL_CTX_sess_set_cache_size(SSL_CTX *ctx, unsigned long size) { |
| 1879 | unsigned long ret = ctx->session_cache_size; |
| 1880 | ctx->session_cache_size = size; |
| 1881 | return ret; |
| 1882 | } |
| 1883 | |
| 1884 | unsigned long SSL_CTX_sess_get_cache_size(const SSL_CTX *ctx) { |
| 1885 | return ctx->session_cache_size; |
| 1886 | } |
| 1887 | |
| 1888 | int SSL_CTX_set_session_cache_mode(SSL_CTX *ctx, int mode) { |
| 1889 | int ret = ctx->session_cache_mode; |
| 1890 | ctx->session_cache_mode = mode; |
| 1891 | return ret; |
| 1892 | } |
| 1893 | |
| 1894 | int SSL_CTX_get_session_cache_mode(const SSL_CTX *ctx) { |
| 1895 | return ctx->session_cache_mode; |
| 1896 | } |
| 1897 | |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 1898 | |
| 1899 | int SSL_CTX_get_tlsext_ticket_keys(SSL_CTX *ctx, void *out, size_t len) { |
| 1900 | if (out == NULL) { |
| 1901 | return 48; |
| 1902 | } |
| 1903 | if (len != 48) { |
| 1904 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH); |
| 1905 | return 0; |
| 1906 | } |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 1907 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1908 | // The default ticket keys are initialized lazily. Trigger a key |
| 1909 | // rotation to initialize them. |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 1910 | if (!ssl_ctx_rotate_ticket_encryption_key(ctx)) { |
| 1911 | return 0; |
| 1912 | } |
| 1913 | |
David Benjamin | f526081 | 2017-07-12 17:16:39 -0400 | [diff] [blame] | 1914 | uint8_t *out_bytes = reinterpret_cast<uint8_t *>(out); |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 1915 | MutexReadLock lock(&ctx->lock); |
David Benjamin | 7bb0fbf | 2018-07-03 13:55:42 -0400 | [diff] [blame] | 1916 | OPENSSL_memcpy(out_bytes, ctx->ticket_key_current->name, 16); |
| 1917 | OPENSSL_memcpy(out_bytes + 16, ctx->ticket_key_current->hmac_key, 16); |
| 1918 | OPENSSL_memcpy(out_bytes + 32, ctx->ticket_key_current->aes_key, 16); |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 1919 | return 1; |
| 1920 | } |
| 1921 | |
| 1922 | int SSL_CTX_set_tlsext_ticket_keys(SSL_CTX *ctx, const void *in, size_t len) { |
| 1923 | if (in == NULL) { |
| 1924 | return 48; |
| 1925 | } |
| 1926 | if (len != 48) { |
| 1927 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH); |
| 1928 | return 0; |
| 1929 | } |
David Benjamin | 7bb0fbf | 2018-07-03 13:55:42 -0400 | [diff] [blame] | 1930 | auto key = MakeUnique<TicketKey>(); |
| 1931 | if (!key) { |
| 1932 | return 0; |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 1933 | } |
David Benjamin | f526081 | 2017-07-12 17:16:39 -0400 | [diff] [blame] | 1934 | const uint8_t *in_bytes = reinterpret_cast<const uint8_t *>(in); |
David Benjamin | 7bb0fbf | 2018-07-03 13:55:42 -0400 | [diff] [blame] | 1935 | OPENSSL_memcpy(key->name, in_bytes, 16); |
| 1936 | OPENSSL_memcpy(key->hmac_key, in_bytes + 16, 16); |
| 1937 | OPENSSL_memcpy(key->aes_key, in_bytes + 32, 16); |
| 1938 | // Disable automatic key rotation for manually-configured keys. This is now |
| 1939 | // the caller's responsibility. |
| 1940 | key->next_rotation_tv_sec = 0; |
| 1941 | ctx->ticket_key_current = std::move(key); |
| 1942 | ctx->ticket_key_prev.reset(); |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 1943 | return 1; |
| 1944 | } |
| 1945 | |
| 1946 | int SSL_CTX_set_tlsext_ticket_key_cb( |
| 1947 | SSL_CTX *ctx, int (*callback)(SSL *ssl, uint8_t *key_name, uint8_t *iv, |
| 1948 | EVP_CIPHER_CTX *ctx, HMAC_CTX *hmac_ctx, |
| 1949 | int encrypt)) { |
David Benjamin | 7bb0fbf | 2018-07-03 13:55:42 -0400 | [diff] [blame] | 1950 | ctx->ticket_key_cb = callback; |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 1951 | return 1; |
| 1952 | } |
| 1953 | |
David Benjamin | 6cf9820 | 2023-05-26 22:03:30 -0400 | [diff] [blame] | 1954 | static bool check_group_ids(Span<const uint16_t> group_ids) { |
| 1955 | for (uint16_t group_id : group_ids) { |
| 1956 | if (ssl_group_id_to_nid(group_id) == NID_undef) { |
| 1957 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_ELLIPTIC_CURVE); |
| 1958 | return false; |
| 1959 | } |
| 1960 | } |
| 1961 | return true; |
| 1962 | } |
| 1963 | |
| 1964 | int SSL_CTX_set1_group_ids(SSL_CTX *ctx, const uint16_t *group_ids, |
| 1965 | size_t num_group_ids) { |
| 1966 | auto span = MakeConstSpan(group_ids, num_group_ids); |
| 1967 | return check_group_ids(span) && ctx->supported_group_list.CopyFrom(span); |
| 1968 | } |
| 1969 | |
| 1970 | int SSL_set1_group_ids(SSL *ssl, const uint16_t *group_ids, |
| 1971 | size_t num_group_ids) { |
| 1972 | if (!ssl->config) { |
| 1973 | return 0; |
| 1974 | } |
| 1975 | auto span = MakeConstSpan(group_ids, num_group_ids); |
| 1976 | return check_group_ids(span) && |
| 1977 | ssl->config->supported_group_list.CopyFrom(span); |
| 1978 | } |
| 1979 | |
David Benjamin | 2da5ba9 | 2023-05-26 21:23:39 -0400 | [diff] [blame] | 1980 | static bool ssl_nids_to_group_ids(Array<uint16_t> *out_group_ids, |
| 1981 | Span<const int> nids) { |
| 1982 | Array<uint16_t> group_ids; |
| 1983 | if (!group_ids.Init(nids.size())) { |
| 1984 | return false; |
| 1985 | } |
| 1986 | |
| 1987 | for (size_t i = 0; i < nids.size(); i++) { |
| 1988 | if (!ssl_nid_to_group_id(&group_ids[i], nids[i])) { |
David Benjamin | 6cf9820 | 2023-05-26 22:03:30 -0400 | [diff] [blame] | 1989 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_ELLIPTIC_CURVE); |
David Benjamin | 2da5ba9 | 2023-05-26 21:23:39 -0400 | [diff] [blame] | 1990 | return false; |
| 1991 | } |
| 1992 | } |
| 1993 | |
| 1994 | *out_group_ids = std::move(group_ids); |
| 1995 | return true; |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 1996 | } |
| 1997 | |
David Benjamin | 2da5ba9 | 2023-05-26 21:23:39 -0400 | [diff] [blame] | 1998 | int SSL_CTX_set1_groups(SSL_CTX *ctx, const int *groups, size_t num_groups) { |
| 1999 | return ssl_nids_to_group_ids(&ctx->supported_group_list, |
| 2000 | MakeConstSpan(groups, num_groups)); |
| 2001 | } |
| 2002 | |
| 2003 | int SSL_set1_groups(SSL *ssl, const int *groups, size_t num_groups) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 2004 | if (!ssl->config) { |
| 2005 | return 0; |
| 2006 | } |
David Benjamin | 2da5ba9 | 2023-05-26 21:23:39 -0400 | [diff] [blame] | 2007 | return ssl_nids_to_group_ids(&ssl->config->supported_group_list, |
| 2008 | MakeConstSpan(groups, num_groups)); |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 2009 | } |
| 2010 | |
David Benjamin | 2da5ba9 | 2023-05-26 21:23:39 -0400 | [diff] [blame] | 2011 | static bool ssl_str_to_group_ids(Array<uint16_t> *out_group_ids, |
| 2012 | const char *str) { |
| 2013 | // Count the number of groups in the list. |
| 2014 | size_t count = 0; |
| 2015 | const char *ptr = str, *col; |
| 2016 | do { |
| 2017 | col = strchr(ptr, ':'); |
| 2018 | count++; |
| 2019 | if (col) { |
| 2020 | ptr = col + 1; |
| 2021 | } |
| 2022 | } while (col); |
Alessandro Ghedini | 5fd1807 | 2016-09-28 21:04:25 +0100 | [diff] [blame] | 2023 | |
David Benjamin | 2da5ba9 | 2023-05-26 21:23:39 -0400 | [diff] [blame] | 2024 | Array<uint16_t> group_ids; |
| 2025 | if (!group_ids.Init(count)) { |
| 2026 | return false; |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 2027 | } |
Alessandro Ghedini | 5fd1807 | 2016-09-28 21:04:25 +0100 | [diff] [blame] | 2028 | |
David Benjamin | 2da5ba9 | 2023-05-26 21:23:39 -0400 | [diff] [blame] | 2029 | size_t i = 0; |
| 2030 | ptr = str; |
| 2031 | do { |
| 2032 | col = strchr(ptr, ':'); |
| 2033 | if (!ssl_name_to_group_id(&group_ids[i++], ptr, |
| 2034 | col ? (size_t)(col - ptr) : strlen(ptr))) { |
David Benjamin | 6cf9820 | 2023-05-26 22:03:30 -0400 | [diff] [blame] | 2035 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_ELLIPTIC_CURVE); |
David Benjamin | 2da5ba9 | 2023-05-26 21:23:39 -0400 | [diff] [blame] | 2036 | return false; |
| 2037 | } |
| 2038 | if (col) { |
| 2039 | ptr = col + 1; |
| 2040 | } |
| 2041 | } while (col); |
David Benjamin | e8e6cac | 2022-09-06 12:03:53 -0400 | [diff] [blame] | 2042 | |
David Benjamin | 2da5ba9 | 2023-05-26 21:23:39 -0400 | [diff] [blame] | 2043 | assert(i == count); |
| 2044 | *out_group_ids = std::move(group_ids); |
| 2045 | return true; |
David Benjamin | e8e6cac | 2022-09-06 12:03:53 -0400 | [diff] [blame] | 2046 | } |
| 2047 | |
| 2048 | int SSL_CTX_set1_groups_list(SSL_CTX *ctx, const char *groups) { |
David Benjamin | 2da5ba9 | 2023-05-26 21:23:39 -0400 | [diff] [blame] | 2049 | return ssl_str_to_group_ids(&ctx->supported_group_list, groups); |
David Benjamin | e8e6cac | 2022-09-06 12:03:53 -0400 | [diff] [blame] | 2050 | } |
| 2051 | |
| 2052 | int SSL_set1_groups_list(SSL *ssl, const char *groups) { |
David Benjamin | 2da5ba9 | 2023-05-26 21:23:39 -0400 | [diff] [blame] | 2053 | if (!ssl->config) { |
| 2054 | return 0; |
| 2055 | } |
| 2056 | return ssl_str_to_group_ids(&ssl->config->supported_group_list, groups); |
David Benjamin | e8e6cac | 2022-09-06 12:03:53 -0400 | [diff] [blame] | 2057 | } |
| 2058 | |
David Benjamin | 335523a | 2023-05-26 21:55:56 -0400 | [diff] [blame] | 2059 | uint16_t SSL_get_group_id(const SSL *ssl) { |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 2060 | SSL_SESSION *session = SSL_get_session(ssl); |
David Benjamin | 4882a6c | 2016-12-11 02:48:12 -0500 | [diff] [blame] | 2061 | if (session == NULL) { |
David Benjamin | 9e68f19 | 2016-06-30 14:55:33 -0400 | [diff] [blame] | 2062 | return 0; |
| 2063 | } |
| 2064 | |
David Benjamin | 4882a6c | 2016-12-11 02:48:12 -0500 | [diff] [blame] | 2065 | return session->group_id; |
David Benjamin | 9e68f19 | 2016-06-30 14:55:33 -0400 | [diff] [blame] | 2066 | } |
| 2067 | |
David Benjamin | 6cf9820 | 2023-05-26 22:03:30 -0400 | [diff] [blame] | 2068 | int SSL_get_negotiated_group(const SSL *ssl) { |
| 2069 | uint16_t group_id = SSL_get_group_id(ssl); |
| 2070 | if (group_id == 0) { |
| 2071 | return NID_undef; |
| 2072 | } |
| 2073 | return ssl_group_id_to_nid(group_id); |
| 2074 | } |
| 2075 | |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 2076 | int SSL_CTX_set_tmp_dh(SSL_CTX *ctx, const DH *dh) { |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 2077 | return 1; |
| 2078 | } |
| 2079 | |
| 2080 | int SSL_set_tmp_dh(SSL *ssl, const DH *dh) { |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 2081 | return 1; |
| 2082 | } |
| 2083 | |
David Benjamin | 70dbf04 | 2017-08-08 18:51:37 -0400 | [diff] [blame] | 2084 | STACK_OF(SSL_CIPHER) *SSL_CTX_get_ciphers(const SSL_CTX *ctx) { |
David Benjamin | 48b276d | 2018-04-12 17:37:32 -0400 | [diff] [blame] | 2085 | return ctx->cipher_list->ciphers.get(); |
David Benjamin | 8ebeabf | 2017-03-02 22:04:07 -0500 | [diff] [blame] | 2086 | } |
| 2087 | |
David Benjamin | 70dbf04 | 2017-08-08 18:51:37 -0400 | [diff] [blame] | 2088 | int SSL_CTX_cipher_in_group(const SSL_CTX *ctx, size_t i) { |
David Benjamin | 48b276d | 2018-04-12 17:37:32 -0400 | [diff] [blame] | 2089 | if (i >= sk_SSL_CIPHER_num(ctx->cipher_list->ciphers.get())) { |
David Benjamin | 70dbf04 | 2017-08-08 18:51:37 -0400 | [diff] [blame] | 2090 | return 0; |
| 2091 | } |
| 2092 | return ctx->cipher_list->in_group_flags[i]; |
| 2093 | } |
| 2094 | |
David Benjamin | 32876b3 | 2015-09-20 12:17:03 -0400 | [diff] [blame] | 2095 | STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *ssl) { |
| 2096 | if (ssl == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2097 | return NULL; |
| 2098 | } |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 2099 | if (ssl->config == NULL) { |
| 2100 | assert(ssl->config); |
| 2101 | return NULL; |
| 2102 | } |
Adam Langley | 0b5c1ac | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2103 | |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 2104 | return ssl->config->cipher_list ? ssl->config->cipher_list->ciphers.get() |
| 2105 | : ssl->ctx->cipher_list->ciphers.get(); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2106 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2107 | |
David Benjamin | 32876b3 | 2015-09-20 12:17:03 -0400 | [diff] [blame] | 2108 | const char *SSL_get_cipher_list(const SSL *ssl, int n) { |
David Benjamin | 32876b3 | 2015-09-20 12:17:03 -0400 | [diff] [blame] | 2109 | if (ssl == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2110 | return NULL; |
| 2111 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2112 | |
David Benjamin | 8ebeabf | 2017-03-02 22:04:07 -0500 | [diff] [blame] | 2113 | STACK_OF(SSL_CIPHER) *sk = SSL_get_ciphers(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2114 | if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) { |
| 2115 | return NULL; |
| 2116 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2117 | |
David Benjamin | 8ebeabf | 2017-03-02 22:04:07 -0500 | [diff] [blame] | 2118 | const SSL_CIPHER *c = sk_SSL_CIPHER_value(sk, n); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2119 | if (c == NULL) { |
| 2120 | return NULL; |
| 2121 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2122 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2123 | return c->name; |
| 2124 | } |
David Benjamin | 5491e3f | 2014-09-29 19:33:09 -0400 | [diff] [blame] | 2125 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2126 | int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) { |
Bob Beck | 8cacbd9 | 2023-04-05 14:38:38 -0600 | [diff] [blame] | 2127 | const bool has_aes_hw = ctx->aes_hw_override ? ctx->aes_hw_override_value |
| 2128 | : EVP_has_aes_hardware(); |
| 2129 | return ssl_create_cipher_list(&ctx->cipher_list, has_aes_hw, str, |
| 2130 | false /* not strict */); |
Matthew Braithwaite | a57dcfb | 2017-02-17 22:08:23 -0800 | [diff] [blame] | 2131 | } |
| 2132 | |
| 2133 | int SSL_CTX_set_strict_cipher_list(SSL_CTX *ctx, const char *str) { |
Bob Beck | 8cacbd9 | 2023-04-05 14:38:38 -0600 | [diff] [blame] | 2134 | const bool has_aes_hw = ctx->aes_hw_override ? ctx->aes_hw_override_value |
| 2135 | : EVP_has_aes_hardware(); |
| 2136 | return ssl_create_cipher_list(&ctx->cipher_list, has_aes_hw, str, |
| 2137 | true /* strict */); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2138 | } |
David Benjamin | 39482a1 | 2014-07-20 13:30:15 -0400 | [diff] [blame] | 2139 | |
David Benjamin | 32876b3 | 2015-09-20 12:17:03 -0400 | [diff] [blame] | 2140 | int SSL_set_cipher_list(SSL *ssl, const char *str) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 2141 | if (!ssl->config) { |
| 2142 | return 0; |
| 2143 | } |
Bob Beck | 8cacbd9 | 2023-04-05 14:38:38 -0600 | [diff] [blame] | 2144 | const bool has_aes_hw = ssl->config->aes_hw_override |
| 2145 | ? ssl->config->aes_hw_override_value |
| 2146 | : EVP_has_aes_hardware(); |
| 2147 | return ssl_create_cipher_list(&ssl->config->cipher_list, has_aes_hw, str, |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 2148 | false /* not strict */); |
Matthew Braithwaite | a57dcfb | 2017-02-17 22:08:23 -0800 | [diff] [blame] | 2149 | } |
| 2150 | |
| 2151 | int SSL_set_strict_cipher_list(SSL *ssl, const char *str) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 2152 | if (!ssl->config) { |
| 2153 | return 0; |
| 2154 | } |
Bob Beck | 8cacbd9 | 2023-04-05 14:38:38 -0600 | [diff] [blame] | 2155 | const bool has_aes_hw = ssl->config->aes_hw_override |
| 2156 | ? ssl->config->aes_hw_override_value |
| 2157 | : EVP_has_aes_hardware(); |
| 2158 | return ssl_create_cipher_list(&ssl->config->cipher_list, has_aes_hw, str, |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 2159 | true /* strict */); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2160 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2161 | |
David Benjamin | 07e1384 | 2015-10-17 13:48:04 -0400 | [diff] [blame] | 2162 | const char *SSL_get_servername(const SSL *ssl, const int type) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2163 | if (type != TLSEXT_NAMETYPE_host_name) { |
| 2164 | return NULL; |
| 2165 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2166 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2167 | // Historically, |SSL_get_servername| was also the configuration getter |
| 2168 | // corresponding to |SSL_set_tlsext_host_name|. |
David Benjamin | 4685376 | 2018-07-03 14:01:26 -0400 | [diff] [blame] | 2169 | if (ssl->hostname != nullptr) { |
| 2170 | return ssl->hostname.get(); |
David Benjamin | 07e1384 | 2015-10-17 13:48:04 -0400 | [diff] [blame] | 2171 | } |
| 2172 | |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 2173 | return ssl->s3->hostname.get(); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2174 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2175 | |
David Benjamin | 07e1384 | 2015-10-17 13:48:04 -0400 | [diff] [blame] | 2176 | int SSL_get_servername_type(const SSL *ssl) { |
David Benjamin | a861460 | 2017-09-06 15:40:19 -0400 | [diff] [blame] | 2177 | if (SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) == NULL) { |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 2178 | return -1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2179 | } |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 2180 | return TLSEXT_NAMETYPE_host_name; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2181 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2182 | |
David Benjamin | 3a1dd46 | 2017-07-11 16:13:10 -0400 | [diff] [blame] | 2183 | void SSL_CTX_set_custom_verify( |
| 2184 | SSL_CTX *ctx, int mode, |
| 2185 | enum ssl_verify_result_t (*callback)(SSL *ssl, uint8_t *out_alert)) { |
| 2186 | ctx->verify_mode = mode; |
| 2187 | ctx->custom_verify_callback = callback; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2188 | } |
HÃ¥vard Molland | 9169c96 | 2014-08-14 14:42:37 +0200 | [diff] [blame] | 2189 | |
David Benjamin | 3a1dd46 | 2017-07-11 16:13:10 -0400 | [diff] [blame] | 2190 | void SSL_set_custom_verify( |
| 2191 | SSL *ssl, int mode, |
| 2192 | enum ssl_verify_result_t (*callback)(SSL *ssl, uint8_t *out_alert)) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 2193 | if (!ssl->config) { |
| 2194 | return; |
| 2195 | } |
| 2196 | ssl->config->verify_mode = mode; |
| 2197 | ssl->config->custom_verify_callback = callback; |
David Benjamin | 3a1dd46 | 2017-07-11 16:13:10 -0400 | [diff] [blame] | 2198 | } |
| 2199 | |
| 2200 | void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) { |
David Benjamin | 7e58c5e | 2017-10-11 13:01:08 -0400 | [diff] [blame] | 2201 | ctx->signed_cert_timestamps_enabled = true; |
Adam Langley | fe36672 | 2017-03-07 16:41:04 -0800 | [diff] [blame] | 2202 | } |
| 2203 | |
David Benjamin | 26e1ff3 | 2017-02-14 20:13:00 -0500 | [diff] [blame] | 2204 | void SSL_enable_signed_cert_timestamps(SSL *ssl) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 2205 | if (!ssl->config) { |
| 2206 | return; |
| 2207 | } |
| 2208 | ssl->config->signed_cert_timestamps_enabled = true; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2209 | } |
HÃ¥vard Molland | 9169c96 | 2014-08-14 14:42:37 +0200 | [diff] [blame] | 2210 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2211 | void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) { |
David Benjamin | 7e58c5e | 2017-10-11 13:01:08 -0400 | [diff] [blame] | 2212 | ctx->ocsp_stapling_enabled = true; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2213 | } |
David Benjamin | 6c7aed0 | 2014-08-27 16:42:38 -0400 | [diff] [blame] | 2214 | |
David Benjamin | 26e1ff3 | 2017-02-14 20:13:00 -0500 | [diff] [blame] | 2215 | void SSL_enable_ocsp_stapling(SSL *ssl) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 2216 | if (!ssl->config) { |
| 2217 | return; |
| 2218 | } |
| 2219 | ssl->config->ocsp_stapling_enabled = true; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2220 | } |
David Benjamin | 6c7aed0 | 2014-08-27 16:42:38 -0400 | [diff] [blame] | 2221 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2222 | void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out, |
| 2223 | size_t *out_len) { |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 2224 | SSL_SESSION *session = SSL_get_session(ssl); |
David Benjamin | 8fc2dc0 | 2017-08-22 15:07:51 -0700 | [diff] [blame] | 2225 | if (ssl->server || !session || !session->signed_cert_timestamp_list) { |
| 2226 | *out_len = 0; |
| 2227 | *out = NULL; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2228 | return; |
| 2229 | } |
HÃ¥vard Molland | 9169c96 | 2014-08-14 14:42:37 +0200 | [diff] [blame] | 2230 | |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 2231 | *out = CRYPTO_BUFFER_data(session->signed_cert_timestamp_list.get()); |
| 2232 | *out_len = CRYPTO_BUFFER_len(session->signed_cert_timestamp_list.get()); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2233 | } |
David Benjamin | 6c7aed0 | 2014-08-27 16:42:38 -0400 | [diff] [blame] | 2234 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2235 | void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out, |
| 2236 | size_t *out_len) { |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 2237 | SSL_SESSION *session = SSL_get_session(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2238 | if (ssl->server || !session || !session->ocsp_response) { |
David Benjamin | 8fc2dc0 | 2017-08-22 15:07:51 -0700 | [diff] [blame] | 2239 | *out_len = 0; |
| 2240 | *out = NULL; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2241 | return; |
| 2242 | } |
David Benjamin | 8fc2dc0 | 2017-08-22 15:07:51 -0700 | [diff] [blame] | 2243 | |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 2244 | *out = CRYPTO_BUFFER_data(session->ocsp_response.get()); |
| 2245 | *out_len = CRYPTO_BUFFER_len(session->ocsp_response.get()); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2246 | } |
David Benjamin | 6c7aed0 | 2014-08-27 16:42:38 -0400 | [diff] [blame] | 2247 | |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 2248 | int SSL_set_tlsext_host_name(SSL *ssl, const char *name) { |
David Benjamin | 4685376 | 2018-07-03 14:01:26 -0400 | [diff] [blame] | 2249 | ssl->hostname.reset(); |
David Benjamin | 50596f8 | 2018-07-02 19:47:27 -0400 | [diff] [blame] | 2250 | if (name == nullptr) { |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 2251 | return 1; |
| 2252 | } |
| 2253 | |
| 2254 | size_t len = strlen(name); |
| 2255 | if (len == 0 || len > TLSEXT_MAXLEN_host_name) { |
| 2256 | OPENSSL_PUT_ERROR(SSL, SSL_R_SSL3_EXT_INVALID_SERVERNAME); |
| 2257 | return 0; |
| 2258 | } |
David Benjamin | 3ba9586 | 2019-10-21 16:14:33 -0400 | [diff] [blame] | 2259 | ssl->hostname.reset(OPENSSL_strdup(name)); |
David Benjamin | 4685376 | 2018-07-03 14:01:26 -0400 | [diff] [blame] | 2260 | if (ssl->hostname == nullptr) { |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 2261 | return 0; |
| 2262 | } |
| 2263 | return 1; |
| 2264 | } |
| 2265 | |
| 2266 | int SSL_CTX_set_tlsext_servername_callback( |
| 2267 | SSL_CTX *ctx, int (*callback)(SSL *ssl, int *out_alert, void *arg)) { |
David Benjamin | 4685376 | 2018-07-03 14:01:26 -0400 | [diff] [blame] | 2268 | ctx->servername_callback = callback; |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 2269 | return 1; |
| 2270 | } |
| 2271 | |
| 2272 | int SSL_CTX_set_tlsext_servername_arg(SSL_CTX *ctx, void *arg) { |
David Benjamin | 4685376 | 2018-07-03 14:01:26 -0400 | [diff] [blame] | 2273 | ctx->servername_arg = arg; |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 2274 | return 1; |
| 2275 | } |
| 2276 | |
David Benjamin | ca74358 | 2017-06-15 17:51:35 -0400 | [diff] [blame] | 2277 | int SSL_select_next_proto(uint8_t **out, uint8_t *out_len, const uint8_t *peer, |
| 2278 | unsigned peer_len, const uint8_t *supported, |
| 2279 | unsigned supported_len) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2280 | const uint8_t *result; |
David Benjamin | ca74358 | 2017-06-15 17:51:35 -0400 | [diff] [blame] | 2281 | int status; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2282 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2283 | // For each protocol in peer preference order, see if we support it. |
David Benjamin | ca74358 | 2017-06-15 17:51:35 -0400 | [diff] [blame] | 2284 | for (unsigned i = 0; i < peer_len;) { |
| 2285 | for (unsigned j = 0; j < supported_len;) { |
| 2286 | if (peer[i] == supported[j] && |
| 2287 | OPENSSL_memcmp(&peer[i + 1], &supported[j + 1], peer[i]) == 0) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2288 | // We found a match |
David Benjamin | ca74358 | 2017-06-15 17:51:35 -0400 | [diff] [blame] | 2289 | result = &peer[i]; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2290 | status = OPENSSL_NPN_NEGOTIATED; |
| 2291 | goto found; |
| 2292 | } |
David Benjamin | ca74358 | 2017-06-15 17:51:35 -0400 | [diff] [blame] | 2293 | j += supported[j]; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2294 | j++; |
| 2295 | } |
David Benjamin | ca74358 | 2017-06-15 17:51:35 -0400 | [diff] [blame] | 2296 | i += peer[i]; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2297 | i++; |
| 2298 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2299 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2300 | // There's no overlap between our protocols and the peer's list. |
David Benjamin | ca74358 | 2017-06-15 17:51:35 -0400 | [diff] [blame] | 2301 | result = supported; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2302 | status = OPENSSL_NPN_NO_OVERLAP; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2303 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2304 | found: |
| 2305 | *out = (uint8_t *)result + 1; |
David Benjamin | 977547b | 2015-09-16 00:25:52 -0400 | [diff] [blame] | 2306 | *out_len = result[0]; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2307 | return status; |
| 2308 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2309 | |
David Benjamin | 977547b | 2015-09-16 00:25:52 -0400 | [diff] [blame] | 2310 | void SSL_get0_next_proto_negotiated(const SSL *ssl, const uint8_t **out_data, |
| 2311 | unsigned *out_len) { |
David Benjamin | 7ac94aa | 2022-10-24 15:44:37 -0400 | [diff] [blame] | 2312 | // NPN protocols have one-byte lengths, so they must fit in |unsigned|. |
| 2313 | assert(ssl->s3->next_proto_negotiated.size() <= UINT_MAX); |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 2314 | *out_data = ssl->s3->next_proto_negotiated.data(); |
David Benjamin | 7ac94aa | 2022-10-24 15:44:37 -0400 | [diff] [blame] | 2315 | *out_len = static_cast<unsigned>(ssl->s3->next_proto_negotiated.size()); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2316 | } |
| 2317 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2318 | void SSL_CTX_set_next_protos_advertised_cb( |
| 2319 | SSL_CTX *ctx, |
David Benjamin | 977547b | 2015-09-16 00:25:52 -0400 | [diff] [blame] | 2320 | int (*cb)(SSL *ssl, const uint8_t **out, unsigned *out_len, void *arg), |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2321 | void *arg) { |
| 2322 | ctx->next_protos_advertised_cb = cb; |
| 2323 | ctx->next_protos_advertised_cb_arg = arg; |
| 2324 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2325 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2326 | void SSL_CTX_set_next_proto_select_cb( |
David Benjamin | 977547b | 2015-09-16 00:25:52 -0400 | [diff] [blame] | 2327 | SSL_CTX *ctx, int (*cb)(SSL *ssl, uint8_t **out, uint8_t *out_len, |
| 2328 | const uint8_t *in, unsigned in_len, void *arg), |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2329 | void *arg) { |
| 2330 | ctx->next_proto_select_cb = cb; |
| 2331 | ctx->next_proto_select_cb_arg = arg; |
| 2332 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2333 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2334 | int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos, |
David Benjamin | 7ac94aa | 2022-10-24 15:44:37 -0400 | [diff] [blame] | 2335 | size_t protos_len) { |
David Benjamin | 12a3e7e | 2021-04-13 11:47:36 -0400 | [diff] [blame] | 2336 | // Note this function's return value is backwards. |
| 2337 | auto span = MakeConstSpan(protos, protos_len); |
| 2338 | if (!span.empty() && !ssl_is_valid_alpn_list(span)) { |
| 2339 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL_LIST); |
| 2340 | return 1; |
| 2341 | } |
| 2342 | return ctx->alpn_client_proto_list.CopyFrom(span) ? 0 : 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2343 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2344 | |
David Benjamin | 7ac94aa | 2022-10-24 15:44:37 -0400 | [diff] [blame] | 2345 | int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, size_t protos_len) { |
David Benjamin | 12a3e7e | 2021-04-13 11:47:36 -0400 | [diff] [blame] | 2346 | // Note this function's return value is backwards. |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 2347 | if (!ssl->config) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2348 | return 1; |
| 2349 | } |
David Benjamin | 12a3e7e | 2021-04-13 11:47:36 -0400 | [diff] [blame] | 2350 | auto span = MakeConstSpan(protos, protos_len); |
| 2351 | if (!span.empty() && !ssl_is_valid_alpn_list(span)) { |
| 2352 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL_LIST); |
| 2353 | return 1; |
| 2354 | } |
| 2355 | return ssl->config->alpn_client_proto_list.CopyFrom(span) ? 0 : 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2356 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2357 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2358 | void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx, |
| 2359 | int (*cb)(SSL *ssl, const uint8_t **out, |
David Benjamin | 8984f1f | 2015-09-16 00:10:19 -0400 | [diff] [blame] | 2360 | uint8_t *out_len, const uint8_t *in, |
| 2361 | unsigned in_len, void *arg), |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2362 | void *arg) { |
| 2363 | ctx->alpn_select_cb = cb; |
| 2364 | ctx->alpn_select_cb_arg = arg; |
| 2365 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2366 | |
David Benjamin | 8984f1f | 2015-09-16 00:10:19 -0400 | [diff] [blame] | 2367 | void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **out_data, |
| 2368 | unsigned *out_len) { |
David Benjamin | 7ac94aa | 2022-10-24 15:44:37 -0400 | [diff] [blame] | 2369 | Span<const uint8_t> protocol; |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 2370 | if (SSL_in_early_data(ssl) && !ssl->server) { |
David Benjamin | 7ac94aa | 2022-10-24 15:44:37 -0400 | [diff] [blame] | 2371 | protocol = ssl->s3->hs->early_session->early_alpn; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2372 | } else { |
David Benjamin | 7ac94aa | 2022-10-24 15:44:37 -0400 | [diff] [blame] | 2373 | protocol = ssl->s3->alpn_selected; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2374 | } |
David Benjamin | 7ac94aa | 2022-10-24 15:44:37 -0400 | [diff] [blame] | 2375 | // ALPN protocols have one-byte lengths, so they must fit in |unsigned|. |
| 2376 | assert(protocol.size() < UINT_MAX); |
| 2377 | *out_data = protocol.data(); |
| 2378 | *out_len = static_cast<unsigned>(protocol.size()); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2379 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2380 | |
David Benjamin | c8ff30c | 2017-04-04 13:52:36 -0400 | [diff] [blame] | 2381 | void SSL_CTX_set_allow_unknown_alpn_protos(SSL_CTX *ctx, int enabled) { |
| 2382 | ctx->allow_unknown_alpn_protos = !!enabled; |
| 2383 | } |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 2384 | |
Steven Valdez | 51607f1 | 2020-08-05 10:46:05 -0400 | [diff] [blame] | 2385 | int SSL_add_application_settings(SSL *ssl, const uint8_t *proto, |
| 2386 | size_t proto_len, const uint8_t *settings, |
| 2387 | size_t settings_len) { |
| 2388 | if (!ssl->config) { |
| 2389 | return 0; |
| 2390 | } |
| 2391 | ALPSConfig config; |
| 2392 | if (!config.protocol.CopyFrom(MakeConstSpan(proto, proto_len)) || |
| 2393 | !config.settings.CopyFrom(MakeConstSpan(settings, settings_len)) || |
| 2394 | !ssl->config->alps_configs.Push(std::move(config))) { |
| 2395 | return 0; |
| 2396 | } |
| 2397 | return 1; |
| 2398 | } |
| 2399 | |
| 2400 | void SSL_get0_peer_application_settings(const SSL *ssl, |
| 2401 | const uint8_t **out_data, |
| 2402 | size_t *out_len) { |
| 2403 | const SSL_SESSION *session = SSL_get_session(ssl); |
| 2404 | Span<const uint8_t> settings = |
| 2405 | session ? session->peer_application_settings : Span<const uint8_t>(); |
| 2406 | *out_data = settings.data(); |
| 2407 | *out_len = settings.size(); |
| 2408 | } |
| 2409 | |
| 2410 | int SSL_has_application_settings(const SSL *ssl) { |
| 2411 | const SSL_SESSION *session = SSL_get_session(ssl); |
| 2412 | return session && session->has_application_settings; |
| 2413 | } |
| 2414 | |
Victor Tan | 558960d | 2023-06-23 15:04:33 +0000 | [diff] [blame] | 2415 | void SSL_set_alps_use_new_codepoint(SSL *ssl, int use_new) { |
| 2416 | if (!ssl->config) { |
| 2417 | return; |
| 2418 | } |
| 2419 | ssl->config->alps_use_new_codepoint = !!use_new; |
| 2420 | } |
| 2421 | |
Adam Langley | 8596795 | 2018-07-03 08:04:58 -0700 | [diff] [blame] | 2422 | int SSL_CTX_add_cert_compression_alg(SSL_CTX *ctx, uint16_t alg_id, |
| 2423 | ssl_cert_compression_func_t compress, |
| 2424 | ssl_cert_decompression_func_t decompress) { |
| 2425 | assert(compress != nullptr || decompress != nullptr); |
| 2426 | |
David Benjamin | 8fe1584 | 2019-10-08 16:57:38 -0400 | [diff] [blame] | 2427 | for (const auto &alg : ctx->cert_compression_algs) { |
| 2428 | if (alg.alg_id == alg_id) { |
Adam Langley | 8596795 | 2018-07-03 08:04:58 -0700 | [diff] [blame] | 2429 | return 0; |
| 2430 | } |
| 2431 | } |
| 2432 | |
David Benjamin | 8fe1584 | 2019-10-08 16:57:38 -0400 | [diff] [blame] | 2433 | CertCompressionAlg alg; |
| 2434 | alg.alg_id = alg_id; |
| 2435 | alg.compress = compress; |
| 2436 | alg.decompress = decompress; |
| 2437 | return ctx->cert_compression_algs.Push(alg); |
Adam Langley | 8596795 | 2018-07-03 08:04:58 -0700 | [diff] [blame] | 2438 | } |
| 2439 | |
David Benjamin | eebd3c8 | 2016-12-06 17:43:58 -0500 | [diff] [blame] | 2440 | void SSL_CTX_set_tls_channel_id_enabled(SSL_CTX *ctx, int enabled) { |
David Benjamin | 4685376 | 2018-07-03 14:01:26 -0400 | [diff] [blame] | 2441 | ctx->channel_id_enabled = !!enabled; |
David Benjamin | eebd3c8 | 2016-12-06 17:43:58 -0500 | [diff] [blame] | 2442 | } |
| 2443 | |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 2444 | int SSL_CTX_enable_tls_channel_id(SSL_CTX *ctx) { |
David Benjamin | eebd3c8 | 2016-12-06 17:43:58 -0500 | [diff] [blame] | 2445 | SSL_CTX_set_tls_channel_id_enabled(ctx, 1); |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 2446 | return 1; |
| 2447 | } |
| 2448 | |
David Benjamin | eebd3c8 | 2016-12-06 17:43:58 -0500 | [diff] [blame] | 2449 | void SSL_set_tls_channel_id_enabled(SSL *ssl, int enabled) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 2450 | if (!ssl->config) { |
| 2451 | return; |
| 2452 | } |
David Benjamin | 4685376 | 2018-07-03 14:01:26 -0400 | [diff] [blame] | 2453 | ssl->config->channel_id_enabled = !!enabled; |
David Benjamin | eebd3c8 | 2016-12-06 17:43:58 -0500 | [diff] [blame] | 2454 | } |
| 2455 | |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 2456 | int SSL_enable_tls_channel_id(SSL *ssl) { |
David Benjamin | eebd3c8 | 2016-12-06 17:43:58 -0500 | [diff] [blame] | 2457 | SSL_set_tls_channel_id_enabled(ssl, 1); |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 2458 | return 1; |
| 2459 | } |
| 2460 | |
| 2461 | static int is_p256_key(EVP_PKEY *private_key) { |
| 2462 | const EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(private_key); |
| 2463 | return ec_key != NULL && |
| 2464 | EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key)) == |
| 2465 | NID_X9_62_prime256v1; |
| 2466 | } |
| 2467 | |
| 2468 | int SSL_CTX_set1_tls_channel_id(SSL_CTX *ctx, EVP_PKEY *private_key) { |
| 2469 | if (!is_p256_key(private_key)) { |
| 2470 | OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256); |
| 2471 | return 0; |
| 2472 | } |
| 2473 | |
David Benjamin | 4685376 | 2018-07-03 14:01:26 -0400 | [diff] [blame] | 2474 | ctx->channel_id_private = UpRef(private_key); |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 2475 | return 1; |
| 2476 | } |
| 2477 | |
| 2478 | int SSL_set1_tls_channel_id(SSL *ssl, EVP_PKEY *private_key) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 2479 | if (!ssl->config) { |
| 2480 | return 0; |
| 2481 | } |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 2482 | if (!is_p256_key(private_key)) { |
| 2483 | OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256); |
| 2484 | return 0; |
| 2485 | } |
| 2486 | |
David Benjamin | 4685376 | 2018-07-03 14:01:26 -0400 | [diff] [blame] | 2487 | ssl->config->channel_id_private = UpRef(private_key); |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 2488 | return 1; |
| 2489 | } |
| 2490 | |
| 2491 | size_t SSL_get_tls_channel_id(SSL *ssl, uint8_t *out, size_t max_out) { |
David Benjamin | 4685376 | 2018-07-03 14:01:26 -0400 | [diff] [blame] | 2492 | if (!ssl->s3->channel_id_valid) { |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 2493 | return 0; |
| 2494 | } |
David Benjamin | 4685376 | 2018-07-03 14:01:26 -0400 | [diff] [blame] | 2495 | OPENSSL_memcpy(out, ssl->s3->channel_id, (max_out < 64) ? max_out : 64); |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 2496 | return 64; |
| 2497 | } |
| 2498 | |
David Benjamin | bc118ee6 | 2018-07-10 17:27:45 -0400 | [diff] [blame] | 2499 | size_t SSL_get0_certificate_types(const SSL *ssl, const uint8_t **out_types) { |
| 2500 | Span<const uint8_t> types; |
| 2501 | if (!ssl->server && ssl->s3->hs != nullptr) { |
| 2502 | types = ssl->s3->hs->certificate_types; |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 2503 | } |
David Benjamin | bc118ee6 | 2018-07-10 17:27:45 -0400 | [diff] [blame] | 2504 | *out_types = types.data(); |
| 2505 | return types.size(); |
| 2506 | } |
| 2507 | |
| 2508 | size_t SSL_get0_peer_verify_algorithms(const SSL *ssl, |
| 2509 | const uint16_t **out_sigalgs) { |
| 2510 | Span<const uint16_t> sigalgs; |
| 2511 | if (ssl->s3->hs != nullptr) { |
| 2512 | sigalgs = ssl->s3->hs->peer_sigalgs; |
| 2513 | } |
| 2514 | *out_sigalgs = sigalgs.data(); |
| 2515 | return sigalgs.size(); |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 2516 | } |
| 2517 | |
Watson Ladd | dcd6e44 | 2020-08-10 15:12:45 -0400 | [diff] [blame] | 2518 | size_t SSL_get0_peer_delegation_algorithms(const SSL *ssl, |
| 2519 | const uint16_t **out_sigalgs){ |
| 2520 | Span<const uint16_t> sigalgs; |
| 2521 | if (ssl->s3->hs != nullptr) { |
| 2522 | sigalgs = ssl->s3->hs->peer_delegated_credential_sigalgs; |
| 2523 | } |
| 2524 | *out_sigalgs = sigalgs.data(); |
| 2525 | return sigalgs.size(); |
| 2526 | } |
| 2527 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2528 | EVP_PKEY *SSL_get_privatekey(const SSL *ssl) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 2529 | if (!ssl->config) { |
| 2530 | assert(ssl->config); |
David Benjamin | 281053e | 2024-02-13 22:52:54 -0500 | [diff] [blame] | 2531 | return nullptr; |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 2532 | } |
David Benjamin | 281053e | 2024-02-13 22:52:54 -0500 | [diff] [blame] | 2533 | return ssl->config->cert->privatekey.get(); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2534 | } |
| 2535 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2536 | EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) { |
David Benjamin | 281053e | 2024-02-13 22:52:54 -0500 | [diff] [blame] | 2537 | return ctx->cert->privatekey.get(); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2538 | } |
| 2539 | |
David Benjamin | 42fea37 | 2015-09-19 01:22:44 -0400 | [diff] [blame] | 2540 | const SSL_CIPHER *SSL_get_current_cipher(const SSL *ssl) { |
Steven Valdez | 384d0ea | 2018-11-06 10:45:36 -0500 | [diff] [blame] | 2541 | const SSL_SESSION *session = SSL_get_session(ssl); |
| 2542 | return session == nullptr ? nullptr : session->cipher; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2543 | } |
| 2544 | |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 2545 | int SSL_session_reused(const SSL *ssl) { |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 2546 | return ssl->s3->session_reused || SSL_in_early_data(ssl); |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 2547 | } |
| 2548 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2549 | const COMP_METHOD *SSL_get_current_compression(SSL *ssl) { return NULL; } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2550 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2551 | const COMP_METHOD *SSL_get_current_expansion(SSL *ssl) { return NULL; } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2552 | |
David Benjamin | 103ed08 | 2018-05-10 19:55:02 -0400 | [diff] [blame] | 2553 | int SSL_get_server_tmp_key(SSL *ssl, EVP_PKEY **out_key) { return 0; } |
Adam Langley | 29ec5d1 | 2016-03-01 16:12:28 -0800 | [diff] [blame] | 2554 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2555 | void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) { |
Adam Langley | bb85f3d | 2015-10-28 18:44:11 -0700 | [diff] [blame] | 2556 | ctx->quiet_shutdown = (mode != 0); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2557 | } |
| 2558 | |
| 2559 | int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) { |
| 2560 | return ctx->quiet_shutdown; |
| 2561 | } |
| 2562 | |
Adam Langley | bb85f3d | 2015-10-28 18:44:11 -0700 | [diff] [blame] | 2563 | void SSL_set_quiet_shutdown(SSL *ssl, int mode) { |
| 2564 | ssl->quiet_shutdown = (mode != 0); |
| 2565 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2566 | |
David Benjamin | 9f85949 | 2015-10-03 10:44:30 -0400 | [diff] [blame] | 2567 | int SSL_get_quiet_shutdown(const SSL *ssl) { return ssl->quiet_shutdown; } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2568 | |
David Benjamin | 63006a9 | 2015-10-18 00:00:06 -0400 | [diff] [blame] | 2569 | void SSL_set_shutdown(SSL *ssl, int mode) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2570 | // It is an error to clear any bits that have already been set. (We can't try |
| 2571 | // to get a second close_notify or send two.) |
David Benjamin | fa214e4 | 2016-05-10 17:03:10 -0400 | [diff] [blame] | 2572 | assert((SSL_get_shutdown(ssl) & mode) == SSL_get_shutdown(ssl)); |
David Benjamin | 63006a9 | 2015-10-18 00:00:06 -0400 | [diff] [blame] | 2573 | |
David Benjamin | fa214e4 | 2016-05-10 17:03:10 -0400 | [diff] [blame] | 2574 | if (mode & SSL_RECEIVED_SHUTDOWN && |
David Benjamin | 23c25d5 | 2017-10-06 16:02:47 -0400 | [diff] [blame] | 2575 | ssl->s3->read_shutdown == ssl_shutdown_none) { |
| 2576 | ssl->s3->read_shutdown = ssl_shutdown_close_notify; |
David Benjamin | fa214e4 | 2016-05-10 17:03:10 -0400 | [diff] [blame] | 2577 | } |
| 2578 | |
| 2579 | if (mode & SSL_SENT_SHUTDOWN && |
David Benjamin | 23c25d5 | 2017-10-06 16:02:47 -0400 | [diff] [blame] | 2580 | ssl->s3->write_shutdown == ssl_shutdown_none) { |
| 2581 | ssl->s3->write_shutdown = ssl_shutdown_close_notify; |
David Benjamin | fa214e4 | 2016-05-10 17:03:10 -0400 | [diff] [blame] | 2582 | } |
David Benjamin | 63006a9 | 2015-10-18 00:00:06 -0400 | [diff] [blame] | 2583 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2584 | |
David Benjamin | fa214e4 | 2016-05-10 17:03:10 -0400 | [diff] [blame] | 2585 | int SSL_get_shutdown(const SSL *ssl) { |
| 2586 | int ret = 0; |
David Benjamin | 23c25d5 | 2017-10-06 16:02:47 -0400 | [diff] [blame] | 2587 | if (ssl->s3->read_shutdown != ssl_shutdown_none) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2588 | // Historically, OpenSSL set |SSL_RECEIVED_SHUTDOWN| on both close_notify |
| 2589 | // and fatal alert. |
David Benjamin | fa214e4 | 2016-05-10 17:03:10 -0400 | [diff] [blame] | 2590 | ret |= SSL_RECEIVED_SHUTDOWN; |
| 2591 | } |
David Benjamin | 23c25d5 | 2017-10-06 16:02:47 -0400 | [diff] [blame] | 2592 | if (ssl->s3->write_shutdown == ssl_shutdown_close_notify) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2593 | // Historically, OpenSSL set |SSL_SENT_SHUTDOWN| on only close_notify. |
David Benjamin | fa214e4 | 2016-05-10 17:03:10 -0400 | [diff] [blame] | 2594 | ret |= SSL_SENT_SHUTDOWN; |
| 2595 | } |
| 2596 | return ret; |
| 2597 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2598 | |
David Benjamin | 50596f8 | 2018-07-02 19:47:27 -0400 | [diff] [blame] | 2599 | SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx.get(); } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2600 | |
| 2601 | SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 2602 | if (!ssl->config) { |
| 2603 | return NULL; |
| 2604 | } |
David Benjamin | 50596f8 | 2018-07-02 19:47:27 -0400 | [diff] [blame] | 2605 | if (ssl->ctx.get() == ctx) { |
| 2606 | return ssl->ctx.get(); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2607 | } |
| 2608 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2609 | // One cannot change the X.509 callbacks during a connection. |
Adam Langley | 3509dac | 2017-02-01 11:59:18 -0800 | [diff] [blame] | 2610 | if (ssl->ctx->x509_method != ctx->x509_method) { |
| 2611 | assert(0); |
| 2612 | return NULL; |
| 2613 | } |
| 2614 | |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 2615 | UniquePtr<CERT> new_cert = ssl_cert_dup(ctx->cert.get()); |
| 2616 | if (!new_cert) { |
| 2617 | return nullptr; |
| 2618 | } |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 2619 | |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 2620 | ssl->config->cert = std::move(new_cert); |
David Benjamin | 50596f8 | 2018-07-02 19:47:27 -0400 | [diff] [blame] | 2621 | ssl->ctx = UpRef(ctx); |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 2622 | ssl->enable_early_data = ssl->ctx->enable_early_data; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2623 | |
David Benjamin | 50596f8 | 2018-07-02 19:47:27 -0400 | [diff] [blame] | 2624 | return ssl->ctx.get(); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2625 | } |
| 2626 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2627 | void SSL_set_info_callback(SSL *ssl, |
David Benjamin | 8217024 | 2015-10-17 22:51:17 -0400 | [diff] [blame] | 2628 | void (*cb)(const SSL *ssl, int type, int value)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2629 | ssl->info_callback = cb; |
| 2630 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2631 | |
David Benjamin | 8217024 | 2015-10-17 22:51:17 -0400 | [diff] [blame] | 2632 | void (*SSL_get_info_callback(const SSL *ssl))(const SSL *ssl, int type, |
| 2633 | int value) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2634 | return ssl->info_callback; |
| 2635 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2636 | |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 2637 | int SSL_state(const SSL *ssl) { |
David Benjamin | 9e766d7 | 2017-02-10 23:14:17 -0500 | [diff] [blame] | 2638 | return SSL_in_init(ssl) ? SSL_ST_INIT : SSL_ST_OK; |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 2639 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2640 | |
David Benjamin | ece089c | 2015-05-15 23:52:42 -0400 | [diff] [blame] | 2641 | void SSL_set_state(SSL *ssl, int state) { } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2642 | |
Emily Stark | 95a79ee | 2016-02-25 21:12:28 -0800 | [diff] [blame] | 2643 | char *SSL_get_shared_ciphers(const SSL *ssl, char *buf, int len) { |
| 2644 | if (len <= 0) { |
| 2645 | return NULL; |
| 2646 | } |
| 2647 | buf[0] = '\0'; |
| 2648 | return buf; |
| 2649 | } |
| 2650 | |
Shelley Vohr | 3ab3b12 | 2019-10-21 09:51:32 -0700 | [diff] [blame] | 2651 | int SSL_get_shared_sigalgs(SSL *ssl, int idx, int *psign, int *phash, |
| 2652 | int *psignandhash, uint8_t *rsig, uint8_t *rhash) { |
| 2653 | return 0; |
| 2654 | } |
| 2655 | |
Steven Valdez | c8e0f90 | 2018-07-14 11:23:01 -0400 | [diff] [blame] | 2656 | int SSL_CTX_set_quic_method(SSL_CTX *ctx, const SSL_QUIC_METHOD *quic_method) { |
| 2657 | if (ctx->method->is_dtls) { |
| 2658 | return 0; |
| 2659 | } |
| 2660 | ctx->quic_method = quic_method; |
| 2661 | return 1; |
| 2662 | } |
| 2663 | |
Alessandro Ghedini | 3cbb029 | 2018-12-13 13:53:57 +0000 | [diff] [blame] | 2664 | int SSL_set_quic_method(SSL *ssl, const SSL_QUIC_METHOD *quic_method) { |
| 2665 | if (ssl->method->is_dtls) { |
| 2666 | return 0; |
| 2667 | } |
| 2668 | ssl->quic_method = quic_method; |
| 2669 | return 1; |
| 2670 | } |
| 2671 | |
David Benjamin | 8a58933 | 2015-12-04 23:14:35 -0500 | [diff] [blame] | 2672 | int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused, |
David Benjamin | d94682d | 2017-05-14 17:10:18 -0400 | [diff] [blame] | 2673 | CRYPTO_EX_dup *dup_unused, CRYPTO_EX_free *free_func) { |
David Benjamin | 9f33fc6 | 2015-04-15 17:29:53 -0400 | [diff] [blame] | 2674 | int index; |
| 2675 | if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp, |
David Benjamin | d94682d | 2017-05-14 17:10:18 -0400 | [diff] [blame] | 2676 | free_func)) { |
David Benjamin | 9f33fc6 | 2015-04-15 17:29:53 -0400 | [diff] [blame] | 2677 | return -1; |
| 2678 | } |
| 2679 | return index; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2680 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2681 | |
David Benjamin | 27e377e | 2017-07-31 19:09:42 -0400 | [diff] [blame] | 2682 | int SSL_set_ex_data(SSL *ssl, int idx, void *data) { |
| 2683 | return CRYPTO_set_ex_data(&ssl->ex_data, idx, data); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2684 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2685 | |
David Benjamin | 1ac08fe | 2015-09-15 01:36:22 -0400 | [diff] [blame] | 2686 | void *SSL_get_ex_data(const SSL *ssl, int idx) { |
| 2687 | return CRYPTO_get_ex_data(&ssl->ex_data, idx); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2688 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2689 | |
David Benjamin | 8a58933 | 2015-12-04 23:14:35 -0500 | [diff] [blame] | 2690 | int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused, |
David Benjamin | d94682d | 2017-05-14 17:10:18 -0400 | [diff] [blame] | 2691 | CRYPTO_EX_dup *dup_unused, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2692 | CRYPTO_EX_free *free_func) { |
David Benjamin | 9f33fc6 | 2015-04-15 17:29:53 -0400 | [diff] [blame] | 2693 | int index; |
| 2694 | if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp, |
David Benjamin | d94682d | 2017-05-14 17:10:18 -0400 | [diff] [blame] | 2695 | free_func)) { |
David Benjamin | 9f33fc6 | 2015-04-15 17:29:53 -0400 | [diff] [blame] | 2696 | return -1; |
| 2697 | } |
| 2698 | return index; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2699 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2700 | |
David Benjamin | 27e377e | 2017-07-31 19:09:42 -0400 | [diff] [blame] | 2701 | int SSL_CTX_set_ex_data(SSL_CTX *ctx, int idx, void *data) { |
| 2702 | return CRYPTO_set_ex_data(&ctx->ex_data, idx, data); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2703 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2704 | |
David Benjamin | 1ac08fe | 2015-09-15 01:36:22 -0400 | [diff] [blame] | 2705 | void *SSL_CTX_get_ex_data(const SSL_CTX *ctx, int idx) { |
| 2706 | return CRYPTO_get_ex_data(&ctx->ex_data, idx); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2707 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2708 | |
David Benjamin | ebd8b89 | 2022-07-27 18:41:51 -0700 | [diff] [blame] | 2709 | int SSL_want(const SSL *ssl) { |
| 2710 | // Historically, OpenSSL did not track |SSL_ERROR_ZERO_RETURN| as an |rwstate| |
| 2711 | // value. We do, but map it back to |SSL_ERROR_NONE| to preserve the original |
| 2712 | // behavior. |
| 2713 | return ssl->s3->rwstate == SSL_ERROR_ZERO_RETURN ? SSL_ERROR_NONE |
| 2714 | : ssl->s3->rwstate; |
| 2715 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2716 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2717 | void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx, |
| 2718 | RSA *(*cb)(SSL *ssl, int is_export, |
David Benjamin | 27e377e | 2017-07-31 19:09:42 -0400 | [diff] [blame] | 2719 | int keylength)) {} |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2720 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2721 | void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export, |
David Benjamin | 27e377e | 2017-07-31 19:09:42 -0400 | [diff] [blame] | 2722 | int keylength)) {} |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2723 | |
| 2724 | void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx, |
David Benjamin | 27e377e | 2017-07-31 19:09:42 -0400 | [diff] [blame] | 2725 | DH *(*cb)(SSL *ssl, int is_export, |
| 2726 | int keylength)) {} |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2727 | |
David Benjamin | 27e377e | 2017-07-31 19:09:42 -0400 | [diff] [blame] | 2728 | void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*cb)(SSL *ssl, int is_export, |
| 2729 | int keylength)) {} |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2730 | |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 2731 | static int use_psk_identity_hint(UniquePtr<char> *out, |
| 2732 | const char *identity_hint) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2733 | if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 2734 | OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2735 | return 0; |
| 2736 | } |
| 2737 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2738 | // Clear currently configured hint, if any. |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 2739 | out->reset(); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2740 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2741 | // Treat the empty hint as not supplying one. Plain PSK makes it possible to |
| 2742 | // send either no hint (omit ServerKeyExchange) or an empty hint, while |
| 2743 | // ECDHE_PSK can only spell empty hint. Having different capabilities is odd, |
| 2744 | // so we interpret empty and missing as identical. |
David Benjamin | 7867934 | 2016-09-16 19:42:05 -0400 | [diff] [blame] | 2745 | if (identity_hint != NULL && identity_hint[0] != '\0') { |
David Benjamin | 3ba9586 | 2019-10-21 16:14:33 -0400 | [diff] [blame] | 2746 | out->reset(OPENSSL_strdup(identity_hint)); |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 2747 | if (*out == nullptr) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2748 | return 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2749 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2750 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2751 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2752 | return 1; |
| 2753 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2754 | |
David Benjamin | 0a9bf66 | 2017-06-21 16:34:15 -0400 | [diff] [blame] | 2755 | int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) { |
| 2756 | return use_psk_identity_hint(&ctx->psk_identity_hint, identity_hint); |
| 2757 | } |
| 2758 | |
| 2759 | int SSL_use_psk_identity_hint(SSL *ssl, const char *identity_hint) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 2760 | if (!ssl->config) { |
| 2761 | return 0; |
| 2762 | } |
| 2763 | return use_psk_identity_hint(&ssl->config->psk_identity_hint, identity_hint); |
David Benjamin | 0a9bf66 | 2017-06-21 16:34:15 -0400 | [diff] [blame] | 2764 | } |
| 2765 | |
David Benjamin | e8814df | 2015-09-15 08:05:54 -0400 | [diff] [blame] | 2766 | const char *SSL_get_psk_identity_hint(const SSL *ssl) { |
| 2767 | if (ssl == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2768 | return NULL; |
| 2769 | } |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 2770 | if (ssl->config == NULL) { |
| 2771 | assert(ssl->config); |
| 2772 | return NULL; |
| 2773 | } |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 2774 | return ssl->config->psk_identity_hint.get(); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2775 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2776 | |
David Benjamin | e8814df | 2015-09-15 08:05:54 -0400 | [diff] [blame] | 2777 | const char *SSL_get_psk_identity(const SSL *ssl) { |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 2778 | if (ssl == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2779 | return NULL; |
| 2780 | } |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 2781 | SSL_SESSION *session = SSL_get_session(ssl); |
| 2782 | if (session == NULL) { |
| 2783 | return NULL; |
| 2784 | } |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 2785 | return session->psk_identity.get(); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2786 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2787 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2788 | void SSL_set_psk_client_callback( |
David Benjamin | e8814df | 2015-09-15 08:05:54 -0400 | [diff] [blame] | 2789 | SSL *ssl, unsigned (*cb)(SSL *ssl, const char *hint, char *identity, |
| 2790 | unsigned max_identity_len, uint8_t *psk, |
| 2791 | unsigned max_psk_len)) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 2792 | if (!ssl->config) { |
| 2793 | return; |
| 2794 | } |
| 2795 | ssl->config->psk_client_callback = cb; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2796 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2797 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2798 | void SSL_CTX_set_psk_client_callback( |
David Benjamin | e8814df | 2015-09-15 08:05:54 -0400 | [diff] [blame] | 2799 | SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *hint, char *identity, |
| 2800 | unsigned max_identity_len, uint8_t *psk, |
| 2801 | unsigned max_psk_len)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2802 | ctx->psk_client_callback = cb; |
| 2803 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2804 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2805 | void SSL_set_psk_server_callback( |
David Benjamin | e8814df | 2015-09-15 08:05:54 -0400 | [diff] [blame] | 2806 | SSL *ssl, unsigned (*cb)(SSL *ssl, const char *identity, uint8_t *psk, |
| 2807 | unsigned max_psk_len)) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 2808 | if (!ssl->config) { |
| 2809 | return; |
| 2810 | } |
| 2811 | ssl->config->psk_server_callback = cb; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2812 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2813 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2814 | void SSL_CTX_set_psk_server_callback( |
David Benjamin | e8814df | 2015-09-15 08:05:54 -0400 | [diff] [blame] | 2815 | SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *identity, |
| 2816 | uint8_t *psk, unsigned max_psk_len)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2817 | ctx->psk_server_callback = cb; |
| 2818 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2819 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2820 | void SSL_CTX_set_msg_callback(SSL_CTX *ctx, |
| 2821 | void (*cb)(int write_p, int version, |
| 2822 | int content_type, const void *buf, |
| 2823 | size_t len, SSL *ssl, void *arg)) { |
David Benjamin | 59015c3 | 2015-04-26 13:13:55 -0400 | [diff] [blame] | 2824 | ctx->msg_callback = cb; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2825 | } |
David Benjamin | 61ecccf | 2015-05-05 09:44:51 -0400 | [diff] [blame] | 2826 | |
| 2827 | void SSL_CTX_set_msg_callback_arg(SSL_CTX *ctx, void *arg) { |
| 2828 | ctx->msg_callback_arg = arg; |
| 2829 | } |
| 2830 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2831 | void SSL_set_msg_callback(SSL *ssl, |
| 2832 | void (*cb)(int write_p, int version, int content_type, |
| 2833 | const void *buf, size_t len, SSL *ssl, |
| 2834 | void *arg)) { |
David Benjamin | 59015c3 | 2015-04-26 13:13:55 -0400 | [diff] [blame] | 2835 | ssl->msg_callback = cb; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2836 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2837 | |
David Benjamin | 61ecccf | 2015-05-05 09:44:51 -0400 | [diff] [blame] | 2838 | void SSL_set_msg_callback_arg(SSL *ssl, void *arg) { |
| 2839 | ssl->msg_callback_arg = arg; |
| 2840 | } |
| 2841 | |
David Benjamin | d28f59c | 2015-11-17 22:32:50 -0500 | [diff] [blame] | 2842 | void SSL_CTX_set_keylog_callback(SSL_CTX *ctx, |
| 2843 | void (*cb)(const SSL *ssl, const char *line)) { |
| 2844 | ctx->keylog_callback = cb; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2845 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2846 | |
David Benjamin | 6e3f5cc | 2016-09-14 14:43:14 -0400 | [diff] [blame] | 2847 | void (*SSL_CTX_get_keylog_callback(const SSL_CTX *ctx))(const SSL *ssl, |
| 2848 | const char *line) { |
| 2849 | return ctx->keylog_callback; |
| 2850 | } |
| 2851 | |
Taylor Brandstetter | 9edb2c6 | 2016-06-08 15:26:59 -0700 | [diff] [blame] | 2852 | void SSL_CTX_set_current_time_cb(SSL_CTX *ctx, |
| 2853 | void (*cb)(const SSL *ssl, |
| 2854 | struct timeval *out_clock)) { |
| 2855 | ctx->current_time_cb = cb; |
| 2856 | } |
| 2857 | |
David Benjamin | 9b2cdb7 | 2021-04-01 23:21:53 -0400 | [diff] [blame] | 2858 | int SSL_can_release_private_key(const SSL *ssl) { |
| 2859 | if (ssl_can_renegotiate(ssl)) { |
| 2860 | // If the connection can renegotiate (client only), the private key may be |
| 2861 | // used in a future handshake. |
| 2862 | return 0; |
| 2863 | } |
| 2864 | |
| 2865 | // Otherwise, this is determined by the current handshake. |
| 2866 | return !ssl->s3->hs || ssl->s3->hs->can_release_private_key; |
| 2867 | } |
| 2868 | |
David Benjamin | 5d8b128 | 2015-10-17 23:26:35 -0400 | [diff] [blame] | 2869 | int SSL_is_init_finished(const SSL *ssl) { |
David Benjamin | 9e766d7 | 2017-02-10 23:14:17 -0500 | [diff] [blame] | 2870 | return !SSL_in_init(ssl); |
David Benjamin | 5d8b128 | 2015-10-17 23:26:35 -0400 | [diff] [blame] | 2871 | } |
| 2872 | |
| 2873 | int SSL_in_init(const SSL *ssl) { |
Steven Valdez | d816874 | 2017-08-31 10:15:48 -0400 | [diff] [blame] | 2874 | // This returns false once all the handshake state has been finalized, to |
| 2875 | // allow callbacks and getters based on SSL_in_init to return the correct |
| 2876 | // values. |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 2877 | SSL_HANDSHAKE *hs = ssl->s3->hs.get(); |
Steven Valdez | d816874 | 2017-08-31 10:15:48 -0400 | [diff] [blame] | 2878 | return hs != nullptr && !hs->handshake_finalized; |
David Benjamin | 5d8b128 | 2015-10-17 23:26:35 -0400 | [diff] [blame] | 2879 | } |
| 2880 | |
| 2881 | int SSL_in_false_start(const SSL *ssl) { |
David Benjamin | a048678 | 2016-10-06 19:11:32 -0400 | [diff] [blame] | 2882 | if (ssl->s3->hs == NULL) { |
| 2883 | return 0; |
| 2884 | } |
| 2885 | return ssl->s3->hs->in_false_start; |
David Benjamin | ed7c475 | 2015-02-16 19:16:46 -0500 | [diff] [blame] | 2886 | } |
| 2887 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2888 | int SSL_cutthrough_complete(const SSL *ssl) { |
| 2889 | return SSL_in_false_start(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2890 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2891 | |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 2892 | int SSL_is_server(const SSL *ssl) { return ssl->server; } |
| 2893 | |
| 2894 | int SSL_is_dtls(const SSL *ssl) { return ssl->method->is_dtls; } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2895 | |
Alessandro Ghedini | 57e81e6 | 2017-03-14 23:36:00 +0000 | [diff] [blame] | 2896 | void SSL_CTX_set_select_certificate_cb( |
| 2897 | SSL_CTX *ctx, |
| 2898 | enum ssl_select_cert_result_t (*cb)(const SSL_CLIENT_HELLO *)) { |
David Benjamin | d4c2bce | 2015-10-17 12:28:18 -0400 | [diff] [blame] | 2899 | ctx->select_certificate_cb = cb; |
| 2900 | } |
| 2901 | |
David Benjamin | 731058e | 2016-12-03 23:15:13 -0500 | [diff] [blame] | 2902 | void SSL_CTX_set_dos_protection_cb(SSL_CTX *ctx, |
| 2903 | int (*cb)(const SSL_CLIENT_HELLO *)) { |
Adam Langley | 524e717 | 2015-02-20 16:04:00 -0800 | [diff] [blame] | 2904 | ctx->dos_protection_cb = cb; |
| 2905 | } |
| 2906 | |
Jesse Selover | 1c337e5 | 2018-08-10 13:28:47 -0400 | [diff] [blame] | 2907 | void SSL_CTX_set_reverify_on_resume(SSL_CTX *ctx, int enabled) { |
| 2908 | ctx->reverify_on_resume = !!enabled; |
| 2909 | } |
| 2910 | |
Jesse Selover | d7266ec | 2019-01-30 16:06:10 -0500 | [diff] [blame] | 2911 | void SSL_set_enforce_rsa_key_usage(SSL *ssl, int enabled) { |
| 2912 | if (!ssl->config) { |
| 2913 | return; |
| 2914 | } |
| 2915 | ssl->config->enforce_rsa_key_usage = !!enabled; |
| 2916 | } |
| 2917 | |
David Benjamin | a614d46 | 2022-12-02 15:30:15 -0500 | [diff] [blame] | 2918 | int SSL_was_key_usage_invalid(const SSL *ssl) { |
| 2919 | return ssl->s3->was_key_usage_invalid; |
| 2920 | } |
| 2921 | |
David Benjamin | 1d5ef3b | 2015-10-12 19:54:18 -0400 | [diff] [blame] | 2922 | void SSL_set_renegotiate_mode(SSL *ssl, enum ssl_renegotiate_mode_t mode) { |
| 2923 | ssl->renegotiate_mode = mode; |
David Benjamin | 71666cb | 2018-05-01 17:14:27 -0400 | [diff] [blame] | 2924 | |
| 2925 | // Check if |ssl_can_renegotiate| has changed and the configuration may now be |
| 2926 | // shed. HTTP clients may initially allow renegotiation for HTTP/1.1, and then |
| 2927 | // disable after the handshake once the ALPN protocol is known to be HTTP/2. |
| 2928 | ssl_maybe_shed_handshake_config(ssl); |
David Benjamin | 1d5ef3b | 2015-10-12 19:54:18 -0400 | [diff] [blame] | 2929 | } |
| 2930 | |
Adam Langley | c2d3280 | 2015-11-03 18:36:10 -0800 | [diff] [blame] | 2931 | int SSL_get_ivs(const SSL *ssl, const uint8_t **out_read_iv, |
| 2932 | const uint8_t **out_write_iv, size_t *out_iv_len) { |
Adam Langley | c2d3280 | 2015-11-03 18:36:10 -0800 | [diff] [blame] | 2933 | size_t write_iv_len; |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 2934 | if (!ssl->s3->aead_read_ctx->GetIV(out_read_iv, out_iv_len) || |
| 2935 | !ssl->s3->aead_write_ctx->GetIV(out_write_iv, &write_iv_len) || |
Adam Langley | c2d3280 | 2015-11-03 18:36:10 -0800 | [diff] [blame] | 2936 | *out_iv_len != write_iv_len) { |
| 2937 | return 0; |
| 2938 | } |
| 2939 | |
| 2940 | return 1; |
David Benjamin | e348ff4 | 2015-11-06 17:55:30 -0500 | [diff] [blame] | 2941 | } |
David Benjamin | 6e80765 | 2015-11-02 12:02:20 -0500 | [diff] [blame] | 2942 | |
David Benjamin | 91b2501 | 2016-01-21 16:39:58 -0500 | [diff] [blame] | 2943 | uint64_t SSL_get_read_sequence(const SSL *ssl) { |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 2944 | if (SSL_is_dtls(ssl)) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2945 | // max_seq_num already includes the epoch. |
David Benjamin | de94238 | 2016-02-11 12:02:01 -0500 | [diff] [blame] | 2946 | assert(ssl->d1->r_epoch == (ssl->d1->bitmap.max_seq_num >> 48)); |
| 2947 | return ssl->d1->bitmap.max_seq_num; |
| 2948 | } |
David Benjamin | 32013e8 | 2022-09-22 16:55:34 -0400 | [diff] [blame] | 2949 | return ssl->s3->read_sequence; |
David Benjamin | 91b2501 | 2016-01-21 16:39:58 -0500 | [diff] [blame] | 2950 | } |
| 2951 | |
| 2952 | uint64_t SSL_get_write_sequence(const SSL *ssl) { |
David Benjamin | 32013e8 | 2022-09-22 16:55:34 -0400 | [diff] [blame] | 2953 | uint64_t ret = ssl->s3->write_sequence; |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 2954 | if (SSL_is_dtls(ssl)) { |
David Benjamin | de94238 | 2016-02-11 12:02:01 -0500 | [diff] [blame] | 2955 | assert((ret >> 48) == 0); |
David Benjamin | 32013e8 | 2022-09-22 16:55:34 -0400 | [diff] [blame] | 2956 | ret |= uint64_t{ssl->d1->w_epoch} << 48; |
David Benjamin | de94238 | 2016-02-11 12:02:01 -0500 | [diff] [blame] | 2957 | } |
| 2958 | return ret; |
David Benjamin | 91b2501 | 2016-01-21 16:39:58 -0500 | [diff] [blame] | 2959 | } |
| 2960 | |
Steven Valdez | 0256385 | 2016-06-23 13:33:05 -0400 | [diff] [blame] | 2961 | uint16_t SSL_get_peer_signature_algorithm(const SSL *ssl) { |
David Benjamin | f1050fd | 2016-12-13 20:05:36 -0500 | [diff] [blame] | 2962 | SSL_SESSION *session = SSL_get_session(ssl); |
| 2963 | if (session == NULL) { |
| 2964 | return 0; |
| 2965 | } |
| 2966 | |
| 2967 | return session->peer_signature_algorithm; |
Steven Valdez | 0256385 | 2016-06-23 13:33:05 -0400 | [diff] [blame] | 2968 | } |
| 2969 | |
David Benjamin | 96ba15f | 2015-12-25 14:10:17 -0500 | [diff] [blame] | 2970 | size_t SSL_get_client_random(const SSL *ssl, uint8_t *out, size_t max_out) { |
| 2971 | if (max_out == 0) { |
| 2972 | return sizeof(ssl->s3->client_random); |
| 2973 | } |
| 2974 | if (max_out > sizeof(ssl->s3->client_random)) { |
| 2975 | max_out = sizeof(ssl->s3->client_random); |
| 2976 | } |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 2977 | OPENSSL_memcpy(out, ssl->s3->client_random, max_out); |
David Benjamin | 96ba15f | 2015-12-25 14:10:17 -0500 | [diff] [blame] | 2978 | return max_out; |
| 2979 | } |
| 2980 | |
| 2981 | size_t SSL_get_server_random(const SSL *ssl, uint8_t *out, size_t max_out) { |
| 2982 | if (max_out == 0) { |
| 2983 | return sizeof(ssl->s3->server_random); |
| 2984 | } |
| 2985 | if (max_out > sizeof(ssl->s3->server_random)) { |
| 2986 | max_out = sizeof(ssl->s3->server_random); |
| 2987 | } |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 2988 | OPENSSL_memcpy(out, ssl->s3->server_random, max_out); |
David Benjamin | 96ba15f | 2015-12-25 14:10:17 -0500 | [diff] [blame] | 2989 | return max_out; |
| 2990 | } |
| 2991 | |
David Benjamin | f6494f4 | 2016-01-07 12:37:41 -0800 | [diff] [blame] | 2992 | const SSL_CIPHER *SSL_get_pending_cipher(const SSL *ssl) { |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 2993 | SSL_HANDSHAKE *hs = ssl->s3->hs.get(); |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 2994 | if (hs == NULL) { |
David Benjamin | f6494f4 | 2016-01-07 12:37:41 -0800 | [diff] [blame] | 2995 | return NULL; |
| 2996 | } |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 2997 | return hs->new_cipher; |
David Benjamin | f6494f4 | 2016-01-07 12:37:41 -0800 | [diff] [blame] | 2998 | } |
| 2999 | |
David Benjamin | bbaf367 | 2016-11-17 10:53:09 +0900 | [diff] [blame] | 3000 | void SSL_set_retain_only_sha256_of_client_certs(SSL *ssl, int enabled) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 3001 | if (!ssl->config) { |
| 3002 | return; |
| 3003 | } |
| 3004 | ssl->config->retain_only_sha256_of_client_certs = !!enabled; |
David Benjamin | bbaf367 | 2016-11-17 10:53:09 +0900 | [diff] [blame] | 3005 | } |
| 3006 | |
David Benjamin | cdd0b7e | 2016-01-07 10:10:41 -0800 | [diff] [blame] | 3007 | void SSL_CTX_set_retain_only_sha256_of_client_certs(SSL_CTX *ctx, int enabled) { |
| 3008 | ctx->retain_only_sha256_of_client_certs = !!enabled; |
| 3009 | } |
| 3010 | |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 3011 | void SSL_CTX_set_grease_enabled(SSL_CTX *ctx, int enabled) { |
| 3012 | ctx->grease_enabled = !!enabled; |
| 3013 | } |
| 3014 | |
David Benjamin | e9c5d72 | 2021-06-09 17:43:16 -0400 | [diff] [blame] | 3015 | void SSL_CTX_set_permute_extensions(SSL_CTX *ctx, int enabled) { |
| 3016 | ctx->permute_extensions = !!enabled; |
| 3017 | } |
| 3018 | |
| 3019 | void SSL_set_permute_extensions(SSL *ssl, int enabled) { |
| 3020 | if (!ssl->config) { |
| 3021 | return; |
| 3022 | } |
| 3023 | ssl->config->permute_extensions = !!enabled; |
| 3024 | } |
| 3025 | |
David Benjamin | 35ac5b7 | 2017-03-03 15:05:56 -0500 | [diff] [blame] | 3026 | int32_t SSL_get_ticket_age_skew(const SSL *ssl) { |
| 3027 | return ssl->s3->ticket_age_skew; |
| 3028 | } |
| 3029 | |
David Benjamin | 494e4d0 | 2017-12-01 13:05:12 -0500 | [diff] [blame] | 3030 | void SSL_CTX_set_false_start_allowed_without_alpn(SSL_CTX *ctx, int allowed) { |
| 3031 | ctx->false_start_allowed_without_alpn = !!allowed; |
| 3032 | } |
| 3033 | |
Kris Kwiatkowski | b11902a | 2019-08-24 11:01:04 +0100 | [diff] [blame] | 3034 | int SSL_used_hello_retry_request(const SSL *ssl) { |
| 3035 | return ssl->s3->used_hello_retry_request; |
| 3036 | } |
| 3037 | |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 3038 | void SSL_set_shed_handshake_config(SSL *ssl, int enable) { |
| 3039 | if (!ssl->config) { |
| 3040 | return; |
| 3041 | } |
| 3042 | ssl->config->shed_handshake_config = !!enable; |
| 3043 | } |
| 3044 | |
David Benjamin | 6965d25 | 2018-11-19 15:49:56 -0600 | [diff] [blame] | 3045 | void SSL_set_jdk11_workaround(SSL *ssl, int enable) { |
| 3046 | if (!ssl->config) { |
| 3047 | return; |
| 3048 | } |
| 3049 | ssl->config->jdk11_workaround = !!enable; |
| 3050 | } |
| 3051 | |
David Schinazi | 3d8b8c3 | 2021-01-14 11:25:49 -0800 | [diff] [blame] | 3052 | void SSL_set_quic_use_legacy_codepoint(SSL *ssl, int use_legacy) { |
| 3053 | if (!ssl->config) { |
| 3054 | return; |
| 3055 | } |
| 3056 | ssl->config->quic_use_legacy_codepoint = !!use_legacy; |
| 3057 | } |
| 3058 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 3059 | int SSL_clear(SSL *ssl) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 3060 | if (!ssl->config) { |
| 3061 | return 0; // SSL_clear may not be used after shedding config. |
| 3062 | } |
| 3063 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3064 | // In OpenSSL, reusing a client |SSL| with |SSL_clear| causes the previously |
| 3065 | // established session to be offered the next time around. wpa_supplicant |
| 3066 | // depends on this behavior, so emulate it. |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 3067 | UniquePtr<SSL_SESSION> session; |
David Benjamin | b79cc84 | 2016-12-07 15:57:14 -0500 | [diff] [blame] | 3068 | if (!ssl->server && ssl->s3->established_session != NULL) { |
David Benjamin | 2908dd1 | 2018-06-29 17:46:42 -0400 | [diff] [blame] | 3069 | session = UpRef(ssl->s3->established_session); |
David Benjamin | b79cc84 | 2016-12-07 15:57:14 -0500 | [diff] [blame] | 3070 | } |
| 3071 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3072 | // The ssl->d1->mtu is simultaneously configuration (preserved across |
| 3073 | // clear) and connection-specific state (gets reset). |
| 3074 | // |
| 3075 | // TODO(davidben): Avoid this. |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 3076 | unsigned mtu = 0; |
| 3077 | if (ssl->d1 != NULL) { |
| 3078 | mtu = ssl->d1->mtu; |
| 3079 | } |
| 3080 | |
| 3081 | ssl->method->ssl_free(ssl); |
| 3082 | if (!ssl->method->ssl_new(ssl)) { |
| 3083 | return 0; |
| 3084 | } |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 3085 | |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 3086 | if (SSL_is_dtls(ssl) && (SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) { |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 3087 | ssl->d1->mtu = mtu; |
| 3088 | } |
| 3089 | |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 3090 | if (session != nullptr) { |
| 3091 | SSL_set_session(ssl, session.get()); |
David Benjamin | b79cc84 | 2016-12-07 15:57:14 -0500 | [diff] [blame] | 3092 | } |
| 3093 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 3094 | return 1; |
| 3095 | } |
| 3096 | |
David Benjamin | da881e9 | 2015-04-26 14:45:04 -0400 | [diff] [blame] | 3097 | int SSL_CTX_sess_connect(const SSL_CTX *ctx) { return 0; } |
| 3098 | int SSL_CTX_sess_connect_good(const SSL_CTX *ctx) { return 0; } |
| 3099 | int SSL_CTX_sess_connect_renegotiate(const SSL_CTX *ctx) { return 0; } |
| 3100 | int SSL_CTX_sess_accept(const SSL_CTX *ctx) { return 0; } |
| 3101 | int SSL_CTX_sess_accept_renegotiate(const SSL_CTX *ctx) { return 0; } |
| 3102 | int SSL_CTX_sess_accept_good(const SSL_CTX *ctx) { return 0; } |
| 3103 | int SSL_CTX_sess_hits(const SSL_CTX *ctx) { return 0; } |
| 3104 | int SSL_CTX_sess_cb_hits(const SSL_CTX *ctx) { return 0; } |
| 3105 | int SSL_CTX_sess_misses(const SSL_CTX *ctx) { return 0; } |
| 3106 | int SSL_CTX_sess_timeouts(const SSL_CTX *ctx) { return 0; } |
| 3107 | int SSL_CTX_sess_cache_full(const SSL_CTX *ctx) { return 0; } |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 3108 | |
| 3109 | int SSL_num_renegotiations(const SSL *ssl) { |
| 3110 | return SSL_total_renegotiations(ssl); |
| 3111 | } |
| 3112 | |
| 3113 | int SSL_CTX_need_tmp_RSA(const SSL_CTX *ctx) { return 0; } |
| 3114 | int SSL_need_tmp_RSA(const SSL *ssl) { return 0; } |
| 3115 | int SSL_CTX_set_tmp_rsa(SSL_CTX *ctx, const RSA *rsa) { return 1; } |
| 3116 | int SSL_set_tmp_rsa(SSL *ssl, const RSA *rsa) { return 1; } |
Matt Braithwaite | 4838d8a | 2015-08-20 13:20:03 -0700 | [diff] [blame] | 3117 | void ERR_load_SSL_strings(void) {} |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 3118 | void SSL_load_error_strings(void) {} |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 3119 | int SSL_cache_hit(SSL *ssl) { return SSL_session_reused(ssl); } |
| 3120 | |
| 3121 | int SSL_CTX_set_tmp_ecdh(SSL_CTX *ctx, const EC_KEY *ec_key) { |
| 3122 | if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) { |
| 3123 | OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER); |
| 3124 | return 0; |
| 3125 | } |
| 3126 | int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key)); |
David Benjamin | 2da5ba9 | 2023-05-26 21:23:39 -0400 | [diff] [blame] | 3127 | return SSL_CTX_set1_groups(ctx, &nid, 1); |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 3128 | } |
| 3129 | |
| 3130 | int SSL_set_tmp_ecdh(SSL *ssl, const EC_KEY *ec_key) { |
| 3131 | if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) { |
| 3132 | OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER); |
| 3133 | return 0; |
| 3134 | } |
| 3135 | int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key)); |
David Benjamin | 2da5ba9 | 2023-05-26 21:23:39 -0400 | [diff] [blame] | 3136 | return SSL_set1_groups(ssl, &nid, 1); |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 3137 | } |
David Benjamin | 721e8b7 | 2016-08-03 13:13:17 -0400 | [diff] [blame] | 3138 | |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 3139 | void SSL_CTX_set_ticket_aead_method(SSL_CTX *ctx, |
| 3140 | const SSL_TICKET_AEAD_METHOD *aead_method) { |
| 3141 | ctx->ticket_aead_method = aead_method; |
| 3142 | } |
David Benjamin | 103ed08 | 2018-05-10 19:55:02 -0400 | [diff] [blame] | 3143 | |
Adam Langley | 53a17f5 | 2020-05-26 14:44:07 -0700 | [diff] [blame] | 3144 | SSL_SESSION *SSL_process_tls13_new_session_ticket(SSL *ssl, const uint8_t *buf, |
| 3145 | size_t buf_len) { |
| 3146 | if (SSL_in_init(ssl) || |
| 3147 | ssl_protocol_version(ssl) != TLS1_3_VERSION || |
| 3148 | ssl->server) { |
| 3149 | // Only TLS 1.3 clients are supported. |
| 3150 | OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 3151 | return nullptr; |
| 3152 | } |
| 3153 | |
| 3154 | CBS cbs, body; |
| 3155 | CBS_init(&cbs, buf, buf_len); |
| 3156 | uint8_t type; |
| 3157 | if (!CBS_get_u8(&cbs, &type) || |
| 3158 | !CBS_get_u24_length_prefixed(&cbs, &body) || |
| 3159 | CBS_len(&cbs) != 0) { |
| 3160 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 3161 | return nullptr; |
| 3162 | } |
| 3163 | |
| 3164 | UniquePtr<SSL_SESSION> session = tls13_create_session_with_ticket(ssl, &body); |
| 3165 | if (!session) { |
| 3166 | // |tls13_create_session_with_ticket| puts the correct error. |
| 3167 | return nullptr; |
| 3168 | } |
| 3169 | return session.release(); |
| 3170 | } |
| 3171 | |
David Benjamin | 3f180b8 | 2022-05-09 17:45:18 -0400 | [diff] [blame] | 3172 | int SSL_CTX_set_num_tickets(SSL_CTX *ctx, size_t num_tickets) { |
| 3173 | num_tickets = std::min(num_tickets, kMaxTickets); |
| 3174 | static_assert(kMaxTickets <= 0xff, "Too many tickets."); |
| 3175 | ctx->num_tickets = static_cast<uint8_t>(num_tickets); |
| 3176 | return 1; |
| 3177 | } |
| 3178 | |
David Benjamin | 5697a92 | 2022-07-21 10:35:59 -0700 | [diff] [blame] | 3179 | size_t SSL_CTX_get_num_tickets(const SSL_CTX *ctx) { return ctx->num_tickets; } |
| 3180 | |
David Benjamin | 103ed08 | 2018-05-10 19:55:02 -0400 | [diff] [blame] | 3181 | int SSL_set_tlsext_status_type(SSL *ssl, int type) { |
| 3182 | if (!ssl->config) { |
| 3183 | return 0; |
| 3184 | } |
| 3185 | ssl->config->ocsp_stapling_enabled = type == TLSEXT_STATUSTYPE_ocsp; |
| 3186 | return 1; |
| 3187 | } |
| 3188 | |
Jeremy Apthorp | c0c9001 | 2018-10-18 14:18:05 -0700 | [diff] [blame] | 3189 | int SSL_get_tlsext_status_type(const SSL *ssl) { |
| 3190 | if (ssl->server) { |
| 3191 | SSL_HANDSHAKE *hs = ssl->s3->hs.get(); |
| 3192 | return hs != nullptr && hs->ocsp_stapling_requested |
| 3193 | ? TLSEXT_STATUSTYPE_ocsp |
| 3194 | : TLSEXT_STATUSTYPE_nothing; |
| 3195 | } |
| 3196 | |
| 3197 | return ssl->config != nullptr && ssl->config->ocsp_stapling_enabled |
| 3198 | ? TLSEXT_STATUSTYPE_ocsp |
| 3199 | : TLSEXT_STATUSTYPE_nothing; |
| 3200 | } |
| 3201 | |
David Benjamin | 103ed08 | 2018-05-10 19:55:02 -0400 | [diff] [blame] | 3202 | int SSL_set_tlsext_status_ocsp_resp(SSL *ssl, uint8_t *resp, size_t resp_len) { |
| 3203 | if (SSL_set_ocsp_response(ssl, resp, resp_len)) { |
| 3204 | OPENSSL_free(resp); |
| 3205 | return 1; |
| 3206 | } |
| 3207 | return 0; |
| 3208 | } |
| 3209 | |
| 3210 | size_t SSL_get_tlsext_status_ocsp_resp(const SSL *ssl, const uint8_t **out) { |
| 3211 | size_t ret; |
| 3212 | SSL_get0_ocsp_response(ssl, out, &ret); |
| 3213 | return ret; |
| 3214 | } |
| 3215 | |
| 3216 | int SSL_CTX_set_tlsext_status_cb(SSL_CTX *ctx, |
| 3217 | int (*callback)(SSL *ssl, void *arg)) { |
| 3218 | ctx->legacy_ocsp_callback = callback; |
| 3219 | return 1; |
| 3220 | } |
| 3221 | |
| 3222 | int SSL_CTX_set_tlsext_status_arg(SSL_CTX *ctx, void *arg) { |
| 3223 | ctx->legacy_ocsp_callback_arg = arg; |
| 3224 | return 1; |
| 3225 | } |
Adam Langley | 451ea3c | 2022-05-18 16:15:49 -0700 | [diff] [blame] | 3226 | |
David Benjamin | 4e88a35 | 2023-06-26 15:24:49 -0400 | [diff] [blame] | 3227 | uint16_t SSL_get_curve_id(const SSL *ssl) { return SSL_get_group_id(ssl); } |
| 3228 | |
| 3229 | const char *SSL_get_curve_name(uint16_t curve_id) { |
| 3230 | return SSL_get_group_name(curve_id); |
| 3231 | } |
| 3232 | |
| 3233 | size_t SSL_get_all_curve_names(const char **out, size_t max_out) { |
| 3234 | return SSL_get_all_group_names(out, max_out); |
| 3235 | } |
| 3236 | |
| 3237 | int SSL_CTX_set1_curves(SSL_CTX *ctx, const int *curves, size_t num_curves) { |
| 3238 | return SSL_CTX_set1_groups(ctx, curves, num_curves); |
| 3239 | } |
| 3240 | |
| 3241 | int SSL_set1_curves(SSL *ssl, const int *curves, size_t num_curves) { |
| 3242 | return SSL_set1_groups(ssl, curves, num_curves); |
| 3243 | } |
| 3244 | |
| 3245 | int SSL_CTX_set1_curves_list(SSL_CTX *ctx, const char *curves) { |
| 3246 | return SSL_CTX_set1_groups_list(ctx, curves); |
| 3247 | } |
| 3248 | |
| 3249 | int SSL_set1_curves_list(SSL *ssl, const char *curves) { |
| 3250 | return SSL_set1_groups_list(ssl, curves); |
| 3251 | } |
| 3252 | |
Adam Langley | 451ea3c | 2022-05-18 16:15:49 -0700 | [diff] [blame] | 3253 | namespace fips202205 { |
| 3254 | |
| 3255 | // (References are to SP 800-52r2): |
| 3256 | |
| 3257 | // Section 3.4.2.2 |
| 3258 | // "at least one of the NIST-approved curves, P-256 (secp256r1) and P384 |
| 3259 | // (secp384r1), shall be supported as described in RFC 8422." |
| 3260 | // |
| 3261 | // Section 3.3.1 |
| 3262 | // "The server shall be configured to only use cipher suites that are |
| 3263 | // composed entirely of NIST approved algorithms" |
David Benjamin | 6cf9820 | 2023-05-26 22:03:30 -0400 | [diff] [blame] | 3264 | static const uint16_t kGroups[] = {SSL_GROUP_SECP256R1, SSL_GROUP_SECP384R1}; |
Adam Langley | 451ea3c | 2022-05-18 16:15:49 -0700 | [diff] [blame] | 3265 | |
| 3266 | static const uint16_t kSigAlgs[] = { |
| 3267 | SSL_SIGN_RSA_PKCS1_SHA256, |
| 3268 | SSL_SIGN_RSA_PKCS1_SHA384, |
| 3269 | SSL_SIGN_RSA_PKCS1_SHA512, |
| 3270 | // Table 4.1: |
| 3271 | // "The curve should be P-256 or P-384" |
| 3272 | SSL_SIGN_ECDSA_SECP256R1_SHA256, |
| 3273 | SSL_SIGN_ECDSA_SECP384R1_SHA384, |
| 3274 | SSL_SIGN_RSA_PSS_RSAE_SHA256, |
| 3275 | SSL_SIGN_RSA_PSS_RSAE_SHA384, |
| 3276 | SSL_SIGN_RSA_PSS_RSAE_SHA512, |
| 3277 | }; |
| 3278 | |
| 3279 | static const char kTLS12Ciphers[] = |
| 3280 | "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256:" |
| 3281 | "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256:" |
| 3282 | "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384:" |
| 3283 | "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384"; |
| 3284 | |
| 3285 | static int Configure(SSL_CTX *ctx) { |
Adam Langley | 2f6409e | 2023-04-10 21:09:11 +0000 | [diff] [blame] | 3286 | ctx->tls13_cipher_policy = ssl_compliance_policy_fips_202205; |
Adam Langley | 451ea3c | 2022-05-18 16:15:49 -0700 | [diff] [blame] | 3287 | |
| 3288 | return |
| 3289 | // Section 3.1: |
| 3290 | // "Servers that support government-only applications shall be |
| 3291 | // configured to use TLS 1.2 and should be configured to use TLS 1.3 |
| 3292 | // as well. These servers should not be configured to use TLS 1.1 and |
| 3293 | // shall not use TLS 1.0, SSL 3.0, or SSL 2.0. |
| 3294 | SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION) && |
| 3295 | SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION) && |
| 3296 | // Sections 3.3.1.1.1 and 3.3.1.1.2 are ambiguous about whether |
| 3297 | // HMAC-SHA-1 cipher suites are permitted with TLS 1.2. However, later the |
| 3298 | // Encrypt-then-MAC extension is required for all CBC cipher suites and so |
| 3299 | // it's easier to drop them. |
| 3300 | SSL_CTX_set_strict_cipher_list(ctx, kTLS12Ciphers) && |
David Benjamin | 6cf9820 | 2023-05-26 22:03:30 -0400 | [diff] [blame] | 3301 | SSL_CTX_set1_group_ids(ctx, kGroups, OPENSSL_ARRAY_SIZE(kGroups)) && |
Adam Langley | 451ea3c | 2022-05-18 16:15:49 -0700 | [diff] [blame] | 3302 | SSL_CTX_set_signing_algorithm_prefs(ctx, kSigAlgs, |
| 3303 | OPENSSL_ARRAY_SIZE(kSigAlgs)) && |
| 3304 | SSL_CTX_set_verify_algorithm_prefs(ctx, kSigAlgs, |
| 3305 | OPENSSL_ARRAY_SIZE(kSigAlgs)); |
| 3306 | } |
| 3307 | |
| 3308 | static int Configure(SSL *ssl) { |
Adam Langley | 2f6409e | 2023-04-10 21:09:11 +0000 | [diff] [blame] | 3309 | ssl->config->tls13_cipher_policy = ssl_compliance_policy_fips_202205; |
Adam Langley | 451ea3c | 2022-05-18 16:15:49 -0700 | [diff] [blame] | 3310 | |
| 3311 | // See |Configure(SSL_CTX)|, above, for reasoning. |
| 3312 | return SSL_set_min_proto_version(ssl, TLS1_2_VERSION) && |
| 3313 | SSL_set_max_proto_version(ssl, TLS1_3_VERSION) && |
| 3314 | SSL_set_strict_cipher_list(ssl, kTLS12Ciphers) && |
David Benjamin | 6cf9820 | 2023-05-26 22:03:30 -0400 | [diff] [blame] | 3315 | SSL_set1_group_ids(ssl, kGroups, OPENSSL_ARRAY_SIZE(kGroups)) && |
Adam Langley | 451ea3c | 2022-05-18 16:15:49 -0700 | [diff] [blame] | 3316 | SSL_set_signing_algorithm_prefs(ssl, kSigAlgs, |
| 3317 | OPENSSL_ARRAY_SIZE(kSigAlgs)) && |
| 3318 | SSL_set_verify_algorithm_prefs(ssl, kSigAlgs, |
| 3319 | OPENSSL_ARRAY_SIZE(kSigAlgs)); |
| 3320 | } |
| 3321 | |
| 3322 | } // namespace fips202205 |
| 3323 | |
Adam Langley | 2f6409e | 2023-04-10 21:09:11 +0000 | [diff] [blame] | 3324 | namespace wpa202304 { |
| 3325 | |
| 3326 | // See WPA version 3.1, section 3.5. |
| 3327 | |
David Benjamin | 6cf9820 | 2023-05-26 22:03:30 -0400 | [diff] [blame] | 3328 | static const uint16_t kGroups[] = {SSL_GROUP_SECP384R1}; |
Adam Langley | 2f6409e | 2023-04-10 21:09:11 +0000 | [diff] [blame] | 3329 | |
| 3330 | static const uint16_t kSigAlgs[] = { |
| 3331 | SSL_SIGN_RSA_PKCS1_SHA384, // |
| 3332 | SSL_SIGN_RSA_PKCS1_SHA512, // |
| 3333 | SSL_SIGN_ECDSA_SECP384R1_SHA384, // |
| 3334 | SSL_SIGN_RSA_PSS_RSAE_SHA384, // |
| 3335 | SSL_SIGN_RSA_PSS_RSAE_SHA512, // |
| 3336 | }; |
| 3337 | |
| 3338 | static const char kTLS12Ciphers[] = |
| 3339 | "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384:" |
| 3340 | "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384"; |
| 3341 | |
| 3342 | static int Configure(SSL_CTX *ctx) { |
| 3343 | ctx->tls13_cipher_policy = ssl_compliance_policy_wpa3_192_202304; |
| 3344 | |
| 3345 | return SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION) && |
| 3346 | SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION) && |
| 3347 | SSL_CTX_set_strict_cipher_list(ctx, kTLS12Ciphers) && |
David Benjamin | 6cf9820 | 2023-05-26 22:03:30 -0400 | [diff] [blame] | 3348 | SSL_CTX_set1_group_ids(ctx, kGroups, OPENSSL_ARRAY_SIZE(kGroups)) && |
Adam Langley | 2f6409e | 2023-04-10 21:09:11 +0000 | [diff] [blame] | 3349 | SSL_CTX_set_signing_algorithm_prefs(ctx, kSigAlgs, |
| 3350 | OPENSSL_ARRAY_SIZE(kSigAlgs)) && |
| 3351 | SSL_CTX_set_verify_algorithm_prefs(ctx, kSigAlgs, |
| 3352 | OPENSSL_ARRAY_SIZE(kSigAlgs)); |
| 3353 | } |
| 3354 | |
| 3355 | static int Configure(SSL *ssl) { |
| 3356 | ssl->config->tls13_cipher_policy = ssl_compliance_policy_wpa3_192_202304; |
| 3357 | |
| 3358 | return SSL_set_min_proto_version(ssl, TLS1_2_VERSION) && |
| 3359 | SSL_set_max_proto_version(ssl, TLS1_3_VERSION) && |
| 3360 | SSL_set_strict_cipher_list(ssl, kTLS12Ciphers) && |
David Benjamin | 6cf9820 | 2023-05-26 22:03:30 -0400 | [diff] [blame] | 3361 | SSL_set1_group_ids(ssl, kGroups, OPENSSL_ARRAY_SIZE(kGroups)) && |
Adam Langley | 2f6409e | 2023-04-10 21:09:11 +0000 | [diff] [blame] | 3362 | SSL_set_signing_algorithm_prefs(ssl, kSigAlgs, |
| 3363 | OPENSSL_ARRAY_SIZE(kSigAlgs)) && |
| 3364 | SSL_set_verify_algorithm_prefs(ssl, kSigAlgs, |
| 3365 | OPENSSL_ARRAY_SIZE(kSigAlgs)); |
| 3366 | } |
| 3367 | |
| 3368 | } // namespace wpa202304 |
| 3369 | |
Adam Langley | 451ea3c | 2022-05-18 16:15:49 -0700 | [diff] [blame] | 3370 | int SSL_CTX_set_compliance_policy(SSL_CTX *ctx, |
| 3371 | enum ssl_compliance_policy_t policy) { |
| 3372 | switch (policy) { |
| 3373 | case ssl_compliance_policy_fips_202205: |
| 3374 | return fips202205::Configure(ctx); |
Adam Langley | 2f6409e | 2023-04-10 21:09:11 +0000 | [diff] [blame] | 3375 | case ssl_compliance_policy_wpa3_192_202304: |
| 3376 | return wpa202304::Configure(ctx); |
Adam Langley | 451ea3c | 2022-05-18 16:15:49 -0700 | [diff] [blame] | 3377 | default: |
| 3378 | return 0; |
| 3379 | } |
| 3380 | } |
| 3381 | |
| 3382 | int SSL_set_compliance_policy(SSL *ssl, enum ssl_compliance_policy_t policy) { |
| 3383 | switch (policy) { |
| 3384 | case ssl_compliance_policy_fips_202205: |
| 3385 | return fips202205::Configure(ssl); |
Adam Langley | 2f6409e | 2023-04-10 21:09:11 +0000 | [diff] [blame] | 3386 | case ssl_compliance_policy_wpa3_192_202304: |
| 3387 | return wpa202304::Configure(ssl); |
Adam Langley | 451ea3c | 2022-05-18 16:15:49 -0700 | [diff] [blame] | 3388 | default: |
| 3389 | return 0; |
| 3390 | } |
| 3391 | } |