blob: 97a94a2efc4b3bd70723b64661c2c67cbe1014d8 [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) {
463 sk_SSL_CIPHER_free(cipher_list->ciphers);
464 OPENSSL_free(cipher_list->in_group_flags);
465 OPENSSL_free(cipher_list);
466}
Adam Langley858a88d2014-06-20 12:00:00 -0700467
Adam Langleyfcf25832014-12-18 17:42:32 -0800468struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_dup(
469 struct ssl_cipher_preference_list_st *cipher_list) {
470 struct ssl_cipher_preference_list_st *ret = NULL;
471 size_t n = sk_SSL_CIPHER_num(cipher_list->ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700472
Adam Langleyfcf25832014-12-18 17:42:32 -0800473 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
474 if (!ret) {
475 goto err;
476 }
477
478 ret->ciphers = NULL;
479 ret->in_group_flags = NULL;
480 ret->ciphers = sk_SSL_CIPHER_dup(cipher_list->ciphers);
481 if (!ret->ciphers) {
482 goto err;
483 }
484 ret->in_group_flags = BUF_memdup(cipher_list->in_group_flags, n);
485 if (!ret->in_group_flags) {
486 goto err;
487 }
488
489 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -0700490
491err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800492 if (ret && ret->ciphers) {
493 sk_SSL_CIPHER_free(ret->ciphers);
494 }
495 if (ret) {
496 OPENSSL_free(ret);
497 }
498 return NULL;
499}
Adam Langley858a88d2014-06-20 12:00:00 -0700500
Adam Langleyfcf25832014-12-18 17:42:32 -0800501struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_from_ciphers(
502 STACK_OF(SSL_CIPHER) * ciphers) {
503 struct ssl_cipher_preference_list_st *ret = NULL;
504 size_t n = sk_SSL_CIPHER_num(ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700505
Adam Langleyfcf25832014-12-18 17:42:32 -0800506 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
507 if (!ret) {
508 goto err;
509 }
510 ret->ciphers = NULL;
511 ret->in_group_flags = NULL;
512 ret->ciphers = sk_SSL_CIPHER_dup(ciphers);
513 if (!ret->ciphers) {
514 goto err;
515 }
516 ret->in_group_flags = OPENSSL_malloc(n);
517 if (!ret->in_group_flags) {
518 goto err;
519 }
520 memset(ret->in_group_flags, 0, n);
521 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -0700522
523err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800524 if (ret && ret->ciphers) {
525 sk_SSL_CIPHER_free(ret->ciphers);
526 }
527 if (ret) {
528 OPENSSL_free(ret);
529 }
530 return NULL;
531}
Adam Langley858a88d2014-06-20 12:00:00 -0700532
Adam Langleyfcf25832014-12-18 17:42:32 -0800533X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) { return ctx->param; }
Adam Langley95c29f32014-06-20 12:00:00 -0700534
Adam Langleyfcf25832014-12-18 17:42:32 -0800535X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) { return ssl->param; }
Adam Langley95c29f32014-06-20 12:00:00 -0700536
Adam Langleyfcf25832014-12-18 17:42:32 -0800537void SSL_certs_clear(SSL *s) { ssl_cert_clear_certs(s->cert); }
Adam Langley95c29f32014-06-20 12:00:00 -0700538
Adam Langleyfcf25832014-12-18 17:42:32 -0800539void SSL_free(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800540 if (s == NULL) {
541 return;
542 }
Adam Langley95c29f32014-06-20 12:00:00 -0700543
Adam Langleyfcf25832014-12-18 17:42:32 -0800544 if (s->param) {
545 X509_VERIFY_PARAM_free(s->param);
546 }
Adam Langley95c29f32014-06-20 12:00:00 -0700547
David Benjamin9f33fc62015-04-15 17:29:53 -0400548 CRYPTO_free_ex_data(&g_ex_data_class_ssl, s, &s->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700549
Adam Langleyfcf25832014-12-18 17:42:32 -0800550 if (s->bbio != NULL) {
551 /* If the buffering BIO is in place, pop it off */
552 if (s->bbio == s->wbio) {
553 s->wbio = BIO_pop(s->wbio);
554 }
555 BIO_free(s->bbio);
556 s->bbio = NULL;
557 }
Adam Langley95c29f32014-06-20 12:00:00 -0700558
Adam Langleyfcf25832014-12-18 17:42:32 -0800559 if (s->rbio != NULL) {
560 BIO_free_all(s->rbio);
561 }
Adam Langley95c29f32014-06-20 12:00:00 -0700562
Adam Langleyfcf25832014-12-18 17:42:32 -0800563 if (s->wbio != NULL && s->wbio != s->rbio) {
564 BIO_free_all(s->wbio);
565 }
Adam Langley95c29f32014-06-20 12:00:00 -0700566
Adam Langleyfcf25832014-12-18 17:42:32 -0800567 if (s->init_buf != NULL) {
568 BUF_MEM_free(s->init_buf);
569 }
Adam Langley95c29f32014-06-20 12:00:00 -0700570
Adam Langleyfcf25832014-12-18 17:42:32 -0800571 /* add extra stuff */
572 if (s->cipher_list != NULL) {
573 ssl_cipher_preference_list_free(s->cipher_list);
574 }
575 if (s->cipher_list_by_id != NULL) {
576 sk_SSL_CIPHER_free(s->cipher_list_by_id);
577 }
Adam Langley95c29f32014-06-20 12:00:00 -0700578
Adam Langleyfcf25832014-12-18 17:42:32 -0800579 if (s->session != NULL) {
580 ssl_clear_bad_session(s);
581 SSL_SESSION_free(s->session);
582 }
Adam Langley95c29f32014-06-20 12:00:00 -0700583
Adam Langleyfcf25832014-12-18 17:42:32 -0800584 ssl_clear_cipher_ctx(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700585
Adam Langleyfcf25832014-12-18 17:42:32 -0800586 if (s->cert != NULL) {
587 ssl_cert_free(s->cert);
588 }
Adam Langley0289c732014-06-20 12:00:00 -0700589
Adam Langleyfcf25832014-12-18 17:42:32 -0800590 if (s->tlsext_hostname) {
591 OPENSSL_free(s->tlsext_hostname);
592 }
593 if (s->initial_ctx) {
594 SSL_CTX_free(s->initial_ctx);
595 }
596 if (s->tlsext_ecpointformatlist) {
597 OPENSSL_free(s->tlsext_ecpointformatlist);
598 }
599 if (s->tlsext_ellipticcurvelist) {
600 OPENSSL_free(s->tlsext_ellipticcurvelist);
601 }
602 if (s->alpn_client_proto_list) {
603 OPENSSL_free(s->alpn_client_proto_list);
604 }
605 if (s->tlsext_channel_id_private) {
606 EVP_PKEY_free(s->tlsext_channel_id_private);
607 }
608 if (s->psk_identity_hint) {
609 OPENSSL_free(s->psk_identity_hint);
610 }
611 if (s->client_CA != NULL) {
612 sk_X509_NAME_pop_free(s->client_CA, X509_NAME_free);
613 }
614 if (s->next_proto_negotiated) {
615 OPENSSL_free(s->next_proto_negotiated);
616 }
617 if (s->srtp_profiles) {
618 sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);
619 }
Adam Langley95c29f32014-06-20 12:00:00 -0700620
Adam Langleyfcf25832014-12-18 17:42:32 -0800621 if (s->method != NULL) {
622 s->method->ssl_free(s);
623 }
624 if (s->ctx) {
625 SSL_CTX_free(s->ctx);
626 }
Adam Langley95c29f32014-06-20 12:00:00 -0700627
Adam Langleyfcf25832014-12-18 17:42:32 -0800628 OPENSSL_free(s);
629}
Adam Langley95c29f32014-06-20 12:00:00 -0700630
Adam Langleyfcf25832014-12-18 17:42:32 -0800631void SSL_set_bio(SSL *s, BIO *rbio, BIO *wbio) {
632 /* If the output buffering BIO is still in place, remove it. */
633 if (s->bbio != NULL) {
634 if (s->wbio == s->bbio) {
635 s->wbio = s->wbio->next_bio;
636 s->bbio->next_bio = NULL;
637 }
638 }
Adam Langley95c29f32014-06-20 12:00:00 -0700639
Adam Langleyfcf25832014-12-18 17:42:32 -0800640 if (s->rbio != NULL && s->rbio != rbio) {
641 BIO_free_all(s->rbio);
642 }
643 if (s->wbio != NULL && s->wbio != wbio && s->rbio != s->wbio) {
644 BIO_free_all(s->wbio);
645 }
646 s->rbio = rbio;
647 s->wbio = wbio;
648}
Adam Langley95c29f32014-06-20 12:00:00 -0700649
Adam Langleyfcf25832014-12-18 17:42:32 -0800650BIO *SSL_get_rbio(const SSL *s) { return s->rbio; }
Adam Langley95c29f32014-06-20 12:00:00 -0700651
Adam Langleyfcf25832014-12-18 17:42:32 -0800652BIO *SSL_get_wbio(const SSL *s) { return s->wbio; }
Adam Langley95c29f32014-06-20 12:00:00 -0700653
Adam Langleyfcf25832014-12-18 17:42:32 -0800654int SSL_get_fd(const SSL *s) { return SSL_get_rfd(s); }
Adam Langley95c29f32014-06-20 12:00:00 -0700655
Adam Langleyfcf25832014-12-18 17:42:32 -0800656int SSL_get_rfd(const SSL *s) {
657 int ret = -1;
658 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700659
Adam Langleyfcf25832014-12-18 17:42:32 -0800660 b = SSL_get_rbio(s);
661 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
662 if (r != NULL) {
663 BIO_get_fd(r, &ret);
664 }
665 return ret;
666}
Adam Langley95c29f32014-06-20 12:00:00 -0700667
Adam Langleyfcf25832014-12-18 17:42:32 -0800668int SSL_get_wfd(const SSL *s) {
669 int ret = -1;
670 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700671
Adam Langleyfcf25832014-12-18 17:42:32 -0800672 b = SSL_get_wbio(s);
673 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
674 if (r != NULL) {
675 BIO_get_fd(r, &ret);
676 }
Adam Langley95c29f32014-06-20 12:00:00 -0700677
Adam Langleyfcf25832014-12-18 17:42:32 -0800678 return ret;
679}
Adam Langley95c29f32014-06-20 12:00:00 -0700680
Adam Langleyfcf25832014-12-18 17:42:32 -0800681int SSL_set_fd(SSL *s, int fd) {
682 int ret = 0;
683 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700684
Adam Langleyfcf25832014-12-18 17:42:32 -0800685 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700686
Adam Langleyfcf25832014-12-18 17:42:32 -0800687 if (bio == NULL) {
688 OPENSSL_PUT_ERROR(SSL, SSL_set_fd, ERR_R_BUF_LIB);
689 goto err;
690 }
691 BIO_set_fd(bio, fd, BIO_NOCLOSE);
692 SSL_set_bio(s, bio, bio);
693 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700694
Adam Langley95c29f32014-06-20 12:00:00 -0700695err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800696 return ret;
697}
Adam Langley95c29f32014-06-20 12:00:00 -0700698
Adam Langleyfcf25832014-12-18 17:42:32 -0800699int SSL_set_wfd(SSL *s, int fd) {
700 int ret = 0;
701 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700702
Adam Langleyfcf25832014-12-18 17:42:32 -0800703 if (s->rbio == NULL || BIO_method_type(s->rbio) != BIO_TYPE_FD ||
704 (int)BIO_get_fd(s->rbio, NULL) != fd) {
705 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700706
Adam Langleyfcf25832014-12-18 17:42:32 -0800707 if (bio == NULL) {
708 OPENSSL_PUT_ERROR(SSL, SSL_set_wfd, ERR_R_BUF_LIB);
709 goto err;
710 }
711 BIO_set_fd(bio, fd, BIO_NOCLOSE);
712 SSL_set_bio(s, SSL_get_rbio(s), bio);
713 } else {
714 SSL_set_bio(s, SSL_get_rbio(s), SSL_get_rbio(s));
715 }
716
717 ret = 1;
718
Adam Langley95c29f32014-06-20 12:00:00 -0700719err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800720 return ret;
721}
Adam Langley95c29f32014-06-20 12:00:00 -0700722
Adam Langleyfcf25832014-12-18 17:42:32 -0800723int SSL_set_rfd(SSL *s, int fd) {
724 int ret = 0;
725 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700726
Adam Langleyfcf25832014-12-18 17:42:32 -0800727 if (s->wbio == NULL || BIO_method_type(s->wbio) != BIO_TYPE_FD ||
728 (int)BIO_get_fd(s->wbio, NULL) != fd) {
729 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700730
Adam Langleyfcf25832014-12-18 17:42:32 -0800731 if (bio == NULL) {
732 OPENSSL_PUT_ERROR(SSL, SSL_set_rfd, ERR_R_BUF_LIB);
733 goto err;
734 }
735 BIO_set_fd(bio, fd, BIO_NOCLOSE);
736 SSL_set_bio(s, bio, SSL_get_wbio(s));
737 } else {
738 SSL_set_bio(s, SSL_get_wbio(s), SSL_get_wbio(s));
739 }
740 ret = 1;
741
Adam Langley95c29f32014-06-20 12:00:00 -0700742err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800743 return ret;
744}
Adam Langley95c29f32014-06-20 12:00:00 -0700745
746/* return length of latest Finished message we sent, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -0800747size_t SSL_get_finished(const SSL *s, void *buf, size_t count) {
748 size_t ret = 0;
749
750 if (s->s3 != NULL) {
751 ret = s->s3->tmp.finish_md_len;
752 if (count > ret) {
753 count = ret;
754 }
755 memcpy(buf, s->s3->tmp.finish_md, count);
756 }
757
758 return ret;
759}
Adam Langley95c29f32014-06-20 12:00:00 -0700760
761/* return length of latest Finished message we expected, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -0800762size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count) {
763 size_t ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700764
Adam Langleyfcf25832014-12-18 17:42:32 -0800765 if (s->s3 != NULL) {
766 ret = s->s3->tmp.peer_finish_md_len;
767 if (count > ret) {
768 count = ret;
769 }
770 memcpy(buf, s->s3->tmp.peer_finish_md, count);
771 }
Adam Langley95c29f32014-06-20 12:00:00 -0700772
Adam Langleyfcf25832014-12-18 17:42:32 -0800773 return ret;
774}
Adam Langley95c29f32014-06-20 12:00:00 -0700775
Adam Langleyfcf25832014-12-18 17:42:32 -0800776int SSL_get_verify_mode(const SSL *s) { return s->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -0700777
Adam Langleyfcf25832014-12-18 17:42:32 -0800778int SSL_get_verify_depth(const SSL *s) {
779 return X509_VERIFY_PARAM_get_depth(s->param);
780}
Adam Langley95c29f32014-06-20 12:00:00 -0700781
Adam Langleyfcf25832014-12-18 17:42:32 -0800782int (*SSL_get_verify_callback(const SSL *s))(int, X509_STORE_CTX *) {
783 return s->verify_callback;
784}
Adam Langley95c29f32014-06-20 12:00:00 -0700785
Adam Langleyfcf25832014-12-18 17:42:32 -0800786int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) { return ctx->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -0700787
Adam Langleyfcf25832014-12-18 17:42:32 -0800788int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) {
789 return X509_VERIFY_PARAM_get_depth(ctx->param);
790}
Adam Langley95c29f32014-06-20 12:00:00 -0700791
Adam Langleyfcf25832014-12-18 17:42:32 -0800792int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int, X509_STORE_CTX *) {
793 return ctx->default_verify_callback;
794}
Adam Langley95c29f32014-06-20 12:00:00 -0700795
Adam Langleyfcf25832014-12-18 17:42:32 -0800796void SSL_set_verify(SSL *s, int mode,
797 int (*callback)(int ok, X509_STORE_CTX *ctx)) {
798 s->verify_mode = mode;
799 if (callback != NULL) {
800 s->verify_callback = callback;
801 }
802}
Adam Langley95c29f32014-06-20 12:00:00 -0700803
Adam Langleyfcf25832014-12-18 17:42:32 -0800804void SSL_set_verify_depth(SSL *s, int depth) {
805 X509_VERIFY_PARAM_set_depth(s->param, depth);
806}
Adam Langley95c29f32014-06-20 12:00:00 -0700807
Adam Langleyfcf25832014-12-18 17:42:32 -0800808void SSL_set_read_ahead(SSL *s, int yes) { s->read_ahead = yes; }
Adam Langley95c29f32014-06-20 12:00:00 -0700809
Adam Langleyfcf25832014-12-18 17:42:32 -0800810int SSL_get_read_ahead(const SSL *s) { return s->read_ahead; }
Adam Langley95c29f32014-06-20 12:00:00 -0700811
Adam Langleyfcf25832014-12-18 17:42:32 -0800812int SSL_pending(const SSL *s) {
813 /* SSL_pending cannot work properly if read-ahead is enabled
814 * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)), and it is
815 * impossible to fix since SSL_pending cannot report errors that may be
816 * observed while scanning the new data. (Note that SSL_pending() is often
817 * used as a boolean value, so we'd better not return -1.). */
818 return s->method->ssl_pending(s);
819}
Adam Langley95c29f32014-06-20 12:00:00 -0700820
Adam Langleyfcf25832014-12-18 17:42:32 -0800821X509 *SSL_get_peer_certificate(const SSL *s) {
822 X509 *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700823
Adam Langleyfcf25832014-12-18 17:42:32 -0800824 if (s == NULL || s->session == NULL) {
825 r = NULL;
826 } else {
827 r = s->session->peer;
828 }
Adam Langley95c29f32014-06-20 12:00:00 -0700829
Adam Langleyfcf25832014-12-18 17:42:32 -0800830 if (r == NULL) {
831 return NULL;
832 }
Adam Langley95c29f32014-06-20 12:00:00 -0700833
Adam Langleyfcf25832014-12-18 17:42:32 -0800834 return X509_up_ref(r);
835}
836
837STACK_OF(X509) * SSL_get_peer_cert_chain(const SSL *s) {
838 STACK_OF(X509) * r;
839
840 if (s == NULL || s->session == NULL || s->session->sess_cert == NULL) {
841 r = NULL;
842 } else {
843 r = s->session->sess_cert->cert_chain;
844 }
845
846 /* If we are a client, cert_chain includes the peer's own certificate; if we
847 * are a server, it does not. */
848 return r;
849}
Adam Langley95c29f32014-06-20 12:00:00 -0700850
Adam Langley95c29f32014-06-20 12:00:00 -0700851/* Fix this so it checks all the valid key/cert options */
Adam Langleyfcf25832014-12-18 17:42:32 -0800852int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
853 if (ctx == NULL || ctx->cert == NULL || ctx->cert->key->x509 == NULL) {
854 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key,
855 SSL_R_NO_CERTIFICATE_ASSIGNED);
856 return 0;
857 }
858
859 if (ctx->cert->key->privatekey == NULL) {
860 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key,
861 SSL_R_NO_PRIVATE_KEY_ASSIGNED);
862 return 0;
863 }
864
865 return X509_check_private_key(ctx->cert->key->x509,
866 ctx->cert->key->privatekey);
867}
Adam Langley95c29f32014-06-20 12:00:00 -0700868
869/* Fix this function so that it takes an optional type parameter */
Adam Langleyfcf25832014-12-18 17:42:32 -0800870int SSL_check_private_key(const SSL *ssl) {
871 if (ssl == NULL) {
872 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, ERR_R_PASSED_NULL_PARAMETER);
873 return 0;
874 }
Adam Langley95c29f32014-06-20 12:00:00 -0700875
Adam Langleyfcf25832014-12-18 17:42:32 -0800876 if (ssl->cert == NULL) {
877 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
878 SSL_R_NO_CERTIFICATE_ASSIGNED);
879 return 0;
880 }
Adam Langley95c29f32014-06-20 12:00:00 -0700881
Adam Langleyfcf25832014-12-18 17:42:32 -0800882 if (ssl->cert->key->x509 == NULL) {
883 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
884 SSL_R_NO_CERTIFICATE_ASSIGNED);
885 return 0;
886 }
David Benjamin0b145c22014-11-26 20:10:09 -0500887
Adam Langleyfcf25832014-12-18 17:42:32 -0800888 if (ssl->cert->key->privatekey == NULL) {
889 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
890 SSL_R_NO_PRIVATE_KEY_ASSIGNED);
891 return 0;
892 }
Adam Langley95c29f32014-06-20 12:00:00 -0700893
Adam Langleyfcf25832014-12-18 17:42:32 -0800894 return X509_check_private_key(ssl->cert->key->x509,
895 ssl->cert->key->privatekey);
896}
Adam Langley95c29f32014-06-20 12:00:00 -0700897
Adam Langleyfcf25832014-12-18 17:42:32 -0800898int SSL_accept(SSL *s) {
899 if (s->handshake_func == 0) {
900 /* Not properly initialized yet */
901 SSL_set_accept_state(s);
902 }
David Benjamin0b145c22014-11-26 20:10:09 -0500903
Adam Langleyfcf25832014-12-18 17:42:32 -0800904 if (s->handshake_func != s->method->ssl_accept) {
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +0100905 OPENSSL_PUT_ERROR(SSL, SSL_accept, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800906 return -1;
907 }
Adam Langley95c29f32014-06-20 12:00:00 -0700908
Adam Langleyfcf25832014-12-18 17:42:32 -0800909 return s->handshake_func(s);
910}
Adam Langley95c29f32014-06-20 12:00:00 -0700911
Adam Langleyfcf25832014-12-18 17:42:32 -0800912int SSL_connect(SSL *s) {
913 if (s->handshake_func == 0) {
914 /* Not properly initialized yet */
915 SSL_set_connect_state(s);
916 }
Adam Langley95c29f32014-06-20 12:00:00 -0700917
Adam Langleyfcf25832014-12-18 17:42:32 -0800918 if (s->handshake_func != s->method->ssl_connect) {
919 OPENSSL_PUT_ERROR(SSL, SSL_connect, ERR_R_INTERNAL_ERROR);
920 return -1;
921 }
Adam Langley95c29f32014-06-20 12:00:00 -0700922
Adam Langleyfcf25832014-12-18 17:42:32 -0800923 return s->handshake_func(s);
924}
Adam Langley95c29f32014-06-20 12:00:00 -0700925
Adam Langleyfcf25832014-12-18 17:42:32 -0800926long SSL_get_default_timeout(const SSL *s) {
927 return SSL_DEFAULT_SESSION_TIMEOUT;
928}
Adam Langley95c29f32014-06-20 12:00:00 -0700929
Adam Langleyfcf25832014-12-18 17:42:32 -0800930int SSL_read(SSL *s, void *buf, int num) {
931 if (s->handshake_func == 0) {
932 OPENSSL_PUT_ERROR(SSL, SSL_read, SSL_R_UNINITIALIZED);
933 return -1;
934 }
Adam Langley95c29f32014-06-20 12:00:00 -0700935
Adam Langleyfcf25832014-12-18 17:42:32 -0800936 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
937 s->rwstate = SSL_NOTHING;
938 return 0;
939 }
Adam Langley95c29f32014-06-20 12:00:00 -0700940
Adam Langleyfcf25832014-12-18 17:42:32 -0800941 return s->method->ssl_read(s, buf, num);
942}
Adam Langley95c29f32014-06-20 12:00:00 -0700943
Adam Langleyfcf25832014-12-18 17:42:32 -0800944int SSL_peek(SSL *s, void *buf, int num) {
945 if (s->handshake_func == 0) {
946 OPENSSL_PUT_ERROR(SSL, SSL_peek, SSL_R_UNINITIALIZED);
947 return -1;
948 }
Adam Langley95c29f32014-06-20 12:00:00 -0700949
Adam Langleyfcf25832014-12-18 17:42:32 -0800950 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
951 return 0;
952 }
Adam Langley95c29f32014-06-20 12:00:00 -0700953
Adam Langleyfcf25832014-12-18 17:42:32 -0800954 return s->method->ssl_peek(s, buf, num);
955}
Adam Langley95c29f32014-06-20 12:00:00 -0700956
Adam Langleyfcf25832014-12-18 17:42:32 -0800957int SSL_write(SSL *s, const void *buf, int num) {
958 if (s->handshake_func == 0) {
959 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_UNINITIALIZED);
960 return -1;
961 }
Adam Langley95c29f32014-06-20 12:00:00 -0700962
Adam Langleyfcf25832014-12-18 17:42:32 -0800963 if (s->shutdown & SSL_SENT_SHUTDOWN) {
964 s->rwstate = SSL_NOTHING;
965 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_PROTOCOL_IS_SHUTDOWN);
966 return -1;
967 }
Adam Langley95c29f32014-06-20 12:00:00 -0700968
Adam Langleyfcf25832014-12-18 17:42:32 -0800969 return s->method->ssl_write(s, buf, num);
970}
Adam Langley95c29f32014-06-20 12:00:00 -0700971
Adam Langleyfcf25832014-12-18 17:42:32 -0800972int SSL_shutdown(SSL *s) {
973 /* Note that this function behaves differently from what one might expect.
974 * Return values are 0 for no success (yet), 1 for success; but calling it
975 * once is usually not enough, even if blocking I/O is used (see
976 * ssl3_shutdown). */
Adam Langley95c29f32014-06-20 12:00:00 -0700977
Adam Langleyfcf25832014-12-18 17:42:32 -0800978 if (s->handshake_func == 0) {
979 OPENSSL_PUT_ERROR(SSL, SSL_shutdown, SSL_R_UNINITIALIZED);
980 return -1;
981 }
Adam Langley95c29f32014-06-20 12:00:00 -0700982
Adam Langleyfcf25832014-12-18 17:42:32 -0800983 if (!SSL_in_init(s)) {
984 return s->method->ssl_shutdown(s);
985 }
Adam Langley95c29f32014-06-20 12:00:00 -0700986
Adam Langleyfcf25832014-12-18 17:42:32 -0800987 return 1;
988}
Adam Langley95c29f32014-06-20 12:00:00 -0700989
Adam Langleyfcf25832014-12-18 17:42:32 -0800990int SSL_renegotiate(SSL *s) {
David Benjamin2cdace92015-01-24 13:30:02 -0500991 if (SSL_IS_DTLS(s)) {
992 /* Renegotiation is not supported for DTLS. */
993 OPENSSL_PUT_ERROR(SSL, SSL_renegotiate, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
994 return 0;
995 }
996
Adam Langleyfcf25832014-12-18 17:42:32 -0800997 if (s->renegotiate == 0) {
998 s->renegotiate = 1;
999 }
Adam Langley95c29f32014-06-20 12:00:00 -07001000
Adam Langleyfcf25832014-12-18 17:42:32 -08001001 s->new_session = 1;
1002 return s->method->ssl_renegotiate(s);
1003}
Adam Langley95c29f32014-06-20 12:00:00 -07001004
Adam Langleyfcf25832014-12-18 17:42:32 -08001005int SSL_renegotiate_pending(SSL *s) {
1006 /* becomes true when negotiation is requested; false again once a handshake
1007 * has finished */
1008 return s->renegotiate != 0;
1009}
Adam Langley95c29f32014-06-20 12:00:00 -07001010
Adam Langleyfcf25832014-12-18 17:42:32 -08001011long SSL_ctrl(SSL *s, int cmd, long larg, void *parg) {
1012 long l;
Adam Langley95c29f32014-06-20 12:00:00 -07001013
Adam Langleyfcf25832014-12-18 17:42:32 -08001014 switch (cmd) {
1015 case SSL_CTRL_GET_READ_AHEAD:
1016 return s->read_ahead;
Adam Langley95c29f32014-06-20 12:00:00 -07001017
Adam Langleyfcf25832014-12-18 17:42:32 -08001018 case SSL_CTRL_SET_READ_AHEAD:
1019 l = s->read_ahead;
1020 s->read_ahead = larg;
1021 return l;
Adam Langley95c29f32014-06-20 12:00:00 -07001022
Adam Langleyfcf25832014-12-18 17:42:32 -08001023 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1024 s->msg_callback_arg = parg;
1025 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001026
Adam Langleyfcf25832014-12-18 17:42:32 -08001027 case SSL_CTRL_OPTIONS:
1028 return s->options |= larg;
Adam Langley95c29f32014-06-20 12:00:00 -07001029
Adam Langleyfcf25832014-12-18 17:42:32 -08001030 case SSL_CTRL_CLEAR_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_MODE:
1034 return s->mode |= larg;
Adam Langley95c29f32014-06-20 12:00:00 -07001035
Adam Langleyfcf25832014-12-18 17:42:32 -08001036 case SSL_CTRL_CLEAR_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_GET_MAX_CERT_LIST:
1040 return s->max_cert_list;
Adam Langley95c29f32014-06-20 12:00:00 -07001041
Adam Langleyfcf25832014-12-18 17:42:32 -08001042 case SSL_CTRL_SET_MAX_CERT_LIST:
1043 l = s->max_cert_list;
1044 s->max_cert_list = larg;
1045 return l;
Adam Langley95c29f32014-06-20 12:00:00 -07001046
Adam Langleyfcf25832014-12-18 17:42:32 -08001047 case SSL_CTRL_SET_MTU:
1048 if (larg < (long)dtls1_min_mtu()) {
1049 return 0;
1050 }
1051 if (SSL_IS_DTLS(s)) {
1052 s->d1->mtu = larg;
1053 return larg;
1054 }
1055 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001056
Adam Langleyfcf25832014-12-18 17:42:32 -08001057 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1058 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH) {
1059 return 0;
1060 }
1061 s->max_send_fragment = larg;
1062 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001063
Adam Langleyfcf25832014-12-18 17:42:32 -08001064 case SSL_CTRL_GET_RI_SUPPORT:
1065 if (s->s3) {
1066 return s->s3->send_connection_binding;
1067 }
1068 return 0;
1069
1070 case SSL_CTRL_CERT_FLAGS:
1071 return s->cert->cert_flags |= larg;
1072
1073 case SSL_CTRL_CLEAR_CERT_FLAGS:
1074 return s->cert->cert_flags &= ~larg;
1075
1076 case SSL_CTRL_GET_RAW_CIPHERLIST:
1077 if (parg) {
1078 if (s->cert->ciphers_raw == NULL) {
1079 return 0;
1080 }
1081 *(uint8_t **)parg = s->cert->ciphers_raw;
1082 return (int)s->cert->ciphers_rawlen;
1083 }
1084
1085 /* Passing a NULL |parg| returns the size of a single
1086 * cipher suite value. */
1087 return 2;
1088
1089 default:
1090 return s->method->ssl_ctrl(s, cmd, larg, parg);
1091 }
1092}
1093
1094long SSL_callback_ctrl(SSL *s, int cmd, void (*fp)(void)) {
1095 switch (cmd) {
1096 case SSL_CTRL_SET_MSG_CALLBACK:
1097 s->msg_callback =
1098 (void (*)(int write_p, int version, int content_type, const void *buf,
1099 size_t len, SSL *ssl, void *arg))(fp);
1100 return 1;
1101
1102 default:
1103 return s->method->ssl_callback_ctrl(s, cmd, fp);
1104 }
1105}
1106
1107LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) { return ctx->sessions; }
1108
1109long SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg) {
1110 long l;
1111
1112 switch (cmd) {
1113 case SSL_CTRL_GET_READ_AHEAD:
1114 return ctx->read_ahead;
1115
1116 case SSL_CTRL_SET_READ_AHEAD:
1117 l = ctx->read_ahead;
1118 ctx->read_ahead = larg;
1119 return l;
1120
1121 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1122 ctx->msg_callback_arg = parg;
1123 return 1;
1124
1125 case SSL_CTRL_GET_MAX_CERT_LIST:
1126 return ctx->max_cert_list;
1127
1128 case SSL_CTRL_SET_MAX_CERT_LIST:
1129 l = ctx->max_cert_list;
1130 ctx->max_cert_list = larg;
1131 return l;
1132
1133 case SSL_CTRL_SET_SESS_CACHE_SIZE:
1134 l = ctx->session_cache_size;
1135 ctx->session_cache_size = larg;
1136 return l;
1137
1138 case SSL_CTRL_GET_SESS_CACHE_SIZE:
1139 return ctx->session_cache_size;
1140
1141 case SSL_CTRL_SET_SESS_CACHE_MODE:
1142 l = ctx->session_cache_mode;
1143 ctx->session_cache_mode = larg;
1144 return l;
1145
1146 case SSL_CTRL_GET_SESS_CACHE_MODE:
1147 return ctx->session_cache_mode;
1148
1149 case SSL_CTRL_SESS_NUMBER:
1150 return lh_SSL_SESSION_num_items(ctx->sessions);
1151
1152 case SSL_CTRL_SESS_CONNECT:
Adam Langleyfcf25832014-12-18 17:42:32 -08001153 case SSL_CTRL_SESS_CONNECT_GOOD:
Adam Langleyfcf25832014-12-18 17:42:32 -08001154 case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001155 case SSL_CTRL_SESS_ACCEPT:
Adam Langleyfcf25832014-12-18 17:42:32 -08001156 case SSL_CTRL_SESS_ACCEPT_GOOD:
Adam Langleyfcf25832014-12-18 17:42:32 -08001157 case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001158 case SSL_CTRL_SESS_HIT:
Adam Langleyfcf25832014-12-18 17:42:32 -08001159 case SSL_CTRL_SESS_CB_HIT:
Adam Langleyfcf25832014-12-18 17:42:32 -08001160 case SSL_CTRL_SESS_MISSES:
Adam Langleyfcf25832014-12-18 17:42:32 -08001161 case SSL_CTRL_SESS_TIMEOUTS:
Adam Langleyfcf25832014-12-18 17:42:32 -08001162 case SSL_CTRL_SESS_CACHE_FULL:
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001163 /* Statistics are no longer supported.
1164 * TODO(davidben): Try to remove the accessors altogether. */
1165 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001166
1167 case SSL_CTRL_OPTIONS:
1168 return ctx->options |= larg;
1169
1170 case SSL_CTRL_CLEAR_OPTIONS:
1171 return ctx->options &= ~larg;
1172
1173 case SSL_CTRL_MODE:
1174 return ctx->mode |= larg;
1175
1176 case SSL_CTRL_CLEAR_MODE:
1177 return ctx->mode &= ~larg;
1178
1179 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1180 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH) {
1181 return 0;
1182 }
1183 ctx->max_send_fragment = larg;
1184 return 1;
1185
1186 case SSL_CTRL_CERT_FLAGS:
1187 return ctx->cert->cert_flags |= larg;
1188
1189 case SSL_CTRL_CLEAR_CERT_FLAGS:
1190 return ctx->cert->cert_flags &= ~larg;
1191
1192 default:
1193 return ctx->method->ssl_ctx_ctrl(ctx, cmd, larg, parg);
1194 }
1195}
1196
1197long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void)) {
1198 switch (cmd) {
1199 case SSL_CTRL_SET_MSG_CALLBACK:
1200 ctx->msg_callback =
1201 (void (*)(int write_p, int version, int content_type, const void *buf,
1202 size_t len, SSL *ssl, void *arg))(fp);
1203 return 1;
1204
1205 default:
1206 return ctx->method->ssl_ctx_callback_ctrl(ctx, cmd, fp);
1207 }
1208}
1209
1210int ssl_cipher_id_cmp(const void *in_a, const void *in_b) {
1211 long l;
1212 const SSL_CIPHER *a = in_a;
1213 const SSL_CIPHER *b = in_b;
1214 const long a_id = a->id;
1215 const long b_id = b->id;
1216
1217 l = a_id - b_id;
1218 if (l == 0L) {
1219 return 0;
1220 } else {
1221 return (l > 0) ? 1 : -1;
1222 }
1223}
1224
1225int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **ap, const SSL_CIPHER **bp) {
1226 long l;
1227 const long a_id = (*ap)->id;
1228 const long b_id = (*bp)->id;
1229
1230 l = a_id - b_id;
1231 if (l == 0) {
1232 return 0;
1233 } else {
1234 return (l > 0) ? 1 : -1;
1235 }
1236}
1237
1238/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001239 * preference */
Adam Langleyfcf25832014-12-18 17:42:32 -08001240STACK_OF(SSL_CIPHER) * SSL_get_ciphers(const SSL *s) {
1241 if (s == NULL) {
1242 return NULL;
1243 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001244
Adam Langleyfcf25832014-12-18 17:42:32 -08001245 if (s->cipher_list != NULL) {
1246 return s->cipher_list->ciphers;
1247 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001248
Adam Langleyfcf25832014-12-18 17:42:32 -08001249 if (s->version >= TLS1_1_VERSION && s->ctx != NULL &&
1250 s->ctx->cipher_list_tls11 != NULL) {
1251 return s->ctx->cipher_list_tls11->ciphers;
1252 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001253
Adam Langleyfcf25832014-12-18 17:42:32 -08001254 if (s->ctx != NULL && s->ctx->cipher_list != NULL) {
1255 return s->ctx->cipher_list->ciphers;
1256 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001257
Adam Langleyfcf25832014-12-18 17:42:32 -08001258 return NULL;
1259}
Adam Langley95c29f32014-06-20 12:00:00 -07001260
Adam Langleyfcf25832014-12-18 17:42:32 -08001261/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001262 * algorithm id */
Adam Langleyfcf25832014-12-18 17:42:32 -08001263STACK_OF(SSL_CIPHER) * ssl_get_ciphers_by_id(SSL *s) {
1264 if (s == NULL) {
1265 return NULL;
1266 }
Adam Langley95c29f32014-06-20 12:00:00 -07001267
Adam Langleyfcf25832014-12-18 17:42:32 -08001268 if (s->cipher_list_by_id != NULL) {
1269 return s->cipher_list_by_id;
1270 }
Adam Langley95c29f32014-06-20 12:00:00 -07001271
Adam Langleyfcf25832014-12-18 17:42:32 -08001272 if (s->ctx != NULL && s->ctx->cipher_list_by_id != NULL) {
1273 return s->ctx->cipher_list_by_id;
1274 }
Adam Langley95c29f32014-06-20 12:00:00 -07001275
Adam Langleyfcf25832014-12-18 17:42:32 -08001276 return NULL;
1277}
Adam Langley95c29f32014-06-20 12:00:00 -07001278
Adam Langleyfcf25832014-12-18 17:42:32 -08001279/* The old interface to get the same thing as SSL_get_ciphers() */
1280const char *SSL_get_cipher_list(const SSL *s, int n) {
1281 const SSL_CIPHER *c;
1282 STACK_OF(SSL_CIPHER) * sk;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001283
Adam Langleyfcf25832014-12-18 17:42:32 -08001284 if (s == NULL) {
1285 return NULL;
1286 }
Adam Langley95c29f32014-06-20 12:00:00 -07001287
Adam Langleyfcf25832014-12-18 17:42:32 -08001288 sk = SSL_get_ciphers(s);
1289 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
1290 return NULL;
1291 }
Adam Langley95c29f32014-06-20 12:00:00 -07001292
Adam Langleyfcf25832014-12-18 17:42:32 -08001293 c = sk_SSL_CIPHER_value(sk, n);
1294 if (c == NULL) {
1295 return NULL;
1296 }
Adam Langley95c29f32014-06-20 12:00:00 -07001297
Adam Langleyfcf25832014-12-18 17:42:32 -08001298 return c->name;
1299}
David Benjamin5491e3f2014-09-29 19:33:09 -04001300
Adam Langleyfcf25832014-12-18 17:42:32 -08001301/* specify the ciphers to be used by default by the SSL_CTX */
1302int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
1303 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001304
Adam Langleyfcf25832014-12-18 17:42:32 -08001305 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001306 &ctx->cipher_list_by_id, str);
Adam Langleyfcf25832014-12-18 17:42:32 -08001307 /* ssl_create_cipher_list may return an empty stack if it was unable to find
1308 * a cipher matching the given rule string (for example if the rule string
1309 * specifies a cipher which has been disabled). This is not an error as far
1310 * as ssl_create_cipher_list is concerned, and hence ctx->cipher_list and
1311 * ctx->cipher_list_by_id has been updated. */
1312 if (sk == NULL) {
1313 return 0;
1314 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1315 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1316 return 0;
1317 }
Adam Langley95c29f32014-06-20 12:00:00 -07001318
Adam Langleyfcf25832014-12-18 17:42:32 -08001319 return 1;
1320}
David Benjamin39482a12014-07-20 13:30:15 -04001321
Adam Langleyfcf25832014-12-18 17:42:32 -08001322int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str) {
1323 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001324
David Benjamin71f07942015-04-08 02:36:59 -04001325 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str);
Adam Langleyfcf25832014-12-18 17:42:32 -08001326 if (sk == NULL) {
1327 return 0;
1328 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1329 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list_tls11,
1330 SSL_R_NO_CIPHER_MATCH);
1331 return 0;
1332 }
David Benjamin9f2c0d72014-10-21 22:00:19 -04001333
Adam Langleyfcf25832014-12-18 17:42:32 -08001334 return 1;
1335}
Adam Langley95c29f32014-06-20 12:00:00 -07001336
Adam Langleyfcf25832014-12-18 17:42:32 -08001337/* specify the ciphers to be used by the SSL */
1338int SSL_set_cipher_list(SSL *s, const char *str) {
1339 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001340
Adam Langleyfcf25832014-12-18 17:42:32 -08001341 sk = ssl_create_cipher_list(s->ctx->method, &s->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001342 &s->cipher_list_by_id, str);
David Benjamin39482a12014-07-20 13:30:15 -04001343
Adam Langleyfcf25832014-12-18 17:42:32 -08001344 /* see comment in SSL_CTX_set_cipher_list */
1345 if (sk == NULL) {
1346 return 0;
1347 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1348 OPENSSL_PUT_ERROR(SSL, SSL_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1349 return 0;
1350 }
David Benjamin39482a12014-07-20 13:30:15 -04001351
Adam Langleyfcf25832014-12-18 17:42:32 -08001352 return 1;
1353}
Adam Langley95c29f32014-06-20 12:00:00 -07001354
Adam Langleyfcf25832014-12-18 17:42:32 -08001355int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, uint8_t *p) {
1356 size_t i;
1357 const SSL_CIPHER *c;
1358 CERT *ct = s->cert;
1359 uint8_t *q;
1360 /* Set disabled masks for this session */
1361 ssl_set_client_disabled(s);
Adam Langley29707792014-06-20 12:00:00 -07001362
Adam Langleyfcf25832014-12-18 17:42:32 -08001363 if (sk == NULL) {
1364 return 0;
1365 }
1366 q = p;
Adam Langley95c29f32014-06-20 12:00:00 -07001367
Adam Langleyfcf25832014-12-18 17:42:32 -08001368 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
1369 c = sk_SSL_CIPHER_value(sk, i);
1370 /* Skip disabled ciphers */
1371 if (c->algorithm_ssl & ct->mask_ssl ||
1372 c->algorithm_mkey & ct->mask_k ||
1373 c->algorithm_auth & ct->mask_a) {
1374 continue;
1375 }
1376 s2n(ssl3_get_cipher_value(c), p);
1377 }
1378
1379 /* If all ciphers were disabled, return the error to the caller. */
1380 if (p == q) {
1381 return 0;
1382 }
1383
1384 /* Add SCSVs. */
1385 if (!s->renegotiate) {
1386 s2n(SSL3_CK_SCSV & 0xffff, p);
1387 }
1388
Adam Langley5f0efe02015-02-20 13:03:16 -08001389 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001390 s2n(SSL3_CK_FALLBACK_SCSV & 0xffff, p);
1391 }
1392
1393 return p - q;
1394}
1395
1396STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs) {
1397 CBS cipher_suites = *cbs;
1398 const SSL_CIPHER *c;
1399 STACK_OF(SSL_CIPHER) * sk;
1400
1401 if (s->s3) {
1402 s->s3->send_connection_binding = 0;
1403 }
1404
1405 if (CBS_len(&cipher_suites) % 2 != 0) {
1406 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1407 SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
1408 return NULL;
1409 }
1410
1411 sk = sk_SSL_CIPHER_new_null();
1412 if (sk == NULL) {
1413 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1414 goto err;
1415 }
1416
1417 if (!CBS_stow(&cipher_suites, &s->cert->ciphers_raw,
1418 &s->cert->ciphers_rawlen)) {
1419 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1420 goto err;
1421 }
1422
1423 while (CBS_len(&cipher_suites) > 0) {
1424 uint16_t cipher_suite;
1425
1426 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
1427 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_INTERNAL_ERROR);
1428 goto err;
1429 }
1430
1431 /* Check for SCSV. */
1432 if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff)) {
1433 /* SCSV is fatal if renegotiating. */
1434 if (s->renegotiate) {
1435 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1436 SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1437 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1438 goto err;
1439 }
1440 s->s3->send_connection_binding = 1;
1441 continue;
1442 }
1443
1444 /* Check for FALLBACK_SCSV. */
1445 if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff)) {
1446 uint16_t max_version = ssl3_get_max_server_version(s);
1447 if (SSL_IS_DTLS(s) ? (uint16_t)s->version > max_version
1448 : (uint16_t)s->version < max_version) {
1449 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1450 SSL_R_INAPPROPRIATE_FALLBACK);
1451 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
1452 goto err;
1453 }
1454 continue;
1455 }
1456
1457 c = ssl3_get_cipher_by_value(cipher_suite);
1458 if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
1459 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1460 goto err;
1461 }
1462 }
1463
1464 return sk;
David Benjamin9f2c0d72014-10-21 22:00:19 -04001465
Adam Langley95c29f32014-06-20 12:00:00 -07001466err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001467 if (sk != NULL) {
1468 sk_SSL_CIPHER_free(sk);
1469 }
1470 return NULL;
1471}
Adam Langley95c29f32014-06-20 12:00:00 -07001472
1473
Adam Langleyfcf25832014-12-18 17:42:32 -08001474/* return a servername extension value if provided in Client Hello, or NULL. So
1475 * far, only host_name types are defined (RFC 3546). */
1476const char *SSL_get_servername(const SSL *s, const int type) {
1477 if (type != TLSEXT_NAMETYPE_host_name) {
1478 return NULL;
1479 }
Adam Langley95c29f32014-06-20 12:00:00 -07001480
Adam Langleyfcf25832014-12-18 17:42:32 -08001481 return s->session && !s->tlsext_hostname ? s->session->tlsext_hostname
1482 : s->tlsext_hostname;
1483}
Adam Langley95c29f32014-06-20 12:00:00 -07001484
Adam Langleyfcf25832014-12-18 17:42:32 -08001485int SSL_get_servername_type(const SSL *s) {
1486 if (s->session &&
1487 (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname)) {
1488 return TLSEXT_NAMETYPE_host_name;
1489 }
Adam Langley95c29f32014-06-20 12:00:00 -07001490
Adam Langleyfcf25832014-12-18 17:42:32 -08001491 return -1;
1492}
Adam Langley95c29f32014-06-20 12:00:00 -07001493
Adam Langleyfcf25832014-12-18 17:42:32 -08001494void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
1495 ctx->signed_cert_timestamps_enabled = 1;
1496}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001497
Adam Langleyfcf25832014-12-18 17:42:32 -08001498int SSL_enable_signed_cert_timestamps(SSL *ssl) {
1499 ssl->signed_cert_timestamps_enabled = 1;
1500 return 1;
1501}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001502
Adam Langleyfcf25832014-12-18 17:42:32 -08001503void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
1504 ctx->ocsp_stapling_enabled = 1;
1505}
David Benjamin6c7aed02014-08-27 16:42:38 -04001506
Adam Langleyfcf25832014-12-18 17:42:32 -08001507int SSL_enable_ocsp_stapling(SSL *ssl) {
1508 ssl->ocsp_stapling_enabled = 1;
1509 return 1;
1510}
David Benjamin6c7aed02014-08-27 16:42:38 -04001511
Adam Langleyfcf25832014-12-18 17:42:32 -08001512void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
1513 size_t *out_len) {
1514 SSL_SESSION *session = ssl->session;
Adam Langley3cb50e02014-08-26 14:00:31 -07001515
Adam Langleyfcf25832014-12-18 17:42:32 -08001516 *out_len = 0;
1517 *out = NULL;
1518 if (ssl->server || !session || !session->tlsext_signed_cert_timestamp_list) {
1519 return;
1520 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001521
Adam Langleyfcf25832014-12-18 17:42:32 -08001522 *out = session->tlsext_signed_cert_timestamp_list;
1523 *out_len = session->tlsext_signed_cert_timestamp_list_length;
1524}
David Benjamin6c7aed02014-08-27 16:42:38 -04001525
Adam Langleyfcf25832014-12-18 17:42:32 -08001526void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
1527 size_t *out_len) {
1528 SSL_SESSION *session = ssl->session;
1529
1530 *out_len = 0;
1531 *out = NULL;
1532 if (ssl->server || !session || !session->ocsp_response) {
1533 return;
1534 }
1535 *out = session->ocsp_response;
1536 *out_len = session->ocsp_response_length;
1537}
David Benjamin6c7aed02014-08-27 16:42:38 -04001538
Adam Langley95c29f32014-06-20 12:00:00 -07001539/* SSL_select_next_proto implements the standard protocol selection. It is
1540 * expected that this function is called from the callback set by
1541 * SSL_CTX_set_next_proto_select_cb.
1542 *
1543 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1544 * strings. The length byte itself is not included in the length. A byte
1545 * string of length 0 is invalid. No byte string may be truncated.
1546 *
1547 * The current, but experimental algorithm for selecting the protocol is:
1548 *
1549 * 1) If the server doesn't support NPN then this is indicated to the
1550 * callback. In this case, the client application has to abort the connection
1551 * or have a default application level protocol.
1552 *
1553 * 2) If the server supports NPN, but advertises an empty list then the
1554 * client selects the first protcol in its list, but indicates via the
1555 * API that this fallback case was enacted.
1556 *
1557 * 3) Otherwise, the client finds the first protocol in the server's list
1558 * that it supports and selects this protocol. This is because it's
1559 * assumed that the server has better information about which protocol
1560 * a client should use.
1561 *
1562 * 4) If the client doesn't support any of the server's advertised
1563 * protocols, then this is treated the same as case 2.
1564 *
1565 * It returns either
1566 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1567 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1568 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001569int SSL_select_next_proto(uint8_t **out, uint8_t *outlen, const uint8_t *server,
1570 unsigned int server_len, const uint8_t *client,
1571 unsigned int client_len) {
1572 unsigned int i, j;
1573 const uint8_t *result;
1574 int status = OPENSSL_NPN_UNSUPPORTED;
Adam Langley95c29f32014-06-20 12:00:00 -07001575
Adam Langleyfcf25832014-12-18 17:42:32 -08001576 /* For each protocol in server preference order, see if we support it. */
1577 for (i = 0; i < server_len;) {
1578 for (j = 0; j < client_len;) {
1579 if (server[i] == client[j] &&
1580 memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
1581 /* We found a match */
1582 result = &server[i];
1583 status = OPENSSL_NPN_NEGOTIATED;
1584 goto found;
1585 }
1586 j += client[j];
1587 j++;
1588 }
1589 i += server[i];
1590 i++;
1591 }
Adam Langley95c29f32014-06-20 12:00:00 -07001592
Adam Langleyfcf25832014-12-18 17:42:32 -08001593 /* There's no overlap between our protocols and the server's list. */
1594 result = client;
1595 status = OPENSSL_NPN_NO_OVERLAP;
Adam Langley95c29f32014-06-20 12:00:00 -07001596
Adam Langleyfcf25832014-12-18 17:42:32 -08001597found:
1598 *out = (uint8_t *)result + 1;
1599 *outlen = result[0];
1600 return status;
1601}
Adam Langley95c29f32014-06-20 12:00:00 -07001602
Adam Langley95c29f32014-06-20 12:00:00 -07001603/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
1604 * requested protocol for this connection and returns 0. If the client didn't
1605 * request any protocol, then *data is set to NULL.
1606 *
1607 * Note that the client can request any protocol it chooses. The value returned
1608 * from this function need not be a member of the list of supported protocols
Adam Langleyfcf25832014-12-18 17:42:32 -08001609 * provided by the callback. */
1610void SSL_get0_next_proto_negotiated(const SSL *s, const uint8_t **data,
1611 unsigned *len) {
1612 *data = s->next_proto_negotiated;
1613 if (!*data) {
1614 *len = 0;
1615 } else {
1616 *len = s->next_proto_negotiated_len;
1617 }
Adam Langley95c29f32014-06-20 12:00:00 -07001618}
1619
1620/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
1621 * TLS server needs a list of supported protocols for Next Protocol
1622 * Negotiation. The returned list must be in wire format. The list is returned
1623 * by setting |out| to point to it and |outlen| to its length. This memory will
1624 * not be modified, but one should assume that the SSL* keeps a reference to
1625 * it.
1626 *
Adam Langleyfcf25832014-12-18 17:42:32 -08001627 * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise.
1628 * Otherwise, no such extension will be included in the ServerHello. */
1629void SSL_CTX_set_next_protos_advertised_cb(
1630 SSL_CTX *ctx,
1631 int (*cb)(SSL *ssl, const uint8_t **out, unsigned int *outlen, void *arg),
1632 void *arg) {
1633 ctx->next_protos_advertised_cb = cb;
1634 ctx->next_protos_advertised_cb_arg = arg;
1635}
Adam Langley95c29f32014-06-20 12:00:00 -07001636
1637/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1638 * client needs to select a protocol from the server's provided list. |out|
1639 * must be set to point to the selected protocol (which may be within |in|).
1640 * The length of the protocol name must be written into |outlen|. The server's
1641 * advertised protocols are provided in |in| and |inlen|. The callback can
1642 * assume that |in| is syntactically valid.
1643 *
1644 * The client must select a protocol. It is fatal to the connection if this
1645 * callback returns a value other than SSL_TLSEXT_ERR_OK.
1646 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001647void SSL_CTX_set_next_proto_select_cb(
1648 SSL_CTX *ctx, int (*cb)(SSL *s, uint8_t **out, uint8_t *outlen,
1649 const uint8_t *in, unsigned int inlen, void *arg),
1650 void *arg) {
1651 ctx->next_proto_select_cb = cb;
1652 ctx->next_proto_select_cb_arg = arg;
1653}
Adam Langley95c29f32014-06-20 12:00:00 -07001654
Adam Langleyfcf25832014-12-18 17:42:32 -08001655int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
1656 unsigned protos_len) {
1657 if (ctx->alpn_client_proto_list) {
1658 OPENSSL_free(ctx->alpn_client_proto_list);
1659 }
Adam Langley95c29f32014-06-20 12:00:00 -07001660
Adam Langleyfcf25832014-12-18 17:42:32 -08001661 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1662 if (!ctx->alpn_client_proto_list) {
1663 return 1;
1664 }
1665 ctx->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001666
Adam Langleyfcf25832014-12-18 17:42:32 -08001667 return 0;
1668}
Adam Langley95c29f32014-06-20 12:00:00 -07001669
Adam Langleyfcf25832014-12-18 17:42:32 -08001670int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
1671 if (ssl->alpn_client_proto_list) {
1672 OPENSSL_free(ssl->alpn_client_proto_list);
1673 }
Adam Langley95c29f32014-06-20 12:00:00 -07001674
Adam Langleyfcf25832014-12-18 17:42:32 -08001675 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1676 if (!ssl->alpn_client_proto_list) {
1677 return 1;
1678 }
1679 ssl->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001680
Adam Langleyfcf25832014-12-18 17:42:32 -08001681 return 0;
1682}
Adam Langley95c29f32014-06-20 12:00:00 -07001683
1684/* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
1685 * during ClientHello processing in order to select an ALPN protocol from the
1686 * client's list of offered protocols. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001687void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
1688 int (*cb)(SSL *ssl, const uint8_t **out,
1689 uint8_t *outlen, const uint8_t *in,
1690 unsigned int inlen, void *arg),
1691 void *arg) {
1692 ctx->alpn_select_cb = cb;
1693 ctx->alpn_select_cb_arg = arg;
1694}
Adam Langley95c29f32014-06-20 12:00:00 -07001695
1696/* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
1697 * On return it sets |*data| to point to |*len| bytes of protocol name (not
1698 * including the leading length-prefix byte). If the server didn't respond with
1699 * a negotiated protocol then |*len| will be zero. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001700void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **data,
1701 unsigned *len) {
1702 *data = NULL;
1703 if (ssl->s3) {
1704 *data = ssl->s3->alpn_selected;
1705 }
1706 if (*data == NULL) {
1707 *len = 0;
1708 } else {
1709 *len = ssl->s3->alpn_selected_len;
1710 }
1711}
Adam Langley95c29f32014-06-20 12:00:00 -07001712
David Benjamincfd248b2015-04-03 11:02:24 -04001713int SSL_export_keying_material(SSL *s, uint8_t *out, size_t out_len,
1714 const char *label, size_t label_len,
1715 const uint8_t *context, size_t context_len,
1716 int use_context) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001717 if (s->version < TLS1_VERSION) {
David Benjaminc565ebb2015-04-03 04:06:36 -04001718 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001719 }
Adam Langley95c29f32014-06-20 12:00:00 -07001720
David Benjamincfd248b2015-04-03 11:02:24 -04001721 return s->enc_method->export_keying_material(
1722 s, out, out_len, label, label_len, context, context_len, use_context);
Adam Langleyfcf25832014-12-18 17:42:32 -08001723}
Adam Langley95c29f32014-06-20 12:00:00 -07001724
Adam Langleyfcf25832014-12-18 17:42:32 -08001725static uint32_t ssl_session_hash(const SSL_SESSION *a) {
1726 uint32_t hash =
1727 ((uint32_t)a->session_id[0]) ||
1728 ((uint32_t)a->session_id[1] << 8) ||
1729 ((uint32_t)a->session_id[2] << 16) ||
1730 ((uint32_t)a->session_id[3] << 24);
Adam Langley95c29f32014-06-20 12:00:00 -07001731
Adam Langleyfcf25832014-12-18 17:42:32 -08001732 return hash;
1733}
Adam Langley95c29f32014-06-20 12:00:00 -07001734
1735/* NB: If this function (or indeed the hash function which uses a sort of
1736 * coarser function than this one) is changed, ensure
1737 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1738 * able to construct an SSL_SESSION that will collide with any existing session
1739 * with a matching session ID. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001740static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
1741 if (a->ssl_version != b->ssl_version) {
1742 return 1;
1743 }
Adam Langley95c29f32014-06-20 12:00:00 -07001744
Adam Langleyfcf25832014-12-18 17:42:32 -08001745 if (a->session_id_length != b->session_id_length) {
1746 return 1;
1747 }
Adam Langley95c29f32014-06-20 12:00:00 -07001748
Adam Langleyfcf25832014-12-18 17:42:32 -08001749 return memcmp(a->session_id, b->session_id, a->session_id_length);
1750}
Adam Langley95c29f32014-06-20 12:00:00 -07001751
Adam Langleyfcf25832014-12-18 17:42:32 -08001752SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth) {
1753 SSL_CTX *ret = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001754
Adam Langleyfcf25832014-12-18 17:42:32 -08001755 if (meth == NULL) {
1756 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_NULL_SSL_METHOD_PASSED);
1757 return NULL;
1758 }
Adam Langley95c29f32014-06-20 12:00:00 -07001759
Adam Langleyfcf25832014-12-18 17:42:32 -08001760 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
1761 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
1762 goto err;
1763 }
Adam Langley95c29f32014-06-20 12:00:00 -07001764
Adam Langleyfcf25832014-12-18 17:42:32 -08001765 ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1766 if (ret == NULL) {
1767 goto err;
1768 }
Adam Langley95c29f32014-06-20 12:00:00 -07001769
Adam Langleyfcf25832014-12-18 17:42:32 -08001770 memset(ret, 0, sizeof(SSL_CTX));
Adam Langley95c29f32014-06-20 12:00:00 -07001771
Adam Langleyfcf25832014-12-18 17:42:32 -08001772 ret->method = meth->method;
Adam Langley95c29f32014-06-20 12:00:00 -07001773
Adam Langleyfcf25832014-12-18 17:42:32 -08001774 ret->cert_store = NULL;
1775 ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
1776 ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1777 ret->session_cache_head = NULL;
1778 ret->session_cache_tail = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001779
Adam Langleyfcf25832014-12-18 17:42:32 -08001780 /* We take the system default */
1781 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001782
Adam Langleyfcf25832014-12-18 17:42:32 -08001783 ret->new_session_cb = 0;
1784 ret->remove_session_cb = 0;
1785 ret->get_session_cb = 0;
1786 ret->generate_session_id = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001787
Adam Langleyfcf25832014-12-18 17:42:32 -08001788 ret->references = 1;
1789 ret->quiet_shutdown = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001790
Adam Langleyfcf25832014-12-18 17:42:32 -08001791 ret->info_callback = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001792
Adam Langleyfcf25832014-12-18 17:42:32 -08001793 ret->app_verify_callback = 0;
1794 ret->app_verify_arg = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001795
Adam Langleyfcf25832014-12-18 17:42:32 -08001796 ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
1797 ret->read_ahead = 0;
1798 ret->msg_callback = 0;
1799 ret->msg_callback_arg = NULL;
1800 ret->verify_mode = SSL_VERIFY_NONE;
1801 ret->sid_ctx_length = 0;
1802 ret->default_verify_callback = NULL;
1803 ret->cert = ssl_cert_new();
1804 if (ret->cert == NULL) {
1805 goto err;
1806 }
Adam Langley95c29f32014-06-20 12:00:00 -07001807
Adam Langleyfcf25832014-12-18 17:42:32 -08001808 ret->default_passwd_callback = 0;
1809 ret->default_passwd_callback_userdata = NULL;
1810 ret->client_cert_cb = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001811
Adam Langleyfcf25832014-12-18 17:42:32 -08001812 ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
1813 if (ret->sessions == NULL) {
1814 goto err;
1815 }
1816 ret->cert_store = X509_STORE_new();
1817 if (ret->cert_store == NULL) {
1818 goto err;
1819 }
Adam Langley95c29f32014-06-20 12:00:00 -07001820
Adam Langleyfcf25832014-12-18 17:42:32 -08001821 ssl_create_cipher_list(ret->method, &ret->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001822 &ret->cipher_list_by_id, SSL_DEFAULT_CIPHER_LIST);
Adam Langleyfcf25832014-12-18 17:42:32 -08001823 if (ret->cipher_list == NULL ||
1824 sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0) {
1825 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_LIBRARY_HAS_NO_CIPHERS);
1826 goto err2;
1827 }
Adam Langley95c29f32014-06-20 12:00:00 -07001828
Adam Langleyfcf25832014-12-18 17:42:32 -08001829 ret->param = X509_VERIFY_PARAM_new();
1830 if (!ret->param) {
1831 goto err;
1832 }
Adam Langley95c29f32014-06-20 12:00:00 -07001833
Adam Langleyfcf25832014-12-18 17:42:32 -08001834 ret->client_CA = sk_X509_NAME_new_null();
1835 if (ret->client_CA == NULL) {
1836 goto err;
1837 }
Adam Langley95c29f32014-06-20 12:00:00 -07001838
David Benjamin9f33fc62015-04-15 17:29:53 -04001839 CRYPTO_new_ex_data(&g_ex_data_class_ssl_ctx, ret, &ret->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001840
Adam Langleyfcf25832014-12-18 17:42:32 -08001841 ret->extra_certs = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001842
Adam Langleyfcf25832014-12-18 17:42:32 -08001843 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
David Benjamin422d3a42014-08-20 11:09:03 -04001844
Adam Langleyfcf25832014-12-18 17:42:32 -08001845 ret->tlsext_servername_callback = 0;
1846 ret->tlsext_servername_arg = NULL;
1847 /* Setup RFC4507 ticket keys */
1848 if (!RAND_bytes(ret->tlsext_tick_key_name, 16) ||
1849 !RAND_bytes(ret->tlsext_tick_hmac_key, 16) ||
1850 !RAND_bytes(ret->tlsext_tick_aes_key, 16)) {
1851 ret->options |= SSL_OP_NO_TICKET;
1852 }
Adam Langley95c29f32014-06-20 12:00:00 -07001853
Adam Langleyfcf25832014-12-18 17:42:32 -08001854 ret->tlsext_status_cb = 0;
1855 ret->tlsext_status_arg = NULL;
David Benjamin82c9e902014-12-12 15:55:27 -05001856
Adam Langleyfcf25832014-12-18 17:42:32 -08001857 ret->next_protos_advertised_cb = 0;
1858 ret->next_proto_select_cb = 0;
1859 ret->psk_identity_hint = NULL;
1860 ret->psk_client_callback = NULL;
1861 ret->psk_server_callback = NULL;
1862
1863 /* Default is to connect to non-RI servers. When RI is more widely deployed
1864 * might change this. */
1865 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
1866
1867 /* Lock the SSL_CTX to the specified version, for compatibility with legacy
1868 * uses of SSL_METHOD. */
1869 if (meth->version != 0) {
1870 SSL_CTX_set_max_version(ret, meth->version);
1871 SSL_CTX_set_min_version(ret, meth->version);
1872 }
1873
1874 return ret;
1875
Adam Langley95c29f32014-06-20 12:00:00 -07001876err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001877 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -07001878err2:
Adam Langleyfcf25832014-12-18 17:42:32 -08001879 if (ret != NULL) {
1880 SSL_CTX_free(ret);
1881 }
1882 return NULL;
1883}
Adam Langley95c29f32014-06-20 12:00:00 -07001884
Adam Langleyfcf25832014-12-18 17:42:32 -08001885void SSL_CTX_free(SSL_CTX *a) {
1886 int i;
Adam Langley95c29f32014-06-20 12:00:00 -07001887
Adam Langleyfcf25832014-12-18 17:42:32 -08001888 if (a == NULL) {
1889 return;
1890 }
Adam Langley95c29f32014-06-20 12:00:00 -07001891
Adam Langleyfcf25832014-12-18 17:42:32 -08001892 i = CRYPTO_add(&a->references, -1, CRYPTO_LOCK_SSL_CTX);
1893 if (i > 0) {
1894 return;
1895 }
Adam Langley95c29f32014-06-20 12:00:00 -07001896
Adam Langleyfcf25832014-12-18 17:42:32 -08001897 if (a->param) {
1898 X509_VERIFY_PARAM_free(a->param);
1899 }
Adam Langley95c29f32014-06-20 12:00:00 -07001900
Adam Langleyfcf25832014-12-18 17:42:32 -08001901 /* Free internal session cache. However: the remove_cb() may reference the
1902 * ex_data of SSL_CTX, thus the ex_data store can only be removed after the
1903 * sessions were flushed. As the ex_data handling routines might also touch
1904 * the session cache, the most secure solution seems to be: empty (flush) the
1905 * cache, then free ex_data, then finally free the cache. (See ticket
1906 * [openssl.org #212].) */
1907 if (a->sessions != NULL) {
1908 SSL_CTX_flush_sessions(a, 0);
1909 }
Adam Langley95c29f32014-06-20 12:00:00 -07001910
David Benjamin9f33fc62015-04-15 17:29:53 -04001911 CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, a, &a->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001912
Adam Langleyfcf25832014-12-18 17:42:32 -08001913 if (a->sessions != NULL) {
1914 lh_SSL_SESSION_free(a->sessions);
1915 }
1916 if (a->cert_store != NULL) {
1917 X509_STORE_free(a->cert_store);
1918 }
1919 if (a->cipher_list != NULL) {
1920 ssl_cipher_preference_list_free(a->cipher_list);
1921 }
1922 if (a->cipher_list_by_id != NULL) {
1923 sk_SSL_CIPHER_free(a->cipher_list_by_id);
1924 }
1925 if (a->cipher_list_tls11 != NULL) {
1926 ssl_cipher_preference_list_free(a->cipher_list_tls11);
1927 }
1928 if (a->cert != NULL) {
1929 ssl_cert_free(a->cert);
1930 }
1931 if (a->client_CA != NULL) {
1932 sk_X509_NAME_pop_free(a->client_CA, X509_NAME_free);
1933 }
1934 if (a->extra_certs != NULL) {
1935 sk_X509_pop_free(a->extra_certs, X509_free);
1936 }
1937 if (a->srtp_profiles) {
1938 sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);
1939 }
1940 if (a->psk_identity_hint) {
1941 OPENSSL_free(a->psk_identity_hint);
1942 }
1943 if (a->tlsext_ecpointformatlist) {
1944 OPENSSL_free(a->tlsext_ecpointformatlist);
1945 }
1946 if (a->tlsext_ellipticcurvelist) {
1947 OPENSSL_free(a->tlsext_ellipticcurvelist);
1948 }
1949 if (a->alpn_client_proto_list != NULL) {
1950 OPENSSL_free(a->alpn_client_proto_list);
1951 }
1952 if (a->tlsext_channel_id_private) {
1953 EVP_PKEY_free(a->tlsext_channel_id_private);
1954 }
1955 if (a->keylog_bio) {
1956 BIO_free(a->keylog_bio);
1957 }
Adam Langley95c29f32014-06-20 12:00:00 -07001958
Adam Langleyfcf25832014-12-18 17:42:32 -08001959 OPENSSL_free(a);
1960}
Adam Langley95c29f32014-06-20 12:00:00 -07001961
Adam Langleyfcf25832014-12-18 17:42:32 -08001962void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) {
1963 ctx->default_passwd_callback = cb;
1964}
Adam Langley95c29f32014-06-20 12:00:00 -07001965
Adam Langleyfcf25832014-12-18 17:42:32 -08001966void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u) {
1967 ctx->default_passwd_callback_userdata = u;
1968}
Adam Langley95c29f32014-06-20 12:00:00 -07001969
Adam Langleyfcf25832014-12-18 17:42:32 -08001970void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
1971 int (*cb)(X509_STORE_CTX *, void *),
1972 void *arg) {
1973 ctx->app_verify_callback = cb;
1974 ctx->app_verify_arg = arg;
1975}
Adam Langley95c29f32014-06-20 12:00:00 -07001976
Adam Langleyfcf25832014-12-18 17:42:32 -08001977void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
1978 int (*cb)(int, X509_STORE_CTX *)) {
1979 ctx->verify_mode = mode;
1980 ctx->default_verify_callback = cb;
1981}
Adam Langley95c29f32014-06-20 12:00:00 -07001982
Adam Langleyfcf25832014-12-18 17:42:32 -08001983void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
1984 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
1985}
Adam Langley1258b6a2014-06-20 12:00:00 -07001986
Adam Langleyfcf25832014-12-18 17:42:32 -08001987void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb)(SSL *ssl, void *arg),
1988 void *arg) {
1989 ssl_cert_set_cert_cb(c->cert, cb, arg);
1990}
David Benjamin859ec3c2014-09-02 16:29:36 -04001991
Adam Langleyfcf25832014-12-18 17:42:32 -08001992void SSL_set_cert_cb(SSL *s, int (*cb)(SSL *ssl, void *arg), void *arg) {
1993 ssl_cert_set_cert_cb(s->cert, cb, arg);
1994}
Adam Langley95c29f32014-06-20 12:00:00 -07001995
Adam Langleyfcf25832014-12-18 17:42:32 -08001996static int ssl_has_key(SSL *s, size_t idx) {
1997 CERT_PKEY *cpk = &s->cert->pkeys[idx];
1998 return cpk->x509 && cpk->privatekey;
1999}
David Benjamin033e5f42014-11-13 18:47:41 -05002000
David Benjamin107db582015-04-08 00:41:59 -04002001void ssl_get_compatible_server_ciphers(SSL *s, uint32_t *out_mask_k,
2002 uint32_t *out_mask_a) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002003 CERT *c = s->cert;
2004 int rsa_enc, rsa_sign, dh_tmp;
David Benjamin107db582015-04-08 00:41:59 -04002005 uint32_t mask_k, mask_a;
Adam Langleyfcf25832014-12-18 17:42:32 -08002006 int have_ecc_cert, ecdsa_ok;
2007 int have_ecdh_tmp;
2008 X509 *x;
Adam Langley95c29f32014-06-20 12:00:00 -07002009
Adam Langleyfcf25832014-12-18 17:42:32 -08002010 if (c == NULL) {
2011 /* TODO(davidben): Is this codepath possible? */
2012 *out_mask_k = 0;
2013 *out_mask_a = 0;
2014 return;
2015 }
Adam Langley95c29f32014-06-20 12:00:00 -07002016
Adam Langleyfcf25832014-12-18 17:42:32 -08002017 dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07002018
David Benjaminc0f763b2015-03-27 02:05:39 -04002019 have_ecdh_tmp = (c->ecdh_nid != NID_undef || c->ecdh_tmp_cb != NULL ||
2020 c->ecdh_tmp_auto);
Adam Langleyfcf25832014-12-18 17:42:32 -08002021 rsa_enc = ssl_has_key(s, SSL_PKEY_RSA_ENC);
2022 rsa_sign = ssl_has_key(s, SSL_PKEY_RSA_SIGN);
2023 have_ecc_cert = ssl_has_key(s, SSL_PKEY_ECC);
2024 mask_k = 0;
2025 mask_a = 0;
David Benjaminf31e6812014-11-13 18:05:55 -05002026
Adam Langleyfcf25832014-12-18 17:42:32 -08002027 if (rsa_enc) {
2028 mask_k |= SSL_kRSA;
2029 }
2030 if (dh_tmp) {
David Benjamin7061e282015-03-19 11:10:48 -04002031 mask_k |= SSL_kDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08002032 }
2033 if (rsa_enc || rsa_sign) {
2034 mask_a |= SSL_aRSA;
2035 }
Adam Langley95c29f32014-06-20 12:00:00 -07002036
Adam Langleyfcf25832014-12-18 17:42:32 -08002037 /* An ECC certificate may be usable for ECDSA cipher suites depending on the
2038 * key usage extension and on the client's curve preferences. */
2039 if (have_ecc_cert) {
2040 x = c->pkeys[SSL_PKEY_ECC].x509;
2041 /* This call populates extension flags (ex_flags). */
2042 X509_check_purpose(x, -1, 0);
2043 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE)
2044 ? (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)
2045 : 1;
2046 if (!tls1_check_ec_cert(s, x)) {
2047 ecdsa_ok = 0;
2048 }
2049 if (ecdsa_ok) {
2050 mask_a |= SSL_aECDSA;
2051 }
2052 }
Adam Langley95c29f32014-06-20 12:00:00 -07002053
Adam Langleyfcf25832014-12-18 17:42:32 -08002054 /* If we are considering an ECC cipher suite that uses an ephemeral EC
2055 * key, check it. */
2056 if (have_ecdh_tmp && tls1_check_ec_tmp_key(s)) {
David Benjamin7061e282015-03-19 11:10:48 -04002057 mask_k |= SSL_kECDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08002058 }
Adam Langley95c29f32014-06-20 12:00:00 -07002059
Adam Langleyfcf25832014-12-18 17:42:32 -08002060 /* PSK requires a server callback. */
2061 if (s->psk_server_callback != NULL) {
2062 mask_k |= SSL_kPSK;
2063 mask_a |= SSL_aPSK;
2064 }
Adam Langley95c29f32014-06-20 12:00:00 -07002065
Adam Langleyfcf25832014-12-18 17:42:32 -08002066 *out_mask_k = mask_k;
2067 *out_mask_a = mask_a;
2068}
Adam Langley95c29f32014-06-20 12:00:00 -07002069
2070/* This handy macro borrowed from crypto/x509v3/v3_purp.c */
2071#define ku_reject(x, usage) \
Adam Langleyfcf25832014-12-18 17:42:32 -08002072 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
Adam Langley95c29f32014-06-20 12:00:00 -07002073
Adam Langleyfcf25832014-12-18 17:42:32 -08002074int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s) {
2075 unsigned long alg_a;
2076 int signature_nid = 0, md_nid = 0, pk_nid = 0;
2077 const SSL_CIPHER *cs = s->s3->tmp.new_cipher;
Adam Langley95c29f32014-06-20 12:00:00 -07002078
Adam Langleyfcf25832014-12-18 17:42:32 -08002079 alg_a = cs->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -07002080
Adam Langleyfcf25832014-12-18 17:42:32 -08002081 /* This call populates the ex_flags field correctly */
2082 X509_check_purpose(x, -1, 0);
2083 if (x->sig_alg && x->sig_alg->algorithm) {
2084 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
2085 OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
2086 }
2087 if (alg_a & SSL_aECDSA) {
2088 /* key usage, if present, must allow signing */
2089 if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE)) {
2090 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg,
2091 SSL_R_ECC_CERT_NOT_FOR_SIGNING);
2092 return 0;
2093 }
2094 }
Adam Langley95c29f32014-06-20 12:00:00 -07002095
Adam Langleyfcf25832014-12-18 17:42:32 -08002096 return 1; /* all checks are ok */
2097}
Adam Langley95c29f32014-06-20 12:00:00 -07002098
Adam Langleyfcf25832014-12-18 17:42:32 -08002099static int ssl_get_server_cert_index(const SSL *s) {
2100 int idx;
2101 idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
2102 if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509) {
2103 idx = SSL_PKEY_RSA_SIGN;
2104 }
2105 if (idx == -1) {
2106 OPENSSL_PUT_ERROR(SSL, ssl_get_server_cert_index, ERR_R_INTERNAL_ERROR);
2107 }
2108 return idx;
2109}
Adam Langley95c29f32014-06-20 12:00:00 -07002110
Adam Langleyfcf25832014-12-18 17:42:32 -08002111CERT_PKEY *ssl_get_server_send_pkey(const SSL *s) {
2112 int i = ssl_get_server_cert_index(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002113
Adam Langleyfcf25832014-12-18 17:42:32 -08002114 /* This may or may not be an error. */
2115 if (i < 0) {
2116 return NULL;
2117 }
Adam Langley95c29f32014-06-20 12:00:00 -07002118
Adam Langleyfcf25832014-12-18 17:42:32 -08002119 /* May be NULL. */
2120 return &s->cert->pkeys[i];
2121}
Adam Langley95c29f32014-06-20 12:00:00 -07002122
Adam Langleyfcf25832014-12-18 17:42:32 -08002123EVP_PKEY *ssl_get_sign_pkey(SSL *s, const SSL_CIPHER *cipher) {
2124 unsigned long alg_a;
2125 CERT *c;
2126 int idx = -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002127
Adam Langleyfcf25832014-12-18 17:42:32 -08002128 alg_a = cipher->algorithm_auth;
2129 c = s->cert;
Adam Langley95c29f32014-06-20 12:00:00 -07002130
Adam Langleyfcf25832014-12-18 17:42:32 -08002131 if (alg_a & SSL_aRSA) {
2132 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL) {
2133 idx = SSL_PKEY_RSA_SIGN;
2134 } else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL) {
2135 idx = SSL_PKEY_RSA_ENC;
2136 }
2137 } else if ((alg_a & SSL_aECDSA) &&
2138 (c->pkeys[SSL_PKEY_ECC].privatekey != NULL)) {
2139 idx = SSL_PKEY_ECC;
2140 }
Adam Langley95c29f32014-06-20 12:00:00 -07002141
Adam Langleyfcf25832014-12-18 17:42:32 -08002142 if (idx == -1) {
2143 OPENSSL_PUT_ERROR(SSL, ssl_get_sign_pkey, ERR_R_INTERNAL_ERROR);
2144 return NULL;
2145 }
Adam Langley95c29f32014-06-20 12:00:00 -07002146
Adam Langleyfcf25832014-12-18 17:42:32 -08002147 return c->pkeys[idx].privatekey;
2148}
Adam Langley95c29f32014-06-20 12:00:00 -07002149
Adam Langleyfcf25832014-12-18 17:42:32 -08002150void ssl_update_cache(SSL *s, int mode) {
David Benjaminb6d0c6d2015-03-19 19:07:26 -04002151 /* Never cache sessions with empty session IDs. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002152 if (s->session->session_id_length == 0) {
2153 return;
2154 }
Adam Langley95c29f32014-06-20 12:00:00 -07002155
David Benjaminb6d0c6d2015-03-19 19:07:26 -04002156 SSL_CTX *ctx = s->initial_ctx;
2157 if ((ctx->session_cache_mode & mode) == mode && !s->hit &&
2158 ((ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) ||
2159 SSL_CTX_add_session(ctx, s->session)) &&
2160 ctx->new_session_cb != NULL) {
2161 /* Note: |new_session_cb| is called whether the internal session cache is
2162 * used or not. */
2163 if (!ctx->new_session_cb(s, SSL_SESSION_up_ref(s->session))) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002164 SSL_SESSION_free(s->session);
2165 }
2166 }
Adam Langley95c29f32014-06-20 12:00:00 -07002167
David Benjaminb6d0c6d2015-03-19 19:07:26 -04002168 if (!(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR) &&
2169 !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) &&
2170 (ctx->session_cache_mode & mode) == mode) {
2171 /* Automatically flush the internal session cache every 255 connections. */
2172 int flush_cache = 0;
2173 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2174 ctx->handshakes_since_cache_flush++;
2175 if (ctx->handshakes_since_cache_flush >= 255) {
2176 flush_cache = 1;
2177 ctx->handshakes_since_cache_flush = 0;
2178 }
2179 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
2180
2181 if (flush_cache) {
2182 SSL_CTX_flush_sessions(ctx, (unsigned long)time(NULL));
Adam Langleyfcf25832014-12-18 17:42:32 -08002183 }
2184 }
2185}
Adam Langley95c29f32014-06-20 12:00:00 -07002186
David Benjamin1a5c50f2015-03-11 16:22:37 -04002187int SSL_get_error(const SSL *s, int ret_code) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002188 int reason;
David Benjamin1a5c50f2015-03-11 16:22:37 -04002189 uint32_t err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002190 BIO *bio;
Adam Langley95c29f32014-06-20 12:00:00 -07002191
David Benjamin1a5c50f2015-03-11 16:22:37 -04002192 if (ret_code > 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002193 return SSL_ERROR_NONE;
2194 }
Adam Langley95c29f32014-06-20 12:00:00 -07002195
Adam Langleyfcf25832014-12-18 17:42:32 -08002196 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
2197 * where we do encode the error */
David Benjamin1a5c50f2015-03-11 16:22:37 -04002198 err = ERR_peek_error();
2199 if (err != 0) {
2200 if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002201 return SSL_ERROR_SYSCALL;
2202 }
2203 return SSL_ERROR_SSL;
2204 }
Adam Langley95c29f32014-06-20 12:00:00 -07002205
David Benjamin1a5c50f2015-03-11 16:22:37 -04002206 if (ret_code == 0) {
David Benjamin9a38e922015-01-22 16:06:11 -05002207 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2208 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) {
2209 /* The socket was cleanly shut down with a close_notify. */
2210 return SSL_ERROR_ZERO_RETURN;
2211 }
2212 /* An EOF was observed which violates the protocol, and the underlying
2213 * transport does not participate in the error queue. Bubble up to the
2214 * caller. */
2215 return SSL_ERROR_SYSCALL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002216 }
Adam Langleyb2ce0582014-06-20 12:00:00 -07002217
Adam Langleyfcf25832014-12-18 17:42:32 -08002218 if (SSL_want_session(s)) {
2219 return SSL_ERROR_PENDING_SESSION;
2220 }
Adam Langley95c29f32014-06-20 12:00:00 -07002221
Adam Langleyfcf25832014-12-18 17:42:32 -08002222 if (SSL_want_certificate(s)) {
2223 return SSL_ERROR_PENDING_CERTIFICATE;
2224 }
Adam Langley95c29f32014-06-20 12:00:00 -07002225
Adam Langleyfcf25832014-12-18 17:42:32 -08002226 if (SSL_want_read(s)) {
2227 bio = SSL_get_rbio(s);
2228 if (BIO_should_read(bio)) {
2229 return SSL_ERROR_WANT_READ;
2230 }
Adam Langley95c29f32014-06-20 12:00:00 -07002231
Adam Langleyfcf25832014-12-18 17:42:32 -08002232 if (BIO_should_write(bio)) {
2233 /* This one doesn't make too much sense ... We never try to write to the
2234 * rbio, and an application program where rbio and wbio are separate
2235 * couldn't even know what it should wait for. However if we ever set
2236 * s->rwstate incorrectly (so that we have SSL_want_read(s) instead of
2237 * SSL_want_write(s)) and rbio and wbio *are* the same, this test works
2238 * around that bug; so it might be safer to keep it. */
2239 return SSL_ERROR_WANT_WRITE;
2240 }
Adam Langley95c29f32014-06-20 12:00:00 -07002241
Adam Langleyfcf25832014-12-18 17:42:32 -08002242 if (BIO_should_io_special(bio)) {
2243 reason = BIO_get_retry_reason(bio);
2244 if (reason == BIO_RR_CONNECT) {
2245 return SSL_ERROR_WANT_CONNECT;
2246 }
Adam Langley95c29f32014-06-20 12:00:00 -07002247
Adam Langleyfcf25832014-12-18 17:42:32 -08002248 if (reason == BIO_RR_ACCEPT) {
2249 return SSL_ERROR_WANT_ACCEPT;
2250 }
Adam Langley95c29f32014-06-20 12:00:00 -07002251
Adam Langleyfcf25832014-12-18 17:42:32 -08002252 return SSL_ERROR_SYSCALL; /* unknown */
2253 }
2254 }
Adam Langley95c29f32014-06-20 12:00:00 -07002255
Adam Langleyfcf25832014-12-18 17:42:32 -08002256 if (SSL_want_write(s)) {
2257 bio = SSL_get_wbio(s);
2258 if (BIO_should_write(bio)) {
2259 return SSL_ERROR_WANT_WRITE;
2260 }
Adam Langley95c29f32014-06-20 12:00:00 -07002261
Adam Langleyfcf25832014-12-18 17:42:32 -08002262 if (BIO_should_read(bio)) {
2263 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2264 return SSL_ERROR_WANT_READ;
2265 }
Adam Langley95c29f32014-06-20 12:00:00 -07002266
Adam Langleyfcf25832014-12-18 17:42:32 -08002267 if (BIO_should_io_special(bio)) {
2268 reason = BIO_get_retry_reason(bio);
2269 if (reason == BIO_RR_CONNECT) {
2270 return SSL_ERROR_WANT_CONNECT;
2271 }
Adam Langley95c29f32014-06-20 12:00:00 -07002272
Adam Langleyfcf25832014-12-18 17:42:32 -08002273 if (reason == BIO_RR_ACCEPT) {
2274 return SSL_ERROR_WANT_ACCEPT;
2275 }
Adam Langley95c29f32014-06-20 12:00:00 -07002276
Adam Langleyfcf25832014-12-18 17:42:32 -08002277 return SSL_ERROR_SYSCALL;
2278 }
2279 }
Adam Langley95c29f32014-06-20 12:00:00 -07002280
Adam Langleyfcf25832014-12-18 17:42:32 -08002281 if (SSL_want_x509_lookup(s)) {
2282 return SSL_ERROR_WANT_X509_LOOKUP;
2283 }
Adam Langley95c29f32014-06-20 12:00:00 -07002284
Adam Langleyfcf25832014-12-18 17:42:32 -08002285 if (SSL_want_channel_id_lookup(s)) {
2286 return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
2287 }
Adam Langley0f4746e2014-08-13 12:26:32 -07002288
Adam Langleyfcf25832014-12-18 17:42:32 -08002289 return SSL_ERROR_SYSCALL;
2290}
Adam Langley0f4746e2014-08-13 12:26:32 -07002291
Adam Langleyfcf25832014-12-18 17:42:32 -08002292int SSL_do_handshake(SSL *s) {
2293 int ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002294
Adam Langleyfcf25832014-12-18 17:42:32 -08002295 if (s->handshake_func == NULL) {
2296 OPENSSL_PUT_ERROR(SSL, SSL_do_handshake, SSL_R_CONNECTION_TYPE_NOT_SET);
2297 return -1;
2298 }
Adam Langley95c29f32014-06-20 12:00:00 -07002299
Adam Langleyfcf25832014-12-18 17:42:32 -08002300 s->method->ssl_renegotiate_check(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002301
Adam Langleyfcf25832014-12-18 17:42:32 -08002302 if (SSL_in_init(s)) {
2303 ret = s->handshake_func(s);
2304 }
2305 return ret;
2306}
Adam Langley95c29f32014-06-20 12:00:00 -07002307
Adam Langleyfcf25832014-12-18 17:42:32 -08002308void SSL_set_accept_state(SSL *s) {
2309 s->server = 1;
2310 s->shutdown = 0;
2311 s->state = SSL_ST_ACCEPT | SSL_ST_BEFORE;
2312 s->handshake_func = s->method->ssl_accept;
2313 /* clear the current cipher */
2314 ssl_clear_cipher_ctx(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002315}
Adam Langley95c29f32014-06-20 12:00:00 -07002316
Adam Langleyfcf25832014-12-18 17:42:32 -08002317void SSL_set_connect_state(SSL *s) {
2318 s->server = 0;
2319 s->shutdown = 0;
2320 s->state = SSL_ST_CONNECT | SSL_ST_BEFORE;
2321 s->handshake_func = s->method->ssl_connect;
2322 /* clear the current cipher */
2323 ssl_clear_cipher_ctx(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002324}
Adam Langley95c29f32014-06-20 12:00:00 -07002325
Adam Langleyfcf25832014-12-18 17:42:32 -08002326int ssl_undefined_function(SSL *s) {
2327 OPENSSL_PUT_ERROR(SSL, ssl_undefined_function,
2328 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2329 return 0;
2330}
Adam Langley95c29f32014-06-20 12:00:00 -07002331
Adam Langleyfcf25832014-12-18 17:42:32 -08002332int ssl_undefined_void_function(void) {
2333 OPENSSL_PUT_ERROR(SSL, ssl_undefined_void_function,
2334 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2335 return 0;
2336}
Adam Langley95c29f32014-06-20 12:00:00 -07002337
Adam Langleyfcf25832014-12-18 17:42:32 -08002338int ssl_undefined_const_function(const SSL *s) {
2339 OPENSSL_PUT_ERROR(SSL, ssl_undefined_const_function,
2340 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2341 return 0;
2342}
Adam Langley95c29f32014-06-20 12:00:00 -07002343
Adam Langleyfcf25832014-12-18 17:42:32 -08002344static const char *ssl_get_version(int version) {
2345 switch (version) {
2346 case TLS1_2_VERSION:
2347 return "TLSv1.2";
Adam Langley95c29f32014-06-20 12:00:00 -07002348
Adam Langleyfcf25832014-12-18 17:42:32 -08002349 case TLS1_1_VERSION:
2350 return "TLSv1.1";
Adam Langley95c29f32014-06-20 12:00:00 -07002351
Adam Langleyfcf25832014-12-18 17:42:32 -08002352 case TLS1_VERSION:
2353 return "TLSv1";
Adam Langley95c29f32014-06-20 12:00:00 -07002354
Adam Langleyfcf25832014-12-18 17:42:32 -08002355 case SSL3_VERSION:
2356 return "SSLv3";
Adam Langley95c29f32014-06-20 12:00:00 -07002357
Adam Langleyfcf25832014-12-18 17:42:32 -08002358 default:
2359 return "unknown";
2360 }
2361}
Adam Langley95c29f32014-06-20 12:00:00 -07002362
Adam Langleyfcf25832014-12-18 17:42:32 -08002363const char *SSL_get_version(const SSL *s) {
2364 return ssl_get_version(s->version);
2365}
Adam Langley95c29f32014-06-20 12:00:00 -07002366
Adam Langleyfcf25832014-12-18 17:42:32 -08002367const char *SSL_SESSION_get_version(const SSL_SESSION *sess) {
2368 return ssl_get_version(sess->ssl_version);
2369}
Adam Langley95c29f32014-06-20 12:00:00 -07002370
Adam Langleyfcf25832014-12-18 17:42:32 -08002371void ssl_clear_cipher_ctx(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002372 if (s->aead_read_ctx != NULL) {
2373 EVP_AEAD_CTX_cleanup(&s->aead_read_ctx->ctx);
2374 OPENSSL_free(s->aead_read_ctx);
2375 s->aead_read_ctx = NULL;
2376 }
Adam Langleya5dc5452014-06-20 12:00:00 -07002377
Adam Langleyfcf25832014-12-18 17:42:32 -08002378 if (s->aead_write_ctx != NULL) {
2379 EVP_AEAD_CTX_cleanup(&s->aead_write_ctx->ctx);
2380 OPENSSL_free(s->aead_write_ctx);
2381 s->aead_write_ctx = NULL;
2382 }
2383}
Adam Langley95c29f32014-06-20 12:00:00 -07002384
Adam Langleyfcf25832014-12-18 17:42:32 -08002385X509 *SSL_get_certificate(const SSL *s) {
2386 if (s->cert != NULL) {
2387 return s->cert->key->x509;
2388 }
2389
2390 return NULL;
2391}
2392
2393EVP_PKEY *SSL_get_privatekey(const SSL *s) {
2394 if (s->cert != NULL) {
2395 return s->cert->key->privatekey;
2396 }
2397
2398 return NULL;
2399}
2400
2401X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
2402 if (ctx->cert != NULL) {
2403 return ctx->cert->key->x509;
2404 }
2405
2406 return NULL;
2407}
2408
2409EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
2410 if (ctx->cert != NULL) {
2411 return ctx->cert->key->privatekey;
2412 }
2413
2414 return NULL;
2415}
2416
2417const SSL_CIPHER *SSL_get_current_cipher(const SSL *s) {
2418 if (s->session != NULL && s->session->cipher != NULL) {
2419 return s->session->cipher;
2420 }
2421
2422 return NULL;
2423}
2424
2425const void *SSL_get_current_compression(SSL *s) { return NULL; }
2426
2427const void *SSL_get_current_expansion(SSL *s) { return NULL; }
2428
2429int ssl_init_wbio_buffer(SSL *s, int push) {
2430 BIO *bbio;
2431
2432 if (s->bbio == NULL) {
2433 bbio = BIO_new(BIO_f_buffer());
2434 if (bbio == NULL) {
2435 return 0;
2436 }
2437 s->bbio = bbio;
2438 } else {
2439 bbio = s->bbio;
2440 if (s->bbio == s->wbio) {
2441 s->wbio = BIO_pop(s->wbio);
2442 }
2443 }
2444
2445 BIO_reset(bbio);
2446 if (!BIO_set_read_buffer_size(bbio, 1)) {
2447 OPENSSL_PUT_ERROR(SSL, ssl_init_wbio_buffer, ERR_R_BUF_LIB);
2448 return 0;
2449 }
2450
2451 if (push) {
2452 if (s->wbio != bbio) {
2453 s->wbio = BIO_push(bbio, s->wbio);
2454 }
2455 } else {
2456 if (s->wbio == bbio) {
2457 s->wbio = BIO_pop(bbio);
2458 }
2459 }
2460
2461 return 1;
2462}
2463
2464void ssl_free_wbio_buffer(SSL *s) {
2465 if (s->bbio == NULL) {
2466 return;
2467 }
2468
2469 if (s->bbio == s->wbio) {
2470 /* remove buffering */
2471 s->wbio = BIO_pop(s->wbio);
2472 }
2473
2474 BIO_free(s->bbio);
2475 s->bbio = NULL;
2476}
2477
2478void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
2479 ctx->quiet_shutdown = mode;
2480}
2481
2482int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
2483 return ctx->quiet_shutdown;
2484}
2485
2486void SSL_set_quiet_shutdown(SSL *s, int mode) { s->quiet_shutdown = mode; }
2487
2488int SSL_get_quiet_shutdown(const SSL *s) { return s->quiet_shutdown; }
2489
2490void SSL_set_shutdown(SSL *s, int mode) { s->shutdown = mode; }
2491
2492int SSL_get_shutdown(const SSL *s) { return s->shutdown; }
2493
2494int SSL_version(const SSL *s) { return s->version; }
2495
2496SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
2497
2498SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
2499 if (ssl->ctx == ctx) {
2500 return ssl->ctx;
2501 }
2502
2503 if (ctx == NULL) {
2504 ctx = ssl->initial_ctx;
2505 }
2506
2507 if (ssl->cert != NULL) {
2508 ssl_cert_free(ssl->cert);
2509 }
2510
2511 ssl->cert = ssl_cert_dup(ctx->cert);
2512 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
2513 if (ssl->ctx != NULL) {
2514 SSL_CTX_free(ssl->ctx); /* decrement reference count */
2515 }
2516 ssl->ctx = ctx;
2517
2518 ssl->sid_ctx_length = ctx->sid_ctx_length;
2519 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2520 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2521
2522 return ssl->ctx;
2523}
2524
2525int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
2526 return X509_STORE_set_default_paths(ctx->cert_store);
2527}
Adam Langley95c29f32014-06-20 12:00:00 -07002528
2529int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
Adam Langleyfcf25832014-12-18 17:42:32 -08002530 const char *CApath) {
2531 return X509_STORE_load_locations(ctx->cert_store, CAfile, CApath);
2532}
Adam Langley95c29f32014-06-20 12:00:00 -07002533
2534void SSL_set_info_callback(SSL *ssl,
Adam Langleyfcf25832014-12-18 17:42:32 -08002535 void (*cb)(const SSL *ssl, int type, int val)) {
2536 ssl->info_callback = cb;
2537}
Adam Langley95c29f32014-06-20 12:00:00 -07002538
Adam Langleyfcf25832014-12-18 17:42:32 -08002539void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/, int /*type*/,
2540 int /*val*/) {
2541 return ssl->info_callback;
2542}
Adam Langley95c29f32014-06-20 12:00:00 -07002543
Adam Langleyfcf25832014-12-18 17:42:32 -08002544int SSL_state(const SSL *ssl) { return ssl->state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002545
Adam Langleyfcf25832014-12-18 17:42:32 -08002546void SSL_set_state(SSL *ssl, int state) { ssl->state = state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002547
Adam Langleyfcf25832014-12-18 17:42:32 -08002548void SSL_set_verify_result(SSL *ssl, long arg) { ssl->verify_result = arg; }
Adam Langley95c29f32014-06-20 12:00:00 -07002549
Adam Langleyfcf25832014-12-18 17:42:32 -08002550long SSL_get_verify_result(const SSL *ssl) { return ssl->verify_result; }
Adam Langley95c29f32014-06-20 12:00:00 -07002551
Adam Langleyfcf25832014-12-18 17:42:32 -08002552int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2553 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002554 int index;
2555 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp,
2556 new_func, dup_func, free_func)) {
2557 return -1;
2558 }
2559 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002560}
Adam Langley95c29f32014-06-20 12:00:00 -07002561
Adam Langleyfcf25832014-12-18 17:42:32 -08002562int SSL_set_ex_data(SSL *s, int idx, void *arg) {
2563 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2564}
Adam Langley95c29f32014-06-20 12:00:00 -07002565
Adam Langleyfcf25832014-12-18 17:42:32 -08002566void *SSL_get_ex_data(const SSL *s, int idx) {
2567 return CRYPTO_get_ex_data(&s->ex_data, idx);
2568}
Adam Langley95c29f32014-06-20 12:00:00 -07002569
Adam Langleyfcf25832014-12-18 17:42:32 -08002570int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2571 CRYPTO_EX_dup *dup_func,
2572 CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002573 int index;
2574 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp,
2575 new_func, dup_func, free_func)) {
2576 return -1;
2577 }
2578 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002579}
Adam Langley95c29f32014-06-20 12:00:00 -07002580
Adam Langleyfcf25832014-12-18 17:42:32 -08002581int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg) {
2582 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2583}
Adam Langley95c29f32014-06-20 12:00:00 -07002584
Adam Langleyfcf25832014-12-18 17:42:32 -08002585void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx) {
2586 return CRYPTO_get_ex_data(&s->ex_data, idx);
2587}
Adam Langley95c29f32014-06-20 12:00:00 -07002588
Adam Langleyfcf25832014-12-18 17:42:32 -08002589int ssl_ok(SSL *s) { return 1; }
Adam Langley95c29f32014-06-20 12:00:00 -07002590
Adam Langleyfcf25832014-12-18 17:42:32 -08002591X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
2592 return ctx->cert_store;
2593}
Adam Langley95c29f32014-06-20 12:00:00 -07002594
Adam Langleyfcf25832014-12-18 17:42:32 -08002595void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
2596 if (ctx->cert_store != NULL) {
2597 X509_STORE_free(ctx->cert_store);
2598 }
2599 ctx->cert_store = store;
2600}
Adam Langley95c29f32014-06-20 12:00:00 -07002601
Adam Langleyfcf25832014-12-18 17:42:32 -08002602int SSL_want(const SSL *s) { return s->rwstate; }
Adam Langley95c29f32014-06-20 12:00:00 -07002603
Adam Langleyfcf25832014-12-18 17:42:32 -08002604void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
2605 RSA *(*cb)(SSL *ssl, int is_export,
2606 int keylength)) {
2607 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_RSA_CB, (void (*)(void))cb);
2608}
Adam Langley95c29f32014-06-20 12:00:00 -07002609
Adam Langleyfcf25832014-12-18 17:42:32 -08002610void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
2611 int keylength)) {
2612 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_RSA_CB, (void (*)(void))cb);
2613}
2614
2615void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
2616 DH *(*dh)(SSL *ssl, int is_export,
2617 int keylength)) {
2618 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh);
2619}
2620
2621void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*dh)(SSL *ssl, int is_export,
2622 int keylength)) {
2623 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh);
2624}
2625
2626void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,
2627 EC_KEY *(*ecdh)(SSL *ssl, int is_export,
2628 int keylength)) {
2629 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH_CB, (void (*)(void))ecdh);
2630}
2631
2632void SSL_set_tmp_ecdh_callback(SSL *ssl,
2633 EC_KEY *(*ecdh)(SSL *ssl, int is_export,
2634 int keylength)) {
2635 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_ECDH_CB, (void (*)(void))ecdh);
2636}
2637
2638int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
2639 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2640 OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_psk_identity_hint,
2641 SSL_R_DATA_LENGTH_TOO_LONG);
2642 return 0;
2643 }
2644
2645 if (ctx->psk_identity_hint != NULL) {
2646 OPENSSL_free(ctx->psk_identity_hint);
2647 }
2648
2649 if (identity_hint != NULL) {
2650 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2651 if (ctx->psk_identity_hint == NULL) {
2652 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002653 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002654 } else {
2655 ctx->psk_identity_hint = NULL;
2656 }
Adam Langley95c29f32014-06-20 12:00:00 -07002657
Adam Langleyfcf25832014-12-18 17:42:32 -08002658 return 1;
2659}
2660
2661int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint) {
2662 if (s == NULL) {
2663 return 0;
2664 }
2665
2666 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2667 OPENSSL_PUT_ERROR(SSL, SSL_use_psk_identity_hint,
2668 SSL_R_DATA_LENGTH_TOO_LONG);
2669 return 0;
2670 }
2671
2672 /* Clear currently configured hint, if any. */
2673 if (s->psk_identity_hint != NULL) {
2674 OPENSSL_free(s->psk_identity_hint);
2675 s->psk_identity_hint = NULL;
2676 }
2677
2678 if (identity_hint != NULL) {
2679 s->psk_identity_hint = BUF_strdup(identity_hint);
2680 if (s->psk_identity_hint == NULL) {
2681 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002682 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002683 }
Adam Langley95c29f32014-06-20 12:00:00 -07002684
Adam Langleyfcf25832014-12-18 17:42:32 -08002685 return 1;
2686}
Adam Langley95c29f32014-06-20 12:00:00 -07002687
Adam Langleyfcf25832014-12-18 17:42:32 -08002688const char *SSL_get_psk_identity_hint(const SSL *s) {
2689 if (s == NULL) {
2690 return NULL;
2691 }
2692 return s->psk_identity_hint;
2693}
Adam Langley95c29f32014-06-20 12:00:00 -07002694
Adam Langleyfcf25832014-12-18 17:42:32 -08002695const char *SSL_get_psk_identity(const SSL *s) {
2696 if (s == NULL || s->session == NULL) {
2697 return NULL;
2698 }
Adam Langley95c29f32014-06-20 12:00:00 -07002699
Adam Langleyfcf25832014-12-18 17:42:32 -08002700 return s->session->psk_identity;
2701}
Adam Langley95c29f32014-06-20 12:00:00 -07002702
Adam Langleyfcf25832014-12-18 17:42:32 -08002703void SSL_set_psk_client_callback(
2704 SSL *s, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2705 unsigned int max_identity_len, uint8_t *psk,
2706 unsigned int max_psk_len)) {
2707 s->psk_client_callback = cb;
2708}
Adam Langley95c29f32014-06-20 12:00:00 -07002709
Adam Langleyfcf25832014-12-18 17:42:32 -08002710void SSL_CTX_set_psk_client_callback(
2711 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2712 unsigned int max_identity_len,
2713 uint8_t *psk, unsigned int max_psk_len)) {
2714 ctx->psk_client_callback = cb;
2715}
Adam Langley95c29f32014-06-20 12:00:00 -07002716
Adam Langleyfcf25832014-12-18 17:42:32 -08002717void SSL_set_psk_server_callback(
2718 SSL *s, unsigned int (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
2719 unsigned int max_psk_len)) {
2720 s->psk_server_callback = cb;
2721}
Adam Langley95c29f32014-06-20 12:00:00 -07002722
Adam Langleyfcf25832014-12-18 17:42:32 -08002723void SSL_CTX_set_psk_server_callback(
2724 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *identity,
2725 uint8_t *psk, unsigned int max_psk_len)) {
2726 ctx->psk_server_callback = cb;
2727}
Adam Langley95c29f32014-06-20 12:00:00 -07002728
Adam Langleyfcf25832014-12-18 17:42:32 -08002729void SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) {
2730 ctx->min_version = version;
2731}
Adam Langley95c29f32014-06-20 12:00:00 -07002732
Adam Langleyfcf25832014-12-18 17:42:32 -08002733void SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) {
2734 ctx->max_version = version;
2735}
Adam Langley0289c732014-06-20 12:00:00 -07002736
Adam Langleyfcf25832014-12-18 17:42:32 -08002737void SSL_set_min_version(SSL *ssl, uint16_t version) {
2738 ssl->min_version = version;
2739}
Adam Langley0289c732014-06-20 12:00:00 -07002740
Adam Langleyfcf25832014-12-18 17:42:32 -08002741void SSL_set_max_version(SSL *ssl, uint16_t version) {
2742 ssl->max_version = version;
2743}
Adam Langley95c29f32014-06-20 12:00:00 -07002744
Adam Langleyfcf25832014-12-18 17:42:32 -08002745void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
2746 void (*cb)(int write_p, int version,
2747 int content_type, const void *buf,
2748 size_t len, SSL *ssl, void *arg)) {
2749 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2750}
2751void SSL_set_msg_callback(SSL *ssl,
2752 void (*cb)(int write_p, int version, int content_type,
2753 const void *buf, size_t len, SSL *ssl,
2754 void *arg)) {
2755 SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2756}
Adam Langley95c29f32014-06-20 12:00:00 -07002757
Adam Langleyfcf25832014-12-18 17:42:32 -08002758void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio) {
2759 if (ctx->keylog_bio != NULL) {
2760 BIO_free(ctx->keylog_bio);
2761 }
2762 ctx->keylog_bio = keylog_bio;
2763}
Adam Langley95c29f32014-06-20 12:00:00 -07002764
Adam Langleyfcf25832014-12-18 17:42:32 -08002765static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
2766 static const char hextable[] = "0123456789abcdef";
2767 uint8_t *out;
2768 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002769
Adam Langleyfcf25832014-12-18 17:42:32 -08002770 if (!CBB_add_space(cbb, &out, in_len * 2)) {
2771 return 0;
2772 }
Adam Langley95c29f32014-06-20 12:00:00 -07002773
Adam Langleyfcf25832014-12-18 17:42:32 -08002774 for (i = 0; i < in_len; i++) {
2775 *(out++) = (uint8_t)hextable[in[i] >> 4];
2776 *(out++) = (uint8_t)hextable[in[i] & 0xf];
2777 }
Adam Langley95c29f32014-06-20 12:00:00 -07002778
Adam Langleyfcf25832014-12-18 17:42:32 -08002779 return 1;
2780}
David Benjamin859ec3c2014-09-02 16:29:36 -04002781
2782int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -08002783 const uint8_t *encrypted_premaster,
2784 size_t encrypted_premaster_len,
2785 const uint8_t *premaster,
2786 size_t premaster_len) {
2787 BIO *bio = ctx->keylog_bio;
2788 CBB cbb;
2789 uint8_t *out;
2790 size_t out_len;
2791 int ret;
David Benjamin859ec3c2014-09-02 16:29:36 -04002792
Adam Langleyfcf25832014-12-18 17:42:32 -08002793 if (bio == NULL) {
2794 return 1;
2795 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002796
Adam Langleyfcf25832014-12-18 17:42:32 -08002797 if (encrypted_premaster_len < 8) {
2798 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_rsa_client_key_exchange,
2799 ERR_R_INTERNAL_ERROR);
2800 return 0;
2801 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002802
Adam Langleyfcf25832014-12-18 17:42:32 -08002803 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len * 2 + 1)) {
2804 return 0;
2805 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002806
Adam Langleyfcf25832014-12-18 17:42:32 -08002807 if (!CBB_add_bytes(&cbb, (const uint8_t *)"RSA ", 4) ||
2808 /* Only the first 8 bytes of the encrypted premaster secret are
2809 * logged. */
2810 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
2811 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2812 !cbb_add_hex(&cbb, premaster, premaster_len) ||
2813 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2814 !CBB_finish(&cbb, &out, &out_len)) {
2815 CBB_cleanup(&cbb);
2816 return 0;
2817 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002818
Adam Langleyfcf25832014-12-18 17:42:32 -08002819 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2820 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2821 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
David Benjamin859ec3c2014-09-02 16:29:36 -04002822
Adam Langleyfcf25832014-12-18 17:42:32 -08002823 OPENSSL_free(out);
2824 return ret;
Adam Langley95f22882014-06-20 12:00:00 -07002825}
2826
Adam Langleyfcf25832014-12-18 17:42:32 -08002827int ssl_ctx_log_master_secret(SSL_CTX *ctx, const uint8_t *client_random,
2828 size_t client_random_len, const uint8_t *master,
2829 size_t master_len) {
2830 BIO *bio = ctx->keylog_bio;
2831 CBB cbb;
2832 uint8_t *out;
2833 size_t out_len;
2834 int ret;
Adam Langleyadb739e2014-06-20 12:00:00 -07002835
Adam Langleyfcf25832014-12-18 17:42:32 -08002836 if (bio == NULL) {
2837 return 1;
2838 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002839
Adam Langleyfcf25832014-12-18 17:42:32 -08002840 if (client_random_len != 32) {
2841 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_master_secret, ERR_R_INTERNAL_ERROR);
2842 return 0;
2843 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002844
Adam Langleyfcf25832014-12-18 17:42:32 -08002845 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len * 2 + 1)) {
2846 return 0;
2847 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002848
Adam Langleyfcf25832014-12-18 17:42:32 -08002849 if (!CBB_add_bytes(&cbb, (const uint8_t *)"CLIENT_RANDOM ", 14) ||
2850 !cbb_add_hex(&cbb, client_random, 32) ||
2851 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2852 !cbb_add_hex(&cbb, master, master_len) ||
2853 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2854 !CBB_finish(&cbb, &out, &out_len)) {
2855 CBB_cleanup(&cbb);
2856 return 0;
2857 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002858
Adam Langleyfcf25832014-12-18 17:42:32 -08002859 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2860 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2861 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
David Benjamine99e9122014-12-11 01:46:01 -05002862
Adam Langleyfcf25832014-12-18 17:42:32 -08002863 OPENSSL_free(out);
2864 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -07002865}
2866
David Benjamined7c4752015-02-16 19:16:46 -05002867int SSL_in_false_start(const SSL *s) {
2868 return s->s3->tmp.in_false_start;
2869}
2870
Adam Langleyfcf25832014-12-18 17:42:32 -08002871int SSL_cutthrough_complete(const SSL *s) {
David Benjamined7c4752015-02-16 19:16:46 -05002872 return SSL_in_false_start(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002873}
Adam Langley95c29f32014-06-20 12:00:00 -07002874
Adam Langleyfcf25832014-12-18 17:42:32 -08002875void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
2876 size_t *ssl_session_size) {
2877 *ssl_size = sizeof(SSL);
2878 *ssl_ctx_size = sizeof(SSL_CTX);
2879 *ssl_session_size = sizeof(SSL_SESSION);
2880}
Feng Lu41aa3252014-11-21 22:47:56 -08002881
David Benjamined7c4752015-02-16 19:16:46 -05002882int ssl3_can_false_start(const SSL *s) {
David Benjamin195dc782015-02-19 13:27:05 -05002883 const SSL_CIPHER *const cipher = SSL_get_current_cipher(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002884
Adam Langleye631d962015-04-01 13:11:01 -07002885 /* False Start only for TLS 1.2 with an ECDHE+AEAD cipher and ALPN or NPN. */
David Benjamin195dc782015-02-19 13:27:05 -05002886 return !SSL_IS_DTLS(s) &&
2887 SSL_version(s) >= TLS1_2_VERSION &&
2888 (s->s3->alpn_selected || s->s3->next_proto_neg_seen) &&
2889 cipher != NULL &&
Adam Langleye631d962015-04-01 13:11:01 -07002890 cipher->algorithm_mkey == SSL_kECDHE &&
David Benjamin195dc782015-02-19 13:27:05 -05002891 (cipher->algorithm_enc == SSL_AES128GCM ||
2892 cipher->algorithm_enc == SSL_AES256GCM ||
2893 cipher->algorithm_enc == SSL_CHACHA20POLY1305);
Adam Langleyfcf25832014-12-18 17:42:32 -08002894}
2895
2896const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version) {
2897 switch (version) {
2898 case SSL3_VERSION:
2899 return &SSLv3_enc_data;
2900
2901 case TLS1_VERSION:
2902 return &TLSv1_enc_data;
2903
David Benjamin9e13e1a2015-03-05 01:56:32 -05002904 case DTLS1_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002905 case TLS1_1_VERSION:
2906 return &TLSv1_1_enc_data;
2907
David Benjamin9e13e1a2015-03-05 01:56:32 -05002908 case DTLS1_2_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002909 case TLS1_2_VERSION:
2910 return &TLSv1_2_enc_data;
2911
Adam Langleyfcf25832014-12-18 17:42:32 -08002912 default:
2913 return NULL;
2914 }
2915}
2916
2917uint16_t ssl3_get_max_server_version(const SSL *s) {
2918 uint16_t max_version;
2919
2920 if (SSL_IS_DTLS(s)) {
2921 max_version = (s->max_version != 0) ? s->max_version : DTLS1_2_VERSION;
2922 if (!(s->options & SSL_OP_NO_DTLSv1_2) && DTLS1_2_VERSION >= max_version) {
2923 return DTLS1_2_VERSION;
2924 }
2925 if (!(s->options & SSL_OP_NO_DTLSv1) && DTLS1_VERSION >= max_version) {
2926 return DTLS1_VERSION;
2927 }
2928 return 0;
2929 }
2930
2931 max_version = (s->max_version != 0) ? s->max_version : TLS1_2_VERSION;
2932 if (!(s->options & SSL_OP_NO_TLSv1_2) && TLS1_2_VERSION <= max_version) {
2933 return TLS1_2_VERSION;
2934 }
2935 if (!(s->options & SSL_OP_NO_TLSv1_1) && TLS1_1_VERSION <= max_version) {
2936 return TLS1_1_VERSION;
2937 }
2938 if (!(s->options & SSL_OP_NO_TLSv1) && TLS1_VERSION <= max_version) {
2939 return TLS1_VERSION;
2940 }
2941 if (!(s->options & SSL_OP_NO_SSLv3) && SSL3_VERSION <= max_version) {
2942 return SSL3_VERSION;
2943 }
2944 return 0;
2945}
2946
2947uint16_t ssl3_get_mutual_version(SSL *s, uint16_t client_version) {
2948 uint16_t version = 0;
2949
2950 if (SSL_IS_DTLS(s)) {
2951 /* Clamp client_version to max_version. */
2952 if (s->max_version != 0 && client_version < s->max_version) {
2953 client_version = s->max_version;
2954 }
2955
2956 if (client_version <= DTLS1_2_VERSION && !(s->options & SSL_OP_NO_DTLSv1_2)) {
2957 version = DTLS1_2_VERSION;
2958 } else if (client_version <= DTLS1_VERSION &&
David Benjaminb18f0242015-03-10 18:30:08 -04002959 !(s->options & SSL_OP_NO_DTLSv1)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002960 version = DTLS1_VERSION;
2961 }
2962
2963 /* Check against min_version. */
2964 if (version != 0 && s->min_version != 0 && version > s->min_version) {
2965 return 0;
2966 }
2967 return version;
2968 } else {
2969 /* Clamp client_version to max_version. */
2970 if (s->max_version != 0 && client_version > s->max_version) {
2971 client_version = s->max_version;
2972 }
2973
2974 if (client_version >= TLS1_2_VERSION && !(s->options & SSL_OP_NO_TLSv1_2)) {
2975 version = TLS1_2_VERSION;
2976 } else if (client_version >= TLS1_1_VERSION &&
2977 !(s->options & SSL_OP_NO_TLSv1_1)) {
2978 version = TLS1_1_VERSION;
2979 } else if (client_version >= TLS1_VERSION && !(s->options & SSL_OP_NO_TLSv1)) {
2980 version = TLS1_VERSION;
2981 } else if (client_version >= SSL3_VERSION && !(s->options & SSL_OP_NO_SSLv3)) {
2982 version = SSL3_VERSION;
2983 }
2984
2985 /* Check against min_version. */
2986 if (version != 0 && s->min_version != 0 && version < s->min_version) {
2987 return 0;
2988 }
2989 return version;
2990 }
2991}
2992
2993uint16_t ssl3_get_max_client_version(SSL *s) {
2994 unsigned long options = s->options;
2995 uint16_t version = 0;
2996
2997 /* OpenSSL's API for controlling versions entails blacklisting individual
2998 * protocols. This has two problems. First, on the client, the protocol can
2999 * only express a contiguous range of versions. Second, a library consumer
3000 * trying to set a maximum version cannot disable protocol versions that get
3001 * added in a future version of the library.
3002 *
3003 * To account for both of these, OpenSSL interprets the client-side bitmask
3004 * as a min/max range by picking the lowest contiguous non-empty range of
3005 * enabled protocols. Note that this means it is impossible to set a maximum
3006 * version of TLS 1.2 in a future-proof way.
3007 *
3008 * By this scheme, the maximum version is the lowest version V such that V is
3009 * enabled and V+1 is disabled or unimplemented. */
3010 if (SSL_IS_DTLS(s)) {
3011 if (!(options & SSL_OP_NO_DTLSv1_2)) {
3012 version = DTLS1_2_VERSION;
3013 }
3014 if (!(options & SSL_OP_NO_DTLSv1) && (options & SSL_OP_NO_DTLSv1_2)) {
3015 version = DTLS1_VERSION;
3016 }
3017 if (s->max_version != 0 && version < s->max_version) {
3018 version = s->max_version;
3019 }
3020 } else {
3021 if (!(options & SSL_OP_NO_TLSv1_2)) {
3022 version = TLS1_2_VERSION;
3023 }
3024 if (!(options & SSL_OP_NO_TLSv1_1) && (options & SSL_OP_NO_TLSv1_2)) {
3025 version = TLS1_1_VERSION;
3026 }
3027 if (!(options & SSL_OP_NO_TLSv1) && (options & SSL_OP_NO_TLSv1_1)) {
3028 version = TLS1_VERSION;
3029 }
3030 if (!(options & SSL_OP_NO_SSLv3) && (options & SSL_OP_NO_TLSv1)) {
3031 version = SSL3_VERSION;
3032 }
3033 if (s->max_version != 0 && version > s->max_version) {
3034 version = s->max_version;
3035 }
3036 }
3037
3038 return version;
3039}
3040
3041int ssl3_is_version_enabled(SSL *s, uint16_t version) {
3042 if (SSL_IS_DTLS(s)) {
3043 if (s->max_version != 0 && version < s->max_version) {
3044 return 0;
3045 }
3046 if (s->min_version != 0 && version > s->min_version) {
3047 return 0;
3048 }
3049
3050 switch (version) {
3051 case DTLS1_VERSION:
3052 return !(s->options & SSL_OP_NO_DTLSv1);
3053
3054 case DTLS1_2_VERSION:
3055 return !(s->options & SSL_OP_NO_DTLSv1_2);
3056
3057 default:
3058 return 0;
3059 }
3060 } else {
3061 if (s->max_version != 0 && version > s->max_version) {
3062 return 0;
3063 }
3064 if (s->min_version != 0 && version < s->min_version) {
3065 return 0;
3066 }
3067
3068 switch (version) {
3069 case SSL3_VERSION:
3070 return !(s->options & SSL_OP_NO_SSLv3);
3071
3072 case TLS1_VERSION:
3073 return !(s->options & SSL_OP_NO_TLSv1);
3074
3075 case TLS1_1_VERSION:
3076 return !(s->options & SSL_OP_NO_TLSv1_1);
3077
3078 case TLS1_2_VERSION:
3079 return !(s->options & SSL_OP_NO_TLSv1_2);
3080
3081 default:
3082 return 0;
3083 }
3084 }
3085}
3086
David Benjaminea72bd02014-12-21 21:27:41 -05003087uint16_t ssl3_version_from_wire(SSL *s, uint16_t wire_version) {
3088 if (!SSL_IS_DTLS(s)) {
3089 return wire_version;
3090 }
3091
3092 uint16_t tls_version = ~wire_version;
3093 uint16_t version = tls_version + 0x0201;
3094 /* If either component overflowed, clamp it so comparisons still work. */
3095 if ((version >> 8) < (tls_version >> 8)) {
3096 version = 0xff00 | (version & 0xff);
3097 }
3098 if ((version & 0xff) < (tls_version & 0xff)) {
3099 version = (version & 0xff00) | 0xff;
3100 }
3101 /* DTLS 1.0 maps to TLS 1.1, not TLS 1.0. */
3102 if (version == TLS1_VERSION) {
3103 version = TLS1_1_VERSION;
3104 }
3105 return version;
3106}
3107
Adam Langleyfcf25832014-12-18 17:42:32 -08003108int SSL_cache_hit(SSL *s) { return s->hit; }
3109
3110int SSL_is_server(SSL *s) { return s->server; }
3111
Adam Langley524e7172015-02-20 16:04:00 -08003112void SSL_CTX_set_dos_protection_cb(
3113 SSL_CTX *ctx, int (*cb)(const struct ssl_early_callback_ctx *)) {
3114 ctx->dos_protection_cb = cb;
3115}
3116
Adam Langleyfcf25832014-12-18 17:42:32 -08003117void SSL_enable_fastradio_padding(SSL *s, char on_off) {
3118 s->fastradio_padding = on_off;
3119}
Adam Langley44972942015-02-20 13:43:23 -08003120
David Benjaminb16346b2015-04-08 19:16:58 -04003121void SSL_set_reject_peer_renegotiations(SSL *s, int reject) {
3122 s->reject_peer_renegotiations = !!reject;
3123}
3124
Adam Langley44972942015-02-20 13:43:23 -08003125const SSL_CIPHER *SSL_get_cipher_by_value(uint16_t value) {
3126 return ssl3_get_cipher_by_value(value);
3127}
Adam Langley3f92d212015-02-20 15:32:52 -08003128
3129int SSL_get_rc4_state(const SSL *ssl, const RC4_KEY **read_key,
3130 const RC4_KEY **write_key) {
3131 if (ssl->aead_read_ctx == NULL || ssl->aead_write_ctx == NULL) {
3132 return 0;
3133 }
3134
3135 return EVP_AEAD_CTX_get_rc4_state(&ssl->aead_read_ctx->ctx, read_key) &&
3136 EVP_AEAD_CTX_get_rc4_state(&ssl->aead_write_ctx->ctx, write_key);
3137}