Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/ssl/ssl_lib.cc

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