blob: 7edcacf54879ea1eb49ee9ba37433e859cc578f5 [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 Benjamin1d0a1942015-04-26 15:35:35 -0400164/* kMaxHandshakeSize is the maximum size, in bytes, of a handshake message. */
165static const size_t kMaxHandshakeSize = (1u << 24) - 1;
166
David Benjamin9f33fc62015-04-15 17:29:53 -0400167static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl = CRYPTO_EX_DATA_CLASS_INIT;
168static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl_ctx = CRYPTO_EX_DATA_CLASS_INIT;
169
Adam Langleyfcf25832014-12-18 17:42:32 -0800170int SSL_clear(SSL *s) {
171 if (s->method == NULL) {
172 OPENSSL_PUT_ERROR(SSL, SSL_clear, SSL_R_NO_METHOD_SPECIFIED);
173 return 0;
174 }
Adam Langley95c29f32014-06-20 12:00:00 -0700175
Adam Langleyfcf25832014-12-18 17:42:32 -0800176 if (ssl_clear_bad_session(s)) {
177 SSL_SESSION_free(s->session);
178 s->session = NULL;
179 }
Adam Langley95c29f32014-06-20 12:00:00 -0700180
Adam Langleyfcf25832014-12-18 17:42:32 -0800181 s->hit = 0;
182 s->shutdown = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700183
Adam Langleyfcf25832014-12-18 17:42:32 -0800184 if (s->renegotiate) {
185 OPENSSL_PUT_ERROR(SSL, SSL_clear, ERR_R_INTERNAL_ERROR);
186 return 0;
187 }
Adam Langley95c29f32014-06-20 12:00:00 -0700188
Adam Langleyfcf25832014-12-18 17:42:32 -0800189 /* SSL_clear may be called before or after the |s| is initialized in either
190 * accept or connect state. In the latter case, SSL_clear should preserve the
191 * half and reset |s->state| accordingly. */
192 if (s->handshake_func != NULL) {
193 if (s->server) {
194 SSL_set_accept_state(s);
195 } else {
196 SSL_set_connect_state(s);
197 }
198 } else {
199 assert(s->state == 0);
200 }
Adam Langley95c29f32014-06-20 12:00:00 -0700201
David Benjamin62fd1622015-01-11 13:30:01 -0500202 /* TODO(davidben): Some state on |s| is reset both in |SSL_new| and
203 * |SSL_clear| because it is per-connection state rather than configuration
204 * state. Per-connection state should be on |s->s3| and |s->d1| so it is
205 * naturally reset at the right points between |SSL_new|, |SSL_clear|, and
206 * |ssl3_new|. */
207
Adam Langleyfcf25832014-12-18 17:42:32 -0800208 s->rwstate = SSL_NOTHING;
209 s->rstate = SSL_ST_READ_HEADER;
Adam Langley95c29f32014-06-20 12:00:00 -0700210
David Benjamin2755a3e2015-04-22 16:17:58 -0400211 BUF_MEM_free(s->init_buf);
212 s->init_buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700213
David Benjamin62fd1622015-01-11 13:30:01 -0500214 s->packet = NULL;
215 s->packet_length = 0;
216
Adam Langleyfcf25832014-12-18 17:42:32 -0800217 ssl_clear_cipher_ctx(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700218
David Benjamin2755a3e2015-04-22 16:17:58 -0400219 OPENSSL_free(s->next_proto_negotiated);
220 s->next_proto_negotiated = NULL;
221 s->next_proto_negotiated_len = 0;
David Benjamin62fd1622015-01-11 13:30:01 -0500222
223 /* The s->d1->mtu is simultaneously configuration (preserved across
224 * clear) and connection-specific state (gets reset).
225 *
226 * TODO(davidben): Avoid this. */
227 unsigned mtu = 0;
228 if (s->d1 != NULL) {
229 mtu = s->d1->mtu;
230 }
231
232 s->method->ssl_free(s);
233 if (!s->method->ssl_new(s)) {
234 return 0;
235 }
236 s->enc_method = ssl3_get_enc_method(s->version);
237 assert(s->enc_method != NULL);
238
239 if (SSL_IS_DTLS(s) && (SSL_get_options(s) & SSL_OP_NO_QUERY_MTU)) {
240 s->d1->mtu = mtu;
241 }
242
Adam Langleyfcf25832014-12-18 17:42:32 -0800243 s->client_version = s->version;
Adam Langley95c29f32014-06-20 12:00:00 -0700244
Adam Langleyfcf25832014-12-18 17:42:32 -0800245 return 1;
246}
Adam Langley95c29f32014-06-20 12:00:00 -0700247
Adam Langleyfcf25832014-12-18 17:42:32 -0800248SSL *SSL_new(SSL_CTX *ctx) {
249 SSL *s;
Adam Langley95c29f32014-06-20 12:00:00 -0700250
Adam Langleyfcf25832014-12-18 17:42:32 -0800251 if (ctx == NULL) {
252 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_NULL_SSL_CTX);
253 return NULL;
254 }
255 if (ctx->method == NULL) {
256 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
257 return NULL;
258 }
Adam Langley95c29f32014-06-20 12:00:00 -0700259
Adam Langleyfcf25832014-12-18 17:42:32 -0800260 s = (SSL *)OPENSSL_malloc(sizeof(SSL));
261 if (s == NULL) {
262 goto err;
263 }
264 memset(s, 0, sizeof(SSL));
Adam Langley95c29f32014-06-20 12:00:00 -0700265
Adam Langleyfcf25832014-12-18 17:42:32 -0800266 s->min_version = ctx->min_version;
267 s->max_version = ctx->max_version;
David Benjamin1eb367c2014-12-12 18:17:51 -0500268
Adam Langleyfcf25832014-12-18 17:42:32 -0800269 s->options = ctx->options;
270 s->mode = ctx->mode;
271 s->max_cert_list = ctx->max_cert_list;
Adam Langley95c29f32014-06-20 12:00:00 -0700272
David Benjamina5a3eeb2015-03-18 20:26:30 -0400273 s->cert = ssl_cert_dup(ctx->cert);
274 if (s->cert == NULL) {
275 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -0800276 }
Adam Langley95c29f32014-06-20 12:00:00 -0700277
Adam Langleyfcf25832014-12-18 17:42:32 -0800278 s->read_ahead = ctx->read_ahead;
279 s->msg_callback = ctx->msg_callback;
280 s->msg_callback_arg = ctx->msg_callback_arg;
281 s->verify_mode = ctx->verify_mode;
282 s->sid_ctx_length = ctx->sid_ctx_length;
283 assert(s->sid_ctx_length <= sizeof s->sid_ctx);
284 memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));
285 s->verify_callback = ctx->default_verify_callback;
286 s->generate_session_id = ctx->generate_session_id;
Adam Langley95c29f32014-06-20 12:00:00 -0700287
Adam Langleyfcf25832014-12-18 17:42:32 -0800288 s->param = X509_VERIFY_PARAM_new();
289 if (!s->param) {
290 goto err;
291 }
292 X509_VERIFY_PARAM_inherit(s->param, ctx->param);
293 s->quiet_shutdown = ctx->quiet_shutdown;
294 s->max_send_fragment = ctx->max_send_fragment;
Adam Langley95c29f32014-06-20 12:00:00 -0700295
Adam Langleyfcf25832014-12-18 17:42:32 -0800296 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
297 s->ctx = ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -0800298 s->tlsext_ticket_expected = 0;
299 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
300 s->initial_ctx = ctx;
301 if (ctx->tlsext_ecpointformatlist) {
302 s->tlsext_ecpointformatlist = BUF_memdup(
303 ctx->tlsext_ecpointformatlist, ctx->tlsext_ecpointformatlist_length);
304 if (!s->tlsext_ecpointformatlist) {
305 goto err;
306 }
307 s->tlsext_ecpointformatlist_length = ctx->tlsext_ecpointformatlist_length;
308 }
Adam Langley95c29f32014-06-20 12:00:00 -0700309
Adam Langleyfcf25832014-12-18 17:42:32 -0800310 if (ctx->tlsext_ellipticcurvelist) {
311 s->tlsext_ellipticcurvelist =
312 BUF_memdup(ctx->tlsext_ellipticcurvelist,
313 ctx->tlsext_ellipticcurvelist_length * 2);
314 if (!s->tlsext_ellipticcurvelist) {
315 goto err;
316 }
317 s->tlsext_ellipticcurvelist_length = ctx->tlsext_ellipticcurvelist_length;
318 }
319 s->next_proto_negotiated = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700320
Adam Langleyfcf25832014-12-18 17:42:32 -0800321 if (s->ctx->alpn_client_proto_list) {
322 s->alpn_client_proto_list = BUF_memdup(s->ctx->alpn_client_proto_list,
323 s->ctx->alpn_client_proto_list_len);
324 if (s->alpn_client_proto_list == NULL) {
325 goto err;
326 }
327 s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;
328 }
Adam Langley95c29f32014-06-20 12:00:00 -0700329
Adam Langleyfcf25832014-12-18 17:42:32 -0800330 s->verify_result = X509_V_OK;
331 s->method = ctx->method;
Adam Langley95c29f32014-06-20 12:00:00 -0700332
Adam Langleyfcf25832014-12-18 17:42:32 -0800333 if (!s->method->ssl_new(s)) {
334 goto err;
335 }
336 s->enc_method = ssl3_get_enc_method(s->version);
337 assert(s->enc_method != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700338
David Benjamin62fd1622015-01-11 13:30:01 -0500339 s->rwstate = SSL_NOTHING;
340 s->rstate = SSL_ST_READ_HEADER;
Adam Langley95c29f32014-06-20 12:00:00 -0700341
David Benjamin9f33fc62015-04-15 17:29:53 -0400342 CRYPTO_new_ex_data(&g_ex_data_class_ssl, s, &s->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700343
Adam Langleyfcf25832014-12-18 17:42:32 -0800344 s->psk_identity_hint = NULL;
345 if (ctx->psk_identity_hint) {
346 s->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
347 if (s->psk_identity_hint == NULL) {
348 goto err;
349 }
350 }
351 s->psk_client_callback = ctx->psk_client_callback;
352 s->psk_server_callback = ctx->psk_server_callback;
Adam Langley95c29f32014-06-20 12:00:00 -0700353
David Benjamin02ddbfd2015-01-11 13:09:11 -0500354 s->tlsext_channel_id_enabled = ctx->tlsext_channel_id_enabled;
355 if (ctx->tlsext_channel_id_private) {
356 s->tlsext_channel_id_private = EVP_PKEY_dup(ctx->tlsext_channel_id_private);
357 }
358
Adam Langleyfcf25832014-12-18 17:42:32 -0800359 s->signed_cert_timestamps_enabled = s->ctx->signed_cert_timestamps_enabled;
360 s->ocsp_stapling_enabled = s->ctx->ocsp_stapling_enabled;
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200361
Adam Langleyfcf25832014-12-18 17:42:32 -0800362 return s;
363
Adam Langley95c29f32014-06-20 12:00:00 -0700364err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400365 SSL_free(s);
Adam Langleyfcf25832014-12-18 17:42:32 -0800366 OPENSSL_PUT_ERROR(SSL, SSL_new, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700367
Adam Langleyfcf25832014-12-18 17:42:32 -0800368 return NULL;
369}
Adam Langley95c29f32014-06-20 12:00:00 -0700370
Adam Langleyfcf25832014-12-18 17:42:32 -0800371int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
372 unsigned int sid_ctx_len) {
373 if (sid_ctx_len > sizeof ctx->sid_ctx) {
374 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_session_id_context,
375 SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
376 return 0;
377 }
378 ctx->sid_ctx_length = sid_ctx_len;
379 memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700380
Adam Langleyfcf25832014-12-18 17:42:32 -0800381 return 1;
382}
Adam Langley95c29f32014-06-20 12:00:00 -0700383
Adam Langleyfcf25832014-12-18 17:42:32 -0800384int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
385 unsigned int sid_ctx_len) {
386 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
387 OPENSSL_PUT_ERROR(SSL, SSL_set_session_id_context,
388 SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
389 return 0;
390 }
391 ssl->sid_ctx_length = sid_ctx_len;
392 memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700393
Adam Langleyfcf25832014-12-18 17:42:32 -0800394 return 1;
395}
Adam Langley95c29f32014-06-20 12:00:00 -0700396
Adam Langleyfcf25832014-12-18 17:42:32 -0800397int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb) {
398 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
399 ctx->generate_session_id = cb;
400 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
401 return 1;
402}
Adam Langley95c29f32014-06-20 12:00:00 -0700403
Adam Langleyfcf25832014-12-18 17:42:32 -0800404int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800405 ssl->generate_session_id = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -0800406 return 1;
407}
Adam Langley95c29f32014-06-20 12:00:00 -0700408
Adam Langleyfcf25832014-12-18 17:42:32 -0800409int SSL_has_matching_session_id(const SSL *ssl, const uint8_t *id,
410 unsigned int id_len) {
411 /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how we
412 * can "construct" a session to give us the desired check - ie. to find if
413 * there's a session in the hash table that would conflict with any new
414 * session built out of this id/id_len and the ssl_version in use by this
415 * SSL. */
416 SSL_SESSION r, *p;
Adam Langley95c29f32014-06-20 12:00:00 -0700417
Adam Langleyfcf25832014-12-18 17:42:32 -0800418 if (id_len > sizeof r.session_id) {
419 return 0;
420 }
Adam Langley95c29f32014-06-20 12:00:00 -0700421
Adam Langleyfcf25832014-12-18 17:42:32 -0800422 r.ssl_version = ssl->version;
423 r.session_id_length = id_len;
424 memcpy(r.session_id, id, id_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700425
Adam Langleyfcf25832014-12-18 17:42:32 -0800426 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
427 p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r);
428 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
429 return p != NULL;
430}
Adam Langley95c29f32014-06-20 12:00:00 -0700431
Adam Langleyfcf25832014-12-18 17:42:32 -0800432int SSL_CTX_set_purpose(SSL_CTX *s, int purpose) {
433 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
434}
435
436int SSL_set_purpose(SSL *s, int purpose) {
437 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
438}
439
440int SSL_CTX_set_trust(SSL_CTX *s, int trust) {
441 return X509_VERIFY_PARAM_set_trust(s->param, trust);
442}
443
444int SSL_set_trust(SSL *s, int trust) {
445 return X509_VERIFY_PARAM_set_trust(s->param, trust);
446}
447
448int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm) {
449 return X509_VERIFY_PARAM_set1(ctx->param, vpm);
450}
451
452int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm) {
453 return X509_VERIFY_PARAM_set1(ssl->param, vpm);
454}
Adam Langley95c29f32014-06-20 12:00:00 -0700455
Adam Langley858a88d2014-06-20 12:00:00 -0700456void ssl_cipher_preference_list_free(
Adam Langleyfcf25832014-12-18 17:42:32 -0800457 struct ssl_cipher_preference_list_st *cipher_list) {
David Benjamin5d1ec732015-04-22 13:38:00 -0400458 if (cipher_list == NULL) {
459 return;
460 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800461 sk_SSL_CIPHER_free(cipher_list->ciphers);
462 OPENSSL_free(cipher_list->in_group_flags);
463 OPENSSL_free(cipher_list);
464}
Adam Langley858a88d2014-06-20 12:00:00 -0700465
Adam Langleyfcf25832014-12-18 17:42:32 -0800466struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_dup(
467 struct ssl_cipher_preference_list_st *cipher_list) {
468 struct ssl_cipher_preference_list_st *ret = NULL;
469 size_t n = sk_SSL_CIPHER_num(cipher_list->ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700470
Adam Langleyfcf25832014-12-18 17:42:32 -0800471 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
472 if (!ret) {
473 goto err;
474 }
475
476 ret->ciphers = NULL;
477 ret->in_group_flags = NULL;
478 ret->ciphers = sk_SSL_CIPHER_dup(cipher_list->ciphers);
479 if (!ret->ciphers) {
480 goto err;
481 }
482 ret->in_group_flags = BUF_memdup(cipher_list->in_group_flags, n);
483 if (!ret->in_group_flags) {
484 goto err;
485 }
486
487 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -0700488
489err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400490 ssl_cipher_preference_list_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -0800491 return NULL;
492}
Adam Langley858a88d2014-06-20 12:00:00 -0700493
Adam Langleyfcf25832014-12-18 17:42:32 -0800494struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_from_ciphers(
495 STACK_OF(SSL_CIPHER) * ciphers) {
496 struct ssl_cipher_preference_list_st *ret = NULL;
497 size_t n = sk_SSL_CIPHER_num(ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700498
Adam Langleyfcf25832014-12-18 17:42:32 -0800499 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
500 if (!ret) {
501 goto err;
502 }
503 ret->ciphers = NULL;
504 ret->in_group_flags = NULL;
505 ret->ciphers = sk_SSL_CIPHER_dup(ciphers);
506 if (!ret->ciphers) {
507 goto err;
508 }
509 ret->in_group_flags = OPENSSL_malloc(n);
510 if (!ret->in_group_flags) {
511 goto err;
512 }
513 memset(ret->in_group_flags, 0, n);
514 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -0700515
516err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400517 ssl_cipher_preference_list_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -0800518 return NULL;
519}
Adam Langley858a88d2014-06-20 12:00:00 -0700520
Adam Langleyfcf25832014-12-18 17:42:32 -0800521X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) { return ctx->param; }
Adam Langley95c29f32014-06-20 12:00:00 -0700522
Adam Langleyfcf25832014-12-18 17:42:32 -0800523X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) { return ssl->param; }
Adam Langley95c29f32014-06-20 12:00:00 -0700524
Adam Langleyfcf25832014-12-18 17:42:32 -0800525void SSL_certs_clear(SSL *s) { ssl_cert_clear_certs(s->cert); }
Adam Langley95c29f32014-06-20 12:00:00 -0700526
Adam Langleyfcf25832014-12-18 17:42:32 -0800527void SSL_free(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800528 if (s == NULL) {
529 return;
530 }
Adam Langley95c29f32014-06-20 12:00:00 -0700531
David Benjamin2755a3e2015-04-22 16:17:58 -0400532 X509_VERIFY_PARAM_free(s->param);
Adam Langley95c29f32014-06-20 12:00:00 -0700533
David Benjamin9f33fc62015-04-15 17:29:53 -0400534 CRYPTO_free_ex_data(&g_ex_data_class_ssl, s, &s->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700535
Adam Langleyfcf25832014-12-18 17:42:32 -0800536 if (s->bbio != NULL) {
537 /* If the buffering BIO is in place, pop it off */
538 if (s->bbio == s->wbio) {
539 s->wbio = BIO_pop(s->wbio);
540 }
541 BIO_free(s->bbio);
542 s->bbio = NULL;
543 }
Adam Langley95c29f32014-06-20 12:00:00 -0700544
David Benjamin2755a3e2015-04-22 16:17:58 -0400545 int free_wbio = s->wbio != s->rbio;
546 BIO_free_all(s->rbio);
547 if (free_wbio) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800548 BIO_free_all(s->wbio);
549 }
Adam Langley95c29f32014-06-20 12:00:00 -0700550
David Benjamin2755a3e2015-04-22 16:17:58 -0400551 BUF_MEM_free(s->init_buf);
Adam Langley95c29f32014-06-20 12:00:00 -0700552
Adam Langleyfcf25832014-12-18 17:42:32 -0800553 /* add extra stuff */
David Benjamin2755a3e2015-04-22 16:17:58 -0400554 ssl_cipher_preference_list_free(s->cipher_list);
555 sk_SSL_CIPHER_free(s->cipher_list_by_id);
Adam Langley95c29f32014-06-20 12:00:00 -0700556
David Benjamin2755a3e2015-04-22 16:17:58 -0400557 ssl_clear_bad_session(s);
558 SSL_SESSION_free(s->session);
Adam Langley95c29f32014-06-20 12:00:00 -0700559
Adam Langleyfcf25832014-12-18 17:42:32 -0800560 ssl_clear_cipher_ctx(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700561
David Benjamin2755a3e2015-04-22 16:17:58 -0400562 ssl_cert_free(s->cert);
Adam Langley0289c732014-06-20 12:00:00 -0700563
David Benjamin2755a3e2015-04-22 16:17:58 -0400564 OPENSSL_free(s->tlsext_hostname);
565 SSL_CTX_free(s->initial_ctx);
566 OPENSSL_free(s->tlsext_ecpointformatlist);
567 OPENSSL_free(s->tlsext_ellipticcurvelist);
568 OPENSSL_free(s->alpn_client_proto_list);
569 EVP_PKEY_free(s->tlsext_channel_id_private);
570 OPENSSL_free(s->psk_identity_hint);
571 sk_X509_NAME_pop_free(s->client_CA, X509_NAME_free);
572 OPENSSL_free(s->next_proto_negotiated);
573 sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);
Adam Langley95c29f32014-06-20 12:00:00 -0700574
Adam Langleyfcf25832014-12-18 17:42:32 -0800575 if (s->method != NULL) {
576 s->method->ssl_free(s);
577 }
David Benjamin2755a3e2015-04-22 16:17:58 -0400578 SSL_CTX_free(s->ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700579
Adam Langleyfcf25832014-12-18 17:42:32 -0800580 OPENSSL_free(s);
581}
Adam Langley95c29f32014-06-20 12:00:00 -0700582
Adam Langleyfcf25832014-12-18 17:42:32 -0800583void SSL_set_bio(SSL *s, BIO *rbio, BIO *wbio) {
584 /* If the output buffering BIO is still in place, remove it. */
585 if (s->bbio != NULL) {
586 if (s->wbio == s->bbio) {
587 s->wbio = s->wbio->next_bio;
588 s->bbio->next_bio = NULL;
589 }
590 }
Adam Langley95c29f32014-06-20 12:00:00 -0700591
David Benjamin2755a3e2015-04-22 16:17:58 -0400592 if (s->rbio != rbio) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800593 BIO_free_all(s->rbio);
594 }
David Benjamin2755a3e2015-04-22 16:17:58 -0400595 if (s->wbio != wbio && s->rbio != s->wbio) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800596 BIO_free_all(s->wbio);
597 }
598 s->rbio = rbio;
599 s->wbio = wbio;
600}
Adam Langley95c29f32014-06-20 12:00:00 -0700601
Adam Langleyfcf25832014-12-18 17:42:32 -0800602BIO *SSL_get_rbio(const SSL *s) { return s->rbio; }
Adam Langley95c29f32014-06-20 12:00:00 -0700603
Adam Langleyfcf25832014-12-18 17:42:32 -0800604BIO *SSL_get_wbio(const SSL *s) { return s->wbio; }
Adam Langley95c29f32014-06-20 12:00:00 -0700605
Adam Langleyfcf25832014-12-18 17:42:32 -0800606int SSL_get_fd(const SSL *s) { return SSL_get_rfd(s); }
Adam Langley95c29f32014-06-20 12:00:00 -0700607
Adam Langleyfcf25832014-12-18 17:42:32 -0800608int SSL_get_rfd(const SSL *s) {
609 int ret = -1;
610 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700611
Adam Langleyfcf25832014-12-18 17:42:32 -0800612 b = SSL_get_rbio(s);
613 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
614 if (r != NULL) {
615 BIO_get_fd(r, &ret);
616 }
617 return ret;
618}
Adam Langley95c29f32014-06-20 12:00:00 -0700619
Adam Langleyfcf25832014-12-18 17:42:32 -0800620int SSL_get_wfd(const SSL *s) {
621 int ret = -1;
622 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700623
Adam Langleyfcf25832014-12-18 17:42:32 -0800624 b = SSL_get_wbio(s);
625 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
626 if (r != NULL) {
627 BIO_get_fd(r, &ret);
628 }
Adam Langley95c29f32014-06-20 12:00:00 -0700629
Adam Langleyfcf25832014-12-18 17:42:32 -0800630 return ret;
631}
Adam Langley95c29f32014-06-20 12:00:00 -0700632
Adam Langleyfcf25832014-12-18 17:42:32 -0800633int SSL_set_fd(SSL *s, int fd) {
634 int ret = 0;
635 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700636
Adam Langleyfcf25832014-12-18 17:42:32 -0800637 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700638
Adam Langleyfcf25832014-12-18 17:42:32 -0800639 if (bio == NULL) {
640 OPENSSL_PUT_ERROR(SSL, SSL_set_fd, ERR_R_BUF_LIB);
641 goto err;
642 }
643 BIO_set_fd(bio, fd, BIO_NOCLOSE);
644 SSL_set_bio(s, bio, bio);
645 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700646
Adam Langley95c29f32014-06-20 12:00:00 -0700647err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800648 return ret;
649}
Adam Langley95c29f32014-06-20 12:00:00 -0700650
Adam Langleyfcf25832014-12-18 17:42:32 -0800651int SSL_set_wfd(SSL *s, int fd) {
652 int ret = 0;
653 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700654
Adam Langleyfcf25832014-12-18 17:42:32 -0800655 if (s->rbio == NULL || BIO_method_type(s->rbio) != BIO_TYPE_FD ||
656 (int)BIO_get_fd(s->rbio, NULL) != fd) {
657 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700658
Adam Langleyfcf25832014-12-18 17:42:32 -0800659 if (bio == NULL) {
660 OPENSSL_PUT_ERROR(SSL, SSL_set_wfd, ERR_R_BUF_LIB);
661 goto err;
662 }
663 BIO_set_fd(bio, fd, BIO_NOCLOSE);
664 SSL_set_bio(s, SSL_get_rbio(s), bio);
665 } else {
666 SSL_set_bio(s, SSL_get_rbio(s), SSL_get_rbio(s));
667 }
668
669 ret = 1;
670
Adam Langley95c29f32014-06-20 12:00:00 -0700671err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800672 return ret;
673}
Adam Langley95c29f32014-06-20 12:00:00 -0700674
Adam Langleyfcf25832014-12-18 17:42:32 -0800675int SSL_set_rfd(SSL *s, int fd) {
676 int ret = 0;
677 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700678
Adam Langleyfcf25832014-12-18 17:42:32 -0800679 if (s->wbio == NULL || BIO_method_type(s->wbio) != BIO_TYPE_FD ||
680 (int)BIO_get_fd(s->wbio, NULL) != fd) {
681 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700682
Adam Langleyfcf25832014-12-18 17:42:32 -0800683 if (bio == NULL) {
684 OPENSSL_PUT_ERROR(SSL, SSL_set_rfd, ERR_R_BUF_LIB);
685 goto err;
686 }
687 BIO_set_fd(bio, fd, BIO_NOCLOSE);
688 SSL_set_bio(s, bio, SSL_get_wbio(s));
689 } else {
690 SSL_set_bio(s, SSL_get_wbio(s), SSL_get_wbio(s));
691 }
692 ret = 1;
693
Adam Langley95c29f32014-06-20 12:00:00 -0700694err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800695 return ret;
696}
Adam Langley95c29f32014-06-20 12:00:00 -0700697
698/* return length of latest Finished message we sent, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -0800699size_t SSL_get_finished(const SSL *s, void *buf, size_t count) {
700 size_t ret = 0;
701
702 if (s->s3 != NULL) {
703 ret = s->s3->tmp.finish_md_len;
704 if (count > ret) {
705 count = ret;
706 }
707 memcpy(buf, s->s3->tmp.finish_md, count);
708 }
709
710 return ret;
711}
Adam Langley95c29f32014-06-20 12:00:00 -0700712
713/* return length of latest Finished message we expected, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -0800714size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count) {
715 size_t ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700716
Adam Langleyfcf25832014-12-18 17:42:32 -0800717 if (s->s3 != NULL) {
718 ret = s->s3->tmp.peer_finish_md_len;
719 if (count > ret) {
720 count = ret;
721 }
722 memcpy(buf, s->s3->tmp.peer_finish_md, count);
723 }
Adam Langley95c29f32014-06-20 12:00:00 -0700724
Adam Langleyfcf25832014-12-18 17:42:32 -0800725 return ret;
726}
Adam Langley95c29f32014-06-20 12:00:00 -0700727
Adam Langleyfcf25832014-12-18 17:42:32 -0800728int SSL_get_verify_mode(const SSL *s) { return s->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -0700729
Adam Langleyfcf25832014-12-18 17:42:32 -0800730int SSL_get_verify_depth(const SSL *s) {
731 return X509_VERIFY_PARAM_get_depth(s->param);
732}
Adam Langley95c29f32014-06-20 12:00:00 -0700733
Adam Langleyfcf25832014-12-18 17:42:32 -0800734int (*SSL_get_verify_callback(const SSL *s))(int, X509_STORE_CTX *) {
735 return s->verify_callback;
736}
Adam Langley95c29f32014-06-20 12:00:00 -0700737
Adam Langleyfcf25832014-12-18 17:42:32 -0800738int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) { return ctx->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -0700739
Adam Langleyfcf25832014-12-18 17:42:32 -0800740int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) {
741 return X509_VERIFY_PARAM_get_depth(ctx->param);
742}
Adam Langley95c29f32014-06-20 12:00:00 -0700743
Adam Langleyfcf25832014-12-18 17:42:32 -0800744int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int, X509_STORE_CTX *) {
745 return ctx->default_verify_callback;
746}
Adam Langley95c29f32014-06-20 12:00:00 -0700747
Adam Langleyfcf25832014-12-18 17:42:32 -0800748void SSL_set_verify(SSL *s, int mode,
749 int (*callback)(int ok, X509_STORE_CTX *ctx)) {
750 s->verify_mode = mode;
751 if (callback != NULL) {
752 s->verify_callback = callback;
753 }
754}
Adam Langley95c29f32014-06-20 12:00:00 -0700755
Adam Langleyfcf25832014-12-18 17:42:32 -0800756void SSL_set_verify_depth(SSL *s, int depth) {
757 X509_VERIFY_PARAM_set_depth(s->param, depth);
758}
Adam Langley95c29f32014-06-20 12:00:00 -0700759
David Benjamin61ecccf2015-05-05 09:44:51 -0400760int SSL_CTX_get_read_ahead(const SSL_CTX *ctx) { return ctx->read_ahead; }
Adam Langley95c29f32014-06-20 12:00:00 -0700761
Adam Langleyfcf25832014-12-18 17:42:32 -0800762int SSL_get_read_ahead(const SSL *s) { return s->read_ahead; }
Adam Langley95c29f32014-06-20 12:00:00 -0700763
David Benjamin61ecccf2015-05-05 09:44:51 -0400764void SSL_CTX_set_read_ahead(SSL_CTX *ctx, int yes) { ctx->read_ahead = !!yes; }
765
766void SSL_set_read_ahead(SSL *s, int yes) { s->read_ahead = !!yes; }
767
Adam Langleyfcf25832014-12-18 17:42:32 -0800768int SSL_pending(const SSL *s) {
769 /* SSL_pending cannot work properly if read-ahead is enabled
770 * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)), and it is
771 * impossible to fix since SSL_pending cannot report errors that may be
772 * observed while scanning the new data. (Note that SSL_pending() is often
773 * used as a boolean value, so we'd better not return -1.). */
774 return s->method->ssl_pending(s);
775}
Adam Langley95c29f32014-06-20 12:00:00 -0700776
Adam Langleyfcf25832014-12-18 17:42:32 -0800777X509 *SSL_get_peer_certificate(const SSL *s) {
778 X509 *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700779
Adam Langleyfcf25832014-12-18 17:42:32 -0800780 if (s == NULL || s->session == NULL) {
781 r = NULL;
782 } else {
783 r = s->session->peer;
784 }
Adam Langley95c29f32014-06-20 12:00:00 -0700785
Adam Langleyfcf25832014-12-18 17:42:32 -0800786 if (r == NULL) {
787 return NULL;
788 }
Adam Langley95c29f32014-06-20 12:00:00 -0700789
Adam Langleyfcf25832014-12-18 17:42:32 -0800790 return X509_up_ref(r);
791}
792
793STACK_OF(X509) * SSL_get_peer_cert_chain(const SSL *s) {
794 STACK_OF(X509) * r;
795
796 if (s == NULL || s->session == NULL || s->session->sess_cert == NULL) {
797 r = NULL;
798 } else {
799 r = s->session->sess_cert->cert_chain;
800 }
801
802 /* If we are a client, cert_chain includes the peer's own certificate; if we
803 * are a server, it does not. */
804 return r;
805}
Adam Langley95c29f32014-06-20 12:00:00 -0700806
Adam Langley95c29f32014-06-20 12:00:00 -0700807/* Fix this so it checks all the valid key/cert options */
Adam Langleyfcf25832014-12-18 17:42:32 -0800808int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
809 if (ctx == NULL || ctx->cert == NULL || ctx->cert->key->x509 == NULL) {
810 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key,
811 SSL_R_NO_CERTIFICATE_ASSIGNED);
812 return 0;
813 }
814
815 if (ctx->cert->key->privatekey == NULL) {
816 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key,
817 SSL_R_NO_PRIVATE_KEY_ASSIGNED);
818 return 0;
819 }
820
821 return X509_check_private_key(ctx->cert->key->x509,
822 ctx->cert->key->privatekey);
823}
Adam Langley95c29f32014-06-20 12:00:00 -0700824
825/* Fix this function so that it takes an optional type parameter */
Adam Langleyfcf25832014-12-18 17:42:32 -0800826int SSL_check_private_key(const SSL *ssl) {
827 if (ssl == NULL) {
828 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, ERR_R_PASSED_NULL_PARAMETER);
829 return 0;
830 }
Adam Langley95c29f32014-06-20 12:00:00 -0700831
Adam Langleyfcf25832014-12-18 17:42:32 -0800832 if (ssl->cert == NULL) {
833 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
834 SSL_R_NO_CERTIFICATE_ASSIGNED);
835 return 0;
836 }
Adam Langley95c29f32014-06-20 12:00:00 -0700837
Adam Langleyfcf25832014-12-18 17:42:32 -0800838 if (ssl->cert->key->x509 == NULL) {
839 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
840 SSL_R_NO_CERTIFICATE_ASSIGNED);
841 return 0;
842 }
David Benjamin0b145c22014-11-26 20:10:09 -0500843
Adam Langleyfcf25832014-12-18 17:42:32 -0800844 if (ssl->cert->key->privatekey == NULL) {
845 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
846 SSL_R_NO_PRIVATE_KEY_ASSIGNED);
847 return 0;
848 }
Adam Langley95c29f32014-06-20 12:00:00 -0700849
Adam Langleyfcf25832014-12-18 17:42:32 -0800850 return X509_check_private_key(ssl->cert->key->x509,
851 ssl->cert->key->privatekey);
852}
Adam Langley95c29f32014-06-20 12:00:00 -0700853
Adam Langleyfcf25832014-12-18 17:42:32 -0800854int SSL_accept(SSL *s) {
855 if (s->handshake_func == 0) {
856 /* Not properly initialized yet */
857 SSL_set_accept_state(s);
858 }
David Benjamin0b145c22014-11-26 20:10:09 -0500859
Adam Langleyfcf25832014-12-18 17:42:32 -0800860 if (s->handshake_func != s->method->ssl_accept) {
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +0100861 OPENSSL_PUT_ERROR(SSL, SSL_accept, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800862 return -1;
863 }
Adam Langley95c29f32014-06-20 12:00:00 -0700864
Adam Langleyfcf25832014-12-18 17:42:32 -0800865 return s->handshake_func(s);
866}
Adam Langley95c29f32014-06-20 12:00:00 -0700867
Adam Langleyfcf25832014-12-18 17:42:32 -0800868int SSL_connect(SSL *s) {
869 if (s->handshake_func == 0) {
870 /* Not properly initialized yet */
871 SSL_set_connect_state(s);
872 }
Adam Langley95c29f32014-06-20 12:00:00 -0700873
Adam Langleyfcf25832014-12-18 17:42:32 -0800874 if (s->handshake_func != s->method->ssl_connect) {
875 OPENSSL_PUT_ERROR(SSL, SSL_connect, ERR_R_INTERNAL_ERROR);
876 return -1;
877 }
Adam Langley95c29f32014-06-20 12:00:00 -0700878
Adam Langleyfcf25832014-12-18 17:42:32 -0800879 return s->handshake_func(s);
880}
Adam Langley95c29f32014-06-20 12:00:00 -0700881
Adam Langleyfcf25832014-12-18 17:42:32 -0800882long SSL_get_default_timeout(const SSL *s) {
883 return SSL_DEFAULT_SESSION_TIMEOUT;
884}
Adam Langley95c29f32014-06-20 12:00:00 -0700885
Adam Langleyfcf25832014-12-18 17:42:32 -0800886int SSL_read(SSL *s, void *buf, int num) {
887 if (s->handshake_func == 0) {
888 OPENSSL_PUT_ERROR(SSL, SSL_read, SSL_R_UNINITIALIZED);
889 return -1;
890 }
Adam Langley95c29f32014-06-20 12:00:00 -0700891
Adam Langleyfcf25832014-12-18 17:42:32 -0800892 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
893 s->rwstate = SSL_NOTHING;
894 return 0;
895 }
Adam Langley95c29f32014-06-20 12:00:00 -0700896
Adam Langleyfcf25832014-12-18 17:42:32 -0800897 return s->method->ssl_read(s, buf, num);
898}
Adam Langley95c29f32014-06-20 12:00:00 -0700899
Adam Langleyfcf25832014-12-18 17:42:32 -0800900int SSL_peek(SSL *s, void *buf, int num) {
901 if (s->handshake_func == 0) {
902 OPENSSL_PUT_ERROR(SSL, SSL_peek, SSL_R_UNINITIALIZED);
903 return -1;
904 }
Adam Langley95c29f32014-06-20 12:00:00 -0700905
Adam Langleyfcf25832014-12-18 17:42:32 -0800906 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
907 return 0;
908 }
Adam Langley95c29f32014-06-20 12:00:00 -0700909
Adam Langleyfcf25832014-12-18 17:42:32 -0800910 return s->method->ssl_peek(s, buf, num);
911}
Adam Langley95c29f32014-06-20 12:00:00 -0700912
Adam Langleyfcf25832014-12-18 17:42:32 -0800913int SSL_write(SSL *s, const void *buf, int num) {
914 if (s->handshake_func == 0) {
915 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_UNINITIALIZED);
916 return -1;
917 }
Adam Langley95c29f32014-06-20 12:00:00 -0700918
Adam Langleyfcf25832014-12-18 17:42:32 -0800919 if (s->shutdown & SSL_SENT_SHUTDOWN) {
920 s->rwstate = SSL_NOTHING;
921 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_PROTOCOL_IS_SHUTDOWN);
922 return -1;
923 }
Adam Langley95c29f32014-06-20 12:00:00 -0700924
Adam Langleyfcf25832014-12-18 17:42:32 -0800925 return s->method->ssl_write(s, buf, num);
926}
Adam Langley95c29f32014-06-20 12:00:00 -0700927
Adam Langleyfcf25832014-12-18 17:42:32 -0800928int SSL_shutdown(SSL *s) {
929 /* Note that this function behaves differently from what one might expect.
930 * Return values are 0 for no success (yet), 1 for success; but calling it
931 * once is usually not enough, even if blocking I/O is used (see
932 * ssl3_shutdown). */
Adam Langley95c29f32014-06-20 12:00:00 -0700933
Adam Langleyfcf25832014-12-18 17:42:32 -0800934 if (s->handshake_func == 0) {
935 OPENSSL_PUT_ERROR(SSL, SSL_shutdown, SSL_R_UNINITIALIZED);
936 return -1;
937 }
Adam Langley95c29f32014-06-20 12:00:00 -0700938
Adam Langleyfcf25832014-12-18 17:42:32 -0800939 if (!SSL_in_init(s)) {
940 return s->method->ssl_shutdown(s);
941 }
Adam Langley95c29f32014-06-20 12:00:00 -0700942
Adam Langleyfcf25832014-12-18 17:42:32 -0800943 return 1;
944}
Adam Langley95c29f32014-06-20 12:00:00 -0700945
Adam Langleyfcf25832014-12-18 17:42:32 -0800946int SSL_renegotiate(SSL *s) {
David Benjamin2cdace92015-01-24 13:30:02 -0500947 if (SSL_IS_DTLS(s)) {
948 /* Renegotiation is not supported for DTLS. */
949 OPENSSL_PUT_ERROR(SSL, SSL_renegotiate, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
950 return 0;
951 }
952
Adam Langleyfcf25832014-12-18 17:42:32 -0800953 if (s->renegotiate == 0) {
954 s->renegotiate = 1;
955 }
Adam Langley95c29f32014-06-20 12:00:00 -0700956
Adam Langleyfcf25832014-12-18 17:42:32 -0800957 s->new_session = 1;
958 return s->method->ssl_renegotiate(s);
959}
Adam Langley95c29f32014-06-20 12:00:00 -0700960
Adam Langleyfcf25832014-12-18 17:42:32 -0800961int SSL_renegotiate_pending(SSL *s) {
962 /* becomes true when negotiation is requested; false again once a handshake
963 * has finished */
964 return s->renegotiate != 0;
965}
Adam Langley95c29f32014-06-20 12:00:00 -0700966
David Benjamin61ecccf2015-05-05 09:44:51 -0400967uint32_t SSL_CTX_set_options(SSL_CTX *ctx, uint32_t options) {
968 ctx->options |= options;
969 return ctx->options;
970}
971
972uint32_t SSL_set_options(SSL *ssl, uint32_t options) {
973 ssl->options |= options;
974 return ssl->options;
975}
976
977uint32_t SSL_CTX_clear_options(SSL_CTX *ctx, uint32_t options) {
978 ctx->options &= ~options;
979 return ctx->options;
980}
981
982uint32_t SSL_clear_options(SSL *ssl, uint32_t options) {
983 ssl->options &= ~options;
984 return ssl->options;
985}
986
987uint32_t SSL_CTX_get_options(const SSL_CTX *ctx) { return ctx->options; }
988
989uint32_t SSL_get_options(const SSL *ssl) { return ssl->options; }
990
991uint32_t SSL_CTX_set_mode(SSL_CTX *ctx, uint32_t mode) {
992 ctx->mode |= mode;
993 return ctx->mode;
994}
995
996uint32_t SSL_set_mode(SSL *ssl, uint32_t mode) {
997 ssl->mode |= mode;
998 return ssl->mode;
999}
1000
1001uint32_t SSL_CTX_clear_mode(SSL_CTX *ctx, uint32_t mode) {
1002 ctx->mode &= ~mode;
1003 return ctx->mode;
1004}
1005
1006uint32_t SSL_clear_mode(SSL *ssl, uint32_t mode) {
1007 ssl->mode &= ~mode;
1008 return ssl->mode;
1009}
1010
1011uint32_t SSL_CTX_get_mode(const SSL_CTX *ctx) { return ctx->mode; }
1012
1013uint32_t SSL_get_mode(const SSL *ssl) { return ssl->mode; }
1014
David Benjamin1d0a1942015-04-26 15:35:35 -04001015size_t SSL_CTX_get_max_cert_list(const SSL_CTX *ctx) {
1016 return ctx->max_cert_list;
1017}
1018
1019void SSL_CTX_set_max_cert_list(SSL_CTX *ctx, size_t max_cert_list) {
1020 if (max_cert_list > kMaxHandshakeSize) {
1021 max_cert_list = kMaxHandshakeSize;
1022 }
1023 ctx->max_cert_list = (uint32_t)max_cert_list;
1024}
1025
1026size_t SSL_get_max_cert_list(const SSL *ssl) {
1027 return ssl->max_cert_list;
1028}
1029
1030void SSL_set_max_cert_list(SSL *ssl, size_t max_cert_list) {
1031 if (max_cert_list > kMaxHandshakeSize) {
1032 max_cert_list = kMaxHandshakeSize;
1033 }
1034 ssl->max_cert_list = (uint32_t)max_cert_list;
1035}
1036
1037void SSL_CTX_set_max_send_fragment(SSL_CTX *ctx, size_t max_send_fragment) {
1038 if (max_send_fragment < 512) {
1039 max_send_fragment = 512;
1040 }
1041 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1042 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1043 }
1044 ctx->max_send_fragment = (uint16_t)max_send_fragment;
1045}
1046
1047void SSL_set_max_send_fragment(SSL *ssl, size_t max_send_fragment) {
1048 if (max_send_fragment < 512) {
1049 max_send_fragment = 512;
1050 }
1051 if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1052 max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1053 }
1054 ssl->max_send_fragment = (uint16_t)max_send_fragment;
1055}
1056
Adam Langleyfcf25832014-12-18 17:42:32 -08001057long SSL_ctrl(SSL *s, int cmd, long larg, void *parg) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001058 switch (cmd) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001059 case SSL_CTRL_SET_MTU:
1060 if (larg < (long)dtls1_min_mtu()) {
1061 return 0;
1062 }
1063 if (SSL_IS_DTLS(s)) {
1064 s->d1->mtu = larg;
1065 return larg;
1066 }
1067 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001068
Adam Langleyfcf25832014-12-18 17:42:32 -08001069 case SSL_CTRL_GET_RI_SUPPORT:
1070 if (s->s3) {
1071 return s->s3->send_connection_binding;
1072 }
1073 return 0;
1074
Adam Langleyfcf25832014-12-18 17:42:32 -08001075 case SSL_CTRL_GET_RAW_CIPHERLIST:
1076 if (parg) {
1077 if (s->cert->ciphers_raw == NULL) {
1078 return 0;
1079 }
1080 *(uint8_t **)parg = s->cert->ciphers_raw;
1081 return (int)s->cert->ciphers_rawlen;
1082 }
1083
1084 /* Passing a NULL |parg| returns the size of a single
1085 * cipher suite value. */
1086 return 2;
1087
1088 default:
1089 return s->method->ssl_ctrl(s, cmd, larg, parg);
1090 }
1091}
1092
Adam Langleyfcf25832014-12-18 17:42:32 -08001093LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) { return ctx->sessions; }
1094
1095long SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg) {
1096 long l;
1097
1098 switch (cmd) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001099 case SSL_CTRL_SET_SESS_CACHE_SIZE:
1100 l = ctx->session_cache_size;
1101 ctx->session_cache_size = larg;
1102 return l;
1103
1104 case SSL_CTRL_GET_SESS_CACHE_SIZE:
1105 return ctx->session_cache_size;
1106
1107 case SSL_CTRL_SET_SESS_CACHE_MODE:
1108 l = ctx->session_cache_mode;
1109 ctx->session_cache_mode = larg;
1110 return l;
1111
1112 case SSL_CTRL_GET_SESS_CACHE_MODE:
1113 return ctx->session_cache_mode;
1114
1115 case SSL_CTRL_SESS_NUMBER:
1116 return lh_SSL_SESSION_num_items(ctx->sessions);
1117
Adam Langleyfcf25832014-12-18 17:42:32 -08001118 default:
1119 return ctx->method->ssl_ctx_ctrl(ctx, cmd, larg, parg);
1120 }
1121}
1122
Adam Langleyfcf25832014-12-18 17:42:32 -08001123int ssl_cipher_id_cmp(const void *in_a, const void *in_b) {
1124 long l;
1125 const SSL_CIPHER *a = in_a;
1126 const SSL_CIPHER *b = in_b;
1127 const long a_id = a->id;
1128 const long b_id = b->id;
1129
1130 l = a_id - b_id;
1131 if (l == 0L) {
1132 return 0;
1133 } else {
1134 return (l > 0) ? 1 : -1;
1135 }
1136}
1137
1138int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **ap, const SSL_CIPHER **bp) {
1139 long l;
1140 const long a_id = (*ap)->id;
1141 const long b_id = (*bp)->id;
1142
1143 l = a_id - b_id;
1144 if (l == 0) {
1145 return 0;
1146 } else {
1147 return (l > 0) ? 1 : -1;
1148 }
1149}
1150
1151/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001152 * preference */
Adam Langleyfcf25832014-12-18 17:42:32 -08001153STACK_OF(SSL_CIPHER) * SSL_get_ciphers(const SSL *s) {
1154 if (s == NULL) {
1155 return NULL;
1156 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001157
Adam Langleyfcf25832014-12-18 17:42:32 -08001158 if (s->cipher_list != NULL) {
1159 return s->cipher_list->ciphers;
1160 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001161
Adam Langleyfcf25832014-12-18 17:42:32 -08001162 if (s->version >= TLS1_1_VERSION && s->ctx != NULL &&
1163 s->ctx->cipher_list_tls11 != NULL) {
1164 return s->ctx->cipher_list_tls11->ciphers;
1165 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001166
Adam Langleyfcf25832014-12-18 17:42:32 -08001167 if (s->ctx != NULL && s->ctx->cipher_list != NULL) {
1168 return s->ctx->cipher_list->ciphers;
1169 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001170
Adam Langleyfcf25832014-12-18 17:42:32 -08001171 return NULL;
1172}
Adam Langley95c29f32014-06-20 12:00:00 -07001173
Adam Langleyfcf25832014-12-18 17:42:32 -08001174/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001175 * algorithm id */
Adam Langleyfcf25832014-12-18 17:42:32 -08001176STACK_OF(SSL_CIPHER) * ssl_get_ciphers_by_id(SSL *s) {
1177 if (s == NULL) {
1178 return NULL;
1179 }
Adam Langley95c29f32014-06-20 12:00:00 -07001180
Adam Langleyfcf25832014-12-18 17:42:32 -08001181 if (s->cipher_list_by_id != NULL) {
1182 return s->cipher_list_by_id;
1183 }
Adam Langley95c29f32014-06-20 12:00:00 -07001184
Adam Langleyfcf25832014-12-18 17:42:32 -08001185 if (s->ctx != NULL && s->ctx->cipher_list_by_id != NULL) {
1186 return s->ctx->cipher_list_by_id;
1187 }
Adam Langley95c29f32014-06-20 12:00:00 -07001188
Adam Langleyfcf25832014-12-18 17:42:32 -08001189 return NULL;
1190}
Adam Langley95c29f32014-06-20 12:00:00 -07001191
Adam Langleyfcf25832014-12-18 17:42:32 -08001192/* The old interface to get the same thing as SSL_get_ciphers() */
1193const char *SSL_get_cipher_list(const SSL *s, int n) {
1194 const SSL_CIPHER *c;
1195 STACK_OF(SSL_CIPHER) * sk;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001196
Adam Langleyfcf25832014-12-18 17:42:32 -08001197 if (s == NULL) {
1198 return NULL;
1199 }
Adam Langley95c29f32014-06-20 12:00:00 -07001200
Adam Langleyfcf25832014-12-18 17:42:32 -08001201 sk = SSL_get_ciphers(s);
1202 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
1203 return NULL;
1204 }
Adam Langley95c29f32014-06-20 12:00:00 -07001205
Adam Langleyfcf25832014-12-18 17:42:32 -08001206 c = sk_SSL_CIPHER_value(sk, n);
1207 if (c == NULL) {
1208 return NULL;
1209 }
Adam Langley95c29f32014-06-20 12:00:00 -07001210
Adam Langleyfcf25832014-12-18 17:42:32 -08001211 return c->name;
1212}
David Benjamin5491e3f2014-09-29 19:33:09 -04001213
Adam Langleyfcf25832014-12-18 17:42:32 -08001214/* specify the ciphers to be used by default by the SSL_CTX */
1215int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
1216 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001217
Adam Langleyfcf25832014-12-18 17:42:32 -08001218 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001219 &ctx->cipher_list_by_id, str);
Adam Langleyfcf25832014-12-18 17:42:32 -08001220 /* ssl_create_cipher_list may return an empty stack if it was unable to find
1221 * a cipher matching the given rule string (for example if the rule string
1222 * specifies a cipher which has been disabled). This is not an error as far
1223 * as ssl_create_cipher_list is concerned, and hence ctx->cipher_list and
1224 * ctx->cipher_list_by_id has been updated. */
1225 if (sk == NULL) {
1226 return 0;
1227 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1228 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1229 return 0;
1230 }
Adam Langley95c29f32014-06-20 12:00:00 -07001231
Adam Langleyfcf25832014-12-18 17:42:32 -08001232 return 1;
1233}
David Benjamin39482a12014-07-20 13:30:15 -04001234
Adam Langleyfcf25832014-12-18 17:42:32 -08001235int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str) {
1236 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001237
David Benjamin71f07942015-04-08 02:36:59 -04001238 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str);
Adam Langleyfcf25832014-12-18 17:42:32 -08001239 if (sk == NULL) {
1240 return 0;
1241 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1242 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list_tls11,
1243 SSL_R_NO_CIPHER_MATCH);
1244 return 0;
1245 }
David Benjamin9f2c0d72014-10-21 22:00:19 -04001246
Adam Langleyfcf25832014-12-18 17:42:32 -08001247 return 1;
1248}
Adam Langley95c29f32014-06-20 12:00:00 -07001249
Adam Langleyfcf25832014-12-18 17:42:32 -08001250/* specify the ciphers to be used by the SSL */
1251int SSL_set_cipher_list(SSL *s, const char *str) {
1252 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001253
Adam Langleyfcf25832014-12-18 17:42:32 -08001254 sk = ssl_create_cipher_list(s->ctx->method, &s->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001255 &s->cipher_list_by_id, str);
David Benjamin39482a12014-07-20 13:30:15 -04001256
Adam Langleyfcf25832014-12-18 17:42:32 -08001257 /* see comment in SSL_CTX_set_cipher_list */
1258 if (sk == NULL) {
1259 return 0;
1260 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1261 OPENSSL_PUT_ERROR(SSL, SSL_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1262 return 0;
1263 }
David Benjamin39482a12014-07-20 13:30:15 -04001264
Adam Langleyfcf25832014-12-18 17:42:32 -08001265 return 1;
1266}
Adam Langley95c29f32014-06-20 12:00:00 -07001267
Adam Langleyfcf25832014-12-18 17:42:32 -08001268int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, uint8_t *p) {
1269 size_t i;
1270 const SSL_CIPHER *c;
1271 CERT *ct = s->cert;
1272 uint8_t *q;
1273 /* Set disabled masks for this session */
1274 ssl_set_client_disabled(s);
Adam Langley29707792014-06-20 12:00:00 -07001275
Adam Langleyfcf25832014-12-18 17:42:32 -08001276 if (sk == NULL) {
1277 return 0;
1278 }
1279 q = p;
Adam Langley95c29f32014-06-20 12:00:00 -07001280
Adam Langleyfcf25832014-12-18 17:42:32 -08001281 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
1282 c = sk_SSL_CIPHER_value(sk, i);
1283 /* Skip disabled ciphers */
1284 if (c->algorithm_ssl & ct->mask_ssl ||
1285 c->algorithm_mkey & ct->mask_k ||
1286 c->algorithm_auth & ct->mask_a) {
1287 continue;
1288 }
1289 s2n(ssl3_get_cipher_value(c), p);
1290 }
1291
1292 /* If all ciphers were disabled, return the error to the caller. */
1293 if (p == q) {
1294 return 0;
1295 }
1296
1297 /* Add SCSVs. */
1298 if (!s->renegotiate) {
1299 s2n(SSL3_CK_SCSV & 0xffff, p);
1300 }
1301
Adam Langley5f0efe02015-02-20 13:03:16 -08001302 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001303 s2n(SSL3_CK_FALLBACK_SCSV & 0xffff, p);
1304 }
1305
1306 return p - q;
1307}
1308
1309STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs) {
1310 CBS cipher_suites = *cbs;
1311 const SSL_CIPHER *c;
1312 STACK_OF(SSL_CIPHER) * sk;
1313
1314 if (s->s3) {
1315 s->s3->send_connection_binding = 0;
1316 }
1317
1318 if (CBS_len(&cipher_suites) % 2 != 0) {
1319 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1320 SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
1321 return NULL;
1322 }
1323
1324 sk = sk_SSL_CIPHER_new_null();
1325 if (sk == NULL) {
1326 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1327 goto err;
1328 }
1329
1330 if (!CBS_stow(&cipher_suites, &s->cert->ciphers_raw,
1331 &s->cert->ciphers_rawlen)) {
1332 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1333 goto err;
1334 }
1335
1336 while (CBS_len(&cipher_suites) > 0) {
1337 uint16_t cipher_suite;
1338
1339 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
1340 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_INTERNAL_ERROR);
1341 goto err;
1342 }
1343
1344 /* Check for SCSV. */
1345 if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff)) {
1346 /* SCSV is fatal if renegotiating. */
1347 if (s->renegotiate) {
1348 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1349 SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1350 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1351 goto err;
1352 }
1353 s->s3->send_connection_binding = 1;
1354 continue;
1355 }
1356
1357 /* Check for FALLBACK_SCSV. */
1358 if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff)) {
1359 uint16_t max_version = ssl3_get_max_server_version(s);
1360 if (SSL_IS_DTLS(s) ? (uint16_t)s->version > max_version
1361 : (uint16_t)s->version < max_version) {
1362 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1363 SSL_R_INAPPROPRIATE_FALLBACK);
1364 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
1365 goto err;
1366 }
1367 continue;
1368 }
1369
1370 c = ssl3_get_cipher_by_value(cipher_suite);
1371 if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
1372 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1373 goto err;
1374 }
1375 }
1376
1377 return sk;
David Benjamin9f2c0d72014-10-21 22:00:19 -04001378
Adam Langley95c29f32014-06-20 12:00:00 -07001379err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001380 sk_SSL_CIPHER_free(sk);
Adam Langleyfcf25832014-12-18 17:42:32 -08001381 return NULL;
1382}
Adam Langley95c29f32014-06-20 12:00:00 -07001383
1384
Adam Langleyfcf25832014-12-18 17:42:32 -08001385/* return a servername extension value if provided in Client Hello, or NULL. So
1386 * far, only host_name types are defined (RFC 3546). */
1387const char *SSL_get_servername(const SSL *s, const int type) {
1388 if (type != TLSEXT_NAMETYPE_host_name) {
1389 return NULL;
1390 }
Adam Langley95c29f32014-06-20 12:00:00 -07001391
Adam Langleyfcf25832014-12-18 17:42:32 -08001392 return s->session && !s->tlsext_hostname ? s->session->tlsext_hostname
1393 : s->tlsext_hostname;
1394}
Adam Langley95c29f32014-06-20 12:00:00 -07001395
Adam Langleyfcf25832014-12-18 17:42:32 -08001396int SSL_get_servername_type(const SSL *s) {
1397 if (s->session &&
1398 (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname)) {
1399 return TLSEXT_NAMETYPE_host_name;
1400 }
Adam Langley95c29f32014-06-20 12:00:00 -07001401
Adam Langleyfcf25832014-12-18 17:42:32 -08001402 return -1;
1403}
Adam Langley95c29f32014-06-20 12:00:00 -07001404
Adam Langleyfcf25832014-12-18 17:42:32 -08001405void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
1406 ctx->signed_cert_timestamps_enabled = 1;
1407}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001408
Adam Langleyfcf25832014-12-18 17:42:32 -08001409int SSL_enable_signed_cert_timestamps(SSL *ssl) {
1410 ssl->signed_cert_timestamps_enabled = 1;
1411 return 1;
1412}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001413
Adam Langleyfcf25832014-12-18 17:42:32 -08001414void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
1415 ctx->ocsp_stapling_enabled = 1;
1416}
David Benjamin6c7aed02014-08-27 16:42:38 -04001417
Adam Langleyfcf25832014-12-18 17:42:32 -08001418int SSL_enable_ocsp_stapling(SSL *ssl) {
1419 ssl->ocsp_stapling_enabled = 1;
1420 return 1;
1421}
David Benjamin6c7aed02014-08-27 16:42:38 -04001422
Adam Langleyfcf25832014-12-18 17:42:32 -08001423void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
1424 size_t *out_len) {
1425 SSL_SESSION *session = ssl->session;
Adam Langley3cb50e02014-08-26 14:00:31 -07001426
Adam Langleyfcf25832014-12-18 17:42:32 -08001427 *out_len = 0;
1428 *out = NULL;
1429 if (ssl->server || !session || !session->tlsext_signed_cert_timestamp_list) {
1430 return;
1431 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001432
Adam Langleyfcf25832014-12-18 17:42:32 -08001433 *out = session->tlsext_signed_cert_timestamp_list;
1434 *out_len = session->tlsext_signed_cert_timestamp_list_length;
1435}
David Benjamin6c7aed02014-08-27 16:42:38 -04001436
Adam Langleyfcf25832014-12-18 17:42:32 -08001437void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
1438 size_t *out_len) {
1439 SSL_SESSION *session = ssl->session;
1440
1441 *out_len = 0;
1442 *out = NULL;
1443 if (ssl->server || !session || !session->ocsp_response) {
1444 return;
1445 }
1446 *out = session->ocsp_response;
1447 *out_len = session->ocsp_response_length;
1448}
David Benjamin6c7aed02014-08-27 16:42:38 -04001449
Adam Langley95c29f32014-06-20 12:00:00 -07001450/* SSL_select_next_proto implements the standard protocol selection. It is
1451 * expected that this function is called from the callback set by
1452 * SSL_CTX_set_next_proto_select_cb.
1453 *
1454 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1455 * strings. The length byte itself is not included in the length. A byte
1456 * string of length 0 is invalid. No byte string may be truncated.
1457 *
1458 * The current, but experimental algorithm for selecting the protocol is:
1459 *
1460 * 1) If the server doesn't support NPN then this is indicated to the
1461 * callback. In this case, the client application has to abort the connection
1462 * or have a default application level protocol.
1463 *
1464 * 2) If the server supports NPN, but advertises an empty list then the
1465 * client selects the first protcol in its list, but indicates via the
1466 * API that this fallback case was enacted.
1467 *
1468 * 3) Otherwise, the client finds the first protocol in the server's list
1469 * that it supports and selects this protocol. This is because it's
1470 * assumed that the server has better information about which protocol
1471 * a client should use.
1472 *
1473 * 4) If the client doesn't support any of the server's advertised
1474 * protocols, then this is treated the same as case 2.
1475 *
1476 * It returns either
1477 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1478 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1479 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001480int SSL_select_next_proto(uint8_t **out, uint8_t *outlen, const uint8_t *server,
1481 unsigned int server_len, const uint8_t *client,
1482 unsigned int client_len) {
1483 unsigned int i, j;
1484 const uint8_t *result;
1485 int status = OPENSSL_NPN_UNSUPPORTED;
Adam Langley95c29f32014-06-20 12:00:00 -07001486
Adam Langleyfcf25832014-12-18 17:42:32 -08001487 /* For each protocol in server preference order, see if we support it. */
1488 for (i = 0; i < server_len;) {
1489 for (j = 0; j < client_len;) {
1490 if (server[i] == client[j] &&
1491 memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
1492 /* We found a match */
1493 result = &server[i];
1494 status = OPENSSL_NPN_NEGOTIATED;
1495 goto found;
1496 }
1497 j += client[j];
1498 j++;
1499 }
1500 i += server[i];
1501 i++;
1502 }
Adam Langley95c29f32014-06-20 12:00:00 -07001503
Adam Langleyfcf25832014-12-18 17:42:32 -08001504 /* There's no overlap between our protocols and the server's list. */
1505 result = client;
1506 status = OPENSSL_NPN_NO_OVERLAP;
Adam Langley95c29f32014-06-20 12:00:00 -07001507
Adam Langleyfcf25832014-12-18 17:42:32 -08001508found:
1509 *out = (uint8_t *)result + 1;
1510 *outlen = result[0];
1511 return status;
1512}
Adam Langley95c29f32014-06-20 12:00:00 -07001513
Adam Langley95c29f32014-06-20 12:00:00 -07001514/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
1515 * requested protocol for this connection and returns 0. If the client didn't
1516 * request any protocol, then *data is set to NULL.
1517 *
1518 * Note that the client can request any protocol it chooses. The value returned
1519 * from this function need not be a member of the list of supported protocols
Adam Langleyfcf25832014-12-18 17:42:32 -08001520 * provided by the callback. */
1521void SSL_get0_next_proto_negotiated(const SSL *s, const uint8_t **data,
1522 unsigned *len) {
1523 *data = s->next_proto_negotiated;
1524 if (!*data) {
1525 *len = 0;
1526 } else {
1527 *len = s->next_proto_negotiated_len;
1528 }
Adam Langley95c29f32014-06-20 12:00:00 -07001529}
1530
1531/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
1532 * TLS server needs a list of supported protocols for Next Protocol
1533 * Negotiation. The returned list must be in wire format. The list is returned
1534 * by setting |out| to point to it and |outlen| to its length. This memory will
1535 * not be modified, but one should assume that the SSL* keeps a reference to
1536 * it.
1537 *
Adam Langleyfcf25832014-12-18 17:42:32 -08001538 * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise.
1539 * Otherwise, no such extension will be included in the ServerHello. */
1540void SSL_CTX_set_next_protos_advertised_cb(
1541 SSL_CTX *ctx,
1542 int (*cb)(SSL *ssl, const uint8_t **out, unsigned int *outlen, void *arg),
1543 void *arg) {
1544 ctx->next_protos_advertised_cb = cb;
1545 ctx->next_protos_advertised_cb_arg = arg;
1546}
Adam Langley95c29f32014-06-20 12:00:00 -07001547
1548/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1549 * client needs to select a protocol from the server's provided list. |out|
1550 * must be set to point to the selected protocol (which may be within |in|).
1551 * The length of the protocol name must be written into |outlen|. The server's
1552 * advertised protocols are provided in |in| and |inlen|. The callback can
1553 * assume that |in| is syntactically valid.
1554 *
1555 * The client must select a protocol. It is fatal to the connection if this
1556 * callback returns a value other than SSL_TLSEXT_ERR_OK.
1557 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001558void SSL_CTX_set_next_proto_select_cb(
1559 SSL_CTX *ctx, int (*cb)(SSL *s, uint8_t **out, uint8_t *outlen,
1560 const uint8_t *in, unsigned int inlen, void *arg),
1561 void *arg) {
1562 ctx->next_proto_select_cb = cb;
1563 ctx->next_proto_select_cb_arg = arg;
1564}
Adam Langley95c29f32014-06-20 12:00:00 -07001565
Adam Langleyfcf25832014-12-18 17:42:32 -08001566int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
1567 unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001568 OPENSSL_free(ctx->alpn_client_proto_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001569 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1570 if (!ctx->alpn_client_proto_list) {
1571 return 1;
1572 }
1573 ctx->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001574
Adam Langleyfcf25832014-12-18 17:42:32 -08001575 return 0;
1576}
Adam Langley95c29f32014-06-20 12:00:00 -07001577
Adam Langleyfcf25832014-12-18 17:42:32 -08001578int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001579 OPENSSL_free(ssl->alpn_client_proto_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001580 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1581 if (!ssl->alpn_client_proto_list) {
1582 return 1;
1583 }
1584 ssl->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001585
Adam Langleyfcf25832014-12-18 17:42:32 -08001586 return 0;
1587}
Adam Langley95c29f32014-06-20 12:00:00 -07001588
1589/* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
1590 * during ClientHello processing in order to select an ALPN protocol from the
1591 * client's list of offered protocols. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001592void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
1593 int (*cb)(SSL *ssl, const uint8_t **out,
1594 uint8_t *outlen, const uint8_t *in,
1595 unsigned int inlen, void *arg),
1596 void *arg) {
1597 ctx->alpn_select_cb = cb;
1598 ctx->alpn_select_cb_arg = arg;
1599}
Adam Langley95c29f32014-06-20 12:00:00 -07001600
1601/* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
1602 * On return it sets |*data| to point to |*len| bytes of protocol name (not
1603 * including the leading length-prefix byte). If the server didn't respond with
1604 * a negotiated protocol then |*len| will be zero. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001605void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **data,
1606 unsigned *len) {
1607 *data = NULL;
1608 if (ssl->s3) {
1609 *data = ssl->s3->alpn_selected;
1610 }
1611 if (*data == NULL) {
1612 *len = 0;
1613 } else {
1614 *len = ssl->s3->alpn_selected_len;
1615 }
1616}
Adam Langley95c29f32014-06-20 12:00:00 -07001617
David Benjamincfd248b2015-04-03 11:02:24 -04001618int SSL_export_keying_material(SSL *s, uint8_t *out, size_t out_len,
1619 const char *label, size_t label_len,
1620 const uint8_t *context, size_t context_len,
1621 int use_context) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001622 if (s->version < TLS1_VERSION) {
David Benjaminc565ebb2015-04-03 04:06:36 -04001623 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001624 }
Adam Langley95c29f32014-06-20 12:00:00 -07001625
David Benjamincfd248b2015-04-03 11:02:24 -04001626 return s->enc_method->export_keying_material(
1627 s, out, out_len, label, label_len, context, context_len, use_context);
Adam Langleyfcf25832014-12-18 17:42:32 -08001628}
Adam Langley95c29f32014-06-20 12:00:00 -07001629
Adam Langleyfcf25832014-12-18 17:42:32 -08001630static uint32_t ssl_session_hash(const SSL_SESSION *a) {
1631 uint32_t hash =
1632 ((uint32_t)a->session_id[0]) ||
1633 ((uint32_t)a->session_id[1] << 8) ||
1634 ((uint32_t)a->session_id[2] << 16) ||
1635 ((uint32_t)a->session_id[3] << 24);
Adam Langley95c29f32014-06-20 12:00:00 -07001636
Adam Langleyfcf25832014-12-18 17:42:32 -08001637 return hash;
1638}
Adam Langley95c29f32014-06-20 12:00:00 -07001639
1640/* NB: If this function (or indeed the hash function which uses a sort of
1641 * coarser function than this one) is changed, ensure
1642 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1643 * able to construct an SSL_SESSION that will collide with any existing session
1644 * with a matching session ID. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001645static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
1646 if (a->ssl_version != b->ssl_version) {
1647 return 1;
1648 }
Adam Langley95c29f32014-06-20 12:00:00 -07001649
Adam Langleyfcf25832014-12-18 17:42:32 -08001650 if (a->session_id_length != b->session_id_length) {
1651 return 1;
1652 }
Adam Langley95c29f32014-06-20 12:00:00 -07001653
Adam Langleyfcf25832014-12-18 17:42:32 -08001654 return memcmp(a->session_id, b->session_id, a->session_id_length);
1655}
Adam Langley95c29f32014-06-20 12:00:00 -07001656
Adam Langleyfcf25832014-12-18 17:42:32 -08001657SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth) {
1658 SSL_CTX *ret = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001659
Adam Langleyfcf25832014-12-18 17:42:32 -08001660 if (meth == NULL) {
1661 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_NULL_SSL_METHOD_PASSED);
1662 return NULL;
1663 }
Adam Langley95c29f32014-06-20 12:00:00 -07001664
Adam Langleyfcf25832014-12-18 17:42:32 -08001665 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
1666 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
1667 goto err;
1668 }
Adam Langley95c29f32014-06-20 12:00:00 -07001669
Adam Langleyfcf25832014-12-18 17:42:32 -08001670 ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1671 if (ret == NULL) {
1672 goto err;
1673 }
Adam Langley95c29f32014-06-20 12:00:00 -07001674
Adam Langleyfcf25832014-12-18 17:42:32 -08001675 memset(ret, 0, sizeof(SSL_CTX));
Adam Langley95c29f32014-06-20 12:00:00 -07001676
Adam Langleyfcf25832014-12-18 17:42:32 -08001677 ret->method = meth->method;
Adam Langley95c29f32014-06-20 12:00:00 -07001678
Adam Langleyfcf25832014-12-18 17:42:32 -08001679 ret->cert_store = NULL;
1680 ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
1681 ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1682 ret->session_cache_head = NULL;
1683 ret->session_cache_tail = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001684
Adam Langleyfcf25832014-12-18 17:42:32 -08001685 /* We take the system default */
1686 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001687
Adam Langleyfcf25832014-12-18 17:42:32 -08001688 ret->new_session_cb = 0;
1689 ret->remove_session_cb = 0;
1690 ret->get_session_cb = 0;
1691 ret->generate_session_id = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001692
Adam Langleyfcf25832014-12-18 17:42:32 -08001693 ret->references = 1;
1694 ret->quiet_shutdown = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001695
Adam Langleyfcf25832014-12-18 17:42:32 -08001696 ret->info_callback = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001697
Adam Langleyfcf25832014-12-18 17:42:32 -08001698 ret->app_verify_callback = 0;
1699 ret->app_verify_arg = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001700
Adam Langleyfcf25832014-12-18 17:42:32 -08001701 ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
1702 ret->read_ahead = 0;
1703 ret->msg_callback = 0;
1704 ret->msg_callback_arg = NULL;
1705 ret->verify_mode = SSL_VERIFY_NONE;
1706 ret->sid_ctx_length = 0;
1707 ret->default_verify_callback = NULL;
1708 ret->cert = ssl_cert_new();
1709 if (ret->cert == NULL) {
1710 goto err;
1711 }
Adam Langley95c29f32014-06-20 12:00:00 -07001712
Adam Langleyfcf25832014-12-18 17:42:32 -08001713 ret->default_passwd_callback = 0;
1714 ret->default_passwd_callback_userdata = NULL;
1715 ret->client_cert_cb = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001716
Adam Langleyfcf25832014-12-18 17:42:32 -08001717 ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
1718 if (ret->sessions == NULL) {
1719 goto err;
1720 }
1721 ret->cert_store = X509_STORE_new();
1722 if (ret->cert_store == NULL) {
1723 goto err;
1724 }
Adam Langley95c29f32014-06-20 12:00:00 -07001725
Adam Langleyfcf25832014-12-18 17:42:32 -08001726 ssl_create_cipher_list(ret->method, &ret->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001727 &ret->cipher_list_by_id, SSL_DEFAULT_CIPHER_LIST);
Adam Langleyfcf25832014-12-18 17:42:32 -08001728 if (ret->cipher_list == NULL ||
1729 sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0) {
1730 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_LIBRARY_HAS_NO_CIPHERS);
1731 goto err2;
1732 }
Adam Langley95c29f32014-06-20 12:00:00 -07001733
Adam Langleyfcf25832014-12-18 17:42:32 -08001734 ret->param = X509_VERIFY_PARAM_new();
1735 if (!ret->param) {
1736 goto err;
1737 }
Adam Langley95c29f32014-06-20 12:00:00 -07001738
Adam Langleyfcf25832014-12-18 17:42:32 -08001739 ret->client_CA = sk_X509_NAME_new_null();
1740 if (ret->client_CA == NULL) {
1741 goto err;
1742 }
Adam Langley95c29f32014-06-20 12:00:00 -07001743
David Benjamin9f33fc62015-04-15 17:29:53 -04001744 CRYPTO_new_ex_data(&g_ex_data_class_ssl_ctx, ret, &ret->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001745
Adam Langleyfcf25832014-12-18 17:42:32 -08001746 ret->extra_certs = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001747
Adam Langleyfcf25832014-12-18 17:42:32 -08001748 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
David Benjamin422d3a42014-08-20 11:09:03 -04001749
Adam Langleyfcf25832014-12-18 17:42:32 -08001750 ret->tlsext_servername_callback = 0;
1751 ret->tlsext_servername_arg = NULL;
1752 /* Setup RFC4507 ticket keys */
1753 if (!RAND_bytes(ret->tlsext_tick_key_name, 16) ||
1754 !RAND_bytes(ret->tlsext_tick_hmac_key, 16) ||
1755 !RAND_bytes(ret->tlsext_tick_aes_key, 16)) {
1756 ret->options |= SSL_OP_NO_TICKET;
1757 }
Adam Langley95c29f32014-06-20 12:00:00 -07001758
Adam Langleyfcf25832014-12-18 17:42:32 -08001759 ret->next_protos_advertised_cb = 0;
1760 ret->next_proto_select_cb = 0;
1761 ret->psk_identity_hint = NULL;
1762 ret->psk_client_callback = NULL;
1763 ret->psk_server_callback = NULL;
1764
1765 /* Default is to connect to non-RI servers. When RI is more widely deployed
1766 * might change this. */
1767 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
1768
1769 /* Lock the SSL_CTX to the specified version, for compatibility with legacy
1770 * uses of SSL_METHOD. */
1771 if (meth->version != 0) {
1772 SSL_CTX_set_max_version(ret, meth->version);
1773 SSL_CTX_set_min_version(ret, meth->version);
1774 }
1775
1776 return ret;
1777
Adam Langley95c29f32014-06-20 12:00:00 -07001778err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001779 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -07001780err2:
David Benjamin2755a3e2015-04-22 16:17:58 -04001781 SSL_CTX_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -08001782 return NULL;
1783}
Adam Langley95c29f32014-06-20 12:00:00 -07001784
David Benjamin4fcc2e22015-04-22 12:58:16 -04001785void SSL_CTX_free(SSL_CTX *ctx) {
1786 if (ctx == NULL ||
1787 CRYPTO_add(&ctx->references, -1, CRYPTO_LOCK_SSL_CTX) > 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001788 return;
1789 }
Adam Langley95c29f32014-06-20 12:00:00 -07001790
David Benjamin2755a3e2015-04-22 16:17:58 -04001791 X509_VERIFY_PARAM_free(ctx->param);
Adam Langley95c29f32014-06-20 12:00:00 -07001792
Adam Langleyfcf25832014-12-18 17:42:32 -08001793 /* Free internal session cache. However: the remove_cb() may reference the
1794 * ex_data of SSL_CTX, thus the ex_data store can only be removed after the
1795 * sessions were flushed. As the ex_data handling routines might also touch
1796 * the session cache, the most secure solution seems to be: empty (flush) the
1797 * cache, then free ex_data, then finally free the cache. (See ticket
1798 * [openssl.org #212].) */
David Benjamin2755a3e2015-04-22 16:17:58 -04001799 SSL_CTX_flush_sessions(ctx, 0);
Adam Langley95c29f32014-06-20 12:00:00 -07001800
David Benjamin4fcc2e22015-04-22 12:58:16 -04001801 CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, ctx, &ctx->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001802
David Benjamin2755a3e2015-04-22 16:17:58 -04001803 lh_SSL_SESSION_free(ctx->sessions);
1804 X509_STORE_free(ctx->cert_store);
1805 ssl_cipher_preference_list_free(ctx->cipher_list);
1806 sk_SSL_CIPHER_free(ctx->cipher_list_by_id);
1807 ssl_cipher_preference_list_free(ctx->cipher_list_tls11);
1808 ssl_cert_free(ctx->cert);
1809 sk_X509_NAME_pop_free(ctx->client_CA, X509_NAME_free);
1810 sk_X509_pop_free(ctx->extra_certs, X509_free);
1811 sk_SRTP_PROTECTION_PROFILE_free(ctx->srtp_profiles);
1812 OPENSSL_free(ctx->psk_identity_hint);
1813 OPENSSL_free(ctx->tlsext_ecpointformatlist);
1814 OPENSSL_free(ctx->tlsext_ellipticcurvelist);
1815 OPENSSL_free(ctx->alpn_client_proto_list);
1816 EVP_PKEY_free(ctx->tlsext_channel_id_private);
1817 BIO_free(ctx->keylog_bio);
Adam Langley95c29f32014-06-20 12:00:00 -07001818
David Benjamin4fcc2e22015-04-22 12:58:16 -04001819 OPENSSL_free(ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08001820}
Adam Langley95c29f32014-06-20 12:00:00 -07001821
Adam Langleyfcf25832014-12-18 17:42:32 -08001822void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) {
1823 ctx->default_passwd_callback = cb;
1824}
Adam Langley95c29f32014-06-20 12:00:00 -07001825
Adam Langleyfcf25832014-12-18 17:42:32 -08001826void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u) {
1827 ctx->default_passwd_callback_userdata = u;
1828}
Adam Langley95c29f32014-06-20 12:00:00 -07001829
Adam Langleyfcf25832014-12-18 17:42:32 -08001830void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
1831 int (*cb)(X509_STORE_CTX *, void *),
1832 void *arg) {
1833 ctx->app_verify_callback = cb;
1834 ctx->app_verify_arg = arg;
1835}
Adam Langley95c29f32014-06-20 12:00:00 -07001836
Adam Langleyfcf25832014-12-18 17:42:32 -08001837void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
1838 int (*cb)(int, X509_STORE_CTX *)) {
1839 ctx->verify_mode = mode;
1840 ctx->default_verify_callback = cb;
1841}
Adam Langley95c29f32014-06-20 12:00:00 -07001842
Adam Langleyfcf25832014-12-18 17:42:32 -08001843void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
1844 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
1845}
Adam Langley1258b6a2014-06-20 12:00:00 -07001846
Adam Langleyfcf25832014-12-18 17:42:32 -08001847void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb)(SSL *ssl, void *arg),
1848 void *arg) {
1849 ssl_cert_set_cert_cb(c->cert, cb, arg);
1850}
David Benjamin859ec3c2014-09-02 16:29:36 -04001851
Adam Langleyfcf25832014-12-18 17:42:32 -08001852void SSL_set_cert_cb(SSL *s, int (*cb)(SSL *ssl, void *arg), void *arg) {
1853 ssl_cert_set_cert_cb(s->cert, cb, arg);
1854}
Adam Langley95c29f32014-06-20 12:00:00 -07001855
Adam Langleyfcf25832014-12-18 17:42:32 -08001856static int ssl_has_key(SSL *s, size_t idx) {
1857 CERT_PKEY *cpk = &s->cert->pkeys[idx];
1858 return cpk->x509 && cpk->privatekey;
1859}
David Benjamin033e5f42014-11-13 18:47:41 -05001860
David Benjamin107db582015-04-08 00:41:59 -04001861void ssl_get_compatible_server_ciphers(SSL *s, uint32_t *out_mask_k,
1862 uint32_t *out_mask_a) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001863 CERT *c = s->cert;
1864 int rsa_enc, rsa_sign, dh_tmp;
David Benjamin107db582015-04-08 00:41:59 -04001865 uint32_t mask_k, mask_a;
Adam Langleyfcf25832014-12-18 17:42:32 -08001866 int have_ecc_cert, ecdsa_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001867 X509 *x;
Adam Langley95c29f32014-06-20 12:00:00 -07001868
Adam Langleyfcf25832014-12-18 17:42:32 -08001869 if (c == NULL) {
1870 /* TODO(davidben): Is this codepath possible? */
1871 *out_mask_k = 0;
1872 *out_mask_a = 0;
1873 return;
1874 }
Adam Langley95c29f32014-06-20 12:00:00 -07001875
Adam Langleyfcf25832014-12-18 17:42:32 -08001876 dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001877
Adam Langleyfcf25832014-12-18 17:42:32 -08001878 rsa_enc = ssl_has_key(s, SSL_PKEY_RSA_ENC);
1879 rsa_sign = ssl_has_key(s, SSL_PKEY_RSA_SIGN);
1880 have_ecc_cert = ssl_has_key(s, SSL_PKEY_ECC);
1881 mask_k = 0;
1882 mask_a = 0;
David Benjaminf31e6812014-11-13 18:05:55 -05001883
Adam Langleyfcf25832014-12-18 17:42:32 -08001884 if (rsa_enc) {
1885 mask_k |= SSL_kRSA;
1886 }
1887 if (dh_tmp) {
David Benjamin7061e282015-03-19 11:10:48 -04001888 mask_k |= SSL_kDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001889 }
1890 if (rsa_enc || rsa_sign) {
1891 mask_a |= SSL_aRSA;
1892 }
Adam Langley95c29f32014-06-20 12:00:00 -07001893
Adam Langleyfcf25832014-12-18 17:42:32 -08001894 /* An ECC certificate may be usable for ECDSA cipher suites depending on the
1895 * key usage extension and on the client's curve preferences. */
1896 if (have_ecc_cert) {
1897 x = c->pkeys[SSL_PKEY_ECC].x509;
1898 /* This call populates extension flags (ex_flags). */
1899 X509_check_purpose(x, -1, 0);
1900 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE)
1901 ? (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)
1902 : 1;
1903 if (!tls1_check_ec_cert(s, x)) {
1904 ecdsa_ok = 0;
1905 }
1906 if (ecdsa_ok) {
1907 mask_a |= SSL_aECDSA;
1908 }
1909 }
Adam Langley95c29f32014-06-20 12:00:00 -07001910
Adam Langleyfcf25832014-12-18 17:42:32 -08001911 /* If we are considering an ECC cipher suite that uses an ephemeral EC
1912 * key, check it. */
David Benjamindd978782015-04-24 15:20:13 -04001913 if (tls1_check_ec_tmp_key(s)) {
David Benjamin7061e282015-03-19 11:10:48 -04001914 mask_k |= SSL_kECDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001915 }
Adam Langley95c29f32014-06-20 12:00:00 -07001916
Adam Langleyfcf25832014-12-18 17:42:32 -08001917 /* PSK requires a server callback. */
1918 if (s->psk_server_callback != NULL) {
1919 mask_k |= SSL_kPSK;
1920 mask_a |= SSL_aPSK;
1921 }
Adam Langley95c29f32014-06-20 12:00:00 -07001922
Adam Langleyfcf25832014-12-18 17:42:32 -08001923 *out_mask_k = mask_k;
1924 *out_mask_a = mask_a;
1925}
Adam Langley95c29f32014-06-20 12:00:00 -07001926
1927/* This handy macro borrowed from crypto/x509v3/v3_purp.c */
1928#define ku_reject(x, usage) \
Adam Langleyfcf25832014-12-18 17:42:32 -08001929 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
Adam Langley95c29f32014-06-20 12:00:00 -07001930
Adam Langleyfcf25832014-12-18 17:42:32 -08001931int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001932 const SSL_CIPHER *cs = s->s3->tmp.new_cipher;
David Benjamin3fa27772015-04-17 22:32:19 -04001933 uint32_t alg_a = cs->algorithm_auth;
1934 int signature_nid = 0, md_nid = 0, pk_nid = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001935
Adam Langleyfcf25832014-12-18 17:42:32 -08001936 /* This call populates the ex_flags field correctly */
1937 X509_check_purpose(x, -1, 0);
1938 if (x->sig_alg && x->sig_alg->algorithm) {
1939 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
1940 OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
1941 }
1942 if (alg_a & SSL_aECDSA) {
1943 /* key usage, if present, must allow signing */
1944 if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE)) {
1945 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg,
1946 SSL_R_ECC_CERT_NOT_FOR_SIGNING);
1947 return 0;
1948 }
1949 }
Adam Langley95c29f32014-06-20 12:00:00 -07001950
Adam Langleyfcf25832014-12-18 17:42:32 -08001951 return 1; /* all checks are ok */
1952}
Adam Langley95c29f32014-06-20 12:00:00 -07001953
Adam Langleyfcf25832014-12-18 17:42:32 -08001954static int ssl_get_server_cert_index(const SSL *s) {
1955 int idx;
1956 idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1957 if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509) {
1958 idx = SSL_PKEY_RSA_SIGN;
1959 }
1960 if (idx == -1) {
1961 OPENSSL_PUT_ERROR(SSL, ssl_get_server_cert_index, ERR_R_INTERNAL_ERROR);
1962 }
1963 return idx;
1964}
Adam Langley95c29f32014-06-20 12:00:00 -07001965
Adam Langleyfcf25832014-12-18 17:42:32 -08001966CERT_PKEY *ssl_get_server_send_pkey(const SSL *s) {
1967 int i = ssl_get_server_cert_index(s);
Adam Langley95c29f32014-06-20 12:00:00 -07001968
Adam Langleyfcf25832014-12-18 17:42:32 -08001969 /* This may or may not be an error. */
1970 if (i < 0) {
1971 return NULL;
1972 }
Adam Langley95c29f32014-06-20 12:00:00 -07001973
Adam Langleyfcf25832014-12-18 17:42:32 -08001974 /* May be NULL. */
1975 return &s->cert->pkeys[i];
1976}
Adam Langley95c29f32014-06-20 12:00:00 -07001977
Adam Langleyfcf25832014-12-18 17:42:32 -08001978EVP_PKEY *ssl_get_sign_pkey(SSL *s, const SSL_CIPHER *cipher) {
David Benjamin3fa27772015-04-17 22:32:19 -04001979 uint32_t alg_a = cipher->algorithm_auth;
1980 CERT *c = s->cert;
Adam Langleyfcf25832014-12-18 17:42:32 -08001981 int idx = -1;
Adam Langley95c29f32014-06-20 12:00:00 -07001982
Adam Langleyfcf25832014-12-18 17:42:32 -08001983 if (alg_a & SSL_aRSA) {
1984 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL) {
1985 idx = SSL_PKEY_RSA_SIGN;
1986 } else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL) {
1987 idx = SSL_PKEY_RSA_ENC;
1988 }
1989 } else if ((alg_a & SSL_aECDSA) &&
1990 (c->pkeys[SSL_PKEY_ECC].privatekey != NULL)) {
1991 idx = SSL_PKEY_ECC;
1992 }
Adam Langley95c29f32014-06-20 12:00:00 -07001993
Adam Langleyfcf25832014-12-18 17:42:32 -08001994 if (idx == -1) {
1995 OPENSSL_PUT_ERROR(SSL, ssl_get_sign_pkey, ERR_R_INTERNAL_ERROR);
1996 return NULL;
1997 }
Adam Langley95c29f32014-06-20 12:00:00 -07001998
Adam Langleyfcf25832014-12-18 17:42:32 -08001999 return c->pkeys[idx].privatekey;
2000}
Adam Langley95c29f32014-06-20 12:00:00 -07002001
Adam Langleyfcf25832014-12-18 17:42:32 -08002002void ssl_update_cache(SSL *s, int mode) {
David Benjaminb6d0c6d2015-03-19 19:07:26 -04002003 /* Never cache sessions with empty session IDs. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002004 if (s->session->session_id_length == 0) {
2005 return;
2006 }
Adam Langley95c29f32014-06-20 12:00:00 -07002007
David Benjaminb6d0c6d2015-03-19 19:07:26 -04002008 SSL_CTX *ctx = s->initial_ctx;
2009 if ((ctx->session_cache_mode & mode) == mode && !s->hit &&
2010 ((ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) ||
2011 SSL_CTX_add_session(ctx, s->session)) &&
2012 ctx->new_session_cb != NULL) {
2013 /* Note: |new_session_cb| is called whether the internal session cache is
2014 * used or not. */
2015 if (!ctx->new_session_cb(s, SSL_SESSION_up_ref(s->session))) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002016 SSL_SESSION_free(s->session);
2017 }
2018 }
Adam Langley95c29f32014-06-20 12:00:00 -07002019
David Benjaminb6d0c6d2015-03-19 19:07:26 -04002020 if (!(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR) &&
2021 !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) &&
2022 (ctx->session_cache_mode & mode) == mode) {
2023 /* Automatically flush the internal session cache every 255 connections. */
2024 int flush_cache = 0;
2025 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2026 ctx->handshakes_since_cache_flush++;
2027 if (ctx->handshakes_since_cache_flush >= 255) {
2028 flush_cache = 1;
2029 ctx->handshakes_since_cache_flush = 0;
2030 }
2031 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
2032
2033 if (flush_cache) {
2034 SSL_CTX_flush_sessions(ctx, (unsigned long)time(NULL));
Adam Langleyfcf25832014-12-18 17:42:32 -08002035 }
2036 }
2037}
Adam Langley95c29f32014-06-20 12:00:00 -07002038
David Benjamin1a5c50f2015-03-11 16:22:37 -04002039int SSL_get_error(const SSL *s, int ret_code) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002040 int reason;
David Benjamin1a5c50f2015-03-11 16:22:37 -04002041 uint32_t err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002042 BIO *bio;
Adam Langley95c29f32014-06-20 12:00:00 -07002043
David Benjamin1a5c50f2015-03-11 16:22:37 -04002044 if (ret_code > 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002045 return SSL_ERROR_NONE;
2046 }
Adam Langley95c29f32014-06-20 12:00:00 -07002047
Adam Langleyfcf25832014-12-18 17:42:32 -08002048 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
2049 * where we do encode the error */
David Benjamin1a5c50f2015-03-11 16:22:37 -04002050 err = ERR_peek_error();
2051 if (err != 0) {
2052 if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002053 return SSL_ERROR_SYSCALL;
2054 }
2055 return SSL_ERROR_SSL;
2056 }
Adam Langley95c29f32014-06-20 12:00:00 -07002057
David Benjamin1a5c50f2015-03-11 16:22:37 -04002058 if (ret_code == 0) {
David Benjamin9a38e922015-01-22 16:06:11 -05002059 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2060 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) {
2061 /* The socket was cleanly shut down with a close_notify. */
2062 return SSL_ERROR_ZERO_RETURN;
2063 }
2064 /* An EOF was observed which violates the protocol, and the underlying
2065 * transport does not participate in the error queue. Bubble up to the
2066 * caller. */
2067 return SSL_ERROR_SYSCALL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002068 }
Adam Langleyb2ce0582014-06-20 12:00:00 -07002069
Adam Langleyfcf25832014-12-18 17:42:32 -08002070 if (SSL_want_session(s)) {
2071 return SSL_ERROR_PENDING_SESSION;
2072 }
Adam Langley95c29f32014-06-20 12:00:00 -07002073
Adam Langleyfcf25832014-12-18 17:42:32 -08002074 if (SSL_want_certificate(s)) {
2075 return SSL_ERROR_PENDING_CERTIFICATE;
2076 }
Adam Langley95c29f32014-06-20 12:00:00 -07002077
Adam Langleyfcf25832014-12-18 17:42:32 -08002078 if (SSL_want_read(s)) {
2079 bio = SSL_get_rbio(s);
2080 if (BIO_should_read(bio)) {
2081 return SSL_ERROR_WANT_READ;
2082 }
Adam Langley95c29f32014-06-20 12:00:00 -07002083
Adam Langleyfcf25832014-12-18 17:42:32 -08002084 if (BIO_should_write(bio)) {
2085 /* This one doesn't make too much sense ... We never try to write to the
2086 * rbio, and an application program where rbio and wbio are separate
2087 * couldn't even know what it should wait for. However if we ever set
2088 * s->rwstate incorrectly (so that we have SSL_want_read(s) instead of
2089 * SSL_want_write(s)) and rbio and wbio *are* the same, this test works
2090 * around that bug; so it might be safer to keep it. */
2091 return SSL_ERROR_WANT_WRITE;
2092 }
Adam Langley95c29f32014-06-20 12:00:00 -07002093
Adam Langleyfcf25832014-12-18 17:42:32 -08002094 if (BIO_should_io_special(bio)) {
2095 reason = BIO_get_retry_reason(bio);
2096 if (reason == BIO_RR_CONNECT) {
2097 return SSL_ERROR_WANT_CONNECT;
2098 }
Adam Langley95c29f32014-06-20 12:00:00 -07002099
Adam Langleyfcf25832014-12-18 17:42:32 -08002100 if (reason == BIO_RR_ACCEPT) {
2101 return SSL_ERROR_WANT_ACCEPT;
2102 }
Adam Langley95c29f32014-06-20 12:00:00 -07002103
Adam Langleyfcf25832014-12-18 17:42:32 -08002104 return SSL_ERROR_SYSCALL; /* unknown */
2105 }
2106 }
Adam Langley95c29f32014-06-20 12:00:00 -07002107
Adam Langleyfcf25832014-12-18 17:42:32 -08002108 if (SSL_want_write(s)) {
2109 bio = SSL_get_wbio(s);
2110 if (BIO_should_write(bio)) {
2111 return SSL_ERROR_WANT_WRITE;
2112 }
Adam Langley95c29f32014-06-20 12:00:00 -07002113
Adam Langleyfcf25832014-12-18 17:42:32 -08002114 if (BIO_should_read(bio)) {
2115 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2116 return SSL_ERROR_WANT_READ;
2117 }
Adam Langley95c29f32014-06-20 12:00:00 -07002118
Adam Langleyfcf25832014-12-18 17:42:32 -08002119 if (BIO_should_io_special(bio)) {
2120 reason = BIO_get_retry_reason(bio);
2121 if (reason == BIO_RR_CONNECT) {
2122 return SSL_ERROR_WANT_CONNECT;
2123 }
Adam Langley95c29f32014-06-20 12:00:00 -07002124
Adam Langleyfcf25832014-12-18 17:42:32 -08002125 if (reason == BIO_RR_ACCEPT) {
2126 return SSL_ERROR_WANT_ACCEPT;
2127 }
Adam Langley95c29f32014-06-20 12:00:00 -07002128
Adam Langleyfcf25832014-12-18 17:42:32 -08002129 return SSL_ERROR_SYSCALL;
2130 }
2131 }
Adam Langley95c29f32014-06-20 12:00:00 -07002132
Adam Langleyfcf25832014-12-18 17:42:32 -08002133 if (SSL_want_x509_lookup(s)) {
2134 return SSL_ERROR_WANT_X509_LOOKUP;
2135 }
Adam Langley95c29f32014-06-20 12:00:00 -07002136
Adam Langleyfcf25832014-12-18 17:42:32 -08002137 if (SSL_want_channel_id_lookup(s)) {
2138 return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
2139 }
Adam Langley0f4746e2014-08-13 12:26:32 -07002140
Adam Langleyfcf25832014-12-18 17:42:32 -08002141 return SSL_ERROR_SYSCALL;
2142}
Adam Langley0f4746e2014-08-13 12:26:32 -07002143
Adam Langleyfcf25832014-12-18 17:42:32 -08002144int SSL_do_handshake(SSL *s) {
2145 int ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002146
Adam Langleyfcf25832014-12-18 17:42:32 -08002147 if (s->handshake_func == NULL) {
2148 OPENSSL_PUT_ERROR(SSL, SSL_do_handshake, SSL_R_CONNECTION_TYPE_NOT_SET);
2149 return -1;
2150 }
Adam Langley95c29f32014-06-20 12:00:00 -07002151
Adam Langleyfcf25832014-12-18 17:42:32 -08002152 s->method->ssl_renegotiate_check(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002153
Adam Langleyfcf25832014-12-18 17:42:32 -08002154 if (SSL_in_init(s)) {
2155 ret = s->handshake_func(s);
2156 }
2157 return ret;
2158}
Adam Langley95c29f32014-06-20 12:00:00 -07002159
Adam Langleyfcf25832014-12-18 17:42:32 -08002160void SSL_set_accept_state(SSL *s) {
2161 s->server = 1;
2162 s->shutdown = 0;
2163 s->state = SSL_ST_ACCEPT | SSL_ST_BEFORE;
2164 s->handshake_func = s->method->ssl_accept;
2165 /* clear the current cipher */
2166 ssl_clear_cipher_ctx(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002167}
Adam Langley95c29f32014-06-20 12:00:00 -07002168
Adam Langleyfcf25832014-12-18 17:42:32 -08002169void SSL_set_connect_state(SSL *s) {
2170 s->server = 0;
2171 s->shutdown = 0;
2172 s->state = SSL_ST_CONNECT | SSL_ST_BEFORE;
2173 s->handshake_func = s->method->ssl_connect;
2174 /* clear the current cipher */
2175 ssl_clear_cipher_ctx(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002176}
Adam Langley95c29f32014-06-20 12:00:00 -07002177
Adam Langleyfcf25832014-12-18 17:42:32 -08002178int ssl_undefined_function(SSL *s) {
2179 OPENSSL_PUT_ERROR(SSL, ssl_undefined_function,
2180 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2181 return 0;
2182}
Adam Langley95c29f32014-06-20 12:00:00 -07002183
Adam Langleyfcf25832014-12-18 17:42:32 -08002184int ssl_undefined_void_function(void) {
2185 OPENSSL_PUT_ERROR(SSL, ssl_undefined_void_function,
2186 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2187 return 0;
2188}
Adam Langley95c29f32014-06-20 12:00:00 -07002189
Adam Langleyfcf25832014-12-18 17:42:32 -08002190int ssl_undefined_const_function(const SSL *s) {
2191 OPENSSL_PUT_ERROR(SSL, ssl_undefined_const_function,
2192 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2193 return 0;
2194}
Adam Langley95c29f32014-06-20 12:00:00 -07002195
Adam Langleyfcf25832014-12-18 17:42:32 -08002196static const char *ssl_get_version(int version) {
2197 switch (version) {
2198 case TLS1_2_VERSION:
2199 return "TLSv1.2";
Adam Langley95c29f32014-06-20 12:00:00 -07002200
Adam Langleyfcf25832014-12-18 17:42:32 -08002201 case TLS1_1_VERSION:
2202 return "TLSv1.1";
Adam Langley95c29f32014-06-20 12:00:00 -07002203
Adam Langleyfcf25832014-12-18 17:42:32 -08002204 case TLS1_VERSION:
2205 return "TLSv1";
Adam Langley95c29f32014-06-20 12:00:00 -07002206
Adam Langleyfcf25832014-12-18 17:42:32 -08002207 case SSL3_VERSION:
2208 return "SSLv3";
Adam Langley95c29f32014-06-20 12:00:00 -07002209
David Benjamin1c722b72015-04-20 13:53:10 -04002210 case DTLS1_VERSION:
2211 return "DTLSv1";
2212
2213 case DTLS1_2_VERSION:
2214 return "DTLSv1.2";
2215
Adam Langleyfcf25832014-12-18 17:42:32 -08002216 default:
2217 return "unknown";
2218 }
2219}
Adam Langley95c29f32014-06-20 12:00:00 -07002220
Adam Langleyfcf25832014-12-18 17:42:32 -08002221const char *SSL_get_version(const SSL *s) {
2222 return ssl_get_version(s->version);
2223}
Adam Langley95c29f32014-06-20 12:00:00 -07002224
Adam Langleyfcf25832014-12-18 17:42:32 -08002225const char *SSL_SESSION_get_version(const SSL_SESSION *sess) {
2226 return ssl_get_version(sess->ssl_version);
2227}
Adam Langley95c29f32014-06-20 12:00:00 -07002228
Adam Langleyfcf25832014-12-18 17:42:32 -08002229void ssl_clear_cipher_ctx(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002230 if (s->aead_read_ctx != NULL) {
2231 EVP_AEAD_CTX_cleanup(&s->aead_read_ctx->ctx);
2232 OPENSSL_free(s->aead_read_ctx);
2233 s->aead_read_ctx = NULL;
2234 }
Adam Langleya5dc5452014-06-20 12:00:00 -07002235
Adam Langleyfcf25832014-12-18 17:42:32 -08002236 if (s->aead_write_ctx != NULL) {
2237 EVP_AEAD_CTX_cleanup(&s->aead_write_ctx->ctx);
2238 OPENSSL_free(s->aead_write_ctx);
2239 s->aead_write_ctx = NULL;
2240 }
2241}
Adam Langley95c29f32014-06-20 12:00:00 -07002242
Adam Langleyfcf25832014-12-18 17:42:32 -08002243X509 *SSL_get_certificate(const SSL *s) {
2244 if (s->cert != NULL) {
2245 return s->cert->key->x509;
2246 }
2247
2248 return NULL;
2249}
2250
2251EVP_PKEY *SSL_get_privatekey(const SSL *s) {
2252 if (s->cert != NULL) {
2253 return s->cert->key->privatekey;
2254 }
2255
2256 return NULL;
2257}
2258
2259X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
2260 if (ctx->cert != NULL) {
2261 return ctx->cert->key->x509;
2262 }
2263
2264 return NULL;
2265}
2266
2267EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
2268 if (ctx->cert != NULL) {
2269 return ctx->cert->key->privatekey;
2270 }
2271
2272 return NULL;
2273}
2274
2275const SSL_CIPHER *SSL_get_current_cipher(const SSL *s) {
2276 if (s->session != NULL && s->session->cipher != NULL) {
2277 return s->session->cipher;
2278 }
2279
2280 return NULL;
2281}
2282
2283const void *SSL_get_current_compression(SSL *s) { return NULL; }
2284
2285const void *SSL_get_current_expansion(SSL *s) { return NULL; }
2286
2287int ssl_init_wbio_buffer(SSL *s, int push) {
2288 BIO *bbio;
2289
2290 if (s->bbio == NULL) {
2291 bbio = BIO_new(BIO_f_buffer());
2292 if (bbio == NULL) {
2293 return 0;
2294 }
2295 s->bbio = bbio;
2296 } else {
2297 bbio = s->bbio;
2298 if (s->bbio == s->wbio) {
2299 s->wbio = BIO_pop(s->wbio);
2300 }
2301 }
2302
2303 BIO_reset(bbio);
2304 if (!BIO_set_read_buffer_size(bbio, 1)) {
2305 OPENSSL_PUT_ERROR(SSL, ssl_init_wbio_buffer, ERR_R_BUF_LIB);
2306 return 0;
2307 }
2308
2309 if (push) {
2310 if (s->wbio != bbio) {
2311 s->wbio = BIO_push(bbio, s->wbio);
2312 }
2313 } else {
2314 if (s->wbio == bbio) {
2315 s->wbio = BIO_pop(bbio);
2316 }
2317 }
2318
2319 return 1;
2320}
2321
2322void ssl_free_wbio_buffer(SSL *s) {
2323 if (s->bbio == NULL) {
2324 return;
2325 }
2326
2327 if (s->bbio == s->wbio) {
2328 /* remove buffering */
2329 s->wbio = BIO_pop(s->wbio);
2330 }
2331
2332 BIO_free(s->bbio);
2333 s->bbio = NULL;
2334}
2335
2336void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
2337 ctx->quiet_shutdown = mode;
2338}
2339
2340int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
2341 return ctx->quiet_shutdown;
2342}
2343
2344void SSL_set_quiet_shutdown(SSL *s, int mode) { s->quiet_shutdown = mode; }
2345
2346int SSL_get_quiet_shutdown(const SSL *s) { return s->quiet_shutdown; }
2347
2348void SSL_set_shutdown(SSL *s, int mode) { s->shutdown = mode; }
2349
2350int SSL_get_shutdown(const SSL *s) { return s->shutdown; }
2351
2352int SSL_version(const SSL *s) { return s->version; }
2353
2354SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
2355
2356SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
2357 if (ssl->ctx == ctx) {
2358 return ssl->ctx;
2359 }
2360
2361 if (ctx == NULL) {
2362 ctx = ssl->initial_ctx;
2363 }
2364
David Benjamin2755a3e2015-04-22 16:17:58 -04002365 ssl_cert_free(ssl->cert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002366 ssl->cert = ssl_cert_dup(ctx->cert);
David Benjamin2755a3e2015-04-22 16:17:58 -04002367
Adam Langleyfcf25832014-12-18 17:42:32 -08002368 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
David Benjamin2755a3e2015-04-22 16:17:58 -04002369 SSL_CTX_free(ssl->ctx); /* decrement reference count */
Adam Langleyfcf25832014-12-18 17:42:32 -08002370 ssl->ctx = ctx;
2371
2372 ssl->sid_ctx_length = ctx->sid_ctx_length;
2373 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2374 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2375
2376 return ssl->ctx;
2377}
2378
2379int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
2380 return X509_STORE_set_default_paths(ctx->cert_store);
2381}
Adam Langley95c29f32014-06-20 12:00:00 -07002382
2383int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
Adam Langleyfcf25832014-12-18 17:42:32 -08002384 const char *CApath) {
2385 return X509_STORE_load_locations(ctx->cert_store, CAfile, CApath);
2386}
Adam Langley95c29f32014-06-20 12:00:00 -07002387
2388void SSL_set_info_callback(SSL *ssl,
Adam Langleyfcf25832014-12-18 17:42:32 -08002389 void (*cb)(const SSL *ssl, int type, int val)) {
2390 ssl->info_callback = cb;
2391}
Adam Langley95c29f32014-06-20 12:00:00 -07002392
Adam Langleyfcf25832014-12-18 17:42:32 -08002393void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/, int /*type*/,
2394 int /*val*/) {
2395 return ssl->info_callback;
2396}
Adam Langley95c29f32014-06-20 12:00:00 -07002397
Adam Langleyfcf25832014-12-18 17:42:32 -08002398int SSL_state(const SSL *ssl) { return ssl->state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002399
Adam Langleyfcf25832014-12-18 17:42:32 -08002400void SSL_set_state(SSL *ssl, int state) { ssl->state = state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002401
Adam Langleyfcf25832014-12-18 17:42:32 -08002402void SSL_set_verify_result(SSL *ssl, long arg) { ssl->verify_result = arg; }
Adam Langley95c29f32014-06-20 12:00:00 -07002403
Adam Langleyfcf25832014-12-18 17:42:32 -08002404long SSL_get_verify_result(const SSL *ssl) { return ssl->verify_result; }
Adam Langley95c29f32014-06-20 12:00:00 -07002405
Adam Langleyfcf25832014-12-18 17:42:32 -08002406int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2407 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002408 int index;
2409 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp,
2410 new_func, dup_func, free_func)) {
2411 return -1;
2412 }
2413 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002414}
Adam Langley95c29f32014-06-20 12:00:00 -07002415
Adam Langleyfcf25832014-12-18 17:42:32 -08002416int SSL_set_ex_data(SSL *s, int idx, void *arg) {
2417 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2418}
Adam Langley95c29f32014-06-20 12:00:00 -07002419
Adam Langleyfcf25832014-12-18 17:42:32 -08002420void *SSL_get_ex_data(const SSL *s, int idx) {
2421 return CRYPTO_get_ex_data(&s->ex_data, idx);
2422}
Adam Langley95c29f32014-06-20 12:00:00 -07002423
Adam Langleyfcf25832014-12-18 17:42:32 -08002424int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2425 CRYPTO_EX_dup *dup_func,
2426 CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002427 int index;
2428 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp,
2429 new_func, dup_func, free_func)) {
2430 return -1;
2431 }
2432 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002433}
Adam Langley95c29f32014-06-20 12:00:00 -07002434
Adam Langleyfcf25832014-12-18 17:42:32 -08002435int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg) {
2436 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2437}
Adam Langley95c29f32014-06-20 12:00:00 -07002438
Adam Langleyfcf25832014-12-18 17:42:32 -08002439void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx) {
2440 return CRYPTO_get_ex_data(&s->ex_data, idx);
2441}
Adam Langley95c29f32014-06-20 12:00:00 -07002442
Adam Langleyfcf25832014-12-18 17:42:32 -08002443int ssl_ok(SSL *s) { return 1; }
Adam Langley95c29f32014-06-20 12:00:00 -07002444
Adam Langleyfcf25832014-12-18 17:42:32 -08002445X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
2446 return ctx->cert_store;
2447}
Adam Langley95c29f32014-06-20 12:00:00 -07002448
Adam Langleyfcf25832014-12-18 17:42:32 -08002449void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
David Benjamin2755a3e2015-04-22 16:17:58 -04002450 X509_STORE_free(ctx->cert_store);
Adam Langleyfcf25832014-12-18 17:42:32 -08002451 ctx->cert_store = store;
2452}
Adam Langley95c29f32014-06-20 12:00:00 -07002453
Adam Langleyfcf25832014-12-18 17:42:32 -08002454int SSL_want(const SSL *s) { return s->rwstate; }
Adam Langley95c29f32014-06-20 12:00:00 -07002455
Adam Langleyfcf25832014-12-18 17:42:32 -08002456void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
2457 RSA *(*cb)(SSL *ssl, int is_export,
2458 int keylength)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002459}
Adam Langley95c29f32014-06-20 12:00:00 -07002460
Adam Langleyfcf25832014-12-18 17:42:32 -08002461void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
2462 int keylength)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002463}
2464
2465void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
David Benjamin59015c32015-04-26 13:13:55 -04002466 DH *(*callback)(SSL *ssl, int is_export,
2467 int keylength)) {
2468 ctx->cert->dh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002469}
2470
David Benjamin59015c32015-04-26 13:13:55 -04002471void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*callback)(SSL *ssl, int is_export,
2472 int keylength)) {
2473 ssl->cert->dh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002474}
2475
2476void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,
David Benjamindd978782015-04-24 15:20:13 -04002477 EC_KEY *(*callback)(SSL *ssl, int is_export,
2478 int keylength)) {
David Benjamin59015c32015-04-26 13:13:55 -04002479 ctx->cert->ecdh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002480}
2481
2482void SSL_set_tmp_ecdh_callback(SSL *ssl,
David Benjamindd978782015-04-24 15:20:13 -04002483 EC_KEY *(*callback)(SSL *ssl, int is_export,
2484 int keylength)) {
David Benjamin59015c32015-04-26 13:13:55 -04002485 ssl->cert->ecdh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002486}
2487
2488int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
2489 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2490 OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_psk_identity_hint,
2491 SSL_R_DATA_LENGTH_TOO_LONG);
2492 return 0;
2493 }
2494
David Benjamin2755a3e2015-04-22 16:17:58 -04002495 OPENSSL_free(ctx->psk_identity_hint);
Adam Langleyfcf25832014-12-18 17:42:32 -08002496
2497 if (identity_hint != NULL) {
2498 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2499 if (ctx->psk_identity_hint == NULL) {
2500 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002501 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002502 } else {
2503 ctx->psk_identity_hint = NULL;
2504 }
Adam Langley95c29f32014-06-20 12:00:00 -07002505
Adam Langleyfcf25832014-12-18 17:42:32 -08002506 return 1;
2507}
2508
2509int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint) {
2510 if (s == NULL) {
2511 return 0;
2512 }
2513
2514 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2515 OPENSSL_PUT_ERROR(SSL, SSL_use_psk_identity_hint,
2516 SSL_R_DATA_LENGTH_TOO_LONG);
2517 return 0;
2518 }
2519
2520 /* Clear currently configured hint, if any. */
David Benjamin2755a3e2015-04-22 16:17:58 -04002521 OPENSSL_free(s->psk_identity_hint);
2522 s->psk_identity_hint = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002523
2524 if (identity_hint != NULL) {
2525 s->psk_identity_hint = BUF_strdup(identity_hint);
2526 if (s->psk_identity_hint == NULL) {
2527 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002528 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002529 }
Adam Langley95c29f32014-06-20 12:00:00 -07002530
Adam Langleyfcf25832014-12-18 17:42:32 -08002531 return 1;
2532}
Adam Langley95c29f32014-06-20 12:00:00 -07002533
Adam Langleyfcf25832014-12-18 17:42:32 -08002534const char *SSL_get_psk_identity_hint(const SSL *s) {
2535 if (s == NULL) {
2536 return NULL;
2537 }
2538 return s->psk_identity_hint;
2539}
Adam Langley95c29f32014-06-20 12:00:00 -07002540
Adam Langleyfcf25832014-12-18 17:42:32 -08002541const char *SSL_get_psk_identity(const SSL *s) {
2542 if (s == NULL || s->session == NULL) {
2543 return NULL;
2544 }
Adam Langley95c29f32014-06-20 12:00:00 -07002545
Adam Langleyfcf25832014-12-18 17:42:32 -08002546 return s->session->psk_identity;
2547}
Adam Langley95c29f32014-06-20 12:00:00 -07002548
Adam Langleyfcf25832014-12-18 17:42:32 -08002549void SSL_set_psk_client_callback(
2550 SSL *s, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2551 unsigned int max_identity_len, uint8_t *psk,
2552 unsigned int max_psk_len)) {
2553 s->psk_client_callback = cb;
2554}
Adam Langley95c29f32014-06-20 12:00:00 -07002555
Adam Langleyfcf25832014-12-18 17:42:32 -08002556void SSL_CTX_set_psk_client_callback(
2557 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2558 unsigned int max_identity_len,
2559 uint8_t *psk, unsigned int max_psk_len)) {
2560 ctx->psk_client_callback = cb;
2561}
Adam Langley95c29f32014-06-20 12:00:00 -07002562
Adam Langleyfcf25832014-12-18 17:42:32 -08002563void SSL_set_psk_server_callback(
2564 SSL *s, unsigned int (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
2565 unsigned int max_psk_len)) {
2566 s->psk_server_callback = cb;
2567}
Adam Langley95c29f32014-06-20 12:00:00 -07002568
Adam Langleyfcf25832014-12-18 17:42:32 -08002569void SSL_CTX_set_psk_server_callback(
2570 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *identity,
2571 uint8_t *psk, unsigned int max_psk_len)) {
2572 ctx->psk_server_callback = cb;
2573}
Adam Langley95c29f32014-06-20 12:00:00 -07002574
Adam Langleyfcf25832014-12-18 17:42:32 -08002575void SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) {
2576 ctx->min_version = version;
2577}
Adam Langley95c29f32014-06-20 12:00:00 -07002578
Adam Langleyfcf25832014-12-18 17:42:32 -08002579void SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) {
2580 ctx->max_version = version;
2581}
Adam Langley0289c732014-06-20 12:00:00 -07002582
Adam Langleyfcf25832014-12-18 17:42:32 -08002583void SSL_set_min_version(SSL *ssl, uint16_t version) {
2584 ssl->min_version = version;
2585}
Adam Langley0289c732014-06-20 12:00:00 -07002586
Adam Langleyfcf25832014-12-18 17:42:32 -08002587void SSL_set_max_version(SSL *ssl, uint16_t version) {
2588 ssl->max_version = version;
2589}
Adam Langley95c29f32014-06-20 12:00:00 -07002590
Adam Langleyfcf25832014-12-18 17:42:32 -08002591void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
2592 void (*cb)(int write_p, int version,
2593 int content_type, const void *buf,
2594 size_t len, SSL *ssl, void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002595 ctx->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002596}
David Benjamin61ecccf2015-05-05 09:44:51 -04002597
2598void SSL_CTX_set_msg_callback_arg(SSL_CTX *ctx, void *arg) {
2599 ctx->msg_callback_arg = arg;
2600}
2601
Adam Langleyfcf25832014-12-18 17:42:32 -08002602void SSL_set_msg_callback(SSL *ssl,
2603 void (*cb)(int write_p, int version, int content_type,
2604 const void *buf, size_t len, SSL *ssl,
2605 void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002606 ssl->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002607}
Adam Langley95c29f32014-06-20 12:00:00 -07002608
David Benjamin61ecccf2015-05-05 09:44:51 -04002609void SSL_set_msg_callback_arg(SSL *ssl, void *arg) {
2610 ssl->msg_callback_arg = arg;
2611}
2612
Adam Langleyfcf25832014-12-18 17:42:32 -08002613void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio) {
David Benjamin2755a3e2015-04-22 16:17:58 -04002614 BIO_free(ctx->keylog_bio);
Adam Langleyfcf25832014-12-18 17:42:32 -08002615 ctx->keylog_bio = keylog_bio;
2616}
Adam Langley95c29f32014-06-20 12:00:00 -07002617
Adam Langleyfcf25832014-12-18 17:42:32 -08002618static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
2619 static const char hextable[] = "0123456789abcdef";
2620 uint8_t *out;
2621 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002622
Adam Langleyfcf25832014-12-18 17:42:32 -08002623 if (!CBB_add_space(cbb, &out, in_len * 2)) {
2624 return 0;
2625 }
Adam Langley95c29f32014-06-20 12:00:00 -07002626
Adam Langleyfcf25832014-12-18 17:42:32 -08002627 for (i = 0; i < in_len; i++) {
2628 *(out++) = (uint8_t)hextable[in[i] >> 4];
2629 *(out++) = (uint8_t)hextable[in[i] & 0xf];
2630 }
Adam Langley95c29f32014-06-20 12:00:00 -07002631
Adam Langleyfcf25832014-12-18 17:42:32 -08002632 return 1;
2633}
David Benjamin859ec3c2014-09-02 16:29:36 -04002634
2635int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -08002636 const uint8_t *encrypted_premaster,
2637 size_t encrypted_premaster_len,
2638 const uint8_t *premaster,
2639 size_t premaster_len) {
2640 BIO *bio = ctx->keylog_bio;
2641 CBB cbb;
2642 uint8_t *out;
2643 size_t out_len;
2644 int ret;
David Benjamin859ec3c2014-09-02 16:29:36 -04002645
Adam Langleyfcf25832014-12-18 17:42:32 -08002646 if (bio == NULL) {
2647 return 1;
2648 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002649
Adam Langleyfcf25832014-12-18 17:42:32 -08002650 if (encrypted_premaster_len < 8) {
2651 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_rsa_client_key_exchange,
2652 ERR_R_INTERNAL_ERROR);
2653 return 0;
2654 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002655
Adam Langleyfcf25832014-12-18 17:42:32 -08002656 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len * 2 + 1)) {
2657 return 0;
2658 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002659
Adam Langleyfcf25832014-12-18 17:42:32 -08002660 if (!CBB_add_bytes(&cbb, (const uint8_t *)"RSA ", 4) ||
2661 /* Only the first 8 bytes of the encrypted premaster secret are
2662 * logged. */
2663 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
2664 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2665 !cbb_add_hex(&cbb, premaster, premaster_len) ||
2666 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2667 !CBB_finish(&cbb, &out, &out_len)) {
2668 CBB_cleanup(&cbb);
2669 return 0;
2670 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002671
Adam Langleyfcf25832014-12-18 17:42:32 -08002672 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2673 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2674 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
David Benjamin859ec3c2014-09-02 16:29:36 -04002675
Adam Langleyfcf25832014-12-18 17:42:32 -08002676 OPENSSL_free(out);
2677 return ret;
Adam Langley95f22882014-06-20 12:00:00 -07002678}
2679
Adam Langleyfcf25832014-12-18 17:42:32 -08002680int ssl_ctx_log_master_secret(SSL_CTX *ctx, const uint8_t *client_random,
2681 size_t client_random_len, const uint8_t *master,
2682 size_t master_len) {
2683 BIO *bio = ctx->keylog_bio;
2684 CBB cbb;
2685 uint8_t *out;
2686 size_t out_len;
2687 int ret;
Adam Langleyadb739e2014-06-20 12:00:00 -07002688
Adam Langleyfcf25832014-12-18 17:42:32 -08002689 if (bio == NULL) {
2690 return 1;
2691 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002692
Adam Langleyfcf25832014-12-18 17:42:32 -08002693 if (client_random_len != 32) {
2694 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_master_secret, ERR_R_INTERNAL_ERROR);
2695 return 0;
2696 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002697
Adam Langleyfcf25832014-12-18 17:42:32 -08002698 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len * 2 + 1)) {
2699 return 0;
2700 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002701
Adam Langleyfcf25832014-12-18 17:42:32 -08002702 if (!CBB_add_bytes(&cbb, (const uint8_t *)"CLIENT_RANDOM ", 14) ||
2703 !cbb_add_hex(&cbb, client_random, 32) ||
2704 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2705 !cbb_add_hex(&cbb, master, master_len) ||
2706 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2707 !CBB_finish(&cbb, &out, &out_len)) {
2708 CBB_cleanup(&cbb);
2709 return 0;
2710 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002711
Adam Langleyfcf25832014-12-18 17:42:32 -08002712 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2713 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2714 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
David Benjamine99e9122014-12-11 01:46:01 -05002715
Adam Langleyfcf25832014-12-18 17:42:32 -08002716 OPENSSL_free(out);
2717 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -07002718}
2719
David Benjamined7c4752015-02-16 19:16:46 -05002720int SSL_in_false_start(const SSL *s) {
2721 return s->s3->tmp.in_false_start;
2722}
2723
Adam Langleyfcf25832014-12-18 17:42:32 -08002724int SSL_cutthrough_complete(const SSL *s) {
David Benjamined7c4752015-02-16 19:16:46 -05002725 return SSL_in_false_start(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002726}
Adam Langley95c29f32014-06-20 12:00:00 -07002727
Adam Langleyfcf25832014-12-18 17:42:32 -08002728void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
2729 size_t *ssl_session_size) {
2730 *ssl_size = sizeof(SSL);
2731 *ssl_ctx_size = sizeof(SSL_CTX);
2732 *ssl_session_size = sizeof(SSL_SESSION);
2733}
Feng Lu41aa3252014-11-21 22:47:56 -08002734
David Benjamined7c4752015-02-16 19:16:46 -05002735int ssl3_can_false_start(const SSL *s) {
David Benjamin195dc782015-02-19 13:27:05 -05002736 const SSL_CIPHER *const cipher = SSL_get_current_cipher(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002737
Adam Langleye631d962015-04-01 13:11:01 -07002738 /* False Start only for TLS 1.2 with an ECDHE+AEAD cipher and ALPN or NPN. */
David Benjamin195dc782015-02-19 13:27:05 -05002739 return !SSL_IS_DTLS(s) &&
2740 SSL_version(s) >= TLS1_2_VERSION &&
2741 (s->s3->alpn_selected || s->s3->next_proto_neg_seen) &&
2742 cipher != NULL &&
Adam Langleye631d962015-04-01 13:11:01 -07002743 cipher->algorithm_mkey == SSL_kECDHE &&
David Benjamin195dc782015-02-19 13:27:05 -05002744 (cipher->algorithm_enc == SSL_AES128GCM ||
2745 cipher->algorithm_enc == SSL_AES256GCM ||
2746 cipher->algorithm_enc == SSL_CHACHA20POLY1305);
Adam Langleyfcf25832014-12-18 17:42:32 -08002747}
2748
2749const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version) {
2750 switch (version) {
2751 case SSL3_VERSION:
2752 return &SSLv3_enc_data;
2753
2754 case TLS1_VERSION:
2755 return &TLSv1_enc_data;
2756
David Benjamin9e13e1a2015-03-05 01:56:32 -05002757 case DTLS1_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002758 case TLS1_1_VERSION:
2759 return &TLSv1_1_enc_data;
2760
David Benjamin9e13e1a2015-03-05 01:56:32 -05002761 case DTLS1_2_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002762 case TLS1_2_VERSION:
2763 return &TLSv1_2_enc_data;
2764
Adam Langleyfcf25832014-12-18 17:42:32 -08002765 default:
2766 return NULL;
2767 }
2768}
2769
2770uint16_t ssl3_get_max_server_version(const SSL *s) {
2771 uint16_t max_version;
2772
2773 if (SSL_IS_DTLS(s)) {
2774 max_version = (s->max_version != 0) ? s->max_version : DTLS1_2_VERSION;
2775 if (!(s->options & SSL_OP_NO_DTLSv1_2) && DTLS1_2_VERSION >= max_version) {
2776 return DTLS1_2_VERSION;
2777 }
2778 if (!(s->options & SSL_OP_NO_DTLSv1) && DTLS1_VERSION >= max_version) {
2779 return DTLS1_VERSION;
2780 }
2781 return 0;
2782 }
2783
2784 max_version = (s->max_version != 0) ? s->max_version : TLS1_2_VERSION;
2785 if (!(s->options & SSL_OP_NO_TLSv1_2) && TLS1_2_VERSION <= max_version) {
2786 return TLS1_2_VERSION;
2787 }
2788 if (!(s->options & SSL_OP_NO_TLSv1_1) && TLS1_1_VERSION <= max_version) {
2789 return TLS1_1_VERSION;
2790 }
2791 if (!(s->options & SSL_OP_NO_TLSv1) && TLS1_VERSION <= max_version) {
2792 return TLS1_VERSION;
2793 }
2794 if (!(s->options & SSL_OP_NO_SSLv3) && SSL3_VERSION <= max_version) {
2795 return SSL3_VERSION;
2796 }
2797 return 0;
2798}
2799
2800uint16_t ssl3_get_mutual_version(SSL *s, uint16_t client_version) {
2801 uint16_t version = 0;
2802
2803 if (SSL_IS_DTLS(s)) {
2804 /* Clamp client_version to max_version. */
2805 if (s->max_version != 0 && client_version < s->max_version) {
2806 client_version = s->max_version;
2807 }
2808
2809 if (client_version <= DTLS1_2_VERSION && !(s->options & SSL_OP_NO_DTLSv1_2)) {
2810 version = DTLS1_2_VERSION;
2811 } else if (client_version <= DTLS1_VERSION &&
David Benjaminb18f0242015-03-10 18:30:08 -04002812 !(s->options & SSL_OP_NO_DTLSv1)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002813 version = DTLS1_VERSION;
2814 }
2815
2816 /* Check against min_version. */
2817 if (version != 0 && s->min_version != 0 && version > s->min_version) {
2818 return 0;
2819 }
2820 return version;
2821 } else {
2822 /* Clamp client_version to max_version. */
2823 if (s->max_version != 0 && client_version > s->max_version) {
2824 client_version = s->max_version;
2825 }
2826
2827 if (client_version >= TLS1_2_VERSION && !(s->options & SSL_OP_NO_TLSv1_2)) {
2828 version = TLS1_2_VERSION;
2829 } else if (client_version >= TLS1_1_VERSION &&
2830 !(s->options & SSL_OP_NO_TLSv1_1)) {
2831 version = TLS1_1_VERSION;
2832 } else if (client_version >= TLS1_VERSION && !(s->options & SSL_OP_NO_TLSv1)) {
2833 version = TLS1_VERSION;
2834 } else if (client_version >= SSL3_VERSION && !(s->options & SSL_OP_NO_SSLv3)) {
2835 version = SSL3_VERSION;
2836 }
2837
2838 /* Check against min_version. */
2839 if (version != 0 && s->min_version != 0 && version < s->min_version) {
2840 return 0;
2841 }
2842 return version;
2843 }
2844}
2845
2846uint16_t ssl3_get_max_client_version(SSL *s) {
David Benjamin123a8fd2015-04-26 14:16:41 -04002847 uint32_t options = s->options;
Adam Langleyfcf25832014-12-18 17:42:32 -08002848 uint16_t version = 0;
2849
2850 /* OpenSSL's API for controlling versions entails blacklisting individual
2851 * protocols. This has two problems. First, on the client, the protocol can
2852 * only express a contiguous range of versions. Second, a library consumer
2853 * trying to set a maximum version cannot disable protocol versions that get
2854 * added in a future version of the library.
2855 *
2856 * To account for both of these, OpenSSL interprets the client-side bitmask
2857 * as a min/max range by picking the lowest contiguous non-empty range of
2858 * enabled protocols. Note that this means it is impossible to set a maximum
2859 * version of TLS 1.2 in a future-proof way.
2860 *
2861 * By this scheme, the maximum version is the lowest version V such that V is
2862 * enabled and V+1 is disabled or unimplemented. */
2863 if (SSL_IS_DTLS(s)) {
2864 if (!(options & SSL_OP_NO_DTLSv1_2)) {
2865 version = DTLS1_2_VERSION;
2866 }
2867 if (!(options & SSL_OP_NO_DTLSv1) && (options & SSL_OP_NO_DTLSv1_2)) {
2868 version = DTLS1_VERSION;
2869 }
2870 if (s->max_version != 0 && version < s->max_version) {
2871 version = s->max_version;
2872 }
2873 } else {
2874 if (!(options & SSL_OP_NO_TLSv1_2)) {
2875 version = TLS1_2_VERSION;
2876 }
2877 if (!(options & SSL_OP_NO_TLSv1_1) && (options & SSL_OP_NO_TLSv1_2)) {
2878 version = TLS1_1_VERSION;
2879 }
2880 if (!(options & SSL_OP_NO_TLSv1) && (options & SSL_OP_NO_TLSv1_1)) {
2881 version = TLS1_VERSION;
2882 }
2883 if (!(options & SSL_OP_NO_SSLv3) && (options & SSL_OP_NO_TLSv1)) {
2884 version = SSL3_VERSION;
2885 }
2886 if (s->max_version != 0 && version > s->max_version) {
2887 version = s->max_version;
2888 }
2889 }
2890
2891 return version;
2892}
2893
2894int ssl3_is_version_enabled(SSL *s, uint16_t version) {
2895 if (SSL_IS_DTLS(s)) {
2896 if (s->max_version != 0 && version < s->max_version) {
2897 return 0;
2898 }
2899 if (s->min_version != 0 && version > s->min_version) {
2900 return 0;
2901 }
2902
2903 switch (version) {
2904 case DTLS1_VERSION:
2905 return !(s->options & SSL_OP_NO_DTLSv1);
2906
2907 case DTLS1_2_VERSION:
2908 return !(s->options & SSL_OP_NO_DTLSv1_2);
2909
2910 default:
2911 return 0;
2912 }
2913 } else {
2914 if (s->max_version != 0 && version > s->max_version) {
2915 return 0;
2916 }
2917 if (s->min_version != 0 && version < s->min_version) {
2918 return 0;
2919 }
2920
2921 switch (version) {
2922 case SSL3_VERSION:
2923 return !(s->options & SSL_OP_NO_SSLv3);
2924
2925 case TLS1_VERSION:
2926 return !(s->options & SSL_OP_NO_TLSv1);
2927
2928 case TLS1_1_VERSION:
2929 return !(s->options & SSL_OP_NO_TLSv1_1);
2930
2931 case TLS1_2_VERSION:
2932 return !(s->options & SSL_OP_NO_TLSv1_2);
2933
2934 default:
2935 return 0;
2936 }
2937 }
2938}
2939
David Benjaminea72bd02014-12-21 21:27:41 -05002940uint16_t ssl3_version_from_wire(SSL *s, uint16_t wire_version) {
2941 if (!SSL_IS_DTLS(s)) {
2942 return wire_version;
2943 }
2944
2945 uint16_t tls_version = ~wire_version;
2946 uint16_t version = tls_version + 0x0201;
2947 /* If either component overflowed, clamp it so comparisons still work. */
2948 if ((version >> 8) < (tls_version >> 8)) {
2949 version = 0xff00 | (version & 0xff);
2950 }
2951 if ((version & 0xff) < (tls_version & 0xff)) {
2952 version = (version & 0xff00) | 0xff;
2953 }
2954 /* DTLS 1.0 maps to TLS 1.1, not TLS 1.0. */
2955 if (version == TLS1_VERSION) {
2956 version = TLS1_1_VERSION;
2957 }
2958 return version;
2959}
2960
Adam Langleyfcf25832014-12-18 17:42:32 -08002961int SSL_cache_hit(SSL *s) { return s->hit; }
2962
2963int SSL_is_server(SSL *s) { return s->server; }
2964
Adam Langley524e7172015-02-20 16:04:00 -08002965void SSL_CTX_set_dos_protection_cb(
2966 SSL_CTX *ctx, int (*cb)(const struct ssl_early_callback_ctx *)) {
2967 ctx->dos_protection_cb = cb;
2968}
2969
Adam Langleyfcf25832014-12-18 17:42:32 -08002970void SSL_enable_fastradio_padding(SSL *s, char on_off) {
2971 s->fastradio_padding = on_off;
2972}
Adam Langley44972942015-02-20 13:43:23 -08002973
David Benjaminb16346b2015-04-08 19:16:58 -04002974void SSL_set_reject_peer_renegotiations(SSL *s, int reject) {
2975 s->reject_peer_renegotiations = !!reject;
2976}
2977
Adam Langley44972942015-02-20 13:43:23 -08002978const SSL_CIPHER *SSL_get_cipher_by_value(uint16_t value) {
2979 return ssl3_get_cipher_by_value(value);
2980}
Adam Langley3f92d212015-02-20 15:32:52 -08002981
2982int SSL_get_rc4_state(const SSL *ssl, const RC4_KEY **read_key,
2983 const RC4_KEY **write_key) {
2984 if (ssl->aead_read_ctx == NULL || ssl->aead_write_ctx == NULL) {
2985 return 0;
2986 }
2987
2988 return EVP_AEAD_CTX_get_rc4_state(&ssl->aead_read_ctx->ctx, read_key) &&
2989 EVP_AEAD_CTX_get_rc4_state(&ssl->aead_write_ctx->ctx, write_key);
2990}
David Benjaminda881e92015-04-26 14:45:04 -04002991
2992int SSL_CTX_sess_connect(const SSL_CTX *ctx) { return 0; }
2993int SSL_CTX_sess_connect_good(const SSL_CTX *ctx) { return 0; }
2994int SSL_CTX_sess_connect_renegotiate(const SSL_CTX *ctx) { return 0; }
2995int SSL_CTX_sess_accept(const SSL_CTX *ctx) { return 0; }
2996int SSL_CTX_sess_accept_renegotiate(const SSL_CTX *ctx) { return 0; }
2997int SSL_CTX_sess_accept_good(const SSL_CTX *ctx) { return 0; }
2998int SSL_CTX_sess_hits(const SSL_CTX *ctx) { return 0; }
2999int SSL_CTX_sess_cb_hits(const SSL_CTX *ctx) { return 0; }
3000int SSL_CTX_sess_misses(const SSL_CTX *ctx) { return 0; }
3001int SSL_CTX_sess_timeouts(const SSL_CTX *ctx) { return 0; }
3002int SSL_CTX_sess_cache_full(const SSL_CTX *ctx) { return 0; }