blob: fee952395aa489cdf316610492e71437b538247b [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 Langleycef75832015-09-03 14:51:12 -07001084 if (s->version >= TLS1_VERSION && s->ctx != NULL &&
1085 s->ctx->cipher_list_tls10 != NULL) {
1086 return s->ctx->cipher_list_tls10->ciphers;
1087 }
1088
Adam Langleyfcf25832014-12-18 17:42:32 -08001089 if (s->ctx != NULL && s->ctx->cipher_list != NULL) {
1090 return s->ctx->cipher_list->ciphers;
1091 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001092
Adam Langleyfcf25832014-12-18 17:42:32 -08001093 return NULL;
1094}
Adam Langley95c29f32014-06-20 12:00:00 -07001095
Adam Langleyfcf25832014-12-18 17:42:32 -08001096/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001097 * algorithm id */
David Benjamin60da0cd2015-05-03 15:21:28 -04001098STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001099 if (s == NULL) {
1100 return NULL;
1101 }
Adam Langley95c29f32014-06-20 12:00:00 -07001102
Adam Langleyfcf25832014-12-18 17:42:32 -08001103 if (s->cipher_list_by_id != NULL) {
1104 return s->cipher_list_by_id;
1105 }
Adam Langley95c29f32014-06-20 12:00:00 -07001106
Adam Langleyfcf25832014-12-18 17:42:32 -08001107 if (s->ctx != NULL && s->ctx->cipher_list_by_id != NULL) {
1108 return s->ctx->cipher_list_by_id;
1109 }
Adam Langley95c29f32014-06-20 12:00:00 -07001110
Adam Langleyfcf25832014-12-18 17:42:32 -08001111 return NULL;
1112}
Adam Langley95c29f32014-06-20 12:00:00 -07001113
Adam Langleyfcf25832014-12-18 17:42:32 -08001114/* The old interface to get the same thing as SSL_get_ciphers() */
1115const char *SSL_get_cipher_list(const SSL *s, int n) {
1116 const SSL_CIPHER *c;
David Benjamin60da0cd2015-05-03 15:21:28 -04001117 STACK_OF(SSL_CIPHER) *sk;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001118
Adam Langleyfcf25832014-12-18 17:42:32 -08001119 if (s == NULL) {
1120 return NULL;
1121 }
Adam Langley95c29f32014-06-20 12:00:00 -07001122
Adam Langleyfcf25832014-12-18 17:42:32 -08001123 sk = SSL_get_ciphers(s);
1124 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
1125 return NULL;
1126 }
Adam Langley95c29f32014-06-20 12:00:00 -07001127
Adam Langleyfcf25832014-12-18 17:42:32 -08001128 c = sk_SSL_CIPHER_value(sk, n);
1129 if (c == NULL) {
1130 return NULL;
1131 }
Adam Langley95c29f32014-06-20 12:00:00 -07001132
Adam Langleyfcf25832014-12-18 17:42:32 -08001133 return c->name;
1134}
David Benjamin5491e3f2014-09-29 19:33:09 -04001135
Adam Langleyfcf25832014-12-18 17:42:32 -08001136/* specify the ciphers to be used by default by the SSL_CTX */
1137int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
1138 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001139
Adam Langleyfcf25832014-12-18 17:42:32 -08001140 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001141 &ctx->cipher_list_by_id, str);
Adam Langleyfcf25832014-12-18 17:42:32 -08001142 /* ssl_create_cipher_list may return an empty stack if it was unable to find
1143 * a cipher matching the given rule string (for example if the rule string
1144 * specifies a cipher which has been disabled). This is not an error as far
1145 * as ssl_create_cipher_list is concerned, and hence ctx->cipher_list and
1146 * ctx->cipher_list_by_id has been updated. */
1147 if (sk == NULL) {
1148 return 0;
1149 } else if (sk_SSL_CIPHER_num(sk) == 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001150 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
Adam Langleyfcf25832014-12-18 17:42:32 -08001151 return 0;
1152 }
Adam Langley95c29f32014-06-20 12:00:00 -07001153
Adam Langleyfcf25832014-12-18 17:42:32 -08001154 return 1;
1155}
David Benjamin39482a12014-07-20 13:30:15 -04001156
Adam Langleycef75832015-09-03 14:51:12 -07001157int SSL_CTX_set_cipher_list_tls10(SSL_CTX *ctx, const char *str) {
1158 STACK_OF(SSL_CIPHER) *sk;
1159
1160 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls10, NULL, str);
1161 if (sk == NULL) {
1162 return 0;
1163 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1164 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
1165 return 0;
1166 }
1167
1168 return 1;
1169}
1170
Adam Langleyfcf25832014-12-18 17:42:32 -08001171int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str) {
1172 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001173
David Benjamin71f07942015-04-08 02:36:59 -04001174 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str);
Adam Langleyfcf25832014-12-18 17:42:32 -08001175 if (sk == NULL) {
1176 return 0;
1177 } else if (sk_SSL_CIPHER_num(sk) == 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001178 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
Adam Langleyfcf25832014-12-18 17:42:32 -08001179 return 0;
1180 }
David Benjamin9f2c0d72014-10-21 22:00:19 -04001181
Adam Langleyfcf25832014-12-18 17:42:32 -08001182 return 1;
1183}
Adam Langley95c29f32014-06-20 12:00:00 -07001184
Adam Langleyfcf25832014-12-18 17:42:32 -08001185/* specify the ciphers to be used by the SSL */
1186int SSL_set_cipher_list(SSL *s, const char *str) {
1187 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001188
Adam Langleyfcf25832014-12-18 17:42:32 -08001189 sk = ssl_create_cipher_list(s->ctx->method, &s->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001190 &s->cipher_list_by_id, str);
David Benjamin39482a12014-07-20 13:30:15 -04001191
Adam Langleyfcf25832014-12-18 17:42:32 -08001192 /* see comment in SSL_CTX_set_cipher_list */
1193 if (sk == NULL) {
1194 return 0;
1195 } else if (sk_SSL_CIPHER_num(sk) == 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001196 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
Adam Langleyfcf25832014-12-18 17:42:32 -08001197 return 0;
1198 }
David Benjamin39482a12014-07-20 13:30:15 -04001199
Adam Langleyfcf25832014-12-18 17:42:32 -08001200 return 1;
1201}
Adam Langley95c29f32014-06-20 12:00:00 -07001202
Adam Langleyfcf25832014-12-18 17:42:32 -08001203int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, uint8_t *p) {
1204 size_t i;
1205 const SSL_CIPHER *c;
1206 CERT *ct = s->cert;
1207 uint8_t *q;
1208 /* Set disabled masks for this session */
1209 ssl_set_client_disabled(s);
Adam Langley29707792014-06-20 12:00:00 -07001210
Adam Langleyfcf25832014-12-18 17:42:32 -08001211 if (sk == NULL) {
1212 return 0;
1213 }
1214 q = p;
Adam Langley95c29f32014-06-20 12:00:00 -07001215
Adam Langleyfcf25832014-12-18 17:42:32 -08001216 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
1217 c = sk_SSL_CIPHER_value(sk, i);
1218 /* Skip disabled ciphers */
1219 if (c->algorithm_ssl & ct->mask_ssl ||
1220 c->algorithm_mkey & ct->mask_k ||
1221 c->algorithm_auth & ct->mask_a) {
1222 continue;
1223 }
David Benjamina1c90a52015-05-30 17:03:14 -04001224 s2n(ssl_cipher_get_value(c), p);
Adam Langleyfcf25832014-12-18 17:42:32 -08001225 }
1226
1227 /* If all ciphers were disabled, return the error to the caller. */
1228 if (p == q) {
1229 return 0;
1230 }
1231
Adam Langley5021b222015-06-12 18:27:58 -07001232 /* For SSLv3, the SCSV is added. Otherwise the renegotiation extension is
1233 * added. */
1234 if (s->client_version == SSL3_VERSION &&
1235 !s->s3->initial_handshake_complete) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001236 s2n(SSL3_CK_SCSV & 0xffff, p);
Adam Langley5021b222015-06-12 18:27:58 -07001237 /* The renegotiation extension is required to be at index zero. */
1238 s->s3->tmp.extensions.sent |= (1u << 0);
Adam Langleyfcf25832014-12-18 17:42:32 -08001239 }
1240
Adam Langley5f0efe02015-02-20 13:03:16 -08001241 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001242 s2n(SSL3_CK_FALLBACK_SCSV & 0xffff, p);
1243 }
1244
1245 return p - q;
1246}
1247
1248STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs) {
1249 CBS cipher_suites = *cbs;
1250 const SSL_CIPHER *c;
David Benjamin60da0cd2015-05-03 15:21:28 -04001251 STACK_OF(SSL_CIPHER) *sk;
Adam Langleyfcf25832014-12-18 17:42:32 -08001252
1253 if (s->s3) {
1254 s->s3->send_connection_binding = 0;
1255 }
1256
1257 if (CBS_len(&cipher_suites) % 2 != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001258 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
Adam Langleyfcf25832014-12-18 17:42:32 -08001259 return NULL;
1260 }
1261
1262 sk = sk_SSL_CIPHER_new_null();
1263 if (sk == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001264 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001265 goto err;
1266 }
1267
Adam Langleyfcf25832014-12-18 17:42:32 -08001268 while (CBS_len(&cipher_suites) > 0) {
1269 uint16_t cipher_suite;
1270
1271 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
David Benjamin3570d732015-06-29 00:28:17 -04001272 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001273 goto err;
1274 }
1275
1276 /* Check for SCSV. */
1277 if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff)) {
1278 /* SCSV is fatal if renegotiating. */
David Benjamin20f6e972015-05-15 21:51:49 -04001279 if (s->s3->initial_handshake_complete) {
David Benjamin3570d732015-06-29 00:28:17 -04001280 OPENSSL_PUT_ERROR(SSL, SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
Adam Langleyfcf25832014-12-18 17:42:32 -08001281 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1282 goto err;
1283 }
1284 s->s3->send_connection_binding = 1;
1285 continue;
1286 }
1287
1288 /* Check for FALLBACK_SCSV. */
1289 if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff)) {
1290 uint16_t max_version = ssl3_get_max_server_version(s);
1291 if (SSL_IS_DTLS(s) ? (uint16_t)s->version > max_version
1292 : (uint16_t)s->version < max_version) {
David Benjamin3570d732015-06-29 00:28:17 -04001293 OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK);
Adam Langleyfcf25832014-12-18 17:42:32 -08001294 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
1295 goto err;
1296 }
1297 continue;
1298 }
1299
David Benjamina1c90a52015-05-30 17:03:14 -04001300 c = SSL_get_cipher_by_value(cipher_suite);
Adam Langleyfcf25832014-12-18 17:42:32 -08001301 if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
David Benjamin3570d732015-06-29 00:28:17 -04001302 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001303 goto err;
1304 }
1305 }
1306
1307 return sk;
David Benjamin9f2c0d72014-10-21 22:00:19 -04001308
Adam Langley95c29f32014-06-20 12:00:00 -07001309err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001310 sk_SSL_CIPHER_free(sk);
Adam Langleyfcf25832014-12-18 17:42:32 -08001311 return NULL;
1312}
Adam Langley95c29f32014-06-20 12:00:00 -07001313
1314
Adam Langleyfcf25832014-12-18 17:42:32 -08001315/* return a servername extension value if provided in Client Hello, or NULL. So
1316 * far, only host_name types are defined (RFC 3546). */
1317const char *SSL_get_servername(const SSL *s, const int type) {
1318 if (type != TLSEXT_NAMETYPE_host_name) {
1319 return NULL;
1320 }
Adam Langley95c29f32014-06-20 12:00:00 -07001321
Adam Langleyfcf25832014-12-18 17:42:32 -08001322 return s->session && !s->tlsext_hostname ? s->session->tlsext_hostname
1323 : s->tlsext_hostname;
1324}
Adam Langley95c29f32014-06-20 12:00:00 -07001325
Adam Langleyfcf25832014-12-18 17:42:32 -08001326int SSL_get_servername_type(const SSL *s) {
1327 if (s->session &&
1328 (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname)) {
1329 return TLSEXT_NAMETYPE_host_name;
1330 }
Adam Langley95c29f32014-06-20 12:00:00 -07001331
Adam Langleyfcf25832014-12-18 17:42:32 -08001332 return -1;
1333}
Adam Langley95c29f32014-06-20 12:00:00 -07001334
Adam Langleyfcf25832014-12-18 17:42:32 -08001335void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
1336 ctx->signed_cert_timestamps_enabled = 1;
1337}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001338
Adam Langleyfcf25832014-12-18 17:42:32 -08001339int SSL_enable_signed_cert_timestamps(SSL *ssl) {
1340 ssl->signed_cert_timestamps_enabled = 1;
1341 return 1;
1342}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001343
Adam Langleyfcf25832014-12-18 17:42:32 -08001344void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
1345 ctx->ocsp_stapling_enabled = 1;
1346}
David Benjamin6c7aed02014-08-27 16:42:38 -04001347
Adam Langleyfcf25832014-12-18 17:42:32 -08001348int SSL_enable_ocsp_stapling(SSL *ssl) {
1349 ssl->ocsp_stapling_enabled = 1;
1350 return 1;
1351}
David Benjamin6c7aed02014-08-27 16:42:38 -04001352
Adam Langleyfcf25832014-12-18 17:42:32 -08001353void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
1354 size_t *out_len) {
1355 SSL_SESSION *session = ssl->session;
Adam Langley3cb50e02014-08-26 14:00:31 -07001356
Adam Langleyfcf25832014-12-18 17:42:32 -08001357 *out_len = 0;
1358 *out = NULL;
1359 if (ssl->server || !session || !session->tlsext_signed_cert_timestamp_list) {
1360 return;
1361 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001362
Adam Langleyfcf25832014-12-18 17:42:32 -08001363 *out = session->tlsext_signed_cert_timestamp_list;
1364 *out_len = session->tlsext_signed_cert_timestamp_list_length;
1365}
David Benjamin6c7aed02014-08-27 16:42:38 -04001366
Adam Langleyfcf25832014-12-18 17:42:32 -08001367void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
1368 size_t *out_len) {
1369 SSL_SESSION *session = ssl->session;
1370
1371 *out_len = 0;
1372 *out = NULL;
1373 if (ssl->server || !session || !session->ocsp_response) {
1374 return;
1375 }
1376 *out = session->ocsp_response;
1377 *out_len = session->ocsp_response_length;
1378}
David Benjamin6c7aed02014-08-27 16:42:38 -04001379
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001380int SSL_CTX_set_ocsp_response(SSL_CTX *ctx, const uint8_t *response,
1381 size_t response_len) {
1382 OPENSSL_free(ctx->ocsp_response);
1383 ctx->ocsp_response_length = 0;
1384
1385 ctx->ocsp_response = BUF_memdup(response, response_len);
1386 if (ctx->ocsp_response == NULL) {
1387 return 0;
1388 }
1389 ctx->ocsp_response_length = response_len;
1390
1391 return 1;
1392}
1393
Adam Langley95c29f32014-06-20 12:00:00 -07001394/* SSL_select_next_proto implements the standard protocol selection. It is
1395 * expected that this function is called from the callback set by
1396 * SSL_CTX_set_next_proto_select_cb.
1397 *
1398 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1399 * strings. The length byte itself is not included in the length. A byte
1400 * string of length 0 is invalid. No byte string may be truncated.
1401 *
1402 * The current, but experimental algorithm for selecting the protocol is:
1403 *
1404 * 1) If the server doesn't support NPN then this is indicated to the
1405 * callback. In this case, the client application has to abort the connection
1406 * or have a default application level protocol.
1407 *
1408 * 2) If the server supports NPN, but advertises an empty list then the
1409 * client selects the first protcol in its list, but indicates via the
1410 * API that this fallback case was enacted.
1411 *
1412 * 3) Otherwise, the client finds the first protocol in the server's list
1413 * that it supports and selects this protocol. This is because it's
1414 * assumed that the server has better information about which protocol
1415 * a client should use.
1416 *
1417 * 4) If the client doesn't support any of the server's advertised
1418 * protocols, then this is treated the same as case 2.
1419 *
1420 * It returns either
1421 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1422 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1423 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001424int SSL_select_next_proto(uint8_t **out, uint8_t *outlen, const uint8_t *server,
1425 unsigned int server_len, const uint8_t *client,
1426 unsigned int client_len) {
1427 unsigned int i, j;
1428 const uint8_t *result;
1429 int status = OPENSSL_NPN_UNSUPPORTED;
Adam Langley95c29f32014-06-20 12:00:00 -07001430
Adam Langleyfcf25832014-12-18 17:42:32 -08001431 /* For each protocol in server preference order, see if we support it. */
1432 for (i = 0; i < server_len;) {
1433 for (j = 0; j < client_len;) {
1434 if (server[i] == client[j] &&
1435 memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
1436 /* We found a match */
1437 result = &server[i];
1438 status = OPENSSL_NPN_NEGOTIATED;
1439 goto found;
1440 }
1441 j += client[j];
1442 j++;
1443 }
1444 i += server[i];
1445 i++;
1446 }
Adam Langley95c29f32014-06-20 12:00:00 -07001447
Adam Langleyfcf25832014-12-18 17:42:32 -08001448 /* There's no overlap between our protocols and the server's list. */
1449 result = client;
1450 status = OPENSSL_NPN_NO_OVERLAP;
Adam Langley95c29f32014-06-20 12:00:00 -07001451
Adam Langleyfcf25832014-12-18 17:42:32 -08001452found:
1453 *out = (uint8_t *)result + 1;
1454 *outlen = result[0];
1455 return status;
1456}
Adam Langley95c29f32014-06-20 12:00:00 -07001457
Adam Langley95c29f32014-06-20 12:00:00 -07001458/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
1459 * requested protocol for this connection and returns 0. If the client didn't
1460 * request any protocol, then *data is set to NULL.
1461 *
1462 * Note that the client can request any protocol it chooses. The value returned
1463 * from this function need not be a member of the list of supported protocols
Adam Langleyfcf25832014-12-18 17:42:32 -08001464 * provided by the callback. */
1465void SSL_get0_next_proto_negotiated(const SSL *s, const uint8_t **data,
1466 unsigned *len) {
1467 *data = s->next_proto_negotiated;
1468 if (!*data) {
1469 *len = 0;
1470 } else {
1471 *len = s->next_proto_negotiated_len;
1472 }
Adam Langley95c29f32014-06-20 12:00:00 -07001473}
1474
1475/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
1476 * TLS server needs a list of supported protocols for Next Protocol
1477 * Negotiation. The returned list must be in wire format. The list is returned
1478 * by setting |out| to point to it and |outlen| to its length. This memory will
1479 * not be modified, but one should assume that the SSL* keeps a reference to
1480 * it.
1481 *
Adam Langleyfcf25832014-12-18 17:42:32 -08001482 * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise.
1483 * Otherwise, no such extension will be included in the ServerHello. */
1484void SSL_CTX_set_next_protos_advertised_cb(
1485 SSL_CTX *ctx,
1486 int (*cb)(SSL *ssl, const uint8_t **out, unsigned int *outlen, void *arg),
1487 void *arg) {
1488 ctx->next_protos_advertised_cb = cb;
1489 ctx->next_protos_advertised_cb_arg = arg;
1490}
Adam Langley95c29f32014-06-20 12:00:00 -07001491
1492/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1493 * client needs to select a protocol from the server's provided list. |out|
1494 * must be set to point to the selected protocol (which may be within |in|).
1495 * The length of the protocol name must be written into |outlen|. The server's
1496 * advertised protocols are provided in |in| and |inlen|. The callback can
1497 * assume that |in| is syntactically valid.
1498 *
1499 * The client must select a protocol. It is fatal to the connection if this
1500 * callback returns a value other than SSL_TLSEXT_ERR_OK.
1501 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001502void SSL_CTX_set_next_proto_select_cb(
1503 SSL_CTX *ctx, int (*cb)(SSL *s, uint8_t **out, uint8_t *outlen,
1504 const uint8_t *in, unsigned int inlen, void *arg),
1505 void *arg) {
1506 ctx->next_proto_select_cb = cb;
1507 ctx->next_proto_select_cb_arg = arg;
1508}
Adam Langley95c29f32014-06-20 12:00:00 -07001509
Adam Langleyfcf25832014-12-18 17:42:32 -08001510int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
1511 unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001512 OPENSSL_free(ctx->alpn_client_proto_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001513 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1514 if (!ctx->alpn_client_proto_list) {
1515 return 1;
1516 }
1517 ctx->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001518
Adam Langleyfcf25832014-12-18 17:42:32 -08001519 return 0;
1520}
Adam Langley95c29f32014-06-20 12:00:00 -07001521
Adam Langleyfcf25832014-12-18 17:42:32 -08001522int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001523 OPENSSL_free(ssl->alpn_client_proto_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001524 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1525 if (!ssl->alpn_client_proto_list) {
1526 return 1;
1527 }
1528 ssl->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001529
Adam Langleyfcf25832014-12-18 17:42:32 -08001530 return 0;
1531}
Adam Langley95c29f32014-06-20 12:00:00 -07001532
1533/* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
1534 * during ClientHello processing in order to select an ALPN protocol from the
1535 * client's list of offered protocols. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001536void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
1537 int (*cb)(SSL *ssl, const uint8_t **out,
1538 uint8_t *outlen, const uint8_t *in,
1539 unsigned int inlen, void *arg),
1540 void *arg) {
1541 ctx->alpn_select_cb = cb;
1542 ctx->alpn_select_cb_arg = arg;
1543}
Adam Langley95c29f32014-06-20 12:00:00 -07001544
1545/* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
1546 * On return it sets |*data| to point to |*len| bytes of protocol name (not
1547 * including the leading length-prefix byte). If the server didn't respond with
1548 * a negotiated protocol then |*len| will be zero. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001549void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **data,
1550 unsigned *len) {
1551 *data = NULL;
1552 if (ssl->s3) {
1553 *data = ssl->s3->alpn_selected;
1554 }
1555 if (*data == NULL) {
1556 *len = 0;
1557 } else {
1558 *len = ssl->s3->alpn_selected_len;
1559 }
1560}
Adam Langley95c29f32014-06-20 12:00:00 -07001561
David Benjamincfd248b2015-04-03 11:02:24 -04001562int SSL_export_keying_material(SSL *s, uint8_t *out, size_t out_len,
1563 const char *label, size_t label_len,
1564 const uint8_t *context, size_t context_len,
1565 int use_context) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001566 if (s->version < TLS1_VERSION) {
David Benjaminc565ebb2015-04-03 04:06:36 -04001567 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001568 }
Adam Langley95c29f32014-06-20 12:00:00 -07001569
David Benjamincfd248b2015-04-03 11:02:24 -04001570 return s->enc_method->export_keying_material(
1571 s, out, out_len, label, label_len, context, context_len, use_context);
Adam Langleyfcf25832014-12-18 17:42:32 -08001572}
Adam Langley95c29f32014-06-20 12:00:00 -07001573
Adam Langleyfcf25832014-12-18 17:42:32 -08001574static uint32_t ssl_session_hash(const SSL_SESSION *a) {
1575 uint32_t hash =
1576 ((uint32_t)a->session_id[0]) ||
1577 ((uint32_t)a->session_id[1] << 8) ||
1578 ((uint32_t)a->session_id[2] << 16) ||
1579 ((uint32_t)a->session_id[3] << 24);
Adam Langley95c29f32014-06-20 12:00:00 -07001580
Adam Langleyfcf25832014-12-18 17:42:32 -08001581 return hash;
1582}
Adam Langley95c29f32014-06-20 12:00:00 -07001583
1584/* NB: If this function (or indeed the hash function which uses a sort of
1585 * coarser function than this one) is changed, ensure
1586 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1587 * able to construct an SSL_SESSION that will collide with any existing session
1588 * with a matching session ID. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001589static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
1590 if (a->ssl_version != b->ssl_version) {
1591 return 1;
1592 }
Adam Langley95c29f32014-06-20 12:00:00 -07001593
Adam Langleyfcf25832014-12-18 17:42:32 -08001594 if (a->session_id_length != b->session_id_length) {
1595 return 1;
1596 }
Adam Langley95c29f32014-06-20 12:00:00 -07001597
Adam Langleyfcf25832014-12-18 17:42:32 -08001598 return memcmp(a->session_id, b->session_id, a->session_id_length);
1599}
Adam Langley95c29f32014-06-20 12:00:00 -07001600
David Benjamin4831c332015-05-16 11:43:13 -04001601SSL_CTX *SSL_CTX_new(const SSL_METHOD *method) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001602 SSL_CTX *ret = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001603
David Benjamin4831c332015-05-16 11:43:13 -04001604 if (method == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001605 OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_METHOD_PASSED);
Adam Langleyfcf25832014-12-18 17:42:32 -08001606 return NULL;
1607 }
Adam Langley95c29f32014-06-20 12:00:00 -07001608
Adam Langleyfcf25832014-12-18 17:42:32 -08001609 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001610 OPENSSL_PUT_ERROR(SSL, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
Adam Langleyfcf25832014-12-18 17:42:32 -08001611 goto err;
1612 }
Adam Langley95c29f32014-06-20 12:00:00 -07001613
Adam Langleyfcf25832014-12-18 17:42:32 -08001614 ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1615 if (ret == NULL) {
1616 goto err;
1617 }
Adam Langley95c29f32014-06-20 12:00:00 -07001618
Adam Langleyfcf25832014-12-18 17:42:32 -08001619 memset(ret, 0, sizeof(SSL_CTX));
Adam Langley95c29f32014-06-20 12:00:00 -07001620
David Benjamin4831c332015-05-16 11:43:13 -04001621 ret->method = method->method;
Adam Langley95c29f32014-06-20 12:00:00 -07001622
Adam Langley4bdb6e42015-05-15 15:29:21 -07001623 CRYPTO_MUTEX_init(&ret->lock);
1624
Adam Langleyfcf25832014-12-18 17:42:32 -08001625 ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
1626 ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
Adam Langley95c29f32014-06-20 12:00:00 -07001627
Adam Langleyfcf25832014-12-18 17:42:32 -08001628 /* We take the system default */
1629 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001630
Adam Langleyfcf25832014-12-18 17:42:32 -08001631 ret->references = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001632
Adam Langleyfcf25832014-12-18 17:42:32 -08001633 ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
Adam Langleyfcf25832014-12-18 17:42:32 -08001634 ret->verify_mode = SSL_VERIFY_NONE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001635 ret->cert = ssl_cert_new();
1636 if (ret->cert == NULL) {
1637 goto err;
1638 }
Adam Langley95c29f32014-06-20 12:00:00 -07001639
Adam Langleyfcf25832014-12-18 17:42:32 -08001640 ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
1641 if (ret->sessions == NULL) {
1642 goto err;
1643 }
1644 ret->cert_store = X509_STORE_new();
1645 if (ret->cert_store == NULL) {
1646 goto err;
1647 }
Adam Langley95c29f32014-06-20 12:00:00 -07001648
Adam Langleyfcf25832014-12-18 17:42:32 -08001649 ssl_create_cipher_list(ret->method, &ret->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001650 &ret->cipher_list_by_id, SSL_DEFAULT_CIPHER_LIST);
Adam Langleyfcf25832014-12-18 17:42:32 -08001651 if (ret->cipher_list == NULL ||
1652 sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001653 OPENSSL_PUT_ERROR(SSL, SSL_R_LIBRARY_HAS_NO_CIPHERS);
Adam Langleyfcf25832014-12-18 17:42:32 -08001654 goto err2;
1655 }
Adam Langley95c29f32014-06-20 12:00:00 -07001656
Adam Langleyfcf25832014-12-18 17:42:32 -08001657 ret->param = X509_VERIFY_PARAM_new();
1658 if (!ret->param) {
1659 goto err;
1660 }
Adam Langley95c29f32014-06-20 12:00:00 -07001661
Adam Langleyfcf25832014-12-18 17:42:32 -08001662 ret->client_CA = sk_X509_NAME_new_null();
1663 if (ret->client_CA == NULL) {
1664 goto err;
1665 }
Adam Langley95c29f32014-06-20 12:00:00 -07001666
David Benjamin9f33fc62015-04-15 17:29:53 -04001667 CRYPTO_new_ex_data(&g_ex_data_class_ssl_ctx, ret, &ret->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001668
Adam Langleyfcf25832014-12-18 17:42:32 -08001669 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
David Benjamin422d3a42014-08-20 11:09:03 -04001670
Adam Langleyfcf25832014-12-18 17:42:32 -08001671 /* Setup RFC4507 ticket keys */
1672 if (!RAND_bytes(ret->tlsext_tick_key_name, 16) ||
1673 !RAND_bytes(ret->tlsext_tick_hmac_key, 16) ||
1674 !RAND_bytes(ret->tlsext_tick_aes_key, 16)) {
1675 ret->options |= SSL_OP_NO_TICKET;
1676 }
Adam Langley95c29f32014-06-20 12:00:00 -07001677
Adam Langleyfcf25832014-12-18 17:42:32 -08001678 /* Default is to connect to non-RI servers. When RI is more widely deployed
1679 * might change this. */
1680 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
1681
1682 /* Lock the SSL_CTX to the specified version, for compatibility with legacy
1683 * uses of SSL_METHOD. */
David Benjamin4831c332015-05-16 11:43:13 -04001684 if (method->version != 0) {
1685 SSL_CTX_set_max_version(ret, method->version);
1686 SSL_CTX_set_min_version(ret, method->version);
Adam Langleyfcf25832014-12-18 17:42:32 -08001687 }
1688
1689 return ret;
1690
Adam Langley95c29f32014-06-20 12:00:00 -07001691err:
David Benjamin3570d732015-06-29 00:28:17 -04001692 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -07001693err2:
David Benjamin2755a3e2015-04-22 16:17:58 -04001694 SSL_CTX_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -08001695 return NULL;
1696}
Adam Langley95c29f32014-06-20 12:00:00 -07001697
David Benjamin4fcc2e22015-04-22 12:58:16 -04001698void SSL_CTX_free(SSL_CTX *ctx) {
1699 if (ctx == NULL ||
Adam Langley0b5e3902015-05-15 13:08:38 -07001700 !CRYPTO_refcount_dec_and_test_zero(&ctx->references)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001701 return;
1702 }
Adam Langley95c29f32014-06-20 12:00:00 -07001703
David Benjamin2755a3e2015-04-22 16:17:58 -04001704 X509_VERIFY_PARAM_free(ctx->param);
Adam Langley95c29f32014-06-20 12:00:00 -07001705
Adam Langleyfcf25832014-12-18 17:42:32 -08001706 /* Free internal session cache. However: the remove_cb() may reference the
1707 * ex_data of SSL_CTX, thus the ex_data store can only be removed after the
1708 * sessions were flushed. As the ex_data handling routines might also touch
1709 * the session cache, the most secure solution seems to be: empty (flush) the
1710 * cache, then free ex_data, then finally free the cache. (See ticket
1711 * [openssl.org #212].) */
David Benjamin2755a3e2015-04-22 16:17:58 -04001712 SSL_CTX_flush_sessions(ctx, 0);
Adam Langley95c29f32014-06-20 12:00:00 -07001713
David Benjamin4fcc2e22015-04-22 12:58:16 -04001714 CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, ctx, &ctx->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001715
Adam Langley4bdb6e42015-05-15 15:29:21 -07001716 CRYPTO_MUTEX_cleanup(&ctx->lock);
David Benjamin2755a3e2015-04-22 16:17:58 -04001717 lh_SSL_SESSION_free(ctx->sessions);
1718 X509_STORE_free(ctx->cert_store);
1719 ssl_cipher_preference_list_free(ctx->cipher_list);
1720 sk_SSL_CIPHER_free(ctx->cipher_list_by_id);
1721 ssl_cipher_preference_list_free(ctx->cipher_list_tls11);
1722 ssl_cert_free(ctx->cert);
Adam Langley09505632015-07-30 18:10:13 -07001723 sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->client_custom_extensions,
1724 SSL_CUSTOM_EXTENSION_free);
1725 sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->server_custom_extensions,
1726 SSL_CUSTOM_EXTENSION_free);
David Benjamin2755a3e2015-04-22 16:17:58 -04001727 sk_X509_NAME_pop_free(ctx->client_CA, X509_NAME_free);
David Benjamin2755a3e2015-04-22 16:17:58 -04001728 sk_SRTP_PROTECTION_PROFILE_free(ctx->srtp_profiles);
1729 OPENSSL_free(ctx->psk_identity_hint);
David Benjamin2755a3e2015-04-22 16:17:58 -04001730 OPENSSL_free(ctx->tlsext_ellipticcurvelist);
1731 OPENSSL_free(ctx->alpn_client_proto_list);
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001732 OPENSSL_free(ctx->ocsp_response);
David Benjamin2755a3e2015-04-22 16:17:58 -04001733 EVP_PKEY_free(ctx->tlsext_channel_id_private);
1734 BIO_free(ctx->keylog_bio);
Adam Langley95c29f32014-06-20 12:00:00 -07001735
David Benjamin4fcc2e22015-04-22 12:58:16 -04001736 OPENSSL_free(ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08001737}
Adam Langley95c29f32014-06-20 12:00:00 -07001738
Adam Langleyfcf25832014-12-18 17:42:32 -08001739void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) {
1740 ctx->default_passwd_callback = cb;
1741}
Adam Langley95c29f32014-06-20 12:00:00 -07001742
Adam Langleyfcf25832014-12-18 17:42:32 -08001743void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u) {
1744 ctx->default_passwd_callback_userdata = u;
1745}
Adam Langley95c29f32014-06-20 12:00:00 -07001746
Adam Langleyfcf25832014-12-18 17:42:32 -08001747void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
1748 int (*cb)(X509_STORE_CTX *, void *),
1749 void *arg) {
1750 ctx->app_verify_callback = cb;
1751 ctx->app_verify_arg = arg;
1752}
Adam Langley95c29f32014-06-20 12:00:00 -07001753
Adam Langleyfcf25832014-12-18 17:42:32 -08001754void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
1755 int (*cb)(int, X509_STORE_CTX *)) {
1756 ctx->verify_mode = mode;
1757 ctx->default_verify_callback = cb;
1758}
Adam Langley95c29f32014-06-20 12:00:00 -07001759
Adam Langleyfcf25832014-12-18 17:42:32 -08001760void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
1761 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
1762}
Adam Langley1258b6a2014-06-20 12:00:00 -07001763
David Benjamin7481d392015-07-05 19:38:46 -04001764void SSL_CTX_set_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl, void *arg),
Adam Langleyfcf25832014-12-18 17:42:32 -08001765 void *arg) {
David Benjamin7481d392015-07-05 19:38:46 -04001766 ssl_cert_set_cert_cb(ctx->cert, cb, arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08001767}
David Benjamin859ec3c2014-09-02 16:29:36 -04001768
David Benjamin7481d392015-07-05 19:38:46 -04001769void SSL_set_cert_cb(SSL *ssl, int (*cb)(SSL *ssl, void *arg), void *arg) {
1770 ssl_cert_set_cert_cb(ssl->cert, cb, arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08001771}
Adam Langley95c29f32014-06-20 12:00:00 -07001772
David Benjamin107db582015-04-08 00:41:59 -04001773void ssl_get_compatible_server_ciphers(SSL *s, uint32_t *out_mask_k,
1774 uint32_t *out_mask_a) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001775 CERT *c = s->cert;
David Benjamind1d80782015-07-05 11:54:09 -04001776 int have_rsa_cert = 0, dh_tmp;
David Benjamin107db582015-04-08 00:41:59 -04001777 uint32_t mask_k, mask_a;
David Benjamind1d80782015-07-05 11:54:09 -04001778 int have_ecc_cert = 0, ecdsa_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001779 X509 *x;
Adam Langley95c29f32014-06-20 12:00:00 -07001780
Adam Langleyfcf25832014-12-18 17:42:32 -08001781 dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001782
nagendra modadugu601448a2015-07-24 09:31:31 -07001783 if (s->cert->x509 != NULL && ssl_has_private_key(s)) {
1784 if (ssl_private_key_type(s) == EVP_PKEY_RSA) {
David Benjamind1d80782015-07-05 11:54:09 -04001785 have_rsa_cert = 1;
nagendra modadugu601448a2015-07-24 09:31:31 -07001786 } else if (ssl_private_key_type(s) == EVP_PKEY_EC) {
David Benjamind1d80782015-07-05 11:54:09 -04001787 have_ecc_cert = 1;
1788 }
1789 }
nagendra modadugu601448a2015-07-24 09:31:31 -07001790
Adam Langleyfcf25832014-12-18 17:42:32 -08001791 mask_k = 0;
1792 mask_a = 0;
David Benjaminf31e6812014-11-13 18:05:55 -05001793
Adam Langleyfcf25832014-12-18 17:42:32 -08001794 if (dh_tmp) {
David Benjamin7061e282015-03-19 11:10:48 -04001795 mask_k |= SSL_kDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001796 }
David Benjaminbb20f522015-07-04 17:18:14 -04001797 if (have_rsa_cert) {
1798 mask_k |= SSL_kRSA;
Adam Langleyfcf25832014-12-18 17:42:32 -08001799 mask_a |= SSL_aRSA;
1800 }
Adam Langley95c29f32014-06-20 12:00:00 -07001801
Adam Langleyfcf25832014-12-18 17:42:32 -08001802 /* An ECC certificate may be usable for ECDSA cipher suites depending on the
1803 * key usage extension and on the client's curve preferences. */
1804 if (have_ecc_cert) {
David Benjamind1d80782015-07-05 11:54:09 -04001805 x = c->x509;
Adam Langleyfcf25832014-12-18 17:42:32 -08001806 /* This call populates extension flags (ex_flags). */
1807 X509_check_purpose(x, -1, 0);
1808 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE)
1809 ? (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)
1810 : 1;
1811 if (!tls1_check_ec_cert(s, x)) {
1812 ecdsa_ok = 0;
1813 }
1814 if (ecdsa_ok) {
1815 mask_a |= SSL_aECDSA;
1816 }
1817 }
Adam Langley95c29f32014-06-20 12:00:00 -07001818
Adam Langleyfcf25832014-12-18 17:42:32 -08001819 /* If we are considering an ECC cipher suite that uses an ephemeral EC
1820 * key, check it. */
David Benjamindd978782015-04-24 15:20:13 -04001821 if (tls1_check_ec_tmp_key(s)) {
David Benjamin7061e282015-03-19 11:10:48 -04001822 mask_k |= SSL_kECDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001823 }
Adam Langley95c29f32014-06-20 12:00:00 -07001824
Adam Langleyfcf25832014-12-18 17:42:32 -08001825 /* PSK requires a server callback. */
1826 if (s->psk_server_callback != NULL) {
1827 mask_k |= SSL_kPSK;
1828 mask_a |= SSL_aPSK;
1829 }
Adam Langley95c29f32014-06-20 12:00:00 -07001830
Adam Langleyfcf25832014-12-18 17:42:32 -08001831 *out_mask_k = mask_k;
1832 *out_mask_a = mask_a;
1833}
Adam Langley95c29f32014-06-20 12:00:00 -07001834
Adam Langleyfcf25832014-12-18 17:42:32 -08001835void ssl_update_cache(SSL *s, int mode) {
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001836 /* Never cache sessions with empty session IDs. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001837 if (s->session->session_id_length == 0) {
1838 return;
1839 }
Adam Langley95c29f32014-06-20 12:00:00 -07001840
David Benjamin95d31822015-06-15 19:53:32 -04001841 int has_new_session = !s->hit;
1842 if (!s->server && s->tlsext_ticket_expected) {
1843 /* A client may see new sessions on abbreviated handshakes if the server
1844 * decides to renew the ticket. Once the handshake is completed, it should
1845 * be inserted into the cache. */
1846 has_new_session = 1;
1847 }
1848
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001849 SSL_CTX *ctx = s->initial_ctx;
David Benjamin95d31822015-06-15 19:53:32 -04001850 if ((ctx->session_cache_mode & mode) == mode && has_new_session &&
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001851 ((ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) ||
1852 SSL_CTX_add_session(ctx, s->session)) &&
1853 ctx->new_session_cb != NULL) {
1854 /* Note: |new_session_cb| is called whether the internal session cache is
1855 * used or not. */
1856 if (!ctx->new_session_cb(s, SSL_SESSION_up_ref(s->session))) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001857 SSL_SESSION_free(s->session);
1858 }
1859 }
Adam Langley95c29f32014-06-20 12:00:00 -07001860
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001861 if (!(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR) &&
1862 !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) &&
1863 (ctx->session_cache_mode & mode) == mode) {
1864 /* Automatically flush the internal session cache every 255 connections. */
1865 int flush_cache = 0;
Adam Langley4bdb6e42015-05-15 15:29:21 -07001866 CRYPTO_MUTEX_lock_write(&ctx->lock);
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001867 ctx->handshakes_since_cache_flush++;
1868 if (ctx->handshakes_since_cache_flush >= 255) {
1869 flush_cache = 1;
1870 ctx->handshakes_since_cache_flush = 0;
1871 }
Adam Langley4bdb6e42015-05-15 15:29:21 -07001872 CRYPTO_MUTEX_unlock(&ctx->lock);
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001873
1874 if (flush_cache) {
1875 SSL_CTX_flush_sessions(ctx, (unsigned long)time(NULL));
Adam Langleyfcf25832014-12-18 17:42:32 -08001876 }
1877 }
1878}
Adam Langley95c29f32014-06-20 12:00:00 -07001879
David Benjamin1a5c50f2015-03-11 16:22:37 -04001880int SSL_get_error(const SSL *s, int ret_code) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001881 int reason;
David Benjamin1a5c50f2015-03-11 16:22:37 -04001882 uint32_t err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001883 BIO *bio;
Adam Langley95c29f32014-06-20 12:00:00 -07001884
David Benjamin1a5c50f2015-03-11 16:22:37 -04001885 if (ret_code > 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001886 return SSL_ERROR_NONE;
1887 }
Adam Langley95c29f32014-06-20 12:00:00 -07001888
Adam Langleyfcf25832014-12-18 17:42:32 -08001889 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
1890 * where we do encode the error */
David Benjamin1a5c50f2015-03-11 16:22:37 -04001891 err = ERR_peek_error();
1892 if (err != 0) {
1893 if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001894 return SSL_ERROR_SYSCALL;
1895 }
1896 return SSL_ERROR_SSL;
1897 }
Adam Langley95c29f32014-06-20 12:00:00 -07001898
David Benjamin1a5c50f2015-03-11 16:22:37 -04001899 if (ret_code == 0) {
David Benjamin9a38e922015-01-22 16:06:11 -05001900 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
1901 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) {
1902 /* The socket was cleanly shut down with a close_notify. */
1903 return SSL_ERROR_ZERO_RETURN;
1904 }
1905 /* An EOF was observed which violates the protocol, and the underlying
1906 * transport does not participate in the error queue. Bubble up to the
1907 * caller. */
1908 return SSL_ERROR_SYSCALL;
Adam Langleyfcf25832014-12-18 17:42:32 -08001909 }
Adam Langleyb2ce0582014-06-20 12:00:00 -07001910
Adam Langleyfcf25832014-12-18 17:42:32 -08001911 if (SSL_want_session(s)) {
1912 return SSL_ERROR_PENDING_SESSION;
1913 }
Adam Langley95c29f32014-06-20 12:00:00 -07001914
Adam Langleyfcf25832014-12-18 17:42:32 -08001915 if (SSL_want_certificate(s)) {
1916 return SSL_ERROR_PENDING_CERTIFICATE;
1917 }
Adam Langley95c29f32014-06-20 12:00:00 -07001918
Adam Langleyfcf25832014-12-18 17:42:32 -08001919 if (SSL_want_read(s)) {
1920 bio = SSL_get_rbio(s);
1921 if (BIO_should_read(bio)) {
1922 return SSL_ERROR_WANT_READ;
1923 }
Adam Langley95c29f32014-06-20 12:00:00 -07001924
Adam Langleyfcf25832014-12-18 17:42:32 -08001925 if (BIO_should_write(bio)) {
1926 /* This one doesn't make too much sense ... We never try to write to the
1927 * rbio, and an application program where rbio and wbio are separate
1928 * couldn't even know what it should wait for. However if we ever set
1929 * s->rwstate incorrectly (so that we have SSL_want_read(s) instead of
1930 * SSL_want_write(s)) and rbio and wbio *are* the same, this test works
1931 * around that bug; so it might be safer to keep it. */
1932 return SSL_ERROR_WANT_WRITE;
1933 }
Adam Langley95c29f32014-06-20 12:00:00 -07001934
Adam Langleyfcf25832014-12-18 17:42:32 -08001935 if (BIO_should_io_special(bio)) {
1936 reason = BIO_get_retry_reason(bio);
1937 if (reason == BIO_RR_CONNECT) {
1938 return SSL_ERROR_WANT_CONNECT;
1939 }
Adam Langley95c29f32014-06-20 12:00:00 -07001940
Adam Langleyfcf25832014-12-18 17:42:32 -08001941 if (reason == BIO_RR_ACCEPT) {
1942 return SSL_ERROR_WANT_ACCEPT;
1943 }
Adam Langley95c29f32014-06-20 12:00:00 -07001944
Adam Langleyfcf25832014-12-18 17:42:32 -08001945 return SSL_ERROR_SYSCALL; /* unknown */
1946 }
1947 }
Adam Langley95c29f32014-06-20 12:00:00 -07001948
Adam Langleyfcf25832014-12-18 17:42:32 -08001949 if (SSL_want_write(s)) {
1950 bio = SSL_get_wbio(s);
1951 if (BIO_should_write(bio)) {
1952 return SSL_ERROR_WANT_WRITE;
1953 }
Adam Langley95c29f32014-06-20 12:00:00 -07001954
Adam Langleyfcf25832014-12-18 17:42:32 -08001955 if (BIO_should_read(bio)) {
1956 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
1957 return SSL_ERROR_WANT_READ;
1958 }
Adam Langley95c29f32014-06-20 12:00:00 -07001959
Adam Langleyfcf25832014-12-18 17:42:32 -08001960 if (BIO_should_io_special(bio)) {
1961 reason = BIO_get_retry_reason(bio);
1962 if (reason == BIO_RR_CONNECT) {
1963 return SSL_ERROR_WANT_CONNECT;
1964 }
Adam Langley95c29f32014-06-20 12:00:00 -07001965
Adam Langleyfcf25832014-12-18 17:42:32 -08001966 if (reason == BIO_RR_ACCEPT) {
1967 return SSL_ERROR_WANT_ACCEPT;
1968 }
Adam Langley95c29f32014-06-20 12:00:00 -07001969
Adam Langleyfcf25832014-12-18 17:42:32 -08001970 return SSL_ERROR_SYSCALL;
1971 }
1972 }
Adam Langley95c29f32014-06-20 12:00:00 -07001973
Adam Langleyfcf25832014-12-18 17:42:32 -08001974 if (SSL_want_x509_lookup(s)) {
1975 return SSL_ERROR_WANT_X509_LOOKUP;
1976 }
Adam Langley95c29f32014-06-20 12:00:00 -07001977
Adam Langleyfcf25832014-12-18 17:42:32 -08001978 if (SSL_want_channel_id_lookup(s)) {
1979 return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
1980 }
Adam Langley0f4746e2014-08-13 12:26:32 -07001981
David Benjaminb4d65fd2015-05-29 17:11:21 -04001982 if (SSL_want_private_key_operation(s)) {
1983 return SSL_ERROR_WANT_PRIVATE_KEY_OPERATION;
1984 }
1985
Adam Langleyfcf25832014-12-18 17:42:32 -08001986 return SSL_ERROR_SYSCALL;
1987}
Adam Langley0f4746e2014-08-13 12:26:32 -07001988
Adam Langleyfcf25832014-12-18 17:42:32 -08001989int SSL_do_handshake(SSL *s) {
1990 int ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001991
Adam Langleyfcf25832014-12-18 17:42:32 -08001992 if (s->handshake_func == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001993 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_TYPE_NOT_SET);
Adam Langleyfcf25832014-12-18 17:42:32 -08001994 return -1;
1995 }
Adam Langley95c29f32014-06-20 12:00:00 -07001996
Adam Langleyfcf25832014-12-18 17:42:32 -08001997 if (SSL_in_init(s)) {
1998 ret = s->handshake_func(s);
1999 }
2000 return ret;
2001}
Adam Langley95c29f32014-06-20 12:00:00 -07002002
David Benjamin4831c332015-05-16 11:43:13 -04002003void SSL_set_accept_state(SSL *ssl) {
2004 ssl->server = 1;
2005 ssl->shutdown = 0;
David Benjamin8ec88102015-05-15 23:40:13 -04002006 ssl->state = SSL_ST_ACCEPT;
David Benjamin4831c332015-05-16 11:43:13 -04002007 ssl->handshake_func = ssl->method->ssl_accept;
Adam Langleyfcf25832014-12-18 17:42:32 -08002008 /* clear the current cipher */
David Benjamin4831c332015-05-16 11:43:13 -04002009 ssl_clear_cipher_ctx(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08002010}
Adam Langley95c29f32014-06-20 12:00:00 -07002011
David Benjamin4831c332015-05-16 11:43:13 -04002012void SSL_set_connect_state(SSL *ssl) {
2013 ssl->server = 0;
2014 ssl->shutdown = 0;
David Benjamin8ec88102015-05-15 23:40:13 -04002015 ssl->state = SSL_ST_CONNECT;
David Benjamin4831c332015-05-16 11:43:13 -04002016 ssl->handshake_func = ssl->method->ssl_connect;
Adam Langleyfcf25832014-12-18 17:42:32 -08002017 /* clear the current cipher */
David Benjamin4831c332015-05-16 11:43:13 -04002018 ssl_clear_cipher_ctx(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08002019}
Adam Langley95c29f32014-06-20 12:00:00 -07002020
Adam Langleyfcf25832014-12-18 17:42:32 -08002021static const char *ssl_get_version(int version) {
2022 switch (version) {
2023 case TLS1_2_VERSION:
2024 return "TLSv1.2";
Adam Langley95c29f32014-06-20 12:00:00 -07002025
Adam Langleyfcf25832014-12-18 17:42:32 -08002026 case TLS1_1_VERSION:
2027 return "TLSv1.1";
Adam Langley95c29f32014-06-20 12:00:00 -07002028
Adam Langleyfcf25832014-12-18 17:42:32 -08002029 case TLS1_VERSION:
2030 return "TLSv1";
Adam Langley95c29f32014-06-20 12:00:00 -07002031
Adam Langleyfcf25832014-12-18 17:42:32 -08002032 case SSL3_VERSION:
2033 return "SSLv3";
Adam Langley95c29f32014-06-20 12:00:00 -07002034
David Benjamin1c722b72015-04-20 13:53:10 -04002035 case DTLS1_VERSION:
2036 return "DTLSv1";
2037
2038 case DTLS1_2_VERSION:
2039 return "DTLSv1.2";
2040
Adam Langleyfcf25832014-12-18 17:42:32 -08002041 default:
2042 return "unknown";
2043 }
2044}
Adam Langley95c29f32014-06-20 12:00:00 -07002045
Adam Langleyfcf25832014-12-18 17:42:32 -08002046const char *SSL_get_version(const SSL *s) {
2047 return ssl_get_version(s->version);
2048}
Adam Langley95c29f32014-06-20 12:00:00 -07002049
Adam Langleyfcf25832014-12-18 17:42:32 -08002050const char *SSL_SESSION_get_version(const SSL_SESSION *sess) {
2051 return ssl_get_version(sess->ssl_version);
2052}
Adam Langley95c29f32014-06-20 12:00:00 -07002053
Sigbjorn Vik2b23d242015-06-29 15:07:26 +02002054const char* SSL_get_curve_name(uint16_t curve_id) {
2055 return tls1_ec_curve_id2name(curve_id);
2056}
2057
Adam Langleyfcf25832014-12-18 17:42:32 -08002058void ssl_clear_cipher_ctx(SSL *s) {
David Benjamin31a07792015-03-03 14:20:26 -05002059 SSL_AEAD_CTX_free(s->aead_read_ctx);
2060 s->aead_read_ctx = NULL;
2061 SSL_AEAD_CTX_free(s->aead_write_ctx);
2062 s->aead_write_ctx = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002063}
Adam Langley95c29f32014-06-20 12:00:00 -07002064
Adam Langleyfcf25832014-12-18 17:42:32 -08002065X509 *SSL_get_certificate(const SSL *s) {
2066 if (s->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04002067 return s->cert->x509;
Adam Langleyfcf25832014-12-18 17:42:32 -08002068 }
2069
2070 return NULL;
2071}
2072
2073EVP_PKEY *SSL_get_privatekey(const SSL *s) {
2074 if (s->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04002075 return s->cert->privatekey;
Adam Langleyfcf25832014-12-18 17:42:32 -08002076 }
2077
2078 return NULL;
2079}
2080
2081X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
2082 if (ctx->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04002083 return ctx->cert->x509;
Adam Langleyfcf25832014-12-18 17:42:32 -08002084 }
2085
2086 return NULL;
2087}
2088
2089EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
2090 if (ctx->cert != NULL) {
David Benjamind1d80782015-07-05 11:54:09 -04002091 return ctx->cert->privatekey;
Adam Langleyfcf25832014-12-18 17:42:32 -08002092 }
2093
2094 return NULL;
2095}
2096
2097const SSL_CIPHER *SSL_get_current_cipher(const SSL *s) {
David Benjamina07c0fc2015-05-13 13:19:42 -04002098 if (s->aead_write_ctx == NULL) {
2099 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002100 }
David Benjamina07c0fc2015-05-13 13:19:42 -04002101 return s->aead_write_ctx->cipher;
Adam Langleyfcf25832014-12-18 17:42:32 -08002102}
2103
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07002104const COMP_METHOD *SSL_get_current_compression(SSL *s) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002105
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07002106const COMP_METHOD *SSL_get_current_expansion(SSL *s) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08002107
2108int ssl_init_wbio_buffer(SSL *s, int push) {
2109 BIO *bbio;
2110
2111 if (s->bbio == NULL) {
2112 bbio = BIO_new(BIO_f_buffer());
2113 if (bbio == NULL) {
2114 return 0;
2115 }
2116 s->bbio = bbio;
2117 } else {
2118 bbio = s->bbio;
2119 if (s->bbio == s->wbio) {
2120 s->wbio = BIO_pop(s->wbio);
2121 }
2122 }
2123
2124 BIO_reset(bbio);
2125 if (!BIO_set_read_buffer_size(bbio, 1)) {
David Benjamin3570d732015-06-29 00:28:17 -04002126 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -08002127 return 0;
2128 }
2129
2130 if (push) {
2131 if (s->wbio != bbio) {
2132 s->wbio = BIO_push(bbio, s->wbio);
2133 }
2134 } else {
2135 if (s->wbio == bbio) {
2136 s->wbio = BIO_pop(bbio);
2137 }
2138 }
2139
2140 return 1;
2141}
2142
2143void ssl_free_wbio_buffer(SSL *s) {
2144 if (s->bbio == NULL) {
2145 return;
2146 }
2147
2148 if (s->bbio == s->wbio) {
2149 /* remove buffering */
2150 s->wbio = BIO_pop(s->wbio);
2151 }
2152
2153 BIO_free(s->bbio);
2154 s->bbio = NULL;
2155}
2156
2157void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
2158 ctx->quiet_shutdown = mode;
2159}
2160
2161int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
2162 return ctx->quiet_shutdown;
2163}
2164
2165void SSL_set_quiet_shutdown(SSL *s, int mode) { s->quiet_shutdown = mode; }
2166
2167int SSL_get_quiet_shutdown(const SSL *s) { return s->quiet_shutdown; }
2168
2169void SSL_set_shutdown(SSL *s, int mode) { s->shutdown = mode; }
2170
2171int SSL_get_shutdown(const SSL *s) { return s->shutdown; }
2172
2173int SSL_version(const SSL *s) { return s->version; }
2174
2175SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
2176
2177SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
2178 if (ssl->ctx == ctx) {
2179 return ssl->ctx;
2180 }
2181
2182 if (ctx == NULL) {
2183 ctx = ssl->initial_ctx;
2184 }
2185
David Benjamin2755a3e2015-04-22 16:17:58 -04002186 ssl_cert_free(ssl->cert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002187 ssl->cert = ssl_cert_dup(ctx->cert);
David Benjamin2755a3e2015-04-22 16:17:58 -04002188
Adam Langley0b5e3902015-05-15 13:08:38 -07002189 CRYPTO_refcount_inc(&ctx->references);
David Benjamin2755a3e2015-04-22 16:17:58 -04002190 SSL_CTX_free(ssl->ctx); /* decrement reference count */
Adam Langleyfcf25832014-12-18 17:42:32 -08002191 ssl->ctx = ctx;
2192
2193 ssl->sid_ctx_length = ctx->sid_ctx_length;
2194 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2195 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2196
2197 return ssl->ctx;
2198}
2199
2200int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
2201 return X509_STORE_set_default_paths(ctx->cert_store);
2202}
Adam Langley95c29f32014-06-20 12:00:00 -07002203
2204int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
Adam Langleyfcf25832014-12-18 17:42:32 -08002205 const char *CApath) {
2206 return X509_STORE_load_locations(ctx->cert_store, CAfile, CApath);
2207}
Adam Langley95c29f32014-06-20 12:00:00 -07002208
2209void SSL_set_info_callback(SSL *ssl,
Adam Langleyfcf25832014-12-18 17:42:32 -08002210 void (*cb)(const SSL *ssl, int type, int val)) {
2211 ssl->info_callback = cb;
2212}
Adam Langley95c29f32014-06-20 12:00:00 -07002213
Adam Langleyfcf25832014-12-18 17:42:32 -08002214void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/, int /*type*/,
2215 int /*val*/) {
2216 return ssl->info_callback;
2217}
Adam Langley95c29f32014-06-20 12:00:00 -07002218
Adam Langleyfcf25832014-12-18 17:42:32 -08002219int SSL_state(const SSL *ssl) { return ssl->state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002220
David Benjaminece089c2015-05-15 23:52:42 -04002221void SSL_set_state(SSL *ssl, int state) { }
Adam Langley95c29f32014-06-20 12:00:00 -07002222
Adam Langleyfcf25832014-12-18 17:42:32 -08002223void SSL_set_verify_result(SSL *ssl, long arg) { ssl->verify_result = arg; }
Adam Langley95c29f32014-06-20 12:00:00 -07002224
Adam Langleyfcf25832014-12-18 17:42:32 -08002225long SSL_get_verify_result(const SSL *ssl) { return ssl->verify_result; }
Adam Langley95c29f32014-06-20 12:00:00 -07002226
Adam Langleyfcf25832014-12-18 17:42:32 -08002227int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2228 CRYPTO_EX_dup *dup_func, 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, &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_set_ex_data(SSL *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_get_ex_data(const SSL *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 -08002245int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2246 CRYPTO_EX_dup *dup_func,
2247 CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002248 int index;
2249 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp,
2250 new_func, dup_func, free_func)) {
2251 return -1;
2252 }
2253 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002254}
Adam Langley95c29f32014-06-20 12:00:00 -07002255
Adam Langleyfcf25832014-12-18 17:42:32 -08002256int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg) {
2257 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2258}
Adam Langley95c29f32014-06-20 12:00:00 -07002259
Adam Langleyfcf25832014-12-18 17:42:32 -08002260void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx) {
2261 return CRYPTO_get_ex_data(&s->ex_data, idx);
2262}
Adam Langley95c29f32014-06-20 12:00:00 -07002263
Adam Langleyfcf25832014-12-18 17:42:32 -08002264X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
2265 return ctx->cert_store;
2266}
Adam Langley95c29f32014-06-20 12:00:00 -07002267
Adam Langleyfcf25832014-12-18 17:42:32 -08002268void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
David Benjamin2755a3e2015-04-22 16:17:58 -04002269 X509_STORE_free(ctx->cert_store);
Adam Langleyfcf25832014-12-18 17:42:32 -08002270 ctx->cert_store = store;
2271}
Adam Langley95c29f32014-06-20 12:00:00 -07002272
Adam Langleyfcf25832014-12-18 17:42:32 -08002273int SSL_want(const SSL *s) { return s->rwstate; }
Adam Langley95c29f32014-06-20 12:00:00 -07002274
Adam Langleyfcf25832014-12-18 17:42:32 -08002275void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
2276 RSA *(*cb)(SSL *ssl, int is_export,
2277 int keylength)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002278}
Adam Langley95c29f32014-06-20 12:00:00 -07002279
Adam Langleyfcf25832014-12-18 17:42:32 -08002280void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
2281 int keylength)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002282}
2283
2284void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
David Benjamin59015c32015-04-26 13:13:55 -04002285 DH *(*callback)(SSL *ssl, int is_export,
2286 int keylength)) {
2287 ctx->cert->dh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002288}
2289
David Benjamin59015c32015-04-26 13:13:55 -04002290void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*callback)(SSL *ssl, int is_export,
2291 int keylength)) {
2292 ssl->cert->dh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002293}
2294
2295void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,
David Benjamindd978782015-04-24 15:20:13 -04002296 EC_KEY *(*callback)(SSL *ssl, int is_export,
2297 int keylength)) {
David Benjamin59015c32015-04-26 13:13:55 -04002298 ctx->cert->ecdh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002299}
2300
2301void SSL_set_tmp_ecdh_callback(SSL *ssl,
David Benjamindd978782015-04-24 15:20:13 -04002302 EC_KEY *(*callback)(SSL *ssl, int is_export,
2303 int keylength)) {
David Benjamin59015c32015-04-26 13:13:55 -04002304 ssl->cert->ecdh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002305}
2306
2307int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
2308 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
David Benjamin3570d732015-06-29 00:28:17 -04002309 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -08002310 return 0;
2311 }
2312
David Benjamin2755a3e2015-04-22 16:17:58 -04002313 OPENSSL_free(ctx->psk_identity_hint);
Adam Langleyfcf25832014-12-18 17:42:32 -08002314
2315 if (identity_hint != NULL) {
2316 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2317 if (ctx->psk_identity_hint == NULL) {
2318 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002319 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002320 } else {
2321 ctx->psk_identity_hint = NULL;
2322 }
Adam Langley95c29f32014-06-20 12:00:00 -07002323
Adam Langleyfcf25832014-12-18 17:42:32 -08002324 return 1;
2325}
2326
2327int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint) {
2328 if (s == NULL) {
2329 return 0;
2330 }
2331
2332 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
David Benjamin3570d732015-06-29 00:28:17 -04002333 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -08002334 return 0;
2335 }
2336
2337 /* Clear currently configured hint, if any. */
David Benjamin2755a3e2015-04-22 16:17:58 -04002338 OPENSSL_free(s->psk_identity_hint);
2339 s->psk_identity_hint = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002340
2341 if (identity_hint != NULL) {
2342 s->psk_identity_hint = BUF_strdup(identity_hint);
2343 if (s->psk_identity_hint == NULL) {
2344 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002345 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002346 }
Adam Langley95c29f32014-06-20 12:00:00 -07002347
Adam Langleyfcf25832014-12-18 17:42:32 -08002348 return 1;
2349}
Adam Langley95c29f32014-06-20 12:00:00 -07002350
Adam Langleyfcf25832014-12-18 17:42:32 -08002351const char *SSL_get_psk_identity_hint(const SSL *s) {
2352 if (s == NULL) {
2353 return NULL;
2354 }
2355 return s->psk_identity_hint;
2356}
Adam Langley95c29f32014-06-20 12:00:00 -07002357
Adam Langleyfcf25832014-12-18 17:42:32 -08002358const char *SSL_get_psk_identity(const SSL *s) {
2359 if (s == NULL || s->session == NULL) {
2360 return NULL;
2361 }
Adam Langley95c29f32014-06-20 12:00:00 -07002362
Adam Langleyfcf25832014-12-18 17:42:32 -08002363 return s->session->psk_identity;
2364}
Adam Langley95c29f32014-06-20 12:00:00 -07002365
Adam Langleyfcf25832014-12-18 17:42:32 -08002366void SSL_set_psk_client_callback(
2367 SSL *s, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2368 unsigned int max_identity_len, uint8_t *psk,
2369 unsigned int max_psk_len)) {
2370 s->psk_client_callback = cb;
2371}
Adam Langley95c29f32014-06-20 12:00:00 -07002372
Adam Langleyfcf25832014-12-18 17:42:32 -08002373void SSL_CTX_set_psk_client_callback(
2374 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2375 unsigned int max_identity_len,
2376 uint8_t *psk, unsigned int max_psk_len)) {
2377 ctx->psk_client_callback = cb;
2378}
Adam Langley95c29f32014-06-20 12:00:00 -07002379
Adam Langleyfcf25832014-12-18 17:42:32 -08002380void SSL_set_psk_server_callback(
2381 SSL *s, unsigned int (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
2382 unsigned int max_psk_len)) {
2383 s->psk_server_callback = cb;
2384}
Adam Langley95c29f32014-06-20 12:00:00 -07002385
Adam Langleyfcf25832014-12-18 17:42:32 -08002386void SSL_CTX_set_psk_server_callback(
2387 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *identity,
2388 uint8_t *psk, unsigned int max_psk_len)) {
2389 ctx->psk_server_callback = cb;
2390}
Adam Langley95c29f32014-06-20 12:00:00 -07002391
Adam Langleyfcf25832014-12-18 17:42:32 -08002392void SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) {
2393 ctx->min_version = version;
2394}
Adam Langley95c29f32014-06-20 12:00:00 -07002395
Adam Langleyfcf25832014-12-18 17:42:32 -08002396void SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) {
2397 ctx->max_version = version;
2398}
Adam Langley0289c732014-06-20 12:00:00 -07002399
Adam Langleyfcf25832014-12-18 17:42:32 -08002400void SSL_set_min_version(SSL *ssl, uint16_t version) {
2401 ssl->min_version = version;
2402}
Adam Langley0289c732014-06-20 12:00:00 -07002403
Adam Langleyfcf25832014-12-18 17:42:32 -08002404void SSL_set_max_version(SSL *ssl, uint16_t version) {
2405 ssl->max_version = version;
2406}
Adam Langley95c29f32014-06-20 12:00:00 -07002407
Adam Langleyfcf25832014-12-18 17:42:32 -08002408void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
2409 void (*cb)(int write_p, int version,
2410 int content_type, const void *buf,
2411 size_t len, SSL *ssl, void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002412 ctx->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002413}
David Benjamin61ecccf2015-05-05 09:44:51 -04002414
2415void SSL_CTX_set_msg_callback_arg(SSL_CTX *ctx, void *arg) {
2416 ctx->msg_callback_arg = arg;
2417}
2418
Adam Langleyfcf25832014-12-18 17:42:32 -08002419void SSL_set_msg_callback(SSL *ssl,
2420 void (*cb)(int write_p, int version, int content_type,
2421 const void *buf, size_t len, SSL *ssl,
2422 void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002423 ssl->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002424}
Adam Langley95c29f32014-06-20 12:00:00 -07002425
David Benjamin61ecccf2015-05-05 09:44:51 -04002426void SSL_set_msg_callback_arg(SSL *ssl, void *arg) {
2427 ssl->msg_callback_arg = arg;
2428}
2429
Adam Langleyfcf25832014-12-18 17:42:32 -08002430void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio) {
David Benjamin2755a3e2015-04-22 16:17:58 -04002431 BIO_free(ctx->keylog_bio);
Adam Langleyfcf25832014-12-18 17:42:32 -08002432 ctx->keylog_bio = keylog_bio;
2433}
Adam Langley95c29f32014-06-20 12:00:00 -07002434
Adam Langleyfcf25832014-12-18 17:42:32 -08002435static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
2436 static const char hextable[] = "0123456789abcdef";
2437 uint8_t *out;
2438 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002439
Adam Langleyfcf25832014-12-18 17:42:32 -08002440 if (!CBB_add_space(cbb, &out, in_len * 2)) {
2441 return 0;
2442 }
Adam Langley95c29f32014-06-20 12:00:00 -07002443
Adam Langleyfcf25832014-12-18 17:42:32 -08002444 for (i = 0; i < in_len; i++) {
2445 *(out++) = (uint8_t)hextable[in[i] >> 4];
2446 *(out++) = (uint8_t)hextable[in[i] & 0xf];
2447 }
Adam Langley95c29f32014-06-20 12:00:00 -07002448
Adam Langleyfcf25832014-12-18 17:42:32 -08002449 return 1;
2450}
David Benjamin859ec3c2014-09-02 16:29:36 -04002451
2452int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -08002453 const uint8_t *encrypted_premaster,
2454 size_t encrypted_premaster_len,
2455 const uint8_t *premaster,
2456 size_t premaster_len) {
2457 BIO *bio = ctx->keylog_bio;
2458 CBB cbb;
2459 uint8_t *out;
2460 size_t out_len;
2461 int ret;
David Benjamin859ec3c2014-09-02 16:29:36 -04002462
Adam Langleyfcf25832014-12-18 17:42:32 -08002463 if (bio == NULL) {
2464 return 1;
2465 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002466
Adam Langleyfcf25832014-12-18 17:42:32 -08002467 if (encrypted_premaster_len < 8) {
David Benjamin3570d732015-06-29 00:28:17 -04002468 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08002469 return 0;
2470 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002471
David Benjamina8653202015-06-28 01:26:10 -04002472 CBB_zero(&cbb);
2473 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len * 2 + 1) ||
2474 !CBB_add_bytes(&cbb, (const uint8_t *)"RSA ", 4) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08002475 /* Only the first 8 bytes of the encrypted premaster secret are
2476 * logged. */
2477 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
2478 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2479 !cbb_add_hex(&cbb, premaster, premaster_len) ||
2480 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2481 !CBB_finish(&cbb, &out, &out_len)) {
2482 CBB_cleanup(&cbb);
2483 return 0;
2484 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002485
Adam Langley4bdb6e42015-05-15 15:29:21 -07002486 CRYPTO_MUTEX_lock_write(&ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -08002487 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
Adam Langley4bdb6e42015-05-15 15:29:21 -07002488 CRYPTO_MUTEX_unlock(&ctx->lock);
David Benjamin859ec3c2014-09-02 16:29:36 -04002489
Adam Langleyfcf25832014-12-18 17:42:32 -08002490 OPENSSL_free(out);
2491 return ret;
Adam Langley95f22882014-06-20 12:00:00 -07002492}
2493
Adam Langleyfcf25832014-12-18 17:42:32 -08002494int ssl_ctx_log_master_secret(SSL_CTX *ctx, const uint8_t *client_random,
2495 size_t client_random_len, const uint8_t *master,
2496 size_t master_len) {
2497 BIO *bio = ctx->keylog_bio;
2498 CBB cbb;
2499 uint8_t *out;
2500 size_t out_len;
2501 int ret;
Adam Langleyadb739e2014-06-20 12:00:00 -07002502
Adam Langleyfcf25832014-12-18 17:42:32 -08002503 if (bio == NULL) {
2504 return 1;
2505 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002506
Adam Langleyfcf25832014-12-18 17:42:32 -08002507 if (client_random_len != 32) {
David Benjamin3570d732015-06-29 00:28:17 -04002508 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08002509 return 0;
2510 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002511
David Benjamina8653202015-06-28 01:26:10 -04002512 CBB_zero(&cbb);
2513 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len * 2 + 1) ||
2514 !CBB_add_bytes(&cbb, (const uint8_t *)"CLIENT_RANDOM ", 14) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08002515 !cbb_add_hex(&cbb, client_random, 32) ||
2516 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2517 !cbb_add_hex(&cbb, master, master_len) ||
2518 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2519 !CBB_finish(&cbb, &out, &out_len)) {
2520 CBB_cleanup(&cbb);
2521 return 0;
2522 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002523
Adam Langley4bdb6e42015-05-15 15:29:21 -07002524 CRYPTO_MUTEX_lock_write(&ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -08002525 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
Adam Langley4bdb6e42015-05-15 15:29:21 -07002526 CRYPTO_MUTEX_unlock(&ctx->lock);
David Benjamine99e9122014-12-11 01:46:01 -05002527
Adam Langleyfcf25832014-12-18 17:42:32 -08002528 OPENSSL_free(out);
2529 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -07002530}
2531
David Benjamined7c4752015-02-16 19:16:46 -05002532int SSL_in_false_start(const SSL *s) {
2533 return s->s3->tmp.in_false_start;
2534}
2535
Adam Langleyfcf25832014-12-18 17:42:32 -08002536int SSL_cutthrough_complete(const SSL *s) {
David Benjamined7c4752015-02-16 19:16:46 -05002537 return SSL_in_false_start(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002538}
Adam Langley95c29f32014-06-20 12:00:00 -07002539
Adam Langleyfcf25832014-12-18 17:42:32 -08002540void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
2541 size_t *ssl_session_size) {
2542 *ssl_size = sizeof(SSL);
2543 *ssl_ctx_size = sizeof(SSL_CTX);
2544 *ssl_session_size = sizeof(SSL_SESSION);
2545}
Feng Lu41aa3252014-11-21 22:47:56 -08002546
David Benjamined7c4752015-02-16 19:16:46 -05002547int ssl3_can_false_start(const SSL *s) {
David Benjamin195dc782015-02-19 13:27:05 -05002548 const SSL_CIPHER *const cipher = SSL_get_current_cipher(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002549
Adam Langleye631d962015-04-01 13:11:01 -07002550 /* False Start only for TLS 1.2 with an ECDHE+AEAD cipher and ALPN or NPN. */
David Benjamin195dc782015-02-19 13:27:05 -05002551 return !SSL_IS_DTLS(s) &&
2552 SSL_version(s) >= TLS1_2_VERSION &&
2553 (s->s3->alpn_selected || s->s3->next_proto_neg_seen) &&
2554 cipher != NULL &&
Adam Langleye631d962015-04-01 13:11:01 -07002555 cipher->algorithm_mkey == SSL_kECDHE &&
David Benjamin195dc782015-02-19 13:27:05 -05002556 (cipher->algorithm_enc == SSL_AES128GCM ||
2557 cipher->algorithm_enc == SSL_AES256GCM ||
2558 cipher->algorithm_enc == SSL_CHACHA20POLY1305);
Adam Langleyfcf25832014-12-18 17:42:32 -08002559}
2560
2561const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version) {
2562 switch (version) {
2563 case SSL3_VERSION:
2564 return &SSLv3_enc_data;
2565
2566 case TLS1_VERSION:
2567 return &TLSv1_enc_data;
2568
David Benjamin9e13e1a2015-03-05 01:56:32 -05002569 case DTLS1_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002570 case TLS1_1_VERSION:
2571 return &TLSv1_1_enc_data;
2572
David Benjamin9e13e1a2015-03-05 01:56:32 -05002573 case DTLS1_2_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002574 case TLS1_2_VERSION:
2575 return &TLSv1_2_enc_data;
2576
Adam Langleyfcf25832014-12-18 17:42:32 -08002577 default:
2578 return NULL;
2579 }
2580}
2581
2582uint16_t ssl3_get_max_server_version(const SSL *s) {
2583 uint16_t max_version;
2584
2585 if (SSL_IS_DTLS(s)) {
2586 max_version = (s->max_version != 0) ? s->max_version : DTLS1_2_VERSION;
2587 if (!(s->options & SSL_OP_NO_DTLSv1_2) && DTLS1_2_VERSION >= max_version) {
2588 return DTLS1_2_VERSION;
2589 }
2590 if (!(s->options & SSL_OP_NO_DTLSv1) && DTLS1_VERSION >= max_version) {
2591 return DTLS1_VERSION;
2592 }
2593 return 0;
2594 }
2595
2596 max_version = (s->max_version != 0) ? s->max_version : TLS1_2_VERSION;
2597 if (!(s->options & SSL_OP_NO_TLSv1_2) && TLS1_2_VERSION <= max_version) {
2598 return TLS1_2_VERSION;
2599 }
2600 if (!(s->options & SSL_OP_NO_TLSv1_1) && TLS1_1_VERSION <= max_version) {
2601 return TLS1_1_VERSION;
2602 }
2603 if (!(s->options & SSL_OP_NO_TLSv1) && TLS1_VERSION <= max_version) {
2604 return TLS1_VERSION;
2605 }
2606 if (!(s->options & SSL_OP_NO_SSLv3) && SSL3_VERSION <= max_version) {
2607 return SSL3_VERSION;
2608 }
2609 return 0;
2610}
2611
2612uint16_t ssl3_get_mutual_version(SSL *s, uint16_t client_version) {
2613 uint16_t version = 0;
2614
2615 if (SSL_IS_DTLS(s)) {
2616 /* Clamp client_version to max_version. */
2617 if (s->max_version != 0 && client_version < s->max_version) {
2618 client_version = s->max_version;
2619 }
2620
2621 if (client_version <= DTLS1_2_VERSION && !(s->options & SSL_OP_NO_DTLSv1_2)) {
2622 version = DTLS1_2_VERSION;
2623 } else if (client_version <= DTLS1_VERSION &&
David Benjaminb18f0242015-03-10 18:30:08 -04002624 !(s->options & SSL_OP_NO_DTLSv1)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002625 version = DTLS1_VERSION;
2626 }
2627
2628 /* Check against min_version. */
2629 if (version != 0 && s->min_version != 0 && version > s->min_version) {
2630 return 0;
2631 }
2632 return version;
2633 } else {
2634 /* Clamp client_version to max_version. */
2635 if (s->max_version != 0 && client_version > s->max_version) {
2636 client_version = s->max_version;
2637 }
2638
2639 if (client_version >= TLS1_2_VERSION && !(s->options & SSL_OP_NO_TLSv1_2)) {
2640 version = TLS1_2_VERSION;
2641 } else if (client_version >= TLS1_1_VERSION &&
2642 !(s->options & SSL_OP_NO_TLSv1_1)) {
2643 version = TLS1_1_VERSION;
2644 } else if (client_version >= TLS1_VERSION && !(s->options & SSL_OP_NO_TLSv1)) {
2645 version = TLS1_VERSION;
2646 } else if (client_version >= SSL3_VERSION && !(s->options & SSL_OP_NO_SSLv3)) {
2647 version = SSL3_VERSION;
2648 }
2649
2650 /* Check against min_version. */
2651 if (version != 0 && s->min_version != 0 && version < s->min_version) {
2652 return 0;
2653 }
2654 return version;
2655 }
2656}
2657
2658uint16_t ssl3_get_max_client_version(SSL *s) {
David Benjamin123a8fd2015-04-26 14:16:41 -04002659 uint32_t options = s->options;
Adam Langleyfcf25832014-12-18 17:42:32 -08002660 uint16_t version = 0;
2661
2662 /* OpenSSL's API for controlling versions entails blacklisting individual
2663 * protocols. This has two problems. First, on the client, the protocol can
2664 * only express a contiguous range of versions. Second, a library consumer
2665 * trying to set a maximum version cannot disable protocol versions that get
2666 * added in a future version of the library.
2667 *
2668 * To account for both of these, OpenSSL interprets the client-side bitmask
2669 * as a min/max range by picking the lowest contiguous non-empty range of
2670 * enabled protocols. Note that this means it is impossible to set a maximum
2671 * version of TLS 1.2 in a future-proof way.
2672 *
2673 * By this scheme, the maximum version is the lowest version V such that V is
2674 * enabled and V+1 is disabled or unimplemented. */
2675 if (SSL_IS_DTLS(s)) {
2676 if (!(options & SSL_OP_NO_DTLSv1_2)) {
2677 version = DTLS1_2_VERSION;
2678 }
2679 if (!(options & SSL_OP_NO_DTLSv1) && (options & SSL_OP_NO_DTLSv1_2)) {
2680 version = DTLS1_VERSION;
2681 }
2682 if (s->max_version != 0 && version < s->max_version) {
2683 version = s->max_version;
2684 }
2685 } else {
2686 if (!(options & SSL_OP_NO_TLSv1_2)) {
2687 version = TLS1_2_VERSION;
2688 }
2689 if (!(options & SSL_OP_NO_TLSv1_1) && (options & SSL_OP_NO_TLSv1_2)) {
2690 version = TLS1_1_VERSION;
2691 }
2692 if (!(options & SSL_OP_NO_TLSv1) && (options & SSL_OP_NO_TLSv1_1)) {
2693 version = TLS1_VERSION;
2694 }
2695 if (!(options & SSL_OP_NO_SSLv3) && (options & SSL_OP_NO_TLSv1)) {
2696 version = SSL3_VERSION;
2697 }
2698 if (s->max_version != 0 && version > s->max_version) {
2699 version = s->max_version;
2700 }
2701 }
2702
2703 return version;
2704}
2705
2706int ssl3_is_version_enabled(SSL *s, uint16_t version) {
2707 if (SSL_IS_DTLS(s)) {
2708 if (s->max_version != 0 && version < s->max_version) {
2709 return 0;
2710 }
2711 if (s->min_version != 0 && version > s->min_version) {
2712 return 0;
2713 }
2714
2715 switch (version) {
2716 case DTLS1_VERSION:
2717 return !(s->options & SSL_OP_NO_DTLSv1);
2718
2719 case DTLS1_2_VERSION:
2720 return !(s->options & SSL_OP_NO_DTLSv1_2);
2721
2722 default:
2723 return 0;
2724 }
2725 } else {
2726 if (s->max_version != 0 && version > s->max_version) {
2727 return 0;
2728 }
2729 if (s->min_version != 0 && version < s->min_version) {
2730 return 0;
2731 }
2732
2733 switch (version) {
2734 case SSL3_VERSION:
2735 return !(s->options & SSL_OP_NO_SSLv3);
2736
2737 case TLS1_VERSION:
2738 return !(s->options & SSL_OP_NO_TLSv1);
2739
2740 case TLS1_1_VERSION:
2741 return !(s->options & SSL_OP_NO_TLSv1_1);
2742
2743 case TLS1_2_VERSION:
2744 return !(s->options & SSL_OP_NO_TLSv1_2);
2745
2746 default:
2747 return 0;
2748 }
2749 }
2750}
2751
David Benjaminea72bd02014-12-21 21:27:41 -05002752uint16_t ssl3_version_from_wire(SSL *s, uint16_t wire_version) {
2753 if (!SSL_IS_DTLS(s)) {
2754 return wire_version;
2755 }
2756
2757 uint16_t tls_version = ~wire_version;
2758 uint16_t version = tls_version + 0x0201;
2759 /* If either component overflowed, clamp it so comparisons still work. */
2760 if ((version >> 8) < (tls_version >> 8)) {
2761 version = 0xff00 | (version & 0xff);
2762 }
2763 if ((version & 0xff) < (tls_version & 0xff)) {
2764 version = (version & 0xff00) | 0xff;
2765 }
2766 /* DTLS 1.0 maps to TLS 1.1, not TLS 1.0. */
2767 if (version == TLS1_VERSION) {
2768 version = TLS1_1_VERSION;
2769 }
2770 return version;
2771}
2772
Adam Langleyfcf25832014-12-18 17:42:32 -08002773int SSL_cache_hit(SSL *s) { return s->hit; }
2774
2775int SSL_is_server(SSL *s) { return s->server; }
2776
Adam Langley524e7172015-02-20 16:04:00 -08002777void SSL_CTX_set_dos_protection_cb(
2778 SSL_CTX *ctx, int (*cb)(const struct ssl_early_callback_ctx *)) {
2779 ctx->dos_protection_cb = cb;
2780}
2781
David Benjaminb16346b2015-04-08 19:16:58 -04002782void SSL_set_reject_peer_renegotiations(SSL *s, int reject) {
David Benjamin897e5e02015-05-12 17:03:54 -04002783 s->accept_peer_renegotiations = !reject;
David Benjaminb16346b2015-04-08 19:16:58 -04002784}
2785
Adam Langley3f92d212015-02-20 15:32:52 -08002786int SSL_get_rc4_state(const SSL *ssl, const RC4_KEY **read_key,
2787 const RC4_KEY **write_key) {
2788 if (ssl->aead_read_ctx == NULL || ssl->aead_write_ctx == NULL) {
2789 return 0;
2790 }
2791
2792 return EVP_AEAD_CTX_get_rc4_state(&ssl->aead_read_ctx->ctx, read_key) &&
2793 EVP_AEAD_CTX_get_rc4_state(&ssl->aead_write_ctx->ctx, write_key);
2794}
David Benjaminda881e92015-04-26 14:45:04 -04002795
Adam Langleyaf0e32c2015-06-03 09:57:23 -07002796int SSL_get_tls_unique(const SSL *ssl, uint8_t *out, size_t *out_len,
2797 size_t max_out) {
2798 /* The tls-unique value is the first Finished message in the handshake, which
2799 * is the client's in a full handshake and the server's for a resumption. See
2800 * https://tools.ietf.org/html/rfc5929#section-3.1. */
2801 const uint8_t *finished = ssl->s3->previous_client_finished;
2802 size_t finished_len = ssl->s3->previous_client_finished_len;
2803 if (ssl->hit) {
2804 /* tls-unique is broken for resumed sessions unless EMS is used. */
2805 if (!ssl->session->extended_master_secret) {
2806 goto err;
2807 }
2808 finished = ssl->s3->previous_server_finished;
2809 finished_len = ssl->s3->previous_server_finished_len;
2810 }
2811
2812 if (!ssl->s3->initial_handshake_complete ||
2813 ssl->version < TLS1_VERSION) {
2814 goto err;
2815 }
2816
2817 *out_len = finished_len;
2818 if (finished_len > max_out) {
2819 *out_len = max_out;
2820 }
2821
2822 memcpy(out, finished, *out_len);
2823 return 1;
2824
2825err:
2826 *out_len = 0;
2827 memset(out, 0, max_out);
2828 return 0;
2829}
2830
David Benjaminda881e92015-04-26 14:45:04 -04002831int SSL_CTX_sess_connect(const SSL_CTX *ctx) { return 0; }
2832int SSL_CTX_sess_connect_good(const SSL_CTX *ctx) { return 0; }
2833int SSL_CTX_sess_connect_renegotiate(const SSL_CTX *ctx) { return 0; }
2834int SSL_CTX_sess_accept(const SSL_CTX *ctx) { return 0; }
2835int SSL_CTX_sess_accept_renegotiate(const SSL_CTX *ctx) { return 0; }
2836int SSL_CTX_sess_accept_good(const SSL_CTX *ctx) { return 0; }
2837int SSL_CTX_sess_hits(const SSL_CTX *ctx) { return 0; }
2838int SSL_CTX_sess_cb_hits(const SSL_CTX *ctx) { return 0; }
2839int SSL_CTX_sess_misses(const SSL_CTX *ctx) { return 0; }
2840int SSL_CTX_sess_timeouts(const SSL_CTX *ctx) { return 0; }
2841int SSL_CTX_sess_cache_full(const SSL_CTX *ctx) { return 0; }
Matt Braithwaite4838d8a2015-08-20 13:20:03 -07002842void ERR_load_SSL_strings(void) {}