blob: 8fb9ada39afb83d78a1675354736085a9dd4551a [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* 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.
Adam Langleye745b252018-02-26 14:02:17 -08007 *
Adam Langley95c29f32014-06-20 12:00:00 -07008 * 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).
Adam Langleye745b252018-02-26 14:02:17 -080014 *
Adam Langley95c29f32014-06-20 12:00:00 -070015 * 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.
Adam Langleye745b252018-02-26 14:02:17 -080021 *
Adam Langley95c29f32014-06-20 12:00:00 -070022 * 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 :-).
Adam Langleye745b252018-02-26 14:02:17 -080036 * 4. If you include any Windows specific code (or a derivative thereof) from
Adam Langley95c29f32014-06-20 12:00:00 -070037 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
Adam Langleye745b252018-02-26 14:02:17 -080039 *
Adam Langley95c29f32014-06-20 12:00:00 -070040 * 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.
Adam Langleye745b252018-02-26 14:02:17 -080051 *
Adam Langley95c29f32014-06-20 12:00:00 -070052 * 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
Adam Langleye745b252018-02-26 14:02:17 -080065 * notice, this list of conditions and the following disclaimer.
Adam Langley95c29f32014-06-20 12:00:00 -070066 *
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.
Adam Langleye745b252018-02-26 14:02:17 -0800112 * ECC cipher suite support in OpenSSL originally developed by
Adam Langley95c29f32014-06-20 12:00:00 -0700113 * 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
David Benjamin9e4e01e2015-09-15 01:48:04 -0400141#include <openssl/ssl.h>
142
Adam Langley95c29f32014-06-20 12:00:00 -0700143#include <assert.h>
David Benjamin93d97432016-08-08 21:22:47 -0400144#include <stdlib.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400145#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700146
David Benjamin39482a12014-07-20 13:30:15 -0400147#include <openssl/bytestring.h>
David Benjamin7c6d09b2015-09-05 11:00:19 -0400148#include <openssl/crypto.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400149#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700150#include <openssl/lhash.h>
151#include <openssl/mem.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700152#include <openssl/rand.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700153
David Benjamin2ee94aa2015-04-07 22:38:30 -0400154#include "internal.h"
David Benjamin546f1a52015-04-15 16:46:09 -0400155#include "../crypto/internal.h"
156
David Benjamin721e8b72016-08-03 13:13:17 -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 Langley95c29f32014-06-20 12:00:00 -0700164
David Benjamin86e95b82017-07-18 16:34:25 -0400165namespace bssl {
166
David Benjaminc11ea9422017-08-29 16:33:21 -0400167// |SSL_R_UNKNOWN_PROTOCOL| is no longer emitted, but continue to define it
168// to avoid downstream churn.
David Benjamin76c2efc2015-08-31 14:24:29 -0400169OPENSSL_DECLARE_ERROR_REASON(SSL, UNKNOWN_PROTOCOL)
David Benjamin97760d52015-07-24 23:02:49 -0400170
David Benjaminc11ea9422017-08-29 16:33:21 -0400171// The following errors are no longer emitted, but are used in nginx without
172// #ifdefs.
David Benjamin311c2572016-08-17 15:54:36 -0400173OPENSSL_DECLARE_ERROR_REASON(SSL, BLOCK_CIPHER_PAD_IS_WRONG)
174OPENSSL_DECLARE_ERROR_REASON(SSL, NO_CIPHERS_SPECIFIED)
175
David Benjaminc11ea9422017-08-29 16:33:21 -0400176// Some error codes are special. Ensure the make_errors.go script never
177// regresses this.
David Benjamina3d76d02017-07-14 19:36:07 -0400178static_assert(SSL_R_TLSV1_ALERT_NO_RENEGOTIATION ==
179 SSL_AD_NO_RENEGOTIATION + SSL_AD_REASON_OFFSET,
180 "alert reason code mismatch");
David Benjamine1136082014-09-20 12:28:58 -0400181
David Benjaminc11ea9422017-08-29 16:33:21 -0400182// kMaxHandshakeSize is the maximum size, in bytes, of a handshake message.
David Benjamin1d0a1942015-04-26 15:35:35 -0400183static const size_t kMaxHandshakeSize = (1u << 24) - 1;
184
David Benjaminaa585132015-06-29 23:36:17 -0400185static 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;
David Benjamin9f33fc62015-04-15 17:29:53 -0400189
David Benjamin879efc32017-09-21 11:20:53 -0400190bool 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
David Benjamin86e95b82017-07-18 16:34:25 -0400201void ssl_reset_error_state(SSL *ssl) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400202 // Functions which use |SSL_get_error| must reset I/O and error state on
203 // entry.
David Benjamin32ce0ac2017-10-13 19:17:22 -0400204 ssl->s3->rwstate = SSL_NOTHING;
David Benjamin86e95b82017-07-18 16:34:25 -0400205 ERR_clear_error();
206 ERR_clear_system_error();
207}
208
David Benjamin31aad2d2017-10-06 16:04:06 -0400209void ssl_set_read_error(SSL* ssl) {
210 ssl->s3->read_shutdown = ssl_shutdown_error;
David Benjamin8e7bbba2017-10-13 17:18:35 -0400211 ssl->s3->read_error.reset(ERR_save_state());
David Benjamin31aad2d2017-10-06 16:04:06 -0400212}
213
David Benjaminf6632da2017-10-12 19:11:47 -0400214static bool check_read_error(const SSL *ssl) {
215 if (ssl->s3->read_shutdown == ssl_shutdown_error) {
David Benjamin8e7bbba2017-10-13 17:18:35 -0400216 ERR_restore_state(ssl->s3->read_error.get());
David Benjaminf6632da2017-10-12 19:11:47 -0400217 return false;
218 }
219 return true;
220}
221
David Benjamin86e95b82017-07-18 16:34:25 -0400222int 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
David Benjaminf6632da2017-10-12 19:11:47 -0400230ssl_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
David Benjamin86e95b82017-07-18 16:34:25 -0400275void ssl_update_cache(SSL_HANDSHAKE *hs, int mode) {
276 SSL *const ssl = hs->ssl;
David Benjamin98472cb2018-05-02 16:05:36 -0400277 SSL_CTX *ctx = ssl->session_ctx;
David Benjaminc11ea9422017-08-29 16:33:21 -0400278 // Never cache sessions with empty session IDs.
David Benjamin86e95b82017-07-18 16:34:25 -0400279 if (ssl->s3->established_session->session_id_length == 0 ||
David Benjamin5ef40c62017-08-23 21:28:29 -0700280 ssl->s3->established_session->not_resumable ||
David Benjamin86e95b82017-07-18 16:34:25 -0400281 (ctx->session_cache_mode & mode) != mode) {
282 return;
283 }
284
David Benjaminc11ea9422017-08-29 16:33:21 -0400285 // Clients never use the internal session cache.
David Benjamin86e95b82017-07-18 16:34:25 -0400286 int use_internal_cache = ssl->server && !(ctx->session_cache_mode &
287 SSL_SESS_CACHE_NO_INTERNAL_STORE);
288
David Benjaminc11ea9422017-08-29 16:33:21 -0400289 // 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.
David Benjamin8e7bbba2017-10-13 17:18:35 -0400292 if (ssl->s3->established_session.get() != ssl->session ||
David Benjamin86e95b82017-07-18 16:34:25 -0400293 (!ssl->server && hs->ticket_expected)) {
294 if (use_internal_cache) {
David Benjamin8e7bbba2017-10-13 17:18:35 -0400295 SSL_CTX_add_session(ctx, ssl->s3->established_session.get());
David Benjamin86e95b82017-07-18 16:34:25 -0400296 }
297 if (ctx->new_session_cb != NULL) {
David Benjamin8e7bbba2017-10-13 17:18:35 -0400298 SSL_SESSION_up_ref(ssl->s3->established_session.get());
299 if (!ctx->new_session_cb(ssl, ssl->s3->established_session.get())) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400300 // |new_session_cb|'s return value signals whether it took ownership.
David Benjamin8e7bbba2017-10-13 17:18:35 -0400301 SSL_SESSION_free(ssl->s3->established_session.get());
David Benjamin86e95b82017-07-18 16:34:25 -0400302 }
303 }
304 }
305
306 if (use_internal_cache &&
307 !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR)) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400308 // Automatically flush the internal session cache every 255 connections.
David Benjamin86e95b82017-07-18 16:34:25 -0400309 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
David Benjamin1386aad2017-07-19 23:57:40 -0400348 ScopedCBB cbb;
David Benjamin86e95b82017-07-18 16:34:25 -0400349 uint8_t *out;
350 size_t out_len;
David Benjamin1386aad2017-07-19 23:57:40 -0400351 if (!CBB_init(cbb.get(), strlen(label) + 1 + SSL3_RANDOM_SIZE * 2 + 1 +
David Benjamin86e95b82017-07-18 16:34:25 -0400352 secret_len * 2 + 1) ||
David Benjamin1386aad2017-07-19 23:57:40 -0400353 !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)) {
David Benjamin86e95b82017-07-18 16:34:25 -0400360 return 0;
361 }
362
363 ssl->ctx->keylog_callback(ssl, (const char *)out);
364 OPENSSL_free(out);
365 return 1;
366}
367
David Benjamin86e95b82017-07-18 16:34:25 -0400368void 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,
David Benjaminc64d1232017-10-04 18:14:28 -0400382 Span<const uint8_t> in) {
David Benjamin86e95b82017-07-18 16:34:25 -0400383 if (ssl->msg_callback == NULL) {
384 return;
385 }
386
David Benjaminc11ea9422017-08-29 16:33:21 -0400387 // |version| is zero when calling for |SSL3_RT_HEADER| and |SSL2_VERSION| for
388 // a V2ClientHello.
David Benjamin86e95b82017-07-18 16:34:25 -0400389 int version;
390 switch (content_type) {
391 case 0:
David Benjaminc11ea9422017-08-29 16:33:21 -0400392 // V2ClientHello
David Benjamin86e95b82017-07-18 16:34:25 -0400393 version = SSL2_VERSION;
394 break;
395 case SSL3_RT_HEADER:
396 version = 0;
397 break;
398 default:
399 version = SSL_version(ssl);
400 }
401
David Benjaminc64d1232017-10-04 18:14:28 -0400402 ssl->msg_callback(is_write, version, content_type, in.data(), in.size(), ssl,
David Benjamin86e95b82017-07-18 16:34:25 -0400403 ssl->msg_callback_arg);
404}
405
406void ssl_get_current_time(const SSL *ssl, struct OPENSSL_timeval *out_clock) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400407 // TODO(martinkr): Change callers to |ssl_ctx_get_current_time| and drop the
408 // |ssl| arg from |current_time_cb| if possible.
Martin Kreichgauer72912d22017-08-04 12:06:43 -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) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400415 // TODO(davidben): Update current_time_cb to use OPENSSL_timeval. See
416 // https://crbug.com/boringssl/155.
David Benjamin86e95b82017-07-18 16:34:25 -0400417 struct timeval clock;
Martin Kreichgauer72912d22017-08-04 12:06:43 -0700418 ctx->current_time_cb(nullptr /* ssl */, &clock);
David Benjamin86e95b82017-07-18 16:34:25 -0400419 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
Adam Langley3fe8fa72018-01-26 09:14:30 -0800458void SSL_CTX_set_handoff_mode(SSL_CTX *ctx, bool on) {
459 ctx->handoff = on;
460}
461
David Benjamin71666cb2018-05-01 17:14:27 -0400462static bool ssl_can_renegotiate(const SSL *ssl) {
463 if (ssl->server || SSL_is_dtls(ssl)) {
464 return false;
465 }
466
467 // We do not accept at SSL 3.0. SSL 3.0 will be removed entirely in the future
468 // and requires retaining more data for renegotiation_info.
469 uint16_t version = ssl_protocol_version(ssl);
470 if (version == SSL3_VERSION || version >= TLS1_3_VERSION) {
471 return false;
472 }
473
474 // The config has already been shed.
475 if (!ssl->config) {
476 return false;
477 }
478
479 switch (ssl->renegotiate_mode) {
480 case ssl_renegotiate_ignore:
481 case ssl_renegotiate_never:
482 return false;
483
484 case ssl_renegotiate_freely:
485 return true;
486 case ssl_renegotiate_once:
487 return ssl->s3->total_renegotiations == 0;
488 }
489
490 assert(0);
491 return false;
492}
493
494static void ssl_maybe_shed_handshake_config(SSL *ssl) {
495 if (ssl->s3->hs != nullptr ||
496 ssl->config == nullptr ||
497 !ssl->config->shed_handshake_config ||
498 ssl_can_renegotiate(ssl)) {
499 return;
500 }
501
502 Delete(ssl->config);
503 ssl->config = nullptr;
504}
505
Matthew Braithwaitee30fac62018-04-05 16:50:21 -0700506void SSL_set_handoff_mode(SSL *ssl, bool on) {
507 if (!ssl->config) {
508 return;
509 }
510 ssl->config->handoff = on;
511}
512
David Benjamin86e95b82017-07-18 16:34:25 -0400513} // namespace bssl
514
515using namespace bssl;
516
David Benjamin7c6d09b2015-09-05 11:00:19 -0400517int SSL_library_init(void) {
518 CRYPTO_library_init();
519 return 1;
520}
521
David Benjamin81f030b2016-08-12 14:48:19 -0400522int OPENSSL_init_ssl(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings) {
523 CRYPTO_library_init();
524 return 1;
525}
526
Adam Langleye976e432016-02-15 14:01:10 -0800527static uint32_t ssl_session_hash(const SSL_SESSION *sess) {
528 const uint8_t *session_id = sess->session_id;
529
530 uint8_t tmp_storage[sizeof(uint32_t)];
531 if (sess->session_id_length < sizeof(tmp_storage)) {
David Benjamin17cf2cb2016-12-13 01:07:13 -0500532 OPENSSL_memset(tmp_storage, 0, sizeof(tmp_storage));
533 OPENSSL_memcpy(tmp_storage, sess->session_id, sess->session_id_length);
Adam Langleye976e432016-02-15 14:01:10 -0800534 session_id = tmp_storage;
535 }
536
David Benjamin27bbae42015-09-13 00:54:37 -0400537 uint32_t hash =
Adam Langleye976e432016-02-15 14:01:10 -0800538 ((uint32_t)session_id[0]) |
539 ((uint32_t)session_id[1] << 8) |
540 ((uint32_t)session_id[2] << 16) |
541 ((uint32_t)session_id[3] << 24);
David Benjamin7c6d09b2015-09-05 11:00:19 -0400542
David Benjamin27bbae42015-09-13 00:54:37 -0400543 return hash;
544}
545
David Benjaminc11ea9422017-08-29 16:33:21 -0400546// NB: If this function (or indeed the hash function which uses a sort of
547// coarser function than this one) is changed, ensure
548// SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
549// able to construct an SSL_SESSION that will collide with any existing session
550// with a matching session ID.
David Benjamin27bbae42015-09-13 00:54:37 -0400551static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
552 if (a->ssl_version != b->ssl_version) {
553 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800554 }
Adam Langley95c29f32014-06-20 12:00:00 -0700555
David Benjamin27bbae42015-09-13 00:54:37 -0400556 if (a->session_id_length != b->session_id_length) {
557 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800558 }
Adam Langley95c29f32014-06-20 12:00:00 -0700559
David Benjamin17cf2cb2016-12-13 01:07:13 -0500560 return OPENSSL_memcmp(a->session_id, b->session_id, a->session_id_length);
David Benjamin27bbae42015-09-13 00:54:37 -0400561}
Adam Langley95c29f32014-06-20 12:00:00 -0700562
David Benjamin27bbae42015-09-13 00:54:37 -0400563SSL_CTX *SSL_CTX_new(const SSL_METHOD *method) {
564 SSL_CTX *ret = NULL;
565
566 if (method == NULL) {
567 OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_METHOD_PASSED);
568 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800569 }
Adam Langley95c29f32014-06-20 12:00:00 -0700570
David Benjaminf5260812017-07-12 17:16:39 -0400571 ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
David Benjamin27bbae42015-09-13 00:54:37 -0400572 if (ret == NULL) {
573 goto err;
David Benjamin62fd1622015-01-11 13:30:01 -0500574 }
575
David Benjamin17cf2cb2016-12-13 01:07:13 -0500576 OPENSSL_memset(ret, 0, sizeof(SSL_CTX));
Adam Langley95c29f32014-06-20 12:00:00 -0700577
David Benjamin27bbae42015-09-13 00:54:37 -0400578 ret->method = method->method;
Adam Langley3509dac2017-02-01 11:59:18 -0800579 ret->x509_method = method->x509_method;
David Benjamin27bbae42015-09-13 00:54:37 -0400580
581 CRYPTO_MUTEX_init(&ret->lock);
582
583 ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
584 ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
585
David Benjamin27bbae42015-09-13 00:54:37 -0400586 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
David Benjamin17b30832017-01-28 14:00:32 -0500587 ret->session_psk_dhe_timeout = SSL_DEFAULT_SESSION_PSK_DHE_TIMEOUT;
David Benjamin27bbae42015-09-13 00:54:37 -0400588
589 ret->references = 1;
590
591 ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
592 ret->verify_mode = SSL_VERIFY_NONE;
David Benjamine325c3f2018-04-12 16:11:15 -0400593 ret->cert = New<CERT>(method->x509_method);
David Benjamin27bbae42015-09-13 00:54:37 -0400594 if (ret->cert == NULL) {
595 goto err;
596 }
597
598 ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
599 if (ret->sessions == NULL) {
600 goto err;
601 }
Adam Langley2a3b3432017-02-10 13:56:48 -0800602
603 if (!ret->x509_method->ssl_ctx_new(ret)) {
David Benjamin27bbae42015-09-13 00:54:37 -0400604 goto err;
605 }
606
Matthew Braithwaite6ad20dc2017-02-21 16:41:33 -0800607 if (!SSL_CTX_set_strict_cipher_list(ret, SSL_DEFAULT_CIPHER_LIST)) {
David Benjamin27bbae42015-09-13 00:54:37 -0400608 goto err2;
609 }
610
Adam Langley34b4c822017-02-02 10:57:17 -0800611 ret->client_CA = sk_CRYPTO_BUFFER_new_null();
David Benjamin27bbae42015-09-13 00:54:37 -0400612 if (ret->client_CA == NULL) {
613 goto err;
614 }
615
David Benjamin8a589332015-12-04 23:14:35 -0500616 CRYPTO_new_ex_data(&ret->ex_data);
David Benjamin27bbae42015-09-13 00:54:37 -0400617
618 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
619
David Benjaminc11ea9422017-08-29 16:33:21 -0400620 // Disable the auto-chaining feature by default. Once this has stuck without
621 // problems, the feature will be removed entirely.
David Benjamina983b4c2016-11-09 14:21:12 -0500622 ret->mode = SSL_MODE_NO_AUTO_CHAIN;
623
David Benjamine28552d2018-04-08 13:59:25 -0400624 ret->rsa_pss_rsae_certs_enabled = true;
625
David Benjaminc11ea9422017-08-29 16:33:21 -0400626 // Lock the SSL_CTX to the specified version, for compatibility with legacy
David Benjaminc03ecb92018-02-02 16:07:50 -0500627 // uses of SSL_METHOD.
David Benjamine34bcc92016-09-21 16:53:09 -0400628 if (!SSL_CTX_set_max_proto_version(ret, method->version) ||
David Benjaminc03ecb92018-02-02 16:07:50 -0500629 !SSL_CTX_set_min_proto_version(ret, method->version)) {
David Benjamine34bcc92016-09-21 16:53:09 -0400630 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
631 goto err2;
David Benjamin27bbae42015-09-13 00:54:37 -0400632 }
633
634 return ret;
635
636err:
637 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
638err2:
639 SSL_CTX_free(ret);
640 return NULL;
641}
642
Adam Langley84cd1592016-06-30 18:29:44 -0700643int SSL_CTX_up_ref(SSL_CTX *ctx) {
644 CRYPTO_refcount_inc(&ctx->references);
645 return 1;
646}
647
David Benjamin27bbae42015-09-13 00:54:37 -0400648void SSL_CTX_free(SSL_CTX *ctx) {
649 if (ctx == NULL ||
650 !CRYPTO_refcount_dec_and_test_zero(&ctx->references)) {
651 return;
652 }
653
David Benjaminc11ea9422017-08-29 16:33:21 -0400654 // Free internal session cache. However: the remove_cb() may reference the
655 // ex_data of SSL_CTX, thus the ex_data store can only be removed after the
656 // sessions were flushed. As the ex_data handling routines might also touch
657 // the session cache, the most secure solution seems to be: empty (flush) the
658 // cache, then free ex_data, then finally free the cache. (See ticket
659 // [openssl.org #212].)
David Benjamin27bbae42015-09-13 00:54:37 -0400660 SSL_CTX_flush_sessions(ctx, 0);
661
662 CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, ctx, &ctx->ex_data);
663
664 CRYPTO_MUTEX_cleanup(&ctx->lock);
665 lh_SSL_SESSION_free(ctx->sessions);
David Benjamin48b276d2018-04-12 17:37:32 -0400666 Delete(ctx->cipher_list);
David Benjamine325c3f2018-04-12 16:11:15 -0400667 Delete(ctx->cert);
David Benjamin27bbae42015-09-13 00:54:37 -0400668 sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->client_custom_extensions,
669 SSL_CUSTOM_EXTENSION_free);
670 sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->server_custom_extensions,
671 SSL_CUSTOM_EXTENSION_free);
Adam Langley34b4c822017-02-02 10:57:17 -0800672 sk_CRYPTO_BUFFER_pop_free(ctx->client_CA, CRYPTO_BUFFER_free);
Adam Langley2a3b3432017-02-10 13:56:48 -0800673 ctx->x509_method->ssl_ctx_free(ctx);
David Benjamin27bbae42015-09-13 00:54:37 -0400674 sk_SRTP_PROTECTION_PROFILE_free(ctx->srtp_profiles);
675 OPENSSL_free(ctx->psk_identity_hint);
Steven Valdezce902a92016-05-17 11:47:53 -0400676 OPENSSL_free(ctx->supported_group_list);
David Benjamin27bbae42015-09-13 00:54:37 -0400677 OPENSSL_free(ctx->alpn_client_proto_list);
David Benjamin27bbae42015-09-13 00:54:37 -0400678 EVP_PKEY_free(ctx->tlsext_channel_id_private);
David Benjamin71c21b42017-04-14 17:05:40 -0400679 OPENSSL_free(ctx->verify_sigalgs);
Martin Kreichgauer72912d22017-08-04 12:06:43 -0700680 OPENSSL_free(ctx->tlsext_ticket_key_current);
681 OPENSSL_free(ctx->tlsext_ticket_key_prev);
David Benjamin27bbae42015-09-13 00:54:37 -0400682
683 OPENSSL_free(ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -0800684}
Adam Langley95c29f32014-06-20 12:00:00 -0700685
Adam Langleyfcf25832014-12-18 17:42:32 -0800686SSL *SSL_new(SSL_CTX *ctx) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800687 if (ctx == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400688 OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_CTX);
Adam Langleyfcf25832014-12-18 17:42:32 -0800689 return NULL;
690 }
691 if (ctx->method == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400692 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
Adam Langleyfcf25832014-12-18 17:42:32 -0800693 return NULL;
694 }
Adam Langley95c29f32014-06-20 12:00:00 -0700695
David Benjaminf5260812017-07-12 17:16:39 -0400696 SSL *ssl = (SSL *)OPENSSL_malloc(sizeof(SSL));
David Benjamin0d56f882015-12-19 17:05:56 -0500697 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800698 goto err;
699 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500700 OPENSSL_memset(ssl, 0, sizeof(SSL));
Adam Langley95c29f32014-06-20 12:00:00 -0700701
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700702 ssl->config = New<SSL_CONFIG>(ssl);
703 if (ssl->config == nullptr) {
704 goto err;
705 }
706 ssl->config->conf_min_version = ctx->conf_min_version;
707 ssl->config->conf_max_version = ctx->conf_max_version;
Steven Valdez52586f92017-07-11 15:08:32 -0400708 ssl->tls13_variant = ctx->tls13_variant;
David Benjamin1eb367c2014-12-12 18:17:51 -0500709
David Benjaminc11ea9422017-08-29 16:33:21 -0400710 // RFC 6347 states that implementations SHOULD use an initial timer value of
711 // 1 second.
Taylor Brandstetter376a0fe2016-05-10 19:30:28 -0700712 ssl->initial_timeout_duration_ms = 1000;
713
David Benjamin0d56f882015-12-19 17:05:56 -0500714 ssl->options = ctx->options;
715 ssl->mode = ctx->mode;
716 ssl->max_cert_list = ctx->max_cert_list;
Adam Langley95c29f32014-06-20 12:00:00 -0700717
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700718 ssl->config->cert = ssl_cert_dup(ctx->cert).release();
719 if (ssl->config->cert == NULL) {
David Benjamina5a3eeb2015-03-18 20:26:30 -0400720 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -0800721 }
Adam Langley95c29f32014-06-20 12:00:00 -0700722
David Benjamin0d56f882015-12-19 17:05:56 -0500723 ssl->msg_callback = ctx->msg_callback;
724 ssl->msg_callback_arg = ctx->msg_callback_arg;
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700725 ssl->config->verify_mode = ctx->verify_mode;
726 ssl->config->verify_callback = ctx->default_verify_callback;
727 ssl->config->custom_verify_callback = ctx->custom_verify_callback;
728 ssl->config->retain_only_sha256_of_client_certs =
David Benjaminbbaf3672016-11-17 10:53:09 +0900729 ctx->retain_only_sha256_of_client_certs;
Adam Langley95c29f32014-06-20 12:00:00 -0700730
David Benjamin0d56f882015-12-19 17:05:56 -0500731 ssl->quiet_shutdown = ctx->quiet_shutdown;
732 ssl->max_send_fragment = ctx->max_send_fragment;
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700733 ssl->enable_early_data = ctx->enable_early_data;
Adam Langley95c29f32014-06-20 12:00:00 -0700734
David Benjaminf650c712016-12-22 15:54:51 -0500735 SSL_CTX_up_ref(ctx);
David Benjamin0d56f882015-12-19 17:05:56 -0500736 ssl->ctx = ctx;
David Benjaminf650c712016-12-22 15:54:51 -0500737 SSL_CTX_up_ref(ctx);
David Benjamin98472cb2018-05-02 16:05:36 -0400738 ssl->session_ctx = ctx;
Adam Langley2a3b3432017-02-10 13:56:48 -0800739
Steven Valdezce902a92016-05-17 11:47:53 -0400740 if (ctx->supported_group_list) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700741 ssl->config->supported_group_list = (uint16_t *)BUF_memdup(
David Benjaminf5260812017-07-12 17:16:39 -0400742 ctx->supported_group_list, ctx->supported_group_list_len * 2);
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700743 if (!ssl->config->supported_group_list) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800744 goto err;
745 }
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700746 ssl->config->supported_group_list_len = ctx->supported_group_list_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800747 }
Adam Langley95c29f32014-06-20 12:00:00 -0700748
David Benjamin17b30832017-01-28 14:00:32 -0500749 if (ctx->alpn_client_proto_list) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700750 ssl->config->alpn_client_proto_list = (uint8_t *)BUF_memdup(
David Benjaminf5260812017-07-12 17:16:39 -0400751 ctx->alpn_client_proto_list, ctx->alpn_client_proto_list_len);
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700752 if (ssl->config->alpn_client_proto_list == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800753 goto err;
754 }
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700755 ssl->config->alpn_client_proto_list_len = ctx->alpn_client_proto_list_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800756 }
Adam Langley95c29f32014-06-20 12:00:00 -0700757
David Benjamin0d56f882015-12-19 17:05:56 -0500758 ssl->method = ctx->method;
Adam Langley95c29f32014-06-20 12:00:00 -0700759
David Benjamin0d56f882015-12-19 17:05:56 -0500760 if (!ssl->method->ssl_new(ssl)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800761 goto err;
762 }
Adam Langley95c29f32014-06-20 12:00:00 -0700763
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700764 if (!ssl->ctx->x509_method->ssl_new(ssl->s3->hs.get())) {
765 goto err;
766 }
767
David Benjamin0d56f882015-12-19 17:05:56 -0500768 CRYPTO_new_ex_data(&ssl->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700769
Adam Langleyfcf25832014-12-18 17:42:32 -0800770 if (ctx->psk_identity_hint) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700771 ssl->config->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
772 if (ssl->config->psk_identity_hint == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800773 goto err;
774 }
775 }
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700776 ssl->config->psk_client_callback = ctx->psk_client_callback;
777 ssl->config->psk_server_callback = ctx->psk_server_callback;
Adam Langley95c29f32014-06-20 12:00:00 -0700778
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700779 ssl->config->tlsext_channel_id_enabled = ctx->tlsext_channel_id_enabled;
David Benjamin02ddbfd2015-01-11 13:09:11 -0500780 if (ctx->tlsext_channel_id_private) {
Adam Langley310d3f62016-07-12 10:39:20 -0700781 EVP_PKEY_up_ref(ctx->tlsext_channel_id_private);
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700782 ssl->config->tlsext_channel_id_private = ctx->tlsext_channel_id_private;
David Benjamin02ddbfd2015-01-11 13:09:11 -0500783 }
784
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700785 ssl->config->signed_cert_timestamps_enabled =
786 ctx->signed_cert_timestamps_enabled;
787 ssl->config->ocsp_stapling_enabled = ctx->ocsp_stapling_enabled;
788 ssl->config->handoff = ctx->handoff;
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200789
David Benjamin0d56f882015-12-19 17:05:56 -0500790 return ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -0800791
Adam Langley95c29f32014-06-20 12:00:00 -0700792err:
David Benjamin0d56f882015-12-19 17:05:56 -0500793 SSL_free(ssl);
David Benjamin3570d732015-06-29 00:28:17 -0400794 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700795
Adam Langleyfcf25832014-12-18 17:42:32 -0800796 return NULL;
797}
Adam Langley95c29f32014-06-20 12:00:00 -0700798
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700799SSL_CONFIG::SSL_CONFIG(SSL *ssl_arg)
800 : ssl(ssl_arg),
801 signed_cert_timestamps_enabled(false),
802 ocsp_stapling_enabled(false),
803 tlsext_channel_id_enabled(false),
804 retain_only_sha256_of_client_certs(false),
805 handoff(false),
806 shed_handshake_config(false) {
807 assert(ssl);
808}
809
810SSL_CONFIG::~SSL_CONFIG() {
811 if (ssl->ctx != nullptr) {
812 ssl->ctx->x509_method->ssl_config_free(this);
813 }
814 Delete(cipher_list);
815 Delete(cert);
David Benjaminb95d4b42018-05-02 16:11:34 -0400816 OPENSSL_free(psk_identity_hint);
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700817 OPENSSL_free(supported_group_list);
David Benjaminb95d4b42018-05-02 16:11:34 -0400818 EVP_PKEY_free(tlsext_channel_id_private);
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700819 OPENSSL_free(alpn_client_proto_list);
820 OPENSSL_free(token_binding_params);
821 OPENSSL_free(quic_transport_params);
David Benjaminb95d4b42018-05-02 16:11:34 -0400822 sk_SRTP_PROTECTION_PROFILE_free(srtp_profiles);
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700823 sk_CRYPTO_BUFFER_pop_free(client_CA, CRYPTO_BUFFER_free);
824}
825
David Benjamin27bbae42015-09-13 00:54:37 -0400826void SSL_free(SSL *ssl) {
827 if (ssl == NULL) {
828 return;
829 }
830
David Benjamin27bbae42015-09-13 00:54:37 -0400831 CRYPTO_free_ex_data(&g_ex_data_class_ssl, ssl, &ssl->ex_data);
832
David Benjamin27bbae42015-09-13 00:54:37 -0400833 BIO_free_all(ssl->rbio);
David Benjaminf715c422016-06-11 19:01:56 -0400834 BIO_free_all(ssl->wbio);
David Benjamin27bbae42015-09-13 00:54:37 -0400835
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700836 Delete(ssl->config);
837 ssl->config = NULL;
David Benjamin27bbae42015-09-13 00:54:37 -0400838
David Benjamin27bbae42015-09-13 00:54:37 -0400839 SSL_SESSION_free(ssl->session);
840
David Benjamin27bbae42015-09-13 00:54:37 -0400841 OPENSSL_free(ssl->tlsext_hostname);
David Benjamin27bbae42015-09-13 00:54:37 -0400842
843 if (ssl->method != NULL) {
844 ssl->method->ssl_free(ssl);
845 }
846 SSL_CTX_free(ssl->ctx);
David Benjamin98472cb2018-05-02 16:05:36 -0400847 SSL_CTX_free(ssl->session_ctx);
David Benjamin27bbae42015-09-13 00:54:37 -0400848
849 OPENSSL_free(ssl);
850}
851
852void SSL_set_connect_state(SSL *ssl) {
David Benjamin7e58c5e2017-10-11 13:01:08 -0400853 ssl->server = false;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400854 ssl->do_handshake = ssl_client_handshake;
David Benjamin27bbae42015-09-13 00:54:37 -0400855}
856
857void SSL_set_accept_state(SSL *ssl) {
David Benjamin7e58c5e2017-10-11 13:01:08 -0400858 ssl->server = true;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400859 ssl->do_handshake = ssl_server_handshake;
David Benjamin27bbae42015-09-13 00:54:37 -0400860}
861
David Benjamin4501bd52016-08-01 13:39:41 -0400862void SSL_set0_rbio(SSL *ssl, BIO *rbio) {
David Benjaminf715c422016-06-11 19:01:56 -0400863 BIO_free_all(ssl->rbio);
864 ssl->rbio = rbio;
865}
866
David Benjamin4501bd52016-08-01 13:39:41 -0400867void SSL_set0_wbio(SSL *ssl, BIO *wbio) {
David Benjaminf715c422016-06-11 19:01:56 -0400868 BIO_free_all(ssl->wbio);
David Benjamin27bbae42015-09-13 00:54:37 -0400869 ssl->wbio = wbio;
870}
871
David Benjaminf715c422016-06-11 19:01:56 -0400872void SSL_set_bio(SSL *ssl, BIO *rbio, BIO *wbio) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400873 // For historical reasons, this function has many different cases in ownership
874 // handling.
David Benjaminf715c422016-06-11 19:01:56 -0400875
David Benjaminc11ea9422017-08-29 16:33:21 -0400876 // If nothing has changed, do nothing
David Benjamin4501bd52016-08-01 13:39:41 -0400877 if (rbio == SSL_get_rbio(ssl) && wbio == SSL_get_wbio(ssl)) {
878 return;
879 }
880
David Benjaminc11ea9422017-08-29 16:33:21 -0400881 // If the two arguments are equal, one fewer reference is granted than
882 // taken.
David Benjaminf715c422016-06-11 19:01:56 -0400883 if (rbio != NULL && rbio == wbio) {
884 BIO_up_ref(rbio);
885 }
886
David Benjaminc11ea9422017-08-29 16:33:21 -0400887 // If only the wbio is changed, adopt only one reference.
David Benjaminf715c422016-06-11 19:01:56 -0400888 if (rbio == SSL_get_rbio(ssl)) {
David Benjamin4501bd52016-08-01 13:39:41 -0400889 SSL_set0_wbio(ssl, wbio);
David Benjaminf715c422016-06-11 19:01:56 -0400890 return;
891 }
David Benjamin4501bd52016-08-01 13:39:41 -0400892
David Benjaminc11ea9422017-08-29 16:33:21 -0400893 // There is an asymmetry here for historical reasons. If only the rbio is
894 // changed AND the rbio and wbio were originally different, then we only adopt
895 // one reference.
David Benjamin4501bd52016-08-01 13:39:41 -0400896 if (wbio == SSL_get_wbio(ssl) && SSL_get_rbio(ssl) != SSL_get_wbio(ssl)) {
897 SSL_set0_rbio(ssl, rbio);
David Benjaminf715c422016-06-11 19:01:56 -0400898 return;
899 }
900
David Benjaminc11ea9422017-08-29 16:33:21 -0400901 // Otherwise, adopt both references.
David Benjamin4501bd52016-08-01 13:39:41 -0400902 SSL_set0_rbio(ssl, rbio);
903 SSL_set0_wbio(ssl, wbio);
David Benjaminf715c422016-06-11 19:01:56 -0400904}
905
David Benjamin27bbae42015-09-13 00:54:37 -0400906BIO *SSL_get_rbio(const SSL *ssl) { return ssl->rbio; }
907
David Benjamindaf207a2017-01-03 18:37:41 -0500908BIO *SSL_get_wbio(const SSL *ssl) { return ssl->wbio; }
David Benjamin27bbae42015-09-13 00:54:37 -0400909
David Benjamin2be4aa72017-01-02 07:56:51 -0500910int SSL_do_handshake(SSL *ssl) {
911 ssl_reset_error_state(ssl);
David Benjamin15c14882016-03-14 14:25:46 -0400912
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400913 if (ssl->do_handshake == NULL) {
David Benjamin27bbae42015-09-13 00:54:37 -0400914 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_TYPE_NOT_SET);
915 return -1;
916 }
917
918 if (!SSL_in_init(ssl)) {
919 return 1;
920 }
921
David Benjaminc11ea9422017-08-29 16:33:21 -0400922 // Run the handshake.
David Benjamin8e7bbba2017-10-13 17:18:35 -0400923 SSL_HANDSHAKE *hs = ssl->s3->hs.get();
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400924
David Benjamin74795b32017-08-31 15:13:12 -0400925 bool early_return = false;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400926 int ret = ssl_run_handshake(hs, &early_return);
927 ssl_do_info_callback(
928 ssl, ssl->server ? SSL_CB_ACCEPT_EXIT : SSL_CB_CONNECT_EXIT, ret);
David Benjamince8c9d22016-11-14 10:45:16 +0900929 if (ret <= 0) {
930 return ret;
931 }
932
David Benjaminc11ea9422017-08-29 16:33:21 -0400933 // Destroy the handshake object if the handshake has completely finished.
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400934 if (!early_return) {
David Benjamin8e7bbba2017-10-13 17:18:35 -0400935 ssl->s3->hs.reset();
David Benjamin71666cb2018-05-01 17:14:27 -0400936 ssl_maybe_shed_handshake_config(ssl);
David Benjamince8c9d22016-11-14 10:45:16 +0900937 }
938
939 return 1;
David Benjamin27bbae42015-09-13 00:54:37 -0400940}
941
942int SSL_connect(SSL *ssl) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400943 if (ssl->do_handshake == NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400944 // Not properly initialized yet
David Benjamin27bbae42015-09-13 00:54:37 -0400945 SSL_set_connect_state(ssl);
946 }
947
David Benjamin15c14882016-03-14 14:25:46 -0400948 return SSL_do_handshake(ssl);
David Benjamin27bbae42015-09-13 00:54:37 -0400949}
950
951int SSL_accept(SSL *ssl) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400952 if (ssl->do_handshake == NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400953 // Not properly initialized yet
David Benjamin27bbae42015-09-13 00:54:37 -0400954 SSL_set_accept_state(ssl);
955 }
956
David Benjamin15c14882016-03-14 14:25:46 -0400957 return SSL_do_handshake(ssl);
David Benjamin27bbae42015-09-13 00:54:37 -0400958}
959
David Benjamin7934f082017-08-01 16:32:25 -0400960static int ssl_do_post_handshake(SSL *ssl, const SSLMessage &msg) {
David Benjamind1e3ce12017-10-06 18:31:15 -0400961 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjamin7934f082017-08-01 16:32:25 -0400962 return tls13_post_handshake(ssl, msg);
963 }
964
David Benjamin71666cb2018-05-01 17:14:27 -0400965 // Check for renegotiation on the server before parsing to use the correct
966 // error. Renegotiation is triggered by a different message for servers.
967 if (ssl->server) {
968 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION);
969 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_NO_RENEGOTIATION);
970 return 0;
David Benjamin163f29a2016-07-28 11:05:58 -0400971 }
972
David Benjamin7934f082017-08-01 16:32:25 -0400973 if (msg.type != SSL3_MT_HELLO_REQUEST || CBS_len(&msg.body) != 0) {
David Benjamind1e3ce12017-10-06 18:31:15 -0400974 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin163f29a2016-07-28 11:05:58 -0400975 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HELLO_REQUEST);
976 return 0;
977 }
978
David Benjamin71666cb2018-05-01 17:14:27 -0400979 if (ssl->renegotiate_mode == ssl_renegotiate_ignore) {
980 return 1; // Ignore the HelloRequest.
David Benjamin163f29a2016-07-28 11:05:58 -0400981 }
982
David Benjamin71666cb2018-05-01 17:14:27 -0400983 if (!ssl_can_renegotiate(ssl) ||
984 // Renegotiation is only supported at quiescent points in the application
985 // protocol, namely in HTTPS, just before reading the HTTP response.
986 // Require the record-layer be idle and avoid complexities of sending a
987 // handshake record while an application_data record is being written.
988 !ssl->s3->write_buffer.empty() ||
David Benjamina031b612017-10-11 20:48:25 -0400989 ssl->s3->write_shutdown != ssl_shutdown_none) {
David Benjamin71666cb2018-05-01 17:14:27 -0400990 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION);
991 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_NO_RENEGOTIATION);
992 return 0;
David Benjamin163f29a2016-07-28 11:05:58 -0400993 }
994
David Benjaminc11ea9422017-08-29 16:33:21 -0400995 // Begin a new handshake.
David Benjamin8e7bbba2017-10-13 17:18:35 -0400996 if (ssl->s3->hs != nullptr) {
David Benjamin2644a132016-12-11 13:41:17 -0500997 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
998 return 0;
999 }
1000 ssl->s3->hs = ssl_handshake_new(ssl);
David Benjamin8e7bbba2017-10-13 17:18:35 -04001001 if (ssl->s3->hs == nullptr) {
David Benjamin2644a132016-12-11 13:41:17 -05001002 return 0;
1003 }
1004
David Benjamin163f29a2016-07-28 11:05:58 -04001005 ssl->s3->total_renegotiations++;
David Benjamin163f29a2016-07-28 11:05:58 -04001006 return 1;
David Benjamin163f29a2016-07-28 11:05:58 -04001007}
1008
David Benjamina031b612017-10-11 20:48:25 -04001009static int ssl_read_impl(SSL *ssl) {
David Benjamin2be4aa72017-01-02 07:56:51 -05001010 ssl_reset_error_state(ssl);
David Benjamin15c14882016-03-14 14:25:46 -04001011
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001012 if (ssl->do_handshake == NULL) {
David Benjamin27bbae42015-09-13 00:54:37 -04001013 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
1014 return -1;
1015 }
1016
David Benjaminf6632da2017-10-12 19:11:47 -04001017 // Replay post-handshake message errors.
1018 if (!check_read_error(ssl)) {
1019 return -1;
1020 }
1021
David Benjamina031b612017-10-11 20:48:25 -04001022 while (ssl->s3->pending_app_data.empty()) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001023 // Complete the current handshake, if any. False Start will cause
1024 // |SSL_do_handshake| to return mid-handshake, so this may require multiple
1025 // iterations.
Steven Valdez681eb6a2016-12-19 13:19:29 -05001026 while (!ssl_can_read(ssl)) {
David Benjamin163f29a2016-07-28 11:05:58 -04001027 int ret = SSL_do_handshake(ssl);
1028 if (ret < 0) {
1029 return ret;
1030 }
1031 if (ret == 0) {
1032 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
1033 return -1;
1034 }
1035 }
1036
David Benjamin40e94702017-10-06 18:26:36 -04001037 // Process any buffered post-handshake messages.
David Benjamin7934f082017-08-01 16:32:25 -04001038 SSLMessage msg;
David Benjamin40e94702017-10-06 18:26:36 -04001039 if (ssl->method->get_message(ssl, &msg)) {
Steven Valdezcd8470f2017-10-11 12:29:36 -04001040 // If we received an interrupt in early read (EndOfEarlyData), loop again
1041 // for the handshake to process it.
1042 if (SSL_in_init(ssl)) {
1043 ssl->s3->hs->can_early_read = false;
1044 continue;
1045 }
1046
David Benjaminc11ea9422017-08-29 16:33:21 -04001047 // Handle the post-handshake message and try again.
David Benjamin7934f082017-08-01 16:32:25 -04001048 if (!ssl_do_post_handshake(ssl, msg)) {
David Benjaminf6632da2017-10-12 19:11:47 -04001049 ssl_set_read_error(ssl);
David Benjamin7934f082017-08-01 16:32:25 -04001050 return -1;
1051 }
1052 ssl->method->next_message(ssl);
David Benjamin40e94702017-10-06 18:26:36 -04001053 continue; // Loop again. We may have begun a new handshake.
David Benjaminc79845c2016-03-10 01:28:00 -05001054 }
David Benjamin40e94702017-10-06 18:26:36 -04001055
David Benjamina031b612017-10-11 20:48:25 -04001056 uint8_t alert = SSL_AD_DECODE_ERROR;
1057 size_t consumed = 0;
David Benjaminf6632da2017-10-12 19:11:47 -04001058 auto ret = ssl_open_app_data(ssl, &ssl->s3->pending_app_data, &consumed,
David Benjaminea712e32017-10-13 16:50:39 -04001059 &alert, ssl->s3->read_buffer.span());
David Benjamina031b612017-10-11 20:48:25 -04001060 bool retry;
1061 int bio_ret = ssl_handle_open_record(ssl, &retry, ret, consumed, alert);
1062 if (bio_ret <= 0) {
1063 return bio_ret;
1064 }
1065 if (!retry) {
1066 assert(!ssl->s3->pending_app_data.empty());
David Benjamin40e94702017-10-06 18:26:36 -04001067 ssl->s3->key_update_count = 0;
1068 }
David Benjaminc79845c2016-03-10 01:28:00 -05001069 }
David Benjamina031b612017-10-11 20:48:25 -04001070
1071 return 1;
David Benjamin15c14882016-03-14 14:25:46 -04001072}
1073
1074int SSL_read(SSL *ssl, void *buf, int num) {
David Benjamina031b612017-10-11 20:48:25 -04001075 int ret = SSL_peek(ssl, buf, num);
1076 if (ret <= 0) {
1077 return ret;
1078 }
1079 // TODO(davidben): In DTLS, should the rest of the record be discarded? DTLS
1080 // is not a stream. See https://crbug.com/boringssl/65.
1081 ssl->s3->pending_app_data =
1082 ssl->s3->pending_app_data.subspan(static_cast<size_t>(ret));
1083 if (ssl->s3->pending_app_data.empty()) {
David Benjaminea712e32017-10-13 16:50:39 -04001084 ssl->s3->read_buffer.DiscardConsumed();
David Benjamina031b612017-10-11 20:48:25 -04001085 }
1086 return ret;
David Benjamin27bbae42015-09-13 00:54:37 -04001087}
1088
1089int SSL_peek(SSL *ssl, void *buf, int num) {
David Benjamina031b612017-10-11 20:48:25 -04001090 int ret = ssl_read_impl(ssl);
1091 if (ret <= 0) {
1092 return ret;
1093 }
1094 if (num <= 0) {
1095 return num;
1096 }
1097 size_t todo =
1098 std::min(ssl->s3->pending_app_data.size(), static_cast<size_t>(num));
1099 OPENSSL_memcpy(buf, ssl->s3->pending_app_data.data(), todo);
1100 return static_cast<int>(todo);
David Benjamin27bbae42015-09-13 00:54:37 -04001101}
1102
1103int SSL_write(SSL *ssl, const void *buf, int num) {
David Benjamin2be4aa72017-01-02 07:56:51 -05001104 ssl_reset_error_state(ssl);
David Benjamin15c14882016-03-14 14:25:46 -04001105
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001106 if (ssl->do_handshake == NULL) {
David Benjamin27bbae42015-09-13 00:54:37 -04001107 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
1108 return -1;
1109 }
1110
David Benjamin23c25d52017-10-06 16:02:47 -04001111 if (ssl->s3->write_shutdown != ssl_shutdown_none) {
David Benjamin27bbae42015-09-13 00:54:37 -04001112 OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
1113 return -1;
1114 }
1115
David Benjamin74795b32017-08-31 15:13:12 -04001116 int ret = 0;
1117 bool needs_handshake = false;
Steven Valdeze831a812017-03-09 14:56:07 -05001118 do {
David Benjaminc11ea9422017-08-29 16:33:21 -04001119 // If necessary, complete the handshake implicitly.
Steven Valdeze831a812017-03-09 14:56:07 -05001120 if (!ssl_can_write(ssl)) {
1121 ret = SSL_do_handshake(ssl);
1122 if (ret < 0) {
1123 return ret;
1124 }
1125 if (ret == 0) {
1126 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
1127 return -1;
1128 }
David Benjamind7ac1432016-03-10 00:41:25 -05001129 }
David Benjamind7ac1432016-03-10 00:41:25 -05001130
David Benjaminf5260812017-07-12 17:16:39 -04001131 ret = ssl->method->write_app_data(ssl, &needs_handshake,
1132 (const uint8_t *)buf, num);
Steven Valdeze831a812017-03-09 14:56:07 -05001133 } while (needs_handshake);
1134 return ret;
David Benjamin27bbae42015-09-13 00:54:37 -04001135}
1136
1137int SSL_shutdown(SSL *ssl) {
David Benjamin2be4aa72017-01-02 07:56:51 -05001138 ssl_reset_error_state(ssl);
David Benjamin15c14882016-03-14 14:25:46 -04001139
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001140 if (ssl->do_handshake == NULL) {
David Benjamin27bbae42015-09-13 00:54:37 -04001141 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
1142 return -1;
1143 }
1144
David Benjaminc11ea9422017-08-29 16:33:21 -04001145 // If we are in the middle of a handshake, silently succeed. Consumers often
1146 // call this function before |SSL_free|, whether the handshake succeeded or
1147 // not. We assume the caller has already handled failed handshakes.
Steven Valdeza14934f2016-02-29 10:05:08 -05001148 if (SSL_in_init(ssl)) {
David Benjamin12709db2017-02-17 13:51:50 -05001149 return 1;
Steven Valdeza14934f2016-02-29 10:05:08 -05001150 }
1151
David Benjamin27bbae42015-09-13 00:54:37 -04001152 if (ssl->quiet_shutdown) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001153 // Do nothing if configured not to send a close_notify.
David Benjamin23c25d52017-10-06 16:02:47 -04001154 ssl->s3->write_shutdown = ssl_shutdown_close_notify;
1155 ssl->s3->read_shutdown = ssl_shutdown_close_notify;
David Benjamin27bbae42015-09-13 00:54:37 -04001156 return 1;
1157 }
1158
David Benjaminc11ea9422017-08-29 16:33:21 -04001159 // This function completes in two stages. It sends a close_notify and then it
1160 // waits for a close_notify to come in. Perform exactly one action and return
1161 // whether or not it succeeds.
David Benjamin27bbae42015-09-13 00:54:37 -04001162
David Benjamin23c25d52017-10-06 16:02:47 -04001163 if (ssl->s3->write_shutdown != ssl_shutdown_close_notify) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001164 // Send a close_notify.
David Benjamind1e3ce12017-10-06 18:31:15 -04001165 if (ssl_send_alert(ssl, SSL3_AL_WARNING, SSL_AD_CLOSE_NOTIFY) <= 0) {
David Benjaminfa214e42016-05-10 17:03:10 -04001166 return -1;
David Benjamin27bbae42015-09-13 00:54:37 -04001167 }
1168 } else if (ssl->s3->alert_dispatch) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001169 // Finish sending the close_notify.
David Benjaminf0ee9072016-06-15 17:44:37 -04001170 if (ssl->method->dispatch_alert(ssl) <= 0) {
David Benjaminfa214e42016-05-10 17:03:10 -04001171 return -1;
David Benjamin27bbae42015-09-13 00:54:37 -04001172 }
David Benjamin23c25d52017-10-06 16:02:47 -04001173 } else if (ssl->s3->read_shutdown != ssl_shutdown_close_notify) {
David Benjamina031b612017-10-11 20:48:25 -04001174 if (SSL_is_dtls(ssl)) {
1175 // Bidirectional shutdown doesn't make sense for an unordered
1176 // transport. DTLS alerts also aren't delivered reliably, so we may even
1177 // time out because the peer never received our close_notify. Report to
1178 // the caller that the channel has fully shut down.
1179 if (ssl->s3->read_shutdown == ssl_shutdown_error) {
David Benjamin8e7bbba2017-10-13 17:18:35 -04001180 ERR_restore_state(ssl->s3->read_error.get());
David Benjamina031b612017-10-11 20:48:25 -04001181 return -1;
David Benjamind9229f92017-10-06 17:36:20 -04001182 }
David Benjamina031b612017-10-11 20:48:25 -04001183 ssl->s3->read_shutdown = ssl_shutdown_close_notify;
1184 } else {
1185 // Keep discarding data until we see a close_notify.
1186 for (;;) {
1187 ssl->s3->pending_app_data = Span<uint8_t>();
1188 int ret = ssl_read_impl(ssl);
1189 if (ret <= 0) {
1190 break;
1191 }
1192 }
1193 if (ssl->s3->read_shutdown != ssl_shutdown_close_notify) {
1194 return -1;
1195 }
David Benjamin27bbae42015-09-13 00:54:37 -04001196 }
1197 }
1198
David Benjaminc11ea9422017-08-29 16:33:21 -04001199 // Return 0 for unidirectional shutdown and 1 for bidirectional shutdown.
David Benjamin23c25d52017-10-06 16:02:47 -04001200 return ssl->s3->read_shutdown == ssl_shutdown_close_notify;
David Benjamin27bbae42015-09-13 00:54:37 -04001201}
1202
David Benjamin1d4f4c02016-07-26 18:03:08 -04001203int SSL_send_fatal_alert(SSL *ssl, uint8_t alert) {
1204 if (ssl->s3->alert_dispatch) {
1205 if (ssl->s3->send_alert[0] != SSL3_AL_FATAL ||
1206 ssl->s3->send_alert[1] != alert) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001207 // We are already attempting to write a different alert.
David Benjamin1d4f4c02016-07-26 18:03:08 -04001208 OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
1209 return -1;
1210 }
1211 return ssl->method->dispatch_alert(ssl);
1212 }
1213
David Benjamind1e3ce12017-10-06 18:31:15 -04001214 return ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
David Benjamin1d4f4c02016-07-26 18:03:08 -04001215}
1216
Nick Harper3c034b22017-12-22 15:50:43 -08001217int SSL_set_quic_transport_params(SSL *ssl, const uint8_t *params,
1218 size_t params_len) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001219 if (!ssl->config) {
Nick Harper3c034b22017-12-22 15:50:43 -08001220 return 0;
1221 }
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001222 ssl->config->quic_transport_params =
1223 (uint8_t *)BUF_memdup(params, params_len);
1224 if (!ssl->config->quic_transport_params) {
1225 return 0;
1226 }
1227 ssl->config->quic_transport_params_len = params_len;
Nick Harper3c034b22017-12-22 15:50:43 -08001228 return 1;
1229}
1230
1231void SSL_get_peer_quic_transport_params(const SSL *ssl,
1232 const uint8_t **out_params,
1233 size_t *out_params_len) {
1234 *out_params = ssl->s3->peer_quic_transport_params.data();
1235 *out_params_len = ssl->s3->peer_quic_transport_params.size();
1236}
1237
Steven Valdez08b65f42016-12-07 15:29:45 -05001238void SSL_CTX_set_early_data_enabled(SSL_CTX *ctx, int enabled) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001239 ctx->enable_early_data = !!enabled;
Alessandro Ghedini67bb45f2017-03-30 16:33:24 -05001240}
1241
Steven Valdez520e1222017-06-13 12:45:25 -04001242void SSL_CTX_set_tls13_variant(SSL_CTX *ctx, enum tls13_variant_t variant) {
1243 ctx->tls13_variant = variant;
1244}
1245
Steven Valdez52586f92017-07-11 15:08:32 -04001246void SSL_set_tls13_variant(SSL *ssl, enum tls13_variant_t variant) {
1247 ssl->tls13_variant = variant;
1248}
1249
Alessandro Ghedini67bb45f2017-03-30 16:33:24 -05001250void SSL_set_early_data_enabled(SSL *ssl, int enabled) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001251 ssl->enable_early_data = !!enabled;
Steven Valdez08b65f42016-12-07 15:29:45 -05001252}
1253
Steven Valdeze831a812017-03-09 14:56:07 -05001254int SSL_in_early_data(const SSL *ssl) {
1255 if (ssl->s3->hs == NULL) {
1256 return 0;
1257 }
1258 return ssl->s3->hs->in_early_data;
1259}
1260
Steven Valdez2d850622017-01-11 11:34:52 -05001261int SSL_early_data_accepted(const SSL *ssl) {
David Benjamin02e62562017-12-18 18:04:01 -05001262 return ssl->s3->early_data_accepted;
Steven Valdez2d850622017-01-11 11:34:52 -05001263}
1264
Steven Valdeze831a812017-03-09 14:56:07 -05001265void SSL_reset_early_data_reject(SSL *ssl) {
David Benjamin8e7bbba2017-10-13 17:18:35 -04001266 SSL_HANDSHAKE *hs = ssl->s3->hs.get();
Steven Valdeze831a812017-03-09 14:56:07 -05001267 if (hs == NULL ||
1268 hs->wait != ssl_hs_early_data_rejected) {
1269 abort();
1270 }
1271
1272 hs->wait = ssl_hs_ok;
David Benjaminfd45ee72017-08-31 14:49:09 -04001273 hs->in_early_data = false;
David Benjamin31b0c9b2017-07-20 14:49:15 -04001274 hs->early_session.reset();
Steven Valdeze831a812017-03-09 14:56:07 -05001275
David Benjaminc11ea9422017-08-29 16:33:21 -04001276 // Discard any unfinished writes from the perspective of |SSL_write|'s
1277 // retry. The handshake will transparently flush out the pending record
1278 // (discarded by the server) to keep the framing correct.
David Benjamin046bc1f2017-08-31 15:06:42 -04001279 ssl->s3->wpend_pending = false;
Steven Valdeze831a812017-03-09 14:56:07 -05001280}
1281
David Benjamin287fc4f2016-12-10 15:12:03 -05001282static int bio_retry_reason_to_error(int reason) {
1283 switch (reason) {
1284 case BIO_RR_CONNECT:
1285 return SSL_ERROR_WANT_CONNECT;
1286 case BIO_RR_ACCEPT:
1287 return SSL_ERROR_WANT_ACCEPT;
1288 default:
1289 return SSL_ERROR_SYSCALL;
1290 }
1291}
David Benjamin27bbae42015-09-13 00:54:37 -04001292
David Benjamin287fc4f2016-12-10 15:12:03 -05001293int SSL_get_error(const SSL *ssl, int ret_code) {
David Benjamin27bbae42015-09-13 00:54:37 -04001294 if (ret_code > 0) {
1295 return SSL_ERROR_NONE;
1296 }
1297
David Benjaminc11ea9422017-08-29 16:33:21 -04001298 // Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
1299 // where we do encode the error
David Benjamin287fc4f2016-12-10 15:12:03 -05001300 uint32_t err = ERR_peek_error();
David Benjamin27bbae42015-09-13 00:54:37 -04001301 if (err != 0) {
1302 if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
1303 return SSL_ERROR_SYSCALL;
1304 }
1305 return SSL_ERROR_SSL;
1306 }
1307
1308 if (ret_code == 0) {
David Benjamin23c25d52017-10-06 16:02:47 -04001309 if (ssl->s3->read_shutdown == ssl_shutdown_close_notify) {
David Benjamin27bbae42015-09-13 00:54:37 -04001310 return SSL_ERROR_ZERO_RETURN;
1311 }
David Benjaminc11ea9422017-08-29 16:33:21 -04001312 // An EOF was observed which violates the protocol, and the underlying
1313 // transport does not participate in the error queue. Bubble up to the
1314 // caller.
David Benjamin27bbae42015-09-13 00:54:37 -04001315 return SSL_ERROR_SYSCALL;
1316 }
1317
David Benjamin32ce0ac2017-10-13 19:17:22 -04001318 switch (ssl->s3->rwstate) {
David Benjamin287fc4f2016-12-10 15:12:03 -05001319 case SSL_PENDING_SESSION:
1320 return SSL_ERROR_PENDING_SESSION;
David Benjamin27bbae42015-09-13 00:54:37 -04001321
David Benjamin287fc4f2016-12-10 15:12:03 -05001322 case SSL_CERTIFICATE_SELECTION_PENDING:
1323 return SSL_ERROR_PENDING_CERTIFICATE;
David Benjamin27bbae42015-09-13 00:54:37 -04001324
Adam Langley3fe8fa72018-01-26 09:14:30 -08001325 case SSL_HANDOFF:
1326 return SSL_ERROR_HANDOFF;
1327
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001328 case SSL_HANDBACK:
1329 return SSL_ERROR_HANDBACK;
1330
David Benjamin287fc4f2016-12-10 15:12:03 -05001331 case SSL_READING: {
1332 BIO *bio = SSL_get_rbio(ssl);
1333 if (BIO_should_read(bio)) {
1334 return SSL_ERROR_WANT_READ;
David Benjamin27bbae42015-09-13 00:54:37 -04001335 }
1336
David Benjamin287fc4f2016-12-10 15:12:03 -05001337 if (BIO_should_write(bio)) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001338 // TODO(davidben): OpenSSL historically checked for writes on the read
1339 // BIO. Can this be removed?
David Benjamin287fc4f2016-12-10 15:12:03 -05001340 return SSL_ERROR_WANT_WRITE;
David Benjamin27bbae42015-09-13 00:54:37 -04001341 }
1342
David Benjamin287fc4f2016-12-10 15:12:03 -05001343 if (BIO_should_io_special(bio)) {
1344 return bio_retry_reason_to_error(BIO_get_retry_reason(bio));
David Benjamin27bbae42015-09-13 00:54:37 -04001345 }
1346
David Benjamin287fc4f2016-12-10 15:12:03 -05001347 break;
1348 }
1349
1350 case SSL_WRITING: {
1351 BIO *bio = SSL_get_wbio(ssl);
1352 if (BIO_should_write(bio)) {
1353 return SSL_ERROR_WANT_WRITE;
David Benjamin27bbae42015-09-13 00:54:37 -04001354 }
1355
David Benjamin287fc4f2016-12-10 15:12:03 -05001356 if (BIO_should_read(bio)) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001357 // TODO(davidben): OpenSSL historically checked for reads on the write
1358 // BIO. Can this be removed?
David Benjamin287fc4f2016-12-10 15:12:03 -05001359 return SSL_ERROR_WANT_READ;
1360 }
1361
1362 if (BIO_should_io_special(bio)) {
1363 return bio_retry_reason_to_error(BIO_get_retry_reason(bio));
1364 }
1365
1366 break;
David Benjamin27bbae42015-09-13 00:54:37 -04001367 }
David Benjamin27bbae42015-09-13 00:54:37 -04001368
David Benjamin287fc4f2016-12-10 15:12:03 -05001369 case SSL_X509_LOOKUP:
1370 return SSL_ERROR_WANT_X509_LOOKUP;
David Benjamin27bbae42015-09-13 00:54:37 -04001371
David Benjamin287fc4f2016-12-10 15:12:03 -05001372 case SSL_CHANNEL_ID_LOOKUP:
1373 return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
David Benjamin27bbae42015-09-13 00:54:37 -04001374
David Benjamin287fc4f2016-12-10 15:12:03 -05001375 case SSL_PRIVATE_KEY_OPERATION:
1376 return SSL_ERROR_WANT_PRIVATE_KEY_OPERATION;
Adam Langley4c341d02017-03-08 19:33:21 -08001377
1378 case SSL_PENDING_TICKET:
1379 return SSL_ERROR_PENDING_TICKET;
Steven Valdeze831a812017-03-09 14:56:07 -05001380
1381 case SSL_EARLY_DATA_REJECTED:
1382 return SSL_ERROR_EARLY_DATA_REJECTED;
David Benjamin3a1dd462017-07-11 16:13:10 -04001383
1384 case SSL_CERTIFICATE_VERIFY:
1385 return SSL_ERROR_WANT_CERTIFICATE_VERIFY;
David Benjamin27bbae42015-09-13 00:54:37 -04001386 }
1387
1388 return SSL_ERROR_SYSCALL;
1389}
1390
David Benjamin27bbae42015-09-13 00:54:37 -04001391uint32_t SSL_CTX_set_options(SSL_CTX *ctx, uint32_t options) {
1392 ctx->options |= options;
1393 return ctx->options;
1394}
1395
1396uint32_t SSL_CTX_clear_options(SSL_CTX *ctx, uint32_t options) {
1397 ctx->options &= ~options;
1398 return ctx->options;
1399}
1400
1401uint32_t SSL_CTX_get_options(const SSL_CTX *ctx) { return ctx->options; }
1402
1403uint32_t SSL_set_options(SSL *ssl, uint32_t options) {
1404 ssl->options |= options;
1405 return ssl->options;
1406}
1407
1408uint32_t SSL_clear_options(SSL *ssl, uint32_t options) {
1409 ssl->options &= ~options;
1410 return ssl->options;
1411}
1412
1413uint32_t SSL_get_options(const SSL *ssl) { return ssl->options; }
1414
1415uint32_t SSL_CTX_set_mode(SSL_CTX *ctx, uint32_t mode) {
1416 ctx->mode |= mode;
1417 return ctx->mode;
1418}
1419
1420uint32_t SSL_CTX_clear_mode(SSL_CTX *ctx, uint32_t mode) {
1421 ctx->mode &= ~mode;
1422 return ctx->mode;
1423}
1424
1425uint32_t SSL_CTX_get_mode(const SSL_CTX *ctx) { return ctx->mode; }
1426
1427uint32_t SSL_set_mode(SSL *ssl, uint32_t mode) {
1428 ssl->mode |= mode;
1429 return ssl->mode;
1430}
1431
1432uint32_t SSL_clear_mode(SSL *ssl, uint32_t mode) {
1433 ssl->mode &= ~mode;
1434 return ssl->mode;
1435}
1436
1437uint32_t SSL_get_mode(const SSL *ssl) { return ssl->mode; }
1438
Adam Langleyd519bf62016-12-12 11:16:44 -08001439void SSL_CTX_set0_buffer_pool(SSL_CTX *ctx, CRYPTO_BUFFER_POOL *pool) {
1440 ctx->pool = pool;
1441}
1442
David Benjaminee0c8272015-09-13 01:03:54 -04001443int SSL_get_tls_unique(const SSL *ssl, uint8_t *out, size_t *out_len,
1444 size_t max_out) {
David Benjaminf5260812017-07-12 17:16:39 -04001445 *out_len = 0;
1446 OPENSSL_memset(out, 0, max_out);
1447
David Benjaminc11ea9422017-08-29 16:33:21 -04001448 // tls-unique is not defined for SSL 3.0 or TLS 1.3.
David Benjaminced00b42016-10-08 11:24:09 -04001449 if (!ssl->s3->initial_handshake_complete ||
David Benjamind1e3ce12017-10-06 18:31:15 -04001450 ssl_protocol_version(ssl) < TLS1_VERSION ||
1451 ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjaminf5260812017-07-12 17:16:39 -04001452 return 0;
David Benjaminced00b42016-10-08 11:24:09 -04001453 }
1454
David Benjaminc11ea9422017-08-29 16:33:21 -04001455 // The tls-unique value is the first Finished message in the handshake, which
1456 // is the client's in a full handshake and the server's for a resumption. See
1457 // https://tools.ietf.org/html/rfc5929#section-3.1.
David Benjaminee0c8272015-09-13 01:03:54 -04001458 const uint8_t *finished = ssl->s3->previous_client_finished;
1459 size_t finished_len = ssl->s3->previous_client_finished_len;
Steven Valdez87eab492016-06-27 16:34:59 -04001460 if (ssl->session != NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001461 // tls-unique is broken for resumed sessions unless EMS is used.
David Benjaminee0c8272015-09-13 01:03:54 -04001462 if (!ssl->session->extended_master_secret) {
David Benjaminf5260812017-07-12 17:16:39 -04001463 return 0;
David Benjaminee0c8272015-09-13 01:03:54 -04001464 }
1465 finished = ssl->s3->previous_server_finished;
1466 finished_len = ssl->s3->previous_server_finished_len;
1467 }
1468
David Benjaminee0c8272015-09-13 01:03:54 -04001469 *out_len = finished_len;
1470 if (finished_len > max_out) {
1471 *out_len = max_out;
1472 }
1473
David Benjamin17cf2cb2016-12-13 01:07:13 -05001474 OPENSSL_memcpy(out, finished, *out_len);
David Benjaminee0c8272015-09-13 01:03:54 -04001475 return 1;
David Benjaminee0c8272015-09-13 01:03:54 -04001476}
1477
David Benjamin5960a902017-02-14 20:07:11 -05001478static int set_session_id_context(CERT *cert, const uint8_t *sid_ctx,
David Benjamin30c4c302016-12-07 22:35:24 -05001479 size_t sid_ctx_len) {
David Benjamin5960a902017-02-14 20:07:11 -05001480 if (sid_ctx_len > sizeof(cert->sid_ctx)) {
David Benjamin3570d732015-06-29 00:28:17 -04001481 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -08001482 return 0;
1483 }
David Benjamin30c4c302016-12-07 22:35:24 -05001484
David Benjamina3d76d02017-07-14 19:36:07 -04001485 static_assert(sizeof(cert->sid_ctx) < 256, "sid_ctx too large");
David Benjamin5960a902017-02-14 20:07:11 -05001486 cert->sid_ctx_length = (uint8_t)sid_ctx_len;
1487 OPENSSL_memcpy(cert->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001488 return 1;
1489}
Adam Langley95c29f32014-06-20 12:00:00 -07001490
David Benjamin5960a902017-02-14 20:07:11 -05001491int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
1492 size_t sid_ctx_len) {
1493 return set_session_id_context(ctx->cert, sid_ctx, sid_ctx_len);
1494}
1495
Adam Langleyfcf25832014-12-18 17:42:32 -08001496int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
David Benjamin30c4c302016-12-07 22:35:24 -05001497 size_t sid_ctx_len) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001498 if (!ssl->config) {
1499 return 0;
1500 }
1501 return set_session_id_context(ssl->config->cert, sid_ctx, sid_ctx_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001502}
Adam Langley95c29f32014-06-20 12:00:00 -07001503
David Benjamin07820b52016-12-07 21:13:41 -05001504const uint8_t *SSL_get0_session_id_context(const SSL *ssl, size_t *out_len) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001505 if (!ssl->config) {
1506 assert(ssl->config);
1507 *out_len = 0;
1508 return NULL;
1509 }
1510 *out_len = ssl->config->cert->sid_ctx_length;
1511 return ssl->config->cert->sid_ctx;
David Benjamin07820b52016-12-07 21:13:41 -05001512}
1513
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001514void SSL_certs_clear(SSL *ssl) {
1515 if (!ssl->config) {
1516 return;
1517 }
1518 ssl_cert_clear_certs(ssl->config->cert);
1519}
Adam Langley95c29f32014-06-20 12:00:00 -07001520
David Benjamin066fe0a2015-10-17 21:11:33 -04001521int SSL_get_fd(const SSL *ssl) { return SSL_get_rfd(ssl); }
Adam Langley95c29f32014-06-20 12:00:00 -07001522
David Benjamin066fe0a2015-10-17 21:11:33 -04001523int SSL_get_rfd(const SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001524 int ret = -1;
David Benjamin066fe0a2015-10-17 21:11:33 -04001525 BIO *b = BIO_find_type(SSL_get_rbio(ssl), BIO_TYPE_DESCRIPTOR);
1526 if (b != NULL) {
1527 BIO_get_fd(b, &ret);
Adam Langleyfcf25832014-12-18 17:42:32 -08001528 }
1529 return ret;
1530}
Adam Langley95c29f32014-06-20 12:00:00 -07001531
David Benjamin066fe0a2015-10-17 21:11:33 -04001532int SSL_get_wfd(const SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001533 int ret = -1;
David Benjamin066fe0a2015-10-17 21:11:33 -04001534 BIO *b = BIO_find_type(SSL_get_wbio(ssl), BIO_TYPE_DESCRIPTOR);
1535 if (b != NULL) {
1536 BIO_get_fd(b, &ret);
Adam Langleyfcf25832014-12-18 17:42:32 -08001537 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001538 return ret;
1539}
Adam Langley95c29f32014-06-20 12:00:00 -07001540
David Benjamin066fe0a2015-10-17 21:11:33 -04001541int SSL_set_fd(SSL *ssl, int fd) {
David Benjamin22ce9b22016-03-06 19:26:52 -05001542 BIO *bio = BIO_new(BIO_s_socket());
Adam Langleyfcf25832014-12-18 17:42:32 -08001543 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001544 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
David Benjamin066fe0a2015-10-17 21:11:33 -04001545 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001546 }
1547 BIO_set_fd(bio, fd, BIO_NOCLOSE);
David Benjamin066fe0a2015-10-17 21:11:33 -04001548 SSL_set_bio(ssl, bio, bio);
1549 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001550}
Adam Langley95c29f32014-06-20 12:00:00 -07001551
David Benjamin066fe0a2015-10-17 21:11:33 -04001552int SSL_set_wfd(SSL *ssl, int fd) {
David Benjamin2f871122016-05-20 14:27:17 -04001553 BIO *rbio = SSL_get_rbio(ssl);
1554 if (rbio == NULL || BIO_method_type(rbio) != BIO_TYPE_SOCKET ||
1555 BIO_get_fd(rbio, NULL) != fd) {
David Benjamin22ce9b22016-03-06 19:26:52 -05001556 BIO *bio = BIO_new(BIO_s_socket());
Adam Langleyfcf25832014-12-18 17:42:32 -08001557 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001558 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
David Benjamin066fe0a2015-10-17 21:11:33 -04001559 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001560 }
1561 BIO_set_fd(bio, fd, BIO_NOCLOSE);
David Benjamin4501bd52016-08-01 13:39:41 -04001562 SSL_set0_wbio(ssl, bio);
Adam Langleyfcf25832014-12-18 17:42:32 -08001563 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -04001564 // Copy the rbio over to the wbio.
David Benjamin4501bd52016-08-01 13:39:41 -04001565 BIO_up_ref(rbio);
1566 SSL_set0_wbio(ssl, rbio);
Adam Langleyfcf25832014-12-18 17:42:32 -08001567 }
1568
David Benjamin066fe0a2015-10-17 21:11:33 -04001569 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001570}
Adam Langley95c29f32014-06-20 12:00:00 -07001571
David Benjamin066fe0a2015-10-17 21:11:33 -04001572int SSL_set_rfd(SSL *ssl, int fd) {
David Benjamin2f871122016-05-20 14:27:17 -04001573 BIO *wbio = SSL_get_wbio(ssl);
1574 if (wbio == NULL || BIO_method_type(wbio) != BIO_TYPE_SOCKET ||
1575 BIO_get_fd(wbio, NULL) != fd) {
David Benjamin22ce9b22016-03-06 19:26:52 -05001576 BIO *bio = BIO_new(BIO_s_socket());
Adam Langleyfcf25832014-12-18 17:42:32 -08001577 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001578 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
David Benjamin066fe0a2015-10-17 21:11:33 -04001579 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001580 }
1581 BIO_set_fd(bio, fd, BIO_NOCLOSE);
David Benjamin4501bd52016-08-01 13:39:41 -04001582 SSL_set0_rbio(ssl, bio);
Adam Langleyfcf25832014-12-18 17:42:32 -08001583 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -04001584 // Copy the wbio over to the rbio.
David Benjamin4501bd52016-08-01 13:39:41 -04001585 BIO_up_ref(wbio);
1586 SSL_set0_rbio(ssl, wbio);
Adam Langleyfcf25832014-12-18 17:42:32 -08001587 }
David Benjamin066fe0a2015-10-17 21:11:33 -04001588 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001589}
Adam Langley95c29f32014-06-20 12:00:00 -07001590
David Benjamin49ddf412016-10-08 11:56:01 -04001591static size_t copy_finished(void *out, size_t out_len, const uint8_t *in,
1592 size_t in_len) {
1593 if (out_len > in_len) {
1594 out_len = in_len;
1595 }
David Benjamin17cf2cb2016-12-13 01:07:13 -05001596 OPENSSL_memcpy(out, in, out_len);
David Benjamin49ddf412016-10-08 11:56:01 -04001597 return in_len;
1598}
1599
David Benjamin1a1b34d2015-10-17 12:51:52 -04001600size_t SSL_get_finished(const SSL *ssl, void *buf, size_t count) {
David Benjaminced00b42016-10-08 11:24:09 -04001601 if (!ssl->s3->initial_handshake_complete ||
David Benjamind1e3ce12017-10-06 18:31:15 -04001602 ssl_protocol_version(ssl) < TLS1_VERSION ||
1603 ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjaminced00b42016-10-08 11:24:09 -04001604 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001605 }
1606
David Benjamin49ddf412016-10-08 11:56:01 -04001607 if (ssl->server) {
1608 return copy_finished(buf, count, ssl->s3->previous_server_finished,
1609 ssl->s3->previous_server_finished_len);
David Benjaminced00b42016-10-08 11:24:09 -04001610 }
David Benjamin49ddf412016-10-08 11:56:01 -04001611
1612 return copy_finished(buf, count, ssl->s3->previous_client_finished,
1613 ssl->s3->previous_client_finished_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001614}
Adam Langley95c29f32014-06-20 12:00:00 -07001615
David Benjamin1a1b34d2015-10-17 12:51:52 -04001616size_t SSL_get_peer_finished(const SSL *ssl, void *buf, size_t count) {
David Benjaminced00b42016-10-08 11:24:09 -04001617 if (!ssl->s3->initial_handshake_complete ||
David Benjamind1e3ce12017-10-06 18:31:15 -04001618 ssl_protocol_version(ssl) < TLS1_VERSION ||
1619 ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjaminced00b42016-10-08 11:24:09 -04001620 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001621 }
Adam Langley95c29f32014-06-20 12:00:00 -07001622
David Benjamin49ddf412016-10-08 11:56:01 -04001623 if (ssl->server) {
1624 return copy_finished(buf, count, ssl->s3->previous_client_finished,
1625 ssl->s3->previous_client_finished_len);
David Benjaminced00b42016-10-08 11:24:09 -04001626 }
David Benjamin49ddf412016-10-08 11:56:01 -04001627
1628 return copy_finished(buf, count, ssl->s3->previous_server_finished,
1629 ssl->s3->previous_server_finished_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001630}
Adam Langley95c29f32014-06-20 12:00:00 -07001631
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001632int SSL_get_verify_mode(const SSL *ssl) {
1633 if (!ssl->config) {
1634 assert(ssl->config);
1635 return -1;
1636 }
1637 return ssl->config->verify_mode;
1638}
Adam Langley95c29f32014-06-20 12:00:00 -07001639
David Benjamin42fea372015-09-19 01:22:44 -04001640int SSL_get_extms_support(const SSL *ssl) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001641 // TLS 1.3 does not require extended master secret and always reports as
1642 // supporting it.
David Benjamina68c1182016-07-24 00:14:33 -04001643 if (!ssl->s3->have_version) {
1644 return 0;
1645 }
David Benjamind1e3ce12017-10-06 18:31:15 -04001646 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjaminfc02b592017-02-17 16:26:01 -05001647 return 1;
1648 }
1649
David Benjaminc11ea9422017-08-29 16:33:21 -04001650 // If the initial handshake completed, query the established session.
David Benjaminfc02b592017-02-17 16:26:01 -05001651 if (ssl->s3->established_session != NULL) {
1652 return ssl->s3->established_session->extended_master_secret;
1653 }
1654
David Benjaminc11ea9422017-08-29 16:33:21 -04001655 // Otherwise, query the in-progress handshake.
David Benjaminfc02b592017-02-17 16:26:01 -05001656 if (ssl->s3->hs != NULL) {
1657 return ssl->s3->hs->extended_master_secret;
1658 }
1659 assert(0);
1660 return 0;
Matt Braithwaitecd6f54b2015-09-17 12:54:42 -07001661}
1662
David Benjamin9a41d1b2015-05-16 01:30:09 -04001663int SSL_CTX_get_read_ahead(const SSL_CTX *ctx) { return 0; }
Adam Langley95c29f32014-06-20 12:00:00 -07001664
David Benjamin0d56f882015-12-19 17:05:56 -05001665int SSL_get_read_ahead(const SSL *ssl) { return 0; }
Adam Langley95c29f32014-06-20 12:00:00 -07001666
David Benjamin9a41d1b2015-05-16 01:30:09 -04001667void SSL_CTX_set_read_ahead(SSL_CTX *ctx, int yes) { }
David Benjamin61ecccf2015-05-05 09:44:51 -04001668
David Benjamin0d56f882015-12-19 17:05:56 -05001669void SSL_set_read_ahead(SSL *ssl, int yes) { }
David Benjamin61ecccf2015-05-05 09:44:51 -04001670
David Benjamin9f859492015-10-03 10:44:30 -04001671int SSL_pending(const SSL *ssl) {
David Benjamind9229f92017-10-06 17:36:20 -04001672 return static_cast<int>(ssl->s3->pending_app_data.size());
Adam Langleyfcf25832014-12-18 17:42:32 -08001673}
Adam Langley95c29f32014-06-20 12:00:00 -07001674
David Benjaminc11ea9422017-08-29 16:33:21 -04001675// Fix this so it checks all the valid key/cert options
Adam Langleyfcf25832014-12-18 17:42:32 -08001676int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
David Benjamine325c3f2018-04-12 16:11:15 -04001677 return ssl_cert_check_private_key(ctx->cert, ctx->cert->privatekey.get());
Adam Langleyc5ac2b62016-11-07 12:02:35 -08001678}
1679
David Benjaminc11ea9422017-08-29 16:33:21 -04001680// Fix this function so that it takes an optional type parameter
Adam Langleyc5ac2b62016-11-07 12:02:35 -08001681int SSL_check_private_key(const SSL *ssl) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001682 if (!ssl->config) {
1683 return 0;
1684 }
1685 return ssl_cert_check_private_key(ssl->config->cert,
1686 ssl->config->cert->privatekey.get());
Adam Langleyfcf25832014-12-18 17:42:32 -08001687}
Adam Langley95c29f32014-06-20 12:00:00 -07001688
David Benjamin42fea372015-09-19 01:22:44 -04001689long SSL_get_default_timeout(const SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001690 return SSL_DEFAULT_SESSION_TIMEOUT;
1691}
Adam Langley95c29f32014-06-20 12:00:00 -07001692
David Benjamin44d3eed2015-05-21 01:29:55 -04001693int SSL_renegotiate(SSL *ssl) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001694 // Caller-initiated renegotiation is not supported.
David Benjamin3570d732015-06-29 00:28:17 -04001695 OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
David Benjamin44d3eed2015-05-21 01:29:55 -04001696 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001697}
Adam Langley95c29f32014-06-20 12:00:00 -07001698
David Benjamin44d3eed2015-05-21 01:29:55 -04001699int SSL_renegotiate_pending(SSL *ssl) {
1700 return SSL_in_init(ssl) && ssl->s3->initial_handshake_complete;
Adam Langleyfcf25832014-12-18 17:42:32 -08001701}
Adam Langley95c29f32014-06-20 12:00:00 -07001702
David Benjamin936aada2016-06-07 19:09:22 -04001703int SSL_total_renegotiations(const SSL *ssl) {
1704 return ssl->s3->total_renegotiations;
1705}
1706
David Benjamin1d0a1942015-04-26 15:35:35 -04001707size_t SSL_CTX_get_max_cert_list(const SSL_CTX *ctx) {
1708 return ctx->max_cert_list;
1709}
1710
1711void SSL_CTX_set_max_cert_list(SSL_CTX *ctx, size_t max_cert_list) {
1712 if (max_cert_list > kMaxHandshakeSize) {
1713 max_cert_list = kMaxHandshakeSize;
1714 }
1715 ctx->max_cert_list = (uint32_t)max_cert_list;
1716}
1717
1718size_t SSL_get_max_cert_list(const SSL *ssl) {
1719 return ssl->max_cert_list;
1720}
1721
1722void SSL_set_max_cert_list(SSL *ssl, size_t max_cert_list) {
1723 if (max_cert_list > kMaxHandshakeSize) {
1724 max_cert_list = kMaxHandshakeSize;
1725 }
1726 ssl->max_cert_list = (uint32_t)max_cert_list;
1727}
1728
Adam Langleyce9d85e2016-01-24 15:58:39 -08001729int SSL_CTX_set_max_send_fragment(SSL_CTX *ctx, size_t max_send_fragment) {
David Benjamin1d0a1942015-04-26 15:35:35 -04001730 if (max_send_fragment < 512) {
1731 max_send_fragment = 512;
1732 }
1733 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1734 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1735 }
1736 ctx->max_send_fragment = (uint16_t)max_send_fragment;
Adam Langleyce9d85e2016-01-24 15:58:39 -08001737
1738 return 1;
David Benjamin1d0a1942015-04-26 15:35:35 -04001739}
1740
Adam Langleyce9d85e2016-01-24 15:58:39 -08001741int SSL_set_max_send_fragment(SSL *ssl, size_t max_send_fragment) {
David Benjamin1d0a1942015-04-26 15:35:35 -04001742 if (max_send_fragment < 512) {
1743 max_send_fragment = 512;
1744 }
1745 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1746 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1747 }
1748 ssl->max_send_fragment = (uint16_t)max_send_fragment;
Adam Langleyce9d85e2016-01-24 15:58:39 -08001749
1750 return 1;
David Benjamin1d0a1942015-04-26 15:35:35 -04001751}
1752
David Benjamincb9cf792015-05-05 09:46:14 -04001753int SSL_set_mtu(SSL *ssl, unsigned mtu) {
David Benjamince079fd2016-08-02 16:22:34 -04001754 if (!SSL_is_dtls(ssl) || mtu < dtls1_min_mtu()) {
David Benjamincb9cf792015-05-05 09:46:14 -04001755 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001756 }
David Benjamincb9cf792015-05-05 09:46:14 -04001757 ssl->d1->mtu = mtu;
1758 return 1;
1759}
1760
1761int SSL_get_secure_renegotiation_support(const SSL *ssl) {
David Benjamind2610042017-01-03 10:49:28 -05001762 if (!ssl->s3->have_version) {
1763 return 0;
1764 }
David Benjamind1e3ce12017-10-06 18:31:15 -04001765 return ssl_protocol_version(ssl) >= TLS1_3_VERSION ||
David Benjamind2610042017-01-03 10:49:28 -05001766 ssl->s3->send_connection_binding;
David Benjamincb9cf792015-05-05 09:46:14 -04001767}
1768
David Benjamin71f7d3d2015-05-05 09:46:38 -04001769size_t SSL_CTX_sess_number(const SSL_CTX *ctx) {
David Benjamin9eaa3bd2017-09-27 17:03:54 -04001770 MutexReadLock lock(const_cast<CRYPTO_MUTEX *>(&ctx->lock));
David Benjamin71f7d3d2015-05-05 09:46:38 -04001771 return lh_SSL_SESSION_num_items(ctx->sessions);
1772}
1773
1774unsigned long SSL_CTX_sess_set_cache_size(SSL_CTX *ctx, unsigned long size) {
1775 unsigned long ret = ctx->session_cache_size;
1776 ctx->session_cache_size = size;
1777 return ret;
1778}
1779
1780unsigned long SSL_CTX_sess_get_cache_size(const SSL_CTX *ctx) {
1781 return ctx->session_cache_size;
1782}
1783
1784int SSL_CTX_set_session_cache_mode(SSL_CTX *ctx, int mode) {
1785 int ret = ctx->session_cache_mode;
1786 ctx->session_cache_mode = mode;
1787 return ret;
1788}
1789
1790int SSL_CTX_get_session_cache_mode(const SSL_CTX *ctx) {
1791 return ctx->session_cache_mode;
1792}
1793
David Benjamin936aada2016-06-07 19:09:22 -04001794
1795int SSL_CTX_get_tlsext_ticket_keys(SSL_CTX *ctx, void *out, size_t len) {
1796 if (out == NULL) {
1797 return 48;
1798 }
1799 if (len != 48) {
1800 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH);
1801 return 0;
1802 }
Martin Kreichgauer72912d22017-08-04 12:06:43 -07001803
David Benjaminc11ea9422017-08-29 16:33:21 -04001804 // The default ticket keys are initialized lazily. Trigger a key
1805 // rotation to initialize them.
Martin Kreichgauer72912d22017-08-04 12:06:43 -07001806 if (!ssl_ctx_rotate_ticket_encryption_key(ctx)) {
1807 return 0;
1808 }
1809
David Benjaminf5260812017-07-12 17:16:39 -04001810 uint8_t *out_bytes = reinterpret_cast<uint8_t *>(out);
Martin Kreichgauer72912d22017-08-04 12:06:43 -07001811 MutexReadLock lock(&ctx->lock);
1812 OPENSSL_memcpy(out_bytes, ctx->tlsext_ticket_key_current->name, 16);
1813 OPENSSL_memcpy(out_bytes + 16, ctx->tlsext_ticket_key_current->hmac_key, 16);
1814 OPENSSL_memcpy(out_bytes + 32, ctx->tlsext_ticket_key_current->aes_key, 16);
David Benjamin936aada2016-06-07 19:09:22 -04001815 return 1;
1816}
1817
1818int SSL_CTX_set_tlsext_ticket_keys(SSL_CTX *ctx, const void *in, size_t len) {
1819 if (in == NULL) {
1820 return 48;
1821 }
1822 if (len != 48) {
1823 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH);
1824 return 0;
1825 }
Martin Kreichgauer72912d22017-08-04 12:06:43 -07001826 if (!ctx->tlsext_ticket_key_current) {
1827 ctx->tlsext_ticket_key_current =
1828 (tlsext_ticket_key *)OPENSSL_malloc(sizeof(tlsext_ticket_key));
1829 if (!ctx->tlsext_ticket_key_current) {
1830 return 0;
1831 }
1832 }
1833 OPENSSL_memset(ctx->tlsext_ticket_key_current, 0, sizeof(tlsext_ticket_key));
David Benjaminf5260812017-07-12 17:16:39 -04001834 const uint8_t *in_bytes = reinterpret_cast<const uint8_t *>(in);
Martin Kreichgauer72912d22017-08-04 12:06:43 -07001835 OPENSSL_memcpy(ctx->tlsext_ticket_key_current->name, in_bytes, 16);
1836 OPENSSL_memcpy(ctx->tlsext_ticket_key_current->hmac_key, in_bytes + 16, 16);
1837 OPENSSL_memcpy(ctx->tlsext_ticket_key_current->aes_key, in_bytes + 32, 16);
1838 OPENSSL_free(ctx->tlsext_ticket_key_prev);
1839 ctx->tlsext_ticket_key_prev = nullptr;
David Benjaminc11ea9422017-08-29 16:33:21 -04001840 // Disable automatic key rotation.
Martin Kreichgauer72912d22017-08-04 12:06:43 -07001841 ctx->tlsext_ticket_key_current->next_rotation_tv_sec = 0;
David Benjamin936aada2016-06-07 19:09:22 -04001842 return 1;
1843}
1844
1845int SSL_CTX_set_tlsext_ticket_key_cb(
1846 SSL_CTX *ctx, int (*callback)(SSL *ssl, uint8_t *key_name, uint8_t *iv,
1847 EVP_CIPHER_CTX *ctx, HMAC_CTX *hmac_ctx,
1848 int encrypt)) {
1849 ctx->tlsext_ticket_key_cb = callback;
1850 return 1;
1851}
1852
1853int SSL_CTX_set1_curves(SSL_CTX *ctx, const int *curves, size_t curves_len) {
1854 return tls1_set_curves(&ctx->supported_group_list,
1855 &ctx->supported_group_list_len, curves,
1856 curves_len);
1857}
1858
1859int SSL_set1_curves(SSL *ssl, const int *curves, size_t curves_len) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001860 if (!ssl->config) {
1861 return 0;
1862 }
1863 return tls1_set_curves(&ssl->config->supported_group_list,
1864 &ssl->config->supported_group_list_len, curves,
David Benjamin936aada2016-06-07 19:09:22 -04001865 curves_len);
1866}
1867
Alessandro Ghedini5fd18072016-09-28 21:04:25 +01001868int SSL_CTX_set1_curves_list(SSL_CTX *ctx, const char *curves) {
1869 return tls1_set_curves_list(&ctx->supported_group_list,
1870 &ctx->supported_group_list_len, curves);
1871}
1872
1873int SSL_set1_curves_list(SSL *ssl, const char *curves) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001874 if (!ssl->config) {
1875 return 0;
1876 }
1877 return tls1_set_curves_list(&ssl->config->supported_group_list,
1878 &ssl->config->supported_group_list_len, curves);
Alessandro Ghedini5fd18072016-09-28 21:04:25 +01001879}
1880
David Benjamin9e68f192016-06-30 14:55:33 -04001881uint16_t SSL_get_curve_id(const SSL *ssl) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001882 // TODO(davidben): This checks the wrong session if there is a renegotiation
1883 // in progress.
Steven Valdez87eab492016-06-27 16:34:59 -04001884 SSL_SESSION *session = SSL_get_session(ssl);
David Benjamin4882a6c2016-12-11 02:48:12 -05001885 if (session == NULL) {
David Benjamin9e68f192016-06-30 14:55:33 -04001886 return 0;
1887 }
1888
David Benjamin4882a6c2016-12-11 02:48:12 -05001889 return session->group_id;
David Benjamin9e68f192016-06-30 14:55:33 -04001890}
1891
David Benjamin936aada2016-06-07 19:09:22 -04001892int SSL_CTX_set_tmp_dh(SSL_CTX *ctx, const DH *dh) {
David Benjamin936aada2016-06-07 19:09:22 -04001893 return 1;
1894}
1895
1896int SSL_set_tmp_dh(SSL *ssl, const DH *dh) {
David Benjamin936aada2016-06-07 19:09:22 -04001897 return 1;
1898}
1899
David Benjamin70dbf042017-08-08 18:51:37 -04001900STACK_OF(SSL_CIPHER) *SSL_CTX_get_ciphers(const SSL_CTX *ctx) {
David Benjamin48b276d2018-04-12 17:37:32 -04001901 return ctx->cipher_list->ciphers.get();
David Benjamin8ebeabf2017-03-02 22:04:07 -05001902}
1903
David Benjamin70dbf042017-08-08 18:51:37 -04001904int SSL_CTX_cipher_in_group(const SSL_CTX *ctx, size_t i) {
David Benjamin48b276d2018-04-12 17:37:32 -04001905 if (i >= sk_SSL_CIPHER_num(ctx->cipher_list->ciphers.get())) {
David Benjamin70dbf042017-08-08 18:51:37 -04001906 return 0;
1907 }
1908 return ctx->cipher_list->in_group_flags[i];
1909}
1910
David Benjamin32876b32015-09-20 12:17:03 -04001911STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *ssl) {
1912 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001913 return NULL;
1914 }
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001915 if (ssl->config == NULL) {
1916 assert(ssl->config);
1917 return NULL;
1918 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001919
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001920 return ssl->config->cipher_list ? ssl->config->cipher_list->ciphers.get()
1921 : ssl->ctx->cipher_list->ciphers.get();
Adam Langleyfcf25832014-12-18 17:42:32 -08001922}
Adam Langley95c29f32014-06-20 12:00:00 -07001923
David Benjamin32876b32015-09-20 12:17:03 -04001924const char *SSL_get_cipher_list(const SSL *ssl, int n) {
David Benjamin32876b32015-09-20 12:17:03 -04001925 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001926 return NULL;
1927 }
Adam Langley95c29f32014-06-20 12:00:00 -07001928
David Benjamin8ebeabf2017-03-02 22:04:07 -05001929 STACK_OF(SSL_CIPHER) *sk = SSL_get_ciphers(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001930 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
1931 return NULL;
1932 }
Adam Langley95c29f32014-06-20 12:00:00 -07001933
David Benjamin8ebeabf2017-03-02 22:04:07 -05001934 const SSL_CIPHER *c = sk_SSL_CIPHER_value(sk, n);
Adam Langleyfcf25832014-12-18 17:42:32 -08001935 if (c == NULL) {
1936 return NULL;
1937 }
Adam Langley95c29f32014-06-20 12:00:00 -07001938
Adam Langleyfcf25832014-12-18 17:42:32 -08001939 return c->name;
1940}
David Benjamin5491e3f2014-09-29 19:33:09 -04001941
Adam Langleyfcf25832014-12-18 17:42:32 -08001942int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
David Benjamin5be3a742017-10-28 16:08:58 -04001943 return ssl_create_cipher_list(&ctx->cipher_list, str, false /* not strict */);
Matthew Braithwaitea57dcfb2017-02-17 22:08:23 -08001944}
1945
1946int SSL_CTX_set_strict_cipher_list(SSL_CTX *ctx, const char *str) {
David Benjamin5be3a742017-10-28 16:08:58 -04001947 return ssl_create_cipher_list(&ctx->cipher_list, str, true /* strict */);
Adam Langleyfcf25832014-12-18 17:42:32 -08001948}
David Benjamin39482a12014-07-20 13:30:15 -04001949
David Benjamin32876b32015-09-20 12:17:03 -04001950int SSL_set_cipher_list(SSL *ssl, const char *str) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001951 if (!ssl->config) {
1952 return 0;
1953 }
1954 return ssl_create_cipher_list(&ssl->config->cipher_list, str,
1955 false /* not strict */);
Matthew Braithwaitea57dcfb2017-02-17 22:08:23 -08001956}
1957
1958int SSL_set_strict_cipher_list(SSL *ssl, const char *str) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001959 if (!ssl->config) {
1960 return 0;
1961 }
1962 return ssl_create_cipher_list(&ssl->config->cipher_list, str,
1963 true /* strict */);
Adam Langleyfcf25832014-12-18 17:42:32 -08001964}
Adam Langley95c29f32014-06-20 12:00:00 -07001965
David Benjamin07e13842015-10-17 13:48:04 -04001966const char *SSL_get_servername(const SSL *ssl, const int type) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001967 if (type != TLSEXT_NAMETYPE_host_name) {
1968 return NULL;
1969 }
Adam Langley95c29f32014-06-20 12:00:00 -07001970
David Benjaminc11ea9422017-08-29 16:33:21 -04001971 // Historically, |SSL_get_servername| was also the configuration getter
1972 // corresponding to |SSL_set_tlsext_host_name|.
David Benjamin07e13842015-10-17 13:48:04 -04001973 if (ssl->tlsext_hostname != NULL) {
1974 return ssl->tlsext_hostname;
1975 }
1976
David Benjamin8e7bbba2017-10-13 17:18:35 -04001977 return ssl->s3->hostname.get();
Adam Langleyfcf25832014-12-18 17:42:32 -08001978}
Adam Langley95c29f32014-06-20 12:00:00 -07001979
David Benjamin07e13842015-10-17 13:48:04 -04001980int SSL_get_servername_type(const SSL *ssl) {
David Benjamina8614602017-09-06 15:40:19 -04001981 if (SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) == NULL) {
Steven Valdez87eab492016-06-27 16:34:59 -04001982 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001983 }
Steven Valdez87eab492016-06-27 16:34:59 -04001984 return TLSEXT_NAMETYPE_host_name;
Adam Langleyfcf25832014-12-18 17:42:32 -08001985}
Adam Langley95c29f32014-06-20 12:00:00 -07001986
David Benjamin3a1dd462017-07-11 16:13:10 -04001987void SSL_CTX_set_custom_verify(
1988 SSL_CTX *ctx, int mode,
1989 enum ssl_verify_result_t (*callback)(SSL *ssl, uint8_t *out_alert)) {
1990 ctx->verify_mode = mode;
1991 ctx->custom_verify_callback = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08001992}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001993
David Benjamin3a1dd462017-07-11 16:13:10 -04001994void SSL_set_custom_verify(
1995 SSL *ssl, int mode,
1996 enum ssl_verify_result_t (*callback)(SSL *ssl, uint8_t *out_alert)) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001997 if (!ssl->config) {
1998 return;
1999 }
2000 ssl->config->verify_mode = mode;
2001 ssl->config->custom_verify_callback = callback;
David Benjamin3a1dd462017-07-11 16:13:10 -04002002}
2003
2004void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
David Benjamin7e58c5e2017-10-11 13:01:08 -04002005 ctx->signed_cert_timestamps_enabled = true;
Adam Langleyfe366722017-03-07 16:41:04 -08002006}
2007
David Benjamin26e1ff32017-02-14 20:13:00 -05002008void SSL_enable_signed_cert_timestamps(SSL *ssl) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002009 if (!ssl->config) {
2010 return;
2011 }
2012 ssl->config->signed_cert_timestamps_enabled = true;
Adam Langleyfcf25832014-12-18 17:42:32 -08002013}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02002014
Adam Langleyfcf25832014-12-18 17:42:32 -08002015void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
David Benjamin7e58c5e2017-10-11 13:01:08 -04002016 ctx->ocsp_stapling_enabled = true;
Adam Langleyfcf25832014-12-18 17:42:32 -08002017}
David Benjamin6c7aed02014-08-27 16:42:38 -04002018
David Benjamin26e1ff32017-02-14 20:13:00 -05002019void SSL_enable_ocsp_stapling(SSL *ssl) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002020 if (!ssl->config) {
2021 return;
2022 }
2023 ssl->config->ocsp_stapling_enabled = true;
Adam Langleyfcf25832014-12-18 17:42:32 -08002024}
David Benjamin6c7aed02014-08-27 16:42:38 -04002025
Adam Langleyfcf25832014-12-18 17:42:32 -08002026void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
2027 size_t *out_len) {
Steven Valdez87eab492016-06-27 16:34:59 -04002028 SSL_SESSION *session = SSL_get_session(ssl);
David Benjamin8fc2dc02017-08-22 15:07:51 -07002029 if (ssl->server || !session || !session->signed_cert_timestamp_list) {
2030 *out_len = 0;
2031 *out = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002032 return;
2033 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02002034
David Benjamin8fc2dc02017-08-22 15:07:51 -07002035 *out = CRYPTO_BUFFER_data(session->signed_cert_timestamp_list);
2036 *out_len = CRYPTO_BUFFER_len(session->signed_cert_timestamp_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08002037}
David Benjamin6c7aed02014-08-27 16:42:38 -04002038
Adam Langleyfcf25832014-12-18 17:42:32 -08002039void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
2040 size_t *out_len) {
Steven Valdez87eab492016-06-27 16:34:59 -04002041 SSL_SESSION *session = SSL_get_session(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08002042 if (ssl->server || !session || !session->ocsp_response) {
David Benjamin8fc2dc02017-08-22 15:07:51 -07002043 *out_len = 0;
2044 *out = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002045 return;
2046 }
David Benjamin8fc2dc02017-08-22 15:07:51 -07002047
2048 *out = CRYPTO_BUFFER_data(session->ocsp_response);
2049 *out_len = CRYPTO_BUFFER_len(session->ocsp_response);
Adam Langleyfcf25832014-12-18 17:42:32 -08002050}
David Benjamin6c7aed02014-08-27 16:42:38 -04002051
David Benjamin936aada2016-06-07 19:09:22 -04002052int SSL_set_tlsext_host_name(SSL *ssl, const char *name) {
2053 OPENSSL_free(ssl->tlsext_hostname);
2054 ssl->tlsext_hostname = NULL;
2055
2056 if (name == NULL) {
2057 return 1;
2058 }
2059
2060 size_t len = strlen(name);
2061 if (len == 0 || len > TLSEXT_MAXLEN_host_name) {
2062 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL3_EXT_INVALID_SERVERNAME);
2063 return 0;
2064 }
2065 ssl->tlsext_hostname = BUF_strdup(name);
2066 if (ssl->tlsext_hostname == NULL) {
2067 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
2068 return 0;
2069 }
2070 return 1;
2071}
2072
2073int SSL_CTX_set_tlsext_servername_callback(
2074 SSL_CTX *ctx, int (*callback)(SSL *ssl, int *out_alert, void *arg)) {
2075 ctx->tlsext_servername_callback = callback;
2076 return 1;
2077}
2078
2079int SSL_CTX_set_tlsext_servername_arg(SSL_CTX *ctx, void *arg) {
2080 ctx->tlsext_servername_arg = arg;
2081 return 1;
2082}
2083
David Benjaminca743582017-06-15 17:51:35 -04002084int SSL_select_next_proto(uint8_t **out, uint8_t *out_len, const uint8_t *peer,
2085 unsigned peer_len, const uint8_t *supported,
2086 unsigned supported_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002087 const uint8_t *result;
David Benjaminca743582017-06-15 17:51:35 -04002088 int status;
Adam Langley95c29f32014-06-20 12:00:00 -07002089
David Benjaminc11ea9422017-08-29 16:33:21 -04002090 // For each protocol in peer preference order, see if we support it.
David Benjaminca743582017-06-15 17:51:35 -04002091 for (unsigned i = 0; i < peer_len;) {
2092 for (unsigned j = 0; j < supported_len;) {
2093 if (peer[i] == supported[j] &&
2094 OPENSSL_memcmp(&peer[i + 1], &supported[j + 1], peer[i]) == 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002095 // We found a match
David Benjaminca743582017-06-15 17:51:35 -04002096 result = &peer[i];
Adam Langleyfcf25832014-12-18 17:42:32 -08002097 status = OPENSSL_NPN_NEGOTIATED;
2098 goto found;
2099 }
David Benjaminca743582017-06-15 17:51:35 -04002100 j += supported[j];
Adam Langleyfcf25832014-12-18 17:42:32 -08002101 j++;
2102 }
David Benjaminca743582017-06-15 17:51:35 -04002103 i += peer[i];
Adam Langleyfcf25832014-12-18 17:42:32 -08002104 i++;
2105 }
Adam Langley95c29f32014-06-20 12:00:00 -07002106
David Benjaminc11ea9422017-08-29 16:33:21 -04002107 // There's no overlap between our protocols and the peer's list.
David Benjaminca743582017-06-15 17:51:35 -04002108 result = supported;
Adam Langleyfcf25832014-12-18 17:42:32 -08002109 status = OPENSSL_NPN_NO_OVERLAP;
Adam Langley95c29f32014-06-20 12:00:00 -07002110
Adam Langleyfcf25832014-12-18 17:42:32 -08002111found:
2112 *out = (uint8_t *)result + 1;
David Benjamin977547b2015-09-16 00:25:52 -04002113 *out_len = result[0];
Adam Langleyfcf25832014-12-18 17:42:32 -08002114 return status;
2115}
Adam Langley95c29f32014-06-20 12:00:00 -07002116
David Benjamin977547b2015-09-16 00:25:52 -04002117void SSL_get0_next_proto_negotiated(const SSL *ssl, const uint8_t **out_data,
2118 unsigned *out_len) {
David Benjamin8e7bbba2017-10-13 17:18:35 -04002119 *out_data = ssl->s3->next_proto_negotiated.data();
2120 *out_len = ssl->s3->next_proto_negotiated.size();
Adam Langley95c29f32014-06-20 12:00:00 -07002121}
2122
Adam Langleyfcf25832014-12-18 17:42:32 -08002123void SSL_CTX_set_next_protos_advertised_cb(
2124 SSL_CTX *ctx,
David Benjamin977547b2015-09-16 00:25:52 -04002125 int (*cb)(SSL *ssl, const uint8_t **out, unsigned *out_len, void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08002126 void *arg) {
2127 ctx->next_protos_advertised_cb = cb;
2128 ctx->next_protos_advertised_cb_arg = arg;
2129}
Adam Langley95c29f32014-06-20 12:00:00 -07002130
Adam Langleyfcf25832014-12-18 17:42:32 -08002131void SSL_CTX_set_next_proto_select_cb(
David Benjamin977547b2015-09-16 00:25:52 -04002132 SSL_CTX *ctx, int (*cb)(SSL *ssl, uint8_t **out, uint8_t *out_len,
2133 const uint8_t *in, unsigned in_len, void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08002134 void *arg) {
2135 ctx->next_proto_select_cb = cb;
2136 ctx->next_proto_select_cb_arg = arg;
2137}
Adam Langley95c29f32014-06-20 12:00:00 -07002138
Adam Langleyfcf25832014-12-18 17:42:32 -08002139int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
2140 unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04002141 OPENSSL_free(ctx->alpn_client_proto_list);
David Benjaminf5260812017-07-12 17:16:39 -04002142 ctx->alpn_client_proto_list = (uint8_t *)BUF_memdup(protos, protos_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002143 if (!ctx->alpn_client_proto_list) {
2144 return 1;
2145 }
2146 ctx->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07002147
Adam Langleyfcf25832014-12-18 17:42:32 -08002148 return 0;
2149}
Adam Langley95c29f32014-06-20 12:00:00 -07002150
Adam Langleyfcf25832014-12-18 17:42:32 -08002151int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002152 if (!ssl->config) {
2153 return 0;
2154 }
2155 OPENSSL_free(ssl->config->alpn_client_proto_list);
2156 ssl->config->alpn_client_proto_list =
2157 (uint8_t *)BUF_memdup(protos, protos_len);
2158 if (!ssl->config->alpn_client_proto_list) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002159 return 1;
2160 }
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002161 ssl->config->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07002162
Adam Langleyfcf25832014-12-18 17:42:32 -08002163 return 0;
2164}
Adam Langley95c29f32014-06-20 12:00:00 -07002165
Adam Langleyfcf25832014-12-18 17:42:32 -08002166void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
2167 int (*cb)(SSL *ssl, const uint8_t **out,
David Benjamin8984f1f2015-09-16 00:10:19 -04002168 uint8_t *out_len, const uint8_t *in,
2169 unsigned in_len, void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08002170 void *arg) {
2171 ctx->alpn_select_cb = cb;
2172 ctx->alpn_select_cb_arg = arg;
2173}
Adam Langley95c29f32014-06-20 12:00:00 -07002174
David Benjamin8984f1f2015-09-16 00:10:19 -04002175void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **out_data,
2176 unsigned *out_len) {
Steven Valdeze831a812017-03-09 14:56:07 -05002177 if (SSL_in_early_data(ssl) && !ssl->server) {
2178 *out_data = ssl->s3->hs->early_session->early_alpn;
2179 *out_len = ssl->s3->hs->early_session->early_alpn_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08002180 } else {
David Benjamin8e7bbba2017-10-13 17:18:35 -04002181 *out_data = ssl->s3->alpn_selected.data();
2182 *out_len = ssl->s3->alpn_selected.size();
Adam Langleyfcf25832014-12-18 17:42:32 -08002183 }
2184}
Adam Langley95c29f32014-06-20 12:00:00 -07002185
David Benjaminc8ff30c2017-04-04 13:52:36 -04002186void SSL_CTX_set_allow_unknown_alpn_protos(SSL_CTX *ctx, int enabled) {
2187 ctx->allow_unknown_alpn_protos = !!enabled;
2188}
David Benjamin936aada2016-06-07 19:09:22 -04002189
David Benjamineebd3c82016-12-06 17:43:58 -05002190void SSL_CTX_set_tls_channel_id_enabled(SSL_CTX *ctx, int enabled) {
2191 ctx->tlsext_channel_id_enabled = !!enabled;
2192}
2193
David Benjamin936aada2016-06-07 19:09:22 -04002194int SSL_CTX_enable_tls_channel_id(SSL_CTX *ctx) {
David Benjamineebd3c82016-12-06 17:43:58 -05002195 SSL_CTX_set_tls_channel_id_enabled(ctx, 1);
David Benjamin936aada2016-06-07 19:09:22 -04002196 return 1;
2197}
2198
David Benjamineebd3c82016-12-06 17:43:58 -05002199void SSL_set_tls_channel_id_enabled(SSL *ssl, int enabled) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002200 if (!ssl->config) {
2201 return;
2202 }
2203 ssl->config->tlsext_channel_id_enabled = !!enabled;
David Benjamineebd3c82016-12-06 17:43:58 -05002204}
2205
David Benjamin936aada2016-06-07 19:09:22 -04002206int SSL_enable_tls_channel_id(SSL *ssl) {
David Benjamineebd3c82016-12-06 17:43:58 -05002207 SSL_set_tls_channel_id_enabled(ssl, 1);
David Benjamin936aada2016-06-07 19:09:22 -04002208 return 1;
2209}
2210
2211static int is_p256_key(EVP_PKEY *private_key) {
2212 const EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(private_key);
2213 return ec_key != NULL &&
2214 EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key)) ==
2215 NID_X9_62_prime256v1;
2216}
2217
2218int SSL_CTX_set1_tls_channel_id(SSL_CTX *ctx, EVP_PKEY *private_key) {
2219 if (!is_p256_key(private_key)) {
2220 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256);
2221 return 0;
2222 }
2223
2224 EVP_PKEY_free(ctx->tlsext_channel_id_private);
Adam Langley310d3f62016-07-12 10:39:20 -07002225 EVP_PKEY_up_ref(private_key);
2226 ctx->tlsext_channel_id_private = private_key;
David Benjamin7e58c5e2017-10-11 13:01:08 -04002227 ctx->tlsext_channel_id_enabled = true;
David Benjamin936aada2016-06-07 19:09:22 -04002228
2229 return 1;
2230}
2231
2232int SSL_set1_tls_channel_id(SSL *ssl, EVP_PKEY *private_key) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002233 if (!ssl->config) {
2234 return 0;
2235 }
David Benjamin936aada2016-06-07 19:09:22 -04002236 if (!is_p256_key(private_key)) {
2237 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256);
2238 return 0;
2239 }
2240
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002241 EVP_PKEY_free(ssl->config->tlsext_channel_id_private);
Adam Langley310d3f62016-07-12 10:39:20 -07002242 EVP_PKEY_up_ref(private_key);
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002243 ssl->config->tlsext_channel_id_private = private_key;
2244 ssl->config->tlsext_channel_id_enabled = true;
David Benjamin936aada2016-06-07 19:09:22 -04002245
2246 return 1;
2247}
2248
2249size_t SSL_get_tls_channel_id(SSL *ssl, uint8_t *out, size_t max_out) {
2250 if (!ssl->s3->tlsext_channel_id_valid) {
2251 return 0;
2252 }
David Benjamin17cf2cb2016-12-13 01:07:13 -05002253 OPENSSL_memcpy(out, ssl->s3->tlsext_channel_id,
2254 (max_out < 64) ? max_out : 64);
David Benjamin936aada2016-06-07 19:09:22 -04002255 return 64;
2256}
2257
Nick Harper36fcc4c2017-09-21 15:02:22 -07002258int SSL_set_token_binding_params(SSL *ssl, const uint8_t *params, size_t len) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002259 if (!ssl->config) {
2260 return 0;
2261 }
Nick Harper36fcc4c2017-09-21 15:02:22 -07002262 if (len > 256) {
2263 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
2264 return 0;
2265 }
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002266 OPENSSL_free(ssl->config->token_binding_params);
2267 ssl->config->token_binding_params = (uint8_t *)BUF_memdup(params, len);
2268 if (!ssl->config->token_binding_params) {
Nick Harper36fcc4c2017-09-21 15:02:22 -07002269 return 0;
2270 }
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002271 ssl->config->token_binding_params_len = len;
Nick Harper36fcc4c2017-09-21 15:02:22 -07002272 return 1;
2273}
2274
2275int SSL_is_token_binding_negotiated(const SSL *ssl) {
David Benjamin9f0e7cb2018-04-12 15:36:30 -04002276 return ssl->s3->token_binding_negotiated;
Nick Harper36fcc4c2017-09-21 15:02:22 -07002277}
2278
2279uint8_t SSL_get_negotiated_token_binding_param(const SSL *ssl) {
David Benjamin9f0e7cb2018-04-12 15:36:30 -04002280 return ssl->s3->negotiated_token_binding_param;
Nick Harper36fcc4c2017-09-21 15:02:22 -07002281}
2282
David Benjamin936aada2016-06-07 19:09:22 -04002283size_t SSL_get0_certificate_types(SSL *ssl, const uint8_t **out_types) {
David Benjamina0486782016-10-06 19:11:32 -04002284 if (ssl->server || ssl->s3->hs == NULL) {
David Benjamin936aada2016-06-07 19:09:22 -04002285 *out_types = NULL;
2286 return 0;
2287 }
David Benjamin08f5c762017-09-21 02:43:05 -04002288 *out_types = ssl->s3->hs->certificate_types.data();
2289 return ssl->s3->hs->certificate_types.size();
David Benjamin936aada2016-06-07 19:09:22 -04002290}
2291
David Benjamin0d56f882015-12-19 17:05:56 -05002292EVP_PKEY *SSL_get_privatekey(const SSL *ssl) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002293 if (!ssl->config) {
2294 assert(ssl->config);
2295 return NULL;
2296 }
2297 if (ssl->config->cert != NULL) {
2298 return ssl->config->cert->privatekey.get();
Adam Langleyfcf25832014-12-18 17:42:32 -08002299 }
2300
2301 return NULL;
2302}
2303
Adam Langleyfcf25832014-12-18 17:42:32 -08002304EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
2305 if (ctx->cert != NULL) {
David Benjamine325c3f2018-04-12 16:11:15 -04002306 return ctx->cert->privatekey.get();
Adam Langleyfcf25832014-12-18 17:42:32 -08002307 }
2308
2309 return NULL;
2310}
2311
David Benjamin42fea372015-09-19 01:22:44 -04002312const SSL_CIPHER *SSL_get_current_cipher(const SSL *ssl) {
David Benjamincfc11c22017-07-18 22:45:18 -04002313 return ssl->s3->aead_write_ctx->cipher();
Adam Langleyfcf25832014-12-18 17:42:32 -08002314}
2315
David Benjamin936aada2016-06-07 19:09:22 -04002316int SSL_session_reused(const SSL *ssl) {
Steven Valdeze831a812017-03-09 14:56:07 -05002317 return ssl->s3->session_reused || SSL_in_early_data(ssl);
David Benjamin936aada2016-06-07 19:09:22 -04002318}
2319
David Benjamin0d56f882015-12-19 17:05:56 -05002320const COMP_METHOD *SSL_get_current_compression(SSL *ssl) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002321
David Benjamin0d56f882015-12-19 17:05:56 -05002322const COMP_METHOD *SSL_get_current_expansion(SSL *ssl) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002323
Adam Langley29ec5d12016-03-01 16:12:28 -08002324int *SSL_get_server_tmp_key(SSL *ssl, EVP_PKEY **out_key) { return 0; }
2325
Adam Langleyfcf25832014-12-18 17:42:32 -08002326void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
Adam Langleybb85f3d2015-10-28 18:44:11 -07002327 ctx->quiet_shutdown = (mode != 0);
Adam Langleyfcf25832014-12-18 17:42:32 -08002328}
2329
2330int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
2331 return ctx->quiet_shutdown;
2332}
2333
Adam Langleybb85f3d2015-10-28 18:44:11 -07002334void SSL_set_quiet_shutdown(SSL *ssl, int mode) {
2335 ssl->quiet_shutdown = (mode != 0);
2336}
Adam Langleyfcf25832014-12-18 17:42:32 -08002337
David Benjamin9f859492015-10-03 10:44:30 -04002338int SSL_get_quiet_shutdown(const SSL *ssl) { return ssl->quiet_shutdown; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002339
David Benjamin63006a92015-10-18 00:00:06 -04002340void SSL_set_shutdown(SSL *ssl, int mode) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002341 // It is an error to clear any bits that have already been set. (We can't try
2342 // to get a second close_notify or send two.)
David Benjaminfa214e42016-05-10 17:03:10 -04002343 assert((SSL_get_shutdown(ssl) & mode) == SSL_get_shutdown(ssl));
David Benjamin63006a92015-10-18 00:00:06 -04002344
David Benjaminfa214e42016-05-10 17:03:10 -04002345 if (mode & SSL_RECEIVED_SHUTDOWN &&
David Benjamin23c25d52017-10-06 16:02:47 -04002346 ssl->s3->read_shutdown == ssl_shutdown_none) {
2347 ssl->s3->read_shutdown = ssl_shutdown_close_notify;
David Benjaminfa214e42016-05-10 17:03:10 -04002348 }
2349
2350 if (mode & SSL_SENT_SHUTDOWN &&
David Benjamin23c25d52017-10-06 16:02:47 -04002351 ssl->s3->write_shutdown == ssl_shutdown_none) {
2352 ssl->s3->write_shutdown = ssl_shutdown_close_notify;
David Benjaminfa214e42016-05-10 17:03:10 -04002353 }
David Benjamin63006a92015-10-18 00:00:06 -04002354}
Adam Langleyfcf25832014-12-18 17:42:32 -08002355
David Benjaminfa214e42016-05-10 17:03:10 -04002356int SSL_get_shutdown(const SSL *ssl) {
2357 int ret = 0;
David Benjamin23c25d52017-10-06 16:02:47 -04002358 if (ssl->s3->read_shutdown != ssl_shutdown_none) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002359 // Historically, OpenSSL set |SSL_RECEIVED_SHUTDOWN| on both close_notify
2360 // and fatal alert.
David Benjaminfa214e42016-05-10 17:03:10 -04002361 ret |= SSL_RECEIVED_SHUTDOWN;
2362 }
David Benjamin23c25d52017-10-06 16:02:47 -04002363 if (ssl->s3->write_shutdown == ssl_shutdown_close_notify) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002364 // Historically, OpenSSL set |SSL_SENT_SHUTDOWN| on only close_notify.
David Benjaminfa214e42016-05-10 17:03:10 -04002365 ret |= SSL_SENT_SHUTDOWN;
2366 }
2367 return ret;
2368}
Adam Langleyfcf25832014-12-18 17:42:32 -08002369
Adam Langleyfcf25832014-12-18 17:42:32 -08002370SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
2371
2372SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002373 if (!ssl->config) {
2374 return NULL;
2375 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002376 if (ssl->ctx == ctx) {
2377 return ssl->ctx;
2378 }
2379
David Benjaminc11ea9422017-08-29 16:33:21 -04002380 // One cannot change the X.509 callbacks during a connection.
Adam Langley3509dac2017-02-01 11:59:18 -08002381 if (ssl->ctx->x509_method != ctx->x509_method) {
2382 assert(0);
2383 return NULL;
2384 }
2385
Adam Langleyfcf25832014-12-18 17:42:32 -08002386 if (ctx == NULL) {
David Benjamin98472cb2018-05-02 16:05:36 -04002387 ctx = ssl->session_ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08002388 }
2389
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002390 Delete(ssl->config->cert);
2391 ssl->config->cert = ssl_cert_dup(ctx->cert).release();
David Benjamin2755a3e2015-04-22 16:17:58 -04002392
David Benjaminf650c712016-12-22 15:54:51 -05002393 SSL_CTX_up_ref(ctx);
2394 SSL_CTX_free(ssl->ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002395 ssl->ctx = ctx;
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002396 ssl->enable_early_data = ssl->ctx->enable_early_data;
Adam Langleyfcf25832014-12-18 17:42:32 -08002397
Adam Langleyfcf25832014-12-18 17:42:32 -08002398 return ssl->ctx;
2399}
2400
Adam Langley95c29f32014-06-20 12:00:00 -07002401void SSL_set_info_callback(SSL *ssl,
David Benjamin82170242015-10-17 22:51:17 -04002402 void (*cb)(const SSL *ssl, int type, int value)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002403 ssl->info_callback = cb;
2404}
Adam Langley95c29f32014-06-20 12:00:00 -07002405
David Benjamin82170242015-10-17 22:51:17 -04002406void (*SSL_get_info_callback(const SSL *ssl))(const SSL *ssl, int type,
2407 int value) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002408 return ssl->info_callback;
2409}
Adam Langley95c29f32014-06-20 12:00:00 -07002410
David Benjamincb0c29f2016-12-12 17:00:50 -05002411int SSL_state(const SSL *ssl) {
David Benjamin9e766d72017-02-10 23:14:17 -05002412 return SSL_in_init(ssl) ? SSL_ST_INIT : SSL_ST_OK;
David Benjamincb0c29f2016-12-12 17:00:50 -05002413}
Adam Langley95c29f32014-06-20 12:00:00 -07002414
David Benjaminece089c2015-05-15 23:52:42 -04002415void SSL_set_state(SSL *ssl, int state) { }
Adam Langley95c29f32014-06-20 12:00:00 -07002416
Emily Stark95a79ee2016-02-25 21:12:28 -08002417char *SSL_get_shared_ciphers(const SSL *ssl, char *buf, int len) {
2418 if (len <= 0) {
2419 return NULL;
2420 }
2421 buf[0] = '\0';
2422 return buf;
2423}
2424
David Benjamin8a589332015-12-04 23:14:35 -05002425int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
David Benjamind94682d2017-05-14 17:10:18 -04002426 CRYPTO_EX_dup *dup_unused, CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002427 int index;
2428 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp,
David Benjamind94682d2017-05-14 17:10:18 -04002429 free_func)) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002430 return -1;
2431 }
2432 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002433}
Adam Langley95c29f32014-06-20 12:00:00 -07002434
David Benjamin27e377e2017-07-31 19:09:42 -04002435int SSL_set_ex_data(SSL *ssl, int idx, void *data) {
2436 return CRYPTO_set_ex_data(&ssl->ex_data, idx, data);
Adam Langleyfcf25832014-12-18 17:42:32 -08002437}
Adam Langley95c29f32014-06-20 12:00:00 -07002438
David Benjamin1ac08fe2015-09-15 01:36:22 -04002439void *SSL_get_ex_data(const SSL *ssl, int idx) {
2440 return CRYPTO_get_ex_data(&ssl->ex_data, idx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002441}
Adam Langley95c29f32014-06-20 12:00:00 -07002442
David Benjamin8a589332015-12-04 23:14:35 -05002443int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
David Benjamind94682d2017-05-14 17:10:18 -04002444 CRYPTO_EX_dup *dup_unused,
Adam Langleyfcf25832014-12-18 17:42:32 -08002445 CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002446 int index;
2447 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp,
David Benjamind94682d2017-05-14 17:10:18 -04002448 free_func)) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002449 return -1;
2450 }
2451 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002452}
Adam Langley95c29f32014-06-20 12:00:00 -07002453
David Benjamin27e377e2017-07-31 19:09:42 -04002454int SSL_CTX_set_ex_data(SSL_CTX *ctx, int idx, void *data) {
2455 return CRYPTO_set_ex_data(&ctx->ex_data, idx, data);
Adam Langleyfcf25832014-12-18 17:42:32 -08002456}
Adam Langley95c29f32014-06-20 12:00:00 -07002457
David Benjamin1ac08fe2015-09-15 01:36:22 -04002458void *SSL_CTX_get_ex_data(const SSL_CTX *ctx, int idx) {
2459 return CRYPTO_get_ex_data(&ctx->ex_data, idx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002460}
Adam Langley95c29f32014-06-20 12:00:00 -07002461
David Benjamin32ce0ac2017-10-13 19:17:22 -04002462int SSL_want(const SSL *ssl) { return ssl->s3->rwstate; }
Adam Langley95c29f32014-06-20 12:00:00 -07002463
Adam Langleyfcf25832014-12-18 17:42:32 -08002464void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
2465 RSA *(*cb)(SSL *ssl, int is_export,
David Benjamin27e377e2017-07-31 19:09:42 -04002466 int keylength)) {}
Adam Langley95c29f32014-06-20 12:00:00 -07002467
Adam Langleyfcf25832014-12-18 17:42:32 -08002468void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
David Benjamin27e377e2017-07-31 19:09:42 -04002469 int keylength)) {}
Adam Langleyfcf25832014-12-18 17:42:32 -08002470
2471void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
David Benjamin27e377e2017-07-31 19:09:42 -04002472 DH *(*cb)(SSL *ssl, int is_export,
2473 int keylength)) {}
Adam Langleyfcf25832014-12-18 17:42:32 -08002474
David Benjamin27e377e2017-07-31 19:09:42 -04002475void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*cb)(SSL *ssl, int is_export,
2476 int keylength)) {}
Adam Langleyfcf25832014-12-18 17:42:32 -08002477
David Benjamin0a9bf662017-06-21 16:34:15 -04002478static int use_psk_identity_hint(char **out, const char *identity_hint) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002479 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
David Benjamin3570d732015-06-29 00:28:17 -04002480 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -08002481 return 0;
2482 }
2483
David Benjaminc11ea9422017-08-29 16:33:21 -04002484 // Clear currently configured hint, if any.
David Benjamin0a9bf662017-06-21 16:34:15 -04002485 OPENSSL_free(*out);
2486 *out = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002487
David Benjaminc11ea9422017-08-29 16:33:21 -04002488 // Treat the empty hint as not supplying one. Plain PSK makes it possible to
2489 // send either no hint (omit ServerKeyExchange) or an empty hint, while
2490 // ECDHE_PSK can only spell empty hint. Having different capabilities is odd,
2491 // so we interpret empty and missing as identical.
David Benjamin78679342016-09-16 19:42:05 -04002492 if (identity_hint != NULL && identity_hint[0] != '\0') {
David Benjamin0a9bf662017-06-21 16:34:15 -04002493 *out = BUF_strdup(identity_hint);
2494 if (*out == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002495 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002496 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002497 }
Adam Langley95c29f32014-06-20 12:00:00 -07002498
Adam Langleyfcf25832014-12-18 17:42:32 -08002499 return 1;
2500}
Adam Langley95c29f32014-06-20 12:00:00 -07002501
David Benjamin0a9bf662017-06-21 16:34:15 -04002502int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
2503 return use_psk_identity_hint(&ctx->psk_identity_hint, identity_hint);
2504}
2505
2506int SSL_use_psk_identity_hint(SSL *ssl, const char *identity_hint) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002507 if (!ssl->config) {
2508 return 0;
2509 }
2510 return use_psk_identity_hint(&ssl->config->psk_identity_hint, identity_hint);
David Benjamin0a9bf662017-06-21 16:34:15 -04002511}
2512
David Benjamine8814df2015-09-15 08:05:54 -04002513const char *SSL_get_psk_identity_hint(const SSL *ssl) {
2514 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002515 return NULL;
2516 }
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002517 if (ssl->config == NULL) {
2518 assert(ssl->config);
2519 return NULL;
2520 }
2521 return ssl->config->psk_identity_hint;
Adam Langleyfcf25832014-12-18 17:42:32 -08002522}
Adam Langley95c29f32014-06-20 12:00:00 -07002523
David Benjamine8814df2015-09-15 08:05:54 -04002524const char *SSL_get_psk_identity(const SSL *ssl) {
Steven Valdez87eab492016-06-27 16:34:59 -04002525 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002526 return NULL;
2527 }
Steven Valdez87eab492016-06-27 16:34:59 -04002528 SSL_SESSION *session = SSL_get_session(ssl);
2529 if (session == NULL) {
2530 return NULL;
2531 }
2532 return session->psk_identity;
Adam Langleyfcf25832014-12-18 17:42:32 -08002533}
Adam Langley95c29f32014-06-20 12:00:00 -07002534
Adam Langleyfcf25832014-12-18 17:42:32 -08002535void SSL_set_psk_client_callback(
David Benjamine8814df2015-09-15 08:05:54 -04002536 SSL *ssl, unsigned (*cb)(SSL *ssl, const char *hint, char *identity,
2537 unsigned max_identity_len, uint8_t *psk,
2538 unsigned max_psk_len)) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002539 if (!ssl->config) {
2540 return;
2541 }
2542 ssl->config->psk_client_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002543}
Adam Langley95c29f32014-06-20 12:00:00 -07002544
Adam Langleyfcf25832014-12-18 17:42:32 -08002545void SSL_CTX_set_psk_client_callback(
David Benjamine8814df2015-09-15 08:05:54 -04002546 SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *hint, char *identity,
2547 unsigned max_identity_len, uint8_t *psk,
2548 unsigned max_psk_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002549 ctx->psk_client_callback = cb;
2550}
Adam Langley95c29f32014-06-20 12:00:00 -07002551
Adam Langleyfcf25832014-12-18 17:42:32 -08002552void SSL_set_psk_server_callback(
David Benjamine8814df2015-09-15 08:05:54 -04002553 SSL *ssl, unsigned (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
2554 unsigned max_psk_len)) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002555 if (!ssl->config) {
2556 return;
2557 }
2558 ssl->config->psk_server_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002559}
Adam Langley95c29f32014-06-20 12:00:00 -07002560
Adam Langleyfcf25832014-12-18 17:42:32 -08002561void SSL_CTX_set_psk_server_callback(
David Benjamine8814df2015-09-15 08:05:54 -04002562 SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *identity,
2563 uint8_t *psk, unsigned max_psk_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002564 ctx->psk_server_callback = cb;
2565}
Adam Langley95c29f32014-06-20 12:00:00 -07002566
Adam Langley512a2892017-12-30 08:04:39 -08002567int SSL_set_dummy_pq_padding_size(SSL *ssl, size_t num_bytes) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002568 if (!ssl->config) {
2569 return 0;
2570 }
Adam Langley512a2892017-12-30 08:04:39 -08002571 if (num_bytes > 0xffff) {
2572 return 0;
2573 }
2574
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002575 ssl->config->dummy_pq_padding_len = num_bytes;
Adam Langley512a2892017-12-30 08:04:39 -08002576 return 1;
2577}
2578
Adam Langley8df8e642018-02-28 13:35:39 -08002579int SSL_dummy_pq_padding_used(SSL *ssl) {
Adam Langley40cdb3b2018-03-07 12:03:35 -08002580 if (ssl->server) {
Adam Langley8df8e642018-02-28 13:35:39 -08002581 return 0;
2582 }
2583
2584 return ssl->did_dummy_pq_padding;
2585}
2586
Adam Langleyfcf25832014-12-18 17:42:32 -08002587void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
2588 void (*cb)(int write_p, int version,
2589 int content_type, const void *buf,
2590 size_t len, SSL *ssl, void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002591 ctx->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002592}
David Benjamin61ecccf2015-05-05 09:44:51 -04002593
2594void SSL_CTX_set_msg_callback_arg(SSL_CTX *ctx, void *arg) {
2595 ctx->msg_callback_arg = arg;
2596}
2597
Adam Langleyfcf25832014-12-18 17:42:32 -08002598void SSL_set_msg_callback(SSL *ssl,
2599 void (*cb)(int write_p, int version, int content_type,
2600 const void *buf, size_t len, SSL *ssl,
2601 void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002602 ssl->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002603}
Adam Langley95c29f32014-06-20 12:00:00 -07002604
David Benjamin61ecccf2015-05-05 09:44:51 -04002605void SSL_set_msg_callback_arg(SSL *ssl, void *arg) {
2606 ssl->msg_callback_arg = arg;
2607}
2608
David Benjamind28f59c2015-11-17 22:32:50 -05002609void SSL_CTX_set_keylog_callback(SSL_CTX *ctx,
2610 void (*cb)(const SSL *ssl, const char *line)) {
2611 ctx->keylog_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002612}
Adam Langley95c29f32014-06-20 12:00:00 -07002613
David Benjamin6e3f5cc2016-09-14 14:43:14 -04002614void (*SSL_CTX_get_keylog_callback(const SSL_CTX *ctx))(const SSL *ssl,
2615 const char *line) {
2616 return ctx->keylog_callback;
2617}
2618
Taylor Brandstetter9edb2c62016-06-08 15:26:59 -07002619void SSL_CTX_set_current_time_cb(SSL_CTX *ctx,
2620 void (*cb)(const SSL *ssl,
2621 struct timeval *out_clock)) {
2622 ctx->current_time_cb = cb;
2623}
2624
David Benjamin5d8b1282015-10-17 23:26:35 -04002625int SSL_is_init_finished(const SSL *ssl) {
David Benjamin9e766d72017-02-10 23:14:17 -05002626 return !SSL_in_init(ssl);
David Benjamin5d8b1282015-10-17 23:26:35 -04002627}
2628
2629int SSL_in_init(const SSL *ssl) {
Steven Valdezd8168742017-08-31 10:15:48 -04002630 // This returns false once all the handshake state has been finalized, to
2631 // allow callbacks and getters based on SSL_in_init to return the correct
2632 // values.
David Benjamin8e7bbba2017-10-13 17:18:35 -04002633 SSL_HANDSHAKE *hs = ssl->s3->hs.get();
Steven Valdezd8168742017-08-31 10:15:48 -04002634 return hs != nullptr && !hs->handshake_finalized;
David Benjamin5d8b1282015-10-17 23:26:35 -04002635}
2636
2637int SSL_in_false_start(const SSL *ssl) {
David Benjamina0486782016-10-06 19:11:32 -04002638 if (ssl->s3->hs == NULL) {
2639 return 0;
2640 }
2641 return ssl->s3->hs->in_false_start;
David Benjamined7c4752015-02-16 19:16:46 -05002642}
2643
David Benjamin0d56f882015-12-19 17:05:56 -05002644int SSL_cutthrough_complete(const SSL *ssl) {
2645 return SSL_in_false_start(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08002646}
Adam Langley95c29f32014-06-20 12:00:00 -07002647
Adam Langleyfcf25832014-12-18 17:42:32 -08002648void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
2649 size_t *ssl_session_size) {
2650 *ssl_size = sizeof(SSL);
2651 *ssl_ctx_size = sizeof(SSL_CTX);
2652 *ssl_session_size = sizeof(SSL_SESSION);
2653}
Feng Lu41aa3252014-11-21 22:47:56 -08002654
David Benjamince079fd2016-08-02 16:22:34 -04002655int SSL_is_server(const SSL *ssl) { return ssl->server; }
2656
2657int SSL_is_dtls(const SSL *ssl) { return ssl->method->is_dtls; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002658
Alessandro Ghedini57e81e62017-03-14 23:36:00 +00002659void SSL_CTX_set_select_certificate_cb(
2660 SSL_CTX *ctx,
2661 enum ssl_select_cert_result_t (*cb)(const SSL_CLIENT_HELLO *)) {
David Benjamind4c2bce2015-10-17 12:28:18 -04002662 ctx->select_certificate_cb = cb;
2663}
2664
David Benjamin731058e2016-12-03 23:15:13 -05002665void SSL_CTX_set_dos_protection_cb(SSL_CTX *ctx,
2666 int (*cb)(const SSL_CLIENT_HELLO *)) {
Adam Langley524e7172015-02-20 16:04:00 -08002667 ctx->dos_protection_cb = cb;
2668}
2669
David Benjamin1d5ef3b2015-10-12 19:54:18 -04002670void SSL_set_renegotiate_mode(SSL *ssl, enum ssl_renegotiate_mode_t mode) {
2671 ssl->renegotiate_mode = mode;
David Benjamin71666cb2018-05-01 17:14:27 -04002672
2673 // Check if |ssl_can_renegotiate| has changed and the configuration may now be
2674 // shed. HTTP clients may initially allow renegotiation for HTTP/1.1, and then
2675 // disable after the handshake once the ALPN protocol is known to be HTTP/2.
2676 ssl_maybe_shed_handshake_config(ssl);
David Benjamin1d5ef3b2015-10-12 19:54:18 -04002677}
2678
Adam Langleyc2d32802015-11-03 18:36:10 -08002679int SSL_get_ivs(const SSL *ssl, const uint8_t **out_read_iv,
2680 const uint8_t **out_write_iv, size_t *out_iv_len) {
Adam Langleyc2d32802015-11-03 18:36:10 -08002681 size_t write_iv_len;
David Benjamincfc11c22017-07-18 22:45:18 -04002682 if (!ssl->s3->aead_read_ctx->GetIV(out_read_iv, out_iv_len) ||
2683 !ssl->s3->aead_write_ctx->GetIV(out_write_iv, &write_iv_len) ||
Adam Langleyc2d32802015-11-03 18:36:10 -08002684 *out_iv_len != write_iv_len) {
2685 return 0;
2686 }
2687
2688 return 1;
David Benjamine348ff42015-11-06 17:55:30 -05002689}
David Benjamin6e807652015-11-02 12:02:20 -05002690
David Benjamin91b25012016-01-21 16:39:58 -05002691static uint64_t be_to_u64(const uint8_t in[8]) {
David Benjaminde942382016-02-11 12:02:01 -05002692 return (((uint64_t)in[0]) << 56) | (((uint64_t)in[1]) << 48) |
2693 (((uint64_t)in[2]) << 40) | (((uint64_t)in[3]) << 32) |
2694 (((uint64_t)in[4]) << 24) | (((uint64_t)in[5]) << 16) |
2695 (((uint64_t)in[6]) << 8) | ((uint64_t)in[7]);
David Benjamin91b25012016-01-21 16:39:58 -05002696}
2697
2698uint64_t SSL_get_read_sequence(const SSL *ssl) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002699 // TODO(davidben): Internally represent sequence numbers as uint64_t.
David Benjamince079fd2016-08-02 16:22:34 -04002700 if (SSL_is_dtls(ssl)) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002701 // max_seq_num already includes the epoch.
David Benjaminde942382016-02-11 12:02:01 -05002702 assert(ssl->d1->r_epoch == (ssl->d1->bitmap.max_seq_num >> 48));
2703 return ssl->d1->bitmap.max_seq_num;
2704 }
David Benjamin91b25012016-01-21 16:39:58 -05002705 return be_to_u64(ssl->s3->read_sequence);
2706}
2707
2708uint64_t SSL_get_write_sequence(const SSL *ssl) {
David Benjaminde942382016-02-11 12:02:01 -05002709 uint64_t ret = be_to_u64(ssl->s3->write_sequence);
David Benjamince079fd2016-08-02 16:22:34 -04002710 if (SSL_is_dtls(ssl)) {
David Benjaminde942382016-02-11 12:02:01 -05002711 assert((ret >> 48) == 0);
2712 ret |= ((uint64_t)ssl->d1->w_epoch) << 48;
2713 }
2714 return ret;
David Benjamin91b25012016-01-21 16:39:58 -05002715}
2716
Steven Valdez02563852016-06-23 13:33:05 -04002717uint16_t SSL_get_peer_signature_algorithm(const SSL *ssl) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002718 // TODO(davidben): This checks the wrong session if there is a renegotiation
2719 // in progress.
David Benjaminf1050fd2016-12-13 20:05:36 -05002720 SSL_SESSION *session = SSL_get_session(ssl);
2721 if (session == NULL) {
2722 return 0;
2723 }
2724
2725 return session->peer_signature_algorithm;
Steven Valdez02563852016-06-23 13:33:05 -04002726}
2727
David Benjamin96ba15f2015-12-25 14:10:17 -05002728size_t SSL_get_client_random(const SSL *ssl, uint8_t *out, size_t max_out) {
2729 if (max_out == 0) {
2730 return sizeof(ssl->s3->client_random);
2731 }
2732 if (max_out > sizeof(ssl->s3->client_random)) {
2733 max_out = sizeof(ssl->s3->client_random);
2734 }
David Benjamin17cf2cb2016-12-13 01:07:13 -05002735 OPENSSL_memcpy(out, ssl->s3->client_random, max_out);
David Benjamin96ba15f2015-12-25 14:10:17 -05002736 return max_out;
2737}
2738
2739size_t SSL_get_server_random(const SSL *ssl, uint8_t *out, size_t max_out) {
2740 if (max_out == 0) {
2741 return sizeof(ssl->s3->server_random);
2742 }
2743 if (max_out > sizeof(ssl->s3->server_random)) {
2744 max_out = sizeof(ssl->s3->server_random);
2745 }
David Benjamin17cf2cb2016-12-13 01:07:13 -05002746 OPENSSL_memcpy(out, ssl->s3->server_random, max_out);
David Benjamin96ba15f2015-12-25 14:10:17 -05002747 return max_out;
2748}
2749
David Benjaminf6494f42016-01-07 12:37:41 -08002750const SSL_CIPHER *SSL_get_pending_cipher(const SSL *ssl) {
David Benjamin8e7bbba2017-10-13 17:18:35 -04002751 SSL_HANDSHAKE *hs = ssl->s3->hs.get();
David Benjamin45738dd2017-02-09 20:01:26 -05002752 if (hs == NULL) {
David Benjaminf6494f42016-01-07 12:37:41 -08002753 return NULL;
2754 }
David Benjamin45738dd2017-02-09 20:01:26 -05002755 return hs->new_cipher;
David Benjaminf6494f42016-01-07 12:37:41 -08002756}
2757
David Benjaminbbaf3672016-11-17 10:53:09 +09002758void SSL_set_retain_only_sha256_of_client_certs(SSL *ssl, int enabled) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002759 if (!ssl->config) {
2760 return;
2761 }
2762 ssl->config->retain_only_sha256_of_client_certs = !!enabled;
David Benjaminbbaf3672016-11-17 10:53:09 +09002763}
2764
David Benjamincdd0b7e2016-01-07 10:10:41 -08002765void SSL_CTX_set_retain_only_sha256_of_client_certs(SSL_CTX *ctx, int enabled) {
2766 ctx->retain_only_sha256_of_client_certs = !!enabled;
2767}
2768
David Benjamin65ac9972016-09-02 21:35:25 -04002769void SSL_CTX_set_grease_enabled(SSL_CTX *ctx, int enabled) {
2770 ctx->grease_enabled = !!enabled;
2771}
2772
David Benjamin35ac5b72017-03-03 15:05:56 -05002773int32_t SSL_get_ticket_age_skew(const SSL *ssl) {
2774 return ssl->s3->ticket_age_skew;
2775}
2776
David Benjamin494e4d02017-12-01 13:05:12 -05002777void SSL_CTX_set_false_start_allowed_without_alpn(SSL_CTX *ctx, int allowed) {
2778 ctx->false_start_allowed_without_alpn = !!allowed;
2779}
2780
David Benjamin6df65402017-12-18 18:00:23 -05002781int SSL_is_draft_downgrade(const SSL *ssl) { return ssl->s3->draft_downgrade; }
2782
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002783void SSL_set_shed_handshake_config(SSL *ssl, int enable) {
2784 if (!ssl->config) {
2785 return;
2786 }
2787 ssl->config->shed_handshake_config = !!enable;
2788}
2789
David Benjamin27bbae42015-09-13 00:54:37 -04002790int SSL_clear(SSL *ssl) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002791 if (!ssl->config) {
2792 return 0; // SSL_clear may not be used after shedding config.
2793 }
2794
David Benjaminc11ea9422017-08-29 16:33:21 -04002795 // In OpenSSL, reusing a client |SSL| with |SSL_clear| causes the previously
2796 // established session to be offered the next time around. wpa_supplicant
2797 // depends on this behavior, so emulate it.
David Benjamin8e7bbba2017-10-13 17:18:35 -04002798 UniquePtr<SSL_SESSION> session;
David Benjaminb79cc842016-12-07 15:57:14 -05002799 if (!ssl->server && ssl->s3->established_session != NULL) {
David Benjamin8e7bbba2017-10-13 17:18:35 -04002800 session.reset(ssl->s3->established_session.get());
2801 SSL_SESSION_up_ref(session.get());
David Benjaminb79cc842016-12-07 15:57:14 -05002802 }
2803
David Benjaminc11ea9422017-08-29 16:33:21 -04002804 // The ssl->d1->mtu is simultaneously configuration (preserved across
2805 // clear) and connection-specific state (gets reset).
2806 //
2807 // TODO(davidben): Avoid this.
David Benjamin27bbae42015-09-13 00:54:37 -04002808 unsigned mtu = 0;
2809 if (ssl->d1 != NULL) {
2810 mtu = ssl->d1->mtu;
2811 }
2812
2813 ssl->method->ssl_free(ssl);
2814 if (!ssl->method->ssl_new(ssl)) {
2815 return 0;
2816 }
David Benjamin27bbae42015-09-13 00:54:37 -04002817
David Benjamince079fd2016-08-02 16:22:34 -04002818 if (SSL_is_dtls(ssl) && (SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
David Benjamin27bbae42015-09-13 00:54:37 -04002819 ssl->d1->mtu = mtu;
2820 }
2821
David Benjamin8e7bbba2017-10-13 17:18:35 -04002822 if (session != nullptr) {
2823 SSL_set_session(ssl, session.get());
David Benjaminb79cc842016-12-07 15:57:14 -05002824 }
2825
David Benjamin27bbae42015-09-13 00:54:37 -04002826 return 1;
2827}
2828
David Benjaminda881e92015-04-26 14:45:04 -04002829int SSL_CTX_sess_connect(const SSL_CTX *ctx) { return 0; }
2830int SSL_CTX_sess_connect_good(const SSL_CTX *ctx) { return 0; }
2831int SSL_CTX_sess_connect_renegotiate(const SSL_CTX *ctx) { return 0; }
2832int SSL_CTX_sess_accept(const SSL_CTX *ctx) { return 0; }
2833int SSL_CTX_sess_accept_renegotiate(const SSL_CTX *ctx) { return 0; }
2834int SSL_CTX_sess_accept_good(const SSL_CTX *ctx) { return 0; }
2835int SSL_CTX_sess_hits(const SSL_CTX *ctx) { return 0; }
2836int SSL_CTX_sess_cb_hits(const SSL_CTX *ctx) { return 0; }
2837int SSL_CTX_sess_misses(const SSL_CTX *ctx) { return 0; }
2838int SSL_CTX_sess_timeouts(const SSL_CTX *ctx) { return 0; }
2839int SSL_CTX_sess_cache_full(const SSL_CTX *ctx) { return 0; }
David Benjamin936aada2016-06-07 19:09:22 -04002840
2841int SSL_num_renegotiations(const SSL *ssl) {
2842 return SSL_total_renegotiations(ssl);
2843}
2844
2845int SSL_CTX_need_tmp_RSA(const SSL_CTX *ctx) { return 0; }
2846int SSL_need_tmp_RSA(const SSL *ssl) { return 0; }
2847int SSL_CTX_set_tmp_rsa(SSL_CTX *ctx, const RSA *rsa) { return 1; }
2848int SSL_set_tmp_rsa(SSL *ssl, const RSA *rsa) { return 1; }
Matt Braithwaite4838d8a2015-08-20 13:20:03 -07002849void ERR_load_SSL_strings(void) {}
David Benjamin27bbae42015-09-13 00:54:37 -04002850void SSL_load_error_strings(void) {}
David Benjamin936aada2016-06-07 19:09:22 -04002851int SSL_cache_hit(SSL *ssl) { return SSL_session_reused(ssl); }
2852
2853int SSL_CTX_set_tmp_ecdh(SSL_CTX *ctx, const EC_KEY *ec_key) {
2854 if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) {
2855 OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
2856 return 0;
2857 }
2858 int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key));
2859 return SSL_CTX_set1_curves(ctx, &nid, 1);
2860}
2861
2862int SSL_set_tmp_ecdh(SSL *ssl, const EC_KEY *ec_key) {
2863 if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) {
2864 OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
2865 return 0;
2866 }
2867 int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key));
2868 return SSL_set1_curves(ssl, &nid, 1);
2869}
David Benjamin721e8b72016-08-03 13:13:17 -04002870
Adam Langley4c341d02017-03-08 19:33:21 -08002871void SSL_CTX_set_ticket_aead_method(SSL_CTX *ctx,
2872 const SSL_TICKET_AEAD_METHOD *aead_method) {
2873 ctx->ticket_aead_method = aead_method;
2874}