blob: f95cd5db2ad891baf83a927ed8ffcfbce71e3802 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* 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 Langleye745b252018-02-26 14:02:17 -08007 *
Adam Langley95c29f32014-06-20 12:00:00 -07008 * 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 Langleye745b252018-02-26 14:02:17 -080014 *
Adam Langley95c29f32014-06-20 12:00:00 -070015 * 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 Langleye745b252018-02-26 14:02:17 -080021 *
Adam Langley95c29f32014-06-20 12:00:00 -070022 * 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 Langleye745b252018-02-26 14:02:17 -080036 * 4. If you include any Windows specific code (or a derivative thereof) from
Adam Langley95c29f32014-06-20 12:00:00 -070037 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
Adam Langleye745b252018-02-26 14:02:17 -080039 *
Adam Langley95c29f32014-06-20 12:00:00 -070040 * 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 Langleye745b252018-02-26 14:02:17 -080051 *
Adam Langley95c29f32014-06-20 12:00:00 -070052 * 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 Langleye745b252018-02-26 14:02:17 -080065 * notice, this list of conditions and the following disclaimer.
Adam Langley95c29f32014-06-20 12:00:00 -070066 *
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 Langleye745b252018-02-26 14:02:17 -0800112 * ECC cipher suite support in OpenSSL originally developed by
Adam Langley95c29f32014-06-20 12:00:00 -0700113 * 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 Benjamin9e4e01e2015-09-15 01:48:04 -0400141#include <openssl/ssl.h>
142
David Benjamin3f180b82022-05-09 17:45:18 -0400143#include <algorithm>
144
Adam Langley95c29f32014-06-20 12:00:00 -0700145#include <assert.h>
David Benjamin7ac94aa2022-10-24 15:44:37 -0400146#include <limits.h>
David Benjamin93d97432016-08-08 21:22:47 -0400147#include <stdlib.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400148#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700149
David Benjamin39482a12014-07-20 13:30:15 -0400150#include <openssl/bytestring.h>
David Benjamin7c6d09b2015-09-05 11:00:19 -0400151#include <openssl/crypto.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400152#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700153#include <openssl/lhash.h>
154#include <openssl/mem.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700155#include <openssl/rand.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700156
David Benjamin2ee94aa2015-04-07 22:38:30 -0400157#include "internal.h"
David Benjamin546f1a52015-04-15 16:46:09 -0400158#include "../crypto/internal.h"
159
David Benjamin721e8b72016-08-03 13:13:17 -0400160#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 Langley95c29f32014-06-20 12:00:00 -0700167
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -0700168BSSL_NAMESPACE_BEGIN
David Benjamin86e95b82017-07-18 16:34:25 -0400169
David Benjaminb7d63202022-07-26 13:25:02 -0700170static_assert(SSL3_RT_MAX_ENCRYPTED_OVERHEAD >=
171 SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD,
172 "max overheads are inconsistent");
173
David Benjaminc11ea9422017-08-29 16:33:21 -0400174// |SSL_R_UNKNOWN_PROTOCOL| is no longer emitted, but continue to define it
175// to avoid downstream churn.
David Benjamin76c2efc2015-08-31 14:24:29 -0400176OPENSSL_DECLARE_ERROR_REASON(SSL, UNKNOWN_PROTOCOL)
David Benjamin97760d52015-07-24 23:02:49 -0400177
David Benjaminc11ea9422017-08-29 16:33:21 -0400178// The following errors are no longer emitted, but are used in nginx without
179// #ifdefs.
David Benjamin311c2572016-08-17 15:54:36 -0400180OPENSSL_DECLARE_ERROR_REASON(SSL, BLOCK_CIPHER_PAD_IS_WRONG)
181OPENSSL_DECLARE_ERROR_REASON(SSL, NO_CIPHERS_SPECIFIED)
182
David Benjaminc11ea9422017-08-29 16:33:21 -0400183// Some error codes are special. Ensure the make_errors.go script never
184// regresses this.
David Benjamina3d76d02017-07-14 19:36:07 -0400185static_assert(SSL_R_TLSV1_ALERT_NO_RENEGOTIATION ==
186 SSL_AD_NO_RENEGOTIATION + SSL_AD_REASON_OFFSET,
187 "alert reason code mismatch");
David Benjamine1136082014-09-20 12:28:58 -0400188
David Benjaminc11ea9422017-08-29 16:33:21 -0400189// kMaxHandshakeSize is the maximum size, in bytes, of a handshake message.
David Benjamin1d0a1942015-04-26 15:35:35 -0400190static const size_t kMaxHandshakeSize = (1u << 24) - 1;
191
David Benjaminaa585132015-06-29 23:36:17 -0400192static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl =
193 CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
194static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl_ctx =
195 CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
David Benjamin9f33fc62015-04-15 17:29:53 -0400196
David Benjamin879efc32017-09-21 11:20:53 -0400197bool 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 Benjamin86e95b82017-07-18 16:34:25 -0400208void ssl_reset_error_state(SSL *ssl) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400209 // Functions which use |SSL_get_error| must reset I/O and error state on
210 // entry.
David Benjaminf4928302019-08-21 16:04:53 -0400211 ssl->s3->rwstate = SSL_ERROR_NONE;
David Benjamin86e95b82017-07-18 16:34:25 -0400212 ERR_clear_error();
213 ERR_clear_system_error();
214}
215
David Benjamin31aad2d2017-10-06 16:04:06 -0400216void ssl_set_read_error(SSL* ssl) {
217 ssl->s3->read_shutdown = ssl_shutdown_error;
David Benjamin8e7bbba2017-10-13 17:18:35 -0400218 ssl->s3->read_error.reset(ERR_save_state());
David Benjamin31aad2d2017-10-06 16:04:06 -0400219}
220
David Benjaminf6632da2017-10-12 19:11:47 -0400221static bool check_read_error(const SSL *ssl) {
222 if (ssl->s3->read_shutdown == ssl_shutdown_error) {
David Benjamin8e7bbba2017-10-13 17:18:35 -0400223 ERR_restore_state(ssl->s3->read_error.get());
David Benjaminf6632da2017-10-12 19:11:47 -0400224 return false;
225 }
226 return true;
227}
228
David Benjamin861abcc2018-07-14 17:40:26 -0400229bool ssl_can_write(const SSL *ssl) {
David Benjamin86e95b82017-07-18 16:34:25 -0400230 return !SSL_in_init(ssl) || ssl->s3->hs->can_early_write;
231}
232
David Benjamin861abcc2018-07-14 17:40:26 -0400233bool ssl_can_read(const SSL *ssl) {
David Benjamin86e95b82017-07-18 16:34:25 -0400234 return !SSL_in_init(ssl) || ssl->s3->hs->can_early_read;
235}
236
David Benjaminf6632da2017-10-12 19:11:47 -0400237ssl_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
251ssl_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
267ssl_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 Benjaminb244e3a2019-08-16 19:33:15 -0400282static bool cbb_add_hex(CBB *cbb, Span<const uint8_t> in) {
David Benjamin86e95b82017-07-18 16:34:25 -0400283 static const char hextable[] = "0123456789abcdef";
284 uint8_t *out;
285
David Benjaminb244e3a2019-08-16 19:33:15 -0400286 if (!CBB_add_space(cbb, &out, in.size() * 2)) {
287 return false;
David Benjamin86e95b82017-07-18 16:34:25 -0400288 }
289
David Benjaminb244e3a2019-08-16 19:33:15 -0400290 for (uint8_t b : in) {
291 *(out++) = (uint8_t)hextable[b >> 4];
292 *(out++) = (uint8_t)hextable[b & 0xf];
David Benjamin86e95b82017-07-18 16:34:25 -0400293 }
294
David Benjaminb244e3a2019-08-16 19:33:15 -0400295 return true;
David Benjamin86e95b82017-07-18 16:34:25 -0400296}
297
David Benjaminb244e3a2019-08-16 19:33:15 -0400298bool ssl_log_secret(const SSL *ssl, const char *label,
299 Span<const uint8_t> secret) {
David Benjamin86e95b82017-07-18 16:34:25 -0400300 if (ssl->ctx->keylog_callback == NULL) {
David Benjaminb244e3a2019-08-16 19:33:15 -0400301 return true;
David Benjamin86e95b82017-07-18 16:34:25 -0400302 }
303
David Benjamin1386aad2017-07-19 23:57:40 -0400304 ScopedCBB cbb;
David Benjaminb244e3a2019-08-16 19:33:15 -0400305 Array<uint8_t> line;
David Benjamin1386aad2017-07-19 23:57:40 -0400306 if (!CBB_init(cbb.get(), strlen(label) + 1 + SSL3_RANDOM_SIZE * 2 + 1 +
David Benjaminb244e3a2019-08-16 19:33:15 -0400307 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 Benjamin1386aad2017-07-19 23:57:40 -0400314 !CBB_add_u8(cbb.get(), 0 /* NUL */) ||
David Benjaminb244e3a2019-08-16 19:33:15 -0400315 !CBBFinishArray(cbb.get(), &line)) {
316 return false;
David Benjamin86e95b82017-07-18 16:34:25 -0400317 }
318
David Benjaminb244e3a2019-08-16 19:33:15 -0400319 ssl->ctx->keylog_callback(ssl, reinterpret_cast<const char *>(line.data()));
320 return true;
David Benjamin86e95b82017-07-18 16:34:25 -0400321}
322
David Benjamin86e95b82017-07-18 16:34:25 -0400323void 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 Langleyc9827e02019-04-12 14:46:50 -0700336void ssl_do_msg_callback(const SSL *ssl, int is_write, int content_type,
David Benjaminc64d1232017-10-04 18:14:28 -0400337 Span<const uint8_t> in) {
David Benjamin86e95b82017-07-18 16:34:25 -0400338 if (ssl->msg_callback == NULL) {
339 return;
340 }
341
David Benjaminc11ea9422017-08-29 16:33:21 -0400342 // |version| is zero when calling for |SSL3_RT_HEADER| and |SSL2_VERSION| for
343 // a V2ClientHello.
David Benjamin86e95b82017-07-18 16:34:25 -0400344 int version;
345 switch (content_type) {
346 case 0:
David Benjaminc11ea9422017-08-29 16:33:21 -0400347 // V2ClientHello
David Benjamin86e95b82017-07-18 16:34:25 -0400348 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 Langleyc9827e02019-04-12 14:46:50 -0700357 ssl->msg_callback(is_write, version, content_type, in.data(), in.size(),
358 const_cast<SSL *>(ssl), ssl->msg_callback_arg);
David Benjamin86e95b82017-07-18 16:34:25 -0400359}
360
361void ssl_get_current_time(const SSL *ssl, struct OPENSSL_timeval *out_clock) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400362 // TODO(martinkr): Change callers to |ssl_ctx_get_current_time| and drop the
363 // |ssl| arg from |current_time_cb| if possible.
David Benjamin50596f82018-07-02 19:47:27 -0400364 ssl_ctx_get_current_time(ssl->ctx.get(), out_clock);
Martin Kreichgauer72912d22017-08-04 12:06:43 -0700365}
366
367void ssl_ctx_get_current_time(const SSL_CTX *ctx,
368 struct OPENSSL_timeval *out_clock) {
369 if (ctx->current_time_cb != NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400370 // TODO(davidben): Update current_time_cb to use OPENSSL_timeval. See
371 // https://crbug.com/boringssl/155.
David Benjamin86e95b82017-07-18 16:34:25 -0400372 struct timeval clock;
Martin Kreichgauer72912d22017-08-04 12:06:43 -0700373 ctx->current_time_cb(nullptr /* ssl */, &clock);
David Benjamin86e95b82017-07-18 16:34:25 -0400374 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 Langley3fe8fa72018-01-26 09:14:30 -0800413void SSL_CTX_set_handoff_mode(SSL_CTX *ctx, bool on) {
414 ctx->handoff = on;
415}
416
David Benjamin71666cb2018-05-01 17:14:27 -0400417static bool ssl_can_renegotiate(const SSL *ssl) {
418 if (ssl->server || SSL_is_dtls(ssl)) {
419 return false;
420 }
421
David Benjamin9b2cdb72021-04-01 23:21:53 -0400422 if (ssl->s3->have_version &&
423 ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjamin71666cb2018-05-01 17:14:27 -0400424 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 Benjamin0e7dbd52019-05-15 16:01:18 -0400438 case ssl_renegotiate_explicit:
David Benjamin71666cb2018-05-01 17:14:27 -0400439 return true;
440 case ssl_renegotiate_once:
441 return ssl->s3->total_renegotiations == 0;
442 }
443
444 assert(0);
445 return false;
446}
447
448static 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 Benjamin50596f82018-07-02 19:47:27 -0400456 ssl->config.reset();
David Benjamin71666cb2018-05-01 17:14:27 -0400457}
458
Matthew Braithwaitee30fac62018-04-05 16:50:21 -0700459void SSL_set_handoff_mode(SSL *ssl, bool on) {
460 if (!ssl->config) {
461 return;
462 }
463 ssl->config->handoff = on;
464}
465
Adam Langleyd6e1f232018-12-12 14:11:01 -0800466bool 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 Beck8cacbd92023-04-05 14:38:38 -0600487void 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
493void 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-Feeser8c7c6352018-08-26 18:53:36 -0700498BSSL_NAMESPACE_END
David Benjamin86e95b82017-07-18 16:34:25 -0400499
500using namespace bssl;
501
David Benjamin7c6d09b2015-09-05 11:00:19 -0400502int SSL_library_init(void) {
503 CRYPTO_library_init();
504 return 1;
505}
506
David Benjamin81f030b2016-08-12 14:48:19 -0400507int OPENSSL_init_ssl(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings) {
508 CRYPTO_library_init();
509 return 1;
510}
511
Adam Langleye976e432016-02-15 14:01:10 -0800512static uint32_t ssl_session_hash(const SSL_SESSION *sess) {
David Benjamin58150ed2018-06-29 23:58:43 -0400513 return ssl_hash_session_id(
514 MakeConstSpan(sess->session_id, sess->session_id_length));
David Benjamin27bbae42015-09-13 00:54:37 -0400515}
516
David Benjamin27bbae42015-09-13 00:54:37 -0400517static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
David Benjamin27bbae42015-09-13 00:54:37 -0400518 if (a->session_id_length != b->session_id_length) {
519 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800520 }
Adam Langley95c29f32014-06-20 12:00:00 -0700521
David Benjamin17cf2cb2016-12-13 01:07:13 -0500522 return OPENSSL_memcmp(a->session_id, b->session_id, a->session_id_length);
David Benjamin27bbae42015-09-13 00:54:37 -0400523}
Adam Langley95c29f32014-06-20 12:00:00 -0700524
David Benjaminc1389f22018-07-02 19:13:42 -0400525ssl_ctx_st::ssl_ctx_st(const SSL_METHOD *ssl_method)
David Benjaminfbf10f02024-02-12 16:57:18 -0500526 : RefCounted(CheckSubClass()),
527 method(ssl_method->method),
David Benjaminc1389f22018-07-02 19:13:42 -0400528 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 Benjamin46853762018-07-03 14:01:26 -0400533 channel_id_enabled(false),
David Benjaminc1389f22018-07-02 19:13:42 -0400534 grease_enabled(false),
David Benjamine9c5d722021-06-09 17:43:16 -0400535 permute_extensions(false),
David Benjaminc1389f22018-07-02 19:13:42 -0400536 allow_unknown_alpn_protos(false),
David Benjaminc1389f22018-07-02 19:13:42 -0400537 false_start_allowed_without_alpn(false),
538 handoff(false),
Adam Langley451ea3c2022-05-18 16:15:49 -0700539 enable_early_data(false),
Bob Beck8cacbd92023-04-05 14:38:38 -0600540 aes_hw_override(false),
541 aes_hw_override_value(false) {
David Benjaminc1389f22018-07-02 19:13:42 -0400542 CRYPTO_MUTEX_init(&lock);
543 CRYPTO_new_ex_data(&ex_data);
544}
David Benjamin27bbae42015-09-13 00:54:37 -0400545
David Benjaminc1389f22018-07-02 19:13:42 -0400546ssl_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 Benjaminc1389f22018-07-02 19:13:42 -0400556 x509_method->ssl_ctx_free(this);
David Benjaminc1389f22018-07-02 19:13:42 -0400557}
558
559SSL_CTX *SSL_CTX_new(const SSL_METHOD *method) {
David Benjamin27bbae42015-09-13 00:54:37 -0400560 if (method == NULL) {
561 OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_METHOD_PASSED);
David Benjaminc1389f22018-07-02 19:13:42 -0400562 return nullptr;
Adam Langleyfcf25832014-12-18 17:42:32 -0800563 }
Adam Langley95c29f32014-06-20 12:00:00 -0700564
David Benjaminc1389f22018-07-02 19:13:42 -0400565 UniquePtr<SSL_CTX> ret = MakeUnique<SSL_CTX>(method);
566 if (!ret) {
567 return nullptr;
David Benjamin62fd1622015-01-11 13:30:01 -0500568 }
569
David Benjamin0ce090a2018-07-02 20:24:40 -0400570 ret->cert = MakeUnique<CERT>(method->x509_method);
David Benjamin27bbae42015-09-13 00:54:37 -0400571 ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
David Benjamin0ce090a2018-07-02 20:24:40 -0400572 ret->client_CA.reset(sk_CRYPTO_BUFFER_new_null());
David Benjaminc1389f22018-07-02 19:13:42 -0400573 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 Benjamin27bbae42015-09-13 00:54:37 -0400578 }
579
David Benjaminc1389f22018-07-02 19:13:42 -0400580 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 Benjamine34bcc92016-09-21 16:53:09 -0400585 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjaminc1389f22018-07-02 19:13:42 -0400586 return nullptr;
David Benjamin27bbae42015-09-13 00:54:37 -0400587 }
588
David Benjaminc1389f22018-07-02 19:13:42 -0400589 return ret.release();
David Benjamin27bbae42015-09-13 00:54:37 -0400590}
591
Adam Langley84cd1592016-06-30 18:29:44 -0700592int SSL_CTX_up_ref(SSL_CTX *ctx) {
David Benjaminfbf10f02024-02-12 16:57:18 -0500593 ctx->UpRefInternal();
Adam Langley84cd1592016-06-30 18:29:44 -0700594 return 1;
595}
596
David Benjamin27bbae42015-09-13 00:54:37 -0400597void SSL_CTX_free(SSL_CTX *ctx) {
David Benjaminfbf10f02024-02-12 16:57:18 -0500598 if (ctx != nullptr) {
599 ctx->DecRefInternal();
David Benjamin27bbae42015-09-13 00:54:37 -0400600 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800601}
Adam Langley95c29f32014-06-20 12:00:00 -0700602
David Benjaminc1389f22018-07-02 19:13:42 -0400603ssl_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 Benjamin50596f82018-07-02 19:47:27 -0400608 ctx(UpRef(ctx_arg)),
609 session_ctx(UpRef(ctx_arg)),
David Benjaminc1389f22018-07-02 19:13:42 -0400610 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 Benjaminc1389f22018-07-02 19:13:42 -0400615 enable_early_data(ctx->enable_early_data) {
616 CRYPTO_new_ex_data(&ex_data);
617}
618
619ssl_st::~ssl_st() {
620 CRYPTO_free_ex_data(&g_ex_data_class_ssl, this, &ex_data);
David Benjamin50596f82018-07-02 19:47:27 -0400621 // |config| refers to |this|, so we must release it earlier.
622 config.reset();
David Benjaminc1389f22018-07-02 19:13:42 -0400623 if (method != NULL) {
624 method->ssl_free(this);
625 }
David Benjaminc1389f22018-07-02 19:13:42 -0400626}
627
Adam Langleyfcf25832014-12-18 17:42:32 -0800628SSL *SSL_new(SSL_CTX *ctx) {
David Benjaminc1389f22018-07-02 19:13:42 -0400629 if (ctx == nullptr) {
David Benjamin3570d732015-06-29 00:28:17 -0400630 OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_CTX);
David Benjaminc1389f22018-07-02 19:13:42 -0400631 return nullptr;
Adam Langleyfcf25832014-12-18 17:42:32 -0800632 }
Adam Langley95c29f32014-06-20 12:00:00 -0700633
David Benjaminc1389f22018-07-02 19:13:42 -0400634 UniquePtr<SSL> ssl = MakeUnique<SSL>(ctx);
635 if (ssl == nullptr) {
636 return nullptr;
Adam Langleyfcf25832014-12-18 17:42:32 -0800637 }
Adam Langley95c29f32014-06-20 12:00:00 -0700638
David Benjamin50596f82018-07-02 19:47:27 -0400639 ssl->config = MakeUnique<SSL_CONFIG>(ssl.get());
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700640 if (ssl->config == nullptr) {
David Benjaminc1389f22018-07-02 19:13:42 -0400641 return nullptr;
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700642 }
643 ssl->config->conf_min_version = ctx->conf_min_version;
644 ssl->config->conf_max_version = ctx->conf_max_version;
Adam Langley95c29f32014-06-20 12:00:00 -0700645
David Benjamin0ce090a2018-07-02 20:24:40 -0400646 ssl->config->cert = ssl_cert_dup(ctx->cert.get());
647 if (ssl->config->cert == nullptr) {
David Benjaminc1389f22018-07-02 19:13:42 -0400648 return nullptr;
Adam Langleyfcf25832014-12-18 17:42:32 -0800649 }
Adam Langley95c29f32014-06-20 12:00:00 -0700650
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700651 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 Benjaminbbaf3672016-11-17 10:53:09 +0900655 ctx->retain_only_sha256_of_client_certs;
David Benjamine9c5d722021-06-09 17:43:16 -0400656 ssl->config->permute_extensions = ctx->permute_extensions;
Bob Beck8cacbd92023-04-05 14:38:38 -0600657 ssl->config->aes_hw_override = ctx->aes_hw_override;
658 ssl->config->aes_hw_override_value = ctx->aes_hw_override_value;
Adam Langley2f6409e2023-04-10 21:09:11 +0000659 ssl->config->tls13_cipher_policy = ctx->tls13_cipher_policy;
Adam Langley95c29f32014-06-20 12:00:00 -0700660
David Benjamin0ce090a2018-07-02 20:24:40 -0400661 if (!ssl->config->supported_group_list.CopyFrom(ctx->supported_group_list) ||
662 !ssl->config->alpn_client_proto_list.CopyFrom(
Adam Langley826ce152018-08-03 10:31:21 -0700663 ctx->alpn_client_proto_list) ||
664 !ssl->config->verify_sigalgs.CopyFrom(ctx->verify_sigalgs)) {
David Benjamin0ce090a2018-07-02 20:24:40 -0400665 return nullptr;
Adam Langleyfcf25832014-12-18 17:42:32 -0800666 }
Adam Langley95c29f32014-06-20 12:00:00 -0700667
Adam Langleyfcf25832014-12-18 17:42:32 -0800668 if (ctx->psk_identity_hint) {
David Benjamin0ce090a2018-07-02 20:24:40 -0400669 ssl->config->psk_identity_hint.reset(
David Benjamin3ba95862019-10-21 16:14:33 -0400670 OPENSSL_strdup(ctx->psk_identity_hint.get()));
David Benjamin0ce090a2018-07-02 20:24:40 -0400671 if (ssl->config->psk_identity_hint == nullptr) {
David Benjaminc1389f22018-07-02 19:13:42 -0400672 return nullptr;
Adam Langleyfcf25832014-12-18 17:42:32 -0800673 }
674 }
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700675 ssl->config->psk_client_callback = ctx->psk_client_callback;
676 ssl->config->psk_server_callback = ctx->psk_server_callback;
Adam Langley95c29f32014-06-20 12:00:00 -0700677
David Benjamin46853762018-07-03 14:01:26 -0400678 ssl->config->channel_id_enabled = ctx->channel_id_enabled;
679 ssl->config->channel_id_private = UpRef(ctx->channel_id_private);
David Benjamin02ddbfd2015-01-11 13:09:11 -0500680
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700681 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 Ghedini3cbb0292018-12-13 13:53:57 +0000685 ssl->quic_method = ctx->quic_method;
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200686
David Benjaminc1389f22018-07-02 19:13:42 -0400687 if (!ssl->method->ssl_new(ssl.get()) ||
688 !ssl->ctx->x509_method->ssl_new(ssl->s3->hs.get())) {
689 return nullptr;
690 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800691
David Benjaminc1389f22018-07-02 19:13:42 -0400692 return ssl.release();
Adam Langleyfcf25832014-12-18 17:42:32 -0800693}
Adam Langley95c29f32014-06-20 12:00:00 -0700694
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700695SSL_CONFIG::SSL_CONFIG(SSL *ssl_arg)
696 : ssl(ssl_arg),
Dan McArdle1920c6f2020-03-11 17:29:40 -0400697 ech_grease_enabled(false),
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700698 signed_cert_timestamps_enabled(false),
699 ocsp_stapling_enabled(false),
David Benjamin46853762018-07-03 14:01:26 -0400700 channel_id_enabled(false),
David Benjamincee2dbb2023-04-14 16:47:59 -0400701 enforce_rsa_key_usage(true),
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700702 retain_only_sha256_of_client_certs(false),
703 handoff(false),
David Benjamin2d98d492018-10-08 17:31:17 -0500704 shed_handshake_config(false),
David Schinazi3d8b8c32021-01-14 11:25:49 -0800705 jdk11_workaround(false),
David Benjamine9c5d722021-06-09 17:43:16 -0400706 quic_use_legacy_codepoint(false),
Victor Tan558960d2023-06-23 15:04:33 +0000707 permute_extensions(false),
708 alps_use_new_codepoint(false) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700709 assert(ssl);
710}
711
712SSL_CONFIG::~SSL_CONFIG() {
713 if (ssl->ctx != nullptr) {
714 ssl->ctx->x509_method->ssl_config_free(this);
715 }
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700716}
717
David Benjamin27bbae42015-09-13 00:54:37 -0400718void SSL_free(SSL *ssl) {
David Benjaminc1389f22018-07-02 19:13:42 -0400719 Delete(ssl);
David Benjamin27bbae42015-09-13 00:54:37 -0400720}
721
722void SSL_set_connect_state(SSL *ssl) {
David Benjamin7e58c5e2017-10-11 13:01:08 -0400723 ssl->server = false;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400724 ssl->do_handshake = ssl_client_handshake;
David Benjamin27bbae42015-09-13 00:54:37 -0400725}
726
727void SSL_set_accept_state(SSL *ssl) {
David Benjamin7e58c5e2017-10-11 13:01:08 -0400728 ssl->server = true;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400729 ssl->do_handshake = ssl_server_handshake;
David Benjamin27bbae42015-09-13 00:54:37 -0400730}
731
David Benjamin4501bd52016-08-01 13:39:41 -0400732void SSL_set0_rbio(SSL *ssl, BIO *rbio) {
David Benjamin50596f82018-07-02 19:47:27 -0400733 ssl->rbio.reset(rbio);
David Benjaminf715c422016-06-11 19:01:56 -0400734}
735
David Benjamin4501bd52016-08-01 13:39:41 -0400736void SSL_set0_wbio(SSL *ssl, BIO *wbio) {
David Benjamin50596f82018-07-02 19:47:27 -0400737 ssl->wbio.reset(wbio);
David Benjamin27bbae42015-09-13 00:54:37 -0400738}
739
David Benjaminf715c422016-06-11 19:01:56 -0400740void SSL_set_bio(SSL *ssl, BIO *rbio, BIO *wbio) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400741 // For historical reasons, this function has many different cases in ownership
742 // handling.
David Benjaminf715c422016-06-11 19:01:56 -0400743
David Benjaminc11ea9422017-08-29 16:33:21 -0400744 // If nothing has changed, do nothing
David Benjamin4501bd52016-08-01 13:39:41 -0400745 if (rbio == SSL_get_rbio(ssl) && wbio == SSL_get_wbio(ssl)) {
746 return;
747 }
748
David Benjaminc11ea9422017-08-29 16:33:21 -0400749 // If the two arguments are equal, one fewer reference is granted than
750 // taken.
David Benjaminf715c422016-06-11 19:01:56 -0400751 if (rbio != NULL && rbio == wbio) {
752 BIO_up_ref(rbio);
753 }
754
David Benjaminc11ea9422017-08-29 16:33:21 -0400755 // If only the wbio is changed, adopt only one reference.
David Benjaminf715c422016-06-11 19:01:56 -0400756 if (rbio == SSL_get_rbio(ssl)) {
David Benjamin4501bd52016-08-01 13:39:41 -0400757 SSL_set0_wbio(ssl, wbio);
David Benjaminf715c422016-06-11 19:01:56 -0400758 return;
759 }
David Benjamin4501bd52016-08-01 13:39:41 -0400760
David Benjaminc11ea9422017-08-29 16:33:21 -0400761 // 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 Benjamin4501bd52016-08-01 13:39:41 -0400764 if (wbio == SSL_get_wbio(ssl) && SSL_get_rbio(ssl) != SSL_get_wbio(ssl)) {
765 SSL_set0_rbio(ssl, rbio);
David Benjaminf715c422016-06-11 19:01:56 -0400766 return;
767 }
768
David Benjaminc11ea9422017-08-29 16:33:21 -0400769 // Otherwise, adopt both references.
David Benjamin4501bd52016-08-01 13:39:41 -0400770 SSL_set0_rbio(ssl, rbio);
771 SSL_set0_wbio(ssl, wbio);
David Benjaminf715c422016-06-11 19:01:56 -0400772}
773
David Benjamin50596f82018-07-02 19:47:27 -0400774BIO *SSL_get_rbio(const SSL *ssl) { return ssl->rbio.get(); }
David Benjamin27bbae42015-09-13 00:54:37 -0400775
David Benjamin50596f82018-07-02 19:47:27 -0400776BIO *SSL_get_wbio(const SSL *ssl) { return ssl->wbio.get(); }
David Benjamin27bbae42015-09-13 00:54:37 -0400777
Steven Valdezc8e0f902018-07-14 11:23:01 -0400778size_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
816enum ssl_encryption_level_t SSL_quic_read_level(const SSL *ssl) {
817 return ssl->s3->read_level;
818}
819
820enum ssl_encryption_level_t SSL_quic_write_level(const SSL *ssl) {
821 return ssl->s3->write_level;
822}
823
824int SSL_provide_quic_data(SSL *ssl, enum ssl_encryption_level_t level,
825 const uint8_t *data, size_t len) {
Alessandro Ghedini3cbb0292018-12-13 13:53:57 +0000826 if (ssl->quic_method == nullptr) {
Steven Valdezc8e0f902018-07-14 11:23:01 -0400827 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 Benjamin7d10ab52018-11-02 18:31:17 -0500843 return tls_append_handshake_data(ssl, MakeConstSpan(data, len));
Steven Valdezc8e0f902018-07-14 11:23:01 -0400844}
845
David Benjamin2be4aa72017-01-02 07:56:51 -0500846int SSL_do_handshake(SSL *ssl) {
847 ssl_reset_error_state(ssl);
David Benjamin15c14882016-03-14 14:25:46 -0400848
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400849 if (ssl->do_handshake == NULL) {
David Benjamin27bbae42015-09-13 00:54:37 -0400850 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 Benjaminc11ea9422017-08-29 16:33:21 -0400858 // Run the handshake.
David Benjamin8e7bbba2017-10-13 17:18:35 -0400859 SSL_HANDSHAKE *hs = ssl->s3->hs.get();
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400860
David Benjamin74795b32017-08-31 15:13:12 -0400861 bool early_return = false;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400862 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 Benjamince8c9d22016-11-14 10:45:16 +0900865 if (ret <= 0) {
866 return ret;
867 }
868
David Benjaminc11ea9422017-08-29 16:33:21 -0400869 // Destroy the handshake object if the handshake has completely finished.
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400870 if (!early_return) {
David Benjamin8e7bbba2017-10-13 17:18:35 -0400871 ssl->s3->hs.reset();
David Benjamin71666cb2018-05-01 17:14:27 -0400872 ssl_maybe_shed_handshake_config(ssl);
David Benjamince8c9d22016-11-14 10:45:16 +0900873 }
874
875 return 1;
David Benjamin27bbae42015-09-13 00:54:37 -0400876}
877
878int SSL_connect(SSL *ssl) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400879 if (ssl->do_handshake == NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400880 // Not properly initialized yet
David Benjamin27bbae42015-09-13 00:54:37 -0400881 SSL_set_connect_state(ssl);
882 }
883
David Benjamin15c14882016-03-14 14:25:46 -0400884 return SSL_do_handshake(ssl);
David Benjamin27bbae42015-09-13 00:54:37 -0400885}
886
887int SSL_accept(SSL *ssl) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400888 if (ssl->do_handshake == NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400889 // Not properly initialized yet
David Benjamin27bbae42015-09-13 00:54:37 -0400890 SSL_set_accept_state(ssl);
891 }
892
David Benjamin15c14882016-03-14 14:25:46 -0400893 return SSL_do_handshake(ssl);
David Benjamin27bbae42015-09-13 00:54:37 -0400894}
895
David Benjamin7934f082017-08-01 16:32:25 -0400896static int ssl_do_post_handshake(SSL *ssl, const SSLMessage &msg) {
David Benjamind1e3ce12017-10-06 18:31:15 -0400897 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjamin7934f082017-08-01 16:32:25 -0400898 return tls13_post_handshake(ssl, msg);
899 }
900
David Benjamin71666cb2018-05-01 17:14:27 -0400901 // 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 Benjamin163f29a2016-07-28 11:05:58 -0400907 }
908
David Benjamin7934f082017-08-01 16:32:25 -0400909 if (msg.type != SSL3_MT_HELLO_REQUEST || CBS_len(&msg.body) != 0) {
David Benjamind1e3ce12017-10-06 18:31:15 -0400910 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin163f29a2016-07-28 11:05:58 -0400911 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HELLO_REQUEST);
912 return 0;
913 }
914
David Benjamin71666cb2018-05-01 17:14:27 -0400915 if (ssl->renegotiate_mode == ssl_renegotiate_ignore) {
916 return 1; // Ignore the HelloRequest.
David Benjamin163f29a2016-07-28 11:05:58 -0400917 }
918
David Benjamin0e7dbd52019-05-15 16:01:18 -0400919 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 Benjamin71666cb2018-05-01 17:14:27 -0400925 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_NO_RENEGOTIATION);
926 return 0;
David Benjamin163f29a2016-07-28 11:05:58 -0400927 }
928
David Benjamin163f29a2016-07-28 11:05:58 -0400929 return 1;
David Benjamin163f29a2016-07-28 11:05:58 -0400930}
931
Steven Valdeze6eef1c2018-11-09 13:32:34 -0500932int 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 Benjamina031b612017-10-11 20:48:25 -0400959static int ssl_read_impl(SSL *ssl) {
David Benjamin2be4aa72017-01-02 07:56:51 -0500960 ssl_reset_error_state(ssl);
David Benjamin15c14882016-03-14 14:25:46 -0400961
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400962 if (ssl->do_handshake == NULL) {
David Benjamin27bbae42015-09-13 00:54:37 -0400963 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
964 return -1;
965 }
966
David Benjaminf6632da2017-10-12 19:11:47 -0400967 // Replay post-handshake message errors.
968 if (!check_read_error(ssl)) {
969 return -1;
970 }
971
David Benjamina031b612017-10-11 20:48:25 -0400972 while (ssl->s3->pending_app_data.empty()) {
David Benjamin0e7dbd52019-05-15 16:01:18 -0400973 if (ssl->s3->renegotiate_pending) {
974 ssl->s3->rwstate = SSL_ERROR_WANT_RENEGOTIATE;
975 return -1;
976 }
977
David Benjaminc11ea9422017-08-29 16:33:21 -0400978 // 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 Valdez681eb6a2016-12-19 13:19:29 -0500981 while (!ssl_can_read(ssl)) {
David Benjamin163f29a2016-07-28 11:05:58 -0400982 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 Benjamin40e94702017-10-06 18:26:36 -0400992 // Process any buffered post-handshake messages.
David Benjamin7934f082017-08-01 16:32:25 -0400993 SSLMessage msg;
David Benjamin40e94702017-10-06 18:26:36 -0400994 if (ssl->method->get_message(ssl, &msg)) {
Steven Valdezcd8470f2017-10-11 12:29:36 -0400995 // 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 Benjaminc11ea9422017-08-29 16:33:21 -04001002 // Handle the post-handshake message and try again.
David Benjamin7934f082017-08-01 16:32:25 -04001003 if (!ssl_do_post_handshake(ssl, msg)) {
David Benjaminf6632da2017-10-12 19:11:47 -04001004 ssl_set_read_error(ssl);
David Benjamin7934f082017-08-01 16:32:25 -04001005 return -1;
1006 }
1007 ssl->method->next_message(ssl);
David Benjamin40e94702017-10-06 18:26:36 -04001008 continue; // Loop again. We may have begun a new handshake.
David Benjaminc79845c2016-03-10 01:28:00 -05001009 }
David Benjamin40e94702017-10-06 18:26:36 -04001010
David Benjamina031b612017-10-11 20:48:25 -04001011 uint8_t alert = SSL_AD_DECODE_ERROR;
1012 size_t consumed = 0;
David Benjaminf6632da2017-10-12 19:11:47 -04001013 auto ret = ssl_open_app_data(ssl, &ssl->s3->pending_app_data, &consumed,
David Benjaminea712e32017-10-13 16:50:39 -04001014 &alert, ssl->s3->read_buffer.span());
David Benjamina031b612017-10-11 20:48:25 -04001015 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 Benjamin40e94702017-10-06 18:26:36 -04001022 ssl->s3->key_update_count = 0;
1023 }
David Benjaminc79845c2016-03-10 01:28:00 -05001024 }
David Benjamina031b612017-10-11 20:48:25 -04001025
1026 return 1;
David Benjamin15c14882016-03-14 14:25:46 -04001027}
1028
1029int SSL_read(SSL *ssl, void *buf, int num) {
David Benjamina031b612017-10-11 20:48:25 -04001030 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 Benjaminea712e32017-10-13 16:50:39 -04001039 ssl->s3->read_buffer.DiscardConsumed();
David Benjamina031b612017-10-11 20:48:25 -04001040 }
1041 return ret;
David Benjamin27bbae42015-09-13 00:54:37 -04001042}
1043
1044int SSL_peek(SSL *ssl, void *buf, int num) {
Alessandro Ghedini3cbb0292018-12-13 13:53:57 +00001045 if (ssl->quic_method != nullptr) {
Steven Valdezc8e0f902018-07-14 11:23:01 -04001046 OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
David Benjamin69ec7c82021-08-11 17:18:00 -04001047 return -1;
Steven Valdezc8e0f902018-07-14 11:23:01 -04001048 }
1049
David Benjamina031b612017-10-11 20:48:25 -04001050 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 Benjamin27bbae42015-09-13 00:54:37 -04001061}
1062
1063int SSL_write(SSL *ssl, const void *buf, int num) {
David Benjamin2be4aa72017-01-02 07:56:51 -05001064 ssl_reset_error_state(ssl);
David Benjamin15c14882016-03-14 14:25:46 -04001065
Alessandro Ghedini3cbb0292018-12-13 13:53:57 +00001066 if (ssl->quic_method != nullptr) {
Steven Valdezc8e0f902018-07-14 11:23:01 -04001067 OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
David Benjamin69ec7c82021-08-11 17:18:00 -04001068 return -1;
Steven Valdezc8e0f902018-07-14 11:23:01 -04001069 }
1070
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001071 if (ssl->do_handshake == NULL) {
David Benjamin27bbae42015-09-13 00:54:37 -04001072 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
1073 return -1;
1074 }
1075
David Benjamin74795b32017-08-31 15:13:12 -04001076 int ret = 0;
David Benjamin4bd32a82021-05-14 17:06:29 -04001077 size_t bytes_written = 0;
David Benjamin74795b32017-08-31 15:13:12 -04001078 bool needs_handshake = false;
Steven Valdeze831a812017-03-09 14:56:07 -05001079 do {
David Benjaminc11ea9422017-08-29 16:33:21 -04001080 // If necessary, complete the handshake implicitly.
Steven Valdeze831a812017-03-09 14:56:07 -05001081 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 Benjamind7ac1432016-03-10 00:41:25 -05001090 }
David Benjamind7ac1432016-03-10 00:41:25 -05001091
David Benjamin4bd32a82021-05-14 17:06:29 -04001092 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 Valdeze831a812017-03-09 14:56:07 -05001100 } while (needs_handshake);
David Benjamin4bd32a82021-05-14 17:06:29 -04001101 return ret <= 0 ? ret : static_cast<int>(bytes_written);
David Benjamin27bbae42015-09-13 00:54:37 -04001102}
1103
Adam Langleyba9ad662018-12-17 13:59:38 -08001104int 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 Benjamin27bbae42015-09-13 00:54:37 -04001135int SSL_shutdown(SSL *ssl) {
David Benjamin2be4aa72017-01-02 07:56:51 -05001136 ssl_reset_error_state(ssl);
David Benjamin15c14882016-03-14 14:25:46 -04001137
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001138 if (ssl->do_handshake == NULL) {
David Benjamin27bbae42015-09-13 00:54:37 -04001139 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
1140 return -1;
1141 }
1142
David Benjaminc11ea9422017-08-29 16:33:21 -04001143 // 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 Valdeza14934f2016-02-29 10:05:08 -05001146 if (SSL_in_init(ssl)) {
David Benjamin12709db2017-02-17 13:51:50 -05001147 return 1;
Steven Valdeza14934f2016-02-29 10:05:08 -05001148 }
1149
David Benjamin27bbae42015-09-13 00:54:37 -04001150 if (ssl->quiet_shutdown) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001151 // Do nothing if configured not to send a close_notify.
David Benjamin23c25d52017-10-06 16:02:47 -04001152 ssl->s3->write_shutdown = ssl_shutdown_close_notify;
1153 ssl->s3->read_shutdown = ssl_shutdown_close_notify;
David Benjamin27bbae42015-09-13 00:54:37 -04001154 return 1;
1155 }
1156
David Benjaminc11ea9422017-08-29 16:33:21 -04001157 // 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 Benjamin27bbae42015-09-13 00:54:37 -04001160
David Benjamin23c25d52017-10-06 16:02:47 -04001161 if (ssl->s3->write_shutdown != ssl_shutdown_close_notify) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001162 // Send a close_notify.
David Benjaminb29e1e12019-05-06 14:44:46 -05001163 if (ssl_send_alert_impl(ssl, SSL3_AL_WARNING, SSL_AD_CLOSE_NOTIFY) <= 0) {
David Benjaminfa214e42016-05-10 17:03:10 -04001164 return -1;
David Benjamin27bbae42015-09-13 00:54:37 -04001165 }
1166 } else if (ssl->s3->alert_dispatch) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001167 // Finish sending the close_notify.
David Benjaminf0ee9072016-06-15 17:44:37 -04001168 if (ssl->method->dispatch_alert(ssl) <= 0) {
David Benjaminfa214e42016-05-10 17:03:10 -04001169 return -1;
David Benjamin27bbae42015-09-13 00:54:37 -04001170 }
David Benjamin23c25d52017-10-06 16:02:47 -04001171 } else if (ssl->s3->read_shutdown != ssl_shutdown_close_notify) {
David Benjamina031b612017-10-11 20:48:25 -04001172 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 Benjamin8e7bbba2017-10-13 17:18:35 -04001178 ERR_restore_state(ssl->s3->read_error.get());
David Benjamina031b612017-10-11 20:48:25 -04001179 return -1;
David Benjamind9229f92017-10-06 17:36:20 -04001180 }
David Benjamina031b612017-10-11 20:48:25 -04001181 ssl->s3->read_shutdown = ssl_shutdown_close_notify;
1182 } else {
David Benjamin5267ef72018-05-30 10:18:09 -04001183 // 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 Benjamina031b612017-10-11 20:48:25 -04001188 }
1189 if (ssl->s3->read_shutdown != ssl_shutdown_close_notify) {
1190 return -1;
1191 }
David Benjamin27bbae42015-09-13 00:54:37 -04001192 }
1193 }
1194
David Benjaminc11ea9422017-08-29 16:33:21 -04001195 // Return 0 for unidirectional shutdown and 1 for bidirectional shutdown.
David Benjamin23c25d52017-10-06 16:02:47 -04001196 return ssl->s3->read_shutdown == ssl_shutdown_close_notify;
David Benjamin27bbae42015-09-13 00:54:37 -04001197}
1198
David Benjamin1d4f4c02016-07-26 18:03:08 -04001199int 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 Benjaminc11ea9422017-08-29 16:33:21 -04001203 // We are already attempting to write a different alert.
David Benjamin1d4f4c02016-07-26 18:03:08 -04001204 OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
1205 return -1;
1206 }
1207 return ssl->method->dispatch_alert(ssl);
1208 }
1209
David Benjaminb29e1e12019-05-06 14:44:46 -05001210 return ssl_send_alert_impl(ssl, SSL3_AL_FATAL, alert);
David Benjamin1d4f4c02016-07-26 18:03:08 -04001211}
1212
Nick Harper3c034b22017-12-22 15:50:43 -08001213int SSL_set_quic_transport_params(SSL *ssl, const uint8_t *params,
1214 size_t params_len) {
David Benjamin0ce090a2018-07-02 20:24:40 -04001215 return ssl->config && ssl->config->quic_transport_params.CopyFrom(
1216 MakeConstSpan(params, params_len));
Nick Harper3c034b22017-12-22 15:50:43 -08001217}
1218
1219void 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 Harper7c522992020-04-30 14:15:49 -07001226int 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 Valdez08b65f42016-12-07 15:29:45 -05001232void SSL_CTX_set_early_data_enabled(SSL_CTX *ctx, int enabled) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001233 ctx->enable_early_data = !!enabled;
Alessandro Ghedini67bb45f2017-03-30 16:33:24 -05001234}
1235
1236void SSL_set_early_data_enabled(SSL *ssl, int enabled) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001237 ssl->enable_early_data = !!enabled;
Steven Valdez08b65f42016-12-07 15:29:45 -05001238}
1239
Steven Valdeze831a812017-03-09 14:56:07 -05001240int 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 Valdez2d850622017-01-11 11:34:52 -05001247int SSL_early_data_accepted(const SSL *ssl) {
David Benjamin02e62562017-12-18 18:04:01 -05001248 return ssl->s3->early_data_accepted;
Steven Valdez2d850622017-01-11 11:34:52 -05001249}
1250
Steven Valdeze831a812017-03-09 14:56:07 -05001251void SSL_reset_early_data_reject(SSL *ssl) {
David Benjamin8e7bbba2017-10-13 17:18:35 -04001252 SSL_HANDSHAKE *hs = ssl->s3->hs.get();
Steven Valdeze831a812017-03-09 14:56:07 -05001253 if (hs == NULL ||
1254 hs->wait != ssl_hs_early_data_rejected) {
1255 abort();
1256 }
1257
1258 hs->wait = ssl_hs_ok;
David Benjaminfd45ee72017-08-31 14:49:09 -04001259 hs->in_early_data = false;
David Benjamin31b0c9b2017-07-20 14:49:15 -04001260 hs->early_session.reset();
Steven Valdeze831a812017-03-09 14:56:07 -05001261
David Benjaminc11ea9422017-08-29 16:33:21 -04001262 // 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 Benjamin4bd32a82021-05-14 17:06:29 -04001265 ssl->s3->pending_write = {};
Steven Valdeze831a812017-03-09 14:56:07 -05001266}
1267
David Benjamin64770122019-05-04 11:00:04 -05001268enum ssl_early_data_reason_t SSL_get_early_data_reason(const SSL *ssl) {
1269 return ssl->s3->early_data_reason;
1270}
1271
David Benjaminfa9796e2020-11-02 13:01:04 -05001272const 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 Benjaminfa9796e2020-11-02 13:01:04 -05001296 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 Benjamin287fc4f2016-12-10 15:12:03 -05001307static 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 Benjamin27bbae42015-09-13 00:54:37 -04001317
David Benjamin287fc4f2016-12-10 15:12:03 -05001318int SSL_get_error(const SSL *ssl, int ret_code) {
David Benjamin27bbae42015-09-13 00:54:37 -04001319 if (ret_code > 0) {
1320 return SSL_ERROR_NONE;
1321 }
1322
David Benjaminc11ea9422017-08-29 16:33:21 -04001323 // Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
1324 // where we do encode the error
David Benjamin287fc4f2016-12-10 15:12:03 -05001325 uint32_t err = ERR_peek_error();
David Benjamin27bbae42015-09-13 00:54:37 -04001326 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 Benjaminebd8b892022-07-27 18:41:51 -07001334 if (ssl->s3->rwstate == SSL_ERROR_ZERO_RETURN) {
David Benjamin27bbae42015-09-13 00:54:37 -04001335 return SSL_ERROR_ZERO_RETURN;
1336 }
David Benjaminc11ea9422017-08-29 16:33:21 -04001337 // 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 Benjamin27bbae42015-09-13 00:54:37 -04001340 return SSL_ERROR_SYSCALL;
1341 }
1342
David Benjamin32ce0ac2017-10-13 19:17:22 -04001343 switch (ssl->s3->rwstate) {
David Benjaminf4928302019-08-21 16:04:53 -04001344 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 Benjaminf4928302019-08-21 16:04:53 -04001349 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 Benjamin0e7dbd52019-05-15 16:01:18 -04001353 case SSL_ERROR_WANT_RENEGOTIATE:
David Benjaminb571e772021-03-25 19:42:16 -04001354 case SSL_ERROR_HANDSHAKE_HINTS_READY:
David Benjaminf4928302019-08-21 16:04:53 -04001355 return ssl->s3->rwstate;
David Benjamin27bbae42015-09-13 00:54:37 -04001356
David Benjaminf4928302019-08-21 16:04:53 -04001357 case SSL_ERROR_WANT_READ: {
Alessandro Ghedini3cbb0292018-12-13 13:53:57 +00001358 if (ssl->quic_method) {
Steven Valdezc8e0f902018-07-14 11:23:01 -04001359 return SSL_ERROR_WANT_READ;
1360 }
David Benjamin287fc4f2016-12-10 15:12:03 -05001361 BIO *bio = SSL_get_rbio(ssl);
1362 if (BIO_should_read(bio)) {
1363 return SSL_ERROR_WANT_READ;
David Benjamin27bbae42015-09-13 00:54:37 -04001364 }
1365
David Benjamin287fc4f2016-12-10 15:12:03 -05001366 if (BIO_should_write(bio)) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001367 // TODO(davidben): OpenSSL historically checked for writes on the read
1368 // BIO. Can this be removed?
David Benjamin287fc4f2016-12-10 15:12:03 -05001369 return SSL_ERROR_WANT_WRITE;
David Benjamin27bbae42015-09-13 00:54:37 -04001370 }
1371
David Benjamin287fc4f2016-12-10 15:12:03 -05001372 if (BIO_should_io_special(bio)) {
1373 return bio_retry_reason_to_error(BIO_get_retry_reason(bio));
David Benjamin27bbae42015-09-13 00:54:37 -04001374 }
1375
David Benjamin287fc4f2016-12-10 15:12:03 -05001376 break;
1377 }
1378
David Benjaminf4928302019-08-21 16:04:53 -04001379 case SSL_ERROR_WANT_WRITE: {
David Benjamin287fc4f2016-12-10 15:12:03 -05001380 BIO *bio = SSL_get_wbio(ssl);
1381 if (BIO_should_write(bio)) {
1382 return SSL_ERROR_WANT_WRITE;
David Benjamin27bbae42015-09-13 00:54:37 -04001383 }
1384
David Benjamin287fc4f2016-12-10 15:12:03 -05001385 if (BIO_should_read(bio)) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001386 // TODO(davidben): OpenSSL historically checked for reads on the write
1387 // BIO. Can this be removed?
David Benjamin287fc4f2016-12-10 15:12:03 -05001388 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 Benjamin27bbae42015-09-13 00:54:37 -04001396 }
David Benjamin27bbae42015-09-13 00:54:37 -04001397 }
1398
1399 return SSL_ERROR_SYSCALL;
1400}
1401
David Benjaminbd2a8d62019-08-21 16:17:17 -04001402const 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 Benjaminbd2a8d62019-08-21 16:17:17 -04001422 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 Benjamin747229e2021-05-18 19:29:58 -04001438 case SSL_ERROR_WANT_RENEGOTIATE:
1439 return "WANT_RENEGOTIATE";
David Benjaminb571e772021-03-25 19:42:16 -04001440 case SSL_ERROR_HANDSHAKE_HINTS_READY:
1441 return "HANDSHAKE_HINTS_READY";
David Benjaminbd2a8d62019-08-21 16:17:17 -04001442 default:
1443 return nullptr;
1444 }
1445}
1446
David Benjamin27bbae42015-09-13 00:54:37 -04001447uint32_t SSL_CTX_set_options(SSL_CTX *ctx, uint32_t options) {
1448 ctx->options |= options;
1449 return ctx->options;
1450}
1451
1452uint32_t SSL_CTX_clear_options(SSL_CTX *ctx, uint32_t options) {
1453 ctx->options &= ~options;
1454 return ctx->options;
1455}
1456
1457uint32_t SSL_CTX_get_options(const SSL_CTX *ctx) { return ctx->options; }
1458
1459uint32_t SSL_set_options(SSL *ssl, uint32_t options) {
1460 ssl->options |= options;
1461 return ssl->options;
1462}
1463
1464uint32_t SSL_clear_options(SSL *ssl, uint32_t options) {
1465 ssl->options &= ~options;
1466 return ssl->options;
1467}
1468
1469uint32_t SSL_get_options(const SSL *ssl) { return ssl->options; }
1470
1471uint32_t SSL_CTX_set_mode(SSL_CTX *ctx, uint32_t mode) {
1472 ctx->mode |= mode;
1473 return ctx->mode;
1474}
1475
1476uint32_t SSL_CTX_clear_mode(SSL_CTX *ctx, uint32_t mode) {
1477 ctx->mode &= ~mode;
1478 return ctx->mode;
1479}
1480
1481uint32_t SSL_CTX_get_mode(const SSL_CTX *ctx) { return ctx->mode; }
1482
1483uint32_t SSL_set_mode(SSL *ssl, uint32_t mode) {
1484 ssl->mode |= mode;
1485 return ssl->mode;
1486}
1487
1488uint32_t SSL_clear_mode(SSL *ssl, uint32_t mode) {
1489 ssl->mode &= ~mode;
1490 return ssl->mode;
1491}
1492
1493uint32_t SSL_get_mode(const SSL *ssl) { return ssl->mode; }
1494
Adam Langleyd519bf62016-12-12 11:16:44 -08001495void SSL_CTX_set0_buffer_pool(SSL_CTX *ctx, CRYPTO_BUFFER_POOL *pool) {
1496 ctx->pool = pool;
1497}
1498
David Benjaminee0c8272015-09-13 01:03:54 -04001499int SSL_get_tls_unique(const SSL *ssl, uint8_t *out, size_t *out_len,
1500 size_t max_out) {
David Benjaminf5260812017-07-12 17:16:39 -04001501 *out_len = 0;
1502 OPENSSL_memset(out, 0, max_out);
1503
David Benjamin9bb15f52018-06-26 00:07:40 -04001504 // tls-unique is not defined for TLS 1.3.
David Benjaminced00b42016-10-08 11:24:09 -04001505 if (!ssl->s3->initial_handshake_complete ||
David Benjamind1e3ce12017-10-06 18:31:15 -04001506 ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjaminf5260812017-07-12 17:16:39 -04001507 return 0;
David Benjaminced00b42016-10-08 11:24:09 -04001508 }
1509
David Benjaminc11ea9422017-08-29 16:33:21 -04001510 // 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 Benjaminee0c8272015-09-13 01:03:54 -04001513 const uint8_t *finished = ssl->s3->previous_client_finished;
1514 size_t finished_len = ssl->s3->previous_client_finished_len;
Steven Valdez87eab492016-06-27 16:34:59 -04001515 if (ssl->session != NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001516 // tls-unique is broken for resumed sessions unless EMS is used.
David Benjaminee0c8272015-09-13 01:03:54 -04001517 if (!ssl->session->extended_master_secret) {
David Benjaminf5260812017-07-12 17:16:39 -04001518 return 0;
David Benjaminee0c8272015-09-13 01:03:54 -04001519 }
1520 finished = ssl->s3->previous_server_finished;
1521 finished_len = ssl->s3->previous_server_finished_len;
1522 }
1523
David Benjaminee0c8272015-09-13 01:03:54 -04001524 *out_len = finished_len;
1525 if (finished_len > max_out) {
1526 *out_len = max_out;
1527 }
1528
David Benjamin17cf2cb2016-12-13 01:07:13 -05001529 OPENSSL_memcpy(out, finished, *out_len);
David Benjaminee0c8272015-09-13 01:03:54 -04001530 return 1;
David Benjaminee0c8272015-09-13 01:03:54 -04001531}
1532
David Benjamin5960a902017-02-14 20:07:11 -05001533static int set_session_id_context(CERT *cert, const uint8_t *sid_ctx,
David Benjamin30c4c302016-12-07 22:35:24 -05001534 size_t sid_ctx_len) {
David Benjamin5960a902017-02-14 20:07:11 -05001535 if (sid_ctx_len > sizeof(cert->sid_ctx)) {
David Benjamin3570d732015-06-29 00:28:17 -04001536 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -08001537 return 0;
1538 }
David Benjamin30c4c302016-12-07 22:35:24 -05001539
David Benjamina3d76d02017-07-14 19:36:07 -04001540 static_assert(sizeof(cert->sid_ctx) < 256, "sid_ctx too large");
David Benjamin5960a902017-02-14 20:07:11 -05001541 cert->sid_ctx_length = (uint8_t)sid_ctx_len;
1542 OPENSSL_memcpy(cert->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001543 return 1;
1544}
Adam Langley95c29f32014-06-20 12:00:00 -07001545
David Benjamin5960a902017-02-14 20:07:11 -05001546int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
1547 size_t sid_ctx_len) {
David Benjamin0ce090a2018-07-02 20:24:40 -04001548 return set_session_id_context(ctx->cert.get(), sid_ctx, sid_ctx_len);
David Benjamin5960a902017-02-14 20:07:11 -05001549}
1550
Adam Langleyfcf25832014-12-18 17:42:32 -08001551int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
David Benjamin30c4c302016-12-07 22:35:24 -05001552 size_t sid_ctx_len) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001553 if (!ssl->config) {
1554 return 0;
1555 }
David Benjamin0ce090a2018-07-02 20:24:40 -04001556 return set_session_id_context(ssl->config->cert.get(), sid_ctx, sid_ctx_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001557}
Adam Langley95c29f32014-06-20 12:00:00 -07001558
David Benjamin07820b52016-12-07 21:13:41 -05001559const uint8_t *SSL_get0_session_id_context(const SSL *ssl, size_t *out_len) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001560 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 Benjamin07820b52016-12-07 21:13:41 -05001567}
1568
David Benjamin066fe0a2015-10-17 21:11:33 -04001569int SSL_get_fd(const SSL *ssl) { return SSL_get_rfd(ssl); }
Adam Langley95c29f32014-06-20 12:00:00 -07001570
David Benjamin066fe0a2015-10-17 21:11:33 -04001571int SSL_get_rfd(const SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001572 int ret = -1;
David Benjamin066fe0a2015-10-17 21:11:33 -04001573 BIO *b = BIO_find_type(SSL_get_rbio(ssl), BIO_TYPE_DESCRIPTOR);
1574 if (b != NULL) {
1575 BIO_get_fd(b, &ret);
Adam Langleyfcf25832014-12-18 17:42:32 -08001576 }
1577 return ret;
1578}
Adam Langley95c29f32014-06-20 12:00:00 -07001579
David Benjamin066fe0a2015-10-17 21:11:33 -04001580int SSL_get_wfd(const SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001581 int ret = -1;
David Benjamin066fe0a2015-10-17 21:11:33 -04001582 BIO *b = BIO_find_type(SSL_get_wbio(ssl), BIO_TYPE_DESCRIPTOR);
1583 if (b != NULL) {
1584 BIO_get_fd(b, &ret);
Adam Langleyfcf25832014-12-18 17:42:32 -08001585 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001586 return ret;
1587}
Adam Langley95c29f32014-06-20 12:00:00 -07001588
David Benjamin7cb91d22023-07-14 16:48:05 -04001589#if !defined(OPENSSL_NO_SOCK)
David Benjamin066fe0a2015-10-17 21:11:33 -04001590int SSL_set_fd(SSL *ssl, int fd) {
David Benjamin22ce9b22016-03-06 19:26:52 -05001591 BIO *bio = BIO_new(BIO_s_socket());
Adam Langleyfcf25832014-12-18 17:42:32 -08001592 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001593 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
David Benjamin066fe0a2015-10-17 21:11:33 -04001594 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001595 }
1596 BIO_set_fd(bio, fd, BIO_NOCLOSE);
David Benjamin066fe0a2015-10-17 21:11:33 -04001597 SSL_set_bio(ssl, bio, bio);
1598 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001599}
Adam Langley95c29f32014-06-20 12:00:00 -07001600
David Benjamin066fe0a2015-10-17 21:11:33 -04001601int SSL_set_wfd(SSL *ssl, int fd) {
David Benjamin2f871122016-05-20 14:27:17 -04001602 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 Benjamin22ce9b22016-03-06 19:26:52 -05001605 BIO *bio = BIO_new(BIO_s_socket());
Adam Langleyfcf25832014-12-18 17:42:32 -08001606 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001607 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
David Benjamin066fe0a2015-10-17 21:11:33 -04001608 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001609 }
1610 BIO_set_fd(bio, fd, BIO_NOCLOSE);
David Benjamin4501bd52016-08-01 13:39:41 -04001611 SSL_set0_wbio(ssl, bio);
Adam Langleyfcf25832014-12-18 17:42:32 -08001612 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -04001613 // Copy the rbio over to the wbio.
David Benjamin4501bd52016-08-01 13:39:41 -04001614 BIO_up_ref(rbio);
1615 SSL_set0_wbio(ssl, rbio);
Adam Langleyfcf25832014-12-18 17:42:32 -08001616 }
1617
David Benjamin066fe0a2015-10-17 21:11:33 -04001618 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001619}
Adam Langley95c29f32014-06-20 12:00:00 -07001620
David Benjamin066fe0a2015-10-17 21:11:33 -04001621int SSL_set_rfd(SSL *ssl, int fd) {
David Benjamin2f871122016-05-20 14:27:17 -04001622 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 Benjamin22ce9b22016-03-06 19:26:52 -05001625 BIO *bio = BIO_new(BIO_s_socket());
Adam Langleyfcf25832014-12-18 17:42:32 -08001626 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001627 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
David Benjamin066fe0a2015-10-17 21:11:33 -04001628 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001629 }
1630 BIO_set_fd(bio, fd, BIO_NOCLOSE);
David Benjamin4501bd52016-08-01 13:39:41 -04001631 SSL_set0_rbio(ssl, bio);
Adam Langleyfcf25832014-12-18 17:42:32 -08001632 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -04001633 // Copy the wbio over to the rbio.
David Benjamin4501bd52016-08-01 13:39:41 -04001634 BIO_up_ref(wbio);
1635 SSL_set0_rbio(ssl, wbio);
Adam Langleyfcf25832014-12-18 17:42:32 -08001636 }
David Benjamin066fe0a2015-10-17 21:11:33 -04001637 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001638}
David Benjamin7cb91d22023-07-14 16:48:05 -04001639#endif // !OPENSSL_NO_SOCK
Adam Langley95c29f32014-06-20 12:00:00 -07001640
David Benjamin49ddf412016-10-08 11:56:01 -04001641static 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 Benjamin17cf2cb2016-12-13 01:07:13 -05001646 OPENSSL_memcpy(out, in, out_len);
David Benjamin49ddf412016-10-08 11:56:01 -04001647 return in_len;
1648}
1649
David Benjamin1a1b34d2015-10-17 12:51:52 -04001650size_t SSL_get_finished(const SSL *ssl, void *buf, size_t count) {
David Benjaminced00b42016-10-08 11:24:09 -04001651 if (!ssl->s3->initial_handshake_complete ||
David Benjamind1e3ce12017-10-06 18:31:15 -04001652 ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjaminced00b42016-10-08 11:24:09 -04001653 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001654 }
1655
David Benjamin49ddf412016-10-08 11:56:01 -04001656 if (ssl->server) {
1657 return copy_finished(buf, count, ssl->s3->previous_server_finished,
1658 ssl->s3->previous_server_finished_len);
David Benjaminced00b42016-10-08 11:24:09 -04001659 }
David Benjamin49ddf412016-10-08 11:56:01 -04001660
1661 return copy_finished(buf, count, ssl->s3->previous_client_finished,
1662 ssl->s3->previous_client_finished_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001663}
Adam Langley95c29f32014-06-20 12:00:00 -07001664
David Benjamin1a1b34d2015-10-17 12:51:52 -04001665size_t SSL_get_peer_finished(const SSL *ssl, void *buf, size_t count) {
David Benjaminced00b42016-10-08 11:24:09 -04001666 if (!ssl->s3->initial_handshake_complete ||
David Benjamind1e3ce12017-10-06 18:31:15 -04001667 ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjaminced00b42016-10-08 11:24:09 -04001668 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001669 }
Adam Langley95c29f32014-06-20 12:00:00 -07001670
David Benjamin49ddf412016-10-08 11:56:01 -04001671 if (ssl->server) {
1672 return copy_finished(buf, count, ssl->s3->previous_client_finished,
1673 ssl->s3->previous_client_finished_len);
David Benjaminced00b42016-10-08 11:24:09 -04001674 }
David Benjamin49ddf412016-10-08 11:56:01 -04001675
1676 return copy_finished(buf, count, ssl->s3->previous_server_finished,
1677 ssl->s3->previous_server_finished_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001678}
Adam Langley95c29f32014-06-20 12:00:00 -07001679
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001680int 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 Langley95c29f32014-06-20 12:00:00 -07001687
David Benjamin42fea372015-09-19 01:22:44 -04001688int SSL_get_extms_support(const SSL *ssl) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001689 // TLS 1.3 does not require extended master secret and always reports as
1690 // supporting it.
David Benjamina68c1182016-07-24 00:14:33 -04001691 if (!ssl->s3->have_version) {
1692 return 0;
1693 }
David Benjamind1e3ce12017-10-06 18:31:15 -04001694 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjaminfc02b592017-02-17 16:26:01 -05001695 return 1;
1696 }
1697
David Benjaminc11ea9422017-08-29 16:33:21 -04001698 // If the initial handshake completed, query the established session.
David Benjaminfc02b592017-02-17 16:26:01 -05001699 if (ssl->s3->established_session != NULL) {
1700 return ssl->s3->established_session->extended_master_secret;
1701 }
1702
David Benjaminc11ea9422017-08-29 16:33:21 -04001703 // Otherwise, query the in-progress handshake.
David Benjaminfc02b592017-02-17 16:26:01 -05001704 if (ssl->s3->hs != NULL) {
1705 return ssl->s3->hs->extended_master_secret;
1706 }
1707 assert(0);
1708 return 0;
Matt Braithwaitecd6f54b2015-09-17 12:54:42 -07001709}
1710
David Benjamin9a41d1b2015-05-16 01:30:09 -04001711int SSL_CTX_get_read_ahead(const SSL_CTX *ctx) { return 0; }
Adam Langley95c29f32014-06-20 12:00:00 -07001712
David Benjamin0d56f882015-12-19 17:05:56 -05001713int SSL_get_read_ahead(const SSL *ssl) { return 0; }
Adam Langley95c29f32014-06-20 12:00:00 -07001714
David Benjamin103ed082018-05-10 19:55:02 -04001715int SSL_CTX_set_read_ahead(SSL_CTX *ctx, int yes) { return 1; }
David Benjamin61ecccf2015-05-05 09:44:51 -04001716
David Benjamin103ed082018-05-10 19:55:02 -04001717int SSL_set_read_ahead(SSL *ssl, int yes) { return 1; }
David Benjamin61ecccf2015-05-05 09:44:51 -04001718
David Benjamin9f859492015-10-03 10:44:30 -04001719int SSL_pending(const SSL *ssl) {
David Benjamind9229f92017-10-06 17:36:20 -04001720 return static_cast<int>(ssl->s3->pending_app_data.size());
Adam Langleyfcf25832014-12-18 17:42:32 -08001721}
Adam Langley95c29f32014-06-20 12:00:00 -07001722
David Benjaminb3ed0712021-11-12 14:43:21 -05001723int SSL_has_pending(const SSL *ssl) {
1724 return SSL_pending(ssl) != 0 || !ssl->s3->read_buffer.empty();
1725}
1726
David Benjamin5d880142024-02-20 17:34:14 -05001727static 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 Langleyfcf25832014-12-18 17:42:32 -08001743int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
David Benjamin5d880142024-02-20 17:34:14 -05001744 // 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 Langleyc5ac2b62016-11-07 12:02:35 -08001747}
1748
Adam Langleyc5ac2b62016-11-07 12:02:35 -08001749int SSL_check_private_key(const SSL *ssl) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001750 if (!ssl->config) {
1751 return 0;
1752 }
David Benjamin5d880142024-02-20 17:34:14 -05001753
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 Langleyfcf25832014-12-18 17:42:32 -08001757}
Adam Langley95c29f32014-06-20 12:00:00 -07001758
David Benjamin42fea372015-09-19 01:22:44 -04001759long SSL_get_default_timeout(const SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001760 return SSL_DEFAULT_SESSION_TIMEOUT;
1761}
Adam Langley95c29f32014-06-20 12:00:00 -07001762
David Benjamin44d3eed2015-05-21 01:29:55 -04001763int SSL_renegotiate(SSL *ssl) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001764 // Caller-initiated renegotiation is not supported.
David Benjamin0e7dbd52019-05-15 16:01:18 -04001765 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 Benjamin9b2cdb72021-04-01 23:21:53 -04001775 // We should not have told the caller to release the private key.
1776 assert(!SSL_can_release_private_key(ssl));
1777
David Benjamin0e7dbd52019-05-15 16:01:18 -04001778 // 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 Langleyfcf25832014-12-18 17:42:32 -08001801}
Adam Langley95c29f32014-06-20 12:00:00 -07001802
David Benjamin44d3eed2015-05-21 01:29:55 -04001803int SSL_renegotiate_pending(SSL *ssl) {
1804 return SSL_in_init(ssl) && ssl->s3->initial_handshake_complete;
Adam Langleyfcf25832014-12-18 17:42:32 -08001805}
Adam Langley95c29f32014-06-20 12:00:00 -07001806
David Benjamin936aada2016-06-07 19:09:22 -04001807int SSL_total_renegotiations(const SSL *ssl) {
1808 return ssl->s3->total_renegotiations;
1809}
1810
David Benjamin1d0a1942015-04-26 15:35:35 -04001811size_t SSL_CTX_get_max_cert_list(const SSL_CTX *ctx) {
1812 return ctx->max_cert_list;
1813}
1814
1815void 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
1822size_t SSL_get_max_cert_list(const SSL *ssl) {
1823 return ssl->max_cert_list;
1824}
1825
1826void 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 Langleyce9d85e2016-01-24 15:58:39 -08001833int SSL_CTX_set_max_send_fragment(SSL_CTX *ctx, size_t max_send_fragment) {
David Benjamin1d0a1942015-04-26 15:35:35 -04001834 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 Langleyce9d85e2016-01-24 15:58:39 -08001841
1842 return 1;
David Benjamin1d0a1942015-04-26 15:35:35 -04001843}
1844
Adam Langleyce9d85e2016-01-24 15:58:39 -08001845int SSL_set_max_send_fragment(SSL *ssl, size_t max_send_fragment) {
David Benjamin1d0a1942015-04-26 15:35:35 -04001846 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 Langleyce9d85e2016-01-24 15:58:39 -08001853
1854 return 1;
David Benjamin1d0a1942015-04-26 15:35:35 -04001855}
1856
David Benjamincb9cf792015-05-05 09:46:14 -04001857int SSL_set_mtu(SSL *ssl, unsigned mtu) {
David Benjamince079fd2016-08-02 16:22:34 -04001858 if (!SSL_is_dtls(ssl) || mtu < dtls1_min_mtu()) {
David Benjamincb9cf792015-05-05 09:46:14 -04001859 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001860 }
David Benjamincb9cf792015-05-05 09:46:14 -04001861 ssl->d1->mtu = mtu;
1862 return 1;
1863}
1864
1865int SSL_get_secure_renegotiation_support(const SSL *ssl) {
David Benjamind2610042017-01-03 10:49:28 -05001866 if (!ssl->s3->have_version) {
1867 return 0;
1868 }
David Benjamind1e3ce12017-10-06 18:31:15 -04001869 return ssl_protocol_version(ssl) >= TLS1_3_VERSION ||
David Benjamind2610042017-01-03 10:49:28 -05001870 ssl->s3->send_connection_binding;
David Benjamincb9cf792015-05-05 09:46:14 -04001871}
1872
David Benjamin71f7d3d2015-05-05 09:46:38 -04001873size_t SSL_CTX_sess_number(const SSL_CTX *ctx) {
David Benjamin9eaa3bd2017-09-27 17:03:54 -04001874 MutexReadLock lock(const_cast<CRYPTO_MUTEX *>(&ctx->lock));
David Benjamin71f7d3d2015-05-05 09:46:38 -04001875 return lh_SSL_SESSION_num_items(ctx->sessions);
1876}
1877
1878unsigned 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
1884unsigned long SSL_CTX_sess_get_cache_size(const SSL_CTX *ctx) {
1885 return ctx->session_cache_size;
1886}
1887
1888int 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
1894int SSL_CTX_get_session_cache_mode(const SSL_CTX *ctx) {
1895 return ctx->session_cache_mode;
1896}
1897
David Benjamin936aada2016-06-07 19:09:22 -04001898
1899int 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 Kreichgauer72912d22017-08-04 12:06:43 -07001907
David Benjaminc11ea9422017-08-29 16:33:21 -04001908 // The default ticket keys are initialized lazily. Trigger a key
1909 // rotation to initialize them.
Martin Kreichgauer72912d22017-08-04 12:06:43 -07001910 if (!ssl_ctx_rotate_ticket_encryption_key(ctx)) {
1911 return 0;
1912 }
1913
David Benjaminf5260812017-07-12 17:16:39 -04001914 uint8_t *out_bytes = reinterpret_cast<uint8_t *>(out);
Martin Kreichgauer72912d22017-08-04 12:06:43 -07001915 MutexReadLock lock(&ctx->lock);
David Benjamin7bb0fbf2018-07-03 13:55:42 -04001916 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 Benjamin936aada2016-06-07 19:09:22 -04001919 return 1;
1920}
1921
1922int 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 Benjamin7bb0fbf2018-07-03 13:55:42 -04001930 auto key = MakeUnique<TicketKey>();
1931 if (!key) {
1932 return 0;
Martin Kreichgauer72912d22017-08-04 12:06:43 -07001933 }
David Benjaminf5260812017-07-12 17:16:39 -04001934 const uint8_t *in_bytes = reinterpret_cast<const uint8_t *>(in);
David Benjamin7bb0fbf2018-07-03 13:55:42 -04001935 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 Benjamin936aada2016-06-07 19:09:22 -04001943 return 1;
1944}
1945
1946int 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 Benjamin7bb0fbf2018-07-03 13:55:42 -04001950 ctx->ticket_key_cb = callback;
David Benjamin936aada2016-06-07 19:09:22 -04001951 return 1;
1952}
1953
David Benjamin6cf98202023-05-26 22:03:30 -04001954static 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
1964int 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
1970int 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 Benjamin2da5ba92023-05-26 21:23:39 -04001980static 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 Benjamin6cf98202023-05-26 22:03:30 -04001989 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
David Benjamin2da5ba92023-05-26 21:23:39 -04001990 return false;
1991 }
1992 }
1993
1994 *out_group_ids = std::move(group_ids);
1995 return true;
David Benjamin936aada2016-06-07 19:09:22 -04001996}
1997
David Benjamin2da5ba92023-05-26 21:23:39 -04001998int 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
2003int SSL_set1_groups(SSL *ssl, const int *groups, size_t num_groups) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002004 if (!ssl->config) {
2005 return 0;
2006 }
David Benjamin2da5ba92023-05-26 21:23:39 -04002007 return ssl_nids_to_group_ids(&ssl->config->supported_group_list,
2008 MakeConstSpan(groups, num_groups));
David Benjamin936aada2016-06-07 19:09:22 -04002009}
2010
David Benjamin2da5ba92023-05-26 21:23:39 -04002011static 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 Ghedini5fd18072016-09-28 21:04:25 +01002023
David Benjamin2da5ba92023-05-26 21:23:39 -04002024 Array<uint16_t> group_ids;
2025 if (!group_ids.Init(count)) {
2026 return false;
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002027 }
Alessandro Ghedini5fd18072016-09-28 21:04:25 +01002028
David Benjamin2da5ba92023-05-26 21:23:39 -04002029 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 Benjamin6cf98202023-05-26 22:03:30 -04002035 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
David Benjamin2da5ba92023-05-26 21:23:39 -04002036 return false;
2037 }
2038 if (col) {
2039 ptr = col + 1;
2040 }
2041 } while (col);
David Benjamine8e6cac2022-09-06 12:03:53 -04002042
David Benjamin2da5ba92023-05-26 21:23:39 -04002043 assert(i == count);
2044 *out_group_ids = std::move(group_ids);
2045 return true;
David Benjamine8e6cac2022-09-06 12:03:53 -04002046}
2047
2048int SSL_CTX_set1_groups_list(SSL_CTX *ctx, const char *groups) {
David Benjamin2da5ba92023-05-26 21:23:39 -04002049 return ssl_str_to_group_ids(&ctx->supported_group_list, groups);
David Benjamine8e6cac2022-09-06 12:03:53 -04002050}
2051
2052int SSL_set1_groups_list(SSL *ssl, const char *groups) {
David Benjamin2da5ba92023-05-26 21:23:39 -04002053 if (!ssl->config) {
2054 return 0;
2055 }
2056 return ssl_str_to_group_ids(&ssl->config->supported_group_list, groups);
David Benjamine8e6cac2022-09-06 12:03:53 -04002057}
2058
David Benjamin335523a2023-05-26 21:55:56 -04002059uint16_t SSL_get_group_id(const SSL *ssl) {
Steven Valdez87eab492016-06-27 16:34:59 -04002060 SSL_SESSION *session = SSL_get_session(ssl);
David Benjamin4882a6c2016-12-11 02:48:12 -05002061 if (session == NULL) {
David Benjamin9e68f192016-06-30 14:55:33 -04002062 return 0;
2063 }
2064
David Benjamin4882a6c2016-12-11 02:48:12 -05002065 return session->group_id;
David Benjamin9e68f192016-06-30 14:55:33 -04002066}
2067
David Benjamin6cf98202023-05-26 22:03:30 -04002068int 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 Benjamin936aada2016-06-07 19:09:22 -04002076int SSL_CTX_set_tmp_dh(SSL_CTX *ctx, const DH *dh) {
David Benjamin936aada2016-06-07 19:09:22 -04002077 return 1;
2078}
2079
2080int SSL_set_tmp_dh(SSL *ssl, const DH *dh) {
David Benjamin936aada2016-06-07 19:09:22 -04002081 return 1;
2082}
2083
David Benjamin70dbf042017-08-08 18:51:37 -04002084STACK_OF(SSL_CIPHER) *SSL_CTX_get_ciphers(const SSL_CTX *ctx) {
David Benjamin48b276d2018-04-12 17:37:32 -04002085 return ctx->cipher_list->ciphers.get();
David Benjamin8ebeabf2017-03-02 22:04:07 -05002086}
2087
David Benjamin70dbf042017-08-08 18:51:37 -04002088int SSL_CTX_cipher_in_group(const SSL_CTX *ctx, size_t i) {
David Benjamin48b276d2018-04-12 17:37:32 -04002089 if (i >= sk_SSL_CIPHER_num(ctx->cipher_list->ciphers.get())) {
David Benjamin70dbf042017-08-08 18:51:37 -04002090 return 0;
2091 }
2092 return ctx->cipher_list->in_group_flags[i];
2093}
2094
David Benjamin32876b32015-09-20 12:17:03 -04002095STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *ssl) {
2096 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002097 return NULL;
2098 }
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002099 if (ssl->config == NULL) {
2100 assert(ssl->config);
2101 return NULL;
2102 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07002103
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002104 return ssl->config->cipher_list ? ssl->config->cipher_list->ciphers.get()
2105 : ssl->ctx->cipher_list->ciphers.get();
Adam Langleyfcf25832014-12-18 17:42:32 -08002106}
Adam Langley95c29f32014-06-20 12:00:00 -07002107
David Benjamin32876b32015-09-20 12:17:03 -04002108const char *SSL_get_cipher_list(const SSL *ssl, int n) {
David Benjamin32876b32015-09-20 12:17:03 -04002109 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002110 return NULL;
2111 }
Adam Langley95c29f32014-06-20 12:00:00 -07002112
David Benjamin8ebeabf2017-03-02 22:04:07 -05002113 STACK_OF(SSL_CIPHER) *sk = SSL_get_ciphers(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08002114 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
2115 return NULL;
2116 }
Adam Langley95c29f32014-06-20 12:00:00 -07002117
David Benjamin8ebeabf2017-03-02 22:04:07 -05002118 const SSL_CIPHER *c = sk_SSL_CIPHER_value(sk, n);
Adam Langleyfcf25832014-12-18 17:42:32 -08002119 if (c == NULL) {
2120 return NULL;
2121 }
Adam Langley95c29f32014-06-20 12:00:00 -07002122
Adam Langleyfcf25832014-12-18 17:42:32 -08002123 return c->name;
2124}
David Benjamin5491e3f2014-09-29 19:33:09 -04002125
Adam Langleyfcf25832014-12-18 17:42:32 -08002126int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
Bob Beck8cacbd92023-04-05 14:38:38 -06002127 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 Braithwaitea57dcfb2017-02-17 22:08:23 -08002131}
2132
2133int SSL_CTX_set_strict_cipher_list(SSL_CTX *ctx, const char *str) {
Bob Beck8cacbd92023-04-05 14:38:38 -06002134 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 Langleyfcf25832014-12-18 17:42:32 -08002138}
David Benjamin39482a12014-07-20 13:30:15 -04002139
David Benjamin32876b32015-09-20 12:17:03 -04002140int SSL_set_cipher_list(SSL *ssl, const char *str) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002141 if (!ssl->config) {
2142 return 0;
2143 }
Bob Beck8cacbd92023-04-05 14:38:38 -06002144 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 Braithwaiteb7bc80a2018-04-13 15:51:30 -07002148 false /* not strict */);
Matthew Braithwaitea57dcfb2017-02-17 22:08:23 -08002149}
2150
2151int SSL_set_strict_cipher_list(SSL *ssl, const char *str) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002152 if (!ssl->config) {
2153 return 0;
2154 }
Bob Beck8cacbd92023-04-05 14:38:38 -06002155 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 Braithwaiteb7bc80a2018-04-13 15:51:30 -07002159 true /* strict */);
Adam Langleyfcf25832014-12-18 17:42:32 -08002160}
Adam Langley95c29f32014-06-20 12:00:00 -07002161
David Benjamin07e13842015-10-17 13:48:04 -04002162const char *SSL_get_servername(const SSL *ssl, const int type) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002163 if (type != TLSEXT_NAMETYPE_host_name) {
2164 return NULL;
2165 }
Adam Langley95c29f32014-06-20 12:00:00 -07002166
David Benjaminc11ea9422017-08-29 16:33:21 -04002167 // Historically, |SSL_get_servername| was also the configuration getter
2168 // corresponding to |SSL_set_tlsext_host_name|.
David Benjamin46853762018-07-03 14:01:26 -04002169 if (ssl->hostname != nullptr) {
2170 return ssl->hostname.get();
David Benjamin07e13842015-10-17 13:48:04 -04002171 }
2172
David Benjamin8e7bbba2017-10-13 17:18:35 -04002173 return ssl->s3->hostname.get();
Adam Langleyfcf25832014-12-18 17:42:32 -08002174}
Adam Langley95c29f32014-06-20 12:00:00 -07002175
David Benjamin07e13842015-10-17 13:48:04 -04002176int SSL_get_servername_type(const SSL *ssl) {
David Benjamina8614602017-09-06 15:40:19 -04002177 if (SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) == NULL) {
Steven Valdez87eab492016-06-27 16:34:59 -04002178 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002179 }
Steven Valdez87eab492016-06-27 16:34:59 -04002180 return TLSEXT_NAMETYPE_host_name;
Adam Langleyfcf25832014-12-18 17:42:32 -08002181}
Adam Langley95c29f32014-06-20 12:00:00 -07002182
David Benjamin3a1dd462017-07-11 16:13:10 -04002183void 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 Langleyfcf25832014-12-18 17:42:32 -08002188}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02002189
David Benjamin3a1dd462017-07-11 16:13:10 -04002190void SSL_set_custom_verify(
2191 SSL *ssl, int mode,
2192 enum ssl_verify_result_t (*callback)(SSL *ssl, uint8_t *out_alert)) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002193 if (!ssl->config) {
2194 return;
2195 }
2196 ssl->config->verify_mode = mode;
2197 ssl->config->custom_verify_callback = callback;
David Benjamin3a1dd462017-07-11 16:13:10 -04002198}
2199
2200void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
David Benjamin7e58c5e2017-10-11 13:01:08 -04002201 ctx->signed_cert_timestamps_enabled = true;
Adam Langleyfe366722017-03-07 16:41:04 -08002202}
2203
David Benjamin26e1ff32017-02-14 20:13:00 -05002204void SSL_enable_signed_cert_timestamps(SSL *ssl) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002205 if (!ssl->config) {
2206 return;
2207 }
2208 ssl->config->signed_cert_timestamps_enabled = true;
Adam Langleyfcf25832014-12-18 17:42:32 -08002209}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02002210
Adam Langleyfcf25832014-12-18 17:42:32 -08002211void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
David Benjamin7e58c5e2017-10-11 13:01:08 -04002212 ctx->ocsp_stapling_enabled = true;
Adam Langleyfcf25832014-12-18 17:42:32 -08002213}
David Benjamin6c7aed02014-08-27 16:42:38 -04002214
David Benjamin26e1ff32017-02-14 20:13:00 -05002215void SSL_enable_ocsp_stapling(SSL *ssl) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002216 if (!ssl->config) {
2217 return;
2218 }
2219 ssl->config->ocsp_stapling_enabled = true;
Adam Langleyfcf25832014-12-18 17:42:32 -08002220}
David Benjamin6c7aed02014-08-27 16:42:38 -04002221
Adam Langleyfcf25832014-12-18 17:42:32 -08002222void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
2223 size_t *out_len) {
Steven Valdez87eab492016-06-27 16:34:59 -04002224 SSL_SESSION *session = SSL_get_session(ssl);
David Benjamin8fc2dc02017-08-22 15:07:51 -07002225 if (ssl->server || !session || !session->signed_cert_timestamp_list) {
2226 *out_len = 0;
2227 *out = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002228 return;
2229 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02002230
David Benjaminbfdd1a92018-06-29 16:26:38 -04002231 *out = CRYPTO_BUFFER_data(session->signed_cert_timestamp_list.get());
2232 *out_len = CRYPTO_BUFFER_len(session->signed_cert_timestamp_list.get());
Adam Langleyfcf25832014-12-18 17:42:32 -08002233}
David Benjamin6c7aed02014-08-27 16:42:38 -04002234
Adam Langleyfcf25832014-12-18 17:42:32 -08002235void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
2236 size_t *out_len) {
Steven Valdez87eab492016-06-27 16:34:59 -04002237 SSL_SESSION *session = SSL_get_session(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08002238 if (ssl->server || !session || !session->ocsp_response) {
David Benjamin8fc2dc02017-08-22 15:07:51 -07002239 *out_len = 0;
2240 *out = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002241 return;
2242 }
David Benjamin8fc2dc02017-08-22 15:07:51 -07002243
David Benjaminbfdd1a92018-06-29 16:26:38 -04002244 *out = CRYPTO_BUFFER_data(session->ocsp_response.get());
2245 *out_len = CRYPTO_BUFFER_len(session->ocsp_response.get());
Adam Langleyfcf25832014-12-18 17:42:32 -08002246}
David Benjamin6c7aed02014-08-27 16:42:38 -04002247
David Benjamin936aada2016-06-07 19:09:22 -04002248int SSL_set_tlsext_host_name(SSL *ssl, const char *name) {
David Benjamin46853762018-07-03 14:01:26 -04002249 ssl->hostname.reset();
David Benjamin50596f82018-07-02 19:47:27 -04002250 if (name == nullptr) {
David Benjamin936aada2016-06-07 19:09:22 -04002251 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 Benjamin3ba95862019-10-21 16:14:33 -04002259 ssl->hostname.reset(OPENSSL_strdup(name));
David Benjamin46853762018-07-03 14:01:26 -04002260 if (ssl->hostname == nullptr) {
David Benjamin936aada2016-06-07 19:09:22 -04002261 return 0;
2262 }
2263 return 1;
2264}
2265
2266int SSL_CTX_set_tlsext_servername_callback(
2267 SSL_CTX *ctx, int (*callback)(SSL *ssl, int *out_alert, void *arg)) {
David Benjamin46853762018-07-03 14:01:26 -04002268 ctx->servername_callback = callback;
David Benjamin936aada2016-06-07 19:09:22 -04002269 return 1;
2270}
2271
2272int SSL_CTX_set_tlsext_servername_arg(SSL_CTX *ctx, void *arg) {
David Benjamin46853762018-07-03 14:01:26 -04002273 ctx->servername_arg = arg;
David Benjamin936aada2016-06-07 19:09:22 -04002274 return 1;
2275}
2276
David Benjaminca743582017-06-15 17:51:35 -04002277int 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 Langleyfcf25832014-12-18 17:42:32 -08002280 const uint8_t *result;
David Benjaminca743582017-06-15 17:51:35 -04002281 int status;
Adam Langley95c29f32014-06-20 12:00:00 -07002282
David Benjaminc11ea9422017-08-29 16:33:21 -04002283 // For each protocol in peer preference order, see if we support it.
David Benjaminca743582017-06-15 17:51:35 -04002284 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 Benjaminc11ea9422017-08-29 16:33:21 -04002288 // We found a match
David Benjaminca743582017-06-15 17:51:35 -04002289 result = &peer[i];
Adam Langleyfcf25832014-12-18 17:42:32 -08002290 status = OPENSSL_NPN_NEGOTIATED;
2291 goto found;
2292 }
David Benjaminca743582017-06-15 17:51:35 -04002293 j += supported[j];
Adam Langleyfcf25832014-12-18 17:42:32 -08002294 j++;
2295 }
David Benjaminca743582017-06-15 17:51:35 -04002296 i += peer[i];
Adam Langleyfcf25832014-12-18 17:42:32 -08002297 i++;
2298 }
Adam Langley95c29f32014-06-20 12:00:00 -07002299
David Benjaminc11ea9422017-08-29 16:33:21 -04002300 // There's no overlap between our protocols and the peer's list.
David Benjaminca743582017-06-15 17:51:35 -04002301 result = supported;
Adam Langleyfcf25832014-12-18 17:42:32 -08002302 status = OPENSSL_NPN_NO_OVERLAP;
Adam Langley95c29f32014-06-20 12:00:00 -07002303
Adam Langleyfcf25832014-12-18 17:42:32 -08002304found:
2305 *out = (uint8_t *)result + 1;
David Benjamin977547b2015-09-16 00:25:52 -04002306 *out_len = result[0];
Adam Langleyfcf25832014-12-18 17:42:32 -08002307 return status;
2308}
Adam Langley95c29f32014-06-20 12:00:00 -07002309
David Benjamin977547b2015-09-16 00:25:52 -04002310void SSL_get0_next_proto_negotiated(const SSL *ssl, const uint8_t **out_data,
2311 unsigned *out_len) {
David Benjamin7ac94aa2022-10-24 15:44:37 -04002312 // NPN protocols have one-byte lengths, so they must fit in |unsigned|.
2313 assert(ssl->s3->next_proto_negotiated.size() <= UINT_MAX);
David Benjamin8e7bbba2017-10-13 17:18:35 -04002314 *out_data = ssl->s3->next_proto_negotiated.data();
David Benjamin7ac94aa2022-10-24 15:44:37 -04002315 *out_len = static_cast<unsigned>(ssl->s3->next_proto_negotiated.size());
Adam Langley95c29f32014-06-20 12:00:00 -07002316}
2317
Adam Langleyfcf25832014-12-18 17:42:32 -08002318void SSL_CTX_set_next_protos_advertised_cb(
2319 SSL_CTX *ctx,
David Benjamin977547b2015-09-16 00:25:52 -04002320 int (*cb)(SSL *ssl, const uint8_t **out, unsigned *out_len, void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08002321 void *arg) {
2322 ctx->next_protos_advertised_cb = cb;
2323 ctx->next_protos_advertised_cb_arg = arg;
2324}
Adam Langley95c29f32014-06-20 12:00:00 -07002325
Adam Langleyfcf25832014-12-18 17:42:32 -08002326void SSL_CTX_set_next_proto_select_cb(
David Benjamin977547b2015-09-16 00:25:52 -04002327 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 Langleyfcf25832014-12-18 17:42:32 -08002329 void *arg) {
2330 ctx->next_proto_select_cb = cb;
2331 ctx->next_proto_select_cb_arg = arg;
2332}
Adam Langley95c29f32014-06-20 12:00:00 -07002333
Adam Langleyfcf25832014-12-18 17:42:32 -08002334int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
David Benjamin7ac94aa2022-10-24 15:44:37 -04002335 size_t protos_len) {
David Benjamin12a3e7e2021-04-13 11:47:36 -04002336 // 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 Langleyfcf25832014-12-18 17:42:32 -08002343}
Adam Langley95c29f32014-06-20 12:00:00 -07002344
David Benjamin7ac94aa2022-10-24 15:44:37 -04002345int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, size_t protos_len) {
David Benjamin12a3e7e2021-04-13 11:47:36 -04002346 // Note this function's return value is backwards.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002347 if (!ssl->config) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002348 return 1;
2349 }
David Benjamin12a3e7e2021-04-13 11:47:36 -04002350 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 Langleyfcf25832014-12-18 17:42:32 -08002356}
Adam Langley95c29f32014-06-20 12:00:00 -07002357
Adam Langleyfcf25832014-12-18 17:42:32 -08002358void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
2359 int (*cb)(SSL *ssl, const uint8_t **out,
David Benjamin8984f1f2015-09-16 00:10:19 -04002360 uint8_t *out_len, const uint8_t *in,
2361 unsigned in_len, void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08002362 void *arg) {
2363 ctx->alpn_select_cb = cb;
2364 ctx->alpn_select_cb_arg = arg;
2365}
Adam Langley95c29f32014-06-20 12:00:00 -07002366
David Benjamin8984f1f2015-09-16 00:10:19 -04002367void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **out_data,
2368 unsigned *out_len) {
David Benjamin7ac94aa2022-10-24 15:44:37 -04002369 Span<const uint8_t> protocol;
Steven Valdeze831a812017-03-09 14:56:07 -05002370 if (SSL_in_early_data(ssl) && !ssl->server) {
David Benjamin7ac94aa2022-10-24 15:44:37 -04002371 protocol = ssl->s3->hs->early_session->early_alpn;
Adam Langleyfcf25832014-12-18 17:42:32 -08002372 } else {
David Benjamin7ac94aa2022-10-24 15:44:37 -04002373 protocol = ssl->s3->alpn_selected;
Adam Langleyfcf25832014-12-18 17:42:32 -08002374 }
David Benjamin7ac94aa2022-10-24 15:44:37 -04002375 // 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 Langleyfcf25832014-12-18 17:42:32 -08002379}
Adam Langley95c29f32014-06-20 12:00:00 -07002380
David Benjaminc8ff30c2017-04-04 13:52:36 -04002381void SSL_CTX_set_allow_unknown_alpn_protos(SSL_CTX *ctx, int enabled) {
2382 ctx->allow_unknown_alpn_protos = !!enabled;
2383}
David Benjamin936aada2016-06-07 19:09:22 -04002384
Steven Valdez51607f12020-08-05 10:46:05 -04002385int 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
2400void 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
2410int 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 Tan558960d2023-06-23 15:04:33 +00002415void 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 Langley85967952018-07-03 08:04:58 -07002422int 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 Benjamin8fe15842019-10-08 16:57:38 -04002427 for (const auto &alg : ctx->cert_compression_algs) {
2428 if (alg.alg_id == alg_id) {
Adam Langley85967952018-07-03 08:04:58 -07002429 return 0;
2430 }
2431 }
2432
David Benjamin8fe15842019-10-08 16:57:38 -04002433 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 Langley85967952018-07-03 08:04:58 -07002438}
2439
David Benjamineebd3c82016-12-06 17:43:58 -05002440void SSL_CTX_set_tls_channel_id_enabled(SSL_CTX *ctx, int enabled) {
David Benjamin46853762018-07-03 14:01:26 -04002441 ctx->channel_id_enabled = !!enabled;
David Benjamineebd3c82016-12-06 17:43:58 -05002442}
2443
David Benjamin936aada2016-06-07 19:09:22 -04002444int SSL_CTX_enable_tls_channel_id(SSL_CTX *ctx) {
David Benjamineebd3c82016-12-06 17:43:58 -05002445 SSL_CTX_set_tls_channel_id_enabled(ctx, 1);
David Benjamin936aada2016-06-07 19:09:22 -04002446 return 1;
2447}
2448
David Benjamineebd3c82016-12-06 17:43:58 -05002449void SSL_set_tls_channel_id_enabled(SSL *ssl, int enabled) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002450 if (!ssl->config) {
2451 return;
2452 }
David Benjamin46853762018-07-03 14:01:26 -04002453 ssl->config->channel_id_enabled = !!enabled;
David Benjamineebd3c82016-12-06 17:43:58 -05002454}
2455
David Benjamin936aada2016-06-07 19:09:22 -04002456int SSL_enable_tls_channel_id(SSL *ssl) {
David Benjamineebd3c82016-12-06 17:43:58 -05002457 SSL_set_tls_channel_id_enabled(ssl, 1);
David Benjamin936aada2016-06-07 19:09:22 -04002458 return 1;
2459}
2460
2461static 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
2468int 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 Benjamin46853762018-07-03 14:01:26 -04002474 ctx->channel_id_private = UpRef(private_key);
David Benjamin936aada2016-06-07 19:09:22 -04002475 return 1;
2476}
2477
2478int SSL_set1_tls_channel_id(SSL *ssl, EVP_PKEY *private_key) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002479 if (!ssl->config) {
2480 return 0;
2481 }
David Benjamin936aada2016-06-07 19:09:22 -04002482 if (!is_p256_key(private_key)) {
2483 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256);
2484 return 0;
2485 }
2486
David Benjamin46853762018-07-03 14:01:26 -04002487 ssl->config->channel_id_private = UpRef(private_key);
David Benjamin936aada2016-06-07 19:09:22 -04002488 return 1;
2489}
2490
2491size_t SSL_get_tls_channel_id(SSL *ssl, uint8_t *out, size_t max_out) {
David Benjamin46853762018-07-03 14:01:26 -04002492 if (!ssl->s3->channel_id_valid) {
David Benjamin936aada2016-06-07 19:09:22 -04002493 return 0;
2494 }
David Benjamin46853762018-07-03 14:01:26 -04002495 OPENSSL_memcpy(out, ssl->s3->channel_id, (max_out < 64) ? max_out : 64);
David Benjamin936aada2016-06-07 19:09:22 -04002496 return 64;
2497}
2498
David Benjaminbc118ee62018-07-10 17:27:45 -04002499size_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 Benjamin936aada2016-06-07 19:09:22 -04002503 }
David Benjaminbc118ee62018-07-10 17:27:45 -04002504 *out_types = types.data();
2505 return types.size();
2506}
2507
2508size_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 Benjamin936aada2016-06-07 19:09:22 -04002516}
2517
Watson Ladddcd6e442020-08-10 15:12:45 -04002518size_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 Benjamin0d56f882015-12-19 17:05:56 -05002528EVP_PKEY *SSL_get_privatekey(const SSL *ssl) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002529 if (!ssl->config) {
2530 assert(ssl->config);
David Benjamin281053e2024-02-13 22:52:54 -05002531 return nullptr;
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002532 }
David Benjamin281053e2024-02-13 22:52:54 -05002533 return ssl->config->cert->privatekey.get();
Adam Langleyfcf25832014-12-18 17:42:32 -08002534}
2535
Adam Langleyfcf25832014-12-18 17:42:32 -08002536EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
David Benjamin281053e2024-02-13 22:52:54 -05002537 return ctx->cert->privatekey.get();
Adam Langleyfcf25832014-12-18 17:42:32 -08002538}
2539
David Benjamin42fea372015-09-19 01:22:44 -04002540const SSL_CIPHER *SSL_get_current_cipher(const SSL *ssl) {
Steven Valdez384d0ea2018-11-06 10:45:36 -05002541 const SSL_SESSION *session = SSL_get_session(ssl);
2542 return session == nullptr ? nullptr : session->cipher;
Adam Langleyfcf25832014-12-18 17:42:32 -08002543}
2544
David Benjamin936aada2016-06-07 19:09:22 -04002545int SSL_session_reused(const SSL *ssl) {
Steven Valdeze831a812017-03-09 14:56:07 -05002546 return ssl->s3->session_reused || SSL_in_early_data(ssl);
David Benjamin936aada2016-06-07 19:09:22 -04002547}
2548
David Benjamin0d56f882015-12-19 17:05:56 -05002549const COMP_METHOD *SSL_get_current_compression(SSL *ssl) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002550
David Benjamin0d56f882015-12-19 17:05:56 -05002551const COMP_METHOD *SSL_get_current_expansion(SSL *ssl) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002552
David Benjamin103ed082018-05-10 19:55:02 -04002553int SSL_get_server_tmp_key(SSL *ssl, EVP_PKEY **out_key) { return 0; }
Adam Langley29ec5d12016-03-01 16:12:28 -08002554
Adam Langleyfcf25832014-12-18 17:42:32 -08002555void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
Adam Langleybb85f3d2015-10-28 18:44:11 -07002556 ctx->quiet_shutdown = (mode != 0);
Adam Langleyfcf25832014-12-18 17:42:32 -08002557}
2558
2559int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
2560 return ctx->quiet_shutdown;
2561}
2562
Adam Langleybb85f3d2015-10-28 18:44:11 -07002563void SSL_set_quiet_shutdown(SSL *ssl, int mode) {
2564 ssl->quiet_shutdown = (mode != 0);
2565}
Adam Langleyfcf25832014-12-18 17:42:32 -08002566
David Benjamin9f859492015-10-03 10:44:30 -04002567int SSL_get_quiet_shutdown(const SSL *ssl) { return ssl->quiet_shutdown; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002568
David Benjamin63006a92015-10-18 00:00:06 -04002569void SSL_set_shutdown(SSL *ssl, int mode) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002570 // 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 Benjaminfa214e42016-05-10 17:03:10 -04002572 assert((SSL_get_shutdown(ssl) & mode) == SSL_get_shutdown(ssl));
David Benjamin63006a92015-10-18 00:00:06 -04002573
David Benjaminfa214e42016-05-10 17:03:10 -04002574 if (mode & SSL_RECEIVED_SHUTDOWN &&
David Benjamin23c25d52017-10-06 16:02:47 -04002575 ssl->s3->read_shutdown == ssl_shutdown_none) {
2576 ssl->s3->read_shutdown = ssl_shutdown_close_notify;
David Benjaminfa214e42016-05-10 17:03:10 -04002577 }
2578
2579 if (mode & SSL_SENT_SHUTDOWN &&
David Benjamin23c25d52017-10-06 16:02:47 -04002580 ssl->s3->write_shutdown == ssl_shutdown_none) {
2581 ssl->s3->write_shutdown = ssl_shutdown_close_notify;
David Benjaminfa214e42016-05-10 17:03:10 -04002582 }
David Benjamin63006a92015-10-18 00:00:06 -04002583}
Adam Langleyfcf25832014-12-18 17:42:32 -08002584
David Benjaminfa214e42016-05-10 17:03:10 -04002585int SSL_get_shutdown(const SSL *ssl) {
2586 int ret = 0;
David Benjamin23c25d52017-10-06 16:02:47 -04002587 if (ssl->s3->read_shutdown != ssl_shutdown_none) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002588 // Historically, OpenSSL set |SSL_RECEIVED_SHUTDOWN| on both close_notify
2589 // and fatal alert.
David Benjaminfa214e42016-05-10 17:03:10 -04002590 ret |= SSL_RECEIVED_SHUTDOWN;
2591 }
David Benjamin23c25d52017-10-06 16:02:47 -04002592 if (ssl->s3->write_shutdown == ssl_shutdown_close_notify) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002593 // Historically, OpenSSL set |SSL_SENT_SHUTDOWN| on only close_notify.
David Benjaminfa214e42016-05-10 17:03:10 -04002594 ret |= SSL_SENT_SHUTDOWN;
2595 }
2596 return ret;
2597}
Adam Langleyfcf25832014-12-18 17:42:32 -08002598
David Benjamin50596f82018-07-02 19:47:27 -04002599SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx.get(); }
Adam Langleyfcf25832014-12-18 17:42:32 -08002600
2601SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002602 if (!ssl->config) {
2603 return NULL;
2604 }
David Benjamin50596f82018-07-02 19:47:27 -04002605 if (ssl->ctx.get() == ctx) {
2606 return ssl->ctx.get();
Adam Langleyfcf25832014-12-18 17:42:32 -08002607 }
2608
David Benjaminc11ea9422017-08-29 16:33:21 -04002609 // One cannot change the X.509 callbacks during a connection.
Adam Langley3509dac2017-02-01 11:59:18 -08002610 if (ssl->ctx->x509_method != ctx->x509_method) {
2611 assert(0);
2612 return NULL;
2613 }
2614
David Benjamin0ce090a2018-07-02 20:24:40 -04002615 UniquePtr<CERT> new_cert = ssl_cert_dup(ctx->cert.get());
2616 if (!new_cert) {
2617 return nullptr;
2618 }
David Benjamin2755a3e2015-04-22 16:17:58 -04002619
David Benjamin0ce090a2018-07-02 20:24:40 -04002620 ssl->config->cert = std::move(new_cert);
David Benjamin50596f82018-07-02 19:47:27 -04002621 ssl->ctx = UpRef(ctx);
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002622 ssl->enable_early_data = ssl->ctx->enable_early_data;
Adam Langleyfcf25832014-12-18 17:42:32 -08002623
David Benjamin50596f82018-07-02 19:47:27 -04002624 return ssl->ctx.get();
Adam Langleyfcf25832014-12-18 17:42:32 -08002625}
2626
Adam Langley95c29f32014-06-20 12:00:00 -07002627void SSL_set_info_callback(SSL *ssl,
David Benjamin82170242015-10-17 22:51:17 -04002628 void (*cb)(const SSL *ssl, int type, int value)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002629 ssl->info_callback = cb;
2630}
Adam Langley95c29f32014-06-20 12:00:00 -07002631
David Benjamin82170242015-10-17 22:51:17 -04002632void (*SSL_get_info_callback(const SSL *ssl))(const SSL *ssl, int type,
2633 int value) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002634 return ssl->info_callback;
2635}
Adam Langley95c29f32014-06-20 12:00:00 -07002636
David Benjamincb0c29f2016-12-12 17:00:50 -05002637int SSL_state(const SSL *ssl) {
David Benjamin9e766d72017-02-10 23:14:17 -05002638 return SSL_in_init(ssl) ? SSL_ST_INIT : SSL_ST_OK;
David Benjamincb0c29f2016-12-12 17:00:50 -05002639}
Adam Langley95c29f32014-06-20 12:00:00 -07002640
David Benjaminece089c2015-05-15 23:52:42 -04002641void SSL_set_state(SSL *ssl, int state) { }
Adam Langley95c29f32014-06-20 12:00:00 -07002642
Emily Stark95a79ee2016-02-25 21:12:28 -08002643char *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 Vohr3ab3b122019-10-21 09:51:32 -07002651int 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 Valdezc8e0f902018-07-14 11:23:01 -04002656int 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 Ghedini3cbb0292018-12-13 13:53:57 +00002664int 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 Benjamin8a589332015-12-04 23:14:35 -05002672int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
David Benjamind94682d2017-05-14 17:10:18 -04002673 CRYPTO_EX_dup *dup_unused, CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002674 int index;
2675 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp,
David Benjamind94682d2017-05-14 17:10:18 -04002676 free_func)) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002677 return -1;
2678 }
2679 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002680}
Adam Langley95c29f32014-06-20 12:00:00 -07002681
David Benjamin27e377e2017-07-31 19:09:42 -04002682int SSL_set_ex_data(SSL *ssl, int idx, void *data) {
2683 return CRYPTO_set_ex_data(&ssl->ex_data, idx, data);
Adam Langleyfcf25832014-12-18 17:42:32 -08002684}
Adam Langley95c29f32014-06-20 12:00:00 -07002685
David Benjamin1ac08fe2015-09-15 01:36:22 -04002686void *SSL_get_ex_data(const SSL *ssl, int idx) {
2687 return CRYPTO_get_ex_data(&ssl->ex_data, idx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002688}
Adam Langley95c29f32014-06-20 12:00:00 -07002689
David Benjamin8a589332015-12-04 23:14:35 -05002690int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
David Benjamind94682d2017-05-14 17:10:18 -04002691 CRYPTO_EX_dup *dup_unused,
Adam Langleyfcf25832014-12-18 17:42:32 -08002692 CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002693 int index;
2694 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp,
David Benjamind94682d2017-05-14 17:10:18 -04002695 free_func)) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002696 return -1;
2697 }
2698 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002699}
Adam Langley95c29f32014-06-20 12:00:00 -07002700
David Benjamin27e377e2017-07-31 19:09:42 -04002701int SSL_CTX_set_ex_data(SSL_CTX *ctx, int idx, void *data) {
2702 return CRYPTO_set_ex_data(&ctx->ex_data, idx, data);
Adam Langleyfcf25832014-12-18 17:42:32 -08002703}
Adam Langley95c29f32014-06-20 12:00:00 -07002704
David Benjamin1ac08fe2015-09-15 01:36:22 -04002705void *SSL_CTX_get_ex_data(const SSL_CTX *ctx, int idx) {
2706 return CRYPTO_get_ex_data(&ctx->ex_data, idx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002707}
Adam Langley95c29f32014-06-20 12:00:00 -07002708
David Benjaminebd8b892022-07-27 18:41:51 -07002709int 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 Langley95c29f32014-06-20 12:00:00 -07002716
Adam Langleyfcf25832014-12-18 17:42:32 -08002717void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
2718 RSA *(*cb)(SSL *ssl, int is_export,
David Benjamin27e377e2017-07-31 19:09:42 -04002719 int keylength)) {}
Adam Langley95c29f32014-06-20 12:00:00 -07002720
Adam Langleyfcf25832014-12-18 17:42:32 -08002721void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
David Benjamin27e377e2017-07-31 19:09:42 -04002722 int keylength)) {}
Adam Langleyfcf25832014-12-18 17:42:32 -08002723
2724void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
David Benjamin27e377e2017-07-31 19:09:42 -04002725 DH *(*cb)(SSL *ssl, int is_export,
2726 int keylength)) {}
Adam Langleyfcf25832014-12-18 17:42:32 -08002727
David Benjamin27e377e2017-07-31 19:09:42 -04002728void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*cb)(SSL *ssl, int is_export,
2729 int keylength)) {}
Adam Langleyfcf25832014-12-18 17:42:32 -08002730
David Benjamin0ce090a2018-07-02 20:24:40 -04002731static int use_psk_identity_hint(UniquePtr<char> *out,
2732 const char *identity_hint) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002733 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
David Benjamin3570d732015-06-29 00:28:17 -04002734 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -08002735 return 0;
2736 }
2737
David Benjaminc11ea9422017-08-29 16:33:21 -04002738 // Clear currently configured hint, if any.
David Benjamin0ce090a2018-07-02 20:24:40 -04002739 out->reset();
Adam Langleyfcf25832014-12-18 17:42:32 -08002740
David Benjaminc11ea9422017-08-29 16:33:21 -04002741 // 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 Benjamin78679342016-09-16 19:42:05 -04002745 if (identity_hint != NULL && identity_hint[0] != '\0') {
David Benjamin3ba95862019-10-21 16:14:33 -04002746 out->reset(OPENSSL_strdup(identity_hint));
David Benjamin0ce090a2018-07-02 20:24:40 -04002747 if (*out == nullptr) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002748 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002749 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002750 }
Adam Langley95c29f32014-06-20 12:00:00 -07002751
Adam Langleyfcf25832014-12-18 17:42:32 -08002752 return 1;
2753}
Adam Langley95c29f32014-06-20 12:00:00 -07002754
David Benjamin0a9bf662017-06-21 16:34:15 -04002755int 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
2759int SSL_use_psk_identity_hint(SSL *ssl, const char *identity_hint) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002760 if (!ssl->config) {
2761 return 0;
2762 }
2763 return use_psk_identity_hint(&ssl->config->psk_identity_hint, identity_hint);
David Benjamin0a9bf662017-06-21 16:34:15 -04002764}
2765
David Benjamine8814df2015-09-15 08:05:54 -04002766const char *SSL_get_psk_identity_hint(const SSL *ssl) {
2767 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002768 return NULL;
2769 }
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002770 if (ssl->config == NULL) {
2771 assert(ssl->config);
2772 return NULL;
2773 }
David Benjamin0ce090a2018-07-02 20:24:40 -04002774 return ssl->config->psk_identity_hint.get();
Adam Langleyfcf25832014-12-18 17:42:32 -08002775}
Adam Langley95c29f32014-06-20 12:00:00 -07002776
David Benjamine8814df2015-09-15 08:05:54 -04002777const char *SSL_get_psk_identity(const SSL *ssl) {
Steven Valdez87eab492016-06-27 16:34:59 -04002778 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002779 return NULL;
2780 }
Steven Valdez87eab492016-06-27 16:34:59 -04002781 SSL_SESSION *session = SSL_get_session(ssl);
2782 if (session == NULL) {
2783 return NULL;
2784 }
David Benjaminbfdd1a92018-06-29 16:26:38 -04002785 return session->psk_identity.get();
Adam Langleyfcf25832014-12-18 17:42:32 -08002786}
Adam Langley95c29f32014-06-20 12:00:00 -07002787
Adam Langleyfcf25832014-12-18 17:42:32 -08002788void SSL_set_psk_client_callback(
David Benjamine8814df2015-09-15 08:05:54 -04002789 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 Braithwaiteb7bc80a2018-04-13 15:51:30 -07002792 if (!ssl->config) {
2793 return;
2794 }
2795 ssl->config->psk_client_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002796}
Adam Langley95c29f32014-06-20 12:00:00 -07002797
Adam Langleyfcf25832014-12-18 17:42:32 -08002798void SSL_CTX_set_psk_client_callback(
David Benjamine8814df2015-09-15 08:05:54 -04002799 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 Langleyfcf25832014-12-18 17:42:32 -08002802 ctx->psk_client_callback = cb;
2803}
Adam Langley95c29f32014-06-20 12:00:00 -07002804
Adam Langleyfcf25832014-12-18 17:42:32 -08002805void SSL_set_psk_server_callback(
David Benjamine8814df2015-09-15 08:05:54 -04002806 SSL *ssl, unsigned (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
2807 unsigned max_psk_len)) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002808 if (!ssl->config) {
2809 return;
2810 }
2811 ssl->config->psk_server_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002812}
Adam Langley95c29f32014-06-20 12:00:00 -07002813
Adam Langleyfcf25832014-12-18 17:42:32 -08002814void SSL_CTX_set_psk_server_callback(
David Benjamine8814df2015-09-15 08:05:54 -04002815 SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *identity,
2816 uint8_t *psk, unsigned max_psk_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002817 ctx->psk_server_callback = cb;
2818}
Adam Langley95c29f32014-06-20 12:00:00 -07002819
Adam Langleyfcf25832014-12-18 17:42:32 -08002820void 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 Benjamin59015c32015-04-26 13:13:55 -04002824 ctx->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002825}
David Benjamin61ecccf2015-05-05 09:44:51 -04002826
2827void SSL_CTX_set_msg_callback_arg(SSL_CTX *ctx, void *arg) {
2828 ctx->msg_callback_arg = arg;
2829}
2830
Adam Langleyfcf25832014-12-18 17:42:32 -08002831void 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 Benjamin59015c32015-04-26 13:13:55 -04002835 ssl->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002836}
Adam Langley95c29f32014-06-20 12:00:00 -07002837
David Benjamin61ecccf2015-05-05 09:44:51 -04002838void SSL_set_msg_callback_arg(SSL *ssl, void *arg) {
2839 ssl->msg_callback_arg = arg;
2840}
2841
David Benjamind28f59c2015-11-17 22:32:50 -05002842void SSL_CTX_set_keylog_callback(SSL_CTX *ctx,
2843 void (*cb)(const SSL *ssl, const char *line)) {
2844 ctx->keylog_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002845}
Adam Langley95c29f32014-06-20 12:00:00 -07002846
David Benjamin6e3f5cc2016-09-14 14:43:14 -04002847void (*SSL_CTX_get_keylog_callback(const SSL_CTX *ctx))(const SSL *ssl,
2848 const char *line) {
2849 return ctx->keylog_callback;
2850}
2851
Taylor Brandstetter9edb2c62016-06-08 15:26:59 -07002852void 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 Benjamin9b2cdb72021-04-01 23:21:53 -04002858int 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 Benjamin5d8b1282015-10-17 23:26:35 -04002869int SSL_is_init_finished(const SSL *ssl) {
David Benjamin9e766d72017-02-10 23:14:17 -05002870 return !SSL_in_init(ssl);
David Benjamin5d8b1282015-10-17 23:26:35 -04002871}
2872
2873int SSL_in_init(const SSL *ssl) {
Steven Valdezd8168742017-08-31 10:15:48 -04002874 // 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 Benjamin8e7bbba2017-10-13 17:18:35 -04002877 SSL_HANDSHAKE *hs = ssl->s3->hs.get();
Steven Valdezd8168742017-08-31 10:15:48 -04002878 return hs != nullptr && !hs->handshake_finalized;
David Benjamin5d8b1282015-10-17 23:26:35 -04002879}
2880
2881int SSL_in_false_start(const SSL *ssl) {
David Benjamina0486782016-10-06 19:11:32 -04002882 if (ssl->s3->hs == NULL) {
2883 return 0;
2884 }
2885 return ssl->s3->hs->in_false_start;
David Benjamined7c4752015-02-16 19:16:46 -05002886}
2887
David Benjamin0d56f882015-12-19 17:05:56 -05002888int SSL_cutthrough_complete(const SSL *ssl) {
2889 return SSL_in_false_start(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08002890}
Adam Langley95c29f32014-06-20 12:00:00 -07002891
David Benjamince079fd2016-08-02 16:22:34 -04002892int SSL_is_server(const SSL *ssl) { return ssl->server; }
2893
2894int SSL_is_dtls(const SSL *ssl) { return ssl->method->is_dtls; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002895
Alessandro Ghedini57e81e62017-03-14 23:36:00 +00002896void SSL_CTX_set_select_certificate_cb(
2897 SSL_CTX *ctx,
2898 enum ssl_select_cert_result_t (*cb)(const SSL_CLIENT_HELLO *)) {
David Benjamind4c2bce2015-10-17 12:28:18 -04002899 ctx->select_certificate_cb = cb;
2900}
2901
David Benjamin731058e2016-12-03 23:15:13 -05002902void SSL_CTX_set_dos_protection_cb(SSL_CTX *ctx,
2903 int (*cb)(const SSL_CLIENT_HELLO *)) {
Adam Langley524e7172015-02-20 16:04:00 -08002904 ctx->dos_protection_cb = cb;
2905}
2906
Jesse Selover1c337e52018-08-10 13:28:47 -04002907void SSL_CTX_set_reverify_on_resume(SSL_CTX *ctx, int enabled) {
2908 ctx->reverify_on_resume = !!enabled;
2909}
2910
Jesse Seloverd7266ec2019-01-30 16:06:10 -05002911void 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 Benjamina614d462022-12-02 15:30:15 -05002918int SSL_was_key_usage_invalid(const SSL *ssl) {
2919 return ssl->s3->was_key_usage_invalid;
2920}
2921
David Benjamin1d5ef3b2015-10-12 19:54:18 -04002922void SSL_set_renegotiate_mode(SSL *ssl, enum ssl_renegotiate_mode_t mode) {
2923 ssl->renegotiate_mode = mode;
David Benjamin71666cb2018-05-01 17:14:27 -04002924
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 Benjamin1d5ef3b2015-10-12 19:54:18 -04002929}
2930
Adam Langleyc2d32802015-11-03 18:36:10 -08002931int 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 Langleyc2d32802015-11-03 18:36:10 -08002933 size_t write_iv_len;
David Benjamincfc11c22017-07-18 22:45:18 -04002934 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 Langleyc2d32802015-11-03 18:36:10 -08002936 *out_iv_len != write_iv_len) {
2937 return 0;
2938 }
2939
2940 return 1;
David Benjamine348ff42015-11-06 17:55:30 -05002941}
David Benjamin6e807652015-11-02 12:02:20 -05002942
David Benjamin91b25012016-01-21 16:39:58 -05002943uint64_t SSL_get_read_sequence(const SSL *ssl) {
David Benjamince079fd2016-08-02 16:22:34 -04002944 if (SSL_is_dtls(ssl)) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002945 // max_seq_num already includes the epoch.
David Benjaminde942382016-02-11 12:02:01 -05002946 assert(ssl->d1->r_epoch == (ssl->d1->bitmap.max_seq_num >> 48));
2947 return ssl->d1->bitmap.max_seq_num;
2948 }
David Benjamin32013e82022-09-22 16:55:34 -04002949 return ssl->s3->read_sequence;
David Benjamin91b25012016-01-21 16:39:58 -05002950}
2951
2952uint64_t SSL_get_write_sequence(const SSL *ssl) {
David Benjamin32013e82022-09-22 16:55:34 -04002953 uint64_t ret = ssl->s3->write_sequence;
David Benjamince079fd2016-08-02 16:22:34 -04002954 if (SSL_is_dtls(ssl)) {
David Benjaminde942382016-02-11 12:02:01 -05002955 assert((ret >> 48) == 0);
David Benjamin32013e82022-09-22 16:55:34 -04002956 ret |= uint64_t{ssl->d1->w_epoch} << 48;
David Benjaminde942382016-02-11 12:02:01 -05002957 }
2958 return ret;
David Benjamin91b25012016-01-21 16:39:58 -05002959}
2960
Steven Valdez02563852016-06-23 13:33:05 -04002961uint16_t SSL_get_peer_signature_algorithm(const SSL *ssl) {
David Benjaminf1050fd2016-12-13 20:05:36 -05002962 SSL_SESSION *session = SSL_get_session(ssl);
2963 if (session == NULL) {
2964 return 0;
2965 }
2966
2967 return session->peer_signature_algorithm;
Steven Valdez02563852016-06-23 13:33:05 -04002968}
2969
David Benjamin96ba15f2015-12-25 14:10:17 -05002970size_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 Benjamin17cf2cb2016-12-13 01:07:13 -05002977 OPENSSL_memcpy(out, ssl->s3->client_random, max_out);
David Benjamin96ba15f2015-12-25 14:10:17 -05002978 return max_out;
2979}
2980
2981size_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 Benjamin17cf2cb2016-12-13 01:07:13 -05002988 OPENSSL_memcpy(out, ssl->s3->server_random, max_out);
David Benjamin96ba15f2015-12-25 14:10:17 -05002989 return max_out;
2990}
2991
David Benjaminf6494f42016-01-07 12:37:41 -08002992const SSL_CIPHER *SSL_get_pending_cipher(const SSL *ssl) {
David Benjamin8e7bbba2017-10-13 17:18:35 -04002993 SSL_HANDSHAKE *hs = ssl->s3->hs.get();
David Benjamin45738dd2017-02-09 20:01:26 -05002994 if (hs == NULL) {
David Benjaminf6494f42016-01-07 12:37:41 -08002995 return NULL;
2996 }
David Benjamin45738dd2017-02-09 20:01:26 -05002997 return hs->new_cipher;
David Benjaminf6494f42016-01-07 12:37:41 -08002998}
2999
David Benjaminbbaf3672016-11-17 10:53:09 +09003000void SSL_set_retain_only_sha256_of_client_certs(SSL *ssl, int enabled) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07003001 if (!ssl->config) {
3002 return;
3003 }
3004 ssl->config->retain_only_sha256_of_client_certs = !!enabled;
David Benjaminbbaf3672016-11-17 10:53:09 +09003005}
3006
David Benjamincdd0b7e2016-01-07 10:10:41 -08003007void 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 Benjamin65ac9972016-09-02 21:35:25 -04003011void SSL_CTX_set_grease_enabled(SSL_CTX *ctx, int enabled) {
3012 ctx->grease_enabled = !!enabled;
3013}
3014
David Benjamine9c5d722021-06-09 17:43:16 -04003015void SSL_CTX_set_permute_extensions(SSL_CTX *ctx, int enabled) {
3016 ctx->permute_extensions = !!enabled;
3017}
3018
3019void SSL_set_permute_extensions(SSL *ssl, int enabled) {
3020 if (!ssl->config) {
3021 return;
3022 }
3023 ssl->config->permute_extensions = !!enabled;
3024}
3025
David Benjamin35ac5b72017-03-03 15:05:56 -05003026int32_t SSL_get_ticket_age_skew(const SSL *ssl) {
3027 return ssl->s3->ticket_age_skew;
3028}
3029
David Benjamin494e4d02017-12-01 13:05:12 -05003030void SSL_CTX_set_false_start_allowed_without_alpn(SSL_CTX *ctx, int allowed) {
3031 ctx->false_start_allowed_without_alpn = !!allowed;
3032}
3033
Kris Kwiatkowskib11902a2019-08-24 11:01:04 +01003034int SSL_used_hello_retry_request(const SSL *ssl) {
3035 return ssl->s3->used_hello_retry_request;
3036}
3037
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07003038void 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 Benjamin6965d252018-11-19 15:49:56 -06003045void SSL_set_jdk11_workaround(SSL *ssl, int enable) {
3046 if (!ssl->config) {
3047 return;
3048 }
3049 ssl->config->jdk11_workaround = !!enable;
3050}
3051
David Schinazi3d8b8c32021-01-14 11:25:49 -08003052void 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 Benjamin27bbae42015-09-13 00:54:37 -04003059int SSL_clear(SSL *ssl) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07003060 if (!ssl->config) {
3061 return 0; // SSL_clear may not be used after shedding config.
3062 }
3063
David Benjaminc11ea9422017-08-29 16:33:21 -04003064 // 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 Benjamin8e7bbba2017-10-13 17:18:35 -04003067 UniquePtr<SSL_SESSION> session;
David Benjaminb79cc842016-12-07 15:57:14 -05003068 if (!ssl->server && ssl->s3->established_session != NULL) {
David Benjamin2908dd12018-06-29 17:46:42 -04003069 session = UpRef(ssl->s3->established_session);
David Benjaminb79cc842016-12-07 15:57:14 -05003070 }
3071
David Benjaminc11ea9422017-08-29 16:33:21 -04003072 // The ssl->d1->mtu is simultaneously configuration (preserved across
3073 // clear) and connection-specific state (gets reset).
3074 //
3075 // TODO(davidben): Avoid this.
David Benjamin27bbae42015-09-13 00:54:37 -04003076 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 Benjamin27bbae42015-09-13 00:54:37 -04003085
David Benjamince079fd2016-08-02 16:22:34 -04003086 if (SSL_is_dtls(ssl) && (SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
David Benjamin27bbae42015-09-13 00:54:37 -04003087 ssl->d1->mtu = mtu;
3088 }
3089
David Benjamin8e7bbba2017-10-13 17:18:35 -04003090 if (session != nullptr) {
3091 SSL_set_session(ssl, session.get());
David Benjaminb79cc842016-12-07 15:57:14 -05003092 }
3093
David Benjamin27bbae42015-09-13 00:54:37 -04003094 return 1;
3095}
3096
David Benjaminda881e92015-04-26 14:45:04 -04003097int SSL_CTX_sess_connect(const SSL_CTX *ctx) { return 0; }
3098int SSL_CTX_sess_connect_good(const SSL_CTX *ctx) { return 0; }
3099int SSL_CTX_sess_connect_renegotiate(const SSL_CTX *ctx) { return 0; }
3100int SSL_CTX_sess_accept(const SSL_CTX *ctx) { return 0; }
3101int SSL_CTX_sess_accept_renegotiate(const SSL_CTX *ctx) { return 0; }
3102int SSL_CTX_sess_accept_good(const SSL_CTX *ctx) { return 0; }
3103int SSL_CTX_sess_hits(const SSL_CTX *ctx) { return 0; }
3104int SSL_CTX_sess_cb_hits(const SSL_CTX *ctx) { return 0; }
3105int SSL_CTX_sess_misses(const SSL_CTX *ctx) { return 0; }
3106int SSL_CTX_sess_timeouts(const SSL_CTX *ctx) { return 0; }
3107int SSL_CTX_sess_cache_full(const SSL_CTX *ctx) { return 0; }
David Benjamin936aada2016-06-07 19:09:22 -04003108
3109int SSL_num_renegotiations(const SSL *ssl) {
3110 return SSL_total_renegotiations(ssl);
3111}
3112
3113int SSL_CTX_need_tmp_RSA(const SSL_CTX *ctx) { return 0; }
3114int SSL_need_tmp_RSA(const SSL *ssl) { return 0; }
3115int SSL_CTX_set_tmp_rsa(SSL_CTX *ctx, const RSA *rsa) { return 1; }
3116int SSL_set_tmp_rsa(SSL *ssl, const RSA *rsa) { return 1; }
Matt Braithwaite4838d8a2015-08-20 13:20:03 -07003117void ERR_load_SSL_strings(void) {}
David Benjamin27bbae42015-09-13 00:54:37 -04003118void SSL_load_error_strings(void) {}
David Benjamin936aada2016-06-07 19:09:22 -04003119int SSL_cache_hit(SSL *ssl) { return SSL_session_reused(ssl); }
3120
3121int 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 Benjamin2da5ba92023-05-26 21:23:39 -04003127 return SSL_CTX_set1_groups(ctx, &nid, 1);
David Benjamin936aada2016-06-07 19:09:22 -04003128}
3129
3130int 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 Benjamin2da5ba92023-05-26 21:23:39 -04003136 return SSL_set1_groups(ssl, &nid, 1);
David Benjamin936aada2016-06-07 19:09:22 -04003137}
David Benjamin721e8b72016-08-03 13:13:17 -04003138
Adam Langley4c341d02017-03-08 19:33:21 -08003139void 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 Benjamin103ed082018-05-10 19:55:02 -04003143
Adam Langley53a17f52020-05-26 14:44:07 -07003144SSL_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 Benjamin3f180b82022-05-09 17:45:18 -04003172int 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 Benjamin5697a922022-07-21 10:35:59 -07003179size_t SSL_CTX_get_num_tickets(const SSL_CTX *ctx) { return ctx->num_tickets; }
3180
David Benjamin103ed082018-05-10 19:55:02 -04003181int 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 Apthorpc0c90012018-10-18 14:18:05 -07003189int 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 Benjamin103ed082018-05-10 19:55:02 -04003202int 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
3210size_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
3216int 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
3222int SSL_CTX_set_tlsext_status_arg(SSL_CTX *ctx, void *arg) {
3223 ctx->legacy_ocsp_callback_arg = arg;
3224 return 1;
3225}
Adam Langley451ea3c2022-05-18 16:15:49 -07003226
David Benjamin4e88a352023-06-26 15:24:49 -04003227uint16_t SSL_get_curve_id(const SSL *ssl) { return SSL_get_group_id(ssl); }
3228
3229const char *SSL_get_curve_name(uint16_t curve_id) {
3230 return SSL_get_group_name(curve_id);
3231}
3232
3233size_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
3237int 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
3241int SSL_set1_curves(SSL *ssl, const int *curves, size_t num_curves) {
3242 return SSL_set1_groups(ssl, curves, num_curves);
3243}
3244
3245int SSL_CTX_set1_curves_list(SSL_CTX *ctx, const char *curves) {
3246 return SSL_CTX_set1_groups_list(ctx, curves);
3247}
3248
3249int SSL_set1_curves_list(SSL *ssl, const char *curves) {
3250 return SSL_set1_groups_list(ssl, curves);
3251}
3252
Adam Langley451ea3c2022-05-18 16:15:49 -07003253namespace 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 Benjamin6cf98202023-05-26 22:03:30 -04003264static const uint16_t kGroups[] = {SSL_GROUP_SECP256R1, SSL_GROUP_SECP384R1};
Adam Langley451ea3c2022-05-18 16:15:49 -07003265
3266static 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
3279static 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
3285static int Configure(SSL_CTX *ctx) {
Adam Langley2f6409e2023-04-10 21:09:11 +00003286 ctx->tls13_cipher_policy = ssl_compliance_policy_fips_202205;
Adam Langley451ea3c2022-05-18 16:15:49 -07003287
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 Benjamin6cf98202023-05-26 22:03:30 -04003301 SSL_CTX_set1_group_ids(ctx, kGroups, OPENSSL_ARRAY_SIZE(kGroups)) &&
Adam Langley451ea3c2022-05-18 16:15:49 -07003302 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
3308static int Configure(SSL *ssl) {
Adam Langley2f6409e2023-04-10 21:09:11 +00003309 ssl->config->tls13_cipher_policy = ssl_compliance_policy_fips_202205;
Adam Langley451ea3c2022-05-18 16:15:49 -07003310
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 Benjamin6cf98202023-05-26 22:03:30 -04003315 SSL_set1_group_ids(ssl, kGroups, OPENSSL_ARRAY_SIZE(kGroups)) &&
Adam Langley451ea3c2022-05-18 16:15:49 -07003316 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 Langley2f6409e2023-04-10 21:09:11 +00003324namespace wpa202304 {
3325
3326// See WPA version 3.1, section 3.5.
3327
David Benjamin6cf98202023-05-26 22:03:30 -04003328static const uint16_t kGroups[] = {SSL_GROUP_SECP384R1};
Adam Langley2f6409e2023-04-10 21:09:11 +00003329
3330static 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
3338static const char kTLS12Ciphers[] =
3339 "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384:"
3340 "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384";
3341
3342static 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 Benjamin6cf98202023-05-26 22:03:30 -04003348 SSL_CTX_set1_group_ids(ctx, kGroups, OPENSSL_ARRAY_SIZE(kGroups)) &&
Adam Langley2f6409e2023-04-10 21:09:11 +00003349 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
3355static 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 Benjamin6cf98202023-05-26 22:03:30 -04003361 SSL_set1_group_ids(ssl, kGroups, OPENSSL_ARRAY_SIZE(kGroups)) &&
Adam Langley2f6409e2023-04-10 21:09:11 +00003362 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 Langley451ea3c2022-05-18 16:15:49 -07003370int 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 Langley2f6409e2023-04-10 21:09:11 +00003375 case ssl_compliance_policy_wpa3_192_202304:
3376 return wpa202304::Configure(ctx);
Adam Langley451ea3c2022-05-18 16:15:49 -07003377 default:
3378 return 0;
3379 }
3380}
3381
3382int 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 Langley2f6409e2023-04-10 21:09:11 +00003386 case ssl_compliance_policy_wpa3_192_202304:
3387 return wpa202304::Configure(ssl);
Adam Langley451ea3c2022-05-18 16:15:49 -07003388 default:
3389 return 0;
3390 }
3391}