blob: c491b12593ba11ed0007d98f2b218959684c81d5 [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) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002075 const SSL_CIPHER *cs = s->s3->tmp.new_cipher;
David Benjamin3fa27772015-04-17 22:32:19 -04002076 uint32_t alg_a = cs->algorithm_auth;
2077 int signature_nid = 0, md_nid = 0, pk_nid = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002078
Adam Langleyfcf25832014-12-18 17:42:32 -08002079 /* This call populates the ex_flags field correctly */
2080 X509_check_purpose(x, -1, 0);
2081 if (x->sig_alg && x->sig_alg->algorithm) {
2082 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
2083 OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
2084 }
2085 if (alg_a & SSL_aECDSA) {
2086 /* key usage, if present, must allow signing */
2087 if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE)) {
2088 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg,
2089 SSL_R_ECC_CERT_NOT_FOR_SIGNING);
2090 return 0;
2091 }
2092 }
Adam Langley95c29f32014-06-20 12:00:00 -07002093
Adam Langleyfcf25832014-12-18 17:42:32 -08002094 return 1; /* all checks are ok */
2095}
Adam Langley95c29f32014-06-20 12:00:00 -07002096
Adam Langleyfcf25832014-12-18 17:42:32 -08002097static int ssl_get_server_cert_index(const SSL *s) {
2098 int idx;
2099 idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
2100 if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509) {
2101 idx = SSL_PKEY_RSA_SIGN;
2102 }
2103 if (idx == -1) {
2104 OPENSSL_PUT_ERROR(SSL, ssl_get_server_cert_index, ERR_R_INTERNAL_ERROR);
2105 }
2106 return idx;
2107}
Adam Langley95c29f32014-06-20 12:00:00 -07002108
Adam Langleyfcf25832014-12-18 17:42:32 -08002109CERT_PKEY *ssl_get_server_send_pkey(const SSL *s) {
2110 int i = ssl_get_server_cert_index(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002111
Adam Langleyfcf25832014-12-18 17:42:32 -08002112 /* This may or may not be an error. */
2113 if (i < 0) {
2114 return NULL;
2115 }
Adam Langley95c29f32014-06-20 12:00:00 -07002116
Adam Langleyfcf25832014-12-18 17:42:32 -08002117 /* May be NULL. */
2118 return &s->cert->pkeys[i];
2119}
Adam Langley95c29f32014-06-20 12:00:00 -07002120
Adam Langleyfcf25832014-12-18 17:42:32 -08002121EVP_PKEY *ssl_get_sign_pkey(SSL *s, const SSL_CIPHER *cipher) {
David Benjamin3fa27772015-04-17 22:32:19 -04002122 uint32_t alg_a = cipher->algorithm_auth;
2123 CERT *c = s->cert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002124 int idx = -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002125
Adam Langleyfcf25832014-12-18 17:42:32 -08002126 if (alg_a & SSL_aRSA) {
2127 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL) {
2128 idx = SSL_PKEY_RSA_SIGN;
2129 } else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL) {
2130 idx = SSL_PKEY_RSA_ENC;
2131 }
2132 } else if ((alg_a & SSL_aECDSA) &&
2133 (c->pkeys[SSL_PKEY_ECC].privatekey != NULL)) {
2134 idx = SSL_PKEY_ECC;
2135 }
Adam Langley95c29f32014-06-20 12:00:00 -07002136
Adam Langleyfcf25832014-12-18 17:42:32 -08002137 if (idx == -1) {
2138 OPENSSL_PUT_ERROR(SSL, ssl_get_sign_pkey, ERR_R_INTERNAL_ERROR);
2139 return NULL;
2140 }
Adam Langley95c29f32014-06-20 12:00:00 -07002141
Adam Langleyfcf25832014-12-18 17:42:32 -08002142 return c->pkeys[idx].privatekey;
2143}
Adam Langley95c29f32014-06-20 12:00:00 -07002144
Adam Langleyfcf25832014-12-18 17:42:32 -08002145void ssl_update_cache(SSL *s, int mode) {
David Benjaminb6d0c6d2015-03-19 19:07:26 -04002146 /* Never cache sessions with empty session IDs. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002147 if (s->session->session_id_length == 0) {
2148 return;
2149 }
Adam Langley95c29f32014-06-20 12:00:00 -07002150
David Benjaminb6d0c6d2015-03-19 19:07:26 -04002151 SSL_CTX *ctx = s->initial_ctx;
2152 if ((ctx->session_cache_mode & mode) == mode && !s->hit &&
2153 ((ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) ||
2154 SSL_CTX_add_session(ctx, s->session)) &&
2155 ctx->new_session_cb != NULL) {
2156 /* Note: |new_session_cb| is called whether the internal session cache is
2157 * used or not. */
2158 if (!ctx->new_session_cb(s, SSL_SESSION_up_ref(s->session))) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002159 SSL_SESSION_free(s->session);
2160 }
2161 }
Adam Langley95c29f32014-06-20 12:00:00 -07002162
David Benjaminb6d0c6d2015-03-19 19:07:26 -04002163 if (!(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR) &&
2164 !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) &&
2165 (ctx->session_cache_mode & mode) == mode) {
2166 /* Automatically flush the internal session cache every 255 connections. */
2167 int flush_cache = 0;
2168 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2169 ctx->handshakes_since_cache_flush++;
2170 if (ctx->handshakes_since_cache_flush >= 255) {
2171 flush_cache = 1;
2172 ctx->handshakes_since_cache_flush = 0;
2173 }
2174 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
2175
2176 if (flush_cache) {
2177 SSL_CTX_flush_sessions(ctx, (unsigned long)time(NULL));
Adam Langleyfcf25832014-12-18 17:42:32 -08002178 }
2179 }
2180}
Adam Langley95c29f32014-06-20 12:00:00 -07002181
David Benjamin1a5c50f2015-03-11 16:22:37 -04002182int SSL_get_error(const SSL *s, int ret_code) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002183 int reason;
David Benjamin1a5c50f2015-03-11 16:22:37 -04002184 uint32_t err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002185 BIO *bio;
Adam Langley95c29f32014-06-20 12:00:00 -07002186
David Benjamin1a5c50f2015-03-11 16:22:37 -04002187 if (ret_code > 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002188 return SSL_ERROR_NONE;
2189 }
Adam Langley95c29f32014-06-20 12:00:00 -07002190
Adam Langleyfcf25832014-12-18 17:42:32 -08002191 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
2192 * where we do encode the error */
David Benjamin1a5c50f2015-03-11 16:22:37 -04002193 err = ERR_peek_error();
2194 if (err != 0) {
2195 if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002196 return SSL_ERROR_SYSCALL;
2197 }
2198 return SSL_ERROR_SSL;
2199 }
Adam Langley95c29f32014-06-20 12:00:00 -07002200
David Benjamin1a5c50f2015-03-11 16:22:37 -04002201 if (ret_code == 0) {
David Benjamin9a38e922015-01-22 16:06:11 -05002202 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2203 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) {
2204 /* The socket was cleanly shut down with a close_notify. */
2205 return SSL_ERROR_ZERO_RETURN;
2206 }
2207 /* An EOF was observed which violates the protocol, and the underlying
2208 * transport does not participate in the error queue. Bubble up to the
2209 * caller. */
2210 return SSL_ERROR_SYSCALL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002211 }
Adam Langleyb2ce0582014-06-20 12:00:00 -07002212
Adam Langleyfcf25832014-12-18 17:42:32 -08002213 if (SSL_want_session(s)) {
2214 return SSL_ERROR_PENDING_SESSION;
2215 }
Adam Langley95c29f32014-06-20 12:00:00 -07002216
Adam Langleyfcf25832014-12-18 17:42:32 -08002217 if (SSL_want_certificate(s)) {
2218 return SSL_ERROR_PENDING_CERTIFICATE;
2219 }
Adam Langley95c29f32014-06-20 12:00:00 -07002220
Adam Langleyfcf25832014-12-18 17:42:32 -08002221 if (SSL_want_read(s)) {
2222 bio = SSL_get_rbio(s);
2223 if (BIO_should_read(bio)) {
2224 return SSL_ERROR_WANT_READ;
2225 }
Adam Langley95c29f32014-06-20 12:00:00 -07002226
Adam Langleyfcf25832014-12-18 17:42:32 -08002227 if (BIO_should_write(bio)) {
2228 /* This one doesn't make too much sense ... We never try to write to the
2229 * rbio, and an application program where rbio and wbio are separate
2230 * couldn't even know what it should wait for. However if we ever set
2231 * s->rwstate incorrectly (so that we have SSL_want_read(s) instead of
2232 * SSL_want_write(s)) and rbio and wbio *are* the same, this test works
2233 * around that bug; so it might be safer to keep it. */
2234 return SSL_ERROR_WANT_WRITE;
2235 }
Adam Langley95c29f32014-06-20 12:00:00 -07002236
Adam Langleyfcf25832014-12-18 17:42:32 -08002237 if (BIO_should_io_special(bio)) {
2238 reason = BIO_get_retry_reason(bio);
2239 if (reason == BIO_RR_CONNECT) {
2240 return SSL_ERROR_WANT_CONNECT;
2241 }
Adam Langley95c29f32014-06-20 12:00:00 -07002242
Adam Langleyfcf25832014-12-18 17:42:32 -08002243 if (reason == BIO_RR_ACCEPT) {
2244 return SSL_ERROR_WANT_ACCEPT;
2245 }
Adam Langley95c29f32014-06-20 12:00:00 -07002246
Adam Langleyfcf25832014-12-18 17:42:32 -08002247 return SSL_ERROR_SYSCALL; /* unknown */
2248 }
2249 }
Adam Langley95c29f32014-06-20 12:00:00 -07002250
Adam Langleyfcf25832014-12-18 17:42:32 -08002251 if (SSL_want_write(s)) {
2252 bio = SSL_get_wbio(s);
2253 if (BIO_should_write(bio)) {
2254 return SSL_ERROR_WANT_WRITE;
2255 }
Adam Langley95c29f32014-06-20 12:00:00 -07002256
Adam Langleyfcf25832014-12-18 17:42:32 -08002257 if (BIO_should_read(bio)) {
2258 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2259 return SSL_ERROR_WANT_READ;
2260 }
Adam Langley95c29f32014-06-20 12:00:00 -07002261
Adam Langleyfcf25832014-12-18 17:42:32 -08002262 if (BIO_should_io_special(bio)) {
2263 reason = BIO_get_retry_reason(bio);
2264 if (reason == BIO_RR_CONNECT) {
2265 return SSL_ERROR_WANT_CONNECT;
2266 }
Adam Langley95c29f32014-06-20 12:00:00 -07002267
Adam Langleyfcf25832014-12-18 17:42:32 -08002268 if (reason == BIO_RR_ACCEPT) {
2269 return SSL_ERROR_WANT_ACCEPT;
2270 }
Adam Langley95c29f32014-06-20 12:00:00 -07002271
Adam Langleyfcf25832014-12-18 17:42:32 -08002272 return SSL_ERROR_SYSCALL;
2273 }
2274 }
Adam Langley95c29f32014-06-20 12:00:00 -07002275
Adam Langleyfcf25832014-12-18 17:42:32 -08002276 if (SSL_want_x509_lookup(s)) {
2277 return SSL_ERROR_WANT_X509_LOOKUP;
2278 }
Adam Langley95c29f32014-06-20 12:00:00 -07002279
Adam Langleyfcf25832014-12-18 17:42:32 -08002280 if (SSL_want_channel_id_lookup(s)) {
2281 return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
2282 }
Adam Langley0f4746e2014-08-13 12:26:32 -07002283
Adam Langleyfcf25832014-12-18 17:42:32 -08002284 return SSL_ERROR_SYSCALL;
2285}
Adam Langley0f4746e2014-08-13 12:26:32 -07002286
Adam Langleyfcf25832014-12-18 17:42:32 -08002287int SSL_do_handshake(SSL *s) {
2288 int ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002289
Adam Langleyfcf25832014-12-18 17:42:32 -08002290 if (s->handshake_func == NULL) {
2291 OPENSSL_PUT_ERROR(SSL, SSL_do_handshake, SSL_R_CONNECTION_TYPE_NOT_SET);
2292 return -1;
2293 }
Adam Langley95c29f32014-06-20 12:00:00 -07002294
Adam Langleyfcf25832014-12-18 17:42:32 -08002295 s->method->ssl_renegotiate_check(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002296
Adam Langleyfcf25832014-12-18 17:42:32 -08002297 if (SSL_in_init(s)) {
2298 ret = s->handshake_func(s);
2299 }
2300 return ret;
2301}
Adam Langley95c29f32014-06-20 12:00:00 -07002302
Adam Langleyfcf25832014-12-18 17:42:32 -08002303void SSL_set_accept_state(SSL *s) {
2304 s->server = 1;
2305 s->shutdown = 0;
2306 s->state = SSL_ST_ACCEPT | SSL_ST_BEFORE;
2307 s->handshake_func = s->method->ssl_accept;
2308 /* clear the current cipher */
2309 ssl_clear_cipher_ctx(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002310}
Adam Langley95c29f32014-06-20 12:00:00 -07002311
Adam Langleyfcf25832014-12-18 17:42:32 -08002312void SSL_set_connect_state(SSL *s) {
2313 s->server = 0;
2314 s->shutdown = 0;
2315 s->state = SSL_ST_CONNECT | SSL_ST_BEFORE;
2316 s->handshake_func = s->method->ssl_connect;
2317 /* clear the current cipher */
2318 ssl_clear_cipher_ctx(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002319}
Adam Langley95c29f32014-06-20 12:00:00 -07002320
Adam Langleyfcf25832014-12-18 17:42:32 -08002321int ssl_undefined_function(SSL *s) {
2322 OPENSSL_PUT_ERROR(SSL, ssl_undefined_function,
2323 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2324 return 0;
2325}
Adam Langley95c29f32014-06-20 12:00:00 -07002326
Adam Langleyfcf25832014-12-18 17:42:32 -08002327int ssl_undefined_void_function(void) {
2328 OPENSSL_PUT_ERROR(SSL, ssl_undefined_void_function,
2329 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2330 return 0;
2331}
Adam Langley95c29f32014-06-20 12:00:00 -07002332
Adam Langleyfcf25832014-12-18 17:42:32 -08002333int ssl_undefined_const_function(const SSL *s) {
2334 OPENSSL_PUT_ERROR(SSL, ssl_undefined_const_function,
2335 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2336 return 0;
2337}
Adam Langley95c29f32014-06-20 12:00:00 -07002338
Adam Langleyfcf25832014-12-18 17:42:32 -08002339static const char *ssl_get_version(int version) {
2340 switch (version) {
2341 case TLS1_2_VERSION:
2342 return "TLSv1.2";
Adam Langley95c29f32014-06-20 12:00:00 -07002343
Adam Langleyfcf25832014-12-18 17:42:32 -08002344 case TLS1_1_VERSION:
2345 return "TLSv1.1";
Adam Langley95c29f32014-06-20 12:00:00 -07002346
Adam Langleyfcf25832014-12-18 17:42:32 -08002347 case TLS1_VERSION:
2348 return "TLSv1";
Adam Langley95c29f32014-06-20 12:00:00 -07002349
Adam Langleyfcf25832014-12-18 17:42:32 -08002350 case SSL3_VERSION:
2351 return "SSLv3";
Adam Langley95c29f32014-06-20 12:00:00 -07002352
David Benjamin1c722b72015-04-20 13:53:10 -04002353 case DTLS1_VERSION:
2354 return "DTLSv1";
2355
2356 case DTLS1_2_VERSION:
2357 return "DTLSv1.2";
2358
Adam Langleyfcf25832014-12-18 17:42:32 -08002359 default:
2360 return "unknown";
2361 }
2362}
Adam Langley95c29f32014-06-20 12:00:00 -07002363
Adam Langleyfcf25832014-12-18 17:42:32 -08002364const char *SSL_get_version(const SSL *s) {
2365 return ssl_get_version(s->version);
2366}
Adam Langley95c29f32014-06-20 12:00:00 -07002367
Adam Langleyfcf25832014-12-18 17:42:32 -08002368const char *SSL_SESSION_get_version(const SSL_SESSION *sess) {
2369 return ssl_get_version(sess->ssl_version);
2370}
Adam Langley95c29f32014-06-20 12:00:00 -07002371
Adam Langleyfcf25832014-12-18 17:42:32 -08002372void ssl_clear_cipher_ctx(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002373 if (s->aead_read_ctx != NULL) {
2374 EVP_AEAD_CTX_cleanup(&s->aead_read_ctx->ctx);
2375 OPENSSL_free(s->aead_read_ctx);
2376 s->aead_read_ctx = NULL;
2377 }
Adam Langleya5dc5452014-06-20 12:00:00 -07002378
Adam Langleyfcf25832014-12-18 17:42:32 -08002379 if (s->aead_write_ctx != NULL) {
2380 EVP_AEAD_CTX_cleanup(&s->aead_write_ctx->ctx);
2381 OPENSSL_free(s->aead_write_ctx);
2382 s->aead_write_ctx = NULL;
2383 }
2384}
Adam Langley95c29f32014-06-20 12:00:00 -07002385
Adam Langleyfcf25832014-12-18 17:42:32 -08002386X509 *SSL_get_certificate(const SSL *s) {
2387 if (s->cert != NULL) {
2388 return s->cert->key->x509;
2389 }
2390
2391 return NULL;
2392}
2393
2394EVP_PKEY *SSL_get_privatekey(const SSL *s) {
2395 if (s->cert != NULL) {
2396 return s->cert->key->privatekey;
2397 }
2398
2399 return NULL;
2400}
2401
2402X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
2403 if (ctx->cert != NULL) {
2404 return ctx->cert->key->x509;
2405 }
2406
2407 return NULL;
2408}
2409
2410EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
2411 if (ctx->cert != NULL) {
2412 return ctx->cert->key->privatekey;
2413 }
2414
2415 return NULL;
2416}
2417
2418const SSL_CIPHER *SSL_get_current_cipher(const SSL *s) {
2419 if (s->session != NULL && s->session->cipher != NULL) {
2420 return s->session->cipher;
2421 }
2422
2423 return NULL;
2424}
2425
2426const void *SSL_get_current_compression(SSL *s) { return NULL; }
2427
2428const void *SSL_get_current_expansion(SSL *s) { return NULL; }
2429
2430int ssl_init_wbio_buffer(SSL *s, int push) {
2431 BIO *bbio;
2432
2433 if (s->bbio == NULL) {
2434 bbio = BIO_new(BIO_f_buffer());
2435 if (bbio == NULL) {
2436 return 0;
2437 }
2438 s->bbio = bbio;
2439 } else {
2440 bbio = s->bbio;
2441 if (s->bbio == s->wbio) {
2442 s->wbio = BIO_pop(s->wbio);
2443 }
2444 }
2445
2446 BIO_reset(bbio);
2447 if (!BIO_set_read_buffer_size(bbio, 1)) {
2448 OPENSSL_PUT_ERROR(SSL, ssl_init_wbio_buffer, ERR_R_BUF_LIB);
2449 return 0;
2450 }
2451
2452 if (push) {
2453 if (s->wbio != bbio) {
2454 s->wbio = BIO_push(bbio, s->wbio);
2455 }
2456 } else {
2457 if (s->wbio == bbio) {
2458 s->wbio = BIO_pop(bbio);
2459 }
2460 }
2461
2462 return 1;
2463}
2464
2465void ssl_free_wbio_buffer(SSL *s) {
2466 if (s->bbio == NULL) {
2467 return;
2468 }
2469
2470 if (s->bbio == s->wbio) {
2471 /* remove buffering */
2472 s->wbio = BIO_pop(s->wbio);
2473 }
2474
2475 BIO_free(s->bbio);
2476 s->bbio = NULL;
2477}
2478
2479void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
2480 ctx->quiet_shutdown = mode;
2481}
2482
2483int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
2484 return ctx->quiet_shutdown;
2485}
2486
2487void SSL_set_quiet_shutdown(SSL *s, int mode) { s->quiet_shutdown = mode; }
2488
2489int SSL_get_quiet_shutdown(const SSL *s) { return s->quiet_shutdown; }
2490
2491void SSL_set_shutdown(SSL *s, int mode) { s->shutdown = mode; }
2492
2493int SSL_get_shutdown(const SSL *s) { return s->shutdown; }
2494
2495int SSL_version(const SSL *s) { return s->version; }
2496
2497SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
2498
2499SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
2500 if (ssl->ctx == ctx) {
2501 return ssl->ctx;
2502 }
2503
2504 if (ctx == NULL) {
2505 ctx = ssl->initial_ctx;
2506 }
2507
2508 if (ssl->cert != NULL) {
2509 ssl_cert_free(ssl->cert);
2510 }
2511
2512 ssl->cert = ssl_cert_dup(ctx->cert);
2513 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
2514 if (ssl->ctx != NULL) {
2515 SSL_CTX_free(ssl->ctx); /* decrement reference count */
2516 }
2517 ssl->ctx = ctx;
2518
2519 ssl->sid_ctx_length = ctx->sid_ctx_length;
2520 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2521 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2522
2523 return ssl->ctx;
2524}
2525
2526int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
2527 return X509_STORE_set_default_paths(ctx->cert_store);
2528}
Adam Langley95c29f32014-06-20 12:00:00 -07002529
2530int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
Adam Langleyfcf25832014-12-18 17:42:32 -08002531 const char *CApath) {
2532 return X509_STORE_load_locations(ctx->cert_store, CAfile, CApath);
2533}
Adam Langley95c29f32014-06-20 12:00:00 -07002534
2535void SSL_set_info_callback(SSL *ssl,
Adam Langleyfcf25832014-12-18 17:42:32 -08002536 void (*cb)(const SSL *ssl, int type, int val)) {
2537 ssl->info_callback = cb;
2538}
Adam Langley95c29f32014-06-20 12:00:00 -07002539
Adam Langleyfcf25832014-12-18 17:42:32 -08002540void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/, int /*type*/,
2541 int /*val*/) {
2542 return ssl->info_callback;
2543}
Adam Langley95c29f32014-06-20 12:00:00 -07002544
Adam Langleyfcf25832014-12-18 17:42:32 -08002545int SSL_state(const SSL *ssl) { return ssl->state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002546
Adam Langleyfcf25832014-12-18 17:42:32 -08002547void SSL_set_state(SSL *ssl, int state) { ssl->state = state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002548
Adam Langleyfcf25832014-12-18 17:42:32 -08002549void SSL_set_verify_result(SSL *ssl, long arg) { ssl->verify_result = arg; }
Adam Langley95c29f32014-06-20 12:00:00 -07002550
Adam Langleyfcf25832014-12-18 17:42:32 -08002551long SSL_get_verify_result(const SSL *ssl) { return ssl->verify_result; }
Adam Langley95c29f32014-06-20 12:00:00 -07002552
Adam Langleyfcf25832014-12-18 17:42:32 -08002553int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2554 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002555 int index;
2556 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp,
2557 new_func, dup_func, free_func)) {
2558 return -1;
2559 }
2560 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002561}
Adam Langley95c29f32014-06-20 12:00:00 -07002562
Adam Langleyfcf25832014-12-18 17:42:32 -08002563int SSL_set_ex_data(SSL *s, int idx, void *arg) {
2564 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2565}
Adam Langley95c29f32014-06-20 12:00:00 -07002566
Adam Langleyfcf25832014-12-18 17:42:32 -08002567void *SSL_get_ex_data(const SSL *s, int idx) {
2568 return CRYPTO_get_ex_data(&s->ex_data, idx);
2569}
Adam Langley95c29f32014-06-20 12:00:00 -07002570
Adam Langleyfcf25832014-12-18 17:42:32 -08002571int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2572 CRYPTO_EX_dup *dup_func,
2573 CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002574 int index;
2575 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp,
2576 new_func, dup_func, free_func)) {
2577 return -1;
2578 }
2579 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002580}
Adam Langley95c29f32014-06-20 12:00:00 -07002581
Adam Langleyfcf25832014-12-18 17:42:32 -08002582int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg) {
2583 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2584}
Adam Langley95c29f32014-06-20 12:00:00 -07002585
Adam Langleyfcf25832014-12-18 17:42:32 -08002586void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx) {
2587 return CRYPTO_get_ex_data(&s->ex_data, idx);
2588}
Adam Langley95c29f32014-06-20 12:00:00 -07002589
Adam Langleyfcf25832014-12-18 17:42:32 -08002590int ssl_ok(SSL *s) { return 1; }
Adam Langley95c29f32014-06-20 12:00:00 -07002591
Adam Langleyfcf25832014-12-18 17:42:32 -08002592X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
2593 return ctx->cert_store;
2594}
Adam Langley95c29f32014-06-20 12:00:00 -07002595
Adam Langleyfcf25832014-12-18 17:42:32 -08002596void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
2597 if (ctx->cert_store != NULL) {
2598 X509_STORE_free(ctx->cert_store);
2599 }
2600 ctx->cert_store = store;
2601}
Adam Langley95c29f32014-06-20 12:00:00 -07002602
Adam Langleyfcf25832014-12-18 17:42:32 -08002603int SSL_want(const SSL *s) { return s->rwstate; }
Adam Langley95c29f32014-06-20 12:00:00 -07002604
Adam Langleyfcf25832014-12-18 17:42:32 -08002605void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
2606 RSA *(*cb)(SSL *ssl, int is_export,
2607 int keylength)) {
2608 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_RSA_CB, (void (*)(void))cb);
2609}
Adam Langley95c29f32014-06-20 12:00:00 -07002610
Adam Langleyfcf25832014-12-18 17:42:32 -08002611void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
2612 int keylength)) {
2613 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_RSA_CB, (void (*)(void))cb);
2614}
2615
2616void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
2617 DH *(*dh)(SSL *ssl, int is_export,
2618 int keylength)) {
2619 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh);
2620}
2621
2622void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*dh)(SSL *ssl, int is_export,
2623 int keylength)) {
2624 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh);
2625}
2626
2627void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,
2628 EC_KEY *(*ecdh)(SSL *ssl, int is_export,
2629 int keylength)) {
2630 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH_CB, (void (*)(void))ecdh);
2631}
2632
2633void SSL_set_tmp_ecdh_callback(SSL *ssl,
2634 EC_KEY *(*ecdh)(SSL *ssl, int is_export,
2635 int keylength)) {
2636 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_ECDH_CB, (void (*)(void))ecdh);
2637}
2638
2639int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
2640 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2641 OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_psk_identity_hint,
2642 SSL_R_DATA_LENGTH_TOO_LONG);
2643 return 0;
2644 }
2645
2646 if (ctx->psk_identity_hint != NULL) {
2647 OPENSSL_free(ctx->psk_identity_hint);
2648 }
2649
2650 if (identity_hint != NULL) {
2651 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2652 if (ctx->psk_identity_hint == NULL) {
2653 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002654 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002655 } else {
2656 ctx->psk_identity_hint = NULL;
2657 }
Adam Langley95c29f32014-06-20 12:00:00 -07002658
Adam Langleyfcf25832014-12-18 17:42:32 -08002659 return 1;
2660}
2661
2662int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint) {
2663 if (s == NULL) {
2664 return 0;
2665 }
2666
2667 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2668 OPENSSL_PUT_ERROR(SSL, SSL_use_psk_identity_hint,
2669 SSL_R_DATA_LENGTH_TOO_LONG);
2670 return 0;
2671 }
2672
2673 /* Clear currently configured hint, if any. */
2674 if (s->psk_identity_hint != NULL) {
2675 OPENSSL_free(s->psk_identity_hint);
2676 s->psk_identity_hint = NULL;
2677 }
2678
2679 if (identity_hint != NULL) {
2680 s->psk_identity_hint = BUF_strdup(identity_hint);
2681 if (s->psk_identity_hint == NULL) {
2682 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002683 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002684 }
Adam Langley95c29f32014-06-20 12:00:00 -07002685
Adam Langleyfcf25832014-12-18 17:42:32 -08002686 return 1;
2687}
Adam Langley95c29f32014-06-20 12:00:00 -07002688
Adam Langleyfcf25832014-12-18 17:42:32 -08002689const char *SSL_get_psk_identity_hint(const SSL *s) {
2690 if (s == NULL) {
2691 return NULL;
2692 }
2693 return s->psk_identity_hint;
2694}
Adam Langley95c29f32014-06-20 12:00:00 -07002695
Adam Langleyfcf25832014-12-18 17:42:32 -08002696const char *SSL_get_psk_identity(const SSL *s) {
2697 if (s == NULL || s->session == NULL) {
2698 return NULL;
2699 }
Adam Langley95c29f32014-06-20 12:00:00 -07002700
Adam Langleyfcf25832014-12-18 17:42:32 -08002701 return s->session->psk_identity;
2702}
Adam Langley95c29f32014-06-20 12:00:00 -07002703
Adam Langleyfcf25832014-12-18 17:42:32 -08002704void SSL_set_psk_client_callback(
2705 SSL *s, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2706 unsigned int max_identity_len, uint8_t *psk,
2707 unsigned int max_psk_len)) {
2708 s->psk_client_callback = cb;
2709}
Adam Langley95c29f32014-06-20 12:00:00 -07002710
Adam Langleyfcf25832014-12-18 17:42:32 -08002711void SSL_CTX_set_psk_client_callback(
2712 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2713 unsigned int max_identity_len,
2714 uint8_t *psk, unsigned int max_psk_len)) {
2715 ctx->psk_client_callback = cb;
2716}
Adam Langley95c29f32014-06-20 12:00:00 -07002717
Adam Langleyfcf25832014-12-18 17:42:32 -08002718void SSL_set_psk_server_callback(
2719 SSL *s, unsigned int (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
2720 unsigned int max_psk_len)) {
2721 s->psk_server_callback = cb;
2722}
Adam Langley95c29f32014-06-20 12:00:00 -07002723
Adam Langleyfcf25832014-12-18 17:42:32 -08002724void SSL_CTX_set_psk_server_callback(
2725 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *identity,
2726 uint8_t *psk, unsigned int max_psk_len)) {
2727 ctx->psk_server_callback = cb;
2728}
Adam Langley95c29f32014-06-20 12:00:00 -07002729
Adam Langleyfcf25832014-12-18 17:42:32 -08002730void SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) {
2731 ctx->min_version = version;
2732}
Adam Langley95c29f32014-06-20 12:00:00 -07002733
Adam Langleyfcf25832014-12-18 17:42:32 -08002734void SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) {
2735 ctx->max_version = version;
2736}
Adam Langley0289c732014-06-20 12:00:00 -07002737
Adam Langleyfcf25832014-12-18 17:42:32 -08002738void SSL_set_min_version(SSL *ssl, uint16_t version) {
2739 ssl->min_version = version;
2740}
Adam Langley0289c732014-06-20 12:00:00 -07002741
Adam Langleyfcf25832014-12-18 17:42:32 -08002742void SSL_set_max_version(SSL *ssl, uint16_t version) {
2743 ssl->max_version = version;
2744}
Adam Langley95c29f32014-06-20 12:00:00 -07002745
Adam Langleyfcf25832014-12-18 17:42:32 -08002746void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
2747 void (*cb)(int write_p, int version,
2748 int content_type, const void *buf,
2749 size_t len, SSL *ssl, void *arg)) {
2750 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2751}
2752void SSL_set_msg_callback(SSL *ssl,
2753 void (*cb)(int write_p, int version, int content_type,
2754 const void *buf, size_t len, SSL *ssl,
2755 void *arg)) {
2756 SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2757}
Adam Langley95c29f32014-06-20 12:00:00 -07002758
Adam Langleyfcf25832014-12-18 17:42:32 -08002759void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio) {
2760 if (ctx->keylog_bio != NULL) {
2761 BIO_free(ctx->keylog_bio);
2762 }
2763 ctx->keylog_bio = keylog_bio;
2764}
Adam Langley95c29f32014-06-20 12:00:00 -07002765
Adam Langleyfcf25832014-12-18 17:42:32 -08002766static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
2767 static const char hextable[] = "0123456789abcdef";
2768 uint8_t *out;
2769 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002770
Adam Langleyfcf25832014-12-18 17:42:32 -08002771 if (!CBB_add_space(cbb, &out, in_len * 2)) {
2772 return 0;
2773 }
Adam Langley95c29f32014-06-20 12:00:00 -07002774
Adam Langleyfcf25832014-12-18 17:42:32 -08002775 for (i = 0; i < in_len; i++) {
2776 *(out++) = (uint8_t)hextable[in[i] >> 4];
2777 *(out++) = (uint8_t)hextable[in[i] & 0xf];
2778 }
Adam Langley95c29f32014-06-20 12:00:00 -07002779
Adam Langleyfcf25832014-12-18 17:42:32 -08002780 return 1;
2781}
David Benjamin859ec3c2014-09-02 16:29:36 -04002782
2783int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -08002784 const uint8_t *encrypted_premaster,
2785 size_t encrypted_premaster_len,
2786 const uint8_t *premaster,
2787 size_t premaster_len) {
2788 BIO *bio = ctx->keylog_bio;
2789 CBB cbb;
2790 uint8_t *out;
2791 size_t out_len;
2792 int ret;
David Benjamin859ec3c2014-09-02 16:29:36 -04002793
Adam Langleyfcf25832014-12-18 17:42:32 -08002794 if (bio == NULL) {
2795 return 1;
2796 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002797
Adam Langleyfcf25832014-12-18 17:42:32 -08002798 if (encrypted_premaster_len < 8) {
2799 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_rsa_client_key_exchange,
2800 ERR_R_INTERNAL_ERROR);
2801 return 0;
2802 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002803
Adam Langleyfcf25832014-12-18 17:42:32 -08002804 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len * 2 + 1)) {
2805 return 0;
2806 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002807
Adam Langleyfcf25832014-12-18 17:42:32 -08002808 if (!CBB_add_bytes(&cbb, (const uint8_t *)"RSA ", 4) ||
2809 /* Only the first 8 bytes of the encrypted premaster secret are
2810 * logged. */
2811 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
2812 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2813 !cbb_add_hex(&cbb, premaster, premaster_len) ||
2814 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2815 !CBB_finish(&cbb, &out, &out_len)) {
2816 CBB_cleanup(&cbb);
2817 return 0;
2818 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002819
Adam Langleyfcf25832014-12-18 17:42:32 -08002820 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2821 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2822 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
David Benjamin859ec3c2014-09-02 16:29:36 -04002823
Adam Langleyfcf25832014-12-18 17:42:32 -08002824 OPENSSL_free(out);
2825 return ret;
Adam Langley95f22882014-06-20 12:00:00 -07002826}
2827
Adam Langleyfcf25832014-12-18 17:42:32 -08002828int ssl_ctx_log_master_secret(SSL_CTX *ctx, const uint8_t *client_random,
2829 size_t client_random_len, const uint8_t *master,
2830 size_t master_len) {
2831 BIO *bio = ctx->keylog_bio;
2832 CBB cbb;
2833 uint8_t *out;
2834 size_t out_len;
2835 int ret;
Adam Langleyadb739e2014-06-20 12:00:00 -07002836
Adam Langleyfcf25832014-12-18 17:42:32 -08002837 if (bio == NULL) {
2838 return 1;
2839 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002840
Adam Langleyfcf25832014-12-18 17:42:32 -08002841 if (client_random_len != 32) {
2842 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_master_secret, ERR_R_INTERNAL_ERROR);
2843 return 0;
2844 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002845
Adam Langleyfcf25832014-12-18 17:42:32 -08002846 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len * 2 + 1)) {
2847 return 0;
2848 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002849
Adam Langleyfcf25832014-12-18 17:42:32 -08002850 if (!CBB_add_bytes(&cbb, (const uint8_t *)"CLIENT_RANDOM ", 14) ||
2851 !cbb_add_hex(&cbb, client_random, 32) ||
2852 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2853 !cbb_add_hex(&cbb, master, master_len) ||
2854 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2855 !CBB_finish(&cbb, &out, &out_len)) {
2856 CBB_cleanup(&cbb);
2857 return 0;
2858 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002859
Adam Langleyfcf25832014-12-18 17:42:32 -08002860 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2861 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2862 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
David Benjamine99e9122014-12-11 01:46:01 -05002863
Adam Langleyfcf25832014-12-18 17:42:32 -08002864 OPENSSL_free(out);
2865 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -07002866}
2867
David Benjamined7c4752015-02-16 19:16:46 -05002868int SSL_in_false_start(const SSL *s) {
2869 return s->s3->tmp.in_false_start;
2870}
2871
Adam Langleyfcf25832014-12-18 17:42:32 -08002872int SSL_cutthrough_complete(const SSL *s) {
David Benjamined7c4752015-02-16 19:16:46 -05002873 return SSL_in_false_start(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002874}
Adam Langley95c29f32014-06-20 12:00:00 -07002875
Adam Langleyfcf25832014-12-18 17:42:32 -08002876void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
2877 size_t *ssl_session_size) {
2878 *ssl_size = sizeof(SSL);
2879 *ssl_ctx_size = sizeof(SSL_CTX);
2880 *ssl_session_size = sizeof(SSL_SESSION);
2881}
Feng Lu41aa3252014-11-21 22:47:56 -08002882
David Benjamined7c4752015-02-16 19:16:46 -05002883int ssl3_can_false_start(const SSL *s) {
David Benjamin195dc782015-02-19 13:27:05 -05002884 const SSL_CIPHER *const cipher = SSL_get_current_cipher(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002885
Adam Langleye631d962015-04-01 13:11:01 -07002886 /* False Start only for TLS 1.2 with an ECDHE+AEAD cipher and ALPN or NPN. */
David Benjamin195dc782015-02-19 13:27:05 -05002887 return !SSL_IS_DTLS(s) &&
2888 SSL_version(s) >= TLS1_2_VERSION &&
2889 (s->s3->alpn_selected || s->s3->next_proto_neg_seen) &&
2890 cipher != NULL &&
Adam Langleye631d962015-04-01 13:11:01 -07002891 cipher->algorithm_mkey == SSL_kECDHE &&
David Benjamin195dc782015-02-19 13:27:05 -05002892 (cipher->algorithm_enc == SSL_AES128GCM ||
2893 cipher->algorithm_enc == SSL_AES256GCM ||
2894 cipher->algorithm_enc == SSL_CHACHA20POLY1305);
Adam Langleyfcf25832014-12-18 17:42:32 -08002895}
2896
2897const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version) {
2898 switch (version) {
2899 case SSL3_VERSION:
2900 return &SSLv3_enc_data;
2901
2902 case TLS1_VERSION:
2903 return &TLSv1_enc_data;
2904
David Benjamin9e13e1a2015-03-05 01:56:32 -05002905 case DTLS1_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002906 case TLS1_1_VERSION:
2907 return &TLSv1_1_enc_data;
2908
David Benjamin9e13e1a2015-03-05 01:56:32 -05002909 case DTLS1_2_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002910 case TLS1_2_VERSION:
2911 return &TLSv1_2_enc_data;
2912
Adam Langleyfcf25832014-12-18 17:42:32 -08002913 default:
2914 return NULL;
2915 }
2916}
2917
2918uint16_t ssl3_get_max_server_version(const SSL *s) {
2919 uint16_t max_version;
2920
2921 if (SSL_IS_DTLS(s)) {
2922 max_version = (s->max_version != 0) ? s->max_version : DTLS1_2_VERSION;
2923 if (!(s->options & SSL_OP_NO_DTLSv1_2) && DTLS1_2_VERSION >= max_version) {
2924 return DTLS1_2_VERSION;
2925 }
2926 if (!(s->options & SSL_OP_NO_DTLSv1) && DTLS1_VERSION >= max_version) {
2927 return DTLS1_VERSION;
2928 }
2929 return 0;
2930 }
2931
2932 max_version = (s->max_version != 0) ? s->max_version : TLS1_2_VERSION;
2933 if (!(s->options & SSL_OP_NO_TLSv1_2) && TLS1_2_VERSION <= max_version) {
2934 return TLS1_2_VERSION;
2935 }
2936 if (!(s->options & SSL_OP_NO_TLSv1_1) && TLS1_1_VERSION <= max_version) {
2937 return TLS1_1_VERSION;
2938 }
2939 if (!(s->options & SSL_OP_NO_TLSv1) && TLS1_VERSION <= max_version) {
2940 return TLS1_VERSION;
2941 }
2942 if (!(s->options & SSL_OP_NO_SSLv3) && SSL3_VERSION <= max_version) {
2943 return SSL3_VERSION;
2944 }
2945 return 0;
2946}
2947
2948uint16_t ssl3_get_mutual_version(SSL *s, uint16_t client_version) {
2949 uint16_t version = 0;
2950
2951 if (SSL_IS_DTLS(s)) {
2952 /* Clamp client_version to max_version. */
2953 if (s->max_version != 0 && client_version < s->max_version) {
2954 client_version = s->max_version;
2955 }
2956
2957 if (client_version <= DTLS1_2_VERSION && !(s->options & SSL_OP_NO_DTLSv1_2)) {
2958 version = DTLS1_2_VERSION;
2959 } else if (client_version <= DTLS1_VERSION &&
David Benjaminb18f0242015-03-10 18:30:08 -04002960 !(s->options & SSL_OP_NO_DTLSv1)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002961 version = DTLS1_VERSION;
2962 }
2963
2964 /* Check against min_version. */
2965 if (version != 0 && s->min_version != 0 && version > s->min_version) {
2966 return 0;
2967 }
2968 return version;
2969 } else {
2970 /* Clamp client_version to max_version. */
2971 if (s->max_version != 0 && client_version > s->max_version) {
2972 client_version = s->max_version;
2973 }
2974
2975 if (client_version >= TLS1_2_VERSION && !(s->options & SSL_OP_NO_TLSv1_2)) {
2976 version = TLS1_2_VERSION;
2977 } else if (client_version >= TLS1_1_VERSION &&
2978 !(s->options & SSL_OP_NO_TLSv1_1)) {
2979 version = TLS1_1_VERSION;
2980 } else if (client_version >= TLS1_VERSION && !(s->options & SSL_OP_NO_TLSv1)) {
2981 version = TLS1_VERSION;
2982 } else if (client_version >= SSL3_VERSION && !(s->options & SSL_OP_NO_SSLv3)) {
2983 version = SSL3_VERSION;
2984 }
2985
2986 /* Check against min_version. */
2987 if (version != 0 && s->min_version != 0 && version < s->min_version) {
2988 return 0;
2989 }
2990 return version;
2991 }
2992}
2993
2994uint16_t ssl3_get_max_client_version(SSL *s) {
2995 unsigned long options = s->options;
2996 uint16_t version = 0;
2997
2998 /* OpenSSL's API for controlling versions entails blacklisting individual
2999 * protocols. This has two problems. First, on the client, the protocol can
3000 * only express a contiguous range of versions. Second, a library consumer
3001 * trying to set a maximum version cannot disable protocol versions that get
3002 * added in a future version of the library.
3003 *
3004 * To account for both of these, OpenSSL interprets the client-side bitmask
3005 * as a min/max range by picking the lowest contiguous non-empty range of
3006 * enabled protocols. Note that this means it is impossible to set a maximum
3007 * version of TLS 1.2 in a future-proof way.
3008 *
3009 * By this scheme, the maximum version is the lowest version V such that V is
3010 * enabled and V+1 is disabled or unimplemented. */
3011 if (SSL_IS_DTLS(s)) {
3012 if (!(options & SSL_OP_NO_DTLSv1_2)) {
3013 version = DTLS1_2_VERSION;
3014 }
3015 if (!(options & SSL_OP_NO_DTLSv1) && (options & SSL_OP_NO_DTLSv1_2)) {
3016 version = DTLS1_VERSION;
3017 }
3018 if (s->max_version != 0 && version < s->max_version) {
3019 version = s->max_version;
3020 }
3021 } else {
3022 if (!(options & SSL_OP_NO_TLSv1_2)) {
3023 version = TLS1_2_VERSION;
3024 }
3025 if (!(options & SSL_OP_NO_TLSv1_1) && (options & SSL_OP_NO_TLSv1_2)) {
3026 version = TLS1_1_VERSION;
3027 }
3028 if (!(options & SSL_OP_NO_TLSv1) && (options & SSL_OP_NO_TLSv1_1)) {
3029 version = TLS1_VERSION;
3030 }
3031 if (!(options & SSL_OP_NO_SSLv3) && (options & SSL_OP_NO_TLSv1)) {
3032 version = SSL3_VERSION;
3033 }
3034 if (s->max_version != 0 && version > s->max_version) {
3035 version = s->max_version;
3036 }
3037 }
3038
3039 return version;
3040}
3041
3042int ssl3_is_version_enabled(SSL *s, uint16_t version) {
3043 if (SSL_IS_DTLS(s)) {
3044 if (s->max_version != 0 && version < s->max_version) {
3045 return 0;
3046 }
3047 if (s->min_version != 0 && version > s->min_version) {
3048 return 0;
3049 }
3050
3051 switch (version) {
3052 case DTLS1_VERSION:
3053 return !(s->options & SSL_OP_NO_DTLSv1);
3054
3055 case DTLS1_2_VERSION:
3056 return !(s->options & SSL_OP_NO_DTLSv1_2);
3057
3058 default:
3059 return 0;
3060 }
3061 } else {
3062 if (s->max_version != 0 && version > s->max_version) {
3063 return 0;
3064 }
3065 if (s->min_version != 0 && version < s->min_version) {
3066 return 0;
3067 }
3068
3069 switch (version) {
3070 case SSL3_VERSION:
3071 return !(s->options & SSL_OP_NO_SSLv3);
3072
3073 case TLS1_VERSION:
3074 return !(s->options & SSL_OP_NO_TLSv1);
3075
3076 case TLS1_1_VERSION:
3077 return !(s->options & SSL_OP_NO_TLSv1_1);
3078
3079 case TLS1_2_VERSION:
3080 return !(s->options & SSL_OP_NO_TLSv1_2);
3081
3082 default:
3083 return 0;
3084 }
3085 }
3086}
3087
David Benjaminea72bd02014-12-21 21:27:41 -05003088uint16_t ssl3_version_from_wire(SSL *s, uint16_t wire_version) {
3089 if (!SSL_IS_DTLS(s)) {
3090 return wire_version;
3091 }
3092
3093 uint16_t tls_version = ~wire_version;
3094 uint16_t version = tls_version + 0x0201;
3095 /* If either component overflowed, clamp it so comparisons still work. */
3096 if ((version >> 8) < (tls_version >> 8)) {
3097 version = 0xff00 | (version & 0xff);
3098 }
3099 if ((version & 0xff) < (tls_version & 0xff)) {
3100 version = (version & 0xff00) | 0xff;
3101 }
3102 /* DTLS 1.0 maps to TLS 1.1, not TLS 1.0. */
3103 if (version == TLS1_VERSION) {
3104 version = TLS1_1_VERSION;
3105 }
3106 return version;
3107}
3108
Adam Langleyfcf25832014-12-18 17:42:32 -08003109int SSL_cache_hit(SSL *s) { return s->hit; }
3110
3111int SSL_is_server(SSL *s) { return s->server; }
3112
Adam Langley524e7172015-02-20 16:04:00 -08003113void SSL_CTX_set_dos_protection_cb(
3114 SSL_CTX *ctx, int (*cb)(const struct ssl_early_callback_ctx *)) {
3115 ctx->dos_protection_cb = cb;
3116}
3117
Adam Langleyfcf25832014-12-18 17:42:32 -08003118void SSL_enable_fastradio_padding(SSL *s, char on_off) {
3119 s->fastradio_padding = on_off;
3120}
Adam Langley44972942015-02-20 13:43:23 -08003121
David Benjaminb16346b2015-04-08 19:16:58 -04003122void SSL_set_reject_peer_renegotiations(SSL *s, int reject) {
3123 s->reject_peer_renegotiations = !!reject;
3124}
3125
Adam Langley44972942015-02-20 13:43:23 -08003126const SSL_CIPHER *SSL_get_cipher_by_value(uint16_t value) {
3127 return ssl3_get_cipher_by_value(value);
3128}
Adam Langley3f92d212015-02-20 15:32:52 -08003129
3130int SSL_get_rc4_state(const SSL *ssl, const RC4_KEY **read_key,
3131 const RC4_KEY **write_key) {
3132 if (ssl->aead_read_ctx == NULL || ssl->aead_write_ctx == NULL) {
3133 return 0;
3134 }
3135
3136 return EVP_AEAD_CTX_get_rc4_state(&ssl->aead_read_ctx->ctx, read_key) &&
3137 EVP_AEAD_CTX_get_rc4_state(&ssl->aead_write_ctx->ctx, write_key);
3138}