blob: 574ae7b9dede1a982f91a5704482d2295457500b [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>
David Benjamin7c6d09b2015-09-05 11:00:19 -0400146#include <openssl/crypto.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700147#include <openssl/dh.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400148#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700149#include <openssl/lhash.h>
150#include <openssl/mem.h>
151#include <openssl/obj.h>
152#include <openssl/rand.h>
153#include <openssl/x509v3.h>
154
David Benjamin2ee94aa2015-04-07 22:38:30 -0400155#include "internal.h"
David Benjamin546f1a52015-04-15 16:46:09 -0400156#include "../crypto/internal.h"
157
Adam Langley95c29f32014-06-20 12:00:00 -0700158
David Benjamin76c2efc2015-08-31 14:24:29 -0400159/* |SSL_R_UNKNOWN_PROTOCOL| is no longer emitted, but continue to define it
David Benjamin97760d52015-07-24 23:02:49 -0400160 * to avoid downstream churn. */
David Benjamin76c2efc2015-08-31 14:24:29 -0400161OPENSSL_DECLARE_ERROR_REASON(SSL, UNKNOWN_PROTOCOL)
David Benjamin97760d52015-07-24 23:02:49 -0400162
Adam Langleyfcf25832014-12-18 17:42:32 -0800163/* Some error codes are special. Ensure the make_errors.go script never
164 * regresses this. */
165OPENSSL_COMPILE_ASSERT(SSL_R_TLSV1_ALERT_NO_RENEGOTIATION ==
166 SSL_AD_NO_RENEGOTIATION + SSL_AD_REASON_OFFSET,
167 ssl_alert_reason_code_mismatch);
David Benjamine1136082014-09-20 12:28:58 -0400168
David Benjamin1d0a1942015-04-26 15:35:35 -0400169/* kMaxHandshakeSize is the maximum size, in bytes, of a handshake message. */
170static const size_t kMaxHandshakeSize = (1u << 24) - 1;
171
David Benjaminaa585132015-06-29 23:36:17 -0400172static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl =
173 CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
174static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl_ctx =
175 CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
David Benjamin9f33fc62015-04-15 17:29:53 -0400176
David Benjamin7c6d09b2015-09-05 11:00:19 -0400177int SSL_library_init(void) {
178 CRYPTO_library_init();
179 return 1;
180}
181
182void SSL_load_error_strings(void) {}
183
David Benjamin4831c332015-05-16 11:43:13 -0400184int SSL_clear(SSL *ssl) {
185 if (ssl->method == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400186 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_METHOD_SPECIFIED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800187 return 0;
188 }
Adam Langley95c29f32014-06-20 12:00:00 -0700189
David Benjamin4831c332015-05-16 11:43:13 -0400190 if (ssl_clear_bad_session(ssl)) {
191 SSL_SESSION_free(ssl->session);
192 ssl->session = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800193 }
Adam Langley95c29f32014-06-20 12:00:00 -0700194
David Benjamin4831c332015-05-16 11:43:13 -0400195 ssl->hit = 0;
196 ssl->shutdown = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700197
David Benjamin4831c332015-05-16 11:43:13 -0400198 /* SSL_clear may be called before or after the |ssl| is initialized in either
Adam Langleyfcf25832014-12-18 17:42:32 -0800199 * accept or connect state. In the latter case, SSL_clear should preserve the
David Benjamin4831c332015-05-16 11:43:13 -0400200 * half and reset |ssl->state| accordingly. */
201 if (ssl->handshake_func != NULL) {
202 if (ssl->server) {
203 SSL_set_accept_state(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800204 } else {
David Benjamin4831c332015-05-16 11:43:13 -0400205 SSL_set_connect_state(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800206 }
207 } else {
David Benjamin4831c332015-05-16 11:43:13 -0400208 assert(ssl->state == 0);
Adam Langleyfcf25832014-12-18 17:42:32 -0800209 }
Adam Langley95c29f32014-06-20 12:00:00 -0700210
David Benjamin5f387e32015-05-15 22:05:42 -0400211 /* TODO(davidben): Some state on |ssl| is reset both in |SSL_new| and
David Benjamin62fd1622015-01-11 13:30:01 -0500212 * |SSL_clear| because it is per-connection state rather than configuration
David Benjamin4831c332015-05-16 11:43:13 -0400213 * state. Per-connection state should be on |ssl->s3| and |ssl->d1| so it is
David Benjamin62fd1622015-01-11 13:30:01 -0500214 * naturally reset at the right points between |SSL_new|, |SSL_clear|, and
215 * |ssl3_new|. */
216
David Benjamin4831c332015-05-16 11:43:13 -0400217 ssl->rwstate = SSL_NOTHING;
Adam Langley95c29f32014-06-20 12:00:00 -0700218
David Benjamin4831c332015-05-16 11:43:13 -0400219 BUF_MEM_free(ssl->init_buf);
220 ssl->init_buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700221
David Benjamin4831c332015-05-16 11:43:13 -0400222 ssl_clear_cipher_ctx(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700223
David Benjamin4831c332015-05-16 11:43:13 -0400224 OPENSSL_free(ssl->next_proto_negotiated);
225 ssl->next_proto_negotiated = NULL;
226 ssl->next_proto_negotiated_len = 0;
David Benjamin62fd1622015-01-11 13:30:01 -0500227
David Benjamin4831c332015-05-16 11:43:13 -0400228 /* The ssl->d1->mtu is simultaneously configuration (preserved across
David Benjamin62fd1622015-01-11 13:30:01 -0500229 * clear) and connection-specific state (gets reset).
230 *
231 * TODO(davidben): Avoid this. */
232 unsigned mtu = 0;
David Benjamin4831c332015-05-16 11:43:13 -0400233 if (ssl->d1 != NULL) {
234 mtu = ssl->d1->mtu;
David Benjamin62fd1622015-01-11 13:30:01 -0500235 }
236
David Benjamin4831c332015-05-16 11:43:13 -0400237 ssl->method->ssl_free(ssl);
238 if (!ssl->method->ssl_new(ssl)) {
David Benjamin62fd1622015-01-11 13:30:01 -0500239 return 0;
240 }
David Benjamin4831c332015-05-16 11:43:13 -0400241 ssl->enc_method = ssl3_get_enc_method(ssl->version);
242 assert(ssl->enc_method != NULL);
David Benjamin62fd1622015-01-11 13:30:01 -0500243
David Benjamin4831c332015-05-16 11:43:13 -0400244 if (SSL_IS_DTLS(ssl) && (SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
245 ssl->d1->mtu = mtu;
David Benjamin62fd1622015-01-11 13:30:01 -0500246 }
247
David Benjamin4831c332015-05-16 11:43:13 -0400248 ssl->client_version = ssl->version;
Adam Langley95c29f32014-06-20 12:00:00 -0700249
Adam Langleyfcf25832014-12-18 17:42:32 -0800250 return 1;
251}
Adam Langley95c29f32014-06-20 12:00:00 -0700252
Adam Langleyfcf25832014-12-18 17:42:32 -0800253SSL *SSL_new(SSL_CTX *ctx) {
254 SSL *s;
Adam Langley95c29f32014-06-20 12:00:00 -0700255
Adam Langleyfcf25832014-12-18 17:42:32 -0800256 if (ctx == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400257 OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_CTX);
Adam Langleyfcf25832014-12-18 17:42:32 -0800258 return NULL;
259 }
260 if (ctx->method == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400261 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
Adam Langleyfcf25832014-12-18 17:42:32 -0800262 return NULL;
263 }
Adam Langley95c29f32014-06-20 12:00:00 -0700264
Adam Langleyfcf25832014-12-18 17:42:32 -0800265 s = (SSL *)OPENSSL_malloc(sizeof(SSL));
266 if (s == NULL) {
267 goto err;
268 }
269 memset(s, 0, sizeof(SSL));
Adam Langley95c29f32014-06-20 12:00:00 -0700270
Adam Langleyfcf25832014-12-18 17:42:32 -0800271 s->min_version = ctx->min_version;
272 s->max_version = ctx->max_version;
David Benjamin1eb367c2014-12-12 18:17:51 -0500273
Adam Langleyfcf25832014-12-18 17:42:32 -0800274 s->options = ctx->options;
275 s->mode = ctx->mode;
276 s->max_cert_list = ctx->max_cert_list;
Adam Langley95c29f32014-06-20 12:00:00 -0700277
David Benjamina5a3eeb2015-03-18 20:26:30 -0400278 s->cert = ssl_cert_dup(ctx->cert);
279 if (s->cert == NULL) {
280 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -0800281 }
Adam Langley95c29f32014-06-20 12:00:00 -0700282
Adam Langleyfcf25832014-12-18 17:42:32 -0800283 s->msg_callback = ctx->msg_callback;
284 s->msg_callback_arg = ctx->msg_callback_arg;
285 s->verify_mode = ctx->verify_mode;
286 s->sid_ctx_length = ctx->sid_ctx_length;
287 assert(s->sid_ctx_length <= sizeof s->sid_ctx);
288 memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));
289 s->verify_callback = ctx->default_verify_callback;
290 s->generate_session_id = ctx->generate_session_id;
Adam Langley95c29f32014-06-20 12:00:00 -0700291
Adam Langleyfcf25832014-12-18 17:42:32 -0800292 s->param = X509_VERIFY_PARAM_new();
293 if (!s->param) {
294 goto err;
295 }
296 X509_VERIFY_PARAM_inherit(s->param, ctx->param);
297 s->quiet_shutdown = ctx->quiet_shutdown;
298 s->max_send_fragment = ctx->max_send_fragment;
Adam Langley95c29f32014-06-20 12:00:00 -0700299
Adam Langley0b5e3902015-05-15 13:08:38 -0700300 CRYPTO_refcount_inc(&ctx->references);
Adam Langleyfcf25832014-12-18 17:42:32 -0800301 s->ctx = ctx;
Adam Langley0b5e3902015-05-15 13:08:38 -0700302 CRYPTO_refcount_inc(&ctx->references);
Adam Langleyfcf25832014-12-18 17:42:32 -0800303 s->initial_ctx = ctx;
Adam Langley95c29f32014-06-20 12:00:00 -0700304
Adam Langleyfcf25832014-12-18 17:42:32 -0800305 if (ctx->tlsext_ellipticcurvelist) {
306 s->tlsext_ellipticcurvelist =
307 BUF_memdup(ctx->tlsext_ellipticcurvelist,
308 ctx->tlsext_ellipticcurvelist_length * 2);
309 if (!s->tlsext_ellipticcurvelist) {
310 goto err;
311 }
312 s->tlsext_ellipticcurvelist_length = ctx->tlsext_ellipticcurvelist_length;
313 }
Adam Langley95c29f32014-06-20 12:00:00 -0700314
Adam Langleyfcf25832014-12-18 17:42:32 -0800315 if (s->ctx->alpn_client_proto_list) {
316 s->alpn_client_proto_list = BUF_memdup(s->ctx->alpn_client_proto_list,
317 s->ctx->alpn_client_proto_list_len);
318 if (s->alpn_client_proto_list == NULL) {
319 goto err;
320 }
321 s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;
322 }
Adam Langley95c29f32014-06-20 12:00:00 -0700323
Adam Langleyfcf25832014-12-18 17:42:32 -0800324 s->verify_result = X509_V_OK;
325 s->method = ctx->method;
Adam Langley95c29f32014-06-20 12:00:00 -0700326
Adam Langleyfcf25832014-12-18 17:42:32 -0800327 if (!s->method->ssl_new(s)) {
328 goto err;
329 }
330 s->enc_method = ssl3_get_enc_method(s->version);
331 assert(s->enc_method != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700332
David Benjamin62fd1622015-01-11 13:30:01 -0500333 s->rwstate = SSL_NOTHING;
Adam Langley95c29f32014-06-20 12:00:00 -0700334
David Benjamin9f33fc62015-04-15 17:29:53 -0400335 CRYPTO_new_ex_data(&g_ex_data_class_ssl, s, &s->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700336
Adam Langleyfcf25832014-12-18 17:42:32 -0800337 s->psk_identity_hint = NULL;
338 if (ctx->psk_identity_hint) {
339 s->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
340 if (s->psk_identity_hint == NULL) {
341 goto err;
342 }
343 }
344 s->psk_client_callback = ctx->psk_client_callback;
345 s->psk_server_callback = ctx->psk_server_callback;
Adam Langley95c29f32014-06-20 12:00:00 -0700346
David Benjamin02ddbfd2015-01-11 13:09:11 -0500347 s->tlsext_channel_id_enabled = ctx->tlsext_channel_id_enabled;
348 if (ctx->tlsext_channel_id_private) {
David Benjamin9a10f8f2015-05-05 22:22:40 -0400349 s->tlsext_channel_id_private =
350 EVP_PKEY_up_ref(ctx->tlsext_channel_id_private);
David Benjamin02ddbfd2015-01-11 13:09:11 -0500351 }
352
Adam Langleyfcf25832014-12-18 17:42:32 -0800353 s->signed_cert_timestamps_enabled = s->ctx->signed_cert_timestamps_enabled;
354 s->ocsp_stapling_enabled = s->ctx->ocsp_stapling_enabled;
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200355
Adam Langleyfcf25832014-12-18 17:42:32 -0800356 return s;
357
Adam Langley95c29f32014-06-20 12:00:00 -0700358err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400359 SSL_free(s);
David Benjamin3570d732015-06-29 00:28:17 -0400360 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700361
Adam Langleyfcf25832014-12-18 17:42:32 -0800362 return NULL;
363}
Adam Langley95c29f32014-06-20 12:00:00 -0700364
Adam Langleyfcf25832014-12-18 17:42:32 -0800365int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
366 unsigned int sid_ctx_len) {
367 if (sid_ctx_len > sizeof ctx->sid_ctx) {
David Benjamin3570d732015-06-29 00:28:17 -0400368 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -0800369 return 0;
370 }
371 ctx->sid_ctx_length = sid_ctx_len;
372 memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700373
Adam Langleyfcf25832014-12-18 17:42:32 -0800374 return 1;
375}
Adam Langley95c29f32014-06-20 12:00:00 -0700376
Adam Langleyfcf25832014-12-18 17:42:32 -0800377int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
378 unsigned int sid_ctx_len) {
379 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
David Benjamin3570d732015-06-29 00:28:17 -0400380 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -0800381 return 0;
382 }
383 ssl->sid_ctx_length = sid_ctx_len;
384 memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700385
Adam Langleyfcf25832014-12-18 17:42:32 -0800386 return 1;
387}
Adam Langley95c29f32014-06-20 12:00:00 -0700388
Adam Langleyfcf25832014-12-18 17:42:32 -0800389int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800390 ctx->generate_session_id = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -0800391 return 1;
392}
Adam Langley95c29f32014-06-20 12:00:00 -0700393
Adam Langleyfcf25832014-12-18 17:42:32 -0800394int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800395 ssl->generate_session_id = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -0800396 return 1;
397}
Adam Langley95c29f32014-06-20 12:00:00 -0700398
Adam Langleyfcf25832014-12-18 17:42:32 -0800399int SSL_has_matching_session_id(const SSL *ssl, const uint8_t *id,
400 unsigned int id_len) {
401 /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how we
402 * can "construct" a session to give us the desired check - ie. to find if
403 * there's a session in the hash table that would conflict with any new
404 * session built out of this id/id_len and the ssl_version in use by this
405 * SSL. */
406 SSL_SESSION r, *p;
Adam Langley95c29f32014-06-20 12:00:00 -0700407
Adam Langleyfcf25832014-12-18 17:42:32 -0800408 if (id_len > sizeof r.session_id) {
409 return 0;
410 }
Adam Langley95c29f32014-06-20 12:00:00 -0700411
Adam Langleyfcf25832014-12-18 17:42:32 -0800412 r.ssl_version = ssl->version;
413 r.session_id_length = id_len;
414 memcpy(r.session_id, id, id_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700415
Adam Langley4bdb6e42015-05-15 15:29:21 -0700416 CRYPTO_MUTEX_lock_read(&ssl->ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -0800417 p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r);
Adam Langley4bdb6e42015-05-15 15:29:21 -0700418 CRYPTO_MUTEX_unlock(&ssl->ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -0800419 return p != NULL;
420}
Adam Langley95c29f32014-06-20 12:00:00 -0700421
Adam Langleyfcf25832014-12-18 17:42:32 -0800422int SSL_CTX_set_purpose(SSL_CTX *s, int purpose) {
423 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
424}
425
426int SSL_set_purpose(SSL *s, int purpose) {
427 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
428}
429
430int SSL_CTX_set_trust(SSL_CTX *s, int trust) {
431 return X509_VERIFY_PARAM_set_trust(s->param, trust);
432}
433
434int SSL_set_trust(SSL *s, int trust) {
435 return X509_VERIFY_PARAM_set_trust(s->param, trust);
436}
437
438int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm) {
439 return X509_VERIFY_PARAM_set1(ctx->param, vpm);
440}
441
442int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm) {
443 return X509_VERIFY_PARAM_set1(ssl->param, vpm);
444}
Adam Langley95c29f32014-06-20 12:00:00 -0700445
Adam Langley858a88d2014-06-20 12:00:00 -0700446void ssl_cipher_preference_list_free(
Adam Langleyfcf25832014-12-18 17:42:32 -0800447 struct ssl_cipher_preference_list_st *cipher_list) {
David Benjamin5d1ec732015-04-22 13:38:00 -0400448 if (cipher_list == NULL) {
449 return;
450 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800451 sk_SSL_CIPHER_free(cipher_list->ciphers);
452 OPENSSL_free(cipher_list->in_group_flags);
453 OPENSSL_free(cipher_list);
454}
Adam Langley858a88d2014-06-20 12:00:00 -0700455
Adam Langleyfcf25832014-12-18 17:42:32 -0800456struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_dup(
457 struct ssl_cipher_preference_list_st *cipher_list) {
458 struct ssl_cipher_preference_list_st *ret = NULL;
459 size_t n = sk_SSL_CIPHER_num(cipher_list->ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700460
Adam Langleyfcf25832014-12-18 17:42:32 -0800461 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
462 if (!ret) {
463 goto err;
464 }
465
466 ret->ciphers = NULL;
467 ret->in_group_flags = NULL;
468 ret->ciphers = sk_SSL_CIPHER_dup(cipher_list->ciphers);
469 if (!ret->ciphers) {
470 goto err;
471 }
472 ret->in_group_flags = BUF_memdup(cipher_list->in_group_flags, n);
473 if (!ret->in_group_flags) {
474 goto err;
475 }
476
477 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -0700478
479err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400480 ssl_cipher_preference_list_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -0800481 return NULL;
482}
Adam Langley858a88d2014-06-20 12:00:00 -0700483
Adam Langleyfcf25832014-12-18 17:42:32 -0800484struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_from_ciphers(
David Benjamin60da0cd2015-05-03 15:21:28 -0400485 STACK_OF(SSL_CIPHER) *ciphers) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800486 struct ssl_cipher_preference_list_st *ret = NULL;
487 size_t n = sk_SSL_CIPHER_num(ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700488
Adam Langleyfcf25832014-12-18 17:42:32 -0800489 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
490 if (!ret) {
491 goto err;
492 }
493 ret->ciphers = NULL;
494 ret->in_group_flags = NULL;
495 ret->ciphers = sk_SSL_CIPHER_dup(ciphers);
496 if (!ret->ciphers) {
497 goto err;
498 }
499 ret->in_group_flags = OPENSSL_malloc(n);
500 if (!ret->in_group_flags) {
501 goto err;
502 }
503 memset(ret->in_group_flags, 0, n);
504 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -0700505
506err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400507 ssl_cipher_preference_list_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -0800508 return NULL;
509}
Adam Langley858a88d2014-06-20 12:00:00 -0700510
Adam Langleyfcf25832014-12-18 17:42:32 -0800511X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) { return ctx->param; }
Adam Langley95c29f32014-06-20 12:00:00 -0700512
Adam Langleyfcf25832014-12-18 17:42:32 -0800513X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) { return ssl->param; }
Adam Langley95c29f32014-06-20 12:00:00 -0700514
David Benjamin7481d392015-07-05 19:38:46 -0400515void SSL_certs_clear(SSL *ssl) { ssl_cert_clear_certs(ssl->cert); }
Adam Langley95c29f32014-06-20 12:00:00 -0700516
David Benjamin4831c332015-05-16 11:43:13 -0400517void SSL_free(SSL *ssl) {
518 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800519 return;
520 }
Adam Langley95c29f32014-06-20 12:00:00 -0700521
David Benjamin4831c332015-05-16 11:43:13 -0400522 X509_VERIFY_PARAM_free(ssl->param);
Adam Langley95c29f32014-06-20 12:00:00 -0700523
David Benjamin4831c332015-05-16 11:43:13 -0400524 CRYPTO_free_ex_data(&g_ex_data_class_ssl, ssl, &ssl->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700525
David Benjamin4831c332015-05-16 11:43:13 -0400526 if (ssl->bbio != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800527 /* If the buffering BIO is in place, pop it off */
David Benjamin4831c332015-05-16 11:43:13 -0400528 if (ssl->bbio == ssl->wbio) {
529 ssl->wbio = BIO_pop(ssl->wbio);
Adam Langleyfcf25832014-12-18 17:42:32 -0800530 }
David Benjamin4831c332015-05-16 11:43:13 -0400531 BIO_free(ssl->bbio);
532 ssl->bbio = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800533 }
Adam Langley95c29f32014-06-20 12:00:00 -0700534
David Benjamin4831c332015-05-16 11:43:13 -0400535 int free_wbio = ssl->wbio != ssl->rbio;
536 BIO_free_all(ssl->rbio);
David Benjamin2755a3e2015-04-22 16:17:58 -0400537 if (free_wbio) {
David Benjamin4831c332015-05-16 11:43:13 -0400538 BIO_free_all(ssl->wbio);
Adam Langleyfcf25832014-12-18 17:42:32 -0800539 }
Adam Langley95c29f32014-06-20 12:00:00 -0700540
David Benjamin4831c332015-05-16 11:43:13 -0400541 BUF_MEM_free(ssl->init_buf);
Adam Langley95c29f32014-06-20 12:00:00 -0700542
Adam Langleyfcf25832014-12-18 17:42:32 -0800543 /* add extra stuff */
David Benjamin4831c332015-05-16 11:43:13 -0400544 ssl_cipher_preference_list_free(ssl->cipher_list);
545 sk_SSL_CIPHER_free(ssl->cipher_list_by_id);
Adam Langley95c29f32014-06-20 12:00:00 -0700546
David Benjamin4831c332015-05-16 11:43:13 -0400547 ssl_clear_bad_session(ssl);
548 SSL_SESSION_free(ssl->session);
Adam Langley95c29f32014-06-20 12:00:00 -0700549
David Benjamin4831c332015-05-16 11:43:13 -0400550 ssl_clear_cipher_ctx(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700551
David Benjamin4831c332015-05-16 11:43:13 -0400552 ssl_cert_free(ssl->cert);
Adam Langley0289c732014-06-20 12:00:00 -0700553
David Benjamin4831c332015-05-16 11:43:13 -0400554 OPENSSL_free(ssl->tlsext_hostname);
555 SSL_CTX_free(ssl->initial_ctx);
David Benjamin4831c332015-05-16 11:43:13 -0400556 OPENSSL_free(ssl->tlsext_ellipticcurvelist);
557 OPENSSL_free(ssl->alpn_client_proto_list);
558 EVP_PKEY_free(ssl->tlsext_channel_id_private);
559 OPENSSL_free(ssl->psk_identity_hint);
560 sk_X509_NAME_pop_free(ssl->client_CA, X509_NAME_free);
561 OPENSSL_free(ssl->next_proto_negotiated);
562 sk_SRTP_PROTECTION_PROFILE_free(ssl->srtp_profiles);
Adam Langley95c29f32014-06-20 12:00:00 -0700563
David Benjamin4831c332015-05-16 11:43:13 -0400564 if (ssl->method != NULL) {
565 ssl->method->ssl_free(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800566 }
David Benjamin4831c332015-05-16 11:43:13 -0400567 SSL_CTX_free(ssl->ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700568
David Benjamin4831c332015-05-16 11:43:13 -0400569 OPENSSL_free(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800570}
Adam Langley95c29f32014-06-20 12:00:00 -0700571
Adam Langleyfcf25832014-12-18 17:42:32 -0800572void SSL_set_bio(SSL *s, BIO *rbio, BIO *wbio) {
573 /* If the output buffering BIO is still in place, remove it. */
574 if (s->bbio != NULL) {
575 if (s->wbio == s->bbio) {
576 s->wbio = s->wbio->next_bio;
577 s->bbio->next_bio = NULL;
578 }
579 }
Adam Langley95c29f32014-06-20 12:00:00 -0700580
David Benjamin2755a3e2015-04-22 16:17:58 -0400581 if (s->rbio != rbio) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800582 BIO_free_all(s->rbio);
583 }
David Benjamin2755a3e2015-04-22 16:17:58 -0400584 if (s->wbio != wbio && s->rbio != s->wbio) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800585 BIO_free_all(s->wbio);
586 }
587 s->rbio = rbio;
588 s->wbio = wbio;
589}
Adam Langley95c29f32014-06-20 12:00:00 -0700590
Adam Langleyfcf25832014-12-18 17:42:32 -0800591BIO *SSL_get_rbio(const SSL *s) { return s->rbio; }
Adam Langley95c29f32014-06-20 12:00:00 -0700592
Adam Langleyfcf25832014-12-18 17:42:32 -0800593BIO *SSL_get_wbio(const SSL *s) { return s->wbio; }
Adam Langley95c29f32014-06-20 12:00:00 -0700594
Adam Langleyfcf25832014-12-18 17:42:32 -0800595int SSL_get_fd(const SSL *s) { return SSL_get_rfd(s); }
Adam Langley95c29f32014-06-20 12:00:00 -0700596
Adam Langleyfcf25832014-12-18 17:42:32 -0800597int SSL_get_rfd(const SSL *s) {
598 int ret = -1;
599 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700600
Adam Langleyfcf25832014-12-18 17:42:32 -0800601 b = SSL_get_rbio(s);
602 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
603 if (r != NULL) {
604 BIO_get_fd(r, &ret);
605 }
606 return ret;
607}
Adam Langley95c29f32014-06-20 12:00:00 -0700608
Adam Langleyfcf25832014-12-18 17:42:32 -0800609int SSL_get_wfd(const SSL *s) {
610 int ret = -1;
611 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700612
Adam Langleyfcf25832014-12-18 17:42:32 -0800613 b = SSL_get_wbio(s);
614 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
615 if (r != NULL) {
616 BIO_get_fd(r, &ret);
617 }
Adam Langley95c29f32014-06-20 12:00:00 -0700618
Adam Langleyfcf25832014-12-18 17:42:32 -0800619 return ret;
620}
Adam Langley95c29f32014-06-20 12:00:00 -0700621
Adam Langleyfcf25832014-12-18 17:42:32 -0800622int SSL_set_fd(SSL *s, int fd) {
623 int ret = 0;
624 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700625
Adam Langleyfcf25832014-12-18 17:42:32 -0800626 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700627
Adam Langleyfcf25832014-12-18 17:42:32 -0800628 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400629 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -0800630 goto err;
631 }
632 BIO_set_fd(bio, fd, BIO_NOCLOSE);
633 SSL_set_bio(s, bio, bio);
634 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700635
Adam Langley95c29f32014-06-20 12:00:00 -0700636err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800637 return ret;
638}
Adam Langley95c29f32014-06-20 12:00:00 -0700639
Adam Langleyfcf25832014-12-18 17:42:32 -0800640int SSL_set_wfd(SSL *s, int fd) {
641 int ret = 0;
642 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700643
Adam Langleyfcf25832014-12-18 17:42:32 -0800644 if (s->rbio == NULL || BIO_method_type(s->rbio) != BIO_TYPE_FD ||
645 (int)BIO_get_fd(s->rbio, NULL) != fd) {
646 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700647
Adam Langleyfcf25832014-12-18 17:42:32 -0800648 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400649 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -0800650 goto err;
651 }
652 BIO_set_fd(bio, fd, BIO_NOCLOSE);
653 SSL_set_bio(s, SSL_get_rbio(s), bio);
654 } else {
655 SSL_set_bio(s, SSL_get_rbio(s), SSL_get_rbio(s));
656 }
657
658 ret = 1;
659
Adam Langley95c29f32014-06-20 12:00:00 -0700660err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800661 return ret;
662}
Adam Langley95c29f32014-06-20 12:00:00 -0700663
Adam Langleyfcf25832014-12-18 17:42:32 -0800664int SSL_set_rfd(SSL *s, int fd) {
665 int ret = 0;
666 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700667
Adam Langleyfcf25832014-12-18 17:42:32 -0800668 if (s->wbio == NULL || BIO_method_type(s->wbio) != BIO_TYPE_FD ||
669 (int)BIO_get_fd(s->wbio, NULL) != fd) {
670 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700671
Adam Langleyfcf25832014-12-18 17:42:32 -0800672 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400673 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -0800674 goto err;
675 }
676 BIO_set_fd(bio, fd, BIO_NOCLOSE);
677 SSL_set_bio(s, bio, SSL_get_wbio(s));
678 } else {
679 SSL_set_bio(s, SSL_get_wbio(s), SSL_get_wbio(s));
680 }
681 ret = 1;
682
Adam Langley95c29f32014-06-20 12:00:00 -0700683err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800684 return ret;
685}
Adam Langley95c29f32014-06-20 12:00:00 -0700686
687/* return length of latest Finished message we sent, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -0800688size_t SSL_get_finished(const SSL *s, void *buf, size_t count) {
689 size_t ret = 0;
690
691 if (s->s3 != NULL) {
692 ret = s->s3->tmp.finish_md_len;
693 if (count > ret) {
694 count = ret;
695 }
696 memcpy(buf, s->s3->tmp.finish_md, count);
697 }
698
699 return ret;
700}
Adam Langley95c29f32014-06-20 12:00:00 -0700701
702/* return length of latest Finished message we expected, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -0800703size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count) {
704 size_t ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700705
Adam Langleyfcf25832014-12-18 17:42:32 -0800706 if (s->s3 != NULL) {
707 ret = s->s3->tmp.peer_finish_md_len;
708 if (count > ret) {
709 count = ret;
710 }
711 memcpy(buf, s->s3->tmp.peer_finish_md, count);
712 }
Adam Langley95c29f32014-06-20 12:00:00 -0700713
Adam Langleyfcf25832014-12-18 17:42:32 -0800714 return ret;
715}
Adam Langley95c29f32014-06-20 12:00:00 -0700716
Adam Langleyfcf25832014-12-18 17:42:32 -0800717int SSL_get_verify_mode(const SSL *s) { return s->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -0700718
Adam Langleyfcf25832014-12-18 17:42:32 -0800719int SSL_get_verify_depth(const SSL *s) {
720 return X509_VERIFY_PARAM_get_depth(s->param);
721}
Adam Langley95c29f32014-06-20 12:00:00 -0700722
Adam Langleyfcf25832014-12-18 17:42:32 -0800723int (*SSL_get_verify_callback(const SSL *s))(int, X509_STORE_CTX *) {
724 return s->verify_callback;
725}
Adam Langley95c29f32014-06-20 12:00:00 -0700726
Adam Langleyfcf25832014-12-18 17:42:32 -0800727int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) { return ctx->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -0700728
Adam Langleyfcf25832014-12-18 17:42:32 -0800729int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) {
730 return X509_VERIFY_PARAM_get_depth(ctx->param);
731}
Adam Langley95c29f32014-06-20 12:00:00 -0700732
Adam Langleyfcf25832014-12-18 17:42:32 -0800733int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int, X509_STORE_CTX *) {
734 return ctx->default_verify_callback;
735}
Adam Langley95c29f32014-06-20 12:00:00 -0700736
Adam Langleyfcf25832014-12-18 17:42:32 -0800737void SSL_set_verify(SSL *s, int mode,
738 int (*callback)(int ok, X509_STORE_CTX *ctx)) {
739 s->verify_mode = mode;
740 if (callback != NULL) {
741 s->verify_callback = callback;
742 }
743}
Adam Langley95c29f32014-06-20 12:00:00 -0700744
Adam Langleyfcf25832014-12-18 17:42:32 -0800745void SSL_set_verify_depth(SSL *s, int depth) {
746 X509_VERIFY_PARAM_set_depth(s->param, depth);
747}
Adam Langley95c29f32014-06-20 12:00:00 -0700748
David Benjamin9a41d1b2015-05-16 01:30:09 -0400749int SSL_CTX_get_read_ahead(const SSL_CTX *ctx) { return 0; }
Adam Langley95c29f32014-06-20 12:00:00 -0700750
David Benjamin9a41d1b2015-05-16 01:30:09 -0400751int SSL_get_read_ahead(const SSL *s) { return 0; }
Adam Langley95c29f32014-06-20 12:00:00 -0700752
David Benjamin9a41d1b2015-05-16 01:30:09 -0400753void SSL_CTX_set_read_ahead(SSL_CTX *ctx, int yes) { }
David Benjamin61ecccf2015-05-05 09:44:51 -0400754
David Benjamin9a41d1b2015-05-16 01:30:09 -0400755void SSL_set_read_ahead(SSL *s, int yes) { }
David Benjamin61ecccf2015-05-05 09:44:51 -0400756
Adam Langleyfcf25832014-12-18 17:42:32 -0800757int SSL_pending(const SSL *s) {
David Benjamin904dc722015-05-30 16:39:07 -0400758 return (s->s3->rrec.type == SSL3_RT_APPLICATION_DATA) ? s->s3->rrec.length
759 : 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800760}
Adam Langley95c29f32014-06-20 12:00:00 -0700761
David Benjamin26416e92015-08-22 16:04:17 -0400762X509 *SSL_get_peer_certificate(const SSL *ssl) {
763 if (ssl == NULL || ssl->session == NULL || ssl->session->peer == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800764 return NULL;
765 }
David Benjamin26416e92015-08-22 16:04:17 -0400766 return X509_up_ref(ssl->session->peer);
Adam Langleyfcf25832014-12-18 17:42:32 -0800767}
768
David Benjamin26416e92015-08-22 16:04:17 -0400769STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *ssl) {
770 if (ssl == NULL || ssl->session == NULL) {
771 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800772 }
David Benjamin26416e92015-08-22 16:04:17 -0400773 return ssl->session->cert_chain;
Adam Langleyfcf25832014-12-18 17:42:32 -0800774}
Adam Langley95c29f32014-06-20 12:00:00 -0700775
Adam Langley95c29f32014-06-20 12:00:00 -0700776/* Fix this so it checks all the valid key/cert options */
Adam Langleyfcf25832014-12-18 17:42:32 -0800777int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
David Benjamin651b3d92015-08-09 12:07:25 -0400778 if (ctx->cert->x509 == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400779 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800780 return 0;
781 }
782
David Benjamind1d80782015-07-05 11:54:09 -0400783 if (ctx->cert->privatekey == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400784 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800785 return 0;
786 }
787
David Benjamind1d80782015-07-05 11:54:09 -0400788 return X509_check_private_key(ctx->cert->x509, ctx->cert->privatekey);
Adam Langleyfcf25832014-12-18 17:42:32 -0800789}
Adam Langley95c29f32014-06-20 12:00:00 -0700790
791/* Fix this function so that it takes an optional type parameter */
Adam Langleyfcf25832014-12-18 17:42:32 -0800792int SSL_check_private_key(const SSL *ssl) {
David Benjamind1d80782015-07-05 11:54:09 -0400793 if (ssl->cert->x509 == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400794 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800795 return 0;
796 }
David Benjamin0b145c22014-11-26 20:10:09 -0500797
David Benjamind1d80782015-07-05 11:54:09 -0400798 if (ssl->cert->privatekey == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400799 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800800 return 0;
801 }
Adam Langley95c29f32014-06-20 12:00:00 -0700802
David Benjamind1d80782015-07-05 11:54:09 -0400803 return X509_check_private_key(ssl->cert->x509, ssl->cert->privatekey);
Adam Langleyfcf25832014-12-18 17:42:32 -0800804}
Adam Langley95c29f32014-06-20 12:00:00 -0700805
Adam Langleyfcf25832014-12-18 17:42:32 -0800806int SSL_accept(SSL *s) {
807 if (s->handshake_func == 0) {
808 /* Not properly initialized yet */
809 SSL_set_accept_state(s);
810 }
David Benjamin0b145c22014-11-26 20:10:09 -0500811
Adam Langleyfcf25832014-12-18 17:42:32 -0800812 if (s->handshake_func != s->method->ssl_accept) {
David Benjamin3570d732015-06-29 00:28:17 -0400813 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800814 return -1;
815 }
Adam Langley95c29f32014-06-20 12:00:00 -0700816
Adam Langleyfcf25832014-12-18 17:42:32 -0800817 return s->handshake_func(s);
818}
Adam Langley95c29f32014-06-20 12:00:00 -0700819
Adam Langleyfcf25832014-12-18 17:42:32 -0800820int SSL_connect(SSL *s) {
821 if (s->handshake_func == 0) {
822 /* Not properly initialized yet */
823 SSL_set_connect_state(s);
824 }
Adam Langley95c29f32014-06-20 12:00:00 -0700825
Adam Langleyfcf25832014-12-18 17:42:32 -0800826 if (s->handshake_func != s->method->ssl_connect) {
David Benjamin3570d732015-06-29 00:28:17 -0400827 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800828 return -1;
829 }
Adam Langley95c29f32014-06-20 12:00:00 -0700830
Adam Langleyfcf25832014-12-18 17:42:32 -0800831 return s->handshake_func(s);
832}
Adam Langley95c29f32014-06-20 12:00:00 -0700833
Adam Langleyfcf25832014-12-18 17:42:32 -0800834long SSL_get_default_timeout(const SSL *s) {
835 return SSL_DEFAULT_SESSION_TIMEOUT;
836}
Adam Langley95c29f32014-06-20 12:00:00 -0700837
Adam Langleyfcf25832014-12-18 17:42:32 -0800838int SSL_read(SSL *s, void *buf, int num) {
839 if (s->handshake_func == 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400840 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800841 return -1;
842 }
Adam Langley95c29f32014-06-20 12:00:00 -0700843
Adam Langleyfcf25832014-12-18 17:42:32 -0800844 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
845 s->rwstate = SSL_NOTHING;
846 return 0;
847 }
Adam Langley95c29f32014-06-20 12:00:00 -0700848
David Benjamin904dc722015-05-30 16:39:07 -0400849 ERR_clear_system_error();
850 return s->method->ssl_read_app_data(s, buf, num, 0);
Adam Langleyfcf25832014-12-18 17:42:32 -0800851}
Adam Langley95c29f32014-06-20 12:00:00 -0700852
Adam Langleyfcf25832014-12-18 17:42:32 -0800853int SSL_peek(SSL *s, void *buf, int num) {
854 if (s->handshake_func == 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400855 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800856 return -1;
857 }
Adam Langley95c29f32014-06-20 12:00:00 -0700858
Adam Langleyfcf25832014-12-18 17:42:32 -0800859 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
860 return 0;
861 }
Adam Langley95c29f32014-06-20 12:00:00 -0700862
David Benjamin904dc722015-05-30 16:39:07 -0400863 ERR_clear_system_error();
864 return s->method->ssl_read_app_data(s, buf, num, 1);
Adam Langleyfcf25832014-12-18 17:42:32 -0800865}
Adam Langley95c29f32014-06-20 12:00:00 -0700866
Adam Langleyfcf25832014-12-18 17:42:32 -0800867int SSL_write(SSL *s, const 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_SENT_SHUTDOWN) {
874 s->rwstate = SSL_NOTHING;
David Benjamin3570d732015-06-29 00:28:17 -0400875 OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
Adam Langleyfcf25832014-12-18 17:42:32 -0800876 return -1;
877 }
Adam Langley95c29f32014-06-20 12:00:00 -0700878
David Benjamin904dc722015-05-30 16:39:07 -0400879 ERR_clear_system_error();
880 return s->method->ssl_write_app_data(s, buf, num);
Adam Langleyfcf25832014-12-18 17:42:32 -0800881}
Adam Langley95c29f32014-06-20 12:00:00 -0700882
Adam Langleyfcf25832014-12-18 17:42:32 -0800883int SSL_shutdown(SSL *s) {
884 /* Note that this function behaves differently from what one might expect.
885 * Return values are 0 for no success (yet), 1 for success; but calling it
886 * once is usually not enough, even if blocking I/O is used (see
887 * ssl3_shutdown). */
Adam Langley95c29f32014-06-20 12:00:00 -0700888
Adam Langleyfcf25832014-12-18 17:42:32 -0800889 if (s->handshake_func == 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400890 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800891 return -1;
892 }
Adam Langley95c29f32014-06-20 12:00:00 -0700893
David Benjamin904dc722015-05-30 16:39:07 -0400894 if (SSL_in_init(s)) {
895 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800896 }
Adam Langley95c29f32014-06-20 12:00:00 -0700897
David Benjamin904dc722015-05-30 16:39:07 -0400898 /* Do nothing if configured not to send a close_notify. */
899 if (s->quiet_shutdown) {
900 s->shutdown = SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN;
901 return 1;
902 }
903
904 if (!(s->shutdown & SSL_SENT_SHUTDOWN)) {
905 s->shutdown |= SSL_SENT_SHUTDOWN;
906 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_CLOSE_NOTIFY);
907
908 /* our shutdown alert has been sent now, and if it still needs to be
909 * written, s->s3->alert_dispatch will be true */
910 if (s->s3->alert_dispatch) {
911 return -1; /* return WANT_WRITE */
912 }
913 } else if (s->s3->alert_dispatch) {
914 /* resend it if not sent */
915 int ret = s->method->ssl_dispatch_alert(s);
916 if (ret == -1) {
917 /* we only get to return -1 here the 2nd/Nth invocation, we must have
918 * already signalled return 0 upon a previous invoation, return
919 * WANT_WRITE */
920 return ret;
921 }
922 } else if (!(s->shutdown & SSL_RECEIVED_SHUTDOWN)) {
923 /* If we are waiting for a close from our peer, we are closed */
924 s->method->ssl_read_close_notify(s);
925 if (!(s->shutdown & SSL_RECEIVED_SHUTDOWN)) {
926 return -1; /* return WANT_READ */
927 }
928 }
929
930 if (s->shutdown == (SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN) &&
931 !s->s3->alert_dispatch) {
932 return 1;
933 } else {
934 return 0;
935 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800936}
Adam Langley95c29f32014-06-20 12:00:00 -0700937
David Benjamin44d3eed2015-05-21 01:29:55 -0400938int SSL_renegotiate(SSL *ssl) {
939 /* Caller-initiated renegotiation is not supported. */
David Benjamin3570d732015-06-29 00:28:17 -0400940 OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
David Benjamin44d3eed2015-05-21 01:29:55 -0400941 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800942}
Adam Langley95c29f32014-06-20 12:00:00 -0700943
David Benjamin44d3eed2015-05-21 01:29:55 -0400944int SSL_renegotiate_pending(SSL *ssl) {
945 return SSL_in_init(ssl) && ssl->s3->initial_handshake_complete;
Adam Langleyfcf25832014-12-18 17:42:32 -0800946}
Adam Langley95c29f32014-06-20 12:00:00 -0700947
David Benjamin61ecccf2015-05-05 09:44:51 -0400948uint32_t SSL_CTX_set_options(SSL_CTX *ctx, uint32_t options) {
949 ctx->options |= options;
950 return ctx->options;
951}
952
953uint32_t SSL_set_options(SSL *ssl, uint32_t options) {
954 ssl->options |= options;
955 return ssl->options;
956}
957
958uint32_t SSL_CTX_clear_options(SSL_CTX *ctx, uint32_t options) {
959 ctx->options &= ~options;
960 return ctx->options;
961}
962
963uint32_t SSL_clear_options(SSL *ssl, uint32_t options) {
964 ssl->options &= ~options;
965 return ssl->options;
966}
967
968uint32_t SSL_CTX_get_options(const SSL_CTX *ctx) { return ctx->options; }
969
970uint32_t SSL_get_options(const SSL *ssl) { return ssl->options; }
971
972uint32_t SSL_CTX_set_mode(SSL_CTX *ctx, uint32_t mode) {
973 ctx->mode |= mode;
974 return ctx->mode;
975}
976
977uint32_t SSL_set_mode(SSL *ssl, uint32_t mode) {
978 ssl->mode |= mode;
979 return ssl->mode;
980}
981
982uint32_t SSL_CTX_clear_mode(SSL_CTX *ctx, uint32_t mode) {
983 ctx->mode &= ~mode;
984 return ctx->mode;
985}
986
987uint32_t SSL_clear_mode(SSL *ssl, uint32_t mode) {
988 ssl->mode &= ~mode;
989 return ssl->mode;
990}
991
992uint32_t SSL_CTX_get_mode(const SSL_CTX *ctx) { return ctx->mode; }
993
994uint32_t SSL_get_mode(const SSL *ssl) { return ssl->mode; }
995
David Benjamin1d0a1942015-04-26 15:35:35 -0400996size_t SSL_CTX_get_max_cert_list(const SSL_CTX *ctx) {
997 return ctx->max_cert_list;
998}
999
1000void SSL_CTX_set_max_cert_list(SSL_CTX *ctx, size_t max_cert_list) {
1001 if (max_cert_list > kMaxHandshakeSize) {
1002 max_cert_list = kMaxHandshakeSize;
1003 }
1004 ctx->max_cert_list = (uint32_t)max_cert_list;
1005}
1006
1007size_t SSL_get_max_cert_list(const SSL *ssl) {
1008 return ssl->max_cert_list;
1009}
1010
1011void SSL_set_max_cert_list(SSL *ssl, size_t max_cert_list) {
1012 if (max_cert_list > kMaxHandshakeSize) {
1013 max_cert_list = kMaxHandshakeSize;
1014 }
1015 ssl->max_cert_list = (uint32_t)max_cert_list;
1016}
1017
1018void SSL_CTX_set_max_send_fragment(SSL_CTX *ctx, size_t max_send_fragment) {
1019 if (max_send_fragment < 512) {
1020 max_send_fragment = 512;
1021 }
1022 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1023 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1024 }
1025 ctx->max_send_fragment = (uint16_t)max_send_fragment;
1026}
1027
1028void SSL_set_max_send_fragment(SSL *ssl, size_t max_send_fragment) {
1029 if (max_send_fragment < 512) {
1030 max_send_fragment = 512;
1031 }
1032 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1033 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1034 }
1035 ssl->max_send_fragment = (uint16_t)max_send_fragment;
1036}
1037
David Benjamincb9cf792015-05-05 09:46:14 -04001038int SSL_set_mtu(SSL *ssl, unsigned mtu) {
1039 if (!SSL_IS_DTLS(ssl) || mtu < dtls1_min_mtu()) {
1040 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001041 }
David Benjamincb9cf792015-05-05 09:46:14 -04001042 ssl->d1->mtu = mtu;
1043 return 1;
1044}
1045
1046int SSL_get_secure_renegotiation_support(const SSL *ssl) {
1047 return ssl->s3->send_connection_binding;
1048}
1049
Adam Langleyfcf25832014-12-18 17:42:32 -08001050LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) { return ctx->sessions; }
1051
David Benjamin71f7d3d2015-05-05 09:46:38 -04001052size_t SSL_CTX_sess_number(const SSL_CTX *ctx) {
1053 return lh_SSL_SESSION_num_items(ctx->sessions);
1054}
1055
1056unsigned long SSL_CTX_sess_set_cache_size(SSL_CTX *ctx, unsigned long size) {
1057 unsigned long ret = ctx->session_cache_size;
1058 ctx->session_cache_size = size;
1059 return ret;
1060}
1061
1062unsigned long SSL_CTX_sess_get_cache_size(const SSL_CTX *ctx) {
1063 return ctx->session_cache_size;
1064}
1065
1066int SSL_CTX_set_session_cache_mode(SSL_CTX *ctx, int mode) {
1067 int ret = ctx->session_cache_mode;
1068 ctx->session_cache_mode = mode;
1069 return ret;
1070}
1071
1072int SSL_CTX_get_session_cache_mode(const SSL_CTX *ctx) {
1073 return ctx->session_cache_mode;
1074}
1075
Adam Langleyfcf25832014-12-18 17:42:32 -08001076/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001077 * preference */
David Benjamin60da0cd2015-05-03 15:21:28 -04001078STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001079 if (s == NULL) {
1080 return NULL;
1081 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001082
Adam Langleyfcf25832014-12-18 17:42:32 -08001083 if (s->cipher_list != NULL) {
1084 return s->cipher_list->ciphers;
1085 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001086
Adam Langleyfcf25832014-12-18 17:42:32 -08001087 if (s->version >= TLS1_1_VERSION && s->ctx != NULL &&
1088 s->ctx->cipher_list_tls11 != NULL) {
1089 return s->ctx->cipher_list_tls11->ciphers;
1090 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001091
Adam Langleycef75832015-09-03 14:51:12 -07001092 if (s->version >= TLS1_VERSION && s->ctx != NULL &&
1093 s->ctx->cipher_list_tls10 != NULL) {
1094 return s->ctx->cipher_list_tls10->ciphers;
1095 }
1096
Adam Langleyfcf25832014-12-18 17:42:32 -08001097 if (s->ctx != NULL && s->ctx->cipher_list != NULL) {
1098 return s->ctx->cipher_list->ciphers;
1099 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001100
Adam Langleyfcf25832014-12-18 17:42:32 -08001101 return NULL;
1102}
Adam Langley95c29f32014-06-20 12:00:00 -07001103
Adam Langleyfcf25832014-12-18 17:42:32 -08001104/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001105 * algorithm id */
David Benjamin60da0cd2015-05-03 15:21:28 -04001106STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001107 if (s == NULL) {
1108 return NULL;
1109 }
Adam Langley95c29f32014-06-20 12:00:00 -07001110
Adam Langleyfcf25832014-12-18 17:42:32 -08001111 if (s->cipher_list_by_id != NULL) {
1112 return s->cipher_list_by_id;
1113 }
Adam Langley95c29f32014-06-20 12:00:00 -07001114
Adam Langleyfcf25832014-12-18 17:42:32 -08001115 if (s->ctx != NULL && s->ctx->cipher_list_by_id != NULL) {
1116 return s->ctx->cipher_list_by_id;
1117 }
Adam Langley95c29f32014-06-20 12:00:00 -07001118
Adam Langleyfcf25832014-12-18 17:42:32 -08001119 return NULL;
1120}
Adam Langley95c29f32014-06-20 12:00:00 -07001121
Adam Langleyfcf25832014-12-18 17:42:32 -08001122/* The old interface to get the same thing as SSL_get_ciphers() */
1123const char *SSL_get_cipher_list(const SSL *s, int n) {
1124 const SSL_CIPHER *c;
David Benjamin60da0cd2015-05-03 15:21:28 -04001125 STACK_OF(SSL_CIPHER) *sk;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001126
Adam Langleyfcf25832014-12-18 17:42:32 -08001127 if (s == NULL) {
1128 return NULL;
1129 }
Adam Langley95c29f32014-06-20 12:00:00 -07001130
Adam Langleyfcf25832014-12-18 17:42:32 -08001131 sk = SSL_get_ciphers(s);
1132 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
1133 return NULL;
1134 }
Adam Langley95c29f32014-06-20 12:00:00 -07001135
Adam Langleyfcf25832014-12-18 17:42:32 -08001136 c = sk_SSL_CIPHER_value(sk, n);
1137 if (c == NULL) {
1138 return NULL;
1139 }
Adam Langley95c29f32014-06-20 12:00:00 -07001140
Adam Langleyfcf25832014-12-18 17:42:32 -08001141 return c->name;
1142}
David Benjamin5491e3f2014-09-29 19:33:09 -04001143
Adam Langleyfcf25832014-12-18 17:42:32 -08001144/* specify the ciphers to be used by default by the SSL_CTX */
1145int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
1146 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001147
Adam Langleyfcf25832014-12-18 17:42:32 -08001148 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001149 &ctx->cipher_list_by_id, str);
Adam Langleyfcf25832014-12-18 17:42:32 -08001150 /* ssl_create_cipher_list may return an empty stack if it was unable to find
1151 * a cipher matching the given rule string (for example if the rule string
1152 * specifies a cipher which has been disabled). This is not an error as far
1153 * as ssl_create_cipher_list is concerned, and hence ctx->cipher_list and
1154 * ctx->cipher_list_by_id has been updated. */
1155 if (sk == NULL) {
1156 return 0;
1157 } else if (sk_SSL_CIPHER_num(sk) == 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001158 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
Adam Langleyfcf25832014-12-18 17:42:32 -08001159 return 0;
1160 }
Adam Langley95c29f32014-06-20 12:00:00 -07001161
Adam Langleyfcf25832014-12-18 17:42:32 -08001162 return 1;
1163}
David Benjamin39482a12014-07-20 13:30:15 -04001164
Adam Langleycef75832015-09-03 14:51:12 -07001165int SSL_CTX_set_cipher_list_tls10(SSL_CTX *ctx, const char *str) {
1166 STACK_OF(SSL_CIPHER) *sk;
1167
1168 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls10, NULL, str);
1169 if (sk == NULL) {
1170 return 0;
1171 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1172 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
1173 return 0;
1174 }
1175
1176 return 1;
1177}
1178
Adam Langleyfcf25832014-12-18 17:42:32 -08001179int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str) {
1180 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001181
David Benjamin71f07942015-04-08 02:36:59 -04001182 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str);
Adam Langleyfcf25832014-12-18 17:42:32 -08001183 if (sk == NULL) {
1184 return 0;
1185 } else if (sk_SSL_CIPHER_num(sk) == 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001186 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
Adam Langleyfcf25832014-12-18 17:42:32 -08001187 return 0;
1188 }
David Benjamin9f2c0d72014-10-21 22:00:19 -04001189
Adam Langleyfcf25832014-12-18 17:42:32 -08001190 return 1;
1191}
Adam Langley95c29f32014-06-20 12:00:00 -07001192
Adam Langleyfcf25832014-12-18 17:42:32 -08001193/* specify the ciphers to be used by the SSL */
1194int SSL_set_cipher_list(SSL *s, const char *str) {
1195 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001196
Adam Langleyfcf25832014-12-18 17:42:32 -08001197 sk = ssl_create_cipher_list(s->ctx->method, &s->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001198 &s->cipher_list_by_id, str);
David Benjamin39482a12014-07-20 13:30:15 -04001199
Adam Langleyfcf25832014-12-18 17:42:32 -08001200 /* see comment in SSL_CTX_set_cipher_list */
1201 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 Benjamin39482a12014-07-20 13:30:15 -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 -08001211int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, uint8_t *p) {
1212 size_t i;
1213 const SSL_CIPHER *c;
1214 CERT *ct = s->cert;
1215 uint8_t *q;
1216 /* Set disabled masks for this session */
1217 ssl_set_client_disabled(s);
Adam Langley29707792014-06-20 12:00:00 -07001218
Adam Langleyfcf25832014-12-18 17:42:32 -08001219 if (sk == NULL) {
1220 return 0;
1221 }
1222 q = p;
Adam Langley95c29f32014-06-20 12:00:00 -07001223
Adam Langleyfcf25832014-12-18 17:42:32 -08001224 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
1225 c = sk_SSL_CIPHER_value(sk, i);
1226 /* Skip disabled ciphers */
1227 if (c->algorithm_ssl & ct->mask_ssl ||
1228 c->algorithm_mkey & ct->mask_k ||
1229 c->algorithm_auth & ct->mask_a) {
1230 continue;
1231 }
David Benjamina1c90a52015-05-30 17:03:14 -04001232 s2n(ssl_cipher_get_value(c), p);
Adam Langleyfcf25832014-12-18 17:42:32 -08001233 }
1234
1235 /* If all ciphers were disabled, return the error to the caller. */
1236 if (p == q) {
1237 return 0;
1238 }
1239
Adam Langley5021b222015-06-12 18:27:58 -07001240 /* For SSLv3, the SCSV is added. Otherwise the renegotiation extension is
1241 * added. */
1242 if (s->client_version == SSL3_VERSION &&
1243 !s->s3->initial_handshake_complete) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001244 s2n(SSL3_CK_SCSV & 0xffff, p);
Adam Langley5021b222015-06-12 18:27:58 -07001245 /* The renegotiation extension is required to be at index zero. */
1246 s->s3->tmp.extensions.sent |= (1u << 0);
Adam Langleyfcf25832014-12-18 17:42:32 -08001247 }
1248
Adam Langley5f0efe02015-02-20 13:03:16 -08001249 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001250 s2n(SSL3_CK_FALLBACK_SCSV & 0xffff, p);
1251 }
1252
1253 return p - q;
1254}
1255
1256STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs) {
1257 CBS cipher_suites = *cbs;
1258 const SSL_CIPHER *c;
David Benjamin60da0cd2015-05-03 15:21:28 -04001259 STACK_OF(SSL_CIPHER) *sk;
Adam Langleyfcf25832014-12-18 17:42:32 -08001260
1261 if (s->s3) {
1262 s->s3->send_connection_binding = 0;
1263 }
1264
1265 if (CBS_len(&cipher_suites) % 2 != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001266 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
Adam Langleyfcf25832014-12-18 17:42:32 -08001267 return NULL;
1268 }
1269
1270 sk = sk_SSL_CIPHER_new_null();
1271 if (sk == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001272 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001273 goto err;
1274 }
1275
Adam Langleyfcf25832014-12-18 17:42:32 -08001276 while (CBS_len(&cipher_suites) > 0) {
1277 uint16_t cipher_suite;
1278
1279 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
David Benjamin3570d732015-06-29 00:28:17 -04001280 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001281 goto err;
1282 }
1283
1284 /* Check for SCSV. */
1285 if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff)) {
1286 /* SCSV is fatal if renegotiating. */
David Benjamin20f6e972015-05-15 21:51:49 -04001287 if (s->s3->initial_handshake_complete) {
David Benjamin3570d732015-06-29 00:28:17 -04001288 OPENSSL_PUT_ERROR(SSL, SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
Adam Langleyfcf25832014-12-18 17:42:32 -08001289 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1290 goto err;
1291 }
1292 s->s3->send_connection_binding = 1;
1293 continue;
1294 }
1295
1296 /* Check for FALLBACK_SCSV. */
1297 if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff)) {
1298 uint16_t max_version = ssl3_get_max_server_version(s);
1299 if (SSL_IS_DTLS(s) ? (uint16_t)s->version > max_version
1300 : (uint16_t)s->version < max_version) {
David Benjamin3570d732015-06-29 00:28:17 -04001301 OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK);
Adam Langleyfcf25832014-12-18 17:42:32 -08001302 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
1303 goto err;
1304 }
1305 continue;
1306 }
1307
David Benjamina1c90a52015-05-30 17:03:14 -04001308 c = SSL_get_cipher_by_value(cipher_suite);
Adam Langleyfcf25832014-12-18 17:42:32 -08001309 if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
David Benjamin3570d732015-06-29 00:28:17 -04001310 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001311 goto err;
1312 }
1313 }
1314
1315 return sk;
David Benjamin9f2c0d72014-10-21 22:00:19 -04001316
Adam Langley95c29f32014-06-20 12:00:00 -07001317err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001318 sk_SSL_CIPHER_free(sk);
Adam Langleyfcf25832014-12-18 17:42:32 -08001319 return NULL;
1320}
Adam Langley95c29f32014-06-20 12:00:00 -07001321
1322
Adam Langleyfcf25832014-12-18 17:42:32 -08001323/* return a servername extension value if provided in Client Hello, or NULL. So
1324 * far, only host_name types are defined (RFC 3546). */
1325const char *SSL_get_servername(const SSL *s, const int type) {
1326 if (type != TLSEXT_NAMETYPE_host_name) {
1327 return NULL;
1328 }
Adam Langley95c29f32014-06-20 12:00:00 -07001329
Adam Langleyfcf25832014-12-18 17:42:32 -08001330 return s->session && !s->tlsext_hostname ? s->session->tlsext_hostname
1331 : s->tlsext_hostname;
1332}
Adam Langley95c29f32014-06-20 12:00:00 -07001333
Adam Langleyfcf25832014-12-18 17:42:32 -08001334int SSL_get_servername_type(const SSL *s) {
1335 if (s->session &&
1336 (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname)) {
1337 return TLSEXT_NAMETYPE_host_name;
1338 }
Adam Langley95c29f32014-06-20 12:00:00 -07001339
Adam Langleyfcf25832014-12-18 17:42:32 -08001340 return -1;
1341}
Adam Langley95c29f32014-06-20 12:00:00 -07001342
Adam Langleyfcf25832014-12-18 17:42:32 -08001343void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
1344 ctx->signed_cert_timestamps_enabled = 1;
1345}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001346
Adam Langleyfcf25832014-12-18 17:42:32 -08001347int SSL_enable_signed_cert_timestamps(SSL *ssl) {
1348 ssl->signed_cert_timestamps_enabled = 1;
1349 return 1;
1350}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001351
Adam Langleyfcf25832014-12-18 17:42:32 -08001352void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
1353 ctx->ocsp_stapling_enabled = 1;
1354}
David Benjamin6c7aed02014-08-27 16:42:38 -04001355
Adam Langleyfcf25832014-12-18 17:42:32 -08001356int SSL_enable_ocsp_stapling(SSL *ssl) {
1357 ssl->ocsp_stapling_enabled = 1;
1358 return 1;
1359}
David Benjamin6c7aed02014-08-27 16:42:38 -04001360
Adam Langleyfcf25832014-12-18 17:42:32 -08001361void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
1362 size_t *out_len) {
1363 SSL_SESSION *session = ssl->session;
Adam Langley3cb50e02014-08-26 14:00:31 -07001364
Adam Langleyfcf25832014-12-18 17:42:32 -08001365 *out_len = 0;
1366 *out = NULL;
1367 if (ssl->server || !session || !session->tlsext_signed_cert_timestamp_list) {
1368 return;
1369 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001370
Adam Langleyfcf25832014-12-18 17:42:32 -08001371 *out = session->tlsext_signed_cert_timestamp_list;
1372 *out_len = session->tlsext_signed_cert_timestamp_list_length;
1373}
David Benjamin6c7aed02014-08-27 16:42:38 -04001374
Adam Langleyfcf25832014-12-18 17:42:32 -08001375void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
1376 size_t *out_len) {
1377 SSL_SESSION *session = ssl->session;
1378
1379 *out_len = 0;
1380 *out = NULL;
1381 if (ssl->server || !session || !session->ocsp_response) {
1382 return;
1383 }
1384 *out = session->ocsp_response;
1385 *out_len = session->ocsp_response_length;
1386}
David Benjamin6c7aed02014-08-27 16:42:38 -04001387
Paul Lietar4fac72e2015-09-09 13:44:55 +01001388int SSL_CTX_set_signed_cert_timestamp_list(SSL_CTX *ctx, const uint8_t *list,
1389 size_t list_len) {
1390 OPENSSL_free(ctx->signed_cert_timestamp_list);
1391 ctx->signed_cert_timestamp_list_length = 0;
1392
1393 ctx->signed_cert_timestamp_list = BUF_memdup(list, list_len);
1394 if (ctx->signed_cert_timestamp_list == NULL) {
1395 return 0;
1396 }
1397 ctx->signed_cert_timestamp_list_length = list_len;
1398
1399 return 1;
1400}
1401
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001402int SSL_CTX_set_ocsp_response(SSL_CTX *ctx, const uint8_t *response,
1403 size_t response_len) {
1404 OPENSSL_free(ctx->ocsp_response);
1405 ctx->ocsp_response_length = 0;
1406
1407 ctx->ocsp_response = BUF_memdup(response, response_len);
1408 if (ctx->ocsp_response == NULL) {
1409 return 0;
1410 }
1411 ctx->ocsp_response_length = response_len;
1412
1413 return 1;
1414}
1415
Adam Langley95c29f32014-06-20 12:00:00 -07001416/* SSL_select_next_proto implements the standard protocol selection. It is
1417 * expected that this function is called from the callback set by
1418 * SSL_CTX_set_next_proto_select_cb.
1419 *
1420 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1421 * strings. The length byte itself is not included in the length. A byte
1422 * string of length 0 is invalid. No byte string may be truncated.
1423 *
1424 * The current, but experimental algorithm for selecting the protocol is:
1425 *
1426 * 1) If the server doesn't support NPN then this is indicated to the
1427 * callback. In this case, the client application has to abort the connection
1428 * or have a default application level protocol.
1429 *
1430 * 2) If the server supports NPN, but advertises an empty list then the
1431 * client selects the first protcol in its list, but indicates via the
1432 * API that this fallback case was enacted.
1433 *
1434 * 3) Otherwise, the client finds the first protocol in the server's list
1435 * that it supports and selects this protocol. This is because it's
1436 * assumed that the server has better information about which protocol
1437 * a client should use.
1438 *
1439 * 4) If the client doesn't support any of the server's advertised
1440 * protocols, then this is treated the same as case 2.
1441 *
1442 * It returns either
1443 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1444 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1445 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001446int SSL_select_next_proto(uint8_t **out, uint8_t *outlen, const uint8_t *server,
1447 unsigned int server_len, const uint8_t *client,
1448 unsigned int client_len) {
1449 unsigned int i, j;
1450 const uint8_t *result;
1451 int status = OPENSSL_NPN_UNSUPPORTED;
Adam Langley95c29f32014-06-20 12:00:00 -07001452
Adam Langleyfcf25832014-12-18 17:42:32 -08001453 /* For each protocol in server preference order, see if we support it. */
1454 for (i = 0; i < server_len;) {
1455 for (j = 0; j < client_len;) {
1456 if (server[i] == client[j] &&
1457 memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
1458 /* We found a match */
1459 result = &server[i];
1460 status = OPENSSL_NPN_NEGOTIATED;
1461 goto found;
1462 }
1463 j += client[j];
1464 j++;
1465 }
1466 i += server[i];
1467 i++;
1468 }
Adam Langley95c29f32014-06-20 12:00:00 -07001469
Adam Langleyfcf25832014-12-18 17:42:32 -08001470 /* There's no overlap between our protocols and the server's list. */
1471 result = client;
1472 status = OPENSSL_NPN_NO_OVERLAP;
Adam Langley95c29f32014-06-20 12:00:00 -07001473
Adam Langleyfcf25832014-12-18 17:42:32 -08001474found:
1475 *out = (uint8_t *)result + 1;
1476 *outlen = result[0];
1477 return status;
1478}
Adam Langley95c29f32014-06-20 12:00:00 -07001479
Adam Langley95c29f32014-06-20 12:00:00 -07001480/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
1481 * requested protocol for this connection and returns 0. If the client didn't
1482 * request any protocol, then *data is set to NULL.
1483 *
1484 * Note that the client can request any protocol it chooses. The value returned
1485 * from this function need not be a member of the list of supported protocols
Adam Langleyfcf25832014-12-18 17:42:32 -08001486 * provided by the callback. */
1487void SSL_get0_next_proto_negotiated(const SSL *s, const uint8_t **data,
1488 unsigned *len) {
1489 *data = s->next_proto_negotiated;
1490 if (!*data) {
1491 *len = 0;
1492 } else {
1493 *len = s->next_proto_negotiated_len;
1494 }
Adam Langley95c29f32014-06-20 12:00:00 -07001495}
1496
1497/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
1498 * TLS server needs a list of supported protocols for Next Protocol
1499 * Negotiation. The returned list must be in wire format. The list is returned
1500 * by setting |out| to point to it and |outlen| to its length. This memory will
1501 * not be modified, but one should assume that the SSL* keeps a reference to
1502 * it.
1503 *
Adam Langleyfcf25832014-12-18 17:42:32 -08001504 * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise.
1505 * Otherwise, no such extension will be included in the ServerHello. */
1506void SSL_CTX_set_next_protos_advertised_cb(
1507 SSL_CTX *ctx,
1508 int (*cb)(SSL *ssl, const uint8_t **out, unsigned int *outlen, void *arg),
1509 void *arg) {
1510 ctx->next_protos_advertised_cb = cb;
1511 ctx->next_protos_advertised_cb_arg = arg;
1512}
Adam Langley95c29f32014-06-20 12:00:00 -07001513
1514/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1515 * client needs to select a protocol from the server's provided list. |out|
1516 * must be set to point to the selected protocol (which may be within |in|).
1517 * The length of the protocol name must be written into |outlen|. The server's
1518 * advertised protocols are provided in |in| and |inlen|. The callback can
1519 * assume that |in| is syntactically valid.
1520 *
1521 * The client must select a protocol. It is fatal to the connection if this
1522 * callback returns a value other than SSL_TLSEXT_ERR_OK.
1523 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001524void SSL_CTX_set_next_proto_select_cb(
1525 SSL_CTX *ctx, int (*cb)(SSL *s, uint8_t **out, uint8_t *outlen,
1526 const uint8_t *in, unsigned int inlen, void *arg),
1527 void *arg) {
1528 ctx->next_proto_select_cb = cb;
1529 ctx->next_proto_select_cb_arg = arg;
1530}
Adam Langley95c29f32014-06-20 12:00:00 -07001531
Adam Langleyfcf25832014-12-18 17:42:32 -08001532int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
1533 unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001534 OPENSSL_free(ctx->alpn_client_proto_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001535 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1536 if (!ctx->alpn_client_proto_list) {
1537 return 1;
1538 }
1539 ctx->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001540
Adam Langleyfcf25832014-12-18 17:42:32 -08001541 return 0;
1542}
Adam Langley95c29f32014-06-20 12:00:00 -07001543
Adam Langleyfcf25832014-12-18 17:42:32 -08001544int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001545 OPENSSL_free(ssl->alpn_client_proto_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001546 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1547 if (!ssl->alpn_client_proto_list) {
1548 return 1;
1549 }
1550 ssl->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001551
Adam Langleyfcf25832014-12-18 17:42:32 -08001552 return 0;
1553}
Adam Langley95c29f32014-06-20 12:00:00 -07001554
1555/* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
1556 * during ClientHello processing in order to select an ALPN protocol from the
1557 * client's list of offered protocols. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001558void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
1559 int (*cb)(SSL *ssl, const uint8_t **out,
1560 uint8_t *outlen, const uint8_t *in,
1561 unsigned int inlen, void *arg),
1562 void *arg) {
1563 ctx->alpn_select_cb = cb;
1564 ctx->alpn_select_cb_arg = arg;
1565}
Adam Langley95c29f32014-06-20 12:00:00 -07001566
1567/* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
1568 * On return it sets |*data| to point to |*len| bytes of protocol name (not
1569 * including the leading length-prefix byte). If the server didn't respond with
1570 * a negotiated protocol then |*len| will be zero. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001571void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **data,
1572 unsigned *len) {
1573 *data = NULL;
1574 if (ssl->s3) {
1575 *data = ssl->s3->alpn_selected;
1576 }
1577 if (*data == NULL) {
1578 *len = 0;
1579 } else {
1580 *len = ssl->s3->alpn_selected_len;
1581 }
1582}
Adam Langley95c29f32014-06-20 12:00:00 -07001583
David Benjamincfd248b2015-04-03 11:02:24 -04001584int SSL_export_keying_material(SSL *s, uint8_t *out, size_t out_len,
1585 const char *label, size_t label_len,
1586 const uint8_t *context, size_t context_len,
1587 int use_context) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001588 if (s->version < TLS1_VERSION) {
David Benjaminc565ebb2015-04-03 04:06:36 -04001589 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001590 }
Adam Langley95c29f32014-06-20 12:00:00 -07001591
David Benjamincfd248b2015-04-03 11:02:24 -04001592 return s->enc_method->export_keying_material(
1593 s, out, out_len, label, label_len, context, context_len, use_context);
Adam Langleyfcf25832014-12-18 17:42:32 -08001594}
Adam Langley95c29f32014-06-20 12:00:00 -07001595
Adam Langleyfcf25832014-12-18 17:42:32 -08001596static uint32_t ssl_session_hash(const SSL_SESSION *a) {
1597 uint32_t hash =
1598 ((uint32_t)a->session_id[0]) ||
1599 ((uint32_t)a->session_id[1] << 8) ||
1600 ((uint32_t)a->session_id[2] << 16) ||
1601 ((uint32_t)a->session_id[3] << 24);
Adam Langley95c29f32014-06-20 12:00:00 -07001602
Adam Langleyfcf25832014-12-18 17:42:32 -08001603 return hash;
1604}
Adam Langley95c29f32014-06-20 12:00:00 -07001605
1606/* NB: If this function (or indeed the hash function which uses a sort of
1607 * coarser function than this one) is changed, ensure
1608 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1609 * able to construct an SSL_SESSION that will collide with any existing session
1610 * with a matching session ID. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001611static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
1612 if (a->ssl_version != b->ssl_version) {
1613 return 1;
1614 }
Adam Langley95c29f32014-06-20 12:00:00 -07001615
Adam Langleyfcf25832014-12-18 17:42:32 -08001616 if (a->session_id_length != b->session_id_length) {
1617 return 1;
1618 }
Adam Langley95c29f32014-06-20 12:00:00 -07001619
Adam Langleyfcf25832014-12-18 17:42:32 -08001620 return memcmp(a->session_id, b->session_id, a->session_id_length);
1621}
Adam Langley95c29f32014-06-20 12:00:00 -07001622
David Benjamin4831c332015-05-16 11:43:13 -04001623SSL_CTX *SSL_CTX_new(const SSL_METHOD *method) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001624 SSL_CTX *ret = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001625
David Benjamin4831c332015-05-16 11:43:13 -04001626 if (method == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001627 OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_METHOD_PASSED);
Adam Langleyfcf25832014-12-18 17:42:32 -08001628 return NULL;
1629 }
Adam Langley95c29f32014-06-20 12:00:00 -07001630
Adam Langleyfcf25832014-12-18 17:42:32 -08001631 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001632 OPENSSL_PUT_ERROR(SSL, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
Adam Langleyfcf25832014-12-18 17:42:32 -08001633 goto err;
1634 }
Adam Langley95c29f32014-06-20 12:00:00 -07001635
Adam Langleyfcf25832014-12-18 17:42:32 -08001636 ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1637 if (ret == NULL) {
1638 goto err;
1639 }
Adam Langley95c29f32014-06-20 12:00:00 -07001640
Adam Langleyfcf25832014-12-18 17:42:32 -08001641 memset(ret, 0, sizeof(SSL_CTX));
Adam Langley95c29f32014-06-20 12:00:00 -07001642
David Benjamin4831c332015-05-16 11:43:13 -04001643 ret->method = method->method;
Adam Langley95c29f32014-06-20 12:00:00 -07001644
Adam Langley4bdb6e42015-05-15 15:29:21 -07001645 CRYPTO_MUTEX_init(&ret->lock);
1646
Adam Langleyfcf25832014-12-18 17:42:32 -08001647 ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
1648 ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
Adam Langley95c29f32014-06-20 12:00:00 -07001649
Adam Langleyfcf25832014-12-18 17:42:32 -08001650 /* We take the system default */
1651 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001652
Adam Langleyfcf25832014-12-18 17:42:32 -08001653 ret->references = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001654
Adam Langleyfcf25832014-12-18 17:42:32 -08001655 ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
Adam Langleyfcf25832014-12-18 17:42:32 -08001656 ret->verify_mode = SSL_VERIFY_NONE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001657 ret->cert = ssl_cert_new();
1658 if (ret->cert == NULL) {
1659 goto err;
1660 }
Adam Langley95c29f32014-06-20 12:00:00 -07001661
Adam Langleyfcf25832014-12-18 17:42:32 -08001662 ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
1663 if (ret->sessions == NULL) {
1664 goto err;
1665 }
1666 ret->cert_store = X509_STORE_new();
1667 if (ret->cert_store == NULL) {
1668 goto err;
1669 }
Adam Langley95c29f32014-06-20 12:00:00 -07001670
Adam Langleyfcf25832014-12-18 17:42:32 -08001671 ssl_create_cipher_list(ret->method, &ret->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001672 &ret->cipher_list_by_id, SSL_DEFAULT_CIPHER_LIST);
Adam Langleyfcf25832014-12-18 17:42:32 -08001673 if (ret->cipher_list == NULL ||
1674 sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001675 OPENSSL_PUT_ERROR(SSL, SSL_R_LIBRARY_HAS_NO_CIPHERS);
Adam Langleyfcf25832014-12-18 17:42:32 -08001676 goto err2;
1677 }
Adam Langley95c29f32014-06-20 12:00:00 -07001678
Adam Langleyfcf25832014-12-18 17:42:32 -08001679 ret->param = X509_VERIFY_PARAM_new();
1680 if (!ret->param) {
1681 goto err;
1682 }
Adam Langley95c29f32014-06-20 12:00:00 -07001683
Adam Langleyfcf25832014-12-18 17:42:32 -08001684 ret->client_CA = sk_X509_NAME_new_null();
1685 if (ret->client_CA == NULL) {
1686 goto err;
1687 }
Adam Langley95c29f32014-06-20 12:00:00 -07001688
David Benjamin9f33fc62015-04-15 17:29:53 -04001689 CRYPTO_new_ex_data(&g_ex_data_class_ssl_ctx, ret, &ret->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001690
Adam Langleyfcf25832014-12-18 17:42:32 -08001691 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
David Benjamin422d3a42014-08-20 11:09:03 -04001692
Adam Langleyfcf25832014-12-18 17:42:32 -08001693 /* Setup RFC4507 ticket keys */
1694 if (!RAND_bytes(ret->tlsext_tick_key_name, 16) ||
1695 !RAND_bytes(ret->tlsext_tick_hmac_key, 16) ||
1696 !RAND_bytes(ret->tlsext_tick_aes_key, 16)) {
1697 ret->options |= SSL_OP_NO_TICKET;
1698 }
Adam Langley95c29f32014-06-20 12:00:00 -07001699
Adam Langleyfcf25832014-12-18 17:42:32 -08001700 /* Default is to connect to non-RI servers. When RI is more widely deployed
1701 * might change this. */
1702 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
1703
1704 /* Lock the SSL_CTX to the specified version, for compatibility with legacy
1705 * uses of SSL_METHOD. */
David Benjamin4831c332015-05-16 11:43:13 -04001706 if (method->version != 0) {
1707 SSL_CTX_set_max_version(ret, method->version);
1708 SSL_CTX_set_min_version(ret, method->version);
Adam Langleyfcf25832014-12-18 17:42:32 -08001709 }
1710
1711 return ret;
1712
Adam Langley95c29f32014-06-20 12:00:00 -07001713err:
David Benjamin3570d732015-06-29 00:28:17 -04001714 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -07001715err2:
David Benjamin2755a3e2015-04-22 16:17:58 -04001716 SSL_CTX_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -08001717 return NULL;
1718}
Adam Langley95c29f32014-06-20 12:00:00 -07001719
David Benjamin4fcc2e22015-04-22 12:58:16 -04001720void SSL_CTX_free(SSL_CTX *ctx) {
1721 if (ctx == NULL ||
Adam Langley0b5e3902015-05-15 13:08:38 -07001722 !CRYPTO_refcount_dec_and_test_zero(&ctx->references)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001723 return;
1724 }
Adam Langley95c29f32014-06-20 12:00:00 -07001725
David Benjamin2755a3e2015-04-22 16:17:58 -04001726 X509_VERIFY_PARAM_free(ctx->param);
Adam Langley95c29f32014-06-20 12:00:00 -07001727
Adam Langleyfcf25832014-12-18 17:42:32 -08001728 /* Free internal session cache. However: the remove_cb() may reference the
1729 * ex_data of SSL_CTX, thus the ex_data store can only be removed after the
1730 * sessions were flushed. As the ex_data handling routines might also touch
1731 * the session cache, the most secure solution seems to be: empty (flush) the
1732 * cache, then free ex_data, then finally free the cache. (See ticket
1733 * [openssl.org #212].) */
David Benjamin2755a3e2015-04-22 16:17:58 -04001734 SSL_CTX_flush_sessions(ctx, 0);
Adam Langley95c29f32014-06-20 12:00:00 -07001735
David Benjamin4fcc2e22015-04-22 12:58:16 -04001736 CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, ctx, &ctx->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001737
Adam Langley4bdb6e42015-05-15 15:29:21 -07001738 CRYPTO_MUTEX_cleanup(&ctx->lock);
David Benjamin2755a3e2015-04-22 16:17:58 -04001739 lh_SSL_SESSION_free(ctx->sessions);
1740 X509_STORE_free(ctx->cert_store);
1741 ssl_cipher_preference_list_free(ctx->cipher_list);
1742 sk_SSL_CIPHER_free(ctx->cipher_list_by_id);
Adam Langley87489202015-09-03 15:52:29 -07001743 ssl_cipher_preference_list_free(ctx->cipher_list_tls10);
David Benjamin2755a3e2015-04-22 16:17:58 -04001744 ssl_cipher_preference_list_free(ctx->cipher_list_tls11);
1745 ssl_cert_free(ctx->cert);
Adam Langley09505632015-07-30 18:10:13 -07001746 sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->client_custom_extensions,
1747 SSL_CUSTOM_EXTENSION_free);
1748 sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->server_custom_extensions,
1749 SSL_CUSTOM_EXTENSION_free);
David Benjamin2755a3e2015-04-22 16:17:58 -04001750 sk_X509_NAME_pop_free(ctx->client_CA, X509_NAME_free);
David Benjamin2755a3e2015-04-22 16:17:58 -04001751 sk_SRTP_PROTECTION_PROFILE_free(ctx->srtp_profiles);
1752 OPENSSL_free(ctx->psk_identity_hint);
David Benjamin2755a3e2015-04-22 16:17:58 -04001753 OPENSSL_free(ctx->tlsext_ellipticcurvelist);
1754 OPENSSL_free(ctx->alpn_client_proto_list);
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001755 OPENSSL_free(ctx->ocsp_response);
Paul Lietar4fac72e2015-09-09 13:44:55 +01001756 OPENSSL_free(ctx->signed_cert_timestamp_list);
David Benjamin2755a3e2015-04-22 16:17:58 -04001757 EVP_PKEY_free(ctx->tlsext_channel_id_private);
1758 BIO_free(ctx->keylog_bio);
Adam Langley95c29f32014-06-20 12:00:00 -07001759
David Benjamin4fcc2e22015-04-22 12:58:16 -04001760 OPENSSL_free(ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08001761}
Adam Langley95c29f32014-06-20 12:00:00 -07001762
Adam Langleyfcf25832014-12-18 17:42:32 -08001763void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) {
1764 ctx->default_passwd_callback = cb;
1765}
Adam Langley95c29f32014-06-20 12:00:00 -07001766
Adam Langleyfcf25832014-12-18 17:42:32 -08001767void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u) {
1768 ctx->default_passwd_callback_userdata = u;
1769}
Adam Langley95c29f32014-06-20 12:00:00 -07001770
Adam Langleyfcf25832014-12-18 17:42:32 -08001771void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
1772 int (*cb)(X509_STORE_CTX *, void *),
1773 void *arg) {
1774 ctx->app_verify_callback = cb;
1775 ctx->app_verify_arg = arg;
1776}
Adam Langley95c29f32014-06-20 12:00:00 -07001777
Adam Langleyfcf25832014-12-18 17:42:32 -08001778void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
1779 int (*cb)(int, X509_STORE_CTX *)) {
1780 ctx->verify_mode = mode;
1781 ctx->default_verify_callback = cb;
1782}
Adam Langley95c29f32014-06-20 12:00:00 -07001783
Adam Langleyfcf25832014-12-18 17:42:32 -08001784void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
1785 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
1786}
Adam Langley1258b6a2014-06-20 12:00:00 -07001787
David Benjamin7481d392015-07-05 19:38:46 -04001788void SSL_CTX_set_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl, void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08001789 void *arg) {
David Benjamin7481d392015-07-05 19:38:46 -04001790 ssl_cert_set_cert_cb(ctx->cert, cb, arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08001791}
David Benjamin859ec3c2014-09-02 16:29:36 -04001792
David Benjamin7481d392015-07-05 19:38:46 -04001793void SSL_set_cert_cb(SSL *ssl, int (*cb)(SSL *ssl, void *arg), void *arg) {
1794 ssl_cert_set_cert_cb(ssl->cert, cb, arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08001795}
Adam Langley95c29f32014-06-20 12:00:00 -07001796
David Benjamin107db582015-04-08 00:41:59 -04001797void ssl_get_compatible_server_ciphers(SSL *s, uint32_t *out_mask_k,
1798 uint32_t *out_mask_a) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001799 CERT *c = s->cert;
David Benjamind1d80782015-07-05 11:54:09 -04001800 int have_rsa_cert = 0, dh_tmp;
David Benjamin107db582015-04-08 00:41:59 -04001801 uint32_t mask_k, mask_a;
David Benjamind1d80782015-07-05 11:54:09 -04001802 int have_ecc_cert = 0, ecdsa_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001803 X509 *x;
Adam Langley95c29f32014-06-20 12:00:00 -07001804
Adam Langleyfcf25832014-12-18 17:42:32 -08001805 dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001806
nagendra modadugu601448a2015-07-24 09:31:31 -07001807 if (s->cert->x509 != NULL && ssl_has_private_key(s)) {
1808 if (ssl_private_key_type(s) == EVP_PKEY_RSA) {
David Benjamind1d80782015-07-05 11:54:09 -04001809 have_rsa_cert = 1;
nagendra modadugu601448a2015-07-24 09:31:31 -07001810 } else if (ssl_private_key_type(s) == EVP_PKEY_EC) {
David Benjamind1d80782015-07-05 11:54:09 -04001811 have_ecc_cert = 1;
1812 }
1813 }
nagendra modadugu601448a2015-07-24 09:31:31 -07001814
Adam Langleyfcf25832014-12-18 17:42:32 -08001815 mask_k = 0;
1816 mask_a = 0;
David Benjaminf31e6812014-11-13 18:05:55 -05001817
Adam Langleyfcf25832014-12-18 17:42:32 -08001818 if (dh_tmp) {
David Benjamin7061e282015-03-19 11:10:48 -04001819 mask_k |= SSL_kDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001820 }
David Benjaminbb20f522015-07-04 17:18:14 -04001821 if (have_rsa_cert) {
1822 mask_k |= SSL_kRSA;
Adam Langleyfcf25832014-12-18 17:42:32 -08001823 mask_a |= SSL_aRSA;
1824 }
Adam Langley95c29f32014-06-20 12:00:00 -07001825
Adam Langleyfcf25832014-12-18 17:42:32 -08001826 /* An ECC certificate may be usable for ECDSA cipher suites depending on the
1827 * key usage extension and on the client's curve preferences. */
1828 if (have_ecc_cert) {
David Benjamind1d80782015-07-05 11:54:09 -04001829 x = c->x509;
Adam Langleyfcf25832014-12-18 17:42:32 -08001830 /* This call populates extension flags (ex_flags). */
1831 X509_check_purpose(x, -1, 0);
1832 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE)
1833 ? (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)
1834 : 1;
1835 if (!tls1_check_ec_cert(s, x)) {
1836 ecdsa_ok = 0;
1837 }
1838 if (ecdsa_ok) {
1839 mask_a |= SSL_aECDSA;
1840 }
1841 }
Adam Langley95c29f32014-06-20 12:00:00 -07001842
Adam Langleyfcf25832014-12-18 17:42:32 -08001843 /* If we are considering an ECC cipher suite that uses an ephemeral EC
1844 * key, check it. */
David Benjamindd978782015-04-24 15:20:13 -04001845 if (tls1_check_ec_tmp_key(s)) {
David Benjamin7061e282015-03-19 11:10:48 -04001846 mask_k |= SSL_kECDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001847 }
Adam Langley95c29f32014-06-20 12:00:00 -07001848
Adam Langleyfcf25832014-12-18 17:42:32 -08001849 /* PSK requires a server callback. */
1850 if (s->psk_server_callback != NULL) {
1851 mask_k |= SSL_kPSK;
1852 mask_a |= SSL_aPSK;
1853 }
Adam Langley95c29f32014-06-20 12:00:00 -07001854
Adam Langleyfcf25832014-12-18 17:42:32 -08001855 *out_mask_k = mask_k;
1856 *out_mask_a = mask_a;
1857}
Adam Langley95c29f32014-06-20 12:00:00 -07001858
Adam Langleyfcf25832014-12-18 17:42:32 -08001859void ssl_update_cache(SSL *s, int mode) {
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001860 /* Never cache sessions with empty session IDs. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001861 if (s->session->session_id_length == 0) {
1862 return;
1863 }
Adam Langley95c29f32014-06-20 12:00:00 -07001864
David Benjamin95d31822015-06-15 19:53:32 -04001865 int has_new_session = !s->hit;
1866 if (!s->server && s->tlsext_ticket_expected) {
1867 /* A client may see new sessions on abbreviated handshakes if the server
1868 * decides to renew the ticket. Once the handshake is completed, it should
1869 * be inserted into the cache. */
1870 has_new_session = 1;
1871 }
1872
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001873 SSL_CTX *ctx = s->initial_ctx;
David Benjamin95d31822015-06-15 19:53:32 -04001874 if ((ctx->session_cache_mode & mode) == mode && has_new_session &&
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001875 ((ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) ||
1876 SSL_CTX_add_session(ctx, s->session)) &&
1877 ctx->new_session_cb != NULL) {
1878 /* Note: |new_session_cb| is called whether the internal session cache is
1879 * used or not. */
1880 if (!ctx->new_session_cb(s, SSL_SESSION_up_ref(s->session))) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001881 SSL_SESSION_free(s->session);
1882 }
1883 }
Adam Langley95c29f32014-06-20 12:00:00 -07001884
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001885 if (!(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR) &&
1886 !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) &&
1887 (ctx->session_cache_mode & mode) == mode) {
1888 /* Automatically flush the internal session cache every 255 connections. */
1889 int flush_cache = 0;
Adam Langley4bdb6e42015-05-15 15:29:21 -07001890 CRYPTO_MUTEX_lock_write(&ctx->lock);
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001891 ctx->handshakes_since_cache_flush++;
1892 if (ctx->handshakes_since_cache_flush >= 255) {
1893 flush_cache = 1;
1894 ctx->handshakes_since_cache_flush = 0;
1895 }
Adam Langley4bdb6e42015-05-15 15:29:21 -07001896 CRYPTO_MUTEX_unlock(&ctx->lock);
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001897
1898 if (flush_cache) {
1899 SSL_CTX_flush_sessions(ctx, (unsigned long)time(NULL));
Adam Langleyfcf25832014-12-18 17:42:32 -08001900 }
1901 }
1902}
Adam Langley95c29f32014-06-20 12:00:00 -07001903
David Benjamin1a5c50f2015-03-11 16:22:37 -04001904int SSL_get_error(const SSL *s, int ret_code) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001905 int reason;
David Benjamin1a5c50f2015-03-11 16:22:37 -04001906 uint32_t err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001907 BIO *bio;
Adam Langley95c29f32014-06-20 12:00:00 -07001908
David Benjamin1a5c50f2015-03-11 16:22:37 -04001909 if (ret_code > 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001910 return SSL_ERROR_NONE;
1911 }
Adam Langley95c29f32014-06-20 12:00:00 -07001912
Adam Langleyfcf25832014-12-18 17:42:32 -08001913 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
1914 * where we do encode the error */
David Benjamin1a5c50f2015-03-11 16:22:37 -04001915 err = ERR_peek_error();
1916 if (err != 0) {
1917 if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001918 return SSL_ERROR_SYSCALL;
1919 }
1920 return SSL_ERROR_SSL;
1921 }
Adam Langley95c29f32014-06-20 12:00:00 -07001922
David Benjamin1a5c50f2015-03-11 16:22:37 -04001923 if (ret_code == 0) {
David Benjamin9a38e922015-01-22 16:06:11 -05001924 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
1925 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) {
1926 /* The socket was cleanly shut down with a close_notify. */
1927 return SSL_ERROR_ZERO_RETURN;
1928 }
1929 /* An EOF was observed which violates the protocol, and the underlying
1930 * transport does not participate in the error queue. Bubble up to the
1931 * caller. */
1932 return SSL_ERROR_SYSCALL;
Adam Langleyfcf25832014-12-18 17:42:32 -08001933 }
Adam Langleyb2ce0582014-06-20 12:00:00 -07001934
Adam Langleyfcf25832014-12-18 17:42:32 -08001935 if (SSL_want_session(s)) {
1936 return SSL_ERROR_PENDING_SESSION;
1937 }
Adam Langley95c29f32014-06-20 12:00:00 -07001938
Adam Langleyfcf25832014-12-18 17:42:32 -08001939 if (SSL_want_certificate(s)) {
1940 return SSL_ERROR_PENDING_CERTIFICATE;
1941 }
Adam Langley95c29f32014-06-20 12:00:00 -07001942
Adam Langleyfcf25832014-12-18 17:42:32 -08001943 if (SSL_want_read(s)) {
1944 bio = SSL_get_rbio(s);
1945 if (BIO_should_read(bio)) {
1946 return SSL_ERROR_WANT_READ;
1947 }
Adam Langley95c29f32014-06-20 12:00:00 -07001948
Adam Langleyfcf25832014-12-18 17:42:32 -08001949 if (BIO_should_write(bio)) {
1950 /* This one doesn't make too much sense ... We never try to write to the
1951 * rbio, and an application program where rbio and wbio are separate
1952 * couldn't even know what it should wait for. However if we ever set
1953 * s->rwstate incorrectly (so that we have SSL_want_read(s) instead of
1954 * SSL_want_write(s)) and rbio and wbio *are* the same, this test works
1955 * around that bug; so it might be safer to keep it. */
1956 return SSL_ERROR_WANT_WRITE;
1957 }
Adam Langley95c29f32014-06-20 12:00:00 -07001958
Adam Langleyfcf25832014-12-18 17:42:32 -08001959 if (BIO_should_io_special(bio)) {
1960 reason = BIO_get_retry_reason(bio);
1961 if (reason == BIO_RR_CONNECT) {
1962 return SSL_ERROR_WANT_CONNECT;
1963 }
Adam Langley95c29f32014-06-20 12:00:00 -07001964
Adam Langleyfcf25832014-12-18 17:42:32 -08001965 if (reason == BIO_RR_ACCEPT) {
1966 return SSL_ERROR_WANT_ACCEPT;
1967 }
Adam Langley95c29f32014-06-20 12:00:00 -07001968
Adam Langleyfcf25832014-12-18 17:42:32 -08001969 return SSL_ERROR_SYSCALL; /* unknown */
1970 }
1971 }
Adam Langley95c29f32014-06-20 12:00:00 -07001972
Adam Langleyfcf25832014-12-18 17:42:32 -08001973 if (SSL_want_write(s)) {
1974 bio = SSL_get_wbio(s);
1975 if (BIO_should_write(bio)) {
1976 return SSL_ERROR_WANT_WRITE;
1977 }
Adam Langley95c29f32014-06-20 12:00:00 -07001978
Adam Langleyfcf25832014-12-18 17:42:32 -08001979 if (BIO_should_read(bio)) {
1980 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
1981 return SSL_ERROR_WANT_READ;
1982 }
Adam Langley95c29f32014-06-20 12:00:00 -07001983
Adam Langleyfcf25832014-12-18 17:42:32 -08001984 if (BIO_should_io_special(bio)) {
1985 reason = BIO_get_retry_reason(bio);
1986 if (reason == BIO_RR_CONNECT) {
1987 return SSL_ERROR_WANT_CONNECT;
1988 }
Adam Langley95c29f32014-06-20 12:00:00 -07001989
Adam Langleyfcf25832014-12-18 17:42:32 -08001990 if (reason == BIO_RR_ACCEPT) {
1991 return SSL_ERROR_WANT_ACCEPT;
1992 }
Adam Langley95c29f32014-06-20 12:00:00 -07001993
Adam Langleyfcf25832014-12-18 17:42:32 -08001994 return SSL_ERROR_SYSCALL;
1995 }
1996 }
Adam Langley95c29f32014-06-20 12:00:00 -07001997
Adam Langleyfcf25832014-12-18 17:42:32 -08001998 if (SSL_want_x509_lookup(s)) {
1999 return SSL_ERROR_WANT_X509_LOOKUP;
2000 }
Adam Langley95c29f32014-06-20 12:00:00 -07002001
Adam Langleyfcf25832014-12-18 17:42:32 -08002002 if (SSL_want_channel_id_lookup(s)) {
2003 return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
2004 }
Adam Langley0f4746e2014-08-13 12:26:32 -07002005
David Benjaminb4d65fd2015-05-29 17:11:21 -04002006 if (SSL_want_private_key_operation(s)) {
2007 return SSL_ERROR_WANT_PRIVATE_KEY_OPERATION;
2008 }
2009
Adam Langleyfcf25832014-12-18 17:42:32 -08002010 return SSL_ERROR_SYSCALL;
2011}
Adam Langley0f4746e2014-08-13 12:26:32 -07002012
Adam Langleyfcf25832014-12-18 17:42:32 -08002013int SSL_do_handshake(SSL *s) {
2014 int ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002015
Adam Langleyfcf25832014-12-18 17:42:32 -08002016 if (s->handshake_func == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04002017 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_TYPE_NOT_SET);
Adam Langleyfcf25832014-12-18 17:42:32 -08002018 return -1;
2019 }
Adam Langley95c29f32014-06-20 12:00:00 -07002020
Adam Langleyfcf25832014-12-18 17:42:32 -08002021 if (SSL_in_init(s)) {
2022 ret = s->handshake_func(s);
2023 }
2024 return ret;
2025}
Adam Langley95c29f32014-06-20 12:00:00 -07002026
David Benjamin4831c332015-05-16 11:43:13 -04002027void SSL_set_accept_state(SSL *ssl) {
2028 ssl->server = 1;
2029 ssl->shutdown = 0;
David Benjamin8ec88102015-05-15 23:40:13 -04002030 ssl->state = SSL_ST_ACCEPT;
David Benjamin4831c332015-05-16 11:43:13 -04002031 ssl->handshake_func = ssl->method->ssl_accept;
Adam Langleyfcf25832014-12-18 17:42:32 -08002032 /* clear the current cipher */
David Benjamin4831c332015-05-16 11:43:13 -04002033 ssl_clear_cipher_ctx(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08002034}
Adam Langley95c29f32014-06-20 12:00:00 -07002035
David Benjamin4831c332015-05-16 11:43:13 -04002036void SSL_set_connect_state(SSL *ssl) {
2037 ssl->server = 0;
2038 ssl->shutdown = 0;
David Benjamin8ec88102015-05-15 23:40:13 -04002039 ssl->state = SSL_ST_CONNECT;
David Benjamin4831c332015-05-16 11:43:13 -04002040 ssl->handshake_func = ssl->method->ssl_connect;
Adam Langleyfcf25832014-12-18 17:42:32 -08002041 /* clear the current cipher */
David Benjamin4831c332015-05-16 11:43:13 -04002042 ssl_clear_cipher_ctx(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08002043}
Adam Langley95c29f32014-06-20 12:00:00 -07002044
Adam Langleyfcf25832014-12-18 17:42:32 -08002045static const char *ssl_get_version(int version) {
2046 switch (version) {
2047 case TLS1_2_VERSION:
2048 return "TLSv1.2";
Adam Langley95c29f32014-06-20 12:00:00 -07002049
Adam Langleyfcf25832014-12-18 17:42:32 -08002050 case TLS1_1_VERSION:
2051 return "TLSv1.1";
Adam Langley95c29f32014-06-20 12:00:00 -07002052
Adam Langleyfcf25832014-12-18 17:42:32 -08002053 case TLS1_VERSION:
2054 return "TLSv1";
Adam Langley95c29f32014-06-20 12:00:00 -07002055
Adam Langleyfcf25832014-12-18 17:42:32 -08002056 case SSL3_VERSION:
2057 return "SSLv3";
Adam Langley95c29f32014-06-20 12:00:00 -07002058
David Benjamin1c722b72015-04-20 13:53:10 -04002059 case DTLS1_VERSION:
2060 return "DTLSv1";
2061
2062 case DTLS1_2_VERSION:
2063 return "DTLSv1.2";
2064
Adam Langleyfcf25832014-12-18 17:42:32 -08002065 default:
2066 return "unknown";
2067 }
2068}
Adam Langley95c29f32014-06-20 12:00:00 -07002069
Adam Langleyfcf25832014-12-18 17:42:32 -08002070const char *SSL_get_version(const SSL *s) {
2071 return ssl_get_version(s->version);
2072}
Adam Langley95c29f32014-06-20 12:00:00 -07002073
Adam Langleyfcf25832014-12-18 17:42:32 -08002074const char *SSL_SESSION_get_version(const SSL_SESSION *sess) {
2075 return ssl_get_version(sess->ssl_version);
2076}
Adam Langley95c29f32014-06-20 12:00:00 -07002077
Sigbjorn Vik2b23d242015-06-29 15:07:26 +02002078const char* SSL_get_curve_name(uint16_t curve_id) {
2079 return tls1_ec_curve_id2name(curve_id);
2080}
2081
Adam Langleyfcf25832014-12-18 17:42:32 -08002082void ssl_clear_cipher_ctx(SSL *s) {
David Benjamin31a07792015-03-03 14:20:26 -05002083 SSL_AEAD_CTX_free(s->aead_read_ctx);
2084 s->aead_read_ctx = NULL;
2085 SSL_AEAD_CTX_free(s->aead_write_ctx);
2086 s->aead_write_ctx = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002087}
Adam Langley95c29f32014-06-20 12:00:00 -07002088
Adam Langleyfcf25832014-12-18 17:42:32 -08002089X509 *SSL_get_certificate(const SSL *s) {
2090 if (s->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04002091 return s->cert->x509;
Adam Langleyfcf25832014-12-18 17:42:32 -08002092 }
2093
2094 return NULL;
2095}
2096
2097EVP_PKEY *SSL_get_privatekey(const SSL *s) {
2098 if (s->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04002099 return s->cert->privatekey;
Adam Langleyfcf25832014-12-18 17:42:32 -08002100 }
2101
2102 return NULL;
2103}
2104
2105X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
2106 if (ctx->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04002107 return ctx->cert->x509;
Adam Langleyfcf25832014-12-18 17:42:32 -08002108 }
2109
2110 return NULL;
2111}
2112
2113EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
2114 if (ctx->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04002115 return ctx->cert->privatekey;
Adam Langleyfcf25832014-12-18 17:42:32 -08002116 }
2117
2118 return NULL;
2119}
2120
2121const SSL_CIPHER *SSL_get_current_cipher(const SSL *s) {
David Benjamina07c0fc2015-05-13 13:19:42 -04002122 if (s->aead_write_ctx == NULL) {
2123 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002124 }
David Benjamina07c0fc2015-05-13 13:19:42 -04002125 return s->aead_write_ctx->cipher;
Adam Langleyfcf25832014-12-18 17:42:32 -08002126}
2127
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07002128const COMP_METHOD *SSL_get_current_compression(SSL *s) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002129
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07002130const COMP_METHOD *SSL_get_current_expansion(SSL *s) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002131
2132int ssl_init_wbio_buffer(SSL *s, int push) {
2133 BIO *bbio;
2134
2135 if (s->bbio == NULL) {
2136 bbio = BIO_new(BIO_f_buffer());
2137 if (bbio == NULL) {
2138 return 0;
2139 }
2140 s->bbio = bbio;
2141 } else {
2142 bbio = s->bbio;
2143 if (s->bbio == s->wbio) {
2144 s->wbio = BIO_pop(s->wbio);
2145 }
2146 }
2147
2148 BIO_reset(bbio);
2149 if (!BIO_set_read_buffer_size(bbio, 1)) {
David Benjamin3570d732015-06-29 00:28:17 -04002150 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -08002151 return 0;
2152 }
2153
2154 if (push) {
2155 if (s->wbio != bbio) {
2156 s->wbio = BIO_push(bbio, s->wbio);
2157 }
2158 } else {
2159 if (s->wbio == bbio) {
2160 s->wbio = BIO_pop(bbio);
2161 }
2162 }
2163
2164 return 1;
2165}
2166
2167void ssl_free_wbio_buffer(SSL *s) {
2168 if (s->bbio == NULL) {
2169 return;
2170 }
2171
2172 if (s->bbio == s->wbio) {
2173 /* remove buffering */
2174 s->wbio = BIO_pop(s->wbio);
2175 }
2176
2177 BIO_free(s->bbio);
2178 s->bbio = NULL;
2179}
2180
2181void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
2182 ctx->quiet_shutdown = mode;
2183}
2184
2185int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
2186 return ctx->quiet_shutdown;
2187}
2188
2189void SSL_set_quiet_shutdown(SSL *s, int mode) { s->quiet_shutdown = mode; }
2190
2191int SSL_get_quiet_shutdown(const SSL *s) { return s->quiet_shutdown; }
2192
2193void SSL_set_shutdown(SSL *s, int mode) { s->shutdown = mode; }
2194
2195int SSL_get_shutdown(const SSL *s) { return s->shutdown; }
2196
2197int SSL_version(const SSL *s) { return s->version; }
2198
2199SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
2200
2201SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
2202 if (ssl->ctx == ctx) {
2203 return ssl->ctx;
2204 }
2205
2206 if (ctx == NULL) {
2207 ctx = ssl->initial_ctx;
2208 }
2209
David Benjamin2755a3e2015-04-22 16:17:58 -04002210 ssl_cert_free(ssl->cert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002211 ssl->cert = ssl_cert_dup(ctx->cert);
David Benjamin2755a3e2015-04-22 16:17:58 -04002212
Adam Langley0b5e3902015-05-15 13:08:38 -07002213 CRYPTO_refcount_inc(&ctx->references);
David Benjamin2755a3e2015-04-22 16:17:58 -04002214 SSL_CTX_free(ssl->ctx); /* decrement reference count */
Adam Langleyfcf25832014-12-18 17:42:32 -08002215 ssl->ctx = ctx;
2216
2217 ssl->sid_ctx_length = ctx->sid_ctx_length;
2218 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2219 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2220
2221 return ssl->ctx;
2222}
2223
2224int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
2225 return X509_STORE_set_default_paths(ctx->cert_store);
2226}
Adam Langley95c29f32014-06-20 12:00:00 -07002227
2228int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
Adam Langleyfcf25832014-12-18 17:42:32 -08002229 const char *CApath) {
2230 return X509_STORE_load_locations(ctx->cert_store, CAfile, CApath);
2231}
Adam Langley95c29f32014-06-20 12:00:00 -07002232
2233void SSL_set_info_callback(SSL *ssl,
Adam Langleyfcf25832014-12-18 17:42:32 -08002234 void (*cb)(const SSL *ssl, int type, int val)) {
2235 ssl->info_callback = cb;
2236}
Adam Langley95c29f32014-06-20 12:00:00 -07002237
Adam Langleyfcf25832014-12-18 17:42:32 -08002238void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/, int /*type*/,
2239 int /*val*/) {
2240 return ssl->info_callback;
2241}
Adam Langley95c29f32014-06-20 12:00:00 -07002242
Adam Langleyfcf25832014-12-18 17:42:32 -08002243int SSL_state(const SSL *ssl) { return ssl->state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002244
David Benjaminece089c2015-05-15 23:52:42 -04002245void SSL_set_state(SSL *ssl, int state) { }
Adam Langley95c29f32014-06-20 12:00:00 -07002246
Adam Langleyfcf25832014-12-18 17:42:32 -08002247void SSL_set_verify_result(SSL *ssl, long arg) { ssl->verify_result = arg; }
Adam Langley95c29f32014-06-20 12:00:00 -07002248
Adam Langleyfcf25832014-12-18 17:42:32 -08002249long SSL_get_verify_result(const SSL *ssl) { return ssl->verify_result; }
Adam Langley95c29f32014-06-20 12:00:00 -07002250
Adam Langleyfcf25832014-12-18 17:42:32 -08002251int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2252 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002253 int index;
2254 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp,
2255 new_func, dup_func, free_func)) {
2256 return -1;
2257 }
2258 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002259}
Adam Langley95c29f32014-06-20 12:00:00 -07002260
Adam Langleyfcf25832014-12-18 17:42:32 -08002261int SSL_set_ex_data(SSL *s, int idx, void *arg) {
2262 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2263}
Adam Langley95c29f32014-06-20 12:00:00 -07002264
Adam Langleyfcf25832014-12-18 17:42:32 -08002265void *SSL_get_ex_data(const SSL *s, int idx) {
2266 return CRYPTO_get_ex_data(&s->ex_data, idx);
2267}
Adam Langley95c29f32014-06-20 12:00:00 -07002268
Adam Langleyfcf25832014-12-18 17:42:32 -08002269int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2270 CRYPTO_EX_dup *dup_func,
2271 CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002272 int index;
2273 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp,
2274 new_func, dup_func, free_func)) {
2275 return -1;
2276 }
2277 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002278}
Adam Langley95c29f32014-06-20 12:00:00 -07002279
Adam Langleyfcf25832014-12-18 17:42:32 -08002280int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg) {
2281 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2282}
Adam Langley95c29f32014-06-20 12:00:00 -07002283
Adam Langleyfcf25832014-12-18 17:42:32 -08002284void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx) {
2285 return CRYPTO_get_ex_data(&s->ex_data, idx);
2286}
Adam Langley95c29f32014-06-20 12:00:00 -07002287
Adam Langleyfcf25832014-12-18 17:42:32 -08002288X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
2289 return ctx->cert_store;
2290}
Adam Langley95c29f32014-06-20 12:00:00 -07002291
Adam Langleyfcf25832014-12-18 17:42:32 -08002292void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
David Benjamin2755a3e2015-04-22 16:17:58 -04002293 X509_STORE_free(ctx->cert_store);
Adam Langleyfcf25832014-12-18 17:42:32 -08002294 ctx->cert_store = store;
2295}
Adam Langley95c29f32014-06-20 12:00:00 -07002296
Adam Langleyfcf25832014-12-18 17:42:32 -08002297int SSL_want(const SSL *s) { return s->rwstate; }
Adam Langley95c29f32014-06-20 12:00:00 -07002298
Adam Langleyfcf25832014-12-18 17:42:32 -08002299void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
2300 RSA *(*cb)(SSL *ssl, int is_export,
2301 int keylength)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002302}
Adam Langley95c29f32014-06-20 12:00:00 -07002303
Adam Langleyfcf25832014-12-18 17:42:32 -08002304void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
2305 int keylength)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002306}
2307
2308void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
David Benjamin59015c32015-04-26 13:13:55 -04002309 DH *(*callback)(SSL *ssl, int is_export,
2310 int keylength)) {
2311 ctx->cert->dh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002312}
2313
David Benjamin59015c32015-04-26 13:13:55 -04002314void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*callback)(SSL *ssl, int is_export,
2315 int keylength)) {
2316 ssl->cert->dh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002317}
2318
2319void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,
David Benjamindd978782015-04-24 15:20:13 -04002320 EC_KEY *(*callback)(SSL *ssl, int is_export,
2321 int keylength)) {
David Benjamin59015c32015-04-26 13:13:55 -04002322 ctx->cert->ecdh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002323}
2324
2325void SSL_set_tmp_ecdh_callback(SSL *ssl,
David Benjamindd978782015-04-24 15:20:13 -04002326 EC_KEY *(*callback)(SSL *ssl, int is_export,
2327 int keylength)) {
David Benjamin59015c32015-04-26 13:13:55 -04002328 ssl->cert->ecdh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002329}
2330
2331int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
2332 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
David Benjamin3570d732015-06-29 00:28:17 -04002333 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -08002334 return 0;
2335 }
2336
David Benjamin2755a3e2015-04-22 16:17:58 -04002337 OPENSSL_free(ctx->psk_identity_hint);
Adam Langleyfcf25832014-12-18 17:42:32 -08002338
2339 if (identity_hint != NULL) {
2340 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2341 if (ctx->psk_identity_hint == NULL) {
2342 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002343 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002344 } else {
2345 ctx->psk_identity_hint = NULL;
2346 }
Adam Langley95c29f32014-06-20 12:00:00 -07002347
Adam Langleyfcf25832014-12-18 17:42:32 -08002348 return 1;
2349}
2350
2351int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint) {
2352 if (s == NULL) {
2353 return 0;
2354 }
2355
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
2361 /* Clear currently configured hint, if any. */
David Benjamin2755a3e2015-04-22 16:17:58 -04002362 OPENSSL_free(s->psk_identity_hint);
2363 s->psk_identity_hint = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002364
2365 if (identity_hint != NULL) {
2366 s->psk_identity_hint = BUF_strdup(identity_hint);
2367 if (s->psk_identity_hint == NULL) {
2368 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002369 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002370 }
Adam Langley95c29f32014-06-20 12:00:00 -07002371
Adam Langleyfcf25832014-12-18 17:42:32 -08002372 return 1;
2373}
Adam Langley95c29f32014-06-20 12:00:00 -07002374
Adam Langleyfcf25832014-12-18 17:42:32 -08002375const char *SSL_get_psk_identity_hint(const SSL *s) {
2376 if (s == NULL) {
2377 return NULL;
2378 }
2379 return s->psk_identity_hint;
2380}
Adam Langley95c29f32014-06-20 12:00:00 -07002381
Adam Langleyfcf25832014-12-18 17:42:32 -08002382const char *SSL_get_psk_identity(const SSL *s) {
2383 if (s == NULL || s->session == NULL) {
2384 return NULL;
2385 }
Adam Langley95c29f32014-06-20 12:00:00 -07002386
Adam Langleyfcf25832014-12-18 17:42:32 -08002387 return s->session->psk_identity;
2388}
Adam Langley95c29f32014-06-20 12:00:00 -07002389
Adam Langleyfcf25832014-12-18 17:42:32 -08002390void SSL_set_psk_client_callback(
2391 SSL *s, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2392 unsigned int max_identity_len, uint8_t *psk,
2393 unsigned int max_psk_len)) {
2394 s->psk_client_callback = cb;
2395}
Adam Langley95c29f32014-06-20 12:00:00 -07002396
Adam Langleyfcf25832014-12-18 17:42:32 -08002397void SSL_CTX_set_psk_client_callback(
2398 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2399 unsigned int max_identity_len,
2400 uint8_t *psk, unsigned int max_psk_len)) {
2401 ctx->psk_client_callback = cb;
2402}
Adam Langley95c29f32014-06-20 12:00:00 -07002403
Adam Langleyfcf25832014-12-18 17:42:32 -08002404void SSL_set_psk_server_callback(
2405 SSL *s, unsigned int (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
2406 unsigned int max_psk_len)) {
2407 s->psk_server_callback = cb;
2408}
Adam Langley95c29f32014-06-20 12:00:00 -07002409
Adam Langleyfcf25832014-12-18 17:42:32 -08002410void SSL_CTX_set_psk_server_callback(
2411 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *identity,
2412 uint8_t *psk, unsigned int max_psk_len)) {
2413 ctx->psk_server_callback = cb;
2414}
Adam Langley95c29f32014-06-20 12:00:00 -07002415
Adam Langleyfcf25832014-12-18 17:42:32 -08002416void SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) {
2417 ctx->min_version = version;
2418}
Adam Langley95c29f32014-06-20 12:00:00 -07002419
Adam Langleyfcf25832014-12-18 17:42:32 -08002420void SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) {
2421 ctx->max_version = version;
2422}
Adam Langley0289c732014-06-20 12:00:00 -07002423
Adam Langleyfcf25832014-12-18 17:42:32 -08002424void SSL_set_min_version(SSL *ssl, uint16_t version) {
2425 ssl->min_version = version;
2426}
Adam Langley0289c732014-06-20 12:00:00 -07002427
Adam Langleyfcf25832014-12-18 17:42:32 -08002428void SSL_set_max_version(SSL *ssl, uint16_t version) {
2429 ssl->max_version = version;
2430}
Adam Langley95c29f32014-06-20 12:00:00 -07002431
Adam Langleyfcf25832014-12-18 17:42:32 -08002432void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
2433 void (*cb)(int write_p, int version,
2434 int content_type, const void *buf,
2435 size_t len, SSL *ssl, void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002436 ctx->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002437}
David Benjamin61ecccf2015-05-05 09:44:51 -04002438
2439void SSL_CTX_set_msg_callback_arg(SSL_CTX *ctx, void *arg) {
2440 ctx->msg_callback_arg = arg;
2441}
2442
Adam Langleyfcf25832014-12-18 17:42:32 -08002443void SSL_set_msg_callback(SSL *ssl,
2444 void (*cb)(int write_p, int version, int content_type,
2445 const void *buf, size_t len, SSL *ssl,
2446 void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002447 ssl->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002448}
Adam Langley95c29f32014-06-20 12:00:00 -07002449
David Benjamin61ecccf2015-05-05 09:44:51 -04002450void SSL_set_msg_callback_arg(SSL *ssl, void *arg) {
2451 ssl->msg_callback_arg = arg;
2452}
2453
Adam Langleyfcf25832014-12-18 17:42:32 -08002454void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio) {
David Benjamin2755a3e2015-04-22 16:17:58 -04002455 BIO_free(ctx->keylog_bio);
Adam Langleyfcf25832014-12-18 17:42:32 -08002456 ctx->keylog_bio = keylog_bio;
2457}
Adam Langley95c29f32014-06-20 12:00:00 -07002458
Adam Langleyfcf25832014-12-18 17:42:32 -08002459static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
2460 static const char hextable[] = "0123456789abcdef";
2461 uint8_t *out;
2462 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002463
Adam Langleyfcf25832014-12-18 17:42:32 -08002464 if (!CBB_add_space(cbb, &out, in_len * 2)) {
2465 return 0;
2466 }
Adam Langley95c29f32014-06-20 12:00:00 -07002467
Adam Langleyfcf25832014-12-18 17:42:32 -08002468 for (i = 0; i < in_len; i++) {
2469 *(out++) = (uint8_t)hextable[in[i] >> 4];
2470 *(out++) = (uint8_t)hextable[in[i] & 0xf];
2471 }
Adam Langley95c29f32014-06-20 12:00:00 -07002472
Adam Langleyfcf25832014-12-18 17:42:32 -08002473 return 1;
2474}
David Benjamin859ec3c2014-09-02 16:29:36 -04002475
2476int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -08002477 const uint8_t *encrypted_premaster,
2478 size_t encrypted_premaster_len,
2479 const uint8_t *premaster,
2480 size_t premaster_len) {
2481 BIO *bio = ctx->keylog_bio;
2482 CBB cbb;
2483 uint8_t *out;
2484 size_t out_len;
2485 int ret;
David Benjamin859ec3c2014-09-02 16:29:36 -04002486
Adam Langleyfcf25832014-12-18 17:42:32 -08002487 if (bio == NULL) {
2488 return 1;
2489 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002490
Adam Langleyfcf25832014-12-18 17:42:32 -08002491 if (encrypted_premaster_len < 8) {
David Benjamin3570d732015-06-29 00:28:17 -04002492 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08002493 return 0;
2494 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002495
David Benjamina8653202015-06-28 01:26:10 -04002496 CBB_zero(&cbb);
2497 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len * 2 + 1) ||
2498 !CBB_add_bytes(&cbb, (const uint8_t *)"RSA ", 4) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08002499 /* Only the first 8 bytes of the encrypted premaster secret are
2500 * logged. */
2501 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
2502 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2503 !cbb_add_hex(&cbb, premaster, premaster_len) ||
2504 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2505 !CBB_finish(&cbb, &out, &out_len)) {
2506 CBB_cleanup(&cbb);
2507 return 0;
2508 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002509
Adam Langley4bdb6e42015-05-15 15:29:21 -07002510 CRYPTO_MUTEX_lock_write(&ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -08002511 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
Adam Langley4bdb6e42015-05-15 15:29:21 -07002512 CRYPTO_MUTEX_unlock(&ctx->lock);
David Benjamin859ec3c2014-09-02 16:29:36 -04002513
Adam Langleyfcf25832014-12-18 17:42:32 -08002514 OPENSSL_free(out);
2515 return ret;
Adam Langley95f22882014-06-20 12:00:00 -07002516}
2517
Adam Langleyfcf25832014-12-18 17:42:32 -08002518int ssl_ctx_log_master_secret(SSL_CTX *ctx, const uint8_t *client_random,
2519 size_t client_random_len, const uint8_t *master,
2520 size_t master_len) {
2521 BIO *bio = ctx->keylog_bio;
2522 CBB cbb;
2523 uint8_t *out;
2524 size_t out_len;
2525 int ret;
Adam Langleyadb739e2014-06-20 12:00:00 -07002526
Adam Langleyfcf25832014-12-18 17:42:32 -08002527 if (bio == NULL) {
2528 return 1;
2529 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002530
Adam Langleyfcf25832014-12-18 17:42:32 -08002531 if (client_random_len != 32) {
David Benjamin3570d732015-06-29 00:28:17 -04002532 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08002533 return 0;
2534 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002535
David Benjamina8653202015-06-28 01:26:10 -04002536 CBB_zero(&cbb);
2537 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len * 2 + 1) ||
2538 !CBB_add_bytes(&cbb, (const uint8_t *)"CLIENT_RANDOM ", 14) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08002539 !cbb_add_hex(&cbb, client_random, 32) ||
2540 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2541 !cbb_add_hex(&cbb, master, master_len) ||
2542 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2543 !CBB_finish(&cbb, &out, &out_len)) {
2544 CBB_cleanup(&cbb);
2545 return 0;
2546 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002547
Adam Langley4bdb6e42015-05-15 15:29:21 -07002548 CRYPTO_MUTEX_lock_write(&ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -08002549 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
Adam Langley4bdb6e42015-05-15 15:29:21 -07002550 CRYPTO_MUTEX_unlock(&ctx->lock);
David Benjamine99e9122014-12-11 01:46:01 -05002551
Adam Langleyfcf25832014-12-18 17:42:32 -08002552 OPENSSL_free(out);
2553 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -07002554}
2555
David Benjamined7c4752015-02-16 19:16:46 -05002556int SSL_in_false_start(const SSL *s) {
2557 return s->s3->tmp.in_false_start;
2558}
2559
Adam Langleyfcf25832014-12-18 17:42:32 -08002560int SSL_cutthrough_complete(const SSL *s) {
David Benjamined7c4752015-02-16 19:16:46 -05002561 return SSL_in_false_start(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002562}
Adam Langley95c29f32014-06-20 12:00:00 -07002563
Adam Langleyfcf25832014-12-18 17:42:32 -08002564void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
2565 size_t *ssl_session_size) {
2566 *ssl_size = sizeof(SSL);
2567 *ssl_ctx_size = sizeof(SSL_CTX);
2568 *ssl_session_size = sizeof(SSL_SESSION);
2569}
Feng Lu41aa3252014-11-21 22:47:56 -08002570
David Benjamined7c4752015-02-16 19:16:46 -05002571int ssl3_can_false_start(const SSL *s) {
David Benjamin195dc782015-02-19 13:27:05 -05002572 const SSL_CIPHER *const cipher = SSL_get_current_cipher(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002573
Adam Langleye631d962015-04-01 13:11:01 -07002574 /* False Start only for TLS 1.2 with an ECDHE+AEAD cipher and ALPN or NPN. */
David Benjamin195dc782015-02-19 13:27:05 -05002575 return !SSL_IS_DTLS(s) &&
2576 SSL_version(s) >= TLS1_2_VERSION &&
2577 (s->s3->alpn_selected || s->s3->next_proto_neg_seen) &&
2578 cipher != NULL &&
Adam Langleye631d962015-04-01 13:11:01 -07002579 cipher->algorithm_mkey == SSL_kECDHE &&
David Benjamin195dc782015-02-19 13:27:05 -05002580 (cipher->algorithm_enc == SSL_AES128GCM ||
2581 cipher->algorithm_enc == SSL_AES256GCM ||
2582 cipher->algorithm_enc == SSL_CHACHA20POLY1305);
Adam Langleyfcf25832014-12-18 17:42:32 -08002583}
2584
2585const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version) {
2586 switch (version) {
2587 case SSL3_VERSION:
2588 return &SSLv3_enc_data;
2589
2590 case TLS1_VERSION:
2591 return &TLSv1_enc_data;
2592
David Benjamin9e13e1a2015-03-05 01:56:32 -05002593 case DTLS1_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002594 case TLS1_1_VERSION:
2595 return &TLSv1_1_enc_data;
2596
David Benjamin9e13e1a2015-03-05 01:56:32 -05002597 case DTLS1_2_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002598 case TLS1_2_VERSION:
2599 return &TLSv1_2_enc_data;
2600
Adam Langleyfcf25832014-12-18 17:42:32 -08002601 default:
2602 return NULL;
2603 }
2604}
2605
2606uint16_t ssl3_get_max_server_version(const SSL *s) {
2607 uint16_t max_version;
2608
2609 if (SSL_IS_DTLS(s)) {
2610 max_version = (s->max_version != 0) ? s->max_version : DTLS1_2_VERSION;
2611 if (!(s->options & SSL_OP_NO_DTLSv1_2) && DTLS1_2_VERSION >= max_version) {
2612 return DTLS1_2_VERSION;
2613 }
2614 if (!(s->options & SSL_OP_NO_DTLSv1) && DTLS1_VERSION >= max_version) {
2615 return DTLS1_VERSION;
2616 }
2617 return 0;
2618 }
2619
2620 max_version = (s->max_version != 0) ? s->max_version : TLS1_2_VERSION;
2621 if (!(s->options & SSL_OP_NO_TLSv1_2) && TLS1_2_VERSION <= max_version) {
2622 return TLS1_2_VERSION;
2623 }
2624 if (!(s->options & SSL_OP_NO_TLSv1_1) && TLS1_1_VERSION <= max_version) {
2625 return TLS1_1_VERSION;
2626 }
2627 if (!(s->options & SSL_OP_NO_TLSv1) && TLS1_VERSION <= max_version) {
2628 return TLS1_VERSION;
2629 }
2630 if (!(s->options & SSL_OP_NO_SSLv3) && SSL3_VERSION <= max_version) {
2631 return SSL3_VERSION;
2632 }
2633 return 0;
2634}
2635
2636uint16_t ssl3_get_mutual_version(SSL *s, uint16_t client_version) {
2637 uint16_t version = 0;
2638
2639 if (SSL_IS_DTLS(s)) {
2640 /* Clamp client_version to max_version. */
2641 if (s->max_version != 0 && client_version < s->max_version) {
2642 client_version = s->max_version;
2643 }
2644
2645 if (client_version <= DTLS1_2_VERSION && !(s->options & SSL_OP_NO_DTLSv1_2)) {
2646 version = DTLS1_2_VERSION;
2647 } else if (client_version <= DTLS1_VERSION &&
David Benjaminb18f0242015-03-10 18:30:08 -04002648 !(s->options & SSL_OP_NO_DTLSv1)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002649 version = DTLS1_VERSION;
2650 }
2651
2652 /* Check against min_version. */
2653 if (version != 0 && s->min_version != 0 && version > s->min_version) {
2654 return 0;
2655 }
2656 return version;
2657 } else {
2658 /* Clamp client_version to max_version. */
2659 if (s->max_version != 0 && client_version > s->max_version) {
2660 client_version = s->max_version;
2661 }
2662
2663 if (client_version >= TLS1_2_VERSION && !(s->options & SSL_OP_NO_TLSv1_2)) {
2664 version = TLS1_2_VERSION;
2665 } else if (client_version >= TLS1_1_VERSION &&
2666 !(s->options & SSL_OP_NO_TLSv1_1)) {
2667 version = TLS1_1_VERSION;
2668 } else if (client_version >= TLS1_VERSION && !(s->options & SSL_OP_NO_TLSv1)) {
2669 version = TLS1_VERSION;
2670 } else if (client_version >= SSL3_VERSION && !(s->options & SSL_OP_NO_SSLv3)) {
2671 version = SSL3_VERSION;
2672 }
2673
2674 /* Check against min_version. */
2675 if (version != 0 && s->min_version != 0 && version < s->min_version) {
2676 return 0;
2677 }
2678 return version;
2679 }
2680}
2681
2682uint16_t ssl3_get_max_client_version(SSL *s) {
David Benjamin123a8fd2015-04-26 14:16:41 -04002683 uint32_t options = s->options;
Adam Langleyfcf25832014-12-18 17:42:32 -08002684 uint16_t version = 0;
2685
2686 /* OpenSSL's API for controlling versions entails blacklisting individual
2687 * protocols. This has two problems. First, on the client, the protocol can
2688 * only express a contiguous range of versions. Second, a library consumer
2689 * trying to set a maximum version cannot disable protocol versions that get
2690 * added in a future version of the library.
2691 *
2692 * To account for both of these, OpenSSL interprets the client-side bitmask
2693 * as a min/max range by picking the lowest contiguous non-empty range of
2694 * enabled protocols. Note that this means it is impossible to set a maximum
2695 * version of TLS 1.2 in a future-proof way.
2696 *
2697 * By this scheme, the maximum version is the lowest version V such that V is
2698 * enabled and V+1 is disabled or unimplemented. */
2699 if (SSL_IS_DTLS(s)) {
2700 if (!(options & SSL_OP_NO_DTLSv1_2)) {
2701 version = DTLS1_2_VERSION;
2702 }
2703 if (!(options & SSL_OP_NO_DTLSv1) && (options & SSL_OP_NO_DTLSv1_2)) {
2704 version = DTLS1_VERSION;
2705 }
2706 if (s->max_version != 0 && version < s->max_version) {
2707 version = s->max_version;
2708 }
2709 } else {
2710 if (!(options & SSL_OP_NO_TLSv1_2)) {
2711 version = TLS1_2_VERSION;
2712 }
2713 if (!(options & SSL_OP_NO_TLSv1_1) && (options & SSL_OP_NO_TLSv1_2)) {
2714 version = TLS1_1_VERSION;
2715 }
2716 if (!(options & SSL_OP_NO_TLSv1) && (options & SSL_OP_NO_TLSv1_1)) {
2717 version = TLS1_VERSION;
2718 }
2719 if (!(options & SSL_OP_NO_SSLv3) && (options & SSL_OP_NO_TLSv1)) {
2720 version = SSL3_VERSION;
2721 }
2722 if (s->max_version != 0 && version > s->max_version) {
2723 version = s->max_version;
2724 }
2725 }
2726
2727 return version;
2728}
2729
2730int ssl3_is_version_enabled(SSL *s, uint16_t version) {
2731 if (SSL_IS_DTLS(s)) {
2732 if (s->max_version != 0 && version < s->max_version) {
2733 return 0;
2734 }
2735 if (s->min_version != 0 && version > s->min_version) {
2736 return 0;
2737 }
2738
2739 switch (version) {
2740 case DTLS1_VERSION:
2741 return !(s->options & SSL_OP_NO_DTLSv1);
2742
2743 case DTLS1_2_VERSION:
2744 return !(s->options & SSL_OP_NO_DTLSv1_2);
2745
2746 default:
2747 return 0;
2748 }
2749 } else {
2750 if (s->max_version != 0 && version > s->max_version) {
2751 return 0;
2752 }
2753 if (s->min_version != 0 && version < s->min_version) {
2754 return 0;
2755 }
2756
2757 switch (version) {
2758 case SSL3_VERSION:
2759 return !(s->options & SSL_OP_NO_SSLv3);
2760
2761 case TLS1_VERSION:
2762 return !(s->options & SSL_OP_NO_TLSv1);
2763
2764 case TLS1_1_VERSION:
2765 return !(s->options & SSL_OP_NO_TLSv1_1);
2766
2767 case TLS1_2_VERSION:
2768 return !(s->options & SSL_OP_NO_TLSv1_2);
2769
2770 default:
2771 return 0;
2772 }
2773 }
2774}
2775
David Benjaminea72bd02014-12-21 21:27:41 -05002776uint16_t ssl3_version_from_wire(SSL *s, uint16_t wire_version) {
2777 if (!SSL_IS_DTLS(s)) {
2778 return wire_version;
2779 }
2780
2781 uint16_t tls_version = ~wire_version;
2782 uint16_t version = tls_version + 0x0201;
2783 /* If either component overflowed, clamp it so comparisons still work. */
2784 if ((version >> 8) < (tls_version >> 8)) {
2785 version = 0xff00 | (version & 0xff);
2786 }
2787 if ((version & 0xff) < (tls_version & 0xff)) {
2788 version = (version & 0xff00) | 0xff;
2789 }
2790 /* DTLS 1.0 maps to TLS 1.1, not TLS 1.0. */
2791 if (version == TLS1_VERSION) {
2792 version = TLS1_1_VERSION;
2793 }
2794 return version;
2795}
2796
Adam Langleyfcf25832014-12-18 17:42:32 -08002797int SSL_cache_hit(SSL *s) { return s->hit; }
2798
2799int SSL_is_server(SSL *s) { return s->server; }
2800
Adam Langley524e7172015-02-20 16:04:00 -08002801void SSL_CTX_set_dos_protection_cb(
2802 SSL_CTX *ctx, int (*cb)(const struct ssl_early_callback_ctx *)) {
2803 ctx->dos_protection_cb = cb;
2804}
2805
David Benjaminb16346b2015-04-08 19:16:58 -04002806void SSL_set_reject_peer_renegotiations(SSL *s, int reject) {
David Benjamin897e5e02015-05-12 17:03:54 -04002807 s->accept_peer_renegotiations = !reject;
David Benjaminb16346b2015-04-08 19:16:58 -04002808}
2809
Adam Langley3f92d212015-02-20 15:32:52 -08002810int SSL_get_rc4_state(const SSL *ssl, const RC4_KEY **read_key,
2811 const RC4_KEY **write_key) {
2812 if (ssl->aead_read_ctx == NULL || ssl->aead_write_ctx == NULL) {
2813 return 0;
2814 }
2815
2816 return EVP_AEAD_CTX_get_rc4_state(&ssl->aead_read_ctx->ctx, read_key) &&
2817 EVP_AEAD_CTX_get_rc4_state(&ssl->aead_write_ctx->ctx, write_key);
2818}
David Benjaminda881e92015-04-26 14:45:04 -04002819
Adam Langleyaf0e32c2015-06-03 09:57:23 -07002820int SSL_get_tls_unique(const SSL *ssl, uint8_t *out, size_t *out_len,
2821 size_t max_out) {
2822 /* The tls-unique value is the first Finished message in the handshake, which
2823 * is the client's in a full handshake and the server's for a resumption. See
2824 * https://tools.ietf.org/html/rfc5929#section-3.1. */
2825 const uint8_t *finished = ssl->s3->previous_client_finished;
2826 size_t finished_len = ssl->s3->previous_client_finished_len;
2827 if (ssl->hit) {
2828 /* tls-unique is broken for resumed sessions unless EMS is used. */
2829 if (!ssl->session->extended_master_secret) {
2830 goto err;
2831 }
2832 finished = ssl->s3->previous_server_finished;
2833 finished_len = ssl->s3->previous_server_finished_len;
2834 }
2835
2836 if (!ssl->s3->initial_handshake_complete ||
2837 ssl->version < TLS1_VERSION) {
2838 goto err;
2839 }
2840
2841 *out_len = finished_len;
2842 if (finished_len > max_out) {
2843 *out_len = max_out;
2844 }
2845
2846 memcpy(out, finished, *out_len);
2847 return 1;
2848
2849err:
2850 *out_len = 0;
2851 memset(out, 0, max_out);
2852 return 0;
2853}
2854
David Benjaminda881e92015-04-26 14:45:04 -04002855int SSL_CTX_sess_connect(const SSL_CTX *ctx) { return 0; }
2856int SSL_CTX_sess_connect_good(const SSL_CTX *ctx) { return 0; }
2857int SSL_CTX_sess_connect_renegotiate(const SSL_CTX *ctx) { return 0; }
2858int SSL_CTX_sess_accept(const SSL_CTX *ctx) { return 0; }
2859int SSL_CTX_sess_accept_renegotiate(const SSL_CTX *ctx) { return 0; }
2860int SSL_CTX_sess_accept_good(const SSL_CTX *ctx) { return 0; }
2861int SSL_CTX_sess_hits(const SSL_CTX *ctx) { return 0; }
2862int SSL_CTX_sess_cb_hits(const SSL_CTX *ctx) { return 0; }
2863int SSL_CTX_sess_misses(const SSL_CTX *ctx) { return 0; }
2864int SSL_CTX_sess_timeouts(const SSL_CTX *ctx) { return 0; }
2865int SSL_CTX_sess_cache_full(const SSL_CTX *ctx) { return 0; }
Matt Braithwaite4838d8a2015-08-20 13:20:03 -07002866void ERR_load_SSL_strings(void) {}