blob: 36c26cd5612155245b19a220bbe04dac1b31051a [file] [log] [blame]
Adam Langleyd9e397b2015-01-22 14:27:53 -08001/* 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.
Robert Sloanab8b8882018-03-26 11:39:51 -07007 *
Adam Langleyd9e397b2015-01-22 14:27:53 -08008 * 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).
Robert Sloanab8b8882018-03-26 11:39:51 -070014 *
Adam Langleyd9e397b2015-01-22 14:27:53 -080015 * 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.
Robert Sloanab8b8882018-03-26 11:39:51 -070021 *
Adam Langleyd9e397b2015-01-22 14:27:53 -080022 * 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 :-).
Robert Sloanab8b8882018-03-26 11:39:51 -070036 * 4. If you include any Windows specific code (or a derivative thereof) from
Adam Langleyd9e397b2015-01-22 14:27:53 -080037 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
Robert Sloanab8b8882018-03-26 11:39:51 -070039 *
Adam Langleyd9e397b2015-01-22 14:27:53 -080040 * 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.
Robert Sloanab8b8882018-03-26 11:39:51 -070051 *
Adam Langleyd9e397b2015-01-22 14:27:53 -080052 * 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
Robert Sloanab8b8882018-03-26 11:39:51 -070065 * notice, this list of conditions and the following disclaimer.
Adam Langleyd9e397b2015-01-22 14:27:53 -080066 *
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.
Robert Sloanab8b8882018-03-26 11:39:51 -0700112 * ECC cipher suite support in OpenSSL originally developed by
Adam Langleyd9e397b2015-01-22 14:27:53 -0800113 * 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
Kenny Rootb8494592015-09-25 02:29:14 +0000141#include <openssl/ssl.h>
142
Adam Langleyd9e397b2015-01-22 14:27:53 -0800143#include <assert.h>
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400144#include <stdlib.h>
Adam Langleye9ada862015-05-11 17:20:37 -0700145#include <string.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800146
147#include <openssl/bytestring.h>
Kenny Rootb8494592015-09-25 02:29:14 +0000148#include <openssl/crypto.h>
Adam Langleye9ada862015-05-11 17:20:37 -0700149#include <openssl/err.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800150#include <openssl/lhash.h>
151#include <openssl/mem.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800152#include <openssl/rand.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800153
Adam Langleye9ada862015-05-11 17:20:37 -0700154#include "internal.h"
155#include "../crypto/internal.h"
156
David Benjaminc895d6b2016-08-11 13:26:41 -0400157#if defined(OPENSSL_WINDOWS)
158#include <sys/timeb.h>
159#else
160#include <sys/socket.h>
161#include <sys/time.h>
162#endif
163
Adam Langleyd9e397b2015-01-22 14:27:53 -0800164
Robert Sloanb6d070c2017-07-24 08:40:01 -0700165namespace bssl {
166
Robert Sloana27a6a42017-09-05 08:39:28 -0700167// |SSL_R_UNKNOWN_PROTOCOL| is no longer emitted, but continue to define it
168// to avoid downstream churn.
Kenny Rootb8494592015-09-25 02:29:14 +0000169OPENSSL_DECLARE_ERROR_REASON(SSL, UNKNOWN_PROTOCOL)
170
Robert Sloana27a6a42017-09-05 08:39:28 -0700171// The following errors are no longer emitted, but are used in nginx without
172// #ifdefs.
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400173OPENSSL_DECLARE_ERROR_REASON(SSL, BLOCK_CIPHER_PAD_IS_WRONG)
174OPENSSL_DECLARE_ERROR_REASON(SSL, NO_CIPHERS_SPECIFIED)
175
Robert Sloana27a6a42017-09-05 08:39:28 -0700176// Some error codes are special. Ensure the make_errors.go script never
177// regresses this.
Robert Sloana12bf462017-07-17 07:08:26 -0700178static_assert(SSL_R_TLSV1_ALERT_NO_RENEGOTIATION ==
179 SSL_AD_NO_RENEGOTIATION + SSL_AD_REASON_OFFSET,
180 "alert reason code mismatch");
Adam Langleyd9e397b2015-01-22 14:27:53 -0800181
Robert Sloana27a6a42017-09-05 08:39:28 -0700182// kMaxHandshakeSize is the maximum size, in bytes, of a handshake message.
Adam Langleye9ada862015-05-11 17:20:37 -0700183static const size_t kMaxHandshakeSize = (1u << 24) - 1;
184
Kenny Rootb8494592015-09-25 02:29:14 +0000185static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl =
186 CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
187static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl_ctx =
188 CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
Adam Langleye9ada862015-05-11 17:20:37 -0700189
Robert Sloan4562e9d2017-10-02 10:26:51 -0700190bool 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
Robert Sloanb6d070c2017-07-24 08:40:01 -0700201void ssl_reset_error_state(SSL *ssl) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700202 // Functions which use |SSL_get_error| must reset I/O and error state on
203 // entry.
Robert Sloan29c1d2c2017-10-30 14:10:28 -0700204 ssl->s3->rwstate = SSL_NOTHING;
Robert Sloanb6d070c2017-07-24 08:40:01 -0700205 ERR_clear_error();
206 ERR_clear_system_error();
207}
208
Robert Sloan36272962017-10-23 10:28:39 -0700209void ssl_set_read_error(SSL* ssl) {
210 ssl->s3->read_shutdown = ssl_shutdown_error;
Robert Sloan29c1d2c2017-10-30 14:10:28 -0700211 ssl->s3->read_error.reset(ERR_save_state());
Robert Sloan36272962017-10-23 10:28:39 -0700212}
213
214static bool check_read_error(const SSL *ssl) {
215 if (ssl->s3->read_shutdown == ssl_shutdown_error) {
Robert Sloan29c1d2c2017-10-30 14:10:28 -0700216 ERR_restore_state(ssl->s3->read_error.get());
Robert Sloan36272962017-10-23 10:28:39 -0700217 return false;
218 }
219 return true;
220}
221
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100222bool ssl_can_write(const SSL *ssl) {
Robert Sloanb6d070c2017-07-24 08:40:01 -0700223 return !SSL_in_init(ssl) || ssl->s3->hs->can_early_write;
224}
225
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100226bool ssl_can_read(const SSL *ssl) {
Robert Sloanb6d070c2017-07-24 08:40:01 -0700227 return !SSL_in_init(ssl) || ssl->s3->hs->can_early_read;
228}
229
Robert Sloan36272962017-10-23 10:28:39 -0700230ssl_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
244ssl_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
260ssl_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
Robert Sloanb6d070c2017-07-24 08:40:01 -0700275void ssl_update_cache(SSL_HANDSHAKE *hs, int mode) {
276 SSL *const ssl = hs->ssl;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100277 SSL_CTX *ctx = ssl->session_ctx.get();
Robert Sloana27a6a42017-09-05 08:39:28 -0700278 // Never cache sessions with empty session IDs.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700279 if (ssl->s3->established_session->session_id_length == 0 ||
Robert Sloan8f860b12017-08-28 07:37:06 -0700280 ssl->s3->established_session->not_resumable ||
Robert Sloanb6d070c2017-07-24 08:40:01 -0700281 (ctx->session_cache_mode & mode) != mode) {
282 return;
283 }
284
Robert Sloana27a6a42017-09-05 08:39:28 -0700285 // Clients never use the internal session cache.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700286 int use_internal_cache = ssl->server && !(ctx->session_cache_mode &
287 SSL_SESS_CACHE_NO_INTERNAL_STORE);
288
Robert Sloana27a6a42017-09-05 08:39:28 -0700289 // 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.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100292 if (ssl->s3->established_session.get() != ssl->session.get() ||
Robert Sloanb6d070c2017-07-24 08:40:01 -0700293 (!ssl->server && hs->ticket_expected)) {
294 if (use_internal_cache) {
Robert Sloan29c1d2c2017-10-30 14:10:28 -0700295 SSL_CTX_add_session(ctx, ssl->s3->established_session.get());
Robert Sloanb6d070c2017-07-24 08:40:01 -0700296 }
297 if (ctx->new_session_cb != NULL) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100298 UniquePtr<SSL_SESSION> ref = UpRef(ssl->s3->established_session);
299 if (ctx->new_session_cb(ssl, ref.get())) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700300 // |new_session_cb|'s return value signals whether it took ownership.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100301 ref.release();
Robert Sloanb6d070c2017-07-24 08:40:01 -0700302 }
303 }
304 }
305
306 if (use_internal_cache &&
307 !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700308 // Automatically flush the internal session cache every 255 connections.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700309 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
326static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
327 static const char hextable[] = "0123456789abcdef";
328 uint8_t *out;
329
330 if (!CBB_add_space(cbb, &out, in_len * 2)) {
331 return 0;
332 }
333
334 for (size_t i = 0; i < in_len; i++) {
335 *(out++) = (uint8_t)hextable[in[i] >> 4];
336 *(out++) = (uint8_t)hextable[in[i] & 0xf];
337 }
338
339 return 1;
340}
341
342int ssl_log_secret(const SSL *ssl, const char *label, const uint8_t *secret,
343 size_t secret_len) {
344 if (ssl->ctx->keylog_callback == NULL) {
345 return 1;
346 }
347
348 ScopedCBB cbb;
349 uint8_t *out;
350 size_t out_len;
351 if (!CBB_init(cbb.get(), strlen(label) + 1 + SSL3_RANDOM_SIZE * 2 + 1 +
352 secret_len * 2 + 1) ||
353 !CBB_add_bytes(cbb.get(), (const uint8_t *)label, strlen(label)) ||
354 !CBB_add_bytes(cbb.get(), (const uint8_t *)" ", 1) ||
355 !cbb_add_hex(cbb.get(), ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
356 !CBB_add_bytes(cbb.get(), (const uint8_t *)" ", 1) ||
357 !cbb_add_hex(cbb.get(), secret, secret_len) ||
358 !CBB_add_u8(cbb.get(), 0 /* NUL */) ||
359 !CBB_finish(cbb.get(), &out, &out_len)) {
360 return 0;
361 }
362
363 ssl->ctx->keylog_callback(ssl, (const char *)out);
364 OPENSSL_free(out);
365 return 1;
366}
367
Robert Sloanb6d070c2017-07-24 08:40:01 -0700368void ssl_do_info_callback(const SSL *ssl, int type, int value) {
369 void (*cb)(const SSL *ssl, int type, int value) = NULL;
370 if (ssl->info_callback != NULL) {
371 cb = ssl->info_callback;
372 } else if (ssl->ctx->info_callback != NULL) {
373 cb = ssl->ctx->info_callback;
374 }
375
376 if (cb != NULL) {
377 cb(ssl, type, value);
378 }
379}
380
381void ssl_do_msg_callback(SSL *ssl, int is_write, int content_type,
Robert Sloan921ef2c2017-10-17 09:02:20 -0700382 Span<const uint8_t> in) {
Robert Sloanb6d070c2017-07-24 08:40:01 -0700383 if (ssl->msg_callback == NULL) {
384 return;
385 }
386
Robert Sloana27a6a42017-09-05 08:39:28 -0700387 // |version| is zero when calling for |SSL3_RT_HEADER| and |SSL2_VERSION| for
388 // a V2ClientHello.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700389 int version;
390 switch (content_type) {
391 case 0:
Robert Sloana27a6a42017-09-05 08:39:28 -0700392 // V2ClientHello
Robert Sloanb6d070c2017-07-24 08:40:01 -0700393 version = SSL2_VERSION;
394 break;
395 case SSL3_RT_HEADER:
396 version = 0;
397 break;
398 default:
399 version = SSL_version(ssl);
400 }
401
Robert Sloan921ef2c2017-10-17 09:02:20 -0700402 ssl->msg_callback(is_write, version, content_type, in.data(), in.size(), ssl,
Robert Sloanb6d070c2017-07-24 08:40:01 -0700403 ssl->msg_callback_arg);
404}
405
406void ssl_get_current_time(const SSL *ssl, struct OPENSSL_timeval *out_clock) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700407 // TODO(martinkr): Change callers to |ssl_ctx_get_current_time| and drop the
408 // |ssl| arg from |current_time_cb| if possible.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100409 ssl_ctx_get_current_time(ssl->ctx.get(), out_clock);
Robert Sloan8f860b12017-08-28 07:37:06 -0700410}
411
412void ssl_ctx_get_current_time(const SSL_CTX *ctx,
413 struct OPENSSL_timeval *out_clock) {
414 if (ctx->current_time_cb != NULL) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700415 // TODO(davidben): Update current_time_cb to use OPENSSL_timeval. See
416 // https://crbug.com/boringssl/155.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700417 struct timeval clock;
Robert Sloan8f860b12017-08-28 07:37:06 -0700418 ctx->current_time_cb(nullptr /* ssl */, &clock);
Robert Sloanb6d070c2017-07-24 08:40:01 -0700419 if (clock.tv_sec < 0) {
420 assert(0);
421 out_clock->tv_sec = 0;
422 out_clock->tv_usec = 0;
423 } else {
424 out_clock->tv_sec = (uint64_t)clock.tv_sec;
425 out_clock->tv_usec = (uint32_t)clock.tv_usec;
426 }
427 return;
428 }
429
430#if defined(BORINGSSL_UNSAFE_DETERMINISTIC_MODE)
431 out_clock->tv_sec = 1234;
432 out_clock->tv_usec = 1234;
433#elif defined(OPENSSL_WINDOWS)
434 struct _timeb time;
435 _ftime(&time);
436 if (time.time < 0) {
437 assert(0);
438 out_clock->tv_sec = 0;
439 out_clock->tv_usec = 0;
440 } else {
441 out_clock->tv_sec = time.time;
442 out_clock->tv_usec = time.millitm * 1000;
443 }
444#else
445 struct timeval clock;
446 gettimeofday(&clock, NULL);
447 if (clock.tv_sec < 0) {
448 assert(0);
449 out_clock->tv_sec = 0;
450 out_clock->tv_usec = 0;
451 } else {
452 out_clock->tv_sec = (uint64_t)clock.tv_sec;
453 out_clock->tv_usec = (uint32_t)clock.tv_usec;
454 }
455#endif
456}
457
Robert Sloan8542c082018-02-05 09:07:34 -0800458void SSL_CTX_set_handoff_mode(SSL_CTX *ctx, bool on) {
459 ctx->handoff = on;
460}
461
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100462static bool ssl_can_renegotiate(const SSL *ssl) {
463 if (ssl->server || SSL_is_dtls(ssl)) {
464 return false;
465 }
466
467 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
468 return false;
469 }
470
471 // The config has already been shed.
472 if (!ssl->config) {
473 return false;
474 }
475
476 switch (ssl->renegotiate_mode) {
477 case ssl_renegotiate_ignore:
478 case ssl_renegotiate_never:
479 return false;
480
481 case ssl_renegotiate_freely:
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
491static 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
502void SSL_set_handoff_mode(SSL *ssl, bool on) {
503 if (!ssl->config) {
504 return;
505 }
506 ssl->config->handoff = on;
507}
508
Robert Sloanb6d070c2017-07-24 08:40:01 -0700509} // namespace bssl
510
511using namespace bssl;
512
Kenny Rootb8494592015-09-25 02:29:14 +0000513int SSL_library_init(void) {
514 CRYPTO_library_init();
Adam Langleyd9e397b2015-01-22 14:27:53 -0800515 return 1;
516}
517
Robert Sloan4562e9d2017-10-02 10:26:51 -0700518int OPENSSL_init_ssl(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings) {
519 CRYPTO_library_init();
520 return 1;
521}
522
David Benjamin4969cc92016-04-22 15:02:23 -0400523static uint32_t ssl_session_hash(const SSL_SESSION *sess) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100524 return ssl_hash_session_id(
525 MakeConstSpan(sess->session_id, sess->session_id_length));
Kenny Rootb8494592015-09-25 02:29:14 +0000526}
527
Kenny Rootb8494592015-09-25 02:29:14 +0000528static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
Kenny Rootb8494592015-09-25 02:29:14 +0000529 if (a->session_id_length != b->session_id_length) {
530 return 1;
531 }
532
Robert Sloan69939df2017-01-09 10:53:07 -0800533 return OPENSSL_memcmp(a->session_id, b->session_id, a->session_id_length);
Kenny Rootb8494592015-09-25 02:29:14 +0000534}
535
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100536ssl_ctx_st::ssl_ctx_st(const SSL_METHOD *ssl_method)
537 : method(ssl_method->method),
538 x509_method(ssl_method->x509_method),
539 retain_only_sha256_of_client_certs(false),
540 quiet_shutdown(false),
541 ocsp_stapling_enabled(false),
542 signed_cert_timestamps_enabled(false),
543 channel_id_enabled(false),
544 grease_enabled(false),
545 allow_unknown_alpn_protos(false),
546 ed25519_enabled(false),
547 rsa_pss_rsae_certs_enabled(true),
548 false_start_allowed_without_alpn(false),
549 handoff(false),
550 enable_early_data(false) {
551 CRYPTO_MUTEX_init(&lock);
552 CRYPTO_new_ex_data(&ex_data);
553}
Kenny Rootb8494592015-09-25 02:29:14 +0000554
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100555ssl_ctx_st::~ssl_ctx_st() {
556 // Free the internal session cache. Note that this calls the caller-supplied
557 // remove callback, so we must do it before clearing ex_data. (See ticket
558 // [openssl.org #212].)
559 SSL_CTX_flush_sessions(this, 0);
560
561 CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, this, &ex_data);
562
563 CRYPTO_MUTEX_cleanup(&lock);
564 lh_SSL_SESSION_free(sessions);
565 x509_method->ssl_ctx_free(this);
566}
567
568SSL_CTX *SSL_CTX_new(const SSL_METHOD *method) {
Kenny Rootb8494592015-09-25 02:29:14 +0000569 if (method == NULL) {
570 OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_METHOD_PASSED);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100571 return nullptr;
Kenny Rootb8494592015-09-25 02:29:14 +0000572 }
573
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100574 UniquePtr<SSL_CTX> ret = MakeUnique<SSL_CTX>(method);
575 if (!ret) {
576 return nullptr;
Kenny Rootb8494592015-09-25 02:29:14 +0000577 }
578
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100579 ret->cert = MakeUnique<CERT>(method->x509_method);
Kenny Rootb8494592015-09-25 02:29:14 +0000580 ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100581 ret->client_CA.reset(sk_CRYPTO_BUFFER_new_null());
582 if (ret->cert == nullptr ||
583 ret->sessions == nullptr ||
584 ret->client_CA == nullptr ||
585 !ret->x509_method->ssl_ctx_new(ret.get())) {
586 return nullptr;
Kenny Rootb8494592015-09-25 02:29:14 +0000587 }
Robert Sloan7d422bc2017-03-06 10:04:29 -0800588
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100589 if (!SSL_CTX_set_strict_cipher_list(ret.get(), SSL_DEFAULT_CIPHER_LIST) ||
590 // Lock the SSL_CTX to the specified version, for compatibility with
591 // legacy uses of SSL_METHOD.
592 !SSL_CTX_set_max_proto_version(ret.get(), method->version) ||
593 !SSL_CTX_set_min_proto_version(ret.get(), method->version)) {
David Benjamin7c0d06c2016-08-11 13:26:41 -0400594 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100595 return nullptr;
Kenny Rootb8494592015-09-25 02:29:14 +0000596 }
597
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100598 return ret.release();
Kenny Rootb8494592015-09-25 02:29:14 +0000599}
600
David Benjaminc895d6b2016-08-11 13:26:41 -0400601int SSL_CTX_up_ref(SSL_CTX *ctx) {
602 CRYPTO_refcount_inc(&ctx->references);
603 return 1;
604}
605
Kenny Rootb8494592015-09-25 02:29:14 +0000606void SSL_CTX_free(SSL_CTX *ctx) {
607 if (ctx == NULL ||
608 !CRYPTO_refcount_dec_and_test_zero(&ctx->references)) {
609 return;
610 }
611
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100612 ctx->~ssl_ctx_st();
Kenny Rootb8494592015-09-25 02:29:14 +0000613 OPENSSL_free(ctx);
614}
615
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100616ssl_st::ssl_st(SSL_CTX *ctx_arg)
617 : method(ctx_arg->method),
618 max_send_fragment(ctx_arg->max_send_fragment),
619 msg_callback(ctx_arg->msg_callback),
620 msg_callback_arg(ctx_arg->msg_callback_arg),
621 tls13_variant(ctx_arg->tls13_variant),
622 ctx(UpRef(ctx_arg)),
623 session_ctx(UpRef(ctx_arg)),
624 options(ctx->options),
625 mode(ctx->mode),
626 max_cert_list(ctx->max_cert_list),
627 server(false),
628 quiet_shutdown(ctx->quiet_shutdown),
629 did_dummy_pq_padding(false),
630 enable_early_data(ctx->enable_early_data) {
631 CRYPTO_new_ex_data(&ex_data);
632}
633
634ssl_st::~ssl_st() {
635 CRYPTO_free_ex_data(&g_ex_data_class_ssl, this, &ex_data);
636 // |config| refers to |this|, so we must release it earlier.
637 config.reset();
638 if (method != NULL) {
639 method->ssl_free(this);
640 }
641}
642
Adam Langleyd9e397b2015-01-22 14:27:53 -0800643SSL *SSL_new(SSL_CTX *ctx) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100644 if (ctx == nullptr) {
Kenny Rootb8494592015-09-25 02:29:14 +0000645 OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_CTX);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100646 return nullptr;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800647 }
648
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100649 UniquePtr<SSL> ssl = MakeUnique<SSL>(ctx);
650 if (ssl == nullptr) {
651 return nullptr;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800652 }
653
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100654 ssl->config = MakeUnique<SSL_CONFIG>(ssl.get());
655 if (ssl->config == nullptr) {
656 return nullptr;
657 }
658 ssl->config->conf_min_version = ctx->conf_min_version;
659 ssl->config->conf_max_version = ctx->conf_max_version;
660
661 ssl->config->cert = ssl_cert_dup(ctx->cert.get());
662 if (ssl->config->cert == nullptr) {
663 return nullptr;
664 }
665
666 ssl->config->verify_mode = ctx->verify_mode;
667 ssl->config->verify_callback = ctx->default_verify_callback;
668 ssl->config->custom_verify_callback = ctx->custom_verify_callback;
669 ssl->config->retain_only_sha256_of_client_certs =
Steven Valdez909b19f2016-11-21 15:35:44 -0500670 ctx->retain_only_sha256_of_client_certs;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800671
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100672 if (!ssl->config->supported_group_list.CopyFrom(ctx->supported_group_list) ||
673 !ssl->config->alpn_client_proto_list.CopyFrom(
674 ctx->alpn_client_proto_list) ||
675 !ssl->config->verify_sigalgs.CopyFrom(ctx->verify_sigalgs)) {
676 return nullptr;
Robert Sloan7d422bc2017-03-06 10:04:29 -0800677 }
678
Adam Langleyd9e397b2015-01-22 14:27:53 -0800679 if (ctx->psk_identity_hint) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100680 ssl->config->psk_identity_hint.reset(
681 BUF_strdup(ctx->psk_identity_hint.get()));
682 if (ssl->config->psk_identity_hint == nullptr) {
683 return nullptr;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800684 }
685 }
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100686 ssl->config->psk_client_callback = ctx->psk_client_callback;
687 ssl->config->psk_server_callback = ctx->psk_server_callback;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800688
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100689 ssl->config->channel_id_enabled = ctx->channel_id_enabled;
690 ssl->config->channel_id_private = UpRef(ctx->channel_id_private);
691
692 ssl->config->signed_cert_timestamps_enabled =
693 ctx->signed_cert_timestamps_enabled;
694 ssl->config->ocsp_stapling_enabled = ctx->ocsp_stapling_enabled;
695 ssl->config->handoff = ctx->handoff;
696
697 if (!ssl->method->ssl_new(ssl.get()) ||
698 !ssl->ctx->x509_method->ssl_new(ssl->s3->hs.get())) {
699 return nullptr;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800700 }
701
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100702 return ssl.release();
703}
Adam Langleyd9e397b2015-01-22 14:27:53 -0800704
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100705SSL_CONFIG::SSL_CONFIG(SSL *ssl_arg)
706 : ssl(ssl_arg),
707 signed_cert_timestamps_enabled(false),
708 ocsp_stapling_enabled(false),
709 channel_id_enabled(false),
710 retain_only_sha256_of_client_certs(false),
711 handoff(false),
712 shed_handshake_config(false) {
713 assert(ssl);
714}
Adam Langleyd9e397b2015-01-22 14:27:53 -0800715
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100716SSL_CONFIG::~SSL_CONFIG() {
717 if (ssl->ctx != nullptr) {
718 ssl->ctx->x509_method->ssl_config_free(this);
719 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800720}
721
Kenny Rootb8494592015-09-25 02:29:14 +0000722void SSL_free(SSL *ssl) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100723 Delete(ssl);
Kenny Rootb8494592015-09-25 02:29:14 +0000724}
725
726void SSL_set_connect_state(SSL *ssl) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700727 ssl->server = false;
Robert Sloana27a6a42017-09-05 08:39:28 -0700728 ssl->do_handshake = ssl_client_handshake;
Kenny Rootb8494592015-09-25 02:29:14 +0000729}
730
731void SSL_set_accept_state(SSL *ssl) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700732 ssl->server = true;
Robert Sloana27a6a42017-09-05 08:39:28 -0700733 ssl->do_handshake = ssl_server_handshake;
Kenny Rootb8494592015-09-25 02:29:14 +0000734}
735
David Benjaminc895d6b2016-08-11 13:26:41 -0400736void SSL_set0_rbio(SSL *ssl, BIO *rbio) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100737 ssl->rbio.reset(rbio);
David Benjaminc895d6b2016-08-11 13:26:41 -0400738}
739
740void SSL_set0_wbio(SSL *ssl, BIO *wbio) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100741 ssl->wbio.reset(wbio);
Kenny Rootb8494592015-09-25 02:29:14 +0000742}
743
David Benjaminc895d6b2016-08-11 13:26:41 -0400744void SSL_set_bio(SSL *ssl, BIO *rbio, BIO *wbio) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700745 // For historical reasons, this function has many different cases in ownership
746 // handling.
David Benjaminc895d6b2016-08-11 13:26:41 -0400747
Robert Sloana27a6a42017-09-05 08:39:28 -0700748 // If nothing has changed, do nothing
David Benjaminc895d6b2016-08-11 13:26:41 -0400749 if (rbio == SSL_get_rbio(ssl) && wbio == SSL_get_wbio(ssl)) {
750 return;
751 }
752
Robert Sloana27a6a42017-09-05 08:39:28 -0700753 // If the two arguments are equal, one fewer reference is granted than
754 // taken.
David Benjaminc895d6b2016-08-11 13:26:41 -0400755 if (rbio != NULL && rbio == wbio) {
756 BIO_up_ref(rbio);
757 }
758
Robert Sloana27a6a42017-09-05 08:39:28 -0700759 // If only the wbio is changed, adopt only one reference.
David Benjaminc895d6b2016-08-11 13:26:41 -0400760 if (rbio == SSL_get_rbio(ssl)) {
761 SSL_set0_wbio(ssl, wbio);
762 return;
763 }
764
Robert Sloana27a6a42017-09-05 08:39:28 -0700765 // There is an asymmetry here for historical reasons. If only the rbio is
766 // changed AND the rbio and wbio were originally different, then we only adopt
767 // one reference.
David Benjaminc895d6b2016-08-11 13:26:41 -0400768 if (wbio == SSL_get_wbio(ssl) && SSL_get_rbio(ssl) != SSL_get_wbio(ssl)) {
769 SSL_set0_rbio(ssl, rbio);
770 return;
771 }
772
Robert Sloana27a6a42017-09-05 08:39:28 -0700773 // Otherwise, adopt both references.
David Benjaminc895d6b2016-08-11 13:26:41 -0400774 SSL_set0_rbio(ssl, rbio);
775 SSL_set0_wbio(ssl, wbio);
776}
777
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100778BIO *SSL_get_rbio(const SSL *ssl) { return ssl->rbio.get(); }
Kenny Rootb8494592015-09-25 02:29:14 +0000779
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100780BIO *SSL_get_wbio(const SSL *ssl) { return ssl->wbio.get(); }
Kenny Rootb8494592015-09-25 02:29:14 +0000781
Robert Sloan69939df2017-01-09 10:53:07 -0800782int SSL_do_handshake(SSL *ssl) {
783 ssl_reset_error_state(ssl);
David Benjamin4969cc92016-04-22 15:02:23 -0400784
Robert Sloana27a6a42017-09-05 08:39:28 -0700785 if (ssl->do_handshake == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +0000786 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_TYPE_NOT_SET);
787 return -1;
788 }
789
790 if (!SSL_in_init(ssl)) {
791 return 1;
792 }
793
Robert Sloana27a6a42017-09-05 08:39:28 -0700794 // Run the handshake.
Robert Sloan29c1d2c2017-10-30 14:10:28 -0700795 SSL_HANDSHAKE *hs = ssl->s3->hs.get();
David Benjamin1b249672016-12-06 18:25:50 -0500796
Robert Sloana27a6a42017-09-05 08:39:28 -0700797 bool early_return = false;
798 int ret = ssl_run_handshake(hs, &early_return);
799 ssl_do_info_callback(
800 ssl, ssl->server ? SSL_CB_ACCEPT_EXIT : SSL_CB_CONNECT_EXIT, ret);
David Benjamin1b249672016-12-06 18:25:50 -0500801 if (ret <= 0) {
802 return ret;
803 }
804
Robert Sloana27a6a42017-09-05 08:39:28 -0700805 // Destroy the handshake object if the handshake has completely finished.
806 if (!early_return) {
Robert Sloan29c1d2c2017-10-30 14:10:28 -0700807 ssl->s3->hs.reset();
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100808 ssl_maybe_shed_handshake_config(ssl);
David Benjamin1b249672016-12-06 18:25:50 -0500809 }
810
811 return 1;
Kenny Rootb8494592015-09-25 02:29:14 +0000812}
813
814int SSL_connect(SSL *ssl) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700815 if (ssl->do_handshake == NULL) {
816 // Not properly initialized yet
Kenny Rootb8494592015-09-25 02:29:14 +0000817 SSL_set_connect_state(ssl);
818 }
819
David Benjamin4969cc92016-04-22 15:02:23 -0400820 return SSL_do_handshake(ssl);
Kenny Rootb8494592015-09-25 02:29:14 +0000821}
822
823int SSL_accept(SSL *ssl) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700824 if (ssl->do_handshake == NULL) {
825 // Not properly initialized yet
Kenny Rootb8494592015-09-25 02:29:14 +0000826 SSL_set_accept_state(ssl);
827 }
828
David Benjamin4969cc92016-04-22 15:02:23 -0400829 return SSL_do_handshake(ssl);
830}
831
Robert Sloan84377092017-08-14 09:33:19 -0700832static int ssl_do_post_handshake(SSL *ssl, const SSLMessage &msg) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700833 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
Robert Sloan84377092017-08-14 09:33:19 -0700834 return tls13_post_handshake(ssl, msg);
835 }
836
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100837 // Check for renegotiation on the server before parsing to use the correct
838 // error. Renegotiation is triggered by a different message for servers.
839 if (ssl->server) {
840 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION);
841 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_NO_RENEGOTIATION);
842 return 0;
David Benjaminc895d6b2016-08-11 13:26:41 -0400843 }
844
Robert Sloan84377092017-08-14 09:33:19 -0700845 if (msg.type != SSL3_MT_HELLO_REQUEST || CBS_len(&msg.body) != 0) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700846 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjaminc895d6b2016-08-11 13:26:41 -0400847 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HELLO_REQUEST);
848 return 0;
849 }
850
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100851 if (ssl->renegotiate_mode == ssl_renegotiate_ignore) {
852 return 1; // Ignore the HelloRequest.
David Benjaminc895d6b2016-08-11 13:26:41 -0400853 }
854
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100855 if (!ssl_can_renegotiate(ssl) ||
856 // Renegotiation is only supported at quiescent points in the application
857 // protocol, namely in HTTPS, just before reading the HTTP response.
858 // Require the record-layer be idle and avoid complexities of sending a
859 // handshake record while an application_data record is being written.
860 !ssl->s3->write_buffer.empty() ||
Robert Sloan36272962017-10-23 10:28:39 -0700861 ssl->s3->write_shutdown != ssl_shutdown_none) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100862 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION);
863 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_NO_RENEGOTIATION);
864 return 0;
David Benjaminc895d6b2016-08-11 13:26:41 -0400865 }
866
Robert Sloana27a6a42017-09-05 08:39:28 -0700867 // Begin a new handshake.
Robert Sloan29c1d2c2017-10-30 14:10:28 -0700868 if (ssl->s3->hs != nullptr) {
Steven Valdeze7531f02016-12-14 13:29:57 -0500869 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
870 return 0;
871 }
872 ssl->s3->hs = ssl_handshake_new(ssl);
Robert Sloan29c1d2c2017-10-30 14:10:28 -0700873 if (ssl->s3->hs == nullptr) {
Steven Valdeze7531f02016-12-14 13:29:57 -0500874 return 0;
875 }
876
David Benjaminc895d6b2016-08-11 13:26:41 -0400877 ssl->s3->total_renegotiations++;
David Benjaminc895d6b2016-08-11 13:26:41 -0400878 return 1;
David Benjaminc895d6b2016-08-11 13:26:41 -0400879}
880
Robert Sloan36272962017-10-23 10:28:39 -0700881static int ssl_read_impl(SSL *ssl) {
Robert Sloan69939df2017-01-09 10:53:07 -0800882 ssl_reset_error_state(ssl);
David Benjamin4969cc92016-04-22 15:02:23 -0400883
Robert Sloana27a6a42017-09-05 08:39:28 -0700884 if (ssl->do_handshake == NULL) {
David Benjamin4969cc92016-04-22 15:02:23 -0400885 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
Kenny Rootb8494592015-09-25 02:29:14 +0000886 return -1;
887 }
888
Robert Sloan36272962017-10-23 10:28:39 -0700889 // Replay post-handshake message errors.
890 if (!check_read_error(ssl)) {
891 return -1;
892 }
893
894 while (ssl->s3->pending_app_data.empty()) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700895 // Complete the current handshake, if any. False Start will cause
896 // |SSL_do_handshake| to return mid-handshake, so this may require multiple
897 // iterations.
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700898 while (!ssl_can_read(ssl)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400899 int ret = SSL_do_handshake(ssl);
900 if (ret < 0) {
901 return ret;
902 }
903 if (ret == 0) {
904 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
905 return -1;
906 }
907 }
908
Robert Sloan921ef2c2017-10-17 09:02:20 -0700909 // Process any buffered post-handshake messages.
Robert Sloan84377092017-08-14 09:33:19 -0700910 SSLMessage msg;
Robert Sloan921ef2c2017-10-17 09:02:20 -0700911 if (ssl->method->get_message(ssl, &msg)) {
Robert Sloanb1b54b82017-11-06 13:50:02 -0800912 // If we received an interrupt in early read (EndOfEarlyData), loop again
913 // for the handshake to process it.
914 if (SSL_in_init(ssl)) {
915 ssl->s3->hs->can_early_read = false;
916 continue;
917 }
918
Robert Sloana27a6a42017-09-05 08:39:28 -0700919 // Handle the post-handshake message and try again.
Robert Sloan84377092017-08-14 09:33:19 -0700920 if (!ssl_do_post_handshake(ssl, msg)) {
Robert Sloan36272962017-10-23 10:28:39 -0700921 ssl_set_read_error(ssl);
Robert Sloan84377092017-08-14 09:33:19 -0700922 return -1;
923 }
924 ssl->method->next_message(ssl);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700925 continue; // Loop again. We may have begun a new handshake.
David Benjamin4969cc92016-04-22 15:02:23 -0400926 }
Robert Sloan921ef2c2017-10-17 09:02:20 -0700927
Robert Sloan36272962017-10-23 10:28:39 -0700928 uint8_t alert = SSL_AD_DECODE_ERROR;
929 size_t consumed = 0;
930 auto ret = ssl_open_app_data(ssl, &ssl->s3->pending_app_data, &consumed,
Robert Sloan29c1d2c2017-10-30 14:10:28 -0700931 &alert, ssl->s3->read_buffer.span());
Robert Sloan36272962017-10-23 10:28:39 -0700932 bool retry;
933 int bio_ret = ssl_handle_open_record(ssl, &retry, ret, consumed, alert);
934 if (bio_ret <= 0) {
935 return bio_ret;
Robert Sloan921ef2c2017-10-17 09:02:20 -0700936 }
Robert Sloan36272962017-10-23 10:28:39 -0700937 if (!retry) {
938 assert(!ssl->s3->pending_app_data.empty());
Robert Sloan921ef2c2017-10-17 09:02:20 -0700939 ssl->s3->key_update_count = 0;
940 }
David Benjamin4969cc92016-04-22 15:02:23 -0400941 }
Robert Sloan36272962017-10-23 10:28:39 -0700942
943 return 1;
Kenny Rootb8494592015-09-25 02:29:14 +0000944}
945
946int SSL_read(SSL *ssl, void *buf, int num) {
Robert Sloan36272962017-10-23 10:28:39 -0700947 int ret = SSL_peek(ssl, buf, num);
948 if (ret <= 0) {
949 return ret;
950 }
951 // TODO(davidben): In DTLS, should the rest of the record be discarded? DTLS
952 // is not a stream. See https://crbug.com/boringssl/65.
953 ssl->s3->pending_app_data =
954 ssl->s3->pending_app_data.subspan(static_cast<size_t>(ret));
955 if (ssl->s3->pending_app_data.empty()) {
Robert Sloan29c1d2c2017-10-30 14:10:28 -0700956 ssl->s3->read_buffer.DiscardConsumed();
Robert Sloan36272962017-10-23 10:28:39 -0700957 }
958 return ret;
Kenny Rootb8494592015-09-25 02:29:14 +0000959}
960
961int SSL_peek(SSL *ssl, void *buf, int num) {
Robert Sloan36272962017-10-23 10:28:39 -0700962 int ret = ssl_read_impl(ssl);
963 if (ret <= 0) {
964 return ret;
965 }
966 if (num <= 0) {
967 return num;
968 }
969 size_t todo =
970 std::min(ssl->s3->pending_app_data.size(), static_cast<size_t>(num));
971 OPENSSL_memcpy(buf, ssl->s3->pending_app_data.data(), todo);
972 return static_cast<int>(todo);
Kenny Rootb8494592015-09-25 02:29:14 +0000973}
974
975int SSL_write(SSL *ssl, const void *buf, int num) {
Robert Sloan69939df2017-01-09 10:53:07 -0800976 ssl_reset_error_state(ssl);
David Benjamin4969cc92016-04-22 15:02:23 -0400977
Robert Sloana27a6a42017-09-05 08:39:28 -0700978 if (ssl->do_handshake == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +0000979 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
980 return -1;
981 }
982
Robert Sloan921ef2c2017-10-17 09:02:20 -0700983 if (ssl->s3->write_shutdown != ssl_shutdown_none) {
Kenny Rootb8494592015-09-25 02:29:14 +0000984 OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
985 return -1;
986 }
987
Robert Sloana27a6a42017-09-05 08:39:28 -0700988 int ret = 0;
989 bool needs_handshake = false;
Robert Sloane56da3e2017-06-26 08:26:42 -0700990 do {
Robert Sloana27a6a42017-09-05 08:39:28 -0700991 // If necessary, complete the handshake implicitly.
Robert Sloane56da3e2017-06-26 08:26:42 -0700992 if (!ssl_can_write(ssl)) {
993 ret = SSL_do_handshake(ssl);
994 if (ret < 0) {
995 return ret;
996 }
997 if (ret == 0) {
998 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
999 return -1;
1000 }
David Benjamin4969cc92016-04-22 15:02:23 -04001001 }
David Benjamin4969cc92016-04-22 15:02:23 -04001002
Robert Sloana12bf462017-07-17 07:08:26 -07001003 ret = ssl->method->write_app_data(ssl, &needs_handshake,
1004 (const uint8_t *)buf, num);
Robert Sloane56da3e2017-06-26 08:26:42 -07001005 } while (needs_handshake);
1006 return ret;
Kenny Rootb8494592015-09-25 02:29:14 +00001007}
1008
1009int SSL_shutdown(SSL *ssl) {
Robert Sloan69939df2017-01-09 10:53:07 -08001010 ssl_reset_error_state(ssl);
David Benjamin4969cc92016-04-22 15:02:23 -04001011
Robert Sloana27a6a42017-09-05 08:39:28 -07001012 if (ssl->do_handshake == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001013 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
1014 return -1;
1015 }
1016
Robert Sloana27a6a42017-09-05 08:39:28 -07001017 // If we are in the middle of a handshake, silently succeed. Consumers often
1018 // call this function before |SSL_free|, whether the handshake succeeded or
1019 // not. We assume the caller has already handled failed handshakes.
Kenny Rootb8494592015-09-25 02:29:14 +00001020 if (SSL_in_init(ssl)) {
Robert Sloana94fe052017-02-21 08:49:28 -08001021 return 1;
Kenny Rootb8494592015-09-25 02:29:14 +00001022 }
1023
Kenny Rootb8494592015-09-25 02:29:14 +00001024 if (ssl->quiet_shutdown) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001025 // Do nothing if configured not to send a close_notify.
Robert Sloan921ef2c2017-10-17 09:02:20 -07001026 ssl->s3->write_shutdown = ssl_shutdown_close_notify;
1027 ssl->s3->read_shutdown = ssl_shutdown_close_notify;
Kenny Rootb8494592015-09-25 02:29:14 +00001028 return 1;
1029 }
1030
Robert Sloana27a6a42017-09-05 08:39:28 -07001031 // This function completes in two stages. It sends a close_notify and then it
1032 // waits for a close_notify to come in. Perform exactly one action and return
1033 // whether or not it succeeds.
Kenny Rootb8494592015-09-25 02:29:14 +00001034
Robert Sloan921ef2c2017-10-17 09:02:20 -07001035 if (ssl->s3->write_shutdown != ssl_shutdown_close_notify) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001036 // Send a close_notify.
Robert Sloan921ef2c2017-10-17 09:02:20 -07001037 if (ssl_send_alert(ssl, SSL3_AL_WARNING, SSL_AD_CLOSE_NOTIFY) <= 0) {
David Benjamind316cba2016-06-02 16:17:39 -04001038 return -1;
Kenny Rootb8494592015-09-25 02:29:14 +00001039 }
1040 } else if (ssl->s3->alert_dispatch) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001041 // Finish sending the close_notify.
David Benjaminc895d6b2016-08-11 13:26:41 -04001042 if (ssl->method->dispatch_alert(ssl) <= 0) {
David Benjamind316cba2016-06-02 16:17:39 -04001043 return -1;
Kenny Rootb8494592015-09-25 02:29:14 +00001044 }
Robert Sloan921ef2c2017-10-17 09:02:20 -07001045 } else if (ssl->s3->read_shutdown != ssl_shutdown_close_notify) {
Robert Sloan36272962017-10-23 10:28:39 -07001046 if (SSL_is_dtls(ssl)) {
1047 // Bidirectional shutdown doesn't make sense for an unordered
1048 // transport. DTLS alerts also aren't delivered reliably, so we may even
1049 // time out because the peer never received our close_notify. Report to
1050 // the caller that the channel has fully shut down.
1051 if (ssl->s3->read_shutdown == ssl_shutdown_error) {
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001052 ERR_restore_state(ssl->s3->read_error.get());
Robert Sloan36272962017-10-23 10:28:39 -07001053 return -1;
1054 }
1055 ssl->s3->read_shutdown = ssl_shutdown_close_notify;
1056 } else {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001057 // Process records until an error, close_notify, or application data.
1058 if (ssl_read_impl(ssl) > 0) {
1059 // We received some unexpected application data.
1060 OPENSSL_PUT_ERROR(SSL, SSL_R_APPLICATION_DATA_ON_SHUTDOWN);
1061 return -1;
Robert Sloan36272962017-10-23 10:28:39 -07001062 }
1063 if (ssl->s3->read_shutdown != ssl_shutdown_close_notify) {
1064 return -1;
1065 }
Kenny Rootb8494592015-09-25 02:29:14 +00001066 }
1067 }
1068
Robert Sloana27a6a42017-09-05 08:39:28 -07001069 // Return 0 for unidirectional shutdown and 1 for bidirectional shutdown.
Robert Sloan921ef2c2017-10-17 09:02:20 -07001070 return ssl->s3->read_shutdown == ssl_shutdown_close_notify;
Kenny Rootb8494592015-09-25 02:29:14 +00001071}
1072
David Benjaminc895d6b2016-08-11 13:26:41 -04001073int SSL_send_fatal_alert(SSL *ssl, uint8_t alert) {
1074 if (ssl->s3->alert_dispatch) {
1075 if (ssl->s3->send_alert[0] != SSL3_AL_FATAL ||
1076 ssl->s3->send_alert[1] != alert) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001077 // We are already attempting to write a different alert.
David Benjaminc895d6b2016-08-11 13:26:41 -04001078 OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
1079 return -1;
1080 }
1081 return ssl->method->dispatch_alert(ssl);
1082 }
1083
Robert Sloan921ef2c2017-10-17 09:02:20 -07001084 return ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
David Benjaminc895d6b2016-08-11 13:26:41 -04001085}
1086
Robert Sloan8542c082018-02-05 09:07:34 -08001087int SSL_set_quic_transport_params(SSL *ssl, const uint8_t *params,
1088 size_t params_len) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001089 return ssl->config && ssl->config->quic_transport_params.CopyFrom(
1090 MakeConstSpan(params, params_len));
Robert Sloan8542c082018-02-05 09:07:34 -08001091}
1092
1093void SSL_get_peer_quic_transport_params(const SSL *ssl,
1094 const uint8_t **out_params,
1095 size_t *out_params_len) {
1096 *out_params = ssl->s3->peer_quic_transport_params.data();
1097 *out_params_len = ssl->s3->peer_quic_transport_params.size();
1098}
1099
Robert Sloan69939df2017-01-09 10:53:07 -08001100void SSL_CTX_set_early_data_enabled(SSL_CTX *ctx, int enabled) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001101 ctx->enable_early_data = !!enabled;
Robert Sloan572a4e22017-04-17 10:52:19 -07001102}
1103
Robert Sloana12bf462017-07-17 07:08:26 -07001104void SSL_CTX_set_tls13_variant(SSL_CTX *ctx, enum tls13_variant_t variant) {
1105 ctx->tls13_variant = variant;
1106}
1107
1108void SSL_set_tls13_variant(SSL *ssl, enum tls13_variant_t variant) {
1109 ssl->tls13_variant = variant;
1110}
1111
Robert Sloan572a4e22017-04-17 10:52:19 -07001112void SSL_set_early_data_enabled(SSL *ssl, int enabled) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001113 ssl->enable_early_data = !!enabled;
Robert Sloan69939df2017-01-09 10:53:07 -08001114}
1115
Robert Sloane56da3e2017-06-26 08:26:42 -07001116int SSL_in_early_data(const SSL *ssl) {
1117 if (ssl->s3->hs == NULL) {
1118 return 0;
1119 }
1120 return ssl->s3->hs->in_early_data;
1121}
1122
Robert Sloan6d0d00e2017-03-27 07:13:07 -07001123int SSL_early_data_accepted(const SSL *ssl) {
Robert Sloan0da43952018-01-03 15:13:14 -08001124 return ssl->s3->early_data_accepted;
Robert Sloan6d0d00e2017-03-27 07:13:07 -07001125}
1126
Robert Sloane56da3e2017-06-26 08:26:42 -07001127void SSL_reset_early_data_reject(SSL *ssl) {
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001128 SSL_HANDSHAKE *hs = ssl->s3->hs.get();
Robert Sloane56da3e2017-06-26 08:26:42 -07001129 if (hs == NULL ||
1130 hs->wait != ssl_hs_early_data_rejected) {
1131 abort();
1132 }
1133
1134 hs->wait = ssl_hs_ok;
Robert Sloana27a6a42017-09-05 08:39:28 -07001135 hs->in_early_data = false;
Robert Sloanb6d070c2017-07-24 08:40:01 -07001136 hs->early_session.reset();
Robert Sloane56da3e2017-06-26 08:26:42 -07001137
Robert Sloana27a6a42017-09-05 08:39:28 -07001138 // Discard any unfinished writes from the perspective of |SSL_write|'s
1139 // retry. The handshake will transparently flush out the pending record
1140 // (discarded by the server) to keep the framing correct.
1141 ssl->s3->wpend_pending = false;
Robert Sloane56da3e2017-06-26 08:26:42 -07001142}
1143
Steven Valdeze7531f02016-12-14 13:29:57 -05001144static int bio_retry_reason_to_error(int reason) {
1145 switch (reason) {
1146 case BIO_RR_CONNECT:
1147 return SSL_ERROR_WANT_CONNECT;
1148 case BIO_RR_ACCEPT:
1149 return SSL_ERROR_WANT_ACCEPT;
1150 default:
1151 return SSL_ERROR_SYSCALL;
1152 }
1153}
Kenny Rootb8494592015-09-25 02:29:14 +00001154
Steven Valdeze7531f02016-12-14 13:29:57 -05001155int SSL_get_error(const SSL *ssl, int ret_code) {
Kenny Rootb8494592015-09-25 02:29:14 +00001156 if (ret_code > 0) {
1157 return SSL_ERROR_NONE;
1158 }
1159
Robert Sloana27a6a42017-09-05 08:39:28 -07001160 // Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
1161 // where we do encode the error
Steven Valdeze7531f02016-12-14 13:29:57 -05001162 uint32_t err = ERR_peek_error();
Kenny Rootb8494592015-09-25 02:29:14 +00001163 if (err != 0) {
1164 if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
1165 return SSL_ERROR_SYSCALL;
1166 }
1167 return SSL_ERROR_SSL;
1168 }
1169
1170 if (ret_code == 0) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001171 if (ssl->s3->read_shutdown == ssl_shutdown_close_notify) {
Kenny Rootb8494592015-09-25 02:29:14 +00001172 return SSL_ERROR_ZERO_RETURN;
1173 }
Robert Sloana27a6a42017-09-05 08:39:28 -07001174 // An EOF was observed which violates the protocol, and the underlying
1175 // transport does not participate in the error queue. Bubble up to the
1176 // caller.
Kenny Rootb8494592015-09-25 02:29:14 +00001177 return SSL_ERROR_SYSCALL;
1178 }
1179
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001180 switch (ssl->s3->rwstate) {
Steven Valdeze7531f02016-12-14 13:29:57 -05001181 case SSL_PENDING_SESSION:
1182 return SSL_ERROR_PENDING_SESSION;
Kenny Rootb8494592015-09-25 02:29:14 +00001183
Steven Valdeze7531f02016-12-14 13:29:57 -05001184 case SSL_CERTIFICATE_SELECTION_PENDING:
1185 return SSL_ERROR_PENDING_CERTIFICATE;
Kenny Rootb8494592015-09-25 02:29:14 +00001186
Robert Sloan8542c082018-02-05 09:07:34 -08001187 case SSL_HANDOFF:
1188 return SSL_ERROR_HANDOFF;
1189
Robert Sloandc2f6092018-04-10 10:22:33 -07001190 case SSL_HANDBACK:
1191 return SSL_ERROR_HANDBACK;
1192
Steven Valdeze7531f02016-12-14 13:29:57 -05001193 case SSL_READING: {
1194 BIO *bio = SSL_get_rbio(ssl);
1195 if (BIO_should_read(bio)) {
1196 return SSL_ERROR_WANT_READ;
Kenny Rootb8494592015-09-25 02:29:14 +00001197 }
1198
Steven Valdeze7531f02016-12-14 13:29:57 -05001199 if (BIO_should_write(bio)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001200 // TODO(davidben): OpenSSL historically checked for writes on the read
1201 // BIO. Can this be removed?
Steven Valdeze7531f02016-12-14 13:29:57 -05001202 return SSL_ERROR_WANT_WRITE;
Kenny Rootb8494592015-09-25 02:29:14 +00001203 }
1204
Steven Valdeze7531f02016-12-14 13:29:57 -05001205 if (BIO_should_io_special(bio)) {
1206 return bio_retry_reason_to_error(BIO_get_retry_reason(bio));
Kenny Rootb8494592015-09-25 02:29:14 +00001207 }
1208
Steven Valdeze7531f02016-12-14 13:29:57 -05001209 break;
1210 }
1211
1212 case SSL_WRITING: {
1213 BIO *bio = SSL_get_wbio(ssl);
1214 if (BIO_should_write(bio)) {
1215 return SSL_ERROR_WANT_WRITE;
Kenny Rootb8494592015-09-25 02:29:14 +00001216 }
1217
Steven Valdeze7531f02016-12-14 13:29:57 -05001218 if (BIO_should_read(bio)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001219 // TODO(davidben): OpenSSL historically checked for reads on the write
1220 // BIO. Can this be removed?
Steven Valdeze7531f02016-12-14 13:29:57 -05001221 return SSL_ERROR_WANT_READ;
1222 }
1223
1224 if (BIO_should_io_special(bio)) {
1225 return bio_retry_reason_to_error(BIO_get_retry_reason(bio));
1226 }
1227
1228 break;
Kenny Rootb8494592015-09-25 02:29:14 +00001229 }
Kenny Rootb8494592015-09-25 02:29:14 +00001230
Steven Valdeze7531f02016-12-14 13:29:57 -05001231 case SSL_X509_LOOKUP:
1232 return SSL_ERROR_WANT_X509_LOOKUP;
Kenny Rootb8494592015-09-25 02:29:14 +00001233
Steven Valdeze7531f02016-12-14 13:29:57 -05001234 case SSL_CHANNEL_ID_LOOKUP:
1235 return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
Kenny Rootb8494592015-09-25 02:29:14 +00001236
Steven Valdeze7531f02016-12-14 13:29:57 -05001237 case SSL_PRIVATE_KEY_OPERATION:
1238 return SSL_ERROR_WANT_PRIVATE_KEY_OPERATION;
Robert Sloan1c9db532017-03-13 08:03:59 -07001239
1240 case SSL_PENDING_TICKET:
1241 return SSL_ERROR_PENDING_TICKET;
Robert Sloane56da3e2017-06-26 08:26:42 -07001242
1243 case SSL_EARLY_DATA_REJECTED:
1244 return SSL_ERROR_EARLY_DATA_REJECTED;
Robert Sloanb6d070c2017-07-24 08:40:01 -07001245
1246 case SSL_CERTIFICATE_VERIFY:
1247 return SSL_ERROR_WANT_CERTIFICATE_VERIFY;
Kenny Rootb8494592015-09-25 02:29:14 +00001248 }
1249
1250 return SSL_ERROR_SYSCALL;
1251}
1252
Kenny Rootb8494592015-09-25 02:29:14 +00001253uint32_t SSL_CTX_set_options(SSL_CTX *ctx, uint32_t options) {
1254 ctx->options |= options;
1255 return ctx->options;
1256}
1257
1258uint32_t SSL_CTX_clear_options(SSL_CTX *ctx, uint32_t options) {
1259 ctx->options &= ~options;
1260 return ctx->options;
1261}
1262
1263uint32_t SSL_CTX_get_options(const SSL_CTX *ctx) { return ctx->options; }
1264
1265uint32_t SSL_set_options(SSL *ssl, uint32_t options) {
1266 ssl->options |= options;
1267 return ssl->options;
1268}
1269
1270uint32_t SSL_clear_options(SSL *ssl, uint32_t options) {
1271 ssl->options &= ~options;
1272 return ssl->options;
1273}
1274
1275uint32_t SSL_get_options(const SSL *ssl) { return ssl->options; }
1276
1277uint32_t SSL_CTX_set_mode(SSL_CTX *ctx, uint32_t mode) {
1278 ctx->mode |= mode;
1279 return ctx->mode;
1280}
1281
1282uint32_t SSL_CTX_clear_mode(SSL_CTX *ctx, uint32_t mode) {
1283 ctx->mode &= ~mode;
1284 return ctx->mode;
1285}
1286
1287uint32_t SSL_CTX_get_mode(const SSL_CTX *ctx) { return ctx->mode; }
1288
1289uint32_t SSL_set_mode(SSL *ssl, uint32_t mode) {
1290 ssl->mode |= mode;
1291 return ssl->mode;
1292}
1293
1294uint32_t SSL_clear_mode(SSL *ssl, uint32_t mode) {
1295 ssl->mode &= ~mode;
1296 return ssl->mode;
1297}
1298
1299uint32_t SSL_get_mode(const SSL *ssl) { return ssl->mode; }
1300
Steven Valdeze7531f02016-12-14 13:29:57 -05001301void SSL_CTX_set0_buffer_pool(SSL_CTX *ctx, CRYPTO_BUFFER_POOL *pool) {
1302 ctx->pool = pool;
1303}
1304
Kenny Rootb8494592015-09-25 02:29:14 +00001305int SSL_get_tls_unique(const SSL *ssl, uint8_t *out, size_t *out_len,
1306 size_t max_out) {
Robert Sloana12bf462017-07-17 07:08:26 -07001307 *out_len = 0;
1308 OPENSSL_memset(out, 0, max_out);
1309
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001310 // tls-unique is not defined for TLS 1.3.
David Benjamin95add822016-10-19 01:09:12 -04001311 if (!ssl->s3->initial_handshake_complete ||
Robert Sloan921ef2c2017-10-17 09:02:20 -07001312 ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
Robert Sloana12bf462017-07-17 07:08:26 -07001313 return 0;
David Benjamin95add822016-10-19 01:09:12 -04001314 }
1315
Robert Sloana27a6a42017-09-05 08:39:28 -07001316 // The tls-unique value is the first Finished message in the handshake, which
1317 // is the client's in a full handshake and the server's for a resumption. See
1318 // https://tools.ietf.org/html/rfc5929#section-3.1.
Kenny Rootb8494592015-09-25 02:29:14 +00001319 const uint8_t *finished = ssl->s3->previous_client_finished;
1320 size_t finished_len = ssl->s3->previous_client_finished_len;
David Benjaminc895d6b2016-08-11 13:26:41 -04001321 if (ssl->session != NULL) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001322 // tls-unique is broken for resumed sessions unless EMS is used.
Kenny Rootb8494592015-09-25 02:29:14 +00001323 if (!ssl->session->extended_master_secret) {
Robert Sloana12bf462017-07-17 07:08:26 -07001324 return 0;
Kenny Rootb8494592015-09-25 02:29:14 +00001325 }
1326 finished = ssl->s3->previous_server_finished;
1327 finished_len = ssl->s3->previous_server_finished_len;
1328 }
1329
Kenny Rootb8494592015-09-25 02:29:14 +00001330 *out_len = finished_len;
1331 if (finished_len > max_out) {
1332 *out_len = max_out;
1333 }
1334
Robert Sloan69939df2017-01-09 10:53:07 -08001335 OPENSSL_memcpy(out, finished, *out_len);
Kenny Rootb8494592015-09-25 02:29:14 +00001336 return 1;
Kenny Rootb8494592015-09-25 02:29:14 +00001337}
1338
Robert Sloana94fe052017-02-21 08:49:28 -08001339static int set_session_id_context(CERT *cert, const uint8_t *sid_ctx,
David Benjamin1b249672016-12-06 18:25:50 -05001340 size_t sid_ctx_len) {
Robert Sloana94fe052017-02-21 08:49:28 -08001341 if (sid_ctx_len > sizeof(cert->sid_ctx)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001342 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001343 return 0;
1344 }
David Benjamin1b249672016-12-06 18:25:50 -05001345
Robert Sloana12bf462017-07-17 07:08:26 -07001346 static_assert(sizeof(cert->sid_ctx) < 256, "sid_ctx too large");
Robert Sloana94fe052017-02-21 08:49:28 -08001347 cert->sid_ctx_length = (uint8_t)sid_ctx_len;
1348 OPENSSL_memcpy(cert->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001349 return 1;
1350}
1351
Robert Sloana94fe052017-02-21 08:49:28 -08001352int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
1353 size_t sid_ctx_len) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001354 return set_session_id_context(ctx->cert.get(), sid_ctx, sid_ctx_len);
Robert Sloana94fe052017-02-21 08:49:28 -08001355}
1356
Adam Langleyd9e397b2015-01-22 14:27:53 -08001357int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
David Benjamin1b249672016-12-06 18:25:50 -05001358 size_t sid_ctx_len) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001359 if (!ssl->config) {
1360 return 0;
1361 }
1362 return set_session_id_context(ssl->config->cert.get(), sid_ctx, sid_ctx_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001363}
1364
David Benjaminf31229b2017-01-25 14:08:15 -05001365const uint8_t *SSL_get0_session_id_context(const SSL *ssl, size_t *out_len) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001366 if (!ssl->config) {
1367 assert(ssl->config);
1368 *out_len = 0;
1369 return NULL;
1370 }
1371 *out_len = ssl->config->cert->sid_ctx_length;
1372 return ssl->config->cert->sid_ctx;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001373}
1374
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001375void SSL_certs_clear(SSL *ssl) {
1376 if (!ssl->config) {
1377 return;
1378 }
1379 ssl_cert_clear_certs(ssl->config->cert.get());
1380}
Adam Langleyd9e397b2015-01-22 14:27:53 -08001381
Kenny Roote99801b2015-11-06 15:31:15 -08001382int SSL_get_fd(const SSL *ssl) { return SSL_get_rfd(ssl); }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001383
Kenny Roote99801b2015-11-06 15:31:15 -08001384int SSL_get_rfd(const SSL *ssl) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001385 int ret = -1;
Kenny Roote99801b2015-11-06 15:31:15 -08001386 BIO *b = BIO_find_type(SSL_get_rbio(ssl), BIO_TYPE_DESCRIPTOR);
1387 if (b != NULL) {
1388 BIO_get_fd(b, &ret);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001389 }
1390 return ret;
1391}
1392
Kenny Roote99801b2015-11-06 15:31:15 -08001393int SSL_get_wfd(const SSL *ssl) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001394 int ret = -1;
Kenny Roote99801b2015-11-06 15:31:15 -08001395 BIO *b = BIO_find_type(SSL_get_wbio(ssl), BIO_TYPE_DESCRIPTOR);
1396 if (b != NULL) {
1397 BIO_get_fd(b, &ret);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001398 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001399 return ret;
1400}
1401
Kenny Roote99801b2015-11-06 15:31:15 -08001402int SSL_set_fd(SSL *ssl, int fd) {
David Benjamin4969cc92016-04-22 15:02:23 -04001403 BIO *bio = BIO_new(BIO_s_socket());
Adam Langleyd9e397b2015-01-22 14:27:53 -08001404 if (bio == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001405 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
Kenny Roote99801b2015-11-06 15:31:15 -08001406 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001407 }
1408 BIO_set_fd(bio, fd, BIO_NOCLOSE);
Kenny Roote99801b2015-11-06 15:31:15 -08001409 SSL_set_bio(ssl, bio, bio);
1410 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001411}
1412
Kenny Roote99801b2015-11-06 15:31:15 -08001413int SSL_set_wfd(SSL *ssl, int fd) {
David Benjamind316cba2016-06-02 16:17:39 -04001414 BIO *rbio = SSL_get_rbio(ssl);
1415 if (rbio == NULL || BIO_method_type(rbio) != BIO_TYPE_SOCKET ||
1416 BIO_get_fd(rbio, NULL) != fd) {
David Benjamin4969cc92016-04-22 15:02:23 -04001417 BIO *bio = BIO_new(BIO_s_socket());
Adam Langleyd9e397b2015-01-22 14:27:53 -08001418 if (bio == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001419 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
Kenny Roote99801b2015-11-06 15:31:15 -08001420 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001421 }
1422 BIO_set_fd(bio, fd, BIO_NOCLOSE);
David Benjaminc895d6b2016-08-11 13:26:41 -04001423 SSL_set0_wbio(ssl, bio);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001424 } else {
Robert Sloana27a6a42017-09-05 08:39:28 -07001425 // Copy the rbio over to the wbio.
David Benjaminc895d6b2016-08-11 13:26:41 -04001426 BIO_up_ref(rbio);
1427 SSL_set0_wbio(ssl, rbio);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001428 }
1429
Kenny Roote99801b2015-11-06 15:31:15 -08001430 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001431}
1432
Kenny Roote99801b2015-11-06 15:31:15 -08001433int SSL_set_rfd(SSL *ssl, int fd) {
David Benjamind316cba2016-06-02 16:17:39 -04001434 BIO *wbio = SSL_get_wbio(ssl);
1435 if (wbio == NULL || BIO_method_type(wbio) != BIO_TYPE_SOCKET ||
1436 BIO_get_fd(wbio, NULL) != fd) {
David Benjamin4969cc92016-04-22 15:02:23 -04001437 BIO *bio = BIO_new(BIO_s_socket());
Adam Langleyd9e397b2015-01-22 14:27:53 -08001438 if (bio == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001439 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
Kenny Roote99801b2015-11-06 15:31:15 -08001440 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001441 }
1442 BIO_set_fd(bio, fd, BIO_NOCLOSE);
David Benjaminc895d6b2016-08-11 13:26:41 -04001443 SSL_set0_rbio(ssl, bio);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001444 } else {
Robert Sloana27a6a42017-09-05 08:39:28 -07001445 // Copy the wbio over to the rbio.
David Benjaminc895d6b2016-08-11 13:26:41 -04001446 BIO_up_ref(wbio);
1447 SSL_set0_rbio(ssl, wbio);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001448 }
Kenny Roote99801b2015-11-06 15:31:15 -08001449 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001450}
1451
David Benjamin95add822016-10-19 01:09:12 -04001452static size_t copy_finished(void *out, size_t out_len, const uint8_t *in,
1453 size_t in_len) {
1454 if (out_len > in_len) {
1455 out_len = in_len;
1456 }
Robert Sloan69939df2017-01-09 10:53:07 -08001457 OPENSSL_memcpy(out, in, out_len);
David Benjamin95add822016-10-19 01:09:12 -04001458 return in_len;
1459}
Adam Langleyd9e397b2015-01-22 14:27:53 -08001460
David Benjamin95add822016-10-19 01:09:12 -04001461size_t SSL_get_finished(const SSL *ssl, void *buf, size_t count) {
1462 if (!ssl->s3->initial_handshake_complete ||
Robert Sloan921ef2c2017-10-17 09:02:20 -07001463 ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjamin95add822016-10-19 01:09:12 -04001464 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001465 }
1466
David Benjamin95add822016-10-19 01:09:12 -04001467 if (ssl->server) {
1468 return copy_finished(buf, count, ssl->s3->previous_server_finished,
1469 ssl->s3->previous_server_finished_len);
1470 }
1471
1472 return copy_finished(buf, count, ssl->s3->previous_client_finished,
1473 ssl->s3->previous_client_finished_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001474}
1475
Kenny Roote99801b2015-11-06 15:31:15 -08001476size_t SSL_get_peer_finished(const SSL *ssl, void *buf, size_t count) {
David Benjamin95add822016-10-19 01:09:12 -04001477 if (!ssl->s3->initial_handshake_complete ||
Robert Sloan921ef2c2017-10-17 09:02:20 -07001478 ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjamin95add822016-10-19 01:09:12 -04001479 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001480 }
1481
David Benjamin95add822016-10-19 01:09:12 -04001482 if (ssl->server) {
1483 return copy_finished(buf, count, ssl->s3->previous_client_finished,
1484 ssl->s3->previous_client_finished_len);
1485 }
1486
1487 return copy_finished(buf, count, ssl->s3->previous_server_finished,
1488 ssl->s3->previous_server_finished_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001489}
1490
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001491int SSL_get_verify_mode(const SSL *ssl) {
1492 if (!ssl->config) {
1493 assert(ssl->config);
1494 return -1;
1495 }
1496 return ssl->config->verify_mode;
1497}
Adam Langleyd9e397b2015-01-22 14:27:53 -08001498
Kenny Rootb8494592015-09-25 02:29:14 +00001499int SSL_get_extms_support(const SSL *ssl) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001500 // TLS 1.3 does not require extended master secret and always reports as
1501 // supporting it.
David Benjaminc895d6b2016-08-11 13:26:41 -04001502 if (!ssl->s3->have_version) {
1503 return 0;
1504 }
Robert Sloan921ef2c2017-10-17 09:02:20 -07001505 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
Robert Sloana94fe052017-02-21 08:49:28 -08001506 return 1;
1507 }
1508
Robert Sloana27a6a42017-09-05 08:39:28 -07001509 // If the initial handshake completed, query the established session.
Robert Sloana94fe052017-02-21 08:49:28 -08001510 if (ssl->s3->established_session != NULL) {
1511 return ssl->s3->established_session->extended_master_secret;
1512 }
1513
Robert Sloana27a6a42017-09-05 08:39:28 -07001514 // Otherwise, query the in-progress handshake.
Robert Sloana94fe052017-02-21 08:49:28 -08001515 if (ssl->s3->hs != NULL) {
1516 return ssl->s3->hs->extended_master_secret;
1517 }
1518 assert(0);
1519 return 0;
Kenny Rootb8494592015-09-25 02:29:14 +00001520}
1521
Adam Langleyf4e42722015-06-04 17:45:09 -07001522int SSL_CTX_get_read_ahead(const SSL_CTX *ctx) { return 0; }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001523
Adam Langley4139edb2016-01-13 15:00:54 -08001524int SSL_get_read_ahead(const SSL *ssl) { return 0; }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001525
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001526int SSL_CTX_set_read_ahead(SSL_CTX *ctx, int yes) { return 1; }
Adam Langleye9ada862015-05-11 17:20:37 -07001527
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001528int SSL_set_read_ahead(SSL *ssl, int yes) { return 1; }
Adam Langleye9ada862015-05-11 17:20:37 -07001529
Kenny Roote99801b2015-11-06 15:31:15 -08001530int SSL_pending(const SSL *ssl) {
Robert Sloan36272962017-10-23 10:28:39 -07001531 return static_cast<int>(ssl->s3->pending_app_data.size());
Adam Langleyd9e397b2015-01-22 14:27:53 -08001532}
1533
Adam Langleyd9e397b2015-01-22 14:27:53 -08001534int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001535 return ssl_cert_check_private_key(ctx->cert.get(),
1536 ctx->cert->privatekey.get());
Steven Valdez909b19f2016-11-21 15:35:44 -05001537}
1538
Steven Valdez909b19f2016-11-21 15:35:44 -05001539int SSL_check_private_key(const SSL *ssl) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001540 if (!ssl->config) {
1541 return 0;
1542 }
1543 return ssl_cert_check_private_key(ssl->config->cert.get(),
1544 ssl->config->cert->privatekey.get());
Adam Langleyd9e397b2015-01-22 14:27:53 -08001545}
1546
Kenny Rootb8494592015-09-25 02:29:14 +00001547long SSL_get_default_timeout(const SSL *ssl) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001548 return SSL_DEFAULT_SESSION_TIMEOUT;
1549}
1550
Adam Langleyf4e42722015-06-04 17:45:09 -07001551int SSL_renegotiate(SSL *ssl) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001552 // Caller-initiated renegotiation is not supported.
Kenny Rootb8494592015-09-25 02:29:14 +00001553 OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
Adam Langleyf4e42722015-06-04 17:45:09 -07001554 return 0;
1555}
1556
1557int SSL_renegotiate_pending(SSL *ssl) {
1558 return SSL_in_init(ssl) && ssl->s3->initial_handshake_complete;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001559}
1560
David Benjamin6e899c72016-06-09 18:02:18 -04001561int SSL_total_renegotiations(const SSL *ssl) {
1562 return ssl->s3->total_renegotiations;
1563}
1564
Adam Langleye9ada862015-05-11 17:20:37 -07001565size_t SSL_CTX_get_max_cert_list(const SSL_CTX *ctx) {
1566 return ctx->max_cert_list;
1567}
1568
1569void SSL_CTX_set_max_cert_list(SSL_CTX *ctx, size_t max_cert_list) {
1570 if (max_cert_list > kMaxHandshakeSize) {
1571 max_cert_list = kMaxHandshakeSize;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001572 }
Adam Langleye9ada862015-05-11 17:20:37 -07001573 ctx->max_cert_list = (uint32_t)max_cert_list;
1574}
1575
1576size_t SSL_get_max_cert_list(const SSL *ssl) {
1577 return ssl->max_cert_list;
1578}
1579
1580void SSL_set_max_cert_list(SSL *ssl, size_t max_cert_list) {
1581 if (max_cert_list > kMaxHandshakeSize) {
1582 max_cert_list = kMaxHandshakeSize;
1583 }
1584 ssl->max_cert_list = (uint32_t)max_cert_list;
1585}
1586
David Benjamin4969cc92016-04-22 15:02:23 -04001587int SSL_CTX_set_max_send_fragment(SSL_CTX *ctx, size_t max_send_fragment) {
Adam Langleye9ada862015-05-11 17:20:37 -07001588 if (max_send_fragment < 512) {
1589 max_send_fragment = 512;
1590 }
1591 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1592 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1593 }
1594 ctx->max_send_fragment = (uint16_t)max_send_fragment;
David Benjamin4969cc92016-04-22 15:02:23 -04001595
1596 return 1;
Adam Langleye9ada862015-05-11 17:20:37 -07001597}
1598
David Benjamin4969cc92016-04-22 15:02:23 -04001599int SSL_set_max_send_fragment(SSL *ssl, size_t max_send_fragment) {
Adam Langleye9ada862015-05-11 17:20:37 -07001600 if (max_send_fragment < 512) {
1601 max_send_fragment = 512;
1602 }
1603 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1604 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1605 }
1606 ssl->max_send_fragment = (uint16_t)max_send_fragment;
David Benjamin4969cc92016-04-22 15:02:23 -04001607
1608 return 1;
Adam Langleye9ada862015-05-11 17:20:37 -07001609}
1610
1611int SSL_set_mtu(SSL *ssl, unsigned mtu) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001612 if (!SSL_is_dtls(ssl) || mtu < dtls1_min_mtu()) {
Adam Langleye9ada862015-05-11 17:20:37 -07001613 return 0;
1614 }
1615 ssl->d1->mtu = mtu;
1616 return 1;
1617}
1618
1619int SSL_get_secure_renegotiation_support(const SSL *ssl) {
Steven Valdezb0b45c62017-01-17 16:23:54 -05001620 if (!ssl->s3->have_version) {
1621 return 0;
1622 }
Robert Sloan921ef2c2017-10-17 09:02:20 -07001623 return ssl_protocol_version(ssl) >= TLS1_3_VERSION ||
Steven Valdezb0b45c62017-01-17 16:23:54 -05001624 ssl->s3->send_connection_binding;
Adam Langleye9ada862015-05-11 17:20:37 -07001625}
1626
Adam Langleye9ada862015-05-11 17:20:37 -07001627size_t SSL_CTX_sess_number(const SSL_CTX *ctx) {
Robert Sloan4562e9d2017-10-02 10:26:51 -07001628 MutexReadLock lock(const_cast<CRYPTO_MUTEX *>(&ctx->lock));
Adam Langleye9ada862015-05-11 17:20:37 -07001629 return lh_SSL_SESSION_num_items(ctx->sessions);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001630}
1631
Adam Langleye9ada862015-05-11 17:20:37 -07001632unsigned long SSL_CTX_sess_set_cache_size(SSL_CTX *ctx, unsigned long size) {
1633 unsigned long ret = ctx->session_cache_size;
1634 ctx->session_cache_size = size;
1635 return ret;
1636}
Adam Langleyd9e397b2015-01-22 14:27:53 -08001637
Adam Langleye9ada862015-05-11 17:20:37 -07001638unsigned long SSL_CTX_sess_get_cache_size(const SSL_CTX *ctx) {
1639 return ctx->session_cache_size;
1640}
1641
1642int SSL_CTX_set_session_cache_mode(SSL_CTX *ctx, int mode) {
1643 int ret = ctx->session_cache_mode;
1644 ctx->session_cache_mode = mode;
1645 return ret;
1646}
1647
1648int SSL_CTX_get_session_cache_mode(const SSL_CTX *ctx) {
1649 return ctx->session_cache_mode;
1650}
1651
David Benjamin6e899c72016-06-09 18:02:18 -04001652
1653int SSL_CTX_get_tlsext_ticket_keys(SSL_CTX *ctx, void *out, size_t len) {
1654 if (out == NULL) {
1655 return 48;
1656 }
1657 if (len != 48) {
1658 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH);
1659 return 0;
1660 }
Robert Sloan8f860b12017-08-28 07:37:06 -07001661
Robert Sloana27a6a42017-09-05 08:39:28 -07001662 // The default ticket keys are initialized lazily. Trigger a key
1663 // rotation to initialize them.
Robert Sloan8f860b12017-08-28 07:37:06 -07001664 if (!ssl_ctx_rotate_ticket_encryption_key(ctx)) {
1665 return 0;
1666 }
1667
Robert Sloana12bf462017-07-17 07:08:26 -07001668 uint8_t *out_bytes = reinterpret_cast<uint8_t *>(out);
Robert Sloan8f860b12017-08-28 07:37:06 -07001669 MutexReadLock lock(&ctx->lock);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001670 OPENSSL_memcpy(out_bytes, ctx->ticket_key_current->name, 16);
1671 OPENSSL_memcpy(out_bytes + 16, ctx->ticket_key_current->hmac_key, 16);
1672 OPENSSL_memcpy(out_bytes + 32, ctx->ticket_key_current->aes_key, 16);
David Benjamin6e899c72016-06-09 18:02:18 -04001673 return 1;
1674}
1675
1676int SSL_CTX_set_tlsext_ticket_keys(SSL_CTX *ctx, const void *in, size_t len) {
1677 if (in == NULL) {
1678 return 48;
1679 }
1680 if (len != 48) {
1681 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH);
1682 return 0;
1683 }
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001684 auto key = MakeUnique<TicketKey>();
1685 if (!key) {
1686 return 0;
Robert Sloan8f860b12017-08-28 07:37:06 -07001687 }
Robert Sloana12bf462017-07-17 07:08:26 -07001688 const uint8_t *in_bytes = reinterpret_cast<const uint8_t *>(in);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001689 OPENSSL_memcpy(key->name, in_bytes, 16);
1690 OPENSSL_memcpy(key->hmac_key, in_bytes + 16, 16);
1691 OPENSSL_memcpy(key->aes_key, in_bytes + 32, 16);
1692 // Disable automatic key rotation for manually-configured keys. This is now
1693 // the caller's responsibility.
1694 key->next_rotation_tv_sec = 0;
1695 ctx->ticket_key_current = std::move(key);
1696 ctx->ticket_key_prev.reset();
David Benjamin6e899c72016-06-09 18:02:18 -04001697 return 1;
1698}
1699
1700int SSL_CTX_set_tlsext_ticket_key_cb(
1701 SSL_CTX *ctx, int (*callback)(SSL *ssl, uint8_t *key_name, uint8_t *iv,
1702 EVP_CIPHER_CTX *ctx, HMAC_CTX *hmac_ctx,
1703 int encrypt)) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001704 ctx->ticket_key_cb = callback;
David Benjamin6e899c72016-06-09 18:02:18 -04001705 return 1;
1706}
1707
1708int SSL_CTX_set1_curves(SSL_CTX *ctx, const int *curves, size_t curves_len) {
1709 return tls1_set_curves(&ctx->supported_group_list,
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001710 MakeConstSpan(curves, curves_len));
David Benjamin6e899c72016-06-09 18:02:18 -04001711}
1712
1713int SSL_set1_curves(SSL *ssl, const int *curves, size_t curves_len) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001714 if (!ssl->config) {
1715 return 0;
1716 }
1717 return tls1_set_curves(&ssl->config->supported_group_list,
1718 MakeConstSpan(curves, curves_len));
David Benjamin6e899c72016-06-09 18:02:18 -04001719}
1720
Steven Valdezbb1ceac2016-10-07 10:34:51 -04001721int SSL_CTX_set1_curves_list(SSL_CTX *ctx, const char *curves) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001722 return tls1_set_curves_list(&ctx->supported_group_list, curves);
Steven Valdezbb1ceac2016-10-07 10:34:51 -04001723}
1724
1725int SSL_set1_curves_list(SSL *ssl, const char *curves) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001726 if (!ssl->config) {
1727 return 0;
1728 }
1729 return tls1_set_curves_list(&ssl->config->supported_group_list, curves);
Steven Valdezbb1ceac2016-10-07 10:34:51 -04001730}
1731
David Benjaminc895d6b2016-08-11 13:26:41 -04001732uint16_t SSL_get_curve_id(const SSL *ssl) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001733 // TODO(davidben): This checks the wrong session if there is a renegotiation
1734 // in progress.
David Benjaminc895d6b2016-08-11 13:26:41 -04001735 SSL_SESSION *session = SSL_get_session(ssl);
Steven Valdeze7531f02016-12-14 13:29:57 -05001736 if (session == NULL) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001737 return 0;
1738 }
1739
Steven Valdeze7531f02016-12-14 13:29:57 -05001740 return session->group_id;
David Benjaminc895d6b2016-08-11 13:26:41 -04001741}
1742
David Benjamin6e899c72016-06-09 18:02:18 -04001743int SSL_CTX_set_tmp_dh(SSL_CTX *ctx, const DH *dh) {
David Benjamin6e899c72016-06-09 18:02:18 -04001744 return 1;
1745}
1746
1747int SSL_set_tmp_dh(SSL *ssl, const DH *dh) {
David Benjamin6e899c72016-06-09 18:02:18 -04001748 return 1;
1749}
1750
Robert Sloan84377092017-08-14 09:33:19 -07001751STACK_OF(SSL_CIPHER) *SSL_CTX_get_ciphers(const SSL_CTX *ctx) {
Robert Sloanc6ebb282018-04-30 10:10:26 -07001752 return ctx->cipher_list->ciphers.get();
Robert Sloan7d422bc2017-03-06 10:04:29 -08001753}
1754
Robert Sloan84377092017-08-14 09:33:19 -07001755int SSL_CTX_cipher_in_group(const SSL_CTX *ctx, size_t i) {
Robert Sloanc6ebb282018-04-30 10:10:26 -07001756 if (i >= sk_SSL_CIPHER_num(ctx->cipher_list->ciphers.get())) {
Robert Sloan84377092017-08-14 09:33:19 -07001757 return 0;
1758 }
1759 return ctx->cipher_list->in_group_flags[i];
1760}
1761
Kenny Roote99801b2015-11-06 15:31:15 -08001762STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *ssl) {
1763 if (ssl == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001764 return NULL;
1765 }
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001766 if (ssl->config == NULL) {
1767 assert(ssl->config);
1768 return NULL;
1769 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001770
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001771 return ssl->config->cipher_list ? ssl->config->cipher_list->ciphers.get()
1772 : ssl->ctx->cipher_list->ciphers.get();
Adam Langleyd9e397b2015-01-22 14:27:53 -08001773}
1774
Kenny Roote99801b2015-11-06 15:31:15 -08001775const char *SSL_get_cipher_list(const SSL *ssl, int n) {
Kenny Roote99801b2015-11-06 15:31:15 -08001776 if (ssl == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001777 return NULL;
1778 }
1779
Robert Sloan7d422bc2017-03-06 10:04:29 -08001780 STACK_OF(SSL_CIPHER) *sk = SSL_get_ciphers(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001781 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
1782 return NULL;
1783 }
1784
Robert Sloan7d422bc2017-03-06 10:04:29 -08001785 const SSL_CIPHER *c = sk_SSL_CIPHER_value(sk, n);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001786 if (c == NULL) {
1787 return NULL;
1788 }
1789
1790 return c->name;
1791}
1792
Adam Langleyd9e397b2015-01-22 14:27:53 -08001793int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
Robert Sloanb1b54b82017-11-06 13:50:02 -08001794 return ssl_create_cipher_list(&ctx->cipher_list, str, false /* not strict */);
Robert Sloan7c50ec52017-02-27 08:17:21 -08001795}
1796
1797int SSL_CTX_set_strict_cipher_list(SSL_CTX *ctx, const char *str) {
Robert Sloanb1b54b82017-11-06 13:50:02 -08001798 return ssl_create_cipher_list(&ctx->cipher_list, str, true /* strict */);
Kenny Rootb8494592015-09-25 02:29:14 +00001799}
1800
Kenny Roote99801b2015-11-06 15:31:15 -08001801int SSL_set_cipher_list(SSL *ssl, const char *str) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001802 if (!ssl->config) {
1803 return 0;
1804 }
1805 return ssl_create_cipher_list(&ssl->config->cipher_list, str,
1806 false /* not strict */);
Robert Sloan7c50ec52017-02-27 08:17:21 -08001807}
1808
1809int SSL_set_strict_cipher_list(SSL *ssl, const char *str) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001810 if (!ssl->config) {
1811 return 0;
1812 }
1813 return ssl_create_cipher_list(&ssl->config->cipher_list, str,
1814 true /* strict */);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001815}
1816
Kenny Roote99801b2015-11-06 15:31:15 -08001817const char *SSL_get_servername(const SSL *ssl, const int type) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001818 if (type != TLSEXT_NAMETYPE_host_name) {
1819 return NULL;
1820 }
1821
Robert Sloana27a6a42017-09-05 08:39:28 -07001822 // Historically, |SSL_get_servername| was also the configuration getter
1823 // corresponding to |SSL_set_tlsext_host_name|.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001824 if (ssl->hostname != nullptr) {
1825 return ssl->hostname.get();
Kenny Roote99801b2015-11-06 15:31:15 -08001826 }
1827
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001828 return ssl->s3->hostname.get();
Adam Langleyd9e397b2015-01-22 14:27:53 -08001829}
1830
Kenny Roote99801b2015-11-06 15:31:15 -08001831int SSL_get_servername_type(const SSL *ssl) {
Robert Sloand1d118f2017-09-11 09:00:48 -07001832 if (SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) == NULL) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001833 return -1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001834 }
David Benjaminc895d6b2016-08-11 13:26:41 -04001835 return TLSEXT_NAMETYPE_host_name;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001836}
1837
Robert Sloanb6d070c2017-07-24 08:40:01 -07001838void SSL_CTX_set_custom_verify(
1839 SSL_CTX *ctx, int mode,
1840 enum ssl_verify_result_t (*callback)(SSL *ssl, uint8_t *out_alert)) {
1841 ctx->verify_mode = mode;
1842 ctx->custom_verify_callback = callback;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001843}
1844
Robert Sloanb6d070c2017-07-24 08:40:01 -07001845void SSL_set_custom_verify(
1846 SSL *ssl, int mode,
1847 enum ssl_verify_result_t (*callback)(SSL *ssl, uint8_t *out_alert)) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001848 if (!ssl->config) {
1849 return;
1850 }
1851 ssl->config->verify_mode = mode;
1852 ssl->config->custom_verify_callback = callback;
Robert Sloanb6d070c2017-07-24 08:40:01 -07001853}
1854
1855void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001856 ctx->signed_cert_timestamps_enabled = true;
Robert Sloan1c9db532017-03-13 08:03:59 -07001857}
1858
Robert Sloana94fe052017-02-21 08:49:28 -08001859void SSL_enable_signed_cert_timestamps(SSL *ssl) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001860 if (!ssl->config) {
1861 return;
1862 }
1863 ssl->config->signed_cert_timestamps_enabled = true;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001864}
1865
1866void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001867 ctx->ocsp_stapling_enabled = true;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001868}
1869
Robert Sloana94fe052017-02-21 08:49:28 -08001870void SSL_enable_ocsp_stapling(SSL *ssl) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001871 if (!ssl->config) {
1872 return;
1873 }
1874 ssl->config->ocsp_stapling_enabled = true;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001875}
1876
1877void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
1878 size_t *out_len) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001879 SSL_SESSION *session = SSL_get_session(ssl);
Robert Sloan8f860b12017-08-28 07:37:06 -07001880 if (ssl->server || !session || !session->signed_cert_timestamp_list) {
1881 *out_len = 0;
1882 *out = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001883 return;
1884 }
1885
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001886 *out = CRYPTO_BUFFER_data(session->signed_cert_timestamp_list.get());
1887 *out_len = CRYPTO_BUFFER_len(session->signed_cert_timestamp_list.get());
Adam Langleyd9e397b2015-01-22 14:27:53 -08001888}
1889
1890void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
1891 size_t *out_len) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001892 SSL_SESSION *session = SSL_get_session(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001893 if (ssl->server || !session || !session->ocsp_response) {
Robert Sloan8f860b12017-08-28 07:37:06 -07001894 *out_len = 0;
1895 *out = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001896 return;
1897 }
Robert Sloan8f860b12017-08-28 07:37:06 -07001898
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001899 *out = CRYPTO_BUFFER_data(session->ocsp_response.get());
1900 *out_len = CRYPTO_BUFFER_len(session->ocsp_response.get());
Adam Langleyd9e397b2015-01-22 14:27:53 -08001901}
1902
David Benjamin6e899c72016-06-09 18:02:18 -04001903int SSL_set_tlsext_host_name(SSL *ssl, const char *name) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001904 ssl->hostname.reset();
1905 if (name == nullptr) {
David Benjamin6e899c72016-06-09 18:02:18 -04001906 return 1;
1907 }
1908
1909 size_t len = strlen(name);
1910 if (len == 0 || len > TLSEXT_MAXLEN_host_name) {
1911 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL3_EXT_INVALID_SERVERNAME);
1912 return 0;
1913 }
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001914 ssl->hostname.reset(BUF_strdup(name));
1915 if (ssl->hostname == nullptr) {
David Benjamin6e899c72016-06-09 18:02:18 -04001916 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1917 return 0;
1918 }
1919 return 1;
1920}
1921
1922int SSL_CTX_set_tlsext_servername_callback(
1923 SSL_CTX *ctx, int (*callback)(SSL *ssl, int *out_alert, void *arg)) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001924 ctx->servername_callback = callback;
David Benjamin6e899c72016-06-09 18:02:18 -04001925 return 1;
1926}
1927
1928int SSL_CTX_set_tlsext_servername_arg(SSL_CTX *ctx, void *arg) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001929 ctx->servername_arg = arg;
David Benjamin6e899c72016-06-09 18:02:18 -04001930 return 1;
1931}
1932
Robert Sloane56da3e2017-06-26 08:26:42 -07001933int SSL_select_next_proto(uint8_t **out, uint8_t *out_len, const uint8_t *peer,
1934 unsigned peer_len, const uint8_t *supported,
1935 unsigned supported_len) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001936 const uint8_t *result;
Robert Sloane56da3e2017-06-26 08:26:42 -07001937 int status;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001938
Robert Sloana27a6a42017-09-05 08:39:28 -07001939 // For each protocol in peer preference order, see if we support it.
Robert Sloane56da3e2017-06-26 08:26:42 -07001940 for (unsigned i = 0; i < peer_len;) {
1941 for (unsigned j = 0; j < supported_len;) {
1942 if (peer[i] == supported[j] &&
1943 OPENSSL_memcmp(&peer[i + 1], &supported[j + 1], peer[i]) == 0) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001944 // We found a match
Robert Sloane56da3e2017-06-26 08:26:42 -07001945 result = &peer[i];
Adam Langleyd9e397b2015-01-22 14:27:53 -08001946 status = OPENSSL_NPN_NEGOTIATED;
1947 goto found;
1948 }
Robert Sloane56da3e2017-06-26 08:26:42 -07001949 j += supported[j];
Adam Langleyd9e397b2015-01-22 14:27:53 -08001950 j++;
1951 }
Robert Sloane56da3e2017-06-26 08:26:42 -07001952 i += peer[i];
Adam Langleyd9e397b2015-01-22 14:27:53 -08001953 i++;
1954 }
1955
Robert Sloana27a6a42017-09-05 08:39:28 -07001956 // There's no overlap between our protocols and the peer's list.
Robert Sloane56da3e2017-06-26 08:26:42 -07001957 result = supported;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001958 status = OPENSSL_NPN_NO_OVERLAP;
1959
1960found:
1961 *out = (uint8_t *)result + 1;
Kenny Rootb8494592015-09-25 02:29:14 +00001962 *out_len = result[0];
Adam Langleyd9e397b2015-01-22 14:27:53 -08001963 return status;
1964}
1965
Kenny Rootb8494592015-09-25 02:29:14 +00001966void SSL_get0_next_proto_negotiated(const SSL *ssl, const uint8_t **out_data,
1967 unsigned *out_len) {
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001968 *out_data = ssl->s3->next_proto_negotiated.data();
1969 *out_len = ssl->s3->next_proto_negotiated.size();
Adam Langleyd9e397b2015-01-22 14:27:53 -08001970}
1971
Adam Langleyd9e397b2015-01-22 14:27:53 -08001972void SSL_CTX_set_next_protos_advertised_cb(
1973 SSL_CTX *ctx,
Kenny Rootb8494592015-09-25 02:29:14 +00001974 int (*cb)(SSL *ssl, const uint8_t **out, unsigned *out_len, void *arg),
Adam Langleyd9e397b2015-01-22 14:27:53 -08001975 void *arg) {
1976 ctx->next_protos_advertised_cb = cb;
1977 ctx->next_protos_advertised_cb_arg = arg;
1978}
1979
Adam Langleyd9e397b2015-01-22 14:27:53 -08001980void SSL_CTX_set_next_proto_select_cb(
Kenny Rootb8494592015-09-25 02:29:14 +00001981 SSL_CTX *ctx, int (*cb)(SSL *ssl, uint8_t **out, uint8_t *out_len,
1982 const uint8_t *in, unsigned in_len, void *arg),
Adam Langleyd9e397b2015-01-22 14:27:53 -08001983 void *arg) {
1984 ctx->next_proto_select_cb = cb;
1985 ctx->next_proto_select_cb_arg = arg;
1986}
1987
Adam Langleyd9e397b2015-01-22 14:27:53 -08001988int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
1989 unsigned protos_len) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001990 // Note this function's calling convention is backwards.
1991 return ctx->alpn_client_proto_list.CopyFrom(MakeConstSpan(protos, protos_len))
1992 ? 0
1993 : 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001994}
1995
Adam Langleyd9e397b2015-01-22 14:27:53 -08001996int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001997 // Note this function's calling convention is backwards.
1998 if (!ssl->config) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001999 return 1;
2000 }
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002001 return ssl->config->alpn_client_proto_list.CopyFrom(
2002 MakeConstSpan(protos, protos_len))
2003 ? 0
2004 : 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002005}
2006
Adam Langleyd9e397b2015-01-22 14:27:53 -08002007void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
2008 int (*cb)(SSL *ssl, const uint8_t **out,
Kenny Rootb8494592015-09-25 02:29:14 +00002009 uint8_t *out_len, const uint8_t *in,
2010 unsigned in_len, void *arg),
Adam Langleyd9e397b2015-01-22 14:27:53 -08002011 void *arg) {
2012 ctx->alpn_select_cb = cb;
2013 ctx->alpn_select_cb_arg = arg;
2014}
2015
Kenny Rootb8494592015-09-25 02:29:14 +00002016void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **out_data,
2017 unsigned *out_len) {
Robert Sloane56da3e2017-06-26 08:26:42 -07002018 if (SSL_in_early_data(ssl) && !ssl->server) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002019 *out_data = ssl->s3->hs->early_session->early_alpn.data();
2020 *out_len = ssl->s3->hs->early_session->early_alpn.size();
Adam Langleyd9e397b2015-01-22 14:27:53 -08002021 } else {
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002022 *out_data = ssl->s3->alpn_selected.data();
2023 *out_len = ssl->s3->alpn_selected.size();
Adam Langleyd9e397b2015-01-22 14:27:53 -08002024 }
2025}
2026
Robert Sloan572a4e22017-04-17 10:52:19 -07002027void SSL_CTX_set_allow_unknown_alpn_protos(SSL_CTX *ctx, int enabled) {
2028 ctx->allow_unknown_alpn_protos = !!enabled;
2029}
David Benjamin6e899c72016-06-09 18:02:18 -04002030
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002031int SSL_CTX_add_cert_compression_alg(SSL_CTX *ctx, uint16_t alg_id,
2032 ssl_cert_compression_func_t compress,
2033 ssl_cert_decompression_func_t decompress) {
2034 assert(compress != nullptr || decompress != nullptr);
2035
2036 for (const auto *alg : ctx->cert_compression_algs.get()) {
2037 if (alg->alg_id == alg_id) {
2038 return 0;
2039 }
2040 }
2041
2042 UniquePtr<CertCompressionAlg> alg = MakeUnique<CertCompressionAlg>();
2043 if (alg == nullptr) {
2044 return 0;
2045 }
2046
2047 alg->alg_id = alg_id;
2048 alg->compress = compress;
2049 alg->decompress = decompress;
2050
2051 if (ctx->cert_compression_algs == nullptr) {
2052 ctx->cert_compression_algs.reset(sk_CertCompressionAlg_new_null());
2053 if (ctx->cert_compression_algs == nullptr) {
2054 return 0;
2055 }
2056 }
2057
2058 if (!PushToStack(ctx->cert_compression_algs.get(), std::move(alg))) {
2059 if (sk_CertCompressionAlg_num(ctx->cert_compression_algs.get()) == 0) {
2060 ctx->cert_compression_algs.reset();
2061 }
2062 return 0;
2063 }
2064
2065 return 1;
2066}
2067
David Benjamin1b249672016-12-06 18:25:50 -05002068void SSL_CTX_set_tls_channel_id_enabled(SSL_CTX *ctx, int enabled) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002069 ctx->channel_id_enabled = !!enabled;
David Benjamin1b249672016-12-06 18:25:50 -05002070}
2071
David Benjamin6e899c72016-06-09 18:02:18 -04002072int SSL_CTX_enable_tls_channel_id(SSL_CTX *ctx) {
David Benjamin1b249672016-12-06 18:25:50 -05002073 SSL_CTX_set_tls_channel_id_enabled(ctx, 1);
David Benjamin6e899c72016-06-09 18:02:18 -04002074 return 1;
2075}
2076
David Benjamin1b249672016-12-06 18:25:50 -05002077void SSL_set_tls_channel_id_enabled(SSL *ssl, int enabled) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002078 if (!ssl->config) {
2079 return;
2080 }
2081 ssl->config->channel_id_enabled = !!enabled;
David Benjamin1b249672016-12-06 18:25:50 -05002082}
2083
David Benjamin6e899c72016-06-09 18:02:18 -04002084int SSL_enable_tls_channel_id(SSL *ssl) {
David Benjamin1b249672016-12-06 18:25:50 -05002085 SSL_set_tls_channel_id_enabled(ssl, 1);
David Benjamin6e899c72016-06-09 18:02:18 -04002086 return 1;
2087}
2088
2089static int is_p256_key(EVP_PKEY *private_key) {
2090 const EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(private_key);
2091 return ec_key != NULL &&
2092 EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key)) ==
2093 NID_X9_62_prime256v1;
2094}
2095
2096int SSL_CTX_set1_tls_channel_id(SSL_CTX *ctx, EVP_PKEY *private_key) {
2097 if (!is_p256_key(private_key)) {
2098 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256);
2099 return 0;
2100 }
2101
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002102 ctx->channel_id_private = UpRef(private_key);
2103 ctx->channel_id_enabled = true;
David Benjamin6e899c72016-06-09 18:02:18 -04002104
2105 return 1;
2106}
2107
2108int SSL_set1_tls_channel_id(SSL *ssl, EVP_PKEY *private_key) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002109 if (!ssl->config) {
2110 return 0;
2111 }
David Benjamin6e899c72016-06-09 18:02:18 -04002112 if (!is_p256_key(private_key)) {
2113 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256);
2114 return 0;
2115 }
2116
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002117 ssl->config->channel_id_private = UpRef(private_key);
2118 ssl->config->channel_id_enabled = true;
David Benjamin6e899c72016-06-09 18:02:18 -04002119
2120 return 1;
2121}
2122
2123size_t SSL_get_tls_channel_id(SSL *ssl, uint8_t *out, size_t max_out) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002124 if (!ssl->s3->channel_id_valid) {
David Benjamin6e899c72016-06-09 18:02:18 -04002125 return 0;
2126 }
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002127 OPENSSL_memcpy(out, ssl->s3->channel_id, (max_out < 64) ? max_out : 64);
David Benjamin6e899c72016-06-09 18:02:18 -04002128 return 64;
2129}
2130
Robert Sloan978112c2018-01-22 12:53:01 -08002131int SSL_set_token_binding_params(SSL *ssl, const uint8_t *params, size_t len) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002132 if (!ssl->config) {
2133 return 0;
2134 }
Robert Sloan978112c2018-01-22 12:53:01 -08002135 if (len > 256) {
2136 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
2137 return 0;
2138 }
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002139 return ssl->config->token_binding_params.CopyFrom(MakeConstSpan(params, len));
Robert Sloan978112c2018-01-22 12:53:01 -08002140}
2141
2142int SSL_is_token_binding_negotiated(const SSL *ssl) {
Robert Sloan15c0b352018-04-16 08:36:46 -07002143 return ssl->s3->token_binding_negotiated;
Robert Sloan978112c2018-01-22 12:53:01 -08002144}
2145
2146uint8_t SSL_get_negotiated_token_binding_param(const SSL *ssl) {
Robert Sloan15c0b352018-04-16 08:36:46 -07002147 return ssl->s3->negotiated_token_binding_param;
Robert Sloan978112c2018-01-22 12:53:01 -08002148}
2149
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002150size_t SSL_get0_certificate_types(const SSL *ssl, const uint8_t **out_types) {
2151 Span<const uint8_t> types;
2152 if (!ssl->server && ssl->s3->hs != nullptr) {
2153 types = ssl->s3->hs->certificate_types;
David Benjamin6e899c72016-06-09 18:02:18 -04002154 }
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002155 *out_types = types.data();
2156 return types.size();
2157}
2158
2159size_t SSL_get0_peer_verify_algorithms(const SSL *ssl,
2160 const uint16_t **out_sigalgs) {
2161 Span<const uint16_t> sigalgs;
2162 if (ssl->s3->hs != nullptr) {
2163 sigalgs = ssl->s3->hs->peer_sigalgs;
2164 }
2165 *out_sigalgs = sigalgs.data();
2166 return sigalgs.size();
David Benjamin6e899c72016-06-09 18:02:18 -04002167}
2168
Adam Langley4139edb2016-01-13 15:00:54 -08002169EVP_PKEY *SSL_get_privatekey(const SSL *ssl) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002170 if (!ssl->config) {
2171 assert(ssl->config);
2172 return NULL;
2173 }
2174 if (ssl->config->cert != NULL) {
2175 return ssl->config->cert->privatekey.get();
Adam Langleyd9e397b2015-01-22 14:27:53 -08002176 }
2177
2178 return NULL;
2179}
2180
Adam Langleyd9e397b2015-01-22 14:27:53 -08002181EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
2182 if (ctx->cert != NULL) {
Robert Sloan5cbb5c82018-04-24 11:35:46 -07002183 return ctx->cert->privatekey.get();
Adam Langleyd9e397b2015-01-22 14:27:53 -08002184 }
2185
2186 return NULL;
2187}
2188
Kenny Rootb8494592015-09-25 02:29:14 +00002189const SSL_CIPHER *SSL_get_current_cipher(const SSL *ssl) {
Robert Sloanb6d070c2017-07-24 08:40:01 -07002190 return ssl->s3->aead_write_ctx->cipher();
Adam Langleyd9e397b2015-01-22 14:27:53 -08002191}
2192
David Benjamin6e899c72016-06-09 18:02:18 -04002193int SSL_session_reused(const SSL *ssl) {
Robert Sloane56da3e2017-06-26 08:26:42 -07002194 return ssl->s3->session_reused || SSL_in_early_data(ssl);
David Benjamin6e899c72016-06-09 18:02:18 -04002195}
2196
Adam Langley4139edb2016-01-13 15:00:54 -08002197const COMP_METHOD *SSL_get_current_compression(SSL *ssl) { return NULL; }
Adam Langleyd9e397b2015-01-22 14:27:53 -08002198
Adam Langley4139edb2016-01-13 15:00:54 -08002199const COMP_METHOD *SSL_get_current_expansion(SSL *ssl) { return NULL; }
Adam Langleyd9e397b2015-01-22 14:27:53 -08002200
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002201int SSL_get_server_tmp_key(SSL *ssl, EVP_PKEY **out_key) { return 0; }
David Benjamin4969cc92016-04-22 15:02:23 -04002202
Adam Langleyd9e397b2015-01-22 14:27:53 -08002203void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
Adam Langleyfad63272015-11-12 12:15:39 -08002204 ctx->quiet_shutdown = (mode != 0);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002205}
2206
2207int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
2208 return ctx->quiet_shutdown;
2209}
2210
Adam Langleyfad63272015-11-12 12:15:39 -08002211void SSL_set_quiet_shutdown(SSL *ssl, int mode) {
2212 ssl->quiet_shutdown = (mode != 0);
2213}
Adam Langleyd9e397b2015-01-22 14:27:53 -08002214
Kenny Roote99801b2015-11-06 15:31:15 -08002215int SSL_get_quiet_shutdown(const SSL *ssl) { return ssl->quiet_shutdown; }
Adam Langleyd9e397b2015-01-22 14:27:53 -08002216
Kenny Roote99801b2015-11-06 15:31:15 -08002217void SSL_set_shutdown(SSL *ssl, int mode) {
Robert Sloana27a6a42017-09-05 08:39:28 -07002218 // It is an error to clear any bits that have already been set. (We can't try
2219 // to get a second close_notify or send two.)
David Benjamind316cba2016-06-02 16:17:39 -04002220 assert((SSL_get_shutdown(ssl) & mode) == SSL_get_shutdown(ssl));
Adam Langleyd9e397b2015-01-22 14:27:53 -08002221
David Benjamind316cba2016-06-02 16:17:39 -04002222 if (mode & SSL_RECEIVED_SHUTDOWN &&
Robert Sloan921ef2c2017-10-17 09:02:20 -07002223 ssl->s3->read_shutdown == ssl_shutdown_none) {
2224 ssl->s3->read_shutdown = ssl_shutdown_close_notify;
David Benjamind316cba2016-06-02 16:17:39 -04002225 }
2226
2227 if (mode & SSL_SENT_SHUTDOWN &&
Robert Sloan921ef2c2017-10-17 09:02:20 -07002228 ssl->s3->write_shutdown == ssl_shutdown_none) {
2229 ssl->s3->write_shutdown = ssl_shutdown_close_notify;
David Benjamind316cba2016-06-02 16:17:39 -04002230 }
Kenny Roote99801b2015-11-06 15:31:15 -08002231}
Adam Langleyd9e397b2015-01-22 14:27:53 -08002232
David Benjamind316cba2016-06-02 16:17:39 -04002233int SSL_get_shutdown(const SSL *ssl) {
2234 int ret = 0;
Robert Sloan921ef2c2017-10-17 09:02:20 -07002235 if (ssl->s3->read_shutdown != ssl_shutdown_none) {
Robert Sloana27a6a42017-09-05 08:39:28 -07002236 // Historically, OpenSSL set |SSL_RECEIVED_SHUTDOWN| on both close_notify
2237 // and fatal alert.
David Benjamind316cba2016-06-02 16:17:39 -04002238 ret |= SSL_RECEIVED_SHUTDOWN;
2239 }
Robert Sloan921ef2c2017-10-17 09:02:20 -07002240 if (ssl->s3->write_shutdown == ssl_shutdown_close_notify) {
Robert Sloana27a6a42017-09-05 08:39:28 -07002241 // Historically, OpenSSL set |SSL_SENT_SHUTDOWN| on only close_notify.
David Benjamind316cba2016-06-02 16:17:39 -04002242 ret |= SSL_SENT_SHUTDOWN;
2243 }
2244 return ret;
2245}
Kenny Roote99801b2015-11-06 15:31:15 -08002246
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002247SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx.get(); }
Adam Langleyd9e397b2015-01-22 14:27:53 -08002248
2249SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002250 if (!ssl->config) {
2251 return NULL;
2252 }
2253 if (ssl->ctx.get() == ctx) {
2254 return ssl->ctx.get();
Adam Langleyd9e397b2015-01-22 14:27:53 -08002255 }
2256
Robert Sloana27a6a42017-09-05 08:39:28 -07002257 // One cannot change the X.509 callbacks during a connection.
Robert Sloan5d625782017-02-13 09:55:39 -08002258 if (ssl->ctx->x509_method != ctx->x509_method) {
2259 assert(0);
2260 return NULL;
2261 }
2262
Adam Langleyd9e397b2015-01-22 14:27:53 -08002263 if (ctx == NULL) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002264 ctx = ssl->session_ctx.get();
Adam Langleyd9e397b2015-01-22 14:27:53 -08002265 }
2266
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002267 UniquePtr<CERT> new_cert = ssl_cert_dup(ctx->cert.get());
2268 if (!new_cert) {
2269 return nullptr;
2270 }
Adam Langleye9ada862015-05-11 17:20:37 -07002271
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002272 ssl->config->cert = std::move(new_cert);
2273 ssl->ctx = UpRef(ctx);
2274 ssl->enable_early_data = ssl->ctx->enable_early_data;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002275
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002276 return ssl->ctx.get();
Adam Langleyd9e397b2015-01-22 14:27:53 -08002277}
2278
Adam Langleyd9e397b2015-01-22 14:27:53 -08002279void SSL_set_info_callback(SSL *ssl,
Kenny Roote99801b2015-11-06 15:31:15 -08002280 void (*cb)(const SSL *ssl, int type, int value)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08002281 ssl->info_callback = cb;
2282}
2283
Kenny Roote99801b2015-11-06 15:31:15 -08002284void (*SSL_get_info_callback(const SSL *ssl))(const SSL *ssl, int type,
2285 int value) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08002286 return ssl->info_callback;
2287}
2288
Steven Valdeze7531f02016-12-14 13:29:57 -05002289int SSL_state(const SSL *ssl) {
Robert Sloana94fe052017-02-21 08:49:28 -08002290 return SSL_in_init(ssl) ? SSL_ST_INIT : SSL_ST_OK;
Steven Valdeze7531f02016-12-14 13:29:57 -05002291}
Adam Langleyd9e397b2015-01-22 14:27:53 -08002292
Adam Langleyf4e42722015-06-04 17:45:09 -07002293void SSL_set_state(SSL *ssl, int state) { }
Adam Langleyd9e397b2015-01-22 14:27:53 -08002294
David Benjamin4969cc92016-04-22 15:02:23 -04002295char *SSL_get_shared_ciphers(const SSL *ssl, char *buf, int len) {
2296 if (len <= 0) {
2297 return NULL;
2298 }
2299 buf[0] = '\0';
2300 return buf;
2301}
2302
Adam Langley4139edb2016-01-13 15:00:54 -08002303int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
Robert Sloan8ff03552017-06-14 12:40:58 -07002304 CRYPTO_EX_dup *dup_unused, CRYPTO_EX_free *free_func) {
Adam Langleye9ada862015-05-11 17:20:37 -07002305 int index;
2306 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp,
Robert Sloan8ff03552017-06-14 12:40:58 -07002307 free_func)) {
Adam Langleye9ada862015-05-11 17:20:37 -07002308 return -1;
2309 }
2310 return index;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002311}
2312
Robert Sloanfe7cd212017-08-07 09:03:39 -07002313int SSL_set_ex_data(SSL *ssl, int idx, void *data) {
2314 return CRYPTO_set_ex_data(&ssl->ex_data, idx, data);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002315}
2316
Kenny Rootb8494592015-09-25 02:29:14 +00002317void *SSL_get_ex_data(const SSL *ssl, int idx) {
2318 return CRYPTO_get_ex_data(&ssl->ex_data, idx);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002319}
2320
Adam Langley4139edb2016-01-13 15:00:54 -08002321int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
Robert Sloan8ff03552017-06-14 12:40:58 -07002322 CRYPTO_EX_dup *dup_unused,
Adam Langleyd9e397b2015-01-22 14:27:53 -08002323 CRYPTO_EX_free *free_func) {
Adam Langleye9ada862015-05-11 17:20:37 -07002324 int index;
2325 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp,
Robert Sloan8ff03552017-06-14 12:40:58 -07002326 free_func)) {
Adam Langleye9ada862015-05-11 17:20:37 -07002327 return -1;
2328 }
2329 return index;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002330}
2331
Robert Sloanfe7cd212017-08-07 09:03:39 -07002332int SSL_CTX_set_ex_data(SSL_CTX *ctx, int idx, void *data) {
2333 return CRYPTO_set_ex_data(&ctx->ex_data, idx, data);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002334}
2335
Kenny Rootb8494592015-09-25 02:29:14 +00002336void *SSL_CTX_get_ex_data(const SSL_CTX *ctx, int idx) {
2337 return CRYPTO_get_ex_data(&ctx->ex_data, idx);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002338}
2339
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002340int SSL_want(const SSL *ssl) { return ssl->s3->rwstate; }
Adam Langleyd9e397b2015-01-22 14:27:53 -08002341
2342void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
2343 RSA *(*cb)(SSL *ssl, int is_export,
Robert Sloanfe7cd212017-08-07 09:03:39 -07002344 int keylength)) {}
Adam Langleyd9e397b2015-01-22 14:27:53 -08002345
2346void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
Robert Sloanfe7cd212017-08-07 09:03:39 -07002347 int keylength)) {}
Adam Langleyd9e397b2015-01-22 14:27:53 -08002348
2349void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
Robert Sloanfe7cd212017-08-07 09:03:39 -07002350 DH *(*cb)(SSL *ssl, int is_export,
2351 int keylength)) {}
Adam Langleyd9e397b2015-01-22 14:27:53 -08002352
Robert Sloanfe7cd212017-08-07 09:03:39 -07002353void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*cb)(SSL *ssl, int is_export,
2354 int keylength)) {}
Adam Langleyd9e397b2015-01-22 14:27:53 -08002355
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002356static int use_psk_identity_hint(UniquePtr<char> *out,
2357 const char *identity_hint) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08002358 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
Kenny Rootb8494592015-09-25 02:29:14 +00002359 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002360 return 0;
2361 }
2362
Robert Sloana27a6a42017-09-05 08:39:28 -07002363 // Clear currently configured hint, if any.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002364 out->reset();
Adam Langleyd9e397b2015-01-22 14:27:53 -08002365
Robert Sloana27a6a42017-09-05 08:39:28 -07002366 // Treat the empty hint as not supplying one. Plain PSK makes it possible to
2367 // send either no hint (omit ServerKeyExchange) or an empty hint, while
2368 // ECDHE_PSK can only spell empty hint. Having different capabilities is odd,
2369 // so we interpret empty and missing as identical.
David Benjamin7c0d06c2016-08-11 13:26:41 -04002370 if (identity_hint != NULL && identity_hint[0] != '\0') {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002371 out->reset(BUF_strdup(identity_hint));
2372 if (*out == nullptr) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08002373 return 0;
2374 }
2375 }
2376
2377 return 1;
2378}
2379
Robert Sloane56da3e2017-06-26 08:26:42 -07002380int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
2381 return use_psk_identity_hint(&ctx->psk_identity_hint, identity_hint);
2382}
2383
2384int SSL_use_psk_identity_hint(SSL *ssl, const char *identity_hint) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002385 if (!ssl->config) {
2386 return 0;
2387 }
2388 return use_psk_identity_hint(&ssl->config->psk_identity_hint, identity_hint);
Robert Sloane56da3e2017-06-26 08:26:42 -07002389}
2390
Kenny Rootb8494592015-09-25 02:29:14 +00002391const char *SSL_get_psk_identity_hint(const SSL *ssl) {
2392 if (ssl == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08002393 return NULL;
2394 }
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002395 if (ssl->config == NULL) {
2396 assert(ssl->config);
2397 return NULL;
2398 }
2399 return ssl->config->psk_identity_hint.get();
Adam Langleyd9e397b2015-01-22 14:27:53 -08002400}
2401
Kenny Rootb8494592015-09-25 02:29:14 +00002402const char *SSL_get_psk_identity(const SSL *ssl) {
David Benjaminc895d6b2016-08-11 13:26:41 -04002403 if (ssl == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08002404 return NULL;
2405 }
David Benjaminc895d6b2016-08-11 13:26:41 -04002406 SSL_SESSION *session = SSL_get_session(ssl);
2407 if (session == NULL) {
2408 return NULL;
2409 }
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002410 return session->psk_identity.get();
Adam Langleyd9e397b2015-01-22 14:27:53 -08002411}
2412
2413void SSL_set_psk_client_callback(
Kenny Rootb8494592015-09-25 02:29:14 +00002414 SSL *ssl, unsigned (*cb)(SSL *ssl, const char *hint, char *identity,
2415 unsigned max_identity_len, uint8_t *psk,
2416 unsigned max_psk_len)) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002417 if (!ssl->config) {
2418 return;
2419 }
2420 ssl->config->psk_client_callback = cb;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002421}
2422
2423void SSL_CTX_set_psk_client_callback(
Kenny Rootb8494592015-09-25 02:29:14 +00002424 SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *hint, char *identity,
2425 unsigned max_identity_len, uint8_t *psk,
2426 unsigned max_psk_len)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08002427 ctx->psk_client_callback = cb;
2428}
2429
2430void SSL_set_psk_server_callback(
Kenny Rootb8494592015-09-25 02:29:14 +00002431 SSL *ssl, unsigned (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
2432 unsigned max_psk_len)) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002433 if (!ssl->config) {
2434 return;
2435 }
2436 ssl->config->psk_server_callback = cb;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002437}
2438
2439void SSL_CTX_set_psk_server_callback(
Kenny Rootb8494592015-09-25 02:29:14 +00002440 SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *identity,
2441 uint8_t *psk, unsigned max_psk_len)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08002442 ctx->psk_server_callback = cb;
2443}
2444
Robert Sloan0db7f542018-01-16 15:48:33 -08002445int SSL_set_dummy_pq_padding_size(SSL *ssl, size_t num_bytes) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002446 if (!ssl->config) {
2447 return 0;
2448 }
Robert Sloan0db7f542018-01-16 15:48:33 -08002449 if (num_bytes > 0xffff) {
2450 return 0;
2451 }
2452
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002453 ssl->config->dummy_pq_padding_len = num_bytes;
Robert Sloan0db7f542018-01-16 15:48:33 -08002454 return 1;
2455}
2456
Robert Sloanab8b8882018-03-26 11:39:51 -07002457int SSL_dummy_pq_padding_used(SSL *ssl) {
2458 if (ssl->server) {
2459 return 0;
2460 }
2461
2462 return ssl->did_dummy_pq_padding;
2463}
2464
Adam Langleyd9e397b2015-01-22 14:27:53 -08002465void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
2466 void (*cb)(int write_p, int version,
2467 int content_type, const void *buf,
2468 size_t len, SSL *ssl, void *arg)) {
Adam Langleye9ada862015-05-11 17:20:37 -07002469 ctx->msg_callback = cb;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002470}
Adam Langleye9ada862015-05-11 17:20:37 -07002471
2472void SSL_CTX_set_msg_callback_arg(SSL_CTX *ctx, void *arg) {
2473 ctx->msg_callback_arg = arg;
2474}
2475
Adam Langleyd9e397b2015-01-22 14:27:53 -08002476void SSL_set_msg_callback(SSL *ssl,
2477 void (*cb)(int write_p, int version, int content_type,
2478 const void *buf, size_t len, SSL *ssl,
2479 void *arg)) {
Adam Langleye9ada862015-05-11 17:20:37 -07002480 ssl->msg_callback = cb;
2481}
2482
2483void SSL_set_msg_callback_arg(SSL *ssl, void *arg) {
2484 ssl->msg_callback_arg = arg;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002485}
2486
Adam Langley4139edb2016-01-13 15:00:54 -08002487void SSL_CTX_set_keylog_callback(SSL_CTX *ctx,
2488 void (*cb)(const SSL *ssl, const char *line)) {
2489 ctx->keylog_callback = cb;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002490}
2491
David Benjamin7c0d06c2016-08-11 13:26:41 -04002492void (*SSL_CTX_get_keylog_callback(const SSL_CTX *ctx))(const SSL *ssl,
2493 const char *line) {
2494 return ctx->keylog_callback;
2495}
2496
David Benjamin6e899c72016-06-09 18:02:18 -04002497void SSL_CTX_set_current_time_cb(SSL_CTX *ctx,
2498 void (*cb)(const SSL *ssl,
2499 struct timeval *out_clock)) {
2500 ctx->current_time_cb = cb;
2501}
2502
Kenny Roote99801b2015-11-06 15:31:15 -08002503int SSL_is_init_finished(const SSL *ssl) {
Robert Sloana94fe052017-02-21 08:49:28 -08002504 return !SSL_in_init(ssl);
Kenny Roote99801b2015-11-06 15:31:15 -08002505}
2506
2507int SSL_in_init(const SSL *ssl) {
Robert Sloana27a6a42017-09-05 08:39:28 -07002508 // This returns false once all the handshake state has been finalized, to
2509 // allow callbacks and getters based on SSL_in_init to return the correct
2510 // values.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002511 SSL_HANDSHAKE *hs = ssl->s3->hs.get();
Robert Sloana27a6a42017-09-05 08:39:28 -07002512 return hs != nullptr && !hs->handshake_finalized;
Kenny Roote99801b2015-11-06 15:31:15 -08002513}
2514
2515int SSL_in_false_start(const SSL *ssl) {
David Benjamin95add822016-10-19 01:09:12 -04002516 if (ssl->s3->hs == NULL) {
2517 return 0;
2518 }
2519 return ssl->s3->hs->in_false_start;
Adam Langleye9ada862015-05-11 17:20:37 -07002520}
2521
Adam Langley4139edb2016-01-13 15:00:54 -08002522int SSL_cutthrough_complete(const SSL *ssl) {
2523 return SSL_in_false_start(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002524}
2525
2526void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
2527 size_t *ssl_session_size) {
2528 *ssl_size = sizeof(SSL);
2529 *ssl_ctx_size = sizeof(SSL_CTX);
2530 *ssl_session_size = sizeof(SSL_SESSION);
2531}
2532
David Benjaminc895d6b2016-08-11 13:26:41 -04002533int SSL_is_server(const SSL *ssl) { return ssl->server; }
2534
2535int SSL_is_dtls(const SSL *ssl) { return ssl->method->is_dtls; }
Adam Langleyd9e397b2015-01-22 14:27:53 -08002536
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002537void SSL_CTX_set_select_certificate_cb(
2538 SSL_CTX *ctx,
2539 enum ssl_select_cert_result_t (*cb)(const SSL_CLIENT_HELLO *)) {
Kenny Roote99801b2015-11-06 15:31:15 -08002540 ctx->select_certificate_cb = cb;
2541}
2542
David Benjamin1b249672016-12-06 18:25:50 -05002543void SSL_CTX_set_dos_protection_cb(SSL_CTX *ctx,
2544 int (*cb)(const SSL_CLIENT_HELLO *)) {
Adam Langleye9ada862015-05-11 17:20:37 -07002545 ctx->dos_protection_cb = cb;
2546}
2547
Kenny Roote99801b2015-11-06 15:31:15 -08002548void SSL_set_renegotiate_mode(SSL *ssl, enum ssl_renegotiate_mode_t mode) {
2549 ssl->renegotiate_mode = mode;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002550
2551 // Check if |ssl_can_renegotiate| has changed and the configuration may now be
2552 // shed. HTTP clients may initially allow renegotiation for HTTP/1.1, and then
2553 // disable after the handshake once the ALPN protocol is known to be HTTP/2.
2554 ssl_maybe_shed_handshake_config(ssl);
Kenny Roote99801b2015-11-06 15:31:15 -08002555}
2556
Adam Langleyfad63272015-11-12 12:15:39 -08002557int SSL_get_ivs(const SSL *ssl, const uint8_t **out_read_iv,
2558 const uint8_t **out_write_iv, size_t *out_iv_len) {
Adam Langleyfad63272015-11-12 12:15:39 -08002559 size_t write_iv_len;
Robert Sloanb6d070c2017-07-24 08:40:01 -07002560 if (!ssl->s3->aead_read_ctx->GetIV(out_read_iv, out_iv_len) ||
2561 !ssl->s3->aead_write_ctx->GetIV(out_write_iv, &write_iv_len) ||
Adam Langleyfad63272015-11-12 12:15:39 -08002562 *out_iv_len != write_iv_len) {
2563 return 0;
2564 }
2565
2566 return 1;
2567}
2568
David Benjamin4969cc92016-04-22 15:02:23 -04002569static uint64_t be_to_u64(const uint8_t in[8]) {
2570 return (((uint64_t)in[0]) << 56) | (((uint64_t)in[1]) << 48) |
2571 (((uint64_t)in[2]) << 40) | (((uint64_t)in[3]) << 32) |
2572 (((uint64_t)in[4]) << 24) | (((uint64_t)in[5]) << 16) |
2573 (((uint64_t)in[6]) << 8) | ((uint64_t)in[7]);
2574}
2575
2576uint64_t SSL_get_read_sequence(const SSL *ssl) {
Robert Sloana27a6a42017-09-05 08:39:28 -07002577 // TODO(davidben): Internally represent sequence numbers as uint64_t.
David Benjaminc895d6b2016-08-11 13:26:41 -04002578 if (SSL_is_dtls(ssl)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07002579 // max_seq_num already includes the epoch.
David Benjamin4969cc92016-04-22 15:02:23 -04002580 assert(ssl->d1->r_epoch == (ssl->d1->bitmap.max_seq_num >> 48));
2581 return ssl->d1->bitmap.max_seq_num;
2582 }
2583 return be_to_u64(ssl->s3->read_sequence);
2584}
2585
2586uint64_t SSL_get_write_sequence(const SSL *ssl) {
2587 uint64_t ret = be_to_u64(ssl->s3->write_sequence);
David Benjaminc895d6b2016-08-11 13:26:41 -04002588 if (SSL_is_dtls(ssl)) {
David Benjamin4969cc92016-04-22 15:02:23 -04002589 assert((ret >> 48) == 0);
2590 ret |= ((uint64_t)ssl->d1->w_epoch) << 48;
2591 }
2592 return ret;
2593}
2594
David Benjaminc895d6b2016-08-11 13:26:41 -04002595uint16_t SSL_get_peer_signature_algorithm(const SSL *ssl) {
Robert Sloana27a6a42017-09-05 08:39:28 -07002596 // TODO(davidben): This checks the wrong session if there is a renegotiation
2597 // in progress.
Steven Valdeze7531f02016-12-14 13:29:57 -05002598 SSL_SESSION *session = SSL_get_session(ssl);
2599 if (session == NULL) {
2600 return 0;
2601 }
2602
2603 return session->peer_signature_algorithm;
Adam Langleyfad63272015-11-12 12:15:39 -08002604}
2605
David Benjamin4969cc92016-04-22 15:02:23 -04002606size_t SSL_get_client_random(const SSL *ssl, uint8_t *out, size_t max_out) {
2607 if (max_out == 0) {
2608 return sizeof(ssl->s3->client_random);
2609 }
2610 if (max_out > sizeof(ssl->s3->client_random)) {
2611 max_out = sizeof(ssl->s3->client_random);
2612 }
Robert Sloan69939df2017-01-09 10:53:07 -08002613 OPENSSL_memcpy(out, ssl->s3->client_random, max_out);
David Benjamin4969cc92016-04-22 15:02:23 -04002614 return max_out;
2615}
2616
2617size_t SSL_get_server_random(const SSL *ssl, uint8_t *out, size_t max_out) {
2618 if (max_out == 0) {
2619 return sizeof(ssl->s3->server_random);
2620 }
2621 if (max_out > sizeof(ssl->s3->server_random)) {
2622 max_out = sizeof(ssl->s3->server_random);
2623 }
Robert Sloan69939df2017-01-09 10:53:07 -08002624 OPENSSL_memcpy(out, ssl->s3->server_random, max_out);
David Benjamin4969cc92016-04-22 15:02:23 -04002625 return max_out;
2626}
2627
2628const SSL_CIPHER *SSL_get_pending_cipher(const SSL *ssl) {
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002629 SSL_HANDSHAKE *hs = ssl->s3->hs.get();
Robert Sloana94fe052017-02-21 08:49:28 -08002630 if (hs == NULL) {
David Benjamin4969cc92016-04-22 15:02:23 -04002631 return NULL;
2632 }
Robert Sloana94fe052017-02-21 08:49:28 -08002633 return hs->new_cipher;
David Benjamin4969cc92016-04-22 15:02:23 -04002634}
2635
Steven Valdez909b19f2016-11-21 15:35:44 -05002636void SSL_set_retain_only_sha256_of_client_certs(SSL *ssl, int enabled) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002637 if (!ssl->config) {
2638 return;
2639 }
2640 ssl->config->retain_only_sha256_of_client_certs = !!enabled;
Steven Valdez909b19f2016-11-21 15:35:44 -05002641}
2642
David Benjamin4969cc92016-04-22 15:02:23 -04002643void SSL_CTX_set_retain_only_sha256_of_client_certs(SSL_CTX *ctx, int enabled) {
2644 ctx->retain_only_sha256_of_client_certs = !!enabled;
2645}
2646
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002647void SSL_CTX_set_grease_enabled(SSL_CTX *ctx, int enabled) {
2648 ctx->grease_enabled = !!enabled;
2649}
2650
Robert Sloan1c9db532017-03-13 08:03:59 -07002651int32_t SSL_get_ticket_age_skew(const SSL *ssl) {
2652 return ssl->s3->ticket_age_skew;
2653}
2654
Robert Sloancd79cde2017-12-11 09:06:12 -08002655void SSL_CTX_set_false_start_allowed_without_alpn(SSL_CTX *ctx, int allowed) {
2656 ctx->false_start_allowed_without_alpn = !!allowed;
2657}
2658
Robert Sloan0da43952018-01-03 15:13:14 -08002659int SSL_is_draft_downgrade(const SSL *ssl) { return ssl->s3->draft_downgrade; }
2660
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002661void SSL_set_shed_handshake_config(SSL *ssl, int enable) {
2662 if (!ssl->config) {
2663 return;
2664 }
2665 ssl->config->shed_handshake_config = !!enable;
2666}
2667
Kenny Rootb8494592015-09-25 02:29:14 +00002668int SSL_clear(SSL *ssl) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002669 if (!ssl->config) {
2670 return 0; // SSL_clear may not be used after shedding config.
2671 }
2672
Robert Sloana27a6a42017-09-05 08:39:28 -07002673 // In OpenSSL, reusing a client |SSL| with |SSL_clear| causes the previously
2674 // established session to be offered the next time around. wpa_supplicant
2675 // depends on this behavior, so emulate it.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002676 UniquePtr<SSL_SESSION> session;
David Benjamin1b249672016-12-06 18:25:50 -05002677 if (!ssl->server && ssl->s3->established_session != NULL) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002678 session = UpRef(ssl->s3->established_session);
Kenny Rootb8494592015-09-25 02:29:14 +00002679 }
2680
Robert Sloana27a6a42017-09-05 08:39:28 -07002681 // The ssl->d1->mtu is simultaneously configuration (preserved across
2682 // clear) and connection-specific state (gets reset).
2683 //
2684 // TODO(davidben): Avoid this.
Kenny Rootb8494592015-09-25 02:29:14 +00002685 unsigned mtu = 0;
2686 if (ssl->d1 != NULL) {
2687 mtu = ssl->d1->mtu;
Adam Langleyf4e42722015-06-04 17:45:09 -07002688 }
2689
Kenny Rootb8494592015-09-25 02:29:14 +00002690 ssl->method->ssl_free(ssl);
2691 if (!ssl->method->ssl_new(ssl)) {
2692 return 0;
2693 }
Kenny Rootb8494592015-09-25 02:29:14 +00002694
David Benjaminc895d6b2016-08-11 13:26:41 -04002695 if (SSL_is_dtls(ssl) && (SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
Kenny Rootb8494592015-09-25 02:29:14 +00002696 ssl->d1->mtu = mtu;
Adam Langleyf4e42722015-06-04 17:45:09 -07002697 }
2698
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002699 if (session != nullptr) {
2700 SSL_set_session(ssl, session.get());
David Benjamin1b249672016-12-06 18:25:50 -05002701 }
Kenny Rootb8494592015-09-25 02:29:14 +00002702
Adam Langleyf4e42722015-06-04 17:45:09 -07002703 return 1;
Adam Langley56d25032015-06-23 16:20:13 -07002704}
2705
Adam Langleye9ada862015-05-11 17:20:37 -07002706int SSL_CTX_sess_connect(const SSL_CTX *ctx) { return 0; }
2707int SSL_CTX_sess_connect_good(const SSL_CTX *ctx) { return 0; }
2708int SSL_CTX_sess_connect_renegotiate(const SSL_CTX *ctx) { return 0; }
2709int SSL_CTX_sess_accept(const SSL_CTX *ctx) { return 0; }
2710int SSL_CTX_sess_accept_renegotiate(const SSL_CTX *ctx) { return 0; }
2711int SSL_CTX_sess_accept_good(const SSL_CTX *ctx) { return 0; }
2712int SSL_CTX_sess_hits(const SSL_CTX *ctx) { return 0; }
2713int SSL_CTX_sess_cb_hits(const SSL_CTX *ctx) { return 0; }
2714int SSL_CTX_sess_misses(const SSL_CTX *ctx) { return 0; }
2715int SSL_CTX_sess_timeouts(const SSL_CTX *ctx) { return 0; }
2716int SSL_CTX_sess_cache_full(const SSL_CTX *ctx) { return 0; }
David Benjamin6e899c72016-06-09 18:02:18 -04002717
2718int SSL_num_renegotiations(const SSL *ssl) {
2719 return SSL_total_renegotiations(ssl);
2720}
2721
2722int SSL_CTX_need_tmp_RSA(const SSL_CTX *ctx) { return 0; }
2723int SSL_need_tmp_RSA(const SSL *ssl) { return 0; }
2724int SSL_CTX_set_tmp_rsa(SSL_CTX *ctx, const RSA *rsa) { return 1; }
2725int SSL_set_tmp_rsa(SSL *ssl, const RSA *rsa) { return 1; }
Kenny Rootb8494592015-09-25 02:29:14 +00002726void ERR_load_SSL_strings(void) {}
2727void SSL_load_error_strings(void) {}
David Benjamin6e899c72016-06-09 18:02:18 -04002728int SSL_cache_hit(SSL *ssl) { return SSL_session_reused(ssl); }
2729
2730int SSL_CTX_set_tmp_ecdh(SSL_CTX *ctx, const EC_KEY *ec_key) {
2731 if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) {
2732 OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
2733 return 0;
2734 }
2735 int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key));
2736 return SSL_CTX_set1_curves(ctx, &nid, 1);
2737}
2738
2739int SSL_set_tmp_ecdh(SSL *ssl, const EC_KEY *ec_key) {
2740 if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) {
2741 OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
2742 return 0;
2743 }
2744 int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key));
2745 return SSL_set1_curves(ssl, &nid, 1);
2746}
David Benjaminc895d6b2016-08-11 13:26:41 -04002747
Robert Sloan1c9db532017-03-13 08:03:59 -07002748void SSL_CTX_set_ticket_aead_method(SSL_CTX *ctx,
2749 const SSL_TICKET_AEAD_METHOD *aead_method) {
2750 ctx->ticket_aead_method = aead_method;
2751}
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002752
2753int SSL_set_tlsext_status_type(SSL *ssl, int type) {
2754 if (!ssl->config) {
2755 return 0;
2756 }
2757 ssl->config->ocsp_stapling_enabled = type == TLSEXT_STATUSTYPE_ocsp;
2758 return 1;
2759}
2760
2761int SSL_set_tlsext_status_ocsp_resp(SSL *ssl, uint8_t *resp, size_t resp_len) {
2762 if (SSL_set_ocsp_response(ssl, resp, resp_len)) {
2763 OPENSSL_free(resp);
2764 return 1;
2765 }
2766 return 0;
2767}
2768
2769size_t SSL_get_tlsext_status_ocsp_resp(const SSL *ssl, const uint8_t **out) {
2770 size_t ret;
2771 SSL_get0_ocsp_response(ssl, out, &ret);
2772 return ret;
2773}
2774
2775int SSL_CTX_set_tlsext_status_cb(SSL_CTX *ctx,
2776 int (*callback)(SSL *ssl, void *arg)) {
2777 ctx->legacy_ocsp_callback = callback;
2778 return 1;
2779}
2780
2781int SSL_CTX_set_tlsext_status_arg(SSL_CTX *ctx, void *arg) {
2782 ctx->legacy_ocsp_callback_arg = arg;
2783 return 1;
2784}