blob: f8e9bab2b289e827b399764de87d168e6b52bc15 [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,
David Benjamin71f07942015-04-08 02:36:59 -04001301 &ctx->cipher_list_by_id, str);
Adam Langleyfcf25832014-12-18 17:42:32 -08001302 /* 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
David Benjamin71f07942015-04-08 02:36:59 -04001320 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str);
Adam Langleyfcf25832014-12-18 17:42:32 -08001321 if (sk == NULL) {
1322 return 0;
1323 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1324 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list_tls11,
1325 SSL_R_NO_CIPHER_MATCH);
1326 return 0;
1327 }
David Benjamin9f2c0d72014-10-21 22:00:19 -04001328
Adam Langleyfcf25832014-12-18 17:42:32 -08001329 return 1;
1330}
Adam Langley95c29f32014-06-20 12:00:00 -07001331
Adam Langleyfcf25832014-12-18 17:42:32 -08001332/* specify the ciphers to be used by the SSL */
1333int SSL_set_cipher_list(SSL *s, const char *str) {
1334 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001335
Adam Langleyfcf25832014-12-18 17:42:32 -08001336 sk = ssl_create_cipher_list(s->ctx->method, &s->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001337 &s->cipher_list_by_id, str);
David Benjamin39482a12014-07-20 13:30:15 -04001338
Adam Langleyfcf25832014-12-18 17:42:32 -08001339 /* see comment in SSL_CTX_set_cipher_list */
1340 if (sk == NULL) {
1341 return 0;
1342 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1343 OPENSSL_PUT_ERROR(SSL, SSL_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1344 return 0;
1345 }
David Benjamin39482a12014-07-20 13:30:15 -04001346
Adam Langleyfcf25832014-12-18 17:42:32 -08001347 return 1;
1348}
Adam Langley95c29f32014-06-20 12:00:00 -07001349
Adam Langleyfcf25832014-12-18 17:42:32 -08001350int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, uint8_t *p) {
1351 size_t i;
1352 const SSL_CIPHER *c;
1353 CERT *ct = s->cert;
1354 uint8_t *q;
1355 /* Set disabled masks for this session */
1356 ssl_set_client_disabled(s);
Adam Langley29707792014-06-20 12:00:00 -07001357
Adam Langleyfcf25832014-12-18 17:42:32 -08001358 if (sk == NULL) {
1359 return 0;
1360 }
1361 q = p;
Adam Langley95c29f32014-06-20 12:00:00 -07001362
Adam Langleyfcf25832014-12-18 17:42:32 -08001363 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
1364 c = sk_SSL_CIPHER_value(sk, i);
1365 /* Skip disabled ciphers */
1366 if (c->algorithm_ssl & ct->mask_ssl ||
1367 c->algorithm_mkey & ct->mask_k ||
1368 c->algorithm_auth & ct->mask_a) {
1369 continue;
1370 }
1371 s2n(ssl3_get_cipher_value(c), p);
1372 }
1373
1374 /* If all ciphers were disabled, return the error to the caller. */
1375 if (p == q) {
1376 return 0;
1377 }
1378
1379 /* Add SCSVs. */
1380 if (!s->renegotiate) {
1381 s2n(SSL3_CK_SCSV & 0xffff, p);
1382 }
1383
Adam Langley5f0efe02015-02-20 13:03:16 -08001384 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001385 s2n(SSL3_CK_FALLBACK_SCSV & 0xffff, p);
1386 }
1387
1388 return p - q;
1389}
1390
1391STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs) {
1392 CBS cipher_suites = *cbs;
1393 const SSL_CIPHER *c;
1394 STACK_OF(SSL_CIPHER) * sk;
1395
1396 if (s->s3) {
1397 s->s3->send_connection_binding = 0;
1398 }
1399
1400 if (CBS_len(&cipher_suites) % 2 != 0) {
1401 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1402 SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
1403 return NULL;
1404 }
1405
1406 sk = sk_SSL_CIPHER_new_null();
1407 if (sk == NULL) {
1408 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1409 goto err;
1410 }
1411
1412 if (!CBS_stow(&cipher_suites, &s->cert->ciphers_raw,
1413 &s->cert->ciphers_rawlen)) {
1414 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1415 goto err;
1416 }
1417
1418 while (CBS_len(&cipher_suites) > 0) {
1419 uint16_t cipher_suite;
1420
1421 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
1422 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_INTERNAL_ERROR);
1423 goto err;
1424 }
1425
1426 /* Check for SCSV. */
1427 if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff)) {
1428 /* SCSV is fatal if renegotiating. */
1429 if (s->renegotiate) {
1430 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1431 SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1432 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1433 goto err;
1434 }
1435 s->s3->send_connection_binding = 1;
1436 continue;
1437 }
1438
1439 /* Check for FALLBACK_SCSV. */
1440 if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff)) {
1441 uint16_t max_version = ssl3_get_max_server_version(s);
1442 if (SSL_IS_DTLS(s) ? (uint16_t)s->version > max_version
1443 : (uint16_t)s->version < max_version) {
1444 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1445 SSL_R_INAPPROPRIATE_FALLBACK);
1446 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
1447 goto err;
1448 }
1449 continue;
1450 }
1451
1452 c = ssl3_get_cipher_by_value(cipher_suite);
1453 if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
1454 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1455 goto err;
1456 }
1457 }
1458
1459 return sk;
David Benjamin9f2c0d72014-10-21 22:00:19 -04001460
Adam Langley95c29f32014-06-20 12:00:00 -07001461err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001462 if (sk != NULL) {
1463 sk_SSL_CIPHER_free(sk);
1464 }
1465 return NULL;
1466}
Adam Langley95c29f32014-06-20 12:00:00 -07001467
1468
Adam Langleyfcf25832014-12-18 17:42:32 -08001469/* return a servername extension value if provided in Client Hello, or NULL. So
1470 * far, only host_name types are defined (RFC 3546). */
1471const char *SSL_get_servername(const SSL *s, const int type) {
1472 if (type != TLSEXT_NAMETYPE_host_name) {
1473 return NULL;
1474 }
Adam Langley95c29f32014-06-20 12:00:00 -07001475
Adam Langleyfcf25832014-12-18 17:42:32 -08001476 return s->session && !s->tlsext_hostname ? s->session->tlsext_hostname
1477 : s->tlsext_hostname;
1478}
Adam Langley95c29f32014-06-20 12:00:00 -07001479
Adam Langleyfcf25832014-12-18 17:42:32 -08001480int SSL_get_servername_type(const SSL *s) {
1481 if (s->session &&
1482 (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname)) {
1483 return TLSEXT_NAMETYPE_host_name;
1484 }
Adam Langley95c29f32014-06-20 12:00:00 -07001485
Adam Langleyfcf25832014-12-18 17:42:32 -08001486 return -1;
1487}
Adam Langley95c29f32014-06-20 12:00:00 -07001488
Adam Langleyfcf25832014-12-18 17:42:32 -08001489void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
1490 ctx->signed_cert_timestamps_enabled = 1;
1491}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001492
Adam Langleyfcf25832014-12-18 17:42:32 -08001493int SSL_enable_signed_cert_timestamps(SSL *ssl) {
1494 ssl->signed_cert_timestamps_enabled = 1;
1495 return 1;
1496}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001497
Adam Langleyfcf25832014-12-18 17:42:32 -08001498void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
1499 ctx->ocsp_stapling_enabled = 1;
1500}
David Benjamin6c7aed02014-08-27 16:42:38 -04001501
Adam Langleyfcf25832014-12-18 17:42:32 -08001502int SSL_enable_ocsp_stapling(SSL *ssl) {
1503 ssl->ocsp_stapling_enabled = 1;
1504 return 1;
1505}
David Benjamin6c7aed02014-08-27 16:42:38 -04001506
Adam Langleyfcf25832014-12-18 17:42:32 -08001507void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
1508 size_t *out_len) {
1509 SSL_SESSION *session = ssl->session;
Adam Langley3cb50e02014-08-26 14:00:31 -07001510
Adam Langleyfcf25832014-12-18 17:42:32 -08001511 *out_len = 0;
1512 *out = NULL;
1513 if (ssl->server || !session || !session->tlsext_signed_cert_timestamp_list) {
1514 return;
1515 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001516
Adam Langleyfcf25832014-12-18 17:42:32 -08001517 *out = session->tlsext_signed_cert_timestamp_list;
1518 *out_len = session->tlsext_signed_cert_timestamp_list_length;
1519}
David Benjamin6c7aed02014-08-27 16:42:38 -04001520
Adam Langleyfcf25832014-12-18 17:42:32 -08001521void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
1522 size_t *out_len) {
1523 SSL_SESSION *session = ssl->session;
1524
1525 *out_len = 0;
1526 *out = NULL;
1527 if (ssl->server || !session || !session->ocsp_response) {
1528 return;
1529 }
1530 *out = session->ocsp_response;
1531 *out_len = session->ocsp_response_length;
1532}
David Benjamin6c7aed02014-08-27 16:42:38 -04001533
Adam Langley95c29f32014-06-20 12:00:00 -07001534/* SSL_select_next_proto implements the standard protocol selection. It is
1535 * expected that this function is called from the callback set by
1536 * SSL_CTX_set_next_proto_select_cb.
1537 *
1538 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1539 * strings. The length byte itself is not included in the length. A byte
1540 * string of length 0 is invalid. No byte string may be truncated.
1541 *
1542 * The current, but experimental algorithm for selecting the protocol is:
1543 *
1544 * 1) If the server doesn't support NPN then this is indicated to the
1545 * callback. In this case, the client application has to abort the connection
1546 * or have a default application level protocol.
1547 *
1548 * 2) If the server supports NPN, but advertises an empty list then the
1549 * client selects the first protcol in its list, but indicates via the
1550 * API that this fallback case was enacted.
1551 *
1552 * 3) Otherwise, the client finds the first protocol in the server's list
1553 * that it supports and selects this protocol. This is because it's
1554 * assumed that the server has better information about which protocol
1555 * a client should use.
1556 *
1557 * 4) If the client doesn't support any of the server's advertised
1558 * protocols, then this is treated the same as case 2.
1559 *
1560 * It returns either
1561 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1562 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1563 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001564int SSL_select_next_proto(uint8_t **out, uint8_t *outlen, const uint8_t *server,
1565 unsigned int server_len, const uint8_t *client,
1566 unsigned int client_len) {
1567 unsigned int i, j;
1568 const uint8_t *result;
1569 int status = OPENSSL_NPN_UNSUPPORTED;
Adam Langley95c29f32014-06-20 12:00:00 -07001570
Adam Langleyfcf25832014-12-18 17:42:32 -08001571 /* For each protocol in server preference order, see if we support it. */
1572 for (i = 0; i < server_len;) {
1573 for (j = 0; j < client_len;) {
1574 if (server[i] == client[j] &&
1575 memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
1576 /* We found a match */
1577 result = &server[i];
1578 status = OPENSSL_NPN_NEGOTIATED;
1579 goto found;
1580 }
1581 j += client[j];
1582 j++;
1583 }
1584 i += server[i];
1585 i++;
1586 }
Adam Langley95c29f32014-06-20 12:00:00 -07001587
Adam Langleyfcf25832014-12-18 17:42:32 -08001588 /* There's no overlap between our protocols and the server's list. */
1589 result = client;
1590 status = OPENSSL_NPN_NO_OVERLAP;
Adam Langley95c29f32014-06-20 12:00:00 -07001591
Adam Langleyfcf25832014-12-18 17:42:32 -08001592found:
1593 *out = (uint8_t *)result + 1;
1594 *outlen = result[0];
1595 return status;
1596}
Adam Langley95c29f32014-06-20 12:00:00 -07001597
Adam Langley95c29f32014-06-20 12:00:00 -07001598/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
1599 * requested protocol for this connection and returns 0. If the client didn't
1600 * request any protocol, then *data is set to NULL.
1601 *
1602 * Note that the client can request any protocol it chooses. The value returned
1603 * from this function need not be a member of the list of supported protocols
Adam Langleyfcf25832014-12-18 17:42:32 -08001604 * provided by the callback. */
1605void SSL_get0_next_proto_negotiated(const SSL *s, const uint8_t **data,
1606 unsigned *len) {
1607 *data = s->next_proto_negotiated;
1608 if (!*data) {
1609 *len = 0;
1610 } else {
1611 *len = s->next_proto_negotiated_len;
1612 }
Adam Langley95c29f32014-06-20 12:00:00 -07001613}
1614
1615/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
1616 * TLS server needs a list of supported protocols for Next Protocol
1617 * Negotiation. The returned list must be in wire format. The list is returned
1618 * by setting |out| to point to it and |outlen| to its length. This memory will
1619 * not be modified, but one should assume that the SSL* keeps a reference to
1620 * it.
1621 *
Adam Langleyfcf25832014-12-18 17:42:32 -08001622 * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise.
1623 * Otherwise, no such extension will be included in the ServerHello. */
1624void SSL_CTX_set_next_protos_advertised_cb(
1625 SSL_CTX *ctx,
1626 int (*cb)(SSL *ssl, const uint8_t **out, unsigned int *outlen, void *arg),
1627 void *arg) {
1628 ctx->next_protos_advertised_cb = cb;
1629 ctx->next_protos_advertised_cb_arg = arg;
1630}
Adam Langley95c29f32014-06-20 12:00:00 -07001631
1632/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1633 * client needs to select a protocol from the server's provided list. |out|
1634 * must be set to point to the selected protocol (which may be within |in|).
1635 * The length of the protocol name must be written into |outlen|. The server's
1636 * advertised protocols are provided in |in| and |inlen|. The callback can
1637 * assume that |in| is syntactically valid.
1638 *
1639 * The client must select a protocol. It is fatal to the connection if this
1640 * callback returns a value other than SSL_TLSEXT_ERR_OK.
1641 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001642void SSL_CTX_set_next_proto_select_cb(
1643 SSL_CTX *ctx, int (*cb)(SSL *s, uint8_t **out, uint8_t *outlen,
1644 const uint8_t *in, unsigned int inlen, void *arg),
1645 void *arg) {
1646 ctx->next_proto_select_cb = cb;
1647 ctx->next_proto_select_cb_arg = arg;
1648}
Adam Langley95c29f32014-06-20 12:00:00 -07001649
Adam Langleyfcf25832014-12-18 17:42:32 -08001650int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
1651 unsigned protos_len) {
1652 if (ctx->alpn_client_proto_list) {
1653 OPENSSL_free(ctx->alpn_client_proto_list);
1654 }
Adam Langley95c29f32014-06-20 12:00:00 -07001655
Adam Langleyfcf25832014-12-18 17:42:32 -08001656 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1657 if (!ctx->alpn_client_proto_list) {
1658 return 1;
1659 }
1660 ctx->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001661
Adam Langleyfcf25832014-12-18 17:42:32 -08001662 return 0;
1663}
Adam Langley95c29f32014-06-20 12:00:00 -07001664
Adam Langleyfcf25832014-12-18 17:42:32 -08001665int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
1666 if (ssl->alpn_client_proto_list) {
1667 OPENSSL_free(ssl->alpn_client_proto_list);
1668 }
Adam Langley95c29f32014-06-20 12:00:00 -07001669
Adam Langleyfcf25832014-12-18 17:42:32 -08001670 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1671 if (!ssl->alpn_client_proto_list) {
1672 return 1;
1673 }
1674 ssl->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001675
Adam Langleyfcf25832014-12-18 17:42:32 -08001676 return 0;
1677}
Adam Langley95c29f32014-06-20 12:00:00 -07001678
1679/* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
1680 * during ClientHello processing in order to select an ALPN protocol from the
1681 * client's list of offered protocols. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001682void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
1683 int (*cb)(SSL *ssl, const uint8_t **out,
1684 uint8_t *outlen, const uint8_t *in,
1685 unsigned int inlen, void *arg),
1686 void *arg) {
1687 ctx->alpn_select_cb = cb;
1688 ctx->alpn_select_cb_arg = arg;
1689}
Adam Langley95c29f32014-06-20 12:00:00 -07001690
1691/* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
1692 * On return it sets |*data| to point to |*len| bytes of protocol name (not
1693 * including the leading length-prefix byte). If the server didn't respond with
1694 * a negotiated protocol then |*len| will be zero. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001695void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **data,
1696 unsigned *len) {
1697 *data = NULL;
1698 if (ssl->s3) {
1699 *data = ssl->s3->alpn_selected;
1700 }
1701 if (*data == NULL) {
1702 *len = 0;
1703 } else {
1704 *len = ssl->s3->alpn_selected_len;
1705 }
1706}
Adam Langley95c29f32014-06-20 12:00:00 -07001707
David Benjamincfd248b2015-04-03 11:02:24 -04001708int SSL_export_keying_material(SSL *s, uint8_t *out, size_t out_len,
1709 const char *label, size_t label_len,
1710 const uint8_t *context, size_t context_len,
1711 int use_context) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001712 if (s->version < TLS1_VERSION) {
David Benjaminc565ebb2015-04-03 04:06:36 -04001713 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001714 }
Adam Langley95c29f32014-06-20 12:00:00 -07001715
David Benjamincfd248b2015-04-03 11:02:24 -04001716 return s->enc_method->export_keying_material(
1717 s, out, out_len, label, label_len, context, context_len, use_context);
Adam Langleyfcf25832014-12-18 17:42:32 -08001718}
Adam Langley95c29f32014-06-20 12:00:00 -07001719
Adam Langleyfcf25832014-12-18 17:42:32 -08001720static uint32_t ssl_session_hash(const SSL_SESSION *a) {
1721 uint32_t hash =
1722 ((uint32_t)a->session_id[0]) ||
1723 ((uint32_t)a->session_id[1] << 8) ||
1724 ((uint32_t)a->session_id[2] << 16) ||
1725 ((uint32_t)a->session_id[3] << 24);
Adam Langley95c29f32014-06-20 12:00:00 -07001726
Adam Langleyfcf25832014-12-18 17:42:32 -08001727 return hash;
1728}
Adam Langley95c29f32014-06-20 12:00:00 -07001729
1730/* NB: If this function (or indeed the hash function which uses a sort of
1731 * coarser function than this one) is changed, ensure
1732 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1733 * able to construct an SSL_SESSION that will collide with any existing session
1734 * with a matching session ID. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001735static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
1736 if (a->ssl_version != b->ssl_version) {
1737 return 1;
1738 }
Adam Langley95c29f32014-06-20 12:00:00 -07001739
Adam Langleyfcf25832014-12-18 17:42:32 -08001740 if (a->session_id_length != b->session_id_length) {
1741 return 1;
1742 }
Adam Langley95c29f32014-06-20 12:00:00 -07001743
Adam Langleyfcf25832014-12-18 17:42:32 -08001744 return memcmp(a->session_id, b->session_id, a->session_id_length);
1745}
Adam Langley95c29f32014-06-20 12:00:00 -07001746
Adam Langleyfcf25832014-12-18 17:42:32 -08001747SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth) {
1748 SSL_CTX *ret = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001749
Adam Langleyfcf25832014-12-18 17:42:32 -08001750 if (meth == NULL) {
1751 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_NULL_SSL_METHOD_PASSED);
1752 return NULL;
1753 }
Adam Langley95c29f32014-06-20 12:00:00 -07001754
Adam Langleyfcf25832014-12-18 17:42:32 -08001755 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
1756 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
1757 goto err;
1758 }
Adam Langley95c29f32014-06-20 12:00:00 -07001759
Adam Langleyfcf25832014-12-18 17:42:32 -08001760 ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1761 if (ret == NULL) {
1762 goto err;
1763 }
Adam Langley95c29f32014-06-20 12:00:00 -07001764
Adam Langleyfcf25832014-12-18 17:42:32 -08001765 memset(ret, 0, sizeof(SSL_CTX));
Adam Langley95c29f32014-06-20 12:00:00 -07001766
Adam Langleyfcf25832014-12-18 17:42:32 -08001767 ret->method = meth->method;
Adam Langley95c29f32014-06-20 12:00:00 -07001768
Adam Langleyfcf25832014-12-18 17:42:32 -08001769 ret->cert_store = NULL;
1770 ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
1771 ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1772 ret->session_cache_head = NULL;
1773 ret->session_cache_tail = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001774
Adam Langleyfcf25832014-12-18 17:42:32 -08001775 /* We take the system default */
1776 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001777
Adam Langleyfcf25832014-12-18 17:42:32 -08001778 ret->new_session_cb = 0;
1779 ret->remove_session_cb = 0;
1780 ret->get_session_cb = 0;
1781 ret->generate_session_id = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001782
Adam Langleyfcf25832014-12-18 17:42:32 -08001783 ret->references = 1;
1784 ret->quiet_shutdown = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001785
Adam Langleyfcf25832014-12-18 17:42:32 -08001786 ret->info_callback = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001787
Adam Langleyfcf25832014-12-18 17:42:32 -08001788 ret->app_verify_callback = 0;
1789 ret->app_verify_arg = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001790
Adam Langleyfcf25832014-12-18 17:42:32 -08001791 ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
1792 ret->read_ahead = 0;
1793 ret->msg_callback = 0;
1794 ret->msg_callback_arg = NULL;
1795 ret->verify_mode = SSL_VERIFY_NONE;
1796 ret->sid_ctx_length = 0;
1797 ret->default_verify_callback = NULL;
1798 ret->cert = ssl_cert_new();
1799 if (ret->cert == NULL) {
1800 goto err;
1801 }
Adam Langley95c29f32014-06-20 12:00:00 -07001802
Adam Langleyfcf25832014-12-18 17:42:32 -08001803 ret->default_passwd_callback = 0;
1804 ret->default_passwd_callback_userdata = NULL;
1805 ret->client_cert_cb = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001806
Adam Langleyfcf25832014-12-18 17:42:32 -08001807 ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
1808 if (ret->sessions == NULL) {
1809 goto err;
1810 }
1811 ret->cert_store = X509_STORE_new();
1812 if (ret->cert_store == NULL) {
1813 goto err;
1814 }
Adam Langley95c29f32014-06-20 12:00:00 -07001815
Adam Langleyfcf25832014-12-18 17:42:32 -08001816 ssl_create_cipher_list(ret->method, &ret->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001817 &ret->cipher_list_by_id, SSL_DEFAULT_CIPHER_LIST);
Adam Langleyfcf25832014-12-18 17:42:32 -08001818 if (ret->cipher_list == NULL ||
1819 sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0) {
1820 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_LIBRARY_HAS_NO_CIPHERS);
1821 goto err2;
1822 }
Adam Langley95c29f32014-06-20 12:00:00 -07001823
Adam Langleyfcf25832014-12-18 17:42:32 -08001824 ret->param = X509_VERIFY_PARAM_new();
1825 if (!ret->param) {
1826 goto err;
1827 }
Adam Langley95c29f32014-06-20 12:00:00 -07001828
Adam Langleyfcf25832014-12-18 17:42:32 -08001829 ret->client_CA = sk_X509_NAME_new_null();
1830 if (ret->client_CA == NULL) {
1831 goto err;
1832 }
Adam Langley95c29f32014-06-20 12:00:00 -07001833
Adam Langleyfcf25832014-12-18 17:42:32 -08001834 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001835
Adam Langleyfcf25832014-12-18 17:42:32 -08001836 ret->extra_certs = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001837
Adam Langleyfcf25832014-12-18 17:42:32 -08001838 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
David Benjamin422d3a42014-08-20 11:09:03 -04001839
Adam Langleyfcf25832014-12-18 17:42:32 -08001840 ret->tlsext_servername_callback = 0;
1841 ret->tlsext_servername_arg = NULL;
1842 /* Setup RFC4507 ticket keys */
1843 if (!RAND_bytes(ret->tlsext_tick_key_name, 16) ||
1844 !RAND_bytes(ret->tlsext_tick_hmac_key, 16) ||
1845 !RAND_bytes(ret->tlsext_tick_aes_key, 16)) {
1846 ret->options |= SSL_OP_NO_TICKET;
1847 }
Adam Langley95c29f32014-06-20 12:00:00 -07001848
Adam Langleyfcf25832014-12-18 17:42:32 -08001849 ret->tlsext_status_cb = 0;
1850 ret->tlsext_status_arg = NULL;
David Benjamin82c9e902014-12-12 15:55:27 -05001851
Adam Langleyfcf25832014-12-18 17:42:32 -08001852 ret->next_protos_advertised_cb = 0;
1853 ret->next_proto_select_cb = 0;
1854 ret->psk_identity_hint = NULL;
1855 ret->psk_client_callback = NULL;
1856 ret->psk_server_callback = NULL;
1857
1858 /* Default is to connect to non-RI servers. When RI is more widely deployed
1859 * might change this. */
1860 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
1861
1862 /* Lock the SSL_CTX to the specified version, for compatibility with legacy
1863 * uses of SSL_METHOD. */
1864 if (meth->version != 0) {
1865 SSL_CTX_set_max_version(ret, meth->version);
1866 SSL_CTX_set_min_version(ret, meth->version);
1867 }
1868
1869 return ret;
1870
Adam Langley95c29f32014-06-20 12:00:00 -07001871err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001872 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -07001873err2:
Adam Langleyfcf25832014-12-18 17:42:32 -08001874 if (ret != NULL) {
1875 SSL_CTX_free(ret);
1876 }
1877 return NULL;
1878}
Adam Langley95c29f32014-06-20 12:00:00 -07001879
Adam Langleyfcf25832014-12-18 17:42:32 -08001880void SSL_CTX_free(SSL_CTX *a) {
1881 int i;
Adam Langley95c29f32014-06-20 12:00:00 -07001882
Adam Langleyfcf25832014-12-18 17:42:32 -08001883 if (a == NULL) {
1884 return;
1885 }
Adam Langley95c29f32014-06-20 12:00:00 -07001886
Adam Langleyfcf25832014-12-18 17:42:32 -08001887 i = CRYPTO_add(&a->references, -1, CRYPTO_LOCK_SSL_CTX);
1888 if (i > 0) {
1889 return;
1890 }
Adam Langley95c29f32014-06-20 12:00:00 -07001891
Adam Langleyfcf25832014-12-18 17:42:32 -08001892 if (a->param) {
1893 X509_VERIFY_PARAM_free(a->param);
1894 }
Adam Langley95c29f32014-06-20 12:00:00 -07001895
Adam Langleyfcf25832014-12-18 17:42:32 -08001896 /* Free internal session cache. However: the remove_cb() may reference the
1897 * ex_data of SSL_CTX, thus the ex_data store can only be removed after the
1898 * sessions were flushed. As the ex_data handling routines might also touch
1899 * the session cache, the most secure solution seems to be: empty (flush) the
1900 * cache, then free ex_data, then finally free the cache. (See ticket
1901 * [openssl.org #212].) */
1902 if (a->sessions != NULL) {
1903 SSL_CTX_flush_sessions(a, 0);
1904 }
Adam Langley95c29f32014-06-20 12:00:00 -07001905
Adam Langleyfcf25832014-12-18 17:42:32 -08001906 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001907
Adam Langleyfcf25832014-12-18 17:42:32 -08001908 if (a->sessions != NULL) {
1909 lh_SSL_SESSION_free(a->sessions);
1910 }
1911 if (a->cert_store != NULL) {
1912 X509_STORE_free(a->cert_store);
1913 }
1914 if (a->cipher_list != NULL) {
1915 ssl_cipher_preference_list_free(a->cipher_list);
1916 }
1917 if (a->cipher_list_by_id != NULL) {
1918 sk_SSL_CIPHER_free(a->cipher_list_by_id);
1919 }
1920 if (a->cipher_list_tls11 != NULL) {
1921 ssl_cipher_preference_list_free(a->cipher_list_tls11);
1922 }
1923 if (a->cert != NULL) {
1924 ssl_cert_free(a->cert);
1925 }
1926 if (a->client_CA != NULL) {
1927 sk_X509_NAME_pop_free(a->client_CA, X509_NAME_free);
1928 }
1929 if (a->extra_certs != NULL) {
1930 sk_X509_pop_free(a->extra_certs, X509_free);
1931 }
1932 if (a->srtp_profiles) {
1933 sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);
1934 }
1935 if (a->psk_identity_hint) {
1936 OPENSSL_free(a->psk_identity_hint);
1937 }
1938 if (a->tlsext_ecpointformatlist) {
1939 OPENSSL_free(a->tlsext_ecpointformatlist);
1940 }
1941 if (a->tlsext_ellipticcurvelist) {
1942 OPENSSL_free(a->tlsext_ellipticcurvelist);
1943 }
1944 if (a->alpn_client_proto_list != NULL) {
1945 OPENSSL_free(a->alpn_client_proto_list);
1946 }
1947 if (a->tlsext_channel_id_private) {
1948 EVP_PKEY_free(a->tlsext_channel_id_private);
1949 }
1950 if (a->keylog_bio) {
1951 BIO_free(a->keylog_bio);
1952 }
Adam Langley95c29f32014-06-20 12:00:00 -07001953
Adam Langleyfcf25832014-12-18 17:42:32 -08001954 OPENSSL_free(a);
1955}
Adam Langley95c29f32014-06-20 12:00:00 -07001956
Adam Langleyfcf25832014-12-18 17:42:32 -08001957void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) {
1958 ctx->default_passwd_callback = cb;
1959}
Adam Langley95c29f32014-06-20 12:00:00 -07001960
Adam Langleyfcf25832014-12-18 17:42:32 -08001961void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u) {
1962 ctx->default_passwd_callback_userdata = u;
1963}
Adam Langley95c29f32014-06-20 12:00:00 -07001964
Adam Langleyfcf25832014-12-18 17:42:32 -08001965void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
1966 int (*cb)(X509_STORE_CTX *, void *),
1967 void *arg) {
1968 ctx->app_verify_callback = cb;
1969 ctx->app_verify_arg = arg;
1970}
Adam Langley95c29f32014-06-20 12:00:00 -07001971
Adam Langleyfcf25832014-12-18 17:42:32 -08001972void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
1973 int (*cb)(int, X509_STORE_CTX *)) {
1974 ctx->verify_mode = mode;
1975 ctx->default_verify_callback = cb;
1976}
Adam Langley95c29f32014-06-20 12:00:00 -07001977
Adam Langleyfcf25832014-12-18 17:42:32 -08001978void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
1979 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
1980}
Adam Langley1258b6a2014-06-20 12:00:00 -07001981
Adam Langleyfcf25832014-12-18 17:42:32 -08001982void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb)(SSL *ssl, void *arg),
1983 void *arg) {
1984 ssl_cert_set_cert_cb(c->cert, cb, arg);
1985}
David Benjamin859ec3c2014-09-02 16:29:36 -04001986
Adam Langleyfcf25832014-12-18 17:42:32 -08001987void SSL_set_cert_cb(SSL *s, int (*cb)(SSL *ssl, void *arg), void *arg) {
1988 ssl_cert_set_cert_cb(s->cert, cb, arg);
1989}
Adam Langley95c29f32014-06-20 12:00:00 -07001990
Adam Langleyfcf25832014-12-18 17:42:32 -08001991static int ssl_has_key(SSL *s, size_t idx) {
1992 CERT_PKEY *cpk = &s->cert->pkeys[idx];
1993 return cpk->x509 && cpk->privatekey;
1994}
David Benjamin033e5f42014-11-13 18:47:41 -05001995
David Benjamin107db582015-04-08 00:41:59 -04001996void ssl_get_compatible_server_ciphers(SSL *s, uint32_t *out_mask_k,
1997 uint32_t *out_mask_a) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001998 CERT *c = s->cert;
1999 int rsa_enc, rsa_sign, dh_tmp;
David Benjamin107db582015-04-08 00:41:59 -04002000 uint32_t mask_k, mask_a;
Adam Langleyfcf25832014-12-18 17:42:32 -08002001 int have_ecc_cert, ecdsa_ok;
2002 int have_ecdh_tmp;
2003 X509 *x;
Adam Langley95c29f32014-06-20 12:00:00 -07002004
Adam Langleyfcf25832014-12-18 17:42:32 -08002005 if (c == NULL) {
2006 /* TODO(davidben): Is this codepath possible? */
2007 *out_mask_k = 0;
2008 *out_mask_a = 0;
2009 return;
2010 }
Adam Langley95c29f32014-06-20 12:00:00 -07002011
Adam Langleyfcf25832014-12-18 17:42:32 -08002012 dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07002013
David Benjaminc0f763b2015-03-27 02:05:39 -04002014 have_ecdh_tmp = (c->ecdh_nid != NID_undef || c->ecdh_tmp_cb != NULL ||
2015 c->ecdh_tmp_auto);
Adam Langleyfcf25832014-12-18 17:42:32 -08002016 rsa_enc = ssl_has_key(s, SSL_PKEY_RSA_ENC);
2017 rsa_sign = ssl_has_key(s, SSL_PKEY_RSA_SIGN);
2018 have_ecc_cert = ssl_has_key(s, SSL_PKEY_ECC);
2019 mask_k = 0;
2020 mask_a = 0;
David Benjaminf31e6812014-11-13 18:05:55 -05002021
Adam Langleyfcf25832014-12-18 17:42:32 -08002022 if (rsa_enc) {
2023 mask_k |= SSL_kRSA;
2024 }
2025 if (dh_tmp) {
David Benjamin7061e282015-03-19 11:10:48 -04002026 mask_k |= SSL_kDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08002027 }
2028 if (rsa_enc || rsa_sign) {
2029 mask_a |= SSL_aRSA;
2030 }
Adam Langley95c29f32014-06-20 12:00:00 -07002031
Adam Langleyfcf25832014-12-18 17:42:32 -08002032 /* An ECC certificate may be usable for ECDSA cipher suites depending on the
2033 * key usage extension and on the client's curve preferences. */
2034 if (have_ecc_cert) {
2035 x = c->pkeys[SSL_PKEY_ECC].x509;
2036 /* This call populates extension flags (ex_flags). */
2037 X509_check_purpose(x, -1, 0);
2038 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE)
2039 ? (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)
2040 : 1;
2041 if (!tls1_check_ec_cert(s, x)) {
2042 ecdsa_ok = 0;
2043 }
2044 if (ecdsa_ok) {
2045 mask_a |= SSL_aECDSA;
2046 }
2047 }
Adam Langley95c29f32014-06-20 12:00:00 -07002048
Adam Langleyfcf25832014-12-18 17:42:32 -08002049 /* If we are considering an ECC cipher suite that uses an ephemeral EC
2050 * key, check it. */
2051 if (have_ecdh_tmp && tls1_check_ec_tmp_key(s)) {
David Benjamin7061e282015-03-19 11:10:48 -04002052 mask_k |= SSL_kECDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08002053 }
Adam Langley95c29f32014-06-20 12:00:00 -07002054
Adam Langleyfcf25832014-12-18 17:42:32 -08002055 /* PSK requires a server callback. */
2056 if (s->psk_server_callback != NULL) {
2057 mask_k |= SSL_kPSK;
2058 mask_a |= SSL_aPSK;
2059 }
Adam Langley95c29f32014-06-20 12:00:00 -07002060
Adam Langleyfcf25832014-12-18 17:42:32 -08002061 *out_mask_k = mask_k;
2062 *out_mask_a = mask_a;
2063}
Adam Langley95c29f32014-06-20 12:00:00 -07002064
2065/* This handy macro borrowed from crypto/x509v3/v3_purp.c */
2066#define ku_reject(x, usage) \
Adam Langleyfcf25832014-12-18 17:42:32 -08002067 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
Adam Langley95c29f32014-06-20 12:00:00 -07002068
Adam Langleyfcf25832014-12-18 17:42:32 -08002069int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s) {
2070 unsigned long alg_a;
2071 int signature_nid = 0, md_nid = 0, pk_nid = 0;
2072 const SSL_CIPHER *cs = s->s3->tmp.new_cipher;
Adam Langley95c29f32014-06-20 12:00:00 -07002073
Adam Langleyfcf25832014-12-18 17:42:32 -08002074 alg_a = cs->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -07002075
Adam Langleyfcf25832014-12-18 17:42:32 -08002076 /* This call populates the ex_flags field correctly */
2077 X509_check_purpose(x, -1, 0);
2078 if (x->sig_alg && x->sig_alg->algorithm) {
2079 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
2080 OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
2081 }
2082 if (alg_a & SSL_aECDSA) {
2083 /* key usage, if present, must allow signing */
2084 if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE)) {
2085 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg,
2086 SSL_R_ECC_CERT_NOT_FOR_SIGNING);
2087 return 0;
2088 }
2089 }
Adam Langley95c29f32014-06-20 12:00:00 -07002090
Adam Langleyfcf25832014-12-18 17:42:32 -08002091 return 1; /* all checks are ok */
2092}
Adam Langley95c29f32014-06-20 12:00:00 -07002093
Adam Langleyfcf25832014-12-18 17:42:32 -08002094static int ssl_get_server_cert_index(const SSL *s) {
2095 int idx;
2096 idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
2097 if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509) {
2098 idx = SSL_PKEY_RSA_SIGN;
2099 }
2100 if (idx == -1) {
2101 OPENSSL_PUT_ERROR(SSL, ssl_get_server_cert_index, ERR_R_INTERNAL_ERROR);
2102 }
2103 return idx;
2104}
Adam Langley95c29f32014-06-20 12:00:00 -07002105
Adam Langleyfcf25832014-12-18 17:42:32 -08002106CERT_PKEY *ssl_get_server_send_pkey(const SSL *s) {
2107 int i = ssl_get_server_cert_index(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002108
Adam Langleyfcf25832014-12-18 17:42:32 -08002109 /* This may or may not be an error. */
2110 if (i < 0) {
2111 return NULL;
2112 }
Adam Langley95c29f32014-06-20 12:00:00 -07002113
Adam Langleyfcf25832014-12-18 17:42:32 -08002114 /* May be NULL. */
2115 return &s->cert->pkeys[i];
2116}
Adam Langley95c29f32014-06-20 12:00:00 -07002117
Adam Langleyfcf25832014-12-18 17:42:32 -08002118EVP_PKEY *ssl_get_sign_pkey(SSL *s, const SSL_CIPHER *cipher) {
2119 unsigned long alg_a;
2120 CERT *c;
2121 int idx = -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002122
Adam Langleyfcf25832014-12-18 17:42:32 -08002123 alg_a = cipher->algorithm_auth;
2124 c = s->cert;
Adam Langley95c29f32014-06-20 12:00:00 -07002125
Adam Langleyfcf25832014-12-18 17:42:32 -08002126 if (alg_a & SSL_aRSA) {
2127 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL) {
2128 idx = SSL_PKEY_RSA_SIGN;
2129 } else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL) {
2130 idx = SSL_PKEY_RSA_ENC;
2131 }
2132 } else if ((alg_a & SSL_aECDSA) &&
2133 (c->pkeys[SSL_PKEY_ECC].privatekey != NULL)) {
2134 idx = SSL_PKEY_ECC;
2135 }
Adam Langley95c29f32014-06-20 12:00:00 -07002136
Adam Langleyfcf25832014-12-18 17:42:32 -08002137 if (idx == -1) {
2138 OPENSSL_PUT_ERROR(SSL, ssl_get_sign_pkey, ERR_R_INTERNAL_ERROR);
2139 return NULL;
2140 }
Adam Langley95c29f32014-06-20 12:00:00 -07002141
Adam Langleyfcf25832014-12-18 17:42:32 -08002142 return c->pkeys[idx].privatekey;
2143}
Adam Langley95c29f32014-06-20 12:00:00 -07002144
Adam Langleyfcf25832014-12-18 17:42:32 -08002145void ssl_update_cache(SSL *s, int mode) {
David Benjaminb6d0c6d2015-03-19 19:07:26 -04002146 /* Never cache sessions with empty session IDs. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002147 if (s->session->session_id_length == 0) {
2148 return;
2149 }
Adam Langley95c29f32014-06-20 12:00:00 -07002150
David Benjaminb6d0c6d2015-03-19 19:07:26 -04002151 SSL_CTX *ctx = s->initial_ctx;
2152 if ((ctx->session_cache_mode & mode) == mode && !s->hit &&
2153 ((ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) ||
2154 SSL_CTX_add_session(ctx, s->session)) &&
2155 ctx->new_session_cb != NULL) {
2156 /* Note: |new_session_cb| is called whether the internal session cache is
2157 * used or not. */
2158 if (!ctx->new_session_cb(s, SSL_SESSION_up_ref(s->session))) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002159 SSL_SESSION_free(s->session);
2160 }
2161 }
Adam Langley95c29f32014-06-20 12:00:00 -07002162
David Benjaminb6d0c6d2015-03-19 19:07:26 -04002163 if (!(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR) &&
2164 !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) &&
2165 (ctx->session_cache_mode & mode) == mode) {
2166 /* Automatically flush the internal session cache every 255 connections. */
2167 int flush_cache = 0;
2168 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2169 ctx->handshakes_since_cache_flush++;
2170 if (ctx->handshakes_since_cache_flush >= 255) {
2171 flush_cache = 1;
2172 ctx->handshakes_since_cache_flush = 0;
2173 }
2174 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
2175
2176 if (flush_cache) {
2177 SSL_CTX_flush_sessions(ctx, (unsigned long)time(NULL));
Adam Langleyfcf25832014-12-18 17:42:32 -08002178 }
2179 }
2180}
Adam Langley95c29f32014-06-20 12:00:00 -07002181
David Benjamin1a5c50f2015-03-11 16:22:37 -04002182int SSL_get_error(const SSL *s, int ret_code) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002183 int reason;
David Benjamin1a5c50f2015-03-11 16:22:37 -04002184 uint32_t err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002185 BIO *bio;
Adam Langley95c29f32014-06-20 12:00:00 -07002186
David Benjamin1a5c50f2015-03-11 16:22:37 -04002187 if (ret_code > 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002188 return SSL_ERROR_NONE;
2189 }
Adam Langley95c29f32014-06-20 12:00:00 -07002190
Adam Langleyfcf25832014-12-18 17:42:32 -08002191 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
2192 * where we do encode the error */
David Benjamin1a5c50f2015-03-11 16:22:37 -04002193 err = ERR_peek_error();
2194 if (err != 0) {
2195 if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002196 return SSL_ERROR_SYSCALL;
2197 }
2198 return SSL_ERROR_SSL;
2199 }
Adam Langley95c29f32014-06-20 12:00:00 -07002200
David Benjamin1a5c50f2015-03-11 16:22:37 -04002201 if (ret_code == 0) {
David Benjamin9a38e922015-01-22 16:06:11 -05002202 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2203 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) {
2204 /* The socket was cleanly shut down with a close_notify. */
2205 return SSL_ERROR_ZERO_RETURN;
2206 }
2207 /* An EOF was observed which violates the protocol, and the underlying
2208 * transport does not participate in the error queue. Bubble up to the
2209 * caller. */
2210 return SSL_ERROR_SYSCALL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002211 }
Adam Langleyb2ce0582014-06-20 12:00:00 -07002212
Adam Langleyfcf25832014-12-18 17:42:32 -08002213 if (SSL_want_session(s)) {
2214 return SSL_ERROR_PENDING_SESSION;
2215 }
Adam Langley95c29f32014-06-20 12:00:00 -07002216
Adam Langleyfcf25832014-12-18 17:42:32 -08002217 if (SSL_want_certificate(s)) {
2218 return SSL_ERROR_PENDING_CERTIFICATE;
2219 }
Adam Langley95c29f32014-06-20 12:00:00 -07002220
Adam Langleyfcf25832014-12-18 17:42:32 -08002221 if (SSL_want_read(s)) {
2222 bio = SSL_get_rbio(s);
2223 if (BIO_should_read(bio)) {
2224 return SSL_ERROR_WANT_READ;
2225 }
Adam Langley95c29f32014-06-20 12:00:00 -07002226
Adam Langleyfcf25832014-12-18 17:42:32 -08002227 if (BIO_should_write(bio)) {
2228 /* This one doesn't make too much sense ... We never try to write to the
2229 * rbio, and an application program where rbio and wbio are separate
2230 * couldn't even know what it should wait for. However if we ever set
2231 * s->rwstate incorrectly (so that we have SSL_want_read(s) instead of
2232 * SSL_want_write(s)) and rbio and wbio *are* the same, this test works
2233 * around that bug; so it might be safer to keep it. */
2234 return SSL_ERROR_WANT_WRITE;
2235 }
Adam Langley95c29f32014-06-20 12:00:00 -07002236
Adam Langleyfcf25832014-12-18 17:42:32 -08002237 if (BIO_should_io_special(bio)) {
2238 reason = BIO_get_retry_reason(bio);
2239 if (reason == BIO_RR_CONNECT) {
2240 return SSL_ERROR_WANT_CONNECT;
2241 }
Adam Langley95c29f32014-06-20 12:00:00 -07002242
Adam Langleyfcf25832014-12-18 17:42:32 -08002243 if (reason == BIO_RR_ACCEPT) {
2244 return SSL_ERROR_WANT_ACCEPT;
2245 }
Adam Langley95c29f32014-06-20 12:00:00 -07002246
Adam Langleyfcf25832014-12-18 17:42:32 -08002247 return SSL_ERROR_SYSCALL; /* unknown */
2248 }
2249 }
Adam Langley95c29f32014-06-20 12:00:00 -07002250
Adam Langleyfcf25832014-12-18 17:42:32 -08002251 if (SSL_want_write(s)) {
2252 bio = SSL_get_wbio(s);
2253 if (BIO_should_write(bio)) {
2254 return SSL_ERROR_WANT_WRITE;
2255 }
Adam Langley95c29f32014-06-20 12:00:00 -07002256
Adam Langleyfcf25832014-12-18 17:42:32 -08002257 if (BIO_should_read(bio)) {
2258 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2259 return SSL_ERROR_WANT_READ;
2260 }
Adam Langley95c29f32014-06-20 12:00:00 -07002261
Adam Langleyfcf25832014-12-18 17:42:32 -08002262 if (BIO_should_io_special(bio)) {
2263 reason = BIO_get_retry_reason(bio);
2264 if (reason == BIO_RR_CONNECT) {
2265 return SSL_ERROR_WANT_CONNECT;
2266 }
Adam Langley95c29f32014-06-20 12:00:00 -07002267
Adam Langleyfcf25832014-12-18 17:42:32 -08002268 if (reason == BIO_RR_ACCEPT) {
2269 return SSL_ERROR_WANT_ACCEPT;
2270 }
Adam Langley95c29f32014-06-20 12:00:00 -07002271
Adam Langleyfcf25832014-12-18 17:42:32 -08002272 return SSL_ERROR_SYSCALL;
2273 }
2274 }
Adam Langley95c29f32014-06-20 12:00:00 -07002275
Adam Langleyfcf25832014-12-18 17:42:32 -08002276 if (SSL_want_x509_lookup(s)) {
2277 return SSL_ERROR_WANT_X509_LOOKUP;
2278 }
Adam Langley95c29f32014-06-20 12:00:00 -07002279
Adam Langleyfcf25832014-12-18 17:42:32 -08002280 if (SSL_want_channel_id_lookup(s)) {
2281 return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
2282 }
Adam Langley0f4746e2014-08-13 12:26:32 -07002283
Adam Langleyfcf25832014-12-18 17:42:32 -08002284 return SSL_ERROR_SYSCALL;
2285}
Adam Langley0f4746e2014-08-13 12:26:32 -07002286
Adam Langleyfcf25832014-12-18 17:42:32 -08002287int SSL_do_handshake(SSL *s) {
2288 int ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002289
Adam Langleyfcf25832014-12-18 17:42:32 -08002290 if (s->handshake_func == NULL) {
2291 OPENSSL_PUT_ERROR(SSL, SSL_do_handshake, SSL_R_CONNECTION_TYPE_NOT_SET);
2292 return -1;
2293 }
Adam Langley95c29f32014-06-20 12:00:00 -07002294
Adam Langleyfcf25832014-12-18 17:42:32 -08002295 s->method->ssl_renegotiate_check(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002296
Adam Langleyfcf25832014-12-18 17:42:32 -08002297 if (SSL_in_init(s)) {
2298 ret = s->handshake_func(s);
2299 }
2300 return ret;
2301}
Adam Langley95c29f32014-06-20 12:00:00 -07002302
Adam Langleyfcf25832014-12-18 17:42:32 -08002303void SSL_set_accept_state(SSL *s) {
2304 s->server = 1;
2305 s->shutdown = 0;
2306 s->state = SSL_ST_ACCEPT | SSL_ST_BEFORE;
2307 s->handshake_func = s->method->ssl_accept;
2308 /* clear the current cipher */
2309 ssl_clear_cipher_ctx(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002310}
Adam Langley95c29f32014-06-20 12:00:00 -07002311
Adam Langleyfcf25832014-12-18 17:42:32 -08002312void SSL_set_connect_state(SSL *s) {
2313 s->server = 0;
2314 s->shutdown = 0;
2315 s->state = SSL_ST_CONNECT | SSL_ST_BEFORE;
2316 s->handshake_func = s->method->ssl_connect;
2317 /* clear the current cipher */
2318 ssl_clear_cipher_ctx(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002319}
Adam Langley95c29f32014-06-20 12:00:00 -07002320
Adam Langleyfcf25832014-12-18 17:42:32 -08002321int ssl_undefined_function(SSL *s) {
2322 OPENSSL_PUT_ERROR(SSL, ssl_undefined_function,
2323 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2324 return 0;
2325}
Adam Langley95c29f32014-06-20 12:00:00 -07002326
Adam Langleyfcf25832014-12-18 17:42:32 -08002327int ssl_undefined_void_function(void) {
2328 OPENSSL_PUT_ERROR(SSL, ssl_undefined_void_function,
2329 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2330 return 0;
2331}
Adam Langley95c29f32014-06-20 12:00:00 -07002332
Adam Langleyfcf25832014-12-18 17:42:32 -08002333int ssl_undefined_const_function(const SSL *s) {
2334 OPENSSL_PUT_ERROR(SSL, ssl_undefined_const_function,
2335 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2336 return 0;
2337}
Adam Langley95c29f32014-06-20 12:00:00 -07002338
Adam Langleyfcf25832014-12-18 17:42:32 -08002339static const char *ssl_get_version(int version) {
2340 switch (version) {
2341 case TLS1_2_VERSION:
2342 return "TLSv1.2";
Adam Langley95c29f32014-06-20 12:00:00 -07002343
Adam Langleyfcf25832014-12-18 17:42:32 -08002344 case TLS1_1_VERSION:
2345 return "TLSv1.1";
Adam Langley95c29f32014-06-20 12:00:00 -07002346
Adam Langleyfcf25832014-12-18 17:42:32 -08002347 case TLS1_VERSION:
2348 return "TLSv1";
Adam Langley95c29f32014-06-20 12:00:00 -07002349
Adam Langleyfcf25832014-12-18 17:42:32 -08002350 case SSL3_VERSION:
2351 return "SSLv3";
Adam Langley95c29f32014-06-20 12:00:00 -07002352
Adam Langleyfcf25832014-12-18 17:42:32 -08002353 default:
2354 return "unknown";
2355 }
2356}
Adam Langley95c29f32014-06-20 12:00:00 -07002357
Adam Langleyfcf25832014-12-18 17:42:32 -08002358const char *SSL_get_version(const SSL *s) {
2359 return ssl_get_version(s->version);
2360}
Adam Langley95c29f32014-06-20 12:00:00 -07002361
Adam Langleyfcf25832014-12-18 17:42:32 -08002362const char *SSL_SESSION_get_version(const SSL_SESSION *sess) {
2363 return ssl_get_version(sess->ssl_version);
2364}
Adam Langley95c29f32014-06-20 12:00:00 -07002365
Adam Langleyfcf25832014-12-18 17:42:32 -08002366void ssl_clear_cipher_ctx(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002367 if (s->aead_read_ctx != NULL) {
2368 EVP_AEAD_CTX_cleanup(&s->aead_read_ctx->ctx);
2369 OPENSSL_free(s->aead_read_ctx);
2370 s->aead_read_ctx = NULL;
2371 }
Adam Langleya5dc5452014-06-20 12:00:00 -07002372
Adam Langleyfcf25832014-12-18 17:42:32 -08002373 if (s->aead_write_ctx != NULL) {
2374 EVP_AEAD_CTX_cleanup(&s->aead_write_ctx->ctx);
2375 OPENSSL_free(s->aead_write_ctx);
2376 s->aead_write_ctx = NULL;
2377 }
2378}
Adam Langley95c29f32014-06-20 12:00:00 -07002379
Adam Langleyfcf25832014-12-18 17:42:32 -08002380X509 *SSL_get_certificate(const SSL *s) {
2381 if (s->cert != NULL) {
2382 return s->cert->key->x509;
2383 }
2384
2385 return NULL;
2386}
2387
2388EVP_PKEY *SSL_get_privatekey(const SSL *s) {
2389 if (s->cert != NULL) {
2390 return s->cert->key->privatekey;
2391 }
2392
2393 return NULL;
2394}
2395
2396X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
2397 if (ctx->cert != NULL) {
2398 return ctx->cert->key->x509;
2399 }
2400
2401 return NULL;
2402}
2403
2404EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
2405 if (ctx->cert != NULL) {
2406 return ctx->cert->key->privatekey;
2407 }
2408
2409 return NULL;
2410}
2411
2412const SSL_CIPHER *SSL_get_current_cipher(const SSL *s) {
2413 if (s->session != NULL && s->session->cipher != NULL) {
2414 return s->session->cipher;
2415 }
2416
2417 return NULL;
2418}
2419
2420const void *SSL_get_current_compression(SSL *s) { return NULL; }
2421
2422const void *SSL_get_current_expansion(SSL *s) { return NULL; }
2423
2424int ssl_init_wbio_buffer(SSL *s, int push) {
2425 BIO *bbio;
2426
2427 if (s->bbio == NULL) {
2428 bbio = BIO_new(BIO_f_buffer());
2429 if (bbio == NULL) {
2430 return 0;
2431 }
2432 s->bbio = bbio;
2433 } else {
2434 bbio = s->bbio;
2435 if (s->bbio == s->wbio) {
2436 s->wbio = BIO_pop(s->wbio);
2437 }
2438 }
2439
2440 BIO_reset(bbio);
2441 if (!BIO_set_read_buffer_size(bbio, 1)) {
2442 OPENSSL_PUT_ERROR(SSL, ssl_init_wbio_buffer, ERR_R_BUF_LIB);
2443 return 0;
2444 }
2445
2446 if (push) {
2447 if (s->wbio != bbio) {
2448 s->wbio = BIO_push(bbio, s->wbio);
2449 }
2450 } else {
2451 if (s->wbio == bbio) {
2452 s->wbio = BIO_pop(bbio);
2453 }
2454 }
2455
2456 return 1;
2457}
2458
2459void ssl_free_wbio_buffer(SSL *s) {
2460 if (s->bbio == NULL) {
2461 return;
2462 }
2463
2464 if (s->bbio == s->wbio) {
2465 /* remove buffering */
2466 s->wbio = BIO_pop(s->wbio);
2467 }
2468
2469 BIO_free(s->bbio);
2470 s->bbio = NULL;
2471}
2472
2473void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
2474 ctx->quiet_shutdown = mode;
2475}
2476
2477int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
2478 return ctx->quiet_shutdown;
2479}
2480
2481void SSL_set_quiet_shutdown(SSL *s, int mode) { s->quiet_shutdown = mode; }
2482
2483int SSL_get_quiet_shutdown(const SSL *s) { return s->quiet_shutdown; }
2484
2485void SSL_set_shutdown(SSL *s, int mode) { s->shutdown = mode; }
2486
2487int SSL_get_shutdown(const SSL *s) { return s->shutdown; }
2488
2489int SSL_version(const SSL *s) { return s->version; }
2490
2491SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
2492
2493SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
2494 if (ssl->ctx == ctx) {
2495 return ssl->ctx;
2496 }
2497
2498 if (ctx == NULL) {
2499 ctx = ssl->initial_ctx;
2500 }
2501
2502 if (ssl->cert != NULL) {
2503 ssl_cert_free(ssl->cert);
2504 }
2505
2506 ssl->cert = ssl_cert_dup(ctx->cert);
2507 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
2508 if (ssl->ctx != NULL) {
2509 SSL_CTX_free(ssl->ctx); /* decrement reference count */
2510 }
2511 ssl->ctx = ctx;
2512
2513 ssl->sid_ctx_length = ctx->sid_ctx_length;
2514 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2515 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2516
2517 return ssl->ctx;
2518}
2519
2520int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
2521 return X509_STORE_set_default_paths(ctx->cert_store);
2522}
Adam Langley95c29f32014-06-20 12:00:00 -07002523
2524int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
Adam Langleyfcf25832014-12-18 17:42:32 -08002525 const char *CApath) {
2526 return X509_STORE_load_locations(ctx->cert_store, CAfile, CApath);
2527}
Adam Langley95c29f32014-06-20 12:00:00 -07002528
2529void SSL_set_info_callback(SSL *ssl,
Adam Langleyfcf25832014-12-18 17:42:32 -08002530 void (*cb)(const SSL *ssl, int type, int val)) {
2531 ssl->info_callback = cb;
2532}
Adam Langley95c29f32014-06-20 12:00:00 -07002533
Adam Langleyfcf25832014-12-18 17:42:32 -08002534void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/, int /*type*/,
2535 int /*val*/) {
2536 return ssl->info_callback;
2537}
Adam Langley95c29f32014-06-20 12:00:00 -07002538
Adam Langleyfcf25832014-12-18 17:42:32 -08002539int SSL_state(const SSL *ssl) { return ssl->state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002540
Adam Langleyfcf25832014-12-18 17:42:32 -08002541void SSL_set_state(SSL *ssl, int state) { ssl->state = state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002542
Adam Langleyfcf25832014-12-18 17:42:32 -08002543void SSL_set_verify_result(SSL *ssl, long arg) { ssl->verify_result = arg; }
Adam Langley95c29f32014-06-20 12:00:00 -07002544
Adam Langleyfcf25832014-12-18 17:42:32 -08002545long SSL_get_verify_result(const SSL *ssl) { return ssl->verify_result; }
Adam Langley95c29f32014-06-20 12:00:00 -07002546
Adam Langleyfcf25832014-12-18 17:42:32 -08002547int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2548 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
2549 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp, new_func,
2550 dup_func, free_func);
2551}
Adam Langley95c29f32014-06-20 12:00:00 -07002552
Adam Langleyfcf25832014-12-18 17:42:32 -08002553int SSL_set_ex_data(SSL *s, int idx, void *arg) {
2554 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2555}
Adam Langley95c29f32014-06-20 12:00:00 -07002556
Adam Langleyfcf25832014-12-18 17:42:32 -08002557void *SSL_get_ex_data(const SSL *s, int idx) {
2558 return CRYPTO_get_ex_data(&s->ex_data, idx);
2559}
Adam Langley95c29f32014-06-20 12:00:00 -07002560
Adam Langleyfcf25832014-12-18 17:42:32 -08002561int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2562 CRYPTO_EX_dup *dup_func,
2563 CRYPTO_EX_free *free_func) {
2564 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp, new_func,
2565 dup_func, free_func);
2566}
Adam Langley95c29f32014-06-20 12:00:00 -07002567
Adam Langleyfcf25832014-12-18 17:42:32 -08002568int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg) {
2569 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2570}
Adam Langley95c29f32014-06-20 12:00:00 -07002571
Adam Langleyfcf25832014-12-18 17:42:32 -08002572void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx) {
2573 return CRYPTO_get_ex_data(&s->ex_data, idx);
2574}
Adam Langley95c29f32014-06-20 12:00:00 -07002575
Adam Langleyfcf25832014-12-18 17:42:32 -08002576int ssl_ok(SSL *s) { return 1; }
Adam Langley95c29f32014-06-20 12:00:00 -07002577
Adam Langleyfcf25832014-12-18 17:42:32 -08002578X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
2579 return ctx->cert_store;
2580}
Adam Langley95c29f32014-06-20 12:00:00 -07002581
Adam Langleyfcf25832014-12-18 17:42:32 -08002582void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
2583 if (ctx->cert_store != NULL) {
2584 X509_STORE_free(ctx->cert_store);
2585 }
2586 ctx->cert_store = store;
2587}
Adam Langley95c29f32014-06-20 12:00:00 -07002588
Adam Langleyfcf25832014-12-18 17:42:32 -08002589int SSL_want(const SSL *s) { return s->rwstate; }
Adam Langley95c29f32014-06-20 12:00:00 -07002590
Adam Langleyfcf25832014-12-18 17:42:32 -08002591void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
2592 RSA *(*cb)(SSL *ssl, int is_export,
2593 int keylength)) {
2594 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_RSA_CB, (void (*)(void))cb);
2595}
Adam Langley95c29f32014-06-20 12:00:00 -07002596
Adam Langleyfcf25832014-12-18 17:42:32 -08002597void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
2598 int keylength)) {
2599 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_RSA_CB, (void (*)(void))cb);
2600}
2601
2602void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
2603 DH *(*dh)(SSL *ssl, int is_export,
2604 int keylength)) {
2605 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh);
2606}
2607
2608void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*dh)(SSL *ssl, int is_export,
2609 int keylength)) {
2610 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh);
2611}
2612
2613void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,
2614 EC_KEY *(*ecdh)(SSL *ssl, int is_export,
2615 int keylength)) {
2616 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH_CB, (void (*)(void))ecdh);
2617}
2618
2619void SSL_set_tmp_ecdh_callback(SSL *ssl,
2620 EC_KEY *(*ecdh)(SSL *ssl, int is_export,
2621 int keylength)) {
2622 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_ECDH_CB, (void (*)(void))ecdh);
2623}
2624
2625int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
2626 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2627 OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_psk_identity_hint,
2628 SSL_R_DATA_LENGTH_TOO_LONG);
2629 return 0;
2630 }
2631
2632 if (ctx->psk_identity_hint != NULL) {
2633 OPENSSL_free(ctx->psk_identity_hint);
2634 }
2635
2636 if (identity_hint != NULL) {
2637 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2638 if (ctx->psk_identity_hint == NULL) {
2639 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002640 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002641 } else {
2642 ctx->psk_identity_hint = NULL;
2643 }
Adam Langley95c29f32014-06-20 12:00:00 -07002644
Adam Langleyfcf25832014-12-18 17:42:32 -08002645 return 1;
2646}
2647
2648int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint) {
2649 if (s == NULL) {
2650 return 0;
2651 }
2652
2653 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2654 OPENSSL_PUT_ERROR(SSL, SSL_use_psk_identity_hint,
2655 SSL_R_DATA_LENGTH_TOO_LONG);
2656 return 0;
2657 }
2658
2659 /* Clear currently configured hint, if any. */
2660 if (s->psk_identity_hint != NULL) {
2661 OPENSSL_free(s->psk_identity_hint);
2662 s->psk_identity_hint = NULL;
2663 }
2664
2665 if (identity_hint != NULL) {
2666 s->psk_identity_hint = BUF_strdup(identity_hint);
2667 if (s->psk_identity_hint == NULL) {
2668 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002669 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002670 }
Adam Langley95c29f32014-06-20 12:00:00 -07002671
Adam Langleyfcf25832014-12-18 17:42:32 -08002672 return 1;
2673}
Adam Langley95c29f32014-06-20 12:00:00 -07002674
Adam Langleyfcf25832014-12-18 17:42:32 -08002675const char *SSL_get_psk_identity_hint(const SSL *s) {
2676 if (s == NULL) {
2677 return NULL;
2678 }
2679 return s->psk_identity_hint;
2680}
Adam Langley95c29f32014-06-20 12:00:00 -07002681
Adam Langleyfcf25832014-12-18 17:42:32 -08002682const char *SSL_get_psk_identity(const SSL *s) {
2683 if (s == NULL || s->session == NULL) {
2684 return NULL;
2685 }
Adam Langley95c29f32014-06-20 12:00:00 -07002686
Adam Langleyfcf25832014-12-18 17:42:32 -08002687 return s->session->psk_identity;
2688}
Adam Langley95c29f32014-06-20 12:00:00 -07002689
Adam Langleyfcf25832014-12-18 17:42:32 -08002690void SSL_set_psk_client_callback(
2691 SSL *s, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2692 unsigned int max_identity_len, uint8_t *psk,
2693 unsigned int max_psk_len)) {
2694 s->psk_client_callback = cb;
2695}
Adam Langley95c29f32014-06-20 12:00:00 -07002696
Adam Langleyfcf25832014-12-18 17:42:32 -08002697void SSL_CTX_set_psk_client_callback(
2698 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2699 unsigned int max_identity_len,
2700 uint8_t *psk, unsigned int max_psk_len)) {
2701 ctx->psk_client_callback = cb;
2702}
Adam Langley95c29f32014-06-20 12:00:00 -07002703
Adam Langleyfcf25832014-12-18 17:42:32 -08002704void SSL_set_psk_server_callback(
2705 SSL *s, unsigned int (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
2706 unsigned int max_psk_len)) {
2707 s->psk_server_callback = cb;
2708}
Adam Langley95c29f32014-06-20 12:00:00 -07002709
Adam Langleyfcf25832014-12-18 17:42:32 -08002710void SSL_CTX_set_psk_server_callback(
2711 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *identity,
2712 uint8_t *psk, unsigned int max_psk_len)) {
2713 ctx->psk_server_callback = cb;
2714}
Adam Langley95c29f32014-06-20 12:00:00 -07002715
Adam Langleyfcf25832014-12-18 17:42:32 -08002716void SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) {
2717 ctx->min_version = version;
2718}
Adam Langley95c29f32014-06-20 12:00:00 -07002719
Adam Langleyfcf25832014-12-18 17:42:32 -08002720void SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) {
2721 ctx->max_version = version;
2722}
Adam Langley0289c732014-06-20 12:00:00 -07002723
Adam Langleyfcf25832014-12-18 17:42:32 -08002724void SSL_set_min_version(SSL *ssl, uint16_t version) {
2725 ssl->min_version = version;
2726}
Adam Langley0289c732014-06-20 12:00:00 -07002727
Adam Langleyfcf25832014-12-18 17:42:32 -08002728void SSL_set_max_version(SSL *ssl, uint16_t version) {
2729 ssl->max_version = version;
2730}
Adam Langley95c29f32014-06-20 12:00:00 -07002731
Adam Langleyfcf25832014-12-18 17:42:32 -08002732void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
2733 void (*cb)(int write_p, int version,
2734 int content_type, const void *buf,
2735 size_t len, SSL *ssl, void *arg)) {
2736 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2737}
2738void SSL_set_msg_callback(SSL *ssl,
2739 void (*cb)(int write_p, int version, int content_type,
2740 const void *buf, size_t len, SSL *ssl,
2741 void *arg)) {
2742 SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2743}
Adam Langley95c29f32014-06-20 12:00:00 -07002744
Adam Langleyfcf25832014-12-18 17:42:32 -08002745void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio) {
2746 if (ctx->keylog_bio != NULL) {
2747 BIO_free(ctx->keylog_bio);
2748 }
2749 ctx->keylog_bio = keylog_bio;
2750}
Adam Langley95c29f32014-06-20 12:00:00 -07002751
Adam Langleyfcf25832014-12-18 17:42:32 -08002752static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
2753 static const char hextable[] = "0123456789abcdef";
2754 uint8_t *out;
2755 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002756
Adam Langleyfcf25832014-12-18 17:42:32 -08002757 if (!CBB_add_space(cbb, &out, in_len * 2)) {
2758 return 0;
2759 }
Adam Langley95c29f32014-06-20 12:00:00 -07002760
Adam Langleyfcf25832014-12-18 17:42:32 -08002761 for (i = 0; i < in_len; i++) {
2762 *(out++) = (uint8_t)hextable[in[i] >> 4];
2763 *(out++) = (uint8_t)hextable[in[i] & 0xf];
2764 }
Adam Langley95c29f32014-06-20 12:00:00 -07002765
Adam Langleyfcf25832014-12-18 17:42:32 -08002766 return 1;
2767}
David Benjamin859ec3c2014-09-02 16:29:36 -04002768
2769int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -08002770 const uint8_t *encrypted_premaster,
2771 size_t encrypted_premaster_len,
2772 const uint8_t *premaster,
2773 size_t premaster_len) {
2774 BIO *bio = ctx->keylog_bio;
2775 CBB cbb;
2776 uint8_t *out;
2777 size_t out_len;
2778 int ret;
David Benjamin859ec3c2014-09-02 16:29:36 -04002779
Adam Langleyfcf25832014-12-18 17:42:32 -08002780 if (bio == NULL) {
2781 return 1;
2782 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002783
Adam Langleyfcf25832014-12-18 17:42:32 -08002784 if (encrypted_premaster_len < 8) {
2785 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_rsa_client_key_exchange,
2786 ERR_R_INTERNAL_ERROR);
2787 return 0;
2788 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002789
Adam Langleyfcf25832014-12-18 17:42:32 -08002790 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len * 2 + 1)) {
2791 return 0;
2792 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002793
Adam Langleyfcf25832014-12-18 17:42:32 -08002794 if (!CBB_add_bytes(&cbb, (const uint8_t *)"RSA ", 4) ||
2795 /* Only the first 8 bytes of the encrypted premaster secret are
2796 * logged. */
2797 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
2798 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2799 !cbb_add_hex(&cbb, premaster, premaster_len) ||
2800 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2801 !CBB_finish(&cbb, &out, &out_len)) {
2802 CBB_cleanup(&cbb);
2803 return 0;
2804 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002805
Adam Langleyfcf25832014-12-18 17:42:32 -08002806 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2807 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2808 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
David Benjamin859ec3c2014-09-02 16:29:36 -04002809
Adam Langleyfcf25832014-12-18 17:42:32 -08002810 OPENSSL_free(out);
2811 return ret;
Adam Langley95f22882014-06-20 12:00:00 -07002812}
2813
Adam Langleyfcf25832014-12-18 17:42:32 -08002814int ssl_ctx_log_master_secret(SSL_CTX *ctx, const uint8_t *client_random,
2815 size_t client_random_len, const uint8_t *master,
2816 size_t master_len) {
2817 BIO *bio = ctx->keylog_bio;
2818 CBB cbb;
2819 uint8_t *out;
2820 size_t out_len;
2821 int ret;
Adam Langleyadb739e2014-06-20 12:00:00 -07002822
Adam Langleyfcf25832014-12-18 17:42:32 -08002823 if (bio == NULL) {
2824 return 1;
2825 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002826
Adam Langleyfcf25832014-12-18 17:42:32 -08002827 if (client_random_len != 32) {
2828 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_master_secret, ERR_R_INTERNAL_ERROR);
2829 return 0;
2830 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002831
Adam Langleyfcf25832014-12-18 17:42:32 -08002832 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len * 2 + 1)) {
2833 return 0;
2834 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002835
Adam Langleyfcf25832014-12-18 17:42:32 -08002836 if (!CBB_add_bytes(&cbb, (const uint8_t *)"CLIENT_RANDOM ", 14) ||
2837 !cbb_add_hex(&cbb, client_random, 32) ||
2838 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2839 !cbb_add_hex(&cbb, master, master_len) ||
2840 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2841 !CBB_finish(&cbb, &out, &out_len)) {
2842 CBB_cleanup(&cbb);
2843 return 0;
2844 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002845
Adam Langleyfcf25832014-12-18 17:42:32 -08002846 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2847 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2848 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
David Benjamine99e9122014-12-11 01:46:01 -05002849
Adam Langleyfcf25832014-12-18 17:42:32 -08002850 OPENSSL_free(out);
2851 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -07002852}
2853
David Benjamined7c4752015-02-16 19:16:46 -05002854int SSL_in_false_start(const SSL *s) {
2855 return s->s3->tmp.in_false_start;
2856}
2857
Adam Langleyfcf25832014-12-18 17:42:32 -08002858int SSL_cutthrough_complete(const SSL *s) {
David Benjamined7c4752015-02-16 19:16:46 -05002859 return SSL_in_false_start(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002860}
Adam Langley95c29f32014-06-20 12:00:00 -07002861
Adam Langleyfcf25832014-12-18 17:42:32 -08002862void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
2863 size_t *ssl_session_size) {
2864 *ssl_size = sizeof(SSL);
2865 *ssl_ctx_size = sizeof(SSL_CTX);
2866 *ssl_session_size = sizeof(SSL_SESSION);
2867}
Feng Lu41aa3252014-11-21 22:47:56 -08002868
David Benjamined7c4752015-02-16 19:16:46 -05002869int ssl3_can_false_start(const SSL *s) {
David Benjamin195dc782015-02-19 13:27:05 -05002870 const SSL_CIPHER *const cipher = SSL_get_current_cipher(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002871
Adam Langleye631d962015-04-01 13:11:01 -07002872 /* False Start only for TLS 1.2 with an ECDHE+AEAD cipher and ALPN or NPN. */
David Benjamin195dc782015-02-19 13:27:05 -05002873 return !SSL_IS_DTLS(s) &&
2874 SSL_version(s) >= TLS1_2_VERSION &&
2875 (s->s3->alpn_selected || s->s3->next_proto_neg_seen) &&
2876 cipher != NULL &&
Adam Langleye631d962015-04-01 13:11:01 -07002877 cipher->algorithm_mkey == SSL_kECDHE &&
David Benjamin195dc782015-02-19 13:27:05 -05002878 (cipher->algorithm_enc == SSL_AES128GCM ||
2879 cipher->algorithm_enc == SSL_AES256GCM ||
2880 cipher->algorithm_enc == SSL_CHACHA20POLY1305);
Adam Langleyfcf25832014-12-18 17:42:32 -08002881}
2882
2883const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version) {
2884 switch (version) {
2885 case SSL3_VERSION:
2886 return &SSLv3_enc_data;
2887
2888 case TLS1_VERSION:
2889 return &TLSv1_enc_data;
2890
David Benjamin9e13e1a2015-03-05 01:56:32 -05002891 case DTLS1_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002892 case TLS1_1_VERSION:
2893 return &TLSv1_1_enc_data;
2894
David Benjamin9e13e1a2015-03-05 01:56:32 -05002895 case DTLS1_2_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002896 case TLS1_2_VERSION:
2897 return &TLSv1_2_enc_data;
2898
Adam Langleyfcf25832014-12-18 17:42:32 -08002899 default:
2900 return NULL;
2901 }
2902}
2903
2904uint16_t ssl3_get_max_server_version(const SSL *s) {
2905 uint16_t max_version;
2906
2907 if (SSL_IS_DTLS(s)) {
2908 max_version = (s->max_version != 0) ? s->max_version : DTLS1_2_VERSION;
2909 if (!(s->options & SSL_OP_NO_DTLSv1_2) && DTLS1_2_VERSION >= max_version) {
2910 return DTLS1_2_VERSION;
2911 }
2912 if (!(s->options & SSL_OP_NO_DTLSv1) && DTLS1_VERSION >= max_version) {
2913 return DTLS1_VERSION;
2914 }
2915 return 0;
2916 }
2917
2918 max_version = (s->max_version != 0) ? s->max_version : TLS1_2_VERSION;
2919 if (!(s->options & SSL_OP_NO_TLSv1_2) && TLS1_2_VERSION <= max_version) {
2920 return TLS1_2_VERSION;
2921 }
2922 if (!(s->options & SSL_OP_NO_TLSv1_1) && TLS1_1_VERSION <= max_version) {
2923 return TLS1_1_VERSION;
2924 }
2925 if (!(s->options & SSL_OP_NO_TLSv1) && TLS1_VERSION <= max_version) {
2926 return TLS1_VERSION;
2927 }
2928 if (!(s->options & SSL_OP_NO_SSLv3) && SSL3_VERSION <= max_version) {
2929 return SSL3_VERSION;
2930 }
2931 return 0;
2932}
2933
2934uint16_t ssl3_get_mutual_version(SSL *s, uint16_t client_version) {
2935 uint16_t version = 0;
2936
2937 if (SSL_IS_DTLS(s)) {
2938 /* Clamp client_version to max_version. */
2939 if (s->max_version != 0 && client_version < s->max_version) {
2940 client_version = s->max_version;
2941 }
2942
2943 if (client_version <= DTLS1_2_VERSION && !(s->options & SSL_OP_NO_DTLSv1_2)) {
2944 version = DTLS1_2_VERSION;
2945 } else if (client_version <= DTLS1_VERSION &&
David Benjaminb18f0242015-03-10 18:30:08 -04002946 !(s->options & SSL_OP_NO_DTLSv1)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002947 version = DTLS1_VERSION;
2948 }
2949
2950 /* Check against min_version. */
2951 if (version != 0 && s->min_version != 0 && version > s->min_version) {
2952 return 0;
2953 }
2954 return version;
2955 } else {
2956 /* Clamp client_version to max_version. */
2957 if (s->max_version != 0 && client_version > s->max_version) {
2958 client_version = s->max_version;
2959 }
2960
2961 if (client_version >= TLS1_2_VERSION && !(s->options & SSL_OP_NO_TLSv1_2)) {
2962 version = TLS1_2_VERSION;
2963 } else if (client_version >= TLS1_1_VERSION &&
2964 !(s->options & SSL_OP_NO_TLSv1_1)) {
2965 version = TLS1_1_VERSION;
2966 } else if (client_version >= TLS1_VERSION && !(s->options & SSL_OP_NO_TLSv1)) {
2967 version = TLS1_VERSION;
2968 } else if (client_version >= SSL3_VERSION && !(s->options & SSL_OP_NO_SSLv3)) {
2969 version = SSL3_VERSION;
2970 }
2971
2972 /* Check against min_version. */
2973 if (version != 0 && s->min_version != 0 && version < s->min_version) {
2974 return 0;
2975 }
2976 return version;
2977 }
2978}
2979
2980uint16_t ssl3_get_max_client_version(SSL *s) {
2981 unsigned long options = s->options;
2982 uint16_t version = 0;
2983
2984 /* OpenSSL's API for controlling versions entails blacklisting individual
2985 * protocols. This has two problems. First, on the client, the protocol can
2986 * only express a contiguous range of versions. Second, a library consumer
2987 * trying to set a maximum version cannot disable protocol versions that get
2988 * added in a future version of the library.
2989 *
2990 * To account for both of these, OpenSSL interprets the client-side bitmask
2991 * as a min/max range by picking the lowest contiguous non-empty range of
2992 * enabled protocols. Note that this means it is impossible to set a maximum
2993 * version of TLS 1.2 in a future-proof way.
2994 *
2995 * By this scheme, the maximum version is the lowest version V such that V is
2996 * enabled and V+1 is disabled or unimplemented. */
2997 if (SSL_IS_DTLS(s)) {
2998 if (!(options & SSL_OP_NO_DTLSv1_2)) {
2999 version = DTLS1_2_VERSION;
3000 }
3001 if (!(options & SSL_OP_NO_DTLSv1) && (options & SSL_OP_NO_DTLSv1_2)) {
3002 version = DTLS1_VERSION;
3003 }
3004 if (s->max_version != 0 && version < s->max_version) {
3005 version = s->max_version;
3006 }
3007 } else {
3008 if (!(options & SSL_OP_NO_TLSv1_2)) {
3009 version = TLS1_2_VERSION;
3010 }
3011 if (!(options & SSL_OP_NO_TLSv1_1) && (options & SSL_OP_NO_TLSv1_2)) {
3012 version = TLS1_1_VERSION;
3013 }
3014 if (!(options & SSL_OP_NO_TLSv1) && (options & SSL_OP_NO_TLSv1_1)) {
3015 version = TLS1_VERSION;
3016 }
3017 if (!(options & SSL_OP_NO_SSLv3) && (options & SSL_OP_NO_TLSv1)) {
3018 version = SSL3_VERSION;
3019 }
3020 if (s->max_version != 0 && version > s->max_version) {
3021 version = s->max_version;
3022 }
3023 }
3024
3025 return version;
3026}
3027
3028int ssl3_is_version_enabled(SSL *s, uint16_t version) {
3029 if (SSL_IS_DTLS(s)) {
3030 if (s->max_version != 0 && version < s->max_version) {
3031 return 0;
3032 }
3033 if (s->min_version != 0 && version > s->min_version) {
3034 return 0;
3035 }
3036
3037 switch (version) {
3038 case DTLS1_VERSION:
3039 return !(s->options & SSL_OP_NO_DTLSv1);
3040
3041 case DTLS1_2_VERSION:
3042 return !(s->options & SSL_OP_NO_DTLSv1_2);
3043
3044 default:
3045 return 0;
3046 }
3047 } else {
3048 if (s->max_version != 0 && version > s->max_version) {
3049 return 0;
3050 }
3051 if (s->min_version != 0 && version < s->min_version) {
3052 return 0;
3053 }
3054
3055 switch (version) {
3056 case SSL3_VERSION:
3057 return !(s->options & SSL_OP_NO_SSLv3);
3058
3059 case TLS1_VERSION:
3060 return !(s->options & SSL_OP_NO_TLSv1);
3061
3062 case TLS1_1_VERSION:
3063 return !(s->options & SSL_OP_NO_TLSv1_1);
3064
3065 case TLS1_2_VERSION:
3066 return !(s->options & SSL_OP_NO_TLSv1_2);
3067
3068 default:
3069 return 0;
3070 }
3071 }
3072}
3073
David Benjaminea72bd02014-12-21 21:27:41 -05003074uint16_t ssl3_version_from_wire(SSL *s, uint16_t wire_version) {
3075 if (!SSL_IS_DTLS(s)) {
3076 return wire_version;
3077 }
3078
3079 uint16_t tls_version = ~wire_version;
3080 uint16_t version = tls_version + 0x0201;
3081 /* If either component overflowed, clamp it so comparisons still work. */
3082 if ((version >> 8) < (tls_version >> 8)) {
3083 version = 0xff00 | (version & 0xff);
3084 }
3085 if ((version & 0xff) < (tls_version & 0xff)) {
3086 version = (version & 0xff00) | 0xff;
3087 }
3088 /* DTLS 1.0 maps to TLS 1.1, not TLS 1.0. */
3089 if (version == TLS1_VERSION) {
3090 version = TLS1_1_VERSION;
3091 }
3092 return version;
3093}
3094
Adam Langleyfcf25832014-12-18 17:42:32 -08003095int SSL_cache_hit(SSL *s) { return s->hit; }
3096
3097int SSL_is_server(SSL *s) { return s->server; }
3098
Adam Langley524e7172015-02-20 16:04:00 -08003099void SSL_CTX_set_dos_protection_cb(
3100 SSL_CTX *ctx, int (*cb)(const struct ssl_early_callback_ctx *)) {
3101 ctx->dos_protection_cb = cb;
3102}
3103
Adam Langleyfcf25832014-12-18 17:42:32 -08003104void SSL_enable_fastradio_padding(SSL *s, char on_off) {
3105 s->fastradio_padding = on_off;
3106}
Adam Langley44972942015-02-20 13:43:23 -08003107
David Benjaminb16346b2015-04-08 19:16:58 -04003108void SSL_set_reject_peer_renegotiations(SSL *s, int reject) {
3109 s->reject_peer_renegotiations = !!reject;
3110}
3111
Adam Langley44972942015-02-20 13:43:23 -08003112const SSL_CIPHER *SSL_get_cipher_by_value(uint16_t value) {
3113 return ssl3_get_cipher_by_value(value);
3114}
Adam Langley3f92d212015-02-20 15:32:52 -08003115
3116int SSL_get_rc4_state(const SSL *ssl, const RC4_KEY **read_key,
3117 const RC4_KEY **write_key) {
3118 if (ssl->aead_read_ctx == NULL || ssl->aead_write_ctx == NULL) {
3119 return 0;
3120 }
3121
3122 return EVP_AEAD_CTX_get_rc4_state(&ssl->aead_read_ctx->ctx, read_key) &&
3123 EVP_AEAD_CTX_get_rc4_state(&ssl->aead_write_ctx->ctx, write_key);
3124}