blob: 3a2e0dd32f76cd86e0af91944597875f3cc12a39 [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 Benjamin9f33fc62015-04-15 17:29:53 -0400167static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl = CRYPTO_EX_DATA_CLASS_INIT;
168static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl_ctx = CRYPTO_EX_DATA_CLASS_INIT;
169
David Benjamin4831c332015-05-16 11:43:13 -0400170int SSL_clear(SSL *ssl) {
171 if (ssl->method == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800172 OPENSSL_PUT_ERROR(SSL, SSL_clear, SSL_R_NO_METHOD_SPECIFIED);
173 return 0;
174 }
Adam Langley95c29f32014-06-20 12:00:00 -0700175
David Benjamin4831c332015-05-16 11:43:13 -0400176 if (ssl_clear_bad_session(ssl)) {
177 SSL_SESSION_free(ssl->session);
178 ssl->session = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800179 }
Adam Langley95c29f32014-06-20 12:00:00 -0700180
David Benjamin4831c332015-05-16 11:43:13 -0400181 ssl->hit = 0;
182 ssl->shutdown = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700183
David Benjamin4831c332015-05-16 11:43:13 -0400184 if (ssl->renegotiate) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800185 OPENSSL_PUT_ERROR(SSL, SSL_clear, ERR_R_INTERNAL_ERROR);
186 return 0;
187 }
Adam Langley95c29f32014-06-20 12:00:00 -0700188
David Benjamin4831c332015-05-16 11:43:13 -0400189 /* SSL_clear may be called before or after the |ssl| is initialized in either
Adam Langleyfcf25832014-12-18 17:42:32 -0800190 * accept or connect state. In the latter case, SSL_clear should preserve the
David Benjamin4831c332015-05-16 11:43:13 -0400191 * half and reset |ssl->state| accordingly. */
192 if (ssl->handshake_func != NULL) {
193 if (ssl->server) {
194 SSL_set_accept_state(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800195 } else {
David Benjamin4831c332015-05-16 11:43:13 -0400196 SSL_set_connect_state(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800197 }
198 } else {
David Benjamin4831c332015-05-16 11:43:13 -0400199 assert(ssl->state == 0);
Adam Langleyfcf25832014-12-18 17:42:32 -0800200 }
Adam Langley95c29f32014-06-20 12:00:00 -0700201
David Benjamin62fd1622015-01-11 13:30:01 -0500202 /* TODO(davidben): Some state on |s| is reset both in |SSL_new| and
203 * |SSL_clear| because it is per-connection state rather than configuration
David Benjamin4831c332015-05-16 11:43:13 -0400204 * state. Per-connection state should be on |ssl->s3| and |ssl->d1| so it is
David Benjamin62fd1622015-01-11 13:30:01 -0500205 * naturally reset at the right points between |SSL_new|, |SSL_clear|, and
206 * |ssl3_new|. */
207
David Benjamin4831c332015-05-16 11:43:13 -0400208 ssl->rwstate = SSL_NOTHING;
209 ssl->rstate = SSL_ST_READ_HEADER;
Adam Langley95c29f32014-06-20 12:00:00 -0700210
David Benjamin4831c332015-05-16 11:43:13 -0400211 BUF_MEM_free(ssl->init_buf);
212 ssl->init_buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700213
David Benjamin4831c332015-05-16 11:43:13 -0400214 ssl->packet = NULL;
215 ssl->packet_length = 0;
David Benjamin62fd1622015-01-11 13:30:01 -0500216
David Benjamin4831c332015-05-16 11:43:13 -0400217 ssl_clear_cipher_ctx(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700218
David Benjamin4831c332015-05-16 11:43:13 -0400219 OPENSSL_free(ssl->next_proto_negotiated);
220 ssl->next_proto_negotiated = NULL;
221 ssl->next_proto_negotiated_len = 0;
David Benjamin62fd1622015-01-11 13:30:01 -0500222
David Benjamin4831c332015-05-16 11:43:13 -0400223 /* The ssl->d1->mtu is simultaneously configuration (preserved across
David Benjamin62fd1622015-01-11 13:30:01 -0500224 * clear) and connection-specific state (gets reset).
225 *
226 * TODO(davidben): Avoid this. */
227 unsigned mtu = 0;
David Benjamin4831c332015-05-16 11:43:13 -0400228 if (ssl->d1 != NULL) {
229 mtu = ssl->d1->mtu;
David Benjamin62fd1622015-01-11 13:30:01 -0500230 }
231
David Benjamin4831c332015-05-16 11:43:13 -0400232 ssl->method->ssl_free(ssl);
233 if (!ssl->method->ssl_new(ssl)) {
David Benjamin62fd1622015-01-11 13:30:01 -0500234 return 0;
235 }
David Benjamin4831c332015-05-16 11:43:13 -0400236 ssl->enc_method = ssl3_get_enc_method(ssl->version);
237 assert(ssl->enc_method != NULL);
David Benjamin62fd1622015-01-11 13:30:01 -0500238
David Benjamin4831c332015-05-16 11:43:13 -0400239 if (SSL_IS_DTLS(ssl) && (SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
240 ssl->d1->mtu = mtu;
David Benjamin62fd1622015-01-11 13:30:01 -0500241 }
242
David Benjamin4831c332015-05-16 11:43:13 -0400243 ssl->client_version = ssl->version;
Adam Langley95c29f32014-06-20 12:00:00 -0700244
Adam Langleyfcf25832014-12-18 17:42:32 -0800245 return 1;
246}
Adam Langley95c29f32014-06-20 12:00:00 -0700247
Adam Langleyfcf25832014-12-18 17:42:32 -0800248SSL *SSL_new(SSL_CTX *ctx) {
249 SSL *s;
Adam Langley95c29f32014-06-20 12:00:00 -0700250
Adam Langleyfcf25832014-12-18 17:42:32 -0800251 if (ctx == NULL) {
252 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_NULL_SSL_CTX);
253 return NULL;
254 }
255 if (ctx->method == NULL) {
256 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
257 return NULL;
258 }
Adam Langley95c29f32014-06-20 12:00:00 -0700259
Adam Langleyfcf25832014-12-18 17:42:32 -0800260 s = (SSL *)OPENSSL_malloc(sizeof(SSL));
261 if (s == NULL) {
262 goto err;
263 }
264 memset(s, 0, sizeof(SSL));
Adam Langley95c29f32014-06-20 12:00:00 -0700265
Adam Langleyfcf25832014-12-18 17:42:32 -0800266 s->min_version = ctx->min_version;
267 s->max_version = ctx->max_version;
David Benjamin1eb367c2014-12-12 18:17:51 -0500268
Adam Langleyfcf25832014-12-18 17:42:32 -0800269 s->options = ctx->options;
270 s->mode = ctx->mode;
271 s->max_cert_list = ctx->max_cert_list;
Adam Langley95c29f32014-06-20 12:00:00 -0700272
David Benjamina5a3eeb2015-03-18 20:26:30 -0400273 s->cert = ssl_cert_dup(ctx->cert);
274 if (s->cert == NULL) {
275 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -0800276 }
Adam Langley95c29f32014-06-20 12:00:00 -0700277
Adam Langleyfcf25832014-12-18 17:42:32 -0800278 s->msg_callback = ctx->msg_callback;
279 s->msg_callback_arg = ctx->msg_callback_arg;
280 s->verify_mode = ctx->verify_mode;
281 s->sid_ctx_length = ctx->sid_ctx_length;
282 assert(s->sid_ctx_length <= sizeof s->sid_ctx);
283 memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));
284 s->verify_callback = ctx->default_verify_callback;
285 s->generate_session_id = ctx->generate_session_id;
Adam Langley95c29f32014-06-20 12:00:00 -0700286
Adam Langleyfcf25832014-12-18 17:42:32 -0800287 s->param = X509_VERIFY_PARAM_new();
288 if (!s->param) {
289 goto err;
290 }
291 X509_VERIFY_PARAM_inherit(s->param, ctx->param);
292 s->quiet_shutdown = ctx->quiet_shutdown;
293 s->max_send_fragment = ctx->max_send_fragment;
Adam Langley95c29f32014-06-20 12:00:00 -0700294
Adam Langley0b5e3902015-05-15 13:08:38 -0700295 CRYPTO_refcount_inc(&ctx->references);
Adam Langleyfcf25832014-12-18 17:42:32 -0800296 s->ctx = ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -0800297 s->tlsext_ticket_expected = 0;
Adam Langley0b5e3902015-05-15 13:08:38 -0700298 CRYPTO_refcount_inc(&ctx->references);
Adam Langleyfcf25832014-12-18 17:42:32 -0800299 s->initial_ctx = ctx;
300 if (ctx->tlsext_ecpointformatlist) {
301 s->tlsext_ecpointformatlist = BUF_memdup(
302 ctx->tlsext_ecpointformatlist, ctx->tlsext_ecpointformatlist_length);
303 if (!s->tlsext_ecpointformatlist) {
304 goto err;
305 }
306 s->tlsext_ecpointformatlist_length = ctx->tlsext_ecpointformatlist_length;
307 }
Adam Langley95c29f32014-06-20 12:00:00 -0700308
Adam Langleyfcf25832014-12-18 17:42:32 -0800309 if (ctx->tlsext_ellipticcurvelist) {
310 s->tlsext_ellipticcurvelist =
311 BUF_memdup(ctx->tlsext_ellipticcurvelist,
312 ctx->tlsext_ellipticcurvelist_length * 2);
313 if (!s->tlsext_ellipticcurvelist) {
314 goto err;
315 }
316 s->tlsext_ellipticcurvelist_length = ctx->tlsext_ellipticcurvelist_length;
317 }
318 s->next_proto_negotiated = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700319
Adam Langleyfcf25832014-12-18 17:42:32 -0800320 if (s->ctx->alpn_client_proto_list) {
321 s->alpn_client_proto_list = BUF_memdup(s->ctx->alpn_client_proto_list,
322 s->ctx->alpn_client_proto_list_len);
323 if (s->alpn_client_proto_list == NULL) {
324 goto err;
325 }
326 s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;
327 }
Adam Langley95c29f32014-06-20 12:00:00 -0700328
Adam Langleyfcf25832014-12-18 17:42:32 -0800329 s->verify_result = X509_V_OK;
330 s->method = ctx->method;
Adam Langley95c29f32014-06-20 12:00:00 -0700331
Adam Langleyfcf25832014-12-18 17:42:32 -0800332 if (!s->method->ssl_new(s)) {
333 goto err;
334 }
335 s->enc_method = ssl3_get_enc_method(s->version);
336 assert(s->enc_method != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700337
David Benjamin62fd1622015-01-11 13:30:01 -0500338 s->rwstate = SSL_NOTHING;
339 s->rstate = SSL_ST_READ_HEADER;
Adam Langley95c29f32014-06-20 12:00:00 -0700340
David Benjamin9f33fc62015-04-15 17:29:53 -0400341 CRYPTO_new_ex_data(&g_ex_data_class_ssl, s, &s->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700342
Adam Langleyfcf25832014-12-18 17:42:32 -0800343 s->psk_identity_hint = NULL;
344 if (ctx->psk_identity_hint) {
345 s->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
346 if (s->psk_identity_hint == NULL) {
347 goto err;
348 }
349 }
350 s->psk_client_callback = ctx->psk_client_callback;
351 s->psk_server_callback = ctx->psk_server_callback;
Adam Langley95c29f32014-06-20 12:00:00 -0700352
David Benjamin02ddbfd2015-01-11 13:09:11 -0500353 s->tlsext_channel_id_enabled = ctx->tlsext_channel_id_enabled;
354 if (ctx->tlsext_channel_id_private) {
David Benjamin9a10f8f2015-05-05 22:22:40 -0400355 s->tlsext_channel_id_private =
356 EVP_PKEY_up_ref(ctx->tlsext_channel_id_private);
David Benjamin02ddbfd2015-01-11 13:09:11 -0500357 }
358
Adam Langleyfcf25832014-12-18 17:42:32 -0800359 s->signed_cert_timestamps_enabled = s->ctx->signed_cert_timestamps_enabled;
360 s->ocsp_stapling_enabled = s->ctx->ocsp_stapling_enabled;
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200361
Adam Langleyfcf25832014-12-18 17:42:32 -0800362 return s;
363
Adam Langley95c29f32014-06-20 12:00:00 -0700364err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400365 SSL_free(s);
Adam Langleyfcf25832014-12-18 17:42:32 -0800366 OPENSSL_PUT_ERROR(SSL, SSL_new, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700367
Adam Langleyfcf25832014-12-18 17:42:32 -0800368 return NULL;
369}
Adam Langley95c29f32014-06-20 12:00:00 -0700370
Adam Langleyfcf25832014-12-18 17:42:32 -0800371int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
372 unsigned int sid_ctx_len) {
373 if (sid_ctx_len > sizeof ctx->sid_ctx) {
374 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_session_id_context,
375 SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
376 return 0;
377 }
378 ctx->sid_ctx_length = sid_ctx_len;
379 memcpy(ctx->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_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
385 unsigned int sid_ctx_len) {
386 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
387 OPENSSL_PUT_ERROR(SSL, SSL_set_session_id_context,
388 SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
389 return 0;
390 }
391 ssl->sid_ctx_length = sid_ctx_len;
392 memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700393
Adam Langleyfcf25832014-12-18 17:42:32 -0800394 return 1;
395}
Adam Langley95c29f32014-06-20 12:00:00 -0700396
Adam Langleyfcf25832014-12-18 17:42:32 -0800397int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800398 ctx->generate_session_id = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -0800399 return 1;
400}
Adam Langley95c29f32014-06-20 12:00:00 -0700401
Adam Langleyfcf25832014-12-18 17:42:32 -0800402int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800403 ssl->generate_session_id = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -0800404 return 1;
405}
Adam Langley95c29f32014-06-20 12:00:00 -0700406
Adam Langleyfcf25832014-12-18 17:42:32 -0800407int SSL_has_matching_session_id(const SSL *ssl, const uint8_t *id,
408 unsigned int id_len) {
409 /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how we
410 * can "construct" a session to give us the desired check - ie. to find if
411 * there's a session in the hash table that would conflict with any new
412 * session built out of this id/id_len and the ssl_version in use by this
413 * SSL. */
414 SSL_SESSION r, *p;
Adam Langley95c29f32014-06-20 12:00:00 -0700415
Adam Langleyfcf25832014-12-18 17:42:32 -0800416 if (id_len > sizeof r.session_id) {
417 return 0;
418 }
Adam Langley95c29f32014-06-20 12:00:00 -0700419
Adam Langleyfcf25832014-12-18 17:42:32 -0800420 r.ssl_version = ssl->version;
421 r.session_id_length = id_len;
422 memcpy(r.session_id, id, id_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700423
Adam Langley4bdb6e42015-05-15 15:29:21 -0700424 CRYPTO_MUTEX_lock_read(&ssl->ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -0800425 p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r);
Adam Langley4bdb6e42015-05-15 15:29:21 -0700426 CRYPTO_MUTEX_unlock(&ssl->ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -0800427 return p != NULL;
428}
Adam Langley95c29f32014-06-20 12:00:00 -0700429
Adam Langleyfcf25832014-12-18 17:42:32 -0800430int SSL_CTX_set_purpose(SSL_CTX *s, int purpose) {
431 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
432}
433
434int SSL_set_purpose(SSL *s, int purpose) {
435 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
436}
437
438int SSL_CTX_set_trust(SSL_CTX *s, int trust) {
439 return X509_VERIFY_PARAM_set_trust(s->param, trust);
440}
441
442int SSL_set_trust(SSL *s, int trust) {
443 return X509_VERIFY_PARAM_set_trust(s->param, trust);
444}
445
446int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm) {
447 return X509_VERIFY_PARAM_set1(ctx->param, vpm);
448}
449
450int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm) {
451 return X509_VERIFY_PARAM_set1(ssl->param, vpm);
452}
Adam Langley95c29f32014-06-20 12:00:00 -0700453
Adam Langley858a88d2014-06-20 12:00:00 -0700454void ssl_cipher_preference_list_free(
Adam Langleyfcf25832014-12-18 17:42:32 -0800455 struct ssl_cipher_preference_list_st *cipher_list) {
David Benjamin5d1ec732015-04-22 13:38:00 -0400456 if (cipher_list == NULL) {
457 return;
458 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800459 sk_SSL_CIPHER_free(cipher_list->ciphers);
460 OPENSSL_free(cipher_list->in_group_flags);
461 OPENSSL_free(cipher_list);
462}
Adam Langley858a88d2014-06-20 12:00:00 -0700463
Adam Langleyfcf25832014-12-18 17:42:32 -0800464struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_dup(
465 struct ssl_cipher_preference_list_st *cipher_list) {
466 struct ssl_cipher_preference_list_st *ret = NULL;
467 size_t n = sk_SSL_CIPHER_num(cipher_list->ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700468
Adam Langleyfcf25832014-12-18 17:42:32 -0800469 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
470 if (!ret) {
471 goto err;
472 }
473
474 ret->ciphers = NULL;
475 ret->in_group_flags = NULL;
476 ret->ciphers = sk_SSL_CIPHER_dup(cipher_list->ciphers);
477 if (!ret->ciphers) {
478 goto err;
479 }
480 ret->in_group_flags = BUF_memdup(cipher_list->in_group_flags, n);
481 if (!ret->in_group_flags) {
482 goto err;
483 }
484
485 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -0700486
487err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400488 ssl_cipher_preference_list_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -0800489 return NULL;
490}
Adam Langley858a88d2014-06-20 12:00:00 -0700491
Adam Langleyfcf25832014-12-18 17:42:32 -0800492struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_from_ciphers(
David Benjamin60da0cd2015-05-03 15:21:28 -0400493 STACK_OF(SSL_CIPHER) *ciphers) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800494 struct ssl_cipher_preference_list_st *ret = NULL;
495 size_t n = sk_SSL_CIPHER_num(ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700496
Adam Langleyfcf25832014-12-18 17:42:32 -0800497 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
498 if (!ret) {
499 goto err;
500 }
501 ret->ciphers = NULL;
502 ret->in_group_flags = NULL;
503 ret->ciphers = sk_SSL_CIPHER_dup(ciphers);
504 if (!ret->ciphers) {
505 goto err;
506 }
507 ret->in_group_flags = OPENSSL_malloc(n);
508 if (!ret->in_group_flags) {
509 goto err;
510 }
511 memset(ret->in_group_flags, 0, n);
512 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -0700513
514err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400515 ssl_cipher_preference_list_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -0800516 return NULL;
517}
Adam Langley858a88d2014-06-20 12:00:00 -0700518
Adam Langleyfcf25832014-12-18 17:42:32 -0800519X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) { return ctx->param; }
Adam Langley95c29f32014-06-20 12:00:00 -0700520
Adam Langleyfcf25832014-12-18 17:42:32 -0800521X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) { return ssl->param; }
Adam Langley95c29f32014-06-20 12:00:00 -0700522
Adam Langleyfcf25832014-12-18 17:42:32 -0800523void SSL_certs_clear(SSL *s) { ssl_cert_clear_certs(s->cert); }
Adam Langley95c29f32014-06-20 12:00:00 -0700524
David Benjamin4831c332015-05-16 11:43:13 -0400525void SSL_free(SSL *ssl) {
526 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800527 return;
528 }
Adam Langley95c29f32014-06-20 12:00:00 -0700529
David Benjamin4831c332015-05-16 11:43:13 -0400530 X509_VERIFY_PARAM_free(ssl->param);
Adam Langley95c29f32014-06-20 12:00:00 -0700531
David Benjamin4831c332015-05-16 11:43:13 -0400532 CRYPTO_free_ex_data(&g_ex_data_class_ssl, ssl, &ssl->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700533
David Benjamin4831c332015-05-16 11:43:13 -0400534 if (ssl->bbio != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800535 /* If the buffering BIO is in place, pop it off */
David Benjamin4831c332015-05-16 11:43:13 -0400536 if (ssl->bbio == ssl->wbio) {
537 ssl->wbio = BIO_pop(ssl->wbio);
Adam Langleyfcf25832014-12-18 17:42:32 -0800538 }
David Benjamin4831c332015-05-16 11:43:13 -0400539 BIO_free(ssl->bbio);
540 ssl->bbio = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800541 }
Adam Langley95c29f32014-06-20 12:00:00 -0700542
David Benjamin4831c332015-05-16 11:43:13 -0400543 int free_wbio = ssl->wbio != ssl->rbio;
544 BIO_free_all(ssl->rbio);
David Benjamin2755a3e2015-04-22 16:17:58 -0400545 if (free_wbio) {
David Benjamin4831c332015-05-16 11:43:13 -0400546 BIO_free_all(ssl->wbio);
Adam Langleyfcf25832014-12-18 17:42:32 -0800547 }
Adam Langley95c29f32014-06-20 12:00:00 -0700548
David Benjamin4831c332015-05-16 11:43:13 -0400549 BUF_MEM_free(ssl->init_buf);
Adam Langley95c29f32014-06-20 12:00:00 -0700550
Adam Langleyfcf25832014-12-18 17:42:32 -0800551 /* add extra stuff */
David Benjamin4831c332015-05-16 11:43:13 -0400552 ssl_cipher_preference_list_free(ssl->cipher_list);
553 sk_SSL_CIPHER_free(ssl->cipher_list_by_id);
Adam Langley95c29f32014-06-20 12:00:00 -0700554
David Benjamin4831c332015-05-16 11:43:13 -0400555 ssl_clear_bad_session(ssl);
556 SSL_SESSION_free(ssl->session);
Adam Langley95c29f32014-06-20 12:00:00 -0700557
David Benjamin4831c332015-05-16 11:43:13 -0400558 ssl_clear_cipher_ctx(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700559
David Benjamin4831c332015-05-16 11:43:13 -0400560 ssl_cert_free(ssl->cert);
Adam Langley0289c732014-06-20 12:00:00 -0700561
David Benjamin4831c332015-05-16 11:43:13 -0400562 OPENSSL_free(ssl->tlsext_hostname);
563 SSL_CTX_free(ssl->initial_ctx);
564 OPENSSL_free(ssl->tlsext_ecpointformatlist);
565 OPENSSL_free(ssl->tlsext_ellipticcurvelist);
566 OPENSSL_free(ssl->alpn_client_proto_list);
567 EVP_PKEY_free(ssl->tlsext_channel_id_private);
568 OPENSSL_free(ssl->psk_identity_hint);
569 sk_X509_NAME_pop_free(ssl->client_CA, X509_NAME_free);
570 OPENSSL_free(ssl->next_proto_negotiated);
571 sk_SRTP_PROTECTION_PROFILE_free(ssl->srtp_profiles);
Adam Langley95c29f32014-06-20 12:00:00 -0700572
David Benjamin4831c332015-05-16 11:43:13 -0400573 if (ssl->method != NULL) {
574 ssl->method->ssl_free(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800575 }
David Benjamin4831c332015-05-16 11:43:13 -0400576 SSL_CTX_free(ssl->ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700577
David Benjamin4831c332015-05-16 11:43:13 -0400578 OPENSSL_free(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800579}
Adam Langley95c29f32014-06-20 12:00:00 -0700580
Adam Langleyfcf25832014-12-18 17:42:32 -0800581void SSL_set_bio(SSL *s, BIO *rbio, BIO *wbio) {
582 /* If the output buffering BIO is still in place, remove it. */
583 if (s->bbio != NULL) {
584 if (s->wbio == s->bbio) {
585 s->wbio = s->wbio->next_bio;
586 s->bbio->next_bio = NULL;
587 }
588 }
Adam Langley95c29f32014-06-20 12:00:00 -0700589
David Benjamin2755a3e2015-04-22 16:17:58 -0400590 if (s->rbio != rbio) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800591 BIO_free_all(s->rbio);
592 }
David Benjamin2755a3e2015-04-22 16:17:58 -0400593 if (s->wbio != wbio && s->rbio != s->wbio) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800594 BIO_free_all(s->wbio);
595 }
596 s->rbio = rbio;
597 s->wbio = wbio;
598}
Adam Langley95c29f32014-06-20 12:00:00 -0700599
Adam Langleyfcf25832014-12-18 17:42:32 -0800600BIO *SSL_get_rbio(const SSL *s) { return s->rbio; }
Adam Langley95c29f32014-06-20 12:00:00 -0700601
Adam Langleyfcf25832014-12-18 17:42:32 -0800602BIO *SSL_get_wbio(const SSL *s) { return s->wbio; }
Adam Langley95c29f32014-06-20 12:00:00 -0700603
Adam Langleyfcf25832014-12-18 17:42:32 -0800604int SSL_get_fd(const SSL *s) { return SSL_get_rfd(s); }
Adam Langley95c29f32014-06-20 12:00:00 -0700605
Adam Langleyfcf25832014-12-18 17:42:32 -0800606int SSL_get_rfd(const SSL *s) {
607 int ret = -1;
608 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700609
Adam Langleyfcf25832014-12-18 17:42:32 -0800610 b = SSL_get_rbio(s);
611 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
612 if (r != NULL) {
613 BIO_get_fd(r, &ret);
614 }
615 return ret;
616}
Adam Langley95c29f32014-06-20 12:00:00 -0700617
Adam Langleyfcf25832014-12-18 17:42:32 -0800618int SSL_get_wfd(const SSL *s) {
619 int ret = -1;
620 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700621
Adam Langleyfcf25832014-12-18 17:42:32 -0800622 b = SSL_get_wbio(s);
623 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
624 if (r != NULL) {
625 BIO_get_fd(r, &ret);
626 }
Adam Langley95c29f32014-06-20 12:00:00 -0700627
Adam Langleyfcf25832014-12-18 17:42:32 -0800628 return ret;
629}
Adam Langley95c29f32014-06-20 12:00:00 -0700630
Adam Langleyfcf25832014-12-18 17:42:32 -0800631int SSL_set_fd(SSL *s, int fd) {
632 int ret = 0;
633 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700634
Adam Langleyfcf25832014-12-18 17:42:32 -0800635 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700636
Adam Langleyfcf25832014-12-18 17:42:32 -0800637 if (bio == NULL) {
638 OPENSSL_PUT_ERROR(SSL, SSL_set_fd, ERR_R_BUF_LIB);
639 goto err;
640 }
641 BIO_set_fd(bio, fd, BIO_NOCLOSE);
642 SSL_set_bio(s, bio, bio);
643 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700644
Adam Langley95c29f32014-06-20 12:00:00 -0700645err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800646 return ret;
647}
Adam Langley95c29f32014-06-20 12:00:00 -0700648
Adam Langleyfcf25832014-12-18 17:42:32 -0800649int SSL_set_wfd(SSL *s, int fd) {
650 int ret = 0;
651 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700652
Adam Langleyfcf25832014-12-18 17:42:32 -0800653 if (s->rbio == NULL || BIO_method_type(s->rbio) != BIO_TYPE_FD ||
654 (int)BIO_get_fd(s->rbio, NULL) != fd) {
655 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700656
Adam Langleyfcf25832014-12-18 17:42:32 -0800657 if (bio == NULL) {
658 OPENSSL_PUT_ERROR(SSL, SSL_set_wfd, ERR_R_BUF_LIB);
659 goto err;
660 }
661 BIO_set_fd(bio, fd, BIO_NOCLOSE);
662 SSL_set_bio(s, SSL_get_rbio(s), bio);
663 } else {
664 SSL_set_bio(s, SSL_get_rbio(s), SSL_get_rbio(s));
665 }
666
667 ret = 1;
668
Adam Langley95c29f32014-06-20 12:00:00 -0700669err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800670 return ret;
671}
Adam Langley95c29f32014-06-20 12:00:00 -0700672
Adam Langleyfcf25832014-12-18 17:42:32 -0800673int SSL_set_rfd(SSL *s, int fd) {
674 int ret = 0;
675 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700676
Adam Langleyfcf25832014-12-18 17:42:32 -0800677 if (s->wbio == NULL || BIO_method_type(s->wbio) != BIO_TYPE_FD ||
678 (int)BIO_get_fd(s->wbio, NULL) != fd) {
679 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700680
Adam Langleyfcf25832014-12-18 17:42:32 -0800681 if (bio == NULL) {
682 OPENSSL_PUT_ERROR(SSL, SSL_set_rfd, ERR_R_BUF_LIB);
683 goto err;
684 }
685 BIO_set_fd(bio, fd, BIO_NOCLOSE);
686 SSL_set_bio(s, bio, SSL_get_wbio(s));
687 } else {
688 SSL_set_bio(s, SSL_get_wbio(s), SSL_get_wbio(s));
689 }
690 ret = 1;
691
Adam Langley95c29f32014-06-20 12:00:00 -0700692err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800693 return ret;
694}
Adam Langley95c29f32014-06-20 12:00:00 -0700695
696/* return length of latest Finished message we sent, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -0800697size_t SSL_get_finished(const SSL *s, void *buf, size_t count) {
698 size_t ret = 0;
699
700 if (s->s3 != NULL) {
701 ret = s->s3->tmp.finish_md_len;
702 if (count > ret) {
703 count = ret;
704 }
705 memcpy(buf, s->s3->tmp.finish_md, count);
706 }
707
708 return ret;
709}
Adam Langley95c29f32014-06-20 12:00:00 -0700710
711/* return length of latest Finished message we expected, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -0800712size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count) {
713 size_t ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700714
Adam Langleyfcf25832014-12-18 17:42:32 -0800715 if (s->s3 != NULL) {
716 ret = s->s3->tmp.peer_finish_md_len;
717 if (count > ret) {
718 count = ret;
719 }
720 memcpy(buf, s->s3->tmp.peer_finish_md, count);
721 }
Adam Langley95c29f32014-06-20 12:00:00 -0700722
Adam Langleyfcf25832014-12-18 17:42:32 -0800723 return ret;
724}
Adam Langley95c29f32014-06-20 12:00:00 -0700725
Adam Langleyfcf25832014-12-18 17:42:32 -0800726int SSL_get_verify_mode(const SSL *s) { return s->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -0700727
Adam Langleyfcf25832014-12-18 17:42:32 -0800728int SSL_get_verify_depth(const SSL *s) {
729 return X509_VERIFY_PARAM_get_depth(s->param);
730}
Adam Langley95c29f32014-06-20 12:00:00 -0700731
Adam Langleyfcf25832014-12-18 17:42:32 -0800732int (*SSL_get_verify_callback(const SSL *s))(int, X509_STORE_CTX *) {
733 return s->verify_callback;
734}
Adam Langley95c29f32014-06-20 12:00:00 -0700735
Adam Langleyfcf25832014-12-18 17:42:32 -0800736int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) { return ctx->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -0700737
Adam Langleyfcf25832014-12-18 17:42:32 -0800738int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) {
739 return X509_VERIFY_PARAM_get_depth(ctx->param);
740}
Adam Langley95c29f32014-06-20 12:00:00 -0700741
Adam Langleyfcf25832014-12-18 17:42:32 -0800742int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int, X509_STORE_CTX *) {
743 return ctx->default_verify_callback;
744}
Adam Langley95c29f32014-06-20 12:00:00 -0700745
Adam Langleyfcf25832014-12-18 17:42:32 -0800746void SSL_set_verify(SSL *s, int mode,
747 int (*callback)(int ok, X509_STORE_CTX *ctx)) {
748 s->verify_mode = mode;
749 if (callback != NULL) {
750 s->verify_callback = callback;
751 }
752}
Adam Langley95c29f32014-06-20 12:00:00 -0700753
Adam Langleyfcf25832014-12-18 17:42:32 -0800754void SSL_set_verify_depth(SSL *s, int depth) {
755 X509_VERIFY_PARAM_set_depth(s->param, depth);
756}
Adam Langley95c29f32014-06-20 12:00:00 -0700757
David Benjamin9a41d1b2015-05-16 01:30:09 -0400758int SSL_CTX_get_read_ahead(const SSL_CTX *ctx) { return 0; }
Adam Langley95c29f32014-06-20 12:00:00 -0700759
David Benjamin9a41d1b2015-05-16 01:30:09 -0400760int SSL_get_read_ahead(const SSL *s) { return 0; }
Adam Langley95c29f32014-06-20 12:00:00 -0700761
David Benjamin9a41d1b2015-05-16 01:30:09 -0400762void SSL_CTX_set_read_ahead(SSL_CTX *ctx, int yes) { }
David Benjamin61ecccf2015-05-05 09:44:51 -0400763
David Benjamin9a41d1b2015-05-16 01:30:09 -0400764void SSL_set_read_ahead(SSL *s, int yes) { }
David Benjamin61ecccf2015-05-05 09:44:51 -0400765
Adam Langleyfcf25832014-12-18 17:42:32 -0800766int SSL_pending(const SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800767 return s->method->ssl_pending(s);
768}
Adam Langley95c29f32014-06-20 12:00:00 -0700769
Adam Langleyfcf25832014-12-18 17:42:32 -0800770X509 *SSL_get_peer_certificate(const SSL *s) {
771 X509 *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700772
Adam Langleyfcf25832014-12-18 17:42:32 -0800773 if (s == NULL || s->session == NULL) {
774 r = NULL;
775 } else {
776 r = s->session->peer;
777 }
Adam Langley95c29f32014-06-20 12:00:00 -0700778
Adam Langleyfcf25832014-12-18 17:42:32 -0800779 if (r == NULL) {
780 return NULL;
781 }
Adam Langley95c29f32014-06-20 12:00:00 -0700782
Adam Langleyfcf25832014-12-18 17:42:32 -0800783 return X509_up_ref(r);
784}
785
David Benjamin60da0cd2015-05-03 15:21:28 -0400786STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s) {
787 STACK_OF(X509) *r;
Adam Langleyfcf25832014-12-18 17:42:32 -0800788
789 if (s == NULL || s->session == NULL || s->session->sess_cert == NULL) {
790 r = NULL;
791 } else {
792 r = s->session->sess_cert->cert_chain;
793 }
794
795 /* If we are a client, cert_chain includes the peer's own certificate; if we
796 * are a server, it does not. */
797 return r;
798}
Adam Langley95c29f32014-06-20 12:00:00 -0700799
Adam Langley95c29f32014-06-20 12:00:00 -0700800/* Fix this so it checks all the valid key/cert options */
Adam Langleyfcf25832014-12-18 17:42:32 -0800801int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
802 if (ctx == NULL || ctx->cert == NULL || ctx->cert->key->x509 == NULL) {
803 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key,
804 SSL_R_NO_CERTIFICATE_ASSIGNED);
805 return 0;
806 }
807
808 if (ctx->cert->key->privatekey == NULL) {
809 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key,
810 SSL_R_NO_PRIVATE_KEY_ASSIGNED);
811 return 0;
812 }
813
814 return X509_check_private_key(ctx->cert->key->x509,
815 ctx->cert->key->privatekey);
816}
Adam Langley95c29f32014-06-20 12:00:00 -0700817
818/* Fix this function so that it takes an optional type parameter */
Adam Langleyfcf25832014-12-18 17:42:32 -0800819int SSL_check_private_key(const SSL *ssl) {
820 if (ssl == NULL) {
821 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, ERR_R_PASSED_NULL_PARAMETER);
822 return 0;
823 }
Adam Langley95c29f32014-06-20 12:00:00 -0700824
Adam Langleyfcf25832014-12-18 17:42:32 -0800825 if (ssl->cert == NULL) {
826 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
827 SSL_R_NO_CERTIFICATE_ASSIGNED);
828 return 0;
829 }
Adam Langley95c29f32014-06-20 12:00:00 -0700830
Adam Langleyfcf25832014-12-18 17:42:32 -0800831 if (ssl->cert->key->x509 == NULL) {
832 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
833 SSL_R_NO_CERTIFICATE_ASSIGNED);
834 return 0;
835 }
David Benjamin0b145c22014-11-26 20:10:09 -0500836
Adam Langleyfcf25832014-12-18 17:42:32 -0800837 if (ssl->cert->key->privatekey == NULL) {
838 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
839 SSL_R_NO_PRIVATE_KEY_ASSIGNED);
840 return 0;
841 }
Adam Langley95c29f32014-06-20 12:00:00 -0700842
Adam Langleyfcf25832014-12-18 17:42:32 -0800843 return X509_check_private_key(ssl->cert->key->x509,
844 ssl->cert->key->privatekey);
845}
Adam Langley95c29f32014-06-20 12:00:00 -0700846
Adam Langleyfcf25832014-12-18 17:42:32 -0800847int SSL_accept(SSL *s) {
848 if (s->handshake_func == 0) {
849 /* Not properly initialized yet */
850 SSL_set_accept_state(s);
851 }
David Benjamin0b145c22014-11-26 20:10:09 -0500852
Adam Langleyfcf25832014-12-18 17:42:32 -0800853 if (s->handshake_func != s->method->ssl_accept) {
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +0100854 OPENSSL_PUT_ERROR(SSL, SSL_accept, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800855 return -1;
856 }
Adam Langley95c29f32014-06-20 12:00:00 -0700857
Adam Langleyfcf25832014-12-18 17:42:32 -0800858 return s->handshake_func(s);
859}
Adam Langley95c29f32014-06-20 12:00:00 -0700860
Adam Langleyfcf25832014-12-18 17:42:32 -0800861int SSL_connect(SSL *s) {
862 if (s->handshake_func == 0) {
863 /* Not properly initialized yet */
864 SSL_set_connect_state(s);
865 }
Adam Langley95c29f32014-06-20 12:00:00 -0700866
Adam Langleyfcf25832014-12-18 17:42:32 -0800867 if (s->handshake_func != s->method->ssl_connect) {
868 OPENSSL_PUT_ERROR(SSL, SSL_connect, ERR_R_INTERNAL_ERROR);
869 return -1;
870 }
Adam Langley95c29f32014-06-20 12:00:00 -0700871
Adam Langleyfcf25832014-12-18 17:42:32 -0800872 return s->handshake_func(s);
873}
Adam Langley95c29f32014-06-20 12:00:00 -0700874
Adam Langleyfcf25832014-12-18 17:42:32 -0800875long SSL_get_default_timeout(const SSL *s) {
876 return SSL_DEFAULT_SESSION_TIMEOUT;
877}
Adam Langley95c29f32014-06-20 12:00:00 -0700878
Adam Langleyfcf25832014-12-18 17:42:32 -0800879int SSL_read(SSL *s, void *buf, int num) {
880 if (s->handshake_func == 0) {
881 OPENSSL_PUT_ERROR(SSL, SSL_read, SSL_R_UNINITIALIZED);
882 return -1;
883 }
Adam Langley95c29f32014-06-20 12:00:00 -0700884
Adam Langleyfcf25832014-12-18 17:42:32 -0800885 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
886 s->rwstate = SSL_NOTHING;
887 return 0;
888 }
Adam Langley95c29f32014-06-20 12:00:00 -0700889
Adam Langleyfcf25832014-12-18 17:42:32 -0800890 return s->method->ssl_read(s, buf, num);
891}
Adam Langley95c29f32014-06-20 12:00:00 -0700892
Adam Langleyfcf25832014-12-18 17:42:32 -0800893int SSL_peek(SSL *s, void *buf, int num) {
894 if (s->handshake_func == 0) {
895 OPENSSL_PUT_ERROR(SSL, SSL_peek, SSL_R_UNINITIALIZED);
896 return -1;
897 }
Adam Langley95c29f32014-06-20 12:00:00 -0700898
Adam Langleyfcf25832014-12-18 17:42:32 -0800899 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
900 return 0;
901 }
Adam Langley95c29f32014-06-20 12:00:00 -0700902
Adam Langleyfcf25832014-12-18 17:42:32 -0800903 return s->method->ssl_peek(s, buf, num);
904}
Adam Langley95c29f32014-06-20 12:00:00 -0700905
Adam Langleyfcf25832014-12-18 17:42:32 -0800906int SSL_write(SSL *s, const void *buf, int num) {
907 if (s->handshake_func == 0) {
908 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_UNINITIALIZED);
909 return -1;
910 }
Adam Langley95c29f32014-06-20 12:00:00 -0700911
Adam Langleyfcf25832014-12-18 17:42:32 -0800912 if (s->shutdown & SSL_SENT_SHUTDOWN) {
913 s->rwstate = SSL_NOTHING;
914 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_PROTOCOL_IS_SHUTDOWN);
915 return -1;
916 }
Adam Langley95c29f32014-06-20 12:00:00 -0700917
Adam Langleyfcf25832014-12-18 17:42:32 -0800918 return s->method->ssl_write(s, buf, num);
919}
Adam Langley95c29f32014-06-20 12:00:00 -0700920
Adam Langleyfcf25832014-12-18 17:42:32 -0800921int SSL_shutdown(SSL *s) {
922 /* Note that this function behaves differently from what one might expect.
923 * Return values are 0 for no success (yet), 1 for success; but calling it
924 * once is usually not enough, even if blocking I/O is used (see
925 * ssl3_shutdown). */
Adam Langley95c29f32014-06-20 12:00:00 -0700926
Adam Langleyfcf25832014-12-18 17:42:32 -0800927 if (s->handshake_func == 0) {
928 OPENSSL_PUT_ERROR(SSL, SSL_shutdown, SSL_R_UNINITIALIZED);
929 return -1;
930 }
Adam Langley95c29f32014-06-20 12:00:00 -0700931
Adam Langleyfcf25832014-12-18 17:42:32 -0800932 if (!SSL_in_init(s)) {
933 return s->method->ssl_shutdown(s);
934 }
Adam Langley95c29f32014-06-20 12:00:00 -0700935
Adam Langleyfcf25832014-12-18 17:42:32 -0800936 return 1;
937}
Adam Langley95c29f32014-06-20 12:00:00 -0700938
Adam Langleyfcf25832014-12-18 17:42:32 -0800939int SSL_renegotiate(SSL *s) {
David Benjamin2cdace92015-01-24 13:30:02 -0500940 if (SSL_IS_DTLS(s)) {
941 /* Renegotiation is not supported for DTLS. */
942 OPENSSL_PUT_ERROR(SSL, SSL_renegotiate, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
943 return 0;
944 }
945
Adam Langleyfcf25832014-12-18 17:42:32 -0800946 if (s->renegotiate == 0) {
947 s->renegotiate = 1;
948 }
Adam Langley95c29f32014-06-20 12:00:00 -0700949
Adam Langleyfcf25832014-12-18 17:42:32 -0800950 return s->method->ssl_renegotiate(s);
951}
Adam Langley95c29f32014-06-20 12:00:00 -0700952
Adam Langleyfcf25832014-12-18 17:42:32 -0800953int SSL_renegotiate_pending(SSL *s) {
954 /* becomes true when negotiation is requested; false again once a handshake
955 * has finished */
956 return s->renegotiate != 0;
957}
Adam Langley95c29f32014-06-20 12:00:00 -0700958
David Benjamin61ecccf2015-05-05 09:44:51 -0400959uint32_t SSL_CTX_set_options(SSL_CTX *ctx, uint32_t options) {
960 ctx->options |= options;
961 return ctx->options;
962}
963
964uint32_t SSL_set_options(SSL *ssl, uint32_t options) {
965 ssl->options |= options;
966 return ssl->options;
967}
968
969uint32_t SSL_CTX_clear_options(SSL_CTX *ctx, uint32_t options) {
970 ctx->options &= ~options;
971 return ctx->options;
972}
973
974uint32_t SSL_clear_options(SSL *ssl, uint32_t options) {
975 ssl->options &= ~options;
976 return ssl->options;
977}
978
979uint32_t SSL_CTX_get_options(const SSL_CTX *ctx) { return ctx->options; }
980
981uint32_t SSL_get_options(const SSL *ssl) { return ssl->options; }
982
983uint32_t SSL_CTX_set_mode(SSL_CTX *ctx, uint32_t mode) {
984 ctx->mode |= mode;
985 return ctx->mode;
986}
987
988uint32_t SSL_set_mode(SSL *ssl, uint32_t mode) {
989 ssl->mode |= mode;
990 return ssl->mode;
991}
992
993uint32_t SSL_CTX_clear_mode(SSL_CTX *ctx, uint32_t mode) {
994 ctx->mode &= ~mode;
995 return ctx->mode;
996}
997
998uint32_t SSL_clear_mode(SSL *ssl, uint32_t mode) {
999 ssl->mode &= ~mode;
1000 return ssl->mode;
1001}
1002
1003uint32_t SSL_CTX_get_mode(const SSL_CTX *ctx) { return ctx->mode; }
1004
1005uint32_t SSL_get_mode(const SSL *ssl) { return ssl->mode; }
1006
David Benjamin1d0a1942015-04-26 15:35:35 -04001007size_t SSL_CTX_get_max_cert_list(const SSL_CTX *ctx) {
1008 return ctx->max_cert_list;
1009}
1010
1011void SSL_CTX_set_max_cert_list(SSL_CTX *ctx, size_t max_cert_list) {
1012 if (max_cert_list > kMaxHandshakeSize) {
1013 max_cert_list = kMaxHandshakeSize;
1014 }
1015 ctx->max_cert_list = (uint32_t)max_cert_list;
1016}
1017
1018size_t SSL_get_max_cert_list(const SSL *ssl) {
1019 return ssl->max_cert_list;
1020}
1021
1022void SSL_set_max_cert_list(SSL *ssl, size_t max_cert_list) {
1023 if (max_cert_list > kMaxHandshakeSize) {
1024 max_cert_list = kMaxHandshakeSize;
1025 }
1026 ssl->max_cert_list = (uint32_t)max_cert_list;
1027}
1028
1029void SSL_CTX_set_max_send_fragment(SSL_CTX *ctx, size_t max_send_fragment) {
1030 if (max_send_fragment < 512) {
1031 max_send_fragment = 512;
1032 }
1033 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1034 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1035 }
1036 ctx->max_send_fragment = (uint16_t)max_send_fragment;
1037}
1038
1039void SSL_set_max_send_fragment(SSL *ssl, size_t max_send_fragment) {
1040 if (max_send_fragment < 512) {
1041 max_send_fragment = 512;
1042 }
1043 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1044 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1045 }
1046 ssl->max_send_fragment = (uint16_t)max_send_fragment;
1047}
1048
David Benjamincb9cf792015-05-05 09:46:14 -04001049int SSL_set_mtu(SSL *ssl, unsigned mtu) {
1050 if (!SSL_IS_DTLS(ssl) || mtu < dtls1_min_mtu()) {
1051 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001052 }
David Benjamincb9cf792015-05-05 09:46:14 -04001053 ssl->d1->mtu = mtu;
1054 return 1;
1055}
1056
1057int SSL_get_secure_renegotiation_support(const SSL *ssl) {
1058 return ssl->s3->send_connection_binding;
1059}
1060
1061long SSL_ctrl(SSL *s, int cmd, long larg, void *parg) {
1062 return s->method->ssl_ctrl(s, cmd, larg, parg);
Adam Langleyfcf25832014-12-18 17:42:32 -08001063}
1064
Adam Langleyfcf25832014-12-18 17:42:32 -08001065LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) { return ctx->sessions; }
1066
David Benjamin71f7d3d2015-05-05 09:46:38 -04001067size_t SSL_CTX_sess_number(const SSL_CTX *ctx) {
1068 return lh_SSL_SESSION_num_items(ctx->sessions);
1069}
1070
1071unsigned long SSL_CTX_sess_set_cache_size(SSL_CTX *ctx, unsigned long size) {
1072 unsigned long ret = ctx->session_cache_size;
1073 ctx->session_cache_size = size;
1074 return ret;
1075}
1076
1077unsigned long SSL_CTX_sess_get_cache_size(const SSL_CTX *ctx) {
1078 return ctx->session_cache_size;
1079}
1080
1081int SSL_CTX_set_session_cache_mode(SSL_CTX *ctx, int mode) {
1082 int ret = ctx->session_cache_mode;
1083 ctx->session_cache_mode = mode;
1084 return ret;
1085}
1086
1087int SSL_CTX_get_session_cache_mode(const SSL_CTX *ctx) {
1088 return ctx->session_cache_mode;
1089}
1090
Adam Langleyfcf25832014-12-18 17:42:32 -08001091long SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg) {
David Benjamin71f7d3d2015-05-05 09:46:38 -04001092 return ctx->method->ssl_ctx_ctrl(ctx, cmd, larg, parg);
Adam Langleyfcf25832014-12-18 17:42:32 -08001093}
1094
Adam Langleyfcf25832014-12-18 17:42:32 -08001095int ssl_cipher_id_cmp(const void *in_a, const void *in_b) {
1096 long l;
1097 const SSL_CIPHER *a = in_a;
1098 const SSL_CIPHER *b = in_b;
1099 const long a_id = a->id;
1100 const long b_id = b->id;
1101
1102 l = a_id - b_id;
1103 if (l == 0L) {
1104 return 0;
1105 } else {
1106 return (l > 0) ? 1 : -1;
1107 }
1108}
1109
1110int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **ap, const SSL_CIPHER **bp) {
1111 long l;
1112 const long a_id = (*ap)->id;
1113 const long b_id = (*bp)->id;
1114
1115 l = a_id - b_id;
1116 if (l == 0) {
1117 return 0;
1118 } else {
1119 return (l > 0) ? 1 : -1;
1120 }
1121}
1122
1123/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001124 * preference */
David Benjamin60da0cd2015-05-03 15:21:28 -04001125STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001126 if (s == NULL) {
1127 return NULL;
1128 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001129
Adam Langleyfcf25832014-12-18 17:42:32 -08001130 if (s->cipher_list != NULL) {
1131 return s->cipher_list->ciphers;
1132 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001133
Adam Langleyfcf25832014-12-18 17:42:32 -08001134 if (s->version >= TLS1_1_VERSION && s->ctx != NULL &&
1135 s->ctx->cipher_list_tls11 != NULL) {
1136 return s->ctx->cipher_list_tls11->ciphers;
1137 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001138
Adam Langleyfcf25832014-12-18 17:42:32 -08001139 if (s->ctx != NULL && s->ctx->cipher_list != NULL) {
1140 return s->ctx->cipher_list->ciphers;
1141 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001142
Adam Langleyfcf25832014-12-18 17:42:32 -08001143 return NULL;
1144}
Adam Langley95c29f32014-06-20 12:00:00 -07001145
Adam Langleyfcf25832014-12-18 17:42:32 -08001146/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001147 * algorithm id */
David Benjamin60da0cd2015-05-03 15:21:28 -04001148STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001149 if (s == NULL) {
1150 return NULL;
1151 }
Adam Langley95c29f32014-06-20 12:00:00 -07001152
Adam Langleyfcf25832014-12-18 17:42:32 -08001153 if (s->cipher_list_by_id != NULL) {
1154 return s->cipher_list_by_id;
1155 }
Adam Langley95c29f32014-06-20 12:00:00 -07001156
Adam Langleyfcf25832014-12-18 17:42:32 -08001157 if (s->ctx != NULL && s->ctx->cipher_list_by_id != NULL) {
1158 return s->ctx->cipher_list_by_id;
1159 }
Adam Langley95c29f32014-06-20 12:00:00 -07001160
Adam Langleyfcf25832014-12-18 17:42:32 -08001161 return NULL;
1162}
Adam Langley95c29f32014-06-20 12:00:00 -07001163
Adam Langleyfcf25832014-12-18 17:42:32 -08001164/* The old interface to get the same thing as SSL_get_ciphers() */
1165const char *SSL_get_cipher_list(const SSL *s, int n) {
1166 const SSL_CIPHER *c;
David Benjamin60da0cd2015-05-03 15:21:28 -04001167 STACK_OF(SSL_CIPHER) *sk;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001168
Adam Langleyfcf25832014-12-18 17:42:32 -08001169 if (s == NULL) {
1170 return NULL;
1171 }
Adam Langley95c29f32014-06-20 12:00:00 -07001172
Adam Langleyfcf25832014-12-18 17:42:32 -08001173 sk = SSL_get_ciphers(s);
1174 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
1175 return NULL;
1176 }
Adam Langley95c29f32014-06-20 12:00:00 -07001177
Adam Langleyfcf25832014-12-18 17:42:32 -08001178 c = sk_SSL_CIPHER_value(sk, n);
1179 if (c == NULL) {
1180 return NULL;
1181 }
Adam Langley95c29f32014-06-20 12:00:00 -07001182
Adam Langleyfcf25832014-12-18 17:42:32 -08001183 return c->name;
1184}
David Benjamin5491e3f2014-09-29 19:33:09 -04001185
Adam Langleyfcf25832014-12-18 17:42:32 -08001186/* specify the ciphers to be used by default by the SSL_CTX */
1187int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
1188 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001189
Adam Langleyfcf25832014-12-18 17:42:32 -08001190 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001191 &ctx->cipher_list_by_id, str);
Adam Langleyfcf25832014-12-18 17:42:32 -08001192 /* ssl_create_cipher_list may return an empty stack if it was unable to find
1193 * a cipher matching the given rule string (for example if the rule string
1194 * specifies a cipher which has been disabled). This is not an error as far
1195 * as ssl_create_cipher_list is concerned, and hence ctx->cipher_list and
1196 * ctx->cipher_list_by_id has been updated. */
1197 if (sk == NULL) {
1198 return 0;
1199 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1200 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1201 return 0;
1202 }
Adam Langley95c29f32014-06-20 12:00:00 -07001203
Adam Langleyfcf25832014-12-18 17:42:32 -08001204 return 1;
1205}
David Benjamin39482a12014-07-20 13:30:15 -04001206
Adam Langleyfcf25832014-12-18 17:42:32 -08001207int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str) {
1208 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001209
David Benjamin71f07942015-04-08 02:36:59 -04001210 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str);
Adam Langleyfcf25832014-12-18 17:42:32 -08001211 if (sk == NULL) {
1212 return 0;
1213 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1214 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list_tls11,
1215 SSL_R_NO_CIPHER_MATCH);
1216 return 0;
1217 }
David Benjamin9f2c0d72014-10-21 22:00:19 -04001218
Adam Langleyfcf25832014-12-18 17:42:32 -08001219 return 1;
1220}
Adam Langley95c29f32014-06-20 12:00:00 -07001221
Adam Langleyfcf25832014-12-18 17:42:32 -08001222/* specify the ciphers to be used by the SSL */
1223int SSL_set_cipher_list(SSL *s, const char *str) {
1224 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001225
Adam Langleyfcf25832014-12-18 17:42:32 -08001226 sk = ssl_create_cipher_list(s->ctx->method, &s->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001227 &s->cipher_list_by_id, str);
David Benjamin39482a12014-07-20 13:30:15 -04001228
Adam Langleyfcf25832014-12-18 17:42:32 -08001229 /* see comment in SSL_CTX_set_cipher_list */
1230 if (sk == NULL) {
1231 return 0;
1232 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1233 OPENSSL_PUT_ERROR(SSL, SSL_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1234 return 0;
1235 }
David Benjamin39482a12014-07-20 13:30:15 -04001236
Adam Langleyfcf25832014-12-18 17:42:32 -08001237 return 1;
1238}
Adam Langley95c29f32014-06-20 12:00:00 -07001239
Adam Langleyfcf25832014-12-18 17:42:32 -08001240int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, uint8_t *p) {
1241 size_t i;
1242 const SSL_CIPHER *c;
1243 CERT *ct = s->cert;
1244 uint8_t *q;
1245 /* Set disabled masks for this session */
1246 ssl_set_client_disabled(s);
Adam Langley29707792014-06-20 12:00:00 -07001247
Adam Langleyfcf25832014-12-18 17:42:32 -08001248 if (sk == NULL) {
1249 return 0;
1250 }
1251 q = p;
Adam Langley95c29f32014-06-20 12:00:00 -07001252
Adam Langleyfcf25832014-12-18 17:42:32 -08001253 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
1254 c = sk_SSL_CIPHER_value(sk, i);
1255 /* Skip disabled ciphers */
1256 if (c->algorithm_ssl & ct->mask_ssl ||
1257 c->algorithm_mkey & ct->mask_k ||
1258 c->algorithm_auth & ct->mask_a) {
1259 continue;
1260 }
1261 s2n(ssl3_get_cipher_value(c), p);
1262 }
1263
1264 /* If all ciphers were disabled, return the error to the caller. */
1265 if (p == q) {
1266 return 0;
1267 }
1268
1269 /* Add SCSVs. */
1270 if (!s->renegotiate) {
1271 s2n(SSL3_CK_SCSV & 0xffff, p);
1272 }
1273
Adam Langley5f0efe02015-02-20 13:03:16 -08001274 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001275 s2n(SSL3_CK_FALLBACK_SCSV & 0xffff, p);
1276 }
1277
1278 return p - q;
1279}
1280
1281STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs) {
1282 CBS cipher_suites = *cbs;
1283 const SSL_CIPHER *c;
David Benjamin60da0cd2015-05-03 15:21:28 -04001284 STACK_OF(SSL_CIPHER) *sk;
Adam Langleyfcf25832014-12-18 17:42:32 -08001285
1286 if (s->s3) {
1287 s->s3->send_connection_binding = 0;
1288 }
1289
1290 if (CBS_len(&cipher_suites) % 2 != 0) {
1291 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1292 SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
1293 return NULL;
1294 }
1295
1296 sk = sk_SSL_CIPHER_new_null();
1297 if (sk == NULL) {
1298 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1299 goto err;
1300 }
1301
Adam Langleyfcf25832014-12-18 17:42:32 -08001302 while (CBS_len(&cipher_suites) > 0) {
1303 uint16_t cipher_suite;
1304
1305 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
1306 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_INTERNAL_ERROR);
1307 goto err;
1308 }
1309
1310 /* Check for SCSV. */
1311 if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff)) {
1312 /* SCSV is fatal if renegotiating. */
1313 if (s->renegotiate) {
1314 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1315 SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1316 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1317 goto err;
1318 }
1319 s->s3->send_connection_binding = 1;
1320 continue;
1321 }
1322
1323 /* Check for FALLBACK_SCSV. */
1324 if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff)) {
1325 uint16_t max_version = ssl3_get_max_server_version(s);
1326 if (SSL_IS_DTLS(s) ? (uint16_t)s->version > max_version
1327 : (uint16_t)s->version < max_version) {
1328 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1329 SSL_R_INAPPROPRIATE_FALLBACK);
1330 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
1331 goto err;
1332 }
1333 continue;
1334 }
1335
1336 c = ssl3_get_cipher_by_value(cipher_suite);
1337 if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
1338 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1339 goto err;
1340 }
1341 }
1342
1343 return sk;
David Benjamin9f2c0d72014-10-21 22:00:19 -04001344
Adam Langley95c29f32014-06-20 12:00:00 -07001345err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001346 sk_SSL_CIPHER_free(sk);
Adam Langleyfcf25832014-12-18 17:42:32 -08001347 return NULL;
1348}
Adam Langley95c29f32014-06-20 12:00:00 -07001349
1350
Adam Langleyfcf25832014-12-18 17:42:32 -08001351/* return a servername extension value if provided in Client Hello, or NULL. So
1352 * far, only host_name types are defined (RFC 3546). */
1353const char *SSL_get_servername(const SSL *s, const int type) {
1354 if (type != TLSEXT_NAMETYPE_host_name) {
1355 return NULL;
1356 }
Adam Langley95c29f32014-06-20 12:00:00 -07001357
Adam Langleyfcf25832014-12-18 17:42:32 -08001358 return s->session && !s->tlsext_hostname ? s->session->tlsext_hostname
1359 : s->tlsext_hostname;
1360}
Adam Langley95c29f32014-06-20 12:00:00 -07001361
Adam Langleyfcf25832014-12-18 17:42:32 -08001362int SSL_get_servername_type(const SSL *s) {
1363 if (s->session &&
1364 (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname)) {
1365 return TLSEXT_NAMETYPE_host_name;
1366 }
Adam Langley95c29f32014-06-20 12:00:00 -07001367
Adam Langleyfcf25832014-12-18 17:42:32 -08001368 return -1;
1369}
Adam Langley95c29f32014-06-20 12:00:00 -07001370
Adam Langleyfcf25832014-12-18 17:42:32 -08001371void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
1372 ctx->signed_cert_timestamps_enabled = 1;
1373}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001374
Adam Langleyfcf25832014-12-18 17:42:32 -08001375int SSL_enable_signed_cert_timestamps(SSL *ssl) {
1376 ssl->signed_cert_timestamps_enabled = 1;
1377 return 1;
1378}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001379
Adam Langleyfcf25832014-12-18 17:42:32 -08001380void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
1381 ctx->ocsp_stapling_enabled = 1;
1382}
David Benjamin6c7aed02014-08-27 16:42:38 -04001383
Adam Langleyfcf25832014-12-18 17:42:32 -08001384int SSL_enable_ocsp_stapling(SSL *ssl) {
1385 ssl->ocsp_stapling_enabled = 1;
1386 return 1;
1387}
David Benjamin6c7aed02014-08-27 16:42:38 -04001388
Adam Langleyfcf25832014-12-18 17:42:32 -08001389void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
1390 size_t *out_len) {
1391 SSL_SESSION *session = ssl->session;
Adam Langley3cb50e02014-08-26 14:00:31 -07001392
Adam Langleyfcf25832014-12-18 17:42:32 -08001393 *out_len = 0;
1394 *out = NULL;
1395 if (ssl->server || !session || !session->tlsext_signed_cert_timestamp_list) {
1396 return;
1397 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001398
Adam Langleyfcf25832014-12-18 17:42:32 -08001399 *out = session->tlsext_signed_cert_timestamp_list;
1400 *out_len = session->tlsext_signed_cert_timestamp_list_length;
1401}
David Benjamin6c7aed02014-08-27 16:42:38 -04001402
Adam Langleyfcf25832014-12-18 17:42:32 -08001403void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
1404 size_t *out_len) {
1405 SSL_SESSION *session = ssl->session;
1406
1407 *out_len = 0;
1408 *out = NULL;
1409 if (ssl->server || !session || !session->ocsp_response) {
1410 return;
1411 }
1412 *out = session->ocsp_response;
1413 *out_len = session->ocsp_response_length;
1414}
David Benjamin6c7aed02014-08-27 16:42:38 -04001415
Adam Langley95c29f32014-06-20 12:00:00 -07001416/* SSL_select_next_proto implements the standard protocol selection. It is
1417 * expected that this function is called from the callback set by
1418 * SSL_CTX_set_next_proto_select_cb.
1419 *
1420 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1421 * strings. The length byte itself is not included in the length. A byte
1422 * string of length 0 is invalid. No byte string may be truncated.
1423 *
1424 * The current, but experimental algorithm for selecting the protocol is:
1425 *
1426 * 1) If the server doesn't support NPN then this is indicated to the
1427 * callback. In this case, the client application has to abort the connection
1428 * or have a default application level protocol.
1429 *
1430 * 2) If the server supports NPN, but advertises an empty list then the
1431 * client selects the first protcol in its list, but indicates via the
1432 * API that this fallback case was enacted.
1433 *
1434 * 3) Otherwise, the client finds the first protocol in the server's list
1435 * that it supports and selects this protocol. This is because it's
1436 * assumed that the server has better information about which protocol
1437 * a client should use.
1438 *
1439 * 4) If the client doesn't support any of the server's advertised
1440 * protocols, then this is treated the same as case 2.
1441 *
1442 * It returns either
1443 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1444 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1445 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001446int SSL_select_next_proto(uint8_t **out, uint8_t *outlen, const uint8_t *server,
1447 unsigned int server_len, const uint8_t *client,
1448 unsigned int client_len) {
1449 unsigned int i, j;
1450 const uint8_t *result;
1451 int status = OPENSSL_NPN_UNSUPPORTED;
Adam Langley95c29f32014-06-20 12:00:00 -07001452
Adam Langleyfcf25832014-12-18 17:42:32 -08001453 /* For each protocol in server preference order, see if we support it. */
1454 for (i = 0; i < server_len;) {
1455 for (j = 0; j < client_len;) {
1456 if (server[i] == client[j] &&
1457 memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
1458 /* We found a match */
1459 result = &server[i];
1460 status = OPENSSL_NPN_NEGOTIATED;
1461 goto found;
1462 }
1463 j += client[j];
1464 j++;
1465 }
1466 i += server[i];
1467 i++;
1468 }
Adam Langley95c29f32014-06-20 12:00:00 -07001469
Adam Langleyfcf25832014-12-18 17:42:32 -08001470 /* There's no overlap between our protocols and the server's list. */
1471 result = client;
1472 status = OPENSSL_NPN_NO_OVERLAP;
Adam Langley95c29f32014-06-20 12:00:00 -07001473
Adam Langleyfcf25832014-12-18 17:42:32 -08001474found:
1475 *out = (uint8_t *)result + 1;
1476 *outlen = result[0];
1477 return status;
1478}
Adam Langley95c29f32014-06-20 12:00:00 -07001479
Adam Langley95c29f32014-06-20 12:00:00 -07001480/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
1481 * requested protocol for this connection and returns 0. If the client didn't
1482 * request any protocol, then *data is set to NULL.
1483 *
1484 * Note that the client can request any protocol it chooses. The value returned
1485 * from this function need not be a member of the list of supported protocols
Adam Langleyfcf25832014-12-18 17:42:32 -08001486 * provided by the callback. */
1487void SSL_get0_next_proto_negotiated(const SSL *s, const uint8_t **data,
1488 unsigned *len) {
1489 *data = s->next_proto_negotiated;
1490 if (!*data) {
1491 *len = 0;
1492 } else {
1493 *len = s->next_proto_negotiated_len;
1494 }
Adam Langley95c29f32014-06-20 12:00:00 -07001495}
1496
1497/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
1498 * TLS server needs a list of supported protocols for Next Protocol
1499 * Negotiation. The returned list must be in wire format. The list is returned
1500 * by setting |out| to point to it and |outlen| to its length. This memory will
1501 * not be modified, but one should assume that the SSL* keeps a reference to
1502 * it.
1503 *
Adam Langleyfcf25832014-12-18 17:42:32 -08001504 * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise.
1505 * Otherwise, no such extension will be included in the ServerHello. */
1506void SSL_CTX_set_next_protos_advertised_cb(
1507 SSL_CTX *ctx,
1508 int (*cb)(SSL *ssl, const uint8_t **out, unsigned int *outlen, void *arg),
1509 void *arg) {
1510 ctx->next_protos_advertised_cb = cb;
1511 ctx->next_protos_advertised_cb_arg = arg;
1512}
Adam Langley95c29f32014-06-20 12:00:00 -07001513
1514/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1515 * client needs to select a protocol from the server's provided list. |out|
1516 * must be set to point to the selected protocol (which may be within |in|).
1517 * The length of the protocol name must be written into |outlen|. The server's
1518 * advertised protocols are provided in |in| and |inlen|. The callback can
1519 * assume that |in| is syntactically valid.
1520 *
1521 * The client must select a protocol. It is fatal to the connection if this
1522 * callback returns a value other than SSL_TLSEXT_ERR_OK.
1523 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001524void SSL_CTX_set_next_proto_select_cb(
1525 SSL_CTX *ctx, int (*cb)(SSL *s, uint8_t **out, uint8_t *outlen,
1526 const uint8_t *in, unsigned int inlen, void *arg),
1527 void *arg) {
1528 ctx->next_proto_select_cb = cb;
1529 ctx->next_proto_select_cb_arg = arg;
1530}
Adam Langley95c29f32014-06-20 12:00:00 -07001531
Adam Langleyfcf25832014-12-18 17:42:32 -08001532int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
1533 unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001534 OPENSSL_free(ctx->alpn_client_proto_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001535 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1536 if (!ctx->alpn_client_proto_list) {
1537 return 1;
1538 }
1539 ctx->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001540
Adam Langleyfcf25832014-12-18 17:42:32 -08001541 return 0;
1542}
Adam Langley95c29f32014-06-20 12:00:00 -07001543
Adam Langleyfcf25832014-12-18 17:42:32 -08001544int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001545 OPENSSL_free(ssl->alpn_client_proto_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001546 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1547 if (!ssl->alpn_client_proto_list) {
1548 return 1;
1549 }
1550 ssl->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001551
Adam Langleyfcf25832014-12-18 17:42:32 -08001552 return 0;
1553}
Adam Langley95c29f32014-06-20 12:00:00 -07001554
1555/* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
1556 * during ClientHello processing in order to select an ALPN protocol from the
1557 * client's list of offered protocols. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001558void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
1559 int (*cb)(SSL *ssl, const uint8_t **out,
1560 uint8_t *outlen, const uint8_t *in,
1561 unsigned int inlen, void *arg),
1562 void *arg) {
1563 ctx->alpn_select_cb = cb;
1564 ctx->alpn_select_cb_arg = arg;
1565}
Adam Langley95c29f32014-06-20 12:00:00 -07001566
1567/* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
1568 * On return it sets |*data| to point to |*len| bytes of protocol name (not
1569 * including the leading length-prefix byte). If the server didn't respond with
1570 * a negotiated protocol then |*len| will be zero. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001571void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **data,
1572 unsigned *len) {
1573 *data = NULL;
1574 if (ssl->s3) {
1575 *data = ssl->s3->alpn_selected;
1576 }
1577 if (*data == NULL) {
1578 *len = 0;
1579 } else {
1580 *len = ssl->s3->alpn_selected_len;
1581 }
1582}
Adam Langley95c29f32014-06-20 12:00:00 -07001583
David Benjamincfd248b2015-04-03 11:02:24 -04001584int SSL_export_keying_material(SSL *s, uint8_t *out, size_t out_len,
1585 const char *label, size_t label_len,
1586 const uint8_t *context, size_t context_len,
1587 int use_context) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001588 if (s->version < TLS1_VERSION) {
David Benjaminc565ebb2015-04-03 04:06:36 -04001589 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001590 }
Adam Langley95c29f32014-06-20 12:00:00 -07001591
David Benjamincfd248b2015-04-03 11:02:24 -04001592 return s->enc_method->export_keying_material(
1593 s, out, out_len, label, label_len, context, context_len, use_context);
Adam Langleyfcf25832014-12-18 17:42:32 -08001594}
Adam Langley95c29f32014-06-20 12:00:00 -07001595
Adam Langleyfcf25832014-12-18 17:42:32 -08001596static uint32_t ssl_session_hash(const SSL_SESSION *a) {
1597 uint32_t hash =
1598 ((uint32_t)a->session_id[0]) ||
1599 ((uint32_t)a->session_id[1] << 8) ||
1600 ((uint32_t)a->session_id[2] << 16) ||
1601 ((uint32_t)a->session_id[3] << 24);
Adam Langley95c29f32014-06-20 12:00:00 -07001602
Adam Langleyfcf25832014-12-18 17:42:32 -08001603 return hash;
1604}
Adam Langley95c29f32014-06-20 12:00:00 -07001605
1606/* NB: If this function (or indeed the hash function which uses a sort of
1607 * coarser function than this one) is changed, ensure
1608 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1609 * able to construct an SSL_SESSION that will collide with any existing session
1610 * with a matching session ID. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001611static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
1612 if (a->ssl_version != b->ssl_version) {
1613 return 1;
1614 }
Adam Langley95c29f32014-06-20 12:00:00 -07001615
Adam Langleyfcf25832014-12-18 17:42:32 -08001616 if (a->session_id_length != b->session_id_length) {
1617 return 1;
1618 }
Adam Langley95c29f32014-06-20 12:00:00 -07001619
Adam Langleyfcf25832014-12-18 17:42:32 -08001620 return memcmp(a->session_id, b->session_id, a->session_id_length);
1621}
Adam Langley95c29f32014-06-20 12:00:00 -07001622
David Benjamin4831c332015-05-16 11:43:13 -04001623SSL_CTX *SSL_CTX_new(const SSL_METHOD *method) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001624 SSL_CTX *ret = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001625
David Benjamin4831c332015-05-16 11:43:13 -04001626 if (method == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001627 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_NULL_SSL_METHOD_PASSED);
1628 return NULL;
1629 }
Adam Langley95c29f32014-06-20 12:00:00 -07001630
Adam Langleyfcf25832014-12-18 17:42:32 -08001631 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
1632 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
1633 goto err;
1634 }
Adam Langley95c29f32014-06-20 12:00:00 -07001635
Adam Langleyfcf25832014-12-18 17:42:32 -08001636 ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1637 if (ret == NULL) {
1638 goto err;
1639 }
Adam Langley95c29f32014-06-20 12:00:00 -07001640
Adam Langleyfcf25832014-12-18 17:42:32 -08001641 memset(ret, 0, sizeof(SSL_CTX));
Adam Langley95c29f32014-06-20 12:00:00 -07001642
David Benjamin4831c332015-05-16 11:43:13 -04001643 ret->method = method->method;
Adam Langley95c29f32014-06-20 12:00:00 -07001644
Adam Langley4bdb6e42015-05-15 15:29:21 -07001645 CRYPTO_MUTEX_init(&ret->lock);
1646
Adam Langleyfcf25832014-12-18 17:42:32 -08001647 ret->cert_store = NULL;
1648 ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
1649 ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1650 ret->session_cache_head = NULL;
1651 ret->session_cache_tail = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001652
Adam Langleyfcf25832014-12-18 17:42:32 -08001653 /* We take the system default */
1654 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001655
Adam Langleyfcf25832014-12-18 17:42:32 -08001656 ret->new_session_cb = 0;
1657 ret->remove_session_cb = 0;
1658 ret->get_session_cb = 0;
1659 ret->generate_session_id = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001660
Adam Langleyfcf25832014-12-18 17:42:32 -08001661 ret->references = 1;
1662 ret->quiet_shutdown = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001663
Adam Langleyfcf25832014-12-18 17:42:32 -08001664 ret->info_callback = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001665
Adam Langleyfcf25832014-12-18 17:42:32 -08001666 ret->app_verify_callback = 0;
1667 ret->app_verify_arg = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001668
Adam Langleyfcf25832014-12-18 17:42:32 -08001669 ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
Adam Langleyfcf25832014-12-18 17:42:32 -08001670 ret->msg_callback = 0;
1671 ret->msg_callback_arg = NULL;
1672 ret->verify_mode = SSL_VERIFY_NONE;
1673 ret->sid_ctx_length = 0;
1674 ret->default_verify_callback = NULL;
1675 ret->cert = ssl_cert_new();
1676 if (ret->cert == NULL) {
1677 goto err;
1678 }
Adam Langley95c29f32014-06-20 12:00:00 -07001679
Adam Langleyfcf25832014-12-18 17:42:32 -08001680 ret->default_passwd_callback = 0;
1681 ret->default_passwd_callback_userdata = NULL;
1682 ret->client_cert_cb = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001683
Adam Langleyfcf25832014-12-18 17:42:32 -08001684 ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
1685 if (ret->sessions == NULL) {
1686 goto err;
1687 }
1688 ret->cert_store = X509_STORE_new();
1689 if (ret->cert_store == NULL) {
1690 goto err;
1691 }
Adam Langley95c29f32014-06-20 12:00:00 -07001692
Adam Langleyfcf25832014-12-18 17:42:32 -08001693 ssl_create_cipher_list(ret->method, &ret->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001694 &ret->cipher_list_by_id, SSL_DEFAULT_CIPHER_LIST);
Adam Langleyfcf25832014-12-18 17:42:32 -08001695 if (ret->cipher_list == NULL ||
1696 sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0) {
1697 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_LIBRARY_HAS_NO_CIPHERS);
1698 goto err2;
1699 }
Adam Langley95c29f32014-06-20 12:00:00 -07001700
Adam Langleyfcf25832014-12-18 17:42:32 -08001701 ret->param = X509_VERIFY_PARAM_new();
1702 if (!ret->param) {
1703 goto err;
1704 }
Adam Langley95c29f32014-06-20 12:00:00 -07001705
Adam Langleyfcf25832014-12-18 17:42:32 -08001706 ret->client_CA = sk_X509_NAME_new_null();
1707 if (ret->client_CA == NULL) {
1708 goto err;
1709 }
Adam Langley95c29f32014-06-20 12:00:00 -07001710
David Benjamin9f33fc62015-04-15 17:29:53 -04001711 CRYPTO_new_ex_data(&g_ex_data_class_ssl_ctx, ret, &ret->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001712
Adam Langleyfcf25832014-12-18 17:42:32 -08001713 ret->extra_certs = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001714
Adam Langleyfcf25832014-12-18 17:42:32 -08001715 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
David Benjamin422d3a42014-08-20 11:09:03 -04001716
Adam Langleyfcf25832014-12-18 17:42:32 -08001717 ret->tlsext_servername_callback = 0;
1718 ret->tlsext_servername_arg = NULL;
1719 /* Setup RFC4507 ticket keys */
1720 if (!RAND_bytes(ret->tlsext_tick_key_name, 16) ||
1721 !RAND_bytes(ret->tlsext_tick_hmac_key, 16) ||
1722 !RAND_bytes(ret->tlsext_tick_aes_key, 16)) {
1723 ret->options |= SSL_OP_NO_TICKET;
1724 }
Adam Langley95c29f32014-06-20 12:00:00 -07001725
Adam Langleyfcf25832014-12-18 17:42:32 -08001726 ret->next_protos_advertised_cb = 0;
1727 ret->next_proto_select_cb = 0;
1728 ret->psk_identity_hint = NULL;
1729 ret->psk_client_callback = NULL;
1730 ret->psk_server_callback = NULL;
1731
1732 /* Default is to connect to non-RI servers. When RI is more widely deployed
1733 * might change this. */
1734 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
1735
1736 /* Lock the SSL_CTX to the specified version, for compatibility with legacy
1737 * uses of SSL_METHOD. */
David Benjamin4831c332015-05-16 11:43:13 -04001738 if (method->version != 0) {
1739 SSL_CTX_set_max_version(ret, method->version);
1740 SSL_CTX_set_min_version(ret, method->version);
Adam Langleyfcf25832014-12-18 17:42:32 -08001741 }
1742
1743 return ret;
1744
Adam Langley95c29f32014-06-20 12:00:00 -07001745err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001746 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -07001747err2:
David Benjamin2755a3e2015-04-22 16:17:58 -04001748 SSL_CTX_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -08001749 return NULL;
1750}
Adam Langley95c29f32014-06-20 12:00:00 -07001751
David Benjamin4fcc2e22015-04-22 12:58:16 -04001752void SSL_CTX_free(SSL_CTX *ctx) {
1753 if (ctx == NULL ||
Adam Langley0b5e3902015-05-15 13:08:38 -07001754 !CRYPTO_refcount_dec_and_test_zero(&ctx->references)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001755 return;
1756 }
Adam Langley95c29f32014-06-20 12:00:00 -07001757
David Benjamin2755a3e2015-04-22 16:17:58 -04001758 X509_VERIFY_PARAM_free(ctx->param);
Adam Langley95c29f32014-06-20 12:00:00 -07001759
Adam Langleyfcf25832014-12-18 17:42:32 -08001760 /* Free internal session cache. However: the remove_cb() may reference the
1761 * ex_data of SSL_CTX, thus the ex_data store can only be removed after the
1762 * sessions were flushed. As the ex_data handling routines might also touch
1763 * the session cache, the most secure solution seems to be: empty (flush) the
1764 * cache, then free ex_data, then finally free the cache. (See ticket
1765 * [openssl.org #212].) */
David Benjamin2755a3e2015-04-22 16:17:58 -04001766 SSL_CTX_flush_sessions(ctx, 0);
Adam Langley95c29f32014-06-20 12:00:00 -07001767
David Benjamin4fcc2e22015-04-22 12:58:16 -04001768 CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, ctx, &ctx->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001769
Adam Langley4bdb6e42015-05-15 15:29:21 -07001770 CRYPTO_MUTEX_cleanup(&ctx->lock);
David Benjamin2755a3e2015-04-22 16:17:58 -04001771 lh_SSL_SESSION_free(ctx->sessions);
1772 X509_STORE_free(ctx->cert_store);
1773 ssl_cipher_preference_list_free(ctx->cipher_list);
1774 sk_SSL_CIPHER_free(ctx->cipher_list_by_id);
1775 ssl_cipher_preference_list_free(ctx->cipher_list_tls11);
1776 ssl_cert_free(ctx->cert);
1777 sk_X509_NAME_pop_free(ctx->client_CA, X509_NAME_free);
1778 sk_X509_pop_free(ctx->extra_certs, X509_free);
1779 sk_SRTP_PROTECTION_PROFILE_free(ctx->srtp_profiles);
1780 OPENSSL_free(ctx->psk_identity_hint);
1781 OPENSSL_free(ctx->tlsext_ecpointformatlist);
1782 OPENSSL_free(ctx->tlsext_ellipticcurvelist);
1783 OPENSSL_free(ctx->alpn_client_proto_list);
1784 EVP_PKEY_free(ctx->tlsext_channel_id_private);
1785 BIO_free(ctx->keylog_bio);
Adam Langley95c29f32014-06-20 12:00:00 -07001786
David Benjamin4fcc2e22015-04-22 12:58:16 -04001787 OPENSSL_free(ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08001788}
Adam Langley95c29f32014-06-20 12:00:00 -07001789
Adam Langleyfcf25832014-12-18 17:42:32 -08001790void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) {
1791 ctx->default_passwd_callback = cb;
1792}
Adam Langley95c29f32014-06-20 12:00:00 -07001793
Adam Langleyfcf25832014-12-18 17:42:32 -08001794void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u) {
1795 ctx->default_passwd_callback_userdata = u;
1796}
Adam Langley95c29f32014-06-20 12:00:00 -07001797
Adam Langleyfcf25832014-12-18 17:42:32 -08001798void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
1799 int (*cb)(X509_STORE_CTX *, void *),
1800 void *arg) {
1801 ctx->app_verify_callback = cb;
1802 ctx->app_verify_arg = arg;
1803}
Adam Langley95c29f32014-06-20 12:00:00 -07001804
Adam Langleyfcf25832014-12-18 17:42:32 -08001805void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
1806 int (*cb)(int, X509_STORE_CTX *)) {
1807 ctx->verify_mode = mode;
1808 ctx->default_verify_callback = cb;
1809}
Adam Langley95c29f32014-06-20 12:00:00 -07001810
Adam Langleyfcf25832014-12-18 17:42:32 -08001811void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
1812 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
1813}
Adam Langley1258b6a2014-06-20 12:00:00 -07001814
Adam Langleyfcf25832014-12-18 17:42:32 -08001815void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb)(SSL *ssl, void *arg),
1816 void *arg) {
1817 ssl_cert_set_cert_cb(c->cert, cb, arg);
1818}
David Benjamin859ec3c2014-09-02 16:29:36 -04001819
Adam Langleyfcf25832014-12-18 17:42:32 -08001820void SSL_set_cert_cb(SSL *s, int (*cb)(SSL *ssl, void *arg), void *arg) {
1821 ssl_cert_set_cert_cb(s->cert, cb, arg);
1822}
Adam Langley95c29f32014-06-20 12:00:00 -07001823
Adam Langleyfcf25832014-12-18 17:42:32 -08001824static int ssl_has_key(SSL *s, size_t idx) {
1825 CERT_PKEY *cpk = &s->cert->pkeys[idx];
1826 return cpk->x509 && cpk->privatekey;
1827}
David Benjamin033e5f42014-11-13 18:47:41 -05001828
David Benjamin107db582015-04-08 00:41:59 -04001829void ssl_get_compatible_server_ciphers(SSL *s, uint32_t *out_mask_k,
1830 uint32_t *out_mask_a) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001831 CERT *c = s->cert;
1832 int rsa_enc, rsa_sign, dh_tmp;
David Benjamin107db582015-04-08 00:41:59 -04001833 uint32_t mask_k, mask_a;
Adam Langleyfcf25832014-12-18 17:42:32 -08001834 int have_ecc_cert, ecdsa_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001835 X509 *x;
Adam Langley95c29f32014-06-20 12:00:00 -07001836
Adam Langleyfcf25832014-12-18 17:42:32 -08001837 if (c == NULL) {
1838 /* TODO(davidben): Is this codepath possible? */
1839 *out_mask_k = 0;
1840 *out_mask_a = 0;
1841 return;
1842 }
Adam Langley95c29f32014-06-20 12:00:00 -07001843
Adam Langleyfcf25832014-12-18 17:42:32 -08001844 dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001845
Adam Langleyfcf25832014-12-18 17:42:32 -08001846 rsa_enc = ssl_has_key(s, SSL_PKEY_RSA_ENC);
1847 rsa_sign = ssl_has_key(s, SSL_PKEY_RSA_SIGN);
1848 have_ecc_cert = ssl_has_key(s, SSL_PKEY_ECC);
1849 mask_k = 0;
1850 mask_a = 0;
David Benjaminf31e6812014-11-13 18:05:55 -05001851
Adam Langleyfcf25832014-12-18 17:42:32 -08001852 if (rsa_enc) {
1853 mask_k |= SSL_kRSA;
1854 }
1855 if (dh_tmp) {
David Benjamin7061e282015-03-19 11:10:48 -04001856 mask_k |= SSL_kDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001857 }
1858 if (rsa_enc || rsa_sign) {
1859 mask_a |= SSL_aRSA;
1860 }
Adam Langley95c29f32014-06-20 12:00:00 -07001861
Adam Langleyfcf25832014-12-18 17:42:32 -08001862 /* An ECC certificate may be usable for ECDSA cipher suites depending on the
1863 * key usage extension and on the client's curve preferences. */
1864 if (have_ecc_cert) {
1865 x = c->pkeys[SSL_PKEY_ECC].x509;
1866 /* This call populates extension flags (ex_flags). */
1867 X509_check_purpose(x, -1, 0);
1868 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE)
1869 ? (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)
1870 : 1;
1871 if (!tls1_check_ec_cert(s, x)) {
1872 ecdsa_ok = 0;
1873 }
1874 if (ecdsa_ok) {
1875 mask_a |= SSL_aECDSA;
1876 }
1877 }
Adam Langley95c29f32014-06-20 12:00:00 -07001878
Adam Langleyfcf25832014-12-18 17:42:32 -08001879 /* If we are considering an ECC cipher suite that uses an ephemeral EC
1880 * key, check it. */
David Benjamindd978782015-04-24 15:20:13 -04001881 if (tls1_check_ec_tmp_key(s)) {
David Benjamin7061e282015-03-19 11:10:48 -04001882 mask_k |= SSL_kECDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001883 }
Adam Langley95c29f32014-06-20 12:00:00 -07001884
Adam Langleyfcf25832014-12-18 17:42:32 -08001885 /* PSK requires a server callback. */
1886 if (s->psk_server_callback != NULL) {
1887 mask_k |= SSL_kPSK;
1888 mask_a |= SSL_aPSK;
1889 }
Adam Langley95c29f32014-06-20 12:00:00 -07001890
Adam Langleyfcf25832014-12-18 17:42:32 -08001891 *out_mask_k = mask_k;
1892 *out_mask_a = mask_a;
1893}
Adam Langley95c29f32014-06-20 12:00:00 -07001894
1895/* This handy macro borrowed from crypto/x509v3/v3_purp.c */
1896#define ku_reject(x, usage) \
Adam Langleyfcf25832014-12-18 17:42:32 -08001897 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
Adam Langley95c29f32014-06-20 12:00:00 -07001898
Adam Langleyfcf25832014-12-18 17:42:32 -08001899int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001900 const SSL_CIPHER *cs = s->s3->tmp.new_cipher;
David Benjamin3fa27772015-04-17 22:32:19 -04001901 uint32_t alg_a = cs->algorithm_auth;
1902 int signature_nid = 0, md_nid = 0, pk_nid = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001903
Adam Langleyfcf25832014-12-18 17:42:32 -08001904 /* This call populates the ex_flags field correctly */
1905 X509_check_purpose(x, -1, 0);
1906 if (x->sig_alg && x->sig_alg->algorithm) {
1907 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
1908 OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
1909 }
1910 if (alg_a & SSL_aECDSA) {
1911 /* key usage, if present, must allow signing */
1912 if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE)) {
1913 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg,
1914 SSL_R_ECC_CERT_NOT_FOR_SIGNING);
1915 return 0;
1916 }
1917 }
Adam Langley95c29f32014-06-20 12:00:00 -07001918
Adam Langleyfcf25832014-12-18 17:42:32 -08001919 return 1; /* all checks are ok */
1920}
Adam Langley95c29f32014-06-20 12:00:00 -07001921
Adam Langleyfcf25832014-12-18 17:42:32 -08001922static int ssl_get_server_cert_index(const SSL *s) {
1923 int idx;
1924 idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1925 if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509) {
1926 idx = SSL_PKEY_RSA_SIGN;
1927 }
1928 if (idx == -1) {
1929 OPENSSL_PUT_ERROR(SSL, ssl_get_server_cert_index, ERR_R_INTERNAL_ERROR);
1930 }
1931 return idx;
1932}
Adam Langley95c29f32014-06-20 12:00:00 -07001933
Adam Langleyfcf25832014-12-18 17:42:32 -08001934CERT_PKEY *ssl_get_server_send_pkey(const SSL *s) {
1935 int i = ssl_get_server_cert_index(s);
Adam Langley95c29f32014-06-20 12:00:00 -07001936
Adam Langleyfcf25832014-12-18 17:42:32 -08001937 /* This may or may not be an error. */
1938 if (i < 0) {
1939 return NULL;
1940 }
Adam Langley95c29f32014-06-20 12:00:00 -07001941
Adam Langleyfcf25832014-12-18 17:42:32 -08001942 /* May be NULL. */
1943 return &s->cert->pkeys[i];
1944}
Adam Langley95c29f32014-06-20 12:00:00 -07001945
Adam Langleyfcf25832014-12-18 17:42:32 -08001946EVP_PKEY *ssl_get_sign_pkey(SSL *s, const SSL_CIPHER *cipher) {
David Benjamin3fa27772015-04-17 22:32:19 -04001947 uint32_t alg_a = cipher->algorithm_auth;
1948 CERT *c = s->cert;
Adam Langleyfcf25832014-12-18 17:42:32 -08001949 int idx = -1;
Adam Langley95c29f32014-06-20 12:00:00 -07001950
Adam Langleyfcf25832014-12-18 17:42:32 -08001951 if (alg_a & SSL_aRSA) {
1952 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL) {
1953 idx = SSL_PKEY_RSA_SIGN;
1954 } else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL) {
1955 idx = SSL_PKEY_RSA_ENC;
1956 }
1957 } else if ((alg_a & SSL_aECDSA) &&
1958 (c->pkeys[SSL_PKEY_ECC].privatekey != NULL)) {
1959 idx = SSL_PKEY_ECC;
1960 }
Adam Langley95c29f32014-06-20 12:00:00 -07001961
Adam Langleyfcf25832014-12-18 17:42:32 -08001962 if (idx == -1) {
1963 OPENSSL_PUT_ERROR(SSL, ssl_get_sign_pkey, ERR_R_INTERNAL_ERROR);
1964 return NULL;
1965 }
Adam Langley95c29f32014-06-20 12:00:00 -07001966
Adam Langleyfcf25832014-12-18 17:42:32 -08001967 return c->pkeys[idx].privatekey;
1968}
Adam Langley95c29f32014-06-20 12:00:00 -07001969
Adam Langleyfcf25832014-12-18 17:42:32 -08001970void ssl_update_cache(SSL *s, int mode) {
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001971 /* Never cache sessions with empty session IDs. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001972 if (s->session->session_id_length == 0) {
1973 return;
1974 }
Adam Langley95c29f32014-06-20 12:00:00 -07001975
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001976 SSL_CTX *ctx = s->initial_ctx;
1977 if ((ctx->session_cache_mode & mode) == mode && !s->hit &&
1978 ((ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) ||
1979 SSL_CTX_add_session(ctx, s->session)) &&
1980 ctx->new_session_cb != NULL) {
1981 /* Note: |new_session_cb| is called whether the internal session cache is
1982 * used or not. */
1983 if (!ctx->new_session_cb(s, SSL_SESSION_up_ref(s->session))) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001984 SSL_SESSION_free(s->session);
1985 }
1986 }
Adam Langley95c29f32014-06-20 12:00:00 -07001987
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001988 if (!(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR) &&
1989 !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) &&
1990 (ctx->session_cache_mode & mode) == mode) {
1991 /* Automatically flush the internal session cache every 255 connections. */
1992 int flush_cache = 0;
Adam Langley4bdb6e42015-05-15 15:29:21 -07001993 CRYPTO_MUTEX_lock_write(&ctx->lock);
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001994 ctx->handshakes_since_cache_flush++;
1995 if (ctx->handshakes_since_cache_flush >= 255) {
1996 flush_cache = 1;
1997 ctx->handshakes_since_cache_flush = 0;
1998 }
Adam Langley4bdb6e42015-05-15 15:29:21 -07001999 CRYPTO_MUTEX_unlock(&ctx->lock);
David Benjaminb6d0c6d2015-03-19 19:07:26 -04002000
2001 if (flush_cache) {
2002 SSL_CTX_flush_sessions(ctx, (unsigned long)time(NULL));
Adam Langleyfcf25832014-12-18 17:42:32 -08002003 }
2004 }
2005}
Adam Langley95c29f32014-06-20 12:00:00 -07002006
David Benjamin1a5c50f2015-03-11 16:22:37 -04002007int SSL_get_error(const SSL *s, int ret_code) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002008 int reason;
David Benjamin1a5c50f2015-03-11 16:22:37 -04002009 uint32_t err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002010 BIO *bio;
Adam Langley95c29f32014-06-20 12:00:00 -07002011
David Benjamin1a5c50f2015-03-11 16:22:37 -04002012 if (ret_code > 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002013 return SSL_ERROR_NONE;
2014 }
Adam Langley95c29f32014-06-20 12:00:00 -07002015
Adam Langleyfcf25832014-12-18 17:42:32 -08002016 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
2017 * where we do encode the error */
David Benjamin1a5c50f2015-03-11 16:22:37 -04002018 err = ERR_peek_error();
2019 if (err != 0) {
2020 if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002021 return SSL_ERROR_SYSCALL;
2022 }
2023 return SSL_ERROR_SSL;
2024 }
Adam Langley95c29f32014-06-20 12:00:00 -07002025
David Benjamin1a5c50f2015-03-11 16:22:37 -04002026 if (ret_code == 0) {
David Benjamin9a38e922015-01-22 16:06:11 -05002027 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2028 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) {
2029 /* The socket was cleanly shut down with a close_notify. */
2030 return SSL_ERROR_ZERO_RETURN;
2031 }
2032 /* An EOF was observed which violates the protocol, and the underlying
2033 * transport does not participate in the error queue. Bubble up to the
2034 * caller. */
2035 return SSL_ERROR_SYSCALL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002036 }
Adam Langleyb2ce0582014-06-20 12:00:00 -07002037
Adam Langleyfcf25832014-12-18 17:42:32 -08002038 if (SSL_want_session(s)) {
2039 return SSL_ERROR_PENDING_SESSION;
2040 }
Adam Langley95c29f32014-06-20 12:00:00 -07002041
Adam Langleyfcf25832014-12-18 17:42:32 -08002042 if (SSL_want_certificate(s)) {
2043 return SSL_ERROR_PENDING_CERTIFICATE;
2044 }
Adam Langley95c29f32014-06-20 12:00:00 -07002045
Adam Langleyfcf25832014-12-18 17:42:32 -08002046 if (SSL_want_read(s)) {
2047 bio = SSL_get_rbio(s);
2048 if (BIO_should_read(bio)) {
2049 return SSL_ERROR_WANT_READ;
2050 }
Adam Langley95c29f32014-06-20 12:00:00 -07002051
Adam Langleyfcf25832014-12-18 17:42:32 -08002052 if (BIO_should_write(bio)) {
2053 /* This one doesn't make too much sense ... We never try to write to the
2054 * rbio, and an application program where rbio and wbio are separate
2055 * couldn't even know what it should wait for. However if we ever set
2056 * s->rwstate incorrectly (so that we have SSL_want_read(s) instead of
2057 * SSL_want_write(s)) and rbio and wbio *are* the same, this test works
2058 * around that bug; so it might be safer to keep it. */
2059 return SSL_ERROR_WANT_WRITE;
2060 }
Adam Langley95c29f32014-06-20 12:00:00 -07002061
Adam Langleyfcf25832014-12-18 17:42:32 -08002062 if (BIO_should_io_special(bio)) {
2063 reason = BIO_get_retry_reason(bio);
2064 if (reason == BIO_RR_CONNECT) {
2065 return SSL_ERROR_WANT_CONNECT;
2066 }
Adam Langley95c29f32014-06-20 12:00:00 -07002067
Adam Langleyfcf25832014-12-18 17:42:32 -08002068 if (reason == BIO_RR_ACCEPT) {
2069 return SSL_ERROR_WANT_ACCEPT;
2070 }
Adam Langley95c29f32014-06-20 12:00:00 -07002071
Adam Langleyfcf25832014-12-18 17:42:32 -08002072 return SSL_ERROR_SYSCALL; /* unknown */
2073 }
2074 }
Adam Langley95c29f32014-06-20 12:00:00 -07002075
Adam Langleyfcf25832014-12-18 17:42:32 -08002076 if (SSL_want_write(s)) {
2077 bio = SSL_get_wbio(s);
2078 if (BIO_should_write(bio)) {
2079 return SSL_ERROR_WANT_WRITE;
2080 }
Adam Langley95c29f32014-06-20 12:00:00 -07002081
Adam Langleyfcf25832014-12-18 17:42:32 -08002082 if (BIO_should_read(bio)) {
2083 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2084 return SSL_ERROR_WANT_READ;
2085 }
Adam Langley95c29f32014-06-20 12:00:00 -07002086
Adam Langleyfcf25832014-12-18 17:42:32 -08002087 if (BIO_should_io_special(bio)) {
2088 reason = BIO_get_retry_reason(bio);
2089 if (reason == BIO_RR_CONNECT) {
2090 return SSL_ERROR_WANT_CONNECT;
2091 }
Adam Langley95c29f32014-06-20 12:00:00 -07002092
Adam Langleyfcf25832014-12-18 17:42:32 -08002093 if (reason == BIO_RR_ACCEPT) {
2094 return SSL_ERROR_WANT_ACCEPT;
2095 }
Adam Langley95c29f32014-06-20 12:00:00 -07002096
Adam Langleyfcf25832014-12-18 17:42:32 -08002097 return SSL_ERROR_SYSCALL;
2098 }
2099 }
Adam Langley95c29f32014-06-20 12:00:00 -07002100
Adam Langleyfcf25832014-12-18 17:42:32 -08002101 if (SSL_want_x509_lookup(s)) {
2102 return SSL_ERROR_WANT_X509_LOOKUP;
2103 }
Adam Langley95c29f32014-06-20 12:00:00 -07002104
Adam Langleyfcf25832014-12-18 17:42:32 -08002105 if (SSL_want_channel_id_lookup(s)) {
2106 return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
2107 }
Adam Langley0f4746e2014-08-13 12:26:32 -07002108
Adam Langleyfcf25832014-12-18 17:42:32 -08002109 return SSL_ERROR_SYSCALL;
2110}
Adam Langley0f4746e2014-08-13 12:26:32 -07002111
Adam Langleyfcf25832014-12-18 17:42:32 -08002112int SSL_do_handshake(SSL *s) {
2113 int ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002114
Adam Langleyfcf25832014-12-18 17:42:32 -08002115 if (s->handshake_func == NULL) {
2116 OPENSSL_PUT_ERROR(SSL, SSL_do_handshake, SSL_R_CONNECTION_TYPE_NOT_SET);
2117 return -1;
2118 }
Adam Langley95c29f32014-06-20 12:00:00 -07002119
Adam Langleyfcf25832014-12-18 17:42:32 -08002120 s->method->ssl_renegotiate_check(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002121
Adam Langleyfcf25832014-12-18 17:42:32 -08002122 if (SSL_in_init(s)) {
2123 ret = s->handshake_func(s);
2124 }
2125 return ret;
2126}
Adam Langley95c29f32014-06-20 12:00:00 -07002127
David Benjamin4831c332015-05-16 11:43:13 -04002128void SSL_set_accept_state(SSL *ssl) {
2129 ssl->server = 1;
2130 ssl->shutdown = 0;
2131 ssl->state = SSL_ST_ACCEPT | SSL_ST_BEFORE;
2132 ssl->handshake_func = ssl->method->ssl_accept;
Adam Langleyfcf25832014-12-18 17:42:32 -08002133 /* clear the current cipher */
David Benjamin4831c332015-05-16 11:43:13 -04002134 ssl_clear_cipher_ctx(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08002135}
Adam Langley95c29f32014-06-20 12:00:00 -07002136
David Benjamin4831c332015-05-16 11:43:13 -04002137void SSL_set_connect_state(SSL *ssl) {
2138 ssl->server = 0;
2139 ssl->shutdown = 0;
2140 ssl->state = SSL_ST_CONNECT | SSL_ST_BEFORE;
2141 ssl->handshake_func = ssl->method->ssl_connect;
Adam Langleyfcf25832014-12-18 17:42:32 -08002142 /* clear the current cipher */
David Benjamin4831c332015-05-16 11:43:13 -04002143 ssl_clear_cipher_ctx(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08002144}
Adam Langley95c29f32014-06-20 12:00:00 -07002145
Adam Langleyfcf25832014-12-18 17:42:32 -08002146static const char *ssl_get_version(int version) {
2147 switch (version) {
2148 case TLS1_2_VERSION:
2149 return "TLSv1.2";
Adam Langley95c29f32014-06-20 12:00:00 -07002150
Adam Langleyfcf25832014-12-18 17:42:32 -08002151 case TLS1_1_VERSION:
2152 return "TLSv1.1";
Adam Langley95c29f32014-06-20 12:00:00 -07002153
Adam Langleyfcf25832014-12-18 17:42:32 -08002154 case TLS1_VERSION:
2155 return "TLSv1";
Adam Langley95c29f32014-06-20 12:00:00 -07002156
Adam Langleyfcf25832014-12-18 17:42:32 -08002157 case SSL3_VERSION:
2158 return "SSLv3";
Adam Langley95c29f32014-06-20 12:00:00 -07002159
David Benjamin1c722b72015-04-20 13:53:10 -04002160 case DTLS1_VERSION:
2161 return "DTLSv1";
2162
2163 case DTLS1_2_VERSION:
2164 return "DTLSv1.2";
2165
Adam Langleyfcf25832014-12-18 17:42:32 -08002166 default:
2167 return "unknown";
2168 }
2169}
Adam Langley95c29f32014-06-20 12:00:00 -07002170
Adam Langleyfcf25832014-12-18 17:42:32 -08002171const char *SSL_get_version(const SSL *s) {
2172 return ssl_get_version(s->version);
2173}
Adam Langley95c29f32014-06-20 12:00:00 -07002174
Adam Langleyfcf25832014-12-18 17:42:32 -08002175const char *SSL_SESSION_get_version(const SSL_SESSION *sess) {
2176 return ssl_get_version(sess->ssl_version);
2177}
Adam Langley95c29f32014-06-20 12:00:00 -07002178
Adam Langleyfcf25832014-12-18 17:42:32 -08002179void ssl_clear_cipher_ctx(SSL *s) {
David Benjamin31a07792015-03-03 14:20:26 -05002180 SSL_AEAD_CTX_free(s->aead_read_ctx);
2181 s->aead_read_ctx = NULL;
2182 SSL_AEAD_CTX_free(s->aead_write_ctx);
2183 s->aead_write_ctx = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002184}
Adam Langley95c29f32014-06-20 12:00:00 -07002185
Adam Langleyfcf25832014-12-18 17:42:32 -08002186X509 *SSL_get_certificate(const SSL *s) {
2187 if (s->cert != NULL) {
2188 return s->cert->key->x509;
2189 }
2190
2191 return NULL;
2192}
2193
2194EVP_PKEY *SSL_get_privatekey(const SSL *s) {
2195 if (s->cert != NULL) {
2196 return s->cert->key->privatekey;
2197 }
2198
2199 return NULL;
2200}
2201
2202X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
2203 if (ctx->cert != NULL) {
2204 return ctx->cert->key->x509;
2205 }
2206
2207 return NULL;
2208}
2209
2210EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
2211 if (ctx->cert != NULL) {
2212 return ctx->cert->key->privatekey;
2213 }
2214
2215 return NULL;
2216}
2217
2218const SSL_CIPHER *SSL_get_current_cipher(const SSL *s) {
David Benjamina07c0fc2015-05-13 13:19:42 -04002219 if (s->aead_write_ctx == NULL) {
2220 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002221 }
David Benjamina07c0fc2015-05-13 13:19:42 -04002222 return s->aead_write_ctx->cipher;
Adam Langleyfcf25832014-12-18 17:42:32 -08002223}
2224
2225const void *SSL_get_current_compression(SSL *s) { return NULL; }
2226
2227const void *SSL_get_current_expansion(SSL *s) { return NULL; }
2228
2229int ssl_init_wbio_buffer(SSL *s, int push) {
2230 BIO *bbio;
2231
2232 if (s->bbio == NULL) {
2233 bbio = BIO_new(BIO_f_buffer());
2234 if (bbio == NULL) {
2235 return 0;
2236 }
2237 s->bbio = bbio;
2238 } else {
2239 bbio = s->bbio;
2240 if (s->bbio == s->wbio) {
2241 s->wbio = BIO_pop(s->wbio);
2242 }
2243 }
2244
2245 BIO_reset(bbio);
2246 if (!BIO_set_read_buffer_size(bbio, 1)) {
2247 OPENSSL_PUT_ERROR(SSL, ssl_init_wbio_buffer, ERR_R_BUF_LIB);
2248 return 0;
2249 }
2250
2251 if (push) {
2252 if (s->wbio != bbio) {
2253 s->wbio = BIO_push(bbio, s->wbio);
2254 }
2255 } else {
2256 if (s->wbio == bbio) {
2257 s->wbio = BIO_pop(bbio);
2258 }
2259 }
2260
2261 return 1;
2262}
2263
2264void ssl_free_wbio_buffer(SSL *s) {
2265 if (s->bbio == NULL) {
2266 return;
2267 }
2268
2269 if (s->bbio == s->wbio) {
2270 /* remove buffering */
2271 s->wbio = BIO_pop(s->wbio);
2272 }
2273
2274 BIO_free(s->bbio);
2275 s->bbio = NULL;
2276}
2277
2278void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
2279 ctx->quiet_shutdown = mode;
2280}
2281
2282int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
2283 return ctx->quiet_shutdown;
2284}
2285
2286void SSL_set_quiet_shutdown(SSL *s, int mode) { s->quiet_shutdown = mode; }
2287
2288int SSL_get_quiet_shutdown(const SSL *s) { return s->quiet_shutdown; }
2289
2290void SSL_set_shutdown(SSL *s, int mode) { s->shutdown = mode; }
2291
2292int SSL_get_shutdown(const SSL *s) { return s->shutdown; }
2293
2294int SSL_version(const SSL *s) { return s->version; }
2295
2296SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
2297
2298SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
2299 if (ssl->ctx == ctx) {
2300 return ssl->ctx;
2301 }
2302
2303 if (ctx == NULL) {
2304 ctx = ssl->initial_ctx;
2305 }
2306
David Benjamin2755a3e2015-04-22 16:17:58 -04002307 ssl_cert_free(ssl->cert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002308 ssl->cert = ssl_cert_dup(ctx->cert);
David Benjamin2755a3e2015-04-22 16:17:58 -04002309
Adam Langley0b5e3902015-05-15 13:08:38 -07002310 CRYPTO_refcount_inc(&ctx->references);
David Benjamin2755a3e2015-04-22 16:17:58 -04002311 SSL_CTX_free(ssl->ctx); /* decrement reference count */
Adam Langleyfcf25832014-12-18 17:42:32 -08002312 ssl->ctx = ctx;
2313
2314 ssl->sid_ctx_length = ctx->sid_ctx_length;
2315 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2316 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2317
2318 return ssl->ctx;
2319}
2320
2321int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
2322 return X509_STORE_set_default_paths(ctx->cert_store);
2323}
Adam Langley95c29f32014-06-20 12:00:00 -07002324
2325int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
Adam Langleyfcf25832014-12-18 17:42:32 -08002326 const char *CApath) {
2327 return X509_STORE_load_locations(ctx->cert_store, CAfile, CApath);
2328}
Adam Langley95c29f32014-06-20 12:00:00 -07002329
2330void SSL_set_info_callback(SSL *ssl,
Adam Langleyfcf25832014-12-18 17:42:32 -08002331 void (*cb)(const SSL *ssl, int type, int val)) {
2332 ssl->info_callback = cb;
2333}
Adam Langley95c29f32014-06-20 12:00:00 -07002334
Adam Langleyfcf25832014-12-18 17:42:32 -08002335void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/, int /*type*/,
2336 int /*val*/) {
2337 return ssl->info_callback;
2338}
Adam Langley95c29f32014-06-20 12:00:00 -07002339
Adam Langleyfcf25832014-12-18 17:42:32 -08002340int SSL_state(const SSL *ssl) { return ssl->state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002341
Adam Langleyfcf25832014-12-18 17:42:32 -08002342void SSL_set_state(SSL *ssl, int state) { ssl->state = state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002343
Adam Langleyfcf25832014-12-18 17:42:32 -08002344void SSL_set_verify_result(SSL *ssl, long arg) { ssl->verify_result = arg; }
Adam Langley95c29f32014-06-20 12:00:00 -07002345
Adam Langleyfcf25832014-12-18 17:42:32 -08002346long SSL_get_verify_result(const SSL *ssl) { return ssl->verify_result; }
Adam Langley95c29f32014-06-20 12:00:00 -07002347
Adam Langleyfcf25832014-12-18 17:42:32 -08002348int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2349 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002350 int index;
2351 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp,
2352 new_func, dup_func, free_func)) {
2353 return -1;
2354 }
2355 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002356}
Adam Langley95c29f32014-06-20 12:00:00 -07002357
Adam Langleyfcf25832014-12-18 17:42:32 -08002358int SSL_set_ex_data(SSL *s, int idx, void *arg) {
2359 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2360}
Adam Langley95c29f32014-06-20 12:00:00 -07002361
Adam Langleyfcf25832014-12-18 17:42:32 -08002362void *SSL_get_ex_data(const SSL *s, int idx) {
2363 return CRYPTO_get_ex_data(&s->ex_data, idx);
2364}
Adam Langley95c29f32014-06-20 12:00:00 -07002365
Adam Langleyfcf25832014-12-18 17:42:32 -08002366int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2367 CRYPTO_EX_dup *dup_func,
2368 CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002369 int index;
2370 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp,
2371 new_func, dup_func, free_func)) {
2372 return -1;
2373 }
2374 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002375}
Adam Langley95c29f32014-06-20 12:00:00 -07002376
Adam Langleyfcf25832014-12-18 17:42:32 -08002377int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg) {
2378 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2379}
Adam Langley95c29f32014-06-20 12:00:00 -07002380
Adam Langleyfcf25832014-12-18 17:42:32 -08002381void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx) {
2382 return CRYPTO_get_ex_data(&s->ex_data, idx);
2383}
Adam Langley95c29f32014-06-20 12:00:00 -07002384
Adam Langleyfcf25832014-12-18 17:42:32 -08002385X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
2386 return ctx->cert_store;
2387}
Adam Langley95c29f32014-06-20 12:00:00 -07002388
Adam Langleyfcf25832014-12-18 17:42:32 -08002389void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
David Benjamin2755a3e2015-04-22 16:17:58 -04002390 X509_STORE_free(ctx->cert_store);
Adam Langleyfcf25832014-12-18 17:42:32 -08002391 ctx->cert_store = store;
2392}
Adam Langley95c29f32014-06-20 12:00:00 -07002393
Adam Langleyfcf25832014-12-18 17:42:32 -08002394int SSL_want(const SSL *s) { return s->rwstate; }
Adam Langley95c29f32014-06-20 12:00:00 -07002395
Adam Langleyfcf25832014-12-18 17:42:32 -08002396void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
2397 RSA *(*cb)(SSL *ssl, int is_export,
2398 int keylength)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002399}
Adam Langley95c29f32014-06-20 12:00:00 -07002400
Adam Langleyfcf25832014-12-18 17:42:32 -08002401void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
2402 int keylength)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002403}
2404
2405void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
David Benjamin59015c32015-04-26 13:13:55 -04002406 DH *(*callback)(SSL *ssl, int is_export,
2407 int keylength)) {
2408 ctx->cert->dh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002409}
2410
David Benjamin59015c32015-04-26 13:13:55 -04002411void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*callback)(SSL *ssl, int is_export,
2412 int keylength)) {
2413 ssl->cert->dh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002414}
2415
2416void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,
David Benjamindd978782015-04-24 15:20:13 -04002417 EC_KEY *(*callback)(SSL *ssl, int is_export,
2418 int keylength)) {
David Benjamin59015c32015-04-26 13:13:55 -04002419 ctx->cert->ecdh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002420}
2421
2422void SSL_set_tmp_ecdh_callback(SSL *ssl,
David Benjamindd978782015-04-24 15:20:13 -04002423 EC_KEY *(*callback)(SSL *ssl, int is_export,
2424 int keylength)) {
David Benjamin59015c32015-04-26 13:13:55 -04002425 ssl->cert->ecdh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002426}
2427
2428int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
2429 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2430 OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_psk_identity_hint,
2431 SSL_R_DATA_LENGTH_TOO_LONG);
2432 return 0;
2433 }
2434
David Benjamin2755a3e2015-04-22 16:17:58 -04002435 OPENSSL_free(ctx->psk_identity_hint);
Adam Langleyfcf25832014-12-18 17:42:32 -08002436
2437 if (identity_hint != NULL) {
2438 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2439 if (ctx->psk_identity_hint == NULL) {
2440 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002441 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002442 } else {
2443 ctx->psk_identity_hint = NULL;
2444 }
Adam Langley95c29f32014-06-20 12:00:00 -07002445
Adam Langleyfcf25832014-12-18 17:42:32 -08002446 return 1;
2447}
2448
2449int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint) {
2450 if (s == NULL) {
2451 return 0;
2452 }
2453
2454 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2455 OPENSSL_PUT_ERROR(SSL, SSL_use_psk_identity_hint,
2456 SSL_R_DATA_LENGTH_TOO_LONG);
2457 return 0;
2458 }
2459
2460 /* Clear currently configured hint, if any. */
David Benjamin2755a3e2015-04-22 16:17:58 -04002461 OPENSSL_free(s->psk_identity_hint);
2462 s->psk_identity_hint = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002463
2464 if (identity_hint != NULL) {
2465 s->psk_identity_hint = BUF_strdup(identity_hint);
2466 if (s->psk_identity_hint == NULL) {
2467 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002468 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002469 }
Adam Langley95c29f32014-06-20 12:00:00 -07002470
Adam Langleyfcf25832014-12-18 17:42:32 -08002471 return 1;
2472}
Adam Langley95c29f32014-06-20 12:00:00 -07002473
Adam Langleyfcf25832014-12-18 17:42:32 -08002474const char *SSL_get_psk_identity_hint(const SSL *s) {
2475 if (s == NULL) {
2476 return NULL;
2477 }
2478 return s->psk_identity_hint;
2479}
Adam Langley95c29f32014-06-20 12:00:00 -07002480
Adam Langleyfcf25832014-12-18 17:42:32 -08002481const char *SSL_get_psk_identity(const SSL *s) {
2482 if (s == NULL || s->session == NULL) {
2483 return NULL;
2484 }
Adam Langley95c29f32014-06-20 12:00:00 -07002485
Adam Langleyfcf25832014-12-18 17:42:32 -08002486 return s->session->psk_identity;
2487}
Adam Langley95c29f32014-06-20 12:00:00 -07002488
Adam Langleyfcf25832014-12-18 17:42:32 -08002489void SSL_set_psk_client_callback(
2490 SSL *s, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2491 unsigned int max_identity_len, uint8_t *psk,
2492 unsigned int max_psk_len)) {
2493 s->psk_client_callback = cb;
2494}
Adam Langley95c29f32014-06-20 12:00:00 -07002495
Adam Langleyfcf25832014-12-18 17:42:32 -08002496void SSL_CTX_set_psk_client_callback(
2497 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2498 unsigned int max_identity_len,
2499 uint8_t *psk, unsigned int max_psk_len)) {
2500 ctx->psk_client_callback = cb;
2501}
Adam Langley95c29f32014-06-20 12:00:00 -07002502
Adam Langleyfcf25832014-12-18 17:42:32 -08002503void SSL_set_psk_server_callback(
2504 SSL *s, unsigned int (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
2505 unsigned int max_psk_len)) {
2506 s->psk_server_callback = cb;
2507}
Adam Langley95c29f32014-06-20 12:00:00 -07002508
Adam Langleyfcf25832014-12-18 17:42:32 -08002509void SSL_CTX_set_psk_server_callback(
2510 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *identity,
2511 uint8_t *psk, unsigned int max_psk_len)) {
2512 ctx->psk_server_callback = cb;
2513}
Adam Langley95c29f32014-06-20 12:00:00 -07002514
Adam Langleyfcf25832014-12-18 17:42:32 -08002515void SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) {
2516 ctx->min_version = version;
2517}
Adam Langley95c29f32014-06-20 12:00:00 -07002518
Adam Langleyfcf25832014-12-18 17:42:32 -08002519void SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) {
2520 ctx->max_version = version;
2521}
Adam Langley0289c732014-06-20 12:00:00 -07002522
Adam Langleyfcf25832014-12-18 17:42:32 -08002523void SSL_set_min_version(SSL *ssl, uint16_t version) {
2524 ssl->min_version = version;
2525}
Adam Langley0289c732014-06-20 12:00:00 -07002526
Adam Langleyfcf25832014-12-18 17:42:32 -08002527void SSL_set_max_version(SSL *ssl, uint16_t version) {
2528 ssl->max_version = version;
2529}
Adam Langley95c29f32014-06-20 12:00:00 -07002530
Adam Langleyfcf25832014-12-18 17:42:32 -08002531void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
2532 void (*cb)(int write_p, int version,
2533 int content_type, const void *buf,
2534 size_t len, SSL *ssl, void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002535 ctx->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002536}
David Benjamin61ecccf2015-05-05 09:44:51 -04002537
2538void SSL_CTX_set_msg_callback_arg(SSL_CTX *ctx, void *arg) {
2539 ctx->msg_callback_arg = arg;
2540}
2541
Adam Langleyfcf25832014-12-18 17:42:32 -08002542void SSL_set_msg_callback(SSL *ssl,
2543 void (*cb)(int write_p, int version, int content_type,
2544 const void *buf, size_t len, SSL *ssl,
2545 void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002546 ssl->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002547}
Adam Langley95c29f32014-06-20 12:00:00 -07002548
David Benjamin61ecccf2015-05-05 09:44:51 -04002549void SSL_set_msg_callback_arg(SSL *ssl, void *arg) {
2550 ssl->msg_callback_arg = arg;
2551}
2552
Adam Langleyfcf25832014-12-18 17:42:32 -08002553void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio) {
David Benjamin2755a3e2015-04-22 16:17:58 -04002554 BIO_free(ctx->keylog_bio);
Adam Langleyfcf25832014-12-18 17:42:32 -08002555 ctx->keylog_bio = keylog_bio;
2556}
Adam Langley95c29f32014-06-20 12:00:00 -07002557
Adam Langleyfcf25832014-12-18 17:42:32 -08002558static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
2559 static const char hextable[] = "0123456789abcdef";
2560 uint8_t *out;
2561 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002562
Adam Langleyfcf25832014-12-18 17:42:32 -08002563 if (!CBB_add_space(cbb, &out, in_len * 2)) {
2564 return 0;
2565 }
Adam Langley95c29f32014-06-20 12:00:00 -07002566
Adam Langleyfcf25832014-12-18 17:42:32 -08002567 for (i = 0; i < in_len; i++) {
2568 *(out++) = (uint8_t)hextable[in[i] >> 4];
2569 *(out++) = (uint8_t)hextable[in[i] & 0xf];
2570 }
Adam Langley95c29f32014-06-20 12:00:00 -07002571
Adam Langleyfcf25832014-12-18 17:42:32 -08002572 return 1;
2573}
David Benjamin859ec3c2014-09-02 16:29:36 -04002574
2575int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -08002576 const uint8_t *encrypted_premaster,
2577 size_t encrypted_premaster_len,
2578 const uint8_t *premaster,
2579 size_t premaster_len) {
2580 BIO *bio = ctx->keylog_bio;
2581 CBB cbb;
2582 uint8_t *out;
2583 size_t out_len;
2584 int ret;
David Benjamin859ec3c2014-09-02 16:29:36 -04002585
Adam Langleyfcf25832014-12-18 17:42:32 -08002586 if (bio == NULL) {
2587 return 1;
2588 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002589
Adam Langleyfcf25832014-12-18 17:42:32 -08002590 if (encrypted_premaster_len < 8) {
2591 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_rsa_client_key_exchange,
2592 ERR_R_INTERNAL_ERROR);
2593 return 0;
2594 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002595
Adam Langleyfcf25832014-12-18 17:42:32 -08002596 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len * 2 + 1)) {
2597 return 0;
2598 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002599
Adam Langleyfcf25832014-12-18 17:42:32 -08002600 if (!CBB_add_bytes(&cbb, (const uint8_t *)"RSA ", 4) ||
2601 /* Only the first 8 bytes of the encrypted premaster secret are
2602 * logged. */
2603 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
2604 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2605 !cbb_add_hex(&cbb, premaster, premaster_len) ||
2606 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2607 !CBB_finish(&cbb, &out, &out_len)) {
2608 CBB_cleanup(&cbb);
2609 return 0;
2610 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002611
Adam Langley4bdb6e42015-05-15 15:29:21 -07002612 CRYPTO_MUTEX_lock_write(&ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -08002613 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
Adam Langley4bdb6e42015-05-15 15:29:21 -07002614 CRYPTO_MUTEX_unlock(&ctx->lock);
David Benjamin859ec3c2014-09-02 16:29:36 -04002615
Adam Langleyfcf25832014-12-18 17:42:32 -08002616 OPENSSL_free(out);
2617 return ret;
Adam Langley95f22882014-06-20 12:00:00 -07002618}
2619
Adam Langleyfcf25832014-12-18 17:42:32 -08002620int ssl_ctx_log_master_secret(SSL_CTX *ctx, const uint8_t *client_random,
2621 size_t client_random_len, const uint8_t *master,
2622 size_t master_len) {
2623 BIO *bio = ctx->keylog_bio;
2624 CBB cbb;
2625 uint8_t *out;
2626 size_t out_len;
2627 int ret;
Adam Langleyadb739e2014-06-20 12:00:00 -07002628
Adam Langleyfcf25832014-12-18 17:42:32 -08002629 if (bio == NULL) {
2630 return 1;
2631 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002632
Adam Langleyfcf25832014-12-18 17:42:32 -08002633 if (client_random_len != 32) {
2634 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_master_secret, ERR_R_INTERNAL_ERROR);
2635 return 0;
2636 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002637
Adam Langleyfcf25832014-12-18 17:42:32 -08002638 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len * 2 + 1)) {
2639 return 0;
2640 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002641
Adam Langleyfcf25832014-12-18 17:42:32 -08002642 if (!CBB_add_bytes(&cbb, (const uint8_t *)"CLIENT_RANDOM ", 14) ||
2643 !cbb_add_hex(&cbb, client_random, 32) ||
2644 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2645 !cbb_add_hex(&cbb, master, master_len) ||
2646 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2647 !CBB_finish(&cbb, &out, &out_len)) {
2648 CBB_cleanup(&cbb);
2649 return 0;
2650 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002651
Adam Langley4bdb6e42015-05-15 15:29:21 -07002652 CRYPTO_MUTEX_lock_write(&ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -08002653 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
Adam Langley4bdb6e42015-05-15 15:29:21 -07002654 CRYPTO_MUTEX_unlock(&ctx->lock);
David Benjamine99e9122014-12-11 01:46:01 -05002655
Adam Langleyfcf25832014-12-18 17:42:32 -08002656 OPENSSL_free(out);
2657 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -07002658}
2659
David Benjamined7c4752015-02-16 19:16:46 -05002660int SSL_in_false_start(const SSL *s) {
2661 return s->s3->tmp.in_false_start;
2662}
2663
Adam Langleyfcf25832014-12-18 17:42:32 -08002664int SSL_cutthrough_complete(const SSL *s) {
David Benjamined7c4752015-02-16 19:16:46 -05002665 return SSL_in_false_start(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002666}
Adam Langley95c29f32014-06-20 12:00:00 -07002667
Adam Langleyfcf25832014-12-18 17:42:32 -08002668void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
2669 size_t *ssl_session_size) {
2670 *ssl_size = sizeof(SSL);
2671 *ssl_ctx_size = sizeof(SSL_CTX);
2672 *ssl_session_size = sizeof(SSL_SESSION);
2673}
Feng Lu41aa3252014-11-21 22:47:56 -08002674
David Benjamined7c4752015-02-16 19:16:46 -05002675int ssl3_can_false_start(const SSL *s) {
David Benjamin195dc782015-02-19 13:27:05 -05002676 const SSL_CIPHER *const cipher = SSL_get_current_cipher(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002677
Adam Langleye631d962015-04-01 13:11:01 -07002678 /* False Start only for TLS 1.2 with an ECDHE+AEAD cipher and ALPN or NPN. */
David Benjamin195dc782015-02-19 13:27:05 -05002679 return !SSL_IS_DTLS(s) &&
2680 SSL_version(s) >= TLS1_2_VERSION &&
2681 (s->s3->alpn_selected || s->s3->next_proto_neg_seen) &&
2682 cipher != NULL &&
Adam Langleye631d962015-04-01 13:11:01 -07002683 cipher->algorithm_mkey == SSL_kECDHE &&
David Benjamin195dc782015-02-19 13:27:05 -05002684 (cipher->algorithm_enc == SSL_AES128GCM ||
2685 cipher->algorithm_enc == SSL_AES256GCM ||
2686 cipher->algorithm_enc == SSL_CHACHA20POLY1305);
Adam Langleyfcf25832014-12-18 17:42:32 -08002687}
2688
2689const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version) {
2690 switch (version) {
2691 case SSL3_VERSION:
2692 return &SSLv3_enc_data;
2693
2694 case TLS1_VERSION:
2695 return &TLSv1_enc_data;
2696
David Benjamin9e13e1a2015-03-05 01:56:32 -05002697 case DTLS1_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002698 case TLS1_1_VERSION:
2699 return &TLSv1_1_enc_data;
2700
David Benjamin9e13e1a2015-03-05 01:56:32 -05002701 case DTLS1_2_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002702 case TLS1_2_VERSION:
2703 return &TLSv1_2_enc_data;
2704
Adam Langleyfcf25832014-12-18 17:42:32 -08002705 default:
2706 return NULL;
2707 }
2708}
2709
2710uint16_t ssl3_get_max_server_version(const SSL *s) {
2711 uint16_t max_version;
2712
2713 if (SSL_IS_DTLS(s)) {
2714 max_version = (s->max_version != 0) ? s->max_version : DTLS1_2_VERSION;
2715 if (!(s->options & SSL_OP_NO_DTLSv1_2) && DTLS1_2_VERSION >= max_version) {
2716 return DTLS1_2_VERSION;
2717 }
2718 if (!(s->options & SSL_OP_NO_DTLSv1) && DTLS1_VERSION >= max_version) {
2719 return DTLS1_VERSION;
2720 }
2721 return 0;
2722 }
2723
2724 max_version = (s->max_version != 0) ? s->max_version : TLS1_2_VERSION;
2725 if (!(s->options & SSL_OP_NO_TLSv1_2) && TLS1_2_VERSION <= max_version) {
2726 return TLS1_2_VERSION;
2727 }
2728 if (!(s->options & SSL_OP_NO_TLSv1_1) && TLS1_1_VERSION <= max_version) {
2729 return TLS1_1_VERSION;
2730 }
2731 if (!(s->options & SSL_OP_NO_TLSv1) && TLS1_VERSION <= max_version) {
2732 return TLS1_VERSION;
2733 }
2734 if (!(s->options & SSL_OP_NO_SSLv3) && SSL3_VERSION <= max_version) {
2735 return SSL3_VERSION;
2736 }
2737 return 0;
2738}
2739
2740uint16_t ssl3_get_mutual_version(SSL *s, uint16_t client_version) {
2741 uint16_t version = 0;
2742
2743 if (SSL_IS_DTLS(s)) {
2744 /* Clamp client_version to max_version. */
2745 if (s->max_version != 0 && client_version < s->max_version) {
2746 client_version = s->max_version;
2747 }
2748
2749 if (client_version <= DTLS1_2_VERSION && !(s->options & SSL_OP_NO_DTLSv1_2)) {
2750 version = DTLS1_2_VERSION;
2751 } else if (client_version <= DTLS1_VERSION &&
David Benjaminb18f0242015-03-10 18:30:08 -04002752 !(s->options & SSL_OP_NO_DTLSv1)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002753 version = DTLS1_VERSION;
2754 }
2755
2756 /* Check against min_version. */
2757 if (version != 0 && s->min_version != 0 && version > s->min_version) {
2758 return 0;
2759 }
2760 return version;
2761 } else {
2762 /* Clamp client_version to max_version. */
2763 if (s->max_version != 0 && client_version > s->max_version) {
2764 client_version = s->max_version;
2765 }
2766
2767 if (client_version >= TLS1_2_VERSION && !(s->options & SSL_OP_NO_TLSv1_2)) {
2768 version = TLS1_2_VERSION;
2769 } else if (client_version >= TLS1_1_VERSION &&
2770 !(s->options & SSL_OP_NO_TLSv1_1)) {
2771 version = TLS1_1_VERSION;
2772 } else if (client_version >= TLS1_VERSION && !(s->options & SSL_OP_NO_TLSv1)) {
2773 version = TLS1_VERSION;
2774 } else if (client_version >= SSL3_VERSION && !(s->options & SSL_OP_NO_SSLv3)) {
2775 version = SSL3_VERSION;
2776 }
2777
2778 /* Check against min_version. */
2779 if (version != 0 && s->min_version != 0 && version < s->min_version) {
2780 return 0;
2781 }
2782 return version;
2783 }
2784}
2785
2786uint16_t ssl3_get_max_client_version(SSL *s) {
David Benjamin123a8fd2015-04-26 14:16:41 -04002787 uint32_t options = s->options;
Adam Langleyfcf25832014-12-18 17:42:32 -08002788 uint16_t version = 0;
2789
2790 /* OpenSSL's API for controlling versions entails blacklisting individual
2791 * protocols. This has two problems. First, on the client, the protocol can
2792 * only express a contiguous range of versions. Second, a library consumer
2793 * trying to set a maximum version cannot disable protocol versions that get
2794 * added in a future version of the library.
2795 *
2796 * To account for both of these, OpenSSL interprets the client-side bitmask
2797 * as a min/max range by picking the lowest contiguous non-empty range of
2798 * enabled protocols. Note that this means it is impossible to set a maximum
2799 * version of TLS 1.2 in a future-proof way.
2800 *
2801 * By this scheme, the maximum version is the lowest version V such that V is
2802 * enabled and V+1 is disabled or unimplemented. */
2803 if (SSL_IS_DTLS(s)) {
2804 if (!(options & SSL_OP_NO_DTLSv1_2)) {
2805 version = DTLS1_2_VERSION;
2806 }
2807 if (!(options & SSL_OP_NO_DTLSv1) && (options & SSL_OP_NO_DTLSv1_2)) {
2808 version = DTLS1_VERSION;
2809 }
2810 if (s->max_version != 0 && version < s->max_version) {
2811 version = s->max_version;
2812 }
2813 } else {
2814 if (!(options & SSL_OP_NO_TLSv1_2)) {
2815 version = TLS1_2_VERSION;
2816 }
2817 if (!(options & SSL_OP_NO_TLSv1_1) && (options & SSL_OP_NO_TLSv1_2)) {
2818 version = TLS1_1_VERSION;
2819 }
2820 if (!(options & SSL_OP_NO_TLSv1) && (options & SSL_OP_NO_TLSv1_1)) {
2821 version = TLS1_VERSION;
2822 }
2823 if (!(options & SSL_OP_NO_SSLv3) && (options & SSL_OP_NO_TLSv1)) {
2824 version = SSL3_VERSION;
2825 }
2826 if (s->max_version != 0 && version > s->max_version) {
2827 version = s->max_version;
2828 }
2829 }
2830
2831 return version;
2832}
2833
2834int ssl3_is_version_enabled(SSL *s, uint16_t version) {
2835 if (SSL_IS_DTLS(s)) {
2836 if (s->max_version != 0 && version < s->max_version) {
2837 return 0;
2838 }
2839 if (s->min_version != 0 && version > s->min_version) {
2840 return 0;
2841 }
2842
2843 switch (version) {
2844 case DTLS1_VERSION:
2845 return !(s->options & SSL_OP_NO_DTLSv1);
2846
2847 case DTLS1_2_VERSION:
2848 return !(s->options & SSL_OP_NO_DTLSv1_2);
2849
2850 default:
2851 return 0;
2852 }
2853 } else {
2854 if (s->max_version != 0 && version > s->max_version) {
2855 return 0;
2856 }
2857 if (s->min_version != 0 && version < s->min_version) {
2858 return 0;
2859 }
2860
2861 switch (version) {
2862 case SSL3_VERSION:
2863 return !(s->options & SSL_OP_NO_SSLv3);
2864
2865 case TLS1_VERSION:
2866 return !(s->options & SSL_OP_NO_TLSv1);
2867
2868 case TLS1_1_VERSION:
2869 return !(s->options & SSL_OP_NO_TLSv1_1);
2870
2871 case TLS1_2_VERSION:
2872 return !(s->options & SSL_OP_NO_TLSv1_2);
2873
2874 default:
2875 return 0;
2876 }
2877 }
2878}
2879
David Benjaminea72bd02014-12-21 21:27:41 -05002880uint16_t ssl3_version_from_wire(SSL *s, uint16_t wire_version) {
2881 if (!SSL_IS_DTLS(s)) {
2882 return wire_version;
2883 }
2884
2885 uint16_t tls_version = ~wire_version;
2886 uint16_t version = tls_version + 0x0201;
2887 /* If either component overflowed, clamp it so comparisons still work. */
2888 if ((version >> 8) < (tls_version >> 8)) {
2889 version = 0xff00 | (version & 0xff);
2890 }
2891 if ((version & 0xff) < (tls_version & 0xff)) {
2892 version = (version & 0xff00) | 0xff;
2893 }
2894 /* DTLS 1.0 maps to TLS 1.1, not TLS 1.0. */
2895 if (version == TLS1_VERSION) {
2896 version = TLS1_1_VERSION;
2897 }
2898 return version;
2899}
2900
Adam Langleyfcf25832014-12-18 17:42:32 -08002901int SSL_cache_hit(SSL *s) { return s->hit; }
2902
2903int SSL_is_server(SSL *s) { return s->server; }
2904
Adam Langley524e7172015-02-20 16:04:00 -08002905void SSL_CTX_set_dos_protection_cb(
2906 SSL_CTX *ctx, int (*cb)(const struct ssl_early_callback_ctx *)) {
2907 ctx->dos_protection_cb = cb;
2908}
2909
Adam Langleyfcf25832014-12-18 17:42:32 -08002910void SSL_enable_fastradio_padding(SSL *s, char on_off) {
2911 s->fastradio_padding = on_off;
2912}
Adam Langley44972942015-02-20 13:43:23 -08002913
David Benjaminb16346b2015-04-08 19:16:58 -04002914void SSL_set_reject_peer_renegotiations(SSL *s, int reject) {
David Benjamin897e5e02015-05-12 17:03:54 -04002915 s->accept_peer_renegotiations = !reject;
David Benjaminb16346b2015-04-08 19:16:58 -04002916}
2917
Adam Langley44972942015-02-20 13:43:23 -08002918const SSL_CIPHER *SSL_get_cipher_by_value(uint16_t value) {
2919 return ssl3_get_cipher_by_value(value);
2920}
Adam Langley3f92d212015-02-20 15:32:52 -08002921
2922int SSL_get_rc4_state(const SSL *ssl, const RC4_KEY **read_key,
2923 const RC4_KEY **write_key) {
2924 if (ssl->aead_read_ctx == NULL || ssl->aead_write_ctx == NULL) {
2925 return 0;
2926 }
2927
2928 return EVP_AEAD_CTX_get_rc4_state(&ssl->aead_read_ctx->ctx, read_key) &&
2929 EVP_AEAD_CTX_get_rc4_state(&ssl->aead_write_ctx->ctx, write_key);
2930}
David Benjaminda881e92015-04-26 14:45:04 -04002931
2932int SSL_CTX_sess_connect(const SSL_CTX *ctx) { return 0; }
2933int SSL_CTX_sess_connect_good(const SSL_CTX *ctx) { return 0; }
2934int SSL_CTX_sess_connect_renegotiate(const SSL_CTX *ctx) { return 0; }
2935int SSL_CTX_sess_accept(const SSL_CTX *ctx) { return 0; }
2936int SSL_CTX_sess_accept_renegotiate(const SSL_CTX *ctx) { return 0; }
2937int SSL_CTX_sess_accept_good(const SSL_CTX *ctx) { return 0; }
2938int SSL_CTX_sess_hits(const SSL_CTX *ctx) { return 0; }
2939int SSL_CTX_sess_cb_hits(const SSL_CTX *ctx) { return 0; }
2940int SSL_CTX_sess_misses(const SSL_CTX *ctx) { return 0; }
2941int SSL_CTX_sess_timeouts(const SSL_CTX *ctx) { return 0; }
2942int SSL_CTX_sess_cache_full(const SSL_CTX *ctx) { return 0; }