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