blob: a1f72436a50aa342a90d4d7c3630fbe872eda7ac [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
Adam Langley95c29f32014-06-20 12:00:00 -0700141#include <assert.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400142#include <stdio.h>
143#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700144
David Benjamin39482a12014-07-20 13:30:15 -0400145#include <openssl/bytestring.h>
David Benjamin7c6d09b2015-09-05 11:00:19 -0400146#include <openssl/crypto.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700147#include <openssl/dh.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400148#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700149#include <openssl/lhash.h>
150#include <openssl/mem.h>
151#include <openssl/obj.h>
152#include <openssl/rand.h>
153#include <openssl/x509v3.h>
154
David Benjamin2ee94aa2015-04-07 22:38:30 -0400155#include "internal.h"
David Benjamin546f1a52015-04-15 16:46:09 -0400156#include "../crypto/internal.h"
157
Adam Langley95c29f32014-06-20 12:00:00 -0700158
David Benjamin76c2efc2015-08-31 14:24:29 -0400159/* |SSL_R_UNKNOWN_PROTOCOL| is no longer emitted, but continue to define it
David Benjamin97760d52015-07-24 23:02:49 -0400160 * to avoid downstream churn. */
David Benjamin76c2efc2015-08-31 14:24:29 -0400161OPENSSL_DECLARE_ERROR_REASON(SSL, UNKNOWN_PROTOCOL)
David Benjamin97760d52015-07-24 23:02:49 -0400162
Adam Langleyfcf25832014-12-18 17:42:32 -0800163/* Some error codes are special. Ensure the make_errors.go script never
164 * regresses this. */
165OPENSSL_COMPILE_ASSERT(SSL_R_TLSV1_ALERT_NO_RENEGOTIATION ==
166 SSL_AD_NO_RENEGOTIATION + SSL_AD_REASON_OFFSET,
167 ssl_alert_reason_code_mismatch);
David Benjamine1136082014-09-20 12:28:58 -0400168
David Benjamin1d0a1942015-04-26 15:35:35 -0400169/* kMaxHandshakeSize is the maximum size, in bytes, of a handshake message. */
170static const size_t kMaxHandshakeSize = (1u << 24) - 1;
171
David Benjaminaa585132015-06-29 23:36:17 -0400172static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl =
173 CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
174static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl_ctx =
175 CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
David Benjamin9f33fc62015-04-15 17:29:53 -0400176
David Benjamin7c6d09b2015-09-05 11:00:19 -0400177int SSL_library_init(void) {
178 CRYPTO_library_init();
179 return 1;
180}
181
David Benjamin27bbae42015-09-13 00:54:37 -0400182static uint32_t ssl_session_hash(const SSL_SESSION *a) {
183 uint32_t hash =
184 ((uint32_t)a->session_id[0]) ||
185 ((uint32_t)a->session_id[1] << 8) ||
186 ((uint32_t)a->session_id[2] << 16) ||
187 ((uint32_t)a->session_id[3] << 24);
David Benjamin7c6d09b2015-09-05 11:00:19 -0400188
David Benjamin27bbae42015-09-13 00:54:37 -0400189 return hash;
190}
191
192/* NB: If this function (or indeed the hash function which uses a sort of
193 * coarser function than this one) is changed, ensure
194 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
195 * able to construct an SSL_SESSION that will collide with any existing session
196 * with a matching session ID. */
197static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
198 if (a->ssl_version != b->ssl_version) {
199 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800200 }
Adam Langley95c29f32014-06-20 12:00:00 -0700201
David Benjamin27bbae42015-09-13 00:54:37 -0400202 if (a->session_id_length != b->session_id_length) {
203 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800204 }
Adam Langley95c29f32014-06-20 12:00:00 -0700205
David Benjamin27bbae42015-09-13 00:54:37 -0400206 return memcmp(a->session_id, b->session_id, a->session_id_length);
207}
Adam Langley95c29f32014-06-20 12:00:00 -0700208
David Benjamin27bbae42015-09-13 00:54:37 -0400209SSL_CTX *SSL_CTX_new(const SSL_METHOD *method) {
210 SSL_CTX *ret = NULL;
211
212 if (method == NULL) {
213 OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_METHOD_PASSED);
214 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800215 }
Adam Langley95c29f32014-06-20 12:00:00 -0700216
David Benjamin27bbae42015-09-13 00:54:37 -0400217 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
218 OPENSSL_PUT_ERROR(SSL, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
219 goto err;
David Benjamin62fd1622015-01-11 13:30:01 -0500220 }
221
David Benjamin27bbae42015-09-13 00:54:37 -0400222 ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
223 if (ret == NULL) {
224 goto err;
David Benjamin62fd1622015-01-11 13:30:01 -0500225 }
226
David Benjamin27bbae42015-09-13 00:54:37 -0400227 memset(ret, 0, sizeof(SSL_CTX));
Adam Langley95c29f32014-06-20 12:00:00 -0700228
David Benjamin27bbae42015-09-13 00:54:37 -0400229 ret->method = method->method;
230
231 CRYPTO_MUTEX_init(&ret->lock);
232
233 ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
234 ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
235
236 /* We take the system default */
237 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
238
239 ret->references = 1;
240
241 ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
242 ret->verify_mode = SSL_VERIFY_NONE;
243 ret->cert = ssl_cert_new();
244 if (ret->cert == NULL) {
245 goto err;
246 }
247
248 ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
249 if (ret->sessions == NULL) {
250 goto err;
251 }
252 ret->cert_store = X509_STORE_new();
253 if (ret->cert_store == NULL) {
254 goto err;
255 }
256
257 ssl_create_cipher_list(ret->method, &ret->cipher_list,
258 &ret->cipher_list_by_id, SSL_DEFAULT_CIPHER_LIST);
259 if (ret->cipher_list == NULL ||
260 sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0) {
261 OPENSSL_PUT_ERROR(SSL, SSL_R_LIBRARY_HAS_NO_CIPHERS);
262 goto err2;
263 }
264
265 ret->param = X509_VERIFY_PARAM_new();
266 if (!ret->param) {
267 goto err;
268 }
269
270 ret->client_CA = sk_X509_NAME_new_null();
271 if (ret->client_CA == NULL) {
272 goto err;
273 }
274
275 CRYPTO_new_ex_data(&g_ex_data_class_ssl_ctx, ret, &ret->ex_data);
276
277 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
278
279 /* Setup RFC4507 ticket keys */
280 if (!RAND_bytes(ret->tlsext_tick_key_name, 16) ||
281 !RAND_bytes(ret->tlsext_tick_hmac_key, 16) ||
282 !RAND_bytes(ret->tlsext_tick_aes_key, 16)) {
283 ret->options |= SSL_OP_NO_TICKET;
284 }
285
286 /* Default is to connect to non-RI servers. When RI is more widely deployed
287 * might change this. */
288 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
289
290 /* Lock the SSL_CTX to the specified version, for compatibility with legacy
291 * uses of SSL_METHOD. */
292 if (method->version != 0) {
293 SSL_CTX_set_max_version(ret, method->version);
294 SSL_CTX_set_min_version(ret, method->version);
295 }
296
297 return ret;
298
299err:
300 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
301err2:
302 SSL_CTX_free(ret);
303 return NULL;
304}
305
306void SSL_CTX_free(SSL_CTX *ctx) {
307 if (ctx == NULL ||
308 !CRYPTO_refcount_dec_and_test_zero(&ctx->references)) {
309 return;
310 }
311
312 X509_VERIFY_PARAM_free(ctx->param);
313
314 /* Free internal session cache. However: the remove_cb() may reference the
315 * ex_data of SSL_CTX, thus the ex_data store can only be removed after the
316 * sessions were flushed. As the ex_data handling routines might also touch
317 * the session cache, the most secure solution seems to be: empty (flush) the
318 * cache, then free ex_data, then finally free the cache. (See ticket
319 * [openssl.org #212].) */
320 SSL_CTX_flush_sessions(ctx, 0);
321
322 CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, ctx, &ctx->ex_data);
323
324 CRYPTO_MUTEX_cleanup(&ctx->lock);
325 lh_SSL_SESSION_free(ctx->sessions);
326 X509_STORE_free(ctx->cert_store);
327 ssl_cipher_preference_list_free(ctx->cipher_list);
328 sk_SSL_CIPHER_free(ctx->cipher_list_by_id);
329 ssl_cipher_preference_list_free(ctx->cipher_list_tls10);
330 ssl_cipher_preference_list_free(ctx->cipher_list_tls11);
331 ssl_cert_free(ctx->cert);
332 sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->client_custom_extensions,
333 SSL_CUSTOM_EXTENSION_free);
334 sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->server_custom_extensions,
335 SSL_CUSTOM_EXTENSION_free);
336 sk_X509_NAME_pop_free(ctx->client_CA, X509_NAME_free);
337 sk_SRTP_PROTECTION_PROFILE_free(ctx->srtp_profiles);
338 OPENSSL_free(ctx->psk_identity_hint);
339 OPENSSL_free(ctx->tlsext_ellipticcurvelist);
340 OPENSSL_free(ctx->alpn_client_proto_list);
341 OPENSSL_free(ctx->ocsp_response);
342 OPENSSL_free(ctx->signed_cert_timestamp_list);
343 EVP_PKEY_free(ctx->tlsext_channel_id_private);
344 BIO_free(ctx->keylog_bio);
345
346 OPENSSL_free(ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -0800347}
Adam Langley95c29f32014-06-20 12:00:00 -0700348
Adam Langleyfcf25832014-12-18 17:42:32 -0800349SSL *SSL_new(SSL_CTX *ctx) {
350 SSL *s;
Adam Langley95c29f32014-06-20 12:00:00 -0700351
Adam Langleyfcf25832014-12-18 17:42:32 -0800352 if (ctx == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400353 OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_CTX);
Adam Langleyfcf25832014-12-18 17:42:32 -0800354 return NULL;
355 }
356 if (ctx->method == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400357 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
Adam Langleyfcf25832014-12-18 17:42:32 -0800358 return NULL;
359 }
Adam Langley95c29f32014-06-20 12:00:00 -0700360
Adam Langleyfcf25832014-12-18 17:42:32 -0800361 s = (SSL *)OPENSSL_malloc(sizeof(SSL));
362 if (s == NULL) {
363 goto err;
364 }
365 memset(s, 0, sizeof(SSL));
Adam Langley95c29f32014-06-20 12:00:00 -0700366
Adam Langleyfcf25832014-12-18 17:42:32 -0800367 s->min_version = ctx->min_version;
368 s->max_version = ctx->max_version;
David Benjamin1eb367c2014-12-12 18:17:51 -0500369
Adam Langleyfcf25832014-12-18 17:42:32 -0800370 s->options = ctx->options;
371 s->mode = ctx->mode;
372 s->max_cert_list = ctx->max_cert_list;
Adam Langley95c29f32014-06-20 12:00:00 -0700373
David Benjamina5a3eeb2015-03-18 20:26:30 -0400374 s->cert = ssl_cert_dup(ctx->cert);
375 if (s->cert == NULL) {
376 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -0800377 }
Adam Langley95c29f32014-06-20 12:00:00 -0700378
Adam Langleyfcf25832014-12-18 17:42:32 -0800379 s->msg_callback = ctx->msg_callback;
380 s->msg_callback_arg = ctx->msg_callback_arg;
381 s->verify_mode = ctx->verify_mode;
382 s->sid_ctx_length = ctx->sid_ctx_length;
383 assert(s->sid_ctx_length <= sizeof s->sid_ctx);
384 memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));
385 s->verify_callback = ctx->default_verify_callback;
386 s->generate_session_id = ctx->generate_session_id;
Adam Langley95c29f32014-06-20 12:00:00 -0700387
Adam Langleyfcf25832014-12-18 17:42:32 -0800388 s->param = X509_VERIFY_PARAM_new();
389 if (!s->param) {
390 goto err;
391 }
392 X509_VERIFY_PARAM_inherit(s->param, ctx->param);
393 s->quiet_shutdown = ctx->quiet_shutdown;
394 s->max_send_fragment = ctx->max_send_fragment;
Adam Langley95c29f32014-06-20 12:00:00 -0700395
Adam Langley0b5e3902015-05-15 13:08:38 -0700396 CRYPTO_refcount_inc(&ctx->references);
Adam Langleyfcf25832014-12-18 17:42:32 -0800397 s->ctx = ctx;
Adam Langley0b5e3902015-05-15 13:08:38 -0700398 CRYPTO_refcount_inc(&ctx->references);
Adam Langleyfcf25832014-12-18 17:42:32 -0800399 s->initial_ctx = ctx;
Adam Langley95c29f32014-06-20 12:00:00 -0700400
Adam Langleyfcf25832014-12-18 17:42:32 -0800401 if (ctx->tlsext_ellipticcurvelist) {
402 s->tlsext_ellipticcurvelist =
403 BUF_memdup(ctx->tlsext_ellipticcurvelist,
404 ctx->tlsext_ellipticcurvelist_length * 2);
405 if (!s->tlsext_ellipticcurvelist) {
406 goto err;
407 }
408 s->tlsext_ellipticcurvelist_length = ctx->tlsext_ellipticcurvelist_length;
409 }
Adam Langley95c29f32014-06-20 12:00:00 -0700410
Adam Langleyfcf25832014-12-18 17:42:32 -0800411 if (s->ctx->alpn_client_proto_list) {
412 s->alpn_client_proto_list = BUF_memdup(s->ctx->alpn_client_proto_list,
413 s->ctx->alpn_client_proto_list_len);
414 if (s->alpn_client_proto_list == NULL) {
415 goto err;
416 }
417 s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;
418 }
Adam Langley95c29f32014-06-20 12:00:00 -0700419
Adam Langleyfcf25832014-12-18 17:42:32 -0800420 s->verify_result = X509_V_OK;
421 s->method = ctx->method;
Adam Langley95c29f32014-06-20 12:00:00 -0700422
Adam Langleyfcf25832014-12-18 17:42:32 -0800423 if (!s->method->ssl_new(s)) {
424 goto err;
425 }
426 s->enc_method = ssl3_get_enc_method(s->version);
427 assert(s->enc_method != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700428
David Benjamin62fd1622015-01-11 13:30:01 -0500429 s->rwstate = SSL_NOTHING;
Adam Langley95c29f32014-06-20 12:00:00 -0700430
David Benjamin9f33fc62015-04-15 17:29:53 -0400431 CRYPTO_new_ex_data(&g_ex_data_class_ssl, s, &s->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700432
Adam Langleyfcf25832014-12-18 17:42:32 -0800433 s->psk_identity_hint = NULL;
434 if (ctx->psk_identity_hint) {
435 s->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
436 if (s->psk_identity_hint == NULL) {
437 goto err;
438 }
439 }
440 s->psk_client_callback = ctx->psk_client_callback;
441 s->psk_server_callback = ctx->psk_server_callback;
Adam Langley95c29f32014-06-20 12:00:00 -0700442
David Benjamin02ddbfd2015-01-11 13:09:11 -0500443 s->tlsext_channel_id_enabled = ctx->tlsext_channel_id_enabled;
444 if (ctx->tlsext_channel_id_private) {
David Benjamin9a10f8f2015-05-05 22:22:40 -0400445 s->tlsext_channel_id_private =
446 EVP_PKEY_up_ref(ctx->tlsext_channel_id_private);
David Benjamin02ddbfd2015-01-11 13:09:11 -0500447 }
448
Adam Langleyfcf25832014-12-18 17:42:32 -0800449 s->signed_cert_timestamps_enabled = s->ctx->signed_cert_timestamps_enabled;
450 s->ocsp_stapling_enabled = s->ctx->ocsp_stapling_enabled;
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200451
Adam Langleyfcf25832014-12-18 17:42:32 -0800452 return s;
453
Adam Langley95c29f32014-06-20 12:00:00 -0700454err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400455 SSL_free(s);
David Benjamin3570d732015-06-29 00:28:17 -0400456 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700457
Adam Langleyfcf25832014-12-18 17:42:32 -0800458 return NULL;
459}
Adam Langley95c29f32014-06-20 12:00:00 -0700460
David Benjamin27bbae42015-09-13 00:54:37 -0400461void SSL_free(SSL *ssl) {
462 if (ssl == NULL) {
463 return;
464 }
465
466 X509_VERIFY_PARAM_free(ssl->param);
467
468 CRYPTO_free_ex_data(&g_ex_data_class_ssl, ssl, &ssl->ex_data);
469
470 if (ssl->bbio != NULL) {
471 /* If the buffering BIO is in place, pop it off */
472 if (ssl->bbio == ssl->wbio) {
473 ssl->wbio = BIO_pop(ssl->wbio);
474 }
475 BIO_free(ssl->bbio);
476 ssl->bbio = NULL;
477 }
478
479 int free_wbio = ssl->wbio != ssl->rbio;
480 BIO_free_all(ssl->rbio);
481 if (free_wbio) {
482 BIO_free_all(ssl->wbio);
483 }
484
485 BUF_MEM_free(ssl->init_buf);
486
487 /* add extra stuff */
488 ssl_cipher_preference_list_free(ssl->cipher_list);
489 sk_SSL_CIPHER_free(ssl->cipher_list_by_id);
490
491 ssl_clear_bad_session(ssl);
492 SSL_SESSION_free(ssl->session);
493
494 ssl_clear_cipher_ctx(ssl);
495
496 ssl_cert_free(ssl->cert);
497
498 OPENSSL_free(ssl->tlsext_hostname);
499 SSL_CTX_free(ssl->initial_ctx);
500 OPENSSL_free(ssl->tlsext_ellipticcurvelist);
501 OPENSSL_free(ssl->alpn_client_proto_list);
502 EVP_PKEY_free(ssl->tlsext_channel_id_private);
503 OPENSSL_free(ssl->psk_identity_hint);
504 sk_X509_NAME_pop_free(ssl->client_CA, X509_NAME_free);
505 OPENSSL_free(ssl->next_proto_negotiated);
506 sk_SRTP_PROTECTION_PROFILE_free(ssl->srtp_profiles);
507
508 if (ssl->method != NULL) {
509 ssl->method->ssl_free(ssl);
510 }
511 SSL_CTX_free(ssl->ctx);
512
513 OPENSSL_free(ssl);
514}
515
516void SSL_set_connect_state(SSL *ssl) {
517 ssl->server = 0;
518 ssl->shutdown = 0;
519 ssl->state = SSL_ST_CONNECT;
520 ssl->handshake_func = ssl->method->ssl_connect;
521 /* clear the current cipher */
522 ssl_clear_cipher_ctx(ssl);
523}
524
525void SSL_set_accept_state(SSL *ssl) {
526 ssl->server = 1;
527 ssl->shutdown = 0;
528 ssl->state = SSL_ST_ACCEPT;
529 ssl->handshake_func = ssl->method->ssl_accept;
530 /* clear the current cipher */
531 ssl_clear_cipher_ctx(ssl);
532}
533
534void SSL_set_bio(SSL *ssl, BIO *rbio, BIO *wbio) {
535 /* If the output buffering BIO is still in place, remove it. */
536 if (ssl->bbio != NULL) {
537 if (ssl->wbio == ssl->bbio) {
538 ssl->wbio = ssl->wbio->next_bio;
539 ssl->bbio->next_bio = NULL;
540 }
541 }
542
543 if (ssl->rbio != rbio) {
544 BIO_free_all(ssl->rbio);
545 }
546 if (ssl->wbio != wbio && ssl->rbio != ssl->wbio) {
547 BIO_free_all(ssl->wbio);
548 }
549 ssl->rbio = rbio;
550 ssl->wbio = wbio;
551}
552
553BIO *SSL_get_rbio(const SSL *ssl) { return ssl->rbio; }
554
555BIO *SSL_get_wbio(const SSL *ssl) { return ssl->wbio; }
556
557int SSL_do_handshake(SSL *ssl) {
558 if (ssl->handshake_func == NULL) {
559 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_TYPE_NOT_SET);
560 return -1;
561 }
562
563 if (!SSL_in_init(ssl)) {
564 return 1;
565 }
566
567 return ssl->handshake_func(ssl);
568}
569
570int SSL_connect(SSL *ssl) {
571 if (ssl->handshake_func == 0) {
572 /* Not properly initialized yet */
573 SSL_set_connect_state(ssl);
574 }
575
576 if (ssl->handshake_func != ssl->method->ssl_connect) {
577 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
578 return -1;
579 }
580
581 return ssl->handshake_func(ssl);
582}
583
584int SSL_accept(SSL *ssl) {
585 if (ssl->handshake_func == 0) {
586 /* Not properly initialized yet */
587 SSL_set_accept_state(ssl);
588 }
589
590 if (ssl->handshake_func != ssl->method->ssl_accept) {
591 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
592 return -1;
593 }
594
595 return ssl->handshake_func(ssl);
596}
597
598int SSL_read(SSL *ssl, void *buf, int num) {
599 if (ssl->handshake_func == 0) {
600 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
601 return -1;
602 }
603
604 if (ssl->shutdown & SSL_RECEIVED_SHUTDOWN) {
605 ssl->rwstate = SSL_NOTHING;
606 return 0;
607 }
608
609 ERR_clear_system_error();
610 return ssl->method->ssl_read_app_data(ssl, buf, num, 0);
611}
612
613int SSL_peek(SSL *ssl, void *buf, int num) {
614 if (ssl->handshake_func == 0) {
615 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
616 return -1;
617 }
618
619 if (ssl->shutdown & SSL_RECEIVED_SHUTDOWN) {
620 return 0;
621 }
622
623 ERR_clear_system_error();
624 return ssl->method->ssl_read_app_data(ssl, buf, num, 1);
625}
626
627int SSL_write(SSL *ssl, const void *buf, int num) {
628 if (ssl->handshake_func == 0) {
629 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
630 return -1;
631 }
632
633 if (ssl->shutdown & SSL_SENT_SHUTDOWN) {
634 ssl->rwstate = SSL_NOTHING;
635 OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
636 return -1;
637 }
638
639 ERR_clear_system_error();
640 return ssl->method->ssl_write_app_data(ssl, buf, num);
641}
642
643int SSL_shutdown(SSL *ssl) {
644 /* Note that this function behaves differently from what one might expect.
645 * Return values are 0 for no success (yet), 1 for success; but calling it
646 * once is usually not enough, even if blocking I/O is used (see
647 * ssl3_shutdown). */
648
649 if (ssl->handshake_func == 0) {
650 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
651 return -1;
652 }
653
654 if (SSL_in_init(ssl)) {
655 return 1;
656 }
657
658 /* Do nothing if configured not to send a close_notify. */
659 if (ssl->quiet_shutdown) {
660 ssl->shutdown = SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN;
661 return 1;
662 }
663
664 if (!(ssl->shutdown & SSL_SENT_SHUTDOWN)) {
665 ssl->shutdown |= SSL_SENT_SHUTDOWN;
666 ssl3_send_alert(ssl, SSL3_AL_WARNING, SSL_AD_CLOSE_NOTIFY);
667
668 /* our shutdown alert has been sent now, and if it still needs to be
669 * written, ssl->s3->alert_dispatch will be true */
670 if (ssl->s3->alert_dispatch) {
671 return -1; /* return WANT_WRITE */
672 }
673 } else if (ssl->s3->alert_dispatch) {
674 /* resend it if not sent */
675 int ret = ssl->method->ssl_dispatch_alert(ssl);
676 if (ret == -1) {
677 /* we only get to return -1 here the 2nd/Nth invocation, we must have
678 * already signalled return 0 upon a previous invoation, return
679 * WANT_WRITE */
680 return ret;
681 }
682 } else if (!(ssl->shutdown & SSL_RECEIVED_SHUTDOWN)) {
683 /* If we are waiting for a close from our peer, we are closed */
684 ssl->method->ssl_read_close_notify(ssl);
685 if (!(ssl->shutdown & SSL_RECEIVED_SHUTDOWN)) {
686 return -1; /* return WANT_READ */
687 }
688 }
689
690 if (ssl->shutdown == (SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN) &&
691 !ssl->s3->alert_dispatch) {
692 return 1;
693 } else {
694 return 0;
695 }
696}
697
698int SSL_get_error(const SSL *ssl, int ret_code) {
699 int reason;
700 uint32_t err;
701 BIO *bio;
702
703 if (ret_code > 0) {
704 return SSL_ERROR_NONE;
705 }
706
707 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
708 * where we do encode the error */
709 err = ERR_peek_error();
710 if (err != 0) {
711 if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
712 return SSL_ERROR_SYSCALL;
713 }
714 return SSL_ERROR_SSL;
715 }
716
717 if (ret_code == 0) {
718 if ((ssl->shutdown & SSL_RECEIVED_SHUTDOWN) &&
719 (ssl->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) {
720 /* The socket was cleanly shut down with a close_notify. */
721 return SSL_ERROR_ZERO_RETURN;
722 }
723 /* An EOF was observed which violates the protocol, and the underlying
724 * transport does not participate in the error queue. Bubble up to the
725 * caller. */
726 return SSL_ERROR_SYSCALL;
727 }
728
729 if (SSL_want_session(ssl)) {
730 return SSL_ERROR_PENDING_SESSION;
731 }
732
733 if (SSL_want_certificate(ssl)) {
734 return SSL_ERROR_PENDING_CERTIFICATE;
735 }
736
737 if (SSL_want_read(ssl)) {
738 bio = SSL_get_rbio(ssl);
739 if (BIO_should_read(bio)) {
740 return SSL_ERROR_WANT_READ;
741 }
742
743 if (BIO_should_write(bio)) {
744 /* This one doesn't make too much sense ... We never try to write to the
745 * rbio, and an application program where rbio and wbio are separate
746 * couldn't even know what it should wait for. However if we ever set
747 * s->rwstate incorrectly (so that we have SSL_want_read(s) instead of
748 * SSL_want_write(s)) and rbio and wbio *are* the same, this test works
749 * around that bug; so it might be safer to keep it. */
750 return SSL_ERROR_WANT_WRITE;
751 }
752
753 if (BIO_should_io_special(bio)) {
754 reason = BIO_get_retry_reason(bio);
755 if (reason == BIO_RR_CONNECT) {
756 return SSL_ERROR_WANT_CONNECT;
757 }
758
759 if (reason == BIO_RR_ACCEPT) {
760 return SSL_ERROR_WANT_ACCEPT;
761 }
762
763 return SSL_ERROR_SYSCALL; /* unknown */
764 }
765 }
766
767 if (SSL_want_write(ssl)) {
768 bio = SSL_get_wbio(ssl);
769 if (BIO_should_write(bio)) {
770 return SSL_ERROR_WANT_WRITE;
771 }
772
773 if (BIO_should_read(bio)) {
774 /* See above (SSL_want_read(ssl) with BIO_should_write(bio)) */
775 return SSL_ERROR_WANT_READ;
776 }
777
778 if (BIO_should_io_special(bio)) {
779 reason = BIO_get_retry_reason(bio);
780 if (reason == BIO_RR_CONNECT) {
781 return SSL_ERROR_WANT_CONNECT;
782 }
783
784 if (reason == BIO_RR_ACCEPT) {
785 return SSL_ERROR_WANT_ACCEPT;
786 }
787
788 return SSL_ERROR_SYSCALL;
789 }
790 }
791
792 if (SSL_want_x509_lookup(ssl)) {
793 return SSL_ERROR_WANT_X509_LOOKUP;
794 }
795
796 if (SSL_want_channel_id_lookup(ssl)) {
797 return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
798 }
799
800 if (SSL_want_private_key_operation(ssl)) {
801 return SSL_ERROR_WANT_PRIVATE_KEY_OPERATION;
802 }
803
804 return SSL_ERROR_SYSCALL;
805}
806
807void SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) {
808 ctx->min_version = version;
809}
810
811void SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) {
812 ctx->max_version = version;
813}
814
815void SSL_set_min_version(SSL *ssl, uint16_t version) {
816 ssl->min_version = version;
817}
818
819void SSL_set_max_version(SSL *ssl, uint16_t version) {
820 ssl->max_version = version;
821}
822
823uint32_t SSL_CTX_set_options(SSL_CTX *ctx, uint32_t options) {
824 ctx->options |= options;
825 return ctx->options;
826}
827
828uint32_t SSL_CTX_clear_options(SSL_CTX *ctx, uint32_t options) {
829 ctx->options &= ~options;
830 return ctx->options;
831}
832
833uint32_t SSL_CTX_get_options(const SSL_CTX *ctx) { return ctx->options; }
834
835uint32_t SSL_set_options(SSL *ssl, uint32_t options) {
836 ssl->options |= options;
837 return ssl->options;
838}
839
840uint32_t SSL_clear_options(SSL *ssl, uint32_t options) {
841 ssl->options &= ~options;
842 return ssl->options;
843}
844
845uint32_t SSL_get_options(const SSL *ssl) { return ssl->options; }
846
847uint32_t SSL_CTX_set_mode(SSL_CTX *ctx, uint32_t mode) {
848 ctx->mode |= mode;
849 return ctx->mode;
850}
851
852uint32_t SSL_CTX_clear_mode(SSL_CTX *ctx, uint32_t mode) {
853 ctx->mode &= ~mode;
854 return ctx->mode;
855}
856
857uint32_t SSL_CTX_get_mode(const SSL_CTX *ctx) { return ctx->mode; }
858
859uint32_t SSL_set_mode(SSL *ssl, uint32_t mode) {
860 ssl->mode |= mode;
861 return ssl->mode;
862}
863
864uint32_t SSL_clear_mode(SSL *ssl, uint32_t mode) {
865 ssl->mode &= ~mode;
866 return ssl->mode;
867}
868
869uint32_t SSL_get_mode(const SSL *ssl) { return ssl->mode; }
870
David Benjaminee0c8272015-09-13 01:03:54 -0400871X509 *SSL_get_peer_certificate(const SSL *ssl) {
872 if (ssl == NULL || ssl->session == NULL || ssl->session->peer == NULL) {
873 return NULL;
874 }
875 return X509_up_ref(ssl->session->peer);
876}
877
878STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *ssl) {
879 if (ssl == NULL || ssl->session == NULL) {
880 return NULL;
881 }
882 return ssl->session->cert_chain;
883}
884
885int SSL_get_tls_unique(const SSL *ssl, uint8_t *out, size_t *out_len,
886 size_t max_out) {
887 /* The tls-unique value is the first Finished message in the handshake, which
888 * is the client's in a full handshake and the server's for a resumption. See
889 * https://tools.ietf.org/html/rfc5929#section-3.1. */
890 const uint8_t *finished = ssl->s3->previous_client_finished;
891 size_t finished_len = ssl->s3->previous_client_finished_len;
892 if (ssl->hit) {
893 /* tls-unique is broken for resumed sessions unless EMS is used. */
894 if (!ssl->session->extended_master_secret) {
895 goto err;
896 }
897 finished = ssl->s3->previous_server_finished;
898 finished_len = ssl->s3->previous_server_finished_len;
899 }
900
901 if (!ssl->s3->initial_handshake_complete ||
902 ssl->version < TLS1_VERSION) {
903 goto err;
904 }
905
906 *out_len = finished_len;
907 if (finished_len > max_out) {
908 *out_len = max_out;
909 }
910
911 memcpy(out, finished, *out_len);
912 return 1;
913
914err:
915 *out_len = 0;
916 memset(out, 0, max_out);
917 return 0;
918}
919
Adam Langleyfcf25832014-12-18 17:42:32 -0800920int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
David Benjamindafbdd42015-09-14 01:40:10 -0400921 unsigned sid_ctx_len) {
922 if (sid_ctx_len > sizeof(ctx->sid_ctx)) {
David Benjamin3570d732015-06-29 00:28:17 -0400923 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -0800924 return 0;
925 }
926 ctx->sid_ctx_length = sid_ctx_len;
927 memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700928
Adam Langleyfcf25832014-12-18 17:42:32 -0800929 return 1;
930}
Adam Langley95c29f32014-06-20 12:00:00 -0700931
Adam Langleyfcf25832014-12-18 17:42:32 -0800932int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
David Benjamindafbdd42015-09-14 01:40:10 -0400933 unsigned sid_ctx_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800934 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
David Benjamin3570d732015-06-29 00:28:17 -0400935 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -0800936 return 0;
937 }
938 ssl->sid_ctx_length = sid_ctx_len;
939 memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700940
Adam Langleyfcf25832014-12-18 17:42:32 -0800941 return 1;
942}
Adam Langley95c29f32014-06-20 12:00:00 -0700943
Adam Langleyfcf25832014-12-18 17:42:32 -0800944int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800945 ctx->generate_session_id = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -0800946 return 1;
947}
Adam Langley95c29f32014-06-20 12:00:00 -0700948
Adam Langleyfcf25832014-12-18 17:42:32 -0800949int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800950 ssl->generate_session_id = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -0800951 return 1;
952}
Adam Langley95c29f32014-06-20 12:00:00 -0700953
Adam Langleyfcf25832014-12-18 17:42:32 -0800954int SSL_has_matching_session_id(const SSL *ssl, const uint8_t *id,
David Benjamindafbdd42015-09-14 01:40:10 -0400955 unsigned id_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800956 /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how we
957 * can "construct" a session to give us the desired check - ie. to find if
958 * there's a session in the hash table that would conflict with any new
959 * session built out of this id/id_len and the ssl_version in use by this
960 * SSL. */
961 SSL_SESSION r, *p;
Adam Langley95c29f32014-06-20 12:00:00 -0700962
Adam Langleyfcf25832014-12-18 17:42:32 -0800963 if (id_len > sizeof r.session_id) {
964 return 0;
965 }
Adam Langley95c29f32014-06-20 12:00:00 -0700966
Adam Langleyfcf25832014-12-18 17:42:32 -0800967 r.ssl_version = ssl->version;
968 r.session_id_length = id_len;
969 memcpy(r.session_id, id, id_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700970
Adam Langley4bdb6e42015-05-15 15:29:21 -0700971 CRYPTO_MUTEX_lock_read(&ssl->ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -0800972 p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r);
Adam Langley4bdb6e42015-05-15 15:29:21 -0700973 CRYPTO_MUTEX_unlock(&ssl->ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -0800974 return p != NULL;
975}
Adam Langley95c29f32014-06-20 12:00:00 -0700976
Adam Langleyfcf25832014-12-18 17:42:32 -0800977int SSL_CTX_set_purpose(SSL_CTX *s, int purpose) {
978 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
979}
980
981int SSL_set_purpose(SSL *s, int purpose) {
982 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
983}
984
985int SSL_CTX_set_trust(SSL_CTX *s, int trust) {
986 return X509_VERIFY_PARAM_set_trust(s->param, trust);
987}
988
989int SSL_set_trust(SSL *s, int trust) {
990 return X509_VERIFY_PARAM_set_trust(s->param, trust);
991}
992
993int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm) {
994 return X509_VERIFY_PARAM_set1(ctx->param, vpm);
995}
996
997int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm) {
998 return X509_VERIFY_PARAM_set1(ssl->param, vpm);
999}
Adam Langley95c29f32014-06-20 12:00:00 -07001000
Adam Langley858a88d2014-06-20 12:00:00 -07001001void ssl_cipher_preference_list_free(
Adam Langleyfcf25832014-12-18 17:42:32 -08001002 struct ssl_cipher_preference_list_st *cipher_list) {
David Benjamin5d1ec732015-04-22 13:38:00 -04001003 if (cipher_list == NULL) {
1004 return;
1005 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001006 sk_SSL_CIPHER_free(cipher_list->ciphers);
1007 OPENSSL_free(cipher_list->in_group_flags);
1008 OPENSSL_free(cipher_list);
1009}
Adam Langley858a88d2014-06-20 12:00:00 -07001010
Adam Langleyfcf25832014-12-18 17:42:32 -08001011struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_dup(
1012 struct ssl_cipher_preference_list_st *cipher_list) {
1013 struct ssl_cipher_preference_list_st *ret = NULL;
1014 size_t n = sk_SSL_CIPHER_num(cipher_list->ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -07001015
Adam Langleyfcf25832014-12-18 17:42:32 -08001016 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
1017 if (!ret) {
1018 goto err;
1019 }
1020
1021 ret->ciphers = NULL;
1022 ret->in_group_flags = NULL;
1023 ret->ciphers = sk_SSL_CIPHER_dup(cipher_list->ciphers);
1024 if (!ret->ciphers) {
1025 goto err;
1026 }
1027 ret->in_group_flags = BUF_memdup(cipher_list->in_group_flags, n);
1028 if (!ret->in_group_flags) {
1029 goto err;
1030 }
1031
1032 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -07001033
1034err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001035 ssl_cipher_preference_list_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -08001036 return NULL;
1037}
Adam Langley858a88d2014-06-20 12:00:00 -07001038
Adam Langleyfcf25832014-12-18 17:42:32 -08001039struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_from_ciphers(
David Benjamin60da0cd2015-05-03 15:21:28 -04001040 STACK_OF(SSL_CIPHER) *ciphers) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001041 struct ssl_cipher_preference_list_st *ret = NULL;
1042 size_t n = sk_SSL_CIPHER_num(ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -07001043
Adam Langleyfcf25832014-12-18 17:42:32 -08001044 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
1045 if (!ret) {
1046 goto err;
1047 }
1048 ret->ciphers = NULL;
1049 ret->in_group_flags = NULL;
1050 ret->ciphers = sk_SSL_CIPHER_dup(ciphers);
1051 if (!ret->ciphers) {
1052 goto err;
1053 }
1054 ret->in_group_flags = OPENSSL_malloc(n);
1055 if (!ret->in_group_flags) {
1056 goto err;
1057 }
1058 memset(ret->in_group_flags, 0, n);
1059 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -07001060
1061err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001062 ssl_cipher_preference_list_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -08001063 return NULL;
1064}
Adam Langley858a88d2014-06-20 12:00:00 -07001065
Adam Langleyfcf25832014-12-18 17:42:32 -08001066X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) { return ctx->param; }
Adam Langley95c29f32014-06-20 12:00:00 -07001067
Adam Langleyfcf25832014-12-18 17:42:32 -08001068X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) { return ssl->param; }
Adam Langley95c29f32014-06-20 12:00:00 -07001069
David Benjamin7481d392015-07-05 19:38:46 -04001070void SSL_certs_clear(SSL *ssl) { ssl_cert_clear_certs(ssl->cert); }
Adam Langley95c29f32014-06-20 12:00:00 -07001071
Adam Langleyfcf25832014-12-18 17:42:32 -08001072int SSL_get_fd(const SSL *s) { return SSL_get_rfd(s); }
Adam Langley95c29f32014-06-20 12:00:00 -07001073
Adam Langleyfcf25832014-12-18 17:42:32 -08001074int SSL_get_rfd(const SSL *s) {
1075 int ret = -1;
1076 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -07001077
Adam Langleyfcf25832014-12-18 17:42:32 -08001078 b = SSL_get_rbio(s);
1079 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
1080 if (r != NULL) {
1081 BIO_get_fd(r, &ret);
1082 }
1083 return ret;
1084}
Adam Langley95c29f32014-06-20 12:00:00 -07001085
Adam Langleyfcf25832014-12-18 17:42:32 -08001086int SSL_get_wfd(const SSL *s) {
1087 int ret = -1;
1088 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -07001089
Adam Langleyfcf25832014-12-18 17:42:32 -08001090 b = SSL_get_wbio(s);
1091 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
1092 if (r != NULL) {
1093 BIO_get_fd(r, &ret);
1094 }
Adam Langley95c29f32014-06-20 12:00:00 -07001095
Adam Langleyfcf25832014-12-18 17:42:32 -08001096 return ret;
1097}
Adam Langley95c29f32014-06-20 12:00:00 -07001098
Adam Langleyfcf25832014-12-18 17:42:32 -08001099int SSL_set_fd(SSL *s, int fd) {
1100 int ret = 0;
1101 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001102
Adam Langleyfcf25832014-12-18 17:42:32 -08001103 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -07001104
Adam Langleyfcf25832014-12-18 17:42:32 -08001105 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001106 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -08001107 goto err;
1108 }
1109 BIO_set_fd(bio, fd, BIO_NOCLOSE);
1110 SSL_set_bio(s, bio, bio);
1111 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001112
Adam Langley95c29f32014-06-20 12:00:00 -07001113err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001114 return ret;
1115}
Adam Langley95c29f32014-06-20 12:00:00 -07001116
Adam Langleyfcf25832014-12-18 17:42:32 -08001117int SSL_set_wfd(SSL *s, int fd) {
1118 int ret = 0;
1119 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001120
Adam Langleyfcf25832014-12-18 17:42:32 -08001121 if (s->rbio == NULL || BIO_method_type(s->rbio) != BIO_TYPE_FD ||
1122 (int)BIO_get_fd(s->rbio, NULL) != fd) {
1123 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -07001124
Adam Langleyfcf25832014-12-18 17:42:32 -08001125 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001126 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -08001127 goto err;
1128 }
1129 BIO_set_fd(bio, fd, BIO_NOCLOSE);
1130 SSL_set_bio(s, SSL_get_rbio(s), bio);
1131 } else {
1132 SSL_set_bio(s, SSL_get_rbio(s), SSL_get_rbio(s));
1133 }
1134
1135 ret = 1;
1136
Adam Langley95c29f32014-06-20 12:00:00 -07001137err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001138 return ret;
1139}
Adam Langley95c29f32014-06-20 12:00:00 -07001140
Adam Langleyfcf25832014-12-18 17:42:32 -08001141int SSL_set_rfd(SSL *s, int fd) {
1142 int ret = 0;
1143 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001144
Adam Langleyfcf25832014-12-18 17:42:32 -08001145 if (s->wbio == NULL || BIO_method_type(s->wbio) != BIO_TYPE_FD ||
1146 (int)BIO_get_fd(s->wbio, NULL) != fd) {
1147 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -07001148
Adam Langleyfcf25832014-12-18 17:42:32 -08001149 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001150 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -08001151 goto err;
1152 }
1153 BIO_set_fd(bio, fd, BIO_NOCLOSE);
1154 SSL_set_bio(s, bio, SSL_get_wbio(s));
1155 } else {
1156 SSL_set_bio(s, SSL_get_wbio(s), SSL_get_wbio(s));
1157 }
1158 ret = 1;
1159
Adam Langley95c29f32014-06-20 12:00:00 -07001160err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001161 return ret;
1162}
Adam Langley95c29f32014-06-20 12:00:00 -07001163
1164/* return length of latest Finished message we sent, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -08001165size_t SSL_get_finished(const SSL *s, void *buf, size_t count) {
1166 size_t ret = 0;
1167
1168 if (s->s3 != NULL) {
1169 ret = s->s3->tmp.finish_md_len;
1170 if (count > ret) {
1171 count = ret;
1172 }
1173 memcpy(buf, s->s3->tmp.finish_md, count);
1174 }
1175
1176 return ret;
1177}
Adam Langley95c29f32014-06-20 12:00:00 -07001178
1179/* return length of latest Finished message we expected, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -08001180size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count) {
1181 size_t ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001182
Adam Langleyfcf25832014-12-18 17:42:32 -08001183 if (s->s3 != NULL) {
1184 ret = s->s3->tmp.peer_finish_md_len;
1185 if (count > ret) {
1186 count = ret;
1187 }
1188 memcpy(buf, s->s3->tmp.peer_finish_md, count);
1189 }
Adam Langley95c29f32014-06-20 12:00:00 -07001190
Adam Langleyfcf25832014-12-18 17:42:32 -08001191 return ret;
1192}
Adam Langley95c29f32014-06-20 12:00:00 -07001193
Adam Langleyfcf25832014-12-18 17:42:32 -08001194int SSL_get_verify_mode(const SSL *s) { return s->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -07001195
Adam Langleyfcf25832014-12-18 17:42:32 -08001196int SSL_get_verify_depth(const SSL *s) {
1197 return X509_VERIFY_PARAM_get_depth(s->param);
1198}
Adam Langley95c29f32014-06-20 12:00:00 -07001199
Adam Langleyfcf25832014-12-18 17:42:32 -08001200int (*SSL_get_verify_callback(const SSL *s))(int, X509_STORE_CTX *) {
1201 return s->verify_callback;
1202}
Adam Langley95c29f32014-06-20 12:00:00 -07001203
Adam Langleyfcf25832014-12-18 17:42:32 -08001204int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) { return ctx->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -07001205
Adam Langleyfcf25832014-12-18 17:42:32 -08001206int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) {
1207 return X509_VERIFY_PARAM_get_depth(ctx->param);
1208}
Adam Langley95c29f32014-06-20 12:00:00 -07001209
Adam Langleyfcf25832014-12-18 17:42:32 -08001210int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int, X509_STORE_CTX *) {
1211 return ctx->default_verify_callback;
1212}
Adam Langley95c29f32014-06-20 12:00:00 -07001213
Adam Langleyfcf25832014-12-18 17:42:32 -08001214void SSL_set_verify(SSL *s, int mode,
1215 int (*callback)(int ok, X509_STORE_CTX *ctx)) {
1216 s->verify_mode = mode;
1217 if (callback != NULL) {
1218 s->verify_callback = callback;
1219 }
1220}
Adam Langley95c29f32014-06-20 12:00:00 -07001221
Adam Langleyfcf25832014-12-18 17:42:32 -08001222void SSL_set_verify_depth(SSL *s, int depth) {
1223 X509_VERIFY_PARAM_set_depth(s->param, depth);
1224}
Adam Langley95c29f32014-06-20 12:00:00 -07001225
David Benjamin9a41d1b2015-05-16 01:30:09 -04001226int SSL_CTX_get_read_ahead(const SSL_CTX *ctx) { return 0; }
Adam Langley95c29f32014-06-20 12:00:00 -07001227
David Benjamin9a41d1b2015-05-16 01:30:09 -04001228int SSL_get_read_ahead(const SSL *s) { return 0; }
Adam Langley95c29f32014-06-20 12:00:00 -07001229
David Benjamin9a41d1b2015-05-16 01:30:09 -04001230void SSL_CTX_set_read_ahead(SSL_CTX *ctx, int yes) { }
David Benjamin61ecccf2015-05-05 09:44:51 -04001231
David Benjamin9a41d1b2015-05-16 01:30:09 -04001232void SSL_set_read_ahead(SSL *s, int yes) { }
David Benjamin61ecccf2015-05-05 09:44:51 -04001233
Adam Langleyfcf25832014-12-18 17:42:32 -08001234int SSL_pending(const SSL *s) {
David Benjamin904dc722015-05-30 16:39:07 -04001235 return (s->s3->rrec.type == SSL3_RT_APPLICATION_DATA) ? s->s3->rrec.length
1236 : 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001237}
Adam Langley95c29f32014-06-20 12:00:00 -07001238
Adam Langley95c29f32014-06-20 12:00:00 -07001239/* Fix this so it checks all the valid key/cert options */
Adam Langleyfcf25832014-12-18 17:42:32 -08001240int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
David Benjamin651b3d92015-08-09 12:07:25 -04001241 if (ctx->cert->x509 == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001242 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -08001243 return 0;
1244 }
1245
David Benjamind1d80782015-07-05 11:54:09 -04001246 if (ctx->cert->privatekey == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001247 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -08001248 return 0;
1249 }
1250
David Benjamind1d80782015-07-05 11:54:09 -04001251 return X509_check_private_key(ctx->cert->x509, ctx->cert->privatekey);
Adam Langleyfcf25832014-12-18 17:42:32 -08001252}
Adam Langley95c29f32014-06-20 12:00:00 -07001253
1254/* Fix this function so that it takes an optional type parameter */
Adam Langleyfcf25832014-12-18 17:42:32 -08001255int SSL_check_private_key(const SSL *ssl) {
David Benjamind1d80782015-07-05 11:54:09 -04001256 if (ssl->cert->x509 == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001257 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -08001258 return 0;
1259 }
David Benjamin0b145c22014-11-26 20:10:09 -05001260
David Benjamind1d80782015-07-05 11:54:09 -04001261 if (ssl->cert->privatekey == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001262 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -08001263 return 0;
1264 }
Adam Langley95c29f32014-06-20 12:00:00 -07001265
David Benjamind1d80782015-07-05 11:54:09 -04001266 return X509_check_private_key(ssl->cert->x509, ssl->cert->privatekey);
Adam Langleyfcf25832014-12-18 17:42:32 -08001267}
Adam Langley95c29f32014-06-20 12:00:00 -07001268
Adam Langleyfcf25832014-12-18 17:42:32 -08001269long SSL_get_default_timeout(const SSL *s) {
1270 return SSL_DEFAULT_SESSION_TIMEOUT;
1271}
Adam Langley95c29f32014-06-20 12:00:00 -07001272
David Benjamin44d3eed2015-05-21 01:29:55 -04001273int SSL_renegotiate(SSL *ssl) {
1274 /* Caller-initiated renegotiation is not supported. */
David Benjamin3570d732015-06-29 00:28:17 -04001275 OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
David Benjamin44d3eed2015-05-21 01:29:55 -04001276 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001277}
Adam Langley95c29f32014-06-20 12:00:00 -07001278
David Benjamin44d3eed2015-05-21 01:29:55 -04001279int SSL_renegotiate_pending(SSL *ssl) {
1280 return SSL_in_init(ssl) && ssl->s3->initial_handshake_complete;
Adam Langleyfcf25832014-12-18 17:42:32 -08001281}
Adam Langley95c29f32014-06-20 12:00:00 -07001282
David Benjamin1d0a1942015-04-26 15:35:35 -04001283size_t SSL_CTX_get_max_cert_list(const SSL_CTX *ctx) {
1284 return ctx->max_cert_list;
1285}
1286
1287void SSL_CTX_set_max_cert_list(SSL_CTX *ctx, size_t max_cert_list) {
1288 if (max_cert_list > kMaxHandshakeSize) {
1289 max_cert_list = kMaxHandshakeSize;
1290 }
1291 ctx->max_cert_list = (uint32_t)max_cert_list;
1292}
1293
1294size_t SSL_get_max_cert_list(const SSL *ssl) {
1295 return ssl->max_cert_list;
1296}
1297
1298void SSL_set_max_cert_list(SSL *ssl, size_t max_cert_list) {
1299 if (max_cert_list > kMaxHandshakeSize) {
1300 max_cert_list = kMaxHandshakeSize;
1301 }
1302 ssl->max_cert_list = (uint32_t)max_cert_list;
1303}
1304
1305void SSL_CTX_set_max_send_fragment(SSL_CTX *ctx, size_t max_send_fragment) {
1306 if (max_send_fragment < 512) {
1307 max_send_fragment = 512;
1308 }
1309 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1310 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1311 }
1312 ctx->max_send_fragment = (uint16_t)max_send_fragment;
1313}
1314
1315void SSL_set_max_send_fragment(SSL *ssl, size_t max_send_fragment) {
1316 if (max_send_fragment < 512) {
1317 max_send_fragment = 512;
1318 }
1319 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1320 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1321 }
1322 ssl->max_send_fragment = (uint16_t)max_send_fragment;
1323}
1324
David Benjamincb9cf792015-05-05 09:46:14 -04001325int SSL_set_mtu(SSL *ssl, unsigned mtu) {
1326 if (!SSL_IS_DTLS(ssl) || mtu < dtls1_min_mtu()) {
1327 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001328 }
David Benjamincb9cf792015-05-05 09:46:14 -04001329 ssl->d1->mtu = mtu;
1330 return 1;
1331}
1332
1333int SSL_get_secure_renegotiation_support(const SSL *ssl) {
1334 return ssl->s3->send_connection_binding;
1335}
1336
Adam Langleyfcf25832014-12-18 17:42:32 -08001337LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) { return ctx->sessions; }
1338
David Benjamin71f7d3d2015-05-05 09:46:38 -04001339size_t SSL_CTX_sess_number(const SSL_CTX *ctx) {
1340 return lh_SSL_SESSION_num_items(ctx->sessions);
1341}
1342
1343unsigned long SSL_CTX_sess_set_cache_size(SSL_CTX *ctx, unsigned long size) {
1344 unsigned long ret = ctx->session_cache_size;
1345 ctx->session_cache_size = size;
1346 return ret;
1347}
1348
1349unsigned long SSL_CTX_sess_get_cache_size(const SSL_CTX *ctx) {
1350 return ctx->session_cache_size;
1351}
1352
1353int SSL_CTX_set_session_cache_mode(SSL_CTX *ctx, int mode) {
1354 int ret = ctx->session_cache_mode;
1355 ctx->session_cache_mode = mode;
1356 return ret;
1357}
1358
1359int SSL_CTX_get_session_cache_mode(const SSL_CTX *ctx) {
1360 return ctx->session_cache_mode;
1361}
1362
Adam Langleyfcf25832014-12-18 17:42:32 -08001363/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001364 * preference */
David Benjamin60da0cd2015-05-03 15:21:28 -04001365STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001366 if (s == NULL) {
1367 return NULL;
1368 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001369
Adam Langleyfcf25832014-12-18 17:42:32 -08001370 if (s->cipher_list != NULL) {
1371 return s->cipher_list->ciphers;
1372 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001373
Adam Langleyfcf25832014-12-18 17:42:32 -08001374 if (s->version >= TLS1_1_VERSION && s->ctx != NULL &&
1375 s->ctx->cipher_list_tls11 != NULL) {
1376 return s->ctx->cipher_list_tls11->ciphers;
1377 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001378
Adam Langleycef75832015-09-03 14:51:12 -07001379 if (s->version >= TLS1_VERSION && s->ctx != NULL &&
1380 s->ctx->cipher_list_tls10 != NULL) {
1381 return s->ctx->cipher_list_tls10->ciphers;
1382 }
1383
Adam Langleyfcf25832014-12-18 17:42:32 -08001384 if (s->ctx != NULL && s->ctx->cipher_list != NULL) {
1385 return s->ctx->cipher_list->ciphers;
1386 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001387
Adam Langleyfcf25832014-12-18 17:42:32 -08001388 return NULL;
1389}
Adam Langley95c29f32014-06-20 12:00:00 -07001390
Adam Langleyfcf25832014-12-18 17:42:32 -08001391/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001392 * algorithm id */
David Benjamin60da0cd2015-05-03 15:21:28 -04001393STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001394 if (s == NULL) {
1395 return NULL;
1396 }
Adam Langley95c29f32014-06-20 12:00:00 -07001397
Adam Langleyfcf25832014-12-18 17:42:32 -08001398 if (s->cipher_list_by_id != NULL) {
1399 return s->cipher_list_by_id;
1400 }
Adam Langley95c29f32014-06-20 12:00:00 -07001401
Adam Langleyfcf25832014-12-18 17:42:32 -08001402 if (s->ctx != NULL && s->ctx->cipher_list_by_id != NULL) {
1403 return s->ctx->cipher_list_by_id;
1404 }
Adam Langley95c29f32014-06-20 12:00:00 -07001405
Adam Langleyfcf25832014-12-18 17:42:32 -08001406 return NULL;
1407}
Adam Langley95c29f32014-06-20 12:00:00 -07001408
Adam Langleyfcf25832014-12-18 17:42:32 -08001409/* The old interface to get the same thing as SSL_get_ciphers() */
1410const char *SSL_get_cipher_list(const SSL *s, int n) {
1411 const SSL_CIPHER *c;
David Benjamin60da0cd2015-05-03 15:21:28 -04001412 STACK_OF(SSL_CIPHER) *sk;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001413
Adam Langleyfcf25832014-12-18 17:42:32 -08001414 if (s == NULL) {
1415 return NULL;
1416 }
Adam Langley95c29f32014-06-20 12:00:00 -07001417
Adam Langleyfcf25832014-12-18 17:42:32 -08001418 sk = SSL_get_ciphers(s);
1419 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
1420 return NULL;
1421 }
Adam Langley95c29f32014-06-20 12:00:00 -07001422
Adam Langleyfcf25832014-12-18 17:42:32 -08001423 c = sk_SSL_CIPHER_value(sk, n);
1424 if (c == NULL) {
1425 return NULL;
1426 }
Adam Langley95c29f32014-06-20 12:00:00 -07001427
Adam Langleyfcf25832014-12-18 17:42:32 -08001428 return c->name;
1429}
David Benjamin5491e3f2014-09-29 19:33:09 -04001430
Adam Langleyfcf25832014-12-18 17:42:32 -08001431/* specify the ciphers to be used by default by the SSL_CTX */
1432int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
1433 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001434
Adam Langleyfcf25832014-12-18 17:42:32 -08001435 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001436 &ctx->cipher_list_by_id, str);
Adam Langleyfcf25832014-12-18 17:42:32 -08001437 /* ssl_create_cipher_list may return an empty stack if it was unable to find
1438 * a cipher matching the given rule string (for example if the rule string
1439 * specifies a cipher which has been disabled). This is not an error as far
1440 * as ssl_create_cipher_list is concerned, and hence ctx->cipher_list and
1441 * ctx->cipher_list_by_id has been updated. */
1442 if (sk == NULL) {
1443 return 0;
1444 } else if (sk_SSL_CIPHER_num(sk) == 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001445 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
Adam Langleyfcf25832014-12-18 17:42:32 -08001446 return 0;
1447 }
Adam Langley95c29f32014-06-20 12:00:00 -07001448
Adam Langleyfcf25832014-12-18 17:42:32 -08001449 return 1;
1450}
David Benjamin39482a12014-07-20 13:30:15 -04001451
Adam Langleycef75832015-09-03 14:51:12 -07001452int SSL_CTX_set_cipher_list_tls10(SSL_CTX *ctx, const char *str) {
1453 STACK_OF(SSL_CIPHER) *sk;
1454
1455 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls10, NULL, str);
1456 if (sk == NULL) {
1457 return 0;
1458 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1459 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
1460 return 0;
1461 }
1462
1463 return 1;
1464}
1465
Adam Langleyfcf25832014-12-18 17:42:32 -08001466int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str) {
1467 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001468
David Benjamin71f07942015-04-08 02:36:59 -04001469 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str);
Adam Langleyfcf25832014-12-18 17:42:32 -08001470 if (sk == NULL) {
1471 return 0;
1472 } else if (sk_SSL_CIPHER_num(sk) == 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001473 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
Adam Langleyfcf25832014-12-18 17:42:32 -08001474 return 0;
1475 }
David Benjamin9f2c0d72014-10-21 22:00:19 -04001476
Adam Langleyfcf25832014-12-18 17:42:32 -08001477 return 1;
1478}
Adam Langley95c29f32014-06-20 12:00:00 -07001479
Adam Langleyfcf25832014-12-18 17:42:32 -08001480/* specify the ciphers to be used by the SSL */
1481int SSL_set_cipher_list(SSL *s, const char *str) {
1482 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001483
Adam Langleyfcf25832014-12-18 17:42:32 -08001484 sk = ssl_create_cipher_list(s->ctx->method, &s->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001485 &s->cipher_list_by_id, str);
David Benjamin39482a12014-07-20 13:30:15 -04001486
Adam Langleyfcf25832014-12-18 17:42:32 -08001487 /* see comment in SSL_CTX_set_cipher_list */
1488 if (sk == NULL) {
1489 return 0;
1490 } else if (sk_SSL_CIPHER_num(sk) == 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001491 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
Adam Langleyfcf25832014-12-18 17:42:32 -08001492 return 0;
1493 }
David Benjamin39482a12014-07-20 13:30:15 -04001494
Adam Langleyfcf25832014-12-18 17:42:32 -08001495 return 1;
1496}
Adam Langley95c29f32014-06-20 12:00:00 -07001497
Adam Langleyfcf25832014-12-18 17:42:32 -08001498int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, uint8_t *p) {
1499 size_t i;
1500 const SSL_CIPHER *c;
1501 CERT *ct = s->cert;
1502 uint8_t *q;
1503 /* Set disabled masks for this session */
1504 ssl_set_client_disabled(s);
Adam Langley29707792014-06-20 12:00:00 -07001505
Adam Langleyfcf25832014-12-18 17:42:32 -08001506 if (sk == NULL) {
1507 return 0;
1508 }
1509 q = p;
Adam Langley95c29f32014-06-20 12:00:00 -07001510
Adam Langleyfcf25832014-12-18 17:42:32 -08001511 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
1512 c = sk_SSL_CIPHER_value(sk, i);
1513 /* Skip disabled ciphers */
1514 if (c->algorithm_ssl & ct->mask_ssl ||
1515 c->algorithm_mkey & ct->mask_k ||
1516 c->algorithm_auth & ct->mask_a) {
1517 continue;
1518 }
David Benjamina1c90a52015-05-30 17:03:14 -04001519 s2n(ssl_cipher_get_value(c), p);
Adam Langleyfcf25832014-12-18 17:42:32 -08001520 }
1521
1522 /* If all ciphers were disabled, return the error to the caller. */
1523 if (p == q) {
1524 return 0;
1525 }
1526
Adam Langley5021b222015-06-12 18:27:58 -07001527 /* For SSLv3, the SCSV is added. Otherwise the renegotiation extension is
1528 * added. */
1529 if (s->client_version == SSL3_VERSION &&
1530 !s->s3->initial_handshake_complete) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001531 s2n(SSL3_CK_SCSV & 0xffff, p);
Adam Langley5021b222015-06-12 18:27:58 -07001532 /* The renegotiation extension is required to be at index zero. */
1533 s->s3->tmp.extensions.sent |= (1u << 0);
Adam Langleyfcf25832014-12-18 17:42:32 -08001534 }
1535
Adam Langley5f0efe02015-02-20 13:03:16 -08001536 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001537 s2n(SSL3_CK_FALLBACK_SCSV & 0xffff, p);
1538 }
1539
1540 return p - q;
1541}
1542
1543STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs) {
1544 CBS cipher_suites = *cbs;
1545 const SSL_CIPHER *c;
David Benjamin60da0cd2015-05-03 15:21:28 -04001546 STACK_OF(SSL_CIPHER) *sk;
Adam Langleyfcf25832014-12-18 17:42:32 -08001547
1548 if (s->s3) {
1549 s->s3->send_connection_binding = 0;
1550 }
1551
1552 if (CBS_len(&cipher_suites) % 2 != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001553 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
Adam Langleyfcf25832014-12-18 17:42:32 -08001554 return NULL;
1555 }
1556
1557 sk = sk_SSL_CIPHER_new_null();
1558 if (sk == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001559 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001560 goto err;
1561 }
1562
Adam Langleyfcf25832014-12-18 17:42:32 -08001563 while (CBS_len(&cipher_suites) > 0) {
1564 uint16_t cipher_suite;
1565
1566 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
David Benjamin3570d732015-06-29 00:28:17 -04001567 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001568 goto err;
1569 }
1570
1571 /* Check for SCSV. */
1572 if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff)) {
1573 /* SCSV is fatal if renegotiating. */
David Benjamin20f6e972015-05-15 21:51:49 -04001574 if (s->s3->initial_handshake_complete) {
David Benjamin3570d732015-06-29 00:28:17 -04001575 OPENSSL_PUT_ERROR(SSL, SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
Adam Langleyfcf25832014-12-18 17:42:32 -08001576 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1577 goto err;
1578 }
1579 s->s3->send_connection_binding = 1;
1580 continue;
1581 }
1582
1583 /* Check for FALLBACK_SCSV. */
1584 if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff)) {
1585 uint16_t max_version = ssl3_get_max_server_version(s);
1586 if (SSL_IS_DTLS(s) ? (uint16_t)s->version > max_version
1587 : (uint16_t)s->version < max_version) {
David Benjamin3570d732015-06-29 00:28:17 -04001588 OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK);
Adam Langleyfcf25832014-12-18 17:42:32 -08001589 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
1590 goto err;
1591 }
1592 continue;
1593 }
1594
David Benjamina1c90a52015-05-30 17:03:14 -04001595 c = SSL_get_cipher_by_value(cipher_suite);
Adam Langleyfcf25832014-12-18 17:42:32 -08001596 if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
David Benjamin3570d732015-06-29 00:28:17 -04001597 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001598 goto err;
1599 }
1600 }
1601
1602 return sk;
David Benjamin9f2c0d72014-10-21 22:00:19 -04001603
Adam Langley95c29f32014-06-20 12:00:00 -07001604err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001605 sk_SSL_CIPHER_free(sk);
Adam Langleyfcf25832014-12-18 17:42:32 -08001606 return NULL;
1607}
Adam Langley95c29f32014-06-20 12:00:00 -07001608
1609
Adam Langleyfcf25832014-12-18 17:42:32 -08001610/* return a servername extension value if provided in Client Hello, or NULL. So
1611 * far, only host_name types are defined (RFC 3546). */
1612const char *SSL_get_servername(const SSL *s, const int type) {
1613 if (type != TLSEXT_NAMETYPE_host_name) {
1614 return NULL;
1615 }
Adam Langley95c29f32014-06-20 12:00:00 -07001616
Adam Langleyfcf25832014-12-18 17:42:32 -08001617 return s->session && !s->tlsext_hostname ? s->session->tlsext_hostname
1618 : s->tlsext_hostname;
1619}
Adam Langley95c29f32014-06-20 12:00:00 -07001620
Adam Langleyfcf25832014-12-18 17:42:32 -08001621int SSL_get_servername_type(const SSL *s) {
1622 if (s->session &&
1623 (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname)) {
1624 return TLSEXT_NAMETYPE_host_name;
1625 }
Adam Langley95c29f32014-06-20 12:00:00 -07001626
Adam Langleyfcf25832014-12-18 17:42:32 -08001627 return -1;
1628}
Adam Langley95c29f32014-06-20 12:00:00 -07001629
Adam Langleyfcf25832014-12-18 17:42:32 -08001630void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
1631 ctx->signed_cert_timestamps_enabled = 1;
1632}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001633
Adam Langleyfcf25832014-12-18 17:42:32 -08001634int SSL_enable_signed_cert_timestamps(SSL *ssl) {
1635 ssl->signed_cert_timestamps_enabled = 1;
1636 return 1;
1637}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001638
Adam Langleyfcf25832014-12-18 17:42:32 -08001639void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
1640 ctx->ocsp_stapling_enabled = 1;
1641}
David Benjamin6c7aed02014-08-27 16:42:38 -04001642
Adam Langleyfcf25832014-12-18 17:42:32 -08001643int SSL_enable_ocsp_stapling(SSL *ssl) {
1644 ssl->ocsp_stapling_enabled = 1;
1645 return 1;
1646}
David Benjamin6c7aed02014-08-27 16:42:38 -04001647
Adam Langleyfcf25832014-12-18 17:42:32 -08001648void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
1649 size_t *out_len) {
1650 SSL_SESSION *session = ssl->session;
Adam Langley3cb50e02014-08-26 14:00:31 -07001651
Adam Langleyfcf25832014-12-18 17:42:32 -08001652 *out_len = 0;
1653 *out = NULL;
1654 if (ssl->server || !session || !session->tlsext_signed_cert_timestamp_list) {
1655 return;
1656 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001657
Adam Langleyfcf25832014-12-18 17:42:32 -08001658 *out = session->tlsext_signed_cert_timestamp_list;
1659 *out_len = session->tlsext_signed_cert_timestamp_list_length;
1660}
David Benjamin6c7aed02014-08-27 16:42:38 -04001661
Adam Langleyfcf25832014-12-18 17:42:32 -08001662void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
1663 size_t *out_len) {
1664 SSL_SESSION *session = ssl->session;
1665
1666 *out_len = 0;
1667 *out = NULL;
1668 if (ssl->server || !session || !session->ocsp_response) {
1669 return;
1670 }
1671 *out = session->ocsp_response;
1672 *out_len = session->ocsp_response_length;
1673}
David Benjamin6c7aed02014-08-27 16:42:38 -04001674
Paul Lietar4fac72e2015-09-09 13:44:55 +01001675int SSL_CTX_set_signed_cert_timestamp_list(SSL_CTX *ctx, const uint8_t *list,
1676 size_t list_len) {
1677 OPENSSL_free(ctx->signed_cert_timestamp_list);
1678 ctx->signed_cert_timestamp_list_length = 0;
1679
1680 ctx->signed_cert_timestamp_list = BUF_memdup(list, list_len);
1681 if (ctx->signed_cert_timestamp_list == NULL) {
1682 return 0;
1683 }
1684 ctx->signed_cert_timestamp_list_length = list_len;
1685
1686 return 1;
1687}
1688
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001689int SSL_CTX_set_ocsp_response(SSL_CTX *ctx, const uint8_t *response,
1690 size_t response_len) {
1691 OPENSSL_free(ctx->ocsp_response);
1692 ctx->ocsp_response_length = 0;
1693
1694 ctx->ocsp_response = BUF_memdup(response, response_len);
1695 if (ctx->ocsp_response == NULL) {
1696 return 0;
1697 }
1698 ctx->ocsp_response_length = response_len;
1699
1700 return 1;
1701}
1702
Adam Langley95c29f32014-06-20 12:00:00 -07001703/* SSL_select_next_proto implements the standard protocol selection. It is
1704 * expected that this function is called from the callback set by
1705 * SSL_CTX_set_next_proto_select_cb.
1706 *
1707 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1708 * strings. The length byte itself is not included in the length. A byte
1709 * string of length 0 is invalid. No byte string may be truncated.
1710 *
1711 * The current, but experimental algorithm for selecting the protocol is:
1712 *
1713 * 1) If the server doesn't support NPN then this is indicated to the
1714 * callback. In this case, the client application has to abort the connection
1715 * or have a default application level protocol.
1716 *
1717 * 2) If the server supports NPN, but advertises an empty list then the
1718 * client selects the first protcol in its list, but indicates via the
1719 * API that this fallback case was enacted.
1720 *
1721 * 3) Otherwise, the client finds the first protocol in the server's list
1722 * that it supports and selects this protocol. This is because it's
1723 * assumed that the server has better information about which protocol
1724 * a client should use.
1725 *
1726 * 4) If the client doesn't support any of the server's advertised
1727 * protocols, then this is treated the same as case 2.
1728 *
1729 * It returns either
1730 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1731 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1732 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001733int SSL_select_next_proto(uint8_t **out, uint8_t *outlen, const uint8_t *server,
1734 unsigned int server_len, const uint8_t *client,
1735 unsigned int client_len) {
1736 unsigned int i, j;
1737 const uint8_t *result;
1738 int status = OPENSSL_NPN_UNSUPPORTED;
Adam Langley95c29f32014-06-20 12:00:00 -07001739
Adam Langleyfcf25832014-12-18 17:42:32 -08001740 /* For each protocol in server preference order, see if we support it. */
1741 for (i = 0; i < server_len;) {
1742 for (j = 0; j < client_len;) {
1743 if (server[i] == client[j] &&
1744 memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
1745 /* We found a match */
1746 result = &server[i];
1747 status = OPENSSL_NPN_NEGOTIATED;
1748 goto found;
1749 }
1750 j += client[j];
1751 j++;
1752 }
1753 i += server[i];
1754 i++;
1755 }
Adam Langley95c29f32014-06-20 12:00:00 -07001756
Adam Langleyfcf25832014-12-18 17:42:32 -08001757 /* There's no overlap between our protocols and the server's list. */
1758 result = client;
1759 status = OPENSSL_NPN_NO_OVERLAP;
Adam Langley95c29f32014-06-20 12:00:00 -07001760
Adam Langleyfcf25832014-12-18 17:42:32 -08001761found:
1762 *out = (uint8_t *)result + 1;
1763 *outlen = result[0];
1764 return status;
1765}
Adam Langley95c29f32014-06-20 12:00:00 -07001766
Adam Langley95c29f32014-06-20 12:00:00 -07001767/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
1768 * requested protocol for this connection and returns 0. If the client didn't
1769 * request any protocol, then *data is set to NULL.
1770 *
1771 * Note that the client can request any protocol it chooses. The value returned
1772 * from this function need not be a member of the list of supported protocols
Adam Langleyfcf25832014-12-18 17:42:32 -08001773 * provided by the callback. */
1774void SSL_get0_next_proto_negotiated(const SSL *s, const uint8_t **data,
1775 unsigned *len) {
1776 *data = s->next_proto_negotiated;
1777 if (!*data) {
1778 *len = 0;
1779 } else {
1780 *len = s->next_proto_negotiated_len;
1781 }
Adam Langley95c29f32014-06-20 12:00:00 -07001782}
1783
1784/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
1785 * TLS server needs a list of supported protocols for Next Protocol
1786 * Negotiation. The returned list must be in wire format. The list is returned
1787 * by setting |out| to point to it and |outlen| to its length. This memory will
1788 * not be modified, but one should assume that the SSL* keeps a reference to
1789 * it.
1790 *
Adam Langleyfcf25832014-12-18 17:42:32 -08001791 * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise.
1792 * Otherwise, no such extension will be included in the ServerHello. */
1793void SSL_CTX_set_next_protos_advertised_cb(
1794 SSL_CTX *ctx,
1795 int (*cb)(SSL *ssl, const uint8_t **out, unsigned int *outlen, void *arg),
1796 void *arg) {
1797 ctx->next_protos_advertised_cb = cb;
1798 ctx->next_protos_advertised_cb_arg = arg;
1799}
Adam Langley95c29f32014-06-20 12:00:00 -07001800
1801/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1802 * client needs to select a protocol from the server's provided list. |out|
1803 * must be set to point to the selected protocol (which may be within |in|).
1804 * The length of the protocol name must be written into |outlen|. The server's
1805 * advertised protocols are provided in |in| and |inlen|. The callback can
1806 * assume that |in| is syntactically valid.
1807 *
1808 * The client must select a protocol. It is fatal to the connection if this
1809 * callback returns a value other than SSL_TLSEXT_ERR_OK.
1810 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001811void SSL_CTX_set_next_proto_select_cb(
1812 SSL_CTX *ctx, int (*cb)(SSL *s, uint8_t **out, uint8_t *outlen,
1813 const uint8_t *in, unsigned int inlen, void *arg),
1814 void *arg) {
1815 ctx->next_proto_select_cb = cb;
1816 ctx->next_proto_select_cb_arg = arg;
1817}
Adam Langley95c29f32014-06-20 12:00:00 -07001818
Adam Langleyfcf25832014-12-18 17:42:32 -08001819int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
1820 unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001821 OPENSSL_free(ctx->alpn_client_proto_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001822 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1823 if (!ctx->alpn_client_proto_list) {
1824 return 1;
1825 }
1826 ctx->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001827
Adam Langleyfcf25832014-12-18 17:42:32 -08001828 return 0;
1829}
Adam Langley95c29f32014-06-20 12:00:00 -07001830
Adam Langleyfcf25832014-12-18 17:42:32 -08001831int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001832 OPENSSL_free(ssl->alpn_client_proto_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001833 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1834 if (!ssl->alpn_client_proto_list) {
1835 return 1;
1836 }
1837 ssl->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001838
Adam Langleyfcf25832014-12-18 17:42:32 -08001839 return 0;
1840}
Adam Langley95c29f32014-06-20 12:00:00 -07001841
1842/* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
1843 * during ClientHello processing in order to select an ALPN protocol from the
1844 * client's list of offered protocols. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001845void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
1846 int (*cb)(SSL *ssl, const uint8_t **out,
1847 uint8_t *outlen, const uint8_t *in,
1848 unsigned int inlen, void *arg),
1849 void *arg) {
1850 ctx->alpn_select_cb = cb;
1851 ctx->alpn_select_cb_arg = arg;
1852}
Adam Langley95c29f32014-06-20 12:00:00 -07001853
1854/* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
1855 * On return it sets |*data| to point to |*len| bytes of protocol name (not
1856 * including the leading length-prefix byte). If the server didn't respond with
1857 * a negotiated protocol then |*len| will be zero. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001858void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **data,
1859 unsigned *len) {
1860 *data = NULL;
1861 if (ssl->s3) {
1862 *data = ssl->s3->alpn_selected;
1863 }
1864 if (*data == NULL) {
1865 *len = 0;
1866 } else {
1867 *len = ssl->s3->alpn_selected_len;
1868 }
1869}
Adam Langley95c29f32014-06-20 12:00:00 -07001870
David Benjamincfd248b2015-04-03 11:02:24 -04001871int SSL_export_keying_material(SSL *s, uint8_t *out, size_t out_len,
1872 const char *label, size_t label_len,
1873 const uint8_t *context, size_t context_len,
1874 int use_context) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001875 if (s->version < TLS1_VERSION) {
David Benjaminc565ebb2015-04-03 04:06:36 -04001876 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001877 }
Adam Langley95c29f32014-06-20 12:00:00 -07001878
David Benjamincfd248b2015-04-03 11:02:24 -04001879 return s->enc_method->export_keying_material(
1880 s, out, out_len, label, label_len, context, context_len, use_context);
Adam Langleyfcf25832014-12-18 17:42:32 -08001881}
Adam Langley95c29f32014-06-20 12:00:00 -07001882
Adam Langleyfcf25832014-12-18 17:42:32 -08001883void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) {
1884 ctx->default_passwd_callback = cb;
1885}
Adam Langley95c29f32014-06-20 12:00:00 -07001886
Adam Langleyfcf25832014-12-18 17:42:32 -08001887void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u) {
1888 ctx->default_passwd_callback_userdata = u;
1889}
Adam Langley95c29f32014-06-20 12:00:00 -07001890
Adam Langleyfcf25832014-12-18 17:42:32 -08001891void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
1892 int (*cb)(X509_STORE_CTX *, void *),
1893 void *arg) {
1894 ctx->app_verify_callback = cb;
1895 ctx->app_verify_arg = arg;
1896}
Adam Langley95c29f32014-06-20 12:00:00 -07001897
Adam Langleyfcf25832014-12-18 17:42:32 -08001898void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
1899 int (*cb)(int, X509_STORE_CTX *)) {
1900 ctx->verify_mode = mode;
1901 ctx->default_verify_callback = cb;
1902}
Adam Langley95c29f32014-06-20 12:00:00 -07001903
Adam Langleyfcf25832014-12-18 17:42:32 -08001904void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
1905 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
1906}
Adam Langley1258b6a2014-06-20 12:00:00 -07001907
David Benjamin7481d392015-07-05 19:38:46 -04001908void SSL_CTX_set_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl, void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08001909 void *arg) {
David Benjamin7481d392015-07-05 19:38:46 -04001910 ssl_cert_set_cert_cb(ctx->cert, cb, arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08001911}
David Benjamin859ec3c2014-09-02 16:29:36 -04001912
David Benjamin7481d392015-07-05 19:38:46 -04001913void SSL_set_cert_cb(SSL *ssl, int (*cb)(SSL *ssl, void *arg), void *arg) {
1914 ssl_cert_set_cert_cb(ssl->cert, cb, arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08001915}
Adam Langley95c29f32014-06-20 12:00:00 -07001916
David Benjamin107db582015-04-08 00:41:59 -04001917void ssl_get_compatible_server_ciphers(SSL *s, uint32_t *out_mask_k,
1918 uint32_t *out_mask_a) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001919 CERT *c = s->cert;
David Benjamind1d80782015-07-05 11:54:09 -04001920 int have_rsa_cert = 0, dh_tmp;
David Benjamin107db582015-04-08 00:41:59 -04001921 uint32_t mask_k, mask_a;
David Benjamind1d80782015-07-05 11:54:09 -04001922 int have_ecc_cert = 0, ecdsa_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001923 X509 *x;
Adam Langley95c29f32014-06-20 12:00:00 -07001924
Adam Langleyfcf25832014-12-18 17:42:32 -08001925 dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001926
nagendra modadugu601448a2015-07-24 09:31:31 -07001927 if (s->cert->x509 != NULL && ssl_has_private_key(s)) {
1928 if (ssl_private_key_type(s) == EVP_PKEY_RSA) {
David Benjamind1d80782015-07-05 11:54:09 -04001929 have_rsa_cert = 1;
nagendra modadugu601448a2015-07-24 09:31:31 -07001930 } else if (ssl_private_key_type(s) == EVP_PKEY_EC) {
David Benjamind1d80782015-07-05 11:54:09 -04001931 have_ecc_cert = 1;
1932 }
1933 }
nagendra modadugu601448a2015-07-24 09:31:31 -07001934
Adam Langleyfcf25832014-12-18 17:42:32 -08001935 mask_k = 0;
1936 mask_a = 0;
David Benjaminf31e6812014-11-13 18:05:55 -05001937
Adam Langleyfcf25832014-12-18 17:42:32 -08001938 if (dh_tmp) {
David Benjamin7061e282015-03-19 11:10:48 -04001939 mask_k |= SSL_kDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001940 }
David Benjaminbb20f522015-07-04 17:18:14 -04001941 if (have_rsa_cert) {
1942 mask_k |= SSL_kRSA;
Adam Langleyfcf25832014-12-18 17:42:32 -08001943 mask_a |= SSL_aRSA;
1944 }
Adam Langley95c29f32014-06-20 12:00:00 -07001945
Adam Langleyfcf25832014-12-18 17:42:32 -08001946 /* An ECC certificate may be usable for ECDSA cipher suites depending on the
1947 * key usage extension and on the client's curve preferences. */
1948 if (have_ecc_cert) {
David Benjamind1d80782015-07-05 11:54:09 -04001949 x = c->x509;
Adam Langleyfcf25832014-12-18 17:42:32 -08001950 /* This call populates extension flags (ex_flags). */
1951 X509_check_purpose(x, -1, 0);
1952 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE)
1953 ? (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)
1954 : 1;
1955 if (!tls1_check_ec_cert(s, x)) {
1956 ecdsa_ok = 0;
1957 }
1958 if (ecdsa_ok) {
1959 mask_a |= SSL_aECDSA;
1960 }
1961 }
Adam Langley95c29f32014-06-20 12:00:00 -07001962
Adam Langleyfcf25832014-12-18 17:42:32 -08001963 /* If we are considering an ECC cipher suite that uses an ephemeral EC
1964 * key, check it. */
David Benjamindd978782015-04-24 15:20:13 -04001965 if (tls1_check_ec_tmp_key(s)) {
David Benjamin7061e282015-03-19 11:10:48 -04001966 mask_k |= SSL_kECDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001967 }
Adam Langley95c29f32014-06-20 12:00:00 -07001968
Adam Langleyfcf25832014-12-18 17:42:32 -08001969 /* PSK requires a server callback. */
1970 if (s->psk_server_callback != NULL) {
1971 mask_k |= SSL_kPSK;
1972 mask_a |= SSL_aPSK;
1973 }
Adam Langley95c29f32014-06-20 12:00:00 -07001974
Adam Langleyfcf25832014-12-18 17:42:32 -08001975 *out_mask_k = mask_k;
1976 *out_mask_a = mask_a;
1977}
Adam Langley95c29f32014-06-20 12:00:00 -07001978
Adam Langleyfcf25832014-12-18 17:42:32 -08001979void ssl_update_cache(SSL *s, int mode) {
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001980 /* Never cache sessions with empty session IDs. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001981 if (s->session->session_id_length == 0) {
1982 return;
1983 }
Adam Langley95c29f32014-06-20 12:00:00 -07001984
David Benjamin95d31822015-06-15 19:53:32 -04001985 int has_new_session = !s->hit;
1986 if (!s->server && s->tlsext_ticket_expected) {
1987 /* A client may see new sessions on abbreviated handshakes if the server
1988 * decides to renew the ticket. Once the handshake is completed, it should
1989 * be inserted into the cache. */
1990 has_new_session = 1;
1991 }
1992
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001993 SSL_CTX *ctx = s->initial_ctx;
David Benjamin95d31822015-06-15 19:53:32 -04001994 if ((ctx->session_cache_mode & mode) == mode && has_new_session &&
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001995 ((ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) ||
1996 SSL_CTX_add_session(ctx, s->session)) &&
1997 ctx->new_session_cb != NULL) {
1998 /* Note: |new_session_cb| is called whether the internal session cache is
1999 * used or not. */
2000 if (!ctx->new_session_cb(s, SSL_SESSION_up_ref(s->session))) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002001 SSL_SESSION_free(s->session);
2002 }
2003 }
Adam Langley95c29f32014-06-20 12:00:00 -07002004
David Benjaminb6d0c6d2015-03-19 19:07:26 -04002005 if (!(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR) &&
2006 !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) &&
2007 (ctx->session_cache_mode & mode) == mode) {
2008 /* Automatically flush the internal session cache every 255 connections. */
2009 int flush_cache = 0;
Adam Langley4bdb6e42015-05-15 15:29:21 -07002010 CRYPTO_MUTEX_lock_write(&ctx->lock);
David Benjaminb6d0c6d2015-03-19 19:07:26 -04002011 ctx->handshakes_since_cache_flush++;
2012 if (ctx->handshakes_since_cache_flush >= 255) {
2013 flush_cache = 1;
2014 ctx->handshakes_since_cache_flush = 0;
2015 }
Adam Langley4bdb6e42015-05-15 15:29:21 -07002016 CRYPTO_MUTEX_unlock(&ctx->lock);
David Benjaminb6d0c6d2015-03-19 19:07:26 -04002017
2018 if (flush_cache) {
2019 SSL_CTX_flush_sessions(ctx, (unsigned long)time(NULL));
Adam Langleyfcf25832014-12-18 17:42:32 -08002020 }
2021 }
2022}
Adam Langley95c29f32014-06-20 12:00:00 -07002023
Adam Langleyfcf25832014-12-18 17:42:32 -08002024static const char *ssl_get_version(int version) {
2025 switch (version) {
2026 case TLS1_2_VERSION:
2027 return "TLSv1.2";
Adam Langley95c29f32014-06-20 12:00:00 -07002028
Adam Langleyfcf25832014-12-18 17:42:32 -08002029 case TLS1_1_VERSION:
2030 return "TLSv1.1";
Adam Langley95c29f32014-06-20 12:00:00 -07002031
Adam Langleyfcf25832014-12-18 17:42:32 -08002032 case TLS1_VERSION:
2033 return "TLSv1";
Adam Langley95c29f32014-06-20 12:00:00 -07002034
Adam Langleyfcf25832014-12-18 17:42:32 -08002035 case SSL3_VERSION:
2036 return "SSLv3";
Adam Langley95c29f32014-06-20 12:00:00 -07002037
David Benjamin1c722b72015-04-20 13:53:10 -04002038 case DTLS1_VERSION:
2039 return "DTLSv1";
2040
2041 case DTLS1_2_VERSION:
2042 return "DTLSv1.2";
2043
Adam Langleyfcf25832014-12-18 17:42:32 -08002044 default:
2045 return "unknown";
2046 }
2047}
Adam Langley95c29f32014-06-20 12:00:00 -07002048
Adam Langleyfcf25832014-12-18 17:42:32 -08002049const char *SSL_get_version(const SSL *s) {
2050 return ssl_get_version(s->version);
2051}
Adam Langley95c29f32014-06-20 12:00:00 -07002052
David Benjamina6b8cdf2015-09-13 14:07:33 -04002053const char *SSL_SESSION_get_version(const SSL_SESSION *session) {
2054 return ssl_get_version(session->ssl_version);
Adam Langleyfcf25832014-12-18 17:42:32 -08002055}
Adam Langley95c29f32014-06-20 12:00:00 -07002056
Sigbjorn Vik2b23d242015-06-29 15:07:26 +02002057const char* SSL_get_curve_name(uint16_t curve_id) {
2058 return tls1_ec_curve_id2name(curve_id);
2059}
2060
Adam Langleyfcf25832014-12-18 17:42:32 -08002061void ssl_clear_cipher_ctx(SSL *s) {
David Benjamin31a07792015-03-03 14:20:26 -05002062 SSL_AEAD_CTX_free(s->aead_read_ctx);
2063 s->aead_read_ctx = NULL;
2064 SSL_AEAD_CTX_free(s->aead_write_ctx);
2065 s->aead_write_ctx = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002066}
Adam Langley95c29f32014-06-20 12:00:00 -07002067
Adam Langleyfcf25832014-12-18 17:42:32 -08002068X509 *SSL_get_certificate(const SSL *s) {
2069 if (s->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04002070 return s->cert->x509;
Adam Langleyfcf25832014-12-18 17:42:32 -08002071 }
2072
2073 return NULL;
2074}
2075
2076EVP_PKEY *SSL_get_privatekey(const SSL *s) {
2077 if (s->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04002078 return s->cert->privatekey;
Adam Langleyfcf25832014-12-18 17:42:32 -08002079 }
2080
2081 return NULL;
2082}
2083
2084X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
2085 if (ctx->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04002086 return ctx->cert->x509;
Adam Langleyfcf25832014-12-18 17:42:32 -08002087 }
2088
2089 return NULL;
2090}
2091
2092EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
2093 if (ctx->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04002094 return ctx->cert->privatekey;
Adam Langleyfcf25832014-12-18 17:42:32 -08002095 }
2096
2097 return NULL;
2098}
2099
2100const SSL_CIPHER *SSL_get_current_cipher(const SSL *s) {
David Benjamina07c0fc2015-05-13 13:19:42 -04002101 if (s->aead_write_ctx == NULL) {
2102 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002103 }
David Benjamina07c0fc2015-05-13 13:19:42 -04002104 return s->aead_write_ctx->cipher;
Adam Langleyfcf25832014-12-18 17:42:32 -08002105}
2106
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07002107const COMP_METHOD *SSL_get_current_compression(SSL *s) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002108
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07002109const COMP_METHOD *SSL_get_current_expansion(SSL *s) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002110
2111int ssl_init_wbio_buffer(SSL *s, int push) {
2112 BIO *bbio;
2113
2114 if (s->bbio == NULL) {
2115 bbio = BIO_new(BIO_f_buffer());
2116 if (bbio == NULL) {
2117 return 0;
2118 }
2119 s->bbio = bbio;
2120 } else {
2121 bbio = s->bbio;
2122 if (s->bbio == s->wbio) {
2123 s->wbio = BIO_pop(s->wbio);
2124 }
2125 }
2126
2127 BIO_reset(bbio);
2128 if (!BIO_set_read_buffer_size(bbio, 1)) {
David Benjamin3570d732015-06-29 00:28:17 -04002129 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -08002130 return 0;
2131 }
2132
2133 if (push) {
2134 if (s->wbio != bbio) {
2135 s->wbio = BIO_push(bbio, s->wbio);
2136 }
2137 } else {
2138 if (s->wbio == bbio) {
2139 s->wbio = BIO_pop(bbio);
2140 }
2141 }
2142
2143 return 1;
2144}
2145
2146void ssl_free_wbio_buffer(SSL *s) {
2147 if (s->bbio == NULL) {
2148 return;
2149 }
2150
2151 if (s->bbio == s->wbio) {
2152 /* remove buffering */
2153 s->wbio = BIO_pop(s->wbio);
2154 }
2155
2156 BIO_free(s->bbio);
2157 s->bbio = NULL;
2158}
2159
2160void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
2161 ctx->quiet_shutdown = mode;
2162}
2163
2164int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
2165 return ctx->quiet_shutdown;
2166}
2167
2168void SSL_set_quiet_shutdown(SSL *s, int mode) { s->quiet_shutdown = mode; }
2169
2170int SSL_get_quiet_shutdown(const SSL *s) { return s->quiet_shutdown; }
2171
2172void SSL_set_shutdown(SSL *s, int mode) { s->shutdown = mode; }
2173
2174int SSL_get_shutdown(const SSL *s) { return s->shutdown; }
2175
2176int SSL_version(const SSL *s) { return s->version; }
2177
2178SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
2179
2180SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
2181 if (ssl->ctx == ctx) {
2182 return ssl->ctx;
2183 }
2184
2185 if (ctx == NULL) {
2186 ctx = ssl->initial_ctx;
2187 }
2188
David Benjamin2755a3e2015-04-22 16:17:58 -04002189 ssl_cert_free(ssl->cert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002190 ssl->cert = ssl_cert_dup(ctx->cert);
David Benjamin2755a3e2015-04-22 16:17:58 -04002191
Adam Langley0b5e3902015-05-15 13:08:38 -07002192 CRYPTO_refcount_inc(&ctx->references);
David Benjamin2755a3e2015-04-22 16:17:58 -04002193 SSL_CTX_free(ssl->ctx); /* decrement reference count */
Adam Langleyfcf25832014-12-18 17:42:32 -08002194 ssl->ctx = ctx;
2195
2196 ssl->sid_ctx_length = ctx->sid_ctx_length;
2197 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2198 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2199
2200 return ssl->ctx;
2201}
2202
2203int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
2204 return X509_STORE_set_default_paths(ctx->cert_store);
2205}
Adam Langley95c29f32014-06-20 12:00:00 -07002206
2207int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
Adam Langleyfcf25832014-12-18 17:42:32 -08002208 const char *CApath) {
2209 return X509_STORE_load_locations(ctx->cert_store, CAfile, CApath);
2210}
Adam Langley95c29f32014-06-20 12:00:00 -07002211
2212void SSL_set_info_callback(SSL *ssl,
Adam Langleyfcf25832014-12-18 17:42:32 -08002213 void (*cb)(const SSL *ssl, int type, int val)) {
2214 ssl->info_callback = cb;
2215}
Adam Langley95c29f32014-06-20 12:00:00 -07002216
Adam Langleyfcf25832014-12-18 17:42:32 -08002217void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/, int /*type*/,
2218 int /*val*/) {
2219 return ssl->info_callback;
2220}
Adam Langley95c29f32014-06-20 12:00:00 -07002221
Adam Langleyfcf25832014-12-18 17:42:32 -08002222int SSL_state(const SSL *ssl) { return ssl->state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002223
David Benjaminece089c2015-05-15 23:52:42 -04002224void SSL_set_state(SSL *ssl, int state) { }
Adam Langley95c29f32014-06-20 12:00:00 -07002225
Adam Langleyfcf25832014-12-18 17:42:32 -08002226void SSL_set_verify_result(SSL *ssl, long arg) { ssl->verify_result = arg; }
Adam Langley95c29f32014-06-20 12:00:00 -07002227
Adam Langleyfcf25832014-12-18 17:42:32 -08002228long SSL_get_verify_result(const SSL *ssl) { return ssl->verify_result; }
Adam Langley95c29f32014-06-20 12:00:00 -07002229
Adam Langleyfcf25832014-12-18 17:42:32 -08002230int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2231 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002232 int index;
2233 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp,
2234 new_func, dup_func, free_func)) {
2235 return -1;
2236 }
2237 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002238}
Adam Langley95c29f32014-06-20 12:00:00 -07002239
Adam Langleyfcf25832014-12-18 17:42:32 -08002240int SSL_set_ex_data(SSL *s, int idx, void *arg) {
2241 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2242}
Adam Langley95c29f32014-06-20 12:00:00 -07002243
Adam Langleyfcf25832014-12-18 17:42:32 -08002244void *SSL_get_ex_data(const SSL *s, int idx) {
2245 return CRYPTO_get_ex_data(&s->ex_data, idx);
2246}
Adam Langley95c29f32014-06-20 12:00:00 -07002247
Adam Langleyfcf25832014-12-18 17:42:32 -08002248int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2249 CRYPTO_EX_dup *dup_func,
2250 CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002251 int index;
2252 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp,
2253 new_func, dup_func, free_func)) {
2254 return -1;
2255 }
2256 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002257}
Adam Langley95c29f32014-06-20 12:00:00 -07002258
Adam Langleyfcf25832014-12-18 17:42:32 -08002259int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg) {
2260 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2261}
Adam Langley95c29f32014-06-20 12:00:00 -07002262
Adam Langleyfcf25832014-12-18 17:42:32 -08002263void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx) {
2264 return CRYPTO_get_ex_data(&s->ex_data, idx);
2265}
Adam Langley95c29f32014-06-20 12:00:00 -07002266
Adam Langleyfcf25832014-12-18 17:42:32 -08002267X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
2268 return ctx->cert_store;
2269}
Adam Langley95c29f32014-06-20 12:00:00 -07002270
Adam Langleyfcf25832014-12-18 17:42:32 -08002271void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
David Benjamin2755a3e2015-04-22 16:17:58 -04002272 X509_STORE_free(ctx->cert_store);
Adam Langleyfcf25832014-12-18 17:42:32 -08002273 ctx->cert_store = store;
2274}
Adam Langley95c29f32014-06-20 12:00:00 -07002275
Adam Langleyfcf25832014-12-18 17:42:32 -08002276int SSL_want(const SSL *s) { return s->rwstate; }
Adam Langley95c29f32014-06-20 12:00:00 -07002277
Adam Langleyfcf25832014-12-18 17:42:32 -08002278void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
2279 RSA *(*cb)(SSL *ssl, int is_export,
2280 int keylength)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002281}
Adam Langley95c29f32014-06-20 12:00:00 -07002282
Adam Langleyfcf25832014-12-18 17:42:32 -08002283void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
2284 int keylength)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002285}
2286
2287void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
David Benjamin59015c32015-04-26 13:13:55 -04002288 DH *(*callback)(SSL *ssl, int is_export,
2289 int keylength)) {
2290 ctx->cert->dh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002291}
2292
David Benjamin59015c32015-04-26 13:13:55 -04002293void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*callback)(SSL *ssl, int is_export,
2294 int keylength)) {
2295 ssl->cert->dh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002296}
2297
2298void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,
David Benjamindd978782015-04-24 15:20:13 -04002299 EC_KEY *(*callback)(SSL *ssl, int is_export,
2300 int keylength)) {
David Benjamin59015c32015-04-26 13:13:55 -04002301 ctx->cert->ecdh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002302}
2303
2304void SSL_set_tmp_ecdh_callback(SSL *ssl,
David Benjamindd978782015-04-24 15:20:13 -04002305 EC_KEY *(*callback)(SSL *ssl, int is_export,
2306 int keylength)) {
David Benjamin59015c32015-04-26 13:13:55 -04002307 ssl->cert->ecdh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002308}
2309
2310int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
2311 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
David Benjamin3570d732015-06-29 00:28:17 -04002312 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -08002313 return 0;
2314 }
2315
David Benjamin2755a3e2015-04-22 16:17:58 -04002316 OPENSSL_free(ctx->psk_identity_hint);
Adam Langleyfcf25832014-12-18 17:42:32 -08002317
2318 if (identity_hint != NULL) {
2319 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2320 if (ctx->psk_identity_hint == NULL) {
2321 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002322 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002323 } else {
2324 ctx->psk_identity_hint = NULL;
2325 }
Adam Langley95c29f32014-06-20 12:00:00 -07002326
Adam Langleyfcf25832014-12-18 17:42:32 -08002327 return 1;
2328}
2329
2330int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint) {
2331 if (s == NULL) {
2332 return 0;
2333 }
2334
2335 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
David Benjamin3570d732015-06-29 00:28:17 -04002336 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -08002337 return 0;
2338 }
2339
2340 /* Clear currently configured hint, if any. */
David Benjamin2755a3e2015-04-22 16:17:58 -04002341 OPENSSL_free(s->psk_identity_hint);
2342 s->psk_identity_hint = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002343
2344 if (identity_hint != NULL) {
2345 s->psk_identity_hint = BUF_strdup(identity_hint);
2346 if (s->psk_identity_hint == NULL) {
2347 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002348 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002349 }
Adam Langley95c29f32014-06-20 12:00:00 -07002350
Adam Langleyfcf25832014-12-18 17:42:32 -08002351 return 1;
2352}
Adam Langley95c29f32014-06-20 12:00:00 -07002353
Adam Langleyfcf25832014-12-18 17:42:32 -08002354const char *SSL_get_psk_identity_hint(const SSL *s) {
2355 if (s == NULL) {
2356 return NULL;
2357 }
2358 return s->psk_identity_hint;
2359}
Adam Langley95c29f32014-06-20 12:00:00 -07002360
Adam Langleyfcf25832014-12-18 17:42:32 -08002361const char *SSL_get_psk_identity(const SSL *s) {
2362 if (s == NULL || s->session == NULL) {
2363 return NULL;
2364 }
Adam Langley95c29f32014-06-20 12:00:00 -07002365
Adam Langleyfcf25832014-12-18 17:42:32 -08002366 return s->session->psk_identity;
2367}
Adam Langley95c29f32014-06-20 12:00:00 -07002368
Adam Langleyfcf25832014-12-18 17:42:32 -08002369void SSL_set_psk_client_callback(
2370 SSL *s, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2371 unsigned int max_identity_len, uint8_t *psk,
2372 unsigned int max_psk_len)) {
2373 s->psk_client_callback = cb;
2374}
Adam Langley95c29f32014-06-20 12:00:00 -07002375
Adam Langleyfcf25832014-12-18 17:42:32 -08002376void SSL_CTX_set_psk_client_callback(
2377 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2378 unsigned int max_identity_len,
2379 uint8_t *psk, unsigned int max_psk_len)) {
2380 ctx->psk_client_callback = cb;
2381}
Adam Langley95c29f32014-06-20 12:00:00 -07002382
Adam Langleyfcf25832014-12-18 17:42:32 -08002383void SSL_set_psk_server_callback(
2384 SSL *s, unsigned int (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
2385 unsigned int max_psk_len)) {
2386 s->psk_server_callback = cb;
2387}
Adam Langley95c29f32014-06-20 12:00:00 -07002388
Adam Langleyfcf25832014-12-18 17:42:32 -08002389void SSL_CTX_set_psk_server_callback(
2390 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *identity,
2391 uint8_t *psk, unsigned int max_psk_len)) {
2392 ctx->psk_server_callback = cb;
2393}
Adam Langley95c29f32014-06-20 12:00:00 -07002394
Adam Langleyfcf25832014-12-18 17:42:32 -08002395void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
2396 void (*cb)(int write_p, int version,
2397 int content_type, const void *buf,
2398 size_t len, SSL *ssl, void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002399 ctx->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002400}
David Benjamin61ecccf2015-05-05 09:44:51 -04002401
2402void SSL_CTX_set_msg_callback_arg(SSL_CTX *ctx, void *arg) {
2403 ctx->msg_callback_arg = arg;
2404}
2405
Adam Langleyfcf25832014-12-18 17:42:32 -08002406void SSL_set_msg_callback(SSL *ssl,
2407 void (*cb)(int write_p, int version, int content_type,
2408 const void *buf, size_t len, SSL *ssl,
2409 void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002410 ssl->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002411}
Adam Langley95c29f32014-06-20 12:00:00 -07002412
David Benjamin61ecccf2015-05-05 09:44:51 -04002413void SSL_set_msg_callback_arg(SSL *ssl, void *arg) {
2414 ssl->msg_callback_arg = arg;
2415}
2416
Adam Langleyfcf25832014-12-18 17:42:32 -08002417void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio) {
David Benjamin2755a3e2015-04-22 16:17:58 -04002418 BIO_free(ctx->keylog_bio);
Adam Langleyfcf25832014-12-18 17:42:32 -08002419 ctx->keylog_bio = keylog_bio;
2420}
Adam Langley95c29f32014-06-20 12:00:00 -07002421
Adam Langleyfcf25832014-12-18 17:42:32 -08002422static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
2423 static const char hextable[] = "0123456789abcdef";
2424 uint8_t *out;
2425 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002426
Adam Langleyfcf25832014-12-18 17:42:32 -08002427 if (!CBB_add_space(cbb, &out, in_len * 2)) {
2428 return 0;
2429 }
Adam Langley95c29f32014-06-20 12:00:00 -07002430
Adam Langleyfcf25832014-12-18 17:42:32 -08002431 for (i = 0; i < in_len; i++) {
2432 *(out++) = (uint8_t)hextable[in[i] >> 4];
2433 *(out++) = (uint8_t)hextable[in[i] & 0xf];
2434 }
Adam Langley95c29f32014-06-20 12:00:00 -07002435
Adam Langleyfcf25832014-12-18 17:42:32 -08002436 return 1;
2437}
David Benjamin859ec3c2014-09-02 16:29:36 -04002438
2439int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -08002440 const uint8_t *encrypted_premaster,
2441 size_t encrypted_premaster_len,
2442 const uint8_t *premaster,
2443 size_t premaster_len) {
2444 BIO *bio = ctx->keylog_bio;
2445 CBB cbb;
2446 uint8_t *out;
2447 size_t out_len;
2448 int ret;
David Benjamin859ec3c2014-09-02 16:29:36 -04002449
Adam Langleyfcf25832014-12-18 17:42:32 -08002450 if (bio == NULL) {
2451 return 1;
2452 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002453
Adam Langleyfcf25832014-12-18 17:42:32 -08002454 if (encrypted_premaster_len < 8) {
David Benjamin3570d732015-06-29 00:28:17 -04002455 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08002456 return 0;
2457 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002458
David Benjamina8653202015-06-28 01:26:10 -04002459 CBB_zero(&cbb);
2460 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len * 2 + 1) ||
2461 !CBB_add_bytes(&cbb, (const uint8_t *)"RSA ", 4) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08002462 /* Only the first 8 bytes of the encrypted premaster secret are
2463 * logged. */
2464 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
2465 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2466 !cbb_add_hex(&cbb, premaster, premaster_len) ||
2467 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2468 !CBB_finish(&cbb, &out, &out_len)) {
2469 CBB_cleanup(&cbb);
2470 return 0;
2471 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002472
Adam Langley4bdb6e42015-05-15 15:29:21 -07002473 CRYPTO_MUTEX_lock_write(&ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -08002474 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
Adam Langley4bdb6e42015-05-15 15:29:21 -07002475 CRYPTO_MUTEX_unlock(&ctx->lock);
David Benjamin859ec3c2014-09-02 16:29:36 -04002476
Adam Langleyfcf25832014-12-18 17:42:32 -08002477 OPENSSL_free(out);
2478 return ret;
Adam Langley95f22882014-06-20 12:00:00 -07002479}
2480
Adam Langleyfcf25832014-12-18 17:42:32 -08002481int ssl_ctx_log_master_secret(SSL_CTX *ctx, const uint8_t *client_random,
2482 size_t client_random_len, const uint8_t *master,
2483 size_t master_len) {
2484 BIO *bio = ctx->keylog_bio;
2485 CBB cbb;
2486 uint8_t *out;
2487 size_t out_len;
2488 int ret;
Adam Langleyadb739e2014-06-20 12:00:00 -07002489
Adam Langleyfcf25832014-12-18 17:42:32 -08002490 if (bio == NULL) {
2491 return 1;
2492 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002493
Adam Langleyfcf25832014-12-18 17:42:32 -08002494 if (client_random_len != 32) {
David Benjamin3570d732015-06-29 00:28:17 -04002495 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08002496 return 0;
2497 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002498
David Benjamina8653202015-06-28 01:26:10 -04002499 CBB_zero(&cbb);
2500 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len * 2 + 1) ||
2501 !CBB_add_bytes(&cbb, (const uint8_t *)"CLIENT_RANDOM ", 14) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08002502 !cbb_add_hex(&cbb, client_random, 32) ||
2503 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2504 !cbb_add_hex(&cbb, master, master_len) ||
2505 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2506 !CBB_finish(&cbb, &out, &out_len)) {
2507 CBB_cleanup(&cbb);
2508 return 0;
2509 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002510
Adam Langley4bdb6e42015-05-15 15:29:21 -07002511 CRYPTO_MUTEX_lock_write(&ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -08002512 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
Adam Langley4bdb6e42015-05-15 15:29:21 -07002513 CRYPTO_MUTEX_unlock(&ctx->lock);
David Benjamine99e9122014-12-11 01:46:01 -05002514
Adam Langleyfcf25832014-12-18 17:42:32 -08002515 OPENSSL_free(out);
2516 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -07002517}
2518
David Benjamined7c4752015-02-16 19:16:46 -05002519int SSL_in_false_start(const SSL *s) {
2520 return s->s3->tmp.in_false_start;
2521}
2522
Adam Langleyfcf25832014-12-18 17:42:32 -08002523int SSL_cutthrough_complete(const SSL *s) {
David Benjamined7c4752015-02-16 19:16:46 -05002524 return SSL_in_false_start(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002525}
Adam Langley95c29f32014-06-20 12:00:00 -07002526
Adam Langleyfcf25832014-12-18 17:42:32 -08002527void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
2528 size_t *ssl_session_size) {
2529 *ssl_size = sizeof(SSL);
2530 *ssl_ctx_size = sizeof(SSL_CTX);
2531 *ssl_session_size = sizeof(SSL_SESSION);
2532}
Feng Lu41aa3252014-11-21 22:47:56 -08002533
David Benjamined7c4752015-02-16 19:16:46 -05002534int ssl3_can_false_start(const SSL *s) {
David Benjamin195dc782015-02-19 13:27:05 -05002535 const SSL_CIPHER *const cipher = SSL_get_current_cipher(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002536
Adam Langleye631d962015-04-01 13:11:01 -07002537 /* False Start only for TLS 1.2 with an ECDHE+AEAD cipher and ALPN or NPN. */
David Benjamin195dc782015-02-19 13:27:05 -05002538 return !SSL_IS_DTLS(s) &&
2539 SSL_version(s) >= TLS1_2_VERSION &&
2540 (s->s3->alpn_selected || s->s3->next_proto_neg_seen) &&
2541 cipher != NULL &&
Adam Langleye631d962015-04-01 13:11:01 -07002542 cipher->algorithm_mkey == SSL_kECDHE &&
David Benjamin195dc782015-02-19 13:27:05 -05002543 (cipher->algorithm_enc == SSL_AES128GCM ||
2544 cipher->algorithm_enc == SSL_AES256GCM ||
2545 cipher->algorithm_enc == SSL_CHACHA20POLY1305);
Adam Langleyfcf25832014-12-18 17:42:32 -08002546}
2547
2548const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version) {
2549 switch (version) {
2550 case SSL3_VERSION:
2551 return &SSLv3_enc_data;
2552
2553 case TLS1_VERSION:
2554 return &TLSv1_enc_data;
2555
David Benjamin9e13e1a2015-03-05 01:56:32 -05002556 case DTLS1_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002557 case TLS1_1_VERSION:
2558 return &TLSv1_1_enc_data;
2559
David Benjamin9e13e1a2015-03-05 01:56:32 -05002560 case DTLS1_2_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002561 case TLS1_2_VERSION:
2562 return &TLSv1_2_enc_data;
2563
Adam Langleyfcf25832014-12-18 17:42:32 -08002564 default:
2565 return NULL;
2566 }
2567}
2568
2569uint16_t ssl3_get_max_server_version(const SSL *s) {
2570 uint16_t max_version;
2571
2572 if (SSL_IS_DTLS(s)) {
2573 max_version = (s->max_version != 0) ? s->max_version : DTLS1_2_VERSION;
2574 if (!(s->options & SSL_OP_NO_DTLSv1_2) && DTLS1_2_VERSION >= max_version) {
2575 return DTLS1_2_VERSION;
2576 }
2577 if (!(s->options & SSL_OP_NO_DTLSv1) && DTLS1_VERSION >= max_version) {
2578 return DTLS1_VERSION;
2579 }
2580 return 0;
2581 }
2582
2583 max_version = (s->max_version != 0) ? s->max_version : TLS1_2_VERSION;
2584 if (!(s->options & SSL_OP_NO_TLSv1_2) && TLS1_2_VERSION <= max_version) {
2585 return TLS1_2_VERSION;
2586 }
2587 if (!(s->options & SSL_OP_NO_TLSv1_1) && TLS1_1_VERSION <= max_version) {
2588 return TLS1_1_VERSION;
2589 }
2590 if (!(s->options & SSL_OP_NO_TLSv1) && TLS1_VERSION <= max_version) {
2591 return TLS1_VERSION;
2592 }
2593 if (!(s->options & SSL_OP_NO_SSLv3) && SSL3_VERSION <= max_version) {
2594 return SSL3_VERSION;
2595 }
2596 return 0;
2597}
2598
2599uint16_t ssl3_get_mutual_version(SSL *s, uint16_t client_version) {
2600 uint16_t version = 0;
2601
2602 if (SSL_IS_DTLS(s)) {
2603 /* Clamp client_version to max_version. */
2604 if (s->max_version != 0 && client_version < s->max_version) {
2605 client_version = s->max_version;
2606 }
2607
2608 if (client_version <= DTLS1_2_VERSION && !(s->options & SSL_OP_NO_DTLSv1_2)) {
2609 version = DTLS1_2_VERSION;
2610 } else if (client_version <= DTLS1_VERSION &&
David Benjaminb18f0242015-03-10 18:30:08 -04002611 !(s->options & SSL_OP_NO_DTLSv1)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002612 version = DTLS1_VERSION;
2613 }
2614
2615 /* Check against min_version. */
2616 if (version != 0 && s->min_version != 0 && version > s->min_version) {
2617 return 0;
2618 }
2619 return version;
2620 } else {
2621 /* Clamp client_version to max_version. */
2622 if (s->max_version != 0 && client_version > s->max_version) {
2623 client_version = s->max_version;
2624 }
2625
2626 if (client_version >= TLS1_2_VERSION && !(s->options & SSL_OP_NO_TLSv1_2)) {
2627 version = TLS1_2_VERSION;
2628 } else if (client_version >= TLS1_1_VERSION &&
2629 !(s->options & SSL_OP_NO_TLSv1_1)) {
2630 version = TLS1_1_VERSION;
2631 } else if (client_version >= TLS1_VERSION && !(s->options & SSL_OP_NO_TLSv1)) {
2632 version = TLS1_VERSION;
2633 } else if (client_version >= SSL3_VERSION && !(s->options & SSL_OP_NO_SSLv3)) {
2634 version = SSL3_VERSION;
2635 }
2636
2637 /* Check against min_version. */
2638 if (version != 0 && s->min_version != 0 && version < s->min_version) {
2639 return 0;
2640 }
2641 return version;
2642 }
2643}
2644
2645uint16_t ssl3_get_max_client_version(SSL *s) {
David Benjamin123a8fd2015-04-26 14:16:41 -04002646 uint32_t options = s->options;
Adam Langleyfcf25832014-12-18 17:42:32 -08002647 uint16_t version = 0;
2648
2649 /* OpenSSL's API for controlling versions entails blacklisting individual
2650 * protocols. This has two problems. First, on the client, the protocol can
2651 * only express a contiguous range of versions. Second, a library consumer
2652 * trying to set a maximum version cannot disable protocol versions that get
2653 * added in a future version of the library.
2654 *
2655 * To account for both of these, OpenSSL interprets the client-side bitmask
2656 * as a min/max range by picking the lowest contiguous non-empty range of
2657 * enabled protocols. Note that this means it is impossible to set a maximum
2658 * version of TLS 1.2 in a future-proof way.
2659 *
2660 * By this scheme, the maximum version is the lowest version V such that V is
2661 * enabled and V+1 is disabled or unimplemented. */
2662 if (SSL_IS_DTLS(s)) {
2663 if (!(options & SSL_OP_NO_DTLSv1_2)) {
2664 version = DTLS1_2_VERSION;
2665 }
2666 if (!(options & SSL_OP_NO_DTLSv1) && (options & SSL_OP_NO_DTLSv1_2)) {
2667 version = DTLS1_VERSION;
2668 }
2669 if (s->max_version != 0 && version < s->max_version) {
2670 version = s->max_version;
2671 }
2672 } else {
2673 if (!(options & SSL_OP_NO_TLSv1_2)) {
2674 version = TLS1_2_VERSION;
2675 }
2676 if (!(options & SSL_OP_NO_TLSv1_1) && (options & SSL_OP_NO_TLSv1_2)) {
2677 version = TLS1_1_VERSION;
2678 }
2679 if (!(options & SSL_OP_NO_TLSv1) && (options & SSL_OP_NO_TLSv1_1)) {
2680 version = TLS1_VERSION;
2681 }
2682 if (!(options & SSL_OP_NO_SSLv3) && (options & SSL_OP_NO_TLSv1)) {
2683 version = SSL3_VERSION;
2684 }
2685 if (s->max_version != 0 && version > s->max_version) {
2686 version = s->max_version;
2687 }
2688 }
2689
2690 return version;
2691}
2692
2693int ssl3_is_version_enabled(SSL *s, uint16_t version) {
2694 if (SSL_IS_DTLS(s)) {
2695 if (s->max_version != 0 && version < s->max_version) {
2696 return 0;
2697 }
2698 if (s->min_version != 0 && version > s->min_version) {
2699 return 0;
2700 }
2701
2702 switch (version) {
2703 case DTLS1_VERSION:
2704 return !(s->options & SSL_OP_NO_DTLSv1);
2705
2706 case DTLS1_2_VERSION:
2707 return !(s->options & SSL_OP_NO_DTLSv1_2);
2708
2709 default:
2710 return 0;
2711 }
2712 } else {
2713 if (s->max_version != 0 && version > s->max_version) {
2714 return 0;
2715 }
2716 if (s->min_version != 0 && version < s->min_version) {
2717 return 0;
2718 }
2719
2720 switch (version) {
2721 case SSL3_VERSION:
2722 return !(s->options & SSL_OP_NO_SSLv3);
2723
2724 case TLS1_VERSION:
2725 return !(s->options & SSL_OP_NO_TLSv1);
2726
2727 case TLS1_1_VERSION:
2728 return !(s->options & SSL_OP_NO_TLSv1_1);
2729
2730 case TLS1_2_VERSION:
2731 return !(s->options & SSL_OP_NO_TLSv1_2);
2732
2733 default:
2734 return 0;
2735 }
2736 }
2737}
2738
David Benjaminea72bd02014-12-21 21:27:41 -05002739uint16_t ssl3_version_from_wire(SSL *s, uint16_t wire_version) {
2740 if (!SSL_IS_DTLS(s)) {
2741 return wire_version;
2742 }
2743
2744 uint16_t tls_version = ~wire_version;
2745 uint16_t version = tls_version + 0x0201;
2746 /* If either component overflowed, clamp it so comparisons still work. */
2747 if ((version >> 8) < (tls_version >> 8)) {
2748 version = 0xff00 | (version & 0xff);
2749 }
2750 if ((version & 0xff) < (tls_version & 0xff)) {
2751 version = (version & 0xff00) | 0xff;
2752 }
2753 /* DTLS 1.0 maps to TLS 1.1, not TLS 1.0. */
2754 if (version == TLS1_VERSION) {
2755 version = TLS1_1_VERSION;
2756 }
2757 return version;
2758}
2759
Adam Langleyfcf25832014-12-18 17:42:32 -08002760int SSL_cache_hit(SSL *s) { return s->hit; }
2761
2762int SSL_is_server(SSL *s) { return s->server; }
2763
Adam Langley524e7172015-02-20 16:04:00 -08002764void SSL_CTX_set_dos_protection_cb(
2765 SSL_CTX *ctx, int (*cb)(const struct ssl_early_callback_ctx *)) {
2766 ctx->dos_protection_cb = cb;
2767}
2768
David Benjaminb16346b2015-04-08 19:16:58 -04002769void SSL_set_reject_peer_renegotiations(SSL *s, int reject) {
David Benjamin897e5e02015-05-12 17:03:54 -04002770 s->accept_peer_renegotiations = !reject;
David Benjaminb16346b2015-04-08 19:16:58 -04002771}
2772
Adam Langley3f92d212015-02-20 15:32:52 -08002773int SSL_get_rc4_state(const SSL *ssl, const RC4_KEY **read_key,
2774 const RC4_KEY **write_key) {
2775 if (ssl->aead_read_ctx == NULL || ssl->aead_write_ctx == NULL) {
2776 return 0;
2777 }
2778
2779 return EVP_AEAD_CTX_get_rc4_state(&ssl->aead_read_ctx->ctx, read_key) &&
2780 EVP_AEAD_CTX_get_rc4_state(&ssl->aead_write_ctx->ctx, write_key);
2781}
David Benjaminda881e92015-04-26 14:45:04 -04002782
David Benjamin27bbae42015-09-13 00:54:37 -04002783int SSL_clear(SSL *ssl) {
2784 if (ssl->method == NULL) {
2785 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_METHOD_SPECIFIED);
2786 return 0;
2787 }
2788
2789 if (ssl_clear_bad_session(ssl)) {
2790 SSL_SESSION_free(ssl->session);
2791 ssl->session = NULL;
2792 }
2793
2794 ssl->hit = 0;
2795 ssl->shutdown = 0;
2796
2797 /* SSL_clear may be called before or after the |ssl| is initialized in either
2798 * accept or connect state. In the latter case, SSL_clear should preserve the
2799 * half and reset |ssl->state| accordingly. */
2800 if (ssl->handshake_func != NULL) {
2801 if (ssl->server) {
2802 SSL_set_accept_state(ssl);
2803 } else {
2804 SSL_set_connect_state(ssl);
2805 }
2806 } else {
2807 assert(ssl->state == 0);
2808 }
2809
2810 /* TODO(davidben): Some state on |ssl| is reset both in |SSL_new| and
2811 * |SSL_clear| because it is per-connection state rather than configuration
2812 * state. Per-connection state should be on |ssl->s3| and |ssl->d1| so it is
2813 * naturally reset at the right points between |SSL_new|, |SSL_clear|, and
2814 * |ssl3_new|. */
2815
2816 ssl->rwstate = SSL_NOTHING;
2817
2818 BUF_MEM_free(ssl->init_buf);
2819 ssl->init_buf = NULL;
2820
2821 ssl_clear_cipher_ctx(ssl);
2822
2823 OPENSSL_free(ssl->next_proto_negotiated);
2824 ssl->next_proto_negotiated = NULL;
2825 ssl->next_proto_negotiated_len = 0;
2826
2827 /* The ssl->d1->mtu is simultaneously configuration (preserved across
2828 * clear) and connection-specific state (gets reset).
2829 *
2830 * TODO(davidben): Avoid this. */
2831 unsigned mtu = 0;
2832 if (ssl->d1 != NULL) {
2833 mtu = ssl->d1->mtu;
2834 }
2835
2836 ssl->method->ssl_free(ssl);
2837 if (!ssl->method->ssl_new(ssl)) {
2838 return 0;
2839 }
2840 ssl->enc_method = ssl3_get_enc_method(ssl->version);
2841 assert(ssl->enc_method != NULL);
2842
2843 if (SSL_IS_DTLS(ssl) && (SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
2844 ssl->d1->mtu = mtu;
2845 }
2846
2847 ssl->client_version = ssl->version;
2848
2849 return 1;
2850}
2851
David Benjaminda881e92015-04-26 14:45:04 -04002852int SSL_CTX_sess_connect(const SSL_CTX *ctx) { return 0; }
2853int SSL_CTX_sess_connect_good(const SSL_CTX *ctx) { return 0; }
2854int SSL_CTX_sess_connect_renegotiate(const SSL_CTX *ctx) { return 0; }
2855int SSL_CTX_sess_accept(const SSL_CTX *ctx) { return 0; }
2856int SSL_CTX_sess_accept_renegotiate(const SSL_CTX *ctx) { return 0; }
2857int SSL_CTX_sess_accept_good(const SSL_CTX *ctx) { return 0; }
2858int SSL_CTX_sess_hits(const SSL_CTX *ctx) { return 0; }
2859int SSL_CTX_sess_cb_hits(const SSL_CTX *ctx) { return 0; }
2860int SSL_CTX_sess_misses(const SSL_CTX *ctx) { return 0; }
2861int SSL_CTX_sess_timeouts(const SSL_CTX *ctx) { return 0; }
2862int SSL_CTX_sess_cache_full(const SSL_CTX *ctx) { return 0; }
Matt Braithwaite4838d8a2015-08-20 13:20:03 -07002863void ERR_load_SSL_strings(void) {}
David Benjamin27bbae42015-09-13 00:54:37 -04002864void SSL_load_error_strings(void) {}