blob: b837ad0d50b510347cf18ab4b82daed2a5c57309 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
108 *
109 */
110/* ====================================================================
111 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
112 * ECC cipher suite support in OpenSSL originally developed by
113 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
114 */
115/* ====================================================================
116 * Copyright 2005 Nokia. All rights reserved.
117 *
118 * The portions of the attached software ("Contribution") is developed by
119 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
120 * license.
121 *
122 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
123 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
124 * support (see RFC 4279) to OpenSSL.
125 *
126 * No patent licenses or other rights except those expressly stated in
127 * the OpenSSL open source license shall be deemed granted or received
128 * expressly, by implication, estoppel, or otherwise.
129 *
130 * No assurances are provided by Nokia that the Contribution does not
131 * infringe the patent or other intellectual property rights of any third
132 * party or that the license provides you with all the necessary rights
133 * to make use of the Contribution.
134 *
135 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
136 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
137 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
138 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
139 * OTHERWISE. */
140
Adam Langley95c29f32014-06-20 12:00:00 -0700141#include <assert.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400142#include <stdio.h>
143#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700144
David Benjamin39482a12014-07-20 13:30:15 -0400145#include <openssl/bytestring.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700146#include <openssl/dh.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400147#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700148#include <openssl/lhash.h>
149#include <openssl/mem.h>
150#include <openssl/obj.h>
151#include <openssl/rand.h>
152#include <openssl/x509v3.h>
153
David Benjamin2ee94aa2015-04-07 22:38:30 -0400154#include "internal.h"
David Benjamin546f1a52015-04-15 16:46:09 -0400155#include "../crypto/internal.h"
156
Adam Langley95c29f32014-06-20 12:00:00 -0700157
Adam Langleyfcf25832014-12-18 17:42:32 -0800158/* Some error codes are special. Ensure the make_errors.go script never
159 * regresses this. */
160OPENSSL_COMPILE_ASSERT(SSL_R_TLSV1_ALERT_NO_RENEGOTIATION ==
161 SSL_AD_NO_RENEGOTIATION + SSL_AD_REASON_OFFSET,
162 ssl_alert_reason_code_mismatch);
David Benjamine1136082014-09-20 12:28:58 -0400163
David Benjamin1d0a1942015-04-26 15:35:35 -0400164/* kMaxHandshakeSize is the maximum size, in bytes, of a handshake message. */
165static const size_t kMaxHandshakeSize = (1u << 24) - 1;
166
David Benjaminaa585132015-06-29 23:36:17 -0400167static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl =
168 CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
169static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl_ctx =
170 CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
David Benjamin9f33fc62015-04-15 17:29:53 -0400171
David Benjamin4831c332015-05-16 11:43:13 -0400172int SSL_clear(SSL *ssl) {
173 if (ssl->method == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400174 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_METHOD_SPECIFIED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800175 return 0;
176 }
Adam Langley95c29f32014-06-20 12:00:00 -0700177
David Benjamin4831c332015-05-16 11:43:13 -0400178 if (ssl_clear_bad_session(ssl)) {
179 SSL_SESSION_free(ssl->session);
180 ssl->session = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800181 }
Adam Langley95c29f32014-06-20 12:00:00 -0700182
David Benjamin4831c332015-05-16 11:43:13 -0400183 ssl->hit = 0;
184 ssl->shutdown = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700185
David Benjamin4831c332015-05-16 11:43:13 -0400186 /* SSL_clear may be called before or after the |ssl| is initialized in either
Adam Langleyfcf25832014-12-18 17:42:32 -0800187 * accept or connect state. In the latter case, SSL_clear should preserve the
David Benjamin4831c332015-05-16 11:43:13 -0400188 * half and reset |ssl->state| accordingly. */
189 if (ssl->handshake_func != NULL) {
190 if (ssl->server) {
191 SSL_set_accept_state(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800192 } else {
David Benjamin4831c332015-05-16 11:43:13 -0400193 SSL_set_connect_state(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800194 }
195 } else {
David Benjamin4831c332015-05-16 11:43:13 -0400196 assert(ssl->state == 0);
Adam Langleyfcf25832014-12-18 17:42:32 -0800197 }
Adam Langley95c29f32014-06-20 12:00:00 -0700198
David Benjamin5f387e32015-05-15 22:05:42 -0400199 /* TODO(davidben): Some state on |ssl| is reset both in |SSL_new| and
David Benjamin62fd1622015-01-11 13:30:01 -0500200 * |SSL_clear| because it is per-connection state rather than configuration
David Benjamin4831c332015-05-16 11:43:13 -0400201 * state. Per-connection state should be on |ssl->s3| and |ssl->d1| so it is
David Benjamin62fd1622015-01-11 13:30:01 -0500202 * naturally reset at the right points between |SSL_new|, |SSL_clear|, and
203 * |ssl3_new|. */
204
David Benjamin4831c332015-05-16 11:43:13 -0400205 ssl->rwstate = SSL_NOTHING;
206 ssl->rstate = SSL_ST_READ_HEADER;
Adam Langley95c29f32014-06-20 12:00:00 -0700207
David Benjamin4831c332015-05-16 11:43:13 -0400208 BUF_MEM_free(ssl->init_buf);
209 ssl->init_buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700210
David Benjamin4831c332015-05-16 11:43:13 -0400211 ssl->packet = NULL;
212 ssl->packet_length = 0;
David Benjamin62fd1622015-01-11 13:30:01 -0500213
David Benjamin4831c332015-05-16 11:43:13 -0400214 ssl_clear_cipher_ctx(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700215
David Benjamin4831c332015-05-16 11:43:13 -0400216 OPENSSL_free(ssl->next_proto_negotiated);
217 ssl->next_proto_negotiated = NULL;
218 ssl->next_proto_negotiated_len = 0;
David Benjamin62fd1622015-01-11 13:30:01 -0500219
David Benjamin4831c332015-05-16 11:43:13 -0400220 /* The ssl->d1->mtu is simultaneously configuration (preserved across
David Benjamin62fd1622015-01-11 13:30:01 -0500221 * clear) and connection-specific state (gets reset).
222 *
223 * TODO(davidben): Avoid this. */
224 unsigned mtu = 0;
David Benjamin4831c332015-05-16 11:43:13 -0400225 if (ssl->d1 != NULL) {
226 mtu = ssl->d1->mtu;
David Benjamin62fd1622015-01-11 13:30:01 -0500227 }
228
David Benjamin4831c332015-05-16 11:43:13 -0400229 ssl->method->ssl_free(ssl);
230 if (!ssl->method->ssl_new(ssl)) {
David Benjamin62fd1622015-01-11 13:30:01 -0500231 return 0;
232 }
David Benjamin4831c332015-05-16 11:43:13 -0400233 ssl->enc_method = ssl3_get_enc_method(ssl->version);
234 assert(ssl->enc_method != NULL);
David Benjamin62fd1622015-01-11 13:30:01 -0500235
David Benjamin4831c332015-05-16 11:43:13 -0400236 if (SSL_IS_DTLS(ssl) && (SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
237 ssl->d1->mtu = mtu;
David Benjamin62fd1622015-01-11 13:30:01 -0500238 }
239
David Benjamin4831c332015-05-16 11:43:13 -0400240 ssl->client_version = ssl->version;
Adam Langley95c29f32014-06-20 12:00:00 -0700241
Adam Langleyfcf25832014-12-18 17:42:32 -0800242 return 1;
243}
Adam Langley95c29f32014-06-20 12:00:00 -0700244
Adam Langleyfcf25832014-12-18 17:42:32 -0800245SSL *SSL_new(SSL_CTX *ctx) {
246 SSL *s;
Adam Langley95c29f32014-06-20 12:00:00 -0700247
Adam Langleyfcf25832014-12-18 17:42:32 -0800248 if (ctx == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400249 OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_CTX);
Adam Langleyfcf25832014-12-18 17:42:32 -0800250 return NULL;
251 }
252 if (ctx->method == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400253 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
Adam Langleyfcf25832014-12-18 17:42:32 -0800254 return NULL;
255 }
Adam Langley95c29f32014-06-20 12:00:00 -0700256
Adam Langleyfcf25832014-12-18 17:42:32 -0800257 s = (SSL *)OPENSSL_malloc(sizeof(SSL));
258 if (s == NULL) {
259 goto err;
260 }
261 memset(s, 0, sizeof(SSL));
Adam Langley95c29f32014-06-20 12:00:00 -0700262
Adam Langleyfcf25832014-12-18 17:42:32 -0800263 s->min_version = ctx->min_version;
264 s->max_version = ctx->max_version;
David Benjamin1eb367c2014-12-12 18:17:51 -0500265
Adam Langleyfcf25832014-12-18 17:42:32 -0800266 s->options = ctx->options;
267 s->mode = ctx->mode;
268 s->max_cert_list = ctx->max_cert_list;
Adam Langley95c29f32014-06-20 12:00:00 -0700269
David Benjamina5a3eeb2015-03-18 20:26:30 -0400270 s->cert = ssl_cert_dup(ctx->cert);
271 if (s->cert == NULL) {
272 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -0800273 }
Adam Langley95c29f32014-06-20 12:00:00 -0700274
Adam Langleyfcf25832014-12-18 17:42:32 -0800275 s->msg_callback = ctx->msg_callback;
276 s->msg_callback_arg = ctx->msg_callback_arg;
277 s->verify_mode = ctx->verify_mode;
278 s->sid_ctx_length = ctx->sid_ctx_length;
279 assert(s->sid_ctx_length <= sizeof s->sid_ctx);
280 memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));
281 s->verify_callback = ctx->default_verify_callback;
282 s->generate_session_id = ctx->generate_session_id;
Adam Langley95c29f32014-06-20 12:00:00 -0700283
Adam Langleyfcf25832014-12-18 17:42:32 -0800284 s->param = X509_VERIFY_PARAM_new();
285 if (!s->param) {
286 goto err;
287 }
288 X509_VERIFY_PARAM_inherit(s->param, ctx->param);
289 s->quiet_shutdown = ctx->quiet_shutdown;
290 s->max_send_fragment = ctx->max_send_fragment;
Adam Langley95c29f32014-06-20 12:00:00 -0700291
Adam Langley0b5e3902015-05-15 13:08:38 -0700292 CRYPTO_refcount_inc(&ctx->references);
Adam Langleyfcf25832014-12-18 17:42:32 -0800293 s->ctx = ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -0800294 s->tlsext_ticket_expected = 0;
Adam Langley0b5e3902015-05-15 13:08:38 -0700295 CRYPTO_refcount_inc(&ctx->references);
Adam Langleyfcf25832014-12-18 17:42:32 -0800296 s->initial_ctx = ctx;
Adam Langley95c29f32014-06-20 12:00:00 -0700297
Adam Langleyfcf25832014-12-18 17:42:32 -0800298 if (ctx->tlsext_ellipticcurvelist) {
299 s->tlsext_ellipticcurvelist =
300 BUF_memdup(ctx->tlsext_ellipticcurvelist,
301 ctx->tlsext_ellipticcurvelist_length * 2);
302 if (!s->tlsext_ellipticcurvelist) {
303 goto err;
304 }
305 s->tlsext_ellipticcurvelist_length = ctx->tlsext_ellipticcurvelist_length;
306 }
307 s->next_proto_negotiated = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700308
Adam Langleyfcf25832014-12-18 17:42:32 -0800309 if (s->ctx->alpn_client_proto_list) {
310 s->alpn_client_proto_list = BUF_memdup(s->ctx->alpn_client_proto_list,
311 s->ctx->alpn_client_proto_list_len);
312 if (s->alpn_client_proto_list == NULL) {
313 goto err;
314 }
315 s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;
316 }
Adam Langley95c29f32014-06-20 12:00:00 -0700317
Adam Langleyfcf25832014-12-18 17:42:32 -0800318 s->verify_result = X509_V_OK;
319 s->method = ctx->method;
Adam Langley95c29f32014-06-20 12:00:00 -0700320
Adam Langleyfcf25832014-12-18 17:42:32 -0800321 if (!s->method->ssl_new(s)) {
322 goto err;
323 }
324 s->enc_method = ssl3_get_enc_method(s->version);
325 assert(s->enc_method != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700326
David Benjamin62fd1622015-01-11 13:30:01 -0500327 s->rwstate = SSL_NOTHING;
328 s->rstate = SSL_ST_READ_HEADER;
Adam Langley95c29f32014-06-20 12:00:00 -0700329
David Benjamin9f33fc62015-04-15 17:29:53 -0400330 CRYPTO_new_ex_data(&g_ex_data_class_ssl, s, &s->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700331
Adam Langleyfcf25832014-12-18 17:42:32 -0800332 s->psk_identity_hint = NULL;
333 if (ctx->psk_identity_hint) {
334 s->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
335 if (s->psk_identity_hint == NULL) {
336 goto err;
337 }
338 }
339 s->psk_client_callback = ctx->psk_client_callback;
340 s->psk_server_callback = ctx->psk_server_callback;
Adam Langley95c29f32014-06-20 12:00:00 -0700341
David Benjamin02ddbfd2015-01-11 13:09:11 -0500342 s->tlsext_channel_id_enabled = ctx->tlsext_channel_id_enabled;
343 if (ctx->tlsext_channel_id_private) {
David Benjamin9a10f8f2015-05-05 22:22:40 -0400344 s->tlsext_channel_id_private =
345 EVP_PKEY_up_ref(ctx->tlsext_channel_id_private);
David Benjamin02ddbfd2015-01-11 13:09:11 -0500346 }
347
Adam Langleyfcf25832014-12-18 17:42:32 -0800348 s->signed_cert_timestamps_enabled = s->ctx->signed_cert_timestamps_enabled;
349 s->ocsp_stapling_enabled = s->ctx->ocsp_stapling_enabled;
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200350
Adam Langleyfcf25832014-12-18 17:42:32 -0800351 return s;
352
Adam Langley95c29f32014-06-20 12:00:00 -0700353err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400354 SSL_free(s);
David Benjamin3570d732015-06-29 00:28:17 -0400355 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700356
Adam Langleyfcf25832014-12-18 17:42:32 -0800357 return NULL;
358}
Adam Langley95c29f32014-06-20 12:00:00 -0700359
Adam Langleyfcf25832014-12-18 17:42:32 -0800360int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
361 unsigned int sid_ctx_len) {
362 if (sid_ctx_len > sizeof ctx->sid_ctx) {
David Benjamin3570d732015-06-29 00:28:17 -0400363 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -0800364 return 0;
365 }
366 ctx->sid_ctx_length = sid_ctx_len;
367 memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700368
Adam Langleyfcf25832014-12-18 17:42:32 -0800369 return 1;
370}
Adam Langley95c29f32014-06-20 12:00:00 -0700371
Adam Langleyfcf25832014-12-18 17:42:32 -0800372int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
373 unsigned int sid_ctx_len) {
374 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
David Benjamin3570d732015-06-29 00:28:17 -0400375 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -0800376 return 0;
377 }
378 ssl->sid_ctx_length = sid_ctx_len;
379 memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700380
Adam Langleyfcf25832014-12-18 17:42:32 -0800381 return 1;
382}
Adam Langley95c29f32014-06-20 12:00:00 -0700383
Adam Langleyfcf25832014-12-18 17:42:32 -0800384int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800385 ctx->generate_session_id = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -0800386 return 1;
387}
Adam Langley95c29f32014-06-20 12:00:00 -0700388
Adam Langleyfcf25832014-12-18 17:42:32 -0800389int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800390 ssl->generate_session_id = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -0800391 return 1;
392}
Adam Langley95c29f32014-06-20 12:00:00 -0700393
Adam Langleyfcf25832014-12-18 17:42:32 -0800394int SSL_has_matching_session_id(const SSL *ssl, const uint8_t *id,
395 unsigned int id_len) {
396 /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how we
397 * can "construct" a session to give us the desired check - ie. to find if
398 * there's a session in the hash table that would conflict with any new
399 * session built out of this id/id_len and the ssl_version in use by this
400 * SSL. */
401 SSL_SESSION r, *p;
Adam Langley95c29f32014-06-20 12:00:00 -0700402
Adam Langleyfcf25832014-12-18 17:42:32 -0800403 if (id_len > sizeof r.session_id) {
404 return 0;
405 }
Adam Langley95c29f32014-06-20 12:00:00 -0700406
Adam Langleyfcf25832014-12-18 17:42:32 -0800407 r.ssl_version = ssl->version;
408 r.session_id_length = id_len;
409 memcpy(r.session_id, id, id_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700410
Adam Langley4bdb6e42015-05-15 15:29:21 -0700411 CRYPTO_MUTEX_lock_read(&ssl->ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -0800412 p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r);
Adam Langley4bdb6e42015-05-15 15:29:21 -0700413 CRYPTO_MUTEX_unlock(&ssl->ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -0800414 return p != NULL;
415}
Adam Langley95c29f32014-06-20 12:00:00 -0700416
Adam Langleyfcf25832014-12-18 17:42:32 -0800417int SSL_CTX_set_purpose(SSL_CTX *s, int purpose) {
418 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
419}
420
421int SSL_set_purpose(SSL *s, int purpose) {
422 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
423}
424
425int SSL_CTX_set_trust(SSL_CTX *s, int trust) {
426 return X509_VERIFY_PARAM_set_trust(s->param, trust);
427}
428
429int SSL_set_trust(SSL *s, int trust) {
430 return X509_VERIFY_PARAM_set_trust(s->param, trust);
431}
432
433int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm) {
434 return X509_VERIFY_PARAM_set1(ctx->param, vpm);
435}
436
437int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm) {
438 return X509_VERIFY_PARAM_set1(ssl->param, vpm);
439}
Adam Langley95c29f32014-06-20 12:00:00 -0700440
Adam Langley858a88d2014-06-20 12:00:00 -0700441void ssl_cipher_preference_list_free(
Adam Langleyfcf25832014-12-18 17:42:32 -0800442 struct ssl_cipher_preference_list_st *cipher_list) {
David Benjamin5d1ec732015-04-22 13:38:00 -0400443 if (cipher_list == NULL) {
444 return;
445 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800446 sk_SSL_CIPHER_free(cipher_list->ciphers);
447 OPENSSL_free(cipher_list->in_group_flags);
448 OPENSSL_free(cipher_list);
449}
Adam Langley858a88d2014-06-20 12:00:00 -0700450
Adam Langleyfcf25832014-12-18 17:42:32 -0800451struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_dup(
452 struct ssl_cipher_preference_list_st *cipher_list) {
453 struct ssl_cipher_preference_list_st *ret = NULL;
454 size_t n = sk_SSL_CIPHER_num(cipher_list->ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700455
Adam Langleyfcf25832014-12-18 17:42:32 -0800456 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
457 if (!ret) {
458 goto err;
459 }
460
461 ret->ciphers = NULL;
462 ret->in_group_flags = NULL;
463 ret->ciphers = sk_SSL_CIPHER_dup(cipher_list->ciphers);
464 if (!ret->ciphers) {
465 goto err;
466 }
467 ret->in_group_flags = BUF_memdup(cipher_list->in_group_flags, n);
468 if (!ret->in_group_flags) {
469 goto err;
470 }
471
472 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -0700473
474err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400475 ssl_cipher_preference_list_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -0800476 return NULL;
477}
Adam Langley858a88d2014-06-20 12:00:00 -0700478
Adam Langleyfcf25832014-12-18 17:42:32 -0800479struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_from_ciphers(
David Benjamin60da0cd2015-05-03 15:21:28 -0400480 STACK_OF(SSL_CIPHER) *ciphers) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800481 struct ssl_cipher_preference_list_st *ret = NULL;
482 size_t n = sk_SSL_CIPHER_num(ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700483
Adam Langleyfcf25832014-12-18 17:42:32 -0800484 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
485 if (!ret) {
486 goto err;
487 }
488 ret->ciphers = NULL;
489 ret->in_group_flags = NULL;
490 ret->ciphers = sk_SSL_CIPHER_dup(ciphers);
491 if (!ret->ciphers) {
492 goto err;
493 }
494 ret->in_group_flags = OPENSSL_malloc(n);
495 if (!ret->in_group_flags) {
496 goto err;
497 }
498 memset(ret->in_group_flags, 0, n);
499 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -0700500
501err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400502 ssl_cipher_preference_list_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -0800503 return NULL;
504}
Adam Langley858a88d2014-06-20 12:00:00 -0700505
Adam Langleyfcf25832014-12-18 17:42:32 -0800506X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) { return ctx->param; }
Adam Langley95c29f32014-06-20 12:00:00 -0700507
Adam Langleyfcf25832014-12-18 17:42:32 -0800508X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) { return ssl->param; }
Adam Langley95c29f32014-06-20 12:00:00 -0700509
David Benjamin7481d392015-07-05 19:38:46 -0400510void SSL_certs_clear(SSL *ssl) { ssl_cert_clear_certs(ssl->cert); }
Adam Langley95c29f32014-06-20 12:00:00 -0700511
David Benjamin4831c332015-05-16 11:43:13 -0400512void SSL_free(SSL *ssl) {
513 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800514 return;
515 }
Adam Langley95c29f32014-06-20 12:00:00 -0700516
David Benjamin4831c332015-05-16 11:43:13 -0400517 X509_VERIFY_PARAM_free(ssl->param);
Adam Langley95c29f32014-06-20 12:00:00 -0700518
David Benjamin4831c332015-05-16 11:43:13 -0400519 CRYPTO_free_ex_data(&g_ex_data_class_ssl, ssl, &ssl->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700520
David Benjamin4831c332015-05-16 11:43:13 -0400521 if (ssl->bbio != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800522 /* If the buffering BIO is in place, pop it off */
David Benjamin4831c332015-05-16 11:43:13 -0400523 if (ssl->bbio == ssl->wbio) {
524 ssl->wbio = BIO_pop(ssl->wbio);
Adam Langleyfcf25832014-12-18 17:42:32 -0800525 }
David Benjamin4831c332015-05-16 11:43:13 -0400526 BIO_free(ssl->bbio);
527 ssl->bbio = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800528 }
Adam Langley95c29f32014-06-20 12:00:00 -0700529
David Benjamin4831c332015-05-16 11:43:13 -0400530 int free_wbio = ssl->wbio != ssl->rbio;
531 BIO_free_all(ssl->rbio);
David Benjamin2755a3e2015-04-22 16:17:58 -0400532 if (free_wbio) {
David Benjamin4831c332015-05-16 11:43:13 -0400533 BIO_free_all(ssl->wbio);
Adam Langleyfcf25832014-12-18 17:42:32 -0800534 }
Adam Langley95c29f32014-06-20 12:00:00 -0700535
David Benjamin4831c332015-05-16 11:43:13 -0400536 BUF_MEM_free(ssl->init_buf);
Adam Langley95c29f32014-06-20 12:00:00 -0700537
Adam Langleyfcf25832014-12-18 17:42:32 -0800538 /* add extra stuff */
David Benjamin4831c332015-05-16 11:43:13 -0400539 ssl_cipher_preference_list_free(ssl->cipher_list);
540 sk_SSL_CIPHER_free(ssl->cipher_list_by_id);
Adam Langley95c29f32014-06-20 12:00:00 -0700541
David Benjamin4831c332015-05-16 11:43:13 -0400542 ssl_clear_bad_session(ssl);
543 SSL_SESSION_free(ssl->session);
Adam Langley95c29f32014-06-20 12:00:00 -0700544
David Benjamin4831c332015-05-16 11:43:13 -0400545 ssl_clear_cipher_ctx(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700546
David Benjamin4831c332015-05-16 11:43:13 -0400547 ssl_cert_free(ssl->cert);
Adam Langley0289c732014-06-20 12:00:00 -0700548
David Benjamin4831c332015-05-16 11:43:13 -0400549 OPENSSL_free(ssl->tlsext_hostname);
550 SSL_CTX_free(ssl->initial_ctx);
David Benjamin4831c332015-05-16 11:43:13 -0400551 OPENSSL_free(ssl->tlsext_ellipticcurvelist);
552 OPENSSL_free(ssl->alpn_client_proto_list);
553 EVP_PKEY_free(ssl->tlsext_channel_id_private);
554 OPENSSL_free(ssl->psk_identity_hint);
555 sk_X509_NAME_pop_free(ssl->client_CA, X509_NAME_free);
556 OPENSSL_free(ssl->next_proto_negotiated);
557 sk_SRTP_PROTECTION_PROFILE_free(ssl->srtp_profiles);
Adam Langley95c29f32014-06-20 12:00:00 -0700558
David Benjamin4831c332015-05-16 11:43:13 -0400559 if (ssl->method != NULL) {
560 ssl->method->ssl_free(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800561 }
David Benjamin4831c332015-05-16 11:43:13 -0400562 SSL_CTX_free(ssl->ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700563
David Benjamin4831c332015-05-16 11:43:13 -0400564 OPENSSL_free(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800565}
Adam Langley95c29f32014-06-20 12:00:00 -0700566
Adam Langleyfcf25832014-12-18 17:42:32 -0800567void SSL_set_bio(SSL *s, BIO *rbio, BIO *wbio) {
568 /* If the output buffering BIO is still in place, remove it. */
569 if (s->bbio != NULL) {
570 if (s->wbio == s->bbio) {
571 s->wbio = s->wbio->next_bio;
572 s->bbio->next_bio = NULL;
573 }
574 }
Adam Langley95c29f32014-06-20 12:00:00 -0700575
David Benjamin2755a3e2015-04-22 16:17:58 -0400576 if (s->rbio != rbio) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800577 BIO_free_all(s->rbio);
578 }
David Benjamin2755a3e2015-04-22 16:17:58 -0400579 if (s->wbio != wbio && s->rbio != s->wbio) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800580 BIO_free_all(s->wbio);
581 }
582 s->rbio = rbio;
583 s->wbio = wbio;
584}
Adam Langley95c29f32014-06-20 12:00:00 -0700585
Adam Langleyfcf25832014-12-18 17:42:32 -0800586BIO *SSL_get_rbio(const SSL *s) { return s->rbio; }
Adam Langley95c29f32014-06-20 12:00:00 -0700587
Adam Langleyfcf25832014-12-18 17:42:32 -0800588BIO *SSL_get_wbio(const SSL *s) { return s->wbio; }
Adam Langley95c29f32014-06-20 12:00:00 -0700589
Adam Langleyfcf25832014-12-18 17:42:32 -0800590int SSL_get_fd(const SSL *s) { return SSL_get_rfd(s); }
Adam Langley95c29f32014-06-20 12:00:00 -0700591
Adam Langleyfcf25832014-12-18 17:42:32 -0800592int SSL_get_rfd(const SSL *s) {
593 int ret = -1;
594 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700595
Adam Langleyfcf25832014-12-18 17:42:32 -0800596 b = SSL_get_rbio(s);
597 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
598 if (r != NULL) {
599 BIO_get_fd(r, &ret);
600 }
601 return ret;
602}
Adam Langley95c29f32014-06-20 12:00:00 -0700603
Adam Langleyfcf25832014-12-18 17:42:32 -0800604int SSL_get_wfd(const SSL *s) {
605 int ret = -1;
606 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700607
Adam Langleyfcf25832014-12-18 17:42:32 -0800608 b = SSL_get_wbio(s);
609 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
610 if (r != NULL) {
611 BIO_get_fd(r, &ret);
612 }
Adam Langley95c29f32014-06-20 12:00:00 -0700613
Adam Langleyfcf25832014-12-18 17:42:32 -0800614 return ret;
615}
Adam Langley95c29f32014-06-20 12:00:00 -0700616
Adam Langleyfcf25832014-12-18 17:42:32 -0800617int SSL_set_fd(SSL *s, int fd) {
618 int ret = 0;
619 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700620
Adam Langleyfcf25832014-12-18 17:42:32 -0800621 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700622
Adam Langleyfcf25832014-12-18 17:42:32 -0800623 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400624 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -0800625 goto err;
626 }
627 BIO_set_fd(bio, fd, BIO_NOCLOSE);
628 SSL_set_bio(s, bio, bio);
629 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700630
Adam Langley95c29f32014-06-20 12:00:00 -0700631err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800632 return ret;
633}
Adam Langley95c29f32014-06-20 12:00:00 -0700634
Adam Langleyfcf25832014-12-18 17:42:32 -0800635int SSL_set_wfd(SSL *s, int fd) {
636 int ret = 0;
637 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700638
Adam Langleyfcf25832014-12-18 17:42:32 -0800639 if (s->rbio == NULL || BIO_method_type(s->rbio) != BIO_TYPE_FD ||
640 (int)BIO_get_fd(s->rbio, NULL) != fd) {
641 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700642
Adam Langleyfcf25832014-12-18 17:42:32 -0800643 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400644 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -0800645 goto err;
646 }
647 BIO_set_fd(bio, fd, BIO_NOCLOSE);
648 SSL_set_bio(s, SSL_get_rbio(s), bio);
649 } else {
650 SSL_set_bio(s, SSL_get_rbio(s), SSL_get_rbio(s));
651 }
652
653 ret = 1;
654
Adam Langley95c29f32014-06-20 12:00:00 -0700655err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800656 return ret;
657}
Adam Langley95c29f32014-06-20 12:00:00 -0700658
Adam Langleyfcf25832014-12-18 17:42:32 -0800659int SSL_set_rfd(SSL *s, int fd) {
660 int ret = 0;
661 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700662
Adam Langleyfcf25832014-12-18 17:42:32 -0800663 if (s->wbio == NULL || BIO_method_type(s->wbio) != BIO_TYPE_FD ||
664 (int)BIO_get_fd(s->wbio, NULL) != fd) {
665 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700666
Adam Langleyfcf25832014-12-18 17:42:32 -0800667 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400668 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -0800669 goto err;
670 }
671 BIO_set_fd(bio, fd, BIO_NOCLOSE);
672 SSL_set_bio(s, bio, SSL_get_wbio(s));
673 } else {
674 SSL_set_bio(s, SSL_get_wbio(s), SSL_get_wbio(s));
675 }
676 ret = 1;
677
Adam Langley95c29f32014-06-20 12:00:00 -0700678err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800679 return ret;
680}
Adam Langley95c29f32014-06-20 12:00:00 -0700681
682/* return length of latest Finished message we sent, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -0800683size_t SSL_get_finished(const SSL *s, void *buf, size_t count) {
684 size_t ret = 0;
685
686 if (s->s3 != NULL) {
687 ret = s->s3->tmp.finish_md_len;
688 if (count > ret) {
689 count = ret;
690 }
691 memcpy(buf, s->s3->tmp.finish_md, count);
692 }
693
694 return ret;
695}
Adam Langley95c29f32014-06-20 12:00:00 -0700696
697/* return length of latest Finished message we expected, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -0800698size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count) {
699 size_t ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700700
Adam Langleyfcf25832014-12-18 17:42:32 -0800701 if (s->s3 != NULL) {
702 ret = s->s3->tmp.peer_finish_md_len;
703 if (count > ret) {
704 count = ret;
705 }
706 memcpy(buf, s->s3->tmp.peer_finish_md, count);
707 }
Adam Langley95c29f32014-06-20 12:00:00 -0700708
Adam Langleyfcf25832014-12-18 17:42:32 -0800709 return ret;
710}
Adam Langley95c29f32014-06-20 12:00:00 -0700711
Adam Langleyfcf25832014-12-18 17:42:32 -0800712int SSL_get_verify_mode(const SSL *s) { return s->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -0700713
Adam Langleyfcf25832014-12-18 17:42:32 -0800714int SSL_get_verify_depth(const SSL *s) {
715 return X509_VERIFY_PARAM_get_depth(s->param);
716}
Adam Langley95c29f32014-06-20 12:00:00 -0700717
Adam Langleyfcf25832014-12-18 17:42:32 -0800718int (*SSL_get_verify_callback(const SSL *s))(int, X509_STORE_CTX *) {
719 return s->verify_callback;
720}
Adam Langley95c29f32014-06-20 12:00:00 -0700721
Adam Langleyfcf25832014-12-18 17:42:32 -0800722int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) { return ctx->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -0700723
Adam Langleyfcf25832014-12-18 17:42:32 -0800724int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) {
725 return X509_VERIFY_PARAM_get_depth(ctx->param);
726}
Adam Langley95c29f32014-06-20 12:00:00 -0700727
Adam Langleyfcf25832014-12-18 17:42:32 -0800728int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int, X509_STORE_CTX *) {
729 return ctx->default_verify_callback;
730}
Adam Langley95c29f32014-06-20 12:00:00 -0700731
Adam Langleyfcf25832014-12-18 17:42:32 -0800732void SSL_set_verify(SSL *s, int mode,
733 int (*callback)(int ok, X509_STORE_CTX *ctx)) {
734 s->verify_mode = mode;
735 if (callback != NULL) {
736 s->verify_callback = callback;
737 }
738}
Adam Langley95c29f32014-06-20 12:00:00 -0700739
Adam Langleyfcf25832014-12-18 17:42:32 -0800740void SSL_set_verify_depth(SSL *s, int depth) {
741 X509_VERIFY_PARAM_set_depth(s->param, depth);
742}
Adam Langley95c29f32014-06-20 12:00:00 -0700743
David Benjamin9a41d1b2015-05-16 01:30:09 -0400744int SSL_CTX_get_read_ahead(const SSL_CTX *ctx) { return 0; }
Adam Langley95c29f32014-06-20 12:00:00 -0700745
David Benjamin9a41d1b2015-05-16 01:30:09 -0400746int SSL_get_read_ahead(const SSL *s) { return 0; }
Adam Langley95c29f32014-06-20 12:00:00 -0700747
David Benjamin9a41d1b2015-05-16 01:30:09 -0400748void SSL_CTX_set_read_ahead(SSL_CTX *ctx, int yes) { }
David Benjamin61ecccf2015-05-05 09:44:51 -0400749
David Benjamin9a41d1b2015-05-16 01:30:09 -0400750void SSL_set_read_ahead(SSL *s, int yes) { }
David Benjamin61ecccf2015-05-05 09:44:51 -0400751
Adam Langleyfcf25832014-12-18 17:42:32 -0800752int SSL_pending(const SSL *s) {
David Benjamin904dc722015-05-30 16:39:07 -0400753 if (s->rstate == SSL_ST_READ_BODY) {
754 return 0;
755 }
756
757 return (s->s3->rrec.type == SSL3_RT_APPLICATION_DATA) ? s->s3->rrec.length
758 : 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800759}
Adam Langley95c29f32014-06-20 12:00:00 -0700760
Adam Langleyfcf25832014-12-18 17:42:32 -0800761X509 *SSL_get_peer_certificate(const SSL *s) {
762 X509 *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700763
Adam Langleyfcf25832014-12-18 17:42:32 -0800764 if (s == NULL || s->session == NULL) {
765 r = NULL;
766 } else {
767 r = s->session->peer;
768 }
Adam Langley95c29f32014-06-20 12:00:00 -0700769
Adam Langleyfcf25832014-12-18 17:42:32 -0800770 if (r == NULL) {
771 return NULL;
772 }
Adam Langley95c29f32014-06-20 12:00:00 -0700773
Adam Langleyfcf25832014-12-18 17:42:32 -0800774 return X509_up_ref(r);
775}
776
David Benjamin60da0cd2015-05-03 15:21:28 -0400777STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s) {
778 STACK_OF(X509) *r;
Adam Langleyfcf25832014-12-18 17:42:32 -0800779
780 if (s == NULL || s->session == NULL || s->session->sess_cert == NULL) {
781 r = NULL;
782 } else {
783 r = s->session->sess_cert->cert_chain;
784 }
785
786 /* If we are a client, cert_chain includes the peer's own certificate; if we
787 * are a server, it does not. */
788 return r;
789}
Adam Langley95c29f32014-06-20 12:00:00 -0700790
Adam Langley95c29f32014-06-20 12:00:00 -0700791/* Fix this so it checks all the valid key/cert options */
Adam Langleyfcf25832014-12-18 17:42:32 -0800792int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
David Benjamind1d80782015-07-05 11:54:09 -0400793 if (ctx == NULL || ctx->cert == NULL || ctx->cert->x509 == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400794 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800795 return 0;
796 }
797
David Benjamind1d80782015-07-05 11:54:09 -0400798 if (ctx->cert->privatekey == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400799 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800800 return 0;
801 }
802
David Benjamind1d80782015-07-05 11:54:09 -0400803 return X509_check_private_key(ctx->cert->x509, ctx->cert->privatekey);
Adam Langleyfcf25832014-12-18 17:42:32 -0800804}
Adam Langley95c29f32014-06-20 12:00:00 -0700805
806/* Fix this function so that it takes an optional type parameter */
Adam Langleyfcf25832014-12-18 17:42:32 -0800807int SSL_check_private_key(const SSL *ssl) {
808 if (ssl == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400809 OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
Adam Langleyfcf25832014-12-18 17:42:32 -0800810 return 0;
811 }
Adam Langley95c29f32014-06-20 12:00:00 -0700812
Adam Langleyfcf25832014-12-18 17:42:32 -0800813 if (ssl->cert == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400814 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800815 return 0;
816 }
Adam Langley95c29f32014-06-20 12:00:00 -0700817
David Benjamind1d80782015-07-05 11:54:09 -0400818 if (ssl->cert->x509 == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400819 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800820 return 0;
821 }
David Benjamin0b145c22014-11-26 20:10:09 -0500822
David Benjamind1d80782015-07-05 11:54:09 -0400823 if (ssl->cert->privatekey == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400824 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800825 return 0;
826 }
Adam Langley95c29f32014-06-20 12:00:00 -0700827
David Benjamind1d80782015-07-05 11:54:09 -0400828 return X509_check_private_key(ssl->cert->x509, ssl->cert->privatekey);
Adam Langleyfcf25832014-12-18 17:42:32 -0800829}
Adam Langley95c29f32014-06-20 12:00:00 -0700830
Adam Langleyfcf25832014-12-18 17:42:32 -0800831int SSL_accept(SSL *s) {
832 if (s->handshake_func == 0) {
833 /* Not properly initialized yet */
834 SSL_set_accept_state(s);
835 }
David Benjamin0b145c22014-11-26 20:10:09 -0500836
Adam Langleyfcf25832014-12-18 17:42:32 -0800837 if (s->handshake_func != s->method->ssl_accept) {
David Benjamin3570d732015-06-29 00:28:17 -0400838 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800839 return -1;
840 }
Adam Langley95c29f32014-06-20 12:00:00 -0700841
Adam Langleyfcf25832014-12-18 17:42:32 -0800842 return s->handshake_func(s);
843}
Adam Langley95c29f32014-06-20 12:00:00 -0700844
Adam Langleyfcf25832014-12-18 17:42:32 -0800845int SSL_connect(SSL *s) {
846 if (s->handshake_func == 0) {
847 /* Not properly initialized yet */
848 SSL_set_connect_state(s);
849 }
Adam Langley95c29f32014-06-20 12:00:00 -0700850
Adam Langleyfcf25832014-12-18 17:42:32 -0800851 if (s->handshake_func != s->method->ssl_connect) {
David Benjamin3570d732015-06-29 00:28:17 -0400852 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800853 return -1;
854 }
Adam Langley95c29f32014-06-20 12:00:00 -0700855
Adam Langleyfcf25832014-12-18 17:42:32 -0800856 return s->handshake_func(s);
857}
Adam Langley95c29f32014-06-20 12:00:00 -0700858
Adam Langleyfcf25832014-12-18 17:42:32 -0800859long SSL_get_default_timeout(const SSL *s) {
860 return SSL_DEFAULT_SESSION_TIMEOUT;
861}
Adam Langley95c29f32014-06-20 12:00:00 -0700862
Adam Langleyfcf25832014-12-18 17:42:32 -0800863int SSL_read(SSL *s, void *buf, int num) {
864 if (s->handshake_func == 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400865 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800866 return -1;
867 }
Adam Langley95c29f32014-06-20 12:00:00 -0700868
Adam Langleyfcf25832014-12-18 17:42:32 -0800869 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
870 s->rwstate = SSL_NOTHING;
871 return 0;
872 }
Adam Langley95c29f32014-06-20 12:00:00 -0700873
David Benjamin904dc722015-05-30 16:39:07 -0400874 ERR_clear_system_error();
875 return s->method->ssl_read_app_data(s, buf, num, 0);
Adam Langleyfcf25832014-12-18 17:42:32 -0800876}
Adam Langley95c29f32014-06-20 12:00:00 -0700877
Adam Langleyfcf25832014-12-18 17:42:32 -0800878int SSL_peek(SSL *s, void *buf, int num) {
879 if (s->handshake_func == 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400880 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800881 return -1;
882 }
Adam Langley95c29f32014-06-20 12:00:00 -0700883
Adam Langleyfcf25832014-12-18 17:42:32 -0800884 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
885 return 0;
886 }
Adam Langley95c29f32014-06-20 12:00:00 -0700887
David Benjamin904dc722015-05-30 16:39:07 -0400888 ERR_clear_system_error();
889 return s->method->ssl_read_app_data(s, buf, num, 1);
Adam Langleyfcf25832014-12-18 17:42:32 -0800890}
Adam Langley95c29f32014-06-20 12:00:00 -0700891
Adam Langleyfcf25832014-12-18 17:42:32 -0800892int SSL_write(SSL *s, const void *buf, int num) {
893 if (s->handshake_func == 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400894 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800895 return -1;
896 }
Adam Langley95c29f32014-06-20 12:00:00 -0700897
Adam Langleyfcf25832014-12-18 17:42:32 -0800898 if (s->shutdown & SSL_SENT_SHUTDOWN) {
899 s->rwstate = SSL_NOTHING;
David Benjamin3570d732015-06-29 00:28:17 -0400900 OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
Adam Langleyfcf25832014-12-18 17:42:32 -0800901 return -1;
902 }
Adam Langley95c29f32014-06-20 12:00:00 -0700903
David Benjamin904dc722015-05-30 16:39:07 -0400904 ERR_clear_system_error();
905 return s->method->ssl_write_app_data(s, buf, num);
Adam Langleyfcf25832014-12-18 17:42:32 -0800906}
Adam Langley95c29f32014-06-20 12:00:00 -0700907
Adam Langleyfcf25832014-12-18 17:42:32 -0800908int SSL_shutdown(SSL *s) {
909 /* Note that this function behaves differently from what one might expect.
910 * Return values are 0 for no success (yet), 1 for success; but calling it
911 * once is usually not enough, even if blocking I/O is used (see
912 * ssl3_shutdown). */
Adam Langley95c29f32014-06-20 12:00:00 -0700913
Adam Langleyfcf25832014-12-18 17:42:32 -0800914 if (s->handshake_func == 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400915 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800916 return -1;
917 }
Adam Langley95c29f32014-06-20 12:00:00 -0700918
David Benjamin904dc722015-05-30 16:39:07 -0400919 if (SSL_in_init(s)) {
920 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800921 }
Adam Langley95c29f32014-06-20 12:00:00 -0700922
David Benjamin904dc722015-05-30 16:39:07 -0400923 /* Do nothing if configured not to send a close_notify. */
924 if (s->quiet_shutdown) {
925 s->shutdown = SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN;
926 return 1;
927 }
928
929 if (!(s->shutdown & SSL_SENT_SHUTDOWN)) {
930 s->shutdown |= SSL_SENT_SHUTDOWN;
931 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_CLOSE_NOTIFY);
932
933 /* our shutdown alert has been sent now, and if it still needs to be
934 * written, s->s3->alert_dispatch will be true */
935 if (s->s3->alert_dispatch) {
936 return -1; /* return WANT_WRITE */
937 }
938 } else if (s->s3->alert_dispatch) {
939 /* resend it if not sent */
940 int ret = s->method->ssl_dispatch_alert(s);
941 if (ret == -1) {
942 /* we only get to return -1 here the 2nd/Nth invocation, we must have
943 * already signalled return 0 upon a previous invoation, return
944 * WANT_WRITE */
945 return ret;
946 }
947 } else if (!(s->shutdown & SSL_RECEIVED_SHUTDOWN)) {
948 /* If we are waiting for a close from our peer, we are closed */
949 s->method->ssl_read_close_notify(s);
950 if (!(s->shutdown & SSL_RECEIVED_SHUTDOWN)) {
951 return -1; /* return WANT_READ */
952 }
953 }
954
955 if (s->shutdown == (SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN) &&
956 !s->s3->alert_dispatch) {
957 return 1;
958 } else {
959 return 0;
960 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800961}
Adam Langley95c29f32014-06-20 12:00:00 -0700962
David Benjamin44d3eed2015-05-21 01:29:55 -0400963int SSL_renegotiate(SSL *ssl) {
964 /* Caller-initiated renegotiation is not supported. */
David Benjamin3570d732015-06-29 00:28:17 -0400965 OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
David Benjamin44d3eed2015-05-21 01:29:55 -0400966 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800967}
Adam Langley95c29f32014-06-20 12:00:00 -0700968
David Benjamin44d3eed2015-05-21 01:29:55 -0400969int SSL_renegotiate_pending(SSL *ssl) {
970 return SSL_in_init(ssl) && ssl->s3->initial_handshake_complete;
Adam Langleyfcf25832014-12-18 17:42:32 -0800971}
Adam Langley95c29f32014-06-20 12:00:00 -0700972
David Benjamin61ecccf2015-05-05 09:44:51 -0400973uint32_t SSL_CTX_set_options(SSL_CTX *ctx, uint32_t options) {
974 ctx->options |= options;
975 return ctx->options;
976}
977
978uint32_t SSL_set_options(SSL *ssl, uint32_t options) {
979 ssl->options |= options;
980 return ssl->options;
981}
982
983uint32_t SSL_CTX_clear_options(SSL_CTX *ctx, uint32_t options) {
984 ctx->options &= ~options;
985 return ctx->options;
986}
987
988uint32_t SSL_clear_options(SSL *ssl, uint32_t options) {
989 ssl->options &= ~options;
990 return ssl->options;
991}
992
993uint32_t SSL_CTX_get_options(const SSL_CTX *ctx) { return ctx->options; }
994
995uint32_t SSL_get_options(const SSL *ssl) { return ssl->options; }
996
997uint32_t SSL_CTX_set_mode(SSL_CTX *ctx, uint32_t mode) {
998 ctx->mode |= mode;
999 return ctx->mode;
1000}
1001
1002uint32_t SSL_set_mode(SSL *ssl, uint32_t mode) {
1003 ssl->mode |= mode;
1004 return ssl->mode;
1005}
1006
1007uint32_t SSL_CTX_clear_mode(SSL_CTX *ctx, uint32_t mode) {
1008 ctx->mode &= ~mode;
1009 return ctx->mode;
1010}
1011
1012uint32_t SSL_clear_mode(SSL *ssl, uint32_t mode) {
1013 ssl->mode &= ~mode;
1014 return ssl->mode;
1015}
1016
1017uint32_t SSL_CTX_get_mode(const SSL_CTX *ctx) { return ctx->mode; }
1018
1019uint32_t SSL_get_mode(const SSL *ssl) { return ssl->mode; }
1020
David Benjamin1d0a1942015-04-26 15:35:35 -04001021size_t SSL_CTX_get_max_cert_list(const SSL_CTX *ctx) {
1022 return ctx->max_cert_list;
1023}
1024
1025void SSL_CTX_set_max_cert_list(SSL_CTX *ctx, size_t max_cert_list) {
1026 if (max_cert_list > kMaxHandshakeSize) {
1027 max_cert_list = kMaxHandshakeSize;
1028 }
1029 ctx->max_cert_list = (uint32_t)max_cert_list;
1030}
1031
1032size_t SSL_get_max_cert_list(const SSL *ssl) {
1033 return ssl->max_cert_list;
1034}
1035
1036void SSL_set_max_cert_list(SSL *ssl, size_t max_cert_list) {
1037 if (max_cert_list > kMaxHandshakeSize) {
1038 max_cert_list = kMaxHandshakeSize;
1039 }
1040 ssl->max_cert_list = (uint32_t)max_cert_list;
1041}
1042
1043void SSL_CTX_set_max_send_fragment(SSL_CTX *ctx, size_t max_send_fragment) {
1044 if (max_send_fragment < 512) {
1045 max_send_fragment = 512;
1046 }
1047 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1048 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1049 }
1050 ctx->max_send_fragment = (uint16_t)max_send_fragment;
1051}
1052
1053void SSL_set_max_send_fragment(SSL *ssl, size_t max_send_fragment) {
1054 if (max_send_fragment < 512) {
1055 max_send_fragment = 512;
1056 }
1057 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1058 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1059 }
1060 ssl->max_send_fragment = (uint16_t)max_send_fragment;
1061}
1062
David Benjamincb9cf792015-05-05 09:46:14 -04001063int SSL_set_mtu(SSL *ssl, unsigned mtu) {
1064 if (!SSL_IS_DTLS(ssl) || mtu < dtls1_min_mtu()) {
1065 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001066 }
David Benjamincb9cf792015-05-05 09:46:14 -04001067 ssl->d1->mtu = mtu;
1068 return 1;
1069}
1070
1071int SSL_get_secure_renegotiation_support(const SSL *ssl) {
1072 return ssl->s3->send_connection_binding;
1073}
1074
1075long SSL_ctrl(SSL *s, int cmd, long larg, void *parg) {
1076 return s->method->ssl_ctrl(s, cmd, larg, parg);
Adam Langleyfcf25832014-12-18 17:42:32 -08001077}
1078
Adam Langleyfcf25832014-12-18 17:42:32 -08001079LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) { return ctx->sessions; }
1080
David Benjamin71f7d3d2015-05-05 09:46:38 -04001081size_t SSL_CTX_sess_number(const SSL_CTX *ctx) {
1082 return lh_SSL_SESSION_num_items(ctx->sessions);
1083}
1084
1085unsigned long SSL_CTX_sess_set_cache_size(SSL_CTX *ctx, unsigned long size) {
1086 unsigned long ret = ctx->session_cache_size;
1087 ctx->session_cache_size = size;
1088 return ret;
1089}
1090
1091unsigned long SSL_CTX_sess_get_cache_size(const SSL_CTX *ctx) {
1092 return ctx->session_cache_size;
1093}
1094
1095int SSL_CTX_set_session_cache_mode(SSL_CTX *ctx, int mode) {
1096 int ret = ctx->session_cache_mode;
1097 ctx->session_cache_mode = mode;
1098 return ret;
1099}
1100
1101int SSL_CTX_get_session_cache_mode(const SSL_CTX *ctx) {
1102 return ctx->session_cache_mode;
1103}
1104
Adam Langleyfcf25832014-12-18 17:42:32 -08001105long SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg) {
David Benjamin71f7d3d2015-05-05 09:46:38 -04001106 return ctx->method->ssl_ctx_ctrl(ctx, cmd, larg, parg);
Adam Langleyfcf25832014-12-18 17:42:32 -08001107}
1108
Adam Langleyfcf25832014-12-18 17:42:32 -08001109/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001110 * preference */
David Benjamin60da0cd2015-05-03 15:21:28 -04001111STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001112 if (s == NULL) {
1113 return NULL;
1114 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001115
Adam Langleyfcf25832014-12-18 17:42:32 -08001116 if (s->cipher_list != NULL) {
1117 return s->cipher_list->ciphers;
1118 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001119
Adam Langleyfcf25832014-12-18 17:42:32 -08001120 if (s->version >= TLS1_1_VERSION && s->ctx != NULL &&
1121 s->ctx->cipher_list_tls11 != NULL) {
1122 return s->ctx->cipher_list_tls11->ciphers;
1123 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001124
Adam Langleyfcf25832014-12-18 17:42:32 -08001125 if (s->ctx != NULL && s->ctx->cipher_list != NULL) {
1126 return s->ctx->cipher_list->ciphers;
1127 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001128
Adam Langleyfcf25832014-12-18 17:42:32 -08001129 return NULL;
1130}
Adam Langley95c29f32014-06-20 12:00:00 -07001131
Adam Langleyfcf25832014-12-18 17:42:32 -08001132/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001133 * algorithm id */
David Benjamin60da0cd2015-05-03 15:21:28 -04001134STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001135 if (s == NULL) {
1136 return NULL;
1137 }
Adam Langley95c29f32014-06-20 12:00:00 -07001138
Adam Langleyfcf25832014-12-18 17:42:32 -08001139 if (s->cipher_list_by_id != NULL) {
1140 return s->cipher_list_by_id;
1141 }
Adam Langley95c29f32014-06-20 12:00:00 -07001142
Adam Langleyfcf25832014-12-18 17:42:32 -08001143 if (s->ctx != NULL && s->ctx->cipher_list_by_id != NULL) {
1144 return s->ctx->cipher_list_by_id;
1145 }
Adam Langley95c29f32014-06-20 12:00:00 -07001146
Adam Langleyfcf25832014-12-18 17:42:32 -08001147 return NULL;
1148}
Adam Langley95c29f32014-06-20 12:00:00 -07001149
Adam Langleyfcf25832014-12-18 17:42:32 -08001150/* The old interface to get the same thing as SSL_get_ciphers() */
1151const char *SSL_get_cipher_list(const SSL *s, int n) {
1152 const SSL_CIPHER *c;
David Benjamin60da0cd2015-05-03 15:21:28 -04001153 STACK_OF(SSL_CIPHER) *sk;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001154
Adam Langleyfcf25832014-12-18 17:42:32 -08001155 if (s == NULL) {
1156 return NULL;
1157 }
Adam Langley95c29f32014-06-20 12:00:00 -07001158
Adam Langleyfcf25832014-12-18 17:42:32 -08001159 sk = SSL_get_ciphers(s);
1160 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
1161 return NULL;
1162 }
Adam Langley95c29f32014-06-20 12:00:00 -07001163
Adam Langleyfcf25832014-12-18 17:42:32 -08001164 c = sk_SSL_CIPHER_value(sk, n);
1165 if (c == NULL) {
1166 return NULL;
1167 }
Adam Langley95c29f32014-06-20 12:00:00 -07001168
Adam Langleyfcf25832014-12-18 17:42:32 -08001169 return c->name;
1170}
David Benjamin5491e3f2014-09-29 19:33:09 -04001171
Adam Langleyfcf25832014-12-18 17:42:32 -08001172/* specify the ciphers to be used by default by the SSL_CTX */
1173int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
1174 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001175
Adam Langleyfcf25832014-12-18 17:42:32 -08001176 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001177 &ctx->cipher_list_by_id, str);
Adam Langleyfcf25832014-12-18 17:42:32 -08001178 /* ssl_create_cipher_list may return an empty stack if it was unable to find
1179 * a cipher matching the given rule string (for example if the rule string
1180 * specifies a cipher which has been disabled). This is not an error as far
1181 * as ssl_create_cipher_list is concerned, and hence ctx->cipher_list and
1182 * ctx->cipher_list_by_id has been updated. */
1183 if (sk == NULL) {
1184 return 0;
1185 } else if (sk_SSL_CIPHER_num(sk) == 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001186 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
Adam Langleyfcf25832014-12-18 17:42:32 -08001187 return 0;
1188 }
Adam Langley95c29f32014-06-20 12:00:00 -07001189
Adam Langleyfcf25832014-12-18 17:42:32 -08001190 return 1;
1191}
David Benjamin39482a12014-07-20 13:30:15 -04001192
Adam Langleyfcf25832014-12-18 17:42:32 -08001193int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str) {
1194 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001195
David Benjamin71f07942015-04-08 02:36:59 -04001196 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str);
Adam Langleyfcf25832014-12-18 17:42:32 -08001197 if (sk == NULL) {
1198 return 0;
1199 } else if (sk_SSL_CIPHER_num(sk) == 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001200 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
Adam Langleyfcf25832014-12-18 17:42:32 -08001201 return 0;
1202 }
David Benjamin9f2c0d72014-10-21 22:00:19 -04001203
Adam Langleyfcf25832014-12-18 17:42:32 -08001204 return 1;
1205}
Adam Langley95c29f32014-06-20 12:00:00 -07001206
Adam Langleyfcf25832014-12-18 17:42:32 -08001207/* specify the ciphers to be used by the SSL */
1208int SSL_set_cipher_list(SSL *s, const char *str) {
1209 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001210
Adam Langleyfcf25832014-12-18 17:42:32 -08001211 sk = ssl_create_cipher_list(s->ctx->method, &s->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001212 &s->cipher_list_by_id, str);
David Benjamin39482a12014-07-20 13:30:15 -04001213
Adam Langleyfcf25832014-12-18 17:42:32 -08001214 /* see comment in SSL_CTX_set_cipher_list */
1215 if (sk == NULL) {
1216 return 0;
1217 } else if (sk_SSL_CIPHER_num(sk) == 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001218 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
Adam Langleyfcf25832014-12-18 17:42:32 -08001219 return 0;
1220 }
David Benjamin39482a12014-07-20 13:30:15 -04001221
Adam Langleyfcf25832014-12-18 17:42:32 -08001222 return 1;
1223}
Adam Langley95c29f32014-06-20 12:00:00 -07001224
Adam Langleyfcf25832014-12-18 17:42:32 -08001225int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, uint8_t *p) {
1226 size_t i;
1227 const SSL_CIPHER *c;
1228 CERT *ct = s->cert;
1229 uint8_t *q;
1230 /* Set disabled masks for this session */
1231 ssl_set_client_disabled(s);
Adam Langley29707792014-06-20 12:00:00 -07001232
Adam Langleyfcf25832014-12-18 17:42:32 -08001233 if (sk == NULL) {
1234 return 0;
1235 }
1236 q = p;
Adam Langley95c29f32014-06-20 12:00:00 -07001237
Adam Langleyfcf25832014-12-18 17:42:32 -08001238 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
1239 c = sk_SSL_CIPHER_value(sk, i);
1240 /* Skip disabled ciphers */
1241 if (c->algorithm_ssl & ct->mask_ssl ||
1242 c->algorithm_mkey & ct->mask_k ||
1243 c->algorithm_auth & ct->mask_a) {
1244 continue;
1245 }
David Benjamina1c90a52015-05-30 17:03:14 -04001246 s2n(ssl_cipher_get_value(c), p);
Adam Langleyfcf25832014-12-18 17:42:32 -08001247 }
1248
1249 /* If all ciphers were disabled, return the error to the caller. */
1250 if (p == q) {
1251 return 0;
1252 }
1253
Adam Langley5021b222015-06-12 18:27:58 -07001254 /* For SSLv3, the SCSV is added. Otherwise the renegotiation extension is
1255 * added. */
1256 if (s->client_version == SSL3_VERSION &&
1257 !s->s3->initial_handshake_complete) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001258 s2n(SSL3_CK_SCSV & 0xffff, p);
Adam Langley5021b222015-06-12 18:27:58 -07001259 /* The renegotiation extension is required to be at index zero. */
1260 s->s3->tmp.extensions.sent |= (1u << 0);
Adam Langleyfcf25832014-12-18 17:42:32 -08001261 }
1262
Adam Langley5f0efe02015-02-20 13:03:16 -08001263 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001264 s2n(SSL3_CK_FALLBACK_SCSV & 0xffff, p);
1265 }
1266
1267 return p - q;
1268}
1269
1270STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs) {
1271 CBS cipher_suites = *cbs;
1272 const SSL_CIPHER *c;
David Benjamin60da0cd2015-05-03 15:21:28 -04001273 STACK_OF(SSL_CIPHER) *sk;
Adam Langleyfcf25832014-12-18 17:42:32 -08001274
1275 if (s->s3) {
1276 s->s3->send_connection_binding = 0;
1277 }
1278
1279 if (CBS_len(&cipher_suites) % 2 != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001280 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
Adam Langleyfcf25832014-12-18 17:42:32 -08001281 return NULL;
1282 }
1283
1284 sk = sk_SSL_CIPHER_new_null();
1285 if (sk == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001286 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001287 goto err;
1288 }
1289
Adam Langleyfcf25832014-12-18 17:42:32 -08001290 while (CBS_len(&cipher_suites) > 0) {
1291 uint16_t cipher_suite;
1292
1293 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
David Benjamin3570d732015-06-29 00:28:17 -04001294 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001295 goto err;
1296 }
1297
1298 /* Check for SCSV. */
1299 if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff)) {
1300 /* SCSV is fatal if renegotiating. */
David Benjamin20f6e972015-05-15 21:51:49 -04001301 if (s->s3->initial_handshake_complete) {
David Benjamin3570d732015-06-29 00:28:17 -04001302 OPENSSL_PUT_ERROR(SSL, SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
Adam Langleyfcf25832014-12-18 17:42:32 -08001303 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1304 goto err;
1305 }
1306 s->s3->send_connection_binding = 1;
1307 continue;
1308 }
1309
1310 /* Check for FALLBACK_SCSV. */
1311 if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff)) {
1312 uint16_t max_version = ssl3_get_max_server_version(s);
1313 if (SSL_IS_DTLS(s) ? (uint16_t)s->version > max_version
1314 : (uint16_t)s->version < max_version) {
David Benjamin3570d732015-06-29 00:28:17 -04001315 OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK);
Adam Langleyfcf25832014-12-18 17:42:32 -08001316 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
1317 goto err;
1318 }
1319 continue;
1320 }
1321
David Benjamina1c90a52015-05-30 17:03:14 -04001322 c = SSL_get_cipher_by_value(cipher_suite);
Adam Langleyfcf25832014-12-18 17:42:32 -08001323 if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
David Benjamin3570d732015-06-29 00:28:17 -04001324 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001325 goto err;
1326 }
1327 }
1328
1329 return sk;
David Benjamin9f2c0d72014-10-21 22:00:19 -04001330
Adam Langley95c29f32014-06-20 12:00:00 -07001331err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001332 sk_SSL_CIPHER_free(sk);
Adam Langleyfcf25832014-12-18 17:42:32 -08001333 return NULL;
1334}
Adam Langley95c29f32014-06-20 12:00:00 -07001335
1336
Adam Langleyfcf25832014-12-18 17:42:32 -08001337/* return a servername extension value if provided in Client Hello, or NULL. So
1338 * far, only host_name types are defined (RFC 3546). */
1339const char *SSL_get_servername(const SSL *s, const int type) {
1340 if (type != TLSEXT_NAMETYPE_host_name) {
1341 return NULL;
1342 }
Adam Langley95c29f32014-06-20 12:00:00 -07001343
Adam Langleyfcf25832014-12-18 17:42:32 -08001344 return s->session && !s->tlsext_hostname ? s->session->tlsext_hostname
1345 : s->tlsext_hostname;
1346}
Adam Langley95c29f32014-06-20 12:00:00 -07001347
Adam Langleyfcf25832014-12-18 17:42:32 -08001348int SSL_get_servername_type(const SSL *s) {
1349 if (s->session &&
1350 (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname)) {
1351 return TLSEXT_NAMETYPE_host_name;
1352 }
Adam Langley95c29f32014-06-20 12:00:00 -07001353
Adam Langleyfcf25832014-12-18 17:42:32 -08001354 return -1;
1355}
Adam Langley95c29f32014-06-20 12:00:00 -07001356
Adam Langleyfcf25832014-12-18 17:42:32 -08001357void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
1358 ctx->signed_cert_timestamps_enabled = 1;
1359}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001360
Adam Langleyfcf25832014-12-18 17:42:32 -08001361int SSL_enable_signed_cert_timestamps(SSL *ssl) {
1362 ssl->signed_cert_timestamps_enabled = 1;
1363 return 1;
1364}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001365
Adam Langleyfcf25832014-12-18 17:42:32 -08001366void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
1367 ctx->ocsp_stapling_enabled = 1;
1368}
David Benjamin6c7aed02014-08-27 16:42:38 -04001369
Adam Langleyfcf25832014-12-18 17:42:32 -08001370int SSL_enable_ocsp_stapling(SSL *ssl) {
1371 ssl->ocsp_stapling_enabled = 1;
1372 return 1;
1373}
David Benjamin6c7aed02014-08-27 16:42:38 -04001374
Adam Langleyfcf25832014-12-18 17:42:32 -08001375void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
1376 size_t *out_len) {
1377 SSL_SESSION *session = ssl->session;
Adam Langley3cb50e02014-08-26 14:00:31 -07001378
Adam Langleyfcf25832014-12-18 17:42:32 -08001379 *out_len = 0;
1380 *out = NULL;
1381 if (ssl->server || !session || !session->tlsext_signed_cert_timestamp_list) {
1382 return;
1383 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001384
Adam Langleyfcf25832014-12-18 17:42:32 -08001385 *out = session->tlsext_signed_cert_timestamp_list;
1386 *out_len = session->tlsext_signed_cert_timestamp_list_length;
1387}
David Benjamin6c7aed02014-08-27 16:42:38 -04001388
Adam Langleyfcf25832014-12-18 17:42:32 -08001389void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
1390 size_t *out_len) {
1391 SSL_SESSION *session = ssl->session;
1392
1393 *out_len = 0;
1394 *out = NULL;
1395 if (ssl->server || !session || !session->ocsp_response) {
1396 return;
1397 }
1398 *out = session->ocsp_response;
1399 *out_len = session->ocsp_response_length;
1400}
David Benjamin6c7aed02014-08-27 16:42:38 -04001401
Adam Langley95c29f32014-06-20 12:00:00 -07001402/* SSL_select_next_proto implements the standard protocol selection. It is
1403 * expected that this function is called from the callback set by
1404 * SSL_CTX_set_next_proto_select_cb.
1405 *
1406 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1407 * strings. The length byte itself is not included in the length. A byte
1408 * string of length 0 is invalid. No byte string may be truncated.
1409 *
1410 * The current, but experimental algorithm for selecting the protocol is:
1411 *
1412 * 1) If the server doesn't support NPN then this is indicated to the
1413 * callback. In this case, the client application has to abort the connection
1414 * or have a default application level protocol.
1415 *
1416 * 2) If the server supports NPN, but advertises an empty list then the
1417 * client selects the first protcol in its list, but indicates via the
1418 * API that this fallback case was enacted.
1419 *
1420 * 3) Otherwise, the client finds the first protocol in the server's list
1421 * that it supports and selects this protocol. This is because it's
1422 * assumed that the server has better information about which protocol
1423 * a client should use.
1424 *
1425 * 4) If the client doesn't support any of the server's advertised
1426 * protocols, then this is treated the same as case 2.
1427 *
1428 * It returns either
1429 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1430 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1431 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001432int SSL_select_next_proto(uint8_t **out, uint8_t *outlen, const uint8_t *server,
1433 unsigned int server_len, const uint8_t *client,
1434 unsigned int client_len) {
1435 unsigned int i, j;
1436 const uint8_t *result;
1437 int status = OPENSSL_NPN_UNSUPPORTED;
Adam Langley95c29f32014-06-20 12:00:00 -07001438
Adam Langleyfcf25832014-12-18 17:42:32 -08001439 /* For each protocol in server preference order, see if we support it. */
1440 for (i = 0; i < server_len;) {
1441 for (j = 0; j < client_len;) {
1442 if (server[i] == client[j] &&
1443 memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
1444 /* We found a match */
1445 result = &server[i];
1446 status = OPENSSL_NPN_NEGOTIATED;
1447 goto found;
1448 }
1449 j += client[j];
1450 j++;
1451 }
1452 i += server[i];
1453 i++;
1454 }
Adam Langley95c29f32014-06-20 12:00:00 -07001455
Adam Langleyfcf25832014-12-18 17:42:32 -08001456 /* There's no overlap between our protocols and the server's list. */
1457 result = client;
1458 status = OPENSSL_NPN_NO_OVERLAP;
Adam Langley95c29f32014-06-20 12:00:00 -07001459
Adam Langleyfcf25832014-12-18 17:42:32 -08001460found:
1461 *out = (uint8_t *)result + 1;
1462 *outlen = result[0];
1463 return status;
1464}
Adam Langley95c29f32014-06-20 12:00:00 -07001465
Adam Langley95c29f32014-06-20 12:00:00 -07001466/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
1467 * requested protocol for this connection and returns 0. If the client didn't
1468 * request any protocol, then *data is set to NULL.
1469 *
1470 * Note that the client can request any protocol it chooses. The value returned
1471 * from this function need not be a member of the list of supported protocols
Adam Langleyfcf25832014-12-18 17:42:32 -08001472 * provided by the callback. */
1473void SSL_get0_next_proto_negotiated(const SSL *s, const uint8_t **data,
1474 unsigned *len) {
1475 *data = s->next_proto_negotiated;
1476 if (!*data) {
1477 *len = 0;
1478 } else {
1479 *len = s->next_proto_negotiated_len;
1480 }
Adam Langley95c29f32014-06-20 12:00:00 -07001481}
1482
1483/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
1484 * TLS server needs a list of supported protocols for Next Protocol
1485 * Negotiation. The returned list must be in wire format. The list is returned
1486 * by setting |out| to point to it and |outlen| to its length. This memory will
1487 * not be modified, but one should assume that the SSL* keeps a reference to
1488 * it.
1489 *
Adam Langleyfcf25832014-12-18 17:42:32 -08001490 * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise.
1491 * Otherwise, no such extension will be included in the ServerHello. */
1492void SSL_CTX_set_next_protos_advertised_cb(
1493 SSL_CTX *ctx,
1494 int (*cb)(SSL *ssl, const uint8_t **out, unsigned int *outlen, void *arg),
1495 void *arg) {
1496 ctx->next_protos_advertised_cb = cb;
1497 ctx->next_protos_advertised_cb_arg = arg;
1498}
Adam Langley95c29f32014-06-20 12:00:00 -07001499
1500/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1501 * client needs to select a protocol from the server's provided list. |out|
1502 * must be set to point to the selected protocol (which may be within |in|).
1503 * The length of the protocol name must be written into |outlen|. The server's
1504 * advertised protocols are provided in |in| and |inlen|. The callback can
1505 * assume that |in| is syntactically valid.
1506 *
1507 * The client must select a protocol. It is fatal to the connection if this
1508 * callback returns a value other than SSL_TLSEXT_ERR_OK.
1509 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001510void SSL_CTX_set_next_proto_select_cb(
1511 SSL_CTX *ctx, int (*cb)(SSL *s, uint8_t **out, uint8_t *outlen,
1512 const uint8_t *in, unsigned int inlen, void *arg),
1513 void *arg) {
1514 ctx->next_proto_select_cb = cb;
1515 ctx->next_proto_select_cb_arg = arg;
1516}
Adam Langley95c29f32014-06-20 12:00:00 -07001517
Adam Langleyfcf25832014-12-18 17:42:32 -08001518int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
1519 unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001520 OPENSSL_free(ctx->alpn_client_proto_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001521 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1522 if (!ctx->alpn_client_proto_list) {
1523 return 1;
1524 }
1525 ctx->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001526
Adam Langleyfcf25832014-12-18 17:42:32 -08001527 return 0;
1528}
Adam Langley95c29f32014-06-20 12:00:00 -07001529
Adam Langleyfcf25832014-12-18 17:42:32 -08001530int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001531 OPENSSL_free(ssl->alpn_client_proto_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001532 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1533 if (!ssl->alpn_client_proto_list) {
1534 return 1;
1535 }
1536 ssl->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001537
Adam Langleyfcf25832014-12-18 17:42:32 -08001538 return 0;
1539}
Adam Langley95c29f32014-06-20 12:00:00 -07001540
1541/* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
1542 * during ClientHello processing in order to select an ALPN protocol from the
1543 * client's list of offered protocols. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001544void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
1545 int (*cb)(SSL *ssl, const uint8_t **out,
1546 uint8_t *outlen, const uint8_t *in,
1547 unsigned int inlen, void *arg),
1548 void *arg) {
1549 ctx->alpn_select_cb = cb;
1550 ctx->alpn_select_cb_arg = arg;
1551}
Adam Langley95c29f32014-06-20 12:00:00 -07001552
1553/* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
1554 * On return it sets |*data| to point to |*len| bytes of protocol name (not
1555 * including the leading length-prefix byte). If the server didn't respond with
1556 * a negotiated protocol then |*len| will be zero. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001557void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **data,
1558 unsigned *len) {
1559 *data = NULL;
1560 if (ssl->s3) {
1561 *data = ssl->s3->alpn_selected;
1562 }
1563 if (*data == NULL) {
1564 *len = 0;
1565 } else {
1566 *len = ssl->s3->alpn_selected_len;
1567 }
1568}
Adam Langley95c29f32014-06-20 12:00:00 -07001569
David Benjamincfd248b2015-04-03 11:02:24 -04001570int SSL_export_keying_material(SSL *s, uint8_t *out, size_t out_len,
1571 const char *label, size_t label_len,
1572 const uint8_t *context, size_t context_len,
1573 int use_context) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001574 if (s->version < TLS1_VERSION) {
David Benjaminc565ebb2015-04-03 04:06:36 -04001575 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001576 }
Adam Langley95c29f32014-06-20 12:00:00 -07001577
David Benjamincfd248b2015-04-03 11:02:24 -04001578 return s->enc_method->export_keying_material(
1579 s, out, out_len, label, label_len, context, context_len, use_context);
Adam Langleyfcf25832014-12-18 17:42:32 -08001580}
Adam Langley95c29f32014-06-20 12:00:00 -07001581
Adam Langleyfcf25832014-12-18 17:42:32 -08001582static uint32_t ssl_session_hash(const SSL_SESSION *a) {
1583 uint32_t hash =
1584 ((uint32_t)a->session_id[0]) ||
1585 ((uint32_t)a->session_id[1] << 8) ||
1586 ((uint32_t)a->session_id[2] << 16) ||
1587 ((uint32_t)a->session_id[3] << 24);
Adam Langley95c29f32014-06-20 12:00:00 -07001588
Adam Langleyfcf25832014-12-18 17:42:32 -08001589 return hash;
1590}
Adam Langley95c29f32014-06-20 12:00:00 -07001591
1592/* NB: If this function (or indeed the hash function which uses a sort of
1593 * coarser function than this one) is changed, ensure
1594 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1595 * able to construct an SSL_SESSION that will collide with any existing session
1596 * with a matching session ID. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001597static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
1598 if (a->ssl_version != b->ssl_version) {
1599 return 1;
1600 }
Adam Langley95c29f32014-06-20 12:00:00 -07001601
Adam Langleyfcf25832014-12-18 17:42:32 -08001602 if (a->session_id_length != b->session_id_length) {
1603 return 1;
1604 }
Adam Langley95c29f32014-06-20 12:00:00 -07001605
Adam Langleyfcf25832014-12-18 17:42:32 -08001606 return memcmp(a->session_id, b->session_id, a->session_id_length);
1607}
Adam Langley95c29f32014-06-20 12:00:00 -07001608
David Benjamin4831c332015-05-16 11:43:13 -04001609SSL_CTX *SSL_CTX_new(const SSL_METHOD *method) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001610 SSL_CTX *ret = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001611
David Benjamin4831c332015-05-16 11:43:13 -04001612 if (method == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001613 OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_METHOD_PASSED);
Adam Langleyfcf25832014-12-18 17:42:32 -08001614 return NULL;
1615 }
Adam Langley95c29f32014-06-20 12:00:00 -07001616
Adam Langleyfcf25832014-12-18 17:42:32 -08001617 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001618 OPENSSL_PUT_ERROR(SSL, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
Adam Langleyfcf25832014-12-18 17:42:32 -08001619 goto err;
1620 }
Adam Langley95c29f32014-06-20 12:00:00 -07001621
Adam Langleyfcf25832014-12-18 17:42:32 -08001622 ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1623 if (ret == NULL) {
1624 goto err;
1625 }
Adam Langley95c29f32014-06-20 12:00:00 -07001626
Adam Langleyfcf25832014-12-18 17:42:32 -08001627 memset(ret, 0, sizeof(SSL_CTX));
Adam Langley95c29f32014-06-20 12:00:00 -07001628
David Benjamin4831c332015-05-16 11:43:13 -04001629 ret->method = method->method;
Adam Langley95c29f32014-06-20 12:00:00 -07001630
Adam Langley4bdb6e42015-05-15 15:29:21 -07001631 CRYPTO_MUTEX_init(&ret->lock);
1632
Adam Langleyfcf25832014-12-18 17:42:32 -08001633 ret->cert_store = NULL;
1634 ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
1635 ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1636 ret->session_cache_head = NULL;
1637 ret->session_cache_tail = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001638
Adam Langleyfcf25832014-12-18 17:42:32 -08001639 /* We take the system default */
1640 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001641
Adam Langleyfcf25832014-12-18 17:42:32 -08001642 ret->new_session_cb = 0;
1643 ret->remove_session_cb = 0;
1644 ret->get_session_cb = 0;
1645 ret->generate_session_id = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001646
Adam Langleyfcf25832014-12-18 17:42:32 -08001647 ret->references = 1;
1648 ret->quiet_shutdown = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001649
Adam Langleyfcf25832014-12-18 17:42:32 -08001650 ret->info_callback = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001651
Adam Langleyfcf25832014-12-18 17:42:32 -08001652 ret->app_verify_callback = 0;
1653 ret->app_verify_arg = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001654
Adam Langleyfcf25832014-12-18 17:42:32 -08001655 ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
Adam Langleyfcf25832014-12-18 17:42:32 -08001656 ret->msg_callback = 0;
1657 ret->msg_callback_arg = NULL;
1658 ret->verify_mode = SSL_VERIFY_NONE;
1659 ret->sid_ctx_length = 0;
1660 ret->default_verify_callback = NULL;
1661 ret->cert = ssl_cert_new();
1662 if (ret->cert == NULL) {
1663 goto err;
1664 }
Adam Langley95c29f32014-06-20 12:00:00 -07001665
Adam Langleyfcf25832014-12-18 17:42:32 -08001666 ret->default_passwd_callback = 0;
1667 ret->default_passwd_callback_userdata = NULL;
1668 ret->client_cert_cb = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001669
Adam Langleyfcf25832014-12-18 17:42:32 -08001670 ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
1671 if (ret->sessions == NULL) {
1672 goto err;
1673 }
1674 ret->cert_store = X509_STORE_new();
1675 if (ret->cert_store == NULL) {
1676 goto err;
1677 }
Adam Langley95c29f32014-06-20 12:00:00 -07001678
Adam Langleyfcf25832014-12-18 17:42:32 -08001679 ssl_create_cipher_list(ret->method, &ret->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001680 &ret->cipher_list_by_id, SSL_DEFAULT_CIPHER_LIST);
Adam Langleyfcf25832014-12-18 17:42:32 -08001681 if (ret->cipher_list == NULL ||
1682 sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001683 OPENSSL_PUT_ERROR(SSL, SSL_R_LIBRARY_HAS_NO_CIPHERS);
Adam Langleyfcf25832014-12-18 17:42:32 -08001684 goto err2;
1685 }
Adam Langley95c29f32014-06-20 12:00:00 -07001686
Adam Langleyfcf25832014-12-18 17:42:32 -08001687 ret->param = X509_VERIFY_PARAM_new();
1688 if (!ret->param) {
1689 goto err;
1690 }
Adam Langley95c29f32014-06-20 12:00:00 -07001691
Adam Langleyfcf25832014-12-18 17:42:32 -08001692 ret->client_CA = sk_X509_NAME_new_null();
1693 if (ret->client_CA == NULL) {
1694 goto err;
1695 }
Adam Langley95c29f32014-06-20 12:00:00 -07001696
David Benjamin9f33fc62015-04-15 17:29:53 -04001697 CRYPTO_new_ex_data(&g_ex_data_class_ssl_ctx, ret, &ret->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001698
Adam Langleyfcf25832014-12-18 17:42:32 -08001699 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
David Benjamin422d3a42014-08-20 11:09:03 -04001700
Adam Langleyfcf25832014-12-18 17:42:32 -08001701 ret->tlsext_servername_callback = 0;
1702 ret->tlsext_servername_arg = NULL;
1703 /* Setup RFC4507 ticket keys */
1704 if (!RAND_bytes(ret->tlsext_tick_key_name, 16) ||
1705 !RAND_bytes(ret->tlsext_tick_hmac_key, 16) ||
1706 !RAND_bytes(ret->tlsext_tick_aes_key, 16)) {
1707 ret->options |= SSL_OP_NO_TICKET;
1708 }
Adam Langley95c29f32014-06-20 12:00:00 -07001709
Adam Langleyfcf25832014-12-18 17:42:32 -08001710 ret->next_protos_advertised_cb = 0;
1711 ret->next_proto_select_cb = 0;
1712 ret->psk_identity_hint = NULL;
1713 ret->psk_client_callback = NULL;
1714 ret->psk_server_callback = NULL;
1715
1716 /* Default is to connect to non-RI servers. When RI is more widely deployed
1717 * might change this. */
1718 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
1719
1720 /* Lock the SSL_CTX to the specified version, for compatibility with legacy
1721 * uses of SSL_METHOD. */
David Benjamin4831c332015-05-16 11:43:13 -04001722 if (method->version != 0) {
1723 SSL_CTX_set_max_version(ret, method->version);
1724 SSL_CTX_set_min_version(ret, method->version);
Adam Langleyfcf25832014-12-18 17:42:32 -08001725 }
1726
1727 return ret;
1728
Adam Langley95c29f32014-06-20 12:00:00 -07001729err:
David Benjamin3570d732015-06-29 00:28:17 -04001730 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -07001731err2:
David Benjamin2755a3e2015-04-22 16:17:58 -04001732 SSL_CTX_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -08001733 return NULL;
1734}
Adam Langley95c29f32014-06-20 12:00:00 -07001735
David Benjamin4fcc2e22015-04-22 12:58:16 -04001736void SSL_CTX_free(SSL_CTX *ctx) {
1737 if (ctx == NULL ||
Adam Langley0b5e3902015-05-15 13:08:38 -07001738 !CRYPTO_refcount_dec_and_test_zero(&ctx->references)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001739 return;
1740 }
Adam Langley95c29f32014-06-20 12:00:00 -07001741
David Benjamin2755a3e2015-04-22 16:17:58 -04001742 X509_VERIFY_PARAM_free(ctx->param);
Adam Langley95c29f32014-06-20 12:00:00 -07001743
Adam Langleyfcf25832014-12-18 17:42:32 -08001744 /* Free internal session cache. However: the remove_cb() may reference the
1745 * ex_data of SSL_CTX, thus the ex_data store can only be removed after the
1746 * sessions were flushed. As the ex_data handling routines might also touch
1747 * the session cache, the most secure solution seems to be: empty (flush) the
1748 * cache, then free ex_data, then finally free the cache. (See ticket
1749 * [openssl.org #212].) */
David Benjamin2755a3e2015-04-22 16:17:58 -04001750 SSL_CTX_flush_sessions(ctx, 0);
Adam Langley95c29f32014-06-20 12:00:00 -07001751
David Benjamin4fcc2e22015-04-22 12:58:16 -04001752 CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, ctx, &ctx->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001753
Adam Langley4bdb6e42015-05-15 15:29:21 -07001754 CRYPTO_MUTEX_cleanup(&ctx->lock);
David Benjamin2755a3e2015-04-22 16:17:58 -04001755 lh_SSL_SESSION_free(ctx->sessions);
1756 X509_STORE_free(ctx->cert_store);
1757 ssl_cipher_preference_list_free(ctx->cipher_list);
1758 sk_SSL_CIPHER_free(ctx->cipher_list_by_id);
1759 ssl_cipher_preference_list_free(ctx->cipher_list_tls11);
1760 ssl_cert_free(ctx->cert);
Adam Langley09505632015-07-30 18:10:13 -07001761 sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->client_custom_extensions,
1762 SSL_CUSTOM_EXTENSION_free);
1763 sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->server_custom_extensions,
1764 SSL_CUSTOM_EXTENSION_free);
David Benjamin2755a3e2015-04-22 16:17:58 -04001765 sk_X509_NAME_pop_free(ctx->client_CA, X509_NAME_free);
David Benjamin2755a3e2015-04-22 16:17:58 -04001766 sk_SRTP_PROTECTION_PROFILE_free(ctx->srtp_profiles);
1767 OPENSSL_free(ctx->psk_identity_hint);
David Benjamin2755a3e2015-04-22 16:17:58 -04001768 OPENSSL_free(ctx->tlsext_ellipticcurvelist);
1769 OPENSSL_free(ctx->alpn_client_proto_list);
1770 EVP_PKEY_free(ctx->tlsext_channel_id_private);
1771 BIO_free(ctx->keylog_bio);
Adam Langley95c29f32014-06-20 12:00:00 -07001772
David Benjamin4fcc2e22015-04-22 12:58:16 -04001773 OPENSSL_free(ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08001774}
Adam Langley95c29f32014-06-20 12:00:00 -07001775
Adam Langleyfcf25832014-12-18 17:42:32 -08001776void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) {
1777 ctx->default_passwd_callback = cb;
1778}
Adam Langley95c29f32014-06-20 12:00:00 -07001779
Adam Langleyfcf25832014-12-18 17:42:32 -08001780void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u) {
1781 ctx->default_passwd_callback_userdata = u;
1782}
Adam Langley95c29f32014-06-20 12:00:00 -07001783
Adam Langleyfcf25832014-12-18 17:42:32 -08001784void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
1785 int (*cb)(X509_STORE_CTX *, void *),
1786 void *arg) {
1787 ctx->app_verify_callback = cb;
1788 ctx->app_verify_arg = arg;
1789}
Adam Langley95c29f32014-06-20 12:00:00 -07001790
Adam Langleyfcf25832014-12-18 17:42:32 -08001791void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
1792 int (*cb)(int, X509_STORE_CTX *)) {
1793 ctx->verify_mode = mode;
1794 ctx->default_verify_callback = cb;
1795}
Adam Langley95c29f32014-06-20 12:00:00 -07001796
Adam Langleyfcf25832014-12-18 17:42:32 -08001797void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
1798 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
1799}
Adam Langley1258b6a2014-06-20 12:00:00 -07001800
David Benjamin7481d392015-07-05 19:38:46 -04001801void SSL_CTX_set_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl, void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08001802 void *arg) {
David Benjamin7481d392015-07-05 19:38:46 -04001803 ssl_cert_set_cert_cb(ctx->cert, cb, arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08001804}
David Benjamin859ec3c2014-09-02 16:29:36 -04001805
David Benjamin7481d392015-07-05 19:38:46 -04001806void SSL_set_cert_cb(SSL *ssl, int (*cb)(SSL *ssl, void *arg), void *arg) {
1807 ssl_cert_set_cert_cb(ssl->cert, cb, arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08001808}
Adam Langley95c29f32014-06-20 12:00:00 -07001809
David Benjamin107db582015-04-08 00:41:59 -04001810void ssl_get_compatible_server_ciphers(SSL *s, uint32_t *out_mask_k,
1811 uint32_t *out_mask_a) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001812 CERT *c = s->cert;
David Benjamind1d80782015-07-05 11:54:09 -04001813 int have_rsa_cert = 0, dh_tmp;
David Benjamin107db582015-04-08 00:41:59 -04001814 uint32_t mask_k, mask_a;
David Benjamind1d80782015-07-05 11:54:09 -04001815 int have_ecc_cert = 0, ecdsa_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001816 X509 *x;
Adam Langley95c29f32014-06-20 12:00:00 -07001817
Adam Langleyfcf25832014-12-18 17:42:32 -08001818 if (c == NULL) {
1819 /* TODO(davidben): Is this codepath possible? */
1820 *out_mask_k = 0;
1821 *out_mask_a = 0;
1822 return;
1823 }
Adam Langley95c29f32014-06-20 12:00:00 -07001824
Adam Langleyfcf25832014-12-18 17:42:32 -08001825 dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001826
nagendra modadugu601448a2015-07-24 09:31:31 -07001827 if (s->cert->x509 != NULL && ssl_has_private_key(s)) {
1828 if (ssl_private_key_type(s) == EVP_PKEY_RSA) {
David Benjamind1d80782015-07-05 11:54:09 -04001829 have_rsa_cert = 1;
nagendra modadugu601448a2015-07-24 09:31:31 -07001830 } else if (ssl_private_key_type(s) == EVP_PKEY_EC) {
David Benjamind1d80782015-07-05 11:54:09 -04001831 have_ecc_cert = 1;
1832 }
1833 }
nagendra modadugu601448a2015-07-24 09:31:31 -07001834
Adam Langleyfcf25832014-12-18 17:42:32 -08001835 mask_k = 0;
1836 mask_a = 0;
David Benjaminf31e6812014-11-13 18:05:55 -05001837
Adam Langleyfcf25832014-12-18 17:42:32 -08001838 if (dh_tmp) {
David Benjamin7061e282015-03-19 11:10:48 -04001839 mask_k |= SSL_kDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001840 }
David Benjaminbb20f522015-07-04 17:18:14 -04001841 if (have_rsa_cert) {
1842 mask_k |= SSL_kRSA;
Adam Langleyfcf25832014-12-18 17:42:32 -08001843 mask_a |= SSL_aRSA;
1844 }
Adam Langley95c29f32014-06-20 12:00:00 -07001845
Adam Langleyfcf25832014-12-18 17:42:32 -08001846 /* An ECC certificate may be usable for ECDSA cipher suites depending on the
1847 * key usage extension and on the client's curve preferences. */
1848 if (have_ecc_cert) {
David Benjamind1d80782015-07-05 11:54:09 -04001849 x = c->x509;
Adam Langleyfcf25832014-12-18 17:42:32 -08001850 /* This call populates extension flags (ex_flags). */
1851 X509_check_purpose(x, -1, 0);
1852 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE)
1853 ? (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)
1854 : 1;
1855 if (!tls1_check_ec_cert(s, x)) {
1856 ecdsa_ok = 0;
1857 }
1858 if (ecdsa_ok) {
1859 mask_a |= SSL_aECDSA;
1860 }
1861 }
Adam Langley95c29f32014-06-20 12:00:00 -07001862
Adam Langleyfcf25832014-12-18 17:42:32 -08001863 /* If we are considering an ECC cipher suite that uses an ephemeral EC
1864 * key, check it. */
David Benjamindd978782015-04-24 15:20:13 -04001865 if (tls1_check_ec_tmp_key(s)) {
David Benjamin7061e282015-03-19 11:10:48 -04001866 mask_k |= SSL_kECDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001867 }
Adam Langley95c29f32014-06-20 12:00:00 -07001868
Adam Langleyfcf25832014-12-18 17:42:32 -08001869 /* PSK requires a server callback. */
1870 if (s->psk_server_callback != NULL) {
1871 mask_k |= SSL_kPSK;
1872 mask_a |= SSL_aPSK;
1873 }
Adam Langley95c29f32014-06-20 12:00:00 -07001874
Adam Langleyfcf25832014-12-18 17:42:32 -08001875 *out_mask_k = mask_k;
1876 *out_mask_a = mask_a;
1877}
Adam Langley95c29f32014-06-20 12:00:00 -07001878
Adam Langleyfcf25832014-12-18 17:42:32 -08001879void ssl_update_cache(SSL *s, int mode) {
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001880 /* Never cache sessions with empty session IDs. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001881 if (s->session->session_id_length == 0) {
1882 return;
1883 }
Adam Langley95c29f32014-06-20 12:00:00 -07001884
David Benjamin95d31822015-06-15 19:53:32 -04001885 int has_new_session = !s->hit;
1886 if (!s->server && s->tlsext_ticket_expected) {
1887 /* A client may see new sessions on abbreviated handshakes if the server
1888 * decides to renew the ticket. Once the handshake is completed, it should
1889 * be inserted into the cache. */
1890 has_new_session = 1;
1891 }
1892
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001893 SSL_CTX *ctx = s->initial_ctx;
David Benjamin95d31822015-06-15 19:53:32 -04001894 if ((ctx->session_cache_mode & mode) == mode && has_new_session &&
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001895 ((ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) ||
1896 SSL_CTX_add_session(ctx, s->session)) &&
1897 ctx->new_session_cb != NULL) {
1898 /* Note: |new_session_cb| is called whether the internal session cache is
1899 * used or not. */
1900 if (!ctx->new_session_cb(s, SSL_SESSION_up_ref(s->session))) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001901 SSL_SESSION_free(s->session);
1902 }
1903 }
Adam Langley95c29f32014-06-20 12:00:00 -07001904
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001905 if (!(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR) &&
1906 !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) &&
1907 (ctx->session_cache_mode & mode) == mode) {
1908 /* Automatically flush the internal session cache every 255 connections. */
1909 int flush_cache = 0;
Adam Langley4bdb6e42015-05-15 15:29:21 -07001910 CRYPTO_MUTEX_lock_write(&ctx->lock);
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001911 ctx->handshakes_since_cache_flush++;
1912 if (ctx->handshakes_since_cache_flush >= 255) {
1913 flush_cache = 1;
1914 ctx->handshakes_since_cache_flush = 0;
1915 }
Adam Langley4bdb6e42015-05-15 15:29:21 -07001916 CRYPTO_MUTEX_unlock(&ctx->lock);
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001917
1918 if (flush_cache) {
1919 SSL_CTX_flush_sessions(ctx, (unsigned long)time(NULL));
Adam Langleyfcf25832014-12-18 17:42:32 -08001920 }
1921 }
1922}
Adam Langley95c29f32014-06-20 12:00:00 -07001923
David Benjamin1a5c50f2015-03-11 16:22:37 -04001924int SSL_get_error(const SSL *s, int ret_code) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001925 int reason;
David Benjamin1a5c50f2015-03-11 16:22:37 -04001926 uint32_t err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001927 BIO *bio;
Adam Langley95c29f32014-06-20 12:00:00 -07001928
David Benjamin1a5c50f2015-03-11 16:22:37 -04001929 if (ret_code > 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001930 return SSL_ERROR_NONE;
1931 }
Adam Langley95c29f32014-06-20 12:00:00 -07001932
Adam Langleyfcf25832014-12-18 17:42:32 -08001933 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
1934 * where we do encode the error */
David Benjamin1a5c50f2015-03-11 16:22:37 -04001935 err = ERR_peek_error();
1936 if (err != 0) {
1937 if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001938 return SSL_ERROR_SYSCALL;
1939 }
1940 return SSL_ERROR_SSL;
1941 }
Adam Langley95c29f32014-06-20 12:00:00 -07001942
David Benjamin1a5c50f2015-03-11 16:22:37 -04001943 if (ret_code == 0) {
David Benjamin9a38e922015-01-22 16:06:11 -05001944 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
1945 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) {
1946 /* The socket was cleanly shut down with a close_notify. */
1947 return SSL_ERROR_ZERO_RETURN;
1948 }
1949 /* An EOF was observed which violates the protocol, and the underlying
1950 * transport does not participate in the error queue. Bubble up to the
1951 * caller. */
1952 return SSL_ERROR_SYSCALL;
Adam Langleyfcf25832014-12-18 17:42:32 -08001953 }
Adam Langleyb2ce0582014-06-20 12:00:00 -07001954
Adam Langleyfcf25832014-12-18 17:42:32 -08001955 if (SSL_want_session(s)) {
1956 return SSL_ERROR_PENDING_SESSION;
1957 }
Adam Langley95c29f32014-06-20 12:00:00 -07001958
Adam Langleyfcf25832014-12-18 17:42:32 -08001959 if (SSL_want_certificate(s)) {
1960 return SSL_ERROR_PENDING_CERTIFICATE;
1961 }
Adam Langley95c29f32014-06-20 12:00:00 -07001962
Adam Langleyfcf25832014-12-18 17:42:32 -08001963 if (SSL_want_read(s)) {
1964 bio = SSL_get_rbio(s);
1965 if (BIO_should_read(bio)) {
1966 return SSL_ERROR_WANT_READ;
1967 }
Adam Langley95c29f32014-06-20 12:00:00 -07001968
Adam Langleyfcf25832014-12-18 17:42:32 -08001969 if (BIO_should_write(bio)) {
1970 /* This one doesn't make too much sense ... We never try to write to the
1971 * rbio, and an application program where rbio and wbio are separate
1972 * couldn't even know what it should wait for. However if we ever set
1973 * s->rwstate incorrectly (so that we have SSL_want_read(s) instead of
1974 * SSL_want_write(s)) and rbio and wbio *are* the same, this test works
1975 * around that bug; so it might be safer to keep it. */
1976 return SSL_ERROR_WANT_WRITE;
1977 }
Adam Langley95c29f32014-06-20 12:00:00 -07001978
Adam Langleyfcf25832014-12-18 17:42:32 -08001979 if (BIO_should_io_special(bio)) {
1980 reason = BIO_get_retry_reason(bio);
1981 if (reason == BIO_RR_CONNECT) {
1982 return SSL_ERROR_WANT_CONNECT;
1983 }
Adam Langley95c29f32014-06-20 12:00:00 -07001984
Adam Langleyfcf25832014-12-18 17:42:32 -08001985 if (reason == BIO_RR_ACCEPT) {
1986 return SSL_ERROR_WANT_ACCEPT;
1987 }
Adam Langley95c29f32014-06-20 12:00:00 -07001988
Adam Langleyfcf25832014-12-18 17:42:32 -08001989 return SSL_ERROR_SYSCALL; /* unknown */
1990 }
1991 }
Adam Langley95c29f32014-06-20 12:00:00 -07001992
Adam Langleyfcf25832014-12-18 17:42:32 -08001993 if (SSL_want_write(s)) {
1994 bio = SSL_get_wbio(s);
1995 if (BIO_should_write(bio)) {
1996 return SSL_ERROR_WANT_WRITE;
1997 }
Adam Langley95c29f32014-06-20 12:00:00 -07001998
Adam Langleyfcf25832014-12-18 17:42:32 -08001999 if (BIO_should_read(bio)) {
2000 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2001 return SSL_ERROR_WANT_READ;
2002 }
Adam Langley95c29f32014-06-20 12:00:00 -07002003
Adam Langleyfcf25832014-12-18 17:42:32 -08002004 if (BIO_should_io_special(bio)) {
2005 reason = BIO_get_retry_reason(bio);
2006 if (reason == BIO_RR_CONNECT) {
2007 return SSL_ERROR_WANT_CONNECT;
2008 }
Adam Langley95c29f32014-06-20 12:00:00 -07002009
Adam Langleyfcf25832014-12-18 17:42:32 -08002010 if (reason == BIO_RR_ACCEPT) {
2011 return SSL_ERROR_WANT_ACCEPT;
2012 }
Adam Langley95c29f32014-06-20 12:00:00 -07002013
Adam Langleyfcf25832014-12-18 17:42:32 -08002014 return SSL_ERROR_SYSCALL;
2015 }
2016 }
Adam Langley95c29f32014-06-20 12:00:00 -07002017
Adam Langleyfcf25832014-12-18 17:42:32 -08002018 if (SSL_want_x509_lookup(s)) {
2019 return SSL_ERROR_WANT_X509_LOOKUP;
2020 }
Adam Langley95c29f32014-06-20 12:00:00 -07002021
Adam Langleyfcf25832014-12-18 17:42:32 -08002022 if (SSL_want_channel_id_lookup(s)) {
2023 return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
2024 }
Adam Langley0f4746e2014-08-13 12:26:32 -07002025
David Benjaminb4d65fd2015-05-29 17:11:21 -04002026 if (SSL_want_private_key_operation(s)) {
2027 return SSL_ERROR_WANT_PRIVATE_KEY_OPERATION;
2028 }
2029
Adam Langleyfcf25832014-12-18 17:42:32 -08002030 return SSL_ERROR_SYSCALL;
2031}
Adam Langley0f4746e2014-08-13 12:26:32 -07002032
Adam Langleyfcf25832014-12-18 17:42:32 -08002033int SSL_do_handshake(SSL *s) {
2034 int ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002035
Adam Langleyfcf25832014-12-18 17:42:32 -08002036 if (s->handshake_func == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04002037 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_TYPE_NOT_SET);
Adam Langleyfcf25832014-12-18 17:42:32 -08002038 return -1;
2039 }
Adam Langley95c29f32014-06-20 12:00:00 -07002040
Adam Langleyfcf25832014-12-18 17:42:32 -08002041 if (SSL_in_init(s)) {
2042 ret = s->handshake_func(s);
2043 }
2044 return ret;
2045}
Adam Langley95c29f32014-06-20 12:00:00 -07002046
David Benjamin4831c332015-05-16 11:43:13 -04002047void SSL_set_accept_state(SSL *ssl) {
2048 ssl->server = 1;
2049 ssl->shutdown = 0;
David Benjamin8ec88102015-05-15 23:40:13 -04002050 ssl->state = SSL_ST_ACCEPT;
David Benjamin4831c332015-05-16 11:43:13 -04002051 ssl->handshake_func = ssl->method->ssl_accept;
Adam Langleyfcf25832014-12-18 17:42:32 -08002052 /* clear the current cipher */
David Benjamin4831c332015-05-16 11:43:13 -04002053 ssl_clear_cipher_ctx(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08002054}
Adam Langley95c29f32014-06-20 12:00:00 -07002055
David Benjamin4831c332015-05-16 11:43:13 -04002056void SSL_set_connect_state(SSL *ssl) {
2057 ssl->server = 0;
2058 ssl->shutdown = 0;
David Benjamin8ec88102015-05-15 23:40:13 -04002059 ssl->state = SSL_ST_CONNECT;
David Benjamin4831c332015-05-16 11:43:13 -04002060 ssl->handshake_func = ssl->method->ssl_connect;
Adam Langleyfcf25832014-12-18 17:42:32 -08002061 /* clear the current cipher */
David Benjamin4831c332015-05-16 11:43:13 -04002062 ssl_clear_cipher_ctx(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08002063}
Adam Langley95c29f32014-06-20 12:00:00 -07002064
Adam Langleyfcf25832014-12-18 17:42:32 -08002065static const char *ssl_get_version(int version) {
2066 switch (version) {
2067 case TLS1_2_VERSION:
2068 return "TLSv1.2";
Adam Langley95c29f32014-06-20 12:00:00 -07002069
Adam Langleyfcf25832014-12-18 17:42:32 -08002070 case TLS1_1_VERSION:
2071 return "TLSv1.1";
Adam Langley95c29f32014-06-20 12:00:00 -07002072
Adam Langleyfcf25832014-12-18 17:42:32 -08002073 case TLS1_VERSION:
2074 return "TLSv1";
Adam Langley95c29f32014-06-20 12:00:00 -07002075
Adam Langleyfcf25832014-12-18 17:42:32 -08002076 case SSL3_VERSION:
2077 return "SSLv3";
Adam Langley95c29f32014-06-20 12:00:00 -07002078
David Benjamin1c722b72015-04-20 13:53:10 -04002079 case DTLS1_VERSION:
2080 return "DTLSv1";
2081
2082 case DTLS1_2_VERSION:
2083 return "DTLSv1.2";
2084
Adam Langleyfcf25832014-12-18 17:42:32 -08002085 default:
2086 return "unknown";
2087 }
2088}
Adam Langley95c29f32014-06-20 12:00:00 -07002089
Adam Langleyfcf25832014-12-18 17:42:32 -08002090const char *SSL_get_version(const SSL *s) {
2091 return ssl_get_version(s->version);
2092}
Adam Langley95c29f32014-06-20 12:00:00 -07002093
Adam Langleyfcf25832014-12-18 17:42:32 -08002094const char *SSL_SESSION_get_version(const SSL_SESSION *sess) {
2095 return ssl_get_version(sess->ssl_version);
2096}
Adam Langley95c29f32014-06-20 12:00:00 -07002097
Adam Langleyfcf25832014-12-18 17:42:32 -08002098void ssl_clear_cipher_ctx(SSL *s) {
David Benjamin31a07792015-03-03 14:20:26 -05002099 SSL_AEAD_CTX_free(s->aead_read_ctx);
2100 s->aead_read_ctx = NULL;
2101 SSL_AEAD_CTX_free(s->aead_write_ctx);
2102 s->aead_write_ctx = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002103}
Adam Langley95c29f32014-06-20 12:00:00 -07002104
Adam Langleyfcf25832014-12-18 17:42:32 -08002105X509 *SSL_get_certificate(const SSL *s) {
2106 if (s->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04002107 return s->cert->x509;
Adam Langleyfcf25832014-12-18 17:42:32 -08002108 }
2109
2110 return NULL;
2111}
2112
2113EVP_PKEY *SSL_get_privatekey(const SSL *s) {
2114 if (s->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04002115 return s->cert->privatekey;
Adam Langleyfcf25832014-12-18 17:42:32 -08002116 }
2117
2118 return NULL;
2119}
2120
2121X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
2122 if (ctx->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04002123 return ctx->cert->x509;
Adam Langleyfcf25832014-12-18 17:42:32 -08002124 }
2125
2126 return NULL;
2127}
2128
2129EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
2130 if (ctx->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04002131 return ctx->cert->privatekey;
Adam Langleyfcf25832014-12-18 17:42:32 -08002132 }
2133
2134 return NULL;
2135}
2136
2137const SSL_CIPHER *SSL_get_current_cipher(const SSL *s) {
David Benjamina07c0fc2015-05-13 13:19:42 -04002138 if (s->aead_write_ctx == NULL) {
2139 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002140 }
David Benjamina07c0fc2015-05-13 13:19:42 -04002141 return s->aead_write_ctx->cipher;
Adam Langleyfcf25832014-12-18 17:42:32 -08002142}
2143
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07002144const COMP_METHOD *SSL_get_current_compression(SSL *s) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002145
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07002146const COMP_METHOD *SSL_get_current_expansion(SSL *s) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002147
2148int ssl_init_wbio_buffer(SSL *s, int push) {
2149 BIO *bbio;
2150
2151 if (s->bbio == NULL) {
2152 bbio = BIO_new(BIO_f_buffer());
2153 if (bbio == NULL) {
2154 return 0;
2155 }
2156 s->bbio = bbio;
2157 } else {
2158 bbio = s->bbio;
2159 if (s->bbio == s->wbio) {
2160 s->wbio = BIO_pop(s->wbio);
2161 }
2162 }
2163
2164 BIO_reset(bbio);
2165 if (!BIO_set_read_buffer_size(bbio, 1)) {
David Benjamin3570d732015-06-29 00:28:17 -04002166 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -08002167 return 0;
2168 }
2169
2170 if (push) {
2171 if (s->wbio != bbio) {
2172 s->wbio = BIO_push(bbio, s->wbio);
2173 }
2174 } else {
2175 if (s->wbio == bbio) {
2176 s->wbio = BIO_pop(bbio);
2177 }
2178 }
2179
2180 return 1;
2181}
2182
2183void ssl_free_wbio_buffer(SSL *s) {
2184 if (s->bbio == NULL) {
2185 return;
2186 }
2187
2188 if (s->bbio == s->wbio) {
2189 /* remove buffering */
2190 s->wbio = BIO_pop(s->wbio);
2191 }
2192
2193 BIO_free(s->bbio);
2194 s->bbio = NULL;
2195}
2196
2197void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
2198 ctx->quiet_shutdown = mode;
2199}
2200
2201int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
2202 return ctx->quiet_shutdown;
2203}
2204
2205void SSL_set_quiet_shutdown(SSL *s, int mode) { s->quiet_shutdown = mode; }
2206
2207int SSL_get_quiet_shutdown(const SSL *s) { return s->quiet_shutdown; }
2208
2209void SSL_set_shutdown(SSL *s, int mode) { s->shutdown = mode; }
2210
2211int SSL_get_shutdown(const SSL *s) { return s->shutdown; }
2212
2213int SSL_version(const SSL *s) { return s->version; }
2214
2215SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
2216
2217SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
2218 if (ssl->ctx == ctx) {
2219 return ssl->ctx;
2220 }
2221
2222 if (ctx == NULL) {
2223 ctx = ssl->initial_ctx;
2224 }
2225
David Benjamin2755a3e2015-04-22 16:17:58 -04002226 ssl_cert_free(ssl->cert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002227 ssl->cert = ssl_cert_dup(ctx->cert);
David Benjamin2755a3e2015-04-22 16:17:58 -04002228
Adam Langley0b5e3902015-05-15 13:08:38 -07002229 CRYPTO_refcount_inc(&ctx->references);
David Benjamin2755a3e2015-04-22 16:17:58 -04002230 SSL_CTX_free(ssl->ctx); /* decrement reference count */
Adam Langleyfcf25832014-12-18 17:42:32 -08002231 ssl->ctx = ctx;
2232
2233 ssl->sid_ctx_length = ctx->sid_ctx_length;
2234 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2235 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2236
2237 return ssl->ctx;
2238}
2239
2240int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
2241 return X509_STORE_set_default_paths(ctx->cert_store);
2242}
Adam Langley95c29f32014-06-20 12:00:00 -07002243
2244int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
Adam Langleyfcf25832014-12-18 17:42:32 -08002245 const char *CApath) {
2246 return X509_STORE_load_locations(ctx->cert_store, CAfile, CApath);
2247}
Adam Langley95c29f32014-06-20 12:00:00 -07002248
2249void SSL_set_info_callback(SSL *ssl,
Adam Langleyfcf25832014-12-18 17:42:32 -08002250 void (*cb)(const SSL *ssl, int type, int val)) {
2251 ssl->info_callback = cb;
2252}
Adam Langley95c29f32014-06-20 12:00:00 -07002253
Adam Langleyfcf25832014-12-18 17:42:32 -08002254void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/, int /*type*/,
2255 int /*val*/) {
2256 return ssl->info_callback;
2257}
Adam Langley95c29f32014-06-20 12:00:00 -07002258
Adam Langleyfcf25832014-12-18 17:42:32 -08002259int SSL_state(const SSL *ssl) { return ssl->state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002260
David Benjaminece089c2015-05-15 23:52:42 -04002261void SSL_set_state(SSL *ssl, int state) { }
Adam Langley95c29f32014-06-20 12:00:00 -07002262
Adam Langleyfcf25832014-12-18 17:42:32 -08002263void SSL_set_verify_result(SSL *ssl, long arg) { ssl->verify_result = arg; }
Adam Langley95c29f32014-06-20 12:00:00 -07002264
Adam Langleyfcf25832014-12-18 17:42:32 -08002265long SSL_get_verify_result(const SSL *ssl) { return ssl->verify_result; }
Adam Langley95c29f32014-06-20 12:00:00 -07002266
Adam Langleyfcf25832014-12-18 17:42:32 -08002267int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2268 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002269 int index;
2270 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp,
2271 new_func, dup_func, free_func)) {
2272 return -1;
2273 }
2274 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002275}
Adam Langley95c29f32014-06-20 12:00:00 -07002276
Adam Langleyfcf25832014-12-18 17:42:32 -08002277int SSL_set_ex_data(SSL *s, int idx, void *arg) {
2278 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2279}
Adam Langley95c29f32014-06-20 12:00:00 -07002280
Adam Langleyfcf25832014-12-18 17:42:32 -08002281void *SSL_get_ex_data(const SSL *s, int idx) {
2282 return CRYPTO_get_ex_data(&s->ex_data, idx);
2283}
Adam Langley95c29f32014-06-20 12:00:00 -07002284
Adam Langleyfcf25832014-12-18 17:42:32 -08002285int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2286 CRYPTO_EX_dup *dup_func,
2287 CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002288 int index;
2289 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp,
2290 new_func, dup_func, free_func)) {
2291 return -1;
2292 }
2293 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002294}
Adam Langley95c29f32014-06-20 12:00:00 -07002295
Adam Langleyfcf25832014-12-18 17:42:32 -08002296int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg) {
2297 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2298}
Adam Langley95c29f32014-06-20 12:00:00 -07002299
Adam Langleyfcf25832014-12-18 17:42:32 -08002300void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx) {
2301 return CRYPTO_get_ex_data(&s->ex_data, idx);
2302}
Adam Langley95c29f32014-06-20 12:00:00 -07002303
Adam Langleyfcf25832014-12-18 17:42:32 -08002304X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
2305 return ctx->cert_store;
2306}
Adam Langley95c29f32014-06-20 12:00:00 -07002307
Adam Langleyfcf25832014-12-18 17:42:32 -08002308void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
David Benjamin2755a3e2015-04-22 16:17:58 -04002309 X509_STORE_free(ctx->cert_store);
Adam Langleyfcf25832014-12-18 17:42:32 -08002310 ctx->cert_store = store;
2311}
Adam Langley95c29f32014-06-20 12:00:00 -07002312
Adam Langleyfcf25832014-12-18 17:42:32 -08002313int SSL_want(const SSL *s) { return s->rwstate; }
Adam Langley95c29f32014-06-20 12:00:00 -07002314
Adam Langleyfcf25832014-12-18 17:42:32 -08002315void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
2316 RSA *(*cb)(SSL *ssl, int is_export,
2317 int keylength)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002318}
Adam Langley95c29f32014-06-20 12:00:00 -07002319
Adam Langleyfcf25832014-12-18 17:42:32 -08002320void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
2321 int keylength)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002322}
2323
2324void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
David Benjamin59015c32015-04-26 13:13:55 -04002325 DH *(*callback)(SSL *ssl, int is_export,
2326 int keylength)) {
2327 ctx->cert->dh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002328}
2329
David Benjamin59015c32015-04-26 13:13:55 -04002330void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*callback)(SSL *ssl, int is_export,
2331 int keylength)) {
2332 ssl->cert->dh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002333}
2334
2335void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,
David Benjamindd978782015-04-24 15:20:13 -04002336 EC_KEY *(*callback)(SSL *ssl, int is_export,
2337 int keylength)) {
David Benjamin59015c32015-04-26 13:13:55 -04002338 ctx->cert->ecdh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002339}
2340
2341void SSL_set_tmp_ecdh_callback(SSL *ssl,
David Benjamindd978782015-04-24 15:20:13 -04002342 EC_KEY *(*callback)(SSL *ssl, int is_export,
2343 int keylength)) {
David Benjamin59015c32015-04-26 13:13:55 -04002344 ssl->cert->ecdh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002345}
2346
2347int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
2348 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
David Benjamin3570d732015-06-29 00:28:17 -04002349 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -08002350 return 0;
2351 }
2352
David Benjamin2755a3e2015-04-22 16:17:58 -04002353 OPENSSL_free(ctx->psk_identity_hint);
Adam Langleyfcf25832014-12-18 17:42:32 -08002354
2355 if (identity_hint != NULL) {
2356 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2357 if (ctx->psk_identity_hint == NULL) {
2358 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002359 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002360 } else {
2361 ctx->psk_identity_hint = NULL;
2362 }
Adam Langley95c29f32014-06-20 12:00:00 -07002363
Adam Langleyfcf25832014-12-18 17:42:32 -08002364 return 1;
2365}
2366
2367int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint) {
2368 if (s == NULL) {
2369 return 0;
2370 }
2371
2372 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
David Benjamin3570d732015-06-29 00:28:17 -04002373 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -08002374 return 0;
2375 }
2376
2377 /* Clear currently configured hint, if any. */
David Benjamin2755a3e2015-04-22 16:17:58 -04002378 OPENSSL_free(s->psk_identity_hint);
2379 s->psk_identity_hint = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002380
2381 if (identity_hint != NULL) {
2382 s->psk_identity_hint = BUF_strdup(identity_hint);
2383 if (s->psk_identity_hint == NULL) {
2384 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002385 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002386 }
Adam Langley95c29f32014-06-20 12:00:00 -07002387
Adam Langleyfcf25832014-12-18 17:42:32 -08002388 return 1;
2389}
Adam Langley95c29f32014-06-20 12:00:00 -07002390
Adam Langleyfcf25832014-12-18 17:42:32 -08002391const char *SSL_get_psk_identity_hint(const SSL *s) {
2392 if (s == NULL) {
2393 return NULL;
2394 }
2395 return s->psk_identity_hint;
2396}
Adam Langley95c29f32014-06-20 12:00:00 -07002397
Adam Langleyfcf25832014-12-18 17:42:32 -08002398const char *SSL_get_psk_identity(const SSL *s) {
2399 if (s == NULL || s->session == NULL) {
2400 return NULL;
2401 }
Adam Langley95c29f32014-06-20 12:00:00 -07002402
Adam Langleyfcf25832014-12-18 17:42:32 -08002403 return s->session->psk_identity;
2404}
Adam Langley95c29f32014-06-20 12:00:00 -07002405
Adam Langleyfcf25832014-12-18 17:42:32 -08002406void SSL_set_psk_client_callback(
2407 SSL *s, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2408 unsigned int max_identity_len, uint8_t *psk,
2409 unsigned int max_psk_len)) {
2410 s->psk_client_callback = cb;
2411}
Adam Langley95c29f32014-06-20 12:00:00 -07002412
Adam Langleyfcf25832014-12-18 17:42:32 -08002413void SSL_CTX_set_psk_client_callback(
2414 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2415 unsigned int max_identity_len,
2416 uint8_t *psk, unsigned int max_psk_len)) {
2417 ctx->psk_client_callback = cb;
2418}
Adam Langley95c29f32014-06-20 12:00:00 -07002419
Adam Langleyfcf25832014-12-18 17:42:32 -08002420void SSL_set_psk_server_callback(
2421 SSL *s, unsigned int (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
2422 unsigned int max_psk_len)) {
2423 s->psk_server_callback = cb;
2424}
Adam Langley95c29f32014-06-20 12:00:00 -07002425
Adam Langleyfcf25832014-12-18 17:42:32 -08002426void SSL_CTX_set_psk_server_callback(
2427 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *identity,
2428 uint8_t *psk, unsigned int max_psk_len)) {
2429 ctx->psk_server_callback = cb;
2430}
Adam Langley95c29f32014-06-20 12:00:00 -07002431
Adam Langleyfcf25832014-12-18 17:42:32 -08002432void SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) {
2433 ctx->min_version = version;
2434}
Adam Langley95c29f32014-06-20 12:00:00 -07002435
Adam Langleyfcf25832014-12-18 17:42:32 -08002436void SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) {
2437 ctx->max_version = version;
2438}
Adam Langley0289c732014-06-20 12:00:00 -07002439
Adam Langleyfcf25832014-12-18 17:42:32 -08002440void SSL_set_min_version(SSL *ssl, uint16_t version) {
2441 ssl->min_version = version;
2442}
Adam Langley0289c732014-06-20 12:00:00 -07002443
Adam Langleyfcf25832014-12-18 17:42:32 -08002444void SSL_set_max_version(SSL *ssl, uint16_t version) {
2445 ssl->max_version = version;
2446}
Adam Langley95c29f32014-06-20 12:00:00 -07002447
Adam Langleyfcf25832014-12-18 17:42:32 -08002448void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
2449 void (*cb)(int write_p, int version,
2450 int content_type, const void *buf,
2451 size_t len, SSL *ssl, void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002452 ctx->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002453}
David Benjamin61ecccf2015-05-05 09:44:51 -04002454
2455void SSL_CTX_set_msg_callback_arg(SSL_CTX *ctx, void *arg) {
2456 ctx->msg_callback_arg = arg;
2457}
2458
Adam Langleyfcf25832014-12-18 17:42:32 -08002459void SSL_set_msg_callback(SSL *ssl,
2460 void (*cb)(int write_p, int version, int content_type,
2461 const void *buf, size_t len, SSL *ssl,
2462 void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002463 ssl->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002464}
Adam Langley95c29f32014-06-20 12:00:00 -07002465
David Benjamin61ecccf2015-05-05 09:44:51 -04002466void SSL_set_msg_callback_arg(SSL *ssl, void *arg) {
2467 ssl->msg_callback_arg = arg;
2468}
2469
Adam Langleyfcf25832014-12-18 17:42:32 -08002470void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio) {
David Benjamin2755a3e2015-04-22 16:17:58 -04002471 BIO_free(ctx->keylog_bio);
Adam Langleyfcf25832014-12-18 17:42:32 -08002472 ctx->keylog_bio = keylog_bio;
2473}
Adam Langley95c29f32014-06-20 12:00:00 -07002474
Adam Langleyfcf25832014-12-18 17:42:32 -08002475static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
2476 static const char hextable[] = "0123456789abcdef";
2477 uint8_t *out;
2478 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002479
Adam Langleyfcf25832014-12-18 17:42:32 -08002480 if (!CBB_add_space(cbb, &out, in_len * 2)) {
2481 return 0;
2482 }
Adam Langley95c29f32014-06-20 12:00:00 -07002483
Adam Langleyfcf25832014-12-18 17:42:32 -08002484 for (i = 0; i < in_len; i++) {
2485 *(out++) = (uint8_t)hextable[in[i] >> 4];
2486 *(out++) = (uint8_t)hextable[in[i] & 0xf];
2487 }
Adam Langley95c29f32014-06-20 12:00:00 -07002488
Adam Langleyfcf25832014-12-18 17:42:32 -08002489 return 1;
2490}
David Benjamin859ec3c2014-09-02 16:29:36 -04002491
2492int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -08002493 const uint8_t *encrypted_premaster,
2494 size_t encrypted_premaster_len,
2495 const uint8_t *premaster,
2496 size_t premaster_len) {
2497 BIO *bio = ctx->keylog_bio;
2498 CBB cbb;
2499 uint8_t *out;
2500 size_t out_len;
2501 int ret;
David Benjamin859ec3c2014-09-02 16:29:36 -04002502
Adam Langleyfcf25832014-12-18 17:42:32 -08002503 if (bio == NULL) {
2504 return 1;
2505 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002506
Adam Langleyfcf25832014-12-18 17:42:32 -08002507 if (encrypted_premaster_len < 8) {
David Benjamin3570d732015-06-29 00:28:17 -04002508 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08002509 return 0;
2510 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002511
David Benjamina8653202015-06-28 01:26:10 -04002512 CBB_zero(&cbb);
2513 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len * 2 + 1) ||
2514 !CBB_add_bytes(&cbb, (const uint8_t *)"RSA ", 4) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08002515 /* Only the first 8 bytes of the encrypted premaster secret are
2516 * logged. */
2517 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
2518 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2519 !cbb_add_hex(&cbb, premaster, premaster_len) ||
2520 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2521 !CBB_finish(&cbb, &out, &out_len)) {
2522 CBB_cleanup(&cbb);
2523 return 0;
2524 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002525
Adam Langley4bdb6e42015-05-15 15:29:21 -07002526 CRYPTO_MUTEX_lock_write(&ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -08002527 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
Adam Langley4bdb6e42015-05-15 15:29:21 -07002528 CRYPTO_MUTEX_unlock(&ctx->lock);
David Benjamin859ec3c2014-09-02 16:29:36 -04002529
Adam Langleyfcf25832014-12-18 17:42:32 -08002530 OPENSSL_free(out);
2531 return ret;
Adam Langley95f22882014-06-20 12:00:00 -07002532}
2533
Adam Langleyfcf25832014-12-18 17:42:32 -08002534int ssl_ctx_log_master_secret(SSL_CTX *ctx, const uint8_t *client_random,
2535 size_t client_random_len, const uint8_t *master,
2536 size_t master_len) {
2537 BIO *bio = ctx->keylog_bio;
2538 CBB cbb;
2539 uint8_t *out;
2540 size_t out_len;
2541 int ret;
Adam Langleyadb739e2014-06-20 12:00:00 -07002542
Adam Langleyfcf25832014-12-18 17:42:32 -08002543 if (bio == NULL) {
2544 return 1;
2545 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002546
Adam Langleyfcf25832014-12-18 17:42:32 -08002547 if (client_random_len != 32) {
David Benjamin3570d732015-06-29 00:28:17 -04002548 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08002549 return 0;
2550 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002551
David Benjamina8653202015-06-28 01:26:10 -04002552 CBB_zero(&cbb);
2553 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len * 2 + 1) ||
2554 !CBB_add_bytes(&cbb, (const uint8_t *)"CLIENT_RANDOM ", 14) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08002555 !cbb_add_hex(&cbb, client_random, 32) ||
2556 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2557 !cbb_add_hex(&cbb, master, master_len) ||
2558 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2559 !CBB_finish(&cbb, &out, &out_len)) {
2560 CBB_cleanup(&cbb);
2561 return 0;
2562 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002563
Adam Langley4bdb6e42015-05-15 15:29:21 -07002564 CRYPTO_MUTEX_lock_write(&ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -08002565 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
Adam Langley4bdb6e42015-05-15 15:29:21 -07002566 CRYPTO_MUTEX_unlock(&ctx->lock);
David Benjamine99e9122014-12-11 01:46:01 -05002567
Adam Langleyfcf25832014-12-18 17:42:32 -08002568 OPENSSL_free(out);
2569 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -07002570}
2571
David Benjamined7c4752015-02-16 19:16:46 -05002572int SSL_in_false_start(const SSL *s) {
2573 return s->s3->tmp.in_false_start;
2574}
2575
Adam Langleyfcf25832014-12-18 17:42:32 -08002576int SSL_cutthrough_complete(const SSL *s) {
David Benjamined7c4752015-02-16 19:16:46 -05002577 return SSL_in_false_start(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002578}
Adam Langley95c29f32014-06-20 12:00:00 -07002579
Adam Langleyfcf25832014-12-18 17:42:32 -08002580void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
2581 size_t *ssl_session_size) {
2582 *ssl_size = sizeof(SSL);
2583 *ssl_ctx_size = sizeof(SSL_CTX);
2584 *ssl_session_size = sizeof(SSL_SESSION);
2585}
Feng Lu41aa3252014-11-21 22:47:56 -08002586
David Benjamined7c4752015-02-16 19:16:46 -05002587int ssl3_can_false_start(const SSL *s) {
David Benjamin195dc782015-02-19 13:27:05 -05002588 const SSL_CIPHER *const cipher = SSL_get_current_cipher(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002589
Adam Langleye631d962015-04-01 13:11:01 -07002590 /* False Start only for TLS 1.2 with an ECDHE+AEAD cipher and ALPN or NPN. */
David Benjamin195dc782015-02-19 13:27:05 -05002591 return !SSL_IS_DTLS(s) &&
2592 SSL_version(s) >= TLS1_2_VERSION &&
2593 (s->s3->alpn_selected || s->s3->next_proto_neg_seen) &&
2594 cipher != NULL &&
Adam Langleye631d962015-04-01 13:11:01 -07002595 cipher->algorithm_mkey == SSL_kECDHE &&
David Benjamin195dc782015-02-19 13:27:05 -05002596 (cipher->algorithm_enc == SSL_AES128GCM ||
2597 cipher->algorithm_enc == SSL_AES256GCM ||
2598 cipher->algorithm_enc == SSL_CHACHA20POLY1305);
Adam Langleyfcf25832014-12-18 17:42:32 -08002599}
2600
2601const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version) {
2602 switch (version) {
2603 case SSL3_VERSION:
2604 return &SSLv3_enc_data;
2605
2606 case TLS1_VERSION:
2607 return &TLSv1_enc_data;
2608
David Benjamin9e13e1a2015-03-05 01:56:32 -05002609 case DTLS1_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002610 case TLS1_1_VERSION:
2611 return &TLSv1_1_enc_data;
2612
David Benjamin9e13e1a2015-03-05 01:56:32 -05002613 case DTLS1_2_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002614 case TLS1_2_VERSION:
2615 return &TLSv1_2_enc_data;
2616
Adam Langleyfcf25832014-12-18 17:42:32 -08002617 default:
2618 return NULL;
2619 }
2620}
2621
2622uint16_t ssl3_get_max_server_version(const SSL *s) {
2623 uint16_t max_version;
2624
2625 if (SSL_IS_DTLS(s)) {
2626 max_version = (s->max_version != 0) ? s->max_version : DTLS1_2_VERSION;
2627 if (!(s->options & SSL_OP_NO_DTLSv1_2) && DTLS1_2_VERSION >= max_version) {
2628 return DTLS1_2_VERSION;
2629 }
2630 if (!(s->options & SSL_OP_NO_DTLSv1) && DTLS1_VERSION >= max_version) {
2631 return DTLS1_VERSION;
2632 }
2633 return 0;
2634 }
2635
2636 max_version = (s->max_version != 0) ? s->max_version : TLS1_2_VERSION;
2637 if (!(s->options & SSL_OP_NO_TLSv1_2) && TLS1_2_VERSION <= max_version) {
2638 return TLS1_2_VERSION;
2639 }
2640 if (!(s->options & SSL_OP_NO_TLSv1_1) && TLS1_1_VERSION <= max_version) {
2641 return TLS1_1_VERSION;
2642 }
2643 if (!(s->options & SSL_OP_NO_TLSv1) && TLS1_VERSION <= max_version) {
2644 return TLS1_VERSION;
2645 }
2646 if (!(s->options & SSL_OP_NO_SSLv3) && SSL3_VERSION <= max_version) {
2647 return SSL3_VERSION;
2648 }
2649 return 0;
2650}
2651
2652uint16_t ssl3_get_mutual_version(SSL *s, uint16_t client_version) {
2653 uint16_t version = 0;
2654
2655 if (SSL_IS_DTLS(s)) {
2656 /* Clamp client_version to max_version. */
2657 if (s->max_version != 0 && client_version < s->max_version) {
2658 client_version = s->max_version;
2659 }
2660
2661 if (client_version <= DTLS1_2_VERSION && !(s->options & SSL_OP_NO_DTLSv1_2)) {
2662 version = DTLS1_2_VERSION;
2663 } else if (client_version <= DTLS1_VERSION &&
David Benjaminb18f0242015-03-10 18:30:08 -04002664 !(s->options & SSL_OP_NO_DTLSv1)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002665 version = DTLS1_VERSION;
2666 }
2667
2668 /* Check against min_version. */
2669 if (version != 0 && s->min_version != 0 && version > s->min_version) {
2670 return 0;
2671 }
2672 return version;
2673 } else {
2674 /* Clamp client_version to max_version. */
2675 if (s->max_version != 0 && client_version > s->max_version) {
2676 client_version = s->max_version;
2677 }
2678
2679 if (client_version >= TLS1_2_VERSION && !(s->options & SSL_OP_NO_TLSv1_2)) {
2680 version = TLS1_2_VERSION;
2681 } else if (client_version >= TLS1_1_VERSION &&
2682 !(s->options & SSL_OP_NO_TLSv1_1)) {
2683 version = TLS1_1_VERSION;
2684 } else if (client_version >= TLS1_VERSION && !(s->options & SSL_OP_NO_TLSv1)) {
2685 version = TLS1_VERSION;
2686 } else if (client_version >= SSL3_VERSION && !(s->options & SSL_OP_NO_SSLv3)) {
2687 version = SSL3_VERSION;
2688 }
2689
2690 /* Check against min_version. */
2691 if (version != 0 && s->min_version != 0 && version < s->min_version) {
2692 return 0;
2693 }
2694 return version;
2695 }
2696}
2697
2698uint16_t ssl3_get_max_client_version(SSL *s) {
David Benjamin123a8fd2015-04-26 14:16:41 -04002699 uint32_t options = s->options;
Adam Langleyfcf25832014-12-18 17:42:32 -08002700 uint16_t version = 0;
2701
2702 /* OpenSSL's API for controlling versions entails blacklisting individual
2703 * protocols. This has two problems. First, on the client, the protocol can
2704 * only express a contiguous range of versions. Second, a library consumer
2705 * trying to set a maximum version cannot disable protocol versions that get
2706 * added in a future version of the library.
2707 *
2708 * To account for both of these, OpenSSL interprets the client-side bitmask
2709 * as a min/max range by picking the lowest contiguous non-empty range of
2710 * enabled protocols. Note that this means it is impossible to set a maximum
2711 * version of TLS 1.2 in a future-proof way.
2712 *
2713 * By this scheme, the maximum version is the lowest version V such that V is
2714 * enabled and V+1 is disabled or unimplemented. */
2715 if (SSL_IS_DTLS(s)) {
2716 if (!(options & SSL_OP_NO_DTLSv1_2)) {
2717 version = DTLS1_2_VERSION;
2718 }
2719 if (!(options & SSL_OP_NO_DTLSv1) && (options & SSL_OP_NO_DTLSv1_2)) {
2720 version = DTLS1_VERSION;
2721 }
2722 if (s->max_version != 0 && version < s->max_version) {
2723 version = s->max_version;
2724 }
2725 } else {
2726 if (!(options & SSL_OP_NO_TLSv1_2)) {
2727 version = TLS1_2_VERSION;
2728 }
2729 if (!(options & SSL_OP_NO_TLSv1_1) && (options & SSL_OP_NO_TLSv1_2)) {
2730 version = TLS1_1_VERSION;
2731 }
2732 if (!(options & SSL_OP_NO_TLSv1) && (options & SSL_OP_NO_TLSv1_1)) {
2733 version = TLS1_VERSION;
2734 }
2735 if (!(options & SSL_OP_NO_SSLv3) && (options & SSL_OP_NO_TLSv1)) {
2736 version = SSL3_VERSION;
2737 }
2738 if (s->max_version != 0 && version > s->max_version) {
2739 version = s->max_version;
2740 }
2741 }
2742
2743 return version;
2744}
2745
2746int ssl3_is_version_enabled(SSL *s, uint16_t version) {
2747 if (SSL_IS_DTLS(s)) {
2748 if (s->max_version != 0 && version < s->max_version) {
2749 return 0;
2750 }
2751 if (s->min_version != 0 && version > s->min_version) {
2752 return 0;
2753 }
2754
2755 switch (version) {
2756 case DTLS1_VERSION:
2757 return !(s->options & SSL_OP_NO_DTLSv1);
2758
2759 case DTLS1_2_VERSION:
2760 return !(s->options & SSL_OP_NO_DTLSv1_2);
2761
2762 default:
2763 return 0;
2764 }
2765 } else {
2766 if (s->max_version != 0 && version > s->max_version) {
2767 return 0;
2768 }
2769 if (s->min_version != 0 && version < s->min_version) {
2770 return 0;
2771 }
2772
2773 switch (version) {
2774 case SSL3_VERSION:
2775 return !(s->options & SSL_OP_NO_SSLv3);
2776
2777 case TLS1_VERSION:
2778 return !(s->options & SSL_OP_NO_TLSv1);
2779
2780 case TLS1_1_VERSION:
2781 return !(s->options & SSL_OP_NO_TLSv1_1);
2782
2783 case TLS1_2_VERSION:
2784 return !(s->options & SSL_OP_NO_TLSv1_2);
2785
2786 default:
2787 return 0;
2788 }
2789 }
2790}
2791
David Benjaminea72bd02014-12-21 21:27:41 -05002792uint16_t ssl3_version_from_wire(SSL *s, uint16_t wire_version) {
2793 if (!SSL_IS_DTLS(s)) {
2794 return wire_version;
2795 }
2796
2797 uint16_t tls_version = ~wire_version;
2798 uint16_t version = tls_version + 0x0201;
2799 /* If either component overflowed, clamp it so comparisons still work. */
2800 if ((version >> 8) < (tls_version >> 8)) {
2801 version = 0xff00 | (version & 0xff);
2802 }
2803 if ((version & 0xff) < (tls_version & 0xff)) {
2804 version = (version & 0xff00) | 0xff;
2805 }
2806 /* DTLS 1.0 maps to TLS 1.1, not TLS 1.0. */
2807 if (version == TLS1_VERSION) {
2808 version = TLS1_1_VERSION;
2809 }
2810 return version;
2811}
2812
Adam Langleyfcf25832014-12-18 17:42:32 -08002813int SSL_cache_hit(SSL *s) { return s->hit; }
2814
2815int SSL_is_server(SSL *s) { return s->server; }
2816
Adam Langley524e7172015-02-20 16:04:00 -08002817void SSL_CTX_set_dos_protection_cb(
2818 SSL_CTX *ctx, int (*cb)(const struct ssl_early_callback_ctx *)) {
2819 ctx->dos_protection_cb = cb;
2820}
2821
David Benjaminb16346b2015-04-08 19:16:58 -04002822void SSL_set_reject_peer_renegotiations(SSL *s, int reject) {
David Benjamin897e5e02015-05-12 17:03:54 -04002823 s->accept_peer_renegotiations = !reject;
David Benjaminb16346b2015-04-08 19:16:58 -04002824}
2825
Adam Langley3f92d212015-02-20 15:32:52 -08002826int SSL_get_rc4_state(const SSL *ssl, const RC4_KEY **read_key,
2827 const RC4_KEY **write_key) {
2828 if (ssl->aead_read_ctx == NULL || ssl->aead_write_ctx == NULL) {
2829 return 0;
2830 }
2831
2832 return EVP_AEAD_CTX_get_rc4_state(&ssl->aead_read_ctx->ctx, read_key) &&
2833 EVP_AEAD_CTX_get_rc4_state(&ssl->aead_write_ctx->ctx, write_key);
2834}
David Benjaminda881e92015-04-26 14:45:04 -04002835
Adam Langleyaf0e32c2015-06-03 09:57:23 -07002836int SSL_get_tls_unique(const SSL *ssl, uint8_t *out, size_t *out_len,
2837 size_t max_out) {
2838 /* The tls-unique value is the first Finished message in the handshake, which
2839 * is the client's in a full handshake and the server's for a resumption. See
2840 * https://tools.ietf.org/html/rfc5929#section-3.1. */
2841 const uint8_t *finished = ssl->s3->previous_client_finished;
2842 size_t finished_len = ssl->s3->previous_client_finished_len;
2843 if (ssl->hit) {
2844 /* tls-unique is broken for resumed sessions unless EMS is used. */
2845 if (!ssl->session->extended_master_secret) {
2846 goto err;
2847 }
2848 finished = ssl->s3->previous_server_finished;
2849 finished_len = ssl->s3->previous_server_finished_len;
2850 }
2851
2852 if (!ssl->s3->initial_handshake_complete ||
2853 ssl->version < TLS1_VERSION) {
2854 goto err;
2855 }
2856
2857 *out_len = finished_len;
2858 if (finished_len > max_out) {
2859 *out_len = max_out;
2860 }
2861
2862 memcpy(out, finished, *out_len);
2863 return 1;
2864
2865err:
2866 *out_len = 0;
2867 memset(out, 0, max_out);
2868 return 0;
2869}
2870
David Benjaminda881e92015-04-26 14:45:04 -04002871int SSL_CTX_sess_connect(const SSL_CTX *ctx) { return 0; }
2872int SSL_CTX_sess_connect_good(const SSL_CTX *ctx) { return 0; }
2873int SSL_CTX_sess_connect_renegotiate(const SSL_CTX *ctx) { return 0; }
2874int SSL_CTX_sess_accept(const SSL_CTX *ctx) { return 0; }
2875int SSL_CTX_sess_accept_renegotiate(const SSL_CTX *ctx) { return 0; }
2876int SSL_CTX_sess_accept_good(const SSL_CTX *ctx) { return 0; }
2877int SSL_CTX_sess_hits(const SSL_CTX *ctx) { return 0; }
2878int SSL_CTX_sess_cb_hits(const SSL_CTX *ctx) { return 0; }
2879int SSL_CTX_sess_misses(const SSL_CTX *ctx) { return 0; }
2880int SSL_CTX_sess_timeouts(const SSL_CTX *ctx) { return 0; }
2881int SSL_CTX_sess_cache_full(const SSL_CTX *ctx) { return 0; }