blob: d1535d185d913d2861bd1ebbb00afe990b3c58c2 [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;
Adam Langley95c29f32014-06-20 12:00:00 -0700388
Adam Langleyfcf25832014-12-18 17:42:32 -0800389 s->param = X509_VERIFY_PARAM_new();
390 if (!s->param) {
391 goto err;
392 }
393 X509_VERIFY_PARAM_inherit(s->param, ctx->param);
394 s->quiet_shutdown = ctx->quiet_shutdown;
395 s->max_send_fragment = ctx->max_send_fragment;
Adam Langley95c29f32014-06-20 12:00:00 -0700396
Adam Langley0b5e3902015-05-15 13:08:38 -0700397 CRYPTO_refcount_inc(&ctx->references);
Adam Langleyfcf25832014-12-18 17:42:32 -0800398 s->ctx = ctx;
Adam Langley0b5e3902015-05-15 13:08:38 -0700399 CRYPTO_refcount_inc(&ctx->references);
Adam Langleyfcf25832014-12-18 17:42:32 -0800400 s->initial_ctx = ctx;
Adam Langley95c29f32014-06-20 12:00:00 -0700401
Adam Langleyfcf25832014-12-18 17:42:32 -0800402 if (ctx->tlsext_ellipticcurvelist) {
403 s->tlsext_ellipticcurvelist =
404 BUF_memdup(ctx->tlsext_ellipticcurvelist,
405 ctx->tlsext_ellipticcurvelist_length * 2);
406 if (!s->tlsext_ellipticcurvelist) {
407 goto err;
408 }
409 s->tlsext_ellipticcurvelist_length = ctx->tlsext_ellipticcurvelist_length;
410 }
Adam Langley95c29f32014-06-20 12:00:00 -0700411
Adam Langleyfcf25832014-12-18 17:42:32 -0800412 if (s->ctx->alpn_client_proto_list) {
413 s->alpn_client_proto_list = BUF_memdup(s->ctx->alpn_client_proto_list,
414 s->ctx->alpn_client_proto_list_len);
415 if (s->alpn_client_proto_list == NULL) {
416 goto err;
417 }
418 s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;
419 }
Adam Langley95c29f32014-06-20 12:00:00 -0700420
Adam Langleyfcf25832014-12-18 17:42:32 -0800421 s->verify_result = X509_V_OK;
422 s->method = ctx->method;
Adam Langley95c29f32014-06-20 12:00:00 -0700423
Adam Langleyfcf25832014-12-18 17:42:32 -0800424 if (!s->method->ssl_new(s)) {
425 goto err;
426 }
427 s->enc_method = ssl3_get_enc_method(s->version);
428 assert(s->enc_method != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700429
David Benjamin62fd1622015-01-11 13:30:01 -0500430 s->rwstate = SSL_NOTHING;
Adam Langley95c29f32014-06-20 12:00:00 -0700431
David Benjamin9f33fc62015-04-15 17:29:53 -0400432 CRYPTO_new_ex_data(&g_ex_data_class_ssl, s, &s->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700433
Adam Langleyfcf25832014-12-18 17:42:32 -0800434 s->psk_identity_hint = NULL;
435 if (ctx->psk_identity_hint) {
436 s->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
437 if (s->psk_identity_hint == NULL) {
438 goto err;
439 }
440 }
441 s->psk_client_callback = ctx->psk_client_callback;
442 s->psk_server_callback = ctx->psk_server_callback;
Adam Langley95c29f32014-06-20 12:00:00 -0700443
David Benjamin02ddbfd2015-01-11 13:09:11 -0500444 s->tlsext_channel_id_enabled = ctx->tlsext_channel_id_enabled;
445 if (ctx->tlsext_channel_id_private) {
David Benjamin9a10f8f2015-05-05 22:22:40 -0400446 s->tlsext_channel_id_private =
447 EVP_PKEY_up_ref(ctx->tlsext_channel_id_private);
David Benjamin02ddbfd2015-01-11 13:09:11 -0500448 }
449
Adam Langleyfcf25832014-12-18 17:42:32 -0800450 s->signed_cert_timestamps_enabled = s->ctx->signed_cert_timestamps_enabled;
451 s->ocsp_stapling_enabled = s->ctx->ocsp_stapling_enabled;
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200452
Adam Langleyfcf25832014-12-18 17:42:32 -0800453 return s;
454
Adam Langley95c29f32014-06-20 12:00:00 -0700455err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400456 SSL_free(s);
David Benjamin3570d732015-06-29 00:28:17 -0400457 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700458
Adam Langleyfcf25832014-12-18 17:42:32 -0800459 return NULL;
460}
Adam Langley95c29f32014-06-20 12:00:00 -0700461
David Benjamin27bbae42015-09-13 00:54:37 -0400462void SSL_free(SSL *ssl) {
463 if (ssl == NULL) {
464 return;
465 }
466
467 X509_VERIFY_PARAM_free(ssl->param);
468
469 CRYPTO_free_ex_data(&g_ex_data_class_ssl, ssl, &ssl->ex_data);
470
471 if (ssl->bbio != NULL) {
472 /* If the buffering BIO is in place, pop it off */
473 if (ssl->bbio == ssl->wbio) {
474 ssl->wbio = BIO_pop(ssl->wbio);
475 }
476 BIO_free(ssl->bbio);
477 ssl->bbio = NULL;
478 }
479
480 int free_wbio = ssl->wbio != ssl->rbio;
481 BIO_free_all(ssl->rbio);
482 if (free_wbio) {
483 BIO_free_all(ssl->wbio);
484 }
485
486 BUF_MEM_free(ssl->init_buf);
487
488 /* add extra stuff */
489 ssl_cipher_preference_list_free(ssl->cipher_list);
490 sk_SSL_CIPHER_free(ssl->cipher_list_by_id);
491
492 ssl_clear_bad_session(ssl);
493 SSL_SESSION_free(ssl->session);
494
495 ssl_clear_cipher_ctx(ssl);
496
497 ssl_cert_free(ssl->cert);
498
499 OPENSSL_free(ssl->tlsext_hostname);
500 SSL_CTX_free(ssl->initial_ctx);
501 OPENSSL_free(ssl->tlsext_ellipticcurvelist);
502 OPENSSL_free(ssl->alpn_client_proto_list);
503 EVP_PKEY_free(ssl->tlsext_channel_id_private);
504 OPENSSL_free(ssl->psk_identity_hint);
505 sk_X509_NAME_pop_free(ssl->client_CA, X509_NAME_free);
506 OPENSSL_free(ssl->next_proto_negotiated);
507 sk_SRTP_PROTECTION_PROFILE_free(ssl->srtp_profiles);
508
509 if (ssl->method != NULL) {
510 ssl->method->ssl_free(ssl);
511 }
512 SSL_CTX_free(ssl->ctx);
513
514 OPENSSL_free(ssl);
515}
516
517void SSL_set_connect_state(SSL *ssl) {
518 ssl->server = 0;
519 ssl->shutdown = 0;
520 ssl->state = SSL_ST_CONNECT;
521 ssl->handshake_func = ssl->method->ssl_connect;
522 /* clear the current cipher */
523 ssl_clear_cipher_ctx(ssl);
524}
525
526void SSL_set_accept_state(SSL *ssl) {
527 ssl->server = 1;
528 ssl->shutdown = 0;
529 ssl->state = SSL_ST_ACCEPT;
530 ssl->handshake_func = ssl->method->ssl_accept;
531 /* clear the current cipher */
532 ssl_clear_cipher_ctx(ssl);
533}
534
535void SSL_set_bio(SSL *ssl, BIO *rbio, BIO *wbio) {
536 /* If the output buffering BIO is still in place, remove it. */
537 if (ssl->bbio != NULL) {
538 if (ssl->wbio == ssl->bbio) {
539 ssl->wbio = ssl->wbio->next_bio;
540 ssl->bbio->next_bio = NULL;
541 }
542 }
543
544 if (ssl->rbio != rbio) {
545 BIO_free_all(ssl->rbio);
546 }
547 if (ssl->wbio != wbio && ssl->rbio != ssl->wbio) {
548 BIO_free_all(ssl->wbio);
549 }
550 ssl->rbio = rbio;
551 ssl->wbio = wbio;
552}
553
554BIO *SSL_get_rbio(const SSL *ssl) { return ssl->rbio; }
555
556BIO *SSL_get_wbio(const SSL *ssl) { return ssl->wbio; }
557
558int SSL_do_handshake(SSL *ssl) {
559 if (ssl->handshake_func == NULL) {
560 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_TYPE_NOT_SET);
561 return -1;
562 }
563
564 if (!SSL_in_init(ssl)) {
565 return 1;
566 }
567
568 return ssl->handshake_func(ssl);
569}
570
571int SSL_connect(SSL *ssl) {
572 if (ssl->handshake_func == 0) {
573 /* Not properly initialized yet */
574 SSL_set_connect_state(ssl);
575 }
576
577 if (ssl->handshake_func != ssl->method->ssl_connect) {
578 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
579 return -1;
580 }
581
582 return ssl->handshake_func(ssl);
583}
584
585int SSL_accept(SSL *ssl) {
586 if (ssl->handshake_func == 0) {
587 /* Not properly initialized yet */
588 SSL_set_accept_state(ssl);
589 }
590
591 if (ssl->handshake_func != ssl->method->ssl_accept) {
592 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
593 return -1;
594 }
595
596 return ssl->handshake_func(ssl);
597}
598
599int SSL_read(SSL *ssl, void *buf, int num) {
600 if (ssl->handshake_func == 0) {
601 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
602 return -1;
603 }
604
605 if (ssl->shutdown & SSL_RECEIVED_SHUTDOWN) {
606 ssl->rwstate = SSL_NOTHING;
607 return 0;
608 }
609
610 ERR_clear_system_error();
611 return ssl->method->ssl_read_app_data(ssl, buf, num, 0);
612}
613
614int SSL_peek(SSL *ssl, void *buf, int num) {
615 if (ssl->handshake_func == 0) {
616 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
617 return -1;
618 }
619
620 if (ssl->shutdown & SSL_RECEIVED_SHUTDOWN) {
621 return 0;
622 }
623
624 ERR_clear_system_error();
625 return ssl->method->ssl_read_app_data(ssl, buf, num, 1);
626}
627
628int SSL_write(SSL *ssl, const void *buf, int num) {
629 if (ssl->handshake_func == 0) {
630 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
631 return -1;
632 }
633
634 if (ssl->shutdown & SSL_SENT_SHUTDOWN) {
635 ssl->rwstate = SSL_NOTHING;
636 OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
637 return -1;
638 }
639
640 ERR_clear_system_error();
641 return ssl->method->ssl_write_app_data(ssl, buf, num);
642}
643
644int SSL_shutdown(SSL *ssl) {
645 /* Note that this function behaves differently from what one might expect.
646 * Return values are 0 for no success (yet), 1 for success; but calling it
647 * once is usually not enough, even if blocking I/O is used (see
648 * ssl3_shutdown). */
649
650 if (ssl->handshake_func == 0) {
651 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
652 return -1;
653 }
654
655 if (SSL_in_init(ssl)) {
656 return 1;
657 }
658
659 /* Do nothing if configured not to send a close_notify. */
660 if (ssl->quiet_shutdown) {
661 ssl->shutdown = SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN;
662 return 1;
663 }
664
665 if (!(ssl->shutdown & SSL_SENT_SHUTDOWN)) {
666 ssl->shutdown |= SSL_SENT_SHUTDOWN;
667 ssl3_send_alert(ssl, SSL3_AL_WARNING, SSL_AD_CLOSE_NOTIFY);
668
669 /* our shutdown alert has been sent now, and if it still needs to be
670 * written, ssl->s3->alert_dispatch will be true */
671 if (ssl->s3->alert_dispatch) {
672 return -1; /* return WANT_WRITE */
673 }
674 } else if (ssl->s3->alert_dispatch) {
675 /* resend it if not sent */
676 int ret = ssl->method->ssl_dispatch_alert(ssl);
677 if (ret == -1) {
678 /* we only get to return -1 here the 2nd/Nth invocation, we must have
679 * already signalled return 0 upon a previous invoation, return
680 * WANT_WRITE */
681 return ret;
682 }
683 } else if (!(ssl->shutdown & SSL_RECEIVED_SHUTDOWN)) {
684 /* If we are waiting for a close from our peer, we are closed */
685 ssl->method->ssl_read_close_notify(ssl);
686 if (!(ssl->shutdown & SSL_RECEIVED_SHUTDOWN)) {
687 return -1; /* return WANT_READ */
688 }
689 }
690
691 if (ssl->shutdown == (SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN) &&
692 !ssl->s3->alert_dispatch) {
693 return 1;
694 } else {
695 return 0;
696 }
697}
698
699int SSL_get_error(const SSL *ssl, int ret_code) {
700 int reason;
701 uint32_t err;
702 BIO *bio;
703
704 if (ret_code > 0) {
705 return SSL_ERROR_NONE;
706 }
707
708 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
709 * where we do encode the error */
710 err = ERR_peek_error();
711 if (err != 0) {
712 if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
713 return SSL_ERROR_SYSCALL;
714 }
715 return SSL_ERROR_SSL;
716 }
717
718 if (ret_code == 0) {
719 if ((ssl->shutdown & SSL_RECEIVED_SHUTDOWN) &&
720 (ssl->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) {
721 /* The socket was cleanly shut down with a close_notify. */
722 return SSL_ERROR_ZERO_RETURN;
723 }
724 /* An EOF was observed which violates the protocol, and the underlying
725 * transport does not participate in the error queue. Bubble up to the
726 * caller. */
727 return SSL_ERROR_SYSCALL;
728 }
729
730 if (SSL_want_session(ssl)) {
731 return SSL_ERROR_PENDING_SESSION;
732 }
733
734 if (SSL_want_certificate(ssl)) {
735 return SSL_ERROR_PENDING_CERTIFICATE;
736 }
737
738 if (SSL_want_read(ssl)) {
739 bio = SSL_get_rbio(ssl);
740 if (BIO_should_read(bio)) {
741 return SSL_ERROR_WANT_READ;
742 }
743
744 if (BIO_should_write(bio)) {
745 /* This one doesn't make too much sense ... We never try to write to the
746 * rbio, and an application program where rbio and wbio are separate
747 * couldn't even know what it should wait for. However if we ever set
748 * s->rwstate incorrectly (so that we have SSL_want_read(s) instead of
749 * SSL_want_write(s)) and rbio and wbio *are* the same, this test works
750 * around that bug; so it might be safer to keep it. */
751 return SSL_ERROR_WANT_WRITE;
752 }
753
754 if (BIO_should_io_special(bio)) {
755 reason = BIO_get_retry_reason(bio);
756 if (reason == BIO_RR_CONNECT) {
757 return SSL_ERROR_WANT_CONNECT;
758 }
759
760 if (reason == BIO_RR_ACCEPT) {
761 return SSL_ERROR_WANT_ACCEPT;
762 }
763
764 return SSL_ERROR_SYSCALL; /* unknown */
765 }
766 }
767
768 if (SSL_want_write(ssl)) {
769 bio = SSL_get_wbio(ssl);
770 if (BIO_should_write(bio)) {
771 return SSL_ERROR_WANT_WRITE;
772 }
773
774 if (BIO_should_read(bio)) {
775 /* See above (SSL_want_read(ssl) with BIO_should_write(bio)) */
776 return SSL_ERROR_WANT_READ;
777 }
778
779 if (BIO_should_io_special(bio)) {
780 reason = BIO_get_retry_reason(bio);
781 if (reason == BIO_RR_CONNECT) {
782 return SSL_ERROR_WANT_CONNECT;
783 }
784
785 if (reason == BIO_RR_ACCEPT) {
786 return SSL_ERROR_WANT_ACCEPT;
787 }
788
789 return SSL_ERROR_SYSCALL;
790 }
791 }
792
793 if (SSL_want_x509_lookup(ssl)) {
794 return SSL_ERROR_WANT_X509_LOOKUP;
795 }
796
797 if (SSL_want_channel_id_lookup(ssl)) {
798 return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
799 }
800
801 if (SSL_want_private_key_operation(ssl)) {
802 return SSL_ERROR_WANT_PRIVATE_KEY_OPERATION;
803 }
804
805 return SSL_ERROR_SYSCALL;
806}
807
808void SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) {
809 ctx->min_version = version;
810}
811
812void SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) {
813 ctx->max_version = version;
814}
815
816void SSL_set_min_version(SSL *ssl, uint16_t version) {
817 ssl->min_version = version;
818}
819
820void SSL_set_max_version(SSL *ssl, uint16_t version) {
821 ssl->max_version = version;
822}
823
824uint32_t SSL_CTX_set_options(SSL_CTX *ctx, uint32_t options) {
825 ctx->options |= options;
826 return ctx->options;
827}
828
829uint32_t SSL_CTX_clear_options(SSL_CTX *ctx, uint32_t options) {
830 ctx->options &= ~options;
831 return ctx->options;
832}
833
834uint32_t SSL_CTX_get_options(const SSL_CTX *ctx) { return ctx->options; }
835
836uint32_t SSL_set_options(SSL *ssl, uint32_t options) {
837 ssl->options |= options;
838 return ssl->options;
839}
840
841uint32_t SSL_clear_options(SSL *ssl, uint32_t options) {
842 ssl->options &= ~options;
843 return ssl->options;
844}
845
846uint32_t SSL_get_options(const SSL *ssl) { return ssl->options; }
847
848uint32_t SSL_CTX_set_mode(SSL_CTX *ctx, uint32_t mode) {
849 ctx->mode |= mode;
850 return ctx->mode;
851}
852
853uint32_t SSL_CTX_clear_mode(SSL_CTX *ctx, uint32_t mode) {
854 ctx->mode &= ~mode;
855 return ctx->mode;
856}
857
858uint32_t SSL_CTX_get_mode(const SSL_CTX *ctx) { return ctx->mode; }
859
860uint32_t SSL_set_mode(SSL *ssl, uint32_t mode) {
861 ssl->mode |= mode;
862 return ssl->mode;
863}
864
865uint32_t SSL_clear_mode(SSL *ssl, uint32_t mode) {
866 ssl->mode &= ~mode;
867 return ssl->mode;
868}
869
870uint32_t SSL_get_mode(const SSL *ssl) { return ssl->mode; }
871
David Benjaminee0c8272015-09-13 01:03:54 -0400872X509 *SSL_get_peer_certificate(const SSL *ssl) {
873 if (ssl == NULL || ssl->session == NULL || ssl->session->peer == NULL) {
874 return NULL;
875 }
876 return X509_up_ref(ssl->session->peer);
877}
878
879STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *ssl) {
880 if (ssl == NULL || ssl->session == NULL) {
881 return NULL;
882 }
883 return ssl->session->cert_chain;
884}
885
886int SSL_get_tls_unique(const SSL *ssl, uint8_t *out, size_t *out_len,
887 size_t max_out) {
888 /* The tls-unique value is the first Finished message in the handshake, which
889 * is the client's in a full handshake and the server's for a resumption. See
890 * https://tools.ietf.org/html/rfc5929#section-3.1. */
891 const uint8_t *finished = ssl->s3->previous_client_finished;
892 size_t finished_len = ssl->s3->previous_client_finished_len;
893 if (ssl->hit) {
894 /* tls-unique is broken for resumed sessions unless EMS is used. */
895 if (!ssl->session->extended_master_secret) {
896 goto err;
897 }
898 finished = ssl->s3->previous_server_finished;
899 finished_len = ssl->s3->previous_server_finished_len;
900 }
901
902 if (!ssl->s3->initial_handshake_complete ||
903 ssl->version < TLS1_VERSION) {
904 goto err;
905 }
906
907 *out_len = finished_len;
908 if (finished_len > max_out) {
909 *out_len = max_out;
910 }
911
912 memcpy(out, finished, *out_len);
913 return 1;
914
915err:
916 *out_len = 0;
917 memset(out, 0, max_out);
918 return 0;
919}
920
Adam Langleyfcf25832014-12-18 17:42:32 -0800921int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
David Benjamindafbdd42015-09-14 01:40:10 -0400922 unsigned sid_ctx_len) {
923 if (sid_ctx_len > sizeof(ctx->sid_ctx)) {
David Benjamin3570d732015-06-29 00:28:17 -0400924 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -0800925 return 0;
926 }
927 ctx->sid_ctx_length = sid_ctx_len;
928 memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700929
Adam Langleyfcf25832014-12-18 17:42:32 -0800930 return 1;
931}
Adam Langley95c29f32014-06-20 12:00:00 -0700932
Adam Langleyfcf25832014-12-18 17:42:32 -0800933int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
David Benjamindafbdd42015-09-14 01:40:10 -0400934 unsigned sid_ctx_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800935 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
David Benjamin3570d732015-06-29 00:28:17 -0400936 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -0800937 return 0;
938 }
939 ssl->sid_ctx_length = sid_ctx_len;
940 memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700941
Adam Langleyfcf25832014-12-18 17:42:32 -0800942 return 1;
943}
Adam Langley95c29f32014-06-20 12:00:00 -0700944
David Benjamin59937042015-09-19 13:04:22 -0400945int SSL_CTX_set_purpose(SSL_CTX *ctx, int purpose) {
946 return X509_VERIFY_PARAM_set_purpose(ctx->param, purpose);
Adam Langleyfcf25832014-12-18 17:42:32 -0800947}
948
David Benjamin59937042015-09-19 13:04:22 -0400949int SSL_set_purpose(SSL *ssl, int purpose) {
950 return X509_VERIFY_PARAM_set_purpose(ssl->param, purpose);
Adam Langleyfcf25832014-12-18 17:42:32 -0800951}
952
David Benjamin59937042015-09-19 13:04:22 -0400953int SSL_CTX_set_trust(SSL_CTX *ctx, int trust) {
954 return X509_VERIFY_PARAM_set_trust(ctx->param, trust);
Adam Langleyfcf25832014-12-18 17:42:32 -0800955}
956
David Benjamin59937042015-09-19 13:04:22 -0400957int SSL_set_trust(SSL *ssl, int trust) {
958 return X509_VERIFY_PARAM_set_trust(ssl->param, trust);
Adam Langleyfcf25832014-12-18 17:42:32 -0800959}
960
David Benjamin59937042015-09-19 13:04:22 -0400961int SSL_CTX_set1_param(SSL_CTX *ctx, const X509_VERIFY_PARAM *param) {
962 return X509_VERIFY_PARAM_set1(ctx->param, param);
Adam Langleyfcf25832014-12-18 17:42:32 -0800963}
964
David Benjamin59937042015-09-19 13:04:22 -0400965int SSL_set1_param(SSL *ssl, const X509_VERIFY_PARAM *param) {
966 return X509_VERIFY_PARAM_set1(ssl->param, param);
Adam Langleyfcf25832014-12-18 17:42:32 -0800967}
Adam Langley95c29f32014-06-20 12:00:00 -0700968
Adam Langley858a88d2014-06-20 12:00:00 -0700969void ssl_cipher_preference_list_free(
Adam Langleyfcf25832014-12-18 17:42:32 -0800970 struct ssl_cipher_preference_list_st *cipher_list) {
David Benjamin5d1ec732015-04-22 13:38:00 -0400971 if (cipher_list == NULL) {
972 return;
973 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800974 sk_SSL_CIPHER_free(cipher_list->ciphers);
975 OPENSSL_free(cipher_list->in_group_flags);
976 OPENSSL_free(cipher_list);
977}
Adam Langley858a88d2014-06-20 12:00:00 -0700978
Adam Langleyfcf25832014-12-18 17:42:32 -0800979struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_dup(
980 struct ssl_cipher_preference_list_st *cipher_list) {
981 struct ssl_cipher_preference_list_st *ret = NULL;
982 size_t n = sk_SSL_CIPHER_num(cipher_list->ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700983
Adam Langleyfcf25832014-12-18 17:42:32 -0800984 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
985 if (!ret) {
986 goto err;
987 }
988
989 ret->ciphers = NULL;
990 ret->in_group_flags = NULL;
991 ret->ciphers = sk_SSL_CIPHER_dup(cipher_list->ciphers);
992 if (!ret->ciphers) {
993 goto err;
994 }
995 ret->in_group_flags = BUF_memdup(cipher_list->in_group_flags, n);
996 if (!ret->in_group_flags) {
997 goto err;
998 }
999
1000 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -07001001
1002err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001003 ssl_cipher_preference_list_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -08001004 return NULL;
1005}
Adam Langley858a88d2014-06-20 12:00:00 -07001006
Adam Langleyfcf25832014-12-18 17:42:32 -08001007struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_from_ciphers(
David Benjamin60da0cd2015-05-03 15:21:28 -04001008 STACK_OF(SSL_CIPHER) *ciphers) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001009 struct ssl_cipher_preference_list_st *ret = NULL;
1010 size_t n = sk_SSL_CIPHER_num(ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -07001011
Adam Langleyfcf25832014-12-18 17:42:32 -08001012 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
1013 if (!ret) {
1014 goto err;
1015 }
1016 ret->ciphers = NULL;
1017 ret->in_group_flags = NULL;
1018 ret->ciphers = sk_SSL_CIPHER_dup(ciphers);
1019 if (!ret->ciphers) {
1020 goto err;
1021 }
1022 ret->in_group_flags = OPENSSL_malloc(n);
1023 if (!ret->in_group_flags) {
1024 goto err;
1025 }
1026 memset(ret->in_group_flags, 0, n);
1027 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -07001028
1029err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001030 ssl_cipher_preference_list_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -08001031 return NULL;
1032}
Adam Langley858a88d2014-06-20 12:00:00 -07001033
Adam Langleyfcf25832014-12-18 17:42:32 -08001034X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) { return ctx->param; }
Adam Langley95c29f32014-06-20 12:00:00 -07001035
Adam Langleyfcf25832014-12-18 17:42:32 -08001036X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) { return ssl->param; }
Adam Langley95c29f32014-06-20 12:00:00 -07001037
David Benjamin7481d392015-07-05 19:38:46 -04001038void SSL_certs_clear(SSL *ssl) { ssl_cert_clear_certs(ssl->cert); }
Adam Langley95c29f32014-06-20 12:00:00 -07001039
David Benjamin066fe0a2015-10-17 21:11:33 -04001040int SSL_get_fd(const SSL *ssl) { return SSL_get_rfd(ssl); }
Adam Langley95c29f32014-06-20 12:00:00 -07001041
David Benjamin066fe0a2015-10-17 21:11:33 -04001042int SSL_get_rfd(const SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001043 int ret = -1;
David Benjamin066fe0a2015-10-17 21:11:33 -04001044 BIO *b = BIO_find_type(SSL_get_rbio(ssl), BIO_TYPE_DESCRIPTOR);
1045 if (b != NULL) {
1046 BIO_get_fd(b, &ret);
Adam Langleyfcf25832014-12-18 17:42:32 -08001047 }
1048 return ret;
1049}
Adam Langley95c29f32014-06-20 12:00:00 -07001050
David Benjamin066fe0a2015-10-17 21:11:33 -04001051int SSL_get_wfd(const SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001052 int ret = -1;
David Benjamin066fe0a2015-10-17 21:11:33 -04001053 BIO *b = BIO_find_type(SSL_get_wbio(ssl), BIO_TYPE_DESCRIPTOR);
1054 if (b != NULL) {
1055 BIO_get_fd(b, &ret);
Adam Langleyfcf25832014-12-18 17:42:32 -08001056 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001057 return ret;
1058}
Adam Langley95c29f32014-06-20 12:00:00 -07001059
David Benjamin066fe0a2015-10-17 21:11:33 -04001060int SSL_set_fd(SSL *ssl, int fd) {
1061 BIO *bio = BIO_new(BIO_s_fd());
Adam Langleyfcf25832014-12-18 17:42:32 -08001062 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001063 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
David Benjamin066fe0a2015-10-17 21:11:33 -04001064 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001065 }
1066 BIO_set_fd(bio, fd, BIO_NOCLOSE);
David Benjamin066fe0a2015-10-17 21:11:33 -04001067 SSL_set_bio(ssl, bio, bio);
1068 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001069}
Adam Langley95c29f32014-06-20 12:00:00 -07001070
David Benjamin066fe0a2015-10-17 21:11:33 -04001071int SSL_set_wfd(SSL *ssl, int fd) {
1072 if (ssl->rbio == NULL ||
1073 BIO_method_type(ssl->rbio) != BIO_TYPE_FD ||
1074 BIO_get_fd(ssl->rbio, NULL) != fd) {
1075 BIO *bio = BIO_new(BIO_s_fd());
Adam Langleyfcf25832014-12-18 17:42:32 -08001076 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001077 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
David Benjamin066fe0a2015-10-17 21:11:33 -04001078 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001079 }
1080 BIO_set_fd(bio, fd, BIO_NOCLOSE);
David Benjamin066fe0a2015-10-17 21:11:33 -04001081 SSL_set_bio(ssl, SSL_get_rbio(ssl), bio);
Adam Langleyfcf25832014-12-18 17:42:32 -08001082 } else {
David Benjamin066fe0a2015-10-17 21:11:33 -04001083 SSL_set_bio(ssl, SSL_get_rbio(ssl), SSL_get_rbio(ssl));
Adam Langleyfcf25832014-12-18 17:42:32 -08001084 }
1085
David Benjamin066fe0a2015-10-17 21:11:33 -04001086 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001087}
Adam Langley95c29f32014-06-20 12:00:00 -07001088
David Benjamin066fe0a2015-10-17 21:11:33 -04001089int SSL_set_rfd(SSL *ssl, int fd) {
1090 if (ssl->wbio == NULL || BIO_method_type(ssl->wbio) != BIO_TYPE_FD ||
1091 BIO_get_fd(ssl->wbio, NULL) != fd) {
1092 BIO *bio = BIO_new(BIO_s_fd());
Adam Langleyfcf25832014-12-18 17:42:32 -08001093 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001094 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
David Benjamin066fe0a2015-10-17 21:11:33 -04001095 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001096 }
1097 BIO_set_fd(bio, fd, BIO_NOCLOSE);
David Benjamin066fe0a2015-10-17 21:11:33 -04001098 SSL_set_bio(ssl, bio, SSL_get_wbio(ssl));
Adam Langleyfcf25832014-12-18 17:42:32 -08001099 } else {
David Benjamin066fe0a2015-10-17 21:11:33 -04001100 SSL_set_bio(ssl, SSL_get_wbio(ssl), SSL_get_wbio(ssl));
Adam Langleyfcf25832014-12-18 17:42:32 -08001101 }
David Benjamin066fe0a2015-10-17 21:11:33 -04001102 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001103}
Adam Langley95c29f32014-06-20 12:00:00 -07001104
David Benjamin1a1b34d2015-10-17 12:51:52 -04001105size_t SSL_get_finished(const SSL *ssl, void *buf, size_t count) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001106 size_t ret = 0;
1107
David Benjamin1a1b34d2015-10-17 12:51:52 -04001108 if (ssl->s3 != NULL) {
1109 ret = ssl->s3->tmp.finish_md_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08001110 if (count > ret) {
1111 count = ret;
1112 }
David Benjamin1a1b34d2015-10-17 12:51:52 -04001113 memcpy(buf, ssl->s3->tmp.finish_md, count);
Adam Langleyfcf25832014-12-18 17:42:32 -08001114 }
1115
1116 return ret;
1117}
Adam Langley95c29f32014-06-20 12:00:00 -07001118
David Benjamin1a1b34d2015-10-17 12:51:52 -04001119size_t SSL_get_peer_finished(const SSL *ssl, void *buf, size_t count) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001120 size_t ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001121
David Benjamin1a1b34d2015-10-17 12:51:52 -04001122 if (ssl->s3 != NULL) {
1123 ret = ssl->s3->tmp.peer_finish_md_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08001124 if (count > ret) {
1125 count = ret;
1126 }
David Benjamin1a1b34d2015-10-17 12:51:52 -04001127 memcpy(buf, ssl->s3->tmp.peer_finish_md, count);
Adam Langleyfcf25832014-12-18 17:42:32 -08001128 }
Adam Langley95c29f32014-06-20 12:00:00 -07001129
Adam Langleyfcf25832014-12-18 17:42:32 -08001130 return ret;
1131}
Adam Langley95c29f32014-06-20 12:00:00 -07001132
David Benjamin59937042015-09-19 13:04:22 -04001133int SSL_get_verify_mode(const SSL *ssl) { return ssl->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -07001134
David Benjamin59937042015-09-19 13:04:22 -04001135int SSL_get_verify_depth(const SSL *ssl) {
1136 return X509_VERIFY_PARAM_get_depth(ssl->param);
Adam Langleyfcf25832014-12-18 17:42:32 -08001137}
Adam Langley95c29f32014-06-20 12:00:00 -07001138
David Benjamin42fea372015-09-19 01:22:44 -04001139int SSL_get_extms_support(const SSL *ssl) {
1140 return ssl->s3->tmp.extended_master_secret == 1;
Matt Braithwaitecd6f54b2015-09-17 12:54:42 -07001141}
1142
David Benjamin59937042015-09-19 13:04:22 -04001143int (*SSL_get_verify_callback(const SSL *ssl))(int, X509_STORE_CTX *) {
1144 return ssl->verify_callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08001145}
Adam Langley95c29f32014-06-20 12:00:00 -07001146
Adam Langleyfcf25832014-12-18 17:42:32 -08001147int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) { return ctx->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -07001148
Adam Langleyfcf25832014-12-18 17:42:32 -08001149int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) {
1150 return X509_VERIFY_PARAM_get_depth(ctx->param);
1151}
Adam Langley95c29f32014-06-20 12:00:00 -07001152
David Benjamin59937042015-09-19 13:04:22 -04001153int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(
1154 int ok, X509_STORE_CTX *store_ctx) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001155 return ctx->default_verify_callback;
1156}
Adam Langley95c29f32014-06-20 12:00:00 -07001157
David Benjamin59937042015-09-19 13:04:22 -04001158void SSL_set_verify(SSL *ssl, int mode,
1159 int (*callback)(int ok, X509_STORE_CTX *store_ctx)) {
1160 ssl->verify_mode = mode;
Adam Langleyfcf25832014-12-18 17:42:32 -08001161 if (callback != NULL) {
David Benjamin59937042015-09-19 13:04:22 -04001162 ssl->verify_callback = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08001163 }
1164}
Adam Langley95c29f32014-06-20 12:00:00 -07001165
David Benjamin59937042015-09-19 13:04:22 -04001166void SSL_set_verify_depth(SSL *ssl, int depth) {
1167 X509_VERIFY_PARAM_set_depth(ssl->param, depth);
Adam Langleyfcf25832014-12-18 17:42:32 -08001168}
Adam Langley95c29f32014-06-20 12:00:00 -07001169
David Benjamin9a41d1b2015-05-16 01:30:09 -04001170int SSL_CTX_get_read_ahead(const SSL_CTX *ctx) { return 0; }
Adam Langley95c29f32014-06-20 12:00:00 -07001171
David Benjamin9a41d1b2015-05-16 01:30:09 -04001172int SSL_get_read_ahead(const SSL *s) { return 0; }
Adam Langley95c29f32014-06-20 12:00:00 -07001173
David Benjamin9a41d1b2015-05-16 01:30:09 -04001174void SSL_CTX_set_read_ahead(SSL_CTX *ctx, int yes) { }
David Benjamin61ecccf2015-05-05 09:44:51 -04001175
David Benjamin9a41d1b2015-05-16 01:30:09 -04001176void SSL_set_read_ahead(SSL *s, int yes) { }
David Benjamin61ecccf2015-05-05 09:44:51 -04001177
David Benjamin9f859492015-10-03 10:44:30 -04001178int SSL_pending(const SSL *ssl) {
1179 if (ssl->s3->rrec.type != SSL3_RT_APPLICATION_DATA) {
1180 return 0;
1181 }
1182 return ssl->s3->rrec.length;
Adam Langleyfcf25832014-12-18 17:42:32 -08001183}
Adam Langley95c29f32014-06-20 12:00:00 -07001184
Adam Langley95c29f32014-06-20 12:00:00 -07001185/* Fix this so it checks all the valid key/cert options */
Adam Langleyfcf25832014-12-18 17:42:32 -08001186int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
David Benjamin651b3d92015-08-09 12:07:25 -04001187 if (ctx->cert->x509 == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001188 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -08001189 return 0;
1190 }
1191
David Benjamind1d80782015-07-05 11:54:09 -04001192 if (ctx->cert->privatekey == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001193 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -08001194 return 0;
1195 }
1196
David Benjamind1d80782015-07-05 11:54:09 -04001197 return X509_check_private_key(ctx->cert->x509, ctx->cert->privatekey);
Adam Langleyfcf25832014-12-18 17:42:32 -08001198}
Adam Langley95c29f32014-06-20 12:00:00 -07001199
1200/* Fix this function so that it takes an optional type parameter */
Adam Langleyfcf25832014-12-18 17:42:32 -08001201int SSL_check_private_key(const SSL *ssl) {
David Benjamind1d80782015-07-05 11:54:09 -04001202 if (ssl->cert->x509 == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001203 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -08001204 return 0;
1205 }
David Benjamin0b145c22014-11-26 20:10:09 -05001206
David Benjamind1d80782015-07-05 11:54:09 -04001207 if (ssl->cert->privatekey == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001208 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -08001209 return 0;
1210 }
Adam Langley95c29f32014-06-20 12:00:00 -07001211
David Benjamind1d80782015-07-05 11:54:09 -04001212 return X509_check_private_key(ssl->cert->x509, ssl->cert->privatekey);
Adam Langleyfcf25832014-12-18 17:42:32 -08001213}
Adam Langley95c29f32014-06-20 12:00:00 -07001214
David Benjamin42fea372015-09-19 01:22:44 -04001215long SSL_get_default_timeout(const SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001216 return SSL_DEFAULT_SESSION_TIMEOUT;
1217}
Adam Langley95c29f32014-06-20 12:00:00 -07001218
David Benjamin44d3eed2015-05-21 01:29:55 -04001219int SSL_renegotiate(SSL *ssl) {
1220 /* Caller-initiated renegotiation is not supported. */
David Benjamin3570d732015-06-29 00:28:17 -04001221 OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
David Benjamin44d3eed2015-05-21 01:29:55 -04001222 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001223}
Adam Langley95c29f32014-06-20 12:00:00 -07001224
David Benjamin44d3eed2015-05-21 01:29:55 -04001225int SSL_renegotiate_pending(SSL *ssl) {
1226 return SSL_in_init(ssl) && ssl->s3->initial_handshake_complete;
Adam Langleyfcf25832014-12-18 17:42:32 -08001227}
Adam Langley95c29f32014-06-20 12:00:00 -07001228
David Benjamin1d0a1942015-04-26 15:35:35 -04001229size_t SSL_CTX_get_max_cert_list(const SSL_CTX *ctx) {
1230 return ctx->max_cert_list;
1231}
1232
1233void SSL_CTX_set_max_cert_list(SSL_CTX *ctx, size_t max_cert_list) {
1234 if (max_cert_list > kMaxHandshakeSize) {
1235 max_cert_list = kMaxHandshakeSize;
1236 }
1237 ctx->max_cert_list = (uint32_t)max_cert_list;
1238}
1239
1240size_t SSL_get_max_cert_list(const SSL *ssl) {
1241 return ssl->max_cert_list;
1242}
1243
1244void SSL_set_max_cert_list(SSL *ssl, size_t max_cert_list) {
1245 if (max_cert_list > kMaxHandshakeSize) {
1246 max_cert_list = kMaxHandshakeSize;
1247 }
1248 ssl->max_cert_list = (uint32_t)max_cert_list;
1249}
1250
1251void SSL_CTX_set_max_send_fragment(SSL_CTX *ctx, size_t max_send_fragment) {
1252 if (max_send_fragment < 512) {
1253 max_send_fragment = 512;
1254 }
1255 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1256 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1257 }
1258 ctx->max_send_fragment = (uint16_t)max_send_fragment;
1259}
1260
1261void SSL_set_max_send_fragment(SSL *ssl, size_t max_send_fragment) {
1262 if (max_send_fragment < 512) {
1263 max_send_fragment = 512;
1264 }
1265 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1266 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1267 }
1268 ssl->max_send_fragment = (uint16_t)max_send_fragment;
1269}
1270
David Benjamincb9cf792015-05-05 09:46:14 -04001271int SSL_set_mtu(SSL *ssl, unsigned mtu) {
1272 if (!SSL_IS_DTLS(ssl) || mtu < dtls1_min_mtu()) {
1273 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001274 }
David Benjamincb9cf792015-05-05 09:46:14 -04001275 ssl->d1->mtu = mtu;
1276 return 1;
1277}
1278
1279int SSL_get_secure_renegotiation_support(const SSL *ssl) {
1280 return ssl->s3->send_connection_binding;
1281}
1282
Adam Langleyfcf25832014-12-18 17:42:32 -08001283LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) { return ctx->sessions; }
1284
David Benjamin71f7d3d2015-05-05 09:46:38 -04001285size_t SSL_CTX_sess_number(const SSL_CTX *ctx) {
1286 return lh_SSL_SESSION_num_items(ctx->sessions);
1287}
1288
1289unsigned long SSL_CTX_sess_set_cache_size(SSL_CTX *ctx, unsigned long size) {
1290 unsigned long ret = ctx->session_cache_size;
1291 ctx->session_cache_size = size;
1292 return ret;
1293}
1294
1295unsigned long SSL_CTX_sess_get_cache_size(const SSL_CTX *ctx) {
1296 return ctx->session_cache_size;
1297}
1298
1299int SSL_CTX_set_session_cache_mode(SSL_CTX *ctx, int mode) {
1300 int ret = ctx->session_cache_mode;
1301 ctx->session_cache_mode = mode;
1302 return ret;
1303}
1304
1305int SSL_CTX_get_session_cache_mode(const SSL_CTX *ctx) {
1306 return ctx->session_cache_mode;
1307}
1308
David Benjamin32876b32015-09-20 12:17:03 -04001309STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *ssl) {
1310 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001311 return NULL;
1312 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001313
David Benjamin32876b32015-09-20 12:17:03 -04001314 if (ssl->cipher_list != NULL) {
1315 return ssl->cipher_list->ciphers;
Adam Langleyfcf25832014-12-18 17:42:32 -08001316 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001317
David Benjamin32876b32015-09-20 12:17:03 -04001318 if (ssl->version >= TLS1_1_VERSION && ssl->ctx != NULL &&
1319 ssl->ctx->cipher_list_tls11 != NULL) {
1320 return ssl->ctx->cipher_list_tls11->ciphers;
Adam Langleyfcf25832014-12-18 17:42:32 -08001321 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001322
David Benjamin32876b32015-09-20 12:17:03 -04001323 if (ssl->version >= TLS1_VERSION && ssl->ctx != NULL &&
1324 ssl->ctx->cipher_list_tls10 != NULL) {
1325 return ssl->ctx->cipher_list_tls10->ciphers;
Adam Langleycef75832015-09-03 14:51:12 -07001326 }
1327
David Benjamin32876b32015-09-20 12:17:03 -04001328 if (ssl->ctx != NULL && ssl->ctx->cipher_list != NULL) {
1329 return ssl->ctx->cipher_list->ciphers;
Adam Langleyfcf25832014-12-18 17:42:32 -08001330 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001331
Adam Langleyfcf25832014-12-18 17:42:32 -08001332 return NULL;
1333}
Adam Langley95c29f32014-06-20 12:00:00 -07001334
Adam Langleyfcf25832014-12-18 17:42:32 -08001335/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001336 * algorithm id */
David Benjamin60da0cd2015-05-03 15:21:28 -04001337STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001338 if (s == NULL) {
1339 return NULL;
1340 }
Adam Langley95c29f32014-06-20 12:00:00 -07001341
Adam Langleyfcf25832014-12-18 17:42:32 -08001342 if (s->cipher_list_by_id != NULL) {
1343 return s->cipher_list_by_id;
1344 }
Adam Langley95c29f32014-06-20 12:00:00 -07001345
Adam Langleyfcf25832014-12-18 17:42:32 -08001346 if (s->ctx != NULL && s->ctx->cipher_list_by_id != NULL) {
1347 return s->ctx->cipher_list_by_id;
1348 }
Adam Langley95c29f32014-06-20 12:00:00 -07001349
Adam Langleyfcf25832014-12-18 17:42:32 -08001350 return NULL;
1351}
Adam Langley95c29f32014-06-20 12:00:00 -07001352
David Benjamin32876b32015-09-20 12:17:03 -04001353const char *SSL_get_cipher_list(const SSL *ssl, int n) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001354 const SSL_CIPHER *c;
David Benjamin60da0cd2015-05-03 15:21:28 -04001355 STACK_OF(SSL_CIPHER) *sk;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001356
David Benjamin32876b32015-09-20 12:17:03 -04001357 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001358 return NULL;
1359 }
Adam Langley95c29f32014-06-20 12:00:00 -07001360
David Benjamin32876b32015-09-20 12:17:03 -04001361 sk = SSL_get_ciphers(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001362 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
1363 return NULL;
1364 }
Adam Langley95c29f32014-06-20 12:00:00 -07001365
Adam Langleyfcf25832014-12-18 17:42:32 -08001366 c = sk_SSL_CIPHER_value(sk, n);
1367 if (c == NULL) {
1368 return NULL;
1369 }
Adam Langley95c29f32014-06-20 12:00:00 -07001370
Adam Langleyfcf25832014-12-18 17:42:32 -08001371 return c->name;
1372}
David Benjamin5491e3f2014-09-29 19:33:09 -04001373
Adam Langleyfcf25832014-12-18 17:42:32 -08001374int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
David Benjamin32876b32015-09-20 12:17:03 -04001375 STACK_OF(SSL_CIPHER) *cipher_list = ssl_create_cipher_list(
1376 ctx->method, &ctx->cipher_list, &ctx->cipher_list_by_id, str);
1377 if (cipher_list == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001378 return 0;
David Benjamin32876b32015-09-20 12:17:03 -04001379 }
1380
1381 /* |ssl_create_cipher_list| may succeed but return an empty cipher list. */
1382 if (sk_SSL_CIPHER_num(cipher_list) == 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001383 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
Adam Langleyfcf25832014-12-18 17:42:32 -08001384 return 0;
1385 }
Adam Langley95c29f32014-06-20 12:00:00 -07001386
Adam Langleyfcf25832014-12-18 17:42:32 -08001387 return 1;
1388}
David Benjamin39482a12014-07-20 13:30:15 -04001389
Adam Langleycef75832015-09-03 14:51:12 -07001390int SSL_CTX_set_cipher_list_tls10(SSL_CTX *ctx, const char *str) {
David Benjamin32876b32015-09-20 12:17:03 -04001391 STACK_OF(SSL_CIPHER) *cipher_list = ssl_create_cipher_list(
1392 ctx->method, &ctx->cipher_list_tls10, NULL, str);
1393 if (cipher_list == NULL) {
Adam Langleycef75832015-09-03 14:51:12 -07001394 return 0;
David Benjamin32876b32015-09-20 12:17:03 -04001395 }
1396
1397 /* |ssl_create_cipher_list| may succeed but return an empty cipher list. */
1398 if (sk_SSL_CIPHER_num(cipher_list) == 0) {
Adam Langleycef75832015-09-03 14:51:12 -07001399 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
1400 return 0;
1401 }
1402
1403 return 1;
1404}
1405
Adam Langleyfcf25832014-12-18 17:42:32 -08001406int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str) {
David Benjamin32876b32015-09-20 12:17:03 -04001407 STACK_OF(SSL_CIPHER) *cipher_list = ssl_create_cipher_list(
1408 ctx->method, &ctx->cipher_list_tls11, NULL, str);
1409 if (cipher_list == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001410 return 0;
David Benjamin32876b32015-09-20 12:17:03 -04001411 }
1412
1413 /* |ssl_create_cipher_list| may succeed but return an empty cipher list. */
1414 if (sk_SSL_CIPHER_num(cipher_list) == 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001415 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
Adam Langleyfcf25832014-12-18 17:42:32 -08001416 return 0;
1417 }
David Benjamin9f2c0d72014-10-21 22:00:19 -04001418
Adam Langleyfcf25832014-12-18 17:42:32 -08001419 return 1;
1420}
Adam Langley95c29f32014-06-20 12:00:00 -07001421
David Benjamin32876b32015-09-20 12:17:03 -04001422int SSL_set_cipher_list(SSL *ssl, const char *str) {
1423 STACK_OF(SSL_CIPHER) *cipher_list = ssl_create_cipher_list(
1424 ssl->ctx->method, &ssl->cipher_list, &ssl->cipher_list_by_id, str);
1425 if (cipher_list == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001426 return 0;
David Benjamin32876b32015-09-20 12:17:03 -04001427 }
1428
1429 /* |ssl_create_cipher_list| may succeed but return an empty cipher list. */
1430 if (sk_SSL_CIPHER_num(cipher_list) == 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001431 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
Adam Langleyfcf25832014-12-18 17:42:32 -08001432 return 0;
1433 }
David Benjamin39482a12014-07-20 13:30:15 -04001434
Adam Langleyfcf25832014-12-18 17:42:32 -08001435 return 1;
1436}
Adam Langley95c29f32014-06-20 12:00:00 -07001437
Adam Langleyfcf25832014-12-18 17:42:32 -08001438STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs) {
1439 CBS cipher_suites = *cbs;
1440 const SSL_CIPHER *c;
David Benjamin60da0cd2015-05-03 15:21:28 -04001441 STACK_OF(SSL_CIPHER) *sk;
Adam Langleyfcf25832014-12-18 17:42:32 -08001442
1443 if (s->s3) {
1444 s->s3->send_connection_binding = 0;
1445 }
1446
1447 if (CBS_len(&cipher_suites) % 2 != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001448 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
Adam Langleyfcf25832014-12-18 17:42:32 -08001449 return NULL;
1450 }
1451
1452 sk = sk_SSL_CIPHER_new_null();
1453 if (sk == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001454 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001455 goto err;
1456 }
1457
Adam Langleyfcf25832014-12-18 17:42:32 -08001458 while (CBS_len(&cipher_suites) > 0) {
1459 uint16_t cipher_suite;
1460
1461 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
David Benjamin3570d732015-06-29 00:28:17 -04001462 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001463 goto err;
1464 }
1465
1466 /* Check for SCSV. */
1467 if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff)) {
1468 /* SCSV is fatal if renegotiating. */
David Benjamin20f6e972015-05-15 21:51:49 -04001469 if (s->s3->initial_handshake_complete) {
David Benjamin3570d732015-06-29 00:28:17 -04001470 OPENSSL_PUT_ERROR(SSL, SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
Adam Langleyfcf25832014-12-18 17:42:32 -08001471 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1472 goto err;
1473 }
1474 s->s3->send_connection_binding = 1;
1475 continue;
1476 }
1477
1478 /* Check for FALLBACK_SCSV. */
1479 if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff)) {
1480 uint16_t max_version = ssl3_get_max_server_version(s);
1481 if (SSL_IS_DTLS(s) ? (uint16_t)s->version > max_version
1482 : (uint16_t)s->version < max_version) {
David Benjamin3570d732015-06-29 00:28:17 -04001483 OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK);
Adam Langleyfcf25832014-12-18 17:42:32 -08001484 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
1485 goto err;
1486 }
1487 continue;
1488 }
1489
David Benjamina1c90a52015-05-30 17:03:14 -04001490 c = SSL_get_cipher_by_value(cipher_suite);
Adam Langleyfcf25832014-12-18 17:42:32 -08001491 if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
David Benjamin3570d732015-06-29 00:28:17 -04001492 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001493 goto err;
1494 }
1495 }
1496
1497 return sk;
David Benjamin9f2c0d72014-10-21 22:00:19 -04001498
Adam Langley95c29f32014-06-20 12:00:00 -07001499err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001500 sk_SSL_CIPHER_free(sk);
Adam Langleyfcf25832014-12-18 17:42:32 -08001501 return NULL;
1502}
Adam Langley95c29f32014-06-20 12:00:00 -07001503
David Benjamin07e13842015-10-17 13:48:04 -04001504const char *SSL_get_servername(const SSL *ssl, const int type) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001505 if (type != TLSEXT_NAMETYPE_host_name) {
1506 return NULL;
1507 }
Adam Langley95c29f32014-06-20 12:00:00 -07001508
David Benjamin07e13842015-10-17 13:48:04 -04001509 /* Historically, |SSL_get_servername| was also the configuration getter
1510 * corresponding to |SSL_set_tlsext_host_name|. */
1511 if (ssl->tlsext_hostname != NULL) {
1512 return ssl->tlsext_hostname;
1513 }
1514
1515 if (ssl->session == NULL) {
1516 return NULL;
1517 }
1518 return ssl->session->tlsext_hostname;
Adam Langleyfcf25832014-12-18 17:42:32 -08001519}
Adam Langley95c29f32014-06-20 12:00:00 -07001520
David Benjamin07e13842015-10-17 13:48:04 -04001521int SSL_get_servername_type(const SSL *ssl) {
1522 if (ssl->session != NULL && ssl->session->tlsext_hostname != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001523 return TLSEXT_NAMETYPE_host_name;
1524 }
Adam Langley95c29f32014-06-20 12:00:00 -07001525
Adam Langleyfcf25832014-12-18 17:42:32 -08001526 return -1;
1527}
Adam Langley95c29f32014-06-20 12:00:00 -07001528
Adam Langleyfcf25832014-12-18 17:42:32 -08001529void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
1530 ctx->signed_cert_timestamps_enabled = 1;
1531}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001532
Adam Langleyfcf25832014-12-18 17:42:32 -08001533int SSL_enable_signed_cert_timestamps(SSL *ssl) {
1534 ssl->signed_cert_timestamps_enabled = 1;
1535 return 1;
1536}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001537
Adam Langleyfcf25832014-12-18 17:42:32 -08001538void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
1539 ctx->ocsp_stapling_enabled = 1;
1540}
David Benjamin6c7aed02014-08-27 16:42:38 -04001541
Adam Langleyfcf25832014-12-18 17:42:32 -08001542int SSL_enable_ocsp_stapling(SSL *ssl) {
1543 ssl->ocsp_stapling_enabled = 1;
1544 return 1;
1545}
David Benjamin6c7aed02014-08-27 16:42:38 -04001546
Adam Langleyfcf25832014-12-18 17:42:32 -08001547void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
1548 size_t *out_len) {
1549 SSL_SESSION *session = ssl->session;
Adam Langley3cb50e02014-08-26 14:00:31 -07001550
Adam Langleyfcf25832014-12-18 17:42:32 -08001551 *out_len = 0;
1552 *out = NULL;
1553 if (ssl->server || !session || !session->tlsext_signed_cert_timestamp_list) {
1554 return;
1555 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001556
Adam Langleyfcf25832014-12-18 17:42:32 -08001557 *out = session->tlsext_signed_cert_timestamp_list;
1558 *out_len = session->tlsext_signed_cert_timestamp_list_length;
1559}
David Benjamin6c7aed02014-08-27 16:42:38 -04001560
Adam Langleyfcf25832014-12-18 17:42:32 -08001561void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
1562 size_t *out_len) {
1563 SSL_SESSION *session = ssl->session;
1564
1565 *out_len = 0;
1566 *out = NULL;
1567 if (ssl->server || !session || !session->ocsp_response) {
1568 return;
1569 }
1570 *out = session->ocsp_response;
1571 *out_len = session->ocsp_response_length;
1572}
David Benjamin6c7aed02014-08-27 16:42:38 -04001573
Paul Lietar4fac72e2015-09-09 13:44:55 +01001574int SSL_CTX_set_signed_cert_timestamp_list(SSL_CTX *ctx, const uint8_t *list,
1575 size_t list_len) {
1576 OPENSSL_free(ctx->signed_cert_timestamp_list);
1577 ctx->signed_cert_timestamp_list_length = 0;
1578
1579 ctx->signed_cert_timestamp_list = BUF_memdup(list, list_len);
1580 if (ctx->signed_cert_timestamp_list == NULL) {
1581 return 0;
1582 }
1583 ctx->signed_cert_timestamp_list_length = list_len;
1584
1585 return 1;
1586}
1587
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001588int SSL_CTX_set_ocsp_response(SSL_CTX *ctx, const uint8_t *response,
1589 size_t response_len) {
1590 OPENSSL_free(ctx->ocsp_response);
1591 ctx->ocsp_response_length = 0;
1592
1593 ctx->ocsp_response = BUF_memdup(response, response_len);
1594 if (ctx->ocsp_response == NULL) {
1595 return 0;
1596 }
1597 ctx->ocsp_response_length = response_len;
1598
1599 return 1;
1600}
1601
David Benjamin977547b2015-09-16 00:25:52 -04001602int SSL_select_next_proto(uint8_t **out, uint8_t *out_len,
1603 const uint8_t *server, unsigned server_len,
1604 const uint8_t *client, unsigned client_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001605 unsigned int i, j;
1606 const uint8_t *result;
1607 int status = OPENSSL_NPN_UNSUPPORTED;
Adam Langley95c29f32014-06-20 12:00:00 -07001608
Adam Langleyfcf25832014-12-18 17:42:32 -08001609 /* For each protocol in server preference order, see if we support it. */
1610 for (i = 0; i < server_len;) {
1611 for (j = 0; j < client_len;) {
1612 if (server[i] == client[j] &&
1613 memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
1614 /* We found a match */
1615 result = &server[i];
1616 status = OPENSSL_NPN_NEGOTIATED;
1617 goto found;
1618 }
1619 j += client[j];
1620 j++;
1621 }
1622 i += server[i];
1623 i++;
1624 }
Adam Langley95c29f32014-06-20 12:00:00 -07001625
Adam Langleyfcf25832014-12-18 17:42:32 -08001626 /* There's no overlap between our protocols and the server's list. */
1627 result = client;
1628 status = OPENSSL_NPN_NO_OVERLAP;
Adam Langley95c29f32014-06-20 12:00:00 -07001629
Adam Langleyfcf25832014-12-18 17:42:32 -08001630found:
1631 *out = (uint8_t *)result + 1;
David Benjamin977547b2015-09-16 00:25:52 -04001632 *out_len = result[0];
Adam Langleyfcf25832014-12-18 17:42:32 -08001633 return status;
1634}
Adam Langley95c29f32014-06-20 12:00:00 -07001635
David Benjamin977547b2015-09-16 00:25:52 -04001636void SSL_get0_next_proto_negotiated(const SSL *ssl, const uint8_t **out_data,
1637 unsigned *out_len) {
1638 *out_data = ssl->next_proto_negotiated;
1639 if (*out_data == NULL) {
1640 *out_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001641 } else {
David Benjamin977547b2015-09-16 00:25:52 -04001642 *out_len = ssl->next_proto_negotiated_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08001643 }
Adam Langley95c29f32014-06-20 12:00:00 -07001644}
1645
Adam Langleyfcf25832014-12-18 17:42:32 -08001646void SSL_CTX_set_next_protos_advertised_cb(
1647 SSL_CTX *ctx,
David Benjamin977547b2015-09-16 00:25:52 -04001648 int (*cb)(SSL *ssl, const uint8_t **out, unsigned *out_len, void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08001649 void *arg) {
1650 ctx->next_protos_advertised_cb = cb;
1651 ctx->next_protos_advertised_cb_arg = arg;
1652}
Adam Langley95c29f32014-06-20 12:00:00 -07001653
Adam Langleyfcf25832014-12-18 17:42:32 -08001654void SSL_CTX_set_next_proto_select_cb(
David Benjamin977547b2015-09-16 00:25:52 -04001655 SSL_CTX *ctx, int (*cb)(SSL *ssl, uint8_t **out, uint8_t *out_len,
1656 const uint8_t *in, unsigned in_len, void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08001657 void *arg) {
1658 ctx->next_proto_select_cb = cb;
1659 ctx->next_proto_select_cb_arg = arg;
1660}
Adam Langley95c29f32014-06-20 12:00:00 -07001661
Adam Langleyfcf25832014-12-18 17:42:32 -08001662int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
1663 unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001664 OPENSSL_free(ctx->alpn_client_proto_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001665 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1666 if (!ctx->alpn_client_proto_list) {
1667 return 1;
1668 }
1669 ctx->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001670
Adam Langleyfcf25832014-12-18 17:42:32 -08001671 return 0;
1672}
Adam Langley95c29f32014-06-20 12:00:00 -07001673
Adam Langleyfcf25832014-12-18 17:42:32 -08001674int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001675 OPENSSL_free(ssl->alpn_client_proto_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001676 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1677 if (!ssl->alpn_client_proto_list) {
1678 return 1;
1679 }
1680 ssl->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001681
Adam Langleyfcf25832014-12-18 17:42:32 -08001682 return 0;
1683}
Adam Langley95c29f32014-06-20 12:00:00 -07001684
Adam Langleyfcf25832014-12-18 17:42:32 -08001685void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
1686 int (*cb)(SSL *ssl, const uint8_t **out,
David Benjamin8984f1f2015-09-16 00:10:19 -04001687 uint8_t *out_len, const uint8_t *in,
1688 unsigned in_len, void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08001689 void *arg) {
1690 ctx->alpn_select_cb = cb;
1691 ctx->alpn_select_cb_arg = arg;
1692}
Adam Langley95c29f32014-06-20 12:00:00 -07001693
David Benjamin8984f1f2015-09-16 00:10:19 -04001694void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **out_data,
1695 unsigned *out_len) {
1696 *out_data = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08001697 if (ssl->s3) {
David Benjamin8984f1f2015-09-16 00:10:19 -04001698 *out_data = ssl->s3->alpn_selected;
Adam Langleyfcf25832014-12-18 17:42:32 -08001699 }
David Benjamin8984f1f2015-09-16 00:10:19 -04001700 if (*out_data == NULL) {
1701 *out_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001702 } else {
David Benjamin8984f1f2015-09-16 00:10:19 -04001703 *out_len = ssl->s3->alpn_selected_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08001704 }
1705}
Adam Langley95c29f32014-06-20 12:00:00 -07001706
David Benjamin07e13842015-10-17 13:48:04 -04001707int SSL_export_keying_material(SSL *ssl, uint8_t *out, size_t out_len,
David Benjamincfd248b2015-04-03 11:02:24 -04001708 const char *label, size_t label_len,
1709 const uint8_t *context, size_t context_len,
1710 int use_context) {
David Benjamin07e13842015-10-17 13:48:04 -04001711 if (ssl->version < TLS1_VERSION) {
David Benjaminc565ebb2015-04-03 04:06:36 -04001712 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001713 }
Adam Langley95c29f32014-06-20 12:00:00 -07001714
David Benjamin07e13842015-10-17 13:48:04 -04001715 return ssl->enc_method->export_keying_material(
1716 ssl, out, out_len, label, label_len, context, context_len, use_context);
Adam Langleyfcf25832014-12-18 17:42:32 -08001717}
Adam Langley95c29f32014-06-20 12:00:00 -07001718
Adam Langleyfcf25832014-12-18 17:42:32 -08001719void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
David Benjamin59937042015-09-19 13:04:22 -04001720 int (*cb)(X509_STORE_CTX *store_ctx,
1721 void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08001722 void *arg) {
1723 ctx->app_verify_callback = cb;
1724 ctx->app_verify_arg = arg;
1725}
Adam Langley95c29f32014-06-20 12:00:00 -07001726
Adam Langleyfcf25832014-12-18 17:42:32 -08001727void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
1728 int (*cb)(int, X509_STORE_CTX *)) {
1729 ctx->verify_mode = mode;
1730 ctx->default_verify_callback = cb;
1731}
Adam Langley95c29f32014-06-20 12:00:00 -07001732
Adam Langleyfcf25832014-12-18 17:42:32 -08001733void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
1734 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
1735}
Adam Langley1258b6a2014-06-20 12:00:00 -07001736
David Benjamin7481d392015-07-05 19:38:46 -04001737void SSL_CTX_set_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl, void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08001738 void *arg) {
David Benjamin7481d392015-07-05 19:38:46 -04001739 ssl_cert_set_cert_cb(ctx->cert, cb, arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08001740}
David Benjamin859ec3c2014-09-02 16:29:36 -04001741
David Benjamin7481d392015-07-05 19:38:46 -04001742void SSL_set_cert_cb(SSL *ssl, int (*cb)(SSL *ssl, void *arg), void *arg) {
1743 ssl_cert_set_cert_cb(ssl->cert, cb, arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08001744}
Adam Langley95c29f32014-06-20 12:00:00 -07001745
David Benjamin107db582015-04-08 00:41:59 -04001746void ssl_get_compatible_server_ciphers(SSL *s, uint32_t *out_mask_k,
1747 uint32_t *out_mask_a) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001748 CERT *c = s->cert;
David Benjamind1d80782015-07-05 11:54:09 -04001749 int have_rsa_cert = 0, dh_tmp;
David Benjamin107db582015-04-08 00:41:59 -04001750 uint32_t mask_k, mask_a;
David Benjamind1d80782015-07-05 11:54:09 -04001751 int have_ecc_cert = 0, ecdsa_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001752 X509 *x;
Adam Langley95c29f32014-06-20 12:00:00 -07001753
Adam Langleyfcf25832014-12-18 17:42:32 -08001754 dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001755
nagendra modadugu601448a2015-07-24 09:31:31 -07001756 if (s->cert->x509 != NULL && ssl_has_private_key(s)) {
1757 if (ssl_private_key_type(s) == EVP_PKEY_RSA) {
David Benjamind1d80782015-07-05 11:54:09 -04001758 have_rsa_cert = 1;
nagendra modadugu601448a2015-07-24 09:31:31 -07001759 } else if (ssl_private_key_type(s) == EVP_PKEY_EC) {
David Benjamind1d80782015-07-05 11:54:09 -04001760 have_ecc_cert = 1;
1761 }
1762 }
nagendra modadugu601448a2015-07-24 09:31:31 -07001763
Adam Langleyfcf25832014-12-18 17:42:32 -08001764 mask_k = 0;
1765 mask_a = 0;
David Benjaminf31e6812014-11-13 18:05:55 -05001766
Adam Langleyfcf25832014-12-18 17:42:32 -08001767 if (dh_tmp) {
David Benjamin7061e282015-03-19 11:10:48 -04001768 mask_k |= SSL_kDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001769 }
David Benjaminbb20f522015-07-04 17:18:14 -04001770 if (have_rsa_cert) {
1771 mask_k |= SSL_kRSA;
Adam Langleyfcf25832014-12-18 17:42:32 -08001772 mask_a |= SSL_aRSA;
1773 }
Adam Langley95c29f32014-06-20 12:00:00 -07001774
Adam Langleyfcf25832014-12-18 17:42:32 -08001775 /* An ECC certificate may be usable for ECDSA cipher suites depending on the
1776 * key usage extension and on the client's curve preferences. */
1777 if (have_ecc_cert) {
David Benjamind1d80782015-07-05 11:54:09 -04001778 x = c->x509;
Adam Langleyfcf25832014-12-18 17:42:32 -08001779 /* This call populates extension flags (ex_flags). */
1780 X509_check_purpose(x, -1, 0);
1781 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE)
1782 ? (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)
1783 : 1;
1784 if (!tls1_check_ec_cert(s, x)) {
1785 ecdsa_ok = 0;
1786 }
1787 if (ecdsa_ok) {
1788 mask_a |= SSL_aECDSA;
1789 }
1790 }
Adam Langley95c29f32014-06-20 12:00:00 -07001791
Adam Langleyfcf25832014-12-18 17:42:32 -08001792 /* If we are considering an ECC cipher suite that uses an ephemeral EC
1793 * key, check it. */
David Benjamindd978782015-04-24 15:20:13 -04001794 if (tls1_check_ec_tmp_key(s)) {
David Benjamin7061e282015-03-19 11:10:48 -04001795 mask_k |= SSL_kECDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001796 }
Adam Langley95c29f32014-06-20 12:00:00 -07001797
Adam Langleyfcf25832014-12-18 17:42:32 -08001798 /* PSK requires a server callback. */
1799 if (s->psk_server_callback != NULL) {
1800 mask_k |= SSL_kPSK;
1801 mask_a |= SSL_aPSK;
1802 }
Adam Langley95c29f32014-06-20 12:00:00 -07001803
Adam Langleyfcf25832014-12-18 17:42:32 -08001804 *out_mask_k = mask_k;
1805 *out_mask_a = mask_a;
1806}
Adam Langley95c29f32014-06-20 12:00:00 -07001807
David Benjamin1269ddd2015-10-18 15:18:55 -04001808void ssl_update_cache(SSL *ssl, int mode) {
1809 SSL_CTX *ctx = ssl->initial_ctx;
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001810 /* Never cache sessions with empty session IDs. */
David Benjamin1269ddd2015-10-18 15:18:55 -04001811 if (ssl->session->session_id_length == 0 ||
1812 (ctx->session_cache_mode & mode) != mode) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001813 return;
1814 }
Adam Langley95c29f32014-06-20 12:00:00 -07001815
David Benjamin1269ddd2015-10-18 15:18:55 -04001816 /* Clients never use the internal session cache. */
1817 int use_internal_cache = ssl->server && !(ctx->session_cache_mode &
1818 SSL_SESS_CACHE_NO_INTERNAL_STORE);
David Benjamin95d31822015-06-15 19:53:32 -04001819
David Benjamin1269ddd2015-10-18 15:18:55 -04001820 /* A client may see new sessions on abbreviated handshakes if the server
1821 * decides to renew the ticket. Once the handshake is completed, it should be
1822 * inserted into the cache. */
1823 if (!ssl->hit || (!ssl->server && ssl->tlsext_ticket_expected)) {
1824 if (use_internal_cache) {
1825 SSL_CTX_add_session(ctx, ssl->session);
1826 }
1827 if (ctx->new_session_cb != NULL &&
1828 !ctx->new_session_cb(ssl, SSL_SESSION_up_ref(ssl->session))) {
1829 /* |new_session_cb|'s return value signals whether it took ownership. */
1830 SSL_SESSION_free(ssl->session);
Adam Langleyfcf25832014-12-18 17:42:32 -08001831 }
1832 }
Adam Langley95c29f32014-06-20 12:00:00 -07001833
David Benjamin1269ddd2015-10-18 15:18:55 -04001834 if (use_internal_cache &&
1835 !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR)) {
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001836 /* Automatically flush the internal session cache every 255 connections. */
1837 int flush_cache = 0;
Adam Langley4bdb6e42015-05-15 15:29:21 -07001838 CRYPTO_MUTEX_lock_write(&ctx->lock);
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001839 ctx->handshakes_since_cache_flush++;
1840 if (ctx->handshakes_since_cache_flush >= 255) {
1841 flush_cache = 1;
1842 ctx->handshakes_since_cache_flush = 0;
1843 }
Adam Langley4bdb6e42015-05-15 15:29:21 -07001844 CRYPTO_MUTEX_unlock(&ctx->lock);
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001845
1846 if (flush_cache) {
1847 SSL_CTX_flush_sessions(ctx, (unsigned long)time(NULL));
Adam Langleyfcf25832014-12-18 17:42:32 -08001848 }
1849 }
1850}
Adam Langley95c29f32014-06-20 12:00:00 -07001851
Adam Langleyfcf25832014-12-18 17:42:32 -08001852static const char *ssl_get_version(int version) {
1853 switch (version) {
1854 case TLS1_2_VERSION:
1855 return "TLSv1.2";
Adam Langley95c29f32014-06-20 12:00:00 -07001856
Adam Langleyfcf25832014-12-18 17:42:32 -08001857 case TLS1_1_VERSION:
1858 return "TLSv1.1";
Adam Langley95c29f32014-06-20 12:00:00 -07001859
Adam Langleyfcf25832014-12-18 17:42:32 -08001860 case TLS1_VERSION:
1861 return "TLSv1";
Adam Langley95c29f32014-06-20 12:00:00 -07001862
Adam Langleyfcf25832014-12-18 17:42:32 -08001863 case SSL3_VERSION:
1864 return "SSLv3";
Adam Langley95c29f32014-06-20 12:00:00 -07001865
David Benjamin1c722b72015-04-20 13:53:10 -04001866 case DTLS1_VERSION:
1867 return "DTLSv1";
1868
1869 case DTLS1_2_VERSION:
1870 return "DTLSv1.2";
1871
Adam Langleyfcf25832014-12-18 17:42:32 -08001872 default:
1873 return "unknown";
1874 }
1875}
Adam Langley95c29f32014-06-20 12:00:00 -07001876
David Benjamin42fea372015-09-19 01:22:44 -04001877const char *SSL_get_version(const SSL *ssl) {
1878 return ssl_get_version(ssl->version);
Adam Langleyfcf25832014-12-18 17:42:32 -08001879}
Adam Langley95c29f32014-06-20 12:00:00 -07001880
David Benjamina6b8cdf2015-09-13 14:07:33 -04001881const char *SSL_SESSION_get_version(const SSL_SESSION *session) {
1882 return ssl_get_version(session->ssl_version);
Adam Langleyfcf25832014-12-18 17:42:32 -08001883}
Adam Langley95c29f32014-06-20 12:00:00 -07001884
Sigbjorn Vik2b23d242015-06-29 15:07:26 +02001885const char* SSL_get_curve_name(uint16_t curve_id) {
1886 return tls1_ec_curve_id2name(curve_id);
1887}
1888
Adam Langleyfcf25832014-12-18 17:42:32 -08001889void ssl_clear_cipher_ctx(SSL *s) {
David Benjamin31a07792015-03-03 14:20:26 -05001890 SSL_AEAD_CTX_free(s->aead_read_ctx);
1891 s->aead_read_ctx = NULL;
1892 SSL_AEAD_CTX_free(s->aead_write_ctx);
1893 s->aead_write_ctx = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08001894}
Adam Langley95c29f32014-06-20 12:00:00 -07001895
Adam Langleyfcf25832014-12-18 17:42:32 -08001896X509 *SSL_get_certificate(const SSL *s) {
1897 if (s->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04001898 return s->cert->x509;
Adam Langleyfcf25832014-12-18 17:42:32 -08001899 }
1900
1901 return NULL;
1902}
1903
1904EVP_PKEY *SSL_get_privatekey(const SSL *s) {
1905 if (s->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04001906 return s->cert->privatekey;
Adam Langleyfcf25832014-12-18 17:42:32 -08001907 }
1908
1909 return NULL;
1910}
1911
1912X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
1913 if (ctx->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04001914 return ctx->cert->x509;
Adam Langleyfcf25832014-12-18 17:42:32 -08001915 }
1916
1917 return NULL;
1918}
1919
1920EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
1921 if (ctx->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04001922 return ctx->cert->privatekey;
Adam Langleyfcf25832014-12-18 17:42:32 -08001923 }
1924
1925 return NULL;
1926}
1927
David Benjamin42fea372015-09-19 01:22:44 -04001928const SSL_CIPHER *SSL_get_current_cipher(const SSL *ssl) {
1929 if (ssl->aead_write_ctx == NULL) {
David Benjamina07c0fc2015-05-13 13:19:42 -04001930 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08001931 }
David Benjamin42fea372015-09-19 01:22:44 -04001932 return ssl->aead_write_ctx->cipher;
Adam Langleyfcf25832014-12-18 17:42:32 -08001933}
1934
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07001935const COMP_METHOD *SSL_get_current_compression(SSL *s) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08001936
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07001937const COMP_METHOD *SSL_get_current_expansion(SSL *s) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08001938
1939int ssl_init_wbio_buffer(SSL *s, int push) {
1940 BIO *bbio;
1941
1942 if (s->bbio == NULL) {
1943 bbio = BIO_new(BIO_f_buffer());
1944 if (bbio == NULL) {
1945 return 0;
1946 }
1947 s->bbio = bbio;
1948 } else {
1949 bbio = s->bbio;
1950 if (s->bbio == s->wbio) {
1951 s->wbio = BIO_pop(s->wbio);
1952 }
1953 }
1954
1955 BIO_reset(bbio);
1956 if (!BIO_set_read_buffer_size(bbio, 1)) {
David Benjamin3570d732015-06-29 00:28:17 -04001957 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -08001958 return 0;
1959 }
1960
1961 if (push) {
1962 if (s->wbio != bbio) {
1963 s->wbio = BIO_push(bbio, s->wbio);
1964 }
1965 } else {
1966 if (s->wbio == bbio) {
1967 s->wbio = BIO_pop(bbio);
1968 }
1969 }
1970
1971 return 1;
1972}
1973
1974void ssl_free_wbio_buffer(SSL *s) {
1975 if (s->bbio == NULL) {
1976 return;
1977 }
1978
1979 if (s->bbio == s->wbio) {
1980 /* remove buffering */
1981 s->wbio = BIO_pop(s->wbio);
1982 }
1983
1984 BIO_free(s->bbio);
1985 s->bbio = NULL;
1986}
1987
1988void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
Adam Langleybb85f3d2015-10-28 18:44:11 -07001989 ctx->quiet_shutdown = (mode != 0);
Adam Langleyfcf25832014-12-18 17:42:32 -08001990}
1991
1992int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
1993 return ctx->quiet_shutdown;
1994}
1995
Adam Langleybb85f3d2015-10-28 18:44:11 -07001996void SSL_set_quiet_shutdown(SSL *ssl, int mode) {
1997 ssl->quiet_shutdown = (mode != 0);
1998}
Adam Langleyfcf25832014-12-18 17:42:32 -08001999
David Benjamin9f859492015-10-03 10:44:30 -04002000int SSL_get_quiet_shutdown(const SSL *ssl) { return ssl->quiet_shutdown; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002001
David Benjamin63006a92015-10-18 00:00:06 -04002002void SSL_set_shutdown(SSL *ssl, int mode) {
2003 /* It is an error to clear any bits that have already been set. (We can't try
2004 * to get a second close_notify or send two.) */
Piotr Sikora7063b6d2015-10-29 02:47:40 -07002005 assert((ssl->shutdown & mode) == ssl->shutdown);
David Benjamin63006a92015-10-18 00:00:06 -04002006
2007 ssl->shutdown |= mode;
2008}
Adam Langleyfcf25832014-12-18 17:42:32 -08002009
David Benjamin9f859492015-10-03 10:44:30 -04002010int SSL_get_shutdown(const SSL *ssl) { return ssl->shutdown; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002011
David Benjamin9f859492015-10-03 10:44:30 -04002012int SSL_version(const SSL *ssl) { return ssl->version; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002013
2014SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
2015
2016SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
2017 if (ssl->ctx == ctx) {
2018 return ssl->ctx;
2019 }
2020
2021 if (ctx == NULL) {
2022 ctx = ssl->initial_ctx;
2023 }
2024
David Benjamin2755a3e2015-04-22 16:17:58 -04002025 ssl_cert_free(ssl->cert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002026 ssl->cert = ssl_cert_dup(ctx->cert);
David Benjamin2755a3e2015-04-22 16:17:58 -04002027
Adam Langley0b5e3902015-05-15 13:08:38 -07002028 CRYPTO_refcount_inc(&ctx->references);
David Benjamin2755a3e2015-04-22 16:17:58 -04002029 SSL_CTX_free(ssl->ctx); /* decrement reference count */
Adam Langleyfcf25832014-12-18 17:42:32 -08002030 ssl->ctx = ctx;
2031
2032 ssl->sid_ctx_length = ctx->sid_ctx_length;
2033 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2034 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2035
2036 return ssl->ctx;
2037}
2038
2039int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
2040 return X509_STORE_set_default_paths(ctx->cert_store);
2041}
Adam Langley95c29f32014-06-20 12:00:00 -07002042
David Benjamin59937042015-09-19 13:04:22 -04002043int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *ca_file,
2044 const char *ca_dir) {
2045 return X509_STORE_load_locations(ctx->cert_store, ca_file, ca_dir);
Adam Langleyfcf25832014-12-18 17:42:32 -08002046}
Adam Langley95c29f32014-06-20 12:00:00 -07002047
2048void SSL_set_info_callback(SSL *ssl,
David Benjamin82170242015-10-17 22:51:17 -04002049 void (*cb)(const SSL *ssl, int type, int value)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002050 ssl->info_callback = cb;
2051}
Adam Langley95c29f32014-06-20 12:00:00 -07002052
David Benjamin82170242015-10-17 22:51:17 -04002053void (*SSL_get_info_callback(const SSL *ssl))(const SSL *ssl, int type,
2054 int value) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002055 return ssl->info_callback;
2056}
Adam Langley95c29f32014-06-20 12:00:00 -07002057
Adam Langleyfcf25832014-12-18 17:42:32 -08002058int SSL_state(const SSL *ssl) { return ssl->state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002059
David Benjaminece089c2015-05-15 23:52:42 -04002060void SSL_set_state(SSL *ssl, int state) { }
Adam Langley95c29f32014-06-20 12:00:00 -07002061
David Benjamin59937042015-09-19 13:04:22 -04002062void SSL_set_verify_result(SSL *ssl, long result) {
2063 ssl->verify_result = result;
2064}
Adam Langley95c29f32014-06-20 12:00:00 -07002065
Adam Langleyfcf25832014-12-18 17:42:32 -08002066long SSL_get_verify_result(const SSL *ssl) { return ssl->verify_result; }
Adam Langley95c29f32014-06-20 12:00:00 -07002067
Adam Langleyfcf25832014-12-18 17:42:32 -08002068int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2069 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002070 int index;
2071 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp,
2072 new_func, dup_func, free_func)) {
2073 return -1;
2074 }
2075 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002076}
Adam Langley95c29f32014-06-20 12:00:00 -07002077
David Benjamin1ac08fe2015-09-15 01:36:22 -04002078int SSL_set_ex_data(SSL *ssl, int idx, void *arg) {
2079 return CRYPTO_set_ex_data(&ssl->ex_data, idx, arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002080}
Adam Langley95c29f32014-06-20 12:00:00 -07002081
David Benjamin1ac08fe2015-09-15 01:36:22 -04002082void *SSL_get_ex_data(const SSL *ssl, int idx) {
2083 return CRYPTO_get_ex_data(&ssl->ex_data, idx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002084}
Adam Langley95c29f32014-06-20 12:00:00 -07002085
Adam Langleyfcf25832014-12-18 17:42:32 -08002086int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2087 CRYPTO_EX_dup *dup_func,
2088 CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002089 int index;
2090 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp,
2091 new_func, dup_func, free_func)) {
2092 return -1;
2093 }
2094 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002095}
Adam Langley95c29f32014-06-20 12:00:00 -07002096
David Benjamin1ac08fe2015-09-15 01:36:22 -04002097int SSL_CTX_set_ex_data(SSL_CTX *ctx, int idx, void *arg) {
2098 return CRYPTO_set_ex_data(&ctx->ex_data, idx, arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002099}
Adam Langley95c29f32014-06-20 12:00:00 -07002100
David Benjamin1ac08fe2015-09-15 01:36:22 -04002101void *SSL_CTX_get_ex_data(const SSL_CTX *ctx, int idx) {
2102 return CRYPTO_get_ex_data(&ctx->ex_data, idx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002103}
Adam Langley95c29f32014-06-20 12:00:00 -07002104
Adam Langleyfcf25832014-12-18 17:42:32 -08002105X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
2106 return ctx->cert_store;
2107}
Adam Langley95c29f32014-06-20 12:00:00 -07002108
Adam Langleyfcf25832014-12-18 17:42:32 -08002109void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
David Benjamin2755a3e2015-04-22 16:17:58 -04002110 X509_STORE_free(ctx->cert_store);
Adam Langleyfcf25832014-12-18 17:42:32 -08002111 ctx->cert_store = store;
2112}
Adam Langley95c29f32014-06-20 12:00:00 -07002113
David Benjamin93d17492015-10-17 12:43:36 -04002114int SSL_want(const SSL *ssl) { return ssl->rwstate; }
Adam Langley95c29f32014-06-20 12:00:00 -07002115
Adam Langleyfcf25832014-12-18 17:42:32 -08002116void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
2117 RSA *(*cb)(SSL *ssl, int is_export,
2118 int keylength)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002119}
Adam Langley95c29f32014-06-20 12:00:00 -07002120
Adam Langleyfcf25832014-12-18 17:42:32 -08002121void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
2122 int keylength)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002123}
2124
2125void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
David Benjamin59015c32015-04-26 13:13:55 -04002126 DH *(*callback)(SSL *ssl, int is_export,
2127 int keylength)) {
2128 ctx->cert->dh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002129}
2130
David Benjamin59015c32015-04-26 13:13:55 -04002131void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*callback)(SSL *ssl, int is_export,
2132 int keylength)) {
2133 ssl->cert->dh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002134}
2135
2136void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,
David Benjamindd978782015-04-24 15:20:13 -04002137 EC_KEY *(*callback)(SSL *ssl, int is_export,
2138 int keylength)) {
David Benjamin59015c32015-04-26 13:13:55 -04002139 ctx->cert->ecdh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002140}
2141
2142void SSL_set_tmp_ecdh_callback(SSL *ssl,
David Benjamindd978782015-04-24 15:20:13 -04002143 EC_KEY *(*callback)(SSL *ssl, int is_export,
2144 int keylength)) {
David Benjamin59015c32015-04-26 13:13:55 -04002145 ssl->cert->ecdh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002146}
2147
2148int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
2149 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
David Benjamin3570d732015-06-29 00:28:17 -04002150 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -08002151 return 0;
2152 }
2153
David Benjamin2755a3e2015-04-22 16:17:58 -04002154 OPENSSL_free(ctx->psk_identity_hint);
Adam Langleyfcf25832014-12-18 17:42:32 -08002155
2156 if (identity_hint != NULL) {
2157 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2158 if (ctx->psk_identity_hint == NULL) {
2159 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002160 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002161 } else {
2162 ctx->psk_identity_hint = NULL;
2163 }
Adam Langley95c29f32014-06-20 12:00:00 -07002164
Adam Langleyfcf25832014-12-18 17:42:32 -08002165 return 1;
2166}
2167
David Benjamine8814df2015-09-15 08:05:54 -04002168int SSL_use_psk_identity_hint(SSL *ssl, const char *identity_hint) {
2169 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002170 return 0;
2171 }
2172
2173 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
David Benjamin3570d732015-06-29 00:28:17 -04002174 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -08002175 return 0;
2176 }
2177
2178 /* Clear currently configured hint, if any. */
David Benjamine8814df2015-09-15 08:05:54 -04002179 OPENSSL_free(ssl->psk_identity_hint);
2180 ssl->psk_identity_hint = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002181
2182 if (identity_hint != NULL) {
David Benjamine8814df2015-09-15 08:05:54 -04002183 ssl->psk_identity_hint = BUF_strdup(identity_hint);
2184 if (ssl->psk_identity_hint == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002185 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002186 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002187 }
Adam Langley95c29f32014-06-20 12:00:00 -07002188
Adam Langleyfcf25832014-12-18 17:42:32 -08002189 return 1;
2190}
Adam Langley95c29f32014-06-20 12:00:00 -07002191
David Benjamine8814df2015-09-15 08:05:54 -04002192const char *SSL_get_psk_identity_hint(const SSL *ssl) {
2193 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002194 return NULL;
2195 }
David Benjamine8814df2015-09-15 08:05:54 -04002196 return ssl->psk_identity_hint;
Adam Langleyfcf25832014-12-18 17:42:32 -08002197}
Adam Langley95c29f32014-06-20 12:00:00 -07002198
David Benjamine8814df2015-09-15 08:05:54 -04002199const char *SSL_get_psk_identity(const SSL *ssl) {
2200 if (ssl == NULL || ssl->session == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002201 return NULL;
2202 }
Adam Langley95c29f32014-06-20 12:00:00 -07002203
David Benjamine8814df2015-09-15 08:05:54 -04002204 return ssl->session->psk_identity;
Adam Langleyfcf25832014-12-18 17:42:32 -08002205}
Adam Langley95c29f32014-06-20 12:00:00 -07002206
Adam Langleyfcf25832014-12-18 17:42:32 -08002207void SSL_set_psk_client_callback(
David Benjamine8814df2015-09-15 08:05:54 -04002208 SSL *ssl, unsigned (*cb)(SSL *ssl, const char *hint, char *identity,
2209 unsigned max_identity_len, uint8_t *psk,
2210 unsigned max_psk_len)) {
2211 ssl->psk_client_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002212}
Adam Langley95c29f32014-06-20 12:00:00 -07002213
Adam Langleyfcf25832014-12-18 17:42:32 -08002214void SSL_CTX_set_psk_client_callback(
David Benjamine8814df2015-09-15 08:05:54 -04002215 SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *hint, char *identity,
2216 unsigned max_identity_len, uint8_t *psk,
2217 unsigned max_psk_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002218 ctx->psk_client_callback = cb;
2219}
Adam Langley95c29f32014-06-20 12:00:00 -07002220
Adam Langleyfcf25832014-12-18 17:42:32 -08002221void SSL_set_psk_server_callback(
David Benjamine8814df2015-09-15 08:05:54 -04002222 SSL *ssl, unsigned (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
2223 unsigned max_psk_len)) {
2224 ssl->psk_server_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002225}
Adam Langley95c29f32014-06-20 12:00:00 -07002226
Adam Langleyfcf25832014-12-18 17:42:32 -08002227void SSL_CTX_set_psk_server_callback(
David Benjamine8814df2015-09-15 08:05:54 -04002228 SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *identity,
2229 uint8_t *psk, unsigned max_psk_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002230 ctx->psk_server_callback = cb;
2231}
Adam Langley95c29f32014-06-20 12:00:00 -07002232
Adam Langleyfcf25832014-12-18 17:42:32 -08002233void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
2234 void (*cb)(int write_p, int version,
2235 int content_type, const void *buf,
2236 size_t len, SSL *ssl, void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002237 ctx->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002238}
David Benjamin61ecccf2015-05-05 09:44:51 -04002239
2240void SSL_CTX_set_msg_callback_arg(SSL_CTX *ctx, void *arg) {
2241 ctx->msg_callback_arg = arg;
2242}
2243
Adam Langleyfcf25832014-12-18 17:42:32 -08002244void SSL_set_msg_callback(SSL *ssl,
2245 void (*cb)(int write_p, int version, int content_type,
2246 const void *buf, size_t len, SSL *ssl,
2247 void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002248 ssl->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002249}
Adam Langley95c29f32014-06-20 12:00:00 -07002250
David Benjamin61ecccf2015-05-05 09:44:51 -04002251void SSL_set_msg_callback_arg(SSL *ssl, void *arg) {
2252 ssl->msg_callback_arg = arg;
2253}
2254
Adam Langleyfcf25832014-12-18 17:42:32 -08002255void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio) {
David Benjamin2755a3e2015-04-22 16:17:58 -04002256 BIO_free(ctx->keylog_bio);
Adam Langleyfcf25832014-12-18 17:42:32 -08002257 ctx->keylog_bio = keylog_bio;
2258}
Adam Langley95c29f32014-06-20 12:00:00 -07002259
Adam Langleyfcf25832014-12-18 17:42:32 -08002260static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
2261 static const char hextable[] = "0123456789abcdef";
2262 uint8_t *out;
2263 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002264
Adam Langleyfcf25832014-12-18 17:42:32 -08002265 if (!CBB_add_space(cbb, &out, in_len * 2)) {
2266 return 0;
2267 }
Adam Langley95c29f32014-06-20 12:00:00 -07002268
Adam Langleyfcf25832014-12-18 17:42:32 -08002269 for (i = 0; i < in_len; i++) {
2270 *(out++) = (uint8_t)hextable[in[i] >> 4];
2271 *(out++) = (uint8_t)hextable[in[i] & 0xf];
2272 }
Adam Langley95c29f32014-06-20 12:00:00 -07002273
Adam Langleyfcf25832014-12-18 17:42:32 -08002274 return 1;
2275}
David Benjamin859ec3c2014-09-02 16:29:36 -04002276
2277int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -08002278 const uint8_t *encrypted_premaster,
2279 size_t encrypted_premaster_len,
2280 const uint8_t *premaster,
2281 size_t premaster_len) {
2282 BIO *bio = ctx->keylog_bio;
2283 CBB cbb;
2284 uint8_t *out;
2285 size_t out_len;
2286 int ret;
David Benjamin859ec3c2014-09-02 16:29:36 -04002287
Adam Langleyfcf25832014-12-18 17:42:32 -08002288 if (bio == NULL) {
2289 return 1;
2290 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002291
Adam Langleyfcf25832014-12-18 17:42:32 -08002292 if (encrypted_premaster_len < 8) {
David Benjamin3570d732015-06-29 00:28:17 -04002293 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08002294 return 0;
2295 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002296
David Benjamina8653202015-06-28 01:26:10 -04002297 CBB_zero(&cbb);
2298 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len * 2 + 1) ||
2299 !CBB_add_bytes(&cbb, (const uint8_t *)"RSA ", 4) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08002300 /* Only the first 8 bytes of the encrypted premaster secret are
2301 * logged. */
2302 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
2303 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2304 !cbb_add_hex(&cbb, premaster, premaster_len) ||
2305 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2306 !CBB_finish(&cbb, &out, &out_len)) {
2307 CBB_cleanup(&cbb);
2308 return 0;
2309 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002310
Adam Langley4bdb6e42015-05-15 15:29:21 -07002311 CRYPTO_MUTEX_lock_write(&ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -08002312 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
Adam Langley4bdb6e42015-05-15 15:29:21 -07002313 CRYPTO_MUTEX_unlock(&ctx->lock);
David Benjamin859ec3c2014-09-02 16:29:36 -04002314
Adam Langleyfcf25832014-12-18 17:42:32 -08002315 OPENSSL_free(out);
2316 return ret;
Adam Langley95f22882014-06-20 12:00:00 -07002317}
2318
Adam Langleyfcf25832014-12-18 17:42:32 -08002319int ssl_ctx_log_master_secret(SSL_CTX *ctx, const uint8_t *client_random,
2320 size_t client_random_len, const uint8_t *master,
2321 size_t master_len) {
2322 BIO *bio = ctx->keylog_bio;
2323 CBB cbb;
2324 uint8_t *out;
2325 size_t out_len;
2326 int ret;
Adam Langleyadb739e2014-06-20 12:00:00 -07002327
Adam Langleyfcf25832014-12-18 17:42:32 -08002328 if (bio == NULL) {
2329 return 1;
2330 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002331
Adam Langleyfcf25832014-12-18 17:42:32 -08002332 if (client_random_len != 32) {
David Benjamin3570d732015-06-29 00:28:17 -04002333 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08002334 return 0;
2335 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002336
David Benjamina8653202015-06-28 01:26:10 -04002337 CBB_zero(&cbb);
2338 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len * 2 + 1) ||
2339 !CBB_add_bytes(&cbb, (const uint8_t *)"CLIENT_RANDOM ", 14) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08002340 !cbb_add_hex(&cbb, client_random, 32) ||
2341 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2342 !cbb_add_hex(&cbb, master, master_len) ||
2343 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2344 !CBB_finish(&cbb, &out, &out_len)) {
2345 CBB_cleanup(&cbb);
2346 return 0;
2347 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002348
Adam Langley4bdb6e42015-05-15 15:29:21 -07002349 CRYPTO_MUTEX_lock_write(&ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -08002350 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
Adam Langley4bdb6e42015-05-15 15:29:21 -07002351 CRYPTO_MUTEX_unlock(&ctx->lock);
David Benjamine99e9122014-12-11 01:46:01 -05002352
Adam Langleyfcf25832014-12-18 17:42:32 -08002353 OPENSSL_free(out);
2354 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -07002355}
2356
David Benjamin5d8b1282015-10-17 23:26:35 -04002357int SSL_is_init_finished(const SSL *ssl) {
2358 return ssl->state == SSL_ST_OK;
2359}
2360
2361int SSL_in_init(const SSL *ssl) {
2362 return (ssl->state & SSL_ST_INIT) != 0;
2363}
2364
2365int SSL_in_false_start(const SSL *ssl) {
2366 return ssl->s3->tmp.in_false_start;
David Benjamined7c4752015-02-16 19:16:46 -05002367}
2368
Adam Langleyfcf25832014-12-18 17:42:32 -08002369int SSL_cutthrough_complete(const SSL *s) {
David Benjamined7c4752015-02-16 19:16:46 -05002370 return SSL_in_false_start(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002371}
Adam Langley95c29f32014-06-20 12:00:00 -07002372
Adam Langleyfcf25832014-12-18 17:42:32 -08002373void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
2374 size_t *ssl_session_size) {
2375 *ssl_size = sizeof(SSL);
2376 *ssl_ctx_size = sizeof(SSL_CTX);
2377 *ssl_session_size = sizeof(SSL_SESSION);
2378}
Feng Lu41aa3252014-11-21 22:47:56 -08002379
David Benjamined7c4752015-02-16 19:16:46 -05002380int ssl3_can_false_start(const SSL *s) {
David Benjamin195dc782015-02-19 13:27:05 -05002381 const SSL_CIPHER *const cipher = SSL_get_current_cipher(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002382
Adam Langleye631d962015-04-01 13:11:01 -07002383 /* False Start only for TLS 1.2 with an ECDHE+AEAD cipher and ALPN or NPN. */
David Benjamin195dc782015-02-19 13:27:05 -05002384 return !SSL_IS_DTLS(s) &&
2385 SSL_version(s) >= TLS1_2_VERSION &&
2386 (s->s3->alpn_selected || s->s3->next_proto_neg_seen) &&
2387 cipher != NULL &&
Adam Langleye631d962015-04-01 13:11:01 -07002388 cipher->algorithm_mkey == SSL_kECDHE &&
David Benjamin195dc782015-02-19 13:27:05 -05002389 (cipher->algorithm_enc == SSL_AES128GCM ||
2390 cipher->algorithm_enc == SSL_AES256GCM ||
Brian Smith271777f2015-10-03 13:53:33 -10002391 cipher->algorithm_enc == SSL_CHACHA20POLY1305_OLD);
Adam Langleyfcf25832014-12-18 17:42:32 -08002392}
2393
2394const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version) {
2395 switch (version) {
2396 case SSL3_VERSION:
2397 return &SSLv3_enc_data;
2398
2399 case TLS1_VERSION:
2400 return &TLSv1_enc_data;
2401
David Benjamin9e13e1a2015-03-05 01:56:32 -05002402 case DTLS1_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002403 case TLS1_1_VERSION:
2404 return &TLSv1_1_enc_data;
2405
David Benjamin9e13e1a2015-03-05 01:56:32 -05002406 case DTLS1_2_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002407 case TLS1_2_VERSION:
2408 return &TLSv1_2_enc_data;
2409
Adam Langleyfcf25832014-12-18 17:42:32 -08002410 default:
2411 return NULL;
2412 }
2413}
2414
2415uint16_t ssl3_get_max_server_version(const SSL *s) {
2416 uint16_t max_version;
2417
2418 if (SSL_IS_DTLS(s)) {
2419 max_version = (s->max_version != 0) ? s->max_version : DTLS1_2_VERSION;
2420 if (!(s->options & SSL_OP_NO_DTLSv1_2) && DTLS1_2_VERSION >= max_version) {
2421 return DTLS1_2_VERSION;
2422 }
2423 if (!(s->options & SSL_OP_NO_DTLSv1) && DTLS1_VERSION >= max_version) {
2424 return DTLS1_VERSION;
2425 }
2426 return 0;
2427 }
2428
2429 max_version = (s->max_version != 0) ? s->max_version : TLS1_2_VERSION;
2430 if (!(s->options & SSL_OP_NO_TLSv1_2) && TLS1_2_VERSION <= max_version) {
2431 return TLS1_2_VERSION;
2432 }
2433 if (!(s->options & SSL_OP_NO_TLSv1_1) && TLS1_1_VERSION <= max_version) {
2434 return TLS1_1_VERSION;
2435 }
2436 if (!(s->options & SSL_OP_NO_TLSv1) && TLS1_VERSION <= max_version) {
2437 return TLS1_VERSION;
2438 }
2439 if (!(s->options & SSL_OP_NO_SSLv3) && SSL3_VERSION <= max_version) {
2440 return SSL3_VERSION;
2441 }
2442 return 0;
2443}
2444
2445uint16_t ssl3_get_mutual_version(SSL *s, uint16_t client_version) {
2446 uint16_t version = 0;
2447
2448 if (SSL_IS_DTLS(s)) {
2449 /* Clamp client_version to max_version. */
2450 if (s->max_version != 0 && client_version < s->max_version) {
2451 client_version = s->max_version;
2452 }
2453
2454 if (client_version <= DTLS1_2_VERSION && !(s->options & SSL_OP_NO_DTLSv1_2)) {
2455 version = DTLS1_2_VERSION;
2456 } else if (client_version <= DTLS1_VERSION &&
David Benjaminb18f0242015-03-10 18:30:08 -04002457 !(s->options & SSL_OP_NO_DTLSv1)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002458 version = DTLS1_VERSION;
2459 }
2460
2461 /* Check against min_version. */
2462 if (version != 0 && s->min_version != 0 && version > s->min_version) {
2463 return 0;
2464 }
2465 return version;
2466 } else {
2467 /* Clamp client_version to max_version. */
2468 if (s->max_version != 0 && client_version > s->max_version) {
2469 client_version = s->max_version;
2470 }
2471
2472 if (client_version >= TLS1_2_VERSION && !(s->options & SSL_OP_NO_TLSv1_2)) {
2473 version = TLS1_2_VERSION;
2474 } else if (client_version >= TLS1_1_VERSION &&
2475 !(s->options & SSL_OP_NO_TLSv1_1)) {
2476 version = TLS1_1_VERSION;
2477 } else if (client_version >= TLS1_VERSION && !(s->options & SSL_OP_NO_TLSv1)) {
2478 version = TLS1_VERSION;
2479 } else if (client_version >= SSL3_VERSION && !(s->options & SSL_OP_NO_SSLv3)) {
2480 version = SSL3_VERSION;
2481 }
2482
2483 /* Check against min_version. */
2484 if (version != 0 && s->min_version != 0 && version < s->min_version) {
2485 return 0;
2486 }
2487 return version;
2488 }
2489}
2490
2491uint16_t ssl3_get_max_client_version(SSL *s) {
David Benjamin123a8fd2015-04-26 14:16:41 -04002492 uint32_t options = s->options;
Adam Langleyfcf25832014-12-18 17:42:32 -08002493 uint16_t version = 0;
2494
2495 /* OpenSSL's API for controlling versions entails blacklisting individual
2496 * protocols. This has two problems. First, on the client, the protocol can
2497 * only express a contiguous range of versions. Second, a library consumer
2498 * trying to set a maximum version cannot disable protocol versions that get
2499 * added in a future version of the library.
2500 *
2501 * To account for both of these, OpenSSL interprets the client-side bitmask
2502 * as a min/max range by picking the lowest contiguous non-empty range of
2503 * enabled protocols. Note that this means it is impossible to set a maximum
2504 * version of TLS 1.2 in a future-proof way.
2505 *
2506 * By this scheme, the maximum version is the lowest version V such that V is
2507 * enabled and V+1 is disabled or unimplemented. */
2508 if (SSL_IS_DTLS(s)) {
2509 if (!(options & SSL_OP_NO_DTLSv1_2)) {
2510 version = DTLS1_2_VERSION;
2511 }
2512 if (!(options & SSL_OP_NO_DTLSv1) && (options & SSL_OP_NO_DTLSv1_2)) {
2513 version = DTLS1_VERSION;
2514 }
2515 if (s->max_version != 0 && version < s->max_version) {
2516 version = s->max_version;
2517 }
2518 } else {
2519 if (!(options & SSL_OP_NO_TLSv1_2)) {
2520 version = TLS1_2_VERSION;
2521 }
2522 if (!(options & SSL_OP_NO_TLSv1_1) && (options & SSL_OP_NO_TLSv1_2)) {
2523 version = TLS1_1_VERSION;
2524 }
2525 if (!(options & SSL_OP_NO_TLSv1) && (options & SSL_OP_NO_TLSv1_1)) {
2526 version = TLS1_VERSION;
2527 }
2528 if (!(options & SSL_OP_NO_SSLv3) && (options & SSL_OP_NO_TLSv1)) {
2529 version = SSL3_VERSION;
2530 }
2531 if (s->max_version != 0 && version > s->max_version) {
2532 version = s->max_version;
2533 }
2534 }
2535
2536 return version;
2537}
2538
2539int ssl3_is_version_enabled(SSL *s, uint16_t version) {
2540 if (SSL_IS_DTLS(s)) {
2541 if (s->max_version != 0 && version < s->max_version) {
2542 return 0;
2543 }
2544 if (s->min_version != 0 && version > s->min_version) {
2545 return 0;
2546 }
2547
2548 switch (version) {
2549 case DTLS1_VERSION:
2550 return !(s->options & SSL_OP_NO_DTLSv1);
2551
2552 case DTLS1_2_VERSION:
2553 return !(s->options & SSL_OP_NO_DTLSv1_2);
2554
2555 default:
2556 return 0;
2557 }
2558 } else {
2559 if (s->max_version != 0 && version > s->max_version) {
2560 return 0;
2561 }
2562 if (s->min_version != 0 && version < s->min_version) {
2563 return 0;
2564 }
2565
2566 switch (version) {
2567 case SSL3_VERSION:
2568 return !(s->options & SSL_OP_NO_SSLv3);
2569
2570 case TLS1_VERSION:
2571 return !(s->options & SSL_OP_NO_TLSv1);
2572
2573 case TLS1_1_VERSION:
2574 return !(s->options & SSL_OP_NO_TLSv1_1);
2575
2576 case TLS1_2_VERSION:
2577 return !(s->options & SSL_OP_NO_TLSv1_2);
2578
2579 default:
2580 return 0;
2581 }
2582 }
2583}
2584
David Benjaminea72bd02014-12-21 21:27:41 -05002585uint16_t ssl3_version_from_wire(SSL *s, uint16_t wire_version) {
2586 if (!SSL_IS_DTLS(s)) {
2587 return wire_version;
2588 }
2589
2590 uint16_t tls_version = ~wire_version;
2591 uint16_t version = tls_version + 0x0201;
2592 /* If either component overflowed, clamp it so comparisons still work. */
2593 if ((version >> 8) < (tls_version >> 8)) {
2594 version = 0xff00 | (version & 0xff);
2595 }
2596 if ((version & 0xff) < (tls_version & 0xff)) {
2597 version = (version & 0xff00) | 0xff;
2598 }
2599 /* DTLS 1.0 maps to TLS 1.1, not TLS 1.0. */
2600 if (version == TLS1_VERSION) {
2601 version = TLS1_1_VERSION;
2602 }
2603 return version;
2604}
2605
David Benjamin42fea372015-09-19 01:22:44 -04002606int SSL_cache_hit(SSL *ssl) { return SSL_session_reused(ssl); }
Adam Langleyfcf25832014-12-18 17:42:32 -08002607
David Benjamin42fea372015-09-19 01:22:44 -04002608int SSL_is_server(SSL *ssl) { return ssl->server; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002609
David Benjamind4c2bce2015-10-17 12:28:18 -04002610void SSL_CTX_set_select_certificate_cb(
2611 SSL_CTX *ctx, int (*cb)(const struct ssl_early_callback_ctx *)) {
2612 ctx->select_certificate_cb = cb;
2613}
2614
Adam Langley524e7172015-02-20 16:04:00 -08002615void SSL_CTX_set_dos_protection_cb(
2616 SSL_CTX *ctx, int (*cb)(const struct ssl_early_callback_ctx *)) {
2617 ctx->dos_protection_cb = cb;
2618}
2619
David Benjamin1d5ef3b2015-10-12 19:54:18 -04002620void SSL_set_renegotiate_mode(SSL *ssl, enum ssl_renegotiate_mode_t mode) {
2621 ssl->renegotiate_mode = mode;
2622}
2623
2624void SSL_set_reject_peer_renegotiations(SSL *ssl, int reject) {
2625 SSL_set_renegotiate_mode(
2626 ssl, reject ? ssl_renegotiate_never : ssl_renegotiate_freely);
David Benjaminb16346b2015-04-08 19:16:58 -04002627}
2628
Adam Langley3f92d212015-02-20 15:32:52 -08002629int SSL_get_rc4_state(const SSL *ssl, const RC4_KEY **read_key,
2630 const RC4_KEY **write_key) {
2631 if (ssl->aead_read_ctx == NULL || ssl->aead_write_ctx == NULL) {
2632 return 0;
2633 }
2634
2635 return EVP_AEAD_CTX_get_rc4_state(&ssl->aead_read_ctx->ctx, read_key) &&
2636 EVP_AEAD_CTX_get_rc4_state(&ssl->aead_write_ctx->ctx, write_key);
2637}
David Benjaminda881e92015-04-26 14:45:04 -04002638
David Benjamin27bbae42015-09-13 00:54:37 -04002639int SSL_clear(SSL *ssl) {
2640 if (ssl->method == NULL) {
2641 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_METHOD_SPECIFIED);
2642 return 0;
2643 }
2644
2645 if (ssl_clear_bad_session(ssl)) {
2646 SSL_SESSION_free(ssl->session);
2647 ssl->session = NULL;
2648 }
2649
2650 ssl->hit = 0;
2651 ssl->shutdown = 0;
2652
2653 /* SSL_clear may be called before or after the |ssl| is initialized in either
2654 * accept or connect state. In the latter case, SSL_clear should preserve the
2655 * half and reset |ssl->state| accordingly. */
2656 if (ssl->handshake_func != NULL) {
2657 if (ssl->server) {
2658 SSL_set_accept_state(ssl);
2659 } else {
2660 SSL_set_connect_state(ssl);
2661 }
2662 } else {
2663 assert(ssl->state == 0);
2664 }
2665
2666 /* TODO(davidben): Some state on |ssl| is reset both in |SSL_new| and
2667 * |SSL_clear| because it is per-connection state rather than configuration
2668 * state. Per-connection state should be on |ssl->s3| and |ssl->d1| so it is
2669 * naturally reset at the right points between |SSL_new|, |SSL_clear|, and
2670 * |ssl3_new|. */
2671
2672 ssl->rwstate = SSL_NOTHING;
2673
2674 BUF_MEM_free(ssl->init_buf);
2675 ssl->init_buf = NULL;
2676
2677 ssl_clear_cipher_ctx(ssl);
2678
2679 OPENSSL_free(ssl->next_proto_negotiated);
2680 ssl->next_proto_negotiated = NULL;
2681 ssl->next_proto_negotiated_len = 0;
2682
2683 /* The ssl->d1->mtu is simultaneously configuration (preserved across
2684 * clear) and connection-specific state (gets reset).
2685 *
2686 * TODO(davidben): Avoid this. */
2687 unsigned mtu = 0;
2688 if (ssl->d1 != NULL) {
2689 mtu = ssl->d1->mtu;
2690 }
2691
2692 ssl->method->ssl_free(ssl);
2693 if (!ssl->method->ssl_new(ssl)) {
2694 return 0;
2695 }
2696 ssl->enc_method = ssl3_get_enc_method(ssl->version);
2697 assert(ssl->enc_method != NULL);
2698
2699 if (SSL_IS_DTLS(ssl) && (SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
2700 ssl->d1->mtu = mtu;
2701 }
2702
2703 ssl->client_version = ssl->version;
2704
2705 return 1;
2706}
2707
David Benjaminda881e92015-04-26 14:45:04 -04002708int SSL_CTX_sess_connect(const SSL_CTX *ctx) { return 0; }
2709int SSL_CTX_sess_connect_good(const SSL_CTX *ctx) { return 0; }
2710int SSL_CTX_sess_connect_renegotiate(const SSL_CTX *ctx) { return 0; }
2711int SSL_CTX_sess_accept(const SSL_CTX *ctx) { return 0; }
2712int SSL_CTX_sess_accept_renegotiate(const SSL_CTX *ctx) { return 0; }
2713int SSL_CTX_sess_accept_good(const SSL_CTX *ctx) { return 0; }
2714int SSL_CTX_sess_hits(const SSL_CTX *ctx) { return 0; }
2715int SSL_CTX_sess_cb_hits(const SSL_CTX *ctx) { return 0; }
2716int SSL_CTX_sess_misses(const SSL_CTX *ctx) { return 0; }
2717int SSL_CTX_sess_timeouts(const SSL_CTX *ctx) { return 0; }
2718int SSL_CTX_sess_cache_full(const SSL_CTX *ctx) { return 0; }
Matt Braithwaite4838d8a2015-08-20 13:20:03 -07002719void ERR_load_SSL_strings(void) {}
David Benjamin27bbae42015-09-13 00:54:37 -04002720void SSL_load_error_strings(void) {}