blob: 348c7a8ff327fc30f31e86379e426b9146126892 [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
Adam Langleyfcf25832014-12-18 17:42:32 -08001070 case SSL_CTRL_GET_RAW_CIPHERLIST:
1071 if (parg) {
1072 if (s->cert->ciphers_raw == NULL) {
1073 return 0;
1074 }
1075 *(uint8_t **)parg = s->cert->ciphers_raw;
1076 return (int)s->cert->ciphers_rawlen;
1077 }
1078
1079 /* Passing a NULL |parg| returns the size of a single
1080 * cipher suite value. */
1081 return 2;
1082
1083 default:
1084 return s->method->ssl_ctrl(s, cmd, larg, parg);
1085 }
1086}
1087
1088long SSL_callback_ctrl(SSL *s, int cmd, void (*fp)(void)) {
1089 switch (cmd) {
1090 case SSL_CTRL_SET_MSG_CALLBACK:
1091 s->msg_callback =
1092 (void (*)(int write_p, int version, int content_type, const void *buf,
1093 size_t len, SSL *ssl, void *arg))(fp);
1094 return 1;
1095
1096 default:
1097 return s->method->ssl_callback_ctrl(s, cmd, fp);
1098 }
1099}
1100
1101LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) { return ctx->sessions; }
1102
1103long SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg) {
1104 long l;
1105
1106 switch (cmd) {
1107 case SSL_CTRL_GET_READ_AHEAD:
1108 return ctx->read_ahead;
1109
1110 case SSL_CTRL_SET_READ_AHEAD:
1111 l = ctx->read_ahead;
1112 ctx->read_ahead = larg;
1113 return l;
1114
1115 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1116 ctx->msg_callback_arg = parg;
1117 return 1;
1118
1119 case SSL_CTRL_GET_MAX_CERT_LIST:
1120 return ctx->max_cert_list;
1121
1122 case SSL_CTRL_SET_MAX_CERT_LIST:
1123 l = ctx->max_cert_list;
1124 ctx->max_cert_list = larg;
1125 return l;
1126
1127 case SSL_CTRL_SET_SESS_CACHE_SIZE:
1128 l = ctx->session_cache_size;
1129 ctx->session_cache_size = larg;
1130 return l;
1131
1132 case SSL_CTRL_GET_SESS_CACHE_SIZE:
1133 return ctx->session_cache_size;
1134
1135 case SSL_CTRL_SET_SESS_CACHE_MODE:
1136 l = ctx->session_cache_mode;
1137 ctx->session_cache_mode = larg;
1138 return l;
1139
1140 case SSL_CTRL_GET_SESS_CACHE_MODE:
1141 return ctx->session_cache_mode;
1142
1143 case SSL_CTRL_SESS_NUMBER:
1144 return lh_SSL_SESSION_num_items(ctx->sessions);
1145
1146 case SSL_CTRL_SESS_CONNECT:
Adam Langleyfcf25832014-12-18 17:42:32 -08001147 case SSL_CTRL_SESS_CONNECT_GOOD:
Adam Langleyfcf25832014-12-18 17:42:32 -08001148 case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001149 case SSL_CTRL_SESS_ACCEPT:
Adam Langleyfcf25832014-12-18 17:42:32 -08001150 case SSL_CTRL_SESS_ACCEPT_GOOD:
Adam Langleyfcf25832014-12-18 17:42:32 -08001151 case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001152 case SSL_CTRL_SESS_HIT:
Adam Langleyfcf25832014-12-18 17:42:32 -08001153 case SSL_CTRL_SESS_CB_HIT:
Adam Langleyfcf25832014-12-18 17:42:32 -08001154 case SSL_CTRL_SESS_MISSES:
Adam Langleyfcf25832014-12-18 17:42:32 -08001155 case SSL_CTRL_SESS_TIMEOUTS:
Adam Langleyfcf25832014-12-18 17:42:32 -08001156 case SSL_CTRL_SESS_CACHE_FULL:
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001157 /* Statistics are no longer supported.
1158 * TODO(davidben): Try to remove the accessors altogether. */
1159 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001160
1161 case SSL_CTRL_OPTIONS:
1162 return ctx->options |= larg;
1163
1164 case SSL_CTRL_CLEAR_OPTIONS:
1165 return ctx->options &= ~larg;
1166
1167 case SSL_CTRL_MODE:
1168 return ctx->mode |= larg;
1169
1170 case SSL_CTRL_CLEAR_MODE:
1171 return ctx->mode &= ~larg;
1172
1173 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1174 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH) {
1175 return 0;
1176 }
1177 ctx->max_send_fragment = larg;
1178 return 1;
1179
Adam Langleyfcf25832014-12-18 17:42:32 -08001180 default:
1181 return ctx->method->ssl_ctx_ctrl(ctx, cmd, larg, parg);
1182 }
1183}
1184
1185long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void)) {
1186 switch (cmd) {
1187 case SSL_CTRL_SET_MSG_CALLBACK:
1188 ctx->msg_callback =
1189 (void (*)(int write_p, int version, int content_type, const void *buf,
1190 size_t len, SSL *ssl, void *arg))(fp);
1191 return 1;
1192
1193 default:
1194 return ctx->method->ssl_ctx_callback_ctrl(ctx, cmd, fp);
1195 }
1196}
1197
1198int ssl_cipher_id_cmp(const void *in_a, const void *in_b) {
1199 long l;
1200 const SSL_CIPHER *a = in_a;
1201 const SSL_CIPHER *b = in_b;
1202 const long a_id = a->id;
1203 const long b_id = b->id;
1204
1205 l = a_id - b_id;
1206 if (l == 0L) {
1207 return 0;
1208 } else {
1209 return (l > 0) ? 1 : -1;
1210 }
1211}
1212
1213int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **ap, const SSL_CIPHER **bp) {
1214 long l;
1215 const long a_id = (*ap)->id;
1216 const long b_id = (*bp)->id;
1217
1218 l = a_id - b_id;
1219 if (l == 0) {
1220 return 0;
1221 } else {
1222 return (l > 0) ? 1 : -1;
1223 }
1224}
1225
1226/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001227 * preference */
Adam Langleyfcf25832014-12-18 17:42:32 -08001228STACK_OF(SSL_CIPHER) * SSL_get_ciphers(const SSL *s) {
1229 if (s == NULL) {
1230 return NULL;
1231 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001232
Adam Langleyfcf25832014-12-18 17:42:32 -08001233 if (s->cipher_list != NULL) {
1234 return s->cipher_list->ciphers;
1235 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001236
Adam Langleyfcf25832014-12-18 17:42:32 -08001237 if (s->version >= TLS1_1_VERSION && s->ctx != NULL &&
1238 s->ctx->cipher_list_tls11 != NULL) {
1239 return s->ctx->cipher_list_tls11->ciphers;
1240 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001241
Adam Langleyfcf25832014-12-18 17:42:32 -08001242 if (s->ctx != NULL && s->ctx->cipher_list != NULL) {
1243 return s->ctx->cipher_list->ciphers;
1244 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001245
Adam Langleyfcf25832014-12-18 17:42:32 -08001246 return NULL;
1247}
Adam Langley95c29f32014-06-20 12:00:00 -07001248
Adam Langleyfcf25832014-12-18 17:42:32 -08001249/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001250 * algorithm id */
Adam Langleyfcf25832014-12-18 17:42:32 -08001251STACK_OF(SSL_CIPHER) * ssl_get_ciphers_by_id(SSL *s) {
1252 if (s == NULL) {
1253 return NULL;
1254 }
Adam Langley95c29f32014-06-20 12:00:00 -07001255
Adam Langleyfcf25832014-12-18 17:42:32 -08001256 if (s->cipher_list_by_id != NULL) {
1257 return s->cipher_list_by_id;
1258 }
Adam Langley95c29f32014-06-20 12:00:00 -07001259
Adam Langleyfcf25832014-12-18 17:42:32 -08001260 if (s->ctx != NULL && s->ctx->cipher_list_by_id != NULL) {
1261 return s->ctx->cipher_list_by_id;
1262 }
Adam Langley95c29f32014-06-20 12:00:00 -07001263
Adam Langleyfcf25832014-12-18 17:42:32 -08001264 return NULL;
1265}
Adam Langley95c29f32014-06-20 12:00:00 -07001266
Adam Langleyfcf25832014-12-18 17:42:32 -08001267/* The old interface to get the same thing as SSL_get_ciphers() */
1268const char *SSL_get_cipher_list(const SSL *s, int n) {
1269 const SSL_CIPHER *c;
1270 STACK_OF(SSL_CIPHER) * sk;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001271
Adam Langleyfcf25832014-12-18 17:42:32 -08001272 if (s == NULL) {
1273 return NULL;
1274 }
Adam Langley95c29f32014-06-20 12:00:00 -07001275
Adam Langleyfcf25832014-12-18 17:42:32 -08001276 sk = SSL_get_ciphers(s);
1277 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
1278 return NULL;
1279 }
Adam Langley95c29f32014-06-20 12:00:00 -07001280
Adam Langleyfcf25832014-12-18 17:42:32 -08001281 c = sk_SSL_CIPHER_value(sk, n);
1282 if (c == NULL) {
1283 return NULL;
1284 }
Adam Langley95c29f32014-06-20 12:00:00 -07001285
Adam Langleyfcf25832014-12-18 17:42:32 -08001286 return c->name;
1287}
David Benjamin5491e3f2014-09-29 19:33:09 -04001288
Adam Langleyfcf25832014-12-18 17:42:32 -08001289/* specify the ciphers to be used by default by the SSL_CTX */
1290int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
1291 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001292
Adam Langleyfcf25832014-12-18 17:42:32 -08001293 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001294 &ctx->cipher_list_by_id, str);
Adam Langleyfcf25832014-12-18 17:42:32 -08001295 /* ssl_create_cipher_list may return an empty stack if it was unable to find
1296 * a cipher matching the given rule string (for example if the rule string
1297 * specifies a cipher which has been disabled). This is not an error as far
1298 * as ssl_create_cipher_list is concerned, and hence ctx->cipher_list and
1299 * ctx->cipher_list_by_id has been updated. */
1300 if (sk == NULL) {
1301 return 0;
1302 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1303 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1304 return 0;
1305 }
Adam Langley95c29f32014-06-20 12:00:00 -07001306
Adam Langleyfcf25832014-12-18 17:42:32 -08001307 return 1;
1308}
David Benjamin39482a12014-07-20 13:30:15 -04001309
Adam Langleyfcf25832014-12-18 17:42:32 -08001310int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str) {
1311 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001312
David Benjamin71f07942015-04-08 02:36:59 -04001313 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str);
Adam Langleyfcf25832014-12-18 17:42:32 -08001314 if (sk == NULL) {
1315 return 0;
1316 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1317 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list_tls11,
1318 SSL_R_NO_CIPHER_MATCH);
1319 return 0;
1320 }
David Benjamin9f2c0d72014-10-21 22:00:19 -04001321
Adam Langleyfcf25832014-12-18 17:42:32 -08001322 return 1;
1323}
Adam Langley95c29f32014-06-20 12:00:00 -07001324
Adam Langleyfcf25832014-12-18 17:42:32 -08001325/* specify the ciphers to be used by the SSL */
1326int SSL_set_cipher_list(SSL *s, const char *str) {
1327 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001328
Adam Langleyfcf25832014-12-18 17:42:32 -08001329 sk = ssl_create_cipher_list(s->ctx->method, &s->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001330 &s->cipher_list_by_id, str);
David Benjamin39482a12014-07-20 13:30:15 -04001331
Adam Langleyfcf25832014-12-18 17:42:32 -08001332 /* see comment in SSL_CTX_set_cipher_list */
1333 if (sk == NULL) {
1334 return 0;
1335 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1336 OPENSSL_PUT_ERROR(SSL, SSL_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1337 return 0;
1338 }
David Benjamin39482a12014-07-20 13:30:15 -04001339
Adam Langleyfcf25832014-12-18 17:42:32 -08001340 return 1;
1341}
Adam Langley95c29f32014-06-20 12:00:00 -07001342
Adam Langleyfcf25832014-12-18 17:42:32 -08001343int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, uint8_t *p) {
1344 size_t i;
1345 const SSL_CIPHER *c;
1346 CERT *ct = s->cert;
1347 uint8_t *q;
1348 /* Set disabled masks for this session */
1349 ssl_set_client_disabled(s);
Adam Langley29707792014-06-20 12:00:00 -07001350
Adam Langleyfcf25832014-12-18 17:42:32 -08001351 if (sk == NULL) {
1352 return 0;
1353 }
1354 q = p;
Adam Langley95c29f32014-06-20 12:00:00 -07001355
Adam Langleyfcf25832014-12-18 17:42:32 -08001356 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
1357 c = sk_SSL_CIPHER_value(sk, i);
1358 /* Skip disabled ciphers */
1359 if (c->algorithm_ssl & ct->mask_ssl ||
1360 c->algorithm_mkey & ct->mask_k ||
1361 c->algorithm_auth & ct->mask_a) {
1362 continue;
1363 }
1364 s2n(ssl3_get_cipher_value(c), p);
1365 }
1366
1367 /* If all ciphers were disabled, return the error to the caller. */
1368 if (p == q) {
1369 return 0;
1370 }
1371
1372 /* Add SCSVs. */
1373 if (!s->renegotiate) {
1374 s2n(SSL3_CK_SCSV & 0xffff, p);
1375 }
1376
Adam Langley5f0efe02015-02-20 13:03:16 -08001377 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001378 s2n(SSL3_CK_FALLBACK_SCSV & 0xffff, p);
1379 }
1380
1381 return p - q;
1382}
1383
1384STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs) {
1385 CBS cipher_suites = *cbs;
1386 const SSL_CIPHER *c;
1387 STACK_OF(SSL_CIPHER) * sk;
1388
1389 if (s->s3) {
1390 s->s3->send_connection_binding = 0;
1391 }
1392
1393 if (CBS_len(&cipher_suites) % 2 != 0) {
1394 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1395 SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
1396 return NULL;
1397 }
1398
1399 sk = sk_SSL_CIPHER_new_null();
1400 if (sk == NULL) {
1401 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1402 goto err;
1403 }
1404
1405 if (!CBS_stow(&cipher_suites, &s->cert->ciphers_raw,
1406 &s->cert->ciphers_rawlen)) {
1407 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1408 goto err;
1409 }
1410
1411 while (CBS_len(&cipher_suites) > 0) {
1412 uint16_t cipher_suite;
1413
1414 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
1415 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_INTERNAL_ERROR);
1416 goto err;
1417 }
1418
1419 /* Check for SCSV. */
1420 if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff)) {
1421 /* SCSV is fatal if renegotiating. */
1422 if (s->renegotiate) {
1423 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1424 SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1425 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1426 goto err;
1427 }
1428 s->s3->send_connection_binding = 1;
1429 continue;
1430 }
1431
1432 /* Check for FALLBACK_SCSV. */
1433 if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff)) {
1434 uint16_t max_version = ssl3_get_max_server_version(s);
1435 if (SSL_IS_DTLS(s) ? (uint16_t)s->version > max_version
1436 : (uint16_t)s->version < max_version) {
1437 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1438 SSL_R_INAPPROPRIATE_FALLBACK);
1439 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
1440 goto err;
1441 }
1442 continue;
1443 }
1444
1445 c = ssl3_get_cipher_by_value(cipher_suite);
1446 if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
1447 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1448 goto err;
1449 }
1450 }
1451
1452 return sk;
David Benjamin9f2c0d72014-10-21 22:00:19 -04001453
Adam Langley95c29f32014-06-20 12:00:00 -07001454err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001455 if (sk != NULL) {
1456 sk_SSL_CIPHER_free(sk);
1457 }
1458 return NULL;
1459}
Adam Langley95c29f32014-06-20 12:00:00 -07001460
1461
Adam Langleyfcf25832014-12-18 17:42:32 -08001462/* return a servername extension value if provided in Client Hello, or NULL. So
1463 * far, only host_name types are defined (RFC 3546). */
1464const char *SSL_get_servername(const SSL *s, const int type) {
1465 if (type != TLSEXT_NAMETYPE_host_name) {
1466 return NULL;
1467 }
Adam Langley95c29f32014-06-20 12:00:00 -07001468
Adam Langleyfcf25832014-12-18 17:42:32 -08001469 return s->session && !s->tlsext_hostname ? s->session->tlsext_hostname
1470 : s->tlsext_hostname;
1471}
Adam Langley95c29f32014-06-20 12:00:00 -07001472
Adam Langleyfcf25832014-12-18 17:42:32 -08001473int SSL_get_servername_type(const SSL *s) {
1474 if (s->session &&
1475 (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname)) {
1476 return TLSEXT_NAMETYPE_host_name;
1477 }
Adam Langley95c29f32014-06-20 12:00:00 -07001478
Adam Langleyfcf25832014-12-18 17:42:32 -08001479 return -1;
1480}
Adam Langley95c29f32014-06-20 12:00:00 -07001481
Adam Langleyfcf25832014-12-18 17:42:32 -08001482void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
1483 ctx->signed_cert_timestamps_enabled = 1;
1484}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001485
Adam Langleyfcf25832014-12-18 17:42:32 -08001486int SSL_enable_signed_cert_timestamps(SSL *ssl) {
1487 ssl->signed_cert_timestamps_enabled = 1;
1488 return 1;
1489}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001490
Adam Langleyfcf25832014-12-18 17:42:32 -08001491void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
1492 ctx->ocsp_stapling_enabled = 1;
1493}
David Benjamin6c7aed02014-08-27 16:42:38 -04001494
Adam Langleyfcf25832014-12-18 17:42:32 -08001495int SSL_enable_ocsp_stapling(SSL *ssl) {
1496 ssl->ocsp_stapling_enabled = 1;
1497 return 1;
1498}
David Benjamin6c7aed02014-08-27 16:42:38 -04001499
Adam Langleyfcf25832014-12-18 17:42:32 -08001500void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
1501 size_t *out_len) {
1502 SSL_SESSION *session = ssl->session;
Adam Langley3cb50e02014-08-26 14:00:31 -07001503
Adam Langleyfcf25832014-12-18 17:42:32 -08001504 *out_len = 0;
1505 *out = NULL;
1506 if (ssl->server || !session || !session->tlsext_signed_cert_timestamp_list) {
1507 return;
1508 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001509
Adam Langleyfcf25832014-12-18 17:42:32 -08001510 *out = session->tlsext_signed_cert_timestamp_list;
1511 *out_len = session->tlsext_signed_cert_timestamp_list_length;
1512}
David Benjamin6c7aed02014-08-27 16:42:38 -04001513
Adam Langleyfcf25832014-12-18 17:42:32 -08001514void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
1515 size_t *out_len) {
1516 SSL_SESSION *session = ssl->session;
1517
1518 *out_len = 0;
1519 *out = NULL;
1520 if (ssl->server || !session || !session->ocsp_response) {
1521 return;
1522 }
1523 *out = session->ocsp_response;
1524 *out_len = session->ocsp_response_length;
1525}
David Benjamin6c7aed02014-08-27 16:42:38 -04001526
Adam Langley95c29f32014-06-20 12:00:00 -07001527/* SSL_select_next_proto implements the standard protocol selection. It is
1528 * expected that this function is called from the callback set by
1529 * SSL_CTX_set_next_proto_select_cb.
1530 *
1531 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1532 * strings. The length byte itself is not included in the length. A byte
1533 * string of length 0 is invalid. No byte string may be truncated.
1534 *
1535 * The current, but experimental algorithm for selecting the protocol is:
1536 *
1537 * 1) If the server doesn't support NPN then this is indicated to the
1538 * callback. In this case, the client application has to abort the connection
1539 * or have a default application level protocol.
1540 *
1541 * 2) If the server supports NPN, but advertises an empty list then the
1542 * client selects the first protcol in its list, but indicates via the
1543 * API that this fallback case was enacted.
1544 *
1545 * 3) Otherwise, the client finds the first protocol in the server's list
1546 * that it supports and selects this protocol. This is because it's
1547 * assumed that the server has better information about which protocol
1548 * a client should use.
1549 *
1550 * 4) If the client doesn't support any of the server's advertised
1551 * protocols, then this is treated the same as case 2.
1552 *
1553 * It returns either
1554 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1555 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1556 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001557int SSL_select_next_proto(uint8_t **out, uint8_t *outlen, const uint8_t *server,
1558 unsigned int server_len, const uint8_t *client,
1559 unsigned int client_len) {
1560 unsigned int i, j;
1561 const uint8_t *result;
1562 int status = OPENSSL_NPN_UNSUPPORTED;
Adam Langley95c29f32014-06-20 12:00:00 -07001563
Adam Langleyfcf25832014-12-18 17:42:32 -08001564 /* For each protocol in server preference order, see if we support it. */
1565 for (i = 0; i < server_len;) {
1566 for (j = 0; j < client_len;) {
1567 if (server[i] == client[j] &&
1568 memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
1569 /* We found a match */
1570 result = &server[i];
1571 status = OPENSSL_NPN_NEGOTIATED;
1572 goto found;
1573 }
1574 j += client[j];
1575 j++;
1576 }
1577 i += server[i];
1578 i++;
1579 }
Adam Langley95c29f32014-06-20 12:00:00 -07001580
Adam Langleyfcf25832014-12-18 17:42:32 -08001581 /* There's no overlap between our protocols and the server's list. */
1582 result = client;
1583 status = OPENSSL_NPN_NO_OVERLAP;
Adam Langley95c29f32014-06-20 12:00:00 -07001584
Adam Langleyfcf25832014-12-18 17:42:32 -08001585found:
1586 *out = (uint8_t *)result + 1;
1587 *outlen = result[0];
1588 return status;
1589}
Adam Langley95c29f32014-06-20 12:00:00 -07001590
Adam Langley95c29f32014-06-20 12:00:00 -07001591/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
1592 * requested protocol for this connection and returns 0. If the client didn't
1593 * request any protocol, then *data is set to NULL.
1594 *
1595 * Note that the client can request any protocol it chooses. The value returned
1596 * from this function need not be a member of the list of supported protocols
Adam Langleyfcf25832014-12-18 17:42:32 -08001597 * provided by the callback. */
1598void SSL_get0_next_proto_negotiated(const SSL *s, const uint8_t **data,
1599 unsigned *len) {
1600 *data = s->next_proto_negotiated;
1601 if (!*data) {
1602 *len = 0;
1603 } else {
1604 *len = s->next_proto_negotiated_len;
1605 }
Adam Langley95c29f32014-06-20 12:00:00 -07001606}
1607
1608/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
1609 * TLS server needs a list of supported protocols for Next Protocol
1610 * Negotiation. The returned list must be in wire format. The list is returned
1611 * by setting |out| to point to it and |outlen| to its length. This memory will
1612 * not be modified, but one should assume that the SSL* keeps a reference to
1613 * it.
1614 *
Adam Langleyfcf25832014-12-18 17:42:32 -08001615 * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise.
1616 * Otherwise, no such extension will be included in the ServerHello. */
1617void SSL_CTX_set_next_protos_advertised_cb(
1618 SSL_CTX *ctx,
1619 int (*cb)(SSL *ssl, const uint8_t **out, unsigned int *outlen, void *arg),
1620 void *arg) {
1621 ctx->next_protos_advertised_cb = cb;
1622 ctx->next_protos_advertised_cb_arg = arg;
1623}
Adam Langley95c29f32014-06-20 12:00:00 -07001624
1625/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1626 * client needs to select a protocol from the server's provided list. |out|
1627 * must be set to point to the selected protocol (which may be within |in|).
1628 * The length of the protocol name must be written into |outlen|. The server's
1629 * advertised protocols are provided in |in| and |inlen|. The callback can
1630 * assume that |in| is syntactically valid.
1631 *
1632 * The client must select a protocol. It is fatal to the connection if this
1633 * callback returns a value other than SSL_TLSEXT_ERR_OK.
1634 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001635void SSL_CTX_set_next_proto_select_cb(
1636 SSL_CTX *ctx, int (*cb)(SSL *s, uint8_t **out, uint8_t *outlen,
1637 const uint8_t *in, unsigned int inlen, void *arg),
1638 void *arg) {
1639 ctx->next_proto_select_cb = cb;
1640 ctx->next_proto_select_cb_arg = arg;
1641}
Adam Langley95c29f32014-06-20 12:00:00 -07001642
Adam Langleyfcf25832014-12-18 17:42:32 -08001643int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
1644 unsigned protos_len) {
1645 if (ctx->alpn_client_proto_list) {
1646 OPENSSL_free(ctx->alpn_client_proto_list);
1647 }
Adam Langley95c29f32014-06-20 12:00:00 -07001648
Adam Langleyfcf25832014-12-18 17:42:32 -08001649 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1650 if (!ctx->alpn_client_proto_list) {
1651 return 1;
1652 }
1653 ctx->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001654
Adam Langleyfcf25832014-12-18 17:42:32 -08001655 return 0;
1656}
Adam Langley95c29f32014-06-20 12:00:00 -07001657
Adam Langleyfcf25832014-12-18 17:42:32 -08001658int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
1659 if (ssl->alpn_client_proto_list) {
1660 OPENSSL_free(ssl->alpn_client_proto_list);
1661 }
Adam Langley95c29f32014-06-20 12:00:00 -07001662
Adam Langleyfcf25832014-12-18 17:42:32 -08001663 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1664 if (!ssl->alpn_client_proto_list) {
1665 return 1;
1666 }
1667 ssl->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001668
Adam Langleyfcf25832014-12-18 17:42:32 -08001669 return 0;
1670}
Adam Langley95c29f32014-06-20 12:00:00 -07001671
1672/* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
1673 * during ClientHello processing in order to select an ALPN protocol from the
1674 * client's list of offered protocols. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001675void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
1676 int (*cb)(SSL *ssl, const uint8_t **out,
1677 uint8_t *outlen, const uint8_t *in,
1678 unsigned int inlen, void *arg),
1679 void *arg) {
1680 ctx->alpn_select_cb = cb;
1681 ctx->alpn_select_cb_arg = arg;
1682}
Adam Langley95c29f32014-06-20 12:00:00 -07001683
1684/* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
1685 * On return it sets |*data| to point to |*len| bytes of protocol name (not
1686 * including the leading length-prefix byte). If the server didn't respond with
1687 * a negotiated protocol then |*len| will be zero. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001688void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **data,
1689 unsigned *len) {
1690 *data = NULL;
1691 if (ssl->s3) {
1692 *data = ssl->s3->alpn_selected;
1693 }
1694 if (*data == NULL) {
1695 *len = 0;
1696 } else {
1697 *len = ssl->s3->alpn_selected_len;
1698 }
1699}
Adam Langley95c29f32014-06-20 12:00:00 -07001700
David Benjamincfd248b2015-04-03 11:02:24 -04001701int SSL_export_keying_material(SSL *s, uint8_t *out, size_t out_len,
1702 const char *label, size_t label_len,
1703 const uint8_t *context, size_t context_len,
1704 int use_context) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001705 if (s->version < TLS1_VERSION) {
David Benjaminc565ebb2015-04-03 04:06:36 -04001706 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001707 }
Adam Langley95c29f32014-06-20 12:00:00 -07001708
David Benjamincfd248b2015-04-03 11:02:24 -04001709 return s->enc_method->export_keying_material(
1710 s, out, out_len, label, label_len, context, context_len, use_context);
Adam Langleyfcf25832014-12-18 17:42:32 -08001711}
Adam Langley95c29f32014-06-20 12:00:00 -07001712
Adam Langleyfcf25832014-12-18 17:42:32 -08001713static uint32_t ssl_session_hash(const SSL_SESSION *a) {
1714 uint32_t hash =
1715 ((uint32_t)a->session_id[0]) ||
1716 ((uint32_t)a->session_id[1] << 8) ||
1717 ((uint32_t)a->session_id[2] << 16) ||
1718 ((uint32_t)a->session_id[3] << 24);
Adam Langley95c29f32014-06-20 12:00:00 -07001719
Adam Langleyfcf25832014-12-18 17:42:32 -08001720 return hash;
1721}
Adam Langley95c29f32014-06-20 12:00:00 -07001722
1723/* NB: If this function (or indeed the hash function which uses a sort of
1724 * coarser function than this one) is changed, ensure
1725 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1726 * able to construct an SSL_SESSION that will collide with any existing session
1727 * with a matching session ID. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001728static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
1729 if (a->ssl_version != b->ssl_version) {
1730 return 1;
1731 }
Adam Langley95c29f32014-06-20 12:00:00 -07001732
Adam Langleyfcf25832014-12-18 17:42:32 -08001733 if (a->session_id_length != b->session_id_length) {
1734 return 1;
1735 }
Adam Langley95c29f32014-06-20 12:00:00 -07001736
Adam Langleyfcf25832014-12-18 17:42:32 -08001737 return memcmp(a->session_id, b->session_id, a->session_id_length);
1738}
Adam Langley95c29f32014-06-20 12:00:00 -07001739
Adam Langleyfcf25832014-12-18 17:42:32 -08001740SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth) {
1741 SSL_CTX *ret = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001742
Adam Langleyfcf25832014-12-18 17:42:32 -08001743 if (meth == NULL) {
1744 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_NULL_SSL_METHOD_PASSED);
1745 return NULL;
1746 }
Adam Langley95c29f32014-06-20 12:00:00 -07001747
Adam Langleyfcf25832014-12-18 17:42:32 -08001748 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
1749 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
1750 goto err;
1751 }
Adam Langley95c29f32014-06-20 12:00:00 -07001752
Adam Langleyfcf25832014-12-18 17:42:32 -08001753 ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1754 if (ret == NULL) {
1755 goto err;
1756 }
Adam Langley95c29f32014-06-20 12:00:00 -07001757
Adam Langleyfcf25832014-12-18 17:42:32 -08001758 memset(ret, 0, sizeof(SSL_CTX));
Adam Langley95c29f32014-06-20 12:00:00 -07001759
Adam Langleyfcf25832014-12-18 17:42:32 -08001760 ret->method = meth->method;
Adam Langley95c29f32014-06-20 12:00:00 -07001761
Adam Langleyfcf25832014-12-18 17:42:32 -08001762 ret->cert_store = NULL;
1763 ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
1764 ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1765 ret->session_cache_head = NULL;
1766 ret->session_cache_tail = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001767
Adam Langleyfcf25832014-12-18 17:42:32 -08001768 /* We take the system default */
1769 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001770
Adam Langleyfcf25832014-12-18 17:42:32 -08001771 ret->new_session_cb = 0;
1772 ret->remove_session_cb = 0;
1773 ret->get_session_cb = 0;
1774 ret->generate_session_id = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001775
Adam Langleyfcf25832014-12-18 17:42:32 -08001776 ret->references = 1;
1777 ret->quiet_shutdown = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001778
Adam Langleyfcf25832014-12-18 17:42:32 -08001779 ret->info_callback = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001780
Adam Langleyfcf25832014-12-18 17:42:32 -08001781 ret->app_verify_callback = 0;
1782 ret->app_verify_arg = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001783
Adam Langleyfcf25832014-12-18 17:42:32 -08001784 ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
1785 ret->read_ahead = 0;
1786 ret->msg_callback = 0;
1787 ret->msg_callback_arg = NULL;
1788 ret->verify_mode = SSL_VERIFY_NONE;
1789 ret->sid_ctx_length = 0;
1790 ret->default_verify_callback = NULL;
1791 ret->cert = ssl_cert_new();
1792 if (ret->cert == NULL) {
1793 goto err;
1794 }
Adam Langley95c29f32014-06-20 12:00:00 -07001795
Adam Langleyfcf25832014-12-18 17:42:32 -08001796 ret->default_passwd_callback = 0;
1797 ret->default_passwd_callback_userdata = NULL;
1798 ret->client_cert_cb = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001799
Adam Langleyfcf25832014-12-18 17:42:32 -08001800 ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
1801 if (ret->sessions == NULL) {
1802 goto err;
1803 }
1804 ret->cert_store = X509_STORE_new();
1805 if (ret->cert_store == NULL) {
1806 goto err;
1807 }
Adam Langley95c29f32014-06-20 12:00:00 -07001808
Adam Langleyfcf25832014-12-18 17:42:32 -08001809 ssl_create_cipher_list(ret->method, &ret->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001810 &ret->cipher_list_by_id, SSL_DEFAULT_CIPHER_LIST);
Adam Langleyfcf25832014-12-18 17:42:32 -08001811 if (ret->cipher_list == NULL ||
1812 sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0) {
1813 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_LIBRARY_HAS_NO_CIPHERS);
1814 goto err2;
1815 }
Adam Langley95c29f32014-06-20 12:00:00 -07001816
Adam Langleyfcf25832014-12-18 17:42:32 -08001817 ret->param = X509_VERIFY_PARAM_new();
1818 if (!ret->param) {
1819 goto err;
1820 }
Adam Langley95c29f32014-06-20 12:00:00 -07001821
Adam Langleyfcf25832014-12-18 17:42:32 -08001822 ret->client_CA = sk_X509_NAME_new_null();
1823 if (ret->client_CA == NULL) {
1824 goto err;
1825 }
Adam Langley95c29f32014-06-20 12:00:00 -07001826
David Benjamin9f33fc62015-04-15 17:29:53 -04001827 CRYPTO_new_ex_data(&g_ex_data_class_ssl_ctx, ret, &ret->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001828
Adam Langleyfcf25832014-12-18 17:42:32 -08001829 ret->extra_certs = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001830
Adam Langleyfcf25832014-12-18 17:42:32 -08001831 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
David Benjamin422d3a42014-08-20 11:09:03 -04001832
Adam Langleyfcf25832014-12-18 17:42:32 -08001833 ret->tlsext_servername_callback = 0;
1834 ret->tlsext_servername_arg = NULL;
1835 /* Setup RFC4507 ticket keys */
1836 if (!RAND_bytes(ret->tlsext_tick_key_name, 16) ||
1837 !RAND_bytes(ret->tlsext_tick_hmac_key, 16) ||
1838 !RAND_bytes(ret->tlsext_tick_aes_key, 16)) {
1839 ret->options |= SSL_OP_NO_TICKET;
1840 }
Adam Langley95c29f32014-06-20 12:00:00 -07001841
Adam Langleyfcf25832014-12-18 17:42:32 -08001842 ret->tlsext_status_cb = 0;
1843 ret->tlsext_status_arg = NULL;
David Benjamin82c9e902014-12-12 15:55:27 -05001844
Adam Langleyfcf25832014-12-18 17:42:32 -08001845 ret->next_protos_advertised_cb = 0;
1846 ret->next_proto_select_cb = 0;
1847 ret->psk_identity_hint = NULL;
1848 ret->psk_client_callback = NULL;
1849 ret->psk_server_callback = NULL;
1850
1851 /* Default is to connect to non-RI servers. When RI is more widely deployed
1852 * might change this. */
1853 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
1854
1855 /* Lock the SSL_CTX to the specified version, for compatibility with legacy
1856 * uses of SSL_METHOD. */
1857 if (meth->version != 0) {
1858 SSL_CTX_set_max_version(ret, meth->version);
1859 SSL_CTX_set_min_version(ret, meth->version);
1860 }
1861
1862 return ret;
1863
Adam Langley95c29f32014-06-20 12:00:00 -07001864err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001865 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -07001866err2:
Adam Langleyfcf25832014-12-18 17:42:32 -08001867 if (ret != NULL) {
1868 SSL_CTX_free(ret);
1869 }
1870 return NULL;
1871}
Adam Langley95c29f32014-06-20 12:00:00 -07001872
David Benjamin4fcc2e22015-04-22 12:58:16 -04001873void SSL_CTX_free(SSL_CTX *ctx) {
1874 if (ctx == NULL ||
1875 CRYPTO_add(&ctx->references, -1, CRYPTO_LOCK_SSL_CTX) > 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001876 return;
1877 }
Adam Langley95c29f32014-06-20 12:00:00 -07001878
David Benjamin4fcc2e22015-04-22 12:58:16 -04001879 if (ctx->param) {
1880 X509_VERIFY_PARAM_free(ctx->param);
Adam Langleyfcf25832014-12-18 17:42:32 -08001881 }
Adam Langley95c29f32014-06-20 12:00:00 -07001882
Adam Langleyfcf25832014-12-18 17:42:32 -08001883 /* Free internal session cache. However: the remove_cb() may reference the
1884 * ex_data of SSL_CTX, thus the ex_data store can only be removed after the
1885 * sessions were flushed. As the ex_data handling routines might also touch
1886 * the session cache, the most secure solution seems to be: empty (flush) the
1887 * cache, then free ex_data, then finally free the cache. (See ticket
1888 * [openssl.org #212].) */
David Benjamin4fcc2e22015-04-22 12:58:16 -04001889 if (ctx->sessions != NULL) {
1890 SSL_CTX_flush_sessions(ctx, 0);
Adam Langleyfcf25832014-12-18 17:42:32 -08001891 }
Adam Langley95c29f32014-06-20 12:00:00 -07001892
David Benjamin4fcc2e22015-04-22 12:58:16 -04001893 CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, ctx, &ctx->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001894
David Benjamin4fcc2e22015-04-22 12:58:16 -04001895 if (ctx->sessions != NULL) {
1896 lh_SSL_SESSION_free(ctx->sessions);
Adam Langleyfcf25832014-12-18 17:42:32 -08001897 }
David Benjamin4fcc2e22015-04-22 12:58:16 -04001898 if (ctx->cert_store != NULL) {
1899 X509_STORE_free(ctx->cert_store);
Adam Langleyfcf25832014-12-18 17:42:32 -08001900 }
David Benjamin4fcc2e22015-04-22 12:58:16 -04001901 if (ctx->cipher_list != NULL) {
1902 ssl_cipher_preference_list_free(ctx->cipher_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001903 }
David Benjamin4fcc2e22015-04-22 12:58:16 -04001904 if (ctx->cipher_list_by_id != NULL) {
1905 sk_SSL_CIPHER_free(ctx->cipher_list_by_id);
Adam Langleyfcf25832014-12-18 17:42:32 -08001906 }
David Benjamin4fcc2e22015-04-22 12:58:16 -04001907 if (ctx->cipher_list_tls11 != NULL) {
1908 ssl_cipher_preference_list_free(ctx->cipher_list_tls11);
Adam Langleyfcf25832014-12-18 17:42:32 -08001909 }
David Benjamin4fcc2e22015-04-22 12:58:16 -04001910 if (ctx->cert != NULL) {
1911 ssl_cert_free(ctx->cert);
Adam Langleyfcf25832014-12-18 17:42:32 -08001912 }
David Benjamin4fcc2e22015-04-22 12:58:16 -04001913 if (ctx->client_CA != NULL) {
1914 sk_X509_NAME_pop_free(ctx->client_CA, X509_NAME_free);
Adam Langleyfcf25832014-12-18 17:42:32 -08001915 }
David Benjamin4fcc2e22015-04-22 12:58:16 -04001916 if (ctx->extra_certs != NULL) {
1917 sk_X509_pop_free(ctx->extra_certs, X509_free);
Adam Langleyfcf25832014-12-18 17:42:32 -08001918 }
David Benjamin4fcc2e22015-04-22 12:58:16 -04001919 if (ctx->srtp_profiles) {
1920 sk_SRTP_PROTECTION_PROFILE_free(ctx->srtp_profiles);
Adam Langleyfcf25832014-12-18 17:42:32 -08001921 }
David Benjamin4fcc2e22015-04-22 12:58:16 -04001922 if (ctx->psk_identity_hint) {
1923 OPENSSL_free(ctx->psk_identity_hint);
Adam Langleyfcf25832014-12-18 17:42:32 -08001924 }
David Benjamin4fcc2e22015-04-22 12:58:16 -04001925 if (ctx->tlsext_ecpointformatlist) {
1926 OPENSSL_free(ctx->tlsext_ecpointformatlist);
Adam Langleyfcf25832014-12-18 17:42:32 -08001927 }
David Benjamin4fcc2e22015-04-22 12:58:16 -04001928 if (ctx->tlsext_ellipticcurvelist) {
1929 OPENSSL_free(ctx->tlsext_ellipticcurvelist);
Adam Langleyfcf25832014-12-18 17:42:32 -08001930 }
David Benjamin4fcc2e22015-04-22 12:58:16 -04001931 if (ctx->alpn_client_proto_list != NULL) {
1932 OPENSSL_free(ctx->alpn_client_proto_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001933 }
David Benjamin4fcc2e22015-04-22 12:58:16 -04001934 if (ctx->tlsext_channel_id_private) {
1935 EVP_PKEY_free(ctx->tlsext_channel_id_private);
Adam Langleyfcf25832014-12-18 17:42:32 -08001936 }
David Benjamin4fcc2e22015-04-22 12:58:16 -04001937 if (ctx->keylog_bio) {
1938 BIO_free(ctx->keylog_bio);
Adam Langleyfcf25832014-12-18 17:42:32 -08001939 }
Adam Langley95c29f32014-06-20 12:00:00 -07001940
David Benjamin4fcc2e22015-04-22 12:58:16 -04001941 OPENSSL_free(ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08001942}
Adam Langley95c29f32014-06-20 12:00:00 -07001943
Adam Langleyfcf25832014-12-18 17:42:32 -08001944void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) {
1945 ctx->default_passwd_callback = cb;
1946}
Adam Langley95c29f32014-06-20 12:00:00 -07001947
Adam Langleyfcf25832014-12-18 17:42:32 -08001948void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u) {
1949 ctx->default_passwd_callback_userdata = u;
1950}
Adam Langley95c29f32014-06-20 12:00:00 -07001951
Adam Langleyfcf25832014-12-18 17:42:32 -08001952void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
1953 int (*cb)(X509_STORE_CTX *, void *),
1954 void *arg) {
1955 ctx->app_verify_callback = cb;
1956 ctx->app_verify_arg = arg;
1957}
Adam Langley95c29f32014-06-20 12:00:00 -07001958
Adam Langleyfcf25832014-12-18 17:42:32 -08001959void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
1960 int (*cb)(int, X509_STORE_CTX *)) {
1961 ctx->verify_mode = mode;
1962 ctx->default_verify_callback = cb;
1963}
Adam Langley95c29f32014-06-20 12:00:00 -07001964
Adam Langleyfcf25832014-12-18 17:42:32 -08001965void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
1966 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
1967}
Adam Langley1258b6a2014-06-20 12:00:00 -07001968
Adam Langleyfcf25832014-12-18 17:42:32 -08001969void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb)(SSL *ssl, void *arg),
1970 void *arg) {
1971 ssl_cert_set_cert_cb(c->cert, cb, arg);
1972}
David Benjamin859ec3c2014-09-02 16:29:36 -04001973
Adam Langleyfcf25832014-12-18 17:42:32 -08001974void SSL_set_cert_cb(SSL *s, int (*cb)(SSL *ssl, void *arg), void *arg) {
1975 ssl_cert_set_cert_cb(s->cert, cb, arg);
1976}
Adam Langley95c29f32014-06-20 12:00:00 -07001977
Adam Langleyfcf25832014-12-18 17:42:32 -08001978static int ssl_has_key(SSL *s, size_t idx) {
1979 CERT_PKEY *cpk = &s->cert->pkeys[idx];
1980 return cpk->x509 && cpk->privatekey;
1981}
David Benjamin033e5f42014-11-13 18:47:41 -05001982
David Benjamin107db582015-04-08 00:41:59 -04001983void ssl_get_compatible_server_ciphers(SSL *s, uint32_t *out_mask_k,
1984 uint32_t *out_mask_a) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001985 CERT *c = s->cert;
1986 int rsa_enc, rsa_sign, dh_tmp;
David Benjamin107db582015-04-08 00:41:59 -04001987 uint32_t mask_k, mask_a;
Adam Langleyfcf25832014-12-18 17:42:32 -08001988 int have_ecc_cert, ecdsa_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001989 X509 *x;
Adam Langley95c29f32014-06-20 12:00:00 -07001990
Adam Langleyfcf25832014-12-18 17:42:32 -08001991 if (c == NULL) {
1992 /* TODO(davidben): Is this codepath possible? */
1993 *out_mask_k = 0;
1994 *out_mask_a = 0;
1995 return;
1996 }
Adam Langley95c29f32014-06-20 12:00:00 -07001997
Adam Langleyfcf25832014-12-18 17:42:32 -08001998 dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001999
Adam Langleyfcf25832014-12-18 17:42:32 -08002000 rsa_enc = ssl_has_key(s, SSL_PKEY_RSA_ENC);
2001 rsa_sign = ssl_has_key(s, SSL_PKEY_RSA_SIGN);
2002 have_ecc_cert = ssl_has_key(s, SSL_PKEY_ECC);
2003 mask_k = 0;
2004 mask_a = 0;
David Benjaminf31e6812014-11-13 18:05:55 -05002005
Adam Langleyfcf25832014-12-18 17:42:32 -08002006 if (rsa_enc) {
2007 mask_k |= SSL_kRSA;
2008 }
2009 if (dh_tmp) {
David Benjamin7061e282015-03-19 11:10:48 -04002010 mask_k |= SSL_kDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08002011 }
2012 if (rsa_enc || rsa_sign) {
2013 mask_a |= SSL_aRSA;
2014 }
Adam Langley95c29f32014-06-20 12:00:00 -07002015
Adam Langleyfcf25832014-12-18 17:42:32 -08002016 /* An ECC certificate may be usable for ECDSA cipher suites depending on the
2017 * key usage extension and on the client's curve preferences. */
2018 if (have_ecc_cert) {
2019 x = c->pkeys[SSL_PKEY_ECC].x509;
2020 /* This call populates extension flags (ex_flags). */
2021 X509_check_purpose(x, -1, 0);
2022 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE)
2023 ? (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)
2024 : 1;
2025 if (!tls1_check_ec_cert(s, x)) {
2026 ecdsa_ok = 0;
2027 }
2028 if (ecdsa_ok) {
2029 mask_a |= SSL_aECDSA;
2030 }
2031 }
Adam Langley95c29f32014-06-20 12:00:00 -07002032
Adam Langleyfcf25832014-12-18 17:42:32 -08002033 /* If we are considering an ECC cipher suite that uses an ephemeral EC
2034 * key, check it. */
David Benjamindd978782015-04-24 15:20:13 -04002035 if (tls1_check_ec_tmp_key(s)) {
David Benjamin7061e282015-03-19 11:10:48 -04002036 mask_k |= SSL_kECDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08002037 }
Adam Langley95c29f32014-06-20 12:00:00 -07002038
Adam Langleyfcf25832014-12-18 17:42:32 -08002039 /* PSK requires a server callback. */
2040 if (s->psk_server_callback != NULL) {
2041 mask_k |= SSL_kPSK;
2042 mask_a |= SSL_aPSK;
2043 }
Adam Langley95c29f32014-06-20 12:00:00 -07002044
Adam Langleyfcf25832014-12-18 17:42:32 -08002045 *out_mask_k = mask_k;
2046 *out_mask_a = mask_a;
2047}
Adam Langley95c29f32014-06-20 12:00:00 -07002048
2049/* This handy macro borrowed from crypto/x509v3/v3_purp.c */
2050#define ku_reject(x, usage) \
Adam Langleyfcf25832014-12-18 17:42:32 -08002051 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
Adam Langley95c29f32014-06-20 12:00:00 -07002052
Adam Langleyfcf25832014-12-18 17:42:32 -08002053int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002054 const SSL_CIPHER *cs = s->s3->tmp.new_cipher;
David Benjamin3fa27772015-04-17 22:32:19 -04002055 uint32_t alg_a = cs->algorithm_auth;
2056 int signature_nid = 0, md_nid = 0, pk_nid = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002057
Adam Langleyfcf25832014-12-18 17:42:32 -08002058 /* This call populates the ex_flags field correctly */
2059 X509_check_purpose(x, -1, 0);
2060 if (x->sig_alg && x->sig_alg->algorithm) {
2061 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
2062 OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
2063 }
2064 if (alg_a & SSL_aECDSA) {
2065 /* key usage, if present, must allow signing */
2066 if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE)) {
2067 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg,
2068 SSL_R_ECC_CERT_NOT_FOR_SIGNING);
2069 return 0;
2070 }
2071 }
Adam Langley95c29f32014-06-20 12:00:00 -07002072
Adam Langleyfcf25832014-12-18 17:42:32 -08002073 return 1; /* all checks are ok */
2074}
Adam Langley95c29f32014-06-20 12:00:00 -07002075
Adam Langleyfcf25832014-12-18 17:42:32 -08002076static int ssl_get_server_cert_index(const SSL *s) {
2077 int idx;
2078 idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
2079 if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509) {
2080 idx = SSL_PKEY_RSA_SIGN;
2081 }
2082 if (idx == -1) {
2083 OPENSSL_PUT_ERROR(SSL, ssl_get_server_cert_index, ERR_R_INTERNAL_ERROR);
2084 }
2085 return idx;
2086}
Adam Langley95c29f32014-06-20 12:00:00 -07002087
Adam Langleyfcf25832014-12-18 17:42:32 -08002088CERT_PKEY *ssl_get_server_send_pkey(const SSL *s) {
2089 int i = ssl_get_server_cert_index(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002090
Adam Langleyfcf25832014-12-18 17:42:32 -08002091 /* This may or may not be an error. */
2092 if (i < 0) {
2093 return NULL;
2094 }
Adam Langley95c29f32014-06-20 12:00:00 -07002095
Adam Langleyfcf25832014-12-18 17:42:32 -08002096 /* May be NULL. */
2097 return &s->cert->pkeys[i];
2098}
Adam Langley95c29f32014-06-20 12:00:00 -07002099
Adam Langleyfcf25832014-12-18 17:42:32 -08002100EVP_PKEY *ssl_get_sign_pkey(SSL *s, const SSL_CIPHER *cipher) {
David Benjamin3fa27772015-04-17 22:32:19 -04002101 uint32_t alg_a = cipher->algorithm_auth;
2102 CERT *c = s->cert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002103 int idx = -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002104
Adam Langleyfcf25832014-12-18 17:42:32 -08002105 if (alg_a & SSL_aRSA) {
2106 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL) {
2107 idx = SSL_PKEY_RSA_SIGN;
2108 } else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL) {
2109 idx = SSL_PKEY_RSA_ENC;
2110 }
2111 } else if ((alg_a & SSL_aECDSA) &&
2112 (c->pkeys[SSL_PKEY_ECC].privatekey != NULL)) {
2113 idx = SSL_PKEY_ECC;
2114 }
Adam Langley95c29f32014-06-20 12:00:00 -07002115
Adam Langleyfcf25832014-12-18 17:42:32 -08002116 if (idx == -1) {
2117 OPENSSL_PUT_ERROR(SSL, ssl_get_sign_pkey, ERR_R_INTERNAL_ERROR);
2118 return NULL;
2119 }
Adam Langley95c29f32014-06-20 12:00:00 -07002120
Adam Langleyfcf25832014-12-18 17:42:32 -08002121 return c->pkeys[idx].privatekey;
2122}
Adam Langley95c29f32014-06-20 12:00:00 -07002123
Adam Langleyfcf25832014-12-18 17:42:32 -08002124void ssl_update_cache(SSL *s, int mode) {
David Benjaminb6d0c6d2015-03-19 19:07:26 -04002125 /* Never cache sessions with empty session IDs. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002126 if (s->session->session_id_length == 0) {
2127 return;
2128 }
Adam Langley95c29f32014-06-20 12:00:00 -07002129
David Benjaminb6d0c6d2015-03-19 19:07:26 -04002130 SSL_CTX *ctx = s->initial_ctx;
2131 if ((ctx->session_cache_mode & mode) == mode && !s->hit &&
2132 ((ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) ||
2133 SSL_CTX_add_session(ctx, s->session)) &&
2134 ctx->new_session_cb != NULL) {
2135 /* Note: |new_session_cb| is called whether the internal session cache is
2136 * used or not. */
2137 if (!ctx->new_session_cb(s, SSL_SESSION_up_ref(s->session))) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002138 SSL_SESSION_free(s->session);
2139 }
2140 }
Adam Langley95c29f32014-06-20 12:00:00 -07002141
David Benjaminb6d0c6d2015-03-19 19:07:26 -04002142 if (!(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR) &&
2143 !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) &&
2144 (ctx->session_cache_mode & mode) == mode) {
2145 /* Automatically flush the internal session cache every 255 connections. */
2146 int flush_cache = 0;
2147 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2148 ctx->handshakes_since_cache_flush++;
2149 if (ctx->handshakes_since_cache_flush >= 255) {
2150 flush_cache = 1;
2151 ctx->handshakes_since_cache_flush = 0;
2152 }
2153 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
2154
2155 if (flush_cache) {
2156 SSL_CTX_flush_sessions(ctx, (unsigned long)time(NULL));
Adam Langleyfcf25832014-12-18 17:42:32 -08002157 }
2158 }
2159}
Adam Langley95c29f32014-06-20 12:00:00 -07002160
David Benjamin1a5c50f2015-03-11 16:22:37 -04002161int SSL_get_error(const SSL *s, int ret_code) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002162 int reason;
David Benjamin1a5c50f2015-03-11 16:22:37 -04002163 uint32_t err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002164 BIO *bio;
Adam Langley95c29f32014-06-20 12:00:00 -07002165
David Benjamin1a5c50f2015-03-11 16:22:37 -04002166 if (ret_code > 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002167 return SSL_ERROR_NONE;
2168 }
Adam Langley95c29f32014-06-20 12:00:00 -07002169
Adam Langleyfcf25832014-12-18 17:42:32 -08002170 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
2171 * where we do encode the error */
David Benjamin1a5c50f2015-03-11 16:22:37 -04002172 err = ERR_peek_error();
2173 if (err != 0) {
2174 if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002175 return SSL_ERROR_SYSCALL;
2176 }
2177 return SSL_ERROR_SSL;
2178 }
Adam Langley95c29f32014-06-20 12:00:00 -07002179
David Benjamin1a5c50f2015-03-11 16:22:37 -04002180 if (ret_code == 0) {
David Benjamin9a38e922015-01-22 16:06:11 -05002181 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2182 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) {
2183 /* The socket was cleanly shut down with a close_notify. */
2184 return SSL_ERROR_ZERO_RETURN;
2185 }
2186 /* An EOF was observed which violates the protocol, and the underlying
2187 * transport does not participate in the error queue. Bubble up to the
2188 * caller. */
2189 return SSL_ERROR_SYSCALL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002190 }
Adam Langleyb2ce0582014-06-20 12:00:00 -07002191
Adam Langleyfcf25832014-12-18 17:42:32 -08002192 if (SSL_want_session(s)) {
2193 return SSL_ERROR_PENDING_SESSION;
2194 }
Adam Langley95c29f32014-06-20 12:00:00 -07002195
Adam Langleyfcf25832014-12-18 17:42:32 -08002196 if (SSL_want_certificate(s)) {
2197 return SSL_ERROR_PENDING_CERTIFICATE;
2198 }
Adam Langley95c29f32014-06-20 12:00:00 -07002199
Adam Langleyfcf25832014-12-18 17:42:32 -08002200 if (SSL_want_read(s)) {
2201 bio = SSL_get_rbio(s);
2202 if (BIO_should_read(bio)) {
2203 return SSL_ERROR_WANT_READ;
2204 }
Adam Langley95c29f32014-06-20 12:00:00 -07002205
Adam Langleyfcf25832014-12-18 17:42:32 -08002206 if (BIO_should_write(bio)) {
2207 /* This one doesn't make too much sense ... We never try to write to the
2208 * rbio, and an application program where rbio and wbio are separate
2209 * couldn't even know what it should wait for. However if we ever set
2210 * s->rwstate incorrectly (so that we have SSL_want_read(s) instead of
2211 * SSL_want_write(s)) and rbio and wbio *are* the same, this test works
2212 * around that bug; so it might be safer to keep it. */
2213 return SSL_ERROR_WANT_WRITE;
2214 }
Adam Langley95c29f32014-06-20 12:00:00 -07002215
Adam Langleyfcf25832014-12-18 17:42:32 -08002216 if (BIO_should_io_special(bio)) {
2217 reason = BIO_get_retry_reason(bio);
2218 if (reason == BIO_RR_CONNECT) {
2219 return SSL_ERROR_WANT_CONNECT;
2220 }
Adam Langley95c29f32014-06-20 12:00:00 -07002221
Adam Langleyfcf25832014-12-18 17:42:32 -08002222 if (reason == BIO_RR_ACCEPT) {
2223 return SSL_ERROR_WANT_ACCEPT;
2224 }
Adam Langley95c29f32014-06-20 12:00:00 -07002225
Adam Langleyfcf25832014-12-18 17:42:32 -08002226 return SSL_ERROR_SYSCALL; /* unknown */
2227 }
2228 }
Adam Langley95c29f32014-06-20 12:00:00 -07002229
Adam Langleyfcf25832014-12-18 17:42:32 -08002230 if (SSL_want_write(s)) {
2231 bio = SSL_get_wbio(s);
2232 if (BIO_should_write(bio)) {
2233 return SSL_ERROR_WANT_WRITE;
2234 }
Adam Langley95c29f32014-06-20 12:00:00 -07002235
Adam Langleyfcf25832014-12-18 17:42:32 -08002236 if (BIO_should_read(bio)) {
2237 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2238 return SSL_ERROR_WANT_READ;
2239 }
Adam Langley95c29f32014-06-20 12:00:00 -07002240
Adam Langleyfcf25832014-12-18 17:42:32 -08002241 if (BIO_should_io_special(bio)) {
2242 reason = BIO_get_retry_reason(bio);
2243 if (reason == BIO_RR_CONNECT) {
2244 return SSL_ERROR_WANT_CONNECT;
2245 }
Adam Langley95c29f32014-06-20 12:00:00 -07002246
Adam Langleyfcf25832014-12-18 17:42:32 -08002247 if (reason == BIO_RR_ACCEPT) {
2248 return SSL_ERROR_WANT_ACCEPT;
2249 }
Adam Langley95c29f32014-06-20 12:00:00 -07002250
Adam Langleyfcf25832014-12-18 17:42:32 -08002251 return SSL_ERROR_SYSCALL;
2252 }
2253 }
Adam Langley95c29f32014-06-20 12:00:00 -07002254
Adam Langleyfcf25832014-12-18 17:42:32 -08002255 if (SSL_want_x509_lookup(s)) {
2256 return SSL_ERROR_WANT_X509_LOOKUP;
2257 }
Adam Langley95c29f32014-06-20 12:00:00 -07002258
Adam Langleyfcf25832014-12-18 17:42:32 -08002259 if (SSL_want_channel_id_lookup(s)) {
2260 return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
2261 }
Adam Langley0f4746e2014-08-13 12:26:32 -07002262
Adam Langleyfcf25832014-12-18 17:42:32 -08002263 return SSL_ERROR_SYSCALL;
2264}
Adam Langley0f4746e2014-08-13 12:26:32 -07002265
Adam Langleyfcf25832014-12-18 17:42:32 -08002266int SSL_do_handshake(SSL *s) {
2267 int ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002268
Adam Langleyfcf25832014-12-18 17:42:32 -08002269 if (s->handshake_func == NULL) {
2270 OPENSSL_PUT_ERROR(SSL, SSL_do_handshake, SSL_R_CONNECTION_TYPE_NOT_SET);
2271 return -1;
2272 }
Adam Langley95c29f32014-06-20 12:00:00 -07002273
Adam Langleyfcf25832014-12-18 17:42:32 -08002274 s->method->ssl_renegotiate_check(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002275
Adam Langleyfcf25832014-12-18 17:42:32 -08002276 if (SSL_in_init(s)) {
2277 ret = s->handshake_func(s);
2278 }
2279 return ret;
2280}
Adam Langley95c29f32014-06-20 12:00:00 -07002281
Adam Langleyfcf25832014-12-18 17:42:32 -08002282void SSL_set_accept_state(SSL *s) {
2283 s->server = 1;
2284 s->shutdown = 0;
2285 s->state = SSL_ST_ACCEPT | SSL_ST_BEFORE;
2286 s->handshake_func = s->method->ssl_accept;
2287 /* clear the current cipher */
2288 ssl_clear_cipher_ctx(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002289}
Adam Langley95c29f32014-06-20 12:00:00 -07002290
Adam Langleyfcf25832014-12-18 17:42:32 -08002291void SSL_set_connect_state(SSL *s) {
2292 s->server = 0;
2293 s->shutdown = 0;
2294 s->state = SSL_ST_CONNECT | SSL_ST_BEFORE;
2295 s->handshake_func = s->method->ssl_connect;
2296 /* clear the current cipher */
2297 ssl_clear_cipher_ctx(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002298}
Adam Langley95c29f32014-06-20 12:00:00 -07002299
Adam Langleyfcf25832014-12-18 17:42:32 -08002300int ssl_undefined_function(SSL *s) {
2301 OPENSSL_PUT_ERROR(SSL, ssl_undefined_function,
2302 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2303 return 0;
2304}
Adam Langley95c29f32014-06-20 12:00:00 -07002305
Adam Langleyfcf25832014-12-18 17:42:32 -08002306int ssl_undefined_void_function(void) {
2307 OPENSSL_PUT_ERROR(SSL, ssl_undefined_void_function,
2308 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2309 return 0;
2310}
Adam Langley95c29f32014-06-20 12:00:00 -07002311
Adam Langleyfcf25832014-12-18 17:42:32 -08002312int ssl_undefined_const_function(const SSL *s) {
2313 OPENSSL_PUT_ERROR(SSL, ssl_undefined_const_function,
2314 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2315 return 0;
2316}
Adam Langley95c29f32014-06-20 12:00:00 -07002317
Adam Langleyfcf25832014-12-18 17:42:32 -08002318static const char *ssl_get_version(int version) {
2319 switch (version) {
2320 case TLS1_2_VERSION:
2321 return "TLSv1.2";
Adam Langley95c29f32014-06-20 12:00:00 -07002322
Adam Langleyfcf25832014-12-18 17:42:32 -08002323 case TLS1_1_VERSION:
2324 return "TLSv1.1";
Adam Langley95c29f32014-06-20 12:00:00 -07002325
Adam Langleyfcf25832014-12-18 17:42:32 -08002326 case TLS1_VERSION:
2327 return "TLSv1";
Adam Langley95c29f32014-06-20 12:00:00 -07002328
Adam Langleyfcf25832014-12-18 17:42:32 -08002329 case SSL3_VERSION:
2330 return "SSLv3";
Adam Langley95c29f32014-06-20 12:00:00 -07002331
David Benjamin1c722b72015-04-20 13:53:10 -04002332 case DTLS1_VERSION:
2333 return "DTLSv1";
2334
2335 case DTLS1_2_VERSION:
2336 return "DTLSv1.2";
2337
Adam Langleyfcf25832014-12-18 17:42:32 -08002338 default:
2339 return "unknown";
2340 }
2341}
Adam Langley95c29f32014-06-20 12:00:00 -07002342
Adam Langleyfcf25832014-12-18 17:42:32 -08002343const char *SSL_get_version(const SSL *s) {
2344 return ssl_get_version(s->version);
2345}
Adam Langley95c29f32014-06-20 12:00:00 -07002346
Adam Langleyfcf25832014-12-18 17:42:32 -08002347const char *SSL_SESSION_get_version(const SSL_SESSION *sess) {
2348 return ssl_get_version(sess->ssl_version);
2349}
Adam Langley95c29f32014-06-20 12:00:00 -07002350
Adam Langleyfcf25832014-12-18 17:42:32 -08002351void ssl_clear_cipher_ctx(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002352 if (s->aead_read_ctx != NULL) {
2353 EVP_AEAD_CTX_cleanup(&s->aead_read_ctx->ctx);
2354 OPENSSL_free(s->aead_read_ctx);
2355 s->aead_read_ctx = NULL;
2356 }
Adam Langleya5dc5452014-06-20 12:00:00 -07002357
Adam Langleyfcf25832014-12-18 17:42:32 -08002358 if (s->aead_write_ctx != NULL) {
2359 EVP_AEAD_CTX_cleanup(&s->aead_write_ctx->ctx);
2360 OPENSSL_free(s->aead_write_ctx);
2361 s->aead_write_ctx = NULL;
2362 }
2363}
Adam Langley95c29f32014-06-20 12:00:00 -07002364
Adam Langleyfcf25832014-12-18 17:42:32 -08002365X509 *SSL_get_certificate(const SSL *s) {
2366 if (s->cert != NULL) {
2367 return s->cert->key->x509;
2368 }
2369
2370 return NULL;
2371}
2372
2373EVP_PKEY *SSL_get_privatekey(const SSL *s) {
2374 if (s->cert != NULL) {
2375 return s->cert->key->privatekey;
2376 }
2377
2378 return NULL;
2379}
2380
2381X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
2382 if (ctx->cert != NULL) {
2383 return ctx->cert->key->x509;
2384 }
2385
2386 return NULL;
2387}
2388
2389EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
2390 if (ctx->cert != NULL) {
2391 return ctx->cert->key->privatekey;
2392 }
2393
2394 return NULL;
2395}
2396
2397const SSL_CIPHER *SSL_get_current_cipher(const SSL *s) {
2398 if (s->session != NULL && s->session->cipher != NULL) {
2399 return s->session->cipher;
2400 }
2401
2402 return NULL;
2403}
2404
2405const void *SSL_get_current_compression(SSL *s) { return NULL; }
2406
2407const void *SSL_get_current_expansion(SSL *s) { return NULL; }
2408
2409int ssl_init_wbio_buffer(SSL *s, int push) {
2410 BIO *bbio;
2411
2412 if (s->bbio == NULL) {
2413 bbio = BIO_new(BIO_f_buffer());
2414 if (bbio == NULL) {
2415 return 0;
2416 }
2417 s->bbio = bbio;
2418 } else {
2419 bbio = s->bbio;
2420 if (s->bbio == s->wbio) {
2421 s->wbio = BIO_pop(s->wbio);
2422 }
2423 }
2424
2425 BIO_reset(bbio);
2426 if (!BIO_set_read_buffer_size(bbio, 1)) {
2427 OPENSSL_PUT_ERROR(SSL, ssl_init_wbio_buffer, ERR_R_BUF_LIB);
2428 return 0;
2429 }
2430
2431 if (push) {
2432 if (s->wbio != bbio) {
2433 s->wbio = BIO_push(bbio, s->wbio);
2434 }
2435 } else {
2436 if (s->wbio == bbio) {
2437 s->wbio = BIO_pop(bbio);
2438 }
2439 }
2440
2441 return 1;
2442}
2443
2444void ssl_free_wbio_buffer(SSL *s) {
2445 if (s->bbio == NULL) {
2446 return;
2447 }
2448
2449 if (s->bbio == s->wbio) {
2450 /* remove buffering */
2451 s->wbio = BIO_pop(s->wbio);
2452 }
2453
2454 BIO_free(s->bbio);
2455 s->bbio = NULL;
2456}
2457
2458void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
2459 ctx->quiet_shutdown = mode;
2460}
2461
2462int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
2463 return ctx->quiet_shutdown;
2464}
2465
2466void SSL_set_quiet_shutdown(SSL *s, int mode) { s->quiet_shutdown = mode; }
2467
2468int SSL_get_quiet_shutdown(const SSL *s) { return s->quiet_shutdown; }
2469
2470void SSL_set_shutdown(SSL *s, int mode) { s->shutdown = mode; }
2471
2472int SSL_get_shutdown(const SSL *s) { return s->shutdown; }
2473
2474int SSL_version(const SSL *s) { return s->version; }
2475
2476SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
2477
2478SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
2479 if (ssl->ctx == ctx) {
2480 return ssl->ctx;
2481 }
2482
2483 if (ctx == NULL) {
2484 ctx = ssl->initial_ctx;
2485 }
2486
2487 if (ssl->cert != NULL) {
2488 ssl_cert_free(ssl->cert);
2489 }
2490
2491 ssl->cert = ssl_cert_dup(ctx->cert);
2492 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
2493 if (ssl->ctx != NULL) {
2494 SSL_CTX_free(ssl->ctx); /* decrement reference count */
2495 }
2496 ssl->ctx = ctx;
2497
2498 ssl->sid_ctx_length = ctx->sid_ctx_length;
2499 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2500 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2501
2502 return ssl->ctx;
2503}
2504
2505int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
2506 return X509_STORE_set_default_paths(ctx->cert_store);
2507}
Adam Langley95c29f32014-06-20 12:00:00 -07002508
2509int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
Adam Langleyfcf25832014-12-18 17:42:32 -08002510 const char *CApath) {
2511 return X509_STORE_load_locations(ctx->cert_store, CAfile, CApath);
2512}
Adam Langley95c29f32014-06-20 12:00:00 -07002513
2514void SSL_set_info_callback(SSL *ssl,
Adam Langleyfcf25832014-12-18 17:42:32 -08002515 void (*cb)(const SSL *ssl, int type, int val)) {
2516 ssl->info_callback = cb;
2517}
Adam Langley95c29f32014-06-20 12:00:00 -07002518
Adam Langleyfcf25832014-12-18 17:42:32 -08002519void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/, int /*type*/,
2520 int /*val*/) {
2521 return ssl->info_callback;
2522}
Adam Langley95c29f32014-06-20 12:00:00 -07002523
Adam Langleyfcf25832014-12-18 17:42:32 -08002524int SSL_state(const SSL *ssl) { return ssl->state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002525
Adam Langleyfcf25832014-12-18 17:42:32 -08002526void SSL_set_state(SSL *ssl, int state) { ssl->state = state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002527
Adam Langleyfcf25832014-12-18 17:42:32 -08002528void SSL_set_verify_result(SSL *ssl, long arg) { ssl->verify_result = arg; }
Adam Langley95c29f32014-06-20 12:00:00 -07002529
Adam Langleyfcf25832014-12-18 17:42:32 -08002530long SSL_get_verify_result(const SSL *ssl) { return ssl->verify_result; }
Adam Langley95c29f32014-06-20 12:00:00 -07002531
Adam Langleyfcf25832014-12-18 17:42:32 -08002532int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2533 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002534 int index;
2535 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp,
2536 new_func, dup_func, free_func)) {
2537 return -1;
2538 }
2539 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002540}
Adam Langley95c29f32014-06-20 12:00:00 -07002541
Adam Langleyfcf25832014-12-18 17:42:32 -08002542int SSL_set_ex_data(SSL *s, int idx, void *arg) {
2543 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2544}
Adam Langley95c29f32014-06-20 12:00:00 -07002545
Adam Langleyfcf25832014-12-18 17:42:32 -08002546void *SSL_get_ex_data(const SSL *s, int idx) {
2547 return CRYPTO_get_ex_data(&s->ex_data, idx);
2548}
Adam Langley95c29f32014-06-20 12:00:00 -07002549
Adam Langleyfcf25832014-12-18 17:42:32 -08002550int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2551 CRYPTO_EX_dup *dup_func,
2552 CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002553 int index;
2554 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp,
2555 new_func, dup_func, free_func)) {
2556 return -1;
2557 }
2558 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002559}
Adam Langley95c29f32014-06-20 12:00:00 -07002560
Adam Langleyfcf25832014-12-18 17:42:32 -08002561int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg) {
2562 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2563}
Adam Langley95c29f32014-06-20 12:00:00 -07002564
Adam Langleyfcf25832014-12-18 17:42:32 -08002565void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx) {
2566 return CRYPTO_get_ex_data(&s->ex_data, idx);
2567}
Adam Langley95c29f32014-06-20 12:00:00 -07002568
Adam Langleyfcf25832014-12-18 17:42:32 -08002569int ssl_ok(SSL *s) { return 1; }
Adam Langley95c29f32014-06-20 12:00:00 -07002570
Adam Langleyfcf25832014-12-18 17:42:32 -08002571X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
2572 return ctx->cert_store;
2573}
Adam Langley95c29f32014-06-20 12:00:00 -07002574
Adam Langleyfcf25832014-12-18 17:42:32 -08002575void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
2576 if (ctx->cert_store != NULL) {
2577 X509_STORE_free(ctx->cert_store);
2578 }
2579 ctx->cert_store = store;
2580}
Adam Langley95c29f32014-06-20 12:00:00 -07002581
Adam Langleyfcf25832014-12-18 17:42:32 -08002582int SSL_want(const SSL *s) { return s->rwstate; }
Adam Langley95c29f32014-06-20 12:00:00 -07002583
Adam Langleyfcf25832014-12-18 17:42:32 -08002584void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
2585 RSA *(*cb)(SSL *ssl, int is_export,
2586 int keylength)) {
2587 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_RSA_CB, (void (*)(void))cb);
2588}
Adam Langley95c29f32014-06-20 12:00:00 -07002589
Adam Langleyfcf25832014-12-18 17:42:32 -08002590void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
2591 int keylength)) {
2592 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_RSA_CB, (void (*)(void))cb);
2593}
2594
2595void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
2596 DH *(*dh)(SSL *ssl, int is_export,
2597 int keylength)) {
2598 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh);
2599}
2600
2601void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*dh)(SSL *ssl, int is_export,
2602 int keylength)) {
2603 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh);
2604}
2605
2606void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,
David Benjamindd978782015-04-24 15:20:13 -04002607 EC_KEY *(*callback)(SSL *ssl, int is_export,
2608 int keylength)) {
2609 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH_CB,
2610 (void (*)(void))callback);
Adam Langleyfcf25832014-12-18 17:42:32 -08002611}
2612
2613void SSL_set_tmp_ecdh_callback(SSL *ssl,
David Benjamindd978782015-04-24 15:20:13 -04002614 EC_KEY *(*callback)(SSL *ssl, int is_export,
2615 int keylength)) {
2616 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_ECDH_CB, (void (*)(void))callback);
Adam Langleyfcf25832014-12-18 17:42:32 -08002617}
2618
2619int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
2620 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2621 OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_psk_identity_hint,
2622 SSL_R_DATA_LENGTH_TOO_LONG);
2623 return 0;
2624 }
2625
2626 if (ctx->psk_identity_hint != NULL) {
2627 OPENSSL_free(ctx->psk_identity_hint);
2628 }
2629
2630 if (identity_hint != NULL) {
2631 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2632 if (ctx->psk_identity_hint == NULL) {
2633 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002634 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002635 } else {
2636 ctx->psk_identity_hint = NULL;
2637 }
Adam Langley95c29f32014-06-20 12:00:00 -07002638
Adam Langleyfcf25832014-12-18 17:42:32 -08002639 return 1;
2640}
2641
2642int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint) {
2643 if (s == NULL) {
2644 return 0;
2645 }
2646
2647 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2648 OPENSSL_PUT_ERROR(SSL, SSL_use_psk_identity_hint,
2649 SSL_R_DATA_LENGTH_TOO_LONG);
2650 return 0;
2651 }
2652
2653 /* Clear currently configured hint, if any. */
2654 if (s->psk_identity_hint != NULL) {
2655 OPENSSL_free(s->psk_identity_hint);
2656 s->psk_identity_hint = NULL;
2657 }
2658
2659 if (identity_hint != NULL) {
2660 s->psk_identity_hint = BUF_strdup(identity_hint);
2661 if (s->psk_identity_hint == NULL) {
2662 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002663 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002664 }
Adam Langley95c29f32014-06-20 12:00:00 -07002665
Adam Langleyfcf25832014-12-18 17:42:32 -08002666 return 1;
2667}
Adam Langley95c29f32014-06-20 12:00:00 -07002668
Adam Langleyfcf25832014-12-18 17:42:32 -08002669const char *SSL_get_psk_identity_hint(const SSL *s) {
2670 if (s == NULL) {
2671 return NULL;
2672 }
2673 return s->psk_identity_hint;
2674}
Adam Langley95c29f32014-06-20 12:00:00 -07002675
Adam Langleyfcf25832014-12-18 17:42:32 -08002676const char *SSL_get_psk_identity(const SSL *s) {
2677 if (s == NULL || s->session == NULL) {
2678 return NULL;
2679 }
Adam Langley95c29f32014-06-20 12:00:00 -07002680
Adam Langleyfcf25832014-12-18 17:42:32 -08002681 return s->session->psk_identity;
2682}
Adam Langley95c29f32014-06-20 12:00:00 -07002683
Adam Langleyfcf25832014-12-18 17:42:32 -08002684void SSL_set_psk_client_callback(
2685 SSL *s, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2686 unsigned int max_identity_len, uint8_t *psk,
2687 unsigned int max_psk_len)) {
2688 s->psk_client_callback = cb;
2689}
Adam Langley95c29f32014-06-20 12:00:00 -07002690
Adam Langleyfcf25832014-12-18 17:42:32 -08002691void SSL_CTX_set_psk_client_callback(
2692 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2693 unsigned int max_identity_len,
2694 uint8_t *psk, unsigned int max_psk_len)) {
2695 ctx->psk_client_callback = cb;
2696}
Adam Langley95c29f32014-06-20 12:00:00 -07002697
Adam Langleyfcf25832014-12-18 17:42:32 -08002698void SSL_set_psk_server_callback(
2699 SSL *s, unsigned int (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
2700 unsigned int max_psk_len)) {
2701 s->psk_server_callback = cb;
2702}
Adam Langley95c29f32014-06-20 12:00:00 -07002703
Adam Langleyfcf25832014-12-18 17:42:32 -08002704void SSL_CTX_set_psk_server_callback(
2705 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *identity,
2706 uint8_t *psk, unsigned int max_psk_len)) {
2707 ctx->psk_server_callback = cb;
2708}
Adam Langley95c29f32014-06-20 12:00:00 -07002709
Adam Langleyfcf25832014-12-18 17:42:32 -08002710void SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) {
2711 ctx->min_version = version;
2712}
Adam Langley95c29f32014-06-20 12:00:00 -07002713
Adam Langleyfcf25832014-12-18 17:42:32 -08002714void SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) {
2715 ctx->max_version = version;
2716}
Adam Langley0289c732014-06-20 12:00:00 -07002717
Adam Langleyfcf25832014-12-18 17:42:32 -08002718void SSL_set_min_version(SSL *ssl, uint16_t version) {
2719 ssl->min_version = version;
2720}
Adam Langley0289c732014-06-20 12:00:00 -07002721
Adam Langleyfcf25832014-12-18 17:42:32 -08002722void SSL_set_max_version(SSL *ssl, uint16_t version) {
2723 ssl->max_version = version;
2724}
Adam Langley95c29f32014-06-20 12:00:00 -07002725
Adam Langleyfcf25832014-12-18 17:42:32 -08002726void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
2727 void (*cb)(int write_p, int version,
2728 int content_type, const void *buf,
2729 size_t len, SSL *ssl, void *arg)) {
2730 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2731}
2732void SSL_set_msg_callback(SSL *ssl,
2733 void (*cb)(int write_p, int version, int content_type,
2734 const void *buf, size_t len, SSL *ssl,
2735 void *arg)) {
2736 SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2737}
Adam Langley95c29f32014-06-20 12:00:00 -07002738
Adam Langleyfcf25832014-12-18 17:42:32 -08002739void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio) {
2740 if (ctx->keylog_bio != NULL) {
2741 BIO_free(ctx->keylog_bio);
2742 }
2743 ctx->keylog_bio = keylog_bio;
2744}
Adam Langley95c29f32014-06-20 12:00:00 -07002745
Adam Langleyfcf25832014-12-18 17:42:32 -08002746static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
2747 static const char hextable[] = "0123456789abcdef";
2748 uint8_t *out;
2749 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002750
Adam Langleyfcf25832014-12-18 17:42:32 -08002751 if (!CBB_add_space(cbb, &out, in_len * 2)) {
2752 return 0;
2753 }
Adam Langley95c29f32014-06-20 12:00:00 -07002754
Adam Langleyfcf25832014-12-18 17:42:32 -08002755 for (i = 0; i < in_len; i++) {
2756 *(out++) = (uint8_t)hextable[in[i] >> 4];
2757 *(out++) = (uint8_t)hextable[in[i] & 0xf];
2758 }
Adam Langley95c29f32014-06-20 12:00:00 -07002759
Adam Langleyfcf25832014-12-18 17:42:32 -08002760 return 1;
2761}
David Benjamin859ec3c2014-09-02 16:29:36 -04002762
2763int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -08002764 const uint8_t *encrypted_premaster,
2765 size_t encrypted_premaster_len,
2766 const uint8_t *premaster,
2767 size_t premaster_len) {
2768 BIO *bio = ctx->keylog_bio;
2769 CBB cbb;
2770 uint8_t *out;
2771 size_t out_len;
2772 int ret;
David Benjamin859ec3c2014-09-02 16:29:36 -04002773
Adam Langleyfcf25832014-12-18 17:42:32 -08002774 if (bio == NULL) {
2775 return 1;
2776 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002777
Adam Langleyfcf25832014-12-18 17:42:32 -08002778 if (encrypted_premaster_len < 8) {
2779 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_rsa_client_key_exchange,
2780 ERR_R_INTERNAL_ERROR);
2781 return 0;
2782 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002783
Adam Langleyfcf25832014-12-18 17:42:32 -08002784 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len * 2 + 1)) {
2785 return 0;
2786 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002787
Adam Langleyfcf25832014-12-18 17:42:32 -08002788 if (!CBB_add_bytes(&cbb, (const uint8_t *)"RSA ", 4) ||
2789 /* Only the first 8 bytes of the encrypted premaster secret are
2790 * logged. */
2791 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
2792 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2793 !cbb_add_hex(&cbb, premaster, premaster_len) ||
2794 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2795 !CBB_finish(&cbb, &out, &out_len)) {
2796 CBB_cleanup(&cbb);
2797 return 0;
2798 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002799
Adam Langleyfcf25832014-12-18 17:42:32 -08002800 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2801 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2802 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
David Benjamin859ec3c2014-09-02 16:29:36 -04002803
Adam Langleyfcf25832014-12-18 17:42:32 -08002804 OPENSSL_free(out);
2805 return ret;
Adam Langley95f22882014-06-20 12:00:00 -07002806}
2807
Adam Langleyfcf25832014-12-18 17:42:32 -08002808int ssl_ctx_log_master_secret(SSL_CTX *ctx, const uint8_t *client_random,
2809 size_t client_random_len, const uint8_t *master,
2810 size_t master_len) {
2811 BIO *bio = ctx->keylog_bio;
2812 CBB cbb;
2813 uint8_t *out;
2814 size_t out_len;
2815 int ret;
Adam Langleyadb739e2014-06-20 12:00:00 -07002816
Adam Langleyfcf25832014-12-18 17:42:32 -08002817 if (bio == NULL) {
2818 return 1;
2819 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002820
Adam Langleyfcf25832014-12-18 17:42:32 -08002821 if (client_random_len != 32) {
2822 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_master_secret, ERR_R_INTERNAL_ERROR);
2823 return 0;
2824 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002825
Adam Langleyfcf25832014-12-18 17:42:32 -08002826 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len * 2 + 1)) {
2827 return 0;
2828 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002829
Adam Langleyfcf25832014-12-18 17:42:32 -08002830 if (!CBB_add_bytes(&cbb, (const uint8_t *)"CLIENT_RANDOM ", 14) ||
2831 !cbb_add_hex(&cbb, client_random, 32) ||
2832 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2833 !cbb_add_hex(&cbb, master, master_len) ||
2834 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2835 !CBB_finish(&cbb, &out, &out_len)) {
2836 CBB_cleanup(&cbb);
2837 return 0;
2838 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002839
Adam Langleyfcf25832014-12-18 17:42:32 -08002840 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2841 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2842 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
David Benjamine99e9122014-12-11 01:46:01 -05002843
Adam Langleyfcf25832014-12-18 17:42:32 -08002844 OPENSSL_free(out);
2845 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -07002846}
2847
David Benjamined7c4752015-02-16 19:16:46 -05002848int SSL_in_false_start(const SSL *s) {
2849 return s->s3->tmp.in_false_start;
2850}
2851
Adam Langleyfcf25832014-12-18 17:42:32 -08002852int SSL_cutthrough_complete(const SSL *s) {
David Benjamined7c4752015-02-16 19:16:46 -05002853 return SSL_in_false_start(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002854}
Adam Langley95c29f32014-06-20 12:00:00 -07002855
Adam Langleyfcf25832014-12-18 17:42:32 -08002856void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
2857 size_t *ssl_session_size) {
2858 *ssl_size = sizeof(SSL);
2859 *ssl_ctx_size = sizeof(SSL_CTX);
2860 *ssl_session_size = sizeof(SSL_SESSION);
2861}
Feng Lu41aa3252014-11-21 22:47:56 -08002862
David Benjamined7c4752015-02-16 19:16:46 -05002863int ssl3_can_false_start(const SSL *s) {
David Benjamin195dc782015-02-19 13:27:05 -05002864 const SSL_CIPHER *const cipher = SSL_get_current_cipher(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002865
Adam Langleye631d962015-04-01 13:11:01 -07002866 /* False Start only for TLS 1.2 with an ECDHE+AEAD cipher and ALPN or NPN. */
David Benjamin195dc782015-02-19 13:27:05 -05002867 return !SSL_IS_DTLS(s) &&
2868 SSL_version(s) >= TLS1_2_VERSION &&
2869 (s->s3->alpn_selected || s->s3->next_proto_neg_seen) &&
2870 cipher != NULL &&
Adam Langleye631d962015-04-01 13:11:01 -07002871 cipher->algorithm_mkey == SSL_kECDHE &&
David Benjamin195dc782015-02-19 13:27:05 -05002872 (cipher->algorithm_enc == SSL_AES128GCM ||
2873 cipher->algorithm_enc == SSL_AES256GCM ||
2874 cipher->algorithm_enc == SSL_CHACHA20POLY1305);
Adam Langleyfcf25832014-12-18 17:42:32 -08002875}
2876
2877const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version) {
2878 switch (version) {
2879 case SSL3_VERSION:
2880 return &SSLv3_enc_data;
2881
2882 case TLS1_VERSION:
2883 return &TLSv1_enc_data;
2884
David Benjamin9e13e1a2015-03-05 01:56:32 -05002885 case DTLS1_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002886 case TLS1_1_VERSION:
2887 return &TLSv1_1_enc_data;
2888
David Benjamin9e13e1a2015-03-05 01:56:32 -05002889 case DTLS1_2_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002890 case TLS1_2_VERSION:
2891 return &TLSv1_2_enc_data;
2892
Adam Langleyfcf25832014-12-18 17:42:32 -08002893 default:
2894 return NULL;
2895 }
2896}
2897
2898uint16_t ssl3_get_max_server_version(const SSL *s) {
2899 uint16_t max_version;
2900
2901 if (SSL_IS_DTLS(s)) {
2902 max_version = (s->max_version != 0) ? s->max_version : DTLS1_2_VERSION;
2903 if (!(s->options & SSL_OP_NO_DTLSv1_2) && DTLS1_2_VERSION >= max_version) {
2904 return DTLS1_2_VERSION;
2905 }
2906 if (!(s->options & SSL_OP_NO_DTLSv1) && DTLS1_VERSION >= max_version) {
2907 return DTLS1_VERSION;
2908 }
2909 return 0;
2910 }
2911
2912 max_version = (s->max_version != 0) ? s->max_version : TLS1_2_VERSION;
2913 if (!(s->options & SSL_OP_NO_TLSv1_2) && TLS1_2_VERSION <= max_version) {
2914 return TLS1_2_VERSION;
2915 }
2916 if (!(s->options & SSL_OP_NO_TLSv1_1) && TLS1_1_VERSION <= max_version) {
2917 return TLS1_1_VERSION;
2918 }
2919 if (!(s->options & SSL_OP_NO_TLSv1) && TLS1_VERSION <= max_version) {
2920 return TLS1_VERSION;
2921 }
2922 if (!(s->options & SSL_OP_NO_SSLv3) && SSL3_VERSION <= max_version) {
2923 return SSL3_VERSION;
2924 }
2925 return 0;
2926}
2927
2928uint16_t ssl3_get_mutual_version(SSL *s, uint16_t client_version) {
2929 uint16_t version = 0;
2930
2931 if (SSL_IS_DTLS(s)) {
2932 /* Clamp client_version to max_version. */
2933 if (s->max_version != 0 && client_version < s->max_version) {
2934 client_version = s->max_version;
2935 }
2936
2937 if (client_version <= DTLS1_2_VERSION && !(s->options & SSL_OP_NO_DTLSv1_2)) {
2938 version = DTLS1_2_VERSION;
2939 } else if (client_version <= DTLS1_VERSION &&
David Benjaminb18f0242015-03-10 18:30:08 -04002940 !(s->options & SSL_OP_NO_DTLSv1)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002941 version = DTLS1_VERSION;
2942 }
2943
2944 /* Check against min_version. */
2945 if (version != 0 && s->min_version != 0 && version > s->min_version) {
2946 return 0;
2947 }
2948 return version;
2949 } else {
2950 /* Clamp client_version to max_version. */
2951 if (s->max_version != 0 && client_version > s->max_version) {
2952 client_version = s->max_version;
2953 }
2954
2955 if (client_version >= TLS1_2_VERSION && !(s->options & SSL_OP_NO_TLSv1_2)) {
2956 version = TLS1_2_VERSION;
2957 } else if (client_version >= TLS1_1_VERSION &&
2958 !(s->options & SSL_OP_NO_TLSv1_1)) {
2959 version = TLS1_1_VERSION;
2960 } else if (client_version >= TLS1_VERSION && !(s->options & SSL_OP_NO_TLSv1)) {
2961 version = TLS1_VERSION;
2962 } else if (client_version >= SSL3_VERSION && !(s->options & SSL_OP_NO_SSLv3)) {
2963 version = SSL3_VERSION;
2964 }
2965
2966 /* Check against min_version. */
2967 if (version != 0 && s->min_version != 0 && version < s->min_version) {
2968 return 0;
2969 }
2970 return version;
2971 }
2972}
2973
2974uint16_t ssl3_get_max_client_version(SSL *s) {
2975 unsigned long options = s->options;
2976 uint16_t version = 0;
2977
2978 /* OpenSSL's API for controlling versions entails blacklisting individual
2979 * protocols. This has two problems. First, on the client, the protocol can
2980 * only express a contiguous range of versions. Second, a library consumer
2981 * trying to set a maximum version cannot disable protocol versions that get
2982 * added in a future version of the library.
2983 *
2984 * To account for both of these, OpenSSL interprets the client-side bitmask
2985 * as a min/max range by picking the lowest contiguous non-empty range of
2986 * enabled protocols. Note that this means it is impossible to set a maximum
2987 * version of TLS 1.2 in a future-proof way.
2988 *
2989 * By this scheme, the maximum version is the lowest version V such that V is
2990 * enabled and V+1 is disabled or unimplemented. */
2991 if (SSL_IS_DTLS(s)) {
2992 if (!(options & SSL_OP_NO_DTLSv1_2)) {
2993 version = DTLS1_2_VERSION;
2994 }
2995 if (!(options & SSL_OP_NO_DTLSv1) && (options & SSL_OP_NO_DTLSv1_2)) {
2996 version = DTLS1_VERSION;
2997 }
2998 if (s->max_version != 0 && version < s->max_version) {
2999 version = s->max_version;
3000 }
3001 } else {
3002 if (!(options & SSL_OP_NO_TLSv1_2)) {
3003 version = TLS1_2_VERSION;
3004 }
3005 if (!(options & SSL_OP_NO_TLSv1_1) && (options & SSL_OP_NO_TLSv1_2)) {
3006 version = TLS1_1_VERSION;
3007 }
3008 if (!(options & SSL_OP_NO_TLSv1) && (options & SSL_OP_NO_TLSv1_1)) {
3009 version = TLS1_VERSION;
3010 }
3011 if (!(options & SSL_OP_NO_SSLv3) && (options & SSL_OP_NO_TLSv1)) {
3012 version = SSL3_VERSION;
3013 }
3014 if (s->max_version != 0 && version > s->max_version) {
3015 version = s->max_version;
3016 }
3017 }
3018
3019 return version;
3020}
3021
3022int ssl3_is_version_enabled(SSL *s, uint16_t version) {
3023 if (SSL_IS_DTLS(s)) {
3024 if (s->max_version != 0 && version < s->max_version) {
3025 return 0;
3026 }
3027 if (s->min_version != 0 && version > s->min_version) {
3028 return 0;
3029 }
3030
3031 switch (version) {
3032 case DTLS1_VERSION:
3033 return !(s->options & SSL_OP_NO_DTLSv1);
3034
3035 case DTLS1_2_VERSION:
3036 return !(s->options & SSL_OP_NO_DTLSv1_2);
3037
3038 default:
3039 return 0;
3040 }
3041 } else {
3042 if (s->max_version != 0 && version > s->max_version) {
3043 return 0;
3044 }
3045 if (s->min_version != 0 && version < s->min_version) {
3046 return 0;
3047 }
3048
3049 switch (version) {
3050 case SSL3_VERSION:
3051 return !(s->options & SSL_OP_NO_SSLv3);
3052
3053 case TLS1_VERSION:
3054 return !(s->options & SSL_OP_NO_TLSv1);
3055
3056 case TLS1_1_VERSION:
3057 return !(s->options & SSL_OP_NO_TLSv1_1);
3058
3059 case TLS1_2_VERSION:
3060 return !(s->options & SSL_OP_NO_TLSv1_2);
3061
3062 default:
3063 return 0;
3064 }
3065 }
3066}
3067
David Benjaminea72bd02014-12-21 21:27:41 -05003068uint16_t ssl3_version_from_wire(SSL *s, uint16_t wire_version) {
3069 if (!SSL_IS_DTLS(s)) {
3070 return wire_version;
3071 }
3072
3073 uint16_t tls_version = ~wire_version;
3074 uint16_t version = tls_version + 0x0201;
3075 /* If either component overflowed, clamp it so comparisons still work. */
3076 if ((version >> 8) < (tls_version >> 8)) {
3077 version = 0xff00 | (version & 0xff);
3078 }
3079 if ((version & 0xff) < (tls_version & 0xff)) {
3080 version = (version & 0xff00) | 0xff;
3081 }
3082 /* DTLS 1.0 maps to TLS 1.1, not TLS 1.0. */
3083 if (version == TLS1_VERSION) {
3084 version = TLS1_1_VERSION;
3085 }
3086 return version;
3087}
3088
Adam Langleyfcf25832014-12-18 17:42:32 -08003089int SSL_cache_hit(SSL *s) { return s->hit; }
3090
3091int SSL_is_server(SSL *s) { return s->server; }
3092
Adam Langley524e7172015-02-20 16:04:00 -08003093void SSL_CTX_set_dos_protection_cb(
3094 SSL_CTX *ctx, int (*cb)(const struct ssl_early_callback_ctx *)) {
3095 ctx->dos_protection_cb = cb;
3096}
3097
Adam Langleyfcf25832014-12-18 17:42:32 -08003098void SSL_enable_fastradio_padding(SSL *s, char on_off) {
3099 s->fastradio_padding = on_off;
3100}
Adam Langley44972942015-02-20 13:43:23 -08003101
David Benjaminb16346b2015-04-08 19:16:58 -04003102void SSL_set_reject_peer_renegotiations(SSL *s, int reject) {
3103 s->reject_peer_renegotiations = !!reject;
3104}
3105
Adam Langley44972942015-02-20 13:43:23 -08003106const SSL_CIPHER *SSL_get_cipher_by_value(uint16_t value) {
3107 return ssl3_get_cipher_by_value(value);
3108}
Adam Langley3f92d212015-02-20 15:32:52 -08003109
3110int SSL_get_rc4_state(const SSL *ssl, const RC4_KEY **read_key,
3111 const RC4_KEY **write_key) {
3112 if (ssl->aead_read_ctx == NULL || ssl->aead_write_ctx == NULL) {
3113 return 0;
3114 }
3115
3116 return EVP_AEAD_CTX_get_rc4_state(&ssl->aead_read_ctx->ctx, read_key) &&
3117 EVP_AEAD_CTX_get_rc4_state(&ssl->aead_write_ctx->ctx, write_key);
3118}