blob: 6c8e2c999193111117907846e1d34ef59bbd0ada [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) {
David Benjamin9a10f8f2015-05-05 22:22:40 -0400356 s->tlsext_channel_id_private =
357 EVP_PKEY_up_ref(ctx->tlsext_channel_id_private);
David Benjamin02ddbfd2015-01-11 13:09:11 -0500358 }
359
Adam Langleyfcf25832014-12-18 17:42:32 -0800360 s->signed_cert_timestamps_enabled = s->ctx->signed_cert_timestamps_enabled;
361 s->ocsp_stapling_enabled = s->ctx->ocsp_stapling_enabled;
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200362
Adam Langleyfcf25832014-12-18 17:42:32 -0800363 return s;
364
Adam Langley95c29f32014-06-20 12:00:00 -0700365err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400366 SSL_free(s);
Adam Langleyfcf25832014-12-18 17:42:32 -0800367 OPENSSL_PUT_ERROR(SSL, SSL_new, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700368
Adam Langleyfcf25832014-12-18 17:42:32 -0800369 return NULL;
370}
Adam Langley95c29f32014-06-20 12:00:00 -0700371
Adam Langleyfcf25832014-12-18 17:42:32 -0800372int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
373 unsigned int sid_ctx_len) {
374 if (sid_ctx_len > sizeof ctx->sid_ctx) {
375 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_session_id_context,
376 SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
377 return 0;
378 }
379 ctx->sid_ctx_length = sid_ctx_len;
380 memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700381
Adam Langleyfcf25832014-12-18 17:42:32 -0800382 return 1;
383}
Adam Langley95c29f32014-06-20 12:00:00 -0700384
Adam Langleyfcf25832014-12-18 17:42:32 -0800385int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
386 unsigned int sid_ctx_len) {
387 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
388 OPENSSL_PUT_ERROR(SSL, SSL_set_session_id_context,
389 SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
390 return 0;
391 }
392 ssl->sid_ctx_length = sid_ctx_len;
393 memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700394
Adam Langleyfcf25832014-12-18 17:42:32 -0800395 return 1;
396}
Adam Langley95c29f32014-06-20 12:00:00 -0700397
Adam Langleyfcf25832014-12-18 17:42:32 -0800398int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb) {
399 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
400 ctx->generate_session_id = cb;
401 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
402 return 1;
403}
Adam Langley95c29f32014-06-20 12:00:00 -0700404
Adam Langleyfcf25832014-12-18 17:42:32 -0800405int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800406 ssl->generate_session_id = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -0800407 return 1;
408}
Adam Langley95c29f32014-06-20 12:00:00 -0700409
Adam Langleyfcf25832014-12-18 17:42:32 -0800410int SSL_has_matching_session_id(const SSL *ssl, const uint8_t *id,
411 unsigned int id_len) {
412 /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how we
413 * can "construct" a session to give us the desired check - ie. to find if
414 * there's a session in the hash table that would conflict with any new
415 * session built out of this id/id_len and the ssl_version in use by this
416 * SSL. */
417 SSL_SESSION r, *p;
Adam Langley95c29f32014-06-20 12:00:00 -0700418
Adam Langleyfcf25832014-12-18 17:42:32 -0800419 if (id_len > sizeof r.session_id) {
420 return 0;
421 }
Adam Langley95c29f32014-06-20 12:00:00 -0700422
Adam Langleyfcf25832014-12-18 17:42:32 -0800423 r.ssl_version = ssl->version;
424 r.session_id_length = id_len;
425 memcpy(r.session_id, id, id_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700426
Adam Langleyfcf25832014-12-18 17:42:32 -0800427 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
428 p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r);
429 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
430 return p != NULL;
431}
Adam Langley95c29f32014-06-20 12:00:00 -0700432
Adam Langleyfcf25832014-12-18 17:42:32 -0800433int SSL_CTX_set_purpose(SSL_CTX *s, int purpose) {
434 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
435}
436
437int SSL_set_purpose(SSL *s, int purpose) {
438 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
439}
440
441int SSL_CTX_set_trust(SSL_CTX *s, int trust) {
442 return X509_VERIFY_PARAM_set_trust(s->param, trust);
443}
444
445int SSL_set_trust(SSL *s, int trust) {
446 return X509_VERIFY_PARAM_set_trust(s->param, trust);
447}
448
449int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm) {
450 return X509_VERIFY_PARAM_set1(ctx->param, vpm);
451}
452
453int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm) {
454 return X509_VERIFY_PARAM_set1(ssl->param, vpm);
455}
Adam Langley95c29f32014-06-20 12:00:00 -0700456
Adam Langley858a88d2014-06-20 12:00:00 -0700457void ssl_cipher_preference_list_free(
Adam Langleyfcf25832014-12-18 17:42:32 -0800458 struct ssl_cipher_preference_list_st *cipher_list) {
David Benjamin5d1ec732015-04-22 13:38:00 -0400459 if (cipher_list == NULL) {
460 return;
461 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800462 sk_SSL_CIPHER_free(cipher_list->ciphers);
463 OPENSSL_free(cipher_list->in_group_flags);
464 OPENSSL_free(cipher_list);
465}
Adam Langley858a88d2014-06-20 12:00:00 -0700466
Adam Langleyfcf25832014-12-18 17:42:32 -0800467struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_dup(
468 struct ssl_cipher_preference_list_st *cipher_list) {
469 struct ssl_cipher_preference_list_st *ret = NULL;
470 size_t n = sk_SSL_CIPHER_num(cipher_list->ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700471
Adam Langleyfcf25832014-12-18 17:42:32 -0800472 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
473 if (!ret) {
474 goto err;
475 }
476
477 ret->ciphers = NULL;
478 ret->in_group_flags = NULL;
479 ret->ciphers = sk_SSL_CIPHER_dup(cipher_list->ciphers);
480 if (!ret->ciphers) {
481 goto err;
482 }
483 ret->in_group_flags = BUF_memdup(cipher_list->in_group_flags, n);
484 if (!ret->in_group_flags) {
485 goto err;
486 }
487
488 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -0700489
490err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400491 ssl_cipher_preference_list_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -0800492 return NULL;
493}
Adam Langley858a88d2014-06-20 12:00:00 -0700494
Adam Langleyfcf25832014-12-18 17:42:32 -0800495struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_from_ciphers(
David Benjamin60da0cd2015-05-03 15:21:28 -0400496 STACK_OF(SSL_CIPHER) *ciphers) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800497 struct ssl_cipher_preference_list_st *ret = NULL;
498 size_t n = sk_SSL_CIPHER_num(ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700499
Adam Langleyfcf25832014-12-18 17:42:32 -0800500 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
501 if (!ret) {
502 goto err;
503 }
504 ret->ciphers = NULL;
505 ret->in_group_flags = NULL;
506 ret->ciphers = sk_SSL_CIPHER_dup(ciphers);
507 if (!ret->ciphers) {
508 goto err;
509 }
510 ret->in_group_flags = OPENSSL_malloc(n);
511 if (!ret->in_group_flags) {
512 goto err;
513 }
514 memset(ret->in_group_flags, 0, n);
515 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -0700516
517err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400518 ssl_cipher_preference_list_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -0800519 return NULL;
520}
Adam Langley858a88d2014-06-20 12:00:00 -0700521
Adam Langleyfcf25832014-12-18 17:42:32 -0800522X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) { return ctx->param; }
Adam Langley95c29f32014-06-20 12:00:00 -0700523
Adam Langleyfcf25832014-12-18 17:42:32 -0800524X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) { return ssl->param; }
Adam Langley95c29f32014-06-20 12:00:00 -0700525
Adam Langleyfcf25832014-12-18 17:42:32 -0800526void SSL_certs_clear(SSL *s) { ssl_cert_clear_certs(s->cert); }
Adam Langley95c29f32014-06-20 12:00:00 -0700527
Adam Langleyfcf25832014-12-18 17:42:32 -0800528void SSL_free(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800529 if (s == NULL) {
530 return;
531 }
Adam Langley95c29f32014-06-20 12:00:00 -0700532
David Benjamin2755a3e2015-04-22 16:17:58 -0400533 X509_VERIFY_PARAM_free(s->param);
Adam Langley95c29f32014-06-20 12:00:00 -0700534
David Benjamin9f33fc62015-04-15 17:29:53 -0400535 CRYPTO_free_ex_data(&g_ex_data_class_ssl, s, &s->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700536
Adam Langleyfcf25832014-12-18 17:42:32 -0800537 if (s->bbio != NULL) {
538 /* If the buffering BIO is in place, pop it off */
539 if (s->bbio == s->wbio) {
540 s->wbio = BIO_pop(s->wbio);
541 }
542 BIO_free(s->bbio);
543 s->bbio = NULL;
544 }
Adam Langley95c29f32014-06-20 12:00:00 -0700545
David Benjamin2755a3e2015-04-22 16:17:58 -0400546 int free_wbio = s->wbio != s->rbio;
547 BIO_free_all(s->rbio);
548 if (free_wbio) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800549 BIO_free_all(s->wbio);
550 }
Adam Langley95c29f32014-06-20 12:00:00 -0700551
David Benjamin2755a3e2015-04-22 16:17:58 -0400552 BUF_MEM_free(s->init_buf);
Adam Langley95c29f32014-06-20 12:00:00 -0700553
Adam Langleyfcf25832014-12-18 17:42:32 -0800554 /* add extra stuff */
David Benjamin2755a3e2015-04-22 16:17:58 -0400555 ssl_cipher_preference_list_free(s->cipher_list);
556 sk_SSL_CIPHER_free(s->cipher_list_by_id);
Adam Langley95c29f32014-06-20 12:00:00 -0700557
David Benjamin2755a3e2015-04-22 16:17:58 -0400558 ssl_clear_bad_session(s);
559 SSL_SESSION_free(s->session);
Adam Langley95c29f32014-06-20 12:00:00 -0700560
Adam Langleyfcf25832014-12-18 17:42:32 -0800561 ssl_clear_cipher_ctx(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700562
David Benjamin2755a3e2015-04-22 16:17:58 -0400563 ssl_cert_free(s->cert);
Adam Langley0289c732014-06-20 12:00:00 -0700564
David Benjamin2755a3e2015-04-22 16:17:58 -0400565 OPENSSL_free(s->tlsext_hostname);
566 SSL_CTX_free(s->initial_ctx);
567 OPENSSL_free(s->tlsext_ecpointformatlist);
568 OPENSSL_free(s->tlsext_ellipticcurvelist);
569 OPENSSL_free(s->alpn_client_proto_list);
570 EVP_PKEY_free(s->tlsext_channel_id_private);
571 OPENSSL_free(s->psk_identity_hint);
572 sk_X509_NAME_pop_free(s->client_CA, X509_NAME_free);
573 OPENSSL_free(s->next_proto_negotiated);
574 sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);
Adam Langley95c29f32014-06-20 12:00:00 -0700575
Adam Langleyfcf25832014-12-18 17:42:32 -0800576 if (s->method != NULL) {
577 s->method->ssl_free(s);
578 }
David Benjamin2755a3e2015-04-22 16:17:58 -0400579 SSL_CTX_free(s->ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700580
Adam Langleyfcf25832014-12-18 17:42:32 -0800581 OPENSSL_free(s);
582}
Adam Langley95c29f32014-06-20 12:00:00 -0700583
Adam Langleyfcf25832014-12-18 17:42:32 -0800584void SSL_set_bio(SSL *s, BIO *rbio, BIO *wbio) {
585 /* If the output buffering BIO is still in place, remove it. */
586 if (s->bbio != NULL) {
587 if (s->wbio == s->bbio) {
588 s->wbio = s->wbio->next_bio;
589 s->bbio->next_bio = NULL;
590 }
591 }
Adam Langley95c29f32014-06-20 12:00:00 -0700592
David Benjamin2755a3e2015-04-22 16:17:58 -0400593 if (s->rbio != rbio) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800594 BIO_free_all(s->rbio);
595 }
David Benjamin2755a3e2015-04-22 16:17:58 -0400596 if (s->wbio != wbio && s->rbio != s->wbio) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800597 BIO_free_all(s->wbio);
598 }
599 s->rbio = rbio;
600 s->wbio = wbio;
601}
Adam Langley95c29f32014-06-20 12:00:00 -0700602
Adam Langleyfcf25832014-12-18 17:42:32 -0800603BIO *SSL_get_rbio(const SSL *s) { return s->rbio; }
Adam Langley95c29f32014-06-20 12:00:00 -0700604
Adam Langleyfcf25832014-12-18 17:42:32 -0800605BIO *SSL_get_wbio(const SSL *s) { return s->wbio; }
Adam Langley95c29f32014-06-20 12:00:00 -0700606
Adam Langleyfcf25832014-12-18 17:42:32 -0800607int SSL_get_fd(const SSL *s) { return SSL_get_rfd(s); }
Adam Langley95c29f32014-06-20 12:00:00 -0700608
Adam Langleyfcf25832014-12-18 17:42:32 -0800609int SSL_get_rfd(const SSL *s) {
610 int ret = -1;
611 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700612
Adam Langleyfcf25832014-12-18 17:42:32 -0800613 b = SSL_get_rbio(s);
614 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
615 if (r != NULL) {
616 BIO_get_fd(r, &ret);
617 }
618 return ret;
619}
Adam Langley95c29f32014-06-20 12:00:00 -0700620
Adam Langleyfcf25832014-12-18 17:42:32 -0800621int SSL_get_wfd(const SSL *s) {
622 int ret = -1;
623 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700624
Adam Langleyfcf25832014-12-18 17:42:32 -0800625 b = SSL_get_wbio(s);
626 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
627 if (r != NULL) {
628 BIO_get_fd(r, &ret);
629 }
Adam Langley95c29f32014-06-20 12:00:00 -0700630
Adam Langleyfcf25832014-12-18 17:42:32 -0800631 return ret;
632}
Adam Langley95c29f32014-06-20 12:00:00 -0700633
Adam Langleyfcf25832014-12-18 17:42:32 -0800634int SSL_set_fd(SSL *s, int fd) {
635 int ret = 0;
636 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700637
Adam Langleyfcf25832014-12-18 17:42:32 -0800638 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700639
Adam Langleyfcf25832014-12-18 17:42:32 -0800640 if (bio == NULL) {
641 OPENSSL_PUT_ERROR(SSL, SSL_set_fd, ERR_R_BUF_LIB);
642 goto err;
643 }
644 BIO_set_fd(bio, fd, BIO_NOCLOSE);
645 SSL_set_bio(s, bio, bio);
646 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700647
Adam Langley95c29f32014-06-20 12:00:00 -0700648err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800649 return ret;
650}
Adam Langley95c29f32014-06-20 12:00:00 -0700651
Adam Langleyfcf25832014-12-18 17:42:32 -0800652int SSL_set_wfd(SSL *s, int fd) {
653 int ret = 0;
654 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700655
Adam Langleyfcf25832014-12-18 17:42:32 -0800656 if (s->rbio == NULL || BIO_method_type(s->rbio) != BIO_TYPE_FD ||
657 (int)BIO_get_fd(s->rbio, NULL) != fd) {
658 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700659
Adam Langleyfcf25832014-12-18 17:42:32 -0800660 if (bio == NULL) {
661 OPENSSL_PUT_ERROR(SSL, SSL_set_wfd, ERR_R_BUF_LIB);
662 goto err;
663 }
664 BIO_set_fd(bio, fd, BIO_NOCLOSE);
665 SSL_set_bio(s, SSL_get_rbio(s), bio);
666 } else {
667 SSL_set_bio(s, SSL_get_rbio(s), SSL_get_rbio(s));
668 }
669
670 ret = 1;
671
Adam Langley95c29f32014-06-20 12:00:00 -0700672err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800673 return ret;
674}
Adam Langley95c29f32014-06-20 12:00:00 -0700675
Adam Langleyfcf25832014-12-18 17:42:32 -0800676int SSL_set_rfd(SSL *s, int fd) {
677 int ret = 0;
678 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700679
Adam Langleyfcf25832014-12-18 17:42:32 -0800680 if (s->wbio == NULL || BIO_method_type(s->wbio) != BIO_TYPE_FD ||
681 (int)BIO_get_fd(s->wbio, NULL) != fd) {
682 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700683
Adam Langleyfcf25832014-12-18 17:42:32 -0800684 if (bio == NULL) {
685 OPENSSL_PUT_ERROR(SSL, SSL_set_rfd, ERR_R_BUF_LIB);
686 goto err;
687 }
688 BIO_set_fd(bio, fd, BIO_NOCLOSE);
689 SSL_set_bio(s, bio, SSL_get_wbio(s));
690 } else {
691 SSL_set_bio(s, SSL_get_wbio(s), SSL_get_wbio(s));
692 }
693 ret = 1;
694
Adam Langley95c29f32014-06-20 12:00:00 -0700695err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800696 return ret;
697}
Adam Langley95c29f32014-06-20 12:00:00 -0700698
699/* return length of latest Finished message we sent, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -0800700size_t SSL_get_finished(const SSL *s, void *buf, size_t count) {
701 size_t ret = 0;
702
703 if (s->s3 != NULL) {
704 ret = s->s3->tmp.finish_md_len;
705 if (count > ret) {
706 count = ret;
707 }
708 memcpy(buf, s->s3->tmp.finish_md, count);
709 }
710
711 return ret;
712}
Adam Langley95c29f32014-06-20 12:00:00 -0700713
714/* return length of latest Finished message we expected, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -0800715size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count) {
716 size_t ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700717
Adam Langleyfcf25832014-12-18 17:42:32 -0800718 if (s->s3 != NULL) {
719 ret = s->s3->tmp.peer_finish_md_len;
720 if (count > ret) {
721 count = ret;
722 }
723 memcpy(buf, s->s3->tmp.peer_finish_md, count);
724 }
Adam Langley95c29f32014-06-20 12:00:00 -0700725
Adam Langleyfcf25832014-12-18 17:42:32 -0800726 return ret;
727}
Adam Langley95c29f32014-06-20 12:00:00 -0700728
Adam Langleyfcf25832014-12-18 17:42:32 -0800729int SSL_get_verify_mode(const SSL *s) { return s->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -0700730
Adam Langleyfcf25832014-12-18 17:42:32 -0800731int SSL_get_verify_depth(const SSL *s) {
732 return X509_VERIFY_PARAM_get_depth(s->param);
733}
Adam Langley95c29f32014-06-20 12:00:00 -0700734
Adam Langleyfcf25832014-12-18 17:42:32 -0800735int (*SSL_get_verify_callback(const SSL *s))(int, X509_STORE_CTX *) {
736 return s->verify_callback;
737}
Adam Langley95c29f32014-06-20 12:00:00 -0700738
Adam Langleyfcf25832014-12-18 17:42:32 -0800739int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) { return ctx->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -0700740
Adam Langleyfcf25832014-12-18 17:42:32 -0800741int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) {
742 return X509_VERIFY_PARAM_get_depth(ctx->param);
743}
Adam Langley95c29f32014-06-20 12:00:00 -0700744
Adam Langleyfcf25832014-12-18 17:42:32 -0800745int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int, X509_STORE_CTX *) {
746 return ctx->default_verify_callback;
747}
Adam Langley95c29f32014-06-20 12:00:00 -0700748
Adam Langleyfcf25832014-12-18 17:42:32 -0800749void SSL_set_verify(SSL *s, int mode,
750 int (*callback)(int ok, X509_STORE_CTX *ctx)) {
751 s->verify_mode = mode;
752 if (callback != NULL) {
753 s->verify_callback = callback;
754 }
755}
Adam Langley95c29f32014-06-20 12:00:00 -0700756
Adam Langleyfcf25832014-12-18 17:42:32 -0800757void SSL_set_verify_depth(SSL *s, int depth) {
758 X509_VERIFY_PARAM_set_depth(s->param, depth);
759}
Adam Langley95c29f32014-06-20 12:00:00 -0700760
David Benjamin61ecccf2015-05-05 09:44:51 -0400761int SSL_CTX_get_read_ahead(const SSL_CTX *ctx) { return ctx->read_ahead; }
Adam Langley95c29f32014-06-20 12:00:00 -0700762
Adam Langleyfcf25832014-12-18 17:42:32 -0800763int SSL_get_read_ahead(const SSL *s) { return s->read_ahead; }
Adam Langley95c29f32014-06-20 12:00:00 -0700764
David Benjamin61ecccf2015-05-05 09:44:51 -0400765void SSL_CTX_set_read_ahead(SSL_CTX *ctx, int yes) { ctx->read_ahead = !!yes; }
766
767void SSL_set_read_ahead(SSL *s, int yes) { s->read_ahead = !!yes; }
768
Adam Langleyfcf25832014-12-18 17:42:32 -0800769int SSL_pending(const SSL *s) {
770 /* SSL_pending cannot work properly if read-ahead is enabled
771 * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)), and it is
772 * impossible to fix since SSL_pending cannot report errors that may be
773 * observed while scanning the new data. (Note that SSL_pending() is often
774 * used as a boolean value, so we'd better not return -1.). */
775 return s->method->ssl_pending(s);
776}
Adam Langley95c29f32014-06-20 12:00:00 -0700777
Adam Langleyfcf25832014-12-18 17:42:32 -0800778X509 *SSL_get_peer_certificate(const SSL *s) {
779 X509 *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700780
Adam Langleyfcf25832014-12-18 17:42:32 -0800781 if (s == NULL || s->session == NULL) {
782 r = NULL;
783 } else {
784 r = s->session->peer;
785 }
Adam Langley95c29f32014-06-20 12:00:00 -0700786
Adam Langleyfcf25832014-12-18 17:42:32 -0800787 if (r == NULL) {
788 return NULL;
789 }
Adam Langley95c29f32014-06-20 12:00:00 -0700790
Adam Langleyfcf25832014-12-18 17:42:32 -0800791 return X509_up_ref(r);
792}
793
David Benjamin60da0cd2015-05-03 15:21:28 -0400794STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s) {
795 STACK_OF(X509) *r;
Adam Langleyfcf25832014-12-18 17:42:32 -0800796
797 if (s == NULL || s->session == NULL || s->session->sess_cert == NULL) {
798 r = NULL;
799 } else {
800 r = s->session->sess_cert->cert_chain;
801 }
802
803 /* If we are a client, cert_chain includes the peer's own certificate; if we
804 * are a server, it does not. */
805 return r;
806}
Adam Langley95c29f32014-06-20 12:00:00 -0700807
Adam Langley95c29f32014-06-20 12:00:00 -0700808/* Fix this so it checks all the valid key/cert options */
Adam Langleyfcf25832014-12-18 17:42:32 -0800809int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
810 if (ctx == NULL || ctx->cert == NULL || ctx->cert->key->x509 == NULL) {
811 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key,
812 SSL_R_NO_CERTIFICATE_ASSIGNED);
813 return 0;
814 }
815
816 if (ctx->cert->key->privatekey == NULL) {
817 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key,
818 SSL_R_NO_PRIVATE_KEY_ASSIGNED);
819 return 0;
820 }
821
822 return X509_check_private_key(ctx->cert->key->x509,
823 ctx->cert->key->privatekey);
824}
Adam Langley95c29f32014-06-20 12:00:00 -0700825
826/* Fix this function so that it takes an optional type parameter */
Adam Langleyfcf25832014-12-18 17:42:32 -0800827int SSL_check_private_key(const SSL *ssl) {
828 if (ssl == NULL) {
829 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, ERR_R_PASSED_NULL_PARAMETER);
830 return 0;
831 }
Adam Langley95c29f32014-06-20 12:00:00 -0700832
Adam Langleyfcf25832014-12-18 17:42:32 -0800833 if (ssl->cert == NULL) {
834 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
835 SSL_R_NO_CERTIFICATE_ASSIGNED);
836 return 0;
837 }
Adam Langley95c29f32014-06-20 12:00:00 -0700838
Adam Langleyfcf25832014-12-18 17:42:32 -0800839 if (ssl->cert->key->x509 == NULL) {
840 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
841 SSL_R_NO_CERTIFICATE_ASSIGNED);
842 return 0;
843 }
David Benjamin0b145c22014-11-26 20:10:09 -0500844
Adam Langleyfcf25832014-12-18 17:42:32 -0800845 if (ssl->cert->key->privatekey == NULL) {
846 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
847 SSL_R_NO_PRIVATE_KEY_ASSIGNED);
848 return 0;
849 }
Adam Langley95c29f32014-06-20 12:00:00 -0700850
Adam Langleyfcf25832014-12-18 17:42:32 -0800851 return X509_check_private_key(ssl->cert->key->x509,
852 ssl->cert->key->privatekey);
853}
Adam Langley95c29f32014-06-20 12:00:00 -0700854
Adam Langleyfcf25832014-12-18 17:42:32 -0800855int SSL_accept(SSL *s) {
856 if (s->handshake_func == 0) {
857 /* Not properly initialized yet */
858 SSL_set_accept_state(s);
859 }
David Benjamin0b145c22014-11-26 20:10:09 -0500860
Adam Langleyfcf25832014-12-18 17:42:32 -0800861 if (s->handshake_func != s->method->ssl_accept) {
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +0100862 OPENSSL_PUT_ERROR(SSL, SSL_accept, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800863 return -1;
864 }
Adam Langley95c29f32014-06-20 12:00:00 -0700865
Adam Langleyfcf25832014-12-18 17:42:32 -0800866 return s->handshake_func(s);
867}
Adam Langley95c29f32014-06-20 12:00:00 -0700868
Adam Langleyfcf25832014-12-18 17:42:32 -0800869int SSL_connect(SSL *s) {
870 if (s->handshake_func == 0) {
871 /* Not properly initialized yet */
872 SSL_set_connect_state(s);
873 }
Adam Langley95c29f32014-06-20 12:00:00 -0700874
Adam Langleyfcf25832014-12-18 17:42:32 -0800875 if (s->handshake_func != s->method->ssl_connect) {
876 OPENSSL_PUT_ERROR(SSL, SSL_connect, ERR_R_INTERNAL_ERROR);
877 return -1;
878 }
Adam Langley95c29f32014-06-20 12:00:00 -0700879
Adam Langleyfcf25832014-12-18 17:42:32 -0800880 return s->handshake_func(s);
881}
Adam Langley95c29f32014-06-20 12:00:00 -0700882
Adam Langleyfcf25832014-12-18 17:42:32 -0800883long SSL_get_default_timeout(const SSL *s) {
884 return SSL_DEFAULT_SESSION_TIMEOUT;
885}
Adam Langley95c29f32014-06-20 12:00:00 -0700886
Adam Langleyfcf25832014-12-18 17:42:32 -0800887int SSL_read(SSL *s, void *buf, int num) {
888 if (s->handshake_func == 0) {
889 OPENSSL_PUT_ERROR(SSL, SSL_read, SSL_R_UNINITIALIZED);
890 return -1;
891 }
Adam Langley95c29f32014-06-20 12:00:00 -0700892
Adam Langleyfcf25832014-12-18 17:42:32 -0800893 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
894 s->rwstate = SSL_NOTHING;
895 return 0;
896 }
Adam Langley95c29f32014-06-20 12:00:00 -0700897
Adam Langleyfcf25832014-12-18 17:42:32 -0800898 return s->method->ssl_read(s, buf, num);
899}
Adam Langley95c29f32014-06-20 12:00:00 -0700900
Adam Langleyfcf25832014-12-18 17:42:32 -0800901int SSL_peek(SSL *s, void *buf, int num) {
902 if (s->handshake_func == 0) {
903 OPENSSL_PUT_ERROR(SSL, SSL_peek, SSL_R_UNINITIALIZED);
904 return -1;
905 }
Adam Langley95c29f32014-06-20 12:00:00 -0700906
Adam Langleyfcf25832014-12-18 17:42:32 -0800907 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
908 return 0;
909 }
Adam Langley95c29f32014-06-20 12:00:00 -0700910
Adam Langleyfcf25832014-12-18 17:42:32 -0800911 return s->method->ssl_peek(s, buf, num);
912}
Adam Langley95c29f32014-06-20 12:00:00 -0700913
Adam Langleyfcf25832014-12-18 17:42:32 -0800914int SSL_write(SSL *s, const void *buf, int num) {
915 if (s->handshake_func == 0) {
916 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_UNINITIALIZED);
917 return -1;
918 }
Adam Langley95c29f32014-06-20 12:00:00 -0700919
Adam Langleyfcf25832014-12-18 17:42:32 -0800920 if (s->shutdown & SSL_SENT_SHUTDOWN) {
921 s->rwstate = SSL_NOTHING;
922 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_PROTOCOL_IS_SHUTDOWN);
923 return -1;
924 }
Adam Langley95c29f32014-06-20 12:00:00 -0700925
Adam Langleyfcf25832014-12-18 17:42:32 -0800926 return s->method->ssl_write(s, buf, num);
927}
Adam Langley95c29f32014-06-20 12:00:00 -0700928
Adam Langleyfcf25832014-12-18 17:42:32 -0800929int SSL_shutdown(SSL *s) {
930 /* Note that this function behaves differently from what one might expect.
931 * Return values are 0 for no success (yet), 1 for success; but calling it
932 * once is usually not enough, even if blocking I/O is used (see
933 * ssl3_shutdown). */
Adam Langley95c29f32014-06-20 12:00:00 -0700934
Adam Langleyfcf25832014-12-18 17:42:32 -0800935 if (s->handshake_func == 0) {
936 OPENSSL_PUT_ERROR(SSL, SSL_shutdown, SSL_R_UNINITIALIZED);
937 return -1;
938 }
Adam Langley95c29f32014-06-20 12:00:00 -0700939
Adam Langleyfcf25832014-12-18 17:42:32 -0800940 if (!SSL_in_init(s)) {
941 return s->method->ssl_shutdown(s);
942 }
Adam Langley95c29f32014-06-20 12:00:00 -0700943
Adam Langleyfcf25832014-12-18 17:42:32 -0800944 return 1;
945}
Adam Langley95c29f32014-06-20 12:00:00 -0700946
Adam Langleyfcf25832014-12-18 17:42:32 -0800947int SSL_renegotiate(SSL *s) {
David Benjamin2cdace92015-01-24 13:30:02 -0500948 if (SSL_IS_DTLS(s)) {
949 /* Renegotiation is not supported for DTLS. */
950 OPENSSL_PUT_ERROR(SSL, SSL_renegotiate, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
951 return 0;
952 }
953
Adam Langleyfcf25832014-12-18 17:42:32 -0800954 if (s->renegotiate == 0) {
955 s->renegotiate = 1;
956 }
Adam Langley95c29f32014-06-20 12:00:00 -0700957
Adam Langleyfcf25832014-12-18 17:42:32 -0800958 s->new_session = 1;
959 return s->method->ssl_renegotiate(s);
960}
Adam Langley95c29f32014-06-20 12:00:00 -0700961
Adam Langleyfcf25832014-12-18 17:42:32 -0800962int SSL_renegotiate_pending(SSL *s) {
963 /* becomes true when negotiation is requested; false again once a handshake
964 * has finished */
965 return s->renegotiate != 0;
966}
Adam Langley95c29f32014-06-20 12:00:00 -0700967
David Benjamin61ecccf2015-05-05 09:44:51 -0400968uint32_t SSL_CTX_set_options(SSL_CTX *ctx, uint32_t options) {
969 ctx->options |= options;
970 return ctx->options;
971}
972
973uint32_t SSL_set_options(SSL *ssl, uint32_t options) {
974 ssl->options |= options;
975 return ssl->options;
976}
977
978uint32_t SSL_CTX_clear_options(SSL_CTX *ctx, uint32_t options) {
979 ctx->options &= ~options;
980 return ctx->options;
981}
982
983uint32_t SSL_clear_options(SSL *ssl, uint32_t options) {
984 ssl->options &= ~options;
985 return ssl->options;
986}
987
988uint32_t SSL_CTX_get_options(const SSL_CTX *ctx) { return ctx->options; }
989
990uint32_t SSL_get_options(const SSL *ssl) { return ssl->options; }
991
992uint32_t SSL_CTX_set_mode(SSL_CTX *ctx, uint32_t mode) {
993 ctx->mode |= mode;
994 return ctx->mode;
995}
996
997uint32_t SSL_set_mode(SSL *ssl, uint32_t mode) {
998 ssl->mode |= mode;
999 return ssl->mode;
1000}
1001
1002uint32_t SSL_CTX_clear_mode(SSL_CTX *ctx, uint32_t mode) {
1003 ctx->mode &= ~mode;
1004 return ctx->mode;
1005}
1006
1007uint32_t SSL_clear_mode(SSL *ssl, uint32_t mode) {
1008 ssl->mode &= ~mode;
1009 return ssl->mode;
1010}
1011
1012uint32_t SSL_CTX_get_mode(const SSL_CTX *ctx) { return ctx->mode; }
1013
1014uint32_t SSL_get_mode(const SSL *ssl) { return ssl->mode; }
1015
David Benjamin1d0a1942015-04-26 15:35:35 -04001016size_t SSL_CTX_get_max_cert_list(const SSL_CTX *ctx) {
1017 return ctx->max_cert_list;
1018}
1019
1020void SSL_CTX_set_max_cert_list(SSL_CTX *ctx, size_t max_cert_list) {
1021 if (max_cert_list > kMaxHandshakeSize) {
1022 max_cert_list = kMaxHandshakeSize;
1023 }
1024 ctx->max_cert_list = (uint32_t)max_cert_list;
1025}
1026
1027size_t SSL_get_max_cert_list(const SSL *ssl) {
1028 return ssl->max_cert_list;
1029}
1030
1031void SSL_set_max_cert_list(SSL *ssl, size_t max_cert_list) {
1032 if (max_cert_list > kMaxHandshakeSize) {
1033 max_cert_list = kMaxHandshakeSize;
1034 }
1035 ssl->max_cert_list = (uint32_t)max_cert_list;
1036}
1037
1038void SSL_CTX_set_max_send_fragment(SSL_CTX *ctx, size_t max_send_fragment) {
1039 if (max_send_fragment < 512) {
1040 max_send_fragment = 512;
1041 }
1042 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1043 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1044 }
1045 ctx->max_send_fragment = (uint16_t)max_send_fragment;
1046}
1047
1048void SSL_set_max_send_fragment(SSL *ssl, size_t max_send_fragment) {
1049 if (max_send_fragment < 512) {
1050 max_send_fragment = 512;
1051 }
1052 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1053 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1054 }
1055 ssl->max_send_fragment = (uint16_t)max_send_fragment;
1056}
1057
David Benjamincb9cf792015-05-05 09:46:14 -04001058int SSL_set_mtu(SSL *ssl, unsigned mtu) {
1059 if (!SSL_IS_DTLS(ssl) || mtu < dtls1_min_mtu()) {
1060 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001061 }
David Benjamincb9cf792015-05-05 09:46:14 -04001062 ssl->d1->mtu = mtu;
1063 return 1;
1064}
1065
1066int SSL_get_secure_renegotiation_support(const SSL *ssl) {
1067 return ssl->s3->send_connection_binding;
1068}
1069
1070long SSL_ctrl(SSL *s, int cmd, long larg, void *parg) {
1071 return s->method->ssl_ctrl(s, cmd, larg, parg);
Adam Langleyfcf25832014-12-18 17:42:32 -08001072}
1073
Adam Langleyfcf25832014-12-18 17:42:32 -08001074LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) { return ctx->sessions; }
1075
David Benjamin71f7d3d2015-05-05 09:46:38 -04001076size_t SSL_CTX_sess_number(const SSL_CTX *ctx) {
1077 return lh_SSL_SESSION_num_items(ctx->sessions);
1078}
1079
1080unsigned long SSL_CTX_sess_set_cache_size(SSL_CTX *ctx, unsigned long size) {
1081 unsigned long ret = ctx->session_cache_size;
1082 ctx->session_cache_size = size;
1083 return ret;
1084}
1085
1086unsigned long SSL_CTX_sess_get_cache_size(const SSL_CTX *ctx) {
1087 return ctx->session_cache_size;
1088}
1089
1090int SSL_CTX_set_session_cache_mode(SSL_CTX *ctx, int mode) {
1091 int ret = ctx->session_cache_mode;
1092 ctx->session_cache_mode = mode;
1093 return ret;
1094}
1095
1096int SSL_CTX_get_session_cache_mode(const SSL_CTX *ctx) {
1097 return ctx->session_cache_mode;
1098}
1099
Adam Langleyfcf25832014-12-18 17:42:32 -08001100long SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg) {
David Benjamin71f7d3d2015-05-05 09:46:38 -04001101 return ctx->method->ssl_ctx_ctrl(ctx, cmd, larg, parg);
Adam Langleyfcf25832014-12-18 17:42:32 -08001102}
1103
Adam Langleyfcf25832014-12-18 17:42:32 -08001104int ssl_cipher_id_cmp(const void *in_a, const void *in_b) {
1105 long l;
1106 const SSL_CIPHER *a = in_a;
1107 const SSL_CIPHER *b = in_b;
1108 const long a_id = a->id;
1109 const long b_id = b->id;
1110
1111 l = a_id - b_id;
1112 if (l == 0L) {
1113 return 0;
1114 } else {
1115 return (l > 0) ? 1 : -1;
1116 }
1117}
1118
1119int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **ap, const SSL_CIPHER **bp) {
1120 long l;
1121 const long a_id = (*ap)->id;
1122 const long b_id = (*bp)->id;
1123
1124 l = a_id - b_id;
1125 if (l == 0) {
1126 return 0;
1127 } else {
1128 return (l > 0) ? 1 : -1;
1129 }
1130}
1131
1132/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001133 * preference */
David Benjamin60da0cd2015-05-03 15:21:28 -04001134STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001135 if (s == NULL) {
1136 return NULL;
1137 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001138
Adam Langleyfcf25832014-12-18 17:42:32 -08001139 if (s->cipher_list != NULL) {
1140 return s->cipher_list->ciphers;
1141 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001142
Adam Langleyfcf25832014-12-18 17:42:32 -08001143 if (s->version >= TLS1_1_VERSION && s->ctx != NULL &&
1144 s->ctx->cipher_list_tls11 != NULL) {
1145 return s->ctx->cipher_list_tls11->ciphers;
1146 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001147
Adam Langleyfcf25832014-12-18 17:42:32 -08001148 if (s->ctx != NULL && s->ctx->cipher_list != NULL) {
1149 return s->ctx->cipher_list->ciphers;
1150 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001151
Adam Langleyfcf25832014-12-18 17:42:32 -08001152 return NULL;
1153}
Adam Langley95c29f32014-06-20 12:00:00 -07001154
Adam Langleyfcf25832014-12-18 17:42:32 -08001155/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001156 * algorithm id */
David Benjamin60da0cd2015-05-03 15:21:28 -04001157STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001158 if (s == NULL) {
1159 return NULL;
1160 }
Adam Langley95c29f32014-06-20 12:00:00 -07001161
Adam Langleyfcf25832014-12-18 17:42:32 -08001162 if (s->cipher_list_by_id != NULL) {
1163 return s->cipher_list_by_id;
1164 }
Adam Langley95c29f32014-06-20 12:00:00 -07001165
Adam Langleyfcf25832014-12-18 17:42:32 -08001166 if (s->ctx != NULL && s->ctx->cipher_list_by_id != NULL) {
1167 return s->ctx->cipher_list_by_id;
1168 }
Adam Langley95c29f32014-06-20 12:00:00 -07001169
Adam Langleyfcf25832014-12-18 17:42:32 -08001170 return NULL;
1171}
Adam Langley95c29f32014-06-20 12:00:00 -07001172
Adam Langleyfcf25832014-12-18 17:42:32 -08001173/* The old interface to get the same thing as SSL_get_ciphers() */
1174const char *SSL_get_cipher_list(const SSL *s, int n) {
1175 const SSL_CIPHER *c;
David Benjamin60da0cd2015-05-03 15:21:28 -04001176 STACK_OF(SSL_CIPHER) *sk;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001177
Adam Langleyfcf25832014-12-18 17:42:32 -08001178 if (s == NULL) {
1179 return NULL;
1180 }
Adam Langley95c29f32014-06-20 12:00:00 -07001181
Adam Langleyfcf25832014-12-18 17:42:32 -08001182 sk = SSL_get_ciphers(s);
1183 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
1184 return NULL;
1185 }
Adam Langley95c29f32014-06-20 12:00:00 -07001186
Adam Langleyfcf25832014-12-18 17:42:32 -08001187 c = sk_SSL_CIPHER_value(sk, n);
1188 if (c == NULL) {
1189 return NULL;
1190 }
Adam Langley95c29f32014-06-20 12:00:00 -07001191
Adam Langleyfcf25832014-12-18 17:42:32 -08001192 return c->name;
1193}
David Benjamin5491e3f2014-09-29 19:33:09 -04001194
Adam Langleyfcf25832014-12-18 17:42:32 -08001195/* specify the ciphers to be used by default by the SSL_CTX */
1196int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
1197 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001198
Adam Langleyfcf25832014-12-18 17:42:32 -08001199 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001200 &ctx->cipher_list_by_id, str);
Adam Langleyfcf25832014-12-18 17:42:32 -08001201 /* ssl_create_cipher_list may return an empty stack if it was unable to find
1202 * a cipher matching the given rule string (for example if the rule string
1203 * specifies a cipher which has been disabled). This is not an error as far
1204 * as ssl_create_cipher_list is concerned, and hence ctx->cipher_list and
1205 * ctx->cipher_list_by_id has been updated. */
1206 if (sk == NULL) {
1207 return 0;
1208 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1209 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1210 return 0;
1211 }
Adam Langley95c29f32014-06-20 12:00:00 -07001212
Adam Langleyfcf25832014-12-18 17:42:32 -08001213 return 1;
1214}
David Benjamin39482a12014-07-20 13:30:15 -04001215
Adam Langleyfcf25832014-12-18 17:42:32 -08001216int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str) {
1217 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001218
David Benjamin71f07942015-04-08 02:36:59 -04001219 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str);
Adam Langleyfcf25832014-12-18 17:42:32 -08001220 if (sk == NULL) {
1221 return 0;
1222 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1223 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list_tls11,
1224 SSL_R_NO_CIPHER_MATCH);
1225 return 0;
1226 }
David Benjamin9f2c0d72014-10-21 22:00:19 -04001227
Adam Langleyfcf25832014-12-18 17:42:32 -08001228 return 1;
1229}
Adam Langley95c29f32014-06-20 12:00:00 -07001230
Adam Langleyfcf25832014-12-18 17:42:32 -08001231/* specify the ciphers to be used by the SSL */
1232int SSL_set_cipher_list(SSL *s, const char *str) {
1233 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001234
Adam Langleyfcf25832014-12-18 17:42:32 -08001235 sk = ssl_create_cipher_list(s->ctx->method, &s->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001236 &s->cipher_list_by_id, str);
David Benjamin39482a12014-07-20 13:30:15 -04001237
Adam Langleyfcf25832014-12-18 17:42:32 -08001238 /* see comment in SSL_CTX_set_cipher_list */
1239 if (sk == NULL) {
1240 return 0;
1241 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1242 OPENSSL_PUT_ERROR(SSL, SSL_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1243 return 0;
1244 }
David Benjamin39482a12014-07-20 13:30:15 -04001245
Adam Langleyfcf25832014-12-18 17:42:32 -08001246 return 1;
1247}
Adam Langley95c29f32014-06-20 12:00:00 -07001248
Adam Langleyfcf25832014-12-18 17:42:32 -08001249int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, uint8_t *p) {
1250 size_t i;
1251 const SSL_CIPHER *c;
1252 CERT *ct = s->cert;
1253 uint8_t *q;
1254 /* Set disabled masks for this session */
1255 ssl_set_client_disabled(s);
Adam Langley29707792014-06-20 12:00:00 -07001256
Adam Langleyfcf25832014-12-18 17:42:32 -08001257 if (sk == NULL) {
1258 return 0;
1259 }
1260 q = p;
Adam Langley95c29f32014-06-20 12:00:00 -07001261
Adam Langleyfcf25832014-12-18 17:42:32 -08001262 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
1263 c = sk_SSL_CIPHER_value(sk, i);
1264 /* Skip disabled ciphers */
1265 if (c->algorithm_ssl & ct->mask_ssl ||
1266 c->algorithm_mkey & ct->mask_k ||
1267 c->algorithm_auth & ct->mask_a) {
1268 continue;
1269 }
1270 s2n(ssl3_get_cipher_value(c), p);
1271 }
1272
1273 /* If all ciphers were disabled, return the error to the caller. */
1274 if (p == q) {
1275 return 0;
1276 }
1277
1278 /* Add SCSVs. */
1279 if (!s->renegotiate) {
1280 s2n(SSL3_CK_SCSV & 0xffff, p);
1281 }
1282
Adam Langley5f0efe02015-02-20 13:03:16 -08001283 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001284 s2n(SSL3_CK_FALLBACK_SCSV & 0xffff, p);
1285 }
1286
1287 return p - q;
1288}
1289
1290STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs) {
1291 CBS cipher_suites = *cbs;
1292 const SSL_CIPHER *c;
David Benjamin60da0cd2015-05-03 15:21:28 -04001293 STACK_OF(SSL_CIPHER) *sk;
Adam Langleyfcf25832014-12-18 17:42:32 -08001294
1295 if (s->s3) {
1296 s->s3->send_connection_binding = 0;
1297 }
1298
1299 if (CBS_len(&cipher_suites) % 2 != 0) {
1300 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1301 SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
1302 return NULL;
1303 }
1304
1305 sk = sk_SSL_CIPHER_new_null();
1306 if (sk == NULL) {
1307 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1308 goto err;
1309 }
1310
Adam Langleyfcf25832014-12-18 17:42:32 -08001311 while (CBS_len(&cipher_suites) > 0) {
1312 uint16_t cipher_suite;
1313
1314 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
1315 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_INTERNAL_ERROR);
1316 goto err;
1317 }
1318
1319 /* Check for SCSV. */
1320 if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff)) {
1321 /* SCSV is fatal if renegotiating. */
1322 if (s->renegotiate) {
1323 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1324 SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1325 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1326 goto err;
1327 }
1328 s->s3->send_connection_binding = 1;
1329 continue;
1330 }
1331
1332 /* Check for FALLBACK_SCSV. */
1333 if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff)) {
1334 uint16_t max_version = ssl3_get_max_server_version(s);
1335 if (SSL_IS_DTLS(s) ? (uint16_t)s->version > max_version
1336 : (uint16_t)s->version < max_version) {
1337 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1338 SSL_R_INAPPROPRIATE_FALLBACK);
1339 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
1340 goto err;
1341 }
1342 continue;
1343 }
1344
1345 c = ssl3_get_cipher_by_value(cipher_suite);
1346 if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
1347 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1348 goto err;
1349 }
1350 }
1351
1352 return sk;
David Benjamin9f2c0d72014-10-21 22:00:19 -04001353
Adam Langley95c29f32014-06-20 12:00:00 -07001354err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001355 sk_SSL_CIPHER_free(sk);
Adam Langleyfcf25832014-12-18 17:42:32 -08001356 return NULL;
1357}
Adam Langley95c29f32014-06-20 12:00:00 -07001358
1359
Adam Langleyfcf25832014-12-18 17:42:32 -08001360/* return a servername extension value if provided in Client Hello, or NULL. So
1361 * far, only host_name types are defined (RFC 3546). */
1362const char *SSL_get_servername(const SSL *s, const int type) {
1363 if (type != TLSEXT_NAMETYPE_host_name) {
1364 return NULL;
1365 }
Adam Langley95c29f32014-06-20 12:00:00 -07001366
Adam Langleyfcf25832014-12-18 17:42:32 -08001367 return s->session && !s->tlsext_hostname ? s->session->tlsext_hostname
1368 : s->tlsext_hostname;
1369}
Adam Langley95c29f32014-06-20 12:00:00 -07001370
Adam Langleyfcf25832014-12-18 17:42:32 -08001371int SSL_get_servername_type(const SSL *s) {
1372 if (s->session &&
1373 (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname)) {
1374 return TLSEXT_NAMETYPE_host_name;
1375 }
Adam Langley95c29f32014-06-20 12:00:00 -07001376
Adam Langleyfcf25832014-12-18 17:42:32 -08001377 return -1;
1378}
Adam Langley95c29f32014-06-20 12:00:00 -07001379
Adam Langleyfcf25832014-12-18 17:42:32 -08001380void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
1381 ctx->signed_cert_timestamps_enabled = 1;
1382}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001383
Adam Langleyfcf25832014-12-18 17:42:32 -08001384int SSL_enable_signed_cert_timestamps(SSL *ssl) {
1385 ssl->signed_cert_timestamps_enabled = 1;
1386 return 1;
1387}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001388
Adam Langleyfcf25832014-12-18 17:42:32 -08001389void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
1390 ctx->ocsp_stapling_enabled = 1;
1391}
David Benjamin6c7aed02014-08-27 16:42:38 -04001392
Adam Langleyfcf25832014-12-18 17:42:32 -08001393int SSL_enable_ocsp_stapling(SSL *ssl) {
1394 ssl->ocsp_stapling_enabled = 1;
1395 return 1;
1396}
David Benjamin6c7aed02014-08-27 16:42:38 -04001397
Adam Langleyfcf25832014-12-18 17:42:32 -08001398void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
1399 size_t *out_len) {
1400 SSL_SESSION *session = ssl->session;
Adam Langley3cb50e02014-08-26 14:00:31 -07001401
Adam Langleyfcf25832014-12-18 17:42:32 -08001402 *out_len = 0;
1403 *out = NULL;
1404 if (ssl->server || !session || !session->tlsext_signed_cert_timestamp_list) {
1405 return;
1406 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001407
Adam Langleyfcf25832014-12-18 17:42:32 -08001408 *out = session->tlsext_signed_cert_timestamp_list;
1409 *out_len = session->tlsext_signed_cert_timestamp_list_length;
1410}
David Benjamin6c7aed02014-08-27 16:42:38 -04001411
Adam Langleyfcf25832014-12-18 17:42:32 -08001412void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
1413 size_t *out_len) {
1414 SSL_SESSION *session = ssl->session;
1415
1416 *out_len = 0;
1417 *out = NULL;
1418 if (ssl->server || !session || !session->ocsp_response) {
1419 return;
1420 }
1421 *out = session->ocsp_response;
1422 *out_len = session->ocsp_response_length;
1423}
David Benjamin6c7aed02014-08-27 16:42:38 -04001424
Adam Langley95c29f32014-06-20 12:00:00 -07001425/* SSL_select_next_proto implements the standard protocol selection. It is
1426 * expected that this function is called from the callback set by
1427 * SSL_CTX_set_next_proto_select_cb.
1428 *
1429 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1430 * strings. The length byte itself is not included in the length. A byte
1431 * string of length 0 is invalid. No byte string may be truncated.
1432 *
1433 * The current, but experimental algorithm for selecting the protocol is:
1434 *
1435 * 1) If the server doesn't support NPN then this is indicated to the
1436 * callback. In this case, the client application has to abort the connection
1437 * or have a default application level protocol.
1438 *
1439 * 2) If the server supports NPN, but advertises an empty list then the
1440 * client selects the first protcol in its list, but indicates via the
1441 * API that this fallback case was enacted.
1442 *
1443 * 3) Otherwise, the client finds the first protocol in the server's list
1444 * that it supports and selects this protocol. This is because it's
1445 * assumed that the server has better information about which protocol
1446 * a client should use.
1447 *
1448 * 4) If the client doesn't support any of the server's advertised
1449 * protocols, then this is treated the same as case 2.
1450 *
1451 * It returns either
1452 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1453 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1454 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001455int SSL_select_next_proto(uint8_t **out, uint8_t *outlen, const uint8_t *server,
1456 unsigned int server_len, const uint8_t *client,
1457 unsigned int client_len) {
1458 unsigned int i, j;
1459 const uint8_t *result;
1460 int status = OPENSSL_NPN_UNSUPPORTED;
Adam Langley95c29f32014-06-20 12:00:00 -07001461
Adam Langleyfcf25832014-12-18 17:42:32 -08001462 /* For each protocol in server preference order, see if we support it. */
1463 for (i = 0; i < server_len;) {
1464 for (j = 0; j < client_len;) {
1465 if (server[i] == client[j] &&
1466 memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
1467 /* We found a match */
1468 result = &server[i];
1469 status = OPENSSL_NPN_NEGOTIATED;
1470 goto found;
1471 }
1472 j += client[j];
1473 j++;
1474 }
1475 i += server[i];
1476 i++;
1477 }
Adam Langley95c29f32014-06-20 12:00:00 -07001478
Adam Langleyfcf25832014-12-18 17:42:32 -08001479 /* There's no overlap between our protocols and the server's list. */
1480 result = client;
1481 status = OPENSSL_NPN_NO_OVERLAP;
Adam Langley95c29f32014-06-20 12:00:00 -07001482
Adam Langleyfcf25832014-12-18 17:42:32 -08001483found:
1484 *out = (uint8_t *)result + 1;
1485 *outlen = result[0];
1486 return status;
1487}
Adam Langley95c29f32014-06-20 12:00:00 -07001488
Adam Langley95c29f32014-06-20 12:00:00 -07001489/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
1490 * requested protocol for this connection and returns 0. If the client didn't
1491 * request any protocol, then *data is set to NULL.
1492 *
1493 * Note that the client can request any protocol it chooses. The value returned
1494 * from this function need not be a member of the list of supported protocols
Adam Langleyfcf25832014-12-18 17:42:32 -08001495 * provided by the callback. */
1496void SSL_get0_next_proto_negotiated(const SSL *s, const uint8_t **data,
1497 unsigned *len) {
1498 *data = s->next_proto_negotiated;
1499 if (!*data) {
1500 *len = 0;
1501 } else {
1502 *len = s->next_proto_negotiated_len;
1503 }
Adam Langley95c29f32014-06-20 12:00:00 -07001504}
1505
1506/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
1507 * TLS server needs a list of supported protocols for Next Protocol
1508 * Negotiation. The returned list must be in wire format. The list is returned
1509 * by setting |out| to point to it and |outlen| to its length. This memory will
1510 * not be modified, but one should assume that the SSL* keeps a reference to
1511 * it.
1512 *
Adam Langleyfcf25832014-12-18 17:42:32 -08001513 * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise.
1514 * Otherwise, no such extension will be included in the ServerHello. */
1515void SSL_CTX_set_next_protos_advertised_cb(
1516 SSL_CTX *ctx,
1517 int (*cb)(SSL *ssl, const uint8_t **out, unsigned int *outlen, void *arg),
1518 void *arg) {
1519 ctx->next_protos_advertised_cb = cb;
1520 ctx->next_protos_advertised_cb_arg = arg;
1521}
Adam Langley95c29f32014-06-20 12:00:00 -07001522
1523/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1524 * client needs to select a protocol from the server's provided list. |out|
1525 * must be set to point to the selected protocol (which may be within |in|).
1526 * The length of the protocol name must be written into |outlen|. The server's
1527 * advertised protocols are provided in |in| and |inlen|. The callback can
1528 * assume that |in| is syntactically valid.
1529 *
1530 * The client must select a protocol. It is fatal to the connection if this
1531 * callback returns a value other than SSL_TLSEXT_ERR_OK.
1532 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001533void SSL_CTX_set_next_proto_select_cb(
1534 SSL_CTX *ctx, int (*cb)(SSL *s, uint8_t **out, uint8_t *outlen,
1535 const uint8_t *in, unsigned int inlen, void *arg),
1536 void *arg) {
1537 ctx->next_proto_select_cb = cb;
1538 ctx->next_proto_select_cb_arg = arg;
1539}
Adam Langley95c29f32014-06-20 12:00:00 -07001540
Adam Langleyfcf25832014-12-18 17:42:32 -08001541int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
1542 unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001543 OPENSSL_free(ctx->alpn_client_proto_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001544 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1545 if (!ctx->alpn_client_proto_list) {
1546 return 1;
1547 }
1548 ctx->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001549
Adam Langleyfcf25832014-12-18 17:42:32 -08001550 return 0;
1551}
Adam Langley95c29f32014-06-20 12:00:00 -07001552
Adam Langleyfcf25832014-12-18 17:42:32 -08001553int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001554 OPENSSL_free(ssl->alpn_client_proto_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001555 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1556 if (!ssl->alpn_client_proto_list) {
1557 return 1;
1558 }
1559 ssl->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001560
Adam Langleyfcf25832014-12-18 17:42:32 -08001561 return 0;
1562}
Adam Langley95c29f32014-06-20 12:00:00 -07001563
1564/* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
1565 * during ClientHello processing in order to select an ALPN protocol from the
1566 * client's list of offered protocols. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001567void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
1568 int (*cb)(SSL *ssl, const uint8_t **out,
1569 uint8_t *outlen, const uint8_t *in,
1570 unsigned int inlen, void *arg),
1571 void *arg) {
1572 ctx->alpn_select_cb = cb;
1573 ctx->alpn_select_cb_arg = arg;
1574}
Adam Langley95c29f32014-06-20 12:00:00 -07001575
1576/* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
1577 * On return it sets |*data| to point to |*len| bytes of protocol name (not
1578 * including the leading length-prefix byte). If the server didn't respond with
1579 * a negotiated protocol then |*len| will be zero. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001580void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **data,
1581 unsigned *len) {
1582 *data = NULL;
1583 if (ssl->s3) {
1584 *data = ssl->s3->alpn_selected;
1585 }
1586 if (*data == NULL) {
1587 *len = 0;
1588 } else {
1589 *len = ssl->s3->alpn_selected_len;
1590 }
1591}
Adam Langley95c29f32014-06-20 12:00:00 -07001592
David Benjamincfd248b2015-04-03 11:02:24 -04001593int SSL_export_keying_material(SSL *s, uint8_t *out, size_t out_len,
1594 const char *label, size_t label_len,
1595 const uint8_t *context, size_t context_len,
1596 int use_context) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001597 if (s->version < TLS1_VERSION) {
David Benjaminc565ebb2015-04-03 04:06:36 -04001598 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001599 }
Adam Langley95c29f32014-06-20 12:00:00 -07001600
David Benjamincfd248b2015-04-03 11:02:24 -04001601 return s->enc_method->export_keying_material(
1602 s, out, out_len, label, label_len, context, context_len, use_context);
Adam Langleyfcf25832014-12-18 17:42:32 -08001603}
Adam Langley95c29f32014-06-20 12:00:00 -07001604
Adam Langleyfcf25832014-12-18 17:42:32 -08001605static uint32_t ssl_session_hash(const SSL_SESSION *a) {
1606 uint32_t hash =
1607 ((uint32_t)a->session_id[0]) ||
1608 ((uint32_t)a->session_id[1] << 8) ||
1609 ((uint32_t)a->session_id[2] << 16) ||
1610 ((uint32_t)a->session_id[3] << 24);
Adam Langley95c29f32014-06-20 12:00:00 -07001611
Adam Langleyfcf25832014-12-18 17:42:32 -08001612 return hash;
1613}
Adam Langley95c29f32014-06-20 12:00:00 -07001614
1615/* NB: If this function (or indeed the hash function which uses a sort of
1616 * coarser function than this one) is changed, ensure
1617 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1618 * able to construct an SSL_SESSION that will collide with any existing session
1619 * with a matching session ID. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001620static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
1621 if (a->ssl_version != b->ssl_version) {
1622 return 1;
1623 }
Adam Langley95c29f32014-06-20 12:00:00 -07001624
Adam Langleyfcf25832014-12-18 17:42:32 -08001625 if (a->session_id_length != b->session_id_length) {
1626 return 1;
1627 }
Adam Langley95c29f32014-06-20 12:00:00 -07001628
Adam Langleyfcf25832014-12-18 17:42:32 -08001629 return memcmp(a->session_id, b->session_id, a->session_id_length);
1630}
Adam Langley95c29f32014-06-20 12:00:00 -07001631
Adam Langleyfcf25832014-12-18 17:42:32 -08001632SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth) {
1633 SSL_CTX *ret = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001634
Adam Langleyfcf25832014-12-18 17:42:32 -08001635 if (meth == NULL) {
1636 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_NULL_SSL_METHOD_PASSED);
1637 return NULL;
1638 }
Adam Langley95c29f32014-06-20 12:00:00 -07001639
Adam Langleyfcf25832014-12-18 17:42:32 -08001640 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
1641 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
1642 goto err;
1643 }
Adam Langley95c29f32014-06-20 12:00:00 -07001644
Adam Langleyfcf25832014-12-18 17:42:32 -08001645 ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1646 if (ret == NULL) {
1647 goto err;
1648 }
Adam Langley95c29f32014-06-20 12:00:00 -07001649
Adam Langleyfcf25832014-12-18 17:42:32 -08001650 memset(ret, 0, sizeof(SSL_CTX));
Adam Langley95c29f32014-06-20 12:00:00 -07001651
Adam Langleyfcf25832014-12-18 17:42:32 -08001652 ret->method = meth->method;
Adam Langley95c29f32014-06-20 12:00:00 -07001653
Adam Langleyfcf25832014-12-18 17:42:32 -08001654 ret->cert_store = NULL;
1655 ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
1656 ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1657 ret->session_cache_head = NULL;
1658 ret->session_cache_tail = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001659
Adam Langleyfcf25832014-12-18 17:42:32 -08001660 /* We take the system default */
1661 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001662
Adam Langleyfcf25832014-12-18 17:42:32 -08001663 ret->new_session_cb = 0;
1664 ret->remove_session_cb = 0;
1665 ret->get_session_cb = 0;
1666 ret->generate_session_id = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001667
Adam Langleyfcf25832014-12-18 17:42:32 -08001668 ret->references = 1;
1669 ret->quiet_shutdown = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001670
Adam Langleyfcf25832014-12-18 17:42:32 -08001671 ret->info_callback = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001672
Adam Langleyfcf25832014-12-18 17:42:32 -08001673 ret->app_verify_callback = 0;
1674 ret->app_verify_arg = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001675
Adam Langleyfcf25832014-12-18 17:42:32 -08001676 ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
1677 ret->read_ahead = 0;
1678 ret->msg_callback = 0;
1679 ret->msg_callback_arg = NULL;
1680 ret->verify_mode = SSL_VERIFY_NONE;
1681 ret->sid_ctx_length = 0;
1682 ret->default_verify_callback = NULL;
1683 ret->cert = ssl_cert_new();
1684 if (ret->cert == NULL) {
1685 goto err;
1686 }
Adam Langley95c29f32014-06-20 12:00:00 -07001687
Adam Langleyfcf25832014-12-18 17:42:32 -08001688 ret->default_passwd_callback = 0;
1689 ret->default_passwd_callback_userdata = NULL;
1690 ret->client_cert_cb = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001691
Adam Langleyfcf25832014-12-18 17:42:32 -08001692 ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
1693 if (ret->sessions == NULL) {
1694 goto err;
1695 }
1696 ret->cert_store = X509_STORE_new();
1697 if (ret->cert_store == NULL) {
1698 goto err;
1699 }
Adam Langley95c29f32014-06-20 12:00:00 -07001700
Adam Langleyfcf25832014-12-18 17:42:32 -08001701 ssl_create_cipher_list(ret->method, &ret->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001702 &ret->cipher_list_by_id, SSL_DEFAULT_CIPHER_LIST);
Adam Langleyfcf25832014-12-18 17:42:32 -08001703 if (ret->cipher_list == NULL ||
1704 sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0) {
1705 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_LIBRARY_HAS_NO_CIPHERS);
1706 goto err2;
1707 }
Adam Langley95c29f32014-06-20 12:00:00 -07001708
Adam Langleyfcf25832014-12-18 17:42:32 -08001709 ret->param = X509_VERIFY_PARAM_new();
1710 if (!ret->param) {
1711 goto err;
1712 }
Adam Langley95c29f32014-06-20 12:00:00 -07001713
Adam Langleyfcf25832014-12-18 17:42:32 -08001714 ret->client_CA = sk_X509_NAME_new_null();
1715 if (ret->client_CA == NULL) {
1716 goto err;
1717 }
Adam Langley95c29f32014-06-20 12:00:00 -07001718
David Benjamin9f33fc62015-04-15 17:29:53 -04001719 CRYPTO_new_ex_data(&g_ex_data_class_ssl_ctx, ret, &ret->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001720
Adam Langleyfcf25832014-12-18 17:42:32 -08001721 ret->extra_certs = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001722
Adam Langleyfcf25832014-12-18 17:42:32 -08001723 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
David Benjamin422d3a42014-08-20 11:09:03 -04001724
Adam Langleyfcf25832014-12-18 17:42:32 -08001725 ret->tlsext_servername_callback = 0;
1726 ret->tlsext_servername_arg = NULL;
1727 /* Setup RFC4507 ticket keys */
1728 if (!RAND_bytes(ret->tlsext_tick_key_name, 16) ||
1729 !RAND_bytes(ret->tlsext_tick_hmac_key, 16) ||
1730 !RAND_bytes(ret->tlsext_tick_aes_key, 16)) {
1731 ret->options |= SSL_OP_NO_TICKET;
1732 }
Adam Langley95c29f32014-06-20 12:00:00 -07001733
Adam Langleyfcf25832014-12-18 17:42:32 -08001734 ret->next_protos_advertised_cb = 0;
1735 ret->next_proto_select_cb = 0;
1736 ret->psk_identity_hint = NULL;
1737 ret->psk_client_callback = NULL;
1738 ret->psk_server_callback = NULL;
1739
1740 /* Default is to connect to non-RI servers. When RI is more widely deployed
1741 * might change this. */
1742 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
1743
1744 /* Lock the SSL_CTX to the specified version, for compatibility with legacy
1745 * uses of SSL_METHOD. */
1746 if (meth->version != 0) {
1747 SSL_CTX_set_max_version(ret, meth->version);
1748 SSL_CTX_set_min_version(ret, meth->version);
1749 }
1750
1751 return ret;
1752
Adam Langley95c29f32014-06-20 12:00:00 -07001753err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001754 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -07001755err2:
David Benjamin2755a3e2015-04-22 16:17:58 -04001756 SSL_CTX_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -08001757 return NULL;
1758}
Adam Langley95c29f32014-06-20 12:00:00 -07001759
David Benjamin4fcc2e22015-04-22 12:58:16 -04001760void SSL_CTX_free(SSL_CTX *ctx) {
1761 if (ctx == NULL ||
1762 CRYPTO_add(&ctx->references, -1, CRYPTO_LOCK_SSL_CTX) > 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001763 return;
1764 }
Adam Langley95c29f32014-06-20 12:00:00 -07001765
David Benjamin2755a3e2015-04-22 16:17:58 -04001766 X509_VERIFY_PARAM_free(ctx->param);
Adam Langley95c29f32014-06-20 12:00:00 -07001767
Adam Langleyfcf25832014-12-18 17:42:32 -08001768 /* Free internal session cache. However: the remove_cb() may reference the
1769 * ex_data of SSL_CTX, thus the ex_data store can only be removed after the
1770 * sessions were flushed. As the ex_data handling routines might also touch
1771 * the session cache, the most secure solution seems to be: empty (flush) the
1772 * cache, then free ex_data, then finally free the cache. (See ticket
1773 * [openssl.org #212].) */
David Benjamin2755a3e2015-04-22 16:17:58 -04001774 SSL_CTX_flush_sessions(ctx, 0);
Adam Langley95c29f32014-06-20 12:00:00 -07001775
David Benjamin4fcc2e22015-04-22 12:58:16 -04001776 CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, ctx, &ctx->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001777
David Benjamin2755a3e2015-04-22 16:17:58 -04001778 lh_SSL_SESSION_free(ctx->sessions);
1779 X509_STORE_free(ctx->cert_store);
1780 ssl_cipher_preference_list_free(ctx->cipher_list);
1781 sk_SSL_CIPHER_free(ctx->cipher_list_by_id);
1782 ssl_cipher_preference_list_free(ctx->cipher_list_tls11);
1783 ssl_cert_free(ctx->cert);
1784 sk_X509_NAME_pop_free(ctx->client_CA, X509_NAME_free);
1785 sk_X509_pop_free(ctx->extra_certs, X509_free);
1786 sk_SRTP_PROTECTION_PROFILE_free(ctx->srtp_profiles);
1787 OPENSSL_free(ctx->psk_identity_hint);
1788 OPENSSL_free(ctx->tlsext_ecpointformatlist);
1789 OPENSSL_free(ctx->tlsext_ellipticcurvelist);
1790 OPENSSL_free(ctx->alpn_client_proto_list);
1791 EVP_PKEY_free(ctx->tlsext_channel_id_private);
1792 BIO_free(ctx->keylog_bio);
Adam Langley95c29f32014-06-20 12:00:00 -07001793
David Benjamin4fcc2e22015-04-22 12:58:16 -04001794 OPENSSL_free(ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08001795}
Adam Langley95c29f32014-06-20 12:00:00 -07001796
Adam Langleyfcf25832014-12-18 17:42:32 -08001797void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) {
1798 ctx->default_passwd_callback = cb;
1799}
Adam Langley95c29f32014-06-20 12:00:00 -07001800
Adam Langleyfcf25832014-12-18 17:42:32 -08001801void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u) {
1802 ctx->default_passwd_callback_userdata = u;
1803}
Adam Langley95c29f32014-06-20 12:00:00 -07001804
Adam Langleyfcf25832014-12-18 17:42:32 -08001805void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
1806 int (*cb)(X509_STORE_CTX *, void *),
1807 void *arg) {
1808 ctx->app_verify_callback = cb;
1809 ctx->app_verify_arg = arg;
1810}
Adam Langley95c29f32014-06-20 12:00:00 -07001811
Adam Langleyfcf25832014-12-18 17:42:32 -08001812void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
1813 int (*cb)(int, X509_STORE_CTX *)) {
1814 ctx->verify_mode = mode;
1815 ctx->default_verify_callback = cb;
1816}
Adam Langley95c29f32014-06-20 12:00:00 -07001817
Adam Langleyfcf25832014-12-18 17:42:32 -08001818void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
1819 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
1820}
Adam Langley1258b6a2014-06-20 12:00:00 -07001821
Adam Langleyfcf25832014-12-18 17:42:32 -08001822void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb)(SSL *ssl, void *arg),
1823 void *arg) {
1824 ssl_cert_set_cert_cb(c->cert, cb, arg);
1825}
David Benjamin859ec3c2014-09-02 16:29:36 -04001826
Adam Langleyfcf25832014-12-18 17:42:32 -08001827void SSL_set_cert_cb(SSL *s, int (*cb)(SSL *ssl, void *arg), void *arg) {
1828 ssl_cert_set_cert_cb(s->cert, cb, arg);
1829}
Adam Langley95c29f32014-06-20 12:00:00 -07001830
Adam Langleyfcf25832014-12-18 17:42:32 -08001831static int ssl_has_key(SSL *s, size_t idx) {
1832 CERT_PKEY *cpk = &s->cert->pkeys[idx];
1833 return cpk->x509 && cpk->privatekey;
1834}
David Benjamin033e5f42014-11-13 18:47:41 -05001835
David Benjamin107db582015-04-08 00:41:59 -04001836void ssl_get_compatible_server_ciphers(SSL *s, uint32_t *out_mask_k,
1837 uint32_t *out_mask_a) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001838 CERT *c = s->cert;
1839 int rsa_enc, rsa_sign, dh_tmp;
David Benjamin107db582015-04-08 00:41:59 -04001840 uint32_t mask_k, mask_a;
Adam Langleyfcf25832014-12-18 17:42:32 -08001841 int have_ecc_cert, ecdsa_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001842 X509 *x;
Adam Langley95c29f32014-06-20 12:00:00 -07001843
Adam Langleyfcf25832014-12-18 17:42:32 -08001844 if (c == NULL) {
1845 /* TODO(davidben): Is this codepath possible? */
1846 *out_mask_k = 0;
1847 *out_mask_a = 0;
1848 return;
1849 }
Adam Langley95c29f32014-06-20 12:00:00 -07001850
Adam Langleyfcf25832014-12-18 17:42:32 -08001851 dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001852
Adam Langleyfcf25832014-12-18 17:42:32 -08001853 rsa_enc = ssl_has_key(s, SSL_PKEY_RSA_ENC);
1854 rsa_sign = ssl_has_key(s, SSL_PKEY_RSA_SIGN);
1855 have_ecc_cert = ssl_has_key(s, SSL_PKEY_ECC);
1856 mask_k = 0;
1857 mask_a = 0;
David Benjaminf31e6812014-11-13 18:05:55 -05001858
Adam Langleyfcf25832014-12-18 17:42:32 -08001859 if (rsa_enc) {
1860 mask_k |= SSL_kRSA;
1861 }
1862 if (dh_tmp) {
David Benjamin7061e282015-03-19 11:10:48 -04001863 mask_k |= SSL_kDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001864 }
1865 if (rsa_enc || rsa_sign) {
1866 mask_a |= SSL_aRSA;
1867 }
Adam Langley95c29f32014-06-20 12:00:00 -07001868
Adam Langleyfcf25832014-12-18 17:42:32 -08001869 /* An ECC certificate may be usable for ECDSA cipher suites depending on the
1870 * key usage extension and on the client's curve preferences. */
1871 if (have_ecc_cert) {
1872 x = c->pkeys[SSL_PKEY_ECC].x509;
1873 /* This call populates extension flags (ex_flags). */
1874 X509_check_purpose(x, -1, 0);
1875 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE)
1876 ? (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)
1877 : 1;
1878 if (!tls1_check_ec_cert(s, x)) {
1879 ecdsa_ok = 0;
1880 }
1881 if (ecdsa_ok) {
1882 mask_a |= SSL_aECDSA;
1883 }
1884 }
Adam Langley95c29f32014-06-20 12:00:00 -07001885
Adam Langleyfcf25832014-12-18 17:42:32 -08001886 /* If we are considering an ECC cipher suite that uses an ephemeral EC
1887 * key, check it. */
David Benjamindd978782015-04-24 15:20:13 -04001888 if (tls1_check_ec_tmp_key(s)) {
David Benjamin7061e282015-03-19 11:10:48 -04001889 mask_k |= SSL_kECDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001890 }
Adam Langley95c29f32014-06-20 12:00:00 -07001891
Adam Langleyfcf25832014-12-18 17:42:32 -08001892 /* PSK requires a server callback. */
1893 if (s->psk_server_callback != NULL) {
1894 mask_k |= SSL_kPSK;
1895 mask_a |= SSL_aPSK;
1896 }
Adam Langley95c29f32014-06-20 12:00:00 -07001897
Adam Langleyfcf25832014-12-18 17:42:32 -08001898 *out_mask_k = mask_k;
1899 *out_mask_a = mask_a;
1900}
Adam Langley95c29f32014-06-20 12:00:00 -07001901
1902/* This handy macro borrowed from crypto/x509v3/v3_purp.c */
1903#define ku_reject(x, usage) \
Adam Langleyfcf25832014-12-18 17:42:32 -08001904 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
Adam Langley95c29f32014-06-20 12:00:00 -07001905
Adam Langleyfcf25832014-12-18 17:42:32 -08001906int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001907 const SSL_CIPHER *cs = s->s3->tmp.new_cipher;
David Benjamin3fa27772015-04-17 22:32:19 -04001908 uint32_t alg_a = cs->algorithm_auth;
1909 int signature_nid = 0, md_nid = 0, pk_nid = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001910
Adam Langleyfcf25832014-12-18 17:42:32 -08001911 /* This call populates the ex_flags field correctly */
1912 X509_check_purpose(x, -1, 0);
1913 if (x->sig_alg && x->sig_alg->algorithm) {
1914 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
1915 OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
1916 }
1917 if (alg_a & SSL_aECDSA) {
1918 /* key usage, if present, must allow signing */
1919 if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE)) {
1920 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg,
1921 SSL_R_ECC_CERT_NOT_FOR_SIGNING);
1922 return 0;
1923 }
1924 }
Adam Langley95c29f32014-06-20 12:00:00 -07001925
Adam Langleyfcf25832014-12-18 17:42:32 -08001926 return 1; /* all checks are ok */
1927}
Adam Langley95c29f32014-06-20 12:00:00 -07001928
Adam Langleyfcf25832014-12-18 17:42:32 -08001929static int ssl_get_server_cert_index(const SSL *s) {
1930 int idx;
1931 idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1932 if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509) {
1933 idx = SSL_PKEY_RSA_SIGN;
1934 }
1935 if (idx == -1) {
1936 OPENSSL_PUT_ERROR(SSL, ssl_get_server_cert_index, ERR_R_INTERNAL_ERROR);
1937 }
1938 return idx;
1939}
Adam Langley95c29f32014-06-20 12:00:00 -07001940
Adam Langleyfcf25832014-12-18 17:42:32 -08001941CERT_PKEY *ssl_get_server_send_pkey(const SSL *s) {
1942 int i = ssl_get_server_cert_index(s);
Adam Langley95c29f32014-06-20 12:00:00 -07001943
Adam Langleyfcf25832014-12-18 17:42:32 -08001944 /* This may or may not be an error. */
1945 if (i < 0) {
1946 return NULL;
1947 }
Adam Langley95c29f32014-06-20 12:00:00 -07001948
Adam Langleyfcf25832014-12-18 17:42:32 -08001949 /* May be NULL. */
1950 return &s->cert->pkeys[i];
1951}
Adam Langley95c29f32014-06-20 12:00:00 -07001952
Adam Langleyfcf25832014-12-18 17:42:32 -08001953EVP_PKEY *ssl_get_sign_pkey(SSL *s, const SSL_CIPHER *cipher) {
David Benjamin3fa27772015-04-17 22:32:19 -04001954 uint32_t alg_a = cipher->algorithm_auth;
1955 CERT *c = s->cert;
Adam Langleyfcf25832014-12-18 17:42:32 -08001956 int idx = -1;
Adam Langley95c29f32014-06-20 12:00:00 -07001957
Adam Langleyfcf25832014-12-18 17:42:32 -08001958 if (alg_a & SSL_aRSA) {
1959 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL) {
1960 idx = SSL_PKEY_RSA_SIGN;
1961 } else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL) {
1962 idx = SSL_PKEY_RSA_ENC;
1963 }
1964 } else if ((alg_a & SSL_aECDSA) &&
1965 (c->pkeys[SSL_PKEY_ECC].privatekey != NULL)) {
1966 idx = SSL_PKEY_ECC;
1967 }
Adam Langley95c29f32014-06-20 12:00:00 -07001968
Adam Langleyfcf25832014-12-18 17:42:32 -08001969 if (idx == -1) {
1970 OPENSSL_PUT_ERROR(SSL, ssl_get_sign_pkey, ERR_R_INTERNAL_ERROR);
1971 return NULL;
1972 }
Adam Langley95c29f32014-06-20 12:00:00 -07001973
Adam Langleyfcf25832014-12-18 17:42:32 -08001974 return c->pkeys[idx].privatekey;
1975}
Adam Langley95c29f32014-06-20 12:00:00 -07001976
Adam Langleyfcf25832014-12-18 17:42:32 -08001977void ssl_update_cache(SSL *s, int mode) {
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001978 /* Never cache sessions with empty session IDs. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001979 if (s->session->session_id_length == 0) {
1980 return;
1981 }
Adam Langley95c29f32014-06-20 12:00:00 -07001982
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001983 SSL_CTX *ctx = s->initial_ctx;
1984 if ((ctx->session_cache_mode & mode) == mode && !s->hit &&
1985 ((ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) ||
1986 SSL_CTX_add_session(ctx, s->session)) &&
1987 ctx->new_session_cb != NULL) {
1988 /* Note: |new_session_cb| is called whether the internal session cache is
1989 * used or not. */
1990 if (!ctx->new_session_cb(s, SSL_SESSION_up_ref(s->session))) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001991 SSL_SESSION_free(s->session);
1992 }
1993 }
Adam Langley95c29f32014-06-20 12:00:00 -07001994
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001995 if (!(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR) &&
1996 !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) &&
1997 (ctx->session_cache_mode & mode) == mode) {
1998 /* Automatically flush the internal session cache every 255 connections. */
1999 int flush_cache = 0;
2000 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2001 ctx->handshakes_since_cache_flush++;
2002 if (ctx->handshakes_since_cache_flush >= 255) {
2003 flush_cache = 1;
2004 ctx->handshakes_since_cache_flush = 0;
2005 }
2006 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
2007
2008 if (flush_cache) {
2009 SSL_CTX_flush_sessions(ctx, (unsigned long)time(NULL));
Adam Langleyfcf25832014-12-18 17:42:32 -08002010 }
2011 }
2012}
Adam Langley95c29f32014-06-20 12:00:00 -07002013
David Benjamin1a5c50f2015-03-11 16:22:37 -04002014int SSL_get_error(const SSL *s, int ret_code) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002015 int reason;
David Benjamin1a5c50f2015-03-11 16:22:37 -04002016 uint32_t err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002017 BIO *bio;
Adam Langley95c29f32014-06-20 12:00:00 -07002018
David Benjamin1a5c50f2015-03-11 16:22:37 -04002019 if (ret_code > 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002020 return SSL_ERROR_NONE;
2021 }
Adam Langley95c29f32014-06-20 12:00:00 -07002022
Adam Langleyfcf25832014-12-18 17:42:32 -08002023 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
2024 * where we do encode the error */
David Benjamin1a5c50f2015-03-11 16:22:37 -04002025 err = ERR_peek_error();
2026 if (err != 0) {
2027 if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002028 return SSL_ERROR_SYSCALL;
2029 }
2030 return SSL_ERROR_SSL;
2031 }
Adam Langley95c29f32014-06-20 12:00:00 -07002032
David Benjamin1a5c50f2015-03-11 16:22:37 -04002033 if (ret_code == 0) {
David Benjamin9a38e922015-01-22 16:06:11 -05002034 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2035 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) {
2036 /* The socket was cleanly shut down with a close_notify. */
2037 return SSL_ERROR_ZERO_RETURN;
2038 }
2039 /* An EOF was observed which violates the protocol, and the underlying
2040 * transport does not participate in the error queue. Bubble up to the
2041 * caller. */
2042 return SSL_ERROR_SYSCALL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002043 }
Adam Langleyb2ce0582014-06-20 12:00:00 -07002044
Adam Langleyfcf25832014-12-18 17:42:32 -08002045 if (SSL_want_session(s)) {
2046 return SSL_ERROR_PENDING_SESSION;
2047 }
Adam Langley95c29f32014-06-20 12:00:00 -07002048
Adam Langleyfcf25832014-12-18 17:42:32 -08002049 if (SSL_want_certificate(s)) {
2050 return SSL_ERROR_PENDING_CERTIFICATE;
2051 }
Adam Langley95c29f32014-06-20 12:00:00 -07002052
Adam Langleyfcf25832014-12-18 17:42:32 -08002053 if (SSL_want_read(s)) {
2054 bio = SSL_get_rbio(s);
2055 if (BIO_should_read(bio)) {
2056 return SSL_ERROR_WANT_READ;
2057 }
Adam Langley95c29f32014-06-20 12:00:00 -07002058
Adam Langleyfcf25832014-12-18 17:42:32 -08002059 if (BIO_should_write(bio)) {
2060 /* This one doesn't make too much sense ... We never try to write to the
2061 * rbio, and an application program where rbio and wbio are separate
2062 * couldn't even know what it should wait for. However if we ever set
2063 * s->rwstate incorrectly (so that we have SSL_want_read(s) instead of
2064 * SSL_want_write(s)) and rbio and wbio *are* the same, this test works
2065 * around that bug; so it might be safer to keep it. */
2066 return SSL_ERROR_WANT_WRITE;
2067 }
Adam Langley95c29f32014-06-20 12:00:00 -07002068
Adam Langleyfcf25832014-12-18 17:42:32 -08002069 if (BIO_should_io_special(bio)) {
2070 reason = BIO_get_retry_reason(bio);
2071 if (reason == BIO_RR_CONNECT) {
2072 return SSL_ERROR_WANT_CONNECT;
2073 }
Adam Langley95c29f32014-06-20 12:00:00 -07002074
Adam Langleyfcf25832014-12-18 17:42:32 -08002075 if (reason == BIO_RR_ACCEPT) {
2076 return SSL_ERROR_WANT_ACCEPT;
2077 }
Adam Langley95c29f32014-06-20 12:00:00 -07002078
Adam Langleyfcf25832014-12-18 17:42:32 -08002079 return SSL_ERROR_SYSCALL; /* unknown */
2080 }
2081 }
Adam Langley95c29f32014-06-20 12:00:00 -07002082
Adam Langleyfcf25832014-12-18 17:42:32 -08002083 if (SSL_want_write(s)) {
2084 bio = SSL_get_wbio(s);
2085 if (BIO_should_write(bio)) {
2086 return SSL_ERROR_WANT_WRITE;
2087 }
Adam Langley95c29f32014-06-20 12:00:00 -07002088
Adam Langleyfcf25832014-12-18 17:42:32 -08002089 if (BIO_should_read(bio)) {
2090 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2091 return SSL_ERROR_WANT_READ;
2092 }
Adam Langley95c29f32014-06-20 12:00:00 -07002093
Adam Langleyfcf25832014-12-18 17:42:32 -08002094 if (BIO_should_io_special(bio)) {
2095 reason = BIO_get_retry_reason(bio);
2096 if (reason == BIO_RR_CONNECT) {
2097 return SSL_ERROR_WANT_CONNECT;
2098 }
Adam Langley95c29f32014-06-20 12:00:00 -07002099
Adam Langleyfcf25832014-12-18 17:42:32 -08002100 if (reason == BIO_RR_ACCEPT) {
2101 return SSL_ERROR_WANT_ACCEPT;
2102 }
Adam Langley95c29f32014-06-20 12:00:00 -07002103
Adam Langleyfcf25832014-12-18 17:42:32 -08002104 return SSL_ERROR_SYSCALL;
2105 }
2106 }
Adam Langley95c29f32014-06-20 12:00:00 -07002107
Adam Langleyfcf25832014-12-18 17:42:32 -08002108 if (SSL_want_x509_lookup(s)) {
2109 return SSL_ERROR_WANT_X509_LOOKUP;
2110 }
Adam Langley95c29f32014-06-20 12:00:00 -07002111
Adam Langleyfcf25832014-12-18 17:42:32 -08002112 if (SSL_want_channel_id_lookup(s)) {
2113 return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
2114 }
Adam Langley0f4746e2014-08-13 12:26:32 -07002115
Adam Langleyfcf25832014-12-18 17:42:32 -08002116 return SSL_ERROR_SYSCALL;
2117}
Adam Langley0f4746e2014-08-13 12:26:32 -07002118
Adam Langleyfcf25832014-12-18 17:42:32 -08002119int SSL_do_handshake(SSL *s) {
2120 int ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002121
Adam Langleyfcf25832014-12-18 17:42:32 -08002122 if (s->handshake_func == NULL) {
2123 OPENSSL_PUT_ERROR(SSL, SSL_do_handshake, SSL_R_CONNECTION_TYPE_NOT_SET);
2124 return -1;
2125 }
Adam Langley95c29f32014-06-20 12:00:00 -07002126
Adam Langleyfcf25832014-12-18 17:42:32 -08002127 s->method->ssl_renegotiate_check(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002128
Adam Langleyfcf25832014-12-18 17:42:32 -08002129 if (SSL_in_init(s)) {
2130 ret = s->handshake_func(s);
2131 }
2132 return ret;
2133}
Adam Langley95c29f32014-06-20 12:00:00 -07002134
Adam Langleyfcf25832014-12-18 17:42:32 -08002135void SSL_set_accept_state(SSL *s) {
2136 s->server = 1;
2137 s->shutdown = 0;
2138 s->state = SSL_ST_ACCEPT | SSL_ST_BEFORE;
2139 s->handshake_func = s->method->ssl_accept;
2140 /* clear the current cipher */
2141 ssl_clear_cipher_ctx(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002142}
Adam Langley95c29f32014-06-20 12:00:00 -07002143
Adam Langleyfcf25832014-12-18 17:42:32 -08002144void SSL_set_connect_state(SSL *s) {
2145 s->server = 0;
2146 s->shutdown = 0;
2147 s->state = SSL_ST_CONNECT | SSL_ST_BEFORE;
2148 s->handshake_func = s->method->ssl_connect;
2149 /* clear the current cipher */
2150 ssl_clear_cipher_ctx(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002151}
Adam Langley95c29f32014-06-20 12:00:00 -07002152
Adam Langleyfcf25832014-12-18 17:42:32 -08002153static const char *ssl_get_version(int version) {
2154 switch (version) {
2155 case TLS1_2_VERSION:
2156 return "TLSv1.2";
Adam Langley95c29f32014-06-20 12:00:00 -07002157
Adam Langleyfcf25832014-12-18 17:42:32 -08002158 case TLS1_1_VERSION:
2159 return "TLSv1.1";
Adam Langley95c29f32014-06-20 12:00:00 -07002160
Adam Langleyfcf25832014-12-18 17:42:32 -08002161 case TLS1_VERSION:
2162 return "TLSv1";
Adam Langley95c29f32014-06-20 12:00:00 -07002163
Adam Langleyfcf25832014-12-18 17:42:32 -08002164 case SSL3_VERSION:
2165 return "SSLv3";
Adam Langley95c29f32014-06-20 12:00:00 -07002166
David Benjamin1c722b72015-04-20 13:53:10 -04002167 case DTLS1_VERSION:
2168 return "DTLSv1";
2169
2170 case DTLS1_2_VERSION:
2171 return "DTLSv1.2";
2172
Adam Langleyfcf25832014-12-18 17:42:32 -08002173 default:
2174 return "unknown";
2175 }
2176}
Adam Langley95c29f32014-06-20 12:00:00 -07002177
Adam Langleyfcf25832014-12-18 17:42:32 -08002178const char *SSL_get_version(const SSL *s) {
2179 return ssl_get_version(s->version);
2180}
Adam Langley95c29f32014-06-20 12:00:00 -07002181
Adam Langleyfcf25832014-12-18 17:42:32 -08002182const char *SSL_SESSION_get_version(const SSL_SESSION *sess) {
2183 return ssl_get_version(sess->ssl_version);
2184}
Adam Langley95c29f32014-06-20 12:00:00 -07002185
Adam Langleyfcf25832014-12-18 17:42:32 -08002186void ssl_clear_cipher_ctx(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002187 if (s->aead_read_ctx != NULL) {
2188 EVP_AEAD_CTX_cleanup(&s->aead_read_ctx->ctx);
2189 OPENSSL_free(s->aead_read_ctx);
2190 s->aead_read_ctx = NULL;
2191 }
Adam Langleya5dc5452014-06-20 12:00:00 -07002192
Adam Langleyfcf25832014-12-18 17:42:32 -08002193 if (s->aead_write_ctx != NULL) {
2194 EVP_AEAD_CTX_cleanup(&s->aead_write_ctx->ctx);
2195 OPENSSL_free(s->aead_write_ctx);
2196 s->aead_write_ctx = NULL;
2197 }
2198}
Adam Langley95c29f32014-06-20 12:00:00 -07002199
Adam Langleyfcf25832014-12-18 17:42:32 -08002200X509 *SSL_get_certificate(const SSL *s) {
2201 if (s->cert != NULL) {
2202 return s->cert->key->x509;
2203 }
2204
2205 return NULL;
2206}
2207
2208EVP_PKEY *SSL_get_privatekey(const SSL *s) {
2209 if (s->cert != NULL) {
2210 return s->cert->key->privatekey;
2211 }
2212
2213 return NULL;
2214}
2215
2216X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
2217 if (ctx->cert != NULL) {
2218 return ctx->cert->key->x509;
2219 }
2220
2221 return NULL;
2222}
2223
2224EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
2225 if (ctx->cert != NULL) {
2226 return ctx->cert->key->privatekey;
2227 }
2228
2229 return NULL;
2230}
2231
2232const SSL_CIPHER *SSL_get_current_cipher(const SSL *s) {
2233 if (s->session != NULL && s->session->cipher != NULL) {
2234 return s->session->cipher;
2235 }
2236
2237 return NULL;
2238}
2239
2240const void *SSL_get_current_compression(SSL *s) { return NULL; }
2241
2242const void *SSL_get_current_expansion(SSL *s) { return NULL; }
2243
2244int ssl_init_wbio_buffer(SSL *s, int push) {
2245 BIO *bbio;
2246
2247 if (s->bbio == NULL) {
2248 bbio = BIO_new(BIO_f_buffer());
2249 if (bbio == NULL) {
2250 return 0;
2251 }
2252 s->bbio = bbio;
2253 } else {
2254 bbio = s->bbio;
2255 if (s->bbio == s->wbio) {
2256 s->wbio = BIO_pop(s->wbio);
2257 }
2258 }
2259
2260 BIO_reset(bbio);
2261 if (!BIO_set_read_buffer_size(bbio, 1)) {
2262 OPENSSL_PUT_ERROR(SSL, ssl_init_wbio_buffer, ERR_R_BUF_LIB);
2263 return 0;
2264 }
2265
2266 if (push) {
2267 if (s->wbio != bbio) {
2268 s->wbio = BIO_push(bbio, s->wbio);
2269 }
2270 } else {
2271 if (s->wbio == bbio) {
2272 s->wbio = BIO_pop(bbio);
2273 }
2274 }
2275
2276 return 1;
2277}
2278
2279void ssl_free_wbio_buffer(SSL *s) {
2280 if (s->bbio == NULL) {
2281 return;
2282 }
2283
2284 if (s->bbio == s->wbio) {
2285 /* remove buffering */
2286 s->wbio = BIO_pop(s->wbio);
2287 }
2288
2289 BIO_free(s->bbio);
2290 s->bbio = NULL;
2291}
2292
2293void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
2294 ctx->quiet_shutdown = mode;
2295}
2296
2297int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
2298 return ctx->quiet_shutdown;
2299}
2300
2301void SSL_set_quiet_shutdown(SSL *s, int mode) { s->quiet_shutdown = mode; }
2302
2303int SSL_get_quiet_shutdown(const SSL *s) { return s->quiet_shutdown; }
2304
2305void SSL_set_shutdown(SSL *s, int mode) { s->shutdown = mode; }
2306
2307int SSL_get_shutdown(const SSL *s) { return s->shutdown; }
2308
2309int SSL_version(const SSL *s) { return s->version; }
2310
2311SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
2312
2313SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
2314 if (ssl->ctx == ctx) {
2315 return ssl->ctx;
2316 }
2317
2318 if (ctx == NULL) {
2319 ctx = ssl->initial_ctx;
2320 }
2321
David Benjamin2755a3e2015-04-22 16:17:58 -04002322 ssl_cert_free(ssl->cert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002323 ssl->cert = ssl_cert_dup(ctx->cert);
David Benjamin2755a3e2015-04-22 16:17:58 -04002324
Adam Langleyfcf25832014-12-18 17:42:32 -08002325 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
David Benjamin2755a3e2015-04-22 16:17:58 -04002326 SSL_CTX_free(ssl->ctx); /* decrement reference count */
Adam Langleyfcf25832014-12-18 17:42:32 -08002327 ssl->ctx = ctx;
2328
2329 ssl->sid_ctx_length = ctx->sid_ctx_length;
2330 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2331 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2332
2333 return ssl->ctx;
2334}
2335
2336int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
2337 return X509_STORE_set_default_paths(ctx->cert_store);
2338}
Adam Langley95c29f32014-06-20 12:00:00 -07002339
2340int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
Adam Langleyfcf25832014-12-18 17:42:32 -08002341 const char *CApath) {
2342 return X509_STORE_load_locations(ctx->cert_store, CAfile, CApath);
2343}
Adam Langley95c29f32014-06-20 12:00:00 -07002344
2345void SSL_set_info_callback(SSL *ssl,
Adam Langleyfcf25832014-12-18 17:42:32 -08002346 void (*cb)(const SSL *ssl, int type, int val)) {
2347 ssl->info_callback = cb;
2348}
Adam Langley95c29f32014-06-20 12:00:00 -07002349
Adam Langleyfcf25832014-12-18 17:42:32 -08002350void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/, int /*type*/,
2351 int /*val*/) {
2352 return ssl->info_callback;
2353}
Adam Langley95c29f32014-06-20 12:00:00 -07002354
Adam Langleyfcf25832014-12-18 17:42:32 -08002355int SSL_state(const SSL *ssl) { return ssl->state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002356
Adam Langleyfcf25832014-12-18 17:42:32 -08002357void SSL_set_state(SSL *ssl, int state) { ssl->state = state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002358
Adam Langleyfcf25832014-12-18 17:42:32 -08002359void SSL_set_verify_result(SSL *ssl, long arg) { ssl->verify_result = arg; }
Adam Langley95c29f32014-06-20 12:00:00 -07002360
Adam Langleyfcf25832014-12-18 17:42:32 -08002361long SSL_get_verify_result(const SSL *ssl) { return ssl->verify_result; }
Adam Langley95c29f32014-06-20 12:00:00 -07002362
Adam Langleyfcf25832014-12-18 17:42:32 -08002363int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2364 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002365 int index;
2366 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp,
2367 new_func, dup_func, free_func)) {
2368 return -1;
2369 }
2370 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002371}
Adam Langley95c29f32014-06-20 12:00:00 -07002372
Adam Langleyfcf25832014-12-18 17:42:32 -08002373int SSL_set_ex_data(SSL *s, int idx, void *arg) {
2374 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2375}
Adam Langley95c29f32014-06-20 12:00:00 -07002376
Adam Langleyfcf25832014-12-18 17:42:32 -08002377void *SSL_get_ex_data(const SSL *s, int idx) {
2378 return CRYPTO_get_ex_data(&s->ex_data, idx);
2379}
Adam Langley95c29f32014-06-20 12:00:00 -07002380
Adam Langleyfcf25832014-12-18 17:42:32 -08002381int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2382 CRYPTO_EX_dup *dup_func,
2383 CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002384 int index;
2385 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp,
2386 new_func, dup_func, free_func)) {
2387 return -1;
2388 }
2389 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002390}
Adam Langley95c29f32014-06-20 12:00:00 -07002391
Adam Langleyfcf25832014-12-18 17:42:32 -08002392int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg) {
2393 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2394}
Adam Langley95c29f32014-06-20 12:00:00 -07002395
Adam Langleyfcf25832014-12-18 17:42:32 -08002396void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx) {
2397 return CRYPTO_get_ex_data(&s->ex_data, idx);
2398}
Adam Langley95c29f32014-06-20 12:00:00 -07002399
Adam Langleyfcf25832014-12-18 17:42:32 -08002400int ssl_ok(SSL *s) { return 1; }
Adam Langley95c29f32014-06-20 12:00:00 -07002401
Adam Langleyfcf25832014-12-18 17:42:32 -08002402X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
2403 return ctx->cert_store;
2404}
Adam Langley95c29f32014-06-20 12:00:00 -07002405
Adam Langleyfcf25832014-12-18 17:42:32 -08002406void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
David Benjamin2755a3e2015-04-22 16:17:58 -04002407 X509_STORE_free(ctx->cert_store);
Adam Langleyfcf25832014-12-18 17:42:32 -08002408 ctx->cert_store = store;
2409}
Adam Langley95c29f32014-06-20 12:00:00 -07002410
Adam Langleyfcf25832014-12-18 17:42:32 -08002411int SSL_want(const SSL *s) { return s->rwstate; }
Adam Langley95c29f32014-06-20 12:00:00 -07002412
Adam Langleyfcf25832014-12-18 17:42:32 -08002413void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
2414 RSA *(*cb)(SSL *ssl, int is_export,
2415 int keylength)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002416}
Adam Langley95c29f32014-06-20 12:00:00 -07002417
Adam Langleyfcf25832014-12-18 17:42:32 -08002418void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
2419 int keylength)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002420}
2421
2422void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
David Benjamin59015c32015-04-26 13:13:55 -04002423 DH *(*callback)(SSL *ssl, int is_export,
2424 int keylength)) {
2425 ctx->cert->dh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002426}
2427
David Benjamin59015c32015-04-26 13:13:55 -04002428void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*callback)(SSL *ssl, int is_export,
2429 int keylength)) {
2430 ssl->cert->dh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002431}
2432
2433void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,
David Benjamindd978782015-04-24 15:20:13 -04002434 EC_KEY *(*callback)(SSL *ssl, int is_export,
2435 int keylength)) {
David Benjamin59015c32015-04-26 13:13:55 -04002436 ctx->cert->ecdh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002437}
2438
2439void SSL_set_tmp_ecdh_callback(SSL *ssl,
David Benjamindd978782015-04-24 15:20:13 -04002440 EC_KEY *(*callback)(SSL *ssl, int is_export,
2441 int keylength)) {
David Benjamin59015c32015-04-26 13:13:55 -04002442 ssl->cert->ecdh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002443}
2444
2445int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
2446 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2447 OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_psk_identity_hint,
2448 SSL_R_DATA_LENGTH_TOO_LONG);
2449 return 0;
2450 }
2451
David Benjamin2755a3e2015-04-22 16:17:58 -04002452 OPENSSL_free(ctx->psk_identity_hint);
Adam Langleyfcf25832014-12-18 17:42:32 -08002453
2454 if (identity_hint != NULL) {
2455 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2456 if (ctx->psk_identity_hint == NULL) {
2457 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002458 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002459 } else {
2460 ctx->psk_identity_hint = NULL;
2461 }
Adam Langley95c29f32014-06-20 12:00:00 -07002462
Adam Langleyfcf25832014-12-18 17:42:32 -08002463 return 1;
2464}
2465
2466int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint) {
2467 if (s == NULL) {
2468 return 0;
2469 }
2470
2471 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2472 OPENSSL_PUT_ERROR(SSL, SSL_use_psk_identity_hint,
2473 SSL_R_DATA_LENGTH_TOO_LONG);
2474 return 0;
2475 }
2476
2477 /* Clear currently configured hint, if any. */
David Benjamin2755a3e2015-04-22 16:17:58 -04002478 OPENSSL_free(s->psk_identity_hint);
2479 s->psk_identity_hint = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002480
2481 if (identity_hint != NULL) {
2482 s->psk_identity_hint = BUF_strdup(identity_hint);
2483 if (s->psk_identity_hint == NULL) {
2484 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002485 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002486 }
Adam Langley95c29f32014-06-20 12:00:00 -07002487
Adam Langleyfcf25832014-12-18 17:42:32 -08002488 return 1;
2489}
Adam Langley95c29f32014-06-20 12:00:00 -07002490
Adam Langleyfcf25832014-12-18 17:42:32 -08002491const char *SSL_get_psk_identity_hint(const SSL *s) {
2492 if (s == NULL) {
2493 return NULL;
2494 }
2495 return s->psk_identity_hint;
2496}
Adam Langley95c29f32014-06-20 12:00:00 -07002497
Adam Langleyfcf25832014-12-18 17:42:32 -08002498const char *SSL_get_psk_identity(const SSL *s) {
2499 if (s == NULL || s->session == NULL) {
2500 return NULL;
2501 }
Adam Langley95c29f32014-06-20 12:00:00 -07002502
Adam Langleyfcf25832014-12-18 17:42:32 -08002503 return s->session->psk_identity;
2504}
Adam Langley95c29f32014-06-20 12:00:00 -07002505
Adam Langleyfcf25832014-12-18 17:42:32 -08002506void SSL_set_psk_client_callback(
2507 SSL *s, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2508 unsigned int max_identity_len, uint8_t *psk,
2509 unsigned int max_psk_len)) {
2510 s->psk_client_callback = cb;
2511}
Adam Langley95c29f32014-06-20 12:00:00 -07002512
Adam Langleyfcf25832014-12-18 17:42:32 -08002513void SSL_CTX_set_psk_client_callback(
2514 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2515 unsigned int max_identity_len,
2516 uint8_t *psk, unsigned int max_psk_len)) {
2517 ctx->psk_client_callback = cb;
2518}
Adam Langley95c29f32014-06-20 12:00:00 -07002519
Adam Langleyfcf25832014-12-18 17:42:32 -08002520void SSL_set_psk_server_callback(
2521 SSL *s, unsigned int (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
2522 unsigned int max_psk_len)) {
2523 s->psk_server_callback = cb;
2524}
Adam Langley95c29f32014-06-20 12:00:00 -07002525
Adam Langleyfcf25832014-12-18 17:42:32 -08002526void SSL_CTX_set_psk_server_callback(
2527 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *identity,
2528 uint8_t *psk, unsigned int max_psk_len)) {
2529 ctx->psk_server_callback = cb;
2530}
Adam Langley95c29f32014-06-20 12:00:00 -07002531
Adam Langleyfcf25832014-12-18 17:42:32 -08002532void SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) {
2533 ctx->min_version = version;
2534}
Adam Langley95c29f32014-06-20 12:00:00 -07002535
Adam Langleyfcf25832014-12-18 17:42:32 -08002536void SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) {
2537 ctx->max_version = version;
2538}
Adam Langley0289c732014-06-20 12:00:00 -07002539
Adam Langleyfcf25832014-12-18 17:42:32 -08002540void SSL_set_min_version(SSL *ssl, uint16_t version) {
2541 ssl->min_version = version;
2542}
Adam Langley0289c732014-06-20 12:00:00 -07002543
Adam Langleyfcf25832014-12-18 17:42:32 -08002544void SSL_set_max_version(SSL *ssl, uint16_t version) {
2545 ssl->max_version = version;
2546}
Adam Langley95c29f32014-06-20 12:00:00 -07002547
Adam Langleyfcf25832014-12-18 17:42:32 -08002548void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
2549 void (*cb)(int write_p, int version,
2550 int content_type, const void *buf,
2551 size_t len, SSL *ssl, void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002552 ctx->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002553}
David Benjamin61ecccf2015-05-05 09:44:51 -04002554
2555void SSL_CTX_set_msg_callback_arg(SSL_CTX *ctx, void *arg) {
2556 ctx->msg_callback_arg = arg;
2557}
2558
Adam Langleyfcf25832014-12-18 17:42:32 -08002559void SSL_set_msg_callback(SSL *ssl,
2560 void (*cb)(int write_p, int version, int content_type,
2561 const void *buf, size_t len, SSL *ssl,
2562 void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002563 ssl->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002564}
Adam Langley95c29f32014-06-20 12:00:00 -07002565
David Benjamin61ecccf2015-05-05 09:44:51 -04002566void SSL_set_msg_callback_arg(SSL *ssl, void *arg) {
2567 ssl->msg_callback_arg = arg;
2568}
2569
Adam Langleyfcf25832014-12-18 17:42:32 -08002570void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio) {
David Benjamin2755a3e2015-04-22 16:17:58 -04002571 BIO_free(ctx->keylog_bio);
Adam Langleyfcf25832014-12-18 17:42:32 -08002572 ctx->keylog_bio = keylog_bio;
2573}
Adam Langley95c29f32014-06-20 12:00:00 -07002574
Adam Langleyfcf25832014-12-18 17:42:32 -08002575static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
2576 static const char hextable[] = "0123456789abcdef";
2577 uint8_t *out;
2578 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002579
Adam Langleyfcf25832014-12-18 17:42:32 -08002580 if (!CBB_add_space(cbb, &out, in_len * 2)) {
2581 return 0;
2582 }
Adam Langley95c29f32014-06-20 12:00:00 -07002583
Adam Langleyfcf25832014-12-18 17:42:32 -08002584 for (i = 0; i < in_len; i++) {
2585 *(out++) = (uint8_t)hextable[in[i] >> 4];
2586 *(out++) = (uint8_t)hextable[in[i] & 0xf];
2587 }
Adam Langley95c29f32014-06-20 12:00:00 -07002588
Adam Langleyfcf25832014-12-18 17:42:32 -08002589 return 1;
2590}
David Benjamin859ec3c2014-09-02 16:29:36 -04002591
2592int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -08002593 const uint8_t *encrypted_premaster,
2594 size_t encrypted_premaster_len,
2595 const uint8_t *premaster,
2596 size_t premaster_len) {
2597 BIO *bio = ctx->keylog_bio;
2598 CBB cbb;
2599 uint8_t *out;
2600 size_t out_len;
2601 int ret;
David Benjamin859ec3c2014-09-02 16:29:36 -04002602
Adam Langleyfcf25832014-12-18 17:42:32 -08002603 if (bio == NULL) {
2604 return 1;
2605 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002606
Adam Langleyfcf25832014-12-18 17:42:32 -08002607 if (encrypted_premaster_len < 8) {
2608 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_rsa_client_key_exchange,
2609 ERR_R_INTERNAL_ERROR);
2610 return 0;
2611 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002612
Adam Langleyfcf25832014-12-18 17:42:32 -08002613 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len * 2 + 1)) {
2614 return 0;
2615 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002616
Adam Langleyfcf25832014-12-18 17:42:32 -08002617 if (!CBB_add_bytes(&cbb, (const uint8_t *)"RSA ", 4) ||
2618 /* Only the first 8 bytes of the encrypted premaster secret are
2619 * logged. */
2620 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
2621 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2622 !cbb_add_hex(&cbb, premaster, premaster_len) ||
2623 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2624 !CBB_finish(&cbb, &out, &out_len)) {
2625 CBB_cleanup(&cbb);
2626 return 0;
2627 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002628
Adam Langleyfcf25832014-12-18 17:42:32 -08002629 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2630 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2631 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
David Benjamin859ec3c2014-09-02 16:29:36 -04002632
Adam Langleyfcf25832014-12-18 17:42:32 -08002633 OPENSSL_free(out);
2634 return ret;
Adam Langley95f22882014-06-20 12:00:00 -07002635}
2636
Adam Langleyfcf25832014-12-18 17:42:32 -08002637int ssl_ctx_log_master_secret(SSL_CTX *ctx, const uint8_t *client_random,
2638 size_t client_random_len, const uint8_t *master,
2639 size_t master_len) {
2640 BIO *bio = ctx->keylog_bio;
2641 CBB cbb;
2642 uint8_t *out;
2643 size_t out_len;
2644 int ret;
Adam Langleyadb739e2014-06-20 12:00:00 -07002645
Adam Langleyfcf25832014-12-18 17:42:32 -08002646 if (bio == NULL) {
2647 return 1;
2648 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002649
Adam Langleyfcf25832014-12-18 17:42:32 -08002650 if (client_random_len != 32) {
2651 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_master_secret, ERR_R_INTERNAL_ERROR);
2652 return 0;
2653 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002654
Adam Langleyfcf25832014-12-18 17:42:32 -08002655 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len * 2 + 1)) {
2656 return 0;
2657 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002658
Adam Langleyfcf25832014-12-18 17:42:32 -08002659 if (!CBB_add_bytes(&cbb, (const uint8_t *)"CLIENT_RANDOM ", 14) ||
2660 !cbb_add_hex(&cbb, client_random, 32) ||
2661 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2662 !cbb_add_hex(&cbb, master, master_len) ||
2663 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2664 !CBB_finish(&cbb, &out, &out_len)) {
2665 CBB_cleanup(&cbb);
2666 return 0;
2667 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002668
Adam Langleyfcf25832014-12-18 17:42:32 -08002669 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2670 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2671 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
David Benjamine99e9122014-12-11 01:46:01 -05002672
Adam Langleyfcf25832014-12-18 17:42:32 -08002673 OPENSSL_free(out);
2674 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -07002675}
2676
David Benjamined7c4752015-02-16 19:16:46 -05002677int SSL_in_false_start(const SSL *s) {
2678 return s->s3->tmp.in_false_start;
2679}
2680
Adam Langleyfcf25832014-12-18 17:42:32 -08002681int SSL_cutthrough_complete(const SSL *s) {
David Benjamined7c4752015-02-16 19:16:46 -05002682 return SSL_in_false_start(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002683}
Adam Langley95c29f32014-06-20 12:00:00 -07002684
Adam Langleyfcf25832014-12-18 17:42:32 -08002685void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
2686 size_t *ssl_session_size) {
2687 *ssl_size = sizeof(SSL);
2688 *ssl_ctx_size = sizeof(SSL_CTX);
2689 *ssl_session_size = sizeof(SSL_SESSION);
2690}
Feng Lu41aa3252014-11-21 22:47:56 -08002691
David Benjamined7c4752015-02-16 19:16:46 -05002692int ssl3_can_false_start(const SSL *s) {
David Benjamin195dc782015-02-19 13:27:05 -05002693 const SSL_CIPHER *const cipher = SSL_get_current_cipher(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002694
Adam Langleye631d962015-04-01 13:11:01 -07002695 /* False Start only for TLS 1.2 with an ECDHE+AEAD cipher and ALPN or NPN. */
David Benjamin195dc782015-02-19 13:27:05 -05002696 return !SSL_IS_DTLS(s) &&
2697 SSL_version(s) >= TLS1_2_VERSION &&
2698 (s->s3->alpn_selected || s->s3->next_proto_neg_seen) &&
2699 cipher != NULL &&
Adam Langleye631d962015-04-01 13:11:01 -07002700 cipher->algorithm_mkey == SSL_kECDHE &&
David Benjamin195dc782015-02-19 13:27:05 -05002701 (cipher->algorithm_enc == SSL_AES128GCM ||
2702 cipher->algorithm_enc == SSL_AES256GCM ||
2703 cipher->algorithm_enc == SSL_CHACHA20POLY1305);
Adam Langleyfcf25832014-12-18 17:42:32 -08002704}
2705
2706const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version) {
2707 switch (version) {
2708 case SSL3_VERSION:
2709 return &SSLv3_enc_data;
2710
2711 case TLS1_VERSION:
2712 return &TLSv1_enc_data;
2713
David Benjamin9e13e1a2015-03-05 01:56:32 -05002714 case DTLS1_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002715 case TLS1_1_VERSION:
2716 return &TLSv1_1_enc_data;
2717
David Benjamin9e13e1a2015-03-05 01:56:32 -05002718 case DTLS1_2_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002719 case TLS1_2_VERSION:
2720 return &TLSv1_2_enc_data;
2721
Adam Langleyfcf25832014-12-18 17:42:32 -08002722 default:
2723 return NULL;
2724 }
2725}
2726
2727uint16_t ssl3_get_max_server_version(const SSL *s) {
2728 uint16_t max_version;
2729
2730 if (SSL_IS_DTLS(s)) {
2731 max_version = (s->max_version != 0) ? s->max_version : DTLS1_2_VERSION;
2732 if (!(s->options & SSL_OP_NO_DTLSv1_2) && DTLS1_2_VERSION >= max_version) {
2733 return DTLS1_2_VERSION;
2734 }
2735 if (!(s->options & SSL_OP_NO_DTLSv1) && DTLS1_VERSION >= max_version) {
2736 return DTLS1_VERSION;
2737 }
2738 return 0;
2739 }
2740
2741 max_version = (s->max_version != 0) ? s->max_version : TLS1_2_VERSION;
2742 if (!(s->options & SSL_OP_NO_TLSv1_2) && TLS1_2_VERSION <= max_version) {
2743 return TLS1_2_VERSION;
2744 }
2745 if (!(s->options & SSL_OP_NO_TLSv1_1) && TLS1_1_VERSION <= max_version) {
2746 return TLS1_1_VERSION;
2747 }
2748 if (!(s->options & SSL_OP_NO_TLSv1) && TLS1_VERSION <= max_version) {
2749 return TLS1_VERSION;
2750 }
2751 if (!(s->options & SSL_OP_NO_SSLv3) && SSL3_VERSION <= max_version) {
2752 return SSL3_VERSION;
2753 }
2754 return 0;
2755}
2756
2757uint16_t ssl3_get_mutual_version(SSL *s, uint16_t client_version) {
2758 uint16_t version = 0;
2759
2760 if (SSL_IS_DTLS(s)) {
2761 /* Clamp client_version to max_version. */
2762 if (s->max_version != 0 && client_version < s->max_version) {
2763 client_version = s->max_version;
2764 }
2765
2766 if (client_version <= DTLS1_2_VERSION && !(s->options & SSL_OP_NO_DTLSv1_2)) {
2767 version = DTLS1_2_VERSION;
2768 } else if (client_version <= DTLS1_VERSION &&
David Benjaminb18f0242015-03-10 18:30:08 -04002769 !(s->options & SSL_OP_NO_DTLSv1)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002770 version = DTLS1_VERSION;
2771 }
2772
2773 /* Check against min_version. */
2774 if (version != 0 && s->min_version != 0 && version > s->min_version) {
2775 return 0;
2776 }
2777 return version;
2778 } else {
2779 /* Clamp client_version to max_version. */
2780 if (s->max_version != 0 && client_version > s->max_version) {
2781 client_version = s->max_version;
2782 }
2783
2784 if (client_version >= TLS1_2_VERSION && !(s->options & SSL_OP_NO_TLSv1_2)) {
2785 version = TLS1_2_VERSION;
2786 } else if (client_version >= TLS1_1_VERSION &&
2787 !(s->options & SSL_OP_NO_TLSv1_1)) {
2788 version = TLS1_1_VERSION;
2789 } else if (client_version >= TLS1_VERSION && !(s->options & SSL_OP_NO_TLSv1)) {
2790 version = TLS1_VERSION;
2791 } else if (client_version >= SSL3_VERSION && !(s->options & SSL_OP_NO_SSLv3)) {
2792 version = SSL3_VERSION;
2793 }
2794
2795 /* Check against min_version. */
2796 if (version != 0 && s->min_version != 0 && version < s->min_version) {
2797 return 0;
2798 }
2799 return version;
2800 }
2801}
2802
2803uint16_t ssl3_get_max_client_version(SSL *s) {
David Benjamin123a8fd2015-04-26 14:16:41 -04002804 uint32_t options = s->options;
Adam Langleyfcf25832014-12-18 17:42:32 -08002805 uint16_t version = 0;
2806
2807 /* OpenSSL's API for controlling versions entails blacklisting individual
2808 * protocols. This has two problems. First, on the client, the protocol can
2809 * only express a contiguous range of versions. Second, a library consumer
2810 * trying to set a maximum version cannot disable protocol versions that get
2811 * added in a future version of the library.
2812 *
2813 * To account for both of these, OpenSSL interprets the client-side bitmask
2814 * as a min/max range by picking the lowest contiguous non-empty range of
2815 * enabled protocols. Note that this means it is impossible to set a maximum
2816 * version of TLS 1.2 in a future-proof way.
2817 *
2818 * By this scheme, the maximum version is the lowest version V such that V is
2819 * enabled and V+1 is disabled or unimplemented. */
2820 if (SSL_IS_DTLS(s)) {
2821 if (!(options & SSL_OP_NO_DTLSv1_2)) {
2822 version = DTLS1_2_VERSION;
2823 }
2824 if (!(options & SSL_OP_NO_DTLSv1) && (options & SSL_OP_NO_DTLSv1_2)) {
2825 version = DTLS1_VERSION;
2826 }
2827 if (s->max_version != 0 && version < s->max_version) {
2828 version = s->max_version;
2829 }
2830 } else {
2831 if (!(options & SSL_OP_NO_TLSv1_2)) {
2832 version = TLS1_2_VERSION;
2833 }
2834 if (!(options & SSL_OP_NO_TLSv1_1) && (options & SSL_OP_NO_TLSv1_2)) {
2835 version = TLS1_1_VERSION;
2836 }
2837 if (!(options & SSL_OP_NO_TLSv1) && (options & SSL_OP_NO_TLSv1_1)) {
2838 version = TLS1_VERSION;
2839 }
2840 if (!(options & SSL_OP_NO_SSLv3) && (options & SSL_OP_NO_TLSv1)) {
2841 version = SSL3_VERSION;
2842 }
2843 if (s->max_version != 0 && version > s->max_version) {
2844 version = s->max_version;
2845 }
2846 }
2847
2848 return version;
2849}
2850
2851int ssl3_is_version_enabled(SSL *s, uint16_t version) {
2852 if (SSL_IS_DTLS(s)) {
2853 if (s->max_version != 0 && version < s->max_version) {
2854 return 0;
2855 }
2856 if (s->min_version != 0 && version > s->min_version) {
2857 return 0;
2858 }
2859
2860 switch (version) {
2861 case DTLS1_VERSION:
2862 return !(s->options & SSL_OP_NO_DTLSv1);
2863
2864 case DTLS1_2_VERSION:
2865 return !(s->options & SSL_OP_NO_DTLSv1_2);
2866
2867 default:
2868 return 0;
2869 }
2870 } else {
2871 if (s->max_version != 0 && version > s->max_version) {
2872 return 0;
2873 }
2874 if (s->min_version != 0 && version < s->min_version) {
2875 return 0;
2876 }
2877
2878 switch (version) {
2879 case SSL3_VERSION:
2880 return !(s->options & SSL_OP_NO_SSLv3);
2881
2882 case TLS1_VERSION:
2883 return !(s->options & SSL_OP_NO_TLSv1);
2884
2885 case TLS1_1_VERSION:
2886 return !(s->options & SSL_OP_NO_TLSv1_1);
2887
2888 case TLS1_2_VERSION:
2889 return !(s->options & SSL_OP_NO_TLSv1_2);
2890
2891 default:
2892 return 0;
2893 }
2894 }
2895}
2896
David Benjaminea72bd02014-12-21 21:27:41 -05002897uint16_t ssl3_version_from_wire(SSL *s, uint16_t wire_version) {
2898 if (!SSL_IS_DTLS(s)) {
2899 return wire_version;
2900 }
2901
2902 uint16_t tls_version = ~wire_version;
2903 uint16_t version = tls_version + 0x0201;
2904 /* If either component overflowed, clamp it so comparisons still work. */
2905 if ((version >> 8) < (tls_version >> 8)) {
2906 version = 0xff00 | (version & 0xff);
2907 }
2908 if ((version & 0xff) < (tls_version & 0xff)) {
2909 version = (version & 0xff00) | 0xff;
2910 }
2911 /* DTLS 1.0 maps to TLS 1.1, not TLS 1.0. */
2912 if (version == TLS1_VERSION) {
2913 version = TLS1_1_VERSION;
2914 }
2915 return version;
2916}
2917
Adam Langleyfcf25832014-12-18 17:42:32 -08002918int SSL_cache_hit(SSL *s) { return s->hit; }
2919
2920int SSL_is_server(SSL *s) { return s->server; }
2921
Adam Langley524e7172015-02-20 16:04:00 -08002922void SSL_CTX_set_dos_protection_cb(
2923 SSL_CTX *ctx, int (*cb)(const struct ssl_early_callback_ctx *)) {
2924 ctx->dos_protection_cb = cb;
2925}
2926
Adam Langleyfcf25832014-12-18 17:42:32 -08002927void SSL_enable_fastradio_padding(SSL *s, char on_off) {
2928 s->fastradio_padding = on_off;
2929}
Adam Langley44972942015-02-20 13:43:23 -08002930
David Benjaminb16346b2015-04-08 19:16:58 -04002931void SSL_set_reject_peer_renegotiations(SSL *s, int reject) {
2932 s->reject_peer_renegotiations = !!reject;
2933}
2934
Adam Langley44972942015-02-20 13:43:23 -08002935const SSL_CIPHER *SSL_get_cipher_by_value(uint16_t value) {
2936 return ssl3_get_cipher_by_value(value);
2937}
Adam Langley3f92d212015-02-20 15:32:52 -08002938
2939int SSL_get_rc4_state(const SSL *ssl, const RC4_KEY **read_key,
2940 const RC4_KEY **write_key) {
2941 if (ssl->aead_read_ctx == NULL || ssl->aead_write_ctx == NULL) {
2942 return 0;
2943 }
2944
2945 return EVP_AEAD_CTX_get_rc4_state(&ssl->aead_read_ctx->ctx, read_key) &&
2946 EVP_AEAD_CTX_get_rc4_state(&ssl->aead_write_ctx->ctx, write_key);
2947}
David Benjaminda881e92015-04-26 14:45:04 -04002948
2949int SSL_CTX_sess_connect(const SSL_CTX *ctx) { return 0; }
2950int SSL_CTX_sess_connect_good(const SSL_CTX *ctx) { return 0; }
2951int SSL_CTX_sess_connect_renegotiate(const SSL_CTX *ctx) { return 0; }
2952int SSL_CTX_sess_accept(const SSL_CTX *ctx) { return 0; }
2953int SSL_CTX_sess_accept_renegotiate(const SSL_CTX *ctx) { return 0; }
2954int SSL_CTX_sess_accept_good(const SSL_CTX *ctx) { return 0; }
2955int SSL_CTX_sess_hits(const SSL_CTX *ctx) { return 0; }
2956int SSL_CTX_sess_cb_hits(const SSL_CTX *ctx) { return 0; }
2957int SSL_CTX_sess_misses(const SSL_CTX *ctx) { return 0; }
2958int SSL_CTX_sess_timeouts(const SSL_CTX *ctx) { return 0; }
2959int SSL_CTX_sess_cache_full(const SSL_CTX *ctx) { return 0; }