blob: f8c865feb1bb98518b30befdd320486b7c4f9473 [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
Adam Langleyfcf25832014-12-18 17:42:32 -0800158/* Some error codes are special. Ensure the make_errors.go script never
159 * regresses this. */
160OPENSSL_COMPILE_ASSERT(SSL_R_TLSV1_ALERT_NO_RENEGOTIATION ==
161 SSL_AD_NO_RENEGOTIATION + SSL_AD_REASON_OFFSET,
162 ssl_alert_reason_code_mismatch);
David Benjamine1136082014-09-20 12:28:58 -0400163
David Benjamin1d0a1942015-04-26 15:35:35 -0400164/* kMaxHandshakeSize is the maximum size, in bytes, of a handshake message. */
165static const size_t kMaxHandshakeSize = (1u << 24) - 1;
166
David Benjamin9f33fc62015-04-15 17:29:53 -0400167static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl = CRYPTO_EX_DATA_CLASS_INIT;
168static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl_ctx = CRYPTO_EX_DATA_CLASS_INIT;
169
Adam Langleyfcf25832014-12-18 17:42:32 -0800170int SSL_clear(SSL *s) {
171 if (s->method == NULL) {
172 OPENSSL_PUT_ERROR(SSL, SSL_clear, SSL_R_NO_METHOD_SPECIFIED);
173 return 0;
174 }
Adam Langley95c29f32014-06-20 12:00:00 -0700175
Adam Langleyfcf25832014-12-18 17:42:32 -0800176 if (ssl_clear_bad_session(s)) {
177 SSL_SESSION_free(s->session);
178 s->session = NULL;
179 }
Adam Langley95c29f32014-06-20 12:00:00 -0700180
Adam Langleyfcf25832014-12-18 17:42:32 -0800181 s->hit = 0;
182 s->shutdown = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700183
Adam Langleyfcf25832014-12-18 17:42:32 -0800184 if (s->renegotiate) {
185 OPENSSL_PUT_ERROR(SSL, SSL_clear, ERR_R_INTERNAL_ERROR);
186 return 0;
187 }
Adam Langley95c29f32014-06-20 12:00:00 -0700188
Adam Langleyfcf25832014-12-18 17:42:32 -0800189 /* SSL_clear may be called before or after the |s| is initialized in either
190 * accept or connect state. In the latter case, SSL_clear should preserve the
191 * half and reset |s->state| accordingly. */
192 if (s->handshake_func != NULL) {
193 if (s->server) {
194 SSL_set_accept_state(s);
195 } else {
196 SSL_set_connect_state(s);
197 }
198 } else {
199 assert(s->state == 0);
200 }
Adam Langley95c29f32014-06-20 12:00:00 -0700201
David Benjamin62fd1622015-01-11 13:30:01 -0500202 /* TODO(davidben): Some state on |s| is reset both in |SSL_new| and
203 * |SSL_clear| because it is per-connection state rather than configuration
204 * state. Per-connection state should be on |s->s3| and |s->d1| so it is
205 * naturally reset at the right points between |SSL_new|, |SSL_clear|, and
206 * |ssl3_new|. */
207
Adam Langleyfcf25832014-12-18 17:42:32 -0800208 s->rwstate = SSL_NOTHING;
209 s->rstate = SSL_ST_READ_HEADER;
Adam Langley95c29f32014-06-20 12:00:00 -0700210
David Benjamin2755a3e2015-04-22 16:17:58 -0400211 BUF_MEM_free(s->init_buf);
212 s->init_buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700213
David Benjamin62fd1622015-01-11 13:30:01 -0500214 s->packet = NULL;
215 s->packet_length = 0;
216
Adam Langleyfcf25832014-12-18 17:42:32 -0800217 ssl_clear_cipher_ctx(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700218
David Benjamin2755a3e2015-04-22 16:17:58 -0400219 OPENSSL_free(s->next_proto_negotiated);
220 s->next_proto_negotiated = NULL;
221 s->next_proto_negotiated_len = 0;
David Benjamin62fd1622015-01-11 13:30:01 -0500222
223 /* The s->d1->mtu is simultaneously configuration (preserved across
224 * clear) and connection-specific state (gets reset).
225 *
226 * TODO(davidben): Avoid this. */
227 unsigned mtu = 0;
228 if (s->d1 != NULL) {
229 mtu = s->d1->mtu;
230 }
231
232 s->method->ssl_free(s);
233 if (!s->method->ssl_new(s)) {
234 return 0;
235 }
236 s->enc_method = ssl3_get_enc_method(s->version);
237 assert(s->enc_method != NULL);
238
239 if (SSL_IS_DTLS(s) && (SSL_get_options(s) & SSL_OP_NO_QUERY_MTU)) {
240 s->d1->mtu = mtu;
241 }
242
Adam Langleyfcf25832014-12-18 17:42:32 -0800243 s->client_version = s->version;
Adam Langley95c29f32014-06-20 12:00:00 -0700244
Adam Langleyfcf25832014-12-18 17:42:32 -0800245 return 1;
246}
Adam Langley95c29f32014-06-20 12:00:00 -0700247
Adam Langleyfcf25832014-12-18 17:42:32 -0800248SSL *SSL_new(SSL_CTX *ctx) {
249 SSL *s;
Adam Langley95c29f32014-06-20 12:00:00 -0700250
Adam Langleyfcf25832014-12-18 17:42:32 -0800251 if (ctx == NULL) {
252 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_NULL_SSL_CTX);
253 return NULL;
254 }
255 if (ctx->method == NULL) {
256 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
257 return NULL;
258 }
Adam Langley95c29f32014-06-20 12:00:00 -0700259
Adam Langleyfcf25832014-12-18 17:42:32 -0800260 s = (SSL *)OPENSSL_malloc(sizeof(SSL));
261 if (s == NULL) {
262 goto err;
263 }
264 memset(s, 0, sizeof(SSL));
Adam Langley95c29f32014-06-20 12:00:00 -0700265
Adam Langleyfcf25832014-12-18 17:42:32 -0800266 s->min_version = ctx->min_version;
267 s->max_version = ctx->max_version;
David Benjamin1eb367c2014-12-12 18:17:51 -0500268
Adam Langleyfcf25832014-12-18 17:42:32 -0800269 s->options = ctx->options;
270 s->mode = ctx->mode;
271 s->max_cert_list = ctx->max_cert_list;
Adam Langley95c29f32014-06-20 12:00:00 -0700272
David Benjamina5a3eeb2015-03-18 20:26:30 -0400273 s->cert = ssl_cert_dup(ctx->cert);
274 if (s->cert == NULL) {
275 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -0800276 }
Adam Langley95c29f32014-06-20 12:00:00 -0700277
Adam Langleyfcf25832014-12-18 17:42:32 -0800278 s->read_ahead = ctx->read_ahead;
279 s->msg_callback = ctx->msg_callback;
280 s->msg_callback_arg = ctx->msg_callback_arg;
281 s->verify_mode = ctx->verify_mode;
282 s->sid_ctx_length = ctx->sid_ctx_length;
283 assert(s->sid_ctx_length <= sizeof s->sid_ctx);
284 memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));
285 s->verify_callback = ctx->default_verify_callback;
286 s->generate_session_id = ctx->generate_session_id;
Adam Langley95c29f32014-06-20 12:00:00 -0700287
Adam Langleyfcf25832014-12-18 17:42:32 -0800288 s->param = X509_VERIFY_PARAM_new();
289 if (!s->param) {
290 goto err;
291 }
292 X509_VERIFY_PARAM_inherit(s->param, ctx->param);
293 s->quiet_shutdown = ctx->quiet_shutdown;
294 s->max_send_fragment = ctx->max_send_fragment;
Adam Langley95c29f32014-06-20 12:00:00 -0700295
Adam Langleyfcf25832014-12-18 17:42:32 -0800296 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
297 s->ctx = ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -0800298 s->tlsext_ticket_expected = 0;
299 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
300 s->initial_ctx = ctx;
301 if (ctx->tlsext_ecpointformatlist) {
302 s->tlsext_ecpointformatlist = BUF_memdup(
303 ctx->tlsext_ecpointformatlist, ctx->tlsext_ecpointformatlist_length);
304 if (!s->tlsext_ecpointformatlist) {
305 goto err;
306 }
307 s->tlsext_ecpointformatlist_length = ctx->tlsext_ecpointformatlist_length;
308 }
Adam Langley95c29f32014-06-20 12:00:00 -0700309
Adam Langleyfcf25832014-12-18 17:42:32 -0800310 if (ctx->tlsext_ellipticcurvelist) {
311 s->tlsext_ellipticcurvelist =
312 BUF_memdup(ctx->tlsext_ellipticcurvelist,
313 ctx->tlsext_ellipticcurvelist_length * 2);
314 if (!s->tlsext_ellipticcurvelist) {
315 goto err;
316 }
317 s->tlsext_ellipticcurvelist_length = ctx->tlsext_ellipticcurvelist_length;
318 }
319 s->next_proto_negotiated = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700320
Adam Langleyfcf25832014-12-18 17:42:32 -0800321 if (s->ctx->alpn_client_proto_list) {
322 s->alpn_client_proto_list = BUF_memdup(s->ctx->alpn_client_proto_list,
323 s->ctx->alpn_client_proto_list_len);
324 if (s->alpn_client_proto_list == NULL) {
325 goto err;
326 }
327 s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;
328 }
Adam Langley95c29f32014-06-20 12:00:00 -0700329
Adam Langleyfcf25832014-12-18 17:42:32 -0800330 s->verify_result = X509_V_OK;
331 s->method = ctx->method;
Adam Langley95c29f32014-06-20 12:00:00 -0700332
Adam Langleyfcf25832014-12-18 17:42:32 -0800333 if (!s->method->ssl_new(s)) {
334 goto err;
335 }
336 s->enc_method = ssl3_get_enc_method(s->version);
337 assert(s->enc_method != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700338
David Benjamin62fd1622015-01-11 13:30:01 -0500339 s->rwstate = SSL_NOTHING;
340 s->rstate = SSL_ST_READ_HEADER;
Adam Langley95c29f32014-06-20 12:00:00 -0700341
David Benjamin9f33fc62015-04-15 17:29:53 -0400342 CRYPTO_new_ex_data(&g_ex_data_class_ssl, s, &s->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700343
Adam Langleyfcf25832014-12-18 17:42:32 -0800344 s->psk_identity_hint = NULL;
345 if (ctx->psk_identity_hint) {
346 s->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
347 if (s->psk_identity_hint == NULL) {
348 goto err;
349 }
350 }
351 s->psk_client_callback = ctx->psk_client_callback;
352 s->psk_server_callback = ctx->psk_server_callback;
Adam Langley95c29f32014-06-20 12:00:00 -0700353
David Benjamin02ddbfd2015-01-11 13:09:11 -0500354 s->tlsext_channel_id_enabled = ctx->tlsext_channel_id_enabled;
355 if (ctx->tlsext_channel_id_private) {
356 s->tlsext_channel_id_private = EVP_PKEY_dup(ctx->tlsext_channel_id_private);
357 }
358
Adam Langleyfcf25832014-12-18 17:42:32 -0800359 s->signed_cert_timestamps_enabled = s->ctx->signed_cert_timestamps_enabled;
360 s->ocsp_stapling_enabled = s->ctx->ocsp_stapling_enabled;
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200361
Adam Langleyfcf25832014-12-18 17:42:32 -0800362 return s;
363
Adam Langley95c29f32014-06-20 12:00:00 -0700364err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400365 SSL_free(s);
Adam Langleyfcf25832014-12-18 17:42:32 -0800366 OPENSSL_PUT_ERROR(SSL, SSL_new, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700367
Adam Langleyfcf25832014-12-18 17:42:32 -0800368 return NULL;
369}
Adam Langley95c29f32014-06-20 12:00:00 -0700370
Adam Langleyfcf25832014-12-18 17:42:32 -0800371int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
372 unsigned int sid_ctx_len) {
373 if (sid_ctx_len > sizeof ctx->sid_ctx) {
374 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_session_id_context,
375 SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
376 return 0;
377 }
378 ctx->sid_ctx_length = sid_ctx_len;
379 memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700380
Adam Langleyfcf25832014-12-18 17:42:32 -0800381 return 1;
382}
Adam Langley95c29f32014-06-20 12:00:00 -0700383
Adam Langleyfcf25832014-12-18 17:42:32 -0800384int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
385 unsigned int sid_ctx_len) {
386 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
387 OPENSSL_PUT_ERROR(SSL, SSL_set_session_id_context,
388 SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
389 return 0;
390 }
391 ssl->sid_ctx_length = sid_ctx_len;
392 memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700393
Adam Langleyfcf25832014-12-18 17:42:32 -0800394 return 1;
395}
Adam Langley95c29f32014-06-20 12:00:00 -0700396
Adam Langleyfcf25832014-12-18 17:42:32 -0800397int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb) {
398 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
399 ctx->generate_session_id = cb;
400 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
401 return 1;
402}
Adam Langley95c29f32014-06-20 12:00:00 -0700403
Adam Langleyfcf25832014-12-18 17:42:32 -0800404int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800405 ssl->generate_session_id = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -0800406 return 1;
407}
Adam Langley95c29f32014-06-20 12:00:00 -0700408
Adam Langleyfcf25832014-12-18 17:42:32 -0800409int SSL_has_matching_session_id(const SSL *ssl, const uint8_t *id,
410 unsigned int id_len) {
411 /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how we
412 * can "construct" a session to give us the desired check - ie. to find if
413 * there's a session in the hash table that would conflict with any new
414 * session built out of this id/id_len and the ssl_version in use by this
415 * SSL. */
416 SSL_SESSION r, *p;
Adam Langley95c29f32014-06-20 12:00:00 -0700417
Adam Langleyfcf25832014-12-18 17:42:32 -0800418 if (id_len > sizeof r.session_id) {
419 return 0;
420 }
Adam Langley95c29f32014-06-20 12:00:00 -0700421
Adam Langleyfcf25832014-12-18 17:42:32 -0800422 r.ssl_version = ssl->version;
423 r.session_id_length = id_len;
424 memcpy(r.session_id, id, id_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700425
Adam Langleyfcf25832014-12-18 17:42:32 -0800426 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
427 p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r);
428 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
429 return p != NULL;
430}
Adam Langley95c29f32014-06-20 12:00:00 -0700431
Adam Langleyfcf25832014-12-18 17:42:32 -0800432int SSL_CTX_set_purpose(SSL_CTX *s, int purpose) {
433 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
434}
435
436int SSL_set_purpose(SSL *s, int purpose) {
437 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
438}
439
440int SSL_CTX_set_trust(SSL_CTX *s, int trust) {
441 return X509_VERIFY_PARAM_set_trust(s->param, trust);
442}
443
444int SSL_set_trust(SSL *s, int trust) {
445 return X509_VERIFY_PARAM_set_trust(s->param, trust);
446}
447
448int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm) {
449 return X509_VERIFY_PARAM_set1(ctx->param, vpm);
450}
451
452int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm) {
453 return X509_VERIFY_PARAM_set1(ssl->param, vpm);
454}
Adam Langley95c29f32014-06-20 12:00:00 -0700455
Adam Langley858a88d2014-06-20 12:00:00 -0700456void ssl_cipher_preference_list_free(
Adam Langleyfcf25832014-12-18 17:42:32 -0800457 struct ssl_cipher_preference_list_st *cipher_list) {
David Benjamin5d1ec732015-04-22 13:38:00 -0400458 if (cipher_list == NULL) {
459 return;
460 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800461 sk_SSL_CIPHER_free(cipher_list->ciphers);
462 OPENSSL_free(cipher_list->in_group_flags);
463 OPENSSL_free(cipher_list);
464}
Adam Langley858a88d2014-06-20 12:00:00 -0700465
Adam Langleyfcf25832014-12-18 17:42:32 -0800466struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_dup(
467 struct ssl_cipher_preference_list_st *cipher_list) {
468 struct ssl_cipher_preference_list_st *ret = NULL;
469 size_t n = sk_SSL_CIPHER_num(cipher_list->ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700470
Adam Langleyfcf25832014-12-18 17:42:32 -0800471 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
472 if (!ret) {
473 goto err;
474 }
475
476 ret->ciphers = NULL;
477 ret->in_group_flags = NULL;
478 ret->ciphers = sk_SSL_CIPHER_dup(cipher_list->ciphers);
479 if (!ret->ciphers) {
480 goto err;
481 }
482 ret->in_group_flags = BUF_memdup(cipher_list->in_group_flags, n);
483 if (!ret->in_group_flags) {
484 goto err;
485 }
486
487 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -0700488
489err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400490 ssl_cipher_preference_list_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -0800491 return NULL;
492}
Adam Langley858a88d2014-06-20 12:00:00 -0700493
Adam Langleyfcf25832014-12-18 17:42:32 -0800494struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_from_ciphers(
495 STACK_OF(SSL_CIPHER) * ciphers) {
496 struct ssl_cipher_preference_list_st *ret = NULL;
497 size_t n = sk_SSL_CIPHER_num(ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700498
Adam Langleyfcf25832014-12-18 17:42:32 -0800499 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
500 if (!ret) {
501 goto err;
502 }
503 ret->ciphers = NULL;
504 ret->in_group_flags = NULL;
505 ret->ciphers = sk_SSL_CIPHER_dup(ciphers);
506 if (!ret->ciphers) {
507 goto err;
508 }
509 ret->in_group_flags = OPENSSL_malloc(n);
510 if (!ret->in_group_flags) {
511 goto err;
512 }
513 memset(ret->in_group_flags, 0, n);
514 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -0700515
516err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400517 ssl_cipher_preference_list_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -0800518 return NULL;
519}
Adam Langley858a88d2014-06-20 12:00:00 -0700520
Adam Langleyfcf25832014-12-18 17:42:32 -0800521X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) { return ctx->param; }
Adam Langley95c29f32014-06-20 12:00:00 -0700522
Adam Langleyfcf25832014-12-18 17:42:32 -0800523X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) { return ssl->param; }
Adam Langley95c29f32014-06-20 12:00:00 -0700524
Adam Langleyfcf25832014-12-18 17:42:32 -0800525void SSL_certs_clear(SSL *s) { ssl_cert_clear_certs(s->cert); }
Adam Langley95c29f32014-06-20 12:00:00 -0700526
Adam Langleyfcf25832014-12-18 17:42:32 -0800527void SSL_free(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800528 if (s == NULL) {
529 return;
530 }
Adam Langley95c29f32014-06-20 12:00:00 -0700531
David Benjamin2755a3e2015-04-22 16:17:58 -0400532 X509_VERIFY_PARAM_free(s->param);
Adam Langley95c29f32014-06-20 12:00:00 -0700533
David Benjamin9f33fc62015-04-15 17:29:53 -0400534 CRYPTO_free_ex_data(&g_ex_data_class_ssl, s, &s->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700535
Adam Langleyfcf25832014-12-18 17:42:32 -0800536 if (s->bbio != NULL) {
537 /* If the buffering BIO is in place, pop it off */
538 if (s->bbio == s->wbio) {
539 s->wbio = BIO_pop(s->wbio);
540 }
541 BIO_free(s->bbio);
542 s->bbio = NULL;
543 }
Adam Langley95c29f32014-06-20 12:00:00 -0700544
David Benjamin2755a3e2015-04-22 16:17:58 -0400545 int free_wbio = s->wbio != s->rbio;
546 BIO_free_all(s->rbio);
547 if (free_wbio) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800548 BIO_free_all(s->wbio);
549 }
Adam Langley95c29f32014-06-20 12:00:00 -0700550
David Benjamin2755a3e2015-04-22 16:17:58 -0400551 BUF_MEM_free(s->init_buf);
Adam Langley95c29f32014-06-20 12:00:00 -0700552
Adam Langleyfcf25832014-12-18 17:42:32 -0800553 /* add extra stuff */
David Benjamin2755a3e2015-04-22 16:17:58 -0400554 ssl_cipher_preference_list_free(s->cipher_list);
555 sk_SSL_CIPHER_free(s->cipher_list_by_id);
Adam Langley95c29f32014-06-20 12:00:00 -0700556
David Benjamin2755a3e2015-04-22 16:17:58 -0400557 ssl_clear_bad_session(s);
558 SSL_SESSION_free(s->session);
Adam Langley95c29f32014-06-20 12:00:00 -0700559
Adam Langleyfcf25832014-12-18 17:42:32 -0800560 ssl_clear_cipher_ctx(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700561
David Benjamin2755a3e2015-04-22 16:17:58 -0400562 ssl_cert_free(s->cert);
Adam Langley0289c732014-06-20 12:00:00 -0700563
David Benjamin2755a3e2015-04-22 16:17:58 -0400564 OPENSSL_free(s->tlsext_hostname);
565 SSL_CTX_free(s->initial_ctx);
566 OPENSSL_free(s->tlsext_ecpointformatlist);
567 OPENSSL_free(s->tlsext_ellipticcurvelist);
568 OPENSSL_free(s->alpn_client_proto_list);
569 EVP_PKEY_free(s->tlsext_channel_id_private);
570 OPENSSL_free(s->psk_identity_hint);
571 sk_X509_NAME_pop_free(s->client_CA, X509_NAME_free);
572 OPENSSL_free(s->next_proto_negotiated);
573 sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);
Adam Langley95c29f32014-06-20 12:00:00 -0700574
Adam Langleyfcf25832014-12-18 17:42:32 -0800575 if (s->method != NULL) {
576 s->method->ssl_free(s);
577 }
David Benjamin2755a3e2015-04-22 16:17:58 -0400578 SSL_CTX_free(s->ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700579
Adam Langleyfcf25832014-12-18 17:42:32 -0800580 OPENSSL_free(s);
581}
Adam Langley95c29f32014-06-20 12:00:00 -0700582
Adam Langleyfcf25832014-12-18 17:42:32 -0800583void SSL_set_bio(SSL *s, BIO *rbio, BIO *wbio) {
584 /* If the output buffering BIO is still in place, remove it. */
585 if (s->bbio != NULL) {
586 if (s->wbio == s->bbio) {
587 s->wbio = s->wbio->next_bio;
588 s->bbio->next_bio = NULL;
589 }
590 }
Adam Langley95c29f32014-06-20 12:00:00 -0700591
David Benjamin2755a3e2015-04-22 16:17:58 -0400592 if (s->rbio != rbio) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800593 BIO_free_all(s->rbio);
594 }
David Benjamin2755a3e2015-04-22 16:17:58 -0400595 if (s->wbio != wbio && s->rbio != s->wbio) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800596 BIO_free_all(s->wbio);
597 }
598 s->rbio = rbio;
599 s->wbio = wbio;
600}
Adam Langley95c29f32014-06-20 12:00:00 -0700601
Adam Langleyfcf25832014-12-18 17:42:32 -0800602BIO *SSL_get_rbio(const SSL *s) { return s->rbio; }
Adam Langley95c29f32014-06-20 12:00:00 -0700603
Adam Langleyfcf25832014-12-18 17:42:32 -0800604BIO *SSL_get_wbio(const SSL *s) { return s->wbio; }
Adam Langley95c29f32014-06-20 12:00:00 -0700605
Adam Langleyfcf25832014-12-18 17:42:32 -0800606int SSL_get_fd(const SSL *s) { return SSL_get_rfd(s); }
Adam Langley95c29f32014-06-20 12:00:00 -0700607
Adam Langleyfcf25832014-12-18 17:42:32 -0800608int SSL_get_rfd(const SSL *s) {
609 int ret = -1;
610 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700611
Adam Langleyfcf25832014-12-18 17:42:32 -0800612 b = SSL_get_rbio(s);
613 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
614 if (r != NULL) {
615 BIO_get_fd(r, &ret);
616 }
617 return ret;
618}
Adam Langley95c29f32014-06-20 12:00:00 -0700619
Adam Langleyfcf25832014-12-18 17:42:32 -0800620int SSL_get_wfd(const SSL *s) {
621 int ret = -1;
622 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700623
Adam Langleyfcf25832014-12-18 17:42:32 -0800624 b = SSL_get_wbio(s);
625 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
626 if (r != NULL) {
627 BIO_get_fd(r, &ret);
628 }
Adam Langley95c29f32014-06-20 12:00:00 -0700629
Adam Langleyfcf25832014-12-18 17:42:32 -0800630 return ret;
631}
Adam Langley95c29f32014-06-20 12:00:00 -0700632
Adam Langleyfcf25832014-12-18 17:42:32 -0800633int SSL_set_fd(SSL *s, int fd) {
634 int ret = 0;
635 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700636
Adam Langleyfcf25832014-12-18 17:42:32 -0800637 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700638
Adam Langleyfcf25832014-12-18 17:42:32 -0800639 if (bio == NULL) {
640 OPENSSL_PUT_ERROR(SSL, SSL_set_fd, ERR_R_BUF_LIB);
641 goto err;
642 }
643 BIO_set_fd(bio, fd, BIO_NOCLOSE);
644 SSL_set_bio(s, bio, bio);
645 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700646
Adam Langley95c29f32014-06-20 12:00:00 -0700647err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800648 return ret;
649}
Adam Langley95c29f32014-06-20 12:00:00 -0700650
Adam Langleyfcf25832014-12-18 17:42:32 -0800651int SSL_set_wfd(SSL *s, int fd) {
652 int ret = 0;
653 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700654
Adam Langleyfcf25832014-12-18 17:42:32 -0800655 if (s->rbio == NULL || BIO_method_type(s->rbio) != BIO_TYPE_FD ||
656 (int)BIO_get_fd(s->rbio, NULL) != fd) {
657 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700658
Adam Langleyfcf25832014-12-18 17:42:32 -0800659 if (bio == NULL) {
660 OPENSSL_PUT_ERROR(SSL, SSL_set_wfd, ERR_R_BUF_LIB);
661 goto err;
662 }
663 BIO_set_fd(bio, fd, BIO_NOCLOSE);
664 SSL_set_bio(s, SSL_get_rbio(s), bio);
665 } else {
666 SSL_set_bio(s, SSL_get_rbio(s), SSL_get_rbio(s));
667 }
668
669 ret = 1;
670
Adam Langley95c29f32014-06-20 12:00:00 -0700671err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800672 return ret;
673}
Adam Langley95c29f32014-06-20 12:00:00 -0700674
Adam Langleyfcf25832014-12-18 17:42:32 -0800675int SSL_set_rfd(SSL *s, int fd) {
676 int ret = 0;
677 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700678
Adam Langleyfcf25832014-12-18 17:42:32 -0800679 if (s->wbio == NULL || BIO_method_type(s->wbio) != BIO_TYPE_FD ||
680 (int)BIO_get_fd(s->wbio, NULL) != fd) {
681 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700682
Adam Langleyfcf25832014-12-18 17:42:32 -0800683 if (bio == NULL) {
684 OPENSSL_PUT_ERROR(SSL, SSL_set_rfd, ERR_R_BUF_LIB);
685 goto err;
686 }
687 BIO_set_fd(bio, fd, BIO_NOCLOSE);
688 SSL_set_bio(s, bio, SSL_get_wbio(s));
689 } else {
690 SSL_set_bio(s, SSL_get_wbio(s), SSL_get_wbio(s));
691 }
692 ret = 1;
693
Adam Langley95c29f32014-06-20 12:00:00 -0700694err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800695 return ret;
696}
Adam Langley95c29f32014-06-20 12:00:00 -0700697
698/* return length of latest Finished message we sent, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -0800699size_t SSL_get_finished(const SSL *s, void *buf, size_t count) {
700 size_t ret = 0;
701
702 if (s->s3 != NULL) {
703 ret = s->s3->tmp.finish_md_len;
704 if (count > ret) {
705 count = ret;
706 }
707 memcpy(buf, s->s3->tmp.finish_md, count);
708 }
709
710 return ret;
711}
Adam Langley95c29f32014-06-20 12:00:00 -0700712
713/* return length of latest Finished message we expected, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -0800714size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count) {
715 size_t ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700716
Adam Langleyfcf25832014-12-18 17:42:32 -0800717 if (s->s3 != NULL) {
718 ret = s->s3->tmp.peer_finish_md_len;
719 if (count > ret) {
720 count = ret;
721 }
722 memcpy(buf, s->s3->tmp.peer_finish_md, count);
723 }
Adam Langley95c29f32014-06-20 12:00:00 -0700724
Adam Langleyfcf25832014-12-18 17:42:32 -0800725 return ret;
726}
Adam Langley95c29f32014-06-20 12:00:00 -0700727
Adam Langleyfcf25832014-12-18 17:42:32 -0800728int SSL_get_verify_mode(const SSL *s) { return s->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -0700729
Adam Langleyfcf25832014-12-18 17:42:32 -0800730int SSL_get_verify_depth(const SSL *s) {
731 return X509_VERIFY_PARAM_get_depth(s->param);
732}
Adam Langley95c29f32014-06-20 12:00:00 -0700733
Adam Langleyfcf25832014-12-18 17:42:32 -0800734int (*SSL_get_verify_callback(const SSL *s))(int, X509_STORE_CTX *) {
735 return s->verify_callback;
736}
Adam Langley95c29f32014-06-20 12:00:00 -0700737
Adam Langleyfcf25832014-12-18 17:42:32 -0800738int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) { return ctx->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -0700739
Adam Langleyfcf25832014-12-18 17:42:32 -0800740int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) {
741 return X509_VERIFY_PARAM_get_depth(ctx->param);
742}
Adam Langley95c29f32014-06-20 12:00:00 -0700743
Adam Langleyfcf25832014-12-18 17:42:32 -0800744int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int, X509_STORE_CTX *) {
745 return ctx->default_verify_callback;
746}
Adam Langley95c29f32014-06-20 12:00:00 -0700747
Adam Langleyfcf25832014-12-18 17:42:32 -0800748void SSL_set_verify(SSL *s, int mode,
749 int (*callback)(int ok, X509_STORE_CTX *ctx)) {
750 s->verify_mode = mode;
751 if (callback != NULL) {
752 s->verify_callback = callback;
753 }
754}
Adam Langley95c29f32014-06-20 12:00:00 -0700755
Adam Langleyfcf25832014-12-18 17:42:32 -0800756void SSL_set_verify_depth(SSL *s, int depth) {
757 X509_VERIFY_PARAM_set_depth(s->param, depth);
758}
Adam Langley95c29f32014-06-20 12:00:00 -0700759
David Benjamin61ecccf2015-05-05 09:44:51 -0400760int SSL_CTX_get_read_ahead(const SSL_CTX *ctx) { return ctx->read_ahead; }
Adam Langley95c29f32014-06-20 12:00:00 -0700761
Adam Langleyfcf25832014-12-18 17:42:32 -0800762int SSL_get_read_ahead(const SSL *s) { return s->read_ahead; }
Adam Langley95c29f32014-06-20 12:00:00 -0700763
David Benjamin61ecccf2015-05-05 09:44:51 -0400764void SSL_CTX_set_read_ahead(SSL_CTX *ctx, int yes) { ctx->read_ahead = !!yes; }
765
766void SSL_set_read_ahead(SSL *s, int yes) { s->read_ahead = !!yes; }
767
Adam Langleyfcf25832014-12-18 17:42:32 -0800768int SSL_pending(const SSL *s) {
769 /* SSL_pending cannot work properly if read-ahead is enabled
770 * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)), and it is
771 * impossible to fix since SSL_pending cannot report errors that may be
772 * observed while scanning the new data. (Note that SSL_pending() is often
773 * used as a boolean value, so we'd better not return -1.). */
774 return s->method->ssl_pending(s);
775}
Adam Langley95c29f32014-06-20 12:00:00 -0700776
Adam Langleyfcf25832014-12-18 17:42:32 -0800777X509 *SSL_get_peer_certificate(const SSL *s) {
778 X509 *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700779
Adam Langleyfcf25832014-12-18 17:42:32 -0800780 if (s == NULL || s->session == NULL) {
781 r = NULL;
782 } else {
783 r = s->session->peer;
784 }
Adam Langley95c29f32014-06-20 12:00:00 -0700785
Adam Langleyfcf25832014-12-18 17:42:32 -0800786 if (r == NULL) {
787 return NULL;
788 }
Adam Langley95c29f32014-06-20 12:00:00 -0700789
Adam Langleyfcf25832014-12-18 17:42:32 -0800790 return X509_up_ref(r);
791}
792
793STACK_OF(X509) * SSL_get_peer_cert_chain(const SSL *s) {
794 STACK_OF(X509) * r;
795
796 if (s == NULL || s->session == NULL || s->session->sess_cert == NULL) {
797 r = NULL;
798 } else {
799 r = s->session->sess_cert->cert_chain;
800 }
801
802 /* If we are a client, cert_chain includes the peer's own certificate; if we
803 * are a server, it does not. */
804 return r;
805}
Adam Langley95c29f32014-06-20 12:00:00 -0700806
Adam Langley95c29f32014-06-20 12:00:00 -0700807/* Fix this so it checks all the valid key/cert options */
Adam Langleyfcf25832014-12-18 17:42:32 -0800808int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
809 if (ctx == NULL || ctx->cert == NULL || ctx->cert->key->x509 == NULL) {
810 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key,
811 SSL_R_NO_CERTIFICATE_ASSIGNED);
812 return 0;
813 }
814
815 if (ctx->cert->key->privatekey == NULL) {
816 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key,
817 SSL_R_NO_PRIVATE_KEY_ASSIGNED);
818 return 0;
819 }
820
821 return X509_check_private_key(ctx->cert->key->x509,
822 ctx->cert->key->privatekey);
823}
Adam Langley95c29f32014-06-20 12:00:00 -0700824
825/* Fix this function so that it takes an optional type parameter */
Adam Langleyfcf25832014-12-18 17:42:32 -0800826int SSL_check_private_key(const SSL *ssl) {
827 if (ssl == NULL) {
828 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, ERR_R_PASSED_NULL_PARAMETER);
829 return 0;
830 }
Adam Langley95c29f32014-06-20 12:00:00 -0700831
Adam Langleyfcf25832014-12-18 17:42:32 -0800832 if (ssl->cert == NULL) {
833 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
834 SSL_R_NO_CERTIFICATE_ASSIGNED);
835 return 0;
836 }
Adam Langley95c29f32014-06-20 12:00:00 -0700837
Adam Langleyfcf25832014-12-18 17:42:32 -0800838 if (ssl->cert->key->x509 == NULL) {
839 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
840 SSL_R_NO_CERTIFICATE_ASSIGNED);
841 return 0;
842 }
David Benjamin0b145c22014-11-26 20:10:09 -0500843
Adam Langleyfcf25832014-12-18 17:42:32 -0800844 if (ssl->cert->key->privatekey == NULL) {
845 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
846 SSL_R_NO_PRIVATE_KEY_ASSIGNED);
847 return 0;
848 }
Adam Langley95c29f32014-06-20 12:00:00 -0700849
Adam Langleyfcf25832014-12-18 17:42:32 -0800850 return X509_check_private_key(ssl->cert->key->x509,
851 ssl->cert->key->privatekey);
852}
Adam Langley95c29f32014-06-20 12:00:00 -0700853
Adam Langleyfcf25832014-12-18 17:42:32 -0800854int SSL_accept(SSL *s) {
855 if (s->handshake_func == 0) {
856 /* Not properly initialized yet */
857 SSL_set_accept_state(s);
858 }
David Benjamin0b145c22014-11-26 20:10:09 -0500859
Adam Langleyfcf25832014-12-18 17:42:32 -0800860 if (s->handshake_func != s->method->ssl_accept) {
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +0100861 OPENSSL_PUT_ERROR(SSL, SSL_accept, ERR_R_INTERNAL_ERROR);
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 return s->handshake_func(s);
866}
Adam Langley95c29f32014-06-20 12:00:00 -0700867
Adam Langleyfcf25832014-12-18 17:42:32 -0800868int SSL_connect(SSL *s) {
869 if (s->handshake_func == 0) {
870 /* Not properly initialized yet */
871 SSL_set_connect_state(s);
872 }
Adam Langley95c29f32014-06-20 12:00:00 -0700873
Adam Langleyfcf25832014-12-18 17:42:32 -0800874 if (s->handshake_func != s->method->ssl_connect) {
875 OPENSSL_PUT_ERROR(SSL, SSL_connect, ERR_R_INTERNAL_ERROR);
876 return -1;
877 }
Adam Langley95c29f32014-06-20 12:00:00 -0700878
Adam Langleyfcf25832014-12-18 17:42:32 -0800879 return s->handshake_func(s);
880}
Adam Langley95c29f32014-06-20 12:00:00 -0700881
Adam Langleyfcf25832014-12-18 17:42:32 -0800882long SSL_get_default_timeout(const SSL *s) {
883 return SSL_DEFAULT_SESSION_TIMEOUT;
884}
Adam Langley95c29f32014-06-20 12:00:00 -0700885
Adam Langleyfcf25832014-12-18 17:42:32 -0800886int SSL_read(SSL *s, void *buf, int num) {
887 if (s->handshake_func == 0) {
888 OPENSSL_PUT_ERROR(SSL, SSL_read, SSL_R_UNINITIALIZED);
889 return -1;
890 }
Adam Langley95c29f32014-06-20 12:00:00 -0700891
Adam Langleyfcf25832014-12-18 17:42:32 -0800892 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
893 s->rwstate = SSL_NOTHING;
894 return 0;
895 }
Adam Langley95c29f32014-06-20 12:00:00 -0700896
Adam Langleyfcf25832014-12-18 17:42:32 -0800897 return s->method->ssl_read(s, buf, num);
898}
Adam Langley95c29f32014-06-20 12:00:00 -0700899
Adam Langleyfcf25832014-12-18 17:42:32 -0800900int SSL_peek(SSL *s, void *buf, int num) {
901 if (s->handshake_func == 0) {
902 OPENSSL_PUT_ERROR(SSL, SSL_peek, SSL_R_UNINITIALIZED);
903 return -1;
904 }
Adam Langley95c29f32014-06-20 12:00:00 -0700905
Adam Langleyfcf25832014-12-18 17:42:32 -0800906 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
907 return 0;
908 }
Adam Langley95c29f32014-06-20 12:00:00 -0700909
Adam Langleyfcf25832014-12-18 17:42:32 -0800910 return s->method->ssl_peek(s, buf, num);
911}
Adam Langley95c29f32014-06-20 12:00:00 -0700912
Adam Langleyfcf25832014-12-18 17:42:32 -0800913int SSL_write(SSL *s, const void *buf, int num) {
914 if (s->handshake_func == 0) {
915 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_UNINITIALIZED);
916 return -1;
917 }
Adam Langley95c29f32014-06-20 12:00:00 -0700918
Adam Langleyfcf25832014-12-18 17:42:32 -0800919 if (s->shutdown & SSL_SENT_SHUTDOWN) {
920 s->rwstate = SSL_NOTHING;
921 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_PROTOCOL_IS_SHUTDOWN);
922 return -1;
923 }
Adam Langley95c29f32014-06-20 12:00:00 -0700924
Adam Langleyfcf25832014-12-18 17:42:32 -0800925 return s->method->ssl_write(s, buf, num);
926}
Adam Langley95c29f32014-06-20 12:00:00 -0700927
Adam Langleyfcf25832014-12-18 17:42:32 -0800928int SSL_shutdown(SSL *s) {
929 /* Note that this function behaves differently from what one might expect.
930 * Return values are 0 for no success (yet), 1 for success; but calling it
931 * once is usually not enough, even if blocking I/O is used (see
932 * ssl3_shutdown). */
Adam Langley95c29f32014-06-20 12:00:00 -0700933
Adam Langleyfcf25832014-12-18 17:42:32 -0800934 if (s->handshake_func == 0) {
935 OPENSSL_PUT_ERROR(SSL, SSL_shutdown, SSL_R_UNINITIALIZED);
936 return -1;
937 }
Adam Langley95c29f32014-06-20 12:00:00 -0700938
Adam Langleyfcf25832014-12-18 17:42:32 -0800939 if (!SSL_in_init(s)) {
940 return s->method->ssl_shutdown(s);
941 }
Adam Langley95c29f32014-06-20 12:00:00 -0700942
Adam Langleyfcf25832014-12-18 17:42:32 -0800943 return 1;
944}
Adam Langley95c29f32014-06-20 12:00:00 -0700945
Adam Langleyfcf25832014-12-18 17:42:32 -0800946int SSL_renegotiate(SSL *s) {
David Benjamin2cdace92015-01-24 13:30:02 -0500947 if (SSL_IS_DTLS(s)) {
948 /* Renegotiation is not supported for DTLS. */
949 OPENSSL_PUT_ERROR(SSL, SSL_renegotiate, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
950 return 0;
951 }
952
Adam Langleyfcf25832014-12-18 17:42:32 -0800953 if (s->renegotiate == 0) {
954 s->renegotiate = 1;
955 }
Adam Langley95c29f32014-06-20 12:00:00 -0700956
Adam Langleyfcf25832014-12-18 17:42:32 -0800957 s->new_session = 1;
958 return s->method->ssl_renegotiate(s);
959}
Adam Langley95c29f32014-06-20 12:00:00 -0700960
Adam Langleyfcf25832014-12-18 17:42:32 -0800961int SSL_renegotiate_pending(SSL *s) {
962 /* becomes true when negotiation is requested; false again once a handshake
963 * has finished */
964 return s->renegotiate != 0;
965}
Adam Langley95c29f32014-06-20 12:00:00 -0700966
David Benjamin61ecccf2015-05-05 09:44:51 -0400967uint32_t SSL_CTX_set_options(SSL_CTX *ctx, uint32_t options) {
968 ctx->options |= options;
969 return ctx->options;
970}
971
972uint32_t SSL_set_options(SSL *ssl, uint32_t options) {
973 ssl->options |= options;
974 return ssl->options;
975}
976
977uint32_t SSL_CTX_clear_options(SSL_CTX *ctx, uint32_t options) {
978 ctx->options &= ~options;
979 return ctx->options;
980}
981
982uint32_t SSL_clear_options(SSL *ssl, uint32_t options) {
983 ssl->options &= ~options;
984 return ssl->options;
985}
986
987uint32_t SSL_CTX_get_options(const SSL_CTX *ctx) { return ctx->options; }
988
989uint32_t SSL_get_options(const SSL *ssl) { return ssl->options; }
990
991uint32_t SSL_CTX_set_mode(SSL_CTX *ctx, uint32_t mode) {
992 ctx->mode |= mode;
993 return ctx->mode;
994}
995
996uint32_t SSL_set_mode(SSL *ssl, uint32_t mode) {
997 ssl->mode |= mode;
998 return ssl->mode;
999}
1000
1001uint32_t SSL_CTX_clear_mode(SSL_CTX *ctx, uint32_t mode) {
1002 ctx->mode &= ~mode;
1003 return ctx->mode;
1004}
1005
1006uint32_t SSL_clear_mode(SSL *ssl, uint32_t mode) {
1007 ssl->mode &= ~mode;
1008 return ssl->mode;
1009}
1010
1011uint32_t SSL_CTX_get_mode(const SSL_CTX *ctx) { return ctx->mode; }
1012
1013uint32_t SSL_get_mode(const SSL *ssl) { return ssl->mode; }
1014
David Benjamin1d0a1942015-04-26 15:35:35 -04001015size_t SSL_CTX_get_max_cert_list(const SSL_CTX *ctx) {
1016 return ctx->max_cert_list;
1017}
1018
1019void SSL_CTX_set_max_cert_list(SSL_CTX *ctx, size_t max_cert_list) {
1020 if (max_cert_list > kMaxHandshakeSize) {
1021 max_cert_list = kMaxHandshakeSize;
1022 }
1023 ctx->max_cert_list = (uint32_t)max_cert_list;
1024}
1025
1026size_t SSL_get_max_cert_list(const SSL *ssl) {
1027 return ssl->max_cert_list;
1028}
1029
1030void SSL_set_max_cert_list(SSL *ssl, size_t max_cert_list) {
1031 if (max_cert_list > kMaxHandshakeSize) {
1032 max_cert_list = kMaxHandshakeSize;
1033 }
1034 ssl->max_cert_list = (uint32_t)max_cert_list;
1035}
1036
1037void SSL_CTX_set_max_send_fragment(SSL_CTX *ctx, size_t max_send_fragment) {
1038 if (max_send_fragment < 512) {
1039 max_send_fragment = 512;
1040 }
1041 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1042 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1043 }
1044 ctx->max_send_fragment = (uint16_t)max_send_fragment;
1045}
1046
1047void SSL_set_max_send_fragment(SSL *ssl, size_t max_send_fragment) {
1048 if (max_send_fragment < 512) {
1049 max_send_fragment = 512;
1050 }
1051 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1052 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1053 }
1054 ssl->max_send_fragment = (uint16_t)max_send_fragment;
1055}
1056
David Benjamincb9cf792015-05-05 09:46:14 -04001057int SSL_set_mtu(SSL *ssl, unsigned mtu) {
1058 if (!SSL_IS_DTLS(ssl) || mtu < dtls1_min_mtu()) {
1059 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001060 }
David Benjamincb9cf792015-05-05 09:46:14 -04001061 ssl->d1->mtu = mtu;
1062 return 1;
1063}
1064
1065int SSL_get_secure_renegotiation_support(const SSL *ssl) {
1066 return ssl->s3->send_connection_binding;
1067}
1068
1069long SSL_ctrl(SSL *s, int cmd, long larg, void *parg) {
1070 return s->method->ssl_ctrl(s, cmd, larg, parg);
Adam Langleyfcf25832014-12-18 17:42:32 -08001071}
1072
Adam Langleyfcf25832014-12-18 17:42:32 -08001073LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) { return ctx->sessions; }
1074
1075long SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg) {
1076 long l;
1077
1078 switch (cmd) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001079 case SSL_CTRL_SET_SESS_CACHE_SIZE:
1080 l = ctx->session_cache_size;
1081 ctx->session_cache_size = larg;
1082 return l;
1083
1084 case SSL_CTRL_GET_SESS_CACHE_SIZE:
1085 return ctx->session_cache_size;
1086
1087 case SSL_CTRL_SET_SESS_CACHE_MODE:
1088 l = ctx->session_cache_mode;
1089 ctx->session_cache_mode = larg;
1090 return l;
1091
1092 case SSL_CTRL_GET_SESS_CACHE_MODE:
1093 return ctx->session_cache_mode;
1094
1095 case SSL_CTRL_SESS_NUMBER:
1096 return lh_SSL_SESSION_num_items(ctx->sessions);
1097
Adam Langleyfcf25832014-12-18 17:42:32 -08001098 default:
1099 return ctx->method->ssl_ctx_ctrl(ctx, cmd, larg, parg);
1100 }
1101}
1102
Adam Langleyfcf25832014-12-18 17:42:32 -08001103int ssl_cipher_id_cmp(const void *in_a, const void *in_b) {
1104 long l;
1105 const SSL_CIPHER *a = in_a;
1106 const SSL_CIPHER *b = in_b;
1107 const long a_id = a->id;
1108 const long b_id = b->id;
1109
1110 l = a_id - b_id;
1111 if (l == 0L) {
1112 return 0;
1113 } else {
1114 return (l > 0) ? 1 : -1;
1115 }
1116}
1117
1118int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **ap, const SSL_CIPHER **bp) {
1119 long l;
1120 const long a_id = (*ap)->id;
1121 const long b_id = (*bp)->id;
1122
1123 l = a_id - b_id;
1124 if (l == 0) {
1125 return 0;
1126 } else {
1127 return (l > 0) ? 1 : -1;
1128 }
1129}
1130
1131/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001132 * preference */
Adam Langleyfcf25832014-12-18 17:42:32 -08001133STACK_OF(SSL_CIPHER) * SSL_get_ciphers(const SSL *s) {
1134 if (s == NULL) {
1135 return NULL;
1136 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001137
Adam Langleyfcf25832014-12-18 17:42:32 -08001138 if (s->cipher_list != NULL) {
1139 return s->cipher_list->ciphers;
1140 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001141
Adam Langleyfcf25832014-12-18 17:42:32 -08001142 if (s->version >= TLS1_1_VERSION && s->ctx != NULL &&
1143 s->ctx->cipher_list_tls11 != NULL) {
1144 return s->ctx->cipher_list_tls11->ciphers;
1145 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001146
Adam Langleyfcf25832014-12-18 17:42:32 -08001147 if (s->ctx != NULL && s->ctx->cipher_list != NULL) {
1148 return s->ctx->cipher_list->ciphers;
1149 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001150
Adam Langleyfcf25832014-12-18 17:42:32 -08001151 return NULL;
1152}
Adam Langley95c29f32014-06-20 12:00:00 -07001153
Adam Langleyfcf25832014-12-18 17:42:32 -08001154/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001155 * algorithm id */
Adam Langleyfcf25832014-12-18 17:42:32 -08001156STACK_OF(SSL_CIPHER) * ssl_get_ciphers_by_id(SSL *s) {
1157 if (s == NULL) {
1158 return NULL;
1159 }
Adam Langley95c29f32014-06-20 12:00:00 -07001160
Adam Langleyfcf25832014-12-18 17:42:32 -08001161 if (s->cipher_list_by_id != NULL) {
1162 return s->cipher_list_by_id;
1163 }
Adam Langley95c29f32014-06-20 12:00:00 -07001164
Adam Langleyfcf25832014-12-18 17:42:32 -08001165 if (s->ctx != NULL && s->ctx->cipher_list_by_id != NULL) {
1166 return s->ctx->cipher_list_by_id;
1167 }
Adam Langley95c29f32014-06-20 12:00:00 -07001168
Adam Langleyfcf25832014-12-18 17:42:32 -08001169 return NULL;
1170}
Adam Langley95c29f32014-06-20 12:00:00 -07001171
Adam Langleyfcf25832014-12-18 17:42:32 -08001172/* The old interface to get the same thing as SSL_get_ciphers() */
1173const char *SSL_get_cipher_list(const SSL *s, int n) {
1174 const SSL_CIPHER *c;
1175 STACK_OF(SSL_CIPHER) * sk;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001176
Adam Langleyfcf25832014-12-18 17:42:32 -08001177 if (s == NULL) {
1178 return NULL;
1179 }
Adam Langley95c29f32014-06-20 12:00:00 -07001180
Adam Langleyfcf25832014-12-18 17:42:32 -08001181 sk = SSL_get_ciphers(s);
1182 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
1183 return NULL;
1184 }
Adam Langley95c29f32014-06-20 12:00:00 -07001185
Adam Langleyfcf25832014-12-18 17:42:32 -08001186 c = sk_SSL_CIPHER_value(sk, n);
1187 if (c == NULL) {
1188 return NULL;
1189 }
Adam Langley95c29f32014-06-20 12:00:00 -07001190
Adam Langleyfcf25832014-12-18 17:42:32 -08001191 return c->name;
1192}
David Benjamin5491e3f2014-09-29 19:33:09 -04001193
Adam Langleyfcf25832014-12-18 17:42:32 -08001194/* specify the ciphers to be used by default by the SSL_CTX */
1195int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
1196 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001197
Adam Langleyfcf25832014-12-18 17:42:32 -08001198 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001199 &ctx->cipher_list_by_id, str);
Adam Langleyfcf25832014-12-18 17:42:32 -08001200 /* ssl_create_cipher_list may return an empty stack if it was unable to find
1201 * a cipher matching the given rule string (for example if the rule string
1202 * specifies a cipher which has been disabled). This is not an error as far
1203 * as ssl_create_cipher_list is concerned, and hence ctx->cipher_list and
1204 * ctx->cipher_list_by_id has been updated. */
1205 if (sk == NULL) {
1206 return 0;
1207 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1208 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1209 return 0;
1210 }
Adam Langley95c29f32014-06-20 12:00:00 -07001211
Adam Langleyfcf25832014-12-18 17:42:32 -08001212 return 1;
1213}
David Benjamin39482a12014-07-20 13:30:15 -04001214
Adam Langleyfcf25832014-12-18 17:42:32 -08001215int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str) {
1216 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001217
David Benjamin71f07942015-04-08 02:36:59 -04001218 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str);
Adam Langleyfcf25832014-12-18 17:42:32 -08001219 if (sk == NULL) {
1220 return 0;
1221 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1222 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list_tls11,
1223 SSL_R_NO_CIPHER_MATCH);
1224 return 0;
1225 }
David Benjamin9f2c0d72014-10-21 22:00:19 -04001226
Adam Langleyfcf25832014-12-18 17:42:32 -08001227 return 1;
1228}
Adam Langley95c29f32014-06-20 12:00:00 -07001229
Adam Langleyfcf25832014-12-18 17:42:32 -08001230/* specify the ciphers to be used by the SSL */
1231int SSL_set_cipher_list(SSL *s, const char *str) {
1232 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001233
Adam Langleyfcf25832014-12-18 17:42:32 -08001234 sk = ssl_create_cipher_list(s->ctx->method, &s->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001235 &s->cipher_list_by_id, str);
David Benjamin39482a12014-07-20 13:30:15 -04001236
Adam Langleyfcf25832014-12-18 17:42:32 -08001237 /* see comment in SSL_CTX_set_cipher_list */
1238 if (sk == NULL) {
1239 return 0;
1240 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1241 OPENSSL_PUT_ERROR(SSL, SSL_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1242 return 0;
1243 }
David Benjamin39482a12014-07-20 13:30:15 -04001244
Adam Langleyfcf25832014-12-18 17:42:32 -08001245 return 1;
1246}
Adam Langley95c29f32014-06-20 12:00:00 -07001247
Adam Langleyfcf25832014-12-18 17:42:32 -08001248int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, uint8_t *p) {
1249 size_t i;
1250 const SSL_CIPHER *c;
1251 CERT *ct = s->cert;
1252 uint8_t *q;
1253 /* Set disabled masks for this session */
1254 ssl_set_client_disabled(s);
Adam Langley29707792014-06-20 12:00:00 -07001255
Adam Langleyfcf25832014-12-18 17:42:32 -08001256 if (sk == NULL) {
1257 return 0;
1258 }
1259 q = p;
Adam Langley95c29f32014-06-20 12:00:00 -07001260
Adam Langleyfcf25832014-12-18 17:42:32 -08001261 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
1262 c = sk_SSL_CIPHER_value(sk, i);
1263 /* Skip disabled ciphers */
1264 if (c->algorithm_ssl & ct->mask_ssl ||
1265 c->algorithm_mkey & ct->mask_k ||
1266 c->algorithm_auth & ct->mask_a) {
1267 continue;
1268 }
1269 s2n(ssl3_get_cipher_value(c), p);
1270 }
1271
1272 /* If all ciphers were disabled, return the error to the caller. */
1273 if (p == q) {
1274 return 0;
1275 }
1276
1277 /* Add SCSVs. */
1278 if (!s->renegotiate) {
1279 s2n(SSL3_CK_SCSV & 0xffff, p);
1280 }
1281
Adam Langley5f0efe02015-02-20 13:03:16 -08001282 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001283 s2n(SSL3_CK_FALLBACK_SCSV & 0xffff, p);
1284 }
1285
1286 return p - q;
1287}
1288
1289STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs) {
1290 CBS cipher_suites = *cbs;
1291 const SSL_CIPHER *c;
1292 STACK_OF(SSL_CIPHER) * sk;
1293
1294 if (s->s3) {
1295 s->s3->send_connection_binding = 0;
1296 }
1297
1298 if (CBS_len(&cipher_suites) % 2 != 0) {
1299 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1300 SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
1301 return NULL;
1302 }
1303
1304 sk = sk_SSL_CIPHER_new_null();
1305 if (sk == NULL) {
1306 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1307 goto err;
1308 }
1309
1310 if (!CBS_stow(&cipher_suites, &s->cert->ciphers_raw,
1311 &s->cert->ciphers_rawlen)) {
1312 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1313 goto err;
1314 }
1315
1316 while (CBS_len(&cipher_suites) > 0) {
1317 uint16_t cipher_suite;
1318
1319 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
1320 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_INTERNAL_ERROR);
1321 goto err;
1322 }
1323
1324 /* Check for SCSV. */
1325 if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff)) {
1326 /* SCSV is fatal if renegotiating. */
1327 if (s->renegotiate) {
1328 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1329 SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1330 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1331 goto err;
1332 }
1333 s->s3->send_connection_binding = 1;
1334 continue;
1335 }
1336
1337 /* Check for FALLBACK_SCSV. */
1338 if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff)) {
1339 uint16_t max_version = ssl3_get_max_server_version(s);
1340 if (SSL_IS_DTLS(s) ? (uint16_t)s->version > max_version
1341 : (uint16_t)s->version < max_version) {
1342 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1343 SSL_R_INAPPROPRIATE_FALLBACK);
1344 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
1345 goto err;
1346 }
1347 continue;
1348 }
1349
1350 c = ssl3_get_cipher_by_value(cipher_suite);
1351 if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
1352 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1353 goto err;
1354 }
1355 }
1356
1357 return sk;
David Benjamin9f2c0d72014-10-21 22:00:19 -04001358
Adam Langley95c29f32014-06-20 12:00:00 -07001359err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001360 sk_SSL_CIPHER_free(sk);
Adam Langleyfcf25832014-12-18 17:42:32 -08001361 return NULL;
1362}
Adam Langley95c29f32014-06-20 12:00:00 -07001363
1364
Adam Langleyfcf25832014-12-18 17:42:32 -08001365/* return a servername extension value if provided in Client Hello, or NULL. So
1366 * far, only host_name types are defined (RFC 3546). */
1367const char *SSL_get_servername(const SSL *s, const int type) {
1368 if (type != TLSEXT_NAMETYPE_host_name) {
1369 return NULL;
1370 }
Adam Langley95c29f32014-06-20 12:00:00 -07001371
Adam Langleyfcf25832014-12-18 17:42:32 -08001372 return s->session && !s->tlsext_hostname ? s->session->tlsext_hostname
1373 : s->tlsext_hostname;
1374}
Adam Langley95c29f32014-06-20 12:00:00 -07001375
Adam Langleyfcf25832014-12-18 17:42:32 -08001376int SSL_get_servername_type(const SSL *s) {
1377 if (s->session &&
1378 (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname)) {
1379 return TLSEXT_NAMETYPE_host_name;
1380 }
Adam Langley95c29f32014-06-20 12:00:00 -07001381
Adam Langleyfcf25832014-12-18 17:42:32 -08001382 return -1;
1383}
Adam Langley95c29f32014-06-20 12:00:00 -07001384
Adam Langleyfcf25832014-12-18 17:42:32 -08001385void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
1386 ctx->signed_cert_timestamps_enabled = 1;
1387}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001388
Adam Langleyfcf25832014-12-18 17:42:32 -08001389int SSL_enable_signed_cert_timestamps(SSL *ssl) {
1390 ssl->signed_cert_timestamps_enabled = 1;
1391 return 1;
1392}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001393
Adam Langleyfcf25832014-12-18 17:42:32 -08001394void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
1395 ctx->ocsp_stapling_enabled = 1;
1396}
David Benjamin6c7aed02014-08-27 16:42:38 -04001397
Adam Langleyfcf25832014-12-18 17:42:32 -08001398int SSL_enable_ocsp_stapling(SSL *ssl) {
1399 ssl->ocsp_stapling_enabled = 1;
1400 return 1;
1401}
David Benjamin6c7aed02014-08-27 16:42:38 -04001402
Adam Langleyfcf25832014-12-18 17:42:32 -08001403void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
1404 size_t *out_len) {
1405 SSL_SESSION *session = ssl->session;
Adam Langley3cb50e02014-08-26 14:00:31 -07001406
Adam Langleyfcf25832014-12-18 17:42:32 -08001407 *out_len = 0;
1408 *out = NULL;
1409 if (ssl->server || !session || !session->tlsext_signed_cert_timestamp_list) {
1410 return;
1411 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001412
Adam Langleyfcf25832014-12-18 17:42:32 -08001413 *out = session->tlsext_signed_cert_timestamp_list;
1414 *out_len = session->tlsext_signed_cert_timestamp_list_length;
1415}
David Benjamin6c7aed02014-08-27 16:42:38 -04001416
Adam Langleyfcf25832014-12-18 17:42:32 -08001417void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
1418 size_t *out_len) {
1419 SSL_SESSION *session = ssl->session;
1420
1421 *out_len = 0;
1422 *out = NULL;
1423 if (ssl->server || !session || !session->ocsp_response) {
1424 return;
1425 }
1426 *out = session->ocsp_response;
1427 *out_len = session->ocsp_response_length;
1428}
David Benjamin6c7aed02014-08-27 16:42:38 -04001429
Adam Langley95c29f32014-06-20 12:00:00 -07001430/* SSL_select_next_proto implements the standard protocol selection. It is
1431 * expected that this function is called from the callback set by
1432 * SSL_CTX_set_next_proto_select_cb.
1433 *
1434 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1435 * strings. The length byte itself is not included in the length. A byte
1436 * string of length 0 is invalid. No byte string may be truncated.
1437 *
1438 * The current, but experimental algorithm for selecting the protocol is:
1439 *
1440 * 1) If the server doesn't support NPN then this is indicated to the
1441 * callback. In this case, the client application has to abort the connection
1442 * or have a default application level protocol.
1443 *
1444 * 2) If the server supports NPN, but advertises an empty list then the
1445 * client selects the first protcol in its list, but indicates via the
1446 * API that this fallback case was enacted.
1447 *
1448 * 3) Otherwise, the client finds the first protocol in the server's list
1449 * that it supports and selects this protocol. This is because it's
1450 * assumed that the server has better information about which protocol
1451 * a client should use.
1452 *
1453 * 4) If the client doesn't support any of the server's advertised
1454 * protocols, then this is treated the same as case 2.
1455 *
1456 * It returns either
1457 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1458 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1459 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001460int SSL_select_next_proto(uint8_t **out, uint8_t *outlen, const uint8_t *server,
1461 unsigned int server_len, const uint8_t *client,
1462 unsigned int client_len) {
1463 unsigned int i, j;
1464 const uint8_t *result;
1465 int status = OPENSSL_NPN_UNSUPPORTED;
Adam Langley95c29f32014-06-20 12:00:00 -07001466
Adam Langleyfcf25832014-12-18 17:42:32 -08001467 /* For each protocol in server preference order, see if we support it. */
1468 for (i = 0; i < server_len;) {
1469 for (j = 0; j < client_len;) {
1470 if (server[i] == client[j] &&
1471 memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
1472 /* We found a match */
1473 result = &server[i];
1474 status = OPENSSL_NPN_NEGOTIATED;
1475 goto found;
1476 }
1477 j += client[j];
1478 j++;
1479 }
1480 i += server[i];
1481 i++;
1482 }
Adam Langley95c29f32014-06-20 12:00:00 -07001483
Adam Langleyfcf25832014-12-18 17:42:32 -08001484 /* There's no overlap between our protocols and the server's list. */
1485 result = client;
1486 status = OPENSSL_NPN_NO_OVERLAP;
Adam Langley95c29f32014-06-20 12:00:00 -07001487
Adam Langleyfcf25832014-12-18 17:42:32 -08001488found:
1489 *out = (uint8_t *)result + 1;
1490 *outlen = result[0];
1491 return status;
1492}
Adam Langley95c29f32014-06-20 12:00:00 -07001493
Adam Langley95c29f32014-06-20 12:00:00 -07001494/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
1495 * requested protocol for this connection and returns 0. If the client didn't
1496 * request any protocol, then *data is set to NULL.
1497 *
1498 * Note that the client can request any protocol it chooses. The value returned
1499 * from this function need not be a member of the list of supported protocols
Adam Langleyfcf25832014-12-18 17:42:32 -08001500 * provided by the callback. */
1501void SSL_get0_next_proto_negotiated(const SSL *s, const uint8_t **data,
1502 unsigned *len) {
1503 *data = s->next_proto_negotiated;
1504 if (!*data) {
1505 *len = 0;
1506 } else {
1507 *len = s->next_proto_negotiated_len;
1508 }
Adam Langley95c29f32014-06-20 12:00:00 -07001509}
1510
1511/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
1512 * TLS server needs a list of supported protocols for Next Protocol
1513 * Negotiation. The returned list must be in wire format. The list is returned
1514 * by setting |out| to point to it and |outlen| to its length. This memory will
1515 * not be modified, but one should assume that the SSL* keeps a reference to
1516 * it.
1517 *
Adam Langleyfcf25832014-12-18 17:42:32 -08001518 * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise.
1519 * Otherwise, no such extension will be included in the ServerHello. */
1520void SSL_CTX_set_next_protos_advertised_cb(
1521 SSL_CTX *ctx,
1522 int (*cb)(SSL *ssl, const uint8_t **out, unsigned int *outlen, void *arg),
1523 void *arg) {
1524 ctx->next_protos_advertised_cb = cb;
1525 ctx->next_protos_advertised_cb_arg = arg;
1526}
Adam Langley95c29f32014-06-20 12:00:00 -07001527
1528/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1529 * client needs to select a protocol from the server's provided list. |out|
1530 * must be set to point to the selected protocol (which may be within |in|).
1531 * The length of the protocol name must be written into |outlen|. The server's
1532 * advertised protocols are provided in |in| and |inlen|. The callback can
1533 * assume that |in| is syntactically valid.
1534 *
1535 * The client must select a protocol. It is fatal to the connection if this
1536 * callback returns a value other than SSL_TLSEXT_ERR_OK.
1537 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001538void SSL_CTX_set_next_proto_select_cb(
1539 SSL_CTX *ctx, int (*cb)(SSL *s, uint8_t **out, uint8_t *outlen,
1540 const uint8_t *in, unsigned int inlen, void *arg),
1541 void *arg) {
1542 ctx->next_proto_select_cb = cb;
1543 ctx->next_proto_select_cb_arg = arg;
1544}
Adam Langley95c29f32014-06-20 12:00:00 -07001545
Adam Langleyfcf25832014-12-18 17:42:32 -08001546int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
1547 unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001548 OPENSSL_free(ctx->alpn_client_proto_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001549 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1550 if (!ctx->alpn_client_proto_list) {
1551 return 1;
1552 }
1553 ctx->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001554
Adam Langleyfcf25832014-12-18 17:42:32 -08001555 return 0;
1556}
Adam Langley95c29f32014-06-20 12:00:00 -07001557
Adam Langleyfcf25832014-12-18 17:42:32 -08001558int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001559 OPENSSL_free(ssl->alpn_client_proto_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001560 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1561 if (!ssl->alpn_client_proto_list) {
1562 return 1;
1563 }
1564 ssl->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001565
Adam Langleyfcf25832014-12-18 17:42:32 -08001566 return 0;
1567}
Adam Langley95c29f32014-06-20 12:00:00 -07001568
1569/* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
1570 * during ClientHello processing in order to select an ALPN protocol from the
1571 * client's list of offered protocols. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001572void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
1573 int (*cb)(SSL *ssl, const uint8_t **out,
1574 uint8_t *outlen, const uint8_t *in,
1575 unsigned int inlen, void *arg),
1576 void *arg) {
1577 ctx->alpn_select_cb = cb;
1578 ctx->alpn_select_cb_arg = arg;
1579}
Adam Langley95c29f32014-06-20 12:00:00 -07001580
1581/* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
1582 * On return it sets |*data| to point to |*len| bytes of protocol name (not
1583 * including the leading length-prefix byte). If the server didn't respond with
1584 * a negotiated protocol then |*len| will be zero. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001585void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **data,
1586 unsigned *len) {
1587 *data = NULL;
1588 if (ssl->s3) {
1589 *data = ssl->s3->alpn_selected;
1590 }
1591 if (*data == NULL) {
1592 *len = 0;
1593 } else {
1594 *len = ssl->s3->alpn_selected_len;
1595 }
1596}
Adam Langley95c29f32014-06-20 12:00:00 -07001597
David Benjamincfd248b2015-04-03 11:02:24 -04001598int SSL_export_keying_material(SSL *s, uint8_t *out, size_t out_len,
1599 const char *label, size_t label_len,
1600 const uint8_t *context, size_t context_len,
1601 int use_context) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001602 if (s->version < TLS1_VERSION) {
David Benjaminc565ebb2015-04-03 04:06:36 -04001603 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001604 }
Adam Langley95c29f32014-06-20 12:00:00 -07001605
David Benjamincfd248b2015-04-03 11:02:24 -04001606 return s->enc_method->export_keying_material(
1607 s, out, out_len, label, label_len, context, context_len, use_context);
Adam Langleyfcf25832014-12-18 17:42:32 -08001608}
Adam Langley95c29f32014-06-20 12:00:00 -07001609
Adam Langleyfcf25832014-12-18 17:42:32 -08001610static uint32_t ssl_session_hash(const SSL_SESSION *a) {
1611 uint32_t hash =
1612 ((uint32_t)a->session_id[0]) ||
1613 ((uint32_t)a->session_id[1] << 8) ||
1614 ((uint32_t)a->session_id[2] << 16) ||
1615 ((uint32_t)a->session_id[3] << 24);
Adam Langley95c29f32014-06-20 12:00:00 -07001616
Adam Langleyfcf25832014-12-18 17:42:32 -08001617 return hash;
1618}
Adam Langley95c29f32014-06-20 12:00:00 -07001619
1620/* NB: If this function (or indeed the hash function which uses a sort of
1621 * coarser function than this one) is changed, ensure
1622 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1623 * able to construct an SSL_SESSION that will collide with any existing session
1624 * with a matching session ID. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001625static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
1626 if (a->ssl_version != b->ssl_version) {
1627 return 1;
1628 }
Adam Langley95c29f32014-06-20 12:00:00 -07001629
Adam Langleyfcf25832014-12-18 17:42:32 -08001630 if (a->session_id_length != b->session_id_length) {
1631 return 1;
1632 }
Adam Langley95c29f32014-06-20 12:00:00 -07001633
Adam Langleyfcf25832014-12-18 17:42:32 -08001634 return memcmp(a->session_id, b->session_id, a->session_id_length);
1635}
Adam Langley95c29f32014-06-20 12:00:00 -07001636
Adam Langleyfcf25832014-12-18 17:42:32 -08001637SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth) {
1638 SSL_CTX *ret = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001639
Adam Langleyfcf25832014-12-18 17:42:32 -08001640 if (meth == NULL) {
1641 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_NULL_SSL_METHOD_PASSED);
1642 return NULL;
1643 }
Adam Langley95c29f32014-06-20 12:00:00 -07001644
Adam Langleyfcf25832014-12-18 17:42:32 -08001645 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
1646 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
1647 goto err;
1648 }
Adam Langley95c29f32014-06-20 12:00:00 -07001649
Adam Langleyfcf25832014-12-18 17:42:32 -08001650 ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1651 if (ret == NULL) {
1652 goto err;
1653 }
Adam Langley95c29f32014-06-20 12:00:00 -07001654
Adam Langleyfcf25832014-12-18 17:42:32 -08001655 memset(ret, 0, sizeof(SSL_CTX));
Adam Langley95c29f32014-06-20 12:00:00 -07001656
Adam Langleyfcf25832014-12-18 17:42:32 -08001657 ret->method = meth->method;
Adam Langley95c29f32014-06-20 12:00:00 -07001658
Adam Langleyfcf25832014-12-18 17:42:32 -08001659 ret->cert_store = NULL;
1660 ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
1661 ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1662 ret->session_cache_head = NULL;
1663 ret->session_cache_tail = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001664
Adam Langleyfcf25832014-12-18 17:42:32 -08001665 /* We take the system default */
1666 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001667
Adam Langleyfcf25832014-12-18 17:42:32 -08001668 ret->new_session_cb = 0;
1669 ret->remove_session_cb = 0;
1670 ret->get_session_cb = 0;
1671 ret->generate_session_id = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001672
Adam Langleyfcf25832014-12-18 17:42:32 -08001673 ret->references = 1;
1674 ret->quiet_shutdown = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001675
Adam Langleyfcf25832014-12-18 17:42:32 -08001676 ret->info_callback = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001677
Adam Langleyfcf25832014-12-18 17:42:32 -08001678 ret->app_verify_callback = 0;
1679 ret->app_verify_arg = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001680
Adam Langleyfcf25832014-12-18 17:42:32 -08001681 ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
1682 ret->read_ahead = 0;
1683 ret->msg_callback = 0;
1684 ret->msg_callback_arg = NULL;
1685 ret->verify_mode = SSL_VERIFY_NONE;
1686 ret->sid_ctx_length = 0;
1687 ret->default_verify_callback = NULL;
1688 ret->cert = ssl_cert_new();
1689 if (ret->cert == NULL) {
1690 goto err;
1691 }
Adam Langley95c29f32014-06-20 12:00:00 -07001692
Adam Langleyfcf25832014-12-18 17:42:32 -08001693 ret->default_passwd_callback = 0;
1694 ret->default_passwd_callback_userdata = NULL;
1695 ret->client_cert_cb = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001696
Adam Langleyfcf25832014-12-18 17:42:32 -08001697 ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
1698 if (ret->sessions == NULL) {
1699 goto err;
1700 }
1701 ret->cert_store = X509_STORE_new();
1702 if (ret->cert_store == NULL) {
1703 goto err;
1704 }
Adam Langley95c29f32014-06-20 12:00:00 -07001705
Adam Langleyfcf25832014-12-18 17:42:32 -08001706 ssl_create_cipher_list(ret->method, &ret->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001707 &ret->cipher_list_by_id, SSL_DEFAULT_CIPHER_LIST);
Adam Langleyfcf25832014-12-18 17:42:32 -08001708 if (ret->cipher_list == NULL ||
1709 sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0) {
1710 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_LIBRARY_HAS_NO_CIPHERS);
1711 goto err2;
1712 }
Adam Langley95c29f32014-06-20 12:00:00 -07001713
Adam Langleyfcf25832014-12-18 17:42:32 -08001714 ret->param = X509_VERIFY_PARAM_new();
1715 if (!ret->param) {
1716 goto err;
1717 }
Adam Langley95c29f32014-06-20 12:00:00 -07001718
Adam Langleyfcf25832014-12-18 17:42:32 -08001719 ret->client_CA = sk_X509_NAME_new_null();
1720 if (ret->client_CA == NULL) {
1721 goto err;
1722 }
Adam Langley95c29f32014-06-20 12:00:00 -07001723
David Benjamin9f33fc62015-04-15 17:29:53 -04001724 CRYPTO_new_ex_data(&g_ex_data_class_ssl_ctx, ret, &ret->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001725
Adam Langleyfcf25832014-12-18 17:42:32 -08001726 ret->extra_certs = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001727
Adam Langleyfcf25832014-12-18 17:42:32 -08001728 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
David Benjamin422d3a42014-08-20 11:09:03 -04001729
Adam Langleyfcf25832014-12-18 17:42:32 -08001730 ret->tlsext_servername_callback = 0;
1731 ret->tlsext_servername_arg = NULL;
1732 /* Setup RFC4507 ticket keys */
1733 if (!RAND_bytes(ret->tlsext_tick_key_name, 16) ||
1734 !RAND_bytes(ret->tlsext_tick_hmac_key, 16) ||
1735 !RAND_bytes(ret->tlsext_tick_aes_key, 16)) {
1736 ret->options |= SSL_OP_NO_TICKET;
1737 }
Adam Langley95c29f32014-06-20 12:00:00 -07001738
Adam Langleyfcf25832014-12-18 17:42:32 -08001739 ret->next_protos_advertised_cb = 0;
1740 ret->next_proto_select_cb = 0;
1741 ret->psk_identity_hint = NULL;
1742 ret->psk_client_callback = NULL;
1743 ret->psk_server_callback = NULL;
1744
1745 /* Default is to connect to non-RI servers. When RI is more widely deployed
1746 * might change this. */
1747 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
1748
1749 /* Lock the SSL_CTX to the specified version, for compatibility with legacy
1750 * uses of SSL_METHOD. */
1751 if (meth->version != 0) {
1752 SSL_CTX_set_max_version(ret, meth->version);
1753 SSL_CTX_set_min_version(ret, meth->version);
1754 }
1755
1756 return ret;
1757
Adam Langley95c29f32014-06-20 12:00:00 -07001758err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001759 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -07001760err2:
David Benjamin2755a3e2015-04-22 16:17:58 -04001761 SSL_CTX_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -08001762 return NULL;
1763}
Adam Langley95c29f32014-06-20 12:00:00 -07001764
David Benjamin4fcc2e22015-04-22 12:58:16 -04001765void SSL_CTX_free(SSL_CTX *ctx) {
1766 if (ctx == NULL ||
1767 CRYPTO_add(&ctx->references, -1, CRYPTO_LOCK_SSL_CTX) > 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001768 return;
1769 }
Adam Langley95c29f32014-06-20 12:00:00 -07001770
David Benjamin2755a3e2015-04-22 16:17:58 -04001771 X509_VERIFY_PARAM_free(ctx->param);
Adam Langley95c29f32014-06-20 12:00:00 -07001772
Adam Langleyfcf25832014-12-18 17:42:32 -08001773 /* Free internal session cache. However: the remove_cb() may reference the
1774 * ex_data of SSL_CTX, thus the ex_data store can only be removed after the
1775 * sessions were flushed. As the ex_data handling routines might also touch
1776 * the session cache, the most secure solution seems to be: empty (flush) the
1777 * cache, then free ex_data, then finally free the cache. (See ticket
1778 * [openssl.org #212].) */
David Benjamin2755a3e2015-04-22 16:17:58 -04001779 SSL_CTX_flush_sessions(ctx, 0);
Adam Langley95c29f32014-06-20 12:00:00 -07001780
David Benjamin4fcc2e22015-04-22 12:58:16 -04001781 CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, ctx, &ctx->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001782
David Benjamin2755a3e2015-04-22 16:17:58 -04001783 lh_SSL_SESSION_free(ctx->sessions);
1784 X509_STORE_free(ctx->cert_store);
1785 ssl_cipher_preference_list_free(ctx->cipher_list);
1786 sk_SSL_CIPHER_free(ctx->cipher_list_by_id);
1787 ssl_cipher_preference_list_free(ctx->cipher_list_tls11);
1788 ssl_cert_free(ctx->cert);
1789 sk_X509_NAME_pop_free(ctx->client_CA, X509_NAME_free);
1790 sk_X509_pop_free(ctx->extra_certs, X509_free);
1791 sk_SRTP_PROTECTION_PROFILE_free(ctx->srtp_profiles);
1792 OPENSSL_free(ctx->psk_identity_hint);
1793 OPENSSL_free(ctx->tlsext_ecpointformatlist);
1794 OPENSSL_free(ctx->tlsext_ellipticcurvelist);
1795 OPENSSL_free(ctx->alpn_client_proto_list);
1796 EVP_PKEY_free(ctx->tlsext_channel_id_private);
1797 BIO_free(ctx->keylog_bio);
Adam Langley95c29f32014-06-20 12:00:00 -07001798
David Benjamin4fcc2e22015-04-22 12:58:16 -04001799 OPENSSL_free(ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08001800}
Adam Langley95c29f32014-06-20 12:00:00 -07001801
Adam Langleyfcf25832014-12-18 17:42:32 -08001802void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) {
1803 ctx->default_passwd_callback = cb;
1804}
Adam Langley95c29f32014-06-20 12:00:00 -07001805
Adam Langleyfcf25832014-12-18 17:42:32 -08001806void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u) {
1807 ctx->default_passwd_callback_userdata = u;
1808}
Adam Langley95c29f32014-06-20 12:00:00 -07001809
Adam Langleyfcf25832014-12-18 17:42:32 -08001810void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
1811 int (*cb)(X509_STORE_CTX *, void *),
1812 void *arg) {
1813 ctx->app_verify_callback = cb;
1814 ctx->app_verify_arg = arg;
1815}
Adam Langley95c29f32014-06-20 12:00:00 -07001816
Adam Langleyfcf25832014-12-18 17:42:32 -08001817void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
1818 int (*cb)(int, X509_STORE_CTX *)) {
1819 ctx->verify_mode = mode;
1820 ctx->default_verify_callback = cb;
1821}
Adam Langley95c29f32014-06-20 12:00:00 -07001822
Adam Langleyfcf25832014-12-18 17:42:32 -08001823void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
1824 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
1825}
Adam Langley1258b6a2014-06-20 12:00:00 -07001826
Adam Langleyfcf25832014-12-18 17:42:32 -08001827void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb)(SSL *ssl, void *arg),
1828 void *arg) {
1829 ssl_cert_set_cert_cb(c->cert, cb, arg);
1830}
David Benjamin859ec3c2014-09-02 16:29:36 -04001831
Adam Langleyfcf25832014-12-18 17:42:32 -08001832void SSL_set_cert_cb(SSL *s, int (*cb)(SSL *ssl, void *arg), void *arg) {
1833 ssl_cert_set_cert_cb(s->cert, cb, arg);
1834}
Adam Langley95c29f32014-06-20 12:00:00 -07001835
Adam Langleyfcf25832014-12-18 17:42:32 -08001836static int ssl_has_key(SSL *s, size_t idx) {
1837 CERT_PKEY *cpk = &s->cert->pkeys[idx];
1838 return cpk->x509 && cpk->privatekey;
1839}
David Benjamin033e5f42014-11-13 18:47:41 -05001840
David Benjamin107db582015-04-08 00:41:59 -04001841void ssl_get_compatible_server_ciphers(SSL *s, uint32_t *out_mask_k,
1842 uint32_t *out_mask_a) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001843 CERT *c = s->cert;
1844 int rsa_enc, rsa_sign, dh_tmp;
David Benjamin107db582015-04-08 00:41:59 -04001845 uint32_t mask_k, mask_a;
Adam Langleyfcf25832014-12-18 17:42:32 -08001846 int have_ecc_cert, ecdsa_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001847 X509 *x;
Adam Langley95c29f32014-06-20 12:00:00 -07001848
Adam Langleyfcf25832014-12-18 17:42:32 -08001849 if (c == NULL) {
1850 /* TODO(davidben): Is this codepath possible? */
1851 *out_mask_k = 0;
1852 *out_mask_a = 0;
1853 return;
1854 }
Adam Langley95c29f32014-06-20 12:00:00 -07001855
Adam Langleyfcf25832014-12-18 17:42:32 -08001856 dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001857
Adam Langleyfcf25832014-12-18 17:42:32 -08001858 rsa_enc = ssl_has_key(s, SSL_PKEY_RSA_ENC);
1859 rsa_sign = ssl_has_key(s, SSL_PKEY_RSA_SIGN);
1860 have_ecc_cert = ssl_has_key(s, SSL_PKEY_ECC);
1861 mask_k = 0;
1862 mask_a = 0;
David Benjaminf31e6812014-11-13 18:05:55 -05001863
Adam Langleyfcf25832014-12-18 17:42:32 -08001864 if (rsa_enc) {
1865 mask_k |= SSL_kRSA;
1866 }
1867 if (dh_tmp) {
David Benjamin7061e282015-03-19 11:10:48 -04001868 mask_k |= SSL_kDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001869 }
1870 if (rsa_enc || rsa_sign) {
1871 mask_a |= SSL_aRSA;
1872 }
Adam Langley95c29f32014-06-20 12:00:00 -07001873
Adam Langleyfcf25832014-12-18 17:42:32 -08001874 /* An ECC certificate may be usable for ECDSA cipher suites depending on the
1875 * key usage extension and on the client's curve preferences. */
1876 if (have_ecc_cert) {
1877 x = c->pkeys[SSL_PKEY_ECC].x509;
1878 /* This call populates extension flags (ex_flags). */
1879 X509_check_purpose(x, -1, 0);
1880 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE)
1881 ? (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)
1882 : 1;
1883 if (!tls1_check_ec_cert(s, x)) {
1884 ecdsa_ok = 0;
1885 }
1886 if (ecdsa_ok) {
1887 mask_a |= SSL_aECDSA;
1888 }
1889 }
Adam Langley95c29f32014-06-20 12:00:00 -07001890
Adam Langleyfcf25832014-12-18 17:42:32 -08001891 /* If we are considering an ECC cipher suite that uses an ephemeral EC
1892 * key, check it. */
David Benjamindd978782015-04-24 15:20:13 -04001893 if (tls1_check_ec_tmp_key(s)) {
David Benjamin7061e282015-03-19 11:10:48 -04001894 mask_k |= SSL_kECDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001895 }
Adam Langley95c29f32014-06-20 12:00:00 -07001896
Adam Langleyfcf25832014-12-18 17:42:32 -08001897 /* PSK requires a server callback. */
1898 if (s->psk_server_callback != NULL) {
1899 mask_k |= SSL_kPSK;
1900 mask_a |= SSL_aPSK;
1901 }
Adam Langley95c29f32014-06-20 12:00:00 -07001902
Adam Langleyfcf25832014-12-18 17:42:32 -08001903 *out_mask_k = mask_k;
1904 *out_mask_a = mask_a;
1905}
Adam Langley95c29f32014-06-20 12:00:00 -07001906
1907/* This handy macro borrowed from crypto/x509v3/v3_purp.c */
1908#define ku_reject(x, usage) \
Adam Langleyfcf25832014-12-18 17:42:32 -08001909 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
Adam Langley95c29f32014-06-20 12:00:00 -07001910
Adam Langleyfcf25832014-12-18 17:42:32 -08001911int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001912 const SSL_CIPHER *cs = s->s3->tmp.new_cipher;
David Benjamin3fa27772015-04-17 22:32:19 -04001913 uint32_t alg_a = cs->algorithm_auth;
1914 int signature_nid = 0, md_nid = 0, pk_nid = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001915
Adam Langleyfcf25832014-12-18 17:42:32 -08001916 /* This call populates the ex_flags field correctly */
1917 X509_check_purpose(x, -1, 0);
1918 if (x->sig_alg && x->sig_alg->algorithm) {
1919 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
1920 OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
1921 }
1922 if (alg_a & SSL_aECDSA) {
1923 /* key usage, if present, must allow signing */
1924 if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE)) {
1925 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg,
1926 SSL_R_ECC_CERT_NOT_FOR_SIGNING);
1927 return 0;
1928 }
1929 }
Adam Langley95c29f32014-06-20 12:00:00 -07001930
Adam Langleyfcf25832014-12-18 17:42:32 -08001931 return 1; /* all checks are ok */
1932}
Adam Langley95c29f32014-06-20 12:00:00 -07001933
Adam Langleyfcf25832014-12-18 17:42:32 -08001934static int ssl_get_server_cert_index(const SSL *s) {
1935 int idx;
1936 idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1937 if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509) {
1938 idx = SSL_PKEY_RSA_SIGN;
1939 }
1940 if (idx == -1) {
1941 OPENSSL_PUT_ERROR(SSL, ssl_get_server_cert_index, ERR_R_INTERNAL_ERROR);
1942 }
1943 return idx;
1944}
Adam Langley95c29f32014-06-20 12:00:00 -07001945
Adam Langleyfcf25832014-12-18 17:42:32 -08001946CERT_PKEY *ssl_get_server_send_pkey(const SSL *s) {
1947 int i = ssl_get_server_cert_index(s);
Adam Langley95c29f32014-06-20 12:00:00 -07001948
Adam Langleyfcf25832014-12-18 17:42:32 -08001949 /* This may or may not be an error. */
1950 if (i < 0) {
1951 return NULL;
1952 }
Adam Langley95c29f32014-06-20 12:00:00 -07001953
Adam Langleyfcf25832014-12-18 17:42:32 -08001954 /* May be NULL. */
1955 return &s->cert->pkeys[i];
1956}
Adam Langley95c29f32014-06-20 12:00:00 -07001957
Adam Langleyfcf25832014-12-18 17:42:32 -08001958EVP_PKEY *ssl_get_sign_pkey(SSL *s, const SSL_CIPHER *cipher) {
David Benjamin3fa27772015-04-17 22:32:19 -04001959 uint32_t alg_a = cipher->algorithm_auth;
1960 CERT *c = s->cert;
Adam Langleyfcf25832014-12-18 17:42:32 -08001961 int idx = -1;
Adam Langley95c29f32014-06-20 12:00:00 -07001962
Adam Langleyfcf25832014-12-18 17:42:32 -08001963 if (alg_a & SSL_aRSA) {
1964 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL) {
1965 idx = SSL_PKEY_RSA_SIGN;
1966 } else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL) {
1967 idx = SSL_PKEY_RSA_ENC;
1968 }
1969 } else if ((alg_a & SSL_aECDSA) &&
1970 (c->pkeys[SSL_PKEY_ECC].privatekey != NULL)) {
1971 idx = SSL_PKEY_ECC;
1972 }
Adam Langley95c29f32014-06-20 12:00:00 -07001973
Adam Langleyfcf25832014-12-18 17:42:32 -08001974 if (idx == -1) {
1975 OPENSSL_PUT_ERROR(SSL, ssl_get_sign_pkey, ERR_R_INTERNAL_ERROR);
1976 return NULL;
1977 }
Adam Langley95c29f32014-06-20 12:00:00 -07001978
Adam Langleyfcf25832014-12-18 17:42:32 -08001979 return c->pkeys[idx].privatekey;
1980}
Adam Langley95c29f32014-06-20 12:00:00 -07001981
Adam Langleyfcf25832014-12-18 17:42:32 -08001982void ssl_update_cache(SSL *s, int mode) {
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001983 /* Never cache sessions with empty session IDs. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001984 if (s->session->session_id_length == 0) {
1985 return;
1986 }
Adam Langley95c29f32014-06-20 12:00:00 -07001987
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001988 SSL_CTX *ctx = s->initial_ctx;
1989 if ((ctx->session_cache_mode & mode) == mode && !s->hit &&
1990 ((ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) ||
1991 SSL_CTX_add_session(ctx, s->session)) &&
1992 ctx->new_session_cb != NULL) {
1993 /* Note: |new_session_cb| is called whether the internal session cache is
1994 * used or not. */
1995 if (!ctx->new_session_cb(s, SSL_SESSION_up_ref(s->session))) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001996 SSL_SESSION_free(s->session);
1997 }
1998 }
Adam Langley95c29f32014-06-20 12:00:00 -07001999
David Benjaminb6d0c6d2015-03-19 19:07:26 -04002000 if (!(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR) &&
2001 !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) &&
2002 (ctx->session_cache_mode & mode) == mode) {
2003 /* Automatically flush the internal session cache every 255 connections. */
2004 int flush_cache = 0;
2005 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2006 ctx->handshakes_since_cache_flush++;
2007 if (ctx->handshakes_since_cache_flush >= 255) {
2008 flush_cache = 1;
2009 ctx->handshakes_since_cache_flush = 0;
2010 }
2011 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
2012
2013 if (flush_cache) {
2014 SSL_CTX_flush_sessions(ctx, (unsigned long)time(NULL));
Adam Langleyfcf25832014-12-18 17:42:32 -08002015 }
2016 }
2017}
Adam Langley95c29f32014-06-20 12:00:00 -07002018
David Benjamin1a5c50f2015-03-11 16:22:37 -04002019int SSL_get_error(const SSL *s, int ret_code) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002020 int reason;
David Benjamin1a5c50f2015-03-11 16:22:37 -04002021 uint32_t err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002022 BIO *bio;
Adam Langley95c29f32014-06-20 12:00:00 -07002023
David Benjamin1a5c50f2015-03-11 16:22:37 -04002024 if (ret_code > 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002025 return SSL_ERROR_NONE;
2026 }
Adam Langley95c29f32014-06-20 12:00:00 -07002027
Adam Langleyfcf25832014-12-18 17:42:32 -08002028 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
2029 * where we do encode the error */
David Benjamin1a5c50f2015-03-11 16:22:37 -04002030 err = ERR_peek_error();
2031 if (err != 0) {
2032 if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002033 return SSL_ERROR_SYSCALL;
2034 }
2035 return SSL_ERROR_SSL;
2036 }
Adam Langley95c29f32014-06-20 12:00:00 -07002037
David Benjamin1a5c50f2015-03-11 16:22:37 -04002038 if (ret_code == 0) {
David Benjamin9a38e922015-01-22 16:06:11 -05002039 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2040 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) {
2041 /* The socket was cleanly shut down with a close_notify. */
2042 return SSL_ERROR_ZERO_RETURN;
2043 }
2044 /* An EOF was observed which violates the protocol, and the underlying
2045 * transport does not participate in the error queue. Bubble up to the
2046 * caller. */
2047 return SSL_ERROR_SYSCALL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002048 }
Adam Langleyb2ce0582014-06-20 12:00:00 -07002049
Adam Langleyfcf25832014-12-18 17:42:32 -08002050 if (SSL_want_session(s)) {
2051 return SSL_ERROR_PENDING_SESSION;
2052 }
Adam Langley95c29f32014-06-20 12:00:00 -07002053
Adam Langleyfcf25832014-12-18 17:42:32 -08002054 if (SSL_want_certificate(s)) {
2055 return SSL_ERROR_PENDING_CERTIFICATE;
2056 }
Adam Langley95c29f32014-06-20 12:00:00 -07002057
Adam Langleyfcf25832014-12-18 17:42:32 -08002058 if (SSL_want_read(s)) {
2059 bio = SSL_get_rbio(s);
2060 if (BIO_should_read(bio)) {
2061 return SSL_ERROR_WANT_READ;
2062 }
Adam Langley95c29f32014-06-20 12:00:00 -07002063
Adam Langleyfcf25832014-12-18 17:42:32 -08002064 if (BIO_should_write(bio)) {
2065 /* This one doesn't make too much sense ... We never try to write to the
2066 * rbio, and an application program where rbio and wbio are separate
2067 * couldn't even know what it should wait for. However if we ever set
2068 * s->rwstate incorrectly (so that we have SSL_want_read(s) instead of
2069 * SSL_want_write(s)) and rbio and wbio *are* the same, this test works
2070 * around that bug; so it might be safer to keep it. */
2071 return SSL_ERROR_WANT_WRITE;
2072 }
Adam Langley95c29f32014-06-20 12:00:00 -07002073
Adam Langleyfcf25832014-12-18 17:42:32 -08002074 if (BIO_should_io_special(bio)) {
2075 reason = BIO_get_retry_reason(bio);
2076 if (reason == BIO_RR_CONNECT) {
2077 return SSL_ERROR_WANT_CONNECT;
2078 }
Adam Langley95c29f32014-06-20 12:00:00 -07002079
Adam Langleyfcf25832014-12-18 17:42:32 -08002080 if (reason == BIO_RR_ACCEPT) {
2081 return SSL_ERROR_WANT_ACCEPT;
2082 }
Adam Langley95c29f32014-06-20 12:00:00 -07002083
Adam Langleyfcf25832014-12-18 17:42:32 -08002084 return SSL_ERROR_SYSCALL; /* unknown */
2085 }
2086 }
Adam Langley95c29f32014-06-20 12:00:00 -07002087
Adam Langleyfcf25832014-12-18 17:42:32 -08002088 if (SSL_want_write(s)) {
2089 bio = SSL_get_wbio(s);
2090 if (BIO_should_write(bio)) {
2091 return SSL_ERROR_WANT_WRITE;
2092 }
Adam Langley95c29f32014-06-20 12:00:00 -07002093
Adam Langleyfcf25832014-12-18 17:42:32 -08002094 if (BIO_should_read(bio)) {
2095 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2096 return SSL_ERROR_WANT_READ;
2097 }
Adam Langley95c29f32014-06-20 12:00:00 -07002098
Adam Langleyfcf25832014-12-18 17:42:32 -08002099 if (BIO_should_io_special(bio)) {
2100 reason = BIO_get_retry_reason(bio);
2101 if (reason == BIO_RR_CONNECT) {
2102 return SSL_ERROR_WANT_CONNECT;
2103 }
Adam Langley95c29f32014-06-20 12:00:00 -07002104
Adam Langleyfcf25832014-12-18 17:42:32 -08002105 if (reason == BIO_RR_ACCEPT) {
2106 return SSL_ERROR_WANT_ACCEPT;
2107 }
Adam Langley95c29f32014-06-20 12:00:00 -07002108
Adam Langleyfcf25832014-12-18 17:42:32 -08002109 return SSL_ERROR_SYSCALL;
2110 }
2111 }
Adam Langley95c29f32014-06-20 12:00:00 -07002112
Adam Langleyfcf25832014-12-18 17:42:32 -08002113 if (SSL_want_x509_lookup(s)) {
2114 return SSL_ERROR_WANT_X509_LOOKUP;
2115 }
Adam Langley95c29f32014-06-20 12:00:00 -07002116
Adam Langleyfcf25832014-12-18 17:42:32 -08002117 if (SSL_want_channel_id_lookup(s)) {
2118 return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
2119 }
Adam Langley0f4746e2014-08-13 12:26:32 -07002120
Adam Langleyfcf25832014-12-18 17:42:32 -08002121 return SSL_ERROR_SYSCALL;
2122}
Adam Langley0f4746e2014-08-13 12:26:32 -07002123
Adam Langleyfcf25832014-12-18 17:42:32 -08002124int SSL_do_handshake(SSL *s) {
2125 int ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002126
Adam Langleyfcf25832014-12-18 17:42:32 -08002127 if (s->handshake_func == NULL) {
2128 OPENSSL_PUT_ERROR(SSL, SSL_do_handshake, SSL_R_CONNECTION_TYPE_NOT_SET);
2129 return -1;
2130 }
Adam Langley95c29f32014-06-20 12:00:00 -07002131
Adam Langleyfcf25832014-12-18 17:42:32 -08002132 s->method->ssl_renegotiate_check(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002133
Adam Langleyfcf25832014-12-18 17:42:32 -08002134 if (SSL_in_init(s)) {
2135 ret = s->handshake_func(s);
2136 }
2137 return ret;
2138}
Adam Langley95c29f32014-06-20 12:00:00 -07002139
Adam Langleyfcf25832014-12-18 17:42:32 -08002140void SSL_set_accept_state(SSL *s) {
2141 s->server = 1;
2142 s->shutdown = 0;
2143 s->state = SSL_ST_ACCEPT | SSL_ST_BEFORE;
2144 s->handshake_func = s->method->ssl_accept;
2145 /* clear the current cipher */
2146 ssl_clear_cipher_ctx(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002147}
Adam Langley95c29f32014-06-20 12:00:00 -07002148
Adam Langleyfcf25832014-12-18 17:42:32 -08002149void SSL_set_connect_state(SSL *s) {
2150 s->server = 0;
2151 s->shutdown = 0;
2152 s->state = SSL_ST_CONNECT | SSL_ST_BEFORE;
2153 s->handshake_func = s->method->ssl_connect;
2154 /* clear the current cipher */
2155 ssl_clear_cipher_ctx(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002156}
Adam Langley95c29f32014-06-20 12:00:00 -07002157
Adam Langleyfcf25832014-12-18 17:42:32 -08002158int ssl_undefined_function(SSL *s) {
2159 OPENSSL_PUT_ERROR(SSL, ssl_undefined_function,
2160 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2161 return 0;
2162}
Adam Langley95c29f32014-06-20 12:00:00 -07002163
Adam Langleyfcf25832014-12-18 17:42:32 -08002164int ssl_undefined_void_function(void) {
2165 OPENSSL_PUT_ERROR(SSL, ssl_undefined_void_function,
2166 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2167 return 0;
2168}
Adam Langley95c29f32014-06-20 12:00:00 -07002169
Adam Langleyfcf25832014-12-18 17:42:32 -08002170int ssl_undefined_const_function(const SSL *s) {
2171 OPENSSL_PUT_ERROR(SSL, ssl_undefined_const_function,
2172 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2173 return 0;
2174}
Adam Langley95c29f32014-06-20 12:00:00 -07002175
Adam Langleyfcf25832014-12-18 17:42:32 -08002176static const char *ssl_get_version(int version) {
2177 switch (version) {
2178 case TLS1_2_VERSION:
2179 return "TLSv1.2";
Adam Langley95c29f32014-06-20 12:00:00 -07002180
Adam Langleyfcf25832014-12-18 17:42:32 -08002181 case TLS1_1_VERSION:
2182 return "TLSv1.1";
Adam Langley95c29f32014-06-20 12:00:00 -07002183
Adam Langleyfcf25832014-12-18 17:42:32 -08002184 case TLS1_VERSION:
2185 return "TLSv1";
Adam Langley95c29f32014-06-20 12:00:00 -07002186
Adam Langleyfcf25832014-12-18 17:42:32 -08002187 case SSL3_VERSION:
2188 return "SSLv3";
Adam Langley95c29f32014-06-20 12:00:00 -07002189
David Benjamin1c722b72015-04-20 13:53:10 -04002190 case DTLS1_VERSION:
2191 return "DTLSv1";
2192
2193 case DTLS1_2_VERSION:
2194 return "DTLSv1.2";
2195
Adam Langleyfcf25832014-12-18 17:42:32 -08002196 default:
2197 return "unknown";
2198 }
2199}
Adam Langley95c29f32014-06-20 12:00:00 -07002200
Adam Langleyfcf25832014-12-18 17:42:32 -08002201const char *SSL_get_version(const SSL *s) {
2202 return ssl_get_version(s->version);
2203}
Adam Langley95c29f32014-06-20 12:00:00 -07002204
Adam Langleyfcf25832014-12-18 17:42:32 -08002205const char *SSL_SESSION_get_version(const SSL_SESSION *sess) {
2206 return ssl_get_version(sess->ssl_version);
2207}
Adam Langley95c29f32014-06-20 12:00:00 -07002208
Adam Langleyfcf25832014-12-18 17:42:32 -08002209void ssl_clear_cipher_ctx(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002210 if (s->aead_read_ctx != NULL) {
2211 EVP_AEAD_CTX_cleanup(&s->aead_read_ctx->ctx);
2212 OPENSSL_free(s->aead_read_ctx);
2213 s->aead_read_ctx = NULL;
2214 }
Adam Langleya5dc5452014-06-20 12:00:00 -07002215
Adam Langleyfcf25832014-12-18 17:42:32 -08002216 if (s->aead_write_ctx != NULL) {
2217 EVP_AEAD_CTX_cleanup(&s->aead_write_ctx->ctx);
2218 OPENSSL_free(s->aead_write_ctx);
2219 s->aead_write_ctx = NULL;
2220 }
2221}
Adam Langley95c29f32014-06-20 12:00:00 -07002222
Adam Langleyfcf25832014-12-18 17:42:32 -08002223X509 *SSL_get_certificate(const SSL *s) {
2224 if (s->cert != NULL) {
2225 return s->cert->key->x509;
2226 }
2227
2228 return NULL;
2229}
2230
2231EVP_PKEY *SSL_get_privatekey(const SSL *s) {
2232 if (s->cert != NULL) {
2233 return s->cert->key->privatekey;
2234 }
2235
2236 return NULL;
2237}
2238
2239X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
2240 if (ctx->cert != NULL) {
2241 return ctx->cert->key->x509;
2242 }
2243
2244 return NULL;
2245}
2246
2247EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
2248 if (ctx->cert != NULL) {
2249 return ctx->cert->key->privatekey;
2250 }
2251
2252 return NULL;
2253}
2254
2255const SSL_CIPHER *SSL_get_current_cipher(const SSL *s) {
2256 if (s->session != NULL && s->session->cipher != NULL) {
2257 return s->session->cipher;
2258 }
2259
2260 return NULL;
2261}
2262
2263const void *SSL_get_current_compression(SSL *s) { return NULL; }
2264
2265const void *SSL_get_current_expansion(SSL *s) { return NULL; }
2266
2267int ssl_init_wbio_buffer(SSL *s, int push) {
2268 BIO *bbio;
2269
2270 if (s->bbio == NULL) {
2271 bbio = BIO_new(BIO_f_buffer());
2272 if (bbio == NULL) {
2273 return 0;
2274 }
2275 s->bbio = bbio;
2276 } else {
2277 bbio = s->bbio;
2278 if (s->bbio == s->wbio) {
2279 s->wbio = BIO_pop(s->wbio);
2280 }
2281 }
2282
2283 BIO_reset(bbio);
2284 if (!BIO_set_read_buffer_size(bbio, 1)) {
2285 OPENSSL_PUT_ERROR(SSL, ssl_init_wbio_buffer, ERR_R_BUF_LIB);
2286 return 0;
2287 }
2288
2289 if (push) {
2290 if (s->wbio != bbio) {
2291 s->wbio = BIO_push(bbio, s->wbio);
2292 }
2293 } else {
2294 if (s->wbio == bbio) {
2295 s->wbio = BIO_pop(bbio);
2296 }
2297 }
2298
2299 return 1;
2300}
2301
2302void ssl_free_wbio_buffer(SSL *s) {
2303 if (s->bbio == NULL) {
2304 return;
2305 }
2306
2307 if (s->bbio == s->wbio) {
2308 /* remove buffering */
2309 s->wbio = BIO_pop(s->wbio);
2310 }
2311
2312 BIO_free(s->bbio);
2313 s->bbio = NULL;
2314}
2315
2316void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
2317 ctx->quiet_shutdown = mode;
2318}
2319
2320int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
2321 return ctx->quiet_shutdown;
2322}
2323
2324void SSL_set_quiet_shutdown(SSL *s, int mode) { s->quiet_shutdown = mode; }
2325
2326int SSL_get_quiet_shutdown(const SSL *s) { return s->quiet_shutdown; }
2327
2328void SSL_set_shutdown(SSL *s, int mode) { s->shutdown = mode; }
2329
2330int SSL_get_shutdown(const SSL *s) { return s->shutdown; }
2331
2332int SSL_version(const SSL *s) { return s->version; }
2333
2334SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
2335
2336SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
2337 if (ssl->ctx == ctx) {
2338 return ssl->ctx;
2339 }
2340
2341 if (ctx == NULL) {
2342 ctx = ssl->initial_ctx;
2343 }
2344
David Benjamin2755a3e2015-04-22 16:17:58 -04002345 ssl_cert_free(ssl->cert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002346 ssl->cert = ssl_cert_dup(ctx->cert);
David Benjamin2755a3e2015-04-22 16:17:58 -04002347
Adam Langleyfcf25832014-12-18 17:42:32 -08002348 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
David Benjamin2755a3e2015-04-22 16:17:58 -04002349 SSL_CTX_free(ssl->ctx); /* decrement reference count */
Adam Langleyfcf25832014-12-18 17:42:32 -08002350 ssl->ctx = ctx;
2351
2352 ssl->sid_ctx_length = ctx->sid_ctx_length;
2353 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2354 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2355
2356 return ssl->ctx;
2357}
2358
2359int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
2360 return X509_STORE_set_default_paths(ctx->cert_store);
2361}
Adam Langley95c29f32014-06-20 12:00:00 -07002362
2363int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
Adam Langleyfcf25832014-12-18 17:42:32 -08002364 const char *CApath) {
2365 return X509_STORE_load_locations(ctx->cert_store, CAfile, CApath);
2366}
Adam Langley95c29f32014-06-20 12:00:00 -07002367
2368void SSL_set_info_callback(SSL *ssl,
Adam Langleyfcf25832014-12-18 17:42:32 -08002369 void (*cb)(const SSL *ssl, int type, int val)) {
2370 ssl->info_callback = cb;
2371}
Adam Langley95c29f32014-06-20 12:00:00 -07002372
Adam Langleyfcf25832014-12-18 17:42:32 -08002373void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/, int /*type*/,
2374 int /*val*/) {
2375 return ssl->info_callback;
2376}
Adam Langley95c29f32014-06-20 12:00:00 -07002377
Adam Langleyfcf25832014-12-18 17:42:32 -08002378int SSL_state(const SSL *ssl) { return ssl->state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002379
Adam Langleyfcf25832014-12-18 17:42:32 -08002380void SSL_set_state(SSL *ssl, int state) { ssl->state = state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002381
Adam Langleyfcf25832014-12-18 17:42:32 -08002382void SSL_set_verify_result(SSL *ssl, long arg) { ssl->verify_result = arg; }
Adam Langley95c29f32014-06-20 12:00:00 -07002383
Adam Langleyfcf25832014-12-18 17:42:32 -08002384long SSL_get_verify_result(const SSL *ssl) { return ssl->verify_result; }
Adam Langley95c29f32014-06-20 12:00:00 -07002385
Adam Langleyfcf25832014-12-18 17:42:32 -08002386int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2387 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002388 int index;
2389 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp,
2390 new_func, dup_func, free_func)) {
2391 return -1;
2392 }
2393 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002394}
Adam Langley95c29f32014-06-20 12:00:00 -07002395
Adam Langleyfcf25832014-12-18 17:42:32 -08002396int SSL_set_ex_data(SSL *s, int idx, void *arg) {
2397 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2398}
Adam Langley95c29f32014-06-20 12:00:00 -07002399
Adam Langleyfcf25832014-12-18 17:42:32 -08002400void *SSL_get_ex_data(const SSL *s, int idx) {
2401 return CRYPTO_get_ex_data(&s->ex_data, idx);
2402}
Adam Langley95c29f32014-06-20 12:00:00 -07002403
Adam Langleyfcf25832014-12-18 17:42:32 -08002404int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2405 CRYPTO_EX_dup *dup_func,
2406 CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002407 int index;
2408 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp,
2409 new_func, dup_func, free_func)) {
2410 return -1;
2411 }
2412 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002413}
Adam Langley95c29f32014-06-20 12:00:00 -07002414
Adam Langleyfcf25832014-12-18 17:42:32 -08002415int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg) {
2416 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2417}
Adam Langley95c29f32014-06-20 12:00:00 -07002418
Adam Langleyfcf25832014-12-18 17:42:32 -08002419void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx) {
2420 return CRYPTO_get_ex_data(&s->ex_data, idx);
2421}
Adam Langley95c29f32014-06-20 12:00:00 -07002422
Adam Langleyfcf25832014-12-18 17:42:32 -08002423int ssl_ok(SSL *s) { return 1; }
Adam Langley95c29f32014-06-20 12:00:00 -07002424
Adam Langleyfcf25832014-12-18 17:42:32 -08002425X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
2426 return ctx->cert_store;
2427}
Adam Langley95c29f32014-06-20 12:00:00 -07002428
Adam Langleyfcf25832014-12-18 17:42:32 -08002429void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
David Benjamin2755a3e2015-04-22 16:17:58 -04002430 X509_STORE_free(ctx->cert_store);
Adam Langleyfcf25832014-12-18 17:42:32 -08002431 ctx->cert_store = store;
2432}
Adam Langley95c29f32014-06-20 12:00:00 -07002433
Adam Langleyfcf25832014-12-18 17:42:32 -08002434int SSL_want(const SSL *s) { return s->rwstate; }
Adam Langley95c29f32014-06-20 12:00:00 -07002435
Adam Langleyfcf25832014-12-18 17:42:32 -08002436void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
2437 RSA *(*cb)(SSL *ssl, int is_export,
2438 int keylength)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002439}
Adam Langley95c29f32014-06-20 12:00:00 -07002440
Adam Langleyfcf25832014-12-18 17:42:32 -08002441void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
2442 int keylength)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002443}
2444
2445void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
David Benjamin59015c32015-04-26 13:13:55 -04002446 DH *(*callback)(SSL *ssl, int is_export,
2447 int keylength)) {
2448 ctx->cert->dh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002449}
2450
David Benjamin59015c32015-04-26 13:13:55 -04002451void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*callback)(SSL *ssl, int is_export,
2452 int keylength)) {
2453 ssl->cert->dh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002454}
2455
2456void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,
David Benjamindd978782015-04-24 15:20:13 -04002457 EC_KEY *(*callback)(SSL *ssl, int is_export,
2458 int keylength)) {
David Benjamin59015c32015-04-26 13:13:55 -04002459 ctx->cert->ecdh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002460}
2461
2462void SSL_set_tmp_ecdh_callback(SSL *ssl,
David Benjamindd978782015-04-24 15:20:13 -04002463 EC_KEY *(*callback)(SSL *ssl, int is_export,
2464 int keylength)) {
David Benjamin59015c32015-04-26 13:13:55 -04002465 ssl->cert->ecdh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002466}
2467
2468int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
2469 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2470 OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_psk_identity_hint,
2471 SSL_R_DATA_LENGTH_TOO_LONG);
2472 return 0;
2473 }
2474
David Benjamin2755a3e2015-04-22 16:17:58 -04002475 OPENSSL_free(ctx->psk_identity_hint);
Adam Langleyfcf25832014-12-18 17:42:32 -08002476
2477 if (identity_hint != NULL) {
2478 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2479 if (ctx->psk_identity_hint == NULL) {
2480 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002481 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002482 } else {
2483 ctx->psk_identity_hint = NULL;
2484 }
Adam Langley95c29f32014-06-20 12:00:00 -07002485
Adam Langleyfcf25832014-12-18 17:42:32 -08002486 return 1;
2487}
2488
2489int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint) {
2490 if (s == NULL) {
2491 return 0;
2492 }
2493
2494 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2495 OPENSSL_PUT_ERROR(SSL, SSL_use_psk_identity_hint,
2496 SSL_R_DATA_LENGTH_TOO_LONG);
2497 return 0;
2498 }
2499
2500 /* Clear currently configured hint, if any. */
David Benjamin2755a3e2015-04-22 16:17:58 -04002501 OPENSSL_free(s->psk_identity_hint);
2502 s->psk_identity_hint = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002503
2504 if (identity_hint != NULL) {
2505 s->psk_identity_hint = BUF_strdup(identity_hint);
2506 if (s->psk_identity_hint == NULL) {
2507 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002508 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002509 }
Adam Langley95c29f32014-06-20 12:00:00 -07002510
Adam Langleyfcf25832014-12-18 17:42:32 -08002511 return 1;
2512}
Adam Langley95c29f32014-06-20 12:00:00 -07002513
Adam Langleyfcf25832014-12-18 17:42:32 -08002514const char *SSL_get_psk_identity_hint(const SSL *s) {
2515 if (s == NULL) {
2516 return NULL;
2517 }
2518 return s->psk_identity_hint;
2519}
Adam Langley95c29f32014-06-20 12:00:00 -07002520
Adam Langleyfcf25832014-12-18 17:42:32 -08002521const char *SSL_get_psk_identity(const SSL *s) {
2522 if (s == NULL || s->session == NULL) {
2523 return NULL;
2524 }
Adam Langley95c29f32014-06-20 12:00:00 -07002525
Adam Langleyfcf25832014-12-18 17:42:32 -08002526 return s->session->psk_identity;
2527}
Adam Langley95c29f32014-06-20 12:00:00 -07002528
Adam Langleyfcf25832014-12-18 17:42:32 -08002529void SSL_set_psk_client_callback(
2530 SSL *s, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2531 unsigned int max_identity_len, uint8_t *psk,
2532 unsigned int max_psk_len)) {
2533 s->psk_client_callback = cb;
2534}
Adam Langley95c29f32014-06-20 12:00:00 -07002535
Adam Langleyfcf25832014-12-18 17:42:32 -08002536void SSL_CTX_set_psk_client_callback(
2537 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2538 unsigned int max_identity_len,
2539 uint8_t *psk, unsigned int max_psk_len)) {
2540 ctx->psk_client_callback = cb;
2541}
Adam Langley95c29f32014-06-20 12:00:00 -07002542
Adam Langleyfcf25832014-12-18 17:42:32 -08002543void SSL_set_psk_server_callback(
2544 SSL *s, unsigned int (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
2545 unsigned int max_psk_len)) {
2546 s->psk_server_callback = cb;
2547}
Adam Langley95c29f32014-06-20 12:00:00 -07002548
Adam Langleyfcf25832014-12-18 17:42:32 -08002549void SSL_CTX_set_psk_server_callback(
2550 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *identity,
2551 uint8_t *psk, unsigned int max_psk_len)) {
2552 ctx->psk_server_callback = cb;
2553}
Adam Langley95c29f32014-06-20 12:00:00 -07002554
Adam Langleyfcf25832014-12-18 17:42:32 -08002555void SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) {
2556 ctx->min_version = version;
2557}
Adam Langley95c29f32014-06-20 12:00:00 -07002558
Adam Langleyfcf25832014-12-18 17:42:32 -08002559void SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) {
2560 ctx->max_version = version;
2561}
Adam Langley0289c732014-06-20 12:00:00 -07002562
Adam Langleyfcf25832014-12-18 17:42:32 -08002563void SSL_set_min_version(SSL *ssl, uint16_t version) {
2564 ssl->min_version = version;
2565}
Adam Langley0289c732014-06-20 12:00:00 -07002566
Adam Langleyfcf25832014-12-18 17:42:32 -08002567void SSL_set_max_version(SSL *ssl, uint16_t version) {
2568 ssl->max_version = version;
2569}
Adam Langley95c29f32014-06-20 12:00:00 -07002570
Adam Langleyfcf25832014-12-18 17:42:32 -08002571void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
2572 void (*cb)(int write_p, int version,
2573 int content_type, const void *buf,
2574 size_t len, SSL *ssl, void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002575 ctx->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002576}
David Benjamin61ecccf2015-05-05 09:44:51 -04002577
2578void SSL_CTX_set_msg_callback_arg(SSL_CTX *ctx, void *arg) {
2579 ctx->msg_callback_arg = arg;
2580}
2581
Adam Langleyfcf25832014-12-18 17:42:32 -08002582void SSL_set_msg_callback(SSL *ssl,
2583 void (*cb)(int write_p, int version, int content_type,
2584 const void *buf, size_t len, SSL *ssl,
2585 void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002586 ssl->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002587}
Adam Langley95c29f32014-06-20 12:00:00 -07002588
David Benjamin61ecccf2015-05-05 09:44:51 -04002589void SSL_set_msg_callback_arg(SSL *ssl, void *arg) {
2590 ssl->msg_callback_arg = arg;
2591}
2592
Adam Langleyfcf25832014-12-18 17:42:32 -08002593void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio) {
David Benjamin2755a3e2015-04-22 16:17:58 -04002594 BIO_free(ctx->keylog_bio);
Adam Langleyfcf25832014-12-18 17:42:32 -08002595 ctx->keylog_bio = keylog_bio;
2596}
Adam Langley95c29f32014-06-20 12:00:00 -07002597
Adam Langleyfcf25832014-12-18 17:42:32 -08002598static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
2599 static const char hextable[] = "0123456789abcdef";
2600 uint8_t *out;
2601 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002602
Adam Langleyfcf25832014-12-18 17:42:32 -08002603 if (!CBB_add_space(cbb, &out, in_len * 2)) {
2604 return 0;
2605 }
Adam Langley95c29f32014-06-20 12:00:00 -07002606
Adam Langleyfcf25832014-12-18 17:42:32 -08002607 for (i = 0; i < in_len; i++) {
2608 *(out++) = (uint8_t)hextable[in[i] >> 4];
2609 *(out++) = (uint8_t)hextable[in[i] & 0xf];
2610 }
Adam Langley95c29f32014-06-20 12:00:00 -07002611
Adam Langleyfcf25832014-12-18 17:42:32 -08002612 return 1;
2613}
David Benjamin859ec3c2014-09-02 16:29:36 -04002614
2615int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -08002616 const uint8_t *encrypted_premaster,
2617 size_t encrypted_premaster_len,
2618 const uint8_t *premaster,
2619 size_t premaster_len) {
2620 BIO *bio = ctx->keylog_bio;
2621 CBB cbb;
2622 uint8_t *out;
2623 size_t out_len;
2624 int ret;
David Benjamin859ec3c2014-09-02 16:29:36 -04002625
Adam Langleyfcf25832014-12-18 17:42:32 -08002626 if (bio == NULL) {
2627 return 1;
2628 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002629
Adam Langleyfcf25832014-12-18 17:42:32 -08002630 if (encrypted_premaster_len < 8) {
2631 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_rsa_client_key_exchange,
2632 ERR_R_INTERNAL_ERROR);
2633 return 0;
2634 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002635
Adam Langleyfcf25832014-12-18 17:42:32 -08002636 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len * 2 + 1)) {
2637 return 0;
2638 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002639
Adam Langleyfcf25832014-12-18 17:42:32 -08002640 if (!CBB_add_bytes(&cbb, (const uint8_t *)"RSA ", 4) ||
2641 /* Only the first 8 bytes of the encrypted premaster secret are
2642 * logged. */
2643 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
2644 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2645 !cbb_add_hex(&cbb, premaster, premaster_len) ||
2646 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2647 !CBB_finish(&cbb, &out, &out_len)) {
2648 CBB_cleanup(&cbb);
2649 return 0;
2650 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002651
Adam Langleyfcf25832014-12-18 17:42:32 -08002652 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2653 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2654 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
David Benjamin859ec3c2014-09-02 16:29:36 -04002655
Adam Langleyfcf25832014-12-18 17:42:32 -08002656 OPENSSL_free(out);
2657 return ret;
Adam Langley95f22882014-06-20 12:00:00 -07002658}
2659
Adam Langleyfcf25832014-12-18 17:42:32 -08002660int ssl_ctx_log_master_secret(SSL_CTX *ctx, const uint8_t *client_random,
2661 size_t client_random_len, const uint8_t *master,
2662 size_t master_len) {
2663 BIO *bio = ctx->keylog_bio;
2664 CBB cbb;
2665 uint8_t *out;
2666 size_t out_len;
2667 int ret;
Adam Langleyadb739e2014-06-20 12:00:00 -07002668
Adam Langleyfcf25832014-12-18 17:42:32 -08002669 if (bio == NULL) {
2670 return 1;
2671 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002672
Adam Langleyfcf25832014-12-18 17:42:32 -08002673 if (client_random_len != 32) {
2674 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_master_secret, ERR_R_INTERNAL_ERROR);
2675 return 0;
2676 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002677
Adam Langleyfcf25832014-12-18 17:42:32 -08002678 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len * 2 + 1)) {
2679 return 0;
2680 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002681
Adam Langleyfcf25832014-12-18 17:42:32 -08002682 if (!CBB_add_bytes(&cbb, (const uint8_t *)"CLIENT_RANDOM ", 14) ||
2683 !cbb_add_hex(&cbb, client_random, 32) ||
2684 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2685 !cbb_add_hex(&cbb, master, master_len) ||
2686 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2687 !CBB_finish(&cbb, &out, &out_len)) {
2688 CBB_cleanup(&cbb);
2689 return 0;
2690 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002691
Adam Langleyfcf25832014-12-18 17:42:32 -08002692 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2693 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2694 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
David Benjamine99e9122014-12-11 01:46:01 -05002695
Adam Langleyfcf25832014-12-18 17:42:32 -08002696 OPENSSL_free(out);
2697 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -07002698}
2699
David Benjamined7c4752015-02-16 19:16:46 -05002700int SSL_in_false_start(const SSL *s) {
2701 return s->s3->tmp.in_false_start;
2702}
2703
Adam Langleyfcf25832014-12-18 17:42:32 -08002704int SSL_cutthrough_complete(const SSL *s) {
David Benjamined7c4752015-02-16 19:16:46 -05002705 return SSL_in_false_start(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002706}
Adam Langley95c29f32014-06-20 12:00:00 -07002707
Adam Langleyfcf25832014-12-18 17:42:32 -08002708void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
2709 size_t *ssl_session_size) {
2710 *ssl_size = sizeof(SSL);
2711 *ssl_ctx_size = sizeof(SSL_CTX);
2712 *ssl_session_size = sizeof(SSL_SESSION);
2713}
Feng Lu41aa3252014-11-21 22:47:56 -08002714
David Benjamined7c4752015-02-16 19:16:46 -05002715int ssl3_can_false_start(const SSL *s) {
David Benjamin195dc782015-02-19 13:27:05 -05002716 const SSL_CIPHER *const cipher = SSL_get_current_cipher(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002717
Adam Langleye631d962015-04-01 13:11:01 -07002718 /* False Start only for TLS 1.2 with an ECDHE+AEAD cipher and ALPN or NPN. */
David Benjamin195dc782015-02-19 13:27:05 -05002719 return !SSL_IS_DTLS(s) &&
2720 SSL_version(s) >= TLS1_2_VERSION &&
2721 (s->s3->alpn_selected || s->s3->next_proto_neg_seen) &&
2722 cipher != NULL &&
Adam Langleye631d962015-04-01 13:11:01 -07002723 cipher->algorithm_mkey == SSL_kECDHE &&
David Benjamin195dc782015-02-19 13:27:05 -05002724 (cipher->algorithm_enc == SSL_AES128GCM ||
2725 cipher->algorithm_enc == SSL_AES256GCM ||
2726 cipher->algorithm_enc == SSL_CHACHA20POLY1305);
Adam Langleyfcf25832014-12-18 17:42:32 -08002727}
2728
2729const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version) {
2730 switch (version) {
2731 case SSL3_VERSION:
2732 return &SSLv3_enc_data;
2733
2734 case TLS1_VERSION:
2735 return &TLSv1_enc_data;
2736
David Benjamin9e13e1a2015-03-05 01:56:32 -05002737 case DTLS1_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002738 case TLS1_1_VERSION:
2739 return &TLSv1_1_enc_data;
2740
David Benjamin9e13e1a2015-03-05 01:56:32 -05002741 case DTLS1_2_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002742 case TLS1_2_VERSION:
2743 return &TLSv1_2_enc_data;
2744
Adam Langleyfcf25832014-12-18 17:42:32 -08002745 default:
2746 return NULL;
2747 }
2748}
2749
2750uint16_t ssl3_get_max_server_version(const SSL *s) {
2751 uint16_t max_version;
2752
2753 if (SSL_IS_DTLS(s)) {
2754 max_version = (s->max_version != 0) ? s->max_version : DTLS1_2_VERSION;
2755 if (!(s->options & SSL_OP_NO_DTLSv1_2) && DTLS1_2_VERSION >= max_version) {
2756 return DTLS1_2_VERSION;
2757 }
2758 if (!(s->options & SSL_OP_NO_DTLSv1) && DTLS1_VERSION >= max_version) {
2759 return DTLS1_VERSION;
2760 }
2761 return 0;
2762 }
2763
2764 max_version = (s->max_version != 0) ? s->max_version : TLS1_2_VERSION;
2765 if (!(s->options & SSL_OP_NO_TLSv1_2) && TLS1_2_VERSION <= max_version) {
2766 return TLS1_2_VERSION;
2767 }
2768 if (!(s->options & SSL_OP_NO_TLSv1_1) && TLS1_1_VERSION <= max_version) {
2769 return TLS1_1_VERSION;
2770 }
2771 if (!(s->options & SSL_OP_NO_TLSv1) && TLS1_VERSION <= max_version) {
2772 return TLS1_VERSION;
2773 }
2774 if (!(s->options & SSL_OP_NO_SSLv3) && SSL3_VERSION <= max_version) {
2775 return SSL3_VERSION;
2776 }
2777 return 0;
2778}
2779
2780uint16_t ssl3_get_mutual_version(SSL *s, uint16_t client_version) {
2781 uint16_t version = 0;
2782
2783 if (SSL_IS_DTLS(s)) {
2784 /* Clamp client_version to max_version. */
2785 if (s->max_version != 0 && client_version < s->max_version) {
2786 client_version = s->max_version;
2787 }
2788
2789 if (client_version <= DTLS1_2_VERSION && !(s->options & SSL_OP_NO_DTLSv1_2)) {
2790 version = DTLS1_2_VERSION;
2791 } else if (client_version <= DTLS1_VERSION &&
David Benjaminb18f0242015-03-10 18:30:08 -04002792 !(s->options & SSL_OP_NO_DTLSv1)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002793 version = DTLS1_VERSION;
2794 }
2795
2796 /* Check against min_version. */
2797 if (version != 0 && s->min_version != 0 && version > s->min_version) {
2798 return 0;
2799 }
2800 return version;
2801 } else {
2802 /* Clamp client_version to max_version. */
2803 if (s->max_version != 0 && client_version > s->max_version) {
2804 client_version = s->max_version;
2805 }
2806
2807 if (client_version >= TLS1_2_VERSION && !(s->options & SSL_OP_NO_TLSv1_2)) {
2808 version = TLS1_2_VERSION;
2809 } else if (client_version >= TLS1_1_VERSION &&
2810 !(s->options & SSL_OP_NO_TLSv1_1)) {
2811 version = TLS1_1_VERSION;
2812 } else if (client_version >= TLS1_VERSION && !(s->options & SSL_OP_NO_TLSv1)) {
2813 version = TLS1_VERSION;
2814 } else if (client_version >= SSL3_VERSION && !(s->options & SSL_OP_NO_SSLv3)) {
2815 version = SSL3_VERSION;
2816 }
2817
2818 /* Check against min_version. */
2819 if (version != 0 && s->min_version != 0 && version < s->min_version) {
2820 return 0;
2821 }
2822 return version;
2823 }
2824}
2825
2826uint16_t ssl3_get_max_client_version(SSL *s) {
David Benjamin123a8fd2015-04-26 14:16:41 -04002827 uint32_t options = s->options;
Adam Langleyfcf25832014-12-18 17:42:32 -08002828 uint16_t version = 0;
2829
2830 /* OpenSSL's API for controlling versions entails blacklisting individual
2831 * protocols. This has two problems. First, on the client, the protocol can
2832 * only express a contiguous range of versions. Second, a library consumer
2833 * trying to set a maximum version cannot disable protocol versions that get
2834 * added in a future version of the library.
2835 *
2836 * To account for both of these, OpenSSL interprets the client-side bitmask
2837 * as a min/max range by picking the lowest contiguous non-empty range of
2838 * enabled protocols. Note that this means it is impossible to set a maximum
2839 * version of TLS 1.2 in a future-proof way.
2840 *
2841 * By this scheme, the maximum version is the lowest version V such that V is
2842 * enabled and V+1 is disabled or unimplemented. */
2843 if (SSL_IS_DTLS(s)) {
2844 if (!(options & SSL_OP_NO_DTLSv1_2)) {
2845 version = DTLS1_2_VERSION;
2846 }
2847 if (!(options & SSL_OP_NO_DTLSv1) && (options & SSL_OP_NO_DTLSv1_2)) {
2848 version = DTLS1_VERSION;
2849 }
2850 if (s->max_version != 0 && version < s->max_version) {
2851 version = s->max_version;
2852 }
2853 } else {
2854 if (!(options & SSL_OP_NO_TLSv1_2)) {
2855 version = TLS1_2_VERSION;
2856 }
2857 if (!(options & SSL_OP_NO_TLSv1_1) && (options & SSL_OP_NO_TLSv1_2)) {
2858 version = TLS1_1_VERSION;
2859 }
2860 if (!(options & SSL_OP_NO_TLSv1) && (options & SSL_OP_NO_TLSv1_1)) {
2861 version = TLS1_VERSION;
2862 }
2863 if (!(options & SSL_OP_NO_SSLv3) && (options & SSL_OP_NO_TLSv1)) {
2864 version = SSL3_VERSION;
2865 }
2866 if (s->max_version != 0 && version > s->max_version) {
2867 version = s->max_version;
2868 }
2869 }
2870
2871 return version;
2872}
2873
2874int ssl3_is_version_enabled(SSL *s, uint16_t version) {
2875 if (SSL_IS_DTLS(s)) {
2876 if (s->max_version != 0 && version < s->max_version) {
2877 return 0;
2878 }
2879 if (s->min_version != 0 && version > s->min_version) {
2880 return 0;
2881 }
2882
2883 switch (version) {
2884 case DTLS1_VERSION:
2885 return !(s->options & SSL_OP_NO_DTLSv1);
2886
2887 case DTLS1_2_VERSION:
2888 return !(s->options & SSL_OP_NO_DTLSv1_2);
2889
2890 default:
2891 return 0;
2892 }
2893 } else {
2894 if (s->max_version != 0 && version > s->max_version) {
2895 return 0;
2896 }
2897 if (s->min_version != 0 && version < s->min_version) {
2898 return 0;
2899 }
2900
2901 switch (version) {
2902 case SSL3_VERSION:
2903 return !(s->options & SSL_OP_NO_SSLv3);
2904
2905 case TLS1_VERSION:
2906 return !(s->options & SSL_OP_NO_TLSv1);
2907
2908 case TLS1_1_VERSION:
2909 return !(s->options & SSL_OP_NO_TLSv1_1);
2910
2911 case TLS1_2_VERSION:
2912 return !(s->options & SSL_OP_NO_TLSv1_2);
2913
2914 default:
2915 return 0;
2916 }
2917 }
2918}
2919
David Benjaminea72bd02014-12-21 21:27:41 -05002920uint16_t ssl3_version_from_wire(SSL *s, uint16_t wire_version) {
2921 if (!SSL_IS_DTLS(s)) {
2922 return wire_version;
2923 }
2924
2925 uint16_t tls_version = ~wire_version;
2926 uint16_t version = tls_version + 0x0201;
2927 /* If either component overflowed, clamp it so comparisons still work. */
2928 if ((version >> 8) < (tls_version >> 8)) {
2929 version = 0xff00 | (version & 0xff);
2930 }
2931 if ((version & 0xff) < (tls_version & 0xff)) {
2932 version = (version & 0xff00) | 0xff;
2933 }
2934 /* DTLS 1.0 maps to TLS 1.1, not TLS 1.0. */
2935 if (version == TLS1_VERSION) {
2936 version = TLS1_1_VERSION;
2937 }
2938 return version;
2939}
2940
Adam Langleyfcf25832014-12-18 17:42:32 -08002941int SSL_cache_hit(SSL *s) { return s->hit; }
2942
2943int SSL_is_server(SSL *s) { return s->server; }
2944
Adam Langley524e7172015-02-20 16:04:00 -08002945void SSL_CTX_set_dos_protection_cb(
2946 SSL_CTX *ctx, int (*cb)(const struct ssl_early_callback_ctx *)) {
2947 ctx->dos_protection_cb = cb;
2948}
2949
Adam Langleyfcf25832014-12-18 17:42:32 -08002950void SSL_enable_fastradio_padding(SSL *s, char on_off) {
2951 s->fastradio_padding = on_off;
2952}
Adam Langley44972942015-02-20 13:43:23 -08002953
David Benjaminb16346b2015-04-08 19:16:58 -04002954void SSL_set_reject_peer_renegotiations(SSL *s, int reject) {
2955 s->reject_peer_renegotiations = !!reject;
2956}
2957
Adam Langley44972942015-02-20 13:43:23 -08002958const SSL_CIPHER *SSL_get_cipher_by_value(uint16_t value) {
2959 return ssl3_get_cipher_by_value(value);
2960}
Adam Langley3f92d212015-02-20 15:32:52 -08002961
2962int SSL_get_rc4_state(const SSL *ssl, const RC4_KEY **read_key,
2963 const RC4_KEY **write_key) {
2964 if (ssl->aead_read_ctx == NULL || ssl->aead_write_ctx == NULL) {
2965 return 0;
2966 }
2967
2968 return EVP_AEAD_CTX_get_rc4_state(&ssl->aead_read_ctx->ctx, read_key) &&
2969 EVP_AEAD_CTX_get_rc4_state(&ssl->aead_write_ctx->ctx, write_key);
2970}
David Benjaminda881e92015-04-26 14:45:04 -04002971
2972int SSL_CTX_sess_connect(const SSL_CTX *ctx) { return 0; }
2973int SSL_CTX_sess_connect_good(const SSL_CTX *ctx) { return 0; }
2974int SSL_CTX_sess_connect_renegotiate(const SSL_CTX *ctx) { return 0; }
2975int SSL_CTX_sess_accept(const SSL_CTX *ctx) { return 0; }
2976int SSL_CTX_sess_accept_renegotiate(const SSL_CTX *ctx) { return 0; }
2977int SSL_CTX_sess_accept_good(const SSL_CTX *ctx) { return 0; }
2978int SSL_CTX_sess_hits(const SSL_CTX *ctx) { return 0; }
2979int SSL_CTX_sess_cb_hits(const SSL_CTX *ctx) { return 0; }
2980int SSL_CTX_sess_misses(const SSL_CTX *ctx) { return 0; }
2981int SSL_CTX_sess_timeouts(const SSL_CTX *ctx) { return 0; }
2982int SSL_CTX_sess_cache_full(const SSL_CTX *ctx) { return 0; }