blob: 0aebdb234ccc02e571f2882801a4043b2002df03 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
108 *
109 */
110/* ====================================================================
111 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
112 * ECC cipher suite support in OpenSSL originally developed by
113 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
114 */
115/* ====================================================================
116 * Copyright 2005 Nokia. All rights reserved.
117 *
118 * The portions of the attached software ("Contribution") is developed by
119 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
120 * license.
121 *
122 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
123 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
124 * support (see RFC 4279) to OpenSSL.
125 *
126 * No patent licenses or other rights except those expressly stated in
127 * the OpenSSL open source license shall be deemed granted or received
128 * expressly, by implication, estoppel, or otherwise.
129 *
130 * No assurances are provided by Nokia that the Contribution does not
131 * infringe the patent or other intellectual property rights of any third
132 * party or that the license provides you with all the necessary rights
133 * to make use of the Contribution.
134 *
135 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
136 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
137 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
138 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
139 * OTHERWISE. */
140
Adam Langley95c29f32014-06-20 12:00:00 -0700141#include <assert.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400142#include <stdio.h>
143#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700144
David Benjamin39482a12014-07-20 13:30:15 -0400145#include <openssl/bytestring.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700146#include <openssl/dh.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400147#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700148#include <openssl/lhash.h>
149#include <openssl/mem.h>
150#include <openssl/obj.h>
151#include <openssl/rand.h>
152#include <openssl/x509v3.h>
153
David Benjamin2ee94aa2015-04-07 22:38:30 -0400154#include "internal.h"
David Benjamin546f1a52015-04-15 16:46:09 -0400155#include "../crypto/internal.h"
156
Adam Langley95c29f32014-06-20 12:00:00 -0700157
David Benjamin76c2efc2015-08-31 14:24:29 -0400158/* |SSL_R_UNKNOWN_PROTOCOL| is no longer emitted, but continue to define it
David Benjamin97760d52015-07-24 23:02:49 -0400159 * to avoid downstream churn. */
David Benjamin76c2efc2015-08-31 14:24:29 -0400160OPENSSL_DECLARE_ERROR_REASON(SSL, UNKNOWN_PROTOCOL)
David Benjamin97760d52015-07-24 23:02:49 -0400161
Adam Langleyfcf25832014-12-18 17:42:32 -0800162/* Some error codes are special. Ensure the make_errors.go script never
163 * regresses this. */
164OPENSSL_COMPILE_ASSERT(SSL_R_TLSV1_ALERT_NO_RENEGOTIATION ==
165 SSL_AD_NO_RENEGOTIATION + SSL_AD_REASON_OFFSET,
166 ssl_alert_reason_code_mismatch);
David Benjamine1136082014-09-20 12:28:58 -0400167
David Benjamin1d0a1942015-04-26 15:35:35 -0400168/* kMaxHandshakeSize is the maximum size, in bytes, of a handshake message. */
169static const size_t kMaxHandshakeSize = (1u << 24) - 1;
170
David Benjaminaa585132015-06-29 23:36:17 -0400171static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl =
172 CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
173static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl_ctx =
174 CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
David Benjamin9f33fc62015-04-15 17:29:53 -0400175
David Benjamin4831c332015-05-16 11:43:13 -0400176int SSL_clear(SSL *ssl) {
177 if (ssl->method == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400178 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_METHOD_SPECIFIED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800179 return 0;
180 }
Adam Langley95c29f32014-06-20 12:00:00 -0700181
David Benjamin4831c332015-05-16 11:43:13 -0400182 if (ssl_clear_bad_session(ssl)) {
183 SSL_SESSION_free(ssl->session);
184 ssl->session = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800185 }
Adam Langley95c29f32014-06-20 12:00:00 -0700186
David Benjamin4831c332015-05-16 11:43:13 -0400187 ssl->hit = 0;
188 ssl->shutdown = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700189
David Benjamin4831c332015-05-16 11:43:13 -0400190 /* SSL_clear may be called before or after the |ssl| is initialized in either
Adam Langleyfcf25832014-12-18 17:42:32 -0800191 * accept or connect state. In the latter case, SSL_clear should preserve the
David Benjamin4831c332015-05-16 11:43:13 -0400192 * half and reset |ssl->state| accordingly. */
193 if (ssl->handshake_func != NULL) {
194 if (ssl->server) {
195 SSL_set_accept_state(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800196 } else {
David Benjamin4831c332015-05-16 11:43:13 -0400197 SSL_set_connect_state(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800198 }
199 } else {
David Benjamin4831c332015-05-16 11:43:13 -0400200 assert(ssl->state == 0);
Adam Langleyfcf25832014-12-18 17:42:32 -0800201 }
Adam Langley95c29f32014-06-20 12:00:00 -0700202
David Benjamin5f387e32015-05-15 22:05:42 -0400203 /* TODO(davidben): Some state on |ssl| is reset both in |SSL_new| and
David Benjamin62fd1622015-01-11 13:30:01 -0500204 * |SSL_clear| because it is per-connection state rather than configuration
David Benjamin4831c332015-05-16 11:43:13 -0400205 * state. Per-connection state should be on |ssl->s3| and |ssl->d1| so it is
David Benjamin62fd1622015-01-11 13:30:01 -0500206 * naturally reset at the right points between |SSL_new|, |SSL_clear|, and
207 * |ssl3_new|. */
208
David Benjamin4831c332015-05-16 11:43:13 -0400209 ssl->rwstate = SSL_NOTHING;
Adam Langley95c29f32014-06-20 12:00:00 -0700210
David Benjamin4831c332015-05-16 11:43:13 -0400211 BUF_MEM_free(ssl->init_buf);
212 ssl->init_buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700213
David Benjamin4831c332015-05-16 11:43:13 -0400214 ssl_clear_cipher_ctx(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700215
David Benjamin4831c332015-05-16 11:43:13 -0400216 OPENSSL_free(ssl->next_proto_negotiated);
217 ssl->next_proto_negotiated = NULL;
218 ssl->next_proto_negotiated_len = 0;
David Benjamin62fd1622015-01-11 13:30:01 -0500219
David Benjamin4831c332015-05-16 11:43:13 -0400220 /* The ssl->d1->mtu is simultaneously configuration (preserved across
David Benjamin62fd1622015-01-11 13:30:01 -0500221 * clear) and connection-specific state (gets reset).
222 *
223 * TODO(davidben): Avoid this. */
224 unsigned mtu = 0;
David Benjamin4831c332015-05-16 11:43:13 -0400225 if (ssl->d1 != NULL) {
226 mtu = ssl->d1->mtu;
David Benjamin62fd1622015-01-11 13:30:01 -0500227 }
228
David Benjamin4831c332015-05-16 11:43:13 -0400229 ssl->method->ssl_free(ssl);
230 if (!ssl->method->ssl_new(ssl)) {
David Benjamin62fd1622015-01-11 13:30:01 -0500231 return 0;
232 }
David Benjamin4831c332015-05-16 11:43:13 -0400233 ssl->enc_method = ssl3_get_enc_method(ssl->version);
234 assert(ssl->enc_method != NULL);
David Benjamin62fd1622015-01-11 13:30:01 -0500235
David Benjamin4831c332015-05-16 11:43:13 -0400236 if (SSL_IS_DTLS(ssl) && (SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
237 ssl->d1->mtu = mtu;
David Benjamin62fd1622015-01-11 13:30:01 -0500238 }
239
David Benjamin4831c332015-05-16 11:43:13 -0400240 ssl->client_version = ssl->version;
Adam Langley95c29f32014-06-20 12:00:00 -0700241
Adam Langleyfcf25832014-12-18 17:42:32 -0800242 return 1;
243}
Adam Langley95c29f32014-06-20 12:00:00 -0700244
Adam Langleyfcf25832014-12-18 17:42:32 -0800245SSL *SSL_new(SSL_CTX *ctx) {
246 SSL *s;
Adam Langley95c29f32014-06-20 12:00:00 -0700247
Adam Langleyfcf25832014-12-18 17:42:32 -0800248 if (ctx == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400249 OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_CTX);
Adam Langleyfcf25832014-12-18 17:42:32 -0800250 return NULL;
251 }
252 if (ctx->method == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400253 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
Adam Langleyfcf25832014-12-18 17:42:32 -0800254 return NULL;
255 }
Adam Langley95c29f32014-06-20 12:00:00 -0700256
Adam Langleyfcf25832014-12-18 17:42:32 -0800257 s = (SSL *)OPENSSL_malloc(sizeof(SSL));
258 if (s == NULL) {
259 goto err;
260 }
261 memset(s, 0, sizeof(SSL));
Adam Langley95c29f32014-06-20 12:00:00 -0700262
Adam Langleyfcf25832014-12-18 17:42:32 -0800263 s->min_version = ctx->min_version;
264 s->max_version = ctx->max_version;
David Benjamin1eb367c2014-12-12 18:17:51 -0500265
Adam Langleyfcf25832014-12-18 17:42:32 -0800266 s->options = ctx->options;
267 s->mode = ctx->mode;
268 s->max_cert_list = ctx->max_cert_list;
Adam Langley95c29f32014-06-20 12:00:00 -0700269
David Benjamina5a3eeb2015-03-18 20:26:30 -0400270 s->cert = ssl_cert_dup(ctx->cert);
271 if (s->cert == NULL) {
272 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -0800273 }
Adam Langley95c29f32014-06-20 12:00:00 -0700274
Adam Langleyfcf25832014-12-18 17:42:32 -0800275 s->msg_callback = ctx->msg_callback;
276 s->msg_callback_arg = ctx->msg_callback_arg;
277 s->verify_mode = ctx->verify_mode;
278 s->sid_ctx_length = ctx->sid_ctx_length;
279 assert(s->sid_ctx_length <= sizeof s->sid_ctx);
280 memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));
281 s->verify_callback = ctx->default_verify_callback;
282 s->generate_session_id = ctx->generate_session_id;
Adam Langley95c29f32014-06-20 12:00:00 -0700283
Adam Langleyfcf25832014-12-18 17:42:32 -0800284 s->param = X509_VERIFY_PARAM_new();
285 if (!s->param) {
286 goto err;
287 }
288 X509_VERIFY_PARAM_inherit(s->param, ctx->param);
289 s->quiet_shutdown = ctx->quiet_shutdown;
290 s->max_send_fragment = ctx->max_send_fragment;
Adam Langley95c29f32014-06-20 12:00:00 -0700291
Adam Langley0b5e3902015-05-15 13:08:38 -0700292 CRYPTO_refcount_inc(&ctx->references);
Adam Langleyfcf25832014-12-18 17:42:32 -0800293 s->ctx = ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -0800294 s->tlsext_ticket_expected = 0;
Adam Langley0b5e3902015-05-15 13:08:38 -0700295 CRYPTO_refcount_inc(&ctx->references);
Adam Langleyfcf25832014-12-18 17:42:32 -0800296 s->initial_ctx = ctx;
Adam Langley95c29f32014-06-20 12:00:00 -0700297
Adam Langleyfcf25832014-12-18 17:42:32 -0800298 if (ctx->tlsext_ellipticcurvelist) {
299 s->tlsext_ellipticcurvelist =
300 BUF_memdup(ctx->tlsext_ellipticcurvelist,
301 ctx->tlsext_ellipticcurvelist_length * 2);
302 if (!s->tlsext_ellipticcurvelist) {
303 goto err;
304 }
305 s->tlsext_ellipticcurvelist_length = ctx->tlsext_ellipticcurvelist_length;
306 }
307 s->next_proto_negotiated = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700308
Adam Langleyfcf25832014-12-18 17:42:32 -0800309 if (s->ctx->alpn_client_proto_list) {
310 s->alpn_client_proto_list = BUF_memdup(s->ctx->alpn_client_proto_list,
311 s->ctx->alpn_client_proto_list_len);
312 if (s->alpn_client_proto_list == NULL) {
313 goto err;
314 }
315 s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;
316 }
Adam Langley95c29f32014-06-20 12:00:00 -0700317
Adam Langleyfcf25832014-12-18 17:42:32 -0800318 s->verify_result = X509_V_OK;
319 s->method = ctx->method;
Adam Langley95c29f32014-06-20 12:00:00 -0700320
Adam Langleyfcf25832014-12-18 17:42:32 -0800321 if (!s->method->ssl_new(s)) {
322 goto err;
323 }
324 s->enc_method = ssl3_get_enc_method(s->version);
325 assert(s->enc_method != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700326
David Benjamin62fd1622015-01-11 13:30:01 -0500327 s->rwstate = SSL_NOTHING;
Adam Langley95c29f32014-06-20 12:00:00 -0700328
David Benjamin9f33fc62015-04-15 17:29:53 -0400329 CRYPTO_new_ex_data(&g_ex_data_class_ssl, s, &s->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700330
Adam Langleyfcf25832014-12-18 17:42:32 -0800331 s->psk_identity_hint = NULL;
332 if (ctx->psk_identity_hint) {
333 s->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
334 if (s->psk_identity_hint == NULL) {
335 goto err;
336 }
337 }
338 s->psk_client_callback = ctx->psk_client_callback;
339 s->psk_server_callback = ctx->psk_server_callback;
Adam Langley95c29f32014-06-20 12:00:00 -0700340
David Benjamin02ddbfd2015-01-11 13:09:11 -0500341 s->tlsext_channel_id_enabled = ctx->tlsext_channel_id_enabled;
342 if (ctx->tlsext_channel_id_private) {
David Benjamin9a10f8f2015-05-05 22:22:40 -0400343 s->tlsext_channel_id_private =
344 EVP_PKEY_up_ref(ctx->tlsext_channel_id_private);
David Benjamin02ddbfd2015-01-11 13:09:11 -0500345 }
346
Adam Langleyfcf25832014-12-18 17:42:32 -0800347 s->signed_cert_timestamps_enabled = s->ctx->signed_cert_timestamps_enabled;
348 s->ocsp_stapling_enabled = s->ctx->ocsp_stapling_enabled;
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200349
Adam Langleyfcf25832014-12-18 17:42:32 -0800350 return s;
351
Adam Langley95c29f32014-06-20 12:00:00 -0700352err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400353 SSL_free(s);
David Benjamin3570d732015-06-29 00:28:17 -0400354 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700355
Adam Langleyfcf25832014-12-18 17:42:32 -0800356 return NULL;
357}
Adam Langley95c29f32014-06-20 12:00:00 -0700358
Adam Langleyfcf25832014-12-18 17:42:32 -0800359int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
360 unsigned int sid_ctx_len) {
361 if (sid_ctx_len > sizeof ctx->sid_ctx) {
David Benjamin3570d732015-06-29 00:28:17 -0400362 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -0800363 return 0;
364 }
365 ctx->sid_ctx_length = sid_ctx_len;
366 memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700367
Adam Langleyfcf25832014-12-18 17:42:32 -0800368 return 1;
369}
Adam Langley95c29f32014-06-20 12:00:00 -0700370
Adam Langleyfcf25832014-12-18 17:42:32 -0800371int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
372 unsigned int sid_ctx_len) {
373 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
David Benjamin3570d732015-06-29 00:28:17 -0400374 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -0800375 return 0;
376 }
377 ssl->sid_ctx_length = sid_ctx_len;
378 memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700379
Adam Langleyfcf25832014-12-18 17:42:32 -0800380 return 1;
381}
Adam Langley95c29f32014-06-20 12:00:00 -0700382
Adam Langleyfcf25832014-12-18 17:42:32 -0800383int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800384 ctx->generate_session_id = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -0800385 return 1;
386}
Adam Langley95c29f32014-06-20 12:00:00 -0700387
Adam Langleyfcf25832014-12-18 17:42:32 -0800388int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800389 ssl->generate_session_id = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -0800390 return 1;
391}
Adam Langley95c29f32014-06-20 12:00:00 -0700392
Adam Langleyfcf25832014-12-18 17:42:32 -0800393int SSL_has_matching_session_id(const SSL *ssl, const uint8_t *id,
394 unsigned int id_len) {
395 /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how we
396 * can "construct" a session to give us the desired check - ie. to find if
397 * there's a session in the hash table that would conflict with any new
398 * session built out of this id/id_len and the ssl_version in use by this
399 * SSL. */
400 SSL_SESSION r, *p;
Adam Langley95c29f32014-06-20 12:00:00 -0700401
Adam Langleyfcf25832014-12-18 17:42:32 -0800402 if (id_len > sizeof r.session_id) {
403 return 0;
404 }
Adam Langley95c29f32014-06-20 12:00:00 -0700405
Adam Langleyfcf25832014-12-18 17:42:32 -0800406 r.ssl_version = ssl->version;
407 r.session_id_length = id_len;
408 memcpy(r.session_id, id, id_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700409
Adam Langley4bdb6e42015-05-15 15:29:21 -0700410 CRYPTO_MUTEX_lock_read(&ssl->ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -0800411 p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r);
Adam Langley4bdb6e42015-05-15 15:29:21 -0700412 CRYPTO_MUTEX_unlock(&ssl->ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -0800413 return p != NULL;
414}
Adam Langley95c29f32014-06-20 12:00:00 -0700415
Adam Langleyfcf25832014-12-18 17:42:32 -0800416int SSL_CTX_set_purpose(SSL_CTX *s, int purpose) {
417 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
418}
419
420int SSL_set_purpose(SSL *s, int purpose) {
421 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
422}
423
424int SSL_CTX_set_trust(SSL_CTX *s, int trust) {
425 return X509_VERIFY_PARAM_set_trust(s->param, trust);
426}
427
428int SSL_set_trust(SSL *s, int trust) {
429 return X509_VERIFY_PARAM_set_trust(s->param, trust);
430}
431
432int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm) {
433 return X509_VERIFY_PARAM_set1(ctx->param, vpm);
434}
435
436int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm) {
437 return X509_VERIFY_PARAM_set1(ssl->param, vpm);
438}
Adam Langley95c29f32014-06-20 12:00:00 -0700439
Adam Langley858a88d2014-06-20 12:00:00 -0700440void ssl_cipher_preference_list_free(
Adam Langleyfcf25832014-12-18 17:42:32 -0800441 struct ssl_cipher_preference_list_st *cipher_list) {
David Benjamin5d1ec732015-04-22 13:38:00 -0400442 if (cipher_list == NULL) {
443 return;
444 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800445 sk_SSL_CIPHER_free(cipher_list->ciphers);
446 OPENSSL_free(cipher_list->in_group_flags);
447 OPENSSL_free(cipher_list);
448}
Adam Langley858a88d2014-06-20 12:00:00 -0700449
Adam Langleyfcf25832014-12-18 17:42:32 -0800450struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_dup(
451 struct ssl_cipher_preference_list_st *cipher_list) {
452 struct ssl_cipher_preference_list_st *ret = NULL;
453 size_t n = sk_SSL_CIPHER_num(cipher_list->ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700454
Adam Langleyfcf25832014-12-18 17:42:32 -0800455 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
456 if (!ret) {
457 goto err;
458 }
459
460 ret->ciphers = NULL;
461 ret->in_group_flags = NULL;
462 ret->ciphers = sk_SSL_CIPHER_dup(cipher_list->ciphers);
463 if (!ret->ciphers) {
464 goto err;
465 }
466 ret->in_group_flags = BUF_memdup(cipher_list->in_group_flags, n);
467 if (!ret->in_group_flags) {
468 goto err;
469 }
470
471 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -0700472
473err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400474 ssl_cipher_preference_list_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -0800475 return NULL;
476}
Adam Langley858a88d2014-06-20 12:00:00 -0700477
Adam Langleyfcf25832014-12-18 17:42:32 -0800478struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_from_ciphers(
David Benjamin60da0cd2015-05-03 15:21:28 -0400479 STACK_OF(SSL_CIPHER) *ciphers) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800480 struct ssl_cipher_preference_list_st *ret = NULL;
481 size_t n = sk_SSL_CIPHER_num(ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700482
Adam Langleyfcf25832014-12-18 17:42:32 -0800483 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
484 if (!ret) {
485 goto err;
486 }
487 ret->ciphers = NULL;
488 ret->in_group_flags = NULL;
489 ret->ciphers = sk_SSL_CIPHER_dup(ciphers);
490 if (!ret->ciphers) {
491 goto err;
492 }
493 ret->in_group_flags = OPENSSL_malloc(n);
494 if (!ret->in_group_flags) {
495 goto err;
496 }
497 memset(ret->in_group_flags, 0, n);
498 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -0700499
500err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400501 ssl_cipher_preference_list_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -0800502 return NULL;
503}
Adam Langley858a88d2014-06-20 12:00:00 -0700504
Adam Langleyfcf25832014-12-18 17:42:32 -0800505X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) { return ctx->param; }
Adam Langley95c29f32014-06-20 12:00:00 -0700506
Adam Langleyfcf25832014-12-18 17:42:32 -0800507X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) { return ssl->param; }
Adam Langley95c29f32014-06-20 12:00:00 -0700508
David Benjamin7481d392015-07-05 19:38:46 -0400509void SSL_certs_clear(SSL *ssl) { ssl_cert_clear_certs(ssl->cert); }
Adam Langley95c29f32014-06-20 12:00:00 -0700510
David Benjamin4831c332015-05-16 11:43:13 -0400511void SSL_free(SSL *ssl) {
512 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800513 return;
514 }
Adam Langley95c29f32014-06-20 12:00:00 -0700515
David Benjamin4831c332015-05-16 11:43:13 -0400516 X509_VERIFY_PARAM_free(ssl->param);
Adam Langley95c29f32014-06-20 12:00:00 -0700517
David Benjamin4831c332015-05-16 11:43:13 -0400518 CRYPTO_free_ex_data(&g_ex_data_class_ssl, ssl, &ssl->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700519
David Benjamin4831c332015-05-16 11:43:13 -0400520 if (ssl->bbio != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800521 /* If the buffering BIO is in place, pop it off */
David Benjamin4831c332015-05-16 11:43:13 -0400522 if (ssl->bbio == ssl->wbio) {
523 ssl->wbio = BIO_pop(ssl->wbio);
Adam Langleyfcf25832014-12-18 17:42:32 -0800524 }
David Benjamin4831c332015-05-16 11:43:13 -0400525 BIO_free(ssl->bbio);
526 ssl->bbio = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800527 }
Adam Langley95c29f32014-06-20 12:00:00 -0700528
David Benjamin4831c332015-05-16 11:43:13 -0400529 int free_wbio = ssl->wbio != ssl->rbio;
530 BIO_free_all(ssl->rbio);
David Benjamin2755a3e2015-04-22 16:17:58 -0400531 if (free_wbio) {
David Benjamin4831c332015-05-16 11:43:13 -0400532 BIO_free_all(ssl->wbio);
Adam Langleyfcf25832014-12-18 17:42:32 -0800533 }
Adam Langley95c29f32014-06-20 12:00:00 -0700534
David Benjamin4831c332015-05-16 11:43:13 -0400535 BUF_MEM_free(ssl->init_buf);
Adam Langley95c29f32014-06-20 12:00:00 -0700536
Adam Langleyfcf25832014-12-18 17:42:32 -0800537 /* add extra stuff */
David Benjamin4831c332015-05-16 11:43:13 -0400538 ssl_cipher_preference_list_free(ssl->cipher_list);
539 sk_SSL_CIPHER_free(ssl->cipher_list_by_id);
Adam Langley95c29f32014-06-20 12:00:00 -0700540
David Benjamin4831c332015-05-16 11:43:13 -0400541 ssl_clear_bad_session(ssl);
542 SSL_SESSION_free(ssl->session);
Adam Langley95c29f32014-06-20 12:00:00 -0700543
David Benjamin4831c332015-05-16 11:43:13 -0400544 ssl_clear_cipher_ctx(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700545
David Benjamin4831c332015-05-16 11:43:13 -0400546 ssl_cert_free(ssl->cert);
Adam Langley0289c732014-06-20 12:00:00 -0700547
David Benjamin4831c332015-05-16 11:43:13 -0400548 OPENSSL_free(ssl->tlsext_hostname);
549 SSL_CTX_free(ssl->initial_ctx);
David Benjamin4831c332015-05-16 11:43:13 -0400550 OPENSSL_free(ssl->tlsext_ellipticcurvelist);
551 OPENSSL_free(ssl->alpn_client_proto_list);
552 EVP_PKEY_free(ssl->tlsext_channel_id_private);
553 OPENSSL_free(ssl->psk_identity_hint);
554 sk_X509_NAME_pop_free(ssl->client_CA, X509_NAME_free);
555 OPENSSL_free(ssl->next_proto_negotiated);
556 sk_SRTP_PROTECTION_PROFILE_free(ssl->srtp_profiles);
Adam Langley95c29f32014-06-20 12:00:00 -0700557
David Benjamin4831c332015-05-16 11:43:13 -0400558 if (ssl->method != NULL) {
559 ssl->method->ssl_free(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800560 }
David Benjamin4831c332015-05-16 11:43:13 -0400561 SSL_CTX_free(ssl->ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700562
David Benjamin4831c332015-05-16 11:43:13 -0400563 OPENSSL_free(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800564}
Adam Langley95c29f32014-06-20 12:00:00 -0700565
Adam Langleyfcf25832014-12-18 17:42:32 -0800566void SSL_set_bio(SSL *s, BIO *rbio, BIO *wbio) {
567 /* If the output buffering BIO is still in place, remove it. */
568 if (s->bbio != NULL) {
569 if (s->wbio == s->bbio) {
570 s->wbio = s->wbio->next_bio;
571 s->bbio->next_bio = NULL;
572 }
573 }
Adam Langley95c29f32014-06-20 12:00:00 -0700574
David Benjamin2755a3e2015-04-22 16:17:58 -0400575 if (s->rbio != rbio) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800576 BIO_free_all(s->rbio);
577 }
David Benjamin2755a3e2015-04-22 16:17:58 -0400578 if (s->wbio != wbio && s->rbio != s->wbio) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800579 BIO_free_all(s->wbio);
580 }
581 s->rbio = rbio;
582 s->wbio = wbio;
583}
Adam Langley95c29f32014-06-20 12:00:00 -0700584
Adam Langleyfcf25832014-12-18 17:42:32 -0800585BIO *SSL_get_rbio(const SSL *s) { return s->rbio; }
Adam Langley95c29f32014-06-20 12:00:00 -0700586
Adam Langleyfcf25832014-12-18 17:42:32 -0800587BIO *SSL_get_wbio(const SSL *s) { return s->wbio; }
Adam Langley95c29f32014-06-20 12:00:00 -0700588
Adam Langleyfcf25832014-12-18 17:42:32 -0800589int SSL_get_fd(const SSL *s) { return SSL_get_rfd(s); }
Adam Langley95c29f32014-06-20 12:00:00 -0700590
Adam Langleyfcf25832014-12-18 17:42:32 -0800591int SSL_get_rfd(const SSL *s) {
592 int ret = -1;
593 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700594
Adam Langleyfcf25832014-12-18 17:42:32 -0800595 b = SSL_get_rbio(s);
596 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
597 if (r != NULL) {
598 BIO_get_fd(r, &ret);
599 }
600 return ret;
601}
Adam Langley95c29f32014-06-20 12:00:00 -0700602
Adam Langleyfcf25832014-12-18 17:42:32 -0800603int SSL_get_wfd(const SSL *s) {
604 int ret = -1;
605 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700606
Adam Langleyfcf25832014-12-18 17:42:32 -0800607 b = SSL_get_wbio(s);
608 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
609 if (r != NULL) {
610 BIO_get_fd(r, &ret);
611 }
Adam Langley95c29f32014-06-20 12:00:00 -0700612
Adam Langleyfcf25832014-12-18 17:42:32 -0800613 return ret;
614}
Adam Langley95c29f32014-06-20 12:00:00 -0700615
Adam Langleyfcf25832014-12-18 17:42:32 -0800616int SSL_set_fd(SSL *s, int fd) {
617 int ret = 0;
618 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700619
Adam Langleyfcf25832014-12-18 17:42:32 -0800620 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700621
Adam Langleyfcf25832014-12-18 17:42:32 -0800622 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400623 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -0800624 goto err;
625 }
626 BIO_set_fd(bio, fd, BIO_NOCLOSE);
627 SSL_set_bio(s, bio, bio);
628 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700629
Adam Langley95c29f32014-06-20 12:00:00 -0700630err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800631 return ret;
632}
Adam Langley95c29f32014-06-20 12:00:00 -0700633
Adam Langleyfcf25832014-12-18 17:42:32 -0800634int SSL_set_wfd(SSL *s, int fd) {
635 int ret = 0;
636 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700637
Adam Langleyfcf25832014-12-18 17:42:32 -0800638 if (s->rbio == NULL || BIO_method_type(s->rbio) != BIO_TYPE_FD ||
639 (int)BIO_get_fd(s->rbio, NULL) != fd) {
640 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700641
Adam Langleyfcf25832014-12-18 17:42:32 -0800642 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400643 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -0800644 goto err;
645 }
646 BIO_set_fd(bio, fd, BIO_NOCLOSE);
647 SSL_set_bio(s, SSL_get_rbio(s), bio);
648 } else {
649 SSL_set_bio(s, SSL_get_rbio(s), SSL_get_rbio(s));
650 }
651
652 ret = 1;
653
Adam Langley95c29f32014-06-20 12:00:00 -0700654err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800655 return ret;
656}
Adam Langley95c29f32014-06-20 12:00:00 -0700657
Adam Langleyfcf25832014-12-18 17:42:32 -0800658int SSL_set_rfd(SSL *s, int fd) {
659 int ret = 0;
660 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700661
Adam Langleyfcf25832014-12-18 17:42:32 -0800662 if (s->wbio == NULL || BIO_method_type(s->wbio) != BIO_TYPE_FD ||
663 (int)BIO_get_fd(s->wbio, NULL) != fd) {
664 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700665
Adam Langleyfcf25832014-12-18 17:42:32 -0800666 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400667 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -0800668 goto err;
669 }
670 BIO_set_fd(bio, fd, BIO_NOCLOSE);
671 SSL_set_bio(s, bio, SSL_get_wbio(s));
672 } else {
673 SSL_set_bio(s, SSL_get_wbio(s), SSL_get_wbio(s));
674 }
675 ret = 1;
676
Adam Langley95c29f32014-06-20 12:00:00 -0700677err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800678 return ret;
679}
Adam Langley95c29f32014-06-20 12:00:00 -0700680
681/* return length of latest Finished message we sent, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -0800682size_t SSL_get_finished(const SSL *s, void *buf, size_t count) {
683 size_t ret = 0;
684
685 if (s->s3 != NULL) {
686 ret = s->s3->tmp.finish_md_len;
687 if (count > ret) {
688 count = ret;
689 }
690 memcpy(buf, s->s3->tmp.finish_md, count);
691 }
692
693 return ret;
694}
Adam Langley95c29f32014-06-20 12:00:00 -0700695
696/* return length of latest Finished message we expected, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -0800697size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count) {
698 size_t ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700699
Adam Langleyfcf25832014-12-18 17:42:32 -0800700 if (s->s3 != NULL) {
701 ret = s->s3->tmp.peer_finish_md_len;
702 if (count > ret) {
703 count = ret;
704 }
705 memcpy(buf, s->s3->tmp.peer_finish_md, count);
706 }
Adam Langley95c29f32014-06-20 12:00:00 -0700707
Adam Langleyfcf25832014-12-18 17:42:32 -0800708 return ret;
709}
Adam Langley95c29f32014-06-20 12:00:00 -0700710
Adam Langleyfcf25832014-12-18 17:42:32 -0800711int SSL_get_verify_mode(const SSL *s) { return s->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -0700712
Adam Langleyfcf25832014-12-18 17:42:32 -0800713int SSL_get_verify_depth(const SSL *s) {
714 return X509_VERIFY_PARAM_get_depth(s->param);
715}
Adam Langley95c29f32014-06-20 12:00:00 -0700716
Adam Langleyfcf25832014-12-18 17:42:32 -0800717int (*SSL_get_verify_callback(const SSL *s))(int, X509_STORE_CTX *) {
718 return s->verify_callback;
719}
Adam Langley95c29f32014-06-20 12:00:00 -0700720
Adam Langleyfcf25832014-12-18 17:42:32 -0800721int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) { return ctx->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -0700722
Adam Langleyfcf25832014-12-18 17:42:32 -0800723int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) {
724 return X509_VERIFY_PARAM_get_depth(ctx->param);
725}
Adam Langley95c29f32014-06-20 12:00:00 -0700726
Adam Langleyfcf25832014-12-18 17:42:32 -0800727int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int, X509_STORE_CTX *) {
728 return ctx->default_verify_callback;
729}
Adam Langley95c29f32014-06-20 12:00:00 -0700730
Adam Langleyfcf25832014-12-18 17:42:32 -0800731void SSL_set_verify(SSL *s, int mode,
732 int (*callback)(int ok, X509_STORE_CTX *ctx)) {
733 s->verify_mode = mode;
734 if (callback != NULL) {
735 s->verify_callback = callback;
736 }
737}
Adam Langley95c29f32014-06-20 12:00:00 -0700738
Adam Langleyfcf25832014-12-18 17:42:32 -0800739void SSL_set_verify_depth(SSL *s, int depth) {
740 X509_VERIFY_PARAM_set_depth(s->param, depth);
741}
Adam Langley95c29f32014-06-20 12:00:00 -0700742
David Benjamin9a41d1b2015-05-16 01:30:09 -0400743int SSL_CTX_get_read_ahead(const SSL_CTX *ctx) { return 0; }
Adam Langley95c29f32014-06-20 12:00:00 -0700744
David Benjamin9a41d1b2015-05-16 01:30:09 -0400745int SSL_get_read_ahead(const SSL *s) { return 0; }
Adam Langley95c29f32014-06-20 12:00:00 -0700746
David Benjamin9a41d1b2015-05-16 01:30:09 -0400747void SSL_CTX_set_read_ahead(SSL_CTX *ctx, int yes) { }
David Benjamin61ecccf2015-05-05 09:44:51 -0400748
David Benjamin9a41d1b2015-05-16 01:30:09 -0400749void SSL_set_read_ahead(SSL *s, int yes) { }
David Benjamin61ecccf2015-05-05 09:44:51 -0400750
Adam Langleyfcf25832014-12-18 17:42:32 -0800751int SSL_pending(const SSL *s) {
David Benjamin904dc722015-05-30 16:39:07 -0400752 return (s->s3->rrec.type == SSL3_RT_APPLICATION_DATA) ? s->s3->rrec.length
753 : 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800754}
Adam Langley95c29f32014-06-20 12:00:00 -0700755
David Benjamin26416e92015-08-22 16:04:17 -0400756X509 *SSL_get_peer_certificate(const SSL *ssl) {
757 if (ssl == NULL || ssl->session == NULL || ssl->session->peer == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800758 return NULL;
759 }
David Benjamin26416e92015-08-22 16:04:17 -0400760 return X509_up_ref(ssl->session->peer);
Adam Langleyfcf25832014-12-18 17:42:32 -0800761}
762
David Benjamin26416e92015-08-22 16:04:17 -0400763STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *ssl) {
764 if (ssl == NULL || ssl->session == NULL) {
765 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800766 }
David Benjamin26416e92015-08-22 16:04:17 -0400767 return ssl->session->cert_chain;
Adam Langleyfcf25832014-12-18 17:42:32 -0800768}
Adam Langley95c29f32014-06-20 12:00:00 -0700769
Adam Langley95c29f32014-06-20 12:00:00 -0700770/* Fix this so it checks all the valid key/cert options */
Adam Langleyfcf25832014-12-18 17:42:32 -0800771int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
David Benjamin651b3d92015-08-09 12:07:25 -0400772 if (ctx->cert->x509 == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400773 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800774 return 0;
775 }
776
David Benjamind1d80782015-07-05 11:54:09 -0400777 if (ctx->cert->privatekey == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400778 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800779 return 0;
780 }
781
David Benjamind1d80782015-07-05 11:54:09 -0400782 return X509_check_private_key(ctx->cert->x509, ctx->cert->privatekey);
Adam Langleyfcf25832014-12-18 17:42:32 -0800783}
Adam Langley95c29f32014-06-20 12:00:00 -0700784
785/* Fix this function so that it takes an optional type parameter */
Adam Langleyfcf25832014-12-18 17:42:32 -0800786int SSL_check_private_key(const SSL *ssl) {
David Benjamind1d80782015-07-05 11:54:09 -0400787 if (ssl->cert->x509 == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400788 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800789 return 0;
790 }
David Benjamin0b145c22014-11-26 20:10:09 -0500791
David Benjamind1d80782015-07-05 11:54:09 -0400792 if (ssl->cert->privatekey == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400793 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800794 return 0;
795 }
Adam Langley95c29f32014-06-20 12:00:00 -0700796
David Benjamind1d80782015-07-05 11:54:09 -0400797 return X509_check_private_key(ssl->cert->x509, ssl->cert->privatekey);
Adam Langleyfcf25832014-12-18 17:42:32 -0800798}
Adam Langley95c29f32014-06-20 12:00:00 -0700799
Adam Langleyfcf25832014-12-18 17:42:32 -0800800int SSL_accept(SSL *s) {
801 if (s->handshake_func == 0) {
802 /* Not properly initialized yet */
803 SSL_set_accept_state(s);
804 }
David Benjamin0b145c22014-11-26 20:10:09 -0500805
Adam Langleyfcf25832014-12-18 17:42:32 -0800806 if (s->handshake_func != s->method->ssl_accept) {
David Benjamin3570d732015-06-29 00:28:17 -0400807 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800808 return -1;
809 }
Adam Langley95c29f32014-06-20 12:00:00 -0700810
Adam Langleyfcf25832014-12-18 17:42:32 -0800811 return s->handshake_func(s);
812}
Adam Langley95c29f32014-06-20 12:00:00 -0700813
Adam Langleyfcf25832014-12-18 17:42:32 -0800814int SSL_connect(SSL *s) {
815 if (s->handshake_func == 0) {
816 /* Not properly initialized yet */
817 SSL_set_connect_state(s);
818 }
Adam Langley95c29f32014-06-20 12:00:00 -0700819
Adam Langleyfcf25832014-12-18 17:42:32 -0800820 if (s->handshake_func != s->method->ssl_connect) {
David Benjamin3570d732015-06-29 00:28:17 -0400821 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800822 return -1;
823 }
Adam Langley95c29f32014-06-20 12:00:00 -0700824
Adam Langleyfcf25832014-12-18 17:42:32 -0800825 return s->handshake_func(s);
826}
Adam Langley95c29f32014-06-20 12:00:00 -0700827
Adam Langleyfcf25832014-12-18 17:42:32 -0800828long SSL_get_default_timeout(const SSL *s) {
829 return SSL_DEFAULT_SESSION_TIMEOUT;
830}
Adam Langley95c29f32014-06-20 12:00:00 -0700831
Adam Langleyfcf25832014-12-18 17:42:32 -0800832int SSL_read(SSL *s, void *buf, int num) {
833 if (s->handshake_func == 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400834 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800835 return -1;
836 }
Adam Langley95c29f32014-06-20 12:00:00 -0700837
Adam Langleyfcf25832014-12-18 17:42:32 -0800838 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
839 s->rwstate = SSL_NOTHING;
840 return 0;
841 }
Adam Langley95c29f32014-06-20 12:00:00 -0700842
David Benjamin904dc722015-05-30 16:39:07 -0400843 ERR_clear_system_error();
844 return s->method->ssl_read_app_data(s, buf, num, 0);
Adam Langleyfcf25832014-12-18 17:42:32 -0800845}
Adam Langley95c29f32014-06-20 12:00:00 -0700846
Adam Langleyfcf25832014-12-18 17:42:32 -0800847int SSL_peek(SSL *s, void *buf, int num) {
848 if (s->handshake_func == 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400849 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800850 return -1;
851 }
Adam Langley95c29f32014-06-20 12:00:00 -0700852
Adam Langleyfcf25832014-12-18 17:42:32 -0800853 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
854 return 0;
855 }
Adam Langley95c29f32014-06-20 12:00:00 -0700856
David Benjamin904dc722015-05-30 16:39:07 -0400857 ERR_clear_system_error();
858 return s->method->ssl_read_app_data(s, buf, num, 1);
Adam Langleyfcf25832014-12-18 17:42:32 -0800859}
Adam Langley95c29f32014-06-20 12:00:00 -0700860
Adam Langleyfcf25832014-12-18 17:42:32 -0800861int SSL_write(SSL *s, const void *buf, int num) {
862 if (s->handshake_func == 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400863 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800864 return -1;
865 }
Adam Langley95c29f32014-06-20 12:00:00 -0700866
Adam Langleyfcf25832014-12-18 17:42:32 -0800867 if (s->shutdown & SSL_SENT_SHUTDOWN) {
868 s->rwstate = SSL_NOTHING;
David Benjamin3570d732015-06-29 00:28:17 -0400869 OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
Adam Langleyfcf25832014-12-18 17:42:32 -0800870 return -1;
871 }
Adam Langley95c29f32014-06-20 12:00:00 -0700872
David Benjamin904dc722015-05-30 16:39:07 -0400873 ERR_clear_system_error();
874 return s->method->ssl_write_app_data(s, buf, num);
Adam Langleyfcf25832014-12-18 17:42:32 -0800875}
Adam Langley95c29f32014-06-20 12:00:00 -0700876
Adam Langleyfcf25832014-12-18 17:42:32 -0800877int SSL_shutdown(SSL *s) {
878 /* Note that this function behaves differently from what one might expect.
879 * Return values are 0 for no success (yet), 1 for success; but calling it
880 * once is usually not enough, even if blocking I/O is used (see
881 * ssl3_shutdown). */
Adam Langley95c29f32014-06-20 12:00:00 -0700882
Adam Langleyfcf25832014-12-18 17:42:32 -0800883 if (s->handshake_func == 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400884 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800885 return -1;
886 }
Adam Langley95c29f32014-06-20 12:00:00 -0700887
David Benjamin904dc722015-05-30 16:39:07 -0400888 if (SSL_in_init(s)) {
889 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800890 }
Adam Langley95c29f32014-06-20 12:00:00 -0700891
David Benjamin904dc722015-05-30 16:39:07 -0400892 /* Do nothing if configured not to send a close_notify. */
893 if (s->quiet_shutdown) {
894 s->shutdown = SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN;
895 return 1;
896 }
897
898 if (!(s->shutdown & SSL_SENT_SHUTDOWN)) {
899 s->shutdown |= SSL_SENT_SHUTDOWN;
900 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_CLOSE_NOTIFY);
901
902 /* our shutdown alert has been sent now, and if it still needs to be
903 * written, s->s3->alert_dispatch will be true */
904 if (s->s3->alert_dispatch) {
905 return -1; /* return WANT_WRITE */
906 }
907 } else if (s->s3->alert_dispatch) {
908 /* resend it if not sent */
909 int ret = s->method->ssl_dispatch_alert(s);
910 if (ret == -1) {
911 /* we only get to return -1 here the 2nd/Nth invocation, we must have
912 * already signalled return 0 upon a previous invoation, return
913 * WANT_WRITE */
914 return ret;
915 }
916 } else if (!(s->shutdown & SSL_RECEIVED_SHUTDOWN)) {
917 /* If we are waiting for a close from our peer, we are closed */
918 s->method->ssl_read_close_notify(s);
919 if (!(s->shutdown & SSL_RECEIVED_SHUTDOWN)) {
920 return -1; /* return WANT_READ */
921 }
922 }
923
924 if (s->shutdown == (SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN) &&
925 !s->s3->alert_dispatch) {
926 return 1;
927 } else {
928 return 0;
929 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800930}
Adam Langley95c29f32014-06-20 12:00:00 -0700931
David Benjamin44d3eed2015-05-21 01:29:55 -0400932int SSL_renegotiate(SSL *ssl) {
933 /* Caller-initiated renegotiation is not supported. */
David Benjamin3570d732015-06-29 00:28:17 -0400934 OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
David Benjamin44d3eed2015-05-21 01:29:55 -0400935 return 0;
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_pending(SSL *ssl) {
939 return SSL_in_init(ssl) && ssl->s3->initial_handshake_complete;
Adam Langleyfcf25832014-12-18 17:42:32 -0800940}
Adam Langley95c29f32014-06-20 12:00:00 -0700941
David Benjamin61ecccf2015-05-05 09:44:51 -0400942uint32_t SSL_CTX_set_options(SSL_CTX *ctx, uint32_t options) {
943 ctx->options |= options;
944 return ctx->options;
945}
946
947uint32_t SSL_set_options(SSL *ssl, uint32_t options) {
948 ssl->options |= options;
949 return ssl->options;
950}
951
952uint32_t SSL_CTX_clear_options(SSL_CTX *ctx, uint32_t options) {
953 ctx->options &= ~options;
954 return ctx->options;
955}
956
957uint32_t SSL_clear_options(SSL *ssl, uint32_t options) {
958 ssl->options &= ~options;
959 return ssl->options;
960}
961
962uint32_t SSL_CTX_get_options(const SSL_CTX *ctx) { return ctx->options; }
963
964uint32_t SSL_get_options(const SSL *ssl) { return ssl->options; }
965
966uint32_t SSL_CTX_set_mode(SSL_CTX *ctx, uint32_t mode) {
967 ctx->mode |= mode;
968 return ctx->mode;
969}
970
971uint32_t SSL_set_mode(SSL *ssl, uint32_t mode) {
972 ssl->mode |= mode;
973 return ssl->mode;
974}
975
976uint32_t SSL_CTX_clear_mode(SSL_CTX *ctx, uint32_t mode) {
977 ctx->mode &= ~mode;
978 return ctx->mode;
979}
980
981uint32_t SSL_clear_mode(SSL *ssl, uint32_t mode) {
982 ssl->mode &= ~mode;
983 return ssl->mode;
984}
985
986uint32_t SSL_CTX_get_mode(const SSL_CTX *ctx) { return ctx->mode; }
987
988uint32_t SSL_get_mode(const SSL *ssl) { return ssl->mode; }
989
David Benjamin1d0a1942015-04-26 15:35:35 -0400990size_t SSL_CTX_get_max_cert_list(const SSL_CTX *ctx) {
991 return ctx->max_cert_list;
992}
993
994void SSL_CTX_set_max_cert_list(SSL_CTX *ctx, size_t max_cert_list) {
995 if (max_cert_list > kMaxHandshakeSize) {
996 max_cert_list = kMaxHandshakeSize;
997 }
998 ctx->max_cert_list = (uint32_t)max_cert_list;
999}
1000
1001size_t SSL_get_max_cert_list(const SSL *ssl) {
1002 return ssl->max_cert_list;
1003}
1004
1005void SSL_set_max_cert_list(SSL *ssl, size_t max_cert_list) {
1006 if (max_cert_list > kMaxHandshakeSize) {
1007 max_cert_list = kMaxHandshakeSize;
1008 }
1009 ssl->max_cert_list = (uint32_t)max_cert_list;
1010}
1011
1012void SSL_CTX_set_max_send_fragment(SSL_CTX *ctx, size_t max_send_fragment) {
1013 if (max_send_fragment < 512) {
1014 max_send_fragment = 512;
1015 }
1016 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1017 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1018 }
1019 ctx->max_send_fragment = (uint16_t)max_send_fragment;
1020}
1021
1022void SSL_set_max_send_fragment(SSL *ssl, size_t max_send_fragment) {
1023 if (max_send_fragment < 512) {
1024 max_send_fragment = 512;
1025 }
1026 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1027 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1028 }
1029 ssl->max_send_fragment = (uint16_t)max_send_fragment;
1030}
1031
David Benjamincb9cf792015-05-05 09:46:14 -04001032int SSL_set_mtu(SSL *ssl, unsigned mtu) {
1033 if (!SSL_IS_DTLS(ssl) || mtu < dtls1_min_mtu()) {
1034 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001035 }
David Benjamincb9cf792015-05-05 09:46:14 -04001036 ssl->d1->mtu = mtu;
1037 return 1;
1038}
1039
1040int SSL_get_secure_renegotiation_support(const SSL *ssl) {
1041 return ssl->s3->send_connection_binding;
1042}
1043
Adam Langleyfcf25832014-12-18 17:42:32 -08001044LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) { return ctx->sessions; }
1045
David Benjamin71f7d3d2015-05-05 09:46:38 -04001046size_t SSL_CTX_sess_number(const SSL_CTX *ctx) {
1047 return lh_SSL_SESSION_num_items(ctx->sessions);
1048}
1049
1050unsigned long SSL_CTX_sess_set_cache_size(SSL_CTX *ctx, unsigned long size) {
1051 unsigned long ret = ctx->session_cache_size;
1052 ctx->session_cache_size = size;
1053 return ret;
1054}
1055
1056unsigned long SSL_CTX_sess_get_cache_size(const SSL_CTX *ctx) {
1057 return ctx->session_cache_size;
1058}
1059
1060int SSL_CTX_set_session_cache_mode(SSL_CTX *ctx, int mode) {
1061 int ret = ctx->session_cache_mode;
1062 ctx->session_cache_mode = mode;
1063 return ret;
1064}
1065
1066int SSL_CTX_get_session_cache_mode(const SSL_CTX *ctx) {
1067 return ctx->session_cache_mode;
1068}
1069
Adam Langleyfcf25832014-12-18 17:42:32 -08001070/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001071 * preference */
David Benjamin60da0cd2015-05-03 15:21:28 -04001072STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001073 if (s == NULL) {
1074 return NULL;
1075 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001076
Adam Langleyfcf25832014-12-18 17:42:32 -08001077 if (s->cipher_list != NULL) {
1078 return s->cipher_list->ciphers;
1079 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001080
Adam Langleyfcf25832014-12-18 17:42:32 -08001081 if (s->version >= TLS1_1_VERSION && s->ctx != NULL &&
1082 s->ctx->cipher_list_tls11 != NULL) {
1083 return s->ctx->cipher_list_tls11->ciphers;
1084 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001085
Adam Langleyfcf25832014-12-18 17:42:32 -08001086 if (s->ctx != NULL && s->ctx->cipher_list != NULL) {
1087 return s->ctx->cipher_list->ciphers;
1088 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001089
Adam Langleyfcf25832014-12-18 17:42:32 -08001090 return NULL;
1091}
Adam Langley95c29f32014-06-20 12:00:00 -07001092
Adam Langleyfcf25832014-12-18 17:42:32 -08001093/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001094 * algorithm id */
David Benjamin60da0cd2015-05-03 15:21:28 -04001095STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001096 if (s == NULL) {
1097 return NULL;
1098 }
Adam Langley95c29f32014-06-20 12:00:00 -07001099
Adam Langleyfcf25832014-12-18 17:42:32 -08001100 if (s->cipher_list_by_id != NULL) {
1101 return s->cipher_list_by_id;
1102 }
Adam Langley95c29f32014-06-20 12:00:00 -07001103
Adam Langleyfcf25832014-12-18 17:42:32 -08001104 if (s->ctx != NULL && s->ctx->cipher_list_by_id != NULL) {
1105 return s->ctx->cipher_list_by_id;
1106 }
Adam Langley95c29f32014-06-20 12:00:00 -07001107
Adam Langleyfcf25832014-12-18 17:42:32 -08001108 return NULL;
1109}
Adam Langley95c29f32014-06-20 12:00:00 -07001110
Adam Langleyfcf25832014-12-18 17:42:32 -08001111/* The old interface to get the same thing as SSL_get_ciphers() */
1112const char *SSL_get_cipher_list(const SSL *s, int n) {
1113 const SSL_CIPHER *c;
David Benjamin60da0cd2015-05-03 15:21:28 -04001114 STACK_OF(SSL_CIPHER) *sk;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001115
Adam Langleyfcf25832014-12-18 17:42:32 -08001116 if (s == NULL) {
1117 return NULL;
1118 }
Adam Langley95c29f32014-06-20 12:00:00 -07001119
Adam Langleyfcf25832014-12-18 17:42:32 -08001120 sk = SSL_get_ciphers(s);
1121 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
1122 return NULL;
1123 }
Adam Langley95c29f32014-06-20 12:00:00 -07001124
Adam Langleyfcf25832014-12-18 17:42:32 -08001125 c = sk_SSL_CIPHER_value(sk, n);
1126 if (c == NULL) {
1127 return NULL;
1128 }
Adam Langley95c29f32014-06-20 12:00:00 -07001129
Adam Langleyfcf25832014-12-18 17:42:32 -08001130 return c->name;
1131}
David Benjamin5491e3f2014-09-29 19:33:09 -04001132
Adam Langleyfcf25832014-12-18 17:42:32 -08001133/* specify the ciphers to be used by default by the SSL_CTX */
1134int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
1135 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001136
Adam Langleyfcf25832014-12-18 17:42:32 -08001137 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001138 &ctx->cipher_list_by_id, str);
Adam Langleyfcf25832014-12-18 17:42:32 -08001139 /* ssl_create_cipher_list may return an empty stack if it was unable to find
1140 * a cipher matching the given rule string (for example if the rule string
1141 * specifies a cipher which has been disabled). This is not an error as far
1142 * as ssl_create_cipher_list is concerned, and hence ctx->cipher_list and
1143 * ctx->cipher_list_by_id has been updated. */
1144 if (sk == NULL) {
1145 return 0;
1146 } else if (sk_SSL_CIPHER_num(sk) == 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001147 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
Adam Langleyfcf25832014-12-18 17:42:32 -08001148 return 0;
1149 }
Adam Langley95c29f32014-06-20 12:00:00 -07001150
Adam Langleyfcf25832014-12-18 17:42:32 -08001151 return 1;
1152}
David Benjamin39482a12014-07-20 13:30:15 -04001153
Adam Langleyfcf25832014-12-18 17:42:32 -08001154int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str) {
1155 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001156
David Benjamin71f07942015-04-08 02:36:59 -04001157 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str);
Adam Langleyfcf25832014-12-18 17:42:32 -08001158 if (sk == NULL) {
1159 return 0;
1160 } else if (sk_SSL_CIPHER_num(sk) == 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001161 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
Adam Langleyfcf25832014-12-18 17:42:32 -08001162 return 0;
1163 }
David Benjamin9f2c0d72014-10-21 22:00:19 -04001164
Adam Langleyfcf25832014-12-18 17:42:32 -08001165 return 1;
1166}
Adam Langley95c29f32014-06-20 12:00:00 -07001167
Adam Langleyfcf25832014-12-18 17:42:32 -08001168/* specify the ciphers to be used by the SSL */
1169int SSL_set_cipher_list(SSL *s, const char *str) {
1170 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001171
Adam Langleyfcf25832014-12-18 17:42:32 -08001172 sk = ssl_create_cipher_list(s->ctx->method, &s->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001173 &s->cipher_list_by_id, str);
David Benjamin39482a12014-07-20 13:30:15 -04001174
Adam Langleyfcf25832014-12-18 17:42:32 -08001175 /* see comment in SSL_CTX_set_cipher_list */
1176 if (sk == NULL) {
1177 return 0;
1178 } else if (sk_SSL_CIPHER_num(sk) == 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001179 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
Adam Langleyfcf25832014-12-18 17:42:32 -08001180 return 0;
1181 }
David Benjamin39482a12014-07-20 13:30:15 -04001182
Adam Langleyfcf25832014-12-18 17:42:32 -08001183 return 1;
1184}
Adam Langley95c29f32014-06-20 12:00:00 -07001185
Adam Langleyfcf25832014-12-18 17:42:32 -08001186int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, uint8_t *p) {
1187 size_t i;
1188 const SSL_CIPHER *c;
1189 CERT *ct = s->cert;
1190 uint8_t *q;
1191 /* Set disabled masks for this session */
1192 ssl_set_client_disabled(s);
Adam Langley29707792014-06-20 12:00:00 -07001193
Adam Langleyfcf25832014-12-18 17:42:32 -08001194 if (sk == NULL) {
1195 return 0;
1196 }
1197 q = p;
Adam Langley95c29f32014-06-20 12:00:00 -07001198
Adam Langleyfcf25832014-12-18 17:42:32 -08001199 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
1200 c = sk_SSL_CIPHER_value(sk, i);
1201 /* Skip disabled ciphers */
1202 if (c->algorithm_ssl & ct->mask_ssl ||
1203 c->algorithm_mkey & ct->mask_k ||
1204 c->algorithm_auth & ct->mask_a) {
1205 continue;
1206 }
David Benjamina1c90a52015-05-30 17:03:14 -04001207 s2n(ssl_cipher_get_value(c), p);
Adam Langleyfcf25832014-12-18 17:42:32 -08001208 }
1209
1210 /* If all ciphers were disabled, return the error to the caller. */
1211 if (p == q) {
1212 return 0;
1213 }
1214
Adam Langley5021b222015-06-12 18:27:58 -07001215 /* For SSLv3, the SCSV is added. Otherwise the renegotiation extension is
1216 * added. */
1217 if (s->client_version == SSL3_VERSION &&
1218 !s->s3->initial_handshake_complete) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001219 s2n(SSL3_CK_SCSV & 0xffff, p);
Adam Langley5021b222015-06-12 18:27:58 -07001220 /* The renegotiation extension is required to be at index zero. */
1221 s->s3->tmp.extensions.sent |= (1u << 0);
Adam Langleyfcf25832014-12-18 17:42:32 -08001222 }
1223
Adam Langley5f0efe02015-02-20 13:03:16 -08001224 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001225 s2n(SSL3_CK_FALLBACK_SCSV & 0xffff, p);
1226 }
1227
1228 return p - q;
1229}
1230
1231STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs) {
1232 CBS cipher_suites = *cbs;
1233 const SSL_CIPHER *c;
David Benjamin60da0cd2015-05-03 15:21:28 -04001234 STACK_OF(SSL_CIPHER) *sk;
Adam Langleyfcf25832014-12-18 17:42:32 -08001235
1236 if (s->s3) {
1237 s->s3->send_connection_binding = 0;
1238 }
1239
1240 if (CBS_len(&cipher_suites) % 2 != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001241 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
Adam Langleyfcf25832014-12-18 17:42:32 -08001242 return NULL;
1243 }
1244
1245 sk = sk_SSL_CIPHER_new_null();
1246 if (sk == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001247 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001248 goto err;
1249 }
1250
Adam Langleyfcf25832014-12-18 17:42:32 -08001251 while (CBS_len(&cipher_suites) > 0) {
1252 uint16_t cipher_suite;
1253
1254 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
David Benjamin3570d732015-06-29 00:28:17 -04001255 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001256 goto err;
1257 }
1258
1259 /* Check for SCSV. */
1260 if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff)) {
1261 /* SCSV is fatal if renegotiating. */
David Benjamin20f6e972015-05-15 21:51:49 -04001262 if (s->s3->initial_handshake_complete) {
David Benjamin3570d732015-06-29 00:28:17 -04001263 OPENSSL_PUT_ERROR(SSL, SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
Adam Langleyfcf25832014-12-18 17:42:32 -08001264 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1265 goto err;
1266 }
1267 s->s3->send_connection_binding = 1;
1268 continue;
1269 }
1270
1271 /* Check for FALLBACK_SCSV. */
1272 if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff)) {
1273 uint16_t max_version = ssl3_get_max_server_version(s);
1274 if (SSL_IS_DTLS(s) ? (uint16_t)s->version > max_version
1275 : (uint16_t)s->version < max_version) {
David Benjamin3570d732015-06-29 00:28:17 -04001276 OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK);
Adam Langleyfcf25832014-12-18 17:42:32 -08001277 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
1278 goto err;
1279 }
1280 continue;
1281 }
1282
David Benjamina1c90a52015-05-30 17:03:14 -04001283 c = SSL_get_cipher_by_value(cipher_suite);
Adam Langleyfcf25832014-12-18 17:42:32 -08001284 if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
David Benjamin3570d732015-06-29 00:28:17 -04001285 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001286 goto err;
1287 }
1288 }
1289
1290 return sk;
David Benjamin9f2c0d72014-10-21 22:00:19 -04001291
Adam Langley95c29f32014-06-20 12:00:00 -07001292err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001293 sk_SSL_CIPHER_free(sk);
Adam Langleyfcf25832014-12-18 17:42:32 -08001294 return NULL;
1295}
Adam Langley95c29f32014-06-20 12:00:00 -07001296
1297
Adam Langleyfcf25832014-12-18 17:42:32 -08001298/* return a servername extension value if provided in Client Hello, or NULL. So
1299 * far, only host_name types are defined (RFC 3546). */
1300const char *SSL_get_servername(const SSL *s, const int type) {
1301 if (type != TLSEXT_NAMETYPE_host_name) {
1302 return NULL;
1303 }
Adam Langley95c29f32014-06-20 12:00:00 -07001304
Adam Langleyfcf25832014-12-18 17:42:32 -08001305 return s->session && !s->tlsext_hostname ? s->session->tlsext_hostname
1306 : s->tlsext_hostname;
1307}
Adam Langley95c29f32014-06-20 12:00:00 -07001308
Adam Langleyfcf25832014-12-18 17:42:32 -08001309int SSL_get_servername_type(const SSL *s) {
1310 if (s->session &&
1311 (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname)) {
1312 return TLSEXT_NAMETYPE_host_name;
1313 }
Adam Langley95c29f32014-06-20 12:00:00 -07001314
Adam Langleyfcf25832014-12-18 17:42:32 -08001315 return -1;
1316}
Adam Langley95c29f32014-06-20 12:00:00 -07001317
Adam Langleyfcf25832014-12-18 17:42:32 -08001318void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
1319 ctx->signed_cert_timestamps_enabled = 1;
1320}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001321
Adam Langleyfcf25832014-12-18 17:42:32 -08001322int SSL_enable_signed_cert_timestamps(SSL *ssl) {
1323 ssl->signed_cert_timestamps_enabled = 1;
1324 return 1;
1325}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001326
Adam Langleyfcf25832014-12-18 17:42:32 -08001327void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
1328 ctx->ocsp_stapling_enabled = 1;
1329}
David Benjamin6c7aed02014-08-27 16:42:38 -04001330
Adam Langleyfcf25832014-12-18 17:42:32 -08001331int SSL_enable_ocsp_stapling(SSL *ssl) {
1332 ssl->ocsp_stapling_enabled = 1;
1333 return 1;
1334}
David Benjamin6c7aed02014-08-27 16:42:38 -04001335
Adam Langleyfcf25832014-12-18 17:42:32 -08001336void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
1337 size_t *out_len) {
1338 SSL_SESSION *session = ssl->session;
Adam Langley3cb50e02014-08-26 14:00:31 -07001339
Adam Langleyfcf25832014-12-18 17:42:32 -08001340 *out_len = 0;
1341 *out = NULL;
1342 if (ssl->server || !session || !session->tlsext_signed_cert_timestamp_list) {
1343 return;
1344 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001345
Adam Langleyfcf25832014-12-18 17:42:32 -08001346 *out = session->tlsext_signed_cert_timestamp_list;
1347 *out_len = session->tlsext_signed_cert_timestamp_list_length;
1348}
David Benjamin6c7aed02014-08-27 16:42:38 -04001349
Adam Langleyfcf25832014-12-18 17:42:32 -08001350void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
1351 size_t *out_len) {
1352 SSL_SESSION *session = ssl->session;
1353
1354 *out_len = 0;
1355 *out = NULL;
1356 if (ssl->server || !session || !session->ocsp_response) {
1357 return;
1358 }
1359 *out = session->ocsp_response;
1360 *out_len = session->ocsp_response_length;
1361}
David Benjamin6c7aed02014-08-27 16:42:38 -04001362
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001363int SSL_CTX_set_ocsp_response(SSL_CTX *ctx, const uint8_t *response,
1364 size_t response_len) {
1365 OPENSSL_free(ctx->ocsp_response);
1366 ctx->ocsp_response_length = 0;
1367
1368 ctx->ocsp_response = BUF_memdup(response, response_len);
1369 if (ctx->ocsp_response == NULL) {
1370 return 0;
1371 }
1372 ctx->ocsp_response_length = response_len;
1373
1374 return 1;
1375}
1376
Adam Langley95c29f32014-06-20 12:00:00 -07001377/* SSL_select_next_proto implements the standard protocol selection. It is
1378 * expected that this function is called from the callback set by
1379 * SSL_CTX_set_next_proto_select_cb.
1380 *
1381 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1382 * strings. The length byte itself is not included in the length. A byte
1383 * string of length 0 is invalid. No byte string may be truncated.
1384 *
1385 * The current, but experimental algorithm for selecting the protocol is:
1386 *
1387 * 1) If the server doesn't support NPN then this is indicated to the
1388 * callback. In this case, the client application has to abort the connection
1389 * or have a default application level protocol.
1390 *
1391 * 2) If the server supports NPN, but advertises an empty list then the
1392 * client selects the first protcol in its list, but indicates via the
1393 * API that this fallback case was enacted.
1394 *
1395 * 3) Otherwise, the client finds the first protocol in the server's list
1396 * that it supports and selects this protocol. This is because it's
1397 * assumed that the server has better information about which protocol
1398 * a client should use.
1399 *
1400 * 4) If the client doesn't support any of the server's advertised
1401 * protocols, then this is treated the same as case 2.
1402 *
1403 * It returns either
1404 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1405 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1406 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001407int SSL_select_next_proto(uint8_t **out, uint8_t *outlen, const uint8_t *server,
1408 unsigned int server_len, const uint8_t *client,
1409 unsigned int client_len) {
1410 unsigned int i, j;
1411 const uint8_t *result;
1412 int status = OPENSSL_NPN_UNSUPPORTED;
Adam Langley95c29f32014-06-20 12:00:00 -07001413
Adam Langleyfcf25832014-12-18 17:42:32 -08001414 /* For each protocol in server preference order, see if we support it. */
1415 for (i = 0; i < server_len;) {
1416 for (j = 0; j < client_len;) {
1417 if (server[i] == client[j] &&
1418 memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
1419 /* We found a match */
1420 result = &server[i];
1421 status = OPENSSL_NPN_NEGOTIATED;
1422 goto found;
1423 }
1424 j += client[j];
1425 j++;
1426 }
1427 i += server[i];
1428 i++;
1429 }
Adam Langley95c29f32014-06-20 12:00:00 -07001430
Adam Langleyfcf25832014-12-18 17:42:32 -08001431 /* There's no overlap between our protocols and the server's list. */
1432 result = client;
1433 status = OPENSSL_NPN_NO_OVERLAP;
Adam Langley95c29f32014-06-20 12:00:00 -07001434
Adam Langleyfcf25832014-12-18 17:42:32 -08001435found:
1436 *out = (uint8_t *)result + 1;
1437 *outlen = result[0];
1438 return status;
1439}
Adam Langley95c29f32014-06-20 12:00:00 -07001440
Adam Langley95c29f32014-06-20 12:00:00 -07001441/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
1442 * requested protocol for this connection and returns 0. If the client didn't
1443 * request any protocol, then *data is set to NULL.
1444 *
1445 * Note that the client can request any protocol it chooses. The value returned
1446 * from this function need not be a member of the list of supported protocols
Adam Langleyfcf25832014-12-18 17:42:32 -08001447 * provided by the callback. */
1448void SSL_get0_next_proto_negotiated(const SSL *s, const uint8_t **data,
1449 unsigned *len) {
1450 *data = s->next_proto_negotiated;
1451 if (!*data) {
1452 *len = 0;
1453 } else {
1454 *len = s->next_proto_negotiated_len;
1455 }
Adam Langley95c29f32014-06-20 12:00:00 -07001456}
1457
1458/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
1459 * TLS server needs a list of supported protocols for Next Protocol
1460 * Negotiation. The returned list must be in wire format. The list is returned
1461 * by setting |out| to point to it and |outlen| to its length. This memory will
1462 * not be modified, but one should assume that the SSL* keeps a reference to
1463 * it.
1464 *
Adam Langleyfcf25832014-12-18 17:42:32 -08001465 * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise.
1466 * Otherwise, no such extension will be included in the ServerHello. */
1467void SSL_CTX_set_next_protos_advertised_cb(
1468 SSL_CTX *ctx,
1469 int (*cb)(SSL *ssl, const uint8_t **out, unsigned int *outlen, void *arg),
1470 void *arg) {
1471 ctx->next_protos_advertised_cb = cb;
1472 ctx->next_protos_advertised_cb_arg = arg;
1473}
Adam Langley95c29f32014-06-20 12:00:00 -07001474
1475/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1476 * client needs to select a protocol from the server's provided list. |out|
1477 * must be set to point to the selected protocol (which may be within |in|).
1478 * The length of the protocol name must be written into |outlen|. The server's
1479 * advertised protocols are provided in |in| and |inlen|. The callback can
1480 * assume that |in| is syntactically valid.
1481 *
1482 * The client must select a protocol. It is fatal to the connection if this
1483 * callback returns a value other than SSL_TLSEXT_ERR_OK.
1484 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001485void SSL_CTX_set_next_proto_select_cb(
1486 SSL_CTX *ctx, int (*cb)(SSL *s, uint8_t **out, uint8_t *outlen,
1487 const uint8_t *in, unsigned int inlen, void *arg),
1488 void *arg) {
1489 ctx->next_proto_select_cb = cb;
1490 ctx->next_proto_select_cb_arg = arg;
1491}
Adam Langley95c29f32014-06-20 12:00:00 -07001492
Adam Langleyfcf25832014-12-18 17:42:32 -08001493int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
1494 unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001495 OPENSSL_free(ctx->alpn_client_proto_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001496 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1497 if (!ctx->alpn_client_proto_list) {
1498 return 1;
1499 }
1500 ctx->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001501
Adam Langleyfcf25832014-12-18 17:42:32 -08001502 return 0;
1503}
Adam Langley95c29f32014-06-20 12:00:00 -07001504
Adam Langleyfcf25832014-12-18 17:42:32 -08001505int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001506 OPENSSL_free(ssl->alpn_client_proto_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001507 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1508 if (!ssl->alpn_client_proto_list) {
1509 return 1;
1510 }
1511 ssl->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001512
Adam Langleyfcf25832014-12-18 17:42:32 -08001513 return 0;
1514}
Adam Langley95c29f32014-06-20 12:00:00 -07001515
1516/* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
1517 * during ClientHello processing in order to select an ALPN protocol from the
1518 * client's list of offered protocols. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001519void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
1520 int (*cb)(SSL *ssl, const uint8_t **out,
1521 uint8_t *outlen, const uint8_t *in,
1522 unsigned int inlen, void *arg),
1523 void *arg) {
1524 ctx->alpn_select_cb = cb;
1525 ctx->alpn_select_cb_arg = arg;
1526}
Adam Langley95c29f32014-06-20 12:00:00 -07001527
1528/* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
1529 * On return it sets |*data| to point to |*len| bytes of protocol name (not
1530 * including the leading length-prefix byte). If the server didn't respond with
1531 * a negotiated protocol then |*len| will be zero. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001532void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **data,
1533 unsigned *len) {
1534 *data = NULL;
1535 if (ssl->s3) {
1536 *data = ssl->s3->alpn_selected;
1537 }
1538 if (*data == NULL) {
1539 *len = 0;
1540 } else {
1541 *len = ssl->s3->alpn_selected_len;
1542 }
1543}
Adam Langley95c29f32014-06-20 12:00:00 -07001544
David Benjamincfd248b2015-04-03 11:02:24 -04001545int SSL_export_keying_material(SSL *s, uint8_t *out, size_t out_len,
1546 const char *label, size_t label_len,
1547 const uint8_t *context, size_t context_len,
1548 int use_context) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001549 if (s->version < TLS1_VERSION) {
David Benjaminc565ebb2015-04-03 04:06:36 -04001550 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001551 }
Adam Langley95c29f32014-06-20 12:00:00 -07001552
David Benjamincfd248b2015-04-03 11:02:24 -04001553 return s->enc_method->export_keying_material(
1554 s, out, out_len, label, label_len, context, context_len, use_context);
Adam Langleyfcf25832014-12-18 17:42:32 -08001555}
Adam Langley95c29f32014-06-20 12:00:00 -07001556
Adam Langleyfcf25832014-12-18 17:42:32 -08001557static uint32_t ssl_session_hash(const SSL_SESSION *a) {
1558 uint32_t hash =
1559 ((uint32_t)a->session_id[0]) ||
1560 ((uint32_t)a->session_id[1] << 8) ||
1561 ((uint32_t)a->session_id[2] << 16) ||
1562 ((uint32_t)a->session_id[3] << 24);
Adam Langley95c29f32014-06-20 12:00:00 -07001563
Adam Langleyfcf25832014-12-18 17:42:32 -08001564 return hash;
1565}
Adam Langley95c29f32014-06-20 12:00:00 -07001566
1567/* NB: If this function (or indeed the hash function which uses a sort of
1568 * coarser function than this one) is changed, ensure
1569 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1570 * able to construct an SSL_SESSION that will collide with any existing session
1571 * with a matching session ID. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001572static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
1573 if (a->ssl_version != b->ssl_version) {
1574 return 1;
1575 }
Adam Langley95c29f32014-06-20 12:00:00 -07001576
Adam Langleyfcf25832014-12-18 17:42:32 -08001577 if (a->session_id_length != b->session_id_length) {
1578 return 1;
1579 }
Adam Langley95c29f32014-06-20 12:00:00 -07001580
Adam Langleyfcf25832014-12-18 17:42:32 -08001581 return memcmp(a->session_id, b->session_id, a->session_id_length);
1582}
Adam Langley95c29f32014-06-20 12:00:00 -07001583
David Benjamin4831c332015-05-16 11:43:13 -04001584SSL_CTX *SSL_CTX_new(const SSL_METHOD *method) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001585 SSL_CTX *ret = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001586
David Benjamin4831c332015-05-16 11:43:13 -04001587 if (method == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001588 OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_METHOD_PASSED);
Adam Langleyfcf25832014-12-18 17:42:32 -08001589 return NULL;
1590 }
Adam Langley95c29f32014-06-20 12:00:00 -07001591
Adam Langleyfcf25832014-12-18 17:42:32 -08001592 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001593 OPENSSL_PUT_ERROR(SSL, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
Adam Langleyfcf25832014-12-18 17:42:32 -08001594 goto err;
1595 }
Adam Langley95c29f32014-06-20 12:00:00 -07001596
Adam Langleyfcf25832014-12-18 17:42:32 -08001597 ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1598 if (ret == NULL) {
1599 goto err;
1600 }
Adam Langley95c29f32014-06-20 12:00:00 -07001601
Adam Langleyfcf25832014-12-18 17:42:32 -08001602 memset(ret, 0, sizeof(SSL_CTX));
Adam Langley95c29f32014-06-20 12:00:00 -07001603
David Benjamin4831c332015-05-16 11:43:13 -04001604 ret->method = method->method;
Adam Langley95c29f32014-06-20 12:00:00 -07001605
Adam Langley4bdb6e42015-05-15 15:29:21 -07001606 CRYPTO_MUTEX_init(&ret->lock);
1607
Adam Langleyfcf25832014-12-18 17:42:32 -08001608 ret->cert_store = NULL;
1609 ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
1610 ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1611 ret->session_cache_head = NULL;
1612 ret->session_cache_tail = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001613
Adam Langleyfcf25832014-12-18 17:42:32 -08001614 /* We take the system default */
1615 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001616
Adam Langleyfcf25832014-12-18 17:42:32 -08001617 ret->new_session_cb = 0;
1618 ret->remove_session_cb = 0;
1619 ret->get_session_cb = 0;
1620 ret->generate_session_id = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001621
Adam Langleyfcf25832014-12-18 17:42:32 -08001622 ret->references = 1;
1623 ret->quiet_shutdown = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001624
Adam Langleyfcf25832014-12-18 17:42:32 -08001625 ret->info_callback = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001626
Adam Langleyfcf25832014-12-18 17:42:32 -08001627 ret->app_verify_callback = 0;
1628 ret->app_verify_arg = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001629
Adam Langleyfcf25832014-12-18 17:42:32 -08001630 ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
Adam Langleyfcf25832014-12-18 17:42:32 -08001631 ret->msg_callback = 0;
1632 ret->msg_callback_arg = NULL;
1633 ret->verify_mode = SSL_VERIFY_NONE;
1634 ret->sid_ctx_length = 0;
1635 ret->default_verify_callback = NULL;
1636 ret->cert = ssl_cert_new();
1637 if (ret->cert == NULL) {
1638 goto err;
1639 }
Adam Langley95c29f32014-06-20 12:00:00 -07001640
Adam Langleyfcf25832014-12-18 17:42:32 -08001641 ret->default_passwd_callback = 0;
1642 ret->default_passwd_callback_userdata = NULL;
1643 ret->client_cert_cb = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001644
Adam Langleyfcf25832014-12-18 17:42:32 -08001645 ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
1646 if (ret->sessions == NULL) {
1647 goto err;
1648 }
1649 ret->cert_store = X509_STORE_new();
1650 if (ret->cert_store == NULL) {
1651 goto err;
1652 }
Adam Langley95c29f32014-06-20 12:00:00 -07001653
Adam Langleyfcf25832014-12-18 17:42:32 -08001654 ssl_create_cipher_list(ret->method, &ret->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001655 &ret->cipher_list_by_id, SSL_DEFAULT_CIPHER_LIST);
Adam Langleyfcf25832014-12-18 17:42:32 -08001656 if (ret->cipher_list == NULL ||
1657 sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001658 OPENSSL_PUT_ERROR(SSL, SSL_R_LIBRARY_HAS_NO_CIPHERS);
Adam Langleyfcf25832014-12-18 17:42:32 -08001659 goto err2;
1660 }
Adam Langley95c29f32014-06-20 12:00:00 -07001661
Adam Langleyfcf25832014-12-18 17:42:32 -08001662 ret->param = X509_VERIFY_PARAM_new();
1663 if (!ret->param) {
1664 goto err;
1665 }
Adam Langley95c29f32014-06-20 12:00:00 -07001666
Adam Langleyfcf25832014-12-18 17:42:32 -08001667 ret->client_CA = sk_X509_NAME_new_null();
1668 if (ret->client_CA == NULL) {
1669 goto err;
1670 }
Adam Langley95c29f32014-06-20 12:00:00 -07001671
David Benjamin9f33fc62015-04-15 17:29:53 -04001672 CRYPTO_new_ex_data(&g_ex_data_class_ssl_ctx, ret, &ret->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001673
Adam Langleyfcf25832014-12-18 17:42:32 -08001674 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
David Benjamin422d3a42014-08-20 11:09:03 -04001675
Adam Langleyfcf25832014-12-18 17:42:32 -08001676 ret->tlsext_servername_callback = 0;
1677 ret->tlsext_servername_arg = NULL;
1678 /* Setup RFC4507 ticket keys */
1679 if (!RAND_bytes(ret->tlsext_tick_key_name, 16) ||
1680 !RAND_bytes(ret->tlsext_tick_hmac_key, 16) ||
1681 !RAND_bytes(ret->tlsext_tick_aes_key, 16)) {
1682 ret->options |= SSL_OP_NO_TICKET;
1683 }
Adam Langley95c29f32014-06-20 12:00:00 -07001684
Adam Langleyfcf25832014-12-18 17:42:32 -08001685 ret->next_protos_advertised_cb = 0;
1686 ret->next_proto_select_cb = 0;
1687 ret->psk_identity_hint = NULL;
1688 ret->psk_client_callback = NULL;
1689 ret->psk_server_callback = NULL;
1690
1691 /* Default is to connect to non-RI servers. When RI is more widely deployed
1692 * might change this. */
1693 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
1694
1695 /* Lock the SSL_CTX to the specified version, for compatibility with legacy
1696 * uses of SSL_METHOD. */
David Benjamin4831c332015-05-16 11:43:13 -04001697 if (method->version != 0) {
1698 SSL_CTX_set_max_version(ret, method->version);
1699 SSL_CTX_set_min_version(ret, method->version);
Adam Langleyfcf25832014-12-18 17:42:32 -08001700 }
1701
1702 return ret;
1703
Adam Langley95c29f32014-06-20 12:00:00 -07001704err:
David Benjamin3570d732015-06-29 00:28:17 -04001705 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -07001706err2:
David Benjamin2755a3e2015-04-22 16:17:58 -04001707 SSL_CTX_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -08001708 return NULL;
1709}
Adam Langley95c29f32014-06-20 12:00:00 -07001710
David Benjamin4fcc2e22015-04-22 12:58:16 -04001711void SSL_CTX_free(SSL_CTX *ctx) {
1712 if (ctx == NULL ||
Adam Langley0b5e3902015-05-15 13:08:38 -07001713 !CRYPTO_refcount_dec_and_test_zero(&ctx->references)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001714 return;
1715 }
Adam Langley95c29f32014-06-20 12:00:00 -07001716
David Benjamin2755a3e2015-04-22 16:17:58 -04001717 X509_VERIFY_PARAM_free(ctx->param);
Adam Langley95c29f32014-06-20 12:00:00 -07001718
Adam Langleyfcf25832014-12-18 17:42:32 -08001719 /* Free internal session cache. However: the remove_cb() may reference the
1720 * ex_data of SSL_CTX, thus the ex_data store can only be removed after the
1721 * sessions were flushed. As the ex_data handling routines might also touch
1722 * the session cache, the most secure solution seems to be: empty (flush) the
1723 * cache, then free ex_data, then finally free the cache. (See ticket
1724 * [openssl.org #212].) */
David Benjamin2755a3e2015-04-22 16:17:58 -04001725 SSL_CTX_flush_sessions(ctx, 0);
Adam Langley95c29f32014-06-20 12:00:00 -07001726
David Benjamin4fcc2e22015-04-22 12:58:16 -04001727 CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, ctx, &ctx->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001728
Adam Langley4bdb6e42015-05-15 15:29:21 -07001729 CRYPTO_MUTEX_cleanup(&ctx->lock);
David Benjamin2755a3e2015-04-22 16:17:58 -04001730 lh_SSL_SESSION_free(ctx->sessions);
1731 X509_STORE_free(ctx->cert_store);
1732 ssl_cipher_preference_list_free(ctx->cipher_list);
1733 sk_SSL_CIPHER_free(ctx->cipher_list_by_id);
1734 ssl_cipher_preference_list_free(ctx->cipher_list_tls11);
1735 ssl_cert_free(ctx->cert);
Adam Langley09505632015-07-30 18:10:13 -07001736 sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->client_custom_extensions,
1737 SSL_CUSTOM_EXTENSION_free);
1738 sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->server_custom_extensions,
1739 SSL_CUSTOM_EXTENSION_free);
David Benjamin2755a3e2015-04-22 16:17:58 -04001740 sk_X509_NAME_pop_free(ctx->client_CA, X509_NAME_free);
David Benjamin2755a3e2015-04-22 16:17:58 -04001741 sk_SRTP_PROTECTION_PROFILE_free(ctx->srtp_profiles);
1742 OPENSSL_free(ctx->psk_identity_hint);
David Benjamin2755a3e2015-04-22 16:17:58 -04001743 OPENSSL_free(ctx->tlsext_ellipticcurvelist);
1744 OPENSSL_free(ctx->alpn_client_proto_list);
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001745 OPENSSL_free(ctx->ocsp_response);
David Benjamin2755a3e2015-04-22 16:17:58 -04001746 EVP_PKEY_free(ctx->tlsext_channel_id_private);
1747 BIO_free(ctx->keylog_bio);
Adam Langley95c29f32014-06-20 12:00:00 -07001748
David Benjamin4fcc2e22015-04-22 12:58:16 -04001749 OPENSSL_free(ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08001750}
Adam Langley95c29f32014-06-20 12:00:00 -07001751
Adam Langleyfcf25832014-12-18 17:42:32 -08001752void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) {
1753 ctx->default_passwd_callback = cb;
1754}
Adam Langley95c29f32014-06-20 12:00:00 -07001755
Adam Langleyfcf25832014-12-18 17:42:32 -08001756void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u) {
1757 ctx->default_passwd_callback_userdata = u;
1758}
Adam Langley95c29f32014-06-20 12:00:00 -07001759
Adam Langleyfcf25832014-12-18 17:42:32 -08001760void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
1761 int (*cb)(X509_STORE_CTX *, void *),
1762 void *arg) {
1763 ctx->app_verify_callback = cb;
1764 ctx->app_verify_arg = arg;
1765}
Adam Langley95c29f32014-06-20 12:00:00 -07001766
Adam Langleyfcf25832014-12-18 17:42:32 -08001767void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
1768 int (*cb)(int, X509_STORE_CTX *)) {
1769 ctx->verify_mode = mode;
1770 ctx->default_verify_callback = cb;
1771}
Adam Langley95c29f32014-06-20 12:00:00 -07001772
Adam Langleyfcf25832014-12-18 17:42:32 -08001773void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
1774 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
1775}
Adam Langley1258b6a2014-06-20 12:00:00 -07001776
David Benjamin7481d392015-07-05 19:38:46 -04001777void SSL_CTX_set_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl, void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08001778 void *arg) {
David Benjamin7481d392015-07-05 19:38:46 -04001779 ssl_cert_set_cert_cb(ctx->cert, cb, arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08001780}
David Benjamin859ec3c2014-09-02 16:29:36 -04001781
David Benjamin7481d392015-07-05 19:38:46 -04001782void SSL_set_cert_cb(SSL *ssl, int (*cb)(SSL *ssl, void *arg), void *arg) {
1783 ssl_cert_set_cert_cb(ssl->cert, cb, arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08001784}
Adam Langley95c29f32014-06-20 12:00:00 -07001785
David Benjamin107db582015-04-08 00:41:59 -04001786void ssl_get_compatible_server_ciphers(SSL *s, uint32_t *out_mask_k,
1787 uint32_t *out_mask_a) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001788 CERT *c = s->cert;
David Benjamind1d80782015-07-05 11:54:09 -04001789 int have_rsa_cert = 0, dh_tmp;
David Benjamin107db582015-04-08 00:41:59 -04001790 uint32_t mask_k, mask_a;
David Benjamind1d80782015-07-05 11:54:09 -04001791 int have_ecc_cert = 0, ecdsa_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001792 X509 *x;
Adam Langley95c29f32014-06-20 12:00:00 -07001793
Adam Langleyfcf25832014-12-18 17:42:32 -08001794 dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001795
nagendra modadugu601448a2015-07-24 09:31:31 -07001796 if (s->cert->x509 != NULL && ssl_has_private_key(s)) {
1797 if (ssl_private_key_type(s) == EVP_PKEY_RSA) {
David Benjamind1d80782015-07-05 11:54:09 -04001798 have_rsa_cert = 1;
nagendra modadugu601448a2015-07-24 09:31:31 -07001799 } else if (ssl_private_key_type(s) == EVP_PKEY_EC) {
David Benjamind1d80782015-07-05 11:54:09 -04001800 have_ecc_cert = 1;
1801 }
1802 }
nagendra modadugu601448a2015-07-24 09:31:31 -07001803
Adam Langleyfcf25832014-12-18 17:42:32 -08001804 mask_k = 0;
1805 mask_a = 0;
David Benjaminf31e6812014-11-13 18:05:55 -05001806
Adam Langleyfcf25832014-12-18 17:42:32 -08001807 if (dh_tmp) {
David Benjamin7061e282015-03-19 11:10:48 -04001808 mask_k |= SSL_kDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001809 }
David Benjaminbb20f522015-07-04 17:18:14 -04001810 if (have_rsa_cert) {
1811 mask_k |= SSL_kRSA;
Adam Langleyfcf25832014-12-18 17:42:32 -08001812 mask_a |= SSL_aRSA;
1813 }
Adam Langley95c29f32014-06-20 12:00:00 -07001814
Adam Langleyfcf25832014-12-18 17:42:32 -08001815 /* An ECC certificate may be usable for ECDSA cipher suites depending on the
1816 * key usage extension and on the client's curve preferences. */
1817 if (have_ecc_cert) {
David Benjamind1d80782015-07-05 11:54:09 -04001818 x = c->x509;
Adam Langleyfcf25832014-12-18 17:42:32 -08001819 /* This call populates extension flags (ex_flags). */
1820 X509_check_purpose(x, -1, 0);
1821 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE)
1822 ? (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)
1823 : 1;
1824 if (!tls1_check_ec_cert(s, x)) {
1825 ecdsa_ok = 0;
1826 }
1827 if (ecdsa_ok) {
1828 mask_a |= SSL_aECDSA;
1829 }
1830 }
Adam Langley95c29f32014-06-20 12:00:00 -07001831
Adam Langleyfcf25832014-12-18 17:42:32 -08001832 /* If we are considering an ECC cipher suite that uses an ephemeral EC
1833 * key, check it. */
David Benjamindd978782015-04-24 15:20:13 -04001834 if (tls1_check_ec_tmp_key(s)) {
David Benjamin7061e282015-03-19 11:10:48 -04001835 mask_k |= SSL_kECDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001836 }
Adam Langley95c29f32014-06-20 12:00:00 -07001837
Adam Langleyfcf25832014-12-18 17:42:32 -08001838 /* PSK requires a server callback. */
1839 if (s->psk_server_callback != NULL) {
1840 mask_k |= SSL_kPSK;
1841 mask_a |= SSL_aPSK;
1842 }
Adam Langley95c29f32014-06-20 12:00:00 -07001843
Adam Langleyfcf25832014-12-18 17:42:32 -08001844 *out_mask_k = mask_k;
1845 *out_mask_a = mask_a;
1846}
Adam Langley95c29f32014-06-20 12:00:00 -07001847
Adam Langleyfcf25832014-12-18 17:42:32 -08001848void ssl_update_cache(SSL *s, int mode) {
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001849 /* Never cache sessions with empty session IDs. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001850 if (s->session->session_id_length == 0) {
1851 return;
1852 }
Adam Langley95c29f32014-06-20 12:00:00 -07001853
David Benjamin95d31822015-06-15 19:53:32 -04001854 int has_new_session = !s->hit;
1855 if (!s->server && s->tlsext_ticket_expected) {
1856 /* A client may see new sessions on abbreviated handshakes if the server
1857 * decides to renew the ticket. Once the handshake is completed, it should
1858 * be inserted into the cache. */
1859 has_new_session = 1;
1860 }
1861
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001862 SSL_CTX *ctx = s->initial_ctx;
David Benjamin95d31822015-06-15 19:53:32 -04001863 if ((ctx->session_cache_mode & mode) == mode && has_new_session &&
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001864 ((ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) ||
1865 SSL_CTX_add_session(ctx, s->session)) &&
1866 ctx->new_session_cb != NULL) {
1867 /* Note: |new_session_cb| is called whether the internal session cache is
1868 * used or not. */
1869 if (!ctx->new_session_cb(s, SSL_SESSION_up_ref(s->session))) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001870 SSL_SESSION_free(s->session);
1871 }
1872 }
Adam Langley95c29f32014-06-20 12:00:00 -07001873
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001874 if (!(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR) &&
1875 !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) &&
1876 (ctx->session_cache_mode & mode) == mode) {
1877 /* Automatically flush the internal session cache every 255 connections. */
1878 int flush_cache = 0;
Adam Langley4bdb6e42015-05-15 15:29:21 -07001879 CRYPTO_MUTEX_lock_write(&ctx->lock);
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001880 ctx->handshakes_since_cache_flush++;
1881 if (ctx->handshakes_since_cache_flush >= 255) {
1882 flush_cache = 1;
1883 ctx->handshakes_since_cache_flush = 0;
1884 }
Adam Langley4bdb6e42015-05-15 15:29:21 -07001885 CRYPTO_MUTEX_unlock(&ctx->lock);
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001886
1887 if (flush_cache) {
1888 SSL_CTX_flush_sessions(ctx, (unsigned long)time(NULL));
Adam Langleyfcf25832014-12-18 17:42:32 -08001889 }
1890 }
1891}
Adam Langley95c29f32014-06-20 12:00:00 -07001892
David Benjamin1a5c50f2015-03-11 16:22:37 -04001893int SSL_get_error(const SSL *s, int ret_code) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001894 int reason;
David Benjamin1a5c50f2015-03-11 16:22:37 -04001895 uint32_t err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001896 BIO *bio;
Adam Langley95c29f32014-06-20 12:00:00 -07001897
David Benjamin1a5c50f2015-03-11 16:22:37 -04001898 if (ret_code > 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001899 return SSL_ERROR_NONE;
1900 }
Adam Langley95c29f32014-06-20 12:00:00 -07001901
Adam Langleyfcf25832014-12-18 17:42:32 -08001902 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
1903 * where we do encode the error */
David Benjamin1a5c50f2015-03-11 16:22:37 -04001904 err = ERR_peek_error();
1905 if (err != 0) {
1906 if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001907 return SSL_ERROR_SYSCALL;
1908 }
1909 return SSL_ERROR_SSL;
1910 }
Adam Langley95c29f32014-06-20 12:00:00 -07001911
David Benjamin1a5c50f2015-03-11 16:22:37 -04001912 if (ret_code == 0) {
David Benjamin9a38e922015-01-22 16:06:11 -05001913 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
1914 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) {
1915 /* The socket was cleanly shut down with a close_notify. */
1916 return SSL_ERROR_ZERO_RETURN;
1917 }
1918 /* An EOF was observed which violates the protocol, and the underlying
1919 * transport does not participate in the error queue. Bubble up to the
1920 * caller. */
1921 return SSL_ERROR_SYSCALL;
Adam Langleyfcf25832014-12-18 17:42:32 -08001922 }
Adam Langleyb2ce0582014-06-20 12:00:00 -07001923
Adam Langleyfcf25832014-12-18 17:42:32 -08001924 if (SSL_want_session(s)) {
1925 return SSL_ERROR_PENDING_SESSION;
1926 }
Adam Langley95c29f32014-06-20 12:00:00 -07001927
Adam Langleyfcf25832014-12-18 17:42:32 -08001928 if (SSL_want_certificate(s)) {
1929 return SSL_ERROR_PENDING_CERTIFICATE;
1930 }
Adam Langley95c29f32014-06-20 12:00:00 -07001931
Adam Langleyfcf25832014-12-18 17:42:32 -08001932 if (SSL_want_read(s)) {
1933 bio = SSL_get_rbio(s);
1934 if (BIO_should_read(bio)) {
1935 return SSL_ERROR_WANT_READ;
1936 }
Adam Langley95c29f32014-06-20 12:00:00 -07001937
Adam Langleyfcf25832014-12-18 17:42:32 -08001938 if (BIO_should_write(bio)) {
1939 /* This one doesn't make too much sense ... We never try to write to the
1940 * rbio, and an application program where rbio and wbio are separate
1941 * couldn't even know what it should wait for. However if we ever set
1942 * s->rwstate incorrectly (so that we have SSL_want_read(s) instead of
1943 * SSL_want_write(s)) and rbio and wbio *are* the same, this test works
1944 * around that bug; so it might be safer to keep it. */
1945 return SSL_ERROR_WANT_WRITE;
1946 }
Adam Langley95c29f32014-06-20 12:00:00 -07001947
Adam Langleyfcf25832014-12-18 17:42:32 -08001948 if (BIO_should_io_special(bio)) {
1949 reason = BIO_get_retry_reason(bio);
1950 if (reason == BIO_RR_CONNECT) {
1951 return SSL_ERROR_WANT_CONNECT;
1952 }
Adam Langley95c29f32014-06-20 12:00:00 -07001953
Adam Langleyfcf25832014-12-18 17:42:32 -08001954 if (reason == BIO_RR_ACCEPT) {
1955 return SSL_ERROR_WANT_ACCEPT;
1956 }
Adam Langley95c29f32014-06-20 12:00:00 -07001957
Adam Langleyfcf25832014-12-18 17:42:32 -08001958 return SSL_ERROR_SYSCALL; /* unknown */
1959 }
1960 }
Adam Langley95c29f32014-06-20 12:00:00 -07001961
Adam Langleyfcf25832014-12-18 17:42:32 -08001962 if (SSL_want_write(s)) {
1963 bio = SSL_get_wbio(s);
1964 if (BIO_should_write(bio)) {
1965 return SSL_ERROR_WANT_WRITE;
1966 }
Adam Langley95c29f32014-06-20 12:00:00 -07001967
Adam Langleyfcf25832014-12-18 17:42:32 -08001968 if (BIO_should_read(bio)) {
1969 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
1970 return SSL_ERROR_WANT_READ;
1971 }
Adam Langley95c29f32014-06-20 12:00:00 -07001972
Adam Langleyfcf25832014-12-18 17:42:32 -08001973 if (BIO_should_io_special(bio)) {
1974 reason = BIO_get_retry_reason(bio);
1975 if (reason == BIO_RR_CONNECT) {
1976 return SSL_ERROR_WANT_CONNECT;
1977 }
Adam Langley95c29f32014-06-20 12:00:00 -07001978
Adam Langleyfcf25832014-12-18 17:42:32 -08001979 if (reason == BIO_RR_ACCEPT) {
1980 return SSL_ERROR_WANT_ACCEPT;
1981 }
Adam Langley95c29f32014-06-20 12:00:00 -07001982
Adam Langleyfcf25832014-12-18 17:42:32 -08001983 return SSL_ERROR_SYSCALL;
1984 }
1985 }
Adam Langley95c29f32014-06-20 12:00:00 -07001986
Adam Langleyfcf25832014-12-18 17:42:32 -08001987 if (SSL_want_x509_lookup(s)) {
1988 return SSL_ERROR_WANT_X509_LOOKUP;
1989 }
Adam Langley95c29f32014-06-20 12:00:00 -07001990
Adam Langleyfcf25832014-12-18 17:42:32 -08001991 if (SSL_want_channel_id_lookup(s)) {
1992 return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
1993 }
Adam Langley0f4746e2014-08-13 12:26:32 -07001994
David Benjaminb4d65fd2015-05-29 17:11:21 -04001995 if (SSL_want_private_key_operation(s)) {
1996 return SSL_ERROR_WANT_PRIVATE_KEY_OPERATION;
1997 }
1998
Adam Langleyfcf25832014-12-18 17:42:32 -08001999 return SSL_ERROR_SYSCALL;
2000}
Adam Langley0f4746e2014-08-13 12:26:32 -07002001
Adam Langleyfcf25832014-12-18 17:42:32 -08002002int SSL_do_handshake(SSL *s) {
2003 int ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002004
Adam Langleyfcf25832014-12-18 17:42:32 -08002005 if (s->handshake_func == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04002006 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_TYPE_NOT_SET);
Adam Langleyfcf25832014-12-18 17:42:32 -08002007 return -1;
2008 }
Adam Langley95c29f32014-06-20 12:00:00 -07002009
Adam Langleyfcf25832014-12-18 17:42:32 -08002010 if (SSL_in_init(s)) {
2011 ret = s->handshake_func(s);
2012 }
2013 return ret;
2014}
Adam Langley95c29f32014-06-20 12:00:00 -07002015
David Benjamin4831c332015-05-16 11:43:13 -04002016void SSL_set_accept_state(SSL *ssl) {
2017 ssl->server = 1;
2018 ssl->shutdown = 0;
David Benjamin8ec88102015-05-15 23:40:13 -04002019 ssl->state = SSL_ST_ACCEPT;
David Benjamin4831c332015-05-16 11:43:13 -04002020 ssl->handshake_func = ssl->method->ssl_accept;
Adam Langleyfcf25832014-12-18 17:42:32 -08002021 /* clear the current cipher */
David Benjamin4831c332015-05-16 11:43:13 -04002022 ssl_clear_cipher_ctx(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08002023}
Adam Langley95c29f32014-06-20 12:00:00 -07002024
David Benjamin4831c332015-05-16 11:43:13 -04002025void SSL_set_connect_state(SSL *ssl) {
2026 ssl->server = 0;
2027 ssl->shutdown = 0;
David Benjamin8ec88102015-05-15 23:40:13 -04002028 ssl->state = SSL_ST_CONNECT;
David Benjamin4831c332015-05-16 11:43:13 -04002029 ssl->handshake_func = ssl->method->ssl_connect;
Adam Langleyfcf25832014-12-18 17:42:32 -08002030 /* clear the current cipher */
David Benjamin4831c332015-05-16 11:43:13 -04002031 ssl_clear_cipher_ctx(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08002032}
Adam Langley95c29f32014-06-20 12:00:00 -07002033
Adam Langleyfcf25832014-12-18 17:42:32 -08002034static const char *ssl_get_version(int version) {
2035 switch (version) {
2036 case TLS1_2_VERSION:
2037 return "TLSv1.2";
Adam Langley95c29f32014-06-20 12:00:00 -07002038
Adam Langleyfcf25832014-12-18 17:42:32 -08002039 case TLS1_1_VERSION:
2040 return "TLSv1.1";
Adam Langley95c29f32014-06-20 12:00:00 -07002041
Adam Langleyfcf25832014-12-18 17:42:32 -08002042 case TLS1_VERSION:
2043 return "TLSv1";
Adam Langley95c29f32014-06-20 12:00:00 -07002044
Adam Langleyfcf25832014-12-18 17:42:32 -08002045 case SSL3_VERSION:
2046 return "SSLv3";
Adam Langley95c29f32014-06-20 12:00:00 -07002047
David Benjamin1c722b72015-04-20 13:53:10 -04002048 case DTLS1_VERSION:
2049 return "DTLSv1";
2050
2051 case DTLS1_2_VERSION:
2052 return "DTLSv1.2";
2053
Adam Langleyfcf25832014-12-18 17:42:32 -08002054 default:
2055 return "unknown";
2056 }
2057}
Adam Langley95c29f32014-06-20 12:00:00 -07002058
Adam Langleyfcf25832014-12-18 17:42:32 -08002059const char *SSL_get_version(const SSL *s) {
2060 return ssl_get_version(s->version);
2061}
Adam Langley95c29f32014-06-20 12:00:00 -07002062
Adam Langleyfcf25832014-12-18 17:42:32 -08002063const char *SSL_SESSION_get_version(const SSL_SESSION *sess) {
2064 return ssl_get_version(sess->ssl_version);
2065}
Adam Langley95c29f32014-06-20 12:00:00 -07002066
Sigbjorn Vik2b23d242015-06-29 15:07:26 +02002067const char* SSL_get_curve_name(uint16_t curve_id) {
2068 return tls1_ec_curve_id2name(curve_id);
2069}
2070
Adam Langleyfcf25832014-12-18 17:42:32 -08002071void ssl_clear_cipher_ctx(SSL *s) {
David Benjamin31a07792015-03-03 14:20:26 -05002072 SSL_AEAD_CTX_free(s->aead_read_ctx);
2073 s->aead_read_ctx = NULL;
2074 SSL_AEAD_CTX_free(s->aead_write_ctx);
2075 s->aead_write_ctx = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002076}
Adam Langley95c29f32014-06-20 12:00:00 -07002077
Adam Langleyfcf25832014-12-18 17:42:32 -08002078X509 *SSL_get_certificate(const SSL *s) {
2079 if (s->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04002080 return s->cert->x509;
Adam Langleyfcf25832014-12-18 17:42:32 -08002081 }
2082
2083 return NULL;
2084}
2085
2086EVP_PKEY *SSL_get_privatekey(const SSL *s) {
2087 if (s->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04002088 return s->cert->privatekey;
Adam Langleyfcf25832014-12-18 17:42:32 -08002089 }
2090
2091 return NULL;
2092}
2093
2094X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
2095 if (ctx->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04002096 return ctx->cert->x509;
Adam Langleyfcf25832014-12-18 17:42:32 -08002097 }
2098
2099 return NULL;
2100}
2101
2102EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
2103 if (ctx->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04002104 return ctx->cert->privatekey;
Adam Langleyfcf25832014-12-18 17:42:32 -08002105 }
2106
2107 return NULL;
2108}
2109
2110const SSL_CIPHER *SSL_get_current_cipher(const SSL *s) {
David Benjamina07c0fc2015-05-13 13:19:42 -04002111 if (s->aead_write_ctx == NULL) {
2112 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002113 }
David Benjamina07c0fc2015-05-13 13:19:42 -04002114 return s->aead_write_ctx->cipher;
Adam Langleyfcf25832014-12-18 17:42:32 -08002115}
2116
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07002117const COMP_METHOD *SSL_get_current_compression(SSL *s) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002118
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07002119const COMP_METHOD *SSL_get_current_expansion(SSL *s) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002120
2121int ssl_init_wbio_buffer(SSL *s, int push) {
2122 BIO *bbio;
2123
2124 if (s->bbio == NULL) {
2125 bbio = BIO_new(BIO_f_buffer());
2126 if (bbio == NULL) {
2127 return 0;
2128 }
2129 s->bbio = bbio;
2130 } else {
2131 bbio = s->bbio;
2132 if (s->bbio == s->wbio) {
2133 s->wbio = BIO_pop(s->wbio);
2134 }
2135 }
2136
2137 BIO_reset(bbio);
2138 if (!BIO_set_read_buffer_size(bbio, 1)) {
David Benjamin3570d732015-06-29 00:28:17 -04002139 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -08002140 return 0;
2141 }
2142
2143 if (push) {
2144 if (s->wbio != bbio) {
2145 s->wbio = BIO_push(bbio, s->wbio);
2146 }
2147 } else {
2148 if (s->wbio == bbio) {
2149 s->wbio = BIO_pop(bbio);
2150 }
2151 }
2152
2153 return 1;
2154}
2155
2156void ssl_free_wbio_buffer(SSL *s) {
2157 if (s->bbio == NULL) {
2158 return;
2159 }
2160
2161 if (s->bbio == s->wbio) {
2162 /* remove buffering */
2163 s->wbio = BIO_pop(s->wbio);
2164 }
2165
2166 BIO_free(s->bbio);
2167 s->bbio = NULL;
2168}
2169
2170void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
2171 ctx->quiet_shutdown = mode;
2172}
2173
2174int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
2175 return ctx->quiet_shutdown;
2176}
2177
2178void SSL_set_quiet_shutdown(SSL *s, int mode) { s->quiet_shutdown = mode; }
2179
2180int SSL_get_quiet_shutdown(const SSL *s) { return s->quiet_shutdown; }
2181
2182void SSL_set_shutdown(SSL *s, int mode) { s->shutdown = mode; }
2183
2184int SSL_get_shutdown(const SSL *s) { return s->shutdown; }
2185
2186int SSL_version(const SSL *s) { return s->version; }
2187
2188SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
2189
2190SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
2191 if (ssl->ctx == ctx) {
2192 return ssl->ctx;
2193 }
2194
2195 if (ctx == NULL) {
2196 ctx = ssl->initial_ctx;
2197 }
2198
David Benjamin2755a3e2015-04-22 16:17:58 -04002199 ssl_cert_free(ssl->cert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002200 ssl->cert = ssl_cert_dup(ctx->cert);
David Benjamin2755a3e2015-04-22 16:17:58 -04002201
Adam Langley0b5e3902015-05-15 13:08:38 -07002202 CRYPTO_refcount_inc(&ctx->references);
David Benjamin2755a3e2015-04-22 16:17:58 -04002203 SSL_CTX_free(ssl->ctx); /* decrement reference count */
Adam Langleyfcf25832014-12-18 17:42:32 -08002204 ssl->ctx = ctx;
2205
2206 ssl->sid_ctx_length = ctx->sid_ctx_length;
2207 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2208 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2209
2210 return ssl->ctx;
2211}
2212
2213int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
2214 return X509_STORE_set_default_paths(ctx->cert_store);
2215}
Adam Langley95c29f32014-06-20 12:00:00 -07002216
2217int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
Adam Langleyfcf25832014-12-18 17:42:32 -08002218 const char *CApath) {
2219 return X509_STORE_load_locations(ctx->cert_store, CAfile, CApath);
2220}
Adam Langley95c29f32014-06-20 12:00:00 -07002221
2222void SSL_set_info_callback(SSL *ssl,
Adam Langleyfcf25832014-12-18 17:42:32 -08002223 void (*cb)(const SSL *ssl, int type, int val)) {
2224 ssl->info_callback = cb;
2225}
Adam Langley95c29f32014-06-20 12:00:00 -07002226
Adam Langleyfcf25832014-12-18 17:42:32 -08002227void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/, int /*type*/,
2228 int /*val*/) {
2229 return ssl->info_callback;
2230}
Adam Langley95c29f32014-06-20 12:00:00 -07002231
Adam Langleyfcf25832014-12-18 17:42:32 -08002232int SSL_state(const SSL *ssl) { return ssl->state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002233
David Benjaminece089c2015-05-15 23:52:42 -04002234void SSL_set_state(SSL *ssl, int state) { }
Adam Langley95c29f32014-06-20 12:00:00 -07002235
Adam Langleyfcf25832014-12-18 17:42:32 -08002236void SSL_set_verify_result(SSL *ssl, long arg) { ssl->verify_result = arg; }
Adam Langley95c29f32014-06-20 12:00:00 -07002237
Adam Langleyfcf25832014-12-18 17:42:32 -08002238long SSL_get_verify_result(const SSL *ssl) { return ssl->verify_result; }
Adam Langley95c29f32014-06-20 12:00:00 -07002239
Adam Langleyfcf25832014-12-18 17:42:32 -08002240int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2241 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002242 int index;
2243 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp,
2244 new_func, dup_func, free_func)) {
2245 return -1;
2246 }
2247 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002248}
Adam Langley95c29f32014-06-20 12:00:00 -07002249
Adam Langleyfcf25832014-12-18 17:42:32 -08002250int SSL_set_ex_data(SSL *s, int idx, void *arg) {
2251 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2252}
Adam Langley95c29f32014-06-20 12:00:00 -07002253
Adam Langleyfcf25832014-12-18 17:42:32 -08002254void *SSL_get_ex_data(const SSL *s, int idx) {
2255 return CRYPTO_get_ex_data(&s->ex_data, idx);
2256}
Adam Langley95c29f32014-06-20 12:00:00 -07002257
Adam Langleyfcf25832014-12-18 17:42:32 -08002258int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2259 CRYPTO_EX_dup *dup_func,
2260 CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002261 int index;
2262 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp,
2263 new_func, dup_func, free_func)) {
2264 return -1;
2265 }
2266 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002267}
Adam Langley95c29f32014-06-20 12:00:00 -07002268
Adam Langleyfcf25832014-12-18 17:42:32 -08002269int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg) {
2270 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2271}
Adam Langley95c29f32014-06-20 12:00:00 -07002272
Adam Langleyfcf25832014-12-18 17:42:32 -08002273void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx) {
2274 return CRYPTO_get_ex_data(&s->ex_data, idx);
2275}
Adam Langley95c29f32014-06-20 12:00:00 -07002276
Adam Langleyfcf25832014-12-18 17:42:32 -08002277X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
2278 return ctx->cert_store;
2279}
Adam Langley95c29f32014-06-20 12:00:00 -07002280
Adam Langleyfcf25832014-12-18 17:42:32 -08002281void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
David Benjamin2755a3e2015-04-22 16:17:58 -04002282 X509_STORE_free(ctx->cert_store);
Adam Langleyfcf25832014-12-18 17:42:32 -08002283 ctx->cert_store = store;
2284}
Adam Langley95c29f32014-06-20 12:00:00 -07002285
Adam Langleyfcf25832014-12-18 17:42:32 -08002286int SSL_want(const SSL *s) { return s->rwstate; }
Adam Langley95c29f32014-06-20 12:00:00 -07002287
Adam Langleyfcf25832014-12-18 17:42:32 -08002288void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
2289 RSA *(*cb)(SSL *ssl, int is_export,
2290 int keylength)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002291}
Adam Langley95c29f32014-06-20 12:00:00 -07002292
Adam Langleyfcf25832014-12-18 17:42:32 -08002293void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
2294 int keylength)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002295}
2296
2297void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
David Benjamin59015c32015-04-26 13:13:55 -04002298 DH *(*callback)(SSL *ssl, int is_export,
2299 int keylength)) {
2300 ctx->cert->dh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002301}
2302
David Benjamin59015c32015-04-26 13:13:55 -04002303void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*callback)(SSL *ssl, int is_export,
2304 int keylength)) {
2305 ssl->cert->dh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002306}
2307
2308void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,
David Benjamindd978782015-04-24 15:20:13 -04002309 EC_KEY *(*callback)(SSL *ssl, int is_export,
2310 int keylength)) {
David Benjamin59015c32015-04-26 13:13:55 -04002311 ctx->cert->ecdh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002312}
2313
2314void SSL_set_tmp_ecdh_callback(SSL *ssl,
David Benjamindd978782015-04-24 15:20:13 -04002315 EC_KEY *(*callback)(SSL *ssl, int is_export,
2316 int keylength)) {
David Benjamin59015c32015-04-26 13:13:55 -04002317 ssl->cert->ecdh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002318}
2319
2320int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
2321 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
David Benjamin3570d732015-06-29 00:28:17 -04002322 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -08002323 return 0;
2324 }
2325
David Benjamin2755a3e2015-04-22 16:17:58 -04002326 OPENSSL_free(ctx->psk_identity_hint);
Adam Langleyfcf25832014-12-18 17:42:32 -08002327
2328 if (identity_hint != NULL) {
2329 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2330 if (ctx->psk_identity_hint == NULL) {
2331 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002332 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002333 } else {
2334 ctx->psk_identity_hint = NULL;
2335 }
Adam Langley95c29f32014-06-20 12:00:00 -07002336
Adam Langleyfcf25832014-12-18 17:42:32 -08002337 return 1;
2338}
2339
2340int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint) {
2341 if (s == NULL) {
2342 return 0;
2343 }
2344
2345 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
David Benjamin3570d732015-06-29 00:28:17 -04002346 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -08002347 return 0;
2348 }
2349
2350 /* Clear currently configured hint, if any. */
David Benjamin2755a3e2015-04-22 16:17:58 -04002351 OPENSSL_free(s->psk_identity_hint);
2352 s->psk_identity_hint = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002353
2354 if (identity_hint != NULL) {
2355 s->psk_identity_hint = BUF_strdup(identity_hint);
2356 if (s->psk_identity_hint == NULL) {
2357 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002358 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002359 }
Adam Langley95c29f32014-06-20 12:00:00 -07002360
Adam Langleyfcf25832014-12-18 17:42:32 -08002361 return 1;
2362}
Adam Langley95c29f32014-06-20 12:00:00 -07002363
Adam Langleyfcf25832014-12-18 17:42:32 -08002364const char *SSL_get_psk_identity_hint(const SSL *s) {
2365 if (s == NULL) {
2366 return NULL;
2367 }
2368 return s->psk_identity_hint;
2369}
Adam Langley95c29f32014-06-20 12:00:00 -07002370
Adam Langleyfcf25832014-12-18 17:42:32 -08002371const char *SSL_get_psk_identity(const SSL *s) {
2372 if (s == NULL || s->session == NULL) {
2373 return NULL;
2374 }
Adam Langley95c29f32014-06-20 12:00:00 -07002375
Adam Langleyfcf25832014-12-18 17:42:32 -08002376 return s->session->psk_identity;
2377}
Adam Langley95c29f32014-06-20 12:00:00 -07002378
Adam Langleyfcf25832014-12-18 17:42:32 -08002379void SSL_set_psk_client_callback(
2380 SSL *s, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2381 unsigned int max_identity_len, uint8_t *psk,
2382 unsigned int max_psk_len)) {
2383 s->psk_client_callback = cb;
2384}
Adam Langley95c29f32014-06-20 12:00:00 -07002385
Adam Langleyfcf25832014-12-18 17:42:32 -08002386void SSL_CTX_set_psk_client_callback(
2387 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2388 unsigned int max_identity_len,
2389 uint8_t *psk, unsigned int max_psk_len)) {
2390 ctx->psk_client_callback = cb;
2391}
Adam Langley95c29f32014-06-20 12:00:00 -07002392
Adam Langleyfcf25832014-12-18 17:42:32 -08002393void SSL_set_psk_server_callback(
2394 SSL *s, unsigned int (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
2395 unsigned int max_psk_len)) {
2396 s->psk_server_callback = cb;
2397}
Adam Langley95c29f32014-06-20 12:00:00 -07002398
Adam Langleyfcf25832014-12-18 17:42:32 -08002399void SSL_CTX_set_psk_server_callback(
2400 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *identity,
2401 uint8_t *psk, unsigned int max_psk_len)) {
2402 ctx->psk_server_callback = cb;
2403}
Adam Langley95c29f32014-06-20 12:00:00 -07002404
Adam Langleyfcf25832014-12-18 17:42:32 -08002405void SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) {
2406 ctx->min_version = version;
2407}
Adam Langley95c29f32014-06-20 12:00:00 -07002408
Adam Langleyfcf25832014-12-18 17:42:32 -08002409void SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) {
2410 ctx->max_version = version;
2411}
Adam Langley0289c732014-06-20 12:00:00 -07002412
Adam Langleyfcf25832014-12-18 17:42:32 -08002413void SSL_set_min_version(SSL *ssl, uint16_t version) {
2414 ssl->min_version = version;
2415}
Adam Langley0289c732014-06-20 12:00:00 -07002416
Adam Langleyfcf25832014-12-18 17:42:32 -08002417void SSL_set_max_version(SSL *ssl, uint16_t version) {
2418 ssl->max_version = version;
2419}
Adam Langley95c29f32014-06-20 12:00:00 -07002420
Adam Langleyfcf25832014-12-18 17:42:32 -08002421void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
2422 void (*cb)(int write_p, int version,
2423 int content_type, const void *buf,
2424 size_t len, SSL *ssl, void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002425 ctx->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002426}
David Benjamin61ecccf2015-05-05 09:44:51 -04002427
2428void SSL_CTX_set_msg_callback_arg(SSL_CTX *ctx, void *arg) {
2429 ctx->msg_callback_arg = arg;
2430}
2431
Adam Langleyfcf25832014-12-18 17:42:32 -08002432void SSL_set_msg_callback(SSL *ssl,
2433 void (*cb)(int write_p, int version, int content_type,
2434 const void *buf, size_t len, SSL *ssl,
2435 void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002436 ssl->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002437}
Adam Langley95c29f32014-06-20 12:00:00 -07002438
David Benjamin61ecccf2015-05-05 09:44:51 -04002439void SSL_set_msg_callback_arg(SSL *ssl, void *arg) {
2440 ssl->msg_callback_arg = arg;
2441}
2442
Adam Langleyfcf25832014-12-18 17:42:32 -08002443void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio) {
David Benjamin2755a3e2015-04-22 16:17:58 -04002444 BIO_free(ctx->keylog_bio);
Adam Langleyfcf25832014-12-18 17:42:32 -08002445 ctx->keylog_bio = keylog_bio;
2446}
Adam Langley95c29f32014-06-20 12:00:00 -07002447
Adam Langleyfcf25832014-12-18 17:42:32 -08002448static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
2449 static const char hextable[] = "0123456789abcdef";
2450 uint8_t *out;
2451 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002452
Adam Langleyfcf25832014-12-18 17:42:32 -08002453 if (!CBB_add_space(cbb, &out, in_len * 2)) {
2454 return 0;
2455 }
Adam Langley95c29f32014-06-20 12:00:00 -07002456
Adam Langleyfcf25832014-12-18 17:42:32 -08002457 for (i = 0; i < in_len; i++) {
2458 *(out++) = (uint8_t)hextable[in[i] >> 4];
2459 *(out++) = (uint8_t)hextable[in[i] & 0xf];
2460 }
Adam Langley95c29f32014-06-20 12:00:00 -07002461
Adam Langleyfcf25832014-12-18 17:42:32 -08002462 return 1;
2463}
David Benjamin859ec3c2014-09-02 16:29:36 -04002464
2465int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -08002466 const uint8_t *encrypted_premaster,
2467 size_t encrypted_premaster_len,
2468 const uint8_t *premaster,
2469 size_t premaster_len) {
2470 BIO *bio = ctx->keylog_bio;
2471 CBB cbb;
2472 uint8_t *out;
2473 size_t out_len;
2474 int ret;
David Benjamin859ec3c2014-09-02 16:29:36 -04002475
Adam Langleyfcf25832014-12-18 17:42:32 -08002476 if (bio == NULL) {
2477 return 1;
2478 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002479
Adam Langleyfcf25832014-12-18 17:42:32 -08002480 if (encrypted_premaster_len < 8) {
David Benjamin3570d732015-06-29 00:28:17 -04002481 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08002482 return 0;
2483 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002484
David Benjamina8653202015-06-28 01:26:10 -04002485 CBB_zero(&cbb);
2486 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len * 2 + 1) ||
2487 !CBB_add_bytes(&cbb, (const uint8_t *)"RSA ", 4) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08002488 /* Only the first 8 bytes of the encrypted premaster secret are
2489 * logged. */
2490 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
2491 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2492 !cbb_add_hex(&cbb, premaster, premaster_len) ||
2493 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2494 !CBB_finish(&cbb, &out, &out_len)) {
2495 CBB_cleanup(&cbb);
2496 return 0;
2497 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002498
Adam Langley4bdb6e42015-05-15 15:29:21 -07002499 CRYPTO_MUTEX_lock_write(&ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -08002500 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
Adam Langley4bdb6e42015-05-15 15:29:21 -07002501 CRYPTO_MUTEX_unlock(&ctx->lock);
David Benjamin859ec3c2014-09-02 16:29:36 -04002502
Adam Langleyfcf25832014-12-18 17:42:32 -08002503 OPENSSL_free(out);
2504 return ret;
Adam Langley95f22882014-06-20 12:00:00 -07002505}
2506
Adam Langleyfcf25832014-12-18 17:42:32 -08002507int ssl_ctx_log_master_secret(SSL_CTX *ctx, const uint8_t *client_random,
2508 size_t client_random_len, const uint8_t *master,
2509 size_t master_len) {
2510 BIO *bio = ctx->keylog_bio;
2511 CBB cbb;
2512 uint8_t *out;
2513 size_t out_len;
2514 int ret;
Adam Langleyadb739e2014-06-20 12:00:00 -07002515
Adam Langleyfcf25832014-12-18 17:42:32 -08002516 if (bio == NULL) {
2517 return 1;
2518 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002519
Adam Langleyfcf25832014-12-18 17:42:32 -08002520 if (client_random_len != 32) {
David Benjamin3570d732015-06-29 00:28:17 -04002521 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08002522 return 0;
2523 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002524
David Benjamina8653202015-06-28 01:26:10 -04002525 CBB_zero(&cbb);
2526 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len * 2 + 1) ||
2527 !CBB_add_bytes(&cbb, (const uint8_t *)"CLIENT_RANDOM ", 14) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08002528 !cbb_add_hex(&cbb, client_random, 32) ||
2529 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2530 !cbb_add_hex(&cbb, master, master_len) ||
2531 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2532 !CBB_finish(&cbb, &out, &out_len)) {
2533 CBB_cleanup(&cbb);
2534 return 0;
2535 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002536
Adam Langley4bdb6e42015-05-15 15:29:21 -07002537 CRYPTO_MUTEX_lock_write(&ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -08002538 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
Adam Langley4bdb6e42015-05-15 15:29:21 -07002539 CRYPTO_MUTEX_unlock(&ctx->lock);
David Benjamine99e9122014-12-11 01:46:01 -05002540
Adam Langleyfcf25832014-12-18 17:42:32 -08002541 OPENSSL_free(out);
2542 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -07002543}
2544
David Benjamined7c4752015-02-16 19:16:46 -05002545int SSL_in_false_start(const SSL *s) {
2546 return s->s3->tmp.in_false_start;
2547}
2548
Adam Langleyfcf25832014-12-18 17:42:32 -08002549int SSL_cutthrough_complete(const SSL *s) {
David Benjamined7c4752015-02-16 19:16:46 -05002550 return SSL_in_false_start(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002551}
Adam Langley95c29f32014-06-20 12:00:00 -07002552
Adam Langleyfcf25832014-12-18 17:42:32 -08002553void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
2554 size_t *ssl_session_size) {
2555 *ssl_size = sizeof(SSL);
2556 *ssl_ctx_size = sizeof(SSL_CTX);
2557 *ssl_session_size = sizeof(SSL_SESSION);
2558}
Feng Lu41aa3252014-11-21 22:47:56 -08002559
David Benjamined7c4752015-02-16 19:16:46 -05002560int ssl3_can_false_start(const SSL *s) {
David Benjamin195dc782015-02-19 13:27:05 -05002561 const SSL_CIPHER *const cipher = SSL_get_current_cipher(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002562
Adam Langleye631d962015-04-01 13:11:01 -07002563 /* False Start only for TLS 1.2 with an ECDHE+AEAD cipher and ALPN or NPN. */
David Benjamin195dc782015-02-19 13:27:05 -05002564 return !SSL_IS_DTLS(s) &&
2565 SSL_version(s) >= TLS1_2_VERSION &&
2566 (s->s3->alpn_selected || s->s3->next_proto_neg_seen) &&
2567 cipher != NULL &&
Adam Langleye631d962015-04-01 13:11:01 -07002568 cipher->algorithm_mkey == SSL_kECDHE &&
David Benjamin195dc782015-02-19 13:27:05 -05002569 (cipher->algorithm_enc == SSL_AES128GCM ||
2570 cipher->algorithm_enc == SSL_AES256GCM ||
2571 cipher->algorithm_enc == SSL_CHACHA20POLY1305);
Adam Langleyfcf25832014-12-18 17:42:32 -08002572}
2573
2574const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version) {
2575 switch (version) {
2576 case SSL3_VERSION:
2577 return &SSLv3_enc_data;
2578
2579 case TLS1_VERSION:
2580 return &TLSv1_enc_data;
2581
David Benjamin9e13e1a2015-03-05 01:56:32 -05002582 case DTLS1_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002583 case TLS1_1_VERSION:
2584 return &TLSv1_1_enc_data;
2585
David Benjamin9e13e1a2015-03-05 01:56:32 -05002586 case DTLS1_2_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002587 case TLS1_2_VERSION:
2588 return &TLSv1_2_enc_data;
2589
Adam Langleyfcf25832014-12-18 17:42:32 -08002590 default:
2591 return NULL;
2592 }
2593}
2594
2595uint16_t ssl3_get_max_server_version(const SSL *s) {
2596 uint16_t max_version;
2597
2598 if (SSL_IS_DTLS(s)) {
2599 max_version = (s->max_version != 0) ? s->max_version : DTLS1_2_VERSION;
2600 if (!(s->options & SSL_OP_NO_DTLSv1_2) && DTLS1_2_VERSION >= max_version) {
2601 return DTLS1_2_VERSION;
2602 }
2603 if (!(s->options & SSL_OP_NO_DTLSv1) && DTLS1_VERSION >= max_version) {
2604 return DTLS1_VERSION;
2605 }
2606 return 0;
2607 }
2608
2609 max_version = (s->max_version != 0) ? s->max_version : TLS1_2_VERSION;
2610 if (!(s->options & SSL_OP_NO_TLSv1_2) && TLS1_2_VERSION <= max_version) {
2611 return TLS1_2_VERSION;
2612 }
2613 if (!(s->options & SSL_OP_NO_TLSv1_1) && TLS1_1_VERSION <= max_version) {
2614 return TLS1_1_VERSION;
2615 }
2616 if (!(s->options & SSL_OP_NO_TLSv1) && TLS1_VERSION <= max_version) {
2617 return TLS1_VERSION;
2618 }
2619 if (!(s->options & SSL_OP_NO_SSLv3) && SSL3_VERSION <= max_version) {
2620 return SSL3_VERSION;
2621 }
2622 return 0;
2623}
2624
2625uint16_t ssl3_get_mutual_version(SSL *s, uint16_t client_version) {
2626 uint16_t version = 0;
2627
2628 if (SSL_IS_DTLS(s)) {
2629 /* Clamp client_version to max_version. */
2630 if (s->max_version != 0 && client_version < s->max_version) {
2631 client_version = s->max_version;
2632 }
2633
2634 if (client_version <= DTLS1_2_VERSION && !(s->options & SSL_OP_NO_DTLSv1_2)) {
2635 version = DTLS1_2_VERSION;
2636 } else if (client_version <= DTLS1_VERSION &&
David Benjaminb18f0242015-03-10 18:30:08 -04002637 !(s->options & SSL_OP_NO_DTLSv1)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002638 version = DTLS1_VERSION;
2639 }
2640
2641 /* Check against min_version. */
2642 if (version != 0 && s->min_version != 0 && version > s->min_version) {
2643 return 0;
2644 }
2645 return version;
2646 } else {
2647 /* Clamp client_version to max_version. */
2648 if (s->max_version != 0 && client_version > s->max_version) {
2649 client_version = s->max_version;
2650 }
2651
2652 if (client_version >= TLS1_2_VERSION && !(s->options & SSL_OP_NO_TLSv1_2)) {
2653 version = TLS1_2_VERSION;
2654 } else if (client_version >= TLS1_1_VERSION &&
2655 !(s->options & SSL_OP_NO_TLSv1_1)) {
2656 version = TLS1_1_VERSION;
2657 } else if (client_version >= TLS1_VERSION && !(s->options & SSL_OP_NO_TLSv1)) {
2658 version = TLS1_VERSION;
2659 } else if (client_version >= SSL3_VERSION && !(s->options & SSL_OP_NO_SSLv3)) {
2660 version = SSL3_VERSION;
2661 }
2662
2663 /* Check against min_version. */
2664 if (version != 0 && s->min_version != 0 && version < s->min_version) {
2665 return 0;
2666 }
2667 return version;
2668 }
2669}
2670
2671uint16_t ssl3_get_max_client_version(SSL *s) {
David Benjamin123a8fd2015-04-26 14:16:41 -04002672 uint32_t options = s->options;
Adam Langleyfcf25832014-12-18 17:42:32 -08002673 uint16_t version = 0;
2674
2675 /* OpenSSL's API for controlling versions entails blacklisting individual
2676 * protocols. This has two problems. First, on the client, the protocol can
2677 * only express a contiguous range of versions. Second, a library consumer
2678 * trying to set a maximum version cannot disable protocol versions that get
2679 * added in a future version of the library.
2680 *
2681 * To account for both of these, OpenSSL interprets the client-side bitmask
2682 * as a min/max range by picking the lowest contiguous non-empty range of
2683 * enabled protocols. Note that this means it is impossible to set a maximum
2684 * version of TLS 1.2 in a future-proof way.
2685 *
2686 * By this scheme, the maximum version is the lowest version V such that V is
2687 * enabled and V+1 is disabled or unimplemented. */
2688 if (SSL_IS_DTLS(s)) {
2689 if (!(options & SSL_OP_NO_DTLSv1_2)) {
2690 version = DTLS1_2_VERSION;
2691 }
2692 if (!(options & SSL_OP_NO_DTLSv1) && (options & SSL_OP_NO_DTLSv1_2)) {
2693 version = DTLS1_VERSION;
2694 }
2695 if (s->max_version != 0 && version < s->max_version) {
2696 version = s->max_version;
2697 }
2698 } else {
2699 if (!(options & SSL_OP_NO_TLSv1_2)) {
2700 version = TLS1_2_VERSION;
2701 }
2702 if (!(options & SSL_OP_NO_TLSv1_1) && (options & SSL_OP_NO_TLSv1_2)) {
2703 version = TLS1_1_VERSION;
2704 }
2705 if (!(options & SSL_OP_NO_TLSv1) && (options & SSL_OP_NO_TLSv1_1)) {
2706 version = TLS1_VERSION;
2707 }
2708 if (!(options & SSL_OP_NO_SSLv3) && (options & SSL_OP_NO_TLSv1)) {
2709 version = SSL3_VERSION;
2710 }
2711 if (s->max_version != 0 && version > s->max_version) {
2712 version = s->max_version;
2713 }
2714 }
2715
2716 return version;
2717}
2718
2719int ssl3_is_version_enabled(SSL *s, uint16_t version) {
2720 if (SSL_IS_DTLS(s)) {
2721 if (s->max_version != 0 && version < s->max_version) {
2722 return 0;
2723 }
2724 if (s->min_version != 0 && version > s->min_version) {
2725 return 0;
2726 }
2727
2728 switch (version) {
2729 case DTLS1_VERSION:
2730 return !(s->options & SSL_OP_NO_DTLSv1);
2731
2732 case DTLS1_2_VERSION:
2733 return !(s->options & SSL_OP_NO_DTLSv1_2);
2734
2735 default:
2736 return 0;
2737 }
2738 } else {
2739 if (s->max_version != 0 && version > s->max_version) {
2740 return 0;
2741 }
2742 if (s->min_version != 0 && version < s->min_version) {
2743 return 0;
2744 }
2745
2746 switch (version) {
2747 case SSL3_VERSION:
2748 return !(s->options & SSL_OP_NO_SSLv3);
2749
2750 case TLS1_VERSION:
2751 return !(s->options & SSL_OP_NO_TLSv1);
2752
2753 case TLS1_1_VERSION:
2754 return !(s->options & SSL_OP_NO_TLSv1_1);
2755
2756 case TLS1_2_VERSION:
2757 return !(s->options & SSL_OP_NO_TLSv1_2);
2758
2759 default:
2760 return 0;
2761 }
2762 }
2763}
2764
David Benjaminea72bd02014-12-21 21:27:41 -05002765uint16_t ssl3_version_from_wire(SSL *s, uint16_t wire_version) {
2766 if (!SSL_IS_DTLS(s)) {
2767 return wire_version;
2768 }
2769
2770 uint16_t tls_version = ~wire_version;
2771 uint16_t version = tls_version + 0x0201;
2772 /* If either component overflowed, clamp it so comparisons still work. */
2773 if ((version >> 8) < (tls_version >> 8)) {
2774 version = 0xff00 | (version & 0xff);
2775 }
2776 if ((version & 0xff) < (tls_version & 0xff)) {
2777 version = (version & 0xff00) | 0xff;
2778 }
2779 /* DTLS 1.0 maps to TLS 1.1, not TLS 1.0. */
2780 if (version == TLS1_VERSION) {
2781 version = TLS1_1_VERSION;
2782 }
2783 return version;
2784}
2785
Adam Langleyfcf25832014-12-18 17:42:32 -08002786int SSL_cache_hit(SSL *s) { return s->hit; }
2787
2788int SSL_is_server(SSL *s) { return s->server; }
2789
Adam Langley524e7172015-02-20 16:04:00 -08002790void SSL_CTX_set_dos_protection_cb(
2791 SSL_CTX *ctx, int (*cb)(const struct ssl_early_callback_ctx *)) {
2792 ctx->dos_protection_cb = cb;
2793}
2794
David Benjaminb16346b2015-04-08 19:16:58 -04002795void SSL_set_reject_peer_renegotiations(SSL *s, int reject) {
David Benjamin897e5e02015-05-12 17:03:54 -04002796 s->accept_peer_renegotiations = !reject;
David Benjaminb16346b2015-04-08 19:16:58 -04002797}
2798
Adam Langley3f92d212015-02-20 15:32:52 -08002799int SSL_get_rc4_state(const SSL *ssl, const RC4_KEY **read_key,
2800 const RC4_KEY **write_key) {
2801 if (ssl->aead_read_ctx == NULL || ssl->aead_write_ctx == NULL) {
2802 return 0;
2803 }
2804
2805 return EVP_AEAD_CTX_get_rc4_state(&ssl->aead_read_ctx->ctx, read_key) &&
2806 EVP_AEAD_CTX_get_rc4_state(&ssl->aead_write_ctx->ctx, write_key);
2807}
David Benjaminda881e92015-04-26 14:45:04 -04002808
Adam Langleyaf0e32c2015-06-03 09:57:23 -07002809int SSL_get_tls_unique(const SSL *ssl, uint8_t *out, size_t *out_len,
2810 size_t max_out) {
2811 /* The tls-unique value is the first Finished message in the handshake, which
2812 * is the client's in a full handshake and the server's for a resumption. See
2813 * https://tools.ietf.org/html/rfc5929#section-3.1. */
2814 const uint8_t *finished = ssl->s3->previous_client_finished;
2815 size_t finished_len = ssl->s3->previous_client_finished_len;
2816 if (ssl->hit) {
2817 /* tls-unique is broken for resumed sessions unless EMS is used. */
2818 if (!ssl->session->extended_master_secret) {
2819 goto err;
2820 }
2821 finished = ssl->s3->previous_server_finished;
2822 finished_len = ssl->s3->previous_server_finished_len;
2823 }
2824
2825 if (!ssl->s3->initial_handshake_complete ||
2826 ssl->version < TLS1_VERSION) {
2827 goto err;
2828 }
2829
2830 *out_len = finished_len;
2831 if (finished_len > max_out) {
2832 *out_len = max_out;
2833 }
2834
2835 memcpy(out, finished, *out_len);
2836 return 1;
2837
2838err:
2839 *out_len = 0;
2840 memset(out, 0, max_out);
2841 return 0;
2842}
2843
David Benjaminda881e92015-04-26 14:45:04 -04002844int SSL_CTX_sess_connect(const SSL_CTX *ctx) { return 0; }
2845int SSL_CTX_sess_connect_good(const SSL_CTX *ctx) { return 0; }
2846int SSL_CTX_sess_connect_renegotiate(const SSL_CTX *ctx) { return 0; }
2847int SSL_CTX_sess_accept(const SSL_CTX *ctx) { return 0; }
2848int SSL_CTX_sess_accept_renegotiate(const SSL_CTX *ctx) { return 0; }
2849int SSL_CTX_sess_accept_good(const SSL_CTX *ctx) { return 0; }
2850int SSL_CTX_sess_hits(const SSL_CTX *ctx) { return 0; }
2851int SSL_CTX_sess_cb_hits(const SSL_CTX *ctx) { return 0; }
2852int SSL_CTX_sess_misses(const SSL_CTX *ctx) { return 0; }
2853int SSL_CTX_sess_timeouts(const SSL_CTX *ctx) { return 0; }
2854int SSL_CTX_sess_cache_full(const SSL_CTX *ctx) { return 0; }
Matt Braithwaite4838d8a2015-08-20 13:20:03 -07002855void ERR_load_SSL_strings(void) {}