blob: b2eeb371fbec8c15aa6e87a26b6f93e70541b8b9 [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 Langley0b5e3902015-05-15 13:08:38 -0700294 CRYPTO_refcount_inc(&ctx->references);
Adam Langleyfcf25832014-12-18 17:42:32 -0800295 s->initial_ctx = ctx;
Adam Langley95c29f32014-06-20 12:00:00 -0700296
Adam Langleyfcf25832014-12-18 17:42:32 -0800297 if (ctx->tlsext_ellipticcurvelist) {
298 s->tlsext_ellipticcurvelist =
299 BUF_memdup(ctx->tlsext_ellipticcurvelist,
300 ctx->tlsext_ellipticcurvelist_length * 2);
301 if (!s->tlsext_ellipticcurvelist) {
302 goto err;
303 }
304 s->tlsext_ellipticcurvelist_length = ctx->tlsext_ellipticcurvelist_length;
305 }
Adam Langley95c29f32014-06-20 12:00:00 -0700306
Adam Langleyfcf25832014-12-18 17:42:32 -0800307 if (s->ctx->alpn_client_proto_list) {
308 s->alpn_client_proto_list = BUF_memdup(s->ctx->alpn_client_proto_list,
309 s->ctx->alpn_client_proto_list_len);
310 if (s->alpn_client_proto_list == NULL) {
311 goto err;
312 }
313 s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;
314 }
Adam Langley95c29f32014-06-20 12:00:00 -0700315
Adam Langleyfcf25832014-12-18 17:42:32 -0800316 s->verify_result = X509_V_OK;
317 s->method = ctx->method;
Adam Langley95c29f32014-06-20 12:00:00 -0700318
Adam Langleyfcf25832014-12-18 17:42:32 -0800319 if (!s->method->ssl_new(s)) {
320 goto err;
321 }
322 s->enc_method = ssl3_get_enc_method(s->version);
323 assert(s->enc_method != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700324
David Benjamin62fd1622015-01-11 13:30:01 -0500325 s->rwstate = SSL_NOTHING;
Adam Langley95c29f32014-06-20 12:00:00 -0700326
David Benjamin9f33fc62015-04-15 17:29:53 -0400327 CRYPTO_new_ex_data(&g_ex_data_class_ssl, s, &s->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700328
Adam Langleyfcf25832014-12-18 17:42:32 -0800329 s->psk_identity_hint = NULL;
330 if (ctx->psk_identity_hint) {
331 s->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
332 if (s->psk_identity_hint == NULL) {
333 goto err;
334 }
335 }
336 s->psk_client_callback = ctx->psk_client_callback;
337 s->psk_server_callback = ctx->psk_server_callback;
Adam Langley95c29f32014-06-20 12:00:00 -0700338
David Benjamin02ddbfd2015-01-11 13:09:11 -0500339 s->tlsext_channel_id_enabled = ctx->tlsext_channel_id_enabled;
340 if (ctx->tlsext_channel_id_private) {
David Benjamin9a10f8f2015-05-05 22:22:40 -0400341 s->tlsext_channel_id_private =
342 EVP_PKEY_up_ref(ctx->tlsext_channel_id_private);
David Benjamin02ddbfd2015-01-11 13:09:11 -0500343 }
344
Adam Langleyfcf25832014-12-18 17:42:32 -0800345 s->signed_cert_timestamps_enabled = s->ctx->signed_cert_timestamps_enabled;
346 s->ocsp_stapling_enabled = s->ctx->ocsp_stapling_enabled;
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200347
Adam Langleyfcf25832014-12-18 17:42:32 -0800348 return s;
349
Adam Langley95c29f32014-06-20 12:00:00 -0700350err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400351 SSL_free(s);
David Benjamin3570d732015-06-29 00:28:17 -0400352 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700353
Adam Langleyfcf25832014-12-18 17:42:32 -0800354 return NULL;
355}
Adam Langley95c29f32014-06-20 12:00:00 -0700356
Adam Langleyfcf25832014-12-18 17:42:32 -0800357int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
358 unsigned int sid_ctx_len) {
359 if (sid_ctx_len > sizeof ctx->sid_ctx) {
David Benjamin3570d732015-06-29 00:28:17 -0400360 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -0800361 return 0;
362 }
363 ctx->sid_ctx_length = sid_ctx_len;
364 memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700365
Adam Langleyfcf25832014-12-18 17:42:32 -0800366 return 1;
367}
Adam Langley95c29f32014-06-20 12:00:00 -0700368
Adam Langleyfcf25832014-12-18 17:42:32 -0800369int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
370 unsigned int sid_ctx_len) {
371 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
David Benjamin3570d732015-06-29 00:28:17 -0400372 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -0800373 return 0;
374 }
375 ssl->sid_ctx_length = sid_ctx_len;
376 memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700377
Adam Langleyfcf25832014-12-18 17:42:32 -0800378 return 1;
379}
Adam Langley95c29f32014-06-20 12:00:00 -0700380
Adam Langleyfcf25832014-12-18 17:42:32 -0800381int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800382 ctx->generate_session_id = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -0800383 return 1;
384}
Adam Langley95c29f32014-06-20 12:00:00 -0700385
Adam Langleyfcf25832014-12-18 17:42:32 -0800386int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800387 ssl->generate_session_id = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -0800388 return 1;
389}
Adam Langley95c29f32014-06-20 12:00:00 -0700390
Adam Langleyfcf25832014-12-18 17:42:32 -0800391int SSL_has_matching_session_id(const SSL *ssl, const uint8_t *id,
392 unsigned int id_len) {
393 /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how we
394 * can "construct" a session to give us the desired check - ie. to find if
395 * there's a session in the hash table that would conflict with any new
396 * session built out of this id/id_len and the ssl_version in use by this
397 * SSL. */
398 SSL_SESSION r, *p;
Adam Langley95c29f32014-06-20 12:00:00 -0700399
Adam Langleyfcf25832014-12-18 17:42:32 -0800400 if (id_len > sizeof r.session_id) {
401 return 0;
402 }
Adam Langley95c29f32014-06-20 12:00:00 -0700403
Adam Langleyfcf25832014-12-18 17:42:32 -0800404 r.ssl_version = ssl->version;
405 r.session_id_length = id_len;
406 memcpy(r.session_id, id, id_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700407
Adam Langley4bdb6e42015-05-15 15:29:21 -0700408 CRYPTO_MUTEX_lock_read(&ssl->ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -0800409 p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r);
Adam Langley4bdb6e42015-05-15 15:29:21 -0700410 CRYPTO_MUTEX_unlock(&ssl->ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -0800411 return p != NULL;
412}
Adam Langley95c29f32014-06-20 12:00:00 -0700413
Adam Langleyfcf25832014-12-18 17:42:32 -0800414int SSL_CTX_set_purpose(SSL_CTX *s, int purpose) {
415 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
416}
417
418int SSL_set_purpose(SSL *s, int purpose) {
419 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
420}
421
422int SSL_CTX_set_trust(SSL_CTX *s, int trust) {
423 return X509_VERIFY_PARAM_set_trust(s->param, trust);
424}
425
426int SSL_set_trust(SSL *s, int trust) {
427 return X509_VERIFY_PARAM_set_trust(s->param, trust);
428}
429
430int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm) {
431 return X509_VERIFY_PARAM_set1(ctx->param, vpm);
432}
433
434int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm) {
435 return X509_VERIFY_PARAM_set1(ssl->param, vpm);
436}
Adam Langley95c29f32014-06-20 12:00:00 -0700437
Adam Langley858a88d2014-06-20 12:00:00 -0700438void ssl_cipher_preference_list_free(
Adam Langleyfcf25832014-12-18 17:42:32 -0800439 struct ssl_cipher_preference_list_st *cipher_list) {
David Benjamin5d1ec732015-04-22 13:38:00 -0400440 if (cipher_list == NULL) {
441 return;
442 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800443 sk_SSL_CIPHER_free(cipher_list->ciphers);
444 OPENSSL_free(cipher_list->in_group_flags);
445 OPENSSL_free(cipher_list);
446}
Adam Langley858a88d2014-06-20 12:00:00 -0700447
Adam Langleyfcf25832014-12-18 17:42:32 -0800448struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_dup(
449 struct ssl_cipher_preference_list_st *cipher_list) {
450 struct ssl_cipher_preference_list_st *ret = NULL;
451 size_t n = sk_SSL_CIPHER_num(cipher_list->ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700452
Adam Langleyfcf25832014-12-18 17:42:32 -0800453 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
454 if (!ret) {
455 goto err;
456 }
457
458 ret->ciphers = NULL;
459 ret->in_group_flags = NULL;
460 ret->ciphers = sk_SSL_CIPHER_dup(cipher_list->ciphers);
461 if (!ret->ciphers) {
462 goto err;
463 }
464 ret->in_group_flags = BUF_memdup(cipher_list->in_group_flags, n);
465 if (!ret->in_group_flags) {
466 goto err;
467 }
468
469 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -0700470
471err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400472 ssl_cipher_preference_list_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -0800473 return NULL;
474}
Adam Langley858a88d2014-06-20 12:00:00 -0700475
Adam Langleyfcf25832014-12-18 17:42:32 -0800476struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_from_ciphers(
David Benjamin60da0cd2015-05-03 15:21:28 -0400477 STACK_OF(SSL_CIPHER) *ciphers) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800478 struct ssl_cipher_preference_list_st *ret = NULL;
479 size_t n = sk_SSL_CIPHER_num(ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700480
Adam Langleyfcf25832014-12-18 17:42:32 -0800481 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
482 if (!ret) {
483 goto err;
484 }
485 ret->ciphers = NULL;
486 ret->in_group_flags = NULL;
487 ret->ciphers = sk_SSL_CIPHER_dup(ciphers);
488 if (!ret->ciphers) {
489 goto err;
490 }
491 ret->in_group_flags = OPENSSL_malloc(n);
492 if (!ret->in_group_flags) {
493 goto err;
494 }
495 memset(ret->in_group_flags, 0, n);
496 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -0700497
498err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400499 ssl_cipher_preference_list_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -0800500 return NULL;
501}
Adam Langley858a88d2014-06-20 12:00:00 -0700502
Adam Langleyfcf25832014-12-18 17:42:32 -0800503X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) { return ctx->param; }
Adam Langley95c29f32014-06-20 12:00:00 -0700504
Adam Langleyfcf25832014-12-18 17:42:32 -0800505X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) { return ssl->param; }
Adam Langley95c29f32014-06-20 12:00:00 -0700506
David Benjamin7481d392015-07-05 19:38:46 -0400507void SSL_certs_clear(SSL *ssl) { ssl_cert_clear_certs(ssl->cert); }
Adam Langley95c29f32014-06-20 12:00:00 -0700508
David Benjamin4831c332015-05-16 11:43:13 -0400509void SSL_free(SSL *ssl) {
510 if (ssl == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800511 return;
512 }
Adam Langley95c29f32014-06-20 12:00:00 -0700513
David Benjamin4831c332015-05-16 11:43:13 -0400514 X509_VERIFY_PARAM_free(ssl->param);
Adam Langley95c29f32014-06-20 12:00:00 -0700515
David Benjamin4831c332015-05-16 11:43:13 -0400516 CRYPTO_free_ex_data(&g_ex_data_class_ssl, ssl, &ssl->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700517
David Benjamin4831c332015-05-16 11:43:13 -0400518 if (ssl->bbio != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800519 /* If the buffering BIO is in place, pop it off */
David Benjamin4831c332015-05-16 11:43:13 -0400520 if (ssl->bbio == ssl->wbio) {
521 ssl->wbio = BIO_pop(ssl->wbio);
Adam Langleyfcf25832014-12-18 17:42:32 -0800522 }
David Benjamin4831c332015-05-16 11:43:13 -0400523 BIO_free(ssl->bbio);
524 ssl->bbio = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800525 }
Adam Langley95c29f32014-06-20 12:00:00 -0700526
David Benjamin4831c332015-05-16 11:43:13 -0400527 int free_wbio = ssl->wbio != ssl->rbio;
528 BIO_free_all(ssl->rbio);
David Benjamin2755a3e2015-04-22 16:17:58 -0400529 if (free_wbio) {
David Benjamin4831c332015-05-16 11:43:13 -0400530 BIO_free_all(ssl->wbio);
Adam Langleyfcf25832014-12-18 17:42:32 -0800531 }
Adam Langley95c29f32014-06-20 12:00:00 -0700532
David Benjamin4831c332015-05-16 11:43:13 -0400533 BUF_MEM_free(ssl->init_buf);
Adam Langley95c29f32014-06-20 12:00:00 -0700534
Adam Langleyfcf25832014-12-18 17:42:32 -0800535 /* add extra stuff */
David Benjamin4831c332015-05-16 11:43:13 -0400536 ssl_cipher_preference_list_free(ssl->cipher_list);
537 sk_SSL_CIPHER_free(ssl->cipher_list_by_id);
Adam Langley95c29f32014-06-20 12:00:00 -0700538
David Benjamin4831c332015-05-16 11:43:13 -0400539 ssl_clear_bad_session(ssl);
540 SSL_SESSION_free(ssl->session);
Adam Langley95c29f32014-06-20 12:00:00 -0700541
David Benjamin4831c332015-05-16 11:43:13 -0400542 ssl_clear_cipher_ctx(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700543
David Benjamin4831c332015-05-16 11:43:13 -0400544 ssl_cert_free(ssl->cert);
Adam Langley0289c732014-06-20 12:00:00 -0700545
David Benjamin4831c332015-05-16 11:43:13 -0400546 OPENSSL_free(ssl->tlsext_hostname);
547 SSL_CTX_free(ssl->initial_ctx);
David Benjamin4831c332015-05-16 11:43:13 -0400548 OPENSSL_free(ssl->tlsext_ellipticcurvelist);
549 OPENSSL_free(ssl->alpn_client_proto_list);
550 EVP_PKEY_free(ssl->tlsext_channel_id_private);
551 OPENSSL_free(ssl->psk_identity_hint);
552 sk_X509_NAME_pop_free(ssl->client_CA, X509_NAME_free);
553 OPENSSL_free(ssl->next_proto_negotiated);
554 sk_SRTP_PROTECTION_PROFILE_free(ssl->srtp_profiles);
Adam Langley95c29f32014-06-20 12:00:00 -0700555
David Benjamin4831c332015-05-16 11:43:13 -0400556 if (ssl->method != NULL) {
557 ssl->method->ssl_free(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800558 }
David Benjamin4831c332015-05-16 11:43:13 -0400559 SSL_CTX_free(ssl->ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700560
David Benjamin4831c332015-05-16 11:43:13 -0400561 OPENSSL_free(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800562}
Adam Langley95c29f32014-06-20 12:00:00 -0700563
Adam Langleyfcf25832014-12-18 17:42:32 -0800564void SSL_set_bio(SSL *s, BIO *rbio, BIO *wbio) {
565 /* If the output buffering BIO is still in place, remove it. */
566 if (s->bbio != NULL) {
567 if (s->wbio == s->bbio) {
568 s->wbio = s->wbio->next_bio;
569 s->bbio->next_bio = NULL;
570 }
571 }
Adam Langley95c29f32014-06-20 12:00:00 -0700572
David Benjamin2755a3e2015-04-22 16:17:58 -0400573 if (s->rbio != rbio) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800574 BIO_free_all(s->rbio);
575 }
David Benjamin2755a3e2015-04-22 16:17:58 -0400576 if (s->wbio != wbio && s->rbio != s->wbio) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800577 BIO_free_all(s->wbio);
578 }
579 s->rbio = rbio;
580 s->wbio = wbio;
581}
Adam Langley95c29f32014-06-20 12:00:00 -0700582
Adam Langleyfcf25832014-12-18 17:42:32 -0800583BIO *SSL_get_rbio(const SSL *s) { return s->rbio; }
Adam Langley95c29f32014-06-20 12:00:00 -0700584
Adam Langleyfcf25832014-12-18 17:42:32 -0800585BIO *SSL_get_wbio(const SSL *s) { return s->wbio; }
Adam Langley95c29f32014-06-20 12:00:00 -0700586
Adam Langleyfcf25832014-12-18 17:42:32 -0800587int SSL_get_fd(const SSL *s) { return SSL_get_rfd(s); }
Adam Langley95c29f32014-06-20 12:00:00 -0700588
Adam Langleyfcf25832014-12-18 17:42:32 -0800589int SSL_get_rfd(const SSL *s) {
590 int ret = -1;
591 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700592
Adam Langleyfcf25832014-12-18 17:42:32 -0800593 b = SSL_get_rbio(s);
594 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
595 if (r != NULL) {
596 BIO_get_fd(r, &ret);
597 }
598 return ret;
599}
Adam Langley95c29f32014-06-20 12:00:00 -0700600
Adam Langleyfcf25832014-12-18 17:42:32 -0800601int SSL_get_wfd(const SSL *s) {
602 int ret = -1;
603 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700604
Adam Langleyfcf25832014-12-18 17:42:32 -0800605 b = SSL_get_wbio(s);
606 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
607 if (r != NULL) {
608 BIO_get_fd(r, &ret);
609 }
Adam Langley95c29f32014-06-20 12:00:00 -0700610
Adam Langleyfcf25832014-12-18 17:42:32 -0800611 return ret;
612}
Adam Langley95c29f32014-06-20 12:00:00 -0700613
Adam Langleyfcf25832014-12-18 17:42:32 -0800614int SSL_set_fd(SSL *s, int fd) {
615 int ret = 0;
616 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700617
Adam Langleyfcf25832014-12-18 17:42:32 -0800618 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700619
Adam Langleyfcf25832014-12-18 17:42:32 -0800620 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400621 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -0800622 goto err;
623 }
624 BIO_set_fd(bio, fd, BIO_NOCLOSE);
625 SSL_set_bio(s, bio, bio);
626 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700627
Adam Langley95c29f32014-06-20 12:00:00 -0700628err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800629 return ret;
630}
Adam Langley95c29f32014-06-20 12:00:00 -0700631
Adam Langleyfcf25832014-12-18 17:42:32 -0800632int SSL_set_wfd(SSL *s, int fd) {
633 int ret = 0;
634 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700635
Adam Langleyfcf25832014-12-18 17:42:32 -0800636 if (s->rbio == NULL || BIO_method_type(s->rbio) != BIO_TYPE_FD ||
637 (int)BIO_get_fd(s->rbio, NULL) != fd) {
638 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700639
Adam Langleyfcf25832014-12-18 17:42:32 -0800640 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400641 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -0800642 goto err;
643 }
644 BIO_set_fd(bio, fd, BIO_NOCLOSE);
645 SSL_set_bio(s, SSL_get_rbio(s), bio);
646 } else {
647 SSL_set_bio(s, SSL_get_rbio(s), SSL_get_rbio(s));
648 }
649
650 ret = 1;
651
Adam Langley95c29f32014-06-20 12:00:00 -0700652err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800653 return ret;
654}
Adam Langley95c29f32014-06-20 12:00:00 -0700655
Adam Langleyfcf25832014-12-18 17:42:32 -0800656int SSL_set_rfd(SSL *s, int fd) {
657 int ret = 0;
658 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700659
Adam Langleyfcf25832014-12-18 17:42:32 -0800660 if (s->wbio == NULL || BIO_method_type(s->wbio) != BIO_TYPE_FD ||
661 (int)BIO_get_fd(s->wbio, NULL) != fd) {
662 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700663
Adam Langleyfcf25832014-12-18 17:42:32 -0800664 if (bio == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400665 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -0800666 goto err;
667 }
668 BIO_set_fd(bio, fd, BIO_NOCLOSE);
669 SSL_set_bio(s, bio, SSL_get_wbio(s));
670 } else {
671 SSL_set_bio(s, SSL_get_wbio(s), SSL_get_wbio(s));
672 }
673 ret = 1;
674
Adam Langley95c29f32014-06-20 12:00:00 -0700675err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800676 return ret;
677}
Adam Langley95c29f32014-06-20 12:00:00 -0700678
679/* return length of latest Finished message we sent, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -0800680size_t SSL_get_finished(const SSL *s, void *buf, size_t count) {
681 size_t ret = 0;
682
683 if (s->s3 != NULL) {
684 ret = s->s3->tmp.finish_md_len;
685 if (count > ret) {
686 count = ret;
687 }
688 memcpy(buf, s->s3->tmp.finish_md, count);
689 }
690
691 return ret;
692}
Adam Langley95c29f32014-06-20 12:00:00 -0700693
694/* return length of latest Finished message we expected, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -0800695size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count) {
696 size_t ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700697
Adam Langleyfcf25832014-12-18 17:42:32 -0800698 if (s->s3 != NULL) {
699 ret = s->s3->tmp.peer_finish_md_len;
700 if (count > ret) {
701 count = ret;
702 }
703 memcpy(buf, s->s3->tmp.peer_finish_md, count);
704 }
Adam Langley95c29f32014-06-20 12:00:00 -0700705
Adam Langleyfcf25832014-12-18 17:42:32 -0800706 return ret;
707}
Adam Langley95c29f32014-06-20 12:00:00 -0700708
Adam Langleyfcf25832014-12-18 17:42:32 -0800709int SSL_get_verify_mode(const SSL *s) { return s->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -0700710
Adam Langleyfcf25832014-12-18 17:42:32 -0800711int SSL_get_verify_depth(const SSL *s) {
712 return X509_VERIFY_PARAM_get_depth(s->param);
713}
Adam Langley95c29f32014-06-20 12:00:00 -0700714
Adam Langleyfcf25832014-12-18 17:42:32 -0800715int (*SSL_get_verify_callback(const SSL *s))(int, X509_STORE_CTX *) {
716 return s->verify_callback;
717}
Adam Langley95c29f32014-06-20 12:00:00 -0700718
Adam Langleyfcf25832014-12-18 17:42:32 -0800719int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) { return ctx->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -0700720
Adam Langleyfcf25832014-12-18 17:42:32 -0800721int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) {
722 return X509_VERIFY_PARAM_get_depth(ctx->param);
723}
Adam Langley95c29f32014-06-20 12:00:00 -0700724
Adam Langleyfcf25832014-12-18 17:42:32 -0800725int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int, X509_STORE_CTX *) {
726 return ctx->default_verify_callback;
727}
Adam Langley95c29f32014-06-20 12:00:00 -0700728
Adam Langleyfcf25832014-12-18 17:42:32 -0800729void SSL_set_verify(SSL *s, int mode,
730 int (*callback)(int ok, X509_STORE_CTX *ctx)) {
731 s->verify_mode = mode;
732 if (callback != NULL) {
733 s->verify_callback = callback;
734 }
735}
Adam Langley95c29f32014-06-20 12:00:00 -0700736
Adam Langleyfcf25832014-12-18 17:42:32 -0800737void SSL_set_verify_depth(SSL *s, int depth) {
738 X509_VERIFY_PARAM_set_depth(s->param, depth);
739}
Adam Langley95c29f32014-06-20 12:00:00 -0700740
David Benjamin9a41d1b2015-05-16 01:30:09 -0400741int SSL_CTX_get_read_ahead(const SSL_CTX *ctx) { return 0; }
Adam Langley95c29f32014-06-20 12:00:00 -0700742
David Benjamin9a41d1b2015-05-16 01:30:09 -0400743int SSL_get_read_ahead(const SSL *s) { return 0; }
Adam Langley95c29f32014-06-20 12:00:00 -0700744
David Benjamin9a41d1b2015-05-16 01:30:09 -0400745void SSL_CTX_set_read_ahead(SSL_CTX *ctx, int yes) { }
David Benjamin61ecccf2015-05-05 09:44:51 -0400746
David Benjamin9a41d1b2015-05-16 01:30:09 -0400747void SSL_set_read_ahead(SSL *s, int yes) { }
David Benjamin61ecccf2015-05-05 09:44:51 -0400748
Adam Langleyfcf25832014-12-18 17:42:32 -0800749int SSL_pending(const SSL *s) {
David Benjamin904dc722015-05-30 16:39:07 -0400750 return (s->s3->rrec.type == SSL3_RT_APPLICATION_DATA) ? s->s3->rrec.length
751 : 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800752}
Adam Langley95c29f32014-06-20 12:00:00 -0700753
David Benjamin26416e92015-08-22 16:04:17 -0400754X509 *SSL_get_peer_certificate(const SSL *ssl) {
755 if (ssl == NULL || ssl->session == NULL || ssl->session->peer == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800756 return NULL;
757 }
David Benjamin26416e92015-08-22 16:04:17 -0400758 return X509_up_ref(ssl->session->peer);
Adam Langleyfcf25832014-12-18 17:42:32 -0800759}
760
David Benjamin26416e92015-08-22 16:04:17 -0400761STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *ssl) {
762 if (ssl == NULL || ssl->session == NULL) {
763 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800764 }
David Benjamin26416e92015-08-22 16:04:17 -0400765 return ssl->session->cert_chain;
Adam Langleyfcf25832014-12-18 17:42:32 -0800766}
Adam Langley95c29f32014-06-20 12:00:00 -0700767
Adam Langley95c29f32014-06-20 12:00:00 -0700768/* Fix this so it checks all the valid key/cert options */
Adam Langleyfcf25832014-12-18 17:42:32 -0800769int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
David Benjamin651b3d92015-08-09 12:07:25 -0400770 if (ctx->cert->x509 == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400771 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800772 return 0;
773 }
774
David Benjamind1d80782015-07-05 11:54:09 -0400775 if (ctx->cert->privatekey == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400776 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800777 return 0;
778 }
779
David Benjamind1d80782015-07-05 11:54:09 -0400780 return X509_check_private_key(ctx->cert->x509, ctx->cert->privatekey);
Adam Langleyfcf25832014-12-18 17:42:32 -0800781}
Adam Langley95c29f32014-06-20 12:00:00 -0700782
783/* Fix this function so that it takes an optional type parameter */
Adam Langleyfcf25832014-12-18 17:42:32 -0800784int SSL_check_private_key(const SSL *ssl) {
David Benjamind1d80782015-07-05 11:54:09 -0400785 if (ssl->cert->x509 == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400786 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800787 return 0;
788 }
David Benjamin0b145c22014-11-26 20:10:09 -0500789
David Benjamind1d80782015-07-05 11:54:09 -0400790 if (ssl->cert->privatekey == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400791 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800792 return 0;
793 }
Adam Langley95c29f32014-06-20 12:00:00 -0700794
David Benjamind1d80782015-07-05 11:54:09 -0400795 return X509_check_private_key(ssl->cert->x509, ssl->cert->privatekey);
Adam Langleyfcf25832014-12-18 17:42:32 -0800796}
Adam Langley95c29f32014-06-20 12:00:00 -0700797
Adam Langleyfcf25832014-12-18 17:42:32 -0800798int SSL_accept(SSL *s) {
799 if (s->handshake_func == 0) {
800 /* Not properly initialized yet */
801 SSL_set_accept_state(s);
802 }
David Benjamin0b145c22014-11-26 20:10:09 -0500803
Adam Langleyfcf25832014-12-18 17:42:32 -0800804 if (s->handshake_func != s->method->ssl_accept) {
David Benjamin3570d732015-06-29 00:28:17 -0400805 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800806 return -1;
807 }
Adam Langley95c29f32014-06-20 12:00:00 -0700808
Adam Langleyfcf25832014-12-18 17:42:32 -0800809 return s->handshake_func(s);
810}
Adam Langley95c29f32014-06-20 12:00:00 -0700811
Adam Langleyfcf25832014-12-18 17:42:32 -0800812int SSL_connect(SSL *s) {
813 if (s->handshake_func == 0) {
814 /* Not properly initialized yet */
815 SSL_set_connect_state(s);
816 }
Adam Langley95c29f32014-06-20 12:00:00 -0700817
Adam Langleyfcf25832014-12-18 17:42:32 -0800818 if (s->handshake_func != s->method->ssl_connect) {
David Benjamin3570d732015-06-29 00:28:17 -0400819 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800820 return -1;
821 }
Adam Langley95c29f32014-06-20 12:00:00 -0700822
Adam Langleyfcf25832014-12-18 17:42:32 -0800823 return s->handshake_func(s);
824}
Adam Langley95c29f32014-06-20 12:00:00 -0700825
Adam Langleyfcf25832014-12-18 17:42:32 -0800826long SSL_get_default_timeout(const SSL *s) {
827 return SSL_DEFAULT_SESSION_TIMEOUT;
828}
Adam Langley95c29f32014-06-20 12:00:00 -0700829
Adam Langleyfcf25832014-12-18 17:42:32 -0800830int SSL_read(SSL *s, void *buf, int num) {
831 if (s->handshake_func == 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400832 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800833 return -1;
834 }
Adam Langley95c29f32014-06-20 12:00:00 -0700835
Adam Langleyfcf25832014-12-18 17:42:32 -0800836 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
837 s->rwstate = SSL_NOTHING;
838 return 0;
839 }
Adam Langley95c29f32014-06-20 12:00:00 -0700840
David Benjamin904dc722015-05-30 16:39:07 -0400841 ERR_clear_system_error();
842 return s->method->ssl_read_app_data(s, buf, num, 0);
Adam Langleyfcf25832014-12-18 17:42:32 -0800843}
Adam Langley95c29f32014-06-20 12:00:00 -0700844
Adam Langleyfcf25832014-12-18 17:42:32 -0800845int SSL_peek(SSL *s, void *buf, int num) {
846 if (s->handshake_func == 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400847 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800848 return -1;
849 }
Adam Langley95c29f32014-06-20 12:00:00 -0700850
Adam Langleyfcf25832014-12-18 17:42:32 -0800851 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
852 return 0;
853 }
Adam Langley95c29f32014-06-20 12:00:00 -0700854
David Benjamin904dc722015-05-30 16:39:07 -0400855 ERR_clear_system_error();
856 return s->method->ssl_read_app_data(s, buf, num, 1);
Adam Langleyfcf25832014-12-18 17:42:32 -0800857}
Adam Langley95c29f32014-06-20 12:00:00 -0700858
Adam Langleyfcf25832014-12-18 17:42:32 -0800859int SSL_write(SSL *s, const void *buf, int num) {
860 if (s->handshake_func == 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400861 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800862 return -1;
863 }
Adam Langley95c29f32014-06-20 12:00:00 -0700864
Adam Langleyfcf25832014-12-18 17:42:32 -0800865 if (s->shutdown & SSL_SENT_SHUTDOWN) {
866 s->rwstate = SSL_NOTHING;
David Benjamin3570d732015-06-29 00:28:17 -0400867 OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
Adam Langleyfcf25832014-12-18 17:42:32 -0800868 return -1;
869 }
Adam Langley95c29f32014-06-20 12:00:00 -0700870
David Benjamin904dc722015-05-30 16:39:07 -0400871 ERR_clear_system_error();
872 return s->method->ssl_write_app_data(s, buf, num);
Adam Langleyfcf25832014-12-18 17:42:32 -0800873}
Adam Langley95c29f32014-06-20 12:00:00 -0700874
Adam Langleyfcf25832014-12-18 17:42:32 -0800875int SSL_shutdown(SSL *s) {
876 /* Note that this function behaves differently from what one might expect.
877 * Return values are 0 for no success (yet), 1 for success; but calling it
878 * once is usually not enough, even if blocking I/O is used (see
879 * ssl3_shutdown). */
Adam Langley95c29f32014-06-20 12:00:00 -0700880
Adam Langleyfcf25832014-12-18 17:42:32 -0800881 if (s->handshake_func == 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400882 OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800883 return -1;
884 }
Adam Langley95c29f32014-06-20 12:00:00 -0700885
David Benjamin904dc722015-05-30 16:39:07 -0400886 if (SSL_in_init(s)) {
887 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800888 }
Adam Langley95c29f32014-06-20 12:00:00 -0700889
David Benjamin904dc722015-05-30 16:39:07 -0400890 /* Do nothing if configured not to send a close_notify. */
891 if (s->quiet_shutdown) {
892 s->shutdown = SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN;
893 return 1;
894 }
895
896 if (!(s->shutdown & SSL_SENT_SHUTDOWN)) {
897 s->shutdown |= SSL_SENT_SHUTDOWN;
898 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_CLOSE_NOTIFY);
899
900 /* our shutdown alert has been sent now, and if it still needs to be
901 * written, s->s3->alert_dispatch will be true */
902 if (s->s3->alert_dispatch) {
903 return -1; /* return WANT_WRITE */
904 }
905 } else if (s->s3->alert_dispatch) {
906 /* resend it if not sent */
907 int ret = s->method->ssl_dispatch_alert(s);
908 if (ret == -1) {
909 /* we only get to return -1 here the 2nd/Nth invocation, we must have
910 * already signalled return 0 upon a previous invoation, return
911 * WANT_WRITE */
912 return ret;
913 }
914 } else if (!(s->shutdown & SSL_RECEIVED_SHUTDOWN)) {
915 /* If we are waiting for a close from our peer, we are closed */
916 s->method->ssl_read_close_notify(s);
917 if (!(s->shutdown & SSL_RECEIVED_SHUTDOWN)) {
918 return -1; /* return WANT_READ */
919 }
920 }
921
922 if (s->shutdown == (SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN) &&
923 !s->s3->alert_dispatch) {
924 return 1;
925 } else {
926 return 0;
927 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800928}
Adam Langley95c29f32014-06-20 12:00:00 -0700929
David Benjamin44d3eed2015-05-21 01:29:55 -0400930int SSL_renegotiate(SSL *ssl) {
931 /* Caller-initiated renegotiation is not supported. */
David Benjamin3570d732015-06-29 00:28:17 -0400932 OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
David Benjamin44d3eed2015-05-21 01:29:55 -0400933 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800934}
Adam Langley95c29f32014-06-20 12:00:00 -0700935
David Benjamin44d3eed2015-05-21 01:29:55 -0400936int SSL_renegotiate_pending(SSL *ssl) {
937 return SSL_in_init(ssl) && ssl->s3->initial_handshake_complete;
Adam Langleyfcf25832014-12-18 17:42:32 -0800938}
Adam Langley95c29f32014-06-20 12:00:00 -0700939
David Benjamin61ecccf2015-05-05 09:44:51 -0400940uint32_t SSL_CTX_set_options(SSL_CTX *ctx, uint32_t options) {
941 ctx->options |= options;
942 return ctx->options;
943}
944
945uint32_t SSL_set_options(SSL *ssl, uint32_t options) {
946 ssl->options |= options;
947 return ssl->options;
948}
949
950uint32_t SSL_CTX_clear_options(SSL_CTX *ctx, uint32_t options) {
951 ctx->options &= ~options;
952 return ctx->options;
953}
954
955uint32_t SSL_clear_options(SSL *ssl, uint32_t options) {
956 ssl->options &= ~options;
957 return ssl->options;
958}
959
960uint32_t SSL_CTX_get_options(const SSL_CTX *ctx) { return ctx->options; }
961
962uint32_t SSL_get_options(const SSL *ssl) { return ssl->options; }
963
964uint32_t SSL_CTX_set_mode(SSL_CTX *ctx, uint32_t mode) {
965 ctx->mode |= mode;
966 return ctx->mode;
967}
968
969uint32_t SSL_set_mode(SSL *ssl, uint32_t mode) {
970 ssl->mode |= mode;
971 return ssl->mode;
972}
973
974uint32_t SSL_CTX_clear_mode(SSL_CTX *ctx, uint32_t mode) {
975 ctx->mode &= ~mode;
976 return ctx->mode;
977}
978
979uint32_t SSL_clear_mode(SSL *ssl, uint32_t mode) {
980 ssl->mode &= ~mode;
981 return ssl->mode;
982}
983
984uint32_t SSL_CTX_get_mode(const SSL_CTX *ctx) { return ctx->mode; }
985
986uint32_t SSL_get_mode(const SSL *ssl) { return ssl->mode; }
987
David Benjamin1d0a1942015-04-26 15:35:35 -0400988size_t SSL_CTX_get_max_cert_list(const SSL_CTX *ctx) {
989 return ctx->max_cert_list;
990}
991
992void SSL_CTX_set_max_cert_list(SSL_CTX *ctx, size_t max_cert_list) {
993 if (max_cert_list > kMaxHandshakeSize) {
994 max_cert_list = kMaxHandshakeSize;
995 }
996 ctx->max_cert_list = (uint32_t)max_cert_list;
997}
998
999size_t SSL_get_max_cert_list(const SSL *ssl) {
1000 return ssl->max_cert_list;
1001}
1002
1003void SSL_set_max_cert_list(SSL *ssl, size_t max_cert_list) {
1004 if (max_cert_list > kMaxHandshakeSize) {
1005 max_cert_list = kMaxHandshakeSize;
1006 }
1007 ssl->max_cert_list = (uint32_t)max_cert_list;
1008}
1009
1010void SSL_CTX_set_max_send_fragment(SSL_CTX *ctx, size_t max_send_fragment) {
1011 if (max_send_fragment < 512) {
1012 max_send_fragment = 512;
1013 }
1014 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1015 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1016 }
1017 ctx->max_send_fragment = (uint16_t)max_send_fragment;
1018}
1019
1020void SSL_set_max_send_fragment(SSL *ssl, size_t max_send_fragment) {
1021 if (max_send_fragment < 512) {
1022 max_send_fragment = 512;
1023 }
1024 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1025 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1026 }
1027 ssl->max_send_fragment = (uint16_t)max_send_fragment;
1028}
1029
David Benjamincb9cf792015-05-05 09:46:14 -04001030int SSL_set_mtu(SSL *ssl, unsigned mtu) {
1031 if (!SSL_IS_DTLS(ssl) || mtu < dtls1_min_mtu()) {
1032 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001033 }
David Benjamincb9cf792015-05-05 09:46:14 -04001034 ssl->d1->mtu = mtu;
1035 return 1;
1036}
1037
1038int SSL_get_secure_renegotiation_support(const SSL *ssl) {
1039 return ssl->s3->send_connection_binding;
1040}
1041
Adam Langleyfcf25832014-12-18 17:42:32 -08001042LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) { return ctx->sessions; }
1043
David Benjamin71f7d3d2015-05-05 09:46:38 -04001044size_t SSL_CTX_sess_number(const SSL_CTX *ctx) {
1045 return lh_SSL_SESSION_num_items(ctx->sessions);
1046}
1047
1048unsigned long SSL_CTX_sess_set_cache_size(SSL_CTX *ctx, unsigned long size) {
1049 unsigned long ret = ctx->session_cache_size;
1050 ctx->session_cache_size = size;
1051 return ret;
1052}
1053
1054unsigned long SSL_CTX_sess_get_cache_size(const SSL_CTX *ctx) {
1055 return ctx->session_cache_size;
1056}
1057
1058int SSL_CTX_set_session_cache_mode(SSL_CTX *ctx, int mode) {
1059 int ret = ctx->session_cache_mode;
1060 ctx->session_cache_mode = mode;
1061 return ret;
1062}
1063
1064int SSL_CTX_get_session_cache_mode(const SSL_CTX *ctx) {
1065 return ctx->session_cache_mode;
1066}
1067
Adam Langleyfcf25832014-12-18 17:42:32 -08001068/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001069 * preference */
David Benjamin60da0cd2015-05-03 15:21:28 -04001070STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001071 if (s == NULL) {
1072 return NULL;
1073 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001074
Adam Langleyfcf25832014-12-18 17:42:32 -08001075 if (s->cipher_list != NULL) {
1076 return s->cipher_list->ciphers;
1077 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001078
Adam Langleyfcf25832014-12-18 17:42:32 -08001079 if (s->version >= TLS1_1_VERSION && s->ctx != NULL &&
1080 s->ctx->cipher_list_tls11 != NULL) {
1081 return s->ctx->cipher_list_tls11->ciphers;
1082 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001083
Adam Langleyfcf25832014-12-18 17:42:32 -08001084 if (s->ctx != NULL && s->ctx->cipher_list != NULL) {
1085 return s->ctx->cipher_list->ciphers;
1086 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001087
Adam Langleyfcf25832014-12-18 17:42:32 -08001088 return NULL;
1089}
Adam Langley95c29f32014-06-20 12:00:00 -07001090
Adam Langleyfcf25832014-12-18 17:42:32 -08001091/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001092 * algorithm id */
David Benjamin60da0cd2015-05-03 15:21:28 -04001093STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001094 if (s == NULL) {
1095 return NULL;
1096 }
Adam Langley95c29f32014-06-20 12:00:00 -07001097
Adam Langleyfcf25832014-12-18 17:42:32 -08001098 if (s->cipher_list_by_id != NULL) {
1099 return s->cipher_list_by_id;
1100 }
Adam Langley95c29f32014-06-20 12:00:00 -07001101
Adam Langleyfcf25832014-12-18 17:42:32 -08001102 if (s->ctx != NULL && s->ctx->cipher_list_by_id != NULL) {
1103 return s->ctx->cipher_list_by_id;
1104 }
Adam Langley95c29f32014-06-20 12:00:00 -07001105
Adam Langleyfcf25832014-12-18 17:42:32 -08001106 return NULL;
1107}
Adam Langley95c29f32014-06-20 12:00:00 -07001108
Adam Langleyfcf25832014-12-18 17:42:32 -08001109/* The old interface to get the same thing as SSL_get_ciphers() */
1110const char *SSL_get_cipher_list(const SSL *s, int n) {
1111 const SSL_CIPHER *c;
David Benjamin60da0cd2015-05-03 15:21:28 -04001112 STACK_OF(SSL_CIPHER) *sk;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001113
Adam Langleyfcf25832014-12-18 17:42:32 -08001114 if (s == NULL) {
1115 return NULL;
1116 }
Adam Langley95c29f32014-06-20 12:00:00 -07001117
Adam Langleyfcf25832014-12-18 17:42:32 -08001118 sk = SSL_get_ciphers(s);
1119 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
1120 return NULL;
1121 }
Adam Langley95c29f32014-06-20 12:00:00 -07001122
Adam Langleyfcf25832014-12-18 17:42:32 -08001123 c = sk_SSL_CIPHER_value(sk, n);
1124 if (c == NULL) {
1125 return NULL;
1126 }
Adam Langley95c29f32014-06-20 12:00:00 -07001127
Adam Langleyfcf25832014-12-18 17:42:32 -08001128 return c->name;
1129}
David Benjamin5491e3f2014-09-29 19:33:09 -04001130
Adam Langleyfcf25832014-12-18 17:42:32 -08001131/* specify the ciphers to be used by default by the SSL_CTX */
1132int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
1133 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001134
Adam Langleyfcf25832014-12-18 17:42:32 -08001135 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001136 &ctx->cipher_list_by_id, str);
Adam Langleyfcf25832014-12-18 17:42:32 -08001137 /* ssl_create_cipher_list may return an empty stack if it was unable to find
1138 * a cipher matching the given rule string (for example if the rule string
1139 * specifies a cipher which has been disabled). This is not an error as far
1140 * as ssl_create_cipher_list is concerned, and hence ctx->cipher_list and
1141 * ctx->cipher_list_by_id has been updated. */
1142 if (sk == NULL) {
1143 return 0;
1144 } else if (sk_SSL_CIPHER_num(sk) == 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001145 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
Adam Langleyfcf25832014-12-18 17:42:32 -08001146 return 0;
1147 }
Adam Langley95c29f32014-06-20 12:00:00 -07001148
Adam Langleyfcf25832014-12-18 17:42:32 -08001149 return 1;
1150}
David Benjamin39482a12014-07-20 13:30:15 -04001151
Adam Langleyfcf25832014-12-18 17:42:32 -08001152int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str) {
1153 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001154
David Benjamin71f07942015-04-08 02:36:59 -04001155 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str);
Adam Langleyfcf25832014-12-18 17:42:32 -08001156 if (sk == NULL) {
1157 return 0;
1158 } else if (sk_SSL_CIPHER_num(sk) == 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001159 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
Adam Langleyfcf25832014-12-18 17:42:32 -08001160 return 0;
1161 }
David Benjamin9f2c0d72014-10-21 22:00:19 -04001162
Adam Langleyfcf25832014-12-18 17:42:32 -08001163 return 1;
1164}
Adam Langley95c29f32014-06-20 12:00:00 -07001165
Adam Langleyfcf25832014-12-18 17:42:32 -08001166/* specify the ciphers to be used by the SSL */
1167int SSL_set_cipher_list(SSL *s, const char *str) {
1168 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001169
Adam Langleyfcf25832014-12-18 17:42:32 -08001170 sk = ssl_create_cipher_list(s->ctx->method, &s->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001171 &s->cipher_list_by_id, str);
David Benjamin39482a12014-07-20 13:30:15 -04001172
Adam Langleyfcf25832014-12-18 17:42:32 -08001173 /* see comment in SSL_CTX_set_cipher_list */
1174 if (sk == NULL) {
1175 return 0;
1176 } else if (sk_SSL_CIPHER_num(sk) == 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001177 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
Adam Langleyfcf25832014-12-18 17:42:32 -08001178 return 0;
1179 }
David Benjamin39482a12014-07-20 13:30:15 -04001180
Adam Langleyfcf25832014-12-18 17:42:32 -08001181 return 1;
1182}
Adam Langley95c29f32014-06-20 12:00:00 -07001183
Adam Langleyfcf25832014-12-18 17:42:32 -08001184int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, uint8_t *p) {
1185 size_t i;
1186 const SSL_CIPHER *c;
1187 CERT *ct = s->cert;
1188 uint8_t *q;
1189 /* Set disabled masks for this session */
1190 ssl_set_client_disabled(s);
Adam Langley29707792014-06-20 12:00:00 -07001191
Adam Langleyfcf25832014-12-18 17:42:32 -08001192 if (sk == NULL) {
1193 return 0;
1194 }
1195 q = p;
Adam Langley95c29f32014-06-20 12:00:00 -07001196
Adam Langleyfcf25832014-12-18 17:42:32 -08001197 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
1198 c = sk_SSL_CIPHER_value(sk, i);
1199 /* Skip disabled ciphers */
1200 if (c->algorithm_ssl & ct->mask_ssl ||
1201 c->algorithm_mkey & ct->mask_k ||
1202 c->algorithm_auth & ct->mask_a) {
1203 continue;
1204 }
David Benjamina1c90a52015-05-30 17:03:14 -04001205 s2n(ssl_cipher_get_value(c), p);
Adam Langleyfcf25832014-12-18 17:42:32 -08001206 }
1207
1208 /* If all ciphers were disabled, return the error to the caller. */
1209 if (p == q) {
1210 return 0;
1211 }
1212
Adam Langley5021b222015-06-12 18:27:58 -07001213 /* For SSLv3, the SCSV is added. Otherwise the renegotiation extension is
1214 * added. */
1215 if (s->client_version == SSL3_VERSION &&
1216 !s->s3->initial_handshake_complete) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001217 s2n(SSL3_CK_SCSV & 0xffff, p);
Adam Langley5021b222015-06-12 18:27:58 -07001218 /* The renegotiation extension is required to be at index zero. */
1219 s->s3->tmp.extensions.sent |= (1u << 0);
Adam Langleyfcf25832014-12-18 17:42:32 -08001220 }
1221
Adam Langley5f0efe02015-02-20 13:03:16 -08001222 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001223 s2n(SSL3_CK_FALLBACK_SCSV & 0xffff, p);
1224 }
1225
1226 return p - q;
1227}
1228
1229STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs) {
1230 CBS cipher_suites = *cbs;
1231 const SSL_CIPHER *c;
David Benjamin60da0cd2015-05-03 15:21:28 -04001232 STACK_OF(SSL_CIPHER) *sk;
Adam Langleyfcf25832014-12-18 17:42:32 -08001233
1234 if (s->s3) {
1235 s->s3->send_connection_binding = 0;
1236 }
1237
1238 if (CBS_len(&cipher_suites) % 2 != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001239 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
Adam Langleyfcf25832014-12-18 17:42:32 -08001240 return NULL;
1241 }
1242
1243 sk = sk_SSL_CIPHER_new_null();
1244 if (sk == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001245 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001246 goto err;
1247 }
1248
Adam Langleyfcf25832014-12-18 17:42:32 -08001249 while (CBS_len(&cipher_suites) > 0) {
1250 uint16_t cipher_suite;
1251
1252 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
David Benjamin3570d732015-06-29 00:28:17 -04001253 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001254 goto err;
1255 }
1256
1257 /* Check for SCSV. */
1258 if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff)) {
1259 /* SCSV is fatal if renegotiating. */
David Benjamin20f6e972015-05-15 21:51:49 -04001260 if (s->s3->initial_handshake_complete) {
David Benjamin3570d732015-06-29 00:28:17 -04001261 OPENSSL_PUT_ERROR(SSL, SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
Adam Langleyfcf25832014-12-18 17:42:32 -08001262 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1263 goto err;
1264 }
1265 s->s3->send_connection_binding = 1;
1266 continue;
1267 }
1268
1269 /* Check for FALLBACK_SCSV. */
1270 if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff)) {
1271 uint16_t max_version = ssl3_get_max_server_version(s);
1272 if (SSL_IS_DTLS(s) ? (uint16_t)s->version > max_version
1273 : (uint16_t)s->version < max_version) {
David Benjamin3570d732015-06-29 00:28:17 -04001274 OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK);
Adam Langleyfcf25832014-12-18 17:42:32 -08001275 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
1276 goto err;
1277 }
1278 continue;
1279 }
1280
David Benjamina1c90a52015-05-30 17:03:14 -04001281 c = SSL_get_cipher_by_value(cipher_suite);
Adam Langleyfcf25832014-12-18 17:42:32 -08001282 if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
David Benjamin3570d732015-06-29 00:28:17 -04001283 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001284 goto err;
1285 }
1286 }
1287
1288 return sk;
David Benjamin9f2c0d72014-10-21 22:00:19 -04001289
Adam Langley95c29f32014-06-20 12:00:00 -07001290err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001291 sk_SSL_CIPHER_free(sk);
Adam Langleyfcf25832014-12-18 17:42:32 -08001292 return NULL;
1293}
Adam Langley95c29f32014-06-20 12:00:00 -07001294
1295
Adam Langleyfcf25832014-12-18 17:42:32 -08001296/* return a servername extension value if provided in Client Hello, or NULL. So
1297 * far, only host_name types are defined (RFC 3546). */
1298const char *SSL_get_servername(const SSL *s, const int type) {
1299 if (type != TLSEXT_NAMETYPE_host_name) {
1300 return NULL;
1301 }
Adam Langley95c29f32014-06-20 12:00:00 -07001302
Adam Langleyfcf25832014-12-18 17:42:32 -08001303 return s->session && !s->tlsext_hostname ? s->session->tlsext_hostname
1304 : s->tlsext_hostname;
1305}
Adam Langley95c29f32014-06-20 12:00:00 -07001306
Adam Langleyfcf25832014-12-18 17:42:32 -08001307int SSL_get_servername_type(const SSL *s) {
1308 if (s->session &&
1309 (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname)) {
1310 return TLSEXT_NAMETYPE_host_name;
1311 }
Adam Langley95c29f32014-06-20 12:00:00 -07001312
Adam Langleyfcf25832014-12-18 17:42:32 -08001313 return -1;
1314}
Adam Langley95c29f32014-06-20 12:00:00 -07001315
Adam Langleyfcf25832014-12-18 17:42:32 -08001316void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
1317 ctx->signed_cert_timestamps_enabled = 1;
1318}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001319
Adam Langleyfcf25832014-12-18 17:42:32 -08001320int SSL_enable_signed_cert_timestamps(SSL *ssl) {
1321 ssl->signed_cert_timestamps_enabled = 1;
1322 return 1;
1323}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001324
Adam Langleyfcf25832014-12-18 17:42:32 -08001325void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
1326 ctx->ocsp_stapling_enabled = 1;
1327}
David Benjamin6c7aed02014-08-27 16:42:38 -04001328
Adam Langleyfcf25832014-12-18 17:42:32 -08001329int SSL_enable_ocsp_stapling(SSL *ssl) {
1330 ssl->ocsp_stapling_enabled = 1;
1331 return 1;
1332}
David Benjamin6c7aed02014-08-27 16:42:38 -04001333
Adam Langleyfcf25832014-12-18 17:42:32 -08001334void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
1335 size_t *out_len) {
1336 SSL_SESSION *session = ssl->session;
Adam Langley3cb50e02014-08-26 14:00:31 -07001337
Adam Langleyfcf25832014-12-18 17:42:32 -08001338 *out_len = 0;
1339 *out = NULL;
1340 if (ssl->server || !session || !session->tlsext_signed_cert_timestamp_list) {
1341 return;
1342 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001343
Adam Langleyfcf25832014-12-18 17:42:32 -08001344 *out = session->tlsext_signed_cert_timestamp_list;
1345 *out_len = session->tlsext_signed_cert_timestamp_list_length;
1346}
David Benjamin6c7aed02014-08-27 16:42:38 -04001347
Adam Langleyfcf25832014-12-18 17:42:32 -08001348void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
1349 size_t *out_len) {
1350 SSL_SESSION *session = ssl->session;
1351
1352 *out_len = 0;
1353 *out = NULL;
1354 if (ssl->server || !session || !session->ocsp_response) {
1355 return;
1356 }
1357 *out = session->ocsp_response;
1358 *out_len = session->ocsp_response_length;
1359}
David Benjamin6c7aed02014-08-27 16:42:38 -04001360
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001361int SSL_CTX_set_ocsp_response(SSL_CTX *ctx, const uint8_t *response,
1362 size_t response_len) {
1363 OPENSSL_free(ctx->ocsp_response);
1364 ctx->ocsp_response_length = 0;
1365
1366 ctx->ocsp_response = BUF_memdup(response, response_len);
1367 if (ctx->ocsp_response == NULL) {
1368 return 0;
1369 }
1370 ctx->ocsp_response_length = response_len;
1371
1372 return 1;
1373}
1374
Adam Langley95c29f32014-06-20 12:00:00 -07001375/* SSL_select_next_proto implements the standard protocol selection. It is
1376 * expected that this function is called from the callback set by
1377 * SSL_CTX_set_next_proto_select_cb.
1378 *
1379 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1380 * strings. The length byte itself is not included in the length. A byte
1381 * string of length 0 is invalid. No byte string may be truncated.
1382 *
1383 * The current, but experimental algorithm for selecting the protocol is:
1384 *
1385 * 1) If the server doesn't support NPN then this is indicated to the
1386 * callback. In this case, the client application has to abort the connection
1387 * or have a default application level protocol.
1388 *
1389 * 2) If the server supports NPN, but advertises an empty list then the
1390 * client selects the first protcol in its list, but indicates via the
1391 * API that this fallback case was enacted.
1392 *
1393 * 3) Otherwise, the client finds the first protocol in the server's list
1394 * that it supports and selects this protocol. This is because it's
1395 * assumed that the server has better information about which protocol
1396 * a client should use.
1397 *
1398 * 4) If the client doesn't support any of the server's advertised
1399 * protocols, then this is treated the same as case 2.
1400 *
1401 * It returns either
1402 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1403 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1404 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001405int SSL_select_next_proto(uint8_t **out, uint8_t *outlen, const uint8_t *server,
1406 unsigned int server_len, const uint8_t *client,
1407 unsigned int client_len) {
1408 unsigned int i, j;
1409 const uint8_t *result;
1410 int status = OPENSSL_NPN_UNSUPPORTED;
Adam Langley95c29f32014-06-20 12:00:00 -07001411
Adam Langleyfcf25832014-12-18 17:42:32 -08001412 /* For each protocol in server preference order, see if we support it. */
1413 for (i = 0; i < server_len;) {
1414 for (j = 0; j < client_len;) {
1415 if (server[i] == client[j] &&
1416 memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
1417 /* We found a match */
1418 result = &server[i];
1419 status = OPENSSL_NPN_NEGOTIATED;
1420 goto found;
1421 }
1422 j += client[j];
1423 j++;
1424 }
1425 i += server[i];
1426 i++;
1427 }
Adam Langley95c29f32014-06-20 12:00:00 -07001428
Adam Langleyfcf25832014-12-18 17:42:32 -08001429 /* There's no overlap between our protocols and the server's list. */
1430 result = client;
1431 status = OPENSSL_NPN_NO_OVERLAP;
Adam Langley95c29f32014-06-20 12:00:00 -07001432
Adam Langleyfcf25832014-12-18 17:42:32 -08001433found:
1434 *out = (uint8_t *)result + 1;
1435 *outlen = result[0];
1436 return status;
1437}
Adam Langley95c29f32014-06-20 12:00:00 -07001438
Adam Langley95c29f32014-06-20 12:00:00 -07001439/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
1440 * requested protocol for this connection and returns 0. If the client didn't
1441 * request any protocol, then *data is set to NULL.
1442 *
1443 * Note that the client can request any protocol it chooses. The value returned
1444 * from this function need not be a member of the list of supported protocols
Adam Langleyfcf25832014-12-18 17:42:32 -08001445 * provided by the callback. */
1446void SSL_get0_next_proto_negotiated(const SSL *s, const uint8_t **data,
1447 unsigned *len) {
1448 *data = s->next_proto_negotiated;
1449 if (!*data) {
1450 *len = 0;
1451 } else {
1452 *len = s->next_proto_negotiated_len;
1453 }
Adam Langley95c29f32014-06-20 12:00:00 -07001454}
1455
1456/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
1457 * TLS server needs a list of supported protocols for Next Protocol
1458 * Negotiation. The returned list must be in wire format. The list is returned
1459 * by setting |out| to point to it and |outlen| to its length. This memory will
1460 * not be modified, but one should assume that the SSL* keeps a reference to
1461 * it.
1462 *
Adam Langleyfcf25832014-12-18 17:42:32 -08001463 * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise.
1464 * Otherwise, no such extension will be included in the ServerHello. */
1465void SSL_CTX_set_next_protos_advertised_cb(
1466 SSL_CTX *ctx,
1467 int (*cb)(SSL *ssl, const uint8_t **out, unsigned int *outlen, void *arg),
1468 void *arg) {
1469 ctx->next_protos_advertised_cb = cb;
1470 ctx->next_protos_advertised_cb_arg = arg;
1471}
Adam Langley95c29f32014-06-20 12:00:00 -07001472
1473/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1474 * client needs to select a protocol from the server's provided list. |out|
1475 * must be set to point to the selected protocol (which may be within |in|).
1476 * The length of the protocol name must be written into |outlen|. The server's
1477 * advertised protocols are provided in |in| and |inlen|. The callback can
1478 * assume that |in| is syntactically valid.
1479 *
1480 * The client must select a protocol. It is fatal to the connection if this
1481 * callback returns a value other than SSL_TLSEXT_ERR_OK.
1482 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001483void SSL_CTX_set_next_proto_select_cb(
1484 SSL_CTX *ctx, int (*cb)(SSL *s, uint8_t **out, uint8_t *outlen,
1485 const uint8_t *in, unsigned int inlen, void *arg),
1486 void *arg) {
1487 ctx->next_proto_select_cb = cb;
1488 ctx->next_proto_select_cb_arg = arg;
1489}
Adam Langley95c29f32014-06-20 12:00:00 -07001490
Adam Langleyfcf25832014-12-18 17:42:32 -08001491int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
1492 unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001493 OPENSSL_free(ctx->alpn_client_proto_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001494 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1495 if (!ctx->alpn_client_proto_list) {
1496 return 1;
1497 }
1498 ctx->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001499
Adam Langleyfcf25832014-12-18 17:42:32 -08001500 return 0;
1501}
Adam Langley95c29f32014-06-20 12:00:00 -07001502
Adam Langleyfcf25832014-12-18 17:42:32 -08001503int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001504 OPENSSL_free(ssl->alpn_client_proto_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001505 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1506 if (!ssl->alpn_client_proto_list) {
1507 return 1;
1508 }
1509 ssl->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001510
Adam Langleyfcf25832014-12-18 17:42:32 -08001511 return 0;
1512}
Adam Langley95c29f32014-06-20 12:00:00 -07001513
1514/* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
1515 * during ClientHello processing in order to select an ALPN protocol from the
1516 * client's list of offered protocols. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001517void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
1518 int (*cb)(SSL *ssl, const uint8_t **out,
1519 uint8_t *outlen, const uint8_t *in,
1520 unsigned int inlen, void *arg),
1521 void *arg) {
1522 ctx->alpn_select_cb = cb;
1523 ctx->alpn_select_cb_arg = arg;
1524}
Adam Langley95c29f32014-06-20 12:00:00 -07001525
1526/* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
1527 * On return it sets |*data| to point to |*len| bytes of protocol name (not
1528 * including the leading length-prefix byte). If the server didn't respond with
1529 * a negotiated protocol then |*len| will be zero. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001530void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **data,
1531 unsigned *len) {
1532 *data = NULL;
1533 if (ssl->s3) {
1534 *data = ssl->s3->alpn_selected;
1535 }
1536 if (*data == NULL) {
1537 *len = 0;
1538 } else {
1539 *len = ssl->s3->alpn_selected_len;
1540 }
1541}
Adam Langley95c29f32014-06-20 12:00:00 -07001542
David Benjamincfd248b2015-04-03 11:02:24 -04001543int SSL_export_keying_material(SSL *s, uint8_t *out, size_t out_len,
1544 const char *label, size_t label_len,
1545 const uint8_t *context, size_t context_len,
1546 int use_context) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001547 if (s->version < TLS1_VERSION) {
David Benjaminc565ebb2015-04-03 04:06:36 -04001548 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001549 }
Adam Langley95c29f32014-06-20 12:00:00 -07001550
David Benjamincfd248b2015-04-03 11:02:24 -04001551 return s->enc_method->export_keying_material(
1552 s, out, out_len, label, label_len, context, context_len, use_context);
Adam Langleyfcf25832014-12-18 17:42:32 -08001553}
Adam Langley95c29f32014-06-20 12:00:00 -07001554
Adam Langleyfcf25832014-12-18 17:42:32 -08001555static uint32_t ssl_session_hash(const SSL_SESSION *a) {
1556 uint32_t hash =
1557 ((uint32_t)a->session_id[0]) ||
1558 ((uint32_t)a->session_id[1] << 8) ||
1559 ((uint32_t)a->session_id[2] << 16) ||
1560 ((uint32_t)a->session_id[3] << 24);
Adam Langley95c29f32014-06-20 12:00:00 -07001561
Adam Langleyfcf25832014-12-18 17:42:32 -08001562 return hash;
1563}
Adam Langley95c29f32014-06-20 12:00:00 -07001564
1565/* NB: If this function (or indeed the hash function which uses a sort of
1566 * coarser function than this one) is changed, ensure
1567 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1568 * able to construct an SSL_SESSION that will collide with any existing session
1569 * with a matching session ID. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001570static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
1571 if (a->ssl_version != b->ssl_version) {
1572 return 1;
1573 }
Adam Langley95c29f32014-06-20 12:00:00 -07001574
Adam Langleyfcf25832014-12-18 17:42:32 -08001575 if (a->session_id_length != b->session_id_length) {
1576 return 1;
1577 }
Adam Langley95c29f32014-06-20 12:00:00 -07001578
Adam Langleyfcf25832014-12-18 17:42:32 -08001579 return memcmp(a->session_id, b->session_id, a->session_id_length);
1580}
Adam Langley95c29f32014-06-20 12:00:00 -07001581
David Benjamin4831c332015-05-16 11:43:13 -04001582SSL_CTX *SSL_CTX_new(const SSL_METHOD *method) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001583 SSL_CTX *ret = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001584
David Benjamin4831c332015-05-16 11:43:13 -04001585 if (method == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001586 OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_METHOD_PASSED);
Adam Langleyfcf25832014-12-18 17:42:32 -08001587 return NULL;
1588 }
Adam Langley95c29f32014-06-20 12:00:00 -07001589
Adam Langleyfcf25832014-12-18 17:42:32 -08001590 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001591 OPENSSL_PUT_ERROR(SSL, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
Adam Langleyfcf25832014-12-18 17:42:32 -08001592 goto err;
1593 }
Adam Langley95c29f32014-06-20 12:00:00 -07001594
Adam Langleyfcf25832014-12-18 17:42:32 -08001595 ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1596 if (ret == NULL) {
1597 goto err;
1598 }
Adam Langley95c29f32014-06-20 12:00:00 -07001599
Adam Langleyfcf25832014-12-18 17:42:32 -08001600 memset(ret, 0, sizeof(SSL_CTX));
Adam Langley95c29f32014-06-20 12:00:00 -07001601
David Benjamin4831c332015-05-16 11:43:13 -04001602 ret->method = method->method;
Adam Langley95c29f32014-06-20 12:00:00 -07001603
Adam Langley4bdb6e42015-05-15 15:29:21 -07001604 CRYPTO_MUTEX_init(&ret->lock);
1605
Adam Langleyfcf25832014-12-18 17:42:32 -08001606 ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
1607 ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
Adam Langley95c29f32014-06-20 12:00:00 -07001608
Adam Langleyfcf25832014-12-18 17:42:32 -08001609 /* We take the system default */
1610 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001611
Adam Langleyfcf25832014-12-18 17:42:32 -08001612 ret->references = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001613
Adam Langleyfcf25832014-12-18 17:42:32 -08001614 ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
Adam Langleyfcf25832014-12-18 17:42:32 -08001615 ret->verify_mode = SSL_VERIFY_NONE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001616 ret->cert = ssl_cert_new();
1617 if (ret->cert == NULL) {
1618 goto err;
1619 }
Adam Langley95c29f32014-06-20 12:00:00 -07001620
Adam Langleyfcf25832014-12-18 17:42:32 -08001621 ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
1622 if (ret->sessions == NULL) {
1623 goto err;
1624 }
1625 ret->cert_store = X509_STORE_new();
1626 if (ret->cert_store == NULL) {
1627 goto err;
1628 }
Adam Langley95c29f32014-06-20 12:00:00 -07001629
Adam Langleyfcf25832014-12-18 17:42:32 -08001630 ssl_create_cipher_list(ret->method, &ret->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001631 &ret->cipher_list_by_id, SSL_DEFAULT_CIPHER_LIST);
Adam Langleyfcf25832014-12-18 17:42:32 -08001632 if (ret->cipher_list == NULL ||
1633 sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001634 OPENSSL_PUT_ERROR(SSL, SSL_R_LIBRARY_HAS_NO_CIPHERS);
Adam Langleyfcf25832014-12-18 17:42:32 -08001635 goto err2;
1636 }
Adam Langley95c29f32014-06-20 12:00:00 -07001637
Adam Langleyfcf25832014-12-18 17:42:32 -08001638 ret->param = X509_VERIFY_PARAM_new();
1639 if (!ret->param) {
1640 goto err;
1641 }
Adam Langley95c29f32014-06-20 12:00:00 -07001642
Adam Langleyfcf25832014-12-18 17:42:32 -08001643 ret->client_CA = sk_X509_NAME_new_null();
1644 if (ret->client_CA == NULL) {
1645 goto err;
1646 }
Adam Langley95c29f32014-06-20 12:00:00 -07001647
David Benjamin9f33fc62015-04-15 17:29:53 -04001648 CRYPTO_new_ex_data(&g_ex_data_class_ssl_ctx, ret, &ret->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001649
Adam Langleyfcf25832014-12-18 17:42:32 -08001650 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
David Benjamin422d3a42014-08-20 11:09:03 -04001651
Adam Langleyfcf25832014-12-18 17:42:32 -08001652 /* Setup RFC4507 ticket keys */
1653 if (!RAND_bytes(ret->tlsext_tick_key_name, 16) ||
1654 !RAND_bytes(ret->tlsext_tick_hmac_key, 16) ||
1655 !RAND_bytes(ret->tlsext_tick_aes_key, 16)) {
1656 ret->options |= SSL_OP_NO_TICKET;
1657 }
Adam Langley95c29f32014-06-20 12:00:00 -07001658
Adam Langleyfcf25832014-12-18 17:42:32 -08001659 /* Default is to connect to non-RI servers. When RI is more widely deployed
1660 * might change this. */
1661 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
1662
1663 /* Lock the SSL_CTX to the specified version, for compatibility with legacy
1664 * uses of SSL_METHOD. */
David Benjamin4831c332015-05-16 11:43:13 -04001665 if (method->version != 0) {
1666 SSL_CTX_set_max_version(ret, method->version);
1667 SSL_CTX_set_min_version(ret, method->version);
Adam Langleyfcf25832014-12-18 17:42:32 -08001668 }
1669
1670 return ret;
1671
Adam Langley95c29f32014-06-20 12:00:00 -07001672err:
David Benjamin3570d732015-06-29 00:28:17 -04001673 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -07001674err2:
David Benjamin2755a3e2015-04-22 16:17:58 -04001675 SSL_CTX_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -08001676 return NULL;
1677}
Adam Langley95c29f32014-06-20 12:00:00 -07001678
David Benjamin4fcc2e22015-04-22 12:58:16 -04001679void SSL_CTX_free(SSL_CTX *ctx) {
1680 if (ctx == NULL ||
Adam Langley0b5e3902015-05-15 13:08:38 -07001681 !CRYPTO_refcount_dec_and_test_zero(&ctx->references)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001682 return;
1683 }
Adam Langley95c29f32014-06-20 12:00:00 -07001684
David Benjamin2755a3e2015-04-22 16:17:58 -04001685 X509_VERIFY_PARAM_free(ctx->param);
Adam Langley95c29f32014-06-20 12:00:00 -07001686
Adam Langleyfcf25832014-12-18 17:42:32 -08001687 /* Free internal session cache. However: the remove_cb() may reference the
1688 * ex_data of SSL_CTX, thus the ex_data store can only be removed after the
1689 * sessions were flushed. As the ex_data handling routines might also touch
1690 * the session cache, the most secure solution seems to be: empty (flush) the
1691 * cache, then free ex_data, then finally free the cache. (See ticket
1692 * [openssl.org #212].) */
David Benjamin2755a3e2015-04-22 16:17:58 -04001693 SSL_CTX_flush_sessions(ctx, 0);
Adam Langley95c29f32014-06-20 12:00:00 -07001694
David Benjamin4fcc2e22015-04-22 12:58:16 -04001695 CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, ctx, &ctx->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001696
Adam Langley4bdb6e42015-05-15 15:29:21 -07001697 CRYPTO_MUTEX_cleanup(&ctx->lock);
David Benjamin2755a3e2015-04-22 16:17:58 -04001698 lh_SSL_SESSION_free(ctx->sessions);
1699 X509_STORE_free(ctx->cert_store);
1700 ssl_cipher_preference_list_free(ctx->cipher_list);
1701 sk_SSL_CIPHER_free(ctx->cipher_list_by_id);
1702 ssl_cipher_preference_list_free(ctx->cipher_list_tls11);
1703 ssl_cert_free(ctx->cert);
Adam Langley09505632015-07-30 18:10:13 -07001704 sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->client_custom_extensions,
1705 SSL_CUSTOM_EXTENSION_free);
1706 sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->server_custom_extensions,
1707 SSL_CUSTOM_EXTENSION_free);
David Benjamin2755a3e2015-04-22 16:17:58 -04001708 sk_X509_NAME_pop_free(ctx->client_CA, X509_NAME_free);
David Benjamin2755a3e2015-04-22 16:17:58 -04001709 sk_SRTP_PROTECTION_PROFILE_free(ctx->srtp_profiles);
1710 OPENSSL_free(ctx->psk_identity_hint);
David Benjamin2755a3e2015-04-22 16:17:58 -04001711 OPENSSL_free(ctx->tlsext_ellipticcurvelist);
1712 OPENSSL_free(ctx->alpn_client_proto_list);
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001713 OPENSSL_free(ctx->ocsp_response);
David Benjamin2755a3e2015-04-22 16:17:58 -04001714 EVP_PKEY_free(ctx->tlsext_channel_id_private);
1715 BIO_free(ctx->keylog_bio);
Adam Langley95c29f32014-06-20 12:00:00 -07001716
David Benjamin4fcc2e22015-04-22 12:58:16 -04001717 OPENSSL_free(ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08001718}
Adam Langley95c29f32014-06-20 12:00:00 -07001719
Adam Langleyfcf25832014-12-18 17:42:32 -08001720void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) {
1721 ctx->default_passwd_callback = cb;
1722}
Adam Langley95c29f32014-06-20 12:00:00 -07001723
Adam Langleyfcf25832014-12-18 17:42:32 -08001724void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u) {
1725 ctx->default_passwd_callback_userdata = u;
1726}
Adam Langley95c29f32014-06-20 12:00:00 -07001727
Adam Langleyfcf25832014-12-18 17:42:32 -08001728void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
1729 int (*cb)(X509_STORE_CTX *, void *),
1730 void *arg) {
1731 ctx->app_verify_callback = cb;
1732 ctx->app_verify_arg = arg;
1733}
Adam Langley95c29f32014-06-20 12:00:00 -07001734
Adam Langleyfcf25832014-12-18 17:42:32 -08001735void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
1736 int (*cb)(int, X509_STORE_CTX *)) {
1737 ctx->verify_mode = mode;
1738 ctx->default_verify_callback = cb;
1739}
Adam Langley95c29f32014-06-20 12:00:00 -07001740
Adam Langleyfcf25832014-12-18 17:42:32 -08001741void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
1742 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
1743}
Adam Langley1258b6a2014-06-20 12:00:00 -07001744
David Benjamin7481d392015-07-05 19:38:46 -04001745void SSL_CTX_set_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl, void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08001746 void *arg) {
David Benjamin7481d392015-07-05 19:38:46 -04001747 ssl_cert_set_cert_cb(ctx->cert, cb, arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08001748}
David Benjamin859ec3c2014-09-02 16:29:36 -04001749
David Benjamin7481d392015-07-05 19:38:46 -04001750void SSL_set_cert_cb(SSL *ssl, int (*cb)(SSL *ssl, void *arg), void *arg) {
1751 ssl_cert_set_cert_cb(ssl->cert, cb, arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08001752}
Adam Langley95c29f32014-06-20 12:00:00 -07001753
David Benjamin107db582015-04-08 00:41:59 -04001754void ssl_get_compatible_server_ciphers(SSL *s, uint32_t *out_mask_k,
1755 uint32_t *out_mask_a) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001756 CERT *c = s->cert;
David Benjamind1d80782015-07-05 11:54:09 -04001757 int have_rsa_cert = 0, dh_tmp;
David Benjamin107db582015-04-08 00:41:59 -04001758 uint32_t mask_k, mask_a;
David Benjamind1d80782015-07-05 11:54:09 -04001759 int have_ecc_cert = 0, ecdsa_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001760 X509 *x;
Adam Langley95c29f32014-06-20 12:00:00 -07001761
Adam Langleyfcf25832014-12-18 17:42:32 -08001762 dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001763
nagendra modadugu601448a2015-07-24 09:31:31 -07001764 if (s->cert->x509 != NULL && ssl_has_private_key(s)) {
1765 if (ssl_private_key_type(s) == EVP_PKEY_RSA) {
David Benjamind1d80782015-07-05 11:54:09 -04001766 have_rsa_cert = 1;
nagendra modadugu601448a2015-07-24 09:31:31 -07001767 } else if (ssl_private_key_type(s) == EVP_PKEY_EC) {
David Benjamind1d80782015-07-05 11:54:09 -04001768 have_ecc_cert = 1;
1769 }
1770 }
nagendra modadugu601448a2015-07-24 09:31:31 -07001771
Adam Langleyfcf25832014-12-18 17:42:32 -08001772 mask_k = 0;
1773 mask_a = 0;
David Benjaminf31e6812014-11-13 18:05:55 -05001774
Adam Langleyfcf25832014-12-18 17:42:32 -08001775 if (dh_tmp) {
David Benjamin7061e282015-03-19 11:10:48 -04001776 mask_k |= SSL_kDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001777 }
David Benjaminbb20f522015-07-04 17:18:14 -04001778 if (have_rsa_cert) {
1779 mask_k |= SSL_kRSA;
Adam Langleyfcf25832014-12-18 17:42:32 -08001780 mask_a |= SSL_aRSA;
1781 }
Adam Langley95c29f32014-06-20 12:00:00 -07001782
Adam Langleyfcf25832014-12-18 17:42:32 -08001783 /* An ECC certificate may be usable for ECDSA cipher suites depending on the
1784 * key usage extension and on the client's curve preferences. */
1785 if (have_ecc_cert) {
David Benjamind1d80782015-07-05 11:54:09 -04001786 x = c->x509;
Adam Langleyfcf25832014-12-18 17:42:32 -08001787 /* This call populates extension flags (ex_flags). */
1788 X509_check_purpose(x, -1, 0);
1789 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE)
1790 ? (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)
1791 : 1;
1792 if (!tls1_check_ec_cert(s, x)) {
1793 ecdsa_ok = 0;
1794 }
1795 if (ecdsa_ok) {
1796 mask_a |= SSL_aECDSA;
1797 }
1798 }
Adam Langley95c29f32014-06-20 12:00:00 -07001799
Adam Langleyfcf25832014-12-18 17:42:32 -08001800 /* If we are considering an ECC cipher suite that uses an ephemeral EC
1801 * key, check it. */
David Benjamindd978782015-04-24 15:20:13 -04001802 if (tls1_check_ec_tmp_key(s)) {
David Benjamin7061e282015-03-19 11:10:48 -04001803 mask_k |= SSL_kECDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001804 }
Adam Langley95c29f32014-06-20 12:00:00 -07001805
Adam Langleyfcf25832014-12-18 17:42:32 -08001806 /* PSK requires a server callback. */
1807 if (s->psk_server_callback != NULL) {
1808 mask_k |= SSL_kPSK;
1809 mask_a |= SSL_aPSK;
1810 }
Adam Langley95c29f32014-06-20 12:00:00 -07001811
Adam Langleyfcf25832014-12-18 17:42:32 -08001812 *out_mask_k = mask_k;
1813 *out_mask_a = mask_a;
1814}
Adam Langley95c29f32014-06-20 12:00:00 -07001815
Adam Langleyfcf25832014-12-18 17:42:32 -08001816void ssl_update_cache(SSL *s, int mode) {
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001817 /* Never cache sessions with empty session IDs. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001818 if (s->session->session_id_length == 0) {
1819 return;
1820 }
Adam Langley95c29f32014-06-20 12:00:00 -07001821
David Benjamin95d31822015-06-15 19:53:32 -04001822 int has_new_session = !s->hit;
1823 if (!s->server && s->tlsext_ticket_expected) {
1824 /* A client may see new sessions on abbreviated handshakes if the server
1825 * decides to renew the ticket. Once the handshake is completed, it should
1826 * be inserted into the cache. */
1827 has_new_session = 1;
1828 }
1829
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001830 SSL_CTX *ctx = s->initial_ctx;
David Benjamin95d31822015-06-15 19:53:32 -04001831 if ((ctx->session_cache_mode & mode) == mode && has_new_session &&
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001832 ((ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) ||
1833 SSL_CTX_add_session(ctx, s->session)) &&
1834 ctx->new_session_cb != NULL) {
1835 /* Note: |new_session_cb| is called whether the internal session cache is
1836 * used or not. */
1837 if (!ctx->new_session_cb(s, SSL_SESSION_up_ref(s->session))) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001838 SSL_SESSION_free(s->session);
1839 }
1840 }
Adam Langley95c29f32014-06-20 12:00:00 -07001841
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001842 if (!(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR) &&
1843 !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) &&
1844 (ctx->session_cache_mode & mode) == mode) {
1845 /* Automatically flush the internal session cache every 255 connections. */
1846 int flush_cache = 0;
Adam Langley4bdb6e42015-05-15 15:29:21 -07001847 CRYPTO_MUTEX_lock_write(&ctx->lock);
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001848 ctx->handshakes_since_cache_flush++;
1849 if (ctx->handshakes_since_cache_flush >= 255) {
1850 flush_cache = 1;
1851 ctx->handshakes_since_cache_flush = 0;
1852 }
Adam Langley4bdb6e42015-05-15 15:29:21 -07001853 CRYPTO_MUTEX_unlock(&ctx->lock);
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001854
1855 if (flush_cache) {
1856 SSL_CTX_flush_sessions(ctx, (unsigned long)time(NULL));
Adam Langleyfcf25832014-12-18 17:42:32 -08001857 }
1858 }
1859}
Adam Langley95c29f32014-06-20 12:00:00 -07001860
David Benjamin1a5c50f2015-03-11 16:22:37 -04001861int SSL_get_error(const SSL *s, int ret_code) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001862 int reason;
David Benjamin1a5c50f2015-03-11 16:22:37 -04001863 uint32_t err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001864 BIO *bio;
Adam Langley95c29f32014-06-20 12:00:00 -07001865
David Benjamin1a5c50f2015-03-11 16:22:37 -04001866 if (ret_code > 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001867 return SSL_ERROR_NONE;
1868 }
Adam Langley95c29f32014-06-20 12:00:00 -07001869
Adam Langleyfcf25832014-12-18 17:42:32 -08001870 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
1871 * where we do encode the error */
David Benjamin1a5c50f2015-03-11 16:22:37 -04001872 err = ERR_peek_error();
1873 if (err != 0) {
1874 if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001875 return SSL_ERROR_SYSCALL;
1876 }
1877 return SSL_ERROR_SSL;
1878 }
Adam Langley95c29f32014-06-20 12:00:00 -07001879
David Benjamin1a5c50f2015-03-11 16:22:37 -04001880 if (ret_code == 0) {
David Benjamin9a38e922015-01-22 16:06:11 -05001881 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
1882 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) {
1883 /* The socket was cleanly shut down with a close_notify. */
1884 return SSL_ERROR_ZERO_RETURN;
1885 }
1886 /* An EOF was observed which violates the protocol, and the underlying
1887 * transport does not participate in the error queue. Bubble up to the
1888 * caller. */
1889 return SSL_ERROR_SYSCALL;
Adam Langleyfcf25832014-12-18 17:42:32 -08001890 }
Adam Langleyb2ce0582014-06-20 12:00:00 -07001891
Adam Langleyfcf25832014-12-18 17:42:32 -08001892 if (SSL_want_session(s)) {
1893 return SSL_ERROR_PENDING_SESSION;
1894 }
Adam Langley95c29f32014-06-20 12:00:00 -07001895
Adam Langleyfcf25832014-12-18 17:42:32 -08001896 if (SSL_want_certificate(s)) {
1897 return SSL_ERROR_PENDING_CERTIFICATE;
1898 }
Adam Langley95c29f32014-06-20 12:00:00 -07001899
Adam Langleyfcf25832014-12-18 17:42:32 -08001900 if (SSL_want_read(s)) {
1901 bio = SSL_get_rbio(s);
1902 if (BIO_should_read(bio)) {
1903 return SSL_ERROR_WANT_READ;
1904 }
Adam Langley95c29f32014-06-20 12:00:00 -07001905
Adam Langleyfcf25832014-12-18 17:42:32 -08001906 if (BIO_should_write(bio)) {
1907 /* This one doesn't make too much sense ... We never try to write to the
1908 * rbio, and an application program where rbio and wbio are separate
1909 * couldn't even know what it should wait for. However if we ever set
1910 * s->rwstate incorrectly (so that we have SSL_want_read(s) instead of
1911 * SSL_want_write(s)) and rbio and wbio *are* the same, this test works
1912 * around that bug; so it might be safer to keep it. */
1913 return SSL_ERROR_WANT_WRITE;
1914 }
Adam Langley95c29f32014-06-20 12:00:00 -07001915
Adam Langleyfcf25832014-12-18 17:42:32 -08001916 if (BIO_should_io_special(bio)) {
1917 reason = BIO_get_retry_reason(bio);
1918 if (reason == BIO_RR_CONNECT) {
1919 return SSL_ERROR_WANT_CONNECT;
1920 }
Adam Langley95c29f32014-06-20 12:00:00 -07001921
Adam Langleyfcf25832014-12-18 17:42:32 -08001922 if (reason == BIO_RR_ACCEPT) {
1923 return SSL_ERROR_WANT_ACCEPT;
1924 }
Adam Langley95c29f32014-06-20 12:00:00 -07001925
Adam Langleyfcf25832014-12-18 17:42:32 -08001926 return SSL_ERROR_SYSCALL; /* unknown */
1927 }
1928 }
Adam Langley95c29f32014-06-20 12:00:00 -07001929
Adam Langleyfcf25832014-12-18 17:42:32 -08001930 if (SSL_want_write(s)) {
1931 bio = SSL_get_wbio(s);
1932 if (BIO_should_write(bio)) {
1933 return SSL_ERROR_WANT_WRITE;
1934 }
Adam Langley95c29f32014-06-20 12:00:00 -07001935
Adam Langleyfcf25832014-12-18 17:42:32 -08001936 if (BIO_should_read(bio)) {
1937 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
1938 return SSL_ERROR_WANT_READ;
1939 }
Adam Langley95c29f32014-06-20 12:00:00 -07001940
Adam Langleyfcf25832014-12-18 17:42:32 -08001941 if (BIO_should_io_special(bio)) {
1942 reason = BIO_get_retry_reason(bio);
1943 if (reason == BIO_RR_CONNECT) {
1944 return SSL_ERROR_WANT_CONNECT;
1945 }
Adam Langley95c29f32014-06-20 12:00:00 -07001946
Adam Langleyfcf25832014-12-18 17:42:32 -08001947 if (reason == BIO_RR_ACCEPT) {
1948 return SSL_ERROR_WANT_ACCEPT;
1949 }
Adam Langley95c29f32014-06-20 12:00:00 -07001950
Adam Langleyfcf25832014-12-18 17:42:32 -08001951 return SSL_ERROR_SYSCALL;
1952 }
1953 }
Adam Langley95c29f32014-06-20 12:00:00 -07001954
Adam Langleyfcf25832014-12-18 17:42:32 -08001955 if (SSL_want_x509_lookup(s)) {
1956 return SSL_ERROR_WANT_X509_LOOKUP;
1957 }
Adam Langley95c29f32014-06-20 12:00:00 -07001958
Adam Langleyfcf25832014-12-18 17:42:32 -08001959 if (SSL_want_channel_id_lookup(s)) {
1960 return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
1961 }
Adam Langley0f4746e2014-08-13 12:26:32 -07001962
David Benjaminb4d65fd2015-05-29 17:11:21 -04001963 if (SSL_want_private_key_operation(s)) {
1964 return SSL_ERROR_WANT_PRIVATE_KEY_OPERATION;
1965 }
1966
Adam Langleyfcf25832014-12-18 17:42:32 -08001967 return SSL_ERROR_SYSCALL;
1968}
Adam Langley0f4746e2014-08-13 12:26:32 -07001969
Adam Langleyfcf25832014-12-18 17:42:32 -08001970int SSL_do_handshake(SSL *s) {
1971 int ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001972
Adam Langleyfcf25832014-12-18 17:42:32 -08001973 if (s->handshake_func == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001974 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_TYPE_NOT_SET);
Adam Langleyfcf25832014-12-18 17:42:32 -08001975 return -1;
1976 }
Adam Langley95c29f32014-06-20 12:00:00 -07001977
Adam Langleyfcf25832014-12-18 17:42:32 -08001978 if (SSL_in_init(s)) {
1979 ret = s->handshake_func(s);
1980 }
1981 return ret;
1982}
Adam Langley95c29f32014-06-20 12:00:00 -07001983
David Benjamin4831c332015-05-16 11:43:13 -04001984void SSL_set_accept_state(SSL *ssl) {
1985 ssl->server = 1;
1986 ssl->shutdown = 0;
David Benjamin8ec88102015-05-15 23:40:13 -04001987 ssl->state = SSL_ST_ACCEPT;
David Benjamin4831c332015-05-16 11:43:13 -04001988 ssl->handshake_func = ssl->method->ssl_accept;
Adam Langleyfcf25832014-12-18 17:42:32 -08001989 /* clear the current cipher */
David Benjamin4831c332015-05-16 11:43:13 -04001990 ssl_clear_cipher_ctx(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001991}
Adam Langley95c29f32014-06-20 12:00:00 -07001992
David Benjamin4831c332015-05-16 11:43:13 -04001993void SSL_set_connect_state(SSL *ssl) {
1994 ssl->server = 0;
1995 ssl->shutdown = 0;
David Benjamin8ec88102015-05-15 23:40:13 -04001996 ssl->state = SSL_ST_CONNECT;
David Benjamin4831c332015-05-16 11:43:13 -04001997 ssl->handshake_func = ssl->method->ssl_connect;
Adam Langleyfcf25832014-12-18 17:42:32 -08001998 /* clear the current cipher */
David Benjamin4831c332015-05-16 11:43:13 -04001999 ssl_clear_cipher_ctx(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08002000}
Adam Langley95c29f32014-06-20 12:00:00 -07002001
Adam Langleyfcf25832014-12-18 17:42:32 -08002002static const char *ssl_get_version(int version) {
2003 switch (version) {
2004 case TLS1_2_VERSION:
2005 return "TLSv1.2";
Adam Langley95c29f32014-06-20 12:00:00 -07002006
Adam Langleyfcf25832014-12-18 17:42:32 -08002007 case TLS1_1_VERSION:
2008 return "TLSv1.1";
Adam Langley95c29f32014-06-20 12:00:00 -07002009
Adam Langleyfcf25832014-12-18 17:42:32 -08002010 case TLS1_VERSION:
2011 return "TLSv1";
Adam Langley95c29f32014-06-20 12:00:00 -07002012
Adam Langleyfcf25832014-12-18 17:42:32 -08002013 case SSL3_VERSION:
2014 return "SSLv3";
Adam Langley95c29f32014-06-20 12:00:00 -07002015
David Benjamin1c722b72015-04-20 13:53:10 -04002016 case DTLS1_VERSION:
2017 return "DTLSv1";
2018
2019 case DTLS1_2_VERSION:
2020 return "DTLSv1.2";
2021
Adam Langleyfcf25832014-12-18 17:42:32 -08002022 default:
2023 return "unknown";
2024 }
2025}
Adam Langley95c29f32014-06-20 12:00:00 -07002026
Adam Langleyfcf25832014-12-18 17:42:32 -08002027const char *SSL_get_version(const SSL *s) {
2028 return ssl_get_version(s->version);
2029}
Adam Langley95c29f32014-06-20 12:00:00 -07002030
Adam Langleyfcf25832014-12-18 17:42:32 -08002031const char *SSL_SESSION_get_version(const SSL_SESSION *sess) {
2032 return ssl_get_version(sess->ssl_version);
2033}
Adam Langley95c29f32014-06-20 12:00:00 -07002034
Sigbjorn Vik2b23d242015-06-29 15:07:26 +02002035const char* SSL_get_curve_name(uint16_t curve_id) {
2036 return tls1_ec_curve_id2name(curve_id);
2037}
2038
Adam Langleyfcf25832014-12-18 17:42:32 -08002039void ssl_clear_cipher_ctx(SSL *s) {
David Benjamin31a07792015-03-03 14:20:26 -05002040 SSL_AEAD_CTX_free(s->aead_read_ctx);
2041 s->aead_read_ctx = NULL;
2042 SSL_AEAD_CTX_free(s->aead_write_ctx);
2043 s->aead_write_ctx = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002044}
Adam Langley95c29f32014-06-20 12:00:00 -07002045
Adam Langleyfcf25832014-12-18 17:42:32 -08002046X509 *SSL_get_certificate(const SSL *s) {
2047 if (s->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04002048 return s->cert->x509;
Adam Langleyfcf25832014-12-18 17:42:32 -08002049 }
2050
2051 return NULL;
2052}
2053
2054EVP_PKEY *SSL_get_privatekey(const SSL *s) {
2055 if (s->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04002056 return s->cert->privatekey;
Adam Langleyfcf25832014-12-18 17:42:32 -08002057 }
2058
2059 return NULL;
2060}
2061
2062X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
2063 if (ctx->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04002064 return ctx->cert->x509;
Adam Langleyfcf25832014-12-18 17:42:32 -08002065 }
2066
2067 return NULL;
2068}
2069
2070EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
2071 if (ctx->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04002072 return ctx->cert->privatekey;
Adam Langleyfcf25832014-12-18 17:42:32 -08002073 }
2074
2075 return NULL;
2076}
2077
2078const SSL_CIPHER *SSL_get_current_cipher(const SSL *s) {
David Benjamina07c0fc2015-05-13 13:19:42 -04002079 if (s->aead_write_ctx == NULL) {
2080 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002081 }
David Benjamina07c0fc2015-05-13 13:19:42 -04002082 return s->aead_write_ctx->cipher;
Adam Langleyfcf25832014-12-18 17:42:32 -08002083}
2084
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07002085const COMP_METHOD *SSL_get_current_compression(SSL *s) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002086
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07002087const COMP_METHOD *SSL_get_current_expansion(SSL *s) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002088
2089int ssl_init_wbio_buffer(SSL *s, int push) {
2090 BIO *bbio;
2091
2092 if (s->bbio == NULL) {
2093 bbio = BIO_new(BIO_f_buffer());
2094 if (bbio == NULL) {
2095 return 0;
2096 }
2097 s->bbio = bbio;
2098 } else {
2099 bbio = s->bbio;
2100 if (s->bbio == s->wbio) {
2101 s->wbio = BIO_pop(s->wbio);
2102 }
2103 }
2104
2105 BIO_reset(bbio);
2106 if (!BIO_set_read_buffer_size(bbio, 1)) {
David Benjamin3570d732015-06-29 00:28:17 -04002107 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -08002108 return 0;
2109 }
2110
2111 if (push) {
2112 if (s->wbio != bbio) {
2113 s->wbio = BIO_push(bbio, s->wbio);
2114 }
2115 } else {
2116 if (s->wbio == bbio) {
2117 s->wbio = BIO_pop(bbio);
2118 }
2119 }
2120
2121 return 1;
2122}
2123
2124void ssl_free_wbio_buffer(SSL *s) {
2125 if (s->bbio == NULL) {
2126 return;
2127 }
2128
2129 if (s->bbio == s->wbio) {
2130 /* remove buffering */
2131 s->wbio = BIO_pop(s->wbio);
2132 }
2133
2134 BIO_free(s->bbio);
2135 s->bbio = NULL;
2136}
2137
2138void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
2139 ctx->quiet_shutdown = mode;
2140}
2141
2142int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
2143 return ctx->quiet_shutdown;
2144}
2145
2146void SSL_set_quiet_shutdown(SSL *s, int mode) { s->quiet_shutdown = mode; }
2147
2148int SSL_get_quiet_shutdown(const SSL *s) { return s->quiet_shutdown; }
2149
2150void SSL_set_shutdown(SSL *s, int mode) { s->shutdown = mode; }
2151
2152int SSL_get_shutdown(const SSL *s) { return s->shutdown; }
2153
2154int SSL_version(const SSL *s) { return s->version; }
2155
2156SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
2157
2158SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
2159 if (ssl->ctx == ctx) {
2160 return ssl->ctx;
2161 }
2162
2163 if (ctx == NULL) {
2164 ctx = ssl->initial_ctx;
2165 }
2166
David Benjamin2755a3e2015-04-22 16:17:58 -04002167 ssl_cert_free(ssl->cert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002168 ssl->cert = ssl_cert_dup(ctx->cert);
David Benjamin2755a3e2015-04-22 16:17:58 -04002169
Adam Langley0b5e3902015-05-15 13:08:38 -07002170 CRYPTO_refcount_inc(&ctx->references);
David Benjamin2755a3e2015-04-22 16:17:58 -04002171 SSL_CTX_free(ssl->ctx); /* decrement reference count */
Adam Langleyfcf25832014-12-18 17:42:32 -08002172 ssl->ctx = ctx;
2173
2174 ssl->sid_ctx_length = ctx->sid_ctx_length;
2175 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2176 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2177
2178 return ssl->ctx;
2179}
2180
2181int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
2182 return X509_STORE_set_default_paths(ctx->cert_store);
2183}
Adam Langley95c29f32014-06-20 12:00:00 -07002184
2185int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
Adam Langleyfcf25832014-12-18 17:42:32 -08002186 const char *CApath) {
2187 return X509_STORE_load_locations(ctx->cert_store, CAfile, CApath);
2188}
Adam Langley95c29f32014-06-20 12:00:00 -07002189
2190void SSL_set_info_callback(SSL *ssl,
Adam Langleyfcf25832014-12-18 17:42:32 -08002191 void (*cb)(const SSL *ssl, int type, int val)) {
2192 ssl->info_callback = cb;
2193}
Adam Langley95c29f32014-06-20 12:00:00 -07002194
Adam Langleyfcf25832014-12-18 17:42:32 -08002195void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/, int /*type*/,
2196 int /*val*/) {
2197 return ssl->info_callback;
2198}
Adam Langley95c29f32014-06-20 12:00:00 -07002199
Adam Langleyfcf25832014-12-18 17:42:32 -08002200int SSL_state(const SSL *ssl) { return ssl->state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002201
David Benjaminece089c2015-05-15 23:52:42 -04002202void SSL_set_state(SSL *ssl, int state) { }
Adam Langley95c29f32014-06-20 12:00:00 -07002203
Adam Langleyfcf25832014-12-18 17:42:32 -08002204void SSL_set_verify_result(SSL *ssl, long arg) { ssl->verify_result = arg; }
Adam Langley95c29f32014-06-20 12:00:00 -07002205
Adam Langleyfcf25832014-12-18 17:42:32 -08002206long SSL_get_verify_result(const SSL *ssl) { return ssl->verify_result; }
Adam Langley95c29f32014-06-20 12:00:00 -07002207
Adam Langleyfcf25832014-12-18 17:42:32 -08002208int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2209 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002210 int index;
2211 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp,
2212 new_func, dup_func, free_func)) {
2213 return -1;
2214 }
2215 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002216}
Adam Langley95c29f32014-06-20 12:00:00 -07002217
Adam Langleyfcf25832014-12-18 17:42:32 -08002218int SSL_set_ex_data(SSL *s, int idx, void *arg) {
2219 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2220}
Adam Langley95c29f32014-06-20 12:00:00 -07002221
Adam Langleyfcf25832014-12-18 17:42:32 -08002222void *SSL_get_ex_data(const SSL *s, int idx) {
2223 return CRYPTO_get_ex_data(&s->ex_data, idx);
2224}
Adam Langley95c29f32014-06-20 12:00:00 -07002225
Adam Langleyfcf25832014-12-18 17:42:32 -08002226int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2227 CRYPTO_EX_dup *dup_func,
2228 CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002229 int index;
2230 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp,
2231 new_func, dup_func, free_func)) {
2232 return -1;
2233 }
2234 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002235}
Adam Langley95c29f32014-06-20 12:00:00 -07002236
Adam Langleyfcf25832014-12-18 17:42:32 -08002237int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg) {
2238 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2239}
Adam Langley95c29f32014-06-20 12:00:00 -07002240
Adam Langleyfcf25832014-12-18 17:42:32 -08002241void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx) {
2242 return CRYPTO_get_ex_data(&s->ex_data, idx);
2243}
Adam Langley95c29f32014-06-20 12:00:00 -07002244
Adam Langleyfcf25832014-12-18 17:42:32 -08002245X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
2246 return ctx->cert_store;
2247}
Adam Langley95c29f32014-06-20 12:00:00 -07002248
Adam Langleyfcf25832014-12-18 17:42:32 -08002249void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
David Benjamin2755a3e2015-04-22 16:17:58 -04002250 X509_STORE_free(ctx->cert_store);
Adam Langleyfcf25832014-12-18 17:42:32 -08002251 ctx->cert_store = store;
2252}
Adam Langley95c29f32014-06-20 12:00:00 -07002253
Adam Langleyfcf25832014-12-18 17:42:32 -08002254int SSL_want(const SSL *s) { return s->rwstate; }
Adam Langley95c29f32014-06-20 12:00:00 -07002255
Adam Langleyfcf25832014-12-18 17:42:32 -08002256void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
2257 RSA *(*cb)(SSL *ssl, int is_export,
2258 int keylength)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002259}
Adam Langley95c29f32014-06-20 12:00:00 -07002260
Adam Langleyfcf25832014-12-18 17:42:32 -08002261void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
2262 int keylength)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002263}
2264
2265void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
David Benjamin59015c32015-04-26 13:13:55 -04002266 DH *(*callback)(SSL *ssl, int is_export,
2267 int keylength)) {
2268 ctx->cert->dh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002269}
2270
David Benjamin59015c32015-04-26 13:13:55 -04002271void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*callback)(SSL *ssl, int is_export,
2272 int keylength)) {
2273 ssl->cert->dh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002274}
2275
2276void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,
David Benjamindd978782015-04-24 15:20:13 -04002277 EC_KEY *(*callback)(SSL *ssl, int is_export,
2278 int keylength)) {
David Benjamin59015c32015-04-26 13:13:55 -04002279 ctx->cert->ecdh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002280}
2281
2282void SSL_set_tmp_ecdh_callback(SSL *ssl,
David Benjamindd978782015-04-24 15:20:13 -04002283 EC_KEY *(*callback)(SSL *ssl, int is_export,
2284 int keylength)) {
David Benjamin59015c32015-04-26 13:13:55 -04002285 ssl->cert->ecdh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002286}
2287
2288int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
2289 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
David Benjamin3570d732015-06-29 00:28:17 -04002290 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -08002291 return 0;
2292 }
2293
David Benjamin2755a3e2015-04-22 16:17:58 -04002294 OPENSSL_free(ctx->psk_identity_hint);
Adam Langleyfcf25832014-12-18 17:42:32 -08002295
2296 if (identity_hint != NULL) {
2297 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2298 if (ctx->psk_identity_hint == NULL) {
2299 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002300 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002301 } else {
2302 ctx->psk_identity_hint = NULL;
2303 }
Adam Langley95c29f32014-06-20 12:00:00 -07002304
Adam Langleyfcf25832014-12-18 17:42:32 -08002305 return 1;
2306}
2307
2308int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint) {
2309 if (s == NULL) {
2310 return 0;
2311 }
2312
2313 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
David Benjamin3570d732015-06-29 00:28:17 -04002314 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -08002315 return 0;
2316 }
2317
2318 /* Clear currently configured hint, if any. */
David Benjamin2755a3e2015-04-22 16:17:58 -04002319 OPENSSL_free(s->psk_identity_hint);
2320 s->psk_identity_hint = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002321
2322 if (identity_hint != NULL) {
2323 s->psk_identity_hint = BUF_strdup(identity_hint);
2324 if (s->psk_identity_hint == NULL) {
2325 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002326 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002327 }
Adam Langley95c29f32014-06-20 12:00:00 -07002328
Adam Langleyfcf25832014-12-18 17:42:32 -08002329 return 1;
2330}
Adam Langley95c29f32014-06-20 12:00:00 -07002331
Adam Langleyfcf25832014-12-18 17:42:32 -08002332const char *SSL_get_psk_identity_hint(const SSL *s) {
2333 if (s == NULL) {
2334 return NULL;
2335 }
2336 return s->psk_identity_hint;
2337}
Adam Langley95c29f32014-06-20 12:00:00 -07002338
Adam Langleyfcf25832014-12-18 17:42:32 -08002339const char *SSL_get_psk_identity(const SSL *s) {
2340 if (s == NULL || s->session == NULL) {
2341 return NULL;
2342 }
Adam Langley95c29f32014-06-20 12:00:00 -07002343
Adam Langleyfcf25832014-12-18 17:42:32 -08002344 return s->session->psk_identity;
2345}
Adam Langley95c29f32014-06-20 12:00:00 -07002346
Adam Langleyfcf25832014-12-18 17:42:32 -08002347void SSL_set_psk_client_callback(
2348 SSL *s, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2349 unsigned int max_identity_len, uint8_t *psk,
2350 unsigned int max_psk_len)) {
2351 s->psk_client_callback = cb;
2352}
Adam Langley95c29f32014-06-20 12:00:00 -07002353
Adam Langleyfcf25832014-12-18 17:42:32 -08002354void SSL_CTX_set_psk_client_callback(
2355 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2356 unsigned int max_identity_len,
2357 uint8_t *psk, unsigned int max_psk_len)) {
2358 ctx->psk_client_callback = cb;
2359}
Adam Langley95c29f32014-06-20 12:00:00 -07002360
Adam Langleyfcf25832014-12-18 17:42:32 -08002361void SSL_set_psk_server_callback(
2362 SSL *s, unsigned int (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
2363 unsigned int max_psk_len)) {
2364 s->psk_server_callback = cb;
2365}
Adam Langley95c29f32014-06-20 12:00:00 -07002366
Adam Langleyfcf25832014-12-18 17:42:32 -08002367void SSL_CTX_set_psk_server_callback(
2368 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *identity,
2369 uint8_t *psk, unsigned int max_psk_len)) {
2370 ctx->psk_server_callback = cb;
2371}
Adam Langley95c29f32014-06-20 12:00:00 -07002372
Adam Langleyfcf25832014-12-18 17:42:32 -08002373void SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) {
2374 ctx->min_version = version;
2375}
Adam Langley95c29f32014-06-20 12:00:00 -07002376
Adam Langleyfcf25832014-12-18 17:42:32 -08002377void SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) {
2378 ctx->max_version = version;
2379}
Adam Langley0289c732014-06-20 12:00:00 -07002380
Adam Langleyfcf25832014-12-18 17:42:32 -08002381void SSL_set_min_version(SSL *ssl, uint16_t version) {
2382 ssl->min_version = version;
2383}
Adam Langley0289c732014-06-20 12:00:00 -07002384
Adam Langleyfcf25832014-12-18 17:42:32 -08002385void SSL_set_max_version(SSL *ssl, uint16_t version) {
2386 ssl->max_version = version;
2387}
Adam Langley95c29f32014-06-20 12:00:00 -07002388
Adam Langleyfcf25832014-12-18 17:42:32 -08002389void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
2390 void (*cb)(int write_p, int version,
2391 int content_type, const void *buf,
2392 size_t len, SSL *ssl, void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002393 ctx->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002394}
David Benjamin61ecccf2015-05-05 09:44:51 -04002395
2396void SSL_CTX_set_msg_callback_arg(SSL_CTX *ctx, void *arg) {
2397 ctx->msg_callback_arg = arg;
2398}
2399
Adam Langleyfcf25832014-12-18 17:42:32 -08002400void SSL_set_msg_callback(SSL *ssl,
2401 void (*cb)(int write_p, int version, int content_type,
2402 const void *buf, size_t len, SSL *ssl,
2403 void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002404 ssl->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002405}
Adam Langley95c29f32014-06-20 12:00:00 -07002406
David Benjamin61ecccf2015-05-05 09:44:51 -04002407void SSL_set_msg_callback_arg(SSL *ssl, void *arg) {
2408 ssl->msg_callback_arg = arg;
2409}
2410
Adam Langleyfcf25832014-12-18 17:42:32 -08002411void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio) {
David Benjamin2755a3e2015-04-22 16:17:58 -04002412 BIO_free(ctx->keylog_bio);
Adam Langleyfcf25832014-12-18 17:42:32 -08002413 ctx->keylog_bio = keylog_bio;
2414}
Adam Langley95c29f32014-06-20 12:00:00 -07002415
Adam Langleyfcf25832014-12-18 17:42:32 -08002416static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
2417 static const char hextable[] = "0123456789abcdef";
2418 uint8_t *out;
2419 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002420
Adam Langleyfcf25832014-12-18 17:42:32 -08002421 if (!CBB_add_space(cbb, &out, in_len * 2)) {
2422 return 0;
2423 }
Adam Langley95c29f32014-06-20 12:00:00 -07002424
Adam Langleyfcf25832014-12-18 17:42:32 -08002425 for (i = 0; i < in_len; i++) {
2426 *(out++) = (uint8_t)hextable[in[i] >> 4];
2427 *(out++) = (uint8_t)hextable[in[i] & 0xf];
2428 }
Adam Langley95c29f32014-06-20 12:00:00 -07002429
Adam Langleyfcf25832014-12-18 17:42:32 -08002430 return 1;
2431}
David Benjamin859ec3c2014-09-02 16:29:36 -04002432
2433int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -08002434 const uint8_t *encrypted_premaster,
2435 size_t encrypted_premaster_len,
2436 const uint8_t *premaster,
2437 size_t premaster_len) {
2438 BIO *bio = ctx->keylog_bio;
2439 CBB cbb;
2440 uint8_t *out;
2441 size_t out_len;
2442 int ret;
David Benjamin859ec3c2014-09-02 16:29:36 -04002443
Adam Langleyfcf25832014-12-18 17:42:32 -08002444 if (bio == NULL) {
2445 return 1;
2446 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002447
Adam Langleyfcf25832014-12-18 17:42:32 -08002448 if (encrypted_premaster_len < 8) {
David Benjamin3570d732015-06-29 00:28:17 -04002449 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08002450 return 0;
2451 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002452
David Benjamina8653202015-06-28 01:26:10 -04002453 CBB_zero(&cbb);
2454 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len * 2 + 1) ||
2455 !CBB_add_bytes(&cbb, (const uint8_t *)"RSA ", 4) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08002456 /* Only the first 8 bytes of the encrypted premaster secret are
2457 * logged. */
2458 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
2459 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2460 !cbb_add_hex(&cbb, premaster, premaster_len) ||
2461 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2462 !CBB_finish(&cbb, &out, &out_len)) {
2463 CBB_cleanup(&cbb);
2464 return 0;
2465 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002466
Adam Langley4bdb6e42015-05-15 15:29:21 -07002467 CRYPTO_MUTEX_lock_write(&ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -08002468 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
Adam Langley4bdb6e42015-05-15 15:29:21 -07002469 CRYPTO_MUTEX_unlock(&ctx->lock);
David Benjamin859ec3c2014-09-02 16:29:36 -04002470
Adam Langleyfcf25832014-12-18 17:42:32 -08002471 OPENSSL_free(out);
2472 return ret;
Adam Langley95f22882014-06-20 12:00:00 -07002473}
2474
Adam Langleyfcf25832014-12-18 17:42:32 -08002475int ssl_ctx_log_master_secret(SSL_CTX *ctx, const uint8_t *client_random,
2476 size_t client_random_len, const uint8_t *master,
2477 size_t master_len) {
2478 BIO *bio = ctx->keylog_bio;
2479 CBB cbb;
2480 uint8_t *out;
2481 size_t out_len;
2482 int ret;
Adam Langleyadb739e2014-06-20 12:00:00 -07002483
Adam Langleyfcf25832014-12-18 17:42:32 -08002484 if (bio == NULL) {
2485 return 1;
2486 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002487
Adam Langleyfcf25832014-12-18 17:42:32 -08002488 if (client_random_len != 32) {
David Benjamin3570d732015-06-29 00:28:17 -04002489 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08002490 return 0;
2491 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002492
David Benjamina8653202015-06-28 01:26:10 -04002493 CBB_zero(&cbb);
2494 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len * 2 + 1) ||
2495 !CBB_add_bytes(&cbb, (const uint8_t *)"CLIENT_RANDOM ", 14) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08002496 !cbb_add_hex(&cbb, client_random, 32) ||
2497 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2498 !cbb_add_hex(&cbb, master, master_len) ||
2499 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2500 !CBB_finish(&cbb, &out, &out_len)) {
2501 CBB_cleanup(&cbb);
2502 return 0;
2503 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002504
Adam Langley4bdb6e42015-05-15 15:29:21 -07002505 CRYPTO_MUTEX_lock_write(&ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -08002506 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
Adam Langley4bdb6e42015-05-15 15:29:21 -07002507 CRYPTO_MUTEX_unlock(&ctx->lock);
David Benjamine99e9122014-12-11 01:46:01 -05002508
Adam Langleyfcf25832014-12-18 17:42:32 -08002509 OPENSSL_free(out);
2510 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -07002511}
2512
David Benjamined7c4752015-02-16 19:16:46 -05002513int SSL_in_false_start(const SSL *s) {
2514 return s->s3->tmp.in_false_start;
2515}
2516
Adam Langleyfcf25832014-12-18 17:42:32 -08002517int SSL_cutthrough_complete(const SSL *s) {
David Benjamined7c4752015-02-16 19:16:46 -05002518 return SSL_in_false_start(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002519}
Adam Langley95c29f32014-06-20 12:00:00 -07002520
Adam Langleyfcf25832014-12-18 17:42:32 -08002521void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
2522 size_t *ssl_session_size) {
2523 *ssl_size = sizeof(SSL);
2524 *ssl_ctx_size = sizeof(SSL_CTX);
2525 *ssl_session_size = sizeof(SSL_SESSION);
2526}
Feng Lu41aa3252014-11-21 22:47:56 -08002527
David Benjamined7c4752015-02-16 19:16:46 -05002528int ssl3_can_false_start(const SSL *s) {
David Benjamin195dc782015-02-19 13:27:05 -05002529 const SSL_CIPHER *const cipher = SSL_get_current_cipher(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002530
Adam Langleye631d962015-04-01 13:11:01 -07002531 /* False Start only for TLS 1.2 with an ECDHE+AEAD cipher and ALPN or NPN. */
David Benjamin195dc782015-02-19 13:27:05 -05002532 return !SSL_IS_DTLS(s) &&
2533 SSL_version(s) >= TLS1_2_VERSION &&
2534 (s->s3->alpn_selected || s->s3->next_proto_neg_seen) &&
2535 cipher != NULL &&
Adam Langleye631d962015-04-01 13:11:01 -07002536 cipher->algorithm_mkey == SSL_kECDHE &&
David Benjamin195dc782015-02-19 13:27:05 -05002537 (cipher->algorithm_enc == SSL_AES128GCM ||
2538 cipher->algorithm_enc == SSL_AES256GCM ||
2539 cipher->algorithm_enc == SSL_CHACHA20POLY1305);
Adam Langleyfcf25832014-12-18 17:42:32 -08002540}
2541
2542const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version) {
2543 switch (version) {
2544 case SSL3_VERSION:
2545 return &SSLv3_enc_data;
2546
2547 case TLS1_VERSION:
2548 return &TLSv1_enc_data;
2549
David Benjamin9e13e1a2015-03-05 01:56:32 -05002550 case DTLS1_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002551 case TLS1_1_VERSION:
2552 return &TLSv1_1_enc_data;
2553
David Benjamin9e13e1a2015-03-05 01:56:32 -05002554 case DTLS1_2_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002555 case TLS1_2_VERSION:
2556 return &TLSv1_2_enc_data;
2557
Adam Langleyfcf25832014-12-18 17:42:32 -08002558 default:
2559 return NULL;
2560 }
2561}
2562
2563uint16_t ssl3_get_max_server_version(const SSL *s) {
2564 uint16_t max_version;
2565
2566 if (SSL_IS_DTLS(s)) {
2567 max_version = (s->max_version != 0) ? s->max_version : DTLS1_2_VERSION;
2568 if (!(s->options & SSL_OP_NO_DTLSv1_2) && DTLS1_2_VERSION >= max_version) {
2569 return DTLS1_2_VERSION;
2570 }
2571 if (!(s->options & SSL_OP_NO_DTLSv1) && DTLS1_VERSION >= max_version) {
2572 return DTLS1_VERSION;
2573 }
2574 return 0;
2575 }
2576
2577 max_version = (s->max_version != 0) ? s->max_version : TLS1_2_VERSION;
2578 if (!(s->options & SSL_OP_NO_TLSv1_2) && TLS1_2_VERSION <= max_version) {
2579 return TLS1_2_VERSION;
2580 }
2581 if (!(s->options & SSL_OP_NO_TLSv1_1) && TLS1_1_VERSION <= max_version) {
2582 return TLS1_1_VERSION;
2583 }
2584 if (!(s->options & SSL_OP_NO_TLSv1) && TLS1_VERSION <= max_version) {
2585 return TLS1_VERSION;
2586 }
2587 if (!(s->options & SSL_OP_NO_SSLv3) && SSL3_VERSION <= max_version) {
2588 return SSL3_VERSION;
2589 }
2590 return 0;
2591}
2592
2593uint16_t ssl3_get_mutual_version(SSL *s, uint16_t client_version) {
2594 uint16_t version = 0;
2595
2596 if (SSL_IS_DTLS(s)) {
2597 /* Clamp client_version to max_version. */
2598 if (s->max_version != 0 && client_version < s->max_version) {
2599 client_version = s->max_version;
2600 }
2601
2602 if (client_version <= DTLS1_2_VERSION && !(s->options & SSL_OP_NO_DTLSv1_2)) {
2603 version = DTLS1_2_VERSION;
2604 } else if (client_version <= DTLS1_VERSION &&
David Benjaminb18f0242015-03-10 18:30:08 -04002605 !(s->options & SSL_OP_NO_DTLSv1)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002606 version = DTLS1_VERSION;
2607 }
2608
2609 /* Check against min_version. */
2610 if (version != 0 && s->min_version != 0 && version > s->min_version) {
2611 return 0;
2612 }
2613 return version;
2614 } else {
2615 /* Clamp client_version to max_version. */
2616 if (s->max_version != 0 && client_version > s->max_version) {
2617 client_version = s->max_version;
2618 }
2619
2620 if (client_version >= TLS1_2_VERSION && !(s->options & SSL_OP_NO_TLSv1_2)) {
2621 version = TLS1_2_VERSION;
2622 } else if (client_version >= TLS1_1_VERSION &&
2623 !(s->options & SSL_OP_NO_TLSv1_1)) {
2624 version = TLS1_1_VERSION;
2625 } else if (client_version >= TLS1_VERSION && !(s->options & SSL_OP_NO_TLSv1)) {
2626 version = TLS1_VERSION;
2627 } else if (client_version >= SSL3_VERSION && !(s->options & SSL_OP_NO_SSLv3)) {
2628 version = SSL3_VERSION;
2629 }
2630
2631 /* Check against min_version. */
2632 if (version != 0 && s->min_version != 0 && version < s->min_version) {
2633 return 0;
2634 }
2635 return version;
2636 }
2637}
2638
2639uint16_t ssl3_get_max_client_version(SSL *s) {
David Benjamin123a8fd2015-04-26 14:16:41 -04002640 uint32_t options = s->options;
Adam Langleyfcf25832014-12-18 17:42:32 -08002641 uint16_t version = 0;
2642
2643 /* OpenSSL's API for controlling versions entails blacklisting individual
2644 * protocols. This has two problems. First, on the client, the protocol can
2645 * only express a contiguous range of versions. Second, a library consumer
2646 * trying to set a maximum version cannot disable protocol versions that get
2647 * added in a future version of the library.
2648 *
2649 * To account for both of these, OpenSSL interprets the client-side bitmask
2650 * as a min/max range by picking the lowest contiguous non-empty range of
2651 * enabled protocols. Note that this means it is impossible to set a maximum
2652 * version of TLS 1.2 in a future-proof way.
2653 *
2654 * By this scheme, the maximum version is the lowest version V such that V is
2655 * enabled and V+1 is disabled or unimplemented. */
2656 if (SSL_IS_DTLS(s)) {
2657 if (!(options & SSL_OP_NO_DTLSv1_2)) {
2658 version = DTLS1_2_VERSION;
2659 }
2660 if (!(options & SSL_OP_NO_DTLSv1) && (options & SSL_OP_NO_DTLSv1_2)) {
2661 version = DTLS1_VERSION;
2662 }
2663 if (s->max_version != 0 && version < s->max_version) {
2664 version = s->max_version;
2665 }
2666 } else {
2667 if (!(options & SSL_OP_NO_TLSv1_2)) {
2668 version = TLS1_2_VERSION;
2669 }
2670 if (!(options & SSL_OP_NO_TLSv1_1) && (options & SSL_OP_NO_TLSv1_2)) {
2671 version = TLS1_1_VERSION;
2672 }
2673 if (!(options & SSL_OP_NO_TLSv1) && (options & SSL_OP_NO_TLSv1_1)) {
2674 version = TLS1_VERSION;
2675 }
2676 if (!(options & SSL_OP_NO_SSLv3) && (options & SSL_OP_NO_TLSv1)) {
2677 version = SSL3_VERSION;
2678 }
2679 if (s->max_version != 0 && version > s->max_version) {
2680 version = s->max_version;
2681 }
2682 }
2683
2684 return version;
2685}
2686
2687int ssl3_is_version_enabled(SSL *s, uint16_t version) {
2688 if (SSL_IS_DTLS(s)) {
2689 if (s->max_version != 0 && version < s->max_version) {
2690 return 0;
2691 }
2692 if (s->min_version != 0 && version > s->min_version) {
2693 return 0;
2694 }
2695
2696 switch (version) {
2697 case DTLS1_VERSION:
2698 return !(s->options & SSL_OP_NO_DTLSv1);
2699
2700 case DTLS1_2_VERSION:
2701 return !(s->options & SSL_OP_NO_DTLSv1_2);
2702
2703 default:
2704 return 0;
2705 }
2706 } else {
2707 if (s->max_version != 0 && version > s->max_version) {
2708 return 0;
2709 }
2710 if (s->min_version != 0 && version < s->min_version) {
2711 return 0;
2712 }
2713
2714 switch (version) {
2715 case SSL3_VERSION:
2716 return !(s->options & SSL_OP_NO_SSLv3);
2717
2718 case TLS1_VERSION:
2719 return !(s->options & SSL_OP_NO_TLSv1);
2720
2721 case TLS1_1_VERSION:
2722 return !(s->options & SSL_OP_NO_TLSv1_1);
2723
2724 case TLS1_2_VERSION:
2725 return !(s->options & SSL_OP_NO_TLSv1_2);
2726
2727 default:
2728 return 0;
2729 }
2730 }
2731}
2732
David Benjaminea72bd02014-12-21 21:27:41 -05002733uint16_t ssl3_version_from_wire(SSL *s, uint16_t wire_version) {
2734 if (!SSL_IS_DTLS(s)) {
2735 return wire_version;
2736 }
2737
2738 uint16_t tls_version = ~wire_version;
2739 uint16_t version = tls_version + 0x0201;
2740 /* If either component overflowed, clamp it so comparisons still work. */
2741 if ((version >> 8) < (tls_version >> 8)) {
2742 version = 0xff00 | (version & 0xff);
2743 }
2744 if ((version & 0xff) < (tls_version & 0xff)) {
2745 version = (version & 0xff00) | 0xff;
2746 }
2747 /* DTLS 1.0 maps to TLS 1.1, not TLS 1.0. */
2748 if (version == TLS1_VERSION) {
2749 version = TLS1_1_VERSION;
2750 }
2751 return version;
2752}
2753
Adam Langleyfcf25832014-12-18 17:42:32 -08002754int SSL_cache_hit(SSL *s) { return s->hit; }
2755
2756int SSL_is_server(SSL *s) { return s->server; }
2757
Adam Langley524e7172015-02-20 16:04:00 -08002758void SSL_CTX_set_dos_protection_cb(
2759 SSL_CTX *ctx, int (*cb)(const struct ssl_early_callback_ctx *)) {
2760 ctx->dos_protection_cb = cb;
2761}
2762
David Benjaminb16346b2015-04-08 19:16:58 -04002763void SSL_set_reject_peer_renegotiations(SSL *s, int reject) {
David Benjamin897e5e02015-05-12 17:03:54 -04002764 s->accept_peer_renegotiations = !reject;
David Benjaminb16346b2015-04-08 19:16:58 -04002765}
2766
Adam Langley3f92d212015-02-20 15:32:52 -08002767int SSL_get_rc4_state(const SSL *ssl, const RC4_KEY **read_key,
2768 const RC4_KEY **write_key) {
2769 if (ssl->aead_read_ctx == NULL || ssl->aead_write_ctx == NULL) {
2770 return 0;
2771 }
2772
2773 return EVP_AEAD_CTX_get_rc4_state(&ssl->aead_read_ctx->ctx, read_key) &&
2774 EVP_AEAD_CTX_get_rc4_state(&ssl->aead_write_ctx->ctx, write_key);
2775}
David Benjaminda881e92015-04-26 14:45:04 -04002776
Adam Langleyaf0e32c2015-06-03 09:57:23 -07002777int SSL_get_tls_unique(const SSL *ssl, uint8_t *out, size_t *out_len,
2778 size_t max_out) {
2779 /* The tls-unique value is the first Finished message in the handshake, which
2780 * is the client's in a full handshake and the server's for a resumption. See
2781 * https://tools.ietf.org/html/rfc5929#section-3.1. */
2782 const uint8_t *finished = ssl->s3->previous_client_finished;
2783 size_t finished_len = ssl->s3->previous_client_finished_len;
2784 if (ssl->hit) {
2785 /* tls-unique is broken for resumed sessions unless EMS is used. */
2786 if (!ssl->session->extended_master_secret) {
2787 goto err;
2788 }
2789 finished = ssl->s3->previous_server_finished;
2790 finished_len = ssl->s3->previous_server_finished_len;
2791 }
2792
2793 if (!ssl->s3->initial_handshake_complete ||
2794 ssl->version < TLS1_VERSION) {
2795 goto err;
2796 }
2797
2798 *out_len = finished_len;
2799 if (finished_len > max_out) {
2800 *out_len = max_out;
2801 }
2802
2803 memcpy(out, finished, *out_len);
2804 return 1;
2805
2806err:
2807 *out_len = 0;
2808 memset(out, 0, max_out);
2809 return 0;
2810}
2811
David Benjaminda881e92015-04-26 14:45:04 -04002812int SSL_CTX_sess_connect(const SSL_CTX *ctx) { return 0; }
2813int SSL_CTX_sess_connect_good(const SSL_CTX *ctx) { return 0; }
2814int SSL_CTX_sess_connect_renegotiate(const SSL_CTX *ctx) { return 0; }
2815int SSL_CTX_sess_accept(const SSL_CTX *ctx) { return 0; }
2816int SSL_CTX_sess_accept_renegotiate(const SSL_CTX *ctx) { return 0; }
2817int SSL_CTX_sess_accept_good(const SSL_CTX *ctx) { return 0; }
2818int SSL_CTX_sess_hits(const SSL_CTX *ctx) { return 0; }
2819int SSL_CTX_sess_cb_hits(const SSL_CTX *ctx) { return 0; }
2820int SSL_CTX_sess_misses(const SSL_CTX *ctx) { return 0; }
2821int SSL_CTX_sess_timeouts(const SSL_CTX *ctx) { return 0; }
2822int SSL_CTX_sess_cache_full(const SSL_CTX *ctx) { return 0; }
Matt Braithwaite4838d8a2015-08-20 13:20:03 -07002823void ERR_load_SSL_strings(void) {}