blob: 950bbf8427354852ea4dc15b68e4155aa4493894 [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 Benjamin76c2efc2015-08-31 14:24:29 -0400167/* |SSL_R_UNKNOWN_PROTOCOL| is no longer emitted, but continue to define it
David Benjamin97760d52015-07-24 23:02:49 -0400168 * 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 Benjamin311c2572016-08-17 15:54:36 -0400171/* The following errors are no longer emitted, but are used in nginx without
172 * #ifdefs. */
173OPENSSL_DECLARE_ERROR_REASON(SSL, BLOCK_CIPHER_PAD_IS_WRONG)
174OPENSSL_DECLARE_ERROR_REASON(SSL, NO_CIPHERS_SPECIFIED)
175
Adam Langleyfcf25832014-12-18 17:42:32 -0800176/* 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 Benjamin1d0a1942015-04-26 15:35:35 -0400182/* kMaxHandshakeSize is the maximum size, in bytes, of a handshake message. */
183static const size_t kMaxHandshakeSize = (1u << 24) - 1;
184
David Benjaminaa585132015-06-29 23:36:17 -0400185static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl =
186 CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
187static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl_ctx =
188 CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
David Benjamin9f33fc62015-04-15 17:29:53 -0400189
David Benjamin86e95b82017-07-18 16:34:25 -0400190void ssl_reset_error_state(SSL *ssl) {
191 /* Functions which use |SSL_get_error| must reset I/O and error state on
192 * entry. */
193 ssl->rwstate = SSL_NOTHING;
194 ERR_clear_error();
195 ERR_clear_system_error();
196}
197
198int ssl_can_write(const SSL *ssl) {
199 return !SSL_in_init(ssl) || ssl->s3->hs->can_early_write;
200}
201
202int ssl_can_read(const SSL *ssl) {
203 return !SSL_in_init(ssl) || ssl->s3->hs->can_early_read;
204}
205
206void ssl_cipher_preference_list_free(
207 struct ssl_cipher_preference_list_st *cipher_list) {
208 if (cipher_list == NULL) {
209 return;
210 }
211 sk_SSL_CIPHER_free(cipher_list->ciphers);
212 OPENSSL_free(cipher_list->in_group_flags);
213 OPENSSL_free(cipher_list);
214}
215
216void ssl_update_cache(SSL_HANDSHAKE *hs, int mode) {
217 SSL *const ssl = hs->ssl;
218 SSL_CTX *ctx = ssl->session_ctx;
219 /* Never cache sessions with empty session IDs. */
220 if (ssl->s3->established_session->session_id_length == 0 ||
221 (ctx->session_cache_mode & mode) != mode) {
222 return;
223 }
224
225 /* Clients never use the internal session cache. */
226 int use_internal_cache = ssl->server && !(ctx->session_cache_mode &
227 SSL_SESS_CACHE_NO_INTERNAL_STORE);
228
229 /* A client may see new sessions on abbreviated handshakes if the server
230 * decides to renew the ticket. Once the handshake is completed, it should be
231 * inserted into the cache. */
232 if (ssl->s3->established_session != ssl->session ||
233 (!ssl->server && hs->ticket_expected)) {
234 if (use_internal_cache) {
235 SSL_CTX_add_session(ctx, ssl->s3->established_session);
236 }
237 if (ctx->new_session_cb != NULL) {
238 SSL_SESSION_up_ref(ssl->s3->established_session);
239 if (!ctx->new_session_cb(ssl, ssl->s3->established_session)) {
240 /* |new_session_cb|'s return value signals whether it took ownership. */
241 SSL_SESSION_free(ssl->s3->established_session);
242 }
243 }
244 }
245
246 if (use_internal_cache &&
247 !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR)) {
248 /* Automatically flush the internal session cache every 255 connections. */
249 int flush_cache = 0;
250 CRYPTO_MUTEX_lock_write(&ctx->lock);
251 ctx->handshakes_since_cache_flush++;
252 if (ctx->handshakes_since_cache_flush >= 255) {
253 flush_cache = 1;
254 ctx->handshakes_since_cache_flush = 0;
255 }
256 CRYPTO_MUTEX_unlock_write(&ctx->lock);
257
258 if (flush_cache) {
259 struct OPENSSL_timeval now;
260 ssl_get_current_time(ssl, &now);
261 SSL_CTX_flush_sessions(ctx, now.tv_sec);
262 }
263 }
264}
265
266static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
267 static const char hextable[] = "0123456789abcdef";
268 uint8_t *out;
269
270 if (!CBB_add_space(cbb, &out, in_len * 2)) {
271 return 0;
272 }
273
274 for (size_t i = 0; i < in_len; i++) {
275 *(out++) = (uint8_t)hextable[in[i] >> 4];
276 *(out++) = (uint8_t)hextable[in[i] & 0xf];
277 }
278
279 return 1;
280}
281
282int ssl_log_secret(const SSL *ssl, const char *label, const uint8_t *secret,
283 size_t secret_len) {
284 if (ssl->ctx->keylog_callback == NULL) {
285 return 1;
286 }
287
David Benjamin1386aad2017-07-19 23:57:40 -0400288 ScopedCBB cbb;
David Benjamin86e95b82017-07-18 16:34:25 -0400289 uint8_t *out;
290 size_t out_len;
David Benjamin1386aad2017-07-19 23:57:40 -0400291 if (!CBB_init(cbb.get(), strlen(label) + 1 + SSL3_RANDOM_SIZE * 2 + 1 +
David Benjamin86e95b82017-07-18 16:34:25 -0400292 secret_len * 2 + 1) ||
David Benjamin1386aad2017-07-19 23:57:40 -0400293 !CBB_add_bytes(cbb.get(), (const uint8_t *)label, strlen(label)) ||
294 !CBB_add_bytes(cbb.get(), (const uint8_t *)" ", 1) ||
295 !cbb_add_hex(cbb.get(), ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
296 !CBB_add_bytes(cbb.get(), (const uint8_t *)" ", 1) ||
297 !cbb_add_hex(cbb.get(), secret, secret_len) ||
298 !CBB_add_u8(cbb.get(), 0 /* NUL */) ||
299 !CBB_finish(cbb.get(), &out, &out_len)) {
David Benjamin86e95b82017-07-18 16:34:25 -0400300 return 0;
301 }
302
303 ssl->ctx->keylog_callback(ssl, (const char *)out);
304 OPENSSL_free(out);
305 return 1;
306}
307
308int ssl3_can_false_start(const SSL *ssl) {
309 const SSL_CIPHER *const cipher = SSL_get_current_cipher(ssl);
310
311 /* False Start only for TLS 1.2 with an ECDHE+AEAD cipher and ALPN or NPN. */
312 return !SSL_is_dtls(ssl) &&
313 SSL_version(ssl) == TLS1_2_VERSION &&
314 (ssl->s3->alpn_selected != NULL ||
315 ssl->s3->next_proto_negotiated != NULL) &&
316 cipher != NULL &&
317 cipher->algorithm_mkey == SSL_kECDHE &&
318 cipher->algorithm_mac == SSL_AEAD;
319}
320
321void ssl_do_info_callback(const SSL *ssl, int type, int value) {
322 void (*cb)(const SSL *ssl, int type, int value) = NULL;
323 if (ssl->info_callback != NULL) {
324 cb = ssl->info_callback;
325 } else if (ssl->ctx->info_callback != NULL) {
326 cb = ssl->ctx->info_callback;
327 }
328
329 if (cb != NULL) {
330 cb(ssl, type, value);
331 }
332}
333
334void ssl_do_msg_callback(SSL *ssl, int is_write, int content_type,
335 const void *buf, size_t len) {
336 if (ssl->msg_callback == NULL) {
337 return;
338 }
339
340 /* |version| is zero when calling for |SSL3_RT_HEADER| and |SSL2_VERSION| for
341 * a V2ClientHello. */
342 int version;
343 switch (content_type) {
344 case 0:
345 /* V2ClientHello */
346 version = SSL2_VERSION;
347 break;
348 case SSL3_RT_HEADER:
349 version = 0;
350 break;
351 default:
352 version = SSL_version(ssl);
353 }
354
355 ssl->msg_callback(is_write, version, content_type, buf, len, ssl,
356 ssl->msg_callback_arg);
357}
358
359void ssl_get_current_time(const SSL *ssl, struct OPENSSL_timeval *out_clock) {
360 if (ssl->ctx->current_time_cb != NULL) {
361 /* TODO(davidben): Update current_time_cb to use OPENSSL_timeval. See
362 * https://crbug.com/boringssl/155. */
363 struct timeval clock;
364 ssl->ctx->current_time_cb(ssl, &clock);
365 if (clock.tv_sec < 0) {
366 assert(0);
367 out_clock->tv_sec = 0;
368 out_clock->tv_usec = 0;
369 } else {
370 out_clock->tv_sec = (uint64_t)clock.tv_sec;
371 out_clock->tv_usec = (uint32_t)clock.tv_usec;
372 }
373 return;
374 }
375
376#if defined(BORINGSSL_UNSAFE_DETERMINISTIC_MODE)
377 out_clock->tv_sec = 1234;
378 out_clock->tv_usec = 1234;
379#elif defined(OPENSSL_WINDOWS)
380 struct _timeb time;
381 _ftime(&time);
382 if (time.time < 0) {
383 assert(0);
384 out_clock->tv_sec = 0;
385 out_clock->tv_usec = 0;
386 } else {
387 out_clock->tv_sec = time.time;
388 out_clock->tv_usec = time.millitm * 1000;
389 }
390#else
391 struct timeval clock;
392 gettimeofday(&clock, NULL);
393 if (clock.tv_sec < 0) {
394 assert(0);
395 out_clock->tv_sec = 0;
396 out_clock->tv_usec = 0;
397 } else {
398 out_clock->tv_sec = (uint64_t)clock.tv_sec;
399 out_clock->tv_usec = (uint32_t)clock.tv_usec;
400 }
401#endif
402}
403
404} // namespace bssl
405
406using namespace bssl;
407
David Benjamin7c6d09b2015-09-05 11:00:19 -0400408int SSL_library_init(void) {
409 CRYPTO_library_init();
410 return 1;
411}
412
Adam Langleye976e432016-02-15 14:01:10 -0800413static uint32_t ssl_session_hash(const SSL_SESSION *sess) {
414 const uint8_t *session_id = sess->session_id;
415
416 uint8_t tmp_storage[sizeof(uint32_t)];
417 if (sess->session_id_length < sizeof(tmp_storage)) {
David Benjamin17cf2cb2016-12-13 01:07:13 -0500418 OPENSSL_memset(tmp_storage, 0, sizeof(tmp_storage));
419 OPENSSL_memcpy(tmp_storage, sess->session_id, sess->session_id_length);
Adam Langleye976e432016-02-15 14:01:10 -0800420 session_id = tmp_storage;
421 }
422
David Benjamin27bbae42015-09-13 00:54:37 -0400423 uint32_t hash =
Adam Langleye976e432016-02-15 14:01:10 -0800424 ((uint32_t)session_id[0]) |
425 ((uint32_t)session_id[1] << 8) |
426 ((uint32_t)session_id[2] << 16) |
427 ((uint32_t)session_id[3] << 24);
David Benjamin7c6d09b2015-09-05 11:00:19 -0400428
David Benjamin27bbae42015-09-13 00:54:37 -0400429 return hash;
430}
431
432/* NB: If this function (or indeed the hash function which uses a sort of
433 * coarser function than this one) is changed, ensure
434 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
435 * able to construct an SSL_SESSION that will collide with any existing session
436 * with a matching session ID. */
437static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
438 if (a->ssl_version != b->ssl_version) {
439 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800440 }
Adam Langley95c29f32014-06-20 12:00:00 -0700441
David Benjamin27bbae42015-09-13 00:54:37 -0400442 if (a->session_id_length != b->session_id_length) {
443 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800444 }
Adam Langley95c29f32014-06-20 12:00:00 -0700445
David Benjamin17cf2cb2016-12-13 01:07:13 -0500446 return OPENSSL_memcmp(a->session_id, b->session_id, a->session_id_length);
David Benjamin27bbae42015-09-13 00:54:37 -0400447}
Adam Langley95c29f32014-06-20 12:00:00 -0700448
David Benjamin27bbae42015-09-13 00:54:37 -0400449SSL_CTX *SSL_CTX_new(const SSL_METHOD *method) {
450 SSL_CTX *ret = NULL;
451
452 if (method == NULL) {
453 OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_METHOD_PASSED);
454 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800455 }
Adam Langley95c29f32014-06-20 12:00:00 -0700456
David Benjaminf5260812017-07-12 17:16:39 -0400457 ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
David Benjamin27bbae42015-09-13 00:54:37 -0400458 if (ret == NULL) {
459 goto err;
David Benjamin62fd1622015-01-11 13:30:01 -0500460 }
461
David Benjamin17cf2cb2016-12-13 01:07:13 -0500462 OPENSSL_memset(ret, 0, sizeof(SSL_CTX));
Adam Langley95c29f32014-06-20 12:00:00 -0700463
David Benjamin27bbae42015-09-13 00:54:37 -0400464 ret->method = method->method;
Adam Langley3509dac2017-02-01 11:59:18 -0800465 ret->x509_method = method->x509_method;
David Benjamin27bbae42015-09-13 00:54:37 -0400466
467 CRYPTO_MUTEX_init(&ret->lock);
468
469 ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
470 ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
471
David Benjamin27bbae42015-09-13 00:54:37 -0400472 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
David Benjamin17b30832017-01-28 14:00:32 -0500473 ret->session_psk_dhe_timeout = SSL_DEFAULT_SESSION_PSK_DHE_TIMEOUT;
David Benjamin27bbae42015-09-13 00:54:37 -0400474
475 ret->references = 1;
476
477 ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
478 ret->verify_mode = SSL_VERIFY_NONE;
Adam Langley3509dac2017-02-01 11:59:18 -0800479 ret->cert = ssl_cert_new(method->x509_method);
David Benjamin27bbae42015-09-13 00:54:37 -0400480 if (ret->cert == NULL) {
481 goto err;
482 }
483
484 ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
485 if (ret->sessions == NULL) {
486 goto err;
487 }
Adam Langley2a3b3432017-02-10 13:56:48 -0800488
489 if (!ret->x509_method->ssl_ctx_new(ret)) {
David Benjamin27bbae42015-09-13 00:54:37 -0400490 goto err;
491 }
492
Matthew Braithwaite6ad20dc2017-02-21 16:41:33 -0800493 if (!SSL_CTX_set_strict_cipher_list(ret, SSL_DEFAULT_CIPHER_LIST)) {
David Benjamin27bbae42015-09-13 00:54:37 -0400494 goto err2;
495 }
496
Adam Langley34b4c822017-02-02 10:57:17 -0800497 ret->client_CA = sk_CRYPTO_BUFFER_new_null();
David Benjamin27bbae42015-09-13 00:54:37 -0400498 if (ret->client_CA == NULL) {
499 goto err;
500 }
501
David Benjamin8a589332015-12-04 23:14:35 -0500502 CRYPTO_new_ex_data(&ret->ex_data);
David Benjamin27bbae42015-09-13 00:54:37 -0400503
504 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
505
506 /* Setup RFC4507 ticket keys */
507 if (!RAND_bytes(ret->tlsext_tick_key_name, 16) ||
508 !RAND_bytes(ret->tlsext_tick_hmac_key, 16) ||
509 !RAND_bytes(ret->tlsext_tick_aes_key, 16)) {
510 ret->options |= SSL_OP_NO_TICKET;
511 }
512
David Benjamina983b4c2016-11-09 14:21:12 -0500513 /* Disable the auto-chaining feature by default. Once this has stuck without
514 * problems, the feature will be removed entirely. */
515 ret->mode = SSL_MODE_NO_AUTO_CHAIN;
516
David Benjamin27bbae42015-09-13 00:54:37 -0400517 /* Lock the SSL_CTX to the specified version, for compatibility with legacy
David Benjamin3cfeb952017-03-01 16:48:38 -0500518 * uses of SSL_METHOD, but we do not set the minimum version for
519 * |SSLv3_method|. */
David Benjamine34bcc92016-09-21 16:53:09 -0400520 if (!SSL_CTX_set_max_proto_version(ret, method->version) ||
David Benjamin3cfeb952017-03-01 16:48:38 -0500521 !SSL_CTX_set_min_proto_version(ret, method->version == SSL3_VERSION
522 ? 0 /* default */
523 : method->version)) {
David Benjamine34bcc92016-09-21 16:53:09 -0400524 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
525 goto err2;
David Benjamin27bbae42015-09-13 00:54:37 -0400526 }
527
528 return ret;
529
530err:
531 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
532err2:
533 SSL_CTX_free(ret);
534 return NULL;
535}
536
Adam Langley84cd1592016-06-30 18:29:44 -0700537int SSL_CTX_up_ref(SSL_CTX *ctx) {
538 CRYPTO_refcount_inc(&ctx->references);
539 return 1;
540}
541
David Benjamin27bbae42015-09-13 00:54:37 -0400542void SSL_CTX_free(SSL_CTX *ctx) {
543 if (ctx == NULL ||
544 !CRYPTO_refcount_dec_and_test_zero(&ctx->references)) {
545 return;
546 }
547
David Benjamin27bbae42015-09-13 00:54:37 -0400548 /* Free internal session cache. However: the remove_cb() may reference the
549 * ex_data of SSL_CTX, thus the ex_data store can only be removed after the
550 * sessions were flushed. As the ex_data handling routines might also touch
551 * the session cache, the most secure solution seems to be: empty (flush) the
552 * cache, then free ex_data, then finally free the cache. (See ticket
553 * [openssl.org #212].) */
554 SSL_CTX_flush_sessions(ctx, 0);
555
556 CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, ctx, &ctx->ex_data);
557
558 CRYPTO_MUTEX_cleanup(&ctx->lock);
559 lh_SSL_SESSION_free(ctx->sessions);
David Benjamin27bbae42015-09-13 00:54:37 -0400560 ssl_cipher_preference_list_free(ctx->cipher_list);
David Benjamin27bbae42015-09-13 00:54:37 -0400561 ssl_cert_free(ctx->cert);
562 sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->client_custom_extensions,
563 SSL_CUSTOM_EXTENSION_free);
564 sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->server_custom_extensions,
565 SSL_CUSTOM_EXTENSION_free);
Adam Langley34b4c822017-02-02 10:57:17 -0800566 sk_CRYPTO_BUFFER_pop_free(ctx->client_CA, CRYPTO_BUFFER_free);
Adam Langley2a3b3432017-02-10 13:56:48 -0800567 ctx->x509_method->ssl_ctx_free(ctx);
David Benjamin27bbae42015-09-13 00:54:37 -0400568 sk_SRTP_PROTECTION_PROFILE_free(ctx->srtp_profiles);
569 OPENSSL_free(ctx->psk_identity_hint);
Steven Valdezce902a92016-05-17 11:47:53 -0400570 OPENSSL_free(ctx->supported_group_list);
David Benjamin27bbae42015-09-13 00:54:37 -0400571 OPENSSL_free(ctx->alpn_client_proto_list);
David Benjamin27bbae42015-09-13 00:54:37 -0400572 EVP_PKEY_free(ctx->tlsext_channel_id_private);
David Benjamin71c21b42017-04-14 17:05:40 -0400573 OPENSSL_free(ctx->verify_sigalgs);
David Benjamin27bbae42015-09-13 00:54:37 -0400574
575 OPENSSL_free(ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -0800576}
Adam Langley95c29f32014-06-20 12:00:00 -0700577
Adam Langleyfcf25832014-12-18 17:42:32 -0800578SSL *SSL_new(SSL_CTX *ctx) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800579 if (ctx == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400580 OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_CTX);
Adam Langleyfcf25832014-12-18 17:42:32 -0800581 return NULL;
582 }
583 if (ctx->method == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400584 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
Adam Langleyfcf25832014-12-18 17:42:32 -0800585 return NULL;
586 }
Adam Langley95c29f32014-06-20 12:00:00 -0700587
David Benjaminf5260812017-07-12 17:16:39 -0400588 SSL *ssl = (SSL *)OPENSSL_malloc(sizeof(SSL));
David Benjamin0d56f882015-12-19 17:05:56 -0500589 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800590 goto err;
591 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500592 OPENSSL_memset(ssl, 0, sizeof(SSL));
Adam Langley95c29f32014-06-20 12:00:00 -0700593
David Benjaminfc08dfc2017-06-20 14:39:32 -0400594 ssl->conf_min_version = ctx->conf_min_version;
595 ssl->conf_max_version = ctx->conf_max_version;
Steven Valdez52586f92017-07-11 15:08:32 -0400596 ssl->tls13_variant = ctx->tls13_variant;
David Benjamin1eb367c2014-12-12 18:17:51 -0500597
Taylor Brandstetter376a0fe2016-05-10 19:30:28 -0700598 /* RFC 6347 states that implementations SHOULD use an initial timer value of
599 * 1 second. */
600 ssl->initial_timeout_duration_ms = 1000;
601
David Benjamin0d56f882015-12-19 17:05:56 -0500602 ssl->options = ctx->options;
603 ssl->mode = ctx->mode;
604 ssl->max_cert_list = ctx->max_cert_list;
Adam Langley95c29f32014-06-20 12:00:00 -0700605
David Benjamin0d56f882015-12-19 17:05:56 -0500606 ssl->cert = ssl_cert_dup(ctx->cert);
607 if (ssl->cert == NULL) {
David Benjamina5a3eeb2015-03-18 20:26:30 -0400608 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -0800609 }
Adam Langley95c29f32014-06-20 12:00:00 -0700610
David Benjamin0d56f882015-12-19 17:05:56 -0500611 ssl->msg_callback = ctx->msg_callback;
612 ssl->msg_callback_arg = ctx->msg_callback_arg;
613 ssl->verify_mode = ctx->verify_mode;
David Benjamin0d56f882015-12-19 17:05:56 -0500614 ssl->verify_callback = ctx->default_verify_callback;
David Benjamin3a1dd462017-07-11 16:13:10 -0400615 ssl->custom_verify_callback = ctx->custom_verify_callback;
David Benjaminbbaf3672016-11-17 10:53:09 +0900616 ssl->retain_only_sha256_of_client_certs =
617 ctx->retain_only_sha256_of_client_certs;
Adam Langley95c29f32014-06-20 12:00:00 -0700618
David Benjamin0d56f882015-12-19 17:05:56 -0500619 ssl->quiet_shutdown = ctx->quiet_shutdown;
620 ssl->max_send_fragment = ctx->max_send_fragment;
Adam Langley95c29f32014-06-20 12:00:00 -0700621
David Benjaminf650c712016-12-22 15:54:51 -0500622 SSL_CTX_up_ref(ctx);
David Benjamin0d56f882015-12-19 17:05:56 -0500623 ssl->ctx = ctx;
David Benjaminf650c712016-12-22 15:54:51 -0500624 SSL_CTX_up_ref(ctx);
David Benjaminbe497062017-03-10 16:08:36 -0500625 ssl->session_ctx = ctx;
Adam Langley95c29f32014-06-20 12:00:00 -0700626
Adam Langley2a3b3432017-02-10 13:56:48 -0800627 if (!ssl->ctx->x509_method->ssl_new(ssl)) {
628 goto err;
629 }
630
Steven Valdezce902a92016-05-17 11:47:53 -0400631 if (ctx->supported_group_list) {
David Benjaminf5260812017-07-12 17:16:39 -0400632 ssl->supported_group_list = (uint16_t *)BUF_memdup(
633 ctx->supported_group_list, ctx->supported_group_list_len * 2);
Steven Valdezce902a92016-05-17 11:47:53 -0400634 if (!ssl->supported_group_list) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800635 goto err;
636 }
Steven Valdezce902a92016-05-17 11:47:53 -0400637 ssl->supported_group_list_len = ctx->supported_group_list_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800638 }
Adam Langley95c29f32014-06-20 12:00:00 -0700639
David Benjamin17b30832017-01-28 14:00:32 -0500640 if (ctx->alpn_client_proto_list) {
David Benjaminf5260812017-07-12 17:16:39 -0400641 ssl->alpn_client_proto_list = (uint8_t *)BUF_memdup(
642 ctx->alpn_client_proto_list, ctx->alpn_client_proto_list_len);
David Benjamin0d56f882015-12-19 17:05:56 -0500643 if (ssl->alpn_client_proto_list == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800644 goto err;
645 }
David Benjamin17b30832017-01-28 14:00:32 -0500646 ssl->alpn_client_proto_list_len = ctx->alpn_client_proto_list_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800647 }
Adam Langley95c29f32014-06-20 12:00:00 -0700648
David Benjamin0d56f882015-12-19 17:05:56 -0500649 ssl->method = ctx->method;
Adam Langley95c29f32014-06-20 12:00:00 -0700650
David Benjamin0d56f882015-12-19 17:05:56 -0500651 if (!ssl->method->ssl_new(ssl)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800652 goto err;
653 }
Adam Langley95c29f32014-06-20 12:00:00 -0700654
David Benjamin0d56f882015-12-19 17:05:56 -0500655 ssl->rwstate = SSL_NOTHING;
Adam Langley95c29f32014-06-20 12:00:00 -0700656
David Benjamin0d56f882015-12-19 17:05:56 -0500657 CRYPTO_new_ex_data(&ssl->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700658
David Benjamin0d56f882015-12-19 17:05:56 -0500659 ssl->psk_identity_hint = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800660 if (ctx->psk_identity_hint) {
David Benjamin0d56f882015-12-19 17:05:56 -0500661 ssl->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
662 if (ssl->psk_identity_hint == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800663 goto err;
664 }
665 }
David Benjamin0d56f882015-12-19 17:05:56 -0500666 ssl->psk_client_callback = ctx->psk_client_callback;
667 ssl->psk_server_callback = ctx->psk_server_callback;
Adam Langley95c29f32014-06-20 12:00:00 -0700668
David Benjamin0d56f882015-12-19 17:05:56 -0500669 ssl->tlsext_channel_id_enabled = ctx->tlsext_channel_id_enabled;
David Benjamin02ddbfd2015-01-11 13:09:11 -0500670 if (ctx->tlsext_channel_id_private) {
Adam Langley310d3f62016-07-12 10:39:20 -0700671 EVP_PKEY_up_ref(ctx->tlsext_channel_id_private);
672 ssl->tlsext_channel_id_private = ctx->tlsext_channel_id_private;
David Benjamin02ddbfd2015-01-11 13:09:11 -0500673 }
674
David Benjamin17b30832017-01-28 14:00:32 -0500675 ssl->signed_cert_timestamps_enabled = ctx->signed_cert_timestamps_enabled;
676 ssl->ocsp_stapling_enabled = ctx->ocsp_stapling_enabled;
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200677
David Benjamin0d56f882015-12-19 17:05:56 -0500678 return ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -0800679
Adam Langley95c29f32014-06-20 12:00:00 -0700680err:
David Benjamin0d56f882015-12-19 17:05:56 -0500681 SSL_free(ssl);
David Benjamin3570d732015-06-29 00:28:17 -0400682 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700683
Adam Langleyfcf25832014-12-18 17:42:32 -0800684 return NULL;
685}
Adam Langley95c29f32014-06-20 12:00:00 -0700686
David Benjamin27bbae42015-09-13 00:54:37 -0400687void SSL_free(SSL *ssl) {
688 if (ssl == NULL) {
689 return;
690 }
691
David Benjamin0391f162017-06-15 15:27:59 -0400692 if (ssl->ctx != NULL) {
693 ssl->ctx->x509_method->ssl_free(ssl);
694 }
695
David Benjamin27bbae42015-09-13 00:54:37 -0400696 CRYPTO_free_ex_data(&g_ex_data_class_ssl, ssl, &ssl->ex_data);
697
David Benjamin27bbae42015-09-13 00:54:37 -0400698 BIO_free_all(ssl->rbio);
David Benjaminf715c422016-06-11 19:01:56 -0400699 BIO_free_all(ssl->wbio);
David Benjamin27bbae42015-09-13 00:54:37 -0400700
701 BUF_MEM_free(ssl->init_buf);
702
703 /* add extra stuff */
704 ssl_cipher_preference_list_free(ssl->cipher_list);
David Benjamin27bbae42015-09-13 00:54:37 -0400705
David Benjamin27bbae42015-09-13 00:54:37 -0400706 SSL_SESSION_free(ssl->session);
707
David Benjamin27bbae42015-09-13 00:54:37 -0400708 ssl_cert_free(ssl->cert);
709
710 OPENSSL_free(ssl->tlsext_hostname);
David Benjaminbe497062017-03-10 16:08:36 -0500711 SSL_CTX_free(ssl->session_ctx);
Steven Valdezce902a92016-05-17 11:47:53 -0400712 OPENSSL_free(ssl->supported_group_list);
David Benjamin27bbae42015-09-13 00:54:37 -0400713 OPENSSL_free(ssl->alpn_client_proto_list);
714 EVP_PKEY_free(ssl->tlsext_channel_id_private);
715 OPENSSL_free(ssl->psk_identity_hint);
Adam Langley34b4c822017-02-02 10:57:17 -0800716 sk_CRYPTO_BUFFER_pop_free(ssl->client_CA, CRYPTO_BUFFER_free);
David Benjamin27bbae42015-09-13 00:54:37 -0400717 sk_SRTP_PROTECTION_PROFILE_free(ssl->srtp_profiles);
718
719 if (ssl->method != NULL) {
720 ssl->method->ssl_free(ssl);
721 }
722 SSL_CTX_free(ssl->ctx);
723
724 OPENSSL_free(ssl);
725}
726
727void SSL_set_connect_state(SSL *ssl) {
728 ssl->server = 0;
David Benjamin9f1dc822016-06-07 17:03:46 -0400729 ssl->handshake_func = ssl3_connect;
David Benjamin27bbae42015-09-13 00:54:37 -0400730}
731
732void SSL_set_accept_state(SSL *ssl) {
733 ssl->server = 1;
David Benjamin9f1dc822016-06-07 17:03:46 -0400734 ssl->handshake_func = ssl3_accept;
David Benjamin27bbae42015-09-13 00:54:37 -0400735}
736
David Benjamin4501bd52016-08-01 13:39:41 -0400737void SSL_set0_rbio(SSL *ssl, BIO *rbio) {
David Benjaminf715c422016-06-11 19:01:56 -0400738 BIO_free_all(ssl->rbio);
739 ssl->rbio = rbio;
740}
741
David Benjamin4501bd52016-08-01 13:39:41 -0400742void SSL_set0_wbio(SSL *ssl, BIO *wbio) {
David Benjaminf715c422016-06-11 19:01:56 -0400743 BIO_free_all(ssl->wbio);
David Benjamin27bbae42015-09-13 00:54:37 -0400744 ssl->wbio = wbio;
745}
746
David Benjaminf715c422016-06-11 19:01:56 -0400747void SSL_set_bio(SSL *ssl, BIO *rbio, BIO *wbio) {
748 /* For historical reasons, this function has many different cases in ownership
749 * handling. */
750
David Benjamin4501bd52016-08-01 13:39:41 -0400751 /* If nothing has changed, do nothing */
752 if (rbio == SSL_get_rbio(ssl) && wbio == SSL_get_wbio(ssl)) {
753 return;
754 }
755
David Benjaminf715c422016-06-11 19:01:56 -0400756 /* If the two arguments are equal, one fewer reference is granted than
757 * taken. */
758 if (rbio != NULL && rbio == wbio) {
759 BIO_up_ref(rbio);
760 }
761
David Benjamin4501bd52016-08-01 13:39:41 -0400762 /* If only the wbio is changed, adopt only one reference. */
David Benjaminf715c422016-06-11 19:01:56 -0400763 if (rbio == SSL_get_rbio(ssl)) {
David Benjamin4501bd52016-08-01 13:39:41 -0400764 SSL_set0_wbio(ssl, wbio);
David Benjaminf715c422016-06-11 19:01:56 -0400765 return;
766 }
David Benjamin4501bd52016-08-01 13:39:41 -0400767
768 /* There is an asymmetry here for historical reasons. If only the rbio is
769 * changed AND the rbio and wbio were originally different, then we only adopt
770 * one reference. */
771 if (wbio == SSL_get_wbio(ssl) && SSL_get_rbio(ssl) != SSL_get_wbio(ssl)) {
772 SSL_set0_rbio(ssl, rbio);
David Benjaminf715c422016-06-11 19:01:56 -0400773 return;
774 }
775
776 /* Otherwise, adopt both references. */
David Benjamin4501bd52016-08-01 13:39:41 -0400777 SSL_set0_rbio(ssl, rbio);
778 SSL_set0_wbio(ssl, wbio);
David Benjaminf715c422016-06-11 19:01:56 -0400779}
780
David Benjamin27bbae42015-09-13 00:54:37 -0400781BIO *SSL_get_rbio(const SSL *ssl) { return ssl->rbio; }
782
David Benjamindaf207a2017-01-03 18:37:41 -0500783BIO *SSL_get_wbio(const SSL *ssl) { return ssl->wbio; }
David Benjamin27bbae42015-09-13 00:54:37 -0400784
David Benjamin2be4aa72017-01-02 07:56:51 -0500785int SSL_do_handshake(SSL *ssl) {
786 ssl_reset_error_state(ssl);
David Benjamin15c14882016-03-14 14:25:46 -0400787
David Benjamin27bbae42015-09-13 00:54:37 -0400788 if (ssl->handshake_func == NULL) {
789 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_TYPE_NOT_SET);
790 return -1;
791 }
792
793 if (!SSL_in_init(ssl)) {
794 return 1;
795 }
796
David Benjamin2644a132016-12-11 13:41:17 -0500797 if (ssl->s3->hs == NULL) {
798 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
799 return -1;
David Benjamince8c9d22016-11-14 10:45:16 +0900800 }
801
802 /* Run the handshake. */
803 assert(ssl->s3->hs != NULL);
804 int ret = ssl->handshake_func(ssl->s3->hs);
805 if (ret <= 0) {
806 return ret;
807 }
808
809 /* Destroy the handshake object if the handshake has completely finished. */
810 if (!SSL_in_init(ssl)) {
811 ssl_handshake_free(ssl->s3->hs);
812 ssl->s3->hs = NULL;
813 }
814
815 return 1;
David Benjamin27bbae42015-09-13 00:54:37 -0400816}
817
818int SSL_connect(SSL *ssl) {
David Benjaminb2a73182016-03-10 01:25:26 -0500819 if (ssl->handshake_func == NULL) {
David Benjamin27bbae42015-09-13 00:54:37 -0400820 /* Not properly initialized yet */
821 SSL_set_connect_state(ssl);
822 }
823
David Benjamin15c14882016-03-14 14:25:46 -0400824 return SSL_do_handshake(ssl);
David Benjamin27bbae42015-09-13 00:54:37 -0400825}
826
827int SSL_accept(SSL *ssl) {
David Benjaminb2a73182016-03-10 01:25:26 -0500828 if (ssl->handshake_func == NULL) {
David Benjamin27bbae42015-09-13 00:54:37 -0400829 /* Not properly initialized yet */
830 SSL_set_accept_state(ssl);
831 }
832
David Benjamin15c14882016-03-14 14:25:46 -0400833 return SSL_do_handshake(ssl);
David Benjamin27bbae42015-09-13 00:54:37 -0400834}
835
David Benjamin7934f082017-08-01 16:32:25 -0400836static int ssl_do_post_handshake(SSL *ssl, const SSLMessage &msg) {
837 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
838 return tls13_post_handshake(ssl, msg);
839 }
840
David Benjamin34941c02016-10-08 11:45:31 -0400841 /* We do not accept renegotiations as a server or SSL 3.0. SSL 3.0 will be
842 * removed entirely in the future and requires retaining more data for
843 * renegotiation_info. */
844 if (ssl->server || ssl->version == SSL3_VERSION) {
David Benjamin163f29a2016-07-28 11:05:58 -0400845 goto no_renegotiation;
846 }
847
David Benjamin7934f082017-08-01 16:32:25 -0400848 if (msg.type != SSL3_MT_HELLO_REQUEST || CBS_len(&msg.body) != 0) {
David Benjamin163f29a2016-07-28 11:05:58 -0400849 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
850 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HELLO_REQUEST);
851 return 0;
852 }
853
854 switch (ssl->renegotiate_mode) {
855 case ssl_renegotiate_ignore:
856 /* Ignore the HelloRequest. */
857 return 1;
858
859 case ssl_renegotiate_once:
860 if (ssl->s3->total_renegotiations != 0) {
861 goto no_renegotiation;
862 }
863 break;
864
865 case ssl_renegotiate_never:
866 goto no_renegotiation;
867
868 case ssl_renegotiate_freely:
869 break;
870 }
871
872 /* Renegotiation is only supported at quiescent points in the application
873 * protocol, namely in HTTPS, just before reading the HTTP response. Require
874 * the record-layer be idle and avoid complexities of sending a handshake
875 * record while an application_data record is being written. */
876 if (ssl_write_buffer_is_pending(ssl)) {
877 goto no_renegotiation;
878 }
879
880 /* Begin a new handshake. */
David Benjamin2644a132016-12-11 13:41:17 -0500881 if (ssl->s3->hs != NULL) {
882 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
883 return 0;
884 }
885 ssl->s3->hs = ssl_handshake_new(ssl);
886 if (ssl->s3->hs == NULL) {
887 return 0;
888 }
889
David Benjamin163f29a2016-07-28 11:05:58 -0400890 ssl->s3->total_renegotiations++;
David Benjamin163f29a2016-07-28 11:05:58 -0400891 return 1;
892
893no_renegotiation:
894 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_NO_RENEGOTIATION);
895 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION);
896 return 0;
897}
898
David Benjamin15c14882016-03-14 14:25:46 -0400899static int ssl_read_impl(SSL *ssl, void *buf, int num, int peek) {
David Benjamin2be4aa72017-01-02 07:56:51 -0500900 ssl_reset_error_state(ssl);
David Benjamin15c14882016-03-14 14:25:46 -0400901
David Benjaminb2a73182016-03-10 01:25:26 -0500902 if (ssl->handshake_func == NULL) {
David Benjamin27bbae42015-09-13 00:54:37 -0400903 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
904 return -1;
905 }
906
David Benjamin163f29a2016-07-28 11:05:58 -0400907 for (;;) {
908 /* Complete the current handshake, if any. False Start will cause
909 * |SSL_do_handshake| to return mid-handshake, so this may require multiple
910 * iterations. */
Steven Valdez681eb6a2016-12-19 13:19:29 -0500911 while (!ssl_can_read(ssl)) {
David Benjamin163f29a2016-07-28 11:05:58 -0400912 int ret = SSL_do_handshake(ssl);
913 if (ret < 0) {
914 return ret;
915 }
916 if (ret == 0) {
917 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
918 return -1;
919 }
920 }
921
922 int got_handshake;
David Benjaminf5260812017-07-12 17:16:39 -0400923 int ret = ssl->method->read_app_data(ssl, &got_handshake, (uint8_t *)buf,
924 num, peek);
David Benjamin163f29a2016-07-28 11:05:58 -0400925 if (ret > 0 || !got_handshake) {
Steven Valdez32635b82016-08-16 11:25:03 -0400926 ssl->s3->key_update_count = 0;
David Benjaminc79845c2016-03-10 01:28:00 -0500927 return ret;
928 }
David Benjamin163f29a2016-07-28 11:05:58 -0400929
Steven Valdez681eb6a2016-12-19 13:19:29 -0500930 /* If we received an interrupt in early read (the end_of_early_data alert),
931 * loop again for the handshake to process it. */
932 if (SSL_in_init(ssl)) {
933 continue;
934 }
935
David Benjamin7934f082017-08-01 16:32:25 -0400936 SSLMessage msg;
937 while (ssl->method->get_message(ssl, &msg)) {
938 /* Handle the post-handshake message and try again. */
939 if (!ssl_do_post_handshake(ssl, msg)) {
940 return -1;
941 }
942 ssl->method->next_message(ssl);
David Benjaminc79845c2016-03-10 01:28:00 -0500943 }
944 }
David Benjamin15c14882016-03-14 14:25:46 -0400945}
946
947int SSL_read(SSL *ssl, void *buf, int num) {
948 return ssl_read_impl(ssl, buf, num, 0 /* consume bytes */);
David Benjamin27bbae42015-09-13 00:54:37 -0400949}
950
951int SSL_peek(SSL *ssl, void *buf, int num) {
David Benjamin15c14882016-03-14 14:25:46 -0400952 return ssl_read_impl(ssl, buf, num, 1 /* peek */);
David Benjamin27bbae42015-09-13 00:54:37 -0400953}
954
955int SSL_write(SSL *ssl, const void *buf, int num) {
David Benjamin2be4aa72017-01-02 07:56:51 -0500956 ssl_reset_error_state(ssl);
David Benjamin15c14882016-03-14 14:25:46 -0400957
David Benjaminb2a73182016-03-10 01:25:26 -0500958 if (ssl->handshake_func == NULL) {
David Benjamin27bbae42015-09-13 00:54:37 -0400959 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
960 return -1;
961 }
962
David Benjaminfa214e42016-05-10 17:03:10 -0400963 if (ssl->s3->send_shutdown != ssl_shutdown_none) {
David Benjamin27bbae42015-09-13 00:54:37 -0400964 OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
965 return -1;
966 }
967
Steven Valdeze831a812017-03-09 14:56:07 -0500968 int ret = 0, needs_handshake = 0;
969 do {
970 /* If necessary, complete the handshake implicitly. */
971 if (!ssl_can_write(ssl)) {
972 ret = SSL_do_handshake(ssl);
973 if (ret < 0) {
974 return ret;
975 }
976 if (ret == 0) {
977 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
978 return -1;
979 }
David Benjamind7ac1432016-03-10 00:41:25 -0500980 }
David Benjamind7ac1432016-03-10 00:41:25 -0500981
David Benjaminf5260812017-07-12 17:16:39 -0400982 ret = ssl->method->write_app_data(ssl, &needs_handshake,
983 (const uint8_t *)buf, num);
Steven Valdeze831a812017-03-09 14:56:07 -0500984 } while (needs_handshake);
985 return ret;
David Benjamin27bbae42015-09-13 00:54:37 -0400986}
987
988int SSL_shutdown(SSL *ssl) {
David Benjamin2be4aa72017-01-02 07:56:51 -0500989 ssl_reset_error_state(ssl);
David Benjamin15c14882016-03-14 14:25:46 -0400990
David Benjaminb2a73182016-03-10 01:25:26 -0500991 if (ssl->handshake_func == NULL) {
David Benjamin27bbae42015-09-13 00:54:37 -0400992 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
993 return -1;
994 }
995
David Benjamin12709db2017-02-17 13:51:50 -0500996 /* If we are in the middle of a handshake, silently succeed. Consumers often
997 * call this function before |SSL_free|, whether the handshake succeeded or
998 * not. We assume the caller has already handled failed handshakes. */
Steven Valdeza14934f2016-02-29 10:05:08 -0500999 if (SSL_in_init(ssl)) {
David Benjamin12709db2017-02-17 13:51:50 -05001000 return 1;
Steven Valdeza14934f2016-02-29 10:05:08 -05001001 }
1002
David Benjamin27bbae42015-09-13 00:54:37 -04001003 if (ssl->quiet_shutdown) {
David Benjaminfa214e42016-05-10 17:03:10 -04001004 /* Do nothing if configured not to send a close_notify. */
1005 ssl->s3->send_shutdown = ssl_shutdown_close_notify;
1006 ssl->s3->recv_shutdown = ssl_shutdown_close_notify;
David Benjamin27bbae42015-09-13 00:54:37 -04001007 return 1;
1008 }
1009
David Benjaminfa214e42016-05-10 17:03:10 -04001010 /* This function completes in two stages. It sends a close_notify and then it
1011 * waits for a close_notify to come in. Perform exactly one action and return
1012 * whether or not it succeeds. */
David Benjamin27bbae42015-09-13 00:54:37 -04001013
David Benjaminfa214e42016-05-10 17:03:10 -04001014 if (ssl->s3->send_shutdown != ssl_shutdown_close_notify) {
1015 /* Send a close_notify. */
1016 if (ssl3_send_alert(ssl, SSL3_AL_WARNING, SSL_AD_CLOSE_NOTIFY) <= 0) {
1017 return -1;
David Benjamin27bbae42015-09-13 00:54:37 -04001018 }
1019 } else if (ssl->s3->alert_dispatch) {
David Benjaminfa214e42016-05-10 17:03:10 -04001020 /* Finish sending the close_notify. */
David Benjaminf0ee9072016-06-15 17:44:37 -04001021 if (ssl->method->dispatch_alert(ssl) <= 0) {
David Benjaminfa214e42016-05-10 17:03:10 -04001022 return -1;
David Benjamin27bbae42015-09-13 00:54:37 -04001023 }
David Benjaminfa214e42016-05-10 17:03:10 -04001024 } else if (ssl->s3->recv_shutdown != ssl_shutdown_close_notify) {
1025 /* Wait for the peer's close_notify. */
David Benjaminf0ee9072016-06-15 17:44:37 -04001026 ssl->method->read_close_notify(ssl);
David Benjaminfa214e42016-05-10 17:03:10 -04001027 if (ssl->s3->recv_shutdown != ssl_shutdown_close_notify) {
1028 return -1;
David Benjamin27bbae42015-09-13 00:54:37 -04001029 }
1030 }
1031
David Benjaminfa214e42016-05-10 17:03:10 -04001032 /* Return 0 for unidirectional shutdown and 1 for bidirectional shutdown. */
1033 return ssl->s3->recv_shutdown == ssl_shutdown_close_notify;
David Benjamin27bbae42015-09-13 00:54:37 -04001034}
1035
David Benjamin1d4f4c02016-07-26 18:03:08 -04001036int SSL_send_fatal_alert(SSL *ssl, uint8_t alert) {
1037 if (ssl->s3->alert_dispatch) {
1038 if (ssl->s3->send_alert[0] != SSL3_AL_FATAL ||
1039 ssl->s3->send_alert[1] != alert) {
1040 /* We are already attempting to write a different alert. */
1041 OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
1042 return -1;
1043 }
1044 return ssl->method->dispatch_alert(ssl);
1045 }
1046
1047 return ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
1048}
1049
Steven Valdez08b65f42016-12-07 15:29:45 -05001050void SSL_CTX_set_early_data_enabled(SSL_CTX *ctx, int enabled) {
Alessandro Ghedini67bb45f2017-03-30 16:33:24 -05001051 ctx->cert->enable_early_data = !!enabled;
1052}
1053
Steven Valdez520e1222017-06-13 12:45:25 -04001054void SSL_CTX_set_tls13_variant(SSL_CTX *ctx, enum tls13_variant_t variant) {
1055 ctx->tls13_variant = variant;
1056}
1057
Steven Valdez52586f92017-07-11 15:08:32 -04001058void SSL_set_tls13_variant(SSL *ssl, enum tls13_variant_t variant) {
1059 ssl->tls13_variant = variant;
1060}
1061
Alessandro Ghedini67bb45f2017-03-30 16:33:24 -05001062void SSL_set_early_data_enabled(SSL *ssl, int enabled) {
1063 ssl->cert->enable_early_data = !!enabled;
Steven Valdez08b65f42016-12-07 15:29:45 -05001064}
1065
Steven Valdeze831a812017-03-09 14:56:07 -05001066int SSL_in_early_data(const SSL *ssl) {
1067 if (ssl->s3->hs == NULL) {
1068 return 0;
1069 }
1070 return ssl->s3->hs->in_early_data;
1071}
1072
Steven Valdez2d850622017-01-11 11:34:52 -05001073int SSL_early_data_accepted(const SSL *ssl) {
1074 return ssl->early_data_accepted;
1075}
1076
Steven Valdeze831a812017-03-09 14:56:07 -05001077void SSL_reset_early_data_reject(SSL *ssl) {
1078 SSL_HANDSHAKE *hs = ssl->s3->hs;
1079 if (hs == NULL ||
1080 hs->wait != ssl_hs_early_data_rejected) {
1081 abort();
1082 }
1083
1084 hs->wait = ssl_hs_ok;
1085 hs->in_early_data = 0;
David Benjamin31b0c9b2017-07-20 14:49:15 -04001086 hs->early_session.reset();
Steven Valdeze831a812017-03-09 14:56:07 -05001087
1088 /* Discard any unfinished writes from the perspective of |SSL_write|'s
1089 * retry. The handshake will transparently flush out the pending record
1090 * (discarded by the server) to keep the framing correct. */
1091 ssl->s3->wpend_pending = 0;
1092}
1093
David Benjamin287fc4f2016-12-10 15:12:03 -05001094static int bio_retry_reason_to_error(int reason) {
1095 switch (reason) {
1096 case BIO_RR_CONNECT:
1097 return SSL_ERROR_WANT_CONNECT;
1098 case BIO_RR_ACCEPT:
1099 return SSL_ERROR_WANT_ACCEPT;
1100 default:
1101 return SSL_ERROR_SYSCALL;
1102 }
1103}
David Benjamin27bbae42015-09-13 00:54:37 -04001104
David Benjamin287fc4f2016-12-10 15:12:03 -05001105int SSL_get_error(const SSL *ssl, int ret_code) {
David Benjamin27bbae42015-09-13 00:54:37 -04001106 if (ret_code > 0) {
1107 return SSL_ERROR_NONE;
1108 }
1109
1110 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
1111 * where we do encode the error */
David Benjamin287fc4f2016-12-10 15:12:03 -05001112 uint32_t err = ERR_peek_error();
David Benjamin27bbae42015-09-13 00:54:37 -04001113 if (err != 0) {
1114 if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
1115 return SSL_ERROR_SYSCALL;
1116 }
1117 return SSL_ERROR_SSL;
1118 }
1119
1120 if (ret_code == 0) {
David Benjaminfa214e42016-05-10 17:03:10 -04001121 if (ssl->s3->recv_shutdown == ssl_shutdown_close_notify) {
David Benjamin27bbae42015-09-13 00:54:37 -04001122 return SSL_ERROR_ZERO_RETURN;
1123 }
1124 /* An EOF was observed which violates the protocol, and the underlying
1125 * transport does not participate in the error queue. Bubble up to the
1126 * caller. */
1127 return SSL_ERROR_SYSCALL;
1128 }
1129
David Benjamin287fc4f2016-12-10 15:12:03 -05001130 switch (ssl->rwstate) {
1131 case SSL_PENDING_SESSION:
1132 return SSL_ERROR_PENDING_SESSION;
David Benjamin27bbae42015-09-13 00:54:37 -04001133
David Benjamin287fc4f2016-12-10 15:12:03 -05001134 case SSL_CERTIFICATE_SELECTION_PENDING:
1135 return SSL_ERROR_PENDING_CERTIFICATE;
David Benjamin27bbae42015-09-13 00:54:37 -04001136
David Benjamin287fc4f2016-12-10 15:12:03 -05001137 case SSL_READING: {
1138 BIO *bio = SSL_get_rbio(ssl);
1139 if (BIO_should_read(bio)) {
1140 return SSL_ERROR_WANT_READ;
David Benjamin27bbae42015-09-13 00:54:37 -04001141 }
1142
David Benjamin287fc4f2016-12-10 15:12:03 -05001143 if (BIO_should_write(bio)) {
1144 /* TODO(davidben): OpenSSL historically checked for writes on the read
1145 * BIO. Can this be removed? */
1146 return SSL_ERROR_WANT_WRITE;
David Benjamin27bbae42015-09-13 00:54:37 -04001147 }
1148
David Benjamin287fc4f2016-12-10 15:12:03 -05001149 if (BIO_should_io_special(bio)) {
1150 return bio_retry_reason_to_error(BIO_get_retry_reason(bio));
David Benjamin27bbae42015-09-13 00:54:37 -04001151 }
1152
David Benjamin287fc4f2016-12-10 15:12:03 -05001153 break;
1154 }
1155
1156 case SSL_WRITING: {
1157 BIO *bio = SSL_get_wbio(ssl);
1158 if (BIO_should_write(bio)) {
1159 return SSL_ERROR_WANT_WRITE;
David Benjamin27bbae42015-09-13 00:54:37 -04001160 }
1161
David Benjamin287fc4f2016-12-10 15:12:03 -05001162 if (BIO_should_read(bio)) {
1163 /* TODO(davidben): OpenSSL historically checked for reads on the write
1164 * BIO. Can this be removed? */
1165 return SSL_ERROR_WANT_READ;
1166 }
1167
1168 if (BIO_should_io_special(bio)) {
1169 return bio_retry_reason_to_error(BIO_get_retry_reason(bio));
1170 }
1171
1172 break;
David Benjamin27bbae42015-09-13 00:54:37 -04001173 }
David Benjamin27bbae42015-09-13 00:54:37 -04001174
David Benjamin287fc4f2016-12-10 15:12:03 -05001175 case SSL_X509_LOOKUP:
1176 return SSL_ERROR_WANT_X509_LOOKUP;
David Benjamin27bbae42015-09-13 00:54:37 -04001177
David Benjamin287fc4f2016-12-10 15:12:03 -05001178 case SSL_CHANNEL_ID_LOOKUP:
1179 return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
David Benjamin27bbae42015-09-13 00:54:37 -04001180
David Benjamin287fc4f2016-12-10 15:12:03 -05001181 case SSL_PRIVATE_KEY_OPERATION:
1182 return SSL_ERROR_WANT_PRIVATE_KEY_OPERATION;
Adam Langley4c341d02017-03-08 19:33:21 -08001183
1184 case SSL_PENDING_TICKET:
1185 return SSL_ERROR_PENDING_TICKET;
Steven Valdeze831a812017-03-09 14:56:07 -05001186
1187 case SSL_EARLY_DATA_REJECTED:
1188 return SSL_ERROR_EARLY_DATA_REJECTED;
David Benjamin3a1dd462017-07-11 16:13:10 -04001189
1190 case SSL_CERTIFICATE_VERIFY:
1191 return SSL_ERROR_WANT_CERTIFICATE_VERIFY;
David Benjamin27bbae42015-09-13 00:54:37 -04001192 }
1193
1194 return SSL_ERROR_SYSCALL;
1195}
1196
David Benjamin27bbae42015-09-13 00:54:37 -04001197uint32_t SSL_CTX_set_options(SSL_CTX *ctx, uint32_t options) {
1198 ctx->options |= options;
1199 return ctx->options;
1200}
1201
1202uint32_t SSL_CTX_clear_options(SSL_CTX *ctx, uint32_t options) {
1203 ctx->options &= ~options;
1204 return ctx->options;
1205}
1206
1207uint32_t SSL_CTX_get_options(const SSL_CTX *ctx) { return ctx->options; }
1208
1209uint32_t SSL_set_options(SSL *ssl, uint32_t options) {
1210 ssl->options |= options;
1211 return ssl->options;
1212}
1213
1214uint32_t SSL_clear_options(SSL *ssl, uint32_t options) {
1215 ssl->options &= ~options;
1216 return ssl->options;
1217}
1218
1219uint32_t SSL_get_options(const SSL *ssl) { return ssl->options; }
1220
1221uint32_t SSL_CTX_set_mode(SSL_CTX *ctx, uint32_t mode) {
1222 ctx->mode |= mode;
1223 return ctx->mode;
1224}
1225
1226uint32_t SSL_CTX_clear_mode(SSL_CTX *ctx, uint32_t mode) {
1227 ctx->mode &= ~mode;
1228 return ctx->mode;
1229}
1230
1231uint32_t SSL_CTX_get_mode(const SSL_CTX *ctx) { return ctx->mode; }
1232
1233uint32_t SSL_set_mode(SSL *ssl, uint32_t mode) {
1234 ssl->mode |= mode;
1235 return ssl->mode;
1236}
1237
1238uint32_t SSL_clear_mode(SSL *ssl, uint32_t mode) {
1239 ssl->mode &= ~mode;
1240 return ssl->mode;
1241}
1242
1243uint32_t SSL_get_mode(const SSL *ssl) { return ssl->mode; }
1244
Adam Langleyd519bf62016-12-12 11:16:44 -08001245void SSL_CTX_set0_buffer_pool(SSL_CTX *ctx, CRYPTO_BUFFER_POOL *pool) {
1246 ctx->pool = pool;
1247}
1248
David Benjaminee0c8272015-09-13 01:03:54 -04001249int SSL_get_tls_unique(const SSL *ssl, uint8_t *out, size_t *out_len,
1250 size_t max_out) {
David Benjaminf5260812017-07-12 17:16:39 -04001251 *out_len = 0;
1252 OPENSSL_memset(out, 0, max_out);
1253
David Benjaminced00b42016-10-08 11:24:09 -04001254 /* tls-unique is not defined for SSL 3.0 or TLS 1.3. */
1255 if (!ssl->s3->initial_handshake_complete ||
1256 ssl3_protocol_version(ssl) < TLS1_VERSION ||
1257 ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjaminf5260812017-07-12 17:16:39 -04001258 return 0;
David Benjaminced00b42016-10-08 11:24:09 -04001259 }
1260
David Benjaminee0c8272015-09-13 01:03:54 -04001261 /* The tls-unique value is the first Finished message in the handshake, which
1262 * is the client's in a full handshake and the server's for a resumption. See
1263 * https://tools.ietf.org/html/rfc5929#section-3.1. */
1264 const uint8_t *finished = ssl->s3->previous_client_finished;
1265 size_t finished_len = ssl->s3->previous_client_finished_len;
Steven Valdez87eab492016-06-27 16:34:59 -04001266 if (ssl->session != NULL) {
David Benjaminee0c8272015-09-13 01:03:54 -04001267 /* tls-unique is broken for resumed sessions unless EMS is used. */
1268 if (!ssl->session->extended_master_secret) {
David Benjaminf5260812017-07-12 17:16:39 -04001269 return 0;
David Benjaminee0c8272015-09-13 01:03:54 -04001270 }
1271 finished = ssl->s3->previous_server_finished;
1272 finished_len = ssl->s3->previous_server_finished_len;
1273 }
1274
David Benjaminee0c8272015-09-13 01:03:54 -04001275 *out_len = finished_len;
1276 if (finished_len > max_out) {
1277 *out_len = max_out;
1278 }
1279
David Benjamin17cf2cb2016-12-13 01:07:13 -05001280 OPENSSL_memcpy(out, finished, *out_len);
David Benjaminee0c8272015-09-13 01:03:54 -04001281 return 1;
David Benjaminee0c8272015-09-13 01:03:54 -04001282}
1283
David Benjamin5960a902017-02-14 20:07:11 -05001284static int set_session_id_context(CERT *cert, const uint8_t *sid_ctx,
David Benjamin30c4c302016-12-07 22:35:24 -05001285 size_t sid_ctx_len) {
David Benjamin5960a902017-02-14 20:07:11 -05001286 if (sid_ctx_len > sizeof(cert->sid_ctx)) {
David Benjamin3570d732015-06-29 00:28:17 -04001287 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -08001288 return 0;
1289 }
David Benjamin30c4c302016-12-07 22:35:24 -05001290
David Benjamina3d76d02017-07-14 19:36:07 -04001291 static_assert(sizeof(cert->sid_ctx) < 256, "sid_ctx too large");
David Benjamin5960a902017-02-14 20:07:11 -05001292 cert->sid_ctx_length = (uint8_t)sid_ctx_len;
1293 OPENSSL_memcpy(cert->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001294 return 1;
1295}
Adam Langley95c29f32014-06-20 12:00:00 -07001296
David Benjamin5960a902017-02-14 20:07:11 -05001297int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
1298 size_t sid_ctx_len) {
1299 return set_session_id_context(ctx->cert, sid_ctx, sid_ctx_len);
1300}
1301
Adam Langleyfcf25832014-12-18 17:42:32 -08001302int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
David Benjamin30c4c302016-12-07 22:35:24 -05001303 size_t sid_ctx_len) {
David Benjamin5960a902017-02-14 20:07:11 -05001304 return set_session_id_context(ssl->cert, sid_ctx, sid_ctx_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001305}
Adam Langley95c29f32014-06-20 12:00:00 -07001306
David Benjamin07820b52016-12-07 21:13:41 -05001307const uint8_t *SSL_get0_session_id_context(const SSL *ssl, size_t *out_len) {
David Benjamin5960a902017-02-14 20:07:11 -05001308 *out_len = ssl->cert->sid_ctx_length;
1309 return ssl->cert->sid_ctx;
David Benjamin07820b52016-12-07 21:13:41 -05001310}
1311
David Benjamin7481d392015-07-05 19:38:46 -04001312void SSL_certs_clear(SSL *ssl) { ssl_cert_clear_certs(ssl->cert); }
Adam Langley95c29f32014-06-20 12:00:00 -07001313
David Benjamin066fe0a2015-10-17 21:11:33 -04001314int SSL_get_fd(const SSL *ssl) { return SSL_get_rfd(ssl); }
Adam Langley95c29f32014-06-20 12:00:00 -07001315
David Benjamin066fe0a2015-10-17 21:11:33 -04001316int SSL_get_rfd(const SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001317 int ret = -1;
David Benjamin066fe0a2015-10-17 21:11:33 -04001318 BIO *b = BIO_find_type(SSL_get_rbio(ssl), BIO_TYPE_DESCRIPTOR);
1319 if (b != NULL) {
1320 BIO_get_fd(b, &ret);
Adam Langleyfcf25832014-12-18 17:42:32 -08001321 }
1322 return ret;
1323}
Adam Langley95c29f32014-06-20 12:00:00 -07001324
David Benjamin066fe0a2015-10-17 21:11:33 -04001325int SSL_get_wfd(const SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001326 int ret = -1;
David Benjamin066fe0a2015-10-17 21:11:33 -04001327 BIO *b = BIO_find_type(SSL_get_wbio(ssl), BIO_TYPE_DESCRIPTOR);
1328 if (b != NULL) {
1329 BIO_get_fd(b, &ret);
Adam Langleyfcf25832014-12-18 17:42:32 -08001330 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001331 return ret;
1332}
Adam Langley95c29f32014-06-20 12:00:00 -07001333
David Benjamin066fe0a2015-10-17 21:11:33 -04001334int SSL_set_fd(SSL *ssl, int fd) {
David Benjamin22ce9b22016-03-06 19:26:52 -05001335 BIO *bio = BIO_new(BIO_s_socket());
Adam Langleyfcf25832014-12-18 17:42:32 -08001336 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001337 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
David Benjamin066fe0a2015-10-17 21:11:33 -04001338 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001339 }
1340 BIO_set_fd(bio, fd, BIO_NOCLOSE);
David Benjamin066fe0a2015-10-17 21:11:33 -04001341 SSL_set_bio(ssl, bio, bio);
1342 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001343}
Adam Langley95c29f32014-06-20 12:00:00 -07001344
David Benjamin066fe0a2015-10-17 21:11:33 -04001345int SSL_set_wfd(SSL *ssl, int fd) {
David Benjamin2f871122016-05-20 14:27:17 -04001346 BIO *rbio = SSL_get_rbio(ssl);
1347 if (rbio == NULL || BIO_method_type(rbio) != BIO_TYPE_SOCKET ||
1348 BIO_get_fd(rbio, NULL) != fd) {
David Benjamin22ce9b22016-03-06 19:26:52 -05001349 BIO *bio = BIO_new(BIO_s_socket());
Adam Langleyfcf25832014-12-18 17:42:32 -08001350 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001351 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
David Benjamin066fe0a2015-10-17 21:11:33 -04001352 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001353 }
1354 BIO_set_fd(bio, fd, BIO_NOCLOSE);
David Benjamin4501bd52016-08-01 13:39:41 -04001355 SSL_set0_wbio(ssl, bio);
Adam Langleyfcf25832014-12-18 17:42:32 -08001356 } else {
David Benjamin4501bd52016-08-01 13:39:41 -04001357 /* Copy the rbio over to the wbio. */
1358 BIO_up_ref(rbio);
1359 SSL_set0_wbio(ssl, rbio);
Adam Langleyfcf25832014-12-18 17:42:32 -08001360 }
1361
David Benjamin066fe0a2015-10-17 21:11:33 -04001362 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001363}
Adam Langley95c29f32014-06-20 12:00:00 -07001364
David Benjamin066fe0a2015-10-17 21:11:33 -04001365int SSL_set_rfd(SSL *ssl, int fd) {
David Benjamin2f871122016-05-20 14:27:17 -04001366 BIO *wbio = SSL_get_wbio(ssl);
1367 if (wbio == NULL || BIO_method_type(wbio) != BIO_TYPE_SOCKET ||
1368 BIO_get_fd(wbio, NULL) != fd) {
David Benjamin22ce9b22016-03-06 19:26:52 -05001369 BIO *bio = BIO_new(BIO_s_socket());
Adam Langleyfcf25832014-12-18 17:42:32 -08001370 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001371 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
David Benjamin066fe0a2015-10-17 21:11:33 -04001372 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001373 }
1374 BIO_set_fd(bio, fd, BIO_NOCLOSE);
David Benjamin4501bd52016-08-01 13:39:41 -04001375 SSL_set0_rbio(ssl, bio);
Adam Langleyfcf25832014-12-18 17:42:32 -08001376 } else {
David Benjamin4501bd52016-08-01 13:39:41 -04001377 /* Copy the wbio over to the rbio. */
1378 BIO_up_ref(wbio);
1379 SSL_set0_rbio(ssl, wbio);
Adam Langleyfcf25832014-12-18 17:42:32 -08001380 }
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 Benjamin49ddf412016-10-08 11:56:01 -04001384static size_t copy_finished(void *out, size_t out_len, const uint8_t *in,
1385 size_t in_len) {
1386 if (out_len > in_len) {
1387 out_len = in_len;
1388 }
David Benjamin17cf2cb2016-12-13 01:07:13 -05001389 OPENSSL_memcpy(out, in, out_len);
David Benjamin49ddf412016-10-08 11:56:01 -04001390 return in_len;
1391}
1392
David Benjamin1a1b34d2015-10-17 12:51:52 -04001393size_t SSL_get_finished(const SSL *ssl, void *buf, size_t count) {
David Benjaminced00b42016-10-08 11:24:09 -04001394 if (!ssl->s3->initial_handshake_complete ||
1395 ssl3_protocol_version(ssl) < TLS1_VERSION ||
1396 ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1397 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001398 }
1399
David Benjamin49ddf412016-10-08 11:56:01 -04001400 if (ssl->server) {
1401 return copy_finished(buf, count, ssl->s3->previous_server_finished,
1402 ssl->s3->previous_server_finished_len);
David Benjaminced00b42016-10-08 11:24:09 -04001403 }
David Benjamin49ddf412016-10-08 11:56:01 -04001404
1405 return copy_finished(buf, count, ssl->s3->previous_client_finished,
1406 ssl->s3->previous_client_finished_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001407}
Adam Langley95c29f32014-06-20 12:00:00 -07001408
David Benjamin1a1b34d2015-10-17 12:51:52 -04001409size_t SSL_get_peer_finished(const SSL *ssl, void *buf, size_t count) {
David Benjaminced00b42016-10-08 11:24:09 -04001410 if (!ssl->s3->initial_handshake_complete ||
1411 ssl3_protocol_version(ssl) < TLS1_VERSION ||
1412 ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1413 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001414 }
Adam Langley95c29f32014-06-20 12:00:00 -07001415
David Benjamin49ddf412016-10-08 11:56:01 -04001416 if (ssl->server) {
1417 return copy_finished(buf, count, ssl->s3->previous_client_finished,
1418 ssl->s3->previous_client_finished_len);
David Benjaminced00b42016-10-08 11:24:09 -04001419 }
David Benjamin49ddf412016-10-08 11:56:01 -04001420
1421 return copy_finished(buf, count, ssl->s3->previous_server_finished,
1422 ssl->s3->previous_server_finished_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001423}
Adam Langley95c29f32014-06-20 12:00:00 -07001424
David Benjamin59937042015-09-19 13:04:22 -04001425int SSL_get_verify_mode(const SSL *ssl) { return ssl->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -07001426
David Benjamin42fea372015-09-19 01:22:44 -04001427int SSL_get_extms_support(const SSL *ssl) {
David Benjaminfc02b592017-02-17 16:26:01 -05001428 /* TLS 1.3 does not require extended master secret and always reports as
1429 * supporting it. */
David Benjamina68c1182016-07-24 00:14:33 -04001430 if (!ssl->s3->have_version) {
1431 return 0;
1432 }
David Benjaminfc02b592017-02-17 16:26:01 -05001433 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1434 return 1;
1435 }
1436
1437 /* If the initial handshake completed, query the established session. */
1438 if (ssl->s3->established_session != NULL) {
1439 return ssl->s3->established_session->extended_master_secret;
1440 }
1441
1442 /* Otherwise, query the in-progress handshake. */
1443 if (ssl->s3->hs != NULL) {
1444 return ssl->s3->hs->extended_master_secret;
1445 }
1446 assert(0);
1447 return 0;
Matt Braithwaitecd6f54b2015-09-17 12:54:42 -07001448}
1449
David Benjamin9a41d1b2015-05-16 01:30:09 -04001450int SSL_CTX_get_read_ahead(const SSL_CTX *ctx) { return 0; }
Adam Langley95c29f32014-06-20 12:00:00 -07001451
David Benjamin0d56f882015-12-19 17:05:56 -05001452int SSL_get_read_ahead(const SSL *ssl) { return 0; }
Adam Langley95c29f32014-06-20 12:00:00 -07001453
David Benjamin9a41d1b2015-05-16 01:30:09 -04001454void SSL_CTX_set_read_ahead(SSL_CTX *ctx, int yes) { }
David Benjamin61ecccf2015-05-05 09:44:51 -04001455
David Benjamin0d56f882015-12-19 17:05:56 -05001456void SSL_set_read_ahead(SSL *ssl, int yes) { }
David Benjamin61ecccf2015-05-05 09:44:51 -04001457
David Benjamin9f859492015-10-03 10:44:30 -04001458int SSL_pending(const SSL *ssl) {
1459 if (ssl->s3->rrec.type != SSL3_RT_APPLICATION_DATA) {
1460 return 0;
1461 }
1462 return ssl->s3->rrec.length;
Adam Langleyfcf25832014-12-18 17:42:32 -08001463}
Adam Langley95c29f32014-06-20 12:00:00 -07001464
Adam Langley95c29f32014-06-20 12:00:00 -07001465/* Fix this so it checks all the valid key/cert options */
Adam Langleyfcf25832014-12-18 17:42:32 -08001466int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
Adam Langley3a2b47a2017-01-24 13:59:42 -08001467 return ssl_cert_check_private_key(ctx->cert, ctx->cert->privatekey);
Adam Langleyc5ac2b62016-11-07 12:02:35 -08001468}
1469
1470/* Fix this function so that it takes an optional type parameter */
1471int SSL_check_private_key(const SSL *ssl) {
Adam Langley3a2b47a2017-01-24 13:59:42 -08001472 return ssl_cert_check_private_key(ssl->cert, ssl->cert->privatekey);
Adam Langleyfcf25832014-12-18 17:42:32 -08001473}
Adam Langley95c29f32014-06-20 12:00:00 -07001474
David Benjamin42fea372015-09-19 01:22:44 -04001475long SSL_get_default_timeout(const SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001476 return SSL_DEFAULT_SESSION_TIMEOUT;
1477}
Adam Langley95c29f32014-06-20 12:00:00 -07001478
David Benjamin44d3eed2015-05-21 01:29:55 -04001479int SSL_renegotiate(SSL *ssl) {
1480 /* Caller-initiated renegotiation is not supported. */
David Benjamin3570d732015-06-29 00:28:17 -04001481 OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
David Benjamin44d3eed2015-05-21 01:29:55 -04001482 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001483}
Adam Langley95c29f32014-06-20 12:00:00 -07001484
David Benjamin44d3eed2015-05-21 01:29:55 -04001485int SSL_renegotiate_pending(SSL *ssl) {
1486 return SSL_in_init(ssl) && ssl->s3->initial_handshake_complete;
Adam Langleyfcf25832014-12-18 17:42:32 -08001487}
Adam Langley95c29f32014-06-20 12:00:00 -07001488
David Benjamin936aada2016-06-07 19:09:22 -04001489int SSL_total_renegotiations(const SSL *ssl) {
1490 return ssl->s3->total_renegotiations;
1491}
1492
David Benjamin1d0a1942015-04-26 15:35:35 -04001493size_t SSL_CTX_get_max_cert_list(const SSL_CTX *ctx) {
1494 return ctx->max_cert_list;
1495}
1496
1497void SSL_CTX_set_max_cert_list(SSL_CTX *ctx, size_t max_cert_list) {
1498 if (max_cert_list > kMaxHandshakeSize) {
1499 max_cert_list = kMaxHandshakeSize;
1500 }
1501 ctx->max_cert_list = (uint32_t)max_cert_list;
1502}
1503
1504size_t SSL_get_max_cert_list(const SSL *ssl) {
1505 return ssl->max_cert_list;
1506}
1507
1508void SSL_set_max_cert_list(SSL *ssl, size_t max_cert_list) {
1509 if (max_cert_list > kMaxHandshakeSize) {
1510 max_cert_list = kMaxHandshakeSize;
1511 }
1512 ssl->max_cert_list = (uint32_t)max_cert_list;
1513}
1514
Adam Langleyce9d85e2016-01-24 15:58:39 -08001515int SSL_CTX_set_max_send_fragment(SSL_CTX *ctx, size_t max_send_fragment) {
David Benjamin1d0a1942015-04-26 15:35:35 -04001516 if (max_send_fragment < 512) {
1517 max_send_fragment = 512;
1518 }
1519 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1520 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1521 }
1522 ctx->max_send_fragment = (uint16_t)max_send_fragment;
Adam Langleyce9d85e2016-01-24 15:58:39 -08001523
1524 return 1;
David Benjamin1d0a1942015-04-26 15:35:35 -04001525}
1526
Adam Langleyce9d85e2016-01-24 15:58:39 -08001527int SSL_set_max_send_fragment(SSL *ssl, size_t max_send_fragment) {
David Benjamin1d0a1942015-04-26 15:35:35 -04001528 if (max_send_fragment < 512) {
1529 max_send_fragment = 512;
1530 }
1531 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1532 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1533 }
1534 ssl->max_send_fragment = (uint16_t)max_send_fragment;
Adam Langleyce9d85e2016-01-24 15:58:39 -08001535
1536 return 1;
David Benjamin1d0a1942015-04-26 15:35:35 -04001537}
1538
David Benjamincb9cf792015-05-05 09:46:14 -04001539int SSL_set_mtu(SSL *ssl, unsigned mtu) {
David Benjamince079fd2016-08-02 16:22:34 -04001540 if (!SSL_is_dtls(ssl) || mtu < dtls1_min_mtu()) {
David Benjamincb9cf792015-05-05 09:46:14 -04001541 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001542 }
David Benjamincb9cf792015-05-05 09:46:14 -04001543 ssl->d1->mtu = mtu;
1544 return 1;
1545}
1546
1547int SSL_get_secure_renegotiation_support(const SSL *ssl) {
David Benjamind2610042017-01-03 10:49:28 -05001548 if (!ssl->s3->have_version) {
1549 return 0;
1550 }
1551 return ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
1552 ssl->s3->send_connection_binding;
David Benjamincb9cf792015-05-05 09:46:14 -04001553}
1554
Adam Langleyfcf25832014-12-18 17:42:32 -08001555LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) { return ctx->sessions; }
1556
David Benjamin71f7d3d2015-05-05 09:46:38 -04001557size_t SSL_CTX_sess_number(const SSL_CTX *ctx) {
1558 return lh_SSL_SESSION_num_items(ctx->sessions);
1559}
1560
1561unsigned long SSL_CTX_sess_set_cache_size(SSL_CTX *ctx, unsigned long size) {
1562 unsigned long ret = ctx->session_cache_size;
1563 ctx->session_cache_size = size;
1564 return ret;
1565}
1566
1567unsigned long SSL_CTX_sess_get_cache_size(const SSL_CTX *ctx) {
1568 return ctx->session_cache_size;
1569}
1570
1571int SSL_CTX_set_session_cache_mode(SSL_CTX *ctx, int mode) {
1572 int ret = ctx->session_cache_mode;
1573 ctx->session_cache_mode = mode;
1574 return ret;
1575}
1576
1577int SSL_CTX_get_session_cache_mode(const SSL_CTX *ctx) {
1578 return ctx->session_cache_mode;
1579}
1580
David Benjamin936aada2016-06-07 19:09:22 -04001581
1582int SSL_CTX_get_tlsext_ticket_keys(SSL_CTX *ctx, void *out, size_t len) {
1583 if (out == NULL) {
1584 return 48;
1585 }
1586 if (len != 48) {
1587 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH);
1588 return 0;
1589 }
David Benjaminf5260812017-07-12 17:16:39 -04001590 uint8_t *out_bytes = reinterpret_cast<uint8_t *>(out);
David Benjamin17cf2cb2016-12-13 01:07:13 -05001591 OPENSSL_memcpy(out_bytes, ctx->tlsext_tick_key_name, 16);
1592 OPENSSL_memcpy(out_bytes + 16, ctx->tlsext_tick_hmac_key, 16);
1593 OPENSSL_memcpy(out_bytes + 32, ctx->tlsext_tick_aes_key, 16);
David Benjamin936aada2016-06-07 19:09:22 -04001594 return 1;
1595}
1596
1597int SSL_CTX_set_tlsext_ticket_keys(SSL_CTX *ctx, const void *in, size_t len) {
1598 if (in == NULL) {
1599 return 48;
1600 }
1601 if (len != 48) {
1602 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH);
1603 return 0;
1604 }
David Benjaminf5260812017-07-12 17:16:39 -04001605 const uint8_t *in_bytes = reinterpret_cast<const uint8_t *>(in);
David Benjamin17cf2cb2016-12-13 01:07:13 -05001606 OPENSSL_memcpy(ctx->tlsext_tick_key_name, in_bytes, 16);
1607 OPENSSL_memcpy(ctx->tlsext_tick_hmac_key, in_bytes + 16, 16);
1608 OPENSSL_memcpy(ctx->tlsext_tick_aes_key, in_bytes + 32, 16);
David Benjamin936aada2016-06-07 19:09:22 -04001609 return 1;
1610}
1611
1612int SSL_CTX_set_tlsext_ticket_key_cb(
1613 SSL_CTX *ctx, int (*callback)(SSL *ssl, uint8_t *key_name, uint8_t *iv,
1614 EVP_CIPHER_CTX *ctx, HMAC_CTX *hmac_ctx,
1615 int encrypt)) {
1616 ctx->tlsext_ticket_key_cb = callback;
1617 return 1;
1618}
1619
1620int SSL_CTX_set1_curves(SSL_CTX *ctx, const int *curves, size_t curves_len) {
1621 return tls1_set_curves(&ctx->supported_group_list,
1622 &ctx->supported_group_list_len, curves,
1623 curves_len);
1624}
1625
1626int SSL_set1_curves(SSL *ssl, const int *curves, size_t curves_len) {
1627 return tls1_set_curves(&ssl->supported_group_list,
1628 &ssl->supported_group_list_len, curves,
1629 curves_len);
1630}
1631
Alessandro Ghedini5fd18072016-09-28 21:04:25 +01001632int SSL_CTX_set1_curves_list(SSL_CTX *ctx, const char *curves) {
1633 return tls1_set_curves_list(&ctx->supported_group_list,
1634 &ctx->supported_group_list_len, curves);
1635}
1636
1637int SSL_set1_curves_list(SSL *ssl, const char *curves) {
1638 return tls1_set_curves_list(&ssl->supported_group_list,
1639 &ssl->supported_group_list_len, curves);
1640}
1641
David Benjamin9e68f192016-06-30 14:55:33 -04001642uint16_t SSL_get_curve_id(const SSL *ssl) {
1643 /* TODO(davidben): This checks the wrong session if there is a renegotiation in
1644 * progress. */
Steven Valdez87eab492016-06-27 16:34:59 -04001645 SSL_SESSION *session = SSL_get_session(ssl);
David Benjamin4882a6c2016-12-11 02:48:12 -05001646 if (session == NULL) {
David Benjamin9e68f192016-06-30 14:55:33 -04001647 return 0;
1648 }
1649
David Benjamin4882a6c2016-12-11 02:48:12 -05001650 return session->group_id;
David Benjamin9e68f192016-06-30 14:55:33 -04001651}
1652
David Benjamin936aada2016-06-07 19:09:22 -04001653int SSL_CTX_set_tmp_dh(SSL_CTX *ctx, const DH *dh) {
David Benjamin936aada2016-06-07 19:09:22 -04001654 return 1;
1655}
1656
1657int SSL_set_tmp_dh(SSL *ssl, const DH *dh) {
David Benjamin936aada2016-06-07 19:09:22 -04001658 return 1;
1659}
1660
David Benjamin8ebeabf2017-03-02 22:04:07 -05001661OPENSSL_EXPORT STACK_OF(SSL_CIPHER) *SSL_CTX_get_ciphers(const SSL_CTX *ctx) {
1662 return ctx->cipher_list->ciphers;
1663}
1664
David Benjamin32876b32015-09-20 12:17:03 -04001665STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *ssl) {
1666 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001667 return NULL;
1668 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001669
David Benjaminaf3b3d32016-10-31 16:29:57 -04001670 const struct ssl_cipher_preference_list_st *prefs =
1671 ssl_get_cipher_preferences(ssl);
1672 if (prefs == NULL) {
1673 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08001674 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001675
David Benjaminaf3b3d32016-10-31 16:29:57 -04001676 return prefs->ciphers;
Adam Langleyfcf25832014-12-18 17:42:32 -08001677}
Adam Langley95c29f32014-06-20 12:00:00 -07001678
David Benjamin32876b32015-09-20 12:17:03 -04001679const char *SSL_get_cipher_list(const SSL *ssl, int n) {
David Benjamin32876b32015-09-20 12:17:03 -04001680 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001681 return NULL;
1682 }
Adam Langley95c29f32014-06-20 12:00:00 -07001683
David Benjamin8ebeabf2017-03-02 22:04:07 -05001684 STACK_OF(SSL_CIPHER) *sk = SSL_get_ciphers(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001685 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
1686 return NULL;
1687 }
Adam Langley95c29f32014-06-20 12:00:00 -07001688
David Benjamin8ebeabf2017-03-02 22:04:07 -05001689 const SSL_CIPHER *c = sk_SSL_CIPHER_value(sk, n);
Adam Langleyfcf25832014-12-18 17:42:32 -08001690 if (c == NULL) {
1691 return NULL;
1692 }
Adam Langley95c29f32014-06-20 12:00:00 -07001693
Adam Langleyfcf25832014-12-18 17:42:32 -08001694 return c->name;
1695}
David Benjamin5491e3f2014-09-29 19:33:09 -04001696
Adam Langleyfcf25832014-12-18 17:42:32 -08001697int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
Matthew Braithwaite6ad20dc2017-02-21 16:41:33 -08001698 return ssl_create_cipher_list(ctx->method, &ctx->cipher_list, str,
1699 0 /* not strict */);
Matthew Braithwaitea57dcfb2017-02-17 22:08:23 -08001700}
1701
1702int SSL_CTX_set_strict_cipher_list(SSL_CTX *ctx, const char *str) {
Matthew Braithwaite6ad20dc2017-02-21 16:41:33 -08001703 return ssl_create_cipher_list(ctx->method, &ctx->cipher_list, str,
1704 1 /* strict */);
Adam Langleyfcf25832014-12-18 17:42:32 -08001705}
David Benjamin39482a12014-07-20 13:30:15 -04001706
David Benjamin32876b32015-09-20 12:17:03 -04001707int SSL_set_cipher_list(SSL *ssl, const char *str) {
Matthew Braithwaite6ad20dc2017-02-21 16:41:33 -08001708 return ssl_create_cipher_list(ssl->ctx->method, &ssl->cipher_list, str,
1709 0 /* not strict */);
Matthew Braithwaitea57dcfb2017-02-17 22:08:23 -08001710}
1711
1712int SSL_set_strict_cipher_list(SSL *ssl, const char *str) {
Matthew Braithwaite6ad20dc2017-02-21 16:41:33 -08001713 return ssl_create_cipher_list(ssl->ctx->method, &ssl->cipher_list, str,
1714 1 /* strict */);
Adam Langleyfcf25832014-12-18 17:42:32 -08001715}
Adam Langley95c29f32014-06-20 12:00:00 -07001716
David Benjamin07e13842015-10-17 13:48:04 -04001717const char *SSL_get_servername(const SSL *ssl, const int type) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001718 if (type != TLSEXT_NAMETYPE_host_name) {
1719 return NULL;
1720 }
Adam Langley95c29f32014-06-20 12:00:00 -07001721
David Benjamin07e13842015-10-17 13:48:04 -04001722 /* Historically, |SSL_get_servername| was also the configuration getter
1723 * corresponding to |SSL_set_tlsext_host_name|. */
1724 if (ssl->tlsext_hostname != NULL) {
1725 return ssl->tlsext_hostname;
1726 }
1727
David Benjamin4eb95cc2016-11-16 17:08:23 +09001728 /* During the handshake, report the handshake value. */
1729 if (ssl->s3->hs != NULL) {
David Benjamin31b0c9b2017-07-20 14:49:15 -04001730 return ssl->s3->hs->hostname.get();
David Benjamin4eb95cc2016-11-16 17:08:23 +09001731 }
1732
1733 /* SSL_get_servername may also be called after the handshake to look up the
1734 * SNI value.
1735 *
1736 * TODO(davidben): This is almost unused. Can we remove it? */
Steven Valdez87eab492016-06-27 16:34:59 -04001737 SSL_SESSION *session = SSL_get_session(ssl);
1738 if (session == NULL) {
David Benjamin07e13842015-10-17 13:48:04 -04001739 return NULL;
1740 }
Steven Valdez87eab492016-06-27 16:34:59 -04001741 return session->tlsext_hostname;
Adam Langleyfcf25832014-12-18 17:42:32 -08001742}
Adam Langley95c29f32014-06-20 12:00:00 -07001743
David Benjamin07e13842015-10-17 13:48:04 -04001744int SSL_get_servername_type(const SSL *ssl) {
Steven Valdez87eab492016-06-27 16:34:59 -04001745 SSL_SESSION *session = SSL_get_session(ssl);
1746 if (session == NULL || session->tlsext_hostname == NULL) {
1747 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001748 }
Steven Valdez87eab492016-06-27 16:34:59 -04001749 return TLSEXT_NAMETYPE_host_name;
Adam Langleyfcf25832014-12-18 17:42:32 -08001750}
Adam Langley95c29f32014-06-20 12:00:00 -07001751
David Benjamin3a1dd462017-07-11 16:13:10 -04001752void SSL_CTX_set_custom_verify(
1753 SSL_CTX *ctx, int mode,
1754 enum ssl_verify_result_t (*callback)(SSL *ssl, uint8_t *out_alert)) {
1755 ctx->verify_mode = mode;
1756 ctx->custom_verify_callback = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08001757}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001758
David Benjamin3a1dd462017-07-11 16:13:10 -04001759void SSL_set_custom_verify(
1760 SSL *ssl, int mode,
1761 enum ssl_verify_result_t (*callback)(SSL *ssl, uint8_t *out_alert)) {
1762 ssl->verify_mode = mode;
1763 ssl->custom_verify_callback = callback;
1764}
1765
1766void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
1767 ctx->signed_cert_timestamps_enabled = 1;
Adam Langleyfe366722017-03-07 16:41:04 -08001768}
1769
David Benjamin26e1ff32017-02-14 20:13:00 -05001770void SSL_enable_signed_cert_timestamps(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001771 ssl->signed_cert_timestamps_enabled = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001772}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001773
Adam Langleyfcf25832014-12-18 17:42:32 -08001774void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
1775 ctx->ocsp_stapling_enabled = 1;
1776}
David Benjamin6c7aed02014-08-27 16:42:38 -04001777
David Benjamin26e1ff32017-02-14 20:13:00 -05001778void SSL_enable_ocsp_stapling(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001779 ssl->ocsp_stapling_enabled = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001780}
David Benjamin6c7aed02014-08-27 16:42:38 -04001781
Adam Langleyfcf25832014-12-18 17:42:32 -08001782void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
1783 size_t *out_len) {
Steven Valdez87eab492016-06-27 16:34:59 -04001784 SSL_SESSION *session = SSL_get_session(ssl);
Adam Langley3cb50e02014-08-26 14:00:31 -07001785
Adam Langleyfcf25832014-12-18 17:42:32 -08001786 *out_len = 0;
1787 *out = NULL;
1788 if (ssl->server || !session || !session->tlsext_signed_cert_timestamp_list) {
1789 return;
1790 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001791
Adam Langleyfcf25832014-12-18 17:42:32 -08001792 *out = session->tlsext_signed_cert_timestamp_list;
1793 *out_len = session->tlsext_signed_cert_timestamp_list_length;
1794}
David Benjamin6c7aed02014-08-27 16:42:38 -04001795
Adam Langleyfcf25832014-12-18 17:42:32 -08001796void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
1797 size_t *out_len) {
Steven Valdez87eab492016-06-27 16:34:59 -04001798 SSL_SESSION *session = SSL_get_session(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001799
1800 *out_len = 0;
1801 *out = NULL;
1802 if (ssl->server || !session || !session->ocsp_response) {
1803 return;
1804 }
1805 *out = session->ocsp_response;
1806 *out_len = session->ocsp_response_length;
1807}
David Benjamin6c7aed02014-08-27 16:42:38 -04001808
David Benjamin936aada2016-06-07 19:09:22 -04001809int SSL_set_tlsext_host_name(SSL *ssl, const char *name) {
1810 OPENSSL_free(ssl->tlsext_hostname);
1811 ssl->tlsext_hostname = NULL;
1812
1813 if (name == NULL) {
1814 return 1;
1815 }
1816
1817 size_t len = strlen(name);
1818 if (len == 0 || len > TLSEXT_MAXLEN_host_name) {
1819 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL3_EXT_INVALID_SERVERNAME);
1820 return 0;
1821 }
1822 ssl->tlsext_hostname = BUF_strdup(name);
1823 if (ssl->tlsext_hostname == NULL) {
1824 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1825 return 0;
1826 }
1827 return 1;
1828}
1829
1830int SSL_CTX_set_tlsext_servername_callback(
1831 SSL_CTX *ctx, int (*callback)(SSL *ssl, int *out_alert, void *arg)) {
1832 ctx->tlsext_servername_callback = callback;
1833 return 1;
1834}
1835
1836int SSL_CTX_set_tlsext_servername_arg(SSL_CTX *ctx, void *arg) {
1837 ctx->tlsext_servername_arg = arg;
1838 return 1;
1839}
1840
David Benjaminca743582017-06-15 17:51:35 -04001841int SSL_select_next_proto(uint8_t **out, uint8_t *out_len, const uint8_t *peer,
1842 unsigned peer_len, const uint8_t *supported,
1843 unsigned supported_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001844 const uint8_t *result;
David Benjaminca743582017-06-15 17:51:35 -04001845 int status;
Adam Langley95c29f32014-06-20 12:00:00 -07001846
David Benjaminca743582017-06-15 17:51:35 -04001847 /* For each protocol in peer preference order, see if we support it. */
1848 for (unsigned i = 0; i < peer_len;) {
1849 for (unsigned j = 0; j < supported_len;) {
1850 if (peer[i] == supported[j] &&
1851 OPENSSL_memcmp(&peer[i + 1], &supported[j + 1], peer[i]) == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001852 /* We found a match */
David Benjaminca743582017-06-15 17:51:35 -04001853 result = &peer[i];
Adam Langleyfcf25832014-12-18 17:42:32 -08001854 status = OPENSSL_NPN_NEGOTIATED;
1855 goto found;
1856 }
David Benjaminca743582017-06-15 17:51:35 -04001857 j += supported[j];
Adam Langleyfcf25832014-12-18 17:42:32 -08001858 j++;
1859 }
David Benjaminca743582017-06-15 17:51:35 -04001860 i += peer[i];
Adam Langleyfcf25832014-12-18 17:42:32 -08001861 i++;
1862 }
Adam Langley95c29f32014-06-20 12:00:00 -07001863
David Benjaminca743582017-06-15 17:51:35 -04001864 /* There's no overlap between our protocols and the peer's list. */
1865 result = supported;
Adam Langleyfcf25832014-12-18 17:42:32 -08001866 status = OPENSSL_NPN_NO_OVERLAP;
Adam Langley95c29f32014-06-20 12:00:00 -07001867
Adam Langleyfcf25832014-12-18 17:42:32 -08001868found:
1869 *out = (uint8_t *)result + 1;
David Benjamin977547b2015-09-16 00:25:52 -04001870 *out_len = result[0];
Adam Langleyfcf25832014-12-18 17:42:32 -08001871 return status;
1872}
Adam Langley95c29f32014-06-20 12:00:00 -07001873
David Benjamin977547b2015-09-16 00:25:52 -04001874void SSL_get0_next_proto_negotiated(const SSL *ssl, const uint8_t **out_data,
1875 unsigned *out_len) {
David Benjamin79978df2015-12-25 15:56:49 -05001876 *out_data = ssl->s3->next_proto_negotiated;
David Benjamin05d4c972017-06-21 16:35:10 -04001877 *out_len = ssl->s3->next_proto_negotiated_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001878}
1879
Adam Langleyfcf25832014-12-18 17:42:32 -08001880void SSL_CTX_set_next_protos_advertised_cb(
1881 SSL_CTX *ctx,
David Benjamin977547b2015-09-16 00:25:52 -04001882 int (*cb)(SSL *ssl, const uint8_t **out, unsigned *out_len, void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08001883 void *arg) {
1884 ctx->next_protos_advertised_cb = cb;
1885 ctx->next_protos_advertised_cb_arg = arg;
1886}
Adam Langley95c29f32014-06-20 12:00:00 -07001887
Adam Langleyfcf25832014-12-18 17:42:32 -08001888void SSL_CTX_set_next_proto_select_cb(
David Benjamin977547b2015-09-16 00:25:52 -04001889 SSL_CTX *ctx, int (*cb)(SSL *ssl, uint8_t **out, uint8_t *out_len,
1890 const uint8_t *in, unsigned in_len, void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08001891 void *arg) {
1892 ctx->next_proto_select_cb = cb;
1893 ctx->next_proto_select_cb_arg = arg;
1894}
Adam Langley95c29f32014-06-20 12:00:00 -07001895
Adam Langleyfcf25832014-12-18 17:42:32 -08001896int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
1897 unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001898 OPENSSL_free(ctx->alpn_client_proto_list);
David Benjaminf5260812017-07-12 17:16:39 -04001899 ctx->alpn_client_proto_list = (uint8_t *)BUF_memdup(protos, protos_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001900 if (!ctx->alpn_client_proto_list) {
1901 return 1;
1902 }
1903 ctx->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001904
Adam Langleyfcf25832014-12-18 17:42:32 -08001905 return 0;
1906}
Adam Langley95c29f32014-06-20 12:00:00 -07001907
Adam Langleyfcf25832014-12-18 17:42:32 -08001908int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001909 OPENSSL_free(ssl->alpn_client_proto_list);
David Benjaminf5260812017-07-12 17:16:39 -04001910 ssl->alpn_client_proto_list = (uint8_t *)BUF_memdup(protos, protos_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001911 if (!ssl->alpn_client_proto_list) {
1912 return 1;
1913 }
1914 ssl->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001915
Adam Langleyfcf25832014-12-18 17:42:32 -08001916 return 0;
1917}
Adam Langley95c29f32014-06-20 12:00:00 -07001918
Adam Langleyfcf25832014-12-18 17:42:32 -08001919void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
1920 int (*cb)(SSL *ssl, const uint8_t **out,
David Benjamin8984f1f2015-09-16 00:10:19 -04001921 uint8_t *out_len, const uint8_t *in,
1922 unsigned in_len, void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08001923 void *arg) {
1924 ctx->alpn_select_cb = cb;
1925 ctx->alpn_select_cb_arg = arg;
1926}
Adam Langley95c29f32014-06-20 12:00:00 -07001927
David Benjamin8984f1f2015-09-16 00:10:19 -04001928void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **out_data,
1929 unsigned *out_len) {
Steven Valdeze831a812017-03-09 14:56:07 -05001930 if (SSL_in_early_data(ssl) && !ssl->server) {
1931 *out_data = ssl->s3->hs->early_session->early_alpn;
1932 *out_len = ssl->s3->hs->early_session->early_alpn_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08001933 } else {
Steven Valdeze831a812017-03-09 14:56:07 -05001934 *out_data = ssl->s3->alpn_selected;
David Benjamin8984f1f2015-09-16 00:10:19 -04001935 *out_len = ssl->s3->alpn_selected_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08001936 }
1937}
Adam Langley95c29f32014-06-20 12:00:00 -07001938
David Benjaminc8ff30c2017-04-04 13:52:36 -04001939void SSL_CTX_set_allow_unknown_alpn_protos(SSL_CTX *ctx, int enabled) {
1940 ctx->allow_unknown_alpn_protos = !!enabled;
1941}
David Benjamin936aada2016-06-07 19:09:22 -04001942
David Benjamineebd3c82016-12-06 17:43:58 -05001943void SSL_CTX_set_tls_channel_id_enabled(SSL_CTX *ctx, int enabled) {
1944 ctx->tlsext_channel_id_enabled = !!enabled;
1945}
1946
David Benjamin936aada2016-06-07 19:09:22 -04001947int SSL_CTX_enable_tls_channel_id(SSL_CTX *ctx) {
David Benjamineebd3c82016-12-06 17:43:58 -05001948 SSL_CTX_set_tls_channel_id_enabled(ctx, 1);
David Benjamin936aada2016-06-07 19:09:22 -04001949 return 1;
1950}
1951
David Benjamineebd3c82016-12-06 17:43:58 -05001952void SSL_set_tls_channel_id_enabled(SSL *ssl, int enabled) {
1953 ssl->tlsext_channel_id_enabled = !!enabled;
1954}
1955
David Benjamin936aada2016-06-07 19:09:22 -04001956int SSL_enable_tls_channel_id(SSL *ssl) {
David Benjamineebd3c82016-12-06 17:43:58 -05001957 SSL_set_tls_channel_id_enabled(ssl, 1);
David Benjamin936aada2016-06-07 19:09:22 -04001958 return 1;
1959}
1960
1961static int is_p256_key(EVP_PKEY *private_key) {
1962 const EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(private_key);
1963 return ec_key != NULL &&
1964 EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key)) ==
1965 NID_X9_62_prime256v1;
1966}
1967
1968int SSL_CTX_set1_tls_channel_id(SSL_CTX *ctx, EVP_PKEY *private_key) {
1969 if (!is_p256_key(private_key)) {
1970 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256);
1971 return 0;
1972 }
1973
1974 EVP_PKEY_free(ctx->tlsext_channel_id_private);
Adam Langley310d3f62016-07-12 10:39:20 -07001975 EVP_PKEY_up_ref(private_key);
1976 ctx->tlsext_channel_id_private = private_key;
David Benjamin936aada2016-06-07 19:09:22 -04001977 ctx->tlsext_channel_id_enabled = 1;
1978
1979 return 1;
1980}
1981
1982int SSL_set1_tls_channel_id(SSL *ssl, EVP_PKEY *private_key) {
1983 if (!is_p256_key(private_key)) {
1984 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256);
1985 return 0;
1986 }
1987
1988 EVP_PKEY_free(ssl->tlsext_channel_id_private);
Adam Langley310d3f62016-07-12 10:39:20 -07001989 EVP_PKEY_up_ref(private_key);
1990 ssl->tlsext_channel_id_private = private_key;
David Benjamin936aada2016-06-07 19:09:22 -04001991 ssl->tlsext_channel_id_enabled = 1;
1992
1993 return 1;
1994}
1995
1996size_t SSL_get_tls_channel_id(SSL *ssl, uint8_t *out, size_t max_out) {
1997 if (!ssl->s3->tlsext_channel_id_valid) {
1998 return 0;
1999 }
David Benjamin17cf2cb2016-12-13 01:07:13 -05002000 OPENSSL_memcpy(out, ssl->s3->tlsext_channel_id,
2001 (max_out < 64) ? max_out : 64);
David Benjamin936aada2016-06-07 19:09:22 -04002002 return 64;
2003}
2004
David Benjamin936aada2016-06-07 19:09:22 -04002005size_t SSL_get0_certificate_types(SSL *ssl, const uint8_t **out_types) {
David Benjamina0486782016-10-06 19:11:32 -04002006 if (ssl->server || ssl->s3->hs == NULL) {
David Benjamin936aada2016-06-07 19:09:22 -04002007 *out_types = NULL;
2008 return 0;
2009 }
David Benjamina0486782016-10-06 19:11:32 -04002010 *out_types = ssl->s3->hs->certificate_types;
2011 return ssl->s3->hs->num_certificate_types;
David Benjamin936aada2016-06-07 19:09:22 -04002012}
2013
David Benjamin0d56f882015-12-19 17:05:56 -05002014EVP_PKEY *SSL_get_privatekey(const SSL *ssl) {
2015 if (ssl->cert != NULL) {
2016 return ssl->cert->privatekey;
Adam Langleyfcf25832014-12-18 17:42:32 -08002017 }
2018
2019 return NULL;
2020}
2021
Adam Langleyfcf25832014-12-18 17:42:32 -08002022EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
2023 if (ctx->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04002024 return ctx->cert->privatekey;
Adam Langleyfcf25832014-12-18 17:42:32 -08002025 }
2026
2027 return NULL;
2028}
2029
David Benjamin42fea372015-09-19 01:22:44 -04002030const SSL_CIPHER *SSL_get_current_cipher(const SSL *ssl) {
David Benjamincfc11c22017-07-18 22:45:18 -04002031 return ssl->s3->aead_write_ctx->cipher();
Adam Langleyfcf25832014-12-18 17:42:32 -08002032}
2033
David Benjamin936aada2016-06-07 19:09:22 -04002034int SSL_session_reused(const SSL *ssl) {
Steven Valdeze831a812017-03-09 14:56:07 -05002035 return ssl->s3->session_reused || SSL_in_early_data(ssl);
David Benjamin936aada2016-06-07 19:09:22 -04002036}
2037
David Benjamin0d56f882015-12-19 17:05:56 -05002038const COMP_METHOD *SSL_get_current_compression(SSL *ssl) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002039
David Benjamin0d56f882015-12-19 17:05:56 -05002040const COMP_METHOD *SSL_get_current_expansion(SSL *ssl) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002041
Adam Langley29ec5d12016-03-01 16:12:28 -08002042int *SSL_get_server_tmp_key(SSL *ssl, EVP_PKEY **out_key) { return 0; }
2043
Adam Langleyfcf25832014-12-18 17:42:32 -08002044void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
Adam Langleybb85f3d2015-10-28 18:44:11 -07002045 ctx->quiet_shutdown = (mode != 0);
Adam Langleyfcf25832014-12-18 17:42:32 -08002046}
2047
2048int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
2049 return ctx->quiet_shutdown;
2050}
2051
Adam Langleybb85f3d2015-10-28 18:44:11 -07002052void SSL_set_quiet_shutdown(SSL *ssl, int mode) {
2053 ssl->quiet_shutdown = (mode != 0);
2054}
Adam Langleyfcf25832014-12-18 17:42:32 -08002055
David Benjamin9f859492015-10-03 10:44:30 -04002056int SSL_get_quiet_shutdown(const SSL *ssl) { return ssl->quiet_shutdown; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002057
David Benjamin63006a92015-10-18 00:00:06 -04002058void SSL_set_shutdown(SSL *ssl, int mode) {
2059 /* It is an error to clear any bits that have already been set. (We can't try
2060 * to get a second close_notify or send two.) */
David Benjaminfa214e42016-05-10 17:03:10 -04002061 assert((SSL_get_shutdown(ssl) & mode) == SSL_get_shutdown(ssl));
David Benjamin63006a92015-10-18 00:00:06 -04002062
David Benjaminfa214e42016-05-10 17:03:10 -04002063 if (mode & SSL_RECEIVED_SHUTDOWN &&
2064 ssl->s3->recv_shutdown == ssl_shutdown_none) {
2065 ssl->s3->recv_shutdown = ssl_shutdown_close_notify;
2066 }
2067
2068 if (mode & SSL_SENT_SHUTDOWN &&
2069 ssl->s3->send_shutdown == ssl_shutdown_none) {
2070 ssl->s3->send_shutdown = ssl_shutdown_close_notify;
2071 }
David Benjamin63006a92015-10-18 00:00:06 -04002072}
Adam Langleyfcf25832014-12-18 17:42:32 -08002073
David Benjaminfa214e42016-05-10 17:03:10 -04002074int SSL_get_shutdown(const SSL *ssl) {
2075 int ret = 0;
2076 if (ssl->s3->recv_shutdown != ssl_shutdown_none) {
2077 /* Historically, OpenSSL set |SSL_RECEIVED_SHUTDOWN| on both close_notify
2078 * and fatal alert. */
2079 ret |= SSL_RECEIVED_SHUTDOWN;
2080 }
2081 if (ssl->s3->send_shutdown == ssl_shutdown_close_notify) {
2082 /* Historically, OpenSSL set |SSL_SENT_SHUTDOWN| on only close_notify. */
2083 ret |= SSL_SENT_SHUTDOWN;
2084 }
2085 return ret;
2086}
Adam Langleyfcf25832014-12-18 17:42:32 -08002087
Adam Langleyfcf25832014-12-18 17:42:32 -08002088SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
2089
2090SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
2091 if (ssl->ctx == ctx) {
2092 return ssl->ctx;
2093 }
2094
Adam Langley3509dac2017-02-01 11:59:18 -08002095 /* One cannot change the X.509 callbacks during a connection. */
2096 if (ssl->ctx->x509_method != ctx->x509_method) {
2097 assert(0);
2098 return NULL;
2099 }
2100
Adam Langleyfcf25832014-12-18 17:42:32 -08002101 if (ctx == NULL) {
David Benjaminbe497062017-03-10 16:08:36 -05002102 ctx = ssl->session_ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08002103 }
2104
David Benjamin2755a3e2015-04-22 16:17:58 -04002105 ssl_cert_free(ssl->cert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002106 ssl->cert = ssl_cert_dup(ctx->cert);
David Benjamin2755a3e2015-04-22 16:17:58 -04002107
David Benjaminf650c712016-12-22 15:54:51 -05002108 SSL_CTX_up_ref(ctx);
2109 SSL_CTX_free(ssl->ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002110 ssl->ctx = ctx;
2111
Adam Langleyfcf25832014-12-18 17:42:32 -08002112 return ssl->ctx;
2113}
2114
Adam Langley95c29f32014-06-20 12:00:00 -07002115void SSL_set_info_callback(SSL *ssl,
David Benjamin82170242015-10-17 22:51:17 -04002116 void (*cb)(const SSL *ssl, int type, int value)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002117 ssl->info_callback = cb;
2118}
Adam Langley95c29f32014-06-20 12:00:00 -07002119
David Benjamin82170242015-10-17 22:51:17 -04002120void (*SSL_get_info_callback(const SSL *ssl))(const SSL *ssl, int type,
2121 int value) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002122 return ssl->info_callback;
2123}
Adam Langley95c29f32014-06-20 12:00:00 -07002124
David Benjamincb0c29f2016-12-12 17:00:50 -05002125int SSL_state(const SSL *ssl) {
David Benjamin9e766d72017-02-10 23:14:17 -05002126 return SSL_in_init(ssl) ? SSL_ST_INIT : SSL_ST_OK;
David Benjamincb0c29f2016-12-12 17:00:50 -05002127}
Adam Langley95c29f32014-06-20 12:00:00 -07002128
David Benjaminece089c2015-05-15 23:52:42 -04002129void SSL_set_state(SSL *ssl, int state) { }
Adam Langley95c29f32014-06-20 12:00:00 -07002130
Emily Stark95a79ee2016-02-25 21:12:28 -08002131char *SSL_get_shared_ciphers(const SSL *ssl, char *buf, int len) {
2132 if (len <= 0) {
2133 return NULL;
2134 }
2135 buf[0] = '\0';
2136 return buf;
2137}
2138
David Benjamin8a589332015-12-04 23:14:35 -05002139int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
David Benjamind94682d2017-05-14 17:10:18 -04002140 CRYPTO_EX_dup *dup_unused, CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002141 int index;
2142 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp,
David Benjamind94682d2017-05-14 17:10:18 -04002143 free_func)) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002144 return -1;
2145 }
2146 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002147}
Adam Langley95c29f32014-06-20 12:00:00 -07002148
David Benjamin27e377e2017-07-31 19:09:42 -04002149int SSL_set_ex_data(SSL *ssl, int idx, void *data) {
2150 return CRYPTO_set_ex_data(&ssl->ex_data, idx, data);
Adam Langleyfcf25832014-12-18 17:42:32 -08002151}
Adam Langley95c29f32014-06-20 12:00:00 -07002152
David Benjamin1ac08fe2015-09-15 01:36:22 -04002153void *SSL_get_ex_data(const SSL *ssl, int idx) {
2154 return CRYPTO_get_ex_data(&ssl->ex_data, idx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002155}
Adam Langley95c29f32014-06-20 12:00:00 -07002156
David Benjamin8a589332015-12-04 23:14:35 -05002157int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
David Benjamind94682d2017-05-14 17:10:18 -04002158 CRYPTO_EX_dup *dup_unused,
Adam Langleyfcf25832014-12-18 17:42:32 -08002159 CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002160 int index;
2161 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp,
David Benjamind94682d2017-05-14 17:10:18 -04002162 free_func)) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002163 return -1;
2164 }
2165 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002166}
Adam Langley95c29f32014-06-20 12:00:00 -07002167
David Benjamin27e377e2017-07-31 19:09:42 -04002168int SSL_CTX_set_ex_data(SSL_CTX *ctx, int idx, void *data) {
2169 return CRYPTO_set_ex_data(&ctx->ex_data, idx, data);
Adam Langleyfcf25832014-12-18 17:42:32 -08002170}
Adam Langley95c29f32014-06-20 12:00:00 -07002171
David Benjamin1ac08fe2015-09-15 01:36:22 -04002172void *SSL_CTX_get_ex_data(const SSL_CTX *ctx, int idx) {
2173 return CRYPTO_get_ex_data(&ctx->ex_data, idx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002174}
Adam Langley95c29f32014-06-20 12:00:00 -07002175
David Benjamin93d17492015-10-17 12:43:36 -04002176int SSL_want(const SSL *ssl) { return ssl->rwstate; }
Adam Langley95c29f32014-06-20 12:00:00 -07002177
Adam Langleyfcf25832014-12-18 17:42:32 -08002178void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
2179 RSA *(*cb)(SSL *ssl, int is_export,
David Benjamin27e377e2017-07-31 19:09:42 -04002180 int keylength)) {}
Adam Langley95c29f32014-06-20 12:00:00 -07002181
Adam Langleyfcf25832014-12-18 17:42:32 -08002182void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
David Benjamin27e377e2017-07-31 19:09:42 -04002183 int keylength)) {}
Adam Langleyfcf25832014-12-18 17:42:32 -08002184
2185void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
David Benjamin27e377e2017-07-31 19:09:42 -04002186 DH *(*cb)(SSL *ssl, int is_export,
2187 int keylength)) {}
Adam Langleyfcf25832014-12-18 17:42:32 -08002188
David Benjamin27e377e2017-07-31 19:09:42 -04002189void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*cb)(SSL *ssl, int is_export,
2190 int keylength)) {}
Adam Langleyfcf25832014-12-18 17:42:32 -08002191
David Benjamin0a9bf662017-06-21 16:34:15 -04002192static int use_psk_identity_hint(char **out, const char *identity_hint) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002193 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
David Benjamin3570d732015-06-29 00:28:17 -04002194 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -08002195 return 0;
2196 }
2197
2198 /* Clear currently configured hint, if any. */
David Benjamin0a9bf662017-06-21 16:34:15 -04002199 OPENSSL_free(*out);
2200 *out = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002201
David Benjamin78679342016-09-16 19:42:05 -04002202 /* Treat the empty hint as not supplying one. Plain PSK makes it possible to
2203 * send either no hint (omit ServerKeyExchange) or an empty hint, while
2204 * ECDHE_PSK can only spell empty hint. Having different capabilities is odd,
2205 * so we interpret empty and missing as identical. */
2206 if (identity_hint != NULL && identity_hint[0] != '\0') {
David Benjamin0a9bf662017-06-21 16:34:15 -04002207 *out = BUF_strdup(identity_hint);
2208 if (*out == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002209 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002210 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002211 }
Adam Langley95c29f32014-06-20 12:00:00 -07002212
Adam Langleyfcf25832014-12-18 17:42:32 -08002213 return 1;
2214}
Adam Langley95c29f32014-06-20 12:00:00 -07002215
David Benjamin0a9bf662017-06-21 16:34:15 -04002216int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
2217 return use_psk_identity_hint(&ctx->psk_identity_hint, identity_hint);
2218}
2219
2220int SSL_use_psk_identity_hint(SSL *ssl, const char *identity_hint) {
2221 return use_psk_identity_hint(&ssl->psk_identity_hint, identity_hint);
2222}
2223
David Benjamine8814df2015-09-15 08:05:54 -04002224const char *SSL_get_psk_identity_hint(const SSL *ssl) {
2225 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002226 return NULL;
2227 }
David Benjamine8814df2015-09-15 08:05:54 -04002228 return ssl->psk_identity_hint;
Adam Langleyfcf25832014-12-18 17:42:32 -08002229}
Adam Langley95c29f32014-06-20 12:00:00 -07002230
David Benjamine8814df2015-09-15 08:05:54 -04002231const char *SSL_get_psk_identity(const SSL *ssl) {
Steven Valdez87eab492016-06-27 16:34:59 -04002232 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002233 return NULL;
2234 }
Steven Valdez87eab492016-06-27 16:34:59 -04002235 SSL_SESSION *session = SSL_get_session(ssl);
2236 if (session == NULL) {
2237 return NULL;
2238 }
2239 return session->psk_identity;
Adam Langleyfcf25832014-12-18 17:42:32 -08002240}
Adam Langley95c29f32014-06-20 12:00:00 -07002241
Adam Langleyfcf25832014-12-18 17:42:32 -08002242void SSL_set_psk_client_callback(
David Benjamine8814df2015-09-15 08:05:54 -04002243 SSL *ssl, unsigned (*cb)(SSL *ssl, const char *hint, char *identity,
2244 unsigned max_identity_len, uint8_t *psk,
2245 unsigned max_psk_len)) {
2246 ssl->psk_client_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002247}
Adam Langley95c29f32014-06-20 12:00:00 -07002248
Adam Langleyfcf25832014-12-18 17:42:32 -08002249void SSL_CTX_set_psk_client_callback(
David Benjamine8814df2015-09-15 08:05:54 -04002250 SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *hint, char *identity,
2251 unsigned max_identity_len, uint8_t *psk,
2252 unsigned max_psk_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002253 ctx->psk_client_callback = cb;
2254}
Adam Langley95c29f32014-06-20 12:00:00 -07002255
Adam Langleyfcf25832014-12-18 17:42:32 -08002256void SSL_set_psk_server_callback(
David Benjamine8814df2015-09-15 08:05:54 -04002257 SSL *ssl, unsigned (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
2258 unsigned max_psk_len)) {
2259 ssl->psk_server_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002260}
Adam Langley95c29f32014-06-20 12:00:00 -07002261
Adam Langleyfcf25832014-12-18 17:42:32 -08002262void SSL_CTX_set_psk_server_callback(
David Benjamine8814df2015-09-15 08:05:54 -04002263 SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *identity,
2264 uint8_t *psk, unsigned max_psk_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002265 ctx->psk_server_callback = cb;
2266}
Adam Langley95c29f32014-06-20 12:00:00 -07002267
Adam Langleyfcf25832014-12-18 17:42:32 -08002268void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
2269 void (*cb)(int write_p, int version,
2270 int content_type, const void *buf,
2271 size_t len, SSL *ssl, void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002272 ctx->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002273}
David Benjamin61ecccf2015-05-05 09:44:51 -04002274
2275void SSL_CTX_set_msg_callback_arg(SSL_CTX *ctx, void *arg) {
2276 ctx->msg_callback_arg = arg;
2277}
2278
Adam Langleyfcf25832014-12-18 17:42:32 -08002279void SSL_set_msg_callback(SSL *ssl,
2280 void (*cb)(int write_p, int version, int content_type,
2281 const void *buf, size_t len, SSL *ssl,
2282 void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002283 ssl->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002284}
Adam Langley95c29f32014-06-20 12:00:00 -07002285
David Benjamin61ecccf2015-05-05 09:44:51 -04002286void SSL_set_msg_callback_arg(SSL *ssl, void *arg) {
2287 ssl->msg_callback_arg = arg;
2288}
2289
David Benjamind28f59c2015-11-17 22:32:50 -05002290void SSL_CTX_set_keylog_callback(SSL_CTX *ctx,
2291 void (*cb)(const SSL *ssl, const char *line)) {
2292 ctx->keylog_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002293}
Adam Langley95c29f32014-06-20 12:00:00 -07002294
David Benjamin6e3f5cc2016-09-14 14:43:14 -04002295void (*SSL_CTX_get_keylog_callback(const SSL_CTX *ctx))(const SSL *ssl,
2296 const char *line) {
2297 return ctx->keylog_callback;
2298}
2299
Taylor Brandstetter9edb2c62016-06-08 15:26:59 -07002300void SSL_CTX_set_current_time_cb(SSL_CTX *ctx,
2301 void (*cb)(const SSL *ssl,
2302 struct timeval *out_clock)) {
2303 ctx->current_time_cb = cb;
2304}
2305
David Benjamin5d8b1282015-10-17 23:26:35 -04002306int SSL_is_init_finished(const SSL *ssl) {
David Benjamin9e766d72017-02-10 23:14:17 -05002307 return !SSL_in_init(ssl);
David Benjamin5d8b1282015-10-17 23:26:35 -04002308}
2309
2310int SSL_in_init(const SSL *ssl) {
David Benjamin9e766d72017-02-10 23:14:17 -05002311 SSL_HANDSHAKE *hs = ssl->s3->hs;
2312 return hs != NULL && hs->state != SSL_ST_OK;
David Benjamin5d8b1282015-10-17 23:26:35 -04002313}
2314
2315int SSL_in_false_start(const SSL *ssl) {
David Benjamina0486782016-10-06 19:11:32 -04002316 if (ssl->s3->hs == NULL) {
2317 return 0;
2318 }
2319 return ssl->s3->hs->in_false_start;
David Benjamined7c4752015-02-16 19:16:46 -05002320}
2321
David Benjamin0d56f882015-12-19 17:05:56 -05002322int SSL_cutthrough_complete(const SSL *ssl) {
2323 return SSL_in_false_start(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08002324}
Adam Langley95c29f32014-06-20 12:00:00 -07002325
Adam Langleyfcf25832014-12-18 17:42:32 -08002326void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
2327 size_t *ssl_session_size) {
2328 *ssl_size = sizeof(SSL);
2329 *ssl_ctx_size = sizeof(SSL_CTX);
2330 *ssl_session_size = sizeof(SSL_SESSION);
2331}
Feng Lu41aa3252014-11-21 22:47:56 -08002332
David Benjamince079fd2016-08-02 16:22:34 -04002333int SSL_is_server(const SSL *ssl) { return ssl->server; }
2334
2335int SSL_is_dtls(const SSL *ssl) { return ssl->method->is_dtls; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002336
Alessandro Ghedini57e81e62017-03-14 23:36:00 +00002337void SSL_CTX_set_select_certificate_cb(
2338 SSL_CTX *ctx,
2339 enum ssl_select_cert_result_t (*cb)(const SSL_CLIENT_HELLO *)) {
David Benjamind4c2bce2015-10-17 12:28:18 -04002340 ctx->select_certificate_cb = cb;
2341}
2342
David Benjamin731058e2016-12-03 23:15:13 -05002343void SSL_CTX_set_dos_protection_cb(SSL_CTX *ctx,
2344 int (*cb)(const SSL_CLIENT_HELLO *)) {
Adam Langley524e7172015-02-20 16:04:00 -08002345 ctx->dos_protection_cb = cb;
2346}
2347
David Benjamin1d5ef3b2015-10-12 19:54:18 -04002348void SSL_set_renegotiate_mode(SSL *ssl, enum ssl_renegotiate_mode_t mode) {
2349 ssl->renegotiate_mode = mode;
2350}
2351
Adam Langleyc2d32802015-11-03 18:36:10 -08002352int SSL_get_ivs(const SSL *ssl, const uint8_t **out_read_iv,
2353 const uint8_t **out_write_iv, size_t *out_iv_len) {
Adam Langleyc2d32802015-11-03 18:36:10 -08002354 size_t write_iv_len;
David Benjamincfc11c22017-07-18 22:45:18 -04002355 if (!ssl->s3->aead_read_ctx->GetIV(out_read_iv, out_iv_len) ||
2356 !ssl->s3->aead_write_ctx->GetIV(out_write_iv, &write_iv_len) ||
Adam Langleyc2d32802015-11-03 18:36:10 -08002357 *out_iv_len != write_iv_len) {
2358 return 0;
2359 }
2360
2361 return 1;
David Benjamine348ff42015-11-06 17:55:30 -05002362}
David Benjamin6e807652015-11-02 12:02:20 -05002363
David Benjamin91b25012016-01-21 16:39:58 -05002364static uint64_t be_to_u64(const uint8_t in[8]) {
David Benjaminde942382016-02-11 12:02:01 -05002365 return (((uint64_t)in[0]) << 56) | (((uint64_t)in[1]) << 48) |
2366 (((uint64_t)in[2]) << 40) | (((uint64_t)in[3]) << 32) |
2367 (((uint64_t)in[4]) << 24) | (((uint64_t)in[5]) << 16) |
2368 (((uint64_t)in[6]) << 8) | ((uint64_t)in[7]);
David Benjamin91b25012016-01-21 16:39:58 -05002369}
2370
2371uint64_t SSL_get_read_sequence(const SSL *ssl) {
2372 /* TODO(davidben): Internally represent sequence numbers as uint64_t. */
David Benjamince079fd2016-08-02 16:22:34 -04002373 if (SSL_is_dtls(ssl)) {
David Benjaminde942382016-02-11 12:02:01 -05002374 /* max_seq_num already includes the epoch. */
2375 assert(ssl->d1->r_epoch == (ssl->d1->bitmap.max_seq_num >> 48));
2376 return ssl->d1->bitmap.max_seq_num;
2377 }
David Benjamin91b25012016-01-21 16:39:58 -05002378 return be_to_u64(ssl->s3->read_sequence);
2379}
2380
2381uint64_t SSL_get_write_sequence(const SSL *ssl) {
David Benjaminde942382016-02-11 12:02:01 -05002382 uint64_t ret = be_to_u64(ssl->s3->write_sequence);
David Benjamince079fd2016-08-02 16:22:34 -04002383 if (SSL_is_dtls(ssl)) {
David Benjaminde942382016-02-11 12:02:01 -05002384 assert((ret >> 48) == 0);
2385 ret |= ((uint64_t)ssl->d1->w_epoch) << 48;
2386 }
2387 return ret;
David Benjamin91b25012016-01-21 16:39:58 -05002388}
2389
Steven Valdez02563852016-06-23 13:33:05 -04002390uint16_t SSL_get_peer_signature_algorithm(const SSL *ssl) {
David Benjaminf1050fd2016-12-13 20:05:36 -05002391 /* TODO(davidben): This checks the wrong session if there is a renegotiation
2392 * in progress. */
2393 SSL_SESSION *session = SSL_get_session(ssl);
2394 if (session == NULL) {
2395 return 0;
2396 }
2397
2398 return session->peer_signature_algorithm;
Steven Valdez02563852016-06-23 13:33:05 -04002399}
2400
David Benjamin96ba15f2015-12-25 14:10:17 -05002401size_t SSL_get_client_random(const SSL *ssl, uint8_t *out, size_t max_out) {
2402 if (max_out == 0) {
2403 return sizeof(ssl->s3->client_random);
2404 }
2405 if (max_out > sizeof(ssl->s3->client_random)) {
2406 max_out = sizeof(ssl->s3->client_random);
2407 }
David Benjamin17cf2cb2016-12-13 01:07:13 -05002408 OPENSSL_memcpy(out, ssl->s3->client_random, max_out);
David Benjamin96ba15f2015-12-25 14:10:17 -05002409 return max_out;
2410}
2411
2412size_t SSL_get_server_random(const SSL *ssl, uint8_t *out, size_t max_out) {
2413 if (max_out == 0) {
2414 return sizeof(ssl->s3->server_random);
2415 }
2416 if (max_out > sizeof(ssl->s3->server_random)) {
2417 max_out = sizeof(ssl->s3->server_random);
2418 }
David Benjamin17cf2cb2016-12-13 01:07:13 -05002419 OPENSSL_memcpy(out, ssl->s3->server_random, max_out);
David Benjamin96ba15f2015-12-25 14:10:17 -05002420 return max_out;
2421}
2422
David Benjaminf6494f42016-01-07 12:37:41 -08002423const SSL_CIPHER *SSL_get_pending_cipher(const SSL *ssl) {
David Benjamin45738dd2017-02-09 20:01:26 -05002424 SSL_HANDSHAKE *hs = ssl->s3->hs;
2425 if (hs == NULL) {
David Benjaminf6494f42016-01-07 12:37:41 -08002426 return NULL;
2427 }
David Benjamin45738dd2017-02-09 20:01:26 -05002428 return hs->new_cipher;
David Benjaminf6494f42016-01-07 12:37:41 -08002429}
2430
David Benjaminbbaf3672016-11-17 10:53:09 +09002431void SSL_set_retain_only_sha256_of_client_certs(SSL *ssl, int enabled) {
2432 ssl->retain_only_sha256_of_client_certs = !!enabled;
2433}
2434
David Benjamincdd0b7e2016-01-07 10:10:41 -08002435void SSL_CTX_set_retain_only_sha256_of_client_certs(SSL_CTX *ctx, int enabled) {
2436 ctx->retain_only_sha256_of_client_certs = !!enabled;
2437}
2438
David Benjamin65ac9972016-09-02 21:35:25 -04002439void SSL_CTX_set_grease_enabled(SSL_CTX *ctx, int enabled) {
2440 ctx->grease_enabled = !!enabled;
2441}
2442
David Benjamin35ac5b72017-03-03 15:05:56 -05002443int32_t SSL_get_ticket_age_skew(const SSL *ssl) {
2444 return ssl->s3->ticket_age_skew;
2445}
2446
David Benjamin27bbae42015-09-13 00:54:37 -04002447int SSL_clear(SSL *ssl) {
David Benjaminb79cc842016-12-07 15:57:14 -05002448 /* In OpenSSL, reusing a client |SSL| with |SSL_clear| causes the previously
2449 * established session to be offered the next time around. wpa_supplicant
2450 * depends on this behavior, so emulate it. */
2451 SSL_SESSION *session = NULL;
2452 if (!ssl->server && ssl->s3->established_session != NULL) {
2453 session = ssl->s3->established_session;
2454 SSL_SESSION_up_ref(session);
2455 }
2456
David Benjamin27bbae42015-09-13 00:54:37 -04002457 /* TODO(davidben): Some state on |ssl| is reset both in |SSL_new| and
2458 * |SSL_clear| because it is per-connection state rather than configuration
2459 * state. Per-connection state should be on |ssl->s3| and |ssl->d1| so it is
2460 * naturally reset at the right points between |SSL_new|, |SSL_clear|, and
2461 * |ssl3_new|. */
2462
2463 ssl->rwstate = SSL_NOTHING;
2464
2465 BUF_MEM_free(ssl->init_buf);
2466 ssl->init_buf = NULL;
2467
David Benjamin27bbae42015-09-13 00:54:37 -04002468 /* The ssl->d1->mtu is simultaneously configuration (preserved across
2469 * clear) and connection-specific state (gets reset).
2470 *
2471 * TODO(davidben): Avoid this. */
2472 unsigned mtu = 0;
2473 if (ssl->d1 != NULL) {
2474 mtu = ssl->d1->mtu;
2475 }
2476
2477 ssl->method->ssl_free(ssl);
2478 if (!ssl->method->ssl_new(ssl)) {
David Benjaminb79cc842016-12-07 15:57:14 -05002479 SSL_SESSION_free(session);
David Benjamin27bbae42015-09-13 00:54:37 -04002480 return 0;
2481 }
David Benjamin27bbae42015-09-13 00:54:37 -04002482
David Benjamince079fd2016-08-02 16:22:34 -04002483 if (SSL_is_dtls(ssl) && (SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
David Benjamin27bbae42015-09-13 00:54:37 -04002484 ssl->d1->mtu = mtu;
2485 }
2486
David Benjaminb79cc842016-12-07 15:57:14 -05002487 if (session != NULL) {
2488 SSL_set_session(ssl, session);
2489 SSL_SESSION_free(session);
2490 }
2491
David Benjamin27bbae42015-09-13 00:54:37 -04002492 return 1;
2493}
2494
David Benjaminda881e92015-04-26 14:45:04 -04002495int SSL_CTX_sess_connect(const SSL_CTX *ctx) { return 0; }
2496int SSL_CTX_sess_connect_good(const SSL_CTX *ctx) { return 0; }
2497int SSL_CTX_sess_connect_renegotiate(const SSL_CTX *ctx) { return 0; }
2498int SSL_CTX_sess_accept(const SSL_CTX *ctx) { return 0; }
2499int SSL_CTX_sess_accept_renegotiate(const SSL_CTX *ctx) { return 0; }
2500int SSL_CTX_sess_accept_good(const SSL_CTX *ctx) { return 0; }
2501int SSL_CTX_sess_hits(const SSL_CTX *ctx) { return 0; }
2502int SSL_CTX_sess_cb_hits(const SSL_CTX *ctx) { return 0; }
2503int SSL_CTX_sess_misses(const SSL_CTX *ctx) { return 0; }
2504int SSL_CTX_sess_timeouts(const SSL_CTX *ctx) { return 0; }
2505int SSL_CTX_sess_cache_full(const SSL_CTX *ctx) { return 0; }
David Benjamin936aada2016-06-07 19:09:22 -04002506
2507int SSL_num_renegotiations(const SSL *ssl) {
2508 return SSL_total_renegotiations(ssl);
2509}
2510
2511int SSL_CTX_need_tmp_RSA(const SSL_CTX *ctx) { return 0; }
2512int SSL_need_tmp_RSA(const SSL *ssl) { return 0; }
2513int SSL_CTX_set_tmp_rsa(SSL_CTX *ctx, const RSA *rsa) { return 1; }
2514int SSL_set_tmp_rsa(SSL *ssl, const RSA *rsa) { return 1; }
Matt Braithwaite4838d8a2015-08-20 13:20:03 -07002515void ERR_load_SSL_strings(void) {}
David Benjamin27bbae42015-09-13 00:54:37 -04002516void SSL_load_error_strings(void) {}
David Benjamin936aada2016-06-07 19:09:22 -04002517int SSL_cache_hit(SSL *ssl) { return SSL_session_reused(ssl); }
2518
2519int SSL_CTX_set_tmp_ecdh(SSL_CTX *ctx, const EC_KEY *ec_key) {
2520 if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) {
2521 OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
2522 return 0;
2523 }
2524 int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key));
2525 return SSL_CTX_set1_curves(ctx, &nid, 1);
2526}
2527
2528int SSL_set_tmp_ecdh(SSL *ssl, const EC_KEY *ec_key) {
2529 if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) {
2530 OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
2531 return 0;
2532 }
2533 int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key));
2534 return SSL_set1_curves(ssl, &nid, 1);
2535}
David Benjamin721e8b72016-08-03 13:13:17 -04002536
Adam Langley4c341d02017-03-08 19:33:21 -08002537void SSL_CTX_set_ticket_aead_method(SSL_CTX *ctx,
2538 const SSL_TICKET_AEAD_METHOD *aead_method) {
2539 ctx->ticket_aead_method = aead_method;
2540}