blob: 9c551c2a8a000237f70ef6f22b4896ec8b51ed68 [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 Benjamin879efc32017-09-21 11:20:53 -0400190bool CBBFinishArray(CBB *cbb, Array<uint8_t> *out) {
191 uint8_t *ptr;
192 size_t len;
193 if (!CBB_finish(cbb, &ptr, &len)) {
194 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
195 return false;
196 }
197 out->Reset(ptr, len);
198 return true;
199}
200
David Benjamin86e95b82017-07-18 16:34:25 -0400201void ssl_reset_error_state(SSL *ssl) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400202 // Functions which use |SSL_get_error| must reset I/O and error state on
203 // entry.
David Benjamin86e95b82017-07-18 16:34:25 -0400204 ssl->rwstate = SSL_NOTHING;
205 ERR_clear_error();
206 ERR_clear_system_error();
207}
208
209int ssl_can_write(const SSL *ssl) {
210 return !SSL_in_init(ssl) || ssl->s3->hs->can_early_write;
211}
212
213int ssl_can_read(const SSL *ssl) {
214 return !SSL_in_init(ssl) || ssl->s3->hs->can_early_read;
215}
216
217void ssl_cipher_preference_list_free(
218 struct ssl_cipher_preference_list_st *cipher_list) {
219 if (cipher_list == NULL) {
220 return;
221 }
222 sk_SSL_CIPHER_free(cipher_list->ciphers);
223 OPENSSL_free(cipher_list->in_group_flags);
224 OPENSSL_free(cipher_list);
225}
226
227void ssl_update_cache(SSL_HANDSHAKE *hs, int mode) {
228 SSL *const ssl = hs->ssl;
229 SSL_CTX *ctx = ssl->session_ctx;
David Benjaminc11ea9422017-08-29 16:33:21 -0400230 // Never cache sessions with empty session IDs.
David Benjamin86e95b82017-07-18 16:34:25 -0400231 if (ssl->s3->established_session->session_id_length == 0 ||
David Benjamin5ef40c62017-08-23 21:28:29 -0700232 ssl->s3->established_session->not_resumable ||
David Benjamin86e95b82017-07-18 16:34:25 -0400233 (ctx->session_cache_mode & mode) != mode) {
234 return;
235 }
236
David Benjaminc11ea9422017-08-29 16:33:21 -0400237 // Clients never use the internal session cache.
David Benjamin86e95b82017-07-18 16:34:25 -0400238 int use_internal_cache = ssl->server && !(ctx->session_cache_mode &
239 SSL_SESS_CACHE_NO_INTERNAL_STORE);
240
David Benjaminc11ea9422017-08-29 16:33:21 -0400241 // A client may see new sessions on abbreviated handshakes if the server
242 // decides to renew the ticket. Once the handshake is completed, it should be
243 // inserted into the cache.
David Benjamin86e95b82017-07-18 16:34:25 -0400244 if (ssl->s3->established_session != ssl->session ||
245 (!ssl->server && hs->ticket_expected)) {
246 if (use_internal_cache) {
247 SSL_CTX_add_session(ctx, ssl->s3->established_session);
248 }
249 if (ctx->new_session_cb != NULL) {
250 SSL_SESSION_up_ref(ssl->s3->established_session);
251 if (!ctx->new_session_cb(ssl, ssl->s3->established_session)) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400252 // |new_session_cb|'s return value signals whether it took ownership.
David Benjamin86e95b82017-07-18 16:34:25 -0400253 SSL_SESSION_free(ssl->s3->established_session);
254 }
255 }
256 }
257
258 if (use_internal_cache &&
259 !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR)) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400260 // Automatically flush the internal session cache every 255 connections.
David Benjamin86e95b82017-07-18 16:34:25 -0400261 int flush_cache = 0;
262 CRYPTO_MUTEX_lock_write(&ctx->lock);
263 ctx->handshakes_since_cache_flush++;
264 if (ctx->handshakes_since_cache_flush >= 255) {
265 flush_cache = 1;
266 ctx->handshakes_since_cache_flush = 0;
267 }
268 CRYPTO_MUTEX_unlock_write(&ctx->lock);
269
270 if (flush_cache) {
271 struct OPENSSL_timeval now;
272 ssl_get_current_time(ssl, &now);
273 SSL_CTX_flush_sessions(ctx, now.tv_sec);
274 }
275 }
276}
277
278static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
279 static const char hextable[] = "0123456789abcdef";
280 uint8_t *out;
281
282 if (!CBB_add_space(cbb, &out, in_len * 2)) {
283 return 0;
284 }
285
286 for (size_t i = 0; i < in_len; i++) {
287 *(out++) = (uint8_t)hextable[in[i] >> 4];
288 *(out++) = (uint8_t)hextable[in[i] & 0xf];
289 }
290
291 return 1;
292}
293
294int ssl_log_secret(const SSL *ssl, const char *label, const uint8_t *secret,
295 size_t secret_len) {
296 if (ssl->ctx->keylog_callback == NULL) {
297 return 1;
298 }
299
David Benjamin1386aad2017-07-19 23:57:40 -0400300 ScopedCBB cbb;
David Benjamin86e95b82017-07-18 16:34:25 -0400301 uint8_t *out;
302 size_t out_len;
David Benjamin1386aad2017-07-19 23:57:40 -0400303 if (!CBB_init(cbb.get(), strlen(label) + 1 + SSL3_RANDOM_SIZE * 2 + 1 +
David Benjamin86e95b82017-07-18 16:34:25 -0400304 secret_len * 2 + 1) ||
David Benjamin1386aad2017-07-19 23:57:40 -0400305 !CBB_add_bytes(cbb.get(), (const uint8_t *)label, strlen(label)) ||
306 !CBB_add_bytes(cbb.get(), (const uint8_t *)" ", 1) ||
307 !cbb_add_hex(cbb.get(), ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
308 !CBB_add_bytes(cbb.get(), (const uint8_t *)" ", 1) ||
309 !cbb_add_hex(cbb.get(), secret, secret_len) ||
310 !CBB_add_u8(cbb.get(), 0 /* NUL */) ||
311 !CBB_finish(cbb.get(), &out, &out_len)) {
David Benjamin86e95b82017-07-18 16:34:25 -0400312 return 0;
313 }
314
315 ssl->ctx->keylog_callback(ssl, (const char *)out);
316 OPENSSL_free(out);
317 return 1;
318}
319
320int ssl3_can_false_start(const SSL *ssl) {
321 const SSL_CIPHER *const cipher = SSL_get_current_cipher(ssl);
322
David Benjaminc11ea9422017-08-29 16:33:21 -0400323 // False Start only for TLS 1.2 with an ECDHE+AEAD cipher and ALPN or NPN.
David Benjamin86e95b82017-07-18 16:34:25 -0400324 return !SSL_is_dtls(ssl) &&
325 SSL_version(ssl) == TLS1_2_VERSION &&
326 (ssl->s3->alpn_selected != NULL ||
327 ssl->s3->next_proto_negotiated != NULL) &&
328 cipher != NULL &&
329 cipher->algorithm_mkey == SSL_kECDHE &&
330 cipher->algorithm_mac == SSL_AEAD;
331}
332
333void ssl_do_info_callback(const SSL *ssl, int type, int value) {
334 void (*cb)(const SSL *ssl, int type, int value) = NULL;
335 if (ssl->info_callback != NULL) {
336 cb = ssl->info_callback;
337 } else if (ssl->ctx->info_callback != NULL) {
338 cb = ssl->ctx->info_callback;
339 }
340
341 if (cb != NULL) {
342 cb(ssl, type, value);
343 }
344}
345
346void ssl_do_msg_callback(SSL *ssl, int is_write, int content_type,
347 const void *buf, size_t len) {
348 if (ssl->msg_callback == NULL) {
349 return;
350 }
351
David Benjaminc11ea9422017-08-29 16:33:21 -0400352 // |version| is zero when calling for |SSL3_RT_HEADER| and |SSL2_VERSION| for
353 // a V2ClientHello.
David Benjamin86e95b82017-07-18 16:34:25 -0400354 int version;
355 switch (content_type) {
356 case 0:
David Benjaminc11ea9422017-08-29 16:33:21 -0400357 // V2ClientHello
David Benjamin86e95b82017-07-18 16:34:25 -0400358 version = SSL2_VERSION;
359 break;
360 case SSL3_RT_HEADER:
361 version = 0;
362 break;
363 default:
364 version = SSL_version(ssl);
365 }
366
367 ssl->msg_callback(is_write, version, content_type, buf, len, ssl,
368 ssl->msg_callback_arg);
369}
370
371void ssl_get_current_time(const SSL *ssl, struct OPENSSL_timeval *out_clock) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400372 // TODO(martinkr): Change callers to |ssl_ctx_get_current_time| and drop the
373 // |ssl| arg from |current_time_cb| if possible.
Martin Kreichgauer72912d22017-08-04 12:06:43 -0700374 ssl_ctx_get_current_time(ssl->ctx, out_clock);
375}
376
377void ssl_ctx_get_current_time(const SSL_CTX *ctx,
378 struct OPENSSL_timeval *out_clock) {
379 if (ctx->current_time_cb != NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400380 // TODO(davidben): Update current_time_cb to use OPENSSL_timeval. See
381 // https://crbug.com/boringssl/155.
David Benjamin86e95b82017-07-18 16:34:25 -0400382 struct timeval clock;
Martin Kreichgauer72912d22017-08-04 12:06:43 -0700383 ctx->current_time_cb(nullptr /* ssl */, &clock);
David Benjamin86e95b82017-07-18 16:34:25 -0400384 if (clock.tv_sec < 0) {
385 assert(0);
386 out_clock->tv_sec = 0;
387 out_clock->tv_usec = 0;
388 } else {
389 out_clock->tv_sec = (uint64_t)clock.tv_sec;
390 out_clock->tv_usec = (uint32_t)clock.tv_usec;
391 }
392 return;
393 }
394
395#if defined(BORINGSSL_UNSAFE_DETERMINISTIC_MODE)
396 out_clock->tv_sec = 1234;
397 out_clock->tv_usec = 1234;
398#elif defined(OPENSSL_WINDOWS)
399 struct _timeb time;
400 _ftime(&time);
401 if (time.time < 0) {
402 assert(0);
403 out_clock->tv_sec = 0;
404 out_clock->tv_usec = 0;
405 } else {
406 out_clock->tv_sec = time.time;
407 out_clock->tv_usec = time.millitm * 1000;
408 }
409#else
410 struct timeval clock;
411 gettimeofday(&clock, NULL);
412 if (clock.tv_sec < 0) {
413 assert(0);
414 out_clock->tv_sec = 0;
415 out_clock->tv_usec = 0;
416 } else {
417 out_clock->tv_sec = (uint64_t)clock.tv_sec;
418 out_clock->tv_usec = (uint32_t)clock.tv_usec;
419 }
420#endif
421}
422
423} // namespace bssl
424
425using namespace bssl;
426
David Benjamin7c6d09b2015-09-05 11:00:19 -0400427int SSL_library_init(void) {
428 CRYPTO_library_init();
429 return 1;
430}
431
Adam Langleye976e432016-02-15 14:01:10 -0800432static uint32_t ssl_session_hash(const SSL_SESSION *sess) {
433 const uint8_t *session_id = sess->session_id;
434
435 uint8_t tmp_storage[sizeof(uint32_t)];
436 if (sess->session_id_length < sizeof(tmp_storage)) {
David Benjamin17cf2cb2016-12-13 01:07:13 -0500437 OPENSSL_memset(tmp_storage, 0, sizeof(tmp_storage));
438 OPENSSL_memcpy(tmp_storage, sess->session_id, sess->session_id_length);
Adam Langleye976e432016-02-15 14:01:10 -0800439 session_id = tmp_storage;
440 }
441
David Benjamin27bbae42015-09-13 00:54:37 -0400442 uint32_t hash =
Adam Langleye976e432016-02-15 14:01:10 -0800443 ((uint32_t)session_id[0]) |
444 ((uint32_t)session_id[1] << 8) |
445 ((uint32_t)session_id[2] << 16) |
446 ((uint32_t)session_id[3] << 24);
David Benjamin7c6d09b2015-09-05 11:00:19 -0400447
David Benjamin27bbae42015-09-13 00:54:37 -0400448 return hash;
449}
450
David Benjaminc11ea9422017-08-29 16:33:21 -0400451// NB: If this function (or indeed the hash function which uses a sort of
452// coarser function than this one) is changed, ensure
453// SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
454// able to construct an SSL_SESSION that will collide with any existing session
455// with a matching session ID.
David Benjamin27bbae42015-09-13 00:54:37 -0400456static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
457 if (a->ssl_version != b->ssl_version) {
458 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800459 }
Adam Langley95c29f32014-06-20 12:00:00 -0700460
David Benjamin27bbae42015-09-13 00:54:37 -0400461 if (a->session_id_length != b->session_id_length) {
462 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800463 }
Adam Langley95c29f32014-06-20 12:00:00 -0700464
David Benjamin17cf2cb2016-12-13 01:07:13 -0500465 return OPENSSL_memcmp(a->session_id, b->session_id, a->session_id_length);
David Benjamin27bbae42015-09-13 00:54:37 -0400466}
Adam Langley95c29f32014-06-20 12:00:00 -0700467
David Benjamin27bbae42015-09-13 00:54:37 -0400468SSL_CTX *SSL_CTX_new(const SSL_METHOD *method) {
469 SSL_CTX *ret = NULL;
470
471 if (method == NULL) {
472 OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_METHOD_PASSED);
473 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800474 }
Adam Langley95c29f32014-06-20 12:00:00 -0700475
David Benjaminf5260812017-07-12 17:16:39 -0400476 ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
David Benjamin27bbae42015-09-13 00:54:37 -0400477 if (ret == NULL) {
478 goto err;
David Benjamin62fd1622015-01-11 13:30:01 -0500479 }
480
David Benjamin17cf2cb2016-12-13 01:07:13 -0500481 OPENSSL_memset(ret, 0, sizeof(SSL_CTX));
Adam Langley95c29f32014-06-20 12:00:00 -0700482
David Benjamin27bbae42015-09-13 00:54:37 -0400483 ret->method = method->method;
Adam Langley3509dac2017-02-01 11:59:18 -0800484 ret->x509_method = method->x509_method;
David Benjamin27bbae42015-09-13 00:54:37 -0400485
486 CRYPTO_MUTEX_init(&ret->lock);
487
488 ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
489 ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
490
David Benjamin27bbae42015-09-13 00:54:37 -0400491 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
David Benjamin17b30832017-01-28 14:00:32 -0500492 ret->session_psk_dhe_timeout = SSL_DEFAULT_SESSION_PSK_DHE_TIMEOUT;
David Benjamin27bbae42015-09-13 00:54:37 -0400493
494 ret->references = 1;
495
496 ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
497 ret->verify_mode = SSL_VERIFY_NONE;
Adam Langley3509dac2017-02-01 11:59:18 -0800498 ret->cert = ssl_cert_new(method->x509_method);
David Benjamin27bbae42015-09-13 00:54:37 -0400499 if (ret->cert == NULL) {
500 goto err;
501 }
502
503 ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
504 if (ret->sessions == NULL) {
505 goto err;
506 }
Adam Langley2a3b3432017-02-10 13:56:48 -0800507
508 if (!ret->x509_method->ssl_ctx_new(ret)) {
David Benjamin27bbae42015-09-13 00:54:37 -0400509 goto err;
510 }
511
Matthew Braithwaite6ad20dc2017-02-21 16:41:33 -0800512 if (!SSL_CTX_set_strict_cipher_list(ret, SSL_DEFAULT_CIPHER_LIST)) {
David Benjamin27bbae42015-09-13 00:54:37 -0400513 goto err2;
514 }
515
Adam Langley34b4c822017-02-02 10:57:17 -0800516 ret->client_CA = sk_CRYPTO_BUFFER_new_null();
David Benjamin27bbae42015-09-13 00:54:37 -0400517 if (ret->client_CA == NULL) {
518 goto err;
519 }
520
David Benjamin8a589332015-12-04 23:14:35 -0500521 CRYPTO_new_ex_data(&ret->ex_data);
David Benjamin27bbae42015-09-13 00:54:37 -0400522
523 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
524
David Benjaminc11ea9422017-08-29 16:33:21 -0400525 // Disable the auto-chaining feature by default. Once this has stuck without
526 // problems, the feature will be removed entirely.
David Benjamina983b4c2016-11-09 14:21:12 -0500527 ret->mode = SSL_MODE_NO_AUTO_CHAIN;
528
David Benjaminc11ea9422017-08-29 16:33:21 -0400529 // Lock the SSL_CTX to the specified version, for compatibility with legacy
530 // uses of SSL_METHOD, but we do not set the minimum version for
531 // |SSLv3_method|.
David Benjamine34bcc92016-09-21 16:53:09 -0400532 if (!SSL_CTX_set_max_proto_version(ret, method->version) ||
David Benjamin3cfeb952017-03-01 16:48:38 -0500533 !SSL_CTX_set_min_proto_version(ret, method->version == SSL3_VERSION
David Benjaminc11ea9422017-08-29 16:33:21 -0400534 ? 0 // default
David Benjamin3cfeb952017-03-01 16:48:38 -0500535 : method->version)) {
David Benjamine34bcc92016-09-21 16:53:09 -0400536 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
537 goto err2;
David Benjamin27bbae42015-09-13 00:54:37 -0400538 }
539
540 return ret;
541
542err:
543 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
544err2:
545 SSL_CTX_free(ret);
546 return NULL;
547}
548
Adam Langley84cd1592016-06-30 18:29:44 -0700549int SSL_CTX_up_ref(SSL_CTX *ctx) {
550 CRYPTO_refcount_inc(&ctx->references);
551 return 1;
552}
553
David Benjamin27bbae42015-09-13 00:54:37 -0400554void SSL_CTX_free(SSL_CTX *ctx) {
555 if (ctx == NULL ||
556 !CRYPTO_refcount_dec_and_test_zero(&ctx->references)) {
557 return;
558 }
559
David Benjaminc11ea9422017-08-29 16:33:21 -0400560 // Free internal session cache. However: the remove_cb() may reference the
561 // ex_data of SSL_CTX, thus the ex_data store can only be removed after the
562 // sessions were flushed. As the ex_data handling routines might also touch
563 // the session cache, the most secure solution seems to be: empty (flush) the
564 // cache, then free ex_data, then finally free the cache. (See ticket
565 // [openssl.org #212].)
David Benjamin27bbae42015-09-13 00:54:37 -0400566 SSL_CTX_flush_sessions(ctx, 0);
567
568 CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, ctx, &ctx->ex_data);
569
570 CRYPTO_MUTEX_cleanup(&ctx->lock);
571 lh_SSL_SESSION_free(ctx->sessions);
David Benjamin27bbae42015-09-13 00:54:37 -0400572 ssl_cipher_preference_list_free(ctx->cipher_list);
David Benjamin27bbae42015-09-13 00:54:37 -0400573 ssl_cert_free(ctx->cert);
574 sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->client_custom_extensions,
575 SSL_CUSTOM_EXTENSION_free);
576 sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->server_custom_extensions,
577 SSL_CUSTOM_EXTENSION_free);
Adam Langley34b4c822017-02-02 10:57:17 -0800578 sk_CRYPTO_BUFFER_pop_free(ctx->client_CA, CRYPTO_BUFFER_free);
Adam Langley2a3b3432017-02-10 13:56:48 -0800579 ctx->x509_method->ssl_ctx_free(ctx);
David Benjamin27bbae42015-09-13 00:54:37 -0400580 sk_SRTP_PROTECTION_PROFILE_free(ctx->srtp_profiles);
581 OPENSSL_free(ctx->psk_identity_hint);
Steven Valdezce902a92016-05-17 11:47:53 -0400582 OPENSSL_free(ctx->supported_group_list);
David Benjamin27bbae42015-09-13 00:54:37 -0400583 OPENSSL_free(ctx->alpn_client_proto_list);
David Benjamin27bbae42015-09-13 00:54:37 -0400584 EVP_PKEY_free(ctx->tlsext_channel_id_private);
David Benjamin71c21b42017-04-14 17:05:40 -0400585 OPENSSL_free(ctx->verify_sigalgs);
Martin Kreichgauer72912d22017-08-04 12:06:43 -0700586 OPENSSL_free(ctx->tlsext_ticket_key_current);
587 OPENSSL_free(ctx->tlsext_ticket_key_prev);
David Benjamin27bbae42015-09-13 00:54:37 -0400588
589 OPENSSL_free(ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -0800590}
Adam Langley95c29f32014-06-20 12:00:00 -0700591
Adam Langleyfcf25832014-12-18 17:42:32 -0800592SSL *SSL_new(SSL_CTX *ctx) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800593 if (ctx == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400594 OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_CTX);
Adam Langleyfcf25832014-12-18 17:42:32 -0800595 return NULL;
596 }
597 if (ctx->method == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400598 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
Adam Langleyfcf25832014-12-18 17:42:32 -0800599 return NULL;
600 }
Adam Langley95c29f32014-06-20 12:00:00 -0700601
David Benjaminf5260812017-07-12 17:16:39 -0400602 SSL *ssl = (SSL *)OPENSSL_malloc(sizeof(SSL));
David Benjamin0d56f882015-12-19 17:05:56 -0500603 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800604 goto err;
605 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500606 OPENSSL_memset(ssl, 0, sizeof(SSL));
Adam Langley95c29f32014-06-20 12:00:00 -0700607
David Benjaminfc08dfc2017-06-20 14:39:32 -0400608 ssl->conf_min_version = ctx->conf_min_version;
609 ssl->conf_max_version = ctx->conf_max_version;
Steven Valdez52586f92017-07-11 15:08:32 -0400610 ssl->tls13_variant = ctx->tls13_variant;
David Benjamin1eb367c2014-12-12 18:17:51 -0500611
David Benjaminc11ea9422017-08-29 16:33:21 -0400612 // RFC 6347 states that implementations SHOULD use an initial timer value of
613 // 1 second.
Taylor Brandstetter376a0fe2016-05-10 19:30:28 -0700614 ssl->initial_timeout_duration_ms = 1000;
615
David Benjamin0d56f882015-12-19 17:05:56 -0500616 ssl->options = ctx->options;
617 ssl->mode = ctx->mode;
618 ssl->max_cert_list = ctx->max_cert_list;
Adam Langley95c29f32014-06-20 12:00:00 -0700619
David Benjamin0d56f882015-12-19 17:05:56 -0500620 ssl->cert = ssl_cert_dup(ctx->cert);
621 if (ssl->cert == NULL) {
David Benjamina5a3eeb2015-03-18 20:26:30 -0400622 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -0800623 }
Adam Langley95c29f32014-06-20 12:00:00 -0700624
David Benjamin0d56f882015-12-19 17:05:56 -0500625 ssl->msg_callback = ctx->msg_callback;
626 ssl->msg_callback_arg = ctx->msg_callback_arg;
627 ssl->verify_mode = ctx->verify_mode;
David Benjamin0d56f882015-12-19 17:05:56 -0500628 ssl->verify_callback = ctx->default_verify_callback;
David Benjamin3a1dd462017-07-11 16:13:10 -0400629 ssl->custom_verify_callback = ctx->custom_verify_callback;
David Benjaminbbaf3672016-11-17 10:53:09 +0900630 ssl->retain_only_sha256_of_client_certs =
631 ctx->retain_only_sha256_of_client_certs;
Adam Langley95c29f32014-06-20 12:00:00 -0700632
David Benjamin0d56f882015-12-19 17:05:56 -0500633 ssl->quiet_shutdown = ctx->quiet_shutdown;
634 ssl->max_send_fragment = ctx->max_send_fragment;
Adam Langley95c29f32014-06-20 12:00:00 -0700635
David Benjaminf650c712016-12-22 15:54:51 -0500636 SSL_CTX_up_ref(ctx);
David Benjamin0d56f882015-12-19 17:05:56 -0500637 ssl->ctx = ctx;
David Benjaminf650c712016-12-22 15:54:51 -0500638 SSL_CTX_up_ref(ctx);
David Benjaminbe497062017-03-10 16:08:36 -0500639 ssl->session_ctx = ctx;
Adam Langley95c29f32014-06-20 12:00:00 -0700640
Adam Langley2a3b3432017-02-10 13:56:48 -0800641 if (!ssl->ctx->x509_method->ssl_new(ssl)) {
642 goto err;
643 }
644
Steven Valdezce902a92016-05-17 11:47:53 -0400645 if (ctx->supported_group_list) {
David Benjaminf5260812017-07-12 17:16:39 -0400646 ssl->supported_group_list = (uint16_t *)BUF_memdup(
647 ctx->supported_group_list, ctx->supported_group_list_len * 2);
Steven Valdezce902a92016-05-17 11:47:53 -0400648 if (!ssl->supported_group_list) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800649 goto err;
650 }
Steven Valdezce902a92016-05-17 11:47:53 -0400651 ssl->supported_group_list_len = ctx->supported_group_list_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800652 }
Adam Langley95c29f32014-06-20 12:00:00 -0700653
David Benjamin17b30832017-01-28 14:00:32 -0500654 if (ctx->alpn_client_proto_list) {
David Benjaminf5260812017-07-12 17:16:39 -0400655 ssl->alpn_client_proto_list = (uint8_t *)BUF_memdup(
656 ctx->alpn_client_proto_list, ctx->alpn_client_proto_list_len);
David Benjamin0d56f882015-12-19 17:05:56 -0500657 if (ssl->alpn_client_proto_list == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800658 goto err;
659 }
David Benjamin17b30832017-01-28 14:00:32 -0500660 ssl->alpn_client_proto_list_len = ctx->alpn_client_proto_list_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800661 }
Adam Langley95c29f32014-06-20 12:00:00 -0700662
David Benjamin0d56f882015-12-19 17:05:56 -0500663 ssl->method = ctx->method;
Adam Langley95c29f32014-06-20 12:00:00 -0700664
David Benjamin0d56f882015-12-19 17:05:56 -0500665 if (!ssl->method->ssl_new(ssl)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800666 goto err;
667 }
Adam Langley95c29f32014-06-20 12:00:00 -0700668
David Benjamin0d56f882015-12-19 17:05:56 -0500669 ssl->rwstate = SSL_NOTHING;
Adam Langley95c29f32014-06-20 12:00:00 -0700670
David Benjamin0d56f882015-12-19 17:05:56 -0500671 CRYPTO_new_ex_data(&ssl->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700672
David Benjamin0d56f882015-12-19 17:05:56 -0500673 ssl->psk_identity_hint = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800674 if (ctx->psk_identity_hint) {
David Benjamin0d56f882015-12-19 17:05:56 -0500675 ssl->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
676 if (ssl->psk_identity_hint == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800677 goto err;
678 }
679 }
David Benjamin0d56f882015-12-19 17:05:56 -0500680 ssl->psk_client_callback = ctx->psk_client_callback;
681 ssl->psk_server_callback = ctx->psk_server_callback;
Adam Langley95c29f32014-06-20 12:00:00 -0700682
David Benjamin0d56f882015-12-19 17:05:56 -0500683 ssl->tlsext_channel_id_enabled = ctx->tlsext_channel_id_enabled;
David Benjamin02ddbfd2015-01-11 13:09:11 -0500684 if (ctx->tlsext_channel_id_private) {
Adam Langley310d3f62016-07-12 10:39:20 -0700685 EVP_PKEY_up_ref(ctx->tlsext_channel_id_private);
686 ssl->tlsext_channel_id_private = ctx->tlsext_channel_id_private;
David Benjamin02ddbfd2015-01-11 13:09:11 -0500687 }
688
David Benjamin17b30832017-01-28 14:00:32 -0500689 ssl->signed_cert_timestamps_enabled = ctx->signed_cert_timestamps_enabled;
690 ssl->ocsp_stapling_enabled = ctx->ocsp_stapling_enabled;
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200691
David Benjamin0d56f882015-12-19 17:05:56 -0500692 return ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -0800693
Adam Langley95c29f32014-06-20 12:00:00 -0700694err:
David Benjamin0d56f882015-12-19 17:05:56 -0500695 SSL_free(ssl);
David Benjamin3570d732015-06-29 00:28:17 -0400696 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700697
Adam Langleyfcf25832014-12-18 17:42:32 -0800698 return NULL;
699}
Adam Langley95c29f32014-06-20 12:00:00 -0700700
David Benjamin27bbae42015-09-13 00:54:37 -0400701void SSL_free(SSL *ssl) {
702 if (ssl == NULL) {
703 return;
704 }
705
David Benjamin0391f162017-06-15 15:27:59 -0400706 if (ssl->ctx != NULL) {
707 ssl->ctx->x509_method->ssl_free(ssl);
708 }
709
David Benjamin27bbae42015-09-13 00:54:37 -0400710 CRYPTO_free_ex_data(&g_ex_data_class_ssl, ssl, &ssl->ex_data);
711
David Benjamin27bbae42015-09-13 00:54:37 -0400712 BIO_free_all(ssl->rbio);
David Benjaminf715c422016-06-11 19:01:56 -0400713 BIO_free_all(ssl->wbio);
David Benjamin27bbae42015-09-13 00:54:37 -0400714
715 BUF_MEM_free(ssl->init_buf);
716
David Benjaminc11ea9422017-08-29 16:33:21 -0400717 // add extra stuff
David Benjamin27bbae42015-09-13 00:54:37 -0400718 ssl_cipher_preference_list_free(ssl->cipher_list);
David Benjamin27bbae42015-09-13 00:54:37 -0400719
David Benjamin27bbae42015-09-13 00:54:37 -0400720 SSL_SESSION_free(ssl->session);
721
David Benjamin27bbae42015-09-13 00:54:37 -0400722 ssl_cert_free(ssl->cert);
723
724 OPENSSL_free(ssl->tlsext_hostname);
David Benjaminbe497062017-03-10 16:08:36 -0500725 SSL_CTX_free(ssl->session_ctx);
Steven Valdezce902a92016-05-17 11:47:53 -0400726 OPENSSL_free(ssl->supported_group_list);
David Benjamin27bbae42015-09-13 00:54:37 -0400727 OPENSSL_free(ssl->alpn_client_proto_list);
728 EVP_PKEY_free(ssl->tlsext_channel_id_private);
729 OPENSSL_free(ssl->psk_identity_hint);
Adam Langley34b4c822017-02-02 10:57:17 -0800730 sk_CRYPTO_BUFFER_pop_free(ssl->client_CA, CRYPTO_BUFFER_free);
David Benjamin27bbae42015-09-13 00:54:37 -0400731 sk_SRTP_PROTECTION_PROFILE_free(ssl->srtp_profiles);
732
733 if (ssl->method != NULL) {
734 ssl->method->ssl_free(ssl);
735 }
736 SSL_CTX_free(ssl->ctx);
737
738 OPENSSL_free(ssl);
739}
740
741void SSL_set_connect_state(SSL *ssl) {
742 ssl->server = 0;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400743 ssl->do_handshake = ssl_client_handshake;
David Benjamin27bbae42015-09-13 00:54:37 -0400744}
745
746void SSL_set_accept_state(SSL *ssl) {
747 ssl->server = 1;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400748 ssl->do_handshake = ssl_server_handshake;
David Benjamin27bbae42015-09-13 00:54:37 -0400749}
750
David Benjamin4501bd52016-08-01 13:39:41 -0400751void SSL_set0_rbio(SSL *ssl, BIO *rbio) {
David Benjaminf715c422016-06-11 19:01:56 -0400752 BIO_free_all(ssl->rbio);
753 ssl->rbio = rbio;
754}
755
David Benjamin4501bd52016-08-01 13:39:41 -0400756void SSL_set0_wbio(SSL *ssl, BIO *wbio) {
David Benjaminf715c422016-06-11 19:01:56 -0400757 BIO_free_all(ssl->wbio);
David Benjamin27bbae42015-09-13 00:54:37 -0400758 ssl->wbio = wbio;
759}
760
David Benjaminf715c422016-06-11 19:01:56 -0400761void SSL_set_bio(SSL *ssl, BIO *rbio, BIO *wbio) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400762 // For historical reasons, this function has many different cases in ownership
763 // handling.
David Benjaminf715c422016-06-11 19:01:56 -0400764
David Benjaminc11ea9422017-08-29 16:33:21 -0400765 // If nothing has changed, do nothing
David Benjamin4501bd52016-08-01 13:39:41 -0400766 if (rbio == SSL_get_rbio(ssl) && wbio == SSL_get_wbio(ssl)) {
767 return;
768 }
769
David Benjaminc11ea9422017-08-29 16:33:21 -0400770 // If the two arguments are equal, one fewer reference is granted than
771 // taken.
David Benjaminf715c422016-06-11 19:01:56 -0400772 if (rbio != NULL && rbio == wbio) {
773 BIO_up_ref(rbio);
774 }
775
David Benjaminc11ea9422017-08-29 16:33:21 -0400776 // If only the wbio is changed, adopt only one reference.
David Benjaminf715c422016-06-11 19:01:56 -0400777 if (rbio == SSL_get_rbio(ssl)) {
David Benjamin4501bd52016-08-01 13:39:41 -0400778 SSL_set0_wbio(ssl, wbio);
David Benjaminf715c422016-06-11 19:01:56 -0400779 return;
780 }
David Benjamin4501bd52016-08-01 13:39:41 -0400781
David Benjaminc11ea9422017-08-29 16:33:21 -0400782 // There is an asymmetry here for historical reasons. If only the rbio is
783 // changed AND the rbio and wbio were originally different, then we only adopt
784 // one reference.
David Benjamin4501bd52016-08-01 13:39:41 -0400785 if (wbio == SSL_get_wbio(ssl) && SSL_get_rbio(ssl) != SSL_get_wbio(ssl)) {
786 SSL_set0_rbio(ssl, rbio);
David Benjaminf715c422016-06-11 19:01:56 -0400787 return;
788 }
789
David Benjaminc11ea9422017-08-29 16:33:21 -0400790 // Otherwise, adopt both references.
David Benjamin4501bd52016-08-01 13:39:41 -0400791 SSL_set0_rbio(ssl, rbio);
792 SSL_set0_wbio(ssl, wbio);
David Benjaminf715c422016-06-11 19:01:56 -0400793}
794
David Benjamin27bbae42015-09-13 00:54:37 -0400795BIO *SSL_get_rbio(const SSL *ssl) { return ssl->rbio; }
796
David Benjamindaf207a2017-01-03 18:37:41 -0500797BIO *SSL_get_wbio(const SSL *ssl) { return ssl->wbio; }
David Benjamin27bbae42015-09-13 00:54:37 -0400798
David Benjamin2be4aa72017-01-02 07:56:51 -0500799int SSL_do_handshake(SSL *ssl) {
800 ssl_reset_error_state(ssl);
David Benjamin15c14882016-03-14 14:25:46 -0400801
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400802 if (ssl->do_handshake == NULL) {
David Benjamin27bbae42015-09-13 00:54:37 -0400803 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_TYPE_NOT_SET);
804 return -1;
805 }
806
807 if (!SSL_in_init(ssl)) {
808 return 1;
809 }
810
David Benjaminc11ea9422017-08-29 16:33:21 -0400811 // Run the handshake.
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400812 SSL_HANDSHAKE *hs = ssl->s3->hs;
813
David Benjamin74795b32017-08-31 15:13:12 -0400814 bool early_return = false;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400815 int ret = ssl_run_handshake(hs, &early_return);
816 ssl_do_info_callback(
817 ssl, ssl->server ? SSL_CB_ACCEPT_EXIT : SSL_CB_CONNECT_EXIT, ret);
David Benjamince8c9d22016-11-14 10:45:16 +0900818 if (ret <= 0) {
819 return ret;
820 }
821
David Benjaminc11ea9422017-08-29 16:33:21 -0400822 // Destroy the handshake object if the handshake has completely finished.
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400823 if (!early_return) {
David Benjamince8c9d22016-11-14 10:45:16 +0900824 ssl_handshake_free(ssl->s3->hs);
825 ssl->s3->hs = NULL;
826 }
827
828 return 1;
David Benjamin27bbae42015-09-13 00:54:37 -0400829}
830
831int SSL_connect(SSL *ssl) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400832 if (ssl->do_handshake == NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400833 // Not properly initialized yet
David Benjamin27bbae42015-09-13 00:54:37 -0400834 SSL_set_connect_state(ssl);
835 }
836
David Benjamin15c14882016-03-14 14:25:46 -0400837 return SSL_do_handshake(ssl);
David Benjamin27bbae42015-09-13 00:54:37 -0400838}
839
840int SSL_accept(SSL *ssl) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400841 if (ssl->do_handshake == NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400842 // Not properly initialized yet
David Benjamin27bbae42015-09-13 00:54:37 -0400843 SSL_set_accept_state(ssl);
844 }
845
David Benjamin15c14882016-03-14 14:25:46 -0400846 return SSL_do_handshake(ssl);
David Benjamin27bbae42015-09-13 00:54:37 -0400847}
848
David Benjamin7934f082017-08-01 16:32:25 -0400849static int ssl_do_post_handshake(SSL *ssl, const SSLMessage &msg) {
850 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
851 return tls13_post_handshake(ssl, msg);
852 }
853
David Benjaminc11ea9422017-08-29 16:33:21 -0400854 // We do not accept renegotiations as a server or SSL 3.0. SSL 3.0 will be
855 // removed entirely in the future and requires retaining more data for
856 // renegotiation_info.
David Benjamin34941c02016-10-08 11:45:31 -0400857 if (ssl->server || ssl->version == SSL3_VERSION) {
David Benjamin163f29a2016-07-28 11:05:58 -0400858 goto no_renegotiation;
859 }
860
David Benjamin7934f082017-08-01 16:32:25 -0400861 if (msg.type != SSL3_MT_HELLO_REQUEST || CBS_len(&msg.body) != 0) {
David Benjamin163f29a2016-07-28 11:05:58 -0400862 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
863 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HELLO_REQUEST);
864 return 0;
865 }
866
867 switch (ssl->renegotiate_mode) {
868 case ssl_renegotiate_ignore:
David Benjaminc11ea9422017-08-29 16:33:21 -0400869 // Ignore the HelloRequest.
David Benjamin163f29a2016-07-28 11:05:58 -0400870 return 1;
871
872 case ssl_renegotiate_once:
873 if (ssl->s3->total_renegotiations != 0) {
874 goto no_renegotiation;
875 }
876 break;
877
878 case ssl_renegotiate_never:
879 goto no_renegotiation;
880
881 case ssl_renegotiate_freely:
882 break;
883 }
884
David Benjaminc11ea9422017-08-29 16:33:21 -0400885 // Renegotiation is only supported at quiescent points in the application
886 // protocol, namely in HTTPS, just before reading the HTTP response. Require
887 // the record-layer be idle and avoid complexities of sending a handshake
888 // record while an application_data record is being written.
David Benjamin163f29a2016-07-28 11:05:58 -0400889 if (ssl_write_buffer_is_pending(ssl)) {
890 goto no_renegotiation;
891 }
892
David Benjaminc11ea9422017-08-29 16:33:21 -0400893 // Begin a new handshake.
David Benjamin2644a132016-12-11 13:41:17 -0500894 if (ssl->s3->hs != NULL) {
895 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
896 return 0;
897 }
898 ssl->s3->hs = ssl_handshake_new(ssl);
899 if (ssl->s3->hs == NULL) {
900 return 0;
901 }
902
David Benjamin163f29a2016-07-28 11:05:58 -0400903 ssl->s3->total_renegotiations++;
David Benjamin163f29a2016-07-28 11:05:58 -0400904 return 1;
905
906no_renegotiation:
907 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_NO_RENEGOTIATION);
908 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION);
909 return 0;
910}
911
David Benjamin15c14882016-03-14 14:25:46 -0400912static int ssl_read_impl(SSL *ssl, void *buf, int num, int peek) {
David Benjamin2be4aa72017-01-02 07:56:51 -0500913 ssl_reset_error_state(ssl);
David Benjamin15c14882016-03-14 14:25:46 -0400914
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400915 if (ssl->do_handshake == NULL) {
David Benjamin27bbae42015-09-13 00:54:37 -0400916 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
917 return -1;
918 }
919
David Benjamin163f29a2016-07-28 11:05:58 -0400920 for (;;) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400921 // Complete the current handshake, if any. False Start will cause
922 // |SSL_do_handshake| to return mid-handshake, so this may require multiple
923 // iterations.
Steven Valdez681eb6a2016-12-19 13:19:29 -0500924 while (!ssl_can_read(ssl)) {
David Benjamin163f29a2016-07-28 11:05:58 -0400925 int ret = SSL_do_handshake(ssl);
926 if (ret < 0) {
927 return ret;
928 }
929 if (ret == 0) {
930 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
931 return -1;
932 }
933 }
934
David Benjamin74795b32017-08-31 15:13:12 -0400935 bool got_handshake = false;
David Benjaminf5260812017-07-12 17:16:39 -0400936 int ret = ssl->method->read_app_data(ssl, &got_handshake, (uint8_t *)buf,
937 num, peek);
David Benjamin163f29a2016-07-28 11:05:58 -0400938 if (ret > 0 || !got_handshake) {
Steven Valdez32635b82016-08-16 11:25:03 -0400939 ssl->s3->key_update_count = 0;
David Benjaminc79845c2016-03-10 01:28:00 -0500940 return ret;
941 }
David Benjamin163f29a2016-07-28 11:05:58 -0400942
David Benjaminc11ea9422017-08-29 16:33:21 -0400943 // If we received an interrupt in early read (the end_of_early_data alert),
944 // loop again for the handshake to process it.
Steven Valdez681eb6a2016-12-19 13:19:29 -0500945 if (SSL_in_init(ssl)) {
946 continue;
947 }
948
David Benjamin7934f082017-08-01 16:32:25 -0400949 SSLMessage msg;
950 while (ssl->method->get_message(ssl, &msg)) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400951 // Handle the post-handshake message and try again.
David Benjamin7934f082017-08-01 16:32:25 -0400952 if (!ssl_do_post_handshake(ssl, msg)) {
953 return -1;
954 }
955 ssl->method->next_message(ssl);
David Benjaminc79845c2016-03-10 01:28:00 -0500956 }
957 }
David Benjamin15c14882016-03-14 14:25:46 -0400958}
959
960int SSL_read(SSL *ssl, void *buf, int num) {
961 return ssl_read_impl(ssl, buf, num, 0 /* consume bytes */);
David Benjamin27bbae42015-09-13 00:54:37 -0400962}
963
964int SSL_peek(SSL *ssl, void *buf, int num) {
David Benjamin15c14882016-03-14 14:25:46 -0400965 return ssl_read_impl(ssl, buf, num, 1 /* peek */);
David Benjamin27bbae42015-09-13 00:54:37 -0400966}
967
968int SSL_write(SSL *ssl, const void *buf, int num) {
David Benjamin2be4aa72017-01-02 07:56:51 -0500969 ssl_reset_error_state(ssl);
David Benjamin15c14882016-03-14 14:25:46 -0400970
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400971 if (ssl->do_handshake == NULL) {
David Benjamin27bbae42015-09-13 00:54:37 -0400972 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
973 return -1;
974 }
975
David Benjaminfa214e42016-05-10 17:03:10 -0400976 if (ssl->s3->send_shutdown != ssl_shutdown_none) {
David Benjamin27bbae42015-09-13 00:54:37 -0400977 OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
978 return -1;
979 }
980
David Benjamin74795b32017-08-31 15:13:12 -0400981 int ret = 0;
982 bool needs_handshake = false;
Steven Valdeze831a812017-03-09 14:56:07 -0500983 do {
David Benjaminc11ea9422017-08-29 16:33:21 -0400984 // If necessary, complete the handshake implicitly.
Steven Valdeze831a812017-03-09 14:56:07 -0500985 if (!ssl_can_write(ssl)) {
986 ret = SSL_do_handshake(ssl);
987 if (ret < 0) {
988 return ret;
989 }
990 if (ret == 0) {
991 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
992 return -1;
993 }
David Benjamind7ac1432016-03-10 00:41:25 -0500994 }
David Benjamind7ac1432016-03-10 00:41:25 -0500995
David Benjaminf5260812017-07-12 17:16:39 -0400996 ret = ssl->method->write_app_data(ssl, &needs_handshake,
997 (const uint8_t *)buf, num);
Steven Valdeze831a812017-03-09 14:56:07 -0500998 } while (needs_handshake);
999 return ret;
David Benjamin27bbae42015-09-13 00:54:37 -04001000}
1001
1002int SSL_shutdown(SSL *ssl) {
David Benjamin2be4aa72017-01-02 07:56:51 -05001003 ssl_reset_error_state(ssl);
David Benjamin15c14882016-03-14 14:25:46 -04001004
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001005 if (ssl->do_handshake == NULL) {
David Benjamin27bbae42015-09-13 00:54:37 -04001006 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
1007 return -1;
1008 }
1009
David Benjaminc11ea9422017-08-29 16:33:21 -04001010 // If we are in the middle of a handshake, silently succeed. Consumers often
1011 // call this function before |SSL_free|, whether the handshake succeeded or
1012 // not. We assume the caller has already handled failed handshakes.
Steven Valdeza14934f2016-02-29 10:05:08 -05001013 if (SSL_in_init(ssl)) {
David Benjamin12709db2017-02-17 13:51:50 -05001014 return 1;
Steven Valdeza14934f2016-02-29 10:05:08 -05001015 }
1016
David Benjamin27bbae42015-09-13 00:54:37 -04001017 if (ssl->quiet_shutdown) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001018 // Do nothing if configured not to send a close_notify.
David Benjaminfa214e42016-05-10 17:03:10 -04001019 ssl->s3->send_shutdown = ssl_shutdown_close_notify;
1020 ssl->s3->recv_shutdown = ssl_shutdown_close_notify;
David Benjamin27bbae42015-09-13 00:54:37 -04001021 return 1;
1022 }
1023
David Benjaminc11ea9422017-08-29 16:33:21 -04001024 // This function completes in two stages. It sends a close_notify and then it
1025 // waits for a close_notify to come in. Perform exactly one action and return
1026 // whether or not it succeeds.
David Benjamin27bbae42015-09-13 00:54:37 -04001027
David Benjaminfa214e42016-05-10 17:03:10 -04001028 if (ssl->s3->send_shutdown != ssl_shutdown_close_notify) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001029 // Send a close_notify.
David Benjaminfa214e42016-05-10 17:03:10 -04001030 if (ssl3_send_alert(ssl, SSL3_AL_WARNING, SSL_AD_CLOSE_NOTIFY) <= 0) {
1031 return -1;
David Benjamin27bbae42015-09-13 00:54:37 -04001032 }
1033 } else if (ssl->s3->alert_dispatch) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001034 // Finish sending the close_notify.
David Benjaminf0ee9072016-06-15 17:44:37 -04001035 if (ssl->method->dispatch_alert(ssl) <= 0) {
David Benjaminfa214e42016-05-10 17:03:10 -04001036 return -1;
David Benjamin27bbae42015-09-13 00:54:37 -04001037 }
David Benjaminfa214e42016-05-10 17:03:10 -04001038 } else if (ssl->s3->recv_shutdown != ssl_shutdown_close_notify) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001039 // Wait for the peer's close_notify.
David Benjaminf0ee9072016-06-15 17:44:37 -04001040 ssl->method->read_close_notify(ssl);
David Benjaminfa214e42016-05-10 17:03:10 -04001041 if (ssl->s3->recv_shutdown != ssl_shutdown_close_notify) {
1042 return -1;
David Benjamin27bbae42015-09-13 00:54:37 -04001043 }
1044 }
1045
David Benjaminc11ea9422017-08-29 16:33:21 -04001046 // Return 0 for unidirectional shutdown and 1 for bidirectional shutdown.
David Benjaminfa214e42016-05-10 17:03:10 -04001047 return ssl->s3->recv_shutdown == ssl_shutdown_close_notify;
David Benjamin27bbae42015-09-13 00:54:37 -04001048}
1049
David Benjamin1d4f4c02016-07-26 18:03:08 -04001050int SSL_send_fatal_alert(SSL *ssl, uint8_t alert) {
1051 if (ssl->s3->alert_dispatch) {
1052 if (ssl->s3->send_alert[0] != SSL3_AL_FATAL ||
1053 ssl->s3->send_alert[1] != alert) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001054 // We are already attempting to write a different alert.
David Benjamin1d4f4c02016-07-26 18:03:08 -04001055 OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
1056 return -1;
1057 }
1058 return ssl->method->dispatch_alert(ssl);
1059 }
1060
1061 return ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
1062}
1063
Steven Valdez08b65f42016-12-07 15:29:45 -05001064void SSL_CTX_set_early_data_enabled(SSL_CTX *ctx, int enabled) {
Alessandro Ghedini67bb45f2017-03-30 16:33:24 -05001065 ctx->cert->enable_early_data = !!enabled;
1066}
1067
Steven Valdez520e1222017-06-13 12:45:25 -04001068void SSL_CTX_set_tls13_variant(SSL_CTX *ctx, enum tls13_variant_t variant) {
1069 ctx->tls13_variant = variant;
1070}
1071
Steven Valdez52586f92017-07-11 15:08:32 -04001072void SSL_set_tls13_variant(SSL *ssl, enum tls13_variant_t variant) {
1073 ssl->tls13_variant = variant;
1074}
1075
Alessandro Ghedini67bb45f2017-03-30 16:33:24 -05001076void SSL_set_early_data_enabled(SSL *ssl, int enabled) {
1077 ssl->cert->enable_early_data = !!enabled;
Steven Valdez08b65f42016-12-07 15:29:45 -05001078}
1079
Steven Valdeze831a812017-03-09 14:56:07 -05001080int SSL_in_early_data(const SSL *ssl) {
1081 if (ssl->s3->hs == NULL) {
1082 return 0;
1083 }
1084 return ssl->s3->hs->in_early_data;
1085}
1086
Steven Valdez2d850622017-01-11 11:34:52 -05001087int SSL_early_data_accepted(const SSL *ssl) {
1088 return ssl->early_data_accepted;
1089}
1090
Steven Valdeze831a812017-03-09 14:56:07 -05001091void SSL_reset_early_data_reject(SSL *ssl) {
1092 SSL_HANDSHAKE *hs = ssl->s3->hs;
1093 if (hs == NULL ||
1094 hs->wait != ssl_hs_early_data_rejected) {
1095 abort();
1096 }
1097
1098 hs->wait = ssl_hs_ok;
David Benjaminfd45ee72017-08-31 14:49:09 -04001099 hs->in_early_data = false;
David Benjamin31b0c9b2017-07-20 14:49:15 -04001100 hs->early_session.reset();
Steven Valdeze831a812017-03-09 14:56:07 -05001101
David Benjaminc11ea9422017-08-29 16:33:21 -04001102 // Discard any unfinished writes from the perspective of |SSL_write|'s
1103 // retry. The handshake will transparently flush out the pending record
1104 // (discarded by the server) to keep the framing correct.
David Benjamin046bc1f2017-08-31 15:06:42 -04001105 ssl->s3->wpend_pending = false;
Steven Valdeze831a812017-03-09 14:56:07 -05001106}
1107
David Benjamin287fc4f2016-12-10 15:12:03 -05001108static int bio_retry_reason_to_error(int reason) {
1109 switch (reason) {
1110 case BIO_RR_CONNECT:
1111 return SSL_ERROR_WANT_CONNECT;
1112 case BIO_RR_ACCEPT:
1113 return SSL_ERROR_WANT_ACCEPT;
1114 default:
1115 return SSL_ERROR_SYSCALL;
1116 }
1117}
David Benjamin27bbae42015-09-13 00:54:37 -04001118
David Benjamin287fc4f2016-12-10 15:12:03 -05001119int SSL_get_error(const SSL *ssl, int ret_code) {
David Benjamin27bbae42015-09-13 00:54:37 -04001120 if (ret_code > 0) {
1121 return SSL_ERROR_NONE;
1122 }
1123
David Benjaminc11ea9422017-08-29 16:33:21 -04001124 // Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
1125 // where we do encode the error
David Benjamin287fc4f2016-12-10 15:12:03 -05001126 uint32_t err = ERR_peek_error();
David Benjamin27bbae42015-09-13 00:54:37 -04001127 if (err != 0) {
1128 if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
1129 return SSL_ERROR_SYSCALL;
1130 }
1131 return SSL_ERROR_SSL;
1132 }
1133
1134 if (ret_code == 0) {
David Benjaminfa214e42016-05-10 17:03:10 -04001135 if (ssl->s3->recv_shutdown == ssl_shutdown_close_notify) {
David Benjamin27bbae42015-09-13 00:54:37 -04001136 return SSL_ERROR_ZERO_RETURN;
1137 }
David Benjaminc11ea9422017-08-29 16:33:21 -04001138 // An EOF was observed which violates the protocol, and the underlying
1139 // transport does not participate in the error queue. Bubble up to the
1140 // caller.
David Benjamin27bbae42015-09-13 00:54:37 -04001141 return SSL_ERROR_SYSCALL;
1142 }
1143
David Benjamin287fc4f2016-12-10 15:12:03 -05001144 switch (ssl->rwstate) {
1145 case SSL_PENDING_SESSION:
1146 return SSL_ERROR_PENDING_SESSION;
David Benjamin27bbae42015-09-13 00:54:37 -04001147
David Benjamin287fc4f2016-12-10 15:12:03 -05001148 case SSL_CERTIFICATE_SELECTION_PENDING:
1149 return SSL_ERROR_PENDING_CERTIFICATE;
David Benjamin27bbae42015-09-13 00:54:37 -04001150
David Benjamin287fc4f2016-12-10 15:12:03 -05001151 case SSL_READING: {
1152 BIO *bio = SSL_get_rbio(ssl);
1153 if (BIO_should_read(bio)) {
1154 return SSL_ERROR_WANT_READ;
David Benjamin27bbae42015-09-13 00:54:37 -04001155 }
1156
David Benjamin287fc4f2016-12-10 15:12:03 -05001157 if (BIO_should_write(bio)) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001158 // TODO(davidben): OpenSSL historically checked for writes on the read
1159 // BIO. Can this be removed?
David Benjamin287fc4f2016-12-10 15:12:03 -05001160 return SSL_ERROR_WANT_WRITE;
David Benjamin27bbae42015-09-13 00:54:37 -04001161 }
1162
David Benjamin287fc4f2016-12-10 15:12:03 -05001163 if (BIO_should_io_special(bio)) {
1164 return bio_retry_reason_to_error(BIO_get_retry_reason(bio));
David Benjamin27bbae42015-09-13 00:54:37 -04001165 }
1166
David Benjamin287fc4f2016-12-10 15:12:03 -05001167 break;
1168 }
1169
1170 case SSL_WRITING: {
1171 BIO *bio = SSL_get_wbio(ssl);
1172 if (BIO_should_write(bio)) {
1173 return SSL_ERROR_WANT_WRITE;
David Benjamin27bbae42015-09-13 00:54:37 -04001174 }
1175
David Benjamin287fc4f2016-12-10 15:12:03 -05001176 if (BIO_should_read(bio)) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001177 // TODO(davidben): OpenSSL historically checked for reads on the write
1178 // BIO. Can this be removed?
David Benjamin287fc4f2016-12-10 15:12:03 -05001179 return SSL_ERROR_WANT_READ;
1180 }
1181
1182 if (BIO_should_io_special(bio)) {
1183 return bio_retry_reason_to_error(BIO_get_retry_reason(bio));
1184 }
1185
1186 break;
David Benjamin27bbae42015-09-13 00:54:37 -04001187 }
David Benjamin27bbae42015-09-13 00:54:37 -04001188
David Benjamin287fc4f2016-12-10 15:12:03 -05001189 case SSL_X509_LOOKUP:
1190 return SSL_ERROR_WANT_X509_LOOKUP;
David Benjamin27bbae42015-09-13 00:54:37 -04001191
David Benjamin287fc4f2016-12-10 15:12:03 -05001192 case SSL_CHANNEL_ID_LOOKUP:
1193 return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
David Benjamin27bbae42015-09-13 00:54:37 -04001194
David Benjamin287fc4f2016-12-10 15:12:03 -05001195 case SSL_PRIVATE_KEY_OPERATION:
1196 return SSL_ERROR_WANT_PRIVATE_KEY_OPERATION;
Adam Langley4c341d02017-03-08 19:33:21 -08001197
1198 case SSL_PENDING_TICKET:
1199 return SSL_ERROR_PENDING_TICKET;
Steven Valdeze831a812017-03-09 14:56:07 -05001200
1201 case SSL_EARLY_DATA_REJECTED:
1202 return SSL_ERROR_EARLY_DATA_REJECTED;
David Benjamin3a1dd462017-07-11 16:13:10 -04001203
1204 case SSL_CERTIFICATE_VERIFY:
1205 return SSL_ERROR_WANT_CERTIFICATE_VERIFY;
David Benjamin27bbae42015-09-13 00:54:37 -04001206 }
1207
1208 return SSL_ERROR_SYSCALL;
1209}
1210
David Benjamin27bbae42015-09-13 00:54:37 -04001211uint32_t SSL_CTX_set_options(SSL_CTX *ctx, uint32_t options) {
1212 ctx->options |= options;
1213 return ctx->options;
1214}
1215
1216uint32_t SSL_CTX_clear_options(SSL_CTX *ctx, uint32_t options) {
1217 ctx->options &= ~options;
1218 return ctx->options;
1219}
1220
1221uint32_t SSL_CTX_get_options(const SSL_CTX *ctx) { return ctx->options; }
1222
1223uint32_t SSL_set_options(SSL *ssl, uint32_t options) {
1224 ssl->options |= options;
1225 return ssl->options;
1226}
1227
1228uint32_t SSL_clear_options(SSL *ssl, uint32_t options) {
1229 ssl->options &= ~options;
1230 return ssl->options;
1231}
1232
1233uint32_t SSL_get_options(const SSL *ssl) { return ssl->options; }
1234
1235uint32_t SSL_CTX_set_mode(SSL_CTX *ctx, uint32_t mode) {
1236 ctx->mode |= mode;
1237 return ctx->mode;
1238}
1239
1240uint32_t SSL_CTX_clear_mode(SSL_CTX *ctx, uint32_t mode) {
1241 ctx->mode &= ~mode;
1242 return ctx->mode;
1243}
1244
1245uint32_t SSL_CTX_get_mode(const SSL_CTX *ctx) { return ctx->mode; }
1246
1247uint32_t SSL_set_mode(SSL *ssl, uint32_t mode) {
1248 ssl->mode |= mode;
1249 return ssl->mode;
1250}
1251
1252uint32_t SSL_clear_mode(SSL *ssl, uint32_t mode) {
1253 ssl->mode &= ~mode;
1254 return ssl->mode;
1255}
1256
1257uint32_t SSL_get_mode(const SSL *ssl) { return ssl->mode; }
1258
Adam Langleyd519bf62016-12-12 11:16:44 -08001259void SSL_CTX_set0_buffer_pool(SSL_CTX *ctx, CRYPTO_BUFFER_POOL *pool) {
1260 ctx->pool = pool;
1261}
1262
David Benjaminee0c8272015-09-13 01:03:54 -04001263int SSL_get_tls_unique(const SSL *ssl, uint8_t *out, size_t *out_len,
1264 size_t max_out) {
David Benjaminf5260812017-07-12 17:16:39 -04001265 *out_len = 0;
1266 OPENSSL_memset(out, 0, max_out);
1267
David Benjaminc11ea9422017-08-29 16:33:21 -04001268 // tls-unique is not defined for SSL 3.0 or TLS 1.3.
David Benjaminced00b42016-10-08 11:24:09 -04001269 if (!ssl->s3->initial_handshake_complete ||
1270 ssl3_protocol_version(ssl) < TLS1_VERSION ||
1271 ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjaminf5260812017-07-12 17:16:39 -04001272 return 0;
David Benjaminced00b42016-10-08 11:24:09 -04001273 }
1274
David Benjaminc11ea9422017-08-29 16:33:21 -04001275 // The tls-unique value is the first Finished message in the handshake, which
1276 // is the client's in a full handshake and the server's for a resumption. See
1277 // https://tools.ietf.org/html/rfc5929#section-3.1.
David Benjaminee0c8272015-09-13 01:03:54 -04001278 const uint8_t *finished = ssl->s3->previous_client_finished;
1279 size_t finished_len = ssl->s3->previous_client_finished_len;
Steven Valdez87eab492016-06-27 16:34:59 -04001280 if (ssl->session != NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001281 // tls-unique is broken for resumed sessions unless EMS is used.
David Benjaminee0c8272015-09-13 01:03:54 -04001282 if (!ssl->session->extended_master_secret) {
David Benjaminf5260812017-07-12 17:16:39 -04001283 return 0;
David Benjaminee0c8272015-09-13 01:03:54 -04001284 }
1285 finished = ssl->s3->previous_server_finished;
1286 finished_len = ssl->s3->previous_server_finished_len;
1287 }
1288
David Benjaminee0c8272015-09-13 01:03:54 -04001289 *out_len = finished_len;
1290 if (finished_len > max_out) {
1291 *out_len = max_out;
1292 }
1293
David Benjamin17cf2cb2016-12-13 01:07:13 -05001294 OPENSSL_memcpy(out, finished, *out_len);
David Benjaminee0c8272015-09-13 01:03:54 -04001295 return 1;
David Benjaminee0c8272015-09-13 01:03:54 -04001296}
1297
David Benjamin5960a902017-02-14 20:07:11 -05001298static int set_session_id_context(CERT *cert, const uint8_t *sid_ctx,
David Benjamin30c4c302016-12-07 22:35:24 -05001299 size_t sid_ctx_len) {
David Benjamin5960a902017-02-14 20:07:11 -05001300 if (sid_ctx_len > sizeof(cert->sid_ctx)) {
David Benjamin3570d732015-06-29 00:28:17 -04001301 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -08001302 return 0;
1303 }
David Benjamin30c4c302016-12-07 22:35:24 -05001304
David Benjamina3d76d02017-07-14 19:36:07 -04001305 static_assert(sizeof(cert->sid_ctx) < 256, "sid_ctx too large");
David Benjamin5960a902017-02-14 20:07:11 -05001306 cert->sid_ctx_length = (uint8_t)sid_ctx_len;
1307 OPENSSL_memcpy(cert->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001308 return 1;
1309}
Adam Langley95c29f32014-06-20 12:00:00 -07001310
David Benjamin5960a902017-02-14 20:07:11 -05001311int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
1312 size_t sid_ctx_len) {
1313 return set_session_id_context(ctx->cert, sid_ctx, sid_ctx_len);
1314}
1315
Adam Langleyfcf25832014-12-18 17:42:32 -08001316int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
David Benjamin30c4c302016-12-07 22:35:24 -05001317 size_t sid_ctx_len) {
David Benjamin5960a902017-02-14 20:07:11 -05001318 return set_session_id_context(ssl->cert, sid_ctx, sid_ctx_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001319}
Adam Langley95c29f32014-06-20 12:00:00 -07001320
David Benjamin07820b52016-12-07 21:13:41 -05001321const uint8_t *SSL_get0_session_id_context(const SSL *ssl, size_t *out_len) {
David Benjamin5960a902017-02-14 20:07:11 -05001322 *out_len = ssl->cert->sid_ctx_length;
1323 return ssl->cert->sid_ctx;
David Benjamin07820b52016-12-07 21:13:41 -05001324}
1325
David Benjamin7481d392015-07-05 19:38:46 -04001326void SSL_certs_clear(SSL *ssl) { ssl_cert_clear_certs(ssl->cert); }
Adam Langley95c29f32014-06-20 12:00:00 -07001327
David Benjamin066fe0a2015-10-17 21:11:33 -04001328int SSL_get_fd(const SSL *ssl) { return SSL_get_rfd(ssl); }
Adam Langley95c29f32014-06-20 12:00:00 -07001329
David Benjamin066fe0a2015-10-17 21:11:33 -04001330int SSL_get_rfd(const SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001331 int ret = -1;
David Benjamin066fe0a2015-10-17 21:11:33 -04001332 BIO *b = BIO_find_type(SSL_get_rbio(ssl), BIO_TYPE_DESCRIPTOR);
1333 if (b != NULL) {
1334 BIO_get_fd(b, &ret);
Adam Langleyfcf25832014-12-18 17:42:32 -08001335 }
1336 return ret;
1337}
Adam Langley95c29f32014-06-20 12:00:00 -07001338
David Benjamin066fe0a2015-10-17 21:11:33 -04001339int SSL_get_wfd(const SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001340 int ret = -1;
David Benjamin066fe0a2015-10-17 21:11:33 -04001341 BIO *b = BIO_find_type(SSL_get_wbio(ssl), BIO_TYPE_DESCRIPTOR);
1342 if (b != NULL) {
1343 BIO_get_fd(b, &ret);
Adam Langleyfcf25832014-12-18 17:42:32 -08001344 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001345 return ret;
1346}
Adam Langley95c29f32014-06-20 12:00:00 -07001347
David Benjamin066fe0a2015-10-17 21:11:33 -04001348int SSL_set_fd(SSL *ssl, int fd) {
David Benjamin22ce9b22016-03-06 19:26:52 -05001349 BIO *bio = BIO_new(BIO_s_socket());
Adam Langleyfcf25832014-12-18 17:42:32 -08001350 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001351 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
David Benjamin066fe0a2015-10-17 21:11:33 -04001352 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001353 }
1354 BIO_set_fd(bio, fd, BIO_NOCLOSE);
David Benjamin066fe0a2015-10-17 21:11:33 -04001355 SSL_set_bio(ssl, bio, bio);
1356 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001357}
Adam Langley95c29f32014-06-20 12:00:00 -07001358
David Benjamin066fe0a2015-10-17 21:11:33 -04001359int SSL_set_wfd(SSL *ssl, int fd) {
David Benjamin2f871122016-05-20 14:27:17 -04001360 BIO *rbio = SSL_get_rbio(ssl);
1361 if (rbio == NULL || BIO_method_type(rbio) != BIO_TYPE_SOCKET ||
1362 BIO_get_fd(rbio, NULL) != fd) {
David Benjamin22ce9b22016-03-06 19:26:52 -05001363 BIO *bio = BIO_new(BIO_s_socket());
Adam Langleyfcf25832014-12-18 17:42:32 -08001364 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001365 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
David Benjamin066fe0a2015-10-17 21:11:33 -04001366 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001367 }
1368 BIO_set_fd(bio, fd, BIO_NOCLOSE);
David Benjamin4501bd52016-08-01 13:39:41 -04001369 SSL_set0_wbio(ssl, bio);
Adam Langleyfcf25832014-12-18 17:42:32 -08001370 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -04001371 // Copy the rbio over to the wbio.
David Benjamin4501bd52016-08-01 13:39:41 -04001372 BIO_up_ref(rbio);
1373 SSL_set0_wbio(ssl, rbio);
Adam Langleyfcf25832014-12-18 17:42:32 -08001374 }
1375
David Benjamin066fe0a2015-10-17 21:11:33 -04001376 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001377}
Adam Langley95c29f32014-06-20 12:00:00 -07001378
David Benjamin066fe0a2015-10-17 21:11:33 -04001379int SSL_set_rfd(SSL *ssl, int fd) {
David Benjamin2f871122016-05-20 14:27:17 -04001380 BIO *wbio = SSL_get_wbio(ssl);
1381 if (wbio == NULL || BIO_method_type(wbio) != BIO_TYPE_SOCKET ||
1382 BIO_get_fd(wbio, NULL) != fd) {
David Benjamin22ce9b22016-03-06 19:26:52 -05001383 BIO *bio = BIO_new(BIO_s_socket());
Adam Langleyfcf25832014-12-18 17:42:32 -08001384 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001385 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
David Benjamin066fe0a2015-10-17 21:11:33 -04001386 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001387 }
1388 BIO_set_fd(bio, fd, BIO_NOCLOSE);
David Benjamin4501bd52016-08-01 13:39:41 -04001389 SSL_set0_rbio(ssl, bio);
Adam Langleyfcf25832014-12-18 17:42:32 -08001390 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -04001391 // Copy the wbio over to the rbio.
David Benjamin4501bd52016-08-01 13:39:41 -04001392 BIO_up_ref(wbio);
1393 SSL_set0_rbio(ssl, wbio);
Adam Langleyfcf25832014-12-18 17:42:32 -08001394 }
David Benjamin066fe0a2015-10-17 21:11:33 -04001395 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001396}
Adam Langley95c29f32014-06-20 12:00:00 -07001397
David Benjamin49ddf412016-10-08 11:56:01 -04001398static size_t copy_finished(void *out, size_t out_len, const uint8_t *in,
1399 size_t in_len) {
1400 if (out_len > in_len) {
1401 out_len = in_len;
1402 }
David Benjamin17cf2cb2016-12-13 01:07:13 -05001403 OPENSSL_memcpy(out, in, out_len);
David Benjamin49ddf412016-10-08 11:56:01 -04001404 return in_len;
1405}
1406
David Benjamin1a1b34d2015-10-17 12:51:52 -04001407size_t SSL_get_finished(const SSL *ssl, void *buf, size_t count) {
David Benjaminced00b42016-10-08 11:24:09 -04001408 if (!ssl->s3->initial_handshake_complete ||
1409 ssl3_protocol_version(ssl) < TLS1_VERSION ||
1410 ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1411 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001412 }
1413
David Benjamin49ddf412016-10-08 11:56:01 -04001414 if (ssl->server) {
1415 return copy_finished(buf, count, ssl->s3->previous_server_finished,
1416 ssl->s3->previous_server_finished_len);
David Benjaminced00b42016-10-08 11:24:09 -04001417 }
David Benjamin49ddf412016-10-08 11:56:01 -04001418
1419 return copy_finished(buf, count, ssl->s3->previous_client_finished,
1420 ssl->s3->previous_client_finished_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001421}
Adam Langley95c29f32014-06-20 12:00:00 -07001422
David Benjamin1a1b34d2015-10-17 12:51:52 -04001423size_t SSL_get_peer_finished(const SSL *ssl, void *buf, size_t count) {
David Benjaminced00b42016-10-08 11:24:09 -04001424 if (!ssl->s3->initial_handshake_complete ||
1425 ssl3_protocol_version(ssl) < TLS1_VERSION ||
1426 ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1427 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001428 }
Adam Langley95c29f32014-06-20 12:00:00 -07001429
David Benjamin49ddf412016-10-08 11:56:01 -04001430 if (ssl->server) {
1431 return copy_finished(buf, count, ssl->s3->previous_client_finished,
1432 ssl->s3->previous_client_finished_len);
David Benjaminced00b42016-10-08 11:24:09 -04001433 }
David Benjamin49ddf412016-10-08 11:56:01 -04001434
1435 return copy_finished(buf, count, ssl->s3->previous_server_finished,
1436 ssl->s3->previous_server_finished_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001437}
Adam Langley95c29f32014-06-20 12:00:00 -07001438
David Benjamin59937042015-09-19 13:04:22 -04001439int SSL_get_verify_mode(const SSL *ssl) { return ssl->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -07001440
David Benjamin42fea372015-09-19 01:22:44 -04001441int SSL_get_extms_support(const SSL *ssl) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001442 // TLS 1.3 does not require extended master secret and always reports as
1443 // supporting it.
David Benjamina68c1182016-07-24 00:14:33 -04001444 if (!ssl->s3->have_version) {
1445 return 0;
1446 }
David Benjaminfc02b592017-02-17 16:26:01 -05001447 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1448 return 1;
1449 }
1450
David Benjaminc11ea9422017-08-29 16:33:21 -04001451 // If the initial handshake completed, query the established session.
David Benjaminfc02b592017-02-17 16:26:01 -05001452 if (ssl->s3->established_session != NULL) {
1453 return ssl->s3->established_session->extended_master_secret;
1454 }
1455
David Benjaminc11ea9422017-08-29 16:33:21 -04001456 // Otherwise, query the in-progress handshake.
David Benjaminfc02b592017-02-17 16:26:01 -05001457 if (ssl->s3->hs != NULL) {
1458 return ssl->s3->hs->extended_master_secret;
1459 }
1460 assert(0);
1461 return 0;
Matt Braithwaitecd6f54b2015-09-17 12:54:42 -07001462}
1463
David Benjamin9a41d1b2015-05-16 01:30:09 -04001464int SSL_CTX_get_read_ahead(const SSL_CTX *ctx) { return 0; }
Adam Langley95c29f32014-06-20 12:00:00 -07001465
David Benjamin0d56f882015-12-19 17:05:56 -05001466int SSL_get_read_ahead(const SSL *ssl) { return 0; }
Adam Langley95c29f32014-06-20 12:00:00 -07001467
David Benjamin9a41d1b2015-05-16 01:30:09 -04001468void SSL_CTX_set_read_ahead(SSL_CTX *ctx, int yes) { }
David Benjamin61ecccf2015-05-05 09:44:51 -04001469
David Benjamin0d56f882015-12-19 17:05:56 -05001470void SSL_set_read_ahead(SSL *ssl, int yes) { }
David Benjamin61ecccf2015-05-05 09:44:51 -04001471
David Benjamin9f859492015-10-03 10:44:30 -04001472int SSL_pending(const SSL *ssl) {
1473 if (ssl->s3->rrec.type != SSL3_RT_APPLICATION_DATA) {
1474 return 0;
1475 }
1476 return ssl->s3->rrec.length;
Adam Langleyfcf25832014-12-18 17:42:32 -08001477}
Adam Langley95c29f32014-06-20 12:00:00 -07001478
David Benjaminc11ea9422017-08-29 16:33:21 -04001479// Fix this so it checks all the valid key/cert options
Adam Langleyfcf25832014-12-18 17:42:32 -08001480int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
Adam Langley3a2b47a2017-01-24 13:59:42 -08001481 return ssl_cert_check_private_key(ctx->cert, ctx->cert->privatekey);
Adam Langleyc5ac2b62016-11-07 12:02:35 -08001482}
1483
David Benjaminc11ea9422017-08-29 16:33:21 -04001484// Fix this function so that it takes an optional type parameter
Adam Langleyc5ac2b62016-11-07 12:02:35 -08001485int SSL_check_private_key(const SSL *ssl) {
Adam Langley3a2b47a2017-01-24 13:59:42 -08001486 return ssl_cert_check_private_key(ssl->cert, ssl->cert->privatekey);
Adam Langleyfcf25832014-12-18 17:42:32 -08001487}
Adam Langley95c29f32014-06-20 12:00:00 -07001488
David Benjamin42fea372015-09-19 01:22:44 -04001489long SSL_get_default_timeout(const SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001490 return SSL_DEFAULT_SESSION_TIMEOUT;
1491}
Adam Langley95c29f32014-06-20 12:00:00 -07001492
David Benjamin44d3eed2015-05-21 01:29:55 -04001493int SSL_renegotiate(SSL *ssl) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001494 // Caller-initiated renegotiation is not supported.
David Benjamin3570d732015-06-29 00:28:17 -04001495 OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
David Benjamin44d3eed2015-05-21 01:29:55 -04001496 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001497}
Adam Langley95c29f32014-06-20 12:00:00 -07001498
David Benjamin44d3eed2015-05-21 01:29:55 -04001499int SSL_renegotiate_pending(SSL *ssl) {
1500 return SSL_in_init(ssl) && ssl->s3->initial_handshake_complete;
Adam Langleyfcf25832014-12-18 17:42:32 -08001501}
Adam Langley95c29f32014-06-20 12:00:00 -07001502
David Benjamin936aada2016-06-07 19:09:22 -04001503int SSL_total_renegotiations(const SSL *ssl) {
1504 return ssl->s3->total_renegotiations;
1505}
1506
David Benjamin1d0a1942015-04-26 15:35:35 -04001507size_t SSL_CTX_get_max_cert_list(const SSL_CTX *ctx) {
1508 return ctx->max_cert_list;
1509}
1510
1511void SSL_CTX_set_max_cert_list(SSL_CTX *ctx, size_t max_cert_list) {
1512 if (max_cert_list > kMaxHandshakeSize) {
1513 max_cert_list = kMaxHandshakeSize;
1514 }
1515 ctx->max_cert_list = (uint32_t)max_cert_list;
1516}
1517
1518size_t SSL_get_max_cert_list(const SSL *ssl) {
1519 return ssl->max_cert_list;
1520}
1521
1522void SSL_set_max_cert_list(SSL *ssl, size_t max_cert_list) {
1523 if (max_cert_list > kMaxHandshakeSize) {
1524 max_cert_list = kMaxHandshakeSize;
1525 }
1526 ssl->max_cert_list = (uint32_t)max_cert_list;
1527}
1528
Adam Langleyce9d85e2016-01-24 15:58:39 -08001529int SSL_CTX_set_max_send_fragment(SSL_CTX *ctx, size_t max_send_fragment) {
David Benjamin1d0a1942015-04-26 15:35:35 -04001530 if (max_send_fragment < 512) {
1531 max_send_fragment = 512;
1532 }
1533 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1534 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1535 }
1536 ctx->max_send_fragment = (uint16_t)max_send_fragment;
Adam Langleyce9d85e2016-01-24 15:58:39 -08001537
1538 return 1;
David Benjamin1d0a1942015-04-26 15:35:35 -04001539}
1540
Adam Langleyce9d85e2016-01-24 15:58:39 -08001541int SSL_set_max_send_fragment(SSL *ssl, size_t max_send_fragment) {
David Benjamin1d0a1942015-04-26 15:35:35 -04001542 if (max_send_fragment < 512) {
1543 max_send_fragment = 512;
1544 }
1545 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1546 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1547 }
1548 ssl->max_send_fragment = (uint16_t)max_send_fragment;
Adam Langleyce9d85e2016-01-24 15:58:39 -08001549
1550 return 1;
David Benjamin1d0a1942015-04-26 15:35:35 -04001551}
1552
David Benjamincb9cf792015-05-05 09:46:14 -04001553int SSL_set_mtu(SSL *ssl, unsigned mtu) {
David Benjamince079fd2016-08-02 16:22:34 -04001554 if (!SSL_is_dtls(ssl) || mtu < dtls1_min_mtu()) {
David Benjamincb9cf792015-05-05 09:46:14 -04001555 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001556 }
David Benjamincb9cf792015-05-05 09:46:14 -04001557 ssl->d1->mtu = mtu;
1558 return 1;
1559}
1560
1561int SSL_get_secure_renegotiation_support(const SSL *ssl) {
David Benjamind2610042017-01-03 10:49:28 -05001562 if (!ssl->s3->have_version) {
1563 return 0;
1564 }
1565 return ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
1566 ssl->s3->send_connection_binding;
David Benjamincb9cf792015-05-05 09:46:14 -04001567}
1568
Adam Langleyfcf25832014-12-18 17:42:32 -08001569LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) { return ctx->sessions; }
1570
David Benjamin71f7d3d2015-05-05 09:46:38 -04001571size_t SSL_CTX_sess_number(const SSL_CTX *ctx) {
1572 return lh_SSL_SESSION_num_items(ctx->sessions);
1573}
1574
1575unsigned long SSL_CTX_sess_set_cache_size(SSL_CTX *ctx, unsigned long size) {
1576 unsigned long ret = ctx->session_cache_size;
1577 ctx->session_cache_size = size;
1578 return ret;
1579}
1580
1581unsigned long SSL_CTX_sess_get_cache_size(const SSL_CTX *ctx) {
1582 return ctx->session_cache_size;
1583}
1584
1585int SSL_CTX_set_session_cache_mode(SSL_CTX *ctx, int mode) {
1586 int ret = ctx->session_cache_mode;
1587 ctx->session_cache_mode = mode;
1588 return ret;
1589}
1590
1591int SSL_CTX_get_session_cache_mode(const SSL_CTX *ctx) {
1592 return ctx->session_cache_mode;
1593}
1594
David Benjamin936aada2016-06-07 19:09:22 -04001595
1596int SSL_CTX_get_tlsext_ticket_keys(SSL_CTX *ctx, void *out, size_t len) {
1597 if (out == NULL) {
1598 return 48;
1599 }
1600 if (len != 48) {
1601 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH);
1602 return 0;
1603 }
Martin Kreichgauer72912d22017-08-04 12:06:43 -07001604
David Benjaminc11ea9422017-08-29 16:33:21 -04001605 // The default ticket keys are initialized lazily. Trigger a key
1606 // rotation to initialize them.
Martin Kreichgauer72912d22017-08-04 12:06:43 -07001607 if (!ssl_ctx_rotate_ticket_encryption_key(ctx)) {
1608 return 0;
1609 }
1610
David Benjaminf5260812017-07-12 17:16:39 -04001611 uint8_t *out_bytes = reinterpret_cast<uint8_t *>(out);
Martin Kreichgauer72912d22017-08-04 12:06:43 -07001612 MutexReadLock lock(&ctx->lock);
1613 OPENSSL_memcpy(out_bytes, ctx->tlsext_ticket_key_current->name, 16);
1614 OPENSSL_memcpy(out_bytes + 16, ctx->tlsext_ticket_key_current->hmac_key, 16);
1615 OPENSSL_memcpy(out_bytes + 32, ctx->tlsext_ticket_key_current->aes_key, 16);
David Benjamin936aada2016-06-07 19:09:22 -04001616 return 1;
1617}
1618
1619int SSL_CTX_set_tlsext_ticket_keys(SSL_CTX *ctx, const void *in, size_t len) {
1620 if (in == NULL) {
1621 return 48;
1622 }
1623 if (len != 48) {
1624 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH);
1625 return 0;
1626 }
Martin Kreichgauer72912d22017-08-04 12:06:43 -07001627 if (!ctx->tlsext_ticket_key_current) {
1628 ctx->tlsext_ticket_key_current =
1629 (tlsext_ticket_key *)OPENSSL_malloc(sizeof(tlsext_ticket_key));
1630 if (!ctx->tlsext_ticket_key_current) {
1631 return 0;
1632 }
1633 }
1634 OPENSSL_memset(ctx->tlsext_ticket_key_current, 0, sizeof(tlsext_ticket_key));
David Benjaminf5260812017-07-12 17:16:39 -04001635 const uint8_t *in_bytes = reinterpret_cast<const uint8_t *>(in);
Martin Kreichgauer72912d22017-08-04 12:06:43 -07001636 OPENSSL_memcpy(ctx->tlsext_ticket_key_current->name, in_bytes, 16);
1637 OPENSSL_memcpy(ctx->tlsext_ticket_key_current->hmac_key, in_bytes + 16, 16);
1638 OPENSSL_memcpy(ctx->tlsext_ticket_key_current->aes_key, in_bytes + 32, 16);
1639 OPENSSL_free(ctx->tlsext_ticket_key_prev);
1640 ctx->tlsext_ticket_key_prev = nullptr;
David Benjaminc11ea9422017-08-29 16:33:21 -04001641 // Disable automatic key rotation.
Martin Kreichgauer72912d22017-08-04 12:06:43 -07001642 ctx->tlsext_ticket_key_current->next_rotation_tv_sec = 0;
David Benjamin936aada2016-06-07 19:09:22 -04001643 return 1;
1644}
1645
1646int SSL_CTX_set_tlsext_ticket_key_cb(
1647 SSL_CTX *ctx, int (*callback)(SSL *ssl, uint8_t *key_name, uint8_t *iv,
1648 EVP_CIPHER_CTX *ctx, HMAC_CTX *hmac_ctx,
1649 int encrypt)) {
1650 ctx->tlsext_ticket_key_cb = callback;
1651 return 1;
1652}
1653
1654int SSL_CTX_set1_curves(SSL_CTX *ctx, const int *curves, size_t curves_len) {
1655 return tls1_set_curves(&ctx->supported_group_list,
1656 &ctx->supported_group_list_len, curves,
1657 curves_len);
1658}
1659
1660int SSL_set1_curves(SSL *ssl, const int *curves, size_t curves_len) {
1661 return tls1_set_curves(&ssl->supported_group_list,
1662 &ssl->supported_group_list_len, curves,
1663 curves_len);
1664}
1665
Alessandro Ghedini5fd18072016-09-28 21:04:25 +01001666int SSL_CTX_set1_curves_list(SSL_CTX *ctx, const char *curves) {
1667 return tls1_set_curves_list(&ctx->supported_group_list,
1668 &ctx->supported_group_list_len, curves);
1669}
1670
1671int SSL_set1_curves_list(SSL *ssl, const char *curves) {
1672 return tls1_set_curves_list(&ssl->supported_group_list,
1673 &ssl->supported_group_list_len, curves);
1674}
1675
David Benjamin9e68f192016-06-30 14:55:33 -04001676uint16_t SSL_get_curve_id(const SSL *ssl) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001677 // TODO(davidben): This checks the wrong session if there is a renegotiation
1678 // in progress.
Steven Valdez87eab492016-06-27 16:34:59 -04001679 SSL_SESSION *session = SSL_get_session(ssl);
David Benjamin4882a6c2016-12-11 02:48:12 -05001680 if (session == NULL) {
David Benjamin9e68f192016-06-30 14:55:33 -04001681 return 0;
1682 }
1683
David Benjamin4882a6c2016-12-11 02:48:12 -05001684 return session->group_id;
David Benjamin9e68f192016-06-30 14:55:33 -04001685}
1686
David Benjamin936aada2016-06-07 19:09:22 -04001687int SSL_CTX_set_tmp_dh(SSL_CTX *ctx, const DH *dh) {
David Benjamin936aada2016-06-07 19:09:22 -04001688 return 1;
1689}
1690
1691int SSL_set_tmp_dh(SSL *ssl, const DH *dh) {
David Benjamin936aada2016-06-07 19:09:22 -04001692 return 1;
1693}
1694
David Benjamin70dbf042017-08-08 18:51:37 -04001695STACK_OF(SSL_CIPHER) *SSL_CTX_get_ciphers(const SSL_CTX *ctx) {
David Benjamin8ebeabf2017-03-02 22:04:07 -05001696 return ctx->cipher_list->ciphers;
1697}
1698
David Benjamin70dbf042017-08-08 18:51:37 -04001699int SSL_CTX_cipher_in_group(const SSL_CTX *ctx, size_t i) {
1700 if (i >= sk_SSL_CIPHER_num(ctx->cipher_list->ciphers)) {
1701 return 0;
1702 }
1703 return ctx->cipher_list->in_group_flags[i];
1704}
1705
David Benjamin32876b32015-09-20 12:17:03 -04001706STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *ssl) {
1707 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001708 return NULL;
1709 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001710
David Benjaminaf3b3d32016-10-31 16:29:57 -04001711 const struct ssl_cipher_preference_list_st *prefs =
1712 ssl_get_cipher_preferences(ssl);
1713 if (prefs == NULL) {
1714 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08001715 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001716
David Benjaminaf3b3d32016-10-31 16:29:57 -04001717 return prefs->ciphers;
Adam Langleyfcf25832014-12-18 17:42:32 -08001718}
Adam Langley95c29f32014-06-20 12:00:00 -07001719
David Benjamin32876b32015-09-20 12:17:03 -04001720const char *SSL_get_cipher_list(const SSL *ssl, int n) {
David Benjamin32876b32015-09-20 12:17:03 -04001721 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001722 return NULL;
1723 }
Adam Langley95c29f32014-06-20 12:00:00 -07001724
David Benjamin8ebeabf2017-03-02 22:04:07 -05001725 STACK_OF(SSL_CIPHER) *sk = SSL_get_ciphers(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001726 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
1727 return NULL;
1728 }
Adam Langley95c29f32014-06-20 12:00:00 -07001729
David Benjamin8ebeabf2017-03-02 22:04:07 -05001730 const SSL_CIPHER *c = sk_SSL_CIPHER_value(sk, n);
Adam Langleyfcf25832014-12-18 17:42:32 -08001731 if (c == NULL) {
1732 return NULL;
1733 }
Adam Langley95c29f32014-06-20 12:00:00 -07001734
Adam Langleyfcf25832014-12-18 17:42:32 -08001735 return c->name;
1736}
David Benjamin5491e3f2014-09-29 19:33:09 -04001737
Adam Langleyfcf25832014-12-18 17:42:32 -08001738int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
Matthew Braithwaite6ad20dc2017-02-21 16:41:33 -08001739 return ssl_create_cipher_list(ctx->method, &ctx->cipher_list, str,
1740 0 /* not strict */);
Matthew Braithwaitea57dcfb2017-02-17 22:08:23 -08001741}
1742
1743int SSL_CTX_set_strict_cipher_list(SSL_CTX *ctx, const char *str) {
Matthew Braithwaite6ad20dc2017-02-21 16:41:33 -08001744 return ssl_create_cipher_list(ctx->method, &ctx->cipher_list, str,
1745 1 /* strict */);
Adam Langleyfcf25832014-12-18 17:42:32 -08001746}
David Benjamin39482a12014-07-20 13:30:15 -04001747
David Benjamin32876b32015-09-20 12:17:03 -04001748int SSL_set_cipher_list(SSL *ssl, const char *str) {
Matthew Braithwaite6ad20dc2017-02-21 16:41:33 -08001749 return ssl_create_cipher_list(ssl->ctx->method, &ssl->cipher_list, str,
1750 0 /* not strict */);
Matthew Braithwaitea57dcfb2017-02-17 22:08:23 -08001751}
1752
1753int SSL_set_strict_cipher_list(SSL *ssl, const char *str) {
Matthew Braithwaite6ad20dc2017-02-21 16:41:33 -08001754 return ssl_create_cipher_list(ssl->ctx->method, &ssl->cipher_list, str,
1755 1 /* strict */);
Adam Langleyfcf25832014-12-18 17:42:32 -08001756}
Adam Langley95c29f32014-06-20 12:00:00 -07001757
David Benjamin07e13842015-10-17 13:48:04 -04001758const char *SSL_get_servername(const SSL *ssl, const int type) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001759 if (type != TLSEXT_NAMETYPE_host_name) {
1760 return NULL;
1761 }
Adam Langley95c29f32014-06-20 12:00:00 -07001762
David Benjaminc11ea9422017-08-29 16:33:21 -04001763 // Historically, |SSL_get_servername| was also the configuration getter
1764 // corresponding to |SSL_set_tlsext_host_name|.
David Benjamin07e13842015-10-17 13:48:04 -04001765 if (ssl->tlsext_hostname != NULL) {
1766 return ssl->tlsext_hostname;
1767 }
1768
David Benjamina8614602017-09-06 15:40:19 -04001769 return ssl->s3->hostname;
Adam Langleyfcf25832014-12-18 17:42:32 -08001770}
Adam Langley95c29f32014-06-20 12:00:00 -07001771
David Benjamin07e13842015-10-17 13:48:04 -04001772int SSL_get_servername_type(const SSL *ssl) {
David Benjamina8614602017-09-06 15:40:19 -04001773 if (SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) == NULL) {
Steven Valdez87eab492016-06-27 16:34:59 -04001774 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001775 }
Steven Valdez87eab492016-06-27 16:34:59 -04001776 return TLSEXT_NAMETYPE_host_name;
Adam Langleyfcf25832014-12-18 17:42:32 -08001777}
Adam Langley95c29f32014-06-20 12:00:00 -07001778
David Benjamin3a1dd462017-07-11 16:13:10 -04001779void SSL_CTX_set_custom_verify(
1780 SSL_CTX *ctx, int mode,
1781 enum ssl_verify_result_t (*callback)(SSL *ssl, uint8_t *out_alert)) {
1782 ctx->verify_mode = mode;
1783 ctx->custom_verify_callback = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08001784}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001785
David Benjamin3a1dd462017-07-11 16:13:10 -04001786void SSL_set_custom_verify(
1787 SSL *ssl, int mode,
1788 enum ssl_verify_result_t (*callback)(SSL *ssl, uint8_t *out_alert)) {
1789 ssl->verify_mode = mode;
1790 ssl->custom_verify_callback = callback;
1791}
1792
1793void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
1794 ctx->signed_cert_timestamps_enabled = 1;
Adam Langleyfe366722017-03-07 16:41:04 -08001795}
1796
David Benjamin26e1ff32017-02-14 20:13:00 -05001797void SSL_enable_signed_cert_timestamps(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001798 ssl->signed_cert_timestamps_enabled = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001799}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001800
Adam Langleyfcf25832014-12-18 17:42:32 -08001801void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
1802 ctx->ocsp_stapling_enabled = 1;
1803}
David Benjamin6c7aed02014-08-27 16:42:38 -04001804
David Benjamin26e1ff32017-02-14 20:13:00 -05001805void SSL_enable_ocsp_stapling(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001806 ssl->ocsp_stapling_enabled = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001807}
David Benjamin6c7aed02014-08-27 16:42:38 -04001808
Adam Langleyfcf25832014-12-18 17:42:32 -08001809void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
1810 size_t *out_len) {
Steven Valdez87eab492016-06-27 16:34:59 -04001811 SSL_SESSION *session = SSL_get_session(ssl);
David Benjamin8fc2dc02017-08-22 15:07:51 -07001812 if (ssl->server || !session || !session->signed_cert_timestamp_list) {
1813 *out_len = 0;
1814 *out = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08001815 return;
1816 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001817
David Benjamin8fc2dc02017-08-22 15:07:51 -07001818 *out = CRYPTO_BUFFER_data(session->signed_cert_timestamp_list);
1819 *out_len = CRYPTO_BUFFER_len(session->signed_cert_timestamp_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001820}
David Benjamin6c7aed02014-08-27 16:42:38 -04001821
Adam Langleyfcf25832014-12-18 17:42:32 -08001822void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
1823 size_t *out_len) {
Steven Valdez87eab492016-06-27 16:34:59 -04001824 SSL_SESSION *session = SSL_get_session(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001825 if (ssl->server || !session || !session->ocsp_response) {
David Benjamin8fc2dc02017-08-22 15:07:51 -07001826 *out_len = 0;
1827 *out = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08001828 return;
1829 }
David Benjamin8fc2dc02017-08-22 15:07:51 -07001830
1831 *out = CRYPTO_BUFFER_data(session->ocsp_response);
1832 *out_len = CRYPTO_BUFFER_len(session->ocsp_response);
Adam Langleyfcf25832014-12-18 17:42:32 -08001833}
David Benjamin6c7aed02014-08-27 16:42:38 -04001834
David Benjamin936aada2016-06-07 19:09:22 -04001835int SSL_set_tlsext_host_name(SSL *ssl, const char *name) {
1836 OPENSSL_free(ssl->tlsext_hostname);
1837 ssl->tlsext_hostname = NULL;
1838
1839 if (name == NULL) {
1840 return 1;
1841 }
1842
1843 size_t len = strlen(name);
1844 if (len == 0 || len > TLSEXT_MAXLEN_host_name) {
1845 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL3_EXT_INVALID_SERVERNAME);
1846 return 0;
1847 }
1848 ssl->tlsext_hostname = BUF_strdup(name);
1849 if (ssl->tlsext_hostname == NULL) {
1850 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1851 return 0;
1852 }
1853 return 1;
1854}
1855
1856int SSL_CTX_set_tlsext_servername_callback(
1857 SSL_CTX *ctx, int (*callback)(SSL *ssl, int *out_alert, void *arg)) {
1858 ctx->tlsext_servername_callback = callback;
1859 return 1;
1860}
1861
1862int SSL_CTX_set_tlsext_servername_arg(SSL_CTX *ctx, void *arg) {
1863 ctx->tlsext_servername_arg = arg;
1864 return 1;
1865}
1866
David Benjaminca743582017-06-15 17:51:35 -04001867int SSL_select_next_proto(uint8_t **out, uint8_t *out_len, const uint8_t *peer,
1868 unsigned peer_len, const uint8_t *supported,
1869 unsigned supported_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001870 const uint8_t *result;
David Benjaminca743582017-06-15 17:51:35 -04001871 int status;
Adam Langley95c29f32014-06-20 12:00:00 -07001872
David Benjaminc11ea9422017-08-29 16:33:21 -04001873 // For each protocol in peer preference order, see if we support it.
David Benjaminca743582017-06-15 17:51:35 -04001874 for (unsigned i = 0; i < peer_len;) {
1875 for (unsigned j = 0; j < supported_len;) {
1876 if (peer[i] == supported[j] &&
1877 OPENSSL_memcmp(&peer[i + 1], &supported[j + 1], peer[i]) == 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001878 // We found a match
David Benjaminca743582017-06-15 17:51:35 -04001879 result = &peer[i];
Adam Langleyfcf25832014-12-18 17:42:32 -08001880 status = OPENSSL_NPN_NEGOTIATED;
1881 goto found;
1882 }
David Benjaminca743582017-06-15 17:51:35 -04001883 j += supported[j];
Adam Langleyfcf25832014-12-18 17:42:32 -08001884 j++;
1885 }
David Benjaminca743582017-06-15 17:51:35 -04001886 i += peer[i];
Adam Langleyfcf25832014-12-18 17:42:32 -08001887 i++;
1888 }
Adam Langley95c29f32014-06-20 12:00:00 -07001889
David Benjaminc11ea9422017-08-29 16:33:21 -04001890 // There's no overlap between our protocols and the peer's list.
David Benjaminca743582017-06-15 17:51:35 -04001891 result = supported;
Adam Langleyfcf25832014-12-18 17:42:32 -08001892 status = OPENSSL_NPN_NO_OVERLAP;
Adam Langley95c29f32014-06-20 12:00:00 -07001893
Adam Langleyfcf25832014-12-18 17:42:32 -08001894found:
1895 *out = (uint8_t *)result + 1;
David Benjamin977547b2015-09-16 00:25:52 -04001896 *out_len = result[0];
Adam Langleyfcf25832014-12-18 17:42:32 -08001897 return status;
1898}
Adam Langley95c29f32014-06-20 12:00:00 -07001899
David Benjamin977547b2015-09-16 00:25:52 -04001900void SSL_get0_next_proto_negotiated(const SSL *ssl, const uint8_t **out_data,
1901 unsigned *out_len) {
David Benjamin79978df2015-12-25 15:56:49 -05001902 *out_data = ssl->s3->next_proto_negotiated;
David Benjamin05d4c972017-06-21 16:35:10 -04001903 *out_len = ssl->s3->next_proto_negotiated_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001904}
1905
Adam Langleyfcf25832014-12-18 17:42:32 -08001906void SSL_CTX_set_next_protos_advertised_cb(
1907 SSL_CTX *ctx,
David Benjamin977547b2015-09-16 00:25:52 -04001908 int (*cb)(SSL *ssl, const uint8_t **out, unsigned *out_len, void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08001909 void *arg) {
1910 ctx->next_protos_advertised_cb = cb;
1911 ctx->next_protos_advertised_cb_arg = arg;
1912}
Adam Langley95c29f32014-06-20 12:00:00 -07001913
Adam Langleyfcf25832014-12-18 17:42:32 -08001914void SSL_CTX_set_next_proto_select_cb(
David Benjamin977547b2015-09-16 00:25:52 -04001915 SSL_CTX *ctx, int (*cb)(SSL *ssl, uint8_t **out, uint8_t *out_len,
1916 const uint8_t *in, unsigned in_len, void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08001917 void *arg) {
1918 ctx->next_proto_select_cb = cb;
1919 ctx->next_proto_select_cb_arg = arg;
1920}
Adam Langley95c29f32014-06-20 12:00:00 -07001921
Adam Langleyfcf25832014-12-18 17:42:32 -08001922int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
1923 unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001924 OPENSSL_free(ctx->alpn_client_proto_list);
David Benjaminf5260812017-07-12 17:16:39 -04001925 ctx->alpn_client_proto_list = (uint8_t *)BUF_memdup(protos, protos_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001926 if (!ctx->alpn_client_proto_list) {
1927 return 1;
1928 }
1929 ctx->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 -08001934int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001935 OPENSSL_free(ssl->alpn_client_proto_list);
David Benjaminf5260812017-07-12 17:16:39 -04001936 ssl->alpn_client_proto_list = (uint8_t *)BUF_memdup(protos, protos_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001937 if (!ssl->alpn_client_proto_list) {
1938 return 1;
1939 }
1940 ssl->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001941
Adam Langleyfcf25832014-12-18 17:42:32 -08001942 return 0;
1943}
Adam Langley95c29f32014-06-20 12:00:00 -07001944
Adam Langleyfcf25832014-12-18 17:42:32 -08001945void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
1946 int (*cb)(SSL *ssl, const uint8_t **out,
David Benjamin8984f1f2015-09-16 00:10:19 -04001947 uint8_t *out_len, const uint8_t *in,
1948 unsigned in_len, void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08001949 void *arg) {
1950 ctx->alpn_select_cb = cb;
1951 ctx->alpn_select_cb_arg = arg;
1952}
Adam Langley95c29f32014-06-20 12:00:00 -07001953
David Benjamin8984f1f2015-09-16 00:10:19 -04001954void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **out_data,
1955 unsigned *out_len) {
Steven Valdeze831a812017-03-09 14:56:07 -05001956 if (SSL_in_early_data(ssl) && !ssl->server) {
1957 *out_data = ssl->s3->hs->early_session->early_alpn;
1958 *out_len = ssl->s3->hs->early_session->early_alpn_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08001959 } else {
Steven Valdeze831a812017-03-09 14:56:07 -05001960 *out_data = ssl->s3->alpn_selected;
David Benjamin8984f1f2015-09-16 00:10:19 -04001961 *out_len = ssl->s3->alpn_selected_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08001962 }
1963}
Adam Langley95c29f32014-06-20 12:00:00 -07001964
David Benjaminc8ff30c2017-04-04 13:52:36 -04001965void SSL_CTX_set_allow_unknown_alpn_protos(SSL_CTX *ctx, int enabled) {
1966 ctx->allow_unknown_alpn_protos = !!enabled;
1967}
David Benjamin936aada2016-06-07 19:09:22 -04001968
David Benjamineebd3c82016-12-06 17:43:58 -05001969void SSL_CTX_set_tls_channel_id_enabled(SSL_CTX *ctx, int enabled) {
1970 ctx->tlsext_channel_id_enabled = !!enabled;
1971}
1972
David Benjamin936aada2016-06-07 19:09:22 -04001973int SSL_CTX_enable_tls_channel_id(SSL_CTX *ctx) {
David Benjamineebd3c82016-12-06 17:43:58 -05001974 SSL_CTX_set_tls_channel_id_enabled(ctx, 1);
David Benjamin936aada2016-06-07 19:09:22 -04001975 return 1;
1976}
1977
David Benjamineebd3c82016-12-06 17:43:58 -05001978void SSL_set_tls_channel_id_enabled(SSL *ssl, int enabled) {
1979 ssl->tlsext_channel_id_enabled = !!enabled;
1980}
1981
David Benjamin936aada2016-06-07 19:09:22 -04001982int SSL_enable_tls_channel_id(SSL *ssl) {
David Benjamineebd3c82016-12-06 17:43:58 -05001983 SSL_set_tls_channel_id_enabled(ssl, 1);
David Benjamin936aada2016-06-07 19:09:22 -04001984 return 1;
1985}
1986
1987static int is_p256_key(EVP_PKEY *private_key) {
1988 const EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(private_key);
1989 return ec_key != NULL &&
1990 EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key)) ==
1991 NID_X9_62_prime256v1;
1992}
1993
1994int SSL_CTX_set1_tls_channel_id(SSL_CTX *ctx, EVP_PKEY *private_key) {
1995 if (!is_p256_key(private_key)) {
1996 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256);
1997 return 0;
1998 }
1999
2000 EVP_PKEY_free(ctx->tlsext_channel_id_private);
Adam Langley310d3f62016-07-12 10:39:20 -07002001 EVP_PKEY_up_ref(private_key);
2002 ctx->tlsext_channel_id_private = private_key;
David Benjamin936aada2016-06-07 19:09:22 -04002003 ctx->tlsext_channel_id_enabled = 1;
2004
2005 return 1;
2006}
2007
2008int SSL_set1_tls_channel_id(SSL *ssl, EVP_PKEY *private_key) {
2009 if (!is_p256_key(private_key)) {
2010 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256);
2011 return 0;
2012 }
2013
2014 EVP_PKEY_free(ssl->tlsext_channel_id_private);
Adam Langley310d3f62016-07-12 10:39:20 -07002015 EVP_PKEY_up_ref(private_key);
2016 ssl->tlsext_channel_id_private = private_key;
David Benjamin936aada2016-06-07 19:09:22 -04002017 ssl->tlsext_channel_id_enabled = 1;
2018
2019 return 1;
2020}
2021
2022size_t SSL_get_tls_channel_id(SSL *ssl, uint8_t *out, size_t max_out) {
2023 if (!ssl->s3->tlsext_channel_id_valid) {
2024 return 0;
2025 }
David Benjamin17cf2cb2016-12-13 01:07:13 -05002026 OPENSSL_memcpy(out, ssl->s3->tlsext_channel_id,
2027 (max_out < 64) ? max_out : 64);
David Benjamin936aada2016-06-07 19:09:22 -04002028 return 64;
2029}
2030
David Benjamin936aada2016-06-07 19:09:22 -04002031size_t SSL_get0_certificate_types(SSL *ssl, const uint8_t **out_types) {
David Benjamina0486782016-10-06 19:11:32 -04002032 if (ssl->server || ssl->s3->hs == NULL) {
David Benjamin936aada2016-06-07 19:09:22 -04002033 *out_types = NULL;
2034 return 0;
2035 }
David Benjamin08f5c762017-09-21 02:43:05 -04002036 *out_types = ssl->s3->hs->certificate_types.data();
2037 return ssl->s3->hs->certificate_types.size();
David Benjamin936aada2016-06-07 19:09:22 -04002038}
2039
David Benjamin0d56f882015-12-19 17:05:56 -05002040EVP_PKEY *SSL_get_privatekey(const SSL *ssl) {
2041 if (ssl->cert != NULL) {
2042 return ssl->cert->privatekey;
Adam Langleyfcf25832014-12-18 17:42:32 -08002043 }
2044
2045 return NULL;
2046}
2047
Adam Langleyfcf25832014-12-18 17:42:32 -08002048EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
2049 if (ctx->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04002050 return ctx->cert->privatekey;
Adam Langleyfcf25832014-12-18 17:42:32 -08002051 }
2052
2053 return NULL;
2054}
2055
David Benjamin42fea372015-09-19 01:22:44 -04002056const SSL_CIPHER *SSL_get_current_cipher(const SSL *ssl) {
David Benjamincfc11c22017-07-18 22:45:18 -04002057 return ssl->s3->aead_write_ctx->cipher();
Adam Langleyfcf25832014-12-18 17:42:32 -08002058}
2059
David Benjamin936aada2016-06-07 19:09:22 -04002060int SSL_session_reused(const SSL *ssl) {
Steven Valdeze831a812017-03-09 14:56:07 -05002061 return ssl->s3->session_reused || SSL_in_early_data(ssl);
David Benjamin936aada2016-06-07 19:09:22 -04002062}
2063
David Benjamin0d56f882015-12-19 17:05:56 -05002064const COMP_METHOD *SSL_get_current_compression(SSL *ssl) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002065
David Benjamin0d56f882015-12-19 17:05:56 -05002066const COMP_METHOD *SSL_get_current_expansion(SSL *ssl) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002067
Adam Langley29ec5d12016-03-01 16:12:28 -08002068int *SSL_get_server_tmp_key(SSL *ssl, EVP_PKEY **out_key) { return 0; }
2069
Adam Langleyfcf25832014-12-18 17:42:32 -08002070void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
Adam Langleybb85f3d2015-10-28 18:44:11 -07002071 ctx->quiet_shutdown = (mode != 0);
Adam Langleyfcf25832014-12-18 17:42:32 -08002072}
2073
2074int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
2075 return ctx->quiet_shutdown;
2076}
2077
Adam Langleybb85f3d2015-10-28 18:44:11 -07002078void SSL_set_quiet_shutdown(SSL *ssl, int mode) {
2079 ssl->quiet_shutdown = (mode != 0);
2080}
Adam Langleyfcf25832014-12-18 17:42:32 -08002081
David Benjamin9f859492015-10-03 10:44:30 -04002082int SSL_get_quiet_shutdown(const SSL *ssl) { return ssl->quiet_shutdown; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002083
David Benjamin63006a92015-10-18 00:00:06 -04002084void SSL_set_shutdown(SSL *ssl, int mode) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002085 // It is an error to clear any bits that have already been set. (We can't try
2086 // to get a second close_notify or send two.)
David Benjaminfa214e42016-05-10 17:03:10 -04002087 assert((SSL_get_shutdown(ssl) & mode) == SSL_get_shutdown(ssl));
David Benjamin63006a92015-10-18 00:00:06 -04002088
David Benjaminfa214e42016-05-10 17:03:10 -04002089 if (mode & SSL_RECEIVED_SHUTDOWN &&
2090 ssl->s3->recv_shutdown == ssl_shutdown_none) {
2091 ssl->s3->recv_shutdown = ssl_shutdown_close_notify;
2092 }
2093
2094 if (mode & SSL_SENT_SHUTDOWN &&
2095 ssl->s3->send_shutdown == ssl_shutdown_none) {
2096 ssl->s3->send_shutdown = ssl_shutdown_close_notify;
2097 }
David Benjamin63006a92015-10-18 00:00:06 -04002098}
Adam Langleyfcf25832014-12-18 17:42:32 -08002099
David Benjaminfa214e42016-05-10 17:03:10 -04002100int SSL_get_shutdown(const SSL *ssl) {
2101 int ret = 0;
2102 if (ssl->s3->recv_shutdown != ssl_shutdown_none) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002103 // Historically, OpenSSL set |SSL_RECEIVED_SHUTDOWN| on both close_notify
2104 // and fatal alert.
David Benjaminfa214e42016-05-10 17:03:10 -04002105 ret |= SSL_RECEIVED_SHUTDOWN;
2106 }
2107 if (ssl->s3->send_shutdown == ssl_shutdown_close_notify) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002108 // Historically, OpenSSL set |SSL_SENT_SHUTDOWN| on only close_notify.
David Benjaminfa214e42016-05-10 17:03:10 -04002109 ret |= SSL_SENT_SHUTDOWN;
2110 }
2111 return ret;
2112}
Adam Langleyfcf25832014-12-18 17:42:32 -08002113
Adam Langleyfcf25832014-12-18 17:42:32 -08002114SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
2115
2116SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
2117 if (ssl->ctx == ctx) {
2118 return ssl->ctx;
2119 }
2120
David Benjaminc11ea9422017-08-29 16:33:21 -04002121 // One cannot change the X.509 callbacks during a connection.
Adam Langley3509dac2017-02-01 11:59:18 -08002122 if (ssl->ctx->x509_method != ctx->x509_method) {
2123 assert(0);
2124 return NULL;
2125 }
2126
Adam Langleyfcf25832014-12-18 17:42:32 -08002127 if (ctx == NULL) {
David Benjaminbe497062017-03-10 16:08:36 -05002128 ctx = ssl->session_ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08002129 }
2130
David Benjamin2755a3e2015-04-22 16:17:58 -04002131 ssl_cert_free(ssl->cert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002132 ssl->cert = ssl_cert_dup(ctx->cert);
David Benjamin2755a3e2015-04-22 16:17:58 -04002133
David Benjaminf650c712016-12-22 15:54:51 -05002134 SSL_CTX_up_ref(ctx);
2135 SSL_CTX_free(ssl->ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002136 ssl->ctx = ctx;
2137
Adam Langleyfcf25832014-12-18 17:42:32 -08002138 return ssl->ctx;
2139}
2140
Adam Langley95c29f32014-06-20 12:00:00 -07002141void SSL_set_info_callback(SSL *ssl,
David Benjamin82170242015-10-17 22:51:17 -04002142 void (*cb)(const SSL *ssl, int type, int value)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002143 ssl->info_callback = cb;
2144}
Adam Langley95c29f32014-06-20 12:00:00 -07002145
David Benjamin82170242015-10-17 22:51:17 -04002146void (*SSL_get_info_callback(const SSL *ssl))(const SSL *ssl, int type,
2147 int value) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002148 return ssl->info_callback;
2149}
Adam Langley95c29f32014-06-20 12:00:00 -07002150
David Benjamincb0c29f2016-12-12 17:00:50 -05002151int SSL_state(const SSL *ssl) {
David Benjamin9e766d72017-02-10 23:14:17 -05002152 return SSL_in_init(ssl) ? SSL_ST_INIT : SSL_ST_OK;
David Benjamincb0c29f2016-12-12 17:00:50 -05002153}
Adam Langley95c29f32014-06-20 12:00:00 -07002154
David Benjaminece089c2015-05-15 23:52:42 -04002155void SSL_set_state(SSL *ssl, int state) { }
Adam Langley95c29f32014-06-20 12:00:00 -07002156
Emily Stark95a79ee2016-02-25 21:12:28 -08002157char *SSL_get_shared_ciphers(const SSL *ssl, char *buf, int len) {
2158 if (len <= 0) {
2159 return NULL;
2160 }
2161 buf[0] = '\0';
2162 return buf;
2163}
2164
David Benjamin8a589332015-12-04 23:14:35 -05002165int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
David Benjamind94682d2017-05-14 17:10:18 -04002166 CRYPTO_EX_dup *dup_unused, CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002167 int index;
2168 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp,
David Benjamind94682d2017-05-14 17:10:18 -04002169 free_func)) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002170 return -1;
2171 }
2172 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002173}
Adam Langley95c29f32014-06-20 12:00:00 -07002174
David Benjamin27e377e2017-07-31 19:09:42 -04002175int SSL_set_ex_data(SSL *ssl, int idx, void *data) {
2176 return CRYPTO_set_ex_data(&ssl->ex_data, idx, data);
Adam Langleyfcf25832014-12-18 17:42:32 -08002177}
Adam Langley95c29f32014-06-20 12:00:00 -07002178
David Benjamin1ac08fe2015-09-15 01:36:22 -04002179void *SSL_get_ex_data(const SSL *ssl, int idx) {
2180 return CRYPTO_get_ex_data(&ssl->ex_data, idx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002181}
Adam Langley95c29f32014-06-20 12:00:00 -07002182
David Benjamin8a589332015-12-04 23:14:35 -05002183int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
David Benjamind94682d2017-05-14 17:10:18 -04002184 CRYPTO_EX_dup *dup_unused,
Adam Langleyfcf25832014-12-18 17:42:32 -08002185 CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002186 int index;
2187 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp,
David Benjamind94682d2017-05-14 17:10:18 -04002188 free_func)) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002189 return -1;
2190 }
2191 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002192}
Adam Langley95c29f32014-06-20 12:00:00 -07002193
David Benjamin27e377e2017-07-31 19:09:42 -04002194int SSL_CTX_set_ex_data(SSL_CTX *ctx, int idx, void *data) {
2195 return CRYPTO_set_ex_data(&ctx->ex_data, idx, data);
Adam Langleyfcf25832014-12-18 17:42:32 -08002196}
Adam Langley95c29f32014-06-20 12:00:00 -07002197
David Benjamin1ac08fe2015-09-15 01:36:22 -04002198void *SSL_CTX_get_ex_data(const SSL_CTX *ctx, int idx) {
2199 return CRYPTO_get_ex_data(&ctx->ex_data, idx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002200}
Adam Langley95c29f32014-06-20 12:00:00 -07002201
David Benjamin93d17492015-10-17 12:43:36 -04002202int SSL_want(const SSL *ssl) { return ssl->rwstate; }
Adam Langley95c29f32014-06-20 12:00:00 -07002203
Adam Langleyfcf25832014-12-18 17:42:32 -08002204void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
2205 RSA *(*cb)(SSL *ssl, int is_export,
David Benjamin27e377e2017-07-31 19:09:42 -04002206 int keylength)) {}
Adam Langley95c29f32014-06-20 12:00:00 -07002207
Adam Langleyfcf25832014-12-18 17:42:32 -08002208void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
David Benjamin27e377e2017-07-31 19:09:42 -04002209 int keylength)) {}
Adam Langleyfcf25832014-12-18 17:42:32 -08002210
2211void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
David Benjamin27e377e2017-07-31 19:09:42 -04002212 DH *(*cb)(SSL *ssl, int is_export,
2213 int keylength)) {}
Adam Langleyfcf25832014-12-18 17:42:32 -08002214
David Benjamin27e377e2017-07-31 19:09:42 -04002215void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*cb)(SSL *ssl, int is_export,
2216 int keylength)) {}
Adam Langleyfcf25832014-12-18 17:42:32 -08002217
David Benjamin0a9bf662017-06-21 16:34:15 -04002218static int use_psk_identity_hint(char **out, const char *identity_hint) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002219 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
David Benjamin3570d732015-06-29 00:28:17 -04002220 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -08002221 return 0;
2222 }
2223
David Benjaminc11ea9422017-08-29 16:33:21 -04002224 // Clear currently configured hint, if any.
David Benjamin0a9bf662017-06-21 16:34:15 -04002225 OPENSSL_free(*out);
2226 *out = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002227
David Benjaminc11ea9422017-08-29 16:33:21 -04002228 // Treat the empty hint as not supplying one. Plain PSK makes it possible to
2229 // send either no hint (omit ServerKeyExchange) or an empty hint, while
2230 // ECDHE_PSK can only spell empty hint. Having different capabilities is odd,
2231 // so we interpret empty and missing as identical.
David Benjamin78679342016-09-16 19:42:05 -04002232 if (identity_hint != NULL && identity_hint[0] != '\0') {
David Benjamin0a9bf662017-06-21 16:34:15 -04002233 *out = BUF_strdup(identity_hint);
2234 if (*out == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002235 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002236 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002237 }
Adam Langley95c29f32014-06-20 12:00:00 -07002238
Adam Langleyfcf25832014-12-18 17:42:32 -08002239 return 1;
2240}
Adam Langley95c29f32014-06-20 12:00:00 -07002241
David Benjamin0a9bf662017-06-21 16:34:15 -04002242int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
2243 return use_psk_identity_hint(&ctx->psk_identity_hint, identity_hint);
2244}
2245
2246int SSL_use_psk_identity_hint(SSL *ssl, const char *identity_hint) {
2247 return use_psk_identity_hint(&ssl->psk_identity_hint, identity_hint);
2248}
2249
David Benjamine8814df2015-09-15 08:05:54 -04002250const char *SSL_get_psk_identity_hint(const SSL *ssl) {
2251 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002252 return NULL;
2253 }
David Benjamine8814df2015-09-15 08:05:54 -04002254 return ssl->psk_identity_hint;
Adam Langleyfcf25832014-12-18 17:42:32 -08002255}
Adam Langley95c29f32014-06-20 12:00:00 -07002256
David Benjamine8814df2015-09-15 08:05:54 -04002257const char *SSL_get_psk_identity(const SSL *ssl) {
Steven Valdez87eab492016-06-27 16:34:59 -04002258 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002259 return NULL;
2260 }
Steven Valdez87eab492016-06-27 16:34:59 -04002261 SSL_SESSION *session = SSL_get_session(ssl);
2262 if (session == NULL) {
2263 return NULL;
2264 }
2265 return session->psk_identity;
Adam Langleyfcf25832014-12-18 17:42:32 -08002266}
Adam Langley95c29f32014-06-20 12:00:00 -07002267
Adam Langleyfcf25832014-12-18 17:42:32 -08002268void SSL_set_psk_client_callback(
David Benjamine8814df2015-09-15 08:05:54 -04002269 SSL *ssl, unsigned (*cb)(SSL *ssl, const char *hint, char *identity,
2270 unsigned max_identity_len, uint8_t *psk,
2271 unsigned max_psk_len)) {
2272 ssl->psk_client_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002273}
Adam Langley95c29f32014-06-20 12:00:00 -07002274
Adam Langleyfcf25832014-12-18 17:42:32 -08002275void SSL_CTX_set_psk_client_callback(
David Benjamine8814df2015-09-15 08:05:54 -04002276 SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *hint, char *identity,
2277 unsigned max_identity_len, uint8_t *psk,
2278 unsigned max_psk_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002279 ctx->psk_client_callback = cb;
2280}
Adam Langley95c29f32014-06-20 12:00:00 -07002281
Adam Langleyfcf25832014-12-18 17:42:32 -08002282void SSL_set_psk_server_callback(
David Benjamine8814df2015-09-15 08:05:54 -04002283 SSL *ssl, unsigned (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
2284 unsigned max_psk_len)) {
2285 ssl->psk_server_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002286}
Adam Langley95c29f32014-06-20 12:00:00 -07002287
Adam Langleyfcf25832014-12-18 17:42:32 -08002288void SSL_CTX_set_psk_server_callback(
David Benjamine8814df2015-09-15 08:05:54 -04002289 SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *identity,
2290 uint8_t *psk, unsigned max_psk_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002291 ctx->psk_server_callback = cb;
2292}
Adam Langley95c29f32014-06-20 12:00:00 -07002293
Adam Langleyfcf25832014-12-18 17:42:32 -08002294void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
2295 void (*cb)(int write_p, int version,
2296 int content_type, const void *buf,
2297 size_t len, SSL *ssl, void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002298 ctx->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002299}
David Benjamin61ecccf2015-05-05 09:44:51 -04002300
2301void SSL_CTX_set_msg_callback_arg(SSL_CTX *ctx, void *arg) {
2302 ctx->msg_callback_arg = arg;
2303}
2304
Adam Langleyfcf25832014-12-18 17:42:32 -08002305void SSL_set_msg_callback(SSL *ssl,
2306 void (*cb)(int write_p, int version, int content_type,
2307 const void *buf, size_t len, SSL *ssl,
2308 void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002309 ssl->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002310}
Adam Langley95c29f32014-06-20 12:00:00 -07002311
David Benjamin61ecccf2015-05-05 09:44:51 -04002312void SSL_set_msg_callback_arg(SSL *ssl, void *arg) {
2313 ssl->msg_callback_arg = arg;
2314}
2315
David Benjamind28f59c2015-11-17 22:32:50 -05002316void SSL_CTX_set_keylog_callback(SSL_CTX *ctx,
2317 void (*cb)(const SSL *ssl, const char *line)) {
2318 ctx->keylog_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002319}
Adam Langley95c29f32014-06-20 12:00:00 -07002320
David Benjamin6e3f5cc2016-09-14 14:43:14 -04002321void (*SSL_CTX_get_keylog_callback(const SSL_CTX *ctx))(const SSL *ssl,
2322 const char *line) {
2323 return ctx->keylog_callback;
2324}
2325
Taylor Brandstetter9edb2c62016-06-08 15:26:59 -07002326void SSL_CTX_set_current_time_cb(SSL_CTX *ctx,
2327 void (*cb)(const SSL *ssl,
2328 struct timeval *out_clock)) {
2329 ctx->current_time_cb = cb;
2330}
2331
David Benjamin5d8b1282015-10-17 23:26:35 -04002332int SSL_is_init_finished(const SSL *ssl) {
David Benjamin9e766d72017-02-10 23:14:17 -05002333 return !SSL_in_init(ssl);
David Benjamin5d8b1282015-10-17 23:26:35 -04002334}
2335
2336int SSL_in_init(const SSL *ssl) {
Steven Valdezd8168742017-08-31 10:15:48 -04002337 // This returns false once all the handshake state has been finalized, to
2338 // allow callbacks and getters based on SSL_in_init to return the correct
2339 // values.
2340 SSL_HANDSHAKE *hs = ssl->s3->hs;
2341 return hs != nullptr && !hs->handshake_finalized;
David Benjamin5d8b1282015-10-17 23:26:35 -04002342}
2343
2344int SSL_in_false_start(const SSL *ssl) {
David Benjamina0486782016-10-06 19:11:32 -04002345 if (ssl->s3->hs == NULL) {
2346 return 0;
2347 }
2348 return ssl->s3->hs->in_false_start;
David Benjamined7c4752015-02-16 19:16:46 -05002349}
2350
David Benjamin0d56f882015-12-19 17:05:56 -05002351int SSL_cutthrough_complete(const SSL *ssl) {
2352 return SSL_in_false_start(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08002353}
Adam Langley95c29f32014-06-20 12:00:00 -07002354
Adam Langleyfcf25832014-12-18 17:42:32 -08002355void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
2356 size_t *ssl_session_size) {
2357 *ssl_size = sizeof(SSL);
2358 *ssl_ctx_size = sizeof(SSL_CTX);
2359 *ssl_session_size = sizeof(SSL_SESSION);
2360}
Feng Lu41aa3252014-11-21 22:47:56 -08002361
David Benjamince079fd2016-08-02 16:22:34 -04002362int SSL_is_server(const SSL *ssl) { return ssl->server; }
2363
2364int SSL_is_dtls(const SSL *ssl) { return ssl->method->is_dtls; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002365
Alessandro Ghedini57e81e62017-03-14 23:36:00 +00002366void SSL_CTX_set_select_certificate_cb(
2367 SSL_CTX *ctx,
2368 enum ssl_select_cert_result_t (*cb)(const SSL_CLIENT_HELLO *)) {
David Benjamind4c2bce2015-10-17 12:28:18 -04002369 ctx->select_certificate_cb = cb;
2370}
2371
David Benjamin731058e2016-12-03 23:15:13 -05002372void SSL_CTX_set_dos_protection_cb(SSL_CTX *ctx,
2373 int (*cb)(const SSL_CLIENT_HELLO *)) {
Adam Langley524e7172015-02-20 16:04:00 -08002374 ctx->dos_protection_cb = cb;
2375}
2376
David Benjamin1d5ef3b2015-10-12 19:54:18 -04002377void SSL_set_renegotiate_mode(SSL *ssl, enum ssl_renegotiate_mode_t mode) {
2378 ssl->renegotiate_mode = mode;
2379}
2380
Adam Langleyc2d32802015-11-03 18:36:10 -08002381int SSL_get_ivs(const SSL *ssl, const uint8_t **out_read_iv,
2382 const uint8_t **out_write_iv, size_t *out_iv_len) {
Adam Langleyc2d32802015-11-03 18:36:10 -08002383 size_t write_iv_len;
David Benjamincfc11c22017-07-18 22:45:18 -04002384 if (!ssl->s3->aead_read_ctx->GetIV(out_read_iv, out_iv_len) ||
2385 !ssl->s3->aead_write_ctx->GetIV(out_write_iv, &write_iv_len) ||
Adam Langleyc2d32802015-11-03 18:36:10 -08002386 *out_iv_len != write_iv_len) {
2387 return 0;
2388 }
2389
2390 return 1;
David Benjamine348ff42015-11-06 17:55:30 -05002391}
David Benjamin6e807652015-11-02 12:02:20 -05002392
David Benjamin91b25012016-01-21 16:39:58 -05002393static uint64_t be_to_u64(const uint8_t in[8]) {
David Benjaminde942382016-02-11 12:02:01 -05002394 return (((uint64_t)in[0]) << 56) | (((uint64_t)in[1]) << 48) |
2395 (((uint64_t)in[2]) << 40) | (((uint64_t)in[3]) << 32) |
2396 (((uint64_t)in[4]) << 24) | (((uint64_t)in[5]) << 16) |
2397 (((uint64_t)in[6]) << 8) | ((uint64_t)in[7]);
David Benjamin91b25012016-01-21 16:39:58 -05002398}
2399
2400uint64_t SSL_get_read_sequence(const SSL *ssl) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002401 // TODO(davidben): Internally represent sequence numbers as uint64_t.
David Benjamince079fd2016-08-02 16:22:34 -04002402 if (SSL_is_dtls(ssl)) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002403 // max_seq_num already includes the epoch.
David Benjaminde942382016-02-11 12:02:01 -05002404 assert(ssl->d1->r_epoch == (ssl->d1->bitmap.max_seq_num >> 48));
2405 return ssl->d1->bitmap.max_seq_num;
2406 }
David Benjamin91b25012016-01-21 16:39:58 -05002407 return be_to_u64(ssl->s3->read_sequence);
2408}
2409
2410uint64_t SSL_get_write_sequence(const SSL *ssl) {
David Benjaminde942382016-02-11 12:02:01 -05002411 uint64_t ret = be_to_u64(ssl->s3->write_sequence);
David Benjamince079fd2016-08-02 16:22:34 -04002412 if (SSL_is_dtls(ssl)) {
David Benjaminde942382016-02-11 12:02:01 -05002413 assert((ret >> 48) == 0);
2414 ret |= ((uint64_t)ssl->d1->w_epoch) << 48;
2415 }
2416 return ret;
David Benjamin91b25012016-01-21 16:39:58 -05002417}
2418
Steven Valdez02563852016-06-23 13:33:05 -04002419uint16_t SSL_get_peer_signature_algorithm(const SSL *ssl) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002420 // TODO(davidben): This checks the wrong session if there is a renegotiation
2421 // in progress.
David Benjaminf1050fd2016-12-13 20:05:36 -05002422 SSL_SESSION *session = SSL_get_session(ssl);
2423 if (session == NULL) {
2424 return 0;
2425 }
2426
2427 return session->peer_signature_algorithm;
Steven Valdez02563852016-06-23 13:33:05 -04002428}
2429
David Benjamin96ba15f2015-12-25 14:10:17 -05002430size_t SSL_get_client_random(const SSL *ssl, uint8_t *out, size_t max_out) {
2431 if (max_out == 0) {
2432 return sizeof(ssl->s3->client_random);
2433 }
2434 if (max_out > sizeof(ssl->s3->client_random)) {
2435 max_out = sizeof(ssl->s3->client_random);
2436 }
David Benjamin17cf2cb2016-12-13 01:07:13 -05002437 OPENSSL_memcpy(out, ssl->s3->client_random, max_out);
David Benjamin96ba15f2015-12-25 14:10:17 -05002438 return max_out;
2439}
2440
2441size_t SSL_get_server_random(const SSL *ssl, uint8_t *out, size_t max_out) {
2442 if (max_out == 0) {
2443 return sizeof(ssl->s3->server_random);
2444 }
2445 if (max_out > sizeof(ssl->s3->server_random)) {
2446 max_out = sizeof(ssl->s3->server_random);
2447 }
David Benjamin17cf2cb2016-12-13 01:07:13 -05002448 OPENSSL_memcpy(out, ssl->s3->server_random, max_out);
David Benjamin96ba15f2015-12-25 14:10:17 -05002449 return max_out;
2450}
2451
David Benjaminf6494f42016-01-07 12:37:41 -08002452const SSL_CIPHER *SSL_get_pending_cipher(const SSL *ssl) {
David Benjamin45738dd2017-02-09 20:01:26 -05002453 SSL_HANDSHAKE *hs = ssl->s3->hs;
2454 if (hs == NULL) {
David Benjaminf6494f42016-01-07 12:37:41 -08002455 return NULL;
2456 }
David Benjamin45738dd2017-02-09 20:01:26 -05002457 return hs->new_cipher;
David Benjaminf6494f42016-01-07 12:37:41 -08002458}
2459
David Benjaminbbaf3672016-11-17 10:53:09 +09002460void SSL_set_retain_only_sha256_of_client_certs(SSL *ssl, int enabled) {
2461 ssl->retain_only_sha256_of_client_certs = !!enabled;
2462}
2463
David Benjamincdd0b7e2016-01-07 10:10:41 -08002464void SSL_CTX_set_retain_only_sha256_of_client_certs(SSL_CTX *ctx, int enabled) {
2465 ctx->retain_only_sha256_of_client_certs = !!enabled;
2466}
2467
David Benjamin65ac9972016-09-02 21:35:25 -04002468void SSL_CTX_set_grease_enabled(SSL_CTX *ctx, int enabled) {
2469 ctx->grease_enabled = !!enabled;
2470}
2471
David Benjamin35ac5b72017-03-03 15:05:56 -05002472int32_t SSL_get_ticket_age_skew(const SSL *ssl) {
2473 return ssl->s3->ticket_age_skew;
2474}
2475
David Benjamin27bbae42015-09-13 00:54:37 -04002476int SSL_clear(SSL *ssl) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002477 // In OpenSSL, reusing a client |SSL| with |SSL_clear| causes the previously
2478 // established session to be offered the next time around. wpa_supplicant
2479 // depends on this behavior, so emulate it.
David Benjaminb79cc842016-12-07 15:57:14 -05002480 SSL_SESSION *session = NULL;
2481 if (!ssl->server && ssl->s3->established_session != NULL) {
2482 session = ssl->s3->established_session;
2483 SSL_SESSION_up_ref(session);
2484 }
2485
David Benjaminc11ea9422017-08-29 16:33:21 -04002486 // TODO(davidben): Some state on |ssl| is reset both in |SSL_new| and
2487 // |SSL_clear| because it is per-connection state rather than configuration
2488 // state. Per-connection state should be on |ssl->s3| and |ssl->d1| so it is
2489 // naturally reset at the right points between |SSL_new|, |SSL_clear|, and
2490 // |ssl3_new|.
David Benjamin27bbae42015-09-13 00:54:37 -04002491
2492 ssl->rwstate = SSL_NOTHING;
2493
2494 BUF_MEM_free(ssl->init_buf);
2495 ssl->init_buf = NULL;
2496
David Benjaminc11ea9422017-08-29 16:33:21 -04002497 // The ssl->d1->mtu is simultaneously configuration (preserved across
2498 // clear) and connection-specific state (gets reset).
2499 //
2500 // TODO(davidben): Avoid this.
David Benjamin27bbae42015-09-13 00:54:37 -04002501 unsigned mtu = 0;
2502 if (ssl->d1 != NULL) {
2503 mtu = ssl->d1->mtu;
2504 }
2505
2506 ssl->method->ssl_free(ssl);
2507 if (!ssl->method->ssl_new(ssl)) {
David Benjaminb79cc842016-12-07 15:57:14 -05002508 SSL_SESSION_free(session);
David Benjamin27bbae42015-09-13 00:54:37 -04002509 return 0;
2510 }
David Benjamin27bbae42015-09-13 00:54:37 -04002511
David Benjamince079fd2016-08-02 16:22:34 -04002512 if (SSL_is_dtls(ssl) && (SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
David Benjamin27bbae42015-09-13 00:54:37 -04002513 ssl->d1->mtu = mtu;
2514 }
2515
David Benjaminb79cc842016-12-07 15:57:14 -05002516 if (session != NULL) {
2517 SSL_set_session(ssl, session);
2518 SSL_SESSION_free(session);
2519 }
2520
David Benjamin27bbae42015-09-13 00:54:37 -04002521 return 1;
2522}
2523
David Benjaminda881e92015-04-26 14:45:04 -04002524int SSL_CTX_sess_connect(const SSL_CTX *ctx) { return 0; }
2525int SSL_CTX_sess_connect_good(const SSL_CTX *ctx) { return 0; }
2526int SSL_CTX_sess_connect_renegotiate(const SSL_CTX *ctx) { return 0; }
2527int SSL_CTX_sess_accept(const SSL_CTX *ctx) { return 0; }
2528int SSL_CTX_sess_accept_renegotiate(const SSL_CTX *ctx) { return 0; }
2529int SSL_CTX_sess_accept_good(const SSL_CTX *ctx) { return 0; }
2530int SSL_CTX_sess_hits(const SSL_CTX *ctx) { return 0; }
2531int SSL_CTX_sess_cb_hits(const SSL_CTX *ctx) { return 0; }
2532int SSL_CTX_sess_misses(const SSL_CTX *ctx) { return 0; }
2533int SSL_CTX_sess_timeouts(const SSL_CTX *ctx) { return 0; }
2534int SSL_CTX_sess_cache_full(const SSL_CTX *ctx) { return 0; }
David Benjamin936aada2016-06-07 19:09:22 -04002535
2536int SSL_num_renegotiations(const SSL *ssl) {
2537 return SSL_total_renegotiations(ssl);
2538}
2539
2540int SSL_CTX_need_tmp_RSA(const SSL_CTX *ctx) { return 0; }
2541int SSL_need_tmp_RSA(const SSL *ssl) { return 0; }
2542int SSL_CTX_set_tmp_rsa(SSL_CTX *ctx, const RSA *rsa) { return 1; }
2543int SSL_set_tmp_rsa(SSL *ssl, const RSA *rsa) { return 1; }
Matt Braithwaite4838d8a2015-08-20 13:20:03 -07002544void ERR_load_SSL_strings(void) {}
David Benjamin27bbae42015-09-13 00:54:37 -04002545void SSL_load_error_strings(void) {}
David Benjamin936aada2016-06-07 19:09:22 -04002546int SSL_cache_hit(SSL *ssl) { return SSL_session_reused(ssl); }
2547
2548int SSL_CTX_set_tmp_ecdh(SSL_CTX *ctx, const EC_KEY *ec_key) {
2549 if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) {
2550 OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
2551 return 0;
2552 }
2553 int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key));
2554 return SSL_CTX_set1_curves(ctx, &nid, 1);
2555}
2556
2557int SSL_set_tmp_ecdh(SSL *ssl, const EC_KEY *ec_key) {
2558 if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) {
2559 OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
2560 return 0;
2561 }
2562 int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key));
2563 return SSL_set1_curves(ssl, &nid, 1);
2564}
David Benjamin721e8b72016-08-03 13:13:17 -04002565
Adam Langley4c341d02017-03-08 19:33:21 -08002566void SSL_CTX_set_ticket_aead_method(SSL_CTX *ctx,
2567 const SSL_TICKET_AEAD_METHOD *aead_method) {
2568 ctx->ticket_aead_method = aead_method;
2569}