blob: 16abc47c83d029d46521beb0e0e49ac8260e8534 [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
Adam Langleyfcf25832014-12-18 17:42:32 -0800871int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
872 unsigned int sid_ctx_len) {
873 if (sid_ctx_len > sizeof ctx->sid_ctx) {
David Benjamin3570d732015-06-29 00:28:17 -0400874 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -0800875 return 0;
876 }
877 ctx->sid_ctx_length = sid_ctx_len;
878 memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700879
Adam Langleyfcf25832014-12-18 17:42:32 -0800880 return 1;
881}
Adam Langley95c29f32014-06-20 12:00:00 -0700882
Adam Langleyfcf25832014-12-18 17:42:32 -0800883int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
884 unsigned int sid_ctx_len) {
885 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
David Benjamin3570d732015-06-29 00:28:17 -0400886 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -0800887 return 0;
888 }
889 ssl->sid_ctx_length = sid_ctx_len;
890 memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700891
Adam Langleyfcf25832014-12-18 17:42:32 -0800892 return 1;
893}
Adam Langley95c29f32014-06-20 12:00:00 -0700894
Adam Langleyfcf25832014-12-18 17:42:32 -0800895int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800896 ctx->generate_session_id = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -0800897 return 1;
898}
Adam Langley95c29f32014-06-20 12:00:00 -0700899
Adam Langleyfcf25832014-12-18 17:42:32 -0800900int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800901 ssl->generate_session_id = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -0800902 return 1;
903}
Adam Langley95c29f32014-06-20 12:00:00 -0700904
Adam Langleyfcf25832014-12-18 17:42:32 -0800905int SSL_has_matching_session_id(const SSL *ssl, const uint8_t *id,
906 unsigned int id_len) {
907 /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how we
908 * can "construct" a session to give us the desired check - ie. to find if
909 * there's a session in the hash table that would conflict with any new
910 * session built out of this id/id_len and the ssl_version in use by this
911 * SSL. */
912 SSL_SESSION r, *p;
Adam Langley95c29f32014-06-20 12:00:00 -0700913
Adam Langleyfcf25832014-12-18 17:42:32 -0800914 if (id_len > sizeof r.session_id) {
915 return 0;
916 }
Adam Langley95c29f32014-06-20 12:00:00 -0700917
Adam Langleyfcf25832014-12-18 17:42:32 -0800918 r.ssl_version = ssl->version;
919 r.session_id_length = id_len;
920 memcpy(r.session_id, id, id_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700921
Adam Langley4bdb6e42015-05-15 15:29:21 -0700922 CRYPTO_MUTEX_lock_read(&ssl->ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -0800923 p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r);
Adam Langley4bdb6e42015-05-15 15:29:21 -0700924 CRYPTO_MUTEX_unlock(&ssl->ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -0800925 return p != NULL;
926}
Adam Langley95c29f32014-06-20 12:00:00 -0700927
Adam Langleyfcf25832014-12-18 17:42:32 -0800928int SSL_CTX_set_purpose(SSL_CTX *s, int purpose) {
929 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
930}
931
932int SSL_set_purpose(SSL *s, int purpose) {
933 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
934}
935
936int SSL_CTX_set_trust(SSL_CTX *s, int trust) {
937 return X509_VERIFY_PARAM_set_trust(s->param, trust);
938}
939
940int SSL_set_trust(SSL *s, int trust) {
941 return X509_VERIFY_PARAM_set_trust(s->param, trust);
942}
943
944int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm) {
945 return X509_VERIFY_PARAM_set1(ctx->param, vpm);
946}
947
948int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm) {
949 return X509_VERIFY_PARAM_set1(ssl->param, vpm);
950}
Adam Langley95c29f32014-06-20 12:00:00 -0700951
Adam Langley858a88d2014-06-20 12:00:00 -0700952void ssl_cipher_preference_list_free(
Adam Langleyfcf25832014-12-18 17:42:32 -0800953 struct ssl_cipher_preference_list_st *cipher_list) {
David Benjamin5d1ec732015-04-22 13:38:00 -0400954 if (cipher_list == NULL) {
955 return;
956 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800957 sk_SSL_CIPHER_free(cipher_list->ciphers);
958 OPENSSL_free(cipher_list->in_group_flags);
959 OPENSSL_free(cipher_list);
960}
Adam Langley858a88d2014-06-20 12:00:00 -0700961
Adam Langleyfcf25832014-12-18 17:42:32 -0800962struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_dup(
963 struct ssl_cipher_preference_list_st *cipher_list) {
964 struct ssl_cipher_preference_list_st *ret = NULL;
965 size_t n = sk_SSL_CIPHER_num(cipher_list->ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700966
Adam Langleyfcf25832014-12-18 17:42:32 -0800967 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
968 if (!ret) {
969 goto err;
970 }
971
972 ret->ciphers = NULL;
973 ret->in_group_flags = NULL;
974 ret->ciphers = sk_SSL_CIPHER_dup(cipher_list->ciphers);
975 if (!ret->ciphers) {
976 goto err;
977 }
978 ret->in_group_flags = BUF_memdup(cipher_list->in_group_flags, n);
979 if (!ret->in_group_flags) {
980 goto err;
981 }
982
983 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -0700984
985err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400986 ssl_cipher_preference_list_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -0800987 return NULL;
988}
Adam Langley858a88d2014-06-20 12:00:00 -0700989
Adam Langleyfcf25832014-12-18 17:42:32 -0800990struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_from_ciphers(
David Benjamin60da0cd2015-05-03 15:21:28 -0400991 STACK_OF(SSL_CIPHER) *ciphers) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800992 struct ssl_cipher_preference_list_st *ret = NULL;
993 size_t n = sk_SSL_CIPHER_num(ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700994
Adam Langleyfcf25832014-12-18 17:42:32 -0800995 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
996 if (!ret) {
997 goto err;
998 }
999 ret->ciphers = NULL;
1000 ret->in_group_flags = NULL;
1001 ret->ciphers = sk_SSL_CIPHER_dup(ciphers);
1002 if (!ret->ciphers) {
1003 goto err;
1004 }
1005 ret->in_group_flags = OPENSSL_malloc(n);
1006 if (!ret->in_group_flags) {
1007 goto err;
1008 }
1009 memset(ret->in_group_flags, 0, n);
1010 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -07001011
1012err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001013 ssl_cipher_preference_list_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -08001014 return NULL;
1015}
Adam Langley858a88d2014-06-20 12:00:00 -07001016
Adam Langleyfcf25832014-12-18 17:42:32 -08001017X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) { return ctx->param; }
Adam Langley95c29f32014-06-20 12:00:00 -07001018
Adam Langleyfcf25832014-12-18 17:42:32 -08001019X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) { return ssl->param; }
Adam Langley95c29f32014-06-20 12:00:00 -07001020
David Benjamin7481d392015-07-05 19:38:46 -04001021void SSL_certs_clear(SSL *ssl) { ssl_cert_clear_certs(ssl->cert); }
Adam Langley95c29f32014-06-20 12:00:00 -07001022
Adam Langleyfcf25832014-12-18 17:42:32 -08001023int SSL_get_fd(const SSL *s) { return SSL_get_rfd(s); }
Adam Langley95c29f32014-06-20 12:00:00 -07001024
Adam Langleyfcf25832014-12-18 17:42:32 -08001025int SSL_get_rfd(const SSL *s) {
1026 int ret = -1;
1027 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -07001028
Adam Langleyfcf25832014-12-18 17:42:32 -08001029 b = SSL_get_rbio(s);
1030 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
1031 if (r != NULL) {
1032 BIO_get_fd(r, &ret);
1033 }
1034 return ret;
1035}
Adam Langley95c29f32014-06-20 12:00:00 -07001036
Adam Langleyfcf25832014-12-18 17:42:32 -08001037int SSL_get_wfd(const SSL *s) {
1038 int ret = -1;
1039 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -07001040
Adam Langleyfcf25832014-12-18 17:42:32 -08001041 b = SSL_get_wbio(s);
1042 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
1043 if (r != NULL) {
1044 BIO_get_fd(r, &ret);
1045 }
Adam Langley95c29f32014-06-20 12:00:00 -07001046
Adam Langleyfcf25832014-12-18 17:42:32 -08001047 return ret;
1048}
Adam Langley95c29f32014-06-20 12:00:00 -07001049
Adam Langleyfcf25832014-12-18 17:42:32 -08001050int SSL_set_fd(SSL *s, int fd) {
1051 int ret = 0;
1052 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001053
Adam Langleyfcf25832014-12-18 17:42:32 -08001054 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -07001055
Adam Langleyfcf25832014-12-18 17:42:32 -08001056 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001057 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -08001058 goto err;
1059 }
1060 BIO_set_fd(bio, fd, BIO_NOCLOSE);
1061 SSL_set_bio(s, bio, bio);
1062 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001063
Adam Langley95c29f32014-06-20 12:00:00 -07001064err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001065 return ret;
1066}
Adam Langley95c29f32014-06-20 12:00:00 -07001067
Adam Langleyfcf25832014-12-18 17:42:32 -08001068int SSL_set_wfd(SSL *s, int fd) {
1069 int ret = 0;
1070 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001071
Adam Langleyfcf25832014-12-18 17:42:32 -08001072 if (s->rbio == NULL || BIO_method_type(s->rbio) != BIO_TYPE_FD ||
1073 (int)BIO_get_fd(s->rbio, NULL) != fd) {
1074 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -07001075
Adam Langleyfcf25832014-12-18 17:42:32 -08001076 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001077 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -08001078 goto err;
1079 }
1080 BIO_set_fd(bio, fd, BIO_NOCLOSE);
1081 SSL_set_bio(s, SSL_get_rbio(s), bio);
1082 } else {
1083 SSL_set_bio(s, SSL_get_rbio(s), SSL_get_rbio(s));
1084 }
1085
1086 ret = 1;
1087
Adam Langley95c29f32014-06-20 12:00:00 -07001088err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001089 return ret;
1090}
Adam Langley95c29f32014-06-20 12:00:00 -07001091
Adam Langleyfcf25832014-12-18 17:42:32 -08001092int SSL_set_rfd(SSL *s, int fd) {
1093 int ret = 0;
1094 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001095
Adam Langleyfcf25832014-12-18 17:42:32 -08001096 if (s->wbio == NULL || BIO_method_type(s->wbio) != BIO_TYPE_FD ||
1097 (int)BIO_get_fd(s->wbio, NULL) != fd) {
1098 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -07001099
Adam Langleyfcf25832014-12-18 17:42:32 -08001100 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001101 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -08001102 goto err;
1103 }
1104 BIO_set_fd(bio, fd, BIO_NOCLOSE);
1105 SSL_set_bio(s, bio, SSL_get_wbio(s));
1106 } else {
1107 SSL_set_bio(s, SSL_get_wbio(s), SSL_get_wbio(s));
1108 }
1109 ret = 1;
1110
Adam Langley95c29f32014-06-20 12:00:00 -07001111err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001112 return ret;
1113}
Adam Langley95c29f32014-06-20 12:00:00 -07001114
1115/* return length of latest Finished message we sent, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -08001116size_t SSL_get_finished(const SSL *s, void *buf, size_t count) {
1117 size_t ret = 0;
1118
1119 if (s->s3 != NULL) {
1120 ret = s->s3->tmp.finish_md_len;
1121 if (count > ret) {
1122 count = ret;
1123 }
1124 memcpy(buf, s->s3->tmp.finish_md, count);
1125 }
1126
1127 return ret;
1128}
Adam Langley95c29f32014-06-20 12:00:00 -07001129
1130/* return length of latest Finished message we expected, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -08001131size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count) {
1132 size_t ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001133
Adam Langleyfcf25832014-12-18 17:42:32 -08001134 if (s->s3 != NULL) {
1135 ret = s->s3->tmp.peer_finish_md_len;
1136 if (count > ret) {
1137 count = ret;
1138 }
1139 memcpy(buf, s->s3->tmp.peer_finish_md, count);
1140 }
Adam Langley95c29f32014-06-20 12:00:00 -07001141
Adam Langleyfcf25832014-12-18 17:42:32 -08001142 return ret;
1143}
Adam Langley95c29f32014-06-20 12:00:00 -07001144
Adam Langleyfcf25832014-12-18 17:42:32 -08001145int SSL_get_verify_mode(const SSL *s) { return s->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -07001146
Adam Langleyfcf25832014-12-18 17:42:32 -08001147int SSL_get_verify_depth(const SSL *s) {
1148 return X509_VERIFY_PARAM_get_depth(s->param);
1149}
Adam Langley95c29f32014-06-20 12:00:00 -07001150
Adam Langleyfcf25832014-12-18 17:42:32 -08001151int (*SSL_get_verify_callback(const SSL *s))(int, X509_STORE_CTX *) {
1152 return s->verify_callback;
1153}
Adam Langley95c29f32014-06-20 12:00:00 -07001154
Adam Langleyfcf25832014-12-18 17:42:32 -08001155int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) { return ctx->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -07001156
Adam Langleyfcf25832014-12-18 17:42:32 -08001157int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) {
1158 return X509_VERIFY_PARAM_get_depth(ctx->param);
1159}
Adam Langley95c29f32014-06-20 12:00:00 -07001160
Adam Langleyfcf25832014-12-18 17:42:32 -08001161int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int, X509_STORE_CTX *) {
1162 return ctx->default_verify_callback;
1163}
Adam Langley95c29f32014-06-20 12:00:00 -07001164
Adam Langleyfcf25832014-12-18 17:42:32 -08001165void SSL_set_verify(SSL *s, int mode,
1166 int (*callback)(int ok, X509_STORE_CTX *ctx)) {
1167 s->verify_mode = mode;
1168 if (callback != NULL) {
1169 s->verify_callback = callback;
1170 }
1171}
Adam Langley95c29f32014-06-20 12:00:00 -07001172
Adam Langleyfcf25832014-12-18 17:42:32 -08001173void SSL_set_verify_depth(SSL *s, int depth) {
1174 X509_VERIFY_PARAM_set_depth(s->param, depth);
1175}
Adam Langley95c29f32014-06-20 12:00:00 -07001176
David Benjamin9a41d1b2015-05-16 01:30:09 -04001177int SSL_CTX_get_read_ahead(const SSL_CTX *ctx) { return 0; }
Adam Langley95c29f32014-06-20 12:00:00 -07001178
David Benjamin9a41d1b2015-05-16 01:30:09 -04001179int SSL_get_read_ahead(const SSL *s) { return 0; }
Adam Langley95c29f32014-06-20 12:00:00 -07001180
David Benjamin9a41d1b2015-05-16 01:30:09 -04001181void SSL_CTX_set_read_ahead(SSL_CTX *ctx, int yes) { }
David Benjamin61ecccf2015-05-05 09:44:51 -04001182
David Benjamin9a41d1b2015-05-16 01:30:09 -04001183void SSL_set_read_ahead(SSL *s, int yes) { }
David Benjamin61ecccf2015-05-05 09:44:51 -04001184
Adam Langleyfcf25832014-12-18 17:42:32 -08001185int SSL_pending(const SSL *s) {
David Benjamin904dc722015-05-30 16:39:07 -04001186 return (s->s3->rrec.type == SSL3_RT_APPLICATION_DATA) ? s->s3->rrec.length
1187 : 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001188}
Adam Langley95c29f32014-06-20 12:00:00 -07001189
David Benjamin26416e92015-08-22 16:04:17 -04001190X509 *SSL_get_peer_certificate(const SSL *ssl) {
1191 if (ssl == NULL || ssl->session == NULL || ssl->session->peer == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001192 return NULL;
1193 }
David Benjamin26416e92015-08-22 16:04:17 -04001194 return X509_up_ref(ssl->session->peer);
Adam Langleyfcf25832014-12-18 17:42:32 -08001195}
1196
David Benjamin26416e92015-08-22 16:04:17 -04001197STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *ssl) {
1198 if (ssl == NULL || ssl->session == NULL) {
1199 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08001200 }
David Benjamin26416e92015-08-22 16:04:17 -04001201 return ssl->session->cert_chain;
Adam Langleyfcf25832014-12-18 17:42:32 -08001202}
Adam Langley95c29f32014-06-20 12:00:00 -07001203
Adam Langley95c29f32014-06-20 12:00:00 -07001204/* Fix this so it checks all the valid key/cert options */
Adam Langleyfcf25832014-12-18 17:42:32 -08001205int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
David Benjamin651b3d92015-08-09 12:07:25 -04001206 if (ctx->cert->x509 == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001207 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -08001208 return 0;
1209 }
1210
David Benjamind1d80782015-07-05 11:54:09 -04001211 if (ctx->cert->privatekey == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001212 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -08001213 return 0;
1214 }
1215
David Benjamind1d80782015-07-05 11:54:09 -04001216 return X509_check_private_key(ctx->cert->x509, ctx->cert->privatekey);
Adam Langleyfcf25832014-12-18 17:42:32 -08001217}
Adam Langley95c29f32014-06-20 12:00:00 -07001218
1219/* Fix this function so that it takes an optional type parameter */
Adam Langleyfcf25832014-12-18 17:42:32 -08001220int SSL_check_private_key(const SSL *ssl) {
David Benjamind1d80782015-07-05 11:54:09 -04001221 if (ssl->cert->x509 == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001222 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -08001223 return 0;
1224 }
David Benjamin0b145c22014-11-26 20:10:09 -05001225
David Benjamind1d80782015-07-05 11:54:09 -04001226 if (ssl->cert->privatekey == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001227 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -08001228 return 0;
1229 }
Adam Langley95c29f32014-06-20 12:00:00 -07001230
David Benjamind1d80782015-07-05 11:54:09 -04001231 return X509_check_private_key(ssl->cert->x509, ssl->cert->privatekey);
Adam Langleyfcf25832014-12-18 17:42:32 -08001232}
Adam Langley95c29f32014-06-20 12:00:00 -07001233
Adam Langleyfcf25832014-12-18 17:42:32 -08001234long SSL_get_default_timeout(const SSL *s) {
1235 return SSL_DEFAULT_SESSION_TIMEOUT;
1236}
Adam Langley95c29f32014-06-20 12:00:00 -07001237
David Benjamin44d3eed2015-05-21 01:29:55 -04001238int SSL_renegotiate(SSL *ssl) {
1239 /* Caller-initiated renegotiation is not supported. */
David Benjamin3570d732015-06-29 00:28:17 -04001240 OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
David Benjamin44d3eed2015-05-21 01:29:55 -04001241 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001242}
Adam Langley95c29f32014-06-20 12:00:00 -07001243
David Benjamin44d3eed2015-05-21 01:29:55 -04001244int SSL_renegotiate_pending(SSL *ssl) {
1245 return SSL_in_init(ssl) && ssl->s3->initial_handshake_complete;
Adam Langleyfcf25832014-12-18 17:42:32 -08001246}
Adam Langley95c29f32014-06-20 12:00:00 -07001247
David Benjamin1d0a1942015-04-26 15:35:35 -04001248size_t SSL_CTX_get_max_cert_list(const SSL_CTX *ctx) {
1249 return ctx->max_cert_list;
1250}
1251
1252void SSL_CTX_set_max_cert_list(SSL_CTX *ctx, size_t max_cert_list) {
1253 if (max_cert_list > kMaxHandshakeSize) {
1254 max_cert_list = kMaxHandshakeSize;
1255 }
1256 ctx->max_cert_list = (uint32_t)max_cert_list;
1257}
1258
1259size_t SSL_get_max_cert_list(const SSL *ssl) {
1260 return ssl->max_cert_list;
1261}
1262
1263void SSL_set_max_cert_list(SSL *ssl, size_t max_cert_list) {
1264 if (max_cert_list > kMaxHandshakeSize) {
1265 max_cert_list = kMaxHandshakeSize;
1266 }
1267 ssl->max_cert_list = (uint32_t)max_cert_list;
1268}
1269
1270void SSL_CTX_set_max_send_fragment(SSL_CTX *ctx, size_t max_send_fragment) {
1271 if (max_send_fragment < 512) {
1272 max_send_fragment = 512;
1273 }
1274 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1275 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1276 }
1277 ctx->max_send_fragment = (uint16_t)max_send_fragment;
1278}
1279
1280void SSL_set_max_send_fragment(SSL *ssl, size_t max_send_fragment) {
1281 if (max_send_fragment < 512) {
1282 max_send_fragment = 512;
1283 }
1284 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1285 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1286 }
1287 ssl->max_send_fragment = (uint16_t)max_send_fragment;
1288}
1289
David Benjamincb9cf792015-05-05 09:46:14 -04001290int SSL_set_mtu(SSL *ssl, unsigned mtu) {
1291 if (!SSL_IS_DTLS(ssl) || mtu < dtls1_min_mtu()) {
1292 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001293 }
David Benjamincb9cf792015-05-05 09:46:14 -04001294 ssl->d1->mtu = mtu;
1295 return 1;
1296}
1297
1298int SSL_get_secure_renegotiation_support(const SSL *ssl) {
1299 return ssl->s3->send_connection_binding;
1300}
1301
Adam Langleyfcf25832014-12-18 17:42:32 -08001302LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) { return ctx->sessions; }
1303
David Benjamin71f7d3d2015-05-05 09:46:38 -04001304size_t SSL_CTX_sess_number(const SSL_CTX *ctx) {
1305 return lh_SSL_SESSION_num_items(ctx->sessions);
1306}
1307
1308unsigned long SSL_CTX_sess_set_cache_size(SSL_CTX *ctx, unsigned long size) {
1309 unsigned long ret = ctx->session_cache_size;
1310 ctx->session_cache_size = size;
1311 return ret;
1312}
1313
1314unsigned long SSL_CTX_sess_get_cache_size(const SSL_CTX *ctx) {
1315 return ctx->session_cache_size;
1316}
1317
1318int SSL_CTX_set_session_cache_mode(SSL_CTX *ctx, int mode) {
1319 int ret = ctx->session_cache_mode;
1320 ctx->session_cache_mode = mode;
1321 return ret;
1322}
1323
1324int SSL_CTX_get_session_cache_mode(const SSL_CTX *ctx) {
1325 return ctx->session_cache_mode;
1326}
1327
Adam Langleyfcf25832014-12-18 17:42:32 -08001328/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001329 * preference */
David Benjamin60da0cd2015-05-03 15:21:28 -04001330STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001331 if (s == NULL) {
1332 return NULL;
1333 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001334
Adam Langleyfcf25832014-12-18 17:42:32 -08001335 if (s->cipher_list != NULL) {
1336 return s->cipher_list->ciphers;
1337 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001338
Adam Langleyfcf25832014-12-18 17:42:32 -08001339 if (s->version >= TLS1_1_VERSION && s->ctx != NULL &&
1340 s->ctx->cipher_list_tls11 != NULL) {
1341 return s->ctx->cipher_list_tls11->ciphers;
1342 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001343
Adam Langleycef75832015-09-03 14:51:12 -07001344 if (s->version >= TLS1_VERSION && s->ctx != NULL &&
1345 s->ctx->cipher_list_tls10 != NULL) {
1346 return s->ctx->cipher_list_tls10->ciphers;
1347 }
1348
Adam Langleyfcf25832014-12-18 17:42:32 -08001349 if (s->ctx != NULL && s->ctx->cipher_list != NULL) {
1350 return s->ctx->cipher_list->ciphers;
1351 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001352
Adam Langleyfcf25832014-12-18 17:42:32 -08001353 return NULL;
1354}
Adam Langley95c29f32014-06-20 12:00:00 -07001355
Adam Langleyfcf25832014-12-18 17:42:32 -08001356/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001357 * algorithm id */
David Benjamin60da0cd2015-05-03 15:21:28 -04001358STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001359 if (s == NULL) {
1360 return NULL;
1361 }
Adam Langley95c29f32014-06-20 12:00:00 -07001362
Adam Langleyfcf25832014-12-18 17:42:32 -08001363 if (s->cipher_list_by_id != NULL) {
1364 return s->cipher_list_by_id;
1365 }
Adam Langley95c29f32014-06-20 12:00:00 -07001366
Adam Langleyfcf25832014-12-18 17:42:32 -08001367 if (s->ctx != NULL && s->ctx->cipher_list_by_id != NULL) {
1368 return s->ctx->cipher_list_by_id;
1369 }
Adam Langley95c29f32014-06-20 12:00:00 -07001370
Adam Langleyfcf25832014-12-18 17:42:32 -08001371 return NULL;
1372}
Adam Langley95c29f32014-06-20 12:00:00 -07001373
Adam Langleyfcf25832014-12-18 17:42:32 -08001374/* The old interface to get the same thing as SSL_get_ciphers() */
1375const char *SSL_get_cipher_list(const SSL *s, int n) {
1376 const SSL_CIPHER *c;
David Benjamin60da0cd2015-05-03 15:21:28 -04001377 STACK_OF(SSL_CIPHER) *sk;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001378
Adam Langleyfcf25832014-12-18 17:42:32 -08001379 if (s == NULL) {
1380 return NULL;
1381 }
Adam Langley95c29f32014-06-20 12:00:00 -07001382
Adam Langleyfcf25832014-12-18 17:42:32 -08001383 sk = SSL_get_ciphers(s);
1384 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
1385 return NULL;
1386 }
Adam Langley95c29f32014-06-20 12:00:00 -07001387
Adam Langleyfcf25832014-12-18 17:42:32 -08001388 c = sk_SSL_CIPHER_value(sk, n);
1389 if (c == NULL) {
1390 return NULL;
1391 }
Adam Langley95c29f32014-06-20 12:00:00 -07001392
Adam Langleyfcf25832014-12-18 17:42:32 -08001393 return c->name;
1394}
David Benjamin5491e3f2014-09-29 19:33:09 -04001395
Adam Langleyfcf25832014-12-18 17:42:32 -08001396/* specify the ciphers to be used by default by the SSL_CTX */
1397int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
1398 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001399
Adam Langleyfcf25832014-12-18 17:42:32 -08001400 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001401 &ctx->cipher_list_by_id, str);
Adam Langleyfcf25832014-12-18 17:42:32 -08001402 /* ssl_create_cipher_list may return an empty stack if it was unable to find
1403 * a cipher matching the given rule string (for example if the rule string
1404 * specifies a cipher which has been disabled). This is not an error as far
1405 * as ssl_create_cipher_list is concerned, and hence ctx->cipher_list and
1406 * ctx->cipher_list_by_id has been updated. */
1407 if (sk == NULL) {
1408 return 0;
1409 } else if (sk_SSL_CIPHER_num(sk) == 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001410 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
Adam Langleyfcf25832014-12-18 17:42:32 -08001411 return 0;
1412 }
Adam Langley95c29f32014-06-20 12:00:00 -07001413
Adam Langleyfcf25832014-12-18 17:42:32 -08001414 return 1;
1415}
David Benjamin39482a12014-07-20 13:30:15 -04001416
Adam Langleycef75832015-09-03 14:51:12 -07001417int SSL_CTX_set_cipher_list_tls10(SSL_CTX *ctx, const char *str) {
1418 STACK_OF(SSL_CIPHER) *sk;
1419
1420 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls10, NULL, str);
1421 if (sk == NULL) {
1422 return 0;
1423 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1424 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
1425 return 0;
1426 }
1427
1428 return 1;
1429}
1430
Adam Langleyfcf25832014-12-18 17:42:32 -08001431int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str) {
1432 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001433
David Benjamin71f07942015-04-08 02:36:59 -04001434 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str);
Adam Langleyfcf25832014-12-18 17:42:32 -08001435 if (sk == NULL) {
1436 return 0;
1437 } else if (sk_SSL_CIPHER_num(sk) == 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001438 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
Adam Langleyfcf25832014-12-18 17:42:32 -08001439 return 0;
1440 }
David Benjamin9f2c0d72014-10-21 22:00:19 -04001441
Adam Langleyfcf25832014-12-18 17:42:32 -08001442 return 1;
1443}
Adam Langley95c29f32014-06-20 12:00:00 -07001444
Adam Langleyfcf25832014-12-18 17:42:32 -08001445/* specify the ciphers to be used by the SSL */
1446int SSL_set_cipher_list(SSL *s, const char *str) {
1447 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001448
Adam Langleyfcf25832014-12-18 17:42:32 -08001449 sk = ssl_create_cipher_list(s->ctx->method, &s->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001450 &s->cipher_list_by_id, str);
David Benjamin39482a12014-07-20 13:30:15 -04001451
Adam Langleyfcf25832014-12-18 17:42:32 -08001452 /* see comment in SSL_CTX_set_cipher_list */
1453 if (sk == NULL) {
1454 return 0;
1455 } else if (sk_SSL_CIPHER_num(sk) == 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001456 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
Adam Langleyfcf25832014-12-18 17:42:32 -08001457 return 0;
1458 }
David Benjamin39482a12014-07-20 13:30:15 -04001459
Adam Langleyfcf25832014-12-18 17:42:32 -08001460 return 1;
1461}
Adam Langley95c29f32014-06-20 12:00:00 -07001462
Adam Langleyfcf25832014-12-18 17:42:32 -08001463int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, uint8_t *p) {
1464 size_t i;
1465 const SSL_CIPHER *c;
1466 CERT *ct = s->cert;
1467 uint8_t *q;
1468 /* Set disabled masks for this session */
1469 ssl_set_client_disabled(s);
Adam Langley29707792014-06-20 12:00:00 -07001470
Adam Langleyfcf25832014-12-18 17:42:32 -08001471 if (sk == NULL) {
1472 return 0;
1473 }
1474 q = p;
Adam Langley95c29f32014-06-20 12:00:00 -07001475
Adam Langleyfcf25832014-12-18 17:42:32 -08001476 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
1477 c = sk_SSL_CIPHER_value(sk, i);
1478 /* Skip disabled ciphers */
1479 if (c->algorithm_ssl & ct->mask_ssl ||
1480 c->algorithm_mkey & ct->mask_k ||
1481 c->algorithm_auth & ct->mask_a) {
1482 continue;
1483 }
David Benjamina1c90a52015-05-30 17:03:14 -04001484 s2n(ssl_cipher_get_value(c), p);
Adam Langleyfcf25832014-12-18 17:42:32 -08001485 }
1486
1487 /* If all ciphers were disabled, return the error to the caller. */
1488 if (p == q) {
1489 return 0;
1490 }
1491
Adam Langley5021b222015-06-12 18:27:58 -07001492 /* For SSLv3, the SCSV is added. Otherwise the renegotiation extension is
1493 * added. */
1494 if (s->client_version == SSL3_VERSION &&
1495 !s->s3->initial_handshake_complete) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001496 s2n(SSL3_CK_SCSV & 0xffff, p);
Adam Langley5021b222015-06-12 18:27:58 -07001497 /* The renegotiation extension is required to be at index zero. */
1498 s->s3->tmp.extensions.sent |= (1u << 0);
Adam Langleyfcf25832014-12-18 17:42:32 -08001499 }
1500
Adam Langley5f0efe02015-02-20 13:03:16 -08001501 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001502 s2n(SSL3_CK_FALLBACK_SCSV & 0xffff, p);
1503 }
1504
1505 return p - q;
1506}
1507
1508STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs) {
1509 CBS cipher_suites = *cbs;
1510 const SSL_CIPHER *c;
David Benjamin60da0cd2015-05-03 15:21:28 -04001511 STACK_OF(SSL_CIPHER) *sk;
Adam Langleyfcf25832014-12-18 17:42:32 -08001512
1513 if (s->s3) {
1514 s->s3->send_connection_binding = 0;
1515 }
1516
1517 if (CBS_len(&cipher_suites) % 2 != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001518 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
Adam Langleyfcf25832014-12-18 17:42:32 -08001519 return NULL;
1520 }
1521
1522 sk = sk_SSL_CIPHER_new_null();
1523 if (sk == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001524 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001525 goto err;
1526 }
1527
Adam Langleyfcf25832014-12-18 17:42:32 -08001528 while (CBS_len(&cipher_suites) > 0) {
1529 uint16_t cipher_suite;
1530
1531 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
David Benjamin3570d732015-06-29 00:28:17 -04001532 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001533 goto err;
1534 }
1535
1536 /* Check for SCSV. */
1537 if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff)) {
1538 /* SCSV is fatal if renegotiating. */
David Benjamin20f6e972015-05-15 21:51:49 -04001539 if (s->s3->initial_handshake_complete) {
David Benjamin3570d732015-06-29 00:28:17 -04001540 OPENSSL_PUT_ERROR(SSL, SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
Adam Langleyfcf25832014-12-18 17:42:32 -08001541 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1542 goto err;
1543 }
1544 s->s3->send_connection_binding = 1;
1545 continue;
1546 }
1547
1548 /* Check for FALLBACK_SCSV. */
1549 if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff)) {
1550 uint16_t max_version = ssl3_get_max_server_version(s);
1551 if (SSL_IS_DTLS(s) ? (uint16_t)s->version > max_version
1552 : (uint16_t)s->version < max_version) {
David Benjamin3570d732015-06-29 00:28:17 -04001553 OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK);
Adam Langleyfcf25832014-12-18 17:42:32 -08001554 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
1555 goto err;
1556 }
1557 continue;
1558 }
1559
David Benjamina1c90a52015-05-30 17:03:14 -04001560 c = SSL_get_cipher_by_value(cipher_suite);
Adam Langleyfcf25832014-12-18 17:42:32 -08001561 if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
David Benjamin3570d732015-06-29 00:28:17 -04001562 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001563 goto err;
1564 }
1565 }
1566
1567 return sk;
David Benjamin9f2c0d72014-10-21 22:00:19 -04001568
Adam Langley95c29f32014-06-20 12:00:00 -07001569err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001570 sk_SSL_CIPHER_free(sk);
Adam Langleyfcf25832014-12-18 17:42:32 -08001571 return NULL;
1572}
Adam Langley95c29f32014-06-20 12:00:00 -07001573
1574
Adam Langleyfcf25832014-12-18 17:42:32 -08001575/* return a servername extension value if provided in Client Hello, or NULL. So
1576 * far, only host_name types are defined (RFC 3546). */
1577const char *SSL_get_servername(const SSL *s, const int type) {
1578 if (type != TLSEXT_NAMETYPE_host_name) {
1579 return NULL;
1580 }
Adam Langley95c29f32014-06-20 12:00:00 -07001581
Adam Langleyfcf25832014-12-18 17:42:32 -08001582 return s->session && !s->tlsext_hostname ? s->session->tlsext_hostname
1583 : s->tlsext_hostname;
1584}
Adam Langley95c29f32014-06-20 12:00:00 -07001585
Adam Langleyfcf25832014-12-18 17:42:32 -08001586int SSL_get_servername_type(const SSL *s) {
1587 if (s->session &&
1588 (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname)) {
1589 return TLSEXT_NAMETYPE_host_name;
1590 }
Adam Langley95c29f32014-06-20 12:00:00 -07001591
Adam Langleyfcf25832014-12-18 17:42:32 -08001592 return -1;
1593}
Adam Langley95c29f32014-06-20 12:00:00 -07001594
Adam Langleyfcf25832014-12-18 17:42:32 -08001595void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
1596 ctx->signed_cert_timestamps_enabled = 1;
1597}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001598
Adam Langleyfcf25832014-12-18 17:42:32 -08001599int SSL_enable_signed_cert_timestamps(SSL *ssl) {
1600 ssl->signed_cert_timestamps_enabled = 1;
1601 return 1;
1602}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001603
Adam Langleyfcf25832014-12-18 17:42:32 -08001604void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
1605 ctx->ocsp_stapling_enabled = 1;
1606}
David Benjamin6c7aed02014-08-27 16:42:38 -04001607
Adam Langleyfcf25832014-12-18 17:42:32 -08001608int SSL_enable_ocsp_stapling(SSL *ssl) {
1609 ssl->ocsp_stapling_enabled = 1;
1610 return 1;
1611}
David Benjamin6c7aed02014-08-27 16:42:38 -04001612
Adam Langleyfcf25832014-12-18 17:42:32 -08001613void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
1614 size_t *out_len) {
1615 SSL_SESSION *session = ssl->session;
Adam Langley3cb50e02014-08-26 14:00:31 -07001616
Adam Langleyfcf25832014-12-18 17:42:32 -08001617 *out_len = 0;
1618 *out = NULL;
1619 if (ssl->server || !session || !session->tlsext_signed_cert_timestamp_list) {
1620 return;
1621 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001622
Adam Langleyfcf25832014-12-18 17:42:32 -08001623 *out = session->tlsext_signed_cert_timestamp_list;
1624 *out_len = session->tlsext_signed_cert_timestamp_list_length;
1625}
David Benjamin6c7aed02014-08-27 16:42:38 -04001626
Adam Langleyfcf25832014-12-18 17:42:32 -08001627void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
1628 size_t *out_len) {
1629 SSL_SESSION *session = ssl->session;
1630
1631 *out_len = 0;
1632 *out = NULL;
1633 if (ssl->server || !session || !session->ocsp_response) {
1634 return;
1635 }
1636 *out = session->ocsp_response;
1637 *out_len = session->ocsp_response_length;
1638}
David Benjamin6c7aed02014-08-27 16:42:38 -04001639
Paul Lietar4fac72e2015-09-09 13:44:55 +01001640int SSL_CTX_set_signed_cert_timestamp_list(SSL_CTX *ctx, const uint8_t *list,
1641 size_t list_len) {
1642 OPENSSL_free(ctx->signed_cert_timestamp_list);
1643 ctx->signed_cert_timestamp_list_length = 0;
1644
1645 ctx->signed_cert_timestamp_list = BUF_memdup(list, list_len);
1646 if (ctx->signed_cert_timestamp_list == NULL) {
1647 return 0;
1648 }
1649 ctx->signed_cert_timestamp_list_length = list_len;
1650
1651 return 1;
1652}
1653
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001654int SSL_CTX_set_ocsp_response(SSL_CTX *ctx, const uint8_t *response,
1655 size_t response_len) {
1656 OPENSSL_free(ctx->ocsp_response);
1657 ctx->ocsp_response_length = 0;
1658
1659 ctx->ocsp_response = BUF_memdup(response, response_len);
1660 if (ctx->ocsp_response == NULL) {
1661 return 0;
1662 }
1663 ctx->ocsp_response_length = response_len;
1664
1665 return 1;
1666}
1667
Adam Langley95c29f32014-06-20 12:00:00 -07001668/* SSL_select_next_proto implements the standard protocol selection. It is
1669 * expected that this function is called from the callback set by
1670 * SSL_CTX_set_next_proto_select_cb.
1671 *
1672 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1673 * strings. The length byte itself is not included in the length. A byte
1674 * string of length 0 is invalid. No byte string may be truncated.
1675 *
1676 * The current, but experimental algorithm for selecting the protocol is:
1677 *
1678 * 1) If the server doesn't support NPN then this is indicated to the
1679 * callback. In this case, the client application has to abort the connection
1680 * or have a default application level protocol.
1681 *
1682 * 2) If the server supports NPN, but advertises an empty list then the
1683 * client selects the first protcol in its list, but indicates via the
1684 * API that this fallback case was enacted.
1685 *
1686 * 3) Otherwise, the client finds the first protocol in the server's list
1687 * that it supports and selects this protocol. This is because it's
1688 * assumed that the server has better information about which protocol
1689 * a client should use.
1690 *
1691 * 4) If the client doesn't support any of the server's advertised
1692 * protocols, then this is treated the same as case 2.
1693 *
1694 * It returns either
1695 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1696 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1697 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001698int SSL_select_next_proto(uint8_t **out, uint8_t *outlen, const uint8_t *server,
1699 unsigned int server_len, const uint8_t *client,
1700 unsigned int client_len) {
1701 unsigned int i, j;
1702 const uint8_t *result;
1703 int status = OPENSSL_NPN_UNSUPPORTED;
Adam Langley95c29f32014-06-20 12:00:00 -07001704
Adam Langleyfcf25832014-12-18 17:42:32 -08001705 /* For each protocol in server preference order, see if we support it. */
1706 for (i = 0; i < server_len;) {
1707 for (j = 0; j < client_len;) {
1708 if (server[i] == client[j] &&
1709 memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
1710 /* We found a match */
1711 result = &server[i];
1712 status = OPENSSL_NPN_NEGOTIATED;
1713 goto found;
1714 }
1715 j += client[j];
1716 j++;
1717 }
1718 i += server[i];
1719 i++;
1720 }
Adam Langley95c29f32014-06-20 12:00:00 -07001721
Adam Langleyfcf25832014-12-18 17:42:32 -08001722 /* There's no overlap between our protocols and the server's list. */
1723 result = client;
1724 status = OPENSSL_NPN_NO_OVERLAP;
Adam Langley95c29f32014-06-20 12:00:00 -07001725
Adam Langleyfcf25832014-12-18 17:42:32 -08001726found:
1727 *out = (uint8_t *)result + 1;
1728 *outlen = result[0];
1729 return status;
1730}
Adam Langley95c29f32014-06-20 12:00:00 -07001731
Adam Langley95c29f32014-06-20 12:00:00 -07001732/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
1733 * requested protocol for this connection and returns 0. If the client didn't
1734 * request any protocol, then *data is set to NULL.
1735 *
1736 * Note that the client can request any protocol it chooses. The value returned
1737 * from this function need not be a member of the list of supported protocols
Adam Langleyfcf25832014-12-18 17:42:32 -08001738 * provided by the callback. */
1739void SSL_get0_next_proto_negotiated(const SSL *s, const uint8_t **data,
1740 unsigned *len) {
1741 *data = s->next_proto_negotiated;
1742 if (!*data) {
1743 *len = 0;
1744 } else {
1745 *len = s->next_proto_negotiated_len;
1746 }
Adam Langley95c29f32014-06-20 12:00:00 -07001747}
1748
1749/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
1750 * TLS server needs a list of supported protocols for Next Protocol
1751 * Negotiation. The returned list must be in wire format. The list is returned
1752 * by setting |out| to point to it and |outlen| to its length. This memory will
1753 * not be modified, but one should assume that the SSL* keeps a reference to
1754 * it.
1755 *
Adam Langleyfcf25832014-12-18 17:42:32 -08001756 * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise.
1757 * Otherwise, no such extension will be included in the ServerHello. */
1758void SSL_CTX_set_next_protos_advertised_cb(
1759 SSL_CTX *ctx,
1760 int (*cb)(SSL *ssl, const uint8_t **out, unsigned int *outlen, void *arg),
1761 void *arg) {
1762 ctx->next_protos_advertised_cb = cb;
1763 ctx->next_protos_advertised_cb_arg = arg;
1764}
Adam Langley95c29f32014-06-20 12:00:00 -07001765
1766/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1767 * client needs to select a protocol from the server's provided list. |out|
1768 * must be set to point to the selected protocol (which may be within |in|).
1769 * The length of the protocol name must be written into |outlen|. The server's
1770 * advertised protocols are provided in |in| and |inlen|. The callback can
1771 * assume that |in| is syntactically valid.
1772 *
1773 * The client must select a protocol. It is fatal to the connection if this
1774 * callback returns a value other than SSL_TLSEXT_ERR_OK.
1775 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001776void SSL_CTX_set_next_proto_select_cb(
1777 SSL_CTX *ctx, int (*cb)(SSL *s, uint8_t **out, uint8_t *outlen,
1778 const uint8_t *in, unsigned int inlen, void *arg),
1779 void *arg) {
1780 ctx->next_proto_select_cb = cb;
1781 ctx->next_proto_select_cb_arg = arg;
1782}
Adam Langley95c29f32014-06-20 12:00:00 -07001783
Adam Langleyfcf25832014-12-18 17:42:32 -08001784int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
1785 unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001786 OPENSSL_free(ctx->alpn_client_proto_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001787 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1788 if (!ctx->alpn_client_proto_list) {
1789 return 1;
1790 }
1791 ctx->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001792
Adam Langleyfcf25832014-12-18 17:42:32 -08001793 return 0;
1794}
Adam Langley95c29f32014-06-20 12:00:00 -07001795
Adam Langleyfcf25832014-12-18 17:42:32 -08001796int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001797 OPENSSL_free(ssl->alpn_client_proto_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001798 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1799 if (!ssl->alpn_client_proto_list) {
1800 return 1;
1801 }
1802 ssl->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001803
Adam Langleyfcf25832014-12-18 17:42:32 -08001804 return 0;
1805}
Adam Langley95c29f32014-06-20 12:00:00 -07001806
1807/* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
1808 * during ClientHello processing in order to select an ALPN protocol from the
1809 * client's list of offered protocols. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001810void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
1811 int (*cb)(SSL *ssl, const uint8_t **out,
1812 uint8_t *outlen, const uint8_t *in,
1813 unsigned int inlen, void *arg),
1814 void *arg) {
1815 ctx->alpn_select_cb = cb;
1816 ctx->alpn_select_cb_arg = arg;
1817}
Adam Langley95c29f32014-06-20 12:00:00 -07001818
1819/* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
1820 * On return it sets |*data| to point to |*len| bytes of protocol name (not
1821 * including the leading length-prefix byte). If the server didn't respond with
1822 * a negotiated protocol then |*len| will be zero. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001823void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **data,
1824 unsigned *len) {
1825 *data = NULL;
1826 if (ssl->s3) {
1827 *data = ssl->s3->alpn_selected;
1828 }
1829 if (*data == NULL) {
1830 *len = 0;
1831 } else {
1832 *len = ssl->s3->alpn_selected_len;
1833 }
1834}
Adam Langley95c29f32014-06-20 12:00:00 -07001835
David Benjamincfd248b2015-04-03 11:02:24 -04001836int SSL_export_keying_material(SSL *s, uint8_t *out, size_t out_len,
1837 const char *label, size_t label_len,
1838 const uint8_t *context, size_t context_len,
1839 int use_context) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001840 if (s->version < TLS1_VERSION) {
David Benjaminc565ebb2015-04-03 04:06:36 -04001841 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001842 }
Adam Langley95c29f32014-06-20 12:00:00 -07001843
David Benjamincfd248b2015-04-03 11:02:24 -04001844 return s->enc_method->export_keying_material(
1845 s, out, out_len, label, label_len, context, context_len, use_context);
Adam Langleyfcf25832014-12-18 17:42:32 -08001846}
Adam Langley95c29f32014-06-20 12:00:00 -07001847
Adam Langleyfcf25832014-12-18 17:42:32 -08001848void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) {
1849 ctx->default_passwd_callback = cb;
1850}
Adam Langley95c29f32014-06-20 12:00:00 -07001851
Adam Langleyfcf25832014-12-18 17:42:32 -08001852void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u) {
1853 ctx->default_passwd_callback_userdata = u;
1854}
Adam Langley95c29f32014-06-20 12:00:00 -07001855
Adam Langleyfcf25832014-12-18 17:42:32 -08001856void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
1857 int (*cb)(X509_STORE_CTX *, void *),
1858 void *arg) {
1859 ctx->app_verify_callback = cb;
1860 ctx->app_verify_arg = arg;
1861}
Adam Langley95c29f32014-06-20 12:00:00 -07001862
Adam Langleyfcf25832014-12-18 17:42:32 -08001863void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
1864 int (*cb)(int, X509_STORE_CTX *)) {
1865 ctx->verify_mode = mode;
1866 ctx->default_verify_callback = cb;
1867}
Adam Langley95c29f32014-06-20 12:00:00 -07001868
Adam Langleyfcf25832014-12-18 17:42:32 -08001869void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
1870 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
1871}
Adam Langley1258b6a2014-06-20 12:00:00 -07001872
David Benjamin7481d392015-07-05 19:38:46 -04001873void SSL_CTX_set_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl, void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08001874 void *arg) {
David Benjamin7481d392015-07-05 19:38:46 -04001875 ssl_cert_set_cert_cb(ctx->cert, cb, arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08001876}
David Benjamin859ec3c2014-09-02 16:29:36 -04001877
David Benjamin7481d392015-07-05 19:38:46 -04001878void SSL_set_cert_cb(SSL *ssl, int (*cb)(SSL *ssl, void *arg), void *arg) {
1879 ssl_cert_set_cert_cb(ssl->cert, cb, arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08001880}
Adam Langley95c29f32014-06-20 12:00:00 -07001881
David Benjamin107db582015-04-08 00:41:59 -04001882void ssl_get_compatible_server_ciphers(SSL *s, uint32_t *out_mask_k,
1883 uint32_t *out_mask_a) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001884 CERT *c = s->cert;
David Benjamind1d80782015-07-05 11:54:09 -04001885 int have_rsa_cert = 0, dh_tmp;
David Benjamin107db582015-04-08 00:41:59 -04001886 uint32_t mask_k, mask_a;
David Benjamind1d80782015-07-05 11:54:09 -04001887 int have_ecc_cert = 0, ecdsa_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001888 X509 *x;
Adam Langley95c29f32014-06-20 12:00:00 -07001889
Adam Langleyfcf25832014-12-18 17:42:32 -08001890 dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001891
nagendra modadugu601448a2015-07-24 09:31:31 -07001892 if (s->cert->x509 != NULL && ssl_has_private_key(s)) {
1893 if (ssl_private_key_type(s) == EVP_PKEY_RSA) {
David Benjamind1d80782015-07-05 11:54:09 -04001894 have_rsa_cert = 1;
nagendra modadugu601448a2015-07-24 09:31:31 -07001895 } else if (ssl_private_key_type(s) == EVP_PKEY_EC) {
David Benjamind1d80782015-07-05 11:54:09 -04001896 have_ecc_cert = 1;
1897 }
1898 }
nagendra modadugu601448a2015-07-24 09:31:31 -07001899
Adam Langleyfcf25832014-12-18 17:42:32 -08001900 mask_k = 0;
1901 mask_a = 0;
David Benjaminf31e6812014-11-13 18:05:55 -05001902
Adam Langleyfcf25832014-12-18 17:42:32 -08001903 if (dh_tmp) {
David Benjamin7061e282015-03-19 11:10:48 -04001904 mask_k |= SSL_kDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001905 }
David Benjaminbb20f522015-07-04 17:18:14 -04001906 if (have_rsa_cert) {
1907 mask_k |= SSL_kRSA;
Adam Langleyfcf25832014-12-18 17:42:32 -08001908 mask_a |= SSL_aRSA;
1909 }
Adam Langley95c29f32014-06-20 12:00:00 -07001910
Adam Langleyfcf25832014-12-18 17:42:32 -08001911 /* An ECC certificate may be usable for ECDSA cipher suites depending on the
1912 * key usage extension and on the client's curve preferences. */
1913 if (have_ecc_cert) {
David Benjamind1d80782015-07-05 11:54:09 -04001914 x = c->x509;
Adam Langleyfcf25832014-12-18 17:42:32 -08001915 /* This call populates extension flags (ex_flags). */
1916 X509_check_purpose(x, -1, 0);
1917 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE)
1918 ? (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)
1919 : 1;
1920 if (!tls1_check_ec_cert(s, x)) {
1921 ecdsa_ok = 0;
1922 }
1923 if (ecdsa_ok) {
1924 mask_a |= SSL_aECDSA;
1925 }
1926 }
Adam Langley95c29f32014-06-20 12:00:00 -07001927
Adam Langleyfcf25832014-12-18 17:42:32 -08001928 /* If we are considering an ECC cipher suite that uses an ephemeral EC
1929 * key, check it. */
David Benjamindd978782015-04-24 15:20:13 -04001930 if (tls1_check_ec_tmp_key(s)) {
David Benjamin7061e282015-03-19 11:10:48 -04001931 mask_k |= SSL_kECDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001932 }
Adam Langley95c29f32014-06-20 12:00:00 -07001933
Adam Langleyfcf25832014-12-18 17:42:32 -08001934 /* PSK requires a server callback. */
1935 if (s->psk_server_callback != NULL) {
1936 mask_k |= SSL_kPSK;
1937 mask_a |= SSL_aPSK;
1938 }
Adam Langley95c29f32014-06-20 12:00:00 -07001939
Adam Langleyfcf25832014-12-18 17:42:32 -08001940 *out_mask_k = mask_k;
1941 *out_mask_a = mask_a;
1942}
Adam Langley95c29f32014-06-20 12:00:00 -07001943
Adam Langleyfcf25832014-12-18 17:42:32 -08001944void ssl_update_cache(SSL *s, int mode) {
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001945 /* Never cache sessions with empty session IDs. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001946 if (s->session->session_id_length == 0) {
1947 return;
1948 }
Adam Langley95c29f32014-06-20 12:00:00 -07001949
David Benjamin95d31822015-06-15 19:53:32 -04001950 int has_new_session = !s->hit;
1951 if (!s->server && s->tlsext_ticket_expected) {
1952 /* A client may see new sessions on abbreviated handshakes if the server
1953 * decides to renew the ticket. Once the handshake is completed, it should
1954 * be inserted into the cache. */
1955 has_new_session = 1;
1956 }
1957
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001958 SSL_CTX *ctx = s->initial_ctx;
David Benjamin95d31822015-06-15 19:53:32 -04001959 if ((ctx->session_cache_mode & mode) == mode && has_new_session &&
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001960 ((ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) ||
1961 SSL_CTX_add_session(ctx, s->session)) &&
1962 ctx->new_session_cb != NULL) {
1963 /* Note: |new_session_cb| is called whether the internal session cache is
1964 * used or not. */
1965 if (!ctx->new_session_cb(s, SSL_SESSION_up_ref(s->session))) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001966 SSL_SESSION_free(s->session);
1967 }
1968 }
Adam Langley95c29f32014-06-20 12:00:00 -07001969
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001970 if (!(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR) &&
1971 !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) &&
1972 (ctx->session_cache_mode & mode) == mode) {
1973 /* Automatically flush the internal session cache every 255 connections. */
1974 int flush_cache = 0;
Adam Langley4bdb6e42015-05-15 15:29:21 -07001975 CRYPTO_MUTEX_lock_write(&ctx->lock);
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001976 ctx->handshakes_since_cache_flush++;
1977 if (ctx->handshakes_since_cache_flush >= 255) {
1978 flush_cache = 1;
1979 ctx->handshakes_since_cache_flush = 0;
1980 }
Adam Langley4bdb6e42015-05-15 15:29:21 -07001981 CRYPTO_MUTEX_unlock(&ctx->lock);
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001982
1983 if (flush_cache) {
1984 SSL_CTX_flush_sessions(ctx, (unsigned long)time(NULL));
Adam Langleyfcf25832014-12-18 17:42:32 -08001985 }
1986 }
1987}
Adam Langley95c29f32014-06-20 12:00:00 -07001988
Adam Langleyfcf25832014-12-18 17:42:32 -08001989static const char *ssl_get_version(int version) {
1990 switch (version) {
1991 case TLS1_2_VERSION:
1992 return "TLSv1.2";
Adam Langley95c29f32014-06-20 12:00:00 -07001993
Adam Langleyfcf25832014-12-18 17:42:32 -08001994 case TLS1_1_VERSION:
1995 return "TLSv1.1";
Adam Langley95c29f32014-06-20 12:00:00 -07001996
Adam Langleyfcf25832014-12-18 17:42:32 -08001997 case TLS1_VERSION:
1998 return "TLSv1";
Adam Langley95c29f32014-06-20 12:00:00 -07001999
Adam Langleyfcf25832014-12-18 17:42:32 -08002000 case SSL3_VERSION:
2001 return "SSLv3";
Adam Langley95c29f32014-06-20 12:00:00 -07002002
David Benjamin1c722b72015-04-20 13:53:10 -04002003 case DTLS1_VERSION:
2004 return "DTLSv1";
2005
2006 case DTLS1_2_VERSION:
2007 return "DTLSv1.2";
2008
Adam Langleyfcf25832014-12-18 17:42:32 -08002009 default:
2010 return "unknown";
2011 }
2012}
Adam Langley95c29f32014-06-20 12:00:00 -07002013
Adam Langleyfcf25832014-12-18 17:42:32 -08002014const char *SSL_get_version(const SSL *s) {
2015 return ssl_get_version(s->version);
2016}
Adam Langley95c29f32014-06-20 12:00:00 -07002017
Adam Langleyfcf25832014-12-18 17:42:32 -08002018const char *SSL_SESSION_get_version(const SSL_SESSION *sess) {
2019 return ssl_get_version(sess->ssl_version);
2020}
Adam Langley95c29f32014-06-20 12:00:00 -07002021
Sigbjorn Vik2b23d242015-06-29 15:07:26 +02002022const char* SSL_get_curve_name(uint16_t curve_id) {
2023 return tls1_ec_curve_id2name(curve_id);
2024}
2025
Adam Langleyfcf25832014-12-18 17:42:32 -08002026void ssl_clear_cipher_ctx(SSL *s) {
David Benjamin31a07792015-03-03 14:20:26 -05002027 SSL_AEAD_CTX_free(s->aead_read_ctx);
2028 s->aead_read_ctx = NULL;
2029 SSL_AEAD_CTX_free(s->aead_write_ctx);
2030 s->aead_write_ctx = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002031}
Adam Langley95c29f32014-06-20 12:00:00 -07002032
Adam Langleyfcf25832014-12-18 17:42:32 -08002033X509 *SSL_get_certificate(const SSL *s) {
2034 if (s->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04002035 return s->cert->x509;
Adam Langleyfcf25832014-12-18 17:42:32 -08002036 }
2037
2038 return NULL;
2039}
2040
2041EVP_PKEY *SSL_get_privatekey(const SSL *s) {
2042 if (s->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04002043 return s->cert->privatekey;
Adam Langleyfcf25832014-12-18 17:42:32 -08002044 }
2045
2046 return NULL;
2047}
2048
2049X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
2050 if (ctx->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04002051 return ctx->cert->x509;
Adam Langleyfcf25832014-12-18 17:42:32 -08002052 }
2053
2054 return NULL;
2055}
2056
2057EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
2058 if (ctx->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04002059 return ctx->cert->privatekey;
Adam Langleyfcf25832014-12-18 17:42:32 -08002060 }
2061
2062 return NULL;
2063}
2064
2065const SSL_CIPHER *SSL_get_current_cipher(const SSL *s) {
David Benjamina07c0fc2015-05-13 13:19:42 -04002066 if (s->aead_write_ctx == NULL) {
2067 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002068 }
David Benjamina07c0fc2015-05-13 13:19:42 -04002069 return s->aead_write_ctx->cipher;
Adam Langleyfcf25832014-12-18 17:42:32 -08002070}
2071
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07002072const COMP_METHOD *SSL_get_current_compression(SSL *s) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002073
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07002074const COMP_METHOD *SSL_get_current_expansion(SSL *s) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002075
2076int ssl_init_wbio_buffer(SSL *s, int push) {
2077 BIO *bbio;
2078
2079 if (s->bbio == NULL) {
2080 bbio = BIO_new(BIO_f_buffer());
2081 if (bbio == NULL) {
2082 return 0;
2083 }
2084 s->bbio = bbio;
2085 } else {
2086 bbio = s->bbio;
2087 if (s->bbio == s->wbio) {
2088 s->wbio = BIO_pop(s->wbio);
2089 }
2090 }
2091
2092 BIO_reset(bbio);
2093 if (!BIO_set_read_buffer_size(bbio, 1)) {
David Benjamin3570d732015-06-29 00:28:17 -04002094 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -08002095 return 0;
2096 }
2097
2098 if (push) {
2099 if (s->wbio != bbio) {
2100 s->wbio = BIO_push(bbio, s->wbio);
2101 }
2102 } else {
2103 if (s->wbio == bbio) {
2104 s->wbio = BIO_pop(bbio);
2105 }
2106 }
2107
2108 return 1;
2109}
2110
2111void ssl_free_wbio_buffer(SSL *s) {
2112 if (s->bbio == NULL) {
2113 return;
2114 }
2115
2116 if (s->bbio == s->wbio) {
2117 /* remove buffering */
2118 s->wbio = BIO_pop(s->wbio);
2119 }
2120
2121 BIO_free(s->bbio);
2122 s->bbio = NULL;
2123}
2124
2125void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
2126 ctx->quiet_shutdown = mode;
2127}
2128
2129int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
2130 return ctx->quiet_shutdown;
2131}
2132
2133void SSL_set_quiet_shutdown(SSL *s, int mode) { s->quiet_shutdown = mode; }
2134
2135int SSL_get_quiet_shutdown(const SSL *s) { return s->quiet_shutdown; }
2136
2137void SSL_set_shutdown(SSL *s, int mode) { s->shutdown = mode; }
2138
2139int SSL_get_shutdown(const SSL *s) { return s->shutdown; }
2140
2141int SSL_version(const SSL *s) { return s->version; }
2142
2143SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
2144
2145SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
2146 if (ssl->ctx == ctx) {
2147 return ssl->ctx;
2148 }
2149
2150 if (ctx == NULL) {
2151 ctx = ssl->initial_ctx;
2152 }
2153
David Benjamin2755a3e2015-04-22 16:17:58 -04002154 ssl_cert_free(ssl->cert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002155 ssl->cert = ssl_cert_dup(ctx->cert);
David Benjamin2755a3e2015-04-22 16:17:58 -04002156
Adam Langley0b5e3902015-05-15 13:08:38 -07002157 CRYPTO_refcount_inc(&ctx->references);
David Benjamin2755a3e2015-04-22 16:17:58 -04002158 SSL_CTX_free(ssl->ctx); /* decrement reference count */
Adam Langleyfcf25832014-12-18 17:42:32 -08002159 ssl->ctx = ctx;
2160
2161 ssl->sid_ctx_length = ctx->sid_ctx_length;
2162 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2163 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2164
2165 return ssl->ctx;
2166}
2167
2168int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
2169 return X509_STORE_set_default_paths(ctx->cert_store);
2170}
Adam Langley95c29f32014-06-20 12:00:00 -07002171
2172int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
Adam Langleyfcf25832014-12-18 17:42:32 -08002173 const char *CApath) {
2174 return X509_STORE_load_locations(ctx->cert_store, CAfile, CApath);
2175}
Adam Langley95c29f32014-06-20 12:00:00 -07002176
2177void SSL_set_info_callback(SSL *ssl,
Adam Langleyfcf25832014-12-18 17:42:32 -08002178 void (*cb)(const SSL *ssl, int type, int val)) {
2179 ssl->info_callback = cb;
2180}
Adam Langley95c29f32014-06-20 12:00:00 -07002181
Adam Langleyfcf25832014-12-18 17:42:32 -08002182void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/, int /*type*/,
2183 int /*val*/) {
2184 return ssl->info_callback;
2185}
Adam Langley95c29f32014-06-20 12:00:00 -07002186
Adam Langleyfcf25832014-12-18 17:42:32 -08002187int SSL_state(const SSL *ssl) { return ssl->state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002188
David Benjaminece089c2015-05-15 23:52:42 -04002189void SSL_set_state(SSL *ssl, int state) { }
Adam Langley95c29f32014-06-20 12:00:00 -07002190
Adam Langleyfcf25832014-12-18 17:42:32 -08002191void SSL_set_verify_result(SSL *ssl, long arg) { ssl->verify_result = arg; }
Adam Langley95c29f32014-06-20 12:00:00 -07002192
Adam Langleyfcf25832014-12-18 17:42:32 -08002193long SSL_get_verify_result(const SSL *ssl) { return ssl->verify_result; }
Adam Langley95c29f32014-06-20 12:00:00 -07002194
Adam Langleyfcf25832014-12-18 17:42:32 -08002195int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2196 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002197 int index;
2198 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp,
2199 new_func, dup_func, free_func)) {
2200 return -1;
2201 }
2202 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002203}
Adam Langley95c29f32014-06-20 12:00:00 -07002204
Adam Langleyfcf25832014-12-18 17:42:32 -08002205int SSL_set_ex_data(SSL *s, int idx, void *arg) {
2206 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2207}
Adam Langley95c29f32014-06-20 12:00:00 -07002208
Adam Langleyfcf25832014-12-18 17:42:32 -08002209void *SSL_get_ex_data(const SSL *s, int idx) {
2210 return CRYPTO_get_ex_data(&s->ex_data, idx);
2211}
Adam Langley95c29f32014-06-20 12:00:00 -07002212
Adam Langleyfcf25832014-12-18 17:42:32 -08002213int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2214 CRYPTO_EX_dup *dup_func,
2215 CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002216 int index;
2217 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp,
2218 new_func, dup_func, free_func)) {
2219 return -1;
2220 }
2221 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002222}
Adam Langley95c29f32014-06-20 12:00:00 -07002223
Adam Langleyfcf25832014-12-18 17:42:32 -08002224int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg) {
2225 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2226}
Adam Langley95c29f32014-06-20 12:00:00 -07002227
Adam Langleyfcf25832014-12-18 17:42:32 -08002228void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx) {
2229 return CRYPTO_get_ex_data(&s->ex_data, idx);
2230}
Adam Langley95c29f32014-06-20 12:00:00 -07002231
Adam Langleyfcf25832014-12-18 17:42:32 -08002232X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
2233 return ctx->cert_store;
2234}
Adam Langley95c29f32014-06-20 12:00:00 -07002235
Adam Langleyfcf25832014-12-18 17:42:32 -08002236void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
David Benjamin2755a3e2015-04-22 16:17:58 -04002237 X509_STORE_free(ctx->cert_store);
Adam Langleyfcf25832014-12-18 17:42:32 -08002238 ctx->cert_store = store;
2239}
Adam Langley95c29f32014-06-20 12:00:00 -07002240
Adam Langleyfcf25832014-12-18 17:42:32 -08002241int SSL_want(const SSL *s) { return s->rwstate; }
Adam Langley95c29f32014-06-20 12:00:00 -07002242
Adam Langleyfcf25832014-12-18 17:42:32 -08002243void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
2244 RSA *(*cb)(SSL *ssl, int is_export,
2245 int keylength)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002246}
Adam Langley95c29f32014-06-20 12:00:00 -07002247
Adam Langleyfcf25832014-12-18 17:42:32 -08002248void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
2249 int keylength)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002250}
2251
2252void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
David Benjamin59015c32015-04-26 13:13:55 -04002253 DH *(*callback)(SSL *ssl, int is_export,
2254 int keylength)) {
2255 ctx->cert->dh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002256}
2257
David Benjamin59015c32015-04-26 13:13:55 -04002258void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*callback)(SSL *ssl, int is_export,
2259 int keylength)) {
2260 ssl->cert->dh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002261}
2262
2263void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,
David Benjamindd978782015-04-24 15:20:13 -04002264 EC_KEY *(*callback)(SSL *ssl, int is_export,
2265 int keylength)) {
David Benjamin59015c32015-04-26 13:13:55 -04002266 ctx->cert->ecdh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002267}
2268
2269void SSL_set_tmp_ecdh_callback(SSL *ssl,
David Benjamindd978782015-04-24 15:20:13 -04002270 EC_KEY *(*callback)(SSL *ssl, int is_export,
2271 int keylength)) {
David Benjamin59015c32015-04-26 13:13:55 -04002272 ssl->cert->ecdh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002273}
2274
2275int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
2276 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
David Benjamin3570d732015-06-29 00:28:17 -04002277 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -08002278 return 0;
2279 }
2280
David Benjamin2755a3e2015-04-22 16:17:58 -04002281 OPENSSL_free(ctx->psk_identity_hint);
Adam Langleyfcf25832014-12-18 17:42:32 -08002282
2283 if (identity_hint != NULL) {
2284 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2285 if (ctx->psk_identity_hint == NULL) {
2286 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002287 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002288 } else {
2289 ctx->psk_identity_hint = NULL;
2290 }
Adam Langley95c29f32014-06-20 12:00:00 -07002291
Adam Langleyfcf25832014-12-18 17:42:32 -08002292 return 1;
2293}
2294
2295int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint) {
2296 if (s == NULL) {
2297 return 0;
2298 }
2299
2300 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
David Benjamin3570d732015-06-29 00:28:17 -04002301 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -08002302 return 0;
2303 }
2304
2305 /* Clear currently configured hint, if any. */
David Benjamin2755a3e2015-04-22 16:17:58 -04002306 OPENSSL_free(s->psk_identity_hint);
2307 s->psk_identity_hint = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002308
2309 if (identity_hint != NULL) {
2310 s->psk_identity_hint = BUF_strdup(identity_hint);
2311 if (s->psk_identity_hint == NULL) {
2312 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002313 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002314 }
Adam Langley95c29f32014-06-20 12:00:00 -07002315
Adam Langleyfcf25832014-12-18 17:42:32 -08002316 return 1;
2317}
Adam Langley95c29f32014-06-20 12:00:00 -07002318
Adam Langleyfcf25832014-12-18 17:42:32 -08002319const char *SSL_get_psk_identity_hint(const SSL *s) {
2320 if (s == NULL) {
2321 return NULL;
2322 }
2323 return s->psk_identity_hint;
2324}
Adam Langley95c29f32014-06-20 12:00:00 -07002325
Adam Langleyfcf25832014-12-18 17:42:32 -08002326const char *SSL_get_psk_identity(const SSL *s) {
2327 if (s == NULL || s->session == NULL) {
2328 return NULL;
2329 }
Adam Langley95c29f32014-06-20 12:00:00 -07002330
Adam Langleyfcf25832014-12-18 17:42:32 -08002331 return s->session->psk_identity;
2332}
Adam Langley95c29f32014-06-20 12:00:00 -07002333
Adam Langleyfcf25832014-12-18 17:42:32 -08002334void SSL_set_psk_client_callback(
2335 SSL *s, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2336 unsigned int max_identity_len, uint8_t *psk,
2337 unsigned int max_psk_len)) {
2338 s->psk_client_callback = cb;
2339}
Adam Langley95c29f32014-06-20 12:00:00 -07002340
Adam Langleyfcf25832014-12-18 17:42:32 -08002341void SSL_CTX_set_psk_client_callback(
2342 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2343 unsigned int max_identity_len,
2344 uint8_t *psk, unsigned int max_psk_len)) {
2345 ctx->psk_client_callback = cb;
2346}
Adam Langley95c29f32014-06-20 12:00:00 -07002347
Adam Langleyfcf25832014-12-18 17:42:32 -08002348void SSL_set_psk_server_callback(
2349 SSL *s, unsigned int (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
2350 unsigned int max_psk_len)) {
2351 s->psk_server_callback = cb;
2352}
Adam Langley95c29f32014-06-20 12:00:00 -07002353
Adam Langleyfcf25832014-12-18 17:42:32 -08002354void SSL_CTX_set_psk_server_callback(
2355 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *identity,
2356 uint8_t *psk, unsigned int max_psk_len)) {
2357 ctx->psk_server_callback = cb;
2358}
Adam Langley95c29f32014-06-20 12:00:00 -07002359
Adam Langleyfcf25832014-12-18 17:42:32 -08002360void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
2361 void (*cb)(int write_p, int version,
2362 int content_type, const void *buf,
2363 size_t len, SSL *ssl, void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002364 ctx->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002365}
David Benjamin61ecccf2015-05-05 09:44:51 -04002366
2367void SSL_CTX_set_msg_callback_arg(SSL_CTX *ctx, void *arg) {
2368 ctx->msg_callback_arg = arg;
2369}
2370
Adam Langleyfcf25832014-12-18 17:42:32 -08002371void SSL_set_msg_callback(SSL *ssl,
2372 void (*cb)(int write_p, int version, int content_type,
2373 const void *buf, size_t len, SSL *ssl,
2374 void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002375 ssl->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002376}
Adam Langley95c29f32014-06-20 12:00:00 -07002377
David Benjamin61ecccf2015-05-05 09:44:51 -04002378void SSL_set_msg_callback_arg(SSL *ssl, void *arg) {
2379 ssl->msg_callback_arg = arg;
2380}
2381
Adam Langleyfcf25832014-12-18 17:42:32 -08002382void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio) {
David Benjamin2755a3e2015-04-22 16:17:58 -04002383 BIO_free(ctx->keylog_bio);
Adam Langleyfcf25832014-12-18 17:42:32 -08002384 ctx->keylog_bio = keylog_bio;
2385}
Adam Langley95c29f32014-06-20 12:00:00 -07002386
Adam Langleyfcf25832014-12-18 17:42:32 -08002387static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
2388 static const char hextable[] = "0123456789abcdef";
2389 uint8_t *out;
2390 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002391
Adam Langleyfcf25832014-12-18 17:42:32 -08002392 if (!CBB_add_space(cbb, &out, in_len * 2)) {
2393 return 0;
2394 }
Adam Langley95c29f32014-06-20 12:00:00 -07002395
Adam Langleyfcf25832014-12-18 17:42:32 -08002396 for (i = 0; i < in_len; i++) {
2397 *(out++) = (uint8_t)hextable[in[i] >> 4];
2398 *(out++) = (uint8_t)hextable[in[i] & 0xf];
2399 }
Adam Langley95c29f32014-06-20 12:00:00 -07002400
Adam Langleyfcf25832014-12-18 17:42:32 -08002401 return 1;
2402}
David Benjamin859ec3c2014-09-02 16:29:36 -04002403
2404int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -08002405 const uint8_t *encrypted_premaster,
2406 size_t encrypted_premaster_len,
2407 const uint8_t *premaster,
2408 size_t premaster_len) {
2409 BIO *bio = ctx->keylog_bio;
2410 CBB cbb;
2411 uint8_t *out;
2412 size_t out_len;
2413 int ret;
David Benjamin859ec3c2014-09-02 16:29:36 -04002414
Adam Langleyfcf25832014-12-18 17:42:32 -08002415 if (bio == NULL) {
2416 return 1;
2417 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002418
Adam Langleyfcf25832014-12-18 17:42:32 -08002419 if (encrypted_premaster_len < 8) {
David Benjamin3570d732015-06-29 00:28:17 -04002420 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08002421 return 0;
2422 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002423
David Benjamina8653202015-06-28 01:26:10 -04002424 CBB_zero(&cbb);
2425 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len * 2 + 1) ||
2426 !CBB_add_bytes(&cbb, (const uint8_t *)"RSA ", 4) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08002427 /* Only the first 8 bytes of the encrypted premaster secret are
2428 * logged. */
2429 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
2430 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2431 !cbb_add_hex(&cbb, premaster, premaster_len) ||
2432 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2433 !CBB_finish(&cbb, &out, &out_len)) {
2434 CBB_cleanup(&cbb);
2435 return 0;
2436 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002437
Adam Langley4bdb6e42015-05-15 15:29:21 -07002438 CRYPTO_MUTEX_lock_write(&ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -08002439 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
Adam Langley4bdb6e42015-05-15 15:29:21 -07002440 CRYPTO_MUTEX_unlock(&ctx->lock);
David Benjamin859ec3c2014-09-02 16:29:36 -04002441
Adam Langleyfcf25832014-12-18 17:42:32 -08002442 OPENSSL_free(out);
2443 return ret;
Adam Langley95f22882014-06-20 12:00:00 -07002444}
2445
Adam Langleyfcf25832014-12-18 17:42:32 -08002446int ssl_ctx_log_master_secret(SSL_CTX *ctx, const uint8_t *client_random,
2447 size_t client_random_len, const uint8_t *master,
2448 size_t master_len) {
2449 BIO *bio = ctx->keylog_bio;
2450 CBB cbb;
2451 uint8_t *out;
2452 size_t out_len;
2453 int ret;
Adam Langleyadb739e2014-06-20 12:00:00 -07002454
Adam Langleyfcf25832014-12-18 17:42:32 -08002455 if (bio == NULL) {
2456 return 1;
2457 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002458
Adam Langleyfcf25832014-12-18 17:42:32 -08002459 if (client_random_len != 32) {
David Benjamin3570d732015-06-29 00:28:17 -04002460 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08002461 return 0;
2462 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002463
David Benjamina8653202015-06-28 01:26:10 -04002464 CBB_zero(&cbb);
2465 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len * 2 + 1) ||
2466 !CBB_add_bytes(&cbb, (const uint8_t *)"CLIENT_RANDOM ", 14) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08002467 !cbb_add_hex(&cbb, client_random, 32) ||
2468 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2469 !cbb_add_hex(&cbb, master, master_len) ||
2470 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2471 !CBB_finish(&cbb, &out, &out_len)) {
2472 CBB_cleanup(&cbb);
2473 return 0;
2474 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002475
Adam Langley4bdb6e42015-05-15 15:29:21 -07002476 CRYPTO_MUTEX_lock_write(&ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -08002477 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
Adam Langley4bdb6e42015-05-15 15:29:21 -07002478 CRYPTO_MUTEX_unlock(&ctx->lock);
David Benjamine99e9122014-12-11 01:46:01 -05002479
Adam Langleyfcf25832014-12-18 17:42:32 -08002480 OPENSSL_free(out);
2481 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -07002482}
2483
David Benjamined7c4752015-02-16 19:16:46 -05002484int SSL_in_false_start(const SSL *s) {
2485 return s->s3->tmp.in_false_start;
2486}
2487
Adam Langleyfcf25832014-12-18 17:42:32 -08002488int SSL_cutthrough_complete(const SSL *s) {
David Benjamined7c4752015-02-16 19:16:46 -05002489 return SSL_in_false_start(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002490}
Adam Langley95c29f32014-06-20 12:00:00 -07002491
Adam Langleyfcf25832014-12-18 17:42:32 -08002492void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
2493 size_t *ssl_session_size) {
2494 *ssl_size = sizeof(SSL);
2495 *ssl_ctx_size = sizeof(SSL_CTX);
2496 *ssl_session_size = sizeof(SSL_SESSION);
2497}
Feng Lu41aa3252014-11-21 22:47:56 -08002498
David Benjamined7c4752015-02-16 19:16:46 -05002499int ssl3_can_false_start(const SSL *s) {
David Benjamin195dc782015-02-19 13:27:05 -05002500 const SSL_CIPHER *const cipher = SSL_get_current_cipher(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002501
Adam Langleye631d962015-04-01 13:11:01 -07002502 /* False Start only for TLS 1.2 with an ECDHE+AEAD cipher and ALPN or NPN. */
David Benjamin195dc782015-02-19 13:27:05 -05002503 return !SSL_IS_DTLS(s) &&
2504 SSL_version(s) >= TLS1_2_VERSION &&
2505 (s->s3->alpn_selected || s->s3->next_proto_neg_seen) &&
2506 cipher != NULL &&
Adam Langleye631d962015-04-01 13:11:01 -07002507 cipher->algorithm_mkey == SSL_kECDHE &&
David Benjamin195dc782015-02-19 13:27:05 -05002508 (cipher->algorithm_enc == SSL_AES128GCM ||
2509 cipher->algorithm_enc == SSL_AES256GCM ||
2510 cipher->algorithm_enc == SSL_CHACHA20POLY1305);
Adam Langleyfcf25832014-12-18 17:42:32 -08002511}
2512
2513const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version) {
2514 switch (version) {
2515 case SSL3_VERSION:
2516 return &SSLv3_enc_data;
2517
2518 case TLS1_VERSION:
2519 return &TLSv1_enc_data;
2520
David Benjamin9e13e1a2015-03-05 01:56:32 -05002521 case DTLS1_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002522 case TLS1_1_VERSION:
2523 return &TLSv1_1_enc_data;
2524
David Benjamin9e13e1a2015-03-05 01:56:32 -05002525 case DTLS1_2_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002526 case TLS1_2_VERSION:
2527 return &TLSv1_2_enc_data;
2528
Adam Langleyfcf25832014-12-18 17:42:32 -08002529 default:
2530 return NULL;
2531 }
2532}
2533
2534uint16_t ssl3_get_max_server_version(const SSL *s) {
2535 uint16_t max_version;
2536
2537 if (SSL_IS_DTLS(s)) {
2538 max_version = (s->max_version != 0) ? s->max_version : DTLS1_2_VERSION;
2539 if (!(s->options & SSL_OP_NO_DTLSv1_2) && DTLS1_2_VERSION >= max_version) {
2540 return DTLS1_2_VERSION;
2541 }
2542 if (!(s->options & SSL_OP_NO_DTLSv1) && DTLS1_VERSION >= max_version) {
2543 return DTLS1_VERSION;
2544 }
2545 return 0;
2546 }
2547
2548 max_version = (s->max_version != 0) ? s->max_version : TLS1_2_VERSION;
2549 if (!(s->options & SSL_OP_NO_TLSv1_2) && TLS1_2_VERSION <= max_version) {
2550 return TLS1_2_VERSION;
2551 }
2552 if (!(s->options & SSL_OP_NO_TLSv1_1) && TLS1_1_VERSION <= max_version) {
2553 return TLS1_1_VERSION;
2554 }
2555 if (!(s->options & SSL_OP_NO_TLSv1) && TLS1_VERSION <= max_version) {
2556 return TLS1_VERSION;
2557 }
2558 if (!(s->options & SSL_OP_NO_SSLv3) && SSL3_VERSION <= max_version) {
2559 return SSL3_VERSION;
2560 }
2561 return 0;
2562}
2563
2564uint16_t ssl3_get_mutual_version(SSL *s, uint16_t client_version) {
2565 uint16_t version = 0;
2566
2567 if (SSL_IS_DTLS(s)) {
2568 /* Clamp client_version to max_version. */
2569 if (s->max_version != 0 && client_version < s->max_version) {
2570 client_version = s->max_version;
2571 }
2572
2573 if (client_version <= DTLS1_2_VERSION && !(s->options & SSL_OP_NO_DTLSv1_2)) {
2574 version = DTLS1_2_VERSION;
2575 } else if (client_version <= DTLS1_VERSION &&
David Benjaminb18f0242015-03-10 18:30:08 -04002576 !(s->options & SSL_OP_NO_DTLSv1)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002577 version = DTLS1_VERSION;
2578 }
2579
2580 /* Check against min_version. */
2581 if (version != 0 && s->min_version != 0 && version > s->min_version) {
2582 return 0;
2583 }
2584 return version;
2585 } else {
2586 /* Clamp client_version to max_version. */
2587 if (s->max_version != 0 && client_version > s->max_version) {
2588 client_version = s->max_version;
2589 }
2590
2591 if (client_version >= TLS1_2_VERSION && !(s->options & SSL_OP_NO_TLSv1_2)) {
2592 version = TLS1_2_VERSION;
2593 } else if (client_version >= TLS1_1_VERSION &&
2594 !(s->options & SSL_OP_NO_TLSv1_1)) {
2595 version = TLS1_1_VERSION;
2596 } else if (client_version >= TLS1_VERSION && !(s->options & SSL_OP_NO_TLSv1)) {
2597 version = TLS1_VERSION;
2598 } else if (client_version >= SSL3_VERSION && !(s->options & SSL_OP_NO_SSLv3)) {
2599 version = SSL3_VERSION;
2600 }
2601
2602 /* Check against min_version. */
2603 if (version != 0 && s->min_version != 0 && version < s->min_version) {
2604 return 0;
2605 }
2606 return version;
2607 }
2608}
2609
2610uint16_t ssl3_get_max_client_version(SSL *s) {
David Benjamin123a8fd2015-04-26 14:16:41 -04002611 uint32_t options = s->options;
Adam Langleyfcf25832014-12-18 17:42:32 -08002612 uint16_t version = 0;
2613
2614 /* OpenSSL's API for controlling versions entails blacklisting individual
2615 * protocols. This has two problems. First, on the client, the protocol can
2616 * only express a contiguous range of versions. Second, a library consumer
2617 * trying to set a maximum version cannot disable protocol versions that get
2618 * added in a future version of the library.
2619 *
2620 * To account for both of these, OpenSSL interprets the client-side bitmask
2621 * as a min/max range by picking the lowest contiguous non-empty range of
2622 * enabled protocols. Note that this means it is impossible to set a maximum
2623 * version of TLS 1.2 in a future-proof way.
2624 *
2625 * By this scheme, the maximum version is the lowest version V such that V is
2626 * enabled and V+1 is disabled or unimplemented. */
2627 if (SSL_IS_DTLS(s)) {
2628 if (!(options & SSL_OP_NO_DTLSv1_2)) {
2629 version = DTLS1_2_VERSION;
2630 }
2631 if (!(options & SSL_OP_NO_DTLSv1) && (options & SSL_OP_NO_DTLSv1_2)) {
2632 version = DTLS1_VERSION;
2633 }
2634 if (s->max_version != 0 && version < s->max_version) {
2635 version = s->max_version;
2636 }
2637 } else {
2638 if (!(options & SSL_OP_NO_TLSv1_2)) {
2639 version = TLS1_2_VERSION;
2640 }
2641 if (!(options & SSL_OP_NO_TLSv1_1) && (options & SSL_OP_NO_TLSv1_2)) {
2642 version = TLS1_1_VERSION;
2643 }
2644 if (!(options & SSL_OP_NO_TLSv1) && (options & SSL_OP_NO_TLSv1_1)) {
2645 version = TLS1_VERSION;
2646 }
2647 if (!(options & SSL_OP_NO_SSLv3) && (options & SSL_OP_NO_TLSv1)) {
2648 version = SSL3_VERSION;
2649 }
2650 if (s->max_version != 0 && version > s->max_version) {
2651 version = s->max_version;
2652 }
2653 }
2654
2655 return version;
2656}
2657
2658int ssl3_is_version_enabled(SSL *s, uint16_t version) {
2659 if (SSL_IS_DTLS(s)) {
2660 if (s->max_version != 0 && version < s->max_version) {
2661 return 0;
2662 }
2663 if (s->min_version != 0 && version > s->min_version) {
2664 return 0;
2665 }
2666
2667 switch (version) {
2668 case DTLS1_VERSION:
2669 return !(s->options & SSL_OP_NO_DTLSv1);
2670
2671 case DTLS1_2_VERSION:
2672 return !(s->options & SSL_OP_NO_DTLSv1_2);
2673
2674 default:
2675 return 0;
2676 }
2677 } else {
2678 if (s->max_version != 0 && version > s->max_version) {
2679 return 0;
2680 }
2681 if (s->min_version != 0 && version < s->min_version) {
2682 return 0;
2683 }
2684
2685 switch (version) {
2686 case SSL3_VERSION:
2687 return !(s->options & SSL_OP_NO_SSLv3);
2688
2689 case TLS1_VERSION:
2690 return !(s->options & SSL_OP_NO_TLSv1);
2691
2692 case TLS1_1_VERSION:
2693 return !(s->options & SSL_OP_NO_TLSv1_1);
2694
2695 case TLS1_2_VERSION:
2696 return !(s->options & SSL_OP_NO_TLSv1_2);
2697
2698 default:
2699 return 0;
2700 }
2701 }
2702}
2703
David Benjaminea72bd02014-12-21 21:27:41 -05002704uint16_t ssl3_version_from_wire(SSL *s, uint16_t wire_version) {
2705 if (!SSL_IS_DTLS(s)) {
2706 return wire_version;
2707 }
2708
2709 uint16_t tls_version = ~wire_version;
2710 uint16_t version = tls_version + 0x0201;
2711 /* If either component overflowed, clamp it so comparisons still work. */
2712 if ((version >> 8) < (tls_version >> 8)) {
2713 version = 0xff00 | (version & 0xff);
2714 }
2715 if ((version & 0xff) < (tls_version & 0xff)) {
2716 version = (version & 0xff00) | 0xff;
2717 }
2718 /* DTLS 1.0 maps to TLS 1.1, not TLS 1.0. */
2719 if (version == TLS1_VERSION) {
2720 version = TLS1_1_VERSION;
2721 }
2722 return version;
2723}
2724
Adam Langleyfcf25832014-12-18 17:42:32 -08002725int SSL_cache_hit(SSL *s) { return s->hit; }
2726
2727int SSL_is_server(SSL *s) { return s->server; }
2728
Adam Langley524e7172015-02-20 16:04:00 -08002729void SSL_CTX_set_dos_protection_cb(
2730 SSL_CTX *ctx, int (*cb)(const struct ssl_early_callback_ctx *)) {
2731 ctx->dos_protection_cb = cb;
2732}
2733
David Benjaminb16346b2015-04-08 19:16:58 -04002734void SSL_set_reject_peer_renegotiations(SSL *s, int reject) {
David Benjamin897e5e02015-05-12 17:03:54 -04002735 s->accept_peer_renegotiations = !reject;
David Benjaminb16346b2015-04-08 19:16:58 -04002736}
2737
Adam Langley3f92d212015-02-20 15:32:52 -08002738int SSL_get_rc4_state(const SSL *ssl, const RC4_KEY **read_key,
2739 const RC4_KEY **write_key) {
2740 if (ssl->aead_read_ctx == NULL || ssl->aead_write_ctx == NULL) {
2741 return 0;
2742 }
2743
2744 return EVP_AEAD_CTX_get_rc4_state(&ssl->aead_read_ctx->ctx, read_key) &&
2745 EVP_AEAD_CTX_get_rc4_state(&ssl->aead_write_ctx->ctx, write_key);
2746}
David Benjaminda881e92015-04-26 14:45:04 -04002747
Adam Langleyaf0e32c2015-06-03 09:57:23 -07002748int SSL_get_tls_unique(const SSL *ssl, uint8_t *out, size_t *out_len,
2749 size_t max_out) {
2750 /* The tls-unique value is the first Finished message in the handshake, which
2751 * is the client's in a full handshake and the server's for a resumption. See
2752 * https://tools.ietf.org/html/rfc5929#section-3.1. */
2753 const uint8_t *finished = ssl->s3->previous_client_finished;
2754 size_t finished_len = ssl->s3->previous_client_finished_len;
2755 if (ssl->hit) {
2756 /* tls-unique is broken for resumed sessions unless EMS is used. */
2757 if (!ssl->session->extended_master_secret) {
2758 goto err;
2759 }
2760 finished = ssl->s3->previous_server_finished;
2761 finished_len = ssl->s3->previous_server_finished_len;
2762 }
2763
2764 if (!ssl->s3->initial_handshake_complete ||
2765 ssl->version < TLS1_VERSION) {
2766 goto err;
2767 }
2768
2769 *out_len = finished_len;
2770 if (finished_len > max_out) {
2771 *out_len = max_out;
2772 }
2773
2774 memcpy(out, finished, *out_len);
2775 return 1;
2776
2777err:
2778 *out_len = 0;
2779 memset(out, 0, max_out);
2780 return 0;
2781}
2782
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) {}