blob: 961dec0fb51d133df14a3a61482474378f32c6b0 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
108 *
109 */
110/* ====================================================================
111 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
112 * ECC cipher suite support in OpenSSL originally developed by
113 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
114 */
115/* ====================================================================
116 * Copyright 2005 Nokia. All rights reserved.
117 *
118 * The portions of the attached software ("Contribution") is developed by
119 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
120 * license.
121 *
122 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
123 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
124 * support (see RFC 4279) to OpenSSL.
125 *
126 * No patent licenses or other rights except those expressly stated in
127 * the OpenSSL open source license shall be deemed granted or received
128 * expressly, by implication, estoppel, or otherwise.
129 *
130 * No assurances are provided by Nokia that the Contribution does not
131 * infringe the patent or other intellectual property rights of any third
132 * party or that the license provides you with all the necessary rights
133 * to make use of the Contribution.
134 *
135 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
136 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
137 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
138 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
139 * OTHERWISE. */
140
Adam Langley95c29f32014-06-20 12:00:00 -0700141#include <assert.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400142#include <stdio.h>
143#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700144
David Benjamin39482a12014-07-20 13:30:15 -0400145#include <openssl/bytestring.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700146#include <openssl/dh.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400147#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700148#include <openssl/lhash.h>
149#include <openssl/mem.h>
150#include <openssl/obj.h>
151#include <openssl/rand.h>
152#include <openssl/x509v3.h>
153
David Benjamin2ee94aa2015-04-07 22:38:30 -0400154#include "internal.h"
David Benjamin546f1a52015-04-15 16:46:09 -0400155#include "../crypto/internal.h"
156
Adam Langley95c29f32014-06-20 12:00:00 -0700157
Adam Langleyfcf25832014-12-18 17:42:32 -0800158/* Some error codes are special. Ensure the make_errors.go script never
159 * regresses this. */
160OPENSSL_COMPILE_ASSERT(SSL_R_TLSV1_ALERT_NO_RENEGOTIATION ==
161 SSL_AD_NO_RENEGOTIATION + SSL_AD_REASON_OFFSET,
162 ssl_alert_reason_code_mismatch);
David Benjamine1136082014-09-20 12:28:58 -0400163
David Benjamin9f33fc62015-04-15 17:29:53 -0400164static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl = CRYPTO_EX_DATA_CLASS_INIT;
165static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl_ctx = CRYPTO_EX_DATA_CLASS_INIT;
166
Adam Langleyfcf25832014-12-18 17:42:32 -0800167int SSL_clear(SSL *s) {
168 if (s->method == NULL) {
169 OPENSSL_PUT_ERROR(SSL, SSL_clear, SSL_R_NO_METHOD_SPECIFIED);
170 return 0;
171 }
Adam Langley95c29f32014-06-20 12:00:00 -0700172
Adam Langleyfcf25832014-12-18 17:42:32 -0800173 if (ssl_clear_bad_session(s)) {
174 SSL_SESSION_free(s->session);
175 s->session = NULL;
176 }
Adam Langley95c29f32014-06-20 12:00:00 -0700177
Adam Langleyfcf25832014-12-18 17:42:32 -0800178 s->hit = 0;
179 s->shutdown = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700180
Adam Langleyfcf25832014-12-18 17:42:32 -0800181 if (s->renegotiate) {
182 OPENSSL_PUT_ERROR(SSL, SSL_clear, ERR_R_INTERNAL_ERROR);
183 return 0;
184 }
Adam Langley95c29f32014-06-20 12:00:00 -0700185
Adam Langleyfcf25832014-12-18 17:42:32 -0800186 /* SSL_clear may be called before or after the |s| is initialized in either
187 * accept or connect state. In the latter case, SSL_clear should preserve the
188 * half and reset |s->state| accordingly. */
189 if (s->handshake_func != NULL) {
190 if (s->server) {
191 SSL_set_accept_state(s);
192 } else {
193 SSL_set_connect_state(s);
194 }
195 } else {
196 assert(s->state == 0);
197 }
Adam Langley95c29f32014-06-20 12:00:00 -0700198
David Benjamin62fd1622015-01-11 13:30:01 -0500199 /* TODO(davidben): Some state on |s| is reset both in |SSL_new| and
200 * |SSL_clear| because it is per-connection state rather than configuration
201 * state. Per-connection state should be on |s->s3| and |s->d1| so it is
202 * naturally reset at the right points between |SSL_new|, |SSL_clear|, and
203 * |ssl3_new|. */
204
Adam Langleyfcf25832014-12-18 17:42:32 -0800205 s->rwstate = SSL_NOTHING;
206 s->rstate = SSL_ST_READ_HEADER;
Adam Langley95c29f32014-06-20 12:00:00 -0700207
David Benjamin2755a3e2015-04-22 16:17:58 -0400208 BUF_MEM_free(s->init_buf);
209 s->init_buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700210
David Benjamin62fd1622015-01-11 13:30:01 -0500211 s->packet = NULL;
212 s->packet_length = 0;
213
Adam Langleyfcf25832014-12-18 17:42:32 -0800214 ssl_clear_cipher_ctx(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700215
David Benjamin2755a3e2015-04-22 16:17:58 -0400216 OPENSSL_free(s->next_proto_negotiated);
217 s->next_proto_negotiated = NULL;
218 s->next_proto_negotiated_len = 0;
David Benjamin62fd1622015-01-11 13:30:01 -0500219
220 /* The s->d1->mtu is simultaneously configuration (preserved across
221 * clear) and connection-specific state (gets reset).
222 *
223 * TODO(davidben): Avoid this. */
224 unsigned mtu = 0;
225 if (s->d1 != NULL) {
226 mtu = s->d1->mtu;
227 }
228
229 s->method->ssl_free(s);
230 if (!s->method->ssl_new(s)) {
231 return 0;
232 }
233 s->enc_method = ssl3_get_enc_method(s->version);
234 assert(s->enc_method != NULL);
235
236 if (SSL_IS_DTLS(s) && (SSL_get_options(s) & SSL_OP_NO_QUERY_MTU)) {
237 s->d1->mtu = mtu;
238 }
239
Adam Langleyfcf25832014-12-18 17:42:32 -0800240 s->client_version = s->version;
Adam Langley95c29f32014-06-20 12:00:00 -0700241
Adam Langleyfcf25832014-12-18 17:42:32 -0800242 return 1;
243}
Adam Langley95c29f32014-06-20 12:00:00 -0700244
Adam Langleyfcf25832014-12-18 17:42:32 -0800245SSL *SSL_new(SSL_CTX *ctx) {
246 SSL *s;
Adam Langley95c29f32014-06-20 12:00:00 -0700247
Adam Langleyfcf25832014-12-18 17:42:32 -0800248 if (ctx == NULL) {
249 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_NULL_SSL_CTX);
250 return NULL;
251 }
252 if (ctx->method == NULL) {
253 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
254 return NULL;
255 }
Adam Langley95c29f32014-06-20 12:00:00 -0700256
Adam Langleyfcf25832014-12-18 17:42:32 -0800257 s = (SSL *)OPENSSL_malloc(sizeof(SSL));
258 if (s == NULL) {
259 goto err;
260 }
261 memset(s, 0, sizeof(SSL));
Adam Langley95c29f32014-06-20 12:00:00 -0700262
Adam Langleyfcf25832014-12-18 17:42:32 -0800263 s->min_version = ctx->min_version;
264 s->max_version = ctx->max_version;
David Benjamin1eb367c2014-12-12 18:17:51 -0500265
Adam Langleyfcf25832014-12-18 17:42:32 -0800266 s->options = ctx->options;
267 s->mode = ctx->mode;
268 s->max_cert_list = ctx->max_cert_list;
Adam Langley95c29f32014-06-20 12:00:00 -0700269
David Benjamina5a3eeb2015-03-18 20:26:30 -0400270 s->cert = ssl_cert_dup(ctx->cert);
271 if (s->cert == NULL) {
272 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -0800273 }
Adam Langley95c29f32014-06-20 12:00:00 -0700274
Adam Langleyfcf25832014-12-18 17:42:32 -0800275 s->read_ahead = ctx->read_ahead;
276 s->msg_callback = ctx->msg_callback;
277 s->msg_callback_arg = ctx->msg_callback_arg;
278 s->verify_mode = ctx->verify_mode;
279 s->sid_ctx_length = ctx->sid_ctx_length;
280 assert(s->sid_ctx_length <= sizeof s->sid_ctx);
281 memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));
282 s->verify_callback = ctx->default_verify_callback;
283 s->generate_session_id = ctx->generate_session_id;
Adam Langley95c29f32014-06-20 12:00:00 -0700284
Adam Langleyfcf25832014-12-18 17:42:32 -0800285 s->param = X509_VERIFY_PARAM_new();
286 if (!s->param) {
287 goto err;
288 }
289 X509_VERIFY_PARAM_inherit(s->param, ctx->param);
290 s->quiet_shutdown = ctx->quiet_shutdown;
291 s->max_send_fragment = ctx->max_send_fragment;
Adam Langley95c29f32014-06-20 12:00:00 -0700292
Adam Langleyfcf25832014-12-18 17:42:32 -0800293 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
294 s->ctx = ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -0800295 s->tlsext_ticket_expected = 0;
296 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
297 s->initial_ctx = ctx;
298 if (ctx->tlsext_ecpointformatlist) {
299 s->tlsext_ecpointformatlist = BUF_memdup(
300 ctx->tlsext_ecpointformatlist, ctx->tlsext_ecpointformatlist_length);
301 if (!s->tlsext_ecpointformatlist) {
302 goto err;
303 }
304 s->tlsext_ecpointformatlist_length = ctx->tlsext_ecpointformatlist_length;
305 }
Adam Langley95c29f32014-06-20 12:00:00 -0700306
Adam Langleyfcf25832014-12-18 17:42:32 -0800307 if (ctx->tlsext_ellipticcurvelist) {
308 s->tlsext_ellipticcurvelist =
309 BUF_memdup(ctx->tlsext_ellipticcurvelist,
310 ctx->tlsext_ellipticcurvelist_length * 2);
311 if (!s->tlsext_ellipticcurvelist) {
312 goto err;
313 }
314 s->tlsext_ellipticcurvelist_length = ctx->tlsext_ellipticcurvelist_length;
315 }
316 s->next_proto_negotiated = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700317
Adam Langleyfcf25832014-12-18 17:42:32 -0800318 if (s->ctx->alpn_client_proto_list) {
319 s->alpn_client_proto_list = BUF_memdup(s->ctx->alpn_client_proto_list,
320 s->ctx->alpn_client_proto_list_len);
321 if (s->alpn_client_proto_list == NULL) {
322 goto err;
323 }
324 s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;
325 }
Adam Langley95c29f32014-06-20 12:00:00 -0700326
Adam Langleyfcf25832014-12-18 17:42:32 -0800327 s->verify_result = X509_V_OK;
328 s->method = ctx->method;
Adam Langley95c29f32014-06-20 12:00:00 -0700329
Adam Langleyfcf25832014-12-18 17:42:32 -0800330 if (!s->method->ssl_new(s)) {
331 goto err;
332 }
333 s->enc_method = ssl3_get_enc_method(s->version);
334 assert(s->enc_method != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700335
David Benjamin62fd1622015-01-11 13:30:01 -0500336 s->rwstate = SSL_NOTHING;
337 s->rstate = SSL_ST_READ_HEADER;
Adam Langley95c29f32014-06-20 12:00:00 -0700338
David Benjamin9f33fc62015-04-15 17:29:53 -0400339 CRYPTO_new_ex_data(&g_ex_data_class_ssl, s, &s->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700340
Adam Langleyfcf25832014-12-18 17:42:32 -0800341 s->psk_identity_hint = NULL;
342 if (ctx->psk_identity_hint) {
343 s->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
344 if (s->psk_identity_hint == NULL) {
345 goto err;
346 }
347 }
348 s->psk_client_callback = ctx->psk_client_callback;
349 s->psk_server_callback = ctx->psk_server_callback;
Adam Langley95c29f32014-06-20 12:00:00 -0700350
David Benjamin02ddbfd2015-01-11 13:09:11 -0500351 s->tlsext_channel_id_enabled = ctx->tlsext_channel_id_enabled;
352 if (ctx->tlsext_channel_id_private) {
353 s->tlsext_channel_id_private = EVP_PKEY_dup(ctx->tlsext_channel_id_private);
354 }
355
Adam Langleyfcf25832014-12-18 17:42:32 -0800356 s->signed_cert_timestamps_enabled = s->ctx->signed_cert_timestamps_enabled;
357 s->ocsp_stapling_enabled = s->ctx->ocsp_stapling_enabled;
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200358
Adam Langleyfcf25832014-12-18 17:42:32 -0800359 return s;
360
Adam Langley95c29f32014-06-20 12:00:00 -0700361err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400362 SSL_free(s);
Adam Langleyfcf25832014-12-18 17:42:32 -0800363 OPENSSL_PUT_ERROR(SSL, SSL_new, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700364
Adam Langleyfcf25832014-12-18 17:42:32 -0800365 return NULL;
366}
Adam Langley95c29f32014-06-20 12:00:00 -0700367
Adam Langleyfcf25832014-12-18 17:42:32 -0800368int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
369 unsigned int sid_ctx_len) {
370 if (sid_ctx_len > sizeof ctx->sid_ctx) {
371 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_session_id_context,
372 SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
373 return 0;
374 }
375 ctx->sid_ctx_length = sid_ctx_len;
376 memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700377
Adam Langleyfcf25832014-12-18 17:42:32 -0800378 return 1;
379}
Adam Langley95c29f32014-06-20 12:00:00 -0700380
Adam Langleyfcf25832014-12-18 17:42:32 -0800381int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
382 unsigned int sid_ctx_len) {
383 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
384 OPENSSL_PUT_ERROR(SSL, SSL_set_session_id_context,
385 SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
386 return 0;
387 }
388 ssl->sid_ctx_length = sid_ctx_len;
389 memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700390
Adam Langleyfcf25832014-12-18 17:42:32 -0800391 return 1;
392}
Adam Langley95c29f32014-06-20 12:00:00 -0700393
Adam Langleyfcf25832014-12-18 17:42:32 -0800394int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb) {
395 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
396 ctx->generate_session_id = cb;
397 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
398 return 1;
399}
Adam Langley95c29f32014-06-20 12:00:00 -0700400
Adam Langleyfcf25832014-12-18 17:42:32 -0800401int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800402 ssl->generate_session_id = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -0800403 return 1;
404}
Adam Langley95c29f32014-06-20 12:00:00 -0700405
Adam Langleyfcf25832014-12-18 17:42:32 -0800406int SSL_has_matching_session_id(const SSL *ssl, const uint8_t *id,
407 unsigned int id_len) {
408 /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how we
409 * can "construct" a session to give us the desired check - ie. to find if
410 * there's a session in the hash table that would conflict with any new
411 * session built out of this id/id_len and the ssl_version in use by this
412 * SSL. */
413 SSL_SESSION r, *p;
Adam Langley95c29f32014-06-20 12:00:00 -0700414
Adam Langleyfcf25832014-12-18 17:42:32 -0800415 if (id_len > sizeof r.session_id) {
416 return 0;
417 }
Adam Langley95c29f32014-06-20 12:00:00 -0700418
Adam Langleyfcf25832014-12-18 17:42:32 -0800419 r.ssl_version = ssl->version;
420 r.session_id_length = id_len;
421 memcpy(r.session_id, id, id_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700422
Adam Langleyfcf25832014-12-18 17:42:32 -0800423 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
424 p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r);
425 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
426 return p != NULL;
427}
Adam Langley95c29f32014-06-20 12:00:00 -0700428
Adam Langleyfcf25832014-12-18 17:42:32 -0800429int SSL_CTX_set_purpose(SSL_CTX *s, int purpose) {
430 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
431}
432
433int SSL_set_purpose(SSL *s, int purpose) {
434 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
435}
436
437int SSL_CTX_set_trust(SSL_CTX *s, int trust) {
438 return X509_VERIFY_PARAM_set_trust(s->param, trust);
439}
440
441int SSL_set_trust(SSL *s, int trust) {
442 return X509_VERIFY_PARAM_set_trust(s->param, trust);
443}
444
445int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm) {
446 return X509_VERIFY_PARAM_set1(ctx->param, vpm);
447}
448
449int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm) {
450 return X509_VERIFY_PARAM_set1(ssl->param, vpm);
451}
Adam Langley95c29f32014-06-20 12:00:00 -0700452
Adam Langley858a88d2014-06-20 12:00:00 -0700453void ssl_cipher_preference_list_free(
Adam Langleyfcf25832014-12-18 17:42:32 -0800454 struct ssl_cipher_preference_list_st *cipher_list) {
David Benjamin5d1ec732015-04-22 13:38:00 -0400455 if (cipher_list == NULL) {
456 return;
457 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800458 sk_SSL_CIPHER_free(cipher_list->ciphers);
459 OPENSSL_free(cipher_list->in_group_flags);
460 OPENSSL_free(cipher_list);
461}
Adam Langley858a88d2014-06-20 12:00:00 -0700462
Adam Langleyfcf25832014-12-18 17:42:32 -0800463struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_dup(
464 struct ssl_cipher_preference_list_st *cipher_list) {
465 struct ssl_cipher_preference_list_st *ret = NULL;
466 size_t n = sk_SSL_CIPHER_num(cipher_list->ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700467
Adam Langleyfcf25832014-12-18 17:42:32 -0800468 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
469 if (!ret) {
470 goto err;
471 }
472
473 ret->ciphers = NULL;
474 ret->in_group_flags = NULL;
475 ret->ciphers = sk_SSL_CIPHER_dup(cipher_list->ciphers);
476 if (!ret->ciphers) {
477 goto err;
478 }
479 ret->in_group_flags = BUF_memdup(cipher_list->in_group_flags, n);
480 if (!ret->in_group_flags) {
481 goto err;
482 }
483
484 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -0700485
486err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400487 ssl_cipher_preference_list_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -0800488 return NULL;
489}
Adam Langley858a88d2014-06-20 12:00:00 -0700490
Adam Langleyfcf25832014-12-18 17:42:32 -0800491struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_from_ciphers(
492 STACK_OF(SSL_CIPHER) * ciphers) {
493 struct ssl_cipher_preference_list_st *ret = NULL;
494 size_t n = sk_SSL_CIPHER_num(ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700495
Adam Langleyfcf25832014-12-18 17:42:32 -0800496 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
497 if (!ret) {
498 goto err;
499 }
500 ret->ciphers = NULL;
501 ret->in_group_flags = NULL;
502 ret->ciphers = sk_SSL_CIPHER_dup(ciphers);
503 if (!ret->ciphers) {
504 goto err;
505 }
506 ret->in_group_flags = OPENSSL_malloc(n);
507 if (!ret->in_group_flags) {
508 goto err;
509 }
510 memset(ret->in_group_flags, 0, n);
511 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -0700512
513err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400514 ssl_cipher_preference_list_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -0800515 return NULL;
516}
Adam Langley858a88d2014-06-20 12:00:00 -0700517
Adam Langleyfcf25832014-12-18 17:42:32 -0800518X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) { return ctx->param; }
Adam Langley95c29f32014-06-20 12:00:00 -0700519
Adam Langleyfcf25832014-12-18 17:42:32 -0800520X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) { return ssl->param; }
Adam Langley95c29f32014-06-20 12:00:00 -0700521
Adam Langleyfcf25832014-12-18 17:42:32 -0800522void SSL_certs_clear(SSL *s) { ssl_cert_clear_certs(s->cert); }
Adam Langley95c29f32014-06-20 12:00:00 -0700523
Adam Langleyfcf25832014-12-18 17:42:32 -0800524void SSL_free(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800525 if (s == NULL) {
526 return;
527 }
Adam Langley95c29f32014-06-20 12:00:00 -0700528
David Benjamin2755a3e2015-04-22 16:17:58 -0400529 X509_VERIFY_PARAM_free(s->param);
Adam Langley95c29f32014-06-20 12:00:00 -0700530
David Benjamin9f33fc62015-04-15 17:29:53 -0400531 CRYPTO_free_ex_data(&g_ex_data_class_ssl, s, &s->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700532
Adam Langleyfcf25832014-12-18 17:42:32 -0800533 if (s->bbio != NULL) {
534 /* If the buffering BIO is in place, pop it off */
535 if (s->bbio == s->wbio) {
536 s->wbio = BIO_pop(s->wbio);
537 }
538 BIO_free(s->bbio);
539 s->bbio = NULL;
540 }
Adam Langley95c29f32014-06-20 12:00:00 -0700541
David Benjamin2755a3e2015-04-22 16:17:58 -0400542 int free_wbio = s->wbio != s->rbio;
543 BIO_free_all(s->rbio);
544 if (free_wbio) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800545 BIO_free_all(s->wbio);
546 }
Adam Langley95c29f32014-06-20 12:00:00 -0700547
David Benjamin2755a3e2015-04-22 16:17:58 -0400548 BUF_MEM_free(s->init_buf);
Adam Langley95c29f32014-06-20 12:00:00 -0700549
Adam Langleyfcf25832014-12-18 17:42:32 -0800550 /* add extra stuff */
David Benjamin2755a3e2015-04-22 16:17:58 -0400551 ssl_cipher_preference_list_free(s->cipher_list);
552 sk_SSL_CIPHER_free(s->cipher_list_by_id);
Adam Langley95c29f32014-06-20 12:00:00 -0700553
David Benjamin2755a3e2015-04-22 16:17:58 -0400554 ssl_clear_bad_session(s);
555 SSL_SESSION_free(s->session);
Adam Langley95c29f32014-06-20 12:00:00 -0700556
Adam Langleyfcf25832014-12-18 17:42:32 -0800557 ssl_clear_cipher_ctx(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700558
David Benjamin2755a3e2015-04-22 16:17:58 -0400559 ssl_cert_free(s->cert);
Adam Langley0289c732014-06-20 12:00:00 -0700560
David Benjamin2755a3e2015-04-22 16:17:58 -0400561 OPENSSL_free(s->tlsext_hostname);
562 SSL_CTX_free(s->initial_ctx);
563 OPENSSL_free(s->tlsext_ecpointformatlist);
564 OPENSSL_free(s->tlsext_ellipticcurvelist);
565 OPENSSL_free(s->alpn_client_proto_list);
566 EVP_PKEY_free(s->tlsext_channel_id_private);
567 OPENSSL_free(s->psk_identity_hint);
568 sk_X509_NAME_pop_free(s->client_CA, X509_NAME_free);
569 OPENSSL_free(s->next_proto_negotiated);
570 sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);
Adam Langley95c29f32014-06-20 12:00:00 -0700571
Adam Langleyfcf25832014-12-18 17:42:32 -0800572 if (s->method != NULL) {
573 s->method->ssl_free(s);
574 }
David Benjamin2755a3e2015-04-22 16:17:58 -0400575 SSL_CTX_free(s->ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700576
Adam Langleyfcf25832014-12-18 17:42:32 -0800577 OPENSSL_free(s);
578}
Adam Langley95c29f32014-06-20 12:00:00 -0700579
Adam Langleyfcf25832014-12-18 17:42:32 -0800580void SSL_set_bio(SSL *s, BIO *rbio, BIO *wbio) {
581 /* If the output buffering BIO is still in place, remove it. */
582 if (s->bbio != NULL) {
583 if (s->wbio == s->bbio) {
584 s->wbio = s->wbio->next_bio;
585 s->bbio->next_bio = NULL;
586 }
587 }
Adam Langley95c29f32014-06-20 12:00:00 -0700588
David Benjamin2755a3e2015-04-22 16:17:58 -0400589 if (s->rbio != rbio) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800590 BIO_free_all(s->rbio);
591 }
David Benjamin2755a3e2015-04-22 16:17:58 -0400592 if (s->wbio != wbio && s->rbio != s->wbio) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800593 BIO_free_all(s->wbio);
594 }
595 s->rbio = rbio;
596 s->wbio = wbio;
597}
Adam Langley95c29f32014-06-20 12:00:00 -0700598
Adam Langleyfcf25832014-12-18 17:42:32 -0800599BIO *SSL_get_rbio(const SSL *s) { return s->rbio; }
Adam Langley95c29f32014-06-20 12:00:00 -0700600
Adam Langleyfcf25832014-12-18 17:42:32 -0800601BIO *SSL_get_wbio(const SSL *s) { return s->wbio; }
Adam Langley95c29f32014-06-20 12:00:00 -0700602
Adam Langleyfcf25832014-12-18 17:42:32 -0800603int SSL_get_fd(const SSL *s) { return SSL_get_rfd(s); }
Adam Langley95c29f32014-06-20 12:00:00 -0700604
Adam Langleyfcf25832014-12-18 17:42:32 -0800605int SSL_get_rfd(const SSL *s) {
606 int ret = -1;
607 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700608
Adam Langleyfcf25832014-12-18 17:42:32 -0800609 b = SSL_get_rbio(s);
610 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
611 if (r != NULL) {
612 BIO_get_fd(r, &ret);
613 }
614 return ret;
615}
Adam Langley95c29f32014-06-20 12:00:00 -0700616
Adam Langleyfcf25832014-12-18 17:42:32 -0800617int SSL_get_wfd(const SSL *s) {
618 int ret = -1;
619 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700620
Adam Langleyfcf25832014-12-18 17:42:32 -0800621 b = SSL_get_wbio(s);
622 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
623 if (r != NULL) {
624 BIO_get_fd(r, &ret);
625 }
Adam Langley95c29f32014-06-20 12:00:00 -0700626
Adam Langleyfcf25832014-12-18 17:42:32 -0800627 return ret;
628}
Adam Langley95c29f32014-06-20 12:00:00 -0700629
Adam Langleyfcf25832014-12-18 17:42:32 -0800630int SSL_set_fd(SSL *s, int fd) {
631 int ret = 0;
632 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700633
Adam Langleyfcf25832014-12-18 17:42:32 -0800634 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700635
Adam Langleyfcf25832014-12-18 17:42:32 -0800636 if (bio == NULL) {
637 OPENSSL_PUT_ERROR(SSL, SSL_set_fd, ERR_R_BUF_LIB);
638 goto err;
639 }
640 BIO_set_fd(bio, fd, BIO_NOCLOSE);
641 SSL_set_bio(s, bio, bio);
642 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700643
Adam Langley95c29f32014-06-20 12:00:00 -0700644err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800645 return ret;
646}
Adam Langley95c29f32014-06-20 12:00:00 -0700647
Adam Langleyfcf25832014-12-18 17:42:32 -0800648int SSL_set_wfd(SSL *s, int fd) {
649 int ret = 0;
650 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700651
Adam Langleyfcf25832014-12-18 17:42:32 -0800652 if (s->rbio == NULL || BIO_method_type(s->rbio) != BIO_TYPE_FD ||
653 (int)BIO_get_fd(s->rbio, NULL) != fd) {
654 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700655
Adam Langleyfcf25832014-12-18 17:42:32 -0800656 if (bio == NULL) {
657 OPENSSL_PUT_ERROR(SSL, SSL_set_wfd, ERR_R_BUF_LIB);
658 goto err;
659 }
660 BIO_set_fd(bio, fd, BIO_NOCLOSE);
661 SSL_set_bio(s, SSL_get_rbio(s), bio);
662 } else {
663 SSL_set_bio(s, SSL_get_rbio(s), SSL_get_rbio(s));
664 }
665
666 ret = 1;
667
Adam Langley95c29f32014-06-20 12:00:00 -0700668err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800669 return ret;
670}
Adam Langley95c29f32014-06-20 12:00:00 -0700671
Adam Langleyfcf25832014-12-18 17:42:32 -0800672int SSL_set_rfd(SSL *s, int fd) {
673 int ret = 0;
674 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700675
Adam Langleyfcf25832014-12-18 17:42:32 -0800676 if (s->wbio == NULL || BIO_method_type(s->wbio) != BIO_TYPE_FD ||
677 (int)BIO_get_fd(s->wbio, NULL) != fd) {
678 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700679
Adam Langleyfcf25832014-12-18 17:42:32 -0800680 if (bio == NULL) {
681 OPENSSL_PUT_ERROR(SSL, SSL_set_rfd, ERR_R_BUF_LIB);
682 goto err;
683 }
684 BIO_set_fd(bio, fd, BIO_NOCLOSE);
685 SSL_set_bio(s, bio, SSL_get_wbio(s));
686 } else {
687 SSL_set_bio(s, SSL_get_wbio(s), SSL_get_wbio(s));
688 }
689 ret = 1;
690
Adam Langley95c29f32014-06-20 12:00:00 -0700691err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800692 return ret;
693}
Adam Langley95c29f32014-06-20 12:00:00 -0700694
695/* return length of latest Finished message we sent, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -0800696size_t SSL_get_finished(const SSL *s, void *buf, size_t count) {
697 size_t ret = 0;
698
699 if (s->s3 != NULL) {
700 ret = s->s3->tmp.finish_md_len;
701 if (count > ret) {
702 count = ret;
703 }
704 memcpy(buf, s->s3->tmp.finish_md, count);
705 }
706
707 return ret;
708}
Adam Langley95c29f32014-06-20 12:00:00 -0700709
710/* return length of latest Finished message we expected, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -0800711size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count) {
712 size_t ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700713
Adam Langleyfcf25832014-12-18 17:42:32 -0800714 if (s->s3 != NULL) {
715 ret = s->s3->tmp.peer_finish_md_len;
716 if (count > ret) {
717 count = ret;
718 }
719 memcpy(buf, s->s3->tmp.peer_finish_md, count);
720 }
Adam Langley95c29f32014-06-20 12:00:00 -0700721
Adam Langleyfcf25832014-12-18 17:42:32 -0800722 return ret;
723}
Adam Langley95c29f32014-06-20 12:00:00 -0700724
Adam Langleyfcf25832014-12-18 17:42:32 -0800725int SSL_get_verify_mode(const SSL *s) { return s->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -0700726
Adam Langleyfcf25832014-12-18 17:42:32 -0800727int SSL_get_verify_depth(const SSL *s) {
728 return X509_VERIFY_PARAM_get_depth(s->param);
729}
Adam Langley95c29f32014-06-20 12:00:00 -0700730
Adam Langleyfcf25832014-12-18 17:42:32 -0800731int (*SSL_get_verify_callback(const SSL *s))(int, X509_STORE_CTX *) {
732 return s->verify_callback;
733}
Adam Langley95c29f32014-06-20 12:00:00 -0700734
Adam Langleyfcf25832014-12-18 17:42:32 -0800735int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) { return ctx->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -0700736
Adam Langleyfcf25832014-12-18 17:42:32 -0800737int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) {
738 return X509_VERIFY_PARAM_get_depth(ctx->param);
739}
Adam Langley95c29f32014-06-20 12:00:00 -0700740
Adam Langleyfcf25832014-12-18 17:42:32 -0800741int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int, X509_STORE_CTX *) {
742 return ctx->default_verify_callback;
743}
Adam Langley95c29f32014-06-20 12:00:00 -0700744
Adam Langleyfcf25832014-12-18 17:42:32 -0800745void SSL_set_verify(SSL *s, int mode,
746 int (*callback)(int ok, X509_STORE_CTX *ctx)) {
747 s->verify_mode = mode;
748 if (callback != NULL) {
749 s->verify_callback = callback;
750 }
751}
Adam Langley95c29f32014-06-20 12:00:00 -0700752
Adam Langleyfcf25832014-12-18 17:42:32 -0800753void SSL_set_verify_depth(SSL *s, int depth) {
754 X509_VERIFY_PARAM_set_depth(s->param, depth);
755}
Adam Langley95c29f32014-06-20 12:00:00 -0700756
David Benjamin61ecccf2015-05-05 09:44:51 -0400757int SSL_CTX_get_read_ahead(const SSL_CTX *ctx) { return ctx->read_ahead; }
Adam Langley95c29f32014-06-20 12:00:00 -0700758
Adam Langleyfcf25832014-12-18 17:42:32 -0800759int SSL_get_read_ahead(const SSL *s) { return s->read_ahead; }
Adam Langley95c29f32014-06-20 12:00:00 -0700760
David Benjamin61ecccf2015-05-05 09:44:51 -0400761void SSL_CTX_set_read_ahead(SSL_CTX *ctx, int yes) { ctx->read_ahead = !!yes; }
762
763void SSL_set_read_ahead(SSL *s, int yes) { s->read_ahead = !!yes; }
764
Adam Langleyfcf25832014-12-18 17:42:32 -0800765int SSL_pending(const SSL *s) {
766 /* SSL_pending cannot work properly if read-ahead is enabled
767 * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)), and it is
768 * impossible to fix since SSL_pending cannot report errors that may be
769 * observed while scanning the new data. (Note that SSL_pending() is often
770 * used as a boolean value, so we'd better not return -1.). */
771 return s->method->ssl_pending(s);
772}
Adam Langley95c29f32014-06-20 12:00:00 -0700773
Adam Langleyfcf25832014-12-18 17:42:32 -0800774X509 *SSL_get_peer_certificate(const SSL *s) {
775 X509 *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700776
Adam Langleyfcf25832014-12-18 17:42:32 -0800777 if (s == NULL || s->session == NULL) {
778 r = NULL;
779 } else {
780 r = s->session->peer;
781 }
Adam Langley95c29f32014-06-20 12:00:00 -0700782
Adam Langleyfcf25832014-12-18 17:42:32 -0800783 if (r == NULL) {
784 return NULL;
785 }
Adam Langley95c29f32014-06-20 12:00:00 -0700786
Adam Langleyfcf25832014-12-18 17:42:32 -0800787 return X509_up_ref(r);
788}
789
790STACK_OF(X509) * SSL_get_peer_cert_chain(const SSL *s) {
791 STACK_OF(X509) * r;
792
793 if (s == NULL || s->session == NULL || s->session->sess_cert == NULL) {
794 r = NULL;
795 } else {
796 r = s->session->sess_cert->cert_chain;
797 }
798
799 /* If we are a client, cert_chain includes the peer's own certificate; if we
800 * are a server, it does not. */
801 return r;
802}
Adam Langley95c29f32014-06-20 12:00:00 -0700803
Adam Langley95c29f32014-06-20 12:00:00 -0700804/* Fix this so it checks all the valid key/cert options */
Adam Langleyfcf25832014-12-18 17:42:32 -0800805int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
806 if (ctx == NULL || ctx->cert == NULL || ctx->cert->key->x509 == NULL) {
807 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key,
808 SSL_R_NO_CERTIFICATE_ASSIGNED);
809 return 0;
810 }
811
812 if (ctx->cert->key->privatekey == NULL) {
813 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key,
814 SSL_R_NO_PRIVATE_KEY_ASSIGNED);
815 return 0;
816 }
817
818 return X509_check_private_key(ctx->cert->key->x509,
819 ctx->cert->key->privatekey);
820}
Adam Langley95c29f32014-06-20 12:00:00 -0700821
822/* Fix this function so that it takes an optional type parameter */
Adam Langleyfcf25832014-12-18 17:42:32 -0800823int SSL_check_private_key(const SSL *ssl) {
824 if (ssl == NULL) {
825 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, ERR_R_PASSED_NULL_PARAMETER);
826 return 0;
827 }
Adam Langley95c29f32014-06-20 12:00:00 -0700828
Adam Langleyfcf25832014-12-18 17:42:32 -0800829 if (ssl->cert == NULL) {
830 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
831 SSL_R_NO_CERTIFICATE_ASSIGNED);
832 return 0;
833 }
Adam Langley95c29f32014-06-20 12:00:00 -0700834
Adam Langleyfcf25832014-12-18 17:42:32 -0800835 if (ssl->cert->key->x509 == NULL) {
836 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
837 SSL_R_NO_CERTIFICATE_ASSIGNED);
838 return 0;
839 }
David Benjamin0b145c22014-11-26 20:10:09 -0500840
Adam Langleyfcf25832014-12-18 17:42:32 -0800841 if (ssl->cert->key->privatekey == NULL) {
842 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
843 SSL_R_NO_PRIVATE_KEY_ASSIGNED);
844 return 0;
845 }
Adam Langley95c29f32014-06-20 12:00:00 -0700846
Adam Langleyfcf25832014-12-18 17:42:32 -0800847 return X509_check_private_key(ssl->cert->key->x509,
848 ssl->cert->key->privatekey);
849}
Adam Langley95c29f32014-06-20 12:00:00 -0700850
Adam Langleyfcf25832014-12-18 17:42:32 -0800851int SSL_accept(SSL *s) {
852 if (s->handshake_func == 0) {
853 /* Not properly initialized yet */
854 SSL_set_accept_state(s);
855 }
David Benjamin0b145c22014-11-26 20:10:09 -0500856
Adam Langleyfcf25832014-12-18 17:42:32 -0800857 if (s->handshake_func != s->method->ssl_accept) {
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +0100858 OPENSSL_PUT_ERROR(SSL, SSL_accept, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800859 return -1;
860 }
Adam Langley95c29f32014-06-20 12:00:00 -0700861
Adam Langleyfcf25832014-12-18 17:42:32 -0800862 return s->handshake_func(s);
863}
Adam Langley95c29f32014-06-20 12:00:00 -0700864
Adam Langleyfcf25832014-12-18 17:42:32 -0800865int SSL_connect(SSL *s) {
866 if (s->handshake_func == 0) {
867 /* Not properly initialized yet */
868 SSL_set_connect_state(s);
869 }
Adam Langley95c29f32014-06-20 12:00:00 -0700870
Adam Langleyfcf25832014-12-18 17:42:32 -0800871 if (s->handshake_func != s->method->ssl_connect) {
872 OPENSSL_PUT_ERROR(SSL, SSL_connect, ERR_R_INTERNAL_ERROR);
873 return -1;
874 }
Adam Langley95c29f32014-06-20 12:00:00 -0700875
Adam Langleyfcf25832014-12-18 17:42:32 -0800876 return s->handshake_func(s);
877}
Adam Langley95c29f32014-06-20 12:00:00 -0700878
Adam Langleyfcf25832014-12-18 17:42:32 -0800879long SSL_get_default_timeout(const SSL *s) {
880 return SSL_DEFAULT_SESSION_TIMEOUT;
881}
Adam Langley95c29f32014-06-20 12:00:00 -0700882
Adam Langleyfcf25832014-12-18 17:42:32 -0800883int SSL_read(SSL *s, void *buf, int num) {
884 if (s->handshake_func == 0) {
885 OPENSSL_PUT_ERROR(SSL, SSL_read, SSL_R_UNINITIALIZED);
886 return -1;
887 }
Adam Langley95c29f32014-06-20 12:00:00 -0700888
Adam Langleyfcf25832014-12-18 17:42:32 -0800889 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
890 s->rwstate = SSL_NOTHING;
891 return 0;
892 }
Adam Langley95c29f32014-06-20 12:00:00 -0700893
Adam Langleyfcf25832014-12-18 17:42:32 -0800894 return s->method->ssl_read(s, buf, num);
895}
Adam Langley95c29f32014-06-20 12:00:00 -0700896
Adam Langleyfcf25832014-12-18 17:42:32 -0800897int SSL_peek(SSL *s, void *buf, int num) {
898 if (s->handshake_func == 0) {
899 OPENSSL_PUT_ERROR(SSL, SSL_peek, SSL_R_UNINITIALIZED);
900 return -1;
901 }
Adam Langley95c29f32014-06-20 12:00:00 -0700902
Adam Langleyfcf25832014-12-18 17:42:32 -0800903 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
904 return 0;
905 }
Adam Langley95c29f32014-06-20 12:00:00 -0700906
Adam Langleyfcf25832014-12-18 17:42:32 -0800907 return s->method->ssl_peek(s, buf, num);
908}
Adam Langley95c29f32014-06-20 12:00:00 -0700909
Adam Langleyfcf25832014-12-18 17:42:32 -0800910int SSL_write(SSL *s, const void *buf, int num) {
911 if (s->handshake_func == 0) {
912 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_UNINITIALIZED);
913 return -1;
914 }
Adam Langley95c29f32014-06-20 12:00:00 -0700915
Adam Langleyfcf25832014-12-18 17:42:32 -0800916 if (s->shutdown & SSL_SENT_SHUTDOWN) {
917 s->rwstate = SSL_NOTHING;
918 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_PROTOCOL_IS_SHUTDOWN);
919 return -1;
920 }
Adam Langley95c29f32014-06-20 12:00:00 -0700921
Adam Langleyfcf25832014-12-18 17:42:32 -0800922 return s->method->ssl_write(s, buf, num);
923}
Adam Langley95c29f32014-06-20 12:00:00 -0700924
Adam Langleyfcf25832014-12-18 17:42:32 -0800925int SSL_shutdown(SSL *s) {
926 /* Note that this function behaves differently from what one might expect.
927 * Return values are 0 for no success (yet), 1 for success; but calling it
928 * once is usually not enough, even if blocking I/O is used (see
929 * ssl3_shutdown). */
Adam Langley95c29f32014-06-20 12:00:00 -0700930
Adam Langleyfcf25832014-12-18 17:42:32 -0800931 if (s->handshake_func == 0) {
932 OPENSSL_PUT_ERROR(SSL, SSL_shutdown, SSL_R_UNINITIALIZED);
933 return -1;
934 }
Adam Langley95c29f32014-06-20 12:00:00 -0700935
Adam Langleyfcf25832014-12-18 17:42:32 -0800936 if (!SSL_in_init(s)) {
937 return s->method->ssl_shutdown(s);
938 }
Adam Langley95c29f32014-06-20 12:00:00 -0700939
Adam Langleyfcf25832014-12-18 17:42:32 -0800940 return 1;
941}
Adam Langley95c29f32014-06-20 12:00:00 -0700942
Adam Langleyfcf25832014-12-18 17:42:32 -0800943int SSL_renegotiate(SSL *s) {
David Benjamin2cdace92015-01-24 13:30:02 -0500944 if (SSL_IS_DTLS(s)) {
945 /* Renegotiation is not supported for DTLS. */
946 OPENSSL_PUT_ERROR(SSL, SSL_renegotiate, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
947 return 0;
948 }
949
Adam Langleyfcf25832014-12-18 17:42:32 -0800950 if (s->renegotiate == 0) {
951 s->renegotiate = 1;
952 }
Adam Langley95c29f32014-06-20 12:00:00 -0700953
Adam Langleyfcf25832014-12-18 17:42:32 -0800954 s->new_session = 1;
955 return s->method->ssl_renegotiate(s);
956}
Adam Langley95c29f32014-06-20 12:00:00 -0700957
Adam Langleyfcf25832014-12-18 17:42:32 -0800958int SSL_renegotiate_pending(SSL *s) {
959 /* becomes true when negotiation is requested; false again once a handshake
960 * has finished */
961 return s->renegotiate != 0;
962}
Adam Langley95c29f32014-06-20 12:00:00 -0700963
David Benjamin61ecccf2015-05-05 09:44:51 -0400964uint32_t SSL_CTX_set_options(SSL_CTX *ctx, uint32_t options) {
965 ctx->options |= options;
966 return ctx->options;
967}
968
969uint32_t SSL_set_options(SSL *ssl, uint32_t options) {
970 ssl->options |= options;
971 return ssl->options;
972}
973
974uint32_t SSL_CTX_clear_options(SSL_CTX *ctx, uint32_t options) {
975 ctx->options &= ~options;
976 return ctx->options;
977}
978
979uint32_t SSL_clear_options(SSL *ssl, uint32_t options) {
980 ssl->options &= ~options;
981 return ssl->options;
982}
983
984uint32_t SSL_CTX_get_options(const SSL_CTX *ctx) { return ctx->options; }
985
986uint32_t SSL_get_options(const SSL *ssl) { return ssl->options; }
987
988uint32_t SSL_CTX_set_mode(SSL_CTX *ctx, uint32_t mode) {
989 ctx->mode |= mode;
990 return ctx->mode;
991}
992
993uint32_t SSL_set_mode(SSL *ssl, uint32_t mode) {
994 ssl->mode |= mode;
995 return ssl->mode;
996}
997
998uint32_t SSL_CTX_clear_mode(SSL_CTX *ctx, uint32_t mode) {
999 ctx->mode &= ~mode;
1000 return ctx->mode;
1001}
1002
1003uint32_t SSL_clear_mode(SSL *ssl, uint32_t mode) {
1004 ssl->mode &= ~mode;
1005 return ssl->mode;
1006}
1007
1008uint32_t SSL_CTX_get_mode(const SSL_CTX *ctx) { return ctx->mode; }
1009
1010uint32_t SSL_get_mode(const SSL *ssl) { return ssl->mode; }
1011
Adam Langleyfcf25832014-12-18 17:42:32 -08001012long SSL_ctrl(SSL *s, int cmd, long larg, void *parg) {
1013 long l;
Adam Langley95c29f32014-06-20 12:00:00 -07001014
Adam Langleyfcf25832014-12-18 17:42:32 -08001015 switch (cmd) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001016 case SSL_CTRL_GET_MAX_CERT_LIST:
1017 return s->max_cert_list;
Adam Langley95c29f32014-06-20 12:00:00 -07001018
Adam Langleyfcf25832014-12-18 17:42:32 -08001019 case SSL_CTRL_SET_MAX_CERT_LIST:
1020 l = s->max_cert_list;
1021 s->max_cert_list = larg;
1022 return l;
Adam Langley95c29f32014-06-20 12:00:00 -07001023
Adam Langleyfcf25832014-12-18 17:42:32 -08001024 case SSL_CTRL_SET_MTU:
1025 if (larg < (long)dtls1_min_mtu()) {
1026 return 0;
1027 }
1028 if (SSL_IS_DTLS(s)) {
1029 s->d1->mtu = larg;
1030 return larg;
1031 }
1032 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001033
Adam Langleyfcf25832014-12-18 17:42:32 -08001034 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1035 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH) {
1036 return 0;
1037 }
1038 s->max_send_fragment = larg;
1039 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001040
Adam Langleyfcf25832014-12-18 17:42:32 -08001041 case SSL_CTRL_GET_RI_SUPPORT:
1042 if (s->s3) {
1043 return s->s3->send_connection_binding;
1044 }
1045 return 0;
1046
Adam Langleyfcf25832014-12-18 17:42:32 -08001047 case SSL_CTRL_GET_RAW_CIPHERLIST:
1048 if (parg) {
1049 if (s->cert->ciphers_raw == NULL) {
1050 return 0;
1051 }
1052 *(uint8_t **)parg = s->cert->ciphers_raw;
1053 return (int)s->cert->ciphers_rawlen;
1054 }
1055
1056 /* Passing a NULL |parg| returns the size of a single
1057 * cipher suite value. */
1058 return 2;
1059
1060 default:
1061 return s->method->ssl_ctrl(s, cmd, larg, parg);
1062 }
1063}
1064
Adam Langleyfcf25832014-12-18 17:42:32 -08001065LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) { return ctx->sessions; }
1066
1067long SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg) {
1068 long l;
1069
1070 switch (cmd) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001071 case SSL_CTRL_GET_MAX_CERT_LIST:
1072 return ctx->max_cert_list;
1073
1074 case SSL_CTRL_SET_MAX_CERT_LIST:
1075 l = ctx->max_cert_list;
1076 ctx->max_cert_list = larg;
1077 return l;
1078
1079 case SSL_CTRL_SET_SESS_CACHE_SIZE:
1080 l = ctx->session_cache_size;
1081 ctx->session_cache_size = larg;
1082 return l;
1083
1084 case SSL_CTRL_GET_SESS_CACHE_SIZE:
1085 return ctx->session_cache_size;
1086
1087 case SSL_CTRL_SET_SESS_CACHE_MODE:
1088 l = ctx->session_cache_mode;
1089 ctx->session_cache_mode = larg;
1090 return l;
1091
1092 case SSL_CTRL_GET_SESS_CACHE_MODE:
1093 return ctx->session_cache_mode;
1094
1095 case SSL_CTRL_SESS_NUMBER:
1096 return lh_SSL_SESSION_num_items(ctx->sessions);
1097
Adam Langleyfcf25832014-12-18 17:42:32 -08001098 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1099 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH) {
1100 return 0;
1101 }
1102 ctx->max_send_fragment = larg;
1103 return 1;
1104
Adam Langleyfcf25832014-12-18 17:42:32 -08001105 default:
1106 return ctx->method->ssl_ctx_ctrl(ctx, cmd, larg, parg);
1107 }
1108}
1109
Adam Langleyfcf25832014-12-18 17:42:32 -08001110int ssl_cipher_id_cmp(const void *in_a, const void *in_b) {
1111 long l;
1112 const SSL_CIPHER *a = in_a;
1113 const SSL_CIPHER *b = in_b;
1114 const long a_id = a->id;
1115 const long b_id = b->id;
1116
1117 l = a_id - b_id;
1118 if (l == 0L) {
1119 return 0;
1120 } else {
1121 return (l > 0) ? 1 : -1;
1122 }
1123}
1124
1125int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **ap, const SSL_CIPHER **bp) {
1126 long l;
1127 const long a_id = (*ap)->id;
1128 const long b_id = (*bp)->id;
1129
1130 l = a_id - b_id;
1131 if (l == 0) {
1132 return 0;
1133 } else {
1134 return (l > 0) ? 1 : -1;
1135 }
1136}
1137
1138/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001139 * preference */
Adam Langleyfcf25832014-12-18 17:42:32 -08001140STACK_OF(SSL_CIPHER) * SSL_get_ciphers(const SSL *s) {
1141 if (s == NULL) {
1142 return NULL;
1143 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001144
Adam Langleyfcf25832014-12-18 17:42:32 -08001145 if (s->cipher_list != NULL) {
1146 return s->cipher_list->ciphers;
1147 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001148
Adam Langleyfcf25832014-12-18 17:42:32 -08001149 if (s->version >= TLS1_1_VERSION && s->ctx != NULL &&
1150 s->ctx->cipher_list_tls11 != NULL) {
1151 return s->ctx->cipher_list_tls11->ciphers;
1152 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001153
Adam Langleyfcf25832014-12-18 17:42:32 -08001154 if (s->ctx != NULL && s->ctx->cipher_list != NULL) {
1155 return s->ctx->cipher_list->ciphers;
1156 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001157
Adam Langleyfcf25832014-12-18 17:42:32 -08001158 return NULL;
1159}
Adam Langley95c29f32014-06-20 12:00:00 -07001160
Adam Langleyfcf25832014-12-18 17:42:32 -08001161/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001162 * algorithm id */
Adam Langleyfcf25832014-12-18 17:42:32 -08001163STACK_OF(SSL_CIPHER) * ssl_get_ciphers_by_id(SSL *s) {
1164 if (s == NULL) {
1165 return NULL;
1166 }
Adam Langley95c29f32014-06-20 12:00:00 -07001167
Adam Langleyfcf25832014-12-18 17:42:32 -08001168 if (s->cipher_list_by_id != NULL) {
1169 return s->cipher_list_by_id;
1170 }
Adam Langley95c29f32014-06-20 12:00:00 -07001171
Adam Langleyfcf25832014-12-18 17:42:32 -08001172 if (s->ctx != NULL && s->ctx->cipher_list_by_id != NULL) {
1173 return s->ctx->cipher_list_by_id;
1174 }
Adam Langley95c29f32014-06-20 12:00:00 -07001175
Adam Langleyfcf25832014-12-18 17:42:32 -08001176 return NULL;
1177}
Adam Langley95c29f32014-06-20 12:00:00 -07001178
Adam Langleyfcf25832014-12-18 17:42:32 -08001179/* The old interface to get the same thing as SSL_get_ciphers() */
1180const char *SSL_get_cipher_list(const SSL *s, int n) {
1181 const SSL_CIPHER *c;
1182 STACK_OF(SSL_CIPHER) * sk;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001183
Adam Langleyfcf25832014-12-18 17:42:32 -08001184 if (s == NULL) {
1185 return NULL;
1186 }
Adam Langley95c29f32014-06-20 12:00:00 -07001187
Adam Langleyfcf25832014-12-18 17:42:32 -08001188 sk = SSL_get_ciphers(s);
1189 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
1190 return NULL;
1191 }
Adam Langley95c29f32014-06-20 12:00:00 -07001192
Adam Langleyfcf25832014-12-18 17:42:32 -08001193 c = sk_SSL_CIPHER_value(sk, n);
1194 if (c == NULL) {
1195 return NULL;
1196 }
Adam Langley95c29f32014-06-20 12:00:00 -07001197
Adam Langleyfcf25832014-12-18 17:42:32 -08001198 return c->name;
1199}
David Benjamin5491e3f2014-09-29 19:33:09 -04001200
Adam Langleyfcf25832014-12-18 17:42:32 -08001201/* specify the ciphers to be used by default by the SSL_CTX */
1202int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
1203 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001204
Adam Langleyfcf25832014-12-18 17:42:32 -08001205 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001206 &ctx->cipher_list_by_id, str);
Adam Langleyfcf25832014-12-18 17:42:32 -08001207 /* ssl_create_cipher_list may return an empty stack if it was unable to find
1208 * a cipher matching the given rule string (for example if the rule string
1209 * specifies a cipher which has been disabled). This is not an error as far
1210 * as ssl_create_cipher_list is concerned, and hence ctx->cipher_list and
1211 * ctx->cipher_list_by_id has been updated. */
1212 if (sk == NULL) {
1213 return 0;
1214 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1215 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1216 return 0;
1217 }
Adam Langley95c29f32014-06-20 12:00:00 -07001218
Adam Langleyfcf25832014-12-18 17:42:32 -08001219 return 1;
1220}
David Benjamin39482a12014-07-20 13:30:15 -04001221
Adam Langleyfcf25832014-12-18 17:42:32 -08001222int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str) {
1223 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001224
David Benjamin71f07942015-04-08 02:36:59 -04001225 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str);
Adam Langleyfcf25832014-12-18 17:42:32 -08001226 if (sk == NULL) {
1227 return 0;
1228 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1229 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list_tls11,
1230 SSL_R_NO_CIPHER_MATCH);
1231 return 0;
1232 }
David Benjamin9f2c0d72014-10-21 22:00:19 -04001233
Adam Langleyfcf25832014-12-18 17:42:32 -08001234 return 1;
1235}
Adam Langley95c29f32014-06-20 12:00:00 -07001236
Adam Langleyfcf25832014-12-18 17:42:32 -08001237/* specify the ciphers to be used by the SSL */
1238int SSL_set_cipher_list(SSL *s, const char *str) {
1239 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001240
Adam Langleyfcf25832014-12-18 17:42:32 -08001241 sk = ssl_create_cipher_list(s->ctx->method, &s->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001242 &s->cipher_list_by_id, str);
David Benjamin39482a12014-07-20 13:30:15 -04001243
Adam Langleyfcf25832014-12-18 17:42:32 -08001244 /* see comment in SSL_CTX_set_cipher_list */
1245 if (sk == NULL) {
1246 return 0;
1247 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1248 OPENSSL_PUT_ERROR(SSL, SSL_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1249 return 0;
1250 }
David Benjamin39482a12014-07-20 13:30:15 -04001251
Adam Langleyfcf25832014-12-18 17:42:32 -08001252 return 1;
1253}
Adam Langley95c29f32014-06-20 12:00:00 -07001254
Adam Langleyfcf25832014-12-18 17:42:32 -08001255int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, uint8_t *p) {
1256 size_t i;
1257 const SSL_CIPHER *c;
1258 CERT *ct = s->cert;
1259 uint8_t *q;
1260 /* Set disabled masks for this session */
1261 ssl_set_client_disabled(s);
Adam Langley29707792014-06-20 12:00:00 -07001262
Adam Langleyfcf25832014-12-18 17:42:32 -08001263 if (sk == NULL) {
1264 return 0;
1265 }
1266 q = p;
Adam Langley95c29f32014-06-20 12:00:00 -07001267
Adam Langleyfcf25832014-12-18 17:42:32 -08001268 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
1269 c = sk_SSL_CIPHER_value(sk, i);
1270 /* Skip disabled ciphers */
1271 if (c->algorithm_ssl & ct->mask_ssl ||
1272 c->algorithm_mkey & ct->mask_k ||
1273 c->algorithm_auth & ct->mask_a) {
1274 continue;
1275 }
1276 s2n(ssl3_get_cipher_value(c), p);
1277 }
1278
1279 /* If all ciphers were disabled, return the error to the caller. */
1280 if (p == q) {
1281 return 0;
1282 }
1283
1284 /* Add SCSVs. */
1285 if (!s->renegotiate) {
1286 s2n(SSL3_CK_SCSV & 0xffff, p);
1287 }
1288
Adam Langley5f0efe02015-02-20 13:03:16 -08001289 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001290 s2n(SSL3_CK_FALLBACK_SCSV & 0xffff, p);
1291 }
1292
1293 return p - q;
1294}
1295
1296STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs) {
1297 CBS cipher_suites = *cbs;
1298 const SSL_CIPHER *c;
1299 STACK_OF(SSL_CIPHER) * sk;
1300
1301 if (s->s3) {
1302 s->s3->send_connection_binding = 0;
1303 }
1304
1305 if (CBS_len(&cipher_suites) % 2 != 0) {
1306 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1307 SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
1308 return NULL;
1309 }
1310
1311 sk = sk_SSL_CIPHER_new_null();
1312 if (sk == NULL) {
1313 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1314 goto err;
1315 }
1316
1317 if (!CBS_stow(&cipher_suites, &s->cert->ciphers_raw,
1318 &s->cert->ciphers_rawlen)) {
1319 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1320 goto err;
1321 }
1322
1323 while (CBS_len(&cipher_suites) > 0) {
1324 uint16_t cipher_suite;
1325
1326 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
1327 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_INTERNAL_ERROR);
1328 goto err;
1329 }
1330
1331 /* Check for SCSV. */
1332 if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff)) {
1333 /* SCSV is fatal if renegotiating. */
1334 if (s->renegotiate) {
1335 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1336 SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1337 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1338 goto err;
1339 }
1340 s->s3->send_connection_binding = 1;
1341 continue;
1342 }
1343
1344 /* Check for FALLBACK_SCSV. */
1345 if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff)) {
1346 uint16_t max_version = ssl3_get_max_server_version(s);
1347 if (SSL_IS_DTLS(s) ? (uint16_t)s->version > max_version
1348 : (uint16_t)s->version < max_version) {
1349 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1350 SSL_R_INAPPROPRIATE_FALLBACK);
1351 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
1352 goto err;
1353 }
1354 continue;
1355 }
1356
1357 c = ssl3_get_cipher_by_value(cipher_suite);
1358 if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
1359 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1360 goto err;
1361 }
1362 }
1363
1364 return sk;
David Benjamin9f2c0d72014-10-21 22:00:19 -04001365
Adam Langley95c29f32014-06-20 12:00:00 -07001366err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001367 sk_SSL_CIPHER_free(sk);
Adam Langleyfcf25832014-12-18 17:42:32 -08001368 return NULL;
1369}
Adam Langley95c29f32014-06-20 12:00:00 -07001370
1371
Adam Langleyfcf25832014-12-18 17:42:32 -08001372/* return a servername extension value if provided in Client Hello, or NULL. So
1373 * far, only host_name types are defined (RFC 3546). */
1374const char *SSL_get_servername(const SSL *s, const int type) {
1375 if (type != TLSEXT_NAMETYPE_host_name) {
1376 return NULL;
1377 }
Adam Langley95c29f32014-06-20 12:00:00 -07001378
Adam Langleyfcf25832014-12-18 17:42:32 -08001379 return s->session && !s->tlsext_hostname ? s->session->tlsext_hostname
1380 : s->tlsext_hostname;
1381}
Adam Langley95c29f32014-06-20 12:00:00 -07001382
Adam Langleyfcf25832014-12-18 17:42:32 -08001383int SSL_get_servername_type(const SSL *s) {
1384 if (s->session &&
1385 (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname)) {
1386 return TLSEXT_NAMETYPE_host_name;
1387 }
Adam Langley95c29f32014-06-20 12:00:00 -07001388
Adam Langleyfcf25832014-12-18 17:42:32 -08001389 return -1;
1390}
Adam Langley95c29f32014-06-20 12:00:00 -07001391
Adam Langleyfcf25832014-12-18 17:42:32 -08001392void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
1393 ctx->signed_cert_timestamps_enabled = 1;
1394}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001395
Adam Langleyfcf25832014-12-18 17:42:32 -08001396int SSL_enable_signed_cert_timestamps(SSL *ssl) {
1397 ssl->signed_cert_timestamps_enabled = 1;
1398 return 1;
1399}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001400
Adam Langleyfcf25832014-12-18 17:42:32 -08001401void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
1402 ctx->ocsp_stapling_enabled = 1;
1403}
David Benjamin6c7aed02014-08-27 16:42:38 -04001404
Adam Langleyfcf25832014-12-18 17:42:32 -08001405int SSL_enable_ocsp_stapling(SSL *ssl) {
1406 ssl->ocsp_stapling_enabled = 1;
1407 return 1;
1408}
David Benjamin6c7aed02014-08-27 16:42:38 -04001409
Adam Langleyfcf25832014-12-18 17:42:32 -08001410void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
1411 size_t *out_len) {
1412 SSL_SESSION *session = ssl->session;
Adam Langley3cb50e02014-08-26 14:00:31 -07001413
Adam Langleyfcf25832014-12-18 17:42:32 -08001414 *out_len = 0;
1415 *out = NULL;
1416 if (ssl->server || !session || !session->tlsext_signed_cert_timestamp_list) {
1417 return;
1418 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001419
Adam Langleyfcf25832014-12-18 17:42:32 -08001420 *out = session->tlsext_signed_cert_timestamp_list;
1421 *out_len = session->tlsext_signed_cert_timestamp_list_length;
1422}
David Benjamin6c7aed02014-08-27 16:42:38 -04001423
Adam Langleyfcf25832014-12-18 17:42:32 -08001424void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
1425 size_t *out_len) {
1426 SSL_SESSION *session = ssl->session;
1427
1428 *out_len = 0;
1429 *out = NULL;
1430 if (ssl->server || !session || !session->ocsp_response) {
1431 return;
1432 }
1433 *out = session->ocsp_response;
1434 *out_len = session->ocsp_response_length;
1435}
David Benjamin6c7aed02014-08-27 16:42:38 -04001436
Adam Langley95c29f32014-06-20 12:00:00 -07001437/* SSL_select_next_proto implements the standard protocol selection. It is
1438 * expected that this function is called from the callback set by
1439 * SSL_CTX_set_next_proto_select_cb.
1440 *
1441 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1442 * strings. The length byte itself is not included in the length. A byte
1443 * string of length 0 is invalid. No byte string may be truncated.
1444 *
1445 * The current, but experimental algorithm for selecting the protocol is:
1446 *
1447 * 1) If the server doesn't support NPN then this is indicated to the
1448 * callback. In this case, the client application has to abort the connection
1449 * or have a default application level protocol.
1450 *
1451 * 2) If the server supports NPN, but advertises an empty list then the
1452 * client selects the first protcol in its list, but indicates via the
1453 * API that this fallback case was enacted.
1454 *
1455 * 3) Otherwise, the client finds the first protocol in the server's list
1456 * that it supports and selects this protocol. This is because it's
1457 * assumed that the server has better information about which protocol
1458 * a client should use.
1459 *
1460 * 4) If the client doesn't support any of the server's advertised
1461 * protocols, then this is treated the same as case 2.
1462 *
1463 * It returns either
1464 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1465 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1466 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001467int SSL_select_next_proto(uint8_t **out, uint8_t *outlen, const uint8_t *server,
1468 unsigned int server_len, const uint8_t *client,
1469 unsigned int client_len) {
1470 unsigned int i, j;
1471 const uint8_t *result;
1472 int status = OPENSSL_NPN_UNSUPPORTED;
Adam Langley95c29f32014-06-20 12:00:00 -07001473
Adam Langleyfcf25832014-12-18 17:42:32 -08001474 /* For each protocol in server preference order, see if we support it. */
1475 for (i = 0; i < server_len;) {
1476 for (j = 0; j < client_len;) {
1477 if (server[i] == client[j] &&
1478 memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
1479 /* We found a match */
1480 result = &server[i];
1481 status = OPENSSL_NPN_NEGOTIATED;
1482 goto found;
1483 }
1484 j += client[j];
1485 j++;
1486 }
1487 i += server[i];
1488 i++;
1489 }
Adam Langley95c29f32014-06-20 12:00:00 -07001490
Adam Langleyfcf25832014-12-18 17:42:32 -08001491 /* There's no overlap between our protocols and the server's list. */
1492 result = client;
1493 status = OPENSSL_NPN_NO_OVERLAP;
Adam Langley95c29f32014-06-20 12:00:00 -07001494
Adam Langleyfcf25832014-12-18 17:42:32 -08001495found:
1496 *out = (uint8_t *)result + 1;
1497 *outlen = result[0];
1498 return status;
1499}
Adam Langley95c29f32014-06-20 12:00:00 -07001500
Adam Langley95c29f32014-06-20 12:00:00 -07001501/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
1502 * requested protocol for this connection and returns 0. If the client didn't
1503 * request any protocol, then *data is set to NULL.
1504 *
1505 * Note that the client can request any protocol it chooses. The value returned
1506 * from this function need not be a member of the list of supported protocols
Adam Langleyfcf25832014-12-18 17:42:32 -08001507 * provided by the callback. */
1508void SSL_get0_next_proto_negotiated(const SSL *s, const uint8_t **data,
1509 unsigned *len) {
1510 *data = s->next_proto_negotiated;
1511 if (!*data) {
1512 *len = 0;
1513 } else {
1514 *len = s->next_proto_negotiated_len;
1515 }
Adam Langley95c29f32014-06-20 12:00:00 -07001516}
1517
1518/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
1519 * TLS server needs a list of supported protocols for Next Protocol
1520 * Negotiation. The returned list must be in wire format. The list is returned
1521 * by setting |out| to point to it and |outlen| to its length. This memory will
1522 * not be modified, but one should assume that the SSL* keeps a reference to
1523 * it.
1524 *
Adam Langleyfcf25832014-12-18 17:42:32 -08001525 * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise.
1526 * Otherwise, no such extension will be included in the ServerHello. */
1527void SSL_CTX_set_next_protos_advertised_cb(
1528 SSL_CTX *ctx,
1529 int (*cb)(SSL *ssl, const uint8_t **out, unsigned int *outlen, void *arg),
1530 void *arg) {
1531 ctx->next_protos_advertised_cb = cb;
1532 ctx->next_protos_advertised_cb_arg = arg;
1533}
Adam Langley95c29f32014-06-20 12:00:00 -07001534
1535/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1536 * client needs to select a protocol from the server's provided list. |out|
1537 * must be set to point to the selected protocol (which may be within |in|).
1538 * The length of the protocol name must be written into |outlen|. The server's
1539 * advertised protocols are provided in |in| and |inlen|. The callback can
1540 * assume that |in| is syntactically valid.
1541 *
1542 * The client must select a protocol. It is fatal to the connection if this
1543 * callback returns a value other than SSL_TLSEXT_ERR_OK.
1544 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001545void SSL_CTX_set_next_proto_select_cb(
1546 SSL_CTX *ctx, int (*cb)(SSL *s, uint8_t **out, uint8_t *outlen,
1547 const uint8_t *in, unsigned int inlen, void *arg),
1548 void *arg) {
1549 ctx->next_proto_select_cb = cb;
1550 ctx->next_proto_select_cb_arg = arg;
1551}
Adam Langley95c29f32014-06-20 12:00:00 -07001552
Adam Langleyfcf25832014-12-18 17:42:32 -08001553int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
1554 unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001555 OPENSSL_free(ctx->alpn_client_proto_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001556 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1557 if (!ctx->alpn_client_proto_list) {
1558 return 1;
1559 }
1560 ctx->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001561
Adam Langleyfcf25832014-12-18 17:42:32 -08001562 return 0;
1563}
Adam Langley95c29f32014-06-20 12:00:00 -07001564
Adam Langleyfcf25832014-12-18 17:42:32 -08001565int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001566 OPENSSL_free(ssl->alpn_client_proto_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001567 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1568 if (!ssl->alpn_client_proto_list) {
1569 return 1;
1570 }
1571 ssl->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001572
Adam Langleyfcf25832014-12-18 17:42:32 -08001573 return 0;
1574}
Adam Langley95c29f32014-06-20 12:00:00 -07001575
1576/* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
1577 * during ClientHello processing in order to select an ALPN protocol from the
1578 * client's list of offered protocols. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001579void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
1580 int (*cb)(SSL *ssl, const uint8_t **out,
1581 uint8_t *outlen, const uint8_t *in,
1582 unsigned int inlen, void *arg),
1583 void *arg) {
1584 ctx->alpn_select_cb = cb;
1585 ctx->alpn_select_cb_arg = arg;
1586}
Adam Langley95c29f32014-06-20 12:00:00 -07001587
1588/* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
1589 * On return it sets |*data| to point to |*len| bytes of protocol name (not
1590 * including the leading length-prefix byte). If the server didn't respond with
1591 * a negotiated protocol then |*len| will be zero. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001592void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **data,
1593 unsigned *len) {
1594 *data = NULL;
1595 if (ssl->s3) {
1596 *data = ssl->s3->alpn_selected;
1597 }
1598 if (*data == NULL) {
1599 *len = 0;
1600 } else {
1601 *len = ssl->s3->alpn_selected_len;
1602 }
1603}
Adam Langley95c29f32014-06-20 12:00:00 -07001604
David Benjamincfd248b2015-04-03 11:02:24 -04001605int SSL_export_keying_material(SSL *s, uint8_t *out, size_t out_len,
1606 const char *label, size_t label_len,
1607 const uint8_t *context, size_t context_len,
1608 int use_context) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001609 if (s->version < TLS1_VERSION) {
David Benjaminc565ebb2015-04-03 04:06:36 -04001610 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001611 }
Adam Langley95c29f32014-06-20 12:00:00 -07001612
David Benjamincfd248b2015-04-03 11:02:24 -04001613 return s->enc_method->export_keying_material(
1614 s, out, out_len, label, label_len, context, context_len, use_context);
Adam Langleyfcf25832014-12-18 17:42:32 -08001615}
Adam Langley95c29f32014-06-20 12:00:00 -07001616
Adam Langleyfcf25832014-12-18 17:42:32 -08001617static uint32_t ssl_session_hash(const SSL_SESSION *a) {
1618 uint32_t hash =
1619 ((uint32_t)a->session_id[0]) ||
1620 ((uint32_t)a->session_id[1] << 8) ||
1621 ((uint32_t)a->session_id[2] << 16) ||
1622 ((uint32_t)a->session_id[3] << 24);
Adam Langley95c29f32014-06-20 12:00:00 -07001623
Adam Langleyfcf25832014-12-18 17:42:32 -08001624 return hash;
1625}
Adam Langley95c29f32014-06-20 12:00:00 -07001626
1627/* NB: If this function (or indeed the hash function which uses a sort of
1628 * coarser function than this one) is changed, ensure
1629 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1630 * able to construct an SSL_SESSION that will collide with any existing session
1631 * with a matching session ID. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001632static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
1633 if (a->ssl_version != b->ssl_version) {
1634 return 1;
1635 }
Adam Langley95c29f32014-06-20 12:00:00 -07001636
Adam Langleyfcf25832014-12-18 17:42:32 -08001637 if (a->session_id_length != b->session_id_length) {
1638 return 1;
1639 }
Adam Langley95c29f32014-06-20 12:00:00 -07001640
Adam Langleyfcf25832014-12-18 17:42:32 -08001641 return memcmp(a->session_id, b->session_id, a->session_id_length);
1642}
Adam Langley95c29f32014-06-20 12:00:00 -07001643
Adam Langleyfcf25832014-12-18 17:42:32 -08001644SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth) {
1645 SSL_CTX *ret = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001646
Adam Langleyfcf25832014-12-18 17:42:32 -08001647 if (meth == NULL) {
1648 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_NULL_SSL_METHOD_PASSED);
1649 return NULL;
1650 }
Adam Langley95c29f32014-06-20 12:00:00 -07001651
Adam Langleyfcf25832014-12-18 17:42:32 -08001652 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
1653 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
1654 goto err;
1655 }
Adam Langley95c29f32014-06-20 12:00:00 -07001656
Adam Langleyfcf25832014-12-18 17:42:32 -08001657 ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1658 if (ret == NULL) {
1659 goto err;
1660 }
Adam Langley95c29f32014-06-20 12:00:00 -07001661
Adam Langleyfcf25832014-12-18 17:42:32 -08001662 memset(ret, 0, sizeof(SSL_CTX));
Adam Langley95c29f32014-06-20 12:00:00 -07001663
Adam Langleyfcf25832014-12-18 17:42:32 -08001664 ret->method = meth->method;
Adam Langley95c29f32014-06-20 12:00:00 -07001665
Adam Langleyfcf25832014-12-18 17:42:32 -08001666 ret->cert_store = NULL;
1667 ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
1668 ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1669 ret->session_cache_head = NULL;
1670 ret->session_cache_tail = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001671
Adam Langleyfcf25832014-12-18 17:42:32 -08001672 /* We take the system default */
1673 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001674
Adam Langleyfcf25832014-12-18 17:42:32 -08001675 ret->new_session_cb = 0;
1676 ret->remove_session_cb = 0;
1677 ret->get_session_cb = 0;
1678 ret->generate_session_id = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001679
Adam Langleyfcf25832014-12-18 17:42:32 -08001680 ret->references = 1;
1681 ret->quiet_shutdown = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001682
Adam Langleyfcf25832014-12-18 17:42:32 -08001683 ret->info_callback = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001684
Adam Langleyfcf25832014-12-18 17:42:32 -08001685 ret->app_verify_callback = 0;
1686 ret->app_verify_arg = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001687
Adam Langleyfcf25832014-12-18 17:42:32 -08001688 ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
1689 ret->read_ahead = 0;
1690 ret->msg_callback = 0;
1691 ret->msg_callback_arg = NULL;
1692 ret->verify_mode = SSL_VERIFY_NONE;
1693 ret->sid_ctx_length = 0;
1694 ret->default_verify_callback = NULL;
1695 ret->cert = ssl_cert_new();
1696 if (ret->cert == NULL) {
1697 goto err;
1698 }
Adam Langley95c29f32014-06-20 12:00:00 -07001699
Adam Langleyfcf25832014-12-18 17:42:32 -08001700 ret->default_passwd_callback = 0;
1701 ret->default_passwd_callback_userdata = NULL;
1702 ret->client_cert_cb = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001703
Adam Langleyfcf25832014-12-18 17:42:32 -08001704 ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
1705 if (ret->sessions == NULL) {
1706 goto err;
1707 }
1708 ret->cert_store = X509_STORE_new();
1709 if (ret->cert_store == NULL) {
1710 goto err;
1711 }
Adam Langley95c29f32014-06-20 12:00:00 -07001712
Adam Langleyfcf25832014-12-18 17:42:32 -08001713 ssl_create_cipher_list(ret->method, &ret->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001714 &ret->cipher_list_by_id, SSL_DEFAULT_CIPHER_LIST);
Adam Langleyfcf25832014-12-18 17:42:32 -08001715 if (ret->cipher_list == NULL ||
1716 sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0) {
1717 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_LIBRARY_HAS_NO_CIPHERS);
1718 goto err2;
1719 }
Adam Langley95c29f32014-06-20 12:00:00 -07001720
Adam Langleyfcf25832014-12-18 17:42:32 -08001721 ret->param = X509_VERIFY_PARAM_new();
1722 if (!ret->param) {
1723 goto err;
1724 }
Adam Langley95c29f32014-06-20 12:00:00 -07001725
Adam Langleyfcf25832014-12-18 17:42:32 -08001726 ret->client_CA = sk_X509_NAME_new_null();
1727 if (ret->client_CA == NULL) {
1728 goto err;
1729 }
Adam Langley95c29f32014-06-20 12:00:00 -07001730
David Benjamin9f33fc62015-04-15 17:29:53 -04001731 CRYPTO_new_ex_data(&g_ex_data_class_ssl_ctx, ret, &ret->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001732
Adam Langleyfcf25832014-12-18 17:42:32 -08001733 ret->extra_certs = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001734
Adam Langleyfcf25832014-12-18 17:42:32 -08001735 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
David Benjamin422d3a42014-08-20 11:09:03 -04001736
Adam Langleyfcf25832014-12-18 17:42:32 -08001737 ret->tlsext_servername_callback = 0;
1738 ret->tlsext_servername_arg = NULL;
1739 /* Setup RFC4507 ticket keys */
1740 if (!RAND_bytes(ret->tlsext_tick_key_name, 16) ||
1741 !RAND_bytes(ret->tlsext_tick_hmac_key, 16) ||
1742 !RAND_bytes(ret->tlsext_tick_aes_key, 16)) {
1743 ret->options |= SSL_OP_NO_TICKET;
1744 }
Adam Langley95c29f32014-06-20 12:00:00 -07001745
Adam Langleyfcf25832014-12-18 17:42:32 -08001746 ret->next_protos_advertised_cb = 0;
1747 ret->next_proto_select_cb = 0;
1748 ret->psk_identity_hint = NULL;
1749 ret->psk_client_callback = NULL;
1750 ret->psk_server_callback = NULL;
1751
1752 /* Default is to connect to non-RI servers. When RI is more widely deployed
1753 * might change this. */
1754 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
1755
1756 /* Lock the SSL_CTX to the specified version, for compatibility with legacy
1757 * uses of SSL_METHOD. */
1758 if (meth->version != 0) {
1759 SSL_CTX_set_max_version(ret, meth->version);
1760 SSL_CTX_set_min_version(ret, meth->version);
1761 }
1762
1763 return ret;
1764
Adam Langley95c29f32014-06-20 12:00:00 -07001765err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001766 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -07001767err2:
David Benjamin2755a3e2015-04-22 16:17:58 -04001768 SSL_CTX_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -08001769 return NULL;
1770}
Adam Langley95c29f32014-06-20 12:00:00 -07001771
David Benjamin4fcc2e22015-04-22 12:58:16 -04001772void SSL_CTX_free(SSL_CTX *ctx) {
1773 if (ctx == NULL ||
1774 CRYPTO_add(&ctx->references, -1, CRYPTO_LOCK_SSL_CTX) > 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001775 return;
1776 }
Adam Langley95c29f32014-06-20 12:00:00 -07001777
David Benjamin2755a3e2015-04-22 16:17:58 -04001778 X509_VERIFY_PARAM_free(ctx->param);
Adam Langley95c29f32014-06-20 12:00:00 -07001779
Adam Langleyfcf25832014-12-18 17:42:32 -08001780 /* Free internal session cache. However: the remove_cb() may reference the
1781 * ex_data of SSL_CTX, thus the ex_data store can only be removed after the
1782 * sessions were flushed. As the ex_data handling routines might also touch
1783 * the session cache, the most secure solution seems to be: empty (flush) the
1784 * cache, then free ex_data, then finally free the cache. (See ticket
1785 * [openssl.org #212].) */
David Benjamin2755a3e2015-04-22 16:17:58 -04001786 SSL_CTX_flush_sessions(ctx, 0);
Adam Langley95c29f32014-06-20 12:00:00 -07001787
David Benjamin4fcc2e22015-04-22 12:58:16 -04001788 CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, ctx, &ctx->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001789
David Benjamin2755a3e2015-04-22 16:17:58 -04001790 lh_SSL_SESSION_free(ctx->sessions);
1791 X509_STORE_free(ctx->cert_store);
1792 ssl_cipher_preference_list_free(ctx->cipher_list);
1793 sk_SSL_CIPHER_free(ctx->cipher_list_by_id);
1794 ssl_cipher_preference_list_free(ctx->cipher_list_tls11);
1795 ssl_cert_free(ctx->cert);
1796 sk_X509_NAME_pop_free(ctx->client_CA, X509_NAME_free);
1797 sk_X509_pop_free(ctx->extra_certs, X509_free);
1798 sk_SRTP_PROTECTION_PROFILE_free(ctx->srtp_profiles);
1799 OPENSSL_free(ctx->psk_identity_hint);
1800 OPENSSL_free(ctx->tlsext_ecpointformatlist);
1801 OPENSSL_free(ctx->tlsext_ellipticcurvelist);
1802 OPENSSL_free(ctx->alpn_client_proto_list);
1803 EVP_PKEY_free(ctx->tlsext_channel_id_private);
1804 BIO_free(ctx->keylog_bio);
Adam Langley95c29f32014-06-20 12:00:00 -07001805
David Benjamin4fcc2e22015-04-22 12:58:16 -04001806 OPENSSL_free(ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08001807}
Adam Langley95c29f32014-06-20 12:00:00 -07001808
Adam Langleyfcf25832014-12-18 17:42:32 -08001809void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) {
1810 ctx->default_passwd_callback = cb;
1811}
Adam Langley95c29f32014-06-20 12:00:00 -07001812
Adam Langleyfcf25832014-12-18 17:42:32 -08001813void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u) {
1814 ctx->default_passwd_callback_userdata = u;
1815}
Adam Langley95c29f32014-06-20 12:00:00 -07001816
Adam Langleyfcf25832014-12-18 17:42:32 -08001817void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
1818 int (*cb)(X509_STORE_CTX *, void *),
1819 void *arg) {
1820 ctx->app_verify_callback = cb;
1821 ctx->app_verify_arg = arg;
1822}
Adam Langley95c29f32014-06-20 12:00:00 -07001823
Adam Langleyfcf25832014-12-18 17:42:32 -08001824void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
1825 int (*cb)(int, X509_STORE_CTX *)) {
1826 ctx->verify_mode = mode;
1827 ctx->default_verify_callback = cb;
1828}
Adam Langley95c29f32014-06-20 12:00:00 -07001829
Adam Langleyfcf25832014-12-18 17:42:32 -08001830void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
1831 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
1832}
Adam Langley1258b6a2014-06-20 12:00:00 -07001833
Adam Langleyfcf25832014-12-18 17:42:32 -08001834void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb)(SSL *ssl, void *arg),
1835 void *arg) {
1836 ssl_cert_set_cert_cb(c->cert, cb, arg);
1837}
David Benjamin859ec3c2014-09-02 16:29:36 -04001838
Adam Langleyfcf25832014-12-18 17:42:32 -08001839void SSL_set_cert_cb(SSL *s, int (*cb)(SSL *ssl, void *arg), void *arg) {
1840 ssl_cert_set_cert_cb(s->cert, cb, arg);
1841}
Adam Langley95c29f32014-06-20 12:00:00 -07001842
Adam Langleyfcf25832014-12-18 17:42:32 -08001843static int ssl_has_key(SSL *s, size_t idx) {
1844 CERT_PKEY *cpk = &s->cert->pkeys[idx];
1845 return cpk->x509 && cpk->privatekey;
1846}
David Benjamin033e5f42014-11-13 18:47:41 -05001847
David Benjamin107db582015-04-08 00:41:59 -04001848void ssl_get_compatible_server_ciphers(SSL *s, uint32_t *out_mask_k,
1849 uint32_t *out_mask_a) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001850 CERT *c = s->cert;
1851 int rsa_enc, rsa_sign, dh_tmp;
David Benjamin107db582015-04-08 00:41:59 -04001852 uint32_t mask_k, mask_a;
Adam Langleyfcf25832014-12-18 17:42:32 -08001853 int have_ecc_cert, ecdsa_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001854 X509 *x;
Adam Langley95c29f32014-06-20 12:00:00 -07001855
Adam Langleyfcf25832014-12-18 17:42:32 -08001856 if (c == NULL) {
1857 /* TODO(davidben): Is this codepath possible? */
1858 *out_mask_k = 0;
1859 *out_mask_a = 0;
1860 return;
1861 }
Adam Langley95c29f32014-06-20 12:00:00 -07001862
Adam Langleyfcf25832014-12-18 17:42:32 -08001863 dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001864
Adam Langleyfcf25832014-12-18 17:42:32 -08001865 rsa_enc = ssl_has_key(s, SSL_PKEY_RSA_ENC);
1866 rsa_sign = ssl_has_key(s, SSL_PKEY_RSA_SIGN);
1867 have_ecc_cert = ssl_has_key(s, SSL_PKEY_ECC);
1868 mask_k = 0;
1869 mask_a = 0;
David Benjaminf31e6812014-11-13 18:05:55 -05001870
Adam Langleyfcf25832014-12-18 17:42:32 -08001871 if (rsa_enc) {
1872 mask_k |= SSL_kRSA;
1873 }
1874 if (dh_tmp) {
David Benjamin7061e282015-03-19 11:10:48 -04001875 mask_k |= SSL_kDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001876 }
1877 if (rsa_enc || rsa_sign) {
1878 mask_a |= SSL_aRSA;
1879 }
Adam Langley95c29f32014-06-20 12:00:00 -07001880
Adam Langleyfcf25832014-12-18 17:42:32 -08001881 /* An ECC certificate may be usable for ECDSA cipher suites depending on the
1882 * key usage extension and on the client's curve preferences. */
1883 if (have_ecc_cert) {
1884 x = c->pkeys[SSL_PKEY_ECC].x509;
1885 /* This call populates extension flags (ex_flags). */
1886 X509_check_purpose(x, -1, 0);
1887 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE)
1888 ? (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)
1889 : 1;
1890 if (!tls1_check_ec_cert(s, x)) {
1891 ecdsa_ok = 0;
1892 }
1893 if (ecdsa_ok) {
1894 mask_a |= SSL_aECDSA;
1895 }
1896 }
Adam Langley95c29f32014-06-20 12:00:00 -07001897
Adam Langleyfcf25832014-12-18 17:42:32 -08001898 /* If we are considering an ECC cipher suite that uses an ephemeral EC
1899 * key, check it. */
David Benjamindd978782015-04-24 15:20:13 -04001900 if (tls1_check_ec_tmp_key(s)) {
David Benjamin7061e282015-03-19 11:10:48 -04001901 mask_k |= SSL_kECDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001902 }
Adam Langley95c29f32014-06-20 12:00:00 -07001903
Adam Langleyfcf25832014-12-18 17:42:32 -08001904 /* PSK requires a server callback. */
1905 if (s->psk_server_callback != NULL) {
1906 mask_k |= SSL_kPSK;
1907 mask_a |= SSL_aPSK;
1908 }
Adam Langley95c29f32014-06-20 12:00:00 -07001909
Adam Langleyfcf25832014-12-18 17:42:32 -08001910 *out_mask_k = mask_k;
1911 *out_mask_a = mask_a;
1912}
Adam Langley95c29f32014-06-20 12:00:00 -07001913
1914/* This handy macro borrowed from crypto/x509v3/v3_purp.c */
1915#define ku_reject(x, usage) \
Adam Langleyfcf25832014-12-18 17:42:32 -08001916 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
Adam Langley95c29f32014-06-20 12:00:00 -07001917
Adam Langleyfcf25832014-12-18 17:42:32 -08001918int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001919 const SSL_CIPHER *cs = s->s3->tmp.new_cipher;
David Benjamin3fa27772015-04-17 22:32:19 -04001920 uint32_t alg_a = cs->algorithm_auth;
1921 int signature_nid = 0, md_nid = 0, pk_nid = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001922
Adam Langleyfcf25832014-12-18 17:42:32 -08001923 /* This call populates the ex_flags field correctly */
1924 X509_check_purpose(x, -1, 0);
1925 if (x->sig_alg && x->sig_alg->algorithm) {
1926 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
1927 OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
1928 }
1929 if (alg_a & SSL_aECDSA) {
1930 /* key usage, if present, must allow signing */
1931 if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE)) {
1932 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg,
1933 SSL_R_ECC_CERT_NOT_FOR_SIGNING);
1934 return 0;
1935 }
1936 }
Adam Langley95c29f32014-06-20 12:00:00 -07001937
Adam Langleyfcf25832014-12-18 17:42:32 -08001938 return 1; /* all checks are ok */
1939}
Adam Langley95c29f32014-06-20 12:00:00 -07001940
Adam Langleyfcf25832014-12-18 17:42:32 -08001941static int ssl_get_server_cert_index(const SSL *s) {
1942 int idx;
1943 idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1944 if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509) {
1945 idx = SSL_PKEY_RSA_SIGN;
1946 }
1947 if (idx == -1) {
1948 OPENSSL_PUT_ERROR(SSL, ssl_get_server_cert_index, ERR_R_INTERNAL_ERROR);
1949 }
1950 return idx;
1951}
Adam Langley95c29f32014-06-20 12:00:00 -07001952
Adam Langleyfcf25832014-12-18 17:42:32 -08001953CERT_PKEY *ssl_get_server_send_pkey(const SSL *s) {
1954 int i = ssl_get_server_cert_index(s);
Adam Langley95c29f32014-06-20 12:00:00 -07001955
Adam Langleyfcf25832014-12-18 17:42:32 -08001956 /* This may or may not be an error. */
1957 if (i < 0) {
1958 return NULL;
1959 }
Adam Langley95c29f32014-06-20 12:00:00 -07001960
Adam Langleyfcf25832014-12-18 17:42:32 -08001961 /* May be NULL. */
1962 return &s->cert->pkeys[i];
1963}
Adam Langley95c29f32014-06-20 12:00:00 -07001964
Adam Langleyfcf25832014-12-18 17:42:32 -08001965EVP_PKEY *ssl_get_sign_pkey(SSL *s, const SSL_CIPHER *cipher) {
David Benjamin3fa27772015-04-17 22:32:19 -04001966 uint32_t alg_a = cipher->algorithm_auth;
1967 CERT *c = s->cert;
Adam Langleyfcf25832014-12-18 17:42:32 -08001968 int idx = -1;
Adam Langley95c29f32014-06-20 12:00:00 -07001969
Adam Langleyfcf25832014-12-18 17:42:32 -08001970 if (alg_a & SSL_aRSA) {
1971 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL) {
1972 idx = SSL_PKEY_RSA_SIGN;
1973 } else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL) {
1974 idx = SSL_PKEY_RSA_ENC;
1975 }
1976 } else if ((alg_a & SSL_aECDSA) &&
1977 (c->pkeys[SSL_PKEY_ECC].privatekey != NULL)) {
1978 idx = SSL_PKEY_ECC;
1979 }
Adam Langley95c29f32014-06-20 12:00:00 -07001980
Adam Langleyfcf25832014-12-18 17:42:32 -08001981 if (idx == -1) {
1982 OPENSSL_PUT_ERROR(SSL, ssl_get_sign_pkey, ERR_R_INTERNAL_ERROR);
1983 return NULL;
1984 }
Adam Langley95c29f32014-06-20 12:00:00 -07001985
Adam Langleyfcf25832014-12-18 17:42:32 -08001986 return c->pkeys[idx].privatekey;
1987}
Adam Langley95c29f32014-06-20 12:00:00 -07001988
Adam Langleyfcf25832014-12-18 17:42:32 -08001989void ssl_update_cache(SSL *s, int mode) {
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001990 /* Never cache sessions with empty session IDs. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001991 if (s->session->session_id_length == 0) {
1992 return;
1993 }
Adam Langley95c29f32014-06-20 12:00:00 -07001994
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001995 SSL_CTX *ctx = s->initial_ctx;
1996 if ((ctx->session_cache_mode & mode) == mode && !s->hit &&
1997 ((ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) ||
1998 SSL_CTX_add_session(ctx, s->session)) &&
1999 ctx->new_session_cb != NULL) {
2000 /* Note: |new_session_cb| is called whether the internal session cache is
2001 * used or not. */
2002 if (!ctx->new_session_cb(s, SSL_SESSION_up_ref(s->session))) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002003 SSL_SESSION_free(s->session);
2004 }
2005 }
Adam Langley95c29f32014-06-20 12:00:00 -07002006
David Benjaminb6d0c6d2015-03-19 19:07:26 -04002007 if (!(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR) &&
2008 !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) &&
2009 (ctx->session_cache_mode & mode) == mode) {
2010 /* Automatically flush the internal session cache every 255 connections. */
2011 int flush_cache = 0;
2012 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2013 ctx->handshakes_since_cache_flush++;
2014 if (ctx->handshakes_since_cache_flush >= 255) {
2015 flush_cache = 1;
2016 ctx->handshakes_since_cache_flush = 0;
2017 }
2018 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
2019
2020 if (flush_cache) {
2021 SSL_CTX_flush_sessions(ctx, (unsigned long)time(NULL));
Adam Langleyfcf25832014-12-18 17:42:32 -08002022 }
2023 }
2024}
Adam Langley95c29f32014-06-20 12:00:00 -07002025
David Benjamin1a5c50f2015-03-11 16:22:37 -04002026int SSL_get_error(const SSL *s, int ret_code) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002027 int reason;
David Benjamin1a5c50f2015-03-11 16:22:37 -04002028 uint32_t err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002029 BIO *bio;
Adam Langley95c29f32014-06-20 12:00:00 -07002030
David Benjamin1a5c50f2015-03-11 16:22:37 -04002031 if (ret_code > 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002032 return SSL_ERROR_NONE;
2033 }
Adam Langley95c29f32014-06-20 12:00:00 -07002034
Adam Langleyfcf25832014-12-18 17:42:32 -08002035 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
2036 * where we do encode the error */
David Benjamin1a5c50f2015-03-11 16:22:37 -04002037 err = ERR_peek_error();
2038 if (err != 0) {
2039 if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002040 return SSL_ERROR_SYSCALL;
2041 }
2042 return SSL_ERROR_SSL;
2043 }
Adam Langley95c29f32014-06-20 12:00:00 -07002044
David Benjamin1a5c50f2015-03-11 16:22:37 -04002045 if (ret_code == 0) {
David Benjamin9a38e922015-01-22 16:06:11 -05002046 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2047 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) {
2048 /* The socket was cleanly shut down with a close_notify. */
2049 return SSL_ERROR_ZERO_RETURN;
2050 }
2051 /* An EOF was observed which violates the protocol, and the underlying
2052 * transport does not participate in the error queue. Bubble up to the
2053 * caller. */
2054 return SSL_ERROR_SYSCALL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002055 }
Adam Langleyb2ce0582014-06-20 12:00:00 -07002056
Adam Langleyfcf25832014-12-18 17:42:32 -08002057 if (SSL_want_session(s)) {
2058 return SSL_ERROR_PENDING_SESSION;
2059 }
Adam Langley95c29f32014-06-20 12:00:00 -07002060
Adam Langleyfcf25832014-12-18 17:42:32 -08002061 if (SSL_want_certificate(s)) {
2062 return SSL_ERROR_PENDING_CERTIFICATE;
2063 }
Adam Langley95c29f32014-06-20 12:00:00 -07002064
Adam Langleyfcf25832014-12-18 17:42:32 -08002065 if (SSL_want_read(s)) {
2066 bio = SSL_get_rbio(s);
2067 if (BIO_should_read(bio)) {
2068 return SSL_ERROR_WANT_READ;
2069 }
Adam Langley95c29f32014-06-20 12:00:00 -07002070
Adam Langleyfcf25832014-12-18 17:42:32 -08002071 if (BIO_should_write(bio)) {
2072 /* This one doesn't make too much sense ... We never try to write to the
2073 * rbio, and an application program where rbio and wbio are separate
2074 * couldn't even know what it should wait for. However if we ever set
2075 * s->rwstate incorrectly (so that we have SSL_want_read(s) instead of
2076 * SSL_want_write(s)) and rbio and wbio *are* the same, this test works
2077 * around that bug; so it might be safer to keep it. */
2078 return SSL_ERROR_WANT_WRITE;
2079 }
Adam Langley95c29f32014-06-20 12:00:00 -07002080
Adam Langleyfcf25832014-12-18 17:42:32 -08002081 if (BIO_should_io_special(bio)) {
2082 reason = BIO_get_retry_reason(bio);
2083 if (reason == BIO_RR_CONNECT) {
2084 return SSL_ERROR_WANT_CONNECT;
2085 }
Adam Langley95c29f32014-06-20 12:00:00 -07002086
Adam Langleyfcf25832014-12-18 17:42:32 -08002087 if (reason == BIO_RR_ACCEPT) {
2088 return SSL_ERROR_WANT_ACCEPT;
2089 }
Adam Langley95c29f32014-06-20 12:00:00 -07002090
Adam Langleyfcf25832014-12-18 17:42:32 -08002091 return SSL_ERROR_SYSCALL; /* unknown */
2092 }
2093 }
Adam Langley95c29f32014-06-20 12:00:00 -07002094
Adam Langleyfcf25832014-12-18 17:42:32 -08002095 if (SSL_want_write(s)) {
2096 bio = SSL_get_wbio(s);
2097 if (BIO_should_write(bio)) {
2098 return SSL_ERROR_WANT_WRITE;
2099 }
Adam Langley95c29f32014-06-20 12:00:00 -07002100
Adam Langleyfcf25832014-12-18 17:42:32 -08002101 if (BIO_should_read(bio)) {
2102 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2103 return SSL_ERROR_WANT_READ;
2104 }
Adam Langley95c29f32014-06-20 12:00:00 -07002105
Adam Langleyfcf25832014-12-18 17:42:32 -08002106 if (BIO_should_io_special(bio)) {
2107 reason = BIO_get_retry_reason(bio);
2108 if (reason == BIO_RR_CONNECT) {
2109 return SSL_ERROR_WANT_CONNECT;
2110 }
Adam Langley95c29f32014-06-20 12:00:00 -07002111
Adam Langleyfcf25832014-12-18 17:42:32 -08002112 if (reason == BIO_RR_ACCEPT) {
2113 return SSL_ERROR_WANT_ACCEPT;
2114 }
Adam Langley95c29f32014-06-20 12:00:00 -07002115
Adam Langleyfcf25832014-12-18 17:42:32 -08002116 return SSL_ERROR_SYSCALL;
2117 }
2118 }
Adam Langley95c29f32014-06-20 12:00:00 -07002119
Adam Langleyfcf25832014-12-18 17:42:32 -08002120 if (SSL_want_x509_lookup(s)) {
2121 return SSL_ERROR_WANT_X509_LOOKUP;
2122 }
Adam Langley95c29f32014-06-20 12:00:00 -07002123
Adam Langleyfcf25832014-12-18 17:42:32 -08002124 if (SSL_want_channel_id_lookup(s)) {
2125 return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
2126 }
Adam Langley0f4746e2014-08-13 12:26:32 -07002127
Adam Langleyfcf25832014-12-18 17:42:32 -08002128 return SSL_ERROR_SYSCALL;
2129}
Adam Langley0f4746e2014-08-13 12:26:32 -07002130
Adam Langleyfcf25832014-12-18 17:42:32 -08002131int SSL_do_handshake(SSL *s) {
2132 int ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002133
Adam Langleyfcf25832014-12-18 17:42:32 -08002134 if (s->handshake_func == NULL) {
2135 OPENSSL_PUT_ERROR(SSL, SSL_do_handshake, SSL_R_CONNECTION_TYPE_NOT_SET);
2136 return -1;
2137 }
Adam Langley95c29f32014-06-20 12:00:00 -07002138
Adam Langleyfcf25832014-12-18 17:42:32 -08002139 s->method->ssl_renegotiate_check(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002140
Adam Langleyfcf25832014-12-18 17:42:32 -08002141 if (SSL_in_init(s)) {
2142 ret = s->handshake_func(s);
2143 }
2144 return ret;
2145}
Adam Langley95c29f32014-06-20 12:00:00 -07002146
Adam Langleyfcf25832014-12-18 17:42:32 -08002147void SSL_set_accept_state(SSL *s) {
2148 s->server = 1;
2149 s->shutdown = 0;
2150 s->state = SSL_ST_ACCEPT | SSL_ST_BEFORE;
2151 s->handshake_func = s->method->ssl_accept;
2152 /* clear the current cipher */
2153 ssl_clear_cipher_ctx(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002154}
Adam Langley95c29f32014-06-20 12:00:00 -07002155
Adam Langleyfcf25832014-12-18 17:42:32 -08002156void SSL_set_connect_state(SSL *s) {
2157 s->server = 0;
2158 s->shutdown = 0;
2159 s->state = SSL_ST_CONNECT | SSL_ST_BEFORE;
2160 s->handshake_func = s->method->ssl_connect;
2161 /* clear the current cipher */
2162 ssl_clear_cipher_ctx(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002163}
Adam Langley95c29f32014-06-20 12:00:00 -07002164
Adam Langleyfcf25832014-12-18 17:42:32 -08002165int ssl_undefined_function(SSL *s) {
2166 OPENSSL_PUT_ERROR(SSL, ssl_undefined_function,
2167 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2168 return 0;
2169}
Adam Langley95c29f32014-06-20 12:00:00 -07002170
Adam Langleyfcf25832014-12-18 17:42:32 -08002171int ssl_undefined_void_function(void) {
2172 OPENSSL_PUT_ERROR(SSL, ssl_undefined_void_function,
2173 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2174 return 0;
2175}
Adam Langley95c29f32014-06-20 12:00:00 -07002176
Adam Langleyfcf25832014-12-18 17:42:32 -08002177int ssl_undefined_const_function(const SSL *s) {
2178 OPENSSL_PUT_ERROR(SSL, ssl_undefined_const_function,
2179 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2180 return 0;
2181}
Adam Langley95c29f32014-06-20 12:00:00 -07002182
Adam Langleyfcf25832014-12-18 17:42:32 -08002183static const char *ssl_get_version(int version) {
2184 switch (version) {
2185 case TLS1_2_VERSION:
2186 return "TLSv1.2";
Adam Langley95c29f32014-06-20 12:00:00 -07002187
Adam Langleyfcf25832014-12-18 17:42:32 -08002188 case TLS1_1_VERSION:
2189 return "TLSv1.1";
Adam Langley95c29f32014-06-20 12:00:00 -07002190
Adam Langleyfcf25832014-12-18 17:42:32 -08002191 case TLS1_VERSION:
2192 return "TLSv1";
Adam Langley95c29f32014-06-20 12:00:00 -07002193
Adam Langleyfcf25832014-12-18 17:42:32 -08002194 case SSL3_VERSION:
2195 return "SSLv3";
Adam Langley95c29f32014-06-20 12:00:00 -07002196
David Benjamin1c722b72015-04-20 13:53:10 -04002197 case DTLS1_VERSION:
2198 return "DTLSv1";
2199
2200 case DTLS1_2_VERSION:
2201 return "DTLSv1.2";
2202
Adam Langleyfcf25832014-12-18 17:42:32 -08002203 default:
2204 return "unknown";
2205 }
2206}
Adam Langley95c29f32014-06-20 12:00:00 -07002207
Adam Langleyfcf25832014-12-18 17:42:32 -08002208const char *SSL_get_version(const SSL *s) {
2209 return ssl_get_version(s->version);
2210}
Adam Langley95c29f32014-06-20 12:00:00 -07002211
Adam Langleyfcf25832014-12-18 17:42:32 -08002212const char *SSL_SESSION_get_version(const SSL_SESSION *sess) {
2213 return ssl_get_version(sess->ssl_version);
2214}
Adam Langley95c29f32014-06-20 12:00:00 -07002215
Adam Langleyfcf25832014-12-18 17:42:32 -08002216void ssl_clear_cipher_ctx(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002217 if (s->aead_read_ctx != NULL) {
2218 EVP_AEAD_CTX_cleanup(&s->aead_read_ctx->ctx);
2219 OPENSSL_free(s->aead_read_ctx);
2220 s->aead_read_ctx = NULL;
2221 }
Adam Langleya5dc5452014-06-20 12:00:00 -07002222
Adam Langleyfcf25832014-12-18 17:42:32 -08002223 if (s->aead_write_ctx != NULL) {
2224 EVP_AEAD_CTX_cleanup(&s->aead_write_ctx->ctx);
2225 OPENSSL_free(s->aead_write_ctx);
2226 s->aead_write_ctx = NULL;
2227 }
2228}
Adam Langley95c29f32014-06-20 12:00:00 -07002229
Adam Langleyfcf25832014-12-18 17:42:32 -08002230X509 *SSL_get_certificate(const SSL *s) {
2231 if (s->cert != NULL) {
2232 return s->cert->key->x509;
2233 }
2234
2235 return NULL;
2236}
2237
2238EVP_PKEY *SSL_get_privatekey(const SSL *s) {
2239 if (s->cert != NULL) {
2240 return s->cert->key->privatekey;
2241 }
2242
2243 return NULL;
2244}
2245
2246X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
2247 if (ctx->cert != NULL) {
2248 return ctx->cert->key->x509;
2249 }
2250
2251 return NULL;
2252}
2253
2254EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
2255 if (ctx->cert != NULL) {
2256 return ctx->cert->key->privatekey;
2257 }
2258
2259 return NULL;
2260}
2261
2262const SSL_CIPHER *SSL_get_current_cipher(const SSL *s) {
2263 if (s->session != NULL && s->session->cipher != NULL) {
2264 return s->session->cipher;
2265 }
2266
2267 return NULL;
2268}
2269
2270const void *SSL_get_current_compression(SSL *s) { return NULL; }
2271
2272const void *SSL_get_current_expansion(SSL *s) { return NULL; }
2273
2274int ssl_init_wbio_buffer(SSL *s, int push) {
2275 BIO *bbio;
2276
2277 if (s->bbio == NULL) {
2278 bbio = BIO_new(BIO_f_buffer());
2279 if (bbio == NULL) {
2280 return 0;
2281 }
2282 s->bbio = bbio;
2283 } else {
2284 bbio = s->bbio;
2285 if (s->bbio == s->wbio) {
2286 s->wbio = BIO_pop(s->wbio);
2287 }
2288 }
2289
2290 BIO_reset(bbio);
2291 if (!BIO_set_read_buffer_size(bbio, 1)) {
2292 OPENSSL_PUT_ERROR(SSL, ssl_init_wbio_buffer, ERR_R_BUF_LIB);
2293 return 0;
2294 }
2295
2296 if (push) {
2297 if (s->wbio != bbio) {
2298 s->wbio = BIO_push(bbio, s->wbio);
2299 }
2300 } else {
2301 if (s->wbio == bbio) {
2302 s->wbio = BIO_pop(bbio);
2303 }
2304 }
2305
2306 return 1;
2307}
2308
2309void ssl_free_wbio_buffer(SSL *s) {
2310 if (s->bbio == NULL) {
2311 return;
2312 }
2313
2314 if (s->bbio == s->wbio) {
2315 /* remove buffering */
2316 s->wbio = BIO_pop(s->wbio);
2317 }
2318
2319 BIO_free(s->bbio);
2320 s->bbio = NULL;
2321}
2322
2323void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
2324 ctx->quiet_shutdown = mode;
2325}
2326
2327int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
2328 return ctx->quiet_shutdown;
2329}
2330
2331void SSL_set_quiet_shutdown(SSL *s, int mode) { s->quiet_shutdown = mode; }
2332
2333int SSL_get_quiet_shutdown(const SSL *s) { return s->quiet_shutdown; }
2334
2335void SSL_set_shutdown(SSL *s, int mode) { s->shutdown = mode; }
2336
2337int SSL_get_shutdown(const SSL *s) { return s->shutdown; }
2338
2339int SSL_version(const SSL *s) { return s->version; }
2340
2341SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
2342
2343SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
2344 if (ssl->ctx == ctx) {
2345 return ssl->ctx;
2346 }
2347
2348 if (ctx == NULL) {
2349 ctx = ssl->initial_ctx;
2350 }
2351
David Benjamin2755a3e2015-04-22 16:17:58 -04002352 ssl_cert_free(ssl->cert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002353 ssl->cert = ssl_cert_dup(ctx->cert);
David Benjamin2755a3e2015-04-22 16:17:58 -04002354
Adam Langleyfcf25832014-12-18 17:42:32 -08002355 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
David Benjamin2755a3e2015-04-22 16:17:58 -04002356 SSL_CTX_free(ssl->ctx); /* decrement reference count */
Adam Langleyfcf25832014-12-18 17:42:32 -08002357 ssl->ctx = ctx;
2358
2359 ssl->sid_ctx_length = ctx->sid_ctx_length;
2360 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2361 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2362
2363 return ssl->ctx;
2364}
2365
2366int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
2367 return X509_STORE_set_default_paths(ctx->cert_store);
2368}
Adam Langley95c29f32014-06-20 12:00:00 -07002369
2370int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
Adam Langleyfcf25832014-12-18 17:42:32 -08002371 const char *CApath) {
2372 return X509_STORE_load_locations(ctx->cert_store, CAfile, CApath);
2373}
Adam Langley95c29f32014-06-20 12:00:00 -07002374
2375void SSL_set_info_callback(SSL *ssl,
Adam Langleyfcf25832014-12-18 17:42:32 -08002376 void (*cb)(const SSL *ssl, int type, int val)) {
2377 ssl->info_callback = cb;
2378}
Adam Langley95c29f32014-06-20 12:00:00 -07002379
Adam Langleyfcf25832014-12-18 17:42:32 -08002380void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/, int /*type*/,
2381 int /*val*/) {
2382 return ssl->info_callback;
2383}
Adam Langley95c29f32014-06-20 12:00:00 -07002384
Adam Langleyfcf25832014-12-18 17:42:32 -08002385int SSL_state(const SSL *ssl) { return ssl->state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002386
Adam Langleyfcf25832014-12-18 17:42:32 -08002387void SSL_set_state(SSL *ssl, int state) { ssl->state = state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002388
Adam Langleyfcf25832014-12-18 17:42:32 -08002389void SSL_set_verify_result(SSL *ssl, long arg) { ssl->verify_result = arg; }
Adam Langley95c29f32014-06-20 12:00:00 -07002390
Adam Langleyfcf25832014-12-18 17:42:32 -08002391long SSL_get_verify_result(const SSL *ssl) { return ssl->verify_result; }
Adam Langley95c29f32014-06-20 12:00:00 -07002392
Adam Langleyfcf25832014-12-18 17:42:32 -08002393int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2394 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002395 int index;
2396 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp,
2397 new_func, dup_func, free_func)) {
2398 return -1;
2399 }
2400 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002401}
Adam Langley95c29f32014-06-20 12:00:00 -07002402
Adam Langleyfcf25832014-12-18 17:42:32 -08002403int SSL_set_ex_data(SSL *s, int idx, void *arg) {
2404 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2405}
Adam Langley95c29f32014-06-20 12:00:00 -07002406
Adam Langleyfcf25832014-12-18 17:42:32 -08002407void *SSL_get_ex_data(const SSL *s, int idx) {
2408 return CRYPTO_get_ex_data(&s->ex_data, idx);
2409}
Adam Langley95c29f32014-06-20 12:00:00 -07002410
Adam Langleyfcf25832014-12-18 17:42:32 -08002411int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2412 CRYPTO_EX_dup *dup_func,
2413 CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002414 int index;
2415 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp,
2416 new_func, dup_func, free_func)) {
2417 return -1;
2418 }
2419 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002420}
Adam Langley95c29f32014-06-20 12:00:00 -07002421
Adam Langleyfcf25832014-12-18 17:42:32 -08002422int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg) {
2423 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2424}
Adam Langley95c29f32014-06-20 12:00:00 -07002425
Adam Langleyfcf25832014-12-18 17:42:32 -08002426void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx) {
2427 return CRYPTO_get_ex_data(&s->ex_data, idx);
2428}
Adam Langley95c29f32014-06-20 12:00:00 -07002429
Adam Langleyfcf25832014-12-18 17:42:32 -08002430int ssl_ok(SSL *s) { return 1; }
Adam Langley95c29f32014-06-20 12:00:00 -07002431
Adam Langleyfcf25832014-12-18 17:42:32 -08002432X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
2433 return ctx->cert_store;
2434}
Adam Langley95c29f32014-06-20 12:00:00 -07002435
Adam Langleyfcf25832014-12-18 17:42:32 -08002436void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
David Benjamin2755a3e2015-04-22 16:17:58 -04002437 X509_STORE_free(ctx->cert_store);
Adam Langleyfcf25832014-12-18 17:42:32 -08002438 ctx->cert_store = store;
2439}
Adam Langley95c29f32014-06-20 12:00:00 -07002440
Adam Langleyfcf25832014-12-18 17:42:32 -08002441int SSL_want(const SSL *s) { return s->rwstate; }
Adam Langley95c29f32014-06-20 12:00:00 -07002442
Adam Langleyfcf25832014-12-18 17:42:32 -08002443void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
2444 RSA *(*cb)(SSL *ssl, int is_export,
2445 int keylength)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002446}
Adam Langley95c29f32014-06-20 12:00:00 -07002447
Adam Langleyfcf25832014-12-18 17:42:32 -08002448void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
2449 int keylength)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002450}
2451
2452void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
David Benjamin59015c32015-04-26 13:13:55 -04002453 DH *(*callback)(SSL *ssl, int is_export,
2454 int keylength)) {
2455 ctx->cert->dh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002456}
2457
David Benjamin59015c32015-04-26 13:13:55 -04002458void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*callback)(SSL *ssl, int is_export,
2459 int keylength)) {
2460 ssl->cert->dh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002461}
2462
2463void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,
David Benjamindd978782015-04-24 15:20:13 -04002464 EC_KEY *(*callback)(SSL *ssl, int is_export,
2465 int keylength)) {
David Benjamin59015c32015-04-26 13:13:55 -04002466 ctx->cert->ecdh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002467}
2468
2469void SSL_set_tmp_ecdh_callback(SSL *ssl,
David Benjamindd978782015-04-24 15:20:13 -04002470 EC_KEY *(*callback)(SSL *ssl, int is_export,
2471 int keylength)) {
David Benjamin59015c32015-04-26 13:13:55 -04002472 ssl->cert->ecdh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002473}
2474
2475int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
2476 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2477 OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_psk_identity_hint,
2478 SSL_R_DATA_LENGTH_TOO_LONG);
2479 return 0;
2480 }
2481
David Benjamin2755a3e2015-04-22 16:17:58 -04002482 OPENSSL_free(ctx->psk_identity_hint);
Adam Langleyfcf25832014-12-18 17:42:32 -08002483
2484 if (identity_hint != NULL) {
2485 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2486 if (ctx->psk_identity_hint == NULL) {
2487 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002488 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002489 } else {
2490 ctx->psk_identity_hint = NULL;
2491 }
Adam Langley95c29f32014-06-20 12:00:00 -07002492
Adam Langleyfcf25832014-12-18 17:42:32 -08002493 return 1;
2494}
2495
2496int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint) {
2497 if (s == NULL) {
2498 return 0;
2499 }
2500
2501 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2502 OPENSSL_PUT_ERROR(SSL, SSL_use_psk_identity_hint,
2503 SSL_R_DATA_LENGTH_TOO_LONG);
2504 return 0;
2505 }
2506
2507 /* Clear currently configured hint, if any. */
David Benjamin2755a3e2015-04-22 16:17:58 -04002508 OPENSSL_free(s->psk_identity_hint);
2509 s->psk_identity_hint = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002510
2511 if (identity_hint != NULL) {
2512 s->psk_identity_hint = BUF_strdup(identity_hint);
2513 if (s->psk_identity_hint == NULL) {
2514 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002515 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002516 }
Adam Langley95c29f32014-06-20 12:00:00 -07002517
Adam Langleyfcf25832014-12-18 17:42:32 -08002518 return 1;
2519}
Adam Langley95c29f32014-06-20 12:00:00 -07002520
Adam Langleyfcf25832014-12-18 17:42:32 -08002521const char *SSL_get_psk_identity_hint(const SSL *s) {
2522 if (s == NULL) {
2523 return NULL;
2524 }
2525 return s->psk_identity_hint;
2526}
Adam Langley95c29f32014-06-20 12:00:00 -07002527
Adam Langleyfcf25832014-12-18 17:42:32 -08002528const char *SSL_get_psk_identity(const SSL *s) {
2529 if (s == NULL || s->session == NULL) {
2530 return NULL;
2531 }
Adam Langley95c29f32014-06-20 12:00:00 -07002532
Adam Langleyfcf25832014-12-18 17:42:32 -08002533 return s->session->psk_identity;
2534}
Adam Langley95c29f32014-06-20 12:00:00 -07002535
Adam Langleyfcf25832014-12-18 17:42:32 -08002536void SSL_set_psk_client_callback(
2537 SSL *s, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2538 unsigned int max_identity_len, uint8_t *psk,
2539 unsigned int max_psk_len)) {
2540 s->psk_client_callback = cb;
2541}
Adam Langley95c29f32014-06-20 12:00:00 -07002542
Adam Langleyfcf25832014-12-18 17:42:32 -08002543void SSL_CTX_set_psk_client_callback(
2544 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2545 unsigned int max_identity_len,
2546 uint8_t *psk, unsigned int max_psk_len)) {
2547 ctx->psk_client_callback = cb;
2548}
Adam Langley95c29f32014-06-20 12:00:00 -07002549
Adam Langleyfcf25832014-12-18 17:42:32 -08002550void SSL_set_psk_server_callback(
2551 SSL *s, unsigned int (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
2552 unsigned int max_psk_len)) {
2553 s->psk_server_callback = cb;
2554}
Adam Langley95c29f32014-06-20 12:00:00 -07002555
Adam Langleyfcf25832014-12-18 17:42:32 -08002556void SSL_CTX_set_psk_server_callback(
2557 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *identity,
2558 uint8_t *psk, unsigned int max_psk_len)) {
2559 ctx->psk_server_callback = cb;
2560}
Adam Langley95c29f32014-06-20 12:00:00 -07002561
Adam Langleyfcf25832014-12-18 17:42:32 -08002562void SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) {
2563 ctx->min_version = version;
2564}
Adam Langley95c29f32014-06-20 12:00:00 -07002565
Adam Langleyfcf25832014-12-18 17:42:32 -08002566void SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) {
2567 ctx->max_version = version;
2568}
Adam Langley0289c732014-06-20 12:00:00 -07002569
Adam Langleyfcf25832014-12-18 17:42:32 -08002570void SSL_set_min_version(SSL *ssl, uint16_t version) {
2571 ssl->min_version = version;
2572}
Adam Langley0289c732014-06-20 12:00:00 -07002573
Adam Langleyfcf25832014-12-18 17:42:32 -08002574void SSL_set_max_version(SSL *ssl, uint16_t version) {
2575 ssl->max_version = version;
2576}
Adam Langley95c29f32014-06-20 12:00:00 -07002577
Adam Langleyfcf25832014-12-18 17:42:32 -08002578void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
2579 void (*cb)(int write_p, int version,
2580 int content_type, const void *buf,
2581 size_t len, SSL *ssl, void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002582 ctx->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002583}
David Benjamin61ecccf2015-05-05 09:44:51 -04002584
2585void SSL_CTX_set_msg_callback_arg(SSL_CTX *ctx, void *arg) {
2586 ctx->msg_callback_arg = arg;
2587}
2588
Adam Langleyfcf25832014-12-18 17:42:32 -08002589void SSL_set_msg_callback(SSL *ssl,
2590 void (*cb)(int write_p, int version, int content_type,
2591 const void *buf, size_t len, SSL *ssl,
2592 void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002593 ssl->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002594}
Adam Langley95c29f32014-06-20 12:00:00 -07002595
David Benjamin61ecccf2015-05-05 09:44:51 -04002596void SSL_set_msg_callback_arg(SSL *ssl, void *arg) {
2597 ssl->msg_callback_arg = arg;
2598}
2599
Adam Langleyfcf25832014-12-18 17:42:32 -08002600void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio) {
David Benjamin2755a3e2015-04-22 16:17:58 -04002601 BIO_free(ctx->keylog_bio);
Adam Langleyfcf25832014-12-18 17:42:32 -08002602 ctx->keylog_bio = keylog_bio;
2603}
Adam Langley95c29f32014-06-20 12:00:00 -07002604
Adam Langleyfcf25832014-12-18 17:42:32 -08002605static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
2606 static const char hextable[] = "0123456789abcdef";
2607 uint8_t *out;
2608 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002609
Adam Langleyfcf25832014-12-18 17:42:32 -08002610 if (!CBB_add_space(cbb, &out, in_len * 2)) {
2611 return 0;
2612 }
Adam Langley95c29f32014-06-20 12:00:00 -07002613
Adam Langleyfcf25832014-12-18 17:42:32 -08002614 for (i = 0; i < in_len; i++) {
2615 *(out++) = (uint8_t)hextable[in[i] >> 4];
2616 *(out++) = (uint8_t)hextable[in[i] & 0xf];
2617 }
Adam Langley95c29f32014-06-20 12:00:00 -07002618
Adam Langleyfcf25832014-12-18 17:42:32 -08002619 return 1;
2620}
David Benjamin859ec3c2014-09-02 16:29:36 -04002621
2622int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -08002623 const uint8_t *encrypted_premaster,
2624 size_t encrypted_premaster_len,
2625 const uint8_t *premaster,
2626 size_t premaster_len) {
2627 BIO *bio = ctx->keylog_bio;
2628 CBB cbb;
2629 uint8_t *out;
2630 size_t out_len;
2631 int ret;
David Benjamin859ec3c2014-09-02 16:29:36 -04002632
Adam Langleyfcf25832014-12-18 17:42:32 -08002633 if (bio == NULL) {
2634 return 1;
2635 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002636
Adam Langleyfcf25832014-12-18 17:42:32 -08002637 if (encrypted_premaster_len < 8) {
2638 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_rsa_client_key_exchange,
2639 ERR_R_INTERNAL_ERROR);
2640 return 0;
2641 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002642
Adam Langleyfcf25832014-12-18 17:42:32 -08002643 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len * 2 + 1)) {
2644 return 0;
2645 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002646
Adam Langleyfcf25832014-12-18 17:42:32 -08002647 if (!CBB_add_bytes(&cbb, (const uint8_t *)"RSA ", 4) ||
2648 /* Only the first 8 bytes of the encrypted premaster secret are
2649 * logged. */
2650 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
2651 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2652 !cbb_add_hex(&cbb, premaster, premaster_len) ||
2653 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2654 !CBB_finish(&cbb, &out, &out_len)) {
2655 CBB_cleanup(&cbb);
2656 return 0;
2657 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002658
Adam Langleyfcf25832014-12-18 17:42:32 -08002659 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2660 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2661 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
David Benjamin859ec3c2014-09-02 16:29:36 -04002662
Adam Langleyfcf25832014-12-18 17:42:32 -08002663 OPENSSL_free(out);
2664 return ret;
Adam Langley95f22882014-06-20 12:00:00 -07002665}
2666
Adam Langleyfcf25832014-12-18 17:42:32 -08002667int ssl_ctx_log_master_secret(SSL_CTX *ctx, const uint8_t *client_random,
2668 size_t client_random_len, const uint8_t *master,
2669 size_t master_len) {
2670 BIO *bio = ctx->keylog_bio;
2671 CBB cbb;
2672 uint8_t *out;
2673 size_t out_len;
2674 int ret;
Adam Langleyadb739e2014-06-20 12:00:00 -07002675
Adam Langleyfcf25832014-12-18 17:42:32 -08002676 if (bio == NULL) {
2677 return 1;
2678 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002679
Adam Langleyfcf25832014-12-18 17:42:32 -08002680 if (client_random_len != 32) {
2681 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_master_secret, ERR_R_INTERNAL_ERROR);
2682 return 0;
2683 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002684
Adam Langleyfcf25832014-12-18 17:42:32 -08002685 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len * 2 + 1)) {
2686 return 0;
2687 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002688
Adam Langleyfcf25832014-12-18 17:42:32 -08002689 if (!CBB_add_bytes(&cbb, (const uint8_t *)"CLIENT_RANDOM ", 14) ||
2690 !cbb_add_hex(&cbb, client_random, 32) ||
2691 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2692 !cbb_add_hex(&cbb, master, master_len) ||
2693 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2694 !CBB_finish(&cbb, &out, &out_len)) {
2695 CBB_cleanup(&cbb);
2696 return 0;
2697 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002698
Adam Langleyfcf25832014-12-18 17:42:32 -08002699 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2700 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2701 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
David Benjamine99e9122014-12-11 01:46:01 -05002702
Adam Langleyfcf25832014-12-18 17:42:32 -08002703 OPENSSL_free(out);
2704 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -07002705}
2706
David Benjamined7c4752015-02-16 19:16:46 -05002707int SSL_in_false_start(const SSL *s) {
2708 return s->s3->tmp.in_false_start;
2709}
2710
Adam Langleyfcf25832014-12-18 17:42:32 -08002711int SSL_cutthrough_complete(const SSL *s) {
David Benjamined7c4752015-02-16 19:16:46 -05002712 return SSL_in_false_start(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002713}
Adam Langley95c29f32014-06-20 12:00:00 -07002714
Adam Langleyfcf25832014-12-18 17:42:32 -08002715void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
2716 size_t *ssl_session_size) {
2717 *ssl_size = sizeof(SSL);
2718 *ssl_ctx_size = sizeof(SSL_CTX);
2719 *ssl_session_size = sizeof(SSL_SESSION);
2720}
Feng Lu41aa3252014-11-21 22:47:56 -08002721
David Benjamined7c4752015-02-16 19:16:46 -05002722int ssl3_can_false_start(const SSL *s) {
David Benjamin195dc782015-02-19 13:27:05 -05002723 const SSL_CIPHER *const cipher = SSL_get_current_cipher(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002724
Adam Langleye631d962015-04-01 13:11:01 -07002725 /* False Start only for TLS 1.2 with an ECDHE+AEAD cipher and ALPN or NPN. */
David Benjamin195dc782015-02-19 13:27:05 -05002726 return !SSL_IS_DTLS(s) &&
2727 SSL_version(s) >= TLS1_2_VERSION &&
2728 (s->s3->alpn_selected || s->s3->next_proto_neg_seen) &&
2729 cipher != NULL &&
Adam Langleye631d962015-04-01 13:11:01 -07002730 cipher->algorithm_mkey == SSL_kECDHE &&
David Benjamin195dc782015-02-19 13:27:05 -05002731 (cipher->algorithm_enc == SSL_AES128GCM ||
2732 cipher->algorithm_enc == SSL_AES256GCM ||
2733 cipher->algorithm_enc == SSL_CHACHA20POLY1305);
Adam Langleyfcf25832014-12-18 17:42:32 -08002734}
2735
2736const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version) {
2737 switch (version) {
2738 case SSL3_VERSION:
2739 return &SSLv3_enc_data;
2740
2741 case TLS1_VERSION:
2742 return &TLSv1_enc_data;
2743
David Benjamin9e13e1a2015-03-05 01:56:32 -05002744 case DTLS1_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002745 case TLS1_1_VERSION:
2746 return &TLSv1_1_enc_data;
2747
David Benjamin9e13e1a2015-03-05 01:56:32 -05002748 case DTLS1_2_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002749 case TLS1_2_VERSION:
2750 return &TLSv1_2_enc_data;
2751
Adam Langleyfcf25832014-12-18 17:42:32 -08002752 default:
2753 return NULL;
2754 }
2755}
2756
2757uint16_t ssl3_get_max_server_version(const SSL *s) {
2758 uint16_t max_version;
2759
2760 if (SSL_IS_DTLS(s)) {
2761 max_version = (s->max_version != 0) ? s->max_version : DTLS1_2_VERSION;
2762 if (!(s->options & SSL_OP_NO_DTLSv1_2) && DTLS1_2_VERSION >= max_version) {
2763 return DTLS1_2_VERSION;
2764 }
2765 if (!(s->options & SSL_OP_NO_DTLSv1) && DTLS1_VERSION >= max_version) {
2766 return DTLS1_VERSION;
2767 }
2768 return 0;
2769 }
2770
2771 max_version = (s->max_version != 0) ? s->max_version : TLS1_2_VERSION;
2772 if (!(s->options & SSL_OP_NO_TLSv1_2) && TLS1_2_VERSION <= max_version) {
2773 return TLS1_2_VERSION;
2774 }
2775 if (!(s->options & SSL_OP_NO_TLSv1_1) && TLS1_1_VERSION <= max_version) {
2776 return TLS1_1_VERSION;
2777 }
2778 if (!(s->options & SSL_OP_NO_TLSv1) && TLS1_VERSION <= max_version) {
2779 return TLS1_VERSION;
2780 }
2781 if (!(s->options & SSL_OP_NO_SSLv3) && SSL3_VERSION <= max_version) {
2782 return SSL3_VERSION;
2783 }
2784 return 0;
2785}
2786
2787uint16_t ssl3_get_mutual_version(SSL *s, uint16_t client_version) {
2788 uint16_t version = 0;
2789
2790 if (SSL_IS_DTLS(s)) {
2791 /* Clamp client_version to max_version. */
2792 if (s->max_version != 0 && client_version < s->max_version) {
2793 client_version = s->max_version;
2794 }
2795
2796 if (client_version <= DTLS1_2_VERSION && !(s->options & SSL_OP_NO_DTLSv1_2)) {
2797 version = DTLS1_2_VERSION;
2798 } else if (client_version <= DTLS1_VERSION &&
David Benjaminb18f0242015-03-10 18:30:08 -04002799 !(s->options & SSL_OP_NO_DTLSv1)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002800 version = DTLS1_VERSION;
2801 }
2802
2803 /* Check against min_version. */
2804 if (version != 0 && s->min_version != 0 && version > s->min_version) {
2805 return 0;
2806 }
2807 return version;
2808 } else {
2809 /* Clamp client_version to max_version. */
2810 if (s->max_version != 0 && client_version > s->max_version) {
2811 client_version = s->max_version;
2812 }
2813
2814 if (client_version >= TLS1_2_VERSION && !(s->options & SSL_OP_NO_TLSv1_2)) {
2815 version = TLS1_2_VERSION;
2816 } else if (client_version >= TLS1_1_VERSION &&
2817 !(s->options & SSL_OP_NO_TLSv1_1)) {
2818 version = TLS1_1_VERSION;
2819 } else if (client_version >= TLS1_VERSION && !(s->options & SSL_OP_NO_TLSv1)) {
2820 version = TLS1_VERSION;
2821 } else if (client_version >= SSL3_VERSION && !(s->options & SSL_OP_NO_SSLv3)) {
2822 version = SSL3_VERSION;
2823 }
2824
2825 /* Check against min_version. */
2826 if (version != 0 && s->min_version != 0 && version < s->min_version) {
2827 return 0;
2828 }
2829 return version;
2830 }
2831}
2832
2833uint16_t ssl3_get_max_client_version(SSL *s) {
David Benjamin123a8fd2015-04-26 14:16:41 -04002834 uint32_t options = s->options;
Adam Langleyfcf25832014-12-18 17:42:32 -08002835 uint16_t version = 0;
2836
2837 /* OpenSSL's API for controlling versions entails blacklisting individual
2838 * protocols. This has two problems. First, on the client, the protocol can
2839 * only express a contiguous range of versions. Second, a library consumer
2840 * trying to set a maximum version cannot disable protocol versions that get
2841 * added in a future version of the library.
2842 *
2843 * To account for both of these, OpenSSL interprets the client-side bitmask
2844 * as a min/max range by picking the lowest contiguous non-empty range of
2845 * enabled protocols. Note that this means it is impossible to set a maximum
2846 * version of TLS 1.2 in a future-proof way.
2847 *
2848 * By this scheme, the maximum version is the lowest version V such that V is
2849 * enabled and V+1 is disabled or unimplemented. */
2850 if (SSL_IS_DTLS(s)) {
2851 if (!(options & SSL_OP_NO_DTLSv1_2)) {
2852 version = DTLS1_2_VERSION;
2853 }
2854 if (!(options & SSL_OP_NO_DTLSv1) && (options & SSL_OP_NO_DTLSv1_2)) {
2855 version = DTLS1_VERSION;
2856 }
2857 if (s->max_version != 0 && version < s->max_version) {
2858 version = s->max_version;
2859 }
2860 } else {
2861 if (!(options & SSL_OP_NO_TLSv1_2)) {
2862 version = TLS1_2_VERSION;
2863 }
2864 if (!(options & SSL_OP_NO_TLSv1_1) && (options & SSL_OP_NO_TLSv1_2)) {
2865 version = TLS1_1_VERSION;
2866 }
2867 if (!(options & SSL_OP_NO_TLSv1) && (options & SSL_OP_NO_TLSv1_1)) {
2868 version = TLS1_VERSION;
2869 }
2870 if (!(options & SSL_OP_NO_SSLv3) && (options & SSL_OP_NO_TLSv1)) {
2871 version = SSL3_VERSION;
2872 }
2873 if (s->max_version != 0 && version > s->max_version) {
2874 version = s->max_version;
2875 }
2876 }
2877
2878 return version;
2879}
2880
2881int ssl3_is_version_enabled(SSL *s, uint16_t version) {
2882 if (SSL_IS_DTLS(s)) {
2883 if (s->max_version != 0 && version < s->max_version) {
2884 return 0;
2885 }
2886 if (s->min_version != 0 && version > s->min_version) {
2887 return 0;
2888 }
2889
2890 switch (version) {
2891 case DTLS1_VERSION:
2892 return !(s->options & SSL_OP_NO_DTLSv1);
2893
2894 case DTLS1_2_VERSION:
2895 return !(s->options & SSL_OP_NO_DTLSv1_2);
2896
2897 default:
2898 return 0;
2899 }
2900 } else {
2901 if (s->max_version != 0 && version > s->max_version) {
2902 return 0;
2903 }
2904 if (s->min_version != 0 && version < s->min_version) {
2905 return 0;
2906 }
2907
2908 switch (version) {
2909 case SSL3_VERSION:
2910 return !(s->options & SSL_OP_NO_SSLv3);
2911
2912 case TLS1_VERSION:
2913 return !(s->options & SSL_OP_NO_TLSv1);
2914
2915 case TLS1_1_VERSION:
2916 return !(s->options & SSL_OP_NO_TLSv1_1);
2917
2918 case TLS1_2_VERSION:
2919 return !(s->options & SSL_OP_NO_TLSv1_2);
2920
2921 default:
2922 return 0;
2923 }
2924 }
2925}
2926
David Benjaminea72bd02014-12-21 21:27:41 -05002927uint16_t ssl3_version_from_wire(SSL *s, uint16_t wire_version) {
2928 if (!SSL_IS_DTLS(s)) {
2929 return wire_version;
2930 }
2931
2932 uint16_t tls_version = ~wire_version;
2933 uint16_t version = tls_version + 0x0201;
2934 /* If either component overflowed, clamp it so comparisons still work. */
2935 if ((version >> 8) < (tls_version >> 8)) {
2936 version = 0xff00 | (version & 0xff);
2937 }
2938 if ((version & 0xff) < (tls_version & 0xff)) {
2939 version = (version & 0xff00) | 0xff;
2940 }
2941 /* DTLS 1.0 maps to TLS 1.1, not TLS 1.0. */
2942 if (version == TLS1_VERSION) {
2943 version = TLS1_1_VERSION;
2944 }
2945 return version;
2946}
2947
Adam Langleyfcf25832014-12-18 17:42:32 -08002948int SSL_cache_hit(SSL *s) { return s->hit; }
2949
2950int SSL_is_server(SSL *s) { return s->server; }
2951
Adam Langley524e7172015-02-20 16:04:00 -08002952void SSL_CTX_set_dos_protection_cb(
2953 SSL_CTX *ctx, int (*cb)(const struct ssl_early_callback_ctx *)) {
2954 ctx->dos_protection_cb = cb;
2955}
2956
Adam Langleyfcf25832014-12-18 17:42:32 -08002957void SSL_enable_fastradio_padding(SSL *s, char on_off) {
2958 s->fastradio_padding = on_off;
2959}
Adam Langley44972942015-02-20 13:43:23 -08002960
David Benjaminb16346b2015-04-08 19:16:58 -04002961void SSL_set_reject_peer_renegotiations(SSL *s, int reject) {
2962 s->reject_peer_renegotiations = !!reject;
2963}
2964
Adam Langley44972942015-02-20 13:43:23 -08002965const SSL_CIPHER *SSL_get_cipher_by_value(uint16_t value) {
2966 return ssl3_get_cipher_by_value(value);
2967}
Adam Langley3f92d212015-02-20 15:32:52 -08002968
2969int SSL_get_rc4_state(const SSL *ssl, const RC4_KEY **read_key,
2970 const RC4_KEY **write_key) {
2971 if (ssl->aead_read_ctx == NULL || ssl->aead_write_ctx == NULL) {
2972 return 0;
2973 }
2974
2975 return EVP_AEAD_CTX_get_rc4_state(&ssl->aead_read_ctx->ctx, read_key) &&
2976 EVP_AEAD_CTX_get_rc4_state(&ssl->aead_write_ctx->ctx, write_key);
2977}
David Benjaminda881e92015-04-26 14:45:04 -04002978
2979int SSL_CTX_sess_connect(const SSL_CTX *ctx) { return 0; }
2980int SSL_CTX_sess_connect_good(const SSL_CTX *ctx) { return 0; }
2981int SSL_CTX_sess_connect_renegotiate(const SSL_CTX *ctx) { return 0; }
2982int SSL_CTX_sess_accept(const SSL_CTX *ctx) { return 0; }
2983int SSL_CTX_sess_accept_renegotiate(const SSL_CTX *ctx) { return 0; }
2984int SSL_CTX_sess_accept_good(const SSL_CTX *ctx) { return 0; }
2985int SSL_CTX_sess_hits(const SSL_CTX *ctx) { return 0; }
2986int SSL_CTX_sess_cb_hits(const SSL_CTX *ctx) { return 0; }
2987int SSL_CTX_sess_misses(const SSL_CTX *ctx) { return 0; }
2988int SSL_CTX_sess_timeouts(const SSL_CTX *ctx) { return 0; }
2989int SSL_CTX_sess_cache_full(const SSL_CTX *ctx) { return 0; }