blob: a7070d108d23996fa17dd4c45338e5000f53db83 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
108 *
109 */
110/* ====================================================================
111 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
112 * ECC cipher suite support in OpenSSL originally developed by
113 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
114 */
115/* ====================================================================
116 * Copyright 2005 Nokia. All rights reserved.
117 *
118 * The portions of the attached software ("Contribution") is developed by
119 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
120 * license.
121 *
122 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
123 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
124 * support (see RFC 4279) to OpenSSL.
125 *
126 * No patent licenses or other rights except those expressly stated in
127 * the OpenSSL open source license shall be deemed granted or received
128 * expressly, by implication, estoppel, or otherwise.
129 *
130 * No assurances are provided by Nokia that the Contribution does not
131 * infringe the patent or other intellectual property rights of any third
132 * party or that the license provides you with all the necessary rights
133 * to make use of the Contribution.
134 *
135 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
136 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
137 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
138 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
139 * OTHERWISE. */
140
David Benjamin9e4e01e2015-09-15 01:48:04 -0400141#include <openssl/ssl.h>
142
Adam Langley95c29f32014-06-20 12:00:00 -0700143#include <assert.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400144#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700145
David Benjamin39482a12014-07-20 13:30:15 -0400146#include <openssl/bytestring.h>
David Benjamin7c6d09b2015-09-05 11:00:19 -0400147#include <openssl/crypto.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700148#include <openssl/dh.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400149#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700150#include <openssl/lhash.h>
151#include <openssl/mem.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700152#include <openssl/rand.h>
153#include <openssl/x509v3.h>
154
David Benjamin2ee94aa2015-04-07 22:38:30 -0400155#include "internal.h"
David Benjamin546f1a52015-04-15 16:46:09 -0400156#include "../crypto/internal.h"
157
Adam Langley95c29f32014-06-20 12:00:00 -0700158
David Benjamin76c2efc2015-08-31 14:24:29 -0400159/* |SSL_R_UNKNOWN_PROTOCOL| is no longer emitted, but continue to define it
David Benjamin97760d52015-07-24 23:02:49 -0400160 * to avoid downstream churn. */
David Benjamin76c2efc2015-08-31 14:24:29 -0400161OPENSSL_DECLARE_ERROR_REASON(SSL, UNKNOWN_PROTOCOL)
David Benjamin97760d52015-07-24 23:02:49 -0400162
Adam Langleyfcf25832014-12-18 17:42:32 -0800163/* Some error codes are special. Ensure the make_errors.go script never
164 * regresses this. */
165OPENSSL_COMPILE_ASSERT(SSL_R_TLSV1_ALERT_NO_RENEGOTIATION ==
166 SSL_AD_NO_RENEGOTIATION + SSL_AD_REASON_OFFSET,
167 ssl_alert_reason_code_mismatch);
David Benjamine1136082014-09-20 12:28:58 -0400168
David Benjamin1d0a1942015-04-26 15:35:35 -0400169/* kMaxHandshakeSize is the maximum size, in bytes, of a handshake message. */
170static const size_t kMaxHandshakeSize = (1u << 24) - 1;
171
David Benjaminaa585132015-06-29 23:36:17 -0400172static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl =
173 CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
174static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl_ctx =
175 CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
David Benjamin9f33fc62015-04-15 17:29:53 -0400176
David Benjamin7c6d09b2015-09-05 11:00:19 -0400177int SSL_library_init(void) {
178 CRYPTO_library_init();
179 return 1;
180}
181
Adam Langleye976e432016-02-15 14:01:10 -0800182static uint32_t ssl_session_hash(const SSL_SESSION *sess) {
183 const uint8_t *session_id = sess->session_id;
184
185 uint8_t tmp_storage[sizeof(uint32_t)];
186 if (sess->session_id_length < sizeof(tmp_storage)) {
187 memset(tmp_storage, 0, sizeof(tmp_storage));
188 memcpy(tmp_storage, sess->session_id, sess->session_id_length);
189 session_id = tmp_storage;
190 }
191
David Benjamin27bbae42015-09-13 00:54:37 -0400192 uint32_t hash =
Adam Langleye976e432016-02-15 14:01:10 -0800193 ((uint32_t)session_id[0]) |
194 ((uint32_t)session_id[1] << 8) |
195 ((uint32_t)session_id[2] << 16) |
196 ((uint32_t)session_id[3] << 24);
David Benjamin7c6d09b2015-09-05 11:00:19 -0400197
David Benjamin27bbae42015-09-13 00:54:37 -0400198 return hash;
199}
200
201/* NB: If this function (or indeed the hash function which uses a sort of
202 * coarser function than this one) is changed, ensure
203 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
204 * able to construct an SSL_SESSION that will collide with any existing session
205 * with a matching session ID. */
206static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
207 if (a->ssl_version != b->ssl_version) {
208 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800209 }
Adam Langley95c29f32014-06-20 12:00:00 -0700210
David Benjamin27bbae42015-09-13 00:54:37 -0400211 if (a->session_id_length != b->session_id_length) {
212 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800213 }
Adam Langley95c29f32014-06-20 12:00:00 -0700214
David Benjamin27bbae42015-09-13 00:54:37 -0400215 return memcmp(a->session_id, b->session_id, a->session_id_length);
216}
Adam Langley95c29f32014-06-20 12:00:00 -0700217
David Benjamin27bbae42015-09-13 00:54:37 -0400218SSL_CTX *SSL_CTX_new(const SSL_METHOD *method) {
219 SSL_CTX *ret = NULL;
220
221 if (method == NULL) {
222 OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_METHOD_PASSED);
223 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800224 }
Adam Langley95c29f32014-06-20 12:00:00 -0700225
David Benjamin27bbae42015-09-13 00:54:37 -0400226 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
227 OPENSSL_PUT_ERROR(SSL, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
228 goto err;
David Benjamin62fd1622015-01-11 13:30:01 -0500229 }
230
Brian Smith5ba06892016-02-07 09:36:04 -1000231 ret = OPENSSL_malloc(sizeof(SSL_CTX));
David Benjamin27bbae42015-09-13 00:54:37 -0400232 if (ret == NULL) {
233 goto err;
David Benjamin62fd1622015-01-11 13:30:01 -0500234 }
235
David Benjamin27bbae42015-09-13 00:54:37 -0400236 memset(ret, 0, sizeof(SSL_CTX));
Adam Langley95c29f32014-06-20 12:00:00 -0700237
David Benjamin27bbae42015-09-13 00:54:37 -0400238 ret->method = method->method;
239
240 CRYPTO_MUTEX_init(&ret->lock);
241
242 ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
243 ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
244
245 /* We take the system default */
246 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
247
248 ret->references = 1;
249
250 ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
251 ret->verify_mode = SSL_VERIFY_NONE;
252 ret->cert = ssl_cert_new();
253 if (ret->cert == NULL) {
254 goto err;
255 }
256
257 ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
258 if (ret->sessions == NULL) {
259 goto err;
260 }
261 ret->cert_store = X509_STORE_new();
262 if (ret->cert_store == NULL) {
263 goto err;
264 }
265
266 ssl_create_cipher_list(ret->method, &ret->cipher_list,
267 &ret->cipher_list_by_id, SSL_DEFAULT_CIPHER_LIST);
268 if (ret->cipher_list == NULL ||
269 sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0) {
270 OPENSSL_PUT_ERROR(SSL, SSL_R_LIBRARY_HAS_NO_CIPHERS);
271 goto err2;
272 }
273
274 ret->param = X509_VERIFY_PARAM_new();
275 if (!ret->param) {
276 goto err;
277 }
278
279 ret->client_CA = sk_X509_NAME_new_null();
280 if (ret->client_CA == NULL) {
281 goto err;
282 }
283
David Benjamin8a589332015-12-04 23:14:35 -0500284 CRYPTO_new_ex_data(&ret->ex_data);
David Benjamin27bbae42015-09-13 00:54:37 -0400285
286 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
287
288 /* Setup RFC4507 ticket keys */
289 if (!RAND_bytes(ret->tlsext_tick_key_name, 16) ||
290 !RAND_bytes(ret->tlsext_tick_hmac_key, 16) ||
291 !RAND_bytes(ret->tlsext_tick_aes_key, 16)) {
292 ret->options |= SSL_OP_NO_TICKET;
293 }
294
David Benjamin27bbae42015-09-13 00:54:37 -0400295 /* Lock the SSL_CTX to the specified version, for compatibility with legacy
296 * uses of SSL_METHOD. */
297 if (method->version != 0) {
298 SSL_CTX_set_max_version(ret, method->version);
299 SSL_CTX_set_min_version(ret, method->version);
David Benjamin10e664b2016-06-20 22:20:47 -0400300 } else if (method->method->is_dtls) {
301 /* TODO(svaldez): Enable DTLS 1.3 once implemented. */
302 SSL_CTX_set_max_version(ret, DTLS1_2_VERSION);
303 SSL_CTX_set_min_version(ret, DTLS1_VERSION);
304 } else {
Steven Valdez4f94b1c2016-05-24 12:31:07 -0400305 /* TODO(svaldez): Enable TLS 1.3 once implemented. */
306 SSL_CTX_set_max_version(ret, TLS1_2_VERSION);
David Benjamin10e664b2016-06-20 22:20:47 -0400307 SSL_CTX_set_min_version(ret, SSL3_VERSION);
David Benjamin27bbae42015-09-13 00:54:37 -0400308 }
309
310 return ret;
311
312err:
313 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
314err2:
315 SSL_CTX_free(ret);
316 return NULL;
317}
318
319void SSL_CTX_free(SSL_CTX *ctx) {
320 if (ctx == NULL ||
321 !CRYPTO_refcount_dec_and_test_zero(&ctx->references)) {
322 return;
323 }
324
325 X509_VERIFY_PARAM_free(ctx->param);
326
327 /* Free internal session cache. However: the remove_cb() may reference the
328 * ex_data of SSL_CTX, thus the ex_data store can only be removed after the
329 * sessions were flushed. As the ex_data handling routines might also touch
330 * the session cache, the most secure solution seems to be: empty (flush) the
331 * cache, then free ex_data, then finally free the cache. (See ticket
332 * [openssl.org #212].) */
333 SSL_CTX_flush_sessions(ctx, 0);
334
335 CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, ctx, &ctx->ex_data);
336
337 CRYPTO_MUTEX_cleanup(&ctx->lock);
338 lh_SSL_SESSION_free(ctx->sessions);
339 X509_STORE_free(ctx->cert_store);
340 ssl_cipher_preference_list_free(ctx->cipher_list);
341 sk_SSL_CIPHER_free(ctx->cipher_list_by_id);
342 ssl_cipher_preference_list_free(ctx->cipher_list_tls10);
343 ssl_cipher_preference_list_free(ctx->cipher_list_tls11);
344 ssl_cert_free(ctx->cert);
345 sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->client_custom_extensions,
346 SSL_CUSTOM_EXTENSION_free);
347 sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->server_custom_extensions,
348 SSL_CUSTOM_EXTENSION_free);
349 sk_X509_NAME_pop_free(ctx->client_CA, X509_NAME_free);
350 sk_SRTP_PROTECTION_PROFILE_free(ctx->srtp_profiles);
351 OPENSSL_free(ctx->psk_identity_hint);
Steven Valdezce902a92016-05-17 11:47:53 -0400352 OPENSSL_free(ctx->supported_group_list);
David Benjamin27bbae42015-09-13 00:54:37 -0400353 OPENSSL_free(ctx->alpn_client_proto_list);
354 OPENSSL_free(ctx->ocsp_response);
355 OPENSSL_free(ctx->signed_cert_timestamp_list);
356 EVP_PKEY_free(ctx->tlsext_channel_id_private);
David Benjamin27bbae42015-09-13 00:54:37 -0400357
358 OPENSSL_free(ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -0800359}
Adam Langley95c29f32014-06-20 12:00:00 -0700360
Adam Langleyfcf25832014-12-18 17:42:32 -0800361SSL *SSL_new(SSL_CTX *ctx) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800362 if (ctx == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400363 OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_CTX);
Adam Langleyfcf25832014-12-18 17:42:32 -0800364 return NULL;
365 }
366 if (ctx->method == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400367 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
Adam Langleyfcf25832014-12-18 17:42:32 -0800368 return NULL;
369 }
Adam Langley95c29f32014-06-20 12:00:00 -0700370
Brian Smith5ba06892016-02-07 09:36:04 -1000371 SSL *ssl = OPENSSL_malloc(sizeof(SSL));
David Benjamin0d56f882015-12-19 17:05:56 -0500372 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800373 goto err;
374 }
David Benjamin0d56f882015-12-19 17:05:56 -0500375 memset(ssl, 0, sizeof(SSL));
Adam Langley95c29f32014-06-20 12:00:00 -0700376
David Benjamin0d56f882015-12-19 17:05:56 -0500377 ssl->min_version = ctx->min_version;
378 ssl->max_version = ctx->max_version;
David Benjamin1eb367c2014-12-12 18:17:51 -0500379
Taylor Brandstetter376a0fe2016-05-10 19:30:28 -0700380 /* RFC 6347 states that implementations SHOULD use an initial timer value of
381 * 1 second. */
382 ssl->initial_timeout_duration_ms = 1000;
383
David Benjamin0d56f882015-12-19 17:05:56 -0500384 ssl->options = ctx->options;
385 ssl->mode = ctx->mode;
386 ssl->max_cert_list = ctx->max_cert_list;
Adam Langley95c29f32014-06-20 12:00:00 -0700387
David Benjamin0d56f882015-12-19 17:05:56 -0500388 ssl->cert = ssl_cert_dup(ctx->cert);
389 if (ssl->cert == NULL) {
David Benjamina5a3eeb2015-03-18 20:26:30 -0400390 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -0800391 }
Adam Langley95c29f32014-06-20 12:00:00 -0700392
David Benjamin0d56f882015-12-19 17:05:56 -0500393 ssl->msg_callback = ctx->msg_callback;
394 ssl->msg_callback_arg = ctx->msg_callback_arg;
395 ssl->verify_mode = ctx->verify_mode;
396 ssl->sid_ctx_length = ctx->sid_ctx_length;
397 assert(ssl->sid_ctx_length <= sizeof ssl->sid_ctx);
398 memcpy(&ssl->sid_ctx, &ctx->sid_ctx, sizeof(ssl->sid_ctx));
399 ssl->verify_callback = ctx->default_verify_callback;
Adam Langley95c29f32014-06-20 12:00:00 -0700400
David Benjamin0d56f882015-12-19 17:05:56 -0500401 ssl->param = X509_VERIFY_PARAM_new();
402 if (!ssl->param) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800403 goto err;
404 }
David Benjamin0d56f882015-12-19 17:05:56 -0500405 X509_VERIFY_PARAM_inherit(ssl->param, ctx->param);
406 ssl->quiet_shutdown = ctx->quiet_shutdown;
407 ssl->max_send_fragment = ctx->max_send_fragment;
Adam Langley95c29f32014-06-20 12:00:00 -0700408
Adam Langley0b5e3902015-05-15 13:08:38 -0700409 CRYPTO_refcount_inc(&ctx->references);
David Benjamin0d56f882015-12-19 17:05:56 -0500410 ssl->ctx = ctx;
Adam Langley0b5e3902015-05-15 13:08:38 -0700411 CRYPTO_refcount_inc(&ctx->references);
David Benjamin0d56f882015-12-19 17:05:56 -0500412 ssl->initial_ctx = ctx;
Adam Langley95c29f32014-06-20 12:00:00 -0700413
Steven Valdezce902a92016-05-17 11:47:53 -0400414 if (ctx->supported_group_list) {
415 ssl->supported_group_list =
416 BUF_memdup(ctx->supported_group_list,
417 ctx->supported_group_list_len * 2);
418 if (!ssl->supported_group_list) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800419 goto err;
420 }
Steven Valdezce902a92016-05-17 11:47:53 -0400421 ssl->supported_group_list_len = ctx->supported_group_list_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800422 }
Adam Langley95c29f32014-06-20 12:00:00 -0700423
David Benjamin0d56f882015-12-19 17:05:56 -0500424 if (ssl->ctx->alpn_client_proto_list) {
425 ssl->alpn_client_proto_list = BUF_memdup(
426 ssl->ctx->alpn_client_proto_list, ssl->ctx->alpn_client_proto_list_len);
427 if (ssl->alpn_client_proto_list == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800428 goto err;
429 }
David Benjamin0d56f882015-12-19 17:05:56 -0500430 ssl->alpn_client_proto_list_len = ssl->ctx->alpn_client_proto_list_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800431 }
Adam Langley95c29f32014-06-20 12:00:00 -0700432
David Benjamin0d56f882015-12-19 17:05:56 -0500433 ssl->verify_result = X509_V_OK;
434 ssl->method = ctx->method;
Adam Langley95c29f32014-06-20 12:00:00 -0700435
David Benjamin0d56f882015-12-19 17:05:56 -0500436 if (!ssl->method->ssl_new(ssl)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800437 goto err;
438 }
Adam Langley95c29f32014-06-20 12:00:00 -0700439
David Benjamin0d56f882015-12-19 17:05:56 -0500440 ssl->rwstate = SSL_NOTHING;
Adam Langley95c29f32014-06-20 12:00:00 -0700441
David Benjamin0d56f882015-12-19 17:05:56 -0500442 CRYPTO_new_ex_data(&ssl->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700443
David Benjamin0d56f882015-12-19 17:05:56 -0500444 ssl->psk_identity_hint = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800445 if (ctx->psk_identity_hint) {
David Benjamin0d56f882015-12-19 17:05:56 -0500446 ssl->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
447 if (ssl->psk_identity_hint == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800448 goto err;
449 }
450 }
David Benjamin0d56f882015-12-19 17:05:56 -0500451 ssl->psk_client_callback = ctx->psk_client_callback;
452 ssl->psk_server_callback = ctx->psk_server_callback;
Adam Langley95c29f32014-06-20 12:00:00 -0700453
David Benjamin0d56f882015-12-19 17:05:56 -0500454 ssl->tlsext_channel_id_enabled = ctx->tlsext_channel_id_enabled;
David Benjamin02ddbfd2015-01-11 13:09:11 -0500455 if (ctx->tlsext_channel_id_private) {
David Benjamin0d56f882015-12-19 17:05:56 -0500456 ssl->tlsext_channel_id_private =
David Benjamin9a10f8f2015-05-05 22:22:40 -0400457 EVP_PKEY_up_ref(ctx->tlsext_channel_id_private);
David Benjamin02ddbfd2015-01-11 13:09:11 -0500458 }
459
David Benjamin0d56f882015-12-19 17:05:56 -0500460 ssl->signed_cert_timestamps_enabled =
461 ssl->ctx->signed_cert_timestamps_enabled;
462 ssl->ocsp_stapling_enabled = ssl->ctx->ocsp_stapling_enabled;
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200463
David Benjamin0d56f882015-12-19 17:05:56 -0500464 return ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -0800465
Adam Langley95c29f32014-06-20 12:00:00 -0700466err:
David Benjamin0d56f882015-12-19 17:05:56 -0500467 SSL_free(ssl);
David Benjamin3570d732015-06-29 00:28:17 -0400468 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700469
Adam Langleyfcf25832014-12-18 17:42:32 -0800470 return NULL;
471}
Adam Langley95c29f32014-06-20 12:00:00 -0700472
David Benjamin27bbae42015-09-13 00:54:37 -0400473void SSL_free(SSL *ssl) {
474 if (ssl == NULL) {
475 return;
476 }
477
478 X509_VERIFY_PARAM_free(ssl->param);
479
480 CRYPTO_free_ex_data(&g_ex_data_class_ssl, ssl, &ssl->ex_data);
481
David Benjamin2f871122016-05-20 14:27:17 -0400482 ssl_free_wbio_buffer(ssl);
483 assert(ssl->bbio == NULL);
David Benjamin27bbae42015-09-13 00:54:37 -0400484
David Benjamin27bbae42015-09-13 00:54:37 -0400485 BIO_free_all(ssl->rbio);
David Benjaminf715c422016-06-11 19:01:56 -0400486 BIO_free_all(ssl->wbio);
David Benjamin27bbae42015-09-13 00:54:37 -0400487
488 BUF_MEM_free(ssl->init_buf);
489
490 /* add extra stuff */
491 ssl_cipher_preference_list_free(ssl->cipher_list);
492 sk_SSL_CIPHER_free(ssl->cipher_list_by_id);
493
494 ssl_clear_bad_session(ssl);
495 SSL_SESSION_free(ssl->session);
496
David Benjamin27bbae42015-09-13 00:54:37 -0400497 ssl_cert_free(ssl->cert);
498
499 OPENSSL_free(ssl->tlsext_hostname);
500 SSL_CTX_free(ssl->initial_ctx);
Steven Valdezce902a92016-05-17 11:47:53 -0400501 OPENSSL_free(ssl->supported_group_list);
David Benjamin27bbae42015-09-13 00:54:37 -0400502 OPENSSL_free(ssl->alpn_client_proto_list);
503 EVP_PKEY_free(ssl->tlsext_channel_id_private);
504 OPENSSL_free(ssl->psk_identity_hint);
505 sk_X509_NAME_pop_free(ssl->client_CA, X509_NAME_free);
David Benjamin27bbae42015-09-13 00:54:37 -0400506 sk_SRTP_PROTECTION_PROFILE_free(ssl->srtp_profiles);
507
508 if (ssl->method != NULL) {
509 ssl->method->ssl_free(ssl);
510 }
511 SSL_CTX_free(ssl->ctx);
512
513 OPENSSL_free(ssl);
514}
515
516void SSL_set_connect_state(SSL *ssl) {
517 ssl->server = 0;
David Benjamin27bbae42015-09-13 00:54:37 -0400518 ssl->state = SSL_ST_CONNECT;
David Benjamin9f1dc822016-06-07 17:03:46 -0400519 ssl->handshake_func = ssl3_connect;
David Benjamin27bbae42015-09-13 00:54:37 -0400520}
521
522void SSL_set_accept_state(SSL *ssl) {
523 ssl->server = 1;
David Benjamin27bbae42015-09-13 00:54:37 -0400524 ssl->state = SSL_ST_ACCEPT;
David Benjamin9f1dc822016-06-07 17:03:46 -0400525 ssl->handshake_func = ssl3_accept;
David Benjamin27bbae42015-09-13 00:54:37 -0400526}
527
David Benjaminf715c422016-06-11 19:01:56 -0400528static void ssl_set_rbio(SSL *ssl, BIO *rbio) {
529 BIO_free_all(ssl->rbio);
530 ssl->rbio = rbio;
531}
532
533static void ssl_set_wbio(SSL *ssl, BIO *wbio) {
David Benjamin27bbae42015-09-13 00:54:37 -0400534 /* If the output buffering BIO is still in place, remove it. */
535 if (ssl->bbio != NULL) {
David Benjamin2f871122016-05-20 14:27:17 -0400536 ssl->wbio = BIO_pop(ssl->wbio);
David Benjamin27bbae42015-09-13 00:54:37 -0400537 }
538
David Benjaminf715c422016-06-11 19:01:56 -0400539 BIO_free_all(ssl->wbio);
David Benjamin27bbae42015-09-13 00:54:37 -0400540 ssl->wbio = wbio;
David Benjamin2f871122016-05-20 14:27:17 -0400541
542 /* Re-attach |bbio| to the new |wbio|. */
543 if (ssl->bbio != NULL) {
544 ssl->wbio = BIO_push(ssl->bbio, ssl->wbio);
545 }
David Benjamin27bbae42015-09-13 00:54:37 -0400546}
547
David Benjaminf715c422016-06-11 19:01:56 -0400548void SSL_set_bio(SSL *ssl, BIO *rbio, BIO *wbio) {
549 /* For historical reasons, this function has many different cases in ownership
550 * handling. */
551
552 /* If the two arguments are equal, one fewer reference is granted than
553 * taken. */
554 if (rbio != NULL && rbio == wbio) {
555 BIO_up_ref(rbio);
556 }
557
558 /* If at most one of rbio or wbio is changed, only adopt one reference. */
559 if (rbio == SSL_get_rbio(ssl)) {
560 ssl_set_wbio(ssl, wbio);
561 return;
562 }
563 if (wbio == SSL_get_wbio(ssl)) {
564 ssl_set_rbio(ssl, rbio);
565 return;
566 }
567
568 /* Otherwise, adopt both references. */
569 ssl_set_rbio(ssl, rbio);
570 ssl_set_wbio(ssl, wbio);
571}
572
David Benjamin27bbae42015-09-13 00:54:37 -0400573BIO *SSL_get_rbio(const SSL *ssl) { return ssl->rbio; }
574
David Benjamin2f871122016-05-20 14:27:17 -0400575BIO *SSL_get_wbio(const SSL *ssl) {
576 if (ssl->bbio != NULL) {
577 /* If |bbio| is active, the true caller-configured BIO is its |next_bio|. */
578 assert(ssl->bbio == ssl->wbio);
579 return ssl->bbio->next_bio;
580 }
581 return ssl->wbio;
582}
David Benjamin27bbae42015-09-13 00:54:37 -0400583
584int SSL_do_handshake(SSL *ssl) {
David Benjamin4c5ddb82016-03-11 22:56:19 -0500585 ssl->rwstate = SSL_NOTHING;
David Benjamin15c14882016-03-14 14:25:46 -0400586 /* Functions which use SSL_get_error must clear the error queue on entry. */
587 ERR_clear_error();
David Benjamin0d275bd2016-06-01 20:19:34 -0400588 ERR_clear_system_error();
David Benjamin15c14882016-03-14 14:25:46 -0400589
David Benjamin27bbae42015-09-13 00:54:37 -0400590 if (ssl->handshake_func == NULL) {
591 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_TYPE_NOT_SET);
592 return -1;
593 }
594
595 if (!SSL_in_init(ssl)) {
596 return 1;
597 }
598
599 return ssl->handshake_func(ssl);
600}
601
602int SSL_connect(SSL *ssl) {
David Benjaminb2a73182016-03-10 01:25:26 -0500603 if (ssl->handshake_func == NULL) {
David Benjamin27bbae42015-09-13 00:54:37 -0400604 /* Not properly initialized yet */
605 SSL_set_connect_state(ssl);
606 }
607
David Benjamin15c14882016-03-14 14:25:46 -0400608 return SSL_do_handshake(ssl);
David Benjamin27bbae42015-09-13 00:54:37 -0400609}
610
611int SSL_accept(SSL *ssl) {
David Benjaminb2a73182016-03-10 01:25:26 -0500612 if (ssl->handshake_func == NULL) {
David Benjamin27bbae42015-09-13 00:54:37 -0400613 /* Not properly initialized yet */
614 SSL_set_accept_state(ssl);
615 }
616
David Benjamin15c14882016-03-14 14:25:46 -0400617 return SSL_do_handshake(ssl);
David Benjamin27bbae42015-09-13 00:54:37 -0400618}
619
David Benjamin15c14882016-03-14 14:25:46 -0400620static int ssl_read_impl(SSL *ssl, void *buf, int num, int peek) {
David Benjamin4c5ddb82016-03-11 22:56:19 -0500621 ssl->rwstate = SSL_NOTHING;
David Benjamin15c14882016-03-14 14:25:46 -0400622 /* Functions which use SSL_get_error must clear the error queue on entry. */
623 ERR_clear_error();
David Benjaminc79845c2016-03-10 01:28:00 -0500624 ERR_clear_system_error();
David Benjamin15c14882016-03-14 14:25:46 -0400625
David Benjaminb2a73182016-03-10 01:25:26 -0500626 if (ssl->handshake_func == NULL) {
David Benjamin27bbae42015-09-13 00:54:37 -0400627 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
628 return -1;
629 }
630
David Benjaminc79845c2016-03-10 01:28:00 -0500631 /* This may require multiple iterations. False Start will cause
632 * |ssl->handshake_func| to signal success one step early, but the handshake
633 * must be completely finished before other modes are accepted. */
634 while (SSL_in_init(ssl)) {
635 int ret = SSL_do_handshake(ssl);
636 if (ret < 0) {
637 return ret;
638 }
639 if (ret == 0) {
640 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
641 return -1;
642 }
643 }
644
David Benjaminf0ee9072016-06-15 17:44:37 -0400645 return ssl->method->read_app_data(ssl, buf, num, peek);
David Benjamin15c14882016-03-14 14:25:46 -0400646}
647
648int SSL_read(SSL *ssl, void *buf, int num) {
649 return ssl_read_impl(ssl, buf, num, 0 /* consume bytes */);
David Benjamin27bbae42015-09-13 00:54:37 -0400650}
651
652int SSL_peek(SSL *ssl, void *buf, int num) {
David Benjamin15c14882016-03-14 14:25:46 -0400653 return ssl_read_impl(ssl, buf, num, 1 /* peek */);
David Benjamin27bbae42015-09-13 00:54:37 -0400654}
655
656int SSL_write(SSL *ssl, const void *buf, int num) {
David Benjamin4c5ddb82016-03-11 22:56:19 -0500657 ssl->rwstate = SSL_NOTHING;
David Benjamin15c14882016-03-14 14:25:46 -0400658 /* Functions which use SSL_get_error must clear the error queue on entry. */
659 ERR_clear_error();
David Benjaminc79845c2016-03-10 01:28:00 -0500660 ERR_clear_system_error();
David Benjamin15c14882016-03-14 14:25:46 -0400661
David Benjaminb2a73182016-03-10 01:25:26 -0500662 if (ssl->handshake_func == NULL) {
David Benjamin27bbae42015-09-13 00:54:37 -0400663 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
664 return -1;
665 }
666
David Benjaminfa214e42016-05-10 17:03:10 -0400667 if (ssl->s3->send_shutdown != ssl_shutdown_none) {
David Benjamin27bbae42015-09-13 00:54:37 -0400668 OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
669 return -1;
670 }
671
David Benjamind7ac1432016-03-10 00:41:25 -0500672 /* If necessary, complete the handshake implicitly. */
673 if (SSL_in_init(ssl) && !SSL_in_false_start(ssl)) {
674 int ret = SSL_do_handshake(ssl);
675 if (ret < 0) {
676 return ret;
677 }
678 if (ret == 0) {
679 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
680 return -1;
681 }
682 }
683
David Benjaminf0ee9072016-06-15 17:44:37 -0400684 return ssl->method->write_app_data(ssl, buf, num);
David Benjamin27bbae42015-09-13 00:54:37 -0400685}
686
687int SSL_shutdown(SSL *ssl) {
David Benjamin4c5ddb82016-03-11 22:56:19 -0500688 ssl->rwstate = SSL_NOTHING;
David Benjamin15c14882016-03-14 14:25:46 -0400689 /* Functions which use SSL_get_error must clear the error queue on entry. */
690 ERR_clear_error();
David Benjamin0d275bd2016-06-01 20:19:34 -0400691 ERR_clear_system_error();
David Benjamin15c14882016-03-14 14:25:46 -0400692
David Benjaminb2a73182016-03-10 01:25:26 -0500693 if (ssl->handshake_func == NULL) {
David Benjamin27bbae42015-09-13 00:54:37 -0400694 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
695 return -1;
696 }
697
Steven Valdeza14934f2016-02-29 10:05:08 -0500698 /* We can't shutdown properly if we are in the middle of a handshake. */
699 if (SSL_in_init(ssl)) {
700 OPENSSL_PUT_ERROR(SSL, SSL_R_SHUTDOWN_WHILE_IN_INIT);
701 return -1;
702 }
703
David Benjamin27bbae42015-09-13 00:54:37 -0400704 if (ssl->quiet_shutdown) {
David Benjaminfa214e42016-05-10 17:03:10 -0400705 /* Do nothing if configured not to send a close_notify. */
706 ssl->s3->send_shutdown = ssl_shutdown_close_notify;
707 ssl->s3->recv_shutdown = ssl_shutdown_close_notify;
David Benjamin27bbae42015-09-13 00:54:37 -0400708 return 1;
709 }
710
David Benjaminfa214e42016-05-10 17:03:10 -0400711 /* This function completes in two stages. It sends a close_notify and then it
712 * waits for a close_notify to come in. Perform exactly one action and return
713 * whether or not it succeeds. */
David Benjamin27bbae42015-09-13 00:54:37 -0400714
David Benjaminfa214e42016-05-10 17:03:10 -0400715 if (ssl->s3->send_shutdown != ssl_shutdown_close_notify) {
716 /* Send a close_notify. */
717 if (ssl3_send_alert(ssl, SSL3_AL_WARNING, SSL_AD_CLOSE_NOTIFY) <= 0) {
718 return -1;
David Benjamin27bbae42015-09-13 00:54:37 -0400719 }
720 } else if (ssl->s3->alert_dispatch) {
David Benjaminfa214e42016-05-10 17:03:10 -0400721 /* Finish sending the close_notify. */
David Benjaminf0ee9072016-06-15 17:44:37 -0400722 if (ssl->method->dispatch_alert(ssl) <= 0) {
David Benjaminfa214e42016-05-10 17:03:10 -0400723 return -1;
David Benjamin27bbae42015-09-13 00:54:37 -0400724 }
David Benjaminfa214e42016-05-10 17:03:10 -0400725 } else if (ssl->s3->recv_shutdown != ssl_shutdown_close_notify) {
726 /* Wait for the peer's close_notify. */
David Benjaminf0ee9072016-06-15 17:44:37 -0400727 ssl->method->read_close_notify(ssl);
David Benjaminfa214e42016-05-10 17:03:10 -0400728 if (ssl->s3->recv_shutdown != ssl_shutdown_close_notify) {
729 return -1;
David Benjamin27bbae42015-09-13 00:54:37 -0400730 }
731 }
732
David Benjaminfa214e42016-05-10 17:03:10 -0400733 /* Return 0 for unidirectional shutdown and 1 for bidirectional shutdown. */
734 return ssl->s3->recv_shutdown == ssl_shutdown_close_notify;
David Benjamin27bbae42015-09-13 00:54:37 -0400735}
736
737int SSL_get_error(const SSL *ssl, int ret_code) {
738 int reason;
739 uint32_t err;
740 BIO *bio;
741
742 if (ret_code > 0) {
743 return SSL_ERROR_NONE;
744 }
745
746 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
747 * where we do encode the error */
748 err = ERR_peek_error();
749 if (err != 0) {
750 if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
751 return SSL_ERROR_SYSCALL;
752 }
753 return SSL_ERROR_SSL;
754 }
755
756 if (ret_code == 0) {
David Benjaminfa214e42016-05-10 17:03:10 -0400757 if (ssl->s3->recv_shutdown == ssl_shutdown_close_notify) {
David Benjamin27bbae42015-09-13 00:54:37 -0400758 return SSL_ERROR_ZERO_RETURN;
759 }
760 /* An EOF was observed which violates the protocol, and the underlying
761 * transport does not participate in the error queue. Bubble up to the
762 * caller. */
763 return SSL_ERROR_SYSCALL;
764 }
765
766 if (SSL_want_session(ssl)) {
767 return SSL_ERROR_PENDING_SESSION;
768 }
769
770 if (SSL_want_certificate(ssl)) {
771 return SSL_ERROR_PENDING_CERTIFICATE;
772 }
773
774 if (SSL_want_read(ssl)) {
775 bio = SSL_get_rbio(ssl);
776 if (BIO_should_read(bio)) {
777 return SSL_ERROR_WANT_READ;
778 }
779
780 if (BIO_should_write(bio)) {
781 /* This one doesn't make too much sense ... We never try to write to the
782 * rbio, and an application program where rbio and wbio are separate
783 * couldn't even know what it should wait for. However if we ever set
David Benjamin0d56f882015-12-19 17:05:56 -0500784 * ssl->rwstate incorrectly (so that we have SSL_want_read(ssl) instead of
785 * SSL_want_write(ssl)) and rbio and wbio *are* the same, this test works
David Benjamin27bbae42015-09-13 00:54:37 -0400786 * around that bug; so it might be safer to keep it. */
787 return SSL_ERROR_WANT_WRITE;
788 }
789
790 if (BIO_should_io_special(bio)) {
791 reason = BIO_get_retry_reason(bio);
792 if (reason == BIO_RR_CONNECT) {
793 return SSL_ERROR_WANT_CONNECT;
794 }
795
796 if (reason == BIO_RR_ACCEPT) {
797 return SSL_ERROR_WANT_ACCEPT;
798 }
799
800 return SSL_ERROR_SYSCALL; /* unknown */
801 }
802 }
803
804 if (SSL_want_write(ssl)) {
805 bio = SSL_get_wbio(ssl);
806 if (BIO_should_write(bio)) {
807 return SSL_ERROR_WANT_WRITE;
808 }
809
810 if (BIO_should_read(bio)) {
811 /* See above (SSL_want_read(ssl) with BIO_should_write(bio)) */
812 return SSL_ERROR_WANT_READ;
813 }
814
815 if (BIO_should_io_special(bio)) {
816 reason = BIO_get_retry_reason(bio);
817 if (reason == BIO_RR_CONNECT) {
818 return SSL_ERROR_WANT_CONNECT;
819 }
820
821 if (reason == BIO_RR_ACCEPT) {
822 return SSL_ERROR_WANT_ACCEPT;
823 }
824
825 return SSL_ERROR_SYSCALL;
826 }
827 }
828
829 if (SSL_want_x509_lookup(ssl)) {
830 return SSL_ERROR_WANT_X509_LOOKUP;
831 }
832
833 if (SSL_want_channel_id_lookup(ssl)) {
834 return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
835 }
836
837 if (SSL_want_private_key_operation(ssl)) {
838 return SSL_ERROR_WANT_PRIVATE_KEY_OPERATION;
839 }
840
841 return SSL_ERROR_SYSCALL;
842}
843
844void SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) {
845 ctx->min_version = version;
846}
847
848void SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) {
849 ctx->max_version = version;
850}
851
852void SSL_set_min_version(SSL *ssl, uint16_t version) {
853 ssl->min_version = version;
854}
855
856void SSL_set_max_version(SSL *ssl, uint16_t version) {
857 ssl->max_version = version;
858}
859
860uint32_t SSL_CTX_set_options(SSL_CTX *ctx, uint32_t options) {
861 ctx->options |= options;
862 return ctx->options;
863}
864
865uint32_t SSL_CTX_clear_options(SSL_CTX *ctx, uint32_t options) {
866 ctx->options &= ~options;
867 return ctx->options;
868}
869
870uint32_t SSL_CTX_get_options(const SSL_CTX *ctx) { return ctx->options; }
871
872uint32_t SSL_set_options(SSL *ssl, uint32_t options) {
873 ssl->options |= options;
874 return ssl->options;
875}
876
877uint32_t SSL_clear_options(SSL *ssl, uint32_t options) {
878 ssl->options &= ~options;
879 return ssl->options;
880}
881
882uint32_t SSL_get_options(const SSL *ssl) { return ssl->options; }
883
884uint32_t SSL_CTX_set_mode(SSL_CTX *ctx, uint32_t mode) {
885 ctx->mode |= mode;
886 return ctx->mode;
887}
888
889uint32_t SSL_CTX_clear_mode(SSL_CTX *ctx, uint32_t mode) {
890 ctx->mode &= ~mode;
891 return ctx->mode;
892}
893
894uint32_t SSL_CTX_get_mode(const SSL_CTX *ctx) { return ctx->mode; }
895
896uint32_t SSL_set_mode(SSL *ssl, uint32_t mode) {
897 ssl->mode |= mode;
898 return ssl->mode;
899}
900
901uint32_t SSL_clear_mode(SSL *ssl, uint32_t mode) {
902 ssl->mode &= ~mode;
903 return ssl->mode;
904}
905
906uint32_t SSL_get_mode(const SSL *ssl) { return ssl->mode; }
907
David Benjaminee0c8272015-09-13 01:03:54 -0400908X509 *SSL_get_peer_certificate(const SSL *ssl) {
909 if (ssl == NULL || ssl->session == NULL || ssl->session->peer == NULL) {
910 return NULL;
911 }
912 return X509_up_ref(ssl->session->peer);
913}
914
915STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *ssl) {
916 if (ssl == NULL || ssl->session == NULL) {
917 return NULL;
918 }
919 return ssl->session->cert_chain;
920}
921
922int SSL_get_tls_unique(const SSL *ssl, uint8_t *out, size_t *out_len,
923 size_t max_out) {
924 /* The tls-unique value is the first Finished message in the handshake, which
925 * is the client's in a full handshake and the server's for a resumption. See
926 * https://tools.ietf.org/html/rfc5929#section-3.1. */
927 const uint8_t *finished = ssl->s3->previous_client_finished;
928 size_t finished_len = ssl->s3->previous_client_finished_len;
929 if (ssl->hit) {
930 /* tls-unique is broken for resumed sessions unless EMS is used. */
931 if (!ssl->session->extended_master_secret) {
932 goto err;
933 }
934 finished = ssl->s3->previous_server_finished;
935 finished_len = ssl->s3->previous_server_finished_len;
936 }
937
938 if (!ssl->s3->initial_handshake_complete ||
939 ssl->version < TLS1_VERSION) {
940 goto err;
941 }
942
943 *out_len = finished_len;
944 if (finished_len > max_out) {
945 *out_len = max_out;
946 }
947
948 memcpy(out, finished, *out_len);
949 return 1;
950
951err:
952 *out_len = 0;
953 memset(out, 0, max_out);
954 return 0;
955}
956
Adam Langleyfcf25832014-12-18 17:42:32 -0800957int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
David Benjamindafbdd42015-09-14 01:40:10 -0400958 unsigned sid_ctx_len) {
959 if (sid_ctx_len > sizeof(ctx->sid_ctx)) {
David Benjamin3570d732015-06-29 00:28:17 -0400960 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -0800961 return 0;
962 }
963 ctx->sid_ctx_length = sid_ctx_len;
964 memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700965
Adam Langleyfcf25832014-12-18 17:42:32 -0800966 return 1;
967}
Adam Langley95c29f32014-06-20 12:00:00 -0700968
Adam Langleyfcf25832014-12-18 17:42:32 -0800969int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
David Benjamindafbdd42015-09-14 01:40:10 -0400970 unsigned sid_ctx_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800971 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
David Benjamin3570d732015-06-29 00:28:17 -0400972 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -0800973 return 0;
974 }
975 ssl->sid_ctx_length = sid_ctx_len;
976 memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700977
Adam Langleyfcf25832014-12-18 17:42:32 -0800978 return 1;
979}
Adam Langley95c29f32014-06-20 12:00:00 -0700980
David Benjamin59937042015-09-19 13:04:22 -0400981int SSL_CTX_set_purpose(SSL_CTX *ctx, int purpose) {
982 return X509_VERIFY_PARAM_set_purpose(ctx->param, purpose);
Adam Langleyfcf25832014-12-18 17:42:32 -0800983}
984
David Benjamin59937042015-09-19 13:04:22 -0400985int SSL_set_purpose(SSL *ssl, int purpose) {
986 return X509_VERIFY_PARAM_set_purpose(ssl->param, purpose);
Adam Langleyfcf25832014-12-18 17:42:32 -0800987}
988
David Benjamin59937042015-09-19 13:04:22 -0400989int SSL_CTX_set_trust(SSL_CTX *ctx, int trust) {
990 return X509_VERIFY_PARAM_set_trust(ctx->param, trust);
Adam Langleyfcf25832014-12-18 17:42:32 -0800991}
992
David Benjamin59937042015-09-19 13:04:22 -0400993int SSL_set_trust(SSL *ssl, int trust) {
994 return X509_VERIFY_PARAM_set_trust(ssl->param, trust);
Adam Langleyfcf25832014-12-18 17:42:32 -0800995}
996
David Benjamin59937042015-09-19 13:04:22 -0400997int SSL_CTX_set1_param(SSL_CTX *ctx, const X509_VERIFY_PARAM *param) {
998 return X509_VERIFY_PARAM_set1(ctx->param, param);
Adam Langleyfcf25832014-12-18 17:42:32 -0800999}
1000
David Benjamin59937042015-09-19 13:04:22 -04001001int SSL_set1_param(SSL *ssl, const X509_VERIFY_PARAM *param) {
1002 return X509_VERIFY_PARAM_set1(ssl->param, param);
Adam Langleyfcf25832014-12-18 17:42:32 -08001003}
Adam Langley95c29f32014-06-20 12:00:00 -07001004
Adam Langley858a88d2014-06-20 12:00:00 -07001005void ssl_cipher_preference_list_free(
Adam Langleyfcf25832014-12-18 17:42:32 -08001006 struct ssl_cipher_preference_list_st *cipher_list) {
David Benjamin5d1ec732015-04-22 13:38:00 -04001007 if (cipher_list == NULL) {
1008 return;
1009 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001010 sk_SSL_CIPHER_free(cipher_list->ciphers);
1011 OPENSSL_free(cipher_list->in_group_flags);
1012 OPENSSL_free(cipher_list);
1013}
Adam Langley858a88d2014-06-20 12:00:00 -07001014
Adam Langleyfcf25832014-12-18 17:42:32 -08001015X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) { return ctx->param; }
Adam Langley95c29f32014-06-20 12:00:00 -07001016
Adam Langleyfcf25832014-12-18 17:42:32 -08001017X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) { return ssl->param; }
Adam Langley95c29f32014-06-20 12:00:00 -07001018
David Benjamin7481d392015-07-05 19:38:46 -04001019void SSL_certs_clear(SSL *ssl) { ssl_cert_clear_certs(ssl->cert); }
Adam Langley95c29f32014-06-20 12:00:00 -07001020
David Benjamin066fe0a2015-10-17 21:11:33 -04001021int SSL_get_fd(const SSL *ssl) { return SSL_get_rfd(ssl); }
Adam Langley95c29f32014-06-20 12:00:00 -07001022
David Benjamin066fe0a2015-10-17 21:11:33 -04001023int SSL_get_rfd(const SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001024 int ret = -1;
David Benjamin066fe0a2015-10-17 21:11:33 -04001025 BIO *b = BIO_find_type(SSL_get_rbio(ssl), BIO_TYPE_DESCRIPTOR);
1026 if (b != NULL) {
1027 BIO_get_fd(b, &ret);
Adam Langleyfcf25832014-12-18 17:42:32 -08001028 }
1029 return ret;
1030}
Adam Langley95c29f32014-06-20 12:00:00 -07001031
David Benjamin066fe0a2015-10-17 21:11:33 -04001032int SSL_get_wfd(const SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001033 int ret = -1;
David Benjamin066fe0a2015-10-17 21:11:33 -04001034 BIO *b = BIO_find_type(SSL_get_wbio(ssl), BIO_TYPE_DESCRIPTOR);
1035 if (b != NULL) {
1036 BIO_get_fd(b, &ret);
Adam Langleyfcf25832014-12-18 17:42:32 -08001037 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001038 return ret;
1039}
Adam Langley95c29f32014-06-20 12:00:00 -07001040
David Benjamin066fe0a2015-10-17 21:11:33 -04001041int SSL_set_fd(SSL *ssl, int fd) {
David Benjamin22ce9b22016-03-06 19:26:52 -05001042 BIO *bio = BIO_new(BIO_s_socket());
Adam Langleyfcf25832014-12-18 17:42:32 -08001043 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001044 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
David Benjamin066fe0a2015-10-17 21:11:33 -04001045 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001046 }
1047 BIO_set_fd(bio, fd, BIO_NOCLOSE);
David Benjamin066fe0a2015-10-17 21:11:33 -04001048 SSL_set_bio(ssl, bio, bio);
1049 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001050}
Adam Langley95c29f32014-06-20 12:00:00 -07001051
David Benjamin066fe0a2015-10-17 21:11:33 -04001052int SSL_set_wfd(SSL *ssl, int fd) {
David Benjamin2f871122016-05-20 14:27:17 -04001053 BIO *rbio = SSL_get_rbio(ssl);
1054 if (rbio == NULL || BIO_method_type(rbio) != BIO_TYPE_SOCKET ||
1055 BIO_get_fd(rbio, NULL) != fd) {
David Benjamin22ce9b22016-03-06 19:26:52 -05001056 BIO *bio = BIO_new(BIO_s_socket());
Adam Langleyfcf25832014-12-18 17:42:32 -08001057 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001058 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
David Benjamin066fe0a2015-10-17 21:11:33 -04001059 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001060 }
1061 BIO_set_fd(bio, fd, BIO_NOCLOSE);
David Benjamin2f871122016-05-20 14:27:17 -04001062 SSL_set_bio(ssl, rbio, bio);
Adam Langleyfcf25832014-12-18 17:42:32 -08001063 } else {
David Benjamin2f871122016-05-20 14:27:17 -04001064 SSL_set_bio(ssl, rbio, rbio);
Adam Langleyfcf25832014-12-18 17:42:32 -08001065 }
1066
David Benjamin066fe0a2015-10-17 21:11:33 -04001067 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001068}
Adam Langley95c29f32014-06-20 12:00:00 -07001069
David Benjamin066fe0a2015-10-17 21:11:33 -04001070int SSL_set_rfd(SSL *ssl, int fd) {
David Benjamin2f871122016-05-20 14:27:17 -04001071 BIO *wbio = SSL_get_wbio(ssl);
1072 if (wbio == NULL || BIO_method_type(wbio) != BIO_TYPE_SOCKET ||
1073 BIO_get_fd(wbio, NULL) != fd) {
David Benjamin22ce9b22016-03-06 19:26:52 -05001074 BIO *bio = BIO_new(BIO_s_socket());
Adam Langleyfcf25832014-12-18 17:42:32 -08001075 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001076 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
David Benjamin066fe0a2015-10-17 21:11:33 -04001077 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001078 }
1079 BIO_set_fd(bio, fd, BIO_NOCLOSE);
David Benjamin2f871122016-05-20 14:27:17 -04001080 SSL_set_bio(ssl, bio, wbio);
Adam Langleyfcf25832014-12-18 17:42:32 -08001081 } else {
David Benjamin2f871122016-05-20 14:27:17 -04001082 SSL_set_bio(ssl, wbio, wbio);
Adam Langleyfcf25832014-12-18 17:42:32 -08001083 }
David Benjamin066fe0a2015-10-17 21:11:33 -04001084 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001085}
Adam Langley95c29f32014-06-20 12:00:00 -07001086
David Benjamin1a1b34d2015-10-17 12:51:52 -04001087size_t SSL_get_finished(const SSL *ssl, void *buf, size_t count) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001088 size_t ret = 0;
1089
David Benjamin1a1b34d2015-10-17 12:51:52 -04001090 if (ssl->s3 != NULL) {
1091 ret = ssl->s3->tmp.finish_md_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08001092 if (count > ret) {
1093 count = ret;
1094 }
David Benjamin1a1b34d2015-10-17 12:51:52 -04001095 memcpy(buf, ssl->s3->tmp.finish_md, count);
Adam Langleyfcf25832014-12-18 17:42:32 -08001096 }
1097
1098 return ret;
1099}
Adam Langley95c29f32014-06-20 12:00:00 -07001100
David Benjamin1a1b34d2015-10-17 12:51:52 -04001101size_t SSL_get_peer_finished(const SSL *ssl, void *buf, size_t count) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001102 size_t ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001103
David Benjamin1a1b34d2015-10-17 12:51:52 -04001104 if (ssl->s3 != NULL) {
1105 ret = ssl->s3->tmp.peer_finish_md_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08001106 if (count > ret) {
1107 count = ret;
1108 }
David Benjamin1a1b34d2015-10-17 12:51:52 -04001109 memcpy(buf, ssl->s3->tmp.peer_finish_md, count);
Adam Langleyfcf25832014-12-18 17:42:32 -08001110 }
Adam Langley95c29f32014-06-20 12:00:00 -07001111
Adam Langleyfcf25832014-12-18 17:42:32 -08001112 return ret;
1113}
Adam Langley95c29f32014-06-20 12:00:00 -07001114
David Benjamin59937042015-09-19 13:04:22 -04001115int SSL_get_verify_mode(const SSL *ssl) { return ssl->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -07001116
David Benjamin59937042015-09-19 13:04:22 -04001117int SSL_get_verify_depth(const SSL *ssl) {
1118 return X509_VERIFY_PARAM_get_depth(ssl->param);
Adam Langleyfcf25832014-12-18 17:42:32 -08001119}
Adam Langley95c29f32014-06-20 12:00:00 -07001120
David Benjamin42fea372015-09-19 01:22:44 -04001121int SSL_get_extms_support(const SSL *ssl) {
1122 return ssl->s3->tmp.extended_master_secret == 1;
Matt Braithwaitecd6f54b2015-09-17 12:54:42 -07001123}
1124
David Benjamin59937042015-09-19 13:04:22 -04001125int (*SSL_get_verify_callback(const SSL *ssl))(int, X509_STORE_CTX *) {
1126 return ssl->verify_callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08001127}
Adam Langley95c29f32014-06-20 12:00:00 -07001128
Adam Langleyfcf25832014-12-18 17:42:32 -08001129int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) { return ctx->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -07001130
Adam Langleyfcf25832014-12-18 17:42:32 -08001131int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) {
1132 return X509_VERIFY_PARAM_get_depth(ctx->param);
1133}
Adam Langley95c29f32014-06-20 12:00:00 -07001134
David Benjamin59937042015-09-19 13:04:22 -04001135int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(
1136 int ok, X509_STORE_CTX *store_ctx) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001137 return ctx->default_verify_callback;
1138}
Adam Langley95c29f32014-06-20 12:00:00 -07001139
David Benjamin59937042015-09-19 13:04:22 -04001140void SSL_set_verify(SSL *ssl, int mode,
1141 int (*callback)(int ok, X509_STORE_CTX *store_ctx)) {
1142 ssl->verify_mode = mode;
Adam Langleyfcf25832014-12-18 17:42:32 -08001143 if (callback != NULL) {
David Benjamin59937042015-09-19 13:04:22 -04001144 ssl->verify_callback = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08001145 }
1146}
Adam Langley95c29f32014-06-20 12:00:00 -07001147
David Benjamin59937042015-09-19 13:04:22 -04001148void SSL_set_verify_depth(SSL *ssl, int depth) {
1149 X509_VERIFY_PARAM_set_depth(ssl->param, depth);
Adam Langleyfcf25832014-12-18 17:42:32 -08001150}
Adam Langley95c29f32014-06-20 12:00:00 -07001151
David Benjamin9a41d1b2015-05-16 01:30:09 -04001152int SSL_CTX_get_read_ahead(const SSL_CTX *ctx) { return 0; }
Adam Langley95c29f32014-06-20 12:00:00 -07001153
David Benjamin0d56f882015-12-19 17:05:56 -05001154int SSL_get_read_ahead(const SSL *ssl) { return 0; }
Adam Langley95c29f32014-06-20 12:00:00 -07001155
David Benjamin9a41d1b2015-05-16 01:30:09 -04001156void SSL_CTX_set_read_ahead(SSL_CTX *ctx, int yes) { }
David Benjamin61ecccf2015-05-05 09:44:51 -04001157
David Benjamin0d56f882015-12-19 17:05:56 -05001158void SSL_set_read_ahead(SSL *ssl, int yes) { }
David Benjamin61ecccf2015-05-05 09:44:51 -04001159
David Benjamin9f859492015-10-03 10:44:30 -04001160int SSL_pending(const SSL *ssl) {
1161 if (ssl->s3->rrec.type != SSL3_RT_APPLICATION_DATA) {
1162 return 0;
1163 }
1164 return ssl->s3->rrec.length;
Adam Langleyfcf25832014-12-18 17:42:32 -08001165}
Adam Langley95c29f32014-06-20 12:00:00 -07001166
Adam Langley95c29f32014-06-20 12:00:00 -07001167/* Fix this so it checks all the valid key/cert options */
Adam Langleyfcf25832014-12-18 17:42:32 -08001168int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
David Benjamin651b3d92015-08-09 12:07:25 -04001169 if (ctx->cert->x509 == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001170 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -08001171 return 0;
1172 }
1173
David Benjamind1d80782015-07-05 11:54:09 -04001174 if (ctx->cert->privatekey == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001175 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -08001176 return 0;
1177 }
1178
David Benjamind1d80782015-07-05 11:54:09 -04001179 return X509_check_private_key(ctx->cert->x509, ctx->cert->privatekey);
Adam Langleyfcf25832014-12-18 17:42:32 -08001180}
Adam Langley95c29f32014-06-20 12:00:00 -07001181
1182/* Fix this function so that it takes an optional type parameter */
Adam Langleyfcf25832014-12-18 17:42:32 -08001183int SSL_check_private_key(const SSL *ssl) {
David Benjamind1d80782015-07-05 11:54:09 -04001184 if (ssl->cert->x509 == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001185 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -08001186 return 0;
1187 }
David Benjamin0b145c22014-11-26 20:10:09 -05001188
David Benjamind1d80782015-07-05 11:54:09 -04001189 if (ssl->cert->privatekey == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001190 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -08001191 return 0;
1192 }
Adam Langley95c29f32014-06-20 12:00:00 -07001193
David Benjamind1d80782015-07-05 11:54:09 -04001194 return X509_check_private_key(ssl->cert->x509, ssl->cert->privatekey);
Adam Langleyfcf25832014-12-18 17:42:32 -08001195}
Adam Langley95c29f32014-06-20 12:00:00 -07001196
David Benjamin42fea372015-09-19 01:22:44 -04001197long SSL_get_default_timeout(const SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001198 return SSL_DEFAULT_SESSION_TIMEOUT;
1199}
Adam Langley95c29f32014-06-20 12:00:00 -07001200
David Benjamin44d3eed2015-05-21 01:29:55 -04001201int SSL_renegotiate(SSL *ssl) {
1202 /* Caller-initiated renegotiation is not supported. */
David Benjamin3570d732015-06-29 00:28:17 -04001203 OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
David Benjamin44d3eed2015-05-21 01:29:55 -04001204 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001205}
Adam Langley95c29f32014-06-20 12:00:00 -07001206
David Benjamin44d3eed2015-05-21 01:29:55 -04001207int SSL_renegotiate_pending(SSL *ssl) {
1208 return SSL_in_init(ssl) && ssl->s3->initial_handshake_complete;
Adam Langleyfcf25832014-12-18 17:42:32 -08001209}
Adam Langley95c29f32014-06-20 12:00:00 -07001210
David Benjamin936aada2016-06-07 19:09:22 -04001211int SSL_total_renegotiations(const SSL *ssl) {
1212 return ssl->s3->total_renegotiations;
1213}
1214
David Benjamin1d0a1942015-04-26 15:35:35 -04001215size_t SSL_CTX_get_max_cert_list(const SSL_CTX *ctx) {
1216 return ctx->max_cert_list;
1217}
1218
1219void SSL_CTX_set_max_cert_list(SSL_CTX *ctx, size_t max_cert_list) {
1220 if (max_cert_list > kMaxHandshakeSize) {
1221 max_cert_list = kMaxHandshakeSize;
1222 }
1223 ctx->max_cert_list = (uint32_t)max_cert_list;
1224}
1225
1226size_t SSL_get_max_cert_list(const SSL *ssl) {
1227 return ssl->max_cert_list;
1228}
1229
1230void SSL_set_max_cert_list(SSL *ssl, size_t max_cert_list) {
1231 if (max_cert_list > kMaxHandshakeSize) {
1232 max_cert_list = kMaxHandshakeSize;
1233 }
1234 ssl->max_cert_list = (uint32_t)max_cert_list;
1235}
1236
Adam Langleyce9d85e2016-01-24 15:58:39 -08001237int SSL_CTX_set_max_send_fragment(SSL_CTX *ctx, size_t max_send_fragment) {
David Benjamin1d0a1942015-04-26 15:35:35 -04001238 if (max_send_fragment < 512) {
1239 max_send_fragment = 512;
1240 }
1241 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1242 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1243 }
1244 ctx->max_send_fragment = (uint16_t)max_send_fragment;
Adam Langleyce9d85e2016-01-24 15:58:39 -08001245
1246 return 1;
David Benjamin1d0a1942015-04-26 15:35:35 -04001247}
1248
Adam Langleyce9d85e2016-01-24 15:58:39 -08001249int SSL_set_max_send_fragment(SSL *ssl, size_t max_send_fragment) {
David Benjamin1d0a1942015-04-26 15:35:35 -04001250 if (max_send_fragment < 512) {
1251 max_send_fragment = 512;
1252 }
1253 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1254 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1255 }
1256 ssl->max_send_fragment = (uint16_t)max_send_fragment;
Adam Langleyce9d85e2016-01-24 15:58:39 -08001257
1258 return 1;
David Benjamin1d0a1942015-04-26 15:35:35 -04001259}
1260
David Benjamincb9cf792015-05-05 09:46:14 -04001261int SSL_set_mtu(SSL *ssl, unsigned mtu) {
1262 if (!SSL_IS_DTLS(ssl) || mtu < dtls1_min_mtu()) {
1263 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001264 }
David Benjamincb9cf792015-05-05 09:46:14 -04001265 ssl->d1->mtu = mtu;
1266 return 1;
1267}
1268
1269int SSL_get_secure_renegotiation_support(const SSL *ssl) {
1270 return ssl->s3->send_connection_binding;
1271}
1272
Adam Langleyfcf25832014-12-18 17:42:32 -08001273LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) { return ctx->sessions; }
1274
David Benjamin71f7d3d2015-05-05 09:46:38 -04001275size_t SSL_CTX_sess_number(const SSL_CTX *ctx) {
1276 return lh_SSL_SESSION_num_items(ctx->sessions);
1277}
1278
1279unsigned long SSL_CTX_sess_set_cache_size(SSL_CTX *ctx, unsigned long size) {
1280 unsigned long ret = ctx->session_cache_size;
1281 ctx->session_cache_size = size;
1282 return ret;
1283}
1284
1285unsigned long SSL_CTX_sess_get_cache_size(const SSL_CTX *ctx) {
1286 return ctx->session_cache_size;
1287}
1288
1289int SSL_CTX_set_session_cache_mode(SSL_CTX *ctx, int mode) {
1290 int ret = ctx->session_cache_mode;
1291 ctx->session_cache_mode = mode;
1292 return ret;
1293}
1294
1295int SSL_CTX_get_session_cache_mode(const SSL_CTX *ctx) {
1296 return ctx->session_cache_mode;
1297}
1298
David Benjamin936aada2016-06-07 19:09:22 -04001299
1300int SSL_CTX_get_tlsext_ticket_keys(SSL_CTX *ctx, void *out, size_t len) {
1301 if (out == NULL) {
1302 return 48;
1303 }
1304 if (len != 48) {
1305 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH);
1306 return 0;
1307 }
1308 uint8_t *out_bytes = out;
1309 memcpy(out_bytes, ctx->tlsext_tick_key_name, 16);
1310 memcpy(out_bytes + 16, ctx->tlsext_tick_hmac_key, 16);
1311 memcpy(out_bytes + 32, ctx->tlsext_tick_aes_key, 16);
1312 return 1;
1313}
1314
1315int SSL_CTX_set_tlsext_ticket_keys(SSL_CTX *ctx, const void *in, size_t len) {
1316 if (in == NULL) {
1317 return 48;
1318 }
1319 if (len != 48) {
1320 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH);
1321 return 0;
1322 }
1323 const uint8_t *in_bytes = in;
1324 memcpy(ctx->tlsext_tick_key_name, in_bytes, 16);
1325 memcpy(ctx->tlsext_tick_hmac_key, in_bytes + 16, 16);
1326 memcpy(ctx->tlsext_tick_aes_key, in_bytes + 32, 16);
1327 return 1;
1328}
1329
1330int SSL_CTX_set_tlsext_ticket_key_cb(
1331 SSL_CTX *ctx, int (*callback)(SSL *ssl, uint8_t *key_name, uint8_t *iv,
1332 EVP_CIPHER_CTX *ctx, HMAC_CTX *hmac_ctx,
1333 int encrypt)) {
1334 ctx->tlsext_ticket_key_cb = callback;
1335 return 1;
1336}
1337
1338int SSL_CTX_set1_curves(SSL_CTX *ctx, const int *curves, size_t curves_len) {
1339 return tls1_set_curves(&ctx->supported_group_list,
1340 &ctx->supported_group_list_len, curves,
1341 curves_len);
1342}
1343
1344int SSL_set1_curves(SSL *ssl, const int *curves, size_t curves_len) {
1345 return tls1_set_curves(&ssl->supported_group_list,
1346 &ssl->supported_group_list_len, curves,
1347 curves_len);
1348}
1349
1350int SSL_CTX_set_tmp_dh(SSL_CTX *ctx, const DH *dh) {
1351 DH_free(ctx->cert->dh_tmp);
1352 ctx->cert->dh_tmp = DHparams_dup(dh);
1353 if (ctx->cert->dh_tmp == NULL) {
1354 OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB);
1355 return 0;
1356 }
1357 return 1;
1358}
1359
1360int SSL_set_tmp_dh(SSL *ssl, const DH *dh) {
1361 DH_free(ssl->cert->dh_tmp);
1362 ssl->cert->dh_tmp = DHparams_dup(dh);
1363 if (ssl->cert->dh_tmp == NULL) {
1364 OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB);
1365 return 0;
1366 }
1367 return 1;
1368}
1369
David Benjamin32876b32015-09-20 12:17:03 -04001370STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *ssl) {
1371 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001372 return NULL;
1373 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001374
David Benjamin32876b32015-09-20 12:17:03 -04001375 if (ssl->cipher_list != NULL) {
1376 return ssl->cipher_list->ciphers;
Adam Langleyfcf25832014-12-18 17:42:32 -08001377 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001378
David Benjamin78f8aab2016-03-10 16:33:58 -05001379 if (ssl->version >= TLS1_1_VERSION && ssl->ctx->cipher_list_tls11 != NULL) {
David Benjamin32876b32015-09-20 12:17:03 -04001380 return ssl->ctx->cipher_list_tls11->ciphers;
Adam Langleyfcf25832014-12-18 17:42:32 -08001381 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001382
David Benjamin78f8aab2016-03-10 16:33:58 -05001383 if (ssl->version >= TLS1_VERSION && ssl->ctx->cipher_list_tls10 != NULL) {
David Benjamin32876b32015-09-20 12:17:03 -04001384 return ssl->ctx->cipher_list_tls10->ciphers;
Adam Langleycef75832015-09-03 14:51:12 -07001385 }
1386
David Benjamin78f8aab2016-03-10 16:33:58 -05001387 if (ssl->ctx->cipher_list != NULL) {
David Benjamin32876b32015-09-20 12:17:03 -04001388 return ssl->ctx->cipher_list->ciphers;
Adam Langleyfcf25832014-12-18 17:42:32 -08001389 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001390
Adam Langleyfcf25832014-12-18 17:42:32 -08001391 return NULL;
1392}
Adam Langley95c29f32014-06-20 12:00:00 -07001393
Adam Langleyfcf25832014-12-18 17:42:32 -08001394/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001395 * algorithm id */
David Benjamin0d56f882015-12-19 17:05:56 -05001396STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *ssl) {
1397 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001398 return NULL;
1399 }
Adam Langley95c29f32014-06-20 12:00:00 -07001400
David Benjamin0d56f882015-12-19 17:05:56 -05001401 if (ssl->cipher_list_by_id != NULL) {
1402 return ssl->cipher_list_by_id;
Adam Langleyfcf25832014-12-18 17:42:32 -08001403 }
Adam Langley95c29f32014-06-20 12:00:00 -07001404
David Benjamin78f8aab2016-03-10 16:33:58 -05001405 if (ssl->ctx->cipher_list_by_id != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05001406 return ssl->ctx->cipher_list_by_id;
Adam Langleyfcf25832014-12-18 17:42:32 -08001407 }
Adam Langley95c29f32014-06-20 12:00:00 -07001408
Adam Langleyfcf25832014-12-18 17:42:32 -08001409 return NULL;
1410}
Adam Langley95c29f32014-06-20 12:00:00 -07001411
David Benjamin32876b32015-09-20 12:17:03 -04001412const char *SSL_get_cipher_list(const SSL *ssl, int n) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001413 const SSL_CIPHER *c;
David Benjamin60da0cd2015-05-03 15:21:28 -04001414 STACK_OF(SSL_CIPHER) *sk;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001415
David Benjamin32876b32015-09-20 12:17:03 -04001416 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001417 return NULL;
1418 }
Adam Langley95c29f32014-06-20 12:00:00 -07001419
David Benjamin32876b32015-09-20 12:17:03 -04001420 sk = SSL_get_ciphers(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001421 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
1422 return NULL;
1423 }
Adam Langley95c29f32014-06-20 12:00:00 -07001424
Adam Langleyfcf25832014-12-18 17:42:32 -08001425 c = sk_SSL_CIPHER_value(sk, n);
1426 if (c == NULL) {
1427 return NULL;
1428 }
Adam Langley95c29f32014-06-20 12:00:00 -07001429
Adam Langleyfcf25832014-12-18 17:42:32 -08001430 return c->name;
1431}
David Benjamin5491e3f2014-09-29 19:33:09 -04001432
Adam Langleyfcf25832014-12-18 17:42:32 -08001433int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
David Benjamin32876b32015-09-20 12:17:03 -04001434 STACK_OF(SSL_CIPHER) *cipher_list = ssl_create_cipher_list(
1435 ctx->method, &ctx->cipher_list, &ctx->cipher_list_by_id, str);
1436 if (cipher_list == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001437 return 0;
David Benjamin32876b32015-09-20 12:17:03 -04001438 }
1439
1440 /* |ssl_create_cipher_list| may succeed but return an empty cipher list. */
1441 if (sk_SSL_CIPHER_num(cipher_list) == 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001442 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
Adam Langleyfcf25832014-12-18 17:42:32 -08001443 return 0;
1444 }
Adam Langley95c29f32014-06-20 12:00:00 -07001445
Adam Langleyfcf25832014-12-18 17:42:32 -08001446 return 1;
1447}
David Benjamin39482a12014-07-20 13:30:15 -04001448
Adam Langleycef75832015-09-03 14:51:12 -07001449int SSL_CTX_set_cipher_list_tls10(SSL_CTX *ctx, const char *str) {
David Benjamin32876b32015-09-20 12:17:03 -04001450 STACK_OF(SSL_CIPHER) *cipher_list = ssl_create_cipher_list(
1451 ctx->method, &ctx->cipher_list_tls10, NULL, str);
1452 if (cipher_list == NULL) {
Adam Langleycef75832015-09-03 14:51:12 -07001453 return 0;
David Benjamin32876b32015-09-20 12:17:03 -04001454 }
1455
1456 /* |ssl_create_cipher_list| may succeed but return an empty cipher list. */
1457 if (sk_SSL_CIPHER_num(cipher_list) == 0) {
Adam Langleycef75832015-09-03 14:51:12 -07001458 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
1459 return 0;
1460 }
1461
1462 return 1;
1463}
1464
Adam Langleyfcf25832014-12-18 17:42:32 -08001465int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str) {
David Benjamin32876b32015-09-20 12:17:03 -04001466 STACK_OF(SSL_CIPHER) *cipher_list = ssl_create_cipher_list(
1467 ctx->method, &ctx->cipher_list_tls11, NULL, str);
1468 if (cipher_list == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001469 return 0;
David Benjamin32876b32015-09-20 12:17:03 -04001470 }
1471
1472 /* |ssl_create_cipher_list| may succeed but return an empty cipher list. */
1473 if (sk_SSL_CIPHER_num(cipher_list) == 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001474 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
Adam Langleyfcf25832014-12-18 17:42:32 -08001475 return 0;
1476 }
David Benjamin9f2c0d72014-10-21 22:00:19 -04001477
Adam Langleyfcf25832014-12-18 17:42:32 -08001478 return 1;
1479}
Adam Langley95c29f32014-06-20 12:00:00 -07001480
David Benjamin32876b32015-09-20 12:17:03 -04001481int SSL_set_cipher_list(SSL *ssl, const char *str) {
1482 STACK_OF(SSL_CIPHER) *cipher_list = ssl_create_cipher_list(
1483 ssl->ctx->method, &ssl->cipher_list, &ssl->cipher_list_by_id, str);
1484 if (cipher_list == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001485 return 0;
David Benjamin32876b32015-09-20 12:17:03 -04001486 }
1487
1488 /* |ssl_create_cipher_list| may succeed but return an empty cipher list. */
1489 if (sk_SSL_CIPHER_num(cipher_list) == 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001490 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
Adam Langleyfcf25832014-12-18 17:42:32 -08001491 return 0;
1492 }
David Benjamin39482a12014-07-20 13:30:15 -04001493
Adam Langleyfcf25832014-12-18 17:42:32 -08001494 return 1;
1495}
Adam Langley95c29f32014-06-20 12:00:00 -07001496
David Benjamin0d56f882015-12-19 17:05:56 -05001497STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *ssl, const CBS *cbs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001498 CBS cipher_suites = *cbs;
1499 const SSL_CIPHER *c;
David Benjamin60da0cd2015-05-03 15:21:28 -04001500 STACK_OF(SSL_CIPHER) *sk;
Adam Langleyfcf25832014-12-18 17:42:32 -08001501
David Benjamin0d56f882015-12-19 17:05:56 -05001502 if (ssl->s3) {
1503 ssl->s3->send_connection_binding = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001504 }
1505
1506 if (CBS_len(&cipher_suites) % 2 != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001507 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
Adam Langleyfcf25832014-12-18 17:42:32 -08001508 return NULL;
1509 }
1510
1511 sk = sk_SSL_CIPHER_new_null();
1512 if (sk == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001513 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001514 goto err;
1515 }
1516
Adam Langleyfcf25832014-12-18 17:42:32 -08001517 while (CBS_len(&cipher_suites) > 0) {
1518 uint16_t cipher_suite;
1519
1520 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
David Benjamin3570d732015-06-29 00:28:17 -04001521 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001522 goto err;
1523 }
1524
1525 /* Check for SCSV. */
David Benjamin0d56f882015-12-19 17:05:56 -05001526 if (ssl->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001527 /* SCSV is fatal if renegotiating. */
David Benjamin0d56f882015-12-19 17:05:56 -05001528 if (ssl->s3->initial_handshake_complete) {
David Benjamin3570d732015-06-29 00:28:17 -04001529 OPENSSL_PUT_ERROR(SSL, SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
David Benjamin0d56f882015-12-19 17:05:56 -05001530 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001531 goto err;
1532 }
David Benjamin0d56f882015-12-19 17:05:56 -05001533 ssl->s3->send_connection_binding = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001534 continue;
1535 }
1536
1537 /* Check for FALLBACK_SCSV. */
David Benjamin0d56f882015-12-19 17:05:56 -05001538 if (ssl->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff)) {
1539 uint16_t max_version = ssl3_get_max_server_version(ssl);
1540 if (SSL_IS_DTLS(ssl) ? (uint16_t)ssl->version > max_version
1541 : (uint16_t)ssl->version < max_version) {
David Benjamin3570d732015-06-29 00:28:17 -04001542 OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK);
David Benjamin0d56f882015-12-19 17:05:56 -05001543 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
Adam Langleyfcf25832014-12-18 17:42:32 -08001544 goto err;
1545 }
1546 continue;
1547 }
1548
David Benjamina1c90a52015-05-30 17:03:14 -04001549 c = SSL_get_cipher_by_value(cipher_suite);
Adam Langleyfcf25832014-12-18 17:42:32 -08001550 if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
David Benjamin3570d732015-06-29 00:28:17 -04001551 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001552 goto err;
1553 }
1554 }
1555
1556 return sk;
David Benjamin9f2c0d72014-10-21 22:00:19 -04001557
Adam Langley95c29f32014-06-20 12:00:00 -07001558err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001559 sk_SSL_CIPHER_free(sk);
Adam Langleyfcf25832014-12-18 17:42:32 -08001560 return NULL;
1561}
Adam Langley95c29f32014-06-20 12:00:00 -07001562
David Benjamin07e13842015-10-17 13:48:04 -04001563const char *SSL_get_servername(const SSL *ssl, const int type) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001564 if (type != TLSEXT_NAMETYPE_host_name) {
1565 return NULL;
1566 }
Adam Langley95c29f32014-06-20 12:00:00 -07001567
David Benjamin07e13842015-10-17 13:48:04 -04001568 /* Historically, |SSL_get_servername| was also the configuration getter
1569 * corresponding to |SSL_set_tlsext_host_name|. */
1570 if (ssl->tlsext_hostname != NULL) {
1571 return ssl->tlsext_hostname;
1572 }
1573
1574 if (ssl->session == NULL) {
1575 return NULL;
1576 }
1577 return ssl->session->tlsext_hostname;
Adam Langleyfcf25832014-12-18 17:42:32 -08001578}
Adam Langley95c29f32014-06-20 12:00:00 -07001579
David Benjamin07e13842015-10-17 13:48:04 -04001580int SSL_get_servername_type(const SSL *ssl) {
1581 if (ssl->session != NULL && ssl->session->tlsext_hostname != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001582 return TLSEXT_NAMETYPE_host_name;
1583 }
Adam Langley95c29f32014-06-20 12:00:00 -07001584
Adam Langleyfcf25832014-12-18 17:42:32 -08001585 return -1;
1586}
Adam Langley95c29f32014-06-20 12:00:00 -07001587
Adam Langleyfcf25832014-12-18 17:42:32 -08001588void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
1589 ctx->signed_cert_timestamps_enabled = 1;
1590}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001591
Adam Langleyfcf25832014-12-18 17:42:32 -08001592int SSL_enable_signed_cert_timestamps(SSL *ssl) {
1593 ssl->signed_cert_timestamps_enabled = 1;
1594 return 1;
1595}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001596
Adam Langleyfcf25832014-12-18 17:42:32 -08001597void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
1598 ctx->ocsp_stapling_enabled = 1;
1599}
David Benjamin6c7aed02014-08-27 16:42:38 -04001600
Adam Langleyfcf25832014-12-18 17:42:32 -08001601int SSL_enable_ocsp_stapling(SSL *ssl) {
1602 ssl->ocsp_stapling_enabled = 1;
1603 return 1;
1604}
David Benjamin6c7aed02014-08-27 16:42:38 -04001605
Adam Langleyfcf25832014-12-18 17:42:32 -08001606void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
1607 size_t *out_len) {
1608 SSL_SESSION *session = ssl->session;
Adam Langley3cb50e02014-08-26 14:00:31 -07001609
Adam Langleyfcf25832014-12-18 17:42:32 -08001610 *out_len = 0;
1611 *out = NULL;
1612 if (ssl->server || !session || !session->tlsext_signed_cert_timestamp_list) {
1613 return;
1614 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001615
Adam Langleyfcf25832014-12-18 17:42:32 -08001616 *out = session->tlsext_signed_cert_timestamp_list;
1617 *out_len = session->tlsext_signed_cert_timestamp_list_length;
1618}
David Benjamin6c7aed02014-08-27 16:42:38 -04001619
Adam Langleyfcf25832014-12-18 17:42:32 -08001620void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
1621 size_t *out_len) {
1622 SSL_SESSION *session = ssl->session;
1623
1624 *out_len = 0;
1625 *out = NULL;
1626 if (ssl->server || !session || !session->ocsp_response) {
1627 return;
1628 }
1629 *out = session->ocsp_response;
1630 *out_len = session->ocsp_response_length;
1631}
David Benjamin6c7aed02014-08-27 16:42:38 -04001632
Paul Lietar4fac72e2015-09-09 13:44:55 +01001633int SSL_CTX_set_signed_cert_timestamp_list(SSL_CTX *ctx, const uint8_t *list,
1634 size_t list_len) {
1635 OPENSSL_free(ctx->signed_cert_timestamp_list);
1636 ctx->signed_cert_timestamp_list_length = 0;
1637
1638 ctx->signed_cert_timestamp_list = BUF_memdup(list, list_len);
1639 if (ctx->signed_cert_timestamp_list == NULL) {
1640 return 0;
1641 }
1642 ctx->signed_cert_timestamp_list_length = list_len;
1643
1644 return 1;
1645}
1646
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001647int SSL_CTX_set_ocsp_response(SSL_CTX *ctx, const uint8_t *response,
1648 size_t response_len) {
1649 OPENSSL_free(ctx->ocsp_response);
1650 ctx->ocsp_response_length = 0;
1651
1652 ctx->ocsp_response = BUF_memdup(response, response_len);
1653 if (ctx->ocsp_response == NULL) {
1654 return 0;
1655 }
1656 ctx->ocsp_response_length = response_len;
1657
1658 return 1;
1659}
1660
David Benjamin936aada2016-06-07 19:09:22 -04001661int SSL_set_tlsext_host_name(SSL *ssl, const char *name) {
1662 OPENSSL_free(ssl->tlsext_hostname);
1663 ssl->tlsext_hostname = NULL;
1664
1665 if (name == NULL) {
1666 return 1;
1667 }
1668
1669 size_t len = strlen(name);
1670 if (len == 0 || len > TLSEXT_MAXLEN_host_name) {
1671 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL3_EXT_INVALID_SERVERNAME);
1672 return 0;
1673 }
1674 ssl->tlsext_hostname = BUF_strdup(name);
1675 if (ssl->tlsext_hostname == NULL) {
1676 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1677 return 0;
1678 }
1679 return 1;
1680}
1681
1682int SSL_CTX_set_tlsext_servername_callback(
1683 SSL_CTX *ctx, int (*callback)(SSL *ssl, int *out_alert, void *arg)) {
1684 ctx->tlsext_servername_callback = callback;
1685 return 1;
1686}
1687
1688int SSL_CTX_set_tlsext_servername_arg(SSL_CTX *ctx, void *arg) {
1689 ctx->tlsext_servername_arg = arg;
1690 return 1;
1691}
1692
David Benjamin977547b2015-09-16 00:25:52 -04001693int SSL_select_next_proto(uint8_t **out, uint8_t *out_len,
1694 const uint8_t *server, unsigned server_len,
1695 const uint8_t *client, unsigned client_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001696 unsigned int i, j;
1697 const uint8_t *result;
1698 int status = OPENSSL_NPN_UNSUPPORTED;
Adam Langley95c29f32014-06-20 12:00:00 -07001699
Adam Langleyfcf25832014-12-18 17:42:32 -08001700 /* For each protocol in server preference order, see if we support it. */
1701 for (i = 0; i < server_len;) {
1702 for (j = 0; j < client_len;) {
1703 if (server[i] == client[j] &&
1704 memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
1705 /* We found a match */
1706 result = &server[i];
1707 status = OPENSSL_NPN_NEGOTIATED;
1708 goto found;
1709 }
1710 j += client[j];
1711 j++;
1712 }
1713 i += server[i];
1714 i++;
1715 }
Adam Langley95c29f32014-06-20 12:00:00 -07001716
Adam Langleyfcf25832014-12-18 17:42:32 -08001717 /* There's no overlap between our protocols and the server's list. */
1718 result = client;
1719 status = OPENSSL_NPN_NO_OVERLAP;
Adam Langley95c29f32014-06-20 12:00:00 -07001720
Adam Langleyfcf25832014-12-18 17:42:32 -08001721found:
1722 *out = (uint8_t *)result + 1;
David Benjamin977547b2015-09-16 00:25:52 -04001723 *out_len = result[0];
Adam Langleyfcf25832014-12-18 17:42:32 -08001724 return status;
1725}
Adam Langley95c29f32014-06-20 12:00:00 -07001726
David Benjamin977547b2015-09-16 00:25:52 -04001727void SSL_get0_next_proto_negotiated(const SSL *ssl, const uint8_t **out_data,
1728 unsigned *out_len) {
David Benjamin79978df2015-12-25 15:56:49 -05001729 *out_data = ssl->s3->next_proto_negotiated;
David Benjamin977547b2015-09-16 00:25:52 -04001730 if (*out_data == NULL) {
1731 *out_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001732 } else {
David Benjamin79978df2015-12-25 15:56:49 -05001733 *out_len = ssl->s3->next_proto_negotiated_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08001734 }
Adam Langley95c29f32014-06-20 12:00:00 -07001735}
1736
Adam Langleyfcf25832014-12-18 17:42:32 -08001737void SSL_CTX_set_next_protos_advertised_cb(
1738 SSL_CTX *ctx,
David Benjamin977547b2015-09-16 00:25:52 -04001739 int (*cb)(SSL *ssl, const uint8_t **out, unsigned *out_len, void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08001740 void *arg) {
1741 ctx->next_protos_advertised_cb = cb;
1742 ctx->next_protos_advertised_cb_arg = arg;
1743}
Adam Langley95c29f32014-06-20 12:00:00 -07001744
Adam Langleyfcf25832014-12-18 17:42:32 -08001745void SSL_CTX_set_next_proto_select_cb(
David Benjamin977547b2015-09-16 00:25:52 -04001746 SSL_CTX *ctx, int (*cb)(SSL *ssl, uint8_t **out, uint8_t *out_len,
1747 const uint8_t *in, unsigned in_len, void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08001748 void *arg) {
1749 ctx->next_proto_select_cb = cb;
1750 ctx->next_proto_select_cb_arg = arg;
1751}
Adam Langley95c29f32014-06-20 12:00:00 -07001752
Adam Langleyfcf25832014-12-18 17:42:32 -08001753int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
1754 unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001755 OPENSSL_free(ctx->alpn_client_proto_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001756 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1757 if (!ctx->alpn_client_proto_list) {
1758 return 1;
1759 }
1760 ctx->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001761
Adam Langleyfcf25832014-12-18 17:42:32 -08001762 return 0;
1763}
Adam Langley95c29f32014-06-20 12:00:00 -07001764
Adam Langleyfcf25832014-12-18 17:42:32 -08001765int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001766 OPENSSL_free(ssl->alpn_client_proto_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001767 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1768 if (!ssl->alpn_client_proto_list) {
1769 return 1;
1770 }
1771 ssl->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001772
Adam Langleyfcf25832014-12-18 17:42:32 -08001773 return 0;
1774}
Adam Langley95c29f32014-06-20 12:00:00 -07001775
Adam Langleyfcf25832014-12-18 17:42:32 -08001776void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
1777 int (*cb)(SSL *ssl, const uint8_t **out,
David Benjamin8984f1f2015-09-16 00:10:19 -04001778 uint8_t *out_len, const uint8_t *in,
1779 unsigned in_len, void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08001780 void *arg) {
1781 ctx->alpn_select_cb = cb;
1782 ctx->alpn_select_cb_arg = arg;
1783}
Adam Langley95c29f32014-06-20 12:00:00 -07001784
David Benjamin8984f1f2015-09-16 00:10:19 -04001785void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **out_data,
1786 unsigned *out_len) {
1787 *out_data = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08001788 if (ssl->s3) {
David Benjamin8984f1f2015-09-16 00:10:19 -04001789 *out_data = ssl->s3->alpn_selected;
Adam Langleyfcf25832014-12-18 17:42:32 -08001790 }
David Benjamin8984f1f2015-09-16 00:10:19 -04001791 if (*out_data == NULL) {
1792 *out_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001793 } else {
David Benjamin8984f1f2015-09-16 00:10:19 -04001794 *out_len = ssl->s3->alpn_selected_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08001795 }
1796}
Adam Langley95c29f32014-06-20 12:00:00 -07001797
David Benjamin936aada2016-06-07 19:09:22 -04001798
1799int SSL_CTX_enable_tls_channel_id(SSL_CTX *ctx) {
1800 ctx->tlsext_channel_id_enabled = 1;
1801 return 1;
1802}
1803
1804int SSL_enable_tls_channel_id(SSL *ssl) {
1805 ssl->tlsext_channel_id_enabled = 1;
1806 return 1;
1807}
1808
1809static int is_p256_key(EVP_PKEY *private_key) {
1810 const EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(private_key);
1811 return ec_key != NULL &&
1812 EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key)) ==
1813 NID_X9_62_prime256v1;
1814}
1815
1816int SSL_CTX_set1_tls_channel_id(SSL_CTX *ctx, EVP_PKEY *private_key) {
1817 if (!is_p256_key(private_key)) {
1818 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256);
1819 return 0;
1820 }
1821
1822 EVP_PKEY_free(ctx->tlsext_channel_id_private);
1823 ctx->tlsext_channel_id_private = EVP_PKEY_up_ref(private_key);
1824 ctx->tlsext_channel_id_enabled = 1;
1825
1826 return 1;
1827}
1828
1829int SSL_set1_tls_channel_id(SSL *ssl, EVP_PKEY *private_key) {
1830 if (!is_p256_key(private_key)) {
1831 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256);
1832 return 0;
1833 }
1834
1835 EVP_PKEY_free(ssl->tlsext_channel_id_private);
1836 ssl->tlsext_channel_id_private = EVP_PKEY_up_ref(private_key);
1837 ssl->tlsext_channel_id_enabled = 1;
1838
1839 return 1;
1840}
1841
1842size_t SSL_get_tls_channel_id(SSL *ssl, uint8_t *out, size_t max_out) {
1843 if (!ssl->s3->tlsext_channel_id_valid) {
1844 return 0;
1845 }
1846 memcpy(out, ssl->s3->tlsext_channel_id, (max_out < 64) ? max_out : 64);
1847 return 64;
1848}
1849
Adam Langleyfcf25832014-12-18 17:42:32 -08001850void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
David Benjamin59937042015-09-19 13:04:22 -04001851 int (*cb)(X509_STORE_CTX *store_ctx,
1852 void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08001853 void *arg) {
1854 ctx->app_verify_callback = cb;
1855 ctx->app_verify_arg = arg;
1856}
Adam Langley95c29f32014-06-20 12:00:00 -07001857
Adam Langleyfcf25832014-12-18 17:42:32 -08001858void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
1859 int (*cb)(int, X509_STORE_CTX *)) {
1860 ctx->verify_mode = mode;
1861 ctx->default_verify_callback = cb;
1862}
Adam Langley95c29f32014-06-20 12:00:00 -07001863
Adam Langleyfcf25832014-12-18 17:42:32 -08001864void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
1865 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
1866}
Adam Langley1258b6a2014-06-20 12:00:00 -07001867
David Benjamin7481d392015-07-05 19:38:46 -04001868void SSL_CTX_set_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl, void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08001869 void *arg) {
David Benjamin7481d392015-07-05 19:38:46 -04001870 ssl_cert_set_cert_cb(ctx->cert, cb, arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08001871}
David Benjamin859ec3c2014-09-02 16:29:36 -04001872
David Benjamin7481d392015-07-05 19:38:46 -04001873void SSL_set_cert_cb(SSL *ssl, int (*cb)(SSL *ssl, void *arg), void *arg) {
1874 ssl_cert_set_cert_cb(ssl->cert, cb, arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08001875}
Adam Langley95c29f32014-06-20 12:00:00 -07001876
David Benjamin936aada2016-06-07 19:09:22 -04001877size_t SSL_get0_certificate_types(SSL *ssl, const uint8_t **out_types) {
David Benjamin5744ca62016-06-13 14:05:19 -04001878 if (ssl->server) {
David Benjamin936aada2016-06-07 19:09:22 -04001879 *out_types = NULL;
1880 return 0;
1881 }
1882 *out_types = ssl->s3->tmp.certificate_types;
1883 return ssl->s3->tmp.num_certificate_types;
1884}
1885
David Benjamin0d56f882015-12-19 17:05:56 -05001886void ssl_get_compatible_server_ciphers(SSL *ssl, uint32_t *out_mask_k,
David Benjamin107db582015-04-08 00:41:59 -04001887 uint32_t *out_mask_a) {
David Benjamin83680502016-05-02 13:15:52 -04001888 uint32_t mask_k = 0;
1889 uint32_t mask_a = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001890
David Benjamin0d56f882015-12-19 17:05:56 -05001891 if (ssl->cert->x509 != NULL && ssl_has_private_key(ssl)) {
1892 if (ssl_private_key_type(ssl) == EVP_PKEY_RSA) {
David Benjamin83680502016-05-02 13:15:52 -04001893 mask_k |= SSL_kRSA;
1894 mask_a |= SSL_aRSA;
David Benjamin0d56f882015-12-19 17:05:56 -05001895 } else if (ssl_private_key_type(ssl) == EVP_PKEY_EC) {
David Benjamin83680502016-05-02 13:15:52 -04001896 /* An ECC certificate may be usable for ECDSA cipher suites depending on
Steven Valdezce902a92016-05-17 11:47:53 -04001897 * the key usage extension and on the client's group preferences. */
David Benjamin83680502016-05-02 13:15:52 -04001898 X509 *x = ssl->cert->x509;
1899 /* This call populates extension flags (ex_flags). */
1900 X509_check_purpose(x, -1, 0);
1901 int ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE)
1902 ? (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)
1903 : 1;
1904 if (ecdsa_ok && tls1_check_ec_cert(ssl, x)) {
1905 mask_a |= SSL_aECDSA;
1906 }
David Benjamind1d80782015-07-05 11:54:09 -04001907 }
1908 }
nagendra modadugu601448a2015-07-24 09:31:31 -07001909
David Benjamin83680502016-05-02 13:15:52 -04001910 if (ssl->cert->dh_tmp != NULL || ssl->cert->dh_tmp_cb != NULL) {
David Benjamin7061e282015-03-19 11:10:48 -04001911 mask_k |= SSL_kDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001912 }
Adam Langley95c29f32014-06-20 12:00:00 -07001913
Steven Valdezce902a92016-05-17 11:47:53 -04001914 /* Check for a shared group to consider ECDHE ciphers. */
David Benjamin4298d772015-12-19 00:18:25 -05001915 uint16_t unused;
Steven Valdezce902a92016-05-17 11:47:53 -04001916 if (tls1_get_shared_group(ssl, &unused)) {
David Benjamin7061e282015-03-19 11:10:48 -04001917 mask_k |= SSL_kECDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001918 }
Adam Langley95c29f32014-06-20 12:00:00 -07001919
Matt Braithwaite053931e2016-05-25 12:06:05 -07001920 /* CECPQ1 ciphers are always acceptable if supported by both sides. */
1921 mask_k |= SSL_kCECPQ1;
1922
Adam Langleyfcf25832014-12-18 17:42:32 -08001923 /* PSK requires a server callback. */
David Benjamin0d56f882015-12-19 17:05:56 -05001924 if (ssl->psk_server_callback != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001925 mask_k |= SSL_kPSK;
1926 mask_a |= SSL_aPSK;
1927 }
Adam Langley95c29f32014-06-20 12:00:00 -07001928
Adam Langleyfcf25832014-12-18 17:42:32 -08001929 *out_mask_k = mask_k;
1930 *out_mask_a = mask_a;
1931}
Adam Langley95c29f32014-06-20 12:00:00 -07001932
David Benjamin1269ddd2015-10-18 15:18:55 -04001933void ssl_update_cache(SSL *ssl, int mode) {
1934 SSL_CTX *ctx = ssl->initial_ctx;
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001935 /* Never cache sessions with empty session IDs. */
David Benjamin1269ddd2015-10-18 15:18:55 -04001936 if (ssl->session->session_id_length == 0 ||
1937 (ctx->session_cache_mode & mode) != mode) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001938 return;
1939 }
Adam Langley95c29f32014-06-20 12:00:00 -07001940
David Benjamin1269ddd2015-10-18 15:18:55 -04001941 /* Clients never use the internal session cache. */
1942 int use_internal_cache = ssl->server && !(ctx->session_cache_mode &
1943 SSL_SESS_CACHE_NO_INTERNAL_STORE);
David Benjamin95d31822015-06-15 19:53:32 -04001944
David Benjamin1269ddd2015-10-18 15:18:55 -04001945 /* A client may see new sessions on abbreviated handshakes if the server
1946 * decides to renew the ticket. Once the handshake is completed, it should be
1947 * inserted into the cache. */
1948 if (!ssl->hit || (!ssl->server && ssl->tlsext_ticket_expected)) {
1949 if (use_internal_cache) {
1950 SSL_CTX_add_session(ctx, ssl->session);
1951 }
1952 if (ctx->new_session_cb != NULL &&
1953 !ctx->new_session_cb(ssl, SSL_SESSION_up_ref(ssl->session))) {
1954 /* |new_session_cb|'s return value signals whether it took ownership. */
1955 SSL_SESSION_free(ssl->session);
Adam Langleyfcf25832014-12-18 17:42:32 -08001956 }
1957 }
Adam Langley95c29f32014-06-20 12:00:00 -07001958
David Benjamin1269ddd2015-10-18 15:18:55 -04001959 if (use_internal_cache &&
1960 !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR)) {
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001961 /* Automatically flush the internal session cache every 255 connections. */
1962 int flush_cache = 0;
Adam Langley4bdb6e42015-05-15 15:29:21 -07001963 CRYPTO_MUTEX_lock_write(&ctx->lock);
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001964 ctx->handshakes_since_cache_flush++;
1965 if (ctx->handshakes_since_cache_flush >= 255) {
1966 flush_cache = 1;
1967 ctx->handshakes_since_cache_flush = 0;
1968 }
David Benjamin29270de2016-05-24 15:28:36 +00001969 CRYPTO_MUTEX_unlock_write(&ctx->lock);
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001970
1971 if (flush_cache) {
1972 SSL_CTX_flush_sessions(ctx, (unsigned long)time(NULL));
Adam Langleyfcf25832014-12-18 17:42:32 -08001973 }
1974 }
1975}
Adam Langley95c29f32014-06-20 12:00:00 -07001976
Adam Langleyfcf25832014-12-18 17:42:32 -08001977static const char *ssl_get_version(int version) {
1978 switch (version) {
Steven Valdez4f94b1c2016-05-24 12:31:07 -04001979 case TLS1_3_VERSION:
1980 return "TLSv1.3";
1981
Adam Langleyfcf25832014-12-18 17:42:32 -08001982 case TLS1_2_VERSION:
1983 return "TLSv1.2";
Adam Langley95c29f32014-06-20 12:00:00 -07001984
Adam Langleyfcf25832014-12-18 17:42:32 -08001985 case TLS1_1_VERSION:
1986 return "TLSv1.1";
Adam Langley95c29f32014-06-20 12:00:00 -07001987
Adam Langleyfcf25832014-12-18 17:42:32 -08001988 case TLS1_VERSION:
1989 return "TLSv1";
Adam Langley95c29f32014-06-20 12:00:00 -07001990
Adam Langleyfcf25832014-12-18 17:42:32 -08001991 case SSL3_VERSION:
1992 return "SSLv3";
Adam Langley95c29f32014-06-20 12:00:00 -07001993
David Benjamin1c722b72015-04-20 13:53:10 -04001994 case DTLS1_VERSION:
1995 return "DTLSv1";
1996
1997 case DTLS1_2_VERSION:
1998 return "DTLSv1.2";
1999
Adam Langleyfcf25832014-12-18 17:42:32 -08002000 default:
2001 return "unknown";
2002 }
2003}
Adam Langley95c29f32014-06-20 12:00:00 -07002004
David Benjamin42fea372015-09-19 01:22:44 -04002005const char *SSL_get_version(const SSL *ssl) {
2006 return ssl_get_version(ssl->version);
Adam Langleyfcf25832014-12-18 17:42:32 -08002007}
Adam Langley95c29f32014-06-20 12:00:00 -07002008
David Benjamina6b8cdf2015-09-13 14:07:33 -04002009const char *SSL_SESSION_get_version(const SSL_SESSION *session) {
2010 return ssl_get_version(session->ssl_version);
Adam Langleyfcf25832014-12-18 17:42:32 -08002011}
Adam Langley95c29f32014-06-20 12:00:00 -07002012
David Benjamin0d56f882015-12-19 17:05:56 -05002013X509 *SSL_get_certificate(const SSL *ssl) {
2014 if (ssl->cert != NULL) {
2015 return ssl->cert->x509;
Adam Langleyfcf25832014-12-18 17:42:32 -08002016 }
2017
2018 return NULL;
2019}
2020
David Benjamin0d56f882015-12-19 17:05:56 -05002021EVP_PKEY *SSL_get_privatekey(const SSL *ssl) {
2022 if (ssl->cert != NULL) {
2023 return ssl->cert->privatekey;
Adam Langleyfcf25832014-12-18 17:42:32 -08002024 }
2025
2026 return NULL;
2027}
2028
2029X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
2030 if (ctx->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04002031 return ctx->cert->x509;
Adam Langleyfcf25832014-12-18 17:42:32 -08002032 }
2033
2034 return NULL;
2035}
2036
2037EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
2038 if (ctx->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04002039 return ctx->cert->privatekey;
Adam Langleyfcf25832014-12-18 17:42:32 -08002040 }
2041
2042 return NULL;
2043}
2044
David Benjamin42fea372015-09-19 01:22:44 -04002045const SSL_CIPHER *SSL_get_current_cipher(const SSL *ssl) {
David Benjamin79978df2015-12-25 15:56:49 -05002046 if (ssl->s3->aead_write_ctx == NULL) {
David Benjamina07c0fc2015-05-13 13:19:42 -04002047 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002048 }
David Benjamin79978df2015-12-25 15:56:49 -05002049 return ssl->s3->aead_write_ctx->cipher;
Adam Langleyfcf25832014-12-18 17:42:32 -08002050}
2051
David Benjamin936aada2016-06-07 19:09:22 -04002052int SSL_session_reused(const SSL *ssl) {
2053 return ssl->hit;
2054}
2055
David Benjamin0d56f882015-12-19 17:05:56 -05002056const COMP_METHOD *SSL_get_current_compression(SSL *ssl) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002057
David Benjamin0d56f882015-12-19 17:05:56 -05002058const COMP_METHOD *SSL_get_current_expansion(SSL *ssl) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002059
Adam Langley29ec5d12016-03-01 16:12:28 -08002060int *SSL_get_server_tmp_key(SSL *ssl, EVP_PKEY **out_key) { return 0; }
2061
David Benjamin30152fd2016-05-05 20:45:48 -04002062int ssl_is_wbio_buffered(const SSL *ssl) {
David Benjamin2f871122016-05-20 14:27:17 -04002063 return ssl->bbio != NULL;
David Benjamin30152fd2016-05-05 20:45:48 -04002064}
2065
David Benjaminb095f0f2016-05-05 21:50:24 -04002066int ssl_init_wbio_buffer(SSL *ssl) {
David Benjamin2f871122016-05-20 14:27:17 -04002067 if (ssl->bbio != NULL) {
2068 /* Already buffered. */
2069 assert(ssl->bbio == ssl->wbio);
2070 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002071 }
2072
David Benjamin2f871122016-05-20 14:27:17 -04002073 BIO *bbio = BIO_new(BIO_f_buffer());
2074 if (bbio == NULL ||
2075 !BIO_set_read_buffer_size(bbio, 1)) {
2076 BIO_free(bbio);
Adam Langleyfcf25832014-12-18 17:42:32 -08002077 return 0;
2078 }
2079
David Benjamin2f871122016-05-20 14:27:17 -04002080 ssl->bbio = bbio;
2081 ssl->wbio = BIO_push(bbio, ssl->wbio);
Adam Langleyfcf25832014-12-18 17:42:32 -08002082 return 1;
2083}
2084
David Benjamin0d56f882015-12-19 17:05:56 -05002085void ssl_free_wbio_buffer(SSL *ssl) {
2086 if (ssl->bbio == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002087 return;
2088 }
2089
David Benjamin2f871122016-05-20 14:27:17 -04002090 assert(ssl->bbio == ssl->wbio);
Adam Langleyfcf25832014-12-18 17:42:32 -08002091
David Benjamin2f871122016-05-20 14:27:17 -04002092 ssl->wbio = BIO_pop(ssl->wbio);
David Benjamin0d56f882015-12-19 17:05:56 -05002093 BIO_free(ssl->bbio);
2094 ssl->bbio = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002095}
2096
2097void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
Adam Langleybb85f3d2015-10-28 18:44:11 -07002098 ctx->quiet_shutdown = (mode != 0);
Adam Langleyfcf25832014-12-18 17:42:32 -08002099}
2100
2101int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
2102 return ctx->quiet_shutdown;
2103}
2104
Adam Langleybb85f3d2015-10-28 18:44:11 -07002105void SSL_set_quiet_shutdown(SSL *ssl, int mode) {
2106 ssl->quiet_shutdown = (mode != 0);
2107}
Adam Langleyfcf25832014-12-18 17:42:32 -08002108
David Benjamin9f859492015-10-03 10:44:30 -04002109int SSL_get_quiet_shutdown(const SSL *ssl) { return ssl->quiet_shutdown; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002110
David Benjamin63006a92015-10-18 00:00:06 -04002111void SSL_set_shutdown(SSL *ssl, int mode) {
2112 /* It is an error to clear any bits that have already been set. (We can't try
2113 * to get a second close_notify or send two.) */
David Benjaminfa214e42016-05-10 17:03:10 -04002114 assert((SSL_get_shutdown(ssl) & mode) == SSL_get_shutdown(ssl));
David Benjamin63006a92015-10-18 00:00:06 -04002115
David Benjaminfa214e42016-05-10 17:03:10 -04002116 if (mode & SSL_RECEIVED_SHUTDOWN &&
2117 ssl->s3->recv_shutdown == ssl_shutdown_none) {
2118 ssl->s3->recv_shutdown = ssl_shutdown_close_notify;
2119 }
2120
2121 if (mode & SSL_SENT_SHUTDOWN &&
2122 ssl->s3->send_shutdown == ssl_shutdown_none) {
2123 ssl->s3->send_shutdown = ssl_shutdown_close_notify;
2124 }
David Benjamin63006a92015-10-18 00:00:06 -04002125}
Adam Langleyfcf25832014-12-18 17:42:32 -08002126
David Benjaminfa214e42016-05-10 17:03:10 -04002127int SSL_get_shutdown(const SSL *ssl) {
2128 int ret = 0;
2129 if (ssl->s3->recv_shutdown != ssl_shutdown_none) {
2130 /* Historically, OpenSSL set |SSL_RECEIVED_SHUTDOWN| on both close_notify
2131 * and fatal alert. */
2132 ret |= SSL_RECEIVED_SHUTDOWN;
2133 }
2134 if (ssl->s3->send_shutdown == ssl_shutdown_close_notify) {
2135 /* Historically, OpenSSL set |SSL_SENT_SHUTDOWN| on only close_notify. */
2136 ret |= SSL_SENT_SHUTDOWN;
2137 }
2138 return ret;
2139}
Adam Langleyfcf25832014-12-18 17:42:32 -08002140
David Benjamin9f859492015-10-03 10:44:30 -04002141int SSL_version(const SSL *ssl) { return ssl->version; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002142
2143SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
2144
2145SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
2146 if (ssl->ctx == ctx) {
2147 return ssl->ctx;
2148 }
2149
2150 if (ctx == NULL) {
2151 ctx = ssl->initial_ctx;
2152 }
2153
David Benjamin2755a3e2015-04-22 16:17:58 -04002154 ssl_cert_free(ssl->cert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002155 ssl->cert = ssl_cert_dup(ctx->cert);
David Benjamin2755a3e2015-04-22 16:17:58 -04002156
Adam Langley0b5e3902015-05-15 13:08:38 -07002157 CRYPTO_refcount_inc(&ctx->references);
David Benjamin2755a3e2015-04-22 16:17:58 -04002158 SSL_CTX_free(ssl->ctx); /* decrement reference count */
Adam Langleyfcf25832014-12-18 17:42:32 -08002159 ssl->ctx = ctx;
2160
2161 ssl->sid_ctx_length = ctx->sid_ctx_length;
2162 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2163 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2164
2165 return ssl->ctx;
2166}
2167
2168int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
2169 return X509_STORE_set_default_paths(ctx->cert_store);
2170}
Adam Langley95c29f32014-06-20 12:00:00 -07002171
David Benjamin59937042015-09-19 13:04:22 -04002172int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *ca_file,
2173 const char *ca_dir) {
2174 return X509_STORE_load_locations(ctx->cert_store, ca_file, ca_dir);
Adam Langleyfcf25832014-12-18 17:42:32 -08002175}
Adam Langley95c29f32014-06-20 12:00:00 -07002176
2177void SSL_set_info_callback(SSL *ssl,
David Benjamin82170242015-10-17 22:51:17 -04002178 void (*cb)(const SSL *ssl, int type, int value)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002179 ssl->info_callback = cb;
2180}
Adam Langley95c29f32014-06-20 12:00:00 -07002181
David Benjamin82170242015-10-17 22:51:17 -04002182void (*SSL_get_info_callback(const SSL *ssl))(const SSL *ssl, int type,
2183 int value) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002184 return ssl->info_callback;
2185}
Adam Langley95c29f32014-06-20 12:00:00 -07002186
Adam Langleyfcf25832014-12-18 17:42:32 -08002187int SSL_state(const SSL *ssl) { return ssl->state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002188
David Benjaminece089c2015-05-15 23:52:42 -04002189void SSL_set_state(SSL *ssl, int state) { }
Adam Langley95c29f32014-06-20 12:00:00 -07002190
Emily Stark95a79ee2016-02-25 21:12:28 -08002191char *SSL_get_shared_ciphers(const SSL *ssl, char *buf, int len) {
2192 if (len <= 0) {
2193 return NULL;
2194 }
2195 buf[0] = '\0';
2196 return buf;
2197}
2198
David Benjamin59937042015-09-19 13:04:22 -04002199void SSL_set_verify_result(SSL *ssl, long result) {
2200 ssl->verify_result = result;
2201}
Adam Langley95c29f32014-06-20 12:00:00 -07002202
Adam Langleyfcf25832014-12-18 17:42:32 -08002203long SSL_get_verify_result(const SSL *ssl) { return ssl->verify_result; }
Adam Langley95c29f32014-06-20 12:00:00 -07002204
David Benjamin8a589332015-12-04 23:14:35 -05002205int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
Adam Langleyfcf25832014-12-18 17:42:32 -08002206 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002207 int index;
2208 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp,
David Benjamin8a589332015-12-04 23:14:35 -05002209 dup_func, free_func)) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002210 return -1;
2211 }
2212 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002213}
Adam Langley95c29f32014-06-20 12:00:00 -07002214
David Benjamin1ac08fe2015-09-15 01:36:22 -04002215int SSL_set_ex_data(SSL *ssl, int idx, void *arg) {
2216 return CRYPTO_set_ex_data(&ssl->ex_data, idx, arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002217}
Adam Langley95c29f32014-06-20 12:00:00 -07002218
David Benjamin1ac08fe2015-09-15 01:36:22 -04002219void *SSL_get_ex_data(const SSL *ssl, int idx) {
2220 return CRYPTO_get_ex_data(&ssl->ex_data, idx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002221}
Adam Langley95c29f32014-06-20 12:00:00 -07002222
David Benjamin8a589332015-12-04 23:14:35 -05002223int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
Adam Langleyfcf25832014-12-18 17:42:32 -08002224 CRYPTO_EX_dup *dup_func,
2225 CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002226 int index;
2227 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp,
David Benjamin8a589332015-12-04 23:14:35 -05002228 dup_func, free_func)) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002229 return -1;
2230 }
2231 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002232}
Adam Langley95c29f32014-06-20 12:00:00 -07002233
David Benjamin1ac08fe2015-09-15 01:36:22 -04002234int SSL_CTX_set_ex_data(SSL_CTX *ctx, int idx, void *arg) {
2235 return CRYPTO_set_ex_data(&ctx->ex_data, idx, arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002236}
Adam Langley95c29f32014-06-20 12:00:00 -07002237
David Benjamin1ac08fe2015-09-15 01:36:22 -04002238void *SSL_CTX_get_ex_data(const SSL_CTX *ctx, int idx) {
2239 return CRYPTO_get_ex_data(&ctx->ex_data, idx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002240}
Adam Langley95c29f32014-06-20 12:00:00 -07002241
Adam Langleyfcf25832014-12-18 17:42:32 -08002242X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
2243 return ctx->cert_store;
2244}
Adam Langley95c29f32014-06-20 12:00:00 -07002245
Adam Langleyfcf25832014-12-18 17:42:32 -08002246void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
David Benjamin2755a3e2015-04-22 16:17:58 -04002247 X509_STORE_free(ctx->cert_store);
Adam Langleyfcf25832014-12-18 17:42:32 -08002248 ctx->cert_store = store;
2249}
Adam Langley95c29f32014-06-20 12:00:00 -07002250
David Benjamin93d17492015-10-17 12:43:36 -04002251int SSL_want(const SSL *ssl) { return ssl->rwstate; }
Adam Langley95c29f32014-06-20 12:00:00 -07002252
Adam Langleyfcf25832014-12-18 17:42:32 -08002253void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
2254 RSA *(*cb)(SSL *ssl, int is_export,
2255 int keylength)) {
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_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
2259 int keylength)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002260}
2261
2262void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
David Benjamin59015c32015-04-26 13:13:55 -04002263 DH *(*callback)(SSL *ssl, int is_export,
2264 int keylength)) {
2265 ctx->cert->dh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002266}
2267
David Benjamin59015c32015-04-26 13:13:55 -04002268void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*callback)(SSL *ssl, int is_export,
2269 int keylength)) {
2270 ssl->cert->dh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002271}
2272
Adam Langleyfcf25832014-12-18 17:42:32 -08002273int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
2274 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
David Benjamin3570d732015-06-29 00:28:17 -04002275 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -08002276 return 0;
2277 }
2278
David Benjamin2755a3e2015-04-22 16:17:58 -04002279 OPENSSL_free(ctx->psk_identity_hint);
Adam Langleyfcf25832014-12-18 17:42:32 -08002280
2281 if (identity_hint != NULL) {
2282 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2283 if (ctx->psk_identity_hint == NULL) {
2284 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002285 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002286 } else {
2287 ctx->psk_identity_hint = NULL;
2288 }
Adam Langley95c29f32014-06-20 12:00:00 -07002289
Adam Langleyfcf25832014-12-18 17:42:32 -08002290 return 1;
2291}
2292
David Benjamine8814df2015-09-15 08:05:54 -04002293int SSL_use_psk_identity_hint(SSL *ssl, const char *identity_hint) {
2294 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002295 return 0;
2296 }
2297
2298 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
David Benjamin3570d732015-06-29 00:28:17 -04002299 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -08002300 return 0;
2301 }
2302
2303 /* Clear currently configured hint, if any. */
David Benjamine8814df2015-09-15 08:05:54 -04002304 OPENSSL_free(ssl->psk_identity_hint);
2305 ssl->psk_identity_hint = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002306
2307 if (identity_hint != NULL) {
David Benjamine8814df2015-09-15 08:05:54 -04002308 ssl->psk_identity_hint = BUF_strdup(identity_hint);
2309 if (ssl->psk_identity_hint == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002310 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002311 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002312 }
Adam Langley95c29f32014-06-20 12:00:00 -07002313
Adam Langleyfcf25832014-12-18 17:42:32 -08002314 return 1;
2315}
Adam Langley95c29f32014-06-20 12:00:00 -07002316
David Benjamine8814df2015-09-15 08:05:54 -04002317const char *SSL_get_psk_identity_hint(const SSL *ssl) {
2318 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002319 return NULL;
2320 }
David Benjamine8814df2015-09-15 08:05:54 -04002321 return ssl->psk_identity_hint;
Adam Langleyfcf25832014-12-18 17:42:32 -08002322}
Adam Langley95c29f32014-06-20 12:00:00 -07002323
David Benjamine8814df2015-09-15 08:05:54 -04002324const char *SSL_get_psk_identity(const SSL *ssl) {
2325 if (ssl == NULL || ssl->session == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002326 return NULL;
2327 }
Adam Langley95c29f32014-06-20 12:00:00 -07002328
David Benjamine8814df2015-09-15 08:05:54 -04002329 return ssl->session->psk_identity;
Adam Langleyfcf25832014-12-18 17:42:32 -08002330}
Adam Langley95c29f32014-06-20 12:00:00 -07002331
Adam Langleyfcf25832014-12-18 17:42:32 -08002332void SSL_set_psk_client_callback(
David Benjamine8814df2015-09-15 08:05:54 -04002333 SSL *ssl, unsigned (*cb)(SSL *ssl, const char *hint, char *identity,
2334 unsigned max_identity_len, uint8_t *psk,
2335 unsigned max_psk_len)) {
2336 ssl->psk_client_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002337}
Adam Langley95c29f32014-06-20 12:00:00 -07002338
Adam Langleyfcf25832014-12-18 17:42:32 -08002339void SSL_CTX_set_psk_client_callback(
David Benjamine8814df2015-09-15 08:05:54 -04002340 SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *hint, char *identity,
2341 unsigned max_identity_len, uint8_t *psk,
2342 unsigned max_psk_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002343 ctx->psk_client_callback = cb;
2344}
Adam Langley95c29f32014-06-20 12:00:00 -07002345
Adam Langleyfcf25832014-12-18 17:42:32 -08002346void SSL_set_psk_server_callback(
David Benjamine8814df2015-09-15 08:05:54 -04002347 SSL *ssl, unsigned (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
2348 unsigned max_psk_len)) {
2349 ssl->psk_server_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002350}
Adam Langley95c29f32014-06-20 12:00:00 -07002351
Adam Langleyfcf25832014-12-18 17:42:32 -08002352void SSL_CTX_set_psk_server_callback(
David Benjamine8814df2015-09-15 08:05:54 -04002353 SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *identity,
2354 uint8_t *psk, unsigned max_psk_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002355 ctx->psk_server_callback = cb;
2356}
Adam Langley95c29f32014-06-20 12:00:00 -07002357
Adam Langleyfcf25832014-12-18 17:42:32 -08002358void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
2359 void (*cb)(int write_p, int version,
2360 int content_type, const void *buf,
2361 size_t len, SSL *ssl, void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002362 ctx->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002363}
David Benjamin61ecccf2015-05-05 09:44:51 -04002364
2365void SSL_CTX_set_msg_callback_arg(SSL_CTX *ctx, void *arg) {
2366 ctx->msg_callback_arg = arg;
2367}
2368
Adam Langleyfcf25832014-12-18 17:42:32 -08002369void SSL_set_msg_callback(SSL *ssl,
2370 void (*cb)(int write_p, int version, int content_type,
2371 const void *buf, size_t len, SSL *ssl,
2372 void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002373 ssl->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002374}
Adam Langley95c29f32014-06-20 12:00:00 -07002375
David Benjamin61ecccf2015-05-05 09:44:51 -04002376void SSL_set_msg_callback_arg(SSL *ssl, void *arg) {
2377 ssl->msg_callback_arg = arg;
2378}
2379
David Benjamind28f59c2015-11-17 22:32:50 -05002380void SSL_CTX_set_keylog_callback(SSL_CTX *ctx,
2381 void (*cb)(const SSL *ssl, const char *line)) {
2382 ctx->keylog_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002383}
Adam Langley95c29f32014-06-20 12:00:00 -07002384
Taylor Brandstetter9edb2c62016-06-08 15:26:59 -07002385void SSL_CTX_set_current_time_cb(SSL_CTX *ctx,
2386 void (*cb)(const SSL *ssl,
2387 struct timeval *out_clock)) {
2388 ctx->current_time_cb = cb;
2389}
2390
Adam Langleyfcf25832014-12-18 17:42:32 -08002391static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
2392 static const char hextable[] = "0123456789abcdef";
2393 uint8_t *out;
2394 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002395
Adam Langleyfcf25832014-12-18 17:42:32 -08002396 if (!CBB_add_space(cbb, &out, in_len * 2)) {
2397 return 0;
2398 }
Adam Langley95c29f32014-06-20 12:00:00 -07002399
Adam Langleyfcf25832014-12-18 17:42:32 -08002400 for (i = 0; i < in_len; i++) {
2401 *(out++) = (uint8_t)hextable[in[i] >> 4];
2402 *(out++) = (uint8_t)hextable[in[i] & 0xf];
2403 }
Adam Langley95c29f32014-06-20 12:00:00 -07002404
Adam Langleyfcf25832014-12-18 17:42:32 -08002405 return 1;
2406}
David Benjamin859ec3c2014-09-02 16:29:36 -04002407
David Benjamind28f59c2015-11-17 22:32:50 -05002408int ssl_log_rsa_client_key_exchange(const SSL *ssl,
2409 const uint8_t *encrypted_premaster,
2410 size_t encrypted_premaster_len,
2411 const uint8_t *premaster,
2412 size_t premaster_len) {
2413 if (ssl->ctx->keylog_callback == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002414 return 1;
2415 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002416
Adam Langleyfcf25832014-12-18 17:42:32 -08002417 if (encrypted_premaster_len < 8) {
David Benjamin3570d732015-06-29 00:28:17 -04002418 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08002419 return 0;
2420 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002421
David Benjamind28f59c2015-11-17 22:32:50 -05002422 CBB cbb;
2423 uint8_t *out;
2424 size_t out_len;
David Benjamina8653202015-06-28 01:26:10 -04002425 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len * 2 + 1) ||
2426 !CBB_add_bytes(&cbb, (const uint8_t *)"RSA ", 4) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08002427 /* Only the first 8 bytes of the encrypted premaster secret are
2428 * logged. */
2429 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
2430 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2431 !cbb_add_hex(&cbb, premaster, premaster_len) ||
David Benjamind28f59c2015-11-17 22:32:50 -05002432 !CBB_add_u8(&cbb, 0 /* NUL */) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08002433 !CBB_finish(&cbb, &out, &out_len)) {
2434 CBB_cleanup(&cbb);
2435 return 0;
2436 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002437
David Benjamind28f59c2015-11-17 22:32:50 -05002438 ssl->ctx->keylog_callback(ssl, (const char *)out);
Adam Langleyfcf25832014-12-18 17:42:32 -08002439 OPENSSL_free(out);
David Benjamind28f59c2015-11-17 22:32:50 -05002440 return 1;
Adam Langley95f22882014-06-20 12:00:00 -07002441}
2442
David Benjamind28f59c2015-11-17 22:32:50 -05002443int ssl_log_master_secret(const SSL *ssl, const uint8_t *client_random,
2444 size_t client_random_len, const uint8_t *master,
2445 size_t master_len) {
2446 if (ssl->ctx->keylog_callback == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002447 return 1;
2448 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002449
Adam Langleyfcf25832014-12-18 17:42:32 -08002450 if (client_random_len != 32) {
David Benjamin3570d732015-06-29 00:28:17 -04002451 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08002452 return 0;
2453 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002454
David Benjamind28f59c2015-11-17 22:32:50 -05002455 CBB cbb;
2456 uint8_t *out;
2457 size_t out_len;
David Benjamina8653202015-06-28 01:26:10 -04002458 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len * 2 + 1) ||
2459 !CBB_add_bytes(&cbb, (const uint8_t *)"CLIENT_RANDOM ", 14) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08002460 !cbb_add_hex(&cbb, client_random, 32) ||
2461 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2462 !cbb_add_hex(&cbb, master, master_len) ||
David Benjamind28f59c2015-11-17 22:32:50 -05002463 !CBB_add_u8(&cbb, 0 /* NUL */) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08002464 !CBB_finish(&cbb, &out, &out_len)) {
2465 CBB_cleanup(&cbb);
2466 return 0;
2467 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002468
David Benjamind28f59c2015-11-17 22:32:50 -05002469 ssl->ctx->keylog_callback(ssl, (const char *)out);
Adam Langleyfcf25832014-12-18 17:42:32 -08002470 OPENSSL_free(out);
David Benjamind28f59c2015-11-17 22:32:50 -05002471 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002472}
2473
David Benjamin5d8b1282015-10-17 23:26:35 -04002474int SSL_is_init_finished(const SSL *ssl) {
2475 return ssl->state == SSL_ST_OK;
2476}
2477
2478int SSL_in_init(const SSL *ssl) {
2479 return (ssl->state & SSL_ST_INIT) != 0;
2480}
2481
2482int SSL_in_false_start(const SSL *ssl) {
2483 return ssl->s3->tmp.in_false_start;
David Benjamined7c4752015-02-16 19:16:46 -05002484}
2485
David Benjamin0d56f882015-12-19 17:05:56 -05002486int SSL_cutthrough_complete(const SSL *ssl) {
2487 return SSL_in_false_start(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08002488}
Adam Langley95c29f32014-06-20 12:00:00 -07002489
Adam Langleyfcf25832014-12-18 17:42:32 -08002490void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
2491 size_t *ssl_session_size) {
2492 *ssl_size = sizeof(SSL);
2493 *ssl_ctx_size = sizeof(SSL_CTX);
2494 *ssl_session_size = sizeof(SSL_SESSION);
2495}
Feng Lu41aa3252014-11-21 22:47:56 -08002496
David Benjamin0d56f882015-12-19 17:05:56 -05002497int ssl3_can_false_start(const SSL *ssl) {
2498 const SSL_CIPHER *const cipher = SSL_get_current_cipher(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08002499
Adam Langleye631d962015-04-01 13:11:01 -07002500 /* False Start only for TLS 1.2 with an ECDHE+AEAD cipher and ALPN or NPN. */
David Benjamin0d56f882015-12-19 17:05:56 -05002501 return !SSL_IS_DTLS(ssl) &&
Steven Valdez4f94b1c2016-05-24 12:31:07 -04002502 SSL_version(ssl) == TLS1_2_VERSION &&
David Benjamin0d56f882015-12-19 17:05:56 -05002503 (ssl->s3->alpn_selected || ssl->s3->next_proto_neg_seen) &&
David Benjamin195dc782015-02-19 13:27:05 -05002504 cipher != NULL &&
Adam Langleye631d962015-04-01 13:11:01 -07002505 cipher->algorithm_mkey == SSL_kECDHE &&
David Benjamin13414b32015-12-09 23:02:39 -05002506 cipher->algorithm_mac == SSL_AEAD;
Adam Langleyfcf25832014-12-18 17:42:32 -08002507}
2508
2509const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version) {
2510 switch (version) {
2511 case SSL3_VERSION:
2512 return &SSLv3_enc_data;
2513
2514 case TLS1_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002515 case TLS1_1_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002516 case TLS1_2_VERSION:
Steven Valdez4f94b1c2016-05-24 12:31:07 -04002517 case TLS1_3_VERSION:
David Benjamina1e9cab2015-12-30 00:08:49 -05002518 case DTLS1_VERSION:
2519 case DTLS1_2_VERSION:
2520 return &TLSv1_enc_data;
Adam Langleyfcf25832014-12-18 17:42:32 -08002521
Adam Langleyfcf25832014-12-18 17:42:32 -08002522 default:
2523 return NULL;
2524 }
2525}
2526
David Benjamin0d56f882015-12-19 17:05:56 -05002527uint16_t ssl3_get_max_server_version(const SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002528 uint16_t max_version;
2529
David Benjamin0d56f882015-12-19 17:05:56 -05002530 if (SSL_IS_DTLS(ssl)) {
2531 max_version = (ssl->max_version != 0) ? ssl->max_version : DTLS1_2_VERSION;
2532 if (!(ssl->options & SSL_OP_NO_DTLSv1_2) &&
2533 DTLS1_2_VERSION >= max_version) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002534 return DTLS1_2_VERSION;
2535 }
David Benjamin0d56f882015-12-19 17:05:56 -05002536 if (!(ssl->options & SSL_OP_NO_DTLSv1) && DTLS1_VERSION >= max_version) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002537 return DTLS1_VERSION;
2538 }
2539 return 0;
2540 }
2541
Steven Valdez4f94b1c2016-05-24 12:31:07 -04002542 max_version = (ssl->max_version != 0) ? ssl->max_version : TLS1_3_VERSION;
2543 if (!(ssl->options & SSL_OP_NO_TLSv1_3) && TLS1_3_VERSION <= max_version) {
2544 return TLS1_3_VERSION;
2545 }
David Benjamin0d56f882015-12-19 17:05:56 -05002546 if (!(ssl->options & SSL_OP_NO_TLSv1_2) && TLS1_2_VERSION <= max_version) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002547 return TLS1_2_VERSION;
2548 }
David Benjamin0d56f882015-12-19 17:05:56 -05002549 if (!(ssl->options & SSL_OP_NO_TLSv1_1) && TLS1_1_VERSION <= max_version) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002550 return TLS1_1_VERSION;
2551 }
David Benjamin0d56f882015-12-19 17:05:56 -05002552 if (!(ssl->options & SSL_OP_NO_TLSv1) && TLS1_VERSION <= max_version) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002553 return TLS1_VERSION;
2554 }
David Benjamin0d56f882015-12-19 17:05:56 -05002555 if (!(ssl->options & SSL_OP_NO_SSLv3) && SSL3_VERSION <= max_version) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002556 return SSL3_VERSION;
2557 }
2558 return 0;
2559}
2560
David Benjamin0d56f882015-12-19 17:05:56 -05002561uint16_t ssl3_get_mutual_version(SSL *ssl, uint16_t client_version) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002562 uint16_t version = 0;
2563
David Benjamin0d56f882015-12-19 17:05:56 -05002564 if (SSL_IS_DTLS(ssl)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002565 /* Clamp client_version to max_version. */
David Benjamin10e664b2016-06-20 22:20:47 -04002566 if (client_version < ssl->max_version) {
David Benjamin0d56f882015-12-19 17:05:56 -05002567 client_version = ssl->max_version;
Adam Langleyfcf25832014-12-18 17:42:32 -08002568 }
2569
David Benjamin0d56f882015-12-19 17:05:56 -05002570 if (client_version <= DTLS1_2_VERSION &&
2571 !(ssl->options & SSL_OP_NO_DTLSv1_2)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002572 version = DTLS1_2_VERSION;
2573 } else if (client_version <= DTLS1_VERSION &&
David Benjamin0d56f882015-12-19 17:05:56 -05002574 !(ssl->options & SSL_OP_NO_DTLSv1)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002575 version = DTLS1_VERSION;
2576 }
2577
2578 /* Check against min_version. */
David Benjamin10e664b2016-06-20 22:20:47 -04002579 if (version != 0 && version > ssl->min_version) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002580 return 0;
2581 }
2582 return version;
2583 } else {
2584 /* Clamp client_version to max_version. */
David Benjamin10e664b2016-06-20 22:20:47 -04002585 if (client_version > ssl->max_version) {
David Benjamin0d56f882015-12-19 17:05:56 -05002586 client_version = ssl->max_version;
Adam Langleyfcf25832014-12-18 17:42:32 -08002587 }
2588
Steven Valdez4f94b1c2016-05-24 12:31:07 -04002589 if (client_version >= TLS1_3_VERSION &&
2590 !(ssl->options & SSL_OP_NO_TLSv1_3)) {
2591 version = TLS1_3_VERSION;
2592 } else if (client_version >= TLS1_2_VERSION &&
2593 !(ssl->options & SSL_OP_NO_TLSv1_2)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002594 version = TLS1_2_VERSION;
2595 } else if (client_version >= TLS1_1_VERSION &&
David Benjamin0d56f882015-12-19 17:05:56 -05002596 !(ssl->options & SSL_OP_NO_TLSv1_1)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002597 version = TLS1_1_VERSION;
David Benjamin0d56f882015-12-19 17:05:56 -05002598 } else if (client_version >= TLS1_VERSION &&
2599 !(ssl->options & SSL_OP_NO_TLSv1)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002600 version = TLS1_VERSION;
David Benjamin0d56f882015-12-19 17:05:56 -05002601 } else if (client_version >= SSL3_VERSION &&
2602 !(ssl->options & SSL_OP_NO_SSLv3)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002603 version = SSL3_VERSION;
2604 }
2605
2606 /* Check against min_version. */
David Benjamin10e664b2016-06-20 22:20:47 -04002607 if (version != 0 && version < ssl->min_version) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002608 return 0;
2609 }
2610 return version;
2611 }
2612}
2613
David Benjamin0d56f882015-12-19 17:05:56 -05002614uint16_t ssl3_get_max_client_version(SSL *ssl) {
2615 uint32_t options = ssl->options;
Adam Langleyfcf25832014-12-18 17:42:32 -08002616 uint16_t version = 0;
2617
2618 /* OpenSSL's API for controlling versions entails blacklisting individual
2619 * protocols. This has two problems. First, on the client, the protocol can
2620 * only express a contiguous range of versions. Second, a library consumer
2621 * trying to set a maximum version cannot disable protocol versions that get
2622 * added in a future version of the library.
2623 *
2624 * To account for both of these, OpenSSL interprets the client-side bitmask
2625 * as a min/max range by picking the lowest contiguous non-empty range of
2626 * enabled protocols. Note that this means it is impossible to set a maximum
2627 * version of TLS 1.2 in a future-proof way.
2628 *
2629 * By this scheme, the maximum version is the lowest version V such that V is
2630 * enabled and V+1 is disabled or unimplemented. */
David Benjamin0d56f882015-12-19 17:05:56 -05002631 if (SSL_IS_DTLS(ssl)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002632 if (!(options & SSL_OP_NO_DTLSv1_2)) {
2633 version = DTLS1_2_VERSION;
2634 }
2635 if (!(options & SSL_OP_NO_DTLSv1) && (options & SSL_OP_NO_DTLSv1_2)) {
2636 version = DTLS1_VERSION;
2637 }
David Benjamin10e664b2016-06-20 22:20:47 -04002638 if (version != 0 && version < ssl->max_version) {
David Benjamin0d56f882015-12-19 17:05:56 -05002639 version = ssl->max_version;
Adam Langleyfcf25832014-12-18 17:42:32 -08002640 }
2641 } else {
Steven Valdez4f94b1c2016-05-24 12:31:07 -04002642 if (!(options & SSL_OP_NO_TLSv1_3)) {
2643 version = TLS1_3_VERSION;
2644 }
2645 if (!(options & SSL_OP_NO_TLSv1_2) && (options & SSL_OP_NO_TLSv1_3)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002646 version = TLS1_2_VERSION;
2647 }
2648 if (!(options & SSL_OP_NO_TLSv1_1) && (options & SSL_OP_NO_TLSv1_2)) {
2649 version = TLS1_1_VERSION;
2650 }
2651 if (!(options & SSL_OP_NO_TLSv1) && (options & SSL_OP_NO_TLSv1_1)) {
2652 version = TLS1_VERSION;
2653 }
2654 if (!(options & SSL_OP_NO_SSLv3) && (options & SSL_OP_NO_TLSv1)) {
2655 version = SSL3_VERSION;
2656 }
David Benjamin10e664b2016-06-20 22:20:47 -04002657 if (version != 0 && version > ssl->max_version) {
David Benjamin0d56f882015-12-19 17:05:56 -05002658 version = ssl->max_version;
Adam Langleyfcf25832014-12-18 17:42:32 -08002659 }
2660 }
2661
2662 return version;
2663}
2664
David Benjamin0d56f882015-12-19 17:05:56 -05002665int ssl3_is_version_enabled(SSL *ssl, uint16_t version) {
2666 if (SSL_IS_DTLS(ssl)) {
David Benjamin10e664b2016-06-20 22:20:47 -04002667 if (version < ssl->max_version) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002668 return 0;
2669 }
David Benjamin10e664b2016-06-20 22:20:47 -04002670 if (version > ssl->min_version) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002671 return 0;
2672 }
2673
2674 switch (version) {
2675 case DTLS1_VERSION:
David Benjamin0d56f882015-12-19 17:05:56 -05002676 return !(ssl->options & SSL_OP_NO_DTLSv1);
Adam Langleyfcf25832014-12-18 17:42:32 -08002677
2678 case DTLS1_2_VERSION:
David Benjamin0d56f882015-12-19 17:05:56 -05002679 return !(ssl->options & SSL_OP_NO_DTLSv1_2);
Adam Langleyfcf25832014-12-18 17:42:32 -08002680
2681 default:
2682 return 0;
2683 }
2684 } else {
David Benjamin10e664b2016-06-20 22:20:47 -04002685 if (version > ssl->max_version) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002686 return 0;
2687 }
David Benjamin10e664b2016-06-20 22:20:47 -04002688 if (version < ssl->min_version) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002689 return 0;
2690 }
2691
2692 switch (version) {
2693 case SSL3_VERSION:
David Benjamin0d56f882015-12-19 17:05:56 -05002694 return !(ssl->options & SSL_OP_NO_SSLv3);
Adam Langleyfcf25832014-12-18 17:42:32 -08002695
2696 case TLS1_VERSION:
David Benjamin0d56f882015-12-19 17:05:56 -05002697 return !(ssl->options & SSL_OP_NO_TLSv1);
Adam Langleyfcf25832014-12-18 17:42:32 -08002698
2699 case TLS1_1_VERSION:
David Benjamin0d56f882015-12-19 17:05:56 -05002700 return !(ssl->options & SSL_OP_NO_TLSv1_1);
Adam Langleyfcf25832014-12-18 17:42:32 -08002701
2702 case TLS1_2_VERSION:
David Benjamin0d56f882015-12-19 17:05:56 -05002703 return !(ssl->options & SSL_OP_NO_TLSv1_2);
Adam Langleyfcf25832014-12-18 17:42:32 -08002704
Steven Valdez4f94b1c2016-05-24 12:31:07 -04002705 case TLS1_3_VERSION:
2706 return !(ssl->options & SSL_OP_NO_TLSv1_3);
2707
Adam Langleyfcf25832014-12-18 17:42:32 -08002708 default:
2709 return 0;
2710 }
2711 }
2712}
2713
David Benjaminb9e4fa52015-12-29 23:58:17 -05002714uint16_t ssl3_version_from_wire(const SSL *ssl, uint16_t wire_version) {
David Benjamin0d56f882015-12-19 17:05:56 -05002715 if (!SSL_IS_DTLS(ssl)) {
David Benjaminea72bd02014-12-21 21:27:41 -05002716 return wire_version;
2717 }
2718
2719 uint16_t tls_version = ~wire_version;
2720 uint16_t version = tls_version + 0x0201;
2721 /* If either component overflowed, clamp it so comparisons still work. */
2722 if ((version >> 8) < (tls_version >> 8)) {
2723 version = 0xff00 | (version & 0xff);
2724 }
2725 if ((version & 0xff) < (tls_version & 0xff)) {
2726 version = (version & 0xff00) | 0xff;
2727 }
2728 /* DTLS 1.0 maps to TLS 1.1, not TLS 1.0. */
2729 if (version == TLS1_VERSION) {
2730 version = TLS1_1_VERSION;
2731 }
2732 return version;
2733}
2734
David Benjaminb9e4fa52015-12-29 23:58:17 -05002735uint16_t ssl3_protocol_version(const SSL *ssl) {
2736 assert(ssl->s3->have_version);
2737 return ssl3_version_from_wire(ssl, ssl->version);
2738}
2739
David Benjamin42fea372015-09-19 01:22:44 -04002740int SSL_is_server(SSL *ssl) { return ssl->server; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002741
David Benjamind4c2bce2015-10-17 12:28:18 -04002742void SSL_CTX_set_select_certificate_cb(
2743 SSL_CTX *ctx, int (*cb)(const struct ssl_early_callback_ctx *)) {
2744 ctx->select_certificate_cb = cb;
2745}
2746
Adam Langley524e7172015-02-20 16:04:00 -08002747void SSL_CTX_set_dos_protection_cb(
2748 SSL_CTX *ctx, int (*cb)(const struct ssl_early_callback_ctx *)) {
2749 ctx->dos_protection_cb = cb;
2750}
2751
David Benjamin1d5ef3b2015-10-12 19:54:18 -04002752void SSL_set_renegotiate_mode(SSL *ssl, enum ssl_renegotiate_mode_t mode) {
2753 ssl->renegotiate_mode = mode;
2754}
2755
2756void SSL_set_reject_peer_renegotiations(SSL *ssl, int reject) {
2757 SSL_set_renegotiate_mode(
2758 ssl, reject ? ssl_renegotiate_never : ssl_renegotiate_freely);
David Benjaminb16346b2015-04-08 19:16:58 -04002759}
2760
Adam Langley3f92d212015-02-20 15:32:52 -08002761int SSL_get_rc4_state(const SSL *ssl, const RC4_KEY **read_key,
2762 const RC4_KEY **write_key) {
David Benjamin79978df2015-12-25 15:56:49 -05002763 if (ssl->s3->aead_read_ctx == NULL || ssl->s3->aead_write_ctx == NULL) {
Adam Langley3f92d212015-02-20 15:32:52 -08002764 return 0;
2765 }
2766
David Benjamin79978df2015-12-25 15:56:49 -05002767 return EVP_AEAD_CTX_get_rc4_state(&ssl->s3->aead_read_ctx->ctx, read_key) &&
2768 EVP_AEAD_CTX_get_rc4_state(&ssl->s3->aead_write_ctx->ctx, write_key);
Adam Langley3f92d212015-02-20 15:32:52 -08002769}
David Benjaminda881e92015-04-26 14:45:04 -04002770
Adam Langleyc2d32802015-11-03 18:36:10 -08002771int SSL_get_ivs(const SSL *ssl, const uint8_t **out_read_iv,
2772 const uint8_t **out_write_iv, size_t *out_iv_len) {
David Benjamin79978df2015-12-25 15:56:49 -05002773 if (ssl->s3->aead_read_ctx == NULL || ssl->s3->aead_write_ctx == NULL) {
Adam Langleyc2d32802015-11-03 18:36:10 -08002774 return 0;
2775 }
2776
2777 size_t write_iv_len;
David Benjamin79978df2015-12-25 15:56:49 -05002778 if (!EVP_AEAD_CTX_get_iv(&ssl->s3->aead_read_ctx->ctx, out_read_iv,
2779 out_iv_len) ||
2780 !EVP_AEAD_CTX_get_iv(&ssl->s3->aead_write_ctx->ctx, out_write_iv,
Adam Langleyc2d32802015-11-03 18:36:10 -08002781 &write_iv_len) ||
2782 *out_iv_len != write_iv_len) {
2783 return 0;
2784 }
2785
2786 return 1;
David Benjamine348ff42015-11-06 17:55:30 -05002787}
David Benjamin6e807652015-11-02 12:02:20 -05002788
David Benjamin91b25012016-01-21 16:39:58 -05002789static uint64_t be_to_u64(const uint8_t in[8]) {
David Benjaminde942382016-02-11 12:02:01 -05002790 return (((uint64_t)in[0]) << 56) | (((uint64_t)in[1]) << 48) |
2791 (((uint64_t)in[2]) << 40) | (((uint64_t)in[3]) << 32) |
2792 (((uint64_t)in[4]) << 24) | (((uint64_t)in[5]) << 16) |
2793 (((uint64_t)in[6]) << 8) | ((uint64_t)in[7]);
David Benjamin91b25012016-01-21 16:39:58 -05002794}
2795
2796uint64_t SSL_get_read_sequence(const SSL *ssl) {
2797 /* TODO(davidben): Internally represent sequence numbers as uint64_t. */
David Benjaminde942382016-02-11 12:02:01 -05002798 if (SSL_IS_DTLS(ssl)) {
2799 /* max_seq_num already includes the epoch. */
2800 assert(ssl->d1->r_epoch == (ssl->d1->bitmap.max_seq_num >> 48));
2801 return ssl->d1->bitmap.max_seq_num;
2802 }
David Benjamin91b25012016-01-21 16:39:58 -05002803 return be_to_u64(ssl->s3->read_sequence);
2804}
2805
2806uint64_t SSL_get_write_sequence(const SSL *ssl) {
David Benjaminde942382016-02-11 12:02:01 -05002807 uint64_t ret = be_to_u64(ssl->s3->write_sequence);
2808 if (SSL_IS_DTLS(ssl)) {
2809 assert((ret >> 48) == 0);
2810 ret |= ((uint64_t)ssl->d1->w_epoch) << 48;
2811 }
2812 return ret;
David Benjamin91b25012016-01-21 16:39:58 -05002813}
2814
David Benjamin6e807652015-11-02 12:02:20 -05002815uint8_t SSL_get_server_key_exchange_hash(const SSL *ssl) {
2816 return ssl->s3->tmp.server_key_exchange_hash;
Adam Langleyc2d32802015-11-03 18:36:10 -08002817}
2818
David Benjamin96ba15f2015-12-25 14:10:17 -05002819size_t SSL_get_client_random(const SSL *ssl, uint8_t *out, size_t max_out) {
2820 if (max_out == 0) {
2821 return sizeof(ssl->s3->client_random);
2822 }
2823 if (max_out > sizeof(ssl->s3->client_random)) {
2824 max_out = sizeof(ssl->s3->client_random);
2825 }
2826 memcpy(out, ssl->s3->client_random, max_out);
2827 return max_out;
2828}
2829
2830size_t SSL_get_server_random(const SSL *ssl, uint8_t *out, size_t max_out) {
2831 if (max_out == 0) {
2832 return sizeof(ssl->s3->server_random);
2833 }
2834 if (max_out > sizeof(ssl->s3->server_random)) {
2835 max_out = sizeof(ssl->s3->server_random);
2836 }
2837 memcpy(out, ssl->s3->server_random, max_out);
2838 return max_out;
2839}
2840
David Benjaminf6494f42016-01-07 12:37:41 -08002841const SSL_CIPHER *SSL_get_pending_cipher(const SSL *ssl) {
2842 if (!SSL_in_init(ssl)) {
2843 return NULL;
2844 }
2845 return ssl->s3->tmp.new_cipher;
2846}
2847
David Benjamincdd0b7e2016-01-07 10:10:41 -08002848void SSL_CTX_set_retain_only_sha256_of_client_certs(SSL_CTX *ctx, int enabled) {
2849 ctx->retain_only_sha256_of_client_certs = !!enabled;
2850}
2851
David Benjamin27bbae42015-09-13 00:54:37 -04002852int SSL_clear(SSL *ssl) {
2853 if (ssl->method == NULL) {
2854 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_METHOD_SPECIFIED);
2855 return 0;
2856 }
2857
2858 if (ssl_clear_bad_session(ssl)) {
2859 SSL_SESSION_free(ssl->session);
2860 ssl->session = NULL;
2861 }
2862
2863 ssl->hit = 0;
David Benjamin27bbae42015-09-13 00:54:37 -04002864
2865 /* SSL_clear may be called before or after the |ssl| is initialized in either
2866 * accept or connect state. In the latter case, SSL_clear should preserve the
2867 * half and reset |ssl->state| accordingly. */
2868 if (ssl->handshake_func != NULL) {
2869 if (ssl->server) {
2870 SSL_set_accept_state(ssl);
2871 } else {
2872 SSL_set_connect_state(ssl);
2873 }
2874 } else {
2875 assert(ssl->state == 0);
2876 }
2877
2878 /* TODO(davidben): Some state on |ssl| is reset both in |SSL_new| and
2879 * |SSL_clear| because it is per-connection state rather than configuration
2880 * state. Per-connection state should be on |ssl->s3| and |ssl->d1| so it is
2881 * naturally reset at the right points between |SSL_new|, |SSL_clear|, and
2882 * |ssl3_new|. */
2883
2884 ssl->rwstate = SSL_NOTHING;
2885
2886 BUF_MEM_free(ssl->init_buf);
2887 ssl->init_buf = NULL;
2888
David Benjamin27bbae42015-09-13 00:54:37 -04002889 /* The ssl->d1->mtu is simultaneously configuration (preserved across
2890 * clear) and connection-specific state (gets reset).
2891 *
2892 * TODO(davidben): Avoid this. */
2893 unsigned mtu = 0;
2894 if (ssl->d1 != NULL) {
2895 mtu = ssl->d1->mtu;
2896 }
2897
2898 ssl->method->ssl_free(ssl);
2899 if (!ssl->method->ssl_new(ssl)) {
2900 return 0;
2901 }
David Benjamin27bbae42015-09-13 00:54:37 -04002902
2903 if (SSL_IS_DTLS(ssl) && (SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
2904 ssl->d1->mtu = mtu;
2905 }
2906
2907 ssl->client_version = ssl->version;
2908
2909 return 1;
2910}
2911
David Benjamin4e9cc712016-06-01 20:16:03 -04002912void ssl_do_info_callback(const SSL *ssl, int type, int value) {
2913 void (*cb)(const SSL *ssl, int type, int value) = NULL;
2914 if (ssl->info_callback != NULL) {
2915 cb = ssl->info_callback;
2916 } else if (ssl->ctx->info_callback != NULL) {
2917 cb = ssl->ctx->info_callback;
2918 }
2919
2920 if (cb != NULL) {
2921 cb(ssl, type, value);
2922 }
2923}
2924
2925void ssl_do_msg_callback(SSL *ssl, int is_write, int version, int content_type,
2926 const void *buf, size_t len) {
2927 if (ssl->msg_callback != NULL) {
2928 ssl->msg_callback(is_write, version, content_type, buf, len, ssl,
2929 ssl->msg_callback_arg);
2930 }
2931}
2932
David Benjaminda881e92015-04-26 14:45:04 -04002933int SSL_CTX_sess_connect(const SSL_CTX *ctx) { return 0; }
2934int SSL_CTX_sess_connect_good(const SSL_CTX *ctx) { return 0; }
2935int SSL_CTX_sess_connect_renegotiate(const SSL_CTX *ctx) { return 0; }
2936int SSL_CTX_sess_accept(const SSL_CTX *ctx) { return 0; }
2937int SSL_CTX_sess_accept_renegotiate(const SSL_CTX *ctx) { return 0; }
2938int SSL_CTX_sess_accept_good(const SSL_CTX *ctx) { return 0; }
2939int SSL_CTX_sess_hits(const SSL_CTX *ctx) { return 0; }
2940int SSL_CTX_sess_cb_hits(const SSL_CTX *ctx) { return 0; }
2941int SSL_CTX_sess_misses(const SSL_CTX *ctx) { return 0; }
2942int SSL_CTX_sess_timeouts(const SSL_CTX *ctx) { return 0; }
2943int SSL_CTX_sess_cache_full(const SSL_CTX *ctx) { return 0; }
David Benjamin936aada2016-06-07 19:09:22 -04002944
2945int SSL_num_renegotiations(const SSL *ssl) {
2946 return SSL_total_renegotiations(ssl);
2947}
2948
2949int SSL_CTX_need_tmp_RSA(const SSL_CTX *ctx) { return 0; }
2950int SSL_need_tmp_RSA(const SSL *ssl) { return 0; }
2951int SSL_CTX_set_tmp_rsa(SSL_CTX *ctx, const RSA *rsa) { return 1; }
2952int SSL_set_tmp_rsa(SSL *ssl, const RSA *rsa) { return 1; }
Matt Braithwaite4838d8a2015-08-20 13:20:03 -07002953void ERR_load_SSL_strings(void) {}
David Benjamin27bbae42015-09-13 00:54:37 -04002954void SSL_load_error_strings(void) {}
David Benjamin936aada2016-06-07 19:09:22 -04002955int SSL_cache_hit(SSL *ssl) { return SSL_session_reused(ssl); }
2956
2957int SSL_CTX_set_tmp_ecdh(SSL_CTX *ctx, const EC_KEY *ec_key) {
2958 if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) {
2959 OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
2960 return 0;
2961 }
2962 int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key));
2963 return SSL_CTX_set1_curves(ctx, &nid, 1);
2964}
2965
2966int SSL_set_tmp_ecdh(SSL *ssl, const EC_KEY *ec_key) {
2967 if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) {
2968 OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
2969 return 0;
2970 }
2971 int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key));
2972 return SSL_set1_curves(ssl, &nid, 1);
2973}