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