blob: 60ee322e76ec5365b09689a743cc74ec759e3d84 [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 Benjamin86e95b82017-07-18 16:34:25 -0400141#define BORINGSSL_INTERNAL_CXX_TYPES
142
David Benjamin9e4e01e2015-09-15 01:48:04 -0400143#include <openssl/ssl.h>
144
Adam Langley95c29f32014-06-20 12:00:00 -0700145#include <assert.h>
David Benjamin93d97432016-08-08 21:22:47 -0400146#include <stdlib.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400147#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700148
David Benjamin39482a12014-07-20 13:30:15 -0400149#include <openssl/bytestring.h>
David Benjamin7c6d09b2015-09-05 11:00:19 -0400150#include <openssl/crypto.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400151#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700152#include <openssl/lhash.h>
153#include <openssl/mem.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700154#include <openssl/rand.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700155
David Benjamin2ee94aa2015-04-07 22:38:30 -0400156#include "internal.h"
David Benjamin546f1a52015-04-15 16:46:09 -0400157#include "../crypto/internal.h"
158
David Benjamin721e8b72016-08-03 13:13:17 -0400159#if defined(OPENSSL_WINDOWS)
160#include <sys/timeb.h>
161#else
162#include <sys/socket.h>
163#include <sys/time.h>
164#endif
165
Adam Langley95c29f32014-06-20 12:00:00 -0700166
David Benjamin86e95b82017-07-18 16:34:25 -0400167namespace bssl {
168
David Benjamin76c2efc2015-08-31 14:24:29 -0400169/* |SSL_R_UNKNOWN_PROTOCOL| is no longer emitted, but continue to define it
David Benjamin97760d52015-07-24 23:02:49 -0400170 * to avoid downstream churn. */
David Benjamin76c2efc2015-08-31 14:24:29 -0400171OPENSSL_DECLARE_ERROR_REASON(SSL, UNKNOWN_PROTOCOL)
David Benjamin97760d52015-07-24 23:02:49 -0400172
David Benjamin311c2572016-08-17 15:54:36 -0400173/* The following errors are no longer emitted, but are used in nginx without
174 * #ifdefs. */
175OPENSSL_DECLARE_ERROR_REASON(SSL, BLOCK_CIPHER_PAD_IS_WRONG)
176OPENSSL_DECLARE_ERROR_REASON(SSL, NO_CIPHERS_SPECIFIED)
177
Adam Langleyfcf25832014-12-18 17:42:32 -0800178/* Some error codes are special. Ensure the make_errors.go script never
179 * regresses this. */
David Benjamina3d76d02017-07-14 19:36:07 -0400180static_assert(SSL_R_TLSV1_ALERT_NO_RENEGOTIATION ==
181 SSL_AD_NO_RENEGOTIATION + SSL_AD_REASON_OFFSET,
182 "alert reason code mismatch");
David Benjamine1136082014-09-20 12:28:58 -0400183
David Benjamin1d0a1942015-04-26 15:35:35 -0400184/* kMaxHandshakeSize is the maximum size, in bytes, of a handshake message. */
185static const size_t kMaxHandshakeSize = (1u << 24) - 1;
186
David Benjaminaa585132015-06-29 23:36:17 -0400187static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl =
188 CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
189static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl_ctx =
190 CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
David Benjamin9f33fc62015-04-15 17:29:53 -0400191
David Benjamin86e95b82017-07-18 16:34:25 -0400192void ssl_reset_error_state(SSL *ssl) {
193 /* Functions which use |SSL_get_error| must reset I/O and error state on
194 * entry. */
195 ssl->rwstate = SSL_NOTHING;
196 ERR_clear_error();
197 ERR_clear_system_error();
198}
199
200int ssl_can_write(const SSL *ssl) {
201 return !SSL_in_init(ssl) || ssl->s3->hs->can_early_write;
202}
203
204int ssl_can_read(const SSL *ssl) {
205 return !SSL_in_init(ssl) || ssl->s3->hs->can_early_read;
206}
207
208void ssl_cipher_preference_list_free(
209 struct ssl_cipher_preference_list_st *cipher_list) {
210 if (cipher_list == NULL) {
211 return;
212 }
213 sk_SSL_CIPHER_free(cipher_list->ciphers);
214 OPENSSL_free(cipher_list->in_group_flags);
215 OPENSSL_free(cipher_list);
216}
217
218void ssl_update_cache(SSL_HANDSHAKE *hs, int mode) {
219 SSL *const ssl = hs->ssl;
220 SSL_CTX *ctx = ssl->session_ctx;
221 /* Never cache sessions with empty session IDs. */
222 if (ssl->s3->established_session->session_id_length == 0 ||
223 (ctx->session_cache_mode & mode) != mode) {
224 return;
225 }
226
227 /* Clients never use the internal session cache. */
228 int use_internal_cache = ssl->server && !(ctx->session_cache_mode &
229 SSL_SESS_CACHE_NO_INTERNAL_STORE);
230
231 /* A client may see new sessions on abbreviated handshakes if the server
232 * decides to renew the ticket. Once the handshake is completed, it should be
233 * inserted into the cache. */
234 if (ssl->s3->established_session != ssl->session ||
235 (!ssl->server && hs->ticket_expected)) {
236 if (use_internal_cache) {
237 SSL_CTX_add_session(ctx, ssl->s3->established_session);
238 }
239 if (ctx->new_session_cb != NULL) {
240 SSL_SESSION_up_ref(ssl->s3->established_session);
241 if (!ctx->new_session_cb(ssl, ssl->s3->established_session)) {
242 /* |new_session_cb|'s return value signals whether it took ownership. */
243 SSL_SESSION_free(ssl->s3->established_session);
244 }
245 }
246 }
247
248 if (use_internal_cache &&
249 !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR)) {
250 /* Automatically flush the internal session cache every 255 connections. */
251 int flush_cache = 0;
252 CRYPTO_MUTEX_lock_write(&ctx->lock);
253 ctx->handshakes_since_cache_flush++;
254 if (ctx->handshakes_since_cache_flush >= 255) {
255 flush_cache = 1;
256 ctx->handshakes_since_cache_flush = 0;
257 }
258 CRYPTO_MUTEX_unlock_write(&ctx->lock);
259
260 if (flush_cache) {
261 struct OPENSSL_timeval now;
262 ssl_get_current_time(ssl, &now);
263 SSL_CTX_flush_sessions(ctx, now.tv_sec);
264 }
265 }
266}
267
268static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
269 static const char hextable[] = "0123456789abcdef";
270 uint8_t *out;
271
272 if (!CBB_add_space(cbb, &out, in_len * 2)) {
273 return 0;
274 }
275
276 for (size_t i = 0; i < in_len; i++) {
277 *(out++) = (uint8_t)hextable[in[i] >> 4];
278 *(out++) = (uint8_t)hextable[in[i] & 0xf];
279 }
280
281 return 1;
282}
283
284int ssl_log_secret(const SSL *ssl, const char *label, const uint8_t *secret,
285 size_t secret_len) {
286 if (ssl->ctx->keylog_callback == NULL) {
287 return 1;
288 }
289
David Benjamin1386aad2017-07-19 23:57:40 -0400290 ScopedCBB cbb;
David Benjamin86e95b82017-07-18 16:34:25 -0400291 uint8_t *out;
292 size_t out_len;
David Benjamin1386aad2017-07-19 23:57:40 -0400293 if (!CBB_init(cbb.get(), strlen(label) + 1 + SSL3_RANDOM_SIZE * 2 + 1 +
David Benjamin86e95b82017-07-18 16:34:25 -0400294 secret_len * 2 + 1) ||
David Benjamin1386aad2017-07-19 23:57:40 -0400295 !CBB_add_bytes(cbb.get(), (const uint8_t *)label, strlen(label)) ||
296 !CBB_add_bytes(cbb.get(), (const uint8_t *)" ", 1) ||
297 !cbb_add_hex(cbb.get(), ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
298 !CBB_add_bytes(cbb.get(), (const uint8_t *)" ", 1) ||
299 !cbb_add_hex(cbb.get(), secret, secret_len) ||
300 !CBB_add_u8(cbb.get(), 0 /* NUL */) ||
301 !CBB_finish(cbb.get(), &out, &out_len)) {
David Benjamin86e95b82017-07-18 16:34:25 -0400302 return 0;
303 }
304
305 ssl->ctx->keylog_callback(ssl, (const char *)out);
306 OPENSSL_free(out);
307 return 1;
308}
309
310int ssl3_can_false_start(const SSL *ssl) {
311 const SSL_CIPHER *const cipher = SSL_get_current_cipher(ssl);
312
313 /* False Start only for TLS 1.2 with an ECDHE+AEAD cipher and ALPN or NPN. */
314 return !SSL_is_dtls(ssl) &&
315 SSL_version(ssl) == TLS1_2_VERSION &&
316 (ssl->s3->alpn_selected != NULL ||
317 ssl->s3->next_proto_negotiated != NULL) &&
318 cipher != NULL &&
319 cipher->algorithm_mkey == SSL_kECDHE &&
320 cipher->algorithm_mac == SSL_AEAD;
321}
322
323void ssl_do_info_callback(const SSL *ssl, int type, int value) {
324 void (*cb)(const SSL *ssl, int type, int value) = NULL;
325 if (ssl->info_callback != NULL) {
326 cb = ssl->info_callback;
327 } else if (ssl->ctx->info_callback != NULL) {
328 cb = ssl->ctx->info_callback;
329 }
330
331 if (cb != NULL) {
332 cb(ssl, type, value);
333 }
334}
335
336void ssl_do_msg_callback(SSL *ssl, int is_write, int content_type,
337 const void *buf, size_t len) {
338 if (ssl->msg_callback == NULL) {
339 return;
340 }
341
342 /* |version| is zero when calling for |SSL3_RT_HEADER| and |SSL2_VERSION| for
343 * a V2ClientHello. */
344 int version;
345 switch (content_type) {
346 case 0:
347 /* V2ClientHello */
348 version = SSL2_VERSION;
349 break;
350 case SSL3_RT_HEADER:
351 version = 0;
352 break;
353 default:
354 version = SSL_version(ssl);
355 }
356
357 ssl->msg_callback(is_write, version, content_type, buf, len, ssl,
358 ssl->msg_callback_arg);
359}
360
361void ssl_get_current_time(const SSL *ssl, struct OPENSSL_timeval *out_clock) {
362 if (ssl->ctx->current_time_cb != NULL) {
363 /* TODO(davidben): Update current_time_cb to use OPENSSL_timeval. See
364 * https://crbug.com/boringssl/155. */
365 struct timeval clock;
366 ssl->ctx->current_time_cb(ssl, &clock);
367 if (clock.tv_sec < 0) {
368 assert(0);
369 out_clock->tv_sec = 0;
370 out_clock->tv_usec = 0;
371 } else {
372 out_clock->tv_sec = (uint64_t)clock.tv_sec;
373 out_clock->tv_usec = (uint32_t)clock.tv_usec;
374 }
375 return;
376 }
377
378#if defined(BORINGSSL_UNSAFE_DETERMINISTIC_MODE)
379 out_clock->tv_sec = 1234;
380 out_clock->tv_usec = 1234;
381#elif defined(OPENSSL_WINDOWS)
382 struct _timeb time;
383 _ftime(&time);
384 if (time.time < 0) {
385 assert(0);
386 out_clock->tv_sec = 0;
387 out_clock->tv_usec = 0;
388 } else {
389 out_clock->tv_sec = time.time;
390 out_clock->tv_usec = time.millitm * 1000;
391 }
392#else
393 struct timeval clock;
394 gettimeofday(&clock, NULL);
395 if (clock.tv_sec < 0) {
396 assert(0);
397 out_clock->tv_sec = 0;
398 out_clock->tv_usec = 0;
399 } else {
400 out_clock->tv_sec = (uint64_t)clock.tv_sec;
401 out_clock->tv_usec = (uint32_t)clock.tv_usec;
402 }
403#endif
404}
405
406} // namespace bssl
407
408using namespace bssl;
409
David Benjamin7c6d09b2015-09-05 11:00:19 -0400410int SSL_library_init(void) {
411 CRYPTO_library_init();
412 return 1;
413}
414
Adam Langleye976e432016-02-15 14:01:10 -0800415static uint32_t ssl_session_hash(const SSL_SESSION *sess) {
416 const uint8_t *session_id = sess->session_id;
417
418 uint8_t tmp_storage[sizeof(uint32_t)];
419 if (sess->session_id_length < sizeof(tmp_storage)) {
David Benjamin17cf2cb2016-12-13 01:07:13 -0500420 OPENSSL_memset(tmp_storage, 0, sizeof(tmp_storage));
421 OPENSSL_memcpy(tmp_storage, sess->session_id, sess->session_id_length);
Adam Langleye976e432016-02-15 14:01:10 -0800422 session_id = tmp_storage;
423 }
424
David Benjamin27bbae42015-09-13 00:54:37 -0400425 uint32_t hash =
Adam Langleye976e432016-02-15 14:01:10 -0800426 ((uint32_t)session_id[0]) |
427 ((uint32_t)session_id[1] << 8) |
428 ((uint32_t)session_id[2] << 16) |
429 ((uint32_t)session_id[3] << 24);
David Benjamin7c6d09b2015-09-05 11:00:19 -0400430
David Benjamin27bbae42015-09-13 00:54:37 -0400431 return hash;
432}
433
434/* NB: If this function (or indeed the hash function which uses a sort of
435 * coarser function than this one) is changed, ensure
436 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
437 * able to construct an SSL_SESSION that will collide with any existing session
438 * with a matching session ID. */
439static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
440 if (a->ssl_version != b->ssl_version) {
441 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800442 }
Adam Langley95c29f32014-06-20 12:00:00 -0700443
David Benjamin27bbae42015-09-13 00:54:37 -0400444 if (a->session_id_length != b->session_id_length) {
445 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800446 }
Adam Langley95c29f32014-06-20 12:00:00 -0700447
David Benjamin17cf2cb2016-12-13 01:07:13 -0500448 return OPENSSL_memcmp(a->session_id, b->session_id, a->session_id_length);
David Benjamin27bbae42015-09-13 00:54:37 -0400449}
Adam Langley95c29f32014-06-20 12:00:00 -0700450
David Benjamin27bbae42015-09-13 00:54:37 -0400451SSL_CTX *SSL_CTX_new(const SSL_METHOD *method) {
452 SSL_CTX *ret = NULL;
453
454 if (method == NULL) {
455 OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_METHOD_PASSED);
456 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800457 }
Adam Langley95c29f32014-06-20 12:00:00 -0700458
David Benjaminf5260812017-07-12 17:16:39 -0400459 ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
David Benjamin27bbae42015-09-13 00:54:37 -0400460 if (ret == NULL) {
461 goto err;
David Benjamin62fd1622015-01-11 13:30:01 -0500462 }
463
David Benjamin17cf2cb2016-12-13 01:07:13 -0500464 OPENSSL_memset(ret, 0, sizeof(SSL_CTX));
Adam Langley95c29f32014-06-20 12:00:00 -0700465
David Benjamin27bbae42015-09-13 00:54:37 -0400466 ret->method = method->method;
Adam Langley3509dac2017-02-01 11:59:18 -0800467 ret->x509_method = method->x509_method;
David Benjamin27bbae42015-09-13 00:54:37 -0400468
469 CRYPTO_MUTEX_init(&ret->lock);
470
471 ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
472 ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
473
David Benjamin27bbae42015-09-13 00:54:37 -0400474 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
David Benjamin17b30832017-01-28 14:00:32 -0500475 ret->session_psk_dhe_timeout = SSL_DEFAULT_SESSION_PSK_DHE_TIMEOUT;
David Benjamin27bbae42015-09-13 00:54:37 -0400476
477 ret->references = 1;
478
479 ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
480 ret->verify_mode = SSL_VERIFY_NONE;
Adam Langley3509dac2017-02-01 11:59:18 -0800481 ret->cert = ssl_cert_new(method->x509_method);
David Benjamin27bbae42015-09-13 00:54:37 -0400482 if (ret->cert == NULL) {
483 goto err;
484 }
485
486 ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
487 if (ret->sessions == NULL) {
488 goto err;
489 }
Adam Langley2a3b3432017-02-10 13:56:48 -0800490
491 if (!ret->x509_method->ssl_ctx_new(ret)) {
David Benjamin27bbae42015-09-13 00:54:37 -0400492 goto err;
493 }
494
Matthew Braithwaite6ad20dc2017-02-21 16:41:33 -0800495 if (!SSL_CTX_set_strict_cipher_list(ret, SSL_DEFAULT_CIPHER_LIST)) {
David Benjamin27bbae42015-09-13 00:54:37 -0400496 goto err2;
497 }
498
Adam Langley34b4c822017-02-02 10:57:17 -0800499 ret->client_CA = sk_CRYPTO_BUFFER_new_null();
David Benjamin27bbae42015-09-13 00:54:37 -0400500 if (ret->client_CA == NULL) {
501 goto err;
502 }
503
David Benjamin8a589332015-12-04 23:14:35 -0500504 CRYPTO_new_ex_data(&ret->ex_data);
David Benjamin27bbae42015-09-13 00:54:37 -0400505
506 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
507
508 /* Setup RFC4507 ticket keys */
509 if (!RAND_bytes(ret->tlsext_tick_key_name, 16) ||
510 !RAND_bytes(ret->tlsext_tick_hmac_key, 16) ||
511 !RAND_bytes(ret->tlsext_tick_aes_key, 16)) {
512 ret->options |= SSL_OP_NO_TICKET;
513 }
514
David Benjamina983b4c2016-11-09 14:21:12 -0500515 /* Disable the auto-chaining feature by default. Once this has stuck without
516 * problems, the feature will be removed entirely. */
517 ret->mode = SSL_MODE_NO_AUTO_CHAIN;
518
David Benjamin27bbae42015-09-13 00:54:37 -0400519 /* Lock the SSL_CTX to the specified version, for compatibility with legacy
David Benjamin3cfeb952017-03-01 16:48:38 -0500520 * uses of SSL_METHOD, but we do not set the minimum version for
521 * |SSLv3_method|. */
David Benjamine34bcc92016-09-21 16:53:09 -0400522 if (!SSL_CTX_set_max_proto_version(ret, method->version) ||
David Benjamin3cfeb952017-03-01 16:48:38 -0500523 !SSL_CTX_set_min_proto_version(ret, method->version == SSL3_VERSION
524 ? 0 /* default */
525 : method->version)) {
David Benjamine34bcc92016-09-21 16:53:09 -0400526 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
527 goto err2;
David Benjamin27bbae42015-09-13 00:54:37 -0400528 }
529
530 return ret;
531
532err:
533 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
534err2:
535 SSL_CTX_free(ret);
536 return NULL;
537}
538
Adam Langley84cd1592016-06-30 18:29:44 -0700539int SSL_CTX_up_ref(SSL_CTX *ctx) {
540 CRYPTO_refcount_inc(&ctx->references);
541 return 1;
542}
543
David Benjamin27bbae42015-09-13 00:54:37 -0400544void SSL_CTX_free(SSL_CTX *ctx) {
545 if (ctx == NULL ||
546 !CRYPTO_refcount_dec_and_test_zero(&ctx->references)) {
547 return;
548 }
549
David Benjamin27bbae42015-09-13 00:54:37 -0400550 /* Free internal session cache. However: the remove_cb() may reference the
551 * ex_data of SSL_CTX, thus the ex_data store can only be removed after the
552 * sessions were flushed. As the ex_data handling routines might also touch
553 * the session cache, the most secure solution seems to be: empty (flush) the
554 * cache, then free ex_data, then finally free the cache. (See ticket
555 * [openssl.org #212].) */
556 SSL_CTX_flush_sessions(ctx, 0);
557
558 CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, ctx, &ctx->ex_data);
559
560 CRYPTO_MUTEX_cleanup(&ctx->lock);
561 lh_SSL_SESSION_free(ctx->sessions);
David Benjamin27bbae42015-09-13 00:54:37 -0400562 ssl_cipher_preference_list_free(ctx->cipher_list);
David Benjamin27bbae42015-09-13 00:54:37 -0400563 ssl_cert_free(ctx->cert);
564 sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->client_custom_extensions,
565 SSL_CUSTOM_EXTENSION_free);
566 sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->server_custom_extensions,
567 SSL_CUSTOM_EXTENSION_free);
Adam Langley34b4c822017-02-02 10:57:17 -0800568 sk_CRYPTO_BUFFER_pop_free(ctx->client_CA, CRYPTO_BUFFER_free);
Adam Langley2a3b3432017-02-10 13:56:48 -0800569 ctx->x509_method->ssl_ctx_free(ctx);
David Benjamin27bbae42015-09-13 00:54:37 -0400570 sk_SRTP_PROTECTION_PROFILE_free(ctx->srtp_profiles);
571 OPENSSL_free(ctx->psk_identity_hint);
Steven Valdezce902a92016-05-17 11:47:53 -0400572 OPENSSL_free(ctx->supported_group_list);
David Benjamin27bbae42015-09-13 00:54:37 -0400573 OPENSSL_free(ctx->alpn_client_proto_list);
David Benjamin27bbae42015-09-13 00:54:37 -0400574 EVP_PKEY_free(ctx->tlsext_channel_id_private);
David Benjamin71c21b42017-04-14 17:05:40 -0400575 OPENSSL_free(ctx->verify_sigalgs);
David Benjamin27bbae42015-09-13 00:54:37 -0400576
577 OPENSSL_free(ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -0800578}
Adam Langley95c29f32014-06-20 12:00:00 -0700579
Adam Langleyfcf25832014-12-18 17:42:32 -0800580SSL *SSL_new(SSL_CTX *ctx) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800581 if (ctx == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400582 OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_CTX);
Adam Langleyfcf25832014-12-18 17:42:32 -0800583 return NULL;
584 }
585 if (ctx->method == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400586 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
Adam Langleyfcf25832014-12-18 17:42:32 -0800587 return NULL;
588 }
Adam Langley95c29f32014-06-20 12:00:00 -0700589
David Benjaminf5260812017-07-12 17:16:39 -0400590 SSL *ssl = (SSL *)OPENSSL_malloc(sizeof(SSL));
David Benjamin0d56f882015-12-19 17:05:56 -0500591 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800592 goto err;
593 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500594 OPENSSL_memset(ssl, 0, sizeof(SSL));
Adam Langley95c29f32014-06-20 12:00:00 -0700595
David Benjaminfc08dfc2017-06-20 14:39:32 -0400596 ssl->conf_min_version = ctx->conf_min_version;
597 ssl->conf_max_version = ctx->conf_max_version;
Steven Valdez52586f92017-07-11 15:08:32 -0400598 ssl->tls13_variant = ctx->tls13_variant;
David Benjamin1eb367c2014-12-12 18:17:51 -0500599
Taylor Brandstetter376a0fe2016-05-10 19:30:28 -0700600 /* RFC 6347 states that implementations SHOULD use an initial timer value of
601 * 1 second. */
602 ssl->initial_timeout_duration_ms = 1000;
603
David Benjamin0d56f882015-12-19 17:05:56 -0500604 ssl->options = ctx->options;
605 ssl->mode = ctx->mode;
606 ssl->max_cert_list = ctx->max_cert_list;
Adam Langley95c29f32014-06-20 12:00:00 -0700607
David Benjamin0d56f882015-12-19 17:05:56 -0500608 ssl->cert = ssl_cert_dup(ctx->cert);
609 if (ssl->cert == NULL) {
David Benjamina5a3eeb2015-03-18 20:26:30 -0400610 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -0800611 }
Adam Langley95c29f32014-06-20 12:00:00 -0700612
David Benjamin0d56f882015-12-19 17:05:56 -0500613 ssl->msg_callback = ctx->msg_callback;
614 ssl->msg_callback_arg = ctx->msg_callback_arg;
615 ssl->verify_mode = ctx->verify_mode;
David Benjamin0d56f882015-12-19 17:05:56 -0500616 ssl->verify_callback = ctx->default_verify_callback;
David Benjamin3a1dd462017-07-11 16:13:10 -0400617 ssl->custom_verify_callback = ctx->custom_verify_callback;
David Benjaminbbaf3672016-11-17 10:53:09 +0900618 ssl->retain_only_sha256_of_client_certs =
619 ctx->retain_only_sha256_of_client_certs;
Adam Langley95c29f32014-06-20 12:00:00 -0700620
David Benjamin0d56f882015-12-19 17:05:56 -0500621 ssl->quiet_shutdown = ctx->quiet_shutdown;
622 ssl->max_send_fragment = ctx->max_send_fragment;
Adam Langley95c29f32014-06-20 12:00:00 -0700623
David Benjaminf650c712016-12-22 15:54:51 -0500624 SSL_CTX_up_ref(ctx);
David Benjamin0d56f882015-12-19 17:05:56 -0500625 ssl->ctx = ctx;
David Benjaminf650c712016-12-22 15:54:51 -0500626 SSL_CTX_up_ref(ctx);
David Benjaminbe497062017-03-10 16:08:36 -0500627 ssl->session_ctx = ctx;
Adam Langley95c29f32014-06-20 12:00:00 -0700628
Adam Langley2a3b3432017-02-10 13:56:48 -0800629 if (!ssl->ctx->x509_method->ssl_new(ssl)) {
630 goto err;
631 }
632
Steven Valdezce902a92016-05-17 11:47:53 -0400633 if (ctx->supported_group_list) {
David Benjaminf5260812017-07-12 17:16:39 -0400634 ssl->supported_group_list = (uint16_t *)BUF_memdup(
635 ctx->supported_group_list, ctx->supported_group_list_len * 2);
Steven Valdezce902a92016-05-17 11:47:53 -0400636 if (!ssl->supported_group_list) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800637 goto err;
638 }
Steven Valdezce902a92016-05-17 11:47:53 -0400639 ssl->supported_group_list_len = ctx->supported_group_list_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800640 }
Adam Langley95c29f32014-06-20 12:00:00 -0700641
David Benjamin17b30832017-01-28 14:00:32 -0500642 if (ctx->alpn_client_proto_list) {
David Benjaminf5260812017-07-12 17:16:39 -0400643 ssl->alpn_client_proto_list = (uint8_t *)BUF_memdup(
644 ctx->alpn_client_proto_list, ctx->alpn_client_proto_list_len);
David Benjamin0d56f882015-12-19 17:05:56 -0500645 if (ssl->alpn_client_proto_list == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800646 goto err;
647 }
David Benjamin17b30832017-01-28 14:00:32 -0500648 ssl->alpn_client_proto_list_len = ctx->alpn_client_proto_list_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800649 }
Adam Langley95c29f32014-06-20 12:00:00 -0700650
David Benjamin0d56f882015-12-19 17:05:56 -0500651 ssl->method = ctx->method;
Adam Langley95c29f32014-06-20 12:00:00 -0700652
David Benjamin0d56f882015-12-19 17:05:56 -0500653 if (!ssl->method->ssl_new(ssl)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800654 goto err;
655 }
Adam Langley95c29f32014-06-20 12:00:00 -0700656
David Benjamin0d56f882015-12-19 17:05:56 -0500657 ssl->rwstate = SSL_NOTHING;
Adam Langley95c29f32014-06-20 12:00:00 -0700658
David Benjamin0d56f882015-12-19 17:05:56 -0500659 CRYPTO_new_ex_data(&ssl->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700660
David Benjamin0d56f882015-12-19 17:05:56 -0500661 ssl->psk_identity_hint = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800662 if (ctx->psk_identity_hint) {
David Benjamin0d56f882015-12-19 17:05:56 -0500663 ssl->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
664 if (ssl->psk_identity_hint == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800665 goto err;
666 }
667 }
David Benjamin0d56f882015-12-19 17:05:56 -0500668 ssl->psk_client_callback = ctx->psk_client_callback;
669 ssl->psk_server_callback = ctx->psk_server_callback;
Adam Langley95c29f32014-06-20 12:00:00 -0700670
David Benjamin0d56f882015-12-19 17:05:56 -0500671 ssl->tlsext_channel_id_enabled = ctx->tlsext_channel_id_enabled;
David Benjamin02ddbfd2015-01-11 13:09:11 -0500672 if (ctx->tlsext_channel_id_private) {
Adam Langley310d3f62016-07-12 10:39:20 -0700673 EVP_PKEY_up_ref(ctx->tlsext_channel_id_private);
674 ssl->tlsext_channel_id_private = ctx->tlsext_channel_id_private;
David Benjamin02ddbfd2015-01-11 13:09:11 -0500675 }
676
David Benjamin17b30832017-01-28 14:00:32 -0500677 ssl->signed_cert_timestamps_enabled = ctx->signed_cert_timestamps_enabled;
678 ssl->ocsp_stapling_enabled = ctx->ocsp_stapling_enabled;
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200679
David Benjamin0d56f882015-12-19 17:05:56 -0500680 return ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -0800681
Adam Langley95c29f32014-06-20 12:00:00 -0700682err:
David Benjamin0d56f882015-12-19 17:05:56 -0500683 SSL_free(ssl);
David Benjamin3570d732015-06-29 00:28:17 -0400684 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700685
Adam Langleyfcf25832014-12-18 17:42:32 -0800686 return NULL;
687}
Adam Langley95c29f32014-06-20 12:00:00 -0700688
David Benjamin27bbae42015-09-13 00:54:37 -0400689void SSL_free(SSL *ssl) {
690 if (ssl == NULL) {
691 return;
692 }
693
David Benjamin0391f162017-06-15 15:27:59 -0400694 if (ssl->ctx != NULL) {
695 ssl->ctx->x509_method->ssl_free(ssl);
696 }
697
David Benjamin27bbae42015-09-13 00:54:37 -0400698 CRYPTO_free_ex_data(&g_ex_data_class_ssl, ssl, &ssl->ex_data);
699
David Benjamin27bbae42015-09-13 00:54:37 -0400700 BIO_free_all(ssl->rbio);
David Benjaminf715c422016-06-11 19:01:56 -0400701 BIO_free_all(ssl->wbio);
David Benjamin27bbae42015-09-13 00:54:37 -0400702
703 BUF_MEM_free(ssl->init_buf);
704
705 /* add extra stuff */
706 ssl_cipher_preference_list_free(ssl->cipher_list);
David Benjamin27bbae42015-09-13 00:54:37 -0400707
David Benjamin27bbae42015-09-13 00:54:37 -0400708 SSL_SESSION_free(ssl->session);
709
David Benjamin27bbae42015-09-13 00:54:37 -0400710 ssl_cert_free(ssl->cert);
711
712 OPENSSL_free(ssl->tlsext_hostname);
David Benjaminbe497062017-03-10 16:08:36 -0500713 SSL_CTX_free(ssl->session_ctx);
Steven Valdezce902a92016-05-17 11:47:53 -0400714 OPENSSL_free(ssl->supported_group_list);
David Benjamin27bbae42015-09-13 00:54:37 -0400715 OPENSSL_free(ssl->alpn_client_proto_list);
716 EVP_PKEY_free(ssl->tlsext_channel_id_private);
717 OPENSSL_free(ssl->psk_identity_hint);
Adam Langley34b4c822017-02-02 10:57:17 -0800718 sk_CRYPTO_BUFFER_pop_free(ssl->client_CA, CRYPTO_BUFFER_free);
David Benjamin27bbae42015-09-13 00:54:37 -0400719 sk_SRTP_PROTECTION_PROFILE_free(ssl->srtp_profiles);
720
721 if (ssl->method != NULL) {
722 ssl->method->ssl_free(ssl);
723 }
724 SSL_CTX_free(ssl->ctx);
725
726 OPENSSL_free(ssl);
727}
728
729void SSL_set_connect_state(SSL *ssl) {
730 ssl->server = 0;
David Benjamin9f1dc822016-06-07 17:03:46 -0400731 ssl->handshake_func = ssl3_connect;
David Benjamin27bbae42015-09-13 00:54:37 -0400732}
733
734void SSL_set_accept_state(SSL *ssl) {
735 ssl->server = 1;
David Benjamin9f1dc822016-06-07 17:03:46 -0400736 ssl->handshake_func = ssl3_accept;
David Benjamin27bbae42015-09-13 00:54:37 -0400737}
738
David Benjamin4501bd52016-08-01 13:39:41 -0400739void SSL_set0_rbio(SSL *ssl, BIO *rbio) {
David Benjaminf715c422016-06-11 19:01:56 -0400740 BIO_free_all(ssl->rbio);
741 ssl->rbio = rbio;
742}
743
David Benjamin4501bd52016-08-01 13:39:41 -0400744void SSL_set0_wbio(SSL *ssl, BIO *wbio) {
David Benjaminf715c422016-06-11 19:01:56 -0400745 BIO_free_all(ssl->wbio);
David Benjamin27bbae42015-09-13 00:54:37 -0400746 ssl->wbio = wbio;
747}
748
David Benjaminf715c422016-06-11 19:01:56 -0400749void SSL_set_bio(SSL *ssl, BIO *rbio, BIO *wbio) {
750 /* For historical reasons, this function has many different cases in ownership
751 * handling. */
752
David Benjamin4501bd52016-08-01 13:39:41 -0400753 /* If nothing has changed, do nothing */
754 if (rbio == SSL_get_rbio(ssl) && wbio == SSL_get_wbio(ssl)) {
755 return;
756 }
757
David Benjaminf715c422016-06-11 19:01:56 -0400758 /* If the two arguments are equal, one fewer reference is granted than
759 * taken. */
760 if (rbio != NULL && rbio == wbio) {
761 BIO_up_ref(rbio);
762 }
763
David Benjamin4501bd52016-08-01 13:39:41 -0400764 /* If only the wbio is changed, adopt only one reference. */
David Benjaminf715c422016-06-11 19:01:56 -0400765 if (rbio == SSL_get_rbio(ssl)) {
David Benjamin4501bd52016-08-01 13:39:41 -0400766 SSL_set0_wbio(ssl, wbio);
David Benjaminf715c422016-06-11 19:01:56 -0400767 return;
768 }
David Benjamin4501bd52016-08-01 13:39:41 -0400769
770 /* There is an asymmetry here for historical reasons. If only the rbio is
771 * changed AND the rbio and wbio were originally different, then we only adopt
772 * one reference. */
773 if (wbio == SSL_get_wbio(ssl) && SSL_get_rbio(ssl) != SSL_get_wbio(ssl)) {
774 SSL_set0_rbio(ssl, rbio);
David Benjaminf715c422016-06-11 19:01:56 -0400775 return;
776 }
777
778 /* Otherwise, adopt both references. */
David Benjamin4501bd52016-08-01 13:39:41 -0400779 SSL_set0_rbio(ssl, rbio);
780 SSL_set0_wbio(ssl, wbio);
David Benjaminf715c422016-06-11 19:01:56 -0400781}
782
David Benjamin27bbae42015-09-13 00:54:37 -0400783BIO *SSL_get_rbio(const SSL *ssl) { return ssl->rbio; }
784
David Benjamindaf207a2017-01-03 18:37:41 -0500785BIO *SSL_get_wbio(const SSL *ssl) { return ssl->wbio; }
David Benjamin27bbae42015-09-13 00:54:37 -0400786
David Benjamin2be4aa72017-01-02 07:56:51 -0500787int SSL_do_handshake(SSL *ssl) {
788 ssl_reset_error_state(ssl);
David Benjamin15c14882016-03-14 14:25:46 -0400789
David Benjamin27bbae42015-09-13 00:54:37 -0400790 if (ssl->handshake_func == NULL) {
791 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_TYPE_NOT_SET);
792 return -1;
793 }
794
795 if (!SSL_in_init(ssl)) {
796 return 1;
797 }
798
David Benjamin2644a132016-12-11 13:41:17 -0500799 if (ssl->s3->hs == NULL) {
800 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
801 return -1;
David Benjamince8c9d22016-11-14 10:45:16 +0900802 }
803
804 /* Run the handshake. */
805 assert(ssl->s3->hs != NULL);
806 int ret = ssl->handshake_func(ssl->s3->hs);
807 if (ret <= 0) {
808 return ret;
809 }
810
811 /* Destroy the handshake object if the handshake has completely finished. */
812 if (!SSL_in_init(ssl)) {
813 ssl_handshake_free(ssl->s3->hs);
814 ssl->s3->hs = NULL;
815 }
816
817 return 1;
David Benjamin27bbae42015-09-13 00:54:37 -0400818}
819
820int SSL_connect(SSL *ssl) {
David Benjaminb2a73182016-03-10 01:25:26 -0500821 if (ssl->handshake_func == NULL) {
David Benjamin27bbae42015-09-13 00:54:37 -0400822 /* Not properly initialized yet */
823 SSL_set_connect_state(ssl);
824 }
825
David Benjamin15c14882016-03-14 14:25:46 -0400826 return SSL_do_handshake(ssl);
David Benjamin27bbae42015-09-13 00:54:37 -0400827}
828
829int SSL_accept(SSL *ssl) {
David Benjaminb2a73182016-03-10 01:25:26 -0500830 if (ssl->handshake_func == NULL) {
David Benjamin27bbae42015-09-13 00:54:37 -0400831 /* Not properly initialized yet */
832 SSL_set_accept_state(ssl);
833 }
834
David Benjamin15c14882016-03-14 14:25:46 -0400835 return SSL_do_handshake(ssl);
David Benjamin27bbae42015-09-13 00:54:37 -0400836}
837
David Benjamin163f29a2016-07-28 11:05:58 -0400838static int ssl_do_renegotiate(SSL *ssl) {
David Benjamin34941c02016-10-08 11:45:31 -0400839 /* We do not accept renegotiations as a server or SSL 3.0. SSL 3.0 will be
840 * removed entirely in the future and requires retaining more data for
841 * renegotiation_info. */
842 if (ssl->server || ssl->version == SSL3_VERSION) {
David Benjamin163f29a2016-07-28 11:05:58 -0400843 goto no_renegotiation;
844 }
845
846 if (ssl->s3->tmp.message_type != SSL3_MT_HELLO_REQUEST ||
847 ssl->init_num != 0) {
848 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
849 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HELLO_REQUEST);
850 return 0;
851 }
852
853 switch (ssl->renegotiate_mode) {
854 case ssl_renegotiate_ignore:
855 /* Ignore the HelloRequest. */
856 return 1;
857
858 case ssl_renegotiate_once:
859 if (ssl->s3->total_renegotiations != 0) {
860 goto no_renegotiation;
861 }
862 break;
863
864 case ssl_renegotiate_never:
865 goto no_renegotiation;
866
867 case ssl_renegotiate_freely:
868 break;
869 }
870
871 /* Renegotiation is only supported at quiescent points in the application
872 * protocol, namely in HTTPS, just before reading the HTTP response. Require
873 * the record-layer be idle and avoid complexities of sending a handshake
874 * record while an application_data record is being written. */
875 if (ssl_write_buffer_is_pending(ssl)) {
876 goto no_renegotiation;
877 }
878
879 /* Begin a new handshake. */
David Benjamin2644a132016-12-11 13:41:17 -0500880 if (ssl->s3->hs != NULL) {
881 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
882 return 0;
883 }
884 ssl->s3->hs = ssl_handshake_new(ssl);
885 if (ssl->s3->hs == NULL) {
886 return 0;
887 }
888
David Benjamin163f29a2016-07-28 11:05:58 -0400889 ssl->s3->total_renegotiations++;
David Benjamin163f29a2016-07-28 11:05:58 -0400890 return 1;
891
892no_renegotiation:
893 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_NO_RENEGOTIATION);
894 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION);
895 return 0;
896}
897
898static int ssl_do_post_handshake(SSL *ssl) {
899 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
900 return ssl_do_renegotiate(ssl);
901 }
902
Steven Valdez8e1c7be2016-07-26 12:39:22 -0400903 return tls13_post_handshake(ssl);
David Benjamin163f29a2016-07-28 11:05:58 -0400904}
905
David Benjamin15c14882016-03-14 14:25:46 -0400906static int ssl_read_impl(SSL *ssl, void *buf, int num, int peek) {
David Benjamin2be4aa72017-01-02 07:56:51 -0500907 ssl_reset_error_state(ssl);
David Benjamin15c14882016-03-14 14:25:46 -0400908
David Benjaminb2a73182016-03-10 01:25:26 -0500909 if (ssl->handshake_func == NULL) {
David Benjamin27bbae42015-09-13 00:54:37 -0400910 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
911 return -1;
912 }
913
David Benjamin163f29a2016-07-28 11:05:58 -0400914 for (;;) {
915 /* Complete the current handshake, if any. False Start will cause
916 * |SSL_do_handshake| to return mid-handshake, so this may require multiple
917 * iterations. */
Steven Valdez681eb6a2016-12-19 13:19:29 -0500918 while (!ssl_can_read(ssl)) {
David Benjamin163f29a2016-07-28 11:05:58 -0400919 int ret = SSL_do_handshake(ssl);
920 if (ret < 0) {
921 return ret;
922 }
923 if (ret == 0) {
924 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
925 return -1;
926 }
927 }
928
929 int got_handshake;
David Benjaminf5260812017-07-12 17:16:39 -0400930 int ret = ssl->method->read_app_data(ssl, &got_handshake, (uint8_t *)buf,
931 num, peek);
David Benjamin163f29a2016-07-28 11:05:58 -0400932 if (ret > 0 || !got_handshake) {
Steven Valdez32635b82016-08-16 11:25:03 -0400933 ssl->s3->key_update_count = 0;
David Benjaminc79845c2016-03-10 01:28:00 -0500934 return ret;
935 }
David Benjamin163f29a2016-07-28 11:05:58 -0400936
Steven Valdez681eb6a2016-12-19 13:19:29 -0500937 /* If we received an interrupt in early read (the end_of_early_data alert),
938 * loop again for the handshake to process it. */
939 if (SSL_in_init(ssl)) {
940 continue;
941 }
942
David Benjamin163f29a2016-07-28 11:05:58 -0400943 /* Handle the post-handshake message and try again. */
944 if (!ssl_do_post_handshake(ssl)) {
David Benjaminc79845c2016-03-10 01:28:00 -0500945 return -1;
946 }
David Benjamin163f29a2016-07-28 11:05:58 -0400947 ssl->method->release_current_message(ssl, 1 /* free buffer */);
David Benjaminc79845c2016-03-10 01:28:00 -0500948 }
David Benjamin15c14882016-03-14 14:25:46 -0400949}
950
951int SSL_read(SSL *ssl, void *buf, int num) {
952 return ssl_read_impl(ssl, buf, num, 0 /* consume bytes */);
David Benjamin27bbae42015-09-13 00:54:37 -0400953}
954
955int SSL_peek(SSL *ssl, void *buf, int num) {
David Benjamin15c14882016-03-14 14:25:46 -0400956 return ssl_read_impl(ssl, buf, num, 1 /* peek */);
David Benjamin27bbae42015-09-13 00:54:37 -0400957}
958
959int SSL_write(SSL *ssl, const void *buf, int num) {
David Benjamin2be4aa72017-01-02 07:56:51 -0500960 ssl_reset_error_state(ssl);
David Benjamin15c14882016-03-14 14:25:46 -0400961
David Benjaminb2a73182016-03-10 01:25:26 -0500962 if (ssl->handshake_func == NULL) {
David Benjamin27bbae42015-09-13 00:54:37 -0400963 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
964 return -1;
965 }
966
David Benjaminfa214e42016-05-10 17:03:10 -0400967 if (ssl->s3->send_shutdown != ssl_shutdown_none) {
David Benjamin27bbae42015-09-13 00:54:37 -0400968 OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
969 return -1;
970 }
971
Steven Valdeze831a812017-03-09 14:56:07 -0500972 int ret = 0, needs_handshake = 0;
973 do {
974 /* If necessary, complete the handshake implicitly. */
975 if (!ssl_can_write(ssl)) {
976 ret = SSL_do_handshake(ssl);
977 if (ret < 0) {
978 return ret;
979 }
980 if (ret == 0) {
981 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
982 return -1;
983 }
David Benjamind7ac1432016-03-10 00:41:25 -0500984 }
David Benjamind7ac1432016-03-10 00:41:25 -0500985
David Benjaminf5260812017-07-12 17:16:39 -0400986 ret = ssl->method->write_app_data(ssl, &needs_handshake,
987 (const uint8_t *)buf, num);
Steven Valdeze831a812017-03-09 14:56:07 -0500988 } while (needs_handshake);
989 return ret;
David Benjamin27bbae42015-09-13 00:54:37 -0400990}
991
992int SSL_shutdown(SSL *ssl) {
David Benjamin2be4aa72017-01-02 07:56:51 -0500993 ssl_reset_error_state(ssl);
David Benjamin15c14882016-03-14 14:25:46 -0400994
David Benjaminb2a73182016-03-10 01:25:26 -0500995 if (ssl->handshake_func == NULL) {
David Benjamin27bbae42015-09-13 00:54:37 -0400996 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
997 return -1;
998 }
999
David Benjamin12709db2017-02-17 13:51:50 -05001000 /* If we are in the middle of a handshake, silently succeed. Consumers often
1001 * call this function before |SSL_free|, whether the handshake succeeded or
1002 * not. We assume the caller has already handled failed handshakes. */
Steven Valdeza14934f2016-02-29 10:05:08 -05001003 if (SSL_in_init(ssl)) {
David Benjamin12709db2017-02-17 13:51:50 -05001004 return 1;
Steven Valdeza14934f2016-02-29 10:05:08 -05001005 }
1006
David Benjamin27bbae42015-09-13 00:54:37 -04001007 if (ssl->quiet_shutdown) {
David Benjaminfa214e42016-05-10 17:03:10 -04001008 /* Do nothing if configured not to send a close_notify. */
1009 ssl->s3->send_shutdown = ssl_shutdown_close_notify;
1010 ssl->s3->recv_shutdown = ssl_shutdown_close_notify;
David Benjamin27bbae42015-09-13 00:54:37 -04001011 return 1;
1012 }
1013
David Benjaminfa214e42016-05-10 17:03:10 -04001014 /* This function completes in two stages. It sends a close_notify and then it
1015 * waits for a close_notify to come in. Perform exactly one action and return
1016 * whether or not it succeeds. */
David Benjamin27bbae42015-09-13 00:54:37 -04001017
David Benjaminfa214e42016-05-10 17:03:10 -04001018 if (ssl->s3->send_shutdown != ssl_shutdown_close_notify) {
1019 /* Send a close_notify. */
1020 if (ssl3_send_alert(ssl, SSL3_AL_WARNING, SSL_AD_CLOSE_NOTIFY) <= 0) {
1021 return -1;
David Benjamin27bbae42015-09-13 00:54:37 -04001022 }
1023 } else if (ssl->s3->alert_dispatch) {
David Benjaminfa214e42016-05-10 17:03:10 -04001024 /* Finish sending the close_notify. */
David Benjaminf0ee9072016-06-15 17:44:37 -04001025 if (ssl->method->dispatch_alert(ssl) <= 0) {
David Benjaminfa214e42016-05-10 17:03:10 -04001026 return -1;
David Benjamin27bbae42015-09-13 00:54:37 -04001027 }
David Benjaminfa214e42016-05-10 17:03:10 -04001028 } else if (ssl->s3->recv_shutdown != ssl_shutdown_close_notify) {
1029 /* Wait for the peer's close_notify. */
David Benjaminf0ee9072016-06-15 17:44:37 -04001030 ssl->method->read_close_notify(ssl);
David Benjaminfa214e42016-05-10 17:03:10 -04001031 if (ssl->s3->recv_shutdown != ssl_shutdown_close_notify) {
1032 return -1;
David Benjamin27bbae42015-09-13 00:54:37 -04001033 }
1034 }
1035
David Benjaminfa214e42016-05-10 17:03:10 -04001036 /* Return 0 for unidirectional shutdown and 1 for bidirectional shutdown. */
1037 return ssl->s3->recv_shutdown == ssl_shutdown_close_notify;
David Benjamin27bbae42015-09-13 00:54:37 -04001038}
1039
David Benjamin1d4f4c02016-07-26 18:03:08 -04001040int SSL_send_fatal_alert(SSL *ssl, uint8_t alert) {
1041 if (ssl->s3->alert_dispatch) {
1042 if (ssl->s3->send_alert[0] != SSL3_AL_FATAL ||
1043 ssl->s3->send_alert[1] != alert) {
1044 /* We are already attempting to write a different alert. */
1045 OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
1046 return -1;
1047 }
1048 return ssl->method->dispatch_alert(ssl);
1049 }
1050
1051 return ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
1052}
1053
Steven Valdez08b65f42016-12-07 15:29:45 -05001054void SSL_CTX_set_early_data_enabled(SSL_CTX *ctx, int enabled) {
Alessandro Ghedini67bb45f2017-03-30 16:33:24 -05001055 ctx->cert->enable_early_data = !!enabled;
1056}
1057
Steven Valdez520e1222017-06-13 12:45:25 -04001058void SSL_CTX_set_tls13_variant(SSL_CTX *ctx, enum tls13_variant_t variant) {
1059 ctx->tls13_variant = variant;
1060}
1061
Steven Valdez52586f92017-07-11 15:08:32 -04001062void SSL_set_tls13_variant(SSL *ssl, enum tls13_variant_t variant) {
1063 ssl->tls13_variant = variant;
1064}
1065
Alessandro Ghedini67bb45f2017-03-30 16:33:24 -05001066void SSL_set_early_data_enabled(SSL *ssl, int enabled) {
1067 ssl->cert->enable_early_data = !!enabled;
Steven Valdez08b65f42016-12-07 15:29:45 -05001068}
1069
Steven Valdeze831a812017-03-09 14:56:07 -05001070int SSL_in_early_data(const SSL *ssl) {
1071 if (ssl->s3->hs == NULL) {
1072 return 0;
1073 }
1074 return ssl->s3->hs->in_early_data;
1075}
1076
Steven Valdez2d850622017-01-11 11:34:52 -05001077int SSL_early_data_accepted(const SSL *ssl) {
1078 return ssl->early_data_accepted;
1079}
1080
Steven Valdeze831a812017-03-09 14:56:07 -05001081void SSL_reset_early_data_reject(SSL *ssl) {
1082 SSL_HANDSHAKE *hs = ssl->s3->hs;
1083 if (hs == NULL ||
1084 hs->wait != ssl_hs_early_data_rejected) {
1085 abort();
1086 }
1087
1088 hs->wait = ssl_hs_ok;
1089 hs->in_early_data = 0;
1090 SSL_SESSION_free(hs->early_session);
1091 hs->early_session = NULL;
1092
1093 /* Discard any unfinished writes from the perspective of |SSL_write|'s
1094 * retry. The handshake will transparently flush out the pending record
1095 * (discarded by the server) to keep the framing correct. */
1096 ssl->s3->wpend_pending = 0;
1097}
1098
David Benjamin287fc4f2016-12-10 15:12:03 -05001099static int bio_retry_reason_to_error(int reason) {
1100 switch (reason) {
1101 case BIO_RR_CONNECT:
1102 return SSL_ERROR_WANT_CONNECT;
1103 case BIO_RR_ACCEPT:
1104 return SSL_ERROR_WANT_ACCEPT;
1105 default:
1106 return SSL_ERROR_SYSCALL;
1107 }
1108}
David Benjamin27bbae42015-09-13 00:54:37 -04001109
David Benjamin287fc4f2016-12-10 15:12:03 -05001110int SSL_get_error(const SSL *ssl, int ret_code) {
David Benjamin27bbae42015-09-13 00:54:37 -04001111 if (ret_code > 0) {
1112 return SSL_ERROR_NONE;
1113 }
1114
1115 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
1116 * where we do encode the error */
David Benjamin287fc4f2016-12-10 15:12:03 -05001117 uint32_t err = ERR_peek_error();
David Benjamin27bbae42015-09-13 00:54:37 -04001118 if (err != 0) {
1119 if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
1120 return SSL_ERROR_SYSCALL;
1121 }
1122 return SSL_ERROR_SSL;
1123 }
1124
1125 if (ret_code == 0) {
David Benjaminfa214e42016-05-10 17:03:10 -04001126 if (ssl->s3->recv_shutdown == ssl_shutdown_close_notify) {
David Benjamin27bbae42015-09-13 00:54:37 -04001127 return SSL_ERROR_ZERO_RETURN;
1128 }
1129 /* An EOF was observed which violates the protocol, and the underlying
1130 * transport does not participate in the error queue. Bubble up to the
1131 * caller. */
1132 return SSL_ERROR_SYSCALL;
1133 }
1134
David Benjamin287fc4f2016-12-10 15:12:03 -05001135 switch (ssl->rwstate) {
1136 case SSL_PENDING_SESSION:
1137 return SSL_ERROR_PENDING_SESSION;
David Benjamin27bbae42015-09-13 00:54:37 -04001138
David Benjamin287fc4f2016-12-10 15:12:03 -05001139 case SSL_CERTIFICATE_SELECTION_PENDING:
1140 return SSL_ERROR_PENDING_CERTIFICATE;
David Benjamin27bbae42015-09-13 00:54:37 -04001141
David Benjamin287fc4f2016-12-10 15:12:03 -05001142 case SSL_READING: {
1143 BIO *bio = SSL_get_rbio(ssl);
1144 if (BIO_should_read(bio)) {
1145 return SSL_ERROR_WANT_READ;
David Benjamin27bbae42015-09-13 00:54:37 -04001146 }
1147
David Benjamin287fc4f2016-12-10 15:12:03 -05001148 if (BIO_should_write(bio)) {
1149 /* TODO(davidben): OpenSSL historically checked for writes on the read
1150 * BIO. Can this be removed? */
1151 return SSL_ERROR_WANT_WRITE;
David Benjamin27bbae42015-09-13 00:54:37 -04001152 }
1153
David Benjamin287fc4f2016-12-10 15:12:03 -05001154 if (BIO_should_io_special(bio)) {
1155 return bio_retry_reason_to_error(BIO_get_retry_reason(bio));
David Benjamin27bbae42015-09-13 00:54:37 -04001156 }
1157
David Benjamin287fc4f2016-12-10 15:12:03 -05001158 break;
1159 }
1160
1161 case SSL_WRITING: {
1162 BIO *bio = SSL_get_wbio(ssl);
1163 if (BIO_should_write(bio)) {
1164 return SSL_ERROR_WANT_WRITE;
David Benjamin27bbae42015-09-13 00:54:37 -04001165 }
1166
David Benjamin287fc4f2016-12-10 15:12:03 -05001167 if (BIO_should_read(bio)) {
1168 /* TODO(davidben): OpenSSL historically checked for reads on the write
1169 * BIO. Can this be removed? */
1170 return SSL_ERROR_WANT_READ;
1171 }
1172
1173 if (BIO_should_io_special(bio)) {
1174 return bio_retry_reason_to_error(BIO_get_retry_reason(bio));
1175 }
1176
1177 break;
David Benjamin27bbae42015-09-13 00:54:37 -04001178 }
David Benjamin27bbae42015-09-13 00:54:37 -04001179
David Benjamin287fc4f2016-12-10 15:12:03 -05001180 case SSL_X509_LOOKUP:
1181 return SSL_ERROR_WANT_X509_LOOKUP;
David Benjamin27bbae42015-09-13 00:54:37 -04001182
David Benjamin287fc4f2016-12-10 15:12:03 -05001183 case SSL_CHANNEL_ID_LOOKUP:
1184 return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
David Benjamin27bbae42015-09-13 00:54:37 -04001185
David Benjamin287fc4f2016-12-10 15:12:03 -05001186 case SSL_PRIVATE_KEY_OPERATION:
1187 return SSL_ERROR_WANT_PRIVATE_KEY_OPERATION;
Adam Langley4c341d02017-03-08 19:33:21 -08001188
1189 case SSL_PENDING_TICKET:
1190 return SSL_ERROR_PENDING_TICKET;
Steven Valdeze831a812017-03-09 14:56:07 -05001191
1192 case SSL_EARLY_DATA_REJECTED:
1193 return SSL_ERROR_EARLY_DATA_REJECTED;
David Benjamin3a1dd462017-07-11 16:13:10 -04001194
1195 case SSL_CERTIFICATE_VERIFY:
1196 return SSL_ERROR_WANT_CERTIFICATE_VERIFY;
David Benjamin27bbae42015-09-13 00:54:37 -04001197 }
1198
1199 return SSL_ERROR_SYSCALL;
1200}
1201
David Benjamin27bbae42015-09-13 00:54:37 -04001202uint32_t SSL_CTX_set_options(SSL_CTX *ctx, uint32_t options) {
1203 ctx->options |= options;
1204 return ctx->options;
1205}
1206
1207uint32_t SSL_CTX_clear_options(SSL_CTX *ctx, uint32_t options) {
1208 ctx->options &= ~options;
1209 return ctx->options;
1210}
1211
1212uint32_t SSL_CTX_get_options(const SSL_CTX *ctx) { return ctx->options; }
1213
1214uint32_t SSL_set_options(SSL *ssl, uint32_t options) {
1215 ssl->options |= options;
1216 return ssl->options;
1217}
1218
1219uint32_t SSL_clear_options(SSL *ssl, uint32_t options) {
1220 ssl->options &= ~options;
1221 return ssl->options;
1222}
1223
1224uint32_t SSL_get_options(const SSL *ssl) { return ssl->options; }
1225
1226uint32_t SSL_CTX_set_mode(SSL_CTX *ctx, uint32_t mode) {
1227 ctx->mode |= mode;
1228 return ctx->mode;
1229}
1230
1231uint32_t SSL_CTX_clear_mode(SSL_CTX *ctx, uint32_t mode) {
1232 ctx->mode &= ~mode;
1233 return ctx->mode;
1234}
1235
1236uint32_t SSL_CTX_get_mode(const SSL_CTX *ctx) { return ctx->mode; }
1237
1238uint32_t SSL_set_mode(SSL *ssl, uint32_t mode) {
1239 ssl->mode |= mode;
1240 return ssl->mode;
1241}
1242
1243uint32_t SSL_clear_mode(SSL *ssl, uint32_t mode) {
1244 ssl->mode &= ~mode;
1245 return ssl->mode;
1246}
1247
1248uint32_t SSL_get_mode(const SSL *ssl) { return ssl->mode; }
1249
Adam Langleyd519bf62016-12-12 11:16:44 -08001250void SSL_CTX_set0_buffer_pool(SSL_CTX *ctx, CRYPTO_BUFFER_POOL *pool) {
1251 ctx->pool = pool;
1252}
1253
David Benjaminee0c8272015-09-13 01:03:54 -04001254int SSL_get_tls_unique(const SSL *ssl, uint8_t *out, size_t *out_len,
1255 size_t max_out) {
David Benjaminf5260812017-07-12 17:16:39 -04001256 *out_len = 0;
1257 OPENSSL_memset(out, 0, max_out);
1258
David Benjaminced00b42016-10-08 11:24:09 -04001259 /* tls-unique is not defined for SSL 3.0 or TLS 1.3. */
1260 if (!ssl->s3->initial_handshake_complete ||
1261 ssl3_protocol_version(ssl) < TLS1_VERSION ||
1262 ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjaminf5260812017-07-12 17:16:39 -04001263 return 0;
David Benjaminced00b42016-10-08 11:24:09 -04001264 }
1265
David Benjaminee0c8272015-09-13 01:03:54 -04001266 /* The tls-unique value is the first Finished message in the handshake, which
1267 * is the client's in a full handshake and the server's for a resumption. See
1268 * https://tools.ietf.org/html/rfc5929#section-3.1. */
1269 const uint8_t *finished = ssl->s3->previous_client_finished;
1270 size_t finished_len = ssl->s3->previous_client_finished_len;
Steven Valdez87eab492016-06-27 16:34:59 -04001271 if (ssl->session != NULL) {
David Benjaminee0c8272015-09-13 01:03:54 -04001272 /* tls-unique is broken for resumed sessions unless EMS is used. */
1273 if (!ssl->session->extended_master_secret) {
David Benjaminf5260812017-07-12 17:16:39 -04001274 return 0;
David Benjaminee0c8272015-09-13 01:03:54 -04001275 }
1276 finished = ssl->s3->previous_server_finished;
1277 finished_len = ssl->s3->previous_server_finished_len;
1278 }
1279
David Benjaminee0c8272015-09-13 01:03:54 -04001280 *out_len = finished_len;
1281 if (finished_len > max_out) {
1282 *out_len = max_out;
1283 }
1284
David Benjamin17cf2cb2016-12-13 01:07:13 -05001285 OPENSSL_memcpy(out, finished, *out_len);
David Benjaminee0c8272015-09-13 01:03:54 -04001286 return 1;
David Benjaminee0c8272015-09-13 01:03:54 -04001287}
1288
David Benjamin5960a902017-02-14 20:07:11 -05001289static int set_session_id_context(CERT *cert, const uint8_t *sid_ctx,
David Benjamin30c4c302016-12-07 22:35:24 -05001290 size_t sid_ctx_len) {
David Benjamin5960a902017-02-14 20:07:11 -05001291 if (sid_ctx_len > sizeof(cert->sid_ctx)) {
David Benjamin3570d732015-06-29 00:28:17 -04001292 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -08001293 return 0;
1294 }
David Benjamin30c4c302016-12-07 22:35:24 -05001295
David Benjamina3d76d02017-07-14 19:36:07 -04001296 static_assert(sizeof(cert->sid_ctx) < 256, "sid_ctx too large");
David Benjamin5960a902017-02-14 20:07:11 -05001297 cert->sid_ctx_length = (uint8_t)sid_ctx_len;
1298 OPENSSL_memcpy(cert->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001299 return 1;
1300}
Adam Langley95c29f32014-06-20 12:00:00 -07001301
David Benjamin5960a902017-02-14 20:07:11 -05001302int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
1303 size_t sid_ctx_len) {
1304 return set_session_id_context(ctx->cert, sid_ctx, sid_ctx_len);
1305}
1306
Adam Langleyfcf25832014-12-18 17:42:32 -08001307int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
David Benjamin30c4c302016-12-07 22:35:24 -05001308 size_t sid_ctx_len) {
David Benjamin5960a902017-02-14 20:07:11 -05001309 return set_session_id_context(ssl->cert, sid_ctx, sid_ctx_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001310}
Adam Langley95c29f32014-06-20 12:00:00 -07001311
David Benjamin07820b52016-12-07 21:13:41 -05001312const uint8_t *SSL_get0_session_id_context(const SSL *ssl, size_t *out_len) {
David Benjamin5960a902017-02-14 20:07:11 -05001313 *out_len = ssl->cert->sid_ctx_length;
1314 return ssl->cert->sid_ctx;
David Benjamin07820b52016-12-07 21:13:41 -05001315}
1316
David Benjamin7481d392015-07-05 19:38:46 -04001317void SSL_certs_clear(SSL *ssl) { ssl_cert_clear_certs(ssl->cert); }
Adam Langley95c29f32014-06-20 12:00:00 -07001318
David Benjamin066fe0a2015-10-17 21:11:33 -04001319int SSL_get_fd(const SSL *ssl) { return SSL_get_rfd(ssl); }
Adam Langley95c29f32014-06-20 12:00:00 -07001320
David Benjamin066fe0a2015-10-17 21:11:33 -04001321int SSL_get_rfd(const SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001322 int ret = -1;
David Benjamin066fe0a2015-10-17 21:11:33 -04001323 BIO *b = BIO_find_type(SSL_get_rbio(ssl), BIO_TYPE_DESCRIPTOR);
1324 if (b != NULL) {
1325 BIO_get_fd(b, &ret);
Adam Langleyfcf25832014-12-18 17:42:32 -08001326 }
1327 return ret;
1328}
Adam Langley95c29f32014-06-20 12:00:00 -07001329
David Benjamin066fe0a2015-10-17 21:11:33 -04001330int SSL_get_wfd(const SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001331 int ret = -1;
David Benjamin066fe0a2015-10-17 21:11:33 -04001332 BIO *b = BIO_find_type(SSL_get_wbio(ssl), BIO_TYPE_DESCRIPTOR);
1333 if (b != NULL) {
1334 BIO_get_fd(b, &ret);
Adam Langleyfcf25832014-12-18 17:42:32 -08001335 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001336 return ret;
1337}
Adam Langley95c29f32014-06-20 12:00:00 -07001338
David Benjamin066fe0a2015-10-17 21:11:33 -04001339int SSL_set_fd(SSL *ssl, int fd) {
David Benjamin22ce9b22016-03-06 19:26:52 -05001340 BIO *bio = BIO_new(BIO_s_socket());
Adam Langleyfcf25832014-12-18 17:42:32 -08001341 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001342 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
David Benjamin066fe0a2015-10-17 21:11:33 -04001343 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001344 }
1345 BIO_set_fd(bio, fd, BIO_NOCLOSE);
David Benjamin066fe0a2015-10-17 21:11:33 -04001346 SSL_set_bio(ssl, bio, bio);
1347 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001348}
Adam Langley95c29f32014-06-20 12:00:00 -07001349
David Benjamin066fe0a2015-10-17 21:11:33 -04001350int SSL_set_wfd(SSL *ssl, int fd) {
David Benjamin2f871122016-05-20 14:27:17 -04001351 BIO *rbio = SSL_get_rbio(ssl);
1352 if (rbio == NULL || BIO_method_type(rbio) != BIO_TYPE_SOCKET ||
1353 BIO_get_fd(rbio, NULL) != fd) {
David Benjamin22ce9b22016-03-06 19:26:52 -05001354 BIO *bio = BIO_new(BIO_s_socket());
Adam Langleyfcf25832014-12-18 17:42:32 -08001355 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001356 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
David Benjamin066fe0a2015-10-17 21:11:33 -04001357 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001358 }
1359 BIO_set_fd(bio, fd, BIO_NOCLOSE);
David Benjamin4501bd52016-08-01 13:39:41 -04001360 SSL_set0_wbio(ssl, bio);
Adam Langleyfcf25832014-12-18 17:42:32 -08001361 } else {
David Benjamin4501bd52016-08-01 13:39:41 -04001362 /* Copy the rbio over to the wbio. */
1363 BIO_up_ref(rbio);
1364 SSL_set0_wbio(ssl, rbio);
Adam Langleyfcf25832014-12-18 17:42:32 -08001365 }
1366
David Benjamin066fe0a2015-10-17 21:11:33 -04001367 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001368}
Adam Langley95c29f32014-06-20 12:00:00 -07001369
David Benjamin066fe0a2015-10-17 21:11:33 -04001370int SSL_set_rfd(SSL *ssl, int fd) {
David Benjamin2f871122016-05-20 14:27:17 -04001371 BIO *wbio = SSL_get_wbio(ssl);
1372 if (wbio == NULL || BIO_method_type(wbio) != BIO_TYPE_SOCKET ||
1373 BIO_get_fd(wbio, NULL) != fd) {
David Benjamin22ce9b22016-03-06 19:26:52 -05001374 BIO *bio = BIO_new(BIO_s_socket());
Adam Langleyfcf25832014-12-18 17:42:32 -08001375 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001376 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
David Benjamin066fe0a2015-10-17 21:11:33 -04001377 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001378 }
1379 BIO_set_fd(bio, fd, BIO_NOCLOSE);
David Benjamin4501bd52016-08-01 13:39:41 -04001380 SSL_set0_rbio(ssl, bio);
Adam Langleyfcf25832014-12-18 17:42:32 -08001381 } else {
David Benjamin4501bd52016-08-01 13:39:41 -04001382 /* Copy the wbio over to the rbio. */
1383 BIO_up_ref(wbio);
1384 SSL_set0_rbio(ssl, wbio);
Adam Langleyfcf25832014-12-18 17:42:32 -08001385 }
David Benjamin066fe0a2015-10-17 21:11:33 -04001386 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001387}
Adam Langley95c29f32014-06-20 12:00:00 -07001388
David Benjamin49ddf412016-10-08 11:56:01 -04001389static size_t copy_finished(void *out, size_t out_len, const uint8_t *in,
1390 size_t in_len) {
1391 if (out_len > in_len) {
1392 out_len = in_len;
1393 }
David Benjamin17cf2cb2016-12-13 01:07:13 -05001394 OPENSSL_memcpy(out, in, out_len);
David Benjamin49ddf412016-10-08 11:56:01 -04001395 return in_len;
1396}
1397
David Benjamin1a1b34d2015-10-17 12:51:52 -04001398size_t SSL_get_finished(const SSL *ssl, void *buf, size_t count) {
David Benjaminced00b42016-10-08 11:24:09 -04001399 if (!ssl->s3->initial_handshake_complete ||
1400 ssl3_protocol_version(ssl) < TLS1_VERSION ||
1401 ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1402 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001403 }
1404
David Benjamin49ddf412016-10-08 11:56:01 -04001405 if (ssl->server) {
1406 return copy_finished(buf, count, ssl->s3->previous_server_finished,
1407 ssl->s3->previous_server_finished_len);
David Benjaminced00b42016-10-08 11:24:09 -04001408 }
David Benjamin49ddf412016-10-08 11:56:01 -04001409
1410 return copy_finished(buf, count, ssl->s3->previous_client_finished,
1411 ssl->s3->previous_client_finished_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001412}
Adam Langley95c29f32014-06-20 12:00:00 -07001413
David Benjamin1a1b34d2015-10-17 12:51:52 -04001414size_t SSL_get_peer_finished(const SSL *ssl, void *buf, size_t count) {
David Benjaminced00b42016-10-08 11:24:09 -04001415 if (!ssl->s3->initial_handshake_complete ||
1416 ssl3_protocol_version(ssl) < TLS1_VERSION ||
1417 ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1418 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001419 }
Adam Langley95c29f32014-06-20 12:00:00 -07001420
David Benjamin49ddf412016-10-08 11:56:01 -04001421 if (ssl->server) {
1422 return copy_finished(buf, count, ssl->s3->previous_client_finished,
1423 ssl->s3->previous_client_finished_len);
David Benjaminced00b42016-10-08 11:24:09 -04001424 }
David Benjamin49ddf412016-10-08 11:56:01 -04001425
1426 return copy_finished(buf, count, ssl->s3->previous_server_finished,
1427 ssl->s3->previous_server_finished_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001428}
Adam Langley95c29f32014-06-20 12:00:00 -07001429
David Benjamin59937042015-09-19 13:04:22 -04001430int SSL_get_verify_mode(const SSL *ssl) { return ssl->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -07001431
David Benjamin42fea372015-09-19 01:22:44 -04001432int SSL_get_extms_support(const SSL *ssl) {
David Benjaminfc02b592017-02-17 16:26:01 -05001433 /* TLS 1.3 does not require extended master secret and always reports as
1434 * supporting it. */
David Benjamina68c1182016-07-24 00:14:33 -04001435 if (!ssl->s3->have_version) {
1436 return 0;
1437 }
David Benjaminfc02b592017-02-17 16:26:01 -05001438 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1439 return 1;
1440 }
1441
1442 /* If the initial handshake completed, query the established session. */
1443 if (ssl->s3->established_session != NULL) {
1444 return ssl->s3->established_session->extended_master_secret;
1445 }
1446
1447 /* Otherwise, query the in-progress handshake. */
1448 if (ssl->s3->hs != NULL) {
1449 return ssl->s3->hs->extended_master_secret;
1450 }
1451 assert(0);
1452 return 0;
Matt Braithwaitecd6f54b2015-09-17 12:54:42 -07001453}
1454
David Benjamin9a41d1b2015-05-16 01:30:09 -04001455int SSL_CTX_get_read_ahead(const SSL_CTX *ctx) { return 0; }
Adam Langley95c29f32014-06-20 12:00:00 -07001456
David Benjamin0d56f882015-12-19 17:05:56 -05001457int SSL_get_read_ahead(const SSL *ssl) { return 0; }
Adam Langley95c29f32014-06-20 12:00:00 -07001458
David Benjamin9a41d1b2015-05-16 01:30:09 -04001459void SSL_CTX_set_read_ahead(SSL_CTX *ctx, int yes) { }
David Benjamin61ecccf2015-05-05 09:44:51 -04001460
David Benjamin0d56f882015-12-19 17:05:56 -05001461void SSL_set_read_ahead(SSL *ssl, int yes) { }
David Benjamin61ecccf2015-05-05 09:44:51 -04001462
David Benjamin9f859492015-10-03 10:44:30 -04001463int SSL_pending(const SSL *ssl) {
1464 if (ssl->s3->rrec.type != SSL3_RT_APPLICATION_DATA) {
1465 return 0;
1466 }
1467 return ssl->s3->rrec.length;
Adam Langleyfcf25832014-12-18 17:42:32 -08001468}
Adam Langley95c29f32014-06-20 12:00:00 -07001469
Adam Langley95c29f32014-06-20 12:00:00 -07001470/* Fix this so it checks all the valid key/cert options */
Adam Langleyfcf25832014-12-18 17:42:32 -08001471int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
Adam Langley3a2b47a2017-01-24 13:59:42 -08001472 return ssl_cert_check_private_key(ctx->cert, ctx->cert->privatekey);
Adam Langleyc5ac2b62016-11-07 12:02:35 -08001473}
1474
1475/* Fix this function so that it takes an optional type parameter */
1476int SSL_check_private_key(const SSL *ssl) {
Adam Langley3a2b47a2017-01-24 13:59:42 -08001477 return ssl_cert_check_private_key(ssl->cert, ssl->cert->privatekey);
Adam Langleyfcf25832014-12-18 17:42:32 -08001478}
Adam Langley95c29f32014-06-20 12:00:00 -07001479
David Benjamin42fea372015-09-19 01:22:44 -04001480long SSL_get_default_timeout(const SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001481 return SSL_DEFAULT_SESSION_TIMEOUT;
1482}
Adam Langley95c29f32014-06-20 12:00:00 -07001483
David Benjamin44d3eed2015-05-21 01:29:55 -04001484int SSL_renegotiate(SSL *ssl) {
1485 /* Caller-initiated renegotiation is not supported. */
David Benjamin3570d732015-06-29 00:28:17 -04001486 OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
David Benjamin44d3eed2015-05-21 01:29:55 -04001487 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001488}
Adam Langley95c29f32014-06-20 12:00:00 -07001489
David Benjamin44d3eed2015-05-21 01:29:55 -04001490int SSL_renegotiate_pending(SSL *ssl) {
1491 return SSL_in_init(ssl) && ssl->s3->initial_handshake_complete;
Adam Langleyfcf25832014-12-18 17:42:32 -08001492}
Adam Langley95c29f32014-06-20 12:00:00 -07001493
David Benjamin936aada2016-06-07 19:09:22 -04001494int SSL_total_renegotiations(const SSL *ssl) {
1495 return ssl->s3->total_renegotiations;
1496}
1497
David Benjamin1d0a1942015-04-26 15:35:35 -04001498size_t SSL_CTX_get_max_cert_list(const SSL_CTX *ctx) {
1499 return ctx->max_cert_list;
1500}
1501
1502void SSL_CTX_set_max_cert_list(SSL_CTX *ctx, size_t max_cert_list) {
1503 if (max_cert_list > kMaxHandshakeSize) {
1504 max_cert_list = kMaxHandshakeSize;
1505 }
1506 ctx->max_cert_list = (uint32_t)max_cert_list;
1507}
1508
1509size_t SSL_get_max_cert_list(const SSL *ssl) {
1510 return ssl->max_cert_list;
1511}
1512
1513void SSL_set_max_cert_list(SSL *ssl, size_t max_cert_list) {
1514 if (max_cert_list > kMaxHandshakeSize) {
1515 max_cert_list = kMaxHandshakeSize;
1516 }
1517 ssl->max_cert_list = (uint32_t)max_cert_list;
1518}
1519
Adam Langleyce9d85e2016-01-24 15:58:39 -08001520int SSL_CTX_set_max_send_fragment(SSL_CTX *ctx, size_t max_send_fragment) {
David Benjamin1d0a1942015-04-26 15:35:35 -04001521 if (max_send_fragment < 512) {
1522 max_send_fragment = 512;
1523 }
1524 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1525 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1526 }
1527 ctx->max_send_fragment = (uint16_t)max_send_fragment;
Adam Langleyce9d85e2016-01-24 15:58:39 -08001528
1529 return 1;
David Benjamin1d0a1942015-04-26 15:35:35 -04001530}
1531
Adam Langleyce9d85e2016-01-24 15:58:39 -08001532int SSL_set_max_send_fragment(SSL *ssl, size_t max_send_fragment) {
David Benjamin1d0a1942015-04-26 15:35:35 -04001533 if (max_send_fragment < 512) {
1534 max_send_fragment = 512;
1535 }
1536 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1537 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1538 }
1539 ssl->max_send_fragment = (uint16_t)max_send_fragment;
Adam Langleyce9d85e2016-01-24 15:58:39 -08001540
1541 return 1;
David Benjamin1d0a1942015-04-26 15:35:35 -04001542}
1543
David Benjamincb9cf792015-05-05 09:46:14 -04001544int SSL_set_mtu(SSL *ssl, unsigned mtu) {
David Benjamince079fd2016-08-02 16:22:34 -04001545 if (!SSL_is_dtls(ssl) || mtu < dtls1_min_mtu()) {
David Benjamincb9cf792015-05-05 09:46:14 -04001546 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001547 }
David Benjamincb9cf792015-05-05 09:46:14 -04001548 ssl->d1->mtu = mtu;
1549 return 1;
1550}
1551
1552int SSL_get_secure_renegotiation_support(const SSL *ssl) {
David Benjamind2610042017-01-03 10:49:28 -05001553 if (!ssl->s3->have_version) {
1554 return 0;
1555 }
1556 return ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
1557 ssl->s3->send_connection_binding;
David Benjamincb9cf792015-05-05 09:46:14 -04001558}
1559
Adam Langleyfcf25832014-12-18 17:42:32 -08001560LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) { return ctx->sessions; }
1561
David Benjamin71f7d3d2015-05-05 09:46:38 -04001562size_t SSL_CTX_sess_number(const SSL_CTX *ctx) {
1563 return lh_SSL_SESSION_num_items(ctx->sessions);
1564}
1565
1566unsigned long SSL_CTX_sess_set_cache_size(SSL_CTX *ctx, unsigned long size) {
1567 unsigned long ret = ctx->session_cache_size;
1568 ctx->session_cache_size = size;
1569 return ret;
1570}
1571
1572unsigned long SSL_CTX_sess_get_cache_size(const SSL_CTX *ctx) {
1573 return ctx->session_cache_size;
1574}
1575
1576int SSL_CTX_set_session_cache_mode(SSL_CTX *ctx, int mode) {
1577 int ret = ctx->session_cache_mode;
1578 ctx->session_cache_mode = mode;
1579 return ret;
1580}
1581
1582int SSL_CTX_get_session_cache_mode(const SSL_CTX *ctx) {
1583 return ctx->session_cache_mode;
1584}
1585
David Benjamin936aada2016-06-07 19:09:22 -04001586
1587int SSL_CTX_get_tlsext_ticket_keys(SSL_CTX *ctx, void *out, size_t len) {
1588 if (out == NULL) {
1589 return 48;
1590 }
1591 if (len != 48) {
1592 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH);
1593 return 0;
1594 }
David Benjaminf5260812017-07-12 17:16:39 -04001595 uint8_t *out_bytes = reinterpret_cast<uint8_t *>(out);
David Benjamin17cf2cb2016-12-13 01:07:13 -05001596 OPENSSL_memcpy(out_bytes, ctx->tlsext_tick_key_name, 16);
1597 OPENSSL_memcpy(out_bytes + 16, ctx->tlsext_tick_hmac_key, 16);
1598 OPENSSL_memcpy(out_bytes + 32, ctx->tlsext_tick_aes_key, 16);
David Benjamin936aada2016-06-07 19:09:22 -04001599 return 1;
1600}
1601
1602int SSL_CTX_set_tlsext_ticket_keys(SSL_CTX *ctx, const void *in, size_t len) {
1603 if (in == NULL) {
1604 return 48;
1605 }
1606 if (len != 48) {
1607 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH);
1608 return 0;
1609 }
David Benjaminf5260812017-07-12 17:16:39 -04001610 const uint8_t *in_bytes = reinterpret_cast<const uint8_t *>(in);
David Benjamin17cf2cb2016-12-13 01:07:13 -05001611 OPENSSL_memcpy(ctx->tlsext_tick_key_name, in_bytes, 16);
1612 OPENSSL_memcpy(ctx->tlsext_tick_hmac_key, in_bytes + 16, 16);
1613 OPENSSL_memcpy(ctx->tlsext_tick_aes_key, in_bytes + 32, 16);
David Benjamin936aada2016-06-07 19:09:22 -04001614 return 1;
1615}
1616
1617int SSL_CTX_set_tlsext_ticket_key_cb(
1618 SSL_CTX *ctx, int (*callback)(SSL *ssl, uint8_t *key_name, uint8_t *iv,
1619 EVP_CIPHER_CTX *ctx, HMAC_CTX *hmac_ctx,
1620 int encrypt)) {
1621 ctx->tlsext_ticket_key_cb = callback;
1622 return 1;
1623}
1624
1625int SSL_CTX_set1_curves(SSL_CTX *ctx, const int *curves, size_t curves_len) {
1626 return tls1_set_curves(&ctx->supported_group_list,
1627 &ctx->supported_group_list_len, curves,
1628 curves_len);
1629}
1630
1631int SSL_set1_curves(SSL *ssl, const int *curves, size_t curves_len) {
1632 return tls1_set_curves(&ssl->supported_group_list,
1633 &ssl->supported_group_list_len, curves,
1634 curves_len);
1635}
1636
Alessandro Ghedini5fd18072016-09-28 21:04:25 +01001637int SSL_CTX_set1_curves_list(SSL_CTX *ctx, const char *curves) {
1638 return tls1_set_curves_list(&ctx->supported_group_list,
1639 &ctx->supported_group_list_len, curves);
1640}
1641
1642int SSL_set1_curves_list(SSL *ssl, const char *curves) {
1643 return tls1_set_curves_list(&ssl->supported_group_list,
1644 &ssl->supported_group_list_len, curves);
1645}
1646
David Benjamin9e68f192016-06-30 14:55:33 -04001647uint16_t SSL_get_curve_id(const SSL *ssl) {
1648 /* TODO(davidben): This checks the wrong session if there is a renegotiation in
1649 * progress. */
Steven Valdez87eab492016-06-27 16:34:59 -04001650 SSL_SESSION *session = SSL_get_session(ssl);
David Benjamin4882a6c2016-12-11 02:48:12 -05001651 if (session == NULL) {
David Benjamin9e68f192016-06-30 14:55:33 -04001652 return 0;
1653 }
1654
David Benjamin4882a6c2016-12-11 02:48:12 -05001655 return session->group_id;
David Benjamin9e68f192016-06-30 14:55:33 -04001656}
1657
David Benjamin936aada2016-06-07 19:09:22 -04001658int SSL_CTX_set_tmp_dh(SSL_CTX *ctx, const DH *dh) {
David Benjamin936aada2016-06-07 19:09:22 -04001659 return 1;
1660}
1661
1662int SSL_set_tmp_dh(SSL *ssl, const DH *dh) {
David Benjamin936aada2016-06-07 19:09:22 -04001663 return 1;
1664}
1665
David Benjamin8ebeabf2017-03-02 22:04:07 -05001666OPENSSL_EXPORT STACK_OF(SSL_CIPHER) *SSL_CTX_get_ciphers(const SSL_CTX *ctx) {
1667 return ctx->cipher_list->ciphers;
1668}
1669
David Benjamin32876b32015-09-20 12:17:03 -04001670STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *ssl) {
1671 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001672 return NULL;
1673 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001674
David Benjaminaf3b3d32016-10-31 16:29:57 -04001675 const struct ssl_cipher_preference_list_st *prefs =
1676 ssl_get_cipher_preferences(ssl);
1677 if (prefs == NULL) {
1678 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08001679 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001680
David Benjaminaf3b3d32016-10-31 16:29:57 -04001681 return prefs->ciphers;
Adam Langleyfcf25832014-12-18 17:42:32 -08001682}
Adam Langley95c29f32014-06-20 12:00:00 -07001683
David Benjamin32876b32015-09-20 12:17:03 -04001684const char *SSL_get_cipher_list(const SSL *ssl, int n) {
David Benjamin32876b32015-09-20 12:17:03 -04001685 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001686 return NULL;
1687 }
Adam Langley95c29f32014-06-20 12:00:00 -07001688
David Benjamin8ebeabf2017-03-02 22:04:07 -05001689 STACK_OF(SSL_CIPHER) *sk = SSL_get_ciphers(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001690 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
1691 return NULL;
1692 }
Adam Langley95c29f32014-06-20 12:00:00 -07001693
David Benjamin8ebeabf2017-03-02 22:04:07 -05001694 const SSL_CIPHER *c = sk_SSL_CIPHER_value(sk, n);
Adam Langleyfcf25832014-12-18 17:42:32 -08001695 if (c == NULL) {
1696 return NULL;
1697 }
Adam Langley95c29f32014-06-20 12:00:00 -07001698
Adam Langleyfcf25832014-12-18 17:42:32 -08001699 return c->name;
1700}
David Benjamin5491e3f2014-09-29 19:33:09 -04001701
Adam Langleyfcf25832014-12-18 17:42:32 -08001702int SSL_CTX_set_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 0 /* not strict */);
Matthew Braithwaitea57dcfb2017-02-17 22:08:23 -08001705}
1706
1707int SSL_CTX_set_strict_cipher_list(SSL_CTX *ctx, const char *str) {
Matthew Braithwaite6ad20dc2017-02-21 16:41:33 -08001708 return ssl_create_cipher_list(ctx->method, &ctx->cipher_list, str,
1709 1 /* strict */);
Adam Langleyfcf25832014-12-18 17:42:32 -08001710}
David Benjamin39482a12014-07-20 13:30:15 -04001711
David Benjamin32876b32015-09-20 12:17:03 -04001712int SSL_set_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 0 /* not strict */);
Matthew Braithwaitea57dcfb2017-02-17 22:08:23 -08001715}
1716
1717int SSL_set_strict_cipher_list(SSL *ssl, const char *str) {
Matthew Braithwaite6ad20dc2017-02-21 16:41:33 -08001718 return ssl_create_cipher_list(ssl->ctx->method, &ssl->cipher_list, str,
1719 1 /* strict */);
Adam Langleyfcf25832014-12-18 17:42:32 -08001720}
Adam Langley95c29f32014-06-20 12:00:00 -07001721
David Benjamin07e13842015-10-17 13:48:04 -04001722const char *SSL_get_servername(const SSL *ssl, const int type) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001723 if (type != TLSEXT_NAMETYPE_host_name) {
1724 return NULL;
1725 }
Adam Langley95c29f32014-06-20 12:00:00 -07001726
David Benjamin07e13842015-10-17 13:48:04 -04001727 /* Historically, |SSL_get_servername| was also the configuration getter
1728 * corresponding to |SSL_set_tlsext_host_name|. */
1729 if (ssl->tlsext_hostname != NULL) {
1730 return ssl->tlsext_hostname;
1731 }
1732
David Benjamin4eb95cc2016-11-16 17:08:23 +09001733 /* During the handshake, report the handshake value. */
1734 if (ssl->s3->hs != NULL) {
1735 return ssl->s3->hs->hostname;
1736 }
1737
1738 /* SSL_get_servername may also be called after the handshake to look up the
1739 * SNI value.
1740 *
1741 * TODO(davidben): This is almost unused. Can we remove it? */
Steven Valdez87eab492016-06-27 16:34:59 -04001742 SSL_SESSION *session = SSL_get_session(ssl);
1743 if (session == NULL) {
David Benjamin07e13842015-10-17 13:48:04 -04001744 return NULL;
1745 }
Steven Valdez87eab492016-06-27 16:34:59 -04001746 return session->tlsext_hostname;
Adam Langleyfcf25832014-12-18 17:42:32 -08001747}
Adam Langley95c29f32014-06-20 12:00:00 -07001748
David Benjamin07e13842015-10-17 13:48:04 -04001749int SSL_get_servername_type(const SSL *ssl) {
Steven Valdez87eab492016-06-27 16:34:59 -04001750 SSL_SESSION *session = SSL_get_session(ssl);
1751 if (session == NULL || session->tlsext_hostname == NULL) {
1752 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001753 }
Steven Valdez87eab492016-06-27 16:34:59 -04001754 return TLSEXT_NAMETYPE_host_name;
Adam Langleyfcf25832014-12-18 17:42:32 -08001755}
Adam Langley95c29f32014-06-20 12:00:00 -07001756
David Benjamin3a1dd462017-07-11 16:13:10 -04001757void SSL_CTX_set_custom_verify(
1758 SSL_CTX *ctx, int mode,
1759 enum ssl_verify_result_t (*callback)(SSL *ssl, uint8_t *out_alert)) {
1760 ctx->verify_mode = mode;
1761 ctx->custom_verify_callback = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08001762}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001763
David Benjamin3a1dd462017-07-11 16:13:10 -04001764void SSL_set_custom_verify(
1765 SSL *ssl, int mode,
1766 enum ssl_verify_result_t (*callback)(SSL *ssl, uint8_t *out_alert)) {
1767 ssl->verify_mode = mode;
1768 ssl->custom_verify_callback = callback;
1769}
1770
1771void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
1772 ctx->signed_cert_timestamps_enabled = 1;
Adam Langleyfe366722017-03-07 16:41:04 -08001773}
1774
David Benjamin26e1ff32017-02-14 20:13:00 -05001775void SSL_enable_signed_cert_timestamps(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001776 ssl->signed_cert_timestamps_enabled = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001777}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001778
Adam Langleyfcf25832014-12-18 17:42:32 -08001779void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
1780 ctx->ocsp_stapling_enabled = 1;
1781}
David Benjamin6c7aed02014-08-27 16:42:38 -04001782
David Benjamin26e1ff32017-02-14 20:13:00 -05001783void SSL_enable_ocsp_stapling(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001784 ssl->ocsp_stapling_enabled = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001785}
David Benjamin6c7aed02014-08-27 16:42:38 -04001786
Adam Langleyfcf25832014-12-18 17:42:32 -08001787void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
1788 size_t *out_len) {
Steven Valdez87eab492016-06-27 16:34:59 -04001789 SSL_SESSION *session = SSL_get_session(ssl);
Adam Langley3cb50e02014-08-26 14:00:31 -07001790
Adam Langleyfcf25832014-12-18 17:42:32 -08001791 *out_len = 0;
1792 *out = NULL;
1793 if (ssl->server || !session || !session->tlsext_signed_cert_timestamp_list) {
1794 return;
1795 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001796
Adam Langleyfcf25832014-12-18 17:42:32 -08001797 *out = session->tlsext_signed_cert_timestamp_list;
1798 *out_len = session->tlsext_signed_cert_timestamp_list_length;
1799}
David Benjamin6c7aed02014-08-27 16:42:38 -04001800
Adam Langleyfcf25832014-12-18 17:42:32 -08001801void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
1802 size_t *out_len) {
Steven Valdez87eab492016-06-27 16:34:59 -04001803 SSL_SESSION *session = SSL_get_session(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001804
1805 *out_len = 0;
1806 *out = NULL;
1807 if (ssl->server || !session || !session->ocsp_response) {
1808 return;
1809 }
1810 *out = session->ocsp_response;
1811 *out_len = session->ocsp_response_length;
1812}
David Benjamin6c7aed02014-08-27 16:42:38 -04001813
David Benjamin936aada2016-06-07 19:09:22 -04001814int SSL_set_tlsext_host_name(SSL *ssl, const char *name) {
1815 OPENSSL_free(ssl->tlsext_hostname);
1816 ssl->tlsext_hostname = NULL;
1817
1818 if (name == NULL) {
1819 return 1;
1820 }
1821
1822 size_t len = strlen(name);
1823 if (len == 0 || len > TLSEXT_MAXLEN_host_name) {
1824 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL3_EXT_INVALID_SERVERNAME);
1825 return 0;
1826 }
1827 ssl->tlsext_hostname = BUF_strdup(name);
1828 if (ssl->tlsext_hostname == NULL) {
1829 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1830 return 0;
1831 }
1832 return 1;
1833}
1834
1835int SSL_CTX_set_tlsext_servername_callback(
1836 SSL_CTX *ctx, int (*callback)(SSL *ssl, int *out_alert, void *arg)) {
1837 ctx->tlsext_servername_callback = callback;
1838 return 1;
1839}
1840
1841int SSL_CTX_set_tlsext_servername_arg(SSL_CTX *ctx, void *arg) {
1842 ctx->tlsext_servername_arg = arg;
1843 return 1;
1844}
1845
David Benjaminca743582017-06-15 17:51:35 -04001846int SSL_select_next_proto(uint8_t **out, uint8_t *out_len, const uint8_t *peer,
1847 unsigned peer_len, const uint8_t *supported,
1848 unsigned supported_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001849 const uint8_t *result;
David Benjaminca743582017-06-15 17:51:35 -04001850 int status;
Adam Langley95c29f32014-06-20 12:00:00 -07001851
David Benjaminca743582017-06-15 17:51:35 -04001852 /* For each protocol in peer preference order, see if we support it. */
1853 for (unsigned i = 0; i < peer_len;) {
1854 for (unsigned j = 0; j < supported_len;) {
1855 if (peer[i] == supported[j] &&
1856 OPENSSL_memcmp(&peer[i + 1], &supported[j + 1], peer[i]) == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001857 /* We found a match */
David Benjaminca743582017-06-15 17:51:35 -04001858 result = &peer[i];
Adam Langleyfcf25832014-12-18 17:42:32 -08001859 status = OPENSSL_NPN_NEGOTIATED;
1860 goto found;
1861 }
David Benjaminca743582017-06-15 17:51:35 -04001862 j += supported[j];
Adam Langleyfcf25832014-12-18 17:42:32 -08001863 j++;
1864 }
David Benjaminca743582017-06-15 17:51:35 -04001865 i += peer[i];
Adam Langleyfcf25832014-12-18 17:42:32 -08001866 i++;
1867 }
Adam Langley95c29f32014-06-20 12:00:00 -07001868
David Benjaminca743582017-06-15 17:51:35 -04001869 /* There's no overlap between our protocols and the peer's list. */
1870 result = supported;
Adam Langleyfcf25832014-12-18 17:42:32 -08001871 status = OPENSSL_NPN_NO_OVERLAP;
Adam Langley95c29f32014-06-20 12:00:00 -07001872
Adam Langleyfcf25832014-12-18 17:42:32 -08001873found:
1874 *out = (uint8_t *)result + 1;
David Benjamin977547b2015-09-16 00:25:52 -04001875 *out_len = result[0];
Adam Langleyfcf25832014-12-18 17:42:32 -08001876 return status;
1877}
Adam Langley95c29f32014-06-20 12:00:00 -07001878
David Benjamin977547b2015-09-16 00:25:52 -04001879void SSL_get0_next_proto_negotiated(const SSL *ssl, const uint8_t **out_data,
1880 unsigned *out_len) {
David Benjamin79978df2015-12-25 15:56:49 -05001881 *out_data = ssl->s3->next_proto_negotiated;
David Benjamin05d4c972017-06-21 16:35:10 -04001882 *out_len = ssl->s3->next_proto_negotiated_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001883}
1884
Adam Langleyfcf25832014-12-18 17:42:32 -08001885void SSL_CTX_set_next_protos_advertised_cb(
1886 SSL_CTX *ctx,
David Benjamin977547b2015-09-16 00:25:52 -04001887 int (*cb)(SSL *ssl, const uint8_t **out, unsigned *out_len, void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08001888 void *arg) {
1889 ctx->next_protos_advertised_cb = cb;
1890 ctx->next_protos_advertised_cb_arg = arg;
1891}
Adam Langley95c29f32014-06-20 12:00:00 -07001892
Adam Langleyfcf25832014-12-18 17:42:32 -08001893void SSL_CTX_set_next_proto_select_cb(
David Benjamin977547b2015-09-16 00:25:52 -04001894 SSL_CTX *ctx, int (*cb)(SSL *ssl, uint8_t **out, uint8_t *out_len,
1895 const uint8_t *in, unsigned in_len, void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08001896 void *arg) {
1897 ctx->next_proto_select_cb = cb;
1898 ctx->next_proto_select_cb_arg = arg;
1899}
Adam Langley95c29f32014-06-20 12:00:00 -07001900
Adam Langleyfcf25832014-12-18 17:42:32 -08001901int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
1902 unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001903 OPENSSL_free(ctx->alpn_client_proto_list);
David Benjaminf5260812017-07-12 17:16:39 -04001904 ctx->alpn_client_proto_list = (uint8_t *)BUF_memdup(protos, protos_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001905 if (!ctx->alpn_client_proto_list) {
1906 return 1;
1907 }
1908 ctx->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001909
Adam Langleyfcf25832014-12-18 17:42:32 -08001910 return 0;
1911}
Adam Langley95c29f32014-06-20 12:00:00 -07001912
Adam Langleyfcf25832014-12-18 17:42:32 -08001913int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001914 OPENSSL_free(ssl->alpn_client_proto_list);
David Benjaminf5260812017-07-12 17:16:39 -04001915 ssl->alpn_client_proto_list = (uint8_t *)BUF_memdup(protos, protos_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001916 if (!ssl->alpn_client_proto_list) {
1917 return 1;
1918 }
1919 ssl->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001920
Adam Langleyfcf25832014-12-18 17:42:32 -08001921 return 0;
1922}
Adam Langley95c29f32014-06-20 12:00:00 -07001923
Adam Langleyfcf25832014-12-18 17:42:32 -08001924void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
1925 int (*cb)(SSL *ssl, const uint8_t **out,
David Benjamin8984f1f2015-09-16 00:10:19 -04001926 uint8_t *out_len, const uint8_t *in,
1927 unsigned in_len, void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08001928 void *arg) {
1929 ctx->alpn_select_cb = cb;
1930 ctx->alpn_select_cb_arg = arg;
1931}
Adam Langley95c29f32014-06-20 12:00:00 -07001932
David Benjamin8984f1f2015-09-16 00:10:19 -04001933void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **out_data,
1934 unsigned *out_len) {
Steven Valdeze831a812017-03-09 14:56:07 -05001935 if (SSL_in_early_data(ssl) && !ssl->server) {
1936 *out_data = ssl->s3->hs->early_session->early_alpn;
1937 *out_len = ssl->s3->hs->early_session->early_alpn_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08001938 } else {
Steven Valdeze831a812017-03-09 14:56:07 -05001939 *out_data = ssl->s3->alpn_selected;
David Benjamin8984f1f2015-09-16 00:10:19 -04001940 *out_len = ssl->s3->alpn_selected_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08001941 }
1942}
Adam Langley95c29f32014-06-20 12:00:00 -07001943
David Benjaminc8ff30c2017-04-04 13:52:36 -04001944void SSL_CTX_set_allow_unknown_alpn_protos(SSL_CTX *ctx, int enabled) {
1945 ctx->allow_unknown_alpn_protos = !!enabled;
1946}
David Benjamin936aada2016-06-07 19:09:22 -04001947
David Benjamineebd3c82016-12-06 17:43:58 -05001948void SSL_CTX_set_tls_channel_id_enabled(SSL_CTX *ctx, int enabled) {
1949 ctx->tlsext_channel_id_enabled = !!enabled;
1950}
1951
David Benjamin936aada2016-06-07 19:09:22 -04001952int SSL_CTX_enable_tls_channel_id(SSL_CTX *ctx) {
David Benjamineebd3c82016-12-06 17:43:58 -05001953 SSL_CTX_set_tls_channel_id_enabled(ctx, 1);
David Benjamin936aada2016-06-07 19:09:22 -04001954 return 1;
1955}
1956
David Benjamineebd3c82016-12-06 17:43:58 -05001957void SSL_set_tls_channel_id_enabled(SSL *ssl, int enabled) {
1958 ssl->tlsext_channel_id_enabled = !!enabled;
1959}
1960
David Benjamin936aada2016-06-07 19:09:22 -04001961int SSL_enable_tls_channel_id(SSL *ssl) {
David Benjamineebd3c82016-12-06 17:43:58 -05001962 SSL_set_tls_channel_id_enabled(ssl, 1);
David Benjamin936aada2016-06-07 19:09:22 -04001963 return 1;
1964}
1965
1966static int is_p256_key(EVP_PKEY *private_key) {
1967 const EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(private_key);
1968 return ec_key != NULL &&
1969 EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key)) ==
1970 NID_X9_62_prime256v1;
1971}
1972
1973int SSL_CTX_set1_tls_channel_id(SSL_CTX *ctx, EVP_PKEY *private_key) {
1974 if (!is_p256_key(private_key)) {
1975 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256);
1976 return 0;
1977 }
1978
1979 EVP_PKEY_free(ctx->tlsext_channel_id_private);
Adam Langley310d3f62016-07-12 10:39:20 -07001980 EVP_PKEY_up_ref(private_key);
1981 ctx->tlsext_channel_id_private = private_key;
David Benjamin936aada2016-06-07 19:09:22 -04001982 ctx->tlsext_channel_id_enabled = 1;
1983
1984 return 1;
1985}
1986
1987int SSL_set1_tls_channel_id(SSL *ssl, EVP_PKEY *private_key) {
1988 if (!is_p256_key(private_key)) {
1989 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256);
1990 return 0;
1991 }
1992
1993 EVP_PKEY_free(ssl->tlsext_channel_id_private);
Adam Langley310d3f62016-07-12 10:39:20 -07001994 EVP_PKEY_up_ref(private_key);
1995 ssl->tlsext_channel_id_private = private_key;
David Benjamin936aada2016-06-07 19:09:22 -04001996 ssl->tlsext_channel_id_enabled = 1;
1997
1998 return 1;
1999}
2000
2001size_t SSL_get_tls_channel_id(SSL *ssl, uint8_t *out, size_t max_out) {
2002 if (!ssl->s3->tlsext_channel_id_valid) {
2003 return 0;
2004 }
David Benjamin17cf2cb2016-12-13 01:07:13 -05002005 OPENSSL_memcpy(out, ssl->s3->tlsext_channel_id,
2006 (max_out < 64) ? max_out : 64);
David Benjamin936aada2016-06-07 19:09:22 -04002007 return 64;
2008}
2009
David Benjamin936aada2016-06-07 19:09:22 -04002010size_t SSL_get0_certificate_types(SSL *ssl, const uint8_t **out_types) {
David Benjamina0486782016-10-06 19:11:32 -04002011 if (ssl->server || ssl->s3->hs == NULL) {
David Benjamin936aada2016-06-07 19:09:22 -04002012 *out_types = NULL;
2013 return 0;
2014 }
David Benjamina0486782016-10-06 19:11:32 -04002015 *out_types = ssl->s3->hs->certificate_types;
2016 return ssl->s3->hs->num_certificate_types;
David Benjamin936aada2016-06-07 19:09:22 -04002017}
2018
David Benjamin0d56f882015-12-19 17:05:56 -05002019EVP_PKEY *SSL_get_privatekey(const SSL *ssl) {
2020 if (ssl->cert != NULL) {
2021 return ssl->cert->privatekey;
Adam Langleyfcf25832014-12-18 17:42:32 -08002022 }
2023
2024 return NULL;
2025}
2026
Adam Langleyfcf25832014-12-18 17:42:32 -08002027EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
2028 if (ctx->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04002029 return ctx->cert->privatekey;
Adam Langleyfcf25832014-12-18 17:42:32 -08002030 }
2031
2032 return NULL;
2033}
2034
David Benjamin42fea372015-09-19 01:22:44 -04002035const SSL_CIPHER *SSL_get_current_cipher(const SSL *ssl) {
David Benjamincfc11c22017-07-18 22:45:18 -04002036 return ssl->s3->aead_write_ctx->cipher();
Adam Langleyfcf25832014-12-18 17:42:32 -08002037}
2038
David Benjamin936aada2016-06-07 19:09:22 -04002039int SSL_session_reused(const SSL *ssl) {
Steven Valdeze831a812017-03-09 14:56:07 -05002040 return ssl->s3->session_reused || SSL_in_early_data(ssl);
David Benjamin936aada2016-06-07 19:09:22 -04002041}
2042
David Benjamin0d56f882015-12-19 17:05:56 -05002043const COMP_METHOD *SSL_get_current_compression(SSL *ssl) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002044
David Benjamin0d56f882015-12-19 17:05:56 -05002045const COMP_METHOD *SSL_get_current_expansion(SSL *ssl) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002046
Adam Langley29ec5d12016-03-01 16:12:28 -08002047int *SSL_get_server_tmp_key(SSL *ssl, EVP_PKEY **out_key) { return 0; }
2048
Adam Langleyfcf25832014-12-18 17:42:32 -08002049void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
Adam Langleybb85f3d2015-10-28 18:44:11 -07002050 ctx->quiet_shutdown = (mode != 0);
Adam Langleyfcf25832014-12-18 17:42:32 -08002051}
2052
2053int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
2054 return ctx->quiet_shutdown;
2055}
2056
Adam Langleybb85f3d2015-10-28 18:44:11 -07002057void SSL_set_quiet_shutdown(SSL *ssl, int mode) {
2058 ssl->quiet_shutdown = (mode != 0);
2059}
Adam Langleyfcf25832014-12-18 17:42:32 -08002060
David Benjamin9f859492015-10-03 10:44:30 -04002061int SSL_get_quiet_shutdown(const SSL *ssl) { return ssl->quiet_shutdown; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002062
David Benjamin63006a92015-10-18 00:00:06 -04002063void SSL_set_shutdown(SSL *ssl, int mode) {
2064 /* It is an error to clear any bits that have already been set. (We can't try
2065 * to get a second close_notify or send two.) */
David Benjaminfa214e42016-05-10 17:03:10 -04002066 assert((SSL_get_shutdown(ssl) & mode) == SSL_get_shutdown(ssl));
David Benjamin63006a92015-10-18 00:00:06 -04002067
David Benjaminfa214e42016-05-10 17:03:10 -04002068 if (mode & SSL_RECEIVED_SHUTDOWN &&
2069 ssl->s3->recv_shutdown == ssl_shutdown_none) {
2070 ssl->s3->recv_shutdown = ssl_shutdown_close_notify;
2071 }
2072
2073 if (mode & SSL_SENT_SHUTDOWN &&
2074 ssl->s3->send_shutdown == ssl_shutdown_none) {
2075 ssl->s3->send_shutdown = ssl_shutdown_close_notify;
2076 }
David Benjamin63006a92015-10-18 00:00:06 -04002077}
Adam Langleyfcf25832014-12-18 17:42:32 -08002078
David Benjaminfa214e42016-05-10 17:03:10 -04002079int SSL_get_shutdown(const SSL *ssl) {
2080 int ret = 0;
2081 if (ssl->s3->recv_shutdown != ssl_shutdown_none) {
2082 /* Historically, OpenSSL set |SSL_RECEIVED_SHUTDOWN| on both close_notify
2083 * and fatal alert. */
2084 ret |= SSL_RECEIVED_SHUTDOWN;
2085 }
2086 if (ssl->s3->send_shutdown == ssl_shutdown_close_notify) {
2087 /* Historically, OpenSSL set |SSL_SENT_SHUTDOWN| on only close_notify. */
2088 ret |= SSL_SENT_SHUTDOWN;
2089 }
2090 return ret;
2091}
Adam Langleyfcf25832014-12-18 17:42:32 -08002092
Adam Langleyfcf25832014-12-18 17:42:32 -08002093SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
2094
2095SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
2096 if (ssl->ctx == ctx) {
2097 return ssl->ctx;
2098 }
2099
Adam Langley3509dac2017-02-01 11:59:18 -08002100 /* One cannot change the X.509 callbacks during a connection. */
2101 if (ssl->ctx->x509_method != ctx->x509_method) {
2102 assert(0);
2103 return NULL;
2104 }
2105
Adam Langleyfcf25832014-12-18 17:42:32 -08002106 if (ctx == NULL) {
David Benjaminbe497062017-03-10 16:08:36 -05002107 ctx = ssl->session_ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08002108 }
2109
David Benjamin2755a3e2015-04-22 16:17:58 -04002110 ssl_cert_free(ssl->cert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002111 ssl->cert = ssl_cert_dup(ctx->cert);
David Benjamin2755a3e2015-04-22 16:17:58 -04002112
David Benjaminf650c712016-12-22 15:54:51 -05002113 SSL_CTX_up_ref(ctx);
2114 SSL_CTX_free(ssl->ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002115 ssl->ctx = ctx;
2116
Adam Langleyfcf25832014-12-18 17:42:32 -08002117 return ssl->ctx;
2118}
2119
Adam Langley95c29f32014-06-20 12:00:00 -07002120void SSL_set_info_callback(SSL *ssl,
David Benjamin82170242015-10-17 22:51:17 -04002121 void (*cb)(const SSL *ssl, int type, int value)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002122 ssl->info_callback = cb;
2123}
Adam Langley95c29f32014-06-20 12:00:00 -07002124
David Benjamin82170242015-10-17 22:51:17 -04002125void (*SSL_get_info_callback(const SSL *ssl))(const SSL *ssl, int type,
2126 int value) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002127 return ssl->info_callback;
2128}
Adam Langley95c29f32014-06-20 12:00:00 -07002129
David Benjamincb0c29f2016-12-12 17:00:50 -05002130int SSL_state(const SSL *ssl) {
David Benjamin9e766d72017-02-10 23:14:17 -05002131 return SSL_in_init(ssl) ? SSL_ST_INIT : SSL_ST_OK;
David Benjamincb0c29f2016-12-12 17:00:50 -05002132}
Adam Langley95c29f32014-06-20 12:00:00 -07002133
David Benjaminece089c2015-05-15 23:52:42 -04002134void SSL_set_state(SSL *ssl, int state) { }
Adam Langley95c29f32014-06-20 12:00:00 -07002135
Emily Stark95a79ee2016-02-25 21:12:28 -08002136char *SSL_get_shared_ciphers(const SSL *ssl, char *buf, int len) {
2137 if (len <= 0) {
2138 return NULL;
2139 }
2140 buf[0] = '\0';
2141 return buf;
2142}
2143
David Benjamin8a589332015-12-04 23:14:35 -05002144int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
David Benjamind94682d2017-05-14 17:10:18 -04002145 CRYPTO_EX_dup *dup_unused, CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002146 int index;
2147 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp,
David Benjamind94682d2017-05-14 17:10:18 -04002148 free_func)) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002149 return -1;
2150 }
2151 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002152}
Adam Langley95c29f32014-06-20 12:00:00 -07002153
David Benjamin1ac08fe2015-09-15 01:36:22 -04002154int SSL_set_ex_data(SSL *ssl, int idx, void *arg) {
2155 return CRYPTO_set_ex_data(&ssl->ex_data, idx, arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002156}
Adam Langley95c29f32014-06-20 12:00:00 -07002157
David Benjamin1ac08fe2015-09-15 01:36:22 -04002158void *SSL_get_ex_data(const SSL *ssl, int idx) {
2159 return CRYPTO_get_ex_data(&ssl->ex_data, idx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002160}
Adam Langley95c29f32014-06-20 12:00:00 -07002161
David Benjamin8a589332015-12-04 23:14:35 -05002162int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
David Benjamind94682d2017-05-14 17:10:18 -04002163 CRYPTO_EX_dup *dup_unused,
Adam Langleyfcf25832014-12-18 17:42:32 -08002164 CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002165 int index;
2166 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp,
David Benjamind94682d2017-05-14 17:10:18 -04002167 free_func)) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002168 return -1;
2169 }
2170 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002171}
Adam Langley95c29f32014-06-20 12:00:00 -07002172
David Benjamin1ac08fe2015-09-15 01:36:22 -04002173int SSL_CTX_set_ex_data(SSL_CTX *ctx, int idx, void *arg) {
2174 return CRYPTO_set_ex_data(&ctx->ex_data, idx, arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002175}
Adam Langley95c29f32014-06-20 12:00:00 -07002176
David Benjamin1ac08fe2015-09-15 01:36:22 -04002177void *SSL_CTX_get_ex_data(const SSL_CTX *ctx, int idx) {
2178 return CRYPTO_get_ex_data(&ctx->ex_data, idx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002179}
Adam Langley95c29f32014-06-20 12:00:00 -07002180
David Benjamin93d17492015-10-17 12:43:36 -04002181int SSL_want(const SSL *ssl) { return ssl->rwstate; }
Adam Langley95c29f32014-06-20 12:00:00 -07002182
Adam Langleyfcf25832014-12-18 17:42:32 -08002183void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
2184 RSA *(*cb)(SSL *ssl, int is_export,
2185 int keylength)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002186}
Adam Langley95c29f32014-06-20 12:00:00 -07002187
Adam Langleyfcf25832014-12-18 17:42:32 -08002188void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
2189 int keylength)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002190}
2191
2192void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
David Benjamin59015c32015-04-26 13:13:55 -04002193 DH *(*callback)(SSL *ssl, int is_export,
2194 int keylength)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002195}
2196
David Benjamin59015c32015-04-26 13:13:55 -04002197void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*callback)(SSL *ssl, int is_export,
2198 int keylength)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002199}
2200
David Benjamin0a9bf662017-06-21 16:34:15 -04002201static int use_psk_identity_hint(char **out, const char *identity_hint) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002202 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
David Benjamin3570d732015-06-29 00:28:17 -04002203 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -08002204 return 0;
2205 }
2206
2207 /* Clear currently configured hint, if any. */
David Benjamin0a9bf662017-06-21 16:34:15 -04002208 OPENSSL_free(*out);
2209 *out = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002210
David Benjamin78679342016-09-16 19:42:05 -04002211 /* Treat the empty hint as not supplying one. Plain PSK makes it possible to
2212 * send either no hint (omit ServerKeyExchange) or an empty hint, while
2213 * ECDHE_PSK can only spell empty hint. Having different capabilities is odd,
2214 * so we interpret empty and missing as identical. */
2215 if (identity_hint != NULL && identity_hint[0] != '\0') {
David Benjamin0a9bf662017-06-21 16:34:15 -04002216 *out = BUF_strdup(identity_hint);
2217 if (*out == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002218 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002219 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002220 }
Adam Langley95c29f32014-06-20 12:00:00 -07002221
Adam Langleyfcf25832014-12-18 17:42:32 -08002222 return 1;
2223}
Adam Langley95c29f32014-06-20 12:00:00 -07002224
David Benjamin0a9bf662017-06-21 16:34:15 -04002225int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
2226 return use_psk_identity_hint(&ctx->psk_identity_hint, identity_hint);
2227}
2228
2229int SSL_use_psk_identity_hint(SSL *ssl, const char *identity_hint) {
2230 return use_psk_identity_hint(&ssl->psk_identity_hint, identity_hint);
2231}
2232
David Benjamine8814df2015-09-15 08:05:54 -04002233const char *SSL_get_psk_identity_hint(const SSL *ssl) {
2234 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002235 return NULL;
2236 }
David Benjamine8814df2015-09-15 08:05:54 -04002237 return ssl->psk_identity_hint;
Adam Langleyfcf25832014-12-18 17:42:32 -08002238}
Adam Langley95c29f32014-06-20 12:00:00 -07002239
David Benjamine8814df2015-09-15 08:05:54 -04002240const char *SSL_get_psk_identity(const SSL *ssl) {
Steven Valdez87eab492016-06-27 16:34:59 -04002241 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002242 return NULL;
2243 }
Steven Valdez87eab492016-06-27 16:34:59 -04002244 SSL_SESSION *session = SSL_get_session(ssl);
2245 if (session == NULL) {
2246 return NULL;
2247 }
2248 return session->psk_identity;
Adam Langleyfcf25832014-12-18 17:42:32 -08002249}
Adam Langley95c29f32014-06-20 12:00:00 -07002250
Adam Langleyfcf25832014-12-18 17:42:32 -08002251void SSL_set_psk_client_callback(
David Benjamine8814df2015-09-15 08:05:54 -04002252 SSL *ssl, unsigned (*cb)(SSL *ssl, const char *hint, char *identity,
2253 unsigned max_identity_len, uint8_t *psk,
2254 unsigned max_psk_len)) {
2255 ssl->psk_client_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002256}
Adam Langley95c29f32014-06-20 12:00:00 -07002257
Adam Langleyfcf25832014-12-18 17:42:32 -08002258void SSL_CTX_set_psk_client_callback(
David Benjamine8814df2015-09-15 08:05:54 -04002259 SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *hint, char *identity,
2260 unsigned max_identity_len, uint8_t *psk,
2261 unsigned max_psk_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002262 ctx->psk_client_callback = cb;
2263}
Adam Langley95c29f32014-06-20 12:00:00 -07002264
Adam Langleyfcf25832014-12-18 17:42:32 -08002265void SSL_set_psk_server_callback(
David Benjamine8814df2015-09-15 08:05:54 -04002266 SSL *ssl, unsigned (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
2267 unsigned max_psk_len)) {
2268 ssl->psk_server_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002269}
Adam Langley95c29f32014-06-20 12:00:00 -07002270
Adam Langleyfcf25832014-12-18 17:42:32 -08002271void SSL_CTX_set_psk_server_callback(
David Benjamine8814df2015-09-15 08:05:54 -04002272 SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *identity,
2273 uint8_t *psk, unsigned max_psk_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002274 ctx->psk_server_callback = cb;
2275}
Adam Langley95c29f32014-06-20 12:00:00 -07002276
Adam Langleyfcf25832014-12-18 17:42:32 -08002277void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
2278 void (*cb)(int write_p, int version,
2279 int content_type, const void *buf,
2280 size_t len, SSL *ssl, void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002281 ctx->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002282}
David Benjamin61ecccf2015-05-05 09:44:51 -04002283
2284void SSL_CTX_set_msg_callback_arg(SSL_CTX *ctx, void *arg) {
2285 ctx->msg_callback_arg = arg;
2286}
2287
Adam Langleyfcf25832014-12-18 17:42:32 -08002288void SSL_set_msg_callback(SSL *ssl,
2289 void (*cb)(int write_p, int version, int content_type,
2290 const void *buf, size_t len, SSL *ssl,
2291 void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002292 ssl->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002293}
Adam Langley95c29f32014-06-20 12:00:00 -07002294
David Benjamin61ecccf2015-05-05 09:44:51 -04002295void SSL_set_msg_callback_arg(SSL *ssl, void *arg) {
2296 ssl->msg_callback_arg = arg;
2297}
2298
David Benjamind28f59c2015-11-17 22:32:50 -05002299void SSL_CTX_set_keylog_callback(SSL_CTX *ctx,
2300 void (*cb)(const SSL *ssl, const char *line)) {
2301 ctx->keylog_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002302}
Adam Langley95c29f32014-06-20 12:00:00 -07002303
David Benjamin6e3f5cc2016-09-14 14:43:14 -04002304void (*SSL_CTX_get_keylog_callback(const SSL_CTX *ctx))(const SSL *ssl,
2305 const char *line) {
2306 return ctx->keylog_callback;
2307}
2308
Taylor Brandstetter9edb2c62016-06-08 15:26:59 -07002309void SSL_CTX_set_current_time_cb(SSL_CTX *ctx,
2310 void (*cb)(const SSL *ssl,
2311 struct timeval *out_clock)) {
2312 ctx->current_time_cb = cb;
2313}
2314
David Benjamin5d8b1282015-10-17 23:26:35 -04002315int SSL_is_init_finished(const SSL *ssl) {
David Benjamin9e766d72017-02-10 23:14:17 -05002316 return !SSL_in_init(ssl);
David Benjamin5d8b1282015-10-17 23:26:35 -04002317}
2318
2319int SSL_in_init(const SSL *ssl) {
David Benjamin9e766d72017-02-10 23:14:17 -05002320 SSL_HANDSHAKE *hs = ssl->s3->hs;
2321 return hs != NULL && hs->state != SSL_ST_OK;
David Benjamin5d8b1282015-10-17 23:26:35 -04002322}
2323
2324int SSL_in_false_start(const SSL *ssl) {
David Benjamina0486782016-10-06 19:11:32 -04002325 if (ssl->s3->hs == NULL) {
2326 return 0;
2327 }
2328 return ssl->s3->hs->in_false_start;
David Benjamined7c4752015-02-16 19:16:46 -05002329}
2330
David Benjamin0d56f882015-12-19 17:05:56 -05002331int SSL_cutthrough_complete(const SSL *ssl) {
2332 return SSL_in_false_start(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08002333}
Adam Langley95c29f32014-06-20 12:00:00 -07002334
Adam Langleyfcf25832014-12-18 17:42:32 -08002335void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
2336 size_t *ssl_session_size) {
2337 *ssl_size = sizeof(SSL);
2338 *ssl_ctx_size = sizeof(SSL_CTX);
2339 *ssl_session_size = sizeof(SSL_SESSION);
2340}
Feng Lu41aa3252014-11-21 22:47:56 -08002341
David Benjamince079fd2016-08-02 16:22:34 -04002342int SSL_is_server(const SSL *ssl) { return ssl->server; }
2343
2344int SSL_is_dtls(const SSL *ssl) { return ssl->method->is_dtls; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002345
Alessandro Ghedini57e81e62017-03-14 23:36:00 +00002346void SSL_CTX_set_select_certificate_cb(
2347 SSL_CTX *ctx,
2348 enum ssl_select_cert_result_t (*cb)(const SSL_CLIENT_HELLO *)) {
David Benjamind4c2bce2015-10-17 12:28:18 -04002349 ctx->select_certificate_cb = cb;
2350}
2351
David Benjamin731058e2016-12-03 23:15:13 -05002352void SSL_CTX_set_dos_protection_cb(SSL_CTX *ctx,
2353 int (*cb)(const SSL_CLIENT_HELLO *)) {
Adam Langley524e7172015-02-20 16:04:00 -08002354 ctx->dos_protection_cb = cb;
2355}
2356
David Benjamin1d5ef3b2015-10-12 19:54:18 -04002357void SSL_set_renegotiate_mode(SSL *ssl, enum ssl_renegotiate_mode_t mode) {
2358 ssl->renegotiate_mode = mode;
2359}
2360
Adam Langleyc2d32802015-11-03 18:36:10 -08002361int SSL_get_ivs(const SSL *ssl, const uint8_t **out_read_iv,
2362 const uint8_t **out_write_iv, size_t *out_iv_len) {
Adam Langleyc2d32802015-11-03 18:36:10 -08002363 size_t write_iv_len;
David Benjamincfc11c22017-07-18 22:45:18 -04002364 if (!ssl->s3->aead_read_ctx->GetIV(out_read_iv, out_iv_len) ||
2365 !ssl->s3->aead_write_ctx->GetIV(out_write_iv, &write_iv_len) ||
Adam Langleyc2d32802015-11-03 18:36:10 -08002366 *out_iv_len != write_iv_len) {
2367 return 0;
2368 }
2369
2370 return 1;
David Benjamine348ff42015-11-06 17:55:30 -05002371}
David Benjamin6e807652015-11-02 12:02:20 -05002372
David Benjamin91b25012016-01-21 16:39:58 -05002373static uint64_t be_to_u64(const uint8_t in[8]) {
David Benjaminde942382016-02-11 12:02:01 -05002374 return (((uint64_t)in[0]) << 56) | (((uint64_t)in[1]) << 48) |
2375 (((uint64_t)in[2]) << 40) | (((uint64_t)in[3]) << 32) |
2376 (((uint64_t)in[4]) << 24) | (((uint64_t)in[5]) << 16) |
2377 (((uint64_t)in[6]) << 8) | ((uint64_t)in[7]);
David Benjamin91b25012016-01-21 16:39:58 -05002378}
2379
2380uint64_t SSL_get_read_sequence(const SSL *ssl) {
2381 /* TODO(davidben): Internally represent sequence numbers as uint64_t. */
David Benjamince079fd2016-08-02 16:22:34 -04002382 if (SSL_is_dtls(ssl)) {
David Benjaminde942382016-02-11 12:02:01 -05002383 /* max_seq_num already includes the epoch. */
2384 assert(ssl->d1->r_epoch == (ssl->d1->bitmap.max_seq_num >> 48));
2385 return ssl->d1->bitmap.max_seq_num;
2386 }
David Benjamin91b25012016-01-21 16:39:58 -05002387 return be_to_u64(ssl->s3->read_sequence);
2388}
2389
2390uint64_t SSL_get_write_sequence(const SSL *ssl) {
David Benjaminde942382016-02-11 12:02:01 -05002391 uint64_t ret = be_to_u64(ssl->s3->write_sequence);
David Benjamince079fd2016-08-02 16:22:34 -04002392 if (SSL_is_dtls(ssl)) {
David Benjaminde942382016-02-11 12:02:01 -05002393 assert((ret >> 48) == 0);
2394 ret |= ((uint64_t)ssl->d1->w_epoch) << 48;
2395 }
2396 return ret;
David Benjamin91b25012016-01-21 16:39:58 -05002397}
2398
Steven Valdez02563852016-06-23 13:33:05 -04002399uint16_t SSL_get_peer_signature_algorithm(const SSL *ssl) {
David Benjaminf1050fd2016-12-13 20:05:36 -05002400 /* TODO(davidben): This checks the wrong session if there is a renegotiation
2401 * in progress. */
2402 SSL_SESSION *session = SSL_get_session(ssl);
2403 if (session == NULL) {
2404 return 0;
2405 }
2406
2407 return session->peer_signature_algorithm;
Steven Valdez02563852016-06-23 13:33:05 -04002408}
2409
David Benjamin96ba15f2015-12-25 14:10:17 -05002410size_t SSL_get_client_random(const SSL *ssl, uint8_t *out, size_t max_out) {
2411 if (max_out == 0) {
2412 return sizeof(ssl->s3->client_random);
2413 }
2414 if (max_out > sizeof(ssl->s3->client_random)) {
2415 max_out = sizeof(ssl->s3->client_random);
2416 }
David Benjamin17cf2cb2016-12-13 01:07:13 -05002417 OPENSSL_memcpy(out, ssl->s3->client_random, max_out);
David Benjamin96ba15f2015-12-25 14:10:17 -05002418 return max_out;
2419}
2420
2421size_t SSL_get_server_random(const SSL *ssl, uint8_t *out, size_t max_out) {
2422 if (max_out == 0) {
2423 return sizeof(ssl->s3->server_random);
2424 }
2425 if (max_out > sizeof(ssl->s3->server_random)) {
2426 max_out = sizeof(ssl->s3->server_random);
2427 }
David Benjamin17cf2cb2016-12-13 01:07:13 -05002428 OPENSSL_memcpy(out, ssl->s3->server_random, max_out);
David Benjamin96ba15f2015-12-25 14:10:17 -05002429 return max_out;
2430}
2431
David Benjaminf6494f42016-01-07 12:37:41 -08002432const SSL_CIPHER *SSL_get_pending_cipher(const SSL *ssl) {
David Benjamin45738dd2017-02-09 20:01:26 -05002433 SSL_HANDSHAKE *hs = ssl->s3->hs;
2434 if (hs == NULL) {
David Benjaminf6494f42016-01-07 12:37:41 -08002435 return NULL;
2436 }
David Benjamin45738dd2017-02-09 20:01:26 -05002437 return hs->new_cipher;
David Benjaminf6494f42016-01-07 12:37:41 -08002438}
2439
David Benjaminbbaf3672016-11-17 10:53:09 +09002440void SSL_set_retain_only_sha256_of_client_certs(SSL *ssl, int enabled) {
2441 ssl->retain_only_sha256_of_client_certs = !!enabled;
2442}
2443
David Benjamincdd0b7e2016-01-07 10:10:41 -08002444void SSL_CTX_set_retain_only_sha256_of_client_certs(SSL_CTX *ctx, int enabled) {
2445 ctx->retain_only_sha256_of_client_certs = !!enabled;
2446}
2447
David Benjamin65ac9972016-09-02 21:35:25 -04002448void SSL_CTX_set_grease_enabled(SSL_CTX *ctx, int enabled) {
2449 ctx->grease_enabled = !!enabled;
2450}
2451
David Benjamin35ac5b72017-03-03 15:05:56 -05002452int32_t SSL_get_ticket_age_skew(const SSL *ssl) {
2453 return ssl->s3->ticket_age_skew;
2454}
2455
David Benjamin27bbae42015-09-13 00:54:37 -04002456int SSL_clear(SSL *ssl) {
David Benjaminb79cc842016-12-07 15:57:14 -05002457 /* In OpenSSL, reusing a client |SSL| with |SSL_clear| causes the previously
2458 * established session to be offered the next time around. wpa_supplicant
2459 * depends on this behavior, so emulate it. */
2460 SSL_SESSION *session = NULL;
2461 if (!ssl->server && ssl->s3->established_session != NULL) {
2462 session = ssl->s3->established_session;
2463 SSL_SESSION_up_ref(session);
2464 }
2465
David Benjamin27bbae42015-09-13 00:54:37 -04002466 /* TODO(davidben): Some state on |ssl| is reset both in |SSL_new| and
2467 * |SSL_clear| because it is per-connection state rather than configuration
2468 * state. Per-connection state should be on |ssl->s3| and |ssl->d1| so it is
2469 * naturally reset at the right points between |SSL_new|, |SSL_clear|, and
2470 * |ssl3_new|. */
2471
2472 ssl->rwstate = SSL_NOTHING;
2473
2474 BUF_MEM_free(ssl->init_buf);
2475 ssl->init_buf = NULL;
David Benjamin49054542016-07-27 13:51:01 -04002476 ssl->init_msg = NULL;
2477 ssl->init_num = 0;
David Benjamin27bbae42015-09-13 00:54:37 -04002478
David Benjamin27bbae42015-09-13 00:54:37 -04002479 /* The ssl->d1->mtu is simultaneously configuration (preserved across
2480 * clear) and connection-specific state (gets reset).
2481 *
2482 * TODO(davidben): Avoid this. */
2483 unsigned mtu = 0;
2484 if (ssl->d1 != NULL) {
2485 mtu = ssl->d1->mtu;
2486 }
2487
2488 ssl->method->ssl_free(ssl);
2489 if (!ssl->method->ssl_new(ssl)) {
David Benjaminb79cc842016-12-07 15:57:14 -05002490 SSL_SESSION_free(session);
David Benjamin27bbae42015-09-13 00:54:37 -04002491 return 0;
2492 }
David Benjamin27bbae42015-09-13 00:54:37 -04002493
David Benjamince079fd2016-08-02 16:22:34 -04002494 if (SSL_is_dtls(ssl) && (SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
David Benjamin27bbae42015-09-13 00:54:37 -04002495 ssl->d1->mtu = mtu;
2496 }
2497
David Benjaminb79cc842016-12-07 15:57:14 -05002498 if (session != NULL) {
2499 SSL_set_session(ssl, session);
2500 SSL_SESSION_free(session);
2501 }
2502
David Benjamin27bbae42015-09-13 00:54:37 -04002503 return 1;
2504}
2505
David Benjaminda881e92015-04-26 14:45:04 -04002506int SSL_CTX_sess_connect(const SSL_CTX *ctx) { return 0; }
2507int SSL_CTX_sess_connect_good(const SSL_CTX *ctx) { return 0; }
2508int SSL_CTX_sess_connect_renegotiate(const SSL_CTX *ctx) { return 0; }
2509int SSL_CTX_sess_accept(const SSL_CTX *ctx) { return 0; }
2510int SSL_CTX_sess_accept_renegotiate(const SSL_CTX *ctx) { return 0; }
2511int SSL_CTX_sess_accept_good(const SSL_CTX *ctx) { return 0; }
2512int SSL_CTX_sess_hits(const SSL_CTX *ctx) { return 0; }
2513int SSL_CTX_sess_cb_hits(const SSL_CTX *ctx) { return 0; }
2514int SSL_CTX_sess_misses(const SSL_CTX *ctx) { return 0; }
2515int SSL_CTX_sess_timeouts(const SSL_CTX *ctx) { return 0; }
2516int SSL_CTX_sess_cache_full(const SSL_CTX *ctx) { return 0; }
David Benjamin936aada2016-06-07 19:09:22 -04002517
2518int SSL_num_renegotiations(const SSL *ssl) {
2519 return SSL_total_renegotiations(ssl);
2520}
2521
2522int SSL_CTX_need_tmp_RSA(const SSL_CTX *ctx) { return 0; }
2523int SSL_need_tmp_RSA(const SSL *ssl) { return 0; }
2524int SSL_CTX_set_tmp_rsa(SSL_CTX *ctx, const RSA *rsa) { return 1; }
2525int SSL_set_tmp_rsa(SSL *ssl, const RSA *rsa) { return 1; }
Matt Braithwaite4838d8a2015-08-20 13:20:03 -07002526void ERR_load_SSL_strings(void) {}
David Benjamin27bbae42015-09-13 00:54:37 -04002527void SSL_load_error_strings(void) {}
David Benjamin936aada2016-06-07 19:09:22 -04002528int SSL_cache_hit(SSL *ssl) { return SSL_session_reused(ssl); }
2529
2530int SSL_CTX_set_tmp_ecdh(SSL_CTX *ctx, const EC_KEY *ec_key) {
2531 if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) {
2532 OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
2533 return 0;
2534 }
2535 int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key));
2536 return SSL_CTX_set1_curves(ctx, &nid, 1);
2537}
2538
2539int SSL_set_tmp_ecdh(SSL *ssl, const EC_KEY *ec_key) {
2540 if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) {
2541 OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
2542 return 0;
2543 }
2544 int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key));
2545 return SSL_set1_curves(ssl, &nid, 1);
2546}
David Benjamin721e8b72016-08-03 13:13:17 -04002547
Adam Langley4c341d02017-03-08 19:33:21 -08002548void SSL_CTX_set_ticket_aead_method(SSL_CTX *ctx,
2549 const SSL_TICKET_AEAD_METHOD *aead_method) {
2550 ctx->ticket_aead_method = aead_method;
2551}