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