blob: 7a75776b7c42467cc534cd58d05b18770518dc6f [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.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
108 *
109 */
110/* ====================================================================
111 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
112 * ECC cipher suite support in OpenSSL originally developed by
113 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
114 */
115/* ====================================================================
116 * Copyright 2005 Nokia. All rights reserved.
117 *
118 * The portions of the attached software ("Contribution") is developed by
119 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
120 * license.
121 *
122 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
123 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
124 * support (see RFC 4279) to OpenSSL.
125 *
126 * No patent licenses or other rights except those expressly stated in
127 * the OpenSSL open source license shall be deemed granted or received
128 * expressly, by implication, estoppel, or otherwise.
129 *
130 * No assurances are provided by Nokia that the Contribution does not
131 * infringe the patent or other intellectual property rights of any third
132 * party or that the license provides you with all the necessary rights
133 * to make use of the Contribution.
134 *
135 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
136 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
137 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
138 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
139 * OTHERWISE. */
140
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 Benjamin86e95b82017-07-18 16:34:25 -0400190void ssl_reset_error_state(SSL *ssl) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400191 // Functions which use |SSL_get_error| must reset I/O and error state on
192 // entry.
David Benjamin86e95b82017-07-18 16:34:25 -0400193 ssl->rwstate = SSL_NOTHING;
194 ERR_clear_error();
195 ERR_clear_system_error();
196}
197
198int ssl_can_write(const SSL *ssl) {
199 return !SSL_in_init(ssl) || ssl->s3->hs->can_early_write;
200}
201
202int ssl_can_read(const SSL *ssl) {
203 return !SSL_in_init(ssl) || ssl->s3->hs->can_early_read;
204}
205
206void ssl_cipher_preference_list_free(
207 struct ssl_cipher_preference_list_st *cipher_list) {
208 if (cipher_list == NULL) {
209 return;
210 }
211 sk_SSL_CIPHER_free(cipher_list->ciphers);
212 OPENSSL_free(cipher_list->in_group_flags);
213 OPENSSL_free(cipher_list);
214}
215
216void ssl_update_cache(SSL_HANDSHAKE *hs, int mode) {
217 SSL *const ssl = hs->ssl;
218 SSL_CTX *ctx = ssl->session_ctx;
David Benjaminc11ea9422017-08-29 16:33:21 -0400219 // Never cache sessions with empty session IDs.
David Benjamin86e95b82017-07-18 16:34:25 -0400220 if (ssl->s3->established_session->session_id_length == 0 ||
David Benjamin5ef40c62017-08-23 21:28:29 -0700221 ssl->s3->established_session->not_resumable ||
David Benjamin86e95b82017-07-18 16:34:25 -0400222 (ctx->session_cache_mode & mode) != mode) {
223 return;
224 }
225
David Benjaminc11ea9422017-08-29 16:33:21 -0400226 // Clients never use the internal session cache.
David Benjamin86e95b82017-07-18 16:34:25 -0400227 int use_internal_cache = ssl->server && !(ctx->session_cache_mode &
228 SSL_SESS_CACHE_NO_INTERNAL_STORE);
229
David Benjaminc11ea9422017-08-29 16:33:21 -0400230 // A client may see new sessions on abbreviated handshakes if the server
231 // decides to renew the ticket. Once the handshake is completed, it should be
232 // inserted into the cache.
David Benjamin86e95b82017-07-18 16:34:25 -0400233 if (ssl->s3->established_session != ssl->session ||
234 (!ssl->server && hs->ticket_expected)) {
235 if (use_internal_cache) {
236 SSL_CTX_add_session(ctx, ssl->s3->established_session);
237 }
238 if (ctx->new_session_cb != NULL) {
239 SSL_SESSION_up_ref(ssl->s3->established_session);
240 if (!ctx->new_session_cb(ssl, ssl->s3->established_session)) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400241 // |new_session_cb|'s return value signals whether it took ownership.
David Benjamin86e95b82017-07-18 16:34:25 -0400242 SSL_SESSION_free(ssl->s3->established_session);
243 }
244 }
245 }
246
247 if (use_internal_cache &&
248 !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR)) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400249 // Automatically flush the internal session cache every 255 connections.
David Benjamin86e95b82017-07-18 16:34:25 -0400250 int flush_cache = 0;
251 CRYPTO_MUTEX_lock_write(&ctx->lock);
252 ctx->handshakes_since_cache_flush++;
253 if (ctx->handshakes_since_cache_flush >= 255) {
254 flush_cache = 1;
255 ctx->handshakes_since_cache_flush = 0;
256 }
257 CRYPTO_MUTEX_unlock_write(&ctx->lock);
258
259 if (flush_cache) {
260 struct OPENSSL_timeval now;
261 ssl_get_current_time(ssl, &now);
262 SSL_CTX_flush_sessions(ctx, now.tv_sec);
263 }
264 }
265}
266
267static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
268 static const char hextable[] = "0123456789abcdef";
269 uint8_t *out;
270
271 if (!CBB_add_space(cbb, &out, in_len * 2)) {
272 return 0;
273 }
274
275 for (size_t i = 0; i < in_len; i++) {
276 *(out++) = (uint8_t)hextable[in[i] >> 4];
277 *(out++) = (uint8_t)hextable[in[i] & 0xf];
278 }
279
280 return 1;
281}
282
283int ssl_log_secret(const SSL *ssl, const char *label, const uint8_t *secret,
284 size_t secret_len) {
285 if (ssl->ctx->keylog_callback == NULL) {
286 return 1;
287 }
288
David Benjamin1386aad2017-07-19 23:57:40 -0400289 ScopedCBB cbb;
David Benjamin86e95b82017-07-18 16:34:25 -0400290 uint8_t *out;
291 size_t out_len;
David Benjamin1386aad2017-07-19 23:57:40 -0400292 if (!CBB_init(cbb.get(), strlen(label) + 1 + SSL3_RANDOM_SIZE * 2 + 1 +
David Benjamin86e95b82017-07-18 16:34:25 -0400293 secret_len * 2 + 1) ||
David Benjamin1386aad2017-07-19 23:57:40 -0400294 !CBB_add_bytes(cbb.get(), (const uint8_t *)label, strlen(label)) ||
295 !CBB_add_bytes(cbb.get(), (const uint8_t *)" ", 1) ||
296 !cbb_add_hex(cbb.get(), ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
297 !CBB_add_bytes(cbb.get(), (const uint8_t *)" ", 1) ||
298 !cbb_add_hex(cbb.get(), secret, secret_len) ||
299 !CBB_add_u8(cbb.get(), 0 /* NUL */) ||
300 !CBB_finish(cbb.get(), &out, &out_len)) {
David Benjamin86e95b82017-07-18 16:34:25 -0400301 return 0;
302 }
303
304 ssl->ctx->keylog_callback(ssl, (const char *)out);
305 OPENSSL_free(out);
306 return 1;
307}
308
309int ssl3_can_false_start(const SSL *ssl) {
310 const SSL_CIPHER *const cipher = SSL_get_current_cipher(ssl);
311
David Benjaminc11ea9422017-08-29 16:33:21 -0400312 // False Start only for TLS 1.2 with an ECDHE+AEAD cipher and ALPN or NPN.
David Benjamin86e95b82017-07-18 16:34:25 -0400313 return !SSL_is_dtls(ssl) &&
314 SSL_version(ssl) == TLS1_2_VERSION &&
315 (ssl->s3->alpn_selected != NULL ||
316 ssl->s3->next_proto_negotiated != NULL) &&
317 cipher != NULL &&
318 cipher->algorithm_mkey == SSL_kECDHE &&
319 cipher->algorithm_mac == SSL_AEAD;
320}
321
322void ssl_do_info_callback(const SSL *ssl, int type, int value) {
323 void (*cb)(const SSL *ssl, int type, int value) = NULL;
324 if (ssl->info_callback != NULL) {
325 cb = ssl->info_callback;
326 } else if (ssl->ctx->info_callback != NULL) {
327 cb = ssl->ctx->info_callback;
328 }
329
330 if (cb != NULL) {
331 cb(ssl, type, value);
332 }
333}
334
335void ssl_do_msg_callback(SSL *ssl, int is_write, int content_type,
336 const void *buf, size_t len) {
337 if (ssl->msg_callback == NULL) {
338 return;
339 }
340
David Benjaminc11ea9422017-08-29 16:33:21 -0400341 // |version| is zero when calling for |SSL3_RT_HEADER| and |SSL2_VERSION| for
342 // a V2ClientHello.
David Benjamin86e95b82017-07-18 16:34:25 -0400343 int version;
344 switch (content_type) {
345 case 0:
David Benjaminc11ea9422017-08-29 16:33:21 -0400346 // V2ClientHello
David Benjamin86e95b82017-07-18 16:34:25 -0400347 version = SSL2_VERSION;
348 break;
349 case SSL3_RT_HEADER:
350 version = 0;
351 break;
352 default:
353 version = SSL_version(ssl);
354 }
355
356 ssl->msg_callback(is_write, version, content_type, buf, len, ssl,
357 ssl->msg_callback_arg);
358}
359
360void ssl_get_current_time(const SSL *ssl, struct OPENSSL_timeval *out_clock) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400361 // TODO(martinkr): Change callers to |ssl_ctx_get_current_time| and drop the
362 // |ssl| arg from |current_time_cb| if possible.
Martin Kreichgauer72912d22017-08-04 12:06:43 -0700363 ssl_ctx_get_current_time(ssl->ctx, out_clock);
364}
365
366void ssl_ctx_get_current_time(const SSL_CTX *ctx,
367 struct OPENSSL_timeval *out_clock) {
368 if (ctx->current_time_cb != NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400369 // TODO(davidben): Update current_time_cb to use OPENSSL_timeval. See
370 // https://crbug.com/boringssl/155.
David Benjamin86e95b82017-07-18 16:34:25 -0400371 struct timeval clock;
Martin Kreichgauer72912d22017-08-04 12:06:43 -0700372 ctx->current_time_cb(nullptr /* ssl */, &clock);
David Benjamin86e95b82017-07-18 16:34:25 -0400373 if (clock.tv_sec < 0) {
374 assert(0);
375 out_clock->tv_sec = 0;
376 out_clock->tv_usec = 0;
377 } else {
378 out_clock->tv_sec = (uint64_t)clock.tv_sec;
379 out_clock->tv_usec = (uint32_t)clock.tv_usec;
380 }
381 return;
382 }
383
384#if defined(BORINGSSL_UNSAFE_DETERMINISTIC_MODE)
385 out_clock->tv_sec = 1234;
386 out_clock->tv_usec = 1234;
387#elif defined(OPENSSL_WINDOWS)
388 struct _timeb time;
389 _ftime(&time);
390 if (time.time < 0) {
391 assert(0);
392 out_clock->tv_sec = 0;
393 out_clock->tv_usec = 0;
394 } else {
395 out_clock->tv_sec = time.time;
396 out_clock->tv_usec = time.millitm * 1000;
397 }
398#else
399 struct timeval clock;
400 gettimeofday(&clock, NULL);
401 if (clock.tv_sec < 0) {
402 assert(0);
403 out_clock->tv_sec = 0;
404 out_clock->tv_usec = 0;
405 } else {
406 out_clock->tv_sec = (uint64_t)clock.tv_sec;
407 out_clock->tv_usec = (uint32_t)clock.tv_usec;
408 }
409#endif
410}
411
412} // namespace bssl
413
414using namespace bssl;
415
David Benjamin7c6d09b2015-09-05 11:00:19 -0400416int SSL_library_init(void) {
417 CRYPTO_library_init();
418 return 1;
419}
420
Adam Langleye976e432016-02-15 14:01:10 -0800421static uint32_t ssl_session_hash(const SSL_SESSION *sess) {
422 const uint8_t *session_id = sess->session_id;
423
424 uint8_t tmp_storage[sizeof(uint32_t)];
425 if (sess->session_id_length < sizeof(tmp_storage)) {
David Benjamin17cf2cb2016-12-13 01:07:13 -0500426 OPENSSL_memset(tmp_storage, 0, sizeof(tmp_storage));
427 OPENSSL_memcpy(tmp_storage, sess->session_id, sess->session_id_length);
Adam Langleye976e432016-02-15 14:01:10 -0800428 session_id = tmp_storage;
429 }
430
David Benjamin27bbae42015-09-13 00:54:37 -0400431 uint32_t hash =
Adam Langleye976e432016-02-15 14:01:10 -0800432 ((uint32_t)session_id[0]) |
433 ((uint32_t)session_id[1] << 8) |
434 ((uint32_t)session_id[2] << 16) |
435 ((uint32_t)session_id[3] << 24);
David Benjamin7c6d09b2015-09-05 11:00:19 -0400436
David Benjamin27bbae42015-09-13 00:54:37 -0400437 return hash;
438}
439
David Benjaminc11ea9422017-08-29 16:33:21 -0400440// NB: If this function (or indeed the hash function which uses a sort of
441// coarser function than this one) is changed, ensure
442// SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
443// able to construct an SSL_SESSION that will collide with any existing session
444// with a matching session ID.
David Benjamin27bbae42015-09-13 00:54:37 -0400445static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
446 if (a->ssl_version != b->ssl_version) {
447 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800448 }
Adam Langley95c29f32014-06-20 12:00:00 -0700449
David Benjamin27bbae42015-09-13 00:54:37 -0400450 if (a->session_id_length != b->session_id_length) {
451 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800452 }
Adam Langley95c29f32014-06-20 12:00:00 -0700453
David Benjamin17cf2cb2016-12-13 01:07:13 -0500454 return OPENSSL_memcmp(a->session_id, b->session_id, a->session_id_length);
David Benjamin27bbae42015-09-13 00:54:37 -0400455}
Adam Langley95c29f32014-06-20 12:00:00 -0700456
David Benjamin27bbae42015-09-13 00:54:37 -0400457SSL_CTX *SSL_CTX_new(const SSL_METHOD *method) {
458 SSL_CTX *ret = NULL;
459
460 if (method == NULL) {
461 OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_METHOD_PASSED);
462 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800463 }
Adam Langley95c29f32014-06-20 12:00:00 -0700464
David Benjaminf5260812017-07-12 17:16:39 -0400465 ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
David Benjamin27bbae42015-09-13 00:54:37 -0400466 if (ret == NULL) {
467 goto err;
David Benjamin62fd1622015-01-11 13:30:01 -0500468 }
469
David Benjamin17cf2cb2016-12-13 01:07:13 -0500470 OPENSSL_memset(ret, 0, sizeof(SSL_CTX));
Adam Langley95c29f32014-06-20 12:00:00 -0700471
David Benjamin27bbae42015-09-13 00:54:37 -0400472 ret->method = method->method;
Adam Langley3509dac2017-02-01 11:59:18 -0800473 ret->x509_method = method->x509_method;
David Benjamin27bbae42015-09-13 00:54:37 -0400474
475 CRYPTO_MUTEX_init(&ret->lock);
476
477 ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
478 ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
479
David Benjamin27bbae42015-09-13 00:54:37 -0400480 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
David Benjamin17b30832017-01-28 14:00:32 -0500481 ret->session_psk_dhe_timeout = SSL_DEFAULT_SESSION_PSK_DHE_TIMEOUT;
David Benjamin27bbae42015-09-13 00:54:37 -0400482
483 ret->references = 1;
484
485 ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
486 ret->verify_mode = SSL_VERIFY_NONE;
Adam Langley3509dac2017-02-01 11:59:18 -0800487 ret->cert = ssl_cert_new(method->x509_method);
David Benjamin27bbae42015-09-13 00:54:37 -0400488 if (ret->cert == NULL) {
489 goto err;
490 }
491
492 ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
493 if (ret->sessions == NULL) {
494 goto err;
495 }
Adam Langley2a3b3432017-02-10 13:56:48 -0800496
497 if (!ret->x509_method->ssl_ctx_new(ret)) {
David Benjamin27bbae42015-09-13 00:54:37 -0400498 goto err;
499 }
500
Matthew Braithwaite6ad20dc2017-02-21 16:41:33 -0800501 if (!SSL_CTX_set_strict_cipher_list(ret, SSL_DEFAULT_CIPHER_LIST)) {
David Benjamin27bbae42015-09-13 00:54:37 -0400502 goto err2;
503 }
504
Adam Langley34b4c822017-02-02 10:57:17 -0800505 ret->client_CA = sk_CRYPTO_BUFFER_new_null();
David Benjamin27bbae42015-09-13 00:54:37 -0400506 if (ret->client_CA == NULL) {
507 goto err;
508 }
509
David Benjamin8a589332015-12-04 23:14:35 -0500510 CRYPTO_new_ex_data(&ret->ex_data);
David Benjamin27bbae42015-09-13 00:54:37 -0400511
512 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
513
David Benjaminc11ea9422017-08-29 16:33:21 -0400514 // Disable the auto-chaining feature by default. Once this has stuck without
515 // problems, the feature will be removed entirely.
David Benjamina983b4c2016-11-09 14:21:12 -0500516 ret->mode = SSL_MODE_NO_AUTO_CHAIN;
517
David Benjaminc11ea9422017-08-29 16:33:21 -0400518 // Lock the SSL_CTX to the specified version, for compatibility with legacy
519 // uses of SSL_METHOD, but we do not set the minimum version for
520 // |SSLv3_method|.
David Benjamine34bcc92016-09-21 16:53:09 -0400521 if (!SSL_CTX_set_max_proto_version(ret, method->version) ||
David Benjamin3cfeb952017-03-01 16:48:38 -0500522 !SSL_CTX_set_min_proto_version(ret, method->version == SSL3_VERSION
David Benjaminc11ea9422017-08-29 16:33:21 -0400523 ? 0 // default
David Benjamin3cfeb952017-03-01 16:48:38 -0500524 : method->version)) {
David Benjamine34bcc92016-09-21 16:53:09 -0400525 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
526 goto err2;
David Benjamin27bbae42015-09-13 00:54:37 -0400527 }
528
529 return ret;
530
531err:
532 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
533err2:
534 SSL_CTX_free(ret);
535 return NULL;
536}
537
Adam Langley84cd1592016-06-30 18:29:44 -0700538int SSL_CTX_up_ref(SSL_CTX *ctx) {
539 CRYPTO_refcount_inc(&ctx->references);
540 return 1;
541}
542
David Benjamin27bbae42015-09-13 00:54:37 -0400543void SSL_CTX_free(SSL_CTX *ctx) {
544 if (ctx == NULL ||
545 !CRYPTO_refcount_dec_and_test_zero(&ctx->references)) {
546 return;
547 }
548
David Benjaminc11ea9422017-08-29 16:33:21 -0400549 // Free internal session cache. However: the remove_cb() may reference the
550 // ex_data of SSL_CTX, thus the ex_data store can only be removed after the
551 // sessions were flushed. As the ex_data handling routines might also touch
552 // the session cache, the most secure solution seems to be: empty (flush) the
553 // cache, then free ex_data, then finally free the cache. (See ticket
554 // [openssl.org #212].)
David Benjamin27bbae42015-09-13 00:54:37 -0400555 SSL_CTX_flush_sessions(ctx, 0);
556
557 CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, ctx, &ctx->ex_data);
558
559 CRYPTO_MUTEX_cleanup(&ctx->lock);
560 lh_SSL_SESSION_free(ctx->sessions);
David Benjamin27bbae42015-09-13 00:54:37 -0400561 ssl_cipher_preference_list_free(ctx->cipher_list);
David Benjamin27bbae42015-09-13 00:54:37 -0400562 ssl_cert_free(ctx->cert);
563 sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->client_custom_extensions,
564 SSL_CUSTOM_EXTENSION_free);
565 sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->server_custom_extensions,
566 SSL_CUSTOM_EXTENSION_free);
Adam Langley34b4c822017-02-02 10:57:17 -0800567 sk_CRYPTO_BUFFER_pop_free(ctx->client_CA, CRYPTO_BUFFER_free);
Adam Langley2a3b3432017-02-10 13:56:48 -0800568 ctx->x509_method->ssl_ctx_free(ctx);
David Benjamin27bbae42015-09-13 00:54:37 -0400569 sk_SRTP_PROTECTION_PROFILE_free(ctx->srtp_profiles);
570 OPENSSL_free(ctx->psk_identity_hint);
Steven Valdezce902a92016-05-17 11:47:53 -0400571 OPENSSL_free(ctx->supported_group_list);
David Benjamin27bbae42015-09-13 00:54:37 -0400572 OPENSSL_free(ctx->alpn_client_proto_list);
David Benjamin27bbae42015-09-13 00:54:37 -0400573 EVP_PKEY_free(ctx->tlsext_channel_id_private);
David Benjamin71c21b42017-04-14 17:05:40 -0400574 OPENSSL_free(ctx->verify_sigalgs);
Martin Kreichgauer72912d22017-08-04 12:06:43 -0700575 OPENSSL_free(ctx->tlsext_ticket_key_current);
576 OPENSSL_free(ctx->tlsext_ticket_key_prev);
David Benjamin27bbae42015-09-13 00:54:37 -0400577
578 OPENSSL_free(ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -0800579}
Adam Langley95c29f32014-06-20 12:00:00 -0700580
Adam Langleyfcf25832014-12-18 17:42:32 -0800581SSL *SSL_new(SSL_CTX *ctx) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800582 if (ctx == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400583 OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_CTX);
Adam Langleyfcf25832014-12-18 17:42:32 -0800584 return NULL;
585 }
586 if (ctx->method == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400587 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
Adam Langleyfcf25832014-12-18 17:42:32 -0800588 return NULL;
589 }
Adam Langley95c29f32014-06-20 12:00:00 -0700590
David Benjaminf5260812017-07-12 17:16:39 -0400591 SSL *ssl = (SSL *)OPENSSL_malloc(sizeof(SSL));
David Benjamin0d56f882015-12-19 17:05:56 -0500592 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800593 goto err;
594 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500595 OPENSSL_memset(ssl, 0, sizeof(SSL));
Adam Langley95c29f32014-06-20 12:00:00 -0700596
David Benjaminfc08dfc2017-06-20 14:39:32 -0400597 ssl->conf_min_version = ctx->conf_min_version;
598 ssl->conf_max_version = ctx->conf_max_version;
Steven Valdez52586f92017-07-11 15:08:32 -0400599 ssl->tls13_variant = ctx->tls13_variant;
David Benjamin1eb367c2014-12-12 18:17:51 -0500600
David Benjaminc11ea9422017-08-29 16:33:21 -0400601 // RFC 6347 states that implementations SHOULD use an initial timer value of
602 // 1 second.
Taylor Brandstetter376a0fe2016-05-10 19:30:28 -0700603 ssl->initial_timeout_duration_ms = 1000;
604
David Benjamin0d56f882015-12-19 17:05:56 -0500605 ssl->options = ctx->options;
606 ssl->mode = ctx->mode;
607 ssl->max_cert_list = ctx->max_cert_list;
Adam Langley95c29f32014-06-20 12:00:00 -0700608
David Benjamin0d56f882015-12-19 17:05:56 -0500609 ssl->cert = ssl_cert_dup(ctx->cert);
610 if (ssl->cert == NULL) {
David Benjamina5a3eeb2015-03-18 20:26:30 -0400611 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -0800612 }
Adam Langley95c29f32014-06-20 12:00:00 -0700613
David Benjamin0d56f882015-12-19 17:05:56 -0500614 ssl->msg_callback = ctx->msg_callback;
615 ssl->msg_callback_arg = ctx->msg_callback_arg;
616 ssl->verify_mode = ctx->verify_mode;
David Benjamin0d56f882015-12-19 17:05:56 -0500617 ssl->verify_callback = ctx->default_verify_callback;
David Benjamin3a1dd462017-07-11 16:13:10 -0400618 ssl->custom_verify_callback = ctx->custom_verify_callback;
David Benjaminbbaf3672016-11-17 10:53:09 +0900619 ssl->retain_only_sha256_of_client_certs =
620 ctx->retain_only_sha256_of_client_certs;
Adam Langley95c29f32014-06-20 12:00:00 -0700621
David Benjamin0d56f882015-12-19 17:05:56 -0500622 ssl->quiet_shutdown = ctx->quiet_shutdown;
623 ssl->max_send_fragment = ctx->max_send_fragment;
Adam Langley95c29f32014-06-20 12:00:00 -0700624
David Benjaminf650c712016-12-22 15:54:51 -0500625 SSL_CTX_up_ref(ctx);
David Benjamin0d56f882015-12-19 17:05:56 -0500626 ssl->ctx = ctx;
David Benjaminf650c712016-12-22 15:54:51 -0500627 SSL_CTX_up_ref(ctx);
David Benjaminbe497062017-03-10 16:08:36 -0500628 ssl->session_ctx = ctx;
Adam Langley95c29f32014-06-20 12:00:00 -0700629
Adam Langley2a3b3432017-02-10 13:56:48 -0800630 if (!ssl->ctx->x509_method->ssl_new(ssl)) {
631 goto err;
632 }
633
Steven Valdezce902a92016-05-17 11:47:53 -0400634 if (ctx->supported_group_list) {
David Benjaminf5260812017-07-12 17:16:39 -0400635 ssl->supported_group_list = (uint16_t *)BUF_memdup(
636 ctx->supported_group_list, ctx->supported_group_list_len * 2);
Steven Valdezce902a92016-05-17 11:47:53 -0400637 if (!ssl->supported_group_list) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800638 goto err;
639 }
Steven Valdezce902a92016-05-17 11:47:53 -0400640 ssl->supported_group_list_len = ctx->supported_group_list_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800641 }
Adam Langley95c29f32014-06-20 12:00:00 -0700642
David Benjamin17b30832017-01-28 14:00:32 -0500643 if (ctx->alpn_client_proto_list) {
David Benjaminf5260812017-07-12 17:16:39 -0400644 ssl->alpn_client_proto_list = (uint8_t *)BUF_memdup(
645 ctx->alpn_client_proto_list, ctx->alpn_client_proto_list_len);
David Benjamin0d56f882015-12-19 17:05:56 -0500646 if (ssl->alpn_client_proto_list == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800647 goto err;
648 }
David Benjamin17b30832017-01-28 14:00:32 -0500649 ssl->alpn_client_proto_list_len = ctx->alpn_client_proto_list_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800650 }
Adam Langley95c29f32014-06-20 12:00:00 -0700651
David Benjamin0d56f882015-12-19 17:05:56 -0500652 ssl->method = ctx->method;
Adam Langley95c29f32014-06-20 12:00:00 -0700653
David Benjamin0d56f882015-12-19 17:05:56 -0500654 if (!ssl->method->ssl_new(ssl)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800655 goto err;
656 }
Adam Langley95c29f32014-06-20 12:00:00 -0700657
David Benjamin0d56f882015-12-19 17:05:56 -0500658 ssl->rwstate = SSL_NOTHING;
Adam Langley95c29f32014-06-20 12:00:00 -0700659
David Benjamin0d56f882015-12-19 17:05:56 -0500660 CRYPTO_new_ex_data(&ssl->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700661
David Benjamin0d56f882015-12-19 17:05:56 -0500662 ssl->psk_identity_hint = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800663 if (ctx->psk_identity_hint) {
David Benjamin0d56f882015-12-19 17:05:56 -0500664 ssl->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
665 if (ssl->psk_identity_hint == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800666 goto err;
667 }
668 }
David Benjamin0d56f882015-12-19 17:05:56 -0500669 ssl->psk_client_callback = ctx->psk_client_callback;
670 ssl->psk_server_callback = ctx->psk_server_callback;
Adam Langley95c29f32014-06-20 12:00:00 -0700671
David Benjamin0d56f882015-12-19 17:05:56 -0500672 ssl->tlsext_channel_id_enabled = ctx->tlsext_channel_id_enabled;
David Benjamin02ddbfd2015-01-11 13:09:11 -0500673 if (ctx->tlsext_channel_id_private) {
Adam Langley310d3f62016-07-12 10:39:20 -0700674 EVP_PKEY_up_ref(ctx->tlsext_channel_id_private);
675 ssl->tlsext_channel_id_private = ctx->tlsext_channel_id_private;
David Benjamin02ddbfd2015-01-11 13:09:11 -0500676 }
677
David Benjamin17b30832017-01-28 14:00:32 -0500678 ssl->signed_cert_timestamps_enabled = ctx->signed_cert_timestamps_enabled;
679 ssl->ocsp_stapling_enabled = ctx->ocsp_stapling_enabled;
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200680
David Benjamin0d56f882015-12-19 17:05:56 -0500681 return ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -0800682
Adam Langley95c29f32014-06-20 12:00:00 -0700683err:
David Benjamin0d56f882015-12-19 17:05:56 -0500684 SSL_free(ssl);
David Benjamin3570d732015-06-29 00:28:17 -0400685 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700686
Adam Langleyfcf25832014-12-18 17:42:32 -0800687 return NULL;
688}
Adam Langley95c29f32014-06-20 12:00:00 -0700689
David Benjamin27bbae42015-09-13 00:54:37 -0400690void SSL_free(SSL *ssl) {
691 if (ssl == NULL) {
692 return;
693 }
694
David Benjamin0391f162017-06-15 15:27:59 -0400695 if (ssl->ctx != NULL) {
696 ssl->ctx->x509_method->ssl_free(ssl);
697 }
698
David Benjamin27bbae42015-09-13 00:54:37 -0400699 CRYPTO_free_ex_data(&g_ex_data_class_ssl, ssl, &ssl->ex_data);
700
David Benjamin27bbae42015-09-13 00:54:37 -0400701 BIO_free_all(ssl->rbio);
David Benjaminf715c422016-06-11 19:01:56 -0400702 BIO_free_all(ssl->wbio);
David Benjamin27bbae42015-09-13 00:54:37 -0400703
704 BUF_MEM_free(ssl->init_buf);
705
David Benjaminc11ea9422017-08-29 16:33:21 -0400706 // add extra stuff
David Benjamin27bbae42015-09-13 00:54:37 -0400707 ssl_cipher_preference_list_free(ssl->cipher_list);
David Benjamin27bbae42015-09-13 00:54:37 -0400708
David Benjamin27bbae42015-09-13 00:54:37 -0400709 SSL_SESSION_free(ssl->session);
710
David Benjamin27bbae42015-09-13 00:54:37 -0400711 ssl_cert_free(ssl->cert);
712
713 OPENSSL_free(ssl->tlsext_hostname);
David Benjaminbe497062017-03-10 16:08:36 -0500714 SSL_CTX_free(ssl->session_ctx);
Steven Valdezce902a92016-05-17 11:47:53 -0400715 OPENSSL_free(ssl->supported_group_list);
David Benjamin27bbae42015-09-13 00:54:37 -0400716 OPENSSL_free(ssl->alpn_client_proto_list);
717 EVP_PKEY_free(ssl->tlsext_channel_id_private);
718 OPENSSL_free(ssl->psk_identity_hint);
Adam Langley34b4c822017-02-02 10:57:17 -0800719 sk_CRYPTO_BUFFER_pop_free(ssl->client_CA, CRYPTO_BUFFER_free);
David Benjamin27bbae42015-09-13 00:54:37 -0400720 sk_SRTP_PROTECTION_PROFILE_free(ssl->srtp_profiles);
721
722 if (ssl->method != NULL) {
723 ssl->method->ssl_free(ssl);
724 }
725 SSL_CTX_free(ssl->ctx);
726
727 OPENSSL_free(ssl);
728}
729
730void SSL_set_connect_state(SSL *ssl) {
731 ssl->server = 0;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400732 ssl->do_handshake = ssl_client_handshake;
David Benjamin27bbae42015-09-13 00:54:37 -0400733}
734
735void SSL_set_accept_state(SSL *ssl) {
736 ssl->server = 1;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400737 ssl->do_handshake = ssl_server_handshake;
David Benjamin27bbae42015-09-13 00:54:37 -0400738}
739
David Benjamin4501bd52016-08-01 13:39:41 -0400740void SSL_set0_rbio(SSL *ssl, BIO *rbio) {
David Benjaminf715c422016-06-11 19:01:56 -0400741 BIO_free_all(ssl->rbio);
742 ssl->rbio = rbio;
743}
744
David Benjamin4501bd52016-08-01 13:39:41 -0400745void SSL_set0_wbio(SSL *ssl, BIO *wbio) {
David Benjaminf715c422016-06-11 19:01:56 -0400746 BIO_free_all(ssl->wbio);
David Benjamin27bbae42015-09-13 00:54:37 -0400747 ssl->wbio = wbio;
748}
749
David Benjaminf715c422016-06-11 19:01:56 -0400750void SSL_set_bio(SSL *ssl, BIO *rbio, BIO *wbio) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400751 // For historical reasons, this function has many different cases in ownership
752 // handling.
David Benjaminf715c422016-06-11 19:01:56 -0400753
David Benjaminc11ea9422017-08-29 16:33:21 -0400754 // If nothing has changed, do nothing
David Benjamin4501bd52016-08-01 13:39:41 -0400755 if (rbio == SSL_get_rbio(ssl) && wbio == SSL_get_wbio(ssl)) {
756 return;
757 }
758
David Benjaminc11ea9422017-08-29 16:33:21 -0400759 // If the two arguments are equal, one fewer reference is granted than
760 // taken.
David Benjaminf715c422016-06-11 19:01:56 -0400761 if (rbio != NULL && rbio == wbio) {
762 BIO_up_ref(rbio);
763 }
764
David Benjaminc11ea9422017-08-29 16:33:21 -0400765 // If only the wbio is changed, adopt only one reference.
David Benjaminf715c422016-06-11 19:01:56 -0400766 if (rbio == SSL_get_rbio(ssl)) {
David Benjamin4501bd52016-08-01 13:39:41 -0400767 SSL_set0_wbio(ssl, wbio);
David Benjaminf715c422016-06-11 19:01:56 -0400768 return;
769 }
David Benjamin4501bd52016-08-01 13:39:41 -0400770
David Benjaminc11ea9422017-08-29 16:33:21 -0400771 // There is an asymmetry here for historical reasons. If only the rbio is
772 // changed AND the rbio and wbio were originally different, then we only adopt
773 // one reference.
David Benjamin4501bd52016-08-01 13:39:41 -0400774 if (wbio == SSL_get_wbio(ssl) && SSL_get_rbio(ssl) != SSL_get_wbio(ssl)) {
775 SSL_set0_rbio(ssl, rbio);
David Benjaminf715c422016-06-11 19:01:56 -0400776 return;
777 }
778
David Benjaminc11ea9422017-08-29 16:33:21 -0400779 // Otherwise, adopt both references.
David Benjamin4501bd52016-08-01 13:39:41 -0400780 SSL_set0_rbio(ssl, rbio);
781 SSL_set0_wbio(ssl, wbio);
David Benjaminf715c422016-06-11 19:01:56 -0400782}
783
David Benjamin27bbae42015-09-13 00:54:37 -0400784BIO *SSL_get_rbio(const SSL *ssl) { return ssl->rbio; }
785
David Benjamindaf207a2017-01-03 18:37:41 -0500786BIO *SSL_get_wbio(const SSL *ssl) { return ssl->wbio; }
David Benjamin27bbae42015-09-13 00:54:37 -0400787
David Benjamin2be4aa72017-01-02 07:56:51 -0500788int SSL_do_handshake(SSL *ssl) {
789 ssl_reset_error_state(ssl);
David Benjamin15c14882016-03-14 14:25:46 -0400790
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400791 if (ssl->do_handshake == NULL) {
David Benjamin27bbae42015-09-13 00:54:37 -0400792 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_TYPE_NOT_SET);
793 return -1;
794 }
795
796 if (!SSL_in_init(ssl)) {
797 return 1;
798 }
799
David Benjaminc11ea9422017-08-29 16:33:21 -0400800 // Run the handshake.
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400801 SSL_HANDSHAKE *hs = ssl->s3->hs;
802
David Benjamin74795b32017-08-31 15:13:12 -0400803 bool early_return = false;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400804 int ret = ssl_run_handshake(hs, &early_return);
805 ssl_do_info_callback(
806 ssl, ssl->server ? SSL_CB_ACCEPT_EXIT : SSL_CB_CONNECT_EXIT, ret);
David Benjamince8c9d22016-11-14 10:45:16 +0900807 if (ret <= 0) {
808 return ret;
809 }
810
David Benjaminc11ea9422017-08-29 16:33:21 -0400811 // Destroy the handshake object if the handshake has completely finished.
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400812 if (!early_return) {
David Benjamince8c9d22016-11-14 10:45:16 +0900813 ssl_handshake_free(ssl->s3->hs);
814 ssl->s3->hs = NULL;
815 }
816
817 return 1;
David Benjamin27bbae42015-09-13 00:54:37 -0400818}
819
820int SSL_connect(SSL *ssl) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400821 if (ssl->do_handshake == NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400822 // Not properly initialized yet
David Benjamin27bbae42015-09-13 00:54:37 -0400823 SSL_set_connect_state(ssl);
824 }
825
David Benjamin15c14882016-03-14 14:25:46 -0400826 return SSL_do_handshake(ssl);
David Benjamin27bbae42015-09-13 00:54:37 -0400827}
828
829int SSL_accept(SSL *ssl) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400830 if (ssl->do_handshake == NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400831 // Not properly initialized yet
David Benjamin27bbae42015-09-13 00:54:37 -0400832 SSL_set_accept_state(ssl);
833 }
834
David Benjamin15c14882016-03-14 14:25:46 -0400835 return SSL_do_handshake(ssl);
David Benjamin27bbae42015-09-13 00:54:37 -0400836}
837
David Benjamin7934f082017-08-01 16:32:25 -0400838static int ssl_do_post_handshake(SSL *ssl, const SSLMessage &msg) {
839 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
840 return tls13_post_handshake(ssl, msg);
841 }
842
David Benjaminc11ea9422017-08-29 16:33:21 -0400843 // We do not accept renegotiations as a server or SSL 3.0. SSL 3.0 will be
844 // removed entirely in the future and requires retaining more data for
845 // renegotiation_info.
David Benjamin34941c02016-10-08 11:45:31 -0400846 if (ssl->server || ssl->version == SSL3_VERSION) {
David Benjamin163f29a2016-07-28 11:05:58 -0400847 goto no_renegotiation;
848 }
849
David Benjamin7934f082017-08-01 16:32:25 -0400850 if (msg.type != SSL3_MT_HELLO_REQUEST || CBS_len(&msg.body) != 0) {
David Benjamin163f29a2016-07-28 11:05:58 -0400851 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
852 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HELLO_REQUEST);
853 return 0;
854 }
855
856 switch (ssl->renegotiate_mode) {
857 case ssl_renegotiate_ignore:
David Benjaminc11ea9422017-08-29 16:33:21 -0400858 // Ignore the HelloRequest.
David Benjamin163f29a2016-07-28 11:05:58 -0400859 return 1;
860
861 case ssl_renegotiate_once:
862 if (ssl->s3->total_renegotiations != 0) {
863 goto no_renegotiation;
864 }
865 break;
866
867 case ssl_renegotiate_never:
868 goto no_renegotiation;
869
870 case ssl_renegotiate_freely:
871 break;
872 }
873
David Benjaminc11ea9422017-08-29 16:33:21 -0400874 // Renegotiation is only supported at quiescent points in the application
875 // protocol, namely in HTTPS, just before reading the HTTP response. Require
876 // the record-layer be idle and avoid complexities of sending a handshake
877 // record while an application_data record is being written.
David Benjamin163f29a2016-07-28 11:05:58 -0400878 if (ssl_write_buffer_is_pending(ssl)) {
879 goto no_renegotiation;
880 }
881
David Benjaminc11ea9422017-08-29 16:33:21 -0400882 // Begin a new handshake.
David Benjamin2644a132016-12-11 13:41:17 -0500883 if (ssl->s3->hs != NULL) {
884 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
885 return 0;
886 }
887 ssl->s3->hs = ssl_handshake_new(ssl);
888 if (ssl->s3->hs == NULL) {
889 return 0;
890 }
891
David Benjamin163f29a2016-07-28 11:05:58 -0400892 ssl->s3->total_renegotiations++;
David Benjamin163f29a2016-07-28 11:05:58 -0400893 return 1;
894
895no_renegotiation:
896 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_NO_RENEGOTIATION);
897 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION);
898 return 0;
899}
900
David Benjamin15c14882016-03-14 14:25:46 -0400901static int ssl_read_impl(SSL *ssl, void *buf, int num, int peek) {
David Benjamin2be4aa72017-01-02 07:56:51 -0500902 ssl_reset_error_state(ssl);
David Benjamin15c14882016-03-14 14:25:46 -0400903
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400904 if (ssl->do_handshake == NULL) {
David Benjamin27bbae42015-09-13 00:54:37 -0400905 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
906 return -1;
907 }
908
David Benjamin163f29a2016-07-28 11:05:58 -0400909 for (;;) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400910 // Complete the current handshake, if any. False Start will cause
911 // |SSL_do_handshake| to return mid-handshake, so this may require multiple
912 // iterations.
Steven Valdez681eb6a2016-12-19 13:19:29 -0500913 while (!ssl_can_read(ssl)) {
David Benjamin163f29a2016-07-28 11:05:58 -0400914 int ret = SSL_do_handshake(ssl);
915 if (ret < 0) {
916 return ret;
917 }
918 if (ret == 0) {
919 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
920 return -1;
921 }
922 }
923
David Benjamin74795b32017-08-31 15:13:12 -0400924 bool got_handshake = false;
David Benjaminf5260812017-07-12 17:16:39 -0400925 int ret = ssl->method->read_app_data(ssl, &got_handshake, (uint8_t *)buf,
926 num, peek);
David Benjamin163f29a2016-07-28 11:05:58 -0400927 if (ret > 0 || !got_handshake) {
Steven Valdez32635b82016-08-16 11:25:03 -0400928 ssl->s3->key_update_count = 0;
David Benjaminc79845c2016-03-10 01:28:00 -0500929 return ret;
930 }
David Benjamin163f29a2016-07-28 11:05:58 -0400931
David Benjaminc11ea9422017-08-29 16:33:21 -0400932 // If we received an interrupt in early read (the end_of_early_data alert),
933 // loop again for the handshake to process it.
Steven Valdez681eb6a2016-12-19 13:19:29 -0500934 if (SSL_in_init(ssl)) {
935 continue;
936 }
937
David Benjamin7934f082017-08-01 16:32:25 -0400938 SSLMessage msg;
939 while (ssl->method->get_message(ssl, &msg)) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400940 // Handle the post-handshake message and try again.
David Benjamin7934f082017-08-01 16:32:25 -0400941 if (!ssl_do_post_handshake(ssl, msg)) {
942 return -1;
943 }
944 ssl->method->next_message(ssl);
David Benjaminc79845c2016-03-10 01:28:00 -0500945 }
946 }
David Benjamin15c14882016-03-14 14:25:46 -0400947}
948
949int SSL_read(SSL *ssl, void *buf, int num) {
950 return ssl_read_impl(ssl, buf, num, 0 /* consume bytes */);
David Benjamin27bbae42015-09-13 00:54:37 -0400951}
952
953int SSL_peek(SSL *ssl, void *buf, int num) {
David Benjamin15c14882016-03-14 14:25:46 -0400954 return ssl_read_impl(ssl, buf, num, 1 /* peek */);
David Benjamin27bbae42015-09-13 00:54:37 -0400955}
956
957int SSL_write(SSL *ssl, const void *buf, int num) {
David Benjamin2be4aa72017-01-02 07:56:51 -0500958 ssl_reset_error_state(ssl);
David Benjamin15c14882016-03-14 14:25:46 -0400959
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400960 if (ssl->do_handshake == NULL) {
David Benjamin27bbae42015-09-13 00:54:37 -0400961 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
962 return -1;
963 }
964
David Benjaminfa214e42016-05-10 17:03:10 -0400965 if (ssl->s3->send_shutdown != ssl_shutdown_none) {
David Benjamin27bbae42015-09-13 00:54:37 -0400966 OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
967 return -1;
968 }
969
David Benjamin74795b32017-08-31 15:13:12 -0400970 int ret = 0;
971 bool needs_handshake = false;
Steven Valdeze831a812017-03-09 14:56:07 -0500972 do {
David Benjaminc11ea9422017-08-29 16:33:21 -0400973 // If necessary, complete the handshake implicitly.
Steven Valdeze831a812017-03-09 14:56:07 -0500974 if (!ssl_can_write(ssl)) {
975 ret = SSL_do_handshake(ssl);
976 if (ret < 0) {
977 return ret;
978 }
979 if (ret == 0) {
980 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
981 return -1;
982 }
David Benjamind7ac1432016-03-10 00:41:25 -0500983 }
David Benjamind7ac1432016-03-10 00:41:25 -0500984
David Benjaminf5260812017-07-12 17:16:39 -0400985 ret = ssl->method->write_app_data(ssl, &needs_handshake,
986 (const uint8_t *)buf, num);
Steven Valdeze831a812017-03-09 14:56:07 -0500987 } while (needs_handshake);
988 return ret;
David Benjamin27bbae42015-09-13 00:54:37 -0400989}
990
991int SSL_shutdown(SSL *ssl) {
David Benjamin2be4aa72017-01-02 07:56:51 -0500992 ssl_reset_error_state(ssl);
David Benjamin15c14882016-03-14 14:25:46 -0400993
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400994 if (ssl->do_handshake == NULL) {
David Benjamin27bbae42015-09-13 00:54:37 -0400995 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
996 return -1;
997 }
998
David Benjaminc11ea9422017-08-29 16:33:21 -0400999 // If we are in the middle of a handshake, silently succeed. Consumers often
1000 // call this function before |SSL_free|, whether the handshake succeeded or
1001 // not. We assume the caller has already handled failed handshakes.
Steven Valdeza14934f2016-02-29 10:05:08 -05001002 if (SSL_in_init(ssl)) {
David Benjamin12709db2017-02-17 13:51:50 -05001003 return 1;
Steven Valdeza14934f2016-02-29 10:05:08 -05001004 }
1005
David Benjamin27bbae42015-09-13 00:54:37 -04001006 if (ssl->quiet_shutdown) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001007 // Do nothing if configured not to send a close_notify.
David Benjaminfa214e42016-05-10 17:03:10 -04001008 ssl->s3->send_shutdown = ssl_shutdown_close_notify;
1009 ssl->s3->recv_shutdown = ssl_shutdown_close_notify;
David Benjamin27bbae42015-09-13 00:54:37 -04001010 return 1;
1011 }
1012
David Benjaminc11ea9422017-08-29 16:33:21 -04001013 // This function completes in two stages. It sends a close_notify and then it
1014 // waits for a close_notify to come in. Perform exactly one action and return
1015 // whether or not it succeeds.
David Benjamin27bbae42015-09-13 00:54:37 -04001016
David Benjaminfa214e42016-05-10 17:03:10 -04001017 if (ssl->s3->send_shutdown != ssl_shutdown_close_notify) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001018 // Send a close_notify.
David Benjaminfa214e42016-05-10 17:03:10 -04001019 if (ssl3_send_alert(ssl, SSL3_AL_WARNING, SSL_AD_CLOSE_NOTIFY) <= 0) {
1020 return -1;
David Benjamin27bbae42015-09-13 00:54:37 -04001021 }
1022 } else if (ssl->s3->alert_dispatch) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001023 // Finish sending the close_notify.
David Benjaminf0ee9072016-06-15 17:44:37 -04001024 if (ssl->method->dispatch_alert(ssl) <= 0) {
David Benjaminfa214e42016-05-10 17:03:10 -04001025 return -1;
David Benjamin27bbae42015-09-13 00:54:37 -04001026 }
David Benjaminfa214e42016-05-10 17:03:10 -04001027 } else if (ssl->s3->recv_shutdown != ssl_shutdown_close_notify) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001028 // Wait for the peer's close_notify.
David Benjaminf0ee9072016-06-15 17:44:37 -04001029 ssl->method->read_close_notify(ssl);
David Benjaminfa214e42016-05-10 17:03:10 -04001030 if (ssl->s3->recv_shutdown != ssl_shutdown_close_notify) {
1031 return -1;
David Benjamin27bbae42015-09-13 00:54:37 -04001032 }
1033 }
1034
David Benjaminc11ea9422017-08-29 16:33:21 -04001035 // Return 0 for unidirectional shutdown and 1 for bidirectional shutdown.
David Benjaminfa214e42016-05-10 17:03:10 -04001036 return ssl->s3->recv_shutdown == ssl_shutdown_close_notify;
David Benjamin27bbae42015-09-13 00:54:37 -04001037}
1038
David Benjamin1d4f4c02016-07-26 18:03:08 -04001039int SSL_send_fatal_alert(SSL *ssl, uint8_t alert) {
1040 if (ssl->s3->alert_dispatch) {
1041 if (ssl->s3->send_alert[0] != SSL3_AL_FATAL ||
1042 ssl->s3->send_alert[1] != alert) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001043 // We are already attempting to write a different alert.
David Benjamin1d4f4c02016-07-26 18:03:08 -04001044 OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
1045 return -1;
1046 }
1047 return ssl->method->dispatch_alert(ssl);
1048 }
1049
1050 return ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
1051}
1052
Steven Valdez08b65f42016-12-07 15:29:45 -05001053void SSL_CTX_set_early_data_enabled(SSL_CTX *ctx, int enabled) {
Alessandro Ghedini67bb45f2017-03-30 16:33:24 -05001054 ctx->cert->enable_early_data = !!enabled;
1055}
1056
Steven Valdez520e1222017-06-13 12:45:25 -04001057void SSL_CTX_set_tls13_variant(SSL_CTX *ctx, enum tls13_variant_t variant) {
1058 ctx->tls13_variant = variant;
1059}
1060
Steven Valdez52586f92017-07-11 15:08:32 -04001061void SSL_set_tls13_variant(SSL *ssl, enum tls13_variant_t variant) {
1062 ssl->tls13_variant = variant;
1063}
1064
Alessandro Ghedini67bb45f2017-03-30 16:33:24 -05001065void SSL_set_early_data_enabled(SSL *ssl, int enabled) {
1066 ssl->cert->enable_early_data = !!enabled;
Steven Valdez08b65f42016-12-07 15:29:45 -05001067}
1068
Steven Valdeze831a812017-03-09 14:56:07 -05001069int SSL_in_early_data(const SSL *ssl) {
1070 if (ssl->s3->hs == NULL) {
1071 return 0;
1072 }
1073 return ssl->s3->hs->in_early_data;
1074}
1075
Steven Valdez2d850622017-01-11 11:34:52 -05001076int SSL_early_data_accepted(const SSL *ssl) {
1077 return ssl->early_data_accepted;
1078}
1079
Steven Valdeze831a812017-03-09 14:56:07 -05001080void SSL_reset_early_data_reject(SSL *ssl) {
1081 SSL_HANDSHAKE *hs = ssl->s3->hs;
1082 if (hs == NULL ||
1083 hs->wait != ssl_hs_early_data_rejected) {
1084 abort();
1085 }
1086
1087 hs->wait = ssl_hs_ok;
David Benjaminfd45ee72017-08-31 14:49:09 -04001088 hs->in_early_data = false;
David Benjamin31b0c9b2017-07-20 14:49:15 -04001089 hs->early_session.reset();
Steven Valdeze831a812017-03-09 14:56:07 -05001090
David Benjaminc11ea9422017-08-29 16:33:21 -04001091 // Discard any unfinished writes from the perspective of |SSL_write|'s
1092 // retry. The handshake will transparently flush out the pending record
1093 // (discarded by the server) to keep the framing correct.
David Benjamin046bc1f2017-08-31 15:06:42 -04001094 ssl->s3->wpend_pending = false;
Steven Valdeze831a812017-03-09 14:56:07 -05001095}
1096
David Benjamin287fc4f2016-12-10 15:12:03 -05001097static int bio_retry_reason_to_error(int reason) {
1098 switch (reason) {
1099 case BIO_RR_CONNECT:
1100 return SSL_ERROR_WANT_CONNECT;
1101 case BIO_RR_ACCEPT:
1102 return SSL_ERROR_WANT_ACCEPT;
1103 default:
1104 return SSL_ERROR_SYSCALL;
1105 }
1106}
David Benjamin27bbae42015-09-13 00:54:37 -04001107
David Benjamin287fc4f2016-12-10 15:12:03 -05001108int SSL_get_error(const SSL *ssl, int ret_code) {
David Benjamin27bbae42015-09-13 00:54:37 -04001109 if (ret_code > 0) {
1110 return SSL_ERROR_NONE;
1111 }
1112
David Benjaminc11ea9422017-08-29 16:33:21 -04001113 // Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
1114 // where we do encode the error
David Benjamin287fc4f2016-12-10 15:12:03 -05001115 uint32_t err = ERR_peek_error();
David Benjamin27bbae42015-09-13 00:54:37 -04001116 if (err != 0) {
1117 if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
1118 return SSL_ERROR_SYSCALL;
1119 }
1120 return SSL_ERROR_SSL;
1121 }
1122
1123 if (ret_code == 0) {
David Benjaminfa214e42016-05-10 17:03:10 -04001124 if (ssl->s3->recv_shutdown == ssl_shutdown_close_notify) {
David Benjamin27bbae42015-09-13 00:54:37 -04001125 return SSL_ERROR_ZERO_RETURN;
1126 }
David Benjaminc11ea9422017-08-29 16:33:21 -04001127 // An EOF was observed which violates the protocol, and the underlying
1128 // transport does not participate in the error queue. Bubble up to the
1129 // caller.
David Benjamin27bbae42015-09-13 00:54:37 -04001130 return SSL_ERROR_SYSCALL;
1131 }
1132
David Benjamin287fc4f2016-12-10 15:12:03 -05001133 switch (ssl->rwstate) {
1134 case SSL_PENDING_SESSION:
1135 return SSL_ERROR_PENDING_SESSION;
David Benjamin27bbae42015-09-13 00:54:37 -04001136
David Benjamin287fc4f2016-12-10 15:12:03 -05001137 case SSL_CERTIFICATE_SELECTION_PENDING:
1138 return SSL_ERROR_PENDING_CERTIFICATE;
David Benjamin27bbae42015-09-13 00:54:37 -04001139
David Benjamin287fc4f2016-12-10 15:12:03 -05001140 case SSL_READING: {
1141 BIO *bio = SSL_get_rbio(ssl);
1142 if (BIO_should_read(bio)) {
1143 return SSL_ERROR_WANT_READ;
David Benjamin27bbae42015-09-13 00:54:37 -04001144 }
1145
David Benjamin287fc4f2016-12-10 15:12:03 -05001146 if (BIO_should_write(bio)) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001147 // TODO(davidben): OpenSSL historically checked for writes on the read
1148 // BIO. Can this be removed?
David Benjamin287fc4f2016-12-10 15:12:03 -05001149 return SSL_ERROR_WANT_WRITE;
David Benjamin27bbae42015-09-13 00:54:37 -04001150 }
1151
David Benjamin287fc4f2016-12-10 15:12:03 -05001152 if (BIO_should_io_special(bio)) {
1153 return bio_retry_reason_to_error(BIO_get_retry_reason(bio));
David Benjamin27bbae42015-09-13 00:54:37 -04001154 }
1155
David Benjamin287fc4f2016-12-10 15:12:03 -05001156 break;
1157 }
1158
1159 case SSL_WRITING: {
1160 BIO *bio = SSL_get_wbio(ssl);
1161 if (BIO_should_write(bio)) {
1162 return SSL_ERROR_WANT_WRITE;
David Benjamin27bbae42015-09-13 00:54:37 -04001163 }
1164
David Benjamin287fc4f2016-12-10 15:12:03 -05001165 if (BIO_should_read(bio)) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001166 // TODO(davidben): OpenSSL historically checked for reads on the write
1167 // BIO. Can this be removed?
David Benjamin287fc4f2016-12-10 15:12:03 -05001168 return SSL_ERROR_WANT_READ;
1169 }
1170
1171 if (BIO_should_io_special(bio)) {
1172 return bio_retry_reason_to_error(BIO_get_retry_reason(bio));
1173 }
1174
1175 break;
David Benjamin27bbae42015-09-13 00:54:37 -04001176 }
David Benjamin27bbae42015-09-13 00:54:37 -04001177
David Benjamin287fc4f2016-12-10 15:12:03 -05001178 case SSL_X509_LOOKUP:
1179 return SSL_ERROR_WANT_X509_LOOKUP;
David Benjamin27bbae42015-09-13 00:54:37 -04001180
David Benjamin287fc4f2016-12-10 15:12:03 -05001181 case SSL_CHANNEL_ID_LOOKUP:
1182 return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
David Benjamin27bbae42015-09-13 00:54:37 -04001183
David Benjamin287fc4f2016-12-10 15:12:03 -05001184 case SSL_PRIVATE_KEY_OPERATION:
1185 return SSL_ERROR_WANT_PRIVATE_KEY_OPERATION;
Adam Langley4c341d02017-03-08 19:33:21 -08001186
1187 case SSL_PENDING_TICKET:
1188 return SSL_ERROR_PENDING_TICKET;
Steven Valdeze831a812017-03-09 14:56:07 -05001189
1190 case SSL_EARLY_DATA_REJECTED:
1191 return SSL_ERROR_EARLY_DATA_REJECTED;
David Benjamin3a1dd462017-07-11 16:13:10 -04001192
1193 case SSL_CERTIFICATE_VERIFY:
1194 return SSL_ERROR_WANT_CERTIFICATE_VERIFY;
David Benjamin27bbae42015-09-13 00:54:37 -04001195 }
1196
1197 return SSL_ERROR_SYSCALL;
1198}
1199
David Benjamin27bbae42015-09-13 00:54:37 -04001200uint32_t SSL_CTX_set_options(SSL_CTX *ctx, uint32_t options) {
1201 ctx->options |= options;
1202 return ctx->options;
1203}
1204
1205uint32_t SSL_CTX_clear_options(SSL_CTX *ctx, uint32_t options) {
1206 ctx->options &= ~options;
1207 return ctx->options;
1208}
1209
1210uint32_t SSL_CTX_get_options(const SSL_CTX *ctx) { return ctx->options; }
1211
1212uint32_t SSL_set_options(SSL *ssl, uint32_t options) {
1213 ssl->options |= options;
1214 return ssl->options;
1215}
1216
1217uint32_t SSL_clear_options(SSL *ssl, uint32_t options) {
1218 ssl->options &= ~options;
1219 return ssl->options;
1220}
1221
1222uint32_t SSL_get_options(const SSL *ssl) { return ssl->options; }
1223
1224uint32_t SSL_CTX_set_mode(SSL_CTX *ctx, uint32_t mode) {
1225 ctx->mode |= mode;
1226 return ctx->mode;
1227}
1228
1229uint32_t SSL_CTX_clear_mode(SSL_CTX *ctx, uint32_t mode) {
1230 ctx->mode &= ~mode;
1231 return ctx->mode;
1232}
1233
1234uint32_t SSL_CTX_get_mode(const SSL_CTX *ctx) { return ctx->mode; }
1235
1236uint32_t SSL_set_mode(SSL *ssl, uint32_t mode) {
1237 ssl->mode |= mode;
1238 return ssl->mode;
1239}
1240
1241uint32_t SSL_clear_mode(SSL *ssl, uint32_t mode) {
1242 ssl->mode &= ~mode;
1243 return ssl->mode;
1244}
1245
1246uint32_t SSL_get_mode(const SSL *ssl) { return ssl->mode; }
1247
Adam Langleyd519bf62016-12-12 11:16:44 -08001248void SSL_CTX_set0_buffer_pool(SSL_CTX *ctx, CRYPTO_BUFFER_POOL *pool) {
1249 ctx->pool = pool;
1250}
1251
David Benjaminee0c8272015-09-13 01:03:54 -04001252int SSL_get_tls_unique(const SSL *ssl, uint8_t *out, size_t *out_len,
1253 size_t max_out) {
David Benjaminf5260812017-07-12 17:16:39 -04001254 *out_len = 0;
1255 OPENSSL_memset(out, 0, max_out);
1256
David Benjaminc11ea9422017-08-29 16:33:21 -04001257 // tls-unique is not defined for SSL 3.0 or TLS 1.3.
David Benjaminced00b42016-10-08 11:24:09 -04001258 if (!ssl->s3->initial_handshake_complete ||
1259 ssl3_protocol_version(ssl) < TLS1_VERSION ||
1260 ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjaminf5260812017-07-12 17:16:39 -04001261 return 0;
David Benjaminced00b42016-10-08 11:24:09 -04001262 }
1263
David Benjaminc11ea9422017-08-29 16:33:21 -04001264 // The tls-unique value is the first Finished message in the handshake, which
1265 // is the client's in a full handshake and the server's for a resumption. See
1266 // https://tools.ietf.org/html/rfc5929#section-3.1.
David Benjaminee0c8272015-09-13 01:03:54 -04001267 const uint8_t *finished = ssl->s3->previous_client_finished;
1268 size_t finished_len = ssl->s3->previous_client_finished_len;
Steven Valdez87eab492016-06-27 16:34:59 -04001269 if (ssl->session != NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001270 // tls-unique is broken for resumed sessions unless EMS is used.
David Benjaminee0c8272015-09-13 01:03:54 -04001271 if (!ssl->session->extended_master_secret) {
David Benjaminf5260812017-07-12 17:16:39 -04001272 return 0;
David Benjaminee0c8272015-09-13 01:03:54 -04001273 }
1274 finished = ssl->s3->previous_server_finished;
1275 finished_len = ssl->s3->previous_server_finished_len;
1276 }
1277
David Benjaminee0c8272015-09-13 01:03:54 -04001278 *out_len = finished_len;
1279 if (finished_len > max_out) {
1280 *out_len = max_out;
1281 }
1282
David Benjamin17cf2cb2016-12-13 01:07:13 -05001283 OPENSSL_memcpy(out, finished, *out_len);
David Benjaminee0c8272015-09-13 01:03:54 -04001284 return 1;
David Benjaminee0c8272015-09-13 01:03:54 -04001285}
1286
David Benjamin5960a902017-02-14 20:07:11 -05001287static int set_session_id_context(CERT *cert, const uint8_t *sid_ctx,
David Benjamin30c4c302016-12-07 22:35:24 -05001288 size_t sid_ctx_len) {
David Benjamin5960a902017-02-14 20:07:11 -05001289 if (sid_ctx_len > sizeof(cert->sid_ctx)) {
David Benjamin3570d732015-06-29 00:28:17 -04001290 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -08001291 return 0;
1292 }
David Benjamin30c4c302016-12-07 22:35:24 -05001293
David Benjamina3d76d02017-07-14 19:36:07 -04001294 static_assert(sizeof(cert->sid_ctx) < 256, "sid_ctx too large");
David Benjamin5960a902017-02-14 20:07:11 -05001295 cert->sid_ctx_length = (uint8_t)sid_ctx_len;
1296 OPENSSL_memcpy(cert->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001297 return 1;
1298}
Adam Langley95c29f32014-06-20 12:00:00 -07001299
David Benjamin5960a902017-02-14 20:07:11 -05001300int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
1301 size_t sid_ctx_len) {
1302 return set_session_id_context(ctx->cert, sid_ctx, sid_ctx_len);
1303}
1304
Adam Langleyfcf25832014-12-18 17:42:32 -08001305int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
David Benjamin30c4c302016-12-07 22:35:24 -05001306 size_t sid_ctx_len) {
David Benjamin5960a902017-02-14 20:07:11 -05001307 return set_session_id_context(ssl->cert, sid_ctx, sid_ctx_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001308}
Adam Langley95c29f32014-06-20 12:00:00 -07001309
David Benjamin07820b52016-12-07 21:13:41 -05001310const uint8_t *SSL_get0_session_id_context(const SSL *ssl, size_t *out_len) {
David Benjamin5960a902017-02-14 20:07:11 -05001311 *out_len = ssl->cert->sid_ctx_length;
1312 return ssl->cert->sid_ctx;
David Benjamin07820b52016-12-07 21:13:41 -05001313}
1314
David Benjamin7481d392015-07-05 19:38:46 -04001315void SSL_certs_clear(SSL *ssl) { ssl_cert_clear_certs(ssl->cert); }
Adam Langley95c29f32014-06-20 12:00:00 -07001316
David Benjamin066fe0a2015-10-17 21:11:33 -04001317int SSL_get_fd(const SSL *ssl) { return SSL_get_rfd(ssl); }
Adam Langley95c29f32014-06-20 12:00:00 -07001318
David Benjamin066fe0a2015-10-17 21:11:33 -04001319int SSL_get_rfd(const SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001320 int ret = -1;
David Benjamin066fe0a2015-10-17 21:11:33 -04001321 BIO *b = BIO_find_type(SSL_get_rbio(ssl), BIO_TYPE_DESCRIPTOR);
1322 if (b != NULL) {
1323 BIO_get_fd(b, &ret);
Adam Langleyfcf25832014-12-18 17:42:32 -08001324 }
1325 return ret;
1326}
Adam Langley95c29f32014-06-20 12:00:00 -07001327
David Benjamin066fe0a2015-10-17 21:11:33 -04001328int SSL_get_wfd(const SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001329 int ret = -1;
David Benjamin066fe0a2015-10-17 21:11:33 -04001330 BIO *b = BIO_find_type(SSL_get_wbio(ssl), BIO_TYPE_DESCRIPTOR);
1331 if (b != NULL) {
1332 BIO_get_fd(b, &ret);
Adam Langleyfcf25832014-12-18 17:42:32 -08001333 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001334 return ret;
1335}
Adam Langley95c29f32014-06-20 12:00:00 -07001336
David Benjamin066fe0a2015-10-17 21:11:33 -04001337int SSL_set_fd(SSL *ssl, int fd) {
David Benjamin22ce9b22016-03-06 19:26:52 -05001338 BIO *bio = BIO_new(BIO_s_socket());
Adam Langleyfcf25832014-12-18 17:42:32 -08001339 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001340 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
David Benjamin066fe0a2015-10-17 21:11:33 -04001341 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001342 }
1343 BIO_set_fd(bio, fd, BIO_NOCLOSE);
David Benjamin066fe0a2015-10-17 21:11:33 -04001344 SSL_set_bio(ssl, bio, bio);
1345 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001346}
Adam Langley95c29f32014-06-20 12:00:00 -07001347
David Benjamin066fe0a2015-10-17 21:11:33 -04001348int SSL_set_wfd(SSL *ssl, int fd) {
David Benjamin2f871122016-05-20 14:27:17 -04001349 BIO *rbio = SSL_get_rbio(ssl);
1350 if (rbio == NULL || BIO_method_type(rbio) != BIO_TYPE_SOCKET ||
1351 BIO_get_fd(rbio, NULL) != fd) {
David Benjamin22ce9b22016-03-06 19:26:52 -05001352 BIO *bio = BIO_new(BIO_s_socket());
Adam Langleyfcf25832014-12-18 17:42:32 -08001353 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001354 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
David Benjamin066fe0a2015-10-17 21:11:33 -04001355 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001356 }
1357 BIO_set_fd(bio, fd, BIO_NOCLOSE);
David Benjamin4501bd52016-08-01 13:39:41 -04001358 SSL_set0_wbio(ssl, bio);
Adam Langleyfcf25832014-12-18 17:42:32 -08001359 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -04001360 // Copy the rbio over to the wbio.
David Benjamin4501bd52016-08-01 13:39:41 -04001361 BIO_up_ref(rbio);
1362 SSL_set0_wbio(ssl, rbio);
Adam Langleyfcf25832014-12-18 17:42:32 -08001363 }
1364
David Benjamin066fe0a2015-10-17 21:11:33 -04001365 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001366}
Adam Langley95c29f32014-06-20 12:00:00 -07001367
David Benjamin066fe0a2015-10-17 21:11:33 -04001368int SSL_set_rfd(SSL *ssl, int fd) {
David Benjamin2f871122016-05-20 14:27:17 -04001369 BIO *wbio = SSL_get_wbio(ssl);
1370 if (wbio == NULL || BIO_method_type(wbio) != BIO_TYPE_SOCKET ||
1371 BIO_get_fd(wbio, NULL) != fd) {
David Benjamin22ce9b22016-03-06 19:26:52 -05001372 BIO *bio = BIO_new(BIO_s_socket());
Adam Langleyfcf25832014-12-18 17:42:32 -08001373 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001374 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
David Benjamin066fe0a2015-10-17 21:11:33 -04001375 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001376 }
1377 BIO_set_fd(bio, fd, BIO_NOCLOSE);
David Benjamin4501bd52016-08-01 13:39:41 -04001378 SSL_set0_rbio(ssl, bio);
Adam Langleyfcf25832014-12-18 17:42:32 -08001379 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -04001380 // Copy the wbio over to the rbio.
David Benjamin4501bd52016-08-01 13:39:41 -04001381 BIO_up_ref(wbio);
1382 SSL_set0_rbio(ssl, wbio);
Adam Langleyfcf25832014-12-18 17:42:32 -08001383 }
David Benjamin066fe0a2015-10-17 21:11:33 -04001384 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001385}
Adam Langley95c29f32014-06-20 12:00:00 -07001386
David Benjamin49ddf412016-10-08 11:56:01 -04001387static size_t copy_finished(void *out, size_t out_len, const uint8_t *in,
1388 size_t in_len) {
1389 if (out_len > in_len) {
1390 out_len = in_len;
1391 }
David Benjamin17cf2cb2016-12-13 01:07:13 -05001392 OPENSSL_memcpy(out, in, out_len);
David Benjamin49ddf412016-10-08 11:56:01 -04001393 return in_len;
1394}
1395
David Benjamin1a1b34d2015-10-17 12:51:52 -04001396size_t SSL_get_finished(const SSL *ssl, void *buf, size_t count) {
David Benjaminced00b42016-10-08 11:24:09 -04001397 if (!ssl->s3->initial_handshake_complete ||
1398 ssl3_protocol_version(ssl) < TLS1_VERSION ||
1399 ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1400 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001401 }
1402
David Benjamin49ddf412016-10-08 11:56:01 -04001403 if (ssl->server) {
1404 return copy_finished(buf, count, ssl->s3->previous_server_finished,
1405 ssl->s3->previous_server_finished_len);
David Benjaminced00b42016-10-08 11:24:09 -04001406 }
David Benjamin49ddf412016-10-08 11:56:01 -04001407
1408 return copy_finished(buf, count, ssl->s3->previous_client_finished,
1409 ssl->s3->previous_client_finished_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001410}
Adam Langley95c29f32014-06-20 12:00:00 -07001411
David Benjamin1a1b34d2015-10-17 12:51:52 -04001412size_t SSL_get_peer_finished(const SSL *ssl, void *buf, size_t count) {
David Benjaminced00b42016-10-08 11:24:09 -04001413 if (!ssl->s3->initial_handshake_complete ||
1414 ssl3_protocol_version(ssl) < TLS1_VERSION ||
1415 ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1416 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001417 }
Adam Langley95c29f32014-06-20 12:00:00 -07001418
David Benjamin49ddf412016-10-08 11:56:01 -04001419 if (ssl->server) {
1420 return copy_finished(buf, count, ssl->s3->previous_client_finished,
1421 ssl->s3->previous_client_finished_len);
David Benjaminced00b42016-10-08 11:24:09 -04001422 }
David Benjamin49ddf412016-10-08 11:56:01 -04001423
1424 return copy_finished(buf, count, ssl->s3->previous_server_finished,
1425 ssl->s3->previous_server_finished_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001426}
Adam Langley95c29f32014-06-20 12:00:00 -07001427
David Benjamin59937042015-09-19 13:04:22 -04001428int SSL_get_verify_mode(const SSL *ssl) { return ssl->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -07001429
David Benjamin42fea372015-09-19 01:22:44 -04001430int SSL_get_extms_support(const SSL *ssl) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001431 // TLS 1.3 does not require extended master secret and always reports as
1432 // supporting it.
David Benjamina68c1182016-07-24 00:14:33 -04001433 if (!ssl->s3->have_version) {
1434 return 0;
1435 }
David Benjaminfc02b592017-02-17 16:26:01 -05001436 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1437 return 1;
1438 }
1439
David Benjaminc11ea9422017-08-29 16:33:21 -04001440 // If the initial handshake completed, query the established session.
David Benjaminfc02b592017-02-17 16:26:01 -05001441 if (ssl->s3->established_session != NULL) {
1442 return ssl->s3->established_session->extended_master_secret;
1443 }
1444
David Benjaminc11ea9422017-08-29 16:33:21 -04001445 // Otherwise, query the in-progress handshake.
David Benjaminfc02b592017-02-17 16:26:01 -05001446 if (ssl->s3->hs != NULL) {
1447 return ssl->s3->hs->extended_master_secret;
1448 }
1449 assert(0);
1450 return 0;
Matt Braithwaitecd6f54b2015-09-17 12:54:42 -07001451}
1452
David Benjamin9a41d1b2015-05-16 01:30:09 -04001453int SSL_CTX_get_read_ahead(const SSL_CTX *ctx) { return 0; }
Adam Langley95c29f32014-06-20 12:00:00 -07001454
David Benjamin0d56f882015-12-19 17:05:56 -05001455int SSL_get_read_ahead(const SSL *ssl) { return 0; }
Adam Langley95c29f32014-06-20 12:00:00 -07001456
David Benjamin9a41d1b2015-05-16 01:30:09 -04001457void SSL_CTX_set_read_ahead(SSL_CTX *ctx, int yes) { }
David Benjamin61ecccf2015-05-05 09:44:51 -04001458
David Benjamin0d56f882015-12-19 17:05:56 -05001459void SSL_set_read_ahead(SSL *ssl, int yes) { }
David Benjamin61ecccf2015-05-05 09:44:51 -04001460
David Benjamin9f859492015-10-03 10:44:30 -04001461int SSL_pending(const SSL *ssl) {
1462 if (ssl->s3->rrec.type != SSL3_RT_APPLICATION_DATA) {
1463 return 0;
1464 }
1465 return ssl->s3->rrec.length;
Adam Langleyfcf25832014-12-18 17:42:32 -08001466}
Adam Langley95c29f32014-06-20 12:00:00 -07001467
David Benjaminc11ea9422017-08-29 16:33:21 -04001468// Fix this so it checks all the valid key/cert options
Adam Langleyfcf25832014-12-18 17:42:32 -08001469int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
Adam Langley3a2b47a2017-01-24 13:59:42 -08001470 return ssl_cert_check_private_key(ctx->cert, ctx->cert->privatekey);
Adam Langleyc5ac2b62016-11-07 12:02:35 -08001471}
1472
David Benjaminc11ea9422017-08-29 16:33:21 -04001473// Fix this function so that it takes an optional type parameter
Adam Langleyc5ac2b62016-11-07 12:02:35 -08001474int SSL_check_private_key(const SSL *ssl) {
Adam Langley3a2b47a2017-01-24 13:59:42 -08001475 return ssl_cert_check_private_key(ssl->cert, ssl->cert->privatekey);
Adam Langleyfcf25832014-12-18 17:42:32 -08001476}
Adam Langley95c29f32014-06-20 12:00:00 -07001477
David Benjamin42fea372015-09-19 01:22:44 -04001478long SSL_get_default_timeout(const SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001479 return SSL_DEFAULT_SESSION_TIMEOUT;
1480}
Adam Langley95c29f32014-06-20 12:00:00 -07001481
David Benjamin44d3eed2015-05-21 01:29:55 -04001482int SSL_renegotiate(SSL *ssl) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001483 // Caller-initiated renegotiation is not supported.
David Benjamin3570d732015-06-29 00:28:17 -04001484 OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
David Benjamin44d3eed2015-05-21 01:29:55 -04001485 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001486}
Adam Langley95c29f32014-06-20 12:00:00 -07001487
David Benjamin44d3eed2015-05-21 01:29:55 -04001488int SSL_renegotiate_pending(SSL *ssl) {
1489 return SSL_in_init(ssl) && ssl->s3->initial_handshake_complete;
Adam Langleyfcf25832014-12-18 17:42:32 -08001490}
Adam Langley95c29f32014-06-20 12:00:00 -07001491
David Benjamin936aada2016-06-07 19:09:22 -04001492int SSL_total_renegotiations(const SSL *ssl) {
1493 return ssl->s3->total_renegotiations;
1494}
1495
David Benjamin1d0a1942015-04-26 15:35:35 -04001496size_t SSL_CTX_get_max_cert_list(const SSL_CTX *ctx) {
1497 return ctx->max_cert_list;
1498}
1499
1500void SSL_CTX_set_max_cert_list(SSL_CTX *ctx, size_t max_cert_list) {
1501 if (max_cert_list > kMaxHandshakeSize) {
1502 max_cert_list = kMaxHandshakeSize;
1503 }
1504 ctx->max_cert_list = (uint32_t)max_cert_list;
1505}
1506
1507size_t SSL_get_max_cert_list(const SSL *ssl) {
1508 return ssl->max_cert_list;
1509}
1510
1511void SSL_set_max_cert_list(SSL *ssl, size_t max_cert_list) {
1512 if (max_cert_list > kMaxHandshakeSize) {
1513 max_cert_list = kMaxHandshakeSize;
1514 }
1515 ssl->max_cert_list = (uint32_t)max_cert_list;
1516}
1517
Adam Langleyce9d85e2016-01-24 15:58:39 -08001518int SSL_CTX_set_max_send_fragment(SSL_CTX *ctx, size_t max_send_fragment) {
David Benjamin1d0a1942015-04-26 15:35:35 -04001519 if (max_send_fragment < 512) {
1520 max_send_fragment = 512;
1521 }
1522 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1523 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1524 }
1525 ctx->max_send_fragment = (uint16_t)max_send_fragment;
Adam Langleyce9d85e2016-01-24 15:58:39 -08001526
1527 return 1;
David Benjamin1d0a1942015-04-26 15:35:35 -04001528}
1529
Adam Langleyce9d85e2016-01-24 15:58:39 -08001530int SSL_set_max_send_fragment(SSL *ssl, size_t max_send_fragment) {
David Benjamin1d0a1942015-04-26 15:35:35 -04001531 if (max_send_fragment < 512) {
1532 max_send_fragment = 512;
1533 }
1534 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1535 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1536 }
1537 ssl->max_send_fragment = (uint16_t)max_send_fragment;
Adam Langleyce9d85e2016-01-24 15:58:39 -08001538
1539 return 1;
David Benjamin1d0a1942015-04-26 15:35:35 -04001540}
1541
David Benjamincb9cf792015-05-05 09:46:14 -04001542int SSL_set_mtu(SSL *ssl, unsigned mtu) {
David Benjamince079fd2016-08-02 16:22:34 -04001543 if (!SSL_is_dtls(ssl) || mtu < dtls1_min_mtu()) {
David Benjamincb9cf792015-05-05 09:46:14 -04001544 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001545 }
David Benjamincb9cf792015-05-05 09:46:14 -04001546 ssl->d1->mtu = mtu;
1547 return 1;
1548}
1549
1550int SSL_get_secure_renegotiation_support(const SSL *ssl) {
David Benjamind2610042017-01-03 10:49:28 -05001551 if (!ssl->s3->have_version) {
1552 return 0;
1553 }
1554 return ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
1555 ssl->s3->send_connection_binding;
David Benjamincb9cf792015-05-05 09:46:14 -04001556}
1557
Adam Langleyfcf25832014-12-18 17:42:32 -08001558LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) { return ctx->sessions; }
1559
David Benjamin71f7d3d2015-05-05 09:46:38 -04001560size_t SSL_CTX_sess_number(const SSL_CTX *ctx) {
1561 return lh_SSL_SESSION_num_items(ctx->sessions);
1562}
1563
1564unsigned long SSL_CTX_sess_set_cache_size(SSL_CTX *ctx, unsigned long size) {
1565 unsigned long ret = ctx->session_cache_size;
1566 ctx->session_cache_size = size;
1567 return ret;
1568}
1569
1570unsigned long SSL_CTX_sess_get_cache_size(const SSL_CTX *ctx) {
1571 return ctx->session_cache_size;
1572}
1573
1574int SSL_CTX_set_session_cache_mode(SSL_CTX *ctx, int mode) {
1575 int ret = ctx->session_cache_mode;
1576 ctx->session_cache_mode = mode;
1577 return ret;
1578}
1579
1580int SSL_CTX_get_session_cache_mode(const SSL_CTX *ctx) {
1581 return ctx->session_cache_mode;
1582}
1583
David Benjamin936aada2016-06-07 19:09:22 -04001584
1585int SSL_CTX_get_tlsext_ticket_keys(SSL_CTX *ctx, void *out, size_t len) {
1586 if (out == NULL) {
1587 return 48;
1588 }
1589 if (len != 48) {
1590 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH);
1591 return 0;
1592 }
Martin Kreichgauer72912d22017-08-04 12:06:43 -07001593
David Benjaminc11ea9422017-08-29 16:33:21 -04001594 // The default ticket keys are initialized lazily. Trigger a key
1595 // rotation to initialize them.
Martin Kreichgauer72912d22017-08-04 12:06:43 -07001596 if (!ssl_ctx_rotate_ticket_encryption_key(ctx)) {
1597 return 0;
1598 }
1599
David Benjaminf5260812017-07-12 17:16:39 -04001600 uint8_t *out_bytes = reinterpret_cast<uint8_t *>(out);
Martin Kreichgauer72912d22017-08-04 12:06:43 -07001601 MutexReadLock lock(&ctx->lock);
1602 OPENSSL_memcpy(out_bytes, ctx->tlsext_ticket_key_current->name, 16);
1603 OPENSSL_memcpy(out_bytes + 16, ctx->tlsext_ticket_key_current->hmac_key, 16);
1604 OPENSSL_memcpy(out_bytes + 32, ctx->tlsext_ticket_key_current->aes_key, 16);
David Benjamin936aada2016-06-07 19:09:22 -04001605 return 1;
1606}
1607
1608int SSL_CTX_set_tlsext_ticket_keys(SSL_CTX *ctx, const void *in, size_t len) {
1609 if (in == NULL) {
1610 return 48;
1611 }
1612 if (len != 48) {
1613 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH);
1614 return 0;
1615 }
Martin Kreichgauer72912d22017-08-04 12:06:43 -07001616 if (!ctx->tlsext_ticket_key_current) {
1617 ctx->tlsext_ticket_key_current =
1618 (tlsext_ticket_key *)OPENSSL_malloc(sizeof(tlsext_ticket_key));
1619 if (!ctx->tlsext_ticket_key_current) {
1620 return 0;
1621 }
1622 }
1623 OPENSSL_memset(ctx->tlsext_ticket_key_current, 0, sizeof(tlsext_ticket_key));
David Benjaminf5260812017-07-12 17:16:39 -04001624 const uint8_t *in_bytes = reinterpret_cast<const uint8_t *>(in);
Martin Kreichgauer72912d22017-08-04 12:06:43 -07001625 OPENSSL_memcpy(ctx->tlsext_ticket_key_current->name, in_bytes, 16);
1626 OPENSSL_memcpy(ctx->tlsext_ticket_key_current->hmac_key, in_bytes + 16, 16);
1627 OPENSSL_memcpy(ctx->tlsext_ticket_key_current->aes_key, in_bytes + 32, 16);
1628 OPENSSL_free(ctx->tlsext_ticket_key_prev);
1629 ctx->tlsext_ticket_key_prev = nullptr;
David Benjaminc11ea9422017-08-29 16:33:21 -04001630 // Disable automatic key rotation.
Martin Kreichgauer72912d22017-08-04 12:06:43 -07001631 ctx->tlsext_ticket_key_current->next_rotation_tv_sec = 0;
David Benjamin936aada2016-06-07 19:09:22 -04001632 return 1;
1633}
1634
1635int SSL_CTX_set_tlsext_ticket_key_cb(
1636 SSL_CTX *ctx, int (*callback)(SSL *ssl, uint8_t *key_name, uint8_t *iv,
1637 EVP_CIPHER_CTX *ctx, HMAC_CTX *hmac_ctx,
1638 int encrypt)) {
1639 ctx->tlsext_ticket_key_cb = callback;
1640 return 1;
1641}
1642
1643int SSL_CTX_set1_curves(SSL_CTX *ctx, const int *curves, size_t curves_len) {
1644 return tls1_set_curves(&ctx->supported_group_list,
1645 &ctx->supported_group_list_len, curves,
1646 curves_len);
1647}
1648
1649int SSL_set1_curves(SSL *ssl, const int *curves, size_t curves_len) {
1650 return tls1_set_curves(&ssl->supported_group_list,
1651 &ssl->supported_group_list_len, curves,
1652 curves_len);
1653}
1654
Alessandro Ghedini5fd18072016-09-28 21:04:25 +01001655int SSL_CTX_set1_curves_list(SSL_CTX *ctx, const char *curves) {
1656 return tls1_set_curves_list(&ctx->supported_group_list,
1657 &ctx->supported_group_list_len, curves);
1658}
1659
1660int SSL_set1_curves_list(SSL *ssl, const char *curves) {
1661 return tls1_set_curves_list(&ssl->supported_group_list,
1662 &ssl->supported_group_list_len, curves);
1663}
1664
David Benjamin9e68f192016-06-30 14:55:33 -04001665uint16_t SSL_get_curve_id(const SSL *ssl) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001666 // TODO(davidben): This checks the wrong session if there is a renegotiation
1667 // in progress.
Steven Valdez87eab492016-06-27 16:34:59 -04001668 SSL_SESSION *session = SSL_get_session(ssl);
David Benjamin4882a6c2016-12-11 02:48:12 -05001669 if (session == NULL) {
David Benjamin9e68f192016-06-30 14:55:33 -04001670 return 0;
1671 }
1672
David Benjamin4882a6c2016-12-11 02:48:12 -05001673 return session->group_id;
David Benjamin9e68f192016-06-30 14:55:33 -04001674}
1675
David Benjamin936aada2016-06-07 19:09:22 -04001676int SSL_CTX_set_tmp_dh(SSL_CTX *ctx, const DH *dh) {
David Benjamin936aada2016-06-07 19:09:22 -04001677 return 1;
1678}
1679
1680int SSL_set_tmp_dh(SSL *ssl, const DH *dh) {
David Benjamin936aada2016-06-07 19:09:22 -04001681 return 1;
1682}
1683
David Benjamin70dbf042017-08-08 18:51:37 -04001684STACK_OF(SSL_CIPHER) *SSL_CTX_get_ciphers(const SSL_CTX *ctx) {
David Benjamin8ebeabf2017-03-02 22:04:07 -05001685 return ctx->cipher_list->ciphers;
1686}
1687
David Benjamin70dbf042017-08-08 18:51:37 -04001688int SSL_CTX_cipher_in_group(const SSL_CTX *ctx, size_t i) {
1689 if (i >= sk_SSL_CIPHER_num(ctx->cipher_list->ciphers)) {
1690 return 0;
1691 }
1692 return ctx->cipher_list->in_group_flags[i];
1693}
1694
David Benjamin32876b32015-09-20 12:17:03 -04001695STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *ssl) {
1696 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001697 return NULL;
1698 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001699
David Benjaminaf3b3d32016-10-31 16:29:57 -04001700 const struct ssl_cipher_preference_list_st *prefs =
1701 ssl_get_cipher_preferences(ssl);
1702 if (prefs == NULL) {
1703 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08001704 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001705
David Benjaminaf3b3d32016-10-31 16:29:57 -04001706 return prefs->ciphers;
Adam Langleyfcf25832014-12-18 17:42:32 -08001707}
Adam Langley95c29f32014-06-20 12:00:00 -07001708
David Benjamin32876b32015-09-20 12:17:03 -04001709const char *SSL_get_cipher_list(const SSL *ssl, int n) {
David Benjamin32876b32015-09-20 12:17:03 -04001710 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001711 return NULL;
1712 }
Adam Langley95c29f32014-06-20 12:00:00 -07001713
David Benjamin8ebeabf2017-03-02 22:04:07 -05001714 STACK_OF(SSL_CIPHER) *sk = SSL_get_ciphers(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001715 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
1716 return NULL;
1717 }
Adam Langley95c29f32014-06-20 12:00:00 -07001718
David Benjamin8ebeabf2017-03-02 22:04:07 -05001719 const SSL_CIPHER *c = sk_SSL_CIPHER_value(sk, n);
Adam Langleyfcf25832014-12-18 17:42:32 -08001720 if (c == NULL) {
1721 return NULL;
1722 }
Adam Langley95c29f32014-06-20 12:00:00 -07001723
Adam Langleyfcf25832014-12-18 17:42:32 -08001724 return c->name;
1725}
David Benjamin5491e3f2014-09-29 19:33:09 -04001726
Adam Langleyfcf25832014-12-18 17:42:32 -08001727int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
Matthew Braithwaite6ad20dc2017-02-21 16:41:33 -08001728 return ssl_create_cipher_list(ctx->method, &ctx->cipher_list, str,
1729 0 /* not strict */);
Matthew Braithwaitea57dcfb2017-02-17 22:08:23 -08001730}
1731
1732int SSL_CTX_set_strict_cipher_list(SSL_CTX *ctx, const char *str) {
Matthew Braithwaite6ad20dc2017-02-21 16:41:33 -08001733 return ssl_create_cipher_list(ctx->method, &ctx->cipher_list, str,
1734 1 /* strict */);
Adam Langleyfcf25832014-12-18 17:42:32 -08001735}
David Benjamin39482a12014-07-20 13:30:15 -04001736
David Benjamin32876b32015-09-20 12:17:03 -04001737int SSL_set_cipher_list(SSL *ssl, const char *str) {
Matthew Braithwaite6ad20dc2017-02-21 16:41:33 -08001738 return ssl_create_cipher_list(ssl->ctx->method, &ssl->cipher_list, str,
1739 0 /* not strict */);
Matthew Braithwaitea57dcfb2017-02-17 22:08:23 -08001740}
1741
1742int SSL_set_strict_cipher_list(SSL *ssl, const char *str) {
Matthew Braithwaite6ad20dc2017-02-21 16:41:33 -08001743 return ssl_create_cipher_list(ssl->ctx->method, &ssl->cipher_list, str,
1744 1 /* strict */);
Adam Langleyfcf25832014-12-18 17:42:32 -08001745}
Adam Langley95c29f32014-06-20 12:00:00 -07001746
David Benjamin07e13842015-10-17 13:48:04 -04001747const char *SSL_get_servername(const SSL *ssl, const int type) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001748 if (type != TLSEXT_NAMETYPE_host_name) {
1749 return NULL;
1750 }
Adam Langley95c29f32014-06-20 12:00:00 -07001751
David Benjaminc11ea9422017-08-29 16:33:21 -04001752 // Historically, |SSL_get_servername| was also the configuration getter
1753 // corresponding to |SSL_set_tlsext_host_name|.
David Benjamin07e13842015-10-17 13:48:04 -04001754 if (ssl->tlsext_hostname != NULL) {
1755 return ssl->tlsext_hostname;
1756 }
1757
David Benjamina8614602017-09-06 15:40:19 -04001758 return ssl->s3->hostname;
Adam Langleyfcf25832014-12-18 17:42:32 -08001759}
Adam Langley95c29f32014-06-20 12:00:00 -07001760
David Benjamin07e13842015-10-17 13:48:04 -04001761int SSL_get_servername_type(const SSL *ssl) {
David Benjamina8614602017-09-06 15:40:19 -04001762 if (SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) == NULL) {
Steven Valdez87eab492016-06-27 16:34:59 -04001763 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001764 }
Steven Valdez87eab492016-06-27 16:34:59 -04001765 return TLSEXT_NAMETYPE_host_name;
Adam Langleyfcf25832014-12-18 17:42:32 -08001766}
Adam Langley95c29f32014-06-20 12:00:00 -07001767
David Benjamin3a1dd462017-07-11 16:13:10 -04001768void SSL_CTX_set_custom_verify(
1769 SSL_CTX *ctx, int mode,
1770 enum ssl_verify_result_t (*callback)(SSL *ssl, uint8_t *out_alert)) {
1771 ctx->verify_mode = mode;
1772 ctx->custom_verify_callback = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08001773}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001774
David Benjamin3a1dd462017-07-11 16:13:10 -04001775void SSL_set_custom_verify(
1776 SSL *ssl, int mode,
1777 enum ssl_verify_result_t (*callback)(SSL *ssl, uint8_t *out_alert)) {
1778 ssl->verify_mode = mode;
1779 ssl->custom_verify_callback = callback;
1780}
1781
1782void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
1783 ctx->signed_cert_timestamps_enabled = 1;
Adam Langleyfe366722017-03-07 16:41:04 -08001784}
1785
David Benjamin26e1ff32017-02-14 20:13:00 -05001786void SSL_enable_signed_cert_timestamps(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001787 ssl->signed_cert_timestamps_enabled = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001788}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001789
Adam Langleyfcf25832014-12-18 17:42:32 -08001790void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
1791 ctx->ocsp_stapling_enabled = 1;
1792}
David Benjamin6c7aed02014-08-27 16:42:38 -04001793
David Benjamin26e1ff32017-02-14 20:13:00 -05001794void SSL_enable_ocsp_stapling(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001795 ssl->ocsp_stapling_enabled = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001796}
David Benjamin6c7aed02014-08-27 16:42:38 -04001797
Adam Langleyfcf25832014-12-18 17:42:32 -08001798void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
1799 size_t *out_len) {
Steven Valdez87eab492016-06-27 16:34:59 -04001800 SSL_SESSION *session = SSL_get_session(ssl);
David Benjamin8fc2dc02017-08-22 15:07:51 -07001801 if (ssl->server || !session || !session->signed_cert_timestamp_list) {
1802 *out_len = 0;
1803 *out = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08001804 return;
1805 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001806
David Benjamin8fc2dc02017-08-22 15:07:51 -07001807 *out = CRYPTO_BUFFER_data(session->signed_cert_timestamp_list);
1808 *out_len = CRYPTO_BUFFER_len(session->signed_cert_timestamp_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001809}
David Benjamin6c7aed02014-08-27 16:42:38 -04001810
Adam Langleyfcf25832014-12-18 17:42:32 -08001811void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
1812 size_t *out_len) {
Steven Valdez87eab492016-06-27 16:34:59 -04001813 SSL_SESSION *session = SSL_get_session(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001814 if (ssl->server || !session || !session->ocsp_response) {
David Benjamin8fc2dc02017-08-22 15:07:51 -07001815 *out_len = 0;
1816 *out = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08001817 return;
1818 }
David Benjamin8fc2dc02017-08-22 15:07:51 -07001819
1820 *out = CRYPTO_BUFFER_data(session->ocsp_response);
1821 *out_len = CRYPTO_BUFFER_len(session->ocsp_response);
Adam Langleyfcf25832014-12-18 17:42:32 -08001822}
David Benjamin6c7aed02014-08-27 16:42:38 -04001823
David Benjamin936aada2016-06-07 19:09:22 -04001824int SSL_set_tlsext_host_name(SSL *ssl, const char *name) {
1825 OPENSSL_free(ssl->tlsext_hostname);
1826 ssl->tlsext_hostname = NULL;
1827
1828 if (name == NULL) {
1829 return 1;
1830 }
1831
1832 size_t len = strlen(name);
1833 if (len == 0 || len > TLSEXT_MAXLEN_host_name) {
1834 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL3_EXT_INVALID_SERVERNAME);
1835 return 0;
1836 }
1837 ssl->tlsext_hostname = BUF_strdup(name);
1838 if (ssl->tlsext_hostname == NULL) {
1839 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1840 return 0;
1841 }
1842 return 1;
1843}
1844
1845int SSL_CTX_set_tlsext_servername_callback(
1846 SSL_CTX *ctx, int (*callback)(SSL *ssl, int *out_alert, void *arg)) {
1847 ctx->tlsext_servername_callback = callback;
1848 return 1;
1849}
1850
1851int SSL_CTX_set_tlsext_servername_arg(SSL_CTX *ctx, void *arg) {
1852 ctx->tlsext_servername_arg = arg;
1853 return 1;
1854}
1855
David Benjaminca743582017-06-15 17:51:35 -04001856int SSL_select_next_proto(uint8_t **out, uint8_t *out_len, const uint8_t *peer,
1857 unsigned peer_len, const uint8_t *supported,
1858 unsigned supported_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001859 const uint8_t *result;
David Benjaminca743582017-06-15 17:51:35 -04001860 int status;
Adam Langley95c29f32014-06-20 12:00:00 -07001861
David Benjaminc11ea9422017-08-29 16:33:21 -04001862 // For each protocol in peer preference order, see if we support it.
David Benjaminca743582017-06-15 17:51:35 -04001863 for (unsigned i = 0; i < peer_len;) {
1864 for (unsigned j = 0; j < supported_len;) {
1865 if (peer[i] == supported[j] &&
1866 OPENSSL_memcmp(&peer[i + 1], &supported[j + 1], peer[i]) == 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001867 // We found a match
David Benjaminca743582017-06-15 17:51:35 -04001868 result = &peer[i];
Adam Langleyfcf25832014-12-18 17:42:32 -08001869 status = OPENSSL_NPN_NEGOTIATED;
1870 goto found;
1871 }
David Benjaminca743582017-06-15 17:51:35 -04001872 j += supported[j];
Adam Langleyfcf25832014-12-18 17:42:32 -08001873 j++;
1874 }
David Benjaminca743582017-06-15 17:51:35 -04001875 i += peer[i];
Adam Langleyfcf25832014-12-18 17:42:32 -08001876 i++;
1877 }
Adam Langley95c29f32014-06-20 12:00:00 -07001878
David Benjaminc11ea9422017-08-29 16:33:21 -04001879 // There's no overlap between our protocols and the peer's list.
David Benjaminca743582017-06-15 17:51:35 -04001880 result = supported;
Adam Langleyfcf25832014-12-18 17:42:32 -08001881 status = OPENSSL_NPN_NO_OVERLAP;
Adam Langley95c29f32014-06-20 12:00:00 -07001882
Adam Langleyfcf25832014-12-18 17:42:32 -08001883found:
1884 *out = (uint8_t *)result + 1;
David Benjamin977547b2015-09-16 00:25:52 -04001885 *out_len = result[0];
Adam Langleyfcf25832014-12-18 17:42:32 -08001886 return status;
1887}
Adam Langley95c29f32014-06-20 12:00:00 -07001888
David Benjamin977547b2015-09-16 00:25:52 -04001889void SSL_get0_next_proto_negotiated(const SSL *ssl, const uint8_t **out_data,
1890 unsigned *out_len) {
David Benjamin79978df2015-12-25 15:56:49 -05001891 *out_data = ssl->s3->next_proto_negotiated;
David Benjamin05d4c972017-06-21 16:35:10 -04001892 *out_len = ssl->s3->next_proto_negotiated_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001893}
1894
Adam Langleyfcf25832014-12-18 17:42:32 -08001895void SSL_CTX_set_next_protos_advertised_cb(
1896 SSL_CTX *ctx,
David Benjamin977547b2015-09-16 00:25:52 -04001897 int (*cb)(SSL *ssl, const uint8_t **out, unsigned *out_len, void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08001898 void *arg) {
1899 ctx->next_protos_advertised_cb = cb;
1900 ctx->next_protos_advertised_cb_arg = arg;
1901}
Adam Langley95c29f32014-06-20 12:00:00 -07001902
Adam Langleyfcf25832014-12-18 17:42:32 -08001903void SSL_CTX_set_next_proto_select_cb(
David Benjamin977547b2015-09-16 00:25:52 -04001904 SSL_CTX *ctx, int (*cb)(SSL *ssl, uint8_t **out, uint8_t *out_len,
1905 const uint8_t *in, unsigned in_len, void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08001906 void *arg) {
1907 ctx->next_proto_select_cb = cb;
1908 ctx->next_proto_select_cb_arg = arg;
1909}
Adam Langley95c29f32014-06-20 12:00:00 -07001910
Adam Langleyfcf25832014-12-18 17:42:32 -08001911int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
1912 unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001913 OPENSSL_free(ctx->alpn_client_proto_list);
David Benjaminf5260812017-07-12 17:16:39 -04001914 ctx->alpn_client_proto_list = (uint8_t *)BUF_memdup(protos, protos_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001915 if (!ctx->alpn_client_proto_list) {
1916 return 1;
1917 }
1918 ctx->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001919
Adam Langleyfcf25832014-12-18 17:42:32 -08001920 return 0;
1921}
Adam Langley95c29f32014-06-20 12:00:00 -07001922
Adam Langleyfcf25832014-12-18 17:42:32 -08001923int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001924 OPENSSL_free(ssl->alpn_client_proto_list);
David Benjaminf5260812017-07-12 17:16:39 -04001925 ssl->alpn_client_proto_list = (uint8_t *)BUF_memdup(protos, protos_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001926 if (!ssl->alpn_client_proto_list) {
1927 return 1;
1928 }
1929 ssl->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001930
Adam Langleyfcf25832014-12-18 17:42:32 -08001931 return 0;
1932}
Adam Langley95c29f32014-06-20 12:00:00 -07001933
Adam Langleyfcf25832014-12-18 17:42:32 -08001934void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
1935 int (*cb)(SSL *ssl, const uint8_t **out,
David Benjamin8984f1f2015-09-16 00:10:19 -04001936 uint8_t *out_len, const uint8_t *in,
1937 unsigned in_len, void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08001938 void *arg) {
1939 ctx->alpn_select_cb = cb;
1940 ctx->alpn_select_cb_arg = arg;
1941}
Adam Langley95c29f32014-06-20 12:00:00 -07001942
David Benjamin8984f1f2015-09-16 00:10:19 -04001943void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **out_data,
1944 unsigned *out_len) {
Steven Valdeze831a812017-03-09 14:56:07 -05001945 if (SSL_in_early_data(ssl) && !ssl->server) {
1946 *out_data = ssl->s3->hs->early_session->early_alpn;
1947 *out_len = ssl->s3->hs->early_session->early_alpn_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08001948 } else {
Steven Valdeze831a812017-03-09 14:56:07 -05001949 *out_data = ssl->s3->alpn_selected;
David Benjamin8984f1f2015-09-16 00:10:19 -04001950 *out_len = ssl->s3->alpn_selected_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08001951 }
1952}
Adam Langley95c29f32014-06-20 12:00:00 -07001953
David Benjaminc8ff30c2017-04-04 13:52:36 -04001954void SSL_CTX_set_allow_unknown_alpn_protos(SSL_CTX *ctx, int enabled) {
1955 ctx->allow_unknown_alpn_protos = !!enabled;
1956}
David Benjamin936aada2016-06-07 19:09:22 -04001957
David Benjamineebd3c82016-12-06 17:43:58 -05001958void SSL_CTX_set_tls_channel_id_enabled(SSL_CTX *ctx, int enabled) {
1959 ctx->tlsext_channel_id_enabled = !!enabled;
1960}
1961
David Benjamin936aada2016-06-07 19:09:22 -04001962int SSL_CTX_enable_tls_channel_id(SSL_CTX *ctx) {
David Benjamineebd3c82016-12-06 17:43:58 -05001963 SSL_CTX_set_tls_channel_id_enabled(ctx, 1);
David Benjamin936aada2016-06-07 19:09:22 -04001964 return 1;
1965}
1966
David Benjamineebd3c82016-12-06 17:43:58 -05001967void SSL_set_tls_channel_id_enabled(SSL *ssl, int enabled) {
1968 ssl->tlsext_channel_id_enabled = !!enabled;
1969}
1970
David Benjamin936aada2016-06-07 19:09:22 -04001971int SSL_enable_tls_channel_id(SSL *ssl) {
David Benjamineebd3c82016-12-06 17:43:58 -05001972 SSL_set_tls_channel_id_enabled(ssl, 1);
David Benjamin936aada2016-06-07 19:09:22 -04001973 return 1;
1974}
1975
1976static int is_p256_key(EVP_PKEY *private_key) {
1977 const EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(private_key);
1978 return ec_key != NULL &&
1979 EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key)) ==
1980 NID_X9_62_prime256v1;
1981}
1982
1983int SSL_CTX_set1_tls_channel_id(SSL_CTX *ctx, EVP_PKEY *private_key) {
1984 if (!is_p256_key(private_key)) {
1985 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256);
1986 return 0;
1987 }
1988
1989 EVP_PKEY_free(ctx->tlsext_channel_id_private);
Adam Langley310d3f62016-07-12 10:39:20 -07001990 EVP_PKEY_up_ref(private_key);
1991 ctx->tlsext_channel_id_private = private_key;
David Benjamin936aada2016-06-07 19:09:22 -04001992 ctx->tlsext_channel_id_enabled = 1;
1993
1994 return 1;
1995}
1996
1997int SSL_set1_tls_channel_id(SSL *ssl, EVP_PKEY *private_key) {
1998 if (!is_p256_key(private_key)) {
1999 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256);
2000 return 0;
2001 }
2002
2003 EVP_PKEY_free(ssl->tlsext_channel_id_private);
Adam Langley310d3f62016-07-12 10:39:20 -07002004 EVP_PKEY_up_ref(private_key);
2005 ssl->tlsext_channel_id_private = private_key;
David Benjamin936aada2016-06-07 19:09:22 -04002006 ssl->tlsext_channel_id_enabled = 1;
2007
2008 return 1;
2009}
2010
2011size_t SSL_get_tls_channel_id(SSL *ssl, uint8_t *out, size_t max_out) {
2012 if (!ssl->s3->tlsext_channel_id_valid) {
2013 return 0;
2014 }
David Benjamin17cf2cb2016-12-13 01:07:13 -05002015 OPENSSL_memcpy(out, ssl->s3->tlsext_channel_id,
2016 (max_out < 64) ? max_out : 64);
David Benjamin936aada2016-06-07 19:09:22 -04002017 return 64;
2018}
2019
David Benjamin936aada2016-06-07 19:09:22 -04002020size_t SSL_get0_certificate_types(SSL *ssl, const uint8_t **out_types) {
David Benjamina0486782016-10-06 19:11:32 -04002021 if (ssl->server || ssl->s3->hs == NULL) {
David Benjamin936aada2016-06-07 19:09:22 -04002022 *out_types = NULL;
2023 return 0;
2024 }
David Benjamina0486782016-10-06 19:11:32 -04002025 *out_types = ssl->s3->hs->certificate_types;
2026 return ssl->s3->hs->num_certificate_types;
David Benjamin936aada2016-06-07 19:09:22 -04002027}
2028
David Benjamin0d56f882015-12-19 17:05:56 -05002029EVP_PKEY *SSL_get_privatekey(const SSL *ssl) {
2030 if (ssl->cert != NULL) {
2031 return ssl->cert->privatekey;
Adam Langleyfcf25832014-12-18 17:42:32 -08002032 }
2033
2034 return NULL;
2035}
2036
Adam Langleyfcf25832014-12-18 17:42:32 -08002037EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
2038 if (ctx->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04002039 return ctx->cert->privatekey;
Adam Langleyfcf25832014-12-18 17:42:32 -08002040 }
2041
2042 return NULL;
2043}
2044
David Benjamin42fea372015-09-19 01:22:44 -04002045const SSL_CIPHER *SSL_get_current_cipher(const SSL *ssl) {
David Benjamincfc11c22017-07-18 22:45:18 -04002046 return ssl->s3->aead_write_ctx->cipher();
Adam Langleyfcf25832014-12-18 17:42:32 -08002047}
2048
David Benjamin936aada2016-06-07 19:09:22 -04002049int SSL_session_reused(const SSL *ssl) {
Steven Valdeze831a812017-03-09 14:56:07 -05002050 return ssl->s3->session_reused || SSL_in_early_data(ssl);
David Benjamin936aada2016-06-07 19:09:22 -04002051}
2052
David Benjamin0d56f882015-12-19 17:05:56 -05002053const COMP_METHOD *SSL_get_current_compression(SSL *ssl) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002054
David Benjamin0d56f882015-12-19 17:05:56 -05002055const COMP_METHOD *SSL_get_current_expansion(SSL *ssl) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002056
Adam Langley29ec5d12016-03-01 16:12:28 -08002057int *SSL_get_server_tmp_key(SSL *ssl, EVP_PKEY **out_key) { return 0; }
2058
Adam Langleyfcf25832014-12-18 17:42:32 -08002059void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
Adam Langleybb85f3d2015-10-28 18:44:11 -07002060 ctx->quiet_shutdown = (mode != 0);
Adam Langleyfcf25832014-12-18 17:42:32 -08002061}
2062
2063int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
2064 return ctx->quiet_shutdown;
2065}
2066
Adam Langleybb85f3d2015-10-28 18:44:11 -07002067void SSL_set_quiet_shutdown(SSL *ssl, int mode) {
2068 ssl->quiet_shutdown = (mode != 0);
2069}
Adam Langleyfcf25832014-12-18 17:42:32 -08002070
David Benjamin9f859492015-10-03 10:44:30 -04002071int SSL_get_quiet_shutdown(const SSL *ssl) { return ssl->quiet_shutdown; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002072
David Benjamin63006a92015-10-18 00:00:06 -04002073void SSL_set_shutdown(SSL *ssl, int mode) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002074 // It is an error to clear any bits that have already been set. (We can't try
2075 // to get a second close_notify or send two.)
David Benjaminfa214e42016-05-10 17:03:10 -04002076 assert((SSL_get_shutdown(ssl) & mode) == SSL_get_shutdown(ssl));
David Benjamin63006a92015-10-18 00:00:06 -04002077
David Benjaminfa214e42016-05-10 17:03:10 -04002078 if (mode & SSL_RECEIVED_SHUTDOWN &&
2079 ssl->s3->recv_shutdown == ssl_shutdown_none) {
2080 ssl->s3->recv_shutdown = ssl_shutdown_close_notify;
2081 }
2082
2083 if (mode & SSL_SENT_SHUTDOWN &&
2084 ssl->s3->send_shutdown == ssl_shutdown_none) {
2085 ssl->s3->send_shutdown = ssl_shutdown_close_notify;
2086 }
David Benjamin63006a92015-10-18 00:00:06 -04002087}
Adam Langleyfcf25832014-12-18 17:42:32 -08002088
David Benjaminfa214e42016-05-10 17:03:10 -04002089int SSL_get_shutdown(const SSL *ssl) {
2090 int ret = 0;
2091 if (ssl->s3->recv_shutdown != ssl_shutdown_none) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002092 // Historically, OpenSSL set |SSL_RECEIVED_SHUTDOWN| on both close_notify
2093 // and fatal alert.
David Benjaminfa214e42016-05-10 17:03:10 -04002094 ret |= SSL_RECEIVED_SHUTDOWN;
2095 }
2096 if (ssl->s3->send_shutdown == ssl_shutdown_close_notify) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002097 // Historically, OpenSSL set |SSL_SENT_SHUTDOWN| on only close_notify.
David Benjaminfa214e42016-05-10 17:03:10 -04002098 ret |= SSL_SENT_SHUTDOWN;
2099 }
2100 return ret;
2101}
Adam Langleyfcf25832014-12-18 17:42:32 -08002102
Adam Langleyfcf25832014-12-18 17:42:32 -08002103SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
2104
2105SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
2106 if (ssl->ctx == ctx) {
2107 return ssl->ctx;
2108 }
2109
David Benjaminc11ea9422017-08-29 16:33:21 -04002110 // One cannot change the X.509 callbacks during a connection.
Adam Langley3509dac2017-02-01 11:59:18 -08002111 if (ssl->ctx->x509_method != ctx->x509_method) {
2112 assert(0);
2113 return NULL;
2114 }
2115
Adam Langleyfcf25832014-12-18 17:42:32 -08002116 if (ctx == NULL) {
David Benjaminbe497062017-03-10 16:08:36 -05002117 ctx = ssl->session_ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08002118 }
2119
David Benjamin2755a3e2015-04-22 16:17:58 -04002120 ssl_cert_free(ssl->cert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002121 ssl->cert = ssl_cert_dup(ctx->cert);
David Benjamin2755a3e2015-04-22 16:17:58 -04002122
David Benjaminf650c712016-12-22 15:54:51 -05002123 SSL_CTX_up_ref(ctx);
2124 SSL_CTX_free(ssl->ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002125 ssl->ctx = ctx;
2126
Adam Langleyfcf25832014-12-18 17:42:32 -08002127 return ssl->ctx;
2128}
2129
Adam Langley95c29f32014-06-20 12:00:00 -07002130void SSL_set_info_callback(SSL *ssl,
David Benjamin82170242015-10-17 22:51:17 -04002131 void (*cb)(const SSL *ssl, int type, int value)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002132 ssl->info_callback = cb;
2133}
Adam Langley95c29f32014-06-20 12:00:00 -07002134
David Benjamin82170242015-10-17 22:51:17 -04002135void (*SSL_get_info_callback(const SSL *ssl))(const SSL *ssl, int type,
2136 int value) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002137 return ssl->info_callback;
2138}
Adam Langley95c29f32014-06-20 12:00:00 -07002139
David Benjamincb0c29f2016-12-12 17:00:50 -05002140int SSL_state(const SSL *ssl) {
David Benjamin9e766d72017-02-10 23:14:17 -05002141 return SSL_in_init(ssl) ? SSL_ST_INIT : SSL_ST_OK;
David Benjamincb0c29f2016-12-12 17:00:50 -05002142}
Adam Langley95c29f32014-06-20 12:00:00 -07002143
David Benjaminece089c2015-05-15 23:52:42 -04002144void SSL_set_state(SSL *ssl, int state) { }
Adam Langley95c29f32014-06-20 12:00:00 -07002145
Emily Stark95a79ee2016-02-25 21:12:28 -08002146char *SSL_get_shared_ciphers(const SSL *ssl, char *buf, int len) {
2147 if (len <= 0) {
2148 return NULL;
2149 }
2150 buf[0] = '\0';
2151 return buf;
2152}
2153
David Benjamin8a589332015-12-04 23:14:35 -05002154int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
David Benjamind94682d2017-05-14 17:10:18 -04002155 CRYPTO_EX_dup *dup_unused, CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002156 int index;
2157 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp,
David Benjamind94682d2017-05-14 17:10:18 -04002158 free_func)) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002159 return -1;
2160 }
2161 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002162}
Adam Langley95c29f32014-06-20 12:00:00 -07002163
David Benjamin27e377e2017-07-31 19:09:42 -04002164int SSL_set_ex_data(SSL *ssl, int idx, void *data) {
2165 return CRYPTO_set_ex_data(&ssl->ex_data, idx, data);
Adam Langleyfcf25832014-12-18 17:42:32 -08002166}
Adam Langley95c29f32014-06-20 12:00:00 -07002167
David Benjamin1ac08fe2015-09-15 01:36:22 -04002168void *SSL_get_ex_data(const SSL *ssl, int idx) {
2169 return CRYPTO_get_ex_data(&ssl->ex_data, idx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002170}
Adam Langley95c29f32014-06-20 12:00:00 -07002171
David Benjamin8a589332015-12-04 23:14:35 -05002172int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
David Benjamind94682d2017-05-14 17:10:18 -04002173 CRYPTO_EX_dup *dup_unused,
Adam Langleyfcf25832014-12-18 17:42:32 -08002174 CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002175 int index;
2176 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp,
David Benjamind94682d2017-05-14 17:10:18 -04002177 free_func)) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002178 return -1;
2179 }
2180 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002181}
Adam Langley95c29f32014-06-20 12:00:00 -07002182
David Benjamin27e377e2017-07-31 19:09:42 -04002183int SSL_CTX_set_ex_data(SSL_CTX *ctx, int idx, void *data) {
2184 return CRYPTO_set_ex_data(&ctx->ex_data, idx, data);
Adam Langleyfcf25832014-12-18 17:42:32 -08002185}
Adam Langley95c29f32014-06-20 12:00:00 -07002186
David Benjamin1ac08fe2015-09-15 01:36:22 -04002187void *SSL_CTX_get_ex_data(const SSL_CTX *ctx, int idx) {
2188 return CRYPTO_get_ex_data(&ctx->ex_data, idx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002189}
Adam Langley95c29f32014-06-20 12:00:00 -07002190
David Benjamin93d17492015-10-17 12:43:36 -04002191int SSL_want(const SSL *ssl) { return ssl->rwstate; }
Adam Langley95c29f32014-06-20 12:00:00 -07002192
Adam Langleyfcf25832014-12-18 17:42:32 -08002193void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
2194 RSA *(*cb)(SSL *ssl, int is_export,
David Benjamin27e377e2017-07-31 19:09:42 -04002195 int keylength)) {}
Adam Langley95c29f32014-06-20 12:00:00 -07002196
Adam Langleyfcf25832014-12-18 17:42:32 -08002197void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
David Benjamin27e377e2017-07-31 19:09:42 -04002198 int keylength)) {}
Adam Langleyfcf25832014-12-18 17:42:32 -08002199
2200void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
David Benjamin27e377e2017-07-31 19:09:42 -04002201 DH *(*cb)(SSL *ssl, int is_export,
2202 int keylength)) {}
Adam Langleyfcf25832014-12-18 17:42:32 -08002203
David Benjamin27e377e2017-07-31 19:09:42 -04002204void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*cb)(SSL *ssl, int is_export,
2205 int keylength)) {}
Adam Langleyfcf25832014-12-18 17:42:32 -08002206
David Benjamin0a9bf662017-06-21 16:34:15 -04002207static int use_psk_identity_hint(char **out, const char *identity_hint) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002208 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
David Benjamin3570d732015-06-29 00:28:17 -04002209 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -08002210 return 0;
2211 }
2212
David Benjaminc11ea9422017-08-29 16:33:21 -04002213 // Clear currently configured hint, if any.
David Benjamin0a9bf662017-06-21 16:34:15 -04002214 OPENSSL_free(*out);
2215 *out = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002216
David Benjaminc11ea9422017-08-29 16:33:21 -04002217 // Treat the empty hint as not supplying one. Plain PSK makes it possible to
2218 // send either no hint (omit ServerKeyExchange) or an empty hint, while
2219 // ECDHE_PSK can only spell empty hint. Having different capabilities is odd,
2220 // so we interpret empty and missing as identical.
David Benjamin78679342016-09-16 19:42:05 -04002221 if (identity_hint != NULL && identity_hint[0] != '\0') {
David Benjamin0a9bf662017-06-21 16:34:15 -04002222 *out = BUF_strdup(identity_hint);
2223 if (*out == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002224 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002225 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002226 }
Adam Langley95c29f32014-06-20 12:00:00 -07002227
Adam Langleyfcf25832014-12-18 17:42:32 -08002228 return 1;
2229}
Adam Langley95c29f32014-06-20 12:00:00 -07002230
David Benjamin0a9bf662017-06-21 16:34:15 -04002231int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
2232 return use_psk_identity_hint(&ctx->psk_identity_hint, identity_hint);
2233}
2234
2235int SSL_use_psk_identity_hint(SSL *ssl, const char *identity_hint) {
2236 return use_psk_identity_hint(&ssl->psk_identity_hint, identity_hint);
2237}
2238
David Benjamine8814df2015-09-15 08:05:54 -04002239const char *SSL_get_psk_identity_hint(const SSL *ssl) {
2240 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002241 return NULL;
2242 }
David Benjamine8814df2015-09-15 08:05:54 -04002243 return ssl->psk_identity_hint;
Adam Langleyfcf25832014-12-18 17:42:32 -08002244}
Adam Langley95c29f32014-06-20 12:00:00 -07002245
David Benjamine8814df2015-09-15 08:05:54 -04002246const char *SSL_get_psk_identity(const SSL *ssl) {
Steven Valdez87eab492016-06-27 16:34:59 -04002247 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002248 return NULL;
2249 }
Steven Valdez87eab492016-06-27 16:34:59 -04002250 SSL_SESSION *session = SSL_get_session(ssl);
2251 if (session == NULL) {
2252 return NULL;
2253 }
2254 return session->psk_identity;
Adam Langleyfcf25832014-12-18 17:42:32 -08002255}
Adam Langley95c29f32014-06-20 12:00:00 -07002256
Adam Langleyfcf25832014-12-18 17:42:32 -08002257void SSL_set_psk_client_callback(
David Benjamine8814df2015-09-15 08:05:54 -04002258 SSL *ssl, unsigned (*cb)(SSL *ssl, const char *hint, char *identity,
2259 unsigned max_identity_len, uint8_t *psk,
2260 unsigned max_psk_len)) {
2261 ssl->psk_client_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002262}
Adam Langley95c29f32014-06-20 12:00:00 -07002263
Adam Langleyfcf25832014-12-18 17:42:32 -08002264void SSL_CTX_set_psk_client_callback(
David Benjamine8814df2015-09-15 08:05:54 -04002265 SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *hint, char *identity,
2266 unsigned max_identity_len, uint8_t *psk,
2267 unsigned max_psk_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002268 ctx->psk_client_callback = cb;
2269}
Adam Langley95c29f32014-06-20 12:00:00 -07002270
Adam Langleyfcf25832014-12-18 17:42:32 -08002271void SSL_set_psk_server_callback(
David Benjamine8814df2015-09-15 08:05:54 -04002272 SSL *ssl, unsigned (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
2273 unsigned max_psk_len)) {
2274 ssl->psk_server_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002275}
Adam Langley95c29f32014-06-20 12:00:00 -07002276
Adam Langleyfcf25832014-12-18 17:42:32 -08002277void SSL_CTX_set_psk_server_callback(
David Benjamine8814df2015-09-15 08:05:54 -04002278 SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *identity,
2279 uint8_t *psk, unsigned max_psk_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002280 ctx->psk_server_callback = cb;
2281}
Adam Langley95c29f32014-06-20 12:00:00 -07002282
Adam Langleyfcf25832014-12-18 17:42:32 -08002283void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
2284 void (*cb)(int write_p, int version,
2285 int content_type, const void *buf,
2286 size_t len, SSL *ssl, void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002287 ctx->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002288}
David Benjamin61ecccf2015-05-05 09:44:51 -04002289
2290void SSL_CTX_set_msg_callback_arg(SSL_CTX *ctx, void *arg) {
2291 ctx->msg_callback_arg = arg;
2292}
2293
Adam Langleyfcf25832014-12-18 17:42:32 -08002294void SSL_set_msg_callback(SSL *ssl,
2295 void (*cb)(int write_p, int version, int content_type,
2296 const void *buf, size_t len, SSL *ssl,
2297 void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002298 ssl->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002299}
Adam Langley95c29f32014-06-20 12:00:00 -07002300
David Benjamin61ecccf2015-05-05 09:44:51 -04002301void SSL_set_msg_callback_arg(SSL *ssl, void *arg) {
2302 ssl->msg_callback_arg = arg;
2303}
2304
David Benjamind28f59c2015-11-17 22:32:50 -05002305void SSL_CTX_set_keylog_callback(SSL_CTX *ctx,
2306 void (*cb)(const SSL *ssl, const char *line)) {
2307 ctx->keylog_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002308}
Adam Langley95c29f32014-06-20 12:00:00 -07002309
David Benjamin6e3f5cc2016-09-14 14:43:14 -04002310void (*SSL_CTX_get_keylog_callback(const SSL_CTX *ctx))(const SSL *ssl,
2311 const char *line) {
2312 return ctx->keylog_callback;
2313}
2314
Taylor Brandstetter9edb2c62016-06-08 15:26:59 -07002315void SSL_CTX_set_current_time_cb(SSL_CTX *ctx,
2316 void (*cb)(const SSL *ssl,
2317 struct timeval *out_clock)) {
2318 ctx->current_time_cb = cb;
2319}
2320
David Benjamin5d8b1282015-10-17 23:26:35 -04002321int SSL_is_init_finished(const SSL *ssl) {
David Benjamin9e766d72017-02-10 23:14:17 -05002322 return !SSL_in_init(ssl);
David Benjamin5d8b1282015-10-17 23:26:35 -04002323}
2324
2325int SSL_in_init(const SSL *ssl) {
Steven Valdezd8168742017-08-31 10:15:48 -04002326 // This returns false once all the handshake state has been finalized, to
2327 // allow callbacks and getters based on SSL_in_init to return the correct
2328 // values.
2329 SSL_HANDSHAKE *hs = ssl->s3->hs;
2330 return hs != nullptr && !hs->handshake_finalized;
David Benjamin5d8b1282015-10-17 23:26:35 -04002331}
2332
2333int SSL_in_false_start(const SSL *ssl) {
David Benjamina0486782016-10-06 19:11:32 -04002334 if (ssl->s3->hs == NULL) {
2335 return 0;
2336 }
2337 return ssl->s3->hs->in_false_start;
David Benjamined7c4752015-02-16 19:16:46 -05002338}
2339
David Benjamin0d56f882015-12-19 17:05:56 -05002340int SSL_cutthrough_complete(const SSL *ssl) {
2341 return SSL_in_false_start(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08002342}
Adam Langley95c29f32014-06-20 12:00:00 -07002343
Adam Langleyfcf25832014-12-18 17:42:32 -08002344void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
2345 size_t *ssl_session_size) {
2346 *ssl_size = sizeof(SSL);
2347 *ssl_ctx_size = sizeof(SSL_CTX);
2348 *ssl_session_size = sizeof(SSL_SESSION);
2349}
Feng Lu41aa3252014-11-21 22:47:56 -08002350
David Benjamince079fd2016-08-02 16:22:34 -04002351int SSL_is_server(const SSL *ssl) { return ssl->server; }
2352
2353int SSL_is_dtls(const SSL *ssl) { return ssl->method->is_dtls; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002354
Alessandro Ghedini57e81e62017-03-14 23:36:00 +00002355void SSL_CTX_set_select_certificate_cb(
2356 SSL_CTX *ctx,
2357 enum ssl_select_cert_result_t (*cb)(const SSL_CLIENT_HELLO *)) {
David Benjamind4c2bce2015-10-17 12:28:18 -04002358 ctx->select_certificate_cb = cb;
2359}
2360
David Benjamin731058e2016-12-03 23:15:13 -05002361void SSL_CTX_set_dos_protection_cb(SSL_CTX *ctx,
2362 int (*cb)(const SSL_CLIENT_HELLO *)) {
Adam Langley524e7172015-02-20 16:04:00 -08002363 ctx->dos_protection_cb = cb;
2364}
2365
David Benjamin1d5ef3b2015-10-12 19:54:18 -04002366void SSL_set_renegotiate_mode(SSL *ssl, enum ssl_renegotiate_mode_t mode) {
2367 ssl->renegotiate_mode = mode;
2368}
2369
Adam Langleyc2d32802015-11-03 18:36:10 -08002370int SSL_get_ivs(const SSL *ssl, const uint8_t **out_read_iv,
2371 const uint8_t **out_write_iv, size_t *out_iv_len) {
Adam Langleyc2d32802015-11-03 18:36:10 -08002372 size_t write_iv_len;
David Benjamincfc11c22017-07-18 22:45:18 -04002373 if (!ssl->s3->aead_read_ctx->GetIV(out_read_iv, out_iv_len) ||
2374 !ssl->s3->aead_write_ctx->GetIV(out_write_iv, &write_iv_len) ||
Adam Langleyc2d32802015-11-03 18:36:10 -08002375 *out_iv_len != write_iv_len) {
2376 return 0;
2377 }
2378
2379 return 1;
David Benjamine348ff42015-11-06 17:55:30 -05002380}
David Benjamin6e807652015-11-02 12:02:20 -05002381
David Benjamin91b25012016-01-21 16:39:58 -05002382static uint64_t be_to_u64(const uint8_t in[8]) {
David Benjaminde942382016-02-11 12:02:01 -05002383 return (((uint64_t)in[0]) << 56) | (((uint64_t)in[1]) << 48) |
2384 (((uint64_t)in[2]) << 40) | (((uint64_t)in[3]) << 32) |
2385 (((uint64_t)in[4]) << 24) | (((uint64_t)in[5]) << 16) |
2386 (((uint64_t)in[6]) << 8) | ((uint64_t)in[7]);
David Benjamin91b25012016-01-21 16:39:58 -05002387}
2388
2389uint64_t SSL_get_read_sequence(const SSL *ssl) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002390 // TODO(davidben): Internally represent sequence numbers as uint64_t.
David Benjamince079fd2016-08-02 16:22:34 -04002391 if (SSL_is_dtls(ssl)) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002392 // max_seq_num already includes the epoch.
David Benjaminde942382016-02-11 12:02:01 -05002393 assert(ssl->d1->r_epoch == (ssl->d1->bitmap.max_seq_num >> 48));
2394 return ssl->d1->bitmap.max_seq_num;
2395 }
David Benjamin91b25012016-01-21 16:39:58 -05002396 return be_to_u64(ssl->s3->read_sequence);
2397}
2398
2399uint64_t SSL_get_write_sequence(const SSL *ssl) {
David Benjaminde942382016-02-11 12:02:01 -05002400 uint64_t ret = be_to_u64(ssl->s3->write_sequence);
David Benjamince079fd2016-08-02 16:22:34 -04002401 if (SSL_is_dtls(ssl)) {
David Benjaminde942382016-02-11 12:02:01 -05002402 assert((ret >> 48) == 0);
2403 ret |= ((uint64_t)ssl->d1->w_epoch) << 48;
2404 }
2405 return ret;
David Benjamin91b25012016-01-21 16:39:58 -05002406}
2407
Steven Valdez02563852016-06-23 13:33:05 -04002408uint16_t SSL_get_peer_signature_algorithm(const SSL *ssl) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002409 // TODO(davidben): This checks the wrong session if there is a renegotiation
2410 // in progress.
David Benjaminf1050fd2016-12-13 20:05:36 -05002411 SSL_SESSION *session = SSL_get_session(ssl);
2412 if (session == NULL) {
2413 return 0;
2414 }
2415
2416 return session->peer_signature_algorithm;
Steven Valdez02563852016-06-23 13:33:05 -04002417}
2418
David Benjamin96ba15f2015-12-25 14:10:17 -05002419size_t SSL_get_client_random(const SSL *ssl, uint8_t *out, size_t max_out) {
2420 if (max_out == 0) {
2421 return sizeof(ssl->s3->client_random);
2422 }
2423 if (max_out > sizeof(ssl->s3->client_random)) {
2424 max_out = sizeof(ssl->s3->client_random);
2425 }
David Benjamin17cf2cb2016-12-13 01:07:13 -05002426 OPENSSL_memcpy(out, ssl->s3->client_random, max_out);
David Benjamin96ba15f2015-12-25 14:10:17 -05002427 return max_out;
2428}
2429
2430size_t SSL_get_server_random(const SSL *ssl, uint8_t *out, size_t max_out) {
2431 if (max_out == 0) {
2432 return sizeof(ssl->s3->server_random);
2433 }
2434 if (max_out > sizeof(ssl->s3->server_random)) {
2435 max_out = sizeof(ssl->s3->server_random);
2436 }
David Benjamin17cf2cb2016-12-13 01:07:13 -05002437 OPENSSL_memcpy(out, ssl->s3->server_random, max_out);
David Benjamin96ba15f2015-12-25 14:10:17 -05002438 return max_out;
2439}
2440
David Benjaminf6494f42016-01-07 12:37:41 -08002441const SSL_CIPHER *SSL_get_pending_cipher(const SSL *ssl) {
David Benjamin45738dd2017-02-09 20:01:26 -05002442 SSL_HANDSHAKE *hs = ssl->s3->hs;
2443 if (hs == NULL) {
David Benjaminf6494f42016-01-07 12:37:41 -08002444 return NULL;
2445 }
David Benjamin45738dd2017-02-09 20:01:26 -05002446 return hs->new_cipher;
David Benjaminf6494f42016-01-07 12:37:41 -08002447}
2448
David Benjaminbbaf3672016-11-17 10:53:09 +09002449void SSL_set_retain_only_sha256_of_client_certs(SSL *ssl, int enabled) {
2450 ssl->retain_only_sha256_of_client_certs = !!enabled;
2451}
2452
David Benjamincdd0b7e2016-01-07 10:10:41 -08002453void SSL_CTX_set_retain_only_sha256_of_client_certs(SSL_CTX *ctx, int enabled) {
2454 ctx->retain_only_sha256_of_client_certs = !!enabled;
2455}
2456
David Benjamin65ac9972016-09-02 21:35:25 -04002457void SSL_CTX_set_grease_enabled(SSL_CTX *ctx, int enabled) {
2458 ctx->grease_enabled = !!enabled;
2459}
2460
David Benjamin35ac5b72017-03-03 15:05:56 -05002461int32_t SSL_get_ticket_age_skew(const SSL *ssl) {
2462 return ssl->s3->ticket_age_skew;
2463}
2464
David Benjamin27bbae42015-09-13 00:54:37 -04002465int SSL_clear(SSL *ssl) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002466 // In OpenSSL, reusing a client |SSL| with |SSL_clear| causes the previously
2467 // established session to be offered the next time around. wpa_supplicant
2468 // depends on this behavior, so emulate it.
David Benjaminb79cc842016-12-07 15:57:14 -05002469 SSL_SESSION *session = NULL;
2470 if (!ssl->server && ssl->s3->established_session != NULL) {
2471 session = ssl->s3->established_session;
2472 SSL_SESSION_up_ref(session);
2473 }
2474
David Benjaminc11ea9422017-08-29 16:33:21 -04002475 // TODO(davidben): Some state on |ssl| is reset both in |SSL_new| and
2476 // |SSL_clear| because it is per-connection state rather than configuration
2477 // state. Per-connection state should be on |ssl->s3| and |ssl->d1| so it is
2478 // naturally reset at the right points between |SSL_new|, |SSL_clear|, and
2479 // |ssl3_new|.
David Benjamin27bbae42015-09-13 00:54:37 -04002480
2481 ssl->rwstate = SSL_NOTHING;
2482
2483 BUF_MEM_free(ssl->init_buf);
2484 ssl->init_buf = NULL;
2485
David Benjaminc11ea9422017-08-29 16:33:21 -04002486 // The ssl->d1->mtu is simultaneously configuration (preserved across
2487 // clear) and connection-specific state (gets reset).
2488 //
2489 // TODO(davidben): Avoid this.
David Benjamin27bbae42015-09-13 00:54:37 -04002490 unsigned mtu = 0;
2491 if (ssl->d1 != NULL) {
2492 mtu = ssl->d1->mtu;
2493 }
2494
2495 ssl->method->ssl_free(ssl);
2496 if (!ssl->method->ssl_new(ssl)) {
David Benjaminb79cc842016-12-07 15:57:14 -05002497 SSL_SESSION_free(session);
David Benjamin27bbae42015-09-13 00:54:37 -04002498 return 0;
2499 }
David Benjamin27bbae42015-09-13 00:54:37 -04002500
David Benjamince079fd2016-08-02 16:22:34 -04002501 if (SSL_is_dtls(ssl) && (SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
David Benjamin27bbae42015-09-13 00:54:37 -04002502 ssl->d1->mtu = mtu;
2503 }
2504
David Benjaminb79cc842016-12-07 15:57:14 -05002505 if (session != NULL) {
2506 SSL_set_session(ssl, session);
2507 SSL_SESSION_free(session);
2508 }
2509
David Benjamin27bbae42015-09-13 00:54:37 -04002510 return 1;
2511}
2512
David Benjaminda881e92015-04-26 14:45:04 -04002513int SSL_CTX_sess_connect(const SSL_CTX *ctx) { return 0; }
2514int SSL_CTX_sess_connect_good(const SSL_CTX *ctx) { return 0; }
2515int SSL_CTX_sess_connect_renegotiate(const SSL_CTX *ctx) { return 0; }
2516int SSL_CTX_sess_accept(const SSL_CTX *ctx) { return 0; }
2517int SSL_CTX_sess_accept_renegotiate(const SSL_CTX *ctx) { return 0; }
2518int SSL_CTX_sess_accept_good(const SSL_CTX *ctx) { return 0; }
2519int SSL_CTX_sess_hits(const SSL_CTX *ctx) { return 0; }
2520int SSL_CTX_sess_cb_hits(const SSL_CTX *ctx) { return 0; }
2521int SSL_CTX_sess_misses(const SSL_CTX *ctx) { return 0; }
2522int SSL_CTX_sess_timeouts(const SSL_CTX *ctx) { return 0; }
2523int SSL_CTX_sess_cache_full(const SSL_CTX *ctx) { return 0; }
David Benjamin936aada2016-06-07 19:09:22 -04002524
2525int SSL_num_renegotiations(const SSL *ssl) {
2526 return SSL_total_renegotiations(ssl);
2527}
2528
2529int SSL_CTX_need_tmp_RSA(const SSL_CTX *ctx) { return 0; }
2530int SSL_need_tmp_RSA(const SSL *ssl) { return 0; }
2531int SSL_CTX_set_tmp_rsa(SSL_CTX *ctx, const RSA *rsa) { return 1; }
2532int SSL_set_tmp_rsa(SSL *ssl, const RSA *rsa) { return 1; }
Matt Braithwaite4838d8a2015-08-20 13:20:03 -07002533void ERR_load_SSL_strings(void) {}
David Benjamin27bbae42015-09-13 00:54:37 -04002534void SSL_load_error_strings(void) {}
David Benjamin936aada2016-06-07 19:09:22 -04002535int SSL_cache_hit(SSL *ssl) { return SSL_session_reused(ssl); }
2536
2537int SSL_CTX_set_tmp_ecdh(SSL_CTX *ctx, const EC_KEY *ec_key) {
2538 if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) {
2539 OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
2540 return 0;
2541 }
2542 int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key));
2543 return SSL_CTX_set1_curves(ctx, &nid, 1);
2544}
2545
2546int SSL_set_tmp_ecdh(SSL *ssl, const EC_KEY *ec_key) {
2547 if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) {
2548 OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
2549 return 0;
2550 }
2551 int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key));
2552 return SSL_set1_curves(ssl, &nid, 1);
2553}
David Benjamin721e8b72016-08-03 13:13:17 -04002554
Adam Langley4c341d02017-03-08 19:33:21 -08002555void SSL_CTX_set_ticket_aead_method(SSL_CTX *ctx,
2556 const SSL_TICKET_AEAD_METHOD *aead_method) {
2557 ctx->ticket_aead_method = aead_method;
2558}