blob: 0d9569507bf562bad4ca37bfb7bc4c1e6dbb091b [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);
Steven Valdez4f94b1c2016-05-24 12:31:07 -0400300 } else if (!method->method->is_dtls) {
301 /* TODO(svaldez): Enable TLS 1.3 once implemented. */
302 SSL_CTX_set_max_version(ret, TLS1_2_VERSION);
David Benjamin27bbae42015-09-13 00:54:37 -0400303 }
304
305 return ret;
306
307err:
308 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
309err2:
310 SSL_CTX_free(ret);
311 return NULL;
312}
313
314void SSL_CTX_free(SSL_CTX *ctx) {
315 if (ctx == NULL ||
316 !CRYPTO_refcount_dec_and_test_zero(&ctx->references)) {
317 return;
318 }
319
320 X509_VERIFY_PARAM_free(ctx->param);
321
322 /* Free internal session cache. However: the remove_cb() may reference the
323 * ex_data of SSL_CTX, thus the ex_data store can only be removed after the
324 * sessions were flushed. As the ex_data handling routines might also touch
325 * the session cache, the most secure solution seems to be: empty (flush) the
326 * cache, then free ex_data, then finally free the cache. (See ticket
327 * [openssl.org #212].) */
328 SSL_CTX_flush_sessions(ctx, 0);
329
330 CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, ctx, &ctx->ex_data);
331
332 CRYPTO_MUTEX_cleanup(&ctx->lock);
333 lh_SSL_SESSION_free(ctx->sessions);
334 X509_STORE_free(ctx->cert_store);
335 ssl_cipher_preference_list_free(ctx->cipher_list);
336 sk_SSL_CIPHER_free(ctx->cipher_list_by_id);
337 ssl_cipher_preference_list_free(ctx->cipher_list_tls10);
338 ssl_cipher_preference_list_free(ctx->cipher_list_tls11);
339 ssl_cert_free(ctx->cert);
340 sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->client_custom_extensions,
341 SSL_CUSTOM_EXTENSION_free);
342 sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->server_custom_extensions,
343 SSL_CUSTOM_EXTENSION_free);
344 sk_X509_NAME_pop_free(ctx->client_CA, X509_NAME_free);
345 sk_SRTP_PROTECTION_PROFILE_free(ctx->srtp_profiles);
346 OPENSSL_free(ctx->psk_identity_hint);
Steven Valdezce902a92016-05-17 11:47:53 -0400347 OPENSSL_free(ctx->supported_group_list);
David Benjamin27bbae42015-09-13 00:54:37 -0400348 OPENSSL_free(ctx->alpn_client_proto_list);
349 OPENSSL_free(ctx->ocsp_response);
350 OPENSSL_free(ctx->signed_cert_timestamp_list);
351 EVP_PKEY_free(ctx->tlsext_channel_id_private);
David Benjamin27bbae42015-09-13 00:54:37 -0400352
353 OPENSSL_free(ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -0800354}
Adam Langley95c29f32014-06-20 12:00:00 -0700355
Adam Langleyfcf25832014-12-18 17:42:32 -0800356SSL *SSL_new(SSL_CTX *ctx) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800357 if (ctx == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400358 OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_CTX);
Adam Langleyfcf25832014-12-18 17:42:32 -0800359 return NULL;
360 }
361 if (ctx->method == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400362 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
Adam Langleyfcf25832014-12-18 17:42:32 -0800363 return NULL;
364 }
Adam Langley95c29f32014-06-20 12:00:00 -0700365
Brian Smith5ba06892016-02-07 09:36:04 -1000366 SSL *ssl = OPENSSL_malloc(sizeof(SSL));
David Benjamin0d56f882015-12-19 17:05:56 -0500367 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800368 goto err;
369 }
David Benjamin0d56f882015-12-19 17:05:56 -0500370 memset(ssl, 0, sizeof(SSL));
Adam Langley95c29f32014-06-20 12:00:00 -0700371
David Benjamin0d56f882015-12-19 17:05:56 -0500372 ssl->min_version = ctx->min_version;
373 ssl->max_version = ctx->max_version;
David Benjamin1eb367c2014-12-12 18:17:51 -0500374
Taylor Brandstetter376a0fe2016-05-10 19:30:28 -0700375 /* RFC 6347 states that implementations SHOULD use an initial timer value of
376 * 1 second. */
377 ssl->initial_timeout_duration_ms = 1000;
378
David Benjamin0d56f882015-12-19 17:05:56 -0500379 ssl->options = ctx->options;
380 ssl->mode = ctx->mode;
381 ssl->max_cert_list = ctx->max_cert_list;
Adam Langley95c29f32014-06-20 12:00:00 -0700382
David Benjamin0d56f882015-12-19 17:05:56 -0500383 ssl->cert = ssl_cert_dup(ctx->cert);
384 if (ssl->cert == NULL) {
David Benjamina5a3eeb2015-03-18 20:26:30 -0400385 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -0800386 }
Adam Langley95c29f32014-06-20 12:00:00 -0700387
David Benjamin0d56f882015-12-19 17:05:56 -0500388 ssl->msg_callback = ctx->msg_callback;
389 ssl->msg_callback_arg = ctx->msg_callback_arg;
390 ssl->verify_mode = ctx->verify_mode;
391 ssl->sid_ctx_length = ctx->sid_ctx_length;
392 assert(ssl->sid_ctx_length <= sizeof ssl->sid_ctx);
393 memcpy(&ssl->sid_ctx, &ctx->sid_ctx, sizeof(ssl->sid_ctx));
394 ssl->verify_callback = ctx->default_verify_callback;
Adam Langley95c29f32014-06-20 12:00:00 -0700395
David Benjamin0d56f882015-12-19 17:05:56 -0500396 ssl->param = X509_VERIFY_PARAM_new();
397 if (!ssl->param) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800398 goto err;
399 }
David Benjamin0d56f882015-12-19 17:05:56 -0500400 X509_VERIFY_PARAM_inherit(ssl->param, ctx->param);
401 ssl->quiet_shutdown = ctx->quiet_shutdown;
402 ssl->max_send_fragment = ctx->max_send_fragment;
Adam Langley95c29f32014-06-20 12:00:00 -0700403
Adam Langley0b5e3902015-05-15 13:08:38 -0700404 CRYPTO_refcount_inc(&ctx->references);
David Benjamin0d56f882015-12-19 17:05:56 -0500405 ssl->ctx = ctx;
Adam Langley0b5e3902015-05-15 13:08:38 -0700406 CRYPTO_refcount_inc(&ctx->references);
David Benjamin0d56f882015-12-19 17:05:56 -0500407 ssl->initial_ctx = ctx;
Adam Langley95c29f32014-06-20 12:00:00 -0700408
Steven Valdezce902a92016-05-17 11:47:53 -0400409 if (ctx->supported_group_list) {
410 ssl->supported_group_list =
411 BUF_memdup(ctx->supported_group_list,
412 ctx->supported_group_list_len * 2);
413 if (!ssl->supported_group_list) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800414 goto err;
415 }
Steven Valdezce902a92016-05-17 11:47:53 -0400416 ssl->supported_group_list_len = ctx->supported_group_list_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800417 }
Adam Langley95c29f32014-06-20 12:00:00 -0700418
David Benjamin0d56f882015-12-19 17:05:56 -0500419 if (ssl->ctx->alpn_client_proto_list) {
420 ssl->alpn_client_proto_list = BUF_memdup(
421 ssl->ctx->alpn_client_proto_list, ssl->ctx->alpn_client_proto_list_len);
422 if (ssl->alpn_client_proto_list == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800423 goto err;
424 }
David Benjamin0d56f882015-12-19 17:05:56 -0500425 ssl->alpn_client_proto_list_len = ssl->ctx->alpn_client_proto_list_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800426 }
Adam Langley95c29f32014-06-20 12:00:00 -0700427
David Benjamin0d56f882015-12-19 17:05:56 -0500428 ssl->verify_result = X509_V_OK;
429 ssl->method = ctx->method;
Adam Langley95c29f32014-06-20 12:00:00 -0700430
David Benjamin0d56f882015-12-19 17:05:56 -0500431 if (!ssl->method->ssl_new(ssl)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800432 goto err;
433 }
Adam Langley95c29f32014-06-20 12:00:00 -0700434
David Benjamin0d56f882015-12-19 17:05:56 -0500435 ssl->rwstate = SSL_NOTHING;
Adam Langley95c29f32014-06-20 12:00:00 -0700436
David Benjamin0d56f882015-12-19 17:05:56 -0500437 CRYPTO_new_ex_data(&ssl->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700438
David Benjamin0d56f882015-12-19 17:05:56 -0500439 ssl->psk_identity_hint = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800440 if (ctx->psk_identity_hint) {
David Benjamin0d56f882015-12-19 17:05:56 -0500441 ssl->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
442 if (ssl->psk_identity_hint == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800443 goto err;
444 }
445 }
David Benjamin0d56f882015-12-19 17:05:56 -0500446 ssl->psk_client_callback = ctx->psk_client_callback;
447 ssl->psk_server_callback = ctx->psk_server_callback;
Adam Langley95c29f32014-06-20 12:00:00 -0700448
David Benjamin0d56f882015-12-19 17:05:56 -0500449 ssl->tlsext_channel_id_enabled = ctx->tlsext_channel_id_enabled;
David Benjamin02ddbfd2015-01-11 13:09:11 -0500450 if (ctx->tlsext_channel_id_private) {
David Benjamin0d56f882015-12-19 17:05:56 -0500451 ssl->tlsext_channel_id_private =
David Benjamin9a10f8f2015-05-05 22:22:40 -0400452 EVP_PKEY_up_ref(ctx->tlsext_channel_id_private);
David Benjamin02ddbfd2015-01-11 13:09:11 -0500453 }
454
David Benjamin0d56f882015-12-19 17:05:56 -0500455 ssl->signed_cert_timestamps_enabled =
456 ssl->ctx->signed_cert_timestamps_enabled;
457 ssl->ocsp_stapling_enabled = ssl->ctx->ocsp_stapling_enabled;
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200458
David Benjamin0d56f882015-12-19 17:05:56 -0500459 return ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -0800460
Adam Langley95c29f32014-06-20 12:00:00 -0700461err:
David Benjamin0d56f882015-12-19 17:05:56 -0500462 SSL_free(ssl);
David Benjamin3570d732015-06-29 00:28:17 -0400463 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700464
Adam Langleyfcf25832014-12-18 17:42:32 -0800465 return NULL;
466}
Adam Langley95c29f32014-06-20 12:00:00 -0700467
David Benjamin27bbae42015-09-13 00:54:37 -0400468void SSL_free(SSL *ssl) {
469 if (ssl == NULL) {
470 return;
471 }
472
473 X509_VERIFY_PARAM_free(ssl->param);
474
475 CRYPTO_free_ex_data(&g_ex_data_class_ssl, ssl, &ssl->ex_data);
476
David Benjamin2f871122016-05-20 14:27:17 -0400477 ssl_free_wbio_buffer(ssl);
478 assert(ssl->bbio == NULL);
David Benjamin27bbae42015-09-13 00:54:37 -0400479
David Benjamin27bbae42015-09-13 00:54:37 -0400480 BIO_free_all(ssl->rbio);
David Benjaminf715c422016-06-11 19:01:56 -0400481 BIO_free_all(ssl->wbio);
David Benjamin27bbae42015-09-13 00:54:37 -0400482
483 BUF_MEM_free(ssl->init_buf);
484
485 /* add extra stuff */
486 ssl_cipher_preference_list_free(ssl->cipher_list);
487 sk_SSL_CIPHER_free(ssl->cipher_list_by_id);
488
489 ssl_clear_bad_session(ssl);
490 SSL_SESSION_free(ssl->session);
491
David Benjamin27bbae42015-09-13 00:54:37 -0400492 ssl_cert_free(ssl->cert);
493
494 OPENSSL_free(ssl->tlsext_hostname);
495 SSL_CTX_free(ssl->initial_ctx);
Steven Valdezce902a92016-05-17 11:47:53 -0400496 OPENSSL_free(ssl->supported_group_list);
David Benjamin27bbae42015-09-13 00:54:37 -0400497 OPENSSL_free(ssl->alpn_client_proto_list);
498 EVP_PKEY_free(ssl->tlsext_channel_id_private);
499 OPENSSL_free(ssl->psk_identity_hint);
500 sk_X509_NAME_pop_free(ssl->client_CA, X509_NAME_free);
David Benjamin27bbae42015-09-13 00:54:37 -0400501 sk_SRTP_PROTECTION_PROFILE_free(ssl->srtp_profiles);
502
503 if (ssl->method != NULL) {
504 ssl->method->ssl_free(ssl);
505 }
506 SSL_CTX_free(ssl->ctx);
507
508 OPENSSL_free(ssl);
509}
510
511void SSL_set_connect_state(SSL *ssl) {
512 ssl->server = 0;
David Benjamin27bbae42015-09-13 00:54:37 -0400513 ssl->state = SSL_ST_CONNECT;
David Benjamin9f1dc822016-06-07 17:03:46 -0400514 ssl->handshake_func = ssl3_connect;
David Benjamin27bbae42015-09-13 00:54:37 -0400515}
516
517void SSL_set_accept_state(SSL *ssl) {
518 ssl->server = 1;
David Benjamin27bbae42015-09-13 00:54:37 -0400519 ssl->state = SSL_ST_ACCEPT;
David Benjamin9f1dc822016-06-07 17:03:46 -0400520 ssl->handshake_func = ssl3_accept;
David Benjamin27bbae42015-09-13 00:54:37 -0400521}
522
David Benjaminf715c422016-06-11 19:01:56 -0400523static void ssl_set_rbio(SSL *ssl, BIO *rbio) {
524 BIO_free_all(ssl->rbio);
525 ssl->rbio = rbio;
526}
527
528static void ssl_set_wbio(SSL *ssl, BIO *wbio) {
David Benjamin27bbae42015-09-13 00:54:37 -0400529 /* If the output buffering BIO is still in place, remove it. */
530 if (ssl->bbio != NULL) {
David Benjamin2f871122016-05-20 14:27:17 -0400531 ssl->wbio = BIO_pop(ssl->wbio);
David Benjamin27bbae42015-09-13 00:54:37 -0400532 }
533
David Benjaminf715c422016-06-11 19:01:56 -0400534 BIO_free_all(ssl->wbio);
David Benjamin27bbae42015-09-13 00:54:37 -0400535 ssl->wbio = wbio;
David Benjamin2f871122016-05-20 14:27:17 -0400536
537 /* Re-attach |bbio| to the new |wbio|. */
538 if (ssl->bbio != NULL) {
539 ssl->wbio = BIO_push(ssl->bbio, ssl->wbio);
540 }
David Benjamin27bbae42015-09-13 00:54:37 -0400541}
542
David Benjaminf715c422016-06-11 19:01:56 -0400543void SSL_set_bio(SSL *ssl, BIO *rbio, BIO *wbio) {
544 /* For historical reasons, this function has many different cases in ownership
545 * handling. */
546
547 /* If the two arguments are equal, one fewer reference is granted than
548 * taken. */
549 if (rbio != NULL && rbio == wbio) {
550 BIO_up_ref(rbio);
551 }
552
553 /* If at most one of rbio or wbio is changed, only adopt one reference. */
554 if (rbio == SSL_get_rbio(ssl)) {
555 ssl_set_wbio(ssl, wbio);
556 return;
557 }
558 if (wbio == SSL_get_wbio(ssl)) {
559 ssl_set_rbio(ssl, rbio);
560 return;
561 }
562
563 /* Otherwise, adopt both references. */
564 ssl_set_rbio(ssl, rbio);
565 ssl_set_wbio(ssl, wbio);
566}
567
David Benjamin27bbae42015-09-13 00:54:37 -0400568BIO *SSL_get_rbio(const SSL *ssl) { return ssl->rbio; }
569
David Benjamin2f871122016-05-20 14:27:17 -0400570BIO *SSL_get_wbio(const SSL *ssl) {
571 if (ssl->bbio != NULL) {
572 /* If |bbio| is active, the true caller-configured BIO is its |next_bio|. */
573 assert(ssl->bbio == ssl->wbio);
574 return ssl->bbio->next_bio;
575 }
576 return ssl->wbio;
577}
David Benjamin27bbae42015-09-13 00:54:37 -0400578
579int SSL_do_handshake(SSL *ssl) {
David Benjamin4c5ddb82016-03-11 22:56:19 -0500580 ssl->rwstate = SSL_NOTHING;
David Benjamin15c14882016-03-14 14:25:46 -0400581 /* Functions which use SSL_get_error must clear the error queue on entry. */
582 ERR_clear_error();
David Benjamin0d275bd2016-06-01 20:19:34 -0400583 ERR_clear_system_error();
David Benjamin15c14882016-03-14 14:25:46 -0400584
David Benjamin27bbae42015-09-13 00:54:37 -0400585 if (ssl->handshake_func == NULL) {
586 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_TYPE_NOT_SET);
587 return -1;
588 }
589
590 if (!SSL_in_init(ssl)) {
591 return 1;
592 }
593
594 return ssl->handshake_func(ssl);
595}
596
597int SSL_connect(SSL *ssl) {
David Benjaminb2a73182016-03-10 01:25:26 -0500598 if (ssl->handshake_func == NULL) {
David Benjamin27bbae42015-09-13 00:54:37 -0400599 /* Not properly initialized yet */
600 SSL_set_connect_state(ssl);
601 }
602
David Benjamin15c14882016-03-14 14:25:46 -0400603 return SSL_do_handshake(ssl);
David Benjamin27bbae42015-09-13 00:54:37 -0400604}
605
606int SSL_accept(SSL *ssl) {
David Benjaminb2a73182016-03-10 01:25:26 -0500607 if (ssl->handshake_func == NULL) {
David Benjamin27bbae42015-09-13 00:54:37 -0400608 /* Not properly initialized yet */
609 SSL_set_accept_state(ssl);
610 }
611
David Benjamin15c14882016-03-14 14:25:46 -0400612 return SSL_do_handshake(ssl);
David Benjamin27bbae42015-09-13 00:54:37 -0400613}
614
David Benjamin15c14882016-03-14 14:25:46 -0400615static int ssl_read_impl(SSL *ssl, void *buf, int num, int peek) {
David Benjamin4c5ddb82016-03-11 22:56:19 -0500616 ssl->rwstate = SSL_NOTHING;
David Benjamin15c14882016-03-14 14:25:46 -0400617 /* Functions which use SSL_get_error must clear the error queue on entry. */
618 ERR_clear_error();
David Benjaminc79845c2016-03-10 01:28:00 -0500619 ERR_clear_system_error();
David Benjamin15c14882016-03-14 14:25:46 -0400620
David Benjaminb2a73182016-03-10 01:25:26 -0500621 if (ssl->handshake_func == NULL) {
David Benjamin27bbae42015-09-13 00:54:37 -0400622 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
623 return -1;
624 }
625
David Benjaminc79845c2016-03-10 01:28:00 -0500626 /* This may require multiple iterations. False Start will cause
627 * |ssl->handshake_func| to signal success one step early, but the handshake
628 * must be completely finished before other modes are accepted. */
629 while (SSL_in_init(ssl)) {
630 int ret = SSL_do_handshake(ssl);
631 if (ret < 0) {
632 return ret;
633 }
634 if (ret == 0) {
635 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
636 return -1;
637 }
638 }
639
David Benjamin15c14882016-03-14 14:25:46 -0400640 return ssl->method->ssl_read_app_data(ssl, buf, num, peek);
641}
642
643int SSL_read(SSL *ssl, void *buf, int num) {
644 return ssl_read_impl(ssl, buf, num, 0 /* consume bytes */);
David Benjamin27bbae42015-09-13 00:54:37 -0400645}
646
647int SSL_peek(SSL *ssl, void *buf, int num) {
David Benjamin15c14882016-03-14 14:25:46 -0400648 return ssl_read_impl(ssl, buf, num, 1 /* peek */);
David Benjamin27bbae42015-09-13 00:54:37 -0400649}
650
651int SSL_write(SSL *ssl, const void *buf, int num) {
David Benjamin4c5ddb82016-03-11 22:56:19 -0500652 ssl->rwstate = SSL_NOTHING;
David Benjamin15c14882016-03-14 14:25:46 -0400653 /* Functions which use SSL_get_error must clear the error queue on entry. */
654 ERR_clear_error();
David Benjaminc79845c2016-03-10 01:28:00 -0500655 ERR_clear_system_error();
David Benjamin15c14882016-03-14 14:25:46 -0400656
David Benjaminb2a73182016-03-10 01:25:26 -0500657 if (ssl->handshake_func == NULL) {
David Benjamin27bbae42015-09-13 00:54:37 -0400658 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
659 return -1;
660 }
661
David Benjaminfa214e42016-05-10 17:03:10 -0400662 if (ssl->s3->send_shutdown != ssl_shutdown_none) {
David Benjamin27bbae42015-09-13 00:54:37 -0400663 OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
664 return -1;
665 }
666
David Benjamind7ac1432016-03-10 00:41:25 -0500667 /* If necessary, complete the handshake implicitly. */
668 if (SSL_in_init(ssl) && !SSL_in_false_start(ssl)) {
669 int ret = SSL_do_handshake(ssl);
670 if (ret < 0) {
671 return ret;
672 }
673 if (ret == 0) {
674 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
675 return -1;
676 }
677 }
678
David Benjamin27bbae42015-09-13 00:54:37 -0400679 return ssl->method->ssl_write_app_data(ssl, buf, num);
680}
681
682int SSL_shutdown(SSL *ssl) {
David Benjamin4c5ddb82016-03-11 22:56:19 -0500683 ssl->rwstate = SSL_NOTHING;
David Benjamin15c14882016-03-14 14:25:46 -0400684 /* Functions which use SSL_get_error must clear the error queue on entry. */
685 ERR_clear_error();
David Benjamin0d275bd2016-06-01 20:19:34 -0400686 ERR_clear_system_error();
David Benjamin15c14882016-03-14 14:25:46 -0400687
David Benjaminb2a73182016-03-10 01:25:26 -0500688 if (ssl->handshake_func == NULL) {
David Benjamin27bbae42015-09-13 00:54:37 -0400689 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
690 return -1;
691 }
692
Steven Valdeza14934f2016-02-29 10:05:08 -0500693 /* We can't shutdown properly if we are in the middle of a handshake. */
694 if (SSL_in_init(ssl)) {
695 OPENSSL_PUT_ERROR(SSL, SSL_R_SHUTDOWN_WHILE_IN_INIT);
696 return -1;
697 }
698
David Benjamin27bbae42015-09-13 00:54:37 -0400699 if (ssl->quiet_shutdown) {
David Benjaminfa214e42016-05-10 17:03:10 -0400700 /* Do nothing if configured not to send a close_notify. */
701 ssl->s3->send_shutdown = ssl_shutdown_close_notify;
702 ssl->s3->recv_shutdown = ssl_shutdown_close_notify;
David Benjamin27bbae42015-09-13 00:54:37 -0400703 return 1;
704 }
705
David Benjaminfa214e42016-05-10 17:03:10 -0400706 /* This function completes in two stages. It sends a close_notify and then it
707 * waits for a close_notify to come in. Perform exactly one action and return
708 * whether or not it succeeds. */
David Benjamin27bbae42015-09-13 00:54:37 -0400709
David Benjaminfa214e42016-05-10 17:03:10 -0400710 if (ssl->s3->send_shutdown != ssl_shutdown_close_notify) {
711 /* Send a close_notify. */
712 if (ssl3_send_alert(ssl, SSL3_AL_WARNING, SSL_AD_CLOSE_NOTIFY) <= 0) {
713 return -1;
David Benjamin27bbae42015-09-13 00:54:37 -0400714 }
715 } else if (ssl->s3->alert_dispatch) {
David Benjaminfa214e42016-05-10 17:03:10 -0400716 /* Finish sending the close_notify. */
717 if (ssl->method->ssl_dispatch_alert(ssl) <= 0) {
718 return -1;
David Benjamin27bbae42015-09-13 00:54:37 -0400719 }
David Benjaminfa214e42016-05-10 17:03:10 -0400720 } else if (ssl->s3->recv_shutdown != ssl_shutdown_close_notify) {
721 /* Wait for the peer's close_notify. */
David Benjamin27bbae42015-09-13 00:54:37 -0400722 ssl->method->ssl_read_close_notify(ssl);
David Benjaminfa214e42016-05-10 17:03:10 -0400723 if (ssl->s3->recv_shutdown != ssl_shutdown_close_notify) {
724 return -1;
David Benjamin27bbae42015-09-13 00:54:37 -0400725 }
726 }
727
David Benjaminfa214e42016-05-10 17:03:10 -0400728 /* Return 0 for unidirectional shutdown and 1 for bidirectional shutdown. */
729 return ssl->s3->recv_shutdown == ssl_shutdown_close_notify;
David Benjamin27bbae42015-09-13 00:54:37 -0400730}
731
732int SSL_get_error(const SSL *ssl, int ret_code) {
733 int reason;
734 uint32_t err;
735 BIO *bio;
736
737 if (ret_code > 0) {
738 return SSL_ERROR_NONE;
739 }
740
741 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
742 * where we do encode the error */
743 err = ERR_peek_error();
744 if (err != 0) {
745 if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
746 return SSL_ERROR_SYSCALL;
747 }
748 return SSL_ERROR_SSL;
749 }
750
751 if (ret_code == 0) {
David Benjaminfa214e42016-05-10 17:03:10 -0400752 if (ssl->s3->recv_shutdown == ssl_shutdown_close_notify) {
David Benjamin27bbae42015-09-13 00:54:37 -0400753 return SSL_ERROR_ZERO_RETURN;
754 }
755 /* An EOF was observed which violates the protocol, and the underlying
756 * transport does not participate in the error queue. Bubble up to the
757 * caller. */
758 return SSL_ERROR_SYSCALL;
759 }
760
761 if (SSL_want_session(ssl)) {
762 return SSL_ERROR_PENDING_SESSION;
763 }
764
765 if (SSL_want_certificate(ssl)) {
766 return SSL_ERROR_PENDING_CERTIFICATE;
767 }
768
769 if (SSL_want_read(ssl)) {
770 bio = SSL_get_rbio(ssl);
771 if (BIO_should_read(bio)) {
772 return SSL_ERROR_WANT_READ;
773 }
774
775 if (BIO_should_write(bio)) {
776 /* This one doesn't make too much sense ... We never try to write to the
777 * rbio, and an application program where rbio and wbio are separate
778 * couldn't even know what it should wait for. However if we ever set
David Benjamin0d56f882015-12-19 17:05:56 -0500779 * ssl->rwstate incorrectly (so that we have SSL_want_read(ssl) instead of
780 * SSL_want_write(ssl)) and rbio and wbio *are* the same, this test works
David Benjamin27bbae42015-09-13 00:54:37 -0400781 * around that bug; so it might be safer to keep it. */
782 return SSL_ERROR_WANT_WRITE;
783 }
784
785 if (BIO_should_io_special(bio)) {
786 reason = BIO_get_retry_reason(bio);
787 if (reason == BIO_RR_CONNECT) {
788 return SSL_ERROR_WANT_CONNECT;
789 }
790
791 if (reason == BIO_RR_ACCEPT) {
792 return SSL_ERROR_WANT_ACCEPT;
793 }
794
795 return SSL_ERROR_SYSCALL; /* unknown */
796 }
797 }
798
799 if (SSL_want_write(ssl)) {
800 bio = SSL_get_wbio(ssl);
801 if (BIO_should_write(bio)) {
802 return SSL_ERROR_WANT_WRITE;
803 }
804
805 if (BIO_should_read(bio)) {
806 /* See above (SSL_want_read(ssl) with BIO_should_write(bio)) */
807 return SSL_ERROR_WANT_READ;
808 }
809
810 if (BIO_should_io_special(bio)) {
811 reason = BIO_get_retry_reason(bio);
812 if (reason == BIO_RR_CONNECT) {
813 return SSL_ERROR_WANT_CONNECT;
814 }
815
816 if (reason == BIO_RR_ACCEPT) {
817 return SSL_ERROR_WANT_ACCEPT;
818 }
819
820 return SSL_ERROR_SYSCALL;
821 }
822 }
823
824 if (SSL_want_x509_lookup(ssl)) {
825 return SSL_ERROR_WANT_X509_LOOKUP;
826 }
827
828 if (SSL_want_channel_id_lookup(ssl)) {
829 return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
830 }
831
832 if (SSL_want_private_key_operation(ssl)) {
833 return SSL_ERROR_WANT_PRIVATE_KEY_OPERATION;
834 }
835
836 return SSL_ERROR_SYSCALL;
837}
838
839void SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) {
840 ctx->min_version = version;
841}
842
843void SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) {
844 ctx->max_version = version;
845}
846
847void SSL_set_min_version(SSL *ssl, uint16_t version) {
848 ssl->min_version = version;
849}
850
851void SSL_set_max_version(SSL *ssl, uint16_t version) {
852 ssl->max_version = version;
853}
854
855uint32_t SSL_CTX_set_options(SSL_CTX *ctx, uint32_t options) {
856 ctx->options |= options;
857 return ctx->options;
858}
859
860uint32_t SSL_CTX_clear_options(SSL_CTX *ctx, uint32_t options) {
861 ctx->options &= ~options;
862 return ctx->options;
863}
864
865uint32_t SSL_CTX_get_options(const SSL_CTX *ctx) { return ctx->options; }
866
867uint32_t SSL_set_options(SSL *ssl, uint32_t options) {
868 ssl->options |= options;
869 return ssl->options;
870}
871
872uint32_t SSL_clear_options(SSL *ssl, uint32_t options) {
873 ssl->options &= ~options;
874 return ssl->options;
875}
876
877uint32_t SSL_get_options(const SSL *ssl) { return ssl->options; }
878
879uint32_t SSL_CTX_set_mode(SSL_CTX *ctx, uint32_t mode) {
880 ctx->mode |= mode;
881 return ctx->mode;
882}
883
884uint32_t SSL_CTX_clear_mode(SSL_CTX *ctx, uint32_t mode) {
885 ctx->mode &= ~mode;
886 return ctx->mode;
887}
888
889uint32_t SSL_CTX_get_mode(const SSL_CTX *ctx) { return ctx->mode; }
890
891uint32_t SSL_set_mode(SSL *ssl, uint32_t mode) {
892 ssl->mode |= mode;
893 return ssl->mode;
894}
895
896uint32_t SSL_clear_mode(SSL *ssl, uint32_t mode) {
897 ssl->mode &= ~mode;
898 return ssl->mode;
899}
900
901uint32_t SSL_get_mode(const SSL *ssl) { return ssl->mode; }
902
David Benjaminee0c8272015-09-13 01:03:54 -0400903X509 *SSL_get_peer_certificate(const SSL *ssl) {
904 if (ssl == NULL || ssl->session == NULL || ssl->session->peer == NULL) {
905 return NULL;
906 }
907 return X509_up_ref(ssl->session->peer);
908}
909
910STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *ssl) {
911 if (ssl == NULL || ssl->session == NULL) {
912 return NULL;
913 }
914 return ssl->session->cert_chain;
915}
916
917int SSL_get_tls_unique(const SSL *ssl, uint8_t *out, size_t *out_len,
918 size_t max_out) {
919 /* The tls-unique value is the first Finished message in the handshake, which
920 * is the client's in a full handshake and the server's for a resumption. See
921 * https://tools.ietf.org/html/rfc5929#section-3.1. */
922 const uint8_t *finished = ssl->s3->previous_client_finished;
923 size_t finished_len = ssl->s3->previous_client_finished_len;
924 if (ssl->hit) {
925 /* tls-unique is broken for resumed sessions unless EMS is used. */
926 if (!ssl->session->extended_master_secret) {
927 goto err;
928 }
929 finished = ssl->s3->previous_server_finished;
930 finished_len = ssl->s3->previous_server_finished_len;
931 }
932
933 if (!ssl->s3->initial_handshake_complete ||
934 ssl->version < TLS1_VERSION) {
935 goto err;
936 }
937
938 *out_len = finished_len;
939 if (finished_len > max_out) {
940 *out_len = max_out;
941 }
942
943 memcpy(out, finished, *out_len);
944 return 1;
945
946err:
947 *out_len = 0;
948 memset(out, 0, max_out);
949 return 0;
950}
951
Adam Langleyfcf25832014-12-18 17:42:32 -0800952int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
David Benjamindafbdd42015-09-14 01:40:10 -0400953 unsigned sid_ctx_len) {
954 if (sid_ctx_len > sizeof(ctx->sid_ctx)) {
David Benjamin3570d732015-06-29 00:28:17 -0400955 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -0800956 return 0;
957 }
958 ctx->sid_ctx_length = sid_ctx_len;
959 memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700960
Adam Langleyfcf25832014-12-18 17:42:32 -0800961 return 1;
962}
Adam Langley95c29f32014-06-20 12:00:00 -0700963
Adam Langleyfcf25832014-12-18 17:42:32 -0800964int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
David Benjamindafbdd42015-09-14 01:40:10 -0400965 unsigned sid_ctx_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800966 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
David Benjamin3570d732015-06-29 00:28:17 -0400967 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -0800968 return 0;
969 }
970 ssl->sid_ctx_length = sid_ctx_len;
971 memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700972
Adam Langleyfcf25832014-12-18 17:42:32 -0800973 return 1;
974}
Adam Langley95c29f32014-06-20 12:00:00 -0700975
David Benjamin59937042015-09-19 13:04:22 -0400976int SSL_CTX_set_purpose(SSL_CTX *ctx, int purpose) {
977 return X509_VERIFY_PARAM_set_purpose(ctx->param, purpose);
Adam Langleyfcf25832014-12-18 17:42:32 -0800978}
979
David Benjamin59937042015-09-19 13:04:22 -0400980int SSL_set_purpose(SSL *ssl, int purpose) {
981 return X509_VERIFY_PARAM_set_purpose(ssl->param, purpose);
Adam Langleyfcf25832014-12-18 17:42:32 -0800982}
983
David Benjamin59937042015-09-19 13:04:22 -0400984int SSL_CTX_set_trust(SSL_CTX *ctx, int trust) {
985 return X509_VERIFY_PARAM_set_trust(ctx->param, trust);
Adam Langleyfcf25832014-12-18 17:42:32 -0800986}
987
David Benjamin59937042015-09-19 13:04:22 -0400988int SSL_set_trust(SSL *ssl, int trust) {
989 return X509_VERIFY_PARAM_set_trust(ssl->param, trust);
Adam Langleyfcf25832014-12-18 17:42:32 -0800990}
991
David Benjamin59937042015-09-19 13:04:22 -0400992int SSL_CTX_set1_param(SSL_CTX *ctx, const X509_VERIFY_PARAM *param) {
993 return X509_VERIFY_PARAM_set1(ctx->param, param);
Adam Langleyfcf25832014-12-18 17:42:32 -0800994}
995
David Benjamin59937042015-09-19 13:04:22 -0400996int SSL_set1_param(SSL *ssl, const X509_VERIFY_PARAM *param) {
997 return X509_VERIFY_PARAM_set1(ssl->param, param);
Adam Langleyfcf25832014-12-18 17:42:32 -0800998}
Adam Langley95c29f32014-06-20 12:00:00 -0700999
Adam Langley858a88d2014-06-20 12:00:00 -07001000void ssl_cipher_preference_list_free(
Adam Langleyfcf25832014-12-18 17:42:32 -08001001 struct ssl_cipher_preference_list_st *cipher_list) {
David Benjamin5d1ec732015-04-22 13:38:00 -04001002 if (cipher_list == NULL) {
1003 return;
1004 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001005 sk_SSL_CIPHER_free(cipher_list->ciphers);
1006 OPENSSL_free(cipher_list->in_group_flags);
1007 OPENSSL_free(cipher_list);
1008}
Adam Langley858a88d2014-06-20 12:00:00 -07001009
Adam Langleyfcf25832014-12-18 17:42:32 -08001010X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) { return ctx->param; }
Adam Langley95c29f32014-06-20 12:00:00 -07001011
Adam Langleyfcf25832014-12-18 17:42:32 -08001012X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) { return ssl->param; }
Adam Langley95c29f32014-06-20 12:00:00 -07001013
David Benjamin7481d392015-07-05 19:38:46 -04001014void SSL_certs_clear(SSL *ssl) { ssl_cert_clear_certs(ssl->cert); }
Adam Langley95c29f32014-06-20 12:00:00 -07001015
David Benjamin066fe0a2015-10-17 21:11:33 -04001016int SSL_get_fd(const SSL *ssl) { return SSL_get_rfd(ssl); }
Adam Langley95c29f32014-06-20 12:00:00 -07001017
David Benjamin066fe0a2015-10-17 21:11:33 -04001018int SSL_get_rfd(const SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001019 int ret = -1;
David Benjamin066fe0a2015-10-17 21:11:33 -04001020 BIO *b = BIO_find_type(SSL_get_rbio(ssl), BIO_TYPE_DESCRIPTOR);
1021 if (b != NULL) {
1022 BIO_get_fd(b, &ret);
Adam Langleyfcf25832014-12-18 17:42:32 -08001023 }
1024 return ret;
1025}
Adam Langley95c29f32014-06-20 12:00:00 -07001026
David Benjamin066fe0a2015-10-17 21:11:33 -04001027int SSL_get_wfd(const SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001028 int ret = -1;
David Benjamin066fe0a2015-10-17 21:11:33 -04001029 BIO *b = BIO_find_type(SSL_get_wbio(ssl), BIO_TYPE_DESCRIPTOR);
1030 if (b != NULL) {
1031 BIO_get_fd(b, &ret);
Adam Langleyfcf25832014-12-18 17:42:32 -08001032 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001033 return ret;
1034}
Adam Langley95c29f32014-06-20 12:00:00 -07001035
David Benjamin066fe0a2015-10-17 21:11:33 -04001036int SSL_set_fd(SSL *ssl, int fd) {
David Benjamin22ce9b22016-03-06 19:26:52 -05001037 BIO *bio = BIO_new(BIO_s_socket());
Adam Langleyfcf25832014-12-18 17:42:32 -08001038 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001039 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
David Benjamin066fe0a2015-10-17 21:11:33 -04001040 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001041 }
1042 BIO_set_fd(bio, fd, BIO_NOCLOSE);
David Benjamin066fe0a2015-10-17 21:11:33 -04001043 SSL_set_bio(ssl, bio, bio);
1044 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001045}
Adam Langley95c29f32014-06-20 12:00:00 -07001046
David Benjamin066fe0a2015-10-17 21:11:33 -04001047int SSL_set_wfd(SSL *ssl, int fd) {
David Benjamin2f871122016-05-20 14:27:17 -04001048 BIO *rbio = SSL_get_rbio(ssl);
1049 if (rbio == NULL || BIO_method_type(rbio) != BIO_TYPE_SOCKET ||
1050 BIO_get_fd(rbio, NULL) != fd) {
David Benjamin22ce9b22016-03-06 19:26:52 -05001051 BIO *bio = BIO_new(BIO_s_socket());
Adam Langleyfcf25832014-12-18 17:42:32 -08001052 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001053 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
David Benjamin066fe0a2015-10-17 21:11:33 -04001054 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001055 }
1056 BIO_set_fd(bio, fd, BIO_NOCLOSE);
David Benjamin2f871122016-05-20 14:27:17 -04001057 SSL_set_bio(ssl, rbio, bio);
Adam Langleyfcf25832014-12-18 17:42:32 -08001058 } else {
David Benjamin2f871122016-05-20 14:27:17 -04001059 SSL_set_bio(ssl, rbio, rbio);
Adam Langleyfcf25832014-12-18 17:42:32 -08001060 }
1061
David Benjamin066fe0a2015-10-17 21:11:33 -04001062 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001063}
Adam Langley95c29f32014-06-20 12:00:00 -07001064
David Benjamin066fe0a2015-10-17 21:11:33 -04001065int SSL_set_rfd(SSL *ssl, int fd) {
David Benjamin2f871122016-05-20 14:27:17 -04001066 BIO *wbio = SSL_get_wbio(ssl);
1067 if (wbio == NULL || BIO_method_type(wbio) != BIO_TYPE_SOCKET ||
1068 BIO_get_fd(wbio, NULL) != fd) {
David Benjamin22ce9b22016-03-06 19:26:52 -05001069 BIO *bio = BIO_new(BIO_s_socket());
Adam Langleyfcf25832014-12-18 17:42:32 -08001070 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001071 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
David Benjamin066fe0a2015-10-17 21:11:33 -04001072 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001073 }
1074 BIO_set_fd(bio, fd, BIO_NOCLOSE);
David Benjamin2f871122016-05-20 14:27:17 -04001075 SSL_set_bio(ssl, bio, wbio);
Adam Langleyfcf25832014-12-18 17:42:32 -08001076 } else {
David Benjamin2f871122016-05-20 14:27:17 -04001077 SSL_set_bio(ssl, wbio, wbio);
Adam Langleyfcf25832014-12-18 17:42:32 -08001078 }
David Benjamin066fe0a2015-10-17 21:11:33 -04001079 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001080}
Adam Langley95c29f32014-06-20 12:00:00 -07001081
David Benjamin1a1b34d2015-10-17 12:51:52 -04001082size_t SSL_get_finished(const SSL *ssl, void *buf, size_t count) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001083 size_t ret = 0;
1084
David Benjamin1a1b34d2015-10-17 12:51:52 -04001085 if (ssl->s3 != NULL) {
1086 ret = ssl->s3->tmp.finish_md_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08001087 if (count > ret) {
1088 count = ret;
1089 }
David Benjamin1a1b34d2015-10-17 12:51:52 -04001090 memcpy(buf, ssl->s3->tmp.finish_md, count);
Adam Langleyfcf25832014-12-18 17:42:32 -08001091 }
1092
1093 return ret;
1094}
Adam Langley95c29f32014-06-20 12:00:00 -07001095
David Benjamin1a1b34d2015-10-17 12:51:52 -04001096size_t SSL_get_peer_finished(const SSL *ssl, void *buf, size_t count) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001097 size_t ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001098
David Benjamin1a1b34d2015-10-17 12:51:52 -04001099 if (ssl->s3 != NULL) {
1100 ret = ssl->s3->tmp.peer_finish_md_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08001101 if (count > ret) {
1102 count = ret;
1103 }
David Benjamin1a1b34d2015-10-17 12:51:52 -04001104 memcpy(buf, ssl->s3->tmp.peer_finish_md, count);
Adam Langleyfcf25832014-12-18 17:42:32 -08001105 }
Adam Langley95c29f32014-06-20 12:00:00 -07001106
Adam Langleyfcf25832014-12-18 17:42:32 -08001107 return ret;
1108}
Adam Langley95c29f32014-06-20 12:00:00 -07001109
David Benjamin59937042015-09-19 13:04:22 -04001110int SSL_get_verify_mode(const SSL *ssl) { return ssl->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -07001111
David Benjamin59937042015-09-19 13:04:22 -04001112int SSL_get_verify_depth(const SSL *ssl) {
1113 return X509_VERIFY_PARAM_get_depth(ssl->param);
Adam Langleyfcf25832014-12-18 17:42:32 -08001114}
Adam Langley95c29f32014-06-20 12:00:00 -07001115
David Benjamin42fea372015-09-19 01:22:44 -04001116int SSL_get_extms_support(const SSL *ssl) {
1117 return ssl->s3->tmp.extended_master_secret == 1;
Matt Braithwaitecd6f54b2015-09-17 12:54:42 -07001118}
1119
David Benjamin59937042015-09-19 13:04:22 -04001120int (*SSL_get_verify_callback(const SSL *ssl))(int, X509_STORE_CTX *) {
1121 return ssl->verify_callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08001122}
Adam Langley95c29f32014-06-20 12:00:00 -07001123
Adam Langleyfcf25832014-12-18 17:42:32 -08001124int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) { return ctx->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -07001125
Adam Langleyfcf25832014-12-18 17:42:32 -08001126int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) {
1127 return X509_VERIFY_PARAM_get_depth(ctx->param);
1128}
Adam Langley95c29f32014-06-20 12:00:00 -07001129
David Benjamin59937042015-09-19 13:04:22 -04001130int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(
1131 int ok, X509_STORE_CTX *store_ctx) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001132 return ctx->default_verify_callback;
1133}
Adam Langley95c29f32014-06-20 12:00:00 -07001134
David Benjamin59937042015-09-19 13:04:22 -04001135void SSL_set_verify(SSL *ssl, int mode,
1136 int (*callback)(int ok, X509_STORE_CTX *store_ctx)) {
1137 ssl->verify_mode = mode;
Adam Langleyfcf25832014-12-18 17:42:32 -08001138 if (callback != NULL) {
David Benjamin59937042015-09-19 13:04:22 -04001139 ssl->verify_callback = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08001140 }
1141}
Adam Langley95c29f32014-06-20 12:00:00 -07001142
David Benjamin59937042015-09-19 13:04:22 -04001143void SSL_set_verify_depth(SSL *ssl, int depth) {
1144 X509_VERIFY_PARAM_set_depth(ssl->param, depth);
Adam Langleyfcf25832014-12-18 17:42:32 -08001145}
Adam Langley95c29f32014-06-20 12:00:00 -07001146
David Benjamin9a41d1b2015-05-16 01:30:09 -04001147int SSL_CTX_get_read_ahead(const SSL_CTX *ctx) { return 0; }
Adam Langley95c29f32014-06-20 12:00:00 -07001148
David Benjamin0d56f882015-12-19 17:05:56 -05001149int SSL_get_read_ahead(const SSL *ssl) { return 0; }
Adam Langley95c29f32014-06-20 12:00:00 -07001150
David Benjamin9a41d1b2015-05-16 01:30:09 -04001151void SSL_CTX_set_read_ahead(SSL_CTX *ctx, int yes) { }
David Benjamin61ecccf2015-05-05 09:44:51 -04001152
David Benjamin0d56f882015-12-19 17:05:56 -05001153void SSL_set_read_ahead(SSL *ssl, int yes) { }
David Benjamin61ecccf2015-05-05 09:44:51 -04001154
David Benjamin9f859492015-10-03 10:44:30 -04001155int SSL_pending(const SSL *ssl) {
1156 if (ssl->s3->rrec.type != SSL3_RT_APPLICATION_DATA) {
1157 return 0;
1158 }
1159 return ssl->s3->rrec.length;
Adam Langleyfcf25832014-12-18 17:42:32 -08001160}
Adam Langley95c29f32014-06-20 12:00:00 -07001161
Adam Langley95c29f32014-06-20 12:00:00 -07001162/* Fix this so it checks all the valid key/cert options */
Adam Langleyfcf25832014-12-18 17:42:32 -08001163int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
David Benjamin651b3d92015-08-09 12:07:25 -04001164 if (ctx->cert->x509 == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001165 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -08001166 return 0;
1167 }
1168
David Benjamind1d80782015-07-05 11:54:09 -04001169 if (ctx->cert->privatekey == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001170 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -08001171 return 0;
1172 }
1173
David Benjamind1d80782015-07-05 11:54:09 -04001174 return X509_check_private_key(ctx->cert->x509, ctx->cert->privatekey);
Adam Langleyfcf25832014-12-18 17:42:32 -08001175}
Adam Langley95c29f32014-06-20 12:00:00 -07001176
1177/* Fix this function so that it takes an optional type parameter */
Adam Langleyfcf25832014-12-18 17:42:32 -08001178int SSL_check_private_key(const SSL *ssl) {
David Benjamind1d80782015-07-05 11:54:09 -04001179 if (ssl->cert->x509 == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001180 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -08001181 return 0;
1182 }
David Benjamin0b145c22014-11-26 20:10:09 -05001183
David Benjamind1d80782015-07-05 11:54:09 -04001184 if (ssl->cert->privatekey == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001185 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -08001186 return 0;
1187 }
Adam Langley95c29f32014-06-20 12:00:00 -07001188
David Benjamind1d80782015-07-05 11:54:09 -04001189 return X509_check_private_key(ssl->cert->x509, ssl->cert->privatekey);
Adam Langleyfcf25832014-12-18 17:42:32 -08001190}
Adam Langley95c29f32014-06-20 12:00:00 -07001191
David Benjamin42fea372015-09-19 01:22:44 -04001192long SSL_get_default_timeout(const SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001193 return SSL_DEFAULT_SESSION_TIMEOUT;
1194}
Adam Langley95c29f32014-06-20 12:00:00 -07001195
David Benjamin44d3eed2015-05-21 01:29:55 -04001196int SSL_renegotiate(SSL *ssl) {
1197 /* Caller-initiated renegotiation is not supported. */
David Benjamin3570d732015-06-29 00:28:17 -04001198 OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
David Benjamin44d3eed2015-05-21 01:29:55 -04001199 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001200}
Adam Langley95c29f32014-06-20 12:00:00 -07001201
David Benjamin44d3eed2015-05-21 01:29:55 -04001202int SSL_renegotiate_pending(SSL *ssl) {
1203 return SSL_in_init(ssl) && ssl->s3->initial_handshake_complete;
Adam Langleyfcf25832014-12-18 17:42:32 -08001204}
Adam Langley95c29f32014-06-20 12:00:00 -07001205
David Benjamin936aada2016-06-07 19:09:22 -04001206int SSL_total_renegotiations(const SSL *ssl) {
1207 return ssl->s3->total_renegotiations;
1208}
1209
David Benjamin1d0a1942015-04-26 15:35:35 -04001210size_t SSL_CTX_get_max_cert_list(const SSL_CTX *ctx) {
1211 return ctx->max_cert_list;
1212}
1213
1214void SSL_CTX_set_max_cert_list(SSL_CTX *ctx, size_t max_cert_list) {
1215 if (max_cert_list > kMaxHandshakeSize) {
1216 max_cert_list = kMaxHandshakeSize;
1217 }
1218 ctx->max_cert_list = (uint32_t)max_cert_list;
1219}
1220
1221size_t SSL_get_max_cert_list(const SSL *ssl) {
1222 return ssl->max_cert_list;
1223}
1224
1225void SSL_set_max_cert_list(SSL *ssl, size_t max_cert_list) {
1226 if (max_cert_list > kMaxHandshakeSize) {
1227 max_cert_list = kMaxHandshakeSize;
1228 }
1229 ssl->max_cert_list = (uint32_t)max_cert_list;
1230}
1231
Adam Langleyce9d85e2016-01-24 15:58:39 -08001232int SSL_CTX_set_max_send_fragment(SSL_CTX *ctx, size_t max_send_fragment) {
David Benjamin1d0a1942015-04-26 15:35:35 -04001233 if (max_send_fragment < 512) {
1234 max_send_fragment = 512;
1235 }
1236 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1237 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1238 }
1239 ctx->max_send_fragment = (uint16_t)max_send_fragment;
Adam Langleyce9d85e2016-01-24 15:58:39 -08001240
1241 return 1;
David Benjamin1d0a1942015-04-26 15:35:35 -04001242}
1243
Adam Langleyce9d85e2016-01-24 15:58:39 -08001244int SSL_set_max_send_fragment(SSL *ssl, size_t max_send_fragment) {
David Benjamin1d0a1942015-04-26 15:35:35 -04001245 if (max_send_fragment < 512) {
1246 max_send_fragment = 512;
1247 }
1248 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1249 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1250 }
1251 ssl->max_send_fragment = (uint16_t)max_send_fragment;
Adam Langleyce9d85e2016-01-24 15:58:39 -08001252
1253 return 1;
David Benjamin1d0a1942015-04-26 15:35:35 -04001254}
1255
David Benjamincb9cf792015-05-05 09:46:14 -04001256int SSL_set_mtu(SSL *ssl, unsigned mtu) {
1257 if (!SSL_IS_DTLS(ssl) || mtu < dtls1_min_mtu()) {
1258 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001259 }
David Benjamincb9cf792015-05-05 09:46:14 -04001260 ssl->d1->mtu = mtu;
1261 return 1;
1262}
1263
1264int SSL_get_secure_renegotiation_support(const SSL *ssl) {
1265 return ssl->s3->send_connection_binding;
1266}
1267
Adam Langleyfcf25832014-12-18 17:42:32 -08001268LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) { return ctx->sessions; }
1269
David Benjamin71f7d3d2015-05-05 09:46:38 -04001270size_t SSL_CTX_sess_number(const SSL_CTX *ctx) {
1271 return lh_SSL_SESSION_num_items(ctx->sessions);
1272}
1273
1274unsigned long SSL_CTX_sess_set_cache_size(SSL_CTX *ctx, unsigned long size) {
1275 unsigned long ret = ctx->session_cache_size;
1276 ctx->session_cache_size = size;
1277 return ret;
1278}
1279
1280unsigned long SSL_CTX_sess_get_cache_size(const SSL_CTX *ctx) {
1281 return ctx->session_cache_size;
1282}
1283
1284int SSL_CTX_set_session_cache_mode(SSL_CTX *ctx, int mode) {
1285 int ret = ctx->session_cache_mode;
1286 ctx->session_cache_mode = mode;
1287 return ret;
1288}
1289
1290int SSL_CTX_get_session_cache_mode(const SSL_CTX *ctx) {
1291 return ctx->session_cache_mode;
1292}
1293
David Benjamin936aada2016-06-07 19:09:22 -04001294
1295int SSL_CTX_get_tlsext_ticket_keys(SSL_CTX *ctx, void *out, size_t len) {
1296 if (out == NULL) {
1297 return 48;
1298 }
1299 if (len != 48) {
1300 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH);
1301 return 0;
1302 }
1303 uint8_t *out_bytes = out;
1304 memcpy(out_bytes, ctx->tlsext_tick_key_name, 16);
1305 memcpy(out_bytes + 16, ctx->tlsext_tick_hmac_key, 16);
1306 memcpy(out_bytes + 32, ctx->tlsext_tick_aes_key, 16);
1307 return 1;
1308}
1309
1310int SSL_CTX_set_tlsext_ticket_keys(SSL_CTX *ctx, const void *in, size_t len) {
1311 if (in == NULL) {
1312 return 48;
1313 }
1314 if (len != 48) {
1315 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH);
1316 return 0;
1317 }
1318 const uint8_t *in_bytes = in;
1319 memcpy(ctx->tlsext_tick_key_name, in_bytes, 16);
1320 memcpy(ctx->tlsext_tick_hmac_key, in_bytes + 16, 16);
1321 memcpy(ctx->tlsext_tick_aes_key, in_bytes + 32, 16);
1322 return 1;
1323}
1324
1325int SSL_CTX_set_tlsext_ticket_key_cb(
1326 SSL_CTX *ctx, int (*callback)(SSL *ssl, uint8_t *key_name, uint8_t *iv,
1327 EVP_CIPHER_CTX *ctx, HMAC_CTX *hmac_ctx,
1328 int encrypt)) {
1329 ctx->tlsext_ticket_key_cb = callback;
1330 return 1;
1331}
1332
1333int SSL_CTX_set1_curves(SSL_CTX *ctx, const int *curves, size_t curves_len) {
1334 return tls1_set_curves(&ctx->supported_group_list,
1335 &ctx->supported_group_list_len, curves,
1336 curves_len);
1337}
1338
1339int SSL_set1_curves(SSL *ssl, const int *curves, size_t curves_len) {
1340 return tls1_set_curves(&ssl->supported_group_list,
1341 &ssl->supported_group_list_len, curves,
1342 curves_len);
1343}
1344
1345int SSL_CTX_set_tmp_dh(SSL_CTX *ctx, const DH *dh) {
1346 DH_free(ctx->cert->dh_tmp);
1347 ctx->cert->dh_tmp = DHparams_dup(dh);
1348 if (ctx->cert->dh_tmp == NULL) {
1349 OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB);
1350 return 0;
1351 }
1352 return 1;
1353}
1354
1355int SSL_set_tmp_dh(SSL *ssl, const DH *dh) {
1356 DH_free(ssl->cert->dh_tmp);
1357 ssl->cert->dh_tmp = DHparams_dup(dh);
1358 if (ssl->cert->dh_tmp == NULL) {
1359 OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB);
1360 return 0;
1361 }
1362 return 1;
1363}
1364
David Benjamin32876b32015-09-20 12:17:03 -04001365STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *ssl) {
1366 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001367 return NULL;
1368 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001369
David Benjamin32876b32015-09-20 12:17:03 -04001370 if (ssl->cipher_list != NULL) {
1371 return ssl->cipher_list->ciphers;
Adam Langleyfcf25832014-12-18 17:42:32 -08001372 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001373
David Benjamin78f8aab2016-03-10 16:33:58 -05001374 if (ssl->version >= TLS1_1_VERSION && ssl->ctx->cipher_list_tls11 != NULL) {
David Benjamin32876b32015-09-20 12:17:03 -04001375 return ssl->ctx->cipher_list_tls11->ciphers;
Adam Langleyfcf25832014-12-18 17:42:32 -08001376 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001377
David Benjamin78f8aab2016-03-10 16:33:58 -05001378 if (ssl->version >= TLS1_VERSION && ssl->ctx->cipher_list_tls10 != NULL) {
David Benjamin32876b32015-09-20 12:17:03 -04001379 return ssl->ctx->cipher_list_tls10->ciphers;
Adam Langleycef75832015-09-03 14:51:12 -07001380 }
1381
David Benjamin78f8aab2016-03-10 16:33:58 -05001382 if (ssl->ctx->cipher_list != NULL) {
David Benjamin32876b32015-09-20 12:17:03 -04001383 return ssl->ctx->cipher_list->ciphers;
Adam Langleyfcf25832014-12-18 17:42:32 -08001384 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001385
Adam Langleyfcf25832014-12-18 17:42:32 -08001386 return NULL;
1387}
Adam Langley95c29f32014-06-20 12:00:00 -07001388
Adam Langleyfcf25832014-12-18 17:42:32 -08001389/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001390 * algorithm id */
David Benjamin0d56f882015-12-19 17:05:56 -05001391STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *ssl) {
1392 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001393 return NULL;
1394 }
Adam Langley95c29f32014-06-20 12:00:00 -07001395
David Benjamin0d56f882015-12-19 17:05:56 -05001396 if (ssl->cipher_list_by_id != NULL) {
1397 return ssl->cipher_list_by_id;
Adam Langleyfcf25832014-12-18 17:42:32 -08001398 }
Adam Langley95c29f32014-06-20 12:00:00 -07001399
David Benjamin78f8aab2016-03-10 16:33:58 -05001400 if (ssl->ctx->cipher_list_by_id != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05001401 return ssl->ctx->cipher_list_by_id;
Adam Langleyfcf25832014-12-18 17:42:32 -08001402 }
Adam Langley95c29f32014-06-20 12:00:00 -07001403
Adam Langleyfcf25832014-12-18 17:42:32 -08001404 return NULL;
1405}
Adam Langley95c29f32014-06-20 12:00:00 -07001406
David Benjamin32876b32015-09-20 12:17:03 -04001407const char *SSL_get_cipher_list(const SSL *ssl, int n) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001408 const SSL_CIPHER *c;
David Benjamin60da0cd2015-05-03 15:21:28 -04001409 STACK_OF(SSL_CIPHER) *sk;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001410
David Benjamin32876b32015-09-20 12:17:03 -04001411 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001412 return NULL;
1413 }
Adam Langley95c29f32014-06-20 12:00:00 -07001414
David Benjamin32876b32015-09-20 12:17:03 -04001415 sk = SSL_get_ciphers(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001416 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
1417 return NULL;
1418 }
Adam Langley95c29f32014-06-20 12:00:00 -07001419
Adam Langleyfcf25832014-12-18 17:42:32 -08001420 c = sk_SSL_CIPHER_value(sk, n);
1421 if (c == NULL) {
1422 return NULL;
1423 }
Adam Langley95c29f32014-06-20 12:00:00 -07001424
Adam Langleyfcf25832014-12-18 17:42:32 -08001425 return c->name;
1426}
David Benjamin5491e3f2014-09-29 19:33:09 -04001427
Adam Langleyfcf25832014-12-18 17:42:32 -08001428int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
David Benjamin32876b32015-09-20 12:17:03 -04001429 STACK_OF(SSL_CIPHER) *cipher_list = ssl_create_cipher_list(
1430 ctx->method, &ctx->cipher_list, &ctx->cipher_list_by_id, str);
1431 if (cipher_list == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001432 return 0;
David Benjamin32876b32015-09-20 12:17:03 -04001433 }
1434
1435 /* |ssl_create_cipher_list| may succeed but return an empty cipher list. */
1436 if (sk_SSL_CIPHER_num(cipher_list) == 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001437 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
Adam Langleyfcf25832014-12-18 17:42:32 -08001438 return 0;
1439 }
Adam Langley95c29f32014-06-20 12:00:00 -07001440
Adam Langleyfcf25832014-12-18 17:42:32 -08001441 return 1;
1442}
David Benjamin39482a12014-07-20 13:30:15 -04001443
Adam Langleycef75832015-09-03 14:51:12 -07001444int SSL_CTX_set_cipher_list_tls10(SSL_CTX *ctx, const char *str) {
David Benjamin32876b32015-09-20 12:17:03 -04001445 STACK_OF(SSL_CIPHER) *cipher_list = ssl_create_cipher_list(
1446 ctx->method, &ctx->cipher_list_tls10, NULL, str);
1447 if (cipher_list == NULL) {
Adam Langleycef75832015-09-03 14:51:12 -07001448 return 0;
David Benjamin32876b32015-09-20 12:17:03 -04001449 }
1450
1451 /* |ssl_create_cipher_list| may succeed but return an empty cipher list. */
1452 if (sk_SSL_CIPHER_num(cipher_list) == 0) {
Adam Langleycef75832015-09-03 14:51:12 -07001453 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
1454 return 0;
1455 }
1456
1457 return 1;
1458}
1459
Adam Langleyfcf25832014-12-18 17:42:32 -08001460int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str) {
David Benjamin32876b32015-09-20 12:17:03 -04001461 STACK_OF(SSL_CIPHER) *cipher_list = ssl_create_cipher_list(
1462 ctx->method, &ctx->cipher_list_tls11, NULL, str);
1463 if (cipher_list == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001464 return 0;
David Benjamin32876b32015-09-20 12:17:03 -04001465 }
1466
1467 /* |ssl_create_cipher_list| may succeed but return an empty cipher list. */
1468 if (sk_SSL_CIPHER_num(cipher_list) == 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001469 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
Adam Langleyfcf25832014-12-18 17:42:32 -08001470 return 0;
1471 }
David Benjamin9f2c0d72014-10-21 22:00:19 -04001472
Adam Langleyfcf25832014-12-18 17:42:32 -08001473 return 1;
1474}
Adam Langley95c29f32014-06-20 12:00:00 -07001475
David Benjamin32876b32015-09-20 12:17:03 -04001476int SSL_set_cipher_list(SSL *ssl, const char *str) {
1477 STACK_OF(SSL_CIPHER) *cipher_list = ssl_create_cipher_list(
1478 ssl->ctx->method, &ssl->cipher_list, &ssl->cipher_list_by_id, str);
1479 if (cipher_list == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001480 return 0;
David Benjamin32876b32015-09-20 12:17:03 -04001481 }
1482
1483 /* |ssl_create_cipher_list| may succeed but return an empty cipher list. */
1484 if (sk_SSL_CIPHER_num(cipher_list) == 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001485 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
Adam Langleyfcf25832014-12-18 17:42:32 -08001486 return 0;
1487 }
David Benjamin39482a12014-07-20 13:30:15 -04001488
Adam Langleyfcf25832014-12-18 17:42:32 -08001489 return 1;
1490}
Adam Langley95c29f32014-06-20 12:00:00 -07001491
David Benjamin0d56f882015-12-19 17:05:56 -05001492STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *ssl, const CBS *cbs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001493 CBS cipher_suites = *cbs;
1494 const SSL_CIPHER *c;
David Benjamin60da0cd2015-05-03 15:21:28 -04001495 STACK_OF(SSL_CIPHER) *sk;
Adam Langleyfcf25832014-12-18 17:42:32 -08001496
David Benjamin0d56f882015-12-19 17:05:56 -05001497 if (ssl->s3) {
1498 ssl->s3->send_connection_binding = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001499 }
1500
1501 if (CBS_len(&cipher_suites) % 2 != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001502 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
Adam Langleyfcf25832014-12-18 17:42:32 -08001503 return NULL;
1504 }
1505
1506 sk = sk_SSL_CIPHER_new_null();
1507 if (sk == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001508 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001509 goto err;
1510 }
1511
Adam Langleyfcf25832014-12-18 17:42:32 -08001512 while (CBS_len(&cipher_suites) > 0) {
1513 uint16_t cipher_suite;
1514
1515 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
David Benjamin3570d732015-06-29 00:28:17 -04001516 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001517 goto err;
1518 }
1519
1520 /* Check for SCSV. */
David Benjamin0d56f882015-12-19 17:05:56 -05001521 if (ssl->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001522 /* SCSV is fatal if renegotiating. */
David Benjamin0d56f882015-12-19 17:05:56 -05001523 if (ssl->s3->initial_handshake_complete) {
David Benjamin3570d732015-06-29 00:28:17 -04001524 OPENSSL_PUT_ERROR(SSL, SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
David Benjamin0d56f882015-12-19 17:05:56 -05001525 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001526 goto err;
1527 }
David Benjamin0d56f882015-12-19 17:05:56 -05001528 ssl->s3->send_connection_binding = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001529 continue;
1530 }
1531
1532 /* Check for FALLBACK_SCSV. */
David Benjamin0d56f882015-12-19 17:05:56 -05001533 if (ssl->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff)) {
1534 uint16_t max_version = ssl3_get_max_server_version(ssl);
1535 if (SSL_IS_DTLS(ssl) ? (uint16_t)ssl->version > max_version
1536 : (uint16_t)ssl->version < max_version) {
David Benjamin3570d732015-06-29 00:28:17 -04001537 OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK);
David Benjamin0d56f882015-12-19 17:05:56 -05001538 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
Adam Langleyfcf25832014-12-18 17:42:32 -08001539 goto err;
1540 }
1541 continue;
1542 }
1543
David Benjamina1c90a52015-05-30 17:03:14 -04001544 c = SSL_get_cipher_by_value(cipher_suite);
Adam Langleyfcf25832014-12-18 17:42:32 -08001545 if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
David Benjamin3570d732015-06-29 00:28:17 -04001546 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001547 goto err;
1548 }
1549 }
1550
1551 return sk;
David Benjamin9f2c0d72014-10-21 22:00:19 -04001552
Adam Langley95c29f32014-06-20 12:00:00 -07001553err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001554 sk_SSL_CIPHER_free(sk);
Adam Langleyfcf25832014-12-18 17:42:32 -08001555 return NULL;
1556}
Adam Langley95c29f32014-06-20 12:00:00 -07001557
David Benjamin07e13842015-10-17 13:48:04 -04001558const char *SSL_get_servername(const SSL *ssl, const int type) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001559 if (type != TLSEXT_NAMETYPE_host_name) {
1560 return NULL;
1561 }
Adam Langley95c29f32014-06-20 12:00:00 -07001562
David Benjamin07e13842015-10-17 13:48:04 -04001563 /* Historically, |SSL_get_servername| was also the configuration getter
1564 * corresponding to |SSL_set_tlsext_host_name|. */
1565 if (ssl->tlsext_hostname != NULL) {
1566 return ssl->tlsext_hostname;
1567 }
1568
1569 if (ssl->session == NULL) {
1570 return NULL;
1571 }
1572 return ssl->session->tlsext_hostname;
Adam Langleyfcf25832014-12-18 17:42:32 -08001573}
Adam Langley95c29f32014-06-20 12:00:00 -07001574
David Benjamin07e13842015-10-17 13:48:04 -04001575int SSL_get_servername_type(const SSL *ssl) {
1576 if (ssl->session != NULL && ssl->session->tlsext_hostname != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001577 return TLSEXT_NAMETYPE_host_name;
1578 }
Adam Langley95c29f32014-06-20 12:00:00 -07001579
Adam Langleyfcf25832014-12-18 17:42:32 -08001580 return -1;
1581}
Adam Langley95c29f32014-06-20 12:00:00 -07001582
Adam Langleyfcf25832014-12-18 17:42:32 -08001583void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
1584 ctx->signed_cert_timestamps_enabled = 1;
1585}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001586
Adam Langleyfcf25832014-12-18 17:42:32 -08001587int SSL_enable_signed_cert_timestamps(SSL *ssl) {
1588 ssl->signed_cert_timestamps_enabled = 1;
1589 return 1;
1590}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001591
Adam Langleyfcf25832014-12-18 17:42:32 -08001592void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
1593 ctx->ocsp_stapling_enabled = 1;
1594}
David Benjamin6c7aed02014-08-27 16:42:38 -04001595
Adam Langleyfcf25832014-12-18 17:42:32 -08001596int SSL_enable_ocsp_stapling(SSL *ssl) {
1597 ssl->ocsp_stapling_enabled = 1;
1598 return 1;
1599}
David Benjamin6c7aed02014-08-27 16:42:38 -04001600
Adam Langleyfcf25832014-12-18 17:42:32 -08001601void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
1602 size_t *out_len) {
1603 SSL_SESSION *session = ssl->session;
Adam Langley3cb50e02014-08-26 14:00:31 -07001604
Adam Langleyfcf25832014-12-18 17:42:32 -08001605 *out_len = 0;
1606 *out = NULL;
1607 if (ssl->server || !session || !session->tlsext_signed_cert_timestamp_list) {
1608 return;
1609 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001610
Adam Langleyfcf25832014-12-18 17:42:32 -08001611 *out = session->tlsext_signed_cert_timestamp_list;
1612 *out_len = session->tlsext_signed_cert_timestamp_list_length;
1613}
David Benjamin6c7aed02014-08-27 16:42:38 -04001614
Adam Langleyfcf25832014-12-18 17:42:32 -08001615void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
1616 size_t *out_len) {
1617 SSL_SESSION *session = ssl->session;
1618
1619 *out_len = 0;
1620 *out = NULL;
1621 if (ssl->server || !session || !session->ocsp_response) {
1622 return;
1623 }
1624 *out = session->ocsp_response;
1625 *out_len = session->ocsp_response_length;
1626}
David Benjamin6c7aed02014-08-27 16:42:38 -04001627
Paul Lietar4fac72e2015-09-09 13:44:55 +01001628int SSL_CTX_set_signed_cert_timestamp_list(SSL_CTX *ctx, const uint8_t *list,
1629 size_t list_len) {
1630 OPENSSL_free(ctx->signed_cert_timestamp_list);
1631 ctx->signed_cert_timestamp_list_length = 0;
1632
1633 ctx->signed_cert_timestamp_list = BUF_memdup(list, list_len);
1634 if (ctx->signed_cert_timestamp_list == NULL) {
1635 return 0;
1636 }
1637 ctx->signed_cert_timestamp_list_length = list_len;
1638
1639 return 1;
1640}
1641
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001642int SSL_CTX_set_ocsp_response(SSL_CTX *ctx, const uint8_t *response,
1643 size_t response_len) {
1644 OPENSSL_free(ctx->ocsp_response);
1645 ctx->ocsp_response_length = 0;
1646
1647 ctx->ocsp_response = BUF_memdup(response, response_len);
1648 if (ctx->ocsp_response == NULL) {
1649 return 0;
1650 }
1651 ctx->ocsp_response_length = response_len;
1652
1653 return 1;
1654}
1655
David Benjamin936aada2016-06-07 19:09:22 -04001656int SSL_set_tlsext_host_name(SSL *ssl, const char *name) {
1657 OPENSSL_free(ssl->tlsext_hostname);
1658 ssl->tlsext_hostname = NULL;
1659
1660 if (name == NULL) {
1661 return 1;
1662 }
1663
1664 size_t len = strlen(name);
1665 if (len == 0 || len > TLSEXT_MAXLEN_host_name) {
1666 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL3_EXT_INVALID_SERVERNAME);
1667 return 0;
1668 }
1669 ssl->tlsext_hostname = BUF_strdup(name);
1670 if (ssl->tlsext_hostname == NULL) {
1671 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1672 return 0;
1673 }
1674 return 1;
1675}
1676
1677int SSL_CTX_set_tlsext_servername_callback(
1678 SSL_CTX *ctx, int (*callback)(SSL *ssl, int *out_alert, void *arg)) {
1679 ctx->tlsext_servername_callback = callback;
1680 return 1;
1681}
1682
1683int SSL_CTX_set_tlsext_servername_arg(SSL_CTX *ctx, void *arg) {
1684 ctx->tlsext_servername_arg = arg;
1685 return 1;
1686}
1687
David Benjamin977547b2015-09-16 00:25:52 -04001688int SSL_select_next_proto(uint8_t **out, uint8_t *out_len,
1689 const uint8_t *server, unsigned server_len,
1690 const uint8_t *client, unsigned client_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001691 unsigned int i, j;
1692 const uint8_t *result;
1693 int status = OPENSSL_NPN_UNSUPPORTED;
Adam Langley95c29f32014-06-20 12:00:00 -07001694
Adam Langleyfcf25832014-12-18 17:42:32 -08001695 /* For each protocol in server preference order, see if we support it. */
1696 for (i = 0; i < server_len;) {
1697 for (j = 0; j < client_len;) {
1698 if (server[i] == client[j] &&
1699 memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
1700 /* We found a match */
1701 result = &server[i];
1702 status = OPENSSL_NPN_NEGOTIATED;
1703 goto found;
1704 }
1705 j += client[j];
1706 j++;
1707 }
1708 i += server[i];
1709 i++;
1710 }
Adam Langley95c29f32014-06-20 12:00:00 -07001711
Adam Langleyfcf25832014-12-18 17:42:32 -08001712 /* There's no overlap between our protocols and the server's list. */
1713 result = client;
1714 status = OPENSSL_NPN_NO_OVERLAP;
Adam Langley95c29f32014-06-20 12:00:00 -07001715
Adam Langleyfcf25832014-12-18 17:42:32 -08001716found:
1717 *out = (uint8_t *)result + 1;
David Benjamin977547b2015-09-16 00:25:52 -04001718 *out_len = result[0];
Adam Langleyfcf25832014-12-18 17:42:32 -08001719 return status;
1720}
Adam Langley95c29f32014-06-20 12:00:00 -07001721
David Benjamin977547b2015-09-16 00:25:52 -04001722void SSL_get0_next_proto_negotiated(const SSL *ssl, const uint8_t **out_data,
1723 unsigned *out_len) {
David Benjamin79978df2015-12-25 15:56:49 -05001724 *out_data = ssl->s3->next_proto_negotiated;
David Benjamin977547b2015-09-16 00:25:52 -04001725 if (*out_data == NULL) {
1726 *out_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001727 } else {
David Benjamin79978df2015-12-25 15:56:49 -05001728 *out_len = ssl->s3->next_proto_negotiated_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08001729 }
Adam Langley95c29f32014-06-20 12:00:00 -07001730}
1731
Adam Langleyfcf25832014-12-18 17:42:32 -08001732void SSL_CTX_set_next_protos_advertised_cb(
1733 SSL_CTX *ctx,
David Benjamin977547b2015-09-16 00:25:52 -04001734 int (*cb)(SSL *ssl, const uint8_t **out, unsigned *out_len, void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08001735 void *arg) {
1736 ctx->next_protos_advertised_cb = cb;
1737 ctx->next_protos_advertised_cb_arg = arg;
1738}
Adam Langley95c29f32014-06-20 12:00:00 -07001739
Adam Langleyfcf25832014-12-18 17:42:32 -08001740void SSL_CTX_set_next_proto_select_cb(
David Benjamin977547b2015-09-16 00:25:52 -04001741 SSL_CTX *ctx, int (*cb)(SSL *ssl, uint8_t **out, uint8_t *out_len,
1742 const uint8_t *in, unsigned in_len, void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08001743 void *arg) {
1744 ctx->next_proto_select_cb = cb;
1745 ctx->next_proto_select_cb_arg = arg;
1746}
Adam Langley95c29f32014-06-20 12:00:00 -07001747
Adam Langleyfcf25832014-12-18 17:42:32 -08001748int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
1749 unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001750 OPENSSL_free(ctx->alpn_client_proto_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001751 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1752 if (!ctx->alpn_client_proto_list) {
1753 return 1;
1754 }
1755 ctx->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001756
Adam Langleyfcf25832014-12-18 17:42:32 -08001757 return 0;
1758}
Adam Langley95c29f32014-06-20 12:00:00 -07001759
Adam Langleyfcf25832014-12-18 17:42:32 -08001760int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001761 OPENSSL_free(ssl->alpn_client_proto_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001762 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1763 if (!ssl->alpn_client_proto_list) {
1764 return 1;
1765 }
1766 ssl->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001767
Adam Langleyfcf25832014-12-18 17:42:32 -08001768 return 0;
1769}
Adam Langley95c29f32014-06-20 12:00:00 -07001770
Adam Langleyfcf25832014-12-18 17:42:32 -08001771void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
1772 int (*cb)(SSL *ssl, const uint8_t **out,
David Benjamin8984f1f2015-09-16 00:10:19 -04001773 uint8_t *out_len, const uint8_t *in,
1774 unsigned in_len, void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08001775 void *arg) {
1776 ctx->alpn_select_cb = cb;
1777 ctx->alpn_select_cb_arg = arg;
1778}
Adam Langley95c29f32014-06-20 12:00:00 -07001779
David Benjamin8984f1f2015-09-16 00:10:19 -04001780void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **out_data,
1781 unsigned *out_len) {
1782 *out_data = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08001783 if (ssl->s3) {
David Benjamin8984f1f2015-09-16 00:10:19 -04001784 *out_data = ssl->s3->alpn_selected;
Adam Langleyfcf25832014-12-18 17:42:32 -08001785 }
David Benjamin8984f1f2015-09-16 00:10:19 -04001786 if (*out_data == NULL) {
1787 *out_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001788 } else {
David Benjamin8984f1f2015-09-16 00:10:19 -04001789 *out_len = ssl->s3->alpn_selected_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08001790 }
1791}
Adam Langley95c29f32014-06-20 12:00:00 -07001792
David Benjamin936aada2016-06-07 19:09:22 -04001793
1794int SSL_CTX_enable_tls_channel_id(SSL_CTX *ctx) {
1795 ctx->tlsext_channel_id_enabled = 1;
1796 return 1;
1797}
1798
1799int SSL_enable_tls_channel_id(SSL *ssl) {
1800 ssl->tlsext_channel_id_enabled = 1;
1801 return 1;
1802}
1803
1804static int is_p256_key(EVP_PKEY *private_key) {
1805 const EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(private_key);
1806 return ec_key != NULL &&
1807 EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key)) ==
1808 NID_X9_62_prime256v1;
1809}
1810
1811int SSL_CTX_set1_tls_channel_id(SSL_CTX *ctx, EVP_PKEY *private_key) {
1812 if (!is_p256_key(private_key)) {
1813 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256);
1814 return 0;
1815 }
1816
1817 EVP_PKEY_free(ctx->tlsext_channel_id_private);
1818 ctx->tlsext_channel_id_private = EVP_PKEY_up_ref(private_key);
1819 ctx->tlsext_channel_id_enabled = 1;
1820
1821 return 1;
1822}
1823
1824int SSL_set1_tls_channel_id(SSL *ssl, EVP_PKEY *private_key) {
1825 if (!is_p256_key(private_key)) {
1826 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256);
1827 return 0;
1828 }
1829
1830 EVP_PKEY_free(ssl->tlsext_channel_id_private);
1831 ssl->tlsext_channel_id_private = EVP_PKEY_up_ref(private_key);
1832 ssl->tlsext_channel_id_enabled = 1;
1833
1834 return 1;
1835}
1836
1837size_t SSL_get_tls_channel_id(SSL *ssl, uint8_t *out, size_t max_out) {
1838 if (!ssl->s3->tlsext_channel_id_valid) {
1839 return 0;
1840 }
1841 memcpy(out, ssl->s3->tlsext_channel_id, (max_out < 64) ? max_out : 64);
1842 return 64;
1843}
1844
Adam Langleyfcf25832014-12-18 17:42:32 -08001845void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
David Benjamin59937042015-09-19 13:04:22 -04001846 int (*cb)(X509_STORE_CTX *store_ctx,
1847 void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08001848 void *arg) {
1849 ctx->app_verify_callback = cb;
1850 ctx->app_verify_arg = arg;
1851}
Adam Langley95c29f32014-06-20 12:00:00 -07001852
Adam Langleyfcf25832014-12-18 17:42:32 -08001853void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
1854 int (*cb)(int, X509_STORE_CTX *)) {
1855 ctx->verify_mode = mode;
1856 ctx->default_verify_callback = cb;
1857}
Adam Langley95c29f32014-06-20 12:00:00 -07001858
Adam Langleyfcf25832014-12-18 17:42:32 -08001859void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
1860 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
1861}
Adam Langley1258b6a2014-06-20 12:00:00 -07001862
David Benjamin7481d392015-07-05 19:38:46 -04001863void SSL_CTX_set_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl, void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08001864 void *arg) {
David Benjamin7481d392015-07-05 19:38:46 -04001865 ssl_cert_set_cert_cb(ctx->cert, cb, arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08001866}
David Benjamin859ec3c2014-09-02 16:29:36 -04001867
David Benjamin7481d392015-07-05 19:38:46 -04001868void SSL_set_cert_cb(SSL *ssl, int (*cb)(SSL *ssl, void *arg), void *arg) {
1869 ssl_cert_set_cert_cb(ssl->cert, cb, arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08001870}
Adam Langley95c29f32014-06-20 12:00:00 -07001871
David Benjamin936aada2016-06-07 19:09:22 -04001872size_t SSL_get0_certificate_types(SSL *ssl, const uint8_t **out_types) {
1873 if (ssl->server || !ssl->s3->tmp.cert_req) {
1874 *out_types = NULL;
1875 return 0;
1876 }
1877 *out_types = ssl->s3->tmp.certificate_types;
1878 return ssl->s3->tmp.num_certificate_types;
1879}
1880
David Benjamin0d56f882015-12-19 17:05:56 -05001881void ssl_get_compatible_server_ciphers(SSL *ssl, uint32_t *out_mask_k,
David Benjamin107db582015-04-08 00:41:59 -04001882 uint32_t *out_mask_a) {
David Benjamin83680502016-05-02 13:15:52 -04001883 uint32_t mask_k = 0;
1884 uint32_t mask_a = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001885
David Benjamin0d56f882015-12-19 17:05:56 -05001886 if (ssl->cert->x509 != NULL && ssl_has_private_key(ssl)) {
1887 if (ssl_private_key_type(ssl) == EVP_PKEY_RSA) {
David Benjamin83680502016-05-02 13:15:52 -04001888 mask_k |= SSL_kRSA;
1889 mask_a |= SSL_aRSA;
David Benjamin0d56f882015-12-19 17:05:56 -05001890 } else if (ssl_private_key_type(ssl) == EVP_PKEY_EC) {
David Benjamin83680502016-05-02 13:15:52 -04001891 /* An ECC certificate may be usable for ECDSA cipher suites depending on
Steven Valdezce902a92016-05-17 11:47:53 -04001892 * the key usage extension and on the client's group preferences. */
David Benjamin83680502016-05-02 13:15:52 -04001893 X509 *x = ssl->cert->x509;
1894 /* This call populates extension flags (ex_flags). */
1895 X509_check_purpose(x, -1, 0);
1896 int ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE)
1897 ? (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)
1898 : 1;
1899 if (ecdsa_ok && tls1_check_ec_cert(ssl, x)) {
1900 mask_a |= SSL_aECDSA;
1901 }
David Benjamind1d80782015-07-05 11:54:09 -04001902 }
1903 }
nagendra modadugu601448a2015-07-24 09:31:31 -07001904
David Benjamin83680502016-05-02 13:15:52 -04001905 if (ssl->cert->dh_tmp != NULL || ssl->cert->dh_tmp_cb != NULL) {
David Benjamin7061e282015-03-19 11:10:48 -04001906 mask_k |= SSL_kDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001907 }
Adam Langley95c29f32014-06-20 12:00:00 -07001908
Steven Valdezce902a92016-05-17 11:47:53 -04001909 /* Check for a shared group to consider ECDHE ciphers. */
David Benjamin4298d772015-12-19 00:18:25 -05001910 uint16_t unused;
Steven Valdezce902a92016-05-17 11:47:53 -04001911 if (tls1_get_shared_group(ssl, &unused)) {
David Benjamin7061e282015-03-19 11:10:48 -04001912 mask_k |= SSL_kECDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001913 }
Adam Langley95c29f32014-06-20 12:00:00 -07001914
Matt Braithwaite053931e2016-05-25 12:06:05 -07001915 /* CECPQ1 ciphers are always acceptable if supported by both sides. */
1916 mask_k |= SSL_kCECPQ1;
1917
Adam Langleyfcf25832014-12-18 17:42:32 -08001918 /* PSK requires a server callback. */
David Benjamin0d56f882015-12-19 17:05:56 -05001919 if (ssl->psk_server_callback != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001920 mask_k |= SSL_kPSK;
1921 mask_a |= SSL_aPSK;
1922 }
Adam Langley95c29f32014-06-20 12:00:00 -07001923
Adam Langleyfcf25832014-12-18 17:42:32 -08001924 *out_mask_k = mask_k;
1925 *out_mask_a = mask_a;
1926}
Adam Langley95c29f32014-06-20 12:00:00 -07001927
David Benjamin1269ddd2015-10-18 15:18:55 -04001928void ssl_update_cache(SSL *ssl, int mode) {
1929 SSL_CTX *ctx = ssl->initial_ctx;
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001930 /* Never cache sessions with empty session IDs. */
David Benjamin1269ddd2015-10-18 15:18:55 -04001931 if (ssl->session->session_id_length == 0 ||
1932 (ctx->session_cache_mode & mode) != mode) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001933 return;
1934 }
Adam Langley95c29f32014-06-20 12:00:00 -07001935
David Benjamin1269ddd2015-10-18 15:18:55 -04001936 /* Clients never use the internal session cache. */
1937 int use_internal_cache = ssl->server && !(ctx->session_cache_mode &
1938 SSL_SESS_CACHE_NO_INTERNAL_STORE);
David Benjamin95d31822015-06-15 19:53:32 -04001939
David Benjamin1269ddd2015-10-18 15:18:55 -04001940 /* A client may see new sessions on abbreviated handshakes if the server
1941 * decides to renew the ticket. Once the handshake is completed, it should be
1942 * inserted into the cache. */
1943 if (!ssl->hit || (!ssl->server && ssl->tlsext_ticket_expected)) {
1944 if (use_internal_cache) {
1945 SSL_CTX_add_session(ctx, ssl->session);
1946 }
1947 if (ctx->new_session_cb != NULL &&
1948 !ctx->new_session_cb(ssl, SSL_SESSION_up_ref(ssl->session))) {
1949 /* |new_session_cb|'s return value signals whether it took ownership. */
1950 SSL_SESSION_free(ssl->session);
Adam Langleyfcf25832014-12-18 17:42:32 -08001951 }
1952 }
Adam Langley95c29f32014-06-20 12:00:00 -07001953
David Benjamin1269ddd2015-10-18 15:18:55 -04001954 if (use_internal_cache &&
1955 !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR)) {
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001956 /* Automatically flush the internal session cache every 255 connections. */
1957 int flush_cache = 0;
Adam Langley4bdb6e42015-05-15 15:29:21 -07001958 CRYPTO_MUTEX_lock_write(&ctx->lock);
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001959 ctx->handshakes_since_cache_flush++;
1960 if (ctx->handshakes_since_cache_flush >= 255) {
1961 flush_cache = 1;
1962 ctx->handshakes_since_cache_flush = 0;
1963 }
David Benjamin29270de2016-05-24 15:28:36 +00001964 CRYPTO_MUTEX_unlock_write(&ctx->lock);
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001965
1966 if (flush_cache) {
1967 SSL_CTX_flush_sessions(ctx, (unsigned long)time(NULL));
Adam Langleyfcf25832014-12-18 17:42:32 -08001968 }
1969 }
1970}
Adam Langley95c29f32014-06-20 12:00:00 -07001971
Adam Langleyfcf25832014-12-18 17:42:32 -08001972static const char *ssl_get_version(int version) {
1973 switch (version) {
Steven Valdez4f94b1c2016-05-24 12:31:07 -04001974 case TLS1_3_VERSION:
1975 return "TLSv1.3";
1976
Adam Langleyfcf25832014-12-18 17:42:32 -08001977 case TLS1_2_VERSION:
1978 return "TLSv1.2";
Adam Langley95c29f32014-06-20 12:00:00 -07001979
Adam Langleyfcf25832014-12-18 17:42:32 -08001980 case TLS1_1_VERSION:
1981 return "TLSv1.1";
Adam Langley95c29f32014-06-20 12:00:00 -07001982
Adam Langleyfcf25832014-12-18 17:42:32 -08001983 case TLS1_VERSION:
1984 return "TLSv1";
Adam Langley95c29f32014-06-20 12:00:00 -07001985
Adam Langleyfcf25832014-12-18 17:42:32 -08001986 case SSL3_VERSION:
1987 return "SSLv3";
Adam Langley95c29f32014-06-20 12:00:00 -07001988
David Benjamin1c722b72015-04-20 13:53:10 -04001989 case DTLS1_VERSION:
1990 return "DTLSv1";
1991
1992 case DTLS1_2_VERSION:
1993 return "DTLSv1.2";
1994
Adam Langleyfcf25832014-12-18 17:42:32 -08001995 default:
1996 return "unknown";
1997 }
1998}
Adam Langley95c29f32014-06-20 12:00:00 -07001999
David Benjamin42fea372015-09-19 01:22:44 -04002000const char *SSL_get_version(const SSL *ssl) {
2001 return ssl_get_version(ssl->version);
Adam Langleyfcf25832014-12-18 17:42:32 -08002002}
Adam Langley95c29f32014-06-20 12:00:00 -07002003
David Benjamina6b8cdf2015-09-13 14:07:33 -04002004const char *SSL_SESSION_get_version(const SSL_SESSION *session) {
2005 return ssl_get_version(session->ssl_version);
Adam Langleyfcf25832014-12-18 17:42:32 -08002006}
Adam Langley95c29f32014-06-20 12:00:00 -07002007
David Benjamin0d56f882015-12-19 17:05:56 -05002008X509 *SSL_get_certificate(const SSL *ssl) {
2009 if (ssl->cert != NULL) {
2010 return ssl->cert->x509;
Adam Langleyfcf25832014-12-18 17:42:32 -08002011 }
2012
2013 return NULL;
2014}
2015
David Benjamin0d56f882015-12-19 17:05:56 -05002016EVP_PKEY *SSL_get_privatekey(const SSL *ssl) {
2017 if (ssl->cert != NULL) {
2018 return ssl->cert->privatekey;
Adam Langleyfcf25832014-12-18 17:42:32 -08002019 }
2020
2021 return NULL;
2022}
2023
2024X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
2025 if (ctx->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04002026 return ctx->cert->x509;
Adam Langleyfcf25832014-12-18 17:42:32 -08002027 }
2028
2029 return NULL;
2030}
2031
2032EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
2033 if (ctx->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04002034 return ctx->cert->privatekey;
Adam Langleyfcf25832014-12-18 17:42:32 -08002035 }
2036
2037 return NULL;
2038}
2039
David Benjamin42fea372015-09-19 01:22:44 -04002040const SSL_CIPHER *SSL_get_current_cipher(const SSL *ssl) {
David Benjamin79978df2015-12-25 15:56:49 -05002041 if (ssl->s3->aead_write_ctx == NULL) {
David Benjamina07c0fc2015-05-13 13:19:42 -04002042 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002043 }
David Benjamin79978df2015-12-25 15:56:49 -05002044 return ssl->s3->aead_write_ctx->cipher;
Adam Langleyfcf25832014-12-18 17:42:32 -08002045}
2046
David Benjamin936aada2016-06-07 19:09:22 -04002047int SSL_session_reused(const SSL *ssl) {
2048 return ssl->hit;
2049}
2050
David Benjamin0d56f882015-12-19 17:05:56 -05002051const COMP_METHOD *SSL_get_current_compression(SSL *ssl) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002052
David Benjamin0d56f882015-12-19 17:05:56 -05002053const COMP_METHOD *SSL_get_current_expansion(SSL *ssl) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002054
Adam Langley29ec5d12016-03-01 16:12:28 -08002055int *SSL_get_server_tmp_key(SSL *ssl, EVP_PKEY **out_key) { return 0; }
2056
David Benjamin30152fd2016-05-05 20:45:48 -04002057int ssl_is_wbio_buffered(const SSL *ssl) {
David Benjamin2f871122016-05-20 14:27:17 -04002058 return ssl->bbio != NULL;
David Benjamin30152fd2016-05-05 20:45:48 -04002059}
2060
David Benjaminb095f0f2016-05-05 21:50:24 -04002061int ssl_init_wbio_buffer(SSL *ssl) {
David Benjamin2f871122016-05-20 14:27:17 -04002062 if (ssl->bbio != NULL) {
2063 /* Already buffered. */
2064 assert(ssl->bbio == ssl->wbio);
2065 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002066 }
2067
David Benjamin2f871122016-05-20 14:27:17 -04002068 BIO *bbio = BIO_new(BIO_f_buffer());
2069 if (bbio == NULL ||
2070 !BIO_set_read_buffer_size(bbio, 1)) {
2071 BIO_free(bbio);
Adam Langleyfcf25832014-12-18 17:42:32 -08002072 return 0;
2073 }
2074
David Benjamin2f871122016-05-20 14:27:17 -04002075 ssl->bbio = bbio;
2076 ssl->wbio = BIO_push(bbio, ssl->wbio);
Adam Langleyfcf25832014-12-18 17:42:32 -08002077 return 1;
2078}
2079
David Benjamin0d56f882015-12-19 17:05:56 -05002080void ssl_free_wbio_buffer(SSL *ssl) {
2081 if (ssl->bbio == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002082 return;
2083 }
2084
David Benjamin2f871122016-05-20 14:27:17 -04002085 assert(ssl->bbio == ssl->wbio);
Adam Langleyfcf25832014-12-18 17:42:32 -08002086
David Benjamin2f871122016-05-20 14:27:17 -04002087 ssl->wbio = BIO_pop(ssl->wbio);
David Benjamin0d56f882015-12-19 17:05:56 -05002088 BIO_free(ssl->bbio);
2089 ssl->bbio = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002090}
2091
2092void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
Adam Langleybb85f3d2015-10-28 18:44:11 -07002093 ctx->quiet_shutdown = (mode != 0);
Adam Langleyfcf25832014-12-18 17:42:32 -08002094}
2095
2096int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
2097 return ctx->quiet_shutdown;
2098}
2099
Adam Langleybb85f3d2015-10-28 18:44:11 -07002100void SSL_set_quiet_shutdown(SSL *ssl, int mode) {
2101 ssl->quiet_shutdown = (mode != 0);
2102}
Adam Langleyfcf25832014-12-18 17:42:32 -08002103
David Benjamin9f859492015-10-03 10:44:30 -04002104int SSL_get_quiet_shutdown(const SSL *ssl) { return ssl->quiet_shutdown; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002105
David Benjamin63006a92015-10-18 00:00:06 -04002106void SSL_set_shutdown(SSL *ssl, int mode) {
2107 /* It is an error to clear any bits that have already been set. (We can't try
2108 * to get a second close_notify or send two.) */
David Benjaminfa214e42016-05-10 17:03:10 -04002109 assert((SSL_get_shutdown(ssl) & mode) == SSL_get_shutdown(ssl));
David Benjamin63006a92015-10-18 00:00:06 -04002110
David Benjaminfa214e42016-05-10 17:03:10 -04002111 if (mode & SSL_RECEIVED_SHUTDOWN &&
2112 ssl->s3->recv_shutdown == ssl_shutdown_none) {
2113 ssl->s3->recv_shutdown = ssl_shutdown_close_notify;
2114 }
2115
2116 if (mode & SSL_SENT_SHUTDOWN &&
2117 ssl->s3->send_shutdown == ssl_shutdown_none) {
2118 ssl->s3->send_shutdown = ssl_shutdown_close_notify;
2119 }
David Benjamin63006a92015-10-18 00:00:06 -04002120}
Adam Langleyfcf25832014-12-18 17:42:32 -08002121
David Benjaminfa214e42016-05-10 17:03:10 -04002122int SSL_get_shutdown(const SSL *ssl) {
2123 int ret = 0;
2124 if (ssl->s3->recv_shutdown != ssl_shutdown_none) {
2125 /* Historically, OpenSSL set |SSL_RECEIVED_SHUTDOWN| on both close_notify
2126 * and fatal alert. */
2127 ret |= SSL_RECEIVED_SHUTDOWN;
2128 }
2129 if (ssl->s3->send_shutdown == ssl_shutdown_close_notify) {
2130 /* Historically, OpenSSL set |SSL_SENT_SHUTDOWN| on only close_notify. */
2131 ret |= SSL_SENT_SHUTDOWN;
2132 }
2133 return ret;
2134}
Adam Langleyfcf25832014-12-18 17:42:32 -08002135
David Benjamin9f859492015-10-03 10:44:30 -04002136int SSL_version(const SSL *ssl) { return ssl->version; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002137
2138SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
2139
2140SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
2141 if (ssl->ctx == ctx) {
2142 return ssl->ctx;
2143 }
2144
2145 if (ctx == NULL) {
2146 ctx = ssl->initial_ctx;
2147 }
2148
David Benjamin2755a3e2015-04-22 16:17:58 -04002149 ssl_cert_free(ssl->cert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002150 ssl->cert = ssl_cert_dup(ctx->cert);
David Benjamin2755a3e2015-04-22 16:17:58 -04002151
Adam Langley0b5e3902015-05-15 13:08:38 -07002152 CRYPTO_refcount_inc(&ctx->references);
David Benjamin2755a3e2015-04-22 16:17:58 -04002153 SSL_CTX_free(ssl->ctx); /* decrement reference count */
Adam Langleyfcf25832014-12-18 17:42:32 -08002154 ssl->ctx = ctx;
2155
2156 ssl->sid_ctx_length = ctx->sid_ctx_length;
2157 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2158 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2159
2160 return ssl->ctx;
2161}
2162
2163int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
2164 return X509_STORE_set_default_paths(ctx->cert_store);
2165}
Adam Langley95c29f32014-06-20 12:00:00 -07002166
David Benjamin59937042015-09-19 13:04:22 -04002167int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *ca_file,
2168 const char *ca_dir) {
2169 return X509_STORE_load_locations(ctx->cert_store, ca_file, ca_dir);
Adam Langleyfcf25832014-12-18 17:42:32 -08002170}
Adam Langley95c29f32014-06-20 12:00:00 -07002171
2172void SSL_set_info_callback(SSL *ssl,
David Benjamin82170242015-10-17 22:51:17 -04002173 void (*cb)(const SSL *ssl, int type, int value)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002174 ssl->info_callback = cb;
2175}
Adam Langley95c29f32014-06-20 12:00:00 -07002176
David Benjamin82170242015-10-17 22:51:17 -04002177void (*SSL_get_info_callback(const SSL *ssl))(const SSL *ssl, int type,
2178 int value) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002179 return ssl->info_callback;
2180}
Adam Langley95c29f32014-06-20 12:00:00 -07002181
Adam Langleyfcf25832014-12-18 17:42:32 -08002182int SSL_state(const SSL *ssl) { return ssl->state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002183
David Benjaminece089c2015-05-15 23:52:42 -04002184void SSL_set_state(SSL *ssl, int state) { }
Adam Langley95c29f32014-06-20 12:00:00 -07002185
Emily Stark95a79ee2016-02-25 21:12:28 -08002186char *SSL_get_shared_ciphers(const SSL *ssl, char *buf, int len) {
2187 if (len <= 0) {
2188 return NULL;
2189 }
2190 buf[0] = '\0';
2191 return buf;
2192}
2193
David Benjamin59937042015-09-19 13:04:22 -04002194void SSL_set_verify_result(SSL *ssl, long result) {
2195 ssl->verify_result = result;
2196}
Adam Langley95c29f32014-06-20 12:00:00 -07002197
Adam Langleyfcf25832014-12-18 17:42:32 -08002198long SSL_get_verify_result(const SSL *ssl) { return ssl->verify_result; }
Adam Langley95c29f32014-06-20 12:00:00 -07002199
David Benjamin8a589332015-12-04 23:14:35 -05002200int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
Adam Langleyfcf25832014-12-18 17:42:32 -08002201 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002202 int index;
2203 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp,
David Benjamin8a589332015-12-04 23:14:35 -05002204 dup_func, free_func)) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002205 return -1;
2206 }
2207 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002208}
Adam Langley95c29f32014-06-20 12:00:00 -07002209
David Benjamin1ac08fe2015-09-15 01:36:22 -04002210int SSL_set_ex_data(SSL *ssl, int idx, void *arg) {
2211 return CRYPTO_set_ex_data(&ssl->ex_data, idx, arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002212}
Adam Langley95c29f32014-06-20 12:00:00 -07002213
David Benjamin1ac08fe2015-09-15 01:36:22 -04002214void *SSL_get_ex_data(const SSL *ssl, int idx) {
2215 return CRYPTO_get_ex_data(&ssl->ex_data, idx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002216}
Adam Langley95c29f32014-06-20 12:00:00 -07002217
David Benjamin8a589332015-12-04 23:14:35 -05002218int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
Adam Langleyfcf25832014-12-18 17:42:32 -08002219 CRYPTO_EX_dup *dup_func,
2220 CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002221 int index;
2222 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp,
David Benjamin8a589332015-12-04 23:14:35 -05002223 dup_func, free_func)) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002224 return -1;
2225 }
2226 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002227}
Adam Langley95c29f32014-06-20 12:00:00 -07002228
David Benjamin1ac08fe2015-09-15 01:36:22 -04002229int SSL_CTX_set_ex_data(SSL_CTX *ctx, int idx, void *arg) {
2230 return CRYPTO_set_ex_data(&ctx->ex_data, idx, arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002231}
Adam Langley95c29f32014-06-20 12:00:00 -07002232
David Benjamin1ac08fe2015-09-15 01:36:22 -04002233void *SSL_CTX_get_ex_data(const SSL_CTX *ctx, int idx) {
2234 return CRYPTO_get_ex_data(&ctx->ex_data, idx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002235}
Adam Langley95c29f32014-06-20 12:00:00 -07002236
Adam Langleyfcf25832014-12-18 17:42:32 -08002237X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
2238 return ctx->cert_store;
2239}
Adam Langley95c29f32014-06-20 12:00:00 -07002240
Adam Langleyfcf25832014-12-18 17:42:32 -08002241void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
David Benjamin2755a3e2015-04-22 16:17:58 -04002242 X509_STORE_free(ctx->cert_store);
Adam Langleyfcf25832014-12-18 17:42:32 -08002243 ctx->cert_store = store;
2244}
Adam Langley95c29f32014-06-20 12:00:00 -07002245
David Benjamin93d17492015-10-17 12:43:36 -04002246int SSL_want(const SSL *ssl) { return ssl->rwstate; }
Adam Langley95c29f32014-06-20 12:00:00 -07002247
Adam Langleyfcf25832014-12-18 17:42:32 -08002248void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
2249 RSA *(*cb)(SSL *ssl, int is_export,
2250 int keylength)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002251}
Adam Langley95c29f32014-06-20 12:00:00 -07002252
Adam Langleyfcf25832014-12-18 17:42:32 -08002253void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
2254 int keylength)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002255}
2256
2257void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
David Benjamin59015c32015-04-26 13:13:55 -04002258 DH *(*callback)(SSL *ssl, int is_export,
2259 int keylength)) {
2260 ctx->cert->dh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002261}
2262
David Benjamin59015c32015-04-26 13:13:55 -04002263void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*callback)(SSL *ssl, int is_export,
2264 int keylength)) {
2265 ssl->cert->dh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002266}
2267
Adam Langleyfcf25832014-12-18 17:42:32 -08002268int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
2269 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
David Benjamin3570d732015-06-29 00:28:17 -04002270 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -08002271 return 0;
2272 }
2273
David Benjamin2755a3e2015-04-22 16:17:58 -04002274 OPENSSL_free(ctx->psk_identity_hint);
Adam Langleyfcf25832014-12-18 17:42:32 -08002275
2276 if (identity_hint != NULL) {
2277 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2278 if (ctx->psk_identity_hint == NULL) {
2279 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002280 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002281 } else {
2282 ctx->psk_identity_hint = NULL;
2283 }
Adam Langley95c29f32014-06-20 12:00:00 -07002284
Adam Langleyfcf25832014-12-18 17:42:32 -08002285 return 1;
2286}
2287
David Benjamine8814df2015-09-15 08:05:54 -04002288int SSL_use_psk_identity_hint(SSL *ssl, const char *identity_hint) {
2289 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002290 return 0;
2291 }
2292
2293 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
David Benjamin3570d732015-06-29 00:28:17 -04002294 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -08002295 return 0;
2296 }
2297
2298 /* Clear currently configured hint, if any. */
David Benjamine8814df2015-09-15 08:05:54 -04002299 OPENSSL_free(ssl->psk_identity_hint);
2300 ssl->psk_identity_hint = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002301
2302 if (identity_hint != NULL) {
David Benjamine8814df2015-09-15 08:05:54 -04002303 ssl->psk_identity_hint = BUF_strdup(identity_hint);
2304 if (ssl->psk_identity_hint == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002305 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002306 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002307 }
Adam Langley95c29f32014-06-20 12:00:00 -07002308
Adam Langleyfcf25832014-12-18 17:42:32 -08002309 return 1;
2310}
Adam Langley95c29f32014-06-20 12:00:00 -07002311
David Benjamine8814df2015-09-15 08:05:54 -04002312const char *SSL_get_psk_identity_hint(const SSL *ssl) {
2313 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002314 return NULL;
2315 }
David Benjamine8814df2015-09-15 08:05:54 -04002316 return ssl->psk_identity_hint;
Adam Langleyfcf25832014-12-18 17:42:32 -08002317}
Adam Langley95c29f32014-06-20 12:00:00 -07002318
David Benjamine8814df2015-09-15 08:05:54 -04002319const char *SSL_get_psk_identity(const SSL *ssl) {
2320 if (ssl == NULL || ssl->session == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002321 return NULL;
2322 }
Adam Langley95c29f32014-06-20 12:00:00 -07002323
David Benjamine8814df2015-09-15 08:05:54 -04002324 return ssl->session->psk_identity;
Adam Langleyfcf25832014-12-18 17:42:32 -08002325}
Adam Langley95c29f32014-06-20 12:00:00 -07002326
Adam Langleyfcf25832014-12-18 17:42:32 -08002327void SSL_set_psk_client_callback(
David Benjamine8814df2015-09-15 08:05:54 -04002328 SSL *ssl, unsigned (*cb)(SSL *ssl, const char *hint, char *identity,
2329 unsigned max_identity_len, uint8_t *psk,
2330 unsigned max_psk_len)) {
2331 ssl->psk_client_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002332}
Adam Langley95c29f32014-06-20 12:00:00 -07002333
Adam Langleyfcf25832014-12-18 17:42:32 -08002334void SSL_CTX_set_psk_client_callback(
David Benjamine8814df2015-09-15 08:05:54 -04002335 SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *hint, char *identity,
2336 unsigned max_identity_len, uint8_t *psk,
2337 unsigned max_psk_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002338 ctx->psk_client_callback = cb;
2339}
Adam Langley95c29f32014-06-20 12:00:00 -07002340
Adam Langleyfcf25832014-12-18 17:42:32 -08002341void SSL_set_psk_server_callback(
David Benjamine8814df2015-09-15 08:05:54 -04002342 SSL *ssl, unsigned (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
2343 unsigned max_psk_len)) {
2344 ssl->psk_server_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002345}
Adam Langley95c29f32014-06-20 12:00:00 -07002346
Adam Langleyfcf25832014-12-18 17:42:32 -08002347void SSL_CTX_set_psk_server_callback(
David Benjamine8814df2015-09-15 08:05:54 -04002348 SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *identity,
2349 uint8_t *psk, unsigned max_psk_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002350 ctx->psk_server_callback = cb;
2351}
Adam Langley95c29f32014-06-20 12:00:00 -07002352
Adam Langleyfcf25832014-12-18 17:42:32 -08002353void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
2354 void (*cb)(int write_p, int version,
2355 int content_type, const void *buf,
2356 size_t len, SSL *ssl, void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002357 ctx->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002358}
David Benjamin61ecccf2015-05-05 09:44:51 -04002359
2360void SSL_CTX_set_msg_callback_arg(SSL_CTX *ctx, void *arg) {
2361 ctx->msg_callback_arg = arg;
2362}
2363
Adam Langleyfcf25832014-12-18 17:42:32 -08002364void SSL_set_msg_callback(SSL *ssl,
2365 void (*cb)(int write_p, int version, int content_type,
2366 const void *buf, size_t len, SSL *ssl,
2367 void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002368 ssl->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002369}
Adam Langley95c29f32014-06-20 12:00:00 -07002370
David Benjamin61ecccf2015-05-05 09:44:51 -04002371void SSL_set_msg_callback_arg(SSL *ssl, void *arg) {
2372 ssl->msg_callback_arg = arg;
2373}
2374
David Benjamind28f59c2015-11-17 22:32:50 -05002375void SSL_CTX_set_keylog_callback(SSL_CTX *ctx,
2376 void (*cb)(const SSL *ssl, const char *line)) {
2377 ctx->keylog_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002378}
Adam Langley95c29f32014-06-20 12:00:00 -07002379
Taylor Brandstetter9edb2c62016-06-08 15:26:59 -07002380void SSL_CTX_set_current_time_cb(SSL_CTX *ctx,
2381 void (*cb)(const SSL *ssl,
2382 struct timeval *out_clock)) {
2383 ctx->current_time_cb = cb;
2384}
2385
Adam Langleyfcf25832014-12-18 17:42:32 -08002386static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
2387 static const char hextable[] = "0123456789abcdef";
2388 uint8_t *out;
2389 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002390
Adam Langleyfcf25832014-12-18 17:42:32 -08002391 if (!CBB_add_space(cbb, &out, in_len * 2)) {
2392 return 0;
2393 }
Adam Langley95c29f32014-06-20 12:00:00 -07002394
Adam Langleyfcf25832014-12-18 17:42:32 -08002395 for (i = 0; i < in_len; i++) {
2396 *(out++) = (uint8_t)hextable[in[i] >> 4];
2397 *(out++) = (uint8_t)hextable[in[i] & 0xf];
2398 }
Adam Langley95c29f32014-06-20 12:00:00 -07002399
Adam Langleyfcf25832014-12-18 17:42:32 -08002400 return 1;
2401}
David Benjamin859ec3c2014-09-02 16:29:36 -04002402
David Benjamind28f59c2015-11-17 22:32:50 -05002403int ssl_log_rsa_client_key_exchange(const SSL *ssl,
2404 const uint8_t *encrypted_premaster,
2405 size_t encrypted_premaster_len,
2406 const uint8_t *premaster,
2407 size_t premaster_len) {
2408 if (ssl->ctx->keylog_callback == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002409 return 1;
2410 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002411
Adam Langleyfcf25832014-12-18 17:42:32 -08002412 if (encrypted_premaster_len < 8) {
David Benjamin3570d732015-06-29 00:28:17 -04002413 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08002414 return 0;
2415 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002416
David Benjamind28f59c2015-11-17 22:32:50 -05002417 CBB cbb;
2418 uint8_t *out;
2419 size_t out_len;
David Benjamina8653202015-06-28 01:26:10 -04002420 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len * 2 + 1) ||
2421 !CBB_add_bytes(&cbb, (const uint8_t *)"RSA ", 4) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08002422 /* Only the first 8 bytes of the encrypted premaster secret are
2423 * logged. */
2424 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
2425 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2426 !cbb_add_hex(&cbb, premaster, premaster_len) ||
David Benjamind28f59c2015-11-17 22:32:50 -05002427 !CBB_add_u8(&cbb, 0 /* NUL */) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08002428 !CBB_finish(&cbb, &out, &out_len)) {
2429 CBB_cleanup(&cbb);
2430 return 0;
2431 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002432
David Benjamind28f59c2015-11-17 22:32:50 -05002433 ssl->ctx->keylog_callback(ssl, (const char *)out);
Adam Langleyfcf25832014-12-18 17:42:32 -08002434 OPENSSL_free(out);
David Benjamind28f59c2015-11-17 22:32:50 -05002435 return 1;
Adam Langley95f22882014-06-20 12:00:00 -07002436}
2437
David Benjamind28f59c2015-11-17 22:32:50 -05002438int ssl_log_master_secret(const SSL *ssl, const uint8_t *client_random,
2439 size_t client_random_len, const uint8_t *master,
2440 size_t master_len) {
2441 if (ssl->ctx->keylog_callback == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002442 return 1;
2443 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002444
Adam Langleyfcf25832014-12-18 17:42:32 -08002445 if (client_random_len != 32) {
David Benjamin3570d732015-06-29 00:28:17 -04002446 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08002447 return 0;
2448 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002449
David Benjamind28f59c2015-11-17 22:32:50 -05002450 CBB cbb;
2451 uint8_t *out;
2452 size_t out_len;
David Benjamina8653202015-06-28 01:26:10 -04002453 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len * 2 + 1) ||
2454 !CBB_add_bytes(&cbb, (const uint8_t *)"CLIENT_RANDOM ", 14) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08002455 !cbb_add_hex(&cbb, client_random, 32) ||
2456 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2457 !cbb_add_hex(&cbb, master, master_len) ||
David Benjamind28f59c2015-11-17 22:32:50 -05002458 !CBB_add_u8(&cbb, 0 /* NUL */) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08002459 !CBB_finish(&cbb, &out, &out_len)) {
2460 CBB_cleanup(&cbb);
2461 return 0;
2462 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002463
David Benjamind28f59c2015-11-17 22:32:50 -05002464 ssl->ctx->keylog_callback(ssl, (const char *)out);
Adam Langleyfcf25832014-12-18 17:42:32 -08002465 OPENSSL_free(out);
David Benjamind28f59c2015-11-17 22:32:50 -05002466 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002467}
2468
David Benjamin5d8b1282015-10-17 23:26:35 -04002469int SSL_is_init_finished(const SSL *ssl) {
2470 return ssl->state == SSL_ST_OK;
2471}
2472
2473int SSL_in_init(const SSL *ssl) {
2474 return (ssl->state & SSL_ST_INIT) != 0;
2475}
2476
2477int SSL_in_false_start(const SSL *ssl) {
2478 return ssl->s3->tmp.in_false_start;
David Benjamined7c4752015-02-16 19:16:46 -05002479}
2480
David Benjamin0d56f882015-12-19 17:05:56 -05002481int SSL_cutthrough_complete(const SSL *ssl) {
2482 return SSL_in_false_start(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08002483}
Adam Langley95c29f32014-06-20 12:00:00 -07002484
Adam Langleyfcf25832014-12-18 17:42:32 -08002485void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
2486 size_t *ssl_session_size) {
2487 *ssl_size = sizeof(SSL);
2488 *ssl_ctx_size = sizeof(SSL_CTX);
2489 *ssl_session_size = sizeof(SSL_SESSION);
2490}
Feng Lu41aa3252014-11-21 22:47:56 -08002491
David Benjamin0d56f882015-12-19 17:05:56 -05002492int ssl3_can_false_start(const SSL *ssl) {
2493 const SSL_CIPHER *const cipher = SSL_get_current_cipher(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08002494
Adam Langleye631d962015-04-01 13:11:01 -07002495 /* False Start only for TLS 1.2 with an ECDHE+AEAD cipher and ALPN or NPN. */
David Benjamin0d56f882015-12-19 17:05:56 -05002496 return !SSL_IS_DTLS(ssl) &&
Steven Valdez4f94b1c2016-05-24 12:31:07 -04002497 SSL_version(ssl) == TLS1_2_VERSION &&
David Benjamin0d56f882015-12-19 17:05:56 -05002498 (ssl->s3->alpn_selected || ssl->s3->next_proto_neg_seen) &&
David Benjamin195dc782015-02-19 13:27:05 -05002499 cipher != NULL &&
Adam Langleye631d962015-04-01 13:11:01 -07002500 cipher->algorithm_mkey == SSL_kECDHE &&
David Benjamin13414b32015-12-09 23:02:39 -05002501 cipher->algorithm_mac == SSL_AEAD;
Adam Langleyfcf25832014-12-18 17:42:32 -08002502}
2503
2504const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version) {
2505 switch (version) {
2506 case SSL3_VERSION:
2507 return &SSLv3_enc_data;
2508
2509 case TLS1_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002510 case TLS1_1_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002511 case TLS1_2_VERSION:
Steven Valdez4f94b1c2016-05-24 12:31:07 -04002512 case TLS1_3_VERSION:
David Benjamina1e9cab2015-12-30 00:08:49 -05002513 case DTLS1_VERSION:
2514 case DTLS1_2_VERSION:
2515 return &TLSv1_enc_data;
Adam Langleyfcf25832014-12-18 17:42:32 -08002516
Adam Langleyfcf25832014-12-18 17:42:32 -08002517 default:
2518 return NULL;
2519 }
2520}
2521
David Benjamin0d56f882015-12-19 17:05:56 -05002522uint16_t ssl3_get_max_server_version(const SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002523 uint16_t max_version;
2524
David Benjamin0d56f882015-12-19 17:05:56 -05002525 if (SSL_IS_DTLS(ssl)) {
2526 max_version = (ssl->max_version != 0) ? ssl->max_version : DTLS1_2_VERSION;
2527 if (!(ssl->options & SSL_OP_NO_DTLSv1_2) &&
2528 DTLS1_2_VERSION >= max_version) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002529 return DTLS1_2_VERSION;
2530 }
David Benjamin0d56f882015-12-19 17:05:56 -05002531 if (!(ssl->options & SSL_OP_NO_DTLSv1) && DTLS1_VERSION >= max_version) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002532 return DTLS1_VERSION;
2533 }
2534 return 0;
2535 }
2536
Steven Valdez4f94b1c2016-05-24 12:31:07 -04002537 max_version = (ssl->max_version != 0) ? ssl->max_version : TLS1_3_VERSION;
2538 if (!(ssl->options & SSL_OP_NO_TLSv1_3) && TLS1_3_VERSION <= max_version) {
2539 return TLS1_3_VERSION;
2540 }
David Benjamin0d56f882015-12-19 17:05:56 -05002541 if (!(ssl->options & SSL_OP_NO_TLSv1_2) && TLS1_2_VERSION <= max_version) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002542 return TLS1_2_VERSION;
2543 }
David Benjamin0d56f882015-12-19 17:05:56 -05002544 if (!(ssl->options & SSL_OP_NO_TLSv1_1) && TLS1_1_VERSION <= max_version) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002545 return TLS1_1_VERSION;
2546 }
David Benjamin0d56f882015-12-19 17:05:56 -05002547 if (!(ssl->options & SSL_OP_NO_TLSv1) && TLS1_VERSION <= max_version) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002548 return TLS1_VERSION;
2549 }
David Benjamin0d56f882015-12-19 17:05:56 -05002550 if (!(ssl->options & SSL_OP_NO_SSLv3) && SSL3_VERSION <= max_version) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002551 return SSL3_VERSION;
2552 }
2553 return 0;
2554}
2555
David Benjamin0d56f882015-12-19 17:05:56 -05002556uint16_t ssl3_get_mutual_version(SSL *ssl, uint16_t client_version) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002557 uint16_t version = 0;
2558
David Benjamin0d56f882015-12-19 17:05:56 -05002559 if (SSL_IS_DTLS(ssl)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002560 /* Clamp client_version to max_version. */
David Benjamin0d56f882015-12-19 17:05:56 -05002561 if (ssl->max_version != 0 && client_version < ssl->max_version) {
2562 client_version = ssl->max_version;
Adam Langleyfcf25832014-12-18 17:42:32 -08002563 }
2564
David Benjamin0d56f882015-12-19 17:05:56 -05002565 if (client_version <= DTLS1_2_VERSION &&
2566 !(ssl->options & SSL_OP_NO_DTLSv1_2)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002567 version = DTLS1_2_VERSION;
2568 } else if (client_version <= DTLS1_VERSION &&
David Benjamin0d56f882015-12-19 17:05:56 -05002569 !(ssl->options & SSL_OP_NO_DTLSv1)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002570 version = DTLS1_VERSION;
2571 }
2572
2573 /* Check against min_version. */
David Benjamin0d56f882015-12-19 17:05:56 -05002574 if (version != 0 && ssl->min_version != 0 && version > ssl->min_version) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002575 return 0;
2576 }
2577 return version;
2578 } else {
2579 /* Clamp client_version to max_version. */
David Benjamin0d56f882015-12-19 17:05:56 -05002580 if (ssl->max_version != 0 && client_version > ssl->max_version) {
2581 client_version = ssl->max_version;
Adam Langleyfcf25832014-12-18 17:42:32 -08002582 }
2583
Steven Valdez4f94b1c2016-05-24 12:31:07 -04002584 if (client_version >= TLS1_3_VERSION &&
2585 !(ssl->options & SSL_OP_NO_TLSv1_3)) {
2586 version = TLS1_3_VERSION;
2587 } else if (client_version >= TLS1_2_VERSION &&
2588 !(ssl->options & SSL_OP_NO_TLSv1_2)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002589 version = TLS1_2_VERSION;
2590 } else if (client_version >= TLS1_1_VERSION &&
David Benjamin0d56f882015-12-19 17:05:56 -05002591 !(ssl->options & SSL_OP_NO_TLSv1_1)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002592 version = TLS1_1_VERSION;
David Benjamin0d56f882015-12-19 17:05:56 -05002593 } else if (client_version >= TLS1_VERSION &&
2594 !(ssl->options & SSL_OP_NO_TLSv1)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002595 version = TLS1_VERSION;
David Benjamin0d56f882015-12-19 17:05:56 -05002596 } else if (client_version >= SSL3_VERSION &&
2597 !(ssl->options & SSL_OP_NO_SSLv3)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002598 version = SSL3_VERSION;
2599 }
2600
2601 /* Check against min_version. */
David Benjamin0d56f882015-12-19 17:05:56 -05002602 if (version != 0 && ssl->min_version != 0 && version < ssl->min_version) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002603 return 0;
2604 }
2605 return version;
2606 }
2607}
2608
David Benjamin0d56f882015-12-19 17:05:56 -05002609uint16_t ssl3_get_max_client_version(SSL *ssl) {
2610 uint32_t options = ssl->options;
Adam Langleyfcf25832014-12-18 17:42:32 -08002611 uint16_t version = 0;
2612
2613 /* OpenSSL's API for controlling versions entails blacklisting individual
2614 * protocols. This has two problems. First, on the client, the protocol can
2615 * only express a contiguous range of versions. Second, a library consumer
2616 * trying to set a maximum version cannot disable protocol versions that get
2617 * added in a future version of the library.
2618 *
2619 * To account for both of these, OpenSSL interprets the client-side bitmask
2620 * as a min/max range by picking the lowest contiguous non-empty range of
2621 * enabled protocols. Note that this means it is impossible to set a maximum
2622 * version of TLS 1.2 in a future-proof way.
2623 *
2624 * By this scheme, the maximum version is the lowest version V such that V is
2625 * enabled and V+1 is disabled or unimplemented. */
David Benjamin0d56f882015-12-19 17:05:56 -05002626 if (SSL_IS_DTLS(ssl)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002627 if (!(options & SSL_OP_NO_DTLSv1_2)) {
2628 version = DTLS1_2_VERSION;
2629 }
2630 if (!(options & SSL_OP_NO_DTLSv1) && (options & SSL_OP_NO_DTLSv1_2)) {
2631 version = DTLS1_VERSION;
2632 }
David Benjamin0d56f882015-12-19 17:05:56 -05002633 if (ssl->max_version != 0 && version < ssl->max_version) {
2634 version = ssl->max_version;
Adam Langleyfcf25832014-12-18 17:42:32 -08002635 }
2636 } else {
Steven Valdez4f94b1c2016-05-24 12:31:07 -04002637 if (!(options & SSL_OP_NO_TLSv1_3)) {
2638 version = TLS1_3_VERSION;
2639 }
2640 if (!(options & SSL_OP_NO_TLSv1_2) && (options & SSL_OP_NO_TLSv1_3)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002641 version = TLS1_2_VERSION;
2642 }
2643 if (!(options & SSL_OP_NO_TLSv1_1) && (options & SSL_OP_NO_TLSv1_2)) {
2644 version = TLS1_1_VERSION;
2645 }
2646 if (!(options & SSL_OP_NO_TLSv1) && (options & SSL_OP_NO_TLSv1_1)) {
2647 version = TLS1_VERSION;
2648 }
2649 if (!(options & SSL_OP_NO_SSLv3) && (options & SSL_OP_NO_TLSv1)) {
2650 version = SSL3_VERSION;
2651 }
David Benjamin0d56f882015-12-19 17:05:56 -05002652 if (ssl->max_version != 0 && version > ssl->max_version) {
2653 version = ssl->max_version;
Adam Langleyfcf25832014-12-18 17:42:32 -08002654 }
2655 }
2656
2657 return version;
2658}
2659
David Benjamin0d56f882015-12-19 17:05:56 -05002660int ssl3_is_version_enabled(SSL *ssl, uint16_t version) {
2661 if (SSL_IS_DTLS(ssl)) {
2662 if (ssl->max_version != 0 && version < ssl->max_version) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002663 return 0;
2664 }
David Benjamin0d56f882015-12-19 17:05:56 -05002665 if (ssl->min_version != 0 && version > ssl->min_version) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002666 return 0;
2667 }
2668
2669 switch (version) {
2670 case DTLS1_VERSION:
David Benjamin0d56f882015-12-19 17:05:56 -05002671 return !(ssl->options & SSL_OP_NO_DTLSv1);
Adam Langleyfcf25832014-12-18 17:42:32 -08002672
2673 case DTLS1_2_VERSION:
David Benjamin0d56f882015-12-19 17:05:56 -05002674 return !(ssl->options & SSL_OP_NO_DTLSv1_2);
Adam Langleyfcf25832014-12-18 17:42:32 -08002675
2676 default:
2677 return 0;
2678 }
2679 } else {
David Benjamin0d56f882015-12-19 17:05:56 -05002680 if (ssl->max_version != 0 && version > ssl->max_version) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002681 return 0;
2682 }
David Benjamin0d56f882015-12-19 17:05:56 -05002683 if (ssl->min_version != 0 && version < ssl->min_version) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002684 return 0;
2685 }
2686
2687 switch (version) {
2688 case SSL3_VERSION:
David Benjamin0d56f882015-12-19 17:05:56 -05002689 return !(ssl->options & SSL_OP_NO_SSLv3);
Adam Langleyfcf25832014-12-18 17:42:32 -08002690
2691 case TLS1_VERSION:
David Benjamin0d56f882015-12-19 17:05:56 -05002692 return !(ssl->options & SSL_OP_NO_TLSv1);
Adam Langleyfcf25832014-12-18 17:42:32 -08002693
2694 case TLS1_1_VERSION:
David Benjamin0d56f882015-12-19 17:05:56 -05002695 return !(ssl->options & SSL_OP_NO_TLSv1_1);
Adam Langleyfcf25832014-12-18 17:42:32 -08002696
2697 case TLS1_2_VERSION:
David Benjamin0d56f882015-12-19 17:05:56 -05002698 return !(ssl->options & SSL_OP_NO_TLSv1_2);
Adam Langleyfcf25832014-12-18 17:42:32 -08002699
Steven Valdez4f94b1c2016-05-24 12:31:07 -04002700 case TLS1_3_VERSION:
2701 return !(ssl->options & SSL_OP_NO_TLSv1_3);
2702
Adam Langleyfcf25832014-12-18 17:42:32 -08002703 default:
2704 return 0;
2705 }
2706 }
2707}
2708
David Benjaminb9e4fa52015-12-29 23:58:17 -05002709uint16_t ssl3_version_from_wire(const SSL *ssl, uint16_t wire_version) {
David Benjamin0d56f882015-12-19 17:05:56 -05002710 if (!SSL_IS_DTLS(ssl)) {
David Benjaminea72bd02014-12-21 21:27:41 -05002711 return wire_version;
2712 }
2713
2714 uint16_t tls_version = ~wire_version;
2715 uint16_t version = tls_version + 0x0201;
2716 /* If either component overflowed, clamp it so comparisons still work. */
2717 if ((version >> 8) < (tls_version >> 8)) {
2718 version = 0xff00 | (version & 0xff);
2719 }
2720 if ((version & 0xff) < (tls_version & 0xff)) {
2721 version = (version & 0xff00) | 0xff;
2722 }
2723 /* DTLS 1.0 maps to TLS 1.1, not TLS 1.0. */
2724 if (version == TLS1_VERSION) {
2725 version = TLS1_1_VERSION;
2726 }
2727 return version;
2728}
2729
David Benjaminb9e4fa52015-12-29 23:58:17 -05002730uint16_t ssl3_protocol_version(const SSL *ssl) {
2731 assert(ssl->s3->have_version);
2732 return ssl3_version_from_wire(ssl, ssl->version);
2733}
2734
David Benjamin42fea372015-09-19 01:22:44 -04002735int SSL_is_server(SSL *ssl) { return ssl->server; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002736
David Benjamind4c2bce2015-10-17 12:28:18 -04002737void SSL_CTX_set_select_certificate_cb(
2738 SSL_CTX *ctx, int (*cb)(const struct ssl_early_callback_ctx *)) {
2739 ctx->select_certificate_cb = cb;
2740}
2741
Adam Langley524e7172015-02-20 16:04:00 -08002742void SSL_CTX_set_dos_protection_cb(
2743 SSL_CTX *ctx, int (*cb)(const struct ssl_early_callback_ctx *)) {
2744 ctx->dos_protection_cb = cb;
2745}
2746
David Benjamin1d5ef3b2015-10-12 19:54:18 -04002747void SSL_set_renegotiate_mode(SSL *ssl, enum ssl_renegotiate_mode_t mode) {
2748 ssl->renegotiate_mode = mode;
2749}
2750
2751void SSL_set_reject_peer_renegotiations(SSL *ssl, int reject) {
2752 SSL_set_renegotiate_mode(
2753 ssl, reject ? ssl_renegotiate_never : ssl_renegotiate_freely);
David Benjaminb16346b2015-04-08 19:16:58 -04002754}
2755
Adam Langley3f92d212015-02-20 15:32:52 -08002756int SSL_get_rc4_state(const SSL *ssl, const RC4_KEY **read_key,
2757 const RC4_KEY **write_key) {
David Benjamin79978df2015-12-25 15:56:49 -05002758 if (ssl->s3->aead_read_ctx == NULL || ssl->s3->aead_write_ctx == NULL) {
Adam Langley3f92d212015-02-20 15:32:52 -08002759 return 0;
2760 }
2761
David Benjamin79978df2015-12-25 15:56:49 -05002762 return EVP_AEAD_CTX_get_rc4_state(&ssl->s3->aead_read_ctx->ctx, read_key) &&
2763 EVP_AEAD_CTX_get_rc4_state(&ssl->s3->aead_write_ctx->ctx, write_key);
Adam Langley3f92d212015-02-20 15:32:52 -08002764}
David Benjaminda881e92015-04-26 14:45:04 -04002765
Adam Langleyc2d32802015-11-03 18:36:10 -08002766int SSL_get_ivs(const SSL *ssl, const uint8_t **out_read_iv,
2767 const uint8_t **out_write_iv, size_t *out_iv_len) {
David Benjamin79978df2015-12-25 15:56:49 -05002768 if (ssl->s3->aead_read_ctx == NULL || ssl->s3->aead_write_ctx == NULL) {
Adam Langleyc2d32802015-11-03 18:36:10 -08002769 return 0;
2770 }
2771
2772 size_t write_iv_len;
David Benjamin79978df2015-12-25 15:56:49 -05002773 if (!EVP_AEAD_CTX_get_iv(&ssl->s3->aead_read_ctx->ctx, out_read_iv,
2774 out_iv_len) ||
2775 !EVP_AEAD_CTX_get_iv(&ssl->s3->aead_write_ctx->ctx, out_write_iv,
Adam Langleyc2d32802015-11-03 18:36:10 -08002776 &write_iv_len) ||
2777 *out_iv_len != write_iv_len) {
2778 return 0;
2779 }
2780
2781 return 1;
David Benjamine348ff42015-11-06 17:55:30 -05002782}
David Benjamin6e807652015-11-02 12:02:20 -05002783
David Benjamin91b25012016-01-21 16:39:58 -05002784static uint64_t be_to_u64(const uint8_t in[8]) {
David Benjaminde942382016-02-11 12:02:01 -05002785 return (((uint64_t)in[0]) << 56) | (((uint64_t)in[1]) << 48) |
2786 (((uint64_t)in[2]) << 40) | (((uint64_t)in[3]) << 32) |
2787 (((uint64_t)in[4]) << 24) | (((uint64_t)in[5]) << 16) |
2788 (((uint64_t)in[6]) << 8) | ((uint64_t)in[7]);
David Benjamin91b25012016-01-21 16:39:58 -05002789}
2790
2791uint64_t SSL_get_read_sequence(const SSL *ssl) {
2792 /* TODO(davidben): Internally represent sequence numbers as uint64_t. */
David Benjaminde942382016-02-11 12:02:01 -05002793 if (SSL_IS_DTLS(ssl)) {
2794 /* max_seq_num already includes the epoch. */
2795 assert(ssl->d1->r_epoch == (ssl->d1->bitmap.max_seq_num >> 48));
2796 return ssl->d1->bitmap.max_seq_num;
2797 }
David Benjamin91b25012016-01-21 16:39:58 -05002798 return be_to_u64(ssl->s3->read_sequence);
2799}
2800
2801uint64_t SSL_get_write_sequence(const SSL *ssl) {
David Benjaminde942382016-02-11 12:02:01 -05002802 uint64_t ret = be_to_u64(ssl->s3->write_sequence);
2803 if (SSL_IS_DTLS(ssl)) {
2804 assert((ret >> 48) == 0);
2805 ret |= ((uint64_t)ssl->d1->w_epoch) << 48;
2806 }
2807 return ret;
David Benjamin91b25012016-01-21 16:39:58 -05002808}
2809
David Benjamin6e807652015-11-02 12:02:20 -05002810uint8_t SSL_get_server_key_exchange_hash(const SSL *ssl) {
2811 return ssl->s3->tmp.server_key_exchange_hash;
Adam Langleyc2d32802015-11-03 18:36:10 -08002812}
2813
David Benjamin96ba15f2015-12-25 14:10:17 -05002814size_t SSL_get_client_random(const SSL *ssl, uint8_t *out, size_t max_out) {
2815 if (max_out == 0) {
2816 return sizeof(ssl->s3->client_random);
2817 }
2818 if (max_out > sizeof(ssl->s3->client_random)) {
2819 max_out = sizeof(ssl->s3->client_random);
2820 }
2821 memcpy(out, ssl->s3->client_random, max_out);
2822 return max_out;
2823}
2824
2825size_t SSL_get_server_random(const SSL *ssl, uint8_t *out, size_t max_out) {
2826 if (max_out == 0) {
2827 return sizeof(ssl->s3->server_random);
2828 }
2829 if (max_out > sizeof(ssl->s3->server_random)) {
2830 max_out = sizeof(ssl->s3->server_random);
2831 }
2832 memcpy(out, ssl->s3->server_random, max_out);
2833 return max_out;
2834}
2835
David Benjaminf6494f42016-01-07 12:37:41 -08002836const SSL_CIPHER *SSL_get_pending_cipher(const SSL *ssl) {
2837 if (!SSL_in_init(ssl)) {
2838 return NULL;
2839 }
2840 return ssl->s3->tmp.new_cipher;
2841}
2842
David Benjamincdd0b7e2016-01-07 10:10:41 -08002843void SSL_CTX_set_retain_only_sha256_of_client_certs(SSL_CTX *ctx, int enabled) {
2844 ctx->retain_only_sha256_of_client_certs = !!enabled;
2845}
2846
David Benjamin27bbae42015-09-13 00:54:37 -04002847int SSL_clear(SSL *ssl) {
2848 if (ssl->method == NULL) {
2849 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_METHOD_SPECIFIED);
2850 return 0;
2851 }
2852
2853 if (ssl_clear_bad_session(ssl)) {
2854 SSL_SESSION_free(ssl->session);
2855 ssl->session = NULL;
2856 }
2857
2858 ssl->hit = 0;
David Benjamin27bbae42015-09-13 00:54:37 -04002859
2860 /* SSL_clear may be called before or after the |ssl| is initialized in either
2861 * accept or connect state. In the latter case, SSL_clear should preserve the
2862 * half and reset |ssl->state| accordingly. */
2863 if (ssl->handshake_func != NULL) {
2864 if (ssl->server) {
2865 SSL_set_accept_state(ssl);
2866 } else {
2867 SSL_set_connect_state(ssl);
2868 }
2869 } else {
2870 assert(ssl->state == 0);
2871 }
2872
2873 /* TODO(davidben): Some state on |ssl| is reset both in |SSL_new| and
2874 * |SSL_clear| because it is per-connection state rather than configuration
2875 * state. Per-connection state should be on |ssl->s3| and |ssl->d1| so it is
2876 * naturally reset at the right points between |SSL_new|, |SSL_clear|, and
2877 * |ssl3_new|. */
2878
2879 ssl->rwstate = SSL_NOTHING;
2880
2881 BUF_MEM_free(ssl->init_buf);
2882 ssl->init_buf = NULL;
2883
David Benjamin27bbae42015-09-13 00:54:37 -04002884 /* The ssl->d1->mtu is simultaneously configuration (preserved across
2885 * clear) and connection-specific state (gets reset).
2886 *
2887 * TODO(davidben): Avoid this. */
2888 unsigned mtu = 0;
2889 if (ssl->d1 != NULL) {
2890 mtu = ssl->d1->mtu;
2891 }
2892
2893 ssl->method->ssl_free(ssl);
2894 if (!ssl->method->ssl_new(ssl)) {
2895 return 0;
2896 }
David Benjamin27bbae42015-09-13 00:54:37 -04002897
2898 if (SSL_IS_DTLS(ssl) && (SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
2899 ssl->d1->mtu = mtu;
2900 }
2901
2902 ssl->client_version = ssl->version;
2903
2904 return 1;
2905}
2906
David Benjamin4e9cc712016-06-01 20:16:03 -04002907void ssl_do_info_callback(const SSL *ssl, int type, int value) {
2908 void (*cb)(const SSL *ssl, int type, int value) = NULL;
2909 if (ssl->info_callback != NULL) {
2910 cb = ssl->info_callback;
2911 } else if (ssl->ctx->info_callback != NULL) {
2912 cb = ssl->ctx->info_callback;
2913 }
2914
2915 if (cb != NULL) {
2916 cb(ssl, type, value);
2917 }
2918}
2919
2920void ssl_do_msg_callback(SSL *ssl, int is_write, int version, int content_type,
2921 const void *buf, size_t len) {
2922 if (ssl->msg_callback != NULL) {
2923 ssl->msg_callback(is_write, version, content_type, buf, len, ssl,
2924 ssl->msg_callback_arg);
2925 }
2926}
2927
David Benjaminda881e92015-04-26 14:45:04 -04002928int SSL_CTX_sess_connect(const SSL_CTX *ctx) { return 0; }
2929int SSL_CTX_sess_connect_good(const SSL_CTX *ctx) { return 0; }
2930int SSL_CTX_sess_connect_renegotiate(const SSL_CTX *ctx) { return 0; }
2931int SSL_CTX_sess_accept(const SSL_CTX *ctx) { return 0; }
2932int SSL_CTX_sess_accept_renegotiate(const SSL_CTX *ctx) { return 0; }
2933int SSL_CTX_sess_accept_good(const SSL_CTX *ctx) { return 0; }
2934int SSL_CTX_sess_hits(const SSL_CTX *ctx) { return 0; }
2935int SSL_CTX_sess_cb_hits(const SSL_CTX *ctx) { return 0; }
2936int SSL_CTX_sess_misses(const SSL_CTX *ctx) { return 0; }
2937int SSL_CTX_sess_timeouts(const SSL_CTX *ctx) { return 0; }
2938int SSL_CTX_sess_cache_full(const SSL_CTX *ctx) { return 0; }
David Benjamin936aada2016-06-07 19:09:22 -04002939
2940int SSL_num_renegotiations(const SSL *ssl) {
2941 return SSL_total_renegotiations(ssl);
2942}
2943
2944int SSL_CTX_need_tmp_RSA(const SSL_CTX *ctx) { return 0; }
2945int SSL_need_tmp_RSA(const SSL *ssl) { return 0; }
2946int SSL_CTX_set_tmp_rsa(SSL_CTX *ctx, const RSA *rsa) { return 1; }
2947int SSL_set_tmp_rsa(SSL *ssl, const RSA *rsa) { return 1; }
Matt Braithwaite4838d8a2015-08-20 13:20:03 -07002948void ERR_load_SSL_strings(void) {}
David Benjamin27bbae42015-09-13 00:54:37 -04002949void SSL_load_error_strings(void) {}
David Benjamin936aada2016-06-07 19:09:22 -04002950int SSL_cache_hit(SSL *ssl) { return SSL_session_reused(ssl); }
2951
2952int SSL_CTX_set_tmp_ecdh(SSL_CTX *ctx, const EC_KEY *ec_key) {
2953 if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) {
2954 OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
2955 return 0;
2956 }
2957 int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key));
2958 return SSL_CTX_set1_curves(ctx, &nid, 1);
2959}
2960
2961int SSL_set_tmp_ecdh(SSL *ssl, const EC_KEY *ec_key) {
2962 if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) {
2963 OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
2964 return 0;
2965 }
2966 int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key));
2967 return SSL_set1_curves(ssl, &nid, 1);
2968}