blob: aa621e1c42bed3bc4e9b8ee88bda3faaa991f009 [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) {
David Benjamin3570d732015-06-29 00:28:17 -0400172 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_METHOD_SPECIFIED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800173 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) {
David Benjamin3570d732015-06-29 00:28:17 -0400247 OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_CTX);
Adam Langleyfcf25832014-12-18 17:42:32 -0800248 return NULL;
249 }
250 if (ctx->method == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400251 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
Adam Langleyfcf25832014-12-18 17:42:32 -0800252 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);
David Benjamin3570d732015-06-29 00:28:17 -0400361 OPENSSL_PUT_ERROR(SSL, 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) {
David Benjamin3570d732015-06-29 00:28:17 -0400369 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -0800370 return 0;
371 }
372 ctx->sid_ctx_length = sid_ctx_len;
373 memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700374
Adam Langleyfcf25832014-12-18 17:42:32 -0800375 return 1;
376}
Adam Langley95c29f32014-06-20 12:00:00 -0700377
Adam Langleyfcf25832014-12-18 17:42:32 -0800378int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
379 unsigned int sid_ctx_len) {
380 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
David Benjamin3570d732015-06-29 00:28:17 -0400381 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -0800382 return 0;
383 }
384 ssl->sid_ctx_length = sid_ctx_len;
385 memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700386
Adam Langleyfcf25832014-12-18 17:42:32 -0800387 return 1;
388}
Adam Langley95c29f32014-06-20 12:00:00 -0700389
Adam Langleyfcf25832014-12-18 17:42:32 -0800390int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800391 ctx->generate_session_id = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -0800392 return 1;
393}
Adam Langley95c29f32014-06-20 12:00:00 -0700394
Adam Langleyfcf25832014-12-18 17:42:32 -0800395int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800396 ssl->generate_session_id = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -0800397 return 1;
398}
Adam Langley95c29f32014-06-20 12:00:00 -0700399
Adam Langleyfcf25832014-12-18 17:42:32 -0800400int SSL_has_matching_session_id(const SSL *ssl, const uint8_t *id,
401 unsigned int id_len) {
402 /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how we
403 * can "construct" a session to give us the desired check - ie. to find if
404 * there's a session in the hash table that would conflict with any new
405 * session built out of this id/id_len and the ssl_version in use by this
406 * SSL. */
407 SSL_SESSION r, *p;
Adam Langley95c29f32014-06-20 12:00:00 -0700408
Adam Langleyfcf25832014-12-18 17:42:32 -0800409 if (id_len > sizeof r.session_id) {
410 return 0;
411 }
Adam Langley95c29f32014-06-20 12:00:00 -0700412
Adam Langleyfcf25832014-12-18 17:42:32 -0800413 r.ssl_version = ssl->version;
414 r.session_id_length = id_len;
415 memcpy(r.session_id, id, id_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700416
Adam Langley4bdb6e42015-05-15 15:29:21 -0700417 CRYPTO_MUTEX_lock_read(&ssl->ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -0800418 p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r);
Adam Langley4bdb6e42015-05-15 15:29:21 -0700419 CRYPTO_MUTEX_unlock(&ssl->ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -0800420 return p != NULL;
421}
Adam Langley95c29f32014-06-20 12:00:00 -0700422
Adam Langleyfcf25832014-12-18 17:42:32 -0800423int SSL_CTX_set_purpose(SSL_CTX *s, int purpose) {
424 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
425}
426
427int SSL_set_purpose(SSL *s, int purpose) {
428 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
429}
430
431int SSL_CTX_set_trust(SSL_CTX *s, int trust) {
432 return X509_VERIFY_PARAM_set_trust(s->param, trust);
433}
434
435int SSL_set_trust(SSL *s, int trust) {
436 return X509_VERIFY_PARAM_set_trust(s->param, trust);
437}
438
439int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm) {
440 return X509_VERIFY_PARAM_set1(ctx->param, vpm);
441}
442
443int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm) {
444 return X509_VERIFY_PARAM_set1(ssl->param, vpm);
445}
Adam Langley95c29f32014-06-20 12:00:00 -0700446
Adam Langley858a88d2014-06-20 12:00:00 -0700447void ssl_cipher_preference_list_free(
Adam Langleyfcf25832014-12-18 17:42:32 -0800448 struct ssl_cipher_preference_list_st *cipher_list) {
David Benjamin5d1ec732015-04-22 13:38:00 -0400449 if (cipher_list == NULL) {
450 return;
451 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800452 sk_SSL_CIPHER_free(cipher_list->ciphers);
453 OPENSSL_free(cipher_list->in_group_flags);
454 OPENSSL_free(cipher_list);
455}
Adam Langley858a88d2014-06-20 12:00:00 -0700456
Adam Langleyfcf25832014-12-18 17:42:32 -0800457struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_dup(
458 struct ssl_cipher_preference_list_st *cipher_list) {
459 struct ssl_cipher_preference_list_st *ret = NULL;
460 size_t n = sk_SSL_CIPHER_num(cipher_list->ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700461
Adam Langleyfcf25832014-12-18 17:42:32 -0800462 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
463 if (!ret) {
464 goto err;
465 }
466
467 ret->ciphers = NULL;
468 ret->in_group_flags = NULL;
469 ret->ciphers = sk_SSL_CIPHER_dup(cipher_list->ciphers);
470 if (!ret->ciphers) {
471 goto err;
472 }
473 ret->in_group_flags = BUF_memdup(cipher_list->in_group_flags, n);
474 if (!ret->in_group_flags) {
475 goto err;
476 }
477
478 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -0700479
480err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400481 ssl_cipher_preference_list_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -0800482 return NULL;
483}
Adam Langley858a88d2014-06-20 12:00:00 -0700484
Adam Langleyfcf25832014-12-18 17:42:32 -0800485struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_from_ciphers(
David Benjamin60da0cd2015-05-03 15:21:28 -0400486 STACK_OF(SSL_CIPHER) *ciphers) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800487 struct ssl_cipher_preference_list_st *ret = NULL;
488 size_t n = sk_SSL_CIPHER_num(ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700489
Adam Langleyfcf25832014-12-18 17:42:32 -0800490 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
491 if (!ret) {
492 goto err;
493 }
494 ret->ciphers = NULL;
495 ret->in_group_flags = NULL;
496 ret->ciphers = sk_SSL_CIPHER_dup(ciphers);
497 if (!ret->ciphers) {
498 goto err;
499 }
500 ret->in_group_flags = OPENSSL_malloc(n);
501 if (!ret->in_group_flags) {
502 goto err;
503 }
504 memset(ret->in_group_flags, 0, n);
505 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -0700506
507err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400508 ssl_cipher_preference_list_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -0800509 return NULL;
510}
Adam Langley858a88d2014-06-20 12:00:00 -0700511
Adam Langleyfcf25832014-12-18 17:42:32 -0800512X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) { return ctx->param; }
Adam Langley95c29f32014-06-20 12:00:00 -0700513
Adam Langleyfcf25832014-12-18 17:42:32 -0800514X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) { return ssl->param; }
Adam Langley95c29f32014-06-20 12:00:00 -0700515
David Benjamin7481d392015-07-05 19:38:46 -0400516void SSL_certs_clear(SSL *ssl) { ssl_cert_clear_certs(ssl->cert); }
Adam Langley95c29f32014-06-20 12:00:00 -0700517
David Benjamin4831c332015-05-16 11:43:13 -0400518void SSL_free(SSL *ssl) {
519 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800520 return;
521 }
Adam Langley95c29f32014-06-20 12:00:00 -0700522
David Benjamin4831c332015-05-16 11:43:13 -0400523 X509_VERIFY_PARAM_free(ssl->param);
Adam Langley95c29f32014-06-20 12:00:00 -0700524
David Benjamin4831c332015-05-16 11:43:13 -0400525 CRYPTO_free_ex_data(&g_ex_data_class_ssl, ssl, &ssl->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700526
David Benjamin4831c332015-05-16 11:43:13 -0400527 if (ssl->bbio != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800528 /* If the buffering BIO is in place, pop it off */
David Benjamin4831c332015-05-16 11:43:13 -0400529 if (ssl->bbio == ssl->wbio) {
530 ssl->wbio = BIO_pop(ssl->wbio);
Adam Langleyfcf25832014-12-18 17:42:32 -0800531 }
David Benjamin4831c332015-05-16 11:43:13 -0400532 BIO_free(ssl->bbio);
533 ssl->bbio = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800534 }
Adam Langley95c29f32014-06-20 12:00:00 -0700535
David Benjamin4831c332015-05-16 11:43:13 -0400536 int free_wbio = ssl->wbio != ssl->rbio;
537 BIO_free_all(ssl->rbio);
David Benjamin2755a3e2015-04-22 16:17:58 -0400538 if (free_wbio) {
David Benjamin4831c332015-05-16 11:43:13 -0400539 BIO_free_all(ssl->wbio);
Adam Langleyfcf25832014-12-18 17:42:32 -0800540 }
Adam Langley95c29f32014-06-20 12:00:00 -0700541
David Benjamin4831c332015-05-16 11:43:13 -0400542 BUF_MEM_free(ssl->init_buf);
Adam Langley95c29f32014-06-20 12:00:00 -0700543
Adam Langleyfcf25832014-12-18 17:42:32 -0800544 /* add extra stuff */
David Benjamin4831c332015-05-16 11:43:13 -0400545 ssl_cipher_preference_list_free(ssl->cipher_list);
546 sk_SSL_CIPHER_free(ssl->cipher_list_by_id);
Adam Langley95c29f32014-06-20 12:00:00 -0700547
David Benjamin4831c332015-05-16 11:43:13 -0400548 ssl_clear_bad_session(ssl);
549 SSL_SESSION_free(ssl->session);
Adam Langley95c29f32014-06-20 12:00:00 -0700550
David Benjamin4831c332015-05-16 11:43:13 -0400551 ssl_clear_cipher_ctx(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700552
David Benjamin4831c332015-05-16 11:43:13 -0400553 ssl_cert_free(ssl->cert);
Adam Langley0289c732014-06-20 12:00:00 -0700554
David Benjamin4831c332015-05-16 11:43:13 -0400555 OPENSSL_free(ssl->tlsext_hostname);
556 SSL_CTX_free(ssl->initial_ctx);
557 OPENSSL_free(ssl->tlsext_ecpointformatlist);
558 OPENSSL_free(ssl->tlsext_ellipticcurvelist);
559 OPENSSL_free(ssl->alpn_client_proto_list);
560 EVP_PKEY_free(ssl->tlsext_channel_id_private);
561 OPENSSL_free(ssl->psk_identity_hint);
562 sk_X509_NAME_pop_free(ssl->client_CA, X509_NAME_free);
563 OPENSSL_free(ssl->next_proto_negotiated);
564 sk_SRTP_PROTECTION_PROFILE_free(ssl->srtp_profiles);
Adam Langley95c29f32014-06-20 12:00:00 -0700565
David Benjamin4831c332015-05-16 11:43:13 -0400566 if (ssl->method != NULL) {
567 ssl->method->ssl_free(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800568 }
David Benjamin4831c332015-05-16 11:43:13 -0400569 SSL_CTX_free(ssl->ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700570
David Benjamin4831c332015-05-16 11:43:13 -0400571 OPENSSL_free(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800572}
Adam Langley95c29f32014-06-20 12:00:00 -0700573
Adam Langleyfcf25832014-12-18 17:42:32 -0800574void SSL_set_bio(SSL *s, BIO *rbio, BIO *wbio) {
575 /* If the output buffering BIO is still in place, remove it. */
576 if (s->bbio != NULL) {
577 if (s->wbio == s->bbio) {
578 s->wbio = s->wbio->next_bio;
579 s->bbio->next_bio = NULL;
580 }
581 }
Adam Langley95c29f32014-06-20 12:00:00 -0700582
David Benjamin2755a3e2015-04-22 16:17:58 -0400583 if (s->rbio != rbio) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800584 BIO_free_all(s->rbio);
585 }
David Benjamin2755a3e2015-04-22 16:17:58 -0400586 if (s->wbio != wbio && s->rbio != s->wbio) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800587 BIO_free_all(s->wbio);
588 }
589 s->rbio = rbio;
590 s->wbio = wbio;
591}
Adam Langley95c29f32014-06-20 12:00:00 -0700592
Adam Langleyfcf25832014-12-18 17:42:32 -0800593BIO *SSL_get_rbio(const SSL *s) { return s->rbio; }
Adam Langley95c29f32014-06-20 12:00:00 -0700594
Adam Langleyfcf25832014-12-18 17:42:32 -0800595BIO *SSL_get_wbio(const SSL *s) { return s->wbio; }
Adam Langley95c29f32014-06-20 12:00:00 -0700596
Adam Langleyfcf25832014-12-18 17:42:32 -0800597int SSL_get_fd(const SSL *s) { return SSL_get_rfd(s); }
Adam Langley95c29f32014-06-20 12:00:00 -0700598
Adam Langleyfcf25832014-12-18 17:42:32 -0800599int SSL_get_rfd(const SSL *s) {
600 int ret = -1;
601 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700602
Adam Langleyfcf25832014-12-18 17:42:32 -0800603 b = SSL_get_rbio(s);
604 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
605 if (r != NULL) {
606 BIO_get_fd(r, &ret);
607 }
608 return ret;
609}
Adam Langley95c29f32014-06-20 12:00:00 -0700610
Adam Langleyfcf25832014-12-18 17:42:32 -0800611int SSL_get_wfd(const SSL *s) {
612 int ret = -1;
613 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700614
Adam Langleyfcf25832014-12-18 17:42:32 -0800615 b = SSL_get_wbio(s);
616 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
617 if (r != NULL) {
618 BIO_get_fd(r, &ret);
619 }
Adam Langley95c29f32014-06-20 12:00:00 -0700620
Adam Langleyfcf25832014-12-18 17:42:32 -0800621 return ret;
622}
Adam Langley95c29f32014-06-20 12:00:00 -0700623
Adam Langleyfcf25832014-12-18 17:42:32 -0800624int SSL_set_fd(SSL *s, int fd) {
625 int ret = 0;
626 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700627
Adam Langleyfcf25832014-12-18 17:42:32 -0800628 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700629
Adam Langleyfcf25832014-12-18 17:42:32 -0800630 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400631 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -0800632 goto err;
633 }
634 BIO_set_fd(bio, fd, BIO_NOCLOSE);
635 SSL_set_bio(s, bio, bio);
636 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700637
Adam Langley95c29f32014-06-20 12:00:00 -0700638err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800639 return ret;
640}
Adam Langley95c29f32014-06-20 12:00:00 -0700641
Adam Langleyfcf25832014-12-18 17:42:32 -0800642int SSL_set_wfd(SSL *s, int fd) {
643 int ret = 0;
644 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700645
Adam Langleyfcf25832014-12-18 17:42:32 -0800646 if (s->rbio == NULL || BIO_method_type(s->rbio) != BIO_TYPE_FD ||
647 (int)BIO_get_fd(s->rbio, NULL) != fd) {
648 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700649
Adam Langleyfcf25832014-12-18 17:42:32 -0800650 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400651 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -0800652 goto err;
653 }
654 BIO_set_fd(bio, fd, BIO_NOCLOSE);
655 SSL_set_bio(s, SSL_get_rbio(s), bio);
656 } else {
657 SSL_set_bio(s, SSL_get_rbio(s), SSL_get_rbio(s));
658 }
659
660 ret = 1;
661
Adam Langley95c29f32014-06-20 12:00:00 -0700662err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800663 return ret;
664}
Adam Langley95c29f32014-06-20 12:00:00 -0700665
Adam Langleyfcf25832014-12-18 17:42:32 -0800666int SSL_set_rfd(SSL *s, int fd) {
667 int ret = 0;
668 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700669
Adam Langleyfcf25832014-12-18 17:42:32 -0800670 if (s->wbio == NULL || BIO_method_type(s->wbio) != BIO_TYPE_FD ||
671 (int)BIO_get_fd(s->wbio, NULL) != fd) {
672 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700673
Adam Langleyfcf25832014-12-18 17:42:32 -0800674 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400675 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -0800676 goto err;
677 }
678 BIO_set_fd(bio, fd, BIO_NOCLOSE);
679 SSL_set_bio(s, bio, SSL_get_wbio(s));
680 } else {
681 SSL_set_bio(s, SSL_get_wbio(s), SSL_get_wbio(s));
682 }
683 ret = 1;
684
Adam Langley95c29f32014-06-20 12:00:00 -0700685err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800686 return ret;
687}
Adam Langley95c29f32014-06-20 12:00:00 -0700688
689/* return length of latest Finished message we sent, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -0800690size_t SSL_get_finished(const SSL *s, void *buf, size_t count) {
691 size_t ret = 0;
692
693 if (s->s3 != NULL) {
694 ret = s->s3->tmp.finish_md_len;
695 if (count > ret) {
696 count = ret;
697 }
698 memcpy(buf, s->s3->tmp.finish_md, count);
699 }
700
701 return ret;
702}
Adam Langley95c29f32014-06-20 12:00:00 -0700703
704/* return length of latest Finished message we expected, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -0800705size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count) {
706 size_t ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700707
Adam Langleyfcf25832014-12-18 17:42:32 -0800708 if (s->s3 != NULL) {
709 ret = s->s3->tmp.peer_finish_md_len;
710 if (count > ret) {
711 count = ret;
712 }
713 memcpy(buf, s->s3->tmp.peer_finish_md, count);
714 }
Adam Langley95c29f32014-06-20 12:00:00 -0700715
Adam Langleyfcf25832014-12-18 17:42:32 -0800716 return ret;
717}
Adam Langley95c29f32014-06-20 12:00:00 -0700718
Adam Langleyfcf25832014-12-18 17:42:32 -0800719int SSL_get_verify_mode(const SSL *s) { return s->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -0700720
Adam Langleyfcf25832014-12-18 17:42:32 -0800721int SSL_get_verify_depth(const SSL *s) {
722 return X509_VERIFY_PARAM_get_depth(s->param);
723}
Adam Langley95c29f32014-06-20 12:00:00 -0700724
Adam Langleyfcf25832014-12-18 17:42:32 -0800725int (*SSL_get_verify_callback(const SSL *s))(int, X509_STORE_CTX *) {
726 return s->verify_callback;
727}
Adam Langley95c29f32014-06-20 12:00:00 -0700728
Adam Langleyfcf25832014-12-18 17:42:32 -0800729int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) { return ctx->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -0700730
Adam Langleyfcf25832014-12-18 17:42:32 -0800731int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) {
732 return X509_VERIFY_PARAM_get_depth(ctx->param);
733}
Adam Langley95c29f32014-06-20 12:00:00 -0700734
Adam Langleyfcf25832014-12-18 17:42:32 -0800735int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int, X509_STORE_CTX *) {
736 return ctx->default_verify_callback;
737}
Adam Langley95c29f32014-06-20 12:00:00 -0700738
Adam Langleyfcf25832014-12-18 17:42:32 -0800739void SSL_set_verify(SSL *s, int mode,
740 int (*callback)(int ok, X509_STORE_CTX *ctx)) {
741 s->verify_mode = mode;
742 if (callback != NULL) {
743 s->verify_callback = callback;
744 }
745}
Adam Langley95c29f32014-06-20 12:00:00 -0700746
Adam Langleyfcf25832014-12-18 17:42:32 -0800747void SSL_set_verify_depth(SSL *s, int depth) {
748 X509_VERIFY_PARAM_set_depth(s->param, depth);
749}
Adam Langley95c29f32014-06-20 12:00:00 -0700750
David Benjamin9a41d1b2015-05-16 01:30:09 -0400751int SSL_CTX_get_read_ahead(const SSL_CTX *ctx) { return 0; }
Adam Langley95c29f32014-06-20 12:00:00 -0700752
David Benjamin9a41d1b2015-05-16 01:30:09 -0400753int SSL_get_read_ahead(const SSL *s) { return 0; }
Adam Langley95c29f32014-06-20 12:00:00 -0700754
David Benjamin9a41d1b2015-05-16 01:30:09 -0400755void SSL_CTX_set_read_ahead(SSL_CTX *ctx, int yes) { }
David Benjamin61ecccf2015-05-05 09:44:51 -0400756
David Benjamin9a41d1b2015-05-16 01:30:09 -0400757void SSL_set_read_ahead(SSL *s, int yes) { }
David Benjamin61ecccf2015-05-05 09:44:51 -0400758
Adam Langleyfcf25832014-12-18 17:42:32 -0800759int SSL_pending(const SSL *s) {
David Benjamin904dc722015-05-30 16:39:07 -0400760 if (s->rstate == SSL_ST_READ_BODY) {
761 return 0;
762 }
763
764 return (s->s3->rrec.type == SSL3_RT_APPLICATION_DATA) ? s->s3->rrec.length
765 : 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800766}
Adam Langley95c29f32014-06-20 12:00:00 -0700767
Adam Langleyfcf25832014-12-18 17:42:32 -0800768X509 *SSL_get_peer_certificate(const SSL *s) {
769 X509 *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700770
Adam Langleyfcf25832014-12-18 17:42:32 -0800771 if (s == NULL || s->session == NULL) {
772 r = NULL;
773 } else {
774 r = s->session->peer;
775 }
Adam Langley95c29f32014-06-20 12:00:00 -0700776
Adam Langleyfcf25832014-12-18 17:42:32 -0800777 if (r == NULL) {
778 return NULL;
779 }
Adam Langley95c29f32014-06-20 12:00:00 -0700780
Adam Langleyfcf25832014-12-18 17:42:32 -0800781 return X509_up_ref(r);
782}
783
David Benjamin60da0cd2015-05-03 15:21:28 -0400784STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s) {
785 STACK_OF(X509) *r;
Adam Langleyfcf25832014-12-18 17:42:32 -0800786
787 if (s == NULL || s->session == NULL || s->session->sess_cert == NULL) {
788 r = NULL;
789 } else {
790 r = s->session->sess_cert->cert_chain;
791 }
792
793 /* If we are a client, cert_chain includes the peer's own certificate; if we
794 * are a server, it does not. */
795 return r;
796}
Adam Langley95c29f32014-06-20 12:00:00 -0700797
Adam Langley95c29f32014-06-20 12:00:00 -0700798/* Fix this so it checks all the valid key/cert options */
Adam Langleyfcf25832014-12-18 17:42:32 -0800799int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
David Benjamind1d80782015-07-05 11:54:09 -0400800 if (ctx == NULL || ctx->cert == NULL || ctx->cert->x509 == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400801 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800802 return 0;
803 }
804
David Benjamind1d80782015-07-05 11:54:09 -0400805 if (ctx->cert->privatekey == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400806 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800807 return 0;
808 }
809
David Benjamind1d80782015-07-05 11:54:09 -0400810 return X509_check_private_key(ctx->cert->x509, ctx->cert->privatekey);
Adam Langleyfcf25832014-12-18 17:42:32 -0800811}
Adam Langley95c29f32014-06-20 12:00:00 -0700812
813/* Fix this function so that it takes an optional type parameter */
Adam Langleyfcf25832014-12-18 17:42:32 -0800814int SSL_check_private_key(const SSL *ssl) {
815 if (ssl == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400816 OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
Adam Langleyfcf25832014-12-18 17:42:32 -0800817 return 0;
818 }
Adam Langley95c29f32014-06-20 12:00:00 -0700819
Adam Langleyfcf25832014-12-18 17:42:32 -0800820 if (ssl->cert == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400821 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800822 return 0;
823 }
Adam Langley95c29f32014-06-20 12:00:00 -0700824
David Benjamind1d80782015-07-05 11:54:09 -0400825 if (ssl->cert->x509 == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400826 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800827 return 0;
828 }
David Benjamin0b145c22014-11-26 20:10:09 -0500829
David Benjamind1d80782015-07-05 11:54:09 -0400830 if (ssl->cert->privatekey == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400831 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800832 return 0;
833 }
Adam Langley95c29f32014-06-20 12:00:00 -0700834
David Benjamind1d80782015-07-05 11:54:09 -0400835 return X509_check_private_key(ssl->cert->x509, ssl->cert->privatekey);
Adam Langleyfcf25832014-12-18 17:42:32 -0800836}
Adam Langley95c29f32014-06-20 12:00:00 -0700837
Adam Langleyfcf25832014-12-18 17:42:32 -0800838int SSL_accept(SSL *s) {
839 if (s->handshake_func == 0) {
840 /* Not properly initialized yet */
841 SSL_set_accept_state(s);
842 }
David Benjamin0b145c22014-11-26 20:10:09 -0500843
Adam Langleyfcf25832014-12-18 17:42:32 -0800844 if (s->handshake_func != s->method->ssl_accept) {
David Benjamin3570d732015-06-29 00:28:17 -0400845 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800846 return -1;
847 }
Adam Langley95c29f32014-06-20 12:00:00 -0700848
Adam Langleyfcf25832014-12-18 17:42:32 -0800849 return s->handshake_func(s);
850}
Adam Langley95c29f32014-06-20 12:00:00 -0700851
Adam Langleyfcf25832014-12-18 17:42:32 -0800852int SSL_connect(SSL *s) {
853 if (s->handshake_func == 0) {
854 /* Not properly initialized yet */
855 SSL_set_connect_state(s);
856 }
Adam Langley95c29f32014-06-20 12:00:00 -0700857
Adam Langleyfcf25832014-12-18 17:42:32 -0800858 if (s->handshake_func != s->method->ssl_connect) {
David Benjamin3570d732015-06-29 00:28:17 -0400859 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800860 return -1;
861 }
Adam Langley95c29f32014-06-20 12:00:00 -0700862
Adam Langleyfcf25832014-12-18 17:42:32 -0800863 return s->handshake_func(s);
864}
Adam Langley95c29f32014-06-20 12:00:00 -0700865
Adam Langleyfcf25832014-12-18 17:42:32 -0800866long SSL_get_default_timeout(const SSL *s) {
867 return SSL_DEFAULT_SESSION_TIMEOUT;
868}
Adam Langley95c29f32014-06-20 12:00:00 -0700869
Adam Langleyfcf25832014-12-18 17:42:32 -0800870int SSL_read(SSL *s, void *buf, int num) {
871 if (s->handshake_func == 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400872 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800873 return -1;
874 }
Adam Langley95c29f32014-06-20 12:00:00 -0700875
Adam Langleyfcf25832014-12-18 17:42:32 -0800876 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
877 s->rwstate = SSL_NOTHING;
878 return 0;
879 }
Adam Langley95c29f32014-06-20 12:00:00 -0700880
David Benjamin904dc722015-05-30 16:39:07 -0400881 ERR_clear_system_error();
882 return s->method->ssl_read_app_data(s, buf, num, 0);
Adam Langleyfcf25832014-12-18 17:42:32 -0800883}
Adam Langley95c29f32014-06-20 12:00:00 -0700884
Adam Langleyfcf25832014-12-18 17:42:32 -0800885int SSL_peek(SSL *s, void *buf, int num) {
886 if (s->handshake_func == 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400887 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800888 return -1;
889 }
Adam Langley95c29f32014-06-20 12:00:00 -0700890
Adam Langleyfcf25832014-12-18 17:42:32 -0800891 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
892 return 0;
893 }
Adam Langley95c29f32014-06-20 12:00:00 -0700894
David Benjamin904dc722015-05-30 16:39:07 -0400895 ERR_clear_system_error();
896 return s->method->ssl_read_app_data(s, buf, num, 1);
Adam Langleyfcf25832014-12-18 17:42:32 -0800897}
Adam Langley95c29f32014-06-20 12:00:00 -0700898
Adam Langleyfcf25832014-12-18 17:42:32 -0800899int SSL_write(SSL *s, const void *buf, int num) {
900 if (s->handshake_func == 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400901 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800902 return -1;
903 }
Adam Langley95c29f32014-06-20 12:00:00 -0700904
Adam Langleyfcf25832014-12-18 17:42:32 -0800905 if (s->shutdown & SSL_SENT_SHUTDOWN) {
906 s->rwstate = SSL_NOTHING;
David Benjamin3570d732015-06-29 00:28:17 -0400907 OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
Adam Langleyfcf25832014-12-18 17:42:32 -0800908 return -1;
909 }
Adam Langley95c29f32014-06-20 12:00:00 -0700910
David Benjamin904dc722015-05-30 16:39:07 -0400911 ERR_clear_system_error();
912 return s->method->ssl_write_app_data(s, buf, num);
Adam Langleyfcf25832014-12-18 17:42:32 -0800913}
Adam Langley95c29f32014-06-20 12:00:00 -0700914
Adam Langleyfcf25832014-12-18 17:42:32 -0800915int SSL_shutdown(SSL *s) {
916 /* Note that this function behaves differently from what one might expect.
917 * Return values are 0 for no success (yet), 1 for success; but calling it
918 * once is usually not enough, even if blocking I/O is used (see
919 * ssl3_shutdown). */
Adam Langley95c29f32014-06-20 12:00:00 -0700920
Adam Langleyfcf25832014-12-18 17:42:32 -0800921 if (s->handshake_func == 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400922 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800923 return -1;
924 }
Adam Langley95c29f32014-06-20 12:00:00 -0700925
David Benjamin904dc722015-05-30 16:39:07 -0400926 if (SSL_in_init(s)) {
927 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800928 }
Adam Langley95c29f32014-06-20 12:00:00 -0700929
David Benjamin904dc722015-05-30 16:39:07 -0400930 /* Do nothing if configured not to send a close_notify. */
931 if (s->quiet_shutdown) {
932 s->shutdown = SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN;
933 return 1;
934 }
935
936 if (!(s->shutdown & SSL_SENT_SHUTDOWN)) {
937 s->shutdown |= SSL_SENT_SHUTDOWN;
938 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_CLOSE_NOTIFY);
939
940 /* our shutdown alert has been sent now, and if it still needs to be
941 * written, s->s3->alert_dispatch will be true */
942 if (s->s3->alert_dispatch) {
943 return -1; /* return WANT_WRITE */
944 }
945 } else if (s->s3->alert_dispatch) {
946 /* resend it if not sent */
947 int ret = s->method->ssl_dispatch_alert(s);
948 if (ret == -1) {
949 /* we only get to return -1 here the 2nd/Nth invocation, we must have
950 * already signalled return 0 upon a previous invoation, return
951 * WANT_WRITE */
952 return ret;
953 }
954 } else if (!(s->shutdown & SSL_RECEIVED_SHUTDOWN)) {
955 /* If we are waiting for a close from our peer, we are closed */
956 s->method->ssl_read_close_notify(s);
957 if (!(s->shutdown & SSL_RECEIVED_SHUTDOWN)) {
958 return -1; /* return WANT_READ */
959 }
960 }
961
962 if (s->shutdown == (SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN) &&
963 !s->s3->alert_dispatch) {
964 return 1;
965 } else {
966 return 0;
967 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800968}
Adam Langley95c29f32014-06-20 12:00:00 -0700969
David Benjamin44d3eed2015-05-21 01:29:55 -0400970int SSL_renegotiate(SSL *ssl) {
971 /* Caller-initiated renegotiation is not supported. */
David Benjamin3570d732015-06-29 00:28:17 -0400972 OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
David Benjamin44d3eed2015-05-21 01:29:55 -0400973 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800974}
Adam Langley95c29f32014-06-20 12:00:00 -0700975
David Benjamin44d3eed2015-05-21 01:29:55 -0400976int SSL_renegotiate_pending(SSL *ssl) {
977 return SSL_in_init(ssl) && ssl->s3->initial_handshake_complete;
Adam Langleyfcf25832014-12-18 17:42:32 -0800978}
Adam Langley95c29f32014-06-20 12:00:00 -0700979
David Benjamin61ecccf2015-05-05 09:44:51 -0400980uint32_t SSL_CTX_set_options(SSL_CTX *ctx, uint32_t options) {
981 ctx->options |= options;
982 return ctx->options;
983}
984
985uint32_t SSL_set_options(SSL *ssl, uint32_t options) {
986 ssl->options |= options;
987 return ssl->options;
988}
989
990uint32_t SSL_CTX_clear_options(SSL_CTX *ctx, uint32_t options) {
991 ctx->options &= ~options;
992 return ctx->options;
993}
994
995uint32_t SSL_clear_options(SSL *ssl, uint32_t options) {
996 ssl->options &= ~options;
997 return ssl->options;
998}
999
1000uint32_t SSL_CTX_get_options(const SSL_CTX *ctx) { return ctx->options; }
1001
1002uint32_t SSL_get_options(const SSL *ssl) { return ssl->options; }
1003
1004uint32_t SSL_CTX_set_mode(SSL_CTX *ctx, uint32_t mode) {
1005 ctx->mode |= mode;
1006 return ctx->mode;
1007}
1008
1009uint32_t SSL_set_mode(SSL *ssl, uint32_t mode) {
1010 ssl->mode |= mode;
1011 return ssl->mode;
1012}
1013
1014uint32_t SSL_CTX_clear_mode(SSL_CTX *ctx, uint32_t mode) {
1015 ctx->mode &= ~mode;
1016 return ctx->mode;
1017}
1018
1019uint32_t SSL_clear_mode(SSL *ssl, uint32_t mode) {
1020 ssl->mode &= ~mode;
1021 return ssl->mode;
1022}
1023
1024uint32_t SSL_CTX_get_mode(const SSL_CTX *ctx) { return ctx->mode; }
1025
1026uint32_t SSL_get_mode(const SSL *ssl) { return ssl->mode; }
1027
David Benjamin1d0a1942015-04-26 15:35:35 -04001028size_t SSL_CTX_get_max_cert_list(const SSL_CTX *ctx) {
1029 return ctx->max_cert_list;
1030}
1031
1032void SSL_CTX_set_max_cert_list(SSL_CTX *ctx, size_t max_cert_list) {
1033 if (max_cert_list > kMaxHandshakeSize) {
1034 max_cert_list = kMaxHandshakeSize;
1035 }
1036 ctx->max_cert_list = (uint32_t)max_cert_list;
1037}
1038
1039size_t SSL_get_max_cert_list(const SSL *ssl) {
1040 return ssl->max_cert_list;
1041}
1042
1043void SSL_set_max_cert_list(SSL *ssl, size_t max_cert_list) {
1044 if (max_cert_list > kMaxHandshakeSize) {
1045 max_cert_list = kMaxHandshakeSize;
1046 }
1047 ssl->max_cert_list = (uint32_t)max_cert_list;
1048}
1049
1050void SSL_CTX_set_max_send_fragment(SSL_CTX *ctx, size_t max_send_fragment) {
1051 if (max_send_fragment < 512) {
1052 max_send_fragment = 512;
1053 }
1054 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1055 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1056 }
1057 ctx->max_send_fragment = (uint16_t)max_send_fragment;
1058}
1059
1060void SSL_set_max_send_fragment(SSL *ssl, size_t max_send_fragment) {
1061 if (max_send_fragment < 512) {
1062 max_send_fragment = 512;
1063 }
1064 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1065 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1066 }
1067 ssl->max_send_fragment = (uint16_t)max_send_fragment;
1068}
1069
David Benjamincb9cf792015-05-05 09:46:14 -04001070int SSL_set_mtu(SSL *ssl, unsigned mtu) {
1071 if (!SSL_IS_DTLS(ssl) || mtu < dtls1_min_mtu()) {
1072 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001073 }
David Benjamincb9cf792015-05-05 09:46:14 -04001074 ssl->d1->mtu = mtu;
1075 return 1;
1076}
1077
1078int SSL_get_secure_renegotiation_support(const SSL *ssl) {
1079 return ssl->s3->send_connection_binding;
1080}
1081
1082long SSL_ctrl(SSL *s, int cmd, long larg, void *parg) {
1083 return s->method->ssl_ctrl(s, cmd, larg, parg);
Adam Langleyfcf25832014-12-18 17:42:32 -08001084}
1085
Adam Langleyfcf25832014-12-18 17:42:32 -08001086LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) { return ctx->sessions; }
1087
David Benjamin71f7d3d2015-05-05 09:46:38 -04001088size_t SSL_CTX_sess_number(const SSL_CTX *ctx) {
1089 return lh_SSL_SESSION_num_items(ctx->sessions);
1090}
1091
1092unsigned long SSL_CTX_sess_set_cache_size(SSL_CTX *ctx, unsigned long size) {
1093 unsigned long ret = ctx->session_cache_size;
1094 ctx->session_cache_size = size;
1095 return ret;
1096}
1097
1098unsigned long SSL_CTX_sess_get_cache_size(const SSL_CTX *ctx) {
1099 return ctx->session_cache_size;
1100}
1101
1102int SSL_CTX_set_session_cache_mode(SSL_CTX *ctx, int mode) {
1103 int ret = ctx->session_cache_mode;
1104 ctx->session_cache_mode = mode;
1105 return ret;
1106}
1107
1108int SSL_CTX_get_session_cache_mode(const SSL_CTX *ctx) {
1109 return ctx->session_cache_mode;
1110}
1111
Adam Langleyfcf25832014-12-18 17:42:32 -08001112long SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg) {
David Benjamin71f7d3d2015-05-05 09:46:38 -04001113 return ctx->method->ssl_ctx_ctrl(ctx, cmd, larg, parg);
Adam Langleyfcf25832014-12-18 17:42:32 -08001114}
1115
Adam Langleyfcf25832014-12-18 17:42:32 -08001116/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001117 * preference */
David Benjamin60da0cd2015-05-03 15:21:28 -04001118STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001119 if (s == NULL) {
1120 return NULL;
1121 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001122
Adam Langleyfcf25832014-12-18 17:42:32 -08001123 if (s->cipher_list != NULL) {
1124 return s->cipher_list->ciphers;
1125 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001126
Adam Langleyfcf25832014-12-18 17:42:32 -08001127 if (s->version >= TLS1_1_VERSION && s->ctx != NULL &&
1128 s->ctx->cipher_list_tls11 != NULL) {
1129 return s->ctx->cipher_list_tls11->ciphers;
1130 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001131
Adam Langleyfcf25832014-12-18 17:42:32 -08001132 if (s->ctx != NULL && s->ctx->cipher_list != NULL) {
1133 return s->ctx->cipher_list->ciphers;
1134 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001135
Adam Langleyfcf25832014-12-18 17:42:32 -08001136 return NULL;
1137}
Adam Langley95c29f32014-06-20 12:00:00 -07001138
Adam Langleyfcf25832014-12-18 17:42:32 -08001139/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001140 * algorithm id */
David Benjamin60da0cd2015-05-03 15:21:28 -04001141STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001142 if (s == NULL) {
1143 return NULL;
1144 }
Adam Langley95c29f32014-06-20 12:00:00 -07001145
Adam Langleyfcf25832014-12-18 17:42:32 -08001146 if (s->cipher_list_by_id != NULL) {
1147 return s->cipher_list_by_id;
1148 }
Adam Langley95c29f32014-06-20 12:00:00 -07001149
Adam Langleyfcf25832014-12-18 17:42:32 -08001150 if (s->ctx != NULL && s->ctx->cipher_list_by_id != NULL) {
1151 return s->ctx->cipher_list_by_id;
1152 }
Adam Langley95c29f32014-06-20 12:00:00 -07001153
Adam Langleyfcf25832014-12-18 17:42:32 -08001154 return NULL;
1155}
Adam Langley95c29f32014-06-20 12:00:00 -07001156
Adam Langleyfcf25832014-12-18 17:42:32 -08001157/* The old interface to get the same thing as SSL_get_ciphers() */
1158const char *SSL_get_cipher_list(const SSL *s, int n) {
1159 const SSL_CIPHER *c;
David Benjamin60da0cd2015-05-03 15:21:28 -04001160 STACK_OF(SSL_CIPHER) *sk;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001161
Adam Langleyfcf25832014-12-18 17:42:32 -08001162 if (s == NULL) {
1163 return NULL;
1164 }
Adam Langley95c29f32014-06-20 12:00:00 -07001165
Adam Langleyfcf25832014-12-18 17:42:32 -08001166 sk = SSL_get_ciphers(s);
1167 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
1168 return NULL;
1169 }
Adam Langley95c29f32014-06-20 12:00:00 -07001170
Adam Langleyfcf25832014-12-18 17:42:32 -08001171 c = sk_SSL_CIPHER_value(sk, n);
1172 if (c == NULL) {
1173 return NULL;
1174 }
Adam Langley95c29f32014-06-20 12:00:00 -07001175
Adam Langleyfcf25832014-12-18 17:42:32 -08001176 return c->name;
1177}
David Benjamin5491e3f2014-09-29 19:33:09 -04001178
Adam Langleyfcf25832014-12-18 17:42:32 -08001179/* specify the ciphers to be used by default by the SSL_CTX */
1180int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
1181 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001182
Adam Langleyfcf25832014-12-18 17:42:32 -08001183 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001184 &ctx->cipher_list_by_id, str);
Adam Langleyfcf25832014-12-18 17:42:32 -08001185 /* ssl_create_cipher_list may return an empty stack if it was unable to find
1186 * a cipher matching the given rule string (for example if the rule string
1187 * specifies a cipher which has been disabled). This is not an error as far
1188 * as ssl_create_cipher_list is concerned, and hence ctx->cipher_list and
1189 * ctx->cipher_list_by_id has been updated. */
1190 if (sk == NULL) {
1191 return 0;
1192 } else if (sk_SSL_CIPHER_num(sk) == 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001193 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
Adam Langleyfcf25832014-12-18 17:42:32 -08001194 return 0;
1195 }
Adam Langley95c29f32014-06-20 12:00:00 -07001196
Adam Langleyfcf25832014-12-18 17:42:32 -08001197 return 1;
1198}
David Benjamin39482a12014-07-20 13:30:15 -04001199
Adam Langleyfcf25832014-12-18 17:42:32 -08001200int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str) {
1201 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001202
David Benjamin71f07942015-04-08 02:36:59 -04001203 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str);
Adam Langleyfcf25832014-12-18 17:42:32 -08001204 if (sk == NULL) {
1205 return 0;
1206 } else if (sk_SSL_CIPHER_num(sk) == 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001207 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
Adam Langleyfcf25832014-12-18 17:42:32 -08001208 return 0;
1209 }
David Benjamin9f2c0d72014-10-21 22:00:19 -04001210
Adam Langleyfcf25832014-12-18 17:42:32 -08001211 return 1;
1212}
Adam Langley95c29f32014-06-20 12:00:00 -07001213
Adam Langleyfcf25832014-12-18 17:42:32 -08001214/* specify the ciphers to be used by the SSL */
1215int SSL_set_cipher_list(SSL *s, const char *str) {
1216 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001217
Adam Langleyfcf25832014-12-18 17:42:32 -08001218 sk = ssl_create_cipher_list(s->ctx->method, &s->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001219 &s->cipher_list_by_id, str);
David Benjamin39482a12014-07-20 13:30:15 -04001220
Adam Langleyfcf25832014-12-18 17:42:32 -08001221 /* see comment in SSL_CTX_set_cipher_list */
1222 if (sk == NULL) {
1223 return 0;
1224 } else if (sk_SSL_CIPHER_num(sk) == 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001225 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
Adam Langleyfcf25832014-12-18 17:42:32 -08001226 return 0;
1227 }
David Benjamin39482a12014-07-20 13:30:15 -04001228
Adam Langleyfcf25832014-12-18 17:42:32 -08001229 return 1;
1230}
Adam Langley95c29f32014-06-20 12:00:00 -07001231
Adam Langleyfcf25832014-12-18 17:42:32 -08001232int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, uint8_t *p) {
1233 size_t i;
1234 const SSL_CIPHER *c;
1235 CERT *ct = s->cert;
1236 uint8_t *q;
1237 /* Set disabled masks for this session */
1238 ssl_set_client_disabled(s);
Adam Langley29707792014-06-20 12:00:00 -07001239
Adam Langleyfcf25832014-12-18 17:42:32 -08001240 if (sk == NULL) {
1241 return 0;
1242 }
1243 q = p;
Adam Langley95c29f32014-06-20 12:00:00 -07001244
Adam Langleyfcf25832014-12-18 17:42:32 -08001245 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
1246 c = sk_SSL_CIPHER_value(sk, i);
1247 /* Skip disabled ciphers */
1248 if (c->algorithm_ssl & ct->mask_ssl ||
1249 c->algorithm_mkey & ct->mask_k ||
1250 c->algorithm_auth & ct->mask_a) {
1251 continue;
1252 }
David Benjamina1c90a52015-05-30 17:03:14 -04001253 s2n(ssl_cipher_get_value(c), p);
Adam Langleyfcf25832014-12-18 17:42:32 -08001254 }
1255
1256 /* If all ciphers were disabled, return the error to the caller. */
1257 if (p == q) {
1258 return 0;
1259 }
1260
Adam Langley5021b222015-06-12 18:27:58 -07001261 /* For SSLv3, the SCSV is added. Otherwise the renegotiation extension is
1262 * added. */
1263 if (s->client_version == SSL3_VERSION &&
1264 !s->s3->initial_handshake_complete) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001265 s2n(SSL3_CK_SCSV & 0xffff, p);
Adam Langley5021b222015-06-12 18:27:58 -07001266 /* The renegotiation extension is required to be at index zero. */
1267 s->s3->tmp.extensions.sent |= (1u << 0);
Adam Langleyfcf25832014-12-18 17:42:32 -08001268 }
1269
Adam Langley5f0efe02015-02-20 13:03:16 -08001270 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001271 s2n(SSL3_CK_FALLBACK_SCSV & 0xffff, p);
1272 }
1273
1274 return p - q;
1275}
1276
1277STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs) {
1278 CBS cipher_suites = *cbs;
1279 const SSL_CIPHER *c;
David Benjamin60da0cd2015-05-03 15:21:28 -04001280 STACK_OF(SSL_CIPHER) *sk;
Adam Langleyfcf25832014-12-18 17:42:32 -08001281
1282 if (s->s3) {
1283 s->s3->send_connection_binding = 0;
1284 }
1285
1286 if (CBS_len(&cipher_suites) % 2 != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001287 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
Adam Langleyfcf25832014-12-18 17:42:32 -08001288 return NULL;
1289 }
1290
1291 sk = sk_SSL_CIPHER_new_null();
1292 if (sk == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001293 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001294 goto err;
1295 }
1296
Adam Langleyfcf25832014-12-18 17:42:32 -08001297 while (CBS_len(&cipher_suites) > 0) {
1298 uint16_t cipher_suite;
1299
1300 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
David Benjamin3570d732015-06-29 00:28:17 -04001301 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001302 goto err;
1303 }
1304
1305 /* Check for SCSV. */
1306 if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff)) {
1307 /* SCSV is fatal if renegotiating. */
David Benjamin20f6e972015-05-15 21:51:49 -04001308 if (s->s3->initial_handshake_complete) {
David Benjamin3570d732015-06-29 00:28:17 -04001309 OPENSSL_PUT_ERROR(SSL, SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
Adam Langleyfcf25832014-12-18 17:42:32 -08001310 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1311 goto err;
1312 }
1313 s->s3->send_connection_binding = 1;
1314 continue;
1315 }
1316
1317 /* Check for FALLBACK_SCSV. */
1318 if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff)) {
1319 uint16_t max_version = ssl3_get_max_server_version(s);
1320 if (SSL_IS_DTLS(s) ? (uint16_t)s->version > max_version
1321 : (uint16_t)s->version < max_version) {
David Benjamin3570d732015-06-29 00:28:17 -04001322 OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK);
Adam Langleyfcf25832014-12-18 17:42:32 -08001323 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
1324 goto err;
1325 }
1326 continue;
1327 }
1328
David Benjamina1c90a52015-05-30 17:03:14 -04001329 c = SSL_get_cipher_by_value(cipher_suite);
Adam Langleyfcf25832014-12-18 17:42:32 -08001330 if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
David Benjamin3570d732015-06-29 00:28:17 -04001331 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001332 goto err;
1333 }
1334 }
1335
1336 return sk;
David Benjamin9f2c0d72014-10-21 22:00:19 -04001337
Adam Langley95c29f32014-06-20 12:00:00 -07001338err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001339 sk_SSL_CIPHER_free(sk);
Adam Langleyfcf25832014-12-18 17:42:32 -08001340 return NULL;
1341}
Adam Langley95c29f32014-06-20 12:00:00 -07001342
1343
Adam Langleyfcf25832014-12-18 17:42:32 -08001344/* return a servername extension value if provided in Client Hello, or NULL. So
1345 * far, only host_name types are defined (RFC 3546). */
1346const char *SSL_get_servername(const SSL *s, const int type) {
1347 if (type != TLSEXT_NAMETYPE_host_name) {
1348 return NULL;
1349 }
Adam Langley95c29f32014-06-20 12:00:00 -07001350
Adam Langleyfcf25832014-12-18 17:42:32 -08001351 return s->session && !s->tlsext_hostname ? s->session->tlsext_hostname
1352 : s->tlsext_hostname;
1353}
Adam Langley95c29f32014-06-20 12:00:00 -07001354
Adam Langleyfcf25832014-12-18 17:42:32 -08001355int SSL_get_servername_type(const SSL *s) {
1356 if (s->session &&
1357 (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname)) {
1358 return TLSEXT_NAMETYPE_host_name;
1359 }
Adam Langley95c29f32014-06-20 12:00:00 -07001360
Adam Langleyfcf25832014-12-18 17:42:32 -08001361 return -1;
1362}
Adam Langley95c29f32014-06-20 12:00:00 -07001363
Adam Langleyfcf25832014-12-18 17:42:32 -08001364void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
1365 ctx->signed_cert_timestamps_enabled = 1;
1366}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001367
Adam Langleyfcf25832014-12-18 17:42:32 -08001368int SSL_enable_signed_cert_timestamps(SSL *ssl) {
1369 ssl->signed_cert_timestamps_enabled = 1;
1370 return 1;
1371}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001372
Adam Langleyfcf25832014-12-18 17:42:32 -08001373void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
1374 ctx->ocsp_stapling_enabled = 1;
1375}
David Benjamin6c7aed02014-08-27 16:42:38 -04001376
Adam Langleyfcf25832014-12-18 17:42:32 -08001377int SSL_enable_ocsp_stapling(SSL *ssl) {
1378 ssl->ocsp_stapling_enabled = 1;
1379 return 1;
1380}
David Benjamin6c7aed02014-08-27 16:42:38 -04001381
Adam Langleyfcf25832014-12-18 17:42:32 -08001382void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
1383 size_t *out_len) {
1384 SSL_SESSION *session = ssl->session;
Adam Langley3cb50e02014-08-26 14:00:31 -07001385
Adam Langleyfcf25832014-12-18 17:42:32 -08001386 *out_len = 0;
1387 *out = NULL;
1388 if (ssl->server || !session || !session->tlsext_signed_cert_timestamp_list) {
1389 return;
1390 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001391
Adam Langleyfcf25832014-12-18 17:42:32 -08001392 *out = session->tlsext_signed_cert_timestamp_list;
1393 *out_len = session->tlsext_signed_cert_timestamp_list_length;
1394}
David Benjamin6c7aed02014-08-27 16:42:38 -04001395
Adam Langleyfcf25832014-12-18 17:42:32 -08001396void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
1397 size_t *out_len) {
1398 SSL_SESSION *session = ssl->session;
1399
1400 *out_len = 0;
1401 *out = NULL;
1402 if (ssl->server || !session || !session->ocsp_response) {
1403 return;
1404 }
1405 *out = session->ocsp_response;
1406 *out_len = session->ocsp_response_length;
1407}
David Benjamin6c7aed02014-08-27 16:42:38 -04001408
Adam Langley95c29f32014-06-20 12:00:00 -07001409/* SSL_select_next_proto implements the standard protocol selection. It is
1410 * expected that this function is called from the callback set by
1411 * SSL_CTX_set_next_proto_select_cb.
1412 *
1413 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1414 * strings. The length byte itself is not included in the length. A byte
1415 * string of length 0 is invalid. No byte string may be truncated.
1416 *
1417 * The current, but experimental algorithm for selecting the protocol is:
1418 *
1419 * 1) If the server doesn't support NPN then this is indicated to the
1420 * callback. In this case, the client application has to abort the connection
1421 * or have a default application level protocol.
1422 *
1423 * 2) If the server supports NPN, but advertises an empty list then the
1424 * client selects the first protcol in its list, but indicates via the
1425 * API that this fallback case was enacted.
1426 *
1427 * 3) Otherwise, the client finds the first protocol in the server's list
1428 * that it supports and selects this protocol. This is because it's
1429 * assumed that the server has better information about which protocol
1430 * a client should use.
1431 *
1432 * 4) If the client doesn't support any of the server's advertised
1433 * protocols, then this is treated the same as case 2.
1434 *
1435 * It returns either
1436 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1437 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1438 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001439int SSL_select_next_proto(uint8_t **out, uint8_t *outlen, const uint8_t *server,
1440 unsigned int server_len, const uint8_t *client,
1441 unsigned int client_len) {
1442 unsigned int i, j;
1443 const uint8_t *result;
1444 int status = OPENSSL_NPN_UNSUPPORTED;
Adam Langley95c29f32014-06-20 12:00:00 -07001445
Adam Langleyfcf25832014-12-18 17:42:32 -08001446 /* For each protocol in server preference order, see if we support it. */
1447 for (i = 0; i < server_len;) {
1448 for (j = 0; j < client_len;) {
1449 if (server[i] == client[j] &&
1450 memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
1451 /* We found a match */
1452 result = &server[i];
1453 status = OPENSSL_NPN_NEGOTIATED;
1454 goto found;
1455 }
1456 j += client[j];
1457 j++;
1458 }
1459 i += server[i];
1460 i++;
1461 }
Adam Langley95c29f32014-06-20 12:00:00 -07001462
Adam Langleyfcf25832014-12-18 17:42:32 -08001463 /* There's no overlap between our protocols and the server's list. */
1464 result = client;
1465 status = OPENSSL_NPN_NO_OVERLAP;
Adam Langley95c29f32014-06-20 12:00:00 -07001466
Adam Langleyfcf25832014-12-18 17:42:32 -08001467found:
1468 *out = (uint8_t *)result + 1;
1469 *outlen = result[0];
1470 return status;
1471}
Adam Langley95c29f32014-06-20 12:00:00 -07001472
Adam Langley95c29f32014-06-20 12:00:00 -07001473/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
1474 * requested protocol for this connection and returns 0. If the client didn't
1475 * request any protocol, then *data is set to NULL.
1476 *
1477 * Note that the client can request any protocol it chooses. The value returned
1478 * from this function need not be a member of the list of supported protocols
Adam Langleyfcf25832014-12-18 17:42:32 -08001479 * provided by the callback. */
1480void SSL_get0_next_proto_negotiated(const SSL *s, const uint8_t **data,
1481 unsigned *len) {
1482 *data = s->next_proto_negotiated;
1483 if (!*data) {
1484 *len = 0;
1485 } else {
1486 *len = s->next_proto_negotiated_len;
1487 }
Adam Langley95c29f32014-06-20 12:00:00 -07001488}
1489
1490/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
1491 * TLS server needs a list of supported protocols for Next Protocol
1492 * Negotiation. The returned list must be in wire format. The list is returned
1493 * by setting |out| to point to it and |outlen| to its length. This memory will
1494 * not be modified, but one should assume that the SSL* keeps a reference to
1495 * it.
1496 *
Adam Langleyfcf25832014-12-18 17:42:32 -08001497 * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise.
1498 * Otherwise, no such extension will be included in the ServerHello. */
1499void SSL_CTX_set_next_protos_advertised_cb(
1500 SSL_CTX *ctx,
1501 int (*cb)(SSL *ssl, const uint8_t **out, unsigned int *outlen, void *arg),
1502 void *arg) {
1503 ctx->next_protos_advertised_cb = cb;
1504 ctx->next_protos_advertised_cb_arg = arg;
1505}
Adam Langley95c29f32014-06-20 12:00:00 -07001506
1507/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1508 * client needs to select a protocol from the server's provided list. |out|
1509 * must be set to point to the selected protocol (which may be within |in|).
1510 * The length of the protocol name must be written into |outlen|. The server's
1511 * advertised protocols are provided in |in| and |inlen|. The callback can
1512 * assume that |in| is syntactically valid.
1513 *
1514 * The client must select a protocol. It is fatal to the connection if this
1515 * callback returns a value other than SSL_TLSEXT_ERR_OK.
1516 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001517void SSL_CTX_set_next_proto_select_cb(
1518 SSL_CTX *ctx, int (*cb)(SSL *s, uint8_t **out, uint8_t *outlen,
1519 const uint8_t *in, unsigned int inlen, void *arg),
1520 void *arg) {
1521 ctx->next_proto_select_cb = cb;
1522 ctx->next_proto_select_cb_arg = arg;
1523}
Adam Langley95c29f32014-06-20 12:00:00 -07001524
Adam Langleyfcf25832014-12-18 17:42:32 -08001525int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
1526 unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001527 OPENSSL_free(ctx->alpn_client_proto_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001528 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1529 if (!ctx->alpn_client_proto_list) {
1530 return 1;
1531 }
1532 ctx->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001533
Adam Langleyfcf25832014-12-18 17:42:32 -08001534 return 0;
1535}
Adam Langley95c29f32014-06-20 12:00:00 -07001536
Adam Langleyfcf25832014-12-18 17:42:32 -08001537int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001538 OPENSSL_free(ssl->alpn_client_proto_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001539 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1540 if (!ssl->alpn_client_proto_list) {
1541 return 1;
1542 }
1543 ssl->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001544
Adam Langleyfcf25832014-12-18 17:42:32 -08001545 return 0;
1546}
Adam Langley95c29f32014-06-20 12:00:00 -07001547
1548/* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
1549 * during ClientHello processing in order to select an ALPN protocol from the
1550 * client's list of offered protocols. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001551void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
1552 int (*cb)(SSL *ssl, const uint8_t **out,
1553 uint8_t *outlen, const uint8_t *in,
1554 unsigned int inlen, void *arg),
1555 void *arg) {
1556 ctx->alpn_select_cb = cb;
1557 ctx->alpn_select_cb_arg = arg;
1558}
Adam Langley95c29f32014-06-20 12:00:00 -07001559
1560/* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
1561 * On return it sets |*data| to point to |*len| bytes of protocol name (not
1562 * including the leading length-prefix byte). If the server didn't respond with
1563 * a negotiated protocol then |*len| will be zero. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001564void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **data,
1565 unsigned *len) {
1566 *data = NULL;
1567 if (ssl->s3) {
1568 *data = ssl->s3->alpn_selected;
1569 }
1570 if (*data == NULL) {
1571 *len = 0;
1572 } else {
1573 *len = ssl->s3->alpn_selected_len;
1574 }
1575}
Adam Langley95c29f32014-06-20 12:00:00 -07001576
David Benjamincfd248b2015-04-03 11:02:24 -04001577int SSL_export_keying_material(SSL *s, uint8_t *out, size_t out_len,
1578 const char *label, size_t label_len,
1579 const uint8_t *context, size_t context_len,
1580 int use_context) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001581 if (s->version < TLS1_VERSION) {
David Benjaminc565ebb2015-04-03 04:06:36 -04001582 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001583 }
Adam Langley95c29f32014-06-20 12:00:00 -07001584
David Benjamincfd248b2015-04-03 11:02:24 -04001585 return s->enc_method->export_keying_material(
1586 s, out, out_len, label, label_len, context, context_len, use_context);
Adam Langleyfcf25832014-12-18 17:42:32 -08001587}
Adam Langley95c29f32014-06-20 12:00:00 -07001588
Adam Langleyfcf25832014-12-18 17:42:32 -08001589static uint32_t ssl_session_hash(const SSL_SESSION *a) {
1590 uint32_t hash =
1591 ((uint32_t)a->session_id[0]) ||
1592 ((uint32_t)a->session_id[1] << 8) ||
1593 ((uint32_t)a->session_id[2] << 16) ||
1594 ((uint32_t)a->session_id[3] << 24);
Adam Langley95c29f32014-06-20 12:00:00 -07001595
Adam Langleyfcf25832014-12-18 17:42:32 -08001596 return hash;
1597}
Adam Langley95c29f32014-06-20 12:00:00 -07001598
1599/* NB: If this function (or indeed the hash function which uses a sort of
1600 * coarser function than this one) is changed, ensure
1601 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1602 * able to construct an SSL_SESSION that will collide with any existing session
1603 * with a matching session ID. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001604static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
1605 if (a->ssl_version != b->ssl_version) {
1606 return 1;
1607 }
Adam Langley95c29f32014-06-20 12:00:00 -07001608
Adam Langleyfcf25832014-12-18 17:42:32 -08001609 if (a->session_id_length != b->session_id_length) {
1610 return 1;
1611 }
Adam Langley95c29f32014-06-20 12:00:00 -07001612
Adam Langleyfcf25832014-12-18 17:42:32 -08001613 return memcmp(a->session_id, b->session_id, a->session_id_length);
1614}
Adam Langley95c29f32014-06-20 12:00:00 -07001615
David Benjamin4831c332015-05-16 11:43:13 -04001616SSL_CTX *SSL_CTX_new(const SSL_METHOD *method) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001617 SSL_CTX *ret = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001618
David Benjamin4831c332015-05-16 11:43:13 -04001619 if (method == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001620 OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_METHOD_PASSED);
Adam Langleyfcf25832014-12-18 17:42:32 -08001621 return NULL;
1622 }
Adam Langley95c29f32014-06-20 12:00:00 -07001623
Adam Langleyfcf25832014-12-18 17:42:32 -08001624 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001625 OPENSSL_PUT_ERROR(SSL, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
Adam Langleyfcf25832014-12-18 17:42:32 -08001626 goto err;
1627 }
Adam Langley95c29f32014-06-20 12:00:00 -07001628
Adam Langleyfcf25832014-12-18 17:42:32 -08001629 ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1630 if (ret == NULL) {
1631 goto err;
1632 }
Adam Langley95c29f32014-06-20 12:00:00 -07001633
Adam Langleyfcf25832014-12-18 17:42:32 -08001634 memset(ret, 0, sizeof(SSL_CTX));
Adam Langley95c29f32014-06-20 12:00:00 -07001635
David Benjamin4831c332015-05-16 11:43:13 -04001636 ret->method = method->method;
Adam Langley95c29f32014-06-20 12:00:00 -07001637
Adam Langley4bdb6e42015-05-15 15:29:21 -07001638 CRYPTO_MUTEX_init(&ret->lock);
1639
Adam Langleyfcf25832014-12-18 17:42:32 -08001640 ret->cert_store = NULL;
1641 ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
1642 ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1643 ret->session_cache_head = NULL;
1644 ret->session_cache_tail = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001645
Adam Langleyfcf25832014-12-18 17:42:32 -08001646 /* We take the system default */
1647 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001648
Adam Langleyfcf25832014-12-18 17:42:32 -08001649 ret->new_session_cb = 0;
1650 ret->remove_session_cb = 0;
1651 ret->get_session_cb = 0;
1652 ret->generate_session_id = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001653
Adam Langleyfcf25832014-12-18 17:42:32 -08001654 ret->references = 1;
1655 ret->quiet_shutdown = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001656
Adam Langleyfcf25832014-12-18 17:42:32 -08001657 ret->info_callback = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001658
Adam Langleyfcf25832014-12-18 17:42:32 -08001659 ret->app_verify_callback = 0;
1660 ret->app_verify_arg = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001661
Adam Langleyfcf25832014-12-18 17:42:32 -08001662 ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
Adam Langleyfcf25832014-12-18 17:42:32 -08001663 ret->msg_callback = 0;
1664 ret->msg_callback_arg = NULL;
1665 ret->verify_mode = SSL_VERIFY_NONE;
1666 ret->sid_ctx_length = 0;
1667 ret->default_verify_callback = NULL;
1668 ret->cert = ssl_cert_new();
1669 if (ret->cert == NULL) {
1670 goto err;
1671 }
Adam Langley95c29f32014-06-20 12:00:00 -07001672
Adam Langleyfcf25832014-12-18 17:42:32 -08001673 ret->default_passwd_callback = 0;
1674 ret->default_passwd_callback_userdata = NULL;
1675 ret->client_cert_cb = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001676
Adam Langleyfcf25832014-12-18 17:42:32 -08001677 ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
1678 if (ret->sessions == NULL) {
1679 goto err;
1680 }
1681 ret->cert_store = X509_STORE_new();
1682 if (ret->cert_store == NULL) {
1683 goto err;
1684 }
Adam Langley95c29f32014-06-20 12:00:00 -07001685
Adam Langleyfcf25832014-12-18 17:42:32 -08001686 ssl_create_cipher_list(ret->method, &ret->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001687 &ret->cipher_list_by_id, SSL_DEFAULT_CIPHER_LIST);
Adam Langleyfcf25832014-12-18 17:42:32 -08001688 if (ret->cipher_list == NULL ||
1689 sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001690 OPENSSL_PUT_ERROR(SSL, SSL_R_LIBRARY_HAS_NO_CIPHERS);
Adam Langleyfcf25832014-12-18 17:42:32 -08001691 goto err2;
1692 }
Adam Langley95c29f32014-06-20 12:00:00 -07001693
Adam Langleyfcf25832014-12-18 17:42:32 -08001694 ret->param = X509_VERIFY_PARAM_new();
1695 if (!ret->param) {
1696 goto err;
1697 }
Adam Langley95c29f32014-06-20 12:00:00 -07001698
Adam Langleyfcf25832014-12-18 17:42:32 -08001699 ret->client_CA = sk_X509_NAME_new_null();
1700 if (ret->client_CA == NULL) {
1701 goto err;
1702 }
Adam Langley95c29f32014-06-20 12:00:00 -07001703
David Benjamin9f33fc62015-04-15 17:29:53 -04001704 CRYPTO_new_ex_data(&g_ex_data_class_ssl_ctx, ret, &ret->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001705
Adam Langleyfcf25832014-12-18 17:42:32 -08001706 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
David Benjamin422d3a42014-08-20 11:09:03 -04001707
Adam Langleyfcf25832014-12-18 17:42:32 -08001708 ret->tlsext_servername_callback = 0;
1709 ret->tlsext_servername_arg = NULL;
1710 /* Setup RFC4507 ticket keys */
1711 if (!RAND_bytes(ret->tlsext_tick_key_name, 16) ||
1712 !RAND_bytes(ret->tlsext_tick_hmac_key, 16) ||
1713 !RAND_bytes(ret->tlsext_tick_aes_key, 16)) {
1714 ret->options |= SSL_OP_NO_TICKET;
1715 }
Adam Langley95c29f32014-06-20 12:00:00 -07001716
Adam Langleyfcf25832014-12-18 17:42:32 -08001717 ret->next_protos_advertised_cb = 0;
1718 ret->next_proto_select_cb = 0;
1719 ret->psk_identity_hint = NULL;
1720 ret->psk_client_callback = NULL;
1721 ret->psk_server_callback = NULL;
1722
1723 /* Default is to connect to non-RI servers. When RI is more widely deployed
1724 * might change this. */
1725 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
1726
1727 /* Lock the SSL_CTX to the specified version, for compatibility with legacy
1728 * uses of SSL_METHOD. */
David Benjamin4831c332015-05-16 11:43:13 -04001729 if (method->version != 0) {
1730 SSL_CTX_set_max_version(ret, method->version);
1731 SSL_CTX_set_min_version(ret, method->version);
Adam Langleyfcf25832014-12-18 17:42:32 -08001732 }
1733
1734 return ret;
1735
Adam Langley95c29f32014-06-20 12:00:00 -07001736err:
David Benjamin3570d732015-06-29 00:28:17 -04001737 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -07001738err2:
David Benjamin2755a3e2015-04-22 16:17:58 -04001739 SSL_CTX_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -08001740 return NULL;
1741}
Adam Langley95c29f32014-06-20 12:00:00 -07001742
David Benjamin4fcc2e22015-04-22 12:58:16 -04001743void SSL_CTX_free(SSL_CTX *ctx) {
1744 if (ctx == NULL ||
Adam Langley0b5e3902015-05-15 13:08:38 -07001745 !CRYPTO_refcount_dec_and_test_zero(&ctx->references)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001746 return;
1747 }
Adam Langley95c29f32014-06-20 12:00:00 -07001748
David Benjamin2755a3e2015-04-22 16:17:58 -04001749 X509_VERIFY_PARAM_free(ctx->param);
Adam Langley95c29f32014-06-20 12:00:00 -07001750
Adam Langleyfcf25832014-12-18 17:42:32 -08001751 /* Free internal session cache. However: the remove_cb() may reference the
1752 * ex_data of SSL_CTX, thus the ex_data store can only be removed after the
1753 * sessions were flushed. As the ex_data handling routines might also touch
1754 * the session cache, the most secure solution seems to be: empty (flush) the
1755 * cache, then free ex_data, then finally free the cache. (See ticket
1756 * [openssl.org #212].) */
David Benjamin2755a3e2015-04-22 16:17:58 -04001757 SSL_CTX_flush_sessions(ctx, 0);
Adam Langley95c29f32014-06-20 12:00:00 -07001758
David Benjamin4fcc2e22015-04-22 12:58:16 -04001759 CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, ctx, &ctx->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001760
Adam Langley4bdb6e42015-05-15 15:29:21 -07001761 CRYPTO_MUTEX_cleanup(&ctx->lock);
David Benjamin2755a3e2015-04-22 16:17:58 -04001762 lh_SSL_SESSION_free(ctx->sessions);
1763 X509_STORE_free(ctx->cert_store);
1764 ssl_cipher_preference_list_free(ctx->cipher_list);
1765 sk_SSL_CIPHER_free(ctx->cipher_list_by_id);
1766 ssl_cipher_preference_list_free(ctx->cipher_list_tls11);
1767 ssl_cert_free(ctx->cert);
1768 sk_X509_NAME_pop_free(ctx->client_CA, X509_NAME_free);
David Benjamin2755a3e2015-04-22 16:17:58 -04001769 sk_SRTP_PROTECTION_PROFILE_free(ctx->srtp_profiles);
1770 OPENSSL_free(ctx->psk_identity_hint);
1771 OPENSSL_free(ctx->tlsext_ecpointformatlist);
1772 OPENSSL_free(ctx->tlsext_ellipticcurvelist);
1773 OPENSSL_free(ctx->alpn_client_proto_list);
1774 EVP_PKEY_free(ctx->tlsext_channel_id_private);
1775 BIO_free(ctx->keylog_bio);
Adam Langley95c29f32014-06-20 12:00:00 -07001776
David Benjamin4fcc2e22015-04-22 12:58:16 -04001777 OPENSSL_free(ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08001778}
Adam Langley95c29f32014-06-20 12:00:00 -07001779
Adam Langleyfcf25832014-12-18 17:42:32 -08001780void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) {
1781 ctx->default_passwd_callback = cb;
1782}
Adam Langley95c29f32014-06-20 12:00:00 -07001783
Adam Langleyfcf25832014-12-18 17:42:32 -08001784void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u) {
1785 ctx->default_passwd_callback_userdata = u;
1786}
Adam Langley95c29f32014-06-20 12:00:00 -07001787
Adam Langleyfcf25832014-12-18 17:42:32 -08001788void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
1789 int (*cb)(X509_STORE_CTX *, void *),
1790 void *arg) {
1791 ctx->app_verify_callback = cb;
1792 ctx->app_verify_arg = arg;
1793}
Adam Langley95c29f32014-06-20 12:00:00 -07001794
Adam Langleyfcf25832014-12-18 17:42:32 -08001795void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
1796 int (*cb)(int, X509_STORE_CTX *)) {
1797 ctx->verify_mode = mode;
1798 ctx->default_verify_callback = cb;
1799}
Adam Langley95c29f32014-06-20 12:00:00 -07001800
Adam Langleyfcf25832014-12-18 17:42:32 -08001801void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
1802 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
1803}
Adam Langley1258b6a2014-06-20 12:00:00 -07001804
David Benjamin7481d392015-07-05 19:38:46 -04001805void SSL_CTX_set_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl, void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08001806 void *arg) {
David Benjamin7481d392015-07-05 19:38:46 -04001807 ssl_cert_set_cert_cb(ctx->cert, cb, arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08001808}
David Benjamin859ec3c2014-09-02 16:29:36 -04001809
David Benjamin7481d392015-07-05 19:38:46 -04001810void SSL_set_cert_cb(SSL *ssl, int (*cb)(SSL *ssl, void *arg), void *arg) {
1811 ssl_cert_set_cert_cb(ssl->cert, cb, arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08001812}
Adam Langley95c29f32014-06-20 12:00:00 -07001813
David Benjamin107db582015-04-08 00:41:59 -04001814void ssl_get_compatible_server_ciphers(SSL *s, uint32_t *out_mask_k,
1815 uint32_t *out_mask_a) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001816 CERT *c = s->cert;
David Benjamind1d80782015-07-05 11:54:09 -04001817 int have_rsa_cert = 0, dh_tmp;
David Benjamin107db582015-04-08 00:41:59 -04001818 uint32_t mask_k, mask_a;
David Benjamind1d80782015-07-05 11:54:09 -04001819 int have_ecc_cert = 0, ecdsa_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001820 X509 *x;
Adam Langley95c29f32014-06-20 12:00:00 -07001821
Adam Langleyfcf25832014-12-18 17:42:32 -08001822 if (c == NULL) {
1823 /* TODO(davidben): Is this codepath possible? */
1824 *out_mask_k = 0;
1825 *out_mask_a = 0;
1826 return;
1827 }
Adam Langley95c29f32014-06-20 12:00:00 -07001828
Adam Langleyfcf25832014-12-18 17:42:32 -08001829 dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001830
David Benjamind1d80782015-07-05 11:54:09 -04001831 if (s->cert->x509 != NULL && s->cert->privatekey != NULL) {
1832 if (s->cert->privatekey->type == EVP_PKEY_RSA) {
1833 have_rsa_cert = 1;
1834 } else if (s->cert->privatekey->type == EVP_PKEY_EC) {
1835 have_ecc_cert = 1;
1836 }
1837 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001838 mask_k = 0;
1839 mask_a = 0;
David Benjaminf31e6812014-11-13 18:05:55 -05001840
Adam Langleyfcf25832014-12-18 17:42:32 -08001841 if (dh_tmp) {
David Benjamin7061e282015-03-19 11:10:48 -04001842 mask_k |= SSL_kDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001843 }
David Benjaminbb20f522015-07-04 17:18:14 -04001844 if (have_rsa_cert) {
1845 mask_k |= SSL_kRSA;
Adam Langleyfcf25832014-12-18 17:42:32 -08001846 mask_a |= SSL_aRSA;
1847 }
Adam Langley95c29f32014-06-20 12:00:00 -07001848
Adam Langleyfcf25832014-12-18 17:42:32 -08001849 /* An ECC certificate may be usable for ECDSA cipher suites depending on the
1850 * key usage extension and on the client's curve preferences. */
1851 if (have_ecc_cert) {
David Benjamind1d80782015-07-05 11:54:09 -04001852 x = c->x509;
Adam Langleyfcf25832014-12-18 17:42:32 -08001853 /* This call populates extension flags (ex_flags). */
1854 X509_check_purpose(x, -1, 0);
1855 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE)
1856 ? (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)
1857 : 1;
1858 if (!tls1_check_ec_cert(s, x)) {
1859 ecdsa_ok = 0;
1860 }
1861 if (ecdsa_ok) {
1862 mask_a |= SSL_aECDSA;
1863 }
1864 }
Adam Langley95c29f32014-06-20 12:00:00 -07001865
Adam Langleyfcf25832014-12-18 17:42:32 -08001866 /* If we are considering an ECC cipher suite that uses an ephemeral EC
1867 * key, check it. */
David Benjamindd978782015-04-24 15:20:13 -04001868 if (tls1_check_ec_tmp_key(s)) {
David Benjamin7061e282015-03-19 11:10:48 -04001869 mask_k |= SSL_kECDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001870 }
Adam Langley95c29f32014-06-20 12:00:00 -07001871
Adam Langleyfcf25832014-12-18 17:42:32 -08001872 /* PSK requires a server callback. */
1873 if (s->psk_server_callback != NULL) {
1874 mask_k |= SSL_kPSK;
1875 mask_a |= SSL_aPSK;
1876 }
Adam Langley95c29f32014-06-20 12:00:00 -07001877
Adam Langleyfcf25832014-12-18 17:42:32 -08001878 *out_mask_k = mask_k;
1879 *out_mask_a = mask_a;
1880}
Adam Langley95c29f32014-06-20 12:00:00 -07001881
Adam Langleyfcf25832014-12-18 17:42:32 -08001882void ssl_update_cache(SSL *s, int mode) {
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001883 /* Never cache sessions with empty session IDs. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001884 if (s->session->session_id_length == 0) {
1885 return;
1886 }
Adam Langley95c29f32014-06-20 12:00:00 -07001887
David Benjamin95d31822015-06-15 19:53:32 -04001888 int has_new_session = !s->hit;
1889 if (!s->server && s->tlsext_ticket_expected) {
1890 /* A client may see new sessions on abbreviated handshakes if the server
1891 * decides to renew the ticket. Once the handshake is completed, it should
1892 * be inserted into the cache. */
1893 has_new_session = 1;
1894 }
1895
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001896 SSL_CTX *ctx = s->initial_ctx;
David Benjamin95d31822015-06-15 19:53:32 -04001897 if ((ctx->session_cache_mode & mode) == mode && has_new_session &&
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001898 ((ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) ||
1899 SSL_CTX_add_session(ctx, s->session)) &&
1900 ctx->new_session_cb != NULL) {
1901 /* Note: |new_session_cb| is called whether the internal session cache is
1902 * used or not. */
1903 if (!ctx->new_session_cb(s, SSL_SESSION_up_ref(s->session))) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001904 SSL_SESSION_free(s->session);
1905 }
1906 }
Adam Langley95c29f32014-06-20 12:00:00 -07001907
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001908 if (!(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR) &&
1909 !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) &&
1910 (ctx->session_cache_mode & mode) == mode) {
1911 /* Automatically flush the internal session cache every 255 connections. */
1912 int flush_cache = 0;
Adam Langley4bdb6e42015-05-15 15:29:21 -07001913 CRYPTO_MUTEX_lock_write(&ctx->lock);
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001914 ctx->handshakes_since_cache_flush++;
1915 if (ctx->handshakes_since_cache_flush >= 255) {
1916 flush_cache = 1;
1917 ctx->handshakes_since_cache_flush = 0;
1918 }
Adam Langley4bdb6e42015-05-15 15:29:21 -07001919 CRYPTO_MUTEX_unlock(&ctx->lock);
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001920
1921 if (flush_cache) {
1922 SSL_CTX_flush_sessions(ctx, (unsigned long)time(NULL));
Adam Langleyfcf25832014-12-18 17:42:32 -08001923 }
1924 }
1925}
Adam Langley95c29f32014-06-20 12:00:00 -07001926
David Benjamin1a5c50f2015-03-11 16:22:37 -04001927int SSL_get_error(const SSL *s, int ret_code) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001928 int reason;
David Benjamin1a5c50f2015-03-11 16:22:37 -04001929 uint32_t err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001930 BIO *bio;
Adam Langley95c29f32014-06-20 12:00:00 -07001931
David Benjamin1a5c50f2015-03-11 16:22:37 -04001932 if (ret_code > 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001933 return SSL_ERROR_NONE;
1934 }
Adam Langley95c29f32014-06-20 12:00:00 -07001935
Adam Langleyfcf25832014-12-18 17:42:32 -08001936 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
1937 * where we do encode the error */
David Benjamin1a5c50f2015-03-11 16:22:37 -04001938 err = ERR_peek_error();
1939 if (err != 0) {
1940 if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001941 return SSL_ERROR_SYSCALL;
1942 }
1943 return SSL_ERROR_SSL;
1944 }
Adam Langley95c29f32014-06-20 12:00:00 -07001945
David Benjamin1a5c50f2015-03-11 16:22:37 -04001946 if (ret_code == 0) {
David Benjamin9a38e922015-01-22 16:06:11 -05001947 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
1948 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) {
1949 /* The socket was cleanly shut down with a close_notify. */
1950 return SSL_ERROR_ZERO_RETURN;
1951 }
1952 /* An EOF was observed which violates the protocol, and the underlying
1953 * transport does not participate in the error queue. Bubble up to the
1954 * caller. */
1955 return SSL_ERROR_SYSCALL;
Adam Langleyfcf25832014-12-18 17:42:32 -08001956 }
Adam Langleyb2ce0582014-06-20 12:00:00 -07001957
Adam Langleyfcf25832014-12-18 17:42:32 -08001958 if (SSL_want_session(s)) {
1959 return SSL_ERROR_PENDING_SESSION;
1960 }
Adam Langley95c29f32014-06-20 12:00:00 -07001961
Adam Langleyfcf25832014-12-18 17:42:32 -08001962 if (SSL_want_certificate(s)) {
1963 return SSL_ERROR_PENDING_CERTIFICATE;
1964 }
Adam Langley95c29f32014-06-20 12:00:00 -07001965
Adam Langleyfcf25832014-12-18 17:42:32 -08001966 if (SSL_want_read(s)) {
1967 bio = SSL_get_rbio(s);
1968 if (BIO_should_read(bio)) {
1969 return SSL_ERROR_WANT_READ;
1970 }
Adam Langley95c29f32014-06-20 12:00:00 -07001971
Adam Langleyfcf25832014-12-18 17:42:32 -08001972 if (BIO_should_write(bio)) {
1973 /* This one doesn't make too much sense ... We never try to write to the
1974 * rbio, and an application program where rbio and wbio are separate
1975 * couldn't even know what it should wait for. However if we ever set
1976 * s->rwstate incorrectly (so that we have SSL_want_read(s) instead of
1977 * SSL_want_write(s)) and rbio and wbio *are* the same, this test works
1978 * around that bug; so it might be safer to keep it. */
1979 return SSL_ERROR_WANT_WRITE;
1980 }
Adam Langley95c29f32014-06-20 12:00:00 -07001981
Adam Langleyfcf25832014-12-18 17:42:32 -08001982 if (BIO_should_io_special(bio)) {
1983 reason = BIO_get_retry_reason(bio);
1984 if (reason == BIO_RR_CONNECT) {
1985 return SSL_ERROR_WANT_CONNECT;
1986 }
Adam Langley95c29f32014-06-20 12:00:00 -07001987
Adam Langleyfcf25832014-12-18 17:42:32 -08001988 if (reason == BIO_RR_ACCEPT) {
1989 return SSL_ERROR_WANT_ACCEPT;
1990 }
Adam Langley95c29f32014-06-20 12:00:00 -07001991
Adam Langleyfcf25832014-12-18 17:42:32 -08001992 return SSL_ERROR_SYSCALL; /* unknown */
1993 }
1994 }
Adam Langley95c29f32014-06-20 12:00:00 -07001995
Adam Langleyfcf25832014-12-18 17:42:32 -08001996 if (SSL_want_write(s)) {
1997 bio = SSL_get_wbio(s);
1998 if (BIO_should_write(bio)) {
1999 return SSL_ERROR_WANT_WRITE;
2000 }
Adam Langley95c29f32014-06-20 12:00:00 -07002001
Adam Langleyfcf25832014-12-18 17:42:32 -08002002 if (BIO_should_read(bio)) {
2003 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2004 return SSL_ERROR_WANT_READ;
2005 }
Adam Langley95c29f32014-06-20 12:00:00 -07002006
Adam Langleyfcf25832014-12-18 17:42:32 -08002007 if (BIO_should_io_special(bio)) {
2008 reason = BIO_get_retry_reason(bio);
2009 if (reason == BIO_RR_CONNECT) {
2010 return SSL_ERROR_WANT_CONNECT;
2011 }
Adam Langley95c29f32014-06-20 12:00:00 -07002012
Adam Langleyfcf25832014-12-18 17:42:32 -08002013 if (reason == BIO_RR_ACCEPT) {
2014 return SSL_ERROR_WANT_ACCEPT;
2015 }
Adam Langley95c29f32014-06-20 12:00:00 -07002016
Adam Langleyfcf25832014-12-18 17:42:32 -08002017 return SSL_ERROR_SYSCALL;
2018 }
2019 }
Adam Langley95c29f32014-06-20 12:00:00 -07002020
Adam Langleyfcf25832014-12-18 17:42:32 -08002021 if (SSL_want_x509_lookup(s)) {
2022 return SSL_ERROR_WANT_X509_LOOKUP;
2023 }
Adam Langley95c29f32014-06-20 12:00:00 -07002024
Adam Langleyfcf25832014-12-18 17:42:32 -08002025 if (SSL_want_channel_id_lookup(s)) {
2026 return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
2027 }
Adam Langley0f4746e2014-08-13 12:26:32 -07002028
David Benjaminb4d65fd2015-05-29 17:11:21 -04002029 if (SSL_want_private_key_operation(s)) {
2030 return SSL_ERROR_WANT_PRIVATE_KEY_OPERATION;
2031 }
2032
Adam Langleyfcf25832014-12-18 17:42:32 -08002033 return SSL_ERROR_SYSCALL;
2034}
Adam Langley0f4746e2014-08-13 12:26:32 -07002035
Adam Langleyfcf25832014-12-18 17:42:32 -08002036int SSL_do_handshake(SSL *s) {
2037 int ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002038
Adam Langleyfcf25832014-12-18 17:42:32 -08002039 if (s->handshake_func == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04002040 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_TYPE_NOT_SET);
Adam Langleyfcf25832014-12-18 17:42:32 -08002041 return -1;
2042 }
Adam Langley95c29f32014-06-20 12:00:00 -07002043
Adam Langleyfcf25832014-12-18 17:42:32 -08002044 if (SSL_in_init(s)) {
2045 ret = s->handshake_func(s);
2046 }
2047 return ret;
2048}
Adam Langley95c29f32014-06-20 12:00:00 -07002049
David Benjamin4831c332015-05-16 11:43:13 -04002050void SSL_set_accept_state(SSL *ssl) {
2051 ssl->server = 1;
2052 ssl->shutdown = 0;
David Benjamin8ec88102015-05-15 23:40:13 -04002053 ssl->state = SSL_ST_ACCEPT;
David Benjamin4831c332015-05-16 11:43:13 -04002054 ssl->handshake_func = ssl->method->ssl_accept;
Adam Langleyfcf25832014-12-18 17:42:32 -08002055 /* clear the current cipher */
David Benjamin4831c332015-05-16 11:43:13 -04002056 ssl_clear_cipher_ctx(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08002057}
Adam Langley95c29f32014-06-20 12:00:00 -07002058
David Benjamin4831c332015-05-16 11:43:13 -04002059void SSL_set_connect_state(SSL *ssl) {
2060 ssl->server = 0;
2061 ssl->shutdown = 0;
David Benjamin8ec88102015-05-15 23:40:13 -04002062 ssl->state = SSL_ST_CONNECT;
David Benjamin4831c332015-05-16 11:43:13 -04002063 ssl->handshake_func = ssl->method->ssl_connect;
Adam Langleyfcf25832014-12-18 17:42:32 -08002064 /* clear the current cipher */
David Benjamin4831c332015-05-16 11:43:13 -04002065 ssl_clear_cipher_ctx(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08002066}
Adam Langley95c29f32014-06-20 12:00:00 -07002067
Adam Langleyfcf25832014-12-18 17:42:32 -08002068static const char *ssl_get_version(int version) {
2069 switch (version) {
2070 case TLS1_2_VERSION:
2071 return "TLSv1.2";
Adam Langley95c29f32014-06-20 12:00:00 -07002072
Adam Langleyfcf25832014-12-18 17:42:32 -08002073 case TLS1_1_VERSION:
2074 return "TLSv1.1";
Adam Langley95c29f32014-06-20 12:00:00 -07002075
Adam Langleyfcf25832014-12-18 17:42:32 -08002076 case TLS1_VERSION:
2077 return "TLSv1";
Adam Langley95c29f32014-06-20 12:00:00 -07002078
Adam Langleyfcf25832014-12-18 17:42:32 -08002079 case SSL3_VERSION:
2080 return "SSLv3";
Adam Langley95c29f32014-06-20 12:00:00 -07002081
David Benjamin1c722b72015-04-20 13:53:10 -04002082 case DTLS1_VERSION:
2083 return "DTLSv1";
2084
2085 case DTLS1_2_VERSION:
2086 return "DTLSv1.2";
2087
Adam Langleyfcf25832014-12-18 17:42:32 -08002088 default:
2089 return "unknown";
2090 }
2091}
Adam Langley95c29f32014-06-20 12:00:00 -07002092
Adam Langleyfcf25832014-12-18 17:42:32 -08002093const char *SSL_get_version(const SSL *s) {
2094 return ssl_get_version(s->version);
2095}
Adam Langley95c29f32014-06-20 12:00:00 -07002096
Adam Langleyfcf25832014-12-18 17:42:32 -08002097const char *SSL_SESSION_get_version(const SSL_SESSION *sess) {
2098 return ssl_get_version(sess->ssl_version);
2099}
Adam Langley95c29f32014-06-20 12:00:00 -07002100
Adam Langleyfcf25832014-12-18 17:42:32 -08002101void ssl_clear_cipher_ctx(SSL *s) {
David Benjamin31a07792015-03-03 14:20:26 -05002102 SSL_AEAD_CTX_free(s->aead_read_ctx);
2103 s->aead_read_ctx = NULL;
2104 SSL_AEAD_CTX_free(s->aead_write_ctx);
2105 s->aead_write_ctx = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002106}
Adam Langley95c29f32014-06-20 12:00:00 -07002107
Adam Langleyfcf25832014-12-18 17:42:32 -08002108X509 *SSL_get_certificate(const SSL *s) {
2109 if (s->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04002110 return s->cert->x509;
Adam Langleyfcf25832014-12-18 17:42:32 -08002111 }
2112
2113 return NULL;
2114}
2115
2116EVP_PKEY *SSL_get_privatekey(const SSL *s) {
2117 if (s->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04002118 return s->cert->privatekey;
Adam Langleyfcf25832014-12-18 17:42:32 -08002119 }
2120
2121 return NULL;
2122}
2123
2124X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
2125 if (ctx->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04002126 return ctx->cert->x509;
Adam Langleyfcf25832014-12-18 17:42:32 -08002127 }
2128
2129 return NULL;
2130}
2131
2132EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
2133 if (ctx->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04002134 return ctx->cert->privatekey;
Adam Langleyfcf25832014-12-18 17:42:32 -08002135 }
2136
2137 return NULL;
2138}
2139
2140const SSL_CIPHER *SSL_get_current_cipher(const SSL *s) {
David Benjamina07c0fc2015-05-13 13:19:42 -04002141 if (s->aead_write_ctx == NULL) {
2142 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002143 }
David Benjamina07c0fc2015-05-13 13:19:42 -04002144 return s->aead_write_ctx->cipher;
Adam Langleyfcf25832014-12-18 17:42:32 -08002145}
2146
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07002147const COMP_METHOD *SSL_get_current_compression(SSL *s) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002148
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07002149const COMP_METHOD *SSL_get_current_expansion(SSL *s) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002150
2151int ssl_init_wbio_buffer(SSL *s, int push) {
2152 BIO *bbio;
2153
2154 if (s->bbio == NULL) {
2155 bbio = BIO_new(BIO_f_buffer());
2156 if (bbio == NULL) {
2157 return 0;
2158 }
2159 s->bbio = bbio;
2160 } else {
2161 bbio = s->bbio;
2162 if (s->bbio == s->wbio) {
2163 s->wbio = BIO_pop(s->wbio);
2164 }
2165 }
2166
2167 BIO_reset(bbio);
2168 if (!BIO_set_read_buffer_size(bbio, 1)) {
David Benjamin3570d732015-06-29 00:28:17 -04002169 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -08002170 return 0;
2171 }
2172
2173 if (push) {
2174 if (s->wbio != bbio) {
2175 s->wbio = BIO_push(bbio, s->wbio);
2176 }
2177 } else {
2178 if (s->wbio == bbio) {
2179 s->wbio = BIO_pop(bbio);
2180 }
2181 }
2182
2183 return 1;
2184}
2185
2186void ssl_free_wbio_buffer(SSL *s) {
2187 if (s->bbio == NULL) {
2188 return;
2189 }
2190
2191 if (s->bbio == s->wbio) {
2192 /* remove buffering */
2193 s->wbio = BIO_pop(s->wbio);
2194 }
2195
2196 BIO_free(s->bbio);
2197 s->bbio = NULL;
2198}
2199
2200void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
2201 ctx->quiet_shutdown = mode;
2202}
2203
2204int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
2205 return ctx->quiet_shutdown;
2206}
2207
2208void SSL_set_quiet_shutdown(SSL *s, int mode) { s->quiet_shutdown = mode; }
2209
2210int SSL_get_quiet_shutdown(const SSL *s) { return s->quiet_shutdown; }
2211
2212void SSL_set_shutdown(SSL *s, int mode) { s->shutdown = mode; }
2213
2214int SSL_get_shutdown(const SSL *s) { return s->shutdown; }
2215
2216int SSL_version(const SSL *s) { return s->version; }
2217
2218SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
2219
2220SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
2221 if (ssl->ctx == ctx) {
2222 return ssl->ctx;
2223 }
2224
2225 if (ctx == NULL) {
2226 ctx = ssl->initial_ctx;
2227 }
2228
David Benjamin2755a3e2015-04-22 16:17:58 -04002229 ssl_cert_free(ssl->cert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002230 ssl->cert = ssl_cert_dup(ctx->cert);
David Benjamin2755a3e2015-04-22 16:17:58 -04002231
Adam Langley0b5e3902015-05-15 13:08:38 -07002232 CRYPTO_refcount_inc(&ctx->references);
David Benjamin2755a3e2015-04-22 16:17:58 -04002233 SSL_CTX_free(ssl->ctx); /* decrement reference count */
Adam Langleyfcf25832014-12-18 17:42:32 -08002234 ssl->ctx = ctx;
2235
2236 ssl->sid_ctx_length = ctx->sid_ctx_length;
2237 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2238 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2239
2240 return ssl->ctx;
2241}
2242
2243int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
2244 return X509_STORE_set_default_paths(ctx->cert_store);
2245}
Adam Langley95c29f32014-06-20 12:00:00 -07002246
2247int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
Adam Langleyfcf25832014-12-18 17:42:32 -08002248 const char *CApath) {
2249 return X509_STORE_load_locations(ctx->cert_store, CAfile, CApath);
2250}
Adam Langley95c29f32014-06-20 12:00:00 -07002251
2252void SSL_set_info_callback(SSL *ssl,
Adam Langleyfcf25832014-12-18 17:42:32 -08002253 void (*cb)(const SSL *ssl, int type, int val)) {
2254 ssl->info_callback = cb;
2255}
Adam Langley95c29f32014-06-20 12:00:00 -07002256
Adam Langleyfcf25832014-12-18 17:42:32 -08002257void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/, int /*type*/,
2258 int /*val*/) {
2259 return ssl->info_callback;
2260}
Adam Langley95c29f32014-06-20 12:00:00 -07002261
Adam Langleyfcf25832014-12-18 17:42:32 -08002262int SSL_state(const SSL *ssl) { return ssl->state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002263
David Benjaminece089c2015-05-15 23:52:42 -04002264void SSL_set_state(SSL *ssl, int state) { }
Adam Langley95c29f32014-06-20 12:00:00 -07002265
Adam Langleyfcf25832014-12-18 17:42:32 -08002266void SSL_set_verify_result(SSL *ssl, long arg) { ssl->verify_result = arg; }
Adam Langley95c29f32014-06-20 12:00:00 -07002267
Adam Langleyfcf25832014-12-18 17:42:32 -08002268long SSL_get_verify_result(const SSL *ssl) { return ssl->verify_result; }
Adam Langley95c29f32014-06-20 12:00:00 -07002269
Adam Langleyfcf25832014-12-18 17:42:32 -08002270int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2271 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002272 int index;
2273 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp,
2274 new_func, dup_func, free_func)) {
2275 return -1;
2276 }
2277 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002278}
Adam Langley95c29f32014-06-20 12:00:00 -07002279
Adam Langleyfcf25832014-12-18 17:42:32 -08002280int SSL_set_ex_data(SSL *s, int idx, void *arg) {
2281 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2282}
Adam Langley95c29f32014-06-20 12:00:00 -07002283
Adam Langleyfcf25832014-12-18 17:42:32 -08002284void *SSL_get_ex_data(const SSL *s, int idx) {
2285 return CRYPTO_get_ex_data(&s->ex_data, idx);
2286}
Adam Langley95c29f32014-06-20 12:00:00 -07002287
Adam Langleyfcf25832014-12-18 17:42:32 -08002288int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2289 CRYPTO_EX_dup *dup_func,
2290 CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002291 int index;
2292 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp,
2293 new_func, dup_func, free_func)) {
2294 return -1;
2295 }
2296 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002297}
Adam Langley95c29f32014-06-20 12:00:00 -07002298
Adam Langleyfcf25832014-12-18 17:42:32 -08002299int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg) {
2300 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2301}
Adam Langley95c29f32014-06-20 12:00:00 -07002302
Adam Langleyfcf25832014-12-18 17:42:32 -08002303void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx) {
2304 return CRYPTO_get_ex_data(&s->ex_data, idx);
2305}
Adam Langley95c29f32014-06-20 12:00:00 -07002306
Adam Langleyfcf25832014-12-18 17:42:32 -08002307X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
2308 return ctx->cert_store;
2309}
Adam Langley95c29f32014-06-20 12:00:00 -07002310
Adam Langleyfcf25832014-12-18 17:42:32 -08002311void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
David Benjamin2755a3e2015-04-22 16:17:58 -04002312 X509_STORE_free(ctx->cert_store);
Adam Langleyfcf25832014-12-18 17:42:32 -08002313 ctx->cert_store = store;
2314}
Adam Langley95c29f32014-06-20 12:00:00 -07002315
Adam Langleyfcf25832014-12-18 17:42:32 -08002316int SSL_want(const SSL *s) { return s->rwstate; }
Adam Langley95c29f32014-06-20 12:00:00 -07002317
Adam Langleyfcf25832014-12-18 17:42:32 -08002318void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
2319 RSA *(*cb)(SSL *ssl, int is_export,
2320 int keylength)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002321}
Adam Langley95c29f32014-06-20 12:00:00 -07002322
Adam Langleyfcf25832014-12-18 17:42:32 -08002323void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
2324 int keylength)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002325}
2326
2327void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
David Benjamin59015c32015-04-26 13:13:55 -04002328 DH *(*callback)(SSL *ssl, int is_export,
2329 int keylength)) {
2330 ctx->cert->dh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002331}
2332
David Benjamin59015c32015-04-26 13:13:55 -04002333void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*callback)(SSL *ssl, int is_export,
2334 int keylength)) {
2335 ssl->cert->dh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002336}
2337
2338void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,
David Benjamindd978782015-04-24 15:20:13 -04002339 EC_KEY *(*callback)(SSL *ssl, int is_export,
2340 int keylength)) {
David Benjamin59015c32015-04-26 13:13:55 -04002341 ctx->cert->ecdh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002342}
2343
2344void SSL_set_tmp_ecdh_callback(SSL *ssl,
David Benjamindd978782015-04-24 15:20:13 -04002345 EC_KEY *(*callback)(SSL *ssl, int is_export,
2346 int keylength)) {
David Benjamin59015c32015-04-26 13:13:55 -04002347 ssl->cert->ecdh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002348}
2349
2350int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
2351 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
David Benjamin3570d732015-06-29 00:28:17 -04002352 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -08002353 return 0;
2354 }
2355
David Benjamin2755a3e2015-04-22 16:17:58 -04002356 OPENSSL_free(ctx->psk_identity_hint);
Adam Langleyfcf25832014-12-18 17:42:32 -08002357
2358 if (identity_hint != NULL) {
2359 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2360 if (ctx->psk_identity_hint == NULL) {
2361 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002362 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002363 } else {
2364 ctx->psk_identity_hint = NULL;
2365 }
Adam Langley95c29f32014-06-20 12:00:00 -07002366
Adam Langleyfcf25832014-12-18 17:42:32 -08002367 return 1;
2368}
2369
2370int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint) {
2371 if (s == NULL) {
2372 return 0;
2373 }
2374
2375 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
David Benjamin3570d732015-06-29 00:28:17 -04002376 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -08002377 return 0;
2378 }
2379
2380 /* Clear currently configured hint, if any. */
David Benjamin2755a3e2015-04-22 16:17:58 -04002381 OPENSSL_free(s->psk_identity_hint);
2382 s->psk_identity_hint = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002383
2384 if (identity_hint != NULL) {
2385 s->psk_identity_hint = BUF_strdup(identity_hint);
2386 if (s->psk_identity_hint == NULL) {
2387 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002388 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002389 }
Adam Langley95c29f32014-06-20 12:00:00 -07002390
Adam Langleyfcf25832014-12-18 17:42:32 -08002391 return 1;
2392}
Adam Langley95c29f32014-06-20 12:00:00 -07002393
Adam Langleyfcf25832014-12-18 17:42:32 -08002394const char *SSL_get_psk_identity_hint(const SSL *s) {
2395 if (s == NULL) {
2396 return NULL;
2397 }
2398 return s->psk_identity_hint;
2399}
Adam Langley95c29f32014-06-20 12:00:00 -07002400
Adam Langleyfcf25832014-12-18 17:42:32 -08002401const char *SSL_get_psk_identity(const SSL *s) {
2402 if (s == NULL || s->session == NULL) {
2403 return NULL;
2404 }
Adam Langley95c29f32014-06-20 12:00:00 -07002405
Adam Langleyfcf25832014-12-18 17:42:32 -08002406 return s->session->psk_identity;
2407}
Adam Langley95c29f32014-06-20 12:00:00 -07002408
Adam Langleyfcf25832014-12-18 17:42:32 -08002409void SSL_set_psk_client_callback(
2410 SSL *s, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2411 unsigned int max_identity_len, uint8_t *psk,
2412 unsigned int max_psk_len)) {
2413 s->psk_client_callback = cb;
2414}
Adam Langley95c29f32014-06-20 12:00:00 -07002415
Adam Langleyfcf25832014-12-18 17:42:32 -08002416void SSL_CTX_set_psk_client_callback(
2417 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2418 unsigned int max_identity_len,
2419 uint8_t *psk, unsigned int max_psk_len)) {
2420 ctx->psk_client_callback = cb;
2421}
Adam Langley95c29f32014-06-20 12:00:00 -07002422
Adam Langleyfcf25832014-12-18 17:42:32 -08002423void SSL_set_psk_server_callback(
2424 SSL *s, unsigned int (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
2425 unsigned int max_psk_len)) {
2426 s->psk_server_callback = cb;
2427}
Adam Langley95c29f32014-06-20 12:00:00 -07002428
Adam Langleyfcf25832014-12-18 17:42:32 -08002429void SSL_CTX_set_psk_server_callback(
2430 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *identity,
2431 uint8_t *psk, unsigned int max_psk_len)) {
2432 ctx->psk_server_callback = cb;
2433}
Adam Langley95c29f32014-06-20 12:00:00 -07002434
Adam Langleyfcf25832014-12-18 17:42:32 -08002435void SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) {
2436 ctx->min_version = version;
2437}
Adam Langley95c29f32014-06-20 12:00:00 -07002438
Adam Langleyfcf25832014-12-18 17:42:32 -08002439void SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) {
2440 ctx->max_version = version;
2441}
Adam Langley0289c732014-06-20 12:00:00 -07002442
Adam Langleyfcf25832014-12-18 17:42:32 -08002443void SSL_set_min_version(SSL *ssl, uint16_t version) {
2444 ssl->min_version = version;
2445}
Adam Langley0289c732014-06-20 12:00:00 -07002446
Adam Langleyfcf25832014-12-18 17:42:32 -08002447void SSL_set_max_version(SSL *ssl, uint16_t version) {
2448 ssl->max_version = version;
2449}
Adam Langley95c29f32014-06-20 12:00:00 -07002450
Adam Langleyfcf25832014-12-18 17:42:32 -08002451void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
2452 void (*cb)(int write_p, int version,
2453 int content_type, const void *buf,
2454 size_t len, SSL *ssl, void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002455 ctx->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002456}
David Benjamin61ecccf2015-05-05 09:44:51 -04002457
2458void SSL_CTX_set_msg_callback_arg(SSL_CTX *ctx, void *arg) {
2459 ctx->msg_callback_arg = arg;
2460}
2461
Adam Langleyfcf25832014-12-18 17:42:32 -08002462void SSL_set_msg_callback(SSL *ssl,
2463 void (*cb)(int write_p, int version, int content_type,
2464 const void *buf, size_t len, SSL *ssl,
2465 void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002466 ssl->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002467}
Adam Langley95c29f32014-06-20 12:00:00 -07002468
David Benjamin61ecccf2015-05-05 09:44:51 -04002469void SSL_set_msg_callback_arg(SSL *ssl, void *arg) {
2470 ssl->msg_callback_arg = arg;
2471}
2472
Adam Langleyfcf25832014-12-18 17:42:32 -08002473void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio) {
David Benjamin2755a3e2015-04-22 16:17:58 -04002474 BIO_free(ctx->keylog_bio);
Adam Langleyfcf25832014-12-18 17:42:32 -08002475 ctx->keylog_bio = keylog_bio;
2476}
Adam Langley95c29f32014-06-20 12:00:00 -07002477
Adam Langleyfcf25832014-12-18 17:42:32 -08002478static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
2479 static const char hextable[] = "0123456789abcdef";
2480 uint8_t *out;
2481 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002482
Adam Langleyfcf25832014-12-18 17:42:32 -08002483 if (!CBB_add_space(cbb, &out, in_len * 2)) {
2484 return 0;
2485 }
Adam Langley95c29f32014-06-20 12:00:00 -07002486
Adam Langleyfcf25832014-12-18 17:42:32 -08002487 for (i = 0; i < in_len; i++) {
2488 *(out++) = (uint8_t)hextable[in[i] >> 4];
2489 *(out++) = (uint8_t)hextable[in[i] & 0xf];
2490 }
Adam Langley95c29f32014-06-20 12:00:00 -07002491
Adam Langleyfcf25832014-12-18 17:42:32 -08002492 return 1;
2493}
David Benjamin859ec3c2014-09-02 16:29:36 -04002494
2495int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -08002496 const uint8_t *encrypted_premaster,
2497 size_t encrypted_premaster_len,
2498 const uint8_t *premaster,
2499 size_t premaster_len) {
2500 BIO *bio = ctx->keylog_bio;
2501 CBB cbb;
2502 uint8_t *out;
2503 size_t out_len;
2504 int ret;
David Benjamin859ec3c2014-09-02 16:29:36 -04002505
Adam Langleyfcf25832014-12-18 17:42:32 -08002506 if (bio == NULL) {
2507 return 1;
2508 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002509
Adam Langleyfcf25832014-12-18 17:42:32 -08002510 if (encrypted_premaster_len < 8) {
David Benjamin3570d732015-06-29 00:28:17 -04002511 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08002512 return 0;
2513 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002514
David Benjamina8653202015-06-28 01:26:10 -04002515 CBB_zero(&cbb);
2516 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len * 2 + 1) ||
2517 !CBB_add_bytes(&cbb, (const uint8_t *)"RSA ", 4) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08002518 /* Only the first 8 bytes of the encrypted premaster secret are
2519 * logged. */
2520 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
2521 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2522 !cbb_add_hex(&cbb, premaster, premaster_len) ||
2523 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2524 !CBB_finish(&cbb, &out, &out_len)) {
2525 CBB_cleanup(&cbb);
2526 return 0;
2527 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002528
Adam Langley4bdb6e42015-05-15 15:29:21 -07002529 CRYPTO_MUTEX_lock_write(&ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -08002530 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
Adam Langley4bdb6e42015-05-15 15:29:21 -07002531 CRYPTO_MUTEX_unlock(&ctx->lock);
David Benjamin859ec3c2014-09-02 16:29:36 -04002532
Adam Langleyfcf25832014-12-18 17:42:32 -08002533 OPENSSL_free(out);
2534 return ret;
Adam Langley95f22882014-06-20 12:00:00 -07002535}
2536
Adam Langleyfcf25832014-12-18 17:42:32 -08002537int ssl_ctx_log_master_secret(SSL_CTX *ctx, const uint8_t *client_random,
2538 size_t client_random_len, const uint8_t *master,
2539 size_t master_len) {
2540 BIO *bio = ctx->keylog_bio;
2541 CBB cbb;
2542 uint8_t *out;
2543 size_t out_len;
2544 int ret;
Adam Langleyadb739e2014-06-20 12:00:00 -07002545
Adam Langleyfcf25832014-12-18 17:42:32 -08002546 if (bio == NULL) {
2547 return 1;
2548 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002549
Adam Langleyfcf25832014-12-18 17:42:32 -08002550 if (client_random_len != 32) {
David Benjamin3570d732015-06-29 00:28:17 -04002551 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08002552 return 0;
2553 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002554
David Benjamina8653202015-06-28 01:26:10 -04002555 CBB_zero(&cbb);
2556 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len * 2 + 1) ||
2557 !CBB_add_bytes(&cbb, (const uint8_t *)"CLIENT_RANDOM ", 14) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08002558 !cbb_add_hex(&cbb, client_random, 32) ||
2559 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2560 !cbb_add_hex(&cbb, master, master_len) ||
2561 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2562 !CBB_finish(&cbb, &out, &out_len)) {
2563 CBB_cleanup(&cbb);
2564 return 0;
2565 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002566
Adam Langley4bdb6e42015-05-15 15:29:21 -07002567 CRYPTO_MUTEX_lock_write(&ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -08002568 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
Adam Langley4bdb6e42015-05-15 15:29:21 -07002569 CRYPTO_MUTEX_unlock(&ctx->lock);
David Benjamine99e9122014-12-11 01:46:01 -05002570
Adam Langleyfcf25832014-12-18 17:42:32 -08002571 OPENSSL_free(out);
2572 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -07002573}
2574
David Benjamined7c4752015-02-16 19:16:46 -05002575int SSL_in_false_start(const SSL *s) {
2576 return s->s3->tmp.in_false_start;
2577}
2578
Adam Langleyfcf25832014-12-18 17:42:32 -08002579int SSL_cutthrough_complete(const SSL *s) {
David Benjamined7c4752015-02-16 19:16:46 -05002580 return SSL_in_false_start(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002581}
Adam Langley95c29f32014-06-20 12:00:00 -07002582
Adam Langleyfcf25832014-12-18 17:42:32 -08002583void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
2584 size_t *ssl_session_size) {
2585 *ssl_size = sizeof(SSL);
2586 *ssl_ctx_size = sizeof(SSL_CTX);
2587 *ssl_session_size = sizeof(SSL_SESSION);
2588}
Feng Lu41aa3252014-11-21 22:47:56 -08002589
David Benjamined7c4752015-02-16 19:16:46 -05002590int ssl3_can_false_start(const SSL *s) {
David Benjamin195dc782015-02-19 13:27:05 -05002591 const SSL_CIPHER *const cipher = SSL_get_current_cipher(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002592
Adam Langleye631d962015-04-01 13:11:01 -07002593 /* False Start only for TLS 1.2 with an ECDHE+AEAD cipher and ALPN or NPN. */
David Benjamin195dc782015-02-19 13:27:05 -05002594 return !SSL_IS_DTLS(s) &&
2595 SSL_version(s) >= TLS1_2_VERSION &&
2596 (s->s3->alpn_selected || s->s3->next_proto_neg_seen) &&
2597 cipher != NULL &&
Adam Langleye631d962015-04-01 13:11:01 -07002598 cipher->algorithm_mkey == SSL_kECDHE &&
David Benjamin195dc782015-02-19 13:27:05 -05002599 (cipher->algorithm_enc == SSL_AES128GCM ||
2600 cipher->algorithm_enc == SSL_AES256GCM ||
2601 cipher->algorithm_enc == SSL_CHACHA20POLY1305);
Adam Langleyfcf25832014-12-18 17:42:32 -08002602}
2603
2604const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version) {
2605 switch (version) {
2606 case SSL3_VERSION:
2607 return &SSLv3_enc_data;
2608
2609 case TLS1_VERSION:
2610 return &TLSv1_enc_data;
2611
David Benjamin9e13e1a2015-03-05 01:56:32 -05002612 case DTLS1_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002613 case TLS1_1_VERSION:
2614 return &TLSv1_1_enc_data;
2615
David Benjamin9e13e1a2015-03-05 01:56:32 -05002616 case DTLS1_2_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002617 case TLS1_2_VERSION:
2618 return &TLSv1_2_enc_data;
2619
Adam Langleyfcf25832014-12-18 17:42:32 -08002620 default:
2621 return NULL;
2622 }
2623}
2624
2625uint16_t ssl3_get_max_server_version(const SSL *s) {
2626 uint16_t max_version;
2627
2628 if (SSL_IS_DTLS(s)) {
2629 max_version = (s->max_version != 0) ? s->max_version : DTLS1_2_VERSION;
2630 if (!(s->options & SSL_OP_NO_DTLSv1_2) && DTLS1_2_VERSION >= max_version) {
2631 return DTLS1_2_VERSION;
2632 }
2633 if (!(s->options & SSL_OP_NO_DTLSv1) && DTLS1_VERSION >= max_version) {
2634 return DTLS1_VERSION;
2635 }
2636 return 0;
2637 }
2638
2639 max_version = (s->max_version != 0) ? s->max_version : TLS1_2_VERSION;
2640 if (!(s->options & SSL_OP_NO_TLSv1_2) && TLS1_2_VERSION <= max_version) {
2641 return TLS1_2_VERSION;
2642 }
2643 if (!(s->options & SSL_OP_NO_TLSv1_1) && TLS1_1_VERSION <= max_version) {
2644 return TLS1_1_VERSION;
2645 }
2646 if (!(s->options & SSL_OP_NO_TLSv1) && TLS1_VERSION <= max_version) {
2647 return TLS1_VERSION;
2648 }
2649 if (!(s->options & SSL_OP_NO_SSLv3) && SSL3_VERSION <= max_version) {
2650 return SSL3_VERSION;
2651 }
2652 return 0;
2653}
2654
2655uint16_t ssl3_get_mutual_version(SSL *s, uint16_t client_version) {
2656 uint16_t version = 0;
2657
2658 if (SSL_IS_DTLS(s)) {
2659 /* Clamp client_version to max_version. */
2660 if (s->max_version != 0 && client_version < s->max_version) {
2661 client_version = s->max_version;
2662 }
2663
2664 if (client_version <= DTLS1_2_VERSION && !(s->options & SSL_OP_NO_DTLSv1_2)) {
2665 version = DTLS1_2_VERSION;
2666 } else if (client_version <= DTLS1_VERSION &&
David Benjaminb18f0242015-03-10 18:30:08 -04002667 !(s->options & SSL_OP_NO_DTLSv1)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002668 version = DTLS1_VERSION;
2669 }
2670
2671 /* Check against min_version. */
2672 if (version != 0 && s->min_version != 0 && version > s->min_version) {
2673 return 0;
2674 }
2675 return version;
2676 } else {
2677 /* Clamp client_version to max_version. */
2678 if (s->max_version != 0 && client_version > s->max_version) {
2679 client_version = s->max_version;
2680 }
2681
2682 if (client_version >= TLS1_2_VERSION && !(s->options & SSL_OP_NO_TLSv1_2)) {
2683 version = TLS1_2_VERSION;
2684 } else if (client_version >= TLS1_1_VERSION &&
2685 !(s->options & SSL_OP_NO_TLSv1_1)) {
2686 version = TLS1_1_VERSION;
2687 } else if (client_version >= TLS1_VERSION && !(s->options & SSL_OP_NO_TLSv1)) {
2688 version = TLS1_VERSION;
2689 } else if (client_version >= SSL3_VERSION && !(s->options & SSL_OP_NO_SSLv3)) {
2690 version = SSL3_VERSION;
2691 }
2692
2693 /* Check against min_version. */
2694 if (version != 0 && s->min_version != 0 && version < s->min_version) {
2695 return 0;
2696 }
2697 return version;
2698 }
2699}
2700
2701uint16_t ssl3_get_max_client_version(SSL *s) {
David Benjamin123a8fd2015-04-26 14:16:41 -04002702 uint32_t options = s->options;
Adam Langleyfcf25832014-12-18 17:42:32 -08002703 uint16_t version = 0;
2704
2705 /* OpenSSL's API for controlling versions entails blacklisting individual
2706 * protocols. This has two problems. First, on the client, the protocol can
2707 * only express a contiguous range of versions. Second, a library consumer
2708 * trying to set a maximum version cannot disable protocol versions that get
2709 * added in a future version of the library.
2710 *
2711 * To account for both of these, OpenSSL interprets the client-side bitmask
2712 * as a min/max range by picking the lowest contiguous non-empty range of
2713 * enabled protocols. Note that this means it is impossible to set a maximum
2714 * version of TLS 1.2 in a future-proof way.
2715 *
2716 * By this scheme, the maximum version is the lowest version V such that V is
2717 * enabled and V+1 is disabled or unimplemented. */
2718 if (SSL_IS_DTLS(s)) {
2719 if (!(options & SSL_OP_NO_DTLSv1_2)) {
2720 version = DTLS1_2_VERSION;
2721 }
2722 if (!(options & SSL_OP_NO_DTLSv1) && (options & SSL_OP_NO_DTLSv1_2)) {
2723 version = DTLS1_VERSION;
2724 }
2725 if (s->max_version != 0 && version < s->max_version) {
2726 version = s->max_version;
2727 }
2728 } else {
2729 if (!(options & SSL_OP_NO_TLSv1_2)) {
2730 version = TLS1_2_VERSION;
2731 }
2732 if (!(options & SSL_OP_NO_TLSv1_1) && (options & SSL_OP_NO_TLSv1_2)) {
2733 version = TLS1_1_VERSION;
2734 }
2735 if (!(options & SSL_OP_NO_TLSv1) && (options & SSL_OP_NO_TLSv1_1)) {
2736 version = TLS1_VERSION;
2737 }
2738 if (!(options & SSL_OP_NO_SSLv3) && (options & SSL_OP_NO_TLSv1)) {
2739 version = SSL3_VERSION;
2740 }
2741 if (s->max_version != 0 && version > s->max_version) {
2742 version = s->max_version;
2743 }
2744 }
2745
2746 return version;
2747}
2748
2749int ssl3_is_version_enabled(SSL *s, uint16_t version) {
2750 if (SSL_IS_DTLS(s)) {
2751 if (s->max_version != 0 && version < s->max_version) {
2752 return 0;
2753 }
2754 if (s->min_version != 0 && version > s->min_version) {
2755 return 0;
2756 }
2757
2758 switch (version) {
2759 case DTLS1_VERSION:
2760 return !(s->options & SSL_OP_NO_DTLSv1);
2761
2762 case DTLS1_2_VERSION:
2763 return !(s->options & SSL_OP_NO_DTLSv1_2);
2764
2765 default:
2766 return 0;
2767 }
2768 } else {
2769 if (s->max_version != 0 && version > s->max_version) {
2770 return 0;
2771 }
2772 if (s->min_version != 0 && version < s->min_version) {
2773 return 0;
2774 }
2775
2776 switch (version) {
2777 case SSL3_VERSION:
2778 return !(s->options & SSL_OP_NO_SSLv3);
2779
2780 case TLS1_VERSION:
2781 return !(s->options & SSL_OP_NO_TLSv1);
2782
2783 case TLS1_1_VERSION:
2784 return !(s->options & SSL_OP_NO_TLSv1_1);
2785
2786 case TLS1_2_VERSION:
2787 return !(s->options & SSL_OP_NO_TLSv1_2);
2788
2789 default:
2790 return 0;
2791 }
2792 }
2793}
2794
David Benjaminea72bd02014-12-21 21:27:41 -05002795uint16_t ssl3_version_from_wire(SSL *s, uint16_t wire_version) {
2796 if (!SSL_IS_DTLS(s)) {
2797 return wire_version;
2798 }
2799
2800 uint16_t tls_version = ~wire_version;
2801 uint16_t version = tls_version + 0x0201;
2802 /* If either component overflowed, clamp it so comparisons still work. */
2803 if ((version >> 8) < (tls_version >> 8)) {
2804 version = 0xff00 | (version & 0xff);
2805 }
2806 if ((version & 0xff) < (tls_version & 0xff)) {
2807 version = (version & 0xff00) | 0xff;
2808 }
2809 /* DTLS 1.0 maps to TLS 1.1, not TLS 1.0. */
2810 if (version == TLS1_VERSION) {
2811 version = TLS1_1_VERSION;
2812 }
2813 return version;
2814}
2815
Adam Langleyfcf25832014-12-18 17:42:32 -08002816int SSL_cache_hit(SSL *s) { return s->hit; }
2817
2818int SSL_is_server(SSL *s) { return s->server; }
2819
Adam Langley524e7172015-02-20 16:04:00 -08002820void SSL_CTX_set_dos_protection_cb(
2821 SSL_CTX *ctx, int (*cb)(const struct ssl_early_callback_ctx *)) {
2822 ctx->dos_protection_cb = cb;
2823}
2824
Adam Langleyfcf25832014-12-18 17:42:32 -08002825void SSL_enable_fastradio_padding(SSL *s, char on_off) {
2826 s->fastradio_padding = on_off;
2827}
Adam Langley44972942015-02-20 13:43:23 -08002828
David Benjaminb16346b2015-04-08 19:16:58 -04002829void SSL_set_reject_peer_renegotiations(SSL *s, int reject) {
David Benjamin897e5e02015-05-12 17:03:54 -04002830 s->accept_peer_renegotiations = !reject;
David Benjaminb16346b2015-04-08 19:16:58 -04002831}
2832
Adam Langley3f92d212015-02-20 15:32:52 -08002833int SSL_get_rc4_state(const SSL *ssl, const RC4_KEY **read_key,
2834 const RC4_KEY **write_key) {
2835 if (ssl->aead_read_ctx == NULL || ssl->aead_write_ctx == NULL) {
2836 return 0;
2837 }
2838
2839 return EVP_AEAD_CTX_get_rc4_state(&ssl->aead_read_ctx->ctx, read_key) &&
2840 EVP_AEAD_CTX_get_rc4_state(&ssl->aead_write_ctx->ctx, write_key);
2841}
David Benjaminda881e92015-04-26 14:45:04 -04002842
Adam Langleyaf0e32c2015-06-03 09:57:23 -07002843int SSL_get_tls_unique(const SSL *ssl, uint8_t *out, size_t *out_len,
2844 size_t max_out) {
2845 /* The tls-unique value is the first Finished message in the handshake, which
2846 * is the client's in a full handshake and the server's for a resumption. See
2847 * https://tools.ietf.org/html/rfc5929#section-3.1. */
2848 const uint8_t *finished = ssl->s3->previous_client_finished;
2849 size_t finished_len = ssl->s3->previous_client_finished_len;
2850 if (ssl->hit) {
2851 /* tls-unique is broken for resumed sessions unless EMS is used. */
2852 if (!ssl->session->extended_master_secret) {
2853 goto err;
2854 }
2855 finished = ssl->s3->previous_server_finished;
2856 finished_len = ssl->s3->previous_server_finished_len;
2857 }
2858
2859 if (!ssl->s3->initial_handshake_complete ||
2860 ssl->version < TLS1_VERSION) {
2861 goto err;
2862 }
2863
2864 *out_len = finished_len;
2865 if (finished_len > max_out) {
2866 *out_len = max_out;
2867 }
2868
2869 memcpy(out, finished, *out_len);
2870 return 1;
2871
2872err:
2873 *out_len = 0;
2874 memset(out, 0, max_out);
2875 return 0;
2876}
2877
David Benjaminda881e92015-04-26 14:45:04 -04002878int SSL_CTX_sess_connect(const SSL_CTX *ctx) { return 0; }
2879int SSL_CTX_sess_connect_good(const SSL_CTX *ctx) { return 0; }
2880int SSL_CTX_sess_connect_renegotiate(const SSL_CTX *ctx) { return 0; }
2881int SSL_CTX_sess_accept(const SSL_CTX *ctx) { return 0; }
2882int SSL_CTX_sess_accept_renegotiate(const SSL_CTX *ctx) { return 0; }
2883int SSL_CTX_sess_accept_good(const SSL_CTX *ctx) { return 0; }
2884int SSL_CTX_sess_hits(const SSL_CTX *ctx) { return 0; }
2885int SSL_CTX_sess_cb_hits(const SSL_CTX *ctx) { return 0; }
2886int SSL_CTX_sess_misses(const SSL_CTX *ctx) { return 0; }
2887int SSL_CTX_sess_timeouts(const SSL_CTX *ctx) { return 0; }
2888int SSL_CTX_sess_cache_full(const SSL_CTX *ctx) { return 0; }