blob: c65e607c1c7bb2f480eb6b5273454aaccb85426a [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
108 *
109 */
110/* ====================================================================
111 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
112 * ECC cipher suite support in OpenSSL originally developed by
113 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
114 */
115/* ====================================================================
116 * Copyright 2005 Nokia. All rights reserved.
117 *
118 * The portions of the attached software ("Contribution") is developed by
119 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
120 * license.
121 *
122 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
123 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
124 * support (see RFC 4279) to OpenSSL.
125 *
126 * No patent licenses or other rights except those expressly stated in
127 * the OpenSSL open source license shall be deemed granted or received
128 * expressly, by implication, estoppel, or otherwise.
129 *
130 * No assurances are provided by Nokia that the Contribution does not
131 * infringe the patent or other intellectual property rights of any third
132 * party or that the license provides you with all the necessary rights
133 * to make use of the Contribution.
134 *
135 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
136 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
137 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
138 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
139 * OTHERWISE. */
140
David Benjamin9e4e01e2015-09-15 01:48:04 -0400141#include <openssl/ssl.h>
142
Adam Langley95c29f32014-06-20 12:00:00 -0700143#include <assert.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400144#include <stdio.h>
145#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700146
David Benjamin39482a12014-07-20 13:30:15 -0400147#include <openssl/bytestring.h>
David Benjamin7c6d09b2015-09-05 11:00:19 -0400148#include <openssl/crypto.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700149#include <openssl/dh.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400150#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700151#include <openssl/lhash.h>
152#include <openssl/mem.h>
153#include <openssl/obj.h>
154#include <openssl/rand.h>
155#include <openssl/x509v3.h>
156
David Benjamin2ee94aa2015-04-07 22:38:30 -0400157#include "internal.h"
David Benjamin546f1a52015-04-15 16:46:09 -0400158#include "../crypto/internal.h"
159
Adam Langley95c29f32014-06-20 12:00:00 -0700160
David Benjamin76c2efc2015-08-31 14:24:29 -0400161/* |SSL_R_UNKNOWN_PROTOCOL| is no longer emitted, but continue to define it
David Benjamin97760d52015-07-24 23:02:49 -0400162 * to avoid downstream churn. */
David Benjamin76c2efc2015-08-31 14:24:29 -0400163OPENSSL_DECLARE_ERROR_REASON(SSL, UNKNOWN_PROTOCOL)
David Benjamin97760d52015-07-24 23:02:49 -0400164
Adam Langleyfcf25832014-12-18 17:42:32 -0800165/* Some error codes are special. Ensure the make_errors.go script never
166 * regresses this. */
167OPENSSL_COMPILE_ASSERT(SSL_R_TLSV1_ALERT_NO_RENEGOTIATION ==
168 SSL_AD_NO_RENEGOTIATION + SSL_AD_REASON_OFFSET,
169 ssl_alert_reason_code_mismatch);
David Benjamine1136082014-09-20 12:28:58 -0400170
David Benjamin1d0a1942015-04-26 15:35:35 -0400171/* kMaxHandshakeSize is the maximum size, in bytes, of a handshake message. */
172static const size_t kMaxHandshakeSize = (1u << 24) - 1;
173
David Benjaminaa585132015-06-29 23:36:17 -0400174static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl =
175 CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
176static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl_ctx =
177 CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
David Benjamin9f33fc62015-04-15 17:29:53 -0400178
David Benjamin7c6d09b2015-09-05 11:00:19 -0400179int SSL_library_init(void) {
180 CRYPTO_library_init();
181 return 1;
182}
183
David Benjamin27bbae42015-09-13 00:54:37 -0400184static uint32_t ssl_session_hash(const SSL_SESSION *a) {
185 uint32_t hash =
Adam Langleyf4e554e2015-09-30 13:46:34 -0700186 ((uint32_t)a->session_id[0]) |
187 ((uint32_t)a->session_id[1] << 8) |
188 ((uint32_t)a->session_id[2] << 16) |
David Benjamin27bbae42015-09-13 00:54:37 -0400189 ((uint32_t)a->session_id[3] << 24);
David Benjamin7c6d09b2015-09-05 11:00:19 -0400190
David Benjamin27bbae42015-09-13 00:54:37 -0400191 return hash;
192}
193
194/* NB: If this function (or indeed the hash function which uses a sort of
195 * coarser function than this one) is changed, ensure
196 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
197 * able to construct an SSL_SESSION that will collide with any existing session
198 * with a matching session ID. */
199static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
200 if (a->ssl_version != b->ssl_version) {
201 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800202 }
Adam Langley95c29f32014-06-20 12:00:00 -0700203
David Benjamin27bbae42015-09-13 00:54:37 -0400204 if (a->session_id_length != b->session_id_length) {
205 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800206 }
Adam Langley95c29f32014-06-20 12:00:00 -0700207
David Benjamin27bbae42015-09-13 00:54:37 -0400208 return memcmp(a->session_id, b->session_id, a->session_id_length);
209}
Adam Langley95c29f32014-06-20 12:00:00 -0700210
David Benjamin27bbae42015-09-13 00:54:37 -0400211SSL_CTX *SSL_CTX_new(const SSL_METHOD *method) {
212 SSL_CTX *ret = NULL;
213
214 if (method == NULL) {
215 OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_METHOD_PASSED);
216 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800217 }
Adam Langley95c29f32014-06-20 12:00:00 -0700218
David Benjamin27bbae42015-09-13 00:54:37 -0400219 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
220 OPENSSL_PUT_ERROR(SSL, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
221 goto err;
David Benjamin62fd1622015-01-11 13:30:01 -0500222 }
223
David Benjamin27bbae42015-09-13 00:54:37 -0400224 ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
225 if (ret == NULL) {
226 goto err;
David Benjamin62fd1622015-01-11 13:30:01 -0500227 }
228
David Benjamin27bbae42015-09-13 00:54:37 -0400229 memset(ret, 0, sizeof(SSL_CTX));
Adam Langley95c29f32014-06-20 12:00:00 -0700230
David Benjamin27bbae42015-09-13 00:54:37 -0400231 ret->method = method->method;
232
233 CRYPTO_MUTEX_init(&ret->lock);
234
235 ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
236 ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
237
238 /* We take the system default */
239 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
240
241 ret->references = 1;
242
243 ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
244 ret->verify_mode = SSL_VERIFY_NONE;
245 ret->cert = ssl_cert_new();
246 if (ret->cert == NULL) {
247 goto err;
248 }
249
250 ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
251 if (ret->sessions == NULL) {
252 goto err;
253 }
254 ret->cert_store = X509_STORE_new();
255 if (ret->cert_store == NULL) {
256 goto err;
257 }
258
259 ssl_create_cipher_list(ret->method, &ret->cipher_list,
260 &ret->cipher_list_by_id, SSL_DEFAULT_CIPHER_LIST);
261 if (ret->cipher_list == NULL ||
262 sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0) {
263 OPENSSL_PUT_ERROR(SSL, SSL_R_LIBRARY_HAS_NO_CIPHERS);
264 goto err2;
265 }
266
267 ret->param = X509_VERIFY_PARAM_new();
268 if (!ret->param) {
269 goto err;
270 }
271
272 ret->client_CA = sk_X509_NAME_new_null();
273 if (ret->client_CA == NULL) {
274 goto err;
275 }
276
David Benjamin8a589332015-12-04 23:14:35 -0500277 CRYPTO_new_ex_data(&ret->ex_data);
David Benjamin27bbae42015-09-13 00:54:37 -0400278
279 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
280
281 /* Setup RFC4507 ticket keys */
282 if (!RAND_bytes(ret->tlsext_tick_key_name, 16) ||
283 !RAND_bytes(ret->tlsext_tick_hmac_key, 16) ||
284 !RAND_bytes(ret->tlsext_tick_aes_key, 16)) {
285 ret->options |= SSL_OP_NO_TICKET;
286 }
287
David Benjamin27bbae42015-09-13 00:54:37 -0400288 /* Lock the SSL_CTX to the specified version, for compatibility with legacy
289 * uses of SSL_METHOD. */
290 if (method->version != 0) {
291 SSL_CTX_set_max_version(ret, method->version);
292 SSL_CTX_set_min_version(ret, method->version);
293 }
294
295 return ret;
296
297err:
298 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
299err2:
300 SSL_CTX_free(ret);
301 return NULL;
302}
303
304void SSL_CTX_free(SSL_CTX *ctx) {
305 if (ctx == NULL ||
306 !CRYPTO_refcount_dec_and_test_zero(&ctx->references)) {
307 return;
308 }
309
310 X509_VERIFY_PARAM_free(ctx->param);
311
312 /* Free internal session cache. However: the remove_cb() may reference the
313 * ex_data of SSL_CTX, thus the ex_data store can only be removed after the
314 * sessions were flushed. As the ex_data handling routines might also touch
315 * the session cache, the most secure solution seems to be: empty (flush) the
316 * cache, then free ex_data, then finally free the cache. (See ticket
317 * [openssl.org #212].) */
318 SSL_CTX_flush_sessions(ctx, 0);
319
320 CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, ctx, &ctx->ex_data);
321
322 CRYPTO_MUTEX_cleanup(&ctx->lock);
323 lh_SSL_SESSION_free(ctx->sessions);
324 X509_STORE_free(ctx->cert_store);
325 ssl_cipher_preference_list_free(ctx->cipher_list);
326 sk_SSL_CIPHER_free(ctx->cipher_list_by_id);
327 ssl_cipher_preference_list_free(ctx->cipher_list_tls10);
328 ssl_cipher_preference_list_free(ctx->cipher_list_tls11);
329 ssl_cert_free(ctx->cert);
330 sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->client_custom_extensions,
331 SSL_CUSTOM_EXTENSION_free);
332 sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->server_custom_extensions,
333 SSL_CUSTOM_EXTENSION_free);
334 sk_X509_NAME_pop_free(ctx->client_CA, X509_NAME_free);
335 sk_SRTP_PROTECTION_PROFILE_free(ctx->srtp_profiles);
336 OPENSSL_free(ctx->psk_identity_hint);
337 OPENSSL_free(ctx->tlsext_ellipticcurvelist);
338 OPENSSL_free(ctx->alpn_client_proto_list);
339 OPENSSL_free(ctx->ocsp_response);
340 OPENSSL_free(ctx->signed_cert_timestamp_list);
341 EVP_PKEY_free(ctx->tlsext_channel_id_private);
David Benjamin27bbae42015-09-13 00:54:37 -0400342
343 OPENSSL_free(ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -0800344}
Adam Langley95c29f32014-06-20 12:00:00 -0700345
Adam Langleyfcf25832014-12-18 17:42:32 -0800346SSL *SSL_new(SSL_CTX *ctx) {
347 SSL *s;
Adam Langley95c29f32014-06-20 12:00:00 -0700348
Adam Langleyfcf25832014-12-18 17:42:32 -0800349 if (ctx == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400350 OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_CTX);
Adam Langleyfcf25832014-12-18 17:42:32 -0800351 return NULL;
352 }
353 if (ctx->method == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400354 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
Adam Langleyfcf25832014-12-18 17:42:32 -0800355 return NULL;
356 }
Adam Langley95c29f32014-06-20 12:00:00 -0700357
Adam Langleyfcf25832014-12-18 17:42:32 -0800358 s = (SSL *)OPENSSL_malloc(sizeof(SSL));
359 if (s == NULL) {
360 goto err;
361 }
362 memset(s, 0, sizeof(SSL));
Adam Langley95c29f32014-06-20 12:00:00 -0700363
Adam Langleyfcf25832014-12-18 17:42:32 -0800364 s->min_version = ctx->min_version;
365 s->max_version = ctx->max_version;
David Benjamin1eb367c2014-12-12 18:17:51 -0500366
Adam Langleyfcf25832014-12-18 17:42:32 -0800367 s->options = ctx->options;
368 s->mode = ctx->mode;
369 s->max_cert_list = ctx->max_cert_list;
Adam Langley95c29f32014-06-20 12:00:00 -0700370
David Benjamina5a3eeb2015-03-18 20:26:30 -0400371 s->cert = ssl_cert_dup(ctx->cert);
372 if (s->cert == NULL) {
373 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -0800374 }
Adam Langley95c29f32014-06-20 12:00:00 -0700375
Adam Langleyfcf25832014-12-18 17:42:32 -0800376 s->msg_callback = ctx->msg_callback;
377 s->msg_callback_arg = ctx->msg_callback_arg;
378 s->verify_mode = ctx->verify_mode;
379 s->sid_ctx_length = ctx->sid_ctx_length;
380 assert(s->sid_ctx_length <= sizeof s->sid_ctx);
381 memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));
382 s->verify_callback = ctx->default_verify_callback;
Adam Langley95c29f32014-06-20 12:00:00 -0700383
Adam Langleyfcf25832014-12-18 17:42:32 -0800384 s->param = X509_VERIFY_PARAM_new();
385 if (!s->param) {
386 goto err;
387 }
388 X509_VERIFY_PARAM_inherit(s->param, ctx->param);
389 s->quiet_shutdown = ctx->quiet_shutdown;
390 s->max_send_fragment = ctx->max_send_fragment;
Adam Langley95c29f32014-06-20 12:00:00 -0700391
Adam Langley0b5e3902015-05-15 13:08:38 -0700392 CRYPTO_refcount_inc(&ctx->references);
Adam Langleyfcf25832014-12-18 17:42:32 -0800393 s->ctx = ctx;
Adam Langley0b5e3902015-05-15 13:08:38 -0700394 CRYPTO_refcount_inc(&ctx->references);
Adam Langleyfcf25832014-12-18 17:42:32 -0800395 s->initial_ctx = ctx;
Adam Langley95c29f32014-06-20 12:00:00 -0700396
Adam Langleyfcf25832014-12-18 17:42:32 -0800397 if (ctx->tlsext_ellipticcurvelist) {
398 s->tlsext_ellipticcurvelist =
399 BUF_memdup(ctx->tlsext_ellipticcurvelist,
400 ctx->tlsext_ellipticcurvelist_length * 2);
401 if (!s->tlsext_ellipticcurvelist) {
402 goto err;
403 }
404 s->tlsext_ellipticcurvelist_length = ctx->tlsext_ellipticcurvelist_length;
405 }
Adam Langley95c29f32014-06-20 12:00:00 -0700406
Adam Langleyfcf25832014-12-18 17:42:32 -0800407 if (s->ctx->alpn_client_proto_list) {
408 s->alpn_client_proto_list = BUF_memdup(s->ctx->alpn_client_proto_list,
409 s->ctx->alpn_client_proto_list_len);
410 if (s->alpn_client_proto_list == NULL) {
411 goto err;
412 }
413 s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;
414 }
Adam Langley95c29f32014-06-20 12:00:00 -0700415
Adam Langleyfcf25832014-12-18 17:42:32 -0800416 s->verify_result = X509_V_OK;
417 s->method = ctx->method;
Adam Langley95c29f32014-06-20 12:00:00 -0700418
Adam Langleyfcf25832014-12-18 17:42:32 -0800419 if (!s->method->ssl_new(s)) {
420 goto err;
421 }
422 s->enc_method = ssl3_get_enc_method(s->version);
423 assert(s->enc_method != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700424
David Benjamin62fd1622015-01-11 13:30:01 -0500425 s->rwstate = SSL_NOTHING;
Adam Langley95c29f32014-06-20 12:00:00 -0700426
David Benjamin8a589332015-12-04 23:14:35 -0500427 CRYPTO_new_ex_data(&s->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700428
Adam Langleyfcf25832014-12-18 17:42:32 -0800429 s->psk_identity_hint = NULL;
430 if (ctx->psk_identity_hint) {
431 s->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
432 if (s->psk_identity_hint == NULL) {
433 goto err;
434 }
435 }
436 s->psk_client_callback = ctx->psk_client_callback;
437 s->psk_server_callback = ctx->psk_server_callback;
Adam Langley95c29f32014-06-20 12:00:00 -0700438
David Benjamin02ddbfd2015-01-11 13:09:11 -0500439 s->tlsext_channel_id_enabled = ctx->tlsext_channel_id_enabled;
440 if (ctx->tlsext_channel_id_private) {
David Benjamin9a10f8f2015-05-05 22:22:40 -0400441 s->tlsext_channel_id_private =
442 EVP_PKEY_up_ref(ctx->tlsext_channel_id_private);
David Benjamin02ddbfd2015-01-11 13:09:11 -0500443 }
444
Adam Langleyfcf25832014-12-18 17:42:32 -0800445 s->signed_cert_timestamps_enabled = s->ctx->signed_cert_timestamps_enabled;
446 s->ocsp_stapling_enabled = s->ctx->ocsp_stapling_enabled;
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200447
Adam Langleyfcf25832014-12-18 17:42:32 -0800448 return s;
449
Adam Langley95c29f32014-06-20 12:00:00 -0700450err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400451 SSL_free(s);
David Benjamin3570d732015-06-29 00:28:17 -0400452 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700453
Adam Langleyfcf25832014-12-18 17:42:32 -0800454 return NULL;
455}
Adam Langley95c29f32014-06-20 12:00:00 -0700456
David Benjamin27bbae42015-09-13 00:54:37 -0400457void SSL_free(SSL *ssl) {
458 if (ssl == NULL) {
459 return;
460 }
461
462 X509_VERIFY_PARAM_free(ssl->param);
463
464 CRYPTO_free_ex_data(&g_ex_data_class_ssl, ssl, &ssl->ex_data);
465
466 if (ssl->bbio != NULL) {
467 /* If the buffering BIO is in place, pop it off */
468 if (ssl->bbio == ssl->wbio) {
469 ssl->wbio = BIO_pop(ssl->wbio);
470 }
471 BIO_free(ssl->bbio);
472 ssl->bbio = NULL;
473 }
474
475 int free_wbio = ssl->wbio != ssl->rbio;
476 BIO_free_all(ssl->rbio);
477 if (free_wbio) {
478 BIO_free_all(ssl->wbio);
479 }
480
481 BUF_MEM_free(ssl->init_buf);
482
483 /* add extra stuff */
484 ssl_cipher_preference_list_free(ssl->cipher_list);
485 sk_SSL_CIPHER_free(ssl->cipher_list_by_id);
486
487 ssl_clear_bad_session(ssl);
488 SSL_SESSION_free(ssl->session);
489
490 ssl_clear_cipher_ctx(ssl);
491
492 ssl_cert_free(ssl->cert);
493
494 OPENSSL_free(ssl->tlsext_hostname);
495 SSL_CTX_free(ssl->initial_ctx);
496 OPENSSL_free(ssl->tlsext_ellipticcurvelist);
497 OPENSSL_free(ssl->alpn_client_proto_list);
498 EVP_PKEY_free(ssl->tlsext_channel_id_private);
499 OPENSSL_free(ssl->psk_identity_hint);
500 sk_X509_NAME_pop_free(ssl->client_CA, X509_NAME_free);
501 OPENSSL_free(ssl->next_proto_negotiated);
502 sk_SRTP_PROTECTION_PROFILE_free(ssl->srtp_profiles);
503
504 if (ssl->method != NULL) {
505 ssl->method->ssl_free(ssl);
506 }
507 SSL_CTX_free(ssl->ctx);
508
509 OPENSSL_free(ssl);
510}
511
512void SSL_set_connect_state(SSL *ssl) {
513 ssl->server = 0;
514 ssl->shutdown = 0;
515 ssl->state = SSL_ST_CONNECT;
516 ssl->handshake_func = ssl->method->ssl_connect;
517 /* clear the current cipher */
518 ssl_clear_cipher_ctx(ssl);
519}
520
521void SSL_set_accept_state(SSL *ssl) {
522 ssl->server = 1;
523 ssl->shutdown = 0;
524 ssl->state = SSL_ST_ACCEPT;
525 ssl->handshake_func = ssl->method->ssl_accept;
526 /* clear the current cipher */
527 ssl_clear_cipher_ctx(ssl);
528}
529
530void SSL_set_bio(SSL *ssl, BIO *rbio, BIO *wbio) {
531 /* If the output buffering BIO is still in place, remove it. */
532 if (ssl->bbio != NULL) {
533 if (ssl->wbio == ssl->bbio) {
534 ssl->wbio = ssl->wbio->next_bio;
535 ssl->bbio->next_bio = NULL;
536 }
537 }
538
539 if (ssl->rbio != rbio) {
540 BIO_free_all(ssl->rbio);
541 }
542 if (ssl->wbio != wbio && ssl->rbio != ssl->wbio) {
543 BIO_free_all(ssl->wbio);
544 }
545 ssl->rbio = rbio;
546 ssl->wbio = wbio;
547}
548
549BIO *SSL_get_rbio(const SSL *ssl) { return ssl->rbio; }
550
551BIO *SSL_get_wbio(const SSL *ssl) { return ssl->wbio; }
552
553int SSL_do_handshake(SSL *ssl) {
554 if (ssl->handshake_func == NULL) {
555 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_TYPE_NOT_SET);
556 return -1;
557 }
558
559 if (!SSL_in_init(ssl)) {
560 return 1;
561 }
562
563 return ssl->handshake_func(ssl);
564}
565
566int SSL_connect(SSL *ssl) {
567 if (ssl->handshake_func == 0) {
568 /* Not properly initialized yet */
569 SSL_set_connect_state(ssl);
570 }
571
572 if (ssl->handshake_func != ssl->method->ssl_connect) {
573 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
574 return -1;
575 }
576
577 return ssl->handshake_func(ssl);
578}
579
580int SSL_accept(SSL *ssl) {
581 if (ssl->handshake_func == 0) {
582 /* Not properly initialized yet */
583 SSL_set_accept_state(ssl);
584 }
585
586 if (ssl->handshake_func != ssl->method->ssl_accept) {
587 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
588 return -1;
589 }
590
591 return ssl->handshake_func(ssl);
592}
593
594int SSL_read(SSL *ssl, void *buf, int num) {
595 if (ssl->handshake_func == 0) {
596 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
597 return -1;
598 }
599
600 if (ssl->shutdown & SSL_RECEIVED_SHUTDOWN) {
601 ssl->rwstate = SSL_NOTHING;
602 return 0;
603 }
604
605 ERR_clear_system_error();
606 return ssl->method->ssl_read_app_data(ssl, buf, num, 0);
607}
608
609int SSL_peek(SSL *ssl, void *buf, int num) {
610 if (ssl->handshake_func == 0) {
611 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
612 return -1;
613 }
614
615 if (ssl->shutdown & SSL_RECEIVED_SHUTDOWN) {
616 return 0;
617 }
618
619 ERR_clear_system_error();
620 return ssl->method->ssl_read_app_data(ssl, buf, num, 1);
621}
622
623int SSL_write(SSL *ssl, const void *buf, int num) {
624 if (ssl->handshake_func == 0) {
625 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
626 return -1;
627 }
628
629 if (ssl->shutdown & SSL_SENT_SHUTDOWN) {
630 ssl->rwstate = SSL_NOTHING;
631 OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
632 return -1;
633 }
634
635 ERR_clear_system_error();
636 return ssl->method->ssl_write_app_data(ssl, buf, num);
637}
638
639int SSL_shutdown(SSL *ssl) {
640 /* Note that this function behaves differently from what one might expect.
641 * Return values are 0 for no success (yet), 1 for success; but calling it
642 * once is usually not enough, even if blocking I/O is used (see
643 * ssl3_shutdown). */
644
645 if (ssl->handshake_func == 0) {
646 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
647 return -1;
648 }
649
650 if (SSL_in_init(ssl)) {
651 return 1;
652 }
653
654 /* Do nothing if configured not to send a close_notify. */
655 if (ssl->quiet_shutdown) {
656 ssl->shutdown = SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN;
657 return 1;
658 }
659
660 if (!(ssl->shutdown & SSL_SENT_SHUTDOWN)) {
661 ssl->shutdown |= SSL_SENT_SHUTDOWN;
662 ssl3_send_alert(ssl, SSL3_AL_WARNING, SSL_AD_CLOSE_NOTIFY);
663
664 /* our shutdown alert has been sent now, and if it still needs to be
665 * written, ssl->s3->alert_dispatch will be true */
666 if (ssl->s3->alert_dispatch) {
667 return -1; /* return WANT_WRITE */
668 }
669 } else if (ssl->s3->alert_dispatch) {
670 /* resend it if not sent */
671 int ret = ssl->method->ssl_dispatch_alert(ssl);
672 if (ret == -1) {
673 /* we only get to return -1 here the 2nd/Nth invocation, we must have
674 * already signalled return 0 upon a previous invoation, return
675 * WANT_WRITE */
676 return ret;
677 }
678 } else if (!(ssl->shutdown & SSL_RECEIVED_SHUTDOWN)) {
679 /* If we are waiting for a close from our peer, we are closed */
680 ssl->method->ssl_read_close_notify(ssl);
681 if (!(ssl->shutdown & SSL_RECEIVED_SHUTDOWN)) {
682 return -1; /* return WANT_READ */
683 }
684 }
685
686 if (ssl->shutdown == (SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN) &&
687 !ssl->s3->alert_dispatch) {
688 return 1;
689 } else {
690 return 0;
691 }
692}
693
694int SSL_get_error(const SSL *ssl, int ret_code) {
695 int reason;
696 uint32_t err;
697 BIO *bio;
698
699 if (ret_code > 0) {
700 return SSL_ERROR_NONE;
701 }
702
703 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
704 * where we do encode the error */
705 err = ERR_peek_error();
706 if (err != 0) {
707 if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
708 return SSL_ERROR_SYSCALL;
709 }
710 return SSL_ERROR_SSL;
711 }
712
713 if (ret_code == 0) {
714 if ((ssl->shutdown & SSL_RECEIVED_SHUTDOWN) &&
715 (ssl->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) {
716 /* The socket was cleanly shut down with a close_notify. */
717 return SSL_ERROR_ZERO_RETURN;
718 }
719 /* An EOF was observed which violates the protocol, and the underlying
720 * transport does not participate in the error queue. Bubble up to the
721 * caller. */
722 return SSL_ERROR_SYSCALL;
723 }
724
725 if (SSL_want_session(ssl)) {
726 return SSL_ERROR_PENDING_SESSION;
727 }
728
729 if (SSL_want_certificate(ssl)) {
730 return SSL_ERROR_PENDING_CERTIFICATE;
731 }
732
733 if (SSL_want_read(ssl)) {
734 bio = SSL_get_rbio(ssl);
735 if (BIO_should_read(bio)) {
736 return SSL_ERROR_WANT_READ;
737 }
738
739 if (BIO_should_write(bio)) {
740 /* This one doesn't make too much sense ... We never try to write to the
741 * rbio, and an application program where rbio and wbio are separate
742 * couldn't even know what it should wait for. However if we ever set
743 * s->rwstate incorrectly (so that we have SSL_want_read(s) instead of
744 * SSL_want_write(s)) and rbio and wbio *are* the same, this test works
745 * around that bug; so it might be safer to keep it. */
746 return SSL_ERROR_WANT_WRITE;
747 }
748
749 if (BIO_should_io_special(bio)) {
750 reason = BIO_get_retry_reason(bio);
751 if (reason == BIO_RR_CONNECT) {
752 return SSL_ERROR_WANT_CONNECT;
753 }
754
755 if (reason == BIO_RR_ACCEPT) {
756 return SSL_ERROR_WANT_ACCEPT;
757 }
758
759 return SSL_ERROR_SYSCALL; /* unknown */
760 }
761 }
762
763 if (SSL_want_write(ssl)) {
764 bio = SSL_get_wbio(ssl);
765 if (BIO_should_write(bio)) {
766 return SSL_ERROR_WANT_WRITE;
767 }
768
769 if (BIO_should_read(bio)) {
770 /* See above (SSL_want_read(ssl) with BIO_should_write(bio)) */
771 return SSL_ERROR_WANT_READ;
772 }
773
774 if (BIO_should_io_special(bio)) {
775 reason = BIO_get_retry_reason(bio);
776 if (reason == BIO_RR_CONNECT) {
777 return SSL_ERROR_WANT_CONNECT;
778 }
779
780 if (reason == BIO_RR_ACCEPT) {
781 return SSL_ERROR_WANT_ACCEPT;
782 }
783
784 return SSL_ERROR_SYSCALL;
785 }
786 }
787
788 if (SSL_want_x509_lookup(ssl)) {
789 return SSL_ERROR_WANT_X509_LOOKUP;
790 }
791
792 if (SSL_want_channel_id_lookup(ssl)) {
793 return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
794 }
795
796 if (SSL_want_private_key_operation(ssl)) {
797 return SSL_ERROR_WANT_PRIVATE_KEY_OPERATION;
798 }
799
800 return SSL_ERROR_SYSCALL;
801}
802
803void SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) {
804 ctx->min_version = version;
805}
806
807void SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) {
808 ctx->max_version = version;
809}
810
811void SSL_set_min_version(SSL *ssl, uint16_t version) {
812 ssl->min_version = version;
813}
814
815void SSL_set_max_version(SSL *ssl, uint16_t version) {
816 ssl->max_version = version;
817}
818
819uint32_t SSL_CTX_set_options(SSL_CTX *ctx, uint32_t options) {
820 ctx->options |= options;
821 return ctx->options;
822}
823
824uint32_t SSL_CTX_clear_options(SSL_CTX *ctx, uint32_t options) {
825 ctx->options &= ~options;
826 return ctx->options;
827}
828
829uint32_t SSL_CTX_get_options(const SSL_CTX *ctx) { return ctx->options; }
830
831uint32_t SSL_set_options(SSL *ssl, uint32_t options) {
832 ssl->options |= options;
833 return ssl->options;
834}
835
836uint32_t SSL_clear_options(SSL *ssl, uint32_t options) {
837 ssl->options &= ~options;
838 return ssl->options;
839}
840
841uint32_t SSL_get_options(const SSL *ssl) { return ssl->options; }
842
843uint32_t SSL_CTX_set_mode(SSL_CTX *ctx, uint32_t mode) {
844 ctx->mode |= mode;
845 return ctx->mode;
846}
847
848uint32_t SSL_CTX_clear_mode(SSL_CTX *ctx, uint32_t mode) {
849 ctx->mode &= ~mode;
850 return ctx->mode;
851}
852
853uint32_t SSL_CTX_get_mode(const SSL_CTX *ctx) { return ctx->mode; }
854
855uint32_t SSL_set_mode(SSL *ssl, uint32_t mode) {
856 ssl->mode |= mode;
857 return ssl->mode;
858}
859
860uint32_t SSL_clear_mode(SSL *ssl, uint32_t mode) {
861 ssl->mode &= ~mode;
862 return ssl->mode;
863}
864
865uint32_t SSL_get_mode(const SSL *ssl) { return ssl->mode; }
866
David Benjaminee0c8272015-09-13 01:03:54 -0400867X509 *SSL_get_peer_certificate(const SSL *ssl) {
868 if (ssl == NULL || ssl->session == NULL || ssl->session->peer == NULL) {
869 return NULL;
870 }
871 return X509_up_ref(ssl->session->peer);
872}
873
874STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *ssl) {
875 if (ssl == NULL || ssl->session == NULL) {
876 return NULL;
877 }
878 return ssl->session->cert_chain;
879}
880
881int SSL_get_tls_unique(const SSL *ssl, uint8_t *out, size_t *out_len,
882 size_t max_out) {
883 /* The tls-unique value is the first Finished message in the handshake, which
884 * is the client's in a full handshake and the server's for a resumption. See
885 * https://tools.ietf.org/html/rfc5929#section-3.1. */
886 const uint8_t *finished = ssl->s3->previous_client_finished;
887 size_t finished_len = ssl->s3->previous_client_finished_len;
888 if (ssl->hit) {
889 /* tls-unique is broken for resumed sessions unless EMS is used. */
890 if (!ssl->session->extended_master_secret) {
891 goto err;
892 }
893 finished = ssl->s3->previous_server_finished;
894 finished_len = ssl->s3->previous_server_finished_len;
895 }
896
897 if (!ssl->s3->initial_handshake_complete ||
898 ssl->version < TLS1_VERSION) {
899 goto err;
900 }
901
902 *out_len = finished_len;
903 if (finished_len > max_out) {
904 *out_len = max_out;
905 }
906
907 memcpy(out, finished, *out_len);
908 return 1;
909
910err:
911 *out_len = 0;
912 memset(out, 0, max_out);
913 return 0;
914}
915
Adam Langleyfcf25832014-12-18 17:42:32 -0800916int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
David Benjamindafbdd42015-09-14 01:40:10 -0400917 unsigned sid_ctx_len) {
918 if (sid_ctx_len > sizeof(ctx->sid_ctx)) {
David Benjamin3570d732015-06-29 00:28:17 -0400919 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -0800920 return 0;
921 }
922 ctx->sid_ctx_length = sid_ctx_len;
923 memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700924
Adam Langleyfcf25832014-12-18 17:42:32 -0800925 return 1;
926}
Adam Langley95c29f32014-06-20 12:00:00 -0700927
Adam Langleyfcf25832014-12-18 17:42:32 -0800928int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
David Benjamindafbdd42015-09-14 01:40:10 -0400929 unsigned sid_ctx_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800930 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
David Benjamin3570d732015-06-29 00:28:17 -0400931 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -0800932 return 0;
933 }
934 ssl->sid_ctx_length = sid_ctx_len;
935 memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700936
Adam Langleyfcf25832014-12-18 17:42:32 -0800937 return 1;
938}
Adam Langley95c29f32014-06-20 12:00:00 -0700939
David Benjamin59937042015-09-19 13:04:22 -0400940int SSL_CTX_set_purpose(SSL_CTX *ctx, int purpose) {
941 return X509_VERIFY_PARAM_set_purpose(ctx->param, purpose);
Adam Langleyfcf25832014-12-18 17:42:32 -0800942}
943
David Benjamin59937042015-09-19 13:04:22 -0400944int SSL_set_purpose(SSL *ssl, int purpose) {
945 return X509_VERIFY_PARAM_set_purpose(ssl->param, purpose);
Adam Langleyfcf25832014-12-18 17:42:32 -0800946}
947
David Benjamin59937042015-09-19 13:04:22 -0400948int SSL_CTX_set_trust(SSL_CTX *ctx, int trust) {
949 return X509_VERIFY_PARAM_set_trust(ctx->param, trust);
Adam Langleyfcf25832014-12-18 17:42:32 -0800950}
951
David Benjamin59937042015-09-19 13:04:22 -0400952int SSL_set_trust(SSL *ssl, int trust) {
953 return X509_VERIFY_PARAM_set_trust(ssl->param, trust);
Adam Langleyfcf25832014-12-18 17:42:32 -0800954}
955
David Benjamin59937042015-09-19 13:04:22 -0400956int SSL_CTX_set1_param(SSL_CTX *ctx, const X509_VERIFY_PARAM *param) {
957 return X509_VERIFY_PARAM_set1(ctx->param, param);
Adam Langleyfcf25832014-12-18 17:42:32 -0800958}
959
David Benjamin59937042015-09-19 13:04:22 -0400960int SSL_set1_param(SSL *ssl, const X509_VERIFY_PARAM *param) {
961 return X509_VERIFY_PARAM_set1(ssl->param, param);
Adam Langleyfcf25832014-12-18 17:42:32 -0800962}
Adam Langley95c29f32014-06-20 12:00:00 -0700963
Adam Langley858a88d2014-06-20 12:00:00 -0700964void ssl_cipher_preference_list_free(
Adam Langleyfcf25832014-12-18 17:42:32 -0800965 struct ssl_cipher_preference_list_st *cipher_list) {
David Benjamin5d1ec732015-04-22 13:38:00 -0400966 if (cipher_list == NULL) {
967 return;
968 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800969 sk_SSL_CIPHER_free(cipher_list->ciphers);
970 OPENSSL_free(cipher_list->in_group_flags);
971 OPENSSL_free(cipher_list);
972}
Adam Langley858a88d2014-06-20 12:00:00 -0700973
Adam Langleyfcf25832014-12-18 17:42:32 -0800974struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_dup(
975 struct ssl_cipher_preference_list_st *cipher_list) {
976 struct ssl_cipher_preference_list_st *ret = NULL;
977 size_t n = sk_SSL_CIPHER_num(cipher_list->ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700978
Adam Langleyfcf25832014-12-18 17:42:32 -0800979 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
980 if (!ret) {
981 goto err;
982 }
983
984 ret->ciphers = NULL;
985 ret->in_group_flags = NULL;
986 ret->ciphers = sk_SSL_CIPHER_dup(cipher_list->ciphers);
987 if (!ret->ciphers) {
988 goto err;
989 }
990 ret->in_group_flags = BUF_memdup(cipher_list->in_group_flags, n);
991 if (!ret->in_group_flags) {
992 goto err;
993 }
994
995 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -0700996
997err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400998 ssl_cipher_preference_list_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -0800999 return NULL;
1000}
Adam Langley858a88d2014-06-20 12:00:00 -07001001
Adam Langleyfcf25832014-12-18 17:42:32 -08001002struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_from_ciphers(
David Benjamin60da0cd2015-05-03 15:21:28 -04001003 STACK_OF(SSL_CIPHER) *ciphers) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001004 struct ssl_cipher_preference_list_st *ret = NULL;
1005 size_t n = sk_SSL_CIPHER_num(ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -07001006
Adam Langleyfcf25832014-12-18 17:42:32 -08001007 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
1008 if (!ret) {
1009 goto err;
1010 }
1011 ret->ciphers = NULL;
1012 ret->in_group_flags = NULL;
1013 ret->ciphers = sk_SSL_CIPHER_dup(ciphers);
1014 if (!ret->ciphers) {
1015 goto err;
1016 }
1017 ret->in_group_flags = OPENSSL_malloc(n);
1018 if (!ret->in_group_flags) {
1019 goto err;
1020 }
1021 memset(ret->in_group_flags, 0, n);
1022 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -07001023
1024err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001025 ssl_cipher_preference_list_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -08001026 return NULL;
1027}
Adam Langley858a88d2014-06-20 12:00:00 -07001028
Adam Langleyfcf25832014-12-18 17:42:32 -08001029X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) { return ctx->param; }
Adam Langley95c29f32014-06-20 12:00:00 -07001030
Adam Langleyfcf25832014-12-18 17:42:32 -08001031X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) { return ssl->param; }
Adam Langley95c29f32014-06-20 12:00:00 -07001032
David Benjamin7481d392015-07-05 19:38:46 -04001033void SSL_certs_clear(SSL *ssl) { ssl_cert_clear_certs(ssl->cert); }
Adam Langley95c29f32014-06-20 12:00:00 -07001034
David Benjamin066fe0a2015-10-17 21:11:33 -04001035int SSL_get_fd(const SSL *ssl) { return SSL_get_rfd(ssl); }
Adam Langley95c29f32014-06-20 12:00:00 -07001036
David Benjamin066fe0a2015-10-17 21:11:33 -04001037int SSL_get_rfd(const SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001038 int ret = -1;
David Benjamin066fe0a2015-10-17 21:11:33 -04001039 BIO *b = BIO_find_type(SSL_get_rbio(ssl), BIO_TYPE_DESCRIPTOR);
1040 if (b != NULL) {
1041 BIO_get_fd(b, &ret);
Adam Langleyfcf25832014-12-18 17:42:32 -08001042 }
1043 return ret;
1044}
Adam Langley95c29f32014-06-20 12:00:00 -07001045
David Benjamin066fe0a2015-10-17 21:11:33 -04001046int SSL_get_wfd(const SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001047 int ret = -1;
David Benjamin066fe0a2015-10-17 21:11:33 -04001048 BIO *b = BIO_find_type(SSL_get_wbio(ssl), BIO_TYPE_DESCRIPTOR);
1049 if (b != NULL) {
1050 BIO_get_fd(b, &ret);
Adam Langleyfcf25832014-12-18 17:42:32 -08001051 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001052 return ret;
1053}
Adam Langley95c29f32014-06-20 12:00:00 -07001054
David Benjamin066fe0a2015-10-17 21:11:33 -04001055int SSL_set_fd(SSL *ssl, int fd) {
1056 BIO *bio = BIO_new(BIO_s_fd());
Adam Langleyfcf25832014-12-18 17:42:32 -08001057 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001058 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
David Benjamin066fe0a2015-10-17 21:11:33 -04001059 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001060 }
1061 BIO_set_fd(bio, fd, BIO_NOCLOSE);
David Benjamin066fe0a2015-10-17 21:11:33 -04001062 SSL_set_bio(ssl, bio, bio);
1063 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001064}
Adam Langley95c29f32014-06-20 12:00:00 -07001065
David Benjamin066fe0a2015-10-17 21:11:33 -04001066int SSL_set_wfd(SSL *ssl, int fd) {
1067 if (ssl->rbio == NULL ||
1068 BIO_method_type(ssl->rbio) != BIO_TYPE_FD ||
1069 BIO_get_fd(ssl->rbio, NULL) != fd) {
1070 BIO *bio = BIO_new(BIO_s_fd());
Adam Langleyfcf25832014-12-18 17:42:32 -08001071 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001072 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
David Benjamin066fe0a2015-10-17 21:11:33 -04001073 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001074 }
1075 BIO_set_fd(bio, fd, BIO_NOCLOSE);
David Benjamin066fe0a2015-10-17 21:11:33 -04001076 SSL_set_bio(ssl, SSL_get_rbio(ssl), bio);
Adam Langleyfcf25832014-12-18 17:42:32 -08001077 } else {
David Benjamin066fe0a2015-10-17 21:11:33 -04001078 SSL_set_bio(ssl, SSL_get_rbio(ssl), SSL_get_rbio(ssl));
Adam Langleyfcf25832014-12-18 17:42:32 -08001079 }
1080
David Benjamin066fe0a2015-10-17 21:11:33 -04001081 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001082}
Adam Langley95c29f32014-06-20 12:00:00 -07001083
David Benjamin066fe0a2015-10-17 21:11:33 -04001084int SSL_set_rfd(SSL *ssl, int fd) {
1085 if (ssl->wbio == NULL || BIO_method_type(ssl->wbio) != BIO_TYPE_FD ||
1086 BIO_get_fd(ssl->wbio, NULL) != fd) {
1087 BIO *bio = BIO_new(BIO_s_fd());
Adam Langleyfcf25832014-12-18 17:42:32 -08001088 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001089 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
David Benjamin066fe0a2015-10-17 21:11:33 -04001090 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001091 }
1092 BIO_set_fd(bio, fd, BIO_NOCLOSE);
David Benjamin066fe0a2015-10-17 21:11:33 -04001093 SSL_set_bio(ssl, bio, SSL_get_wbio(ssl));
Adam Langleyfcf25832014-12-18 17:42:32 -08001094 } else {
David Benjamin066fe0a2015-10-17 21:11:33 -04001095 SSL_set_bio(ssl, SSL_get_wbio(ssl), SSL_get_wbio(ssl));
Adam Langleyfcf25832014-12-18 17:42:32 -08001096 }
David Benjamin066fe0a2015-10-17 21:11:33 -04001097 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001098}
Adam Langley95c29f32014-06-20 12:00:00 -07001099
David Benjamin1a1b34d2015-10-17 12:51:52 -04001100size_t SSL_get_finished(const SSL *ssl, void *buf, size_t count) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001101 size_t ret = 0;
1102
David Benjamin1a1b34d2015-10-17 12:51:52 -04001103 if (ssl->s3 != NULL) {
1104 ret = ssl->s3->tmp.finish_md_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08001105 if (count > ret) {
1106 count = ret;
1107 }
David Benjamin1a1b34d2015-10-17 12:51:52 -04001108 memcpy(buf, ssl->s3->tmp.finish_md, count);
Adam Langleyfcf25832014-12-18 17:42:32 -08001109 }
1110
1111 return ret;
1112}
Adam Langley95c29f32014-06-20 12:00:00 -07001113
David Benjamin1a1b34d2015-10-17 12:51:52 -04001114size_t SSL_get_peer_finished(const SSL *ssl, void *buf, size_t count) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001115 size_t ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001116
David Benjamin1a1b34d2015-10-17 12:51:52 -04001117 if (ssl->s3 != NULL) {
1118 ret = ssl->s3->tmp.peer_finish_md_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08001119 if (count > ret) {
1120 count = ret;
1121 }
David Benjamin1a1b34d2015-10-17 12:51:52 -04001122 memcpy(buf, ssl->s3->tmp.peer_finish_md, count);
Adam Langleyfcf25832014-12-18 17:42:32 -08001123 }
Adam Langley95c29f32014-06-20 12:00:00 -07001124
Adam Langleyfcf25832014-12-18 17:42:32 -08001125 return ret;
1126}
Adam Langley95c29f32014-06-20 12:00:00 -07001127
David Benjamin59937042015-09-19 13:04:22 -04001128int SSL_get_verify_mode(const SSL *ssl) { return ssl->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -07001129
David Benjamin59937042015-09-19 13:04:22 -04001130int SSL_get_verify_depth(const SSL *ssl) {
1131 return X509_VERIFY_PARAM_get_depth(ssl->param);
Adam Langleyfcf25832014-12-18 17:42:32 -08001132}
Adam Langley95c29f32014-06-20 12:00:00 -07001133
David Benjamin42fea372015-09-19 01:22:44 -04001134int SSL_get_extms_support(const SSL *ssl) {
1135 return ssl->s3->tmp.extended_master_secret == 1;
Matt Braithwaitecd6f54b2015-09-17 12:54:42 -07001136}
1137
David Benjamin59937042015-09-19 13:04:22 -04001138int (*SSL_get_verify_callback(const SSL *ssl))(int, X509_STORE_CTX *) {
1139 return ssl->verify_callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08001140}
Adam Langley95c29f32014-06-20 12:00:00 -07001141
Adam Langleyfcf25832014-12-18 17:42:32 -08001142int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) { return ctx->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -07001143
Adam Langleyfcf25832014-12-18 17:42:32 -08001144int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) {
1145 return X509_VERIFY_PARAM_get_depth(ctx->param);
1146}
Adam Langley95c29f32014-06-20 12:00:00 -07001147
David Benjamin59937042015-09-19 13:04:22 -04001148int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(
1149 int ok, X509_STORE_CTX *store_ctx) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001150 return ctx->default_verify_callback;
1151}
Adam Langley95c29f32014-06-20 12:00:00 -07001152
David Benjamin59937042015-09-19 13:04:22 -04001153void SSL_set_verify(SSL *ssl, int mode,
1154 int (*callback)(int ok, X509_STORE_CTX *store_ctx)) {
1155 ssl->verify_mode = mode;
Adam Langleyfcf25832014-12-18 17:42:32 -08001156 if (callback != NULL) {
David Benjamin59937042015-09-19 13:04:22 -04001157 ssl->verify_callback = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08001158 }
1159}
Adam Langley95c29f32014-06-20 12:00:00 -07001160
David Benjamin59937042015-09-19 13:04:22 -04001161void SSL_set_verify_depth(SSL *ssl, int depth) {
1162 X509_VERIFY_PARAM_set_depth(ssl->param, depth);
Adam Langleyfcf25832014-12-18 17:42:32 -08001163}
Adam Langley95c29f32014-06-20 12:00:00 -07001164
David Benjamin9a41d1b2015-05-16 01:30:09 -04001165int SSL_CTX_get_read_ahead(const SSL_CTX *ctx) { return 0; }
Adam Langley95c29f32014-06-20 12:00:00 -07001166
David Benjamin9a41d1b2015-05-16 01:30:09 -04001167int SSL_get_read_ahead(const SSL *s) { return 0; }
Adam Langley95c29f32014-06-20 12:00:00 -07001168
David Benjamin9a41d1b2015-05-16 01:30:09 -04001169void SSL_CTX_set_read_ahead(SSL_CTX *ctx, int yes) { }
David Benjamin61ecccf2015-05-05 09:44:51 -04001170
David Benjamin9a41d1b2015-05-16 01:30:09 -04001171void SSL_set_read_ahead(SSL *s, int yes) { }
David Benjamin61ecccf2015-05-05 09:44:51 -04001172
David Benjamin9f859492015-10-03 10:44:30 -04001173int SSL_pending(const SSL *ssl) {
1174 if (ssl->s3->rrec.type != SSL3_RT_APPLICATION_DATA) {
1175 return 0;
1176 }
1177 return ssl->s3->rrec.length;
Adam Langleyfcf25832014-12-18 17:42:32 -08001178}
Adam Langley95c29f32014-06-20 12:00:00 -07001179
Adam Langley95c29f32014-06-20 12:00:00 -07001180/* Fix this so it checks all the valid key/cert options */
Adam Langleyfcf25832014-12-18 17:42:32 -08001181int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
David Benjamin651b3d92015-08-09 12:07:25 -04001182 if (ctx->cert->x509 == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001183 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -08001184 return 0;
1185 }
1186
David Benjamind1d80782015-07-05 11:54:09 -04001187 if (ctx->cert->privatekey == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001188 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -08001189 return 0;
1190 }
1191
David Benjamind1d80782015-07-05 11:54:09 -04001192 return X509_check_private_key(ctx->cert->x509, ctx->cert->privatekey);
Adam Langleyfcf25832014-12-18 17:42:32 -08001193}
Adam Langley95c29f32014-06-20 12:00:00 -07001194
1195/* Fix this function so that it takes an optional type parameter */
Adam Langleyfcf25832014-12-18 17:42:32 -08001196int SSL_check_private_key(const SSL *ssl) {
David Benjamind1d80782015-07-05 11:54:09 -04001197 if (ssl->cert->x509 == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001198 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -08001199 return 0;
1200 }
David Benjamin0b145c22014-11-26 20:10:09 -05001201
David Benjamind1d80782015-07-05 11:54:09 -04001202 if (ssl->cert->privatekey == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001203 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -08001204 return 0;
1205 }
Adam Langley95c29f32014-06-20 12:00:00 -07001206
David Benjamind1d80782015-07-05 11:54:09 -04001207 return X509_check_private_key(ssl->cert->x509, ssl->cert->privatekey);
Adam Langleyfcf25832014-12-18 17:42:32 -08001208}
Adam Langley95c29f32014-06-20 12:00:00 -07001209
David Benjamin42fea372015-09-19 01:22:44 -04001210long SSL_get_default_timeout(const SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001211 return SSL_DEFAULT_SESSION_TIMEOUT;
1212}
Adam Langley95c29f32014-06-20 12:00:00 -07001213
David Benjamin44d3eed2015-05-21 01:29:55 -04001214int SSL_renegotiate(SSL *ssl) {
1215 /* Caller-initiated renegotiation is not supported. */
David Benjamin3570d732015-06-29 00:28:17 -04001216 OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
David Benjamin44d3eed2015-05-21 01:29:55 -04001217 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001218}
Adam Langley95c29f32014-06-20 12:00:00 -07001219
David Benjamin44d3eed2015-05-21 01:29:55 -04001220int SSL_renegotiate_pending(SSL *ssl) {
1221 return SSL_in_init(ssl) && ssl->s3->initial_handshake_complete;
Adam Langleyfcf25832014-12-18 17:42:32 -08001222}
Adam Langley95c29f32014-06-20 12:00:00 -07001223
David Benjamin1d0a1942015-04-26 15:35:35 -04001224size_t SSL_CTX_get_max_cert_list(const SSL_CTX *ctx) {
1225 return ctx->max_cert_list;
1226}
1227
1228void SSL_CTX_set_max_cert_list(SSL_CTX *ctx, size_t max_cert_list) {
1229 if (max_cert_list > kMaxHandshakeSize) {
1230 max_cert_list = kMaxHandshakeSize;
1231 }
1232 ctx->max_cert_list = (uint32_t)max_cert_list;
1233}
1234
1235size_t SSL_get_max_cert_list(const SSL *ssl) {
1236 return ssl->max_cert_list;
1237}
1238
1239void SSL_set_max_cert_list(SSL *ssl, size_t max_cert_list) {
1240 if (max_cert_list > kMaxHandshakeSize) {
1241 max_cert_list = kMaxHandshakeSize;
1242 }
1243 ssl->max_cert_list = (uint32_t)max_cert_list;
1244}
1245
1246void SSL_CTX_set_max_send_fragment(SSL_CTX *ctx, size_t max_send_fragment) {
1247 if (max_send_fragment < 512) {
1248 max_send_fragment = 512;
1249 }
1250 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1251 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1252 }
1253 ctx->max_send_fragment = (uint16_t)max_send_fragment;
1254}
1255
1256void SSL_set_max_send_fragment(SSL *ssl, size_t max_send_fragment) {
1257 if (max_send_fragment < 512) {
1258 max_send_fragment = 512;
1259 }
1260 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1261 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1262 }
1263 ssl->max_send_fragment = (uint16_t)max_send_fragment;
1264}
1265
David Benjamincb9cf792015-05-05 09:46:14 -04001266int SSL_set_mtu(SSL *ssl, unsigned mtu) {
1267 if (!SSL_IS_DTLS(ssl) || mtu < dtls1_min_mtu()) {
1268 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001269 }
David Benjamincb9cf792015-05-05 09:46:14 -04001270 ssl->d1->mtu = mtu;
1271 return 1;
1272}
1273
1274int SSL_get_secure_renegotiation_support(const SSL *ssl) {
1275 return ssl->s3->send_connection_binding;
1276}
1277
Adam Langleyfcf25832014-12-18 17:42:32 -08001278LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) { return ctx->sessions; }
1279
David Benjamin71f7d3d2015-05-05 09:46:38 -04001280size_t SSL_CTX_sess_number(const SSL_CTX *ctx) {
1281 return lh_SSL_SESSION_num_items(ctx->sessions);
1282}
1283
1284unsigned long SSL_CTX_sess_set_cache_size(SSL_CTX *ctx, unsigned long size) {
1285 unsigned long ret = ctx->session_cache_size;
1286 ctx->session_cache_size = size;
1287 return ret;
1288}
1289
1290unsigned long SSL_CTX_sess_get_cache_size(const SSL_CTX *ctx) {
1291 return ctx->session_cache_size;
1292}
1293
1294int SSL_CTX_set_session_cache_mode(SSL_CTX *ctx, int mode) {
1295 int ret = ctx->session_cache_mode;
1296 ctx->session_cache_mode = mode;
1297 return ret;
1298}
1299
1300int SSL_CTX_get_session_cache_mode(const SSL_CTX *ctx) {
1301 return ctx->session_cache_mode;
1302}
1303
David Benjamin32876b32015-09-20 12:17:03 -04001304STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *ssl) {
1305 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001306 return NULL;
1307 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001308
David Benjamin32876b32015-09-20 12:17:03 -04001309 if (ssl->cipher_list != NULL) {
1310 return ssl->cipher_list->ciphers;
Adam Langleyfcf25832014-12-18 17:42:32 -08001311 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001312
David Benjamin32876b32015-09-20 12:17:03 -04001313 if (ssl->version >= TLS1_1_VERSION && ssl->ctx != NULL &&
1314 ssl->ctx->cipher_list_tls11 != NULL) {
1315 return ssl->ctx->cipher_list_tls11->ciphers;
Adam Langleyfcf25832014-12-18 17:42:32 -08001316 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001317
David Benjamin32876b32015-09-20 12:17:03 -04001318 if (ssl->version >= TLS1_VERSION && ssl->ctx != NULL &&
1319 ssl->ctx->cipher_list_tls10 != NULL) {
1320 return ssl->ctx->cipher_list_tls10->ciphers;
Adam Langleycef75832015-09-03 14:51:12 -07001321 }
1322
David Benjamin32876b32015-09-20 12:17:03 -04001323 if (ssl->ctx != NULL && ssl->ctx->cipher_list != NULL) {
1324 return ssl->ctx->cipher_list->ciphers;
Adam Langleyfcf25832014-12-18 17:42:32 -08001325 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001326
Adam Langleyfcf25832014-12-18 17:42:32 -08001327 return NULL;
1328}
Adam Langley95c29f32014-06-20 12:00:00 -07001329
Adam Langleyfcf25832014-12-18 17:42:32 -08001330/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001331 * algorithm id */
David Benjamin60da0cd2015-05-03 15:21:28 -04001332STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001333 if (s == NULL) {
1334 return NULL;
1335 }
Adam Langley95c29f32014-06-20 12:00:00 -07001336
Adam Langleyfcf25832014-12-18 17:42:32 -08001337 if (s->cipher_list_by_id != NULL) {
1338 return s->cipher_list_by_id;
1339 }
Adam Langley95c29f32014-06-20 12:00:00 -07001340
Adam Langleyfcf25832014-12-18 17:42:32 -08001341 if (s->ctx != NULL && s->ctx->cipher_list_by_id != NULL) {
1342 return s->ctx->cipher_list_by_id;
1343 }
Adam Langley95c29f32014-06-20 12:00:00 -07001344
Adam Langleyfcf25832014-12-18 17:42:32 -08001345 return NULL;
1346}
Adam Langley95c29f32014-06-20 12:00:00 -07001347
David Benjamin32876b32015-09-20 12:17:03 -04001348const char *SSL_get_cipher_list(const SSL *ssl, int n) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001349 const SSL_CIPHER *c;
David Benjamin60da0cd2015-05-03 15:21:28 -04001350 STACK_OF(SSL_CIPHER) *sk;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001351
David Benjamin32876b32015-09-20 12:17:03 -04001352 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001353 return NULL;
1354 }
Adam Langley95c29f32014-06-20 12:00:00 -07001355
David Benjamin32876b32015-09-20 12:17:03 -04001356 sk = SSL_get_ciphers(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001357 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
1358 return NULL;
1359 }
Adam Langley95c29f32014-06-20 12:00:00 -07001360
Adam Langleyfcf25832014-12-18 17:42:32 -08001361 c = sk_SSL_CIPHER_value(sk, n);
1362 if (c == NULL) {
1363 return NULL;
1364 }
Adam Langley95c29f32014-06-20 12:00:00 -07001365
Adam Langleyfcf25832014-12-18 17:42:32 -08001366 return c->name;
1367}
David Benjamin5491e3f2014-09-29 19:33:09 -04001368
Adam Langleyfcf25832014-12-18 17:42:32 -08001369int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
David Benjamin32876b32015-09-20 12:17:03 -04001370 STACK_OF(SSL_CIPHER) *cipher_list = ssl_create_cipher_list(
1371 ctx->method, &ctx->cipher_list, &ctx->cipher_list_by_id, str);
1372 if (cipher_list == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001373 return 0;
David Benjamin32876b32015-09-20 12:17:03 -04001374 }
1375
1376 /* |ssl_create_cipher_list| may succeed but return an empty cipher list. */
1377 if (sk_SSL_CIPHER_num(cipher_list) == 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001378 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
Adam Langleyfcf25832014-12-18 17:42:32 -08001379 return 0;
1380 }
Adam Langley95c29f32014-06-20 12:00:00 -07001381
Adam Langleyfcf25832014-12-18 17:42:32 -08001382 return 1;
1383}
David Benjamin39482a12014-07-20 13:30:15 -04001384
Adam Langleycef75832015-09-03 14:51:12 -07001385int SSL_CTX_set_cipher_list_tls10(SSL_CTX *ctx, const char *str) {
David Benjamin32876b32015-09-20 12:17:03 -04001386 STACK_OF(SSL_CIPHER) *cipher_list = ssl_create_cipher_list(
1387 ctx->method, &ctx->cipher_list_tls10, NULL, str);
1388 if (cipher_list == NULL) {
Adam Langleycef75832015-09-03 14:51:12 -07001389 return 0;
David Benjamin32876b32015-09-20 12:17:03 -04001390 }
1391
1392 /* |ssl_create_cipher_list| may succeed but return an empty cipher list. */
1393 if (sk_SSL_CIPHER_num(cipher_list) == 0) {
Adam Langleycef75832015-09-03 14:51:12 -07001394 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
1395 return 0;
1396 }
1397
1398 return 1;
1399}
1400
Adam Langleyfcf25832014-12-18 17:42:32 -08001401int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str) {
David Benjamin32876b32015-09-20 12:17:03 -04001402 STACK_OF(SSL_CIPHER) *cipher_list = ssl_create_cipher_list(
1403 ctx->method, &ctx->cipher_list_tls11, NULL, str);
1404 if (cipher_list == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001405 return 0;
David Benjamin32876b32015-09-20 12:17:03 -04001406 }
1407
1408 /* |ssl_create_cipher_list| may succeed but return an empty cipher list. */
1409 if (sk_SSL_CIPHER_num(cipher_list) == 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 }
David Benjamin9f2c0d72014-10-21 22:00:19 -04001413
Adam Langleyfcf25832014-12-18 17:42:32 -08001414 return 1;
1415}
Adam Langley95c29f32014-06-20 12:00:00 -07001416
David Benjamin32876b32015-09-20 12:17:03 -04001417int SSL_set_cipher_list(SSL *ssl, const char *str) {
1418 STACK_OF(SSL_CIPHER) *cipher_list = ssl_create_cipher_list(
1419 ssl->ctx->method, &ssl->cipher_list, &ssl->cipher_list_by_id, str);
1420 if (cipher_list == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001421 return 0;
David Benjamin32876b32015-09-20 12:17:03 -04001422 }
1423
1424 /* |ssl_create_cipher_list| may succeed but return an empty cipher list. */
1425 if (sk_SSL_CIPHER_num(cipher_list) == 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001426 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
Adam Langleyfcf25832014-12-18 17:42:32 -08001427 return 0;
1428 }
David Benjamin39482a12014-07-20 13:30:15 -04001429
Adam Langleyfcf25832014-12-18 17:42:32 -08001430 return 1;
1431}
Adam Langley95c29f32014-06-20 12:00:00 -07001432
Adam Langleyfcf25832014-12-18 17:42:32 -08001433STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs) {
1434 CBS cipher_suites = *cbs;
1435 const SSL_CIPHER *c;
David Benjamin60da0cd2015-05-03 15:21:28 -04001436 STACK_OF(SSL_CIPHER) *sk;
Adam Langleyfcf25832014-12-18 17:42:32 -08001437
1438 if (s->s3) {
1439 s->s3->send_connection_binding = 0;
1440 }
1441
1442 if (CBS_len(&cipher_suites) % 2 != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001443 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
Adam Langleyfcf25832014-12-18 17:42:32 -08001444 return NULL;
1445 }
1446
1447 sk = sk_SSL_CIPHER_new_null();
1448 if (sk == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001449 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001450 goto err;
1451 }
1452
Adam Langleyfcf25832014-12-18 17:42:32 -08001453 while (CBS_len(&cipher_suites) > 0) {
1454 uint16_t cipher_suite;
1455
1456 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
David Benjamin3570d732015-06-29 00:28:17 -04001457 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001458 goto err;
1459 }
1460
1461 /* Check for SCSV. */
1462 if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff)) {
1463 /* SCSV is fatal if renegotiating. */
David Benjamin20f6e972015-05-15 21:51:49 -04001464 if (s->s3->initial_handshake_complete) {
David Benjamin3570d732015-06-29 00:28:17 -04001465 OPENSSL_PUT_ERROR(SSL, SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
Adam Langleyfcf25832014-12-18 17:42:32 -08001466 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1467 goto err;
1468 }
1469 s->s3->send_connection_binding = 1;
1470 continue;
1471 }
1472
1473 /* Check for FALLBACK_SCSV. */
1474 if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff)) {
1475 uint16_t max_version = ssl3_get_max_server_version(s);
1476 if (SSL_IS_DTLS(s) ? (uint16_t)s->version > max_version
1477 : (uint16_t)s->version < max_version) {
David Benjamin3570d732015-06-29 00:28:17 -04001478 OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK);
Adam Langleyfcf25832014-12-18 17:42:32 -08001479 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
1480 goto err;
1481 }
1482 continue;
1483 }
1484
David Benjamina1c90a52015-05-30 17:03:14 -04001485 c = SSL_get_cipher_by_value(cipher_suite);
Adam Langleyfcf25832014-12-18 17:42:32 -08001486 if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
David Benjamin3570d732015-06-29 00:28:17 -04001487 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001488 goto err;
1489 }
1490 }
1491
1492 return sk;
David Benjamin9f2c0d72014-10-21 22:00:19 -04001493
Adam Langley95c29f32014-06-20 12:00:00 -07001494err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001495 sk_SSL_CIPHER_free(sk);
Adam Langleyfcf25832014-12-18 17:42:32 -08001496 return NULL;
1497}
Adam Langley95c29f32014-06-20 12:00:00 -07001498
David Benjamin07e13842015-10-17 13:48:04 -04001499const char *SSL_get_servername(const SSL *ssl, const int type) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001500 if (type != TLSEXT_NAMETYPE_host_name) {
1501 return NULL;
1502 }
Adam Langley95c29f32014-06-20 12:00:00 -07001503
David Benjamin07e13842015-10-17 13:48:04 -04001504 /* Historically, |SSL_get_servername| was also the configuration getter
1505 * corresponding to |SSL_set_tlsext_host_name|. */
1506 if (ssl->tlsext_hostname != NULL) {
1507 return ssl->tlsext_hostname;
1508 }
1509
1510 if (ssl->session == NULL) {
1511 return NULL;
1512 }
1513 return ssl->session->tlsext_hostname;
Adam Langleyfcf25832014-12-18 17:42:32 -08001514}
Adam Langley95c29f32014-06-20 12:00:00 -07001515
David Benjamin07e13842015-10-17 13:48:04 -04001516int SSL_get_servername_type(const SSL *ssl) {
1517 if (ssl->session != NULL && ssl->session->tlsext_hostname != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001518 return TLSEXT_NAMETYPE_host_name;
1519 }
Adam Langley95c29f32014-06-20 12:00:00 -07001520
Adam Langleyfcf25832014-12-18 17:42:32 -08001521 return -1;
1522}
Adam Langley95c29f32014-06-20 12:00:00 -07001523
Adam Langleyfcf25832014-12-18 17:42:32 -08001524void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
1525 ctx->signed_cert_timestamps_enabled = 1;
1526}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001527
Adam Langleyfcf25832014-12-18 17:42:32 -08001528int SSL_enable_signed_cert_timestamps(SSL *ssl) {
1529 ssl->signed_cert_timestamps_enabled = 1;
1530 return 1;
1531}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001532
Adam Langleyfcf25832014-12-18 17:42:32 -08001533void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
1534 ctx->ocsp_stapling_enabled = 1;
1535}
David Benjamin6c7aed02014-08-27 16:42:38 -04001536
Adam Langleyfcf25832014-12-18 17:42:32 -08001537int SSL_enable_ocsp_stapling(SSL *ssl) {
1538 ssl->ocsp_stapling_enabled = 1;
1539 return 1;
1540}
David Benjamin6c7aed02014-08-27 16:42:38 -04001541
Adam Langleyfcf25832014-12-18 17:42:32 -08001542void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
1543 size_t *out_len) {
1544 SSL_SESSION *session = ssl->session;
Adam Langley3cb50e02014-08-26 14:00:31 -07001545
Adam Langleyfcf25832014-12-18 17:42:32 -08001546 *out_len = 0;
1547 *out = NULL;
1548 if (ssl->server || !session || !session->tlsext_signed_cert_timestamp_list) {
1549 return;
1550 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001551
Adam Langleyfcf25832014-12-18 17:42:32 -08001552 *out = session->tlsext_signed_cert_timestamp_list;
1553 *out_len = session->tlsext_signed_cert_timestamp_list_length;
1554}
David Benjamin6c7aed02014-08-27 16:42:38 -04001555
Adam Langleyfcf25832014-12-18 17:42:32 -08001556void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
1557 size_t *out_len) {
1558 SSL_SESSION *session = ssl->session;
1559
1560 *out_len = 0;
1561 *out = NULL;
1562 if (ssl->server || !session || !session->ocsp_response) {
1563 return;
1564 }
1565 *out = session->ocsp_response;
1566 *out_len = session->ocsp_response_length;
1567}
David Benjamin6c7aed02014-08-27 16:42:38 -04001568
Paul Lietar4fac72e2015-09-09 13:44:55 +01001569int SSL_CTX_set_signed_cert_timestamp_list(SSL_CTX *ctx, const uint8_t *list,
1570 size_t list_len) {
1571 OPENSSL_free(ctx->signed_cert_timestamp_list);
1572 ctx->signed_cert_timestamp_list_length = 0;
1573
1574 ctx->signed_cert_timestamp_list = BUF_memdup(list, list_len);
1575 if (ctx->signed_cert_timestamp_list == NULL) {
1576 return 0;
1577 }
1578 ctx->signed_cert_timestamp_list_length = list_len;
1579
1580 return 1;
1581}
1582
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001583int SSL_CTX_set_ocsp_response(SSL_CTX *ctx, const uint8_t *response,
1584 size_t response_len) {
1585 OPENSSL_free(ctx->ocsp_response);
1586 ctx->ocsp_response_length = 0;
1587
1588 ctx->ocsp_response = BUF_memdup(response, response_len);
1589 if (ctx->ocsp_response == NULL) {
1590 return 0;
1591 }
1592 ctx->ocsp_response_length = response_len;
1593
1594 return 1;
1595}
1596
David Benjamin977547b2015-09-16 00:25:52 -04001597int SSL_select_next_proto(uint8_t **out, uint8_t *out_len,
1598 const uint8_t *server, unsigned server_len,
1599 const uint8_t *client, unsigned client_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001600 unsigned int i, j;
1601 const uint8_t *result;
1602 int status = OPENSSL_NPN_UNSUPPORTED;
Adam Langley95c29f32014-06-20 12:00:00 -07001603
Adam Langleyfcf25832014-12-18 17:42:32 -08001604 /* For each protocol in server preference order, see if we support it. */
1605 for (i = 0; i < server_len;) {
1606 for (j = 0; j < client_len;) {
1607 if (server[i] == client[j] &&
1608 memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
1609 /* We found a match */
1610 result = &server[i];
1611 status = OPENSSL_NPN_NEGOTIATED;
1612 goto found;
1613 }
1614 j += client[j];
1615 j++;
1616 }
1617 i += server[i];
1618 i++;
1619 }
Adam Langley95c29f32014-06-20 12:00:00 -07001620
Adam Langleyfcf25832014-12-18 17:42:32 -08001621 /* There's no overlap between our protocols and the server's list. */
1622 result = client;
1623 status = OPENSSL_NPN_NO_OVERLAP;
Adam Langley95c29f32014-06-20 12:00:00 -07001624
Adam Langleyfcf25832014-12-18 17:42:32 -08001625found:
1626 *out = (uint8_t *)result + 1;
David Benjamin977547b2015-09-16 00:25:52 -04001627 *out_len = result[0];
Adam Langleyfcf25832014-12-18 17:42:32 -08001628 return status;
1629}
Adam Langley95c29f32014-06-20 12:00:00 -07001630
David Benjamin977547b2015-09-16 00:25:52 -04001631void SSL_get0_next_proto_negotiated(const SSL *ssl, const uint8_t **out_data,
1632 unsigned *out_len) {
1633 *out_data = ssl->next_proto_negotiated;
1634 if (*out_data == NULL) {
1635 *out_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001636 } else {
David Benjamin977547b2015-09-16 00:25:52 -04001637 *out_len = ssl->next_proto_negotiated_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08001638 }
Adam Langley95c29f32014-06-20 12:00:00 -07001639}
1640
Adam Langleyfcf25832014-12-18 17:42:32 -08001641void SSL_CTX_set_next_protos_advertised_cb(
1642 SSL_CTX *ctx,
David Benjamin977547b2015-09-16 00:25:52 -04001643 int (*cb)(SSL *ssl, const uint8_t **out, unsigned *out_len, void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08001644 void *arg) {
1645 ctx->next_protos_advertised_cb = cb;
1646 ctx->next_protos_advertised_cb_arg = arg;
1647}
Adam Langley95c29f32014-06-20 12:00:00 -07001648
Adam Langleyfcf25832014-12-18 17:42:32 -08001649void SSL_CTX_set_next_proto_select_cb(
David Benjamin977547b2015-09-16 00:25:52 -04001650 SSL_CTX *ctx, int (*cb)(SSL *ssl, uint8_t **out, uint8_t *out_len,
1651 const uint8_t *in, unsigned in_len, void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08001652 void *arg) {
1653 ctx->next_proto_select_cb = cb;
1654 ctx->next_proto_select_cb_arg = arg;
1655}
Adam Langley95c29f32014-06-20 12:00:00 -07001656
Adam Langleyfcf25832014-12-18 17:42:32 -08001657int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
1658 unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001659 OPENSSL_free(ctx->alpn_client_proto_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001660 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1661 if (!ctx->alpn_client_proto_list) {
1662 return 1;
1663 }
1664 ctx->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001665
Adam Langleyfcf25832014-12-18 17:42:32 -08001666 return 0;
1667}
Adam Langley95c29f32014-06-20 12:00:00 -07001668
Adam Langleyfcf25832014-12-18 17:42:32 -08001669int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001670 OPENSSL_free(ssl->alpn_client_proto_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001671 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1672 if (!ssl->alpn_client_proto_list) {
1673 return 1;
1674 }
1675 ssl->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001676
Adam Langleyfcf25832014-12-18 17:42:32 -08001677 return 0;
1678}
Adam Langley95c29f32014-06-20 12:00:00 -07001679
Adam Langleyfcf25832014-12-18 17:42:32 -08001680void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
1681 int (*cb)(SSL *ssl, const uint8_t **out,
David Benjamin8984f1f2015-09-16 00:10:19 -04001682 uint8_t *out_len, const uint8_t *in,
1683 unsigned in_len, void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08001684 void *arg) {
1685 ctx->alpn_select_cb = cb;
1686 ctx->alpn_select_cb_arg = arg;
1687}
Adam Langley95c29f32014-06-20 12:00:00 -07001688
David Benjamin8984f1f2015-09-16 00:10:19 -04001689void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **out_data,
1690 unsigned *out_len) {
1691 *out_data = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08001692 if (ssl->s3) {
David Benjamin8984f1f2015-09-16 00:10:19 -04001693 *out_data = ssl->s3->alpn_selected;
Adam Langleyfcf25832014-12-18 17:42:32 -08001694 }
David Benjamin8984f1f2015-09-16 00:10:19 -04001695 if (*out_data == NULL) {
1696 *out_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001697 } else {
David Benjamin8984f1f2015-09-16 00:10:19 -04001698 *out_len = ssl->s3->alpn_selected_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08001699 }
1700}
Adam Langley95c29f32014-06-20 12:00:00 -07001701
David Benjamin07e13842015-10-17 13:48:04 -04001702int SSL_export_keying_material(SSL *ssl, uint8_t *out, size_t out_len,
David Benjamincfd248b2015-04-03 11:02:24 -04001703 const char *label, size_t label_len,
1704 const uint8_t *context, size_t context_len,
1705 int use_context) {
David Benjamin07e13842015-10-17 13:48:04 -04001706 if (ssl->version < TLS1_VERSION) {
David Benjaminc565ebb2015-04-03 04:06:36 -04001707 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001708 }
Adam Langley95c29f32014-06-20 12:00:00 -07001709
David Benjamin07e13842015-10-17 13:48:04 -04001710 return ssl->enc_method->export_keying_material(
1711 ssl, out, out_len, label, label_len, context, context_len, use_context);
Adam Langleyfcf25832014-12-18 17:42:32 -08001712}
Adam Langley95c29f32014-06-20 12:00:00 -07001713
Adam Langleyfcf25832014-12-18 17:42:32 -08001714void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
David Benjamin59937042015-09-19 13:04:22 -04001715 int (*cb)(X509_STORE_CTX *store_ctx,
1716 void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08001717 void *arg) {
1718 ctx->app_verify_callback = cb;
1719 ctx->app_verify_arg = arg;
1720}
Adam Langley95c29f32014-06-20 12:00:00 -07001721
Adam Langleyfcf25832014-12-18 17:42:32 -08001722void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
1723 int (*cb)(int, X509_STORE_CTX *)) {
1724 ctx->verify_mode = mode;
1725 ctx->default_verify_callback = cb;
1726}
Adam Langley95c29f32014-06-20 12:00:00 -07001727
Adam Langleyfcf25832014-12-18 17:42:32 -08001728void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
1729 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
1730}
Adam Langley1258b6a2014-06-20 12:00:00 -07001731
David Benjamin7481d392015-07-05 19:38:46 -04001732void SSL_CTX_set_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl, void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08001733 void *arg) {
David Benjamin7481d392015-07-05 19:38:46 -04001734 ssl_cert_set_cert_cb(ctx->cert, cb, arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08001735}
David Benjamin859ec3c2014-09-02 16:29:36 -04001736
David Benjamin7481d392015-07-05 19:38:46 -04001737void SSL_set_cert_cb(SSL *ssl, int (*cb)(SSL *ssl, void *arg), void *arg) {
1738 ssl_cert_set_cert_cb(ssl->cert, cb, arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08001739}
Adam Langley95c29f32014-06-20 12:00:00 -07001740
David Benjamin107db582015-04-08 00:41:59 -04001741void ssl_get_compatible_server_ciphers(SSL *s, uint32_t *out_mask_k,
1742 uint32_t *out_mask_a) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001743 CERT *c = s->cert;
David Benjamind1d80782015-07-05 11:54:09 -04001744 int have_rsa_cert = 0, dh_tmp;
David Benjamin107db582015-04-08 00:41:59 -04001745 uint32_t mask_k, mask_a;
David Benjamind1d80782015-07-05 11:54:09 -04001746 int have_ecc_cert = 0, ecdsa_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001747 X509 *x;
Adam Langley95c29f32014-06-20 12:00:00 -07001748
Adam Langleyfcf25832014-12-18 17:42:32 -08001749 dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001750
nagendra modadugu601448a2015-07-24 09:31:31 -07001751 if (s->cert->x509 != NULL && ssl_has_private_key(s)) {
1752 if (ssl_private_key_type(s) == EVP_PKEY_RSA) {
David Benjamind1d80782015-07-05 11:54:09 -04001753 have_rsa_cert = 1;
nagendra modadugu601448a2015-07-24 09:31:31 -07001754 } else if (ssl_private_key_type(s) == EVP_PKEY_EC) {
David Benjamind1d80782015-07-05 11:54:09 -04001755 have_ecc_cert = 1;
1756 }
1757 }
nagendra modadugu601448a2015-07-24 09:31:31 -07001758
Adam Langleyfcf25832014-12-18 17:42:32 -08001759 mask_k = 0;
1760 mask_a = 0;
David Benjaminf31e6812014-11-13 18:05:55 -05001761
Adam Langleyfcf25832014-12-18 17:42:32 -08001762 if (dh_tmp) {
David Benjamin7061e282015-03-19 11:10:48 -04001763 mask_k |= SSL_kDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001764 }
David Benjaminbb20f522015-07-04 17:18:14 -04001765 if (have_rsa_cert) {
1766 mask_k |= SSL_kRSA;
Adam Langleyfcf25832014-12-18 17:42:32 -08001767 mask_a |= SSL_aRSA;
1768 }
Adam Langley95c29f32014-06-20 12:00:00 -07001769
Adam Langleyfcf25832014-12-18 17:42:32 -08001770 /* An ECC certificate may be usable for ECDSA cipher suites depending on the
1771 * key usage extension and on the client's curve preferences. */
1772 if (have_ecc_cert) {
David Benjamind1d80782015-07-05 11:54:09 -04001773 x = c->x509;
Adam Langleyfcf25832014-12-18 17:42:32 -08001774 /* This call populates extension flags (ex_flags). */
1775 X509_check_purpose(x, -1, 0);
1776 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE)
1777 ? (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)
1778 : 1;
1779 if (!tls1_check_ec_cert(s, x)) {
1780 ecdsa_ok = 0;
1781 }
1782 if (ecdsa_ok) {
1783 mask_a |= SSL_aECDSA;
1784 }
1785 }
Adam Langley95c29f32014-06-20 12:00:00 -07001786
Adam Langleyfcf25832014-12-18 17:42:32 -08001787 /* If we are considering an ECC cipher suite that uses an ephemeral EC
David Benjamin5ddffbb2015-12-04 21:08:47 -05001788 * key, check for a shared curve. */
1789 if (tls1_get_shared_curve(s) != NID_undef) {
David Benjamin7061e282015-03-19 11:10:48 -04001790 mask_k |= SSL_kECDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001791 }
Adam Langley95c29f32014-06-20 12:00:00 -07001792
Adam Langleyfcf25832014-12-18 17:42:32 -08001793 /* PSK requires a server callback. */
1794 if (s->psk_server_callback != NULL) {
1795 mask_k |= SSL_kPSK;
1796 mask_a |= SSL_aPSK;
1797 }
Adam Langley95c29f32014-06-20 12:00:00 -07001798
Adam Langleyfcf25832014-12-18 17:42:32 -08001799 *out_mask_k = mask_k;
1800 *out_mask_a = mask_a;
1801}
Adam Langley95c29f32014-06-20 12:00:00 -07001802
David Benjamin1269ddd2015-10-18 15:18:55 -04001803void ssl_update_cache(SSL *ssl, int mode) {
1804 SSL_CTX *ctx = ssl->initial_ctx;
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001805 /* Never cache sessions with empty session IDs. */
David Benjamin1269ddd2015-10-18 15:18:55 -04001806 if (ssl->session->session_id_length == 0 ||
1807 (ctx->session_cache_mode & mode) != mode) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001808 return;
1809 }
Adam Langley95c29f32014-06-20 12:00:00 -07001810
David Benjamin1269ddd2015-10-18 15:18:55 -04001811 /* Clients never use the internal session cache. */
1812 int use_internal_cache = ssl->server && !(ctx->session_cache_mode &
1813 SSL_SESS_CACHE_NO_INTERNAL_STORE);
David Benjamin95d31822015-06-15 19:53:32 -04001814
David Benjamin1269ddd2015-10-18 15:18:55 -04001815 /* A client may see new sessions on abbreviated handshakes if the server
1816 * decides to renew the ticket. Once the handshake is completed, it should be
1817 * inserted into the cache. */
1818 if (!ssl->hit || (!ssl->server && ssl->tlsext_ticket_expected)) {
1819 if (use_internal_cache) {
1820 SSL_CTX_add_session(ctx, ssl->session);
1821 }
1822 if (ctx->new_session_cb != NULL &&
1823 !ctx->new_session_cb(ssl, SSL_SESSION_up_ref(ssl->session))) {
1824 /* |new_session_cb|'s return value signals whether it took ownership. */
1825 SSL_SESSION_free(ssl->session);
Adam Langleyfcf25832014-12-18 17:42:32 -08001826 }
1827 }
Adam Langley95c29f32014-06-20 12:00:00 -07001828
David Benjamin1269ddd2015-10-18 15:18:55 -04001829 if (use_internal_cache &&
1830 !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR)) {
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001831 /* Automatically flush the internal session cache every 255 connections. */
1832 int flush_cache = 0;
Adam Langley4bdb6e42015-05-15 15:29:21 -07001833 CRYPTO_MUTEX_lock_write(&ctx->lock);
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001834 ctx->handshakes_since_cache_flush++;
1835 if (ctx->handshakes_since_cache_flush >= 255) {
1836 flush_cache = 1;
1837 ctx->handshakes_since_cache_flush = 0;
1838 }
Adam Langley4bdb6e42015-05-15 15:29:21 -07001839 CRYPTO_MUTEX_unlock(&ctx->lock);
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001840
1841 if (flush_cache) {
1842 SSL_CTX_flush_sessions(ctx, (unsigned long)time(NULL));
Adam Langleyfcf25832014-12-18 17:42:32 -08001843 }
1844 }
1845}
Adam Langley95c29f32014-06-20 12:00:00 -07001846
Adam Langleyfcf25832014-12-18 17:42:32 -08001847static const char *ssl_get_version(int version) {
1848 switch (version) {
1849 case TLS1_2_VERSION:
1850 return "TLSv1.2";
Adam Langley95c29f32014-06-20 12:00:00 -07001851
Adam Langleyfcf25832014-12-18 17:42:32 -08001852 case TLS1_1_VERSION:
1853 return "TLSv1.1";
Adam Langley95c29f32014-06-20 12:00:00 -07001854
Adam Langleyfcf25832014-12-18 17:42:32 -08001855 case TLS1_VERSION:
1856 return "TLSv1";
Adam Langley95c29f32014-06-20 12:00:00 -07001857
Adam Langleyfcf25832014-12-18 17:42:32 -08001858 case SSL3_VERSION:
1859 return "SSLv3";
Adam Langley95c29f32014-06-20 12:00:00 -07001860
David Benjamin1c722b72015-04-20 13:53:10 -04001861 case DTLS1_VERSION:
1862 return "DTLSv1";
1863
1864 case DTLS1_2_VERSION:
1865 return "DTLSv1.2";
1866
Adam Langleyfcf25832014-12-18 17:42:32 -08001867 default:
1868 return "unknown";
1869 }
1870}
Adam Langley95c29f32014-06-20 12:00:00 -07001871
David Benjamin42fea372015-09-19 01:22:44 -04001872const char *SSL_get_version(const SSL *ssl) {
1873 return ssl_get_version(ssl->version);
Adam Langleyfcf25832014-12-18 17:42:32 -08001874}
Adam Langley95c29f32014-06-20 12:00:00 -07001875
David Benjamina6b8cdf2015-09-13 14:07:33 -04001876const char *SSL_SESSION_get_version(const SSL_SESSION *session) {
1877 return ssl_get_version(session->ssl_version);
Adam Langleyfcf25832014-12-18 17:42:32 -08001878}
Adam Langley95c29f32014-06-20 12:00:00 -07001879
Sigbjorn Vik2b23d242015-06-29 15:07:26 +02001880const char* SSL_get_curve_name(uint16_t curve_id) {
1881 return tls1_ec_curve_id2name(curve_id);
1882}
1883
Adam Langleyfcf25832014-12-18 17:42:32 -08001884void ssl_clear_cipher_ctx(SSL *s) {
David Benjamin31a07792015-03-03 14:20:26 -05001885 SSL_AEAD_CTX_free(s->aead_read_ctx);
1886 s->aead_read_ctx = NULL;
1887 SSL_AEAD_CTX_free(s->aead_write_ctx);
1888 s->aead_write_ctx = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08001889}
Adam Langley95c29f32014-06-20 12:00:00 -07001890
Adam Langleyfcf25832014-12-18 17:42:32 -08001891X509 *SSL_get_certificate(const SSL *s) {
1892 if (s->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04001893 return s->cert->x509;
Adam Langleyfcf25832014-12-18 17:42:32 -08001894 }
1895
1896 return NULL;
1897}
1898
1899EVP_PKEY *SSL_get_privatekey(const SSL *s) {
1900 if (s->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04001901 return s->cert->privatekey;
Adam Langleyfcf25832014-12-18 17:42:32 -08001902 }
1903
1904 return NULL;
1905}
1906
1907X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
1908 if (ctx->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04001909 return ctx->cert->x509;
Adam Langleyfcf25832014-12-18 17:42:32 -08001910 }
1911
1912 return NULL;
1913}
1914
1915EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
1916 if (ctx->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04001917 return ctx->cert->privatekey;
Adam Langleyfcf25832014-12-18 17:42:32 -08001918 }
1919
1920 return NULL;
1921}
1922
David Benjamin42fea372015-09-19 01:22:44 -04001923const SSL_CIPHER *SSL_get_current_cipher(const SSL *ssl) {
1924 if (ssl->aead_write_ctx == NULL) {
David Benjamina07c0fc2015-05-13 13:19:42 -04001925 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08001926 }
David Benjamin42fea372015-09-19 01:22:44 -04001927 return ssl->aead_write_ctx->cipher;
Adam Langleyfcf25832014-12-18 17:42:32 -08001928}
1929
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07001930const COMP_METHOD *SSL_get_current_compression(SSL *s) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08001931
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07001932const COMP_METHOD *SSL_get_current_expansion(SSL *s) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08001933
1934int ssl_init_wbio_buffer(SSL *s, int push) {
1935 BIO *bbio;
1936
1937 if (s->bbio == NULL) {
1938 bbio = BIO_new(BIO_f_buffer());
1939 if (bbio == NULL) {
1940 return 0;
1941 }
1942 s->bbio = bbio;
1943 } else {
1944 bbio = s->bbio;
1945 if (s->bbio == s->wbio) {
1946 s->wbio = BIO_pop(s->wbio);
1947 }
1948 }
1949
1950 BIO_reset(bbio);
1951 if (!BIO_set_read_buffer_size(bbio, 1)) {
David Benjamin3570d732015-06-29 00:28:17 -04001952 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -08001953 return 0;
1954 }
1955
1956 if (push) {
1957 if (s->wbio != bbio) {
1958 s->wbio = BIO_push(bbio, s->wbio);
1959 }
1960 } else {
1961 if (s->wbio == bbio) {
1962 s->wbio = BIO_pop(bbio);
1963 }
1964 }
1965
1966 return 1;
1967}
1968
1969void ssl_free_wbio_buffer(SSL *s) {
1970 if (s->bbio == NULL) {
1971 return;
1972 }
1973
1974 if (s->bbio == s->wbio) {
1975 /* remove buffering */
1976 s->wbio = BIO_pop(s->wbio);
1977 }
1978
1979 BIO_free(s->bbio);
1980 s->bbio = NULL;
1981}
1982
1983void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
Adam Langleybb85f3d2015-10-28 18:44:11 -07001984 ctx->quiet_shutdown = (mode != 0);
Adam Langleyfcf25832014-12-18 17:42:32 -08001985}
1986
1987int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
1988 return ctx->quiet_shutdown;
1989}
1990
Adam Langleybb85f3d2015-10-28 18:44:11 -07001991void SSL_set_quiet_shutdown(SSL *ssl, int mode) {
1992 ssl->quiet_shutdown = (mode != 0);
1993}
Adam Langleyfcf25832014-12-18 17:42:32 -08001994
David Benjamin9f859492015-10-03 10:44:30 -04001995int SSL_get_quiet_shutdown(const SSL *ssl) { return ssl->quiet_shutdown; }
Adam Langleyfcf25832014-12-18 17:42:32 -08001996
David Benjamin63006a92015-10-18 00:00:06 -04001997void SSL_set_shutdown(SSL *ssl, int mode) {
1998 /* It is an error to clear any bits that have already been set. (We can't try
1999 * to get a second close_notify or send two.) */
Piotr Sikora7063b6d2015-10-29 02:47:40 -07002000 assert((ssl->shutdown & mode) == ssl->shutdown);
David Benjamin63006a92015-10-18 00:00:06 -04002001
2002 ssl->shutdown |= mode;
2003}
Adam Langleyfcf25832014-12-18 17:42:32 -08002004
David Benjamin9f859492015-10-03 10:44:30 -04002005int SSL_get_shutdown(const SSL *ssl) { return ssl->shutdown; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002006
David Benjamin9f859492015-10-03 10:44:30 -04002007int SSL_version(const SSL *ssl) { return ssl->version; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002008
2009SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
2010
2011SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
2012 if (ssl->ctx == ctx) {
2013 return ssl->ctx;
2014 }
2015
2016 if (ctx == NULL) {
2017 ctx = ssl->initial_ctx;
2018 }
2019
David Benjamin2755a3e2015-04-22 16:17:58 -04002020 ssl_cert_free(ssl->cert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002021 ssl->cert = ssl_cert_dup(ctx->cert);
David Benjamin2755a3e2015-04-22 16:17:58 -04002022
Adam Langley0b5e3902015-05-15 13:08:38 -07002023 CRYPTO_refcount_inc(&ctx->references);
David Benjamin2755a3e2015-04-22 16:17:58 -04002024 SSL_CTX_free(ssl->ctx); /* decrement reference count */
Adam Langleyfcf25832014-12-18 17:42:32 -08002025 ssl->ctx = ctx;
2026
2027 ssl->sid_ctx_length = ctx->sid_ctx_length;
2028 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2029 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2030
2031 return ssl->ctx;
2032}
2033
2034int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
2035 return X509_STORE_set_default_paths(ctx->cert_store);
2036}
Adam Langley95c29f32014-06-20 12:00:00 -07002037
David Benjamin59937042015-09-19 13:04:22 -04002038int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *ca_file,
2039 const char *ca_dir) {
2040 return X509_STORE_load_locations(ctx->cert_store, ca_file, ca_dir);
Adam Langleyfcf25832014-12-18 17:42:32 -08002041}
Adam Langley95c29f32014-06-20 12:00:00 -07002042
2043void SSL_set_info_callback(SSL *ssl,
David Benjamin82170242015-10-17 22:51:17 -04002044 void (*cb)(const SSL *ssl, int type, int value)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002045 ssl->info_callback = cb;
2046}
Adam Langley95c29f32014-06-20 12:00:00 -07002047
David Benjamin82170242015-10-17 22:51:17 -04002048void (*SSL_get_info_callback(const SSL *ssl))(const SSL *ssl, int type,
2049 int value) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002050 return ssl->info_callback;
2051}
Adam Langley95c29f32014-06-20 12:00:00 -07002052
Adam Langleyfcf25832014-12-18 17:42:32 -08002053int SSL_state(const SSL *ssl) { return ssl->state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002054
David Benjaminece089c2015-05-15 23:52:42 -04002055void SSL_set_state(SSL *ssl, int state) { }
Adam Langley95c29f32014-06-20 12:00:00 -07002056
David Benjamin59937042015-09-19 13:04:22 -04002057void SSL_set_verify_result(SSL *ssl, long result) {
2058 ssl->verify_result = result;
2059}
Adam Langley95c29f32014-06-20 12:00:00 -07002060
Adam Langleyfcf25832014-12-18 17:42:32 -08002061long SSL_get_verify_result(const SSL *ssl) { return ssl->verify_result; }
Adam Langley95c29f32014-06-20 12:00:00 -07002062
David Benjamin8a589332015-12-04 23:14:35 -05002063int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
Adam Langleyfcf25832014-12-18 17:42:32 -08002064 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002065 int index;
2066 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp,
David Benjamin8a589332015-12-04 23:14:35 -05002067 dup_func, free_func)) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002068 return -1;
2069 }
2070 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002071}
Adam Langley95c29f32014-06-20 12:00:00 -07002072
David Benjamin1ac08fe2015-09-15 01:36:22 -04002073int SSL_set_ex_data(SSL *ssl, int idx, void *arg) {
2074 return CRYPTO_set_ex_data(&ssl->ex_data, idx, arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002075}
Adam Langley95c29f32014-06-20 12:00:00 -07002076
David Benjamin1ac08fe2015-09-15 01:36:22 -04002077void *SSL_get_ex_data(const SSL *ssl, int idx) {
2078 return CRYPTO_get_ex_data(&ssl->ex_data, idx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002079}
Adam Langley95c29f32014-06-20 12:00:00 -07002080
David Benjamin8a589332015-12-04 23:14:35 -05002081int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
Adam Langleyfcf25832014-12-18 17:42:32 -08002082 CRYPTO_EX_dup *dup_func,
2083 CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002084 int index;
2085 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp,
David Benjamin8a589332015-12-04 23:14:35 -05002086 dup_func, free_func)) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002087 return -1;
2088 }
2089 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002090}
Adam Langley95c29f32014-06-20 12:00:00 -07002091
David Benjamin1ac08fe2015-09-15 01:36:22 -04002092int SSL_CTX_set_ex_data(SSL_CTX *ctx, int idx, void *arg) {
2093 return CRYPTO_set_ex_data(&ctx->ex_data, idx, arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002094}
Adam Langley95c29f32014-06-20 12:00:00 -07002095
David Benjamin1ac08fe2015-09-15 01:36:22 -04002096void *SSL_CTX_get_ex_data(const SSL_CTX *ctx, int idx) {
2097 return CRYPTO_get_ex_data(&ctx->ex_data, idx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002098}
Adam Langley95c29f32014-06-20 12:00:00 -07002099
Adam Langleyfcf25832014-12-18 17:42:32 -08002100X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
2101 return ctx->cert_store;
2102}
Adam Langley95c29f32014-06-20 12:00:00 -07002103
Adam Langleyfcf25832014-12-18 17:42:32 -08002104void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
David Benjamin2755a3e2015-04-22 16:17:58 -04002105 X509_STORE_free(ctx->cert_store);
Adam Langleyfcf25832014-12-18 17:42:32 -08002106 ctx->cert_store = store;
2107}
Adam Langley95c29f32014-06-20 12:00:00 -07002108
David Benjamin93d17492015-10-17 12:43:36 -04002109int SSL_want(const SSL *ssl) { return ssl->rwstate; }
Adam Langley95c29f32014-06-20 12:00:00 -07002110
Adam Langleyfcf25832014-12-18 17:42:32 -08002111void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
2112 RSA *(*cb)(SSL *ssl, int is_export,
2113 int keylength)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002114}
Adam Langley95c29f32014-06-20 12:00:00 -07002115
Adam Langleyfcf25832014-12-18 17:42:32 -08002116void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
2117 int keylength)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002118}
2119
2120void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
David Benjamin59015c32015-04-26 13:13:55 -04002121 DH *(*callback)(SSL *ssl, int is_export,
2122 int keylength)) {
2123 ctx->cert->dh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002124}
2125
David Benjamin59015c32015-04-26 13:13:55 -04002126void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*callback)(SSL *ssl, int is_export,
2127 int keylength)) {
2128 ssl->cert->dh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002129}
2130
Adam Langleyfcf25832014-12-18 17:42:32 -08002131int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
2132 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
David Benjamin3570d732015-06-29 00:28:17 -04002133 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -08002134 return 0;
2135 }
2136
David Benjamin2755a3e2015-04-22 16:17:58 -04002137 OPENSSL_free(ctx->psk_identity_hint);
Adam Langleyfcf25832014-12-18 17:42:32 -08002138
2139 if (identity_hint != NULL) {
2140 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2141 if (ctx->psk_identity_hint == NULL) {
2142 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002143 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002144 } else {
2145 ctx->psk_identity_hint = NULL;
2146 }
Adam Langley95c29f32014-06-20 12:00:00 -07002147
Adam Langleyfcf25832014-12-18 17:42:32 -08002148 return 1;
2149}
2150
David Benjamine8814df2015-09-15 08:05:54 -04002151int SSL_use_psk_identity_hint(SSL *ssl, const char *identity_hint) {
2152 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002153 return 0;
2154 }
2155
2156 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
David Benjamin3570d732015-06-29 00:28:17 -04002157 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -08002158 return 0;
2159 }
2160
2161 /* Clear currently configured hint, if any. */
David Benjamine8814df2015-09-15 08:05:54 -04002162 OPENSSL_free(ssl->psk_identity_hint);
2163 ssl->psk_identity_hint = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002164
2165 if (identity_hint != NULL) {
David Benjamine8814df2015-09-15 08:05:54 -04002166 ssl->psk_identity_hint = BUF_strdup(identity_hint);
2167 if (ssl->psk_identity_hint == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002168 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002169 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002170 }
Adam Langley95c29f32014-06-20 12:00:00 -07002171
Adam Langleyfcf25832014-12-18 17:42:32 -08002172 return 1;
2173}
Adam Langley95c29f32014-06-20 12:00:00 -07002174
David Benjamine8814df2015-09-15 08:05:54 -04002175const char *SSL_get_psk_identity_hint(const SSL *ssl) {
2176 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002177 return NULL;
2178 }
David Benjamine8814df2015-09-15 08:05:54 -04002179 return ssl->psk_identity_hint;
Adam Langleyfcf25832014-12-18 17:42:32 -08002180}
Adam Langley95c29f32014-06-20 12:00:00 -07002181
David Benjamine8814df2015-09-15 08:05:54 -04002182const char *SSL_get_psk_identity(const SSL *ssl) {
2183 if (ssl == NULL || ssl->session == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002184 return NULL;
2185 }
Adam Langley95c29f32014-06-20 12:00:00 -07002186
David Benjamine8814df2015-09-15 08:05:54 -04002187 return ssl->session->psk_identity;
Adam Langleyfcf25832014-12-18 17:42:32 -08002188}
Adam Langley95c29f32014-06-20 12:00:00 -07002189
Adam Langleyfcf25832014-12-18 17:42:32 -08002190void SSL_set_psk_client_callback(
David Benjamine8814df2015-09-15 08:05:54 -04002191 SSL *ssl, unsigned (*cb)(SSL *ssl, const char *hint, char *identity,
2192 unsigned max_identity_len, uint8_t *psk,
2193 unsigned max_psk_len)) {
2194 ssl->psk_client_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002195}
Adam Langley95c29f32014-06-20 12:00:00 -07002196
Adam Langleyfcf25832014-12-18 17:42:32 -08002197void SSL_CTX_set_psk_client_callback(
David Benjamine8814df2015-09-15 08:05:54 -04002198 SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *hint, char *identity,
2199 unsigned max_identity_len, uint8_t *psk,
2200 unsigned max_psk_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002201 ctx->psk_client_callback = cb;
2202}
Adam Langley95c29f32014-06-20 12:00:00 -07002203
Adam Langleyfcf25832014-12-18 17:42:32 -08002204void SSL_set_psk_server_callback(
David Benjamine8814df2015-09-15 08:05:54 -04002205 SSL *ssl, unsigned (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
2206 unsigned max_psk_len)) {
2207 ssl->psk_server_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002208}
Adam Langley95c29f32014-06-20 12:00:00 -07002209
Adam Langleyfcf25832014-12-18 17:42:32 -08002210void SSL_CTX_set_psk_server_callback(
David Benjamine8814df2015-09-15 08:05:54 -04002211 SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *identity,
2212 uint8_t *psk, unsigned max_psk_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002213 ctx->psk_server_callback = cb;
2214}
Adam Langley95c29f32014-06-20 12:00:00 -07002215
Adam Langleyfcf25832014-12-18 17:42:32 -08002216void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
2217 void (*cb)(int write_p, int version,
2218 int content_type, const void *buf,
2219 size_t len, SSL *ssl, void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002220 ctx->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002221}
David Benjamin61ecccf2015-05-05 09:44:51 -04002222
2223void SSL_CTX_set_msg_callback_arg(SSL_CTX *ctx, void *arg) {
2224 ctx->msg_callback_arg = arg;
2225}
2226
Adam Langleyfcf25832014-12-18 17:42:32 -08002227void SSL_set_msg_callback(SSL *ssl,
2228 void (*cb)(int write_p, int version, int content_type,
2229 const void *buf, size_t len, SSL *ssl,
2230 void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002231 ssl->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002232}
Adam Langley95c29f32014-06-20 12:00:00 -07002233
David Benjamin61ecccf2015-05-05 09:44:51 -04002234void SSL_set_msg_callback_arg(SSL *ssl, void *arg) {
2235 ssl->msg_callback_arg = arg;
2236}
2237
David Benjamind28f59c2015-11-17 22:32:50 -05002238void SSL_CTX_set_keylog_callback(SSL_CTX *ctx,
2239 void (*cb)(const SSL *ssl, const char *line)) {
2240 ctx->keylog_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002241}
Adam Langley95c29f32014-06-20 12:00:00 -07002242
Adam Langleyfcf25832014-12-18 17:42:32 -08002243static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
2244 static const char hextable[] = "0123456789abcdef";
2245 uint8_t *out;
2246 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002247
Adam Langleyfcf25832014-12-18 17:42:32 -08002248 if (!CBB_add_space(cbb, &out, in_len * 2)) {
2249 return 0;
2250 }
Adam Langley95c29f32014-06-20 12:00:00 -07002251
Adam Langleyfcf25832014-12-18 17:42:32 -08002252 for (i = 0; i < in_len; i++) {
2253 *(out++) = (uint8_t)hextable[in[i] >> 4];
2254 *(out++) = (uint8_t)hextable[in[i] & 0xf];
2255 }
Adam Langley95c29f32014-06-20 12:00:00 -07002256
Adam Langleyfcf25832014-12-18 17:42:32 -08002257 return 1;
2258}
David Benjamin859ec3c2014-09-02 16:29:36 -04002259
David Benjamind28f59c2015-11-17 22:32:50 -05002260int ssl_log_rsa_client_key_exchange(const SSL *ssl,
2261 const uint8_t *encrypted_premaster,
2262 size_t encrypted_premaster_len,
2263 const uint8_t *premaster,
2264 size_t premaster_len) {
2265 if (ssl->ctx->keylog_callback == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002266 return 1;
2267 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002268
Adam Langleyfcf25832014-12-18 17:42:32 -08002269 if (encrypted_premaster_len < 8) {
David Benjamin3570d732015-06-29 00:28:17 -04002270 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08002271 return 0;
2272 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002273
David Benjamind28f59c2015-11-17 22:32:50 -05002274 CBB cbb;
2275 uint8_t *out;
2276 size_t out_len;
David Benjamina8653202015-06-28 01:26:10 -04002277 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len * 2 + 1) ||
2278 !CBB_add_bytes(&cbb, (const uint8_t *)"RSA ", 4) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08002279 /* Only the first 8 bytes of the encrypted premaster secret are
2280 * logged. */
2281 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
2282 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2283 !cbb_add_hex(&cbb, premaster, premaster_len) ||
David Benjamind28f59c2015-11-17 22:32:50 -05002284 !CBB_add_u8(&cbb, 0 /* NUL */) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08002285 !CBB_finish(&cbb, &out, &out_len)) {
2286 CBB_cleanup(&cbb);
2287 return 0;
2288 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002289
David Benjamind28f59c2015-11-17 22:32:50 -05002290 ssl->ctx->keylog_callback(ssl, (const char *)out);
Adam Langleyfcf25832014-12-18 17:42:32 -08002291 OPENSSL_free(out);
David Benjamind28f59c2015-11-17 22:32:50 -05002292 return 1;
Adam Langley95f22882014-06-20 12:00:00 -07002293}
2294
David Benjamind28f59c2015-11-17 22:32:50 -05002295int ssl_log_master_secret(const SSL *ssl, const uint8_t *client_random,
2296 size_t client_random_len, const uint8_t *master,
2297 size_t master_len) {
2298 if (ssl->ctx->keylog_callback == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002299 return 1;
2300 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002301
Adam Langleyfcf25832014-12-18 17:42:32 -08002302 if (client_random_len != 32) {
David Benjamin3570d732015-06-29 00:28:17 -04002303 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08002304 return 0;
2305 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002306
David Benjamind28f59c2015-11-17 22:32:50 -05002307 CBB cbb;
2308 uint8_t *out;
2309 size_t out_len;
David Benjamina8653202015-06-28 01:26:10 -04002310 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len * 2 + 1) ||
2311 !CBB_add_bytes(&cbb, (const uint8_t *)"CLIENT_RANDOM ", 14) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08002312 !cbb_add_hex(&cbb, client_random, 32) ||
2313 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2314 !cbb_add_hex(&cbb, master, master_len) ||
David Benjamind28f59c2015-11-17 22:32:50 -05002315 !CBB_add_u8(&cbb, 0 /* NUL */) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08002316 !CBB_finish(&cbb, &out, &out_len)) {
2317 CBB_cleanup(&cbb);
2318 return 0;
2319 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002320
David Benjamind28f59c2015-11-17 22:32:50 -05002321 ssl->ctx->keylog_callback(ssl, (const char *)out);
Adam Langleyfcf25832014-12-18 17:42:32 -08002322 OPENSSL_free(out);
David Benjamind28f59c2015-11-17 22:32:50 -05002323 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002324}
2325
David Benjamin5d8b1282015-10-17 23:26:35 -04002326int SSL_is_init_finished(const SSL *ssl) {
2327 return ssl->state == SSL_ST_OK;
2328}
2329
2330int SSL_in_init(const SSL *ssl) {
2331 return (ssl->state & SSL_ST_INIT) != 0;
2332}
2333
2334int SSL_in_false_start(const SSL *ssl) {
2335 return ssl->s3->tmp.in_false_start;
David Benjamined7c4752015-02-16 19:16:46 -05002336}
2337
Adam Langleyfcf25832014-12-18 17:42:32 -08002338int SSL_cutthrough_complete(const SSL *s) {
David Benjamined7c4752015-02-16 19:16:46 -05002339 return SSL_in_false_start(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002340}
Adam Langley95c29f32014-06-20 12:00:00 -07002341
Adam Langleyfcf25832014-12-18 17:42:32 -08002342void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
2343 size_t *ssl_session_size) {
2344 *ssl_size = sizeof(SSL);
2345 *ssl_ctx_size = sizeof(SSL_CTX);
2346 *ssl_session_size = sizeof(SSL_SESSION);
2347}
Feng Lu41aa3252014-11-21 22:47:56 -08002348
David Benjamined7c4752015-02-16 19:16:46 -05002349int ssl3_can_false_start(const SSL *s) {
David Benjamin195dc782015-02-19 13:27:05 -05002350 const SSL_CIPHER *const cipher = SSL_get_current_cipher(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002351
Adam Langleye631d962015-04-01 13:11:01 -07002352 /* False Start only for TLS 1.2 with an ECDHE+AEAD cipher and ALPN or NPN. */
David Benjamin195dc782015-02-19 13:27:05 -05002353 return !SSL_IS_DTLS(s) &&
2354 SSL_version(s) >= TLS1_2_VERSION &&
2355 (s->s3->alpn_selected || s->s3->next_proto_neg_seen) &&
2356 cipher != NULL &&
Adam Langleye631d962015-04-01 13:11:01 -07002357 cipher->algorithm_mkey == SSL_kECDHE &&
David Benjamin195dc782015-02-19 13:27:05 -05002358 (cipher->algorithm_enc == SSL_AES128GCM ||
2359 cipher->algorithm_enc == SSL_AES256GCM ||
Brian Smith271777f2015-10-03 13:53:33 -10002360 cipher->algorithm_enc == SSL_CHACHA20POLY1305_OLD);
Adam Langleyfcf25832014-12-18 17:42:32 -08002361}
2362
2363const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version) {
2364 switch (version) {
2365 case SSL3_VERSION:
2366 return &SSLv3_enc_data;
2367
2368 case TLS1_VERSION:
2369 return &TLSv1_enc_data;
2370
David Benjamin9e13e1a2015-03-05 01:56:32 -05002371 case DTLS1_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002372 case TLS1_1_VERSION:
2373 return &TLSv1_1_enc_data;
2374
David Benjamin9e13e1a2015-03-05 01:56:32 -05002375 case DTLS1_2_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002376 case TLS1_2_VERSION:
2377 return &TLSv1_2_enc_data;
2378
Adam Langleyfcf25832014-12-18 17:42:32 -08002379 default:
2380 return NULL;
2381 }
2382}
2383
2384uint16_t ssl3_get_max_server_version(const SSL *s) {
2385 uint16_t max_version;
2386
2387 if (SSL_IS_DTLS(s)) {
2388 max_version = (s->max_version != 0) ? s->max_version : DTLS1_2_VERSION;
2389 if (!(s->options & SSL_OP_NO_DTLSv1_2) && DTLS1_2_VERSION >= max_version) {
2390 return DTLS1_2_VERSION;
2391 }
2392 if (!(s->options & SSL_OP_NO_DTLSv1) && DTLS1_VERSION >= max_version) {
2393 return DTLS1_VERSION;
2394 }
2395 return 0;
2396 }
2397
2398 max_version = (s->max_version != 0) ? s->max_version : TLS1_2_VERSION;
2399 if (!(s->options & SSL_OP_NO_TLSv1_2) && TLS1_2_VERSION <= max_version) {
2400 return TLS1_2_VERSION;
2401 }
2402 if (!(s->options & SSL_OP_NO_TLSv1_1) && TLS1_1_VERSION <= max_version) {
2403 return TLS1_1_VERSION;
2404 }
2405 if (!(s->options & SSL_OP_NO_TLSv1) && TLS1_VERSION <= max_version) {
2406 return TLS1_VERSION;
2407 }
2408 if (!(s->options & SSL_OP_NO_SSLv3) && SSL3_VERSION <= max_version) {
2409 return SSL3_VERSION;
2410 }
2411 return 0;
2412}
2413
2414uint16_t ssl3_get_mutual_version(SSL *s, uint16_t client_version) {
2415 uint16_t version = 0;
2416
2417 if (SSL_IS_DTLS(s)) {
2418 /* Clamp client_version to max_version. */
2419 if (s->max_version != 0 && client_version < s->max_version) {
2420 client_version = s->max_version;
2421 }
2422
2423 if (client_version <= DTLS1_2_VERSION && !(s->options & SSL_OP_NO_DTLSv1_2)) {
2424 version = DTLS1_2_VERSION;
2425 } else if (client_version <= DTLS1_VERSION &&
David Benjaminb18f0242015-03-10 18:30:08 -04002426 !(s->options & SSL_OP_NO_DTLSv1)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002427 version = DTLS1_VERSION;
2428 }
2429
2430 /* Check against min_version. */
2431 if (version != 0 && s->min_version != 0 && version > s->min_version) {
2432 return 0;
2433 }
2434 return version;
2435 } else {
2436 /* Clamp client_version to max_version. */
2437 if (s->max_version != 0 && client_version > s->max_version) {
2438 client_version = s->max_version;
2439 }
2440
2441 if (client_version >= TLS1_2_VERSION && !(s->options & SSL_OP_NO_TLSv1_2)) {
2442 version = TLS1_2_VERSION;
2443 } else if (client_version >= TLS1_1_VERSION &&
2444 !(s->options & SSL_OP_NO_TLSv1_1)) {
2445 version = TLS1_1_VERSION;
2446 } else if (client_version >= TLS1_VERSION && !(s->options & SSL_OP_NO_TLSv1)) {
2447 version = TLS1_VERSION;
2448 } else if (client_version >= SSL3_VERSION && !(s->options & SSL_OP_NO_SSLv3)) {
2449 version = SSL3_VERSION;
2450 }
2451
2452 /* Check against min_version. */
2453 if (version != 0 && s->min_version != 0 && version < s->min_version) {
2454 return 0;
2455 }
2456 return version;
2457 }
2458}
2459
2460uint16_t ssl3_get_max_client_version(SSL *s) {
David Benjamin123a8fd2015-04-26 14:16:41 -04002461 uint32_t options = s->options;
Adam Langleyfcf25832014-12-18 17:42:32 -08002462 uint16_t version = 0;
2463
2464 /* OpenSSL's API for controlling versions entails blacklisting individual
2465 * protocols. This has two problems. First, on the client, the protocol can
2466 * only express a contiguous range of versions. Second, a library consumer
2467 * trying to set a maximum version cannot disable protocol versions that get
2468 * added in a future version of the library.
2469 *
2470 * To account for both of these, OpenSSL interprets the client-side bitmask
2471 * as a min/max range by picking the lowest contiguous non-empty range of
2472 * enabled protocols. Note that this means it is impossible to set a maximum
2473 * version of TLS 1.2 in a future-proof way.
2474 *
2475 * By this scheme, the maximum version is the lowest version V such that V is
2476 * enabled and V+1 is disabled or unimplemented. */
2477 if (SSL_IS_DTLS(s)) {
2478 if (!(options & SSL_OP_NO_DTLSv1_2)) {
2479 version = DTLS1_2_VERSION;
2480 }
2481 if (!(options & SSL_OP_NO_DTLSv1) && (options & SSL_OP_NO_DTLSv1_2)) {
2482 version = DTLS1_VERSION;
2483 }
2484 if (s->max_version != 0 && version < s->max_version) {
2485 version = s->max_version;
2486 }
2487 } else {
2488 if (!(options & SSL_OP_NO_TLSv1_2)) {
2489 version = TLS1_2_VERSION;
2490 }
2491 if (!(options & SSL_OP_NO_TLSv1_1) && (options & SSL_OP_NO_TLSv1_2)) {
2492 version = TLS1_1_VERSION;
2493 }
2494 if (!(options & SSL_OP_NO_TLSv1) && (options & SSL_OP_NO_TLSv1_1)) {
2495 version = TLS1_VERSION;
2496 }
2497 if (!(options & SSL_OP_NO_SSLv3) && (options & SSL_OP_NO_TLSv1)) {
2498 version = SSL3_VERSION;
2499 }
2500 if (s->max_version != 0 && version > s->max_version) {
2501 version = s->max_version;
2502 }
2503 }
2504
2505 return version;
2506}
2507
2508int ssl3_is_version_enabled(SSL *s, uint16_t version) {
2509 if (SSL_IS_DTLS(s)) {
2510 if (s->max_version != 0 && version < s->max_version) {
2511 return 0;
2512 }
2513 if (s->min_version != 0 && version > s->min_version) {
2514 return 0;
2515 }
2516
2517 switch (version) {
2518 case DTLS1_VERSION:
2519 return !(s->options & SSL_OP_NO_DTLSv1);
2520
2521 case DTLS1_2_VERSION:
2522 return !(s->options & SSL_OP_NO_DTLSv1_2);
2523
2524 default:
2525 return 0;
2526 }
2527 } else {
2528 if (s->max_version != 0 && version > s->max_version) {
2529 return 0;
2530 }
2531 if (s->min_version != 0 && version < s->min_version) {
2532 return 0;
2533 }
2534
2535 switch (version) {
2536 case SSL3_VERSION:
2537 return !(s->options & SSL_OP_NO_SSLv3);
2538
2539 case TLS1_VERSION:
2540 return !(s->options & SSL_OP_NO_TLSv1);
2541
2542 case TLS1_1_VERSION:
2543 return !(s->options & SSL_OP_NO_TLSv1_1);
2544
2545 case TLS1_2_VERSION:
2546 return !(s->options & SSL_OP_NO_TLSv1_2);
2547
2548 default:
2549 return 0;
2550 }
2551 }
2552}
2553
David Benjaminea72bd02014-12-21 21:27:41 -05002554uint16_t ssl3_version_from_wire(SSL *s, uint16_t wire_version) {
2555 if (!SSL_IS_DTLS(s)) {
2556 return wire_version;
2557 }
2558
2559 uint16_t tls_version = ~wire_version;
2560 uint16_t version = tls_version + 0x0201;
2561 /* If either component overflowed, clamp it so comparisons still work. */
2562 if ((version >> 8) < (tls_version >> 8)) {
2563 version = 0xff00 | (version & 0xff);
2564 }
2565 if ((version & 0xff) < (tls_version & 0xff)) {
2566 version = (version & 0xff00) | 0xff;
2567 }
2568 /* DTLS 1.0 maps to TLS 1.1, not TLS 1.0. */
2569 if (version == TLS1_VERSION) {
2570 version = TLS1_1_VERSION;
2571 }
2572 return version;
2573}
2574
David Benjamin42fea372015-09-19 01:22:44 -04002575int SSL_cache_hit(SSL *ssl) { return SSL_session_reused(ssl); }
Adam Langleyfcf25832014-12-18 17:42:32 -08002576
David Benjamin42fea372015-09-19 01:22:44 -04002577int SSL_is_server(SSL *ssl) { return ssl->server; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002578
David Benjamind4c2bce2015-10-17 12:28:18 -04002579void SSL_CTX_set_select_certificate_cb(
2580 SSL_CTX *ctx, int (*cb)(const struct ssl_early_callback_ctx *)) {
2581 ctx->select_certificate_cb = cb;
2582}
2583
Adam Langley524e7172015-02-20 16:04:00 -08002584void SSL_CTX_set_dos_protection_cb(
2585 SSL_CTX *ctx, int (*cb)(const struct ssl_early_callback_ctx *)) {
2586 ctx->dos_protection_cb = cb;
2587}
2588
David Benjamin1d5ef3b2015-10-12 19:54:18 -04002589void SSL_set_renegotiate_mode(SSL *ssl, enum ssl_renegotiate_mode_t mode) {
2590 ssl->renegotiate_mode = mode;
2591}
2592
2593void SSL_set_reject_peer_renegotiations(SSL *ssl, int reject) {
2594 SSL_set_renegotiate_mode(
2595 ssl, reject ? ssl_renegotiate_never : ssl_renegotiate_freely);
David Benjaminb16346b2015-04-08 19:16:58 -04002596}
2597
Adam Langley3f92d212015-02-20 15:32:52 -08002598int SSL_get_rc4_state(const SSL *ssl, const RC4_KEY **read_key,
2599 const RC4_KEY **write_key) {
2600 if (ssl->aead_read_ctx == NULL || ssl->aead_write_ctx == NULL) {
2601 return 0;
2602 }
2603
2604 return EVP_AEAD_CTX_get_rc4_state(&ssl->aead_read_ctx->ctx, read_key) &&
2605 EVP_AEAD_CTX_get_rc4_state(&ssl->aead_write_ctx->ctx, write_key);
2606}
David Benjaminda881e92015-04-26 14:45:04 -04002607
Adam Langleyc2d32802015-11-03 18:36:10 -08002608int SSL_get_ivs(const SSL *ssl, const uint8_t **out_read_iv,
2609 const uint8_t **out_write_iv, size_t *out_iv_len) {
2610 if (ssl->aead_read_ctx == NULL || ssl->aead_write_ctx == NULL) {
2611 return 0;
2612 }
2613
2614 size_t write_iv_len;
2615 if (!EVP_AEAD_CTX_get_iv(&ssl->aead_read_ctx->ctx, out_read_iv, out_iv_len) ||
2616 !EVP_AEAD_CTX_get_iv(&ssl->aead_write_ctx->ctx, out_write_iv,
2617 &write_iv_len) ||
2618 *out_iv_len != write_iv_len) {
2619 return 0;
2620 }
2621
2622 return 1;
David Benjamine348ff42015-11-06 17:55:30 -05002623}
David Benjamin6e807652015-11-02 12:02:20 -05002624
2625uint8_t SSL_get_server_key_exchange_hash(const SSL *ssl) {
2626 return ssl->s3->tmp.server_key_exchange_hash;
Adam Langleyc2d32802015-11-03 18:36:10 -08002627}
2628
David Benjamin27bbae42015-09-13 00:54:37 -04002629int SSL_clear(SSL *ssl) {
2630 if (ssl->method == NULL) {
2631 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_METHOD_SPECIFIED);
2632 return 0;
2633 }
2634
2635 if (ssl_clear_bad_session(ssl)) {
2636 SSL_SESSION_free(ssl->session);
2637 ssl->session = NULL;
2638 }
2639
2640 ssl->hit = 0;
2641 ssl->shutdown = 0;
2642
2643 /* SSL_clear may be called before or after the |ssl| is initialized in either
2644 * accept or connect state. In the latter case, SSL_clear should preserve the
2645 * half and reset |ssl->state| accordingly. */
2646 if (ssl->handshake_func != NULL) {
2647 if (ssl->server) {
2648 SSL_set_accept_state(ssl);
2649 } else {
2650 SSL_set_connect_state(ssl);
2651 }
2652 } else {
2653 assert(ssl->state == 0);
2654 }
2655
2656 /* TODO(davidben): Some state on |ssl| is reset both in |SSL_new| and
2657 * |SSL_clear| because it is per-connection state rather than configuration
2658 * state. Per-connection state should be on |ssl->s3| and |ssl->d1| so it is
2659 * naturally reset at the right points between |SSL_new|, |SSL_clear|, and
2660 * |ssl3_new|. */
2661
2662 ssl->rwstate = SSL_NOTHING;
2663
2664 BUF_MEM_free(ssl->init_buf);
2665 ssl->init_buf = NULL;
2666
2667 ssl_clear_cipher_ctx(ssl);
2668
2669 OPENSSL_free(ssl->next_proto_negotiated);
2670 ssl->next_proto_negotiated = NULL;
2671 ssl->next_proto_negotiated_len = 0;
2672
2673 /* The ssl->d1->mtu is simultaneously configuration (preserved across
2674 * clear) and connection-specific state (gets reset).
2675 *
2676 * TODO(davidben): Avoid this. */
2677 unsigned mtu = 0;
2678 if (ssl->d1 != NULL) {
2679 mtu = ssl->d1->mtu;
2680 }
2681
2682 ssl->method->ssl_free(ssl);
2683 if (!ssl->method->ssl_new(ssl)) {
2684 return 0;
2685 }
2686 ssl->enc_method = ssl3_get_enc_method(ssl->version);
2687 assert(ssl->enc_method != NULL);
2688
2689 if (SSL_IS_DTLS(ssl) && (SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
2690 ssl->d1->mtu = mtu;
2691 }
2692
2693 ssl->client_version = ssl->version;
2694
2695 return 1;
2696}
2697
David Benjaminda881e92015-04-26 14:45:04 -04002698int SSL_CTX_sess_connect(const SSL_CTX *ctx) { return 0; }
2699int SSL_CTX_sess_connect_good(const SSL_CTX *ctx) { return 0; }
2700int SSL_CTX_sess_connect_renegotiate(const SSL_CTX *ctx) { return 0; }
2701int SSL_CTX_sess_accept(const SSL_CTX *ctx) { return 0; }
2702int SSL_CTX_sess_accept_renegotiate(const SSL_CTX *ctx) { return 0; }
2703int SSL_CTX_sess_accept_good(const SSL_CTX *ctx) { return 0; }
2704int SSL_CTX_sess_hits(const SSL_CTX *ctx) { return 0; }
2705int SSL_CTX_sess_cb_hits(const SSL_CTX *ctx) { return 0; }
2706int SSL_CTX_sess_misses(const SSL_CTX *ctx) { return 0; }
2707int SSL_CTX_sess_timeouts(const SSL_CTX *ctx) { return 0; }
2708int SSL_CTX_sess_cache_full(const SSL_CTX *ctx) { return 0; }
Matt Braithwaite4838d8a2015-08-20 13:20:03 -07002709void ERR_load_SSL_strings(void) {}
David Benjamin27bbae42015-09-13 00:54:37 -04002710void SSL_load_error_strings(void) {}