blob: 97d917c7c477a5c482e113d207d1ab52a201c685 [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 <stdio.h>
145#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700146
David Benjamin39482a12014-07-20 13:30:15 -0400147#include <openssl/bytestring.h>
David Benjamin7c6d09b2015-09-05 11:00:19 -0400148#include <openssl/crypto.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700149#include <openssl/dh.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400150#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700151#include <openssl/lhash.h>
152#include <openssl/mem.h>
153#include <openssl/obj.h>
154#include <openssl/rand.h>
155#include <openssl/x509v3.h>
156
David Benjamin2ee94aa2015-04-07 22:38:30 -0400157#include "internal.h"
David Benjamin546f1a52015-04-15 16:46:09 -0400158#include "../crypto/internal.h"
159
Adam Langley95c29f32014-06-20 12:00:00 -0700160
David Benjamin76c2efc2015-08-31 14:24:29 -0400161/* |SSL_R_UNKNOWN_PROTOCOL| is no longer emitted, but continue to define it
David Benjamin97760d52015-07-24 23:02:49 -0400162 * to avoid downstream churn. */
David Benjamin76c2efc2015-08-31 14:24:29 -0400163OPENSSL_DECLARE_ERROR_REASON(SSL, UNKNOWN_PROTOCOL)
David Benjamin97760d52015-07-24 23:02:49 -0400164
Adam Langleyfcf25832014-12-18 17:42:32 -0800165/* Some error codes are special. Ensure the make_errors.go script never
166 * regresses this. */
167OPENSSL_COMPILE_ASSERT(SSL_R_TLSV1_ALERT_NO_RENEGOTIATION ==
168 SSL_AD_NO_RENEGOTIATION + SSL_AD_REASON_OFFSET,
169 ssl_alert_reason_code_mismatch);
David Benjamine1136082014-09-20 12:28:58 -0400170
David Benjamin1d0a1942015-04-26 15:35:35 -0400171/* kMaxHandshakeSize is the maximum size, in bytes, of a handshake message. */
172static const size_t kMaxHandshakeSize = (1u << 24) - 1;
173
David Benjaminaa585132015-06-29 23:36:17 -0400174static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl =
175 CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
176static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl_ctx =
177 CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
David Benjamin9f33fc62015-04-15 17:29:53 -0400178
David Benjamin7c6d09b2015-09-05 11:00:19 -0400179int SSL_library_init(void) {
180 CRYPTO_library_init();
181 return 1;
182}
183
David Benjamin27bbae42015-09-13 00:54:37 -0400184static uint32_t ssl_session_hash(const SSL_SESSION *a) {
185 uint32_t hash =
Adam Langleyf4e554e2015-09-30 13:46:34 -0700186 ((uint32_t)a->session_id[0]) |
187 ((uint32_t)a->session_id[1] << 8) |
188 ((uint32_t)a->session_id[2] << 16) |
David Benjamin27bbae42015-09-13 00:54:37 -0400189 ((uint32_t)a->session_id[3] << 24);
David Benjamin7c6d09b2015-09-05 11:00:19 -0400190
David Benjamin27bbae42015-09-13 00:54:37 -0400191 return hash;
192}
193
194/* NB: If this function (or indeed the hash function which uses a sort of
195 * coarser function than this one) is changed, ensure
196 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
197 * able to construct an SSL_SESSION that will collide with any existing session
198 * with a matching session ID. */
199static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
200 if (a->ssl_version != b->ssl_version) {
201 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800202 }
Adam Langley95c29f32014-06-20 12:00:00 -0700203
David Benjamin27bbae42015-09-13 00:54:37 -0400204 if (a->session_id_length != b->session_id_length) {
205 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800206 }
Adam Langley95c29f32014-06-20 12:00:00 -0700207
David Benjamin27bbae42015-09-13 00:54:37 -0400208 return memcmp(a->session_id, b->session_id, a->session_id_length);
209}
Adam Langley95c29f32014-06-20 12:00:00 -0700210
David Benjamin27bbae42015-09-13 00:54:37 -0400211SSL_CTX *SSL_CTX_new(const SSL_METHOD *method) {
212 SSL_CTX *ret = NULL;
213
214 if (method == NULL) {
215 OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_METHOD_PASSED);
216 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800217 }
Adam Langley95c29f32014-06-20 12:00:00 -0700218
David Benjamin27bbae42015-09-13 00:54:37 -0400219 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
220 OPENSSL_PUT_ERROR(SSL, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
221 goto err;
David Benjamin62fd1622015-01-11 13:30:01 -0500222 }
223
David Benjamin27bbae42015-09-13 00:54:37 -0400224 ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
225 if (ret == NULL) {
226 goto err;
David Benjamin62fd1622015-01-11 13:30:01 -0500227 }
228
David Benjamin27bbae42015-09-13 00:54:37 -0400229 memset(ret, 0, sizeof(SSL_CTX));
Adam Langley95c29f32014-06-20 12:00:00 -0700230
David Benjamin27bbae42015-09-13 00:54:37 -0400231 ret->method = method->method;
232
233 CRYPTO_MUTEX_init(&ret->lock);
234
235 ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
236 ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
237
238 /* We take the system default */
239 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
240
241 ret->references = 1;
242
243 ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
244 ret->verify_mode = SSL_VERIFY_NONE;
245 ret->cert = ssl_cert_new();
246 if (ret->cert == NULL) {
247 goto err;
248 }
249
250 ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
251 if (ret->sessions == NULL) {
252 goto err;
253 }
254 ret->cert_store = X509_STORE_new();
255 if (ret->cert_store == NULL) {
256 goto err;
257 }
258
259 ssl_create_cipher_list(ret->method, &ret->cipher_list,
260 &ret->cipher_list_by_id, SSL_DEFAULT_CIPHER_LIST);
261 if (ret->cipher_list == NULL ||
262 sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0) {
263 OPENSSL_PUT_ERROR(SSL, SSL_R_LIBRARY_HAS_NO_CIPHERS);
264 goto err2;
265 }
266
267 ret->param = X509_VERIFY_PARAM_new();
268 if (!ret->param) {
269 goto err;
270 }
271
272 ret->client_CA = sk_X509_NAME_new_null();
273 if (ret->client_CA == NULL) {
274 goto err;
275 }
276
277 CRYPTO_new_ex_data(&g_ex_data_class_ssl_ctx, ret, &ret->ex_data);
278
279 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
280
281 /* Setup RFC4507 ticket keys */
282 if (!RAND_bytes(ret->tlsext_tick_key_name, 16) ||
283 !RAND_bytes(ret->tlsext_tick_hmac_key, 16) ||
284 !RAND_bytes(ret->tlsext_tick_aes_key, 16)) {
285 ret->options |= SSL_OP_NO_TICKET;
286 }
287
288 /* Default is to connect to non-RI servers. When RI is more widely deployed
289 * might change this. */
290 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
291
292 /* Lock the SSL_CTX to the specified version, for compatibility with legacy
293 * uses of SSL_METHOD. */
294 if (method->version != 0) {
295 SSL_CTX_set_max_version(ret, method->version);
296 SSL_CTX_set_min_version(ret, method->version);
297 }
298
299 return ret;
300
301err:
302 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
303err2:
304 SSL_CTX_free(ret);
305 return NULL;
306}
307
308void SSL_CTX_free(SSL_CTX *ctx) {
309 if (ctx == NULL ||
310 !CRYPTO_refcount_dec_and_test_zero(&ctx->references)) {
311 return;
312 }
313
314 X509_VERIFY_PARAM_free(ctx->param);
315
316 /* Free internal session cache. However: the remove_cb() may reference the
317 * ex_data of SSL_CTX, thus the ex_data store can only be removed after the
318 * sessions were flushed. As the ex_data handling routines might also touch
319 * the session cache, the most secure solution seems to be: empty (flush) the
320 * cache, then free ex_data, then finally free the cache. (See ticket
321 * [openssl.org #212].) */
322 SSL_CTX_flush_sessions(ctx, 0);
323
324 CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, ctx, &ctx->ex_data);
325
326 CRYPTO_MUTEX_cleanup(&ctx->lock);
327 lh_SSL_SESSION_free(ctx->sessions);
328 X509_STORE_free(ctx->cert_store);
329 ssl_cipher_preference_list_free(ctx->cipher_list);
330 sk_SSL_CIPHER_free(ctx->cipher_list_by_id);
331 ssl_cipher_preference_list_free(ctx->cipher_list_tls10);
332 ssl_cipher_preference_list_free(ctx->cipher_list_tls11);
333 ssl_cert_free(ctx->cert);
334 sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->client_custom_extensions,
335 SSL_CUSTOM_EXTENSION_free);
336 sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->server_custom_extensions,
337 SSL_CUSTOM_EXTENSION_free);
338 sk_X509_NAME_pop_free(ctx->client_CA, X509_NAME_free);
339 sk_SRTP_PROTECTION_PROFILE_free(ctx->srtp_profiles);
340 OPENSSL_free(ctx->psk_identity_hint);
341 OPENSSL_free(ctx->tlsext_ellipticcurvelist);
342 OPENSSL_free(ctx->alpn_client_proto_list);
343 OPENSSL_free(ctx->ocsp_response);
344 OPENSSL_free(ctx->signed_cert_timestamp_list);
345 EVP_PKEY_free(ctx->tlsext_channel_id_private);
346 BIO_free(ctx->keylog_bio);
347
348 OPENSSL_free(ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -0800349}
Adam Langley95c29f32014-06-20 12:00:00 -0700350
Adam Langleyfcf25832014-12-18 17:42:32 -0800351SSL *SSL_new(SSL_CTX *ctx) {
352 SSL *s;
Adam Langley95c29f32014-06-20 12:00:00 -0700353
Adam Langleyfcf25832014-12-18 17:42:32 -0800354 if (ctx == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400355 OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_CTX);
Adam Langleyfcf25832014-12-18 17:42:32 -0800356 return NULL;
357 }
358 if (ctx->method == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400359 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
Adam Langleyfcf25832014-12-18 17:42:32 -0800360 return NULL;
361 }
Adam Langley95c29f32014-06-20 12:00:00 -0700362
Adam Langleyfcf25832014-12-18 17:42:32 -0800363 s = (SSL *)OPENSSL_malloc(sizeof(SSL));
364 if (s == NULL) {
365 goto err;
366 }
367 memset(s, 0, sizeof(SSL));
Adam Langley95c29f32014-06-20 12:00:00 -0700368
Adam Langleyfcf25832014-12-18 17:42:32 -0800369 s->min_version = ctx->min_version;
370 s->max_version = ctx->max_version;
David Benjamin1eb367c2014-12-12 18:17:51 -0500371
Adam Langleyfcf25832014-12-18 17:42:32 -0800372 s->options = ctx->options;
373 s->mode = ctx->mode;
374 s->max_cert_list = ctx->max_cert_list;
Adam Langley95c29f32014-06-20 12:00:00 -0700375
David Benjamina5a3eeb2015-03-18 20:26:30 -0400376 s->cert = ssl_cert_dup(ctx->cert);
377 if (s->cert == NULL) {
378 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -0800379 }
Adam Langley95c29f32014-06-20 12:00:00 -0700380
Adam Langleyfcf25832014-12-18 17:42:32 -0800381 s->msg_callback = ctx->msg_callback;
382 s->msg_callback_arg = ctx->msg_callback_arg;
383 s->verify_mode = ctx->verify_mode;
384 s->sid_ctx_length = ctx->sid_ctx_length;
385 assert(s->sid_ctx_length <= sizeof s->sid_ctx);
386 memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));
387 s->verify_callback = ctx->default_verify_callback;
388 s->generate_session_id = ctx->generate_session_id;
Adam Langley95c29f32014-06-20 12:00:00 -0700389
Adam Langleyfcf25832014-12-18 17:42:32 -0800390 s->param = X509_VERIFY_PARAM_new();
391 if (!s->param) {
392 goto err;
393 }
394 X509_VERIFY_PARAM_inherit(s->param, ctx->param);
395 s->quiet_shutdown = ctx->quiet_shutdown;
396 s->max_send_fragment = ctx->max_send_fragment;
Adam Langley95c29f32014-06-20 12:00:00 -0700397
Adam Langley0b5e3902015-05-15 13:08:38 -0700398 CRYPTO_refcount_inc(&ctx->references);
Adam Langleyfcf25832014-12-18 17:42:32 -0800399 s->ctx = ctx;
Adam Langley0b5e3902015-05-15 13:08:38 -0700400 CRYPTO_refcount_inc(&ctx->references);
Adam Langleyfcf25832014-12-18 17:42:32 -0800401 s->initial_ctx = ctx;
Adam Langley95c29f32014-06-20 12:00:00 -0700402
Adam Langleyfcf25832014-12-18 17:42:32 -0800403 if (ctx->tlsext_ellipticcurvelist) {
404 s->tlsext_ellipticcurvelist =
405 BUF_memdup(ctx->tlsext_ellipticcurvelist,
406 ctx->tlsext_ellipticcurvelist_length * 2);
407 if (!s->tlsext_ellipticcurvelist) {
408 goto err;
409 }
410 s->tlsext_ellipticcurvelist_length = ctx->tlsext_ellipticcurvelist_length;
411 }
Adam Langley95c29f32014-06-20 12:00:00 -0700412
Adam Langleyfcf25832014-12-18 17:42:32 -0800413 if (s->ctx->alpn_client_proto_list) {
414 s->alpn_client_proto_list = BUF_memdup(s->ctx->alpn_client_proto_list,
415 s->ctx->alpn_client_proto_list_len);
416 if (s->alpn_client_proto_list == NULL) {
417 goto err;
418 }
419 s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;
420 }
Adam Langley95c29f32014-06-20 12:00:00 -0700421
Adam Langleyfcf25832014-12-18 17:42:32 -0800422 s->verify_result = X509_V_OK;
423 s->method = ctx->method;
Adam Langley95c29f32014-06-20 12:00:00 -0700424
Adam Langleyfcf25832014-12-18 17:42:32 -0800425 if (!s->method->ssl_new(s)) {
426 goto err;
427 }
428 s->enc_method = ssl3_get_enc_method(s->version);
429 assert(s->enc_method != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700430
David Benjamin62fd1622015-01-11 13:30:01 -0500431 s->rwstate = SSL_NOTHING;
Adam Langley95c29f32014-06-20 12:00:00 -0700432
David Benjamin9f33fc62015-04-15 17:29:53 -0400433 CRYPTO_new_ex_data(&g_ex_data_class_ssl, s, &s->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700434
Adam Langleyfcf25832014-12-18 17:42:32 -0800435 s->psk_identity_hint = NULL;
436 if (ctx->psk_identity_hint) {
437 s->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
438 if (s->psk_identity_hint == NULL) {
439 goto err;
440 }
441 }
442 s->psk_client_callback = ctx->psk_client_callback;
443 s->psk_server_callback = ctx->psk_server_callback;
Adam Langley95c29f32014-06-20 12:00:00 -0700444
David Benjamin02ddbfd2015-01-11 13:09:11 -0500445 s->tlsext_channel_id_enabled = ctx->tlsext_channel_id_enabled;
446 if (ctx->tlsext_channel_id_private) {
David Benjamin9a10f8f2015-05-05 22:22:40 -0400447 s->tlsext_channel_id_private =
448 EVP_PKEY_up_ref(ctx->tlsext_channel_id_private);
David Benjamin02ddbfd2015-01-11 13:09:11 -0500449 }
450
Adam Langleyfcf25832014-12-18 17:42:32 -0800451 s->signed_cert_timestamps_enabled = s->ctx->signed_cert_timestamps_enabled;
452 s->ocsp_stapling_enabled = s->ctx->ocsp_stapling_enabled;
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200453
Adam Langleyfcf25832014-12-18 17:42:32 -0800454 return s;
455
Adam Langley95c29f32014-06-20 12:00:00 -0700456err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400457 SSL_free(s);
David Benjamin3570d732015-06-29 00:28:17 -0400458 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700459
Adam Langleyfcf25832014-12-18 17:42:32 -0800460 return NULL;
461}
Adam Langley95c29f32014-06-20 12:00:00 -0700462
David Benjamin27bbae42015-09-13 00:54:37 -0400463void SSL_free(SSL *ssl) {
464 if (ssl == NULL) {
465 return;
466 }
467
468 X509_VERIFY_PARAM_free(ssl->param);
469
470 CRYPTO_free_ex_data(&g_ex_data_class_ssl, ssl, &ssl->ex_data);
471
472 if (ssl->bbio != NULL) {
473 /* If the buffering BIO is in place, pop it off */
474 if (ssl->bbio == ssl->wbio) {
475 ssl->wbio = BIO_pop(ssl->wbio);
476 }
477 BIO_free(ssl->bbio);
478 ssl->bbio = NULL;
479 }
480
481 int free_wbio = ssl->wbio != ssl->rbio;
482 BIO_free_all(ssl->rbio);
483 if (free_wbio) {
484 BIO_free_all(ssl->wbio);
485 }
486
487 BUF_MEM_free(ssl->init_buf);
488
489 /* add extra stuff */
490 ssl_cipher_preference_list_free(ssl->cipher_list);
491 sk_SSL_CIPHER_free(ssl->cipher_list_by_id);
492
493 ssl_clear_bad_session(ssl);
494 SSL_SESSION_free(ssl->session);
495
496 ssl_clear_cipher_ctx(ssl);
497
498 ssl_cert_free(ssl->cert);
499
500 OPENSSL_free(ssl->tlsext_hostname);
501 SSL_CTX_free(ssl->initial_ctx);
502 OPENSSL_free(ssl->tlsext_ellipticcurvelist);
503 OPENSSL_free(ssl->alpn_client_proto_list);
504 EVP_PKEY_free(ssl->tlsext_channel_id_private);
505 OPENSSL_free(ssl->psk_identity_hint);
506 sk_X509_NAME_pop_free(ssl->client_CA, X509_NAME_free);
507 OPENSSL_free(ssl->next_proto_negotiated);
508 sk_SRTP_PROTECTION_PROFILE_free(ssl->srtp_profiles);
509
510 if (ssl->method != NULL) {
511 ssl->method->ssl_free(ssl);
512 }
513 SSL_CTX_free(ssl->ctx);
514
515 OPENSSL_free(ssl);
516}
517
518void SSL_set_connect_state(SSL *ssl) {
519 ssl->server = 0;
520 ssl->shutdown = 0;
521 ssl->state = SSL_ST_CONNECT;
522 ssl->handshake_func = ssl->method->ssl_connect;
523 /* clear the current cipher */
524 ssl_clear_cipher_ctx(ssl);
525}
526
527void SSL_set_accept_state(SSL *ssl) {
528 ssl->server = 1;
529 ssl->shutdown = 0;
530 ssl->state = SSL_ST_ACCEPT;
531 ssl->handshake_func = ssl->method->ssl_accept;
532 /* clear the current cipher */
533 ssl_clear_cipher_ctx(ssl);
534}
535
536void SSL_set_bio(SSL *ssl, BIO *rbio, BIO *wbio) {
537 /* If the output buffering BIO is still in place, remove it. */
538 if (ssl->bbio != NULL) {
539 if (ssl->wbio == ssl->bbio) {
540 ssl->wbio = ssl->wbio->next_bio;
541 ssl->bbio->next_bio = NULL;
542 }
543 }
544
545 if (ssl->rbio != rbio) {
546 BIO_free_all(ssl->rbio);
547 }
548 if (ssl->wbio != wbio && ssl->rbio != ssl->wbio) {
549 BIO_free_all(ssl->wbio);
550 }
551 ssl->rbio = rbio;
552 ssl->wbio = wbio;
553}
554
555BIO *SSL_get_rbio(const SSL *ssl) { return ssl->rbio; }
556
557BIO *SSL_get_wbio(const SSL *ssl) { return ssl->wbio; }
558
559int SSL_do_handshake(SSL *ssl) {
560 if (ssl->handshake_func == NULL) {
561 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_TYPE_NOT_SET);
562 return -1;
563 }
564
565 if (!SSL_in_init(ssl)) {
566 return 1;
567 }
568
569 return ssl->handshake_func(ssl);
570}
571
572int SSL_connect(SSL *ssl) {
573 if (ssl->handshake_func == 0) {
574 /* Not properly initialized yet */
575 SSL_set_connect_state(ssl);
576 }
577
578 if (ssl->handshake_func != ssl->method->ssl_connect) {
579 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
580 return -1;
581 }
582
583 return ssl->handshake_func(ssl);
584}
585
586int SSL_accept(SSL *ssl) {
587 if (ssl->handshake_func == 0) {
588 /* Not properly initialized yet */
589 SSL_set_accept_state(ssl);
590 }
591
592 if (ssl->handshake_func != ssl->method->ssl_accept) {
593 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
594 return -1;
595 }
596
597 return ssl->handshake_func(ssl);
598}
599
600int SSL_read(SSL *ssl, void *buf, int num) {
601 if (ssl->handshake_func == 0) {
602 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
603 return -1;
604 }
605
606 if (ssl->shutdown & SSL_RECEIVED_SHUTDOWN) {
607 ssl->rwstate = SSL_NOTHING;
608 return 0;
609 }
610
611 ERR_clear_system_error();
612 return ssl->method->ssl_read_app_data(ssl, buf, num, 0);
613}
614
615int SSL_peek(SSL *ssl, void *buf, int num) {
616 if (ssl->handshake_func == 0) {
617 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
618 return -1;
619 }
620
621 if (ssl->shutdown & SSL_RECEIVED_SHUTDOWN) {
622 return 0;
623 }
624
625 ERR_clear_system_error();
626 return ssl->method->ssl_read_app_data(ssl, buf, num, 1);
627}
628
629int SSL_write(SSL *ssl, const void *buf, int num) {
630 if (ssl->handshake_func == 0) {
631 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
632 return -1;
633 }
634
635 if (ssl->shutdown & SSL_SENT_SHUTDOWN) {
636 ssl->rwstate = SSL_NOTHING;
637 OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
638 return -1;
639 }
640
641 ERR_clear_system_error();
642 return ssl->method->ssl_write_app_data(ssl, buf, num);
643}
644
645int SSL_shutdown(SSL *ssl) {
646 /* Note that this function behaves differently from what one might expect.
647 * Return values are 0 for no success (yet), 1 for success; but calling it
648 * once is usually not enough, even if blocking I/O is used (see
649 * ssl3_shutdown). */
650
651 if (ssl->handshake_func == 0) {
652 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
653 return -1;
654 }
655
656 if (SSL_in_init(ssl)) {
657 return 1;
658 }
659
660 /* Do nothing if configured not to send a close_notify. */
661 if (ssl->quiet_shutdown) {
662 ssl->shutdown = SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN;
663 return 1;
664 }
665
666 if (!(ssl->shutdown & SSL_SENT_SHUTDOWN)) {
667 ssl->shutdown |= SSL_SENT_SHUTDOWN;
668 ssl3_send_alert(ssl, SSL3_AL_WARNING, SSL_AD_CLOSE_NOTIFY);
669
670 /* our shutdown alert has been sent now, and if it still needs to be
671 * written, ssl->s3->alert_dispatch will be true */
672 if (ssl->s3->alert_dispatch) {
673 return -1; /* return WANT_WRITE */
674 }
675 } else if (ssl->s3->alert_dispatch) {
676 /* resend it if not sent */
677 int ret = ssl->method->ssl_dispatch_alert(ssl);
678 if (ret == -1) {
679 /* we only get to return -1 here the 2nd/Nth invocation, we must have
680 * already signalled return 0 upon a previous invoation, return
681 * WANT_WRITE */
682 return ret;
683 }
684 } else if (!(ssl->shutdown & SSL_RECEIVED_SHUTDOWN)) {
685 /* If we are waiting for a close from our peer, we are closed */
686 ssl->method->ssl_read_close_notify(ssl);
687 if (!(ssl->shutdown & SSL_RECEIVED_SHUTDOWN)) {
688 return -1; /* return WANT_READ */
689 }
690 }
691
692 if (ssl->shutdown == (SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN) &&
693 !ssl->s3->alert_dispatch) {
694 return 1;
695 } else {
696 return 0;
697 }
698}
699
700int SSL_get_error(const SSL *ssl, int ret_code) {
701 int reason;
702 uint32_t err;
703 BIO *bio;
704
705 if (ret_code > 0) {
706 return SSL_ERROR_NONE;
707 }
708
709 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
710 * where we do encode the error */
711 err = ERR_peek_error();
712 if (err != 0) {
713 if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
714 return SSL_ERROR_SYSCALL;
715 }
716 return SSL_ERROR_SSL;
717 }
718
719 if (ret_code == 0) {
720 if ((ssl->shutdown & SSL_RECEIVED_SHUTDOWN) &&
721 (ssl->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) {
722 /* The socket was cleanly shut down with a close_notify. */
723 return SSL_ERROR_ZERO_RETURN;
724 }
725 /* An EOF was observed which violates the protocol, and the underlying
726 * transport does not participate in the error queue. Bubble up to the
727 * caller. */
728 return SSL_ERROR_SYSCALL;
729 }
730
731 if (SSL_want_session(ssl)) {
732 return SSL_ERROR_PENDING_SESSION;
733 }
734
735 if (SSL_want_certificate(ssl)) {
736 return SSL_ERROR_PENDING_CERTIFICATE;
737 }
738
739 if (SSL_want_read(ssl)) {
740 bio = SSL_get_rbio(ssl);
741 if (BIO_should_read(bio)) {
742 return SSL_ERROR_WANT_READ;
743 }
744
745 if (BIO_should_write(bio)) {
746 /* This one doesn't make too much sense ... We never try to write to the
747 * rbio, and an application program where rbio and wbio are separate
748 * couldn't even know what it should wait for. However if we ever set
749 * s->rwstate incorrectly (so that we have SSL_want_read(s) instead of
750 * SSL_want_write(s)) and rbio and wbio *are* the same, this test works
751 * around that bug; so it might be safer to keep it. */
752 return SSL_ERROR_WANT_WRITE;
753 }
754
755 if (BIO_should_io_special(bio)) {
756 reason = BIO_get_retry_reason(bio);
757 if (reason == BIO_RR_CONNECT) {
758 return SSL_ERROR_WANT_CONNECT;
759 }
760
761 if (reason == BIO_RR_ACCEPT) {
762 return SSL_ERROR_WANT_ACCEPT;
763 }
764
765 return SSL_ERROR_SYSCALL; /* unknown */
766 }
767 }
768
769 if (SSL_want_write(ssl)) {
770 bio = SSL_get_wbio(ssl);
771 if (BIO_should_write(bio)) {
772 return SSL_ERROR_WANT_WRITE;
773 }
774
775 if (BIO_should_read(bio)) {
776 /* See above (SSL_want_read(ssl) with BIO_should_write(bio)) */
777 return SSL_ERROR_WANT_READ;
778 }
779
780 if (BIO_should_io_special(bio)) {
781 reason = BIO_get_retry_reason(bio);
782 if (reason == BIO_RR_CONNECT) {
783 return SSL_ERROR_WANT_CONNECT;
784 }
785
786 if (reason == BIO_RR_ACCEPT) {
787 return SSL_ERROR_WANT_ACCEPT;
788 }
789
790 return SSL_ERROR_SYSCALL;
791 }
792 }
793
794 if (SSL_want_x509_lookup(ssl)) {
795 return SSL_ERROR_WANT_X509_LOOKUP;
796 }
797
798 if (SSL_want_channel_id_lookup(ssl)) {
799 return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
800 }
801
802 if (SSL_want_private_key_operation(ssl)) {
803 return SSL_ERROR_WANT_PRIVATE_KEY_OPERATION;
804 }
805
806 return SSL_ERROR_SYSCALL;
807}
808
809void SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) {
810 ctx->min_version = version;
811}
812
813void SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) {
814 ctx->max_version = version;
815}
816
817void SSL_set_min_version(SSL *ssl, uint16_t version) {
818 ssl->min_version = version;
819}
820
821void SSL_set_max_version(SSL *ssl, uint16_t version) {
822 ssl->max_version = version;
823}
824
825uint32_t SSL_CTX_set_options(SSL_CTX *ctx, uint32_t options) {
826 ctx->options |= options;
827 return ctx->options;
828}
829
830uint32_t SSL_CTX_clear_options(SSL_CTX *ctx, uint32_t options) {
831 ctx->options &= ~options;
832 return ctx->options;
833}
834
835uint32_t SSL_CTX_get_options(const SSL_CTX *ctx) { return ctx->options; }
836
837uint32_t SSL_set_options(SSL *ssl, uint32_t options) {
838 ssl->options |= options;
839 return ssl->options;
840}
841
842uint32_t SSL_clear_options(SSL *ssl, uint32_t options) {
843 ssl->options &= ~options;
844 return ssl->options;
845}
846
847uint32_t SSL_get_options(const SSL *ssl) { return ssl->options; }
848
849uint32_t SSL_CTX_set_mode(SSL_CTX *ctx, uint32_t mode) {
850 ctx->mode |= mode;
851 return ctx->mode;
852}
853
854uint32_t SSL_CTX_clear_mode(SSL_CTX *ctx, uint32_t mode) {
855 ctx->mode &= ~mode;
856 return ctx->mode;
857}
858
859uint32_t SSL_CTX_get_mode(const SSL_CTX *ctx) { return ctx->mode; }
860
861uint32_t SSL_set_mode(SSL *ssl, uint32_t mode) {
862 ssl->mode |= mode;
863 return ssl->mode;
864}
865
866uint32_t SSL_clear_mode(SSL *ssl, uint32_t mode) {
867 ssl->mode &= ~mode;
868 return ssl->mode;
869}
870
871uint32_t SSL_get_mode(const SSL *ssl) { return ssl->mode; }
872
David Benjaminee0c8272015-09-13 01:03:54 -0400873X509 *SSL_get_peer_certificate(const SSL *ssl) {
874 if (ssl == NULL || ssl->session == NULL || ssl->session->peer == NULL) {
875 return NULL;
876 }
877 return X509_up_ref(ssl->session->peer);
878}
879
880STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *ssl) {
881 if (ssl == NULL || ssl->session == NULL) {
882 return NULL;
883 }
884 return ssl->session->cert_chain;
885}
886
887int SSL_get_tls_unique(const SSL *ssl, uint8_t *out, size_t *out_len,
888 size_t max_out) {
889 /* The tls-unique value is the first Finished message in the handshake, which
890 * is the client's in a full handshake and the server's for a resumption. See
891 * https://tools.ietf.org/html/rfc5929#section-3.1. */
892 const uint8_t *finished = ssl->s3->previous_client_finished;
893 size_t finished_len = ssl->s3->previous_client_finished_len;
894 if (ssl->hit) {
895 /* tls-unique is broken for resumed sessions unless EMS is used. */
896 if (!ssl->session->extended_master_secret) {
897 goto err;
898 }
899 finished = ssl->s3->previous_server_finished;
900 finished_len = ssl->s3->previous_server_finished_len;
901 }
902
903 if (!ssl->s3->initial_handshake_complete ||
904 ssl->version < TLS1_VERSION) {
905 goto err;
906 }
907
908 *out_len = finished_len;
909 if (finished_len > max_out) {
910 *out_len = max_out;
911 }
912
913 memcpy(out, finished, *out_len);
914 return 1;
915
916err:
917 *out_len = 0;
918 memset(out, 0, max_out);
919 return 0;
920}
921
Adam Langleyfcf25832014-12-18 17:42:32 -0800922int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
David Benjamindafbdd42015-09-14 01:40:10 -0400923 unsigned sid_ctx_len) {
924 if (sid_ctx_len > sizeof(ctx->sid_ctx)) {
David Benjamin3570d732015-06-29 00:28:17 -0400925 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -0800926 return 0;
927 }
928 ctx->sid_ctx_length = sid_ctx_len;
929 memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700930
Adam Langleyfcf25832014-12-18 17:42:32 -0800931 return 1;
932}
Adam Langley95c29f32014-06-20 12:00:00 -0700933
Adam Langleyfcf25832014-12-18 17:42:32 -0800934int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
David Benjamindafbdd42015-09-14 01:40:10 -0400935 unsigned sid_ctx_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800936 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
David Benjamin3570d732015-06-29 00:28:17 -0400937 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -0800938 return 0;
939 }
940 ssl->sid_ctx_length = sid_ctx_len;
941 memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700942
Adam Langleyfcf25832014-12-18 17:42:32 -0800943 return 1;
944}
Adam Langley95c29f32014-06-20 12:00:00 -0700945
Adam Langleyfcf25832014-12-18 17:42:32 -0800946int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800947 ctx->generate_session_id = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -0800948 return 1;
949}
Adam Langley95c29f32014-06-20 12:00:00 -0700950
Adam Langleyfcf25832014-12-18 17:42:32 -0800951int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800952 ssl->generate_session_id = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -0800953 return 1;
954}
Adam Langley95c29f32014-06-20 12:00:00 -0700955
Adam Langleyfcf25832014-12-18 17:42:32 -0800956int SSL_has_matching_session_id(const SSL *ssl, const uint8_t *id,
David Benjamindafbdd42015-09-14 01:40:10 -0400957 unsigned id_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800958 /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how we
959 * can "construct" a session to give us the desired check - ie. to find if
960 * there's a session in the hash table that would conflict with any new
961 * session built out of this id/id_len and the ssl_version in use by this
962 * SSL. */
963 SSL_SESSION r, *p;
Adam Langley95c29f32014-06-20 12:00:00 -0700964
Adam Langleyfcf25832014-12-18 17:42:32 -0800965 if (id_len > sizeof r.session_id) {
966 return 0;
967 }
Adam Langley95c29f32014-06-20 12:00:00 -0700968
Adam Langleyfcf25832014-12-18 17:42:32 -0800969 r.ssl_version = ssl->version;
970 r.session_id_length = id_len;
971 memcpy(r.session_id, id, id_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700972
Adam Langley4bdb6e42015-05-15 15:29:21 -0700973 CRYPTO_MUTEX_lock_read(&ssl->ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -0800974 p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r);
Adam Langley4bdb6e42015-05-15 15:29:21 -0700975 CRYPTO_MUTEX_unlock(&ssl->ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -0800976 return p != NULL;
977}
Adam Langley95c29f32014-06-20 12:00:00 -0700978
David Benjamin59937042015-09-19 13:04:22 -0400979int SSL_CTX_set_purpose(SSL_CTX *ctx, int purpose) {
980 return X509_VERIFY_PARAM_set_purpose(ctx->param, purpose);
Adam Langleyfcf25832014-12-18 17:42:32 -0800981}
982
David Benjamin59937042015-09-19 13:04:22 -0400983int SSL_set_purpose(SSL *ssl, int purpose) {
984 return X509_VERIFY_PARAM_set_purpose(ssl->param, purpose);
Adam Langleyfcf25832014-12-18 17:42:32 -0800985}
986
David Benjamin59937042015-09-19 13:04:22 -0400987int SSL_CTX_set_trust(SSL_CTX *ctx, int trust) {
988 return X509_VERIFY_PARAM_set_trust(ctx->param, trust);
Adam Langleyfcf25832014-12-18 17:42:32 -0800989}
990
David Benjamin59937042015-09-19 13:04:22 -0400991int SSL_set_trust(SSL *ssl, int trust) {
992 return X509_VERIFY_PARAM_set_trust(ssl->param, trust);
Adam Langleyfcf25832014-12-18 17:42:32 -0800993}
994
David Benjamin59937042015-09-19 13:04:22 -0400995int SSL_CTX_set1_param(SSL_CTX *ctx, const X509_VERIFY_PARAM *param) {
996 return X509_VERIFY_PARAM_set1(ctx->param, param);
Adam Langleyfcf25832014-12-18 17:42:32 -0800997}
998
David Benjamin59937042015-09-19 13:04:22 -0400999int SSL_set1_param(SSL *ssl, const X509_VERIFY_PARAM *param) {
1000 return X509_VERIFY_PARAM_set1(ssl->param, param);
Adam Langleyfcf25832014-12-18 17:42:32 -08001001}
Adam Langley95c29f32014-06-20 12:00:00 -07001002
Adam Langley858a88d2014-06-20 12:00:00 -07001003void ssl_cipher_preference_list_free(
Adam Langleyfcf25832014-12-18 17:42:32 -08001004 struct ssl_cipher_preference_list_st *cipher_list) {
David Benjamin5d1ec732015-04-22 13:38:00 -04001005 if (cipher_list == NULL) {
1006 return;
1007 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001008 sk_SSL_CIPHER_free(cipher_list->ciphers);
1009 OPENSSL_free(cipher_list->in_group_flags);
1010 OPENSSL_free(cipher_list);
1011}
Adam Langley858a88d2014-06-20 12:00:00 -07001012
Adam Langleyfcf25832014-12-18 17:42:32 -08001013struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_dup(
1014 struct ssl_cipher_preference_list_st *cipher_list) {
1015 struct ssl_cipher_preference_list_st *ret = NULL;
1016 size_t n = sk_SSL_CIPHER_num(cipher_list->ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -07001017
Adam Langleyfcf25832014-12-18 17:42:32 -08001018 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
1019 if (!ret) {
1020 goto err;
1021 }
1022
1023 ret->ciphers = NULL;
1024 ret->in_group_flags = NULL;
1025 ret->ciphers = sk_SSL_CIPHER_dup(cipher_list->ciphers);
1026 if (!ret->ciphers) {
1027 goto err;
1028 }
1029 ret->in_group_flags = BUF_memdup(cipher_list->in_group_flags, n);
1030 if (!ret->in_group_flags) {
1031 goto err;
1032 }
1033
1034 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -07001035
1036err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001037 ssl_cipher_preference_list_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -08001038 return NULL;
1039}
Adam Langley858a88d2014-06-20 12:00:00 -07001040
Adam Langleyfcf25832014-12-18 17:42:32 -08001041struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_from_ciphers(
David Benjamin60da0cd2015-05-03 15:21:28 -04001042 STACK_OF(SSL_CIPHER) *ciphers) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001043 struct ssl_cipher_preference_list_st *ret = NULL;
1044 size_t n = sk_SSL_CIPHER_num(ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -07001045
Adam Langleyfcf25832014-12-18 17:42:32 -08001046 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
1047 if (!ret) {
1048 goto err;
1049 }
1050 ret->ciphers = NULL;
1051 ret->in_group_flags = NULL;
1052 ret->ciphers = sk_SSL_CIPHER_dup(ciphers);
1053 if (!ret->ciphers) {
1054 goto err;
1055 }
1056 ret->in_group_flags = OPENSSL_malloc(n);
1057 if (!ret->in_group_flags) {
1058 goto err;
1059 }
1060 memset(ret->in_group_flags, 0, n);
1061 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -07001062
1063err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001064 ssl_cipher_preference_list_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -08001065 return NULL;
1066}
Adam Langley858a88d2014-06-20 12:00:00 -07001067
Adam Langleyfcf25832014-12-18 17:42:32 -08001068X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) { return ctx->param; }
Adam Langley95c29f32014-06-20 12:00:00 -07001069
Adam Langleyfcf25832014-12-18 17:42:32 -08001070X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) { return ssl->param; }
Adam Langley95c29f32014-06-20 12:00:00 -07001071
David Benjamin7481d392015-07-05 19:38:46 -04001072void SSL_certs_clear(SSL *ssl) { ssl_cert_clear_certs(ssl->cert); }
Adam Langley95c29f32014-06-20 12:00:00 -07001073
Adam Langleyfcf25832014-12-18 17:42:32 -08001074int SSL_get_fd(const SSL *s) { return SSL_get_rfd(s); }
Adam Langley95c29f32014-06-20 12:00:00 -07001075
Adam Langleyfcf25832014-12-18 17:42:32 -08001076int SSL_get_rfd(const SSL *s) {
1077 int ret = -1;
1078 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -07001079
Adam Langleyfcf25832014-12-18 17:42:32 -08001080 b = SSL_get_rbio(s);
1081 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
1082 if (r != NULL) {
1083 BIO_get_fd(r, &ret);
1084 }
1085 return ret;
1086}
Adam Langley95c29f32014-06-20 12:00:00 -07001087
Adam Langleyfcf25832014-12-18 17:42:32 -08001088int SSL_get_wfd(const SSL *s) {
1089 int ret = -1;
1090 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -07001091
Adam Langleyfcf25832014-12-18 17:42:32 -08001092 b = SSL_get_wbio(s);
1093 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
1094 if (r != NULL) {
1095 BIO_get_fd(r, &ret);
1096 }
Adam Langley95c29f32014-06-20 12:00:00 -07001097
Adam Langleyfcf25832014-12-18 17:42:32 -08001098 return ret;
1099}
Adam Langley95c29f32014-06-20 12:00:00 -07001100
Adam Langleyfcf25832014-12-18 17:42:32 -08001101int SSL_set_fd(SSL *s, int fd) {
1102 int ret = 0;
1103 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001104
Adam Langleyfcf25832014-12-18 17:42:32 -08001105 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -07001106
Adam Langleyfcf25832014-12-18 17:42:32 -08001107 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001108 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -08001109 goto err;
1110 }
1111 BIO_set_fd(bio, fd, BIO_NOCLOSE);
1112 SSL_set_bio(s, bio, bio);
1113 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001114
Adam Langley95c29f32014-06-20 12:00:00 -07001115err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001116 return ret;
1117}
Adam Langley95c29f32014-06-20 12:00:00 -07001118
Adam Langleyfcf25832014-12-18 17:42:32 -08001119int SSL_set_wfd(SSL *s, int fd) {
1120 int ret = 0;
1121 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001122
Adam Langleyfcf25832014-12-18 17:42:32 -08001123 if (s->rbio == NULL || BIO_method_type(s->rbio) != BIO_TYPE_FD ||
David Benjamin617eac62015-10-02 14:54:20 -04001124 BIO_get_fd(s->rbio, NULL) != fd) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001125 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -07001126
Adam Langleyfcf25832014-12-18 17:42:32 -08001127 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001128 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -08001129 goto err;
1130 }
1131 BIO_set_fd(bio, fd, BIO_NOCLOSE);
1132 SSL_set_bio(s, SSL_get_rbio(s), bio);
1133 } else {
1134 SSL_set_bio(s, SSL_get_rbio(s), SSL_get_rbio(s));
1135 }
1136
1137 ret = 1;
1138
Adam Langley95c29f32014-06-20 12:00:00 -07001139err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001140 return ret;
1141}
Adam Langley95c29f32014-06-20 12:00:00 -07001142
Adam Langleyfcf25832014-12-18 17:42:32 -08001143int SSL_set_rfd(SSL *s, int fd) {
1144 int ret = 0;
1145 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001146
Adam Langleyfcf25832014-12-18 17:42:32 -08001147 if (s->wbio == NULL || BIO_method_type(s->wbio) != BIO_TYPE_FD ||
David Benjamin617eac62015-10-02 14:54:20 -04001148 BIO_get_fd(s->wbio, NULL) != fd) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001149 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -07001150
Adam Langleyfcf25832014-12-18 17:42:32 -08001151 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001152 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -08001153 goto err;
1154 }
1155 BIO_set_fd(bio, fd, BIO_NOCLOSE);
1156 SSL_set_bio(s, bio, SSL_get_wbio(s));
1157 } else {
1158 SSL_set_bio(s, SSL_get_wbio(s), SSL_get_wbio(s));
1159 }
1160 ret = 1;
1161
Adam Langley95c29f32014-06-20 12:00:00 -07001162err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001163 return ret;
1164}
Adam Langley95c29f32014-06-20 12:00:00 -07001165
1166/* return length of latest Finished message we sent, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -08001167size_t SSL_get_finished(const SSL *s, void *buf, size_t count) {
1168 size_t ret = 0;
1169
1170 if (s->s3 != NULL) {
1171 ret = s->s3->tmp.finish_md_len;
1172 if (count > ret) {
1173 count = ret;
1174 }
1175 memcpy(buf, s->s3->tmp.finish_md, count);
1176 }
1177
1178 return ret;
1179}
Adam Langley95c29f32014-06-20 12:00:00 -07001180
1181/* return length of latest Finished message we expected, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -08001182size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count) {
1183 size_t ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001184
Adam Langleyfcf25832014-12-18 17:42:32 -08001185 if (s->s3 != NULL) {
1186 ret = s->s3->tmp.peer_finish_md_len;
1187 if (count > ret) {
1188 count = ret;
1189 }
1190 memcpy(buf, s->s3->tmp.peer_finish_md, count);
1191 }
Adam Langley95c29f32014-06-20 12:00:00 -07001192
Adam Langleyfcf25832014-12-18 17:42:32 -08001193 return ret;
1194}
Adam Langley95c29f32014-06-20 12:00:00 -07001195
David Benjamin59937042015-09-19 13:04:22 -04001196int SSL_get_verify_mode(const SSL *ssl) { return ssl->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -07001197
David Benjamin59937042015-09-19 13:04:22 -04001198int SSL_get_verify_depth(const SSL *ssl) {
1199 return X509_VERIFY_PARAM_get_depth(ssl->param);
Adam Langleyfcf25832014-12-18 17:42:32 -08001200}
Adam Langley95c29f32014-06-20 12:00:00 -07001201
David Benjamin42fea372015-09-19 01:22:44 -04001202int SSL_get_extms_support(const SSL *ssl) {
1203 return ssl->s3->tmp.extended_master_secret == 1;
Matt Braithwaitecd6f54b2015-09-17 12:54:42 -07001204}
1205
David Benjamin59937042015-09-19 13:04:22 -04001206int (*SSL_get_verify_callback(const SSL *ssl))(int, X509_STORE_CTX *) {
1207 return ssl->verify_callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08001208}
Adam Langley95c29f32014-06-20 12:00:00 -07001209
Adam Langleyfcf25832014-12-18 17:42:32 -08001210int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) { return ctx->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -07001211
Adam Langleyfcf25832014-12-18 17:42:32 -08001212int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) {
1213 return X509_VERIFY_PARAM_get_depth(ctx->param);
1214}
Adam Langley95c29f32014-06-20 12:00:00 -07001215
David Benjamin59937042015-09-19 13:04:22 -04001216int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(
1217 int ok, X509_STORE_CTX *store_ctx) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001218 return ctx->default_verify_callback;
1219}
Adam Langley95c29f32014-06-20 12:00:00 -07001220
David Benjamin59937042015-09-19 13:04:22 -04001221void SSL_set_verify(SSL *ssl, int mode,
1222 int (*callback)(int ok, X509_STORE_CTX *store_ctx)) {
1223 ssl->verify_mode = mode;
Adam Langleyfcf25832014-12-18 17:42:32 -08001224 if (callback != NULL) {
David Benjamin59937042015-09-19 13:04:22 -04001225 ssl->verify_callback = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08001226 }
1227}
Adam Langley95c29f32014-06-20 12:00:00 -07001228
David Benjamin59937042015-09-19 13:04:22 -04001229void SSL_set_verify_depth(SSL *ssl, int depth) {
1230 X509_VERIFY_PARAM_set_depth(ssl->param, depth);
Adam Langleyfcf25832014-12-18 17:42:32 -08001231}
Adam Langley95c29f32014-06-20 12:00:00 -07001232
David Benjamin9a41d1b2015-05-16 01:30:09 -04001233int SSL_CTX_get_read_ahead(const SSL_CTX *ctx) { return 0; }
Adam Langley95c29f32014-06-20 12:00:00 -07001234
David Benjamin9a41d1b2015-05-16 01:30:09 -04001235int SSL_get_read_ahead(const SSL *s) { return 0; }
Adam Langley95c29f32014-06-20 12:00:00 -07001236
David Benjamin9a41d1b2015-05-16 01:30:09 -04001237void SSL_CTX_set_read_ahead(SSL_CTX *ctx, int yes) { }
David Benjamin61ecccf2015-05-05 09:44:51 -04001238
David Benjamin9a41d1b2015-05-16 01:30:09 -04001239void SSL_set_read_ahead(SSL *s, int yes) { }
David Benjamin61ecccf2015-05-05 09:44:51 -04001240
David Benjamin9f859492015-10-03 10:44:30 -04001241int SSL_pending(const SSL *ssl) {
1242 if (ssl->s3->rrec.type != SSL3_RT_APPLICATION_DATA) {
1243 return 0;
1244 }
1245 return ssl->s3->rrec.length;
Adam Langleyfcf25832014-12-18 17:42:32 -08001246}
Adam Langley95c29f32014-06-20 12:00:00 -07001247
Adam Langley95c29f32014-06-20 12:00:00 -07001248/* Fix this so it checks all the valid key/cert options */
Adam Langleyfcf25832014-12-18 17:42:32 -08001249int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
David Benjamin651b3d92015-08-09 12:07:25 -04001250 if (ctx->cert->x509 == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001251 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -08001252 return 0;
1253 }
1254
David Benjamind1d80782015-07-05 11:54:09 -04001255 if (ctx->cert->privatekey == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001256 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -08001257 return 0;
1258 }
1259
David Benjamind1d80782015-07-05 11:54:09 -04001260 return X509_check_private_key(ctx->cert->x509, ctx->cert->privatekey);
Adam Langleyfcf25832014-12-18 17:42:32 -08001261}
Adam Langley95c29f32014-06-20 12:00:00 -07001262
1263/* Fix this function so that it takes an optional type parameter */
Adam Langleyfcf25832014-12-18 17:42:32 -08001264int SSL_check_private_key(const SSL *ssl) {
David Benjamind1d80782015-07-05 11:54:09 -04001265 if (ssl->cert->x509 == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001266 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -08001267 return 0;
1268 }
David Benjamin0b145c22014-11-26 20:10:09 -05001269
David Benjamind1d80782015-07-05 11:54:09 -04001270 if (ssl->cert->privatekey == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001271 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -08001272 return 0;
1273 }
Adam Langley95c29f32014-06-20 12:00:00 -07001274
David Benjamind1d80782015-07-05 11:54:09 -04001275 return X509_check_private_key(ssl->cert->x509, ssl->cert->privatekey);
Adam Langleyfcf25832014-12-18 17:42:32 -08001276}
Adam Langley95c29f32014-06-20 12:00:00 -07001277
David Benjamin42fea372015-09-19 01:22:44 -04001278long SSL_get_default_timeout(const SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001279 return SSL_DEFAULT_SESSION_TIMEOUT;
1280}
Adam Langley95c29f32014-06-20 12:00:00 -07001281
David Benjamin44d3eed2015-05-21 01:29:55 -04001282int SSL_renegotiate(SSL *ssl) {
1283 /* Caller-initiated renegotiation is not supported. */
David Benjamin3570d732015-06-29 00:28:17 -04001284 OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
David Benjamin44d3eed2015-05-21 01:29:55 -04001285 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001286}
Adam Langley95c29f32014-06-20 12:00:00 -07001287
David Benjamin44d3eed2015-05-21 01:29:55 -04001288int SSL_renegotiate_pending(SSL *ssl) {
1289 return SSL_in_init(ssl) && ssl->s3->initial_handshake_complete;
Adam Langleyfcf25832014-12-18 17:42:32 -08001290}
Adam Langley95c29f32014-06-20 12:00:00 -07001291
David Benjamin1d0a1942015-04-26 15:35:35 -04001292size_t SSL_CTX_get_max_cert_list(const SSL_CTX *ctx) {
1293 return ctx->max_cert_list;
1294}
1295
1296void SSL_CTX_set_max_cert_list(SSL_CTX *ctx, size_t max_cert_list) {
1297 if (max_cert_list > kMaxHandshakeSize) {
1298 max_cert_list = kMaxHandshakeSize;
1299 }
1300 ctx->max_cert_list = (uint32_t)max_cert_list;
1301}
1302
1303size_t SSL_get_max_cert_list(const SSL *ssl) {
1304 return ssl->max_cert_list;
1305}
1306
1307void SSL_set_max_cert_list(SSL *ssl, size_t max_cert_list) {
1308 if (max_cert_list > kMaxHandshakeSize) {
1309 max_cert_list = kMaxHandshakeSize;
1310 }
1311 ssl->max_cert_list = (uint32_t)max_cert_list;
1312}
1313
1314void SSL_CTX_set_max_send_fragment(SSL_CTX *ctx, size_t max_send_fragment) {
1315 if (max_send_fragment < 512) {
1316 max_send_fragment = 512;
1317 }
1318 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1319 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1320 }
1321 ctx->max_send_fragment = (uint16_t)max_send_fragment;
1322}
1323
1324void SSL_set_max_send_fragment(SSL *ssl, size_t max_send_fragment) {
1325 if (max_send_fragment < 512) {
1326 max_send_fragment = 512;
1327 }
1328 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1329 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1330 }
1331 ssl->max_send_fragment = (uint16_t)max_send_fragment;
1332}
1333
David Benjamincb9cf792015-05-05 09:46:14 -04001334int SSL_set_mtu(SSL *ssl, unsigned mtu) {
1335 if (!SSL_IS_DTLS(ssl) || mtu < dtls1_min_mtu()) {
1336 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001337 }
David Benjamincb9cf792015-05-05 09:46:14 -04001338 ssl->d1->mtu = mtu;
1339 return 1;
1340}
1341
1342int SSL_get_secure_renegotiation_support(const SSL *ssl) {
1343 return ssl->s3->send_connection_binding;
1344}
1345
Adam Langleyfcf25832014-12-18 17:42:32 -08001346LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) { return ctx->sessions; }
1347
David Benjamin71f7d3d2015-05-05 09:46:38 -04001348size_t SSL_CTX_sess_number(const SSL_CTX *ctx) {
1349 return lh_SSL_SESSION_num_items(ctx->sessions);
1350}
1351
1352unsigned long SSL_CTX_sess_set_cache_size(SSL_CTX *ctx, unsigned long size) {
1353 unsigned long ret = ctx->session_cache_size;
1354 ctx->session_cache_size = size;
1355 return ret;
1356}
1357
1358unsigned long SSL_CTX_sess_get_cache_size(const SSL_CTX *ctx) {
1359 return ctx->session_cache_size;
1360}
1361
1362int SSL_CTX_set_session_cache_mode(SSL_CTX *ctx, int mode) {
1363 int ret = ctx->session_cache_mode;
1364 ctx->session_cache_mode = mode;
1365 return ret;
1366}
1367
1368int SSL_CTX_get_session_cache_mode(const SSL_CTX *ctx) {
1369 return ctx->session_cache_mode;
1370}
1371
David Benjamin32876b32015-09-20 12:17:03 -04001372STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *ssl) {
1373 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001374 return NULL;
1375 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001376
David Benjamin32876b32015-09-20 12:17:03 -04001377 if (ssl->cipher_list != NULL) {
1378 return ssl->cipher_list->ciphers;
Adam Langleyfcf25832014-12-18 17:42:32 -08001379 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001380
David Benjamin32876b32015-09-20 12:17:03 -04001381 if (ssl->version >= TLS1_1_VERSION && ssl->ctx != NULL &&
1382 ssl->ctx->cipher_list_tls11 != NULL) {
1383 return ssl->ctx->cipher_list_tls11->ciphers;
Adam Langleyfcf25832014-12-18 17:42:32 -08001384 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001385
David Benjamin32876b32015-09-20 12:17:03 -04001386 if (ssl->version >= TLS1_VERSION && ssl->ctx != NULL &&
1387 ssl->ctx->cipher_list_tls10 != NULL) {
1388 return ssl->ctx->cipher_list_tls10->ciphers;
Adam Langleycef75832015-09-03 14:51:12 -07001389 }
1390
David Benjamin32876b32015-09-20 12:17:03 -04001391 if (ssl->ctx != NULL && ssl->ctx->cipher_list != NULL) {
1392 return ssl->ctx->cipher_list->ciphers;
Adam Langleyfcf25832014-12-18 17:42:32 -08001393 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001394
Adam Langleyfcf25832014-12-18 17:42:32 -08001395 return NULL;
1396}
Adam Langley95c29f32014-06-20 12:00:00 -07001397
Adam Langleyfcf25832014-12-18 17:42:32 -08001398/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001399 * algorithm id */
David Benjamin60da0cd2015-05-03 15:21:28 -04001400STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001401 if (s == NULL) {
1402 return NULL;
1403 }
Adam Langley95c29f32014-06-20 12:00:00 -07001404
Adam Langleyfcf25832014-12-18 17:42:32 -08001405 if (s->cipher_list_by_id != NULL) {
1406 return s->cipher_list_by_id;
1407 }
Adam Langley95c29f32014-06-20 12:00:00 -07001408
Adam Langleyfcf25832014-12-18 17:42:32 -08001409 if (s->ctx != NULL && s->ctx->cipher_list_by_id != NULL) {
1410 return s->ctx->cipher_list_by_id;
1411 }
Adam Langley95c29f32014-06-20 12:00:00 -07001412
Adam Langleyfcf25832014-12-18 17:42:32 -08001413 return NULL;
1414}
Adam Langley95c29f32014-06-20 12:00:00 -07001415
David Benjamin32876b32015-09-20 12:17:03 -04001416const char *SSL_get_cipher_list(const SSL *ssl, int n) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001417 const SSL_CIPHER *c;
David Benjamin60da0cd2015-05-03 15:21:28 -04001418 STACK_OF(SSL_CIPHER) *sk;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001419
David Benjamin32876b32015-09-20 12:17:03 -04001420 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001421 return NULL;
1422 }
Adam Langley95c29f32014-06-20 12:00:00 -07001423
David Benjamin32876b32015-09-20 12:17:03 -04001424 sk = SSL_get_ciphers(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001425 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
1426 return NULL;
1427 }
Adam Langley95c29f32014-06-20 12:00:00 -07001428
Adam Langleyfcf25832014-12-18 17:42:32 -08001429 c = sk_SSL_CIPHER_value(sk, n);
1430 if (c == NULL) {
1431 return NULL;
1432 }
Adam Langley95c29f32014-06-20 12:00:00 -07001433
Adam Langleyfcf25832014-12-18 17:42:32 -08001434 return c->name;
1435}
David Benjamin5491e3f2014-09-29 19:33:09 -04001436
Adam Langleyfcf25832014-12-18 17:42:32 -08001437int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
David Benjamin32876b32015-09-20 12:17:03 -04001438 STACK_OF(SSL_CIPHER) *cipher_list = ssl_create_cipher_list(
1439 ctx->method, &ctx->cipher_list, &ctx->cipher_list_by_id, str);
1440 if (cipher_list == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001441 return 0;
David Benjamin32876b32015-09-20 12:17:03 -04001442 }
1443
1444 /* |ssl_create_cipher_list| may succeed but return an empty cipher list. */
1445 if (sk_SSL_CIPHER_num(cipher_list) == 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001446 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
Adam Langleyfcf25832014-12-18 17:42:32 -08001447 return 0;
1448 }
Adam Langley95c29f32014-06-20 12:00:00 -07001449
Adam Langleyfcf25832014-12-18 17:42:32 -08001450 return 1;
1451}
David Benjamin39482a12014-07-20 13:30:15 -04001452
Adam Langleycef75832015-09-03 14:51:12 -07001453int SSL_CTX_set_cipher_list_tls10(SSL_CTX *ctx, const char *str) {
David Benjamin32876b32015-09-20 12:17:03 -04001454 STACK_OF(SSL_CIPHER) *cipher_list = ssl_create_cipher_list(
1455 ctx->method, &ctx->cipher_list_tls10, NULL, str);
1456 if (cipher_list == NULL) {
Adam Langleycef75832015-09-03 14:51:12 -07001457 return 0;
David Benjamin32876b32015-09-20 12:17:03 -04001458 }
1459
1460 /* |ssl_create_cipher_list| may succeed but return an empty cipher list. */
1461 if (sk_SSL_CIPHER_num(cipher_list) == 0) {
Adam Langleycef75832015-09-03 14:51:12 -07001462 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
1463 return 0;
1464 }
1465
1466 return 1;
1467}
1468
Adam Langleyfcf25832014-12-18 17:42:32 -08001469int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str) {
David Benjamin32876b32015-09-20 12:17:03 -04001470 STACK_OF(SSL_CIPHER) *cipher_list = ssl_create_cipher_list(
1471 ctx->method, &ctx->cipher_list_tls11, NULL, str);
1472 if (cipher_list == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001473 return 0;
David Benjamin32876b32015-09-20 12:17:03 -04001474 }
1475
1476 /* |ssl_create_cipher_list| may succeed but return an empty cipher list. */
1477 if (sk_SSL_CIPHER_num(cipher_list) == 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001478 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
Adam Langleyfcf25832014-12-18 17:42:32 -08001479 return 0;
1480 }
David Benjamin9f2c0d72014-10-21 22:00:19 -04001481
Adam Langleyfcf25832014-12-18 17:42:32 -08001482 return 1;
1483}
Adam Langley95c29f32014-06-20 12:00:00 -07001484
David Benjamin32876b32015-09-20 12:17:03 -04001485int SSL_set_cipher_list(SSL *ssl, const char *str) {
1486 STACK_OF(SSL_CIPHER) *cipher_list = ssl_create_cipher_list(
1487 ssl->ctx->method, &ssl->cipher_list, &ssl->cipher_list_by_id, str);
1488 if (cipher_list == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001489 return 0;
David Benjamin32876b32015-09-20 12:17:03 -04001490 }
1491
1492 /* |ssl_create_cipher_list| may succeed but return an empty cipher list. */
1493 if (sk_SSL_CIPHER_num(cipher_list) == 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001494 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
Adam Langleyfcf25832014-12-18 17:42:32 -08001495 return 0;
1496 }
David Benjamin39482a12014-07-20 13:30:15 -04001497
Adam Langleyfcf25832014-12-18 17:42:32 -08001498 return 1;
1499}
Adam Langley95c29f32014-06-20 12:00:00 -07001500
Adam Langleyfcf25832014-12-18 17:42:32 -08001501int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, uint8_t *p) {
1502 size_t i;
1503 const SSL_CIPHER *c;
1504 CERT *ct = s->cert;
1505 uint8_t *q;
1506 /* Set disabled masks for this session */
1507 ssl_set_client_disabled(s);
Adam Langley29707792014-06-20 12:00:00 -07001508
Adam Langleyfcf25832014-12-18 17:42:32 -08001509 if (sk == NULL) {
1510 return 0;
1511 }
1512 q = p;
Adam Langley95c29f32014-06-20 12:00:00 -07001513
Adam Langleyfcf25832014-12-18 17:42:32 -08001514 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
1515 c = sk_SSL_CIPHER_value(sk, i);
1516 /* Skip disabled ciphers */
1517 if (c->algorithm_ssl & ct->mask_ssl ||
1518 c->algorithm_mkey & ct->mask_k ||
1519 c->algorithm_auth & ct->mask_a) {
1520 continue;
1521 }
David Benjamina1c90a52015-05-30 17:03:14 -04001522 s2n(ssl_cipher_get_value(c), p);
Adam Langleyfcf25832014-12-18 17:42:32 -08001523 }
1524
1525 /* If all ciphers were disabled, return the error to the caller. */
1526 if (p == q) {
1527 return 0;
1528 }
1529
Adam Langley5021b222015-06-12 18:27:58 -07001530 /* For SSLv3, the SCSV is added. Otherwise the renegotiation extension is
1531 * added. */
1532 if (s->client_version == SSL3_VERSION &&
1533 !s->s3->initial_handshake_complete) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001534 s2n(SSL3_CK_SCSV & 0xffff, p);
Adam Langley5021b222015-06-12 18:27:58 -07001535 /* The renegotiation extension is required to be at index zero. */
1536 s->s3->tmp.extensions.sent |= (1u << 0);
Adam Langleyfcf25832014-12-18 17:42:32 -08001537 }
1538
Adam Langley5f0efe02015-02-20 13:03:16 -08001539 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001540 s2n(SSL3_CK_FALLBACK_SCSV & 0xffff, p);
1541 }
1542
1543 return p - q;
1544}
1545
1546STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs) {
1547 CBS cipher_suites = *cbs;
1548 const SSL_CIPHER *c;
David Benjamin60da0cd2015-05-03 15:21:28 -04001549 STACK_OF(SSL_CIPHER) *sk;
Adam Langleyfcf25832014-12-18 17:42:32 -08001550
1551 if (s->s3) {
1552 s->s3->send_connection_binding = 0;
1553 }
1554
1555 if (CBS_len(&cipher_suites) % 2 != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001556 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
Adam Langleyfcf25832014-12-18 17:42:32 -08001557 return NULL;
1558 }
1559
1560 sk = sk_SSL_CIPHER_new_null();
1561 if (sk == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001562 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001563 goto err;
1564 }
1565
Adam Langleyfcf25832014-12-18 17:42:32 -08001566 while (CBS_len(&cipher_suites) > 0) {
1567 uint16_t cipher_suite;
1568
1569 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
David Benjamin3570d732015-06-29 00:28:17 -04001570 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001571 goto err;
1572 }
1573
1574 /* Check for SCSV. */
1575 if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff)) {
1576 /* SCSV is fatal if renegotiating. */
David Benjamin20f6e972015-05-15 21:51:49 -04001577 if (s->s3->initial_handshake_complete) {
David Benjamin3570d732015-06-29 00:28:17 -04001578 OPENSSL_PUT_ERROR(SSL, SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
Adam Langleyfcf25832014-12-18 17:42:32 -08001579 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1580 goto err;
1581 }
1582 s->s3->send_connection_binding = 1;
1583 continue;
1584 }
1585
1586 /* Check for FALLBACK_SCSV. */
1587 if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff)) {
1588 uint16_t max_version = ssl3_get_max_server_version(s);
1589 if (SSL_IS_DTLS(s) ? (uint16_t)s->version > max_version
1590 : (uint16_t)s->version < max_version) {
David Benjamin3570d732015-06-29 00:28:17 -04001591 OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK);
Adam Langleyfcf25832014-12-18 17:42:32 -08001592 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
1593 goto err;
1594 }
1595 continue;
1596 }
1597
David Benjamina1c90a52015-05-30 17:03:14 -04001598 c = SSL_get_cipher_by_value(cipher_suite);
Adam Langleyfcf25832014-12-18 17:42:32 -08001599 if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
David Benjamin3570d732015-06-29 00:28:17 -04001600 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001601 goto err;
1602 }
1603 }
1604
1605 return sk;
David Benjamin9f2c0d72014-10-21 22:00:19 -04001606
Adam Langley95c29f32014-06-20 12:00:00 -07001607err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001608 sk_SSL_CIPHER_free(sk);
Adam Langleyfcf25832014-12-18 17:42:32 -08001609 return NULL;
1610}
Adam Langley95c29f32014-06-20 12:00:00 -07001611
1612
Adam Langleyfcf25832014-12-18 17:42:32 -08001613/* return a servername extension value if provided in Client Hello, or NULL. So
1614 * far, only host_name types are defined (RFC 3546). */
1615const char *SSL_get_servername(const SSL *s, const int type) {
1616 if (type != TLSEXT_NAMETYPE_host_name) {
1617 return NULL;
1618 }
Adam Langley95c29f32014-06-20 12:00:00 -07001619
Adam Langleyfcf25832014-12-18 17:42:32 -08001620 return s->session && !s->tlsext_hostname ? s->session->tlsext_hostname
1621 : s->tlsext_hostname;
1622}
Adam Langley95c29f32014-06-20 12:00:00 -07001623
Adam Langleyfcf25832014-12-18 17:42:32 -08001624int SSL_get_servername_type(const SSL *s) {
1625 if (s->session &&
1626 (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname)) {
1627 return TLSEXT_NAMETYPE_host_name;
1628 }
Adam Langley95c29f32014-06-20 12:00:00 -07001629
Adam Langleyfcf25832014-12-18 17:42:32 -08001630 return -1;
1631}
Adam Langley95c29f32014-06-20 12:00:00 -07001632
Adam Langleyfcf25832014-12-18 17:42:32 -08001633void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
1634 ctx->signed_cert_timestamps_enabled = 1;
1635}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001636
Adam Langleyfcf25832014-12-18 17:42:32 -08001637int SSL_enable_signed_cert_timestamps(SSL *ssl) {
1638 ssl->signed_cert_timestamps_enabled = 1;
1639 return 1;
1640}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001641
Adam Langleyfcf25832014-12-18 17:42:32 -08001642void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
1643 ctx->ocsp_stapling_enabled = 1;
1644}
David Benjamin6c7aed02014-08-27 16:42:38 -04001645
Adam Langleyfcf25832014-12-18 17:42:32 -08001646int SSL_enable_ocsp_stapling(SSL *ssl) {
1647 ssl->ocsp_stapling_enabled = 1;
1648 return 1;
1649}
David Benjamin6c7aed02014-08-27 16:42:38 -04001650
Adam Langleyfcf25832014-12-18 17:42:32 -08001651void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
1652 size_t *out_len) {
1653 SSL_SESSION *session = ssl->session;
Adam Langley3cb50e02014-08-26 14:00:31 -07001654
Adam Langleyfcf25832014-12-18 17:42:32 -08001655 *out_len = 0;
1656 *out = NULL;
1657 if (ssl->server || !session || !session->tlsext_signed_cert_timestamp_list) {
1658 return;
1659 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001660
Adam Langleyfcf25832014-12-18 17:42:32 -08001661 *out = session->tlsext_signed_cert_timestamp_list;
1662 *out_len = session->tlsext_signed_cert_timestamp_list_length;
1663}
David Benjamin6c7aed02014-08-27 16:42:38 -04001664
Adam Langleyfcf25832014-12-18 17:42:32 -08001665void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
1666 size_t *out_len) {
1667 SSL_SESSION *session = ssl->session;
1668
1669 *out_len = 0;
1670 *out = NULL;
1671 if (ssl->server || !session || !session->ocsp_response) {
1672 return;
1673 }
1674 *out = session->ocsp_response;
1675 *out_len = session->ocsp_response_length;
1676}
David Benjamin6c7aed02014-08-27 16:42:38 -04001677
Paul Lietar4fac72e2015-09-09 13:44:55 +01001678int SSL_CTX_set_signed_cert_timestamp_list(SSL_CTX *ctx, const uint8_t *list,
1679 size_t list_len) {
1680 OPENSSL_free(ctx->signed_cert_timestamp_list);
1681 ctx->signed_cert_timestamp_list_length = 0;
1682
1683 ctx->signed_cert_timestamp_list = BUF_memdup(list, list_len);
1684 if (ctx->signed_cert_timestamp_list == NULL) {
1685 return 0;
1686 }
1687 ctx->signed_cert_timestamp_list_length = list_len;
1688
1689 return 1;
1690}
1691
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001692int SSL_CTX_set_ocsp_response(SSL_CTX *ctx, const uint8_t *response,
1693 size_t response_len) {
1694 OPENSSL_free(ctx->ocsp_response);
1695 ctx->ocsp_response_length = 0;
1696
1697 ctx->ocsp_response = BUF_memdup(response, response_len);
1698 if (ctx->ocsp_response == NULL) {
1699 return 0;
1700 }
1701 ctx->ocsp_response_length = response_len;
1702
1703 return 1;
1704}
1705
David Benjamin977547b2015-09-16 00:25:52 -04001706int SSL_select_next_proto(uint8_t **out, uint8_t *out_len,
1707 const uint8_t *server, unsigned server_len,
1708 const uint8_t *client, unsigned client_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001709 unsigned int i, j;
1710 const uint8_t *result;
1711 int status = OPENSSL_NPN_UNSUPPORTED;
Adam Langley95c29f32014-06-20 12:00:00 -07001712
Adam Langleyfcf25832014-12-18 17:42:32 -08001713 /* For each protocol in server preference order, see if we support it. */
1714 for (i = 0; i < server_len;) {
1715 for (j = 0; j < client_len;) {
1716 if (server[i] == client[j] &&
1717 memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
1718 /* We found a match */
1719 result = &server[i];
1720 status = OPENSSL_NPN_NEGOTIATED;
1721 goto found;
1722 }
1723 j += client[j];
1724 j++;
1725 }
1726 i += server[i];
1727 i++;
1728 }
Adam Langley95c29f32014-06-20 12:00:00 -07001729
Adam Langleyfcf25832014-12-18 17:42:32 -08001730 /* There's no overlap between our protocols and the server's list. */
1731 result = client;
1732 status = OPENSSL_NPN_NO_OVERLAP;
Adam Langley95c29f32014-06-20 12:00:00 -07001733
Adam Langleyfcf25832014-12-18 17:42:32 -08001734found:
1735 *out = (uint8_t *)result + 1;
David Benjamin977547b2015-09-16 00:25:52 -04001736 *out_len = result[0];
Adam Langleyfcf25832014-12-18 17:42:32 -08001737 return status;
1738}
Adam Langley95c29f32014-06-20 12:00:00 -07001739
David Benjamin977547b2015-09-16 00:25:52 -04001740void SSL_get0_next_proto_negotiated(const SSL *ssl, const uint8_t **out_data,
1741 unsigned *out_len) {
1742 *out_data = ssl->next_proto_negotiated;
1743 if (*out_data == NULL) {
1744 *out_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001745 } else {
David Benjamin977547b2015-09-16 00:25:52 -04001746 *out_len = ssl->next_proto_negotiated_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08001747 }
Adam Langley95c29f32014-06-20 12:00:00 -07001748}
1749
Adam Langleyfcf25832014-12-18 17:42:32 -08001750void SSL_CTX_set_next_protos_advertised_cb(
1751 SSL_CTX *ctx,
David Benjamin977547b2015-09-16 00:25:52 -04001752 int (*cb)(SSL *ssl, const uint8_t **out, unsigned *out_len, void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08001753 void *arg) {
1754 ctx->next_protos_advertised_cb = cb;
1755 ctx->next_protos_advertised_cb_arg = arg;
1756}
Adam Langley95c29f32014-06-20 12:00:00 -07001757
Adam Langleyfcf25832014-12-18 17:42:32 -08001758void SSL_CTX_set_next_proto_select_cb(
David Benjamin977547b2015-09-16 00:25:52 -04001759 SSL_CTX *ctx, int (*cb)(SSL *ssl, uint8_t **out, uint8_t *out_len,
1760 const uint8_t *in, unsigned in_len, void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08001761 void *arg) {
1762 ctx->next_proto_select_cb = cb;
1763 ctx->next_proto_select_cb_arg = arg;
1764}
Adam Langley95c29f32014-06-20 12:00:00 -07001765
Adam Langleyfcf25832014-12-18 17:42:32 -08001766int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
1767 unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001768 OPENSSL_free(ctx->alpn_client_proto_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001769 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1770 if (!ctx->alpn_client_proto_list) {
1771 return 1;
1772 }
1773 ctx->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001774
Adam Langleyfcf25832014-12-18 17:42:32 -08001775 return 0;
1776}
Adam Langley95c29f32014-06-20 12:00:00 -07001777
Adam Langleyfcf25832014-12-18 17:42:32 -08001778int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001779 OPENSSL_free(ssl->alpn_client_proto_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001780 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1781 if (!ssl->alpn_client_proto_list) {
1782 return 1;
1783 }
1784 ssl->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001785
Adam Langleyfcf25832014-12-18 17:42:32 -08001786 return 0;
1787}
Adam Langley95c29f32014-06-20 12:00:00 -07001788
Adam Langleyfcf25832014-12-18 17:42:32 -08001789void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
1790 int (*cb)(SSL *ssl, const uint8_t **out,
David Benjamin8984f1f2015-09-16 00:10:19 -04001791 uint8_t *out_len, const uint8_t *in,
1792 unsigned in_len, void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08001793 void *arg) {
1794 ctx->alpn_select_cb = cb;
1795 ctx->alpn_select_cb_arg = arg;
1796}
Adam Langley95c29f32014-06-20 12:00:00 -07001797
David Benjamin8984f1f2015-09-16 00:10:19 -04001798void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **out_data,
1799 unsigned *out_len) {
1800 *out_data = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08001801 if (ssl->s3) {
David Benjamin8984f1f2015-09-16 00:10:19 -04001802 *out_data = ssl->s3->alpn_selected;
Adam Langleyfcf25832014-12-18 17:42:32 -08001803 }
David Benjamin8984f1f2015-09-16 00:10:19 -04001804 if (*out_data == NULL) {
1805 *out_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001806 } else {
David Benjamin8984f1f2015-09-16 00:10:19 -04001807 *out_len = ssl->s3->alpn_selected_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08001808 }
1809}
Adam Langley95c29f32014-06-20 12:00:00 -07001810
David Benjamincfd248b2015-04-03 11:02:24 -04001811int SSL_export_keying_material(SSL *s, uint8_t *out, size_t out_len,
1812 const char *label, size_t label_len,
1813 const uint8_t *context, size_t context_len,
1814 int use_context) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001815 if (s->version < TLS1_VERSION) {
David Benjaminc565ebb2015-04-03 04:06:36 -04001816 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001817 }
Adam Langley95c29f32014-06-20 12:00:00 -07001818
David Benjamincfd248b2015-04-03 11:02:24 -04001819 return s->enc_method->export_keying_material(
1820 s, out, out_len, label, label_len, context, context_len, use_context);
Adam Langleyfcf25832014-12-18 17:42:32 -08001821}
Adam Langley95c29f32014-06-20 12:00:00 -07001822
Adam Langleyfcf25832014-12-18 17:42:32 -08001823void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
David Benjamin59937042015-09-19 13:04:22 -04001824 int (*cb)(X509_STORE_CTX *store_ctx,
1825 void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08001826 void *arg) {
1827 ctx->app_verify_callback = cb;
1828 ctx->app_verify_arg = arg;
1829}
Adam Langley95c29f32014-06-20 12:00:00 -07001830
Adam Langleyfcf25832014-12-18 17:42:32 -08001831void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
1832 int (*cb)(int, X509_STORE_CTX *)) {
1833 ctx->verify_mode = mode;
1834 ctx->default_verify_callback = cb;
1835}
Adam Langley95c29f32014-06-20 12:00:00 -07001836
Adam Langleyfcf25832014-12-18 17:42:32 -08001837void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
1838 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
1839}
Adam Langley1258b6a2014-06-20 12:00:00 -07001840
David Benjamin7481d392015-07-05 19:38:46 -04001841void SSL_CTX_set_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl, void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08001842 void *arg) {
David Benjamin7481d392015-07-05 19:38:46 -04001843 ssl_cert_set_cert_cb(ctx->cert, cb, arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08001844}
David Benjamin859ec3c2014-09-02 16:29:36 -04001845
David Benjamin7481d392015-07-05 19:38:46 -04001846void SSL_set_cert_cb(SSL *ssl, int (*cb)(SSL *ssl, void *arg), void *arg) {
1847 ssl_cert_set_cert_cb(ssl->cert, cb, arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08001848}
Adam Langley95c29f32014-06-20 12:00:00 -07001849
David Benjamin107db582015-04-08 00:41:59 -04001850void ssl_get_compatible_server_ciphers(SSL *s, uint32_t *out_mask_k,
1851 uint32_t *out_mask_a) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001852 CERT *c = s->cert;
David Benjamind1d80782015-07-05 11:54:09 -04001853 int have_rsa_cert = 0, dh_tmp;
David Benjamin107db582015-04-08 00:41:59 -04001854 uint32_t mask_k, mask_a;
David Benjamind1d80782015-07-05 11:54:09 -04001855 int have_ecc_cert = 0, ecdsa_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001856 X509 *x;
Adam Langley95c29f32014-06-20 12:00:00 -07001857
Adam Langleyfcf25832014-12-18 17:42:32 -08001858 dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001859
nagendra modadugu601448a2015-07-24 09:31:31 -07001860 if (s->cert->x509 != NULL && ssl_has_private_key(s)) {
1861 if (ssl_private_key_type(s) == EVP_PKEY_RSA) {
David Benjamind1d80782015-07-05 11:54:09 -04001862 have_rsa_cert = 1;
nagendra modadugu601448a2015-07-24 09:31:31 -07001863 } else if (ssl_private_key_type(s) == EVP_PKEY_EC) {
David Benjamind1d80782015-07-05 11:54:09 -04001864 have_ecc_cert = 1;
1865 }
1866 }
nagendra modadugu601448a2015-07-24 09:31:31 -07001867
Adam Langleyfcf25832014-12-18 17:42:32 -08001868 mask_k = 0;
1869 mask_a = 0;
David Benjaminf31e6812014-11-13 18:05:55 -05001870
Adam Langleyfcf25832014-12-18 17:42:32 -08001871 if (dh_tmp) {
David Benjamin7061e282015-03-19 11:10:48 -04001872 mask_k |= SSL_kDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001873 }
David Benjaminbb20f522015-07-04 17:18:14 -04001874 if (have_rsa_cert) {
1875 mask_k |= SSL_kRSA;
Adam Langleyfcf25832014-12-18 17:42:32 -08001876 mask_a |= SSL_aRSA;
1877 }
Adam Langley95c29f32014-06-20 12:00:00 -07001878
Adam Langleyfcf25832014-12-18 17:42:32 -08001879 /* An ECC certificate may be usable for ECDSA cipher suites depending on the
1880 * key usage extension and on the client's curve preferences. */
1881 if (have_ecc_cert) {
David Benjamind1d80782015-07-05 11:54:09 -04001882 x = c->x509;
Adam Langleyfcf25832014-12-18 17:42:32 -08001883 /* This call populates extension flags (ex_flags). */
1884 X509_check_purpose(x, -1, 0);
1885 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE)
1886 ? (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)
1887 : 1;
1888 if (!tls1_check_ec_cert(s, x)) {
1889 ecdsa_ok = 0;
1890 }
1891 if (ecdsa_ok) {
1892 mask_a |= SSL_aECDSA;
1893 }
1894 }
Adam Langley95c29f32014-06-20 12:00:00 -07001895
Adam Langleyfcf25832014-12-18 17:42:32 -08001896 /* If we are considering an ECC cipher suite that uses an ephemeral EC
1897 * key, check it. */
David Benjamindd978782015-04-24 15:20:13 -04001898 if (tls1_check_ec_tmp_key(s)) {
David Benjamin7061e282015-03-19 11:10:48 -04001899 mask_k |= SSL_kECDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001900 }
Adam Langley95c29f32014-06-20 12:00:00 -07001901
Adam Langleyfcf25832014-12-18 17:42:32 -08001902 /* PSK requires a server callback. */
1903 if (s->psk_server_callback != NULL) {
1904 mask_k |= SSL_kPSK;
1905 mask_a |= SSL_aPSK;
1906 }
Adam Langley95c29f32014-06-20 12:00:00 -07001907
Adam Langleyfcf25832014-12-18 17:42:32 -08001908 *out_mask_k = mask_k;
1909 *out_mask_a = mask_a;
1910}
Adam Langley95c29f32014-06-20 12:00:00 -07001911
Adam Langleyfcf25832014-12-18 17:42:32 -08001912void ssl_update_cache(SSL *s, int mode) {
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001913 /* Never cache sessions with empty session IDs. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001914 if (s->session->session_id_length == 0) {
1915 return;
1916 }
Adam Langley95c29f32014-06-20 12:00:00 -07001917
David Benjamin95d31822015-06-15 19:53:32 -04001918 int has_new_session = !s->hit;
1919 if (!s->server && s->tlsext_ticket_expected) {
1920 /* A client may see new sessions on abbreviated handshakes if the server
1921 * decides to renew the ticket. Once the handshake is completed, it should
1922 * be inserted into the cache. */
1923 has_new_session = 1;
1924 }
1925
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001926 SSL_CTX *ctx = s->initial_ctx;
David Benjamin95d31822015-06-15 19:53:32 -04001927 if ((ctx->session_cache_mode & mode) == mode && has_new_session &&
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001928 ((ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) ||
1929 SSL_CTX_add_session(ctx, s->session)) &&
1930 ctx->new_session_cb != NULL) {
1931 /* Note: |new_session_cb| is called whether the internal session cache is
1932 * used or not. */
1933 if (!ctx->new_session_cb(s, SSL_SESSION_up_ref(s->session))) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001934 SSL_SESSION_free(s->session);
1935 }
1936 }
Adam Langley95c29f32014-06-20 12:00:00 -07001937
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001938 if (!(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR) &&
1939 !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) &&
1940 (ctx->session_cache_mode & mode) == mode) {
1941 /* Automatically flush the internal session cache every 255 connections. */
1942 int flush_cache = 0;
Adam Langley4bdb6e42015-05-15 15:29:21 -07001943 CRYPTO_MUTEX_lock_write(&ctx->lock);
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001944 ctx->handshakes_since_cache_flush++;
1945 if (ctx->handshakes_since_cache_flush >= 255) {
1946 flush_cache = 1;
1947 ctx->handshakes_since_cache_flush = 0;
1948 }
Adam Langley4bdb6e42015-05-15 15:29:21 -07001949 CRYPTO_MUTEX_unlock(&ctx->lock);
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001950
1951 if (flush_cache) {
1952 SSL_CTX_flush_sessions(ctx, (unsigned long)time(NULL));
Adam Langleyfcf25832014-12-18 17:42:32 -08001953 }
1954 }
1955}
Adam Langley95c29f32014-06-20 12:00:00 -07001956
Adam Langleyfcf25832014-12-18 17:42:32 -08001957static const char *ssl_get_version(int version) {
1958 switch (version) {
1959 case TLS1_2_VERSION:
1960 return "TLSv1.2";
Adam Langley95c29f32014-06-20 12:00:00 -07001961
Adam Langleyfcf25832014-12-18 17:42:32 -08001962 case TLS1_1_VERSION:
1963 return "TLSv1.1";
Adam Langley95c29f32014-06-20 12:00:00 -07001964
Adam Langleyfcf25832014-12-18 17:42:32 -08001965 case TLS1_VERSION:
1966 return "TLSv1";
Adam Langley95c29f32014-06-20 12:00:00 -07001967
Adam Langleyfcf25832014-12-18 17:42:32 -08001968 case SSL3_VERSION:
1969 return "SSLv3";
Adam Langley95c29f32014-06-20 12:00:00 -07001970
David Benjamin1c722b72015-04-20 13:53:10 -04001971 case DTLS1_VERSION:
1972 return "DTLSv1";
1973
1974 case DTLS1_2_VERSION:
1975 return "DTLSv1.2";
1976
Adam Langleyfcf25832014-12-18 17:42:32 -08001977 default:
1978 return "unknown";
1979 }
1980}
Adam Langley95c29f32014-06-20 12:00:00 -07001981
David Benjamin42fea372015-09-19 01:22:44 -04001982const char *SSL_get_version(const SSL *ssl) {
1983 return ssl_get_version(ssl->version);
Adam Langleyfcf25832014-12-18 17:42:32 -08001984}
Adam Langley95c29f32014-06-20 12:00:00 -07001985
David Benjamina6b8cdf2015-09-13 14:07:33 -04001986const char *SSL_SESSION_get_version(const SSL_SESSION *session) {
1987 return ssl_get_version(session->ssl_version);
Adam Langleyfcf25832014-12-18 17:42:32 -08001988}
Adam Langley95c29f32014-06-20 12:00:00 -07001989
Sigbjorn Vik2b23d242015-06-29 15:07:26 +02001990const char* SSL_get_curve_name(uint16_t curve_id) {
1991 return tls1_ec_curve_id2name(curve_id);
1992}
1993
Adam Langleyfcf25832014-12-18 17:42:32 -08001994void ssl_clear_cipher_ctx(SSL *s) {
David Benjamin31a07792015-03-03 14:20:26 -05001995 SSL_AEAD_CTX_free(s->aead_read_ctx);
1996 s->aead_read_ctx = NULL;
1997 SSL_AEAD_CTX_free(s->aead_write_ctx);
1998 s->aead_write_ctx = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08001999}
Adam Langley95c29f32014-06-20 12:00:00 -07002000
Adam Langleyfcf25832014-12-18 17:42:32 -08002001X509 *SSL_get_certificate(const SSL *s) {
2002 if (s->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04002003 return s->cert->x509;
Adam Langleyfcf25832014-12-18 17:42:32 -08002004 }
2005
2006 return NULL;
2007}
2008
2009EVP_PKEY *SSL_get_privatekey(const SSL *s) {
2010 if (s->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04002011 return s->cert->privatekey;
Adam Langleyfcf25832014-12-18 17:42:32 -08002012 }
2013
2014 return NULL;
2015}
2016
2017X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
2018 if (ctx->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04002019 return ctx->cert->x509;
Adam Langleyfcf25832014-12-18 17:42:32 -08002020 }
2021
2022 return NULL;
2023}
2024
2025EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
2026 if (ctx->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04002027 return ctx->cert->privatekey;
Adam Langleyfcf25832014-12-18 17:42:32 -08002028 }
2029
2030 return NULL;
2031}
2032
David Benjamin42fea372015-09-19 01:22:44 -04002033const SSL_CIPHER *SSL_get_current_cipher(const SSL *ssl) {
2034 if (ssl->aead_write_ctx == NULL) {
David Benjamina07c0fc2015-05-13 13:19:42 -04002035 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002036 }
David Benjamin42fea372015-09-19 01:22:44 -04002037 return ssl->aead_write_ctx->cipher;
Adam Langleyfcf25832014-12-18 17:42:32 -08002038}
2039
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07002040const COMP_METHOD *SSL_get_current_compression(SSL *s) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002041
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07002042const COMP_METHOD *SSL_get_current_expansion(SSL *s) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002043
2044int ssl_init_wbio_buffer(SSL *s, int push) {
2045 BIO *bbio;
2046
2047 if (s->bbio == NULL) {
2048 bbio = BIO_new(BIO_f_buffer());
2049 if (bbio == NULL) {
2050 return 0;
2051 }
2052 s->bbio = bbio;
2053 } else {
2054 bbio = s->bbio;
2055 if (s->bbio == s->wbio) {
2056 s->wbio = BIO_pop(s->wbio);
2057 }
2058 }
2059
2060 BIO_reset(bbio);
2061 if (!BIO_set_read_buffer_size(bbio, 1)) {
David Benjamin3570d732015-06-29 00:28:17 -04002062 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -08002063 return 0;
2064 }
2065
2066 if (push) {
2067 if (s->wbio != bbio) {
2068 s->wbio = BIO_push(bbio, s->wbio);
2069 }
2070 } else {
2071 if (s->wbio == bbio) {
2072 s->wbio = BIO_pop(bbio);
2073 }
2074 }
2075
2076 return 1;
2077}
2078
2079void ssl_free_wbio_buffer(SSL *s) {
2080 if (s->bbio == NULL) {
2081 return;
2082 }
2083
2084 if (s->bbio == s->wbio) {
2085 /* remove buffering */
2086 s->wbio = BIO_pop(s->wbio);
2087 }
2088
2089 BIO_free(s->bbio);
2090 s->bbio = NULL;
2091}
2092
2093void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
2094 ctx->quiet_shutdown = mode;
2095}
2096
2097int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
2098 return ctx->quiet_shutdown;
2099}
2100
David Benjamin9f859492015-10-03 10:44:30 -04002101void SSL_set_quiet_shutdown(SSL *ssl, int mode) { ssl->quiet_shutdown = mode; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002102
David Benjamin9f859492015-10-03 10:44:30 -04002103int SSL_get_quiet_shutdown(const SSL *ssl) { return ssl->quiet_shutdown; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002104
David Benjamin9f859492015-10-03 10:44:30 -04002105void SSL_set_shutdown(SSL *ssl, int mode) { ssl->shutdown = mode; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002106
David Benjamin9f859492015-10-03 10:44:30 -04002107int SSL_get_shutdown(const SSL *ssl) { return ssl->shutdown; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002108
David Benjamin9f859492015-10-03 10:44:30 -04002109int SSL_version(const SSL *ssl) { return ssl->version; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002110
2111SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
2112
2113SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
2114 if (ssl->ctx == ctx) {
2115 return ssl->ctx;
2116 }
2117
2118 if (ctx == NULL) {
2119 ctx = ssl->initial_ctx;
2120 }
2121
David Benjamin2755a3e2015-04-22 16:17:58 -04002122 ssl_cert_free(ssl->cert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002123 ssl->cert = ssl_cert_dup(ctx->cert);
David Benjamin2755a3e2015-04-22 16:17:58 -04002124
Adam Langley0b5e3902015-05-15 13:08:38 -07002125 CRYPTO_refcount_inc(&ctx->references);
David Benjamin2755a3e2015-04-22 16:17:58 -04002126 SSL_CTX_free(ssl->ctx); /* decrement reference count */
Adam Langleyfcf25832014-12-18 17:42:32 -08002127 ssl->ctx = ctx;
2128
2129 ssl->sid_ctx_length = ctx->sid_ctx_length;
2130 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2131 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2132
2133 return ssl->ctx;
2134}
2135
2136int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
2137 return X509_STORE_set_default_paths(ctx->cert_store);
2138}
Adam Langley95c29f32014-06-20 12:00:00 -07002139
David Benjamin59937042015-09-19 13:04:22 -04002140int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *ca_file,
2141 const char *ca_dir) {
2142 return X509_STORE_load_locations(ctx->cert_store, ca_file, ca_dir);
Adam Langleyfcf25832014-12-18 17:42:32 -08002143}
Adam Langley95c29f32014-06-20 12:00:00 -07002144
2145void SSL_set_info_callback(SSL *ssl,
Adam Langleyfcf25832014-12-18 17:42:32 -08002146 void (*cb)(const SSL *ssl, int type, int val)) {
2147 ssl->info_callback = cb;
2148}
Adam Langley95c29f32014-06-20 12:00:00 -07002149
Adam Langleyfcf25832014-12-18 17:42:32 -08002150void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/, int /*type*/,
2151 int /*val*/) {
2152 return ssl->info_callback;
2153}
Adam Langley95c29f32014-06-20 12:00:00 -07002154
Adam Langleyfcf25832014-12-18 17:42:32 -08002155int SSL_state(const SSL *ssl) { return ssl->state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002156
David Benjaminece089c2015-05-15 23:52:42 -04002157void SSL_set_state(SSL *ssl, int state) { }
Adam Langley95c29f32014-06-20 12:00:00 -07002158
David Benjamin59937042015-09-19 13:04:22 -04002159void SSL_set_verify_result(SSL *ssl, long result) {
2160 ssl->verify_result = result;
2161}
Adam Langley95c29f32014-06-20 12:00:00 -07002162
Adam Langleyfcf25832014-12-18 17:42:32 -08002163long SSL_get_verify_result(const SSL *ssl) { return ssl->verify_result; }
Adam Langley95c29f32014-06-20 12:00:00 -07002164
Adam Langleyfcf25832014-12-18 17:42:32 -08002165int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2166 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002167 int index;
2168 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp,
2169 new_func, dup_func, free_func)) {
2170 return -1;
2171 }
2172 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002173}
Adam Langley95c29f32014-06-20 12:00:00 -07002174
David Benjamin1ac08fe2015-09-15 01:36:22 -04002175int SSL_set_ex_data(SSL *ssl, int idx, void *arg) {
2176 return CRYPTO_set_ex_data(&ssl->ex_data, idx, arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002177}
Adam Langley95c29f32014-06-20 12:00:00 -07002178
David Benjamin1ac08fe2015-09-15 01:36:22 -04002179void *SSL_get_ex_data(const SSL *ssl, int idx) {
2180 return CRYPTO_get_ex_data(&ssl->ex_data, idx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002181}
Adam Langley95c29f32014-06-20 12:00:00 -07002182
Adam Langleyfcf25832014-12-18 17:42:32 -08002183int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2184 CRYPTO_EX_dup *dup_func,
2185 CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002186 int index;
2187 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp,
2188 new_func, dup_func, free_func)) {
2189 return -1;
2190 }
2191 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002192}
Adam Langley95c29f32014-06-20 12:00:00 -07002193
David Benjamin1ac08fe2015-09-15 01:36:22 -04002194int SSL_CTX_set_ex_data(SSL_CTX *ctx, int idx, void *arg) {
2195 return CRYPTO_set_ex_data(&ctx->ex_data, idx, arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002196}
Adam Langley95c29f32014-06-20 12:00:00 -07002197
David Benjamin1ac08fe2015-09-15 01:36:22 -04002198void *SSL_CTX_get_ex_data(const SSL_CTX *ctx, int idx) {
2199 return CRYPTO_get_ex_data(&ctx->ex_data, idx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002200}
Adam Langley95c29f32014-06-20 12:00:00 -07002201
Adam Langleyfcf25832014-12-18 17:42:32 -08002202X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
2203 return ctx->cert_store;
2204}
Adam Langley95c29f32014-06-20 12:00:00 -07002205
Adam Langleyfcf25832014-12-18 17:42:32 -08002206void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
David Benjamin2755a3e2015-04-22 16:17:58 -04002207 X509_STORE_free(ctx->cert_store);
Adam Langleyfcf25832014-12-18 17:42:32 -08002208 ctx->cert_store = store;
2209}
Adam Langley95c29f32014-06-20 12:00:00 -07002210
Adam Langleyfcf25832014-12-18 17:42:32 -08002211int SSL_want(const SSL *s) { return s->rwstate; }
Adam Langley95c29f32014-06-20 12:00:00 -07002212
Adam Langleyfcf25832014-12-18 17:42:32 -08002213void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
2214 RSA *(*cb)(SSL *ssl, int is_export,
2215 int keylength)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002216}
Adam Langley95c29f32014-06-20 12:00:00 -07002217
Adam Langleyfcf25832014-12-18 17:42:32 -08002218void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
2219 int keylength)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002220}
2221
2222void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
David Benjamin59015c32015-04-26 13:13:55 -04002223 DH *(*callback)(SSL *ssl, int is_export,
2224 int keylength)) {
2225 ctx->cert->dh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002226}
2227
David Benjamin59015c32015-04-26 13:13:55 -04002228void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*callback)(SSL *ssl, int is_export,
2229 int keylength)) {
2230 ssl->cert->dh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002231}
2232
2233void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,
David Benjamindd978782015-04-24 15:20:13 -04002234 EC_KEY *(*callback)(SSL *ssl, int is_export,
2235 int keylength)) {
David Benjamin59015c32015-04-26 13:13:55 -04002236 ctx->cert->ecdh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002237}
2238
2239void SSL_set_tmp_ecdh_callback(SSL *ssl,
David Benjamindd978782015-04-24 15:20:13 -04002240 EC_KEY *(*callback)(SSL *ssl, int is_export,
2241 int keylength)) {
David Benjamin59015c32015-04-26 13:13:55 -04002242 ssl->cert->ecdh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002243}
2244
2245int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
2246 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
David Benjamin3570d732015-06-29 00:28:17 -04002247 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -08002248 return 0;
2249 }
2250
David Benjamin2755a3e2015-04-22 16:17:58 -04002251 OPENSSL_free(ctx->psk_identity_hint);
Adam Langleyfcf25832014-12-18 17:42:32 -08002252
2253 if (identity_hint != NULL) {
2254 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2255 if (ctx->psk_identity_hint == NULL) {
2256 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002257 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002258 } else {
2259 ctx->psk_identity_hint = NULL;
2260 }
Adam Langley95c29f32014-06-20 12:00:00 -07002261
Adam Langleyfcf25832014-12-18 17:42:32 -08002262 return 1;
2263}
2264
David Benjamine8814df2015-09-15 08:05:54 -04002265int SSL_use_psk_identity_hint(SSL *ssl, const char *identity_hint) {
2266 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002267 return 0;
2268 }
2269
2270 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
David Benjamin3570d732015-06-29 00:28:17 -04002271 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -08002272 return 0;
2273 }
2274
2275 /* Clear currently configured hint, if any. */
David Benjamine8814df2015-09-15 08:05:54 -04002276 OPENSSL_free(ssl->psk_identity_hint);
2277 ssl->psk_identity_hint = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002278
2279 if (identity_hint != NULL) {
David Benjamine8814df2015-09-15 08:05:54 -04002280 ssl->psk_identity_hint = BUF_strdup(identity_hint);
2281 if (ssl->psk_identity_hint == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002282 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002283 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002284 }
Adam Langley95c29f32014-06-20 12:00:00 -07002285
Adam Langleyfcf25832014-12-18 17:42:32 -08002286 return 1;
2287}
Adam Langley95c29f32014-06-20 12:00:00 -07002288
David Benjamine8814df2015-09-15 08:05:54 -04002289const char *SSL_get_psk_identity_hint(const SSL *ssl) {
2290 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002291 return NULL;
2292 }
David Benjamine8814df2015-09-15 08:05:54 -04002293 return ssl->psk_identity_hint;
Adam Langleyfcf25832014-12-18 17:42:32 -08002294}
Adam Langley95c29f32014-06-20 12:00:00 -07002295
David Benjamine8814df2015-09-15 08:05:54 -04002296const char *SSL_get_psk_identity(const SSL *ssl) {
2297 if (ssl == NULL || ssl->session == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002298 return NULL;
2299 }
Adam Langley95c29f32014-06-20 12:00:00 -07002300
David Benjamine8814df2015-09-15 08:05:54 -04002301 return ssl->session->psk_identity;
Adam Langleyfcf25832014-12-18 17:42:32 -08002302}
Adam Langley95c29f32014-06-20 12:00:00 -07002303
Adam Langleyfcf25832014-12-18 17:42:32 -08002304void SSL_set_psk_client_callback(
David Benjamine8814df2015-09-15 08:05:54 -04002305 SSL *ssl, unsigned (*cb)(SSL *ssl, const char *hint, char *identity,
2306 unsigned max_identity_len, uint8_t *psk,
2307 unsigned max_psk_len)) {
2308 ssl->psk_client_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002309}
Adam Langley95c29f32014-06-20 12:00:00 -07002310
Adam Langleyfcf25832014-12-18 17:42:32 -08002311void SSL_CTX_set_psk_client_callback(
David Benjamine8814df2015-09-15 08:05:54 -04002312 SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *hint, char *identity,
2313 unsigned max_identity_len, uint8_t *psk,
2314 unsigned max_psk_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002315 ctx->psk_client_callback = cb;
2316}
Adam Langley95c29f32014-06-20 12:00:00 -07002317
Adam Langleyfcf25832014-12-18 17:42:32 -08002318void SSL_set_psk_server_callback(
David Benjamine8814df2015-09-15 08:05:54 -04002319 SSL *ssl, unsigned (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
2320 unsigned max_psk_len)) {
2321 ssl->psk_server_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002322}
Adam Langley95c29f32014-06-20 12:00:00 -07002323
Adam Langleyfcf25832014-12-18 17:42:32 -08002324void SSL_CTX_set_psk_server_callback(
David Benjamine8814df2015-09-15 08:05:54 -04002325 SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *identity,
2326 uint8_t *psk, unsigned max_psk_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002327 ctx->psk_server_callback = cb;
2328}
Adam Langley95c29f32014-06-20 12:00:00 -07002329
Adam Langleyfcf25832014-12-18 17:42:32 -08002330void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
2331 void (*cb)(int write_p, int version,
2332 int content_type, const void *buf,
2333 size_t len, SSL *ssl, void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002334 ctx->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002335}
David Benjamin61ecccf2015-05-05 09:44:51 -04002336
2337void SSL_CTX_set_msg_callback_arg(SSL_CTX *ctx, void *arg) {
2338 ctx->msg_callback_arg = arg;
2339}
2340
Adam Langleyfcf25832014-12-18 17:42:32 -08002341void SSL_set_msg_callback(SSL *ssl,
2342 void (*cb)(int write_p, int version, int content_type,
2343 const void *buf, size_t len, SSL *ssl,
2344 void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002345 ssl->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002346}
Adam Langley95c29f32014-06-20 12:00:00 -07002347
David Benjamin61ecccf2015-05-05 09:44:51 -04002348void SSL_set_msg_callback_arg(SSL *ssl, void *arg) {
2349 ssl->msg_callback_arg = arg;
2350}
2351
Adam Langleyfcf25832014-12-18 17:42:32 -08002352void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio) {
David Benjamin2755a3e2015-04-22 16:17:58 -04002353 BIO_free(ctx->keylog_bio);
Adam Langleyfcf25832014-12-18 17:42:32 -08002354 ctx->keylog_bio = keylog_bio;
2355}
Adam Langley95c29f32014-06-20 12:00:00 -07002356
Adam Langleyfcf25832014-12-18 17:42:32 -08002357static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
2358 static const char hextable[] = "0123456789abcdef";
2359 uint8_t *out;
2360 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002361
Adam Langleyfcf25832014-12-18 17:42:32 -08002362 if (!CBB_add_space(cbb, &out, in_len * 2)) {
2363 return 0;
2364 }
Adam Langley95c29f32014-06-20 12:00:00 -07002365
Adam Langleyfcf25832014-12-18 17:42:32 -08002366 for (i = 0; i < in_len; i++) {
2367 *(out++) = (uint8_t)hextable[in[i] >> 4];
2368 *(out++) = (uint8_t)hextable[in[i] & 0xf];
2369 }
Adam Langley95c29f32014-06-20 12:00:00 -07002370
Adam Langleyfcf25832014-12-18 17:42:32 -08002371 return 1;
2372}
David Benjamin859ec3c2014-09-02 16:29:36 -04002373
2374int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -08002375 const uint8_t *encrypted_premaster,
2376 size_t encrypted_premaster_len,
2377 const uint8_t *premaster,
2378 size_t premaster_len) {
2379 BIO *bio = ctx->keylog_bio;
2380 CBB cbb;
2381 uint8_t *out;
2382 size_t out_len;
2383 int ret;
David Benjamin859ec3c2014-09-02 16:29:36 -04002384
Adam Langleyfcf25832014-12-18 17:42:32 -08002385 if (bio == NULL) {
2386 return 1;
2387 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002388
Adam Langleyfcf25832014-12-18 17:42:32 -08002389 if (encrypted_premaster_len < 8) {
David Benjamin3570d732015-06-29 00:28:17 -04002390 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08002391 return 0;
2392 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002393
David Benjamina8653202015-06-28 01:26:10 -04002394 CBB_zero(&cbb);
2395 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len * 2 + 1) ||
2396 !CBB_add_bytes(&cbb, (const uint8_t *)"RSA ", 4) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08002397 /* Only the first 8 bytes of the encrypted premaster secret are
2398 * logged. */
2399 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
2400 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2401 !cbb_add_hex(&cbb, premaster, premaster_len) ||
2402 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2403 !CBB_finish(&cbb, &out, &out_len)) {
2404 CBB_cleanup(&cbb);
2405 return 0;
2406 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002407
Adam Langley4bdb6e42015-05-15 15:29:21 -07002408 CRYPTO_MUTEX_lock_write(&ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -08002409 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
Adam Langley4bdb6e42015-05-15 15:29:21 -07002410 CRYPTO_MUTEX_unlock(&ctx->lock);
David Benjamin859ec3c2014-09-02 16:29:36 -04002411
Adam Langleyfcf25832014-12-18 17:42:32 -08002412 OPENSSL_free(out);
2413 return ret;
Adam Langley95f22882014-06-20 12:00:00 -07002414}
2415
Adam Langleyfcf25832014-12-18 17:42:32 -08002416int ssl_ctx_log_master_secret(SSL_CTX *ctx, const uint8_t *client_random,
2417 size_t client_random_len, const uint8_t *master,
2418 size_t master_len) {
2419 BIO *bio = ctx->keylog_bio;
2420 CBB cbb;
2421 uint8_t *out;
2422 size_t out_len;
2423 int ret;
Adam Langleyadb739e2014-06-20 12:00:00 -07002424
Adam Langleyfcf25832014-12-18 17:42:32 -08002425 if (bio == NULL) {
2426 return 1;
2427 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002428
Adam Langleyfcf25832014-12-18 17:42:32 -08002429 if (client_random_len != 32) {
David Benjamin3570d732015-06-29 00:28:17 -04002430 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08002431 return 0;
2432 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002433
David Benjamina8653202015-06-28 01:26:10 -04002434 CBB_zero(&cbb);
2435 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len * 2 + 1) ||
2436 !CBB_add_bytes(&cbb, (const uint8_t *)"CLIENT_RANDOM ", 14) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08002437 !cbb_add_hex(&cbb, client_random, 32) ||
2438 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2439 !cbb_add_hex(&cbb, master, master_len) ||
2440 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2441 !CBB_finish(&cbb, &out, &out_len)) {
2442 CBB_cleanup(&cbb);
2443 return 0;
2444 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002445
Adam Langley4bdb6e42015-05-15 15:29:21 -07002446 CRYPTO_MUTEX_lock_write(&ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -08002447 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
Adam Langley4bdb6e42015-05-15 15:29:21 -07002448 CRYPTO_MUTEX_unlock(&ctx->lock);
David Benjamine99e9122014-12-11 01:46:01 -05002449
Adam Langleyfcf25832014-12-18 17:42:32 -08002450 OPENSSL_free(out);
2451 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -07002452}
2453
David Benjamined7c4752015-02-16 19:16:46 -05002454int SSL_in_false_start(const SSL *s) {
2455 return s->s3->tmp.in_false_start;
2456}
2457
Adam Langleyfcf25832014-12-18 17:42:32 -08002458int SSL_cutthrough_complete(const SSL *s) {
David Benjamined7c4752015-02-16 19:16:46 -05002459 return SSL_in_false_start(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002460}
Adam Langley95c29f32014-06-20 12:00:00 -07002461
Adam Langleyfcf25832014-12-18 17:42:32 -08002462void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
2463 size_t *ssl_session_size) {
2464 *ssl_size = sizeof(SSL);
2465 *ssl_ctx_size = sizeof(SSL_CTX);
2466 *ssl_session_size = sizeof(SSL_SESSION);
2467}
Feng Lu41aa3252014-11-21 22:47:56 -08002468
David Benjamined7c4752015-02-16 19:16:46 -05002469int ssl3_can_false_start(const SSL *s) {
David Benjamin195dc782015-02-19 13:27:05 -05002470 const SSL_CIPHER *const cipher = SSL_get_current_cipher(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002471
Adam Langleye631d962015-04-01 13:11:01 -07002472 /* False Start only for TLS 1.2 with an ECDHE+AEAD cipher and ALPN or NPN. */
David Benjamin195dc782015-02-19 13:27:05 -05002473 return !SSL_IS_DTLS(s) &&
2474 SSL_version(s) >= TLS1_2_VERSION &&
2475 (s->s3->alpn_selected || s->s3->next_proto_neg_seen) &&
2476 cipher != NULL &&
Adam Langleye631d962015-04-01 13:11:01 -07002477 cipher->algorithm_mkey == SSL_kECDHE &&
David Benjamin195dc782015-02-19 13:27:05 -05002478 (cipher->algorithm_enc == SSL_AES128GCM ||
2479 cipher->algorithm_enc == SSL_AES256GCM ||
2480 cipher->algorithm_enc == SSL_CHACHA20POLY1305);
Adam Langleyfcf25832014-12-18 17:42:32 -08002481}
2482
2483const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version) {
2484 switch (version) {
2485 case SSL3_VERSION:
2486 return &SSLv3_enc_data;
2487
2488 case TLS1_VERSION:
2489 return &TLSv1_enc_data;
2490
David Benjamin9e13e1a2015-03-05 01:56:32 -05002491 case DTLS1_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002492 case TLS1_1_VERSION:
2493 return &TLSv1_1_enc_data;
2494
David Benjamin9e13e1a2015-03-05 01:56:32 -05002495 case DTLS1_2_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002496 case TLS1_2_VERSION:
2497 return &TLSv1_2_enc_data;
2498
Adam Langleyfcf25832014-12-18 17:42:32 -08002499 default:
2500 return NULL;
2501 }
2502}
2503
2504uint16_t ssl3_get_max_server_version(const SSL *s) {
2505 uint16_t max_version;
2506
2507 if (SSL_IS_DTLS(s)) {
2508 max_version = (s->max_version != 0) ? s->max_version : DTLS1_2_VERSION;
2509 if (!(s->options & SSL_OP_NO_DTLSv1_2) && DTLS1_2_VERSION >= max_version) {
2510 return DTLS1_2_VERSION;
2511 }
2512 if (!(s->options & SSL_OP_NO_DTLSv1) && DTLS1_VERSION >= max_version) {
2513 return DTLS1_VERSION;
2514 }
2515 return 0;
2516 }
2517
2518 max_version = (s->max_version != 0) ? s->max_version : TLS1_2_VERSION;
2519 if (!(s->options & SSL_OP_NO_TLSv1_2) && TLS1_2_VERSION <= max_version) {
2520 return TLS1_2_VERSION;
2521 }
2522 if (!(s->options & SSL_OP_NO_TLSv1_1) && TLS1_1_VERSION <= max_version) {
2523 return TLS1_1_VERSION;
2524 }
2525 if (!(s->options & SSL_OP_NO_TLSv1) && TLS1_VERSION <= max_version) {
2526 return TLS1_VERSION;
2527 }
2528 if (!(s->options & SSL_OP_NO_SSLv3) && SSL3_VERSION <= max_version) {
2529 return SSL3_VERSION;
2530 }
2531 return 0;
2532}
2533
2534uint16_t ssl3_get_mutual_version(SSL *s, uint16_t client_version) {
2535 uint16_t version = 0;
2536
2537 if (SSL_IS_DTLS(s)) {
2538 /* Clamp client_version to max_version. */
2539 if (s->max_version != 0 && client_version < s->max_version) {
2540 client_version = s->max_version;
2541 }
2542
2543 if (client_version <= DTLS1_2_VERSION && !(s->options & SSL_OP_NO_DTLSv1_2)) {
2544 version = DTLS1_2_VERSION;
2545 } else if (client_version <= DTLS1_VERSION &&
David Benjaminb18f0242015-03-10 18:30:08 -04002546 !(s->options & SSL_OP_NO_DTLSv1)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002547 version = DTLS1_VERSION;
2548 }
2549
2550 /* Check against min_version. */
2551 if (version != 0 && s->min_version != 0 && version > s->min_version) {
2552 return 0;
2553 }
2554 return version;
2555 } else {
2556 /* Clamp client_version to max_version. */
2557 if (s->max_version != 0 && client_version > s->max_version) {
2558 client_version = s->max_version;
2559 }
2560
2561 if (client_version >= TLS1_2_VERSION && !(s->options & SSL_OP_NO_TLSv1_2)) {
2562 version = TLS1_2_VERSION;
2563 } else if (client_version >= TLS1_1_VERSION &&
2564 !(s->options & SSL_OP_NO_TLSv1_1)) {
2565 version = TLS1_1_VERSION;
2566 } else if (client_version >= TLS1_VERSION && !(s->options & SSL_OP_NO_TLSv1)) {
2567 version = TLS1_VERSION;
2568 } else if (client_version >= SSL3_VERSION && !(s->options & SSL_OP_NO_SSLv3)) {
2569 version = SSL3_VERSION;
2570 }
2571
2572 /* Check against min_version. */
2573 if (version != 0 && s->min_version != 0 && version < s->min_version) {
2574 return 0;
2575 }
2576 return version;
2577 }
2578}
2579
2580uint16_t ssl3_get_max_client_version(SSL *s) {
David Benjamin123a8fd2015-04-26 14:16:41 -04002581 uint32_t options = s->options;
Adam Langleyfcf25832014-12-18 17:42:32 -08002582 uint16_t version = 0;
2583
2584 /* OpenSSL's API for controlling versions entails blacklisting individual
2585 * protocols. This has two problems. First, on the client, the protocol can
2586 * only express a contiguous range of versions. Second, a library consumer
2587 * trying to set a maximum version cannot disable protocol versions that get
2588 * added in a future version of the library.
2589 *
2590 * To account for both of these, OpenSSL interprets the client-side bitmask
2591 * as a min/max range by picking the lowest contiguous non-empty range of
2592 * enabled protocols. Note that this means it is impossible to set a maximum
2593 * version of TLS 1.2 in a future-proof way.
2594 *
2595 * By this scheme, the maximum version is the lowest version V such that V is
2596 * enabled and V+1 is disabled or unimplemented. */
2597 if (SSL_IS_DTLS(s)) {
2598 if (!(options & SSL_OP_NO_DTLSv1_2)) {
2599 version = DTLS1_2_VERSION;
2600 }
2601 if (!(options & SSL_OP_NO_DTLSv1) && (options & SSL_OP_NO_DTLSv1_2)) {
2602 version = DTLS1_VERSION;
2603 }
2604 if (s->max_version != 0 && version < s->max_version) {
2605 version = s->max_version;
2606 }
2607 } else {
2608 if (!(options & SSL_OP_NO_TLSv1_2)) {
2609 version = TLS1_2_VERSION;
2610 }
2611 if (!(options & SSL_OP_NO_TLSv1_1) && (options & SSL_OP_NO_TLSv1_2)) {
2612 version = TLS1_1_VERSION;
2613 }
2614 if (!(options & SSL_OP_NO_TLSv1) && (options & SSL_OP_NO_TLSv1_1)) {
2615 version = TLS1_VERSION;
2616 }
2617 if (!(options & SSL_OP_NO_SSLv3) && (options & SSL_OP_NO_TLSv1)) {
2618 version = SSL3_VERSION;
2619 }
2620 if (s->max_version != 0 && version > s->max_version) {
2621 version = s->max_version;
2622 }
2623 }
2624
2625 return version;
2626}
2627
2628int ssl3_is_version_enabled(SSL *s, uint16_t version) {
2629 if (SSL_IS_DTLS(s)) {
2630 if (s->max_version != 0 && version < s->max_version) {
2631 return 0;
2632 }
2633 if (s->min_version != 0 && version > s->min_version) {
2634 return 0;
2635 }
2636
2637 switch (version) {
2638 case DTLS1_VERSION:
2639 return !(s->options & SSL_OP_NO_DTLSv1);
2640
2641 case DTLS1_2_VERSION:
2642 return !(s->options & SSL_OP_NO_DTLSv1_2);
2643
2644 default:
2645 return 0;
2646 }
2647 } else {
2648 if (s->max_version != 0 && version > s->max_version) {
2649 return 0;
2650 }
2651 if (s->min_version != 0 && version < s->min_version) {
2652 return 0;
2653 }
2654
2655 switch (version) {
2656 case SSL3_VERSION:
2657 return !(s->options & SSL_OP_NO_SSLv3);
2658
2659 case TLS1_VERSION:
2660 return !(s->options & SSL_OP_NO_TLSv1);
2661
2662 case TLS1_1_VERSION:
2663 return !(s->options & SSL_OP_NO_TLSv1_1);
2664
2665 case TLS1_2_VERSION:
2666 return !(s->options & SSL_OP_NO_TLSv1_2);
2667
2668 default:
2669 return 0;
2670 }
2671 }
2672}
2673
David Benjaminea72bd02014-12-21 21:27:41 -05002674uint16_t ssl3_version_from_wire(SSL *s, uint16_t wire_version) {
2675 if (!SSL_IS_DTLS(s)) {
2676 return wire_version;
2677 }
2678
2679 uint16_t tls_version = ~wire_version;
2680 uint16_t version = tls_version + 0x0201;
2681 /* If either component overflowed, clamp it so comparisons still work. */
2682 if ((version >> 8) < (tls_version >> 8)) {
2683 version = 0xff00 | (version & 0xff);
2684 }
2685 if ((version & 0xff) < (tls_version & 0xff)) {
2686 version = (version & 0xff00) | 0xff;
2687 }
2688 /* DTLS 1.0 maps to TLS 1.1, not TLS 1.0. */
2689 if (version == TLS1_VERSION) {
2690 version = TLS1_1_VERSION;
2691 }
2692 return version;
2693}
2694
David Benjamin42fea372015-09-19 01:22:44 -04002695int SSL_cache_hit(SSL *ssl) { return SSL_session_reused(ssl); }
Adam Langleyfcf25832014-12-18 17:42:32 -08002696
David Benjamin42fea372015-09-19 01:22:44 -04002697int SSL_is_server(SSL *ssl) { return ssl->server; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002698
Adam Langley524e7172015-02-20 16:04:00 -08002699void SSL_CTX_set_dos_protection_cb(
2700 SSL_CTX *ctx, int (*cb)(const struct ssl_early_callback_ctx *)) {
2701 ctx->dos_protection_cb = cb;
2702}
2703
David Benjamin1d5ef3b2015-10-12 19:54:18 -04002704void SSL_set_renegotiate_mode(SSL *ssl, enum ssl_renegotiate_mode_t mode) {
2705 ssl->renegotiate_mode = mode;
2706}
2707
2708void SSL_set_reject_peer_renegotiations(SSL *ssl, int reject) {
2709 SSL_set_renegotiate_mode(
2710 ssl, reject ? ssl_renegotiate_never : ssl_renegotiate_freely);
David Benjaminb16346b2015-04-08 19:16:58 -04002711}
2712
Adam Langley3f92d212015-02-20 15:32:52 -08002713int SSL_get_rc4_state(const SSL *ssl, const RC4_KEY **read_key,
2714 const RC4_KEY **write_key) {
2715 if (ssl->aead_read_ctx == NULL || ssl->aead_write_ctx == NULL) {
2716 return 0;
2717 }
2718
2719 return EVP_AEAD_CTX_get_rc4_state(&ssl->aead_read_ctx->ctx, read_key) &&
2720 EVP_AEAD_CTX_get_rc4_state(&ssl->aead_write_ctx->ctx, write_key);
2721}
David Benjaminda881e92015-04-26 14:45:04 -04002722
David Benjamin27bbae42015-09-13 00:54:37 -04002723int SSL_clear(SSL *ssl) {
2724 if (ssl->method == NULL) {
2725 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_METHOD_SPECIFIED);
2726 return 0;
2727 }
2728
2729 if (ssl_clear_bad_session(ssl)) {
2730 SSL_SESSION_free(ssl->session);
2731 ssl->session = NULL;
2732 }
2733
2734 ssl->hit = 0;
2735 ssl->shutdown = 0;
2736
2737 /* SSL_clear may be called before or after the |ssl| is initialized in either
2738 * accept or connect state. In the latter case, SSL_clear should preserve the
2739 * half and reset |ssl->state| accordingly. */
2740 if (ssl->handshake_func != NULL) {
2741 if (ssl->server) {
2742 SSL_set_accept_state(ssl);
2743 } else {
2744 SSL_set_connect_state(ssl);
2745 }
2746 } else {
2747 assert(ssl->state == 0);
2748 }
2749
2750 /* TODO(davidben): Some state on |ssl| is reset both in |SSL_new| and
2751 * |SSL_clear| because it is per-connection state rather than configuration
2752 * state. Per-connection state should be on |ssl->s3| and |ssl->d1| so it is
2753 * naturally reset at the right points between |SSL_new|, |SSL_clear|, and
2754 * |ssl3_new|. */
2755
2756 ssl->rwstate = SSL_NOTHING;
2757
2758 BUF_MEM_free(ssl->init_buf);
2759 ssl->init_buf = NULL;
2760
2761 ssl_clear_cipher_ctx(ssl);
2762
2763 OPENSSL_free(ssl->next_proto_negotiated);
2764 ssl->next_proto_negotiated = NULL;
2765 ssl->next_proto_negotiated_len = 0;
2766
2767 /* The ssl->d1->mtu is simultaneously configuration (preserved across
2768 * clear) and connection-specific state (gets reset).
2769 *
2770 * TODO(davidben): Avoid this. */
2771 unsigned mtu = 0;
2772 if (ssl->d1 != NULL) {
2773 mtu = ssl->d1->mtu;
2774 }
2775
2776 ssl->method->ssl_free(ssl);
2777 if (!ssl->method->ssl_new(ssl)) {
2778 return 0;
2779 }
2780 ssl->enc_method = ssl3_get_enc_method(ssl->version);
2781 assert(ssl->enc_method != NULL);
2782
2783 if (SSL_IS_DTLS(ssl) && (SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
2784 ssl->d1->mtu = mtu;
2785 }
2786
2787 ssl->client_version = ssl->version;
2788
2789 return 1;
2790}
2791
David Benjaminda881e92015-04-26 14:45:04 -04002792int SSL_CTX_sess_connect(const SSL_CTX *ctx) { return 0; }
2793int SSL_CTX_sess_connect_good(const SSL_CTX *ctx) { return 0; }
2794int SSL_CTX_sess_connect_renegotiate(const SSL_CTX *ctx) { return 0; }
2795int SSL_CTX_sess_accept(const SSL_CTX *ctx) { return 0; }
2796int SSL_CTX_sess_accept_renegotiate(const SSL_CTX *ctx) { return 0; }
2797int SSL_CTX_sess_accept_good(const SSL_CTX *ctx) { return 0; }
2798int SSL_CTX_sess_hits(const SSL_CTX *ctx) { return 0; }
2799int SSL_CTX_sess_cb_hits(const SSL_CTX *ctx) { return 0; }
2800int SSL_CTX_sess_misses(const SSL_CTX *ctx) { return 0; }
2801int SSL_CTX_sess_timeouts(const SSL_CTX *ctx) { return 0; }
2802int SSL_CTX_sess_cache_full(const SSL_CTX *ctx) { return 0; }
Matt Braithwaite4838d8a2015-08-20 13:20:03 -07002803void ERR_load_SSL_strings(void) {}
David Benjamin27bbae42015-09-13 00:54:37 -04002804void SSL_load_error_strings(void) {}