blob: 6af0040b3fcce386c33c257b60b976f5a3255a0d [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
David Benjamin97760d52015-07-24 23:02:49 -0400158/* |SSL_R_UKNOWN_PROTOCOL| is no longer emitted, but continue to define it
159 * to avoid downstream churn. */
160OPENSSL_DECLARE_ERROR_REASON(SSL, SSL_R_UKNOWN_PROTOCOL)
161
Adam Langleyfcf25832014-12-18 17:42:32 -0800162/* Some error codes are special. Ensure the make_errors.go script never
163 * regresses this. */
164OPENSSL_COMPILE_ASSERT(SSL_R_TLSV1_ALERT_NO_RENEGOTIATION ==
165 SSL_AD_NO_RENEGOTIATION + SSL_AD_REASON_OFFSET,
166 ssl_alert_reason_code_mismatch);
David Benjamine1136082014-09-20 12:28:58 -0400167
David Benjamin1d0a1942015-04-26 15:35:35 -0400168/* kMaxHandshakeSize is the maximum size, in bytes, of a handshake message. */
169static const size_t kMaxHandshakeSize = (1u << 24) - 1;
170
David Benjaminaa585132015-06-29 23:36:17 -0400171static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl =
172 CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
173static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl_ctx =
174 CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
David Benjamin9f33fc62015-04-15 17:29:53 -0400175
David Benjamin4831c332015-05-16 11:43:13 -0400176int SSL_clear(SSL *ssl) {
177 if (ssl->method == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400178 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_METHOD_SPECIFIED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800179 return 0;
180 }
Adam Langley95c29f32014-06-20 12:00:00 -0700181
David Benjamin4831c332015-05-16 11:43:13 -0400182 if (ssl_clear_bad_session(ssl)) {
183 SSL_SESSION_free(ssl->session);
184 ssl->session = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800185 }
Adam Langley95c29f32014-06-20 12:00:00 -0700186
David Benjamin4831c332015-05-16 11:43:13 -0400187 ssl->hit = 0;
188 ssl->shutdown = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700189
David Benjamin4831c332015-05-16 11:43:13 -0400190 /* SSL_clear may be called before or after the |ssl| is initialized in either
Adam Langleyfcf25832014-12-18 17:42:32 -0800191 * accept or connect state. In the latter case, SSL_clear should preserve the
David Benjamin4831c332015-05-16 11:43:13 -0400192 * half and reset |ssl->state| accordingly. */
193 if (ssl->handshake_func != NULL) {
194 if (ssl->server) {
195 SSL_set_accept_state(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800196 } else {
David Benjamin4831c332015-05-16 11:43:13 -0400197 SSL_set_connect_state(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800198 }
199 } else {
David Benjamin4831c332015-05-16 11:43:13 -0400200 assert(ssl->state == 0);
Adam Langleyfcf25832014-12-18 17:42:32 -0800201 }
Adam Langley95c29f32014-06-20 12:00:00 -0700202
David Benjamin5f387e32015-05-15 22:05:42 -0400203 /* TODO(davidben): Some state on |ssl| is reset both in |SSL_new| and
David Benjamin62fd1622015-01-11 13:30:01 -0500204 * |SSL_clear| because it is per-connection state rather than configuration
David Benjamin4831c332015-05-16 11:43:13 -0400205 * state. Per-connection state should be on |ssl->s3| and |ssl->d1| so it is
David Benjamin62fd1622015-01-11 13:30:01 -0500206 * naturally reset at the right points between |SSL_new|, |SSL_clear|, and
207 * |ssl3_new|. */
208
David Benjamin4831c332015-05-16 11:43:13 -0400209 ssl->rwstate = SSL_NOTHING;
210 ssl->rstate = SSL_ST_READ_HEADER;
Adam Langley95c29f32014-06-20 12:00:00 -0700211
David Benjamin4831c332015-05-16 11:43:13 -0400212 BUF_MEM_free(ssl->init_buf);
213 ssl->init_buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700214
David Benjamin4831c332015-05-16 11:43:13 -0400215 ssl->packet = NULL;
216 ssl->packet_length = 0;
David Benjamin62fd1622015-01-11 13:30:01 -0500217
David Benjamin4831c332015-05-16 11:43:13 -0400218 ssl_clear_cipher_ctx(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700219
David Benjamin4831c332015-05-16 11:43:13 -0400220 OPENSSL_free(ssl->next_proto_negotiated);
221 ssl->next_proto_negotiated = NULL;
222 ssl->next_proto_negotiated_len = 0;
David Benjamin62fd1622015-01-11 13:30:01 -0500223
David Benjamin4831c332015-05-16 11:43:13 -0400224 /* The ssl->d1->mtu is simultaneously configuration (preserved across
David Benjamin62fd1622015-01-11 13:30:01 -0500225 * clear) and connection-specific state (gets reset).
226 *
227 * TODO(davidben): Avoid this. */
228 unsigned mtu = 0;
David Benjamin4831c332015-05-16 11:43:13 -0400229 if (ssl->d1 != NULL) {
230 mtu = ssl->d1->mtu;
David Benjamin62fd1622015-01-11 13:30:01 -0500231 }
232
David Benjamin4831c332015-05-16 11:43:13 -0400233 ssl->method->ssl_free(ssl);
234 if (!ssl->method->ssl_new(ssl)) {
David Benjamin62fd1622015-01-11 13:30:01 -0500235 return 0;
236 }
David Benjamin4831c332015-05-16 11:43:13 -0400237 ssl->enc_method = ssl3_get_enc_method(ssl->version);
238 assert(ssl->enc_method != NULL);
David Benjamin62fd1622015-01-11 13:30:01 -0500239
David Benjamin4831c332015-05-16 11:43:13 -0400240 if (SSL_IS_DTLS(ssl) && (SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
241 ssl->d1->mtu = mtu;
David Benjamin62fd1622015-01-11 13:30:01 -0500242 }
243
David Benjamin4831c332015-05-16 11:43:13 -0400244 ssl->client_version = ssl->version;
Adam Langley95c29f32014-06-20 12:00:00 -0700245
Adam Langleyfcf25832014-12-18 17:42:32 -0800246 return 1;
247}
Adam Langley95c29f32014-06-20 12:00:00 -0700248
Adam Langleyfcf25832014-12-18 17:42:32 -0800249SSL *SSL_new(SSL_CTX *ctx) {
250 SSL *s;
Adam Langley95c29f32014-06-20 12:00:00 -0700251
Adam Langleyfcf25832014-12-18 17:42:32 -0800252 if (ctx == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400253 OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_CTX);
Adam Langleyfcf25832014-12-18 17:42:32 -0800254 return NULL;
255 }
256 if (ctx->method == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400257 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
Adam Langleyfcf25832014-12-18 17:42:32 -0800258 return NULL;
259 }
Adam Langley95c29f32014-06-20 12:00:00 -0700260
Adam Langleyfcf25832014-12-18 17:42:32 -0800261 s = (SSL *)OPENSSL_malloc(sizeof(SSL));
262 if (s == NULL) {
263 goto err;
264 }
265 memset(s, 0, sizeof(SSL));
Adam Langley95c29f32014-06-20 12:00:00 -0700266
Adam Langleyfcf25832014-12-18 17:42:32 -0800267 s->min_version = ctx->min_version;
268 s->max_version = ctx->max_version;
David Benjamin1eb367c2014-12-12 18:17:51 -0500269
Adam Langleyfcf25832014-12-18 17:42:32 -0800270 s->options = ctx->options;
271 s->mode = ctx->mode;
272 s->max_cert_list = ctx->max_cert_list;
Adam Langley95c29f32014-06-20 12:00:00 -0700273
David Benjamina5a3eeb2015-03-18 20:26:30 -0400274 s->cert = ssl_cert_dup(ctx->cert);
275 if (s->cert == NULL) {
276 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -0800277 }
Adam Langley95c29f32014-06-20 12:00:00 -0700278
Adam Langleyfcf25832014-12-18 17:42:32 -0800279 s->msg_callback = ctx->msg_callback;
280 s->msg_callback_arg = ctx->msg_callback_arg;
281 s->verify_mode = ctx->verify_mode;
282 s->sid_ctx_length = ctx->sid_ctx_length;
283 assert(s->sid_ctx_length <= sizeof s->sid_ctx);
284 memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));
285 s->verify_callback = ctx->default_verify_callback;
286 s->generate_session_id = ctx->generate_session_id;
Adam Langley95c29f32014-06-20 12:00:00 -0700287
Adam Langleyfcf25832014-12-18 17:42:32 -0800288 s->param = X509_VERIFY_PARAM_new();
289 if (!s->param) {
290 goto err;
291 }
292 X509_VERIFY_PARAM_inherit(s->param, ctx->param);
293 s->quiet_shutdown = ctx->quiet_shutdown;
294 s->max_send_fragment = ctx->max_send_fragment;
Adam Langley95c29f32014-06-20 12:00:00 -0700295
Adam Langley0b5e3902015-05-15 13:08:38 -0700296 CRYPTO_refcount_inc(&ctx->references);
Adam Langleyfcf25832014-12-18 17:42:32 -0800297 s->ctx = ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -0800298 s->tlsext_ticket_expected = 0;
Adam Langley0b5e3902015-05-15 13:08:38 -0700299 CRYPTO_refcount_inc(&ctx->references);
Adam Langleyfcf25832014-12-18 17:42:32 -0800300 s->initial_ctx = ctx;
Adam Langley95c29f32014-06-20 12:00:00 -0700301
Adam Langleyfcf25832014-12-18 17:42:32 -0800302 if (ctx->tlsext_ellipticcurvelist) {
303 s->tlsext_ellipticcurvelist =
304 BUF_memdup(ctx->tlsext_ellipticcurvelist,
305 ctx->tlsext_ellipticcurvelist_length * 2);
306 if (!s->tlsext_ellipticcurvelist) {
307 goto err;
308 }
309 s->tlsext_ellipticcurvelist_length = ctx->tlsext_ellipticcurvelist_length;
310 }
311 s->next_proto_negotiated = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700312
Adam Langleyfcf25832014-12-18 17:42:32 -0800313 if (s->ctx->alpn_client_proto_list) {
314 s->alpn_client_proto_list = BUF_memdup(s->ctx->alpn_client_proto_list,
315 s->ctx->alpn_client_proto_list_len);
316 if (s->alpn_client_proto_list == NULL) {
317 goto err;
318 }
319 s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;
320 }
Adam Langley95c29f32014-06-20 12:00:00 -0700321
Adam Langleyfcf25832014-12-18 17:42:32 -0800322 s->verify_result = X509_V_OK;
323 s->method = ctx->method;
Adam Langley95c29f32014-06-20 12:00:00 -0700324
Adam Langleyfcf25832014-12-18 17:42:32 -0800325 if (!s->method->ssl_new(s)) {
326 goto err;
327 }
328 s->enc_method = ssl3_get_enc_method(s->version);
329 assert(s->enc_method != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700330
David Benjamin62fd1622015-01-11 13:30:01 -0500331 s->rwstate = SSL_NOTHING;
332 s->rstate = SSL_ST_READ_HEADER;
Adam Langley95c29f32014-06-20 12:00:00 -0700333
David Benjamin9f33fc62015-04-15 17:29:53 -0400334 CRYPTO_new_ex_data(&g_ex_data_class_ssl, s, &s->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700335
Adam Langleyfcf25832014-12-18 17:42:32 -0800336 s->psk_identity_hint = NULL;
337 if (ctx->psk_identity_hint) {
338 s->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
339 if (s->psk_identity_hint == NULL) {
340 goto err;
341 }
342 }
343 s->psk_client_callback = ctx->psk_client_callback;
344 s->psk_server_callback = ctx->psk_server_callback;
Adam Langley95c29f32014-06-20 12:00:00 -0700345
David Benjamin02ddbfd2015-01-11 13:09:11 -0500346 s->tlsext_channel_id_enabled = ctx->tlsext_channel_id_enabled;
347 if (ctx->tlsext_channel_id_private) {
David Benjamin9a10f8f2015-05-05 22:22:40 -0400348 s->tlsext_channel_id_private =
349 EVP_PKEY_up_ref(ctx->tlsext_channel_id_private);
David Benjamin02ddbfd2015-01-11 13:09:11 -0500350 }
351
Adam Langleyfcf25832014-12-18 17:42:32 -0800352 s->signed_cert_timestamps_enabled = s->ctx->signed_cert_timestamps_enabled;
353 s->ocsp_stapling_enabled = s->ctx->ocsp_stapling_enabled;
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200354
Adam Langleyfcf25832014-12-18 17:42:32 -0800355 return s;
356
Adam Langley95c29f32014-06-20 12:00:00 -0700357err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400358 SSL_free(s);
David Benjamin3570d732015-06-29 00:28:17 -0400359 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700360
Adam Langleyfcf25832014-12-18 17:42:32 -0800361 return NULL;
362}
Adam Langley95c29f32014-06-20 12:00:00 -0700363
Adam Langleyfcf25832014-12-18 17:42:32 -0800364int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
365 unsigned int sid_ctx_len) {
366 if (sid_ctx_len > sizeof ctx->sid_ctx) {
David Benjamin3570d732015-06-29 00:28:17 -0400367 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -0800368 return 0;
369 }
370 ctx->sid_ctx_length = sid_ctx_len;
371 memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700372
Adam Langleyfcf25832014-12-18 17:42:32 -0800373 return 1;
374}
Adam Langley95c29f32014-06-20 12:00:00 -0700375
Adam Langleyfcf25832014-12-18 17:42:32 -0800376int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
377 unsigned int sid_ctx_len) {
378 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
David Benjamin3570d732015-06-29 00:28:17 -0400379 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -0800380 return 0;
381 }
382 ssl->sid_ctx_length = sid_ctx_len;
383 memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700384
Adam Langleyfcf25832014-12-18 17:42:32 -0800385 return 1;
386}
Adam Langley95c29f32014-06-20 12:00:00 -0700387
Adam Langleyfcf25832014-12-18 17:42:32 -0800388int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800389 ctx->generate_session_id = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -0800390 return 1;
391}
Adam Langley95c29f32014-06-20 12:00:00 -0700392
Adam Langleyfcf25832014-12-18 17:42:32 -0800393int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800394 ssl->generate_session_id = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -0800395 return 1;
396}
Adam Langley95c29f32014-06-20 12:00:00 -0700397
Adam Langleyfcf25832014-12-18 17:42:32 -0800398int SSL_has_matching_session_id(const SSL *ssl, const uint8_t *id,
399 unsigned int id_len) {
400 /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how we
401 * can "construct" a session to give us the desired check - ie. to find if
402 * there's a session in the hash table that would conflict with any new
403 * session built out of this id/id_len and the ssl_version in use by this
404 * SSL. */
405 SSL_SESSION r, *p;
Adam Langley95c29f32014-06-20 12:00:00 -0700406
Adam Langleyfcf25832014-12-18 17:42:32 -0800407 if (id_len > sizeof r.session_id) {
408 return 0;
409 }
Adam Langley95c29f32014-06-20 12:00:00 -0700410
Adam Langleyfcf25832014-12-18 17:42:32 -0800411 r.ssl_version = ssl->version;
412 r.session_id_length = id_len;
413 memcpy(r.session_id, id, id_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700414
Adam Langley4bdb6e42015-05-15 15:29:21 -0700415 CRYPTO_MUTEX_lock_read(&ssl->ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -0800416 p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r);
Adam Langley4bdb6e42015-05-15 15:29:21 -0700417 CRYPTO_MUTEX_unlock(&ssl->ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -0800418 return p != NULL;
419}
Adam Langley95c29f32014-06-20 12:00:00 -0700420
Adam Langleyfcf25832014-12-18 17:42:32 -0800421int SSL_CTX_set_purpose(SSL_CTX *s, int purpose) {
422 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
423}
424
425int SSL_set_purpose(SSL *s, int purpose) {
426 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
427}
428
429int SSL_CTX_set_trust(SSL_CTX *s, int trust) {
430 return X509_VERIFY_PARAM_set_trust(s->param, trust);
431}
432
433int SSL_set_trust(SSL *s, int trust) {
434 return X509_VERIFY_PARAM_set_trust(s->param, trust);
435}
436
437int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm) {
438 return X509_VERIFY_PARAM_set1(ctx->param, vpm);
439}
440
441int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm) {
442 return X509_VERIFY_PARAM_set1(ssl->param, vpm);
443}
Adam Langley95c29f32014-06-20 12:00:00 -0700444
Adam Langley858a88d2014-06-20 12:00:00 -0700445void ssl_cipher_preference_list_free(
Adam Langleyfcf25832014-12-18 17:42:32 -0800446 struct ssl_cipher_preference_list_st *cipher_list) {
David Benjamin5d1ec732015-04-22 13:38:00 -0400447 if (cipher_list == NULL) {
448 return;
449 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800450 sk_SSL_CIPHER_free(cipher_list->ciphers);
451 OPENSSL_free(cipher_list->in_group_flags);
452 OPENSSL_free(cipher_list);
453}
Adam Langley858a88d2014-06-20 12:00:00 -0700454
Adam Langleyfcf25832014-12-18 17:42:32 -0800455struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_dup(
456 struct ssl_cipher_preference_list_st *cipher_list) {
457 struct ssl_cipher_preference_list_st *ret = NULL;
458 size_t n = sk_SSL_CIPHER_num(cipher_list->ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700459
Adam Langleyfcf25832014-12-18 17:42:32 -0800460 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
461 if (!ret) {
462 goto err;
463 }
464
465 ret->ciphers = NULL;
466 ret->in_group_flags = NULL;
467 ret->ciphers = sk_SSL_CIPHER_dup(cipher_list->ciphers);
468 if (!ret->ciphers) {
469 goto err;
470 }
471 ret->in_group_flags = BUF_memdup(cipher_list->in_group_flags, n);
472 if (!ret->in_group_flags) {
473 goto err;
474 }
475
476 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -0700477
478err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400479 ssl_cipher_preference_list_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -0800480 return NULL;
481}
Adam Langley858a88d2014-06-20 12:00:00 -0700482
Adam Langleyfcf25832014-12-18 17:42:32 -0800483struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_from_ciphers(
David Benjamin60da0cd2015-05-03 15:21:28 -0400484 STACK_OF(SSL_CIPHER) *ciphers) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800485 struct ssl_cipher_preference_list_st *ret = NULL;
486 size_t n = sk_SSL_CIPHER_num(ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700487
Adam Langleyfcf25832014-12-18 17:42:32 -0800488 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
489 if (!ret) {
490 goto err;
491 }
492 ret->ciphers = NULL;
493 ret->in_group_flags = NULL;
494 ret->ciphers = sk_SSL_CIPHER_dup(ciphers);
495 if (!ret->ciphers) {
496 goto err;
497 }
498 ret->in_group_flags = OPENSSL_malloc(n);
499 if (!ret->in_group_flags) {
500 goto err;
501 }
502 memset(ret->in_group_flags, 0, n);
503 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -0700504
505err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400506 ssl_cipher_preference_list_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -0800507 return NULL;
508}
Adam Langley858a88d2014-06-20 12:00:00 -0700509
Adam Langleyfcf25832014-12-18 17:42:32 -0800510X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) { return ctx->param; }
Adam Langley95c29f32014-06-20 12:00:00 -0700511
Adam Langleyfcf25832014-12-18 17:42:32 -0800512X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) { return ssl->param; }
Adam Langley95c29f32014-06-20 12:00:00 -0700513
David Benjamin7481d392015-07-05 19:38:46 -0400514void SSL_certs_clear(SSL *ssl) { ssl_cert_clear_certs(ssl->cert); }
Adam Langley95c29f32014-06-20 12:00:00 -0700515
David Benjamin4831c332015-05-16 11:43:13 -0400516void SSL_free(SSL *ssl) {
517 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800518 return;
519 }
Adam Langley95c29f32014-06-20 12:00:00 -0700520
David Benjamin4831c332015-05-16 11:43:13 -0400521 X509_VERIFY_PARAM_free(ssl->param);
Adam Langley95c29f32014-06-20 12:00:00 -0700522
David Benjamin4831c332015-05-16 11:43:13 -0400523 CRYPTO_free_ex_data(&g_ex_data_class_ssl, ssl, &ssl->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700524
David Benjamin4831c332015-05-16 11:43:13 -0400525 if (ssl->bbio != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800526 /* If the buffering BIO is in place, pop it off */
David Benjamin4831c332015-05-16 11:43:13 -0400527 if (ssl->bbio == ssl->wbio) {
528 ssl->wbio = BIO_pop(ssl->wbio);
Adam Langleyfcf25832014-12-18 17:42:32 -0800529 }
David Benjamin4831c332015-05-16 11:43:13 -0400530 BIO_free(ssl->bbio);
531 ssl->bbio = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800532 }
Adam Langley95c29f32014-06-20 12:00:00 -0700533
David Benjamin4831c332015-05-16 11:43:13 -0400534 int free_wbio = ssl->wbio != ssl->rbio;
535 BIO_free_all(ssl->rbio);
David Benjamin2755a3e2015-04-22 16:17:58 -0400536 if (free_wbio) {
David Benjamin4831c332015-05-16 11:43:13 -0400537 BIO_free_all(ssl->wbio);
Adam Langleyfcf25832014-12-18 17:42:32 -0800538 }
Adam Langley95c29f32014-06-20 12:00:00 -0700539
David Benjamin4831c332015-05-16 11:43:13 -0400540 BUF_MEM_free(ssl->init_buf);
Adam Langley95c29f32014-06-20 12:00:00 -0700541
Adam Langleyfcf25832014-12-18 17:42:32 -0800542 /* add extra stuff */
David Benjamin4831c332015-05-16 11:43:13 -0400543 ssl_cipher_preference_list_free(ssl->cipher_list);
544 sk_SSL_CIPHER_free(ssl->cipher_list_by_id);
Adam Langley95c29f32014-06-20 12:00:00 -0700545
David Benjamin4831c332015-05-16 11:43:13 -0400546 ssl_clear_bad_session(ssl);
547 SSL_SESSION_free(ssl->session);
Adam Langley95c29f32014-06-20 12:00:00 -0700548
David Benjamin4831c332015-05-16 11:43:13 -0400549 ssl_clear_cipher_ctx(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700550
David Benjamin4831c332015-05-16 11:43:13 -0400551 ssl_cert_free(ssl->cert);
Adam Langley0289c732014-06-20 12:00:00 -0700552
David Benjamin4831c332015-05-16 11:43:13 -0400553 OPENSSL_free(ssl->tlsext_hostname);
554 SSL_CTX_free(ssl->initial_ctx);
David Benjamin4831c332015-05-16 11:43:13 -0400555 OPENSSL_free(ssl->tlsext_ellipticcurvelist);
556 OPENSSL_free(ssl->alpn_client_proto_list);
557 EVP_PKEY_free(ssl->tlsext_channel_id_private);
558 OPENSSL_free(ssl->psk_identity_hint);
559 sk_X509_NAME_pop_free(ssl->client_CA, X509_NAME_free);
560 OPENSSL_free(ssl->next_proto_negotiated);
561 sk_SRTP_PROTECTION_PROFILE_free(ssl->srtp_profiles);
Adam Langley95c29f32014-06-20 12:00:00 -0700562
David Benjamin4831c332015-05-16 11:43:13 -0400563 if (ssl->method != NULL) {
564 ssl->method->ssl_free(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800565 }
David Benjamin4831c332015-05-16 11:43:13 -0400566 SSL_CTX_free(ssl->ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700567
David Benjamin4831c332015-05-16 11:43:13 -0400568 OPENSSL_free(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800569}
Adam Langley95c29f32014-06-20 12:00:00 -0700570
Adam Langleyfcf25832014-12-18 17:42:32 -0800571void SSL_set_bio(SSL *s, BIO *rbio, BIO *wbio) {
572 /* If the output buffering BIO is still in place, remove it. */
573 if (s->bbio != NULL) {
574 if (s->wbio == s->bbio) {
575 s->wbio = s->wbio->next_bio;
576 s->bbio->next_bio = NULL;
577 }
578 }
Adam Langley95c29f32014-06-20 12:00:00 -0700579
David Benjamin2755a3e2015-04-22 16:17:58 -0400580 if (s->rbio != rbio) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800581 BIO_free_all(s->rbio);
582 }
David Benjamin2755a3e2015-04-22 16:17:58 -0400583 if (s->wbio != wbio && s->rbio != s->wbio) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800584 BIO_free_all(s->wbio);
585 }
586 s->rbio = rbio;
587 s->wbio = wbio;
588}
Adam Langley95c29f32014-06-20 12:00:00 -0700589
Adam Langleyfcf25832014-12-18 17:42:32 -0800590BIO *SSL_get_rbio(const SSL *s) { return s->rbio; }
Adam Langley95c29f32014-06-20 12:00:00 -0700591
Adam Langleyfcf25832014-12-18 17:42:32 -0800592BIO *SSL_get_wbio(const SSL *s) { return s->wbio; }
Adam Langley95c29f32014-06-20 12:00:00 -0700593
Adam Langleyfcf25832014-12-18 17:42:32 -0800594int SSL_get_fd(const SSL *s) { return SSL_get_rfd(s); }
Adam Langley95c29f32014-06-20 12:00:00 -0700595
Adam Langleyfcf25832014-12-18 17:42:32 -0800596int SSL_get_rfd(const SSL *s) {
597 int ret = -1;
598 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700599
Adam Langleyfcf25832014-12-18 17:42:32 -0800600 b = SSL_get_rbio(s);
601 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
602 if (r != NULL) {
603 BIO_get_fd(r, &ret);
604 }
605 return ret;
606}
Adam Langley95c29f32014-06-20 12:00:00 -0700607
Adam Langleyfcf25832014-12-18 17:42:32 -0800608int SSL_get_wfd(const SSL *s) {
609 int ret = -1;
610 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700611
Adam Langleyfcf25832014-12-18 17:42:32 -0800612 b = SSL_get_wbio(s);
613 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
614 if (r != NULL) {
615 BIO_get_fd(r, &ret);
616 }
Adam Langley95c29f32014-06-20 12:00:00 -0700617
Adam Langleyfcf25832014-12-18 17:42:32 -0800618 return ret;
619}
Adam Langley95c29f32014-06-20 12:00:00 -0700620
Adam Langleyfcf25832014-12-18 17:42:32 -0800621int SSL_set_fd(SSL *s, int fd) {
622 int ret = 0;
623 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700624
Adam Langleyfcf25832014-12-18 17:42:32 -0800625 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700626
Adam Langleyfcf25832014-12-18 17:42:32 -0800627 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400628 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -0800629 goto err;
630 }
631 BIO_set_fd(bio, fd, BIO_NOCLOSE);
632 SSL_set_bio(s, bio, bio);
633 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700634
Adam Langley95c29f32014-06-20 12:00:00 -0700635err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800636 return ret;
637}
Adam Langley95c29f32014-06-20 12:00:00 -0700638
Adam Langleyfcf25832014-12-18 17:42:32 -0800639int SSL_set_wfd(SSL *s, int fd) {
640 int ret = 0;
641 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700642
Adam Langleyfcf25832014-12-18 17:42:32 -0800643 if (s->rbio == NULL || BIO_method_type(s->rbio) != BIO_TYPE_FD ||
644 (int)BIO_get_fd(s->rbio, NULL) != fd) {
645 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700646
Adam Langleyfcf25832014-12-18 17:42:32 -0800647 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400648 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -0800649 goto err;
650 }
651 BIO_set_fd(bio, fd, BIO_NOCLOSE);
652 SSL_set_bio(s, SSL_get_rbio(s), bio);
653 } else {
654 SSL_set_bio(s, SSL_get_rbio(s), SSL_get_rbio(s));
655 }
656
657 ret = 1;
658
Adam Langley95c29f32014-06-20 12:00:00 -0700659err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800660 return ret;
661}
Adam Langley95c29f32014-06-20 12:00:00 -0700662
Adam Langleyfcf25832014-12-18 17:42:32 -0800663int SSL_set_rfd(SSL *s, int fd) {
664 int ret = 0;
665 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700666
Adam Langleyfcf25832014-12-18 17:42:32 -0800667 if (s->wbio == NULL || BIO_method_type(s->wbio) != BIO_TYPE_FD ||
668 (int)BIO_get_fd(s->wbio, NULL) != fd) {
669 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700670
Adam Langleyfcf25832014-12-18 17:42:32 -0800671 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400672 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -0800673 goto err;
674 }
675 BIO_set_fd(bio, fd, BIO_NOCLOSE);
676 SSL_set_bio(s, bio, SSL_get_wbio(s));
677 } else {
678 SSL_set_bio(s, SSL_get_wbio(s), SSL_get_wbio(s));
679 }
680 ret = 1;
681
Adam Langley95c29f32014-06-20 12:00:00 -0700682err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800683 return ret;
684}
Adam Langley95c29f32014-06-20 12:00:00 -0700685
686/* return length of latest Finished message we sent, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -0800687size_t SSL_get_finished(const SSL *s, void *buf, size_t count) {
688 size_t ret = 0;
689
690 if (s->s3 != NULL) {
691 ret = s->s3->tmp.finish_md_len;
692 if (count > ret) {
693 count = ret;
694 }
695 memcpy(buf, s->s3->tmp.finish_md, count);
696 }
697
698 return ret;
699}
Adam Langley95c29f32014-06-20 12:00:00 -0700700
701/* return length of latest Finished message we expected, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -0800702size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count) {
703 size_t ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700704
Adam Langleyfcf25832014-12-18 17:42:32 -0800705 if (s->s3 != NULL) {
706 ret = s->s3->tmp.peer_finish_md_len;
707 if (count > ret) {
708 count = ret;
709 }
710 memcpy(buf, s->s3->tmp.peer_finish_md, count);
711 }
Adam Langley95c29f32014-06-20 12:00:00 -0700712
Adam Langleyfcf25832014-12-18 17:42:32 -0800713 return ret;
714}
Adam Langley95c29f32014-06-20 12:00:00 -0700715
Adam Langleyfcf25832014-12-18 17:42:32 -0800716int SSL_get_verify_mode(const SSL *s) { return s->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -0700717
Adam Langleyfcf25832014-12-18 17:42:32 -0800718int SSL_get_verify_depth(const SSL *s) {
719 return X509_VERIFY_PARAM_get_depth(s->param);
720}
Adam Langley95c29f32014-06-20 12:00:00 -0700721
Adam Langleyfcf25832014-12-18 17:42:32 -0800722int (*SSL_get_verify_callback(const SSL *s))(int, X509_STORE_CTX *) {
723 return s->verify_callback;
724}
Adam Langley95c29f32014-06-20 12:00:00 -0700725
Adam Langleyfcf25832014-12-18 17:42:32 -0800726int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) { return ctx->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -0700727
Adam Langleyfcf25832014-12-18 17:42:32 -0800728int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) {
729 return X509_VERIFY_PARAM_get_depth(ctx->param);
730}
Adam Langley95c29f32014-06-20 12:00:00 -0700731
Adam Langleyfcf25832014-12-18 17:42:32 -0800732int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int, X509_STORE_CTX *) {
733 return ctx->default_verify_callback;
734}
Adam Langley95c29f32014-06-20 12:00:00 -0700735
Adam Langleyfcf25832014-12-18 17:42:32 -0800736void SSL_set_verify(SSL *s, int mode,
737 int (*callback)(int ok, X509_STORE_CTX *ctx)) {
738 s->verify_mode = mode;
739 if (callback != NULL) {
740 s->verify_callback = callback;
741 }
742}
Adam Langley95c29f32014-06-20 12:00:00 -0700743
Adam Langleyfcf25832014-12-18 17:42:32 -0800744void SSL_set_verify_depth(SSL *s, int depth) {
745 X509_VERIFY_PARAM_set_depth(s->param, depth);
746}
Adam Langley95c29f32014-06-20 12:00:00 -0700747
David Benjamin9a41d1b2015-05-16 01:30:09 -0400748int SSL_CTX_get_read_ahead(const SSL_CTX *ctx) { return 0; }
Adam Langley95c29f32014-06-20 12:00:00 -0700749
David Benjamin9a41d1b2015-05-16 01:30:09 -0400750int SSL_get_read_ahead(const SSL *s) { return 0; }
Adam Langley95c29f32014-06-20 12:00:00 -0700751
David Benjamin9a41d1b2015-05-16 01:30:09 -0400752void SSL_CTX_set_read_ahead(SSL_CTX *ctx, int yes) { }
David Benjamin61ecccf2015-05-05 09:44:51 -0400753
David Benjamin9a41d1b2015-05-16 01:30:09 -0400754void SSL_set_read_ahead(SSL *s, int yes) { }
David Benjamin61ecccf2015-05-05 09:44:51 -0400755
Adam Langleyfcf25832014-12-18 17:42:32 -0800756int SSL_pending(const SSL *s) {
David Benjamin904dc722015-05-30 16:39:07 -0400757 if (s->rstate == SSL_ST_READ_BODY) {
758 return 0;
759 }
760
761 return (s->s3->rrec.type == SSL3_RT_APPLICATION_DATA) ? s->s3->rrec.length
762 : 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800763}
Adam Langley95c29f32014-06-20 12:00:00 -0700764
Adam Langleyfcf25832014-12-18 17:42:32 -0800765X509 *SSL_get_peer_certificate(const SSL *s) {
766 X509 *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700767
Adam Langleyfcf25832014-12-18 17:42:32 -0800768 if (s == NULL || s->session == NULL) {
769 r = NULL;
770 } else {
771 r = s->session->peer;
772 }
Adam Langley95c29f32014-06-20 12:00:00 -0700773
Adam Langleyfcf25832014-12-18 17:42:32 -0800774 if (r == NULL) {
775 return NULL;
776 }
Adam Langley95c29f32014-06-20 12:00:00 -0700777
Adam Langleyfcf25832014-12-18 17:42:32 -0800778 return X509_up_ref(r);
779}
780
David Benjamin60da0cd2015-05-03 15:21:28 -0400781STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s) {
782 STACK_OF(X509) *r;
Adam Langleyfcf25832014-12-18 17:42:32 -0800783
784 if (s == NULL || s->session == NULL || s->session->sess_cert == NULL) {
785 r = NULL;
786 } else {
787 r = s->session->sess_cert->cert_chain;
788 }
789
790 /* If we are a client, cert_chain includes the peer's own certificate; if we
791 * are a server, it does not. */
792 return r;
793}
Adam Langley95c29f32014-06-20 12:00:00 -0700794
Adam Langley95c29f32014-06-20 12:00:00 -0700795/* Fix this so it checks all the valid key/cert options */
Adam Langleyfcf25832014-12-18 17:42:32 -0800796int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
David Benjamind1d80782015-07-05 11:54:09 -0400797 if (ctx == NULL || ctx->cert == NULL || ctx->cert->x509 == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400798 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800799 return 0;
800 }
801
David Benjamind1d80782015-07-05 11:54:09 -0400802 if (ctx->cert->privatekey == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400803 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800804 return 0;
805 }
806
David Benjamind1d80782015-07-05 11:54:09 -0400807 return X509_check_private_key(ctx->cert->x509, ctx->cert->privatekey);
Adam Langleyfcf25832014-12-18 17:42:32 -0800808}
Adam Langley95c29f32014-06-20 12:00:00 -0700809
810/* Fix this function so that it takes an optional type parameter */
Adam Langleyfcf25832014-12-18 17:42:32 -0800811int SSL_check_private_key(const SSL *ssl) {
812 if (ssl == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400813 OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
Adam Langleyfcf25832014-12-18 17:42:32 -0800814 return 0;
815 }
Adam Langley95c29f32014-06-20 12:00:00 -0700816
Adam Langleyfcf25832014-12-18 17:42:32 -0800817 if (ssl->cert == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400818 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800819 return 0;
820 }
Adam Langley95c29f32014-06-20 12:00:00 -0700821
David Benjamind1d80782015-07-05 11:54:09 -0400822 if (ssl->cert->x509 == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400823 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800824 return 0;
825 }
David Benjamin0b145c22014-11-26 20:10:09 -0500826
David Benjamind1d80782015-07-05 11:54:09 -0400827 if (ssl->cert->privatekey == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400828 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800829 return 0;
830 }
Adam Langley95c29f32014-06-20 12:00:00 -0700831
David Benjamind1d80782015-07-05 11:54:09 -0400832 return X509_check_private_key(ssl->cert->x509, ssl->cert->privatekey);
Adam Langleyfcf25832014-12-18 17:42:32 -0800833}
Adam Langley95c29f32014-06-20 12:00:00 -0700834
Adam Langleyfcf25832014-12-18 17:42:32 -0800835int SSL_accept(SSL *s) {
836 if (s->handshake_func == 0) {
837 /* Not properly initialized yet */
838 SSL_set_accept_state(s);
839 }
David Benjamin0b145c22014-11-26 20:10:09 -0500840
Adam Langleyfcf25832014-12-18 17:42:32 -0800841 if (s->handshake_func != s->method->ssl_accept) {
David Benjamin3570d732015-06-29 00:28:17 -0400842 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800843 return -1;
844 }
Adam Langley95c29f32014-06-20 12:00:00 -0700845
Adam Langleyfcf25832014-12-18 17:42:32 -0800846 return s->handshake_func(s);
847}
Adam Langley95c29f32014-06-20 12:00:00 -0700848
Adam Langleyfcf25832014-12-18 17:42:32 -0800849int SSL_connect(SSL *s) {
850 if (s->handshake_func == 0) {
851 /* Not properly initialized yet */
852 SSL_set_connect_state(s);
853 }
Adam Langley95c29f32014-06-20 12:00:00 -0700854
Adam Langleyfcf25832014-12-18 17:42:32 -0800855 if (s->handshake_func != s->method->ssl_connect) {
David Benjamin3570d732015-06-29 00:28:17 -0400856 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800857 return -1;
858 }
Adam Langley95c29f32014-06-20 12:00:00 -0700859
Adam Langleyfcf25832014-12-18 17:42:32 -0800860 return s->handshake_func(s);
861}
Adam Langley95c29f32014-06-20 12:00:00 -0700862
Adam Langleyfcf25832014-12-18 17:42:32 -0800863long SSL_get_default_timeout(const SSL *s) {
864 return SSL_DEFAULT_SESSION_TIMEOUT;
865}
Adam Langley95c29f32014-06-20 12:00:00 -0700866
Adam Langleyfcf25832014-12-18 17:42:32 -0800867int SSL_read(SSL *s, void *buf, int num) {
868 if (s->handshake_func == 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400869 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800870 return -1;
871 }
Adam Langley95c29f32014-06-20 12:00:00 -0700872
Adam Langleyfcf25832014-12-18 17:42:32 -0800873 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
874 s->rwstate = SSL_NOTHING;
875 return 0;
876 }
Adam Langley95c29f32014-06-20 12:00:00 -0700877
David Benjamin904dc722015-05-30 16:39:07 -0400878 ERR_clear_system_error();
879 return s->method->ssl_read_app_data(s, buf, num, 0);
Adam Langleyfcf25832014-12-18 17:42:32 -0800880}
Adam Langley95c29f32014-06-20 12:00:00 -0700881
Adam Langleyfcf25832014-12-18 17:42:32 -0800882int SSL_peek(SSL *s, void *buf, int num) {
883 if (s->handshake_func == 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400884 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800885 return -1;
886 }
Adam Langley95c29f32014-06-20 12:00:00 -0700887
Adam Langleyfcf25832014-12-18 17:42:32 -0800888 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
889 return 0;
890 }
Adam Langley95c29f32014-06-20 12:00:00 -0700891
David Benjamin904dc722015-05-30 16:39:07 -0400892 ERR_clear_system_error();
893 return s->method->ssl_read_app_data(s, buf, num, 1);
Adam Langleyfcf25832014-12-18 17:42:32 -0800894}
Adam Langley95c29f32014-06-20 12:00:00 -0700895
Adam Langleyfcf25832014-12-18 17:42:32 -0800896int SSL_write(SSL *s, const void *buf, int num) {
897 if (s->handshake_func == 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400898 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800899 return -1;
900 }
Adam Langley95c29f32014-06-20 12:00:00 -0700901
Adam Langleyfcf25832014-12-18 17:42:32 -0800902 if (s->shutdown & SSL_SENT_SHUTDOWN) {
903 s->rwstate = SSL_NOTHING;
David Benjamin3570d732015-06-29 00:28:17 -0400904 OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
Adam Langleyfcf25832014-12-18 17:42:32 -0800905 return -1;
906 }
Adam Langley95c29f32014-06-20 12:00:00 -0700907
David Benjamin904dc722015-05-30 16:39:07 -0400908 ERR_clear_system_error();
909 return s->method->ssl_write_app_data(s, buf, num);
Adam Langleyfcf25832014-12-18 17:42:32 -0800910}
Adam Langley95c29f32014-06-20 12:00:00 -0700911
Adam Langleyfcf25832014-12-18 17:42:32 -0800912int SSL_shutdown(SSL *s) {
913 /* Note that this function behaves differently from what one might expect.
914 * Return values are 0 for no success (yet), 1 for success; but calling it
915 * once is usually not enough, even if blocking I/O is used (see
916 * ssl3_shutdown). */
Adam Langley95c29f32014-06-20 12:00:00 -0700917
Adam Langleyfcf25832014-12-18 17:42:32 -0800918 if (s->handshake_func == 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400919 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800920 return -1;
921 }
Adam Langley95c29f32014-06-20 12:00:00 -0700922
David Benjamin904dc722015-05-30 16:39:07 -0400923 if (SSL_in_init(s)) {
924 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800925 }
Adam Langley95c29f32014-06-20 12:00:00 -0700926
David Benjamin904dc722015-05-30 16:39:07 -0400927 /* Do nothing if configured not to send a close_notify. */
928 if (s->quiet_shutdown) {
929 s->shutdown = SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN;
930 return 1;
931 }
932
933 if (!(s->shutdown & SSL_SENT_SHUTDOWN)) {
934 s->shutdown |= SSL_SENT_SHUTDOWN;
935 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_CLOSE_NOTIFY);
936
937 /* our shutdown alert has been sent now, and if it still needs to be
938 * written, s->s3->alert_dispatch will be true */
939 if (s->s3->alert_dispatch) {
940 return -1; /* return WANT_WRITE */
941 }
942 } else if (s->s3->alert_dispatch) {
943 /* resend it if not sent */
944 int ret = s->method->ssl_dispatch_alert(s);
945 if (ret == -1) {
946 /* we only get to return -1 here the 2nd/Nth invocation, we must have
947 * already signalled return 0 upon a previous invoation, return
948 * WANT_WRITE */
949 return ret;
950 }
951 } else if (!(s->shutdown & SSL_RECEIVED_SHUTDOWN)) {
952 /* If we are waiting for a close from our peer, we are closed */
953 s->method->ssl_read_close_notify(s);
954 if (!(s->shutdown & SSL_RECEIVED_SHUTDOWN)) {
955 return -1; /* return WANT_READ */
956 }
957 }
958
959 if (s->shutdown == (SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN) &&
960 !s->s3->alert_dispatch) {
961 return 1;
962 } else {
963 return 0;
964 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800965}
Adam Langley95c29f32014-06-20 12:00:00 -0700966
David Benjamin44d3eed2015-05-21 01:29:55 -0400967int SSL_renegotiate(SSL *ssl) {
968 /* Caller-initiated renegotiation is not supported. */
David Benjamin3570d732015-06-29 00:28:17 -0400969 OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
David Benjamin44d3eed2015-05-21 01:29:55 -0400970 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800971}
Adam Langley95c29f32014-06-20 12:00:00 -0700972
David Benjamin44d3eed2015-05-21 01:29:55 -0400973int SSL_renegotiate_pending(SSL *ssl) {
974 return SSL_in_init(ssl) && ssl->s3->initial_handshake_complete;
Adam Langleyfcf25832014-12-18 17:42:32 -0800975}
Adam Langley95c29f32014-06-20 12:00:00 -0700976
David Benjamin61ecccf2015-05-05 09:44:51 -0400977uint32_t SSL_CTX_set_options(SSL_CTX *ctx, uint32_t options) {
978 ctx->options |= options;
979 return ctx->options;
980}
981
982uint32_t SSL_set_options(SSL *ssl, uint32_t options) {
983 ssl->options |= options;
984 return ssl->options;
985}
986
987uint32_t SSL_CTX_clear_options(SSL_CTX *ctx, uint32_t options) {
988 ctx->options &= ~options;
989 return ctx->options;
990}
991
992uint32_t SSL_clear_options(SSL *ssl, uint32_t options) {
993 ssl->options &= ~options;
994 return ssl->options;
995}
996
997uint32_t SSL_CTX_get_options(const SSL_CTX *ctx) { return ctx->options; }
998
999uint32_t SSL_get_options(const SSL *ssl) { return ssl->options; }
1000
1001uint32_t SSL_CTX_set_mode(SSL_CTX *ctx, uint32_t mode) {
1002 ctx->mode |= mode;
1003 return ctx->mode;
1004}
1005
1006uint32_t SSL_set_mode(SSL *ssl, uint32_t mode) {
1007 ssl->mode |= mode;
1008 return ssl->mode;
1009}
1010
1011uint32_t SSL_CTX_clear_mode(SSL_CTX *ctx, uint32_t mode) {
1012 ctx->mode &= ~mode;
1013 return ctx->mode;
1014}
1015
1016uint32_t SSL_clear_mode(SSL *ssl, uint32_t mode) {
1017 ssl->mode &= ~mode;
1018 return ssl->mode;
1019}
1020
1021uint32_t SSL_CTX_get_mode(const SSL_CTX *ctx) { return ctx->mode; }
1022
1023uint32_t SSL_get_mode(const SSL *ssl) { return ssl->mode; }
1024
David Benjamin1d0a1942015-04-26 15:35:35 -04001025size_t SSL_CTX_get_max_cert_list(const SSL_CTX *ctx) {
1026 return ctx->max_cert_list;
1027}
1028
1029void SSL_CTX_set_max_cert_list(SSL_CTX *ctx, size_t max_cert_list) {
1030 if (max_cert_list > kMaxHandshakeSize) {
1031 max_cert_list = kMaxHandshakeSize;
1032 }
1033 ctx->max_cert_list = (uint32_t)max_cert_list;
1034}
1035
1036size_t SSL_get_max_cert_list(const SSL *ssl) {
1037 return ssl->max_cert_list;
1038}
1039
1040void SSL_set_max_cert_list(SSL *ssl, size_t max_cert_list) {
1041 if (max_cert_list > kMaxHandshakeSize) {
1042 max_cert_list = kMaxHandshakeSize;
1043 }
1044 ssl->max_cert_list = (uint32_t)max_cert_list;
1045}
1046
1047void SSL_CTX_set_max_send_fragment(SSL_CTX *ctx, size_t max_send_fragment) {
1048 if (max_send_fragment < 512) {
1049 max_send_fragment = 512;
1050 }
1051 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1052 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1053 }
1054 ctx->max_send_fragment = (uint16_t)max_send_fragment;
1055}
1056
1057void SSL_set_max_send_fragment(SSL *ssl, size_t max_send_fragment) {
1058 if (max_send_fragment < 512) {
1059 max_send_fragment = 512;
1060 }
1061 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1062 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1063 }
1064 ssl->max_send_fragment = (uint16_t)max_send_fragment;
1065}
1066
David Benjamincb9cf792015-05-05 09:46:14 -04001067int SSL_set_mtu(SSL *ssl, unsigned mtu) {
1068 if (!SSL_IS_DTLS(ssl) || mtu < dtls1_min_mtu()) {
1069 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001070 }
David Benjamincb9cf792015-05-05 09:46:14 -04001071 ssl->d1->mtu = mtu;
1072 return 1;
1073}
1074
1075int SSL_get_secure_renegotiation_support(const SSL *ssl) {
1076 return ssl->s3->send_connection_binding;
1077}
1078
1079long SSL_ctrl(SSL *s, int cmd, long larg, void *parg) {
1080 return s->method->ssl_ctrl(s, cmd, larg, parg);
Adam Langleyfcf25832014-12-18 17:42:32 -08001081}
1082
Adam Langleyfcf25832014-12-18 17:42:32 -08001083LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) { return ctx->sessions; }
1084
David Benjamin71f7d3d2015-05-05 09:46:38 -04001085size_t SSL_CTX_sess_number(const SSL_CTX *ctx) {
1086 return lh_SSL_SESSION_num_items(ctx->sessions);
1087}
1088
1089unsigned long SSL_CTX_sess_set_cache_size(SSL_CTX *ctx, unsigned long size) {
1090 unsigned long ret = ctx->session_cache_size;
1091 ctx->session_cache_size = size;
1092 return ret;
1093}
1094
1095unsigned long SSL_CTX_sess_get_cache_size(const SSL_CTX *ctx) {
1096 return ctx->session_cache_size;
1097}
1098
1099int SSL_CTX_set_session_cache_mode(SSL_CTX *ctx, int mode) {
1100 int ret = ctx->session_cache_mode;
1101 ctx->session_cache_mode = mode;
1102 return ret;
1103}
1104
1105int SSL_CTX_get_session_cache_mode(const SSL_CTX *ctx) {
1106 return ctx->session_cache_mode;
1107}
1108
Adam Langleyfcf25832014-12-18 17:42:32 -08001109long SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg) {
David Benjamin71f7d3d2015-05-05 09:46:38 -04001110 return ctx->method->ssl_ctx_ctrl(ctx, cmd, larg, parg);
Adam Langleyfcf25832014-12-18 17:42:32 -08001111}
1112
Adam Langleyfcf25832014-12-18 17:42:32 -08001113/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001114 * preference */
David Benjamin60da0cd2015-05-03 15:21:28 -04001115STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001116 if (s == NULL) {
1117 return NULL;
1118 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001119
Adam Langleyfcf25832014-12-18 17:42:32 -08001120 if (s->cipher_list != NULL) {
1121 return s->cipher_list->ciphers;
1122 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001123
Adam Langleyfcf25832014-12-18 17:42:32 -08001124 if (s->version >= TLS1_1_VERSION && s->ctx != NULL &&
1125 s->ctx->cipher_list_tls11 != NULL) {
1126 return s->ctx->cipher_list_tls11->ciphers;
1127 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001128
Adam Langleyfcf25832014-12-18 17:42:32 -08001129 if (s->ctx != NULL && s->ctx->cipher_list != NULL) {
1130 return s->ctx->cipher_list->ciphers;
1131 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001132
Adam Langleyfcf25832014-12-18 17:42:32 -08001133 return NULL;
1134}
Adam Langley95c29f32014-06-20 12:00:00 -07001135
Adam Langleyfcf25832014-12-18 17:42:32 -08001136/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001137 * algorithm id */
David Benjamin60da0cd2015-05-03 15:21:28 -04001138STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001139 if (s == NULL) {
1140 return NULL;
1141 }
Adam Langley95c29f32014-06-20 12:00:00 -07001142
Adam Langleyfcf25832014-12-18 17:42:32 -08001143 if (s->cipher_list_by_id != NULL) {
1144 return s->cipher_list_by_id;
1145 }
Adam Langley95c29f32014-06-20 12:00:00 -07001146
Adam Langleyfcf25832014-12-18 17:42:32 -08001147 if (s->ctx != NULL && s->ctx->cipher_list_by_id != NULL) {
1148 return s->ctx->cipher_list_by_id;
1149 }
Adam Langley95c29f32014-06-20 12:00:00 -07001150
Adam Langleyfcf25832014-12-18 17:42:32 -08001151 return NULL;
1152}
Adam Langley95c29f32014-06-20 12:00:00 -07001153
Adam Langleyfcf25832014-12-18 17:42:32 -08001154/* The old interface to get the same thing as SSL_get_ciphers() */
1155const char *SSL_get_cipher_list(const SSL *s, int n) {
1156 const SSL_CIPHER *c;
David Benjamin60da0cd2015-05-03 15:21:28 -04001157 STACK_OF(SSL_CIPHER) *sk;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001158
Adam Langleyfcf25832014-12-18 17:42:32 -08001159 if (s == NULL) {
1160 return NULL;
1161 }
Adam Langley95c29f32014-06-20 12:00:00 -07001162
Adam Langleyfcf25832014-12-18 17:42:32 -08001163 sk = SSL_get_ciphers(s);
1164 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
1165 return NULL;
1166 }
Adam Langley95c29f32014-06-20 12:00:00 -07001167
Adam Langleyfcf25832014-12-18 17:42:32 -08001168 c = sk_SSL_CIPHER_value(sk, n);
1169 if (c == NULL) {
1170 return NULL;
1171 }
Adam Langley95c29f32014-06-20 12:00:00 -07001172
Adam Langleyfcf25832014-12-18 17:42:32 -08001173 return c->name;
1174}
David Benjamin5491e3f2014-09-29 19:33:09 -04001175
Adam Langleyfcf25832014-12-18 17:42:32 -08001176/* specify the ciphers to be used by default by the SSL_CTX */
1177int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
1178 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001179
Adam Langleyfcf25832014-12-18 17:42:32 -08001180 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001181 &ctx->cipher_list_by_id, str);
Adam Langleyfcf25832014-12-18 17:42:32 -08001182 /* ssl_create_cipher_list may return an empty stack if it was unable to find
1183 * a cipher matching the given rule string (for example if the rule string
1184 * specifies a cipher which has been disabled). This is not an error as far
1185 * as ssl_create_cipher_list is concerned, and hence ctx->cipher_list and
1186 * ctx->cipher_list_by_id has been updated. */
1187 if (sk == NULL) {
1188 return 0;
1189 } else if (sk_SSL_CIPHER_num(sk) == 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001190 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
Adam Langleyfcf25832014-12-18 17:42:32 -08001191 return 0;
1192 }
Adam Langley95c29f32014-06-20 12:00:00 -07001193
Adam Langleyfcf25832014-12-18 17:42:32 -08001194 return 1;
1195}
David Benjamin39482a12014-07-20 13:30:15 -04001196
Adam Langleyfcf25832014-12-18 17:42:32 -08001197int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str) {
1198 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001199
David Benjamin71f07942015-04-08 02:36:59 -04001200 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str);
Adam Langleyfcf25832014-12-18 17:42:32 -08001201 if (sk == NULL) {
1202 return 0;
1203 } else if (sk_SSL_CIPHER_num(sk) == 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001204 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
Adam Langleyfcf25832014-12-18 17:42:32 -08001205 return 0;
1206 }
David Benjamin9f2c0d72014-10-21 22:00:19 -04001207
Adam Langleyfcf25832014-12-18 17:42:32 -08001208 return 1;
1209}
Adam Langley95c29f32014-06-20 12:00:00 -07001210
Adam Langleyfcf25832014-12-18 17:42:32 -08001211/* specify the ciphers to be used by the SSL */
1212int SSL_set_cipher_list(SSL *s, const char *str) {
1213 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001214
Adam Langleyfcf25832014-12-18 17:42:32 -08001215 sk = ssl_create_cipher_list(s->ctx->method, &s->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001216 &s->cipher_list_by_id, str);
David Benjamin39482a12014-07-20 13:30:15 -04001217
Adam Langleyfcf25832014-12-18 17:42:32 -08001218 /* see comment in SSL_CTX_set_cipher_list */
1219 if (sk == NULL) {
1220 return 0;
1221 } else if (sk_SSL_CIPHER_num(sk) == 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001222 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
Adam Langleyfcf25832014-12-18 17:42:32 -08001223 return 0;
1224 }
David Benjamin39482a12014-07-20 13:30:15 -04001225
Adam Langleyfcf25832014-12-18 17:42:32 -08001226 return 1;
1227}
Adam Langley95c29f32014-06-20 12:00:00 -07001228
Adam Langleyfcf25832014-12-18 17:42:32 -08001229int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, uint8_t *p) {
1230 size_t i;
1231 const SSL_CIPHER *c;
1232 CERT *ct = s->cert;
1233 uint8_t *q;
1234 /* Set disabled masks for this session */
1235 ssl_set_client_disabled(s);
Adam Langley29707792014-06-20 12:00:00 -07001236
Adam Langleyfcf25832014-12-18 17:42:32 -08001237 if (sk == NULL) {
1238 return 0;
1239 }
1240 q = p;
Adam Langley95c29f32014-06-20 12:00:00 -07001241
Adam Langleyfcf25832014-12-18 17:42:32 -08001242 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
1243 c = sk_SSL_CIPHER_value(sk, i);
1244 /* Skip disabled ciphers */
1245 if (c->algorithm_ssl & ct->mask_ssl ||
1246 c->algorithm_mkey & ct->mask_k ||
1247 c->algorithm_auth & ct->mask_a) {
1248 continue;
1249 }
David Benjamina1c90a52015-05-30 17:03:14 -04001250 s2n(ssl_cipher_get_value(c), p);
Adam Langleyfcf25832014-12-18 17:42:32 -08001251 }
1252
1253 /* If all ciphers were disabled, return the error to the caller. */
1254 if (p == q) {
1255 return 0;
1256 }
1257
Adam Langley5021b222015-06-12 18:27:58 -07001258 /* For SSLv3, the SCSV is added. Otherwise the renegotiation extension is
1259 * added. */
1260 if (s->client_version == SSL3_VERSION &&
1261 !s->s3->initial_handshake_complete) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001262 s2n(SSL3_CK_SCSV & 0xffff, p);
Adam Langley5021b222015-06-12 18:27:58 -07001263 /* The renegotiation extension is required to be at index zero. */
1264 s->s3->tmp.extensions.sent |= (1u << 0);
Adam Langleyfcf25832014-12-18 17:42:32 -08001265 }
1266
Adam Langley5f0efe02015-02-20 13:03:16 -08001267 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001268 s2n(SSL3_CK_FALLBACK_SCSV & 0xffff, p);
1269 }
1270
1271 return p - q;
1272}
1273
1274STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs) {
1275 CBS cipher_suites = *cbs;
1276 const SSL_CIPHER *c;
David Benjamin60da0cd2015-05-03 15:21:28 -04001277 STACK_OF(SSL_CIPHER) *sk;
Adam Langleyfcf25832014-12-18 17:42:32 -08001278
1279 if (s->s3) {
1280 s->s3->send_connection_binding = 0;
1281 }
1282
1283 if (CBS_len(&cipher_suites) % 2 != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001284 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
Adam Langleyfcf25832014-12-18 17:42:32 -08001285 return NULL;
1286 }
1287
1288 sk = sk_SSL_CIPHER_new_null();
1289 if (sk == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001290 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001291 goto err;
1292 }
1293
Adam Langleyfcf25832014-12-18 17:42:32 -08001294 while (CBS_len(&cipher_suites) > 0) {
1295 uint16_t cipher_suite;
1296
1297 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
David Benjamin3570d732015-06-29 00:28:17 -04001298 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001299 goto err;
1300 }
1301
1302 /* Check for SCSV. */
1303 if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff)) {
1304 /* SCSV is fatal if renegotiating. */
David Benjamin20f6e972015-05-15 21:51:49 -04001305 if (s->s3->initial_handshake_complete) {
David Benjamin3570d732015-06-29 00:28:17 -04001306 OPENSSL_PUT_ERROR(SSL, SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
Adam Langleyfcf25832014-12-18 17:42:32 -08001307 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1308 goto err;
1309 }
1310 s->s3->send_connection_binding = 1;
1311 continue;
1312 }
1313
1314 /* Check for FALLBACK_SCSV. */
1315 if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff)) {
1316 uint16_t max_version = ssl3_get_max_server_version(s);
1317 if (SSL_IS_DTLS(s) ? (uint16_t)s->version > max_version
1318 : (uint16_t)s->version < max_version) {
David Benjamin3570d732015-06-29 00:28:17 -04001319 OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK);
Adam Langleyfcf25832014-12-18 17:42:32 -08001320 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
1321 goto err;
1322 }
1323 continue;
1324 }
1325
David Benjamina1c90a52015-05-30 17:03:14 -04001326 c = SSL_get_cipher_by_value(cipher_suite);
Adam Langleyfcf25832014-12-18 17:42:32 -08001327 if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
David Benjamin3570d732015-06-29 00:28:17 -04001328 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001329 goto err;
1330 }
1331 }
1332
1333 return sk;
David Benjamin9f2c0d72014-10-21 22:00:19 -04001334
Adam Langley95c29f32014-06-20 12:00:00 -07001335err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001336 sk_SSL_CIPHER_free(sk);
Adam Langleyfcf25832014-12-18 17:42:32 -08001337 return NULL;
1338}
Adam Langley95c29f32014-06-20 12:00:00 -07001339
1340
Adam Langleyfcf25832014-12-18 17:42:32 -08001341/* return a servername extension value if provided in Client Hello, or NULL. So
1342 * far, only host_name types are defined (RFC 3546). */
1343const char *SSL_get_servername(const SSL *s, const int type) {
1344 if (type != TLSEXT_NAMETYPE_host_name) {
1345 return NULL;
1346 }
Adam Langley95c29f32014-06-20 12:00:00 -07001347
Adam Langleyfcf25832014-12-18 17:42:32 -08001348 return s->session && !s->tlsext_hostname ? s->session->tlsext_hostname
1349 : s->tlsext_hostname;
1350}
Adam Langley95c29f32014-06-20 12:00:00 -07001351
Adam Langleyfcf25832014-12-18 17:42:32 -08001352int SSL_get_servername_type(const SSL *s) {
1353 if (s->session &&
1354 (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname)) {
1355 return TLSEXT_NAMETYPE_host_name;
1356 }
Adam Langley95c29f32014-06-20 12:00:00 -07001357
Adam Langleyfcf25832014-12-18 17:42:32 -08001358 return -1;
1359}
Adam Langley95c29f32014-06-20 12:00:00 -07001360
Adam Langleyfcf25832014-12-18 17:42:32 -08001361void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
1362 ctx->signed_cert_timestamps_enabled = 1;
1363}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001364
Adam Langleyfcf25832014-12-18 17:42:32 -08001365int SSL_enable_signed_cert_timestamps(SSL *ssl) {
1366 ssl->signed_cert_timestamps_enabled = 1;
1367 return 1;
1368}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001369
Adam Langleyfcf25832014-12-18 17:42:32 -08001370void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
1371 ctx->ocsp_stapling_enabled = 1;
1372}
David Benjamin6c7aed02014-08-27 16:42:38 -04001373
Adam Langleyfcf25832014-12-18 17:42:32 -08001374int SSL_enable_ocsp_stapling(SSL *ssl) {
1375 ssl->ocsp_stapling_enabled = 1;
1376 return 1;
1377}
David Benjamin6c7aed02014-08-27 16:42:38 -04001378
Adam Langleyfcf25832014-12-18 17:42:32 -08001379void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
1380 size_t *out_len) {
1381 SSL_SESSION *session = ssl->session;
Adam Langley3cb50e02014-08-26 14:00:31 -07001382
Adam Langleyfcf25832014-12-18 17:42:32 -08001383 *out_len = 0;
1384 *out = NULL;
1385 if (ssl->server || !session || !session->tlsext_signed_cert_timestamp_list) {
1386 return;
1387 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001388
Adam Langleyfcf25832014-12-18 17:42:32 -08001389 *out = session->tlsext_signed_cert_timestamp_list;
1390 *out_len = session->tlsext_signed_cert_timestamp_list_length;
1391}
David Benjamin6c7aed02014-08-27 16:42:38 -04001392
Adam Langleyfcf25832014-12-18 17:42:32 -08001393void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
1394 size_t *out_len) {
1395 SSL_SESSION *session = ssl->session;
1396
1397 *out_len = 0;
1398 *out = NULL;
1399 if (ssl->server || !session || !session->ocsp_response) {
1400 return;
1401 }
1402 *out = session->ocsp_response;
1403 *out_len = session->ocsp_response_length;
1404}
David Benjamin6c7aed02014-08-27 16:42:38 -04001405
Adam Langley95c29f32014-06-20 12:00:00 -07001406/* SSL_select_next_proto implements the standard protocol selection. It is
1407 * expected that this function is called from the callback set by
1408 * SSL_CTX_set_next_proto_select_cb.
1409 *
1410 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1411 * strings. The length byte itself is not included in the length. A byte
1412 * string of length 0 is invalid. No byte string may be truncated.
1413 *
1414 * The current, but experimental algorithm for selecting the protocol is:
1415 *
1416 * 1) If the server doesn't support NPN then this is indicated to the
1417 * callback. In this case, the client application has to abort the connection
1418 * or have a default application level protocol.
1419 *
1420 * 2) If the server supports NPN, but advertises an empty list then the
1421 * client selects the first protcol in its list, but indicates via the
1422 * API that this fallback case was enacted.
1423 *
1424 * 3) Otherwise, the client finds the first protocol in the server's list
1425 * that it supports and selects this protocol. This is because it's
1426 * assumed that the server has better information about which protocol
1427 * a client should use.
1428 *
1429 * 4) If the client doesn't support any of the server's advertised
1430 * protocols, then this is treated the same as case 2.
1431 *
1432 * It returns either
1433 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1434 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1435 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001436int SSL_select_next_proto(uint8_t **out, uint8_t *outlen, const uint8_t *server,
1437 unsigned int server_len, const uint8_t *client,
1438 unsigned int client_len) {
1439 unsigned int i, j;
1440 const uint8_t *result;
1441 int status = OPENSSL_NPN_UNSUPPORTED;
Adam Langley95c29f32014-06-20 12:00:00 -07001442
Adam Langleyfcf25832014-12-18 17:42:32 -08001443 /* For each protocol in server preference order, see if we support it. */
1444 for (i = 0; i < server_len;) {
1445 for (j = 0; j < client_len;) {
1446 if (server[i] == client[j] &&
1447 memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
1448 /* We found a match */
1449 result = &server[i];
1450 status = OPENSSL_NPN_NEGOTIATED;
1451 goto found;
1452 }
1453 j += client[j];
1454 j++;
1455 }
1456 i += server[i];
1457 i++;
1458 }
Adam Langley95c29f32014-06-20 12:00:00 -07001459
Adam Langleyfcf25832014-12-18 17:42:32 -08001460 /* There's no overlap between our protocols and the server's list. */
1461 result = client;
1462 status = OPENSSL_NPN_NO_OVERLAP;
Adam Langley95c29f32014-06-20 12:00:00 -07001463
Adam Langleyfcf25832014-12-18 17:42:32 -08001464found:
1465 *out = (uint8_t *)result + 1;
1466 *outlen = result[0];
1467 return status;
1468}
Adam Langley95c29f32014-06-20 12:00:00 -07001469
Adam Langley95c29f32014-06-20 12:00:00 -07001470/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
1471 * requested protocol for this connection and returns 0. If the client didn't
1472 * request any protocol, then *data is set to NULL.
1473 *
1474 * Note that the client can request any protocol it chooses. The value returned
1475 * from this function need not be a member of the list of supported protocols
Adam Langleyfcf25832014-12-18 17:42:32 -08001476 * provided by the callback. */
1477void SSL_get0_next_proto_negotiated(const SSL *s, const uint8_t **data,
1478 unsigned *len) {
1479 *data = s->next_proto_negotiated;
1480 if (!*data) {
1481 *len = 0;
1482 } else {
1483 *len = s->next_proto_negotiated_len;
1484 }
Adam Langley95c29f32014-06-20 12:00:00 -07001485}
1486
1487/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
1488 * TLS server needs a list of supported protocols for Next Protocol
1489 * Negotiation. The returned list must be in wire format. The list is returned
1490 * by setting |out| to point to it and |outlen| to its length. This memory will
1491 * not be modified, but one should assume that the SSL* keeps a reference to
1492 * it.
1493 *
Adam Langleyfcf25832014-12-18 17:42:32 -08001494 * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise.
1495 * Otherwise, no such extension will be included in the ServerHello. */
1496void SSL_CTX_set_next_protos_advertised_cb(
1497 SSL_CTX *ctx,
1498 int (*cb)(SSL *ssl, const uint8_t **out, unsigned int *outlen, void *arg),
1499 void *arg) {
1500 ctx->next_protos_advertised_cb = cb;
1501 ctx->next_protos_advertised_cb_arg = arg;
1502}
Adam Langley95c29f32014-06-20 12:00:00 -07001503
1504/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1505 * client needs to select a protocol from the server's provided list. |out|
1506 * must be set to point to the selected protocol (which may be within |in|).
1507 * The length of the protocol name must be written into |outlen|. The server's
1508 * advertised protocols are provided in |in| and |inlen|. The callback can
1509 * assume that |in| is syntactically valid.
1510 *
1511 * The client must select a protocol. It is fatal to the connection if this
1512 * callback returns a value other than SSL_TLSEXT_ERR_OK.
1513 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001514void SSL_CTX_set_next_proto_select_cb(
1515 SSL_CTX *ctx, int (*cb)(SSL *s, uint8_t **out, uint8_t *outlen,
1516 const uint8_t *in, unsigned int inlen, void *arg),
1517 void *arg) {
1518 ctx->next_proto_select_cb = cb;
1519 ctx->next_proto_select_cb_arg = arg;
1520}
Adam Langley95c29f32014-06-20 12:00:00 -07001521
Adam Langleyfcf25832014-12-18 17:42:32 -08001522int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
1523 unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001524 OPENSSL_free(ctx->alpn_client_proto_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001525 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1526 if (!ctx->alpn_client_proto_list) {
1527 return 1;
1528 }
1529 ctx->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001530
Adam Langleyfcf25832014-12-18 17:42:32 -08001531 return 0;
1532}
Adam Langley95c29f32014-06-20 12:00:00 -07001533
Adam Langleyfcf25832014-12-18 17:42:32 -08001534int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001535 OPENSSL_free(ssl->alpn_client_proto_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001536 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1537 if (!ssl->alpn_client_proto_list) {
1538 return 1;
1539 }
1540 ssl->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001541
Adam Langleyfcf25832014-12-18 17:42:32 -08001542 return 0;
1543}
Adam Langley95c29f32014-06-20 12:00:00 -07001544
1545/* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
1546 * during ClientHello processing in order to select an ALPN protocol from the
1547 * client's list of offered protocols. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001548void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
1549 int (*cb)(SSL *ssl, const uint8_t **out,
1550 uint8_t *outlen, const uint8_t *in,
1551 unsigned int inlen, void *arg),
1552 void *arg) {
1553 ctx->alpn_select_cb = cb;
1554 ctx->alpn_select_cb_arg = arg;
1555}
Adam Langley95c29f32014-06-20 12:00:00 -07001556
1557/* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
1558 * On return it sets |*data| to point to |*len| bytes of protocol name (not
1559 * including the leading length-prefix byte). If the server didn't respond with
1560 * a negotiated protocol then |*len| will be zero. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001561void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **data,
1562 unsigned *len) {
1563 *data = NULL;
1564 if (ssl->s3) {
1565 *data = ssl->s3->alpn_selected;
1566 }
1567 if (*data == NULL) {
1568 *len = 0;
1569 } else {
1570 *len = ssl->s3->alpn_selected_len;
1571 }
1572}
Adam Langley95c29f32014-06-20 12:00:00 -07001573
David Benjamincfd248b2015-04-03 11:02:24 -04001574int SSL_export_keying_material(SSL *s, uint8_t *out, size_t out_len,
1575 const char *label, size_t label_len,
1576 const uint8_t *context, size_t context_len,
1577 int use_context) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001578 if (s->version < TLS1_VERSION) {
David Benjaminc565ebb2015-04-03 04:06:36 -04001579 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001580 }
Adam Langley95c29f32014-06-20 12:00:00 -07001581
David Benjamincfd248b2015-04-03 11:02:24 -04001582 return s->enc_method->export_keying_material(
1583 s, out, out_len, label, label_len, context, context_len, use_context);
Adam Langleyfcf25832014-12-18 17:42:32 -08001584}
Adam Langley95c29f32014-06-20 12:00:00 -07001585
Adam Langleyfcf25832014-12-18 17:42:32 -08001586static uint32_t ssl_session_hash(const SSL_SESSION *a) {
1587 uint32_t hash =
1588 ((uint32_t)a->session_id[0]) ||
1589 ((uint32_t)a->session_id[1] << 8) ||
1590 ((uint32_t)a->session_id[2] << 16) ||
1591 ((uint32_t)a->session_id[3] << 24);
Adam Langley95c29f32014-06-20 12:00:00 -07001592
Adam Langleyfcf25832014-12-18 17:42:32 -08001593 return hash;
1594}
Adam Langley95c29f32014-06-20 12:00:00 -07001595
1596/* NB: If this function (or indeed the hash function which uses a sort of
1597 * coarser function than this one) is changed, ensure
1598 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1599 * able to construct an SSL_SESSION that will collide with any existing session
1600 * with a matching session ID. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001601static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
1602 if (a->ssl_version != b->ssl_version) {
1603 return 1;
1604 }
Adam Langley95c29f32014-06-20 12:00:00 -07001605
Adam Langleyfcf25832014-12-18 17:42:32 -08001606 if (a->session_id_length != b->session_id_length) {
1607 return 1;
1608 }
Adam Langley95c29f32014-06-20 12:00:00 -07001609
Adam Langleyfcf25832014-12-18 17:42:32 -08001610 return memcmp(a->session_id, b->session_id, a->session_id_length);
1611}
Adam Langley95c29f32014-06-20 12:00:00 -07001612
David Benjamin4831c332015-05-16 11:43:13 -04001613SSL_CTX *SSL_CTX_new(const SSL_METHOD *method) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001614 SSL_CTX *ret = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001615
David Benjamin4831c332015-05-16 11:43:13 -04001616 if (method == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001617 OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_METHOD_PASSED);
Adam Langleyfcf25832014-12-18 17:42:32 -08001618 return NULL;
1619 }
Adam Langley95c29f32014-06-20 12:00:00 -07001620
Adam Langleyfcf25832014-12-18 17:42:32 -08001621 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001622 OPENSSL_PUT_ERROR(SSL, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
Adam Langleyfcf25832014-12-18 17:42:32 -08001623 goto err;
1624 }
Adam Langley95c29f32014-06-20 12:00:00 -07001625
Adam Langleyfcf25832014-12-18 17:42:32 -08001626 ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1627 if (ret == NULL) {
1628 goto err;
1629 }
Adam Langley95c29f32014-06-20 12:00:00 -07001630
Adam Langleyfcf25832014-12-18 17:42:32 -08001631 memset(ret, 0, sizeof(SSL_CTX));
Adam Langley95c29f32014-06-20 12:00:00 -07001632
David Benjamin4831c332015-05-16 11:43:13 -04001633 ret->method = method->method;
Adam Langley95c29f32014-06-20 12:00:00 -07001634
Adam Langley4bdb6e42015-05-15 15:29:21 -07001635 CRYPTO_MUTEX_init(&ret->lock);
1636
Adam Langleyfcf25832014-12-18 17:42:32 -08001637 ret->cert_store = NULL;
1638 ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
1639 ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1640 ret->session_cache_head = NULL;
1641 ret->session_cache_tail = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001642
Adam Langleyfcf25832014-12-18 17:42:32 -08001643 /* We take the system default */
1644 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001645
Adam Langleyfcf25832014-12-18 17:42:32 -08001646 ret->new_session_cb = 0;
1647 ret->remove_session_cb = 0;
1648 ret->get_session_cb = 0;
1649 ret->generate_session_id = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001650
Adam Langleyfcf25832014-12-18 17:42:32 -08001651 ret->references = 1;
1652 ret->quiet_shutdown = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001653
Adam Langleyfcf25832014-12-18 17:42:32 -08001654 ret->info_callback = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001655
Adam Langleyfcf25832014-12-18 17:42:32 -08001656 ret->app_verify_callback = 0;
1657 ret->app_verify_arg = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001658
Adam Langleyfcf25832014-12-18 17:42:32 -08001659 ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
Adam Langleyfcf25832014-12-18 17:42:32 -08001660 ret->msg_callback = 0;
1661 ret->msg_callback_arg = NULL;
1662 ret->verify_mode = SSL_VERIFY_NONE;
1663 ret->sid_ctx_length = 0;
1664 ret->default_verify_callback = NULL;
1665 ret->cert = ssl_cert_new();
1666 if (ret->cert == NULL) {
1667 goto err;
1668 }
Adam Langley95c29f32014-06-20 12:00:00 -07001669
Adam Langleyfcf25832014-12-18 17:42:32 -08001670 ret->default_passwd_callback = 0;
1671 ret->default_passwd_callback_userdata = NULL;
1672 ret->client_cert_cb = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001673
Adam Langleyfcf25832014-12-18 17:42:32 -08001674 ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
1675 if (ret->sessions == NULL) {
1676 goto err;
1677 }
1678 ret->cert_store = X509_STORE_new();
1679 if (ret->cert_store == NULL) {
1680 goto err;
1681 }
Adam Langley95c29f32014-06-20 12:00:00 -07001682
Adam Langleyfcf25832014-12-18 17:42:32 -08001683 ssl_create_cipher_list(ret->method, &ret->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001684 &ret->cipher_list_by_id, SSL_DEFAULT_CIPHER_LIST);
Adam Langleyfcf25832014-12-18 17:42:32 -08001685 if (ret->cipher_list == NULL ||
1686 sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001687 OPENSSL_PUT_ERROR(SSL, SSL_R_LIBRARY_HAS_NO_CIPHERS);
Adam Langleyfcf25832014-12-18 17:42:32 -08001688 goto err2;
1689 }
Adam Langley95c29f32014-06-20 12:00:00 -07001690
Adam Langleyfcf25832014-12-18 17:42:32 -08001691 ret->param = X509_VERIFY_PARAM_new();
1692 if (!ret->param) {
1693 goto err;
1694 }
Adam Langley95c29f32014-06-20 12:00:00 -07001695
Adam Langleyfcf25832014-12-18 17:42:32 -08001696 ret->client_CA = sk_X509_NAME_new_null();
1697 if (ret->client_CA == NULL) {
1698 goto err;
1699 }
Adam Langley95c29f32014-06-20 12:00:00 -07001700
David Benjamin9f33fc62015-04-15 17:29:53 -04001701 CRYPTO_new_ex_data(&g_ex_data_class_ssl_ctx, ret, &ret->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001702
Adam Langleyfcf25832014-12-18 17:42:32 -08001703 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
David Benjamin422d3a42014-08-20 11:09:03 -04001704
Adam Langleyfcf25832014-12-18 17:42:32 -08001705 ret->tlsext_servername_callback = 0;
1706 ret->tlsext_servername_arg = NULL;
1707 /* Setup RFC4507 ticket keys */
1708 if (!RAND_bytes(ret->tlsext_tick_key_name, 16) ||
1709 !RAND_bytes(ret->tlsext_tick_hmac_key, 16) ||
1710 !RAND_bytes(ret->tlsext_tick_aes_key, 16)) {
1711 ret->options |= SSL_OP_NO_TICKET;
1712 }
Adam Langley95c29f32014-06-20 12:00:00 -07001713
Adam Langleyfcf25832014-12-18 17:42:32 -08001714 ret->next_protos_advertised_cb = 0;
1715 ret->next_proto_select_cb = 0;
1716 ret->psk_identity_hint = NULL;
1717 ret->psk_client_callback = NULL;
1718 ret->psk_server_callback = NULL;
1719
1720 /* Default is to connect to non-RI servers. When RI is more widely deployed
1721 * might change this. */
1722 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
1723
1724 /* Lock the SSL_CTX to the specified version, for compatibility with legacy
1725 * uses of SSL_METHOD. */
David Benjamin4831c332015-05-16 11:43:13 -04001726 if (method->version != 0) {
1727 SSL_CTX_set_max_version(ret, method->version);
1728 SSL_CTX_set_min_version(ret, method->version);
Adam Langleyfcf25832014-12-18 17:42:32 -08001729 }
1730
1731 return ret;
1732
Adam Langley95c29f32014-06-20 12:00:00 -07001733err:
David Benjamin3570d732015-06-29 00:28:17 -04001734 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -07001735err2:
David Benjamin2755a3e2015-04-22 16:17:58 -04001736 SSL_CTX_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -08001737 return NULL;
1738}
Adam Langley95c29f32014-06-20 12:00:00 -07001739
David Benjamin4fcc2e22015-04-22 12:58:16 -04001740void SSL_CTX_free(SSL_CTX *ctx) {
1741 if (ctx == NULL ||
Adam Langley0b5e3902015-05-15 13:08:38 -07001742 !CRYPTO_refcount_dec_and_test_zero(&ctx->references)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001743 return;
1744 }
Adam Langley95c29f32014-06-20 12:00:00 -07001745
David Benjamin2755a3e2015-04-22 16:17:58 -04001746 X509_VERIFY_PARAM_free(ctx->param);
Adam Langley95c29f32014-06-20 12:00:00 -07001747
Adam Langleyfcf25832014-12-18 17:42:32 -08001748 /* Free internal session cache. However: the remove_cb() may reference the
1749 * ex_data of SSL_CTX, thus the ex_data store can only be removed after the
1750 * sessions were flushed. As the ex_data handling routines might also touch
1751 * the session cache, the most secure solution seems to be: empty (flush) the
1752 * cache, then free ex_data, then finally free the cache. (See ticket
1753 * [openssl.org #212].) */
David Benjamin2755a3e2015-04-22 16:17:58 -04001754 SSL_CTX_flush_sessions(ctx, 0);
Adam Langley95c29f32014-06-20 12:00:00 -07001755
David Benjamin4fcc2e22015-04-22 12:58:16 -04001756 CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, ctx, &ctx->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001757
Adam Langley4bdb6e42015-05-15 15:29:21 -07001758 CRYPTO_MUTEX_cleanup(&ctx->lock);
David Benjamin2755a3e2015-04-22 16:17:58 -04001759 lh_SSL_SESSION_free(ctx->sessions);
1760 X509_STORE_free(ctx->cert_store);
1761 ssl_cipher_preference_list_free(ctx->cipher_list);
1762 sk_SSL_CIPHER_free(ctx->cipher_list_by_id);
1763 ssl_cipher_preference_list_free(ctx->cipher_list_tls11);
1764 ssl_cert_free(ctx->cert);
Adam Langley09505632015-07-30 18:10:13 -07001765 sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->client_custom_extensions,
1766 SSL_CUSTOM_EXTENSION_free);
1767 sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->server_custom_extensions,
1768 SSL_CUSTOM_EXTENSION_free);
David Benjamin2755a3e2015-04-22 16:17:58 -04001769 sk_X509_NAME_pop_free(ctx->client_CA, X509_NAME_free);
David Benjamin2755a3e2015-04-22 16:17:58 -04001770 sk_SRTP_PROTECTION_PROFILE_free(ctx->srtp_profiles);
1771 OPENSSL_free(ctx->psk_identity_hint);
David Benjamin2755a3e2015-04-22 16:17:58 -04001772 OPENSSL_free(ctx->tlsext_ellipticcurvelist);
1773 OPENSSL_free(ctx->alpn_client_proto_list);
1774 EVP_PKEY_free(ctx->tlsext_channel_id_private);
1775 BIO_free(ctx->keylog_bio);
Adam Langley95c29f32014-06-20 12:00:00 -07001776
David Benjamin4fcc2e22015-04-22 12:58:16 -04001777 OPENSSL_free(ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08001778}
Adam Langley95c29f32014-06-20 12:00:00 -07001779
Adam Langleyfcf25832014-12-18 17:42:32 -08001780void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) {
1781 ctx->default_passwd_callback = cb;
1782}
Adam Langley95c29f32014-06-20 12:00:00 -07001783
Adam Langleyfcf25832014-12-18 17:42:32 -08001784void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u) {
1785 ctx->default_passwd_callback_userdata = u;
1786}
Adam Langley95c29f32014-06-20 12:00:00 -07001787
Adam Langleyfcf25832014-12-18 17:42:32 -08001788void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
1789 int (*cb)(X509_STORE_CTX *, void *),
1790 void *arg) {
1791 ctx->app_verify_callback = cb;
1792 ctx->app_verify_arg = arg;
1793}
Adam Langley95c29f32014-06-20 12:00:00 -07001794
Adam Langleyfcf25832014-12-18 17:42:32 -08001795void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
1796 int (*cb)(int, X509_STORE_CTX *)) {
1797 ctx->verify_mode = mode;
1798 ctx->default_verify_callback = cb;
1799}
Adam Langley95c29f32014-06-20 12:00:00 -07001800
Adam Langleyfcf25832014-12-18 17:42:32 -08001801void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
1802 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
1803}
Adam Langley1258b6a2014-06-20 12:00:00 -07001804
David Benjamin7481d392015-07-05 19:38:46 -04001805void SSL_CTX_set_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl, void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08001806 void *arg) {
David Benjamin7481d392015-07-05 19:38:46 -04001807 ssl_cert_set_cert_cb(ctx->cert, cb, arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08001808}
David Benjamin859ec3c2014-09-02 16:29:36 -04001809
David Benjamin7481d392015-07-05 19:38:46 -04001810void SSL_set_cert_cb(SSL *ssl, int (*cb)(SSL *ssl, void *arg), void *arg) {
1811 ssl_cert_set_cert_cb(ssl->cert, cb, arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08001812}
Adam Langley95c29f32014-06-20 12:00:00 -07001813
David Benjamin107db582015-04-08 00:41:59 -04001814void ssl_get_compatible_server_ciphers(SSL *s, uint32_t *out_mask_k,
1815 uint32_t *out_mask_a) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001816 CERT *c = s->cert;
David Benjamind1d80782015-07-05 11:54:09 -04001817 int have_rsa_cert = 0, dh_tmp;
David Benjamin107db582015-04-08 00:41:59 -04001818 uint32_t mask_k, mask_a;
David Benjamind1d80782015-07-05 11:54:09 -04001819 int have_ecc_cert = 0, ecdsa_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001820 X509 *x;
Adam Langley95c29f32014-06-20 12:00:00 -07001821
Adam Langleyfcf25832014-12-18 17:42:32 -08001822 if (c == NULL) {
1823 /* TODO(davidben): Is this codepath possible? */
1824 *out_mask_k = 0;
1825 *out_mask_a = 0;
1826 return;
1827 }
Adam Langley95c29f32014-06-20 12:00:00 -07001828
Adam Langleyfcf25832014-12-18 17:42:32 -08001829 dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001830
nagendra modadugu601448a2015-07-24 09:31:31 -07001831 if (s->cert->x509 != NULL && ssl_has_private_key(s)) {
1832 if (ssl_private_key_type(s) == EVP_PKEY_RSA) {
David Benjamind1d80782015-07-05 11:54:09 -04001833 have_rsa_cert = 1;
nagendra modadugu601448a2015-07-24 09:31:31 -07001834 } else if (ssl_private_key_type(s) == EVP_PKEY_EC) {
David Benjamind1d80782015-07-05 11:54:09 -04001835 have_ecc_cert = 1;
1836 }
1837 }
nagendra modadugu601448a2015-07-24 09:31:31 -07001838
Adam Langleyfcf25832014-12-18 17:42:32 -08001839 mask_k = 0;
1840 mask_a = 0;
David Benjaminf31e6812014-11-13 18:05:55 -05001841
Adam Langleyfcf25832014-12-18 17:42:32 -08001842 if (dh_tmp) {
David Benjamin7061e282015-03-19 11:10:48 -04001843 mask_k |= SSL_kDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001844 }
David Benjaminbb20f522015-07-04 17:18:14 -04001845 if (have_rsa_cert) {
1846 mask_k |= SSL_kRSA;
Adam Langleyfcf25832014-12-18 17:42:32 -08001847 mask_a |= SSL_aRSA;
1848 }
Adam Langley95c29f32014-06-20 12:00:00 -07001849
Adam Langleyfcf25832014-12-18 17:42:32 -08001850 /* An ECC certificate may be usable for ECDSA cipher suites depending on the
1851 * key usage extension and on the client's curve preferences. */
1852 if (have_ecc_cert) {
David Benjamind1d80782015-07-05 11:54:09 -04001853 x = c->x509;
Adam Langleyfcf25832014-12-18 17:42:32 -08001854 /* This call populates extension flags (ex_flags). */
1855 X509_check_purpose(x, -1, 0);
1856 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE)
1857 ? (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)
1858 : 1;
1859 if (!tls1_check_ec_cert(s, x)) {
1860 ecdsa_ok = 0;
1861 }
1862 if (ecdsa_ok) {
1863 mask_a |= SSL_aECDSA;
1864 }
1865 }
Adam Langley95c29f32014-06-20 12:00:00 -07001866
Adam Langleyfcf25832014-12-18 17:42:32 -08001867 /* If we are considering an ECC cipher suite that uses an ephemeral EC
1868 * key, check it. */
David Benjamindd978782015-04-24 15:20:13 -04001869 if (tls1_check_ec_tmp_key(s)) {
David Benjamin7061e282015-03-19 11:10:48 -04001870 mask_k |= SSL_kECDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001871 }
Adam Langley95c29f32014-06-20 12:00:00 -07001872
Adam Langleyfcf25832014-12-18 17:42:32 -08001873 /* PSK requires a server callback. */
1874 if (s->psk_server_callback != NULL) {
1875 mask_k |= SSL_kPSK;
1876 mask_a |= SSL_aPSK;
1877 }
Adam Langley95c29f32014-06-20 12:00:00 -07001878
Adam Langleyfcf25832014-12-18 17:42:32 -08001879 *out_mask_k = mask_k;
1880 *out_mask_a = mask_a;
1881}
Adam Langley95c29f32014-06-20 12:00:00 -07001882
Adam Langleyfcf25832014-12-18 17:42:32 -08001883void ssl_update_cache(SSL *s, int mode) {
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001884 /* Never cache sessions with empty session IDs. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001885 if (s->session->session_id_length == 0) {
1886 return;
1887 }
Adam Langley95c29f32014-06-20 12:00:00 -07001888
David Benjamin95d31822015-06-15 19:53:32 -04001889 int has_new_session = !s->hit;
1890 if (!s->server && s->tlsext_ticket_expected) {
1891 /* A client may see new sessions on abbreviated handshakes if the server
1892 * decides to renew the ticket. Once the handshake is completed, it should
1893 * be inserted into the cache. */
1894 has_new_session = 1;
1895 }
1896
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001897 SSL_CTX *ctx = s->initial_ctx;
David Benjamin95d31822015-06-15 19:53:32 -04001898 if ((ctx->session_cache_mode & mode) == mode && has_new_session &&
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001899 ((ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) ||
1900 SSL_CTX_add_session(ctx, s->session)) &&
1901 ctx->new_session_cb != NULL) {
1902 /* Note: |new_session_cb| is called whether the internal session cache is
1903 * used or not. */
1904 if (!ctx->new_session_cb(s, SSL_SESSION_up_ref(s->session))) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001905 SSL_SESSION_free(s->session);
1906 }
1907 }
Adam Langley95c29f32014-06-20 12:00:00 -07001908
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001909 if (!(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR) &&
1910 !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) &&
1911 (ctx->session_cache_mode & mode) == mode) {
1912 /* Automatically flush the internal session cache every 255 connections. */
1913 int flush_cache = 0;
Adam Langley4bdb6e42015-05-15 15:29:21 -07001914 CRYPTO_MUTEX_lock_write(&ctx->lock);
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001915 ctx->handshakes_since_cache_flush++;
1916 if (ctx->handshakes_since_cache_flush >= 255) {
1917 flush_cache = 1;
1918 ctx->handshakes_since_cache_flush = 0;
1919 }
Adam Langley4bdb6e42015-05-15 15:29:21 -07001920 CRYPTO_MUTEX_unlock(&ctx->lock);
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001921
1922 if (flush_cache) {
1923 SSL_CTX_flush_sessions(ctx, (unsigned long)time(NULL));
Adam Langleyfcf25832014-12-18 17:42:32 -08001924 }
1925 }
1926}
Adam Langley95c29f32014-06-20 12:00:00 -07001927
David Benjamin1a5c50f2015-03-11 16:22:37 -04001928int SSL_get_error(const SSL *s, int ret_code) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001929 int reason;
David Benjamin1a5c50f2015-03-11 16:22:37 -04001930 uint32_t err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001931 BIO *bio;
Adam Langley95c29f32014-06-20 12:00:00 -07001932
David Benjamin1a5c50f2015-03-11 16:22:37 -04001933 if (ret_code > 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001934 return SSL_ERROR_NONE;
1935 }
Adam Langley95c29f32014-06-20 12:00:00 -07001936
Adam Langleyfcf25832014-12-18 17:42:32 -08001937 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
1938 * where we do encode the error */
David Benjamin1a5c50f2015-03-11 16:22:37 -04001939 err = ERR_peek_error();
1940 if (err != 0) {
1941 if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001942 return SSL_ERROR_SYSCALL;
1943 }
1944 return SSL_ERROR_SSL;
1945 }
Adam Langley95c29f32014-06-20 12:00:00 -07001946
David Benjamin1a5c50f2015-03-11 16:22:37 -04001947 if (ret_code == 0) {
David Benjamin9a38e922015-01-22 16:06:11 -05001948 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
1949 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) {
1950 /* The socket was cleanly shut down with a close_notify. */
1951 return SSL_ERROR_ZERO_RETURN;
1952 }
1953 /* An EOF was observed which violates the protocol, and the underlying
1954 * transport does not participate in the error queue. Bubble up to the
1955 * caller. */
1956 return SSL_ERROR_SYSCALL;
Adam Langleyfcf25832014-12-18 17:42:32 -08001957 }
Adam Langleyb2ce0582014-06-20 12:00:00 -07001958
Adam Langleyfcf25832014-12-18 17:42:32 -08001959 if (SSL_want_session(s)) {
1960 return SSL_ERROR_PENDING_SESSION;
1961 }
Adam Langley95c29f32014-06-20 12:00:00 -07001962
Adam Langleyfcf25832014-12-18 17:42:32 -08001963 if (SSL_want_certificate(s)) {
1964 return SSL_ERROR_PENDING_CERTIFICATE;
1965 }
Adam Langley95c29f32014-06-20 12:00:00 -07001966
Adam Langleyfcf25832014-12-18 17:42:32 -08001967 if (SSL_want_read(s)) {
1968 bio = SSL_get_rbio(s);
1969 if (BIO_should_read(bio)) {
1970 return SSL_ERROR_WANT_READ;
1971 }
Adam Langley95c29f32014-06-20 12:00:00 -07001972
Adam Langleyfcf25832014-12-18 17:42:32 -08001973 if (BIO_should_write(bio)) {
1974 /* This one doesn't make too much sense ... We never try to write to the
1975 * rbio, and an application program where rbio and wbio are separate
1976 * couldn't even know what it should wait for. However if we ever set
1977 * s->rwstate incorrectly (so that we have SSL_want_read(s) instead of
1978 * SSL_want_write(s)) and rbio and wbio *are* the same, this test works
1979 * around that bug; so it might be safer to keep it. */
1980 return SSL_ERROR_WANT_WRITE;
1981 }
Adam Langley95c29f32014-06-20 12:00:00 -07001982
Adam Langleyfcf25832014-12-18 17:42:32 -08001983 if (BIO_should_io_special(bio)) {
1984 reason = BIO_get_retry_reason(bio);
1985 if (reason == BIO_RR_CONNECT) {
1986 return SSL_ERROR_WANT_CONNECT;
1987 }
Adam Langley95c29f32014-06-20 12:00:00 -07001988
Adam Langleyfcf25832014-12-18 17:42:32 -08001989 if (reason == BIO_RR_ACCEPT) {
1990 return SSL_ERROR_WANT_ACCEPT;
1991 }
Adam Langley95c29f32014-06-20 12:00:00 -07001992
Adam Langleyfcf25832014-12-18 17:42:32 -08001993 return SSL_ERROR_SYSCALL; /* unknown */
1994 }
1995 }
Adam Langley95c29f32014-06-20 12:00:00 -07001996
Adam Langleyfcf25832014-12-18 17:42:32 -08001997 if (SSL_want_write(s)) {
1998 bio = SSL_get_wbio(s);
1999 if (BIO_should_write(bio)) {
2000 return SSL_ERROR_WANT_WRITE;
2001 }
Adam Langley95c29f32014-06-20 12:00:00 -07002002
Adam Langleyfcf25832014-12-18 17:42:32 -08002003 if (BIO_should_read(bio)) {
2004 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2005 return SSL_ERROR_WANT_READ;
2006 }
Adam Langley95c29f32014-06-20 12:00:00 -07002007
Adam Langleyfcf25832014-12-18 17:42:32 -08002008 if (BIO_should_io_special(bio)) {
2009 reason = BIO_get_retry_reason(bio);
2010 if (reason == BIO_RR_CONNECT) {
2011 return SSL_ERROR_WANT_CONNECT;
2012 }
Adam Langley95c29f32014-06-20 12:00:00 -07002013
Adam Langleyfcf25832014-12-18 17:42:32 -08002014 if (reason == BIO_RR_ACCEPT) {
2015 return SSL_ERROR_WANT_ACCEPT;
2016 }
Adam Langley95c29f32014-06-20 12:00:00 -07002017
Adam Langleyfcf25832014-12-18 17:42:32 -08002018 return SSL_ERROR_SYSCALL;
2019 }
2020 }
Adam Langley95c29f32014-06-20 12:00:00 -07002021
Adam Langleyfcf25832014-12-18 17:42:32 -08002022 if (SSL_want_x509_lookup(s)) {
2023 return SSL_ERROR_WANT_X509_LOOKUP;
2024 }
Adam Langley95c29f32014-06-20 12:00:00 -07002025
Adam Langleyfcf25832014-12-18 17:42:32 -08002026 if (SSL_want_channel_id_lookup(s)) {
2027 return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
2028 }
Adam Langley0f4746e2014-08-13 12:26:32 -07002029
David Benjaminb4d65fd2015-05-29 17:11:21 -04002030 if (SSL_want_private_key_operation(s)) {
2031 return SSL_ERROR_WANT_PRIVATE_KEY_OPERATION;
2032 }
2033
Adam Langleyfcf25832014-12-18 17:42:32 -08002034 return SSL_ERROR_SYSCALL;
2035}
Adam Langley0f4746e2014-08-13 12:26:32 -07002036
Adam Langleyfcf25832014-12-18 17:42:32 -08002037int SSL_do_handshake(SSL *s) {
2038 int ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002039
Adam Langleyfcf25832014-12-18 17:42:32 -08002040 if (s->handshake_func == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04002041 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_TYPE_NOT_SET);
Adam Langleyfcf25832014-12-18 17:42:32 -08002042 return -1;
2043 }
Adam Langley95c29f32014-06-20 12:00:00 -07002044
Adam Langleyfcf25832014-12-18 17:42:32 -08002045 if (SSL_in_init(s)) {
2046 ret = s->handshake_func(s);
2047 }
2048 return ret;
2049}
Adam Langley95c29f32014-06-20 12:00:00 -07002050
David Benjamin4831c332015-05-16 11:43:13 -04002051void SSL_set_accept_state(SSL *ssl) {
2052 ssl->server = 1;
2053 ssl->shutdown = 0;
David Benjamin8ec88102015-05-15 23:40:13 -04002054 ssl->state = SSL_ST_ACCEPT;
David Benjamin4831c332015-05-16 11:43:13 -04002055 ssl->handshake_func = ssl->method->ssl_accept;
Adam Langleyfcf25832014-12-18 17:42:32 -08002056 /* clear the current cipher */
David Benjamin4831c332015-05-16 11:43:13 -04002057 ssl_clear_cipher_ctx(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08002058}
Adam Langley95c29f32014-06-20 12:00:00 -07002059
David Benjamin4831c332015-05-16 11:43:13 -04002060void SSL_set_connect_state(SSL *ssl) {
2061 ssl->server = 0;
2062 ssl->shutdown = 0;
David Benjamin8ec88102015-05-15 23:40:13 -04002063 ssl->state = SSL_ST_CONNECT;
David Benjamin4831c332015-05-16 11:43:13 -04002064 ssl->handshake_func = ssl->method->ssl_connect;
Adam Langleyfcf25832014-12-18 17:42:32 -08002065 /* clear the current cipher */
David Benjamin4831c332015-05-16 11:43:13 -04002066 ssl_clear_cipher_ctx(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08002067}
Adam Langley95c29f32014-06-20 12:00:00 -07002068
Adam Langleyfcf25832014-12-18 17:42:32 -08002069static const char *ssl_get_version(int version) {
2070 switch (version) {
2071 case TLS1_2_VERSION:
2072 return "TLSv1.2";
Adam Langley95c29f32014-06-20 12:00:00 -07002073
Adam Langleyfcf25832014-12-18 17:42:32 -08002074 case TLS1_1_VERSION:
2075 return "TLSv1.1";
Adam Langley95c29f32014-06-20 12:00:00 -07002076
Adam Langleyfcf25832014-12-18 17:42:32 -08002077 case TLS1_VERSION:
2078 return "TLSv1";
Adam Langley95c29f32014-06-20 12:00:00 -07002079
Adam Langleyfcf25832014-12-18 17:42:32 -08002080 case SSL3_VERSION:
2081 return "SSLv3";
Adam Langley95c29f32014-06-20 12:00:00 -07002082
David Benjamin1c722b72015-04-20 13:53:10 -04002083 case DTLS1_VERSION:
2084 return "DTLSv1";
2085
2086 case DTLS1_2_VERSION:
2087 return "DTLSv1.2";
2088
Adam Langleyfcf25832014-12-18 17:42:32 -08002089 default:
2090 return "unknown";
2091 }
2092}
Adam Langley95c29f32014-06-20 12:00:00 -07002093
Adam Langleyfcf25832014-12-18 17:42:32 -08002094const char *SSL_get_version(const SSL *s) {
2095 return ssl_get_version(s->version);
2096}
Adam Langley95c29f32014-06-20 12:00:00 -07002097
Adam Langleyfcf25832014-12-18 17:42:32 -08002098const char *SSL_SESSION_get_version(const SSL_SESSION *sess) {
2099 return ssl_get_version(sess->ssl_version);
2100}
Adam Langley95c29f32014-06-20 12:00:00 -07002101
Sigbjorn Vik2b23d242015-06-29 15:07:26 +02002102const char* SSL_get_curve_name(uint16_t curve_id) {
2103 return tls1_ec_curve_id2name(curve_id);
2104}
2105
Adam Langleyfcf25832014-12-18 17:42:32 -08002106void ssl_clear_cipher_ctx(SSL *s) {
David Benjamin31a07792015-03-03 14:20:26 -05002107 SSL_AEAD_CTX_free(s->aead_read_ctx);
2108 s->aead_read_ctx = NULL;
2109 SSL_AEAD_CTX_free(s->aead_write_ctx);
2110 s->aead_write_ctx = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002111}
Adam Langley95c29f32014-06-20 12:00:00 -07002112
Adam Langleyfcf25832014-12-18 17:42:32 -08002113X509 *SSL_get_certificate(const SSL *s) {
2114 if (s->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04002115 return s->cert->x509;
Adam Langleyfcf25832014-12-18 17:42:32 -08002116 }
2117
2118 return NULL;
2119}
2120
2121EVP_PKEY *SSL_get_privatekey(const SSL *s) {
2122 if (s->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04002123 return s->cert->privatekey;
Adam Langleyfcf25832014-12-18 17:42:32 -08002124 }
2125
2126 return NULL;
2127}
2128
2129X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
2130 if (ctx->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04002131 return ctx->cert->x509;
Adam Langleyfcf25832014-12-18 17:42:32 -08002132 }
2133
2134 return NULL;
2135}
2136
2137EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
2138 if (ctx->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04002139 return ctx->cert->privatekey;
Adam Langleyfcf25832014-12-18 17:42:32 -08002140 }
2141
2142 return NULL;
2143}
2144
2145const SSL_CIPHER *SSL_get_current_cipher(const SSL *s) {
David Benjamina07c0fc2015-05-13 13:19:42 -04002146 if (s->aead_write_ctx == NULL) {
2147 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002148 }
David Benjamina07c0fc2015-05-13 13:19:42 -04002149 return s->aead_write_ctx->cipher;
Adam Langleyfcf25832014-12-18 17:42:32 -08002150}
2151
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07002152const COMP_METHOD *SSL_get_current_compression(SSL *s) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002153
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07002154const COMP_METHOD *SSL_get_current_expansion(SSL *s) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002155
2156int ssl_init_wbio_buffer(SSL *s, int push) {
2157 BIO *bbio;
2158
2159 if (s->bbio == NULL) {
2160 bbio = BIO_new(BIO_f_buffer());
2161 if (bbio == NULL) {
2162 return 0;
2163 }
2164 s->bbio = bbio;
2165 } else {
2166 bbio = s->bbio;
2167 if (s->bbio == s->wbio) {
2168 s->wbio = BIO_pop(s->wbio);
2169 }
2170 }
2171
2172 BIO_reset(bbio);
2173 if (!BIO_set_read_buffer_size(bbio, 1)) {
David Benjamin3570d732015-06-29 00:28:17 -04002174 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -08002175 return 0;
2176 }
2177
2178 if (push) {
2179 if (s->wbio != bbio) {
2180 s->wbio = BIO_push(bbio, s->wbio);
2181 }
2182 } else {
2183 if (s->wbio == bbio) {
2184 s->wbio = BIO_pop(bbio);
2185 }
2186 }
2187
2188 return 1;
2189}
2190
2191void ssl_free_wbio_buffer(SSL *s) {
2192 if (s->bbio == NULL) {
2193 return;
2194 }
2195
2196 if (s->bbio == s->wbio) {
2197 /* remove buffering */
2198 s->wbio = BIO_pop(s->wbio);
2199 }
2200
2201 BIO_free(s->bbio);
2202 s->bbio = NULL;
2203}
2204
2205void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
2206 ctx->quiet_shutdown = mode;
2207}
2208
2209int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
2210 return ctx->quiet_shutdown;
2211}
2212
2213void SSL_set_quiet_shutdown(SSL *s, int mode) { s->quiet_shutdown = mode; }
2214
2215int SSL_get_quiet_shutdown(const SSL *s) { return s->quiet_shutdown; }
2216
2217void SSL_set_shutdown(SSL *s, int mode) { s->shutdown = mode; }
2218
2219int SSL_get_shutdown(const SSL *s) { return s->shutdown; }
2220
2221int SSL_version(const SSL *s) { return s->version; }
2222
2223SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
2224
2225SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
2226 if (ssl->ctx == ctx) {
2227 return ssl->ctx;
2228 }
2229
2230 if (ctx == NULL) {
2231 ctx = ssl->initial_ctx;
2232 }
2233
David Benjamin2755a3e2015-04-22 16:17:58 -04002234 ssl_cert_free(ssl->cert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002235 ssl->cert = ssl_cert_dup(ctx->cert);
David Benjamin2755a3e2015-04-22 16:17:58 -04002236
Adam Langley0b5e3902015-05-15 13:08:38 -07002237 CRYPTO_refcount_inc(&ctx->references);
David Benjamin2755a3e2015-04-22 16:17:58 -04002238 SSL_CTX_free(ssl->ctx); /* decrement reference count */
Adam Langleyfcf25832014-12-18 17:42:32 -08002239 ssl->ctx = ctx;
2240
2241 ssl->sid_ctx_length = ctx->sid_ctx_length;
2242 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2243 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2244
2245 return ssl->ctx;
2246}
2247
2248int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
2249 return X509_STORE_set_default_paths(ctx->cert_store);
2250}
Adam Langley95c29f32014-06-20 12:00:00 -07002251
2252int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
Adam Langleyfcf25832014-12-18 17:42:32 -08002253 const char *CApath) {
2254 return X509_STORE_load_locations(ctx->cert_store, CAfile, CApath);
2255}
Adam Langley95c29f32014-06-20 12:00:00 -07002256
2257void SSL_set_info_callback(SSL *ssl,
Adam Langleyfcf25832014-12-18 17:42:32 -08002258 void (*cb)(const SSL *ssl, int type, int val)) {
2259 ssl->info_callback = cb;
2260}
Adam Langley95c29f32014-06-20 12:00:00 -07002261
Adam Langleyfcf25832014-12-18 17:42:32 -08002262void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/, int /*type*/,
2263 int /*val*/) {
2264 return ssl->info_callback;
2265}
Adam Langley95c29f32014-06-20 12:00:00 -07002266
Adam Langleyfcf25832014-12-18 17:42:32 -08002267int SSL_state(const SSL *ssl) { return ssl->state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002268
David Benjaminece089c2015-05-15 23:52:42 -04002269void SSL_set_state(SSL *ssl, int state) { }
Adam Langley95c29f32014-06-20 12:00:00 -07002270
Adam Langleyfcf25832014-12-18 17:42:32 -08002271void SSL_set_verify_result(SSL *ssl, long arg) { ssl->verify_result = arg; }
Adam Langley95c29f32014-06-20 12:00:00 -07002272
Adam Langleyfcf25832014-12-18 17:42:32 -08002273long SSL_get_verify_result(const SSL *ssl) { return ssl->verify_result; }
Adam Langley95c29f32014-06-20 12:00:00 -07002274
Adam Langleyfcf25832014-12-18 17:42:32 -08002275int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2276 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002277 int index;
2278 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp,
2279 new_func, dup_func, free_func)) {
2280 return -1;
2281 }
2282 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002283}
Adam Langley95c29f32014-06-20 12:00:00 -07002284
Adam Langleyfcf25832014-12-18 17:42:32 -08002285int SSL_set_ex_data(SSL *s, int idx, void *arg) {
2286 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2287}
Adam Langley95c29f32014-06-20 12:00:00 -07002288
Adam Langleyfcf25832014-12-18 17:42:32 -08002289void *SSL_get_ex_data(const SSL *s, int idx) {
2290 return CRYPTO_get_ex_data(&s->ex_data, idx);
2291}
Adam Langley95c29f32014-06-20 12:00:00 -07002292
Adam Langleyfcf25832014-12-18 17:42:32 -08002293int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2294 CRYPTO_EX_dup *dup_func,
2295 CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002296 int index;
2297 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp,
2298 new_func, dup_func, free_func)) {
2299 return -1;
2300 }
2301 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002302}
Adam Langley95c29f32014-06-20 12:00:00 -07002303
Adam Langleyfcf25832014-12-18 17:42:32 -08002304int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg) {
2305 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2306}
Adam Langley95c29f32014-06-20 12:00:00 -07002307
Adam Langleyfcf25832014-12-18 17:42:32 -08002308void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx) {
2309 return CRYPTO_get_ex_data(&s->ex_data, idx);
2310}
Adam Langley95c29f32014-06-20 12:00:00 -07002311
Adam Langleyfcf25832014-12-18 17:42:32 -08002312X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
2313 return ctx->cert_store;
2314}
Adam Langley95c29f32014-06-20 12:00:00 -07002315
Adam Langleyfcf25832014-12-18 17:42:32 -08002316void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
David Benjamin2755a3e2015-04-22 16:17:58 -04002317 X509_STORE_free(ctx->cert_store);
Adam Langleyfcf25832014-12-18 17:42:32 -08002318 ctx->cert_store = store;
2319}
Adam Langley95c29f32014-06-20 12:00:00 -07002320
Adam Langleyfcf25832014-12-18 17:42:32 -08002321int SSL_want(const SSL *s) { return s->rwstate; }
Adam Langley95c29f32014-06-20 12:00:00 -07002322
Adam Langleyfcf25832014-12-18 17:42:32 -08002323void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
2324 RSA *(*cb)(SSL *ssl, int is_export,
2325 int keylength)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002326}
Adam Langley95c29f32014-06-20 12:00:00 -07002327
Adam Langleyfcf25832014-12-18 17:42:32 -08002328void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
2329 int keylength)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002330}
2331
2332void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
David Benjamin59015c32015-04-26 13:13:55 -04002333 DH *(*callback)(SSL *ssl, int is_export,
2334 int keylength)) {
2335 ctx->cert->dh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002336}
2337
David Benjamin59015c32015-04-26 13:13:55 -04002338void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*callback)(SSL *ssl, int is_export,
2339 int keylength)) {
2340 ssl->cert->dh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002341}
2342
2343void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,
David Benjamindd978782015-04-24 15:20:13 -04002344 EC_KEY *(*callback)(SSL *ssl, int is_export,
2345 int keylength)) {
David Benjamin59015c32015-04-26 13:13:55 -04002346 ctx->cert->ecdh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002347}
2348
2349void SSL_set_tmp_ecdh_callback(SSL *ssl,
David Benjamindd978782015-04-24 15:20:13 -04002350 EC_KEY *(*callback)(SSL *ssl, int is_export,
2351 int keylength)) {
David Benjamin59015c32015-04-26 13:13:55 -04002352 ssl->cert->ecdh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002353}
2354
2355int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
2356 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
David Benjamin3570d732015-06-29 00:28:17 -04002357 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -08002358 return 0;
2359 }
2360
David Benjamin2755a3e2015-04-22 16:17:58 -04002361 OPENSSL_free(ctx->psk_identity_hint);
Adam Langleyfcf25832014-12-18 17:42:32 -08002362
2363 if (identity_hint != NULL) {
2364 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2365 if (ctx->psk_identity_hint == NULL) {
2366 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002367 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002368 } else {
2369 ctx->psk_identity_hint = NULL;
2370 }
Adam Langley95c29f32014-06-20 12:00:00 -07002371
Adam Langleyfcf25832014-12-18 17:42:32 -08002372 return 1;
2373}
2374
2375int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint) {
2376 if (s == NULL) {
2377 return 0;
2378 }
2379
2380 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
David Benjamin3570d732015-06-29 00:28:17 -04002381 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -08002382 return 0;
2383 }
2384
2385 /* Clear currently configured hint, if any. */
David Benjamin2755a3e2015-04-22 16:17:58 -04002386 OPENSSL_free(s->psk_identity_hint);
2387 s->psk_identity_hint = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002388
2389 if (identity_hint != NULL) {
2390 s->psk_identity_hint = BUF_strdup(identity_hint);
2391 if (s->psk_identity_hint == NULL) {
2392 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002393 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002394 }
Adam Langley95c29f32014-06-20 12:00:00 -07002395
Adam Langleyfcf25832014-12-18 17:42:32 -08002396 return 1;
2397}
Adam Langley95c29f32014-06-20 12:00:00 -07002398
Adam Langleyfcf25832014-12-18 17:42:32 -08002399const char *SSL_get_psk_identity_hint(const SSL *s) {
2400 if (s == NULL) {
2401 return NULL;
2402 }
2403 return s->psk_identity_hint;
2404}
Adam Langley95c29f32014-06-20 12:00:00 -07002405
Adam Langleyfcf25832014-12-18 17:42:32 -08002406const char *SSL_get_psk_identity(const SSL *s) {
2407 if (s == NULL || s->session == NULL) {
2408 return NULL;
2409 }
Adam Langley95c29f32014-06-20 12:00:00 -07002410
Adam Langleyfcf25832014-12-18 17:42:32 -08002411 return s->session->psk_identity;
2412}
Adam Langley95c29f32014-06-20 12:00:00 -07002413
Adam Langleyfcf25832014-12-18 17:42:32 -08002414void SSL_set_psk_client_callback(
2415 SSL *s, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2416 unsigned int max_identity_len, uint8_t *psk,
2417 unsigned int max_psk_len)) {
2418 s->psk_client_callback = cb;
2419}
Adam Langley95c29f32014-06-20 12:00:00 -07002420
Adam Langleyfcf25832014-12-18 17:42:32 -08002421void SSL_CTX_set_psk_client_callback(
2422 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2423 unsigned int max_identity_len,
2424 uint8_t *psk, unsigned int max_psk_len)) {
2425 ctx->psk_client_callback = cb;
2426}
Adam Langley95c29f32014-06-20 12:00:00 -07002427
Adam Langleyfcf25832014-12-18 17:42:32 -08002428void SSL_set_psk_server_callback(
2429 SSL *s, unsigned int (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
2430 unsigned int max_psk_len)) {
2431 s->psk_server_callback = cb;
2432}
Adam Langley95c29f32014-06-20 12:00:00 -07002433
Adam Langleyfcf25832014-12-18 17:42:32 -08002434void SSL_CTX_set_psk_server_callback(
2435 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *identity,
2436 uint8_t *psk, unsigned int max_psk_len)) {
2437 ctx->psk_server_callback = cb;
2438}
Adam Langley95c29f32014-06-20 12:00:00 -07002439
Adam Langleyfcf25832014-12-18 17:42:32 -08002440void SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) {
2441 ctx->min_version = version;
2442}
Adam Langley95c29f32014-06-20 12:00:00 -07002443
Adam Langleyfcf25832014-12-18 17:42:32 -08002444void SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) {
2445 ctx->max_version = version;
2446}
Adam Langley0289c732014-06-20 12:00:00 -07002447
Adam Langleyfcf25832014-12-18 17:42:32 -08002448void SSL_set_min_version(SSL *ssl, uint16_t version) {
2449 ssl->min_version = version;
2450}
Adam Langley0289c732014-06-20 12:00:00 -07002451
Adam Langleyfcf25832014-12-18 17:42:32 -08002452void SSL_set_max_version(SSL *ssl, uint16_t version) {
2453 ssl->max_version = version;
2454}
Adam Langley95c29f32014-06-20 12:00:00 -07002455
Adam Langleyfcf25832014-12-18 17:42:32 -08002456void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
2457 void (*cb)(int write_p, int version,
2458 int content_type, const void *buf,
2459 size_t len, SSL *ssl, void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002460 ctx->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002461}
David Benjamin61ecccf2015-05-05 09:44:51 -04002462
2463void SSL_CTX_set_msg_callback_arg(SSL_CTX *ctx, void *arg) {
2464 ctx->msg_callback_arg = arg;
2465}
2466
Adam Langleyfcf25832014-12-18 17:42:32 -08002467void SSL_set_msg_callback(SSL *ssl,
2468 void (*cb)(int write_p, int version, int content_type,
2469 const void *buf, size_t len, SSL *ssl,
2470 void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002471 ssl->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002472}
Adam Langley95c29f32014-06-20 12:00:00 -07002473
David Benjamin61ecccf2015-05-05 09:44:51 -04002474void SSL_set_msg_callback_arg(SSL *ssl, void *arg) {
2475 ssl->msg_callback_arg = arg;
2476}
2477
Adam Langleyfcf25832014-12-18 17:42:32 -08002478void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio) {
David Benjamin2755a3e2015-04-22 16:17:58 -04002479 BIO_free(ctx->keylog_bio);
Adam Langleyfcf25832014-12-18 17:42:32 -08002480 ctx->keylog_bio = keylog_bio;
2481}
Adam Langley95c29f32014-06-20 12:00:00 -07002482
Adam Langleyfcf25832014-12-18 17:42:32 -08002483static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
2484 static const char hextable[] = "0123456789abcdef";
2485 uint8_t *out;
2486 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002487
Adam Langleyfcf25832014-12-18 17:42:32 -08002488 if (!CBB_add_space(cbb, &out, in_len * 2)) {
2489 return 0;
2490 }
Adam Langley95c29f32014-06-20 12:00:00 -07002491
Adam Langleyfcf25832014-12-18 17:42:32 -08002492 for (i = 0; i < in_len; i++) {
2493 *(out++) = (uint8_t)hextable[in[i] >> 4];
2494 *(out++) = (uint8_t)hextable[in[i] & 0xf];
2495 }
Adam Langley95c29f32014-06-20 12:00:00 -07002496
Adam Langleyfcf25832014-12-18 17:42:32 -08002497 return 1;
2498}
David Benjamin859ec3c2014-09-02 16:29:36 -04002499
2500int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -08002501 const uint8_t *encrypted_premaster,
2502 size_t encrypted_premaster_len,
2503 const uint8_t *premaster,
2504 size_t premaster_len) {
2505 BIO *bio = ctx->keylog_bio;
2506 CBB cbb;
2507 uint8_t *out;
2508 size_t out_len;
2509 int ret;
David Benjamin859ec3c2014-09-02 16:29:36 -04002510
Adam Langleyfcf25832014-12-18 17:42:32 -08002511 if (bio == NULL) {
2512 return 1;
2513 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002514
Adam Langleyfcf25832014-12-18 17:42:32 -08002515 if (encrypted_premaster_len < 8) {
David Benjamin3570d732015-06-29 00:28:17 -04002516 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08002517 return 0;
2518 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002519
David Benjamina8653202015-06-28 01:26:10 -04002520 CBB_zero(&cbb);
2521 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len * 2 + 1) ||
2522 !CBB_add_bytes(&cbb, (const uint8_t *)"RSA ", 4) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08002523 /* Only the first 8 bytes of the encrypted premaster secret are
2524 * logged. */
2525 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
2526 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2527 !cbb_add_hex(&cbb, premaster, premaster_len) ||
2528 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2529 !CBB_finish(&cbb, &out, &out_len)) {
2530 CBB_cleanup(&cbb);
2531 return 0;
2532 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002533
Adam Langley4bdb6e42015-05-15 15:29:21 -07002534 CRYPTO_MUTEX_lock_write(&ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -08002535 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
Adam Langley4bdb6e42015-05-15 15:29:21 -07002536 CRYPTO_MUTEX_unlock(&ctx->lock);
David Benjamin859ec3c2014-09-02 16:29:36 -04002537
Adam Langleyfcf25832014-12-18 17:42:32 -08002538 OPENSSL_free(out);
2539 return ret;
Adam Langley95f22882014-06-20 12:00:00 -07002540}
2541
Adam Langleyfcf25832014-12-18 17:42:32 -08002542int ssl_ctx_log_master_secret(SSL_CTX *ctx, const uint8_t *client_random,
2543 size_t client_random_len, const uint8_t *master,
2544 size_t master_len) {
2545 BIO *bio = ctx->keylog_bio;
2546 CBB cbb;
2547 uint8_t *out;
2548 size_t out_len;
2549 int ret;
Adam Langleyadb739e2014-06-20 12:00:00 -07002550
Adam Langleyfcf25832014-12-18 17:42:32 -08002551 if (bio == NULL) {
2552 return 1;
2553 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002554
Adam Langleyfcf25832014-12-18 17:42:32 -08002555 if (client_random_len != 32) {
David Benjamin3570d732015-06-29 00:28:17 -04002556 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08002557 return 0;
2558 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002559
David Benjamina8653202015-06-28 01:26:10 -04002560 CBB_zero(&cbb);
2561 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len * 2 + 1) ||
2562 !CBB_add_bytes(&cbb, (const uint8_t *)"CLIENT_RANDOM ", 14) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08002563 !cbb_add_hex(&cbb, client_random, 32) ||
2564 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2565 !cbb_add_hex(&cbb, master, master_len) ||
2566 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2567 !CBB_finish(&cbb, &out, &out_len)) {
2568 CBB_cleanup(&cbb);
2569 return 0;
2570 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002571
Adam Langley4bdb6e42015-05-15 15:29:21 -07002572 CRYPTO_MUTEX_lock_write(&ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -08002573 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
Adam Langley4bdb6e42015-05-15 15:29:21 -07002574 CRYPTO_MUTEX_unlock(&ctx->lock);
David Benjamine99e9122014-12-11 01:46:01 -05002575
Adam Langleyfcf25832014-12-18 17:42:32 -08002576 OPENSSL_free(out);
2577 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -07002578}
2579
David Benjamined7c4752015-02-16 19:16:46 -05002580int SSL_in_false_start(const SSL *s) {
2581 return s->s3->tmp.in_false_start;
2582}
2583
Adam Langleyfcf25832014-12-18 17:42:32 -08002584int SSL_cutthrough_complete(const SSL *s) {
David Benjamined7c4752015-02-16 19:16:46 -05002585 return SSL_in_false_start(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002586}
Adam Langley95c29f32014-06-20 12:00:00 -07002587
Adam Langleyfcf25832014-12-18 17:42:32 -08002588void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
2589 size_t *ssl_session_size) {
2590 *ssl_size = sizeof(SSL);
2591 *ssl_ctx_size = sizeof(SSL_CTX);
2592 *ssl_session_size = sizeof(SSL_SESSION);
2593}
Feng Lu41aa3252014-11-21 22:47:56 -08002594
David Benjamined7c4752015-02-16 19:16:46 -05002595int ssl3_can_false_start(const SSL *s) {
David Benjamin195dc782015-02-19 13:27:05 -05002596 const SSL_CIPHER *const cipher = SSL_get_current_cipher(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002597
Adam Langleye631d962015-04-01 13:11:01 -07002598 /* False Start only for TLS 1.2 with an ECDHE+AEAD cipher and ALPN or NPN. */
David Benjamin195dc782015-02-19 13:27:05 -05002599 return !SSL_IS_DTLS(s) &&
2600 SSL_version(s) >= TLS1_2_VERSION &&
2601 (s->s3->alpn_selected || s->s3->next_proto_neg_seen) &&
2602 cipher != NULL &&
Adam Langleye631d962015-04-01 13:11:01 -07002603 cipher->algorithm_mkey == SSL_kECDHE &&
David Benjamin195dc782015-02-19 13:27:05 -05002604 (cipher->algorithm_enc == SSL_AES128GCM ||
2605 cipher->algorithm_enc == SSL_AES256GCM ||
2606 cipher->algorithm_enc == SSL_CHACHA20POLY1305);
Adam Langleyfcf25832014-12-18 17:42:32 -08002607}
2608
2609const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version) {
2610 switch (version) {
2611 case SSL3_VERSION:
2612 return &SSLv3_enc_data;
2613
2614 case TLS1_VERSION:
2615 return &TLSv1_enc_data;
2616
David Benjamin9e13e1a2015-03-05 01:56:32 -05002617 case DTLS1_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002618 case TLS1_1_VERSION:
2619 return &TLSv1_1_enc_data;
2620
David Benjamin9e13e1a2015-03-05 01:56:32 -05002621 case DTLS1_2_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002622 case TLS1_2_VERSION:
2623 return &TLSv1_2_enc_data;
2624
Adam Langleyfcf25832014-12-18 17:42:32 -08002625 default:
2626 return NULL;
2627 }
2628}
2629
2630uint16_t ssl3_get_max_server_version(const SSL *s) {
2631 uint16_t max_version;
2632
2633 if (SSL_IS_DTLS(s)) {
2634 max_version = (s->max_version != 0) ? s->max_version : DTLS1_2_VERSION;
2635 if (!(s->options & SSL_OP_NO_DTLSv1_2) && DTLS1_2_VERSION >= max_version) {
2636 return DTLS1_2_VERSION;
2637 }
2638 if (!(s->options & SSL_OP_NO_DTLSv1) && DTLS1_VERSION >= max_version) {
2639 return DTLS1_VERSION;
2640 }
2641 return 0;
2642 }
2643
2644 max_version = (s->max_version != 0) ? s->max_version : TLS1_2_VERSION;
2645 if (!(s->options & SSL_OP_NO_TLSv1_2) && TLS1_2_VERSION <= max_version) {
2646 return TLS1_2_VERSION;
2647 }
2648 if (!(s->options & SSL_OP_NO_TLSv1_1) && TLS1_1_VERSION <= max_version) {
2649 return TLS1_1_VERSION;
2650 }
2651 if (!(s->options & SSL_OP_NO_TLSv1) && TLS1_VERSION <= max_version) {
2652 return TLS1_VERSION;
2653 }
2654 if (!(s->options & SSL_OP_NO_SSLv3) && SSL3_VERSION <= max_version) {
2655 return SSL3_VERSION;
2656 }
2657 return 0;
2658}
2659
2660uint16_t ssl3_get_mutual_version(SSL *s, uint16_t client_version) {
2661 uint16_t version = 0;
2662
2663 if (SSL_IS_DTLS(s)) {
2664 /* Clamp client_version to max_version. */
2665 if (s->max_version != 0 && client_version < s->max_version) {
2666 client_version = s->max_version;
2667 }
2668
2669 if (client_version <= DTLS1_2_VERSION && !(s->options & SSL_OP_NO_DTLSv1_2)) {
2670 version = DTLS1_2_VERSION;
2671 } else if (client_version <= DTLS1_VERSION &&
David Benjaminb18f0242015-03-10 18:30:08 -04002672 !(s->options & SSL_OP_NO_DTLSv1)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002673 version = DTLS1_VERSION;
2674 }
2675
2676 /* Check against min_version. */
2677 if (version != 0 && s->min_version != 0 && version > s->min_version) {
2678 return 0;
2679 }
2680 return version;
2681 } else {
2682 /* Clamp client_version to max_version. */
2683 if (s->max_version != 0 && client_version > s->max_version) {
2684 client_version = s->max_version;
2685 }
2686
2687 if (client_version >= TLS1_2_VERSION && !(s->options & SSL_OP_NO_TLSv1_2)) {
2688 version = TLS1_2_VERSION;
2689 } else if (client_version >= TLS1_1_VERSION &&
2690 !(s->options & SSL_OP_NO_TLSv1_1)) {
2691 version = TLS1_1_VERSION;
2692 } else if (client_version >= TLS1_VERSION && !(s->options & SSL_OP_NO_TLSv1)) {
2693 version = TLS1_VERSION;
2694 } else if (client_version >= SSL3_VERSION && !(s->options & SSL_OP_NO_SSLv3)) {
2695 version = SSL3_VERSION;
2696 }
2697
2698 /* Check against min_version. */
2699 if (version != 0 && s->min_version != 0 && version < s->min_version) {
2700 return 0;
2701 }
2702 return version;
2703 }
2704}
2705
2706uint16_t ssl3_get_max_client_version(SSL *s) {
David Benjamin123a8fd2015-04-26 14:16:41 -04002707 uint32_t options = s->options;
Adam Langleyfcf25832014-12-18 17:42:32 -08002708 uint16_t version = 0;
2709
2710 /* OpenSSL's API for controlling versions entails blacklisting individual
2711 * protocols. This has two problems. First, on the client, the protocol can
2712 * only express a contiguous range of versions. Second, a library consumer
2713 * trying to set a maximum version cannot disable protocol versions that get
2714 * added in a future version of the library.
2715 *
2716 * To account for both of these, OpenSSL interprets the client-side bitmask
2717 * as a min/max range by picking the lowest contiguous non-empty range of
2718 * enabled protocols. Note that this means it is impossible to set a maximum
2719 * version of TLS 1.2 in a future-proof way.
2720 *
2721 * By this scheme, the maximum version is the lowest version V such that V is
2722 * enabled and V+1 is disabled or unimplemented. */
2723 if (SSL_IS_DTLS(s)) {
2724 if (!(options & SSL_OP_NO_DTLSv1_2)) {
2725 version = DTLS1_2_VERSION;
2726 }
2727 if (!(options & SSL_OP_NO_DTLSv1) && (options & SSL_OP_NO_DTLSv1_2)) {
2728 version = DTLS1_VERSION;
2729 }
2730 if (s->max_version != 0 && version < s->max_version) {
2731 version = s->max_version;
2732 }
2733 } else {
2734 if (!(options & SSL_OP_NO_TLSv1_2)) {
2735 version = TLS1_2_VERSION;
2736 }
2737 if (!(options & SSL_OP_NO_TLSv1_1) && (options & SSL_OP_NO_TLSv1_2)) {
2738 version = TLS1_1_VERSION;
2739 }
2740 if (!(options & SSL_OP_NO_TLSv1) && (options & SSL_OP_NO_TLSv1_1)) {
2741 version = TLS1_VERSION;
2742 }
2743 if (!(options & SSL_OP_NO_SSLv3) && (options & SSL_OP_NO_TLSv1)) {
2744 version = SSL3_VERSION;
2745 }
2746 if (s->max_version != 0 && version > s->max_version) {
2747 version = s->max_version;
2748 }
2749 }
2750
2751 return version;
2752}
2753
2754int ssl3_is_version_enabled(SSL *s, uint16_t version) {
2755 if (SSL_IS_DTLS(s)) {
2756 if (s->max_version != 0 && version < s->max_version) {
2757 return 0;
2758 }
2759 if (s->min_version != 0 && version > s->min_version) {
2760 return 0;
2761 }
2762
2763 switch (version) {
2764 case DTLS1_VERSION:
2765 return !(s->options & SSL_OP_NO_DTLSv1);
2766
2767 case DTLS1_2_VERSION:
2768 return !(s->options & SSL_OP_NO_DTLSv1_2);
2769
2770 default:
2771 return 0;
2772 }
2773 } else {
2774 if (s->max_version != 0 && version > s->max_version) {
2775 return 0;
2776 }
2777 if (s->min_version != 0 && version < s->min_version) {
2778 return 0;
2779 }
2780
2781 switch (version) {
2782 case SSL3_VERSION:
2783 return !(s->options & SSL_OP_NO_SSLv3);
2784
2785 case TLS1_VERSION:
2786 return !(s->options & SSL_OP_NO_TLSv1);
2787
2788 case TLS1_1_VERSION:
2789 return !(s->options & SSL_OP_NO_TLSv1_1);
2790
2791 case TLS1_2_VERSION:
2792 return !(s->options & SSL_OP_NO_TLSv1_2);
2793
2794 default:
2795 return 0;
2796 }
2797 }
2798}
2799
David Benjaminea72bd02014-12-21 21:27:41 -05002800uint16_t ssl3_version_from_wire(SSL *s, uint16_t wire_version) {
2801 if (!SSL_IS_DTLS(s)) {
2802 return wire_version;
2803 }
2804
2805 uint16_t tls_version = ~wire_version;
2806 uint16_t version = tls_version + 0x0201;
2807 /* If either component overflowed, clamp it so comparisons still work. */
2808 if ((version >> 8) < (tls_version >> 8)) {
2809 version = 0xff00 | (version & 0xff);
2810 }
2811 if ((version & 0xff) < (tls_version & 0xff)) {
2812 version = (version & 0xff00) | 0xff;
2813 }
2814 /* DTLS 1.0 maps to TLS 1.1, not TLS 1.0. */
2815 if (version == TLS1_VERSION) {
2816 version = TLS1_1_VERSION;
2817 }
2818 return version;
2819}
2820
Adam Langleyfcf25832014-12-18 17:42:32 -08002821int SSL_cache_hit(SSL *s) { return s->hit; }
2822
2823int SSL_is_server(SSL *s) { return s->server; }
2824
Adam Langley524e7172015-02-20 16:04:00 -08002825void SSL_CTX_set_dos_protection_cb(
2826 SSL_CTX *ctx, int (*cb)(const struct ssl_early_callback_ctx *)) {
2827 ctx->dos_protection_cb = cb;
2828}
2829
David Benjaminb16346b2015-04-08 19:16:58 -04002830void SSL_set_reject_peer_renegotiations(SSL *s, int reject) {
David Benjamin897e5e02015-05-12 17:03:54 -04002831 s->accept_peer_renegotiations = !reject;
David Benjaminb16346b2015-04-08 19:16:58 -04002832}
2833
Adam Langley3f92d212015-02-20 15:32:52 -08002834int SSL_get_rc4_state(const SSL *ssl, const RC4_KEY **read_key,
2835 const RC4_KEY **write_key) {
2836 if (ssl->aead_read_ctx == NULL || ssl->aead_write_ctx == NULL) {
2837 return 0;
2838 }
2839
2840 return EVP_AEAD_CTX_get_rc4_state(&ssl->aead_read_ctx->ctx, read_key) &&
2841 EVP_AEAD_CTX_get_rc4_state(&ssl->aead_write_ctx->ctx, write_key);
2842}
David Benjaminda881e92015-04-26 14:45:04 -04002843
Adam Langleyaf0e32c2015-06-03 09:57:23 -07002844int SSL_get_tls_unique(const SSL *ssl, uint8_t *out, size_t *out_len,
2845 size_t max_out) {
2846 /* The tls-unique value is the first Finished message in the handshake, which
2847 * is the client's in a full handshake and the server's for a resumption. See
2848 * https://tools.ietf.org/html/rfc5929#section-3.1. */
2849 const uint8_t *finished = ssl->s3->previous_client_finished;
2850 size_t finished_len = ssl->s3->previous_client_finished_len;
2851 if (ssl->hit) {
2852 /* tls-unique is broken for resumed sessions unless EMS is used. */
2853 if (!ssl->session->extended_master_secret) {
2854 goto err;
2855 }
2856 finished = ssl->s3->previous_server_finished;
2857 finished_len = ssl->s3->previous_server_finished_len;
2858 }
2859
2860 if (!ssl->s3->initial_handshake_complete ||
2861 ssl->version < TLS1_VERSION) {
2862 goto err;
2863 }
2864
2865 *out_len = finished_len;
2866 if (finished_len > max_out) {
2867 *out_len = max_out;
2868 }
2869
2870 memcpy(out, finished, *out_len);
2871 return 1;
2872
2873err:
2874 *out_len = 0;
2875 memset(out, 0, max_out);
2876 return 0;
2877}
2878
David Benjaminda881e92015-04-26 14:45:04 -04002879int SSL_CTX_sess_connect(const SSL_CTX *ctx) { return 0; }
2880int SSL_CTX_sess_connect_good(const SSL_CTX *ctx) { return 0; }
2881int SSL_CTX_sess_connect_renegotiate(const SSL_CTX *ctx) { return 0; }
2882int SSL_CTX_sess_accept(const SSL_CTX *ctx) { return 0; }
2883int SSL_CTX_sess_accept_renegotiate(const SSL_CTX *ctx) { return 0; }
2884int SSL_CTX_sess_accept_good(const SSL_CTX *ctx) { return 0; }
2885int SSL_CTX_sess_hits(const SSL_CTX *ctx) { return 0; }
2886int SSL_CTX_sess_cb_hits(const SSL_CTX *ctx) { return 0; }
2887int SSL_CTX_sess_misses(const SSL_CTX *ctx) { return 0; }
2888int SSL_CTX_sess_timeouts(const SSL_CTX *ctx) { return 0; }
2889int SSL_CTX_sess_cache_full(const SSL_CTX *ctx) { return 0; }