blob: 7149861dd22cc29560c65ebd25bcbb3574656572 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
108 *
109 */
110/* ====================================================================
111 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
112 * ECC cipher suite support in OpenSSL originally developed by
113 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
114 */
115/* ====================================================================
116 * Copyright 2005 Nokia. All rights reserved.
117 *
118 * The portions of the attached software ("Contribution") is developed by
119 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
120 * license.
121 *
122 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
123 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
124 * support (see RFC 4279) to OpenSSL.
125 *
126 * No patent licenses or other rights except those expressly stated in
127 * the OpenSSL open source license shall be deemed granted or received
128 * expressly, by implication, estoppel, or otherwise.
129 *
130 * No assurances are provided by Nokia that the Contribution does not
131 * infringe the patent or other intellectual property rights of any third
132 * party or that the license provides you with all the necessary rights
133 * to make use of the Contribution.
134 *
135 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
136 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
137 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
138 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
139 * OTHERWISE. */
140
Adam Langley95c29f32014-06-20 12:00:00 -0700141#include <assert.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400142#include <stdio.h>
143#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700144
David Benjamin39482a12014-07-20 13:30:15 -0400145#include <openssl/bytestring.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700146#include <openssl/dh.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400147#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700148#include <openssl/lhash.h>
149#include <openssl/mem.h>
150#include <openssl/obj.h>
151#include <openssl/rand.h>
152#include <openssl/x509v3.h>
153
David Benjamin2ee94aa2015-04-07 22:38:30 -0400154#include "internal.h"
David Benjamin546f1a52015-04-15 16:46:09 -0400155#include "../crypto/internal.h"
156
Adam Langley95c29f32014-06-20 12:00:00 -0700157
Adam Langleyfcf25832014-12-18 17:42:32 -0800158/* Some error codes are special. Ensure the make_errors.go script never
159 * regresses this. */
160OPENSSL_COMPILE_ASSERT(SSL_R_TLSV1_ALERT_NO_RENEGOTIATION ==
161 SSL_AD_NO_RENEGOTIATION + SSL_AD_REASON_OFFSET,
162 ssl_alert_reason_code_mismatch);
David Benjamine1136082014-09-20 12:28:58 -0400163
David Benjamin1d0a1942015-04-26 15:35:35 -0400164/* kMaxHandshakeSize is the maximum size, in bytes, of a handshake message. */
165static const size_t kMaxHandshakeSize = (1u << 24) - 1;
166
David Benjamin9f33fc62015-04-15 17:29:53 -0400167static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl = CRYPTO_EX_DATA_CLASS_INIT;
168static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl_ctx = CRYPTO_EX_DATA_CLASS_INIT;
169
David Benjamin4831c332015-05-16 11:43:13 -0400170int SSL_clear(SSL *ssl) {
171 if (ssl->method == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800172 OPENSSL_PUT_ERROR(SSL, SSL_clear, SSL_R_NO_METHOD_SPECIFIED);
173 return 0;
174 }
Adam Langley95c29f32014-06-20 12:00:00 -0700175
David Benjamin4831c332015-05-16 11:43:13 -0400176 if (ssl_clear_bad_session(ssl)) {
177 SSL_SESSION_free(ssl->session);
178 ssl->session = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800179 }
Adam Langley95c29f32014-06-20 12:00:00 -0700180
David Benjamin4831c332015-05-16 11:43:13 -0400181 ssl->hit = 0;
182 ssl->shutdown = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700183
David Benjamin4831c332015-05-16 11:43:13 -0400184 /* SSL_clear may be called before or after the |ssl| is initialized in either
Adam Langleyfcf25832014-12-18 17:42:32 -0800185 * accept or connect state. In the latter case, SSL_clear should preserve the
David Benjamin4831c332015-05-16 11:43:13 -0400186 * half and reset |ssl->state| accordingly. */
187 if (ssl->handshake_func != NULL) {
188 if (ssl->server) {
189 SSL_set_accept_state(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800190 } else {
David Benjamin4831c332015-05-16 11:43:13 -0400191 SSL_set_connect_state(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800192 }
193 } else {
David Benjamin4831c332015-05-16 11:43:13 -0400194 assert(ssl->state == 0);
Adam Langleyfcf25832014-12-18 17:42:32 -0800195 }
Adam Langley95c29f32014-06-20 12:00:00 -0700196
David Benjamin5f387e32015-05-15 22:05:42 -0400197 /* TODO(davidben): Some state on |ssl| is reset both in |SSL_new| and
David Benjamin62fd1622015-01-11 13:30:01 -0500198 * |SSL_clear| because it is per-connection state rather than configuration
David Benjamin4831c332015-05-16 11:43:13 -0400199 * state. Per-connection state should be on |ssl->s3| and |ssl->d1| so it is
David Benjamin62fd1622015-01-11 13:30:01 -0500200 * naturally reset at the right points between |SSL_new|, |SSL_clear|, and
201 * |ssl3_new|. */
202
David Benjamin4831c332015-05-16 11:43:13 -0400203 ssl->rwstate = SSL_NOTHING;
204 ssl->rstate = SSL_ST_READ_HEADER;
Adam Langley95c29f32014-06-20 12:00:00 -0700205
David Benjamin4831c332015-05-16 11:43:13 -0400206 BUF_MEM_free(ssl->init_buf);
207 ssl->init_buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700208
David Benjamin4831c332015-05-16 11:43:13 -0400209 ssl->packet = NULL;
210 ssl->packet_length = 0;
David Benjamin62fd1622015-01-11 13:30:01 -0500211
David Benjamin4831c332015-05-16 11:43:13 -0400212 ssl_clear_cipher_ctx(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700213
David Benjamin4831c332015-05-16 11:43:13 -0400214 OPENSSL_free(ssl->next_proto_negotiated);
215 ssl->next_proto_negotiated = NULL;
216 ssl->next_proto_negotiated_len = 0;
David Benjamin62fd1622015-01-11 13:30:01 -0500217
David Benjamin4831c332015-05-16 11:43:13 -0400218 /* The ssl->d1->mtu is simultaneously configuration (preserved across
David Benjamin62fd1622015-01-11 13:30:01 -0500219 * clear) and connection-specific state (gets reset).
220 *
221 * TODO(davidben): Avoid this. */
222 unsigned mtu = 0;
David Benjamin4831c332015-05-16 11:43:13 -0400223 if (ssl->d1 != NULL) {
224 mtu = ssl->d1->mtu;
David Benjamin62fd1622015-01-11 13:30:01 -0500225 }
226
David Benjamin4831c332015-05-16 11:43:13 -0400227 ssl->method->ssl_free(ssl);
228 if (!ssl->method->ssl_new(ssl)) {
David Benjamin62fd1622015-01-11 13:30:01 -0500229 return 0;
230 }
David Benjamin4831c332015-05-16 11:43:13 -0400231 ssl->enc_method = ssl3_get_enc_method(ssl->version);
232 assert(ssl->enc_method != NULL);
David Benjamin62fd1622015-01-11 13:30:01 -0500233
David Benjamin4831c332015-05-16 11:43:13 -0400234 if (SSL_IS_DTLS(ssl) && (SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
235 ssl->d1->mtu = mtu;
David Benjamin62fd1622015-01-11 13:30:01 -0500236 }
237
David Benjamin4831c332015-05-16 11:43:13 -0400238 ssl->client_version = ssl->version;
Adam Langley95c29f32014-06-20 12:00:00 -0700239
Adam Langleyfcf25832014-12-18 17:42:32 -0800240 return 1;
241}
Adam Langley95c29f32014-06-20 12:00:00 -0700242
Adam Langleyfcf25832014-12-18 17:42:32 -0800243SSL *SSL_new(SSL_CTX *ctx) {
244 SSL *s;
Adam Langley95c29f32014-06-20 12:00:00 -0700245
Adam Langleyfcf25832014-12-18 17:42:32 -0800246 if (ctx == NULL) {
247 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_NULL_SSL_CTX);
248 return NULL;
249 }
250 if (ctx->method == NULL) {
251 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
252 return NULL;
253 }
Adam Langley95c29f32014-06-20 12:00:00 -0700254
Adam Langleyfcf25832014-12-18 17:42:32 -0800255 s = (SSL *)OPENSSL_malloc(sizeof(SSL));
256 if (s == NULL) {
257 goto err;
258 }
259 memset(s, 0, sizeof(SSL));
Adam Langley95c29f32014-06-20 12:00:00 -0700260
Adam Langleyfcf25832014-12-18 17:42:32 -0800261 s->min_version = ctx->min_version;
262 s->max_version = ctx->max_version;
David Benjamin1eb367c2014-12-12 18:17:51 -0500263
Adam Langleyfcf25832014-12-18 17:42:32 -0800264 s->options = ctx->options;
265 s->mode = ctx->mode;
266 s->max_cert_list = ctx->max_cert_list;
Adam Langley95c29f32014-06-20 12:00:00 -0700267
David Benjamina5a3eeb2015-03-18 20:26:30 -0400268 s->cert = ssl_cert_dup(ctx->cert);
269 if (s->cert == NULL) {
270 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -0800271 }
Adam Langley95c29f32014-06-20 12:00:00 -0700272
Adam Langleyfcf25832014-12-18 17:42:32 -0800273 s->msg_callback = ctx->msg_callback;
274 s->msg_callback_arg = ctx->msg_callback_arg;
275 s->verify_mode = ctx->verify_mode;
276 s->sid_ctx_length = ctx->sid_ctx_length;
277 assert(s->sid_ctx_length <= sizeof s->sid_ctx);
278 memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));
279 s->verify_callback = ctx->default_verify_callback;
280 s->generate_session_id = ctx->generate_session_id;
Adam Langley95c29f32014-06-20 12:00:00 -0700281
Adam Langleyfcf25832014-12-18 17:42:32 -0800282 s->param = X509_VERIFY_PARAM_new();
283 if (!s->param) {
284 goto err;
285 }
286 X509_VERIFY_PARAM_inherit(s->param, ctx->param);
287 s->quiet_shutdown = ctx->quiet_shutdown;
288 s->max_send_fragment = ctx->max_send_fragment;
Adam Langley95c29f32014-06-20 12:00:00 -0700289
Adam Langley0b5e3902015-05-15 13:08:38 -0700290 CRYPTO_refcount_inc(&ctx->references);
Adam Langleyfcf25832014-12-18 17:42:32 -0800291 s->ctx = ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -0800292 s->tlsext_ticket_expected = 0;
Adam Langley0b5e3902015-05-15 13:08:38 -0700293 CRYPTO_refcount_inc(&ctx->references);
Adam Langleyfcf25832014-12-18 17:42:32 -0800294 s->initial_ctx = ctx;
295 if (ctx->tlsext_ecpointformatlist) {
296 s->tlsext_ecpointformatlist = BUF_memdup(
297 ctx->tlsext_ecpointformatlist, ctx->tlsext_ecpointformatlist_length);
298 if (!s->tlsext_ecpointformatlist) {
299 goto err;
300 }
301 s->tlsext_ecpointformatlist_length = ctx->tlsext_ecpointformatlist_length;
302 }
Adam Langley95c29f32014-06-20 12:00:00 -0700303
Adam Langleyfcf25832014-12-18 17:42:32 -0800304 if (ctx->tlsext_ellipticcurvelist) {
305 s->tlsext_ellipticcurvelist =
306 BUF_memdup(ctx->tlsext_ellipticcurvelist,
307 ctx->tlsext_ellipticcurvelist_length * 2);
308 if (!s->tlsext_ellipticcurvelist) {
309 goto err;
310 }
311 s->tlsext_ellipticcurvelist_length = ctx->tlsext_ellipticcurvelist_length;
312 }
313 s->next_proto_negotiated = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700314
Adam Langleyfcf25832014-12-18 17:42:32 -0800315 if (s->ctx->alpn_client_proto_list) {
316 s->alpn_client_proto_list = BUF_memdup(s->ctx->alpn_client_proto_list,
317 s->ctx->alpn_client_proto_list_len);
318 if (s->alpn_client_proto_list == NULL) {
319 goto err;
320 }
321 s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;
322 }
Adam Langley95c29f32014-06-20 12:00:00 -0700323
Adam Langleyfcf25832014-12-18 17:42:32 -0800324 s->verify_result = X509_V_OK;
325 s->method = ctx->method;
Adam Langley95c29f32014-06-20 12:00:00 -0700326
Adam Langleyfcf25832014-12-18 17:42:32 -0800327 if (!s->method->ssl_new(s)) {
328 goto err;
329 }
330 s->enc_method = ssl3_get_enc_method(s->version);
331 assert(s->enc_method != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700332
David Benjamin62fd1622015-01-11 13:30:01 -0500333 s->rwstate = SSL_NOTHING;
334 s->rstate = SSL_ST_READ_HEADER;
Adam Langley95c29f32014-06-20 12:00:00 -0700335
David Benjamin9f33fc62015-04-15 17:29:53 -0400336 CRYPTO_new_ex_data(&g_ex_data_class_ssl, s, &s->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700337
Adam Langleyfcf25832014-12-18 17:42:32 -0800338 s->psk_identity_hint = NULL;
339 if (ctx->psk_identity_hint) {
340 s->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
341 if (s->psk_identity_hint == NULL) {
342 goto err;
343 }
344 }
345 s->psk_client_callback = ctx->psk_client_callback;
346 s->psk_server_callback = ctx->psk_server_callback;
Adam Langley95c29f32014-06-20 12:00:00 -0700347
David Benjamin02ddbfd2015-01-11 13:09:11 -0500348 s->tlsext_channel_id_enabled = ctx->tlsext_channel_id_enabled;
349 if (ctx->tlsext_channel_id_private) {
David Benjamin9a10f8f2015-05-05 22:22:40 -0400350 s->tlsext_channel_id_private =
351 EVP_PKEY_up_ref(ctx->tlsext_channel_id_private);
David Benjamin02ddbfd2015-01-11 13:09:11 -0500352 }
353
Adam Langleyfcf25832014-12-18 17:42:32 -0800354 s->signed_cert_timestamps_enabled = s->ctx->signed_cert_timestamps_enabled;
355 s->ocsp_stapling_enabled = s->ctx->ocsp_stapling_enabled;
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200356
Adam Langleyfcf25832014-12-18 17:42:32 -0800357 return s;
358
Adam Langley95c29f32014-06-20 12:00:00 -0700359err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400360 SSL_free(s);
Adam Langleyfcf25832014-12-18 17:42:32 -0800361 OPENSSL_PUT_ERROR(SSL, SSL_new, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700362
Adam Langleyfcf25832014-12-18 17:42:32 -0800363 return NULL;
364}
Adam Langley95c29f32014-06-20 12:00:00 -0700365
Adam Langleyfcf25832014-12-18 17:42:32 -0800366int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
367 unsigned int sid_ctx_len) {
368 if (sid_ctx_len > sizeof ctx->sid_ctx) {
369 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_session_id_context,
370 SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
371 return 0;
372 }
373 ctx->sid_ctx_length = sid_ctx_len;
374 memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700375
Adam Langleyfcf25832014-12-18 17:42:32 -0800376 return 1;
377}
Adam Langley95c29f32014-06-20 12:00:00 -0700378
Adam Langleyfcf25832014-12-18 17:42:32 -0800379int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
380 unsigned int sid_ctx_len) {
381 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
382 OPENSSL_PUT_ERROR(SSL, SSL_set_session_id_context,
383 SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
384 return 0;
385 }
386 ssl->sid_ctx_length = sid_ctx_len;
387 memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700388
Adam Langleyfcf25832014-12-18 17:42:32 -0800389 return 1;
390}
Adam Langley95c29f32014-06-20 12:00:00 -0700391
Adam Langleyfcf25832014-12-18 17:42:32 -0800392int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800393 ctx->generate_session_id = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -0800394 return 1;
395}
Adam Langley95c29f32014-06-20 12:00:00 -0700396
Adam Langleyfcf25832014-12-18 17:42:32 -0800397int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800398 ssl->generate_session_id = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -0800399 return 1;
400}
Adam Langley95c29f32014-06-20 12:00:00 -0700401
Adam Langleyfcf25832014-12-18 17:42:32 -0800402int SSL_has_matching_session_id(const SSL *ssl, const uint8_t *id,
403 unsigned int id_len) {
404 /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how we
405 * can "construct" a session to give us the desired check - ie. to find if
406 * there's a session in the hash table that would conflict with any new
407 * session built out of this id/id_len and the ssl_version in use by this
408 * SSL. */
409 SSL_SESSION r, *p;
Adam Langley95c29f32014-06-20 12:00:00 -0700410
Adam Langleyfcf25832014-12-18 17:42:32 -0800411 if (id_len > sizeof r.session_id) {
412 return 0;
413 }
Adam Langley95c29f32014-06-20 12:00:00 -0700414
Adam Langleyfcf25832014-12-18 17:42:32 -0800415 r.ssl_version = ssl->version;
416 r.session_id_length = id_len;
417 memcpy(r.session_id, id, id_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700418
Adam Langley4bdb6e42015-05-15 15:29:21 -0700419 CRYPTO_MUTEX_lock_read(&ssl->ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -0800420 p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r);
Adam Langley4bdb6e42015-05-15 15:29:21 -0700421 CRYPTO_MUTEX_unlock(&ssl->ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -0800422 return p != NULL;
423}
Adam Langley95c29f32014-06-20 12:00:00 -0700424
Adam Langleyfcf25832014-12-18 17:42:32 -0800425int SSL_CTX_set_purpose(SSL_CTX *s, int purpose) {
426 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
427}
428
429int SSL_set_purpose(SSL *s, int purpose) {
430 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
431}
432
433int SSL_CTX_set_trust(SSL_CTX *s, int trust) {
434 return X509_VERIFY_PARAM_set_trust(s->param, trust);
435}
436
437int SSL_set_trust(SSL *s, int trust) {
438 return X509_VERIFY_PARAM_set_trust(s->param, trust);
439}
440
441int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm) {
442 return X509_VERIFY_PARAM_set1(ctx->param, vpm);
443}
444
445int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm) {
446 return X509_VERIFY_PARAM_set1(ssl->param, vpm);
447}
Adam Langley95c29f32014-06-20 12:00:00 -0700448
Adam Langley858a88d2014-06-20 12:00:00 -0700449void ssl_cipher_preference_list_free(
Adam Langleyfcf25832014-12-18 17:42:32 -0800450 struct ssl_cipher_preference_list_st *cipher_list) {
David Benjamin5d1ec732015-04-22 13:38:00 -0400451 if (cipher_list == NULL) {
452 return;
453 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800454 sk_SSL_CIPHER_free(cipher_list->ciphers);
455 OPENSSL_free(cipher_list->in_group_flags);
456 OPENSSL_free(cipher_list);
457}
Adam Langley858a88d2014-06-20 12:00:00 -0700458
Adam Langleyfcf25832014-12-18 17:42:32 -0800459struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_dup(
460 struct ssl_cipher_preference_list_st *cipher_list) {
461 struct ssl_cipher_preference_list_st *ret = NULL;
462 size_t n = sk_SSL_CIPHER_num(cipher_list->ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700463
Adam Langleyfcf25832014-12-18 17:42:32 -0800464 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
465 if (!ret) {
466 goto err;
467 }
468
469 ret->ciphers = NULL;
470 ret->in_group_flags = NULL;
471 ret->ciphers = sk_SSL_CIPHER_dup(cipher_list->ciphers);
472 if (!ret->ciphers) {
473 goto err;
474 }
475 ret->in_group_flags = BUF_memdup(cipher_list->in_group_flags, n);
476 if (!ret->in_group_flags) {
477 goto err;
478 }
479
480 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -0700481
482err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400483 ssl_cipher_preference_list_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -0800484 return NULL;
485}
Adam Langley858a88d2014-06-20 12:00:00 -0700486
Adam Langleyfcf25832014-12-18 17:42:32 -0800487struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_from_ciphers(
David Benjamin60da0cd2015-05-03 15:21:28 -0400488 STACK_OF(SSL_CIPHER) *ciphers) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800489 struct ssl_cipher_preference_list_st *ret = NULL;
490 size_t n = sk_SSL_CIPHER_num(ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700491
Adam Langleyfcf25832014-12-18 17:42:32 -0800492 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
493 if (!ret) {
494 goto err;
495 }
496 ret->ciphers = NULL;
497 ret->in_group_flags = NULL;
498 ret->ciphers = sk_SSL_CIPHER_dup(ciphers);
499 if (!ret->ciphers) {
500 goto err;
501 }
502 ret->in_group_flags = OPENSSL_malloc(n);
503 if (!ret->in_group_flags) {
504 goto err;
505 }
506 memset(ret->in_group_flags, 0, n);
507 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -0700508
509err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400510 ssl_cipher_preference_list_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -0800511 return NULL;
512}
Adam Langley858a88d2014-06-20 12:00:00 -0700513
Adam Langleyfcf25832014-12-18 17:42:32 -0800514X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) { return ctx->param; }
Adam Langley95c29f32014-06-20 12:00:00 -0700515
Adam Langleyfcf25832014-12-18 17:42:32 -0800516X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) { return ssl->param; }
Adam Langley95c29f32014-06-20 12:00:00 -0700517
Adam Langleyfcf25832014-12-18 17:42:32 -0800518void SSL_certs_clear(SSL *s) { ssl_cert_clear_certs(s->cert); }
Adam Langley95c29f32014-06-20 12:00:00 -0700519
David Benjamin4831c332015-05-16 11:43:13 -0400520void SSL_free(SSL *ssl) {
521 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800522 return;
523 }
Adam Langley95c29f32014-06-20 12:00:00 -0700524
David Benjamin4831c332015-05-16 11:43:13 -0400525 X509_VERIFY_PARAM_free(ssl->param);
Adam Langley95c29f32014-06-20 12:00:00 -0700526
David Benjamin4831c332015-05-16 11:43:13 -0400527 CRYPTO_free_ex_data(&g_ex_data_class_ssl, ssl, &ssl->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700528
David Benjamin4831c332015-05-16 11:43:13 -0400529 if (ssl->bbio != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800530 /* If the buffering BIO is in place, pop it off */
David Benjamin4831c332015-05-16 11:43:13 -0400531 if (ssl->bbio == ssl->wbio) {
532 ssl->wbio = BIO_pop(ssl->wbio);
Adam Langleyfcf25832014-12-18 17:42:32 -0800533 }
David Benjamin4831c332015-05-16 11:43:13 -0400534 BIO_free(ssl->bbio);
535 ssl->bbio = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800536 }
Adam Langley95c29f32014-06-20 12:00:00 -0700537
David Benjamin4831c332015-05-16 11:43:13 -0400538 int free_wbio = ssl->wbio != ssl->rbio;
539 BIO_free_all(ssl->rbio);
David Benjamin2755a3e2015-04-22 16:17:58 -0400540 if (free_wbio) {
David Benjamin4831c332015-05-16 11:43:13 -0400541 BIO_free_all(ssl->wbio);
Adam Langleyfcf25832014-12-18 17:42:32 -0800542 }
Adam Langley95c29f32014-06-20 12:00:00 -0700543
David Benjamin4831c332015-05-16 11:43:13 -0400544 BUF_MEM_free(ssl->init_buf);
Adam Langley95c29f32014-06-20 12:00:00 -0700545
Adam Langleyfcf25832014-12-18 17:42:32 -0800546 /* add extra stuff */
David Benjamin4831c332015-05-16 11:43:13 -0400547 ssl_cipher_preference_list_free(ssl->cipher_list);
548 sk_SSL_CIPHER_free(ssl->cipher_list_by_id);
Adam Langley95c29f32014-06-20 12:00:00 -0700549
David Benjamin4831c332015-05-16 11:43:13 -0400550 ssl_clear_bad_session(ssl);
551 SSL_SESSION_free(ssl->session);
Adam Langley95c29f32014-06-20 12:00:00 -0700552
David Benjamin4831c332015-05-16 11:43:13 -0400553 ssl_clear_cipher_ctx(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700554
David Benjamin4831c332015-05-16 11:43:13 -0400555 ssl_cert_free(ssl->cert);
Adam Langley0289c732014-06-20 12:00:00 -0700556
David Benjamin4831c332015-05-16 11:43:13 -0400557 OPENSSL_free(ssl->tlsext_hostname);
558 SSL_CTX_free(ssl->initial_ctx);
559 OPENSSL_free(ssl->tlsext_ecpointformatlist);
560 OPENSSL_free(ssl->tlsext_ellipticcurvelist);
561 OPENSSL_free(ssl->alpn_client_proto_list);
562 EVP_PKEY_free(ssl->tlsext_channel_id_private);
563 OPENSSL_free(ssl->psk_identity_hint);
564 sk_X509_NAME_pop_free(ssl->client_CA, X509_NAME_free);
565 OPENSSL_free(ssl->next_proto_negotiated);
566 sk_SRTP_PROTECTION_PROFILE_free(ssl->srtp_profiles);
Adam Langley95c29f32014-06-20 12:00:00 -0700567
David Benjamin4831c332015-05-16 11:43:13 -0400568 if (ssl->method != NULL) {
569 ssl->method->ssl_free(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800570 }
David Benjamin4831c332015-05-16 11:43:13 -0400571 SSL_CTX_free(ssl->ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700572
David Benjamin4831c332015-05-16 11:43:13 -0400573 OPENSSL_free(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800574}
Adam Langley95c29f32014-06-20 12:00:00 -0700575
Adam Langleyfcf25832014-12-18 17:42:32 -0800576void SSL_set_bio(SSL *s, BIO *rbio, BIO *wbio) {
577 /* If the output buffering BIO is still in place, remove it. */
578 if (s->bbio != NULL) {
579 if (s->wbio == s->bbio) {
580 s->wbio = s->wbio->next_bio;
581 s->bbio->next_bio = NULL;
582 }
583 }
Adam Langley95c29f32014-06-20 12:00:00 -0700584
David Benjamin2755a3e2015-04-22 16:17:58 -0400585 if (s->rbio != rbio) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800586 BIO_free_all(s->rbio);
587 }
David Benjamin2755a3e2015-04-22 16:17:58 -0400588 if (s->wbio != wbio && s->rbio != s->wbio) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800589 BIO_free_all(s->wbio);
590 }
591 s->rbio = rbio;
592 s->wbio = wbio;
593}
Adam Langley95c29f32014-06-20 12:00:00 -0700594
Adam Langleyfcf25832014-12-18 17:42:32 -0800595BIO *SSL_get_rbio(const SSL *s) { return s->rbio; }
Adam Langley95c29f32014-06-20 12:00:00 -0700596
Adam Langleyfcf25832014-12-18 17:42:32 -0800597BIO *SSL_get_wbio(const SSL *s) { return s->wbio; }
Adam Langley95c29f32014-06-20 12:00:00 -0700598
Adam Langleyfcf25832014-12-18 17:42:32 -0800599int SSL_get_fd(const SSL *s) { return SSL_get_rfd(s); }
Adam Langley95c29f32014-06-20 12:00:00 -0700600
Adam Langleyfcf25832014-12-18 17:42:32 -0800601int SSL_get_rfd(const SSL *s) {
602 int ret = -1;
603 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700604
Adam Langleyfcf25832014-12-18 17:42:32 -0800605 b = SSL_get_rbio(s);
606 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
607 if (r != NULL) {
608 BIO_get_fd(r, &ret);
609 }
610 return ret;
611}
Adam Langley95c29f32014-06-20 12:00:00 -0700612
Adam Langleyfcf25832014-12-18 17:42:32 -0800613int SSL_get_wfd(const SSL *s) {
614 int ret = -1;
615 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700616
Adam Langleyfcf25832014-12-18 17:42:32 -0800617 b = SSL_get_wbio(s);
618 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
619 if (r != NULL) {
620 BIO_get_fd(r, &ret);
621 }
Adam Langley95c29f32014-06-20 12:00:00 -0700622
Adam Langleyfcf25832014-12-18 17:42:32 -0800623 return ret;
624}
Adam Langley95c29f32014-06-20 12:00:00 -0700625
Adam Langleyfcf25832014-12-18 17:42:32 -0800626int SSL_set_fd(SSL *s, int fd) {
627 int ret = 0;
628 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700629
Adam Langleyfcf25832014-12-18 17:42:32 -0800630 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700631
Adam Langleyfcf25832014-12-18 17:42:32 -0800632 if (bio == NULL) {
633 OPENSSL_PUT_ERROR(SSL, SSL_set_fd, ERR_R_BUF_LIB);
634 goto err;
635 }
636 BIO_set_fd(bio, fd, BIO_NOCLOSE);
637 SSL_set_bio(s, bio, bio);
638 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700639
Adam Langley95c29f32014-06-20 12:00:00 -0700640err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800641 return ret;
642}
Adam Langley95c29f32014-06-20 12:00:00 -0700643
Adam Langleyfcf25832014-12-18 17:42:32 -0800644int SSL_set_wfd(SSL *s, int fd) {
645 int ret = 0;
646 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700647
Adam Langleyfcf25832014-12-18 17:42:32 -0800648 if (s->rbio == NULL || BIO_method_type(s->rbio) != BIO_TYPE_FD ||
649 (int)BIO_get_fd(s->rbio, NULL) != fd) {
650 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700651
Adam Langleyfcf25832014-12-18 17:42:32 -0800652 if (bio == NULL) {
653 OPENSSL_PUT_ERROR(SSL, SSL_set_wfd, ERR_R_BUF_LIB);
654 goto err;
655 }
656 BIO_set_fd(bio, fd, BIO_NOCLOSE);
657 SSL_set_bio(s, SSL_get_rbio(s), bio);
658 } else {
659 SSL_set_bio(s, SSL_get_rbio(s), SSL_get_rbio(s));
660 }
661
662 ret = 1;
663
Adam Langley95c29f32014-06-20 12:00:00 -0700664err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800665 return ret;
666}
Adam Langley95c29f32014-06-20 12:00:00 -0700667
Adam Langleyfcf25832014-12-18 17:42:32 -0800668int SSL_set_rfd(SSL *s, int fd) {
669 int ret = 0;
670 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700671
Adam Langleyfcf25832014-12-18 17:42:32 -0800672 if (s->wbio == NULL || BIO_method_type(s->wbio) != BIO_TYPE_FD ||
673 (int)BIO_get_fd(s->wbio, NULL) != fd) {
674 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700675
Adam Langleyfcf25832014-12-18 17:42:32 -0800676 if (bio == NULL) {
677 OPENSSL_PUT_ERROR(SSL, SSL_set_rfd, ERR_R_BUF_LIB);
678 goto err;
679 }
680 BIO_set_fd(bio, fd, BIO_NOCLOSE);
681 SSL_set_bio(s, bio, SSL_get_wbio(s));
682 } else {
683 SSL_set_bio(s, SSL_get_wbio(s), SSL_get_wbio(s));
684 }
685 ret = 1;
686
Adam Langley95c29f32014-06-20 12:00:00 -0700687err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800688 return ret;
689}
Adam Langley95c29f32014-06-20 12:00:00 -0700690
691/* return length of latest Finished message we sent, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -0800692size_t SSL_get_finished(const SSL *s, void *buf, size_t count) {
693 size_t ret = 0;
694
695 if (s->s3 != NULL) {
696 ret = s->s3->tmp.finish_md_len;
697 if (count > ret) {
698 count = ret;
699 }
700 memcpy(buf, s->s3->tmp.finish_md, count);
701 }
702
703 return ret;
704}
Adam Langley95c29f32014-06-20 12:00:00 -0700705
706/* return length of latest Finished message we expected, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -0800707size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count) {
708 size_t ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700709
Adam Langleyfcf25832014-12-18 17:42:32 -0800710 if (s->s3 != NULL) {
711 ret = s->s3->tmp.peer_finish_md_len;
712 if (count > ret) {
713 count = ret;
714 }
715 memcpy(buf, s->s3->tmp.peer_finish_md, count);
716 }
Adam Langley95c29f32014-06-20 12:00:00 -0700717
Adam Langleyfcf25832014-12-18 17:42:32 -0800718 return ret;
719}
Adam Langley95c29f32014-06-20 12:00:00 -0700720
Adam Langleyfcf25832014-12-18 17:42:32 -0800721int SSL_get_verify_mode(const SSL *s) { return s->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -0700722
Adam Langleyfcf25832014-12-18 17:42:32 -0800723int SSL_get_verify_depth(const SSL *s) {
724 return X509_VERIFY_PARAM_get_depth(s->param);
725}
Adam Langley95c29f32014-06-20 12:00:00 -0700726
Adam Langleyfcf25832014-12-18 17:42:32 -0800727int (*SSL_get_verify_callback(const SSL *s))(int, X509_STORE_CTX *) {
728 return s->verify_callback;
729}
Adam Langley95c29f32014-06-20 12:00:00 -0700730
Adam Langleyfcf25832014-12-18 17:42:32 -0800731int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) { return ctx->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -0700732
Adam Langleyfcf25832014-12-18 17:42:32 -0800733int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) {
734 return X509_VERIFY_PARAM_get_depth(ctx->param);
735}
Adam Langley95c29f32014-06-20 12:00:00 -0700736
Adam Langleyfcf25832014-12-18 17:42:32 -0800737int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int, X509_STORE_CTX *) {
738 return ctx->default_verify_callback;
739}
Adam Langley95c29f32014-06-20 12:00:00 -0700740
Adam Langleyfcf25832014-12-18 17:42:32 -0800741void SSL_set_verify(SSL *s, int mode,
742 int (*callback)(int ok, X509_STORE_CTX *ctx)) {
743 s->verify_mode = mode;
744 if (callback != NULL) {
745 s->verify_callback = callback;
746 }
747}
Adam Langley95c29f32014-06-20 12:00:00 -0700748
Adam Langleyfcf25832014-12-18 17:42:32 -0800749void SSL_set_verify_depth(SSL *s, int depth) {
750 X509_VERIFY_PARAM_set_depth(s->param, depth);
751}
Adam Langley95c29f32014-06-20 12:00:00 -0700752
David Benjamin9a41d1b2015-05-16 01:30:09 -0400753int SSL_CTX_get_read_ahead(const SSL_CTX *ctx) { return 0; }
Adam Langley95c29f32014-06-20 12:00:00 -0700754
David Benjamin9a41d1b2015-05-16 01:30:09 -0400755int SSL_get_read_ahead(const SSL *s) { return 0; }
Adam Langley95c29f32014-06-20 12:00:00 -0700756
David Benjamin9a41d1b2015-05-16 01:30:09 -0400757void SSL_CTX_set_read_ahead(SSL_CTX *ctx, int yes) { }
David Benjamin61ecccf2015-05-05 09:44:51 -0400758
David Benjamin9a41d1b2015-05-16 01:30:09 -0400759void SSL_set_read_ahead(SSL *s, int yes) { }
David Benjamin61ecccf2015-05-05 09:44:51 -0400760
Adam Langleyfcf25832014-12-18 17:42:32 -0800761int SSL_pending(const SSL *s) {
David Benjamin904dc722015-05-30 16:39:07 -0400762 if (s->rstate == SSL_ST_READ_BODY) {
763 return 0;
764 }
765
766 return (s->s3->rrec.type == SSL3_RT_APPLICATION_DATA) ? s->s3->rrec.length
767 : 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800768}
Adam Langley95c29f32014-06-20 12:00:00 -0700769
Adam Langleyfcf25832014-12-18 17:42:32 -0800770X509 *SSL_get_peer_certificate(const SSL *s) {
771 X509 *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700772
Adam Langleyfcf25832014-12-18 17:42:32 -0800773 if (s == NULL || s->session == NULL) {
774 r = NULL;
775 } else {
776 r = s->session->peer;
777 }
Adam Langley95c29f32014-06-20 12:00:00 -0700778
Adam Langleyfcf25832014-12-18 17:42:32 -0800779 if (r == NULL) {
780 return NULL;
781 }
Adam Langley95c29f32014-06-20 12:00:00 -0700782
Adam Langleyfcf25832014-12-18 17:42:32 -0800783 return X509_up_ref(r);
784}
785
David Benjamin60da0cd2015-05-03 15:21:28 -0400786STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s) {
787 STACK_OF(X509) *r;
Adam Langleyfcf25832014-12-18 17:42:32 -0800788
789 if (s == NULL || s->session == NULL || s->session->sess_cert == NULL) {
790 r = NULL;
791 } else {
792 r = s->session->sess_cert->cert_chain;
793 }
794
795 /* If we are a client, cert_chain includes the peer's own certificate; if we
796 * are a server, it does not. */
797 return r;
798}
Adam Langley95c29f32014-06-20 12:00:00 -0700799
Adam Langley95c29f32014-06-20 12:00:00 -0700800/* Fix this so it checks all the valid key/cert options */
Adam Langleyfcf25832014-12-18 17:42:32 -0800801int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
802 if (ctx == NULL || ctx->cert == NULL || ctx->cert->key->x509 == NULL) {
803 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key,
804 SSL_R_NO_CERTIFICATE_ASSIGNED);
805 return 0;
806 }
807
808 if (ctx->cert->key->privatekey == NULL) {
809 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key,
810 SSL_R_NO_PRIVATE_KEY_ASSIGNED);
811 return 0;
812 }
813
814 return X509_check_private_key(ctx->cert->key->x509,
815 ctx->cert->key->privatekey);
816}
Adam Langley95c29f32014-06-20 12:00:00 -0700817
818/* Fix this function so that it takes an optional type parameter */
Adam Langleyfcf25832014-12-18 17:42:32 -0800819int SSL_check_private_key(const SSL *ssl) {
820 if (ssl == NULL) {
821 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, ERR_R_PASSED_NULL_PARAMETER);
822 return 0;
823 }
Adam Langley95c29f32014-06-20 12:00:00 -0700824
Adam Langleyfcf25832014-12-18 17:42:32 -0800825 if (ssl->cert == NULL) {
826 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
827 SSL_R_NO_CERTIFICATE_ASSIGNED);
828 return 0;
829 }
Adam Langley95c29f32014-06-20 12:00:00 -0700830
Adam Langleyfcf25832014-12-18 17:42:32 -0800831 if (ssl->cert->key->x509 == NULL) {
832 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
833 SSL_R_NO_CERTIFICATE_ASSIGNED);
834 return 0;
835 }
David Benjamin0b145c22014-11-26 20:10:09 -0500836
Adam Langleyfcf25832014-12-18 17:42:32 -0800837 if (ssl->cert->key->privatekey == NULL) {
838 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
839 SSL_R_NO_PRIVATE_KEY_ASSIGNED);
840 return 0;
841 }
Adam Langley95c29f32014-06-20 12:00:00 -0700842
Adam Langleyfcf25832014-12-18 17:42:32 -0800843 return X509_check_private_key(ssl->cert->key->x509,
844 ssl->cert->key->privatekey);
845}
Adam Langley95c29f32014-06-20 12:00:00 -0700846
Adam Langleyfcf25832014-12-18 17:42:32 -0800847int SSL_accept(SSL *s) {
848 if (s->handshake_func == 0) {
849 /* Not properly initialized yet */
850 SSL_set_accept_state(s);
851 }
David Benjamin0b145c22014-11-26 20:10:09 -0500852
Adam Langleyfcf25832014-12-18 17:42:32 -0800853 if (s->handshake_func != s->method->ssl_accept) {
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +0100854 OPENSSL_PUT_ERROR(SSL, SSL_accept, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800855 return -1;
856 }
Adam Langley95c29f32014-06-20 12:00:00 -0700857
Adam Langleyfcf25832014-12-18 17:42:32 -0800858 return s->handshake_func(s);
859}
Adam Langley95c29f32014-06-20 12:00:00 -0700860
Adam Langleyfcf25832014-12-18 17:42:32 -0800861int SSL_connect(SSL *s) {
862 if (s->handshake_func == 0) {
863 /* Not properly initialized yet */
864 SSL_set_connect_state(s);
865 }
Adam Langley95c29f32014-06-20 12:00:00 -0700866
Adam Langleyfcf25832014-12-18 17:42:32 -0800867 if (s->handshake_func != s->method->ssl_connect) {
868 OPENSSL_PUT_ERROR(SSL, SSL_connect, ERR_R_INTERNAL_ERROR);
869 return -1;
870 }
Adam Langley95c29f32014-06-20 12:00:00 -0700871
Adam Langleyfcf25832014-12-18 17:42:32 -0800872 return s->handshake_func(s);
873}
Adam Langley95c29f32014-06-20 12:00:00 -0700874
Adam Langleyfcf25832014-12-18 17:42:32 -0800875long SSL_get_default_timeout(const SSL *s) {
876 return SSL_DEFAULT_SESSION_TIMEOUT;
877}
Adam Langley95c29f32014-06-20 12:00:00 -0700878
Adam Langleyfcf25832014-12-18 17:42:32 -0800879int SSL_read(SSL *s, void *buf, int num) {
880 if (s->handshake_func == 0) {
881 OPENSSL_PUT_ERROR(SSL, SSL_read, SSL_R_UNINITIALIZED);
882 return -1;
883 }
Adam Langley95c29f32014-06-20 12:00:00 -0700884
Adam Langleyfcf25832014-12-18 17:42:32 -0800885 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
886 s->rwstate = SSL_NOTHING;
887 return 0;
888 }
Adam Langley95c29f32014-06-20 12:00:00 -0700889
David Benjamin904dc722015-05-30 16:39:07 -0400890 ERR_clear_system_error();
891 return s->method->ssl_read_app_data(s, buf, num, 0);
Adam Langleyfcf25832014-12-18 17:42:32 -0800892}
Adam Langley95c29f32014-06-20 12:00:00 -0700893
Adam Langleyfcf25832014-12-18 17:42:32 -0800894int SSL_peek(SSL *s, void *buf, int num) {
895 if (s->handshake_func == 0) {
896 OPENSSL_PUT_ERROR(SSL, SSL_peek, SSL_R_UNINITIALIZED);
897 return -1;
898 }
Adam Langley95c29f32014-06-20 12:00:00 -0700899
Adam Langleyfcf25832014-12-18 17:42:32 -0800900 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
901 return 0;
902 }
Adam Langley95c29f32014-06-20 12:00:00 -0700903
David Benjamin904dc722015-05-30 16:39:07 -0400904 ERR_clear_system_error();
905 return s->method->ssl_read_app_data(s, buf, num, 1);
Adam Langleyfcf25832014-12-18 17:42:32 -0800906}
Adam Langley95c29f32014-06-20 12:00:00 -0700907
Adam Langleyfcf25832014-12-18 17:42:32 -0800908int SSL_write(SSL *s, const void *buf, int num) {
909 if (s->handshake_func == 0) {
910 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_UNINITIALIZED);
911 return -1;
912 }
Adam Langley95c29f32014-06-20 12:00:00 -0700913
Adam Langleyfcf25832014-12-18 17:42:32 -0800914 if (s->shutdown & SSL_SENT_SHUTDOWN) {
915 s->rwstate = SSL_NOTHING;
916 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_PROTOCOL_IS_SHUTDOWN);
917 return -1;
918 }
Adam Langley95c29f32014-06-20 12:00:00 -0700919
David Benjamin904dc722015-05-30 16:39:07 -0400920 ERR_clear_system_error();
921 return s->method->ssl_write_app_data(s, buf, num);
Adam Langleyfcf25832014-12-18 17:42:32 -0800922}
Adam Langley95c29f32014-06-20 12:00:00 -0700923
Adam Langleyfcf25832014-12-18 17:42:32 -0800924int SSL_shutdown(SSL *s) {
925 /* Note that this function behaves differently from what one might expect.
926 * Return values are 0 for no success (yet), 1 for success; but calling it
927 * once is usually not enough, even if blocking I/O is used (see
928 * ssl3_shutdown). */
Adam Langley95c29f32014-06-20 12:00:00 -0700929
Adam Langleyfcf25832014-12-18 17:42:32 -0800930 if (s->handshake_func == 0) {
931 OPENSSL_PUT_ERROR(SSL, SSL_shutdown, SSL_R_UNINITIALIZED);
932 return -1;
933 }
Adam Langley95c29f32014-06-20 12:00:00 -0700934
David Benjamin904dc722015-05-30 16:39:07 -0400935 if (SSL_in_init(s)) {
936 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800937 }
Adam Langley95c29f32014-06-20 12:00:00 -0700938
David Benjamin904dc722015-05-30 16:39:07 -0400939 /* Do nothing if configured not to send a close_notify. */
940 if (s->quiet_shutdown) {
941 s->shutdown = SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN;
942 return 1;
943 }
944
945 if (!(s->shutdown & SSL_SENT_SHUTDOWN)) {
946 s->shutdown |= SSL_SENT_SHUTDOWN;
947 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_CLOSE_NOTIFY);
948
949 /* our shutdown alert has been sent now, and if it still needs to be
950 * written, s->s3->alert_dispatch will be true */
951 if (s->s3->alert_dispatch) {
952 return -1; /* return WANT_WRITE */
953 }
954 } else if (s->s3->alert_dispatch) {
955 /* resend it if not sent */
956 int ret = s->method->ssl_dispatch_alert(s);
957 if (ret == -1) {
958 /* we only get to return -1 here the 2nd/Nth invocation, we must have
959 * already signalled return 0 upon a previous invoation, return
960 * WANT_WRITE */
961 return ret;
962 }
963 } else if (!(s->shutdown & SSL_RECEIVED_SHUTDOWN)) {
964 /* If we are waiting for a close from our peer, we are closed */
965 s->method->ssl_read_close_notify(s);
966 if (!(s->shutdown & SSL_RECEIVED_SHUTDOWN)) {
967 return -1; /* return WANT_READ */
968 }
969 }
970
971 if (s->shutdown == (SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN) &&
972 !s->s3->alert_dispatch) {
973 return 1;
974 } else {
975 return 0;
976 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800977}
Adam Langley95c29f32014-06-20 12:00:00 -0700978
David Benjamin44d3eed2015-05-21 01:29:55 -0400979int SSL_renegotiate(SSL *ssl) {
980 /* Caller-initiated renegotiation is not supported. */
981 OPENSSL_PUT_ERROR(SSL, SSL_renegotiate, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
982 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800983}
Adam Langley95c29f32014-06-20 12:00:00 -0700984
David Benjamin44d3eed2015-05-21 01:29:55 -0400985int SSL_renegotiate_pending(SSL *ssl) {
986 return SSL_in_init(ssl) && ssl->s3->initial_handshake_complete;
Adam Langleyfcf25832014-12-18 17:42:32 -0800987}
Adam Langley95c29f32014-06-20 12:00:00 -0700988
David Benjamin61ecccf2015-05-05 09:44:51 -0400989uint32_t SSL_CTX_set_options(SSL_CTX *ctx, uint32_t options) {
990 ctx->options |= options;
991 return ctx->options;
992}
993
994uint32_t SSL_set_options(SSL *ssl, uint32_t options) {
995 ssl->options |= options;
996 return ssl->options;
997}
998
999uint32_t SSL_CTX_clear_options(SSL_CTX *ctx, uint32_t options) {
1000 ctx->options &= ~options;
1001 return ctx->options;
1002}
1003
1004uint32_t SSL_clear_options(SSL *ssl, uint32_t options) {
1005 ssl->options &= ~options;
1006 return ssl->options;
1007}
1008
1009uint32_t SSL_CTX_get_options(const SSL_CTX *ctx) { return ctx->options; }
1010
1011uint32_t SSL_get_options(const SSL *ssl) { return ssl->options; }
1012
1013uint32_t SSL_CTX_set_mode(SSL_CTX *ctx, uint32_t mode) {
1014 ctx->mode |= mode;
1015 return ctx->mode;
1016}
1017
1018uint32_t SSL_set_mode(SSL *ssl, uint32_t mode) {
1019 ssl->mode |= mode;
1020 return ssl->mode;
1021}
1022
1023uint32_t SSL_CTX_clear_mode(SSL_CTX *ctx, uint32_t mode) {
1024 ctx->mode &= ~mode;
1025 return ctx->mode;
1026}
1027
1028uint32_t SSL_clear_mode(SSL *ssl, uint32_t mode) {
1029 ssl->mode &= ~mode;
1030 return ssl->mode;
1031}
1032
1033uint32_t SSL_CTX_get_mode(const SSL_CTX *ctx) { return ctx->mode; }
1034
1035uint32_t SSL_get_mode(const SSL *ssl) { return ssl->mode; }
1036
David Benjamin1d0a1942015-04-26 15:35:35 -04001037size_t SSL_CTX_get_max_cert_list(const SSL_CTX *ctx) {
1038 return ctx->max_cert_list;
1039}
1040
1041void SSL_CTX_set_max_cert_list(SSL_CTX *ctx, size_t max_cert_list) {
1042 if (max_cert_list > kMaxHandshakeSize) {
1043 max_cert_list = kMaxHandshakeSize;
1044 }
1045 ctx->max_cert_list = (uint32_t)max_cert_list;
1046}
1047
1048size_t SSL_get_max_cert_list(const SSL *ssl) {
1049 return ssl->max_cert_list;
1050}
1051
1052void SSL_set_max_cert_list(SSL *ssl, size_t max_cert_list) {
1053 if (max_cert_list > kMaxHandshakeSize) {
1054 max_cert_list = kMaxHandshakeSize;
1055 }
1056 ssl->max_cert_list = (uint32_t)max_cert_list;
1057}
1058
1059void SSL_CTX_set_max_send_fragment(SSL_CTX *ctx, size_t max_send_fragment) {
1060 if (max_send_fragment < 512) {
1061 max_send_fragment = 512;
1062 }
1063 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1064 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1065 }
1066 ctx->max_send_fragment = (uint16_t)max_send_fragment;
1067}
1068
1069void SSL_set_max_send_fragment(SSL *ssl, size_t max_send_fragment) {
1070 if (max_send_fragment < 512) {
1071 max_send_fragment = 512;
1072 }
1073 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1074 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1075 }
1076 ssl->max_send_fragment = (uint16_t)max_send_fragment;
1077}
1078
David Benjamincb9cf792015-05-05 09:46:14 -04001079int SSL_set_mtu(SSL *ssl, unsigned mtu) {
1080 if (!SSL_IS_DTLS(ssl) || mtu < dtls1_min_mtu()) {
1081 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001082 }
David Benjamincb9cf792015-05-05 09:46:14 -04001083 ssl->d1->mtu = mtu;
1084 return 1;
1085}
1086
1087int SSL_get_secure_renegotiation_support(const SSL *ssl) {
1088 return ssl->s3->send_connection_binding;
1089}
1090
1091long SSL_ctrl(SSL *s, int cmd, long larg, void *parg) {
1092 return s->method->ssl_ctrl(s, cmd, larg, parg);
Adam Langleyfcf25832014-12-18 17:42:32 -08001093}
1094
Adam Langleyfcf25832014-12-18 17:42:32 -08001095LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) { return ctx->sessions; }
1096
David Benjamin71f7d3d2015-05-05 09:46:38 -04001097size_t SSL_CTX_sess_number(const SSL_CTX *ctx) {
1098 return lh_SSL_SESSION_num_items(ctx->sessions);
1099}
1100
1101unsigned long SSL_CTX_sess_set_cache_size(SSL_CTX *ctx, unsigned long size) {
1102 unsigned long ret = ctx->session_cache_size;
1103 ctx->session_cache_size = size;
1104 return ret;
1105}
1106
1107unsigned long SSL_CTX_sess_get_cache_size(const SSL_CTX *ctx) {
1108 return ctx->session_cache_size;
1109}
1110
1111int SSL_CTX_set_session_cache_mode(SSL_CTX *ctx, int mode) {
1112 int ret = ctx->session_cache_mode;
1113 ctx->session_cache_mode = mode;
1114 return ret;
1115}
1116
1117int SSL_CTX_get_session_cache_mode(const SSL_CTX *ctx) {
1118 return ctx->session_cache_mode;
1119}
1120
Adam Langleyfcf25832014-12-18 17:42:32 -08001121long SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg) {
David Benjamin71f7d3d2015-05-05 09:46:38 -04001122 return ctx->method->ssl_ctx_ctrl(ctx, cmd, larg, parg);
Adam Langleyfcf25832014-12-18 17:42:32 -08001123}
1124
Adam Langleyfcf25832014-12-18 17:42:32 -08001125int ssl_cipher_id_cmp(const void *in_a, const void *in_b) {
1126 long l;
1127 const SSL_CIPHER *a = in_a;
1128 const SSL_CIPHER *b = in_b;
1129 const long a_id = a->id;
1130 const long b_id = b->id;
1131
1132 l = a_id - b_id;
1133 if (l == 0L) {
1134 return 0;
1135 } else {
1136 return (l > 0) ? 1 : -1;
1137 }
1138}
1139
1140int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **ap, const SSL_CIPHER **bp) {
1141 long l;
1142 const long a_id = (*ap)->id;
1143 const long b_id = (*bp)->id;
1144
1145 l = a_id - b_id;
1146 if (l == 0) {
1147 return 0;
1148 } else {
1149 return (l > 0) ? 1 : -1;
1150 }
1151}
1152
1153/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001154 * preference */
David Benjamin60da0cd2015-05-03 15:21:28 -04001155STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001156 if (s == NULL) {
1157 return NULL;
1158 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001159
Adam Langleyfcf25832014-12-18 17:42:32 -08001160 if (s->cipher_list != NULL) {
1161 return s->cipher_list->ciphers;
1162 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001163
Adam Langleyfcf25832014-12-18 17:42:32 -08001164 if (s->version >= TLS1_1_VERSION && s->ctx != NULL &&
1165 s->ctx->cipher_list_tls11 != NULL) {
1166 return s->ctx->cipher_list_tls11->ciphers;
1167 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001168
Adam Langleyfcf25832014-12-18 17:42:32 -08001169 if (s->ctx != NULL && s->ctx->cipher_list != NULL) {
1170 return s->ctx->cipher_list->ciphers;
1171 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001172
Adam Langleyfcf25832014-12-18 17:42:32 -08001173 return NULL;
1174}
Adam Langley95c29f32014-06-20 12:00:00 -07001175
Adam Langleyfcf25832014-12-18 17:42:32 -08001176/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001177 * algorithm id */
David Benjamin60da0cd2015-05-03 15:21:28 -04001178STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001179 if (s == NULL) {
1180 return NULL;
1181 }
Adam Langley95c29f32014-06-20 12:00:00 -07001182
Adam Langleyfcf25832014-12-18 17:42:32 -08001183 if (s->cipher_list_by_id != NULL) {
1184 return s->cipher_list_by_id;
1185 }
Adam Langley95c29f32014-06-20 12:00:00 -07001186
Adam Langleyfcf25832014-12-18 17:42:32 -08001187 if (s->ctx != NULL && s->ctx->cipher_list_by_id != NULL) {
1188 return s->ctx->cipher_list_by_id;
1189 }
Adam Langley95c29f32014-06-20 12:00:00 -07001190
Adam Langleyfcf25832014-12-18 17:42:32 -08001191 return NULL;
1192}
Adam Langley95c29f32014-06-20 12:00:00 -07001193
Adam Langleyfcf25832014-12-18 17:42:32 -08001194/* The old interface to get the same thing as SSL_get_ciphers() */
1195const char *SSL_get_cipher_list(const SSL *s, int n) {
1196 const SSL_CIPHER *c;
David Benjamin60da0cd2015-05-03 15:21:28 -04001197 STACK_OF(SSL_CIPHER) *sk;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001198
Adam Langleyfcf25832014-12-18 17:42:32 -08001199 if (s == NULL) {
1200 return NULL;
1201 }
Adam Langley95c29f32014-06-20 12:00:00 -07001202
Adam Langleyfcf25832014-12-18 17:42:32 -08001203 sk = SSL_get_ciphers(s);
1204 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
1205 return NULL;
1206 }
Adam Langley95c29f32014-06-20 12:00:00 -07001207
Adam Langleyfcf25832014-12-18 17:42:32 -08001208 c = sk_SSL_CIPHER_value(sk, n);
1209 if (c == NULL) {
1210 return NULL;
1211 }
Adam Langley95c29f32014-06-20 12:00:00 -07001212
Adam Langleyfcf25832014-12-18 17:42:32 -08001213 return c->name;
1214}
David Benjamin5491e3f2014-09-29 19:33:09 -04001215
Adam Langleyfcf25832014-12-18 17:42:32 -08001216/* specify the ciphers to be used by default by the SSL_CTX */
1217int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
1218 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001219
Adam Langleyfcf25832014-12-18 17:42:32 -08001220 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001221 &ctx->cipher_list_by_id, str);
Adam Langleyfcf25832014-12-18 17:42:32 -08001222 /* ssl_create_cipher_list may return an empty stack if it was unable to find
1223 * a cipher matching the given rule string (for example if the rule string
1224 * specifies a cipher which has been disabled). This is not an error as far
1225 * as ssl_create_cipher_list is concerned, and hence ctx->cipher_list and
1226 * ctx->cipher_list_by_id has been updated. */
1227 if (sk == NULL) {
1228 return 0;
1229 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1230 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1231 return 0;
1232 }
Adam Langley95c29f32014-06-20 12:00:00 -07001233
Adam Langleyfcf25832014-12-18 17:42:32 -08001234 return 1;
1235}
David Benjamin39482a12014-07-20 13:30:15 -04001236
Adam Langleyfcf25832014-12-18 17:42:32 -08001237int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str) {
1238 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001239
David Benjamin71f07942015-04-08 02:36:59 -04001240 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str);
Adam Langleyfcf25832014-12-18 17:42:32 -08001241 if (sk == NULL) {
1242 return 0;
1243 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1244 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list_tls11,
1245 SSL_R_NO_CIPHER_MATCH);
1246 return 0;
1247 }
David Benjamin9f2c0d72014-10-21 22:00:19 -04001248
Adam Langleyfcf25832014-12-18 17:42:32 -08001249 return 1;
1250}
Adam Langley95c29f32014-06-20 12:00:00 -07001251
Adam Langleyfcf25832014-12-18 17:42:32 -08001252/* specify the ciphers to be used by the SSL */
1253int SSL_set_cipher_list(SSL *s, const char *str) {
1254 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001255
Adam Langleyfcf25832014-12-18 17:42:32 -08001256 sk = ssl_create_cipher_list(s->ctx->method, &s->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001257 &s->cipher_list_by_id, str);
David Benjamin39482a12014-07-20 13:30:15 -04001258
Adam Langleyfcf25832014-12-18 17:42:32 -08001259 /* see comment in SSL_CTX_set_cipher_list */
1260 if (sk == NULL) {
1261 return 0;
1262 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1263 OPENSSL_PUT_ERROR(SSL, SSL_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1264 return 0;
1265 }
David Benjamin39482a12014-07-20 13:30:15 -04001266
Adam Langleyfcf25832014-12-18 17:42:32 -08001267 return 1;
1268}
Adam Langley95c29f32014-06-20 12:00:00 -07001269
Adam Langleyfcf25832014-12-18 17:42:32 -08001270int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, uint8_t *p) {
1271 size_t i;
1272 const SSL_CIPHER *c;
1273 CERT *ct = s->cert;
1274 uint8_t *q;
1275 /* Set disabled masks for this session */
1276 ssl_set_client_disabled(s);
Adam Langley29707792014-06-20 12:00:00 -07001277
Adam Langleyfcf25832014-12-18 17:42:32 -08001278 if (sk == NULL) {
1279 return 0;
1280 }
1281 q = p;
Adam Langley95c29f32014-06-20 12:00:00 -07001282
Adam Langleyfcf25832014-12-18 17:42:32 -08001283 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
1284 c = sk_SSL_CIPHER_value(sk, i);
1285 /* Skip disabled ciphers */
1286 if (c->algorithm_ssl & ct->mask_ssl ||
1287 c->algorithm_mkey & ct->mask_k ||
1288 c->algorithm_auth & ct->mask_a) {
1289 continue;
1290 }
1291 s2n(ssl3_get_cipher_value(c), p);
1292 }
1293
1294 /* If all ciphers were disabled, return the error to the caller. */
1295 if (p == q) {
1296 return 0;
1297 }
1298
1299 /* Add SCSVs. */
David Benjamin20f6e972015-05-15 21:51:49 -04001300 if (!s->s3->initial_handshake_complete) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001301 s2n(SSL3_CK_SCSV & 0xffff, p);
1302 }
1303
Adam Langley5f0efe02015-02-20 13:03:16 -08001304 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001305 s2n(SSL3_CK_FALLBACK_SCSV & 0xffff, p);
1306 }
1307
1308 return p - q;
1309}
1310
1311STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs) {
1312 CBS cipher_suites = *cbs;
1313 const SSL_CIPHER *c;
David Benjamin60da0cd2015-05-03 15:21:28 -04001314 STACK_OF(SSL_CIPHER) *sk;
Adam Langleyfcf25832014-12-18 17:42:32 -08001315
1316 if (s->s3) {
1317 s->s3->send_connection_binding = 0;
1318 }
1319
1320 if (CBS_len(&cipher_suites) % 2 != 0) {
1321 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1322 SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
1323 return NULL;
1324 }
1325
1326 sk = sk_SSL_CIPHER_new_null();
1327 if (sk == NULL) {
1328 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1329 goto err;
1330 }
1331
Adam Langleyfcf25832014-12-18 17:42:32 -08001332 while (CBS_len(&cipher_suites) > 0) {
1333 uint16_t cipher_suite;
1334
1335 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
1336 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_INTERNAL_ERROR);
1337 goto err;
1338 }
1339
1340 /* Check for SCSV. */
1341 if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff)) {
1342 /* SCSV is fatal if renegotiating. */
David Benjamin20f6e972015-05-15 21:51:49 -04001343 if (s->s3->initial_handshake_complete) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001344 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1345 SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1346 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1347 goto err;
1348 }
1349 s->s3->send_connection_binding = 1;
1350 continue;
1351 }
1352
1353 /* Check for FALLBACK_SCSV. */
1354 if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff)) {
1355 uint16_t max_version = ssl3_get_max_server_version(s);
1356 if (SSL_IS_DTLS(s) ? (uint16_t)s->version > max_version
1357 : (uint16_t)s->version < max_version) {
1358 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1359 SSL_R_INAPPROPRIATE_FALLBACK);
1360 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
1361 goto err;
1362 }
1363 continue;
1364 }
1365
1366 c = ssl3_get_cipher_by_value(cipher_suite);
1367 if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
1368 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1369 goto err;
1370 }
1371 }
1372
1373 return sk;
David Benjamin9f2c0d72014-10-21 22:00:19 -04001374
Adam Langley95c29f32014-06-20 12:00:00 -07001375err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001376 sk_SSL_CIPHER_free(sk);
Adam Langleyfcf25832014-12-18 17:42:32 -08001377 return NULL;
1378}
Adam Langley95c29f32014-06-20 12:00:00 -07001379
1380
Adam Langleyfcf25832014-12-18 17:42:32 -08001381/* return a servername extension value if provided in Client Hello, or NULL. So
1382 * far, only host_name types are defined (RFC 3546). */
1383const char *SSL_get_servername(const SSL *s, const int type) {
1384 if (type != TLSEXT_NAMETYPE_host_name) {
1385 return NULL;
1386 }
Adam Langley95c29f32014-06-20 12:00:00 -07001387
Adam Langleyfcf25832014-12-18 17:42:32 -08001388 return s->session && !s->tlsext_hostname ? s->session->tlsext_hostname
1389 : s->tlsext_hostname;
1390}
Adam Langley95c29f32014-06-20 12:00:00 -07001391
Adam Langleyfcf25832014-12-18 17:42:32 -08001392int SSL_get_servername_type(const SSL *s) {
1393 if (s->session &&
1394 (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname)) {
1395 return TLSEXT_NAMETYPE_host_name;
1396 }
Adam Langley95c29f32014-06-20 12:00:00 -07001397
Adam Langleyfcf25832014-12-18 17:42:32 -08001398 return -1;
1399}
Adam Langley95c29f32014-06-20 12:00:00 -07001400
Adam Langleyfcf25832014-12-18 17:42:32 -08001401void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
1402 ctx->signed_cert_timestamps_enabled = 1;
1403}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001404
Adam Langleyfcf25832014-12-18 17:42:32 -08001405int SSL_enable_signed_cert_timestamps(SSL *ssl) {
1406 ssl->signed_cert_timestamps_enabled = 1;
1407 return 1;
1408}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001409
Adam Langleyfcf25832014-12-18 17:42:32 -08001410void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
1411 ctx->ocsp_stapling_enabled = 1;
1412}
David Benjamin6c7aed02014-08-27 16:42:38 -04001413
Adam Langleyfcf25832014-12-18 17:42:32 -08001414int SSL_enable_ocsp_stapling(SSL *ssl) {
1415 ssl->ocsp_stapling_enabled = 1;
1416 return 1;
1417}
David Benjamin6c7aed02014-08-27 16:42:38 -04001418
Adam Langleyfcf25832014-12-18 17:42:32 -08001419void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
1420 size_t *out_len) {
1421 SSL_SESSION *session = ssl->session;
Adam Langley3cb50e02014-08-26 14:00:31 -07001422
Adam Langleyfcf25832014-12-18 17:42:32 -08001423 *out_len = 0;
1424 *out = NULL;
1425 if (ssl->server || !session || !session->tlsext_signed_cert_timestamp_list) {
1426 return;
1427 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001428
Adam Langleyfcf25832014-12-18 17:42:32 -08001429 *out = session->tlsext_signed_cert_timestamp_list;
1430 *out_len = session->tlsext_signed_cert_timestamp_list_length;
1431}
David Benjamin6c7aed02014-08-27 16:42:38 -04001432
Adam Langleyfcf25832014-12-18 17:42:32 -08001433void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
1434 size_t *out_len) {
1435 SSL_SESSION *session = ssl->session;
1436
1437 *out_len = 0;
1438 *out = NULL;
1439 if (ssl->server || !session || !session->ocsp_response) {
1440 return;
1441 }
1442 *out = session->ocsp_response;
1443 *out_len = session->ocsp_response_length;
1444}
David Benjamin6c7aed02014-08-27 16:42:38 -04001445
Adam Langley95c29f32014-06-20 12:00:00 -07001446/* SSL_select_next_proto implements the standard protocol selection. It is
1447 * expected that this function is called from the callback set by
1448 * SSL_CTX_set_next_proto_select_cb.
1449 *
1450 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1451 * strings. The length byte itself is not included in the length. A byte
1452 * string of length 0 is invalid. No byte string may be truncated.
1453 *
1454 * The current, but experimental algorithm for selecting the protocol is:
1455 *
1456 * 1) If the server doesn't support NPN then this is indicated to the
1457 * callback. In this case, the client application has to abort the connection
1458 * or have a default application level protocol.
1459 *
1460 * 2) If the server supports NPN, but advertises an empty list then the
1461 * client selects the first protcol in its list, but indicates via the
1462 * API that this fallback case was enacted.
1463 *
1464 * 3) Otherwise, the client finds the first protocol in the server's list
1465 * that it supports and selects this protocol. This is because it's
1466 * assumed that the server has better information about which protocol
1467 * a client should use.
1468 *
1469 * 4) If the client doesn't support any of the server's advertised
1470 * protocols, then this is treated the same as case 2.
1471 *
1472 * It returns either
1473 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1474 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1475 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001476int SSL_select_next_proto(uint8_t **out, uint8_t *outlen, const uint8_t *server,
1477 unsigned int server_len, const uint8_t *client,
1478 unsigned int client_len) {
1479 unsigned int i, j;
1480 const uint8_t *result;
1481 int status = OPENSSL_NPN_UNSUPPORTED;
Adam Langley95c29f32014-06-20 12:00:00 -07001482
Adam Langleyfcf25832014-12-18 17:42:32 -08001483 /* For each protocol in server preference order, see if we support it. */
1484 for (i = 0; i < server_len;) {
1485 for (j = 0; j < client_len;) {
1486 if (server[i] == client[j] &&
1487 memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
1488 /* We found a match */
1489 result = &server[i];
1490 status = OPENSSL_NPN_NEGOTIATED;
1491 goto found;
1492 }
1493 j += client[j];
1494 j++;
1495 }
1496 i += server[i];
1497 i++;
1498 }
Adam Langley95c29f32014-06-20 12:00:00 -07001499
Adam Langleyfcf25832014-12-18 17:42:32 -08001500 /* There's no overlap between our protocols and the server's list. */
1501 result = client;
1502 status = OPENSSL_NPN_NO_OVERLAP;
Adam Langley95c29f32014-06-20 12:00:00 -07001503
Adam Langleyfcf25832014-12-18 17:42:32 -08001504found:
1505 *out = (uint8_t *)result + 1;
1506 *outlen = result[0];
1507 return status;
1508}
Adam Langley95c29f32014-06-20 12:00:00 -07001509
Adam Langley95c29f32014-06-20 12:00:00 -07001510/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
1511 * requested protocol for this connection and returns 0. If the client didn't
1512 * request any protocol, then *data is set to NULL.
1513 *
1514 * Note that the client can request any protocol it chooses. The value returned
1515 * from this function need not be a member of the list of supported protocols
Adam Langleyfcf25832014-12-18 17:42:32 -08001516 * provided by the callback. */
1517void SSL_get0_next_proto_negotiated(const SSL *s, const uint8_t **data,
1518 unsigned *len) {
1519 *data = s->next_proto_negotiated;
1520 if (!*data) {
1521 *len = 0;
1522 } else {
1523 *len = s->next_proto_negotiated_len;
1524 }
Adam Langley95c29f32014-06-20 12:00:00 -07001525}
1526
1527/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
1528 * TLS server needs a list of supported protocols for Next Protocol
1529 * Negotiation. The returned list must be in wire format. The list is returned
1530 * by setting |out| to point to it and |outlen| to its length. This memory will
1531 * not be modified, but one should assume that the SSL* keeps a reference to
1532 * it.
1533 *
Adam Langleyfcf25832014-12-18 17:42:32 -08001534 * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise.
1535 * Otherwise, no such extension will be included in the ServerHello. */
1536void SSL_CTX_set_next_protos_advertised_cb(
1537 SSL_CTX *ctx,
1538 int (*cb)(SSL *ssl, const uint8_t **out, unsigned int *outlen, void *arg),
1539 void *arg) {
1540 ctx->next_protos_advertised_cb = cb;
1541 ctx->next_protos_advertised_cb_arg = arg;
1542}
Adam Langley95c29f32014-06-20 12:00:00 -07001543
1544/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1545 * client needs to select a protocol from the server's provided list. |out|
1546 * must be set to point to the selected protocol (which may be within |in|).
1547 * The length of the protocol name must be written into |outlen|. The server's
1548 * advertised protocols are provided in |in| and |inlen|. The callback can
1549 * assume that |in| is syntactically valid.
1550 *
1551 * The client must select a protocol. It is fatal to the connection if this
1552 * callback returns a value other than SSL_TLSEXT_ERR_OK.
1553 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001554void SSL_CTX_set_next_proto_select_cb(
1555 SSL_CTX *ctx, int (*cb)(SSL *s, uint8_t **out, uint8_t *outlen,
1556 const uint8_t *in, unsigned int inlen, void *arg),
1557 void *arg) {
1558 ctx->next_proto_select_cb = cb;
1559 ctx->next_proto_select_cb_arg = arg;
1560}
Adam Langley95c29f32014-06-20 12:00:00 -07001561
Adam Langleyfcf25832014-12-18 17:42:32 -08001562int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
1563 unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001564 OPENSSL_free(ctx->alpn_client_proto_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001565 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1566 if (!ctx->alpn_client_proto_list) {
1567 return 1;
1568 }
1569 ctx->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001570
Adam Langleyfcf25832014-12-18 17:42:32 -08001571 return 0;
1572}
Adam Langley95c29f32014-06-20 12:00:00 -07001573
Adam Langleyfcf25832014-12-18 17:42:32 -08001574int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001575 OPENSSL_free(ssl->alpn_client_proto_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001576 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1577 if (!ssl->alpn_client_proto_list) {
1578 return 1;
1579 }
1580 ssl->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001581
Adam Langleyfcf25832014-12-18 17:42:32 -08001582 return 0;
1583}
Adam Langley95c29f32014-06-20 12:00:00 -07001584
1585/* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
1586 * during ClientHello processing in order to select an ALPN protocol from the
1587 * client's list of offered protocols. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001588void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
1589 int (*cb)(SSL *ssl, const uint8_t **out,
1590 uint8_t *outlen, const uint8_t *in,
1591 unsigned int inlen, void *arg),
1592 void *arg) {
1593 ctx->alpn_select_cb = cb;
1594 ctx->alpn_select_cb_arg = arg;
1595}
Adam Langley95c29f32014-06-20 12:00:00 -07001596
1597/* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
1598 * On return it sets |*data| to point to |*len| bytes of protocol name (not
1599 * including the leading length-prefix byte). If the server didn't respond with
1600 * a negotiated protocol then |*len| will be zero. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001601void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **data,
1602 unsigned *len) {
1603 *data = NULL;
1604 if (ssl->s3) {
1605 *data = ssl->s3->alpn_selected;
1606 }
1607 if (*data == NULL) {
1608 *len = 0;
1609 } else {
1610 *len = ssl->s3->alpn_selected_len;
1611 }
1612}
Adam Langley95c29f32014-06-20 12:00:00 -07001613
David Benjamincfd248b2015-04-03 11:02:24 -04001614int SSL_export_keying_material(SSL *s, uint8_t *out, size_t out_len,
1615 const char *label, size_t label_len,
1616 const uint8_t *context, size_t context_len,
1617 int use_context) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001618 if (s->version < TLS1_VERSION) {
David Benjaminc565ebb2015-04-03 04:06:36 -04001619 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001620 }
Adam Langley95c29f32014-06-20 12:00:00 -07001621
David Benjamincfd248b2015-04-03 11:02:24 -04001622 return s->enc_method->export_keying_material(
1623 s, out, out_len, label, label_len, context, context_len, use_context);
Adam Langleyfcf25832014-12-18 17:42:32 -08001624}
Adam Langley95c29f32014-06-20 12:00:00 -07001625
Adam Langleyfcf25832014-12-18 17:42:32 -08001626static uint32_t ssl_session_hash(const SSL_SESSION *a) {
1627 uint32_t hash =
1628 ((uint32_t)a->session_id[0]) ||
1629 ((uint32_t)a->session_id[1] << 8) ||
1630 ((uint32_t)a->session_id[2] << 16) ||
1631 ((uint32_t)a->session_id[3] << 24);
Adam Langley95c29f32014-06-20 12:00:00 -07001632
Adam Langleyfcf25832014-12-18 17:42:32 -08001633 return hash;
1634}
Adam Langley95c29f32014-06-20 12:00:00 -07001635
1636/* NB: If this function (or indeed the hash function which uses a sort of
1637 * coarser function than this one) is changed, ensure
1638 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1639 * able to construct an SSL_SESSION that will collide with any existing session
1640 * with a matching session ID. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001641static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
1642 if (a->ssl_version != b->ssl_version) {
1643 return 1;
1644 }
Adam Langley95c29f32014-06-20 12:00:00 -07001645
Adam Langleyfcf25832014-12-18 17:42:32 -08001646 if (a->session_id_length != b->session_id_length) {
1647 return 1;
1648 }
Adam Langley95c29f32014-06-20 12:00:00 -07001649
Adam Langleyfcf25832014-12-18 17:42:32 -08001650 return memcmp(a->session_id, b->session_id, a->session_id_length);
1651}
Adam Langley95c29f32014-06-20 12:00:00 -07001652
David Benjamin4831c332015-05-16 11:43:13 -04001653SSL_CTX *SSL_CTX_new(const SSL_METHOD *method) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001654 SSL_CTX *ret = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001655
David Benjamin4831c332015-05-16 11:43:13 -04001656 if (method == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001657 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_NULL_SSL_METHOD_PASSED);
1658 return NULL;
1659 }
Adam Langley95c29f32014-06-20 12:00:00 -07001660
Adam Langleyfcf25832014-12-18 17:42:32 -08001661 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
1662 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
1663 goto err;
1664 }
Adam Langley95c29f32014-06-20 12:00:00 -07001665
Adam Langleyfcf25832014-12-18 17:42:32 -08001666 ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1667 if (ret == NULL) {
1668 goto err;
1669 }
Adam Langley95c29f32014-06-20 12:00:00 -07001670
Adam Langleyfcf25832014-12-18 17:42:32 -08001671 memset(ret, 0, sizeof(SSL_CTX));
Adam Langley95c29f32014-06-20 12:00:00 -07001672
David Benjamin4831c332015-05-16 11:43:13 -04001673 ret->method = method->method;
Adam Langley95c29f32014-06-20 12:00:00 -07001674
Adam Langley4bdb6e42015-05-15 15:29:21 -07001675 CRYPTO_MUTEX_init(&ret->lock);
1676
Adam Langleyfcf25832014-12-18 17:42:32 -08001677 ret->cert_store = NULL;
1678 ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
1679 ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1680 ret->session_cache_head = NULL;
1681 ret->session_cache_tail = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001682
Adam Langleyfcf25832014-12-18 17:42:32 -08001683 /* We take the system default */
1684 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001685
Adam Langleyfcf25832014-12-18 17:42:32 -08001686 ret->new_session_cb = 0;
1687 ret->remove_session_cb = 0;
1688 ret->get_session_cb = 0;
1689 ret->generate_session_id = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001690
Adam Langleyfcf25832014-12-18 17:42:32 -08001691 ret->references = 1;
1692 ret->quiet_shutdown = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001693
Adam Langleyfcf25832014-12-18 17:42:32 -08001694 ret->info_callback = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001695
Adam Langleyfcf25832014-12-18 17:42:32 -08001696 ret->app_verify_callback = 0;
1697 ret->app_verify_arg = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001698
Adam Langleyfcf25832014-12-18 17:42:32 -08001699 ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
Adam Langleyfcf25832014-12-18 17:42:32 -08001700 ret->msg_callback = 0;
1701 ret->msg_callback_arg = NULL;
1702 ret->verify_mode = SSL_VERIFY_NONE;
1703 ret->sid_ctx_length = 0;
1704 ret->default_verify_callback = NULL;
1705 ret->cert = ssl_cert_new();
1706 if (ret->cert == NULL) {
1707 goto err;
1708 }
Adam Langley95c29f32014-06-20 12:00:00 -07001709
Adam Langleyfcf25832014-12-18 17:42:32 -08001710 ret->default_passwd_callback = 0;
1711 ret->default_passwd_callback_userdata = NULL;
1712 ret->client_cert_cb = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001713
Adam Langleyfcf25832014-12-18 17:42:32 -08001714 ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
1715 if (ret->sessions == NULL) {
1716 goto err;
1717 }
1718 ret->cert_store = X509_STORE_new();
1719 if (ret->cert_store == NULL) {
1720 goto err;
1721 }
Adam Langley95c29f32014-06-20 12:00:00 -07001722
Adam Langleyfcf25832014-12-18 17:42:32 -08001723 ssl_create_cipher_list(ret->method, &ret->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001724 &ret->cipher_list_by_id, SSL_DEFAULT_CIPHER_LIST);
Adam Langleyfcf25832014-12-18 17:42:32 -08001725 if (ret->cipher_list == NULL ||
1726 sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0) {
1727 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_LIBRARY_HAS_NO_CIPHERS);
1728 goto err2;
1729 }
Adam Langley95c29f32014-06-20 12:00:00 -07001730
Adam Langleyfcf25832014-12-18 17:42:32 -08001731 ret->param = X509_VERIFY_PARAM_new();
1732 if (!ret->param) {
1733 goto err;
1734 }
Adam Langley95c29f32014-06-20 12:00:00 -07001735
Adam Langleyfcf25832014-12-18 17:42:32 -08001736 ret->client_CA = sk_X509_NAME_new_null();
1737 if (ret->client_CA == NULL) {
1738 goto err;
1739 }
Adam Langley95c29f32014-06-20 12:00:00 -07001740
David Benjamin9f33fc62015-04-15 17:29:53 -04001741 CRYPTO_new_ex_data(&g_ex_data_class_ssl_ctx, ret, &ret->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001742
Adam Langleyfcf25832014-12-18 17:42:32 -08001743 ret->extra_certs = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001744
Adam Langleyfcf25832014-12-18 17:42:32 -08001745 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
David Benjamin422d3a42014-08-20 11:09:03 -04001746
Adam Langleyfcf25832014-12-18 17:42:32 -08001747 ret->tlsext_servername_callback = 0;
1748 ret->tlsext_servername_arg = NULL;
1749 /* Setup RFC4507 ticket keys */
1750 if (!RAND_bytes(ret->tlsext_tick_key_name, 16) ||
1751 !RAND_bytes(ret->tlsext_tick_hmac_key, 16) ||
1752 !RAND_bytes(ret->tlsext_tick_aes_key, 16)) {
1753 ret->options |= SSL_OP_NO_TICKET;
1754 }
Adam Langley95c29f32014-06-20 12:00:00 -07001755
Adam Langleyfcf25832014-12-18 17:42:32 -08001756 ret->next_protos_advertised_cb = 0;
1757 ret->next_proto_select_cb = 0;
1758 ret->psk_identity_hint = NULL;
1759 ret->psk_client_callback = NULL;
1760 ret->psk_server_callback = NULL;
1761
1762 /* Default is to connect to non-RI servers. When RI is more widely deployed
1763 * might change this. */
1764 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
1765
1766 /* Lock the SSL_CTX to the specified version, for compatibility with legacy
1767 * uses of SSL_METHOD. */
David Benjamin4831c332015-05-16 11:43:13 -04001768 if (method->version != 0) {
1769 SSL_CTX_set_max_version(ret, method->version);
1770 SSL_CTX_set_min_version(ret, method->version);
Adam Langleyfcf25832014-12-18 17:42:32 -08001771 }
1772
1773 return ret;
1774
Adam Langley95c29f32014-06-20 12:00:00 -07001775err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001776 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -07001777err2:
David Benjamin2755a3e2015-04-22 16:17:58 -04001778 SSL_CTX_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -08001779 return NULL;
1780}
Adam Langley95c29f32014-06-20 12:00:00 -07001781
David Benjamin4fcc2e22015-04-22 12:58:16 -04001782void SSL_CTX_free(SSL_CTX *ctx) {
1783 if (ctx == NULL ||
Adam Langley0b5e3902015-05-15 13:08:38 -07001784 !CRYPTO_refcount_dec_and_test_zero(&ctx->references)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001785 return;
1786 }
Adam Langley95c29f32014-06-20 12:00:00 -07001787
David Benjamin2755a3e2015-04-22 16:17:58 -04001788 X509_VERIFY_PARAM_free(ctx->param);
Adam Langley95c29f32014-06-20 12:00:00 -07001789
Adam Langleyfcf25832014-12-18 17:42:32 -08001790 /* Free internal session cache. However: the remove_cb() may reference the
1791 * ex_data of SSL_CTX, thus the ex_data store can only be removed after the
1792 * sessions were flushed. As the ex_data handling routines might also touch
1793 * the session cache, the most secure solution seems to be: empty (flush) the
1794 * cache, then free ex_data, then finally free the cache. (See ticket
1795 * [openssl.org #212].) */
David Benjamin2755a3e2015-04-22 16:17:58 -04001796 SSL_CTX_flush_sessions(ctx, 0);
Adam Langley95c29f32014-06-20 12:00:00 -07001797
David Benjamin4fcc2e22015-04-22 12:58:16 -04001798 CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, ctx, &ctx->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001799
Adam Langley4bdb6e42015-05-15 15:29:21 -07001800 CRYPTO_MUTEX_cleanup(&ctx->lock);
David Benjamin2755a3e2015-04-22 16:17:58 -04001801 lh_SSL_SESSION_free(ctx->sessions);
1802 X509_STORE_free(ctx->cert_store);
1803 ssl_cipher_preference_list_free(ctx->cipher_list);
1804 sk_SSL_CIPHER_free(ctx->cipher_list_by_id);
1805 ssl_cipher_preference_list_free(ctx->cipher_list_tls11);
1806 ssl_cert_free(ctx->cert);
1807 sk_X509_NAME_pop_free(ctx->client_CA, X509_NAME_free);
1808 sk_X509_pop_free(ctx->extra_certs, X509_free);
1809 sk_SRTP_PROTECTION_PROFILE_free(ctx->srtp_profiles);
1810 OPENSSL_free(ctx->psk_identity_hint);
1811 OPENSSL_free(ctx->tlsext_ecpointformatlist);
1812 OPENSSL_free(ctx->tlsext_ellipticcurvelist);
1813 OPENSSL_free(ctx->alpn_client_proto_list);
1814 EVP_PKEY_free(ctx->tlsext_channel_id_private);
1815 BIO_free(ctx->keylog_bio);
Adam Langley95c29f32014-06-20 12:00:00 -07001816
David Benjamin4fcc2e22015-04-22 12:58:16 -04001817 OPENSSL_free(ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08001818}
Adam Langley95c29f32014-06-20 12:00:00 -07001819
Adam Langleyfcf25832014-12-18 17:42:32 -08001820void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) {
1821 ctx->default_passwd_callback = cb;
1822}
Adam Langley95c29f32014-06-20 12:00:00 -07001823
Adam Langleyfcf25832014-12-18 17:42:32 -08001824void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u) {
1825 ctx->default_passwd_callback_userdata = u;
1826}
Adam Langley95c29f32014-06-20 12:00:00 -07001827
Adam Langleyfcf25832014-12-18 17:42:32 -08001828void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
1829 int (*cb)(X509_STORE_CTX *, void *),
1830 void *arg) {
1831 ctx->app_verify_callback = cb;
1832 ctx->app_verify_arg = arg;
1833}
Adam Langley95c29f32014-06-20 12:00:00 -07001834
Adam Langleyfcf25832014-12-18 17:42:32 -08001835void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
1836 int (*cb)(int, X509_STORE_CTX *)) {
1837 ctx->verify_mode = mode;
1838 ctx->default_verify_callback = cb;
1839}
Adam Langley95c29f32014-06-20 12:00:00 -07001840
Adam Langleyfcf25832014-12-18 17:42:32 -08001841void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
1842 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
1843}
Adam Langley1258b6a2014-06-20 12:00:00 -07001844
Adam Langleyfcf25832014-12-18 17:42:32 -08001845void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb)(SSL *ssl, void *arg),
1846 void *arg) {
1847 ssl_cert_set_cert_cb(c->cert, cb, arg);
1848}
David Benjamin859ec3c2014-09-02 16:29:36 -04001849
Adam Langleyfcf25832014-12-18 17:42:32 -08001850void SSL_set_cert_cb(SSL *s, int (*cb)(SSL *ssl, void *arg), void *arg) {
1851 ssl_cert_set_cert_cb(s->cert, cb, arg);
1852}
Adam Langley95c29f32014-06-20 12:00:00 -07001853
Adam Langleyfcf25832014-12-18 17:42:32 -08001854static int ssl_has_key(SSL *s, size_t idx) {
1855 CERT_PKEY *cpk = &s->cert->pkeys[idx];
1856 return cpk->x509 && cpk->privatekey;
1857}
David Benjamin033e5f42014-11-13 18:47:41 -05001858
David Benjamin107db582015-04-08 00:41:59 -04001859void ssl_get_compatible_server_ciphers(SSL *s, uint32_t *out_mask_k,
1860 uint32_t *out_mask_a) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001861 CERT *c = s->cert;
1862 int rsa_enc, rsa_sign, dh_tmp;
David Benjamin107db582015-04-08 00:41:59 -04001863 uint32_t mask_k, mask_a;
Adam Langleyfcf25832014-12-18 17:42:32 -08001864 int have_ecc_cert, ecdsa_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001865 X509 *x;
Adam Langley95c29f32014-06-20 12:00:00 -07001866
Adam Langleyfcf25832014-12-18 17:42:32 -08001867 if (c == NULL) {
1868 /* TODO(davidben): Is this codepath possible? */
1869 *out_mask_k = 0;
1870 *out_mask_a = 0;
1871 return;
1872 }
Adam Langley95c29f32014-06-20 12:00:00 -07001873
Adam Langleyfcf25832014-12-18 17:42:32 -08001874 dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001875
Adam Langleyfcf25832014-12-18 17:42:32 -08001876 rsa_enc = ssl_has_key(s, SSL_PKEY_RSA_ENC);
1877 rsa_sign = ssl_has_key(s, SSL_PKEY_RSA_SIGN);
1878 have_ecc_cert = ssl_has_key(s, SSL_PKEY_ECC);
1879 mask_k = 0;
1880 mask_a = 0;
David Benjaminf31e6812014-11-13 18:05:55 -05001881
Adam Langleyfcf25832014-12-18 17:42:32 -08001882 if (rsa_enc) {
1883 mask_k |= SSL_kRSA;
1884 }
1885 if (dh_tmp) {
David Benjamin7061e282015-03-19 11:10:48 -04001886 mask_k |= SSL_kDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001887 }
1888 if (rsa_enc || rsa_sign) {
1889 mask_a |= SSL_aRSA;
1890 }
Adam Langley95c29f32014-06-20 12:00:00 -07001891
Adam Langleyfcf25832014-12-18 17:42:32 -08001892 /* An ECC certificate may be usable for ECDSA cipher suites depending on the
1893 * key usage extension and on the client's curve preferences. */
1894 if (have_ecc_cert) {
1895 x = c->pkeys[SSL_PKEY_ECC].x509;
1896 /* This call populates extension flags (ex_flags). */
1897 X509_check_purpose(x, -1, 0);
1898 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE)
1899 ? (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)
1900 : 1;
1901 if (!tls1_check_ec_cert(s, x)) {
1902 ecdsa_ok = 0;
1903 }
1904 if (ecdsa_ok) {
1905 mask_a |= SSL_aECDSA;
1906 }
1907 }
Adam Langley95c29f32014-06-20 12:00:00 -07001908
Adam Langleyfcf25832014-12-18 17:42:32 -08001909 /* If we are considering an ECC cipher suite that uses an ephemeral EC
1910 * key, check it. */
David Benjamindd978782015-04-24 15:20:13 -04001911 if (tls1_check_ec_tmp_key(s)) {
David Benjamin7061e282015-03-19 11:10:48 -04001912 mask_k |= SSL_kECDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001913 }
Adam Langley95c29f32014-06-20 12:00:00 -07001914
Adam Langleyfcf25832014-12-18 17:42:32 -08001915 /* PSK requires a server callback. */
1916 if (s->psk_server_callback != NULL) {
1917 mask_k |= SSL_kPSK;
1918 mask_a |= SSL_aPSK;
1919 }
Adam Langley95c29f32014-06-20 12:00:00 -07001920
Adam Langleyfcf25832014-12-18 17:42:32 -08001921 *out_mask_k = mask_k;
1922 *out_mask_a = mask_a;
1923}
Adam Langley95c29f32014-06-20 12:00:00 -07001924
1925/* This handy macro borrowed from crypto/x509v3/v3_purp.c */
1926#define ku_reject(x, usage) \
Adam Langleyfcf25832014-12-18 17:42:32 -08001927 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
Adam Langley95c29f32014-06-20 12:00:00 -07001928
Adam Langleyfcf25832014-12-18 17:42:32 -08001929int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001930 const SSL_CIPHER *cs = s->s3->tmp.new_cipher;
David Benjamin3fa27772015-04-17 22:32:19 -04001931 uint32_t alg_a = cs->algorithm_auth;
1932 int signature_nid = 0, md_nid = 0, pk_nid = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001933
Adam Langleyfcf25832014-12-18 17:42:32 -08001934 /* This call populates the ex_flags field correctly */
1935 X509_check_purpose(x, -1, 0);
1936 if (x->sig_alg && x->sig_alg->algorithm) {
1937 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
1938 OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
1939 }
1940 if (alg_a & SSL_aECDSA) {
1941 /* key usage, if present, must allow signing */
1942 if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE)) {
1943 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg,
1944 SSL_R_ECC_CERT_NOT_FOR_SIGNING);
1945 return 0;
1946 }
1947 }
Adam Langley95c29f32014-06-20 12:00:00 -07001948
Adam Langleyfcf25832014-12-18 17:42:32 -08001949 return 1; /* all checks are ok */
1950}
Adam Langley95c29f32014-06-20 12:00:00 -07001951
Adam Langleyfcf25832014-12-18 17:42:32 -08001952static int ssl_get_server_cert_index(const SSL *s) {
1953 int idx;
1954 idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1955 if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509) {
1956 idx = SSL_PKEY_RSA_SIGN;
1957 }
1958 if (idx == -1) {
1959 OPENSSL_PUT_ERROR(SSL, ssl_get_server_cert_index, ERR_R_INTERNAL_ERROR);
1960 }
1961 return idx;
1962}
Adam Langley95c29f32014-06-20 12:00:00 -07001963
Adam Langleyfcf25832014-12-18 17:42:32 -08001964CERT_PKEY *ssl_get_server_send_pkey(const SSL *s) {
1965 int i = ssl_get_server_cert_index(s);
Adam Langley95c29f32014-06-20 12:00:00 -07001966
Adam Langleyfcf25832014-12-18 17:42:32 -08001967 /* This may or may not be an error. */
1968 if (i < 0) {
1969 return NULL;
1970 }
Adam Langley95c29f32014-06-20 12:00:00 -07001971
Adam Langleyfcf25832014-12-18 17:42:32 -08001972 /* May be NULL. */
1973 return &s->cert->pkeys[i];
1974}
Adam Langley95c29f32014-06-20 12:00:00 -07001975
Adam Langleyfcf25832014-12-18 17:42:32 -08001976EVP_PKEY *ssl_get_sign_pkey(SSL *s, const SSL_CIPHER *cipher) {
David Benjamin3fa27772015-04-17 22:32:19 -04001977 uint32_t alg_a = cipher->algorithm_auth;
1978 CERT *c = s->cert;
Adam Langleyfcf25832014-12-18 17:42:32 -08001979 int idx = -1;
Adam Langley95c29f32014-06-20 12:00:00 -07001980
Adam Langleyfcf25832014-12-18 17:42:32 -08001981 if (alg_a & SSL_aRSA) {
1982 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL) {
1983 idx = SSL_PKEY_RSA_SIGN;
1984 } else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL) {
1985 idx = SSL_PKEY_RSA_ENC;
1986 }
1987 } else if ((alg_a & SSL_aECDSA) &&
1988 (c->pkeys[SSL_PKEY_ECC].privatekey != NULL)) {
1989 idx = SSL_PKEY_ECC;
1990 }
Adam Langley95c29f32014-06-20 12:00:00 -07001991
Adam Langleyfcf25832014-12-18 17:42:32 -08001992 if (idx == -1) {
1993 OPENSSL_PUT_ERROR(SSL, ssl_get_sign_pkey, ERR_R_INTERNAL_ERROR);
1994 return NULL;
1995 }
Adam Langley95c29f32014-06-20 12:00:00 -07001996
Adam Langleyfcf25832014-12-18 17:42:32 -08001997 return c->pkeys[idx].privatekey;
1998}
Adam Langley95c29f32014-06-20 12:00:00 -07001999
Adam Langleyfcf25832014-12-18 17:42:32 -08002000void ssl_update_cache(SSL *s, int mode) {
David Benjaminb6d0c6d2015-03-19 19:07:26 -04002001 /* Never cache sessions with empty session IDs. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002002 if (s->session->session_id_length == 0) {
2003 return;
2004 }
Adam Langley95c29f32014-06-20 12:00:00 -07002005
David Benjaminb6d0c6d2015-03-19 19:07:26 -04002006 SSL_CTX *ctx = s->initial_ctx;
2007 if ((ctx->session_cache_mode & mode) == mode && !s->hit &&
2008 ((ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) ||
2009 SSL_CTX_add_session(ctx, s->session)) &&
2010 ctx->new_session_cb != NULL) {
2011 /* Note: |new_session_cb| is called whether the internal session cache is
2012 * used or not. */
2013 if (!ctx->new_session_cb(s, SSL_SESSION_up_ref(s->session))) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002014 SSL_SESSION_free(s->session);
2015 }
2016 }
Adam Langley95c29f32014-06-20 12:00:00 -07002017
David Benjaminb6d0c6d2015-03-19 19:07:26 -04002018 if (!(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR) &&
2019 !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) &&
2020 (ctx->session_cache_mode & mode) == mode) {
2021 /* Automatically flush the internal session cache every 255 connections. */
2022 int flush_cache = 0;
Adam Langley4bdb6e42015-05-15 15:29:21 -07002023 CRYPTO_MUTEX_lock_write(&ctx->lock);
David Benjaminb6d0c6d2015-03-19 19:07:26 -04002024 ctx->handshakes_since_cache_flush++;
2025 if (ctx->handshakes_since_cache_flush >= 255) {
2026 flush_cache = 1;
2027 ctx->handshakes_since_cache_flush = 0;
2028 }
Adam Langley4bdb6e42015-05-15 15:29:21 -07002029 CRYPTO_MUTEX_unlock(&ctx->lock);
David Benjaminb6d0c6d2015-03-19 19:07:26 -04002030
2031 if (flush_cache) {
2032 SSL_CTX_flush_sessions(ctx, (unsigned long)time(NULL));
Adam Langleyfcf25832014-12-18 17:42:32 -08002033 }
2034 }
2035}
Adam Langley95c29f32014-06-20 12:00:00 -07002036
David Benjamin1a5c50f2015-03-11 16:22:37 -04002037int SSL_get_error(const SSL *s, int ret_code) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002038 int reason;
David Benjamin1a5c50f2015-03-11 16:22:37 -04002039 uint32_t err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002040 BIO *bio;
Adam Langley95c29f32014-06-20 12:00:00 -07002041
David Benjamin1a5c50f2015-03-11 16:22:37 -04002042 if (ret_code > 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002043 return SSL_ERROR_NONE;
2044 }
Adam Langley95c29f32014-06-20 12:00:00 -07002045
Adam Langleyfcf25832014-12-18 17:42:32 -08002046 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
2047 * where we do encode the error */
David Benjamin1a5c50f2015-03-11 16:22:37 -04002048 err = ERR_peek_error();
2049 if (err != 0) {
2050 if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002051 return SSL_ERROR_SYSCALL;
2052 }
2053 return SSL_ERROR_SSL;
2054 }
Adam Langley95c29f32014-06-20 12:00:00 -07002055
David Benjamin1a5c50f2015-03-11 16:22:37 -04002056 if (ret_code == 0) {
David Benjamin9a38e922015-01-22 16:06:11 -05002057 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2058 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) {
2059 /* The socket was cleanly shut down with a close_notify. */
2060 return SSL_ERROR_ZERO_RETURN;
2061 }
2062 /* An EOF was observed which violates the protocol, and the underlying
2063 * transport does not participate in the error queue. Bubble up to the
2064 * caller. */
2065 return SSL_ERROR_SYSCALL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002066 }
Adam Langleyb2ce0582014-06-20 12:00:00 -07002067
Adam Langleyfcf25832014-12-18 17:42:32 -08002068 if (SSL_want_session(s)) {
2069 return SSL_ERROR_PENDING_SESSION;
2070 }
Adam Langley95c29f32014-06-20 12:00:00 -07002071
Adam Langleyfcf25832014-12-18 17:42:32 -08002072 if (SSL_want_certificate(s)) {
2073 return SSL_ERROR_PENDING_CERTIFICATE;
2074 }
Adam Langley95c29f32014-06-20 12:00:00 -07002075
Adam Langleyfcf25832014-12-18 17:42:32 -08002076 if (SSL_want_read(s)) {
2077 bio = SSL_get_rbio(s);
2078 if (BIO_should_read(bio)) {
2079 return SSL_ERROR_WANT_READ;
2080 }
Adam Langley95c29f32014-06-20 12:00:00 -07002081
Adam Langleyfcf25832014-12-18 17:42:32 -08002082 if (BIO_should_write(bio)) {
2083 /* This one doesn't make too much sense ... We never try to write to the
2084 * rbio, and an application program where rbio and wbio are separate
2085 * couldn't even know what it should wait for. However if we ever set
2086 * s->rwstate incorrectly (so that we have SSL_want_read(s) instead of
2087 * SSL_want_write(s)) and rbio and wbio *are* the same, this test works
2088 * around that bug; so it might be safer to keep it. */
2089 return SSL_ERROR_WANT_WRITE;
2090 }
Adam Langley95c29f32014-06-20 12:00:00 -07002091
Adam Langleyfcf25832014-12-18 17:42:32 -08002092 if (BIO_should_io_special(bio)) {
2093 reason = BIO_get_retry_reason(bio);
2094 if (reason == BIO_RR_CONNECT) {
2095 return SSL_ERROR_WANT_CONNECT;
2096 }
Adam Langley95c29f32014-06-20 12:00:00 -07002097
Adam Langleyfcf25832014-12-18 17:42:32 -08002098 if (reason == BIO_RR_ACCEPT) {
2099 return SSL_ERROR_WANT_ACCEPT;
2100 }
Adam Langley95c29f32014-06-20 12:00:00 -07002101
Adam Langleyfcf25832014-12-18 17:42:32 -08002102 return SSL_ERROR_SYSCALL; /* unknown */
2103 }
2104 }
Adam Langley95c29f32014-06-20 12:00:00 -07002105
Adam Langleyfcf25832014-12-18 17:42:32 -08002106 if (SSL_want_write(s)) {
2107 bio = SSL_get_wbio(s);
2108 if (BIO_should_write(bio)) {
2109 return SSL_ERROR_WANT_WRITE;
2110 }
Adam Langley95c29f32014-06-20 12:00:00 -07002111
Adam Langleyfcf25832014-12-18 17:42:32 -08002112 if (BIO_should_read(bio)) {
2113 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2114 return SSL_ERROR_WANT_READ;
2115 }
Adam Langley95c29f32014-06-20 12:00:00 -07002116
Adam Langleyfcf25832014-12-18 17:42:32 -08002117 if (BIO_should_io_special(bio)) {
2118 reason = BIO_get_retry_reason(bio);
2119 if (reason == BIO_RR_CONNECT) {
2120 return SSL_ERROR_WANT_CONNECT;
2121 }
Adam Langley95c29f32014-06-20 12:00:00 -07002122
Adam Langleyfcf25832014-12-18 17:42:32 -08002123 if (reason == BIO_RR_ACCEPT) {
2124 return SSL_ERROR_WANT_ACCEPT;
2125 }
Adam Langley95c29f32014-06-20 12:00:00 -07002126
Adam Langleyfcf25832014-12-18 17:42:32 -08002127 return SSL_ERROR_SYSCALL;
2128 }
2129 }
Adam Langley95c29f32014-06-20 12:00:00 -07002130
Adam Langleyfcf25832014-12-18 17:42:32 -08002131 if (SSL_want_x509_lookup(s)) {
2132 return SSL_ERROR_WANT_X509_LOOKUP;
2133 }
Adam Langley95c29f32014-06-20 12:00:00 -07002134
Adam Langleyfcf25832014-12-18 17:42:32 -08002135 if (SSL_want_channel_id_lookup(s)) {
2136 return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
2137 }
Adam Langley0f4746e2014-08-13 12:26:32 -07002138
Adam Langleyfcf25832014-12-18 17:42:32 -08002139 return SSL_ERROR_SYSCALL;
2140}
Adam Langley0f4746e2014-08-13 12:26:32 -07002141
Adam Langleyfcf25832014-12-18 17:42:32 -08002142int SSL_do_handshake(SSL *s) {
2143 int ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002144
Adam Langleyfcf25832014-12-18 17:42:32 -08002145 if (s->handshake_func == NULL) {
2146 OPENSSL_PUT_ERROR(SSL, SSL_do_handshake, SSL_R_CONNECTION_TYPE_NOT_SET);
2147 return -1;
2148 }
Adam Langley95c29f32014-06-20 12:00:00 -07002149
Adam Langleyfcf25832014-12-18 17:42:32 -08002150 if (SSL_in_init(s)) {
2151 ret = s->handshake_func(s);
2152 }
2153 return ret;
2154}
Adam Langley95c29f32014-06-20 12:00:00 -07002155
David Benjamin4831c332015-05-16 11:43:13 -04002156void SSL_set_accept_state(SSL *ssl) {
2157 ssl->server = 1;
2158 ssl->shutdown = 0;
David Benjamin8ec88102015-05-15 23:40:13 -04002159 ssl->state = SSL_ST_ACCEPT;
David Benjamin4831c332015-05-16 11:43:13 -04002160 ssl->handshake_func = ssl->method->ssl_accept;
Adam Langleyfcf25832014-12-18 17:42:32 -08002161 /* clear the current cipher */
David Benjamin4831c332015-05-16 11:43:13 -04002162 ssl_clear_cipher_ctx(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08002163}
Adam Langley95c29f32014-06-20 12:00:00 -07002164
David Benjamin4831c332015-05-16 11:43:13 -04002165void SSL_set_connect_state(SSL *ssl) {
2166 ssl->server = 0;
2167 ssl->shutdown = 0;
David Benjamin8ec88102015-05-15 23:40:13 -04002168 ssl->state = SSL_ST_CONNECT;
David Benjamin4831c332015-05-16 11:43:13 -04002169 ssl->handshake_func = ssl->method->ssl_connect;
Adam Langleyfcf25832014-12-18 17:42:32 -08002170 /* clear the current cipher */
David Benjamin4831c332015-05-16 11:43:13 -04002171 ssl_clear_cipher_ctx(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08002172}
Adam Langley95c29f32014-06-20 12:00:00 -07002173
Adam Langleyfcf25832014-12-18 17:42:32 -08002174static const char *ssl_get_version(int version) {
2175 switch (version) {
2176 case TLS1_2_VERSION:
2177 return "TLSv1.2";
Adam Langley95c29f32014-06-20 12:00:00 -07002178
Adam Langleyfcf25832014-12-18 17:42:32 -08002179 case TLS1_1_VERSION:
2180 return "TLSv1.1";
Adam Langley95c29f32014-06-20 12:00:00 -07002181
Adam Langleyfcf25832014-12-18 17:42:32 -08002182 case TLS1_VERSION:
2183 return "TLSv1";
Adam Langley95c29f32014-06-20 12:00:00 -07002184
Adam Langleyfcf25832014-12-18 17:42:32 -08002185 case SSL3_VERSION:
2186 return "SSLv3";
Adam Langley95c29f32014-06-20 12:00:00 -07002187
David Benjamin1c722b72015-04-20 13:53:10 -04002188 case DTLS1_VERSION:
2189 return "DTLSv1";
2190
2191 case DTLS1_2_VERSION:
2192 return "DTLSv1.2";
2193
Adam Langleyfcf25832014-12-18 17:42:32 -08002194 default:
2195 return "unknown";
2196 }
2197}
Adam Langley95c29f32014-06-20 12:00:00 -07002198
Adam Langleyfcf25832014-12-18 17:42:32 -08002199const char *SSL_get_version(const SSL *s) {
2200 return ssl_get_version(s->version);
2201}
Adam Langley95c29f32014-06-20 12:00:00 -07002202
Adam Langleyfcf25832014-12-18 17:42:32 -08002203const char *SSL_SESSION_get_version(const SSL_SESSION *sess) {
2204 return ssl_get_version(sess->ssl_version);
2205}
Adam Langley95c29f32014-06-20 12:00:00 -07002206
Adam Langleyfcf25832014-12-18 17:42:32 -08002207void ssl_clear_cipher_ctx(SSL *s) {
David Benjamin31a07792015-03-03 14:20:26 -05002208 SSL_AEAD_CTX_free(s->aead_read_ctx);
2209 s->aead_read_ctx = NULL;
2210 SSL_AEAD_CTX_free(s->aead_write_ctx);
2211 s->aead_write_ctx = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002212}
Adam Langley95c29f32014-06-20 12:00:00 -07002213
Adam Langleyfcf25832014-12-18 17:42:32 -08002214X509 *SSL_get_certificate(const SSL *s) {
2215 if (s->cert != NULL) {
2216 return s->cert->key->x509;
2217 }
2218
2219 return NULL;
2220}
2221
2222EVP_PKEY *SSL_get_privatekey(const SSL *s) {
2223 if (s->cert != NULL) {
2224 return s->cert->key->privatekey;
2225 }
2226
2227 return NULL;
2228}
2229
2230X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
2231 if (ctx->cert != NULL) {
2232 return ctx->cert->key->x509;
2233 }
2234
2235 return NULL;
2236}
2237
2238EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
2239 if (ctx->cert != NULL) {
2240 return ctx->cert->key->privatekey;
2241 }
2242
2243 return NULL;
2244}
2245
2246const SSL_CIPHER *SSL_get_current_cipher(const SSL *s) {
David Benjamina07c0fc2015-05-13 13:19:42 -04002247 if (s->aead_write_ctx == NULL) {
2248 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002249 }
David Benjamina07c0fc2015-05-13 13:19:42 -04002250 return s->aead_write_ctx->cipher;
Adam Langleyfcf25832014-12-18 17:42:32 -08002251}
2252
2253const void *SSL_get_current_compression(SSL *s) { return NULL; }
2254
2255const void *SSL_get_current_expansion(SSL *s) { return NULL; }
2256
2257int ssl_init_wbio_buffer(SSL *s, int push) {
2258 BIO *bbio;
2259
2260 if (s->bbio == NULL) {
2261 bbio = BIO_new(BIO_f_buffer());
2262 if (bbio == NULL) {
2263 return 0;
2264 }
2265 s->bbio = bbio;
2266 } else {
2267 bbio = s->bbio;
2268 if (s->bbio == s->wbio) {
2269 s->wbio = BIO_pop(s->wbio);
2270 }
2271 }
2272
2273 BIO_reset(bbio);
2274 if (!BIO_set_read_buffer_size(bbio, 1)) {
2275 OPENSSL_PUT_ERROR(SSL, ssl_init_wbio_buffer, ERR_R_BUF_LIB);
2276 return 0;
2277 }
2278
2279 if (push) {
2280 if (s->wbio != bbio) {
2281 s->wbio = BIO_push(bbio, s->wbio);
2282 }
2283 } else {
2284 if (s->wbio == bbio) {
2285 s->wbio = BIO_pop(bbio);
2286 }
2287 }
2288
2289 return 1;
2290}
2291
2292void ssl_free_wbio_buffer(SSL *s) {
2293 if (s->bbio == NULL) {
2294 return;
2295 }
2296
2297 if (s->bbio == s->wbio) {
2298 /* remove buffering */
2299 s->wbio = BIO_pop(s->wbio);
2300 }
2301
2302 BIO_free(s->bbio);
2303 s->bbio = NULL;
2304}
2305
2306void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
2307 ctx->quiet_shutdown = mode;
2308}
2309
2310int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
2311 return ctx->quiet_shutdown;
2312}
2313
2314void SSL_set_quiet_shutdown(SSL *s, int mode) { s->quiet_shutdown = mode; }
2315
2316int SSL_get_quiet_shutdown(const SSL *s) { return s->quiet_shutdown; }
2317
2318void SSL_set_shutdown(SSL *s, int mode) { s->shutdown = mode; }
2319
2320int SSL_get_shutdown(const SSL *s) { return s->shutdown; }
2321
2322int SSL_version(const SSL *s) { return s->version; }
2323
2324SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
2325
2326SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
2327 if (ssl->ctx == ctx) {
2328 return ssl->ctx;
2329 }
2330
2331 if (ctx == NULL) {
2332 ctx = ssl->initial_ctx;
2333 }
2334
David Benjamin2755a3e2015-04-22 16:17:58 -04002335 ssl_cert_free(ssl->cert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002336 ssl->cert = ssl_cert_dup(ctx->cert);
David Benjamin2755a3e2015-04-22 16:17:58 -04002337
Adam Langley0b5e3902015-05-15 13:08:38 -07002338 CRYPTO_refcount_inc(&ctx->references);
David Benjamin2755a3e2015-04-22 16:17:58 -04002339 SSL_CTX_free(ssl->ctx); /* decrement reference count */
Adam Langleyfcf25832014-12-18 17:42:32 -08002340 ssl->ctx = ctx;
2341
2342 ssl->sid_ctx_length = ctx->sid_ctx_length;
2343 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2344 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2345
2346 return ssl->ctx;
2347}
2348
2349int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
2350 return X509_STORE_set_default_paths(ctx->cert_store);
2351}
Adam Langley95c29f32014-06-20 12:00:00 -07002352
2353int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
Adam Langleyfcf25832014-12-18 17:42:32 -08002354 const char *CApath) {
2355 return X509_STORE_load_locations(ctx->cert_store, CAfile, CApath);
2356}
Adam Langley95c29f32014-06-20 12:00:00 -07002357
2358void SSL_set_info_callback(SSL *ssl,
Adam Langleyfcf25832014-12-18 17:42:32 -08002359 void (*cb)(const SSL *ssl, int type, int val)) {
2360 ssl->info_callback = cb;
2361}
Adam Langley95c29f32014-06-20 12:00:00 -07002362
Adam Langleyfcf25832014-12-18 17:42:32 -08002363void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/, int /*type*/,
2364 int /*val*/) {
2365 return ssl->info_callback;
2366}
Adam Langley95c29f32014-06-20 12:00:00 -07002367
Adam Langleyfcf25832014-12-18 17:42:32 -08002368int SSL_state(const SSL *ssl) { return ssl->state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002369
David Benjaminece089c2015-05-15 23:52:42 -04002370void SSL_set_state(SSL *ssl, int state) { }
Adam Langley95c29f32014-06-20 12:00:00 -07002371
Adam Langleyfcf25832014-12-18 17:42:32 -08002372void SSL_set_verify_result(SSL *ssl, long arg) { ssl->verify_result = arg; }
Adam Langley95c29f32014-06-20 12:00:00 -07002373
Adam Langleyfcf25832014-12-18 17:42:32 -08002374long SSL_get_verify_result(const SSL *ssl) { return ssl->verify_result; }
Adam Langley95c29f32014-06-20 12:00:00 -07002375
Adam Langleyfcf25832014-12-18 17:42:32 -08002376int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2377 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002378 int index;
2379 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp,
2380 new_func, dup_func, free_func)) {
2381 return -1;
2382 }
2383 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002384}
Adam Langley95c29f32014-06-20 12:00:00 -07002385
Adam Langleyfcf25832014-12-18 17:42:32 -08002386int SSL_set_ex_data(SSL *s, int idx, void *arg) {
2387 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2388}
Adam Langley95c29f32014-06-20 12:00:00 -07002389
Adam Langleyfcf25832014-12-18 17:42:32 -08002390void *SSL_get_ex_data(const SSL *s, int idx) {
2391 return CRYPTO_get_ex_data(&s->ex_data, idx);
2392}
Adam Langley95c29f32014-06-20 12:00:00 -07002393
Adam Langleyfcf25832014-12-18 17:42:32 -08002394int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2395 CRYPTO_EX_dup *dup_func,
2396 CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002397 int index;
2398 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp,
2399 new_func, dup_func, free_func)) {
2400 return -1;
2401 }
2402 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002403}
Adam Langley95c29f32014-06-20 12:00:00 -07002404
Adam Langleyfcf25832014-12-18 17:42:32 -08002405int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg) {
2406 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2407}
Adam Langley95c29f32014-06-20 12:00:00 -07002408
Adam Langleyfcf25832014-12-18 17:42:32 -08002409void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx) {
2410 return CRYPTO_get_ex_data(&s->ex_data, idx);
2411}
Adam Langley95c29f32014-06-20 12:00:00 -07002412
Adam Langleyfcf25832014-12-18 17:42:32 -08002413X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
2414 return ctx->cert_store;
2415}
Adam Langley95c29f32014-06-20 12:00:00 -07002416
Adam Langleyfcf25832014-12-18 17:42:32 -08002417void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
David Benjamin2755a3e2015-04-22 16:17:58 -04002418 X509_STORE_free(ctx->cert_store);
Adam Langleyfcf25832014-12-18 17:42:32 -08002419 ctx->cert_store = store;
2420}
Adam Langley95c29f32014-06-20 12:00:00 -07002421
Adam Langleyfcf25832014-12-18 17:42:32 -08002422int SSL_want(const SSL *s) { return s->rwstate; }
Adam Langley95c29f32014-06-20 12:00:00 -07002423
Adam Langleyfcf25832014-12-18 17:42:32 -08002424void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
2425 RSA *(*cb)(SSL *ssl, int is_export,
2426 int keylength)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002427}
Adam Langley95c29f32014-06-20 12:00:00 -07002428
Adam Langleyfcf25832014-12-18 17:42:32 -08002429void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
2430 int keylength)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002431}
2432
2433void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
David Benjamin59015c32015-04-26 13:13:55 -04002434 DH *(*callback)(SSL *ssl, int is_export,
2435 int keylength)) {
2436 ctx->cert->dh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002437}
2438
David Benjamin59015c32015-04-26 13:13:55 -04002439void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*callback)(SSL *ssl, int is_export,
2440 int keylength)) {
2441 ssl->cert->dh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002442}
2443
2444void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,
David Benjamindd978782015-04-24 15:20:13 -04002445 EC_KEY *(*callback)(SSL *ssl, int is_export,
2446 int keylength)) {
David Benjamin59015c32015-04-26 13:13:55 -04002447 ctx->cert->ecdh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002448}
2449
2450void SSL_set_tmp_ecdh_callback(SSL *ssl,
David Benjamindd978782015-04-24 15:20:13 -04002451 EC_KEY *(*callback)(SSL *ssl, int is_export,
2452 int keylength)) {
David Benjamin59015c32015-04-26 13:13:55 -04002453 ssl->cert->ecdh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002454}
2455
2456int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
2457 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2458 OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_psk_identity_hint,
2459 SSL_R_DATA_LENGTH_TOO_LONG);
2460 return 0;
2461 }
2462
David Benjamin2755a3e2015-04-22 16:17:58 -04002463 OPENSSL_free(ctx->psk_identity_hint);
Adam Langleyfcf25832014-12-18 17:42:32 -08002464
2465 if (identity_hint != NULL) {
2466 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2467 if (ctx->psk_identity_hint == NULL) {
2468 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002469 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002470 } else {
2471 ctx->psk_identity_hint = NULL;
2472 }
Adam Langley95c29f32014-06-20 12:00:00 -07002473
Adam Langleyfcf25832014-12-18 17:42:32 -08002474 return 1;
2475}
2476
2477int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint) {
2478 if (s == NULL) {
2479 return 0;
2480 }
2481
2482 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2483 OPENSSL_PUT_ERROR(SSL, SSL_use_psk_identity_hint,
2484 SSL_R_DATA_LENGTH_TOO_LONG);
2485 return 0;
2486 }
2487
2488 /* Clear currently configured hint, if any. */
David Benjamin2755a3e2015-04-22 16:17:58 -04002489 OPENSSL_free(s->psk_identity_hint);
2490 s->psk_identity_hint = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002491
2492 if (identity_hint != NULL) {
2493 s->psk_identity_hint = BUF_strdup(identity_hint);
2494 if (s->psk_identity_hint == NULL) {
2495 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002496 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002497 }
Adam Langley95c29f32014-06-20 12:00:00 -07002498
Adam Langleyfcf25832014-12-18 17:42:32 -08002499 return 1;
2500}
Adam Langley95c29f32014-06-20 12:00:00 -07002501
Adam Langleyfcf25832014-12-18 17:42:32 -08002502const char *SSL_get_psk_identity_hint(const SSL *s) {
2503 if (s == NULL) {
2504 return NULL;
2505 }
2506 return s->psk_identity_hint;
2507}
Adam Langley95c29f32014-06-20 12:00:00 -07002508
Adam Langleyfcf25832014-12-18 17:42:32 -08002509const char *SSL_get_psk_identity(const SSL *s) {
2510 if (s == NULL || s->session == NULL) {
2511 return NULL;
2512 }
Adam Langley95c29f32014-06-20 12:00:00 -07002513
Adam Langleyfcf25832014-12-18 17:42:32 -08002514 return s->session->psk_identity;
2515}
Adam Langley95c29f32014-06-20 12:00:00 -07002516
Adam Langleyfcf25832014-12-18 17:42:32 -08002517void SSL_set_psk_client_callback(
2518 SSL *s, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2519 unsigned int max_identity_len, uint8_t *psk,
2520 unsigned int max_psk_len)) {
2521 s->psk_client_callback = cb;
2522}
Adam Langley95c29f32014-06-20 12:00:00 -07002523
Adam Langleyfcf25832014-12-18 17:42:32 -08002524void SSL_CTX_set_psk_client_callback(
2525 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2526 unsigned int max_identity_len,
2527 uint8_t *psk, unsigned int max_psk_len)) {
2528 ctx->psk_client_callback = cb;
2529}
Adam Langley95c29f32014-06-20 12:00:00 -07002530
Adam Langleyfcf25832014-12-18 17:42:32 -08002531void SSL_set_psk_server_callback(
2532 SSL *s, unsigned int (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
2533 unsigned int max_psk_len)) {
2534 s->psk_server_callback = cb;
2535}
Adam Langley95c29f32014-06-20 12:00:00 -07002536
Adam Langleyfcf25832014-12-18 17:42:32 -08002537void SSL_CTX_set_psk_server_callback(
2538 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *identity,
2539 uint8_t *psk, unsigned int max_psk_len)) {
2540 ctx->psk_server_callback = cb;
2541}
Adam Langley95c29f32014-06-20 12:00:00 -07002542
Adam Langleyfcf25832014-12-18 17:42:32 -08002543void SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) {
2544 ctx->min_version = version;
2545}
Adam Langley95c29f32014-06-20 12:00:00 -07002546
Adam Langleyfcf25832014-12-18 17:42:32 -08002547void SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) {
2548 ctx->max_version = version;
2549}
Adam Langley0289c732014-06-20 12:00:00 -07002550
Adam Langleyfcf25832014-12-18 17:42:32 -08002551void SSL_set_min_version(SSL *ssl, uint16_t version) {
2552 ssl->min_version = version;
2553}
Adam Langley0289c732014-06-20 12:00:00 -07002554
Adam Langleyfcf25832014-12-18 17:42:32 -08002555void SSL_set_max_version(SSL *ssl, uint16_t version) {
2556 ssl->max_version = version;
2557}
Adam Langley95c29f32014-06-20 12:00:00 -07002558
Adam Langleyfcf25832014-12-18 17:42:32 -08002559void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
2560 void (*cb)(int write_p, int version,
2561 int content_type, const void *buf,
2562 size_t len, SSL *ssl, void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002563 ctx->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002564}
David Benjamin61ecccf2015-05-05 09:44:51 -04002565
2566void SSL_CTX_set_msg_callback_arg(SSL_CTX *ctx, void *arg) {
2567 ctx->msg_callback_arg = arg;
2568}
2569
Adam Langleyfcf25832014-12-18 17:42:32 -08002570void SSL_set_msg_callback(SSL *ssl,
2571 void (*cb)(int write_p, int version, int content_type,
2572 const void *buf, size_t len, SSL *ssl,
2573 void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002574 ssl->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002575}
Adam Langley95c29f32014-06-20 12:00:00 -07002576
David Benjamin61ecccf2015-05-05 09:44:51 -04002577void SSL_set_msg_callback_arg(SSL *ssl, void *arg) {
2578 ssl->msg_callback_arg = arg;
2579}
2580
Adam Langleyfcf25832014-12-18 17:42:32 -08002581void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio) {
David Benjamin2755a3e2015-04-22 16:17:58 -04002582 BIO_free(ctx->keylog_bio);
Adam Langleyfcf25832014-12-18 17:42:32 -08002583 ctx->keylog_bio = keylog_bio;
2584}
Adam Langley95c29f32014-06-20 12:00:00 -07002585
Adam Langleyfcf25832014-12-18 17:42:32 -08002586static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
2587 static const char hextable[] = "0123456789abcdef";
2588 uint8_t *out;
2589 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002590
Adam Langleyfcf25832014-12-18 17:42:32 -08002591 if (!CBB_add_space(cbb, &out, in_len * 2)) {
2592 return 0;
2593 }
Adam Langley95c29f32014-06-20 12:00:00 -07002594
Adam Langleyfcf25832014-12-18 17:42:32 -08002595 for (i = 0; i < in_len; i++) {
2596 *(out++) = (uint8_t)hextable[in[i] >> 4];
2597 *(out++) = (uint8_t)hextable[in[i] & 0xf];
2598 }
Adam Langley95c29f32014-06-20 12:00:00 -07002599
Adam Langleyfcf25832014-12-18 17:42:32 -08002600 return 1;
2601}
David Benjamin859ec3c2014-09-02 16:29:36 -04002602
2603int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -08002604 const uint8_t *encrypted_premaster,
2605 size_t encrypted_premaster_len,
2606 const uint8_t *premaster,
2607 size_t premaster_len) {
2608 BIO *bio = ctx->keylog_bio;
2609 CBB cbb;
2610 uint8_t *out;
2611 size_t out_len;
2612 int ret;
David Benjamin859ec3c2014-09-02 16:29:36 -04002613
Adam Langleyfcf25832014-12-18 17:42:32 -08002614 if (bio == NULL) {
2615 return 1;
2616 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002617
Adam Langleyfcf25832014-12-18 17:42:32 -08002618 if (encrypted_premaster_len < 8) {
2619 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_rsa_client_key_exchange,
2620 ERR_R_INTERNAL_ERROR);
2621 return 0;
2622 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002623
Adam Langleyfcf25832014-12-18 17:42:32 -08002624 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len * 2 + 1)) {
2625 return 0;
2626 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002627
Adam Langleyfcf25832014-12-18 17:42:32 -08002628 if (!CBB_add_bytes(&cbb, (const uint8_t *)"RSA ", 4) ||
2629 /* Only the first 8 bytes of the encrypted premaster secret are
2630 * logged. */
2631 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
2632 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2633 !cbb_add_hex(&cbb, premaster, premaster_len) ||
2634 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2635 !CBB_finish(&cbb, &out, &out_len)) {
2636 CBB_cleanup(&cbb);
2637 return 0;
2638 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002639
Adam Langley4bdb6e42015-05-15 15:29:21 -07002640 CRYPTO_MUTEX_lock_write(&ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -08002641 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
Adam Langley4bdb6e42015-05-15 15:29:21 -07002642 CRYPTO_MUTEX_unlock(&ctx->lock);
David Benjamin859ec3c2014-09-02 16:29:36 -04002643
Adam Langleyfcf25832014-12-18 17:42:32 -08002644 OPENSSL_free(out);
2645 return ret;
Adam Langley95f22882014-06-20 12:00:00 -07002646}
2647
Adam Langleyfcf25832014-12-18 17:42:32 -08002648int ssl_ctx_log_master_secret(SSL_CTX *ctx, const uint8_t *client_random,
2649 size_t client_random_len, const uint8_t *master,
2650 size_t master_len) {
2651 BIO *bio = ctx->keylog_bio;
2652 CBB cbb;
2653 uint8_t *out;
2654 size_t out_len;
2655 int ret;
Adam Langleyadb739e2014-06-20 12:00:00 -07002656
Adam Langleyfcf25832014-12-18 17:42:32 -08002657 if (bio == NULL) {
2658 return 1;
2659 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002660
Adam Langleyfcf25832014-12-18 17:42:32 -08002661 if (client_random_len != 32) {
2662 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_master_secret, ERR_R_INTERNAL_ERROR);
2663 return 0;
2664 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002665
Adam Langleyfcf25832014-12-18 17:42:32 -08002666 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len * 2 + 1)) {
2667 return 0;
2668 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002669
Adam Langleyfcf25832014-12-18 17:42:32 -08002670 if (!CBB_add_bytes(&cbb, (const uint8_t *)"CLIENT_RANDOM ", 14) ||
2671 !cbb_add_hex(&cbb, client_random, 32) ||
2672 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2673 !cbb_add_hex(&cbb, master, master_len) ||
2674 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2675 !CBB_finish(&cbb, &out, &out_len)) {
2676 CBB_cleanup(&cbb);
2677 return 0;
2678 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002679
Adam Langley4bdb6e42015-05-15 15:29:21 -07002680 CRYPTO_MUTEX_lock_write(&ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -08002681 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
Adam Langley4bdb6e42015-05-15 15:29:21 -07002682 CRYPTO_MUTEX_unlock(&ctx->lock);
David Benjamine99e9122014-12-11 01:46:01 -05002683
Adam Langleyfcf25832014-12-18 17:42:32 -08002684 OPENSSL_free(out);
2685 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -07002686}
2687
David Benjamined7c4752015-02-16 19:16:46 -05002688int SSL_in_false_start(const SSL *s) {
2689 return s->s3->tmp.in_false_start;
2690}
2691
Adam Langleyfcf25832014-12-18 17:42:32 -08002692int SSL_cutthrough_complete(const SSL *s) {
David Benjamined7c4752015-02-16 19:16:46 -05002693 return SSL_in_false_start(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002694}
Adam Langley95c29f32014-06-20 12:00:00 -07002695
Adam Langleyfcf25832014-12-18 17:42:32 -08002696void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
2697 size_t *ssl_session_size) {
2698 *ssl_size = sizeof(SSL);
2699 *ssl_ctx_size = sizeof(SSL_CTX);
2700 *ssl_session_size = sizeof(SSL_SESSION);
2701}
Feng Lu41aa3252014-11-21 22:47:56 -08002702
David Benjamined7c4752015-02-16 19:16:46 -05002703int ssl3_can_false_start(const SSL *s) {
David Benjamin195dc782015-02-19 13:27:05 -05002704 const SSL_CIPHER *const cipher = SSL_get_current_cipher(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002705
Adam Langleye631d962015-04-01 13:11:01 -07002706 /* False Start only for TLS 1.2 with an ECDHE+AEAD cipher and ALPN or NPN. */
David Benjamin195dc782015-02-19 13:27:05 -05002707 return !SSL_IS_DTLS(s) &&
2708 SSL_version(s) >= TLS1_2_VERSION &&
2709 (s->s3->alpn_selected || s->s3->next_proto_neg_seen) &&
2710 cipher != NULL &&
Adam Langleye631d962015-04-01 13:11:01 -07002711 cipher->algorithm_mkey == SSL_kECDHE &&
David Benjamin195dc782015-02-19 13:27:05 -05002712 (cipher->algorithm_enc == SSL_AES128GCM ||
2713 cipher->algorithm_enc == SSL_AES256GCM ||
2714 cipher->algorithm_enc == SSL_CHACHA20POLY1305);
Adam Langleyfcf25832014-12-18 17:42:32 -08002715}
2716
2717const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version) {
2718 switch (version) {
2719 case SSL3_VERSION:
2720 return &SSLv3_enc_data;
2721
2722 case TLS1_VERSION:
2723 return &TLSv1_enc_data;
2724
David Benjamin9e13e1a2015-03-05 01:56:32 -05002725 case DTLS1_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002726 case TLS1_1_VERSION:
2727 return &TLSv1_1_enc_data;
2728
David Benjamin9e13e1a2015-03-05 01:56:32 -05002729 case DTLS1_2_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002730 case TLS1_2_VERSION:
2731 return &TLSv1_2_enc_data;
2732
Adam Langleyfcf25832014-12-18 17:42:32 -08002733 default:
2734 return NULL;
2735 }
2736}
2737
2738uint16_t ssl3_get_max_server_version(const SSL *s) {
2739 uint16_t max_version;
2740
2741 if (SSL_IS_DTLS(s)) {
2742 max_version = (s->max_version != 0) ? s->max_version : DTLS1_2_VERSION;
2743 if (!(s->options & SSL_OP_NO_DTLSv1_2) && DTLS1_2_VERSION >= max_version) {
2744 return DTLS1_2_VERSION;
2745 }
2746 if (!(s->options & SSL_OP_NO_DTLSv1) && DTLS1_VERSION >= max_version) {
2747 return DTLS1_VERSION;
2748 }
2749 return 0;
2750 }
2751
2752 max_version = (s->max_version != 0) ? s->max_version : TLS1_2_VERSION;
2753 if (!(s->options & SSL_OP_NO_TLSv1_2) && TLS1_2_VERSION <= max_version) {
2754 return TLS1_2_VERSION;
2755 }
2756 if (!(s->options & SSL_OP_NO_TLSv1_1) && TLS1_1_VERSION <= max_version) {
2757 return TLS1_1_VERSION;
2758 }
2759 if (!(s->options & SSL_OP_NO_TLSv1) && TLS1_VERSION <= max_version) {
2760 return TLS1_VERSION;
2761 }
2762 if (!(s->options & SSL_OP_NO_SSLv3) && SSL3_VERSION <= max_version) {
2763 return SSL3_VERSION;
2764 }
2765 return 0;
2766}
2767
2768uint16_t ssl3_get_mutual_version(SSL *s, uint16_t client_version) {
2769 uint16_t version = 0;
2770
2771 if (SSL_IS_DTLS(s)) {
2772 /* Clamp client_version to max_version. */
2773 if (s->max_version != 0 && client_version < s->max_version) {
2774 client_version = s->max_version;
2775 }
2776
2777 if (client_version <= DTLS1_2_VERSION && !(s->options & SSL_OP_NO_DTLSv1_2)) {
2778 version = DTLS1_2_VERSION;
2779 } else if (client_version <= DTLS1_VERSION &&
David Benjaminb18f0242015-03-10 18:30:08 -04002780 !(s->options & SSL_OP_NO_DTLSv1)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002781 version = DTLS1_VERSION;
2782 }
2783
2784 /* Check against min_version. */
2785 if (version != 0 && s->min_version != 0 && version > s->min_version) {
2786 return 0;
2787 }
2788 return version;
2789 } else {
2790 /* Clamp client_version to max_version. */
2791 if (s->max_version != 0 && client_version > s->max_version) {
2792 client_version = s->max_version;
2793 }
2794
2795 if (client_version >= TLS1_2_VERSION && !(s->options & SSL_OP_NO_TLSv1_2)) {
2796 version = TLS1_2_VERSION;
2797 } else if (client_version >= TLS1_1_VERSION &&
2798 !(s->options & SSL_OP_NO_TLSv1_1)) {
2799 version = TLS1_1_VERSION;
2800 } else if (client_version >= TLS1_VERSION && !(s->options & SSL_OP_NO_TLSv1)) {
2801 version = TLS1_VERSION;
2802 } else if (client_version >= SSL3_VERSION && !(s->options & SSL_OP_NO_SSLv3)) {
2803 version = SSL3_VERSION;
2804 }
2805
2806 /* Check against min_version. */
2807 if (version != 0 && s->min_version != 0 && version < s->min_version) {
2808 return 0;
2809 }
2810 return version;
2811 }
2812}
2813
2814uint16_t ssl3_get_max_client_version(SSL *s) {
David Benjamin123a8fd2015-04-26 14:16:41 -04002815 uint32_t options = s->options;
Adam Langleyfcf25832014-12-18 17:42:32 -08002816 uint16_t version = 0;
2817
2818 /* OpenSSL's API for controlling versions entails blacklisting individual
2819 * protocols. This has two problems. First, on the client, the protocol can
2820 * only express a contiguous range of versions. Second, a library consumer
2821 * trying to set a maximum version cannot disable protocol versions that get
2822 * added in a future version of the library.
2823 *
2824 * To account for both of these, OpenSSL interprets the client-side bitmask
2825 * as a min/max range by picking the lowest contiguous non-empty range of
2826 * enabled protocols. Note that this means it is impossible to set a maximum
2827 * version of TLS 1.2 in a future-proof way.
2828 *
2829 * By this scheme, the maximum version is the lowest version V such that V is
2830 * enabled and V+1 is disabled or unimplemented. */
2831 if (SSL_IS_DTLS(s)) {
2832 if (!(options & SSL_OP_NO_DTLSv1_2)) {
2833 version = DTLS1_2_VERSION;
2834 }
2835 if (!(options & SSL_OP_NO_DTLSv1) && (options & SSL_OP_NO_DTLSv1_2)) {
2836 version = DTLS1_VERSION;
2837 }
2838 if (s->max_version != 0 && version < s->max_version) {
2839 version = s->max_version;
2840 }
2841 } else {
2842 if (!(options & SSL_OP_NO_TLSv1_2)) {
2843 version = TLS1_2_VERSION;
2844 }
2845 if (!(options & SSL_OP_NO_TLSv1_1) && (options & SSL_OP_NO_TLSv1_2)) {
2846 version = TLS1_1_VERSION;
2847 }
2848 if (!(options & SSL_OP_NO_TLSv1) && (options & SSL_OP_NO_TLSv1_1)) {
2849 version = TLS1_VERSION;
2850 }
2851 if (!(options & SSL_OP_NO_SSLv3) && (options & SSL_OP_NO_TLSv1)) {
2852 version = SSL3_VERSION;
2853 }
2854 if (s->max_version != 0 && version > s->max_version) {
2855 version = s->max_version;
2856 }
2857 }
2858
2859 return version;
2860}
2861
2862int ssl3_is_version_enabled(SSL *s, uint16_t version) {
2863 if (SSL_IS_DTLS(s)) {
2864 if (s->max_version != 0 && version < s->max_version) {
2865 return 0;
2866 }
2867 if (s->min_version != 0 && version > s->min_version) {
2868 return 0;
2869 }
2870
2871 switch (version) {
2872 case DTLS1_VERSION:
2873 return !(s->options & SSL_OP_NO_DTLSv1);
2874
2875 case DTLS1_2_VERSION:
2876 return !(s->options & SSL_OP_NO_DTLSv1_2);
2877
2878 default:
2879 return 0;
2880 }
2881 } else {
2882 if (s->max_version != 0 && version > s->max_version) {
2883 return 0;
2884 }
2885 if (s->min_version != 0 && version < s->min_version) {
2886 return 0;
2887 }
2888
2889 switch (version) {
2890 case SSL3_VERSION:
2891 return !(s->options & SSL_OP_NO_SSLv3);
2892
2893 case TLS1_VERSION:
2894 return !(s->options & SSL_OP_NO_TLSv1);
2895
2896 case TLS1_1_VERSION:
2897 return !(s->options & SSL_OP_NO_TLSv1_1);
2898
2899 case TLS1_2_VERSION:
2900 return !(s->options & SSL_OP_NO_TLSv1_2);
2901
2902 default:
2903 return 0;
2904 }
2905 }
2906}
2907
David Benjaminea72bd02014-12-21 21:27:41 -05002908uint16_t ssl3_version_from_wire(SSL *s, uint16_t wire_version) {
2909 if (!SSL_IS_DTLS(s)) {
2910 return wire_version;
2911 }
2912
2913 uint16_t tls_version = ~wire_version;
2914 uint16_t version = tls_version + 0x0201;
2915 /* If either component overflowed, clamp it so comparisons still work. */
2916 if ((version >> 8) < (tls_version >> 8)) {
2917 version = 0xff00 | (version & 0xff);
2918 }
2919 if ((version & 0xff) < (tls_version & 0xff)) {
2920 version = (version & 0xff00) | 0xff;
2921 }
2922 /* DTLS 1.0 maps to TLS 1.1, not TLS 1.0. */
2923 if (version == TLS1_VERSION) {
2924 version = TLS1_1_VERSION;
2925 }
2926 return version;
2927}
2928
Adam Langleyfcf25832014-12-18 17:42:32 -08002929int SSL_cache_hit(SSL *s) { return s->hit; }
2930
2931int SSL_is_server(SSL *s) { return s->server; }
2932
Adam Langley524e7172015-02-20 16:04:00 -08002933void SSL_CTX_set_dos_protection_cb(
2934 SSL_CTX *ctx, int (*cb)(const struct ssl_early_callback_ctx *)) {
2935 ctx->dos_protection_cb = cb;
2936}
2937
Adam Langleyfcf25832014-12-18 17:42:32 -08002938void SSL_enable_fastradio_padding(SSL *s, char on_off) {
2939 s->fastradio_padding = on_off;
2940}
Adam Langley44972942015-02-20 13:43:23 -08002941
David Benjaminb16346b2015-04-08 19:16:58 -04002942void SSL_set_reject_peer_renegotiations(SSL *s, int reject) {
David Benjamin897e5e02015-05-12 17:03:54 -04002943 s->accept_peer_renegotiations = !reject;
David Benjaminb16346b2015-04-08 19:16:58 -04002944}
2945
Adam Langley44972942015-02-20 13:43:23 -08002946const SSL_CIPHER *SSL_get_cipher_by_value(uint16_t value) {
2947 return ssl3_get_cipher_by_value(value);
2948}
Adam Langley3f92d212015-02-20 15:32:52 -08002949
2950int SSL_get_rc4_state(const SSL *ssl, const RC4_KEY **read_key,
2951 const RC4_KEY **write_key) {
2952 if (ssl->aead_read_ctx == NULL || ssl->aead_write_ctx == NULL) {
2953 return 0;
2954 }
2955
2956 return EVP_AEAD_CTX_get_rc4_state(&ssl->aead_read_ctx->ctx, read_key) &&
2957 EVP_AEAD_CTX_get_rc4_state(&ssl->aead_write_ctx->ctx, write_key);
2958}
David Benjaminda881e92015-04-26 14:45:04 -04002959
2960int SSL_CTX_sess_connect(const SSL_CTX *ctx) { return 0; }
2961int SSL_CTX_sess_connect_good(const SSL_CTX *ctx) { return 0; }
2962int SSL_CTX_sess_connect_renegotiate(const SSL_CTX *ctx) { return 0; }
2963int SSL_CTX_sess_accept(const SSL_CTX *ctx) { return 0; }
2964int SSL_CTX_sess_accept_renegotiate(const SSL_CTX *ctx) { return 0; }
2965int SSL_CTX_sess_accept_good(const SSL_CTX *ctx) { return 0; }
2966int SSL_CTX_sess_hits(const SSL_CTX *ctx) { return 0; }
2967int SSL_CTX_sess_cb_hits(const SSL_CTX *ctx) { return 0; }
2968int SSL_CTX_sess_misses(const SSL_CTX *ctx) { return 0; }
2969int SSL_CTX_sess_timeouts(const SSL_CTX *ctx) { return 0; }
2970int SSL_CTX_sess_cache_full(const SSL_CTX *ctx) { return 0; }