blob: 2d0bec704888c2acd1737d62b0f5ad2a3059a14a [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
Adam Langleyfcf25832014-12-18 17:42:32 -0800208 if (s->init_buf != NULL) {
209 BUF_MEM_free(s->init_buf);
210 s->init_buf = NULL;
211 }
Adam Langley95c29f32014-06-20 12:00:00 -0700212
David Benjamin62fd1622015-01-11 13:30:01 -0500213 s->packet = NULL;
214 s->packet_length = 0;
215
Adam Langleyfcf25832014-12-18 17:42:32 -0800216 ssl_clear_cipher_ctx(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700217
David Benjamin62fd1622015-01-11 13:30:01 -0500218 if (s->next_proto_negotiated) {
219 OPENSSL_free(s->next_proto_negotiated);
220 s->next_proto_negotiated = NULL;
221 s->next_proto_negotiated_len = 0;
222 }
223
224 /* The s->d1->mtu is simultaneously configuration (preserved across
225 * clear) and connection-specific state (gets reset).
226 *
227 * TODO(davidben): Avoid this. */
228 unsigned mtu = 0;
229 if (s->d1 != NULL) {
230 mtu = s->d1->mtu;
231 }
232
233 s->method->ssl_free(s);
234 if (!s->method->ssl_new(s)) {
235 return 0;
236 }
237 s->enc_method = ssl3_get_enc_method(s->version);
238 assert(s->enc_method != NULL);
239
240 if (SSL_IS_DTLS(s) && (SSL_get_options(s) & SSL_OP_NO_QUERY_MTU)) {
241 s->d1->mtu = mtu;
242 }
243
Adam Langleyfcf25832014-12-18 17:42:32 -0800244 s->client_version = s->version;
Adam Langley95c29f32014-06-20 12:00:00 -0700245
Adam Langleyfcf25832014-12-18 17:42:32 -0800246 return 1;
247}
Adam Langley95c29f32014-06-20 12:00:00 -0700248
Adam Langleyfcf25832014-12-18 17:42:32 -0800249SSL *SSL_new(SSL_CTX *ctx) {
250 SSL *s;
Adam Langley95c29f32014-06-20 12:00:00 -0700251
Adam Langleyfcf25832014-12-18 17:42:32 -0800252 if (ctx == NULL) {
253 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_NULL_SSL_CTX);
254 return NULL;
255 }
256 if (ctx->method == NULL) {
257 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
258 return NULL;
259 }
Adam Langley95c29f32014-06-20 12:00:00 -0700260
Adam Langleyfcf25832014-12-18 17:42:32 -0800261 s = (SSL *)OPENSSL_malloc(sizeof(SSL));
262 if (s == NULL) {
263 goto err;
264 }
265 memset(s, 0, sizeof(SSL));
Adam Langley95c29f32014-06-20 12:00:00 -0700266
Adam Langleyfcf25832014-12-18 17:42:32 -0800267 s->min_version = ctx->min_version;
268 s->max_version = ctx->max_version;
David Benjamin1eb367c2014-12-12 18:17:51 -0500269
Adam Langleyfcf25832014-12-18 17:42:32 -0800270 s->options = ctx->options;
271 s->mode = ctx->mode;
272 s->max_cert_list = ctx->max_cert_list;
Adam Langley95c29f32014-06-20 12:00:00 -0700273
David Benjamina5a3eeb2015-03-18 20:26:30 -0400274 s->cert = ssl_cert_dup(ctx->cert);
275 if (s->cert == NULL) {
276 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -0800277 }
Adam Langley95c29f32014-06-20 12:00:00 -0700278
Adam Langleyfcf25832014-12-18 17:42:32 -0800279 s->read_ahead = ctx->read_ahead;
280 s->msg_callback = ctx->msg_callback;
281 s->msg_callback_arg = ctx->msg_callback_arg;
282 s->verify_mode = ctx->verify_mode;
283 s->sid_ctx_length = ctx->sid_ctx_length;
284 assert(s->sid_ctx_length <= sizeof s->sid_ctx);
285 memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));
286 s->verify_callback = ctx->default_verify_callback;
287 s->generate_session_id = ctx->generate_session_id;
Adam Langley95c29f32014-06-20 12:00:00 -0700288
Adam Langleyfcf25832014-12-18 17:42:32 -0800289 s->param = X509_VERIFY_PARAM_new();
290 if (!s->param) {
291 goto err;
292 }
293 X509_VERIFY_PARAM_inherit(s->param, ctx->param);
294 s->quiet_shutdown = ctx->quiet_shutdown;
295 s->max_send_fragment = ctx->max_send_fragment;
Adam Langley95c29f32014-06-20 12:00:00 -0700296
Adam Langleyfcf25832014-12-18 17:42:32 -0800297 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
298 s->ctx = ctx;
299 s->tlsext_debug_cb = 0;
300 s->tlsext_debug_arg = NULL;
301 s->tlsext_ticket_expected = 0;
302 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
303 s->initial_ctx = ctx;
304 if (ctx->tlsext_ecpointformatlist) {
305 s->tlsext_ecpointformatlist = BUF_memdup(
306 ctx->tlsext_ecpointformatlist, ctx->tlsext_ecpointformatlist_length);
307 if (!s->tlsext_ecpointformatlist) {
308 goto err;
309 }
310 s->tlsext_ecpointformatlist_length = ctx->tlsext_ecpointformatlist_length;
311 }
Adam Langley95c29f32014-06-20 12:00:00 -0700312
Adam Langleyfcf25832014-12-18 17:42:32 -0800313 if (ctx->tlsext_ellipticcurvelist) {
314 s->tlsext_ellipticcurvelist =
315 BUF_memdup(ctx->tlsext_ellipticcurvelist,
316 ctx->tlsext_ellipticcurvelist_length * 2);
317 if (!s->tlsext_ellipticcurvelist) {
318 goto err;
319 }
320 s->tlsext_ellipticcurvelist_length = ctx->tlsext_ellipticcurvelist_length;
321 }
322 s->next_proto_negotiated = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700323
Adam Langleyfcf25832014-12-18 17:42:32 -0800324 if (s->ctx->alpn_client_proto_list) {
325 s->alpn_client_proto_list = BUF_memdup(s->ctx->alpn_client_proto_list,
326 s->ctx->alpn_client_proto_list_len);
327 if (s->alpn_client_proto_list == NULL) {
328 goto err;
329 }
330 s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;
331 }
Adam Langley95c29f32014-06-20 12:00:00 -0700332
Adam Langleyfcf25832014-12-18 17:42:32 -0800333 s->verify_result = X509_V_OK;
334 s->method = ctx->method;
Adam Langley95c29f32014-06-20 12:00:00 -0700335
Adam Langleyfcf25832014-12-18 17:42:32 -0800336 if (!s->method->ssl_new(s)) {
337 goto err;
338 }
339 s->enc_method = ssl3_get_enc_method(s->version);
340 assert(s->enc_method != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700341
David Benjamin62fd1622015-01-11 13:30:01 -0500342 s->rwstate = SSL_NOTHING;
343 s->rstate = SSL_ST_READ_HEADER;
Adam Langley95c29f32014-06-20 12:00:00 -0700344
David Benjamin9f33fc62015-04-15 17:29:53 -0400345 CRYPTO_new_ex_data(&g_ex_data_class_ssl, s, &s->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700346
Adam Langleyfcf25832014-12-18 17:42:32 -0800347 s->psk_identity_hint = NULL;
348 if (ctx->psk_identity_hint) {
349 s->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
350 if (s->psk_identity_hint == NULL) {
351 goto err;
352 }
353 }
354 s->psk_client_callback = ctx->psk_client_callback;
355 s->psk_server_callback = ctx->psk_server_callback;
Adam Langley95c29f32014-06-20 12:00:00 -0700356
David Benjamin02ddbfd2015-01-11 13:09:11 -0500357 s->tlsext_channel_id_enabled = ctx->tlsext_channel_id_enabled;
358 if (ctx->tlsext_channel_id_private) {
359 s->tlsext_channel_id_private = EVP_PKEY_dup(ctx->tlsext_channel_id_private);
360 }
361
Adam Langleyfcf25832014-12-18 17:42:32 -0800362 s->signed_cert_timestamps_enabled = s->ctx->signed_cert_timestamps_enabled;
363 s->ocsp_stapling_enabled = s->ctx->ocsp_stapling_enabled;
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200364
Adam Langleyfcf25832014-12-18 17:42:32 -0800365 return s;
366
Adam Langley95c29f32014-06-20 12:00:00 -0700367err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800368 if (s != NULL) {
David Benjamin710d2272015-01-11 20:07:59 -0500369 SSL_free(s);
Adam Langleyfcf25832014-12-18 17:42:32 -0800370 }
371 OPENSSL_PUT_ERROR(SSL, SSL_new, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700372
Adam Langleyfcf25832014-12-18 17:42:32 -0800373 return NULL;
374}
Adam Langley95c29f32014-06-20 12:00:00 -0700375
Adam Langleyfcf25832014-12-18 17:42:32 -0800376int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
377 unsigned int sid_ctx_len) {
378 if (sid_ctx_len > sizeof ctx->sid_ctx) {
379 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_session_id_context,
380 SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
381 return 0;
382 }
383 ctx->sid_ctx_length = sid_ctx_len;
384 memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700385
Adam Langleyfcf25832014-12-18 17:42:32 -0800386 return 1;
387}
Adam Langley95c29f32014-06-20 12:00:00 -0700388
Adam Langleyfcf25832014-12-18 17:42:32 -0800389int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
390 unsigned int sid_ctx_len) {
391 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
392 OPENSSL_PUT_ERROR(SSL, SSL_set_session_id_context,
393 SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
394 return 0;
395 }
396 ssl->sid_ctx_length = sid_ctx_len;
397 memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700398
Adam Langleyfcf25832014-12-18 17:42:32 -0800399 return 1;
400}
Adam Langley95c29f32014-06-20 12:00:00 -0700401
Adam Langleyfcf25832014-12-18 17:42:32 -0800402int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb) {
403 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
404 ctx->generate_session_id = cb;
405 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
406 return 1;
407}
Adam Langley95c29f32014-06-20 12:00:00 -0700408
Adam Langleyfcf25832014-12-18 17:42:32 -0800409int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800410 ssl->generate_session_id = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -0800411 return 1;
412}
Adam Langley95c29f32014-06-20 12:00:00 -0700413
Adam Langleyfcf25832014-12-18 17:42:32 -0800414int SSL_has_matching_session_id(const SSL *ssl, const uint8_t *id,
415 unsigned int id_len) {
416 /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how we
417 * can "construct" a session to give us the desired check - ie. to find if
418 * there's a session in the hash table that would conflict with any new
419 * session built out of this id/id_len and the ssl_version in use by this
420 * SSL. */
421 SSL_SESSION r, *p;
Adam Langley95c29f32014-06-20 12:00:00 -0700422
Adam Langleyfcf25832014-12-18 17:42:32 -0800423 if (id_len > sizeof r.session_id) {
424 return 0;
425 }
Adam Langley95c29f32014-06-20 12:00:00 -0700426
Adam Langleyfcf25832014-12-18 17:42:32 -0800427 r.ssl_version = ssl->version;
428 r.session_id_length = id_len;
429 memcpy(r.session_id, id, id_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700430
Adam Langleyfcf25832014-12-18 17:42:32 -0800431 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
432 p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r);
433 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
434 return p != NULL;
435}
Adam Langley95c29f32014-06-20 12:00:00 -0700436
Adam Langleyfcf25832014-12-18 17:42:32 -0800437int SSL_CTX_set_purpose(SSL_CTX *s, int purpose) {
438 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
439}
440
441int SSL_set_purpose(SSL *s, int purpose) {
442 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
443}
444
445int SSL_CTX_set_trust(SSL_CTX *s, int trust) {
446 return X509_VERIFY_PARAM_set_trust(s->param, trust);
447}
448
449int SSL_set_trust(SSL *s, int trust) {
450 return X509_VERIFY_PARAM_set_trust(s->param, trust);
451}
452
453int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm) {
454 return X509_VERIFY_PARAM_set1(ctx->param, vpm);
455}
456
457int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm) {
458 return X509_VERIFY_PARAM_set1(ssl->param, vpm);
459}
Adam Langley95c29f32014-06-20 12:00:00 -0700460
Adam Langley858a88d2014-06-20 12:00:00 -0700461void ssl_cipher_preference_list_free(
Adam Langleyfcf25832014-12-18 17:42:32 -0800462 struct ssl_cipher_preference_list_st *cipher_list) {
David Benjamin5d1ec732015-04-22 13:38:00 -0400463 if (cipher_list == NULL) {
464 return;
465 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800466 sk_SSL_CIPHER_free(cipher_list->ciphers);
467 OPENSSL_free(cipher_list->in_group_flags);
468 OPENSSL_free(cipher_list);
469}
Adam Langley858a88d2014-06-20 12:00:00 -0700470
Adam Langleyfcf25832014-12-18 17:42:32 -0800471struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_dup(
472 struct ssl_cipher_preference_list_st *cipher_list) {
473 struct ssl_cipher_preference_list_st *ret = NULL;
474 size_t n = sk_SSL_CIPHER_num(cipher_list->ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700475
Adam Langleyfcf25832014-12-18 17:42:32 -0800476 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
477 if (!ret) {
478 goto err;
479 }
480
481 ret->ciphers = NULL;
482 ret->in_group_flags = NULL;
483 ret->ciphers = sk_SSL_CIPHER_dup(cipher_list->ciphers);
484 if (!ret->ciphers) {
485 goto err;
486 }
487 ret->in_group_flags = BUF_memdup(cipher_list->in_group_flags, n);
488 if (!ret->in_group_flags) {
489 goto err;
490 }
491
492 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -0700493
494err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800495 if (ret && ret->ciphers) {
496 sk_SSL_CIPHER_free(ret->ciphers);
497 }
498 if (ret) {
499 OPENSSL_free(ret);
500 }
501 return NULL;
502}
Adam Langley858a88d2014-06-20 12:00:00 -0700503
Adam Langleyfcf25832014-12-18 17:42:32 -0800504struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_from_ciphers(
505 STACK_OF(SSL_CIPHER) * ciphers) {
506 struct ssl_cipher_preference_list_st *ret = NULL;
507 size_t n = sk_SSL_CIPHER_num(ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700508
Adam Langleyfcf25832014-12-18 17:42:32 -0800509 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
510 if (!ret) {
511 goto err;
512 }
513 ret->ciphers = NULL;
514 ret->in_group_flags = NULL;
515 ret->ciphers = sk_SSL_CIPHER_dup(ciphers);
516 if (!ret->ciphers) {
517 goto err;
518 }
519 ret->in_group_flags = OPENSSL_malloc(n);
520 if (!ret->in_group_flags) {
521 goto err;
522 }
523 memset(ret->in_group_flags, 0, n);
524 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -0700525
526err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800527 if (ret && ret->ciphers) {
528 sk_SSL_CIPHER_free(ret->ciphers);
529 }
530 if (ret) {
531 OPENSSL_free(ret);
532 }
533 return NULL;
534}
Adam Langley858a88d2014-06-20 12:00:00 -0700535
Adam Langleyfcf25832014-12-18 17:42:32 -0800536X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) { return ctx->param; }
Adam Langley95c29f32014-06-20 12:00:00 -0700537
Adam Langleyfcf25832014-12-18 17:42:32 -0800538X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) { return ssl->param; }
Adam Langley95c29f32014-06-20 12:00:00 -0700539
Adam Langleyfcf25832014-12-18 17:42:32 -0800540void SSL_certs_clear(SSL *s) { ssl_cert_clear_certs(s->cert); }
Adam Langley95c29f32014-06-20 12:00:00 -0700541
Adam Langleyfcf25832014-12-18 17:42:32 -0800542void SSL_free(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800543 if (s == NULL) {
544 return;
545 }
Adam Langley95c29f32014-06-20 12:00:00 -0700546
Adam Langleyfcf25832014-12-18 17:42:32 -0800547 if (s->param) {
548 X509_VERIFY_PARAM_free(s->param);
549 }
Adam Langley95c29f32014-06-20 12:00:00 -0700550
David Benjamin9f33fc62015-04-15 17:29:53 -0400551 CRYPTO_free_ex_data(&g_ex_data_class_ssl, s, &s->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700552
Adam Langleyfcf25832014-12-18 17:42:32 -0800553 if (s->bbio != NULL) {
554 /* If the buffering BIO is in place, pop it off */
555 if (s->bbio == s->wbio) {
556 s->wbio = BIO_pop(s->wbio);
557 }
558 BIO_free(s->bbio);
559 s->bbio = NULL;
560 }
Adam Langley95c29f32014-06-20 12:00:00 -0700561
Adam Langleyfcf25832014-12-18 17:42:32 -0800562 if (s->rbio != NULL) {
563 BIO_free_all(s->rbio);
564 }
Adam Langley95c29f32014-06-20 12:00:00 -0700565
Adam Langleyfcf25832014-12-18 17:42:32 -0800566 if (s->wbio != NULL && s->wbio != s->rbio) {
567 BIO_free_all(s->wbio);
568 }
Adam Langley95c29f32014-06-20 12:00:00 -0700569
Adam Langleyfcf25832014-12-18 17:42:32 -0800570 if (s->init_buf != NULL) {
571 BUF_MEM_free(s->init_buf);
572 }
Adam Langley95c29f32014-06-20 12:00:00 -0700573
Adam Langleyfcf25832014-12-18 17:42:32 -0800574 /* add extra stuff */
575 if (s->cipher_list != NULL) {
576 ssl_cipher_preference_list_free(s->cipher_list);
577 }
578 if (s->cipher_list_by_id != NULL) {
579 sk_SSL_CIPHER_free(s->cipher_list_by_id);
580 }
Adam Langley95c29f32014-06-20 12:00:00 -0700581
Adam Langleyfcf25832014-12-18 17:42:32 -0800582 if (s->session != NULL) {
583 ssl_clear_bad_session(s);
584 SSL_SESSION_free(s->session);
585 }
Adam Langley95c29f32014-06-20 12:00:00 -0700586
Adam Langleyfcf25832014-12-18 17:42:32 -0800587 ssl_clear_cipher_ctx(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700588
Adam Langleyfcf25832014-12-18 17:42:32 -0800589 if (s->cert != NULL) {
590 ssl_cert_free(s->cert);
591 }
Adam Langley0289c732014-06-20 12:00:00 -0700592
Adam Langleyfcf25832014-12-18 17:42:32 -0800593 if (s->tlsext_hostname) {
594 OPENSSL_free(s->tlsext_hostname);
595 }
596 if (s->initial_ctx) {
597 SSL_CTX_free(s->initial_ctx);
598 }
599 if (s->tlsext_ecpointformatlist) {
600 OPENSSL_free(s->tlsext_ecpointformatlist);
601 }
602 if (s->tlsext_ellipticcurvelist) {
603 OPENSSL_free(s->tlsext_ellipticcurvelist);
604 }
605 if (s->alpn_client_proto_list) {
606 OPENSSL_free(s->alpn_client_proto_list);
607 }
608 if (s->tlsext_channel_id_private) {
609 EVP_PKEY_free(s->tlsext_channel_id_private);
610 }
611 if (s->psk_identity_hint) {
612 OPENSSL_free(s->psk_identity_hint);
613 }
614 if (s->client_CA != NULL) {
615 sk_X509_NAME_pop_free(s->client_CA, X509_NAME_free);
616 }
617 if (s->next_proto_negotiated) {
618 OPENSSL_free(s->next_proto_negotiated);
619 }
620 if (s->srtp_profiles) {
621 sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);
622 }
Adam Langley95c29f32014-06-20 12:00:00 -0700623
Adam Langleyfcf25832014-12-18 17:42:32 -0800624 if (s->method != NULL) {
625 s->method->ssl_free(s);
626 }
627 if (s->ctx) {
628 SSL_CTX_free(s->ctx);
629 }
Adam Langley95c29f32014-06-20 12:00:00 -0700630
Adam Langleyfcf25832014-12-18 17:42:32 -0800631 OPENSSL_free(s);
632}
Adam Langley95c29f32014-06-20 12:00:00 -0700633
Adam Langleyfcf25832014-12-18 17:42:32 -0800634void SSL_set_bio(SSL *s, BIO *rbio, BIO *wbio) {
635 /* If the output buffering BIO is still in place, remove it. */
636 if (s->bbio != NULL) {
637 if (s->wbio == s->bbio) {
638 s->wbio = s->wbio->next_bio;
639 s->bbio->next_bio = NULL;
640 }
641 }
Adam Langley95c29f32014-06-20 12:00:00 -0700642
Adam Langleyfcf25832014-12-18 17:42:32 -0800643 if (s->rbio != NULL && s->rbio != rbio) {
644 BIO_free_all(s->rbio);
645 }
646 if (s->wbio != NULL && s->wbio != wbio && s->rbio != s->wbio) {
647 BIO_free_all(s->wbio);
648 }
649 s->rbio = rbio;
650 s->wbio = wbio;
651}
Adam Langley95c29f32014-06-20 12:00:00 -0700652
Adam Langleyfcf25832014-12-18 17:42:32 -0800653BIO *SSL_get_rbio(const SSL *s) { return s->rbio; }
Adam Langley95c29f32014-06-20 12:00:00 -0700654
Adam Langleyfcf25832014-12-18 17:42:32 -0800655BIO *SSL_get_wbio(const SSL *s) { return s->wbio; }
Adam Langley95c29f32014-06-20 12:00:00 -0700656
Adam Langleyfcf25832014-12-18 17:42:32 -0800657int SSL_get_fd(const SSL *s) { return SSL_get_rfd(s); }
Adam Langley95c29f32014-06-20 12:00:00 -0700658
Adam Langleyfcf25832014-12-18 17:42:32 -0800659int SSL_get_rfd(const SSL *s) {
660 int ret = -1;
661 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700662
Adam Langleyfcf25832014-12-18 17:42:32 -0800663 b = SSL_get_rbio(s);
664 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
665 if (r != NULL) {
666 BIO_get_fd(r, &ret);
667 }
668 return ret;
669}
Adam Langley95c29f32014-06-20 12:00:00 -0700670
Adam Langleyfcf25832014-12-18 17:42:32 -0800671int SSL_get_wfd(const SSL *s) {
672 int ret = -1;
673 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700674
Adam Langleyfcf25832014-12-18 17:42:32 -0800675 b = SSL_get_wbio(s);
676 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
677 if (r != NULL) {
678 BIO_get_fd(r, &ret);
679 }
Adam Langley95c29f32014-06-20 12:00:00 -0700680
Adam Langleyfcf25832014-12-18 17:42:32 -0800681 return ret;
682}
Adam Langley95c29f32014-06-20 12:00:00 -0700683
Adam Langleyfcf25832014-12-18 17:42:32 -0800684int SSL_set_fd(SSL *s, int fd) {
685 int ret = 0;
686 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700687
Adam Langleyfcf25832014-12-18 17:42:32 -0800688 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700689
Adam Langleyfcf25832014-12-18 17:42:32 -0800690 if (bio == NULL) {
691 OPENSSL_PUT_ERROR(SSL, SSL_set_fd, ERR_R_BUF_LIB);
692 goto err;
693 }
694 BIO_set_fd(bio, fd, BIO_NOCLOSE);
695 SSL_set_bio(s, bio, bio);
696 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700697
Adam Langley95c29f32014-06-20 12:00:00 -0700698err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800699 return ret;
700}
Adam Langley95c29f32014-06-20 12:00:00 -0700701
Adam Langleyfcf25832014-12-18 17:42:32 -0800702int SSL_set_wfd(SSL *s, int fd) {
703 int ret = 0;
704 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700705
Adam Langleyfcf25832014-12-18 17:42:32 -0800706 if (s->rbio == NULL || BIO_method_type(s->rbio) != BIO_TYPE_FD ||
707 (int)BIO_get_fd(s->rbio, NULL) != fd) {
708 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700709
Adam Langleyfcf25832014-12-18 17:42:32 -0800710 if (bio == NULL) {
711 OPENSSL_PUT_ERROR(SSL, SSL_set_wfd, ERR_R_BUF_LIB);
712 goto err;
713 }
714 BIO_set_fd(bio, fd, BIO_NOCLOSE);
715 SSL_set_bio(s, SSL_get_rbio(s), bio);
716 } else {
717 SSL_set_bio(s, SSL_get_rbio(s), SSL_get_rbio(s));
718 }
719
720 ret = 1;
721
Adam Langley95c29f32014-06-20 12:00:00 -0700722err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800723 return ret;
724}
Adam Langley95c29f32014-06-20 12:00:00 -0700725
Adam Langleyfcf25832014-12-18 17:42:32 -0800726int SSL_set_rfd(SSL *s, int fd) {
727 int ret = 0;
728 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700729
Adam Langleyfcf25832014-12-18 17:42:32 -0800730 if (s->wbio == NULL || BIO_method_type(s->wbio) != BIO_TYPE_FD ||
731 (int)BIO_get_fd(s->wbio, NULL) != fd) {
732 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700733
Adam Langleyfcf25832014-12-18 17:42:32 -0800734 if (bio == NULL) {
735 OPENSSL_PUT_ERROR(SSL, SSL_set_rfd, ERR_R_BUF_LIB);
736 goto err;
737 }
738 BIO_set_fd(bio, fd, BIO_NOCLOSE);
739 SSL_set_bio(s, bio, SSL_get_wbio(s));
740 } else {
741 SSL_set_bio(s, SSL_get_wbio(s), SSL_get_wbio(s));
742 }
743 ret = 1;
744
Adam Langley95c29f32014-06-20 12:00:00 -0700745err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800746 return ret;
747}
Adam Langley95c29f32014-06-20 12:00:00 -0700748
749/* return length of latest Finished message we sent, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -0800750size_t SSL_get_finished(const SSL *s, void *buf, size_t count) {
751 size_t ret = 0;
752
753 if (s->s3 != NULL) {
754 ret = s->s3->tmp.finish_md_len;
755 if (count > ret) {
756 count = ret;
757 }
758 memcpy(buf, s->s3->tmp.finish_md, count);
759 }
760
761 return ret;
762}
Adam Langley95c29f32014-06-20 12:00:00 -0700763
764/* return length of latest Finished message we expected, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -0800765size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count) {
766 size_t ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700767
Adam Langleyfcf25832014-12-18 17:42:32 -0800768 if (s->s3 != NULL) {
769 ret = s->s3->tmp.peer_finish_md_len;
770 if (count > ret) {
771 count = ret;
772 }
773 memcpy(buf, s->s3->tmp.peer_finish_md, count);
774 }
Adam Langley95c29f32014-06-20 12:00:00 -0700775
Adam Langleyfcf25832014-12-18 17:42:32 -0800776 return ret;
777}
Adam Langley95c29f32014-06-20 12:00:00 -0700778
Adam Langleyfcf25832014-12-18 17:42:32 -0800779int SSL_get_verify_mode(const SSL *s) { return s->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -0700780
Adam Langleyfcf25832014-12-18 17:42:32 -0800781int SSL_get_verify_depth(const SSL *s) {
782 return X509_VERIFY_PARAM_get_depth(s->param);
783}
Adam Langley95c29f32014-06-20 12:00:00 -0700784
Adam Langleyfcf25832014-12-18 17:42:32 -0800785int (*SSL_get_verify_callback(const SSL *s))(int, X509_STORE_CTX *) {
786 return s->verify_callback;
787}
Adam Langley95c29f32014-06-20 12:00:00 -0700788
Adam Langleyfcf25832014-12-18 17:42:32 -0800789int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) { return ctx->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -0700790
Adam Langleyfcf25832014-12-18 17:42:32 -0800791int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) {
792 return X509_VERIFY_PARAM_get_depth(ctx->param);
793}
Adam Langley95c29f32014-06-20 12:00:00 -0700794
Adam Langleyfcf25832014-12-18 17:42:32 -0800795int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int, X509_STORE_CTX *) {
796 return ctx->default_verify_callback;
797}
Adam Langley95c29f32014-06-20 12:00:00 -0700798
Adam Langleyfcf25832014-12-18 17:42:32 -0800799void SSL_set_verify(SSL *s, int mode,
800 int (*callback)(int ok, X509_STORE_CTX *ctx)) {
801 s->verify_mode = mode;
802 if (callback != NULL) {
803 s->verify_callback = callback;
804 }
805}
Adam Langley95c29f32014-06-20 12:00:00 -0700806
Adam Langleyfcf25832014-12-18 17:42:32 -0800807void SSL_set_verify_depth(SSL *s, int depth) {
808 X509_VERIFY_PARAM_set_depth(s->param, depth);
809}
Adam Langley95c29f32014-06-20 12:00:00 -0700810
Adam Langleyfcf25832014-12-18 17:42:32 -0800811void SSL_set_read_ahead(SSL *s, int yes) { s->read_ahead = yes; }
Adam Langley95c29f32014-06-20 12:00:00 -0700812
Adam Langleyfcf25832014-12-18 17:42:32 -0800813int SSL_get_read_ahead(const SSL *s) { return s->read_ahead; }
Adam Langley95c29f32014-06-20 12:00:00 -0700814
Adam Langleyfcf25832014-12-18 17:42:32 -0800815int SSL_pending(const SSL *s) {
816 /* SSL_pending cannot work properly if read-ahead is enabled
817 * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)), and it is
818 * impossible to fix since SSL_pending cannot report errors that may be
819 * observed while scanning the new data. (Note that SSL_pending() is often
820 * used as a boolean value, so we'd better not return -1.). */
821 return s->method->ssl_pending(s);
822}
Adam Langley95c29f32014-06-20 12:00:00 -0700823
Adam Langleyfcf25832014-12-18 17:42:32 -0800824X509 *SSL_get_peer_certificate(const SSL *s) {
825 X509 *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700826
Adam Langleyfcf25832014-12-18 17:42:32 -0800827 if (s == NULL || s->session == NULL) {
828 r = NULL;
829 } else {
830 r = s->session->peer;
831 }
Adam Langley95c29f32014-06-20 12:00:00 -0700832
Adam Langleyfcf25832014-12-18 17:42:32 -0800833 if (r == NULL) {
834 return NULL;
835 }
Adam Langley95c29f32014-06-20 12:00:00 -0700836
Adam Langleyfcf25832014-12-18 17:42:32 -0800837 return X509_up_ref(r);
838}
839
840STACK_OF(X509) * SSL_get_peer_cert_chain(const SSL *s) {
841 STACK_OF(X509) * r;
842
843 if (s == NULL || s->session == NULL || s->session->sess_cert == NULL) {
844 r = NULL;
845 } else {
846 r = s->session->sess_cert->cert_chain;
847 }
848
849 /* If we are a client, cert_chain includes the peer's own certificate; if we
850 * are a server, it does not. */
851 return r;
852}
Adam Langley95c29f32014-06-20 12:00:00 -0700853
Adam Langley95c29f32014-06-20 12:00:00 -0700854/* Fix this so it checks all the valid key/cert options */
Adam Langleyfcf25832014-12-18 17:42:32 -0800855int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
856 if (ctx == NULL || ctx->cert == NULL || ctx->cert->key->x509 == NULL) {
857 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key,
858 SSL_R_NO_CERTIFICATE_ASSIGNED);
859 return 0;
860 }
861
862 if (ctx->cert->key->privatekey == NULL) {
863 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key,
864 SSL_R_NO_PRIVATE_KEY_ASSIGNED);
865 return 0;
866 }
867
868 return X509_check_private_key(ctx->cert->key->x509,
869 ctx->cert->key->privatekey);
870}
Adam Langley95c29f32014-06-20 12:00:00 -0700871
872/* Fix this function so that it takes an optional type parameter */
Adam Langleyfcf25832014-12-18 17:42:32 -0800873int SSL_check_private_key(const SSL *ssl) {
874 if (ssl == NULL) {
875 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, ERR_R_PASSED_NULL_PARAMETER);
876 return 0;
877 }
Adam Langley95c29f32014-06-20 12:00:00 -0700878
Adam Langleyfcf25832014-12-18 17:42:32 -0800879 if (ssl->cert == NULL) {
880 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
881 SSL_R_NO_CERTIFICATE_ASSIGNED);
882 return 0;
883 }
Adam Langley95c29f32014-06-20 12:00:00 -0700884
Adam Langleyfcf25832014-12-18 17:42:32 -0800885 if (ssl->cert->key->x509 == NULL) {
886 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
887 SSL_R_NO_CERTIFICATE_ASSIGNED);
888 return 0;
889 }
David Benjamin0b145c22014-11-26 20:10:09 -0500890
Adam Langleyfcf25832014-12-18 17:42:32 -0800891 if (ssl->cert->key->privatekey == NULL) {
892 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
893 SSL_R_NO_PRIVATE_KEY_ASSIGNED);
894 return 0;
895 }
Adam Langley95c29f32014-06-20 12:00:00 -0700896
Adam Langleyfcf25832014-12-18 17:42:32 -0800897 return X509_check_private_key(ssl->cert->key->x509,
898 ssl->cert->key->privatekey);
899}
Adam Langley95c29f32014-06-20 12:00:00 -0700900
Adam Langleyfcf25832014-12-18 17:42:32 -0800901int SSL_accept(SSL *s) {
902 if (s->handshake_func == 0) {
903 /* Not properly initialized yet */
904 SSL_set_accept_state(s);
905 }
David Benjamin0b145c22014-11-26 20:10:09 -0500906
Adam Langleyfcf25832014-12-18 17:42:32 -0800907 if (s->handshake_func != s->method->ssl_accept) {
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +0100908 OPENSSL_PUT_ERROR(SSL, SSL_accept, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800909 return -1;
910 }
Adam Langley95c29f32014-06-20 12:00:00 -0700911
Adam Langleyfcf25832014-12-18 17:42:32 -0800912 return s->handshake_func(s);
913}
Adam Langley95c29f32014-06-20 12:00:00 -0700914
Adam Langleyfcf25832014-12-18 17:42:32 -0800915int SSL_connect(SSL *s) {
916 if (s->handshake_func == 0) {
917 /* Not properly initialized yet */
918 SSL_set_connect_state(s);
919 }
Adam Langley95c29f32014-06-20 12:00:00 -0700920
Adam Langleyfcf25832014-12-18 17:42:32 -0800921 if (s->handshake_func != s->method->ssl_connect) {
922 OPENSSL_PUT_ERROR(SSL, SSL_connect, ERR_R_INTERNAL_ERROR);
923 return -1;
924 }
Adam Langley95c29f32014-06-20 12:00:00 -0700925
Adam Langleyfcf25832014-12-18 17:42:32 -0800926 return s->handshake_func(s);
927}
Adam Langley95c29f32014-06-20 12:00:00 -0700928
Adam Langleyfcf25832014-12-18 17:42:32 -0800929long SSL_get_default_timeout(const SSL *s) {
930 return SSL_DEFAULT_SESSION_TIMEOUT;
931}
Adam Langley95c29f32014-06-20 12:00:00 -0700932
Adam Langleyfcf25832014-12-18 17:42:32 -0800933int SSL_read(SSL *s, void *buf, int num) {
934 if (s->handshake_func == 0) {
935 OPENSSL_PUT_ERROR(SSL, SSL_read, SSL_R_UNINITIALIZED);
936 return -1;
937 }
Adam Langley95c29f32014-06-20 12:00:00 -0700938
Adam Langleyfcf25832014-12-18 17:42:32 -0800939 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
940 s->rwstate = SSL_NOTHING;
941 return 0;
942 }
Adam Langley95c29f32014-06-20 12:00:00 -0700943
Adam Langleyfcf25832014-12-18 17:42:32 -0800944 return s->method->ssl_read(s, buf, num);
945}
Adam Langley95c29f32014-06-20 12:00:00 -0700946
Adam Langleyfcf25832014-12-18 17:42:32 -0800947int SSL_peek(SSL *s, void *buf, int num) {
948 if (s->handshake_func == 0) {
949 OPENSSL_PUT_ERROR(SSL, SSL_peek, SSL_R_UNINITIALIZED);
950 return -1;
951 }
Adam Langley95c29f32014-06-20 12:00:00 -0700952
Adam Langleyfcf25832014-12-18 17:42:32 -0800953 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
954 return 0;
955 }
Adam Langley95c29f32014-06-20 12:00:00 -0700956
Adam Langleyfcf25832014-12-18 17:42:32 -0800957 return s->method->ssl_peek(s, buf, num);
958}
Adam Langley95c29f32014-06-20 12:00:00 -0700959
Adam Langleyfcf25832014-12-18 17:42:32 -0800960int SSL_write(SSL *s, const void *buf, int num) {
961 if (s->handshake_func == 0) {
962 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_UNINITIALIZED);
963 return -1;
964 }
Adam Langley95c29f32014-06-20 12:00:00 -0700965
Adam Langleyfcf25832014-12-18 17:42:32 -0800966 if (s->shutdown & SSL_SENT_SHUTDOWN) {
967 s->rwstate = SSL_NOTHING;
968 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_PROTOCOL_IS_SHUTDOWN);
969 return -1;
970 }
Adam Langley95c29f32014-06-20 12:00:00 -0700971
Adam Langleyfcf25832014-12-18 17:42:32 -0800972 return s->method->ssl_write(s, buf, num);
973}
Adam Langley95c29f32014-06-20 12:00:00 -0700974
Adam Langleyfcf25832014-12-18 17:42:32 -0800975int SSL_shutdown(SSL *s) {
976 /* Note that this function behaves differently from what one might expect.
977 * Return values are 0 for no success (yet), 1 for success; but calling it
978 * once is usually not enough, even if blocking I/O is used (see
979 * ssl3_shutdown). */
Adam Langley95c29f32014-06-20 12:00:00 -0700980
Adam Langleyfcf25832014-12-18 17:42:32 -0800981 if (s->handshake_func == 0) {
982 OPENSSL_PUT_ERROR(SSL, SSL_shutdown, SSL_R_UNINITIALIZED);
983 return -1;
984 }
Adam Langley95c29f32014-06-20 12:00:00 -0700985
Adam Langleyfcf25832014-12-18 17:42:32 -0800986 if (!SSL_in_init(s)) {
987 return s->method->ssl_shutdown(s);
988 }
Adam Langley95c29f32014-06-20 12:00:00 -0700989
Adam Langleyfcf25832014-12-18 17:42:32 -0800990 return 1;
991}
Adam Langley95c29f32014-06-20 12:00:00 -0700992
Adam Langleyfcf25832014-12-18 17:42:32 -0800993int SSL_renegotiate(SSL *s) {
David Benjamin2cdace92015-01-24 13:30:02 -0500994 if (SSL_IS_DTLS(s)) {
995 /* Renegotiation is not supported for DTLS. */
996 OPENSSL_PUT_ERROR(SSL, SSL_renegotiate, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
997 return 0;
998 }
999
Adam Langleyfcf25832014-12-18 17:42:32 -08001000 if (s->renegotiate == 0) {
1001 s->renegotiate = 1;
1002 }
Adam Langley95c29f32014-06-20 12:00:00 -07001003
Adam Langleyfcf25832014-12-18 17:42:32 -08001004 s->new_session = 1;
1005 return s->method->ssl_renegotiate(s);
1006}
Adam Langley95c29f32014-06-20 12:00:00 -07001007
Adam Langleyfcf25832014-12-18 17:42:32 -08001008int SSL_renegotiate_pending(SSL *s) {
1009 /* becomes true when negotiation is requested; false again once a handshake
1010 * has finished */
1011 return s->renegotiate != 0;
1012}
Adam Langley95c29f32014-06-20 12:00:00 -07001013
Adam Langleyfcf25832014-12-18 17:42:32 -08001014long SSL_ctrl(SSL *s, int cmd, long larg, void *parg) {
1015 long l;
Adam Langley95c29f32014-06-20 12:00:00 -07001016
Adam Langleyfcf25832014-12-18 17:42:32 -08001017 switch (cmd) {
1018 case SSL_CTRL_GET_READ_AHEAD:
1019 return s->read_ahead;
Adam Langley95c29f32014-06-20 12:00:00 -07001020
Adam Langleyfcf25832014-12-18 17:42:32 -08001021 case SSL_CTRL_SET_READ_AHEAD:
1022 l = s->read_ahead;
1023 s->read_ahead = larg;
1024 return l;
Adam Langley95c29f32014-06-20 12:00:00 -07001025
Adam Langleyfcf25832014-12-18 17:42:32 -08001026 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1027 s->msg_callback_arg = parg;
1028 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001029
Adam Langleyfcf25832014-12-18 17:42:32 -08001030 case SSL_CTRL_OPTIONS:
1031 return s->options |= larg;
Adam Langley95c29f32014-06-20 12:00:00 -07001032
Adam Langleyfcf25832014-12-18 17:42:32 -08001033 case SSL_CTRL_CLEAR_OPTIONS:
1034 return s->options &= ~larg;
Adam Langley95c29f32014-06-20 12:00:00 -07001035
Adam Langleyfcf25832014-12-18 17:42:32 -08001036 case SSL_CTRL_MODE:
1037 return s->mode |= larg;
Adam Langley95c29f32014-06-20 12:00:00 -07001038
Adam Langleyfcf25832014-12-18 17:42:32 -08001039 case SSL_CTRL_CLEAR_MODE:
1040 return s->mode &= ~larg;
Adam Langley95c29f32014-06-20 12:00:00 -07001041
Adam Langleyfcf25832014-12-18 17:42:32 -08001042 case SSL_CTRL_GET_MAX_CERT_LIST:
1043 return s->max_cert_list;
Adam Langley95c29f32014-06-20 12:00:00 -07001044
Adam Langleyfcf25832014-12-18 17:42:32 -08001045 case SSL_CTRL_SET_MAX_CERT_LIST:
1046 l = s->max_cert_list;
1047 s->max_cert_list = larg;
1048 return l;
Adam Langley95c29f32014-06-20 12:00:00 -07001049
Adam Langleyfcf25832014-12-18 17:42:32 -08001050 case SSL_CTRL_SET_MTU:
1051 if (larg < (long)dtls1_min_mtu()) {
1052 return 0;
1053 }
1054 if (SSL_IS_DTLS(s)) {
1055 s->d1->mtu = larg;
1056 return larg;
1057 }
1058 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001059
Adam Langleyfcf25832014-12-18 17:42:32 -08001060 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1061 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH) {
1062 return 0;
1063 }
1064 s->max_send_fragment = larg;
1065 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001066
Adam Langleyfcf25832014-12-18 17:42:32 -08001067 case SSL_CTRL_GET_RI_SUPPORT:
1068 if (s->s3) {
1069 return s->s3->send_connection_binding;
1070 }
1071 return 0;
1072
Adam Langleyfcf25832014-12-18 17:42:32 -08001073 case SSL_CTRL_GET_RAW_CIPHERLIST:
1074 if (parg) {
1075 if (s->cert->ciphers_raw == NULL) {
1076 return 0;
1077 }
1078 *(uint8_t **)parg = s->cert->ciphers_raw;
1079 return (int)s->cert->ciphers_rawlen;
1080 }
1081
1082 /* Passing a NULL |parg| returns the size of a single
1083 * cipher suite value. */
1084 return 2;
1085
1086 default:
1087 return s->method->ssl_ctrl(s, cmd, larg, parg);
1088 }
1089}
1090
1091long SSL_callback_ctrl(SSL *s, int cmd, void (*fp)(void)) {
1092 switch (cmd) {
1093 case SSL_CTRL_SET_MSG_CALLBACK:
1094 s->msg_callback =
1095 (void (*)(int write_p, int version, int content_type, const void *buf,
1096 size_t len, SSL *ssl, void *arg))(fp);
1097 return 1;
1098
1099 default:
1100 return s->method->ssl_callback_ctrl(s, cmd, fp);
1101 }
1102}
1103
1104LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) { return ctx->sessions; }
1105
1106long SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg) {
1107 long l;
1108
1109 switch (cmd) {
1110 case SSL_CTRL_GET_READ_AHEAD:
1111 return ctx->read_ahead;
1112
1113 case SSL_CTRL_SET_READ_AHEAD:
1114 l = ctx->read_ahead;
1115 ctx->read_ahead = larg;
1116 return l;
1117
1118 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1119 ctx->msg_callback_arg = parg;
1120 return 1;
1121
1122 case SSL_CTRL_GET_MAX_CERT_LIST:
1123 return ctx->max_cert_list;
1124
1125 case SSL_CTRL_SET_MAX_CERT_LIST:
1126 l = ctx->max_cert_list;
1127 ctx->max_cert_list = larg;
1128 return l;
1129
1130 case SSL_CTRL_SET_SESS_CACHE_SIZE:
1131 l = ctx->session_cache_size;
1132 ctx->session_cache_size = larg;
1133 return l;
1134
1135 case SSL_CTRL_GET_SESS_CACHE_SIZE:
1136 return ctx->session_cache_size;
1137
1138 case SSL_CTRL_SET_SESS_CACHE_MODE:
1139 l = ctx->session_cache_mode;
1140 ctx->session_cache_mode = larg;
1141 return l;
1142
1143 case SSL_CTRL_GET_SESS_CACHE_MODE:
1144 return ctx->session_cache_mode;
1145
1146 case SSL_CTRL_SESS_NUMBER:
1147 return lh_SSL_SESSION_num_items(ctx->sessions);
1148
1149 case SSL_CTRL_SESS_CONNECT:
Adam Langleyfcf25832014-12-18 17:42:32 -08001150 case SSL_CTRL_SESS_CONNECT_GOOD:
Adam Langleyfcf25832014-12-18 17:42:32 -08001151 case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001152 case SSL_CTRL_SESS_ACCEPT:
Adam Langleyfcf25832014-12-18 17:42:32 -08001153 case SSL_CTRL_SESS_ACCEPT_GOOD:
Adam Langleyfcf25832014-12-18 17:42:32 -08001154 case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001155 case SSL_CTRL_SESS_HIT:
Adam Langleyfcf25832014-12-18 17:42:32 -08001156 case SSL_CTRL_SESS_CB_HIT:
Adam Langleyfcf25832014-12-18 17:42:32 -08001157 case SSL_CTRL_SESS_MISSES:
Adam Langleyfcf25832014-12-18 17:42:32 -08001158 case SSL_CTRL_SESS_TIMEOUTS:
Adam Langleyfcf25832014-12-18 17:42:32 -08001159 case SSL_CTRL_SESS_CACHE_FULL:
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001160 /* Statistics are no longer supported.
1161 * TODO(davidben): Try to remove the accessors altogether. */
1162 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001163
1164 case SSL_CTRL_OPTIONS:
1165 return ctx->options |= larg;
1166
1167 case SSL_CTRL_CLEAR_OPTIONS:
1168 return ctx->options &= ~larg;
1169
1170 case SSL_CTRL_MODE:
1171 return ctx->mode |= larg;
1172
1173 case SSL_CTRL_CLEAR_MODE:
1174 return ctx->mode &= ~larg;
1175
1176 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1177 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH) {
1178 return 0;
1179 }
1180 ctx->max_send_fragment = larg;
1181 return 1;
1182
Adam Langleyfcf25832014-12-18 17:42:32 -08001183 default:
1184 return ctx->method->ssl_ctx_ctrl(ctx, cmd, larg, parg);
1185 }
1186}
1187
1188long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void)) {
1189 switch (cmd) {
1190 case SSL_CTRL_SET_MSG_CALLBACK:
1191 ctx->msg_callback =
1192 (void (*)(int write_p, int version, int content_type, const void *buf,
1193 size_t len, SSL *ssl, void *arg))(fp);
1194 return 1;
1195
1196 default:
1197 return ctx->method->ssl_ctx_callback_ctrl(ctx, cmd, fp);
1198 }
1199}
1200
1201int ssl_cipher_id_cmp(const void *in_a, const void *in_b) {
1202 long l;
1203 const SSL_CIPHER *a = in_a;
1204 const SSL_CIPHER *b = in_b;
1205 const long a_id = a->id;
1206 const long b_id = b->id;
1207
1208 l = a_id - b_id;
1209 if (l == 0L) {
1210 return 0;
1211 } else {
1212 return (l > 0) ? 1 : -1;
1213 }
1214}
1215
1216int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **ap, const SSL_CIPHER **bp) {
1217 long l;
1218 const long a_id = (*ap)->id;
1219 const long b_id = (*bp)->id;
1220
1221 l = a_id - b_id;
1222 if (l == 0) {
1223 return 0;
1224 } else {
1225 return (l > 0) ? 1 : -1;
1226 }
1227}
1228
1229/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001230 * preference */
Adam Langleyfcf25832014-12-18 17:42:32 -08001231STACK_OF(SSL_CIPHER) * SSL_get_ciphers(const SSL *s) {
1232 if (s == NULL) {
1233 return NULL;
1234 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001235
Adam Langleyfcf25832014-12-18 17:42:32 -08001236 if (s->cipher_list != NULL) {
1237 return s->cipher_list->ciphers;
1238 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001239
Adam Langleyfcf25832014-12-18 17:42:32 -08001240 if (s->version >= TLS1_1_VERSION && s->ctx != NULL &&
1241 s->ctx->cipher_list_tls11 != NULL) {
1242 return s->ctx->cipher_list_tls11->ciphers;
1243 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001244
Adam Langleyfcf25832014-12-18 17:42:32 -08001245 if (s->ctx != NULL && s->ctx->cipher_list != NULL) {
1246 return s->ctx->cipher_list->ciphers;
1247 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001248
Adam Langleyfcf25832014-12-18 17:42:32 -08001249 return NULL;
1250}
Adam Langley95c29f32014-06-20 12:00:00 -07001251
Adam Langleyfcf25832014-12-18 17:42:32 -08001252/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001253 * algorithm id */
Adam Langleyfcf25832014-12-18 17:42:32 -08001254STACK_OF(SSL_CIPHER) * ssl_get_ciphers_by_id(SSL *s) {
1255 if (s == NULL) {
1256 return NULL;
1257 }
Adam Langley95c29f32014-06-20 12:00:00 -07001258
Adam Langleyfcf25832014-12-18 17:42:32 -08001259 if (s->cipher_list_by_id != NULL) {
1260 return s->cipher_list_by_id;
1261 }
Adam Langley95c29f32014-06-20 12:00:00 -07001262
Adam Langleyfcf25832014-12-18 17:42:32 -08001263 if (s->ctx != NULL && s->ctx->cipher_list_by_id != NULL) {
1264 return s->ctx->cipher_list_by_id;
1265 }
Adam Langley95c29f32014-06-20 12:00:00 -07001266
Adam Langleyfcf25832014-12-18 17:42:32 -08001267 return NULL;
1268}
Adam Langley95c29f32014-06-20 12:00:00 -07001269
Adam Langleyfcf25832014-12-18 17:42:32 -08001270/* The old interface to get the same thing as SSL_get_ciphers() */
1271const char *SSL_get_cipher_list(const SSL *s, int n) {
1272 const SSL_CIPHER *c;
1273 STACK_OF(SSL_CIPHER) * sk;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001274
Adam Langleyfcf25832014-12-18 17:42:32 -08001275 if (s == NULL) {
1276 return NULL;
1277 }
Adam Langley95c29f32014-06-20 12:00:00 -07001278
Adam Langleyfcf25832014-12-18 17:42:32 -08001279 sk = SSL_get_ciphers(s);
1280 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
1281 return NULL;
1282 }
Adam Langley95c29f32014-06-20 12:00:00 -07001283
Adam Langleyfcf25832014-12-18 17:42:32 -08001284 c = sk_SSL_CIPHER_value(sk, n);
1285 if (c == NULL) {
1286 return NULL;
1287 }
Adam Langley95c29f32014-06-20 12:00:00 -07001288
Adam Langleyfcf25832014-12-18 17:42:32 -08001289 return c->name;
1290}
David Benjamin5491e3f2014-09-29 19:33:09 -04001291
Adam Langleyfcf25832014-12-18 17:42:32 -08001292/* specify the ciphers to be used by default by the SSL_CTX */
1293int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
1294 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001295
Adam Langleyfcf25832014-12-18 17:42:32 -08001296 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001297 &ctx->cipher_list_by_id, str);
Adam Langleyfcf25832014-12-18 17:42:32 -08001298 /* ssl_create_cipher_list may return an empty stack if it was unable to find
1299 * a cipher matching the given rule string (for example if the rule string
1300 * specifies a cipher which has been disabled). This is not an error as far
1301 * as ssl_create_cipher_list is concerned, and hence ctx->cipher_list and
1302 * ctx->cipher_list_by_id has been updated. */
1303 if (sk == NULL) {
1304 return 0;
1305 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1306 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1307 return 0;
1308 }
Adam Langley95c29f32014-06-20 12:00:00 -07001309
Adam Langleyfcf25832014-12-18 17:42:32 -08001310 return 1;
1311}
David Benjamin39482a12014-07-20 13:30:15 -04001312
Adam Langleyfcf25832014-12-18 17:42:32 -08001313int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str) {
1314 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001315
David Benjamin71f07942015-04-08 02:36:59 -04001316 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str);
Adam Langleyfcf25832014-12-18 17:42:32 -08001317 if (sk == NULL) {
1318 return 0;
1319 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1320 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list_tls11,
1321 SSL_R_NO_CIPHER_MATCH);
1322 return 0;
1323 }
David Benjamin9f2c0d72014-10-21 22:00:19 -04001324
Adam Langleyfcf25832014-12-18 17:42:32 -08001325 return 1;
1326}
Adam Langley95c29f32014-06-20 12:00:00 -07001327
Adam Langleyfcf25832014-12-18 17:42:32 -08001328/* specify the ciphers to be used by the SSL */
1329int SSL_set_cipher_list(SSL *s, const char *str) {
1330 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001331
Adam Langleyfcf25832014-12-18 17:42:32 -08001332 sk = ssl_create_cipher_list(s->ctx->method, &s->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001333 &s->cipher_list_by_id, str);
David Benjamin39482a12014-07-20 13:30:15 -04001334
Adam Langleyfcf25832014-12-18 17:42:32 -08001335 /* see comment in SSL_CTX_set_cipher_list */
1336 if (sk == NULL) {
1337 return 0;
1338 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1339 OPENSSL_PUT_ERROR(SSL, SSL_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1340 return 0;
1341 }
David Benjamin39482a12014-07-20 13:30:15 -04001342
Adam Langleyfcf25832014-12-18 17:42:32 -08001343 return 1;
1344}
Adam Langley95c29f32014-06-20 12:00:00 -07001345
Adam Langleyfcf25832014-12-18 17:42:32 -08001346int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, uint8_t *p) {
1347 size_t i;
1348 const SSL_CIPHER *c;
1349 CERT *ct = s->cert;
1350 uint8_t *q;
1351 /* Set disabled masks for this session */
1352 ssl_set_client_disabled(s);
Adam Langley29707792014-06-20 12:00:00 -07001353
Adam Langleyfcf25832014-12-18 17:42:32 -08001354 if (sk == NULL) {
1355 return 0;
1356 }
1357 q = p;
Adam Langley95c29f32014-06-20 12:00:00 -07001358
Adam Langleyfcf25832014-12-18 17:42:32 -08001359 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
1360 c = sk_SSL_CIPHER_value(sk, i);
1361 /* Skip disabled ciphers */
1362 if (c->algorithm_ssl & ct->mask_ssl ||
1363 c->algorithm_mkey & ct->mask_k ||
1364 c->algorithm_auth & ct->mask_a) {
1365 continue;
1366 }
1367 s2n(ssl3_get_cipher_value(c), p);
1368 }
1369
1370 /* If all ciphers were disabled, return the error to the caller. */
1371 if (p == q) {
1372 return 0;
1373 }
1374
1375 /* Add SCSVs. */
1376 if (!s->renegotiate) {
1377 s2n(SSL3_CK_SCSV & 0xffff, p);
1378 }
1379
Adam Langley5f0efe02015-02-20 13:03:16 -08001380 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001381 s2n(SSL3_CK_FALLBACK_SCSV & 0xffff, p);
1382 }
1383
1384 return p - q;
1385}
1386
1387STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs) {
1388 CBS cipher_suites = *cbs;
1389 const SSL_CIPHER *c;
1390 STACK_OF(SSL_CIPHER) * sk;
1391
1392 if (s->s3) {
1393 s->s3->send_connection_binding = 0;
1394 }
1395
1396 if (CBS_len(&cipher_suites) % 2 != 0) {
1397 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1398 SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
1399 return NULL;
1400 }
1401
1402 sk = sk_SSL_CIPHER_new_null();
1403 if (sk == NULL) {
1404 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1405 goto err;
1406 }
1407
1408 if (!CBS_stow(&cipher_suites, &s->cert->ciphers_raw,
1409 &s->cert->ciphers_rawlen)) {
1410 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1411 goto err;
1412 }
1413
1414 while (CBS_len(&cipher_suites) > 0) {
1415 uint16_t cipher_suite;
1416
1417 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
1418 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_INTERNAL_ERROR);
1419 goto err;
1420 }
1421
1422 /* Check for SCSV. */
1423 if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff)) {
1424 /* SCSV is fatal if renegotiating. */
1425 if (s->renegotiate) {
1426 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1427 SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1428 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1429 goto err;
1430 }
1431 s->s3->send_connection_binding = 1;
1432 continue;
1433 }
1434
1435 /* Check for FALLBACK_SCSV. */
1436 if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff)) {
1437 uint16_t max_version = ssl3_get_max_server_version(s);
1438 if (SSL_IS_DTLS(s) ? (uint16_t)s->version > max_version
1439 : (uint16_t)s->version < max_version) {
1440 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1441 SSL_R_INAPPROPRIATE_FALLBACK);
1442 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
1443 goto err;
1444 }
1445 continue;
1446 }
1447
1448 c = ssl3_get_cipher_by_value(cipher_suite);
1449 if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
1450 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1451 goto err;
1452 }
1453 }
1454
1455 return sk;
David Benjamin9f2c0d72014-10-21 22:00:19 -04001456
Adam Langley95c29f32014-06-20 12:00:00 -07001457err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001458 if (sk != NULL) {
1459 sk_SSL_CIPHER_free(sk);
1460 }
1461 return NULL;
1462}
Adam Langley95c29f32014-06-20 12:00:00 -07001463
1464
Adam Langleyfcf25832014-12-18 17:42:32 -08001465/* return a servername extension value if provided in Client Hello, or NULL. So
1466 * far, only host_name types are defined (RFC 3546). */
1467const char *SSL_get_servername(const SSL *s, const int type) {
1468 if (type != TLSEXT_NAMETYPE_host_name) {
1469 return NULL;
1470 }
Adam Langley95c29f32014-06-20 12:00:00 -07001471
Adam Langleyfcf25832014-12-18 17:42:32 -08001472 return s->session && !s->tlsext_hostname ? s->session->tlsext_hostname
1473 : s->tlsext_hostname;
1474}
Adam Langley95c29f32014-06-20 12:00:00 -07001475
Adam Langleyfcf25832014-12-18 17:42:32 -08001476int SSL_get_servername_type(const SSL *s) {
1477 if (s->session &&
1478 (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname)) {
1479 return TLSEXT_NAMETYPE_host_name;
1480 }
Adam Langley95c29f32014-06-20 12:00:00 -07001481
Adam Langleyfcf25832014-12-18 17:42:32 -08001482 return -1;
1483}
Adam Langley95c29f32014-06-20 12:00:00 -07001484
Adam Langleyfcf25832014-12-18 17:42:32 -08001485void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
1486 ctx->signed_cert_timestamps_enabled = 1;
1487}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001488
Adam Langleyfcf25832014-12-18 17:42:32 -08001489int SSL_enable_signed_cert_timestamps(SSL *ssl) {
1490 ssl->signed_cert_timestamps_enabled = 1;
1491 return 1;
1492}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001493
Adam Langleyfcf25832014-12-18 17:42:32 -08001494void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
1495 ctx->ocsp_stapling_enabled = 1;
1496}
David Benjamin6c7aed02014-08-27 16:42:38 -04001497
Adam Langleyfcf25832014-12-18 17:42:32 -08001498int SSL_enable_ocsp_stapling(SSL *ssl) {
1499 ssl->ocsp_stapling_enabled = 1;
1500 return 1;
1501}
David Benjamin6c7aed02014-08-27 16:42:38 -04001502
Adam Langleyfcf25832014-12-18 17:42:32 -08001503void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
1504 size_t *out_len) {
1505 SSL_SESSION *session = ssl->session;
Adam Langley3cb50e02014-08-26 14:00:31 -07001506
Adam Langleyfcf25832014-12-18 17:42:32 -08001507 *out_len = 0;
1508 *out = NULL;
1509 if (ssl->server || !session || !session->tlsext_signed_cert_timestamp_list) {
1510 return;
1511 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001512
Adam Langleyfcf25832014-12-18 17:42:32 -08001513 *out = session->tlsext_signed_cert_timestamp_list;
1514 *out_len = session->tlsext_signed_cert_timestamp_list_length;
1515}
David Benjamin6c7aed02014-08-27 16:42:38 -04001516
Adam Langleyfcf25832014-12-18 17:42:32 -08001517void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
1518 size_t *out_len) {
1519 SSL_SESSION *session = ssl->session;
1520
1521 *out_len = 0;
1522 *out = NULL;
1523 if (ssl->server || !session || !session->ocsp_response) {
1524 return;
1525 }
1526 *out = session->ocsp_response;
1527 *out_len = session->ocsp_response_length;
1528}
David Benjamin6c7aed02014-08-27 16:42:38 -04001529
Adam Langley95c29f32014-06-20 12:00:00 -07001530/* SSL_select_next_proto implements the standard protocol selection. It is
1531 * expected that this function is called from the callback set by
1532 * SSL_CTX_set_next_proto_select_cb.
1533 *
1534 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1535 * strings. The length byte itself is not included in the length. A byte
1536 * string of length 0 is invalid. No byte string may be truncated.
1537 *
1538 * The current, but experimental algorithm for selecting the protocol is:
1539 *
1540 * 1) If the server doesn't support NPN then this is indicated to the
1541 * callback. In this case, the client application has to abort the connection
1542 * or have a default application level protocol.
1543 *
1544 * 2) If the server supports NPN, but advertises an empty list then the
1545 * client selects the first protcol in its list, but indicates via the
1546 * API that this fallback case was enacted.
1547 *
1548 * 3) Otherwise, the client finds the first protocol in the server's list
1549 * that it supports and selects this protocol. This is because it's
1550 * assumed that the server has better information about which protocol
1551 * a client should use.
1552 *
1553 * 4) If the client doesn't support any of the server's advertised
1554 * protocols, then this is treated the same as case 2.
1555 *
1556 * It returns either
1557 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1558 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1559 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001560int SSL_select_next_proto(uint8_t **out, uint8_t *outlen, const uint8_t *server,
1561 unsigned int server_len, const uint8_t *client,
1562 unsigned int client_len) {
1563 unsigned int i, j;
1564 const uint8_t *result;
1565 int status = OPENSSL_NPN_UNSUPPORTED;
Adam Langley95c29f32014-06-20 12:00:00 -07001566
Adam Langleyfcf25832014-12-18 17:42:32 -08001567 /* For each protocol in server preference order, see if we support it. */
1568 for (i = 0; i < server_len;) {
1569 for (j = 0; j < client_len;) {
1570 if (server[i] == client[j] &&
1571 memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
1572 /* We found a match */
1573 result = &server[i];
1574 status = OPENSSL_NPN_NEGOTIATED;
1575 goto found;
1576 }
1577 j += client[j];
1578 j++;
1579 }
1580 i += server[i];
1581 i++;
1582 }
Adam Langley95c29f32014-06-20 12:00:00 -07001583
Adam Langleyfcf25832014-12-18 17:42:32 -08001584 /* There's no overlap between our protocols and the server's list. */
1585 result = client;
1586 status = OPENSSL_NPN_NO_OVERLAP;
Adam Langley95c29f32014-06-20 12:00:00 -07001587
Adam Langleyfcf25832014-12-18 17:42:32 -08001588found:
1589 *out = (uint8_t *)result + 1;
1590 *outlen = result[0];
1591 return status;
1592}
Adam Langley95c29f32014-06-20 12:00:00 -07001593
Adam Langley95c29f32014-06-20 12:00:00 -07001594/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
1595 * requested protocol for this connection and returns 0. If the client didn't
1596 * request any protocol, then *data is set to NULL.
1597 *
1598 * Note that the client can request any protocol it chooses. The value returned
1599 * from this function need not be a member of the list of supported protocols
Adam Langleyfcf25832014-12-18 17:42:32 -08001600 * provided by the callback. */
1601void SSL_get0_next_proto_negotiated(const SSL *s, const uint8_t **data,
1602 unsigned *len) {
1603 *data = s->next_proto_negotiated;
1604 if (!*data) {
1605 *len = 0;
1606 } else {
1607 *len = s->next_proto_negotiated_len;
1608 }
Adam Langley95c29f32014-06-20 12:00:00 -07001609}
1610
1611/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
1612 * TLS server needs a list of supported protocols for Next Protocol
1613 * Negotiation. The returned list must be in wire format. The list is returned
1614 * by setting |out| to point to it and |outlen| to its length. This memory will
1615 * not be modified, but one should assume that the SSL* keeps a reference to
1616 * it.
1617 *
Adam Langleyfcf25832014-12-18 17:42:32 -08001618 * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise.
1619 * Otherwise, no such extension will be included in the ServerHello. */
1620void SSL_CTX_set_next_protos_advertised_cb(
1621 SSL_CTX *ctx,
1622 int (*cb)(SSL *ssl, const uint8_t **out, unsigned int *outlen, void *arg),
1623 void *arg) {
1624 ctx->next_protos_advertised_cb = cb;
1625 ctx->next_protos_advertised_cb_arg = arg;
1626}
Adam Langley95c29f32014-06-20 12:00:00 -07001627
1628/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1629 * client needs to select a protocol from the server's provided list. |out|
1630 * must be set to point to the selected protocol (which may be within |in|).
1631 * The length of the protocol name must be written into |outlen|. The server's
1632 * advertised protocols are provided in |in| and |inlen|. The callback can
1633 * assume that |in| is syntactically valid.
1634 *
1635 * The client must select a protocol. It is fatal to the connection if this
1636 * callback returns a value other than SSL_TLSEXT_ERR_OK.
1637 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001638void SSL_CTX_set_next_proto_select_cb(
1639 SSL_CTX *ctx, int (*cb)(SSL *s, uint8_t **out, uint8_t *outlen,
1640 const uint8_t *in, unsigned int inlen, void *arg),
1641 void *arg) {
1642 ctx->next_proto_select_cb = cb;
1643 ctx->next_proto_select_cb_arg = arg;
1644}
Adam Langley95c29f32014-06-20 12:00:00 -07001645
Adam Langleyfcf25832014-12-18 17:42:32 -08001646int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
1647 unsigned protos_len) {
1648 if (ctx->alpn_client_proto_list) {
1649 OPENSSL_free(ctx->alpn_client_proto_list);
1650 }
Adam Langley95c29f32014-06-20 12:00:00 -07001651
Adam Langleyfcf25832014-12-18 17:42:32 -08001652 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1653 if (!ctx->alpn_client_proto_list) {
1654 return 1;
1655 }
1656 ctx->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001657
Adam Langleyfcf25832014-12-18 17:42:32 -08001658 return 0;
1659}
Adam Langley95c29f32014-06-20 12:00:00 -07001660
Adam Langleyfcf25832014-12-18 17:42:32 -08001661int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
1662 if (ssl->alpn_client_proto_list) {
1663 OPENSSL_free(ssl->alpn_client_proto_list);
1664 }
Adam Langley95c29f32014-06-20 12:00:00 -07001665
Adam Langleyfcf25832014-12-18 17:42:32 -08001666 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1667 if (!ssl->alpn_client_proto_list) {
1668 return 1;
1669 }
1670 ssl->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001671
Adam Langleyfcf25832014-12-18 17:42:32 -08001672 return 0;
1673}
Adam Langley95c29f32014-06-20 12:00:00 -07001674
1675/* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
1676 * during ClientHello processing in order to select an ALPN protocol from the
1677 * client's list of offered protocols. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001678void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
1679 int (*cb)(SSL *ssl, const uint8_t **out,
1680 uint8_t *outlen, const uint8_t *in,
1681 unsigned int inlen, void *arg),
1682 void *arg) {
1683 ctx->alpn_select_cb = cb;
1684 ctx->alpn_select_cb_arg = arg;
1685}
Adam Langley95c29f32014-06-20 12:00:00 -07001686
1687/* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
1688 * On return it sets |*data| to point to |*len| bytes of protocol name (not
1689 * including the leading length-prefix byte). If the server didn't respond with
1690 * a negotiated protocol then |*len| will be zero. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001691void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **data,
1692 unsigned *len) {
1693 *data = NULL;
1694 if (ssl->s3) {
1695 *data = ssl->s3->alpn_selected;
1696 }
1697 if (*data == NULL) {
1698 *len = 0;
1699 } else {
1700 *len = ssl->s3->alpn_selected_len;
1701 }
1702}
Adam Langley95c29f32014-06-20 12:00:00 -07001703
David Benjamincfd248b2015-04-03 11:02:24 -04001704int SSL_export_keying_material(SSL *s, uint8_t *out, size_t out_len,
1705 const char *label, size_t label_len,
1706 const uint8_t *context, size_t context_len,
1707 int use_context) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001708 if (s->version < TLS1_VERSION) {
David Benjaminc565ebb2015-04-03 04:06:36 -04001709 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001710 }
Adam Langley95c29f32014-06-20 12:00:00 -07001711
David Benjamincfd248b2015-04-03 11:02:24 -04001712 return s->enc_method->export_keying_material(
1713 s, out, out_len, label, label_len, context, context_len, use_context);
Adam Langleyfcf25832014-12-18 17:42:32 -08001714}
Adam Langley95c29f32014-06-20 12:00:00 -07001715
Adam Langleyfcf25832014-12-18 17:42:32 -08001716static uint32_t ssl_session_hash(const SSL_SESSION *a) {
1717 uint32_t hash =
1718 ((uint32_t)a->session_id[0]) ||
1719 ((uint32_t)a->session_id[1] << 8) ||
1720 ((uint32_t)a->session_id[2] << 16) ||
1721 ((uint32_t)a->session_id[3] << 24);
Adam Langley95c29f32014-06-20 12:00:00 -07001722
Adam Langleyfcf25832014-12-18 17:42:32 -08001723 return hash;
1724}
Adam Langley95c29f32014-06-20 12:00:00 -07001725
1726/* NB: If this function (or indeed the hash function which uses a sort of
1727 * coarser function than this one) is changed, ensure
1728 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1729 * able to construct an SSL_SESSION that will collide with any existing session
1730 * with a matching session ID. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001731static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
1732 if (a->ssl_version != b->ssl_version) {
1733 return 1;
1734 }
Adam Langley95c29f32014-06-20 12:00:00 -07001735
Adam Langleyfcf25832014-12-18 17:42:32 -08001736 if (a->session_id_length != b->session_id_length) {
1737 return 1;
1738 }
Adam Langley95c29f32014-06-20 12:00:00 -07001739
Adam Langleyfcf25832014-12-18 17:42:32 -08001740 return memcmp(a->session_id, b->session_id, a->session_id_length);
1741}
Adam Langley95c29f32014-06-20 12:00:00 -07001742
Adam Langleyfcf25832014-12-18 17:42:32 -08001743SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth) {
1744 SSL_CTX *ret = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001745
Adam Langleyfcf25832014-12-18 17:42:32 -08001746 if (meth == NULL) {
1747 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_NULL_SSL_METHOD_PASSED);
1748 return NULL;
1749 }
Adam Langley95c29f32014-06-20 12:00:00 -07001750
Adam Langleyfcf25832014-12-18 17:42:32 -08001751 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
1752 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
1753 goto err;
1754 }
Adam Langley95c29f32014-06-20 12:00:00 -07001755
Adam Langleyfcf25832014-12-18 17:42:32 -08001756 ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1757 if (ret == NULL) {
1758 goto err;
1759 }
Adam Langley95c29f32014-06-20 12:00:00 -07001760
Adam Langleyfcf25832014-12-18 17:42:32 -08001761 memset(ret, 0, sizeof(SSL_CTX));
Adam Langley95c29f32014-06-20 12:00:00 -07001762
Adam Langleyfcf25832014-12-18 17:42:32 -08001763 ret->method = meth->method;
Adam Langley95c29f32014-06-20 12:00:00 -07001764
Adam Langleyfcf25832014-12-18 17:42:32 -08001765 ret->cert_store = NULL;
1766 ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
1767 ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1768 ret->session_cache_head = NULL;
1769 ret->session_cache_tail = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001770
Adam Langleyfcf25832014-12-18 17:42:32 -08001771 /* We take the system default */
1772 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001773
Adam Langleyfcf25832014-12-18 17:42:32 -08001774 ret->new_session_cb = 0;
1775 ret->remove_session_cb = 0;
1776 ret->get_session_cb = 0;
1777 ret->generate_session_id = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001778
Adam Langleyfcf25832014-12-18 17:42:32 -08001779 ret->references = 1;
1780 ret->quiet_shutdown = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001781
Adam Langleyfcf25832014-12-18 17:42:32 -08001782 ret->info_callback = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001783
Adam Langleyfcf25832014-12-18 17:42:32 -08001784 ret->app_verify_callback = 0;
1785 ret->app_verify_arg = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001786
Adam Langleyfcf25832014-12-18 17:42:32 -08001787 ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
1788 ret->read_ahead = 0;
1789 ret->msg_callback = 0;
1790 ret->msg_callback_arg = NULL;
1791 ret->verify_mode = SSL_VERIFY_NONE;
1792 ret->sid_ctx_length = 0;
1793 ret->default_verify_callback = NULL;
1794 ret->cert = ssl_cert_new();
1795 if (ret->cert == NULL) {
1796 goto err;
1797 }
Adam Langley95c29f32014-06-20 12:00:00 -07001798
Adam Langleyfcf25832014-12-18 17:42:32 -08001799 ret->default_passwd_callback = 0;
1800 ret->default_passwd_callback_userdata = NULL;
1801 ret->client_cert_cb = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001802
Adam Langleyfcf25832014-12-18 17:42:32 -08001803 ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
1804 if (ret->sessions == NULL) {
1805 goto err;
1806 }
1807 ret->cert_store = X509_STORE_new();
1808 if (ret->cert_store == NULL) {
1809 goto err;
1810 }
Adam Langley95c29f32014-06-20 12:00:00 -07001811
Adam Langleyfcf25832014-12-18 17:42:32 -08001812 ssl_create_cipher_list(ret->method, &ret->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001813 &ret->cipher_list_by_id, SSL_DEFAULT_CIPHER_LIST);
Adam Langleyfcf25832014-12-18 17:42:32 -08001814 if (ret->cipher_list == NULL ||
1815 sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0) {
1816 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_LIBRARY_HAS_NO_CIPHERS);
1817 goto err2;
1818 }
Adam Langley95c29f32014-06-20 12:00:00 -07001819
Adam Langleyfcf25832014-12-18 17:42:32 -08001820 ret->param = X509_VERIFY_PARAM_new();
1821 if (!ret->param) {
1822 goto err;
1823 }
Adam Langley95c29f32014-06-20 12:00:00 -07001824
Adam Langleyfcf25832014-12-18 17:42:32 -08001825 ret->client_CA = sk_X509_NAME_new_null();
1826 if (ret->client_CA == NULL) {
1827 goto err;
1828 }
Adam Langley95c29f32014-06-20 12:00:00 -07001829
David Benjamin9f33fc62015-04-15 17:29:53 -04001830 CRYPTO_new_ex_data(&g_ex_data_class_ssl_ctx, ret, &ret->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001831
Adam Langleyfcf25832014-12-18 17:42:32 -08001832 ret->extra_certs = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001833
Adam Langleyfcf25832014-12-18 17:42:32 -08001834 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
David Benjamin422d3a42014-08-20 11:09:03 -04001835
Adam Langleyfcf25832014-12-18 17:42:32 -08001836 ret->tlsext_servername_callback = 0;
1837 ret->tlsext_servername_arg = NULL;
1838 /* Setup RFC4507 ticket keys */
1839 if (!RAND_bytes(ret->tlsext_tick_key_name, 16) ||
1840 !RAND_bytes(ret->tlsext_tick_hmac_key, 16) ||
1841 !RAND_bytes(ret->tlsext_tick_aes_key, 16)) {
1842 ret->options |= SSL_OP_NO_TICKET;
1843 }
Adam Langley95c29f32014-06-20 12:00:00 -07001844
Adam Langleyfcf25832014-12-18 17:42:32 -08001845 ret->tlsext_status_cb = 0;
1846 ret->tlsext_status_arg = NULL;
David Benjamin82c9e902014-12-12 15:55:27 -05001847
Adam Langleyfcf25832014-12-18 17:42:32 -08001848 ret->next_protos_advertised_cb = 0;
1849 ret->next_proto_select_cb = 0;
1850 ret->psk_identity_hint = NULL;
1851 ret->psk_client_callback = NULL;
1852 ret->psk_server_callback = NULL;
1853
1854 /* Default is to connect to non-RI servers. When RI is more widely deployed
1855 * might change this. */
1856 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
1857
1858 /* Lock the SSL_CTX to the specified version, for compatibility with legacy
1859 * uses of SSL_METHOD. */
1860 if (meth->version != 0) {
1861 SSL_CTX_set_max_version(ret, meth->version);
1862 SSL_CTX_set_min_version(ret, meth->version);
1863 }
1864
1865 return ret;
1866
Adam Langley95c29f32014-06-20 12:00:00 -07001867err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001868 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -07001869err2:
Adam Langleyfcf25832014-12-18 17:42:32 -08001870 if (ret != NULL) {
1871 SSL_CTX_free(ret);
1872 }
1873 return NULL;
1874}
Adam Langley95c29f32014-06-20 12:00:00 -07001875
David Benjamin4fcc2e22015-04-22 12:58:16 -04001876void SSL_CTX_free(SSL_CTX *ctx) {
1877 if (ctx == NULL ||
1878 CRYPTO_add(&ctx->references, -1, CRYPTO_LOCK_SSL_CTX) > 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001879 return;
1880 }
Adam Langley95c29f32014-06-20 12:00:00 -07001881
David Benjamin4fcc2e22015-04-22 12:58:16 -04001882 if (ctx->param) {
1883 X509_VERIFY_PARAM_free(ctx->param);
Adam Langleyfcf25832014-12-18 17:42:32 -08001884 }
Adam Langley95c29f32014-06-20 12:00:00 -07001885
Adam Langleyfcf25832014-12-18 17:42:32 -08001886 /* Free internal session cache. However: the remove_cb() may reference the
1887 * ex_data of SSL_CTX, thus the ex_data store can only be removed after the
1888 * sessions were flushed. As the ex_data handling routines might also touch
1889 * the session cache, the most secure solution seems to be: empty (flush) the
1890 * cache, then free ex_data, then finally free the cache. (See ticket
1891 * [openssl.org #212].) */
David Benjamin4fcc2e22015-04-22 12:58:16 -04001892 if (ctx->sessions != NULL) {
1893 SSL_CTX_flush_sessions(ctx, 0);
Adam Langleyfcf25832014-12-18 17:42:32 -08001894 }
Adam Langley95c29f32014-06-20 12:00:00 -07001895
David Benjamin4fcc2e22015-04-22 12:58:16 -04001896 CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, ctx, &ctx->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001897
David Benjamin4fcc2e22015-04-22 12:58:16 -04001898 if (ctx->sessions != NULL) {
1899 lh_SSL_SESSION_free(ctx->sessions);
Adam Langleyfcf25832014-12-18 17:42:32 -08001900 }
David Benjamin4fcc2e22015-04-22 12:58:16 -04001901 if (ctx->cert_store != NULL) {
1902 X509_STORE_free(ctx->cert_store);
Adam Langleyfcf25832014-12-18 17:42:32 -08001903 }
David Benjamin4fcc2e22015-04-22 12:58:16 -04001904 if (ctx->cipher_list != NULL) {
1905 ssl_cipher_preference_list_free(ctx->cipher_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001906 }
David Benjamin4fcc2e22015-04-22 12:58:16 -04001907 if (ctx->cipher_list_by_id != NULL) {
1908 sk_SSL_CIPHER_free(ctx->cipher_list_by_id);
Adam Langleyfcf25832014-12-18 17:42:32 -08001909 }
David Benjamin4fcc2e22015-04-22 12:58:16 -04001910 if (ctx->cipher_list_tls11 != NULL) {
1911 ssl_cipher_preference_list_free(ctx->cipher_list_tls11);
Adam Langleyfcf25832014-12-18 17:42:32 -08001912 }
David Benjamin4fcc2e22015-04-22 12:58:16 -04001913 if (ctx->cert != NULL) {
1914 ssl_cert_free(ctx->cert);
Adam Langleyfcf25832014-12-18 17:42:32 -08001915 }
David Benjamin4fcc2e22015-04-22 12:58:16 -04001916 if (ctx->client_CA != NULL) {
1917 sk_X509_NAME_pop_free(ctx->client_CA, X509_NAME_free);
Adam Langleyfcf25832014-12-18 17:42:32 -08001918 }
David Benjamin4fcc2e22015-04-22 12:58:16 -04001919 if (ctx->extra_certs != NULL) {
1920 sk_X509_pop_free(ctx->extra_certs, X509_free);
Adam Langleyfcf25832014-12-18 17:42:32 -08001921 }
David Benjamin4fcc2e22015-04-22 12:58:16 -04001922 if (ctx->srtp_profiles) {
1923 sk_SRTP_PROTECTION_PROFILE_free(ctx->srtp_profiles);
Adam Langleyfcf25832014-12-18 17:42:32 -08001924 }
David Benjamin4fcc2e22015-04-22 12:58:16 -04001925 if (ctx->psk_identity_hint) {
1926 OPENSSL_free(ctx->psk_identity_hint);
Adam Langleyfcf25832014-12-18 17:42:32 -08001927 }
David Benjamin4fcc2e22015-04-22 12:58:16 -04001928 if (ctx->tlsext_ecpointformatlist) {
1929 OPENSSL_free(ctx->tlsext_ecpointformatlist);
Adam Langleyfcf25832014-12-18 17:42:32 -08001930 }
David Benjamin4fcc2e22015-04-22 12:58:16 -04001931 if (ctx->tlsext_ellipticcurvelist) {
1932 OPENSSL_free(ctx->tlsext_ellipticcurvelist);
Adam Langleyfcf25832014-12-18 17:42:32 -08001933 }
David Benjamin4fcc2e22015-04-22 12:58:16 -04001934 if (ctx->alpn_client_proto_list != NULL) {
1935 OPENSSL_free(ctx->alpn_client_proto_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001936 }
David Benjamin4fcc2e22015-04-22 12:58:16 -04001937 if (ctx->tlsext_channel_id_private) {
1938 EVP_PKEY_free(ctx->tlsext_channel_id_private);
Adam Langleyfcf25832014-12-18 17:42:32 -08001939 }
David Benjamin4fcc2e22015-04-22 12:58:16 -04001940 if (ctx->keylog_bio) {
1941 BIO_free(ctx->keylog_bio);
Adam Langleyfcf25832014-12-18 17:42:32 -08001942 }
Adam Langley95c29f32014-06-20 12:00:00 -07001943
David Benjamin4fcc2e22015-04-22 12:58:16 -04001944 OPENSSL_free(ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08001945}
Adam Langley95c29f32014-06-20 12:00:00 -07001946
Adam Langleyfcf25832014-12-18 17:42:32 -08001947void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) {
1948 ctx->default_passwd_callback = cb;
1949}
Adam Langley95c29f32014-06-20 12:00:00 -07001950
Adam Langleyfcf25832014-12-18 17:42:32 -08001951void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u) {
1952 ctx->default_passwd_callback_userdata = u;
1953}
Adam Langley95c29f32014-06-20 12:00:00 -07001954
Adam Langleyfcf25832014-12-18 17:42:32 -08001955void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
1956 int (*cb)(X509_STORE_CTX *, void *),
1957 void *arg) {
1958 ctx->app_verify_callback = cb;
1959 ctx->app_verify_arg = arg;
1960}
Adam Langley95c29f32014-06-20 12:00:00 -07001961
Adam Langleyfcf25832014-12-18 17:42:32 -08001962void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
1963 int (*cb)(int, X509_STORE_CTX *)) {
1964 ctx->verify_mode = mode;
1965 ctx->default_verify_callback = cb;
1966}
Adam Langley95c29f32014-06-20 12:00:00 -07001967
Adam Langleyfcf25832014-12-18 17:42:32 -08001968void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
1969 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
1970}
Adam Langley1258b6a2014-06-20 12:00:00 -07001971
Adam Langleyfcf25832014-12-18 17:42:32 -08001972void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb)(SSL *ssl, void *arg),
1973 void *arg) {
1974 ssl_cert_set_cert_cb(c->cert, cb, arg);
1975}
David Benjamin859ec3c2014-09-02 16:29:36 -04001976
Adam Langleyfcf25832014-12-18 17:42:32 -08001977void SSL_set_cert_cb(SSL *s, int (*cb)(SSL *ssl, void *arg), void *arg) {
1978 ssl_cert_set_cert_cb(s->cert, cb, arg);
1979}
Adam Langley95c29f32014-06-20 12:00:00 -07001980
Adam Langleyfcf25832014-12-18 17:42:32 -08001981static int ssl_has_key(SSL *s, size_t idx) {
1982 CERT_PKEY *cpk = &s->cert->pkeys[idx];
1983 return cpk->x509 && cpk->privatekey;
1984}
David Benjamin033e5f42014-11-13 18:47:41 -05001985
David Benjamin107db582015-04-08 00:41:59 -04001986void ssl_get_compatible_server_ciphers(SSL *s, uint32_t *out_mask_k,
1987 uint32_t *out_mask_a) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001988 CERT *c = s->cert;
1989 int rsa_enc, rsa_sign, dh_tmp;
David Benjamin107db582015-04-08 00:41:59 -04001990 uint32_t mask_k, mask_a;
Adam Langleyfcf25832014-12-18 17:42:32 -08001991 int have_ecc_cert, ecdsa_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001992 X509 *x;
Adam Langley95c29f32014-06-20 12:00:00 -07001993
Adam Langleyfcf25832014-12-18 17:42:32 -08001994 if (c == NULL) {
1995 /* TODO(davidben): Is this codepath possible? */
1996 *out_mask_k = 0;
1997 *out_mask_a = 0;
1998 return;
1999 }
Adam Langley95c29f32014-06-20 12:00:00 -07002000
Adam Langleyfcf25832014-12-18 17:42:32 -08002001 dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07002002
Adam Langleyfcf25832014-12-18 17:42:32 -08002003 rsa_enc = ssl_has_key(s, SSL_PKEY_RSA_ENC);
2004 rsa_sign = ssl_has_key(s, SSL_PKEY_RSA_SIGN);
2005 have_ecc_cert = ssl_has_key(s, SSL_PKEY_ECC);
2006 mask_k = 0;
2007 mask_a = 0;
David Benjaminf31e6812014-11-13 18:05:55 -05002008
Adam Langleyfcf25832014-12-18 17:42:32 -08002009 if (rsa_enc) {
2010 mask_k |= SSL_kRSA;
2011 }
2012 if (dh_tmp) {
David Benjamin7061e282015-03-19 11:10:48 -04002013 mask_k |= SSL_kDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08002014 }
2015 if (rsa_enc || rsa_sign) {
2016 mask_a |= SSL_aRSA;
2017 }
Adam Langley95c29f32014-06-20 12:00:00 -07002018
Adam Langleyfcf25832014-12-18 17:42:32 -08002019 /* An ECC certificate may be usable for ECDSA cipher suites depending on the
2020 * key usage extension and on the client's curve preferences. */
2021 if (have_ecc_cert) {
2022 x = c->pkeys[SSL_PKEY_ECC].x509;
2023 /* This call populates extension flags (ex_flags). */
2024 X509_check_purpose(x, -1, 0);
2025 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE)
2026 ? (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)
2027 : 1;
2028 if (!tls1_check_ec_cert(s, x)) {
2029 ecdsa_ok = 0;
2030 }
2031 if (ecdsa_ok) {
2032 mask_a |= SSL_aECDSA;
2033 }
2034 }
Adam Langley95c29f32014-06-20 12:00:00 -07002035
Adam Langleyfcf25832014-12-18 17:42:32 -08002036 /* If we are considering an ECC cipher suite that uses an ephemeral EC
2037 * key, check it. */
David Benjamindd978782015-04-24 15:20:13 -04002038 if (tls1_check_ec_tmp_key(s)) {
David Benjamin7061e282015-03-19 11:10:48 -04002039 mask_k |= SSL_kECDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08002040 }
Adam Langley95c29f32014-06-20 12:00:00 -07002041
Adam Langleyfcf25832014-12-18 17:42:32 -08002042 /* PSK requires a server callback. */
2043 if (s->psk_server_callback != NULL) {
2044 mask_k |= SSL_kPSK;
2045 mask_a |= SSL_aPSK;
2046 }
Adam Langley95c29f32014-06-20 12:00:00 -07002047
Adam Langleyfcf25832014-12-18 17:42:32 -08002048 *out_mask_k = mask_k;
2049 *out_mask_a = mask_a;
2050}
Adam Langley95c29f32014-06-20 12:00:00 -07002051
2052/* This handy macro borrowed from crypto/x509v3/v3_purp.c */
2053#define ku_reject(x, usage) \
Adam Langleyfcf25832014-12-18 17:42:32 -08002054 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
Adam Langley95c29f32014-06-20 12:00:00 -07002055
Adam Langleyfcf25832014-12-18 17:42:32 -08002056int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002057 const SSL_CIPHER *cs = s->s3->tmp.new_cipher;
David Benjamin3fa27772015-04-17 22:32:19 -04002058 uint32_t alg_a = cs->algorithm_auth;
2059 int signature_nid = 0, md_nid = 0, pk_nid = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002060
Adam Langleyfcf25832014-12-18 17:42:32 -08002061 /* This call populates the ex_flags field correctly */
2062 X509_check_purpose(x, -1, 0);
2063 if (x->sig_alg && x->sig_alg->algorithm) {
2064 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
2065 OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
2066 }
2067 if (alg_a & SSL_aECDSA) {
2068 /* key usage, if present, must allow signing */
2069 if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE)) {
2070 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg,
2071 SSL_R_ECC_CERT_NOT_FOR_SIGNING);
2072 return 0;
2073 }
2074 }
Adam Langley95c29f32014-06-20 12:00:00 -07002075
Adam Langleyfcf25832014-12-18 17:42:32 -08002076 return 1; /* all checks are ok */
2077}
Adam Langley95c29f32014-06-20 12:00:00 -07002078
Adam Langleyfcf25832014-12-18 17:42:32 -08002079static int ssl_get_server_cert_index(const SSL *s) {
2080 int idx;
2081 idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
2082 if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509) {
2083 idx = SSL_PKEY_RSA_SIGN;
2084 }
2085 if (idx == -1) {
2086 OPENSSL_PUT_ERROR(SSL, ssl_get_server_cert_index, ERR_R_INTERNAL_ERROR);
2087 }
2088 return idx;
2089}
Adam Langley95c29f32014-06-20 12:00:00 -07002090
Adam Langleyfcf25832014-12-18 17:42:32 -08002091CERT_PKEY *ssl_get_server_send_pkey(const SSL *s) {
2092 int i = ssl_get_server_cert_index(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002093
Adam Langleyfcf25832014-12-18 17:42:32 -08002094 /* This may or may not be an error. */
2095 if (i < 0) {
2096 return NULL;
2097 }
Adam Langley95c29f32014-06-20 12:00:00 -07002098
Adam Langleyfcf25832014-12-18 17:42:32 -08002099 /* May be NULL. */
2100 return &s->cert->pkeys[i];
2101}
Adam Langley95c29f32014-06-20 12:00:00 -07002102
Adam Langleyfcf25832014-12-18 17:42:32 -08002103EVP_PKEY *ssl_get_sign_pkey(SSL *s, const SSL_CIPHER *cipher) {
David Benjamin3fa27772015-04-17 22:32:19 -04002104 uint32_t alg_a = cipher->algorithm_auth;
2105 CERT *c = s->cert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002106 int idx = -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002107
Adam Langleyfcf25832014-12-18 17:42:32 -08002108 if (alg_a & SSL_aRSA) {
2109 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL) {
2110 idx = SSL_PKEY_RSA_SIGN;
2111 } else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL) {
2112 idx = SSL_PKEY_RSA_ENC;
2113 }
2114 } else if ((alg_a & SSL_aECDSA) &&
2115 (c->pkeys[SSL_PKEY_ECC].privatekey != NULL)) {
2116 idx = SSL_PKEY_ECC;
2117 }
Adam Langley95c29f32014-06-20 12:00:00 -07002118
Adam Langleyfcf25832014-12-18 17:42:32 -08002119 if (idx == -1) {
2120 OPENSSL_PUT_ERROR(SSL, ssl_get_sign_pkey, ERR_R_INTERNAL_ERROR);
2121 return NULL;
2122 }
Adam Langley95c29f32014-06-20 12:00:00 -07002123
Adam Langleyfcf25832014-12-18 17:42:32 -08002124 return c->pkeys[idx].privatekey;
2125}
Adam Langley95c29f32014-06-20 12:00:00 -07002126
Adam Langleyfcf25832014-12-18 17:42:32 -08002127void ssl_update_cache(SSL *s, int mode) {
David Benjaminb6d0c6d2015-03-19 19:07:26 -04002128 /* Never cache sessions with empty session IDs. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002129 if (s->session->session_id_length == 0) {
2130 return;
2131 }
Adam Langley95c29f32014-06-20 12:00:00 -07002132
David Benjaminb6d0c6d2015-03-19 19:07:26 -04002133 SSL_CTX *ctx = s->initial_ctx;
2134 if ((ctx->session_cache_mode & mode) == mode && !s->hit &&
2135 ((ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) ||
2136 SSL_CTX_add_session(ctx, s->session)) &&
2137 ctx->new_session_cb != NULL) {
2138 /* Note: |new_session_cb| is called whether the internal session cache is
2139 * used or not. */
2140 if (!ctx->new_session_cb(s, SSL_SESSION_up_ref(s->session))) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002141 SSL_SESSION_free(s->session);
2142 }
2143 }
Adam Langley95c29f32014-06-20 12:00:00 -07002144
David Benjaminb6d0c6d2015-03-19 19:07:26 -04002145 if (!(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR) &&
2146 !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) &&
2147 (ctx->session_cache_mode & mode) == mode) {
2148 /* Automatically flush the internal session cache every 255 connections. */
2149 int flush_cache = 0;
2150 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2151 ctx->handshakes_since_cache_flush++;
2152 if (ctx->handshakes_since_cache_flush >= 255) {
2153 flush_cache = 1;
2154 ctx->handshakes_since_cache_flush = 0;
2155 }
2156 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
2157
2158 if (flush_cache) {
2159 SSL_CTX_flush_sessions(ctx, (unsigned long)time(NULL));
Adam Langleyfcf25832014-12-18 17:42:32 -08002160 }
2161 }
2162}
Adam Langley95c29f32014-06-20 12:00:00 -07002163
David Benjamin1a5c50f2015-03-11 16:22:37 -04002164int SSL_get_error(const SSL *s, int ret_code) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002165 int reason;
David Benjamin1a5c50f2015-03-11 16:22:37 -04002166 uint32_t err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002167 BIO *bio;
Adam Langley95c29f32014-06-20 12:00:00 -07002168
David Benjamin1a5c50f2015-03-11 16:22:37 -04002169 if (ret_code > 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002170 return SSL_ERROR_NONE;
2171 }
Adam Langley95c29f32014-06-20 12:00:00 -07002172
Adam Langleyfcf25832014-12-18 17:42:32 -08002173 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
2174 * where we do encode the error */
David Benjamin1a5c50f2015-03-11 16:22:37 -04002175 err = ERR_peek_error();
2176 if (err != 0) {
2177 if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002178 return SSL_ERROR_SYSCALL;
2179 }
2180 return SSL_ERROR_SSL;
2181 }
Adam Langley95c29f32014-06-20 12:00:00 -07002182
David Benjamin1a5c50f2015-03-11 16:22:37 -04002183 if (ret_code == 0) {
David Benjamin9a38e922015-01-22 16:06:11 -05002184 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2185 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) {
2186 /* The socket was cleanly shut down with a close_notify. */
2187 return SSL_ERROR_ZERO_RETURN;
2188 }
2189 /* An EOF was observed which violates the protocol, and the underlying
2190 * transport does not participate in the error queue. Bubble up to the
2191 * caller. */
2192 return SSL_ERROR_SYSCALL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002193 }
Adam Langleyb2ce0582014-06-20 12:00:00 -07002194
Adam Langleyfcf25832014-12-18 17:42:32 -08002195 if (SSL_want_session(s)) {
2196 return SSL_ERROR_PENDING_SESSION;
2197 }
Adam Langley95c29f32014-06-20 12:00:00 -07002198
Adam Langleyfcf25832014-12-18 17:42:32 -08002199 if (SSL_want_certificate(s)) {
2200 return SSL_ERROR_PENDING_CERTIFICATE;
2201 }
Adam Langley95c29f32014-06-20 12:00:00 -07002202
Adam Langleyfcf25832014-12-18 17:42:32 -08002203 if (SSL_want_read(s)) {
2204 bio = SSL_get_rbio(s);
2205 if (BIO_should_read(bio)) {
2206 return SSL_ERROR_WANT_READ;
2207 }
Adam Langley95c29f32014-06-20 12:00:00 -07002208
Adam Langleyfcf25832014-12-18 17:42:32 -08002209 if (BIO_should_write(bio)) {
2210 /* This one doesn't make too much sense ... We never try to write to the
2211 * rbio, and an application program where rbio and wbio are separate
2212 * couldn't even know what it should wait for. However if we ever set
2213 * s->rwstate incorrectly (so that we have SSL_want_read(s) instead of
2214 * SSL_want_write(s)) and rbio and wbio *are* the same, this test works
2215 * around that bug; so it might be safer to keep it. */
2216 return SSL_ERROR_WANT_WRITE;
2217 }
Adam Langley95c29f32014-06-20 12:00:00 -07002218
Adam Langleyfcf25832014-12-18 17:42:32 -08002219 if (BIO_should_io_special(bio)) {
2220 reason = BIO_get_retry_reason(bio);
2221 if (reason == BIO_RR_CONNECT) {
2222 return SSL_ERROR_WANT_CONNECT;
2223 }
Adam Langley95c29f32014-06-20 12:00:00 -07002224
Adam Langleyfcf25832014-12-18 17:42:32 -08002225 if (reason == BIO_RR_ACCEPT) {
2226 return SSL_ERROR_WANT_ACCEPT;
2227 }
Adam Langley95c29f32014-06-20 12:00:00 -07002228
Adam Langleyfcf25832014-12-18 17:42:32 -08002229 return SSL_ERROR_SYSCALL; /* unknown */
2230 }
2231 }
Adam Langley95c29f32014-06-20 12:00:00 -07002232
Adam Langleyfcf25832014-12-18 17:42:32 -08002233 if (SSL_want_write(s)) {
2234 bio = SSL_get_wbio(s);
2235 if (BIO_should_write(bio)) {
2236 return SSL_ERROR_WANT_WRITE;
2237 }
Adam Langley95c29f32014-06-20 12:00:00 -07002238
Adam Langleyfcf25832014-12-18 17:42:32 -08002239 if (BIO_should_read(bio)) {
2240 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2241 return SSL_ERROR_WANT_READ;
2242 }
Adam Langley95c29f32014-06-20 12:00:00 -07002243
Adam Langleyfcf25832014-12-18 17:42:32 -08002244 if (BIO_should_io_special(bio)) {
2245 reason = BIO_get_retry_reason(bio);
2246 if (reason == BIO_RR_CONNECT) {
2247 return SSL_ERROR_WANT_CONNECT;
2248 }
Adam Langley95c29f32014-06-20 12:00:00 -07002249
Adam Langleyfcf25832014-12-18 17:42:32 -08002250 if (reason == BIO_RR_ACCEPT) {
2251 return SSL_ERROR_WANT_ACCEPT;
2252 }
Adam Langley95c29f32014-06-20 12:00:00 -07002253
Adam Langleyfcf25832014-12-18 17:42:32 -08002254 return SSL_ERROR_SYSCALL;
2255 }
2256 }
Adam Langley95c29f32014-06-20 12:00:00 -07002257
Adam Langleyfcf25832014-12-18 17:42:32 -08002258 if (SSL_want_x509_lookup(s)) {
2259 return SSL_ERROR_WANT_X509_LOOKUP;
2260 }
Adam Langley95c29f32014-06-20 12:00:00 -07002261
Adam Langleyfcf25832014-12-18 17:42:32 -08002262 if (SSL_want_channel_id_lookup(s)) {
2263 return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
2264 }
Adam Langley0f4746e2014-08-13 12:26:32 -07002265
Adam Langleyfcf25832014-12-18 17:42:32 -08002266 return SSL_ERROR_SYSCALL;
2267}
Adam Langley0f4746e2014-08-13 12:26:32 -07002268
Adam Langleyfcf25832014-12-18 17:42:32 -08002269int SSL_do_handshake(SSL *s) {
2270 int ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002271
Adam Langleyfcf25832014-12-18 17:42:32 -08002272 if (s->handshake_func == NULL) {
2273 OPENSSL_PUT_ERROR(SSL, SSL_do_handshake, SSL_R_CONNECTION_TYPE_NOT_SET);
2274 return -1;
2275 }
Adam Langley95c29f32014-06-20 12:00:00 -07002276
Adam Langleyfcf25832014-12-18 17:42:32 -08002277 s->method->ssl_renegotiate_check(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002278
Adam Langleyfcf25832014-12-18 17:42:32 -08002279 if (SSL_in_init(s)) {
2280 ret = s->handshake_func(s);
2281 }
2282 return ret;
2283}
Adam Langley95c29f32014-06-20 12:00:00 -07002284
Adam Langleyfcf25832014-12-18 17:42:32 -08002285void SSL_set_accept_state(SSL *s) {
2286 s->server = 1;
2287 s->shutdown = 0;
2288 s->state = SSL_ST_ACCEPT | SSL_ST_BEFORE;
2289 s->handshake_func = s->method->ssl_accept;
2290 /* clear the current cipher */
2291 ssl_clear_cipher_ctx(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002292}
Adam Langley95c29f32014-06-20 12:00:00 -07002293
Adam Langleyfcf25832014-12-18 17:42:32 -08002294void SSL_set_connect_state(SSL *s) {
2295 s->server = 0;
2296 s->shutdown = 0;
2297 s->state = SSL_ST_CONNECT | SSL_ST_BEFORE;
2298 s->handshake_func = s->method->ssl_connect;
2299 /* clear the current cipher */
2300 ssl_clear_cipher_ctx(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002301}
Adam Langley95c29f32014-06-20 12:00:00 -07002302
Adam Langleyfcf25832014-12-18 17:42:32 -08002303int ssl_undefined_function(SSL *s) {
2304 OPENSSL_PUT_ERROR(SSL, ssl_undefined_function,
2305 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2306 return 0;
2307}
Adam Langley95c29f32014-06-20 12:00:00 -07002308
Adam Langleyfcf25832014-12-18 17:42:32 -08002309int ssl_undefined_void_function(void) {
2310 OPENSSL_PUT_ERROR(SSL, ssl_undefined_void_function,
2311 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2312 return 0;
2313}
Adam Langley95c29f32014-06-20 12:00:00 -07002314
Adam Langleyfcf25832014-12-18 17:42:32 -08002315int ssl_undefined_const_function(const SSL *s) {
2316 OPENSSL_PUT_ERROR(SSL, ssl_undefined_const_function,
2317 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2318 return 0;
2319}
Adam Langley95c29f32014-06-20 12:00:00 -07002320
Adam Langleyfcf25832014-12-18 17:42:32 -08002321static const char *ssl_get_version(int version) {
2322 switch (version) {
2323 case TLS1_2_VERSION:
2324 return "TLSv1.2";
Adam Langley95c29f32014-06-20 12:00:00 -07002325
Adam Langleyfcf25832014-12-18 17:42:32 -08002326 case TLS1_1_VERSION:
2327 return "TLSv1.1";
Adam Langley95c29f32014-06-20 12:00:00 -07002328
Adam Langleyfcf25832014-12-18 17:42:32 -08002329 case TLS1_VERSION:
2330 return "TLSv1";
Adam Langley95c29f32014-06-20 12:00:00 -07002331
Adam Langleyfcf25832014-12-18 17:42:32 -08002332 case SSL3_VERSION:
2333 return "SSLv3";
Adam Langley95c29f32014-06-20 12:00:00 -07002334
David Benjamin1c722b72015-04-20 13:53:10 -04002335 case DTLS1_VERSION:
2336 return "DTLSv1";
2337
2338 case DTLS1_2_VERSION:
2339 return "DTLSv1.2";
2340
Adam Langleyfcf25832014-12-18 17:42:32 -08002341 default:
2342 return "unknown";
2343 }
2344}
Adam Langley95c29f32014-06-20 12:00:00 -07002345
Adam Langleyfcf25832014-12-18 17:42:32 -08002346const char *SSL_get_version(const SSL *s) {
2347 return ssl_get_version(s->version);
2348}
Adam Langley95c29f32014-06-20 12:00:00 -07002349
Adam Langleyfcf25832014-12-18 17:42:32 -08002350const char *SSL_SESSION_get_version(const SSL_SESSION *sess) {
2351 return ssl_get_version(sess->ssl_version);
2352}
Adam Langley95c29f32014-06-20 12:00:00 -07002353
Adam Langleyfcf25832014-12-18 17:42:32 -08002354void ssl_clear_cipher_ctx(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002355 if (s->aead_read_ctx != NULL) {
2356 EVP_AEAD_CTX_cleanup(&s->aead_read_ctx->ctx);
2357 OPENSSL_free(s->aead_read_ctx);
2358 s->aead_read_ctx = NULL;
2359 }
Adam Langleya5dc5452014-06-20 12:00:00 -07002360
Adam Langleyfcf25832014-12-18 17:42:32 -08002361 if (s->aead_write_ctx != NULL) {
2362 EVP_AEAD_CTX_cleanup(&s->aead_write_ctx->ctx);
2363 OPENSSL_free(s->aead_write_ctx);
2364 s->aead_write_ctx = NULL;
2365 }
2366}
Adam Langley95c29f32014-06-20 12:00:00 -07002367
Adam Langleyfcf25832014-12-18 17:42:32 -08002368X509 *SSL_get_certificate(const SSL *s) {
2369 if (s->cert != NULL) {
2370 return s->cert->key->x509;
2371 }
2372
2373 return NULL;
2374}
2375
2376EVP_PKEY *SSL_get_privatekey(const SSL *s) {
2377 if (s->cert != NULL) {
2378 return s->cert->key->privatekey;
2379 }
2380
2381 return NULL;
2382}
2383
2384X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
2385 if (ctx->cert != NULL) {
2386 return ctx->cert->key->x509;
2387 }
2388
2389 return NULL;
2390}
2391
2392EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
2393 if (ctx->cert != NULL) {
2394 return ctx->cert->key->privatekey;
2395 }
2396
2397 return NULL;
2398}
2399
2400const SSL_CIPHER *SSL_get_current_cipher(const SSL *s) {
2401 if (s->session != NULL && s->session->cipher != NULL) {
2402 return s->session->cipher;
2403 }
2404
2405 return NULL;
2406}
2407
2408const void *SSL_get_current_compression(SSL *s) { return NULL; }
2409
2410const void *SSL_get_current_expansion(SSL *s) { return NULL; }
2411
2412int ssl_init_wbio_buffer(SSL *s, int push) {
2413 BIO *bbio;
2414
2415 if (s->bbio == NULL) {
2416 bbio = BIO_new(BIO_f_buffer());
2417 if (bbio == NULL) {
2418 return 0;
2419 }
2420 s->bbio = bbio;
2421 } else {
2422 bbio = s->bbio;
2423 if (s->bbio == s->wbio) {
2424 s->wbio = BIO_pop(s->wbio);
2425 }
2426 }
2427
2428 BIO_reset(bbio);
2429 if (!BIO_set_read_buffer_size(bbio, 1)) {
2430 OPENSSL_PUT_ERROR(SSL, ssl_init_wbio_buffer, ERR_R_BUF_LIB);
2431 return 0;
2432 }
2433
2434 if (push) {
2435 if (s->wbio != bbio) {
2436 s->wbio = BIO_push(bbio, s->wbio);
2437 }
2438 } else {
2439 if (s->wbio == bbio) {
2440 s->wbio = BIO_pop(bbio);
2441 }
2442 }
2443
2444 return 1;
2445}
2446
2447void ssl_free_wbio_buffer(SSL *s) {
2448 if (s->bbio == NULL) {
2449 return;
2450 }
2451
2452 if (s->bbio == s->wbio) {
2453 /* remove buffering */
2454 s->wbio = BIO_pop(s->wbio);
2455 }
2456
2457 BIO_free(s->bbio);
2458 s->bbio = NULL;
2459}
2460
2461void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
2462 ctx->quiet_shutdown = mode;
2463}
2464
2465int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
2466 return ctx->quiet_shutdown;
2467}
2468
2469void SSL_set_quiet_shutdown(SSL *s, int mode) { s->quiet_shutdown = mode; }
2470
2471int SSL_get_quiet_shutdown(const SSL *s) { return s->quiet_shutdown; }
2472
2473void SSL_set_shutdown(SSL *s, int mode) { s->shutdown = mode; }
2474
2475int SSL_get_shutdown(const SSL *s) { return s->shutdown; }
2476
2477int SSL_version(const SSL *s) { return s->version; }
2478
2479SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
2480
2481SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
2482 if (ssl->ctx == ctx) {
2483 return ssl->ctx;
2484 }
2485
2486 if (ctx == NULL) {
2487 ctx = ssl->initial_ctx;
2488 }
2489
2490 if (ssl->cert != NULL) {
2491 ssl_cert_free(ssl->cert);
2492 }
2493
2494 ssl->cert = ssl_cert_dup(ctx->cert);
2495 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
2496 if (ssl->ctx != NULL) {
2497 SSL_CTX_free(ssl->ctx); /* decrement reference count */
2498 }
2499 ssl->ctx = ctx;
2500
2501 ssl->sid_ctx_length = ctx->sid_ctx_length;
2502 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2503 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2504
2505 return ssl->ctx;
2506}
2507
2508int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
2509 return X509_STORE_set_default_paths(ctx->cert_store);
2510}
Adam Langley95c29f32014-06-20 12:00:00 -07002511
2512int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
Adam Langleyfcf25832014-12-18 17:42:32 -08002513 const char *CApath) {
2514 return X509_STORE_load_locations(ctx->cert_store, CAfile, CApath);
2515}
Adam Langley95c29f32014-06-20 12:00:00 -07002516
2517void SSL_set_info_callback(SSL *ssl,
Adam Langleyfcf25832014-12-18 17:42:32 -08002518 void (*cb)(const SSL *ssl, int type, int val)) {
2519 ssl->info_callback = cb;
2520}
Adam Langley95c29f32014-06-20 12:00:00 -07002521
Adam Langleyfcf25832014-12-18 17:42:32 -08002522void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/, int /*type*/,
2523 int /*val*/) {
2524 return ssl->info_callback;
2525}
Adam Langley95c29f32014-06-20 12:00:00 -07002526
Adam Langleyfcf25832014-12-18 17:42:32 -08002527int SSL_state(const SSL *ssl) { return ssl->state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002528
Adam Langleyfcf25832014-12-18 17:42:32 -08002529void SSL_set_state(SSL *ssl, int state) { ssl->state = state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002530
Adam Langleyfcf25832014-12-18 17:42:32 -08002531void SSL_set_verify_result(SSL *ssl, long arg) { ssl->verify_result = arg; }
Adam Langley95c29f32014-06-20 12:00:00 -07002532
Adam Langleyfcf25832014-12-18 17:42:32 -08002533long SSL_get_verify_result(const SSL *ssl) { return ssl->verify_result; }
Adam Langley95c29f32014-06-20 12:00:00 -07002534
Adam Langleyfcf25832014-12-18 17:42:32 -08002535int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2536 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002537 int index;
2538 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp,
2539 new_func, dup_func, free_func)) {
2540 return -1;
2541 }
2542 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002543}
Adam Langley95c29f32014-06-20 12:00:00 -07002544
Adam Langleyfcf25832014-12-18 17:42:32 -08002545int SSL_set_ex_data(SSL *s, int idx, void *arg) {
2546 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2547}
Adam Langley95c29f32014-06-20 12:00:00 -07002548
Adam Langleyfcf25832014-12-18 17:42:32 -08002549void *SSL_get_ex_data(const SSL *s, int idx) {
2550 return CRYPTO_get_ex_data(&s->ex_data, idx);
2551}
Adam Langley95c29f32014-06-20 12:00:00 -07002552
Adam Langleyfcf25832014-12-18 17:42:32 -08002553int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2554 CRYPTO_EX_dup *dup_func,
2555 CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002556 int index;
2557 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp,
2558 new_func, dup_func, free_func)) {
2559 return -1;
2560 }
2561 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002562}
Adam Langley95c29f32014-06-20 12:00:00 -07002563
Adam Langleyfcf25832014-12-18 17:42:32 -08002564int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg) {
2565 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2566}
Adam Langley95c29f32014-06-20 12:00:00 -07002567
Adam Langleyfcf25832014-12-18 17:42:32 -08002568void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx) {
2569 return CRYPTO_get_ex_data(&s->ex_data, idx);
2570}
Adam Langley95c29f32014-06-20 12:00:00 -07002571
Adam Langleyfcf25832014-12-18 17:42:32 -08002572int ssl_ok(SSL *s) { return 1; }
Adam Langley95c29f32014-06-20 12:00:00 -07002573
Adam Langleyfcf25832014-12-18 17:42:32 -08002574X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
2575 return ctx->cert_store;
2576}
Adam Langley95c29f32014-06-20 12:00:00 -07002577
Adam Langleyfcf25832014-12-18 17:42:32 -08002578void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
2579 if (ctx->cert_store != NULL) {
2580 X509_STORE_free(ctx->cert_store);
2581 }
2582 ctx->cert_store = store;
2583}
Adam Langley95c29f32014-06-20 12:00:00 -07002584
Adam Langleyfcf25832014-12-18 17:42:32 -08002585int SSL_want(const SSL *s) { return s->rwstate; }
Adam Langley95c29f32014-06-20 12:00:00 -07002586
Adam Langleyfcf25832014-12-18 17:42:32 -08002587void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
2588 RSA *(*cb)(SSL *ssl, int is_export,
2589 int keylength)) {
2590 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_RSA_CB, (void (*)(void))cb);
2591}
Adam Langley95c29f32014-06-20 12:00:00 -07002592
Adam Langleyfcf25832014-12-18 17:42:32 -08002593void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
2594 int keylength)) {
2595 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_RSA_CB, (void (*)(void))cb);
2596}
2597
2598void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
2599 DH *(*dh)(SSL *ssl, int is_export,
2600 int keylength)) {
2601 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh);
2602}
2603
2604void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*dh)(SSL *ssl, int is_export,
2605 int keylength)) {
2606 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh);
2607}
2608
2609void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,
David Benjamindd978782015-04-24 15:20:13 -04002610 EC_KEY *(*callback)(SSL *ssl, int is_export,
2611 int keylength)) {
2612 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH_CB,
2613 (void (*)(void))callback);
Adam Langleyfcf25832014-12-18 17:42:32 -08002614}
2615
2616void SSL_set_tmp_ecdh_callback(SSL *ssl,
David Benjamindd978782015-04-24 15:20:13 -04002617 EC_KEY *(*callback)(SSL *ssl, int is_export,
2618 int keylength)) {
2619 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_ECDH_CB, (void (*)(void))callback);
Adam Langleyfcf25832014-12-18 17:42:32 -08002620}
2621
2622int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
2623 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2624 OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_psk_identity_hint,
2625 SSL_R_DATA_LENGTH_TOO_LONG);
2626 return 0;
2627 }
2628
2629 if (ctx->psk_identity_hint != NULL) {
2630 OPENSSL_free(ctx->psk_identity_hint);
2631 }
2632
2633 if (identity_hint != NULL) {
2634 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2635 if (ctx->psk_identity_hint == NULL) {
2636 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002637 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002638 } else {
2639 ctx->psk_identity_hint = NULL;
2640 }
Adam Langley95c29f32014-06-20 12:00:00 -07002641
Adam Langleyfcf25832014-12-18 17:42:32 -08002642 return 1;
2643}
2644
2645int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint) {
2646 if (s == NULL) {
2647 return 0;
2648 }
2649
2650 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2651 OPENSSL_PUT_ERROR(SSL, SSL_use_psk_identity_hint,
2652 SSL_R_DATA_LENGTH_TOO_LONG);
2653 return 0;
2654 }
2655
2656 /* Clear currently configured hint, if any. */
2657 if (s->psk_identity_hint != NULL) {
2658 OPENSSL_free(s->psk_identity_hint);
2659 s->psk_identity_hint = NULL;
2660 }
2661
2662 if (identity_hint != NULL) {
2663 s->psk_identity_hint = BUF_strdup(identity_hint);
2664 if (s->psk_identity_hint == NULL) {
2665 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002666 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002667 }
Adam Langley95c29f32014-06-20 12:00:00 -07002668
Adam Langleyfcf25832014-12-18 17:42:32 -08002669 return 1;
2670}
Adam Langley95c29f32014-06-20 12:00:00 -07002671
Adam Langleyfcf25832014-12-18 17:42:32 -08002672const char *SSL_get_psk_identity_hint(const SSL *s) {
2673 if (s == NULL) {
2674 return NULL;
2675 }
2676 return s->psk_identity_hint;
2677}
Adam Langley95c29f32014-06-20 12:00:00 -07002678
Adam Langleyfcf25832014-12-18 17:42:32 -08002679const char *SSL_get_psk_identity(const SSL *s) {
2680 if (s == NULL || s->session == NULL) {
2681 return NULL;
2682 }
Adam Langley95c29f32014-06-20 12:00:00 -07002683
Adam Langleyfcf25832014-12-18 17:42:32 -08002684 return s->session->psk_identity;
2685}
Adam Langley95c29f32014-06-20 12:00:00 -07002686
Adam Langleyfcf25832014-12-18 17:42:32 -08002687void SSL_set_psk_client_callback(
2688 SSL *s, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2689 unsigned int max_identity_len, uint8_t *psk,
2690 unsigned int max_psk_len)) {
2691 s->psk_client_callback = cb;
2692}
Adam Langley95c29f32014-06-20 12:00:00 -07002693
Adam Langleyfcf25832014-12-18 17:42:32 -08002694void SSL_CTX_set_psk_client_callback(
2695 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2696 unsigned int max_identity_len,
2697 uint8_t *psk, unsigned int max_psk_len)) {
2698 ctx->psk_client_callback = cb;
2699}
Adam Langley95c29f32014-06-20 12:00:00 -07002700
Adam Langleyfcf25832014-12-18 17:42:32 -08002701void SSL_set_psk_server_callback(
2702 SSL *s, unsigned int (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
2703 unsigned int max_psk_len)) {
2704 s->psk_server_callback = cb;
2705}
Adam Langley95c29f32014-06-20 12:00:00 -07002706
Adam Langleyfcf25832014-12-18 17:42:32 -08002707void SSL_CTX_set_psk_server_callback(
2708 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *identity,
2709 uint8_t *psk, unsigned int max_psk_len)) {
2710 ctx->psk_server_callback = cb;
2711}
Adam Langley95c29f32014-06-20 12:00:00 -07002712
Adam Langleyfcf25832014-12-18 17:42:32 -08002713void SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) {
2714 ctx->min_version = version;
2715}
Adam Langley95c29f32014-06-20 12:00:00 -07002716
Adam Langleyfcf25832014-12-18 17:42:32 -08002717void SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) {
2718 ctx->max_version = version;
2719}
Adam Langley0289c732014-06-20 12:00:00 -07002720
Adam Langleyfcf25832014-12-18 17:42:32 -08002721void SSL_set_min_version(SSL *ssl, uint16_t version) {
2722 ssl->min_version = version;
2723}
Adam Langley0289c732014-06-20 12:00:00 -07002724
Adam Langleyfcf25832014-12-18 17:42:32 -08002725void SSL_set_max_version(SSL *ssl, uint16_t version) {
2726 ssl->max_version = version;
2727}
Adam Langley95c29f32014-06-20 12:00:00 -07002728
Adam Langleyfcf25832014-12-18 17:42:32 -08002729void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
2730 void (*cb)(int write_p, int version,
2731 int content_type, const void *buf,
2732 size_t len, SSL *ssl, void *arg)) {
2733 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2734}
2735void SSL_set_msg_callback(SSL *ssl,
2736 void (*cb)(int write_p, int version, int content_type,
2737 const void *buf, size_t len, SSL *ssl,
2738 void *arg)) {
2739 SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2740}
Adam Langley95c29f32014-06-20 12:00:00 -07002741
Adam Langleyfcf25832014-12-18 17:42:32 -08002742void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio) {
2743 if (ctx->keylog_bio != NULL) {
2744 BIO_free(ctx->keylog_bio);
2745 }
2746 ctx->keylog_bio = keylog_bio;
2747}
Adam Langley95c29f32014-06-20 12:00:00 -07002748
Adam Langleyfcf25832014-12-18 17:42:32 -08002749static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
2750 static const char hextable[] = "0123456789abcdef";
2751 uint8_t *out;
2752 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002753
Adam Langleyfcf25832014-12-18 17:42:32 -08002754 if (!CBB_add_space(cbb, &out, in_len * 2)) {
2755 return 0;
2756 }
Adam Langley95c29f32014-06-20 12:00:00 -07002757
Adam Langleyfcf25832014-12-18 17:42:32 -08002758 for (i = 0; i < in_len; i++) {
2759 *(out++) = (uint8_t)hextable[in[i] >> 4];
2760 *(out++) = (uint8_t)hextable[in[i] & 0xf];
2761 }
Adam Langley95c29f32014-06-20 12:00:00 -07002762
Adam Langleyfcf25832014-12-18 17:42:32 -08002763 return 1;
2764}
David Benjamin859ec3c2014-09-02 16:29:36 -04002765
2766int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -08002767 const uint8_t *encrypted_premaster,
2768 size_t encrypted_premaster_len,
2769 const uint8_t *premaster,
2770 size_t premaster_len) {
2771 BIO *bio = ctx->keylog_bio;
2772 CBB cbb;
2773 uint8_t *out;
2774 size_t out_len;
2775 int ret;
David Benjamin859ec3c2014-09-02 16:29:36 -04002776
Adam Langleyfcf25832014-12-18 17:42:32 -08002777 if (bio == NULL) {
2778 return 1;
2779 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002780
Adam Langleyfcf25832014-12-18 17:42:32 -08002781 if (encrypted_premaster_len < 8) {
2782 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_rsa_client_key_exchange,
2783 ERR_R_INTERNAL_ERROR);
2784 return 0;
2785 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002786
Adam Langleyfcf25832014-12-18 17:42:32 -08002787 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len * 2 + 1)) {
2788 return 0;
2789 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002790
Adam Langleyfcf25832014-12-18 17:42:32 -08002791 if (!CBB_add_bytes(&cbb, (const uint8_t *)"RSA ", 4) ||
2792 /* Only the first 8 bytes of the encrypted premaster secret are
2793 * logged. */
2794 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
2795 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2796 !cbb_add_hex(&cbb, premaster, premaster_len) ||
2797 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2798 !CBB_finish(&cbb, &out, &out_len)) {
2799 CBB_cleanup(&cbb);
2800 return 0;
2801 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002802
Adam Langleyfcf25832014-12-18 17:42:32 -08002803 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2804 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2805 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
David Benjamin859ec3c2014-09-02 16:29:36 -04002806
Adam Langleyfcf25832014-12-18 17:42:32 -08002807 OPENSSL_free(out);
2808 return ret;
Adam Langley95f22882014-06-20 12:00:00 -07002809}
2810
Adam Langleyfcf25832014-12-18 17:42:32 -08002811int ssl_ctx_log_master_secret(SSL_CTX *ctx, const uint8_t *client_random,
2812 size_t client_random_len, const uint8_t *master,
2813 size_t master_len) {
2814 BIO *bio = ctx->keylog_bio;
2815 CBB cbb;
2816 uint8_t *out;
2817 size_t out_len;
2818 int ret;
Adam Langleyadb739e2014-06-20 12:00:00 -07002819
Adam Langleyfcf25832014-12-18 17:42:32 -08002820 if (bio == NULL) {
2821 return 1;
2822 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002823
Adam Langleyfcf25832014-12-18 17:42:32 -08002824 if (client_random_len != 32) {
2825 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_master_secret, ERR_R_INTERNAL_ERROR);
2826 return 0;
2827 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002828
Adam Langleyfcf25832014-12-18 17:42:32 -08002829 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len * 2 + 1)) {
2830 return 0;
2831 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002832
Adam Langleyfcf25832014-12-18 17:42:32 -08002833 if (!CBB_add_bytes(&cbb, (const uint8_t *)"CLIENT_RANDOM ", 14) ||
2834 !cbb_add_hex(&cbb, client_random, 32) ||
2835 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2836 !cbb_add_hex(&cbb, master, master_len) ||
2837 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2838 !CBB_finish(&cbb, &out, &out_len)) {
2839 CBB_cleanup(&cbb);
2840 return 0;
2841 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002842
Adam Langleyfcf25832014-12-18 17:42:32 -08002843 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2844 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2845 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
David Benjamine99e9122014-12-11 01:46:01 -05002846
Adam Langleyfcf25832014-12-18 17:42:32 -08002847 OPENSSL_free(out);
2848 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -07002849}
2850
David Benjamined7c4752015-02-16 19:16:46 -05002851int SSL_in_false_start(const SSL *s) {
2852 return s->s3->tmp.in_false_start;
2853}
2854
Adam Langleyfcf25832014-12-18 17:42:32 -08002855int SSL_cutthrough_complete(const SSL *s) {
David Benjamined7c4752015-02-16 19:16:46 -05002856 return SSL_in_false_start(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002857}
Adam Langley95c29f32014-06-20 12:00:00 -07002858
Adam Langleyfcf25832014-12-18 17:42:32 -08002859void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
2860 size_t *ssl_session_size) {
2861 *ssl_size = sizeof(SSL);
2862 *ssl_ctx_size = sizeof(SSL_CTX);
2863 *ssl_session_size = sizeof(SSL_SESSION);
2864}
Feng Lu41aa3252014-11-21 22:47:56 -08002865
David Benjamined7c4752015-02-16 19:16:46 -05002866int ssl3_can_false_start(const SSL *s) {
David Benjamin195dc782015-02-19 13:27:05 -05002867 const SSL_CIPHER *const cipher = SSL_get_current_cipher(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002868
Adam Langleye631d962015-04-01 13:11:01 -07002869 /* False Start only for TLS 1.2 with an ECDHE+AEAD cipher and ALPN or NPN. */
David Benjamin195dc782015-02-19 13:27:05 -05002870 return !SSL_IS_DTLS(s) &&
2871 SSL_version(s) >= TLS1_2_VERSION &&
2872 (s->s3->alpn_selected || s->s3->next_proto_neg_seen) &&
2873 cipher != NULL &&
Adam Langleye631d962015-04-01 13:11:01 -07002874 cipher->algorithm_mkey == SSL_kECDHE &&
David Benjamin195dc782015-02-19 13:27:05 -05002875 (cipher->algorithm_enc == SSL_AES128GCM ||
2876 cipher->algorithm_enc == SSL_AES256GCM ||
2877 cipher->algorithm_enc == SSL_CHACHA20POLY1305);
Adam Langleyfcf25832014-12-18 17:42:32 -08002878}
2879
2880const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version) {
2881 switch (version) {
2882 case SSL3_VERSION:
2883 return &SSLv3_enc_data;
2884
2885 case TLS1_VERSION:
2886 return &TLSv1_enc_data;
2887
David Benjamin9e13e1a2015-03-05 01:56:32 -05002888 case DTLS1_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002889 case TLS1_1_VERSION:
2890 return &TLSv1_1_enc_data;
2891
David Benjamin9e13e1a2015-03-05 01:56:32 -05002892 case DTLS1_2_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002893 case TLS1_2_VERSION:
2894 return &TLSv1_2_enc_data;
2895
Adam Langleyfcf25832014-12-18 17:42:32 -08002896 default:
2897 return NULL;
2898 }
2899}
2900
2901uint16_t ssl3_get_max_server_version(const SSL *s) {
2902 uint16_t max_version;
2903
2904 if (SSL_IS_DTLS(s)) {
2905 max_version = (s->max_version != 0) ? s->max_version : DTLS1_2_VERSION;
2906 if (!(s->options & SSL_OP_NO_DTLSv1_2) && DTLS1_2_VERSION >= max_version) {
2907 return DTLS1_2_VERSION;
2908 }
2909 if (!(s->options & SSL_OP_NO_DTLSv1) && DTLS1_VERSION >= max_version) {
2910 return DTLS1_VERSION;
2911 }
2912 return 0;
2913 }
2914
2915 max_version = (s->max_version != 0) ? s->max_version : TLS1_2_VERSION;
2916 if (!(s->options & SSL_OP_NO_TLSv1_2) && TLS1_2_VERSION <= max_version) {
2917 return TLS1_2_VERSION;
2918 }
2919 if (!(s->options & SSL_OP_NO_TLSv1_1) && TLS1_1_VERSION <= max_version) {
2920 return TLS1_1_VERSION;
2921 }
2922 if (!(s->options & SSL_OP_NO_TLSv1) && TLS1_VERSION <= max_version) {
2923 return TLS1_VERSION;
2924 }
2925 if (!(s->options & SSL_OP_NO_SSLv3) && SSL3_VERSION <= max_version) {
2926 return SSL3_VERSION;
2927 }
2928 return 0;
2929}
2930
2931uint16_t ssl3_get_mutual_version(SSL *s, uint16_t client_version) {
2932 uint16_t version = 0;
2933
2934 if (SSL_IS_DTLS(s)) {
2935 /* Clamp client_version to max_version. */
2936 if (s->max_version != 0 && client_version < s->max_version) {
2937 client_version = s->max_version;
2938 }
2939
2940 if (client_version <= DTLS1_2_VERSION && !(s->options & SSL_OP_NO_DTLSv1_2)) {
2941 version = DTLS1_2_VERSION;
2942 } else if (client_version <= DTLS1_VERSION &&
David Benjaminb18f0242015-03-10 18:30:08 -04002943 !(s->options & SSL_OP_NO_DTLSv1)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002944 version = DTLS1_VERSION;
2945 }
2946
2947 /* Check against min_version. */
2948 if (version != 0 && s->min_version != 0 && version > s->min_version) {
2949 return 0;
2950 }
2951 return version;
2952 } else {
2953 /* Clamp client_version to max_version. */
2954 if (s->max_version != 0 && client_version > s->max_version) {
2955 client_version = s->max_version;
2956 }
2957
2958 if (client_version >= TLS1_2_VERSION && !(s->options & SSL_OP_NO_TLSv1_2)) {
2959 version = TLS1_2_VERSION;
2960 } else if (client_version >= TLS1_1_VERSION &&
2961 !(s->options & SSL_OP_NO_TLSv1_1)) {
2962 version = TLS1_1_VERSION;
2963 } else if (client_version >= TLS1_VERSION && !(s->options & SSL_OP_NO_TLSv1)) {
2964 version = TLS1_VERSION;
2965 } else if (client_version >= SSL3_VERSION && !(s->options & SSL_OP_NO_SSLv3)) {
2966 version = SSL3_VERSION;
2967 }
2968
2969 /* Check against min_version. */
2970 if (version != 0 && s->min_version != 0 && version < s->min_version) {
2971 return 0;
2972 }
2973 return version;
2974 }
2975}
2976
2977uint16_t ssl3_get_max_client_version(SSL *s) {
2978 unsigned long options = s->options;
2979 uint16_t version = 0;
2980
2981 /* OpenSSL's API for controlling versions entails blacklisting individual
2982 * protocols. This has two problems. First, on the client, the protocol can
2983 * only express a contiguous range of versions. Second, a library consumer
2984 * trying to set a maximum version cannot disable protocol versions that get
2985 * added in a future version of the library.
2986 *
2987 * To account for both of these, OpenSSL interprets the client-side bitmask
2988 * as a min/max range by picking the lowest contiguous non-empty range of
2989 * enabled protocols. Note that this means it is impossible to set a maximum
2990 * version of TLS 1.2 in a future-proof way.
2991 *
2992 * By this scheme, the maximum version is the lowest version V such that V is
2993 * enabled and V+1 is disabled or unimplemented. */
2994 if (SSL_IS_DTLS(s)) {
2995 if (!(options & SSL_OP_NO_DTLSv1_2)) {
2996 version = DTLS1_2_VERSION;
2997 }
2998 if (!(options & SSL_OP_NO_DTLSv1) && (options & SSL_OP_NO_DTLSv1_2)) {
2999 version = DTLS1_VERSION;
3000 }
3001 if (s->max_version != 0 && version < s->max_version) {
3002 version = s->max_version;
3003 }
3004 } else {
3005 if (!(options & SSL_OP_NO_TLSv1_2)) {
3006 version = TLS1_2_VERSION;
3007 }
3008 if (!(options & SSL_OP_NO_TLSv1_1) && (options & SSL_OP_NO_TLSv1_2)) {
3009 version = TLS1_1_VERSION;
3010 }
3011 if (!(options & SSL_OP_NO_TLSv1) && (options & SSL_OP_NO_TLSv1_1)) {
3012 version = TLS1_VERSION;
3013 }
3014 if (!(options & SSL_OP_NO_SSLv3) && (options & SSL_OP_NO_TLSv1)) {
3015 version = SSL3_VERSION;
3016 }
3017 if (s->max_version != 0 && version > s->max_version) {
3018 version = s->max_version;
3019 }
3020 }
3021
3022 return version;
3023}
3024
3025int ssl3_is_version_enabled(SSL *s, uint16_t version) {
3026 if (SSL_IS_DTLS(s)) {
3027 if (s->max_version != 0 && version < s->max_version) {
3028 return 0;
3029 }
3030 if (s->min_version != 0 && version > s->min_version) {
3031 return 0;
3032 }
3033
3034 switch (version) {
3035 case DTLS1_VERSION:
3036 return !(s->options & SSL_OP_NO_DTLSv1);
3037
3038 case DTLS1_2_VERSION:
3039 return !(s->options & SSL_OP_NO_DTLSv1_2);
3040
3041 default:
3042 return 0;
3043 }
3044 } else {
3045 if (s->max_version != 0 && version > s->max_version) {
3046 return 0;
3047 }
3048 if (s->min_version != 0 && version < s->min_version) {
3049 return 0;
3050 }
3051
3052 switch (version) {
3053 case SSL3_VERSION:
3054 return !(s->options & SSL_OP_NO_SSLv3);
3055
3056 case TLS1_VERSION:
3057 return !(s->options & SSL_OP_NO_TLSv1);
3058
3059 case TLS1_1_VERSION:
3060 return !(s->options & SSL_OP_NO_TLSv1_1);
3061
3062 case TLS1_2_VERSION:
3063 return !(s->options & SSL_OP_NO_TLSv1_2);
3064
3065 default:
3066 return 0;
3067 }
3068 }
3069}
3070
David Benjaminea72bd02014-12-21 21:27:41 -05003071uint16_t ssl3_version_from_wire(SSL *s, uint16_t wire_version) {
3072 if (!SSL_IS_DTLS(s)) {
3073 return wire_version;
3074 }
3075
3076 uint16_t tls_version = ~wire_version;
3077 uint16_t version = tls_version + 0x0201;
3078 /* If either component overflowed, clamp it so comparisons still work. */
3079 if ((version >> 8) < (tls_version >> 8)) {
3080 version = 0xff00 | (version & 0xff);
3081 }
3082 if ((version & 0xff) < (tls_version & 0xff)) {
3083 version = (version & 0xff00) | 0xff;
3084 }
3085 /* DTLS 1.0 maps to TLS 1.1, not TLS 1.0. */
3086 if (version == TLS1_VERSION) {
3087 version = TLS1_1_VERSION;
3088 }
3089 return version;
3090}
3091
Adam Langleyfcf25832014-12-18 17:42:32 -08003092int SSL_cache_hit(SSL *s) { return s->hit; }
3093
3094int SSL_is_server(SSL *s) { return s->server; }
3095
Adam Langley524e7172015-02-20 16:04:00 -08003096void SSL_CTX_set_dos_protection_cb(
3097 SSL_CTX *ctx, int (*cb)(const struct ssl_early_callback_ctx *)) {
3098 ctx->dos_protection_cb = cb;
3099}
3100
Adam Langleyfcf25832014-12-18 17:42:32 -08003101void SSL_enable_fastradio_padding(SSL *s, char on_off) {
3102 s->fastradio_padding = on_off;
3103}
Adam Langley44972942015-02-20 13:43:23 -08003104
David Benjaminb16346b2015-04-08 19:16:58 -04003105void SSL_set_reject_peer_renegotiations(SSL *s, int reject) {
3106 s->reject_peer_renegotiations = !!reject;
3107}
3108
Adam Langley44972942015-02-20 13:43:23 -08003109const SSL_CIPHER *SSL_get_cipher_by_value(uint16_t value) {
3110 return ssl3_get_cipher_by_value(value);
3111}
Adam Langley3f92d212015-02-20 15:32:52 -08003112
3113int SSL_get_rc4_state(const SSL *ssl, const RC4_KEY **read_key,
3114 const RC4_KEY **write_key) {
3115 if (ssl->aead_read_ctx == NULL || ssl->aead_write_ctx == NULL) {
3116 return 0;
3117 }
3118
3119 return EVP_AEAD_CTX_get_rc4_state(&ssl->aead_read_ctx->ctx, read_key) &&
3120 EVP_AEAD_CTX_get_rc4_state(&ssl->aead_write_ctx->ctx, write_key);
3121}