blob: ce4b949dfb1a7624e92580ac988460b34eb2f9f0 [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 Benjamin9f33fc62015-04-15 17:29:53 -0400164static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl = CRYPTO_EX_DATA_CLASS_INIT;
165static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl_ctx = CRYPTO_EX_DATA_CLASS_INIT;
166
Adam Langleyfcf25832014-12-18 17:42:32 -0800167int SSL_clear(SSL *s) {
168 if (s->method == NULL) {
169 OPENSSL_PUT_ERROR(SSL, SSL_clear, SSL_R_NO_METHOD_SPECIFIED);
170 return 0;
171 }
Adam Langley95c29f32014-06-20 12:00:00 -0700172
Adam Langleyfcf25832014-12-18 17:42:32 -0800173 if (ssl_clear_bad_session(s)) {
174 SSL_SESSION_free(s->session);
175 s->session = NULL;
176 }
Adam Langley95c29f32014-06-20 12:00:00 -0700177
Adam Langleyfcf25832014-12-18 17:42:32 -0800178 s->hit = 0;
179 s->shutdown = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700180
Adam Langleyfcf25832014-12-18 17:42:32 -0800181 if (s->renegotiate) {
182 OPENSSL_PUT_ERROR(SSL, SSL_clear, ERR_R_INTERNAL_ERROR);
183 return 0;
184 }
Adam Langley95c29f32014-06-20 12:00:00 -0700185
Adam Langleyfcf25832014-12-18 17:42:32 -0800186 /* SSL_clear may be called before or after the |s| is initialized in either
187 * accept or connect state. In the latter case, SSL_clear should preserve the
188 * half and reset |s->state| accordingly. */
189 if (s->handshake_func != NULL) {
190 if (s->server) {
191 SSL_set_accept_state(s);
192 } else {
193 SSL_set_connect_state(s);
194 }
195 } else {
196 assert(s->state == 0);
197 }
Adam Langley95c29f32014-06-20 12:00:00 -0700198
David Benjamin62fd1622015-01-11 13:30:01 -0500199 /* TODO(davidben): Some state on |s| is reset both in |SSL_new| and
200 * |SSL_clear| because it is per-connection state rather than configuration
201 * state. Per-connection state should be on |s->s3| and |s->d1| so it is
202 * naturally reset at the right points between |SSL_new|, |SSL_clear|, and
203 * |ssl3_new|. */
204
Adam Langleyfcf25832014-12-18 17:42:32 -0800205 s->rwstate = SSL_NOTHING;
206 s->rstate = SSL_ST_READ_HEADER;
Adam Langley95c29f32014-06-20 12:00:00 -0700207
David Benjamin2755a3e2015-04-22 16:17:58 -0400208 BUF_MEM_free(s->init_buf);
209 s->init_buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700210
David Benjamin62fd1622015-01-11 13:30:01 -0500211 s->packet = NULL;
212 s->packet_length = 0;
213
Adam Langleyfcf25832014-12-18 17:42:32 -0800214 ssl_clear_cipher_ctx(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700215
David Benjamin2755a3e2015-04-22 16:17:58 -0400216 OPENSSL_free(s->next_proto_negotiated);
217 s->next_proto_negotiated = NULL;
218 s->next_proto_negotiated_len = 0;
David Benjamin62fd1622015-01-11 13:30:01 -0500219
220 /* The s->d1->mtu is simultaneously configuration (preserved across
221 * clear) and connection-specific state (gets reset).
222 *
223 * TODO(davidben): Avoid this. */
224 unsigned mtu = 0;
225 if (s->d1 != NULL) {
226 mtu = s->d1->mtu;
227 }
228
229 s->method->ssl_free(s);
230 if (!s->method->ssl_new(s)) {
231 return 0;
232 }
233 s->enc_method = ssl3_get_enc_method(s->version);
234 assert(s->enc_method != NULL);
235
236 if (SSL_IS_DTLS(s) && (SSL_get_options(s) & SSL_OP_NO_QUERY_MTU)) {
237 s->d1->mtu = mtu;
238 }
239
Adam Langleyfcf25832014-12-18 17:42:32 -0800240 s->client_version = s->version;
Adam Langley95c29f32014-06-20 12:00:00 -0700241
Adam Langleyfcf25832014-12-18 17:42:32 -0800242 return 1;
243}
Adam Langley95c29f32014-06-20 12:00:00 -0700244
Adam Langleyfcf25832014-12-18 17:42:32 -0800245SSL *SSL_new(SSL_CTX *ctx) {
246 SSL *s;
Adam Langley95c29f32014-06-20 12:00:00 -0700247
Adam Langleyfcf25832014-12-18 17:42:32 -0800248 if (ctx == NULL) {
249 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_NULL_SSL_CTX);
250 return NULL;
251 }
252 if (ctx->method == NULL) {
253 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
254 return NULL;
255 }
Adam Langley95c29f32014-06-20 12:00:00 -0700256
Adam Langleyfcf25832014-12-18 17:42:32 -0800257 s = (SSL *)OPENSSL_malloc(sizeof(SSL));
258 if (s == NULL) {
259 goto err;
260 }
261 memset(s, 0, sizeof(SSL));
Adam Langley95c29f32014-06-20 12:00:00 -0700262
Adam Langleyfcf25832014-12-18 17:42:32 -0800263 s->min_version = ctx->min_version;
264 s->max_version = ctx->max_version;
David Benjamin1eb367c2014-12-12 18:17:51 -0500265
Adam Langleyfcf25832014-12-18 17:42:32 -0800266 s->options = ctx->options;
267 s->mode = ctx->mode;
268 s->max_cert_list = ctx->max_cert_list;
Adam Langley95c29f32014-06-20 12:00:00 -0700269
David Benjamina5a3eeb2015-03-18 20:26:30 -0400270 s->cert = ssl_cert_dup(ctx->cert);
271 if (s->cert == NULL) {
272 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -0800273 }
Adam Langley95c29f32014-06-20 12:00:00 -0700274
Adam Langleyfcf25832014-12-18 17:42:32 -0800275 s->read_ahead = ctx->read_ahead;
276 s->msg_callback = ctx->msg_callback;
277 s->msg_callback_arg = ctx->msg_callback_arg;
278 s->verify_mode = ctx->verify_mode;
279 s->sid_ctx_length = ctx->sid_ctx_length;
280 assert(s->sid_ctx_length <= sizeof s->sid_ctx);
281 memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));
282 s->verify_callback = ctx->default_verify_callback;
283 s->generate_session_id = ctx->generate_session_id;
Adam Langley95c29f32014-06-20 12:00:00 -0700284
Adam Langleyfcf25832014-12-18 17:42:32 -0800285 s->param = X509_VERIFY_PARAM_new();
286 if (!s->param) {
287 goto err;
288 }
289 X509_VERIFY_PARAM_inherit(s->param, ctx->param);
290 s->quiet_shutdown = ctx->quiet_shutdown;
291 s->max_send_fragment = ctx->max_send_fragment;
Adam Langley95c29f32014-06-20 12:00:00 -0700292
Adam Langleyfcf25832014-12-18 17:42:32 -0800293 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
294 s->ctx = ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -0800295 s->tlsext_ticket_expected = 0;
296 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
297 s->initial_ctx = ctx;
298 if (ctx->tlsext_ecpointformatlist) {
299 s->tlsext_ecpointformatlist = BUF_memdup(
300 ctx->tlsext_ecpointformatlist, ctx->tlsext_ecpointformatlist_length);
301 if (!s->tlsext_ecpointformatlist) {
302 goto err;
303 }
304 s->tlsext_ecpointformatlist_length = ctx->tlsext_ecpointformatlist_length;
305 }
Adam Langley95c29f32014-06-20 12:00:00 -0700306
Adam Langleyfcf25832014-12-18 17:42:32 -0800307 if (ctx->tlsext_ellipticcurvelist) {
308 s->tlsext_ellipticcurvelist =
309 BUF_memdup(ctx->tlsext_ellipticcurvelist,
310 ctx->tlsext_ellipticcurvelist_length * 2);
311 if (!s->tlsext_ellipticcurvelist) {
312 goto err;
313 }
314 s->tlsext_ellipticcurvelist_length = ctx->tlsext_ellipticcurvelist_length;
315 }
316 s->next_proto_negotiated = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700317
Adam Langleyfcf25832014-12-18 17:42:32 -0800318 if (s->ctx->alpn_client_proto_list) {
319 s->alpn_client_proto_list = BUF_memdup(s->ctx->alpn_client_proto_list,
320 s->ctx->alpn_client_proto_list_len);
321 if (s->alpn_client_proto_list == NULL) {
322 goto err;
323 }
324 s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;
325 }
Adam Langley95c29f32014-06-20 12:00:00 -0700326
Adam Langleyfcf25832014-12-18 17:42:32 -0800327 s->verify_result = X509_V_OK;
328 s->method = ctx->method;
Adam Langley95c29f32014-06-20 12:00:00 -0700329
Adam Langleyfcf25832014-12-18 17:42:32 -0800330 if (!s->method->ssl_new(s)) {
331 goto err;
332 }
333 s->enc_method = ssl3_get_enc_method(s->version);
334 assert(s->enc_method != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700335
David Benjamin62fd1622015-01-11 13:30:01 -0500336 s->rwstate = SSL_NOTHING;
337 s->rstate = SSL_ST_READ_HEADER;
Adam Langley95c29f32014-06-20 12:00:00 -0700338
David Benjamin9f33fc62015-04-15 17:29:53 -0400339 CRYPTO_new_ex_data(&g_ex_data_class_ssl, s, &s->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700340
Adam Langleyfcf25832014-12-18 17:42:32 -0800341 s->psk_identity_hint = NULL;
342 if (ctx->psk_identity_hint) {
343 s->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
344 if (s->psk_identity_hint == NULL) {
345 goto err;
346 }
347 }
348 s->psk_client_callback = ctx->psk_client_callback;
349 s->psk_server_callback = ctx->psk_server_callback;
Adam Langley95c29f32014-06-20 12:00:00 -0700350
David Benjamin02ddbfd2015-01-11 13:09:11 -0500351 s->tlsext_channel_id_enabled = ctx->tlsext_channel_id_enabled;
352 if (ctx->tlsext_channel_id_private) {
353 s->tlsext_channel_id_private = EVP_PKEY_dup(ctx->tlsext_channel_id_private);
354 }
355
Adam Langleyfcf25832014-12-18 17:42:32 -0800356 s->signed_cert_timestamps_enabled = s->ctx->signed_cert_timestamps_enabled;
357 s->ocsp_stapling_enabled = s->ctx->ocsp_stapling_enabled;
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200358
Adam Langleyfcf25832014-12-18 17:42:32 -0800359 return s;
360
Adam Langley95c29f32014-06-20 12:00:00 -0700361err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400362 SSL_free(s);
Adam Langleyfcf25832014-12-18 17:42:32 -0800363 OPENSSL_PUT_ERROR(SSL, SSL_new, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700364
Adam Langleyfcf25832014-12-18 17:42:32 -0800365 return NULL;
366}
Adam Langley95c29f32014-06-20 12:00:00 -0700367
Adam Langleyfcf25832014-12-18 17:42:32 -0800368int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
369 unsigned int sid_ctx_len) {
370 if (sid_ctx_len > sizeof ctx->sid_ctx) {
371 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_session_id_context,
372 SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
373 return 0;
374 }
375 ctx->sid_ctx_length = sid_ctx_len;
376 memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700377
Adam Langleyfcf25832014-12-18 17:42:32 -0800378 return 1;
379}
Adam Langley95c29f32014-06-20 12:00:00 -0700380
Adam Langleyfcf25832014-12-18 17:42:32 -0800381int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
382 unsigned int sid_ctx_len) {
383 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
384 OPENSSL_PUT_ERROR(SSL, SSL_set_session_id_context,
385 SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
386 return 0;
387 }
388 ssl->sid_ctx_length = sid_ctx_len;
389 memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700390
Adam Langleyfcf25832014-12-18 17:42:32 -0800391 return 1;
392}
Adam Langley95c29f32014-06-20 12:00:00 -0700393
Adam Langleyfcf25832014-12-18 17:42:32 -0800394int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb) {
395 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
396 ctx->generate_session_id = cb;
397 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
398 return 1;
399}
Adam Langley95c29f32014-06-20 12:00:00 -0700400
Adam Langleyfcf25832014-12-18 17:42:32 -0800401int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800402 ssl->generate_session_id = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -0800403 return 1;
404}
Adam Langley95c29f32014-06-20 12:00:00 -0700405
Adam Langleyfcf25832014-12-18 17:42:32 -0800406int SSL_has_matching_session_id(const SSL *ssl, const uint8_t *id,
407 unsigned int id_len) {
408 /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how we
409 * can "construct" a session to give us the desired check - ie. to find if
410 * there's a session in the hash table that would conflict with any new
411 * session built out of this id/id_len and the ssl_version in use by this
412 * SSL. */
413 SSL_SESSION r, *p;
Adam Langley95c29f32014-06-20 12:00:00 -0700414
Adam Langleyfcf25832014-12-18 17:42:32 -0800415 if (id_len > sizeof r.session_id) {
416 return 0;
417 }
Adam Langley95c29f32014-06-20 12:00:00 -0700418
Adam Langleyfcf25832014-12-18 17:42:32 -0800419 r.ssl_version = ssl->version;
420 r.session_id_length = id_len;
421 memcpy(r.session_id, id, id_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700422
Adam Langleyfcf25832014-12-18 17:42:32 -0800423 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
424 p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r);
425 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
426 return p != NULL;
427}
Adam Langley95c29f32014-06-20 12:00:00 -0700428
Adam Langleyfcf25832014-12-18 17:42:32 -0800429int SSL_CTX_set_purpose(SSL_CTX *s, int purpose) {
430 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
431}
432
433int SSL_set_purpose(SSL *s, int purpose) {
434 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
435}
436
437int SSL_CTX_set_trust(SSL_CTX *s, int trust) {
438 return X509_VERIFY_PARAM_set_trust(s->param, trust);
439}
440
441int SSL_set_trust(SSL *s, int trust) {
442 return X509_VERIFY_PARAM_set_trust(s->param, trust);
443}
444
445int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm) {
446 return X509_VERIFY_PARAM_set1(ctx->param, vpm);
447}
448
449int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm) {
450 return X509_VERIFY_PARAM_set1(ssl->param, vpm);
451}
Adam Langley95c29f32014-06-20 12:00:00 -0700452
Adam Langley858a88d2014-06-20 12:00:00 -0700453void ssl_cipher_preference_list_free(
Adam Langleyfcf25832014-12-18 17:42:32 -0800454 struct ssl_cipher_preference_list_st *cipher_list) {
David Benjamin5d1ec732015-04-22 13:38:00 -0400455 if (cipher_list == NULL) {
456 return;
457 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800458 sk_SSL_CIPHER_free(cipher_list->ciphers);
459 OPENSSL_free(cipher_list->in_group_flags);
460 OPENSSL_free(cipher_list);
461}
Adam Langley858a88d2014-06-20 12:00:00 -0700462
Adam Langleyfcf25832014-12-18 17:42:32 -0800463struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_dup(
464 struct ssl_cipher_preference_list_st *cipher_list) {
465 struct ssl_cipher_preference_list_st *ret = NULL;
466 size_t n = sk_SSL_CIPHER_num(cipher_list->ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700467
Adam Langleyfcf25832014-12-18 17:42:32 -0800468 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
469 if (!ret) {
470 goto err;
471 }
472
473 ret->ciphers = NULL;
474 ret->in_group_flags = NULL;
475 ret->ciphers = sk_SSL_CIPHER_dup(cipher_list->ciphers);
476 if (!ret->ciphers) {
477 goto err;
478 }
479 ret->in_group_flags = BUF_memdup(cipher_list->in_group_flags, n);
480 if (!ret->in_group_flags) {
481 goto err;
482 }
483
484 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -0700485
486err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400487 ssl_cipher_preference_list_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -0800488 return NULL;
489}
Adam Langley858a88d2014-06-20 12:00:00 -0700490
Adam Langleyfcf25832014-12-18 17:42:32 -0800491struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_from_ciphers(
492 STACK_OF(SSL_CIPHER) * ciphers) {
493 struct ssl_cipher_preference_list_st *ret = NULL;
494 size_t n = sk_SSL_CIPHER_num(ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700495
Adam Langleyfcf25832014-12-18 17:42:32 -0800496 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
497 if (!ret) {
498 goto err;
499 }
500 ret->ciphers = NULL;
501 ret->in_group_flags = NULL;
502 ret->ciphers = sk_SSL_CIPHER_dup(ciphers);
503 if (!ret->ciphers) {
504 goto err;
505 }
506 ret->in_group_flags = OPENSSL_malloc(n);
507 if (!ret->in_group_flags) {
508 goto err;
509 }
510 memset(ret->in_group_flags, 0, n);
511 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -0700512
513err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400514 ssl_cipher_preference_list_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -0800515 return NULL;
516}
Adam Langley858a88d2014-06-20 12:00:00 -0700517
Adam Langleyfcf25832014-12-18 17:42:32 -0800518X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) { return ctx->param; }
Adam Langley95c29f32014-06-20 12:00:00 -0700519
Adam Langleyfcf25832014-12-18 17:42:32 -0800520X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) { return ssl->param; }
Adam Langley95c29f32014-06-20 12:00:00 -0700521
Adam Langleyfcf25832014-12-18 17:42:32 -0800522void SSL_certs_clear(SSL *s) { ssl_cert_clear_certs(s->cert); }
Adam Langley95c29f32014-06-20 12:00:00 -0700523
Adam Langleyfcf25832014-12-18 17:42:32 -0800524void SSL_free(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800525 if (s == NULL) {
526 return;
527 }
Adam Langley95c29f32014-06-20 12:00:00 -0700528
David Benjamin2755a3e2015-04-22 16:17:58 -0400529 X509_VERIFY_PARAM_free(s->param);
Adam Langley95c29f32014-06-20 12:00:00 -0700530
David Benjamin9f33fc62015-04-15 17:29:53 -0400531 CRYPTO_free_ex_data(&g_ex_data_class_ssl, s, &s->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700532
Adam Langleyfcf25832014-12-18 17:42:32 -0800533 if (s->bbio != NULL) {
534 /* If the buffering BIO is in place, pop it off */
535 if (s->bbio == s->wbio) {
536 s->wbio = BIO_pop(s->wbio);
537 }
538 BIO_free(s->bbio);
539 s->bbio = NULL;
540 }
Adam Langley95c29f32014-06-20 12:00:00 -0700541
David Benjamin2755a3e2015-04-22 16:17:58 -0400542 int free_wbio = s->wbio != s->rbio;
543 BIO_free_all(s->rbio);
544 if (free_wbio) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800545 BIO_free_all(s->wbio);
546 }
Adam Langley95c29f32014-06-20 12:00:00 -0700547
David Benjamin2755a3e2015-04-22 16:17:58 -0400548 BUF_MEM_free(s->init_buf);
Adam Langley95c29f32014-06-20 12:00:00 -0700549
Adam Langleyfcf25832014-12-18 17:42:32 -0800550 /* add extra stuff */
David Benjamin2755a3e2015-04-22 16:17:58 -0400551 ssl_cipher_preference_list_free(s->cipher_list);
552 sk_SSL_CIPHER_free(s->cipher_list_by_id);
Adam Langley95c29f32014-06-20 12:00:00 -0700553
David Benjamin2755a3e2015-04-22 16:17:58 -0400554 ssl_clear_bad_session(s);
555 SSL_SESSION_free(s->session);
Adam Langley95c29f32014-06-20 12:00:00 -0700556
Adam Langleyfcf25832014-12-18 17:42:32 -0800557 ssl_clear_cipher_ctx(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700558
David Benjamin2755a3e2015-04-22 16:17:58 -0400559 ssl_cert_free(s->cert);
Adam Langley0289c732014-06-20 12:00:00 -0700560
David Benjamin2755a3e2015-04-22 16:17:58 -0400561 OPENSSL_free(s->tlsext_hostname);
562 SSL_CTX_free(s->initial_ctx);
563 OPENSSL_free(s->tlsext_ecpointformatlist);
564 OPENSSL_free(s->tlsext_ellipticcurvelist);
565 OPENSSL_free(s->alpn_client_proto_list);
566 EVP_PKEY_free(s->tlsext_channel_id_private);
567 OPENSSL_free(s->psk_identity_hint);
568 sk_X509_NAME_pop_free(s->client_CA, X509_NAME_free);
569 OPENSSL_free(s->next_proto_negotiated);
570 sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);
Adam Langley95c29f32014-06-20 12:00:00 -0700571
Adam Langleyfcf25832014-12-18 17:42:32 -0800572 if (s->method != NULL) {
573 s->method->ssl_free(s);
574 }
David Benjamin2755a3e2015-04-22 16:17:58 -0400575 SSL_CTX_free(s->ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700576
Adam Langleyfcf25832014-12-18 17:42:32 -0800577 OPENSSL_free(s);
578}
Adam Langley95c29f32014-06-20 12:00:00 -0700579
Adam Langleyfcf25832014-12-18 17:42:32 -0800580void SSL_set_bio(SSL *s, BIO *rbio, BIO *wbio) {
581 /* If the output buffering BIO is still in place, remove it. */
582 if (s->bbio != NULL) {
583 if (s->wbio == s->bbio) {
584 s->wbio = s->wbio->next_bio;
585 s->bbio->next_bio = NULL;
586 }
587 }
Adam Langley95c29f32014-06-20 12:00:00 -0700588
David Benjamin2755a3e2015-04-22 16:17:58 -0400589 if (s->rbio != rbio) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800590 BIO_free_all(s->rbio);
591 }
David Benjamin2755a3e2015-04-22 16:17:58 -0400592 if (s->wbio != wbio && s->rbio != s->wbio) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800593 BIO_free_all(s->wbio);
594 }
595 s->rbio = rbio;
596 s->wbio = wbio;
597}
Adam Langley95c29f32014-06-20 12:00:00 -0700598
Adam Langleyfcf25832014-12-18 17:42:32 -0800599BIO *SSL_get_rbio(const SSL *s) { return s->rbio; }
Adam Langley95c29f32014-06-20 12:00:00 -0700600
Adam Langleyfcf25832014-12-18 17:42:32 -0800601BIO *SSL_get_wbio(const SSL *s) { return s->wbio; }
Adam Langley95c29f32014-06-20 12:00:00 -0700602
Adam Langleyfcf25832014-12-18 17:42:32 -0800603int SSL_get_fd(const SSL *s) { return SSL_get_rfd(s); }
Adam Langley95c29f32014-06-20 12:00:00 -0700604
Adam Langleyfcf25832014-12-18 17:42:32 -0800605int SSL_get_rfd(const SSL *s) {
606 int ret = -1;
607 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700608
Adam Langleyfcf25832014-12-18 17:42:32 -0800609 b = SSL_get_rbio(s);
610 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
611 if (r != NULL) {
612 BIO_get_fd(r, &ret);
613 }
614 return ret;
615}
Adam Langley95c29f32014-06-20 12:00:00 -0700616
Adam Langleyfcf25832014-12-18 17:42:32 -0800617int SSL_get_wfd(const SSL *s) {
618 int ret = -1;
619 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700620
Adam Langleyfcf25832014-12-18 17:42:32 -0800621 b = SSL_get_wbio(s);
622 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
623 if (r != NULL) {
624 BIO_get_fd(r, &ret);
625 }
Adam Langley95c29f32014-06-20 12:00:00 -0700626
Adam Langleyfcf25832014-12-18 17:42:32 -0800627 return ret;
628}
Adam Langley95c29f32014-06-20 12:00:00 -0700629
Adam Langleyfcf25832014-12-18 17:42:32 -0800630int SSL_set_fd(SSL *s, int fd) {
631 int ret = 0;
632 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700633
Adam Langleyfcf25832014-12-18 17:42:32 -0800634 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700635
Adam Langleyfcf25832014-12-18 17:42:32 -0800636 if (bio == NULL) {
637 OPENSSL_PUT_ERROR(SSL, SSL_set_fd, ERR_R_BUF_LIB);
638 goto err;
639 }
640 BIO_set_fd(bio, fd, BIO_NOCLOSE);
641 SSL_set_bio(s, bio, bio);
642 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700643
Adam Langley95c29f32014-06-20 12:00:00 -0700644err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800645 return ret;
646}
Adam Langley95c29f32014-06-20 12:00:00 -0700647
Adam Langleyfcf25832014-12-18 17:42:32 -0800648int SSL_set_wfd(SSL *s, int fd) {
649 int ret = 0;
650 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700651
Adam Langleyfcf25832014-12-18 17:42:32 -0800652 if (s->rbio == NULL || BIO_method_type(s->rbio) != BIO_TYPE_FD ||
653 (int)BIO_get_fd(s->rbio, NULL) != fd) {
654 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700655
Adam Langleyfcf25832014-12-18 17:42:32 -0800656 if (bio == NULL) {
657 OPENSSL_PUT_ERROR(SSL, SSL_set_wfd, ERR_R_BUF_LIB);
658 goto err;
659 }
660 BIO_set_fd(bio, fd, BIO_NOCLOSE);
661 SSL_set_bio(s, SSL_get_rbio(s), bio);
662 } else {
663 SSL_set_bio(s, SSL_get_rbio(s), SSL_get_rbio(s));
664 }
665
666 ret = 1;
667
Adam Langley95c29f32014-06-20 12:00:00 -0700668err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800669 return ret;
670}
Adam Langley95c29f32014-06-20 12:00:00 -0700671
Adam Langleyfcf25832014-12-18 17:42:32 -0800672int SSL_set_rfd(SSL *s, int fd) {
673 int ret = 0;
674 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700675
Adam Langleyfcf25832014-12-18 17:42:32 -0800676 if (s->wbio == NULL || BIO_method_type(s->wbio) != BIO_TYPE_FD ||
677 (int)BIO_get_fd(s->wbio, NULL) != fd) {
678 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700679
Adam Langleyfcf25832014-12-18 17:42:32 -0800680 if (bio == NULL) {
681 OPENSSL_PUT_ERROR(SSL, SSL_set_rfd, ERR_R_BUF_LIB);
682 goto err;
683 }
684 BIO_set_fd(bio, fd, BIO_NOCLOSE);
685 SSL_set_bio(s, bio, SSL_get_wbio(s));
686 } else {
687 SSL_set_bio(s, SSL_get_wbio(s), SSL_get_wbio(s));
688 }
689 ret = 1;
690
Adam Langley95c29f32014-06-20 12:00:00 -0700691err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800692 return ret;
693}
Adam Langley95c29f32014-06-20 12:00:00 -0700694
695/* return length of latest Finished message we sent, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -0800696size_t SSL_get_finished(const SSL *s, void *buf, size_t count) {
697 size_t ret = 0;
698
699 if (s->s3 != NULL) {
700 ret = s->s3->tmp.finish_md_len;
701 if (count > ret) {
702 count = ret;
703 }
704 memcpy(buf, s->s3->tmp.finish_md, count);
705 }
706
707 return ret;
708}
Adam Langley95c29f32014-06-20 12:00:00 -0700709
710/* return length of latest Finished message we expected, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -0800711size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count) {
712 size_t ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700713
Adam Langleyfcf25832014-12-18 17:42:32 -0800714 if (s->s3 != NULL) {
715 ret = s->s3->tmp.peer_finish_md_len;
716 if (count > ret) {
717 count = ret;
718 }
719 memcpy(buf, s->s3->tmp.peer_finish_md, count);
720 }
Adam Langley95c29f32014-06-20 12:00:00 -0700721
Adam Langleyfcf25832014-12-18 17:42:32 -0800722 return ret;
723}
Adam Langley95c29f32014-06-20 12:00:00 -0700724
Adam Langleyfcf25832014-12-18 17:42:32 -0800725int SSL_get_verify_mode(const SSL *s) { return s->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -0700726
Adam Langleyfcf25832014-12-18 17:42:32 -0800727int SSL_get_verify_depth(const SSL *s) {
728 return X509_VERIFY_PARAM_get_depth(s->param);
729}
Adam Langley95c29f32014-06-20 12:00:00 -0700730
Adam Langleyfcf25832014-12-18 17:42:32 -0800731int (*SSL_get_verify_callback(const SSL *s))(int, X509_STORE_CTX *) {
732 return s->verify_callback;
733}
Adam Langley95c29f32014-06-20 12:00:00 -0700734
Adam Langleyfcf25832014-12-18 17:42:32 -0800735int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) { return ctx->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -0700736
Adam Langleyfcf25832014-12-18 17:42:32 -0800737int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) {
738 return X509_VERIFY_PARAM_get_depth(ctx->param);
739}
Adam Langley95c29f32014-06-20 12:00:00 -0700740
Adam Langleyfcf25832014-12-18 17:42:32 -0800741int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int, X509_STORE_CTX *) {
742 return ctx->default_verify_callback;
743}
Adam Langley95c29f32014-06-20 12:00:00 -0700744
Adam Langleyfcf25832014-12-18 17:42:32 -0800745void SSL_set_verify(SSL *s, int mode,
746 int (*callback)(int ok, X509_STORE_CTX *ctx)) {
747 s->verify_mode = mode;
748 if (callback != NULL) {
749 s->verify_callback = callback;
750 }
751}
Adam Langley95c29f32014-06-20 12:00:00 -0700752
Adam Langleyfcf25832014-12-18 17:42:32 -0800753void SSL_set_verify_depth(SSL *s, int depth) {
754 X509_VERIFY_PARAM_set_depth(s->param, depth);
755}
Adam Langley95c29f32014-06-20 12:00:00 -0700756
Adam Langleyfcf25832014-12-18 17:42:32 -0800757void SSL_set_read_ahead(SSL *s, int yes) { s->read_ahead = yes; }
Adam Langley95c29f32014-06-20 12:00:00 -0700758
Adam Langleyfcf25832014-12-18 17:42:32 -0800759int SSL_get_read_ahead(const SSL *s) { return s->read_ahead; }
Adam Langley95c29f32014-06-20 12:00:00 -0700760
Adam Langleyfcf25832014-12-18 17:42:32 -0800761int SSL_pending(const SSL *s) {
762 /* SSL_pending cannot work properly if read-ahead is enabled
763 * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)), and it is
764 * impossible to fix since SSL_pending cannot report errors that may be
765 * observed while scanning the new data. (Note that SSL_pending() is often
766 * used as a boolean value, so we'd better not return -1.). */
767 return s->method->ssl_pending(s);
768}
Adam Langley95c29f32014-06-20 12:00:00 -0700769
Adam Langleyfcf25832014-12-18 17:42:32 -0800770X509 *SSL_get_peer_certificate(const SSL *s) {
771 X509 *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700772
Adam Langleyfcf25832014-12-18 17:42:32 -0800773 if (s == NULL || s->session == NULL) {
774 r = NULL;
775 } else {
776 r = s->session->peer;
777 }
Adam Langley95c29f32014-06-20 12:00:00 -0700778
Adam Langleyfcf25832014-12-18 17:42:32 -0800779 if (r == NULL) {
780 return NULL;
781 }
Adam Langley95c29f32014-06-20 12:00:00 -0700782
Adam Langleyfcf25832014-12-18 17:42:32 -0800783 return X509_up_ref(r);
784}
785
786STACK_OF(X509) * SSL_get_peer_cert_chain(const SSL *s) {
787 STACK_OF(X509) * r;
788
789 if (s == NULL || s->session == NULL || s->session->sess_cert == NULL) {
790 r = NULL;
791 } else {
792 r = s->session->sess_cert->cert_chain;
793 }
794
795 /* If we are a client, cert_chain includes the peer's own certificate; if we
796 * are a server, it does not. */
797 return r;
798}
Adam Langley95c29f32014-06-20 12:00:00 -0700799
Adam Langley95c29f32014-06-20 12:00:00 -0700800/* Fix this so it checks all the valid key/cert options */
Adam Langleyfcf25832014-12-18 17:42:32 -0800801int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
802 if (ctx == NULL || ctx->cert == NULL || ctx->cert->key->x509 == NULL) {
803 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key,
804 SSL_R_NO_CERTIFICATE_ASSIGNED);
805 return 0;
806 }
807
808 if (ctx->cert->key->privatekey == NULL) {
809 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key,
810 SSL_R_NO_PRIVATE_KEY_ASSIGNED);
811 return 0;
812 }
813
814 return X509_check_private_key(ctx->cert->key->x509,
815 ctx->cert->key->privatekey);
816}
Adam Langley95c29f32014-06-20 12:00:00 -0700817
818/* Fix this function so that it takes an optional type parameter */
Adam Langleyfcf25832014-12-18 17:42:32 -0800819int SSL_check_private_key(const SSL *ssl) {
820 if (ssl == NULL) {
821 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, ERR_R_PASSED_NULL_PARAMETER);
822 return 0;
823 }
Adam Langley95c29f32014-06-20 12:00:00 -0700824
Adam Langleyfcf25832014-12-18 17:42:32 -0800825 if (ssl->cert == NULL) {
826 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
827 SSL_R_NO_CERTIFICATE_ASSIGNED);
828 return 0;
829 }
Adam Langley95c29f32014-06-20 12:00:00 -0700830
Adam Langleyfcf25832014-12-18 17:42:32 -0800831 if (ssl->cert->key->x509 == NULL) {
832 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
833 SSL_R_NO_CERTIFICATE_ASSIGNED);
834 return 0;
835 }
David Benjamin0b145c22014-11-26 20:10:09 -0500836
Adam Langleyfcf25832014-12-18 17:42:32 -0800837 if (ssl->cert->key->privatekey == NULL) {
838 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
839 SSL_R_NO_PRIVATE_KEY_ASSIGNED);
840 return 0;
841 }
Adam Langley95c29f32014-06-20 12:00:00 -0700842
Adam Langleyfcf25832014-12-18 17:42:32 -0800843 return X509_check_private_key(ssl->cert->key->x509,
844 ssl->cert->key->privatekey);
845}
Adam Langley95c29f32014-06-20 12:00:00 -0700846
Adam Langleyfcf25832014-12-18 17:42:32 -0800847int SSL_accept(SSL *s) {
848 if (s->handshake_func == 0) {
849 /* Not properly initialized yet */
850 SSL_set_accept_state(s);
851 }
David Benjamin0b145c22014-11-26 20:10:09 -0500852
Adam Langleyfcf25832014-12-18 17:42:32 -0800853 if (s->handshake_func != s->method->ssl_accept) {
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +0100854 OPENSSL_PUT_ERROR(SSL, SSL_accept, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800855 return -1;
856 }
Adam Langley95c29f32014-06-20 12:00:00 -0700857
Adam Langleyfcf25832014-12-18 17:42:32 -0800858 return s->handshake_func(s);
859}
Adam Langley95c29f32014-06-20 12:00:00 -0700860
Adam Langleyfcf25832014-12-18 17:42:32 -0800861int SSL_connect(SSL *s) {
862 if (s->handshake_func == 0) {
863 /* Not properly initialized yet */
864 SSL_set_connect_state(s);
865 }
Adam Langley95c29f32014-06-20 12:00:00 -0700866
Adam Langleyfcf25832014-12-18 17:42:32 -0800867 if (s->handshake_func != s->method->ssl_connect) {
868 OPENSSL_PUT_ERROR(SSL, SSL_connect, ERR_R_INTERNAL_ERROR);
869 return -1;
870 }
Adam Langley95c29f32014-06-20 12:00:00 -0700871
Adam Langleyfcf25832014-12-18 17:42:32 -0800872 return s->handshake_func(s);
873}
Adam Langley95c29f32014-06-20 12:00:00 -0700874
Adam Langleyfcf25832014-12-18 17:42:32 -0800875long SSL_get_default_timeout(const SSL *s) {
876 return SSL_DEFAULT_SESSION_TIMEOUT;
877}
Adam Langley95c29f32014-06-20 12:00:00 -0700878
Adam Langleyfcf25832014-12-18 17:42:32 -0800879int SSL_read(SSL *s, void *buf, int num) {
880 if (s->handshake_func == 0) {
881 OPENSSL_PUT_ERROR(SSL, SSL_read, SSL_R_UNINITIALIZED);
882 return -1;
883 }
Adam Langley95c29f32014-06-20 12:00:00 -0700884
Adam Langleyfcf25832014-12-18 17:42:32 -0800885 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
886 s->rwstate = SSL_NOTHING;
887 return 0;
888 }
Adam Langley95c29f32014-06-20 12:00:00 -0700889
Adam Langleyfcf25832014-12-18 17:42:32 -0800890 return s->method->ssl_read(s, buf, num);
891}
Adam Langley95c29f32014-06-20 12:00:00 -0700892
Adam Langleyfcf25832014-12-18 17:42:32 -0800893int SSL_peek(SSL *s, void *buf, int num) {
894 if (s->handshake_func == 0) {
895 OPENSSL_PUT_ERROR(SSL, SSL_peek, SSL_R_UNINITIALIZED);
896 return -1;
897 }
Adam Langley95c29f32014-06-20 12:00:00 -0700898
Adam Langleyfcf25832014-12-18 17:42:32 -0800899 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
900 return 0;
901 }
Adam Langley95c29f32014-06-20 12:00:00 -0700902
Adam Langleyfcf25832014-12-18 17:42:32 -0800903 return s->method->ssl_peek(s, buf, num);
904}
Adam Langley95c29f32014-06-20 12:00:00 -0700905
Adam Langleyfcf25832014-12-18 17:42:32 -0800906int SSL_write(SSL *s, const void *buf, int num) {
907 if (s->handshake_func == 0) {
908 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_UNINITIALIZED);
909 return -1;
910 }
Adam Langley95c29f32014-06-20 12:00:00 -0700911
Adam Langleyfcf25832014-12-18 17:42:32 -0800912 if (s->shutdown & SSL_SENT_SHUTDOWN) {
913 s->rwstate = SSL_NOTHING;
914 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_PROTOCOL_IS_SHUTDOWN);
915 return -1;
916 }
Adam Langley95c29f32014-06-20 12:00:00 -0700917
Adam Langleyfcf25832014-12-18 17:42:32 -0800918 return s->method->ssl_write(s, buf, num);
919}
Adam Langley95c29f32014-06-20 12:00:00 -0700920
Adam Langleyfcf25832014-12-18 17:42:32 -0800921int SSL_shutdown(SSL *s) {
922 /* Note that this function behaves differently from what one might expect.
923 * Return values are 0 for no success (yet), 1 for success; but calling it
924 * once is usually not enough, even if blocking I/O is used (see
925 * ssl3_shutdown). */
Adam Langley95c29f32014-06-20 12:00:00 -0700926
Adam Langleyfcf25832014-12-18 17:42:32 -0800927 if (s->handshake_func == 0) {
928 OPENSSL_PUT_ERROR(SSL, SSL_shutdown, SSL_R_UNINITIALIZED);
929 return -1;
930 }
Adam Langley95c29f32014-06-20 12:00:00 -0700931
Adam Langleyfcf25832014-12-18 17:42:32 -0800932 if (!SSL_in_init(s)) {
933 return s->method->ssl_shutdown(s);
934 }
Adam Langley95c29f32014-06-20 12:00:00 -0700935
Adam Langleyfcf25832014-12-18 17:42:32 -0800936 return 1;
937}
Adam Langley95c29f32014-06-20 12:00:00 -0700938
Adam Langleyfcf25832014-12-18 17:42:32 -0800939int SSL_renegotiate(SSL *s) {
David Benjamin2cdace92015-01-24 13:30:02 -0500940 if (SSL_IS_DTLS(s)) {
941 /* Renegotiation is not supported for DTLS. */
942 OPENSSL_PUT_ERROR(SSL, SSL_renegotiate, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
943 return 0;
944 }
945
Adam Langleyfcf25832014-12-18 17:42:32 -0800946 if (s->renegotiate == 0) {
947 s->renegotiate = 1;
948 }
Adam Langley95c29f32014-06-20 12:00:00 -0700949
Adam Langleyfcf25832014-12-18 17:42:32 -0800950 s->new_session = 1;
951 return s->method->ssl_renegotiate(s);
952}
Adam Langley95c29f32014-06-20 12:00:00 -0700953
Adam Langleyfcf25832014-12-18 17:42:32 -0800954int SSL_renegotiate_pending(SSL *s) {
955 /* becomes true when negotiation is requested; false again once a handshake
956 * has finished */
957 return s->renegotiate != 0;
958}
Adam Langley95c29f32014-06-20 12:00:00 -0700959
Adam Langleyfcf25832014-12-18 17:42:32 -0800960long SSL_ctrl(SSL *s, int cmd, long larg, void *parg) {
961 long l;
Adam Langley95c29f32014-06-20 12:00:00 -0700962
Adam Langleyfcf25832014-12-18 17:42:32 -0800963 switch (cmd) {
964 case SSL_CTRL_GET_READ_AHEAD:
965 return s->read_ahead;
Adam Langley95c29f32014-06-20 12:00:00 -0700966
Adam Langleyfcf25832014-12-18 17:42:32 -0800967 case SSL_CTRL_SET_READ_AHEAD:
968 l = s->read_ahead;
969 s->read_ahead = larg;
970 return l;
Adam Langley95c29f32014-06-20 12:00:00 -0700971
Adam Langleyfcf25832014-12-18 17:42:32 -0800972 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
973 s->msg_callback_arg = parg;
974 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700975
Adam Langleyfcf25832014-12-18 17:42:32 -0800976 case SSL_CTRL_OPTIONS:
977 return s->options |= larg;
Adam Langley95c29f32014-06-20 12:00:00 -0700978
Adam Langleyfcf25832014-12-18 17:42:32 -0800979 case SSL_CTRL_CLEAR_OPTIONS:
980 return s->options &= ~larg;
Adam Langley95c29f32014-06-20 12:00:00 -0700981
Adam Langleyfcf25832014-12-18 17:42:32 -0800982 case SSL_CTRL_MODE:
983 return s->mode |= larg;
Adam Langley95c29f32014-06-20 12:00:00 -0700984
Adam Langleyfcf25832014-12-18 17:42:32 -0800985 case SSL_CTRL_CLEAR_MODE:
986 return s->mode &= ~larg;
Adam Langley95c29f32014-06-20 12:00:00 -0700987
Adam Langleyfcf25832014-12-18 17:42:32 -0800988 case SSL_CTRL_GET_MAX_CERT_LIST:
989 return s->max_cert_list;
Adam Langley95c29f32014-06-20 12:00:00 -0700990
Adam Langleyfcf25832014-12-18 17:42:32 -0800991 case SSL_CTRL_SET_MAX_CERT_LIST:
992 l = s->max_cert_list;
993 s->max_cert_list = larg;
994 return l;
Adam Langley95c29f32014-06-20 12:00:00 -0700995
Adam Langleyfcf25832014-12-18 17:42:32 -0800996 case SSL_CTRL_SET_MTU:
997 if (larg < (long)dtls1_min_mtu()) {
998 return 0;
999 }
1000 if (SSL_IS_DTLS(s)) {
1001 s->d1->mtu = larg;
1002 return larg;
1003 }
1004 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001005
Adam Langleyfcf25832014-12-18 17:42:32 -08001006 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1007 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH) {
1008 return 0;
1009 }
1010 s->max_send_fragment = larg;
1011 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001012
Adam Langleyfcf25832014-12-18 17:42:32 -08001013 case SSL_CTRL_GET_RI_SUPPORT:
1014 if (s->s3) {
1015 return s->s3->send_connection_binding;
1016 }
1017 return 0;
1018
Adam Langleyfcf25832014-12-18 17:42:32 -08001019 case SSL_CTRL_GET_RAW_CIPHERLIST:
1020 if (parg) {
1021 if (s->cert->ciphers_raw == NULL) {
1022 return 0;
1023 }
1024 *(uint8_t **)parg = s->cert->ciphers_raw;
1025 return (int)s->cert->ciphers_rawlen;
1026 }
1027
1028 /* Passing a NULL |parg| returns the size of a single
1029 * cipher suite value. */
1030 return 2;
1031
1032 default:
1033 return s->method->ssl_ctrl(s, cmd, larg, parg);
1034 }
1035}
1036
Adam Langleyfcf25832014-12-18 17:42:32 -08001037LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) { return ctx->sessions; }
1038
1039long SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg) {
1040 long l;
1041
1042 switch (cmd) {
1043 case SSL_CTRL_GET_READ_AHEAD:
1044 return ctx->read_ahead;
1045
1046 case SSL_CTRL_SET_READ_AHEAD:
1047 l = ctx->read_ahead;
1048 ctx->read_ahead = larg;
1049 return l;
1050
1051 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1052 ctx->msg_callback_arg = parg;
1053 return 1;
1054
1055 case SSL_CTRL_GET_MAX_CERT_LIST:
1056 return ctx->max_cert_list;
1057
1058 case SSL_CTRL_SET_MAX_CERT_LIST:
1059 l = ctx->max_cert_list;
1060 ctx->max_cert_list = larg;
1061 return l;
1062
1063 case SSL_CTRL_SET_SESS_CACHE_SIZE:
1064 l = ctx->session_cache_size;
1065 ctx->session_cache_size = larg;
1066 return l;
1067
1068 case SSL_CTRL_GET_SESS_CACHE_SIZE:
1069 return ctx->session_cache_size;
1070
1071 case SSL_CTRL_SET_SESS_CACHE_MODE:
1072 l = ctx->session_cache_mode;
1073 ctx->session_cache_mode = larg;
1074 return l;
1075
1076 case SSL_CTRL_GET_SESS_CACHE_MODE:
1077 return ctx->session_cache_mode;
1078
1079 case SSL_CTRL_SESS_NUMBER:
1080 return lh_SSL_SESSION_num_items(ctx->sessions);
1081
1082 case SSL_CTRL_SESS_CONNECT:
Adam Langleyfcf25832014-12-18 17:42:32 -08001083 case SSL_CTRL_SESS_CONNECT_GOOD:
Adam Langleyfcf25832014-12-18 17:42:32 -08001084 case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001085 case SSL_CTRL_SESS_ACCEPT:
Adam Langleyfcf25832014-12-18 17:42:32 -08001086 case SSL_CTRL_SESS_ACCEPT_GOOD:
Adam Langleyfcf25832014-12-18 17:42:32 -08001087 case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001088 case SSL_CTRL_SESS_HIT:
Adam Langleyfcf25832014-12-18 17:42:32 -08001089 case SSL_CTRL_SESS_CB_HIT:
Adam Langleyfcf25832014-12-18 17:42:32 -08001090 case SSL_CTRL_SESS_MISSES:
Adam Langleyfcf25832014-12-18 17:42:32 -08001091 case SSL_CTRL_SESS_TIMEOUTS:
Adam Langleyfcf25832014-12-18 17:42:32 -08001092 case SSL_CTRL_SESS_CACHE_FULL:
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001093 /* Statistics are no longer supported.
1094 * TODO(davidben): Try to remove the accessors altogether. */
1095 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001096
1097 case SSL_CTRL_OPTIONS:
1098 return ctx->options |= larg;
1099
1100 case SSL_CTRL_CLEAR_OPTIONS:
1101 return ctx->options &= ~larg;
1102
1103 case SSL_CTRL_MODE:
1104 return ctx->mode |= larg;
1105
1106 case SSL_CTRL_CLEAR_MODE:
1107 return ctx->mode &= ~larg;
1108
1109 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1110 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH) {
1111 return 0;
1112 }
1113 ctx->max_send_fragment = larg;
1114 return 1;
1115
Adam Langleyfcf25832014-12-18 17:42:32 -08001116 default:
1117 return ctx->method->ssl_ctx_ctrl(ctx, cmd, larg, parg);
1118 }
1119}
1120
Adam Langleyfcf25832014-12-18 17:42:32 -08001121int ssl_cipher_id_cmp(const void *in_a, const void *in_b) {
1122 long l;
1123 const SSL_CIPHER *a = in_a;
1124 const SSL_CIPHER *b = in_b;
1125 const long a_id = a->id;
1126 const long b_id = b->id;
1127
1128 l = a_id - b_id;
1129 if (l == 0L) {
1130 return 0;
1131 } else {
1132 return (l > 0) ? 1 : -1;
1133 }
1134}
1135
1136int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **ap, const SSL_CIPHER **bp) {
1137 long l;
1138 const long a_id = (*ap)->id;
1139 const long b_id = (*bp)->id;
1140
1141 l = a_id - b_id;
1142 if (l == 0) {
1143 return 0;
1144 } else {
1145 return (l > 0) ? 1 : -1;
1146 }
1147}
1148
1149/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001150 * preference */
Adam Langleyfcf25832014-12-18 17:42:32 -08001151STACK_OF(SSL_CIPHER) * SSL_get_ciphers(const SSL *s) {
1152 if (s == NULL) {
1153 return NULL;
1154 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001155
Adam Langleyfcf25832014-12-18 17:42:32 -08001156 if (s->cipher_list != NULL) {
1157 return s->cipher_list->ciphers;
1158 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001159
Adam Langleyfcf25832014-12-18 17:42:32 -08001160 if (s->version >= TLS1_1_VERSION && s->ctx != NULL &&
1161 s->ctx->cipher_list_tls11 != NULL) {
1162 return s->ctx->cipher_list_tls11->ciphers;
1163 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001164
Adam Langleyfcf25832014-12-18 17:42:32 -08001165 if (s->ctx != NULL && s->ctx->cipher_list != NULL) {
1166 return s->ctx->cipher_list->ciphers;
1167 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001168
Adam Langleyfcf25832014-12-18 17:42:32 -08001169 return NULL;
1170}
Adam Langley95c29f32014-06-20 12:00:00 -07001171
Adam Langleyfcf25832014-12-18 17:42:32 -08001172/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001173 * algorithm id */
Adam Langleyfcf25832014-12-18 17:42:32 -08001174STACK_OF(SSL_CIPHER) * ssl_get_ciphers_by_id(SSL *s) {
1175 if (s == NULL) {
1176 return NULL;
1177 }
Adam Langley95c29f32014-06-20 12:00:00 -07001178
Adam Langleyfcf25832014-12-18 17:42:32 -08001179 if (s->cipher_list_by_id != NULL) {
1180 return s->cipher_list_by_id;
1181 }
Adam Langley95c29f32014-06-20 12:00:00 -07001182
Adam Langleyfcf25832014-12-18 17:42:32 -08001183 if (s->ctx != NULL && s->ctx->cipher_list_by_id != NULL) {
1184 return s->ctx->cipher_list_by_id;
1185 }
Adam Langley95c29f32014-06-20 12:00:00 -07001186
Adam Langleyfcf25832014-12-18 17:42:32 -08001187 return NULL;
1188}
Adam Langley95c29f32014-06-20 12:00:00 -07001189
Adam Langleyfcf25832014-12-18 17:42:32 -08001190/* The old interface to get the same thing as SSL_get_ciphers() */
1191const char *SSL_get_cipher_list(const SSL *s, int n) {
1192 const SSL_CIPHER *c;
1193 STACK_OF(SSL_CIPHER) * sk;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001194
Adam Langleyfcf25832014-12-18 17:42:32 -08001195 if (s == NULL) {
1196 return NULL;
1197 }
Adam Langley95c29f32014-06-20 12:00:00 -07001198
Adam Langleyfcf25832014-12-18 17:42:32 -08001199 sk = SSL_get_ciphers(s);
1200 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
1201 return NULL;
1202 }
Adam Langley95c29f32014-06-20 12:00:00 -07001203
Adam Langleyfcf25832014-12-18 17:42:32 -08001204 c = sk_SSL_CIPHER_value(sk, n);
1205 if (c == NULL) {
1206 return NULL;
1207 }
Adam Langley95c29f32014-06-20 12:00:00 -07001208
Adam Langleyfcf25832014-12-18 17:42:32 -08001209 return c->name;
1210}
David Benjamin5491e3f2014-09-29 19:33:09 -04001211
Adam Langleyfcf25832014-12-18 17:42:32 -08001212/* specify the ciphers to be used by default by the SSL_CTX */
1213int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
1214 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001215
Adam Langleyfcf25832014-12-18 17:42:32 -08001216 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001217 &ctx->cipher_list_by_id, str);
Adam Langleyfcf25832014-12-18 17:42:32 -08001218 /* ssl_create_cipher_list may return an empty stack if it was unable to find
1219 * a cipher matching the given rule string (for example if the rule string
1220 * specifies a cipher which has been disabled). This is not an error as far
1221 * as ssl_create_cipher_list is concerned, and hence ctx->cipher_list and
1222 * ctx->cipher_list_by_id has been updated. */
1223 if (sk == NULL) {
1224 return 0;
1225 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1226 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1227 return 0;
1228 }
Adam Langley95c29f32014-06-20 12:00:00 -07001229
Adam Langleyfcf25832014-12-18 17:42:32 -08001230 return 1;
1231}
David Benjamin39482a12014-07-20 13:30:15 -04001232
Adam Langleyfcf25832014-12-18 17:42:32 -08001233int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str) {
1234 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001235
David Benjamin71f07942015-04-08 02:36:59 -04001236 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str);
Adam Langleyfcf25832014-12-18 17:42:32 -08001237 if (sk == NULL) {
1238 return 0;
1239 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1240 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list_tls11,
1241 SSL_R_NO_CIPHER_MATCH);
1242 return 0;
1243 }
David Benjamin9f2c0d72014-10-21 22:00:19 -04001244
Adam Langleyfcf25832014-12-18 17:42:32 -08001245 return 1;
1246}
Adam Langley95c29f32014-06-20 12:00:00 -07001247
Adam Langleyfcf25832014-12-18 17:42:32 -08001248/* specify the ciphers to be used by the SSL */
1249int SSL_set_cipher_list(SSL *s, const char *str) {
1250 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001251
Adam Langleyfcf25832014-12-18 17:42:32 -08001252 sk = ssl_create_cipher_list(s->ctx->method, &s->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001253 &s->cipher_list_by_id, str);
David Benjamin39482a12014-07-20 13:30:15 -04001254
Adam Langleyfcf25832014-12-18 17:42:32 -08001255 /* see comment in SSL_CTX_set_cipher_list */
1256 if (sk == NULL) {
1257 return 0;
1258 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1259 OPENSSL_PUT_ERROR(SSL, SSL_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1260 return 0;
1261 }
David Benjamin39482a12014-07-20 13:30:15 -04001262
Adam Langleyfcf25832014-12-18 17:42:32 -08001263 return 1;
1264}
Adam Langley95c29f32014-06-20 12:00:00 -07001265
Adam Langleyfcf25832014-12-18 17:42:32 -08001266int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, uint8_t *p) {
1267 size_t i;
1268 const SSL_CIPHER *c;
1269 CERT *ct = s->cert;
1270 uint8_t *q;
1271 /* Set disabled masks for this session */
1272 ssl_set_client_disabled(s);
Adam Langley29707792014-06-20 12:00:00 -07001273
Adam Langleyfcf25832014-12-18 17:42:32 -08001274 if (sk == NULL) {
1275 return 0;
1276 }
1277 q = p;
Adam Langley95c29f32014-06-20 12:00:00 -07001278
Adam Langleyfcf25832014-12-18 17:42:32 -08001279 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
1280 c = sk_SSL_CIPHER_value(sk, i);
1281 /* Skip disabled ciphers */
1282 if (c->algorithm_ssl & ct->mask_ssl ||
1283 c->algorithm_mkey & ct->mask_k ||
1284 c->algorithm_auth & ct->mask_a) {
1285 continue;
1286 }
1287 s2n(ssl3_get_cipher_value(c), p);
1288 }
1289
1290 /* If all ciphers were disabled, return the error to the caller. */
1291 if (p == q) {
1292 return 0;
1293 }
1294
1295 /* Add SCSVs. */
1296 if (!s->renegotiate) {
1297 s2n(SSL3_CK_SCSV & 0xffff, p);
1298 }
1299
Adam Langley5f0efe02015-02-20 13:03:16 -08001300 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001301 s2n(SSL3_CK_FALLBACK_SCSV & 0xffff, p);
1302 }
1303
1304 return p - q;
1305}
1306
1307STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs) {
1308 CBS cipher_suites = *cbs;
1309 const SSL_CIPHER *c;
1310 STACK_OF(SSL_CIPHER) * sk;
1311
1312 if (s->s3) {
1313 s->s3->send_connection_binding = 0;
1314 }
1315
1316 if (CBS_len(&cipher_suites) % 2 != 0) {
1317 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1318 SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
1319 return NULL;
1320 }
1321
1322 sk = sk_SSL_CIPHER_new_null();
1323 if (sk == NULL) {
1324 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1325 goto err;
1326 }
1327
1328 if (!CBS_stow(&cipher_suites, &s->cert->ciphers_raw,
1329 &s->cert->ciphers_rawlen)) {
1330 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1331 goto err;
1332 }
1333
1334 while (CBS_len(&cipher_suites) > 0) {
1335 uint16_t cipher_suite;
1336
1337 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
1338 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_INTERNAL_ERROR);
1339 goto err;
1340 }
1341
1342 /* Check for SCSV. */
1343 if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff)) {
1344 /* SCSV is fatal if renegotiating. */
1345 if (s->renegotiate) {
1346 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1347 SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1348 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1349 goto err;
1350 }
1351 s->s3->send_connection_binding = 1;
1352 continue;
1353 }
1354
1355 /* Check for FALLBACK_SCSV. */
1356 if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff)) {
1357 uint16_t max_version = ssl3_get_max_server_version(s);
1358 if (SSL_IS_DTLS(s) ? (uint16_t)s->version > max_version
1359 : (uint16_t)s->version < max_version) {
1360 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1361 SSL_R_INAPPROPRIATE_FALLBACK);
1362 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
1363 goto err;
1364 }
1365 continue;
1366 }
1367
1368 c = ssl3_get_cipher_by_value(cipher_suite);
1369 if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
1370 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1371 goto err;
1372 }
1373 }
1374
1375 return sk;
David Benjamin9f2c0d72014-10-21 22:00:19 -04001376
Adam Langley95c29f32014-06-20 12:00:00 -07001377err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001378 sk_SSL_CIPHER_free(sk);
Adam Langleyfcf25832014-12-18 17:42:32 -08001379 return NULL;
1380}
Adam Langley95c29f32014-06-20 12:00:00 -07001381
1382
Adam Langleyfcf25832014-12-18 17:42:32 -08001383/* return a servername extension value if provided in Client Hello, or NULL. So
1384 * far, only host_name types are defined (RFC 3546). */
1385const char *SSL_get_servername(const SSL *s, const int type) {
1386 if (type != TLSEXT_NAMETYPE_host_name) {
1387 return NULL;
1388 }
Adam Langley95c29f32014-06-20 12:00:00 -07001389
Adam Langleyfcf25832014-12-18 17:42:32 -08001390 return s->session && !s->tlsext_hostname ? s->session->tlsext_hostname
1391 : s->tlsext_hostname;
1392}
Adam Langley95c29f32014-06-20 12:00:00 -07001393
Adam Langleyfcf25832014-12-18 17:42:32 -08001394int SSL_get_servername_type(const SSL *s) {
1395 if (s->session &&
1396 (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname)) {
1397 return TLSEXT_NAMETYPE_host_name;
1398 }
Adam Langley95c29f32014-06-20 12:00:00 -07001399
Adam Langleyfcf25832014-12-18 17:42:32 -08001400 return -1;
1401}
Adam Langley95c29f32014-06-20 12:00:00 -07001402
Adam Langleyfcf25832014-12-18 17:42:32 -08001403void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
1404 ctx->signed_cert_timestamps_enabled = 1;
1405}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001406
Adam Langleyfcf25832014-12-18 17:42:32 -08001407int SSL_enable_signed_cert_timestamps(SSL *ssl) {
1408 ssl->signed_cert_timestamps_enabled = 1;
1409 return 1;
1410}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001411
Adam Langleyfcf25832014-12-18 17:42:32 -08001412void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
1413 ctx->ocsp_stapling_enabled = 1;
1414}
David Benjamin6c7aed02014-08-27 16:42:38 -04001415
Adam Langleyfcf25832014-12-18 17:42:32 -08001416int SSL_enable_ocsp_stapling(SSL *ssl) {
1417 ssl->ocsp_stapling_enabled = 1;
1418 return 1;
1419}
David Benjamin6c7aed02014-08-27 16:42:38 -04001420
Adam Langleyfcf25832014-12-18 17:42:32 -08001421void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
1422 size_t *out_len) {
1423 SSL_SESSION *session = ssl->session;
Adam Langley3cb50e02014-08-26 14:00:31 -07001424
Adam Langleyfcf25832014-12-18 17:42:32 -08001425 *out_len = 0;
1426 *out = NULL;
1427 if (ssl->server || !session || !session->tlsext_signed_cert_timestamp_list) {
1428 return;
1429 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001430
Adam Langleyfcf25832014-12-18 17:42:32 -08001431 *out = session->tlsext_signed_cert_timestamp_list;
1432 *out_len = session->tlsext_signed_cert_timestamp_list_length;
1433}
David Benjamin6c7aed02014-08-27 16:42:38 -04001434
Adam Langleyfcf25832014-12-18 17:42:32 -08001435void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
1436 size_t *out_len) {
1437 SSL_SESSION *session = ssl->session;
1438
1439 *out_len = 0;
1440 *out = NULL;
1441 if (ssl->server || !session || !session->ocsp_response) {
1442 return;
1443 }
1444 *out = session->ocsp_response;
1445 *out_len = session->ocsp_response_length;
1446}
David Benjamin6c7aed02014-08-27 16:42:38 -04001447
Adam Langley95c29f32014-06-20 12:00:00 -07001448/* SSL_select_next_proto implements the standard protocol selection. It is
1449 * expected that this function is called from the callback set by
1450 * SSL_CTX_set_next_proto_select_cb.
1451 *
1452 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1453 * strings. The length byte itself is not included in the length. A byte
1454 * string of length 0 is invalid. No byte string may be truncated.
1455 *
1456 * The current, but experimental algorithm for selecting the protocol is:
1457 *
1458 * 1) If the server doesn't support NPN then this is indicated to the
1459 * callback. In this case, the client application has to abort the connection
1460 * or have a default application level protocol.
1461 *
1462 * 2) If the server supports NPN, but advertises an empty list then the
1463 * client selects the first protcol in its list, but indicates via the
1464 * API that this fallback case was enacted.
1465 *
1466 * 3) Otherwise, the client finds the first protocol in the server's list
1467 * that it supports and selects this protocol. This is because it's
1468 * assumed that the server has better information about which protocol
1469 * a client should use.
1470 *
1471 * 4) If the client doesn't support any of the server's advertised
1472 * protocols, then this is treated the same as case 2.
1473 *
1474 * It returns either
1475 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1476 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1477 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001478int SSL_select_next_proto(uint8_t **out, uint8_t *outlen, const uint8_t *server,
1479 unsigned int server_len, const uint8_t *client,
1480 unsigned int client_len) {
1481 unsigned int i, j;
1482 const uint8_t *result;
1483 int status = OPENSSL_NPN_UNSUPPORTED;
Adam Langley95c29f32014-06-20 12:00:00 -07001484
Adam Langleyfcf25832014-12-18 17:42:32 -08001485 /* For each protocol in server preference order, see if we support it. */
1486 for (i = 0; i < server_len;) {
1487 for (j = 0; j < client_len;) {
1488 if (server[i] == client[j] &&
1489 memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
1490 /* We found a match */
1491 result = &server[i];
1492 status = OPENSSL_NPN_NEGOTIATED;
1493 goto found;
1494 }
1495 j += client[j];
1496 j++;
1497 }
1498 i += server[i];
1499 i++;
1500 }
Adam Langley95c29f32014-06-20 12:00:00 -07001501
Adam Langleyfcf25832014-12-18 17:42:32 -08001502 /* There's no overlap between our protocols and the server's list. */
1503 result = client;
1504 status = OPENSSL_NPN_NO_OVERLAP;
Adam Langley95c29f32014-06-20 12:00:00 -07001505
Adam Langleyfcf25832014-12-18 17:42:32 -08001506found:
1507 *out = (uint8_t *)result + 1;
1508 *outlen = result[0];
1509 return status;
1510}
Adam Langley95c29f32014-06-20 12:00:00 -07001511
Adam Langley95c29f32014-06-20 12:00:00 -07001512/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
1513 * requested protocol for this connection and returns 0. If the client didn't
1514 * request any protocol, then *data is set to NULL.
1515 *
1516 * Note that the client can request any protocol it chooses. The value returned
1517 * from this function need not be a member of the list of supported protocols
Adam Langleyfcf25832014-12-18 17:42:32 -08001518 * provided by the callback. */
1519void SSL_get0_next_proto_negotiated(const SSL *s, const uint8_t **data,
1520 unsigned *len) {
1521 *data = s->next_proto_negotiated;
1522 if (!*data) {
1523 *len = 0;
1524 } else {
1525 *len = s->next_proto_negotiated_len;
1526 }
Adam Langley95c29f32014-06-20 12:00:00 -07001527}
1528
1529/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
1530 * TLS server needs a list of supported protocols for Next Protocol
1531 * Negotiation. The returned list must be in wire format. The list is returned
1532 * by setting |out| to point to it and |outlen| to its length. This memory will
1533 * not be modified, but one should assume that the SSL* keeps a reference to
1534 * it.
1535 *
Adam Langleyfcf25832014-12-18 17:42:32 -08001536 * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise.
1537 * Otherwise, no such extension will be included in the ServerHello. */
1538void SSL_CTX_set_next_protos_advertised_cb(
1539 SSL_CTX *ctx,
1540 int (*cb)(SSL *ssl, const uint8_t **out, unsigned int *outlen, void *arg),
1541 void *arg) {
1542 ctx->next_protos_advertised_cb = cb;
1543 ctx->next_protos_advertised_cb_arg = arg;
1544}
Adam Langley95c29f32014-06-20 12:00:00 -07001545
1546/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1547 * client needs to select a protocol from the server's provided list. |out|
1548 * must be set to point to the selected protocol (which may be within |in|).
1549 * The length of the protocol name must be written into |outlen|. The server's
1550 * advertised protocols are provided in |in| and |inlen|. The callback can
1551 * assume that |in| is syntactically valid.
1552 *
1553 * The client must select a protocol. It is fatal to the connection if this
1554 * callback returns a value other than SSL_TLSEXT_ERR_OK.
1555 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001556void SSL_CTX_set_next_proto_select_cb(
1557 SSL_CTX *ctx, int (*cb)(SSL *s, uint8_t **out, uint8_t *outlen,
1558 const uint8_t *in, unsigned int inlen, void *arg),
1559 void *arg) {
1560 ctx->next_proto_select_cb = cb;
1561 ctx->next_proto_select_cb_arg = arg;
1562}
Adam Langley95c29f32014-06-20 12:00:00 -07001563
Adam Langleyfcf25832014-12-18 17:42:32 -08001564int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
1565 unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001566 OPENSSL_free(ctx->alpn_client_proto_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001567 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1568 if (!ctx->alpn_client_proto_list) {
1569 return 1;
1570 }
1571 ctx->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001572
Adam Langleyfcf25832014-12-18 17:42:32 -08001573 return 0;
1574}
Adam Langley95c29f32014-06-20 12:00:00 -07001575
Adam Langleyfcf25832014-12-18 17:42:32 -08001576int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001577 OPENSSL_free(ssl->alpn_client_proto_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001578 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1579 if (!ssl->alpn_client_proto_list) {
1580 return 1;
1581 }
1582 ssl->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001583
Adam Langleyfcf25832014-12-18 17:42:32 -08001584 return 0;
1585}
Adam Langley95c29f32014-06-20 12:00:00 -07001586
1587/* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
1588 * during ClientHello processing in order to select an ALPN protocol from the
1589 * client's list of offered protocols. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001590void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
1591 int (*cb)(SSL *ssl, const uint8_t **out,
1592 uint8_t *outlen, const uint8_t *in,
1593 unsigned int inlen, void *arg),
1594 void *arg) {
1595 ctx->alpn_select_cb = cb;
1596 ctx->alpn_select_cb_arg = arg;
1597}
Adam Langley95c29f32014-06-20 12:00:00 -07001598
1599/* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
1600 * On return it sets |*data| to point to |*len| bytes of protocol name (not
1601 * including the leading length-prefix byte). If the server didn't respond with
1602 * a negotiated protocol then |*len| will be zero. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001603void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **data,
1604 unsigned *len) {
1605 *data = NULL;
1606 if (ssl->s3) {
1607 *data = ssl->s3->alpn_selected;
1608 }
1609 if (*data == NULL) {
1610 *len = 0;
1611 } else {
1612 *len = ssl->s3->alpn_selected_len;
1613 }
1614}
Adam Langley95c29f32014-06-20 12:00:00 -07001615
David Benjamincfd248b2015-04-03 11:02:24 -04001616int SSL_export_keying_material(SSL *s, uint8_t *out, size_t out_len,
1617 const char *label, size_t label_len,
1618 const uint8_t *context, size_t context_len,
1619 int use_context) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001620 if (s->version < TLS1_VERSION) {
David Benjaminc565ebb2015-04-03 04:06:36 -04001621 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001622 }
Adam Langley95c29f32014-06-20 12:00:00 -07001623
David Benjamincfd248b2015-04-03 11:02:24 -04001624 return s->enc_method->export_keying_material(
1625 s, out, out_len, label, label_len, context, context_len, use_context);
Adam Langleyfcf25832014-12-18 17:42:32 -08001626}
Adam Langley95c29f32014-06-20 12:00:00 -07001627
Adam Langleyfcf25832014-12-18 17:42:32 -08001628static uint32_t ssl_session_hash(const SSL_SESSION *a) {
1629 uint32_t hash =
1630 ((uint32_t)a->session_id[0]) ||
1631 ((uint32_t)a->session_id[1] << 8) ||
1632 ((uint32_t)a->session_id[2] << 16) ||
1633 ((uint32_t)a->session_id[3] << 24);
Adam Langley95c29f32014-06-20 12:00:00 -07001634
Adam Langleyfcf25832014-12-18 17:42:32 -08001635 return hash;
1636}
Adam Langley95c29f32014-06-20 12:00:00 -07001637
1638/* NB: If this function (or indeed the hash function which uses a sort of
1639 * coarser function than this one) is changed, ensure
1640 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1641 * able to construct an SSL_SESSION that will collide with any existing session
1642 * with a matching session ID. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001643static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
1644 if (a->ssl_version != b->ssl_version) {
1645 return 1;
1646 }
Adam Langley95c29f32014-06-20 12:00:00 -07001647
Adam Langleyfcf25832014-12-18 17:42:32 -08001648 if (a->session_id_length != b->session_id_length) {
1649 return 1;
1650 }
Adam Langley95c29f32014-06-20 12:00:00 -07001651
Adam Langleyfcf25832014-12-18 17:42:32 -08001652 return memcmp(a->session_id, b->session_id, a->session_id_length);
1653}
Adam Langley95c29f32014-06-20 12:00:00 -07001654
Adam Langleyfcf25832014-12-18 17:42:32 -08001655SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth) {
1656 SSL_CTX *ret = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001657
Adam Langleyfcf25832014-12-18 17:42:32 -08001658 if (meth == NULL) {
1659 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_NULL_SSL_METHOD_PASSED);
1660 return NULL;
1661 }
Adam Langley95c29f32014-06-20 12:00:00 -07001662
Adam Langleyfcf25832014-12-18 17:42:32 -08001663 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
1664 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
1665 goto err;
1666 }
Adam Langley95c29f32014-06-20 12:00:00 -07001667
Adam Langleyfcf25832014-12-18 17:42:32 -08001668 ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1669 if (ret == NULL) {
1670 goto err;
1671 }
Adam Langley95c29f32014-06-20 12:00:00 -07001672
Adam Langleyfcf25832014-12-18 17:42:32 -08001673 memset(ret, 0, sizeof(SSL_CTX));
Adam Langley95c29f32014-06-20 12:00:00 -07001674
Adam Langleyfcf25832014-12-18 17:42:32 -08001675 ret->method = meth->method;
Adam Langley95c29f32014-06-20 12:00:00 -07001676
Adam Langleyfcf25832014-12-18 17:42:32 -08001677 ret->cert_store = NULL;
1678 ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
1679 ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1680 ret->session_cache_head = NULL;
1681 ret->session_cache_tail = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001682
Adam Langleyfcf25832014-12-18 17:42:32 -08001683 /* We take the system default */
1684 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001685
Adam Langleyfcf25832014-12-18 17:42:32 -08001686 ret->new_session_cb = 0;
1687 ret->remove_session_cb = 0;
1688 ret->get_session_cb = 0;
1689 ret->generate_session_id = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001690
Adam Langleyfcf25832014-12-18 17:42:32 -08001691 ret->references = 1;
1692 ret->quiet_shutdown = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001693
Adam Langleyfcf25832014-12-18 17:42:32 -08001694 ret->info_callback = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001695
Adam Langleyfcf25832014-12-18 17:42:32 -08001696 ret->app_verify_callback = 0;
1697 ret->app_verify_arg = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001698
Adam Langleyfcf25832014-12-18 17:42:32 -08001699 ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
1700 ret->read_ahead = 0;
1701 ret->msg_callback = 0;
1702 ret->msg_callback_arg = NULL;
1703 ret->verify_mode = SSL_VERIFY_NONE;
1704 ret->sid_ctx_length = 0;
1705 ret->default_verify_callback = NULL;
1706 ret->cert = ssl_cert_new();
1707 if (ret->cert == NULL) {
1708 goto err;
1709 }
Adam Langley95c29f32014-06-20 12:00:00 -07001710
Adam Langleyfcf25832014-12-18 17:42:32 -08001711 ret->default_passwd_callback = 0;
1712 ret->default_passwd_callback_userdata = NULL;
1713 ret->client_cert_cb = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001714
Adam Langleyfcf25832014-12-18 17:42:32 -08001715 ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
1716 if (ret->sessions == NULL) {
1717 goto err;
1718 }
1719 ret->cert_store = X509_STORE_new();
1720 if (ret->cert_store == NULL) {
1721 goto err;
1722 }
Adam Langley95c29f32014-06-20 12:00:00 -07001723
Adam Langleyfcf25832014-12-18 17:42:32 -08001724 ssl_create_cipher_list(ret->method, &ret->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001725 &ret->cipher_list_by_id, SSL_DEFAULT_CIPHER_LIST);
Adam Langleyfcf25832014-12-18 17:42:32 -08001726 if (ret->cipher_list == NULL ||
1727 sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0) {
1728 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_LIBRARY_HAS_NO_CIPHERS);
1729 goto err2;
1730 }
Adam Langley95c29f32014-06-20 12:00:00 -07001731
Adam Langleyfcf25832014-12-18 17:42:32 -08001732 ret->param = X509_VERIFY_PARAM_new();
1733 if (!ret->param) {
1734 goto err;
1735 }
Adam Langley95c29f32014-06-20 12:00:00 -07001736
Adam Langleyfcf25832014-12-18 17:42:32 -08001737 ret->client_CA = sk_X509_NAME_new_null();
1738 if (ret->client_CA == NULL) {
1739 goto err;
1740 }
Adam Langley95c29f32014-06-20 12:00:00 -07001741
David Benjamin9f33fc62015-04-15 17:29:53 -04001742 CRYPTO_new_ex_data(&g_ex_data_class_ssl_ctx, ret, &ret->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001743
Adam Langleyfcf25832014-12-18 17:42:32 -08001744 ret->extra_certs = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001745
Adam Langleyfcf25832014-12-18 17:42:32 -08001746 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
David Benjamin422d3a42014-08-20 11:09:03 -04001747
Adam Langleyfcf25832014-12-18 17:42:32 -08001748 ret->tlsext_servername_callback = 0;
1749 ret->tlsext_servername_arg = NULL;
1750 /* Setup RFC4507 ticket keys */
1751 if (!RAND_bytes(ret->tlsext_tick_key_name, 16) ||
1752 !RAND_bytes(ret->tlsext_tick_hmac_key, 16) ||
1753 !RAND_bytes(ret->tlsext_tick_aes_key, 16)) {
1754 ret->options |= SSL_OP_NO_TICKET;
1755 }
Adam Langley95c29f32014-06-20 12:00:00 -07001756
Adam Langleyfcf25832014-12-18 17:42:32 -08001757 ret->next_protos_advertised_cb = 0;
1758 ret->next_proto_select_cb = 0;
1759 ret->psk_identity_hint = NULL;
1760 ret->psk_client_callback = NULL;
1761 ret->psk_server_callback = NULL;
1762
1763 /* Default is to connect to non-RI servers. When RI is more widely deployed
1764 * might change this. */
1765 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
1766
1767 /* Lock the SSL_CTX to the specified version, for compatibility with legacy
1768 * uses of SSL_METHOD. */
1769 if (meth->version != 0) {
1770 SSL_CTX_set_max_version(ret, meth->version);
1771 SSL_CTX_set_min_version(ret, meth->version);
1772 }
1773
1774 return ret;
1775
Adam Langley95c29f32014-06-20 12:00:00 -07001776err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001777 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -07001778err2:
David Benjamin2755a3e2015-04-22 16:17:58 -04001779 SSL_CTX_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -08001780 return NULL;
1781}
Adam Langley95c29f32014-06-20 12:00:00 -07001782
David Benjamin4fcc2e22015-04-22 12:58:16 -04001783void SSL_CTX_free(SSL_CTX *ctx) {
1784 if (ctx == NULL ||
1785 CRYPTO_add(&ctx->references, -1, CRYPTO_LOCK_SSL_CTX) > 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001786 return;
1787 }
Adam Langley95c29f32014-06-20 12:00:00 -07001788
David Benjamin2755a3e2015-04-22 16:17:58 -04001789 X509_VERIFY_PARAM_free(ctx->param);
Adam Langley95c29f32014-06-20 12:00:00 -07001790
Adam Langleyfcf25832014-12-18 17:42:32 -08001791 /* Free internal session cache. However: the remove_cb() may reference the
1792 * ex_data of SSL_CTX, thus the ex_data store can only be removed after the
1793 * sessions were flushed. As the ex_data handling routines might also touch
1794 * the session cache, the most secure solution seems to be: empty (flush) the
1795 * cache, then free ex_data, then finally free the cache. (See ticket
1796 * [openssl.org #212].) */
David Benjamin2755a3e2015-04-22 16:17:58 -04001797 SSL_CTX_flush_sessions(ctx, 0);
Adam Langley95c29f32014-06-20 12:00:00 -07001798
David Benjamin4fcc2e22015-04-22 12:58:16 -04001799 CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, ctx, &ctx->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001800
David Benjamin2755a3e2015-04-22 16:17:58 -04001801 lh_SSL_SESSION_free(ctx->sessions);
1802 X509_STORE_free(ctx->cert_store);
1803 ssl_cipher_preference_list_free(ctx->cipher_list);
1804 sk_SSL_CIPHER_free(ctx->cipher_list_by_id);
1805 ssl_cipher_preference_list_free(ctx->cipher_list_tls11);
1806 ssl_cert_free(ctx->cert);
1807 sk_X509_NAME_pop_free(ctx->client_CA, X509_NAME_free);
1808 sk_X509_pop_free(ctx->extra_certs, X509_free);
1809 sk_SRTP_PROTECTION_PROFILE_free(ctx->srtp_profiles);
1810 OPENSSL_free(ctx->psk_identity_hint);
1811 OPENSSL_free(ctx->tlsext_ecpointformatlist);
1812 OPENSSL_free(ctx->tlsext_ellipticcurvelist);
1813 OPENSSL_free(ctx->alpn_client_proto_list);
1814 EVP_PKEY_free(ctx->tlsext_channel_id_private);
1815 BIO_free(ctx->keylog_bio);
Adam Langley95c29f32014-06-20 12:00:00 -07001816
David Benjamin4fcc2e22015-04-22 12:58:16 -04001817 OPENSSL_free(ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08001818}
Adam Langley95c29f32014-06-20 12:00:00 -07001819
Adam Langleyfcf25832014-12-18 17:42:32 -08001820void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) {
1821 ctx->default_passwd_callback = cb;
1822}
Adam Langley95c29f32014-06-20 12:00:00 -07001823
Adam Langleyfcf25832014-12-18 17:42:32 -08001824void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u) {
1825 ctx->default_passwd_callback_userdata = u;
1826}
Adam Langley95c29f32014-06-20 12:00:00 -07001827
Adam Langleyfcf25832014-12-18 17:42:32 -08001828void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
1829 int (*cb)(X509_STORE_CTX *, void *),
1830 void *arg) {
1831 ctx->app_verify_callback = cb;
1832 ctx->app_verify_arg = arg;
1833}
Adam Langley95c29f32014-06-20 12:00:00 -07001834
Adam Langleyfcf25832014-12-18 17:42:32 -08001835void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
1836 int (*cb)(int, X509_STORE_CTX *)) {
1837 ctx->verify_mode = mode;
1838 ctx->default_verify_callback = cb;
1839}
Adam Langley95c29f32014-06-20 12:00:00 -07001840
Adam Langleyfcf25832014-12-18 17:42:32 -08001841void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
1842 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
1843}
Adam Langley1258b6a2014-06-20 12:00:00 -07001844
Adam Langleyfcf25832014-12-18 17:42:32 -08001845void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb)(SSL *ssl, void *arg),
1846 void *arg) {
1847 ssl_cert_set_cert_cb(c->cert, cb, arg);
1848}
David Benjamin859ec3c2014-09-02 16:29:36 -04001849
Adam Langleyfcf25832014-12-18 17:42:32 -08001850void SSL_set_cert_cb(SSL *s, int (*cb)(SSL *ssl, void *arg), void *arg) {
1851 ssl_cert_set_cert_cb(s->cert, cb, arg);
1852}
Adam Langley95c29f32014-06-20 12:00:00 -07001853
Adam Langleyfcf25832014-12-18 17:42:32 -08001854static int ssl_has_key(SSL *s, size_t idx) {
1855 CERT_PKEY *cpk = &s->cert->pkeys[idx];
1856 return cpk->x509 && cpk->privatekey;
1857}
David Benjamin033e5f42014-11-13 18:47:41 -05001858
David Benjamin107db582015-04-08 00:41:59 -04001859void ssl_get_compatible_server_ciphers(SSL *s, uint32_t *out_mask_k,
1860 uint32_t *out_mask_a) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001861 CERT *c = s->cert;
1862 int rsa_enc, rsa_sign, dh_tmp;
David Benjamin107db582015-04-08 00:41:59 -04001863 uint32_t mask_k, mask_a;
Adam Langleyfcf25832014-12-18 17:42:32 -08001864 int have_ecc_cert, ecdsa_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001865 X509 *x;
Adam Langley95c29f32014-06-20 12:00:00 -07001866
Adam Langleyfcf25832014-12-18 17:42:32 -08001867 if (c == NULL) {
1868 /* TODO(davidben): Is this codepath possible? */
1869 *out_mask_k = 0;
1870 *out_mask_a = 0;
1871 return;
1872 }
Adam Langley95c29f32014-06-20 12:00:00 -07001873
Adam Langleyfcf25832014-12-18 17:42:32 -08001874 dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001875
Adam Langleyfcf25832014-12-18 17:42:32 -08001876 rsa_enc = ssl_has_key(s, SSL_PKEY_RSA_ENC);
1877 rsa_sign = ssl_has_key(s, SSL_PKEY_RSA_SIGN);
1878 have_ecc_cert = ssl_has_key(s, SSL_PKEY_ECC);
1879 mask_k = 0;
1880 mask_a = 0;
David Benjaminf31e6812014-11-13 18:05:55 -05001881
Adam Langleyfcf25832014-12-18 17:42:32 -08001882 if (rsa_enc) {
1883 mask_k |= SSL_kRSA;
1884 }
1885 if (dh_tmp) {
David Benjamin7061e282015-03-19 11:10:48 -04001886 mask_k |= SSL_kDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001887 }
1888 if (rsa_enc || rsa_sign) {
1889 mask_a |= SSL_aRSA;
1890 }
Adam Langley95c29f32014-06-20 12:00:00 -07001891
Adam Langleyfcf25832014-12-18 17:42:32 -08001892 /* An ECC certificate may be usable for ECDSA cipher suites depending on the
1893 * key usage extension and on the client's curve preferences. */
1894 if (have_ecc_cert) {
1895 x = c->pkeys[SSL_PKEY_ECC].x509;
1896 /* This call populates extension flags (ex_flags). */
1897 X509_check_purpose(x, -1, 0);
1898 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE)
1899 ? (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)
1900 : 1;
1901 if (!tls1_check_ec_cert(s, x)) {
1902 ecdsa_ok = 0;
1903 }
1904 if (ecdsa_ok) {
1905 mask_a |= SSL_aECDSA;
1906 }
1907 }
Adam Langley95c29f32014-06-20 12:00:00 -07001908
Adam Langleyfcf25832014-12-18 17:42:32 -08001909 /* If we are considering an ECC cipher suite that uses an ephemeral EC
1910 * key, check it. */
David Benjamindd978782015-04-24 15:20:13 -04001911 if (tls1_check_ec_tmp_key(s)) {
David Benjamin7061e282015-03-19 11:10:48 -04001912 mask_k |= SSL_kECDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001913 }
Adam Langley95c29f32014-06-20 12:00:00 -07001914
Adam Langleyfcf25832014-12-18 17:42:32 -08001915 /* PSK requires a server callback. */
1916 if (s->psk_server_callback != NULL) {
1917 mask_k |= SSL_kPSK;
1918 mask_a |= SSL_aPSK;
1919 }
Adam Langley95c29f32014-06-20 12:00:00 -07001920
Adam Langleyfcf25832014-12-18 17:42:32 -08001921 *out_mask_k = mask_k;
1922 *out_mask_a = mask_a;
1923}
Adam Langley95c29f32014-06-20 12:00:00 -07001924
1925/* This handy macro borrowed from crypto/x509v3/v3_purp.c */
1926#define ku_reject(x, usage) \
Adam Langleyfcf25832014-12-18 17:42:32 -08001927 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
Adam Langley95c29f32014-06-20 12:00:00 -07001928
Adam Langleyfcf25832014-12-18 17:42:32 -08001929int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001930 const SSL_CIPHER *cs = s->s3->tmp.new_cipher;
David Benjamin3fa27772015-04-17 22:32:19 -04001931 uint32_t alg_a = cs->algorithm_auth;
1932 int signature_nid = 0, md_nid = 0, pk_nid = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001933
Adam Langleyfcf25832014-12-18 17:42:32 -08001934 /* This call populates the ex_flags field correctly */
1935 X509_check_purpose(x, -1, 0);
1936 if (x->sig_alg && x->sig_alg->algorithm) {
1937 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
1938 OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
1939 }
1940 if (alg_a & SSL_aECDSA) {
1941 /* key usage, if present, must allow signing */
1942 if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE)) {
1943 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg,
1944 SSL_R_ECC_CERT_NOT_FOR_SIGNING);
1945 return 0;
1946 }
1947 }
Adam Langley95c29f32014-06-20 12:00:00 -07001948
Adam Langleyfcf25832014-12-18 17:42:32 -08001949 return 1; /* all checks are ok */
1950}
Adam Langley95c29f32014-06-20 12:00:00 -07001951
Adam Langleyfcf25832014-12-18 17:42:32 -08001952static int ssl_get_server_cert_index(const SSL *s) {
1953 int idx;
1954 idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1955 if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509) {
1956 idx = SSL_PKEY_RSA_SIGN;
1957 }
1958 if (idx == -1) {
1959 OPENSSL_PUT_ERROR(SSL, ssl_get_server_cert_index, ERR_R_INTERNAL_ERROR);
1960 }
1961 return idx;
1962}
Adam Langley95c29f32014-06-20 12:00:00 -07001963
Adam Langleyfcf25832014-12-18 17:42:32 -08001964CERT_PKEY *ssl_get_server_send_pkey(const SSL *s) {
1965 int i = ssl_get_server_cert_index(s);
Adam Langley95c29f32014-06-20 12:00:00 -07001966
Adam Langleyfcf25832014-12-18 17:42:32 -08001967 /* This may or may not be an error. */
1968 if (i < 0) {
1969 return NULL;
1970 }
Adam Langley95c29f32014-06-20 12:00:00 -07001971
Adam Langleyfcf25832014-12-18 17:42:32 -08001972 /* May be NULL. */
1973 return &s->cert->pkeys[i];
1974}
Adam Langley95c29f32014-06-20 12:00:00 -07001975
Adam Langleyfcf25832014-12-18 17:42:32 -08001976EVP_PKEY *ssl_get_sign_pkey(SSL *s, const SSL_CIPHER *cipher) {
David Benjamin3fa27772015-04-17 22:32:19 -04001977 uint32_t alg_a = cipher->algorithm_auth;
1978 CERT *c = s->cert;
Adam Langleyfcf25832014-12-18 17:42:32 -08001979 int idx = -1;
Adam Langley95c29f32014-06-20 12:00:00 -07001980
Adam Langleyfcf25832014-12-18 17:42:32 -08001981 if (alg_a & SSL_aRSA) {
1982 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL) {
1983 idx = SSL_PKEY_RSA_SIGN;
1984 } else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL) {
1985 idx = SSL_PKEY_RSA_ENC;
1986 }
1987 } else if ((alg_a & SSL_aECDSA) &&
1988 (c->pkeys[SSL_PKEY_ECC].privatekey != NULL)) {
1989 idx = SSL_PKEY_ECC;
1990 }
Adam Langley95c29f32014-06-20 12:00:00 -07001991
Adam Langleyfcf25832014-12-18 17:42:32 -08001992 if (idx == -1) {
1993 OPENSSL_PUT_ERROR(SSL, ssl_get_sign_pkey, ERR_R_INTERNAL_ERROR);
1994 return NULL;
1995 }
Adam Langley95c29f32014-06-20 12:00:00 -07001996
Adam Langleyfcf25832014-12-18 17:42:32 -08001997 return c->pkeys[idx].privatekey;
1998}
Adam Langley95c29f32014-06-20 12:00:00 -07001999
Adam Langleyfcf25832014-12-18 17:42:32 -08002000void ssl_update_cache(SSL *s, int mode) {
David Benjaminb6d0c6d2015-03-19 19:07:26 -04002001 /* Never cache sessions with empty session IDs. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002002 if (s->session->session_id_length == 0) {
2003 return;
2004 }
Adam Langley95c29f32014-06-20 12:00:00 -07002005
David Benjaminb6d0c6d2015-03-19 19:07:26 -04002006 SSL_CTX *ctx = s->initial_ctx;
2007 if ((ctx->session_cache_mode & mode) == mode && !s->hit &&
2008 ((ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) ||
2009 SSL_CTX_add_session(ctx, s->session)) &&
2010 ctx->new_session_cb != NULL) {
2011 /* Note: |new_session_cb| is called whether the internal session cache is
2012 * used or not. */
2013 if (!ctx->new_session_cb(s, SSL_SESSION_up_ref(s->session))) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002014 SSL_SESSION_free(s->session);
2015 }
2016 }
Adam Langley95c29f32014-06-20 12:00:00 -07002017
David Benjaminb6d0c6d2015-03-19 19:07:26 -04002018 if (!(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR) &&
2019 !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) &&
2020 (ctx->session_cache_mode & mode) == mode) {
2021 /* Automatically flush the internal session cache every 255 connections. */
2022 int flush_cache = 0;
2023 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2024 ctx->handshakes_since_cache_flush++;
2025 if (ctx->handshakes_since_cache_flush >= 255) {
2026 flush_cache = 1;
2027 ctx->handshakes_since_cache_flush = 0;
2028 }
2029 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
2030
2031 if (flush_cache) {
2032 SSL_CTX_flush_sessions(ctx, (unsigned long)time(NULL));
Adam Langleyfcf25832014-12-18 17:42:32 -08002033 }
2034 }
2035}
Adam Langley95c29f32014-06-20 12:00:00 -07002036
David Benjamin1a5c50f2015-03-11 16:22:37 -04002037int SSL_get_error(const SSL *s, int ret_code) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002038 int reason;
David Benjamin1a5c50f2015-03-11 16:22:37 -04002039 uint32_t err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002040 BIO *bio;
Adam Langley95c29f32014-06-20 12:00:00 -07002041
David Benjamin1a5c50f2015-03-11 16:22:37 -04002042 if (ret_code > 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002043 return SSL_ERROR_NONE;
2044 }
Adam Langley95c29f32014-06-20 12:00:00 -07002045
Adam Langleyfcf25832014-12-18 17:42:32 -08002046 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
2047 * where we do encode the error */
David Benjamin1a5c50f2015-03-11 16:22:37 -04002048 err = ERR_peek_error();
2049 if (err != 0) {
2050 if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002051 return SSL_ERROR_SYSCALL;
2052 }
2053 return SSL_ERROR_SSL;
2054 }
Adam Langley95c29f32014-06-20 12:00:00 -07002055
David Benjamin1a5c50f2015-03-11 16:22:37 -04002056 if (ret_code == 0) {
David Benjamin9a38e922015-01-22 16:06:11 -05002057 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2058 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) {
2059 /* The socket was cleanly shut down with a close_notify. */
2060 return SSL_ERROR_ZERO_RETURN;
2061 }
2062 /* An EOF was observed which violates the protocol, and the underlying
2063 * transport does not participate in the error queue. Bubble up to the
2064 * caller. */
2065 return SSL_ERROR_SYSCALL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002066 }
Adam Langleyb2ce0582014-06-20 12:00:00 -07002067
Adam Langleyfcf25832014-12-18 17:42:32 -08002068 if (SSL_want_session(s)) {
2069 return SSL_ERROR_PENDING_SESSION;
2070 }
Adam Langley95c29f32014-06-20 12:00:00 -07002071
Adam Langleyfcf25832014-12-18 17:42:32 -08002072 if (SSL_want_certificate(s)) {
2073 return SSL_ERROR_PENDING_CERTIFICATE;
2074 }
Adam Langley95c29f32014-06-20 12:00:00 -07002075
Adam Langleyfcf25832014-12-18 17:42:32 -08002076 if (SSL_want_read(s)) {
2077 bio = SSL_get_rbio(s);
2078 if (BIO_should_read(bio)) {
2079 return SSL_ERROR_WANT_READ;
2080 }
Adam Langley95c29f32014-06-20 12:00:00 -07002081
Adam Langleyfcf25832014-12-18 17:42:32 -08002082 if (BIO_should_write(bio)) {
2083 /* This one doesn't make too much sense ... We never try to write to the
2084 * rbio, and an application program where rbio and wbio are separate
2085 * couldn't even know what it should wait for. However if we ever set
2086 * s->rwstate incorrectly (so that we have SSL_want_read(s) instead of
2087 * SSL_want_write(s)) and rbio and wbio *are* the same, this test works
2088 * around that bug; so it might be safer to keep it. */
2089 return SSL_ERROR_WANT_WRITE;
2090 }
Adam Langley95c29f32014-06-20 12:00:00 -07002091
Adam Langleyfcf25832014-12-18 17:42:32 -08002092 if (BIO_should_io_special(bio)) {
2093 reason = BIO_get_retry_reason(bio);
2094 if (reason == BIO_RR_CONNECT) {
2095 return SSL_ERROR_WANT_CONNECT;
2096 }
Adam Langley95c29f32014-06-20 12:00:00 -07002097
Adam Langleyfcf25832014-12-18 17:42:32 -08002098 if (reason == BIO_RR_ACCEPT) {
2099 return SSL_ERROR_WANT_ACCEPT;
2100 }
Adam Langley95c29f32014-06-20 12:00:00 -07002101
Adam Langleyfcf25832014-12-18 17:42:32 -08002102 return SSL_ERROR_SYSCALL; /* unknown */
2103 }
2104 }
Adam Langley95c29f32014-06-20 12:00:00 -07002105
Adam Langleyfcf25832014-12-18 17:42:32 -08002106 if (SSL_want_write(s)) {
2107 bio = SSL_get_wbio(s);
2108 if (BIO_should_write(bio)) {
2109 return SSL_ERROR_WANT_WRITE;
2110 }
Adam Langley95c29f32014-06-20 12:00:00 -07002111
Adam Langleyfcf25832014-12-18 17:42:32 -08002112 if (BIO_should_read(bio)) {
2113 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2114 return SSL_ERROR_WANT_READ;
2115 }
Adam Langley95c29f32014-06-20 12:00:00 -07002116
Adam Langleyfcf25832014-12-18 17:42:32 -08002117 if (BIO_should_io_special(bio)) {
2118 reason = BIO_get_retry_reason(bio);
2119 if (reason == BIO_RR_CONNECT) {
2120 return SSL_ERROR_WANT_CONNECT;
2121 }
Adam Langley95c29f32014-06-20 12:00:00 -07002122
Adam Langleyfcf25832014-12-18 17:42:32 -08002123 if (reason == BIO_RR_ACCEPT) {
2124 return SSL_ERROR_WANT_ACCEPT;
2125 }
Adam Langley95c29f32014-06-20 12:00:00 -07002126
Adam Langleyfcf25832014-12-18 17:42:32 -08002127 return SSL_ERROR_SYSCALL;
2128 }
2129 }
Adam Langley95c29f32014-06-20 12:00:00 -07002130
Adam Langleyfcf25832014-12-18 17:42:32 -08002131 if (SSL_want_x509_lookup(s)) {
2132 return SSL_ERROR_WANT_X509_LOOKUP;
2133 }
Adam Langley95c29f32014-06-20 12:00:00 -07002134
Adam Langleyfcf25832014-12-18 17:42:32 -08002135 if (SSL_want_channel_id_lookup(s)) {
2136 return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
2137 }
Adam Langley0f4746e2014-08-13 12:26:32 -07002138
Adam Langleyfcf25832014-12-18 17:42:32 -08002139 return SSL_ERROR_SYSCALL;
2140}
Adam Langley0f4746e2014-08-13 12:26:32 -07002141
Adam Langleyfcf25832014-12-18 17:42:32 -08002142int SSL_do_handshake(SSL *s) {
2143 int ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002144
Adam Langleyfcf25832014-12-18 17:42:32 -08002145 if (s->handshake_func == NULL) {
2146 OPENSSL_PUT_ERROR(SSL, SSL_do_handshake, SSL_R_CONNECTION_TYPE_NOT_SET);
2147 return -1;
2148 }
Adam Langley95c29f32014-06-20 12:00:00 -07002149
Adam Langleyfcf25832014-12-18 17:42:32 -08002150 s->method->ssl_renegotiate_check(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002151
Adam Langleyfcf25832014-12-18 17:42:32 -08002152 if (SSL_in_init(s)) {
2153 ret = s->handshake_func(s);
2154 }
2155 return ret;
2156}
Adam Langley95c29f32014-06-20 12:00:00 -07002157
Adam Langleyfcf25832014-12-18 17:42:32 -08002158void SSL_set_accept_state(SSL *s) {
2159 s->server = 1;
2160 s->shutdown = 0;
2161 s->state = SSL_ST_ACCEPT | SSL_ST_BEFORE;
2162 s->handshake_func = s->method->ssl_accept;
2163 /* clear the current cipher */
2164 ssl_clear_cipher_ctx(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002165}
Adam Langley95c29f32014-06-20 12:00:00 -07002166
Adam Langleyfcf25832014-12-18 17:42:32 -08002167void SSL_set_connect_state(SSL *s) {
2168 s->server = 0;
2169 s->shutdown = 0;
2170 s->state = SSL_ST_CONNECT | SSL_ST_BEFORE;
2171 s->handshake_func = s->method->ssl_connect;
2172 /* clear the current cipher */
2173 ssl_clear_cipher_ctx(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002174}
Adam Langley95c29f32014-06-20 12:00:00 -07002175
Adam Langleyfcf25832014-12-18 17:42:32 -08002176int ssl_undefined_function(SSL *s) {
2177 OPENSSL_PUT_ERROR(SSL, ssl_undefined_function,
2178 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2179 return 0;
2180}
Adam Langley95c29f32014-06-20 12:00:00 -07002181
Adam Langleyfcf25832014-12-18 17:42:32 -08002182int ssl_undefined_void_function(void) {
2183 OPENSSL_PUT_ERROR(SSL, ssl_undefined_void_function,
2184 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2185 return 0;
2186}
Adam Langley95c29f32014-06-20 12:00:00 -07002187
Adam Langleyfcf25832014-12-18 17:42:32 -08002188int ssl_undefined_const_function(const SSL *s) {
2189 OPENSSL_PUT_ERROR(SSL, ssl_undefined_const_function,
2190 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2191 return 0;
2192}
Adam Langley95c29f32014-06-20 12:00:00 -07002193
Adam Langleyfcf25832014-12-18 17:42:32 -08002194static const char *ssl_get_version(int version) {
2195 switch (version) {
2196 case TLS1_2_VERSION:
2197 return "TLSv1.2";
Adam Langley95c29f32014-06-20 12:00:00 -07002198
Adam Langleyfcf25832014-12-18 17:42:32 -08002199 case TLS1_1_VERSION:
2200 return "TLSv1.1";
Adam Langley95c29f32014-06-20 12:00:00 -07002201
Adam Langleyfcf25832014-12-18 17:42:32 -08002202 case TLS1_VERSION:
2203 return "TLSv1";
Adam Langley95c29f32014-06-20 12:00:00 -07002204
Adam Langleyfcf25832014-12-18 17:42:32 -08002205 case SSL3_VERSION:
2206 return "SSLv3";
Adam Langley95c29f32014-06-20 12:00:00 -07002207
David Benjamin1c722b72015-04-20 13:53:10 -04002208 case DTLS1_VERSION:
2209 return "DTLSv1";
2210
2211 case DTLS1_2_VERSION:
2212 return "DTLSv1.2";
2213
Adam Langleyfcf25832014-12-18 17:42:32 -08002214 default:
2215 return "unknown";
2216 }
2217}
Adam Langley95c29f32014-06-20 12:00:00 -07002218
Adam Langleyfcf25832014-12-18 17:42:32 -08002219const char *SSL_get_version(const SSL *s) {
2220 return ssl_get_version(s->version);
2221}
Adam Langley95c29f32014-06-20 12:00:00 -07002222
Adam Langleyfcf25832014-12-18 17:42:32 -08002223const char *SSL_SESSION_get_version(const SSL_SESSION *sess) {
2224 return ssl_get_version(sess->ssl_version);
2225}
Adam Langley95c29f32014-06-20 12:00:00 -07002226
Adam Langleyfcf25832014-12-18 17:42:32 -08002227void ssl_clear_cipher_ctx(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002228 if (s->aead_read_ctx != NULL) {
2229 EVP_AEAD_CTX_cleanup(&s->aead_read_ctx->ctx);
2230 OPENSSL_free(s->aead_read_ctx);
2231 s->aead_read_ctx = NULL;
2232 }
Adam Langleya5dc5452014-06-20 12:00:00 -07002233
Adam Langleyfcf25832014-12-18 17:42:32 -08002234 if (s->aead_write_ctx != NULL) {
2235 EVP_AEAD_CTX_cleanup(&s->aead_write_ctx->ctx);
2236 OPENSSL_free(s->aead_write_ctx);
2237 s->aead_write_ctx = NULL;
2238 }
2239}
Adam Langley95c29f32014-06-20 12:00:00 -07002240
Adam Langleyfcf25832014-12-18 17:42:32 -08002241X509 *SSL_get_certificate(const SSL *s) {
2242 if (s->cert != NULL) {
2243 return s->cert->key->x509;
2244 }
2245
2246 return NULL;
2247}
2248
2249EVP_PKEY *SSL_get_privatekey(const SSL *s) {
2250 if (s->cert != NULL) {
2251 return s->cert->key->privatekey;
2252 }
2253
2254 return NULL;
2255}
2256
2257X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
2258 if (ctx->cert != NULL) {
2259 return ctx->cert->key->x509;
2260 }
2261
2262 return NULL;
2263}
2264
2265EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
2266 if (ctx->cert != NULL) {
2267 return ctx->cert->key->privatekey;
2268 }
2269
2270 return NULL;
2271}
2272
2273const SSL_CIPHER *SSL_get_current_cipher(const SSL *s) {
2274 if (s->session != NULL && s->session->cipher != NULL) {
2275 return s->session->cipher;
2276 }
2277
2278 return NULL;
2279}
2280
2281const void *SSL_get_current_compression(SSL *s) { return NULL; }
2282
2283const void *SSL_get_current_expansion(SSL *s) { return NULL; }
2284
2285int ssl_init_wbio_buffer(SSL *s, int push) {
2286 BIO *bbio;
2287
2288 if (s->bbio == NULL) {
2289 bbio = BIO_new(BIO_f_buffer());
2290 if (bbio == NULL) {
2291 return 0;
2292 }
2293 s->bbio = bbio;
2294 } else {
2295 bbio = s->bbio;
2296 if (s->bbio == s->wbio) {
2297 s->wbio = BIO_pop(s->wbio);
2298 }
2299 }
2300
2301 BIO_reset(bbio);
2302 if (!BIO_set_read_buffer_size(bbio, 1)) {
2303 OPENSSL_PUT_ERROR(SSL, ssl_init_wbio_buffer, ERR_R_BUF_LIB);
2304 return 0;
2305 }
2306
2307 if (push) {
2308 if (s->wbio != bbio) {
2309 s->wbio = BIO_push(bbio, s->wbio);
2310 }
2311 } else {
2312 if (s->wbio == bbio) {
2313 s->wbio = BIO_pop(bbio);
2314 }
2315 }
2316
2317 return 1;
2318}
2319
2320void ssl_free_wbio_buffer(SSL *s) {
2321 if (s->bbio == NULL) {
2322 return;
2323 }
2324
2325 if (s->bbio == s->wbio) {
2326 /* remove buffering */
2327 s->wbio = BIO_pop(s->wbio);
2328 }
2329
2330 BIO_free(s->bbio);
2331 s->bbio = NULL;
2332}
2333
2334void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
2335 ctx->quiet_shutdown = mode;
2336}
2337
2338int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
2339 return ctx->quiet_shutdown;
2340}
2341
2342void SSL_set_quiet_shutdown(SSL *s, int mode) { s->quiet_shutdown = mode; }
2343
2344int SSL_get_quiet_shutdown(const SSL *s) { return s->quiet_shutdown; }
2345
2346void SSL_set_shutdown(SSL *s, int mode) { s->shutdown = mode; }
2347
2348int SSL_get_shutdown(const SSL *s) { return s->shutdown; }
2349
2350int SSL_version(const SSL *s) { return s->version; }
2351
2352SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
2353
2354SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
2355 if (ssl->ctx == ctx) {
2356 return ssl->ctx;
2357 }
2358
2359 if (ctx == NULL) {
2360 ctx = ssl->initial_ctx;
2361 }
2362
David Benjamin2755a3e2015-04-22 16:17:58 -04002363 ssl_cert_free(ssl->cert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002364 ssl->cert = ssl_cert_dup(ctx->cert);
David Benjamin2755a3e2015-04-22 16:17:58 -04002365
Adam Langleyfcf25832014-12-18 17:42:32 -08002366 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
David Benjamin2755a3e2015-04-22 16:17:58 -04002367 SSL_CTX_free(ssl->ctx); /* decrement reference count */
Adam Langleyfcf25832014-12-18 17:42:32 -08002368 ssl->ctx = ctx;
2369
2370 ssl->sid_ctx_length = ctx->sid_ctx_length;
2371 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2372 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2373
2374 return ssl->ctx;
2375}
2376
2377int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
2378 return X509_STORE_set_default_paths(ctx->cert_store);
2379}
Adam Langley95c29f32014-06-20 12:00:00 -07002380
2381int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
Adam Langleyfcf25832014-12-18 17:42:32 -08002382 const char *CApath) {
2383 return X509_STORE_load_locations(ctx->cert_store, CAfile, CApath);
2384}
Adam Langley95c29f32014-06-20 12:00:00 -07002385
2386void SSL_set_info_callback(SSL *ssl,
Adam Langleyfcf25832014-12-18 17:42:32 -08002387 void (*cb)(const SSL *ssl, int type, int val)) {
2388 ssl->info_callback = cb;
2389}
Adam Langley95c29f32014-06-20 12:00:00 -07002390
Adam Langleyfcf25832014-12-18 17:42:32 -08002391void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/, int /*type*/,
2392 int /*val*/) {
2393 return ssl->info_callback;
2394}
Adam Langley95c29f32014-06-20 12:00:00 -07002395
Adam Langleyfcf25832014-12-18 17:42:32 -08002396int SSL_state(const SSL *ssl) { return ssl->state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002397
Adam Langleyfcf25832014-12-18 17:42:32 -08002398void SSL_set_state(SSL *ssl, int state) { ssl->state = state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002399
Adam Langleyfcf25832014-12-18 17:42:32 -08002400void SSL_set_verify_result(SSL *ssl, long arg) { ssl->verify_result = arg; }
Adam Langley95c29f32014-06-20 12:00:00 -07002401
Adam Langleyfcf25832014-12-18 17:42:32 -08002402long SSL_get_verify_result(const SSL *ssl) { return ssl->verify_result; }
Adam Langley95c29f32014-06-20 12:00:00 -07002403
Adam Langleyfcf25832014-12-18 17:42:32 -08002404int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2405 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002406 int index;
2407 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp,
2408 new_func, dup_func, free_func)) {
2409 return -1;
2410 }
2411 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002412}
Adam Langley95c29f32014-06-20 12:00:00 -07002413
Adam Langleyfcf25832014-12-18 17:42:32 -08002414int SSL_set_ex_data(SSL *s, int idx, void *arg) {
2415 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2416}
Adam Langley95c29f32014-06-20 12:00:00 -07002417
Adam Langleyfcf25832014-12-18 17:42:32 -08002418void *SSL_get_ex_data(const SSL *s, int idx) {
2419 return CRYPTO_get_ex_data(&s->ex_data, idx);
2420}
Adam Langley95c29f32014-06-20 12:00:00 -07002421
Adam Langleyfcf25832014-12-18 17:42:32 -08002422int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2423 CRYPTO_EX_dup *dup_func,
2424 CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002425 int index;
2426 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp,
2427 new_func, dup_func, free_func)) {
2428 return -1;
2429 }
2430 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002431}
Adam Langley95c29f32014-06-20 12:00:00 -07002432
Adam Langleyfcf25832014-12-18 17:42:32 -08002433int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg) {
2434 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2435}
Adam Langley95c29f32014-06-20 12:00:00 -07002436
Adam Langleyfcf25832014-12-18 17:42:32 -08002437void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx) {
2438 return CRYPTO_get_ex_data(&s->ex_data, idx);
2439}
Adam Langley95c29f32014-06-20 12:00:00 -07002440
Adam Langleyfcf25832014-12-18 17:42:32 -08002441int ssl_ok(SSL *s) { return 1; }
Adam Langley95c29f32014-06-20 12:00:00 -07002442
Adam Langleyfcf25832014-12-18 17:42:32 -08002443X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
2444 return ctx->cert_store;
2445}
Adam Langley95c29f32014-06-20 12:00:00 -07002446
Adam Langleyfcf25832014-12-18 17:42:32 -08002447void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
David Benjamin2755a3e2015-04-22 16:17:58 -04002448 X509_STORE_free(ctx->cert_store);
Adam Langleyfcf25832014-12-18 17:42:32 -08002449 ctx->cert_store = store;
2450}
Adam Langley95c29f32014-06-20 12:00:00 -07002451
Adam Langleyfcf25832014-12-18 17:42:32 -08002452int SSL_want(const SSL *s) { return s->rwstate; }
Adam Langley95c29f32014-06-20 12:00:00 -07002453
Adam Langleyfcf25832014-12-18 17:42:32 -08002454void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
2455 RSA *(*cb)(SSL *ssl, int is_export,
2456 int keylength)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002457}
Adam Langley95c29f32014-06-20 12:00:00 -07002458
Adam Langleyfcf25832014-12-18 17:42:32 -08002459void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
2460 int keylength)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002461}
2462
2463void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
David Benjamin59015c32015-04-26 13:13:55 -04002464 DH *(*callback)(SSL *ssl, int is_export,
2465 int keylength)) {
2466 ctx->cert->dh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002467}
2468
David Benjamin59015c32015-04-26 13:13:55 -04002469void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*callback)(SSL *ssl, int is_export,
2470 int keylength)) {
2471 ssl->cert->dh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002472}
2473
2474void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,
David Benjamindd978782015-04-24 15:20:13 -04002475 EC_KEY *(*callback)(SSL *ssl, int is_export,
2476 int keylength)) {
David Benjamin59015c32015-04-26 13:13:55 -04002477 ctx->cert->ecdh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002478}
2479
2480void SSL_set_tmp_ecdh_callback(SSL *ssl,
David Benjamindd978782015-04-24 15:20:13 -04002481 EC_KEY *(*callback)(SSL *ssl, int is_export,
2482 int keylength)) {
David Benjamin59015c32015-04-26 13:13:55 -04002483 ssl->cert->ecdh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002484}
2485
2486int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
2487 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2488 OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_psk_identity_hint,
2489 SSL_R_DATA_LENGTH_TOO_LONG);
2490 return 0;
2491 }
2492
David Benjamin2755a3e2015-04-22 16:17:58 -04002493 OPENSSL_free(ctx->psk_identity_hint);
Adam Langleyfcf25832014-12-18 17:42:32 -08002494
2495 if (identity_hint != NULL) {
2496 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2497 if (ctx->psk_identity_hint == NULL) {
2498 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002499 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002500 } else {
2501 ctx->psk_identity_hint = NULL;
2502 }
Adam Langley95c29f32014-06-20 12:00:00 -07002503
Adam Langleyfcf25832014-12-18 17:42:32 -08002504 return 1;
2505}
2506
2507int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint) {
2508 if (s == NULL) {
2509 return 0;
2510 }
2511
2512 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2513 OPENSSL_PUT_ERROR(SSL, SSL_use_psk_identity_hint,
2514 SSL_R_DATA_LENGTH_TOO_LONG);
2515 return 0;
2516 }
2517
2518 /* Clear currently configured hint, if any. */
David Benjamin2755a3e2015-04-22 16:17:58 -04002519 OPENSSL_free(s->psk_identity_hint);
2520 s->psk_identity_hint = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002521
2522 if (identity_hint != NULL) {
2523 s->psk_identity_hint = BUF_strdup(identity_hint);
2524 if (s->psk_identity_hint == NULL) {
2525 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002526 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002527 }
Adam Langley95c29f32014-06-20 12:00:00 -07002528
Adam Langleyfcf25832014-12-18 17:42:32 -08002529 return 1;
2530}
Adam Langley95c29f32014-06-20 12:00:00 -07002531
Adam Langleyfcf25832014-12-18 17:42:32 -08002532const char *SSL_get_psk_identity_hint(const SSL *s) {
2533 if (s == NULL) {
2534 return NULL;
2535 }
2536 return s->psk_identity_hint;
2537}
Adam Langley95c29f32014-06-20 12:00:00 -07002538
Adam Langleyfcf25832014-12-18 17:42:32 -08002539const char *SSL_get_psk_identity(const SSL *s) {
2540 if (s == NULL || s->session == NULL) {
2541 return NULL;
2542 }
Adam Langley95c29f32014-06-20 12:00:00 -07002543
Adam Langleyfcf25832014-12-18 17:42:32 -08002544 return s->session->psk_identity;
2545}
Adam Langley95c29f32014-06-20 12:00:00 -07002546
Adam Langleyfcf25832014-12-18 17:42:32 -08002547void SSL_set_psk_client_callback(
2548 SSL *s, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2549 unsigned int max_identity_len, uint8_t *psk,
2550 unsigned int max_psk_len)) {
2551 s->psk_client_callback = cb;
2552}
Adam Langley95c29f32014-06-20 12:00:00 -07002553
Adam Langleyfcf25832014-12-18 17:42:32 -08002554void SSL_CTX_set_psk_client_callback(
2555 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2556 unsigned int max_identity_len,
2557 uint8_t *psk, unsigned int max_psk_len)) {
2558 ctx->psk_client_callback = cb;
2559}
Adam Langley95c29f32014-06-20 12:00:00 -07002560
Adam Langleyfcf25832014-12-18 17:42:32 -08002561void SSL_set_psk_server_callback(
2562 SSL *s, unsigned int (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
2563 unsigned int max_psk_len)) {
2564 s->psk_server_callback = cb;
2565}
Adam Langley95c29f32014-06-20 12:00:00 -07002566
Adam Langleyfcf25832014-12-18 17:42:32 -08002567void SSL_CTX_set_psk_server_callback(
2568 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *identity,
2569 uint8_t *psk, unsigned int max_psk_len)) {
2570 ctx->psk_server_callback = cb;
2571}
Adam Langley95c29f32014-06-20 12:00:00 -07002572
Adam Langleyfcf25832014-12-18 17:42:32 -08002573void SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) {
2574 ctx->min_version = version;
2575}
Adam Langley95c29f32014-06-20 12:00:00 -07002576
Adam Langleyfcf25832014-12-18 17:42:32 -08002577void SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) {
2578 ctx->max_version = version;
2579}
Adam Langley0289c732014-06-20 12:00:00 -07002580
Adam Langleyfcf25832014-12-18 17:42:32 -08002581void SSL_set_min_version(SSL *ssl, uint16_t version) {
2582 ssl->min_version = version;
2583}
Adam Langley0289c732014-06-20 12:00:00 -07002584
Adam Langleyfcf25832014-12-18 17:42:32 -08002585void SSL_set_max_version(SSL *ssl, uint16_t version) {
2586 ssl->max_version = version;
2587}
Adam Langley95c29f32014-06-20 12:00:00 -07002588
Adam Langleyfcf25832014-12-18 17:42:32 -08002589void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
2590 void (*cb)(int write_p, int version,
2591 int content_type, const void *buf,
2592 size_t len, SSL *ssl, void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002593 ctx->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002594}
2595void SSL_set_msg_callback(SSL *ssl,
2596 void (*cb)(int write_p, int version, int content_type,
2597 const void *buf, size_t len, SSL *ssl,
2598 void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002599 ssl->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002600}
Adam Langley95c29f32014-06-20 12:00:00 -07002601
Adam Langleyfcf25832014-12-18 17:42:32 -08002602void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio) {
David Benjamin2755a3e2015-04-22 16:17:58 -04002603 BIO_free(ctx->keylog_bio);
Adam Langleyfcf25832014-12-18 17:42:32 -08002604 ctx->keylog_bio = keylog_bio;
2605}
Adam Langley95c29f32014-06-20 12:00:00 -07002606
Adam Langleyfcf25832014-12-18 17:42:32 -08002607static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
2608 static const char hextable[] = "0123456789abcdef";
2609 uint8_t *out;
2610 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002611
Adam Langleyfcf25832014-12-18 17:42:32 -08002612 if (!CBB_add_space(cbb, &out, in_len * 2)) {
2613 return 0;
2614 }
Adam Langley95c29f32014-06-20 12:00:00 -07002615
Adam Langleyfcf25832014-12-18 17:42:32 -08002616 for (i = 0; i < in_len; i++) {
2617 *(out++) = (uint8_t)hextable[in[i] >> 4];
2618 *(out++) = (uint8_t)hextable[in[i] & 0xf];
2619 }
Adam Langley95c29f32014-06-20 12:00:00 -07002620
Adam Langleyfcf25832014-12-18 17:42:32 -08002621 return 1;
2622}
David Benjamin859ec3c2014-09-02 16:29:36 -04002623
2624int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -08002625 const uint8_t *encrypted_premaster,
2626 size_t encrypted_premaster_len,
2627 const uint8_t *premaster,
2628 size_t premaster_len) {
2629 BIO *bio = ctx->keylog_bio;
2630 CBB cbb;
2631 uint8_t *out;
2632 size_t out_len;
2633 int ret;
David Benjamin859ec3c2014-09-02 16:29:36 -04002634
Adam Langleyfcf25832014-12-18 17:42:32 -08002635 if (bio == NULL) {
2636 return 1;
2637 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002638
Adam Langleyfcf25832014-12-18 17:42:32 -08002639 if (encrypted_premaster_len < 8) {
2640 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_rsa_client_key_exchange,
2641 ERR_R_INTERNAL_ERROR);
2642 return 0;
2643 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002644
Adam Langleyfcf25832014-12-18 17:42:32 -08002645 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len * 2 + 1)) {
2646 return 0;
2647 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002648
Adam Langleyfcf25832014-12-18 17:42:32 -08002649 if (!CBB_add_bytes(&cbb, (const uint8_t *)"RSA ", 4) ||
2650 /* Only the first 8 bytes of the encrypted premaster secret are
2651 * logged. */
2652 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
2653 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2654 !cbb_add_hex(&cbb, premaster, premaster_len) ||
2655 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2656 !CBB_finish(&cbb, &out, &out_len)) {
2657 CBB_cleanup(&cbb);
2658 return 0;
2659 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002660
Adam Langleyfcf25832014-12-18 17:42:32 -08002661 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2662 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2663 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
David Benjamin859ec3c2014-09-02 16:29:36 -04002664
Adam Langleyfcf25832014-12-18 17:42:32 -08002665 OPENSSL_free(out);
2666 return ret;
Adam Langley95f22882014-06-20 12:00:00 -07002667}
2668
Adam Langleyfcf25832014-12-18 17:42:32 -08002669int ssl_ctx_log_master_secret(SSL_CTX *ctx, const uint8_t *client_random,
2670 size_t client_random_len, const uint8_t *master,
2671 size_t master_len) {
2672 BIO *bio = ctx->keylog_bio;
2673 CBB cbb;
2674 uint8_t *out;
2675 size_t out_len;
2676 int ret;
Adam Langleyadb739e2014-06-20 12:00:00 -07002677
Adam Langleyfcf25832014-12-18 17:42:32 -08002678 if (bio == NULL) {
2679 return 1;
2680 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002681
Adam Langleyfcf25832014-12-18 17:42:32 -08002682 if (client_random_len != 32) {
2683 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_master_secret, ERR_R_INTERNAL_ERROR);
2684 return 0;
2685 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002686
Adam Langleyfcf25832014-12-18 17:42:32 -08002687 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len * 2 + 1)) {
2688 return 0;
2689 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002690
Adam Langleyfcf25832014-12-18 17:42:32 -08002691 if (!CBB_add_bytes(&cbb, (const uint8_t *)"CLIENT_RANDOM ", 14) ||
2692 !cbb_add_hex(&cbb, client_random, 32) ||
2693 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2694 !cbb_add_hex(&cbb, master, master_len) ||
2695 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2696 !CBB_finish(&cbb, &out, &out_len)) {
2697 CBB_cleanup(&cbb);
2698 return 0;
2699 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002700
Adam Langleyfcf25832014-12-18 17:42:32 -08002701 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2702 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2703 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
David Benjamine99e9122014-12-11 01:46:01 -05002704
Adam Langleyfcf25832014-12-18 17:42:32 -08002705 OPENSSL_free(out);
2706 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -07002707}
2708
David Benjamined7c4752015-02-16 19:16:46 -05002709int SSL_in_false_start(const SSL *s) {
2710 return s->s3->tmp.in_false_start;
2711}
2712
Adam Langleyfcf25832014-12-18 17:42:32 -08002713int SSL_cutthrough_complete(const SSL *s) {
David Benjamined7c4752015-02-16 19:16:46 -05002714 return SSL_in_false_start(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002715}
Adam Langley95c29f32014-06-20 12:00:00 -07002716
Adam Langleyfcf25832014-12-18 17:42:32 -08002717void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
2718 size_t *ssl_session_size) {
2719 *ssl_size = sizeof(SSL);
2720 *ssl_ctx_size = sizeof(SSL_CTX);
2721 *ssl_session_size = sizeof(SSL_SESSION);
2722}
Feng Lu41aa3252014-11-21 22:47:56 -08002723
David Benjamined7c4752015-02-16 19:16:46 -05002724int ssl3_can_false_start(const SSL *s) {
David Benjamin195dc782015-02-19 13:27:05 -05002725 const SSL_CIPHER *const cipher = SSL_get_current_cipher(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002726
Adam Langleye631d962015-04-01 13:11:01 -07002727 /* False Start only for TLS 1.2 with an ECDHE+AEAD cipher and ALPN or NPN. */
David Benjamin195dc782015-02-19 13:27:05 -05002728 return !SSL_IS_DTLS(s) &&
2729 SSL_version(s) >= TLS1_2_VERSION &&
2730 (s->s3->alpn_selected || s->s3->next_proto_neg_seen) &&
2731 cipher != NULL &&
Adam Langleye631d962015-04-01 13:11:01 -07002732 cipher->algorithm_mkey == SSL_kECDHE &&
David Benjamin195dc782015-02-19 13:27:05 -05002733 (cipher->algorithm_enc == SSL_AES128GCM ||
2734 cipher->algorithm_enc == SSL_AES256GCM ||
2735 cipher->algorithm_enc == SSL_CHACHA20POLY1305);
Adam Langleyfcf25832014-12-18 17:42:32 -08002736}
2737
2738const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version) {
2739 switch (version) {
2740 case SSL3_VERSION:
2741 return &SSLv3_enc_data;
2742
2743 case TLS1_VERSION:
2744 return &TLSv1_enc_data;
2745
David Benjamin9e13e1a2015-03-05 01:56:32 -05002746 case DTLS1_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002747 case TLS1_1_VERSION:
2748 return &TLSv1_1_enc_data;
2749
David Benjamin9e13e1a2015-03-05 01:56:32 -05002750 case DTLS1_2_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002751 case TLS1_2_VERSION:
2752 return &TLSv1_2_enc_data;
2753
Adam Langleyfcf25832014-12-18 17:42:32 -08002754 default:
2755 return NULL;
2756 }
2757}
2758
2759uint16_t ssl3_get_max_server_version(const SSL *s) {
2760 uint16_t max_version;
2761
2762 if (SSL_IS_DTLS(s)) {
2763 max_version = (s->max_version != 0) ? s->max_version : DTLS1_2_VERSION;
2764 if (!(s->options & SSL_OP_NO_DTLSv1_2) && DTLS1_2_VERSION >= max_version) {
2765 return DTLS1_2_VERSION;
2766 }
2767 if (!(s->options & SSL_OP_NO_DTLSv1) && DTLS1_VERSION >= max_version) {
2768 return DTLS1_VERSION;
2769 }
2770 return 0;
2771 }
2772
2773 max_version = (s->max_version != 0) ? s->max_version : TLS1_2_VERSION;
2774 if (!(s->options & SSL_OP_NO_TLSv1_2) && TLS1_2_VERSION <= max_version) {
2775 return TLS1_2_VERSION;
2776 }
2777 if (!(s->options & SSL_OP_NO_TLSv1_1) && TLS1_1_VERSION <= max_version) {
2778 return TLS1_1_VERSION;
2779 }
2780 if (!(s->options & SSL_OP_NO_TLSv1) && TLS1_VERSION <= max_version) {
2781 return TLS1_VERSION;
2782 }
2783 if (!(s->options & SSL_OP_NO_SSLv3) && SSL3_VERSION <= max_version) {
2784 return SSL3_VERSION;
2785 }
2786 return 0;
2787}
2788
2789uint16_t ssl3_get_mutual_version(SSL *s, uint16_t client_version) {
2790 uint16_t version = 0;
2791
2792 if (SSL_IS_DTLS(s)) {
2793 /* Clamp client_version to max_version. */
2794 if (s->max_version != 0 && client_version < s->max_version) {
2795 client_version = s->max_version;
2796 }
2797
2798 if (client_version <= DTLS1_2_VERSION && !(s->options & SSL_OP_NO_DTLSv1_2)) {
2799 version = DTLS1_2_VERSION;
2800 } else if (client_version <= DTLS1_VERSION &&
David Benjaminb18f0242015-03-10 18:30:08 -04002801 !(s->options & SSL_OP_NO_DTLSv1)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002802 version = DTLS1_VERSION;
2803 }
2804
2805 /* Check against min_version. */
2806 if (version != 0 && s->min_version != 0 && version > s->min_version) {
2807 return 0;
2808 }
2809 return version;
2810 } else {
2811 /* Clamp client_version to max_version. */
2812 if (s->max_version != 0 && client_version > s->max_version) {
2813 client_version = s->max_version;
2814 }
2815
2816 if (client_version >= TLS1_2_VERSION && !(s->options & SSL_OP_NO_TLSv1_2)) {
2817 version = TLS1_2_VERSION;
2818 } else if (client_version >= TLS1_1_VERSION &&
2819 !(s->options & SSL_OP_NO_TLSv1_1)) {
2820 version = TLS1_1_VERSION;
2821 } else if (client_version >= TLS1_VERSION && !(s->options & SSL_OP_NO_TLSv1)) {
2822 version = TLS1_VERSION;
2823 } else if (client_version >= SSL3_VERSION && !(s->options & SSL_OP_NO_SSLv3)) {
2824 version = SSL3_VERSION;
2825 }
2826
2827 /* Check against min_version. */
2828 if (version != 0 && s->min_version != 0 && version < s->min_version) {
2829 return 0;
2830 }
2831 return version;
2832 }
2833}
2834
2835uint16_t ssl3_get_max_client_version(SSL *s) {
2836 unsigned long options = s->options;
2837 uint16_t version = 0;
2838
2839 /* OpenSSL's API for controlling versions entails blacklisting individual
2840 * protocols. This has two problems. First, on the client, the protocol can
2841 * only express a contiguous range of versions. Second, a library consumer
2842 * trying to set a maximum version cannot disable protocol versions that get
2843 * added in a future version of the library.
2844 *
2845 * To account for both of these, OpenSSL interprets the client-side bitmask
2846 * as a min/max range by picking the lowest contiguous non-empty range of
2847 * enabled protocols. Note that this means it is impossible to set a maximum
2848 * version of TLS 1.2 in a future-proof way.
2849 *
2850 * By this scheme, the maximum version is the lowest version V such that V is
2851 * enabled and V+1 is disabled or unimplemented. */
2852 if (SSL_IS_DTLS(s)) {
2853 if (!(options & SSL_OP_NO_DTLSv1_2)) {
2854 version = DTLS1_2_VERSION;
2855 }
2856 if (!(options & SSL_OP_NO_DTLSv1) && (options & SSL_OP_NO_DTLSv1_2)) {
2857 version = DTLS1_VERSION;
2858 }
2859 if (s->max_version != 0 && version < s->max_version) {
2860 version = s->max_version;
2861 }
2862 } else {
2863 if (!(options & SSL_OP_NO_TLSv1_2)) {
2864 version = TLS1_2_VERSION;
2865 }
2866 if (!(options & SSL_OP_NO_TLSv1_1) && (options & SSL_OP_NO_TLSv1_2)) {
2867 version = TLS1_1_VERSION;
2868 }
2869 if (!(options & SSL_OP_NO_TLSv1) && (options & SSL_OP_NO_TLSv1_1)) {
2870 version = TLS1_VERSION;
2871 }
2872 if (!(options & SSL_OP_NO_SSLv3) && (options & SSL_OP_NO_TLSv1)) {
2873 version = SSL3_VERSION;
2874 }
2875 if (s->max_version != 0 && version > s->max_version) {
2876 version = s->max_version;
2877 }
2878 }
2879
2880 return version;
2881}
2882
2883int ssl3_is_version_enabled(SSL *s, uint16_t version) {
2884 if (SSL_IS_DTLS(s)) {
2885 if (s->max_version != 0 && version < s->max_version) {
2886 return 0;
2887 }
2888 if (s->min_version != 0 && version > s->min_version) {
2889 return 0;
2890 }
2891
2892 switch (version) {
2893 case DTLS1_VERSION:
2894 return !(s->options & SSL_OP_NO_DTLSv1);
2895
2896 case DTLS1_2_VERSION:
2897 return !(s->options & SSL_OP_NO_DTLSv1_2);
2898
2899 default:
2900 return 0;
2901 }
2902 } else {
2903 if (s->max_version != 0 && version > s->max_version) {
2904 return 0;
2905 }
2906 if (s->min_version != 0 && version < s->min_version) {
2907 return 0;
2908 }
2909
2910 switch (version) {
2911 case SSL3_VERSION:
2912 return !(s->options & SSL_OP_NO_SSLv3);
2913
2914 case TLS1_VERSION:
2915 return !(s->options & SSL_OP_NO_TLSv1);
2916
2917 case TLS1_1_VERSION:
2918 return !(s->options & SSL_OP_NO_TLSv1_1);
2919
2920 case TLS1_2_VERSION:
2921 return !(s->options & SSL_OP_NO_TLSv1_2);
2922
2923 default:
2924 return 0;
2925 }
2926 }
2927}
2928
David Benjaminea72bd02014-12-21 21:27:41 -05002929uint16_t ssl3_version_from_wire(SSL *s, uint16_t wire_version) {
2930 if (!SSL_IS_DTLS(s)) {
2931 return wire_version;
2932 }
2933
2934 uint16_t tls_version = ~wire_version;
2935 uint16_t version = tls_version + 0x0201;
2936 /* If either component overflowed, clamp it so comparisons still work. */
2937 if ((version >> 8) < (tls_version >> 8)) {
2938 version = 0xff00 | (version & 0xff);
2939 }
2940 if ((version & 0xff) < (tls_version & 0xff)) {
2941 version = (version & 0xff00) | 0xff;
2942 }
2943 /* DTLS 1.0 maps to TLS 1.1, not TLS 1.0. */
2944 if (version == TLS1_VERSION) {
2945 version = TLS1_1_VERSION;
2946 }
2947 return version;
2948}
2949
Adam Langleyfcf25832014-12-18 17:42:32 -08002950int SSL_cache_hit(SSL *s) { return s->hit; }
2951
2952int SSL_is_server(SSL *s) { return s->server; }
2953
Adam Langley524e7172015-02-20 16:04:00 -08002954void SSL_CTX_set_dos_protection_cb(
2955 SSL_CTX *ctx, int (*cb)(const struct ssl_early_callback_ctx *)) {
2956 ctx->dos_protection_cb = cb;
2957}
2958
Adam Langleyfcf25832014-12-18 17:42:32 -08002959void SSL_enable_fastradio_padding(SSL *s, char on_off) {
2960 s->fastradio_padding = on_off;
2961}
Adam Langley44972942015-02-20 13:43:23 -08002962
David Benjaminb16346b2015-04-08 19:16:58 -04002963void SSL_set_reject_peer_renegotiations(SSL *s, int reject) {
2964 s->reject_peer_renegotiations = !!reject;
2965}
2966
Adam Langley44972942015-02-20 13:43:23 -08002967const SSL_CIPHER *SSL_get_cipher_by_value(uint16_t value) {
2968 return ssl3_get_cipher_by_value(value);
2969}
Adam Langley3f92d212015-02-20 15:32:52 -08002970
2971int SSL_get_rc4_state(const SSL *ssl, const RC4_KEY **read_key,
2972 const RC4_KEY **write_key) {
2973 if (ssl->aead_read_ctx == NULL || ssl->aead_write_ctx == NULL) {
2974 return 0;
2975 }
2976
2977 return EVP_AEAD_CTX_get_rc4_state(&ssl->aead_read_ctx->ctx, read_key) &&
2978 EVP_AEAD_CTX_get_rc4_state(&ssl->aead_write_ctx->ctx, write_key);
2979}