blob: 1328cd5ea02762ef4e758eb349f8ed493487f356 [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 /* SSL_clear may be called before or after the |ssl| is initialized in either
Adam Langleyfcf25832014-12-18 17:42:32 -0800185 * accept or connect state. In the latter case, SSL_clear should preserve the
David Benjamin4831c332015-05-16 11:43:13 -0400186 * half and reset |ssl->state| accordingly. */
187 if (ssl->handshake_func != NULL) {
188 if (ssl->server) {
189 SSL_set_accept_state(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800190 } else {
David Benjamin4831c332015-05-16 11:43:13 -0400191 SSL_set_connect_state(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800192 }
193 } else {
David Benjamin4831c332015-05-16 11:43:13 -0400194 assert(ssl->state == 0);
Adam Langleyfcf25832014-12-18 17:42:32 -0800195 }
Adam Langley95c29f32014-06-20 12:00:00 -0700196
David Benjamin5f387e32015-05-15 22:05:42 -0400197 /* TODO(davidben): Some state on |ssl| is reset both in |SSL_new| and
David Benjamin62fd1622015-01-11 13:30:01 -0500198 * |SSL_clear| because it is per-connection state rather than configuration
David Benjamin4831c332015-05-16 11:43:13 -0400199 * state. Per-connection state should be on |ssl->s3| and |ssl->d1| so it is
David Benjamin62fd1622015-01-11 13:30:01 -0500200 * naturally reset at the right points between |SSL_new|, |SSL_clear|, and
201 * |ssl3_new|. */
202
David Benjamin4831c332015-05-16 11:43:13 -0400203 ssl->rwstate = SSL_NOTHING;
204 ssl->rstate = SSL_ST_READ_HEADER;
Adam Langley95c29f32014-06-20 12:00:00 -0700205
David Benjamin4831c332015-05-16 11:43:13 -0400206 BUF_MEM_free(ssl->init_buf);
207 ssl->init_buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700208
David Benjamin4831c332015-05-16 11:43:13 -0400209 ssl->packet = NULL;
210 ssl->packet_length = 0;
David Benjamin62fd1622015-01-11 13:30:01 -0500211
David Benjamin4831c332015-05-16 11:43:13 -0400212 ssl_clear_cipher_ctx(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700213
David Benjamin4831c332015-05-16 11:43:13 -0400214 OPENSSL_free(ssl->next_proto_negotiated);
215 ssl->next_proto_negotiated = NULL;
216 ssl->next_proto_negotiated_len = 0;
David Benjamin62fd1622015-01-11 13:30:01 -0500217
David Benjamin4831c332015-05-16 11:43:13 -0400218 /* The ssl->d1->mtu is simultaneously configuration (preserved across
David Benjamin62fd1622015-01-11 13:30:01 -0500219 * clear) and connection-specific state (gets reset).
220 *
221 * TODO(davidben): Avoid this. */
222 unsigned mtu = 0;
David Benjamin4831c332015-05-16 11:43:13 -0400223 if (ssl->d1 != NULL) {
224 mtu = ssl->d1->mtu;
David Benjamin62fd1622015-01-11 13:30:01 -0500225 }
226
David Benjamin4831c332015-05-16 11:43:13 -0400227 ssl->method->ssl_free(ssl);
228 if (!ssl->method->ssl_new(ssl)) {
David Benjamin62fd1622015-01-11 13:30:01 -0500229 return 0;
230 }
David Benjamin4831c332015-05-16 11:43:13 -0400231 ssl->enc_method = ssl3_get_enc_method(ssl->version);
232 assert(ssl->enc_method != NULL);
David Benjamin62fd1622015-01-11 13:30:01 -0500233
David Benjamin4831c332015-05-16 11:43:13 -0400234 if (SSL_IS_DTLS(ssl) && (SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
235 ssl->d1->mtu = mtu;
David Benjamin62fd1622015-01-11 13:30:01 -0500236 }
237
David Benjamin4831c332015-05-16 11:43:13 -0400238 ssl->client_version = ssl->version;
Adam Langley95c29f32014-06-20 12:00:00 -0700239
Adam Langleyfcf25832014-12-18 17:42:32 -0800240 return 1;
241}
Adam Langley95c29f32014-06-20 12:00:00 -0700242
Adam Langleyfcf25832014-12-18 17:42:32 -0800243SSL *SSL_new(SSL_CTX *ctx) {
244 SSL *s;
Adam Langley95c29f32014-06-20 12:00:00 -0700245
Adam Langleyfcf25832014-12-18 17:42:32 -0800246 if (ctx == NULL) {
247 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_NULL_SSL_CTX);
248 return NULL;
249 }
250 if (ctx->method == NULL) {
251 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
252 return NULL;
253 }
Adam Langley95c29f32014-06-20 12:00:00 -0700254
Adam Langleyfcf25832014-12-18 17:42:32 -0800255 s = (SSL *)OPENSSL_malloc(sizeof(SSL));
256 if (s == NULL) {
257 goto err;
258 }
259 memset(s, 0, sizeof(SSL));
Adam Langley95c29f32014-06-20 12:00:00 -0700260
Adam Langleyfcf25832014-12-18 17:42:32 -0800261 s->min_version = ctx->min_version;
262 s->max_version = ctx->max_version;
David Benjamin1eb367c2014-12-12 18:17:51 -0500263
Adam Langleyfcf25832014-12-18 17:42:32 -0800264 s->options = ctx->options;
265 s->mode = ctx->mode;
266 s->max_cert_list = ctx->max_cert_list;
Adam Langley95c29f32014-06-20 12:00:00 -0700267
David Benjamina5a3eeb2015-03-18 20:26:30 -0400268 s->cert = ssl_cert_dup(ctx->cert);
269 if (s->cert == NULL) {
270 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -0800271 }
Adam Langley95c29f32014-06-20 12:00:00 -0700272
Adam Langleyfcf25832014-12-18 17:42:32 -0800273 s->msg_callback = ctx->msg_callback;
274 s->msg_callback_arg = ctx->msg_callback_arg;
275 s->verify_mode = ctx->verify_mode;
276 s->sid_ctx_length = ctx->sid_ctx_length;
277 assert(s->sid_ctx_length <= sizeof s->sid_ctx);
278 memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));
279 s->verify_callback = ctx->default_verify_callback;
280 s->generate_session_id = ctx->generate_session_id;
Adam Langley95c29f32014-06-20 12:00:00 -0700281
Adam Langleyfcf25832014-12-18 17:42:32 -0800282 s->param = X509_VERIFY_PARAM_new();
283 if (!s->param) {
284 goto err;
285 }
286 X509_VERIFY_PARAM_inherit(s->param, ctx->param);
287 s->quiet_shutdown = ctx->quiet_shutdown;
288 s->max_send_fragment = ctx->max_send_fragment;
Adam Langley95c29f32014-06-20 12:00:00 -0700289
Adam Langley0b5e3902015-05-15 13:08:38 -0700290 CRYPTO_refcount_inc(&ctx->references);
Adam Langleyfcf25832014-12-18 17:42:32 -0800291 s->ctx = ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -0800292 s->tlsext_ticket_expected = 0;
Adam Langley0b5e3902015-05-15 13:08:38 -0700293 CRYPTO_refcount_inc(&ctx->references);
Adam Langleyfcf25832014-12-18 17:42:32 -0800294 s->initial_ctx = ctx;
295 if (ctx->tlsext_ecpointformatlist) {
296 s->tlsext_ecpointformatlist = BUF_memdup(
297 ctx->tlsext_ecpointformatlist, ctx->tlsext_ecpointformatlist_length);
298 if (!s->tlsext_ecpointformatlist) {
299 goto err;
300 }
301 s->tlsext_ecpointformatlist_length = ctx->tlsext_ecpointformatlist_length;
302 }
Adam Langley95c29f32014-06-20 12:00:00 -0700303
Adam Langleyfcf25832014-12-18 17:42:32 -0800304 if (ctx->tlsext_ellipticcurvelist) {
305 s->tlsext_ellipticcurvelist =
306 BUF_memdup(ctx->tlsext_ellipticcurvelist,
307 ctx->tlsext_ellipticcurvelist_length * 2);
308 if (!s->tlsext_ellipticcurvelist) {
309 goto err;
310 }
311 s->tlsext_ellipticcurvelist_length = ctx->tlsext_ellipticcurvelist_length;
312 }
313 s->next_proto_negotiated = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700314
Adam Langleyfcf25832014-12-18 17:42:32 -0800315 if (s->ctx->alpn_client_proto_list) {
316 s->alpn_client_proto_list = BUF_memdup(s->ctx->alpn_client_proto_list,
317 s->ctx->alpn_client_proto_list_len);
318 if (s->alpn_client_proto_list == NULL) {
319 goto err;
320 }
321 s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;
322 }
Adam Langley95c29f32014-06-20 12:00:00 -0700323
Adam Langleyfcf25832014-12-18 17:42:32 -0800324 s->verify_result = X509_V_OK;
325 s->method = ctx->method;
Adam Langley95c29f32014-06-20 12:00:00 -0700326
Adam Langleyfcf25832014-12-18 17:42:32 -0800327 if (!s->method->ssl_new(s)) {
328 goto err;
329 }
330 s->enc_method = ssl3_get_enc_method(s->version);
331 assert(s->enc_method != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700332
David Benjamin62fd1622015-01-11 13:30:01 -0500333 s->rwstate = SSL_NOTHING;
334 s->rstate = SSL_ST_READ_HEADER;
Adam Langley95c29f32014-06-20 12:00:00 -0700335
David Benjamin9f33fc62015-04-15 17:29:53 -0400336 CRYPTO_new_ex_data(&g_ex_data_class_ssl, s, &s->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700337
Adam Langleyfcf25832014-12-18 17:42:32 -0800338 s->psk_identity_hint = NULL;
339 if (ctx->psk_identity_hint) {
340 s->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
341 if (s->psk_identity_hint == NULL) {
342 goto err;
343 }
344 }
345 s->psk_client_callback = ctx->psk_client_callback;
346 s->psk_server_callback = ctx->psk_server_callback;
Adam Langley95c29f32014-06-20 12:00:00 -0700347
David Benjamin02ddbfd2015-01-11 13:09:11 -0500348 s->tlsext_channel_id_enabled = ctx->tlsext_channel_id_enabled;
349 if (ctx->tlsext_channel_id_private) {
David Benjamin9a10f8f2015-05-05 22:22:40 -0400350 s->tlsext_channel_id_private =
351 EVP_PKEY_up_ref(ctx->tlsext_channel_id_private);
David Benjamin02ddbfd2015-01-11 13:09:11 -0500352 }
353
Adam Langleyfcf25832014-12-18 17:42:32 -0800354 s->signed_cert_timestamps_enabled = s->ctx->signed_cert_timestamps_enabled;
355 s->ocsp_stapling_enabled = s->ctx->ocsp_stapling_enabled;
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200356
Adam Langleyfcf25832014-12-18 17:42:32 -0800357 return s;
358
Adam Langley95c29f32014-06-20 12:00:00 -0700359err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400360 SSL_free(s);
Adam Langleyfcf25832014-12-18 17:42:32 -0800361 OPENSSL_PUT_ERROR(SSL, SSL_new, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700362
Adam Langleyfcf25832014-12-18 17:42:32 -0800363 return NULL;
364}
Adam Langley95c29f32014-06-20 12:00:00 -0700365
Adam Langleyfcf25832014-12-18 17:42:32 -0800366int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
367 unsigned int sid_ctx_len) {
368 if (sid_ctx_len > sizeof ctx->sid_ctx) {
369 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_session_id_context,
370 SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
371 return 0;
372 }
373 ctx->sid_ctx_length = sid_ctx_len;
374 memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700375
Adam Langleyfcf25832014-12-18 17:42:32 -0800376 return 1;
377}
Adam Langley95c29f32014-06-20 12:00:00 -0700378
Adam Langleyfcf25832014-12-18 17:42:32 -0800379int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
380 unsigned int sid_ctx_len) {
381 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
382 OPENSSL_PUT_ERROR(SSL, SSL_set_session_id_context,
383 SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
384 return 0;
385 }
386 ssl->sid_ctx_length = sid_ctx_len;
387 memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700388
Adam Langleyfcf25832014-12-18 17:42:32 -0800389 return 1;
390}
Adam Langley95c29f32014-06-20 12:00:00 -0700391
Adam Langleyfcf25832014-12-18 17:42:32 -0800392int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800393 ctx->generate_session_id = cb;
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_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800398 ssl->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_has_matching_session_id(const SSL *ssl, const uint8_t *id,
403 unsigned int id_len) {
404 /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how we
405 * can "construct" a session to give us the desired check - ie. to find if
406 * there's a session in the hash table that would conflict with any new
407 * session built out of this id/id_len and the ssl_version in use by this
408 * SSL. */
409 SSL_SESSION r, *p;
Adam Langley95c29f32014-06-20 12:00:00 -0700410
Adam Langleyfcf25832014-12-18 17:42:32 -0800411 if (id_len > sizeof r.session_id) {
412 return 0;
413 }
Adam Langley95c29f32014-06-20 12:00:00 -0700414
Adam Langleyfcf25832014-12-18 17:42:32 -0800415 r.ssl_version = ssl->version;
416 r.session_id_length = id_len;
417 memcpy(r.session_id, id, id_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700418
Adam Langley4bdb6e42015-05-15 15:29:21 -0700419 CRYPTO_MUTEX_lock_read(&ssl->ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -0800420 p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r);
Adam Langley4bdb6e42015-05-15 15:29:21 -0700421 CRYPTO_MUTEX_unlock(&ssl->ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -0800422 return p != NULL;
423}
Adam Langley95c29f32014-06-20 12:00:00 -0700424
Adam Langleyfcf25832014-12-18 17:42:32 -0800425int SSL_CTX_set_purpose(SSL_CTX *s, int purpose) {
426 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
427}
428
429int SSL_set_purpose(SSL *s, int purpose) {
430 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
431}
432
433int SSL_CTX_set_trust(SSL_CTX *s, int trust) {
434 return X509_VERIFY_PARAM_set_trust(s->param, trust);
435}
436
437int SSL_set_trust(SSL *s, int trust) {
438 return X509_VERIFY_PARAM_set_trust(s->param, trust);
439}
440
441int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm) {
442 return X509_VERIFY_PARAM_set1(ctx->param, vpm);
443}
444
445int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm) {
446 return X509_VERIFY_PARAM_set1(ssl->param, vpm);
447}
Adam Langley95c29f32014-06-20 12:00:00 -0700448
Adam Langley858a88d2014-06-20 12:00:00 -0700449void ssl_cipher_preference_list_free(
Adam Langleyfcf25832014-12-18 17:42:32 -0800450 struct ssl_cipher_preference_list_st *cipher_list) {
David Benjamin5d1ec732015-04-22 13:38:00 -0400451 if (cipher_list == NULL) {
452 return;
453 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800454 sk_SSL_CIPHER_free(cipher_list->ciphers);
455 OPENSSL_free(cipher_list->in_group_flags);
456 OPENSSL_free(cipher_list);
457}
Adam Langley858a88d2014-06-20 12:00:00 -0700458
Adam Langleyfcf25832014-12-18 17:42:32 -0800459struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_dup(
460 struct ssl_cipher_preference_list_st *cipher_list) {
461 struct ssl_cipher_preference_list_st *ret = NULL;
462 size_t n = sk_SSL_CIPHER_num(cipher_list->ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700463
Adam Langleyfcf25832014-12-18 17:42:32 -0800464 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
465 if (!ret) {
466 goto err;
467 }
468
469 ret->ciphers = NULL;
470 ret->in_group_flags = NULL;
471 ret->ciphers = sk_SSL_CIPHER_dup(cipher_list->ciphers);
472 if (!ret->ciphers) {
473 goto err;
474 }
475 ret->in_group_flags = BUF_memdup(cipher_list->in_group_flags, n);
476 if (!ret->in_group_flags) {
477 goto err;
478 }
479
480 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -0700481
482err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400483 ssl_cipher_preference_list_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -0800484 return NULL;
485}
Adam Langley858a88d2014-06-20 12:00:00 -0700486
Adam Langleyfcf25832014-12-18 17:42:32 -0800487struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_from_ciphers(
David Benjamin60da0cd2015-05-03 15:21:28 -0400488 STACK_OF(SSL_CIPHER) *ciphers) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800489 struct ssl_cipher_preference_list_st *ret = NULL;
490 size_t n = sk_SSL_CIPHER_num(ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700491
Adam Langleyfcf25832014-12-18 17:42:32 -0800492 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
493 if (!ret) {
494 goto err;
495 }
496 ret->ciphers = NULL;
497 ret->in_group_flags = NULL;
498 ret->ciphers = sk_SSL_CIPHER_dup(ciphers);
499 if (!ret->ciphers) {
500 goto err;
501 }
502 ret->in_group_flags = OPENSSL_malloc(n);
503 if (!ret->in_group_flags) {
504 goto err;
505 }
506 memset(ret->in_group_flags, 0, n);
507 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -0700508
509err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400510 ssl_cipher_preference_list_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -0800511 return NULL;
512}
Adam Langley858a88d2014-06-20 12:00:00 -0700513
Adam Langleyfcf25832014-12-18 17:42:32 -0800514X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) { return ctx->param; }
Adam Langley95c29f32014-06-20 12:00:00 -0700515
Adam Langleyfcf25832014-12-18 17:42:32 -0800516X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) { return ssl->param; }
Adam Langley95c29f32014-06-20 12:00:00 -0700517
Adam Langleyfcf25832014-12-18 17:42:32 -0800518void SSL_certs_clear(SSL *s) { ssl_cert_clear_certs(s->cert); }
Adam Langley95c29f32014-06-20 12:00:00 -0700519
David Benjamin4831c332015-05-16 11:43:13 -0400520void SSL_free(SSL *ssl) {
521 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800522 return;
523 }
Adam Langley95c29f32014-06-20 12:00:00 -0700524
David Benjamin4831c332015-05-16 11:43:13 -0400525 X509_VERIFY_PARAM_free(ssl->param);
Adam Langley95c29f32014-06-20 12:00:00 -0700526
David Benjamin4831c332015-05-16 11:43:13 -0400527 CRYPTO_free_ex_data(&g_ex_data_class_ssl, ssl, &ssl->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700528
David Benjamin4831c332015-05-16 11:43:13 -0400529 if (ssl->bbio != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800530 /* If the buffering BIO is in place, pop it off */
David Benjamin4831c332015-05-16 11:43:13 -0400531 if (ssl->bbio == ssl->wbio) {
532 ssl->wbio = BIO_pop(ssl->wbio);
Adam Langleyfcf25832014-12-18 17:42:32 -0800533 }
David Benjamin4831c332015-05-16 11:43:13 -0400534 BIO_free(ssl->bbio);
535 ssl->bbio = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800536 }
Adam Langley95c29f32014-06-20 12:00:00 -0700537
David Benjamin4831c332015-05-16 11:43:13 -0400538 int free_wbio = ssl->wbio != ssl->rbio;
539 BIO_free_all(ssl->rbio);
David Benjamin2755a3e2015-04-22 16:17:58 -0400540 if (free_wbio) {
David Benjamin4831c332015-05-16 11:43:13 -0400541 BIO_free_all(ssl->wbio);
Adam Langleyfcf25832014-12-18 17:42:32 -0800542 }
Adam Langley95c29f32014-06-20 12:00:00 -0700543
David Benjamin4831c332015-05-16 11:43:13 -0400544 BUF_MEM_free(ssl->init_buf);
Adam Langley95c29f32014-06-20 12:00:00 -0700545
Adam Langleyfcf25832014-12-18 17:42:32 -0800546 /* add extra stuff */
David Benjamin4831c332015-05-16 11:43:13 -0400547 ssl_cipher_preference_list_free(ssl->cipher_list);
548 sk_SSL_CIPHER_free(ssl->cipher_list_by_id);
Adam Langley95c29f32014-06-20 12:00:00 -0700549
David Benjamin4831c332015-05-16 11:43:13 -0400550 ssl_clear_bad_session(ssl);
551 SSL_SESSION_free(ssl->session);
Adam Langley95c29f32014-06-20 12:00:00 -0700552
David Benjamin4831c332015-05-16 11:43:13 -0400553 ssl_clear_cipher_ctx(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700554
David Benjamin4831c332015-05-16 11:43:13 -0400555 ssl_cert_free(ssl->cert);
Adam Langley0289c732014-06-20 12:00:00 -0700556
David Benjamin4831c332015-05-16 11:43:13 -0400557 OPENSSL_free(ssl->tlsext_hostname);
558 SSL_CTX_free(ssl->initial_ctx);
559 OPENSSL_free(ssl->tlsext_ecpointformatlist);
560 OPENSSL_free(ssl->tlsext_ellipticcurvelist);
561 OPENSSL_free(ssl->alpn_client_proto_list);
562 EVP_PKEY_free(ssl->tlsext_channel_id_private);
563 OPENSSL_free(ssl->psk_identity_hint);
564 sk_X509_NAME_pop_free(ssl->client_CA, X509_NAME_free);
565 OPENSSL_free(ssl->next_proto_negotiated);
566 sk_SRTP_PROTECTION_PROFILE_free(ssl->srtp_profiles);
Adam Langley95c29f32014-06-20 12:00:00 -0700567
David Benjamin4831c332015-05-16 11:43:13 -0400568 if (ssl->method != NULL) {
569 ssl->method->ssl_free(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800570 }
David Benjamin4831c332015-05-16 11:43:13 -0400571 SSL_CTX_free(ssl->ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700572
David Benjamin4831c332015-05-16 11:43:13 -0400573 OPENSSL_free(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800574}
Adam Langley95c29f32014-06-20 12:00:00 -0700575
Adam Langleyfcf25832014-12-18 17:42:32 -0800576void SSL_set_bio(SSL *s, BIO *rbio, BIO *wbio) {
577 /* If the output buffering BIO is still in place, remove it. */
578 if (s->bbio != NULL) {
579 if (s->wbio == s->bbio) {
580 s->wbio = s->wbio->next_bio;
581 s->bbio->next_bio = NULL;
582 }
583 }
Adam Langley95c29f32014-06-20 12:00:00 -0700584
David Benjamin2755a3e2015-04-22 16:17:58 -0400585 if (s->rbio != rbio) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800586 BIO_free_all(s->rbio);
587 }
David Benjamin2755a3e2015-04-22 16:17:58 -0400588 if (s->wbio != wbio && s->rbio != s->wbio) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800589 BIO_free_all(s->wbio);
590 }
591 s->rbio = rbio;
592 s->wbio = wbio;
593}
Adam Langley95c29f32014-06-20 12:00:00 -0700594
Adam Langleyfcf25832014-12-18 17:42:32 -0800595BIO *SSL_get_rbio(const SSL *s) { return s->rbio; }
Adam Langley95c29f32014-06-20 12:00:00 -0700596
Adam Langleyfcf25832014-12-18 17:42:32 -0800597BIO *SSL_get_wbio(const SSL *s) { return s->wbio; }
Adam Langley95c29f32014-06-20 12:00:00 -0700598
Adam Langleyfcf25832014-12-18 17:42:32 -0800599int SSL_get_fd(const SSL *s) { return SSL_get_rfd(s); }
Adam Langley95c29f32014-06-20 12:00:00 -0700600
Adam Langleyfcf25832014-12-18 17:42:32 -0800601int SSL_get_rfd(const SSL *s) {
602 int ret = -1;
603 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700604
Adam Langleyfcf25832014-12-18 17:42:32 -0800605 b = SSL_get_rbio(s);
606 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
607 if (r != NULL) {
608 BIO_get_fd(r, &ret);
609 }
610 return ret;
611}
Adam Langley95c29f32014-06-20 12:00:00 -0700612
Adam Langleyfcf25832014-12-18 17:42:32 -0800613int SSL_get_wfd(const SSL *s) {
614 int ret = -1;
615 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700616
Adam Langleyfcf25832014-12-18 17:42:32 -0800617 b = SSL_get_wbio(s);
618 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
619 if (r != NULL) {
620 BIO_get_fd(r, &ret);
621 }
Adam Langley95c29f32014-06-20 12:00:00 -0700622
Adam Langleyfcf25832014-12-18 17:42:32 -0800623 return ret;
624}
Adam Langley95c29f32014-06-20 12:00:00 -0700625
Adam Langleyfcf25832014-12-18 17:42:32 -0800626int SSL_set_fd(SSL *s, int fd) {
627 int ret = 0;
628 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700629
Adam Langleyfcf25832014-12-18 17:42:32 -0800630 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700631
Adam Langleyfcf25832014-12-18 17:42:32 -0800632 if (bio == NULL) {
633 OPENSSL_PUT_ERROR(SSL, SSL_set_fd, ERR_R_BUF_LIB);
634 goto err;
635 }
636 BIO_set_fd(bio, fd, BIO_NOCLOSE);
637 SSL_set_bio(s, bio, bio);
638 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700639
Adam Langley95c29f32014-06-20 12:00:00 -0700640err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800641 return ret;
642}
Adam Langley95c29f32014-06-20 12:00:00 -0700643
Adam Langleyfcf25832014-12-18 17:42:32 -0800644int SSL_set_wfd(SSL *s, int fd) {
645 int ret = 0;
646 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700647
Adam Langleyfcf25832014-12-18 17:42:32 -0800648 if (s->rbio == NULL || BIO_method_type(s->rbio) != BIO_TYPE_FD ||
649 (int)BIO_get_fd(s->rbio, NULL) != fd) {
650 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700651
Adam Langleyfcf25832014-12-18 17:42:32 -0800652 if (bio == NULL) {
653 OPENSSL_PUT_ERROR(SSL, SSL_set_wfd, ERR_R_BUF_LIB);
654 goto err;
655 }
656 BIO_set_fd(bio, fd, BIO_NOCLOSE);
657 SSL_set_bio(s, SSL_get_rbio(s), bio);
658 } else {
659 SSL_set_bio(s, SSL_get_rbio(s), SSL_get_rbio(s));
660 }
661
662 ret = 1;
663
Adam Langley95c29f32014-06-20 12:00:00 -0700664err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800665 return ret;
666}
Adam Langley95c29f32014-06-20 12:00:00 -0700667
Adam Langleyfcf25832014-12-18 17:42:32 -0800668int SSL_set_rfd(SSL *s, int fd) {
669 int ret = 0;
670 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700671
Adam Langleyfcf25832014-12-18 17:42:32 -0800672 if (s->wbio == NULL || BIO_method_type(s->wbio) != BIO_TYPE_FD ||
673 (int)BIO_get_fd(s->wbio, NULL) != fd) {
674 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700675
Adam Langleyfcf25832014-12-18 17:42:32 -0800676 if (bio == NULL) {
677 OPENSSL_PUT_ERROR(SSL, SSL_set_rfd, ERR_R_BUF_LIB);
678 goto err;
679 }
680 BIO_set_fd(bio, fd, BIO_NOCLOSE);
681 SSL_set_bio(s, bio, SSL_get_wbio(s));
682 } else {
683 SSL_set_bio(s, SSL_get_wbio(s), SSL_get_wbio(s));
684 }
685 ret = 1;
686
Adam Langley95c29f32014-06-20 12:00:00 -0700687err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800688 return ret;
689}
Adam Langley95c29f32014-06-20 12:00:00 -0700690
691/* return length of latest Finished message we sent, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -0800692size_t SSL_get_finished(const SSL *s, void *buf, size_t count) {
693 size_t ret = 0;
694
695 if (s->s3 != NULL) {
696 ret = s->s3->tmp.finish_md_len;
697 if (count > ret) {
698 count = ret;
699 }
700 memcpy(buf, s->s3->tmp.finish_md, count);
701 }
702
703 return ret;
704}
Adam Langley95c29f32014-06-20 12:00:00 -0700705
706/* return length of latest Finished message we expected, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -0800707size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count) {
708 size_t ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700709
Adam Langleyfcf25832014-12-18 17:42:32 -0800710 if (s->s3 != NULL) {
711 ret = s->s3->tmp.peer_finish_md_len;
712 if (count > ret) {
713 count = ret;
714 }
715 memcpy(buf, s->s3->tmp.peer_finish_md, count);
716 }
Adam Langley95c29f32014-06-20 12:00:00 -0700717
Adam Langleyfcf25832014-12-18 17:42:32 -0800718 return ret;
719}
Adam Langley95c29f32014-06-20 12:00:00 -0700720
Adam Langleyfcf25832014-12-18 17:42:32 -0800721int SSL_get_verify_mode(const SSL *s) { return s->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -0700722
Adam Langleyfcf25832014-12-18 17:42:32 -0800723int SSL_get_verify_depth(const SSL *s) {
724 return X509_VERIFY_PARAM_get_depth(s->param);
725}
Adam Langley95c29f32014-06-20 12:00:00 -0700726
Adam Langleyfcf25832014-12-18 17:42:32 -0800727int (*SSL_get_verify_callback(const SSL *s))(int, X509_STORE_CTX *) {
728 return s->verify_callback;
729}
Adam Langley95c29f32014-06-20 12:00:00 -0700730
Adam Langleyfcf25832014-12-18 17:42:32 -0800731int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) { return ctx->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -0700732
Adam Langleyfcf25832014-12-18 17:42:32 -0800733int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) {
734 return X509_VERIFY_PARAM_get_depth(ctx->param);
735}
Adam Langley95c29f32014-06-20 12:00:00 -0700736
Adam Langleyfcf25832014-12-18 17:42:32 -0800737int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int, X509_STORE_CTX *) {
738 return ctx->default_verify_callback;
739}
Adam Langley95c29f32014-06-20 12:00:00 -0700740
Adam Langleyfcf25832014-12-18 17:42:32 -0800741void SSL_set_verify(SSL *s, int mode,
742 int (*callback)(int ok, X509_STORE_CTX *ctx)) {
743 s->verify_mode = mode;
744 if (callback != NULL) {
745 s->verify_callback = callback;
746 }
747}
Adam Langley95c29f32014-06-20 12:00:00 -0700748
Adam Langleyfcf25832014-12-18 17:42:32 -0800749void SSL_set_verify_depth(SSL *s, int depth) {
750 X509_VERIFY_PARAM_set_depth(s->param, depth);
751}
Adam Langley95c29f32014-06-20 12:00:00 -0700752
David Benjamin9a41d1b2015-05-16 01:30:09 -0400753int SSL_CTX_get_read_ahead(const SSL_CTX *ctx) { return 0; }
Adam Langley95c29f32014-06-20 12:00:00 -0700754
David Benjamin9a41d1b2015-05-16 01:30:09 -0400755int SSL_get_read_ahead(const SSL *s) { return 0; }
Adam Langley95c29f32014-06-20 12:00:00 -0700756
David Benjamin9a41d1b2015-05-16 01:30:09 -0400757void SSL_CTX_set_read_ahead(SSL_CTX *ctx, int yes) { }
David Benjamin61ecccf2015-05-05 09:44:51 -0400758
David Benjamin9a41d1b2015-05-16 01:30:09 -0400759void SSL_set_read_ahead(SSL *s, int yes) { }
David Benjamin61ecccf2015-05-05 09:44:51 -0400760
Adam Langleyfcf25832014-12-18 17:42:32 -0800761int SSL_pending(const SSL *s) {
David Benjamin904dc722015-05-30 16:39:07 -0400762 if (s->rstate == SSL_ST_READ_BODY) {
763 return 0;
764 }
765
766 return (s->s3->rrec.type == SSL3_RT_APPLICATION_DATA) ? s->s3->rrec.length
767 : 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800768}
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
David Benjamin904dc722015-05-30 16:39:07 -0400890 ERR_clear_system_error();
891 return s->method->ssl_read_app_data(s, buf, num, 0);
Adam Langleyfcf25832014-12-18 17:42:32 -0800892}
Adam Langley95c29f32014-06-20 12:00:00 -0700893
Adam Langleyfcf25832014-12-18 17:42:32 -0800894int SSL_peek(SSL *s, void *buf, int num) {
895 if (s->handshake_func == 0) {
896 OPENSSL_PUT_ERROR(SSL, SSL_peek, SSL_R_UNINITIALIZED);
897 return -1;
898 }
Adam Langley95c29f32014-06-20 12:00:00 -0700899
Adam Langleyfcf25832014-12-18 17:42:32 -0800900 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
901 return 0;
902 }
Adam Langley95c29f32014-06-20 12:00:00 -0700903
David Benjamin904dc722015-05-30 16:39:07 -0400904 ERR_clear_system_error();
905 return s->method->ssl_read_app_data(s, buf, num, 1);
Adam Langleyfcf25832014-12-18 17:42:32 -0800906}
Adam Langley95c29f32014-06-20 12:00:00 -0700907
Adam Langleyfcf25832014-12-18 17:42:32 -0800908int SSL_write(SSL *s, const void *buf, int num) {
909 if (s->handshake_func == 0) {
910 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_UNINITIALIZED);
911 return -1;
912 }
Adam Langley95c29f32014-06-20 12:00:00 -0700913
Adam Langleyfcf25832014-12-18 17:42:32 -0800914 if (s->shutdown & SSL_SENT_SHUTDOWN) {
915 s->rwstate = SSL_NOTHING;
916 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_PROTOCOL_IS_SHUTDOWN);
917 return -1;
918 }
Adam Langley95c29f32014-06-20 12:00:00 -0700919
David Benjamin904dc722015-05-30 16:39:07 -0400920 ERR_clear_system_error();
921 return s->method->ssl_write_app_data(s, buf, num);
Adam Langleyfcf25832014-12-18 17:42:32 -0800922}
Adam Langley95c29f32014-06-20 12:00:00 -0700923
Adam Langleyfcf25832014-12-18 17:42:32 -0800924int SSL_shutdown(SSL *s) {
925 /* Note that this function behaves differently from what one might expect.
926 * Return values are 0 for no success (yet), 1 for success; but calling it
927 * once is usually not enough, even if blocking I/O is used (see
928 * ssl3_shutdown). */
Adam Langley95c29f32014-06-20 12:00:00 -0700929
Adam Langleyfcf25832014-12-18 17:42:32 -0800930 if (s->handshake_func == 0) {
931 OPENSSL_PUT_ERROR(SSL, SSL_shutdown, SSL_R_UNINITIALIZED);
932 return -1;
933 }
Adam Langley95c29f32014-06-20 12:00:00 -0700934
David Benjamin904dc722015-05-30 16:39:07 -0400935 if (SSL_in_init(s)) {
936 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800937 }
Adam Langley95c29f32014-06-20 12:00:00 -0700938
David Benjamin904dc722015-05-30 16:39:07 -0400939 /* Do nothing if configured not to send a close_notify. */
940 if (s->quiet_shutdown) {
941 s->shutdown = SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN;
942 return 1;
943 }
944
945 if (!(s->shutdown & SSL_SENT_SHUTDOWN)) {
946 s->shutdown |= SSL_SENT_SHUTDOWN;
947 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_CLOSE_NOTIFY);
948
949 /* our shutdown alert has been sent now, and if it still needs to be
950 * written, s->s3->alert_dispatch will be true */
951 if (s->s3->alert_dispatch) {
952 return -1; /* return WANT_WRITE */
953 }
954 } else if (s->s3->alert_dispatch) {
955 /* resend it if not sent */
956 int ret = s->method->ssl_dispatch_alert(s);
957 if (ret == -1) {
958 /* we only get to return -1 here the 2nd/Nth invocation, we must have
959 * already signalled return 0 upon a previous invoation, return
960 * WANT_WRITE */
961 return ret;
962 }
963 } else if (!(s->shutdown & SSL_RECEIVED_SHUTDOWN)) {
964 /* If we are waiting for a close from our peer, we are closed */
965 s->method->ssl_read_close_notify(s);
966 if (!(s->shutdown & SSL_RECEIVED_SHUTDOWN)) {
967 return -1; /* return WANT_READ */
968 }
969 }
970
971 if (s->shutdown == (SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN) &&
972 !s->s3->alert_dispatch) {
973 return 1;
974 } else {
975 return 0;
976 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800977}
Adam Langley95c29f32014-06-20 12:00:00 -0700978
David Benjamin44d3eed2015-05-21 01:29:55 -0400979int SSL_renegotiate(SSL *ssl) {
980 /* Caller-initiated renegotiation is not supported. */
981 OPENSSL_PUT_ERROR(SSL, SSL_renegotiate, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
982 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800983}
Adam Langley95c29f32014-06-20 12:00:00 -0700984
David Benjamin44d3eed2015-05-21 01:29:55 -0400985int SSL_renegotiate_pending(SSL *ssl) {
986 return SSL_in_init(ssl) && ssl->s3->initial_handshake_complete;
Adam Langleyfcf25832014-12-18 17:42:32 -0800987}
Adam Langley95c29f32014-06-20 12:00:00 -0700988
David Benjamin61ecccf2015-05-05 09:44:51 -0400989uint32_t SSL_CTX_set_options(SSL_CTX *ctx, uint32_t options) {
990 ctx->options |= options;
991 return ctx->options;
992}
993
994uint32_t SSL_set_options(SSL *ssl, uint32_t options) {
995 ssl->options |= options;
996 return ssl->options;
997}
998
999uint32_t SSL_CTX_clear_options(SSL_CTX *ctx, uint32_t options) {
1000 ctx->options &= ~options;
1001 return ctx->options;
1002}
1003
1004uint32_t SSL_clear_options(SSL *ssl, uint32_t options) {
1005 ssl->options &= ~options;
1006 return ssl->options;
1007}
1008
1009uint32_t SSL_CTX_get_options(const SSL_CTX *ctx) { return ctx->options; }
1010
1011uint32_t SSL_get_options(const SSL *ssl) { return ssl->options; }
1012
1013uint32_t SSL_CTX_set_mode(SSL_CTX *ctx, uint32_t mode) {
1014 ctx->mode |= mode;
1015 return ctx->mode;
1016}
1017
1018uint32_t SSL_set_mode(SSL *ssl, uint32_t mode) {
1019 ssl->mode |= mode;
1020 return ssl->mode;
1021}
1022
1023uint32_t SSL_CTX_clear_mode(SSL_CTX *ctx, uint32_t mode) {
1024 ctx->mode &= ~mode;
1025 return ctx->mode;
1026}
1027
1028uint32_t SSL_clear_mode(SSL *ssl, uint32_t mode) {
1029 ssl->mode &= ~mode;
1030 return ssl->mode;
1031}
1032
1033uint32_t SSL_CTX_get_mode(const SSL_CTX *ctx) { return ctx->mode; }
1034
1035uint32_t SSL_get_mode(const SSL *ssl) { return ssl->mode; }
1036
David Benjamin1d0a1942015-04-26 15:35:35 -04001037size_t SSL_CTX_get_max_cert_list(const SSL_CTX *ctx) {
1038 return ctx->max_cert_list;
1039}
1040
1041void SSL_CTX_set_max_cert_list(SSL_CTX *ctx, size_t max_cert_list) {
1042 if (max_cert_list > kMaxHandshakeSize) {
1043 max_cert_list = kMaxHandshakeSize;
1044 }
1045 ctx->max_cert_list = (uint32_t)max_cert_list;
1046}
1047
1048size_t SSL_get_max_cert_list(const SSL *ssl) {
1049 return ssl->max_cert_list;
1050}
1051
1052void SSL_set_max_cert_list(SSL *ssl, size_t max_cert_list) {
1053 if (max_cert_list > kMaxHandshakeSize) {
1054 max_cert_list = kMaxHandshakeSize;
1055 }
1056 ssl->max_cert_list = (uint32_t)max_cert_list;
1057}
1058
1059void SSL_CTX_set_max_send_fragment(SSL_CTX *ctx, size_t max_send_fragment) {
1060 if (max_send_fragment < 512) {
1061 max_send_fragment = 512;
1062 }
1063 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1064 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1065 }
1066 ctx->max_send_fragment = (uint16_t)max_send_fragment;
1067}
1068
1069void SSL_set_max_send_fragment(SSL *ssl, size_t max_send_fragment) {
1070 if (max_send_fragment < 512) {
1071 max_send_fragment = 512;
1072 }
1073 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1074 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1075 }
1076 ssl->max_send_fragment = (uint16_t)max_send_fragment;
1077}
1078
David Benjamincb9cf792015-05-05 09:46:14 -04001079int SSL_set_mtu(SSL *ssl, unsigned mtu) {
1080 if (!SSL_IS_DTLS(ssl) || mtu < dtls1_min_mtu()) {
1081 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001082 }
David Benjamincb9cf792015-05-05 09:46:14 -04001083 ssl->d1->mtu = mtu;
1084 return 1;
1085}
1086
1087int SSL_get_secure_renegotiation_support(const SSL *ssl) {
1088 return ssl->s3->send_connection_binding;
1089}
1090
1091long SSL_ctrl(SSL *s, int cmd, long larg, void *parg) {
1092 return s->method->ssl_ctrl(s, cmd, larg, parg);
Adam Langleyfcf25832014-12-18 17:42:32 -08001093}
1094
Adam Langleyfcf25832014-12-18 17:42:32 -08001095LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) { return ctx->sessions; }
1096
David Benjamin71f7d3d2015-05-05 09:46:38 -04001097size_t SSL_CTX_sess_number(const SSL_CTX *ctx) {
1098 return lh_SSL_SESSION_num_items(ctx->sessions);
1099}
1100
1101unsigned long SSL_CTX_sess_set_cache_size(SSL_CTX *ctx, unsigned long size) {
1102 unsigned long ret = ctx->session_cache_size;
1103 ctx->session_cache_size = size;
1104 return ret;
1105}
1106
1107unsigned long SSL_CTX_sess_get_cache_size(const SSL_CTX *ctx) {
1108 return ctx->session_cache_size;
1109}
1110
1111int SSL_CTX_set_session_cache_mode(SSL_CTX *ctx, int mode) {
1112 int ret = ctx->session_cache_mode;
1113 ctx->session_cache_mode = mode;
1114 return ret;
1115}
1116
1117int SSL_CTX_get_session_cache_mode(const SSL_CTX *ctx) {
1118 return ctx->session_cache_mode;
1119}
1120
Adam Langleyfcf25832014-12-18 17:42:32 -08001121long SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg) {
David Benjamin71f7d3d2015-05-05 09:46:38 -04001122 return ctx->method->ssl_ctx_ctrl(ctx, cmd, larg, parg);
Adam Langleyfcf25832014-12-18 17:42:32 -08001123}
1124
Adam Langleyfcf25832014-12-18 17:42:32 -08001125/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001126 * preference */
David Benjamin60da0cd2015-05-03 15:21:28 -04001127STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001128 if (s == NULL) {
1129 return NULL;
1130 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001131
Adam Langleyfcf25832014-12-18 17:42:32 -08001132 if (s->cipher_list != NULL) {
1133 return s->cipher_list->ciphers;
1134 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001135
Adam Langleyfcf25832014-12-18 17:42:32 -08001136 if (s->version >= TLS1_1_VERSION && s->ctx != NULL &&
1137 s->ctx->cipher_list_tls11 != NULL) {
1138 return s->ctx->cipher_list_tls11->ciphers;
1139 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001140
Adam Langleyfcf25832014-12-18 17:42:32 -08001141 if (s->ctx != NULL && s->ctx->cipher_list != NULL) {
1142 return s->ctx->cipher_list->ciphers;
1143 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001144
Adam Langleyfcf25832014-12-18 17:42:32 -08001145 return NULL;
1146}
Adam Langley95c29f32014-06-20 12:00:00 -07001147
Adam Langleyfcf25832014-12-18 17:42:32 -08001148/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001149 * algorithm id */
David Benjamin60da0cd2015-05-03 15:21:28 -04001150STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001151 if (s == NULL) {
1152 return NULL;
1153 }
Adam Langley95c29f32014-06-20 12:00:00 -07001154
Adam Langleyfcf25832014-12-18 17:42:32 -08001155 if (s->cipher_list_by_id != NULL) {
1156 return s->cipher_list_by_id;
1157 }
Adam Langley95c29f32014-06-20 12:00:00 -07001158
Adam Langleyfcf25832014-12-18 17:42:32 -08001159 if (s->ctx != NULL && s->ctx->cipher_list_by_id != NULL) {
1160 return s->ctx->cipher_list_by_id;
1161 }
Adam Langley95c29f32014-06-20 12:00:00 -07001162
Adam Langleyfcf25832014-12-18 17:42:32 -08001163 return NULL;
1164}
Adam Langley95c29f32014-06-20 12:00:00 -07001165
Adam Langleyfcf25832014-12-18 17:42:32 -08001166/* The old interface to get the same thing as SSL_get_ciphers() */
1167const char *SSL_get_cipher_list(const SSL *s, int n) {
1168 const SSL_CIPHER *c;
David Benjamin60da0cd2015-05-03 15:21:28 -04001169 STACK_OF(SSL_CIPHER) *sk;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001170
Adam Langleyfcf25832014-12-18 17:42:32 -08001171 if (s == NULL) {
1172 return NULL;
1173 }
Adam Langley95c29f32014-06-20 12:00:00 -07001174
Adam Langleyfcf25832014-12-18 17:42:32 -08001175 sk = SSL_get_ciphers(s);
1176 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
1177 return NULL;
1178 }
Adam Langley95c29f32014-06-20 12:00:00 -07001179
Adam Langleyfcf25832014-12-18 17:42:32 -08001180 c = sk_SSL_CIPHER_value(sk, n);
1181 if (c == NULL) {
1182 return NULL;
1183 }
Adam Langley95c29f32014-06-20 12:00:00 -07001184
Adam Langleyfcf25832014-12-18 17:42:32 -08001185 return c->name;
1186}
David Benjamin5491e3f2014-09-29 19:33:09 -04001187
Adam Langleyfcf25832014-12-18 17:42:32 -08001188/* specify the ciphers to be used by default by the SSL_CTX */
1189int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
1190 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001191
Adam Langleyfcf25832014-12-18 17:42:32 -08001192 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001193 &ctx->cipher_list_by_id, str);
Adam Langleyfcf25832014-12-18 17:42:32 -08001194 /* ssl_create_cipher_list may return an empty stack if it was unable to find
1195 * a cipher matching the given rule string (for example if the rule string
1196 * specifies a cipher which has been disabled). This is not an error as far
1197 * as ssl_create_cipher_list is concerned, and hence ctx->cipher_list and
1198 * ctx->cipher_list_by_id has been updated. */
1199 if (sk == NULL) {
1200 return 0;
1201 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1202 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1203 return 0;
1204 }
Adam Langley95c29f32014-06-20 12:00:00 -07001205
Adam Langleyfcf25832014-12-18 17:42:32 -08001206 return 1;
1207}
David Benjamin39482a12014-07-20 13:30:15 -04001208
Adam Langleyfcf25832014-12-18 17:42:32 -08001209int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str) {
1210 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001211
David Benjamin71f07942015-04-08 02:36:59 -04001212 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str);
Adam Langleyfcf25832014-12-18 17:42:32 -08001213 if (sk == NULL) {
1214 return 0;
1215 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1216 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list_tls11,
1217 SSL_R_NO_CIPHER_MATCH);
1218 return 0;
1219 }
David Benjamin9f2c0d72014-10-21 22:00:19 -04001220
Adam Langleyfcf25832014-12-18 17:42:32 -08001221 return 1;
1222}
Adam Langley95c29f32014-06-20 12:00:00 -07001223
Adam Langleyfcf25832014-12-18 17:42:32 -08001224/* specify the ciphers to be used by the SSL */
1225int SSL_set_cipher_list(SSL *s, const char *str) {
1226 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001227
Adam Langleyfcf25832014-12-18 17:42:32 -08001228 sk = ssl_create_cipher_list(s->ctx->method, &s->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001229 &s->cipher_list_by_id, str);
David Benjamin39482a12014-07-20 13:30:15 -04001230
Adam Langleyfcf25832014-12-18 17:42:32 -08001231 /* see comment in SSL_CTX_set_cipher_list */
1232 if (sk == NULL) {
1233 return 0;
1234 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1235 OPENSSL_PUT_ERROR(SSL, SSL_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1236 return 0;
1237 }
David Benjamin39482a12014-07-20 13:30:15 -04001238
Adam Langleyfcf25832014-12-18 17:42:32 -08001239 return 1;
1240}
Adam Langley95c29f32014-06-20 12:00:00 -07001241
Adam Langleyfcf25832014-12-18 17:42:32 -08001242int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, uint8_t *p) {
1243 size_t i;
1244 const SSL_CIPHER *c;
1245 CERT *ct = s->cert;
1246 uint8_t *q;
1247 /* Set disabled masks for this session */
1248 ssl_set_client_disabled(s);
Adam Langley29707792014-06-20 12:00:00 -07001249
Adam Langleyfcf25832014-12-18 17:42:32 -08001250 if (sk == NULL) {
1251 return 0;
1252 }
1253 q = p;
Adam Langley95c29f32014-06-20 12:00:00 -07001254
Adam Langleyfcf25832014-12-18 17:42:32 -08001255 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
1256 c = sk_SSL_CIPHER_value(sk, i);
1257 /* Skip disabled ciphers */
1258 if (c->algorithm_ssl & ct->mask_ssl ||
1259 c->algorithm_mkey & ct->mask_k ||
1260 c->algorithm_auth & ct->mask_a) {
1261 continue;
1262 }
David Benjamina1c90a52015-05-30 17:03:14 -04001263 s2n(ssl_cipher_get_value(c), p);
Adam Langleyfcf25832014-12-18 17:42:32 -08001264 }
1265
1266 /* If all ciphers were disabled, return the error to the caller. */
1267 if (p == q) {
1268 return 0;
1269 }
1270
1271 /* Add SCSVs. */
David Benjamin20f6e972015-05-15 21:51:49 -04001272 if (!s->s3->initial_handshake_complete) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001273 s2n(SSL3_CK_SCSV & 0xffff, p);
1274 }
1275
Adam Langley5f0efe02015-02-20 13:03:16 -08001276 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001277 s2n(SSL3_CK_FALLBACK_SCSV & 0xffff, p);
1278 }
1279
1280 return p - q;
1281}
1282
1283STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs) {
1284 CBS cipher_suites = *cbs;
1285 const SSL_CIPHER *c;
David Benjamin60da0cd2015-05-03 15:21:28 -04001286 STACK_OF(SSL_CIPHER) *sk;
Adam Langleyfcf25832014-12-18 17:42:32 -08001287
1288 if (s->s3) {
1289 s->s3->send_connection_binding = 0;
1290 }
1291
1292 if (CBS_len(&cipher_suites) % 2 != 0) {
1293 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1294 SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
1295 return NULL;
1296 }
1297
1298 sk = sk_SSL_CIPHER_new_null();
1299 if (sk == NULL) {
1300 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1301 goto err;
1302 }
1303
Adam Langleyfcf25832014-12-18 17:42:32 -08001304 while (CBS_len(&cipher_suites) > 0) {
1305 uint16_t cipher_suite;
1306
1307 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
1308 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_INTERNAL_ERROR);
1309 goto err;
1310 }
1311
1312 /* Check for SCSV. */
1313 if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff)) {
1314 /* SCSV is fatal if renegotiating. */
David Benjamin20f6e972015-05-15 21:51:49 -04001315 if (s->s3->initial_handshake_complete) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001316 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1317 SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1318 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1319 goto err;
1320 }
1321 s->s3->send_connection_binding = 1;
1322 continue;
1323 }
1324
1325 /* Check for FALLBACK_SCSV. */
1326 if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff)) {
1327 uint16_t max_version = ssl3_get_max_server_version(s);
1328 if (SSL_IS_DTLS(s) ? (uint16_t)s->version > max_version
1329 : (uint16_t)s->version < max_version) {
1330 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1331 SSL_R_INAPPROPRIATE_FALLBACK);
1332 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
1333 goto err;
1334 }
1335 continue;
1336 }
1337
David Benjamina1c90a52015-05-30 17:03:14 -04001338 c = SSL_get_cipher_by_value(cipher_suite);
Adam Langleyfcf25832014-12-18 17:42:32 -08001339 if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
1340 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1341 goto err;
1342 }
1343 }
1344
1345 return sk;
David Benjamin9f2c0d72014-10-21 22:00:19 -04001346
Adam Langley95c29f32014-06-20 12:00:00 -07001347err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001348 sk_SSL_CIPHER_free(sk);
Adam Langleyfcf25832014-12-18 17:42:32 -08001349 return NULL;
1350}
Adam Langley95c29f32014-06-20 12:00:00 -07001351
1352
Adam Langleyfcf25832014-12-18 17:42:32 -08001353/* return a servername extension value if provided in Client Hello, or NULL. So
1354 * far, only host_name types are defined (RFC 3546). */
1355const char *SSL_get_servername(const SSL *s, const int type) {
1356 if (type != TLSEXT_NAMETYPE_host_name) {
1357 return NULL;
1358 }
Adam Langley95c29f32014-06-20 12:00:00 -07001359
Adam Langleyfcf25832014-12-18 17:42:32 -08001360 return s->session && !s->tlsext_hostname ? s->session->tlsext_hostname
1361 : s->tlsext_hostname;
1362}
Adam Langley95c29f32014-06-20 12:00:00 -07001363
Adam Langleyfcf25832014-12-18 17:42:32 -08001364int SSL_get_servername_type(const SSL *s) {
1365 if (s->session &&
1366 (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname)) {
1367 return TLSEXT_NAMETYPE_host_name;
1368 }
Adam Langley95c29f32014-06-20 12:00:00 -07001369
Adam Langleyfcf25832014-12-18 17:42:32 -08001370 return -1;
1371}
Adam Langley95c29f32014-06-20 12:00:00 -07001372
Adam Langleyfcf25832014-12-18 17:42:32 -08001373void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
1374 ctx->signed_cert_timestamps_enabled = 1;
1375}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001376
Adam Langleyfcf25832014-12-18 17:42:32 -08001377int SSL_enable_signed_cert_timestamps(SSL *ssl) {
1378 ssl->signed_cert_timestamps_enabled = 1;
1379 return 1;
1380}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001381
Adam Langleyfcf25832014-12-18 17:42:32 -08001382void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
1383 ctx->ocsp_stapling_enabled = 1;
1384}
David Benjamin6c7aed02014-08-27 16:42:38 -04001385
Adam Langleyfcf25832014-12-18 17:42:32 -08001386int SSL_enable_ocsp_stapling(SSL *ssl) {
1387 ssl->ocsp_stapling_enabled = 1;
1388 return 1;
1389}
David Benjamin6c7aed02014-08-27 16:42:38 -04001390
Adam Langleyfcf25832014-12-18 17:42:32 -08001391void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
1392 size_t *out_len) {
1393 SSL_SESSION *session = ssl->session;
Adam Langley3cb50e02014-08-26 14:00:31 -07001394
Adam Langleyfcf25832014-12-18 17:42:32 -08001395 *out_len = 0;
1396 *out = NULL;
1397 if (ssl->server || !session || !session->tlsext_signed_cert_timestamp_list) {
1398 return;
1399 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001400
Adam Langleyfcf25832014-12-18 17:42:32 -08001401 *out = session->tlsext_signed_cert_timestamp_list;
1402 *out_len = session->tlsext_signed_cert_timestamp_list_length;
1403}
David Benjamin6c7aed02014-08-27 16:42:38 -04001404
Adam Langleyfcf25832014-12-18 17:42:32 -08001405void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
1406 size_t *out_len) {
1407 SSL_SESSION *session = ssl->session;
1408
1409 *out_len = 0;
1410 *out = NULL;
1411 if (ssl->server || !session || !session->ocsp_response) {
1412 return;
1413 }
1414 *out = session->ocsp_response;
1415 *out_len = session->ocsp_response_length;
1416}
David Benjamin6c7aed02014-08-27 16:42:38 -04001417
Adam Langley95c29f32014-06-20 12:00:00 -07001418/* SSL_select_next_proto implements the standard protocol selection. It is
1419 * expected that this function is called from the callback set by
1420 * SSL_CTX_set_next_proto_select_cb.
1421 *
1422 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1423 * strings. The length byte itself is not included in the length. A byte
1424 * string of length 0 is invalid. No byte string may be truncated.
1425 *
1426 * The current, but experimental algorithm for selecting the protocol is:
1427 *
1428 * 1) If the server doesn't support NPN then this is indicated to the
1429 * callback. In this case, the client application has to abort the connection
1430 * or have a default application level protocol.
1431 *
1432 * 2) If the server supports NPN, but advertises an empty list then the
1433 * client selects the first protcol in its list, but indicates via the
1434 * API that this fallback case was enacted.
1435 *
1436 * 3) Otherwise, the client finds the first protocol in the server's list
1437 * that it supports and selects this protocol. This is because it's
1438 * assumed that the server has better information about which protocol
1439 * a client should use.
1440 *
1441 * 4) If the client doesn't support any of the server's advertised
1442 * protocols, then this is treated the same as case 2.
1443 *
1444 * It returns either
1445 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1446 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1447 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001448int SSL_select_next_proto(uint8_t **out, uint8_t *outlen, const uint8_t *server,
1449 unsigned int server_len, const uint8_t *client,
1450 unsigned int client_len) {
1451 unsigned int i, j;
1452 const uint8_t *result;
1453 int status = OPENSSL_NPN_UNSUPPORTED;
Adam Langley95c29f32014-06-20 12:00:00 -07001454
Adam Langleyfcf25832014-12-18 17:42:32 -08001455 /* For each protocol in server preference order, see if we support it. */
1456 for (i = 0; i < server_len;) {
1457 for (j = 0; j < client_len;) {
1458 if (server[i] == client[j] &&
1459 memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
1460 /* We found a match */
1461 result = &server[i];
1462 status = OPENSSL_NPN_NEGOTIATED;
1463 goto found;
1464 }
1465 j += client[j];
1466 j++;
1467 }
1468 i += server[i];
1469 i++;
1470 }
Adam Langley95c29f32014-06-20 12:00:00 -07001471
Adam Langleyfcf25832014-12-18 17:42:32 -08001472 /* There's no overlap between our protocols and the server's list. */
1473 result = client;
1474 status = OPENSSL_NPN_NO_OVERLAP;
Adam Langley95c29f32014-06-20 12:00:00 -07001475
Adam Langleyfcf25832014-12-18 17:42:32 -08001476found:
1477 *out = (uint8_t *)result + 1;
1478 *outlen = result[0];
1479 return status;
1480}
Adam Langley95c29f32014-06-20 12:00:00 -07001481
Adam Langley95c29f32014-06-20 12:00:00 -07001482/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
1483 * requested protocol for this connection and returns 0. If the client didn't
1484 * request any protocol, then *data is set to NULL.
1485 *
1486 * Note that the client can request any protocol it chooses. The value returned
1487 * from this function need not be a member of the list of supported protocols
Adam Langleyfcf25832014-12-18 17:42:32 -08001488 * provided by the callback. */
1489void SSL_get0_next_proto_negotiated(const SSL *s, const uint8_t **data,
1490 unsigned *len) {
1491 *data = s->next_proto_negotiated;
1492 if (!*data) {
1493 *len = 0;
1494 } else {
1495 *len = s->next_proto_negotiated_len;
1496 }
Adam Langley95c29f32014-06-20 12:00:00 -07001497}
1498
1499/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
1500 * TLS server needs a list of supported protocols for Next Protocol
1501 * Negotiation. The returned list must be in wire format. The list is returned
1502 * by setting |out| to point to it and |outlen| to its length. This memory will
1503 * not be modified, but one should assume that the SSL* keeps a reference to
1504 * it.
1505 *
Adam Langleyfcf25832014-12-18 17:42:32 -08001506 * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise.
1507 * Otherwise, no such extension will be included in the ServerHello. */
1508void SSL_CTX_set_next_protos_advertised_cb(
1509 SSL_CTX *ctx,
1510 int (*cb)(SSL *ssl, const uint8_t **out, unsigned int *outlen, void *arg),
1511 void *arg) {
1512 ctx->next_protos_advertised_cb = cb;
1513 ctx->next_protos_advertised_cb_arg = arg;
1514}
Adam Langley95c29f32014-06-20 12:00:00 -07001515
1516/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1517 * client needs to select a protocol from the server's provided list. |out|
1518 * must be set to point to the selected protocol (which may be within |in|).
1519 * The length of the protocol name must be written into |outlen|. The server's
1520 * advertised protocols are provided in |in| and |inlen|. The callback can
1521 * assume that |in| is syntactically valid.
1522 *
1523 * The client must select a protocol. It is fatal to the connection if this
1524 * callback returns a value other than SSL_TLSEXT_ERR_OK.
1525 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001526void SSL_CTX_set_next_proto_select_cb(
1527 SSL_CTX *ctx, int (*cb)(SSL *s, uint8_t **out, uint8_t *outlen,
1528 const uint8_t *in, unsigned int inlen, void *arg),
1529 void *arg) {
1530 ctx->next_proto_select_cb = cb;
1531 ctx->next_proto_select_cb_arg = arg;
1532}
Adam Langley95c29f32014-06-20 12:00:00 -07001533
Adam Langleyfcf25832014-12-18 17:42:32 -08001534int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
1535 unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001536 OPENSSL_free(ctx->alpn_client_proto_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001537 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1538 if (!ctx->alpn_client_proto_list) {
1539 return 1;
1540 }
1541 ctx->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001542
Adam Langleyfcf25832014-12-18 17:42:32 -08001543 return 0;
1544}
Adam Langley95c29f32014-06-20 12:00:00 -07001545
Adam Langleyfcf25832014-12-18 17:42:32 -08001546int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001547 OPENSSL_free(ssl->alpn_client_proto_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001548 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1549 if (!ssl->alpn_client_proto_list) {
1550 return 1;
1551 }
1552 ssl->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001553
Adam Langleyfcf25832014-12-18 17:42:32 -08001554 return 0;
1555}
Adam Langley95c29f32014-06-20 12:00:00 -07001556
1557/* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
1558 * during ClientHello processing in order to select an ALPN protocol from the
1559 * client's list of offered protocols. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001560void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
1561 int (*cb)(SSL *ssl, const uint8_t **out,
1562 uint8_t *outlen, const uint8_t *in,
1563 unsigned int inlen, void *arg),
1564 void *arg) {
1565 ctx->alpn_select_cb = cb;
1566 ctx->alpn_select_cb_arg = arg;
1567}
Adam Langley95c29f32014-06-20 12:00:00 -07001568
1569/* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
1570 * On return it sets |*data| to point to |*len| bytes of protocol name (not
1571 * including the leading length-prefix byte). If the server didn't respond with
1572 * a negotiated protocol then |*len| will be zero. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001573void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **data,
1574 unsigned *len) {
1575 *data = NULL;
1576 if (ssl->s3) {
1577 *data = ssl->s3->alpn_selected;
1578 }
1579 if (*data == NULL) {
1580 *len = 0;
1581 } else {
1582 *len = ssl->s3->alpn_selected_len;
1583 }
1584}
Adam Langley95c29f32014-06-20 12:00:00 -07001585
David Benjamincfd248b2015-04-03 11:02:24 -04001586int SSL_export_keying_material(SSL *s, uint8_t *out, size_t out_len,
1587 const char *label, size_t label_len,
1588 const uint8_t *context, size_t context_len,
1589 int use_context) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001590 if (s->version < TLS1_VERSION) {
David Benjaminc565ebb2015-04-03 04:06:36 -04001591 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001592 }
Adam Langley95c29f32014-06-20 12:00:00 -07001593
David Benjamincfd248b2015-04-03 11:02:24 -04001594 return s->enc_method->export_keying_material(
1595 s, out, out_len, label, label_len, context, context_len, use_context);
Adam Langleyfcf25832014-12-18 17:42:32 -08001596}
Adam Langley95c29f32014-06-20 12:00:00 -07001597
Adam Langleyfcf25832014-12-18 17:42:32 -08001598static uint32_t ssl_session_hash(const SSL_SESSION *a) {
1599 uint32_t hash =
1600 ((uint32_t)a->session_id[0]) ||
1601 ((uint32_t)a->session_id[1] << 8) ||
1602 ((uint32_t)a->session_id[2] << 16) ||
1603 ((uint32_t)a->session_id[3] << 24);
Adam Langley95c29f32014-06-20 12:00:00 -07001604
Adam Langleyfcf25832014-12-18 17:42:32 -08001605 return hash;
1606}
Adam Langley95c29f32014-06-20 12:00:00 -07001607
1608/* NB: If this function (or indeed the hash function which uses a sort of
1609 * coarser function than this one) is changed, ensure
1610 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1611 * able to construct an SSL_SESSION that will collide with any existing session
1612 * with a matching session ID. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001613static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
1614 if (a->ssl_version != b->ssl_version) {
1615 return 1;
1616 }
Adam Langley95c29f32014-06-20 12:00:00 -07001617
Adam Langleyfcf25832014-12-18 17:42:32 -08001618 if (a->session_id_length != b->session_id_length) {
1619 return 1;
1620 }
Adam Langley95c29f32014-06-20 12:00:00 -07001621
Adam Langleyfcf25832014-12-18 17:42:32 -08001622 return memcmp(a->session_id, b->session_id, a->session_id_length);
1623}
Adam Langley95c29f32014-06-20 12:00:00 -07001624
David Benjamin4831c332015-05-16 11:43:13 -04001625SSL_CTX *SSL_CTX_new(const SSL_METHOD *method) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001626 SSL_CTX *ret = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001627
David Benjamin4831c332015-05-16 11:43:13 -04001628 if (method == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001629 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_NULL_SSL_METHOD_PASSED);
1630 return NULL;
1631 }
Adam Langley95c29f32014-06-20 12:00:00 -07001632
Adam Langleyfcf25832014-12-18 17:42:32 -08001633 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
1634 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
1635 goto err;
1636 }
Adam Langley95c29f32014-06-20 12:00:00 -07001637
Adam Langleyfcf25832014-12-18 17:42:32 -08001638 ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1639 if (ret == NULL) {
1640 goto err;
1641 }
Adam Langley95c29f32014-06-20 12:00:00 -07001642
Adam Langleyfcf25832014-12-18 17:42:32 -08001643 memset(ret, 0, sizeof(SSL_CTX));
Adam Langley95c29f32014-06-20 12:00:00 -07001644
David Benjamin4831c332015-05-16 11:43:13 -04001645 ret->method = method->method;
Adam Langley95c29f32014-06-20 12:00:00 -07001646
Adam Langley4bdb6e42015-05-15 15:29:21 -07001647 CRYPTO_MUTEX_init(&ret->lock);
1648
Adam Langleyfcf25832014-12-18 17:42:32 -08001649 ret->cert_store = NULL;
1650 ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
1651 ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1652 ret->session_cache_head = NULL;
1653 ret->session_cache_tail = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001654
Adam Langleyfcf25832014-12-18 17:42:32 -08001655 /* We take the system default */
1656 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001657
Adam Langleyfcf25832014-12-18 17:42:32 -08001658 ret->new_session_cb = 0;
1659 ret->remove_session_cb = 0;
1660 ret->get_session_cb = 0;
1661 ret->generate_session_id = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001662
Adam Langleyfcf25832014-12-18 17:42:32 -08001663 ret->references = 1;
1664 ret->quiet_shutdown = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001665
Adam Langleyfcf25832014-12-18 17:42:32 -08001666 ret->info_callback = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001667
Adam Langleyfcf25832014-12-18 17:42:32 -08001668 ret->app_verify_callback = 0;
1669 ret->app_verify_arg = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001670
Adam Langleyfcf25832014-12-18 17:42:32 -08001671 ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
Adam Langleyfcf25832014-12-18 17:42:32 -08001672 ret->msg_callback = 0;
1673 ret->msg_callback_arg = NULL;
1674 ret->verify_mode = SSL_VERIFY_NONE;
1675 ret->sid_ctx_length = 0;
1676 ret->default_verify_callback = NULL;
1677 ret->cert = ssl_cert_new();
1678 if (ret->cert == NULL) {
1679 goto err;
1680 }
Adam Langley95c29f32014-06-20 12:00:00 -07001681
Adam Langleyfcf25832014-12-18 17:42:32 -08001682 ret->default_passwd_callback = 0;
1683 ret->default_passwd_callback_userdata = NULL;
1684 ret->client_cert_cb = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001685
Adam Langleyfcf25832014-12-18 17:42:32 -08001686 ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
1687 if (ret->sessions == NULL) {
1688 goto err;
1689 }
1690 ret->cert_store = X509_STORE_new();
1691 if (ret->cert_store == NULL) {
1692 goto err;
1693 }
Adam Langley95c29f32014-06-20 12:00:00 -07001694
Adam Langleyfcf25832014-12-18 17:42:32 -08001695 ssl_create_cipher_list(ret->method, &ret->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001696 &ret->cipher_list_by_id, SSL_DEFAULT_CIPHER_LIST);
Adam Langleyfcf25832014-12-18 17:42:32 -08001697 if (ret->cipher_list == NULL ||
1698 sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0) {
1699 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_LIBRARY_HAS_NO_CIPHERS);
1700 goto err2;
1701 }
Adam Langley95c29f32014-06-20 12:00:00 -07001702
Adam Langleyfcf25832014-12-18 17:42:32 -08001703 ret->param = X509_VERIFY_PARAM_new();
1704 if (!ret->param) {
1705 goto err;
1706 }
Adam Langley95c29f32014-06-20 12:00:00 -07001707
Adam Langleyfcf25832014-12-18 17:42:32 -08001708 ret->client_CA = sk_X509_NAME_new_null();
1709 if (ret->client_CA == NULL) {
1710 goto err;
1711 }
Adam Langley95c29f32014-06-20 12:00:00 -07001712
David Benjamin9f33fc62015-04-15 17:29:53 -04001713 CRYPTO_new_ex_data(&g_ex_data_class_ssl_ctx, ret, &ret->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001714
Adam Langleyfcf25832014-12-18 17:42:32 -08001715 ret->extra_certs = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001716
Adam Langleyfcf25832014-12-18 17:42:32 -08001717 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
David Benjamin422d3a42014-08-20 11:09:03 -04001718
Adam Langleyfcf25832014-12-18 17:42:32 -08001719 ret->tlsext_servername_callback = 0;
1720 ret->tlsext_servername_arg = NULL;
1721 /* Setup RFC4507 ticket keys */
1722 if (!RAND_bytes(ret->tlsext_tick_key_name, 16) ||
1723 !RAND_bytes(ret->tlsext_tick_hmac_key, 16) ||
1724 !RAND_bytes(ret->tlsext_tick_aes_key, 16)) {
1725 ret->options |= SSL_OP_NO_TICKET;
1726 }
Adam Langley95c29f32014-06-20 12:00:00 -07001727
Adam Langleyfcf25832014-12-18 17:42:32 -08001728 ret->next_protos_advertised_cb = 0;
1729 ret->next_proto_select_cb = 0;
1730 ret->psk_identity_hint = NULL;
1731 ret->psk_client_callback = NULL;
1732 ret->psk_server_callback = NULL;
1733
1734 /* Default is to connect to non-RI servers. When RI is more widely deployed
1735 * might change this. */
1736 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
1737
1738 /* Lock the SSL_CTX to the specified version, for compatibility with legacy
1739 * uses of SSL_METHOD. */
David Benjamin4831c332015-05-16 11:43:13 -04001740 if (method->version != 0) {
1741 SSL_CTX_set_max_version(ret, method->version);
1742 SSL_CTX_set_min_version(ret, method->version);
Adam Langleyfcf25832014-12-18 17:42:32 -08001743 }
1744
1745 return ret;
1746
Adam Langley95c29f32014-06-20 12:00:00 -07001747err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001748 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -07001749err2:
David Benjamin2755a3e2015-04-22 16:17:58 -04001750 SSL_CTX_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -08001751 return NULL;
1752}
Adam Langley95c29f32014-06-20 12:00:00 -07001753
David Benjamin4fcc2e22015-04-22 12:58:16 -04001754void SSL_CTX_free(SSL_CTX *ctx) {
1755 if (ctx == NULL ||
Adam Langley0b5e3902015-05-15 13:08:38 -07001756 !CRYPTO_refcount_dec_and_test_zero(&ctx->references)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001757 return;
1758 }
Adam Langley95c29f32014-06-20 12:00:00 -07001759
David Benjamin2755a3e2015-04-22 16:17:58 -04001760 X509_VERIFY_PARAM_free(ctx->param);
Adam Langley95c29f32014-06-20 12:00:00 -07001761
Adam Langleyfcf25832014-12-18 17:42:32 -08001762 /* Free internal session cache. However: the remove_cb() may reference the
1763 * ex_data of SSL_CTX, thus the ex_data store can only be removed after the
1764 * sessions were flushed. As the ex_data handling routines might also touch
1765 * the session cache, the most secure solution seems to be: empty (flush) the
1766 * cache, then free ex_data, then finally free the cache. (See ticket
1767 * [openssl.org #212].) */
David Benjamin2755a3e2015-04-22 16:17:58 -04001768 SSL_CTX_flush_sessions(ctx, 0);
Adam Langley95c29f32014-06-20 12:00:00 -07001769
David Benjamin4fcc2e22015-04-22 12:58:16 -04001770 CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, ctx, &ctx->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001771
Adam Langley4bdb6e42015-05-15 15:29:21 -07001772 CRYPTO_MUTEX_cleanup(&ctx->lock);
David Benjamin2755a3e2015-04-22 16:17:58 -04001773 lh_SSL_SESSION_free(ctx->sessions);
1774 X509_STORE_free(ctx->cert_store);
1775 ssl_cipher_preference_list_free(ctx->cipher_list);
1776 sk_SSL_CIPHER_free(ctx->cipher_list_by_id);
1777 ssl_cipher_preference_list_free(ctx->cipher_list_tls11);
1778 ssl_cert_free(ctx->cert);
1779 sk_X509_NAME_pop_free(ctx->client_CA, X509_NAME_free);
1780 sk_X509_pop_free(ctx->extra_certs, X509_free);
1781 sk_SRTP_PROTECTION_PROFILE_free(ctx->srtp_profiles);
1782 OPENSSL_free(ctx->psk_identity_hint);
1783 OPENSSL_free(ctx->tlsext_ecpointformatlist);
1784 OPENSSL_free(ctx->tlsext_ellipticcurvelist);
1785 OPENSSL_free(ctx->alpn_client_proto_list);
1786 EVP_PKEY_free(ctx->tlsext_channel_id_private);
1787 BIO_free(ctx->keylog_bio);
Adam Langley95c29f32014-06-20 12:00:00 -07001788
David Benjamin4fcc2e22015-04-22 12:58:16 -04001789 OPENSSL_free(ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08001790}
Adam Langley95c29f32014-06-20 12:00:00 -07001791
Adam Langleyfcf25832014-12-18 17:42:32 -08001792void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) {
1793 ctx->default_passwd_callback = cb;
1794}
Adam Langley95c29f32014-06-20 12:00:00 -07001795
Adam Langleyfcf25832014-12-18 17:42:32 -08001796void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u) {
1797 ctx->default_passwd_callback_userdata = u;
1798}
Adam Langley95c29f32014-06-20 12:00:00 -07001799
Adam Langleyfcf25832014-12-18 17:42:32 -08001800void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
1801 int (*cb)(X509_STORE_CTX *, void *),
1802 void *arg) {
1803 ctx->app_verify_callback = cb;
1804 ctx->app_verify_arg = arg;
1805}
Adam Langley95c29f32014-06-20 12:00:00 -07001806
Adam Langleyfcf25832014-12-18 17:42:32 -08001807void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
1808 int (*cb)(int, X509_STORE_CTX *)) {
1809 ctx->verify_mode = mode;
1810 ctx->default_verify_callback = cb;
1811}
Adam Langley95c29f32014-06-20 12:00:00 -07001812
Adam Langleyfcf25832014-12-18 17:42:32 -08001813void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
1814 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
1815}
Adam Langley1258b6a2014-06-20 12:00:00 -07001816
Adam Langleyfcf25832014-12-18 17:42:32 -08001817void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb)(SSL *ssl, void *arg),
1818 void *arg) {
1819 ssl_cert_set_cert_cb(c->cert, cb, arg);
1820}
David Benjamin859ec3c2014-09-02 16:29:36 -04001821
Adam Langleyfcf25832014-12-18 17:42:32 -08001822void SSL_set_cert_cb(SSL *s, int (*cb)(SSL *ssl, void *arg), void *arg) {
1823 ssl_cert_set_cert_cb(s->cert, cb, arg);
1824}
Adam Langley95c29f32014-06-20 12:00:00 -07001825
Adam Langleyfcf25832014-12-18 17:42:32 -08001826static int ssl_has_key(SSL *s, size_t idx) {
1827 CERT_PKEY *cpk = &s->cert->pkeys[idx];
1828 return cpk->x509 && cpk->privatekey;
1829}
David Benjamin033e5f42014-11-13 18:47:41 -05001830
David Benjamin107db582015-04-08 00:41:59 -04001831void ssl_get_compatible_server_ciphers(SSL *s, uint32_t *out_mask_k,
1832 uint32_t *out_mask_a) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001833 CERT *c = s->cert;
1834 int rsa_enc, rsa_sign, dh_tmp;
David Benjamin107db582015-04-08 00:41:59 -04001835 uint32_t mask_k, mask_a;
Adam Langleyfcf25832014-12-18 17:42:32 -08001836 int have_ecc_cert, ecdsa_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001837 X509 *x;
Adam Langley95c29f32014-06-20 12:00:00 -07001838
Adam Langleyfcf25832014-12-18 17:42:32 -08001839 if (c == NULL) {
1840 /* TODO(davidben): Is this codepath possible? */
1841 *out_mask_k = 0;
1842 *out_mask_a = 0;
1843 return;
1844 }
Adam Langley95c29f32014-06-20 12:00:00 -07001845
Adam Langleyfcf25832014-12-18 17:42:32 -08001846 dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001847
Adam Langleyfcf25832014-12-18 17:42:32 -08001848 rsa_enc = ssl_has_key(s, SSL_PKEY_RSA_ENC);
1849 rsa_sign = ssl_has_key(s, SSL_PKEY_RSA_SIGN);
1850 have_ecc_cert = ssl_has_key(s, SSL_PKEY_ECC);
1851 mask_k = 0;
1852 mask_a = 0;
David Benjaminf31e6812014-11-13 18:05:55 -05001853
Adam Langleyfcf25832014-12-18 17:42:32 -08001854 if (rsa_enc) {
1855 mask_k |= SSL_kRSA;
1856 }
1857 if (dh_tmp) {
David Benjamin7061e282015-03-19 11:10:48 -04001858 mask_k |= SSL_kDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001859 }
1860 if (rsa_enc || rsa_sign) {
1861 mask_a |= SSL_aRSA;
1862 }
Adam Langley95c29f32014-06-20 12:00:00 -07001863
Adam Langleyfcf25832014-12-18 17:42:32 -08001864 /* An ECC certificate may be usable for ECDSA cipher suites depending on the
1865 * key usage extension and on the client's curve preferences. */
1866 if (have_ecc_cert) {
1867 x = c->pkeys[SSL_PKEY_ECC].x509;
1868 /* This call populates extension flags (ex_flags). */
1869 X509_check_purpose(x, -1, 0);
1870 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE)
1871 ? (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)
1872 : 1;
1873 if (!tls1_check_ec_cert(s, x)) {
1874 ecdsa_ok = 0;
1875 }
1876 if (ecdsa_ok) {
1877 mask_a |= SSL_aECDSA;
1878 }
1879 }
Adam Langley95c29f32014-06-20 12:00:00 -07001880
Adam Langleyfcf25832014-12-18 17:42:32 -08001881 /* If we are considering an ECC cipher suite that uses an ephemeral EC
1882 * key, check it. */
David Benjamindd978782015-04-24 15:20:13 -04001883 if (tls1_check_ec_tmp_key(s)) {
David Benjamin7061e282015-03-19 11:10:48 -04001884 mask_k |= SSL_kECDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001885 }
Adam Langley95c29f32014-06-20 12:00:00 -07001886
Adam Langleyfcf25832014-12-18 17:42:32 -08001887 /* PSK requires a server callback. */
1888 if (s->psk_server_callback != NULL) {
1889 mask_k |= SSL_kPSK;
1890 mask_a |= SSL_aPSK;
1891 }
Adam Langley95c29f32014-06-20 12:00:00 -07001892
Adam Langleyfcf25832014-12-18 17:42:32 -08001893 *out_mask_k = mask_k;
1894 *out_mask_a = mask_a;
1895}
Adam Langley95c29f32014-06-20 12:00:00 -07001896
Adam Langleyfcf25832014-12-18 17:42:32 -08001897static int ssl_get_server_cert_index(const SSL *s) {
1898 int idx;
1899 idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1900 if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509) {
1901 idx = SSL_PKEY_RSA_SIGN;
1902 }
1903 if (idx == -1) {
1904 OPENSSL_PUT_ERROR(SSL, ssl_get_server_cert_index, ERR_R_INTERNAL_ERROR);
1905 }
1906 return idx;
1907}
Adam Langley95c29f32014-06-20 12:00:00 -07001908
Adam Langleyfcf25832014-12-18 17:42:32 -08001909CERT_PKEY *ssl_get_server_send_pkey(const SSL *s) {
1910 int i = ssl_get_server_cert_index(s);
Adam Langley95c29f32014-06-20 12:00:00 -07001911
Adam Langleyfcf25832014-12-18 17:42:32 -08001912 /* This may or may not be an error. */
1913 if (i < 0) {
1914 return NULL;
1915 }
Adam Langley95c29f32014-06-20 12:00:00 -07001916
Adam Langleyfcf25832014-12-18 17:42:32 -08001917 /* May be NULL. */
1918 return &s->cert->pkeys[i];
1919}
Adam Langley95c29f32014-06-20 12:00:00 -07001920
Adam Langleyfcf25832014-12-18 17:42:32 -08001921EVP_PKEY *ssl_get_sign_pkey(SSL *s, const SSL_CIPHER *cipher) {
David Benjamin3fa27772015-04-17 22:32:19 -04001922 uint32_t alg_a = cipher->algorithm_auth;
1923 CERT *c = s->cert;
Adam Langleyfcf25832014-12-18 17:42:32 -08001924 int idx = -1;
Adam Langley95c29f32014-06-20 12:00:00 -07001925
Adam Langleyfcf25832014-12-18 17:42:32 -08001926 if (alg_a & SSL_aRSA) {
1927 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL) {
1928 idx = SSL_PKEY_RSA_SIGN;
1929 } else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL) {
1930 idx = SSL_PKEY_RSA_ENC;
1931 }
1932 } else if ((alg_a & SSL_aECDSA) &&
1933 (c->pkeys[SSL_PKEY_ECC].privatekey != NULL)) {
1934 idx = SSL_PKEY_ECC;
1935 }
Adam Langley95c29f32014-06-20 12:00:00 -07001936
Adam Langleyfcf25832014-12-18 17:42:32 -08001937 if (idx == -1) {
1938 OPENSSL_PUT_ERROR(SSL, ssl_get_sign_pkey, ERR_R_INTERNAL_ERROR);
1939 return NULL;
1940 }
Adam Langley95c29f32014-06-20 12:00:00 -07001941
Adam Langleyfcf25832014-12-18 17:42:32 -08001942 return c->pkeys[idx].privatekey;
1943}
Adam Langley95c29f32014-06-20 12:00:00 -07001944
Adam Langleyfcf25832014-12-18 17:42:32 -08001945void ssl_update_cache(SSL *s, int mode) {
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001946 /* Never cache sessions with empty session IDs. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001947 if (s->session->session_id_length == 0) {
1948 return;
1949 }
Adam Langley95c29f32014-06-20 12:00:00 -07001950
David Benjamin95d31822015-06-15 19:53:32 -04001951 int has_new_session = !s->hit;
1952 if (!s->server && s->tlsext_ticket_expected) {
1953 /* A client may see new sessions on abbreviated handshakes if the server
1954 * decides to renew the ticket. Once the handshake is completed, it should
1955 * be inserted into the cache. */
1956 has_new_session = 1;
1957 }
1958
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001959 SSL_CTX *ctx = s->initial_ctx;
David Benjamin95d31822015-06-15 19:53:32 -04001960 if ((ctx->session_cache_mode & mode) == mode && has_new_session &&
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001961 ((ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) ||
1962 SSL_CTX_add_session(ctx, s->session)) &&
1963 ctx->new_session_cb != NULL) {
1964 /* Note: |new_session_cb| is called whether the internal session cache is
1965 * used or not. */
1966 if (!ctx->new_session_cb(s, SSL_SESSION_up_ref(s->session))) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001967 SSL_SESSION_free(s->session);
1968 }
1969 }
Adam Langley95c29f32014-06-20 12:00:00 -07001970
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001971 if (!(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR) &&
1972 !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) &&
1973 (ctx->session_cache_mode & mode) == mode) {
1974 /* Automatically flush the internal session cache every 255 connections. */
1975 int flush_cache = 0;
Adam Langley4bdb6e42015-05-15 15:29:21 -07001976 CRYPTO_MUTEX_lock_write(&ctx->lock);
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001977 ctx->handshakes_since_cache_flush++;
1978 if (ctx->handshakes_since_cache_flush >= 255) {
1979 flush_cache = 1;
1980 ctx->handshakes_since_cache_flush = 0;
1981 }
Adam Langley4bdb6e42015-05-15 15:29:21 -07001982 CRYPTO_MUTEX_unlock(&ctx->lock);
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001983
1984 if (flush_cache) {
1985 SSL_CTX_flush_sessions(ctx, (unsigned long)time(NULL));
Adam Langleyfcf25832014-12-18 17:42:32 -08001986 }
1987 }
1988}
Adam Langley95c29f32014-06-20 12:00:00 -07001989
David Benjamin1a5c50f2015-03-11 16:22:37 -04001990int SSL_get_error(const SSL *s, int ret_code) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001991 int reason;
David Benjamin1a5c50f2015-03-11 16:22:37 -04001992 uint32_t err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001993 BIO *bio;
Adam Langley95c29f32014-06-20 12:00:00 -07001994
David Benjamin1a5c50f2015-03-11 16:22:37 -04001995 if (ret_code > 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001996 return SSL_ERROR_NONE;
1997 }
Adam Langley95c29f32014-06-20 12:00:00 -07001998
Adam Langleyfcf25832014-12-18 17:42:32 -08001999 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
2000 * where we do encode the error */
David Benjamin1a5c50f2015-03-11 16:22:37 -04002001 err = ERR_peek_error();
2002 if (err != 0) {
2003 if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002004 return SSL_ERROR_SYSCALL;
2005 }
2006 return SSL_ERROR_SSL;
2007 }
Adam Langley95c29f32014-06-20 12:00:00 -07002008
David Benjamin1a5c50f2015-03-11 16:22:37 -04002009 if (ret_code == 0) {
David Benjamin9a38e922015-01-22 16:06:11 -05002010 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2011 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) {
2012 /* The socket was cleanly shut down with a close_notify. */
2013 return SSL_ERROR_ZERO_RETURN;
2014 }
2015 /* An EOF was observed which violates the protocol, and the underlying
2016 * transport does not participate in the error queue. Bubble up to the
2017 * caller. */
2018 return SSL_ERROR_SYSCALL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002019 }
Adam Langleyb2ce0582014-06-20 12:00:00 -07002020
Adam Langleyfcf25832014-12-18 17:42:32 -08002021 if (SSL_want_session(s)) {
2022 return SSL_ERROR_PENDING_SESSION;
2023 }
Adam Langley95c29f32014-06-20 12:00:00 -07002024
Adam Langleyfcf25832014-12-18 17:42:32 -08002025 if (SSL_want_certificate(s)) {
2026 return SSL_ERROR_PENDING_CERTIFICATE;
2027 }
Adam Langley95c29f32014-06-20 12:00:00 -07002028
Adam Langleyfcf25832014-12-18 17:42:32 -08002029 if (SSL_want_read(s)) {
2030 bio = SSL_get_rbio(s);
2031 if (BIO_should_read(bio)) {
2032 return SSL_ERROR_WANT_READ;
2033 }
Adam Langley95c29f32014-06-20 12:00:00 -07002034
Adam Langleyfcf25832014-12-18 17:42:32 -08002035 if (BIO_should_write(bio)) {
2036 /* This one doesn't make too much sense ... We never try to write to the
2037 * rbio, and an application program where rbio and wbio are separate
2038 * couldn't even know what it should wait for. However if we ever set
2039 * s->rwstate incorrectly (so that we have SSL_want_read(s) instead of
2040 * SSL_want_write(s)) and rbio and wbio *are* the same, this test works
2041 * around that bug; so it might be safer to keep it. */
2042 return SSL_ERROR_WANT_WRITE;
2043 }
Adam Langley95c29f32014-06-20 12:00:00 -07002044
Adam Langleyfcf25832014-12-18 17:42:32 -08002045 if (BIO_should_io_special(bio)) {
2046 reason = BIO_get_retry_reason(bio);
2047 if (reason == BIO_RR_CONNECT) {
2048 return SSL_ERROR_WANT_CONNECT;
2049 }
Adam Langley95c29f32014-06-20 12:00:00 -07002050
Adam Langleyfcf25832014-12-18 17:42:32 -08002051 if (reason == BIO_RR_ACCEPT) {
2052 return SSL_ERROR_WANT_ACCEPT;
2053 }
Adam Langley95c29f32014-06-20 12:00:00 -07002054
Adam Langleyfcf25832014-12-18 17:42:32 -08002055 return SSL_ERROR_SYSCALL; /* unknown */
2056 }
2057 }
Adam Langley95c29f32014-06-20 12:00:00 -07002058
Adam Langleyfcf25832014-12-18 17:42:32 -08002059 if (SSL_want_write(s)) {
2060 bio = SSL_get_wbio(s);
2061 if (BIO_should_write(bio)) {
2062 return SSL_ERROR_WANT_WRITE;
2063 }
Adam Langley95c29f32014-06-20 12:00:00 -07002064
Adam Langleyfcf25832014-12-18 17:42:32 -08002065 if (BIO_should_read(bio)) {
2066 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2067 return SSL_ERROR_WANT_READ;
2068 }
Adam Langley95c29f32014-06-20 12:00:00 -07002069
Adam Langleyfcf25832014-12-18 17:42:32 -08002070 if (BIO_should_io_special(bio)) {
2071 reason = BIO_get_retry_reason(bio);
2072 if (reason == BIO_RR_CONNECT) {
2073 return SSL_ERROR_WANT_CONNECT;
2074 }
Adam Langley95c29f32014-06-20 12:00:00 -07002075
Adam Langleyfcf25832014-12-18 17:42:32 -08002076 if (reason == BIO_RR_ACCEPT) {
2077 return SSL_ERROR_WANT_ACCEPT;
2078 }
Adam Langley95c29f32014-06-20 12:00:00 -07002079
Adam Langleyfcf25832014-12-18 17:42:32 -08002080 return SSL_ERROR_SYSCALL;
2081 }
2082 }
Adam Langley95c29f32014-06-20 12:00:00 -07002083
Adam Langleyfcf25832014-12-18 17:42:32 -08002084 if (SSL_want_x509_lookup(s)) {
2085 return SSL_ERROR_WANT_X509_LOOKUP;
2086 }
Adam Langley95c29f32014-06-20 12:00:00 -07002087
Adam Langleyfcf25832014-12-18 17:42:32 -08002088 if (SSL_want_channel_id_lookup(s)) {
2089 return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
2090 }
Adam Langley0f4746e2014-08-13 12:26:32 -07002091
Adam Langleyfcf25832014-12-18 17:42:32 -08002092 return SSL_ERROR_SYSCALL;
2093}
Adam Langley0f4746e2014-08-13 12:26:32 -07002094
Adam Langleyfcf25832014-12-18 17:42:32 -08002095int SSL_do_handshake(SSL *s) {
2096 int ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002097
Adam Langleyfcf25832014-12-18 17:42:32 -08002098 if (s->handshake_func == NULL) {
2099 OPENSSL_PUT_ERROR(SSL, SSL_do_handshake, SSL_R_CONNECTION_TYPE_NOT_SET);
2100 return -1;
2101 }
Adam Langley95c29f32014-06-20 12:00:00 -07002102
Adam Langleyfcf25832014-12-18 17:42:32 -08002103 if (SSL_in_init(s)) {
2104 ret = s->handshake_func(s);
2105 }
2106 return ret;
2107}
Adam Langley95c29f32014-06-20 12:00:00 -07002108
David Benjamin4831c332015-05-16 11:43:13 -04002109void SSL_set_accept_state(SSL *ssl) {
2110 ssl->server = 1;
2111 ssl->shutdown = 0;
David Benjamin8ec88102015-05-15 23:40:13 -04002112 ssl->state = SSL_ST_ACCEPT;
David Benjamin4831c332015-05-16 11:43:13 -04002113 ssl->handshake_func = ssl->method->ssl_accept;
Adam Langleyfcf25832014-12-18 17:42:32 -08002114 /* clear the current cipher */
David Benjamin4831c332015-05-16 11:43:13 -04002115 ssl_clear_cipher_ctx(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08002116}
Adam Langley95c29f32014-06-20 12:00:00 -07002117
David Benjamin4831c332015-05-16 11:43:13 -04002118void SSL_set_connect_state(SSL *ssl) {
2119 ssl->server = 0;
2120 ssl->shutdown = 0;
David Benjamin8ec88102015-05-15 23:40:13 -04002121 ssl->state = SSL_ST_CONNECT;
David Benjamin4831c332015-05-16 11:43:13 -04002122 ssl->handshake_func = ssl->method->ssl_connect;
Adam Langleyfcf25832014-12-18 17:42:32 -08002123 /* clear the current cipher */
David Benjamin4831c332015-05-16 11:43:13 -04002124 ssl_clear_cipher_ctx(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08002125}
Adam Langley95c29f32014-06-20 12:00:00 -07002126
Adam Langleyfcf25832014-12-18 17:42:32 -08002127static const char *ssl_get_version(int version) {
2128 switch (version) {
2129 case TLS1_2_VERSION:
2130 return "TLSv1.2";
Adam Langley95c29f32014-06-20 12:00:00 -07002131
Adam Langleyfcf25832014-12-18 17:42:32 -08002132 case TLS1_1_VERSION:
2133 return "TLSv1.1";
Adam Langley95c29f32014-06-20 12:00:00 -07002134
Adam Langleyfcf25832014-12-18 17:42:32 -08002135 case TLS1_VERSION:
2136 return "TLSv1";
Adam Langley95c29f32014-06-20 12:00:00 -07002137
Adam Langleyfcf25832014-12-18 17:42:32 -08002138 case SSL3_VERSION:
2139 return "SSLv3";
Adam Langley95c29f32014-06-20 12:00:00 -07002140
David Benjamin1c722b72015-04-20 13:53:10 -04002141 case DTLS1_VERSION:
2142 return "DTLSv1";
2143
2144 case DTLS1_2_VERSION:
2145 return "DTLSv1.2";
2146
Adam Langleyfcf25832014-12-18 17:42:32 -08002147 default:
2148 return "unknown";
2149 }
2150}
Adam Langley95c29f32014-06-20 12:00:00 -07002151
Adam Langleyfcf25832014-12-18 17:42:32 -08002152const char *SSL_get_version(const SSL *s) {
2153 return ssl_get_version(s->version);
2154}
Adam Langley95c29f32014-06-20 12:00:00 -07002155
Adam Langleyfcf25832014-12-18 17:42:32 -08002156const char *SSL_SESSION_get_version(const SSL_SESSION *sess) {
2157 return ssl_get_version(sess->ssl_version);
2158}
Adam Langley95c29f32014-06-20 12:00:00 -07002159
Adam Langleyfcf25832014-12-18 17:42:32 -08002160void ssl_clear_cipher_ctx(SSL *s) {
David Benjamin31a07792015-03-03 14:20:26 -05002161 SSL_AEAD_CTX_free(s->aead_read_ctx);
2162 s->aead_read_ctx = NULL;
2163 SSL_AEAD_CTX_free(s->aead_write_ctx);
2164 s->aead_write_ctx = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002165}
Adam Langley95c29f32014-06-20 12:00:00 -07002166
Adam Langleyfcf25832014-12-18 17:42:32 -08002167X509 *SSL_get_certificate(const SSL *s) {
2168 if (s->cert != NULL) {
2169 return s->cert->key->x509;
2170 }
2171
2172 return NULL;
2173}
2174
2175EVP_PKEY *SSL_get_privatekey(const SSL *s) {
2176 if (s->cert != NULL) {
2177 return s->cert->key->privatekey;
2178 }
2179
2180 return NULL;
2181}
2182
2183X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
2184 if (ctx->cert != NULL) {
2185 return ctx->cert->key->x509;
2186 }
2187
2188 return NULL;
2189}
2190
2191EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
2192 if (ctx->cert != NULL) {
2193 return ctx->cert->key->privatekey;
2194 }
2195
2196 return NULL;
2197}
2198
2199const SSL_CIPHER *SSL_get_current_cipher(const SSL *s) {
David Benjamina07c0fc2015-05-13 13:19:42 -04002200 if (s->aead_write_ctx == NULL) {
2201 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002202 }
David Benjamina07c0fc2015-05-13 13:19:42 -04002203 return s->aead_write_ctx->cipher;
Adam Langleyfcf25832014-12-18 17:42:32 -08002204}
2205
2206const void *SSL_get_current_compression(SSL *s) { return NULL; }
2207
2208const void *SSL_get_current_expansion(SSL *s) { return NULL; }
2209
2210int ssl_init_wbio_buffer(SSL *s, int push) {
2211 BIO *bbio;
2212
2213 if (s->bbio == NULL) {
2214 bbio = BIO_new(BIO_f_buffer());
2215 if (bbio == NULL) {
2216 return 0;
2217 }
2218 s->bbio = bbio;
2219 } else {
2220 bbio = s->bbio;
2221 if (s->bbio == s->wbio) {
2222 s->wbio = BIO_pop(s->wbio);
2223 }
2224 }
2225
2226 BIO_reset(bbio);
2227 if (!BIO_set_read_buffer_size(bbio, 1)) {
2228 OPENSSL_PUT_ERROR(SSL, ssl_init_wbio_buffer, ERR_R_BUF_LIB);
2229 return 0;
2230 }
2231
2232 if (push) {
2233 if (s->wbio != bbio) {
2234 s->wbio = BIO_push(bbio, s->wbio);
2235 }
2236 } else {
2237 if (s->wbio == bbio) {
2238 s->wbio = BIO_pop(bbio);
2239 }
2240 }
2241
2242 return 1;
2243}
2244
2245void ssl_free_wbio_buffer(SSL *s) {
2246 if (s->bbio == NULL) {
2247 return;
2248 }
2249
2250 if (s->bbio == s->wbio) {
2251 /* remove buffering */
2252 s->wbio = BIO_pop(s->wbio);
2253 }
2254
2255 BIO_free(s->bbio);
2256 s->bbio = NULL;
2257}
2258
2259void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
2260 ctx->quiet_shutdown = mode;
2261}
2262
2263int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
2264 return ctx->quiet_shutdown;
2265}
2266
2267void SSL_set_quiet_shutdown(SSL *s, int mode) { s->quiet_shutdown = mode; }
2268
2269int SSL_get_quiet_shutdown(const SSL *s) { return s->quiet_shutdown; }
2270
2271void SSL_set_shutdown(SSL *s, int mode) { s->shutdown = mode; }
2272
2273int SSL_get_shutdown(const SSL *s) { return s->shutdown; }
2274
2275int SSL_version(const SSL *s) { return s->version; }
2276
2277SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
2278
2279SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
2280 if (ssl->ctx == ctx) {
2281 return ssl->ctx;
2282 }
2283
2284 if (ctx == NULL) {
2285 ctx = ssl->initial_ctx;
2286 }
2287
David Benjamin2755a3e2015-04-22 16:17:58 -04002288 ssl_cert_free(ssl->cert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002289 ssl->cert = ssl_cert_dup(ctx->cert);
David Benjamin2755a3e2015-04-22 16:17:58 -04002290
Adam Langley0b5e3902015-05-15 13:08:38 -07002291 CRYPTO_refcount_inc(&ctx->references);
David Benjamin2755a3e2015-04-22 16:17:58 -04002292 SSL_CTX_free(ssl->ctx); /* decrement reference count */
Adam Langleyfcf25832014-12-18 17:42:32 -08002293 ssl->ctx = ctx;
2294
2295 ssl->sid_ctx_length = ctx->sid_ctx_length;
2296 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2297 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2298
2299 return ssl->ctx;
2300}
2301
2302int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
2303 return X509_STORE_set_default_paths(ctx->cert_store);
2304}
Adam Langley95c29f32014-06-20 12:00:00 -07002305
2306int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
Adam Langleyfcf25832014-12-18 17:42:32 -08002307 const char *CApath) {
2308 return X509_STORE_load_locations(ctx->cert_store, CAfile, CApath);
2309}
Adam Langley95c29f32014-06-20 12:00:00 -07002310
2311void SSL_set_info_callback(SSL *ssl,
Adam Langleyfcf25832014-12-18 17:42:32 -08002312 void (*cb)(const SSL *ssl, int type, int val)) {
2313 ssl->info_callback = cb;
2314}
Adam Langley95c29f32014-06-20 12:00:00 -07002315
Adam Langleyfcf25832014-12-18 17:42:32 -08002316void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/, int /*type*/,
2317 int /*val*/) {
2318 return ssl->info_callback;
2319}
Adam Langley95c29f32014-06-20 12:00:00 -07002320
Adam Langleyfcf25832014-12-18 17:42:32 -08002321int SSL_state(const SSL *ssl) { return ssl->state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002322
David Benjaminece089c2015-05-15 23:52:42 -04002323void SSL_set_state(SSL *ssl, int state) { }
Adam Langley95c29f32014-06-20 12:00:00 -07002324
Adam Langleyfcf25832014-12-18 17:42:32 -08002325void SSL_set_verify_result(SSL *ssl, long arg) { ssl->verify_result = arg; }
Adam Langley95c29f32014-06-20 12:00:00 -07002326
Adam Langleyfcf25832014-12-18 17:42:32 -08002327long SSL_get_verify_result(const SSL *ssl) { return ssl->verify_result; }
Adam Langley95c29f32014-06-20 12:00:00 -07002328
Adam Langleyfcf25832014-12-18 17:42:32 -08002329int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2330 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002331 int index;
2332 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp,
2333 new_func, dup_func, free_func)) {
2334 return -1;
2335 }
2336 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002337}
Adam Langley95c29f32014-06-20 12:00:00 -07002338
Adam Langleyfcf25832014-12-18 17:42:32 -08002339int SSL_set_ex_data(SSL *s, int idx, void *arg) {
2340 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2341}
Adam Langley95c29f32014-06-20 12:00:00 -07002342
Adam Langleyfcf25832014-12-18 17:42:32 -08002343void *SSL_get_ex_data(const SSL *s, int idx) {
2344 return CRYPTO_get_ex_data(&s->ex_data, idx);
2345}
Adam Langley95c29f32014-06-20 12:00:00 -07002346
Adam Langleyfcf25832014-12-18 17:42:32 -08002347int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2348 CRYPTO_EX_dup *dup_func,
2349 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_ctx, &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_CTX_set_ex_data(SSL_CTX *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_CTX_get_ex_data(const SSL_CTX *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 -08002366X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
2367 return ctx->cert_store;
2368}
Adam Langley95c29f32014-06-20 12:00:00 -07002369
Adam Langleyfcf25832014-12-18 17:42:32 -08002370void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
David Benjamin2755a3e2015-04-22 16:17:58 -04002371 X509_STORE_free(ctx->cert_store);
Adam Langleyfcf25832014-12-18 17:42:32 -08002372 ctx->cert_store = store;
2373}
Adam Langley95c29f32014-06-20 12:00:00 -07002374
Adam Langleyfcf25832014-12-18 17:42:32 -08002375int SSL_want(const SSL *s) { return s->rwstate; }
Adam Langley95c29f32014-06-20 12:00:00 -07002376
Adam Langleyfcf25832014-12-18 17:42:32 -08002377void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
2378 RSA *(*cb)(SSL *ssl, int is_export,
2379 int keylength)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002380}
Adam Langley95c29f32014-06-20 12:00:00 -07002381
Adam Langleyfcf25832014-12-18 17:42:32 -08002382void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
2383 int keylength)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002384}
2385
2386void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
David Benjamin59015c32015-04-26 13:13:55 -04002387 DH *(*callback)(SSL *ssl, int is_export,
2388 int keylength)) {
2389 ctx->cert->dh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002390}
2391
David Benjamin59015c32015-04-26 13:13:55 -04002392void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*callback)(SSL *ssl, int is_export,
2393 int keylength)) {
2394 ssl->cert->dh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002395}
2396
2397void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,
David Benjamindd978782015-04-24 15:20:13 -04002398 EC_KEY *(*callback)(SSL *ssl, int is_export,
2399 int keylength)) {
David Benjamin59015c32015-04-26 13:13:55 -04002400 ctx->cert->ecdh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002401}
2402
2403void SSL_set_tmp_ecdh_callback(SSL *ssl,
David Benjamindd978782015-04-24 15:20:13 -04002404 EC_KEY *(*callback)(SSL *ssl, int is_export,
2405 int keylength)) {
David Benjamin59015c32015-04-26 13:13:55 -04002406 ssl->cert->ecdh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002407}
2408
2409int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
2410 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2411 OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_psk_identity_hint,
2412 SSL_R_DATA_LENGTH_TOO_LONG);
2413 return 0;
2414 }
2415
David Benjamin2755a3e2015-04-22 16:17:58 -04002416 OPENSSL_free(ctx->psk_identity_hint);
Adam Langleyfcf25832014-12-18 17:42:32 -08002417
2418 if (identity_hint != NULL) {
2419 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2420 if (ctx->psk_identity_hint == NULL) {
2421 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002422 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002423 } else {
2424 ctx->psk_identity_hint = NULL;
2425 }
Adam Langley95c29f32014-06-20 12:00:00 -07002426
Adam Langleyfcf25832014-12-18 17:42:32 -08002427 return 1;
2428}
2429
2430int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint) {
2431 if (s == NULL) {
2432 return 0;
2433 }
2434
2435 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2436 OPENSSL_PUT_ERROR(SSL, SSL_use_psk_identity_hint,
2437 SSL_R_DATA_LENGTH_TOO_LONG);
2438 return 0;
2439 }
2440
2441 /* Clear currently configured hint, if any. */
David Benjamin2755a3e2015-04-22 16:17:58 -04002442 OPENSSL_free(s->psk_identity_hint);
2443 s->psk_identity_hint = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002444
2445 if (identity_hint != NULL) {
2446 s->psk_identity_hint = BUF_strdup(identity_hint);
2447 if (s->psk_identity_hint == NULL) {
2448 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002449 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002450 }
Adam Langley95c29f32014-06-20 12:00:00 -07002451
Adam Langleyfcf25832014-12-18 17:42:32 -08002452 return 1;
2453}
Adam Langley95c29f32014-06-20 12:00:00 -07002454
Adam Langleyfcf25832014-12-18 17:42:32 -08002455const char *SSL_get_psk_identity_hint(const SSL *s) {
2456 if (s == NULL) {
2457 return NULL;
2458 }
2459 return s->psk_identity_hint;
2460}
Adam Langley95c29f32014-06-20 12:00:00 -07002461
Adam Langleyfcf25832014-12-18 17:42:32 -08002462const char *SSL_get_psk_identity(const SSL *s) {
2463 if (s == NULL || s->session == NULL) {
2464 return NULL;
2465 }
Adam Langley95c29f32014-06-20 12:00:00 -07002466
Adam Langleyfcf25832014-12-18 17:42:32 -08002467 return s->session->psk_identity;
2468}
Adam Langley95c29f32014-06-20 12:00:00 -07002469
Adam Langleyfcf25832014-12-18 17:42:32 -08002470void SSL_set_psk_client_callback(
2471 SSL *s, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2472 unsigned int max_identity_len, uint8_t *psk,
2473 unsigned int max_psk_len)) {
2474 s->psk_client_callback = cb;
2475}
Adam Langley95c29f32014-06-20 12:00:00 -07002476
Adam Langleyfcf25832014-12-18 17:42:32 -08002477void SSL_CTX_set_psk_client_callback(
2478 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2479 unsigned int max_identity_len,
2480 uint8_t *psk, unsigned int max_psk_len)) {
2481 ctx->psk_client_callback = cb;
2482}
Adam Langley95c29f32014-06-20 12:00:00 -07002483
Adam Langleyfcf25832014-12-18 17:42:32 -08002484void SSL_set_psk_server_callback(
2485 SSL *s, unsigned int (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
2486 unsigned int max_psk_len)) {
2487 s->psk_server_callback = cb;
2488}
Adam Langley95c29f32014-06-20 12:00:00 -07002489
Adam Langleyfcf25832014-12-18 17:42:32 -08002490void SSL_CTX_set_psk_server_callback(
2491 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *identity,
2492 uint8_t *psk, unsigned int max_psk_len)) {
2493 ctx->psk_server_callback = cb;
2494}
Adam Langley95c29f32014-06-20 12:00:00 -07002495
Adam Langleyfcf25832014-12-18 17:42:32 -08002496void SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) {
2497 ctx->min_version = version;
2498}
Adam Langley95c29f32014-06-20 12:00:00 -07002499
Adam Langleyfcf25832014-12-18 17:42:32 -08002500void SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) {
2501 ctx->max_version = version;
2502}
Adam Langley0289c732014-06-20 12:00:00 -07002503
Adam Langleyfcf25832014-12-18 17:42:32 -08002504void SSL_set_min_version(SSL *ssl, uint16_t version) {
2505 ssl->min_version = version;
2506}
Adam Langley0289c732014-06-20 12:00:00 -07002507
Adam Langleyfcf25832014-12-18 17:42:32 -08002508void SSL_set_max_version(SSL *ssl, uint16_t version) {
2509 ssl->max_version = version;
2510}
Adam Langley95c29f32014-06-20 12:00:00 -07002511
Adam Langleyfcf25832014-12-18 17:42:32 -08002512void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
2513 void (*cb)(int write_p, int version,
2514 int content_type, const void *buf,
2515 size_t len, SSL *ssl, void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002516 ctx->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002517}
David Benjamin61ecccf2015-05-05 09:44:51 -04002518
2519void SSL_CTX_set_msg_callback_arg(SSL_CTX *ctx, void *arg) {
2520 ctx->msg_callback_arg = arg;
2521}
2522
Adam Langleyfcf25832014-12-18 17:42:32 -08002523void SSL_set_msg_callback(SSL *ssl,
2524 void (*cb)(int write_p, int version, int content_type,
2525 const void *buf, size_t len, SSL *ssl,
2526 void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002527 ssl->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002528}
Adam Langley95c29f32014-06-20 12:00:00 -07002529
David Benjamin61ecccf2015-05-05 09:44:51 -04002530void SSL_set_msg_callback_arg(SSL *ssl, void *arg) {
2531 ssl->msg_callback_arg = arg;
2532}
2533
Adam Langleyfcf25832014-12-18 17:42:32 -08002534void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio) {
David Benjamin2755a3e2015-04-22 16:17:58 -04002535 BIO_free(ctx->keylog_bio);
Adam Langleyfcf25832014-12-18 17:42:32 -08002536 ctx->keylog_bio = keylog_bio;
2537}
Adam Langley95c29f32014-06-20 12:00:00 -07002538
Adam Langleyfcf25832014-12-18 17:42:32 -08002539static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
2540 static const char hextable[] = "0123456789abcdef";
2541 uint8_t *out;
2542 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002543
Adam Langleyfcf25832014-12-18 17:42:32 -08002544 if (!CBB_add_space(cbb, &out, in_len * 2)) {
2545 return 0;
2546 }
Adam Langley95c29f32014-06-20 12:00:00 -07002547
Adam Langleyfcf25832014-12-18 17:42:32 -08002548 for (i = 0; i < in_len; i++) {
2549 *(out++) = (uint8_t)hextable[in[i] >> 4];
2550 *(out++) = (uint8_t)hextable[in[i] & 0xf];
2551 }
Adam Langley95c29f32014-06-20 12:00:00 -07002552
Adam Langleyfcf25832014-12-18 17:42:32 -08002553 return 1;
2554}
David Benjamin859ec3c2014-09-02 16:29:36 -04002555
2556int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -08002557 const uint8_t *encrypted_premaster,
2558 size_t encrypted_premaster_len,
2559 const uint8_t *premaster,
2560 size_t premaster_len) {
2561 BIO *bio = ctx->keylog_bio;
2562 CBB cbb;
2563 uint8_t *out;
2564 size_t out_len;
2565 int ret;
David Benjamin859ec3c2014-09-02 16:29:36 -04002566
Adam Langleyfcf25832014-12-18 17:42:32 -08002567 if (bio == NULL) {
2568 return 1;
2569 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002570
Adam Langleyfcf25832014-12-18 17:42:32 -08002571 if (encrypted_premaster_len < 8) {
2572 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_rsa_client_key_exchange,
2573 ERR_R_INTERNAL_ERROR);
2574 return 0;
2575 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002576
Adam Langleyfcf25832014-12-18 17:42:32 -08002577 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len * 2 + 1)) {
2578 return 0;
2579 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002580
Adam Langleyfcf25832014-12-18 17:42:32 -08002581 if (!CBB_add_bytes(&cbb, (const uint8_t *)"RSA ", 4) ||
2582 /* Only the first 8 bytes of the encrypted premaster secret are
2583 * logged. */
2584 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
2585 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2586 !cbb_add_hex(&cbb, premaster, premaster_len) ||
2587 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2588 !CBB_finish(&cbb, &out, &out_len)) {
2589 CBB_cleanup(&cbb);
2590 return 0;
2591 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002592
Adam Langley4bdb6e42015-05-15 15:29:21 -07002593 CRYPTO_MUTEX_lock_write(&ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -08002594 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
Adam Langley4bdb6e42015-05-15 15:29:21 -07002595 CRYPTO_MUTEX_unlock(&ctx->lock);
David Benjamin859ec3c2014-09-02 16:29:36 -04002596
Adam Langleyfcf25832014-12-18 17:42:32 -08002597 OPENSSL_free(out);
2598 return ret;
Adam Langley95f22882014-06-20 12:00:00 -07002599}
2600
Adam Langleyfcf25832014-12-18 17:42:32 -08002601int ssl_ctx_log_master_secret(SSL_CTX *ctx, const uint8_t *client_random,
2602 size_t client_random_len, const uint8_t *master,
2603 size_t master_len) {
2604 BIO *bio = ctx->keylog_bio;
2605 CBB cbb;
2606 uint8_t *out;
2607 size_t out_len;
2608 int ret;
Adam Langleyadb739e2014-06-20 12:00:00 -07002609
Adam Langleyfcf25832014-12-18 17:42:32 -08002610 if (bio == NULL) {
2611 return 1;
2612 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002613
Adam Langleyfcf25832014-12-18 17:42:32 -08002614 if (client_random_len != 32) {
2615 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_master_secret, ERR_R_INTERNAL_ERROR);
2616 return 0;
2617 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002618
Adam Langleyfcf25832014-12-18 17:42:32 -08002619 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len * 2 + 1)) {
2620 return 0;
2621 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002622
Adam Langleyfcf25832014-12-18 17:42:32 -08002623 if (!CBB_add_bytes(&cbb, (const uint8_t *)"CLIENT_RANDOM ", 14) ||
2624 !cbb_add_hex(&cbb, client_random, 32) ||
2625 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2626 !cbb_add_hex(&cbb, master, master_len) ||
2627 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2628 !CBB_finish(&cbb, &out, &out_len)) {
2629 CBB_cleanup(&cbb);
2630 return 0;
2631 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002632
Adam Langley4bdb6e42015-05-15 15:29:21 -07002633 CRYPTO_MUTEX_lock_write(&ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -08002634 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
Adam Langley4bdb6e42015-05-15 15:29:21 -07002635 CRYPTO_MUTEX_unlock(&ctx->lock);
David Benjamine99e9122014-12-11 01:46:01 -05002636
Adam Langleyfcf25832014-12-18 17:42:32 -08002637 OPENSSL_free(out);
2638 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -07002639}
2640
David Benjamined7c4752015-02-16 19:16:46 -05002641int SSL_in_false_start(const SSL *s) {
2642 return s->s3->tmp.in_false_start;
2643}
2644
Adam Langleyfcf25832014-12-18 17:42:32 -08002645int SSL_cutthrough_complete(const SSL *s) {
David Benjamined7c4752015-02-16 19:16:46 -05002646 return SSL_in_false_start(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002647}
Adam Langley95c29f32014-06-20 12:00:00 -07002648
Adam Langleyfcf25832014-12-18 17:42:32 -08002649void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
2650 size_t *ssl_session_size) {
2651 *ssl_size = sizeof(SSL);
2652 *ssl_ctx_size = sizeof(SSL_CTX);
2653 *ssl_session_size = sizeof(SSL_SESSION);
2654}
Feng Lu41aa3252014-11-21 22:47:56 -08002655
David Benjamined7c4752015-02-16 19:16:46 -05002656int ssl3_can_false_start(const SSL *s) {
David Benjamin195dc782015-02-19 13:27:05 -05002657 const SSL_CIPHER *const cipher = SSL_get_current_cipher(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002658
Adam Langleye631d962015-04-01 13:11:01 -07002659 /* False Start only for TLS 1.2 with an ECDHE+AEAD cipher and ALPN or NPN. */
David Benjamin195dc782015-02-19 13:27:05 -05002660 return !SSL_IS_DTLS(s) &&
2661 SSL_version(s) >= TLS1_2_VERSION &&
2662 (s->s3->alpn_selected || s->s3->next_proto_neg_seen) &&
2663 cipher != NULL &&
Adam Langleye631d962015-04-01 13:11:01 -07002664 cipher->algorithm_mkey == SSL_kECDHE &&
David Benjamin195dc782015-02-19 13:27:05 -05002665 (cipher->algorithm_enc == SSL_AES128GCM ||
2666 cipher->algorithm_enc == SSL_AES256GCM ||
2667 cipher->algorithm_enc == SSL_CHACHA20POLY1305);
Adam Langleyfcf25832014-12-18 17:42:32 -08002668}
2669
2670const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version) {
2671 switch (version) {
2672 case SSL3_VERSION:
2673 return &SSLv3_enc_data;
2674
2675 case TLS1_VERSION:
2676 return &TLSv1_enc_data;
2677
David Benjamin9e13e1a2015-03-05 01:56:32 -05002678 case DTLS1_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002679 case TLS1_1_VERSION:
2680 return &TLSv1_1_enc_data;
2681
David Benjamin9e13e1a2015-03-05 01:56:32 -05002682 case DTLS1_2_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002683 case TLS1_2_VERSION:
2684 return &TLSv1_2_enc_data;
2685
Adam Langleyfcf25832014-12-18 17:42:32 -08002686 default:
2687 return NULL;
2688 }
2689}
2690
2691uint16_t ssl3_get_max_server_version(const SSL *s) {
2692 uint16_t max_version;
2693
2694 if (SSL_IS_DTLS(s)) {
2695 max_version = (s->max_version != 0) ? s->max_version : DTLS1_2_VERSION;
2696 if (!(s->options & SSL_OP_NO_DTLSv1_2) && DTLS1_2_VERSION >= max_version) {
2697 return DTLS1_2_VERSION;
2698 }
2699 if (!(s->options & SSL_OP_NO_DTLSv1) && DTLS1_VERSION >= max_version) {
2700 return DTLS1_VERSION;
2701 }
2702 return 0;
2703 }
2704
2705 max_version = (s->max_version != 0) ? s->max_version : TLS1_2_VERSION;
2706 if (!(s->options & SSL_OP_NO_TLSv1_2) && TLS1_2_VERSION <= max_version) {
2707 return TLS1_2_VERSION;
2708 }
2709 if (!(s->options & SSL_OP_NO_TLSv1_1) && TLS1_1_VERSION <= max_version) {
2710 return TLS1_1_VERSION;
2711 }
2712 if (!(s->options & SSL_OP_NO_TLSv1) && TLS1_VERSION <= max_version) {
2713 return TLS1_VERSION;
2714 }
2715 if (!(s->options & SSL_OP_NO_SSLv3) && SSL3_VERSION <= max_version) {
2716 return SSL3_VERSION;
2717 }
2718 return 0;
2719}
2720
2721uint16_t ssl3_get_mutual_version(SSL *s, uint16_t client_version) {
2722 uint16_t version = 0;
2723
2724 if (SSL_IS_DTLS(s)) {
2725 /* Clamp client_version to max_version. */
2726 if (s->max_version != 0 && client_version < s->max_version) {
2727 client_version = s->max_version;
2728 }
2729
2730 if (client_version <= DTLS1_2_VERSION && !(s->options & SSL_OP_NO_DTLSv1_2)) {
2731 version = DTLS1_2_VERSION;
2732 } else if (client_version <= DTLS1_VERSION &&
David Benjaminb18f0242015-03-10 18:30:08 -04002733 !(s->options & SSL_OP_NO_DTLSv1)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002734 version = DTLS1_VERSION;
2735 }
2736
2737 /* Check against min_version. */
2738 if (version != 0 && s->min_version != 0 && version > s->min_version) {
2739 return 0;
2740 }
2741 return version;
2742 } else {
2743 /* Clamp client_version to max_version. */
2744 if (s->max_version != 0 && client_version > s->max_version) {
2745 client_version = s->max_version;
2746 }
2747
2748 if (client_version >= TLS1_2_VERSION && !(s->options & SSL_OP_NO_TLSv1_2)) {
2749 version = TLS1_2_VERSION;
2750 } else if (client_version >= TLS1_1_VERSION &&
2751 !(s->options & SSL_OP_NO_TLSv1_1)) {
2752 version = TLS1_1_VERSION;
2753 } else if (client_version >= TLS1_VERSION && !(s->options & SSL_OP_NO_TLSv1)) {
2754 version = TLS1_VERSION;
2755 } else if (client_version >= SSL3_VERSION && !(s->options & SSL_OP_NO_SSLv3)) {
2756 version = SSL3_VERSION;
2757 }
2758
2759 /* Check against min_version. */
2760 if (version != 0 && s->min_version != 0 && version < s->min_version) {
2761 return 0;
2762 }
2763 return version;
2764 }
2765}
2766
2767uint16_t ssl3_get_max_client_version(SSL *s) {
David Benjamin123a8fd2015-04-26 14:16:41 -04002768 uint32_t options = s->options;
Adam Langleyfcf25832014-12-18 17:42:32 -08002769 uint16_t version = 0;
2770
2771 /* OpenSSL's API for controlling versions entails blacklisting individual
2772 * protocols. This has two problems. First, on the client, the protocol can
2773 * only express a contiguous range of versions. Second, a library consumer
2774 * trying to set a maximum version cannot disable protocol versions that get
2775 * added in a future version of the library.
2776 *
2777 * To account for both of these, OpenSSL interprets the client-side bitmask
2778 * as a min/max range by picking the lowest contiguous non-empty range of
2779 * enabled protocols. Note that this means it is impossible to set a maximum
2780 * version of TLS 1.2 in a future-proof way.
2781 *
2782 * By this scheme, the maximum version is the lowest version V such that V is
2783 * enabled and V+1 is disabled or unimplemented. */
2784 if (SSL_IS_DTLS(s)) {
2785 if (!(options & SSL_OP_NO_DTLSv1_2)) {
2786 version = DTLS1_2_VERSION;
2787 }
2788 if (!(options & SSL_OP_NO_DTLSv1) && (options & SSL_OP_NO_DTLSv1_2)) {
2789 version = DTLS1_VERSION;
2790 }
2791 if (s->max_version != 0 && version < s->max_version) {
2792 version = s->max_version;
2793 }
2794 } else {
2795 if (!(options & SSL_OP_NO_TLSv1_2)) {
2796 version = TLS1_2_VERSION;
2797 }
2798 if (!(options & SSL_OP_NO_TLSv1_1) && (options & SSL_OP_NO_TLSv1_2)) {
2799 version = TLS1_1_VERSION;
2800 }
2801 if (!(options & SSL_OP_NO_TLSv1) && (options & SSL_OP_NO_TLSv1_1)) {
2802 version = TLS1_VERSION;
2803 }
2804 if (!(options & SSL_OP_NO_SSLv3) && (options & SSL_OP_NO_TLSv1)) {
2805 version = SSL3_VERSION;
2806 }
2807 if (s->max_version != 0 && version > s->max_version) {
2808 version = s->max_version;
2809 }
2810 }
2811
2812 return version;
2813}
2814
2815int ssl3_is_version_enabled(SSL *s, uint16_t version) {
2816 if (SSL_IS_DTLS(s)) {
2817 if (s->max_version != 0 && version < s->max_version) {
2818 return 0;
2819 }
2820 if (s->min_version != 0 && version > s->min_version) {
2821 return 0;
2822 }
2823
2824 switch (version) {
2825 case DTLS1_VERSION:
2826 return !(s->options & SSL_OP_NO_DTLSv1);
2827
2828 case DTLS1_2_VERSION:
2829 return !(s->options & SSL_OP_NO_DTLSv1_2);
2830
2831 default:
2832 return 0;
2833 }
2834 } else {
2835 if (s->max_version != 0 && version > s->max_version) {
2836 return 0;
2837 }
2838 if (s->min_version != 0 && version < s->min_version) {
2839 return 0;
2840 }
2841
2842 switch (version) {
2843 case SSL3_VERSION:
2844 return !(s->options & SSL_OP_NO_SSLv3);
2845
2846 case TLS1_VERSION:
2847 return !(s->options & SSL_OP_NO_TLSv1);
2848
2849 case TLS1_1_VERSION:
2850 return !(s->options & SSL_OP_NO_TLSv1_1);
2851
2852 case TLS1_2_VERSION:
2853 return !(s->options & SSL_OP_NO_TLSv1_2);
2854
2855 default:
2856 return 0;
2857 }
2858 }
2859}
2860
David Benjaminea72bd02014-12-21 21:27:41 -05002861uint16_t ssl3_version_from_wire(SSL *s, uint16_t wire_version) {
2862 if (!SSL_IS_DTLS(s)) {
2863 return wire_version;
2864 }
2865
2866 uint16_t tls_version = ~wire_version;
2867 uint16_t version = tls_version + 0x0201;
2868 /* If either component overflowed, clamp it so comparisons still work. */
2869 if ((version >> 8) < (tls_version >> 8)) {
2870 version = 0xff00 | (version & 0xff);
2871 }
2872 if ((version & 0xff) < (tls_version & 0xff)) {
2873 version = (version & 0xff00) | 0xff;
2874 }
2875 /* DTLS 1.0 maps to TLS 1.1, not TLS 1.0. */
2876 if (version == TLS1_VERSION) {
2877 version = TLS1_1_VERSION;
2878 }
2879 return version;
2880}
2881
Adam Langleyfcf25832014-12-18 17:42:32 -08002882int SSL_cache_hit(SSL *s) { return s->hit; }
2883
2884int SSL_is_server(SSL *s) { return s->server; }
2885
Adam Langley524e7172015-02-20 16:04:00 -08002886void SSL_CTX_set_dos_protection_cb(
2887 SSL_CTX *ctx, int (*cb)(const struct ssl_early_callback_ctx *)) {
2888 ctx->dos_protection_cb = cb;
2889}
2890
Adam Langleyfcf25832014-12-18 17:42:32 -08002891void SSL_enable_fastradio_padding(SSL *s, char on_off) {
2892 s->fastradio_padding = on_off;
2893}
Adam Langley44972942015-02-20 13:43:23 -08002894
David Benjaminb16346b2015-04-08 19:16:58 -04002895void SSL_set_reject_peer_renegotiations(SSL *s, int reject) {
David Benjamin897e5e02015-05-12 17:03:54 -04002896 s->accept_peer_renegotiations = !reject;
David Benjaminb16346b2015-04-08 19:16:58 -04002897}
2898
Adam Langley3f92d212015-02-20 15:32:52 -08002899int SSL_get_rc4_state(const SSL *ssl, const RC4_KEY **read_key,
2900 const RC4_KEY **write_key) {
2901 if (ssl->aead_read_ctx == NULL || ssl->aead_write_ctx == NULL) {
2902 return 0;
2903 }
2904
2905 return EVP_AEAD_CTX_get_rc4_state(&ssl->aead_read_ctx->ctx, read_key) &&
2906 EVP_AEAD_CTX_get_rc4_state(&ssl->aead_write_ctx->ctx, write_key);
2907}
David Benjaminda881e92015-04-26 14:45:04 -04002908
Adam Langleyaf0e32c2015-06-03 09:57:23 -07002909int SSL_get_tls_unique(const SSL *ssl, uint8_t *out, size_t *out_len,
2910 size_t max_out) {
2911 /* The tls-unique value is the first Finished message in the handshake, which
2912 * is the client's in a full handshake and the server's for a resumption. See
2913 * https://tools.ietf.org/html/rfc5929#section-3.1. */
2914 const uint8_t *finished = ssl->s3->previous_client_finished;
2915 size_t finished_len = ssl->s3->previous_client_finished_len;
2916 if (ssl->hit) {
2917 /* tls-unique is broken for resumed sessions unless EMS is used. */
2918 if (!ssl->session->extended_master_secret) {
2919 goto err;
2920 }
2921 finished = ssl->s3->previous_server_finished;
2922 finished_len = ssl->s3->previous_server_finished_len;
2923 }
2924
2925 if (!ssl->s3->initial_handshake_complete ||
2926 ssl->version < TLS1_VERSION) {
2927 goto err;
2928 }
2929
2930 *out_len = finished_len;
2931 if (finished_len > max_out) {
2932 *out_len = max_out;
2933 }
2934
2935 memcpy(out, finished, *out_len);
2936 return 1;
2937
2938err:
2939 *out_len = 0;
2940 memset(out, 0, max_out);
2941 return 0;
2942}
2943
David Benjaminda881e92015-04-26 14:45:04 -04002944int SSL_CTX_sess_connect(const SSL_CTX *ctx) { return 0; }
2945int SSL_CTX_sess_connect_good(const SSL_CTX *ctx) { return 0; }
2946int SSL_CTX_sess_connect_renegotiate(const SSL_CTX *ctx) { return 0; }
2947int SSL_CTX_sess_accept(const SSL_CTX *ctx) { return 0; }
2948int SSL_CTX_sess_accept_renegotiate(const SSL_CTX *ctx) { return 0; }
2949int SSL_CTX_sess_accept_good(const SSL_CTX *ctx) { return 0; }
2950int SSL_CTX_sess_hits(const SSL_CTX *ctx) { return 0; }
2951int SSL_CTX_sess_cb_hits(const SSL_CTX *ctx) { return 0; }
2952int SSL_CTX_sess_misses(const SSL_CTX *ctx) { return 0; }
2953int SSL_CTX_sess_timeouts(const SSL_CTX *ctx) { return 0; }
2954int SSL_CTX_sess_cache_full(const SSL_CTX *ctx) { return 0; }