blob: 78a1860d2b4f9f7eb01171da9fa091f2c4b8ee65 [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
Robert Sloanb6d070c2017-07-24 08:40:01 -0700222int ssl_can_write(const SSL *ssl) {
223 return !SSL_in_init(ssl) || ssl->s3->hs->can_early_write;
224}
225
226int ssl_can_read(const SSL *ssl) {
227 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;
277 SSL_CTX *ctx = ssl->session_ctx;
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.
Robert Sloan29c1d2c2017-10-30 14:10:28 -0700292 if (ssl->s3->established_session.get() != ssl->session ||
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) {
Robert Sloan29c1d2c2017-10-30 14:10:28 -0700298 SSL_SESSION_up_ref(ssl->s3->established_session.get());
299 if (!ctx->new_session_cb(ssl, ssl->s3->established_session.get())) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700300 // |new_session_cb|'s return value signals whether it took ownership.
Robert Sloan29c1d2c2017-10-30 14:10:28 -0700301 SSL_SESSION_free(ssl->s3->established_session.get());
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.
Robert Sloan8f860b12017-08-28 07:37:06 -0700409 ssl_ctx_get_current_time(ssl->ctx, out_clock);
410}
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
Robert Sloanb6d070c2017-07-24 08:40:01 -0700462} // namespace bssl
463
464using namespace bssl;
465
Kenny Rootb8494592015-09-25 02:29:14 +0000466int SSL_library_init(void) {
467 CRYPTO_library_init();
Adam Langleyd9e397b2015-01-22 14:27:53 -0800468 return 1;
469}
470
Robert Sloan4562e9d2017-10-02 10:26:51 -0700471int OPENSSL_init_ssl(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings) {
472 CRYPTO_library_init();
473 return 1;
474}
475
David Benjamin4969cc92016-04-22 15:02:23 -0400476static uint32_t ssl_session_hash(const SSL_SESSION *sess) {
477 const uint8_t *session_id = sess->session_id;
478
479 uint8_t tmp_storage[sizeof(uint32_t)];
480 if (sess->session_id_length < sizeof(tmp_storage)) {
Robert Sloan69939df2017-01-09 10:53:07 -0800481 OPENSSL_memset(tmp_storage, 0, sizeof(tmp_storage));
482 OPENSSL_memcpy(tmp_storage, sess->session_id, sess->session_id_length);
David Benjamin4969cc92016-04-22 15:02:23 -0400483 session_id = tmp_storage;
484 }
485
Kenny Rootb8494592015-09-25 02:29:14 +0000486 uint32_t hash =
David Benjamin4969cc92016-04-22 15:02:23 -0400487 ((uint32_t)session_id[0]) |
488 ((uint32_t)session_id[1] << 8) |
489 ((uint32_t)session_id[2] << 16) |
490 ((uint32_t)session_id[3] << 24);
Kenny Rootb8494592015-09-25 02:29:14 +0000491
492 return hash;
493}
494
Robert Sloana27a6a42017-09-05 08:39:28 -0700495// NB: If this function (or indeed the hash function which uses a sort of
496// coarser function than this one) is changed, ensure
497// SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
498// able to construct an SSL_SESSION that will collide with any existing session
499// with a matching session ID.
Kenny Rootb8494592015-09-25 02:29:14 +0000500static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
501 if (a->ssl_version != b->ssl_version) {
502 return 1;
503 }
504
505 if (a->session_id_length != b->session_id_length) {
506 return 1;
507 }
508
Robert Sloan69939df2017-01-09 10:53:07 -0800509 return OPENSSL_memcmp(a->session_id, b->session_id, a->session_id_length);
Kenny Rootb8494592015-09-25 02:29:14 +0000510}
511
512SSL_CTX *SSL_CTX_new(const SSL_METHOD *method) {
513 SSL_CTX *ret = NULL;
514
515 if (method == NULL) {
516 OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_METHOD_PASSED);
517 return NULL;
518 }
519
Robert Sloana12bf462017-07-17 07:08:26 -0700520 ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
Kenny Rootb8494592015-09-25 02:29:14 +0000521 if (ret == NULL) {
522 goto err;
523 }
524
Robert Sloan69939df2017-01-09 10:53:07 -0800525 OPENSSL_memset(ret, 0, sizeof(SSL_CTX));
Kenny Rootb8494592015-09-25 02:29:14 +0000526
527 ret->method = method->method;
Robert Sloan5d625782017-02-13 09:55:39 -0800528 ret->x509_method = method->x509_method;
Kenny Rootb8494592015-09-25 02:29:14 +0000529
530 CRYPTO_MUTEX_init(&ret->lock);
531
532 ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
533 ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
534
Kenny Rootb8494592015-09-25 02:29:14 +0000535 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
Robert Sloan4d1ac502017-02-06 08:36:14 -0800536 ret->session_psk_dhe_timeout = SSL_DEFAULT_SESSION_PSK_DHE_TIMEOUT;
Kenny Rootb8494592015-09-25 02:29:14 +0000537
538 ret->references = 1;
539
540 ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
541 ret->verify_mode = SSL_VERIFY_NONE;
Robert Sloan5cbb5c82018-04-24 11:35:46 -0700542 ret->cert = New<CERT>(method->x509_method);
Kenny Rootb8494592015-09-25 02:29:14 +0000543 if (ret->cert == NULL) {
544 goto err;
545 }
546
547 ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
548 if (ret->sessions == NULL) {
549 goto err;
550 }
Robert Sloan7d422bc2017-03-06 10:04:29 -0800551
552 if (!ret->x509_method->ssl_ctx_new(ret)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000553 goto err;
554 }
555
Robert Sloan1c9db532017-03-13 08:03:59 -0700556 if (!SSL_CTX_set_strict_cipher_list(ret, SSL_DEFAULT_CIPHER_LIST)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000557 goto err2;
558 }
559
Robert Sloan7d422bc2017-03-06 10:04:29 -0800560 ret->client_CA = sk_CRYPTO_BUFFER_new_null();
Kenny Rootb8494592015-09-25 02:29:14 +0000561 if (ret->client_CA == NULL) {
562 goto err;
563 }
564
Adam Langley4139edb2016-01-13 15:00:54 -0800565 CRYPTO_new_ex_data(&ret->ex_data);
Kenny Rootb8494592015-09-25 02:29:14 +0000566
567 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
568
Robert Sloana27a6a42017-09-05 08:39:28 -0700569 // Disable the auto-chaining feature by default. Once this has stuck without
570 // problems, the feature will be removed entirely.
Steven Valdez909b19f2016-11-21 15:35:44 -0500571 ret->mode = SSL_MODE_NO_AUTO_CHAIN;
572
Robert Sloan5cbb5c82018-04-24 11:35:46 -0700573 ret->rsa_pss_rsae_certs_enabled = true;
574
Robert Sloana27a6a42017-09-05 08:39:28 -0700575 // Lock the SSL_CTX to the specified version, for compatibility with legacy
Robert Sloanab8b8882018-03-26 11:39:51 -0700576 // uses of SSL_METHOD.
David Benjamin7c0d06c2016-08-11 13:26:41 -0400577 if (!SSL_CTX_set_max_proto_version(ret, method->version) ||
Robert Sloanab8b8882018-03-26 11:39:51 -0700578 !SSL_CTX_set_min_proto_version(ret, method->version)) {
David Benjamin7c0d06c2016-08-11 13:26:41 -0400579 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
580 goto err2;
Kenny Rootb8494592015-09-25 02:29:14 +0000581 }
582
583 return ret;
584
585err:
586 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
587err2:
588 SSL_CTX_free(ret);
589 return NULL;
590}
591
David Benjaminc895d6b2016-08-11 13:26:41 -0400592int SSL_CTX_up_ref(SSL_CTX *ctx) {
593 CRYPTO_refcount_inc(&ctx->references);
594 return 1;
595}
596
Kenny Rootb8494592015-09-25 02:29:14 +0000597void SSL_CTX_free(SSL_CTX *ctx) {
598 if (ctx == NULL ||
599 !CRYPTO_refcount_dec_and_test_zero(&ctx->references)) {
600 return;
601 }
602
Robert Sloana27a6a42017-09-05 08:39:28 -0700603 // Free internal session cache. However: the remove_cb() may reference the
604 // ex_data of SSL_CTX, thus the ex_data store can only be removed after the
605 // sessions were flushed. As the ex_data handling routines might also touch
606 // the session cache, the most secure solution seems to be: empty (flush) the
607 // cache, then free ex_data, then finally free the cache. (See ticket
608 // [openssl.org #212].)
Kenny Rootb8494592015-09-25 02:29:14 +0000609 SSL_CTX_flush_sessions(ctx, 0);
610
611 CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, ctx, &ctx->ex_data);
612
613 CRYPTO_MUTEX_cleanup(&ctx->lock);
614 lh_SSL_SESSION_free(ctx->sessions);
Robert Sloanc6ebb282018-04-30 10:10:26 -0700615 Delete(ctx->cipher_list);
Robert Sloan5cbb5c82018-04-24 11:35:46 -0700616 Delete(ctx->cert);
Kenny Rootb8494592015-09-25 02:29:14 +0000617 sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->client_custom_extensions,
618 SSL_CUSTOM_EXTENSION_free);
619 sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->server_custom_extensions,
620 SSL_CUSTOM_EXTENSION_free);
Robert Sloan7d422bc2017-03-06 10:04:29 -0800621 sk_CRYPTO_BUFFER_pop_free(ctx->client_CA, CRYPTO_BUFFER_free);
622 ctx->x509_method->ssl_ctx_free(ctx);
Kenny Rootb8494592015-09-25 02:29:14 +0000623 sk_SRTP_PROTECTION_PROFILE_free(ctx->srtp_profiles);
624 OPENSSL_free(ctx->psk_identity_hint);
David Benjamind316cba2016-06-02 16:17:39 -0400625 OPENSSL_free(ctx->supported_group_list);
Kenny Rootb8494592015-09-25 02:29:14 +0000626 OPENSSL_free(ctx->alpn_client_proto_list);
Kenny Rootb8494592015-09-25 02:29:14 +0000627 EVP_PKEY_free(ctx->tlsext_channel_id_private);
Robert Sloan572a4e22017-04-17 10:52:19 -0700628 OPENSSL_free(ctx->verify_sigalgs);
Robert Sloan8f860b12017-08-28 07:37:06 -0700629 OPENSSL_free(ctx->tlsext_ticket_key_current);
630 OPENSSL_free(ctx->tlsext_ticket_key_prev);
Kenny Rootb8494592015-09-25 02:29:14 +0000631
632 OPENSSL_free(ctx);
633}
634
Adam Langleyd9e397b2015-01-22 14:27:53 -0800635SSL *SSL_new(SSL_CTX *ctx) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800636 if (ctx == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +0000637 OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_CTX);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800638 return NULL;
639 }
640 if (ctx->method == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +0000641 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800642 return NULL;
643 }
644
Robert Sloana12bf462017-07-17 07:08:26 -0700645 SSL *ssl = (SSL *)OPENSSL_malloc(sizeof(SSL));
Adam Langley4139edb2016-01-13 15:00:54 -0800646 if (ssl == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800647 goto err;
648 }
Robert Sloan69939df2017-01-09 10:53:07 -0800649 OPENSSL_memset(ssl, 0, sizeof(SSL));
Adam Langleyd9e397b2015-01-22 14:27:53 -0800650
Robert Sloane56da3e2017-06-26 08:26:42 -0700651 ssl->conf_min_version = ctx->conf_min_version;
652 ssl->conf_max_version = ctx->conf_max_version;
Robert Sloana12bf462017-07-17 07:08:26 -0700653 ssl->tls13_variant = ctx->tls13_variant;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800654
Robert Sloana27a6a42017-09-05 08:39:28 -0700655 // RFC 6347 states that implementations SHOULD use an initial timer value of
656 // 1 second.
David Benjamind316cba2016-06-02 16:17:39 -0400657 ssl->initial_timeout_duration_ms = 1000;
658
Adam Langley4139edb2016-01-13 15:00:54 -0800659 ssl->options = ctx->options;
660 ssl->mode = ctx->mode;
661 ssl->max_cert_list = ctx->max_cert_list;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800662
Robert Sloan5cbb5c82018-04-24 11:35:46 -0700663 ssl->cert = ssl_cert_dup(ctx->cert).release();
Adam Langley4139edb2016-01-13 15:00:54 -0800664 if (ssl->cert == NULL) {
Adam Langleye9ada862015-05-11 17:20:37 -0700665 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800666 }
667
Adam Langley4139edb2016-01-13 15:00:54 -0800668 ssl->msg_callback = ctx->msg_callback;
669 ssl->msg_callback_arg = ctx->msg_callback_arg;
670 ssl->verify_mode = ctx->verify_mode;
Adam Langley4139edb2016-01-13 15:00:54 -0800671 ssl->verify_callback = ctx->default_verify_callback;
Robert Sloanb6d070c2017-07-24 08:40:01 -0700672 ssl->custom_verify_callback = ctx->custom_verify_callback;
Steven Valdez909b19f2016-11-21 15:35:44 -0500673 ssl->retain_only_sha256_of_client_certs =
674 ctx->retain_only_sha256_of_client_certs;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800675
Adam Langley4139edb2016-01-13 15:00:54 -0800676 ssl->quiet_shutdown = ctx->quiet_shutdown;
677 ssl->max_send_fragment = ctx->max_send_fragment;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800678
Robert Sloan69939df2017-01-09 10:53:07 -0800679 SSL_CTX_up_ref(ctx);
Adam Langley4139edb2016-01-13 15:00:54 -0800680 ssl->ctx = ctx;
Robert Sloan69939df2017-01-09 10:53:07 -0800681 SSL_CTX_up_ref(ctx);
Robert Sloan1c9db532017-03-13 08:03:59 -0700682 ssl->session_ctx = ctx;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800683
Robert Sloan7d422bc2017-03-06 10:04:29 -0800684 if (!ssl->ctx->x509_method->ssl_new(ssl)) {
685 goto err;
686 }
687
David Benjamind316cba2016-06-02 16:17:39 -0400688 if (ctx->supported_group_list) {
Robert Sloana12bf462017-07-17 07:08:26 -0700689 ssl->supported_group_list = (uint16_t *)BUF_memdup(
690 ctx->supported_group_list, ctx->supported_group_list_len * 2);
David Benjamind316cba2016-06-02 16:17:39 -0400691 if (!ssl->supported_group_list) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800692 goto err;
693 }
David Benjamind316cba2016-06-02 16:17:39 -0400694 ssl->supported_group_list_len = ctx->supported_group_list_len;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800695 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800696
Robert Sloan4d1ac502017-02-06 08:36:14 -0800697 if (ctx->alpn_client_proto_list) {
Robert Sloana12bf462017-07-17 07:08:26 -0700698 ssl->alpn_client_proto_list = (uint8_t *)BUF_memdup(
699 ctx->alpn_client_proto_list, ctx->alpn_client_proto_list_len);
Adam Langley4139edb2016-01-13 15:00:54 -0800700 if (ssl->alpn_client_proto_list == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800701 goto err;
702 }
Robert Sloan4d1ac502017-02-06 08:36:14 -0800703 ssl->alpn_client_proto_list_len = ctx->alpn_client_proto_list_len;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800704 }
705
Adam Langley4139edb2016-01-13 15:00:54 -0800706 ssl->method = ctx->method;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800707
Adam Langley4139edb2016-01-13 15:00:54 -0800708 if (!ssl->method->ssl_new(ssl)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800709 goto err;
710 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800711
Adam Langley4139edb2016-01-13 15:00:54 -0800712 CRYPTO_new_ex_data(&ssl->ex_data);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800713
Adam Langley4139edb2016-01-13 15:00:54 -0800714 ssl->psk_identity_hint = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800715 if (ctx->psk_identity_hint) {
Adam Langley4139edb2016-01-13 15:00:54 -0800716 ssl->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
717 if (ssl->psk_identity_hint == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800718 goto err;
719 }
720 }
Adam Langley4139edb2016-01-13 15:00:54 -0800721 ssl->psk_client_callback = ctx->psk_client_callback;
722 ssl->psk_server_callback = ctx->psk_server_callback;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800723
Adam Langley4139edb2016-01-13 15:00:54 -0800724 ssl->tlsext_channel_id_enabled = ctx->tlsext_channel_id_enabled;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800725 if (ctx->tlsext_channel_id_private) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400726 EVP_PKEY_up_ref(ctx->tlsext_channel_id_private);
727 ssl->tlsext_channel_id_private = ctx->tlsext_channel_id_private;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800728 }
729
Robert Sloan4d1ac502017-02-06 08:36:14 -0800730 ssl->signed_cert_timestamps_enabled = ctx->signed_cert_timestamps_enabled;
731 ssl->ocsp_stapling_enabled = ctx->ocsp_stapling_enabled;
Robert Sloan8542c082018-02-05 09:07:34 -0800732 ssl->handoff = ctx->handoff;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800733
Adam Langley4139edb2016-01-13 15:00:54 -0800734 return ssl;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800735
736err:
Adam Langley4139edb2016-01-13 15:00:54 -0800737 SSL_free(ssl);
Kenny Rootb8494592015-09-25 02:29:14 +0000738 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800739
740 return NULL;
741}
742
Kenny Rootb8494592015-09-25 02:29:14 +0000743void SSL_free(SSL *ssl) {
744 if (ssl == NULL) {
745 return;
746 }
747
Robert Sloane56da3e2017-06-26 08:26:42 -0700748 if (ssl->ctx != NULL) {
749 ssl->ctx->x509_method->ssl_free(ssl);
750 }
751
Kenny Rootb8494592015-09-25 02:29:14 +0000752 CRYPTO_free_ex_data(&g_ex_data_class_ssl, ssl, &ssl->ex_data);
753
Kenny Rootb8494592015-09-25 02:29:14 +0000754 BIO_free_all(ssl->rbio);
David Benjaminc895d6b2016-08-11 13:26:41 -0400755 BIO_free_all(ssl->wbio);
Kenny Rootb8494592015-09-25 02:29:14 +0000756
Robert Sloana27a6a42017-09-05 08:39:28 -0700757 // add extra stuff
Robert Sloanc6ebb282018-04-30 10:10:26 -0700758 Delete(ssl->cipher_list);
Kenny Rootb8494592015-09-25 02:29:14 +0000759
Kenny Rootb8494592015-09-25 02:29:14 +0000760 SSL_SESSION_free(ssl->session);
761
Robert Sloan5cbb5c82018-04-24 11:35:46 -0700762 Delete(ssl->cert);
Kenny Rootb8494592015-09-25 02:29:14 +0000763
764 OPENSSL_free(ssl->tlsext_hostname);
Robert Sloan1c9db532017-03-13 08:03:59 -0700765 SSL_CTX_free(ssl->session_ctx);
David Benjamind316cba2016-06-02 16:17:39 -0400766 OPENSSL_free(ssl->supported_group_list);
Kenny Rootb8494592015-09-25 02:29:14 +0000767 OPENSSL_free(ssl->alpn_client_proto_list);
Robert Sloan978112c2018-01-22 12:53:01 -0800768 OPENSSL_free(ssl->token_binding_params);
Robert Sloan8542c082018-02-05 09:07:34 -0800769 OPENSSL_free(ssl->quic_transport_params);
Kenny Rootb8494592015-09-25 02:29:14 +0000770 EVP_PKEY_free(ssl->tlsext_channel_id_private);
771 OPENSSL_free(ssl->psk_identity_hint);
Robert Sloan7d422bc2017-03-06 10:04:29 -0800772 sk_CRYPTO_BUFFER_pop_free(ssl->client_CA, CRYPTO_BUFFER_free);
Kenny Rootb8494592015-09-25 02:29:14 +0000773 sk_SRTP_PROTECTION_PROFILE_free(ssl->srtp_profiles);
774
775 if (ssl->method != NULL) {
776 ssl->method->ssl_free(ssl);
777 }
778 SSL_CTX_free(ssl->ctx);
779
780 OPENSSL_free(ssl);
781}
782
783void SSL_set_connect_state(SSL *ssl) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700784 ssl->server = false;
Robert Sloana27a6a42017-09-05 08:39:28 -0700785 ssl->do_handshake = ssl_client_handshake;
Kenny Rootb8494592015-09-25 02:29:14 +0000786}
787
788void SSL_set_accept_state(SSL *ssl) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700789 ssl->server = true;
Robert Sloana27a6a42017-09-05 08:39:28 -0700790 ssl->do_handshake = ssl_server_handshake;
Kenny Rootb8494592015-09-25 02:29:14 +0000791}
792
David Benjaminc895d6b2016-08-11 13:26:41 -0400793void SSL_set0_rbio(SSL *ssl, BIO *rbio) {
794 BIO_free_all(ssl->rbio);
795 ssl->rbio = rbio;
796}
797
798void SSL_set0_wbio(SSL *ssl, BIO *wbio) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400799 BIO_free_all(ssl->wbio);
Kenny Rootb8494592015-09-25 02:29:14 +0000800 ssl->wbio = wbio;
801}
802
David Benjaminc895d6b2016-08-11 13:26:41 -0400803void SSL_set_bio(SSL *ssl, BIO *rbio, BIO *wbio) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700804 // For historical reasons, this function has many different cases in ownership
805 // handling.
David Benjaminc895d6b2016-08-11 13:26:41 -0400806
Robert Sloana27a6a42017-09-05 08:39:28 -0700807 // If nothing has changed, do nothing
David Benjaminc895d6b2016-08-11 13:26:41 -0400808 if (rbio == SSL_get_rbio(ssl) && wbio == SSL_get_wbio(ssl)) {
809 return;
810 }
811
Robert Sloana27a6a42017-09-05 08:39:28 -0700812 // If the two arguments are equal, one fewer reference is granted than
813 // taken.
David Benjaminc895d6b2016-08-11 13:26:41 -0400814 if (rbio != NULL && rbio == wbio) {
815 BIO_up_ref(rbio);
816 }
817
Robert Sloana27a6a42017-09-05 08:39:28 -0700818 // If only the wbio is changed, adopt only one reference.
David Benjaminc895d6b2016-08-11 13:26:41 -0400819 if (rbio == SSL_get_rbio(ssl)) {
820 SSL_set0_wbio(ssl, wbio);
821 return;
822 }
823
Robert Sloana27a6a42017-09-05 08:39:28 -0700824 // There is an asymmetry here for historical reasons. If only the rbio is
825 // changed AND the rbio and wbio were originally different, then we only adopt
826 // one reference.
David Benjaminc895d6b2016-08-11 13:26:41 -0400827 if (wbio == SSL_get_wbio(ssl) && SSL_get_rbio(ssl) != SSL_get_wbio(ssl)) {
828 SSL_set0_rbio(ssl, rbio);
829 return;
830 }
831
Robert Sloana27a6a42017-09-05 08:39:28 -0700832 // Otherwise, adopt both references.
David Benjaminc895d6b2016-08-11 13:26:41 -0400833 SSL_set0_rbio(ssl, rbio);
834 SSL_set0_wbio(ssl, wbio);
835}
836
Kenny Rootb8494592015-09-25 02:29:14 +0000837BIO *SSL_get_rbio(const SSL *ssl) { return ssl->rbio; }
838
Robert Sloan4d1ac502017-02-06 08:36:14 -0800839BIO *SSL_get_wbio(const SSL *ssl) { return ssl->wbio; }
Kenny Rootb8494592015-09-25 02:29:14 +0000840
Robert Sloan69939df2017-01-09 10:53:07 -0800841int SSL_do_handshake(SSL *ssl) {
842 ssl_reset_error_state(ssl);
David Benjamin4969cc92016-04-22 15:02:23 -0400843
Robert Sloana27a6a42017-09-05 08:39:28 -0700844 if (ssl->do_handshake == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +0000845 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_TYPE_NOT_SET);
846 return -1;
847 }
848
849 if (!SSL_in_init(ssl)) {
850 return 1;
851 }
852
Robert Sloana27a6a42017-09-05 08:39:28 -0700853 // Run the handshake.
Robert Sloan29c1d2c2017-10-30 14:10:28 -0700854 SSL_HANDSHAKE *hs = ssl->s3->hs.get();
David Benjamin1b249672016-12-06 18:25:50 -0500855
Robert Sloana27a6a42017-09-05 08:39:28 -0700856 bool early_return = false;
857 int ret = ssl_run_handshake(hs, &early_return);
858 ssl_do_info_callback(
859 ssl, ssl->server ? SSL_CB_ACCEPT_EXIT : SSL_CB_CONNECT_EXIT, ret);
David Benjamin1b249672016-12-06 18:25:50 -0500860 if (ret <= 0) {
861 return ret;
862 }
863
Robert Sloana27a6a42017-09-05 08:39:28 -0700864 // Destroy the handshake object if the handshake has completely finished.
865 if (!early_return) {
Robert Sloan29c1d2c2017-10-30 14:10:28 -0700866 ssl->s3->hs.reset();
David Benjamin1b249672016-12-06 18:25:50 -0500867 }
868
869 return 1;
Kenny Rootb8494592015-09-25 02:29:14 +0000870}
871
872int SSL_connect(SSL *ssl) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700873 if (ssl->do_handshake == NULL) {
874 // Not properly initialized yet
Kenny Rootb8494592015-09-25 02:29:14 +0000875 SSL_set_connect_state(ssl);
876 }
877
David Benjamin4969cc92016-04-22 15:02:23 -0400878 return SSL_do_handshake(ssl);
Kenny Rootb8494592015-09-25 02:29:14 +0000879}
880
881int SSL_accept(SSL *ssl) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700882 if (ssl->do_handshake == NULL) {
883 // Not properly initialized yet
Kenny Rootb8494592015-09-25 02:29:14 +0000884 SSL_set_accept_state(ssl);
885 }
886
David Benjamin4969cc92016-04-22 15:02:23 -0400887 return SSL_do_handshake(ssl);
888}
889
Robert Sloan84377092017-08-14 09:33:19 -0700890static int ssl_do_post_handshake(SSL *ssl, const SSLMessage &msg) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700891 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
Robert Sloan84377092017-08-14 09:33:19 -0700892 return tls13_post_handshake(ssl, msg);
893 }
894
Robert Sloana27a6a42017-09-05 08:39:28 -0700895 // We do not accept renegotiations as a server or SSL 3.0. SSL 3.0 will be
896 // removed entirely in the future and requires retaining more data for
897 // renegotiation_info.
David Benjamin95add822016-10-19 01:09:12 -0400898 if (ssl->server || ssl->version == SSL3_VERSION) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400899 goto no_renegotiation;
900 }
901
Robert Sloan84377092017-08-14 09:33:19 -0700902 if (msg.type != SSL3_MT_HELLO_REQUEST || CBS_len(&msg.body) != 0) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700903 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjaminc895d6b2016-08-11 13:26:41 -0400904 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HELLO_REQUEST);
905 return 0;
906 }
907
908 switch (ssl->renegotiate_mode) {
909 case ssl_renegotiate_ignore:
Robert Sloana27a6a42017-09-05 08:39:28 -0700910 // Ignore the HelloRequest.
David Benjaminc895d6b2016-08-11 13:26:41 -0400911 return 1;
912
913 case ssl_renegotiate_once:
914 if (ssl->s3->total_renegotiations != 0) {
915 goto no_renegotiation;
916 }
917 break;
918
919 case ssl_renegotiate_never:
920 goto no_renegotiation;
921
922 case ssl_renegotiate_freely:
923 break;
924 }
925
Robert Sloana27a6a42017-09-05 08:39:28 -0700926 // Renegotiation is only supported at quiescent points in the application
927 // protocol, namely in HTTPS, just before reading the HTTP response. Require
928 // the record-layer be idle and avoid complexities of sending a handshake
929 // record while an application_data record is being written.
Robert Sloan29c1d2c2017-10-30 14:10:28 -0700930 if (!ssl->s3->write_buffer.empty() ||
Robert Sloan36272962017-10-23 10:28:39 -0700931 ssl->s3->write_shutdown != ssl_shutdown_none) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400932 goto no_renegotiation;
933 }
934
Robert Sloana27a6a42017-09-05 08:39:28 -0700935 // Begin a new handshake.
Robert Sloan29c1d2c2017-10-30 14:10:28 -0700936 if (ssl->s3->hs != nullptr) {
Steven Valdeze7531f02016-12-14 13:29:57 -0500937 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
938 return 0;
939 }
940 ssl->s3->hs = ssl_handshake_new(ssl);
Robert Sloan29c1d2c2017-10-30 14:10:28 -0700941 if (ssl->s3->hs == nullptr) {
Steven Valdeze7531f02016-12-14 13:29:57 -0500942 return 0;
943 }
944
David Benjaminc895d6b2016-08-11 13:26:41 -0400945 ssl->s3->total_renegotiations++;
David Benjaminc895d6b2016-08-11 13:26:41 -0400946 return 1;
947
948no_renegotiation:
David Benjaminc895d6b2016-08-11 13:26:41 -0400949 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION);
Robert Sloan36272962017-10-23 10:28:39 -0700950 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_NO_RENEGOTIATION);
David Benjaminc895d6b2016-08-11 13:26:41 -0400951 return 0;
952}
953
Robert Sloan36272962017-10-23 10:28:39 -0700954static int ssl_read_impl(SSL *ssl) {
Robert Sloan69939df2017-01-09 10:53:07 -0800955 ssl_reset_error_state(ssl);
David Benjamin4969cc92016-04-22 15:02:23 -0400956
Robert Sloana27a6a42017-09-05 08:39:28 -0700957 if (ssl->do_handshake == NULL) {
David Benjamin4969cc92016-04-22 15:02:23 -0400958 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
Kenny Rootb8494592015-09-25 02:29:14 +0000959 return -1;
960 }
961
Robert Sloan36272962017-10-23 10:28:39 -0700962 // Replay post-handshake message errors.
963 if (!check_read_error(ssl)) {
964 return -1;
965 }
966
967 while (ssl->s3->pending_app_data.empty()) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700968 // Complete the current handshake, if any. False Start will cause
969 // |SSL_do_handshake| to return mid-handshake, so this may require multiple
970 // iterations.
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700971 while (!ssl_can_read(ssl)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400972 int ret = SSL_do_handshake(ssl);
973 if (ret < 0) {
974 return ret;
975 }
976 if (ret == 0) {
977 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
978 return -1;
979 }
980 }
981
Robert Sloan921ef2c2017-10-17 09:02:20 -0700982 // Process any buffered post-handshake messages.
Robert Sloan84377092017-08-14 09:33:19 -0700983 SSLMessage msg;
Robert Sloan921ef2c2017-10-17 09:02:20 -0700984 if (ssl->method->get_message(ssl, &msg)) {
Robert Sloanb1b54b82017-11-06 13:50:02 -0800985 // If we received an interrupt in early read (EndOfEarlyData), loop again
986 // for the handshake to process it.
987 if (SSL_in_init(ssl)) {
988 ssl->s3->hs->can_early_read = false;
989 continue;
990 }
991
Robert Sloana27a6a42017-09-05 08:39:28 -0700992 // Handle the post-handshake message and try again.
Robert Sloan84377092017-08-14 09:33:19 -0700993 if (!ssl_do_post_handshake(ssl, msg)) {
Robert Sloan36272962017-10-23 10:28:39 -0700994 ssl_set_read_error(ssl);
Robert Sloan84377092017-08-14 09:33:19 -0700995 return -1;
996 }
997 ssl->method->next_message(ssl);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700998 continue; // Loop again. We may have begun a new handshake.
David Benjamin4969cc92016-04-22 15:02:23 -0400999 }
Robert Sloan921ef2c2017-10-17 09:02:20 -07001000
Robert Sloan36272962017-10-23 10:28:39 -07001001 uint8_t alert = SSL_AD_DECODE_ERROR;
1002 size_t consumed = 0;
1003 auto ret = ssl_open_app_data(ssl, &ssl->s3->pending_app_data, &consumed,
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001004 &alert, ssl->s3->read_buffer.span());
Robert Sloan36272962017-10-23 10:28:39 -07001005 bool retry;
1006 int bio_ret = ssl_handle_open_record(ssl, &retry, ret, consumed, alert);
1007 if (bio_ret <= 0) {
1008 return bio_ret;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001009 }
Robert Sloan36272962017-10-23 10:28:39 -07001010 if (!retry) {
1011 assert(!ssl->s3->pending_app_data.empty());
Robert Sloan921ef2c2017-10-17 09:02:20 -07001012 ssl->s3->key_update_count = 0;
1013 }
David Benjamin4969cc92016-04-22 15:02:23 -04001014 }
Robert Sloan36272962017-10-23 10:28:39 -07001015
1016 return 1;
Kenny Rootb8494592015-09-25 02:29:14 +00001017}
1018
1019int SSL_read(SSL *ssl, void *buf, int num) {
Robert Sloan36272962017-10-23 10:28:39 -07001020 int ret = SSL_peek(ssl, buf, num);
1021 if (ret <= 0) {
1022 return ret;
1023 }
1024 // TODO(davidben): In DTLS, should the rest of the record be discarded? DTLS
1025 // is not a stream. See https://crbug.com/boringssl/65.
1026 ssl->s3->pending_app_data =
1027 ssl->s3->pending_app_data.subspan(static_cast<size_t>(ret));
1028 if (ssl->s3->pending_app_data.empty()) {
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001029 ssl->s3->read_buffer.DiscardConsumed();
Robert Sloan36272962017-10-23 10:28:39 -07001030 }
1031 return ret;
Kenny Rootb8494592015-09-25 02:29:14 +00001032}
1033
1034int SSL_peek(SSL *ssl, void *buf, int num) {
Robert Sloan36272962017-10-23 10:28:39 -07001035 int ret = ssl_read_impl(ssl);
1036 if (ret <= 0) {
1037 return ret;
1038 }
1039 if (num <= 0) {
1040 return num;
1041 }
1042 size_t todo =
1043 std::min(ssl->s3->pending_app_data.size(), static_cast<size_t>(num));
1044 OPENSSL_memcpy(buf, ssl->s3->pending_app_data.data(), todo);
1045 return static_cast<int>(todo);
Kenny Rootb8494592015-09-25 02:29:14 +00001046}
1047
1048int SSL_write(SSL *ssl, const void *buf, int num) {
Robert Sloan69939df2017-01-09 10:53:07 -08001049 ssl_reset_error_state(ssl);
David Benjamin4969cc92016-04-22 15:02:23 -04001050
Robert Sloana27a6a42017-09-05 08:39:28 -07001051 if (ssl->do_handshake == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001052 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
1053 return -1;
1054 }
1055
Robert Sloan921ef2c2017-10-17 09:02:20 -07001056 if (ssl->s3->write_shutdown != ssl_shutdown_none) {
Kenny Rootb8494592015-09-25 02:29:14 +00001057 OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
1058 return -1;
1059 }
1060
Robert Sloana27a6a42017-09-05 08:39:28 -07001061 int ret = 0;
1062 bool needs_handshake = false;
Robert Sloane56da3e2017-06-26 08:26:42 -07001063 do {
Robert Sloana27a6a42017-09-05 08:39:28 -07001064 // If necessary, complete the handshake implicitly.
Robert Sloane56da3e2017-06-26 08:26:42 -07001065 if (!ssl_can_write(ssl)) {
1066 ret = SSL_do_handshake(ssl);
1067 if (ret < 0) {
1068 return ret;
1069 }
1070 if (ret == 0) {
1071 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
1072 return -1;
1073 }
David Benjamin4969cc92016-04-22 15:02:23 -04001074 }
David Benjamin4969cc92016-04-22 15:02:23 -04001075
Robert Sloana12bf462017-07-17 07:08:26 -07001076 ret = ssl->method->write_app_data(ssl, &needs_handshake,
1077 (const uint8_t *)buf, num);
Robert Sloane56da3e2017-06-26 08:26:42 -07001078 } while (needs_handshake);
1079 return ret;
Kenny Rootb8494592015-09-25 02:29:14 +00001080}
1081
1082int SSL_shutdown(SSL *ssl) {
Robert Sloan69939df2017-01-09 10:53:07 -08001083 ssl_reset_error_state(ssl);
David Benjamin4969cc92016-04-22 15:02:23 -04001084
Robert Sloana27a6a42017-09-05 08:39:28 -07001085 if (ssl->do_handshake == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001086 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
1087 return -1;
1088 }
1089
Robert Sloana27a6a42017-09-05 08:39:28 -07001090 // If we are in the middle of a handshake, silently succeed. Consumers often
1091 // call this function before |SSL_free|, whether the handshake succeeded or
1092 // not. We assume the caller has already handled failed handshakes.
Kenny Rootb8494592015-09-25 02:29:14 +00001093 if (SSL_in_init(ssl)) {
Robert Sloana94fe052017-02-21 08:49:28 -08001094 return 1;
Kenny Rootb8494592015-09-25 02:29:14 +00001095 }
1096
Kenny Rootb8494592015-09-25 02:29:14 +00001097 if (ssl->quiet_shutdown) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001098 // Do nothing if configured not to send a close_notify.
Robert Sloan921ef2c2017-10-17 09:02:20 -07001099 ssl->s3->write_shutdown = ssl_shutdown_close_notify;
1100 ssl->s3->read_shutdown = ssl_shutdown_close_notify;
Kenny Rootb8494592015-09-25 02:29:14 +00001101 return 1;
1102 }
1103
Robert Sloana27a6a42017-09-05 08:39:28 -07001104 // This function completes in two stages. It sends a close_notify and then it
1105 // waits for a close_notify to come in. Perform exactly one action and return
1106 // whether or not it succeeds.
Kenny Rootb8494592015-09-25 02:29:14 +00001107
Robert Sloan921ef2c2017-10-17 09:02:20 -07001108 if (ssl->s3->write_shutdown != ssl_shutdown_close_notify) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001109 // Send a close_notify.
Robert Sloan921ef2c2017-10-17 09:02:20 -07001110 if (ssl_send_alert(ssl, SSL3_AL_WARNING, SSL_AD_CLOSE_NOTIFY) <= 0) {
David Benjamind316cba2016-06-02 16:17:39 -04001111 return -1;
Kenny Rootb8494592015-09-25 02:29:14 +00001112 }
1113 } else if (ssl->s3->alert_dispatch) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001114 // Finish sending the close_notify.
David Benjaminc895d6b2016-08-11 13:26:41 -04001115 if (ssl->method->dispatch_alert(ssl) <= 0) {
David Benjamind316cba2016-06-02 16:17:39 -04001116 return -1;
Kenny Rootb8494592015-09-25 02:29:14 +00001117 }
Robert Sloan921ef2c2017-10-17 09:02:20 -07001118 } else if (ssl->s3->read_shutdown != ssl_shutdown_close_notify) {
Robert Sloan36272962017-10-23 10:28:39 -07001119 if (SSL_is_dtls(ssl)) {
1120 // Bidirectional shutdown doesn't make sense for an unordered
1121 // transport. DTLS alerts also aren't delivered reliably, so we may even
1122 // time out because the peer never received our close_notify. Report to
1123 // the caller that the channel has fully shut down.
1124 if (ssl->s3->read_shutdown == ssl_shutdown_error) {
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001125 ERR_restore_state(ssl->s3->read_error.get());
Robert Sloan36272962017-10-23 10:28:39 -07001126 return -1;
1127 }
1128 ssl->s3->read_shutdown = ssl_shutdown_close_notify;
1129 } else {
1130 // Keep discarding data until we see a close_notify.
1131 for (;;) {
1132 ssl->s3->pending_app_data = Span<uint8_t>();
1133 int ret = ssl_read_impl(ssl);
1134 if (ret <= 0) {
1135 break;
1136 }
1137 }
1138 if (ssl->s3->read_shutdown != ssl_shutdown_close_notify) {
1139 return -1;
1140 }
Kenny Rootb8494592015-09-25 02:29:14 +00001141 }
1142 }
1143
Robert Sloana27a6a42017-09-05 08:39:28 -07001144 // Return 0 for unidirectional shutdown and 1 for bidirectional shutdown.
Robert Sloan921ef2c2017-10-17 09:02:20 -07001145 return ssl->s3->read_shutdown == ssl_shutdown_close_notify;
Kenny Rootb8494592015-09-25 02:29:14 +00001146}
1147
David Benjaminc895d6b2016-08-11 13:26:41 -04001148int SSL_send_fatal_alert(SSL *ssl, uint8_t alert) {
1149 if (ssl->s3->alert_dispatch) {
1150 if (ssl->s3->send_alert[0] != SSL3_AL_FATAL ||
1151 ssl->s3->send_alert[1] != alert) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001152 // We are already attempting to write a different alert.
David Benjaminc895d6b2016-08-11 13:26:41 -04001153 OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
1154 return -1;
1155 }
1156 return ssl->method->dispatch_alert(ssl);
1157 }
1158
Robert Sloan921ef2c2017-10-17 09:02:20 -07001159 return ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
David Benjaminc895d6b2016-08-11 13:26:41 -04001160}
1161
Robert Sloan8542c082018-02-05 09:07:34 -08001162int SSL_set_quic_transport_params(SSL *ssl, const uint8_t *params,
1163 size_t params_len) {
1164 ssl->quic_transport_params = (uint8_t *)BUF_memdup(params, params_len);
1165 if (!ssl->quic_transport_params) {
1166 return 0;
1167 }
1168 ssl->quic_transport_params_len = params_len;
1169 return 1;
1170}
1171
1172void SSL_get_peer_quic_transport_params(const SSL *ssl,
1173 const uint8_t **out_params,
1174 size_t *out_params_len) {
1175 *out_params = ssl->s3->peer_quic_transport_params.data();
1176 *out_params_len = ssl->s3->peer_quic_transport_params.size();
1177}
1178
Robert Sloan69939df2017-01-09 10:53:07 -08001179void SSL_CTX_set_early_data_enabled(SSL_CTX *ctx, int enabled) {
Robert Sloan572a4e22017-04-17 10:52:19 -07001180 ctx->cert->enable_early_data = !!enabled;
1181}
1182
Robert Sloana12bf462017-07-17 07:08:26 -07001183void SSL_CTX_set_tls13_variant(SSL_CTX *ctx, enum tls13_variant_t variant) {
1184 ctx->tls13_variant = variant;
1185}
1186
1187void SSL_set_tls13_variant(SSL *ssl, enum tls13_variant_t variant) {
1188 ssl->tls13_variant = variant;
1189}
1190
Robert Sloan572a4e22017-04-17 10:52:19 -07001191void SSL_set_early_data_enabled(SSL *ssl, int enabled) {
1192 ssl->cert->enable_early_data = !!enabled;
Robert Sloan69939df2017-01-09 10:53:07 -08001193}
1194
Robert Sloane56da3e2017-06-26 08:26:42 -07001195int SSL_in_early_data(const SSL *ssl) {
1196 if (ssl->s3->hs == NULL) {
1197 return 0;
1198 }
1199 return ssl->s3->hs->in_early_data;
1200}
1201
Robert Sloan6d0d00e2017-03-27 07:13:07 -07001202int SSL_early_data_accepted(const SSL *ssl) {
Robert Sloan0da43952018-01-03 15:13:14 -08001203 return ssl->s3->early_data_accepted;
Robert Sloan6d0d00e2017-03-27 07:13:07 -07001204}
1205
Robert Sloane56da3e2017-06-26 08:26:42 -07001206void SSL_reset_early_data_reject(SSL *ssl) {
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001207 SSL_HANDSHAKE *hs = ssl->s3->hs.get();
Robert Sloane56da3e2017-06-26 08:26:42 -07001208 if (hs == NULL ||
1209 hs->wait != ssl_hs_early_data_rejected) {
1210 abort();
1211 }
1212
1213 hs->wait = ssl_hs_ok;
Robert Sloana27a6a42017-09-05 08:39:28 -07001214 hs->in_early_data = false;
Robert Sloanb6d070c2017-07-24 08:40:01 -07001215 hs->early_session.reset();
Robert Sloane56da3e2017-06-26 08:26:42 -07001216
Robert Sloana27a6a42017-09-05 08:39:28 -07001217 // Discard any unfinished writes from the perspective of |SSL_write|'s
1218 // retry. The handshake will transparently flush out the pending record
1219 // (discarded by the server) to keep the framing correct.
1220 ssl->s3->wpend_pending = false;
Robert Sloane56da3e2017-06-26 08:26:42 -07001221}
1222
Steven Valdeze7531f02016-12-14 13:29:57 -05001223static int bio_retry_reason_to_error(int reason) {
1224 switch (reason) {
1225 case BIO_RR_CONNECT:
1226 return SSL_ERROR_WANT_CONNECT;
1227 case BIO_RR_ACCEPT:
1228 return SSL_ERROR_WANT_ACCEPT;
1229 default:
1230 return SSL_ERROR_SYSCALL;
1231 }
1232}
Kenny Rootb8494592015-09-25 02:29:14 +00001233
Steven Valdeze7531f02016-12-14 13:29:57 -05001234int SSL_get_error(const SSL *ssl, int ret_code) {
Kenny Rootb8494592015-09-25 02:29:14 +00001235 if (ret_code > 0) {
1236 return SSL_ERROR_NONE;
1237 }
1238
Robert Sloana27a6a42017-09-05 08:39:28 -07001239 // Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
1240 // where we do encode the error
Steven Valdeze7531f02016-12-14 13:29:57 -05001241 uint32_t err = ERR_peek_error();
Kenny Rootb8494592015-09-25 02:29:14 +00001242 if (err != 0) {
1243 if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
1244 return SSL_ERROR_SYSCALL;
1245 }
1246 return SSL_ERROR_SSL;
1247 }
1248
1249 if (ret_code == 0) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001250 if (ssl->s3->read_shutdown == ssl_shutdown_close_notify) {
Kenny Rootb8494592015-09-25 02:29:14 +00001251 return SSL_ERROR_ZERO_RETURN;
1252 }
Robert Sloana27a6a42017-09-05 08:39:28 -07001253 // An EOF was observed which violates the protocol, and the underlying
1254 // transport does not participate in the error queue. Bubble up to the
1255 // caller.
Kenny Rootb8494592015-09-25 02:29:14 +00001256 return SSL_ERROR_SYSCALL;
1257 }
1258
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001259 switch (ssl->s3->rwstate) {
Steven Valdeze7531f02016-12-14 13:29:57 -05001260 case SSL_PENDING_SESSION:
1261 return SSL_ERROR_PENDING_SESSION;
Kenny Rootb8494592015-09-25 02:29:14 +00001262
Steven Valdeze7531f02016-12-14 13:29:57 -05001263 case SSL_CERTIFICATE_SELECTION_PENDING:
1264 return SSL_ERROR_PENDING_CERTIFICATE;
Kenny Rootb8494592015-09-25 02:29:14 +00001265
Robert Sloan8542c082018-02-05 09:07:34 -08001266 case SSL_HANDOFF:
1267 return SSL_ERROR_HANDOFF;
1268
Robert Sloandc2f6092018-04-10 10:22:33 -07001269 case SSL_HANDBACK:
1270 return SSL_ERROR_HANDBACK;
1271
Steven Valdeze7531f02016-12-14 13:29:57 -05001272 case SSL_READING: {
1273 BIO *bio = SSL_get_rbio(ssl);
1274 if (BIO_should_read(bio)) {
1275 return SSL_ERROR_WANT_READ;
Kenny Rootb8494592015-09-25 02:29:14 +00001276 }
1277
Steven Valdeze7531f02016-12-14 13:29:57 -05001278 if (BIO_should_write(bio)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001279 // TODO(davidben): OpenSSL historically checked for writes on the read
1280 // BIO. Can this be removed?
Steven Valdeze7531f02016-12-14 13:29:57 -05001281 return SSL_ERROR_WANT_WRITE;
Kenny Rootb8494592015-09-25 02:29:14 +00001282 }
1283
Steven Valdeze7531f02016-12-14 13:29:57 -05001284 if (BIO_should_io_special(bio)) {
1285 return bio_retry_reason_to_error(BIO_get_retry_reason(bio));
Kenny Rootb8494592015-09-25 02:29:14 +00001286 }
1287
Steven Valdeze7531f02016-12-14 13:29:57 -05001288 break;
1289 }
1290
1291 case SSL_WRITING: {
1292 BIO *bio = SSL_get_wbio(ssl);
1293 if (BIO_should_write(bio)) {
1294 return SSL_ERROR_WANT_WRITE;
Kenny Rootb8494592015-09-25 02:29:14 +00001295 }
1296
Steven Valdeze7531f02016-12-14 13:29:57 -05001297 if (BIO_should_read(bio)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001298 // TODO(davidben): OpenSSL historically checked for reads on the write
1299 // BIO. Can this be removed?
Steven Valdeze7531f02016-12-14 13:29:57 -05001300 return SSL_ERROR_WANT_READ;
1301 }
1302
1303 if (BIO_should_io_special(bio)) {
1304 return bio_retry_reason_to_error(BIO_get_retry_reason(bio));
1305 }
1306
1307 break;
Kenny Rootb8494592015-09-25 02:29:14 +00001308 }
Kenny Rootb8494592015-09-25 02:29:14 +00001309
Steven Valdeze7531f02016-12-14 13:29:57 -05001310 case SSL_X509_LOOKUP:
1311 return SSL_ERROR_WANT_X509_LOOKUP;
Kenny Rootb8494592015-09-25 02:29:14 +00001312
Steven Valdeze7531f02016-12-14 13:29:57 -05001313 case SSL_CHANNEL_ID_LOOKUP:
1314 return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
Kenny Rootb8494592015-09-25 02:29:14 +00001315
Steven Valdeze7531f02016-12-14 13:29:57 -05001316 case SSL_PRIVATE_KEY_OPERATION:
1317 return SSL_ERROR_WANT_PRIVATE_KEY_OPERATION;
Robert Sloan1c9db532017-03-13 08:03:59 -07001318
1319 case SSL_PENDING_TICKET:
1320 return SSL_ERROR_PENDING_TICKET;
Robert Sloane56da3e2017-06-26 08:26:42 -07001321
1322 case SSL_EARLY_DATA_REJECTED:
1323 return SSL_ERROR_EARLY_DATA_REJECTED;
Robert Sloanb6d070c2017-07-24 08:40:01 -07001324
1325 case SSL_CERTIFICATE_VERIFY:
1326 return SSL_ERROR_WANT_CERTIFICATE_VERIFY;
Kenny Rootb8494592015-09-25 02:29:14 +00001327 }
1328
1329 return SSL_ERROR_SYSCALL;
1330}
1331
Kenny Rootb8494592015-09-25 02:29:14 +00001332uint32_t SSL_CTX_set_options(SSL_CTX *ctx, uint32_t options) {
1333 ctx->options |= options;
1334 return ctx->options;
1335}
1336
1337uint32_t SSL_CTX_clear_options(SSL_CTX *ctx, uint32_t options) {
1338 ctx->options &= ~options;
1339 return ctx->options;
1340}
1341
1342uint32_t SSL_CTX_get_options(const SSL_CTX *ctx) { return ctx->options; }
1343
1344uint32_t SSL_set_options(SSL *ssl, uint32_t options) {
1345 ssl->options |= options;
1346 return ssl->options;
1347}
1348
1349uint32_t SSL_clear_options(SSL *ssl, uint32_t options) {
1350 ssl->options &= ~options;
1351 return ssl->options;
1352}
1353
1354uint32_t SSL_get_options(const SSL *ssl) { return ssl->options; }
1355
1356uint32_t SSL_CTX_set_mode(SSL_CTX *ctx, uint32_t mode) {
1357 ctx->mode |= mode;
1358 return ctx->mode;
1359}
1360
1361uint32_t SSL_CTX_clear_mode(SSL_CTX *ctx, uint32_t mode) {
1362 ctx->mode &= ~mode;
1363 return ctx->mode;
1364}
1365
1366uint32_t SSL_CTX_get_mode(const SSL_CTX *ctx) { return ctx->mode; }
1367
1368uint32_t SSL_set_mode(SSL *ssl, uint32_t mode) {
1369 ssl->mode |= mode;
1370 return ssl->mode;
1371}
1372
1373uint32_t SSL_clear_mode(SSL *ssl, uint32_t mode) {
1374 ssl->mode &= ~mode;
1375 return ssl->mode;
1376}
1377
1378uint32_t SSL_get_mode(const SSL *ssl) { return ssl->mode; }
1379
Steven Valdeze7531f02016-12-14 13:29:57 -05001380void SSL_CTX_set0_buffer_pool(SSL_CTX *ctx, CRYPTO_BUFFER_POOL *pool) {
1381 ctx->pool = pool;
1382}
1383
Kenny Rootb8494592015-09-25 02:29:14 +00001384int SSL_get_tls_unique(const SSL *ssl, uint8_t *out, size_t *out_len,
1385 size_t max_out) {
Robert Sloana12bf462017-07-17 07:08:26 -07001386 *out_len = 0;
1387 OPENSSL_memset(out, 0, max_out);
1388
Robert Sloana27a6a42017-09-05 08:39:28 -07001389 // tls-unique is not defined for SSL 3.0 or TLS 1.3.
David Benjamin95add822016-10-19 01:09:12 -04001390 if (!ssl->s3->initial_handshake_complete ||
Robert Sloan921ef2c2017-10-17 09:02:20 -07001391 ssl_protocol_version(ssl) < TLS1_VERSION ||
1392 ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
Robert Sloana12bf462017-07-17 07:08:26 -07001393 return 0;
David Benjamin95add822016-10-19 01:09:12 -04001394 }
1395
Robert Sloana27a6a42017-09-05 08:39:28 -07001396 // The tls-unique value is the first Finished message in the handshake, which
1397 // is the client's in a full handshake and the server's for a resumption. See
1398 // https://tools.ietf.org/html/rfc5929#section-3.1.
Kenny Rootb8494592015-09-25 02:29:14 +00001399 const uint8_t *finished = ssl->s3->previous_client_finished;
1400 size_t finished_len = ssl->s3->previous_client_finished_len;
David Benjaminc895d6b2016-08-11 13:26:41 -04001401 if (ssl->session != NULL) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001402 // tls-unique is broken for resumed sessions unless EMS is used.
Kenny Rootb8494592015-09-25 02:29:14 +00001403 if (!ssl->session->extended_master_secret) {
Robert Sloana12bf462017-07-17 07:08:26 -07001404 return 0;
Kenny Rootb8494592015-09-25 02:29:14 +00001405 }
1406 finished = ssl->s3->previous_server_finished;
1407 finished_len = ssl->s3->previous_server_finished_len;
1408 }
1409
Kenny Rootb8494592015-09-25 02:29:14 +00001410 *out_len = finished_len;
1411 if (finished_len > max_out) {
1412 *out_len = max_out;
1413 }
1414
Robert Sloan69939df2017-01-09 10:53:07 -08001415 OPENSSL_memcpy(out, finished, *out_len);
Kenny Rootb8494592015-09-25 02:29:14 +00001416 return 1;
Kenny Rootb8494592015-09-25 02:29:14 +00001417}
1418
Robert Sloana94fe052017-02-21 08:49:28 -08001419static int set_session_id_context(CERT *cert, const uint8_t *sid_ctx,
David Benjamin1b249672016-12-06 18:25:50 -05001420 size_t sid_ctx_len) {
Robert Sloana94fe052017-02-21 08:49:28 -08001421 if (sid_ctx_len > sizeof(cert->sid_ctx)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001422 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001423 return 0;
1424 }
David Benjamin1b249672016-12-06 18:25:50 -05001425
Robert Sloana12bf462017-07-17 07:08:26 -07001426 static_assert(sizeof(cert->sid_ctx) < 256, "sid_ctx too large");
Robert Sloana94fe052017-02-21 08:49:28 -08001427 cert->sid_ctx_length = (uint8_t)sid_ctx_len;
1428 OPENSSL_memcpy(cert->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001429 return 1;
1430}
1431
Robert Sloana94fe052017-02-21 08:49:28 -08001432int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
1433 size_t sid_ctx_len) {
1434 return set_session_id_context(ctx->cert, sid_ctx, sid_ctx_len);
1435}
1436
Adam Langleyd9e397b2015-01-22 14:27:53 -08001437int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
David Benjamin1b249672016-12-06 18:25:50 -05001438 size_t sid_ctx_len) {
Robert Sloana94fe052017-02-21 08:49:28 -08001439 return set_session_id_context(ssl->cert, sid_ctx, sid_ctx_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001440}
1441
David Benjaminf31229b2017-01-25 14:08:15 -05001442const uint8_t *SSL_get0_session_id_context(const SSL *ssl, size_t *out_len) {
Robert Sloana94fe052017-02-21 08:49:28 -08001443 *out_len = ssl->cert->sid_ctx_length;
1444 return ssl->cert->sid_ctx;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001445}
1446
Kenny Rootb8494592015-09-25 02:29:14 +00001447void SSL_certs_clear(SSL *ssl) { ssl_cert_clear_certs(ssl->cert); }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001448
Kenny Roote99801b2015-11-06 15:31:15 -08001449int SSL_get_fd(const SSL *ssl) { return SSL_get_rfd(ssl); }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001450
Kenny Roote99801b2015-11-06 15:31:15 -08001451int SSL_get_rfd(const SSL *ssl) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001452 int ret = -1;
Kenny Roote99801b2015-11-06 15:31:15 -08001453 BIO *b = BIO_find_type(SSL_get_rbio(ssl), BIO_TYPE_DESCRIPTOR);
1454 if (b != NULL) {
1455 BIO_get_fd(b, &ret);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001456 }
1457 return ret;
1458}
1459
Kenny Roote99801b2015-11-06 15:31:15 -08001460int SSL_get_wfd(const SSL *ssl) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001461 int ret = -1;
Kenny Roote99801b2015-11-06 15:31:15 -08001462 BIO *b = BIO_find_type(SSL_get_wbio(ssl), BIO_TYPE_DESCRIPTOR);
1463 if (b != NULL) {
1464 BIO_get_fd(b, &ret);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001465 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001466 return ret;
1467}
1468
Kenny Roote99801b2015-11-06 15:31:15 -08001469int SSL_set_fd(SSL *ssl, int fd) {
David Benjamin4969cc92016-04-22 15:02:23 -04001470 BIO *bio = BIO_new(BIO_s_socket());
Adam Langleyd9e397b2015-01-22 14:27:53 -08001471 if (bio == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001472 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
Kenny Roote99801b2015-11-06 15:31:15 -08001473 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001474 }
1475 BIO_set_fd(bio, fd, BIO_NOCLOSE);
Kenny Roote99801b2015-11-06 15:31:15 -08001476 SSL_set_bio(ssl, bio, bio);
1477 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001478}
1479
Kenny Roote99801b2015-11-06 15:31:15 -08001480int SSL_set_wfd(SSL *ssl, int fd) {
David Benjamind316cba2016-06-02 16:17:39 -04001481 BIO *rbio = SSL_get_rbio(ssl);
1482 if (rbio == NULL || BIO_method_type(rbio) != BIO_TYPE_SOCKET ||
1483 BIO_get_fd(rbio, NULL) != fd) {
David Benjamin4969cc92016-04-22 15:02:23 -04001484 BIO *bio = BIO_new(BIO_s_socket());
Adam Langleyd9e397b2015-01-22 14:27:53 -08001485 if (bio == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001486 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
Kenny Roote99801b2015-11-06 15:31:15 -08001487 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001488 }
1489 BIO_set_fd(bio, fd, BIO_NOCLOSE);
David Benjaminc895d6b2016-08-11 13:26:41 -04001490 SSL_set0_wbio(ssl, bio);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001491 } else {
Robert Sloana27a6a42017-09-05 08:39:28 -07001492 // Copy the rbio over to the wbio.
David Benjaminc895d6b2016-08-11 13:26:41 -04001493 BIO_up_ref(rbio);
1494 SSL_set0_wbio(ssl, rbio);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001495 }
1496
Kenny Roote99801b2015-11-06 15:31:15 -08001497 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001498}
1499
Kenny Roote99801b2015-11-06 15:31:15 -08001500int SSL_set_rfd(SSL *ssl, int fd) {
David Benjamind316cba2016-06-02 16:17:39 -04001501 BIO *wbio = SSL_get_wbio(ssl);
1502 if (wbio == NULL || BIO_method_type(wbio) != BIO_TYPE_SOCKET ||
1503 BIO_get_fd(wbio, NULL) != fd) {
David Benjamin4969cc92016-04-22 15:02:23 -04001504 BIO *bio = BIO_new(BIO_s_socket());
Adam Langleyd9e397b2015-01-22 14:27:53 -08001505 if (bio == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001506 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
Kenny Roote99801b2015-11-06 15:31:15 -08001507 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001508 }
1509 BIO_set_fd(bio, fd, BIO_NOCLOSE);
David Benjaminc895d6b2016-08-11 13:26:41 -04001510 SSL_set0_rbio(ssl, bio);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001511 } else {
Robert Sloana27a6a42017-09-05 08:39:28 -07001512 // Copy the wbio over to the rbio.
David Benjaminc895d6b2016-08-11 13:26:41 -04001513 BIO_up_ref(wbio);
1514 SSL_set0_rbio(ssl, wbio);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001515 }
Kenny Roote99801b2015-11-06 15:31:15 -08001516 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001517}
1518
David Benjamin95add822016-10-19 01:09:12 -04001519static size_t copy_finished(void *out, size_t out_len, const uint8_t *in,
1520 size_t in_len) {
1521 if (out_len > in_len) {
1522 out_len = in_len;
1523 }
Robert Sloan69939df2017-01-09 10:53:07 -08001524 OPENSSL_memcpy(out, in, out_len);
David Benjamin95add822016-10-19 01:09:12 -04001525 return in_len;
1526}
Adam Langleyd9e397b2015-01-22 14:27:53 -08001527
David Benjamin95add822016-10-19 01:09:12 -04001528size_t SSL_get_finished(const SSL *ssl, void *buf, size_t count) {
1529 if (!ssl->s3->initial_handshake_complete ||
Robert Sloan921ef2c2017-10-17 09:02:20 -07001530 ssl_protocol_version(ssl) < TLS1_VERSION ||
1531 ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjamin95add822016-10-19 01:09:12 -04001532 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001533 }
1534
David Benjamin95add822016-10-19 01:09:12 -04001535 if (ssl->server) {
1536 return copy_finished(buf, count, ssl->s3->previous_server_finished,
1537 ssl->s3->previous_server_finished_len);
1538 }
1539
1540 return copy_finished(buf, count, ssl->s3->previous_client_finished,
1541 ssl->s3->previous_client_finished_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001542}
1543
Kenny Roote99801b2015-11-06 15:31:15 -08001544size_t SSL_get_peer_finished(const SSL *ssl, void *buf, size_t count) {
David Benjamin95add822016-10-19 01:09:12 -04001545 if (!ssl->s3->initial_handshake_complete ||
Robert Sloan921ef2c2017-10-17 09:02:20 -07001546 ssl_protocol_version(ssl) < TLS1_VERSION ||
1547 ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjamin95add822016-10-19 01:09:12 -04001548 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001549 }
1550
David Benjamin95add822016-10-19 01:09:12 -04001551 if (ssl->server) {
1552 return copy_finished(buf, count, ssl->s3->previous_client_finished,
1553 ssl->s3->previous_client_finished_len);
1554 }
1555
1556 return copy_finished(buf, count, ssl->s3->previous_server_finished,
1557 ssl->s3->previous_server_finished_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001558}
1559
Kenny Rootb8494592015-09-25 02:29:14 +00001560int SSL_get_verify_mode(const SSL *ssl) { return ssl->verify_mode; }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001561
Kenny Rootb8494592015-09-25 02:29:14 +00001562int SSL_get_extms_support(const SSL *ssl) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001563 // TLS 1.3 does not require extended master secret and always reports as
1564 // supporting it.
David Benjaminc895d6b2016-08-11 13:26:41 -04001565 if (!ssl->s3->have_version) {
1566 return 0;
1567 }
Robert Sloan921ef2c2017-10-17 09:02:20 -07001568 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
Robert Sloana94fe052017-02-21 08:49:28 -08001569 return 1;
1570 }
1571
Robert Sloana27a6a42017-09-05 08:39:28 -07001572 // If the initial handshake completed, query the established session.
Robert Sloana94fe052017-02-21 08:49:28 -08001573 if (ssl->s3->established_session != NULL) {
1574 return ssl->s3->established_session->extended_master_secret;
1575 }
1576
Robert Sloana27a6a42017-09-05 08:39:28 -07001577 // Otherwise, query the in-progress handshake.
Robert Sloana94fe052017-02-21 08:49:28 -08001578 if (ssl->s3->hs != NULL) {
1579 return ssl->s3->hs->extended_master_secret;
1580 }
1581 assert(0);
1582 return 0;
Kenny Rootb8494592015-09-25 02:29:14 +00001583}
1584
Adam Langleyf4e42722015-06-04 17:45:09 -07001585int SSL_CTX_get_read_ahead(const SSL_CTX *ctx) { return 0; }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001586
Adam Langley4139edb2016-01-13 15:00:54 -08001587int SSL_get_read_ahead(const SSL *ssl) { return 0; }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001588
Adam Langleyf4e42722015-06-04 17:45:09 -07001589void SSL_CTX_set_read_ahead(SSL_CTX *ctx, int yes) { }
Adam Langleye9ada862015-05-11 17:20:37 -07001590
Adam Langley4139edb2016-01-13 15:00:54 -08001591void SSL_set_read_ahead(SSL *ssl, int yes) { }
Adam Langleye9ada862015-05-11 17:20:37 -07001592
Kenny Roote99801b2015-11-06 15:31:15 -08001593int SSL_pending(const SSL *ssl) {
Robert Sloan36272962017-10-23 10:28:39 -07001594 return static_cast<int>(ssl->s3->pending_app_data.size());
Adam Langleyd9e397b2015-01-22 14:27:53 -08001595}
1596
Robert Sloana27a6a42017-09-05 08:39:28 -07001597// Fix this so it checks all the valid key/cert options
Adam Langleyd9e397b2015-01-22 14:27:53 -08001598int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
Robert Sloan5cbb5c82018-04-24 11:35:46 -07001599 return ssl_cert_check_private_key(ctx->cert, ctx->cert->privatekey.get());
Steven Valdez909b19f2016-11-21 15:35:44 -05001600}
1601
Robert Sloana27a6a42017-09-05 08:39:28 -07001602// Fix this function so that it takes an optional type parameter
Steven Valdez909b19f2016-11-21 15:35:44 -05001603int SSL_check_private_key(const SSL *ssl) {
Robert Sloan5cbb5c82018-04-24 11:35:46 -07001604 return ssl_cert_check_private_key(ssl->cert, ssl->cert->privatekey.get());
Adam Langleyd9e397b2015-01-22 14:27:53 -08001605}
1606
Kenny Rootb8494592015-09-25 02:29:14 +00001607long SSL_get_default_timeout(const SSL *ssl) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001608 return SSL_DEFAULT_SESSION_TIMEOUT;
1609}
1610
Adam Langleyf4e42722015-06-04 17:45:09 -07001611int SSL_renegotiate(SSL *ssl) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001612 // Caller-initiated renegotiation is not supported.
Kenny Rootb8494592015-09-25 02:29:14 +00001613 OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
Adam Langleyf4e42722015-06-04 17:45:09 -07001614 return 0;
1615}
1616
1617int SSL_renegotiate_pending(SSL *ssl) {
1618 return SSL_in_init(ssl) && ssl->s3->initial_handshake_complete;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001619}
1620
David Benjamin6e899c72016-06-09 18:02:18 -04001621int SSL_total_renegotiations(const SSL *ssl) {
1622 return ssl->s3->total_renegotiations;
1623}
1624
Adam Langleye9ada862015-05-11 17:20:37 -07001625size_t SSL_CTX_get_max_cert_list(const SSL_CTX *ctx) {
1626 return ctx->max_cert_list;
1627}
1628
1629void SSL_CTX_set_max_cert_list(SSL_CTX *ctx, size_t max_cert_list) {
1630 if (max_cert_list > kMaxHandshakeSize) {
1631 max_cert_list = kMaxHandshakeSize;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001632 }
Adam Langleye9ada862015-05-11 17:20:37 -07001633 ctx->max_cert_list = (uint32_t)max_cert_list;
1634}
1635
1636size_t SSL_get_max_cert_list(const SSL *ssl) {
1637 return ssl->max_cert_list;
1638}
1639
1640void SSL_set_max_cert_list(SSL *ssl, size_t max_cert_list) {
1641 if (max_cert_list > kMaxHandshakeSize) {
1642 max_cert_list = kMaxHandshakeSize;
1643 }
1644 ssl->max_cert_list = (uint32_t)max_cert_list;
1645}
1646
David Benjamin4969cc92016-04-22 15:02:23 -04001647int SSL_CTX_set_max_send_fragment(SSL_CTX *ctx, size_t max_send_fragment) {
Adam Langleye9ada862015-05-11 17:20:37 -07001648 if (max_send_fragment < 512) {
1649 max_send_fragment = 512;
1650 }
1651 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1652 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1653 }
1654 ctx->max_send_fragment = (uint16_t)max_send_fragment;
David Benjamin4969cc92016-04-22 15:02:23 -04001655
1656 return 1;
Adam Langleye9ada862015-05-11 17:20:37 -07001657}
1658
David Benjamin4969cc92016-04-22 15:02:23 -04001659int SSL_set_max_send_fragment(SSL *ssl, size_t max_send_fragment) {
Adam Langleye9ada862015-05-11 17:20:37 -07001660 if (max_send_fragment < 512) {
1661 max_send_fragment = 512;
1662 }
1663 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1664 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1665 }
1666 ssl->max_send_fragment = (uint16_t)max_send_fragment;
David Benjamin4969cc92016-04-22 15:02:23 -04001667
1668 return 1;
Adam Langleye9ada862015-05-11 17:20:37 -07001669}
1670
1671int SSL_set_mtu(SSL *ssl, unsigned mtu) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001672 if (!SSL_is_dtls(ssl) || mtu < dtls1_min_mtu()) {
Adam Langleye9ada862015-05-11 17:20:37 -07001673 return 0;
1674 }
1675 ssl->d1->mtu = mtu;
1676 return 1;
1677}
1678
1679int SSL_get_secure_renegotiation_support(const SSL *ssl) {
Steven Valdezb0b45c62017-01-17 16:23:54 -05001680 if (!ssl->s3->have_version) {
1681 return 0;
1682 }
Robert Sloan921ef2c2017-10-17 09:02:20 -07001683 return ssl_protocol_version(ssl) >= TLS1_3_VERSION ||
Steven Valdezb0b45c62017-01-17 16:23:54 -05001684 ssl->s3->send_connection_binding;
Adam Langleye9ada862015-05-11 17:20:37 -07001685}
1686
Adam Langleye9ada862015-05-11 17:20:37 -07001687size_t SSL_CTX_sess_number(const SSL_CTX *ctx) {
Robert Sloan4562e9d2017-10-02 10:26:51 -07001688 MutexReadLock lock(const_cast<CRYPTO_MUTEX *>(&ctx->lock));
Adam Langleye9ada862015-05-11 17:20:37 -07001689 return lh_SSL_SESSION_num_items(ctx->sessions);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001690}
1691
Adam Langleye9ada862015-05-11 17:20:37 -07001692unsigned long SSL_CTX_sess_set_cache_size(SSL_CTX *ctx, unsigned long size) {
1693 unsigned long ret = ctx->session_cache_size;
1694 ctx->session_cache_size = size;
1695 return ret;
1696}
Adam Langleyd9e397b2015-01-22 14:27:53 -08001697
Adam Langleye9ada862015-05-11 17:20:37 -07001698unsigned long SSL_CTX_sess_get_cache_size(const SSL_CTX *ctx) {
1699 return ctx->session_cache_size;
1700}
1701
1702int SSL_CTX_set_session_cache_mode(SSL_CTX *ctx, int mode) {
1703 int ret = ctx->session_cache_mode;
1704 ctx->session_cache_mode = mode;
1705 return ret;
1706}
1707
1708int SSL_CTX_get_session_cache_mode(const SSL_CTX *ctx) {
1709 return ctx->session_cache_mode;
1710}
1711
David Benjamin6e899c72016-06-09 18:02:18 -04001712
1713int SSL_CTX_get_tlsext_ticket_keys(SSL_CTX *ctx, void *out, size_t len) {
1714 if (out == NULL) {
1715 return 48;
1716 }
1717 if (len != 48) {
1718 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH);
1719 return 0;
1720 }
Robert Sloan8f860b12017-08-28 07:37:06 -07001721
Robert Sloana27a6a42017-09-05 08:39:28 -07001722 // The default ticket keys are initialized lazily. Trigger a key
1723 // rotation to initialize them.
Robert Sloan8f860b12017-08-28 07:37:06 -07001724 if (!ssl_ctx_rotate_ticket_encryption_key(ctx)) {
1725 return 0;
1726 }
1727
Robert Sloana12bf462017-07-17 07:08:26 -07001728 uint8_t *out_bytes = reinterpret_cast<uint8_t *>(out);
Robert Sloan8f860b12017-08-28 07:37:06 -07001729 MutexReadLock lock(&ctx->lock);
1730 OPENSSL_memcpy(out_bytes, ctx->tlsext_ticket_key_current->name, 16);
1731 OPENSSL_memcpy(out_bytes + 16, ctx->tlsext_ticket_key_current->hmac_key, 16);
1732 OPENSSL_memcpy(out_bytes + 32, ctx->tlsext_ticket_key_current->aes_key, 16);
David Benjamin6e899c72016-06-09 18:02:18 -04001733 return 1;
1734}
1735
1736int SSL_CTX_set_tlsext_ticket_keys(SSL_CTX *ctx, const void *in, size_t len) {
1737 if (in == NULL) {
1738 return 48;
1739 }
1740 if (len != 48) {
1741 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH);
1742 return 0;
1743 }
Robert Sloan8f860b12017-08-28 07:37:06 -07001744 if (!ctx->tlsext_ticket_key_current) {
1745 ctx->tlsext_ticket_key_current =
1746 (tlsext_ticket_key *)OPENSSL_malloc(sizeof(tlsext_ticket_key));
1747 if (!ctx->tlsext_ticket_key_current) {
1748 return 0;
1749 }
1750 }
1751 OPENSSL_memset(ctx->tlsext_ticket_key_current, 0, sizeof(tlsext_ticket_key));
Robert Sloana12bf462017-07-17 07:08:26 -07001752 const uint8_t *in_bytes = reinterpret_cast<const uint8_t *>(in);
Robert Sloan8f860b12017-08-28 07:37:06 -07001753 OPENSSL_memcpy(ctx->tlsext_ticket_key_current->name, in_bytes, 16);
1754 OPENSSL_memcpy(ctx->tlsext_ticket_key_current->hmac_key, in_bytes + 16, 16);
1755 OPENSSL_memcpy(ctx->tlsext_ticket_key_current->aes_key, in_bytes + 32, 16);
1756 OPENSSL_free(ctx->tlsext_ticket_key_prev);
1757 ctx->tlsext_ticket_key_prev = nullptr;
Robert Sloana27a6a42017-09-05 08:39:28 -07001758 // Disable automatic key rotation.
Robert Sloan8f860b12017-08-28 07:37:06 -07001759 ctx->tlsext_ticket_key_current->next_rotation_tv_sec = 0;
David Benjamin6e899c72016-06-09 18:02:18 -04001760 return 1;
1761}
1762
1763int SSL_CTX_set_tlsext_ticket_key_cb(
1764 SSL_CTX *ctx, int (*callback)(SSL *ssl, uint8_t *key_name, uint8_t *iv,
1765 EVP_CIPHER_CTX *ctx, HMAC_CTX *hmac_ctx,
1766 int encrypt)) {
1767 ctx->tlsext_ticket_key_cb = callback;
1768 return 1;
1769}
1770
1771int SSL_CTX_set1_curves(SSL_CTX *ctx, const int *curves, size_t curves_len) {
1772 return tls1_set_curves(&ctx->supported_group_list,
1773 &ctx->supported_group_list_len, curves,
1774 curves_len);
1775}
1776
1777int SSL_set1_curves(SSL *ssl, const int *curves, size_t curves_len) {
1778 return tls1_set_curves(&ssl->supported_group_list,
1779 &ssl->supported_group_list_len, curves,
1780 curves_len);
1781}
1782
Steven Valdezbb1ceac2016-10-07 10:34:51 -04001783int SSL_CTX_set1_curves_list(SSL_CTX *ctx, const char *curves) {
1784 return tls1_set_curves_list(&ctx->supported_group_list,
1785 &ctx->supported_group_list_len, curves);
1786}
1787
1788int SSL_set1_curves_list(SSL *ssl, const char *curves) {
1789 return tls1_set_curves_list(&ssl->supported_group_list,
1790 &ssl->supported_group_list_len, curves);
1791}
1792
David Benjaminc895d6b2016-08-11 13:26:41 -04001793uint16_t SSL_get_curve_id(const SSL *ssl) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001794 // TODO(davidben): This checks the wrong session if there is a renegotiation
1795 // in progress.
David Benjaminc895d6b2016-08-11 13:26:41 -04001796 SSL_SESSION *session = SSL_get_session(ssl);
Steven Valdeze7531f02016-12-14 13:29:57 -05001797 if (session == NULL) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001798 return 0;
1799 }
1800
Steven Valdeze7531f02016-12-14 13:29:57 -05001801 return session->group_id;
David Benjaminc895d6b2016-08-11 13:26:41 -04001802}
1803
David Benjamin6e899c72016-06-09 18:02:18 -04001804int SSL_CTX_set_tmp_dh(SSL_CTX *ctx, const DH *dh) {
David Benjamin6e899c72016-06-09 18:02:18 -04001805 return 1;
1806}
1807
1808int SSL_set_tmp_dh(SSL *ssl, const DH *dh) {
David Benjamin6e899c72016-06-09 18:02:18 -04001809 return 1;
1810}
1811
Robert Sloan84377092017-08-14 09:33:19 -07001812STACK_OF(SSL_CIPHER) *SSL_CTX_get_ciphers(const SSL_CTX *ctx) {
Robert Sloanc6ebb282018-04-30 10:10:26 -07001813 return ctx->cipher_list->ciphers.get();
Robert Sloan7d422bc2017-03-06 10:04:29 -08001814}
1815
Robert Sloan84377092017-08-14 09:33:19 -07001816int SSL_CTX_cipher_in_group(const SSL_CTX *ctx, size_t i) {
Robert Sloanc6ebb282018-04-30 10:10:26 -07001817 if (i >= sk_SSL_CIPHER_num(ctx->cipher_list->ciphers.get())) {
Robert Sloan84377092017-08-14 09:33:19 -07001818 return 0;
1819 }
1820 return ctx->cipher_list->in_group_flags[i];
1821}
1822
Kenny Roote99801b2015-11-06 15:31:15 -08001823STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *ssl) {
1824 if (ssl == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001825 return NULL;
1826 }
1827
Robert Sloanc6ebb282018-04-30 10:10:26 -07001828 const SSLCipherPreferenceList *prefs = ssl_get_cipher_preferences(ssl);
1829 return prefs == nullptr ? nullptr : prefs->ciphers.get();
Adam Langleyd9e397b2015-01-22 14:27:53 -08001830}
1831
Kenny Roote99801b2015-11-06 15:31:15 -08001832const char *SSL_get_cipher_list(const SSL *ssl, int n) {
Kenny Roote99801b2015-11-06 15:31:15 -08001833 if (ssl == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001834 return NULL;
1835 }
1836
Robert Sloan7d422bc2017-03-06 10:04:29 -08001837 STACK_OF(SSL_CIPHER) *sk = SSL_get_ciphers(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001838 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
1839 return NULL;
1840 }
1841
Robert Sloan7d422bc2017-03-06 10:04:29 -08001842 const SSL_CIPHER *c = sk_SSL_CIPHER_value(sk, n);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001843 if (c == NULL) {
1844 return NULL;
1845 }
1846
1847 return c->name;
1848}
1849
Adam Langleyd9e397b2015-01-22 14:27:53 -08001850int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
Robert Sloanb1b54b82017-11-06 13:50:02 -08001851 return ssl_create_cipher_list(&ctx->cipher_list, str, false /* not strict */);
Robert Sloan7c50ec52017-02-27 08:17:21 -08001852}
1853
1854int SSL_CTX_set_strict_cipher_list(SSL_CTX *ctx, const char *str) {
Robert Sloanb1b54b82017-11-06 13:50:02 -08001855 return ssl_create_cipher_list(&ctx->cipher_list, str, true /* strict */);
Kenny Rootb8494592015-09-25 02:29:14 +00001856}
1857
Kenny Roote99801b2015-11-06 15:31:15 -08001858int SSL_set_cipher_list(SSL *ssl, const char *str) {
Robert Sloanb1b54b82017-11-06 13:50:02 -08001859 return ssl_create_cipher_list(&ssl->cipher_list, str, false /* not strict */);
Robert Sloan7c50ec52017-02-27 08:17:21 -08001860}
1861
1862int SSL_set_strict_cipher_list(SSL *ssl, const char *str) {
Robert Sloanb1b54b82017-11-06 13:50:02 -08001863 return ssl_create_cipher_list(&ssl->cipher_list, str, true /* strict */);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001864}
1865
Kenny Roote99801b2015-11-06 15:31:15 -08001866const char *SSL_get_servername(const SSL *ssl, const int type) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001867 if (type != TLSEXT_NAMETYPE_host_name) {
1868 return NULL;
1869 }
1870
Robert Sloana27a6a42017-09-05 08:39:28 -07001871 // Historically, |SSL_get_servername| was also the configuration getter
1872 // corresponding to |SSL_set_tlsext_host_name|.
Kenny Roote99801b2015-11-06 15:31:15 -08001873 if (ssl->tlsext_hostname != NULL) {
1874 return ssl->tlsext_hostname;
1875 }
1876
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001877 return ssl->s3->hostname.get();
Adam Langleyd9e397b2015-01-22 14:27:53 -08001878}
1879
Kenny Roote99801b2015-11-06 15:31:15 -08001880int SSL_get_servername_type(const SSL *ssl) {
Robert Sloand1d118f2017-09-11 09:00:48 -07001881 if (SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) == NULL) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001882 return -1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001883 }
David Benjaminc895d6b2016-08-11 13:26:41 -04001884 return TLSEXT_NAMETYPE_host_name;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001885}
1886
Robert Sloanb6d070c2017-07-24 08:40:01 -07001887void SSL_CTX_set_custom_verify(
1888 SSL_CTX *ctx, int mode,
1889 enum ssl_verify_result_t (*callback)(SSL *ssl, uint8_t *out_alert)) {
1890 ctx->verify_mode = mode;
1891 ctx->custom_verify_callback = callback;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001892}
1893
Robert Sloanb6d070c2017-07-24 08:40:01 -07001894void SSL_set_custom_verify(
1895 SSL *ssl, int mode,
1896 enum ssl_verify_result_t (*callback)(SSL *ssl, uint8_t *out_alert)) {
1897 ssl->verify_mode = mode;
1898 ssl->custom_verify_callback = callback;
1899}
1900
1901void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001902 ctx->signed_cert_timestamps_enabled = true;
Robert Sloan1c9db532017-03-13 08:03:59 -07001903}
1904
Robert Sloana94fe052017-02-21 08:49:28 -08001905void SSL_enable_signed_cert_timestamps(SSL *ssl) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001906 ssl->signed_cert_timestamps_enabled = true;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001907}
1908
1909void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001910 ctx->ocsp_stapling_enabled = true;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001911}
1912
Robert Sloana94fe052017-02-21 08:49:28 -08001913void SSL_enable_ocsp_stapling(SSL *ssl) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001914 ssl->ocsp_stapling_enabled = true;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001915}
1916
1917void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
1918 size_t *out_len) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001919 SSL_SESSION *session = SSL_get_session(ssl);
Robert Sloan8f860b12017-08-28 07:37:06 -07001920 if (ssl->server || !session || !session->signed_cert_timestamp_list) {
1921 *out_len = 0;
1922 *out = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001923 return;
1924 }
1925
Robert Sloan8f860b12017-08-28 07:37:06 -07001926 *out = CRYPTO_BUFFER_data(session->signed_cert_timestamp_list);
1927 *out_len = CRYPTO_BUFFER_len(session->signed_cert_timestamp_list);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001928}
1929
1930void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
1931 size_t *out_len) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001932 SSL_SESSION *session = SSL_get_session(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001933 if (ssl->server || !session || !session->ocsp_response) {
Robert Sloan8f860b12017-08-28 07:37:06 -07001934 *out_len = 0;
1935 *out = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001936 return;
1937 }
Robert Sloan8f860b12017-08-28 07:37:06 -07001938
1939 *out = CRYPTO_BUFFER_data(session->ocsp_response);
1940 *out_len = CRYPTO_BUFFER_len(session->ocsp_response);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001941}
1942
David Benjamin6e899c72016-06-09 18:02:18 -04001943int SSL_set_tlsext_host_name(SSL *ssl, const char *name) {
1944 OPENSSL_free(ssl->tlsext_hostname);
1945 ssl->tlsext_hostname = NULL;
1946
1947 if (name == NULL) {
1948 return 1;
1949 }
1950
1951 size_t len = strlen(name);
1952 if (len == 0 || len > TLSEXT_MAXLEN_host_name) {
1953 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL3_EXT_INVALID_SERVERNAME);
1954 return 0;
1955 }
1956 ssl->tlsext_hostname = BUF_strdup(name);
1957 if (ssl->tlsext_hostname == NULL) {
1958 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1959 return 0;
1960 }
1961 return 1;
1962}
1963
1964int SSL_CTX_set_tlsext_servername_callback(
1965 SSL_CTX *ctx, int (*callback)(SSL *ssl, int *out_alert, void *arg)) {
1966 ctx->tlsext_servername_callback = callback;
1967 return 1;
1968}
1969
1970int SSL_CTX_set_tlsext_servername_arg(SSL_CTX *ctx, void *arg) {
1971 ctx->tlsext_servername_arg = arg;
1972 return 1;
1973}
1974
Robert Sloane56da3e2017-06-26 08:26:42 -07001975int SSL_select_next_proto(uint8_t **out, uint8_t *out_len, const uint8_t *peer,
1976 unsigned peer_len, const uint8_t *supported,
1977 unsigned supported_len) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001978 const uint8_t *result;
Robert Sloane56da3e2017-06-26 08:26:42 -07001979 int status;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001980
Robert Sloana27a6a42017-09-05 08:39:28 -07001981 // For each protocol in peer preference order, see if we support it.
Robert Sloane56da3e2017-06-26 08:26:42 -07001982 for (unsigned i = 0; i < peer_len;) {
1983 for (unsigned j = 0; j < supported_len;) {
1984 if (peer[i] == supported[j] &&
1985 OPENSSL_memcmp(&peer[i + 1], &supported[j + 1], peer[i]) == 0) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001986 // We found a match
Robert Sloane56da3e2017-06-26 08:26:42 -07001987 result = &peer[i];
Adam Langleyd9e397b2015-01-22 14:27:53 -08001988 status = OPENSSL_NPN_NEGOTIATED;
1989 goto found;
1990 }
Robert Sloane56da3e2017-06-26 08:26:42 -07001991 j += supported[j];
Adam Langleyd9e397b2015-01-22 14:27:53 -08001992 j++;
1993 }
Robert Sloane56da3e2017-06-26 08:26:42 -07001994 i += peer[i];
Adam Langleyd9e397b2015-01-22 14:27:53 -08001995 i++;
1996 }
1997
Robert Sloana27a6a42017-09-05 08:39:28 -07001998 // There's no overlap between our protocols and the peer's list.
Robert Sloane56da3e2017-06-26 08:26:42 -07001999 result = supported;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002000 status = OPENSSL_NPN_NO_OVERLAP;
2001
2002found:
2003 *out = (uint8_t *)result + 1;
Kenny Rootb8494592015-09-25 02:29:14 +00002004 *out_len = result[0];
Adam Langleyd9e397b2015-01-22 14:27:53 -08002005 return status;
2006}
2007
Kenny Rootb8494592015-09-25 02:29:14 +00002008void SSL_get0_next_proto_negotiated(const SSL *ssl, const uint8_t **out_data,
2009 unsigned *out_len) {
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002010 *out_data = ssl->s3->next_proto_negotiated.data();
2011 *out_len = ssl->s3->next_proto_negotiated.size();
Adam Langleyd9e397b2015-01-22 14:27:53 -08002012}
2013
Adam Langleyd9e397b2015-01-22 14:27:53 -08002014void SSL_CTX_set_next_protos_advertised_cb(
2015 SSL_CTX *ctx,
Kenny Rootb8494592015-09-25 02:29:14 +00002016 int (*cb)(SSL *ssl, const uint8_t **out, unsigned *out_len, void *arg),
Adam Langleyd9e397b2015-01-22 14:27:53 -08002017 void *arg) {
2018 ctx->next_protos_advertised_cb = cb;
2019 ctx->next_protos_advertised_cb_arg = arg;
2020}
2021
Adam Langleyd9e397b2015-01-22 14:27:53 -08002022void SSL_CTX_set_next_proto_select_cb(
Kenny Rootb8494592015-09-25 02:29:14 +00002023 SSL_CTX *ctx, int (*cb)(SSL *ssl, uint8_t **out, uint8_t *out_len,
2024 const uint8_t *in, unsigned in_len, void *arg),
Adam Langleyd9e397b2015-01-22 14:27:53 -08002025 void *arg) {
2026 ctx->next_proto_select_cb = cb;
2027 ctx->next_proto_select_cb_arg = arg;
2028}
2029
Adam Langleyd9e397b2015-01-22 14:27:53 -08002030int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
2031 unsigned protos_len) {
Adam Langleye9ada862015-05-11 17:20:37 -07002032 OPENSSL_free(ctx->alpn_client_proto_list);
Robert Sloana12bf462017-07-17 07:08:26 -07002033 ctx->alpn_client_proto_list = (uint8_t *)BUF_memdup(protos, protos_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002034 if (!ctx->alpn_client_proto_list) {
2035 return 1;
2036 }
2037 ctx->alpn_client_proto_list_len = protos_len;
2038
2039 return 0;
2040}
2041
Adam Langleyd9e397b2015-01-22 14:27:53 -08002042int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
Adam Langleye9ada862015-05-11 17:20:37 -07002043 OPENSSL_free(ssl->alpn_client_proto_list);
Robert Sloana12bf462017-07-17 07:08:26 -07002044 ssl->alpn_client_proto_list = (uint8_t *)BUF_memdup(protos, protos_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002045 if (!ssl->alpn_client_proto_list) {
2046 return 1;
2047 }
2048 ssl->alpn_client_proto_list_len = protos_len;
2049
2050 return 0;
2051}
2052
Adam Langleyd9e397b2015-01-22 14:27:53 -08002053void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
2054 int (*cb)(SSL *ssl, const uint8_t **out,
Kenny Rootb8494592015-09-25 02:29:14 +00002055 uint8_t *out_len, const uint8_t *in,
2056 unsigned in_len, void *arg),
Adam Langleyd9e397b2015-01-22 14:27:53 -08002057 void *arg) {
2058 ctx->alpn_select_cb = cb;
2059 ctx->alpn_select_cb_arg = arg;
2060}
2061
Kenny Rootb8494592015-09-25 02:29:14 +00002062void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **out_data,
2063 unsigned *out_len) {
Robert Sloane56da3e2017-06-26 08:26:42 -07002064 if (SSL_in_early_data(ssl) && !ssl->server) {
2065 *out_data = ssl->s3->hs->early_session->early_alpn;
2066 *out_len = ssl->s3->hs->early_session->early_alpn_len;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002067 } else {
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002068 *out_data = ssl->s3->alpn_selected.data();
2069 *out_len = ssl->s3->alpn_selected.size();
Adam Langleyd9e397b2015-01-22 14:27:53 -08002070 }
2071}
2072
Robert Sloan572a4e22017-04-17 10:52:19 -07002073void SSL_CTX_set_allow_unknown_alpn_protos(SSL_CTX *ctx, int enabled) {
2074 ctx->allow_unknown_alpn_protos = !!enabled;
2075}
David Benjamin6e899c72016-06-09 18:02:18 -04002076
David Benjamin1b249672016-12-06 18:25:50 -05002077void SSL_CTX_set_tls_channel_id_enabled(SSL_CTX *ctx, int enabled) {
2078 ctx->tlsext_channel_id_enabled = !!enabled;
2079}
2080
David Benjamin6e899c72016-06-09 18:02:18 -04002081int SSL_CTX_enable_tls_channel_id(SSL_CTX *ctx) {
David Benjamin1b249672016-12-06 18:25:50 -05002082 SSL_CTX_set_tls_channel_id_enabled(ctx, 1);
David Benjamin6e899c72016-06-09 18:02:18 -04002083 return 1;
2084}
2085
David Benjamin1b249672016-12-06 18:25:50 -05002086void SSL_set_tls_channel_id_enabled(SSL *ssl, int enabled) {
2087 ssl->tlsext_channel_id_enabled = !!enabled;
2088}
2089
David Benjamin6e899c72016-06-09 18:02:18 -04002090int SSL_enable_tls_channel_id(SSL *ssl) {
David Benjamin1b249672016-12-06 18:25:50 -05002091 SSL_set_tls_channel_id_enabled(ssl, 1);
David Benjamin6e899c72016-06-09 18:02:18 -04002092 return 1;
2093}
2094
2095static int is_p256_key(EVP_PKEY *private_key) {
2096 const EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(private_key);
2097 return ec_key != NULL &&
2098 EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key)) ==
2099 NID_X9_62_prime256v1;
2100}
2101
2102int SSL_CTX_set1_tls_channel_id(SSL_CTX *ctx, EVP_PKEY *private_key) {
2103 if (!is_p256_key(private_key)) {
2104 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256);
2105 return 0;
2106 }
2107
2108 EVP_PKEY_free(ctx->tlsext_channel_id_private);
David Benjaminc895d6b2016-08-11 13:26:41 -04002109 EVP_PKEY_up_ref(private_key);
2110 ctx->tlsext_channel_id_private = private_key;
Robert Sloan921ef2c2017-10-17 09:02:20 -07002111 ctx->tlsext_channel_id_enabled = true;
David Benjamin6e899c72016-06-09 18:02:18 -04002112
2113 return 1;
2114}
2115
2116int SSL_set1_tls_channel_id(SSL *ssl, EVP_PKEY *private_key) {
2117 if (!is_p256_key(private_key)) {
2118 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256);
2119 return 0;
2120 }
2121
2122 EVP_PKEY_free(ssl->tlsext_channel_id_private);
David Benjaminc895d6b2016-08-11 13:26:41 -04002123 EVP_PKEY_up_ref(private_key);
2124 ssl->tlsext_channel_id_private = private_key;
Robert Sloan921ef2c2017-10-17 09:02:20 -07002125 ssl->tlsext_channel_id_enabled = true;
David Benjamin6e899c72016-06-09 18:02:18 -04002126
2127 return 1;
2128}
2129
2130size_t SSL_get_tls_channel_id(SSL *ssl, uint8_t *out, size_t max_out) {
2131 if (!ssl->s3->tlsext_channel_id_valid) {
2132 return 0;
2133 }
Robert Sloan69939df2017-01-09 10:53:07 -08002134 OPENSSL_memcpy(out, ssl->s3->tlsext_channel_id,
2135 (max_out < 64) ? max_out : 64);
David Benjamin6e899c72016-06-09 18:02:18 -04002136 return 64;
2137}
2138
Robert Sloan978112c2018-01-22 12:53:01 -08002139int SSL_set_token_binding_params(SSL *ssl, const uint8_t *params, size_t len) {
2140 if (len > 256) {
2141 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
2142 return 0;
2143 }
2144 OPENSSL_free(ssl->token_binding_params);
2145 ssl->token_binding_params = (uint8_t *)BUF_memdup(params, len);
2146 if (!ssl->token_binding_params) {
2147 return 0;
2148 }
2149 ssl->token_binding_params_len = len;
2150 return 1;
2151}
2152
2153int SSL_is_token_binding_negotiated(const SSL *ssl) {
Robert Sloan15c0b352018-04-16 08:36:46 -07002154 return ssl->s3->token_binding_negotiated;
Robert Sloan978112c2018-01-22 12:53:01 -08002155}
2156
2157uint8_t SSL_get_negotiated_token_binding_param(const SSL *ssl) {
Robert Sloan15c0b352018-04-16 08:36:46 -07002158 return ssl->s3->negotiated_token_binding_param;
Robert Sloan978112c2018-01-22 12:53:01 -08002159}
2160
David Benjamin6e899c72016-06-09 18:02:18 -04002161size_t SSL_get0_certificate_types(SSL *ssl, const uint8_t **out_types) {
David Benjamin95add822016-10-19 01:09:12 -04002162 if (ssl->server || ssl->s3->hs == NULL) {
David Benjamin6e899c72016-06-09 18:02:18 -04002163 *out_types = NULL;
2164 return 0;
2165 }
Robert Sloan4562e9d2017-10-02 10:26:51 -07002166 *out_types = ssl->s3->hs->certificate_types.data();
2167 return ssl->s3->hs->certificate_types.size();
David Benjamin6e899c72016-06-09 18:02:18 -04002168}
2169
Adam Langley4139edb2016-01-13 15:00:54 -08002170EVP_PKEY *SSL_get_privatekey(const SSL *ssl) {
2171 if (ssl->cert != NULL) {
Robert Sloan5cbb5c82018-04-24 11:35:46 -07002172 return ssl->cert->privatekey.get();
Adam Langleyd9e397b2015-01-22 14:27:53 -08002173 }
2174
2175 return NULL;
2176}
2177
Adam Langleyd9e397b2015-01-22 14:27:53 -08002178EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
2179 if (ctx->cert != NULL) {
Robert Sloan5cbb5c82018-04-24 11:35:46 -07002180 return ctx->cert->privatekey.get();
Adam Langleyd9e397b2015-01-22 14:27:53 -08002181 }
2182
2183 return NULL;
2184}
2185
Kenny Rootb8494592015-09-25 02:29:14 +00002186const SSL_CIPHER *SSL_get_current_cipher(const SSL *ssl) {
Robert Sloanb6d070c2017-07-24 08:40:01 -07002187 return ssl->s3->aead_write_ctx->cipher();
Adam Langleyd9e397b2015-01-22 14:27:53 -08002188}
2189
David Benjamin6e899c72016-06-09 18:02:18 -04002190int SSL_session_reused(const SSL *ssl) {
Robert Sloane56da3e2017-06-26 08:26:42 -07002191 return ssl->s3->session_reused || SSL_in_early_data(ssl);
David Benjamin6e899c72016-06-09 18:02:18 -04002192}
2193
Adam Langley4139edb2016-01-13 15:00:54 -08002194const COMP_METHOD *SSL_get_current_compression(SSL *ssl) { return NULL; }
Adam Langleyd9e397b2015-01-22 14:27:53 -08002195
Adam Langley4139edb2016-01-13 15:00:54 -08002196const COMP_METHOD *SSL_get_current_expansion(SSL *ssl) { return NULL; }
Adam Langleyd9e397b2015-01-22 14:27:53 -08002197
David Benjamin4969cc92016-04-22 15:02:23 -04002198int *SSL_get_server_tmp_key(SSL *ssl, EVP_PKEY **out_key) { return 0; }
2199
Adam Langleyd9e397b2015-01-22 14:27:53 -08002200void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
Adam Langleyfad63272015-11-12 12:15:39 -08002201 ctx->quiet_shutdown = (mode != 0);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002202}
2203
2204int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
2205 return ctx->quiet_shutdown;
2206}
2207
Adam Langleyfad63272015-11-12 12:15:39 -08002208void SSL_set_quiet_shutdown(SSL *ssl, int mode) {
2209 ssl->quiet_shutdown = (mode != 0);
2210}
Adam Langleyd9e397b2015-01-22 14:27:53 -08002211
Kenny Roote99801b2015-11-06 15:31:15 -08002212int SSL_get_quiet_shutdown(const SSL *ssl) { return ssl->quiet_shutdown; }
Adam Langleyd9e397b2015-01-22 14:27:53 -08002213
Kenny Roote99801b2015-11-06 15:31:15 -08002214void SSL_set_shutdown(SSL *ssl, int mode) {
Robert Sloana27a6a42017-09-05 08:39:28 -07002215 // It is an error to clear any bits that have already been set. (We can't try
2216 // to get a second close_notify or send two.)
David Benjamind316cba2016-06-02 16:17:39 -04002217 assert((SSL_get_shutdown(ssl) & mode) == SSL_get_shutdown(ssl));
Adam Langleyd9e397b2015-01-22 14:27:53 -08002218
David Benjamind316cba2016-06-02 16:17:39 -04002219 if (mode & SSL_RECEIVED_SHUTDOWN &&
Robert Sloan921ef2c2017-10-17 09:02:20 -07002220 ssl->s3->read_shutdown == ssl_shutdown_none) {
2221 ssl->s3->read_shutdown = ssl_shutdown_close_notify;
David Benjamind316cba2016-06-02 16:17:39 -04002222 }
2223
2224 if (mode & SSL_SENT_SHUTDOWN &&
Robert Sloan921ef2c2017-10-17 09:02:20 -07002225 ssl->s3->write_shutdown == ssl_shutdown_none) {
2226 ssl->s3->write_shutdown = ssl_shutdown_close_notify;
David Benjamind316cba2016-06-02 16:17:39 -04002227 }
Kenny Roote99801b2015-11-06 15:31:15 -08002228}
Adam Langleyd9e397b2015-01-22 14:27:53 -08002229
David Benjamind316cba2016-06-02 16:17:39 -04002230int SSL_get_shutdown(const SSL *ssl) {
2231 int ret = 0;
Robert Sloan921ef2c2017-10-17 09:02:20 -07002232 if (ssl->s3->read_shutdown != ssl_shutdown_none) {
Robert Sloana27a6a42017-09-05 08:39:28 -07002233 // Historically, OpenSSL set |SSL_RECEIVED_SHUTDOWN| on both close_notify
2234 // and fatal alert.
David Benjamind316cba2016-06-02 16:17:39 -04002235 ret |= SSL_RECEIVED_SHUTDOWN;
2236 }
Robert Sloan921ef2c2017-10-17 09:02:20 -07002237 if (ssl->s3->write_shutdown == ssl_shutdown_close_notify) {
Robert Sloana27a6a42017-09-05 08:39:28 -07002238 // Historically, OpenSSL set |SSL_SENT_SHUTDOWN| on only close_notify.
David Benjamind316cba2016-06-02 16:17:39 -04002239 ret |= SSL_SENT_SHUTDOWN;
2240 }
2241 return ret;
2242}
Kenny Roote99801b2015-11-06 15:31:15 -08002243
Adam Langleyd9e397b2015-01-22 14:27:53 -08002244SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
2245
2246SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
2247 if (ssl->ctx == ctx) {
2248 return ssl->ctx;
2249 }
2250
Robert Sloana27a6a42017-09-05 08:39:28 -07002251 // One cannot change the X.509 callbacks during a connection.
Robert Sloan5d625782017-02-13 09:55:39 -08002252 if (ssl->ctx->x509_method != ctx->x509_method) {
2253 assert(0);
2254 return NULL;
2255 }
2256
Adam Langleyd9e397b2015-01-22 14:27:53 -08002257 if (ctx == NULL) {
Robert Sloan1c9db532017-03-13 08:03:59 -07002258 ctx = ssl->session_ctx;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002259 }
2260
Robert Sloan5cbb5c82018-04-24 11:35:46 -07002261 Delete(ssl->cert);
2262 ssl->cert = ssl_cert_dup(ctx->cert).release();
Adam Langleye9ada862015-05-11 17:20:37 -07002263
Robert Sloan69939df2017-01-09 10:53:07 -08002264 SSL_CTX_up_ref(ctx);
2265 SSL_CTX_free(ssl->ctx);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002266 ssl->ctx = ctx;
2267
Adam Langleyd9e397b2015-01-22 14:27:53 -08002268 return ssl->ctx;
2269}
2270
Adam Langleyd9e397b2015-01-22 14:27:53 -08002271void SSL_set_info_callback(SSL *ssl,
Kenny Roote99801b2015-11-06 15:31:15 -08002272 void (*cb)(const SSL *ssl, int type, int value)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08002273 ssl->info_callback = cb;
2274}
2275
Kenny Roote99801b2015-11-06 15:31:15 -08002276void (*SSL_get_info_callback(const SSL *ssl))(const SSL *ssl, int type,
2277 int value) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08002278 return ssl->info_callback;
2279}
2280
Steven Valdeze7531f02016-12-14 13:29:57 -05002281int SSL_state(const SSL *ssl) {
Robert Sloana94fe052017-02-21 08:49:28 -08002282 return SSL_in_init(ssl) ? SSL_ST_INIT : SSL_ST_OK;
Steven Valdeze7531f02016-12-14 13:29:57 -05002283}
Adam Langleyd9e397b2015-01-22 14:27:53 -08002284
Adam Langleyf4e42722015-06-04 17:45:09 -07002285void SSL_set_state(SSL *ssl, int state) { }
Adam Langleyd9e397b2015-01-22 14:27:53 -08002286
David Benjamin4969cc92016-04-22 15:02:23 -04002287char *SSL_get_shared_ciphers(const SSL *ssl, char *buf, int len) {
2288 if (len <= 0) {
2289 return NULL;
2290 }
2291 buf[0] = '\0';
2292 return buf;
2293}
2294
Adam Langley4139edb2016-01-13 15:00:54 -08002295int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
Robert Sloan8ff03552017-06-14 12:40:58 -07002296 CRYPTO_EX_dup *dup_unused, CRYPTO_EX_free *free_func) {
Adam Langleye9ada862015-05-11 17:20:37 -07002297 int index;
2298 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp,
Robert Sloan8ff03552017-06-14 12:40:58 -07002299 free_func)) {
Adam Langleye9ada862015-05-11 17:20:37 -07002300 return -1;
2301 }
2302 return index;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002303}
2304
Robert Sloanfe7cd212017-08-07 09:03:39 -07002305int SSL_set_ex_data(SSL *ssl, int idx, void *data) {
2306 return CRYPTO_set_ex_data(&ssl->ex_data, idx, data);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002307}
2308
Kenny Rootb8494592015-09-25 02:29:14 +00002309void *SSL_get_ex_data(const SSL *ssl, int idx) {
2310 return CRYPTO_get_ex_data(&ssl->ex_data, idx);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002311}
2312
Adam Langley4139edb2016-01-13 15:00:54 -08002313int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
Robert Sloan8ff03552017-06-14 12:40:58 -07002314 CRYPTO_EX_dup *dup_unused,
Adam Langleyd9e397b2015-01-22 14:27:53 -08002315 CRYPTO_EX_free *free_func) {
Adam Langleye9ada862015-05-11 17:20:37 -07002316 int index;
2317 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp,
Robert Sloan8ff03552017-06-14 12:40:58 -07002318 free_func)) {
Adam Langleye9ada862015-05-11 17:20:37 -07002319 return -1;
2320 }
2321 return index;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002322}
2323
Robert Sloanfe7cd212017-08-07 09:03:39 -07002324int SSL_CTX_set_ex_data(SSL_CTX *ctx, int idx, void *data) {
2325 return CRYPTO_set_ex_data(&ctx->ex_data, idx, data);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002326}
2327
Kenny Rootb8494592015-09-25 02:29:14 +00002328void *SSL_CTX_get_ex_data(const SSL_CTX *ctx, int idx) {
2329 return CRYPTO_get_ex_data(&ctx->ex_data, idx);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002330}
2331
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002332int SSL_want(const SSL *ssl) { return ssl->s3->rwstate; }
Adam Langleyd9e397b2015-01-22 14:27:53 -08002333
2334void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
2335 RSA *(*cb)(SSL *ssl, int is_export,
Robert Sloanfe7cd212017-08-07 09:03:39 -07002336 int keylength)) {}
Adam Langleyd9e397b2015-01-22 14:27:53 -08002337
2338void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
Robert Sloanfe7cd212017-08-07 09:03:39 -07002339 int keylength)) {}
Adam Langleyd9e397b2015-01-22 14:27:53 -08002340
2341void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
Robert Sloanfe7cd212017-08-07 09:03:39 -07002342 DH *(*cb)(SSL *ssl, int is_export,
2343 int keylength)) {}
Adam Langleyd9e397b2015-01-22 14:27:53 -08002344
Robert Sloanfe7cd212017-08-07 09:03:39 -07002345void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*cb)(SSL *ssl, int is_export,
2346 int keylength)) {}
Adam Langleyd9e397b2015-01-22 14:27:53 -08002347
Robert Sloane56da3e2017-06-26 08:26:42 -07002348static int use_psk_identity_hint(char **out, const char *identity_hint) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08002349 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
Kenny Rootb8494592015-09-25 02:29:14 +00002350 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002351 return 0;
2352 }
2353
Robert Sloana27a6a42017-09-05 08:39:28 -07002354 // Clear currently configured hint, if any.
Robert Sloane56da3e2017-06-26 08:26:42 -07002355 OPENSSL_free(*out);
2356 *out = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002357
Robert Sloana27a6a42017-09-05 08:39:28 -07002358 // Treat the empty hint as not supplying one. Plain PSK makes it possible to
2359 // send either no hint (omit ServerKeyExchange) or an empty hint, while
2360 // ECDHE_PSK can only spell empty hint. Having different capabilities is odd,
2361 // so we interpret empty and missing as identical.
David Benjamin7c0d06c2016-08-11 13:26:41 -04002362 if (identity_hint != NULL && identity_hint[0] != '\0') {
Robert Sloane56da3e2017-06-26 08:26:42 -07002363 *out = BUF_strdup(identity_hint);
2364 if (*out == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08002365 return 0;
2366 }
2367 }
2368
2369 return 1;
2370}
2371
Robert Sloane56da3e2017-06-26 08:26:42 -07002372int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
2373 return use_psk_identity_hint(&ctx->psk_identity_hint, identity_hint);
2374}
2375
2376int SSL_use_psk_identity_hint(SSL *ssl, const char *identity_hint) {
2377 return use_psk_identity_hint(&ssl->psk_identity_hint, identity_hint);
2378}
2379
Kenny Rootb8494592015-09-25 02:29:14 +00002380const char *SSL_get_psk_identity_hint(const SSL *ssl) {
2381 if (ssl == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08002382 return NULL;
2383 }
Kenny Rootb8494592015-09-25 02:29:14 +00002384 return ssl->psk_identity_hint;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002385}
2386
Kenny Rootb8494592015-09-25 02:29:14 +00002387const char *SSL_get_psk_identity(const SSL *ssl) {
David Benjaminc895d6b2016-08-11 13:26:41 -04002388 if (ssl == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08002389 return NULL;
2390 }
David Benjaminc895d6b2016-08-11 13:26:41 -04002391 SSL_SESSION *session = SSL_get_session(ssl);
2392 if (session == NULL) {
2393 return NULL;
2394 }
2395 return session->psk_identity;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002396}
2397
2398void SSL_set_psk_client_callback(
Kenny Rootb8494592015-09-25 02:29:14 +00002399 SSL *ssl, unsigned (*cb)(SSL *ssl, const char *hint, char *identity,
2400 unsigned max_identity_len, uint8_t *psk,
2401 unsigned max_psk_len)) {
2402 ssl->psk_client_callback = cb;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002403}
2404
2405void SSL_CTX_set_psk_client_callback(
Kenny Rootb8494592015-09-25 02:29:14 +00002406 SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *hint, char *identity,
2407 unsigned max_identity_len, uint8_t *psk,
2408 unsigned max_psk_len)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08002409 ctx->psk_client_callback = cb;
2410}
2411
2412void SSL_set_psk_server_callback(
Kenny Rootb8494592015-09-25 02:29:14 +00002413 SSL *ssl, unsigned (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
2414 unsigned max_psk_len)) {
2415 ssl->psk_server_callback = cb;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002416}
2417
2418void SSL_CTX_set_psk_server_callback(
Kenny Rootb8494592015-09-25 02:29:14 +00002419 SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *identity,
2420 uint8_t *psk, unsigned max_psk_len)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08002421 ctx->psk_server_callback = cb;
2422}
2423
Robert Sloan0db7f542018-01-16 15:48:33 -08002424int SSL_set_dummy_pq_padding_size(SSL *ssl, size_t num_bytes) {
2425 if (num_bytes > 0xffff) {
2426 return 0;
2427 }
2428
2429 ssl->dummy_pq_padding_len = num_bytes;
2430 return 1;
2431}
2432
Robert Sloanab8b8882018-03-26 11:39:51 -07002433int SSL_dummy_pq_padding_used(SSL *ssl) {
2434 if (ssl->server) {
2435 return 0;
2436 }
2437
2438 return ssl->did_dummy_pq_padding;
2439}
2440
Adam Langleyd9e397b2015-01-22 14:27:53 -08002441void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
2442 void (*cb)(int write_p, int version,
2443 int content_type, const void *buf,
2444 size_t len, SSL *ssl, void *arg)) {
Adam Langleye9ada862015-05-11 17:20:37 -07002445 ctx->msg_callback = cb;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002446}
Adam Langleye9ada862015-05-11 17:20:37 -07002447
2448void SSL_CTX_set_msg_callback_arg(SSL_CTX *ctx, void *arg) {
2449 ctx->msg_callback_arg = arg;
2450}
2451
Adam Langleyd9e397b2015-01-22 14:27:53 -08002452void SSL_set_msg_callback(SSL *ssl,
2453 void (*cb)(int write_p, int version, int content_type,
2454 const void *buf, size_t len, SSL *ssl,
2455 void *arg)) {
Adam Langleye9ada862015-05-11 17:20:37 -07002456 ssl->msg_callback = cb;
2457}
2458
2459void SSL_set_msg_callback_arg(SSL *ssl, void *arg) {
2460 ssl->msg_callback_arg = arg;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002461}
2462
Adam Langley4139edb2016-01-13 15:00:54 -08002463void SSL_CTX_set_keylog_callback(SSL_CTX *ctx,
2464 void (*cb)(const SSL *ssl, const char *line)) {
2465 ctx->keylog_callback = cb;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002466}
2467
David Benjamin7c0d06c2016-08-11 13:26:41 -04002468void (*SSL_CTX_get_keylog_callback(const SSL_CTX *ctx))(const SSL *ssl,
2469 const char *line) {
2470 return ctx->keylog_callback;
2471}
2472
David Benjamin6e899c72016-06-09 18:02:18 -04002473void SSL_CTX_set_current_time_cb(SSL_CTX *ctx,
2474 void (*cb)(const SSL *ssl,
2475 struct timeval *out_clock)) {
2476 ctx->current_time_cb = cb;
2477}
2478
Kenny Roote99801b2015-11-06 15:31:15 -08002479int SSL_is_init_finished(const SSL *ssl) {
Robert Sloana94fe052017-02-21 08:49:28 -08002480 return !SSL_in_init(ssl);
Kenny Roote99801b2015-11-06 15:31:15 -08002481}
2482
2483int SSL_in_init(const SSL *ssl) {
Robert Sloana27a6a42017-09-05 08:39:28 -07002484 // This returns false once all the handshake state has been finalized, to
2485 // allow callbacks and getters based on SSL_in_init to return the correct
2486 // values.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002487 SSL_HANDSHAKE *hs = ssl->s3->hs.get();
Robert Sloana27a6a42017-09-05 08:39:28 -07002488 return hs != nullptr && !hs->handshake_finalized;
Kenny Roote99801b2015-11-06 15:31:15 -08002489}
2490
2491int SSL_in_false_start(const SSL *ssl) {
David Benjamin95add822016-10-19 01:09:12 -04002492 if (ssl->s3->hs == NULL) {
2493 return 0;
2494 }
2495 return ssl->s3->hs->in_false_start;
Adam Langleye9ada862015-05-11 17:20:37 -07002496}
2497
Adam Langley4139edb2016-01-13 15:00:54 -08002498int SSL_cutthrough_complete(const SSL *ssl) {
2499 return SSL_in_false_start(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002500}
2501
2502void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
2503 size_t *ssl_session_size) {
2504 *ssl_size = sizeof(SSL);
2505 *ssl_ctx_size = sizeof(SSL_CTX);
2506 *ssl_session_size = sizeof(SSL_SESSION);
2507}
2508
David Benjaminc895d6b2016-08-11 13:26:41 -04002509int SSL_is_server(const SSL *ssl) { return ssl->server; }
2510
2511int SSL_is_dtls(const SSL *ssl) { return ssl->method->is_dtls; }
Adam Langleyd9e397b2015-01-22 14:27:53 -08002512
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002513void SSL_CTX_set_select_certificate_cb(
2514 SSL_CTX *ctx,
2515 enum ssl_select_cert_result_t (*cb)(const SSL_CLIENT_HELLO *)) {
Kenny Roote99801b2015-11-06 15:31:15 -08002516 ctx->select_certificate_cb = cb;
2517}
2518
David Benjamin1b249672016-12-06 18:25:50 -05002519void SSL_CTX_set_dos_protection_cb(SSL_CTX *ctx,
2520 int (*cb)(const SSL_CLIENT_HELLO *)) {
Adam Langleye9ada862015-05-11 17:20:37 -07002521 ctx->dos_protection_cb = cb;
2522}
2523
Kenny Roote99801b2015-11-06 15:31:15 -08002524void SSL_set_renegotiate_mode(SSL *ssl, enum ssl_renegotiate_mode_t mode) {
2525 ssl->renegotiate_mode = mode;
2526}
2527
Adam Langleyfad63272015-11-12 12:15:39 -08002528int SSL_get_ivs(const SSL *ssl, const uint8_t **out_read_iv,
2529 const uint8_t **out_write_iv, size_t *out_iv_len) {
Adam Langleyfad63272015-11-12 12:15:39 -08002530 size_t write_iv_len;
Robert Sloanb6d070c2017-07-24 08:40:01 -07002531 if (!ssl->s3->aead_read_ctx->GetIV(out_read_iv, out_iv_len) ||
2532 !ssl->s3->aead_write_ctx->GetIV(out_write_iv, &write_iv_len) ||
Adam Langleyfad63272015-11-12 12:15:39 -08002533 *out_iv_len != write_iv_len) {
2534 return 0;
2535 }
2536
2537 return 1;
2538}
2539
David Benjamin4969cc92016-04-22 15:02:23 -04002540static uint64_t be_to_u64(const uint8_t in[8]) {
2541 return (((uint64_t)in[0]) << 56) | (((uint64_t)in[1]) << 48) |
2542 (((uint64_t)in[2]) << 40) | (((uint64_t)in[3]) << 32) |
2543 (((uint64_t)in[4]) << 24) | (((uint64_t)in[5]) << 16) |
2544 (((uint64_t)in[6]) << 8) | ((uint64_t)in[7]);
2545}
2546
2547uint64_t SSL_get_read_sequence(const SSL *ssl) {
Robert Sloana27a6a42017-09-05 08:39:28 -07002548 // TODO(davidben): Internally represent sequence numbers as uint64_t.
David Benjaminc895d6b2016-08-11 13:26:41 -04002549 if (SSL_is_dtls(ssl)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07002550 // max_seq_num already includes the epoch.
David Benjamin4969cc92016-04-22 15:02:23 -04002551 assert(ssl->d1->r_epoch == (ssl->d1->bitmap.max_seq_num >> 48));
2552 return ssl->d1->bitmap.max_seq_num;
2553 }
2554 return be_to_u64(ssl->s3->read_sequence);
2555}
2556
2557uint64_t SSL_get_write_sequence(const SSL *ssl) {
2558 uint64_t ret = be_to_u64(ssl->s3->write_sequence);
David Benjaminc895d6b2016-08-11 13:26:41 -04002559 if (SSL_is_dtls(ssl)) {
David Benjamin4969cc92016-04-22 15:02:23 -04002560 assert((ret >> 48) == 0);
2561 ret |= ((uint64_t)ssl->d1->w_epoch) << 48;
2562 }
2563 return ret;
2564}
2565
David Benjaminc895d6b2016-08-11 13:26:41 -04002566uint16_t SSL_get_peer_signature_algorithm(const SSL *ssl) {
Robert Sloana27a6a42017-09-05 08:39:28 -07002567 // TODO(davidben): This checks the wrong session if there is a renegotiation
2568 // in progress.
Steven Valdeze7531f02016-12-14 13:29:57 -05002569 SSL_SESSION *session = SSL_get_session(ssl);
2570 if (session == NULL) {
2571 return 0;
2572 }
2573
2574 return session->peer_signature_algorithm;
Adam Langleyfad63272015-11-12 12:15:39 -08002575}
2576
David Benjamin4969cc92016-04-22 15:02:23 -04002577size_t SSL_get_client_random(const SSL *ssl, uint8_t *out, size_t max_out) {
2578 if (max_out == 0) {
2579 return sizeof(ssl->s3->client_random);
2580 }
2581 if (max_out > sizeof(ssl->s3->client_random)) {
2582 max_out = sizeof(ssl->s3->client_random);
2583 }
Robert Sloan69939df2017-01-09 10:53:07 -08002584 OPENSSL_memcpy(out, ssl->s3->client_random, max_out);
David Benjamin4969cc92016-04-22 15:02:23 -04002585 return max_out;
2586}
2587
2588size_t SSL_get_server_random(const SSL *ssl, uint8_t *out, size_t max_out) {
2589 if (max_out == 0) {
2590 return sizeof(ssl->s3->server_random);
2591 }
2592 if (max_out > sizeof(ssl->s3->server_random)) {
2593 max_out = sizeof(ssl->s3->server_random);
2594 }
Robert Sloan69939df2017-01-09 10:53:07 -08002595 OPENSSL_memcpy(out, ssl->s3->server_random, max_out);
David Benjamin4969cc92016-04-22 15:02:23 -04002596 return max_out;
2597}
2598
2599const SSL_CIPHER *SSL_get_pending_cipher(const SSL *ssl) {
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002600 SSL_HANDSHAKE *hs = ssl->s3->hs.get();
Robert Sloana94fe052017-02-21 08:49:28 -08002601 if (hs == NULL) {
David Benjamin4969cc92016-04-22 15:02:23 -04002602 return NULL;
2603 }
Robert Sloana94fe052017-02-21 08:49:28 -08002604 return hs->new_cipher;
David Benjamin4969cc92016-04-22 15:02:23 -04002605}
2606
Steven Valdez909b19f2016-11-21 15:35:44 -05002607void SSL_set_retain_only_sha256_of_client_certs(SSL *ssl, int enabled) {
2608 ssl->retain_only_sha256_of_client_certs = !!enabled;
2609}
2610
David Benjamin4969cc92016-04-22 15:02:23 -04002611void SSL_CTX_set_retain_only_sha256_of_client_certs(SSL_CTX *ctx, int enabled) {
2612 ctx->retain_only_sha256_of_client_certs = !!enabled;
2613}
2614
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002615void SSL_CTX_set_grease_enabled(SSL_CTX *ctx, int enabled) {
2616 ctx->grease_enabled = !!enabled;
2617}
2618
Robert Sloan1c9db532017-03-13 08:03:59 -07002619int32_t SSL_get_ticket_age_skew(const SSL *ssl) {
2620 return ssl->s3->ticket_age_skew;
2621}
2622
Robert Sloancd79cde2017-12-11 09:06:12 -08002623void SSL_CTX_set_false_start_allowed_without_alpn(SSL_CTX *ctx, int allowed) {
2624 ctx->false_start_allowed_without_alpn = !!allowed;
2625}
2626
Robert Sloan0da43952018-01-03 15:13:14 -08002627int SSL_is_draft_downgrade(const SSL *ssl) { return ssl->s3->draft_downgrade; }
2628
Kenny Rootb8494592015-09-25 02:29:14 +00002629int SSL_clear(SSL *ssl) {
Robert Sloana27a6a42017-09-05 08:39:28 -07002630 // In OpenSSL, reusing a client |SSL| with |SSL_clear| causes the previously
2631 // established session to be offered the next time around. wpa_supplicant
2632 // depends on this behavior, so emulate it.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002633 UniquePtr<SSL_SESSION> session;
David Benjamin1b249672016-12-06 18:25:50 -05002634 if (!ssl->server && ssl->s3->established_session != NULL) {
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002635 session.reset(ssl->s3->established_session.get());
2636 SSL_SESSION_up_ref(session.get());
Kenny Rootb8494592015-09-25 02:29:14 +00002637 }
2638
Robert Sloana27a6a42017-09-05 08:39:28 -07002639 // The ssl->d1->mtu is simultaneously configuration (preserved across
2640 // clear) and connection-specific state (gets reset).
2641 //
2642 // TODO(davidben): Avoid this.
Kenny Rootb8494592015-09-25 02:29:14 +00002643 unsigned mtu = 0;
2644 if (ssl->d1 != NULL) {
2645 mtu = ssl->d1->mtu;
Adam Langleyf4e42722015-06-04 17:45:09 -07002646 }
2647
Kenny Rootb8494592015-09-25 02:29:14 +00002648 ssl->method->ssl_free(ssl);
2649 if (!ssl->method->ssl_new(ssl)) {
2650 return 0;
2651 }
Kenny Rootb8494592015-09-25 02:29:14 +00002652
David Benjaminc895d6b2016-08-11 13:26:41 -04002653 if (SSL_is_dtls(ssl) && (SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
Kenny Rootb8494592015-09-25 02:29:14 +00002654 ssl->d1->mtu = mtu;
Adam Langleyf4e42722015-06-04 17:45:09 -07002655 }
2656
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002657 if (session != nullptr) {
2658 SSL_set_session(ssl, session.get());
David Benjamin1b249672016-12-06 18:25:50 -05002659 }
Kenny Rootb8494592015-09-25 02:29:14 +00002660
Adam Langleyf4e42722015-06-04 17:45:09 -07002661 return 1;
Adam Langley56d25032015-06-23 16:20:13 -07002662}
2663
Adam Langleye9ada862015-05-11 17:20:37 -07002664int SSL_CTX_sess_connect(const SSL_CTX *ctx) { return 0; }
2665int SSL_CTX_sess_connect_good(const SSL_CTX *ctx) { return 0; }
2666int SSL_CTX_sess_connect_renegotiate(const SSL_CTX *ctx) { return 0; }
2667int SSL_CTX_sess_accept(const SSL_CTX *ctx) { return 0; }
2668int SSL_CTX_sess_accept_renegotiate(const SSL_CTX *ctx) { return 0; }
2669int SSL_CTX_sess_accept_good(const SSL_CTX *ctx) { return 0; }
2670int SSL_CTX_sess_hits(const SSL_CTX *ctx) { return 0; }
2671int SSL_CTX_sess_cb_hits(const SSL_CTX *ctx) { return 0; }
2672int SSL_CTX_sess_misses(const SSL_CTX *ctx) { return 0; }
2673int SSL_CTX_sess_timeouts(const SSL_CTX *ctx) { return 0; }
2674int SSL_CTX_sess_cache_full(const SSL_CTX *ctx) { return 0; }
David Benjamin6e899c72016-06-09 18:02:18 -04002675
2676int SSL_num_renegotiations(const SSL *ssl) {
2677 return SSL_total_renegotiations(ssl);
2678}
2679
2680int SSL_CTX_need_tmp_RSA(const SSL_CTX *ctx) { return 0; }
2681int SSL_need_tmp_RSA(const SSL *ssl) { return 0; }
2682int SSL_CTX_set_tmp_rsa(SSL_CTX *ctx, const RSA *rsa) { return 1; }
2683int SSL_set_tmp_rsa(SSL *ssl, const RSA *rsa) { return 1; }
Kenny Rootb8494592015-09-25 02:29:14 +00002684void ERR_load_SSL_strings(void) {}
2685void SSL_load_error_strings(void) {}
David Benjamin6e899c72016-06-09 18:02:18 -04002686int SSL_cache_hit(SSL *ssl) { return SSL_session_reused(ssl); }
2687
2688int SSL_CTX_set_tmp_ecdh(SSL_CTX *ctx, const EC_KEY *ec_key) {
2689 if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) {
2690 OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
2691 return 0;
2692 }
2693 int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key));
2694 return SSL_CTX_set1_curves(ctx, &nid, 1);
2695}
2696
2697int SSL_set_tmp_ecdh(SSL *ssl, const EC_KEY *ec_key) {
2698 if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) {
2699 OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
2700 return 0;
2701 }
2702 int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key));
2703 return SSL_set1_curves(ssl, &nid, 1);
2704}
David Benjaminc895d6b2016-08-11 13:26:41 -04002705
Robert Sloan1c9db532017-03-13 08:03:59 -07002706void SSL_CTX_set_ticket_aead_method(SSL_CTX *ctx,
2707 const SSL_TICKET_AEAD_METHOD *aead_method) {
2708 ctx->ticket_aead_method = aead_method;
2709}