blob: 491492405f493cf8c346d666c14d32d9de26d53c [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"
Adam Langley95c29f32014-06-20 12:00:00 -0700155
Adam Langleyfcf25832014-12-18 17:42:32 -0800156/* Some error codes are special. Ensure the make_errors.go script never
157 * regresses this. */
158OPENSSL_COMPILE_ASSERT(SSL_R_TLSV1_ALERT_NO_RENEGOTIATION ==
159 SSL_AD_NO_RENEGOTIATION + SSL_AD_REASON_OFFSET,
160 ssl_alert_reason_code_mismatch);
David Benjamine1136082014-09-20 12:28:58 -0400161
Adam Langleyfcf25832014-12-18 17:42:32 -0800162int SSL_clear(SSL *s) {
163 if (s->method == NULL) {
164 OPENSSL_PUT_ERROR(SSL, SSL_clear, SSL_R_NO_METHOD_SPECIFIED);
165 return 0;
166 }
Adam Langley95c29f32014-06-20 12:00:00 -0700167
Adam Langleyfcf25832014-12-18 17:42:32 -0800168 if (ssl_clear_bad_session(s)) {
169 SSL_SESSION_free(s->session);
170 s->session = NULL;
171 }
Adam Langley95c29f32014-06-20 12:00:00 -0700172
Adam Langleyfcf25832014-12-18 17:42:32 -0800173 s->hit = 0;
174 s->shutdown = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700175
Adam Langleyfcf25832014-12-18 17:42:32 -0800176 if (s->renegotiate) {
177 OPENSSL_PUT_ERROR(SSL, SSL_clear, ERR_R_INTERNAL_ERROR);
178 return 0;
179 }
Adam Langley95c29f32014-06-20 12:00:00 -0700180
Adam Langleyfcf25832014-12-18 17:42:32 -0800181 /* SSL_clear may be called before or after the |s| is initialized in either
182 * accept or connect state. In the latter case, SSL_clear should preserve the
183 * half and reset |s->state| accordingly. */
184 if (s->handshake_func != NULL) {
185 if (s->server) {
186 SSL_set_accept_state(s);
187 } else {
188 SSL_set_connect_state(s);
189 }
190 } else {
191 assert(s->state == 0);
192 }
Adam Langley95c29f32014-06-20 12:00:00 -0700193
David Benjamin62fd1622015-01-11 13:30:01 -0500194 /* TODO(davidben): Some state on |s| is reset both in |SSL_new| and
195 * |SSL_clear| because it is per-connection state rather than configuration
196 * state. Per-connection state should be on |s->s3| and |s->d1| so it is
197 * naturally reset at the right points between |SSL_new|, |SSL_clear|, and
198 * |ssl3_new|. */
199
Adam Langleyfcf25832014-12-18 17:42:32 -0800200 s->rwstate = SSL_NOTHING;
201 s->rstate = SSL_ST_READ_HEADER;
Adam Langley95c29f32014-06-20 12:00:00 -0700202
Adam Langleyfcf25832014-12-18 17:42:32 -0800203 if (s->init_buf != NULL) {
204 BUF_MEM_free(s->init_buf);
205 s->init_buf = NULL;
206 }
Adam Langley95c29f32014-06-20 12:00:00 -0700207
David Benjamin62fd1622015-01-11 13:30:01 -0500208 s->packet = NULL;
209 s->packet_length = 0;
210
Adam Langleyfcf25832014-12-18 17:42:32 -0800211 ssl_clear_cipher_ctx(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700212
David Benjamin62fd1622015-01-11 13:30:01 -0500213 if (s->next_proto_negotiated) {
214 OPENSSL_free(s->next_proto_negotiated);
215 s->next_proto_negotiated = NULL;
216 s->next_proto_negotiated_len = 0;
217 }
218
219 /* The s->d1->mtu is simultaneously configuration (preserved across
220 * clear) and connection-specific state (gets reset).
221 *
222 * TODO(davidben): Avoid this. */
223 unsigned mtu = 0;
224 if (s->d1 != NULL) {
225 mtu = s->d1->mtu;
226 }
227
228 s->method->ssl_free(s);
229 if (!s->method->ssl_new(s)) {
230 return 0;
231 }
232 s->enc_method = ssl3_get_enc_method(s->version);
233 assert(s->enc_method != NULL);
234
235 if (SSL_IS_DTLS(s) && (SSL_get_options(s) & SSL_OP_NO_QUERY_MTU)) {
236 s->d1->mtu = mtu;
237 }
238
Adam Langleyfcf25832014-12-18 17:42:32 -0800239 s->client_version = s->version;
Adam Langley95c29f32014-06-20 12:00:00 -0700240
Adam Langleyfcf25832014-12-18 17:42:32 -0800241 return 1;
242}
Adam Langley95c29f32014-06-20 12:00:00 -0700243
Adam Langleyfcf25832014-12-18 17:42:32 -0800244SSL *SSL_new(SSL_CTX *ctx) {
245 SSL *s;
Adam Langley95c29f32014-06-20 12:00:00 -0700246
Adam Langleyfcf25832014-12-18 17:42:32 -0800247 if (ctx == NULL) {
248 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_NULL_SSL_CTX);
249 return NULL;
250 }
251 if (ctx->method == NULL) {
252 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
253 return NULL;
254 }
Adam Langley95c29f32014-06-20 12:00:00 -0700255
Adam Langleyfcf25832014-12-18 17:42:32 -0800256 s = (SSL *)OPENSSL_malloc(sizeof(SSL));
257 if (s == NULL) {
258 goto err;
259 }
260 memset(s, 0, sizeof(SSL));
Adam Langley95c29f32014-06-20 12:00:00 -0700261
Adam Langleyfcf25832014-12-18 17:42:32 -0800262 s->min_version = ctx->min_version;
263 s->max_version = ctx->max_version;
David Benjamin1eb367c2014-12-12 18:17:51 -0500264
Adam Langleyfcf25832014-12-18 17:42:32 -0800265 s->options = ctx->options;
266 s->mode = ctx->mode;
267 s->max_cert_list = ctx->max_cert_list;
Adam Langley95c29f32014-06-20 12:00:00 -0700268
David Benjamina5a3eeb2015-03-18 20:26:30 -0400269 s->cert = ssl_cert_dup(ctx->cert);
270 if (s->cert == NULL) {
271 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -0800272 }
Adam Langley95c29f32014-06-20 12:00:00 -0700273
Adam Langleyfcf25832014-12-18 17:42:32 -0800274 s->read_ahead = ctx->read_ahead;
275 s->msg_callback = ctx->msg_callback;
276 s->msg_callback_arg = ctx->msg_callback_arg;
277 s->verify_mode = ctx->verify_mode;
278 s->sid_ctx_length = ctx->sid_ctx_length;
279 assert(s->sid_ctx_length <= sizeof s->sid_ctx);
280 memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));
281 s->verify_callback = ctx->default_verify_callback;
282 s->generate_session_id = ctx->generate_session_id;
Adam Langley95c29f32014-06-20 12:00:00 -0700283
Adam Langleyfcf25832014-12-18 17:42:32 -0800284 s->param = X509_VERIFY_PARAM_new();
285 if (!s->param) {
286 goto err;
287 }
288 X509_VERIFY_PARAM_inherit(s->param, ctx->param);
289 s->quiet_shutdown = ctx->quiet_shutdown;
290 s->max_send_fragment = ctx->max_send_fragment;
Adam Langley95c29f32014-06-20 12:00:00 -0700291
Adam Langleyfcf25832014-12-18 17:42:32 -0800292 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
293 s->ctx = ctx;
294 s->tlsext_debug_cb = 0;
295 s->tlsext_debug_arg = NULL;
296 s->tlsext_ticket_expected = 0;
297 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
298 s->initial_ctx = ctx;
299 if (ctx->tlsext_ecpointformatlist) {
300 s->tlsext_ecpointformatlist = BUF_memdup(
301 ctx->tlsext_ecpointformatlist, ctx->tlsext_ecpointformatlist_length);
302 if (!s->tlsext_ecpointformatlist) {
303 goto err;
304 }
305 s->tlsext_ecpointformatlist_length = ctx->tlsext_ecpointformatlist_length;
306 }
Adam Langley95c29f32014-06-20 12:00:00 -0700307
Adam Langleyfcf25832014-12-18 17:42:32 -0800308 if (ctx->tlsext_ellipticcurvelist) {
309 s->tlsext_ellipticcurvelist =
310 BUF_memdup(ctx->tlsext_ellipticcurvelist,
311 ctx->tlsext_ellipticcurvelist_length * 2);
312 if (!s->tlsext_ellipticcurvelist) {
313 goto err;
314 }
315 s->tlsext_ellipticcurvelist_length = ctx->tlsext_ellipticcurvelist_length;
316 }
317 s->next_proto_negotiated = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700318
Adam Langleyfcf25832014-12-18 17:42:32 -0800319 if (s->ctx->alpn_client_proto_list) {
320 s->alpn_client_proto_list = BUF_memdup(s->ctx->alpn_client_proto_list,
321 s->ctx->alpn_client_proto_list_len);
322 if (s->alpn_client_proto_list == NULL) {
323 goto err;
324 }
325 s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;
326 }
Adam Langley95c29f32014-06-20 12:00:00 -0700327
Adam Langleyfcf25832014-12-18 17:42:32 -0800328 s->verify_result = X509_V_OK;
329 s->method = ctx->method;
Adam Langley95c29f32014-06-20 12:00:00 -0700330
Adam Langleyfcf25832014-12-18 17:42:32 -0800331 if (!s->method->ssl_new(s)) {
332 goto err;
333 }
334 s->enc_method = ssl3_get_enc_method(s->version);
335 assert(s->enc_method != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700336
David Benjamin62fd1622015-01-11 13:30:01 -0500337 s->rwstate = SSL_NOTHING;
338 s->rstate = SSL_ST_READ_HEADER;
Adam Langley95c29f32014-06-20 12:00:00 -0700339
Adam Langleyfcf25832014-12-18 17:42:32 -0800340 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700341
Adam Langleyfcf25832014-12-18 17:42:32 -0800342 s->psk_identity_hint = NULL;
343 if (ctx->psk_identity_hint) {
344 s->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
345 if (s->psk_identity_hint == NULL) {
346 goto err;
347 }
348 }
349 s->psk_client_callback = ctx->psk_client_callback;
350 s->psk_server_callback = ctx->psk_server_callback;
Adam Langley95c29f32014-06-20 12:00:00 -0700351
David Benjamin02ddbfd2015-01-11 13:09:11 -0500352 s->tlsext_channel_id_enabled = ctx->tlsext_channel_id_enabled;
353 if (ctx->tlsext_channel_id_private) {
354 s->tlsext_channel_id_private = EVP_PKEY_dup(ctx->tlsext_channel_id_private);
355 }
356
Adam Langleyfcf25832014-12-18 17:42:32 -0800357 s->signed_cert_timestamps_enabled = s->ctx->signed_cert_timestamps_enabled;
358 s->ocsp_stapling_enabled = s->ctx->ocsp_stapling_enabled;
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200359
Adam Langleyfcf25832014-12-18 17:42:32 -0800360 return s;
361
Adam Langley95c29f32014-06-20 12:00:00 -0700362err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800363 if (s != NULL) {
David Benjamin710d2272015-01-11 20:07:59 -0500364 SSL_free(s);
Adam Langleyfcf25832014-12-18 17:42:32 -0800365 }
366 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) {
458 sk_SSL_CIPHER_free(cipher_list->ciphers);
459 OPENSSL_free(cipher_list->in_group_flags);
460 OPENSSL_free(cipher_list);
461}
Adam Langley858a88d2014-06-20 12:00:00 -0700462
Adam Langleyfcf25832014-12-18 17:42:32 -0800463struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_dup(
464 struct ssl_cipher_preference_list_st *cipher_list) {
465 struct ssl_cipher_preference_list_st *ret = NULL;
466 size_t n = sk_SSL_CIPHER_num(cipher_list->ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700467
Adam Langleyfcf25832014-12-18 17:42:32 -0800468 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
469 if (!ret) {
470 goto err;
471 }
472
473 ret->ciphers = NULL;
474 ret->in_group_flags = NULL;
475 ret->ciphers = sk_SSL_CIPHER_dup(cipher_list->ciphers);
476 if (!ret->ciphers) {
477 goto err;
478 }
479 ret->in_group_flags = BUF_memdup(cipher_list->in_group_flags, n);
480 if (!ret->in_group_flags) {
481 goto err;
482 }
483
484 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -0700485
486err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800487 if (ret && ret->ciphers) {
488 sk_SSL_CIPHER_free(ret->ciphers);
489 }
490 if (ret) {
491 OPENSSL_free(ret);
492 }
493 return NULL;
494}
Adam Langley858a88d2014-06-20 12:00:00 -0700495
Adam Langleyfcf25832014-12-18 17:42:32 -0800496struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_from_ciphers(
497 STACK_OF(SSL_CIPHER) * ciphers) {
498 struct ssl_cipher_preference_list_st *ret = NULL;
499 size_t n = sk_SSL_CIPHER_num(ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700500
Adam Langleyfcf25832014-12-18 17:42:32 -0800501 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
502 if (!ret) {
503 goto err;
504 }
505 ret->ciphers = NULL;
506 ret->in_group_flags = NULL;
507 ret->ciphers = sk_SSL_CIPHER_dup(ciphers);
508 if (!ret->ciphers) {
509 goto err;
510 }
511 ret->in_group_flags = OPENSSL_malloc(n);
512 if (!ret->in_group_flags) {
513 goto err;
514 }
515 memset(ret->in_group_flags, 0, n);
516 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -0700517
518err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800519 if (ret && ret->ciphers) {
520 sk_SSL_CIPHER_free(ret->ciphers);
521 }
522 if (ret) {
523 OPENSSL_free(ret);
524 }
525 return NULL;
526}
Adam Langley858a88d2014-06-20 12:00:00 -0700527
Adam Langleyfcf25832014-12-18 17:42:32 -0800528X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) { return ctx->param; }
Adam Langley95c29f32014-06-20 12:00:00 -0700529
Adam Langleyfcf25832014-12-18 17:42:32 -0800530X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) { return ssl->param; }
Adam Langley95c29f32014-06-20 12:00:00 -0700531
Adam Langleyfcf25832014-12-18 17:42:32 -0800532void SSL_certs_clear(SSL *s) { ssl_cert_clear_certs(s->cert); }
Adam Langley95c29f32014-06-20 12:00:00 -0700533
Adam Langleyfcf25832014-12-18 17:42:32 -0800534void SSL_free(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800535 if (s == NULL) {
536 return;
537 }
Adam Langley95c29f32014-06-20 12:00:00 -0700538
Adam Langleyfcf25832014-12-18 17:42:32 -0800539 if (s->param) {
540 X509_VERIFY_PARAM_free(s->param);
541 }
Adam Langley95c29f32014-06-20 12:00:00 -0700542
Adam Langleyfcf25832014-12-18 17:42:32 -0800543 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700544
Adam Langleyfcf25832014-12-18 17:42:32 -0800545 if (s->bbio != NULL) {
546 /* If the buffering BIO is in place, pop it off */
547 if (s->bbio == s->wbio) {
548 s->wbio = BIO_pop(s->wbio);
549 }
550 BIO_free(s->bbio);
551 s->bbio = NULL;
552 }
Adam Langley95c29f32014-06-20 12:00:00 -0700553
Adam Langleyfcf25832014-12-18 17:42:32 -0800554 if (s->rbio != NULL) {
555 BIO_free_all(s->rbio);
556 }
Adam Langley95c29f32014-06-20 12:00:00 -0700557
Adam Langleyfcf25832014-12-18 17:42:32 -0800558 if (s->wbio != NULL && s->wbio != s->rbio) {
559 BIO_free_all(s->wbio);
560 }
Adam Langley95c29f32014-06-20 12:00:00 -0700561
Adam Langleyfcf25832014-12-18 17:42:32 -0800562 if (s->init_buf != NULL) {
563 BUF_MEM_free(s->init_buf);
564 }
Adam Langley95c29f32014-06-20 12:00:00 -0700565
Adam Langleyfcf25832014-12-18 17:42:32 -0800566 /* add extra stuff */
567 if (s->cipher_list != NULL) {
568 ssl_cipher_preference_list_free(s->cipher_list);
569 }
570 if (s->cipher_list_by_id != NULL) {
571 sk_SSL_CIPHER_free(s->cipher_list_by_id);
572 }
Adam Langley95c29f32014-06-20 12:00:00 -0700573
Adam Langleyfcf25832014-12-18 17:42:32 -0800574 if (s->session != NULL) {
575 ssl_clear_bad_session(s);
576 SSL_SESSION_free(s->session);
577 }
Adam Langley95c29f32014-06-20 12:00:00 -0700578
Adam Langleyfcf25832014-12-18 17:42:32 -0800579 ssl_clear_cipher_ctx(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700580
Adam Langleyfcf25832014-12-18 17:42:32 -0800581 if (s->cert != NULL) {
582 ssl_cert_free(s->cert);
583 }
Adam Langley0289c732014-06-20 12:00:00 -0700584
Adam Langleyfcf25832014-12-18 17:42:32 -0800585 if (s->tlsext_hostname) {
586 OPENSSL_free(s->tlsext_hostname);
587 }
588 if (s->initial_ctx) {
589 SSL_CTX_free(s->initial_ctx);
590 }
591 if (s->tlsext_ecpointformatlist) {
592 OPENSSL_free(s->tlsext_ecpointformatlist);
593 }
594 if (s->tlsext_ellipticcurvelist) {
595 OPENSSL_free(s->tlsext_ellipticcurvelist);
596 }
597 if (s->alpn_client_proto_list) {
598 OPENSSL_free(s->alpn_client_proto_list);
599 }
600 if (s->tlsext_channel_id_private) {
601 EVP_PKEY_free(s->tlsext_channel_id_private);
602 }
603 if (s->psk_identity_hint) {
604 OPENSSL_free(s->psk_identity_hint);
605 }
606 if (s->client_CA != NULL) {
607 sk_X509_NAME_pop_free(s->client_CA, X509_NAME_free);
608 }
609 if (s->next_proto_negotiated) {
610 OPENSSL_free(s->next_proto_negotiated);
611 }
612 if (s->srtp_profiles) {
613 sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);
614 }
Adam Langley95c29f32014-06-20 12:00:00 -0700615
Adam Langleyfcf25832014-12-18 17:42:32 -0800616 if (s->method != NULL) {
617 s->method->ssl_free(s);
618 }
619 if (s->ctx) {
620 SSL_CTX_free(s->ctx);
621 }
Adam Langley95c29f32014-06-20 12:00:00 -0700622
Adam Langleyfcf25832014-12-18 17:42:32 -0800623 OPENSSL_free(s);
624}
Adam Langley95c29f32014-06-20 12:00:00 -0700625
Adam Langleyfcf25832014-12-18 17:42:32 -0800626void SSL_set_bio(SSL *s, BIO *rbio, BIO *wbio) {
627 /* If the output buffering BIO is still in place, remove it. */
628 if (s->bbio != NULL) {
629 if (s->wbio == s->bbio) {
630 s->wbio = s->wbio->next_bio;
631 s->bbio->next_bio = NULL;
632 }
633 }
Adam Langley95c29f32014-06-20 12:00:00 -0700634
Adam Langleyfcf25832014-12-18 17:42:32 -0800635 if (s->rbio != NULL && s->rbio != rbio) {
636 BIO_free_all(s->rbio);
637 }
638 if (s->wbio != NULL && s->wbio != wbio && s->rbio != s->wbio) {
639 BIO_free_all(s->wbio);
640 }
641 s->rbio = rbio;
642 s->wbio = wbio;
643}
Adam Langley95c29f32014-06-20 12:00:00 -0700644
Adam Langleyfcf25832014-12-18 17:42:32 -0800645BIO *SSL_get_rbio(const SSL *s) { return s->rbio; }
Adam Langley95c29f32014-06-20 12:00:00 -0700646
Adam Langleyfcf25832014-12-18 17:42:32 -0800647BIO *SSL_get_wbio(const SSL *s) { return s->wbio; }
Adam Langley95c29f32014-06-20 12:00:00 -0700648
Adam Langleyfcf25832014-12-18 17:42:32 -0800649int SSL_get_fd(const SSL *s) { return SSL_get_rfd(s); }
Adam Langley95c29f32014-06-20 12:00:00 -0700650
Adam Langleyfcf25832014-12-18 17:42:32 -0800651int SSL_get_rfd(const SSL *s) {
652 int ret = -1;
653 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700654
Adam Langleyfcf25832014-12-18 17:42:32 -0800655 b = SSL_get_rbio(s);
656 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
657 if (r != NULL) {
658 BIO_get_fd(r, &ret);
659 }
660 return ret;
661}
Adam Langley95c29f32014-06-20 12:00:00 -0700662
Adam Langleyfcf25832014-12-18 17:42:32 -0800663int SSL_get_wfd(const SSL *s) {
664 int ret = -1;
665 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700666
Adam Langleyfcf25832014-12-18 17:42:32 -0800667 b = SSL_get_wbio(s);
668 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
669 if (r != NULL) {
670 BIO_get_fd(r, &ret);
671 }
Adam Langley95c29f32014-06-20 12:00:00 -0700672
Adam Langleyfcf25832014-12-18 17:42:32 -0800673 return ret;
674}
Adam Langley95c29f32014-06-20 12:00:00 -0700675
Adam Langleyfcf25832014-12-18 17:42:32 -0800676int SSL_set_fd(SSL *s, int fd) {
677 int ret = 0;
678 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700679
Adam Langleyfcf25832014-12-18 17:42:32 -0800680 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700681
Adam Langleyfcf25832014-12-18 17:42:32 -0800682 if (bio == NULL) {
683 OPENSSL_PUT_ERROR(SSL, SSL_set_fd, ERR_R_BUF_LIB);
684 goto err;
685 }
686 BIO_set_fd(bio, fd, BIO_NOCLOSE);
687 SSL_set_bio(s, bio, bio);
688 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700689
Adam Langley95c29f32014-06-20 12:00:00 -0700690err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800691 return ret;
692}
Adam Langley95c29f32014-06-20 12:00:00 -0700693
Adam Langleyfcf25832014-12-18 17:42:32 -0800694int SSL_set_wfd(SSL *s, int fd) {
695 int ret = 0;
696 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700697
Adam Langleyfcf25832014-12-18 17:42:32 -0800698 if (s->rbio == NULL || BIO_method_type(s->rbio) != BIO_TYPE_FD ||
699 (int)BIO_get_fd(s->rbio, NULL) != fd) {
700 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700701
Adam Langleyfcf25832014-12-18 17:42:32 -0800702 if (bio == NULL) {
703 OPENSSL_PUT_ERROR(SSL, SSL_set_wfd, ERR_R_BUF_LIB);
704 goto err;
705 }
706 BIO_set_fd(bio, fd, BIO_NOCLOSE);
707 SSL_set_bio(s, SSL_get_rbio(s), bio);
708 } else {
709 SSL_set_bio(s, SSL_get_rbio(s), SSL_get_rbio(s));
710 }
711
712 ret = 1;
713
Adam Langley95c29f32014-06-20 12:00:00 -0700714err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800715 return ret;
716}
Adam Langley95c29f32014-06-20 12:00:00 -0700717
Adam Langleyfcf25832014-12-18 17:42:32 -0800718int SSL_set_rfd(SSL *s, int fd) {
719 int ret = 0;
720 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700721
Adam Langleyfcf25832014-12-18 17:42:32 -0800722 if (s->wbio == NULL || BIO_method_type(s->wbio) != BIO_TYPE_FD ||
723 (int)BIO_get_fd(s->wbio, NULL) != fd) {
724 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700725
Adam Langleyfcf25832014-12-18 17:42:32 -0800726 if (bio == NULL) {
727 OPENSSL_PUT_ERROR(SSL, SSL_set_rfd, ERR_R_BUF_LIB);
728 goto err;
729 }
730 BIO_set_fd(bio, fd, BIO_NOCLOSE);
731 SSL_set_bio(s, bio, SSL_get_wbio(s));
732 } else {
733 SSL_set_bio(s, SSL_get_wbio(s), SSL_get_wbio(s));
734 }
735 ret = 1;
736
Adam Langley95c29f32014-06-20 12:00:00 -0700737err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800738 return ret;
739}
Adam Langley95c29f32014-06-20 12:00:00 -0700740
741/* return length of latest Finished message we sent, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -0800742size_t SSL_get_finished(const SSL *s, void *buf, size_t count) {
743 size_t ret = 0;
744
745 if (s->s3 != NULL) {
746 ret = s->s3->tmp.finish_md_len;
747 if (count > ret) {
748 count = ret;
749 }
750 memcpy(buf, s->s3->tmp.finish_md, count);
751 }
752
753 return ret;
754}
Adam Langley95c29f32014-06-20 12:00:00 -0700755
756/* return length of latest Finished message we expected, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -0800757size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count) {
758 size_t ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700759
Adam Langleyfcf25832014-12-18 17:42:32 -0800760 if (s->s3 != NULL) {
761 ret = s->s3->tmp.peer_finish_md_len;
762 if (count > ret) {
763 count = ret;
764 }
765 memcpy(buf, s->s3->tmp.peer_finish_md, count);
766 }
Adam Langley95c29f32014-06-20 12:00:00 -0700767
Adam Langleyfcf25832014-12-18 17:42:32 -0800768 return ret;
769}
Adam Langley95c29f32014-06-20 12:00:00 -0700770
Adam Langleyfcf25832014-12-18 17:42:32 -0800771int SSL_get_verify_mode(const SSL *s) { return s->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -0700772
Adam Langleyfcf25832014-12-18 17:42:32 -0800773int SSL_get_verify_depth(const SSL *s) {
774 return X509_VERIFY_PARAM_get_depth(s->param);
775}
Adam Langley95c29f32014-06-20 12:00:00 -0700776
Adam Langleyfcf25832014-12-18 17:42:32 -0800777int (*SSL_get_verify_callback(const SSL *s))(int, X509_STORE_CTX *) {
778 return s->verify_callback;
779}
Adam Langley95c29f32014-06-20 12:00:00 -0700780
Adam Langleyfcf25832014-12-18 17:42:32 -0800781int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) { return ctx->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -0700782
Adam Langleyfcf25832014-12-18 17:42:32 -0800783int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) {
784 return X509_VERIFY_PARAM_get_depth(ctx->param);
785}
Adam Langley95c29f32014-06-20 12:00:00 -0700786
Adam Langleyfcf25832014-12-18 17:42:32 -0800787int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int, X509_STORE_CTX *) {
788 return ctx->default_verify_callback;
789}
Adam Langley95c29f32014-06-20 12:00:00 -0700790
Adam Langleyfcf25832014-12-18 17:42:32 -0800791void SSL_set_verify(SSL *s, int mode,
792 int (*callback)(int ok, X509_STORE_CTX *ctx)) {
793 s->verify_mode = mode;
794 if (callback != NULL) {
795 s->verify_callback = callback;
796 }
797}
Adam Langley95c29f32014-06-20 12:00:00 -0700798
Adam Langleyfcf25832014-12-18 17:42:32 -0800799void SSL_set_verify_depth(SSL *s, int depth) {
800 X509_VERIFY_PARAM_set_depth(s->param, depth);
801}
Adam Langley95c29f32014-06-20 12:00:00 -0700802
Adam Langleyfcf25832014-12-18 17:42:32 -0800803void SSL_set_read_ahead(SSL *s, int yes) { s->read_ahead = yes; }
Adam Langley95c29f32014-06-20 12:00:00 -0700804
Adam Langleyfcf25832014-12-18 17:42:32 -0800805int SSL_get_read_ahead(const SSL *s) { return s->read_ahead; }
Adam Langley95c29f32014-06-20 12:00:00 -0700806
Adam Langleyfcf25832014-12-18 17:42:32 -0800807int SSL_pending(const SSL *s) {
808 /* SSL_pending cannot work properly if read-ahead is enabled
809 * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)), and it is
810 * impossible to fix since SSL_pending cannot report errors that may be
811 * observed while scanning the new data. (Note that SSL_pending() is often
812 * used as a boolean value, so we'd better not return -1.). */
813 return s->method->ssl_pending(s);
814}
Adam Langley95c29f32014-06-20 12:00:00 -0700815
Adam Langleyfcf25832014-12-18 17:42:32 -0800816X509 *SSL_get_peer_certificate(const SSL *s) {
817 X509 *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700818
Adam Langleyfcf25832014-12-18 17:42:32 -0800819 if (s == NULL || s->session == NULL) {
820 r = NULL;
821 } else {
822 r = s->session->peer;
823 }
Adam Langley95c29f32014-06-20 12:00:00 -0700824
Adam Langleyfcf25832014-12-18 17:42:32 -0800825 if (r == NULL) {
826 return NULL;
827 }
Adam Langley95c29f32014-06-20 12:00:00 -0700828
Adam Langleyfcf25832014-12-18 17:42:32 -0800829 return X509_up_ref(r);
830}
831
832STACK_OF(X509) * SSL_get_peer_cert_chain(const SSL *s) {
833 STACK_OF(X509) * r;
834
835 if (s == NULL || s->session == NULL || s->session->sess_cert == NULL) {
836 r = NULL;
837 } else {
838 r = s->session->sess_cert->cert_chain;
839 }
840
841 /* If we are a client, cert_chain includes the peer's own certificate; if we
842 * are a server, it does not. */
843 return r;
844}
Adam Langley95c29f32014-06-20 12:00:00 -0700845
Adam Langley95c29f32014-06-20 12:00:00 -0700846/* Fix this so it checks all the valid key/cert options */
Adam Langleyfcf25832014-12-18 17:42:32 -0800847int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
848 if (ctx == NULL || ctx->cert == NULL || ctx->cert->key->x509 == NULL) {
849 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key,
850 SSL_R_NO_CERTIFICATE_ASSIGNED);
851 return 0;
852 }
853
854 if (ctx->cert->key->privatekey == NULL) {
855 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key,
856 SSL_R_NO_PRIVATE_KEY_ASSIGNED);
857 return 0;
858 }
859
860 return X509_check_private_key(ctx->cert->key->x509,
861 ctx->cert->key->privatekey);
862}
Adam Langley95c29f32014-06-20 12:00:00 -0700863
864/* Fix this function so that it takes an optional type parameter */
Adam Langleyfcf25832014-12-18 17:42:32 -0800865int SSL_check_private_key(const SSL *ssl) {
866 if (ssl == NULL) {
867 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, ERR_R_PASSED_NULL_PARAMETER);
868 return 0;
869 }
Adam Langley95c29f32014-06-20 12:00:00 -0700870
Adam Langleyfcf25832014-12-18 17:42:32 -0800871 if (ssl->cert == NULL) {
872 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
873 SSL_R_NO_CERTIFICATE_ASSIGNED);
874 return 0;
875 }
Adam Langley95c29f32014-06-20 12:00:00 -0700876
Adam Langleyfcf25832014-12-18 17:42:32 -0800877 if (ssl->cert->key->x509 == NULL) {
878 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
879 SSL_R_NO_CERTIFICATE_ASSIGNED);
880 return 0;
881 }
David Benjamin0b145c22014-11-26 20:10:09 -0500882
Adam Langleyfcf25832014-12-18 17:42:32 -0800883 if (ssl->cert->key->privatekey == NULL) {
884 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
885 SSL_R_NO_PRIVATE_KEY_ASSIGNED);
886 return 0;
887 }
Adam Langley95c29f32014-06-20 12:00:00 -0700888
Adam Langleyfcf25832014-12-18 17:42:32 -0800889 return X509_check_private_key(ssl->cert->key->x509,
890 ssl->cert->key->privatekey);
891}
Adam Langley95c29f32014-06-20 12:00:00 -0700892
Adam Langleyfcf25832014-12-18 17:42:32 -0800893int SSL_accept(SSL *s) {
894 if (s->handshake_func == 0) {
895 /* Not properly initialized yet */
896 SSL_set_accept_state(s);
897 }
David Benjamin0b145c22014-11-26 20:10:09 -0500898
Adam Langleyfcf25832014-12-18 17:42:32 -0800899 if (s->handshake_func != s->method->ssl_accept) {
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +0100900 OPENSSL_PUT_ERROR(SSL, SSL_accept, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800901 return -1;
902 }
Adam Langley95c29f32014-06-20 12:00:00 -0700903
Adam Langleyfcf25832014-12-18 17:42:32 -0800904 return s->handshake_func(s);
905}
Adam Langley95c29f32014-06-20 12:00:00 -0700906
Adam Langleyfcf25832014-12-18 17:42:32 -0800907int SSL_connect(SSL *s) {
908 if (s->handshake_func == 0) {
909 /* Not properly initialized yet */
910 SSL_set_connect_state(s);
911 }
Adam Langley95c29f32014-06-20 12:00:00 -0700912
Adam Langleyfcf25832014-12-18 17:42:32 -0800913 if (s->handshake_func != s->method->ssl_connect) {
914 OPENSSL_PUT_ERROR(SSL, SSL_connect, ERR_R_INTERNAL_ERROR);
915 return -1;
916 }
Adam Langley95c29f32014-06-20 12:00:00 -0700917
Adam Langleyfcf25832014-12-18 17:42:32 -0800918 return s->handshake_func(s);
919}
Adam Langley95c29f32014-06-20 12:00:00 -0700920
Adam Langleyfcf25832014-12-18 17:42:32 -0800921long SSL_get_default_timeout(const SSL *s) {
922 return SSL_DEFAULT_SESSION_TIMEOUT;
923}
Adam Langley95c29f32014-06-20 12:00:00 -0700924
Adam Langleyfcf25832014-12-18 17:42:32 -0800925int SSL_read(SSL *s, void *buf, int num) {
926 if (s->handshake_func == 0) {
927 OPENSSL_PUT_ERROR(SSL, SSL_read, SSL_R_UNINITIALIZED);
928 return -1;
929 }
Adam Langley95c29f32014-06-20 12:00:00 -0700930
Adam Langleyfcf25832014-12-18 17:42:32 -0800931 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
932 s->rwstate = SSL_NOTHING;
933 return 0;
934 }
Adam Langley95c29f32014-06-20 12:00:00 -0700935
Adam Langleyfcf25832014-12-18 17:42:32 -0800936 return s->method->ssl_read(s, buf, num);
937}
Adam Langley95c29f32014-06-20 12:00:00 -0700938
Adam Langleyfcf25832014-12-18 17:42:32 -0800939int SSL_peek(SSL *s, void *buf, int num) {
940 if (s->handshake_func == 0) {
941 OPENSSL_PUT_ERROR(SSL, SSL_peek, SSL_R_UNINITIALIZED);
942 return -1;
943 }
Adam Langley95c29f32014-06-20 12:00:00 -0700944
Adam Langleyfcf25832014-12-18 17:42:32 -0800945 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
946 return 0;
947 }
Adam Langley95c29f32014-06-20 12:00:00 -0700948
Adam Langleyfcf25832014-12-18 17:42:32 -0800949 return s->method->ssl_peek(s, buf, num);
950}
Adam Langley95c29f32014-06-20 12:00:00 -0700951
Adam Langleyfcf25832014-12-18 17:42:32 -0800952int SSL_write(SSL *s, const void *buf, int num) {
953 if (s->handshake_func == 0) {
954 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_UNINITIALIZED);
955 return -1;
956 }
Adam Langley95c29f32014-06-20 12:00:00 -0700957
Adam Langleyfcf25832014-12-18 17:42:32 -0800958 if (s->shutdown & SSL_SENT_SHUTDOWN) {
959 s->rwstate = SSL_NOTHING;
960 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_PROTOCOL_IS_SHUTDOWN);
961 return -1;
962 }
Adam Langley95c29f32014-06-20 12:00:00 -0700963
Adam Langleyfcf25832014-12-18 17:42:32 -0800964 return s->method->ssl_write(s, buf, num);
965}
Adam Langley95c29f32014-06-20 12:00:00 -0700966
Adam Langleyfcf25832014-12-18 17:42:32 -0800967int SSL_shutdown(SSL *s) {
968 /* Note that this function behaves differently from what one might expect.
969 * Return values are 0 for no success (yet), 1 for success; but calling it
970 * once is usually not enough, even if blocking I/O is used (see
971 * ssl3_shutdown). */
Adam Langley95c29f32014-06-20 12:00:00 -0700972
Adam Langleyfcf25832014-12-18 17:42:32 -0800973 if (s->handshake_func == 0) {
974 OPENSSL_PUT_ERROR(SSL, SSL_shutdown, SSL_R_UNINITIALIZED);
975 return -1;
976 }
Adam Langley95c29f32014-06-20 12:00:00 -0700977
Adam Langleyfcf25832014-12-18 17:42:32 -0800978 if (!SSL_in_init(s)) {
979 return s->method->ssl_shutdown(s);
980 }
Adam Langley95c29f32014-06-20 12:00:00 -0700981
Adam Langleyfcf25832014-12-18 17:42:32 -0800982 return 1;
983}
Adam Langley95c29f32014-06-20 12:00:00 -0700984
Adam Langleyfcf25832014-12-18 17:42:32 -0800985int SSL_renegotiate(SSL *s) {
David Benjamin2cdace92015-01-24 13:30:02 -0500986 if (SSL_IS_DTLS(s)) {
987 /* Renegotiation is not supported for DTLS. */
988 OPENSSL_PUT_ERROR(SSL, SSL_renegotiate, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
989 return 0;
990 }
991
Adam Langleyfcf25832014-12-18 17:42:32 -0800992 if (s->renegotiate == 0) {
993 s->renegotiate = 1;
994 }
Adam Langley95c29f32014-06-20 12:00:00 -0700995
Adam Langleyfcf25832014-12-18 17:42:32 -0800996 s->new_session = 1;
997 return s->method->ssl_renegotiate(s);
998}
Adam Langley95c29f32014-06-20 12:00:00 -0700999
Adam Langleyfcf25832014-12-18 17:42:32 -08001000int SSL_renegotiate_pending(SSL *s) {
1001 /* becomes true when negotiation is requested; false again once a handshake
1002 * has finished */
1003 return s->renegotiate != 0;
1004}
Adam Langley95c29f32014-06-20 12:00:00 -07001005
Adam Langleyfcf25832014-12-18 17:42:32 -08001006long SSL_ctrl(SSL *s, int cmd, long larg, void *parg) {
1007 long l;
Adam Langley95c29f32014-06-20 12:00:00 -07001008
Adam Langleyfcf25832014-12-18 17:42:32 -08001009 switch (cmd) {
1010 case SSL_CTRL_GET_READ_AHEAD:
1011 return s->read_ahead;
Adam Langley95c29f32014-06-20 12:00:00 -07001012
Adam Langleyfcf25832014-12-18 17:42:32 -08001013 case SSL_CTRL_SET_READ_AHEAD:
1014 l = s->read_ahead;
1015 s->read_ahead = larg;
1016 return l;
Adam Langley95c29f32014-06-20 12:00:00 -07001017
Adam Langleyfcf25832014-12-18 17:42:32 -08001018 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1019 s->msg_callback_arg = parg;
1020 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001021
Adam Langleyfcf25832014-12-18 17:42:32 -08001022 case SSL_CTRL_OPTIONS:
1023 return s->options |= larg;
Adam Langley95c29f32014-06-20 12:00:00 -07001024
Adam Langleyfcf25832014-12-18 17:42:32 -08001025 case SSL_CTRL_CLEAR_OPTIONS:
1026 return s->options &= ~larg;
Adam Langley95c29f32014-06-20 12:00:00 -07001027
Adam Langleyfcf25832014-12-18 17:42:32 -08001028 case SSL_CTRL_MODE:
1029 return s->mode |= larg;
Adam Langley95c29f32014-06-20 12:00:00 -07001030
Adam Langleyfcf25832014-12-18 17:42:32 -08001031 case SSL_CTRL_CLEAR_MODE:
1032 return s->mode &= ~larg;
Adam Langley95c29f32014-06-20 12:00:00 -07001033
Adam Langleyfcf25832014-12-18 17:42:32 -08001034 case SSL_CTRL_GET_MAX_CERT_LIST:
1035 return s->max_cert_list;
Adam Langley95c29f32014-06-20 12:00:00 -07001036
Adam Langleyfcf25832014-12-18 17:42:32 -08001037 case SSL_CTRL_SET_MAX_CERT_LIST:
1038 l = s->max_cert_list;
1039 s->max_cert_list = larg;
1040 return l;
Adam Langley95c29f32014-06-20 12:00:00 -07001041
Adam Langleyfcf25832014-12-18 17:42:32 -08001042 case SSL_CTRL_SET_MTU:
1043 if (larg < (long)dtls1_min_mtu()) {
1044 return 0;
1045 }
1046 if (SSL_IS_DTLS(s)) {
1047 s->d1->mtu = larg;
1048 return larg;
1049 }
1050 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001051
Adam Langleyfcf25832014-12-18 17:42:32 -08001052 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1053 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH) {
1054 return 0;
1055 }
1056 s->max_send_fragment = larg;
1057 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001058
Adam Langleyfcf25832014-12-18 17:42:32 -08001059 case SSL_CTRL_GET_RI_SUPPORT:
1060 if (s->s3) {
1061 return s->s3->send_connection_binding;
1062 }
1063 return 0;
1064
1065 case SSL_CTRL_CERT_FLAGS:
1066 return s->cert->cert_flags |= larg;
1067
1068 case SSL_CTRL_CLEAR_CERT_FLAGS:
1069 return s->cert->cert_flags &= ~larg;
1070
1071 case SSL_CTRL_GET_RAW_CIPHERLIST:
1072 if (parg) {
1073 if (s->cert->ciphers_raw == NULL) {
1074 return 0;
1075 }
1076 *(uint8_t **)parg = s->cert->ciphers_raw;
1077 return (int)s->cert->ciphers_rawlen;
1078 }
1079
1080 /* Passing a NULL |parg| returns the size of a single
1081 * cipher suite value. */
1082 return 2;
1083
1084 default:
1085 return s->method->ssl_ctrl(s, cmd, larg, parg);
1086 }
1087}
1088
1089long SSL_callback_ctrl(SSL *s, int cmd, void (*fp)(void)) {
1090 switch (cmd) {
1091 case SSL_CTRL_SET_MSG_CALLBACK:
1092 s->msg_callback =
1093 (void (*)(int write_p, int version, int content_type, const void *buf,
1094 size_t len, SSL *ssl, void *arg))(fp);
1095 return 1;
1096
1097 default:
1098 return s->method->ssl_callback_ctrl(s, cmd, fp);
1099 }
1100}
1101
1102LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) { return ctx->sessions; }
1103
1104long SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg) {
1105 long l;
1106
1107 switch (cmd) {
1108 case SSL_CTRL_GET_READ_AHEAD:
1109 return ctx->read_ahead;
1110
1111 case SSL_CTRL_SET_READ_AHEAD:
1112 l = ctx->read_ahead;
1113 ctx->read_ahead = larg;
1114 return l;
1115
1116 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1117 ctx->msg_callback_arg = parg;
1118 return 1;
1119
1120 case SSL_CTRL_GET_MAX_CERT_LIST:
1121 return ctx->max_cert_list;
1122
1123 case SSL_CTRL_SET_MAX_CERT_LIST:
1124 l = ctx->max_cert_list;
1125 ctx->max_cert_list = larg;
1126 return l;
1127
1128 case SSL_CTRL_SET_SESS_CACHE_SIZE:
1129 l = ctx->session_cache_size;
1130 ctx->session_cache_size = larg;
1131 return l;
1132
1133 case SSL_CTRL_GET_SESS_CACHE_SIZE:
1134 return ctx->session_cache_size;
1135
1136 case SSL_CTRL_SET_SESS_CACHE_MODE:
1137 l = ctx->session_cache_mode;
1138 ctx->session_cache_mode = larg;
1139 return l;
1140
1141 case SSL_CTRL_GET_SESS_CACHE_MODE:
1142 return ctx->session_cache_mode;
1143
1144 case SSL_CTRL_SESS_NUMBER:
1145 return lh_SSL_SESSION_num_items(ctx->sessions);
1146
1147 case SSL_CTRL_SESS_CONNECT:
Adam Langleyfcf25832014-12-18 17:42:32 -08001148 case SSL_CTRL_SESS_CONNECT_GOOD:
Adam Langleyfcf25832014-12-18 17:42:32 -08001149 case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001150 case SSL_CTRL_SESS_ACCEPT:
Adam Langleyfcf25832014-12-18 17:42:32 -08001151 case SSL_CTRL_SESS_ACCEPT_GOOD:
Adam Langleyfcf25832014-12-18 17:42:32 -08001152 case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001153 case SSL_CTRL_SESS_HIT:
Adam Langleyfcf25832014-12-18 17:42:32 -08001154 case SSL_CTRL_SESS_CB_HIT:
Adam Langleyfcf25832014-12-18 17:42:32 -08001155 case SSL_CTRL_SESS_MISSES:
Adam Langleyfcf25832014-12-18 17:42:32 -08001156 case SSL_CTRL_SESS_TIMEOUTS:
Adam Langleyfcf25832014-12-18 17:42:32 -08001157 case SSL_CTRL_SESS_CACHE_FULL:
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001158 /* Statistics are no longer supported.
1159 * TODO(davidben): Try to remove the accessors altogether. */
1160 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001161
1162 case SSL_CTRL_OPTIONS:
1163 return ctx->options |= larg;
1164
1165 case SSL_CTRL_CLEAR_OPTIONS:
1166 return ctx->options &= ~larg;
1167
1168 case SSL_CTRL_MODE:
1169 return ctx->mode |= larg;
1170
1171 case SSL_CTRL_CLEAR_MODE:
1172 return ctx->mode &= ~larg;
1173
1174 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1175 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH) {
1176 return 0;
1177 }
1178 ctx->max_send_fragment = larg;
1179 return 1;
1180
1181 case SSL_CTRL_CERT_FLAGS:
1182 return ctx->cert->cert_flags |= larg;
1183
1184 case SSL_CTRL_CLEAR_CERT_FLAGS:
1185 return ctx->cert->cert_flags &= ~larg;
1186
1187 default:
1188 return ctx->method->ssl_ctx_ctrl(ctx, cmd, larg, parg);
1189 }
1190}
1191
1192long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void)) {
1193 switch (cmd) {
1194 case SSL_CTRL_SET_MSG_CALLBACK:
1195 ctx->msg_callback =
1196 (void (*)(int write_p, int version, int content_type, const void *buf,
1197 size_t len, SSL *ssl, void *arg))(fp);
1198 return 1;
1199
1200 default:
1201 return ctx->method->ssl_ctx_callback_ctrl(ctx, cmd, fp);
1202 }
1203}
1204
1205int ssl_cipher_id_cmp(const void *in_a, const void *in_b) {
1206 long l;
1207 const SSL_CIPHER *a = in_a;
1208 const SSL_CIPHER *b = in_b;
1209 const long a_id = a->id;
1210 const long b_id = b->id;
1211
1212 l = a_id - b_id;
1213 if (l == 0L) {
1214 return 0;
1215 } else {
1216 return (l > 0) ? 1 : -1;
1217 }
1218}
1219
1220int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **ap, const SSL_CIPHER **bp) {
1221 long l;
1222 const long a_id = (*ap)->id;
1223 const long b_id = (*bp)->id;
1224
1225 l = a_id - b_id;
1226 if (l == 0) {
1227 return 0;
1228 } else {
1229 return (l > 0) ? 1 : -1;
1230 }
1231}
1232
1233/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001234 * preference */
Adam Langleyfcf25832014-12-18 17:42:32 -08001235STACK_OF(SSL_CIPHER) * SSL_get_ciphers(const SSL *s) {
1236 if (s == NULL) {
1237 return NULL;
1238 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001239
Adam Langleyfcf25832014-12-18 17:42:32 -08001240 if (s->cipher_list != NULL) {
1241 return s->cipher_list->ciphers;
1242 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001243
Adam Langleyfcf25832014-12-18 17:42:32 -08001244 if (s->version >= TLS1_1_VERSION && s->ctx != NULL &&
1245 s->ctx->cipher_list_tls11 != NULL) {
1246 return s->ctx->cipher_list_tls11->ciphers;
1247 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001248
Adam Langleyfcf25832014-12-18 17:42:32 -08001249 if (s->ctx != NULL && s->ctx->cipher_list != NULL) {
1250 return s->ctx->cipher_list->ciphers;
1251 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001252
Adam Langleyfcf25832014-12-18 17:42:32 -08001253 return NULL;
1254}
Adam Langley95c29f32014-06-20 12:00:00 -07001255
Adam Langleyfcf25832014-12-18 17:42:32 -08001256/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001257 * algorithm id */
Adam Langleyfcf25832014-12-18 17:42:32 -08001258STACK_OF(SSL_CIPHER) * ssl_get_ciphers_by_id(SSL *s) {
1259 if (s == NULL) {
1260 return NULL;
1261 }
Adam Langley95c29f32014-06-20 12:00:00 -07001262
Adam Langleyfcf25832014-12-18 17:42:32 -08001263 if (s->cipher_list_by_id != NULL) {
1264 return s->cipher_list_by_id;
1265 }
Adam Langley95c29f32014-06-20 12:00:00 -07001266
Adam Langleyfcf25832014-12-18 17:42:32 -08001267 if (s->ctx != NULL && s->ctx->cipher_list_by_id != NULL) {
1268 return s->ctx->cipher_list_by_id;
1269 }
Adam Langley95c29f32014-06-20 12:00:00 -07001270
Adam Langleyfcf25832014-12-18 17:42:32 -08001271 return NULL;
1272}
Adam Langley95c29f32014-06-20 12:00:00 -07001273
Adam Langleyfcf25832014-12-18 17:42:32 -08001274/* The old interface to get the same thing as SSL_get_ciphers() */
1275const char *SSL_get_cipher_list(const SSL *s, int n) {
1276 const SSL_CIPHER *c;
1277 STACK_OF(SSL_CIPHER) * sk;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001278
Adam Langleyfcf25832014-12-18 17:42:32 -08001279 if (s == NULL) {
1280 return NULL;
1281 }
Adam Langley95c29f32014-06-20 12:00:00 -07001282
Adam Langleyfcf25832014-12-18 17:42:32 -08001283 sk = SSL_get_ciphers(s);
1284 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
1285 return NULL;
1286 }
Adam Langley95c29f32014-06-20 12:00:00 -07001287
Adam Langleyfcf25832014-12-18 17:42:32 -08001288 c = sk_SSL_CIPHER_value(sk, n);
1289 if (c == NULL) {
1290 return NULL;
1291 }
Adam Langley95c29f32014-06-20 12:00:00 -07001292
Adam Langleyfcf25832014-12-18 17:42:32 -08001293 return c->name;
1294}
David Benjamin5491e3f2014-09-29 19:33:09 -04001295
Adam Langleyfcf25832014-12-18 17:42:32 -08001296/* specify the ciphers to be used by default by the SSL_CTX */
1297int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
1298 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001299
Adam Langleyfcf25832014-12-18 17:42:32 -08001300 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list,
1301 &ctx->cipher_list_by_id, str, ctx->cert);
1302 /* ssl_create_cipher_list may return an empty stack if it was unable to find
1303 * a cipher matching the given rule string (for example if the rule string
1304 * specifies a cipher which has been disabled). This is not an error as far
1305 * as ssl_create_cipher_list is concerned, and hence ctx->cipher_list and
1306 * ctx->cipher_list_by_id has been updated. */
1307 if (sk == NULL) {
1308 return 0;
1309 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1310 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1311 return 0;
1312 }
Adam Langley95c29f32014-06-20 12:00:00 -07001313
Adam Langleyfcf25832014-12-18 17:42:32 -08001314 return 1;
1315}
David Benjamin39482a12014-07-20 13:30:15 -04001316
Adam Langleyfcf25832014-12-18 17:42:32 -08001317int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str) {
1318 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001319
Adam Langleyfcf25832014-12-18 17:42:32 -08001320 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str,
1321 ctx->cert);
1322 if (sk == NULL) {
1323 return 0;
1324 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1325 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list_tls11,
1326 SSL_R_NO_CIPHER_MATCH);
1327 return 0;
1328 }
David Benjamin9f2c0d72014-10-21 22:00:19 -04001329
Adam Langleyfcf25832014-12-18 17:42:32 -08001330 return 1;
1331}
Adam Langley95c29f32014-06-20 12:00:00 -07001332
Adam Langleyfcf25832014-12-18 17:42:32 -08001333/* specify the ciphers to be used by the SSL */
1334int SSL_set_cipher_list(SSL *s, const char *str) {
1335 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001336
Adam Langleyfcf25832014-12-18 17:42:32 -08001337 sk = ssl_create_cipher_list(s->ctx->method, &s->cipher_list,
1338 &s->cipher_list_by_id, str, s->cert);
David Benjamin39482a12014-07-20 13:30:15 -04001339
Adam Langleyfcf25832014-12-18 17:42:32 -08001340 /* see comment in SSL_CTX_set_cipher_list */
1341 if (sk == NULL) {
1342 return 0;
1343 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1344 OPENSSL_PUT_ERROR(SSL, SSL_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1345 return 0;
1346 }
David Benjamin39482a12014-07-20 13:30:15 -04001347
Adam Langleyfcf25832014-12-18 17:42:32 -08001348 return 1;
1349}
Adam Langley95c29f32014-06-20 12:00:00 -07001350
Adam Langleyfcf25832014-12-18 17:42:32 -08001351int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, uint8_t *p) {
1352 size_t i;
1353 const SSL_CIPHER *c;
1354 CERT *ct = s->cert;
1355 uint8_t *q;
1356 /* Set disabled masks for this session */
1357 ssl_set_client_disabled(s);
Adam Langley29707792014-06-20 12:00:00 -07001358
Adam Langleyfcf25832014-12-18 17:42:32 -08001359 if (sk == NULL) {
1360 return 0;
1361 }
1362 q = p;
Adam Langley95c29f32014-06-20 12:00:00 -07001363
Adam Langleyfcf25832014-12-18 17:42:32 -08001364 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
1365 c = sk_SSL_CIPHER_value(sk, i);
1366 /* Skip disabled ciphers */
1367 if (c->algorithm_ssl & ct->mask_ssl ||
1368 c->algorithm_mkey & ct->mask_k ||
1369 c->algorithm_auth & ct->mask_a) {
1370 continue;
1371 }
1372 s2n(ssl3_get_cipher_value(c), p);
1373 }
1374
1375 /* If all ciphers were disabled, return the error to the caller. */
1376 if (p == q) {
1377 return 0;
1378 }
1379
1380 /* Add SCSVs. */
1381 if (!s->renegotiate) {
1382 s2n(SSL3_CK_SCSV & 0xffff, p);
1383 }
1384
Adam Langley5f0efe02015-02-20 13:03:16 -08001385 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001386 s2n(SSL3_CK_FALLBACK_SCSV & 0xffff, p);
1387 }
1388
1389 return p - q;
1390}
1391
1392STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs) {
1393 CBS cipher_suites = *cbs;
1394 const SSL_CIPHER *c;
1395 STACK_OF(SSL_CIPHER) * sk;
1396
1397 if (s->s3) {
1398 s->s3->send_connection_binding = 0;
1399 }
1400
1401 if (CBS_len(&cipher_suites) % 2 != 0) {
1402 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1403 SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
1404 return NULL;
1405 }
1406
1407 sk = sk_SSL_CIPHER_new_null();
1408 if (sk == NULL) {
1409 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1410 goto err;
1411 }
1412
1413 if (!CBS_stow(&cipher_suites, &s->cert->ciphers_raw,
1414 &s->cert->ciphers_rawlen)) {
1415 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1416 goto err;
1417 }
1418
1419 while (CBS_len(&cipher_suites) > 0) {
1420 uint16_t cipher_suite;
1421
1422 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
1423 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_INTERNAL_ERROR);
1424 goto err;
1425 }
1426
1427 /* Check for SCSV. */
1428 if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff)) {
1429 /* SCSV is fatal if renegotiating. */
1430 if (s->renegotiate) {
1431 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1432 SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1433 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1434 goto err;
1435 }
1436 s->s3->send_connection_binding = 1;
1437 continue;
1438 }
1439
1440 /* Check for FALLBACK_SCSV. */
1441 if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff)) {
1442 uint16_t max_version = ssl3_get_max_server_version(s);
1443 if (SSL_IS_DTLS(s) ? (uint16_t)s->version > max_version
1444 : (uint16_t)s->version < max_version) {
1445 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1446 SSL_R_INAPPROPRIATE_FALLBACK);
1447 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
1448 goto err;
1449 }
1450 continue;
1451 }
1452
1453 c = ssl3_get_cipher_by_value(cipher_suite);
1454 if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
1455 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1456 goto err;
1457 }
1458 }
1459
1460 return sk;
David Benjamin9f2c0d72014-10-21 22:00:19 -04001461
Adam Langley95c29f32014-06-20 12:00:00 -07001462err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001463 if (sk != NULL) {
1464 sk_SSL_CIPHER_free(sk);
1465 }
1466 return NULL;
1467}
Adam Langley95c29f32014-06-20 12:00:00 -07001468
1469
Adam Langleyfcf25832014-12-18 17:42:32 -08001470/* return a servername extension value if provided in Client Hello, or NULL. So
1471 * far, only host_name types are defined (RFC 3546). */
1472const char *SSL_get_servername(const SSL *s, const int type) {
1473 if (type != TLSEXT_NAMETYPE_host_name) {
1474 return NULL;
1475 }
Adam Langley95c29f32014-06-20 12:00:00 -07001476
Adam Langleyfcf25832014-12-18 17:42:32 -08001477 return s->session && !s->tlsext_hostname ? s->session->tlsext_hostname
1478 : s->tlsext_hostname;
1479}
Adam Langley95c29f32014-06-20 12:00:00 -07001480
Adam Langleyfcf25832014-12-18 17:42:32 -08001481int SSL_get_servername_type(const SSL *s) {
1482 if (s->session &&
1483 (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname)) {
1484 return TLSEXT_NAMETYPE_host_name;
1485 }
Adam Langley95c29f32014-06-20 12:00:00 -07001486
Adam Langleyfcf25832014-12-18 17:42:32 -08001487 return -1;
1488}
Adam Langley95c29f32014-06-20 12:00:00 -07001489
Adam Langleyfcf25832014-12-18 17:42:32 -08001490void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
1491 ctx->signed_cert_timestamps_enabled = 1;
1492}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001493
Adam Langleyfcf25832014-12-18 17:42:32 -08001494int SSL_enable_signed_cert_timestamps(SSL *ssl) {
1495 ssl->signed_cert_timestamps_enabled = 1;
1496 return 1;
1497}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001498
Adam Langleyfcf25832014-12-18 17:42:32 -08001499void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
1500 ctx->ocsp_stapling_enabled = 1;
1501}
David Benjamin6c7aed02014-08-27 16:42:38 -04001502
Adam Langleyfcf25832014-12-18 17:42:32 -08001503int SSL_enable_ocsp_stapling(SSL *ssl) {
1504 ssl->ocsp_stapling_enabled = 1;
1505 return 1;
1506}
David Benjamin6c7aed02014-08-27 16:42:38 -04001507
Adam Langleyfcf25832014-12-18 17:42:32 -08001508void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
1509 size_t *out_len) {
1510 SSL_SESSION *session = ssl->session;
Adam Langley3cb50e02014-08-26 14:00:31 -07001511
Adam Langleyfcf25832014-12-18 17:42:32 -08001512 *out_len = 0;
1513 *out = NULL;
1514 if (ssl->server || !session || !session->tlsext_signed_cert_timestamp_list) {
1515 return;
1516 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001517
Adam Langleyfcf25832014-12-18 17:42:32 -08001518 *out = session->tlsext_signed_cert_timestamp_list;
1519 *out_len = session->tlsext_signed_cert_timestamp_list_length;
1520}
David Benjamin6c7aed02014-08-27 16:42:38 -04001521
Adam Langleyfcf25832014-12-18 17:42:32 -08001522void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
1523 size_t *out_len) {
1524 SSL_SESSION *session = ssl->session;
1525
1526 *out_len = 0;
1527 *out = NULL;
1528 if (ssl->server || !session || !session->ocsp_response) {
1529 return;
1530 }
1531 *out = session->ocsp_response;
1532 *out_len = session->ocsp_response_length;
1533}
David Benjamin6c7aed02014-08-27 16:42:38 -04001534
Adam Langley95c29f32014-06-20 12:00:00 -07001535/* SSL_select_next_proto implements the standard protocol selection. It is
1536 * expected that this function is called from the callback set by
1537 * SSL_CTX_set_next_proto_select_cb.
1538 *
1539 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1540 * strings. The length byte itself is not included in the length. A byte
1541 * string of length 0 is invalid. No byte string may be truncated.
1542 *
1543 * The current, but experimental algorithm for selecting the protocol is:
1544 *
1545 * 1) If the server doesn't support NPN then this is indicated to the
1546 * callback. In this case, the client application has to abort the connection
1547 * or have a default application level protocol.
1548 *
1549 * 2) If the server supports NPN, but advertises an empty list then the
1550 * client selects the first protcol in its list, but indicates via the
1551 * API that this fallback case was enacted.
1552 *
1553 * 3) Otherwise, the client finds the first protocol in the server's list
1554 * that it supports and selects this protocol. This is because it's
1555 * assumed that the server has better information about which protocol
1556 * a client should use.
1557 *
1558 * 4) If the client doesn't support any of the server's advertised
1559 * protocols, then this is treated the same as case 2.
1560 *
1561 * It returns either
1562 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1563 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1564 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001565int SSL_select_next_proto(uint8_t **out, uint8_t *outlen, const uint8_t *server,
1566 unsigned int server_len, const uint8_t *client,
1567 unsigned int client_len) {
1568 unsigned int i, j;
1569 const uint8_t *result;
1570 int status = OPENSSL_NPN_UNSUPPORTED;
Adam Langley95c29f32014-06-20 12:00:00 -07001571
Adam Langleyfcf25832014-12-18 17:42:32 -08001572 /* For each protocol in server preference order, see if we support it. */
1573 for (i = 0; i < server_len;) {
1574 for (j = 0; j < client_len;) {
1575 if (server[i] == client[j] &&
1576 memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
1577 /* We found a match */
1578 result = &server[i];
1579 status = OPENSSL_NPN_NEGOTIATED;
1580 goto found;
1581 }
1582 j += client[j];
1583 j++;
1584 }
1585 i += server[i];
1586 i++;
1587 }
Adam Langley95c29f32014-06-20 12:00:00 -07001588
Adam Langleyfcf25832014-12-18 17:42:32 -08001589 /* There's no overlap between our protocols and the server's list. */
1590 result = client;
1591 status = OPENSSL_NPN_NO_OVERLAP;
Adam Langley95c29f32014-06-20 12:00:00 -07001592
Adam Langleyfcf25832014-12-18 17:42:32 -08001593found:
1594 *out = (uint8_t *)result + 1;
1595 *outlen = result[0];
1596 return status;
1597}
Adam Langley95c29f32014-06-20 12:00:00 -07001598
Adam Langley95c29f32014-06-20 12:00:00 -07001599/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
1600 * requested protocol for this connection and returns 0. If the client didn't
1601 * request any protocol, then *data is set to NULL.
1602 *
1603 * Note that the client can request any protocol it chooses. The value returned
1604 * from this function need not be a member of the list of supported protocols
Adam Langleyfcf25832014-12-18 17:42:32 -08001605 * provided by the callback. */
1606void SSL_get0_next_proto_negotiated(const SSL *s, const uint8_t **data,
1607 unsigned *len) {
1608 *data = s->next_proto_negotiated;
1609 if (!*data) {
1610 *len = 0;
1611 } else {
1612 *len = s->next_proto_negotiated_len;
1613 }
Adam Langley95c29f32014-06-20 12:00:00 -07001614}
1615
1616/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
1617 * TLS server needs a list of supported protocols for Next Protocol
1618 * Negotiation. The returned list must be in wire format. The list is returned
1619 * by setting |out| to point to it and |outlen| to its length. This memory will
1620 * not be modified, but one should assume that the SSL* keeps a reference to
1621 * it.
1622 *
Adam Langleyfcf25832014-12-18 17:42:32 -08001623 * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise.
1624 * Otherwise, no such extension will be included in the ServerHello. */
1625void SSL_CTX_set_next_protos_advertised_cb(
1626 SSL_CTX *ctx,
1627 int (*cb)(SSL *ssl, const uint8_t **out, unsigned int *outlen, void *arg),
1628 void *arg) {
1629 ctx->next_protos_advertised_cb = cb;
1630 ctx->next_protos_advertised_cb_arg = arg;
1631}
Adam Langley95c29f32014-06-20 12:00:00 -07001632
1633/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1634 * client needs to select a protocol from the server's provided list. |out|
1635 * must be set to point to the selected protocol (which may be within |in|).
1636 * The length of the protocol name must be written into |outlen|. The server's
1637 * advertised protocols are provided in |in| and |inlen|. The callback can
1638 * assume that |in| is syntactically valid.
1639 *
1640 * The client must select a protocol. It is fatal to the connection if this
1641 * callback returns a value other than SSL_TLSEXT_ERR_OK.
1642 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001643void SSL_CTX_set_next_proto_select_cb(
1644 SSL_CTX *ctx, int (*cb)(SSL *s, uint8_t **out, uint8_t *outlen,
1645 const uint8_t *in, unsigned int inlen, void *arg),
1646 void *arg) {
1647 ctx->next_proto_select_cb = cb;
1648 ctx->next_proto_select_cb_arg = arg;
1649}
Adam Langley95c29f32014-06-20 12:00:00 -07001650
Adam Langleyfcf25832014-12-18 17:42:32 -08001651int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
1652 unsigned protos_len) {
1653 if (ctx->alpn_client_proto_list) {
1654 OPENSSL_free(ctx->alpn_client_proto_list);
1655 }
Adam Langley95c29f32014-06-20 12:00:00 -07001656
Adam Langleyfcf25832014-12-18 17:42:32 -08001657 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1658 if (!ctx->alpn_client_proto_list) {
1659 return 1;
1660 }
1661 ctx->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001662
Adam Langleyfcf25832014-12-18 17:42:32 -08001663 return 0;
1664}
Adam Langley95c29f32014-06-20 12:00:00 -07001665
Adam Langleyfcf25832014-12-18 17:42:32 -08001666int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
1667 if (ssl->alpn_client_proto_list) {
1668 OPENSSL_free(ssl->alpn_client_proto_list);
1669 }
Adam Langley95c29f32014-06-20 12:00:00 -07001670
Adam Langleyfcf25832014-12-18 17:42:32 -08001671 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1672 if (!ssl->alpn_client_proto_list) {
1673 return 1;
1674 }
1675 ssl->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001676
Adam Langleyfcf25832014-12-18 17:42:32 -08001677 return 0;
1678}
Adam Langley95c29f32014-06-20 12:00:00 -07001679
1680/* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
1681 * during ClientHello processing in order to select an ALPN protocol from the
1682 * client's list of offered protocols. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001683void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
1684 int (*cb)(SSL *ssl, const uint8_t **out,
1685 uint8_t *outlen, const uint8_t *in,
1686 unsigned int inlen, void *arg),
1687 void *arg) {
1688 ctx->alpn_select_cb = cb;
1689 ctx->alpn_select_cb_arg = arg;
1690}
Adam Langley95c29f32014-06-20 12:00:00 -07001691
1692/* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
1693 * On return it sets |*data| to point to |*len| bytes of protocol name (not
1694 * including the leading length-prefix byte). If the server didn't respond with
1695 * a negotiated protocol then |*len| will be zero. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001696void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **data,
1697 unsigned *len) {
1698 *data = NULL;
1699 if (ssl->s3) {
1700 *data = ssl->s3->alpn_selected;
1701 }
1702 if (*data == NULL) {
1703 *len = 0;
1704 } else {
1705 *len = ssl->s3->alpn_selected_len;
1706 }
1707}
Adam Langley95c29f32014-06-20 12:00:00 -07001708
David Benjamincfd248b2015-04-03 11:02:24 -04001709int SSL_export_keying_material(SSL *s, uint8_t *out, size_t out_len,
1710 const char *label, size_t label_len,
1711 const uint8_t *context, size_t context_len,
1712 int use_context) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001713 if (s->version < TLS1_VERSION) {
David Benjaminc565ebb2015-04-03 04:06:36 -04001714 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001715 }
Adam Langley95c29f32014-06-20 12:00:00 -07001716
David Benjamincfd248b2015-04-03 11:02:24 -04001717 return s->enc_method->export_keying_material(
1718 s, out, out_len, label, label_len, context, context_len, use_context);
Adam Langleyfcf25832014-12-18 17:42:32 -08001719}
Adam Langley95c29f32014-06-20 12:00:00 -07001720
Adam Langleyfcf25832014-12-18 17:42:32 -08001721static uint32_t ssl_session_hash(const SSL_SESSION *a) {
1722 uint32_t hash =
1723 ((uint32_t)a->session_id[0]) ||
1724 ((uint32_t)a->session_id[1] << 8) ||
1725 ((uint32_t)a->session_id[2] << 16) ||
1726 ((uint32_t)a->session_id[3] << 24);
Adam Langley95c29f32014-06-20 12:00:00 -07001727
Adam Langleyfcf25832014-12-18 17:42:32 -08001728 return hash;
1729}
Adam Langley95c29f32014-06-20 12:00:00 -07001730
1731/* NB: If this function (or indeed the hash function which uses a sort of
1732 * coarser function than this one) is changed, ensure
1733 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1734 * able to construct an SSL_SESSION that will collide with any existing session
1735 * with a matching session ID. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001736static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
1737 if (a->ssl_version != b->ssl_version) {
1738 return 1;
1739 }
Adam Langley95c29f32014-06-20 12:00:00 -07001740
Adam Langleyfcf25832014-12-18 17:42:32 -08001741 if (a->session_id_length != b->session_id_length) {
1742 return 1;
1743 }
Adam Langley95c29f32014-06-20 12:00:00 -07001744
Adam Langleyfcf25832014-12-18 17:42:32 -08001745 return memcmp(a->session_id, b->session_id, a->session_id_length);
1746}
Adam Langley95c29f32014-06-20 12:00:00 -07001747
Adam Langleyfcf25832014-12-18 17:42:32 -08001748SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth) {
1749 SSL_CTX *ret = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001750
Adam Langleyfcf25832014-12-18 17:42:32 -08001751 if (meth == NULL) {
1752 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_NULL_SSL_METHOD_PASSED);
1753 return NULL;
1754 }
Adam Langley95c29f32014-06-20 12:00:00 -07001755
Adam Langleyfcf25832014-12-18 17:42:32 -08001756 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
1757 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
1758 goto err;
1759 }
Adam Langley95c29f32014-06-20 12:00:00 -07001760
Adam Langleyfcf25832014-12-18 17:42:32 -08001761 ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1762 if (ret == NULL) {
1763 goto err;
1764 }
Adam Langley95c29f32014-06-20 12:00:00 -07001765
Adam Langleyfcf25832014-12-18 17:42:32 -08001766 memset(ret, 0, sizeof(SSL_CTX));
Adam Langley95c29f32014-06-20 12:00:00 -07001767
Adam Langleyfcf25832014-12-18 17:42:32 -08001768 ret->method = meth->method;
Adam Langley95c29f32014-06-20 12:00:00 -07001769
Adam Langleyfcf25832014-12-18 17:42:32 -08001770 ret->cert_store = NULL;
1771 ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
1772 ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1773 ret->session_cache_head = NULL;
1774 ret->session_cache_tail = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001775
Adam Langleyfcf25832014-12-18 17:42:32 -08001776 /* We take the system default */
1777 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001778
Adam Langleyfcf25832014-12-18 17:42:32 -08001779 ret->new_session_cb = 0;
1780 ret->remove_session_cb = 0;
1781 ret->get_session_cb = 0;
1782 ret->generate_session_id = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001783
Adam Langleyfcf25832014-12-18 17:42:32 -08001784 ret->references = 1;
1785 ret->quiet_shutdown = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001786
Adam Langleyfcf25832014-12-18 17:42:32 -08001787 ret->info_callback = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001788
Adam Langleyfcf25832014-12-18 17:42:32 -08001789 ret->app_verify_callback = 0;
1790 ret->app_verify_arg = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001791
Adam Langleyfcf25832014-12-18 17:42:32 -08001792 ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
1793 ret->read_ahead = 0;
1794 ret->msg_callback = 0;
1795 ret->msg_callback_arg = NULL;
1796 ret->verify_mode = SSL_VERIFY_NONE;
1797 ret->sid_ctx_length = 0;
1798 ret->default_verify_callback = NULL;
1799 ret->cert = ssl_cert_new();
1800 if (ret->cert == NULL) {
1801 goto err;
1802 }
Adam Langley95c29f32014-06-20 12:00:00 -07001803
Adam Langleyfcf25832014-12-18 17:42:32 -08001804 ret->default_passwd_callback = 0;
1805 ret->default_passwd_callback_userdata = NULL;
1806 ret->client_cert_cb = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001807
Adam Langleyfcf25832014-12-18 17:42:32 -08001808 ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
1809 if (ret->sessions == NULL) {
1810 goto err;
1811 }
1812 ret->cert_store = X509_STORE_new();
1813 if (ret->cert_store == NULL) {
1814 goto err;
1815 }
Adam Langley95c29f32014-06-20 12:00:00 -07001816
Adam Langleyfcf25832014-12-18 17:42:32 -08001817 ssl_create_cipher_list(ret->method, &ret->cipher_list,
1818 &ret->cipher_list_by_id, SSL_DEFAULT_CIPHER_LIST,
1819 ret->cert);
1820 if (ret->cipher_list == NULL ||
1821 sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0) {
1822 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_LIBRARY_HAS_NO_CIPHERS);
1823 goto err2;
1824 }
Adam Langley95c29f32014-06-20 12:00:00 -07001825
Adam Langleyfcf25832014-12-18 17:42:32 -08001826 ret->param = X509_VERIFY_PARAM_new();
1827 if (!ret->param) {
1828 goto err;
1829 }
Adam Langley95c29f32014-06-20 12:00:00 -07001830
Adam Langleyfcf25832014-12-18 17:42:32 -08001831 ret->client_CA = sk_X509_NAME_new_null();
1832 if (ret->client_CA == NULL) {
1833 goto err;
1834 }
Adam Langley95c29f32014-06-20 12:00:00 -07001835
Adam Langleyfcf25832014-12-18 17:42:32 -08001836 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001837
Adam Langleyfcf25832014-12-18 17:42:32 -08001838 ret->extra_certs = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001839
Adam Langleyfcf25832014-12-18 17:42:32 -08001840 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
David Benjamin422d3a42014-08-20 11:09:03 -04001841
Adam Langleyfcf25832014-12-18 17:42:32 -08001842 ret->tlsext_servername_callback = 0;
1843 ret->tlsext_servername_arg = NULL;
1844 /* Setup RFC4507 ticket keys */
1845 if (!RAND_bytes(ret->tlsext_tick_key_name, 16) ||
1846 !RAND_bytes(ret->tlsext_tick_hmac_key, 16) ||
1847 !RAND_bytes(ret->tlsext_tick_aes_key, 16)) {
1848 ret->options |= SSL_OP_NO_TICKET;
1849 }
Adam Langley95c29f32014-06-20 12:00:00 -07001850
Adam Langleyfcf25832014-12-18 17:42:32 -08001851 ret->tlsext_status_cb = 0;
1852 ret->tlsext_status_arg = NULL;
David Benjamin82c9e902014-12-12 15:55:27 -05001853
Adam Langleyfcf25832014-12-18 17:42:32 -08001854 ret->next_protos_advertised_cb = 0;
1855 ret->next_proto_select_cb = 0;
1856 ret->psk_identity_hint = NULL;
1857 ret->psk_client_callback = NULL;
1858 ret->psk_server_callback = NULL;
1859
1860 /* Default is to connect to non-RI servers. When RI is more widely deployed
1861 * might change this. */
1862 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
1863
1864 /* Lock the SSL_CTX to the specified version, for compatibility with legacy
1865 * uses of SSL_METHOD. */
1866 if (meth->version != 0) {
1867 SSL_CTX_set_max_version(ret, meth->version);
1868 SSL_CTX_set_min_version(ret, meth->version);
1869 }
1870
1871 return ret;
1872
Adam Langley95c29f32014-06-20 12:00:00 -07001873err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001874 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -07001875err2:
Adam Langleyfcf25832014-12-18 17:42:32 -08001876 if (ret != NULL) {
1877 SSL_CTX_free(ret);
1878 }
1879 return NULL;
1880}
Adam Langley95c29f32014-06-20 12:00:00 -07001881
Adam Langleyfcf25832014-12-18 17:42:32 -08001882void SSL_CTX_free(SSL_CTX *a) {
1883 int i;
Adam Langley95c29f32014-06-20 12:00:00 -07001884
Adam Langleyfcf25832014-12-18 17:42:32 -08001885 if (a == NULL) {
1886 return;
1887 }
Adam Langley95c29f32014-06-20 12:00:00 -07001888
Adam Langleyfcf25832014-12-18 17:42:32 -08001889 i = CRYPTO_add(&a->references, -1, CRYPTO_LOCK_SSL_CTX);
1890 if (i > 0) {
1891 return;
1892 }
Adam Langley95c29f32014-06-20 12:00:00 -07001893
Adam Langleyfcf25832014-12-18 17:42:32 -08001894 if (a->param) {
1895 X509_VERIFY_PARAM_free(a->param);
1896 }
Adam Langley95c29f32014-06-20 12:00:00 -07001897
Adam Langleyfcf25832014-12-18 17:42:32 -08001898 /* Free internal session cache. However: the remove_cb() may reference the
1899 * ex_data of SSL_CTX, thus the ex_data store can only be removed after the
1900 * sessions were flushed. As the ex_data handling routines might also touch
1901 * the session cache, the most secure solution seems to be: empty (flush) the
1902 * cache, then free ex_data, then finally free the cache. (See ticket
1903 * [openssl.org #212].) */
1904 if (a->sessions != NULL) {
1905 SSL_CTX_flush_sessions(a, 0);
1906 }
Adam Langley95c29f32014-06-20 12:00:00 -07001907
Adam Langleyfcf25832014-12-18 17:42:32 -08001908 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001909
Adam Langleyfcf25832014-12-18 17:42:32 -08001910 if (a->sessions != NULL) {
1911 lh_SSL_SESSION_free(a->sessions);
1912 }
1913 if (a->cert_store != NULL) {
1914 X509_STORE_free(a->cert_store);
1915 }
1916 if (a->cipher_list != NULL) {
1917 ssl_cipher_preference_list_free(a->cipher_list);
1918 }
1919 if (a->cipher_list_by_id != NULL) {
1920 sk_SSL_CIPHER_free(a->cipher_list_by_id);
1921 }
1922 if (a->cipher_list_tls11 != NULL) {
1923 ssl_cipher_preference_list_free(a->cipher_list_tls11);
1924 }
1925 if (a->cert != NULL) {
1926 ssl_cert_free(a->cert);
1927 }
1928 if (a->client_CA != NULL) {
1929 sk_X509_NAME_pop_free(a->client_CA, X509_NAME_free);
1930 }
1931 if (a->extra_certs != NULL) {
1932 sk_X509_pop_free(a->extra_certs, X509_free);
1933 }
1934 if (a->srtp_profiles) {
1935 sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);
1936 }
1937 if (a->psk_identity_hint) {
1938 OPENSSL_free(a->psk_identity_hint);
1939 }
1940 if (a->tlsext_ecpointformatlist) {
1941 OPENSSL_free(a->tlsext_ecpointformatlist);
1942 }
1943 if (a->tlsext_ellipticcurvelist) {
1944 OPENSSL_free(a->tlsext_ellipticcurvelist);
1945 }
1946 if (a->alpn_client_proto_list != NULL) {
1947 OPENSSL_free(a->alpn_client_proto_list);
1948 }
1949 if (a->tlsext_channel_id_private) {
1950 EVP_PKEY_free(a->tlsext_channel_id_private);
1951 }
1952 if (a->keylog_bio) {
1953 BIO_free(a->keylog_bio);
1954 }
Adam Langley95c29f32014-06-20 12:00:00 -07001955
Adam Langleyfcf25832014-12-18 17:42:32 -08001956 OPENSSL_free(a);
1957}
Adam Langley95c29f32014-06-20 12:00:00 -07001958
Adam Langleyfcf25832014-12-18 17:42:32 -08001959void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) {
1960 ctx->default_passwd_callback = cb;
1961}
Adam Langley95c29f32014-06-20 12:00:00 -07001962
Adam Langleyfcf25832014-12-18 17:42:32 -08001963void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u) {
1964 ctx->default_passwd_callback_userdata = u;
1965}
Adam Langley95c29f32014-06-20 12:00:00 -07001966
Adam Langleyfcf25832014-12-18 17:42:32 -08001967void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
1968 int (*cb)(X509_STORE_CTX *, void *),
1969 void *arg) {
1970 ctx->app_verify_callback = cb;
1971 ctx->app_verify_arg = arg;
1972}
Adam Langley95c29f32014-06-20 12:00:00 -07001973
Adam Langleyfcf25832014-12-18 17:42:32 -08001974void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
1975 int (*cb)(int, X509_STORE_CTX *)) {
1976 ctx->verify_mode = mode;
1977 ctx->default_verify_callback = cb;
1978}
Adam Langley95c29f32014-06-20 12:00:00 -07001979
Adam Langleyfcf25832014-12-18 17:42:32 -08001980void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
1981 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
1982}
Adam Langley1258b6a2014-06-20 12:00:00 -07001983
Adam Langleyfcf25832014-12-18 17:42:32 -08001984void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb)(SSL *ssl, void *arg),
1985 void *arg) {
1986 ssl_cert_set_cert_cb(c->cert, cb, arg);
1987}
David Benjamin859ec3c2014-09-02 16:29:36 -04001988
Adam Langleyfcf25832014-12-18 17:42:32 -08001989void SSL_set_cert_cb(SSL *s, int (*cb)(SSL *ssl, void *arg), void *arg) {
1990 ssl_cert_set_cert_cb(s->cert, cb, arg);
1991}
Adam Langley95c29f32014-06-20 12:00:00 -07001992
Adam Langleyfcf25832014-12-18 17:42:32 -08001993static int ssl_has_key(SSL *s, size_t idx) {
1994 CERT_PKEY *cpk = &s->cert->pkeys[idx];
1995 return cpk->x509 && cpk->privatekey;
1996}
David Benjamin033e5f42014-11-13 18:47:41 -05001997
David Benjaminf31e6812014-11-13 18:05:55 -05001998void ssl_get_compatible_server_ciphers(SSL *s, unsigned long *out_mask_k,
Adam Langleyfcf25832014-12-18 17:42:32 -08001999 unsigned long *out_mask_a) {
2000 CERT *c = s->cert;
2001 int rsa_enc, rsa_sign, dh_tmp;
2002 unsigned long mask_k, mask_a;
2003 int have_ecc_cert, ecdsa_ok;
2004 int have_ecdh_tmp;
2005 X509 *x;
Adam Langley95c29f32014-06-20 12:00:00 -07002006
Adam Langleyfcf25832014-12-18 17:42:32 -08002007 if (c == NULL) {
2008 /* TODO(davidben): Is this codepath possible? */
2009 *out_mask_k = 0;
2010 *out_mask_a = 0;
2011 return;
2012 }
Adam Langley95c29f32014-06-20 12:00:00 -07002013
Adam Langleyfcf25832014-12-18 17:42:32 -08002014 dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07002015
David Benjaminc0f763b2015-03-27 02:05:39 -04002016 have_ecdh_tmp = (c->ecdh_nid != NID_undef || c->ecdh_tmp_cb != NULL ||
2017 c->ecdh_tmp_auto);
Adam Langleyfcf25832014-12-18 17:42:32 -08002018 rsa_enc = ssl_has_key(s, SSL_PKEY_RSA_ENC);
2019 rsa_sign = ssl_has_key(s, SSL_PKEY_RSA_SIGN);
2020 have_ecc_cert = ssl_has_key(s, SSL_PKEY_ECC);
2021 mask_k = 0;
2022 mask_a = 0;
David Benjaminf31e6812014-11-13 18:05:55 -05002023
Adam Langleyfcf25832014-12-18 17:42:32 -08002024 if (rsa_enc) {
2025 mask_k |= SSL_kRSA;
2026 }
2027 if (dh_tmp) {
David Benjamin7061e282015-03-19 11:10:48 -04002028 mask_k |= SSL_kDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08002029 }
2030 if (rsa_enc || rsa_sign) {
2031 mask_a |= SSL_aRSA;
2032 }
Adam Langley95c29f32014-06-20 12:00:00 -07002033
Adam Langleyfcf25832014-12-18 17:42:32 -08002034 /* An ECC certificate may be usable for ECDSA cipher suites depending on the
2035 * key usage extension and on the client's curve preferences. */
2036 if (have_ecc_cert) {
2037 x = c->pkeys[SSL_PKEY_ECC].x509;
2038 /* This call populates extension flags (ex_flags). */
2039 X509_check_purpose(x, -1, 0);
2040 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE)
2041 ? (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)
2042 : 1;
2043 if (!tls1_check_ec_cert(s, x)) {
2044 ecdsa_ok = 0;
2045 }
2046 if (ecdsa_ok) {
2047 mask_a |= SSL_aECDSA;
2048 }
2049 }
Adam Langley95c29f32014-06-20 12:00:00 -07002050
Adam Langleyfcf25832014-12-18 17:42:32 -08002051 /* If we are considering an ECC cipher suite that uses an ephemeral EC
2052 * key, check it. */
2053 if (have_ecdh_tmp && tls1_check_ec_tmp_key(s)) {
David Benjamin7061e282015-03-19 11:10:48 -04002054 mask_k |= SSL_kECDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08002055 }
Adam Langley95c29f32014-06-20 12:00:00 -07002056
Adam Langleyfcf25832014-12-18 17:42:32 -08002057 /* PSK requires a server callback. */
2058 if (s->psk_server_callback != NULL) {
2059 mask_k |= SSL_kPSK;
2060 mask_a |= SSL_aPSK;
2061 }
Adam Langley95c29f32014-06-20 12:00:00 -07002062
Adam Langleyfcf25832014-12-18 17:42:32 -08002063 *out_mask_k = mask_k;
2064 *out_mask_a = mask_a;
2065}
Adam Langley95c29f32014-06-20 12:00:00 -07002066
2067/* This handy macro borrowed from crypto/x509v3/v3_purp.c */
2068#define ku_reject(x, usage) \
Adam Langleyfcf25832014-12-18 17:42:32 -08002069 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
Adam Langley95c29f32014-06-20 12:00:00 -07002070
Adam Langleyfcf25832014-12-18 17:42:32 -08002071int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s) {
2072 unsigned long alg_a;
2073 int signature_nid = 0, md_nid = 0, pk_nid = 0;
2074 const SSL_CIPHER *cs = s->s3->tmp.new_cipher;
Adam Langley95c29f32014-06-20 12:00:00 -07002075
Adam Langleyfcf25832014-12-18 17:42:32 -08002076 alg_a = cs->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -07002077
Adam Langleyfcf25832014-12-18 17:42:32 -08002078 /* This call populates the ex_flags field correctly */
2079 X509_check_purpose(x, -1, 0);
2080 if (x->sig_alg && x->sig_alg->algorithm) {
2081 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
2082 OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
2083 }
2084 if (alg_a & SSL_aECDSA) {
2085 /* key usage, if present, must allow signing */
2086 if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE)) {
2087 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg,
2088 SSL_R_ECC_CERT_NOT_FOR_SIGNING);
2089 return 0;
2090 }
2091 }
Adam Langley95c29f32014-06-20 12:00:00 -07002092
Adam Langleyfcf25832014-12-18 17:42:32 -08002093 return 1; /* all checks are ok */
2094}
Adam Langley95c29f32014-06-20 12:00:00 -07002095
Adam Langleyfcf25832014-12-18 17:42:32 -08002096static int ssl_get_server_cert_index(const SSL *s) {
2097 int idx;
2098 idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
2099 if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509) {
2100 idx = SSL_PKEY_RSA_SIGN;
2101 }
2102 if (idx == -1) {
2103 OPENSSL_PUT_ERROR(SSL, ssl_get_server_cert_index, ERR_R_INTERNAL_ERROR);
2104 }
2105 return idx;
2106}
Adam Langley95c29f32014-06-20 12:00:00 -07002107
Adam Langleyfcf25832014-12-18 17:42:32 -08002108CERT_PKEY *ssl_get_server_send_pkey(const SSL *s) {
2109 int i = ssl_get_server_cert_index(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002110
Adam Langleyfcf25832014-12-18 17:42:32 -08002111 /* This may or may not be an error. */
2112 if (i < 0) {
2113 return NULL;
2114 }
Adam Langley95c29f32014-06-20 12:00:00 -07002115
Adam Langleyfcf25832014-12-18 17:42:32 -08002116 /* May be NULL. */
2117 return &s->cert->pkeys[i];
2118}
Adam Langley95c29f32014-06-20 12:00:00 -07002119
Adam Langleyfcf25832014-12-18 17:42:32 -08002120EVP_PKEY *ssl_get_sign_pkey(SSL *s, const SSL_CIPHER *cipher) {
2121 unsigned long alg_a;
2122 CERT *c;
2123 int idx = -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002124
Adam Langleyfcf25832014-12-18 17:42:32 -08002125 alg_a = cipher->algorithm_auth;
2126 c = s->cert;
Adam Langley95c29f32014-06-20 12:00:00 -07002127
Adam Langleyfcf25832014-12-18 17:42:32 -08002128 if (alg_a & SSL_aRSA) {
2129 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL) {
2130 idx = SSL_PKEY_RSA_SIGN;
2131 } else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL) {
2132 idx = SSL_PKEY_RSA_ENC;
2133 }
2134 } else if ((alg_a & SSL_aECDSA) &&
2135 (c->pkeys[SSL_PKEY_ECC].privatekey != NULL)) {
2136 idx = SSL_PKEY_ECC;
2137 }
Adam Langley95c29f32014-06-20 12:00:00 -07002138
Adam Langleyfcf25832014-12-18 17:42:32 -08002139 if (idx == -1) {
2140 OPENSSL_PUT_ERROR(SSL, ssl_get_sign_pkey, ERR_R_INTERNAL_ERROR);
2141 return NULL;
2142 }
Adam Langley95c29f32014-06-20 12:00:00 -07002143
Adam Langleyfcf25832014-12-18 17:42:32 -08002144 return c->pkeys[idx].privatekey;
2145}
Adam Langley95c29f32014-06-20 12:00:00 -07002146
Adam Langleyfcf25832014-12-18 17:42:32 -08002147void ssl_update_cache(SSL *s, int mode) {
David Benjaminb6d0c6d2015-03-19 19:07:26 -04002148 /* Never cache sessions with empty session IDs. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002149 if (s->session->session_id_length == 0) {
2150 return;
2151 }
Adam Langley95c29f32014-06-20 12:00:00 -07002152
David Benjaminb6d0c6d2015-03-19 19:07:26 -04002153 SSL_CTX *ctx = s->initial_ctx;
2154 if ((ctx->session_cache_mode & mode) == mode && !s->hit &&
2155 ((ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) ||
2156 SSL_CTX_add_session(ctx, s->session)) &&
2157 ctx->new_session_cb != NULL) {
2158 /* Note: |new_session_cb| is called whether the internal session cache is
2159 * used or not. */
2160 if (!ctx->new_session_cb(s, SSL_SESSION_up_ref(s->session))) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002161 SSL_SESSION_free(s->session);
2162 }
2163 }
Adam Langley95c29f32014-06-20 12:00:00 -07002164
David Benjaminb6d0c6d2015-03-19 19:07:26 -04002165 if (!(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR) &&
2166 !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) &&
2167 (ctx->session_cache_mode & mode) == mode) {
2168 /* Automatically flush the internal session cache every 255 connections. */
2169 int flush_cache = 0;
2170 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2171 ctx->handshakes_since_cache_flush++;
2172 if (ctx->handshakes_since_cache_flush >= 255) {
2173 flush_cache = 1;
2174 ctx->handshakes_since_cache_flush = 0;
2175 }
2176 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
2177
2178 if (flush_cache) {
2179 SSL_CTX_flush_sessions(ctx, (unsigned long)time(NULL));
Adam Langleyfcf25832014-12-18 17:42:32 -08002180 }
2181 }
2182}
Adam Langley95c29f32014-06-20 12:00:00 -07002183
David Benjamin1a5c50f2015-03-11 16:22:37 -04002184int SSL_get_error(const SSL *s, int ret_code) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002185 int reason;
David Benjamin1a5c50f2015-03-11 16:22:37 -04002186 uint32_t err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002187 BIO *bio;
Adam Langley95c29f32014-06-20 12:00:00 -07002188
David Benjamin1a5c50f2015-03-11 16:22:37 -04002189 if (ret_code > 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002190 return SSL_ERROR_NONE;
2191 }
Adam Langley95c29f32014-06-20 12:00:00 -07002192
Adam Langleyfcf25832014-12-18 17:42:32 -08002193 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
2194 * where we do encode the error */
David Benjamin1a5c50f2015-03-11 16:22:37 -04002195 err = ERR_peek_error();
2196 if (err != 0) {
2197 if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002198 return SSL_ERROR_SYSCALL;
2199 }
2200 return SSL_ERROR_SSL;
2201 }
Adam Langley95c29f32014-06-20 12:00:00 -07002202
David Benjamin1a5c50f2015-03-11 16:22:37 -04002203 if (ret_code == 0) {
David Benjamin9a38e922015-01-22 16:06:11 -05002204 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2205 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) {
2206 /* The socket was cleanly shut down with a close_notify. */
2207 return SSL_ERROR_ZERO_RETURN;
2208 }
2209 /* An EOF was observed which violates the protocol, and the underlying
2210 * transport does not participate in the error queue. Bubble up to the
2211 * caller. */
2212 return SSL_ERROR_SYSCALL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002213 }
Adam Langleyb2ce0582014-06-20 12:00:00 -07002214
Adam Langleyfcf25832014-12-18 17:42:32 -08002215 if (SSL_want_session(s)) {
2216 return SSL_ERROR_PENDING_SESSION;
2217 }
Adam Langley95c29f32014-06-20 12:00:00 -07002218
Adam Langleyfcf25832014-12-18 17:42:32 -08002219 if (SSL_want_certificate(s)) {
2220 return SSL_ERROR_PENDING_CERTIFICATE;
2221 }
Adam Langley95c29f32014-06-20 12:00:00 -07002222
Adam Langleyfcf25832014-12-18 17:42:32 -08002223 if (SSL_want_read(s)) {
2224 bio = SSL_get_rbio(s);
2225 if (BIO_should_read(bio)) {
2226 return SSL_ERROR_WANT_READ;
2227 }
Adam Langley95c29f32014-06-20 12:00:00 -07002228
Adam Langleyfcf25832014-12-18 17:42:32 -08002229 if (BIO_should_write(bio)) {
2230 /* This one doesn't make too much sense ... We never try to write to the
2231 * rbio, and an application program where rbio and wbio are separate
2232 * couldn't even know what it should wait for. However if we ever set
2233 * s->rwstate incorrectly (so that we have SSL_want_read(s) instead of
2234 * SSL_want_write(s)) and rbio and wbio *are* the same, this test works
2235 * around that bug; so it might be safer to keep it. */
2236 return SSL_ERROR_WANT_WRITE;
2237 }
Adam Langley95c29f32014-06-20 12:00:00 -07002238
Adam Langleyfcf25832014-12-18 17:42:32 -08002239 if (BIO_should_io_special(bio)) {
2240 reason = BIO_get_retry_reason(bio);
2241 if (reason == BIO_RR_CONNECT) {
2242 return SSL_ERROR_WANT_CONNECT;
2243 }
Adam Langley95c29f32014-06-20 12:00:00 -07002244
Adam Langleyfcf25832014-12-18 17:42:32 -08002245 if (reason == BIO_RR_ACCEPT) {
2246 return SSL_ERROR_WANT_ACCEPT;
2247 }
Adam Langley95c29f32014-06-20 12:00:00 -07002248
Adam Langleyfcf25832014-12-18 17:42:32 -08002249 return SSL_ERROR_SYSCALL; /* unknown */
2250 }
2251 }
Adam Langley95c29f32014-06-20 12:00:00 -07002252
Adam Langleyfcf25832014-12-18 17:42:32 -08002253 if (SSL_want_write(s)) {
2254 bio = SSL_get_wbio(s);
2255 if (BIO_should_write(bio)) {
2256 return SSL_ERROR_WANT_WRITE;
2257 }
Adam Langley95c29f32014-06-20 12:00:00 -07002258
Adam Langleyfcf25832014-12-18 17:42:32 -08002259 if (BIO_should_read(bio)) {
2260 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2261 return SSL_ERROR_WANT_READ;
2262 }
Adam Langley95c29f32014-06-20 12:00:00 -07002263
Adam Langleyfcf25832014-12-18 17:42:32 -08002264 if (BIO_should_io_special(bio)) {
2265 reason = BIO_get_retry_reason(bio);
2266 if (reason == BIO_RR_CONNECT) {
2267 return SSL_ERROR_WANT_CONNECT;
2268 }
Adam Langley95c29f32014-06-20 12:00:00 -07002269
Adam Langleyfcf25832014-12-18 17:42:32 -08002270 if (reason == BIO_RR_ACCEPT) {
2271 return SSL_ERROR_WANT_ACCEPT;
2272 }
Adam Langley95c29f32014-06-20 12:00:00 -07002273
Adam Langleyfcf25832014-12-18 17:42:32 -08002274 return SSL_ERROR_SYSCALL;
2275 }
2276 }
Adam Langley95c29f32014-06-20 12:00:00 -07002277
Adam Langleyfcf25832014-12-18 17:42:32 -08002278 if (SSL_want_x509_lookup(s)) {
2279 return SSL_ERROR_WANT_X509_LOOKUP;
2280 }
Adam Langley95c29f32014-06-20 12:00:00 -07002281
Adam Langleyfcf25832014-12-18 17:42:32 -08002282 if (SSL_want_channel_id_lookup(s)) {
2283 return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
2284 }
Adam Langley0f4746e2014-08-13 12:26:32 -07002285
Adam Langleyfcf25832014-12-18 17:42:32 -08002286 return SSL_ERROR_SYSCALL;
2287}
Adam Langley0f4746e2014-08-13 12:26:32 -07002288
Adam Langleyfcf25832014-12-18 17:42:32 -08002289int SSL_do_handshake(SSL *s) {
2290 int ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002291
Adam Langleyfcf25832014-12-18 17:42:32 -08002292 if (s->handshake_func == NULL) {
2293 OPENSSL_PUT_ERROR(SSL, SSL_do_handshake, SSL_R_CONNECTION_TYPE_NOT_SET);
2294 return -1;
2295 }
Adam Langley95c29f32014-06-20 12:00:00 -07002296
Adam Langleyfcf25832014-12-18 17:42:32 -08002297 s->method->ssl_renegotiate_check(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002298
Adam Langleyfcf25832014-12-18 17:42:32 -08002299 if (SSL_in_init(s)) {
2300 ret = s->handshake_func(s);
2301 }
2302 return ret;
2303}
Adam Langley95c29f32014-06-20 12:00:00 -07002304
Adam Langleyfcf25832014-12-18 17:42:32 -08002305void SSL_set_accept_state(SSL *s) {
2306 s->server = 1;
2307 s->shutdown = 0;
2308 s->state = SSL_ST_ACCEPT | SSL_ST_BEFORE;
2309 s->handshake_func = s->method->ssl_accept;
2310 /* clear the current cipher */
2311 ssl_clear_cipher_ctx(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002312}
Adam Langley95c29f32014-06-20 12:00:00 -07002313
Adam Langleyfcf25832014-12-18 17:42:32 -08002314void SSL_set_connect_state(SSL *s) {
2315 s->server = 0;
2316 s->shutdown = 0;
2317 s->state = SSL_ST_CONNECT | SSL_ST_BEFORE;
2318 s->handshake_func = s->method->ssl_connect;
2319 /* clear the current cipher */
2320 ssl_clear_cipher_ctx(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002321}
Adam Langley95c29f32014-06-20 12:00:00 -07002322
Adam Langleyfcf25832014-12-18 17:42:32 -08002323int ssl_undefined_function(SSL *s) {
2324 OPENSSL_PUT_ERROR(SSL, ssl_undefined_function,
2325 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2326 return 0;
2327}
Adam Langley95c29f32014-06-20 12:00:00 -07002328
Adam Langleyfcf25832014-12-18 17:42:32 -08002329int ssl_undefined_void_function(void) {
2330 OPENSSL_PUT_ERROR(SSL, ssl_undefined_void_function,
2331 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2332 return 0;
2333}
Adam Langley95c29f32014-06-20 12:00:00 -07002334
Adam Langleyfcf25832014-12-18 17:42:32 -08002335int ssl_undefined_const_function(const SSL *s) {
2336 OPENSSL_PUT_ERROR(SSL, ssl_undefined_const_function,
2337 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2338 return 0;
2339}
Adam Langley95c29f32014-06-20 12:00:00 -07002340
Adam Langleyfcf25832014-12-18 17:42:32 -08002341static const char *ssl_get_version(int version) {
2342 switch (version) {
2343 case TLS1_2_VERSION:
2344 return "TLSv1.2";
Adam Langley95c29f32014-06-20 12:00:00 -07002345
Adam Langleyfcf25832014-12-18 17:42:32 -08002346 case TLS1_1_VERSION:
2347 return "TLSv1.1";
Adam Langley95c29f32014-06-20 12:00:00 -07002348
Adam Langleyfcf25832014-12-18 17:42:32 -08002349 case TLS1_VERSION:
2350 return "TLSv1";
Adam Langley95c29f32014-06-20 12:00:00 -07002351
Adam Langleyfcf25832014-12-18 17:42:32 -08002352 case SSL3_VERSION:
2353 return "SSLv3";
Adam Langley95c29f32014-06-20 12:00:00 -07002354
Adam Langleyfcf25832014-12-18 17:42:32 -08002355 default:
2356 return "unknown";
2357 }
2358}
Adam Langley95c29f32014-06-20 12:00:00 -07002359
Adam Langleyfcf25832014-12-18 17:42:32 -08002360const char *SSL_get_version(const SSL *s) {
2361 return ssl_get_version(s->version);
2362}
Adam Langley95c29f32014-06-20 12:00:00 -07002363
Adam Langleyfcf25832014-12-18 17:42:32 -08002364const char *SSL_SESSION_get_version(const SSL_SESSION *sess) {
2365 return ssl_get_version(sess->ssl_version);
2366}
Adam Langley95c29f32014-06-20 12:00:00 -07002367
Adam Langleyfcf25832014-12-18 17:42:32 -08002368void ssl_clear_cipher_ctx(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002369 if (s->aead_read_ctx != NULL) {
2370 EVP_AEAD_CTX_cleanup(&s->aead_read_ctx->ctx);
2371 OPENSSL_free(s->aead_read_ctx);
2372 s->aead_read_ctx = NULL;
2373 }
Adam Langleya5dc5452014-06-20 12:00:00 -07002374
Adam Langleyfcf25832014-12-18 17:42:32 -08002375 if (s->aead_write_ctx != NULL) {
2376 EVP_AEAD_CTX_cleanup(&s->aead_write_ctx->ctx);
2377 OPENSSL_free(s->aead_write_ctx);
2378 s->aead_write_ctx = NULL;
2379 }
2380}
Adam Langley95c29f32014-06-20 12:00:00 -07002381
Adam Langleyfcf25832014-12-18 17:42:32 -08002382X509 *SSL_get_certificate(const SSL *s) {
2383 if (s->cert != NULL) {
2384 return s->cert->key->x509;
2385 }
2386
2387 return NULL;
2388}
2389
2390EVP_PKEY *SSL_get_privatekey(const SSL *s) {
2391 if (s->cert != NULL) {
2392 return s->cert->key->privatekey;
2393 }
2394
2395 return NULL;
2396}
2397
2398X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
2399 if (ctx->cert != NULL) {
2400 return ctx->cert->key->x509;
2401 }
2402
2403 return NULL;
2404}
2405
2406EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
2407 if (ctx->cert != NULL) {
2408 return ctx->cert->key->privatekey;
2409 }
2410
2411 return NULL;
2412}
2413
2414const SSL_CIPHER *SSL_get_current_cipher(const SSL *s) {
2415 if (s->session != NULL && s->session->cipher != NULL) {
2416 return s->session->cipher;
2417 }
2418
2419 return NULL;
2420}
2421
2422const void *SSL_get_current_compression(SSL *s) { return NULL; }
2423
2424const void *SSL_get_current_expansion(SSL *s) { return NULL; }
2425
2426int ssl_init_wbio_buffer(SSL *s, int push) {
2427 BIO *bbio;
2428
2429 if (s->bbio == NULL) {
2430 bbio = BIO_new(BIO_f_buffer());
2431 if (bbio == NULL) {
2432 return 0;
2433 }
2434 s->bbio = bbio;
2435 } else {
2436 bbio = s->bbio;
2437 if (s->bbio == s->wbio) {
2438 s->wbio = BIO_pop(s->wbio);
2439 }
2440 }
2441
2442 BIO_reset(bbio);
2443 if (!BIO_set_read_buffer_size(bbio, 1)) {
2444 OPENSSL_PUT_ERROR(SSL, ssl_init_wbio_buffer, ERR_R_BUF_LIB);
2445 return 0;
2446 }
2447
2448 if (push) {
2449 if (s->wbio != bbio) {
2450 s->wbio = BIO_push(bbio, s->wbio);
2451 }
2452 } else {
2453 if (s->wbio == bbio) {
2454 s->wbio = BIO_pop(bbio);
2455 }
2456 }
2457
2458 return 1;
2459}
2460
2461void ssl_free_wbio_buffer(SSL *s) {
2462 if (s->bbio == NULL) {
2463 return;
2464 }
2465
2466 if (s->bbio == s->wbio) {
2467 /* remove buffering */
2468 s->wbio = BIO_pop(s->wbio);
2469 }
2470
2471 BIO_free(s->bbio);
2472 s->bbio = NULL;
2473}
2474
2475void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
2476 ctx->quiet_shutdown = mode;
2477}
2478
2479int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
2480 return ctx->quiet_shutdown;
2481}
2482
2483void SSL_set_quiet_shutdown(SSL *s, int mode) { s->quiet_shutdown = mode; }
2484
2485int SSL_get_quiet_shutdown(const SSL *s) { return s->quiet_shutdown; }
2486
2487void SSL_set_shutdown(SSL *s, int mode) { s->shutdown = mode; }
2488
2489int SSL_get_shutdown(const SSL *s) { return s->shutdown; }
2490
2491int SSL_version(const SSL *s) { return s->version; }
2492
2493SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
2494
2495SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
2496 if (ssl->ctx == ctx) {
2497 return ssl->ctx;
2498 }
2499
2500 if (ctx == NULL) {
2501 ctx = ssl->initial_ctx;
2502 }
2503
2504 if (ssl->cert != NULL) {
2505 ssl_cert_free(ssl->cert);
2506 }
2507
2508 ssl->cert = ssl_cert_dup(ctx->cert);
2509 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
2510 if (ssl->ctx != NULL) {
2511 SSL_CTX_free(ssl->ctx); /* decrement reference count */
2512 }
2513 ssl->ctx = ctx;
2514
2515 ssl->sid_ctx_length = ctx->sid_ctx_length;
2516 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2517 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2518
2519 return ssl->ctx;
2520}
2521
2522int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
2523 return X509_STORE_set_default_paths(ctx->cert_store);
2524}
Adam Langley95c29f32014-06-20 12:00:00 -07002525
2526int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
Adam Langleyfcf25832014-12-18 17:42:32 -08002527 const char *CApath) {
2528 return X509_STORE_load_locations(ctx->cert_store, CAfile, CApath);
2529}
Adam Langley95c29f32014-06-20 12:00:00 -07002530
2531void SSL_set_info_callback(SSL *ssl,
Adam Langleyfcf25832014-12-18 17:42:32 -08002532 void (*cb)(const SSL *ssl, int type, int val)) {
2533 ssl->info_callback = cb;
2534}
Adam Langley95c29f32014-06-20 12:00:00 -07002535
Adam Langleyfcf25832014-12-18 17:42:32 -08002536void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/, int /*type*/,
2537 int /*val*/) {
2538 return ssl->info_callback;
2539}
Adam Langley95c29f32014-06-20 12:00:00 -07002540
Adam Langleyfcf25832014-12-18 17:42:32 -08002541int SSL_state(const SSL *ssl) { return ssl->state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002542
Adam Langleyfcf25832014-12-18 17:42:32 -08002543void SSL_set_state(SSL *ssl, int state) { ssl->state = state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002544
Adam Langleyfcf25832014-12-18 17:42:32 -08002545void SSL_set_verify_result(SSL *ssl, long arg) { ssl->verify_result = arg; }
Adam Langley95c29f32014-06-20 12:00:00 -07002546
Adam Langleyfcf25832014-12-18 17:42:32 -08002547long SSL_get_verify_result(const SSL *ssl) { return ssl->verify_result; }
Adam Langley95c29f32014-06-20 12:00:00 -07002548
Adam Langleyfcf25832014-12-18 17:42:32 -08002549int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2550 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
2551 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp, new_func,
2552 dup_func, free_func);
2553}
Adam Langley95c29f32014-06-20 12:00:00 -07002554
Adam Langleyfcf25832014-12-18 17:42:32 -08002555int SSL_set_ex_data(SSL *s, int idx, void *arg) {
2556 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2557}
Adam Langley95c29f32014-06-20 12:00:00 -07002558
Adam Langleyfcf25832014-12-18 17:42:32 -08002559void *SSL_get_ex_data(const SSL *s, int idx) {
2560 return CRYPTO_get_ex_data(&s->ex_data, idx);
2561}
Adam Langley95c29f32014-06-20 12:00:00 -07002562
Adam Langleyfcf25832014-12-18 17:42:32 -08002563int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2564 CRYPTO_EX_dup *dup_func,
2565 CRYPTO_EX_free *free_func) {
2566 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp, new_func,
2567 dup_func, free_func);
2568}
Adam Langley95c29f32014-06-20 12:00:00 -07002569
Adam Langleyfcf25832014-12-18 17:42:32 -08002570int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg) {
2571 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2572}
Adam Langley95c29f32014-06-20 12:00:00 -07002573
Adam Langleyfcf25832014-12-18 17:42:32 -08002574void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx) {
2575 return CRYPTO_get_ex_data(&s->ex_data, idx);
2576}
Adam Langley95c29f32014-06-20 12:00:00 -07002577
Adam Langleyfcf25832014-12-18 17:42:32 -08002578int ssl_ok(SSL *s) { return 1; }
Adam Langley95c29f32014-06-20 12:00:00 -07002579
Adam Langleyfcf25832014-12-18 17:42:32 -08002580X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
2581 return ctx->cert_store;
2582}
Adam Langley95c29f32014-06-20 12:00:00 -07002583
Adam Langleyfcf25832014-12-18 17:42:32 -08002584void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
2585 if (ctx->cert_store != NULL) {
2586 X509_STORE_free(ctx->cert_store);
2587 }
2588 ctx->cert_store = store;
2589}
Adam Langley95c29f32014-06-20 12:00:00 -07002590
Adam Langleyfcf25832014-12-18 17:42:32 -08002591int SSL_want(const SSL *s) { return s->rwstate; }
Adam Langley95c29f32014-06-20 12:00:00 -07002592
Adam Langleyfcf25832014-12-18 17:42:32 -08002593void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
2594 RSA *(*cb)(SSL *ssl, int is_export,
2595 int keylength)) {
2596 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_RSA_CB, (void (*)(void))cb);
2597}
Adam Langley95c29f32014-06-20 12:00:00 -07002598
Adam Langleyfcf25832014-12-18 17:42:32 -08002599void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
2600 int keylength)) {
2601 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_RSA_CB, (void (*)(void))cb);
2602}
2603
2604void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
2605 DH *(*dh)(SSL *ssl, int is_export,
2606 int keylength)) {
2607 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh);
2608}
2609
2610void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*dh)(SSL *ssl, int is_export,
2611 int keylength)) {
2612 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh);
2613}
2614
2615void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,
2616 EC_KEY *(*ecdh)(SSL *ssl, int is_export,
2617 int keylength)) {
2618 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH_CB, (void (*)(void))ecdh);
2619}
2620
2621void SSL_set_tmp_ecdh_callback(SSL *ssl,
2622 EC_KEY *(*ecdh)(SSL *ssl, int is_export,
2623 int keylength)) {
2624 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_ECDH_CB, (void (*)(void))ecdh);
2625}
2626
2627int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
2628 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2629 OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_psk_identity_hint,
2630 SSL_R_DATA_LENGTH_TOO_LONG);
2631 return 0;
2632 }
2633
2634 if (ctx->psk_identity_hint != NULL) {
2635 OPENSSL_free(ctx->psk_identity_hint);
2636 }
2637
2638 if (identity_hint != NULL) {
2639 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2640 if (ctx->psk_identity_hint == NULL) {
2641 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002642 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002643 } else {
2644 ctx->psk_identity_hint = NULL;
2645 }
Adam Langley95c29f32014-06-20 12:00:00 -07002646
Adam Langleyfcf25832014-12-18 17:42:32 -08002647 return 1;
2648}
2649
2650int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint) {
2651 if (s == NULL) {
2652 return 0;
2653 }
2654
2655 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2656 OPENSSL_PUT_ERROR(SSL, SSL_use_psk_identity_hint,
2657 SSL_R_DATA_LENGTH_TOO_LONG);
2658 return 0;
2659 }
2660
2661 /* Clear currently configured hint, if any. */
2662 if (s->psk_identity_hint != NULL) {
2663 OPENSSL_free(s->psk_identity_hint);
2664 s->psk_identity_hint = NULL;
2665 }
2666
2667 if (identity_hint != NULL) {
2668 s->psk_identity_hint = BUF_strdup(identity_hint);
2669 if (s->psk_identity_hint == NULL) {
2670 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002671 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002672 }
Adam Langley95c29f32014-06-20 12:00:00 -07002673
Adam Langleyfcf25832014-12-18 17:42:32 -08002674 return 1;
2675}
Adam Langley95c29f32014-06-20 12:00:00 -07002676
Adam Langleyfcf25832014-12-18 17:42:32 -08002677const char *SSL_get_psk_identity_hint(const SSL *s) {
2678 if (s == NULL) {
2679 return NULL;
2680 }
2681 return s->psk_identity_hint;
2682}
Adam Langley95c29f32014-06-20 12:00:00 -07002683
Adam Langleyfcf25832014-12-18 17:42:32 -08002684const char *SSL_get_psk_identity(const SSL *s) {
2685 if (s == NULL || s->session == NULL) {
2686 return NULL;
2687 }
Adam Langley95c29f32014-06-20 12:00:00 -07002688
Adam Langleyfcf25832014-12-18 17:42:32 -08002689 return s->session->psk_identity;
2690}
Adam Langley95c29f32014-06-20 12:00:00 -07002691
Adam Langleyfcf25832014-12-18 17:42:32 -08002692void SSL_set_psk_client_callback(
2693 SSL *s, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2694 unsigned int max_identity_len, uint8_t *psk,
2695 unsigned int max_psk_len)) {
2696 s->psk_client_callback = cb;
2697}
Adam Langley95c29f32014-06-20 12:00:00 -07002698
Adam Langleyfcf25832014-12-18 17:42:32 -08002699void SSL_CTX_set_psk_client_callback(
2700 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2701 unsigned int max_identity_len,
2702 uint8_t *psk, unsigned int max_psk_len)) {
2703 ctx->psk_client_callback = cb;
2704}
Adam Langley95c29f32014-06-20 12:00:00 -07002705
Adam Langleyfcf25832014-12-18 17:42:32 -08002706void SSL_set_psk_server_callback(
2707 SSL *s, unsigned int (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
2708 unsigned int max_psk_len)) {
2709 s->psk_server_callback = cb;
2710}
Adam Langley95c29f32014-06-20 12:00:00 -07002711
Adam Langleyfcf25832014-12-18 17:42:32 -08002712void SSL_CTX_set_psk_server_callback(
2713 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *identity,
2714 uint8_t *psk, unsigned int max_psk_len)) {
2715 ctx->psk_server_callback = cb;
2716}
Adam Langley95c29f32014-06-20 12:00:00 -07002717
Adam Langleyfcf25832014-12-18 17:42:32 -08002718void SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) {
2719 ctx->min_version = version;
2720}
Adam Langley95c29f32014-06-20 12:00:00 -07002721
Adam Langleyfcf25832014-12-18 17:42:32 -08002722void SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) {
2723 ctx->max_version = version;
2724}
Adam Langley0289c732014-06-20 12:00:00 -07002725
Adam Langleyfcf25832014-12-18 17:42:32 -08002726void SSL_set_min_version(SSL *ssl, uint16_t version) {
2727 ssl->min_version = version;
2728}
Adam Langley0289c732014-06-20 12:00:00 -07002729
Adam Langleyfcf25832014-12-18 17:42:32 -08002730void SSL_set_max_version(SSL *ssl, uint16_t version) {
2731 ssl->max_version = version;
2732}
Adam Langley95c29f32014-06-20 12:00:00 -07002733
Adam Langleyfcf25832014-12-18 17:42:32 -08002734void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
2735 void (*cb)(int write_p, int version,
2736 int content_type, const void *buf,
2737 size_t len, SSL *ssl, void *arg)) {
2738 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2739}
2740void SSL_set_msg_callback(SSL *ssl,
2741 void (*cb)(int write_p, int version, int content_type,
2742 const void *buf, size_t len, SSL *ssl,
2743 void *arg)) {
2744 SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2745}
Adam Langley95c29f32014-06-20 12:00:00 -07002746
Adam Langleyfcf25832014-12-18 17:42:32 -08002747void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio) {
2748 if (ctx->keylog_bio != NULL) {
2749 BIO_free(ctx->keylog_bio);
2750 }
2751 ctx->keylog_bio = keylog_bio;
2752}
Adam Langley95c29f32014-06-20 12:00:00 -07002753
Adam Langleyfcf25832014-12-18 17:42:32 -08002754static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
2755 static const char hextable[] = "0123456789abcdef";
2756 uint8_t *out;
2757 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002758
Adam Langleyfcf25832014-12-18 17:42:32 -08002759 if (!CBB_add_space(cbb, &out, in_len * 2)) {
2760 return 0;
2761 }
Adam Langley95c29f32014-06-20 12:00:00 -07002762
Adam Langleyfcf25832014-12-18 17:42:32 -08002763 for (i = 0; i < in_len; i++) {
2764 *(out++) = (uint8_t)hextable[in[i] >> 4];
2765 *(out++) = (uint8_t)hextable[in[i] & 0xf];
2766 }
Adam Langley95c29f32014-06-20 12:00:00 -07002767
Adam Langleyfcf25832014-12-18 17:42:32 -08002768 return 1;
2769}
David Benjamin859ec3c2014-09-02 16:29:36 -04002770
2771int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -08002772 const uint8_t *encrypted_premaster,
2773 size_t encrypted_premaster_len,
2774 const uint8_t *premaster,
2775 size_t premaster_len) {
2776 BIO *bio = ctx->keylog_bio;
2777 CBB cbb;
2778 uint8_t *out;
2779 size_t out_len;
2780 int ret;
David Benjamin859ec3c2014-09-02 16:29:36 -04002781
Adam Langleyfcf25832014-12-18 17:42:32 -08002782 if (bio == NULL) {
2783 return 1;
2784 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002785
Adam Langleyfcf25832014-12-18 17:42:32 -08002786 if (encrypted_premaster_len < 8) {
2787 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_rsa_client_key_exchange,
2788 ERR_R_INTERNAL_ERROR);
2789 return 0;
2790 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002791
Adam Langleyfcf25832014-12-18 17:42:32 -08002792 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len * 2 + 1)) {
2793 return 0;
2794 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002795
Adam Langleyfcf25832014-12-18 17:42:32 -08002796 if (!CBB_add_bytes(&cbb, (const uint8_t *)"RSA ", 4) ||
2797 /* Only the first 8 bytes of the encrypted premaster secret are
2798 * logged. */
2799 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
2800 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2801 !cbb_add_hex(&cbb, premaster, premaster_len) ||
2802 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2803 !CBB_finish(&cbb, &out, &out_len)) {
2804 CBB_cleanup(&cbb);
2805 return 0;
2806 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002807
Adam Langleyfcf25832014-12-18 17:42:32 -08002808 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2809 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2810 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
David Benjamin859ec3c2014-09-02 16:29:36 -04002811
Adam Langleyfcf25832014-12-18 17:42:32 -08002812 OPENSSL_free(out);
2813 return ret;
Adam Langley95f22882014-06-20 12:00:00 -07002814}
2815
Adam Langleyfcf25832014-12-18 17:42:32 -08002816int ssl_ctx_log_master_secret(SSL_CTX *ctx, const uint8_t *client_random,
2817 size_t client_random_len, const uint8_t *master,
2818 size_t master_len) {
2819 BIO *bio = ctx->keylog_bio;
2820 CBB cbb;
2821 uint8_t *out;
2822 size_t out_len;
2823 int ret;
Adam Langleyadb739e2014-06-20 12:00:00 -07002824
Adam Langleyfcf25832014-12-18 17:42:32 -08002825 if (bio == NULL) {
2826 return 1;
2827 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002828
Adam Langleyfcf25832014-12-18 17:42:32 -08002829 if (client_random_len != 32) {
2830 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_master_secret, ERR_R_INTERNAL_ERROR);
2831 return 0;
2832 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002833
Adam Langleyfcf25832014-12-18 17:42:32 -08002834 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len * 2 + 1)) {
2835 return 0;
2836 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002837
Adam Langleyfcf25832014-12-18 17:42:32 -08002838 if (!CBB_add_bytes(&cbb, (const uint8_t *)"CLIENT_RANDOM ", 14) ||
2839 !cbb_add_hex(&cbb, client_random, 32) ||
2840 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2841 !cbb_add_hex(&cbb, master, master_len) ||
2842 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2843 !CBB_finish(&cbb, &out, &out_len)) {
2844 CBB_cleanup(&cbb);
2845 return 0;
2846 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002847
Adam Langleyfcf25832014-12-18 17:42:32 -08002848 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2849 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2850 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
David Benjamine99e9122014-12-11 01:46:01 -05002851
Adam Langleyfcf25832014-12-18 17:42:32 -08002852 OPENSSL_free(out);
2853 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -07002854}
2855
David Benjamined7c4752015-02-16 19:16:46 -05002856int SSL_in_false_start(const SSL *s) {
2857 return s->s3->tmp.in_false_start;
2858}
2859
Adam Langleyfcf25832014-12-18 17:42:32 -08002860int SSL_cutthrough_complete(const SSL *s) {
David Benjamined7c4752015-02-16 19:16:46 -05002861 return SSL_in_false_start(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002862}
Adam Langley95c29f32014-06-20 12:00:00 -07002863
Adam Langleyfcf25832014-12-18 17:42:32 -08002864void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
2865 size_t *ssl_session_size) {
2866 *ssl_size = sizeof(SSL);
2867 *ssl_ctx_size = sizeof(SSL_CTX);
2868 *ssl_session_size = sizeof(SSL_SESSION);
2869}
Feng Lu41aa3252014-11-21 22:47:56 -08002870
David Benjamined7c4752015-02-16 19:16:46 -05002871int ssl3_can_false_start(const SSL *s) {
David Benjamin195dc782015-02-19 13:27:05 -05002872 const SSL_CIPHER *const cipher = SSL_get_current_cipher(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002873
Adam Langleye631d962015-04-01 13:11:01 -07002874 /* False Start only for TLS 1.2 with an ECDHE+AEAD cipher and ALPN or NPN. */
David Benjamin195dc782015-02-19 13:27:05 -05002875 return !SSL_IS_DTLS(s) &&
2876 SSL_version(s) >= TLS1_2_VERSION &&
2877 (s->s3->alpn_selected || s->s3->next_proto_neg_seen) &&
2878 cipher != NULL &&
Adam Langleye631d962015-04-01 13:11:01 -07002879 cipher->algorithm_mkey == SSL_kECDHE &&
David Benjamin195dc782015-02-19 13:27:05 -05002880 (cipher->algorithm_enc == SSL_AES128GCM ||
2881 cipher->algorithm_enc == SSL_AES256GCM ||
2882 cipher->algorithm_enc == SSL_CHACHA20POLY1305);
Adam Langleyfcf25832014-12-18 17:42:32 -08002883}
2884
2885const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version) {
2886 switch (version) {
2887 case SSL3_VERSION:
2888 return &SSLv3_enc_data;
2889
2890 case TLS1_VERSION:
2891 return &TLSv1_enc_data;
2892
David Benjamin9e13e1a2015-03-05 01:56:32 -05002893 case DTLS1_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002894 case TLS1_1_VERSION:
2895 return &TLSv1_1_enc_data;
2896
David Benjamin9e13e1a2015-03-05 01:56:32 -05002897 case DTLS1_2_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002898 case TLS1_2_VERSION:
2899 return &TLSv1_2_enc_data;
2900
Adam Langleyfcf25832014-12-18 17:42:32 -08002901 default:
2902 return NULL;
2903 }
2904}
2905
2906uint16_t ssl3_get_max_server_version(const SSL *s) {
2907 uint16_t max_version;
2908
2909 if (SSL_IS_DTLS(s)) {
2910 max_version = (s->max_version != 0) ? s->max_version : DTLS1_2_VERSION;
2911 if (!(s->options & SSL_OP_NO_DTLSv1_2) && DTLS1_2_VERSION >= max_version) {
2912 return DTLS1_2_VERSION;
2913 }
2914 if (!(s->options & SSL_OP_NO_DTLSv1) && DTLS1_VERSION >= max_version) {
2915 return DTLS1_VERSION;
2916 }
2917 return 0;
2918 }
2919
2920 max_version = (s->max_version != 0) ? s->max_version : TLS1_2_VERSION;
2921 if (!(s->options & SSL_OP_NO_TLSv1_2) && TLS1_2_VERSION <= max_version) {
2922 return TLS1_2_VERSION;
2923 }
2924 if (!(s->options & SSL_OP_NO_TLSv1_1) && TLS1_1_VERSION <= max_version) {
2925 return TLS1_1_VERSION;
2926 }
2927 if (!(s->options & SSL_OP_NO_TLSv1) && TLS1_VERSION <= max_version) {
2928 return TLS1_VERSION;
2929 }
2930 if (!(s->options & SSL_OP_NO_SSLv3) && SSL3_VERSION <= max_version) {
2931 return SSL3_VERSION;
2932 }
2933 return 0;
2934}
2935
2936uint16_t ssl3_get_mutual_version(SSL *s, uint16_t client_version) {
2937 uint16_t version = 0;
2938
2939 if (SSL_IS_DTLS(s)) {
2940 /* Clamp client_version to max_version. */
2941 if (s->max_version != 0 && client_version < s->max_version) {
2942 client_version = s->max_version;
2943 }
2944
2945 if (client_version <= DTLS1_2_VERSION && !(s->options & SSL_OP_NO_DTLSv1_2)) {
2946 version = DTLS1_2_VERSION;
2947 } else if (client_version <= DTLS1_VERSION &&
David Benjaminb18f0242015-03-10 18:30:08 -04002948 !(s->options & SSL_OP_NO_DTLSv1)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002949 version = DTLS1_VERSION;
2950 }
2951
2952 /* Check against min_version. */
2953 if (version != 0 && s->min_version != 0 && version > s->min_version) {
2954 return 0;
2955 }
2956 return version;
2957 } else {
2958 /* Clamp client_version to max_version. */
2959 if (s->max_version != 0 && client_version > s->max_version) {
2960 client_version = s->max_version;
2961 }
2962
2963 if (client_version >= TLS1_2_VERSION && !(s->options & SSL_OP_NO_TLSv1_2)) {
2964 version = TLS1_2_VERSION;
2965 } else if (client_version >= TLS1_1_VERSION &&
2966 !(s->options & SSL_OP_NO_TLSv1_1)) {
2967 version = TLS1_1_VERSION;
2968 } else if (client_version >= TLS1_VERSION && !(s->options & SSL_OP_NO_TLSv1)) {
2969 version = TLS1_VERSION;
2970 } else if (client_version >= SSL3_VERSION && !(s->options & SSL_OP_NO_SSLv3)) {
2971 version = SSL3_VERSION;
2972 }
2973
2974 /* Check against min_version. */
2975 if (version != 0 && s->min_version != 0 && version < s->min_version) {
2976 return 0;
2977 }
2978 return version;
2979 }
2980}
2981
2982uint16_t ssl3_get_max_client_version(SSL *s) {
2983 unsigned long options = s->options;
2984 uint16_t version = 0;
2985
2986 /* OpenSSL's API for controlling versions entails blacklisting individual
2987 * protocols. This has two problems. First, on the client, the protocol can
2988 * only express a contiguous range of versions. Second, a library consumer
2989 * trying to set a maximum version cannot disable protocol versions that get
2990 * added in a future version of the library.
2991 *
2992 * To account for both of these, OpenSSL interprets the client-side bitmask
2993 * as a min/max range by picking the lowest contiguous non-empty range of
2994 * enabled protocols. Note that this means it is impossible to set a maximum
2995 * version of TLS 1.2 in a future-proof way.
2996 *
2997 * By this scheme, the maximum version is the lowest version V such that V is
2998 * enabled and V+1 is disabled or unimplemented. */
2999 if (SSL_IS_DTLS(s)) {
3000 if (!(options & SSL_OP_NO_DTLSv1_2)) {
3001 version = DTLS1_2_VERSION;
3002 }
3003 if (!(options & SSL_OP_NO_DTLSv1) && (options & SSL_OP_NO_DTLSv1_2)) {
3004 version = DTLS1_VERSION;
3005 }
3006 if (s->max_version != 0 && version < s->max_version) {
3007 version = s->max_version;
3008 }
3009 } else {
3010 if (!(options & SSL_OP_NO_TLSv1_2)) {
3011 version = TLS1_2_VERSION;
3012 }
3013 if (!(options & SSL_OP_NO_TLSv1_1) && (options & SSL_OP_NO_TLSv1_2)) {
3014 version = TLS1_1_VERSION;
3015 }
3016 if (!(options & SSL_OP_NO_TLSv1) && (options & SSL_OP_NO_TLSv1_1)) {
3017 version = TLS1_VERSION;
3018 }
3019 if (!(options & SSL_OP_NO_SSLv3) && (options & SSL_OP_NO_TLSv1)) {
3020 version = SSL3_VERSION;
3021 }
3022 if (s->max_version != 0 && version > s->max_version) {
3023 version = s->max_version;
3024 }
3025 }
3026
3027 return version;
3028}
3029
3030int ssl3_is_version_enabled(SSL *s, uint16_t version) {
3031 if (SSL_IS_DTLS(s)) {
3032 if (s->max_version != 0 && version < s->max_version) {
3033 return 0;
3034 }
3035 if (s->min_version != 0 && version > s->min_version) {
3036 return 0;
3037 }
3038
3039 switch (version) {
3040 case DTLS1_VERSION:
3041 return !(s->options & SSL_OP_NO_DTLSv1);
3042
3043 case DTLS1_2_VERSION:
3044 return !(s->options & SSL_OP_NO_DTLSv1_2);
3045
3046 default:
3047 return 0;
3048 }
3049 } else {
3050 if (s->max_version != 0 && version > s->max_version) {
3051 return 0;
3052 }
3053 if (s->min_version != 0 && version < s->min_version) {
3054 return 0;
3055 }
3056
3057 switch (version) {
3058 case SSL3_VERSION:
3059 return !(s->options & SSL_OP_NO_SSLv3);
3060
3061 case TLS1_VERSION:
3062 return !(s->options & SSL_OP_NO_TLSv1);
3063
3064 case TLS1_1_VERSION:
3065 return !(s->options & SSL_OP_NO_TLSv1_1);
3066
3067 case TLS1_2_VERSION:
3068 return !(s->options & SSL_OP_NO_TLSv1_2);
3069
3070 default:
3071 return 0;
3072 }
3073 }
3074}
3075
David Benjaminea72bd02014-12-21 21:27:41 -05003076uint16_t ssl3_version_from_wire(SSL *s, uint16_t wire_version) {
3077 if (!SSL_IS_DTLS(s)) {
3078 return wire_version;
3079 }
3080
3081 uint16_t tls_version = ~wire_version;
3082 uint16_t version = tls_version + 0x0201;
3083 /* If either component overflowed, clamp it so comparisons still work. */
3084 if ((version >> 8) < (tls_version >> 8)) {
3085 version = 0xff00 | (version & 0xff);
3086 }
3087 if ((version & 0xff) < (tls_version & 0xff)) {
3088 version = (version & 0xff00) | 0xff;
3089 }
3090 /* DTLS 1.0 maps to TLS 1.1, not TLS 1.0. */
3091 if (version == TLS1_VERSION) {
3092 version = TLS1_1_VERSION;
3093 }
3094 return version;
3095}
3096
Adam Langleyfcf25832014-12-18 17:42:32 -08003097int SSL_cache_hit(SSL *s) { return s->hit; }
3098
3099int SSL_is_server(SSL *s) { return s->server; }
3100
Adam Langley524e7172015-02-20 16:04:00 -08003101void SSL_CTX_set_dos_protection_cb(
3102 SSL_CTX *ctx, int (*cb)(const struct ssl_early_callback_ctx *)) {
3103 ctx->dos_protection_cb = cb;
3104}
3105
Adam Langleyfcf25832014-12-18 17:42:32 -08003106void SSL_enable_fastradio_padding(SSL *s, char on_off) {
3107 s->fastradio_padding = on_off;
3108}
Adam Langley44972942015-02-20 13:43:23 -08003109
3110const SSL_CIPHER *SSL_get_cipher_by_value(uint16_t value) {
3111 return ssl3_get_cipher_by_value(value);
3112}
Adam Langley3f92d212015-02-20 15:32:52 -08003113
3114int SSL_get_rc4_state(const SSL *ssl, const RC4_KEY **read_key,
3115 const RC4_KEY **write_key) {
3116 if (ssl->aead_read_ctx == NULL || ssl->aead_write_ctx == NULL) {
3117 return 0;
3118 }
3119
3120 return EVP_AEAD_CTX_get_rc4_state(&ssl->aead_read_ctx->ctx, read_key) &&
3121 EVP_AEAD_CTX_get_rc4_state(&ssl->aead_write_ctx->ctx, write_key);
3122}