blob: f62d1556114005349d8e2969d4d9d62026864058 [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
David Benjamin81f030b2016-08-12 14:48:19 -0400432int OPENSSL_init_ssl(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings) {
433 CRYPTO_library_init();
434 return 1;
435}
436
Adam Langleye976e432016-02-15 14:01:10 -0800437static uint32_t ssl_session_hash(const SSL_SESSION *sess) {
438 const uint8_t *session_id = sess->session_id;
439
440 uint8_t tmp_storage[sizeof(uint32_t)];
441 if (sess->session_id_length < sizeof(tmp_storage)) {
David Benjamin17cf2cb2016-12-13 01:07:13 -0500442 OPENSSL_memset(tmp_storage, 0, sizeof(tmp_storage));
443 OPENSSL_memcpy(tmp_storage, sess->session_id, sess->session_id_length);
Adam Langleye976e432016-02-15 14:01:10 -0800444 session_id = tmp_storage;
445 }
446
David Benjamin27bbae42015-09-13 00:54:37 -0400447 uint32_t hash =
Adam Langleye976e432016-02-15 14:01:10 -0800448 ((uint32_t)session_id[0]) |
449 ((uint32_t)session_id[1] << 8) |
450 ((uint32_t)session_id[2] << 16) |
451 ((uint32_t)session_id[3] << 24);
David Benjamin7c6d09b2015-09-05 11:00:19 -0400452
David Benjamin27bbae42015-09-13 00:54:37 -0400453 return hash;
454}
455
David Benjaminc11ea9422017-08-29 16:33:21 -0400456// NB: If this function (or indeed the hash function which uses a sort of
457// coarser function than this one) is changed, ensure
458// SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
459// able to construct an SSL_SESSION that will collide with any existing session
460// with a matching session ID.
David Benjamin27bbae42015-09-13 00:54:37 -0400461static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
462 if (a->ssl_version != b->ssl_version) {
463 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800464 }
Adam Langley95c29f32014-06-20 12:00:00 -0700465
David Benjamin27bbae42015-09-13 00:54:37 -0400466 if (a->session_id_length != b->session_id_length) {
467 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800468 }
Adam Langley95c29f32014-06-20 12:00:00 -0700469
David Benjamin17cf2cb2016-12-13 01:07:13 -0500470 return OPENSSL_memcmp(a->session_id, b->session_id, a->session_id_length);
David Benjamin27bbae42015-09-13 00:54:37 -0400471}
Adam Langley95c29f32014-06-20 12:00:00 -0700472
David Benjamin27bbae42015-09-13 00:54:37 -0400473SSL_CTX *SSL_CTX_new(const SSL_METHOD *method) {
474 SSL_CTX *ret = NULL;
475
476 if (method == NULL) {
477 OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_METHOD_PASSED);
478 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800479 }
Adam Langley95c29f32014-06-20 12:00:00 -0700480
David Benjaminf5260812017-07-12 17:16:39 -0400481 ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
David Benjamin27bbae42015-09-13 00:54:37 -0400482 if (ret == NULL) {
483 goto err;
David Benjamin62fd1622015-01-11 13:30:01 -0500484 }
485
David Benjamin17cf2cb2016-12-13 01:07:13 -0500486 OPENSSL_memset(ret, 0, sizeof(SSL_CTX));
Adam Langley95c29f32014-06-20 12:00:00 -0700487
David Benjamin27bbae42015-09-13 00:54:37 -0400488 ret->method = method->method;
Adam Langley3509dac2017-02-01 11:59:18 -0800489 ret->x509_method = method->x509_method;
David Benjamin27bbae42015-09-13 00:54:37 -0400490
491 CRYPTO_MUTEX_init(&ret->lock);
492
493 ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
494 ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
495
David Benjamin27bbae42015-09-13 00:54:37 -0400496 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
David Benjamin17b30832017-01-28 14:00:32 -0500497 ret->session_psk_dhe_timeout = SSL_DEFAULT_SESSION_PSK_DHE_TIMEOUT;
David Benjamin27bbae42015-09-13 00:54:37 -0400498
499 ret->references = 1;
500
501 ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
502 ret->verify_mode = SSL_VERIFY_NONE;
Adam Langley3509dac2017-02-01 11:59:18 -0800503 ret->cert = ssl_cert_new(method->x509_method);
David Benjamin27bbae42015-09-13 00:54:37 -0400504 if (ret->cert == NULL) {
505 goto err;
506 }
507
508 ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
509 if (ret->sessions == NULL) {
510 goto err;
511 }
Adam Langley2a3b3432017-02-10 13:56:48 -0800512
513 if (!ret->x509_method->ssl_ctx_new(ret)) {
David Benjamin27bbae42015-09-13 00:54:37 -0400514 goto err;
515 }
516
Matthew Braithwaite6ad20dc2017-02-21 16:41:33 -0800517 if (!SSL_CTX_set_strict_cipher_list(ret, SSL_DEFAULT_CIPHER_LIST)) {
David Benjamin27bbae42015-09-13 00:54:37 -0400518 goto err2;
519 }
520
Adam Langley34b4c822017-02-02 10:57:17 -0800521 ret->client_CA = sk_CRYPTO_BUFFER_new_null();
David Benjamin27bbae42015-09-13 00:54:37 -0400522 if (ret->client_CA == NULL) {
523 goto err;
524 }
525
David Benjamin8a589332015-12-04 23:14:35 -0500526 CRYPTO_new_ex_data(&ret->ex_data);
David Benjamin27bbae42015-09-13 00:54:37 -0400527
528 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
529
David Benjaminc11ea9422017-08-29 16:33:21 -0400530 // Disable the auto-chaining feature by default. Once this has stuck without
531 // problems, the feature will be removed entirely.
David Benjamina983b4c2016-11-09 14:21:12 -0500532 ret->mode = SSL_MODE_NO_AUTO_CHAIN;
533
David Benjaminc11ea9422017-08-29 16:33:21 -0400534 // Lock the SSL_CTX to the specified version, for compatibility with legacy
535 // uses of SSL_METHOD, but we do not set the minimum version for
536 // |SSLv3_method|.
David Benjamine34bcc92016-09-21 16:53:09 -0400537 if (!SSL_CTX_set_max_proto_version(ret, method->version) ||
David Benjamin3cfeb952017-03-01 16:48:38 -0500538 !SSL_CTX_set_min_proto_version(ret, method->version == SSL3_VERSION
David Benjaminc11ea9422017-08-29 16:33:21 -0400539 ? 0 // default
David Benjamin3cfeb952017-03-01 16:48:38 -0500540 : method->version)) {
David Benjamine34bcc92016-09-21 16:53:09 -0400541 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
542 goto err2;
David Benjamin27bbae42015-09-13 00:54:37 -0400543 }
544
545 return ret;
546
547err:
548 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
549err2:
550 SSL_CTX_free(ret);
551 return NULL;
552}
553
Adam Langley84cd1592016-06-30 18:29:44 -0700554int SSL_CTX_up_ref(SSL_CTX *ctx) {
555 CRYPTO_refcount_inc(&ctx->references);
556 return 1;
557}
558
David Benjamin27bbae42015-09-13 00:54:37 -0400559void SSL_CTX_free(SSL_CTX *ctx) {
560 if (ctx == NULL ||
561 !CRYPTO_refcount_dec_and_test_zero(&ctx->references)) {
562 return;
563 }
564
David Benjaminc11ea9422017-08-29 16:33:21 -0400565 // Free internal session cache. However: the remove_cb() may reference the
566 // ex_data of SSL_CTX, thus the ex_data store can only be removed after the
567 // sessions were flushed. As the ex_data handling routines might also touch
568 // the session cache, the most secure solution seems to be: empty (flush) the
569 // cache, then free ex_data, then finally free the cache. (See ticket
570 // [openssl.org #212].)
David Benjamin27bbae42015-09-13 00:54:37 -0400571 SSL_CTX_flush_sessions(ctx, 0);
572
573 CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, ctx, &ctx->ex_data);
574
575 CRYPTO_MUTEX_cleanup(&ctx->lock);
576 lh_SSL_SESSION_free(ctx->sessions);
David Benjamin27bbae42015-09-13 00:54:37 -0400577 ssl_cipher_preference_list_free(ctx->cipher_list);
David Benjamin27bbae42015-09-13 00:54:37 -0400578 ssl_cert_free(ctx->cert);
579 sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->client_custom_extensions,
580 SSL_CUSTOM_EXTENSION_free);
581 sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->server_custom_extensions,
582 SSL_CUSTOM_EXTENSION_free);
Adam Langley34b4c822017-02-02 10:57:17 -0800583 sk_CRYPTO_BUFFER_pop_free(ctx->client_CA, CRYPTO_BUFFER_free);
Adam Langley2a3b3432017-02-10 13:56:48 -0800584 ctx->x509_method->ssl_ctx_free(ctx);
David Benjamin27bbae42015-09-13 00:54:37 -0400585 sk_SRTP_PROTECTION_PROFILE_free(ctx->srtp_profiles);
586 OPENSSL_free(ctx->psk_identity_hint);
Steven Valdezce902a92016-05-17 11:47:53 -0400587 OPENSSL_free(ctx->supported_group_list);
David Benjamin27bbae42015-09-13 00:54:37 -0400588 OPENSSL_free(ctx->alpn_client_proto_list);
David Benjamin27bbae42015-09-13 00:54:37 -0400589 EVP_PKEY_free(ctx->tlsext_channel_id_private);
David Benjamin71c21b42017-04-14 17:05:40 -0400590 OPENSSL_free(ctx->verify_sigalgs);
Martin Kreichgauer72912d22017-08-04 12:06:43 -0700591 OPENSSL_free(ctx->tlsext_ticket_key_current);
592 OPENSSL_free(ctx->tlsext_ticket_key_prev);
David Benjamin27bbae42015-09-13 00:54:37 -0400593
594 OPENSSL_free(ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -0800595}
Adam Langley95c29f32014-06-20 12:00:00 -0700596
Adam Langleyfcf25832014-12-18 17:42:32 -0800597SSL *SSL_new(SSL_CTX *ctx) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800598 if (ctx == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400599 OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_CTX);
Adam Langleyfcf25832014-12-18 17:42:32 -0800600 return NULL;
601 }
602 if (ctx->method == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400603 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
Adam Langleyfcf25832014-12-18 17:42:32 -0800604 return NULL;
605 }
Adam Langley95c29f32014-06-20 12:00:00 -0700606
David Benjaminf5260812017-07-12 17:16:39 -0400607 SSL *ssl = (SSL *)OPENSSL_malloc(sizeof(SSL));
David Benjamin0d56f882015-12-19 17:05:56 -0500608 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800609 goto err;
610 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500611 OPENSSL_memset(ssl, 0, sizeof(SSL));
Adam Langley95c29f32014-06-20 12:00:00 -0700612
David Benjaminfc08dfc2017-06-20 14:39:32 -0400613 ssl->conf_min_version = ctx->conf_min_version;
614 ssl->conf_max_version = ctx->conf_max_version;
Steven Valdez52586f92017-07-11 15:08:32 -0400615 ssl->tls13_variant = ctx->tls13_variant;
David Benjamin1eb367c2014-12-12 18:17:51 -0500616
David Benjaminc11ea9422017-08-29 16:33:21 -0400617 // RFC 6347 states that implementations SHOULD use an initial timer value of
618 // 1 second.
Taylor Brandstetter376a0fe2016-05-10 19:30:28 -0700619 ssl->initial_timeout_duration_ms = 1000;
620
David Benjamin0d56f882015-12-19 17:05:56 -0500621 ssl->options = ctx->options;
622 ssl->mode = ctx->mode;
623 ssl->max_cert_list = ctx->max_cert_list;
Adam Langley95c29f32014-06-20 12:00:00 -0700624
David Benjamin0d56f882015-12-19 17:05:56 -0500625 ssl->cert = ssl_cert_dup(ctx->cert);
626 if (ssl->cert == NULL) {
David Benjamina5a3eeb2015-03-18 20:26:30 -0400627 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -0800628 }
Adam Langley95c29f32014-06-20 12:00:00 -0700629
David Benjamin0d56f882015-12-19 17:05:56 -0500630 ssl->msg_callback = ctx->msg_callback;
631 ssl->msg_callback_arg = ctx->msg_callback_arg;
632 ssl->verify_mode = ctx->verify_mode;
David Benjamin0d56f882015-12-19 17:05:56 -0500633 ssl->verify_callback = ctx->default_verify_callback;
David Benjamin3a1dd462017-07-11 16:13:10 -0400634 ssl->custom_verify_callback = ctx->custom_verify_callback;
David Benjaminbbaf3672016-11-17 10:53:09 +0900635 ssl->retain_only_sha256_of_client_certs =
636 ctx->retain_only_sha256_of_client_certs;
Adam Langley95c29f32014-06-20 12:00:00 -0700637
David Benjamin0d56f882015-12-19 17:05:56 -0500638 ssl->quiet_shutdown = ctx->quiet_shutdown;
639 ssl->max_send_fragment = ctx->max_send_fragment;
Adam Langley95c29f32014-06-20 12:00:00 -0700640
David Benjaminf650c712016-12-22 15:54:51 -0500641 SSL_CTX_up_ref(ctx);
David Benjamin0d56f882015-12-19 17:05:56 -0500642 ssl->ctx = ctx;
David Benjaminf650c712016-12-22 15:54:51 -0500643 SSL_CTX_up_ref(ctx);
David Benjaminbe497062017-03-10 16:08:36 -0500644 ssl->session_ctx = ctx;
Adam Langley95c29f32014-06-20 12:00:00 -0700645
Adam Langley2a3b3432017-02-10 13:56:48 -0800646 if (!ssl->ctx->x509_method->ssl_new(ssl)) {
647 goto err;
648 }
649
Steven Valdezce902a92016-05-17 11:47:53 -0400650 if (ctx->supported_group_list) {
David Benjaminf5260812017-07-12 17:16:39 -0400651 ssl->supported_group_list = (uint16_t *)BUF_memdup(
652 ctx->supported_group_list, ctx->supported_group_list_len * 2);
Steven Valdezce902a92016-05-17 11:47:53 -0400653 if (!ssl->supported_group_list) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800654 goto err;
655 }
Steven Valdezce902a92016-05-17 11:47:53 -0400656 ssl->supported_group_list_len = ctx->supported_group_list_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800657 }
Adam Langley95c29f32014-06-20 12:00:00 -0700658
David Benjamin17b30832017-01-28 14:00:32 -0500659 if (ctx->alpn_client_proto_list) {
David Benjaminf5260812017-07-12 17:16:39 -0400660 ssl->alpn_client_proto_list = (uint8_t *)BUF_memdup(
661 ctx->alpn_client_proto_list, ctx->alpn_client_proto_list_len);
David Benjamin0d56f882015-12-19 17:05:56 -0500662 if (ssl->alpn_client_proto_list == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800663 goto err;
664 }
David Benjamin17b30832017-01-28 14:00:32 -0500665 ssl->alpn_client_proto_list_len = ctx->alpn_client_proto_list_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800666 }
Adam Langley95c29f32014-06-20 12:00:00 -0700667
David Benjamin0d56f882015-12-19 17:05:56 -0500668 ssl->method = ctx->method;
Adam Langley95c29f32014-06-20 12:00:00 -0700669
David Benjamin0d56f882015-12-19 17:05:56 -0500670 if (!ssl->method->ssl_new(ssl)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800671 goto err;
672 }
Adam Langley95c29f32014-06-20 12:00:00 -0700673
David Benjamin0d56f882015-12-19 17:05:56 -0500674 ssl->rwstate = SSL_NOTHING;
Adam Langley95c29f32014-06-20 12:00:00 -0700675
David Benjamin0d56f882015-12-19 17:05:56 -0500676 CRYPTO_new_ex_data(&ssl->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700677
David Benjamin0d56f882015-12-19 17:05:56 -0500678 ssl->psk_identity_hint = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800679 if (ctx->psk_identity_hint) {
David Benjamin0d56f882015-12-19 17:05:56 -0500680 ssl->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
681 if (ssl->psk_identity_hint == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800682 goto err;
683 }
684 }
David Benjamin0d56f882015-12-19 17:05:56 -0500685 ssl->psk_client_callback = ctx->psk_client_callback;
686 ssl->psk_server_callback = ctx->psk_server_callback;
Adam Langley95c29f32014-06-20 12:00:00 -0700687
David Benjamin0d56f882015-12-19 17:05:56 -0500688 ssl->tlsext_channel_id_enabled = ctx->tlsext_channel_id_enabled;
David Benjamin02ddbfd2015-01-11 13:09:11 -0500689 if (ctx->tlsext_channel_id_private) {
Adam Langley310d3f62016-07-12 10:39:20 -0700690 EVP_PKEY_up_ref(ctx->tlsext_channel_id_private);
691 ssl->tlsext_channel_id_private = ctx->tlsext_channel_id_private;
David Benjamin02ddbfd2015-01-11 13:09:11 -0500692 }
693
David Benjamin17b30832017-01-28 14:00:32 -0500694 ssl->signed_cert_timestamps_enabled = ctx->signed_cert_timestamps_enabled;
695 ssl->ocsp_stapling_enabled = ctx->ocsp_stapling_enabled;
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200696
David Benjamin0d56f882015-12-19 17:05:56 -0500697 return ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -0800698
Adam Langley95c29f32014-06-20 12:00:00 -0700699err:
David Benjamin0d56f882015-12-19 17:05:56 -0500700 SSL_free(ssl);
David Benjamin3570d732015-06-29 00:28:17 -0400701 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700702
Adam Langleyfcf25832014-12-18 17:42:32 -0800703 return NULL;
704}
Adam Langley95c29f32014-06-20 12:00:00 -0700705
David Benjamin27bbae42015-09-13 00:54:37 -0400706void SSL_free(SSL *ssl) {
707 if (ssl == NULL) {
708 return;
709 }
710
David Benjamin0391f162017-06-15 15:27:59 -0400711 if (ssl->ctx != NULL) {
712 ssl->ctx->x509_method->ssl_free(ssl);
713 }
714
David Benjamin27bbae42015-09-13 00:54:37 -0400715 CRYPTO_free_ex_data(&g_ex_data_class_ssl, ssl, &ssl->ex_data);
716
David Benjamin27bbae42015-09-13 00:54:37 -0400717 BIO_free_all(ssl->rbio);
David Benjaminf715c422016-06-11 19:01:56 -0400718 BIO_free_all(ssl->wbio);
David Benjamin27bbae42015-09-13 00:54:37 -0400719
720 BUF_MEM_free(ssl->init_buf);
721
David Benjaminc11ea9422017-08-29 16:33:21 -0400722 // add extra stuff
David Benjamin27bbae42015-09-13 00:54:37 -0400723 ssl_cipher_preference_list_free(ssl->cipher_list);
David Benjamin27bbae42015-09-13 00:54:37 -0400724
David Benjamin27bbae42015-09-13 00:54:37 -0400725 SSL_SESSION_free(ssl->session);
726
David Benjamin27bbae42015-09-13 00:54:37 -0400727 ssl_cert_free(ssl->cert);
728
729 OPENSSL_free(ssl->tlsext_hostname);
David Benjaminbe497062017-03-10 16:08:36 -0500730 SSL_CTX_free(ssl->session_ctx);
Steven Valdezce902a92016-05-17 11:47:53 -0400731 OPENSSL_free(ssl->supported_group_list);
David Benjamin27bbae42015-09-13 00:54:37 -0400732 OPENSSL_free(ssl->alpn_client_proto_list);
733 EVP_PKEY_free(ssl->tlsext_channel_id_private);
734 OPENSSL_free(ssl->psk_identity_hint);
Adam Langley34b4c822017-02-02 10:57:17 -0800735 sk_CRYPTO_BUFFER_pop_free(ssl->client_CA, CRYPTO_BUFFER_free);
David Benjamin27bbae42015-09-13 00:54:37 -0400736 sk_SRTP_PROTECTION_PROFILE_free(ssl->srtp_profiles);
737
738 if (ssl->method != NULL) {
739 ssl->method->ssl_free(ssl);
740 }
741 SSL_CTX_free(ssl->ctx);
742
743 OPENSSL_free(ssl);
744}
745
746void SSL_set_connect_state(SSL *ssl) {
747 ssl->server = 0;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400748 ssl->do_handshake = ssl_client_handshake;
David Benjamin27bbae42015-09-13 00:54:37 -0400749}
750
751void SSL_set_accept_state(SSL *ssl) {
752 ssl->server = 1;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400753 ssl->do_handshake = ssl_server_handshake;
David Benjamin27bbae42015-09-13 00:54:37 -0400754}
755
David Benjamin4501bd52016-08-01 13:39:41 -0400756void SSL_set0_rbio(SSL *ssl, BIO *rbio) {
David Benjaminf715c422016-06-11 19:01:56 -0400757 BIO_free_all(ssl->rbio);
758 ssl->rbio = rbio;
759}
760
David Benjamin4501bd52016-08-01 13:39:41 -0400761void SSL_set0_wbio(SSL *ssl, BIO *wbio) {
David Benjaminf715c422016-06-11 19:01:56 -0400762 BIO_free_all(ssl->wbio);
David Benjamin27bbae42015-09-13 00:54:37 -0400763 ssl->wbio = wbio;
764}
765
David Benjaminf715c422016-06-11 19:01:56 -0400766void SSL_set_bio(SSL *ssl, BIO *rbio, BIO *wbio) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400767 // For historical reasons, this function has many different cases in ownership
768 // handling.
David Benjaminf715c422016-06-11 19:01:56 -0400769
David Benjaminc11ea9422017-08-29 16:33:21 -0400770 // If nothing has changed, do nothing
David Benjamin4501bd52016-08-01 13:39:41 -0400771 if (rbio == SSL_get_rbio(ssl) && wbio == SSL_get_wbio(ssl)) {
772 return;
773 }
774
David Benjaminc11ea9422017-08-29 16:33:21 -0400775 // If the two arguments are equal, one fewer reference is granted than
776 // taken.
David Benjaminf715c422016-06-11 19:01:56 -0400777 if (rbio != NULL && rbio == wbio) {
778 BIO_up_ref(rbio);
779 }
780
David Benjaminc11ea9422017-08-29 16:33:21 -0400781 // If only the wbio is changed, adopt only one reference.
David Benjaminf715c422016-06-11 19:01:56 -0400782 if (rbio == SSL_get_rbio(ssl)) {
David Benjamin4501bd52016-08-01 13:39:41 -0400783 SSL_set0_wbio(ssl, wbio);
David Benjaminf715c422016-06-11 19:01:56 -0400784 return;
785 }
David Benjamin4501bd52016-08-01 13:39:41 -0400786
David Benjaminc11ea9422017-08-29 16:33:21 -0400787 // There is an asymmetry here for historical reasons. If only the rbio is
788 // changed AND the rbio and wbio were originally different, then we only adopt
789 // one reference.
David Benjamin4501bd52016-08-01 13:39:41 -0400790 if (wbio == SSL_get_wbio(ssl) && SSL_get_rbio(ssl) != SSL_get_wbio(ssl)) {
791 SSL_set0_rbio(ssl, rbio);
David Benjaminf715c422016-06-11 19:01:56 -0400792 return;
793 }
794
David Benjaminc11ea9422017-08-29 16:33:21 -0400795 // Otherwise, adopt both references.
David Benjamin4501bd52016-08-01 13:39:41 -0400796 SSL_set0_rbio(ssl, rbio);
797 SSL_set0_wbio(ssl, wbio);
David Benjaminf715c422016-06-11 19:01:56 -0400798}
799
David Benjamin27bbae42015-09-13 00:54:37 -0400800BIO *SSL_get_rbio(const SSL *ssl) { return ssl->rbio; }
801
David Benjamindaf207a2017-01-03 18:37:41 -0500802BIO *SSL_get_wbio(const SSL *ssl) { return ssl->wbio; }
David Benjamin27bbae42015-09-13 00:54:37 -0400803
David Benjamin2be4aa72017-01-02 07:56:51 -0500804int SSL_do_handshake(SSL *ssl) {
805 ssl_reset_error_state(ssl);
David Benjamin15c14882016-03-14 14:25:46 -0400806
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400807 if (ssl->do_handshake == NULL) {
David Benjamin27bbae42015-09-13 00:54:37 -0400808 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_TYPE_NOT_SET);
809 return -1;
810 }
811
812 if (!SSL_in_init(ssl)) {
813 return 1;
814 }
815
David Benjaminc11ea9422017-08-29 16:33:21 -0400816 // Run the handshake.
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400817 SSL_HANDSHAKE *hs = ssl->s3->hs;
818
David Benjamin74795b32017-08-31 15:13:12 -0400819 bool early_return = false;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400820 int ret = ssl_run_handshake(hs, &early_return);
821 ssl_do_info_callback(
822 ssl, ssl->server ? SSL_CB_ACCEPT_EXIT : SSL_CB_CONNECT_EXIT, ret);
David Benjamince8c9d22016-11-14 10:45:16 +0900823 if (ret <= 0) {
824 return ret;
825 }
826
David Benjaminc11ea9422017-08-29 16:33:21 -0400827 // Destroy the handshake object if the handshake has completely finished.
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400828 if (!early_return) {
David Benjamince8c9d22016-11-14 10:45:16 +0900829 ssl_handshake_free(ssl->s3->hs);
830 ssl->s3->hs = NULL;
831 }
832
833 return 1;
David Benjamin27bbae42015-09-13 00:54:37 -0400834}
835
836int SSL_connect(SSL *ssl) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400837 if (ssl->do_handshake == NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400838 // Not properly initialized yet
David Benjamin27bbae42015-09-13 00:54:37 -0400839 SSL_set_connect_state(ssl);
840 }
841
David Benjamin15c14882016-03-14 14:25:46 -0400842 return SSL_do_handshake(ssl);
David Benjamin27bbae42015-09-13 00:54:37 -0400843}
844
845int SSL_accept(SSL *ssl) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400846 if (ssl->do_handshake == NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400847 // Not properly initialized yet
David Benjamin27bbae42015-09-13 00:54:37 -0400848 SSL_set_accept_state(ssl);
849 }
850
David Benjamin15c14882016-03-14 14:25:46 -0400851 return SSL_do_handshake(ssl);
David Benjamin27bbae42015-09-13 00:54:37 -0400852}
853
David Benjamin7934f082017-08-01 16:32:25 -0400854static int ssl_do_post_handshake(SSL *ssl, const SSLMessage &msg) {
855 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
856 return tls13_post_handshake(ssl, msg);
857 }
858
David Benjaminc11ea9422017-08-29 16:33:21 -0400859 // We do not accept renegotiations as a server or SSL 3.0. SSL 3.0 will be
860 // removed entirely in the future and requires retaining more data for
861 // renegotiation_info.
David Benjamin34941c02016-10-08 11:45:31 -0400862 if (ssl->server || ssl->version == SSL3_VERSION) {
David Benjamin163f29a2016-07-28 11:05:58 -0400863 goto no_renegotiation;
864 }
865
David Benjamin7934f082017-08-01 16:32:25 -0400866 if (msg.type != SSL3_MT_HELLO_REQUEST || CBS_len(&msg.body) != 0) {
David Benjamin163f29a2016-07-28 11:05:58 -0400867 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
868 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HELLO_REQUEST);
869 return 0;
870 }
871
872 switch (ssl->renegotiate_mode) {
873 case ssl_renegotiate_ignore:
David Benjaminc11ea9422017-08-29 16:33:21 -0400874 // Ignore the HelloRequest.
David Benjamin163f29a2016-07-28 11:05:58 -0400875 return 1;
876
877 case ssl_renegotiate_once:
878 if (ssl->s3->total_renegotiations != 0) {
879 goto no_renegotiation;
880 }
881 break;
882
883 case ssl_renegotiate_never:
884 goto no_renegotiation;
885
886 case ssl_renegotiate_freely:
887 break;
888 }
889
David Benjaminc11ea9422017-08-29 16:33:21 -0400890 // Renegotiation is only supported at quiescent points in the application
891 // protocol, namely in HTTPS, just before reading the HTTP response. Require
892 // the record-layer be idle and avoid complexities of sending a handshake
893 // record while an application_data record is being written.
David Benjamin163f29a2016-07-28 11:05:58 -0400894 if (ssl_write_buffer_is_pending(ssl)) {
895 goto no_renegotiation;
896 }
897
David Benjaminc11ea9422017-08-29 16:33:21 -0400898 // Begin a new handshake.
David Benjamin2644a132016-12-11 13:41:17 -0500899 if (ssl->s3->hs != NULL) {
900 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
901 return 0;
902 }
903 ssl->s3->hs = ssl_handshake_new(ssl);
904 if (ssl->s3->hs == NULL) {
905 return 0;
906 }
907
David Benjamin163f29a2016-07-28 11:05:58 -0400908 ssl->s3->total_renegotiations++;
David Benjamin163f29a2016-07-28 11:05:58 -0400909 return 1;
910
911no_renegotiation:
912 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_NO_RENEGOTIATION);
913 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION);
914 return 0;
915}
916
David Benjamin15c14882016-03-14 14:25:46 -0400917static int ssl_read_impl(SSL *ssl, void *buf, int num, int peek) {
David Benjamin2be4aa72017-01-02 07:56:51 -0500918 ssl_reset_error_state(ssl);
David Benjamin15c14882016-03-14 14:25:46 -0400919
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400920 if (ssl->do_handshake == NULL) {
David Benjamin27bbae42015-09-13 00:54:37 -0400921 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
922 return -1;
923 }
924
David Benjamin163f29a2016-07-28 11:05:58 -0400925 for (;;) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400926 // Complete the current handshake, if any. False Start will cause
927 // |SSL_do_handshake| to return mid-handshake, so this may require multiple
928 // iterations.
Steven Valdez681eb6a2016-12-19 13:19:29 -0500929 while (!ssl_can_read(ssl)) {
David Benjamin163f29a2016-07-28 11:05:58 -0400930 int ret = SSL_do_handshake(ssl);
931 if (ret < 0) {
932 return ret;
933 }
934 if (ret == 0) {
935 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
936 return -1;
937 }
938 }
939
David Benjamin74795b32017-08-31 15:13:12 -0400940 bool got_handshake = false;
David Benjaminf5260812017-07-12 17:16:39 -0400941 int ret = ssl->method->read_app_data(ssl, &got_handshake, (uint8_t *)buf,
942 num, peek);
David Benjamin163f29a2016-07-28 11:05:58 -0400943 if (ret > 0 || !got_handshake) {
Steven Valdez32635b82016-08-16 11:25:03 -0400944 ssl->s3->key_update_count = 0;
David Benjaminc79845c2016-03-10 01:28:00 -0500945 return ret;
946 }
David Benjamin163f29a2016-07-28 11:05:58 -0400947
David Benjaminc11ea9422017-08-29 16:33:21 -0400948 // If we received an interrupt in early read (the end_of_early_data alert),
949 // loop again for the handshake to process it.
Steven Valdez681eb6a2016-12-19 13:19:29 -0500950 if (SSL_in_init(ssl)) {
951 continue;
952 }
953
David Benjamin7934f082017-08-01 16:32:25 -0400954 SSLMessage msg;
955 while (ssl->method->get_message(ssl, &msg)) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400956 // Handle the post-handshake message and try again.
David Benjamin7934f082017-08-01 16:32:25 -0400957 if (!ssl_do_post_handshake(ssl, msg)) {
958 return -1;
959 }
960 ssl->method->next_message(ssl);
David Benjaminc79845c2016-03-10 01:28:00 -0500961 }
962 }
David Benjamin15c14882016-03-14 14:25:46 -0400963}
964
965int SSL_read(SSL *ssl, void *buf, int num) {
966 return ssl_read_impl(ssl, buf, num, 0 /* consume bytes */);
David Benjamin27bbae42015-09-13 00:54:37 -0400967}
968
969int SSL_peek(SSL *ssl, void *buf, int num) {
David Benjamin15c14882016-03-14 14:25:46 -0400970 return ssl_read_impl(ssl, buf, num, 1 /* peek */);
David Benjamin27bbae42015-09-13 00:54:37 -0400971}
972
973int SSL_write(SSL *ssl, const void *buf, int num) {
David Benjamin2be4aa72017-01-02 07:56:51 -0500974 ssl_reset_error_state(ssl);
David Benjamin15c14882016-03-14 14:25:46 -0400975
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400976 if (ssl->do_handshake == NULL) {
David Benjamin27bbae42015-09-13 00:54:37 -0400977 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
978 return -1;
979 }
980
David Benjaminfa214e42016-05-10 17:03:10 -0400981 if (ssl->s3->send_shutdown != ssl_shutdown_none) {
David Benjamin27bbae42015-09-13 00:54:37 -0400982 OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
983 return -1;
984 }
985
David Benjamin74795b32017-08-31 15:13:12 -0400986 int ret = 0;
987 bool needs_handshake = false;
Steven Valdeze831a812017-03-09 14:56:07 -0500988 do {
David Benjaminc11ea9422017-08-29 16:33:21 -0400989 // If necessary, complete the handshake implicitly.
Steven Valdeze831a812017-03-09 14:56:07 -0500990 if (!ssl_can_write(ssl)) {
991 ret = SSL_do_handshake(ssl);
992 if (ret < 0) {
993 return ret;
994 }
995 if (ret == 0) {
996 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
997 return -1;
998 }
David Benjamind7ac1432016-03-10 00:41:25 -0500999 }
David Benjamind7ac1432016-03-10 00:41:25 -05001000
David Benjaminf5260812017-07-12 17:16:39 -04001001 ret = ssl->method->write_app_data(ssl, &needs_handshake,
1002 (const uint8_t *)buf, num);
Steven Valdeze831a812017-03-09 14:56:07 -05001003 } while (needs_handshake);
1004 return ret;
David Benjamin27bbae42015-09-13 00:54:37 -04001005}
1006
1007int SSL_shutdown(SSL *ssl) {
David Benjamin2be4aa72017-01-02 07:56:51 -05001008 ssl_reset_error_state(ssl);
David Benjamin15c14882016-03-14 14:25:46 -04001009
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001010 if (ssl->do_handshake == NULL) {
David Benjamin27bbae42015-09-13 00:54:37 -04001011 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
1012 return -1;
1013 }
1014
David Benjaminc11ea9422017-08-29 16:33:21 -04001015 // If we are in the middle of a handshake, silently succeed. Consumers often
1016 // call this function before |SSL_free|, whether the handshake succeeded or
1017 // not. We assume the caller has already handled failed handshakes.
Steven Valdeza14934f2016-02-29 10:05:08 -05001018 if (SSL_in_init(ssl)) {
David Benjamin12709db2017-02-17 13:51:50 -05001019 return 1;
Steven Valdeza14934f2016-02-29 10:05:08 -05001020 }
1021
David Benjamin27bbae42015-09-13 00:54:37 -04001022 if (ssl->quiet_shutdown) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001023 // Do nothing if configured not to send a close_notify.
David Benjaminfa214e42016-05-10 17:03:10 -04001024 ssl->s3->send_shutdown = ssl_shutdown_close_notify;
1025 ssl->s3->recv_shutdown = ssl_shutdown_close_notify;
David Benjamin27bbae42015-09-13 00:54:37 -04001026 return 1;
1027 }
1028
David Benjaminc11ea9422017-08-29 16:33:21 -04001029 // This function completes in two stages. It sends a close_notify and then it
1030 // waits for a close_notify to come in. Perform exactly one action and return
1031 // whether or not it succeeds.
David Benjamin27bbae42015-09-13 00:54:37 -04001032
David Benjaminfa214e42016-05-10 17:03:10 -04001033 if (ssl->s3->send_shutdown != ssl_shutdown_close_notify) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001034 // Send a close_notify.
David Benjaminfa214e42016-05-10 17:03:10 -04001035 if (ssl3_send_alert(ssl, SSL3_AL_WARNING, SSL_AD_CLOSE_NOTIFY) <= 0) {
1036 return -1;
David Benjamin27bbae42015-09-13 00:54:37 -04001037 }
1038 } else if (ssl->s3->alert_dispatch) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001039 // Finish sending the close_notify.
David Benjaminf0ee9072016-06-15 17:44:37 -04001040 if (ssl->method->dispatch_alert(ssl) <= 0) {
David Benjaminfa214e42016-05-10 17:03:10 -04001041 return -1;
David Benjamin27bbae42015-09-13 00:54:37 -04001042 }
David Benjaminfa214e42016-05-10 17:03:10 -04001043 } else if (ssl->s3->recv_shutdown != ssl_shutdown_close_notify) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001044 // Wait for the peer's close_notify.
David Benjaminf0ee9072016-06-15 17:44:37 -04001045 ssl->method->read_close_notify(ssl);
David Benjaminfa214e42016-05-10 17:03:10 -04001046 if (ssl->s3->recv_shutdown != ssl_shutdown_close_notify) {
1047 return -1;
David Benjamin27bbae42015-09-13 00:54:37 -04001048 }
1049 }
1050
David Benjaminc11ea9422017-08-29 16:33:21 -04001051 // Return 0 for unidirectional shutdown and 1 for bidirectional shutdown.
David Benjaminfa214e42016-05-10 17:03:10 -04001052 return ssl->s3->recv_shutdown == ssl_shutdown_close_notify;
David Benjamin27bbae42015-09-13 00:54:37 -04001053}
1054
David Benjamin1d4f4c02016-07-26 18:03:08 -04001055int SSL_send_fatal_alert(SSL *ssl, uint8_t alert) {
1056 if (ssl->s3->alert_dispatch) {
1057 if (ssl->s3->send_alert[0] != SSL3_AL_FATAL ||
1058 ssl->s3->send_alert[1] != alert) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001059 // We are already attempting to write a different alert.
David Benjamin1d4f4c02016-07-26 18:03:08 -04001060 OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
1061 return -1;
1062 }
1063 return ssl->method->dispatch_alert(ssl);
1064 }
1065
1066 return ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
1067}
1068
Steven Valdez08b65f42016-12-07 15:29:45 -05001069void SSL_CTX_set_early_data_enabled(SSL_CTX *ctx, int enabled) {
Alessandro Ghedini67bb45f2017-03-30 16:33:24 -05001070 ctx->cert->enable_early_data = !!enabled;
1071}
1072
Steven Valdez520e1222017-06-13 12:45:25 -04001073void SSL_CTX_set_tls13_variant(SSL_CTX *ctx, enum tls13_variant_t variant) {
1074 ctx->tls13_variant = variant;
1075}
1076
Steven Valdez52586f92017-07-11 15:08:32 -04001077void SSL_set_tls13_variant(SSL *ssl, enum tls13_variant_t variant) {
1078 ssl->tls13_variant = variant;
1079}
1080
Alessandro Ghedini67bb45f2017-03-30 16:33:24 -05001081void SSL_set_early_data_enabled(SSL *ssl, int enabled) {
1082 ssl->cert->enable_early_data = !!enabled;
Steven Valdez08b65f42016-12-07 15:29:45 -05001083}
1084
Steven Valdeze831a812017-03-09 14:56:07 -05001085int SSL_in_early_data(const SSL *ssl) {
1086 if (ssl->s3->hs == NULL) {
1087 return 0;
1088 }
1089 return ssl->s3->hs->in_early_data;
1090}
1091
Steven Valdez2d850622017-01-11 11:34:52 -05001092int SSL_early_data_accepted(const SSL *ssl) {
1093 return ssl->early_data_accepted;
1094}
1095
Steven Valdeze831a812017-03-09 14:56:07 -05001096void SSL_reset_early_data_reject(SSL *ssl) {
1097 SSL_HANDSHAKE *hs = ssl->s3->hs;
1098 if (hs == NULL ||
1099 hs->wait != ssl_hs_early_data_rejected) {
1100 abort();
1101 }
1102
1103 hs->wait = ssl_hs_ok;
David Benjaminfd45ee72017-08-31 14:49:09 -04001104 hs->in_early_data = false;
David Benjamin31b0c9b2017-07-20 14:49:15 -04001105 hs->early_session.reset();
Steven Valdeze831a812017-03-09 14:56:07 -05001106
David Benjaminc11ea9422017-08-29 16:33:21 -04001107 // Discard any unfinished writes from the perspective of |SSL_write|'s
1108 // retry. The handshake will transparently flush out the pending record
1109 // (discarded by the server) to keep the framing correct.
David Benjamin046bc1f2017-08-31 15:06:42 -04001110 ssl->s3->wpend_pending = false;
Steven Valdeze831a812017-03-09 14:56:07 -05001111}
1112
David Benjamin287fc4f2016-12-10 15:12:03 -05001113static int bio_retry_reason_to_error(int reason) {
1114 switch (reason) {
1115 case BIO_RR_CONNECT:
1116 return SSL_ERROR_WANT_CONNECT;
1117 case BIO_RR_ACCEPT:
1118 return SSL_ERROR_WANT_ACCEPT;
1119 default:
1120 return SSL_ERROR_SYSCALL;
1121 }
1122}
David Benjamin27bbae42015-09-13 00:54:37 -04001123
David Benjamin287fc4f2016-12-10 15:12:03 -05001124int SSL_get_error(const SSL *ssl, int ret_code) {
David Benjamin27bbae42015-09-13 00:54:37 -04001125 if (ret_code > 0) {
1126 return SSL_ERROR_NONE;
1127 }
1128
David Benjaminc11ea9422017-08-29 16:33:21 -04001129 // Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
1130 // where we do encode the error
David Benjamin287fc4f2016-12-10 15:12:03 -05001131 uint32_t err = ERR_peek_error();
David Benjamin27bbae42015-09-13 00:54:37 -04001132 if (err != 0) {
1133 if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
1134 return SSL_ERROR_SYSCALL;
1135 }
1136 return SSL_ERROR_SSL;
1137 }
1138
1139 if (ret_code == 0) {
David Benjaminfa214e42016-05-10 17:03:10 -04001140 if (ssl->s3->recv_shutdown == ssl_shutdown_close_notify) {
David Benjamin27bbae42015-09-13 00:54:37 -04001141 return SSL_ERROR_ZERO_RETURN;
1142 }
David Benjaminc11ea9422017-08-29 16:33:21 -04001143 // An EOF was observed which violates the protocol, and the underlying
1144 // transport does not participate in the error queue. Bubble up to the
1145 // caller.
David Benjamin27bbae42015-09-13 00:54:37 -04001146 return SSL_ERROR_SYSCALL;
1147 }
1148
David Benjamin287fc4f2016-12-10 15:12:03 -05001149 switch (ssl->rwstate) {
1150 case SSL_PENDING_SESSION:
1151 return SSL_ERROR_PENDING_SESSION;
David Benjamin27bbae42015-09-13 00:54:37 -04001152
David Benjamin287fc4f2016-12-10 15:12:03 -05001153 case SSL_CERTIFICATE_SELECTION_PENDING:
1154 return SSL_ERROR_PENDING_CERTIFICATE;
David Benjamin27bbae42015-09-13 00:54:37 -04001155
David Benjamin287fc4f2016-12-10 15:12:03 -05001156 case SSL_READING: {
1157 BIO *bio = SSL_get_rbio(ssl);
1158 if (BIO_should_read(bio)) {
1159 return SSL_ERROR_WANT_READ;
David Benjamin27bbae42015-09-13 00:54:37 -04001160 }
1161
David Benjamin287fc4f2016-12-10 15:12:03 -05001162 if (BIO_should_write(bio)) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001163 // TODO(davidben): OpenSSL historically checked for writes on the read
1164 // BIO. Can this be removed?
David Benjamin287fc4f2016-12-10 15:12:03 -05001165 return SSL_ERROR_WANT_WRITE;
David Benjamin27bbae42015-09-13 00:54:37 -04001166 }
1167
David Benjamin287fc4f2016-12-10 15:12:03 -05001168 if (BIO_should_io_special(bio)) {
1169 return bio_retry_reason_to_error(BIO_get_retry_reason(bio));
David Benjamin27bbae42015-09-13 00:54:37 -04001170 }
1171
David Benjamin287fc4f2016-12-10 15:12:03 -05001172 break;
1173 }
1174
1175 case SSL_WRITING: {
1176 BIO *bio = SSL_get_wbio(ssl);
1177 if (BIO_should_write(bio)) {
1178 return SSL_ERROR_WANT_WRITE;
David Benjamin27bbae42015-09-13 00:54:37 -04001179 }
1180
David Benjamin287fc4f2016-12-10 15:12:03 -05001181 if (BIO_should_read(bio)) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001182 // TODO(davidben): OpenSSL historically checked for reads on the write
1183 // BIO. Can this be removed?
David Benjamin287fc4f2016-12-10 15:12:03 -05001184 return SSL_ERROR_WANT_READ;
1185 }
1186
1187 if (BIO_should_io_special(bio)) {
1188 return bio_retry_reason_to_error(BIO_get_retry_reason(bio));
1189 }
1190
1191 break;
David Benjamin27bbae42015-09-13 00:54:37 -04001192 }
David Benjamin27bbae42015-09-13 00:54:37 -04001193
David Benjamin287fc4f2016-12-10 15:12:03 -05001194 case SSL_X509_LOOKUP:
1195 return SSL_ERROR_WANT_X509_LOOKUP;
David Benjamin27bbae42015-09-13 00:54:37 -04001196
David Benjamin287fc4f2016-12-10 15:12:03 -05001197 case SSL_CHANNEL_ID_LOOKUP:
1198 return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
David Benjamin27bbae42015-09-13 00:54:37 -04001199
David Benjamin287fc4f2016-12-10 15:12:03 -05001200 case SSL_PRIVATE_KEY_OPERATION:
1201 return SSL_ERROR_WANT_PRIVATE_KEY_OPERATION;
Adam Langley4c341d02017-03-08 19:33:21 -08001202
1203 case SSL_PENDING_TICKET:
1204 return SSL_ERROR_PENDING_TICKET;
Steven Valdeze831a812017-03-09 14:56:07 -05001205
1206 case SSL_EARLY_DATA_REJECTED:
1207 return SSL_ERROR_EARLY_DATA_REJECTED;
David Benjamin3a1dd462017-07-11 16:13:10 -04001208
1209 case SSL_CERTIFICATE_VERIFY:
1210 return SSL_ERROR_WANT_CERTIFICATE_VERIFY;
David Benjamin27bbae42015-09-13 00:54:37 -04001211 }
1212
1213 return SSL_ERROR_SYSCALL;
1214}
1215
David Benjamin27bbae42015-09-13 00:54:37 -04001216uint32_t SSL_CTX_set_options(SSL_CTX *ctx, uint32_t options) {
1217 ctx->options |= options;
1218 return ctx->options;
1219}
1220
1221uint32_t SSL_CTX_clear_options(SSL_CTX *ctx, uint32_t options) {
1222 ctx->options &= ~options;
1223 return ctx->options;
1224}
1225
1226uint32_t SSL_CTX_get_options(const SSL_CTX *ctx) { return ctx->options; }
1227
1228uint32_t SSL_set_options(SSL *ssl, uint32_t options) {
1229 ssl->options |= options;
1230 return ssl->options;
1231}
1232
1233uint32_t SSL_clear_options(SSL *ssl, uint32_t options) {
1234 ssl->options &= ~options;
1235 return ssl->options;
1236}
1237
1238uint32_t SSL_get_options(const SSL *ssl) { return ssl->options; }
1239
1240uint32_t SSL_CTX_set_mode(SSL_CTX *ctx, uint32_t mode) {
1241 ctx->mode |= mode;
1242 return ctx->mode;
1243}
1244
1245uint32_t SSL_CTX_clear_mode(SSL_CTX *ctx, uint32_t mode) {
1246 ctx->mode &= ~mode;
1247 return ctx->mode;
1248}
1249
1250uint32_t SSL_CTX_get_mode(const SSL_CTX *ctx) { return ctx->mode; }
1251
1252uint32_t SSL_set_mode(SSL *ssl, uint32_t mode) {
1253 ssl->mode |= mode;
1254 return ssl->mode;
1255}
1256
1257uint32_t SSL_clear_mode(SSL *ssl, uint32_t mode) {
1258 ssl->mode &= ~mode;
1259 return ssl->mode;
1260}
1261
1262uint32_t SSL_get_mode(const SSL *ssl) { return ssl->mode; }
1263
Adam Langleyd519bf62016-12-12 11:16:44 -08001264void SSL_CTX_set0_buffer_pool(SSL_CTX *ctx, CRYPTO_BUFFER_POOL *pool) {
1265 ctx->pool = pool;
1266}
1267
David Benjaminee0c8272015-09-13 01:03:54 -04001268int SSL_get_tls_unique(const SSL *ssl, uint8_t *out, size_t *out_len,
1269 size_t max_out) {
David Benjaminf5260812017-07-12 17:16:39 -04001270 *out_len = 0;
1271 OPENSSL_memset(out, 0, max_out);
1272
David Benjaminc11ea9422017-08-29 16:33:21 -04001273 // tls-unique is not defined for SSL 3.0 or TLS 1.3.
David Benjaminced00b42016-10-08 11:24:09 -04001274 if (!ssl->s3->initial_handshake_complete ||
1275 ssl3_protocol_version(ssl) < TLS1_VERSION ||
1276 ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjaminf5260812017-07-12 17:16:39 -04001277 return 0;
David Benjaminced00b42016-10-08 11:24:09 -04001278 }
1279
David Benjaminc11ea9422017-08-29 16:33:21 -04001280 // The tls-unique value is the first Finished message in the handshake, which
1281 // is the client's in a full handshake and the server's for a resumption. See
1282 // https://tools.ietf.org/html/rfc5929#section-3.1.
David Benjaminee0c8272015-09-13 01:03:54 -04001283 const uint8_t *finished = ssl->s3->previous_client_finished;
1284 size_t finished_len = ssl->s3->previous_client_finished_len;
Steven Valdez87eab492016-06-27 16:34:59 -04001285 if (ssl->session != NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001286 // tls-unique is broken for resumed sessions unless EMS is used.
David Benjaminee0c8272015-09-13 01:03:54 -04001287 if (!ssl->session->extended_master_secret) {
David Benjaminf5260812017-07-12 17:16:39 -04001288 return 0;
David Benjaminee0c8272015-09-13 01:03:54 -04001289 }
1290 finished = ssl->s3->previous_server_finished;
1291 finished_len = ssl->s3->previous_server_finished_len;
1292 }
1293
David Benjaminee0c8272015-09-13 01:03:54 -04001294 *out_len = finished_len;
1295 if (finished_len > max_out) {
1296 *out_len = max_out;
1297 }
1298
David Benjamin17cf2cb2016-12-13 01:07:13 -05001299 OPENSSL_memcpy(out, finished, *out_len);
David Benjaminee0c8272015-09-13 01:03:54 -04001300 return 1;
David Benjaminee0c8272015-09-13 01:03:54 -04001301}
1302
David Benjamin5960a902017-02-14 20:07:11 -05001303static int set_session_id_context(CERT *cert, const uint8_t *sid_ctx,
David Benjamin30c4c302016-12-07 22:35:24 -05001304 size_t sid_ctx_len) {
David Benjamin5960a902017-02-14 20:07:11 -05001305 if (sid_ctx_len > sizeof(cert->sid_ctx)) {
David Benjamin3570d732015-06-29 00:28:17 -04001306 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -08001307 return 0;
1308 }
David Benjamin30c4c302016-12-07 22:35:24 -05001309
David Benjamina3d76d02017-07-14 19:36:07 -04001310 static_assert(sizeof(cert->sid_ctx) < 256, "sid_ctx too large");
David Benjamin5960a902017-02-14 20:07:11 -05001311 cert->sid_ctx_length = (uint8_t)sid_ctx_len;
1312 OPENSSL_memcpy(cert->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001313 return 1;
1314}
Adam Langley95c29f32014-06-20 12:00:00 -07001315
David Benjamin5960a902017-02-14 20:07:11 -05001316int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
1317 size_t sid_ctx_len) {
1318 return set_session_id_context(ctx->cert, sid_ctx, sid_ctx_len);
1319}
1320
Adam Langleyfcf25832014-12-18 17:42:32 -08001321int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
David Benjamin30c4c302016-12-07 22:35:24 -05001322 size_t sid_ctx_len) {
David Benjamin5960a902017-02-14 20:07:11 -05001323 return set_session_id_context(ssl->cert, sid_ctx, sid_ctx_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001324}
Adam Langley95c29f32014-06-20 12:00:00 -07001325
David Benjamin07820b52016-12-07 21:13:41 -05001326const uint8_t *SSL_get0_session_id_context(const SSL *ssl, size_t *out_len) {
David Benjamin5960a902017-02-14 20:07:11 -05001327 *out_len = ssl->cert->sid_ctx_length;
1328 return ssl->cert->sid_ctx;
David Benjamin07820b52016-12-07 21:13:41 -05001329}
1330
David Benjamin7481d392015-07-05 19:38:46 -04001331void SSL_certs_clear(SSL *ssl) { ssl_cert_clear_certs(ssl->cert); }
Adam Langley95c29f32014-06-20 12:00:00 -07001332
David Benjamin066fe0a2015-10-17 21:11:33 -04001333int SSL_get_fd(const SSL *ssl) { return SSL_get_rfd(ssl); }
Adam Langley95c29f32014-06-20 12:00:00 -07001334
David Benjamin066fe0a2015-10-17 21:11:33 -04001335int SSL_get_rfd(const SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001336 int ret = -1;
David Benjamin066fe0a2015-10-17 21:11:33 -04001337 BIO *b = BIO_find_type(SSL_get_rbio(ssl), BIO_TYPE_DESCRIPTOR);
1338 if (b != NULL) {
1339 BIO_get_fd(b, &ret);
Adam Langleyfcf25832014-12-18 17:42:32 -08001340 }
1341 return ret;
1342}
Adam Langley95c29f32014-06-20 12:00:00 -07001343
David Benjamin066fe0a2015-10-17 21:11:33 -04001344int SSL_get_wfd(const SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001345 int ret = -1;
David Benjamin066fe0a2015-10-17 21:11:33 -04001346 BIO *b = BIO_find_type(SSL_get_wbio(ssl), BIO_TYPE_DESCRIPTOR);
1347 if (b != NULL) {
1348 BIO_get_fd(b, &ret);
Adam Langleyfcf25832014-12-18 17:42:32 -08001349 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001350 return ret;
1351}
Adam Langley95c29f32014-06-20 12:00:00 -07001352
David Benjamin066fe0a2015-10-17 21:11:33 -04001353int SSL_set_fd(SSL *ssl, int fd) {
David Benjamin22ce9b22016-03-06 19:26:52 -05001354 BIO *bio = BIO_new(BIO_s_socket());
Adam Langleyfcf25832014-12-18 17:42:32 -08001355 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001356 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
David Benjamin066fe0a2015-10-17 21:11:33 -04001357 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001358 }
1359 BIO_set_fd(bio, fd, BIO_NOCLOSE);
David Benjamin066fe0a2015-10-17 21:11:33 -04001360 SSL_set_bio(ssl, bio, bio);
1361 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001362}
Adam Langley95c29f32014-06-20 12:00:00 -07001363
David Benjamin066fe0a2015-10-17 21:11:33 -04001364int SSL_set_wfd(SSL *ssl, int fd) {
David Benjamin2f871122016-05-20 14:27:17 -04001365 BIO *rbio = SSL_get_rbio(ssl);
1366 if (rbio == NULL || BIO_method_type(rbio) != BIO_TYPE_SOCKET ||
1367 BIO_get_fd(rbio, NULL) != fd) {
David Benjamin22ce9b22016-03-06 19:26:52 -05001368 BIO *bio = BIO_new(BIO_s_socket());
Adam Langleyfcf25832014-12-18 17:42:32 -08001369 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001370 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
David Benjamin066fe0a2015-10-17 21:11:33 -04001371 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001372 }
1373 BIO_set_fd(bio, fd, BIO_NOCLOSE);
David Benjamin4501bd52016-08-01 13:39:41 -04001374 SSL_set0_wbio(ssl, bio);
Adam Langleyfcf25832014-12-18 17:42:32 -08001375 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -04001376 // Copy the rbio over to the wbio.
David Benjamin4501bd52016-08-01 13:39:41 -04001377 BIO_up_ref(rbio);
1378 SSL_set0_wbio(ssl, rbio);
Adam Langleyfcf25832014-12-18 17:42:32 -08001379 }
1380
David Benjamin066fe0a2015-10-17 21:11:33 -04001381 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001382}
Adam Langley95c29f32014-06-20 12:00:00 -07001383
David Benjamin066fe0a2015-10-17 21:11:33 -04001384int SSL_set_rfd(SSL *ssl, int fd) {
David Benjamin2f871122016-05-20 14:27:17 -04001385 BIO *wbio = SSL_get_wbio(ssl);
1386 if (wbio == NULL || BIO_method_type(wbio) != BIO_TYPE_SOCKET ||
1387 BIO_get_fd(wbio, NULL) != fd) {
David Benjamin22ce9b22016-03-06 19:26:52 -05001388 BIO *bio = BIO_new(BIO_s_socket());
Adam Langleyfcf25832014-12-18 17:42:32 -08001389 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001390 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
David Benjamin066fe0a2015-10-17 21:11:33 -04001391 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001392 }
1393 BIO_set_fd(bio, fd, BIO_NOCLOSE);
David Benjamin4501bd52016-08-01 13:39:41 -04001394 SSL_set0_rbio(ssl, bio);
Adam Langleyfcf25832014-12-18 17:42:32 -08001395 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -04001396 // Copy the wbio over to the rbio.
David Benjamin4501bd52016-08-01 13:39:41 -04001397 BIO_up_ref(wbio);
1398 SSL_set0_rbio(ssl, wbio);
Adam Langleyfcf25832014-12-18 17:42:32 -08001399 }
David Benjamin066fe0a2015-10-17 21:11:33 -04001400 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001401}
Adam Langley95c29f32014-06-20 12:00:00 -07001402
David Benjamin49ddf412016-10-08 11:56:01 -04001403static size_t copy_finished(void *out, size_t out_len, const uint8_t *in,
1404 size_t in_len) {
1405 if (out_len > in_len) {
1406 out_len = in_len;
1407 }
David Benjamin17cf2cb2016-12-13 01:07:13 -05001408 OPENSSL_memcpy(out, in, out_len);
David Benjamin49ddf412016-10-08 11:56:01 -04001409 return in_len;
1410}
1411
David Benjamin1a1b34d2015-10-17 12:51:52 -04001412size_t SSL_get_finished(const SSL *ssl, void *buf, size_t count) {
David Benjaminced00b42016-10-08 11:24:09 -04001413 if (!ssl->s3->initial_handshake_complete ||
1414 ssl3_protocol_version(ssl) < TLS1_VERSION ||
1415 ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1416 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001417 }
1418
David Benjamin49ddf412016-10-08 11:56:01 -04001419 if (ssl->server) {
1420 return copy_finished(buf, count, ssl->s3->previous_server_finished,
1421 ssl->s3->previous_server_finished_len);
David Benjaminced00b42016-10-08 11:24:09 -04001422 }
David Benjamin49ddf412016-10-08 11:56:01 -04001423
1424 return copy_finished(buf, count, ssl->s3->previous_client_finished,
1425 ssl->s3->previous_client_finished_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001426}
Adam Langley95c29f32014-06-20 12:00:00 -07001427
David Benjamin1a1b34d2015-10-17 12:51:52 -04001428size_t SSL_get_peer_finished(const SSL *ssl, void *buf, size_t count) {
David Benjaminced00b42016-10-08 11:24:09 -04001429 if (!ssl->s3->initial_handshake_complete ||
1430 ssl3_protocol_version(ssl) < TLS1_VERSION ||
1431 ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1432 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001433 }
Adam Langley95c29f32014-06-20 12:00:00 -07001434
David Benjamin49ddf412016-10-08 11:56:01 -04001435 if (ssl->server) {
1436 return copy_finished(buf, count, ssl->s3->previous_client_finished,
1437 ssl->s3->previous_client_finished_len);
David Benjaminced00b42016-10-08 11:24:09 -04001438 }
David Benjamin49ddf412016-10-08 11:56:01 -04001439
1440 return copy_finished(buf, count, ssl->s3->previous_server_finished,
1441 ssl->s3->previous_server_finished_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001442}
Adam Langley95c29f32014-06-20 12:00:00 -07001443
David Benjamin59937042015-09-19 13:04:22 -04001444int SSL_get_verify_mode(const SSL *ssl) { return ssl->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -07001445
David Benjamin42fea372015-09-19 01:22:44 -04001446int SSL_get_extms_support(const SSL *ssl) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001447 // TLS 1.3 does not require extended master secret and always reports as
1448 // supporting it.
David Benjamina68c1182016-07-24 00:14:33 -04001449 if (!ssl->s3->have_version) {
1450 return 0;
1451 }
David Benjaminfc02b592017-02-17 16:26:01 -05001452 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1453 return 1;
1454 }
1455
David Benjaminc11ea9422017-08-29 16:33:21 -04001456 // If the initial handshake completed, query the established session.
David Benjaminfc02b592017-02-17 16:26:01 -05001457 if (ssl->s3->established_session != NULL) {
1458 return ssl->s3->established_session->extended_master_secret;
1459 }
1460
David Benjaminc11ea9422017-08-29 16:33:21 -04001461 // Otherwise, query the in-progress handshake.
David Benjaminfc02b592017-02-17 16:26:01 -05001462 if (ssl->s3->hs != NULL) {
1463 return ssl->s3->hs->extended_master_secret;
1464 }
1465 assert(0);
1466 return 0;
Matt Braithwaitecd6f54b2015-09-17 12:54:42 -07001467}
1468
David Benjamin9a41d1b2015-05-16 01:30:09 -04001469int SSL_CTX_get_read_ahead(const SSL_CTX *ctx) { return 0; }
Adam Langley95c29f32014-06-20 12:00:00 -07001470
David Benjamin0d56f882015-12-19 17:05:56 -05001471int SSL_get_read_ahead(const SSL *ssl) { return 0; }
Adam Langley95c29f32014-06-20 12:00:00 -07001472
David Benjamin9a41d1b2015-05-16 01:30:09 -04001473void SSL_CTX_set_read_ahead(SSL_CTX *ctx, int yes) { }
David Benjamin61ecccf2015-05-05 09:44:51 -04001474
David Benjamin0d56f882015-12-19 17:05:56 -05001475void SSL_set_read_ahead(SSL *ssl, int yes) { }
David Benjamin61ecccf2015-05-05 09:44:51 -04001476
David Benjamin9f859492015-10-03 10:44:30 -04001477int SSL_pending(const SSL *ssl) {
1478 if (ssl->s3->rrec.type != SSL3_RT_APPLICATION_DATA) {
1479 return 0;
1480 }
1481 return ssl->s3->rrec.length;
Adam Langleyfcf25832014-12-18 17:42:32 -08001482}
Adam Langley95c29f32014-06-20 12:00:00 -07001483
David Benjaminc11ea9422017-08-29 16:33:21 -04001484// Fix this so it checks all the valid key/cert options
Adam Langleyfcf25832014-12-18 17:42:32 -08001485int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
Adam Langley3a2b47a2017-01-24 13:59:42 -08001486 return ssl_cert_check_private_key(ctx->cert, ctx->cert->privatekey);
Adam Langleyc5ac2b62016-11-07 12:02:35 -08001487}
1488
David Benjaminc11ea9422017-08-29 16:33:21 -04001489// Fix this function so that it takes an optional type parameter
Adam Langleyc5ac2b62016-11-07 12:02:35 -08001490int SSL_check_private_key(const SSL *ssl) {
Adam Langley3a2b47a2017-01-24 13:59:42 -08001491 return ssl_cert_check_private_key(ssl->cert, ssl->cert->privatekey);
Adam Langleyfcf25832014-12-18 17:42:32 -08001492}
Adam Langley95c29f32014-06-20 12:00:00 -07001493
David Benjamin42fea372015-09-19 01:22:44 -04001494long SSL_get_default_timeout(const SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001495 return SSL_DEFAULT_SESSION_TIMEOUT;
1496}
Adam Langley95c29f32014-06-20 12:00:00 -07001497
David Benjamin44d3eed2015-05-21 01:29:55 -04001498int SSL_renegotiate(SSL *ssl) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001499 // Caller-initiated renegotiation is not supported.
David Benjamin3570d732015-06-29 00:28:17 -04001500 OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
David Benjamin44d3eed2015-05-21 01:29:55 -04001501 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001502}
Adam Langley95c29f32014-06-20 12:00:00 -07001503
David Benjamin44d3eed2015-05-21 01:29:55 -04001504int SSL_renegotiate_pending(SSL *ssl) {
1505 return SSL_in_init(ssl) && ssl->s3->initial_handshake_complete;
Adam Langleyfcf25832014-12-18 17:42:32 -08001506}
Adam Langley95c29f32014-06-20 12:00:00 -07001507
David Benjamin936aada2016-06-07 19:09:22 -04001508int SSL_total_renegotiations(const SSL *ssl) {
1509 return ssl->s3->total_renegotiations;
1510}
1511
David Benjamin1d0a1942015-04-26 15:35:35 -04001512size_t SSL_CTX_get_max_cert_list(const SSL_CTX *ctx) {
1513 return ctx->max_cert_list;
1514}
1515
1516void SSL_CTX_set_max_cert_list(SSL_CTX *ctx, size_t max_cert_list) {
1517 if (max_cert_list > kMaxHandshakeSize) {
1518 max_cert_list = kMaxHandshakeSize;
1519 }
1520 ctx->max_cert_list = (uint32_t)max_cert_list;
1521}
1522
1523size_t SSL_get_max_cert_list(const SSL *ssl) {
1524 return ssl->max_cert_list;
1525}
1526
1527void SSL_set_max_cert_list(SSL *ssl, size_t max_cert_list) {
1528 if (max_cert_list > kMaxHandshakeSize) {
1529 max_cert_list = kMaxHandshakeSize;
1530 }
1531 ssl->max_cert_list = (uint32_t)max_cert_list;
1532}
1533
Adam Langleyce9d85e2016-01-24 15:58:39 -08001534int SSL_CTX_set_max_send_fragment(SSL_CTX *ctx, size_t max_send_fragment) {
David Benjamin1d0a1942015-04-26 15:35:35 -04001535 if (max_send_fragment < 512) {
1536 max_send_fragment = 512;
1537 }
1538 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1539 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1540 }
1541 ctx->max_send_fragment = (uint16_t)max_send_fragment;
Adam Langleyce9d85e2016-01-24 15:58:39 -08001542
1543 return 1;
David Benjamin1d0a1942015-04-26 15:35:35 -04001544}
1545
Adam Langleyce9d85e2016-01-24 15:58:39 -08001546int SSL_set_max_send_fragment(SSL *ssl, size_t max_send_fragment) {
David Benjamin1d0a1942015-04-26 15:35:35 -04001547 if (max_send_fragment < 512) {
1548 max_send_fragment = 512;
1549 }
1550 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1551 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1552 }
1553 ssl->max_send_fragment = (uint16_t)max_send_fragment;
Adam Langleyce9d85e2016-01-24 15:58:39 -08001554
1555 return 1;
David Benjamin1d0a1942015-04-26 15:35:35 -04001556}
1557
David Benjamincb9cf792015-05-05 09:46:14 -04001558int SSL_set_mtu(SSL *ssl, unsigned mtu) {
David Benjamince079fd2016-08-02 16:22:34 -04001559 if (!SSL_is_dtls(ssl) || mtu < dtls1_min_mtu()) {
David Benjamincb9cf792015-05-05 09:46:14 -04001560 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001561 }
David Benjamincb9cf792015-05-05 09:46:14 -04001562 ssl->d1->mtu = mtu;
1563 return 1;
1564}
1565
1566int SSL_get_secure_renegotiation_support(const SSL *ssl) {
David Benjamind2610042017-01-03 10:49:28 -05001567 if (!ssl->s3->have_version) {
1568 return 0;
1569 }
1570 return ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
1571 ssl->s3->send_connection_binding;
David Benjamincb9cf792015-05-05 09:46:14 -04001572}
1573
David Benjamin71f7d3d2015-05-05 09:46:38 -04001574size_t SSL_CTX_sess_number(const SSL_CTX *ctx) {
David Benjamin9eaa3bd2017-09-27 17:03:54 -04001575 MutexReadLock lock(const_cast<CRYPTO_MUTEX *>(&ctx->lock));
David Benjamin71f7d3d2015-05-05 09:46:38 -04001576 return lh_SSL_SESSION_num_items(ctx->sessions);
1577}
1578
1579unsigned long SSL_CTX_sess_set_cache_size(SSL_CTX *ctx, unsigned long size) {
1580 unsigned long ret = ctx->session_cache_size;
1581 ctx->session_cache_size = size;
1582 return ret;
1583}
1584
1585unsigned long SSL_CTX_sess_get_cache_size(const SSL_CTX *ctx) {
1586 return ctx->session_cache_size;
1587}
1588
1589int SSL_CTX_set_session_cache_mode(SSL_CTX *ctx, int mode) {
1590 int ret = ctx->session_cache_mode;
1591 ctx->session_cache_mode = mode;
1592 return ret;
1593}
1594
1595int SSL_CTX_get_session_cache_mode(const SSL_CTX *ctx) {
1596 return ctx->session_cache_mode;
1597}
1598
David Benjamin936aada2016-06-07 19:09:22 -04001599
1600int SSL_CTX_get_tlsext_ticket_keys(SSL_CTX *ctx, void *out, size_t len) {
1601 if (out == NULL) {
1602 return 48;
1603 }
1604 if (len != 48) {
1605 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH);
1606 return 0;
1607 }
Martin Kreichgauer72912d22017-08-04 12:06:43 -07001608
David Benjaminc11ea9422017-08-29 16:33:21 -04001609 // The default ticket keys are initialized lazily. Trigger a key
1610 // rotation to initialize them.
Martin Kreichgauer72912d22017-08-04 12:06:43 -07001611 if (!ssl_ctx_rotate_ticket_encryption_key(ctx)) {
1612 return 0;
1613 }
1614
David Benjaminf5260812017-07-12 17:16:39 -04001615 uint8_t *out_bytes = reinterpret_cast<uint8_t *>(out);
Martin Kreichgauer72912d22017-08-04 12:06:43 -07001616 MutexReadLock lock(&ctx->lock);
1617 OPENSSL_memcpy(out_bytes, ctx->tlsext_ticket_key_current->name, 16);
1618 OPENSSL_memcpy(out_bytes + 16, ctx->tlsext_ticket_key_current->hmac_key, 16);
1619 OPENSSL_memcpy(out_bytes + 32, ctx->tlsext_ticket_key_current->aes_key, 16);
David Benjamin936aada2016-06-07 19:09:22 -04001620 return 1;
1621}
1622
1623int SSL_CTX_set_tlsext_ticket_keys(SSL_CTX *ctx, const void *in, size_t len) {
1624 if (in == NULL) {
1625 return 48;
1626 }
1627 if (len != 48) {
1628 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH);
1629 return 0;
1630 }
Martin Kreichgauer72912d22017-08-04 12:06:43 -07001631 if (!ctx->tlsext_ticket_key_current) {
1632 ctx->tlsext_ticket_key_current =
1633 (tlsext_ticket_key *)OPENSSL_malloc(sizeof(tlsext_ticket_key));
1634 if (!ctx->tlsext_ticket_key_current) {
1635 return 0;
1636 }
1637 }
1638 OPENSSL_memset(ctx->tlsext_ticket_key_current, 0, sizeof(tlsext_ticket_key));
David Benjaminf5260812017-07-12 17:16:39 -04001639 const uint8_t *in_bytes = reinterpret_cast<const uint8_t *>(in);
Martin Kreichgauer72912d22017-08-04 12:06:43 -07001640 OPENSSL_memcpy(ctx->tlsext_ticket_key_current->name, in_bytes, 16);
1641 OPENSSL_memcpy(ctx->tlsext_ticket_key_current->hmac_key, in_bytes + 16, 16);
1642 OPENSSL_memcpy(ctx->tlsext_ticket_key_current->aes_key, in_bytes + 32, 16);
1643 OPENSSL_free(ctx->tlsext_ticket_key_prev);
1644 ctx->tlsext_ticket_key_prev = nullptr;
David Benjaminc11ea9422017-08-29 16:33:21 -04001645 // Disable automatic key rotation.
Martin Kreichgauer72912d22017-08-04 12:06:43 -07001646 ctx->tlsext_ticket_key_current->next_rotation_tv_sec = 0;
David Benjamin936aada2016-06-07 19:09:22 -04001647 return 1;
1648}
1649
1650int SSL_CTX_set_tlsext_ticket_key_cb(
1651 SSL_CTX *ctx, int (*callback)(SSL *ssl, uint8_t *key_name, uint8_t *iv,
1652 EVP_CIPHER_CTX *ctx, HMAC_CTX *hmac_ctx,
1653 int encrypt)) {
1654 ctx->tlsext_ticket_key_cb = callback;
1655 return 1;
1656}
1657
1658int SSL_CTX_set1_curves(SSL_CTX *ctx, const int *curves, size_t curves_len) {
1659 return tls1_set_curves(&ctx->supported_group_list,
1660 &ctx->supported_group_list_len, curves,
1661 curves_len);
1662}
1663
1664int SSL_set1_curves(SSL *ssl, const int *curves, size_t curves_len) {
1665 return tls1_set_curves(&ssl->supported_group_list,
1666 &ssl->supported_group_list_len, curves,
1667 curves_len);
1668}
1669
Alessandro Ghedini5fd18072016-09-28 21:04:25 +01001670int SSL_CTX_set1_curves_list(SSL_CTX *ctx, const char *curves) {
1671 return tls1_set_curves_list(&ctx->supported_group_list,
1672 &ctx->supported_group_list_len, curves);
1673}
1674
1675int SSL_set1_curves_list(SSL *ssl, const char *curves) {
1676 return tls1_set_curves_list(&ssl->supported_group_list,
1677 &ssl->supported_group_list_len, curves);
1678}
1679
David Benjamin9e68f192016-06-30 14:55:33 -04001680uint16_t SSL_get_curve_id(const SSL *ssl) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001681 // TODO(davidben): This checks the wrong session if there is a renegotiation
1682 // in progress.
Steven Valdez87eab492016-06-27 16:34:59 -04001683 SSL_SESSION *session = SSL_get_session(ssl);
David Benjamin4882a6c2016-12-11 02:48:12 -05001684 if (session == NULL) {
David Benjamin9e68f192016-06-30 14:55:33 -04001685 return 0;
1686 }
1687
David Benjamin4882a6c2016-12-11 02:48:12 -05001688 return session->group_id;
David Benjamin9e68f192016-06-30 14:55:33 -04001689}
1690
David Benjamin936aada2016-06-07 19:09:22 -04001691int SSL_CTX_set_tmp_dh(SSL_CTX *ctx, const DH *dh) {
David Benjamin936aada2016-06-07 19:09:22 -04001692 return 1;
1693}
1694
1695int SSL_set_tmp_dh(SSL *ssl, const DH *dh) {
David Benjamin936aada2016-06-07 19:09:22 -04001696 return 1;
1697}
1698
David Benjamin70dbf042017-08-08 18:51:37 -04001699STACK_OF(SSL_CIPHER) *SSL_CTX_get_ciphers(const SSL_CTX *ctx) {
David Benjamin8ebeabf2017-03-02 22:04:07 -05001700 return ctx->cipher_list->ciphers;
1701}
1702
David Benjamin70dbf042017-08-08 18:51:37 -04001703int SSL_CTX_cipher_in_group(const SSL_CTX *ctx, size_t i) {
1704 if (i >= sk_SSL_CIPHER_num(ctx->cipher_list->ciphers)) {
1705 return 0;
1706 }
1707 return ctx->cipher_list->in_group_flags[i];
1708}
1709
David Benjamin32876b32015-09-20 12:17:03 -04001710STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *ssl) {
1711 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001712 return NULL;
1713 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001714
David Benjaminaf3b3d32016-10-31 16:29:57 -04001715 const struct ssl_cipher_preference_list_st *prefs =
1716 ssl_get_cipher_preferences(ssl);
1717 if (prefs == NULL) {
1718 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08001719 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001720
David Benjaminaf3b3d32016-10-31 16:29:57 -04001721 return prefs->ciphers;
Adam Langleyfcf25832014-12-18 17:42:32 -08001722}
Adam Langley95c29f32014-06-20 12:00:00 -07001723
David Benjamin32876b32015-09-20 12:17:03 -04001724const char *SSL_get_cipher_list(const SSL *ssl, int n) {
David Benjamin32876b32015-09-20 12:17:03 -04001725 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001726 return NULL;
1727 }
Adam Langley95c29f32014-06-20 12:00:00 -07001728
David Benjamin8ebeabf2017-03-02 22:04:07 -05001729 STACK_OF(SSL_CIPHER) *sk = SSL_get_ciphers(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001730 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
1731 return NULL;
1732 }
Adam Langley95c29f32014-06-20 12:00:00 -07001733
David Benjamin8ebeabf2017-03-02 22:04:07 -05001734 const SSL_CIPHER *c = sk_SSL_CIPHER_value(sk, n);
Adam Langleyfcf25832014-12-18 17:42:32 -08001735 if (c == NULL) {
1736 return NULL;
1737 }
Adam Langley95c29f32014-06-20 12:00:00 -07001738
Adam Langleyfcf25832014-12-18 17:42:32 -08001739 return c->name;
1740}
David Benjamin5491e3f2014-09-29 19:33:09 -04001741
Adam Langleyfcf25832014-12-18 17:42:32 -08001742int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
Matthew Braithwaite6ad20dc2017-02-21 16:41:33 -08001743 return ssl_create_cipher_list(ctx->method, &ctx->cipher_list, str,
David Benjaminf4962492017-09-27 19:34:32 -04001744 false /* not strict */);
Matthew Braithwaitea57dcfb2017-02-17 22:08:23 -08001745}
1746
1747int SSL_CTX_set_strict_cipher_list(SSL_CTX *ctx, const char *str) {
Matthew Braithwaite6ad20dc2017-02-21 16:41:33 -08001748 return ssl_create_cipher_list(ctx->method, &ctx->cipher_list, str,
David Benjaminf4962492017-09-27 19:34:32 -04001749 true /* strict */);
Adam Langleyfcf25832014-12-18 17:42:32 -08001750}
David Benjamin39482a12014-07-20 13:30:15 -04001751
David Benjamin32876b32015-09-20 12:17:03 -04001752int SSL_set_cipher_list(SSL *ssl, const char *str) {
Matthew Braithwaite6ad20dc2017-02-21 16:41:33 -08001753 return ssl_create_cipher_list(ssl->ctx->method, &ssl->cipher_list, str,
David Benjaminf4962492017-09-27 19:34:32 -04001754 false /* not strict */);
Matthew Braithwaitea57dcfb2017-02-17 22:08:23 -08001755}
1756
1757int SSL_set_strict_cipher_list(SSL *ssl, const char *str) {
Matthew Braithwaite6ad20dc2017-02-21 16:41:33 -08001758 return ssl_create_cipher_list(ssl->ctx->method, &ssl->cipher_list, str,
David Benjaminf4962492017-09-27 19:34:32 -04001759 true /* strict */);
Adam Langleyfcf25832014-12-18 17:42:32 -08001760}
Adam Langley95c29f32014-06-20 12:00:00 -07001761
David Benjamin07e13842015-10-17 13:48:04 -04001762const char *SSL_get_servername(const SSL *ssl, const int type) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001763 if (type != TLSEXT_NAMETYPE_host_name) {
1764 return NULL;
1765 }
Adam Langley95c29f32014-06-20 12:00:00 -07001766
David Benjaminc11ea9422017-08-29 16:33:21 -04001767 // Historically, |SSL_get_servername| was also the configuration getter
1768 // corresponding to |SSL_set_tlsext_host_name|.
David Benjamin07e13842015-10-17 13:48:04 -04001769 if (ssl->tlsext_hostname != NULL) {
1770 return ssl->tlsext_hostname;
1771 }
1772
David Benjamina8614602017-09-06 15:40:19 -04001773 return ssl->s3->hostname;
Adam Langleyfcf25832014-12-18 17:42:32 -08001774}
Adam Langley95c29f32014-06-20 12:00:00 -07001775
David Benjamin07e13842015-10-17 13:48:04 -04001776int SSL_get_servername_type(const SSL *ssl) {
David Benjamina8614602017-09-06 15:40:19 -04001777 if (SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) == NULL) {
Steven Valdez87eab492016-06-27 16:34:59 -04001778 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001779 }
Steven Valdez87eab492016-06-27 16:34:59 -04001780 return TLSEXT_NAMETYPE_host_name;
Adam Langleyfcf25832014-12-18 17:42:32 -08001781}
Adam Langley95c29f32014-06-20 12:00:00 -07001782
David Benjamin3a1dd462017-07-11 16:13:10 -04001783void SSL_CTX_set_custom_verify(
1784 SSL_CTX *ctx, int mode,
1785 enum ssl_verify_result_t (*callback)(SSL *ssl, uint8_t *out_alert)) {
1786 ctx->verify_mode = mode;
1787 ctx->custom_verify_callback = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08001788}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001789
David Benjamin3a1dd462017-07-11 16:13:10 -04001790void SSL_set_custom_verify(
1791 SSL *ssl, int mode,
1792 enum ssl_verify_result_t (*callback)(SSL *ssl, uint8_t *out_alert)) {
1793 ssl->verify_mode = mode;
1794 ssl->custom_verify_callback = callback;
1795}
1796
1797void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
1798 ctx->signed_cert_timestamps_enabled = 1;
Adam Langleyfe366722017-03-07 16:41:04 -08001799}
1800
David Benjamin26e1ff32017-02-14 20:13:00 -05001801void SSL_enable_signed_cert_timestamps(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001802 ssl->signed_cert_timestamps_enabled = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001803}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001804
Adam Langleyfcf25832014-12-18 17:42:32 -08001805void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
1806 ctx->ocsp_stapling_enabled = 1;
1807}
David Benjamin6c7aed02014-08-27 16:42:38 -04001808
David Benjamin26e1ff32017-02-14 20:13:00 -05001809void SSL_enable_ocsp_stapling(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001810 ssl->ocsp_stapling_enabled = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001811}
David Benjamin6c7aed02014-08-27 16:42:38 -04001812
Adam Langleyfcf25832014-12-18 17:42:32 -08001813void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
1814 size_t *out_len) {
Steven Valdez87eab492016-06-27 16:34:59 -04001815 SSL_SESSION *session = SSL_get_session(ssl);
David Benjamin8fc2dc02017-08-22 15:07:51 -07001816 if (ssl->server || !session || !session->signed_cert_timestamp_list) {
1817 *out_len = 0;
1818 *out = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08001819 return;
1820 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001821
David Benjamin8fc2dc02017-08-22 15:07:51 -07001822 *out = CRYPTO_BUFFER_data(session->signed_cert_timestamp_list);
1823 *out_len = CRYPTO_BUFFER_len(session->signed_cert_timestamp_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001824}
David Benjamin6c7aed02014-08-27 16:42:38 -04001825
Adam Langleyfcf25832014-12-18 17:42:32 -08001826void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
1827 size_t *out_len) {
Steven Valdez87eab492016-06-27 16:34:59 -04001828 SSL_SESSION *session = SSL_get_session(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001829 if (ssl->server || !session || !session->ocsp_response) {
David Benjamin8fc2dc02017-08-22 15:07:51 -07001830 *out_len = 0;
1831 *out = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08001832 return;
1833 }
David Benjamin8fc2dc02017-08-22 15:07:51 -07001834
1835 *out = CRYPTO_BUFFER_data(session->ocsp_response);
1836 *out_len = CRYPTO_BUFFER_len(session->ocsp_response);
Adam Langleyfcf25832014-12-18 17:42:32 -08001837}
David Benjamin6c7aed02014-08-27 16:42:38 -04001838
David Benjamin936aada2016-06-07 19:09:22 -04001839int SSL_set_tlsext_host_name(SSL *ssl, const char *name) {
1840 OPENSSL_free(ssl->tlsext_hostname);
1841 ssl->tlsext_hostname = NULL;
1842
1843 if (name == NULL) {
1844 return 1;
1845 }
1846
1847 size_t len = strlen(name);
1848 if (len == 0 || len > TLSEXT_MAXLEN_host_name) {
1849 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL3_EXT_INVALID_SERVERNAME);
1850 return 0;
1851 }
1852 ssl->tlsext_hostname = BUF_strdup(name);
1853 if (ssl->tlsext_hostname == NULL) {
1854 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1855 return 0;
1856 }
1857 return 1;
1858}
1859
1860int SSL_CTX_set_tlsext_servername_callback(
1861 SSL_CTX *ctx, int (*callback)(SSL *ssl, int *out_alert, void *arg)) {
1862 ctx->tlsext_servername_callback = callback;
1863 return 1;
1864}
1865
1866int SSL_CTX_set_tlsext_servername_arg(SSL_CTX *ctx, void *arg) {
1867 ctx->tlsext_servername_arg = arg;
1868 return 1;
1869}
1870
David Benjaminca743582017-06-15 17:51:35 -04001871int SSL_select_next_proto(uint8_t **out, uint8_t *out_len, const uint8_t *peer,
1872 unsigned peer_len, const uint8_t *supported,
1873 unsigned supported_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001874 const uint8_t *result;
David Benjaminca743582017-06-15 17:51:35 -04001875 int status;
Adam Langley95c29f32014-06-20 12:00:00 -07001876
David Benjaminc11ea9422017-08-29 16:33:21 -04001877 // For each protocol in peer preference order, see if we support it.
David Benjaminca743582017-06-15 17:51:35 -04001878 for (unsigned i = 0; i < peer_len;) {
1879 for (unsigned j = 0; j < supported_len;) {
1880 if (peer[i] == supported[j] &&
1881 OPENSSL_memcmp(&peer[i + 1], &supported[j + 1], peer[i]) == 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001882 // We found a match
David Benjaminca743582017-06-15 17:51:35 -04001883 result = &peer[i];
Adam Langleyfcf25832014-12-18 17:42:32 -08001884 status = OPENSSL_NPN_NEGOTIATED;
1885 goto found;
1886 }
David Benjaminca743582017-06-15 17:51:35 -04001887 j += supported[j];
Adam Langleyfcf25832014-12-18 17:42:32 -08001888 j++;
1889 }
David Benjaminca743582017-06-15 17:51:35 -04001890 i += peer[i];
Adam Langleyfcf25832014-12-18 17:42:32 -08001891 i++;
1892 }
Adam Langley95c29f32014-06-20 12:00:00 -07001893
David Benjaminc11ea9422017-08-29 16:33:21 -04001894 // There's no overlap between our protocols and the peer's list.
David Benjaminca743582017-06-15 17:51:35 -04001895 result = supported;
Adam Langleyfcf25832014-12-18 17:42:32 -08001896 status = OPENSSL_NPN_NO_OVERLAP;
Adam Langley95c29f32014-06-20 12:00:00 -07001897
Adam Langleyfcf25832014-12-18 17:42:32 -08001898found:
1899 *out = (uint8_t *)result + 1;
David Benjamin977547b2015-09-16 00:25:52 -04001900 *out_len = result[0];
Adam Langleyfcf25832014-12-18 17:42:32 -08001901 return status;
1902}
Adam Langley95c29f32014-06-20 12:00:00 -07001903
David Benjamin977547b2015-09-16 00:25:52 -04001904void SSL_get0_next_proto_negotiated(const SSL *ssl, const uint8_t **out_data,
1905 unsigned *out_len) {
David Benjamin79978df2015-12-25 15:56:49 -05001906 *out_data = ssl->s3->next_proto_negotiated;
David Benjamin05d4c972017-06-21 16:35:10 -04001907 *out_len = ssl->s3->next_proto_negotiated_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001908}
1909
Adam Langleyfcf25832014-12-18 17:42:32 -08001910void SSL_CTX_set_next_protos_advertised_cb(
1911 SSL_CTX *ctx,
David Benjamin977547b2015-09-16 00:25:52 -04001912 int (*cb)(SSL *ssl, const uint8_t **out, unsigned *out_len, void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08001913 void *arg) {
1914 ctx->next_protos_advertised_cb = cb;
1915 ctx->next_protos_advertised_cb_arg = arg;
1916}
Adam Langley95c29f32014-06-20 12:00:00 -07001917
Adam Langleyfcf25832014-12-18 17:42:32 -08001918void SSL_CTX_set_next_proto_select_cb(
David Benjamin977547b2015-09-16 00:25:52 -04001919 SSL_CTX *ctx, int (*cb)(SSL *ssl, uint8_t **out, uint8_t *out_len,
1920 const uint8_t *in, unsigned in_len, void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08001921 void *arg) {
1922 ctx->next_proto_select_cb = cb;
1923 ctx->next_proto_select_cb_arg = arg;
1924}
Adam Langley95c29f32014-06-20 12:00:00 -07001925
Adam Langleyfcf25832014-12-18 17:42:32 -08001926int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
1927 unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001928 OPENSSL_free(ctx->alpn_client_proto_list);
David Benjaminf5260812017-07-12 17:16:39 -04001929 ctx->alpn_client_proto_list = (uint8_t *)BUF_memdup(protos, protos_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001930 if (!ctx->alpn_client_proto_list) {
1931 return 1;
1932 }
1933 ctx->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001934
Adam Langleyfcf25832014-12-18 17:42:32 -08001935 return 0;
1936}
Adam Langley95c29f32014-06-20 12:00:00 -07001937
Adam Langleyfcf25832014-12-18 17:42:32 -08001938int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001939 OPENSSL_free(ssl->alpn_client_proto_list);
David Benjaminf5260812017-07-12 17:16:39 -04001940 ssl->alpn_client_proto_list = (uint8_t *)BUF_memdup(protos, protos_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001941 if (!ssl->alpn_client_proto_list) {
1942 return 1;
1943 }
1944 ssl->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001945
Adam Langleyfcf25832014-12-18 17:42:32 -08001946 return 0;
1947}
Adam Langley95c29f32014-06-20 12:00:00 -07001948
Adam Langleyfcf25832014-12-18 17:42:32 -08001949void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
1950 int (*cb)(SSL *ssl, const uint8_t **out,
David Benjamin8984f1f2015-09-16 00:10:19 -04001951 uint8_t *out_len, const uint8_t *in,
1952 unsigned in_len, void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08001953 void *arg) {
1954 ctx->alpn_select_cb = cb;
1955 ctx->alpn_select_cb_arg = arg;
1956}
Adam Langley95c29f32014-06-20 12:00:00 -07001957
David Benjamin8984f1f2015-09-16 00:10:19 -04001958void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **out_data,
1959 unsigned *out_len) {
Steven Valdeze831a812017-03-09 14:56:07 -05001960 if (SSL_in_early_data(ssl) && !ssl->server) {
1961 *out_data = ssl->s3->hs->early_session->early_alpn;
1962 *out_len = ssl->s3->hs->early_session->early_alpn_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08001963 } else {
Steven Valdeze831a812017-03-09 14:56:07 -05001964 *out_data = ssl->s3->alpn_selected;
David Benjamin8984f1f2015-09-16 00:10:19 -04001965 *out_len = ssl->s3->alpn_selected_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08001966 }
1967}
Adam Langley95c29f32014-06-20 12:00:00 -07001968
David Benjaminc8ff30c2017-04-04 13:52:36 -04001969void SSL_CTX_set_allow_unknown_alpn_protos(SSL_CTX *ctx, int enabled) {
1970 ctx->allow_unknown_alpn_protos = !!enabled;
1971}
David Benjamin936aada2016-06-07 19:09:22 -04001972
David Benjamineebd3c82016-12-06 17:43:58 -05001973void SSL_CTX_set_tls_channel_id_enabled(SSL_CTX *ctx, int enabled) {
1974 ctx->tlsext_channel_id_enabled = !!enabled;
1975}
1976
David Benjamin936aada2016-06-07 19:09:22 -04001977int SSL_CTX_enable_tls_channel_id(SSL_CTX *ctx) {
David Benjamineebd3c82016-12-06 17:43:58 -05001978 SSL_CTX_set_tls_channel_id_enabled(ctx, 1);
David Benjamin936aada2016-06-07 19:09:22 -04001979 return 1;
1980}
1981
David Benjamineebd3c82016-12-06 17:43:58 -05001982void SSL_set_tls_channel_id_enabled(SSL *ssl, int enabled) {
1983 ssl->tlsext_channel_id_enabled = !!enabled;
1984}
1985
David Benjamin936aada2016-06-07 19:09:22 -04001986int SSL_enable_tls_channel_id(SSL *ssl) {
David Benjamineebd3c82016-12-06 17:43:58 -05001987 SSL_set_tls_channel_id_enabled(ssl, 1);
David Benjamin936aada2016-06-07 19:09:22 -04001988 return 1;
1989}
1990
1991static int is_p256_key(EVP_PKEY *private_key) {
1992 const EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(private_key);
1993 return ec_key != NULL &&
1994 EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key)) ==
1995 NID_X9_62_prime256v1;
1996}
1997
1998int SSL_CTX_set1_tls_channel_id(SSL_CTX *ctx, EVP_PKEY *private_key) {
1999 if (!is_p256_key(private_key)) {
2000 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256);
2001 return 0;
2002 }
2003
2004 EVP_PKEY_free(ctx->tlsext_channel_id_private);
Adam Langley310d3f62016-07-12 10:39:20 -07002005 EVP_PKEY_up_ref(private_key);
2006 ctx->tlsext_channel_id_private = private_key;
David Benjamin936aada2016-06-07 19:09:22 -04002007 ctx->tlsext_channel_id_enabled = 1;
2008
2009 return 1;
2010}
2011
2012int SSL_set1_tls_channel_id(SSL *ssl, EVP_PKEY *private_key) {
2013 if (!is_p256_key(private_key)) {
2014 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256);
2015 return 0;
2016 }
2017
2018 EVP_PKEY_free(ssl->tlsext_channel_id_private);
Adam Langley310d3f62016-07-12 10:39:20 -07002019 EVP_PKEY_up_ref(private_key);
2020 ssl->tlsext_channel_id_private = private_key;
David Benjamin936aada2016-06-07 19:09:22 -04002021 ssl->tlsext_channel_id_enabled = 1;
2022
2023 return 1;
2024}
2025
2026size_t SSL_get_tls_channel_id(SSL *ssl, uint8_t *out, size_t max_out) {
2027 if (!ssl->s3->tlsext_channel_id_valid) {
2028 return 0;
2029 }
David Benjamin17cf2cb2016-12-13 01:07:13 -05002030 OPENSSL_memcpy(out, ssl->s3->tlsext_channel_id,
2031 (max_out < 64) ? max_out : 64);
David Benjamin936aada2016-06-07 19:09:22 -04002032 return 64;
2033}
2034
David Benjamin936aada2016-06-07 19:09:22 -04002035size_t SSL_get0_certificate_types(SSL *ssl, const uint8_t **out_types) {
David Benjamina0486782016-10-06 19:11:32 -04002036 if (ssl->server || ssl->s3->hs == NULL) {
David Benjamin936aada2016-06-07 19:09:22 -04002037 *out_types = NULL;
2038 return 0;
2039 }
David Benjamin08f5c762017-09-21 02:43:05 -04002040 *out_types = ssl->s3->hs->certificate_types.data();
2041 return ssl->s3->hs->certificate_types.size();
David Benjamin936aada2016-06-07 19:09:22 -04002042}
2043
David Benjamin0d56f882015-12-19 17:05:56 -05002044EVP_PKEY *SSL_get_privatekey(const SSL *ssl) {
2045 if (ssl->cert != NULL) {
2046 return ssl->cert->privatekey;
Adam Langleyfcf25832014-12-18 17:42:32 -08002047 }
2048
2049 return NULL;
2050}
2051
Adam Langleyfcf25832014-12-18 17:42:32 -08002052EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
2053 if (ctx->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04002054 return ctx->cert->privatekey;
Adam Langleyfcf25832014-12-18 17:42:32 -08002055 }
2056
2057 return NULL;
2058}
2059
David Benjamin42fea372015-09-19 01:22:44 -04002060const SSL_CIPHER *SSL_get_current_cipher(const SSL *ssl) {
David Benjamincfc11c22017-07-18 22:45:18 -04002061 return ssl->s3->aead_write_ctx->cipher();
Adam Langleyfcf25832014-12-18 17:42:32 -08002062}
2063
David Benjamin936aada2016-06-07 19:09:22 -04002064int SSL_session_reused(const SSL *ssl) {
Steven Valdeze831a812017-03-09 14:56:07 -05002065 return ssl->s3->session_reused || SSL_in_early_data(ssl);
David Benjamin936aada2016-06-07 19:09:22 -04002066}
2067
David Benjamin0d56f882015-12-19 17:05:56 -05002068const COMP_METHOD *SSL_get_current_compression(SSL *ssl) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002069
David Benjamin0d56f882015-12-19 17:05:56 -05002070const COMP_METHOD *SSL_get_current_expansion(SSL *ssl) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002071
Adam Langley29ec5d12016-03-01 16:12:28 -08002072int *SSL_get_server_tmp_key(SSL *ssl, EVP_PKEY **out_key) { return 0; }
2073
Adam Langleyfcf25832014-12-18 17:42:32 -08002074void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
Adam Langleybb85f3d2015-10-28 18:44:11 -07002075 ctx->quiet_shutdown = (mode != 0);
Adam Langleyfcf25832014-12-18 17:42:32 -08002076}
2077
2078int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
2079 return ctx->quiet_shutdown;
2080}
2081
Adam Langleybb85f3d2015-10-28 18:44:11 -07002082void SSL_set_quiet_shutdown(SSL *ssl, int mode) {
2083 ssl->quiet_shutdown = (mode != 0);
2084}
Adam Langleyfcf25832014-12-18 17:42:32 -08002085
David Benjamin9f859492015-10-03 10:44:30 -04002086int SSL_get_quiet_shutdown(const SSL *ssl) { return ssl->quiet_shutdown; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002087
David Benjamin63006a92015-10-18 00:00:06 -04002088void SSL_set_shutdown(SSL *ssl, int mode) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002089 // It is an error to clear any bits that have already been set. (We can't try
2090 // to get a second close_notify or send two.)
David Benjaminfa214e42016-05-10 17:03:10 -04002091 assert((SSL_get_shutdown(ssl) & mode) == SSL_get_shutdown(ssl));
David Benjamin63006a92015-10-18 00:00:06 -04002092
David Benjaminfa214e42016-05-10 17:03:10 -04002093 if (mode & SSL_RECEIVED_SHUTDOWN &&
2094 ssl->s3->recv_shutdown == ssl_shutdown_none) {
2095 ssl->s3->recv_shutdown = ssl_shutdown_close_notify;
2096 }
2097
2098 if (mode & SSL_SENT_SHUTDOWN &&
2099 ssl->s3->send_shutdown == ssl_shutdown_none) {
2100 ssl->s3->send_shutdown = ssl_shutdown_close_notify;
2101 }
David Benjamin63006a92015-10-18 00:00:06 -04002102}
Adam Langleyfcf25832014-12-18 17:42:32 -08002103
David Benjaminfa214e42016-05-10 17:03:10 -04002104int SSL_get_shutdown(const SSL *ssl) {
2105 int ret = 0;
2106 if (ssl->s3->recv_shutdown != ssl_shutdown_none) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002107 // Historically, OpenSSL set |SSL_RECEIVED_SHUTDOWN| on both close_notify
2108 // and fatal alert.
David Benjaminfa214e42016-05-10 17:03:10 -04002109 ret |= SSL_RECEIVED_SHUTDOWN;
2110 }
2111 if (ssl->s3->send_shutdown == ssl_shutdown_close_notify) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002112 // Historically, OpenSSL set |SSL_SENT_SHUTDOWN| on only close_notify.
David Benjaminfa214e42016-05-10 17:03:10 -04002113 ret |= SSL_SENT_SHUTDOWN;
2114 }
2115 return ret;
2116}
Adam Langleyfcf25832014-12-18 17:42:32 -08002117
Adam Langleyfcf25832014-12-18 17:42:32 -08002118SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
2119
2120SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
2121 if (ssl->ctx == ctx) {
2122 return ssl->ctx;
2123 }
2124
David Benjaminc11ea9422017-08-29 16:33:21 -04002125 // One cannot change the X.509 callbacks during a connection.
Adam Langley3509dac2017-02-01 11:59:18 -08002126 if (ssl->ctx->x509_method != ctx->x509_method) {
2127 assert(0);
2128 return NULL;
2129 }
2130
Adam Langleyfcf25832014-12-18 17:42:32 -08002131 if (ctx == NULL) {
David Benjaminbe497062017-03-10 16:08:36 -05002132 ctx = ssl->session_ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08002133 }
2134
David Benjamin2755a3e2015-04-22 16:17:58 -04002135 ssl_cert_free(ssl->cert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002136 ssl->cert = ssl_cert_dup(ctx->cert);
David Benjamin2755a3e2015-04-22 16:17:58 -04002137
David Benjaminf650c712016-12-22 15:54:51 -05002138 SSL_CTX_up_ref(ctx);
2139 SSL_CTX_free(ssl->ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002140 ssl->ctx = ctx;
2141
Adam Langleyfcf25832014-12-18 17:42:32 -08002142 return ssl->ctx;
2143}
2144
Adam Langley95c29f32014-06-20 12:00:00 -07002145void SSL_set_info_callback(SSL *ssl,
David Benjamin82170242015-10-17 22:51:17 -04002146 void (*cb)(const SSL *ssl, int type, int value)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002147 ssl->info_callback = cb;
2148}
Adam Langley95c29f32014-06-20 12:00:00 -07002149
David Benjamin82170242015-10-17 22:51:17 -04002150void (*SSL_get_info_callback(const SSL *ssl))(const SSL *ssl, int type,
2151 int value) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002152 return ssl->info_callback;
2153}
Adam Langley95c29f32014-06-20 12:00:00 -07002154
David Benjamincb0c29f2016-12-12 17:00:50 -05002155int SSL_state(const SSL *ssl) {
David Benjamin9e766d72017-02-10 23:14:17 -05002156 return SSL_in_init(ssl) ? SSL_ST_INIT : SSL_ST_OK;
David Benjamincb0c29f2016-12-12 17:00:50 -05002157}
Adam Langley95c29f32014-06-20 12:00:00 -07002158
David Benjaminece089c2015-05-15 23:52:42 -04002159void SSL_set_state(SSL *ssl, int state) { }
Adam Langley95c29f32014-06-20 12:00:00 -07002160
Emily Stark95a79ee2016-02-25 21:12:28 -08002161char *SSL_get_shared_ciphers(const SSL *ssl, char *buf, int len) {
2162 if (len <= 0) {
2163 return NULL;
2164 }
2165 buf[0] = '\0';
2166 return buf;
2167}
2168
David Benjamin8a589332015-12-04 23:14:35 -05002169int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
David Benjamind94682d2017-05-14 17:10:18 -04002170 CRYPTO_EX_dup *dup_unused, CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002171 int index;
2172 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp,
David Benjamind94682d2017-05-14 17:10:18 -04002173 free_func)) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002174 return -1;
2175 }
2176 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002177}
Adam Langley95c29f32014-06-20 12:00:00 -07002178
David Benjamin27e377e2017-07-31 19:09:42 -04002179int SSL_set_ex_data(SSL *ssl, int idx, void *data) {
2180 return CRYPTO_set_ex_data(&ssl->ex_data, idx, data);
Adam Langleyfcf25832014-12-18 17:42:32 -08002181}
Adam Langley95c29f32014-06-20 12:00:00 -07002182
David Benjamin1ac08fe2015-09-15 01:36:22 -04002183void *SSL_get_ex_data(const SSL *ssl, int idx) {
2184 return CRYPTO_get_ex_data(&ssl->ex_data, idx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002185}
Adam Langley95c29f32014-06-20 12:00:00 -07002186
David Benjamin8a589332015-12-04 23:14:35 -05002187int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
David Benjamind94682d2017-05-14 17:10:18 -04002188 CRYPTO_EX_dup *dup_unused,
Adam Langleyfcf25832014-12-18 17:42:32 -08002189 CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002190 int index;
2191 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp,
David Benjamind94682d2017-05-14 17:10:18 -04002192 free_func)) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002193 return -1;
2194 }
2195 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002196}
Adam Langley95c29f32014-06-20 12:00:00 -07002197
David Benjamin27e377e2017-07-31 19:09:42 -04002198int SSL_CTX_set_ex_data(SSL_CTX *ctx, int idx, void *data) {
2199 return CRYPTO_set_ex_data(&ctx->ex_data, idx, data);
Adam Langleyfcf25832014-12-18 17:42:32 -08002200}
Adam Langley95c29f32014-06-20 12:00:00 -07002201
David Benjamin1ac08fe2015-09-15 01:36:22 -04002202void *SSL_CTX_get_ex_data(const SSL_CTX *ctx, int idx) {
2203 return CRYPTO_get_ex_data(&ctx->ex_data, idx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002204}
Adam Langley95c29f32014-06-20 12:00:00 -07002205
David Benjamin93d17492015-10-17 12:43:36 -04002206int SSL_want(const SSL *ssl) { return ssl->rwstate; }
Adam Langley95c29f32014-06-20 12:00:00 -07002207
Adam Langleyfcf25832014-12-18 17:42:32 -08002208void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
2209 RSA *(*cb)(SSL *ssl, int is_export,
David Benjamin27e377e2017-07-31 19:09:42 -04002210 int keylength)) {}
Adam Langley95c29f32014-06-20 12:00:00 -07002211
Adam Langleyfcf25832014-12-18 17:42:32 -08002212void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
David Benjamin27e377e2017-07-31 19:09:42 -04002213 int keylength)) {}
Adam Langleyfcf25832014-12-18 17:42:32 -08002214
2215void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
David Benjamin27e377e2017-07-31 19:09:42 -04002216 DH *(*cb)(SSL *ssl, int is_export,
2217 int keylength)) {}
Adam Langleyfcf25832014-12-18 17:42:32 -08002218
David Benjamin27e377e2017-07-31 19:09:42 -04002219void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*cb)(SSL *ssl, int is_export,
2220 int keylength)) {}
Adam Langleyfcf25832014-12-18 17:42:32 -08002221
David Benjamin0a9bf662017-06-21 16:34:15 -04002222static int use_psk_identity_hint(char **out, const char *identity_hint) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002223 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
David Benjamin3570d732015-06-29 00:28:17 -04002224 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -08002225 return 0;
2226 }
2227
David Benjaminc11ea9422017-08-29 16:33:21 -04002228 // Clear currently configured hint, if any.
David Benjamin0a9bf662017-06-21 16:34:15 -04002229 OPENSSL_free(*out);
2230 *out = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002231
David Benjaminc11ea9422017-08-29 16:33:21 -04002232 // Treat the empty hint as not supplying one. Plain PSK makes it possible to
2233 // send either no hint (omit ServerKeyExchange) or an empty hint, while
2234 // ECDHE_PSK can only spell empty hint. Having different capabilities is odd,
2235 // so we interpret empty and missing as identical.
David Benjamin78679342016-09-16 19:42:05 -04002236 if (identity_hint != NULL && identity_hint[0] != '\0') {
David Benjamin0a9bf662017-06-21 16:34:15 -04002237 *out = BUF_strdup(identity_hint);
2238 if (*out == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002239 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002240 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002241 }
Adam Langley95c29f32014-06-20 12:00:00 -07002242
Adam Langleyfcf25832014-12-18 17:42:32 -08002243 return 1;
2244}
Adam Langley95c29f32014-06-20 12:00:00 -07002245
David Benjamin0a9bf662017-06-21 16:34:15 -04002246int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
2247 return use_psk_identity_hint(&ctx->psk_identity_hint, identity_hint);
2248}
2249
2250int SSL_use_psk_identity_hint(SSL *ssl, const char *identity_hint) {
2251 return use_psk_identity_hint(&ssl->psk_identity_hint, identity_hint);
2252}
2253
David Benjamine8814df2015-09-15 08:05:54 -04002254const char *SSL_get_psk_identity_hint(const SSL *ssl) {
2255 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002256 return NULL;
2257 }
David Benjamine8814df2015-09-15 08:05:54 -04002258 return ssl->psk_identity_hint;
Adam Langleyfcf25832014-12-18 17:42:32 -08002259}
Adam Langley95c29f32014-06-20 12:00:00 -07002260
David Benjamine8814df2015-09-15 08:05:54 -04002261const char *SSL_get_psk_identity(const SSL *ssl) {
Steven Valdez87eab492016-06-27 16:34:59 -04002262 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002263 return NULL;
2264 }
Steven Valdez87eab492016-06-27 16:34:59 -04002265 SSL_SESSION *session = SSL_get_session(ssl);
2266 if (session == NULL) {
2267 return NULL;
2268 }
2269 return session->psk_identity;
Adam Langleyfcf25832014-12-18 17:42:32 -08002270}
Adam Langley95c29f32014-06-20 12:00:00 -07002271
Adam Langleyfcf25832014-12-18 17:42:32 -08002272void SSL_set_psk_client_callback(
David Benjamine8814df2015-09-15 08:05:54 -04002273 SSL *ssl, unsigned (*cb)(SSL *ssl, const char *hint, char *identity,
2274 unsigned max_identity_len, uint8_t *psk,
2275 unsigned max_psk_len)) {
2276 ssl->psk_client_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002277}
Adam Langley95c29f32014-06-20 12:00:00 -07002278
Adam Langleyfcf25832014-12-18 17:42:32 -08002279void SSL_CTX_set_psk_client_callback(
David Benjamine8814df2015-09-15 08:05:54 -04002280 SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *hint, char *identity,
2281 unsigned max_identity_len, uint8_t *psk,
2282 unsigned max_psk_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002283 ctx->psk_client_callback = cb;
2284}
Adam Langley95c29f32014-06-20 12:00:00 -07002285
Adam Langleyfcf25832014-12-18 17:42:32 -08002286void SSL_set_psk_server_callback(
David Benjamine8814df2015-09-15 08:05:54 -04002287 SSL *ssl, unsigned (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
2288 unsigned max_psk_len)) {
2289 ssl->psk_server_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002290}
Adam Langley95c29f32014-06-20 12:00:00 -07002291
Adam Langleyfcf25832014-12-18 17:42:32 -08002292void SSL_CTX_set_psk_server_callback(
David Benjamine8814df2015-09-15 08:05:54 -04002293 SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *identity,
2294 uint8_t *psk, unsigned max_psk_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002295 ctx->psk_server_callback = cb;
2296}
Adam Langley95c29f32014-06-20 12:00:00 -07002297
Adam Langleyfcf25832014-12-18 17:42:32 -08002298void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
2299 void (*cb)(int write_p, int version,
2300 int content_type, const void *buf,
2301 size_t len, SSL *ssl, void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002302 ctx->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002303}
David Benjamin61ecccf2015-05-05 09:44:51 -04002304
2305void SSL_CTX_set_msg_callback_arg(SSL_CTX *ctx, void *arg) {
2306 ctx->msg_callback_arg = arg;
2307}
2308
Adam Langleyfcf25832014-12-18 17:42:32 -08002309void SSL_set_msg_callback(SSL *ssl,
2310 void (*cb)(int write_p, int version, int content_type,
2311 const void *buf, size_t len, SSL *ssl,
2312 void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002313 ssl->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002314}
Adam Langley95c29f32014-06-20 12:00:00 -07002315
David Benjamin61ecccf2015-05-05 09:44:51 -04002316void SSL_set_msg_callback_arg(SSL *ssl, void *arg) {
2317 ssl->msg_callback_arg = arg;
2318}
2319
David Benjamind28f59c2015-11-17 22:32:50 -05002320void SSL_CTX_set_keylog_callback(SSL_CTX *ctx,
2321 void (*cb)(const SSL *ssl, const char *line)) {
2322 ctx->keylog_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002323}
Adam Langley95c29f32014-06-20 12:00:00 -07002324
David Benjamin6e3f5cc2016-09-14 14:43:14 -04002325void (*SSL_CTX_get_keylog_callback(const SSL_CTX *ctx))(const SSL *ssl,
2326 const char *line) {
2327 return ctx->keylog_callback;
2328}
2329
Taylor Brandstetter9edb2c62016-06-08 15:26:59 -07002330void SSL_CTX_set_current_time_cb(SSL_CTX *ctx,
2331 void (*cb)(const SSL *ssl,
2332 struct timeval *out_clock)) {
2333 ctx->current_time_cb = cb;
2334}
2335
David Benjamin5d8b1282015-10-17 23:26:35 -04002336int SSL_is_init_finished(const SSL *ssl) {
David Benjamin9e766d72017-02-10 23:14:17 -05002337 return !SSL_in_init(ssl);
David Benjamin5d8b1282015-10-17 23:26:35 -04002338}
2339
2340int SSL_in_init(const SSL *ssl) {
Steven Valdezd8168742017-08-31 10:15:48 -04002341 // This returns false once all the handshake state has been finalized, to
2342 // allow callbacks and getters based on SSL_in_init to return the correct
2343 // values.
2344 SSL_HANDSHAKE *hs = ssl->s3->hs;
2345 return hs != nullptr && !hs->handshake_finalized;
David Benjamin5d8b1282015-10-17 23:26:35 -04002346}
2347
2348int SSL_in_false_start(const SSL *ssl) {
David Benjamina0486782016-10-06 19:11:32 -04002349 if (ssl->s3->hs == NULL) {
2350 return 0;
2351 }
2352 return ssl->s3->hs->in_false_start;
David Benjamined7c4752015-02-16 19:16:46 -05002353}
2354
David Benjamin0d56f882015-12-19 17:05:56 -05002355int SSL_cutthrough_complete(const SSL *ssl) {
2356 return SSL_in_false_start(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08002357}
Adam Langley95c29f32014-06-20 12:00:00 -07002358
Adam Langleyfcf25832014-12-18 17:42:32 -08002359void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
2360 size_t *ssl_session_size) {
2361 *ssl_size = sizeof(SSL);
2362 *ssl_ctx_size = sizeof(SSL_CTX);
2363 *ssl_session_size = sizeof(SSL_SESSION);
2364}
Feng Lu41aa3252014-11-21 22:47:56 -08002365
David Benjamince079fd2016-08-02 16:22:34 -04002366int SSL_is_server(const SSL *ssl) { return ssl->server; }
2367
2368int SSL_is_dtls(const SSL *ssl) { return ssl->method->is_dtls; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002369
Alessandro Ghedini57e81e62017-03-14 23:36:00 +00002370void SSL_CTX_set_select_certificate_cb(
2371 SSL_CTX *ctx,
2372 enum ssl_select_cert_result_t (*cb)(const SSL_CLIENT_HELLO *)) {
David Benjamind4c2bce2015-10-17 12:28:18 -04002373 ctx->select_certificate_cb = cb;
2374}
2375
David Benjamin731058e2016-12-03 23:15:13 -05002376void SSL_CTX_set_dos_protection_cb(SSL_CTX *ctx,
2377 int (*cb)(const SSL_CLIENT_HELLO *)) {
Adam Langley524e7172015-02-20 16:04:00 -08002378 ctx->dos_protection_cb = cb;
2379}
2380
David Benjamin1d5ef3b2015-10-12 19:54:18 -04002381void SSL_set_renegotiate_mode(SSL *ssl, enum ssl_renegotiate_mode_t mode) {
2382 ssl->renegotiate_mode = mode;
2383}
2384
Adam Langleyc2d32802015-11-03 18:36:10 -08002385int SSL_get_ivs(const SSL *ssl, const uint8_t **out_read_iv,
2386 const uint8_t **out_write_iv, size_t *out_iv_len) {
Adam Langleyc2d32802015-11-03 18:36:10 -08002387 size_t write_iv_len;
David Benjamincfc11c22017-07-18 22:45:18 -04002388 if (!ssl->s3->aead_read_ctx->GetIV(out_read_iv, out_iv_len) ||
2389 !ssl->s3->aead_write_ctx->GetIV(out_write_iv, &write_iv_len) ||
Adam Langleyc2d32802015-11-03 18:36:10 -08002390 *out_iv_len != write_iv_len) {
2391 return 0;
2392 }
2393
2394 return 1;
David Benjamine348ff42015-11-06 17:55:30 -05002395}
David Benjamin6e807652015-11-02 12:02:20 -05002396
David Benjamin91b25012016-01-21 16:39:58 -05002397static uint64_t be_to_u64(const uint8_t in[8]) {
David Benjaminde942382016-02-11 12:02:01 -05002398 return (((uint64_t)in[0]) << 56) | (((uint64_t)in[1]) << 48) |
2399 (((uint64_t)in[2]) << 40) | (((uint64_t)in[3]) << 32) |
2400 (((uint64_t)in[4]) << 24) | (((uint64_t)in[5]) << 16) |
2401 (((uint64_t)in[6]) << 8) | ((uint64_t)in[7]);
David Benjamin91b25012016-01-21 16:39:58 -05002402}
2403
2404uint64_t SSL_get_read_sequence(const SSL *ssl) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002405 // TODO(davidben): Internally represent sequence numbers as uint64_t.
David Benjamince079fd2016-08-02 16:22:34 -04002406 if (SSL_is_dtls(ssl)) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002407 // max_seq_num already includes the epoch.
David Benjaminde942382016-02-11 12:02:01 -05002408 assert(ssl->d1->r_epoch == (ssl->d1->bitmap.max_seq_num >> 48));
2409 return ssl->d1->bitmap.max_seq_num;
2410 }
David Benjamin91b25012016-01-21 16:39:58 -05002411 return be_to_u64(ssl->s3->read_sequence);
2412}
2413
2414uint64_t SSL_get_write_sequence(const SSL *ssl) {
David Benjaminde942382016-02-11 12:02:01 -05002415 uint64_t ret = be_to_u64(ssl->s3->write_sequence);
David Benjamince079fd2016-08-02 16:22:34 -04002416 if (SSL_is_dtls(ssl)) {
David Benjaminde942382016-02-11 12:02:01 -05002417 assert((ret >> 48) == 0);
2418 ret |= ((uint64_t)ssl->d1->w_epoch) << 48;
2419 }
2420 return ret;
David Benjamin91b25012016-01-21 16:39:58 -05002421}
2422
Steven Valdez02563852016-06-23 13:33:05 -04002423uint16_t SSL_get_peer_signature_algorithm(const SSL *ssl) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002424 // TODO(davidben): This checks the wrong session if there is a renegotiation
2425 // in progress.
David Benjaminf1050fd2016-12-13 20:05:36 -05002426 SSL_SESSION *session = SSL_get_session(ssl);
2427 if (session == NULL) {
2428 return 0;
2429 }
2430
2431 return session->peer_signature_algorithm;
Steven Valdez02563852016-06-23 13:33:05 -04002432}
2433
David Benjamin96ba15f2015-12-25 14:10:17 -05002434size_t SSL_get_client_random(const SSL *ssl, uint8_t *out, size_t max_out) {
2435 if (max_out == 0) {
2436 return sizeof(ssl->s3->client_random);
2437 }
2438 if (max_out > sizeof(ssl->s3->client_random)) {
2439 max_out = sizeof(ssl->s3->client_random);
2440 }
David Benjamin17cf2cb2016-12-13 01:07:13 -05002441 OPENSSL_memcpy(out, ssl->s3->client_random, max_out);
David Benjamin96ba15f2015-12-25 14:10:17 -05002442 return max_out;
2443}
2444
2445size_t SSL_get_server_random(const SSL *ssl, uint8_t *out, size_t max_out) {
2446 if (max_out == 0) {
2447 return sizeof(ssl->s3->server_random);
2448 }
2449 if (max_out > sizeof(ssl->s3->server_random)) {
2450 max_out = sizeof(ssl->s3->server_random);
2451 }
David Benjamin17cf2cb2016-12-13 01:07:13 -05002452 OPENSSL_memcpy(out, ssl->s3->server_random, max_out);
David Benjamin96ba15f2015-12-25 14:10:17 -05002453 return max_out;
2454}
2455
David Benjaminf6494f42016-01-07 12:37:41 -08002456const SSL_CIPHER *SSL_get_pending_cipher(const SSL *ssl) {
David Benjamin45738dd2017-02-09 20:01:26 -05002457 SSL_HANDSHAKE *hs = ssl->s3->hs;
2458 if (hs == NULL) {
David Benjaminf6494f42016-01-07 12:37:41 -08002459 return NULL;
2460 }
David Benjamin45738dd2017-02-09 20:01:26 -05002461 return hs->new_cipher;
David Benjaminf6494f42016-01-07 12:37:41 -08002462}
2463
David Benjaminbbaf3672016-11-17 10:53:09 +09002464void SSL_set_retain_only_sha256_of_client_certs(SSL *ssl, int enabled) {
2465 ssl->retain_only_sha256_of_client_certs = !!enabled;
2466}
2467
David Benjamincdd0b7e2016-01-07 10:10:41 -08002468void SSL_CTX_set_retain_only_sha256_of_client_certs(SSL_CTX *ctx, int enabled) {
2469 ctx->retain_only_sha256_of_client_certs = !!enabled;
2470}
2471
David Benjamin65ac9972016-09-02 21:35:25 -04002472void SSL_CTX_set_grease_enabled(SSL_CTX *ctx, int enabled) {
2473 ctx->grease_enabled = !!enabled;
2474}
2475
David Benjamin35ac5b72017-03-03 15:05:56 -05002476int32_t SSL_get_ticket_age_skew(const SSL *ssl) {
2477 return ssl->s3->ticket_age_skew;
2478}
2479
David Benjamin27bbae42015-09-13 00:54:37 -04002480int SSL_clear(SSL *ssl) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002481 // In OpenSSL, reusing a client |SSL| with |SSL_clear| causes the previously
2482 // established session to be offered the next time around. wpa_supplicant
2483 // depends on this behavior, so emulate it.
David Benjaminb79cc842016-12-07 15:57:14 -05002484 SSL_SESSION *session = NULL;
2485 if (!ssl->server && ssl->s3->established_session != NULL) {
2486 session = ssl->s3->established_session;
2487 SSL_SESSION_up_ref(session);
2488 }
2489
David Benjaminc11ea9422017-08-29 16:33:21 -04002490 // TODO(davidben): Some state on |ssl| is reset both in |SSL_new| and
2491 // |SSL_clear| because it is per-connection state rather than configuration
2492 // state. Per-connection state should be on |ssl->s3| and |ssl->d1| so it is
2493 // naturally reset at the right points between |SSL_new|, |SSL_clear|, and
2494 // |ssl3_new|.
David Benjamin27bbae42015-09-13 00:54:37 -04002495
2496 ssl->rwstate = SSL_NOTHING;
2497
2498 BUF_MEM_free(ssl->init_buf);
2499 ssl->init_buf = NULL;
2500
David Benjaminc11ea9422017-08-29 16:33:21 -04002501 // The ssl->d1->mtu is simultaneously configuration (preserved across
2502 // clear) and connection-specific state (gets reset).
2503 //
2504 // TODO(davidben): Avoid this.
David Benjamin27bbae42015-09-13 00:54:37 -04002505 unsigned mtu = 0;
2506 if (ssl->d1 != NULL) {
2507 mtu = ssl->d1->mtu;
2508 }
2509
2510 ssl->method->ssl_free(ssl);
2511 if (!ssl->method->ssl_new(ssl)) {
David Benjaminb79cc842016-12-07 15:57:14 -05002512 SSL_SESSION_free(session);
David Benjamin27bbae42015-09-13 00:54:37 -04002513 return 0;
2514 }
David Benjamin27bbae42015-09-13 00:54:37 -04002515
David Benjamince079fd2016-08-02 16:22:34 -04002516 if (SSL_is_dtls(ssl) && (SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
David Benjamin27bbae42015-09-13 00:54:37 -04002517 ssl->d1->mtu = mtu;
2518 }
2519
David Benjaminb79cc842016-12-07 15:57:14 -05002520 if (session != NULL) {
2521 SSL_set_session(ssl, session);
2522 SSL_SESSION_free(session);
2523 }
2524
David Benjamin27bbae42015-09-13 00:54:37 -04002525 return 1;
2526}
2527
David Benjaminda881e92015-04-26 14:45:04 -04002528int SSL_CTX_sess_connect(const SSL_CTX *ctx) { return 0; }
2529int SSL_CTX_sess_connect_good(const SSL_CTX *ctx) { return 0; }
2530int SSL_CTX_sess_connect_renegotiate(const SSL_CTX *ctx) { return 0; }
2531int SSL_CTX_sess_accept(const SSL_CTX *ctx) { return 0; }
2532int SSL_CTX_sess_accept_renegotiate(const SSL_CTX *ctx) { return 0; }
2533int SSL_CTX_sess_accept_good(const SSL_CTX *ctx) { return 0; }
2534int SSL_CTX_sess_hits(const SSL_CTX *ctx) { return 0; }
2535int SSL_CTX_sess_cb_hits(const SSL_CTX *ctx) { return 0; }
2536int SSL_CTX_sess_misses(const SSL_CTX *ctx) { return 0; }
2537int SSL_CTX_sess_timeouts(const SSL_CTX *ctx) { return 0; }
2538int SSL_CTX_sess_cache_full(const SSL_CTX *ctx) { return 0; }
David Benjamin936aada2016-06-07 19:09:22 -04002539
2540int SSL_num_renegotiations(const SSL *ssl) {
2541 return SSL_total_renegotiations(ssl);
2542}
2543
2544int SSL_CTX_need_tmp_RSA(const SSL_CTX *ctx) { return 0; }
2545int SSL_need_tmp_RSA(const SSL *ssl) { return 0; }
2546int SSL_CTX_set_tmp_rsa(SSL_CTX *ctx, const RSA *rsa) { return 1; }
2547int SSL_set_tmp_rsa(SSL *ssl, const RSA *rsa) { return 1; }
Matt Braithwaite4838d8a2015-08-20 13:20:03 -07002548void ERR_load_SSL_strings(void) {}
David Benjamin27bbae42015-09-13 00:54:37 -04002549void SSL_load_error_strings(void) {}
David Benjamin936aada2016-06-07 19:09:22 -04002550int SSL_cache_hit(SSL *ssl) { return SSL_session_reused(ssl); }
2551
2552int SSL_CTX_set_tmp_ecdh(SSL_CTX *ctx, const EC_KEY *ec_key) {
2553 if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) {
2554 OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
2555 return 0;
2556 }
2557 int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key));
2558 return SSL_CTX_set1_curves(ctx, &nid, 1);
2559}
2560
2561int SSL_set_tmp_ecdh(SSL *ssl, const EC_KEY *ec_key) {
2562 if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) {
2563 OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
2564 return 0;
2565 }
2566 int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key));
2567 return SSL_set1_curves(ssl, &nid, 1);
2568}
David Benjamin721e8b72016-08-03 13:13:17 -04002569
Adam Langley4c341d02017-03-08 19:33:21 -08002570void SSL_CTX_set_ticket_aead_method(SSL_CTX *ctx,
2571 const SSL_TICKET_AEAD_METHOD *aead_method) {
2572 ctx->ticket_aead_method = aead_method;
2573}