blob: c84b6036740105f323816aaa7d1baa1e744fda60 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
108 *
109 */
110/* ====================================================================
111 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
112 * ECC cipher suite support in OpenSSL originally developed by
113 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
114 */
115/* ====================================================================
116 * Copyright 2005 Nokia. All rights reserved.
117 *
118 * The portions of the attached software ("Contribution") is developed by
119 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
120 * license.
121 *
122 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
123 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
124 * support (see RFC 4279) to OpenSSL.
125 *
126 * No patent licenses or other rights except those expressly stated in
127 * the OpenSSL open source license shall be deemed granted or received
128 * expressly, by implication, estoppel, or otherwise.
129 *
130 * No assurances are provided by Nokia that the Contribution does not
131 * infringe the patent or other intellectual property rights of any third
132 * party or that the license provides you with all the necessary rights
133 * to make use of the Contribution.
134 *
135 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
136 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
137 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
138 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
139 * OTHERWISE. */
140
Adam Langley95c29f32014-06-20 12:00:00 -0700141#include <assert.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400142#include <stdio.h>
143#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700144
David Benjamin39482a12014-07-20 13:30:15 -0400145#include <openssl/bytestring.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700146#include <openssl/dh.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400147#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700148#include <openssl/lhash.h>
149#include <openssl/mem.h>
150#include <openssl/obj.h>
151#include <openssl/rand.h>
152#include <openssl/x509v3.h>
153
David Benjamin2ee94aa2015-04-07 22:38:30 -0400154#include "internal.h"
David Benjamin546f1a52015-04-15 16:46:09 -0400155#include "../crypto/internal.h"
156
Adam Langley95c29f32014-06-20 12:00:00 -0700157
Adam Langleyfcf25832014-12-18 17:42:32 -0800158/* Some error codes are special. Ensure the make_errors.go script never
159 * regresses this. */
160OPENSSL_COMPILE_ASSERT(SSL_R_TLSV1_ALERT_NO_RENEGOTIATION ==
161 SSL_AD_NO_RENEGOTIATION + SSL_AD_REASON_OFFSET,
162 ssl_alert_reason_code_mismatch);
David Benjamine1136082014-09-20 12:28:58 -0400163
David Benjamin9f33fc62015-04-15 17:29:53 -0400164static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl = CRYPTO_EX_DATA_CLASS_INIT;
165static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl_ctx = CRYPTO_EX_DATA_CLASS_INIT;
166
Adam Langleyfcf25832014-12-18 17:42:32 -0800167int SSL_clear(SSL *s) {
168 if (s->method == NULL) {
169 OPENSSL_PUT_ERROR(SSL, SSL_clear, SSL_R_NO_METHOD_SPECIFIED);
170 return 0;
171 }
Adam Langley95c29f32014-06-20 12:00:00 -0700172
Adam Langleyfcf25832014-12-18 17:42:32 -0800173 if (ssl_clear_bad_session(s)) {
174 SSL_SESSION_free(s->session);
175 s->session = NULL;
176 }
Adam Langley95c29f32014-06-20 12:00:00 -0700177
Adam Langleyfcf25832014-12-18 17:42:32 -0800178 s->hit = 0;
179 s->shutdown = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700180
Adam Langleyfcf25832014-12-18 17:42:32 -0800181 if (s->renegotiate) {
182 OPENSSL_PUT_ERROR(SSL, SSL_clear, ERR_R_INTERNAL_ERROR);
183 return 0;
184 }
Adam Langley95c29f32014-06-20 12:00:00 -0700185
Adam Langleyfcf25832014-12-18 17:42:32 -0800186 /* SSL_clear may be called before or after the |s| is initialized in either
187 * accept or connect state. In the latter case, SSL_clear should preserve the
188 * half and reset |s->state| accordingly. */
189 if (s->handshake_func != NULL) {
190 if (s->server) {
191 SSL_set_accept_state(s);
192 } else {
193 SSL_set_connect_state(s);
194 }
195 } else {
196 assert(s->state == 0);
197 }
Adam Langley95c29f32014-06-20 12:00:00 -0700198
David Benjamin62fd1622015-01-11 13:30:01 -0500199 /* TODO(davidben): Some state on |s| is reset both in |SSL_new| and
200 * |SSL_clear| because it is per-connection state rather than configuration
201 * state. Per-connection state should be on |s->s3| and |s->d1| so it is
202 * naturally reset at the right points between |SSL_new|, |SSL_clear|, and
203 * |ssl3_new|. */
204
Adam Langleyfcf25832014-12-18 17:42:32 -0800205 s->rwstate = SSL_NOTHING;
206 s->rstate = SSL_ST_READ_HEADER;
Adam Langley95c29f32014-06-20 12:00:00 -0700207
David Benjamin2755a3e2015-04-22 16:17:58 -0400208 BUF_MEM_free(s->init_buf);
209 s->init_buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700210
David Benjamin62fd1622015-01-11 13:30:01 -0500211 s->packet = NULL;
212 s->packet_length = 0;
213
Adam Langleyfcf25832014-12-18 17:42:32 -0800214 ssl_clear_cipher_ctx(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700215
David Benjamin2755a3e2015-04-22 16:17:58 -0400216 OPENSSL_free(s->next_proto_negotiated);
217 s->next_proto_negotiated = NULL;
218 s->next_proto_negotiated_len = 0;
David Benjamin62fd1622015-01-11 13:30:01 -0500219
220 /* The s->d1->mtu is simultaneously configuration (preserved across
221 * clear) and connection-specific state (gets reset).
222 *
223 * TODO(davidben): Avoid this. */
224 unsigned mtu = 0;
225 if (s->d1 != NULL) {
226 mtu = s->d1->mtu;
227 }
228
229 s->method->ssl_free(s);
230 if (!s->method->ssl_new(s)) {
231 return 0;
232 }
233 s->enc_method = ssl3_get_enc_method(s->version);
234 assert(s->enc_method != NULL);
235
236 if (SSL_IS_DTLS(s) && (SSL_get_options(s) & SSL_OP_NO_QUERY_MTU)) {
237 s->d1->mtu = mtu;
238 }
239
Adam Langleyfcf25832014-12-18 17:42:32 -0800240 s->client_version = s->version;
Adam Langley95c29f32014-06-20 12:00:00 -0700241
Adam Langleyfcf25832014-12-18 17:42:32 -0800242 return 1;
243}
Adam Langley95c29f32014-06-20 12:00:00 -0700244
Adam Langleyfcf25832014-12-18 17:42:32 -0800245SSL *SSL_new(SSL_CTX *ctx) {
246 SSL *s;
Adam Langley95c29f32014-06-20 12:00:00 -0700247
Adam Langleyfcf25832014-12-18 17:42:32 -0800248 if (ctx == NULL) {
249 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_NULL_SSL_CTX);
250 return NULL;
251 }
252 if (ctx->method == NULL) {
253 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
254 return NULL;
255 }
Adam Langley95c29f32014-06-20 12:00:00 -0700256
Adam Langleyfcf25832014-12-18 17:42:32 -0800257 s = (SSL *)OPENSSL_malloc(sizeof(SSL));
258 if (s == NULL) {
259 goto err;
260 }
261 memset(s, 0, sizeof(SSL));
Adam Langley95c29f32014-06-20 12:00:00 -0700262
Adam Langleyfcf25832014-12-18 17:42:32 -0800263 s->min_version = ctx->min_version;
264 s->max_version = ctx->max_version;
David Benjamin1eb367c2014-12-12 18:17:51 -0500265
Adam Langleyfcf25832014-12-18 17:42:32 -0800266 s->options = ctx->options;
267 s->mode = ctx->mode;
268 s->max_cert_list = ctx->max_cert_list;
Adam Langley95c29f32014-06-20 12:00:00 -0700269
David Benjamina5a3eeb2015-03-18 20:26:30 -0400270 s->cert = ssl_cert_dup(ctx->cert);
271 if (s->cert == NULL) {
272 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -0800273 }
Adam Langley95c29f32014-06-20 12:00:00 -0700274
Adam Langleyfcf25832014-12-18 17:42:32 -0800275 s->read_ahead = ctx->read_ahead;
276 s->msg_callback = ctx->msg_callback;
277 s->msg_callback_arg = ctx->msg_callback_arg;
278 s->verify_mode = ctx->verify_mode;
279 s->sid_ctx_length = ctx->sid_ctx_length;
280 assert(s->sid_ctx_length <= sizeof s->sid_ctx);
281 memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));
282 s->verify_callback = ctx->default_verify_callback;
283 s->generate_session_id = ctx->generate_session_id;
Adam Langley95c29f32014-06-20 12:00:00 -0700284
Adam Langleyfcf25832014-12-18 17:42:32 -0800285 s->param = X509_VERIFY_PARAM_new();
286 if (!s->param) {
287 goto err;
288 }
289 X509_VERIFY_PARAM_inherit(s->param, ctx->param);
290 s->quiet_shutdown = ctx->quiet_shutdown;
291 s->max_send_fragment = ctx->max_send_fragment;
Adam Langley95c29f32014-06-20 12:00:00 -0700292
Adam Langleyfcf25832014-12-18 17:42:32 -0800293 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
294 s->ctx = ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -0800295 s->tlsext_ticket_expected = 0;
296 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
297 s->initial_ctx = ctx;
298 if (ctx->tlsext_ecpointformatlist) {
299 s->tlsext_ecpointformatlist = BUF_memdup(
300 ctx->tlsext_ecpointformatlist, ctx->tlsext_ecpointformatlist_length);
301 if (!s->tlsext_ecpointformatlist) {
302 goto err;
303 }
304 s->tlsext_ecpointformatlist_length = ctx->tlsext_ecpointformatlist_length;
305 }
Adam Langley95c29f32014-06-20 12:00:00 -0700306
Adam Langleyfcf25832014-12-18 17:42:32 -0800307 if (ctx->tlsext_ellipticcurvelist) {
308 s->tlsext_ellipticcurvelist =
309 BUF_memdup(ctx->tlsext_ellipticcurvelist,
310 ctx->tlsext_ellipticcurvelist_length * 2);
311 if (!s->tlsext_ellipticcurvelist) {
312 goto err;
313 }
314 s->tlsext_ellipticcurvelist_length = ctx->tlsext_ellipticcurvelist_length;
315 }
316 s->next_proto_negotiated = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700317
Adam Langleyfcf25832014-12-18 17:42:32 -0800318 if (s->ctx->alpn_client_proto_list) {
319 s->alpn_client_proto_list = BUF_memdup(s->ctx->alpn_client_proto_list,
320 s->ctx->alpn_client_proto_list_len);
321 if (s->alpn_client_proto_list == NULL) {
322 goto err;
323 }
324 s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;
325 }
Adam Langley95c29f32014-06-20 12:00:00 -0700326
Adam Langleyfcf25832014-12-18 17:42:32 -0800327 s->verify_result = X509_V_OK;
328 s->method = ctx->method;
Adam Langley95c29f32014-06-20 12:00:00 -0700329
Adam Langleyfcf25832014-12-18 17:42:32 -0800330 if (!s->method->ssl_new(s)) {
331 goto err;
332 }
333 s->enc_method = ssl3_get_enc_method(s->version);
334 assert(s->enc_method != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700335
David Benjamin62fd1622015-01-11 13:30:01 -0500336 s->rwstate = SSL_NOTHING;
337 s->rstate = SSL_ST_READ_HEADER;
Adam Langley95c29f32014-06-20 12:00:00 -0700338
David Benjamin9f33fc62015-04-15 17:29:53 -0400339 CRYPTO_new_ex_data(&g_ex_data_class_ssl, s, &s->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700340
Adam Langleyfcf25832014-12-18 17:42:32 -0800341 s->psk_identity_hint = NULL;
342 if (ctx->psk_identity_hint) {
343 s->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
344 if (s->psk_identity_hint == NULL) {
345 goto err;
346 }
347 }
348 s->psk_client_callback = ctx->psk_client_callback;
349 s->psk_server_callback = ctx->psk_server_callback;
Adam Langley95c29f32014-06-20 12:00:00 -0700350
David Benjamin02ddbfd2015-01-11 13:09:11 -0500351 s->tlsext_channel_id_enabled = ctx->tlsext_channel_id_enabled;
352 if (ctx->tlsext_channel_id_private) {
353 s->tlsext_channel_id_private = EVP_PKEY_dup(ctx->tlsext_channel_id_private);
354 }
355
Adam Langleyfcf25832014-12-18 17:42:32 -0800356 s->signed_cert_timestamps_enabled = s->ctx->signed_cert_timestamps_enabled;
357 s->ocsp_stapling_enabled = s->ctx->ocsp_stapling_enabled;
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200358
Adam Langleyfcf25832014-12-18 17:42:32 -0800359 return s;
360
Adam Langley95c29f32014-06-20 12:00:00 -0700361err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400362 SSL_free(s);
Adam Langleyfcf25832014-12-18 17:42:32 -0800363 OPENSSL_PUT_ERROR(SSL, SSL_new, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700364
Adam Langleyfcf25832014-12-18 17:42:32 -0800365 return NULL;
366}
Adam Langley95c29f32014-06-20 12:00:00 -0700367
Adam Langleyfcf25832014-12-18 17:42:32 -0800368int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
369 unsigned int sid_ctx_len) {
370 if (sid_ctx_len > sizeof ctx->sid_ctx) {
371 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_session_id_context,
372 SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
373 return 0;
374 }
375 ctx->sid_ctx_length = sid_ctx_len;
376 memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700377
Adam Langleyfcf25832014-12-18 17:42:32 -0800378 return 1;
379}
Adam Langley95c29f32014-06-20 12:00:00 -0700380
Adam Langleyfcf25832014-12-18 17:42:32 -0800381int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
382 unsigned int sid_ctx_len) {
383 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
384 OPENSSL_PUT_ERROR(SSL, SSL_set_session_id_context,
385 SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
386 return 0;
387 }
388 ssl->sid_ctx_length = sid_ctx_len;
389 memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700390
Adam Langleyfcf25832014-12-18 17:42:32 -0800391 return 1;
392}
Adam Langley95c29f32014-06-20 12:00:00 -0700393
Adam Langleyfcf25832014-12-18 17:42:32 -0800394int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb) {
395 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
396 ctx->generate_session_id = cb;
397 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
398 return 1;
399}
Adam Langley95c29f32014-06-20 12:00:00 -0700400
Adam Langleyfcf25832014-12-18 17:42:32 -0800401int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800402 ssl->generate_session_id = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -0800403 return 1;
404}
Adam Langley95c29f32014-06-20 12:00:00 -0700405
Adam Langleyfcf25832014-12-18 17:42:32 -0800406int SSL_has_matching_session_id(const SSL *ssl, const uint8_t *id,
407 unsigned int id_len) {
408 /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how we
409 * can "construct" a session to give us the desired check - ie. to find if
410 * there's a session in the hash table that would conflict with any new
411 * session built out of this id/id_len and the ssl_version in use by this
412 * SSL. */
413 SSL_SESSION r, *p;
Adam Langley95c29f32014-06-20 12:00:00 -0700414
Adam Langleyfcf25832014-12-18 17:42:32 -0800415 if (id_len > sizeof r.session_id) {
416 return 0;
417 }
Adam Langley95c29f32014-06-20 12:00:00 -0700418
Adam Langleyfcf25832014-12-18 17:42:32 -0800419 r.ssl_version = ssl->version;
420 r.session_id_length = id_len;
421 memcpy(r.session_id, id, id_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700422
Adam Langleyfcf25832014-12-18 17:42:32 -0800423 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
424 p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r);
425 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
426 return p != NULL;
427}
Adam Langley95c29f32014-06-20 12:00:00 -0700428
Adam Langleyfcf25832014-12-18 17:42:32 -0800429int SSL_CTX_set_purpose(SSL_CTX *s, int purpose) {
430 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
431}
432
433int SSL_set_purpose(SSL *s, int purpose) {
434 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
435}
436
437int SSL_CTX_set_trust(SSL_CTX *s, int trust) {
438 return X509_VERIFY_PARAM_set_trust(s->param, trust);
439}
440
441int SSL_set_trust(SSL *s, int trust) {
442 return X509_VERIFY_PARAM_set_trust(s->param, trust);
443}
444
445int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm) {
446 return X509_VERIFY_PARAM_set1(ctx->param, vpm);
447}
448
449int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm) {
450 return X509_VERIFY_PARAM_set1(ssl->param, vpm);
451}
Adam Langley95c29f32014-06-20 12:00:00 -0700452
Adam Langley858a88d2014-06-20 12:00:00 -0700453void ssl_cipher_preference_list_free(
Adam Langleyfcf25832014-12-18 17:42:32 -0800454 struct ssl_cipher_preference_list_st *cipher_list) {
David Benjamin5d1ec732015-04-22 13:38:00 -0400455 if (cipher_list == NULL) {
456 return;
457 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800458 sk_SSL_CIPHER_free(cipher_list->ciphers);
459 OPENSSL_free(cipher_list->in_group_flags);
460 OPENSSL_free(cipher_list);
461}
Adam Langley858a88d2014-06-20 12:00:00 -0700462
Adam Langleyfcf25832014-12-18 17:42:32 -0800463struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_dup(
464 struct ssl_cipher_preference_list_st *cipher_list) {
465 struct ssl_cipher_preference_list_st *ret = NULL;
466 size_t n = sk_SSL_CIPHER_num(cipher_list->ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700467
Adam Langleyfcf25832014-12-18 17:42:32 -0800468 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
469 if (!ret) {
470 goto err;
471 }
472
473 ret->ciphers = NULL;
474 ret->in_group_flags = NULL;
475 ret->ciphers = sk_SSL_CIPHER_dup(cipher_list->ciphers);
476 if (!ret->ciphers) {
477 goto err;
478 }
479 ret->in_group_flags = BUF_memdup(cipher_list->in_group_flags, n);
480 if (!ret->in_group_flags) {
481 goto err;
482 }
483
484 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -0700485
486err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400487 ssl_cipher_preference_list_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -0800488 return NULL;
489}
Adam Langley858a88d2014-06-20 12:00:00 -0700490
Adam Langleyfcf25832014-12-18 17:42:32 -0800491struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_from_ciphers(
492 STACK_OF(SSL_CIPHER) * ciphers) {
493 struct ssl_cipher_preference_list_st *ret = NULL;
494 size_t n = sk_SSL_CIPHER_num(ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700495
Adam Langleyfcf25832014-12-18 17:42:32 -0800496 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
497 if (!ret) {
498 goto err;
499 }
500 ret->ciphers = NULL;
501 ret->in_group_flags = NULL;
502 ret->ciphers = sk_SSL_CIPHER_dup(ciphers);
503 if (!ret->ciphers) {
504 goto err;
505 }
506 ret->in_group_flags = OPENSSL_malloc(n);
507 if (!ret->in_group_flags) {
508 goto err;
509 }
510 memset(ret->in_group_flags, 0, n);
511 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -0700512
513err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400514 ssl_cipher_preference_list_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -0800515 return NULL;
516}
Adam Langley858a88d2014-06-20 12:00:00 -0700517
Adam Langleyfcf25832014-12-18 17:42:32 -0800518X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) { return ctx->param; }
Adam Langley95c29f32014-06-20 12:00:00 -0700519
Adam Langleyfcf25832014-12-18 17:42:32 -0800520X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) { return ssl->param; }
Adam Langley95c29f32014-06-20 12:00:00 -0700521
Adam Langleyfcf25832014-12-18 17:42:32 -0800522void SSL_certs_clear(SSL *s) { ssl_cert_clear_certs(s->cert); }
Adam Langley95c29f32014-06-20 12:00:00 -0700523
Adam Langleyfcf25832014-12-18 17:42:32 -0800524void SSL_free(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800525 if (s == NULL) {
526 return;
527 }
Adam Langley95c29f32014-06-20 12:00:00 -0700528
David Benjamin2755a3e2015-04-22 16:17:58 -0400529 X509_VERIFY_PARAM_free(s->param);
Adam Langley95c29f32014-06-20 12:00:00 -0700530
David Benjamin9f33fc62015-04-15 17:29:53 -0400531 CRYPTO_free_ex_data(&g_ex_data_class_ssl, s, &s->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700532
Adam Langleyfcf25832014-12-18 17:42:32 -0800533 if (s->bbio != NULL) {
534 /* If the buffering BIO is in place, pop it off */
535 if (s->bbio == s->wbio) {
536 s->wbio = BIO_pop(s->wbio);
537 }
538 BIO_free(s->bbio);
539 s->bbio = NULL;
540 }
Adam Langley95c29f32014-06-20 12:00:00 -0700541
David Benjamin2755a3e2015-04-22 16:17:58 -0400542 int free_wbio = s->wbio != s->rbio;
543 BIO_free_all(s->rbio);
544 if (free_wbio) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800545 BIO_free_all(s->wbio);
546 }
Adam Langley95c29f32014-06-20 12:00:00 -0700547
David Benjamin2755a3e2015-04-22 16:17:58 -0400548 BUF_MEM_free(s->init_buf);
Adam Langley95c29f32014-06-20 12:00:00 -0700549
Adam Langleyfcf25832014-12-18 17:42:32 -0800550 /* add extra stuff */
David Benjamin2755a3e2015-04-22 16:17:58 -0400551 ssl_cipher_preference_list_free(s->cipher_list);
552 sk_SSL_CIPHER_free(s->cipher_list_by_id);
Adam Langley95c29f32014-06-20 12:00:00 -0700553
David Benjamin2755a3e2015-04-22 16:17:58 -0400554 ssl_clear_bad_session(s);
555 SSL_SESSION_free(s->session);
Adam Langley95c29f32014-06-20 12:00:00 -0700556
Adam Langleyfcf25832014-12-18 17:42:32 -0800557 ssl_clear_cipher_ctx(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700558
David Benjamin2755a3e2015-04-22 16:17:58 -0400559 ssl_cert_free(s->cert);
Adam Langley0289c732014-06-20 12:00:00 -0700560
David Benjamin2755a3e2015-04-22 16:17:58 -0400561 OPENSSL_free(s->tlsext_hostname);
562 SSL_CTX_free(s->initial_ctx);
563 OPENSSL_free(s->tlsext_ecpointformatlist);
564 OPENSSL_free(s->tlsext_ellipticcurvelist);
565 OPENSSL_free(s->alpn_client_proto_list);
566 EVP_PKEY_free(s->tlsext_channel_id_private);
567 OPENSSL_free(s->psk_identity_hint);
568 sk_X509_NAME_pop_free(s->client_CA, X509_NAME_free);
569 OPENSSL_free(s->next_proto_negotiated);
570 sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);
Adam Langley95c29f32014-06-20 12:00:00 -0700571
Adam Langleyfcf25832014-12-18 17:42:32 -0800572 if (s->method != NULL) {
573 s->method->ssl_free(s);
574 }
David Benjamin2755a3e2015-04-22 16:17:58 -0400575 SSL_CTX_free(s->ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700576
Adam Langleyfcf25832014-12-18 17:42:32 -0800577 OPENSSL_free(s);
578}
Adam Langley95c29f32014-06-20 12:00:00 -0700579
Adam Langleyfcf25832014-12-18 17:42:32 -0800580void SSL_set_bio(SSL *s, BIO *rbio, BIO *wbio) {
581 /* If the output buffering BIO is still in place, remove it. */
582 if (s->bbio != NULL) {
583 if (s->wbio == s->bbio) {
584 s->wbio = s->wbio->next_bio;
585 s->bbio->next_bio = NULL;
586 }
587 }
Adam Langley95c29f32014-06-20 12:00:00 -0700588
David Benjamin2755a3e2015-04-22 16:17:58 -0400589 if (s->rbio != rbio) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800590 BIO_free_all(s->rbio);
591 }
David Benjamin2755a3e2015-04-22 16:17:58 -0400592 if (s->wbio != wbio && s->rbio != s->wbio) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800593 BIO_free_all(s->wbio);
594 }
595 s->rbio = rbio;
596 s->wbio = wbio;
597}
Adam Langley95c29f32014-06-20 12:00:00 -0700598
Adam Langleyfcf25832014-12-18 17:42:32 -0800599BIO *SSL_get_rbio(const SSL *s) { return s->rbio; }
Adam Langley95c29f32014-06-20 12:00:00 -0700600
Adam Langleyfcf25832014-12-18 17:42:32 -0800601BIO *SSL_get_wbio(const SSL *s) { return s->wbio; }
Adam Langley95c29f32014-06-20 12:00:00 -0700602
Adam Langleyfcf25832014-12-18 17:42:32 -0800603int SSL_get_fd(const SSL *s) { return SSL_get_rfd(s); }
Adam Langley95c29f32014-06-20 12:00:00 -0700604
Adam Langleyfcf25832014-12-18 17:42:32 -0800605int SSL_get_rfd(const SSL *s) {
606 int ret = -1;
607 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700608
Adam Langleyfcf25832014-12-18 17:42:32 -0800609 b = SSL_get_rbio(s);
610 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
611 if (r != NULL) {
612 BIO_get_fd(r, &ret);
613 }
614 return ret;
615}
Adam Langley95c29f32014-06-20 12:00:00 -0700616
Adam Langleyfcf25832014-12-18 17:42:32 -0800617int SSL_get_wfd(const SSL *s) {
618 int ret = -1;
619 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700620
Adam Langleyfcf25832014-12-18 17:42:32 -0800621 b = SSL_get_wbio(s);
622 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
623 if (r != NULL) {
624 BIO_get_fd(r, &ret);
625 }
Adam Langley95c29f32014-06-20 12:00:00 -0700626
Adam Langleyfcf25832014-12-18 17:42:32 -0800627 return ret;
628}
Adam Langley95c29f32014-06-20 12:00:00 -0700629
Adam Langleyfcf25832014-12-18 17:42:32 -0800630int SSL_set_fd(SSL *s, int fd) {
631 int ret = 0;
632 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700633
Adam Langleyfcf25832014-12-18 17:42:32 -0800634 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700635
Adam Langleyfcf25832014-12-18 17:42:32 -0800636 if (bio == NULL) {
637 OPENSSL_PUT_ERROR(SSL, SSL_set_fd, ERR_R_BUF_LIB);
638 goto err;
639 }
640 BIO_set_fd(bio, fd, BIO_NOCLOSE);
641 SSL_set_bio(s, bio, bio);
642 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700643
Adam Langley95c29f32014-06-20 12:00:00 -0700644err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800645 return ret;
646}
Adam Langley95c29f32014-06-20 12:00:00 -0700647
Adam Langleyfcf25832014-12-18 17:42:32 -0800648int SSL_set_wfd(SSL *s, int fd) {
649 int ret = 0;
650 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700651
Adam Langleyfcf25832014-12-18 17:42:32 -0800652 if (s->rbio == NULL || BIO_method_type(s->rbio) != BIO_TYPE_FD ||
653 (int)BIO_get_fd(s->rbio, NULL) != fd) {
654 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700655
Adam Langleyfcf25832014-12-18 17:42:32 -0800656 if (bio == NULL) {
657 OPENSSL_PUT_ERROR(SSL, SSL_set_wfd, ERR_R_BUF_LIB);
658 goto err;
659 }
660 BIO_set_fd(bio, fd, BIO_NOCLOSE);
661 SSL_set_bio(s, SSL_get_rbio(s), bio);
662 } else {
663 SSL_set_bio(s, SSL_get_rbio(s), SSL_get_rbio(s));
664 }
665
666 ret = 1;
667
Adam Langley95c29f32014-06-20 12:00:00 -0700668err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800669 return ret;
670}
Adam Langley95c29f32014-06-20 12:00:00 -0700671
Adam Langleyfcf25832014-12-18 17:42:32 -0800672int SSL_set_rfd(SSL *s, int fd) {
673 int ret = 0;
674 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700675
Adam Langleyfcf25832014-12-18 17:42:32 -0800676 if (s->wbio == NULL || BIO_method_type(s->wbio) != BIO_TYPE_FD ||
677 (int)BIO_get_fd(s->wbio, NULL) != fd) {
678 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700679
Adam Langleyfcf25832014-12-18 17:42:32 -0800680 if (bio == NULL) {
681 OPENSSL_PUT_ERROR(SSL, SSL_set_rfd, ERR_R_BUF_LIB);
682 goto err;
683 }
684 BIO_set_fd(bio, fd, BIO_NOCLOSE);
685 SSL_set_bio(s, bio, SSL_get_wbio(s));
686 } else {
687 SSL_set_bio(s, SSL_get_wbio(s), SSL_get_wbio(s));
688 }
689 ret = 1;
690
Adam Langley95c29f32014-06-20 12:00:00 -0700691err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800692 return ret;
693}
Adam Langley95c29f32014-06-20 12:00:00 -0700694
695/* return length of latest Finished message we sent, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -0800696size_t SSL_get_finished(const SSL *s, void *buf, size_t count) {
697 size_t ret = 0;
698
699 if (s->s3 != NULL) {
700 ret = s->s3->tmp.finish_md_len;
701 if (count > ret) {
702 count = ret;
703 }
704 memcpy(buf, s->s3->tmp.finish_md, count);
705 }
706
707 return ret;
708}
Adam Langley95c29f32014-06-20 12:00:00 -0700709
710/* return length of latest Finished message we expected, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -0800711size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count) {
712 size_t ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700713
Adam Langleyfcf25832014-12-18 17:42:32 -0800714 if (s->s3 != NULL) {
715 ret = s->s3->tmp.peer_finish_md_len;
716 if (count > ret) {
717 count = ret;
718 }
719 memcpy(buf, s->s3->tmp.peer_finish_md, count);
720 }
Adam Langley95c29f32014-06-20 12:00:00 -0700721
Adam Langleyfcf25832014-12-18 17:42:32 -0800722 return ret;
723}
Adam Langley95c29f32014-06-20 12:00:00 -0700724
Adam Langleyfcf25832014-12-18 17:42:32 -0800725int SSL_get_verify_mode(const SSL *s) { return s->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -0700726
Adam Langleyfcf25832014-12-18 17:42:32 -0800727int SSL_get_verify_depth(const SSL *s) {
728 return X509_VERIFY_PARAM_get_depth(s->param);
729}
Adam Langley95c29f32014-06-20 12:00:00 -0700730
Adam Langleyfcf25832014-12-18 17:42:32 -0800731int (*SSL_get_verify_callback(const SSL *s))(int, X509_STORE_CTX *) {
732 return s->verify_callback;
733}
Adam Langley95c29f32014-06-20 12:00:00 -0700734
Adam Langleyfcf25832014-12-18 17:42:32 -0800735int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) { return ctx->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -0700736
Adam Langleyfcf25832014-12-18 17:42:32 -0800737int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) {
738 return X509_VERIFY_PARAM_get_depth(ctx->param);
739}
Adam Langley95c29f32014-06-20 12:00:00 -0700740
Adam Langleyfcf25832014-12-18 17:42:32 -0800741int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int, X509_STORE_CTX *) {
742 return ctx->default_verify_callback;
743}
Adam Langley95c29f32014-06-20 12:00:00 -0700744
Adam Langleyfcf25832014-12-18 17:42:32 -0800745void SSL_set_verify(SSL *s, int mode,
746 int (*callback)(int ok, X509_STORE_CTX *ctx)) {
747 s->verify_mode = mode;
748 if (callback != NULL) {
749 s->verify_callback = callback;
750 }
751}
Adam Langley95c29f32014-06-20 12:00:00 -0700752
Adam Langleyfcf25832014-12-18 17:42:32 -0800753void SSL_set_verify_depth(SSL *s, int depth) {
754 X509_VERIFY_PARAM_set_depth(s->param, depth);
755}
Adam Langley95c29f32014-06-20 12:00:00 -0700756
Adam Langleyfcf25832014-12-18 17:42:32 -0800757void SSL_set_read_ahead(SSL *s, int yes) { s->read_ahead = yes; }
Adam Langley95c29f32014-06-20 12:00:00 -0700758
Adam Langleyfcf25832014-12-18 17:42:32 -0800759int SSL_get_read_ahead(const SSL *s) { return s->read_ahead; }
Adam Langley95c29f32014-06-20 12:00:00 -0700760
Adam Langleyfcf25832014-12-18 17:42:32 -0800761int SSL_pending(const SSL *s) {
762 /* SSL_pending cannot work properly if read-ahead is enabled
763 * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)), and it is
764 * impossible to fix since SSL_pending cannot report errors that may be
765 * observed while scanning the new data. (Note that SSL_pending() is often
766 * used as a boolean value, so we'd better not return -1.). */
767 return s->method->ssl_pending(s);
768}
Adam Langley95c29f32014-06-20 12:00:00 -0700769
Adam Langleyfcf25832014-12-18 17:42:32 -0800770X509 *SSL_get_peer_certificate(const SSL *s) {
771 X509 *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700772
Adam Langleyfcf25832014-12-18 17:42:32 -0800773 if (s == NULL || s->session == NULL) {
774 r = NULL;
775 } else {
776 r = s->session->peer;
777 }
Adam Langley95c29f32014-06-20 12:00:00 -0700778
Adam Langleyfcf25832014-12-18 17:42:32 -0800779 if (r == NULL) {
780 return NULL;
781 }
Adam Langley95c29f32014-06-20 12:00:00 -0700782
Adam Langleyfcf25832014-12-18 17:42:32 -0800783 return X509_up_ref(r);
784}
785
786STACK_OF(X509) * SSL_get_peer_cert_chain(const SSL *s) {
787 STACK_OF(X509) * r;
788
789 if (s == NULL || s->session == NULL || s->session->sess_cert == NULL) {
790 r = NULL;
791 } else {
792 r = s->session->sess_cert->cert_chain;
793 }
794
795 /* If we are a client, cert_chain includes the peer's own certificate; if we
796 * are a server, it does not. */
797 return r;
798}
Adam Langley95c29f32014-06-20 12:00:00 -0700799
Adam Langley95c29f32014-06-20 12:00:00 -0700800/* Fix this so it checks all the valid key/cert options */
Adam Langleyfcf25832014-12-18 17:42:32 -0800801int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
802 if (ctx == NULL || ctx->cert == NULL || ctx->cert->key->x509 == NULL) {
803 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key,
804 SSL_R_NO_CERTIFICATE_ASSIGNED);
805 return 0;
806 }
807
808 if (ctx->cert->key->privatekey == NULL) {
809 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key,
810 SSL_R_NO_PRIVATE_KEY_ASSIGNED);
811 return 0;
812 }
813
814 return X509_check_private_key(ctx->cert->key->x509,
815 ctx->cert->key->privatekey);
816}
Adam Langley95c29f32014-06-20 12:00:00 -0700817
818/* Fix this function so that it takes an optional type parameter */
Adam Langleyfcf25832014-12-18 17:42:32 -0800819int SSL_check_private_key(const SSL *ssl) {
820 if (ssl == NULL) {
821 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, ERR_R_PASSED_NULL_PARAMETER);
822 return 0;
823 }
Adam Langley95c29f32014-06-20 12:00:00 -0700824
Adam Langleyfcf25832014-12-18 17:42:32 -0800825 if (ssl->cert == NULL) {
826 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
827 SSL_R_NO_CERTIFICATE_ASSIGNED);
828 return 0;
829 }
Adam Langley95c29f32014-06-20 12:00:00 -0700830
Adam Langleyfcf25832014-12-18 17:42:32 -0800831 if (ssl->cert->key->x509 == NULL) {
832 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
833 SSL_R_NO_CERTIFICATE_ASSIGNED);
834 return 0;
835 }
David Benjamin0b145c22014-11-26 20:10:09 -0500836
Adam Langleyfcf25832014-12-18 17:42:32 -0800837 if (ssl->cert->key->privatekey == NULL) {
838 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
839 SSL_R_NO_PRIVATE_KEY_ASSIGNED);
840 return 0;
841 }
Adam Langley95c29f32014-06-20 12:00:00 -0700842
Adam Langleyfcf25832014-12-18 17:42:32 -0800843 return X509_check_private_key(ssl->cert->key->x509,
844 ssl->cert->key->privatekey);
845}
Adam Langley95c29f32014-06-20 12:00:00 -0700846
Adam Langleyfcf25832014-12-18 17:42:32 -0800847int SSL_accept(SSL *s) {
848 if (s->handshake_func == 0) {
849 /* Not properly initialized yet */
850 SSL_set_accept_state(s);
851 }
David Benjamin0b145c22014-11-26 20:10:09 -0500852
Adam Langleyfcf25832014-12-18 17:42:32 -0800853 if (s->handshake_func != s->method->ssl_accept) {
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +0100854 OPENSSL_PUT_ERROR(SSL, SSL_accept, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800855 return -1;
856 }
Adam Langley95c29f32014-06-20 12:00:00 -0700857
Adam Langleyfcf25832014-12-18 17:42:32 -0800858 return s->handshake_func(s);
859}
Adam Langley95c29f32014-06-20 12:00:00 -0700860
Adam Langleyfcf25832014-12-18 17:42:32 -0800861int SSL_connect(SSL *s) {
862 if (s->handshake_func == 0) {
863 /* Not properly initialized yet */
864 SSL_set_connect_state(s);
865 }
Adam Langley95c29f32014-06-20 12:00:00 -0700866
Adam Langleyfcf25832014-12-18 17:42:32 -0800867 if (s->handshake_func != s->method->ssl_connect) {
868 OPENSSL_PUT_ERROR(SSL, SSL_connect, ERR_R_INTERNAL_ERROR);
869 return -1;
870 }
Adam Langley95c29f32014-06-20 12:00:00 -0700871
Adam Langleyfcf25832014-12-18 17:42:32 -0800872 return s->handshake_func(s);
873}
Adam Langley95c29f32014-06-20 12:00:00 -0700874
Adam Langleyfcf25832014-12-18 17:42:32 -0800875long SSL_get_default_timeout(const SSL *s) {
876 return SSL_DEFAULT_SESSION_TIMEOUT;
877}
Adam Langley95c29f32014-06-20 12:00:00 -0700878
Adam Langleyfcf25832014-12-18 17:42:32 -0800879int SSL_read(SSL *s, void *buf, int num) {
880 if (s->handshake_func == 0) {
881 OPENSSL_PUT_ERROR(SSL, SSL_read, SSL_R_UNINITIALIZED);
882 return -1;
883 }
Adam Langley95c29f32014-06-20 12:00:00 -0700884
Adam Langleyfcf25832014-12-18 17:42:32 -0800885 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
886 s->rwstate = SSL_NOTHING;
887 return 0;
888 }
Adam Langley95c29f32014-06-20 12:00:00 -0700889
Adam Langleyfcf25832014-12-18 17:42:32 -0800890 return s->method->ssl_read(s, buf, num);
891}
Adam Langley95c29f32014-06-20 12:00:00 -0700892
Adam Langleyfcf25832014-12-18 17:42:32 -0800893int SSL_peek(SSL *s, void *buf, int num) {
894 if (s->handshake_func == 0) {
895 OPENSSL_PUT_ERROR(SSL, SSL_peek, SSL_R_UNINITIALIZED);
896 return -1;
897 }
Adam Langley95c29f32014-06-20 12:00:00 -0700898
Adam Langleyfcf25832014-12-18 17:42:32 -0800899 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
900 return 0;
901 }
Adam Langley95c29f32014-06-20 12:00:00 -0700902
Adam Langleyfcf25832014-12-18 17:42:32 -0800903 return s->method->ssl_peek(s, buf, num);
904}
Adam Langley95c29f32014-06-20 12:00:00 -0700905
Adam Langleyfcf25832014-12-18 17:42:32 -0800906int SSL_write(SSL *s, const void *buf, int num) {
907 if (s->handshake_func == 0) {
908 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_UNINITIALIZED);
909 return -1;
910 }
Adam Langley95c29f32014-06-20 12:00:00 -0700911
Adam Langleyfcf25832014-12-18 17:42:32 -0800912 if (s->shutdown & SSL_SENT_SHUTDOWN) {
913 s->rwstate = SSL_NOTHING;
914 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_PROTOCOL_IS_SHUTDOWN);
915 return -1;
916 }
Adam Langley95c29f32014-06-20 12:00:00 -0700917
Adam Langleyfcf25832014-12-18 17:42:32 -0800918 return s->method->ssl_write(s, buf, num);
919}
Adam Langley95c29f32014-06-20 12:00:00 -0700920
Adam Langleyfcf25832014-12-18 17:42:32 -0800921int SSL_shutdown(SSL *s) {
922 /* Note that this function behaves differently from what one might expect.
923 * Return values are 0 for no success (yet), 1 for success; but calling it
924 * once is usually not enough, even if blocking I/O is used (see
925 * ssl3_shutdown). */
Adam Langley95c29f32014-06-20 12:00:00 -0700926
Adam Langleyfcf25832014-12-18 17:42:32 -0800927 if (s->handshake_func == 0) {
928 OPENSSL_PUT_ERROR(SSL, SSL_shutdown, SSL_R_UNINITIALIZED);
929 return -1;
930 }
Adam Langley95c29f32014-06-20 12:00:00 -0700931
Adam Langleyfcf25832014-12-18 17:42:32 -0800932 if (!SSL_in_init(s)) {
933 return s->method->ssl_shutdown(s);
934 }
Adam Langley95c29f32014-06-20 12:00:00 -0700935
Adam Langleyfcf25832014-12-18 17:42:32 -0800936 return 1;
937}
Adam Langley95c29f32014-06-20 12:00:00 -0700938
Adam Langleyfcf25832014-12-18 17:42:32 -0800939int SSL_renegotiate(SSL *s) {
David Benjamin2cdace92015-01-24 13:30:02 -0500940 if (SSL_IS_DTLS(s)) {
941 /* Renegotiation is not supported for DTLS. */
942 OPENSSL_PUT_ERROR(SSL, SSL_renegotiate, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
943 return 0;
944 }
945
Adam Langleyfcf25832014-12-18 17:42:32 -0800946 if (s->renegotiate == 0) {
947 s->renegotiate = 1;
948 }
Adam Langley95c29f32014-06-20 12:00:00 -0700949
Adam Langleyfcf25832014-12-18 17:42:32 -0800950 s->new_session = 1;
951 return s->method->ssl_renegotiate(s);
952}
Adam Langley95c29f32014-06-20 12:00:00 -0700953
Adam Langleyfcf25832014-12-18 17:42:32 -0800954int SSL_renegotiate_pending(SSL *s) {
955 /* becomes true when negotiation is requested; false again once a handshake
956 * has finished */
957 return s->renegotiate != 0;
958}
Adam Langley95c29f32014-06-20 12:00:00 -0700959
Adam Langleyfcf25832014-12-18 17:42:32 -0800960long SSL_ctrl(SSL *s, int cmd, long larg, void *parg) {
961 long l;
Adam Langley95c29f32014-06-20 12:00:00 -0700962
Adam Langleyfcf25832014-12-18 17:42:32 -0800963 switch (cmd) {
964 case SSL_CTRL_GET_READ_AHEAD:
965 return s->read_ahead;
Adam Langley95c29f32014-06-20 12:00:00 -0700966
Adam Langleyfcf25832014-12-18 17:42:32 -0800967 case SSL_CTRL_SET_READ_AHEAD:
968 l = s->read_ahead;
969 s->read_ahead = larg;
970 return l;
Adam Langley95c29f32014-06-20 12:00:00 -0700971
Adam Langleyfcf25832014-12-18 17:42:32 -0800972 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
973 s->msg_callback_arg = parg;
974 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700975
Adam Langleyfcf25832014-12-18 17:42:32 -0800976 case SSL_CTRL_OPTIONS:
977 return s->options |= larg;
Adam Langley95c29f32014-06-20 12:00:00 -0700978
Adam Langleyfcf25832014-12-18 17:42:32 -0800979 case SSL_CTRL_CLEAR_OPTIONS:
980 return s->options &= ~larg;
Adam Langley95c29f32014-06-20 12:00:00 -0700981
Adam Langleyfcf25832014-12-18 17:42:32 -0800982 case SSL_CTRL_MODE:
983 return s->mode |= larg;
Adam Langley95c29f32014-06-20 12:00:00 -0700984
Adam Langleyfcf25832014-12-18 17:42:32 -0800985 case SSL_CTRL_CLEAR_MODE:
986 return s->mode &= ~larg;
Adam Langley95c29f32014-06-20 12:00:00 -0700987
Adam Langleyfcf25832014-12-18 17:42:32 -0800988 case SSL_CTRL_GET_MAX_CERT_LIST:
989 return s->max_cert_list;
Adam Langley95c29f32014-06-20 12:00:00 -0700990
Adam Langleyfcf25832014-12-18 17:42:32 -0800991 case SSL_CTRL_SET_MAX_CERT_LIST:
992 l = s->max_cert_list;
993 s->max_cert_list = larg;
994 return l;
Adam Langley95c29f32014-06-20 12:00:00 -0700995
Adam Langleyfcf25832014-12-18 17:42:32 -0800996 case SSL_CTRL_SET_MTU:
997 if (larg < (long)dtls1_min_mtu()) {
998 return 0;
999 }
1000 if (SSL_IS_DTLS(s)) {
1001 s->d1->mtu = larg;
1002 return larg;
1003 }
1004 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001005
Adam Langleyfcf25832014-12-18 17:42:32 -08001006 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1007 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH) {
1008 return 0;
1009 }
1010 s->max_send_fragment = larg;
1011 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001012
Adam Langleyfcf25832014-12-18 17:42:32 -08001013 case SSL_CTRL_GET_RI_SUPPORT:
1014 if (s->s3) {
1015 return s->s3->send_connection_binding;
1016 }
1017 return 0;
1018
Adam Langleyfcf25832014-12-18 17:42:32 -08001019 case SSL_CTRL_GET_RAW_CIPHERLIST:
1020 if (parg) {
1021 if (s->cert->ciphers_raw == NULL) {
1022 return 0;
1023 }
1024 *(uint8_t **)parg = s->cert->ciphers_raw;
1025 return (int)s->cert->ciphers_rawlen;
1026 }
1027
1028 /* Passing a NULL |parg| returns the size of a single
1029 * cipher suite value. */
1030 return 2;
1031
1032 default:
1033 return s->method->ssl_ctrl(s, cmd, larg, parg);
1034 }
1035}
1036
1037long SSL_callback_ctrl(SSL *s, int cmd, void (*fp)(void)) {
1038 switch (cmd) {
1039 case SSL_CTRL_SET_MSG_CALLBACK:
1040 s->msg_callback =
1041 (void (*)(int write_p, int version, int content_type, const void *buf,
1042 size_t len, SSL *ssl, void *arg))(fp);
1043 return 1;
1044
1045 default:
1046 return s->method->ssl_callback_ctrl(s, cmd, fp);
1047 }
1048}
1049
1050LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) { return ctx->sessions; }
1051
1052long SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg) {
1053 long l;
1054
1055 switch (cmd) {
1056 case SSL_CTRL_GET_READ_AHEAD:
1057 return ctx->read_ahead;
1058
1059 case SSL_CTRL_SET_READ_AHEAD:
1060 l = ctx->read_ahead;
1061 ctx->read_ahead = larg;
1062 return l;
1063
1064 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1065 ctx->msg_callback_arg = parg;
1066 return 1;
1067
1068 case SSL_CTRL_GET_MAX_CERT_LIST:
1069 return ctx->max_cert_list;
1070
1071 case SSL_CTRL_SET_MAX_CERT_LIST:
1072 l = ctx->max_cert_list;
1073 ctx->max_cert_list = larg;
1074 return l;
1075
1076 case SSL_CTRL_SET_SESS_CACHE_SIZE:
1077 l = ctx->session_cache_size;
1078 ctx->session_cache_size = larg;
1079 return l;
1080
1081 case SSL_CTRL_GET_SESS_CACHE_SIZE:
1082 return ctx->session_cache_size;
1083
1084 case SSL_CTRL_SET_SESS_CACHE_MODE:
1085 l = ctx->session_cache_mode;
1086 ctx->session_cache_mode = larg;
1087 return l;
1088
1089 case SSL_CTRL_GET_SESS_CACHE_MODE:
1090 return ctx->session_cache_mode;
1091
1092 case SSL_CTRL_SESS_NUMBER:
1093 return lh_SSL_SESSION_num_items(ctx->sessions);
1094
1095 case SSL_CTRL_SESS_CONNECT:
Adam Langleyfcf25832014-12-18 17:42:32 -08001096 case SSL_CTRL_SESS_CONNECT_GOOD:
Adam Langleyfcf25832014-12-18 17:42:32 -08001097 case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001098 case SSL_CTRL_SESS_ACCEPT:
Adam Langleyfcf25832014-12-18 17:42:32 -08001099 case SSL_CTRL_SESS_ACCEPT_GOOD:
Adam Langleyfcf25832014-12-18 17:42:32 -08001100 case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001101 case SSL_CTRL_SESS_HIT:
Adam Langleyfcf25832014-12-18 17:42:32 -08001102 case SSL_CTRL_SESS_CB_HIT:
Adam Langleyfcf25832014-12-18 17:42:32 -08001103 case SSL_CTRL_SESS_MISSES:
Adam Langleyfcf25832014-12-18 17:42:32 -08001104 case SSL_CTRL_SESS_TIMEOUTS:
Adam Langleyfcf25832014-12-18 17:42:32 -08001105 case SSL_CTRL_SESS_CACHE_FULL:
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001106 /* Statistics are no longer supported.
1107 * TODO(davidben): Try to remove the accessors altogether. */
1108 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001109
1110 case SSL_CTRL_OPTIONS:
1111 return ctx->options |= larg;
1112
1113 case SSL_CTRL_CLEAR_OPTIONS:
1114 return ctx->options &= ~larg;
1115
1116 case SSL_CTRL_MODE:
1117 return ctx->mode |= larg;
1118
1119 case SSL_CTRL_CLEAR_MODE:
1120 return ctx->mode &= ~larg;
1121
1122 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1123 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH) {
1124 return 0;
1125 }
1126 ctx->max_send_fragment = larg;
1127 return 1;
1128
Adam Langleyfcf25832014-12-18 17:42:32 -08001129 default:
1130 return ctx->method->ssl_ctx_ctrl(ctx, cmd, larg, parg);
1131 }
1132}
1133
1134long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void)) {
1135 switch (cmd) {
1136 case SSL_CTRL_SET_MSG_CALLBACK:
1137 ctx->msg_callback =
1138 (void (*)(int write_p, int version, int content_type, const void *buf,
1139 size_t len, SSL *ssl, void *arg))(fp);
1140 return 1;
1141
1142 default:
1143 return ctx->method->ssl_ctx_callback_ctrl(ctx, cmd, fp);
1144 }
1145}
1146
1147int ssl_cipher_id_cmp(const void *in_a, const void *in_b) {
1148 long l;
1149 const SSL_CIPHER *a = in_a;
1150 const SSL_CIPHER *b = in_b;
1151 const long a_id = a->id;
1152 const long b_id = b->id;
1153
1154 l = a_id - b_id;
1155 if (l == 0L) {
1156 return 0;
1157 } else {
1158 return (l > 0) ? 1 : -1;
1159 }
1160}
1161
1162int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **ap, const SSL_CIPHER **bp) {
1163 long l;
1164 const long a_id = (*ap)->id;
1165 const long b_id = (*bp)->id;
1166
1167 l = a_id - b_id;
1168 if (l == 0) {
1169 return 0;
1170 } else {
1171 return (l > 0) ? 1 : -1;
1172 }
1173}
1174
1175/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001176 * preference */
Adam Langleyfcf25832014-12-18 17:42:32 -08001177STACK_OF(SSL_CIPHER) * SSL_get_ciphers(const SSL *s) {
1178 if (s == NULL) {
1179 return NULL;
1180 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001181
Adam Langleyfcf25832014-12-18 17:42:32 -08001182 if (s->cipher_list != NULL) {
1183 return s->cipher_list->ciphers;
1184 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001185
Adam Langleyfcf25832014-12-18 17:42:32 -08001186 if (s->version >= TLS1_1_VERSION && s->ctx != NULL &&
1187 s->ctx->cipher_list_tls11 != NULL) {
1188 return s->ctx->cipher_list_tls11->ciphers;
1189 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001190
Adam Langleyfcf25832014-12-18 17:42:32 -08001191 if (s->ctx != NULL && s->ctx->cipher_list != NULL) {
1192 return s->ctx->cipher_list->ciphers;
1193 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001194
Adam Langleyfcf25832014-12-18 17:42:32 -08001195 return NULL;
1196}
Adam Langley95c29f32014-06-20 12:00:00 -07001197
Adam Langleyfcf25832014-12-18 17:42:32 -08001198/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001199 * algorithm id */
Adam Langleyfcf25832014-12-18 17:42:32 -08001200STACK_OF(SSL_CIPHER) * ssl_get_ciphers_by_id(SSL *s) {
1201 if (s == NULL) {
1202 return NULL;
1203 }
Adam Langley95c29f32014-06-20 12:00:00 -07001204
Adam Langleyfcf25832014-12-18 17:42:32 -08001205 if (s->cipher_list_by_id != NULL) {
1206 return s->cipher_list_by_id;
1207 }
Adam Langley95c29f32014-06-20 12:00:00 -07001208
Adam Langleyfcf25832014-12-18 17:42:32 -08001209 if (s->ctx != NULL && s->ctx->cipher_list_by_id != NULL) {
1210 return s->ctx->cipher_list_by_id;
1211 }
Adam Langley95c29f32014-06-20 12:00:00 -07001212
Adam Langleyfcf25832014-12-18 17:42:32 -08001213 return NULL;
1214}
Adam Langley95c29f32014-06-20 12:00:00 -07001215
Adam Langleyfcf25832014-12-18 17:42:32 -08001216/* The old interface to get the same thing as SSL_get_ciphers() */
1217const char *SSL_get_cipher_list(const SSL *s, int n) {
1218 const SSL_CIPHER *c;
1219 STACK_OF(SSL_CIPHER) * sk;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001220
Adam Langleyfcf25832014-12-18 17:42:32 -08001221 if (s == NULL) {
1222 return NULL;
1223 }
Adam Langley95c29f32014-06-20 12:00:00 -07001224
Adam Langleyfcf25832014-12-18 17:42:32 -08001225 sk = SSL_get_ciphers(s);
1226 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
1227 return NULL;
1228 }
Adam Langley95c29f32014-06-20 12:00:00 -07001229
Adam Langleyfcf25832014-12-18 17:42:32 -08001230 c = sk_SSL_CIPHER_value(sk, n);
1231 if (c == NULL) {
1232 return NULL;
1233 }
Adam Langley95c29f32014-06-20 12:00:00 -07001234
Adam Langleyfcf25832014-12-18 17:42:32 -08001235 return c->name;
1236}
David Benjamin5491e3f2014-09-29 19:33:09 -04001237
Adam Langleyfcf25832014-12-18 17:42:32 -08001238/* specify the ciphers to be used by default by the SSL_CTX */
1239int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
1240 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001241
Adam Langleyfcf25832014-12-18 17:42:32 -08001242 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001243 &ctx->cipher_list_by_id, str);
Adam Langleyfcf25832014-12-18 17:42:32 -08001244 /* ssl_create_cipher_list may return an empty stack if it was unable to find
1245 * a cipher matching the given rule string (for example if the rule string
1246 * specifies a cipher which has been disabled). This is not an error as far
1247 * as ssl_create_cipher_list is concerned, and hence ctx->cipher_list and
1248 * ctx->cipher_list_by_id has been updated. */
1249 if (sk == NULL) {
1250 return 0;
1251 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1252 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1253 return 0;
1254 }
Adam Langley95c29f32014-06-20 12:00:00 -07001255
Adam Langleyfcf25832014-12-18 17:42:32 -08001256 return 1;
1257}
David Benjamin39482a12014-07-20 13:30:15 -04001258
Adam Langleyfcf25832014-12-18 17:42:32 -08001259int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str) {
1260 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001261
David Benjamin71f07942015-04-08 02:36:59 -04001262 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str);
Adam Langleyfcf25832014-12-18 17:42:32 -08001263 if (sk == NULL) {
1264 return 0;
1265 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1266 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list_tls11,
1267 SSL_R_NO_CIPHER_MATCH);
1268 return 0;
1269 }
David Benjamin9f2c0d72014-10-21 22:00:19 -04001270
Adam Langleyfcf25832014-12-18 17:42:32 -08001271 return 1;
1272}
Adam Langley95c29f32014-06-20 12:00:00 -07001273
Adam Langleyfcf25832014-12-18 17:42:32 -08001274/* specify the ciphers to be used by the SSL */
1275int SSL_set_cipher_list(SSL *s, const char *str) {
1276 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001277
Adam Langleyfcf25832014-12-18 17:42:32 -08001278 sk = ssl_create_cipher_list(s->ctx->method, &s->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001279 &s->cipher_list_by_id, str);
David Benjamin39482a12014-07-20 13:30:15 -04001280
Adam Langleyfcf25832014-12-18 17:42:32 -08001281 /* see comment in SSL_CTX_set_cipher_list */
1282 if (sk == NULL) {
1283 return 0;
1284 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1285 OPENSSL_PUT_ERROR(SSL, SSL_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1286 return 0;
1287 }
David Benjamin39482a12014-07-20 13:30:15 -04001288
Adam Langleyfcf25832014-12-18 17:42:32 -08001289 return 1;
1290}
Adam Langley95c29f32014-06-20 12:00:00 -07001291
Adam Langleyfcf25832014-12-18 17:42:32 -08001292int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, uint8_t *p) {
1293 size_t i;
1294 const SSL_CIPHER *c;
1295 CERT *ct = s->cert;
1296 uint8_t *q;
1297 /* Set disabled masks for this session */
1298 ssl_set_client_disabled(s);
Adam Langley29707792014-06-20 12:00:00 -07001299
Adam Langleyfcf25832014-12-18 17:42:32 -08001300 if (sk == NULL) {
1301 return 0;
1302 }
1303 q = p;
Adam Langley95c29f32014-06-20 12:00:00 -07001304
Adam Langleyfcf25832014-12-18 17:42:32 -08001305 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
1306 c = sk_SSL_CIPHER_value(sk, i);
1307 /* Skip disabled ciphers */
1308 if (c->algorithm_ssl & ct->mask_ssl ||
1309 c->algorithm_mkey & ct->mask_k ||
1310 c->algorithm_auth & ct->mask_a) {
1311 continue;
1312 }
1313 s2n(ssl3_get_cipher_value(c), p);
1314 }
1315
1316 /* If all ciphers were disabled, return the error to the caller. */
1317 if (p == q) {
1318 return 0;
1319 }
1320
1321 /* Add SCSVs. */
1322 if (!s->renegotiate) {
1323 s2n(SSL3_CK_SCSV & 0xffff, p);
1324 }
1325
Adam Langley5f0efe02015-02-20 13:03:16 -08001326 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001327 s2n(SSL3_CK_FALLBACK_SCSV & 0xffff, p);
1328 }
1329
1330 return p - q;
1331}
1332
1333STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs) {
1334 CBS cipher_suites = *cbs;
1335 const SSL_CIPHER *c;
1336 STACK_OF(SSL_CIPHER) * sk;
1337
1338 if (s->s3) {
1339 s->s3->send_connection_binding = 0;
1340 }
1341
1342 if (CBS_len(&cipher_suites) % 2 != 0) {
1343 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1344 SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
1345 return NULL;
1346 }
1347
1348 sk = sk_SSL_CIPHER_new_null();
1349 if (sk == NULL) {
1350 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1351 goto err;
1352 }
1353
1354 if (!CBS_stow(&cipher_suites, &s->cert->ciphers_raw,
1355 &s->cert->ciphers_rawlen)) {
1356 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1357 goto err;
1358 }
1359
1360 while (CBS_len(&cipher_suites) > 0) {
1361 uint16_t cipher_suite;
1362
1363 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
1364 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_INTERNAL_ERROR);
1365 goto err;
1366 }
1367
1368 /* Check for SCSV. */
1369 if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff)) {
1370 /* SCSV is fatal if renegotiating. */
1371 if (s->renegotiate) {
1372 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1373 SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1374 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1375 goto err;
1376 }
1377 s->s3->send_connection_binding = 1;
1378 continue;
1379 }
1380
1381 /* Check for FALLBACK_SCSV. */
1382 if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff)) {
1383 uint16_t max_version = ssl3_get_max_server_version(s);
1384 if (SSL_IS_DTLS(s) ? (uint16_t)s->version > max_version
1385 : (uint16_t)s->version < max_version) {
1386 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1387 SSL_R_INAPPROPRIATE_FALLBACK);
1388 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
1389 goto err;
1390 }
1391 continue;
1392 }
1393
1394 c = ssl3_get_cipher_by_value(cipher_suite);
1395 if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
1396 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1397 goto err;
1398 }
1399 }
1400
1401 return sk;
David Benjamin9f2c0d72014-10-21 22:00:19 -04001402
Adam Langley95c29f32014-06-20 12:00:00 -07001403err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001404 sk_SSL_CIPHER_free(sk);
Adam Langleyfcf25832014-12-18 17:42:32 -08001405 return NULL;
1406}
Adam Langley95c29f32014-06-20 12:00:00 -07001407
1408
Adam Langleyfcf25832014-12-18 17:42:32 -08001409/* return a servername extension value if provided in Client Hello, or NULL. So
1410 * far, only host_name types are defined (RFC 3546). */
1411const char *SSL_get_servername(const SSL *s, const int type) {
1412 if (type != TLSEXT_NAMETYPE_host_name) {
1413 return NULL;
1414 }
Adam Langley95c29f32014-06-20 12:00:00 -07001415
Adam Langleyfcf25832014-12-18 17:42:32 -08001416 return s->session && !s->tlsext_hostname ? s->session->tlsext_hostname
1417 : s->tlsext_hostname;
1418}
Adam Langley95c29f32014-06-20 12:00:00 -07001419
Adam Langleyfcf25832014-12-18 17:42:32 -08001420int SSL_get_servername_type(const SSL *s) {
1421 if (s->session &&
1422 (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname)) {
1423 return TLSEXT_NAMETYPE_host_name;
1424 }
Adam Langley95c29f32014-06-20 12:00:00 -07001425
Adam Langleyfcf25832014-12-18 17:42:32 -08001426 return -1;
1427}
Adam Langley95c29f32014-06-20 12:00:00 -07001428
Adam Langleyfcf25832014-12-18 17:42:32 -08001429void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
1430 ctx->signed_cert_timestamps_enabled = 1;
1431}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001432
Adam Langleyfcf25832014-12-18 17:42:32 -08001433int SSL_enable_signed_cert_timestamps(SSL *ssl) {
1434 ssl->signed_cert_timestamps_enabled = 1;
1435 return 1;
1436}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001437
Adam Langleyfcf25832014-12-18 17:42:32 -08001438void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
1439 ctx->ocsp_stapling_enabled = 1;
1440}
David Benjamin6c7aed02014-08-27 16:42:38 -04001441
Adam Langleyfcf25832014-12-18 17:42:32 -08001442int SSL_enable_ocsp_stapling(SSL *ssl) {
1443 ssl->ocsp_stapling_enabled = 1;
1444 return 1;
1445}
David Benjamin6c7aed02014-08-27 16:42:38 -04001446
Adam Langleyfcf25832014-12-18 17:42:32 -08001447void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
1448 size_t *out_len) {
1449 SSL_SESSION *session = ssl->session;
Adam Langley3cb50e02014-08-26 14:00:31 -07001450
Adam Langleyfcf25832014-12-18 17:42:32 -08001451 *out_len = 0;
1452 *out = NULL;
1453 if (ssl->server || !session || !session->tlsext_signed_cert_timestamp_list) {
1454 return;
1455 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001456
Adam Langleyfcf25832014-12-18 17:42:32 -08001457 *out = session->tlsext_signed_cert_timestamp_list;
1458 *out_len = session->tlsext_signed_cert_timestamp_list_length;
1459}
David Benjamin6c7aed02014-08-27 16:42:38 -04001460
Adam Langleyfcf25832014-12-18 17:42:32 -08001461void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
1462 size_t *out_len) {
1463 SSL_SESSION *session = ssl->session;
1464
1465 *out_len = 0;
1466 *out = NULL;
1467 if (ssl->server || !session || !session->ocsp_response) {
1468 return;
1469 }
1470 *out = session->ocsp_response;
1471 *out_len = session->ocsp_response_length;
1472}
David Benjamin6c7aed02014-08-27 16:42:38 -04001473
Adam Langley95c29f32014-06-20 12:00:00 -07001474/* SSL_select_next_proto implements the standard protocol selection. It is
1475 * expected that this function is called from the callback set by
1476 * SSL_CTX_set_next_proto_select_cb.
1477 *
1478 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1479 * strings. The length byte itself is not included in the length. A byte
1480 * string of length 0 is invalid. No byte string may be truncated.
1481 *
1482 * The current, but experimental algorithm for selecting the protocol is:
1483 *
1484 * 1) If the server doesn't support NPN then this is indicated to the
1485 * callback. In this case, the client application has to abort the connection
1486 * or have a default application level protocol.
1487 *
1488 * 2) If the server supports NPN, but advertises an empty list then the
1489 * client selects the first protcol in its list, but indicates via the
1490 * API that this fallback case was enacted.
1491 *
1492 * 3) Otherwise, the client finds the first protocol in the server's list
1493 * that it supports and selects this protocol. This is because it's
1494 * assumed that the server has better information about which protocol
1495 * a client should use.
1496 *
1497 * 4) If the client doesn't support any of the server's advertised
1498 * protocols, then this is treated the same as case 2.
1499 *
1500 * It returns either
1501 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1502 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1503 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001504int SSL_select_next_proto(uint8_t **out, uint8_t *outlen, const uint8_t *server,
1505 unsigned int server_len, const uint8_t *client,
1506 unsigned int client_len) {
1507 unsigned int i, j;
1508 const uint8_t *result;
1509 int status = OPENSSL_NPN_UNSUPPORTED;
Adam Langley95c29f32014-06-20 12:00:00 -07001510
Adam Langleyfcf25832014-12-18 17:42:32 -08001511 /* For each protocol in server preference order, see if we support it. */
1512 for (i = 0; i < server_len;) {
1513 for (j = 0; j < client_len;) {
1514 if (server[i] == client[j] &&
1515 memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
1516 /* We found a match */
1517 result = &server[i];
1518 status = OPENSSL_NPN_NEGOTIATED;
1519 goto found;
1520 }
1521 j += client[j];
1522 j++;
1523 }
1524 i += server[i];
1525 i++;
1526 }
Adam Langley95c29f32014-06-20 12:00:00 -07001527
Adam Langleyfcf25832014-12-18 17:42:32 -08001528 /* There's no overlap between our protocols and the server's list. */
1529 result = client;
1530 status = OPENSSL_NPN_NO_OVERLAP;
Adam Langley95c29f32014-06-20 12:00:00 -07001531
Adam Langleyfcf25832014-12-18 17:42:32 -08001532found:
1533 *out = (uint8_t *)result + 1;
1534 *outlen = result[0];
1535 return status;
1536}
Adam Langley95c29f32014-06-20 12:00:00 -07001537
Adam Langley95c29f32014-06-20 12:00:00 -07001538/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
1539 * requested protocol for this connection and returns 0. If the client didn't
1540 * request any protocol, then *data is set to NULL.
1541 *
1542 * Note that the client can request any protocol it chooses. The value returned
1543 * from this function need not be a member of the list of supported protocols
Adam Langleyfcf25832014-12-18 17:42:32 -08001544 * provided by the callback. */
1545void SSL_get0_next_proto_negotiated(const SSL *s, const uint8_t **data,
1546 unsigned *len) {
1547 *data = s->next_proto_negotiated;
1548 if (!*data) {
1549 *len = 0;
1550 } else {
1551 *len = s->next_proto_negotiated_len;
1552 }
Adam Langley95c29f32014-06-20 12:00:00 -07001553}
1554
1555/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
1556 * TLS server needs a list of supported protocols for Next Protocol
1557 * Negotiation. The returned list must be in wire format. The list is returned
1558 * by setting |out| to point to it and |outlen| to its length. This memory will
1559 * not be modified, but one should assume that the SSL* keeps a reference to
1560 * it.
1561 *
Adam Langleyfcf25832014-12-18 17:42:32 -08001562 * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise.
1563 * Otherwise, no such extension will be included in the ServerHello. */
1564void SSL_CTX_set_next_protos_advertised_cb(
1565 SSL_CTX *ctx,
1566 int (*cb)(SSL *ssl, const uint8_t **out, unsigned int *outlen, void *arg),
1567 void *arg) {
1568 ctx->next_protos_advertised_cb = cb;
1569 ctx->next_protos_advertised_cb_arg = arg;
1570}
Adam Langley95c29f32014-06-20 12:00:00 -07001571
1572/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1573 * client needs to select a protocol from the server's provided list. |out|
1574 * must be set to point to the selected protocol (which may be within |in|).
1575 * The length of the protocol name must be written into |outlen|. The server's
1576 * advertised protocols are provided in |in| and |inlen|. The callback can
1577 * assume that |in| is syntactically valid.
1578 *
1579 * The client must select a protocol. It is fatal to the connection if this
1580 * callback returns a value other than SSL_TLSEXT_ERR_OK.
1581 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001582void SSL_CTX_set_next_proto_select_cb(
1583 SSL_CTX *ctx, int (*cb)(SSL *s, uint8_t **out, uint8_t *outlen,
1584 const uint8_t *in, unsigned int inlen, void *arg),
1585 void *arg) {
1586 ctx->next_proto_select_cb = cb;
1587 ctx->next_proto_select_cb_arg = arg;
1588}
Adam Langley95c29f32014-06-20 12:00:00 -07001589
Adam Langleyfcf25832014-12-18 17:42:32 -08001590int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
1591 unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001592 OPENSSL_free(ctx->alpn_client_proto_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001593 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1594 if (!ctx->alpn_client_proto_list) {
1595 return 1;
1596 }
1597 ctx->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001598
Adam Langleyfcf25832014-12-18 17:42:32 -08001599 return 0;
1600}
Adam Langley95c29f32014-06-20 12:00:00 -07001601
Adam Langleyfcf25832014-12-18 17:42:32 -08001602int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001603 OPENSSL_free(ssl->alpn_client_proto_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001604 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1605 if (!ssl->alpn_client_proto_list) {
1606 return 1;
1607 }
1608 ssl->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001609
Adam Langleyfcf25832014-12-18 17:42:32 -08001610 return 0;
1611}
Adam Langley95c29f32014-06-20 12:00:00 -07001612
1613/* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
1614 * during ClientHello processing in order to select an ALPN protocol from the
1615 * client's list of offered protocols. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001616void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
1617 int (*cb)(SSL *ssl, const uint8_t **out,
1618 uint8_t *outlen, const uint8_t *in,
1619 unsigned int inlen, void *arg),
1620 void *arg) {
1621 ctx->alpn_select_cb = cb;
1622 ctx->alpn_select_cb_arg = arg;
1623}
Adam Langley95c29f32014-06-20 12:00:00 -07001624
1625/* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
1626 * On return it sets |*data| to point to |*len| bytes of protocol name (not
1627 * including the leading length-prefix byte). If the server didn't respond with
1628 * a negotiated protocol then |*len| will be zero. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001629void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **data,
1630 unsigned *len) {
1631 *data = NULL;
1632 if (ssl->s3) {
1633 *data = ssl->s3->alpn_selected;
1634 }
1635 if (*data == NULL) {
1636 *len = 0;
1637 } else {
1638 *len = ssl->s3->alpn_selected_len;
1639 }
1640}
Adam Langley95c29f32014-06-20 12:00:00 -07001641
David Benjamincfd248b2015-04-03 11:02:24 -04001642int SSL_export_keying_material(SSL *s, uint8_t *out, size_t out_len,
1643 const char *label, size_t label_len,
1644 const uint8_t *context, size_t context_len,
1645 int use_context) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001646 if (s->version < TLS1_VERSION) {
David Benjaminc565ebb2015-04-03 04:06:36 -04001647 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001648 }
Adam Langley95c29f32014-06-20 12:00:00 -07001649
David Benjamincfd248b2015-04-03 11:02:24 -04001650 return s->enc_method->export_keying_material(
1651 s, out, out_len, label, label_len, context, context_len, use_context);
Adam Langleyfcf25832014-12-18 17:42:32 -08001652}
Adam Langley95c29f32014-06-20 12:00:00 -07001653
Adam Langleyfcf25832014-12-18 17:42:32 -08001654static uint32_t ssl_session_hash(const SSL_SESSION *a) {
1655 uint32_t hash =
1656 ((uint32_t)a->session_id[0]) ||
1657 ((uint32_t)a->session_id[1] << 8) ||
1658 ((uint32_t)a->session_id[2] << 16) ||
1659 ((uint32_t)a->session_id[3] << 24);
Adam Langley95c29f32014-06-20 12:00:00 -07001660
Adam Langleyfcf25832014-12-18 17:42:32 -08001661 return hash;
1662}
Adam Langley95c29f32014-06-20 12:00:00 -07001663
1664/* NB: If this function (or indeed the hash function which uses a sort of
1665 * coarser function than this one) is changed, ensure
1666 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1667 * able to construct an SSL_SESSION that will collide with any existing session
1668 * with a matching session ID. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001669static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
1670 if (a->ssl_version != b->ssl_version) {
1671 return 1;
1672 }
Adam Langley95c29f32014-06-20 12:00:00 -07001673
Adam Langleyfcf25832014-12-18 17:42:32 -08001674 if (a->session_id_length != b->session_id_length) {
1675 return 1;
1676 }
Adam Langley95c29f32014-06-20 12:00:00 -07001677
Adam Langleyfcf25832014-12-18 17:42:32 -08001678 return memcmp(a->session_id, b->session_id, a->session_id_length);
1679}
Adam Langley95c29f32014-06-20 12:00:00 -07001680
Adam Langleyfcf25832014-12-18 17:42:32 -08001681SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth) {
1682 SSL_CTX *ret = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001683
Adam Langleyfcf25832014-12-18 17:42:32 -08001684 if (meth == NULL) {
1685 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_NULL_SSL_METHOD_PASSED);
1686 return NULL;
1687 }
Adam Langley95c29f32014-06-20 12:00:00 -07001688
Adam Langleyfcf25832014-12-18 17:42:32 -08001689 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
1690 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
1691 goto err;
1692 }
Adam Langley95c29f32014-06-20 12:00:00 -07001693
Adam Langleyfcf25832014-12-18 17:42:32 -08001694 ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1695 if (ret == NULL) {
1696 goto err;
1697 }
Adam Langley95c29f32014-06-20 12:00:00 -07001698
Adam Langleyfcf25832014-12-18 17:42:32 -08001699 memset(ret, 0, sizeof(SSL_CTX));
Adam Langley95c29f32014-06-20 12:00:00 -07001700
Adam Langleyfcf25832014-12-18 17:42:32 -08001701 ret->method = meth->method;
Adam Langley95c29f32014-06-20 12:00:00 -07001702
Adam Langleyfcf25832014-12-18 17:42:32 -08001703 ret->cert_store = NULL;
1704 ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
1705 ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1706 ret->session_cache_head = NULL;
1707 ret->session_cache_tail = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001708
Adam Langleyfcf25832014-12-18 17:42:32 -08001709 /* We take the system default */
1710 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001711
Adam Langleyfcf25832014-12-18 17:42:32 -08001712 ret->new_session_cb = 0;
1713 ret->remove_session_cb = 0;
1714 ret->get_session_cb = 0;
1715 ret->generate_session_id = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001716
Adam Langleyfcf25832014-12-18 17:42:32 -08001717 ret->references = 1;
1718 ret->quiet_shutdown = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001719
Adam Langleyfcf25832014-12-18 17:42:32 -08001720 ret->info_callback = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001721
Adam Langleyfcf25832014-12-18 17:42:32 -08001722 ret->app_verify_callback = 0;
1723 ret->app_verify_arg = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001724
Adam Langleyfcf25832014-12-18 17:42:32 -08001725 ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
1726 ret->read_ahead = 0;
1727 ret->msg_callback = 0;
1728 ret->msg_callback_arg = NULL;
1729 ret->verify_mode = SSL_VERIFY_NONE;
1730 ret->sid_ctx_length = 0;
1731 ret->default_verify_callback = NULL;
1732 ret->cert = ssl_cert_new();
1733 if (ret->cert == NULL) {
1734 goto err;
1735 }
Adam Langley95c29f32014-06-20 12:00:00 -07001736
Adam Langleyfcf25832014-12-18 17:42:32 -08001737 ret->default_passwd_callback = 0;
1738 ret->default_passwd_callback_userdata = NULL;
1739 ret->client_cert_cb = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001740
Adam Langleyfcf25832014-12-18 17:42:32 -08001741 ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
1742 if (ret->sessions == NULL) {
1743 goto err;
1744 }
1745 ret->cert_store = X509_STORE_new();
1746 if (ret->cert_store == NULL) {
1747 goto err;
1748 }
Adam Langley95c29f32014-06-20 12:00:00 -07001749
Adam Langleyfcf25832014-12-18 17:42:32 -08001750 ssl_create_cipher_list(ret->method, &ret->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001751 &ret->cipher_list_by_id, SSL_DEFAULT_CIPHER_LIST);
Adam Langleyfcf25832014-12-18 17:42:32 -08001752 if (ret->cipher_list == NULL ||
1753 sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0) {
1754 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_LIBRARY_HAS_NO_CIPHERS);
1755 goto err2;
1756 }
Adam Langley95c29f32014-06-20 12:00:00 -07001757
Adam Langleyfcf25832014-12-18 17:42:32 -08001758 ret->param = X509_VERIFY_PARAM_new();
1759 if (!ret->param) {
1760 goto err;
1761 }
Adam Langley95c29f32014-06-20 12:00:00 -07001762
Adam Langleyfcf25832014-12-18 17:42:32 -08001763 ret->client_CA = sk_X509_NAME_new_null();
1764 if (ret->client_CA == NULL) {
1765 goto err;
1766 }
Adam Langley95c29f32014-06-20 12:00:00 -07001767
David Benjamin9f33fc62015-04-15 17:29:53 -04001768 CRYPTO_new_ex_data(&g_ex_data_class_ssl_ctx, ret, &ret->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001769
Adam Langleyfcf25832014-12-18 17:42:32 -08001770 ret->extra_certs = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001771
Adam Langleyfcf25832014-12-18 17:42:32 -08001772 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
David Benjamin422d3a42014-08-20 11:09:03 -04001773
Adam Langleyfcf25832014-12-18 17:42:32 -08001774 ret->tlsext_servername_callback = 0;
1775 ret->tlsext_servername_arg = NULL;
1776 /* Setup RFC4507 ticket keys */
1777 if (!RAND_bytes(ret->tlsext_tick_key_name, 16) ||
1778 !RAND_bytes(ret->tlsext_tick_hmac_key, 16) ||
1779 !RAND_bytes(ret->tlsext_tick_aes_key, 16)) {
1780 ret->options |= SSL_OP_NO_TICKET;
1781 }
Adam Langley95c29f32014-06-20 12:00:00 -07001782
Adam Langleyfcf25832014-12-18 17:42:32 -08001783 ret->next_protos_advertised_cb = 0;
1784 ret->next_proto_select_cb = 0;
1785 ret->psk_identity_hint = NULL;
1786 ret->psk_client_callback = NULL;
1787 ret->psk_server_callback = NULL;
1788
1789 /* Default is to connect to non-RI servers. When RI is more widely deployed
1790 * might change this. */
1791 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
1792
1793 /* Lock the SSL_CTX to the specified version, for compatibility with legacy
1794 * uses of SSL_METHOD. */
1795 if (meth->version != 0) {
1796 SSL_CTX_set_max_version(ret, meth->version);
1797 SSL_CTX_set_min_version(ret, meth->version);
1798 }
1799
1800 return ret;
1801
Adam Langley95c29f32014-06-20 12:00:00 -07001802err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001803 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -07001804err2:
David Benjamin2755a3e2015-04-22 16:17:58 -04001805 SSL_CTX_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -08001806 return NULL;
1807}
Adam Langley95c29f32014-06-20 12:00:00 -07001808
David Benjamin4fcc2e22015-04-22 12:58:16 -04001809void SSL_CTX_free(SSL_CTX *ctx) {
1810 if (ctx == NULL ||
1811 CRYPTO_add(&ctx->references, -1, CRYPTO_LOCK_SSL_CTX) > 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001812 return;
1813 }
Adam Langley95c29f32014-06-20 12:00:00 -07001814
David Benjamin2755a3e2015-04-22 16:17:58 -04001815 X509_VERIFY_PARAM_free(ctx->param);
Adam Langley95c29f32014-06-20 12:00:00 -07001816
Adam Langleyfcf25832014-12-18 17:42:32 -08001817 /* Free internal session cache. However: the remove_cb() may reference the
1818 * ex_data of SSL_CTX, thus the ex_data store can only be removed after the
1819 * sessions were flushed. As the ex_data handling routines might also touch
1820 * the session cache, the most secure solution seems to be: empty (flush) the
1821 * cache, then free ex_data, then finally free the cache. (See ticket
1822 * [openssl.org #212].) */
David Benjamin2755a3e2015-04-22 16:17:58 -04001823 SSL_CTX_flush_sessions(ctx, 0);
Adam Langley95c29f32014-06-20 12:00:00 -07001824
David Benjamin4fcc2e22015-04-22 12:58:16 -04001825 CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, ctx, &ctx->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001826
David Benjamin2755a3e2015-04-22 16:17:58 -04001827 lh_SSL_SESSION_free(ctx->sessions);
1828 X509_STORE_free(ctx->cert_store);
1829 ssl_cipher_preference_list_free(ctx->cipher_list);
1830 sk_SSL_CIPHER_free(ctx->cipher_list_by_id);
1831 ssl_cipher_preference_list_free(ctx->cipher_list_tls11);
1832 ssl_cert_free(ctx->cert);
1833 sk_X509_NAME_pop_free(ctx->client_CA, X509_NAME_free);
1834 sk_X509_pop_free(ctx->extra_certs, X509_free);
1835 sk_SRTP_PROTECTION_PROFILE_free(ctx->srtp_profiles);
1836 OPENSSL_free(ctx->psk_identity_hint);
1837 OPENSSL_free(ctx->tlsext_ecpointformatlist);
1838 OPENSSL_free(ctx->tlsext_ellipticcurvelist);
1839 OPENSSL_free(ctx->alpn_client_proto_list);
1840 EVP_PKEY_free(ctx->tlsext_channel_id_private);
1841 BIO_free(ctx->keylog_bio);
Adam Langley95c29f32014-06-20 12:00:00 -07001842
David Benjamin4fcc2e22015-04-22 12:58:16 -04001843 OPENSSL_free(ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08001844}
Adam Langley95c29f32014-06-20 12:00:00 -07001845
Adam Langleyfcf25832014-12-18 17:42:32 -08001846void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) {
1847 ctx->default_passwd_callback = cb;
1848}
Adam Langley95c29f32014-06-20 12:00:00 -07001849
Adam Langleyfcf25832014-12-18 17:42:32 -08001850void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u) {
1851 ctx->default_passwd_callback_userdata = u;
1852}
Adam Langley95c29f32014-06-20 12:00:00 -07001853
Adam Langleyfcf25832014-12-18 17:42:32 -08001854void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
1855 int (*cb)(X509_STORE_CTX *, void *),
1856 void *arg) {
1857 ctx->app_verify_callback = cb;
1858 ctx->app_verify_arg = arg;
1859}
Adam Langley95c29f32014-06-20 12:00:00 -07001860
Adam Langleyfcf25832014-12-18 17:42:32 -08001861void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
1862 int (*cb)(int, X509_STORE_CTX *)) {
1863 ctx->verify_mode = mode;
1864 ctx->default_verify_callback = cb;
1865}
Adam Langley95c29f32014-06-20 12:00:00 -07001866
Adam Langleyfcf25832014-12-18 17:42:32 -08001867void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
1868 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
1869}
Adam Langley1258b6a2014-06-20 12:00:00 -07001870
Adam Langleyfcf25832014-12-18 17:42:32 -08001871void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb)(SSL *ssl, void *arg),
1872 void *arg) {
1873 ssl_cert_set_cert_cb(c->cert, cb, arg);
1874}
David Benjamin859ec3c2014-09-02 16:29:36 -04001875
Adam Langleyfcf25832014-12-18 17:42:32 -08001876void SSL_set_cert_cb(SSL *s, int (*cb)(SSL *ssl, void *arg), void *arg) {
1877 ssl_cert_set_cert_cb(s->cert, cb, arg);
1878}
Adam Langley95c29f32014-06-20 12:00:00 -07001879
Adam Langleyfcf25832014-12-18 17:42:32 -08001880static int ssl_has_key(SSL *s, size_t idx) {
1881 CERT_PKEY *cpk = &s->cert->pkeys[idx];
1882 return cpk->x509 && cpk->privatekey;
1883}
David Benjamin033e5f42014-11-13 18:47:41 -05001884
David Benjamin107db582015-04-08 00:41:59 -04001885void ssl_get_compatible_server_ciphers(SSL *s, uint32_t *out_mask_k,
1886 uint32_t *out_mask_a) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001887 CERT *c = s->cert;
1888 int rsa_enc, rsa_sign, dh_tmp;
David Benjamin107db582015-04-08 00:41:59 -04001889 uint32_t mask_k, mask_a;
Adam Langleyfcf25832014-12-18 17:42:32 -08001890 int have_ecc_cert, ecdsa_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001891 X509 *x;
Adam Langley95c29f32014-06-20 12:00:00 -07001892
Adam Langleyfcf25832014-12-18 17:42:32 -08001893 if (c == NULL) {
1894 /* TODO(davidben): Is this codepath possible? */
1895 *out_mask_k = 0;
1896 *out_mask_a = 0;
1897 return;
1898 }
Adam Langley95c29f32014-06-20 12:00:00 -07001899
Adam Langleyfcf25832014-12-18 17:42:32 -08001900 dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001901
Adam Langleyfcf25832014-12-18 17:42:32 -08001902 rsa_enc = ssl_has_key(s, SSL_PKEY_RSA_ENC);
1903 rsa_sign = ssl_has_key(s, SSL_PKEY_RSA_SIGN);
1904 have_ecc_cert = ssl_has_key(s, SSL_PKEY_ECC);
1905 mask_k = 0;
1906 mask_a = 0;
David Benjaminf31e6812014-11-13 18:05:55 -05001907
Adam Langleyfcf25832014-12-18 17:42:32 -08001908 if (rsa_enc) {
1909 mask_k |= SSL_kRSA;
1910 }
1911 if (dh_tmp) {
David Benjamin7061e282015-03-19 11:10:48 -04001912 mask_k |= SSL_kDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001913 }
1914 if (rsa_enc || rsa_sign) {
1915 mask_a |= SSL_aRSA;
1916 }
Adam Langley95c29f32014-06-20 12:00:00 -07001917
Adam Langleyfcf25832014-12-18 17:42:32 -08001918 /* An ECC certificate may be usable for ECDSA cipher suites depending on the
1919 * key usage extension and on the client's curve preferences. */
1920 if (have_ecc_cert) {
1921 x = c->pkeys[SSL_PKEY_ECC].x509;
1922 /* This call populates extension flags (ex_flags). */
1923 X509_check_purpose(x, -1, 0);
1924 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE)
1925 ? (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)
1926 : 1;
1927 if (!tls1_check_ec_cert(s, x)) {
1928 ecdsa_ok = 0;
1929 }
1930 if (ecdsa_ok) {
1931 mask_a |= SSL_aECDSA;
1932 }
1933 }
Adam Langley95c29f32014-06-20 12:00:00 -07001934
Adam Langleyfcf25832014-12-18 17:42:32 -08001935 /* If we are considering an ECC cipher suite that uses an ephemeral EC
1936 * key, check it. */
David Benjamindd978782015-04-24 15:20:13 -04001937 if (tls1_check_ec_tmp_key(s)) {
David Benjamin7061e282015-03-19 11:10:48 -04001938 mask_k |= SSL_kECDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001939 }
Adam Langley95c29f32014-06-20 12:00:00 -07001940
Adam Langleyfcf25832014-12-18 17:42:32 -08001941 /* PSK requires a server callback. */
1942 if (s->psk_server_callback != NULL) {
1943 mask_k |= SSL_kPSK;
1944 mask_a |= SSL_aPSK;
1945 }
Adam Langley95c29f32014-06-20 12:00:00 -07001946
Adam Langleyfcf25832014-12-18 17:42:32 -08001947 *out_mask_k = mask_k;
1948 *out_mask_a = mask_a;
1949}
Adam Langley95c29f32014-06-20 12:00:00 -07001950
1951/* This handy macro borrowed from crypto/x509v3/v3_purp.c */
1952#define ku_reject(x, usage) \
Adam Langleyfcf25832014-12-18 17:42:32 -08001953 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
Adam Langley95c29f32014-06-20 12:00:00 -07001954
Adam Langleyfcf25832014-12-18 17:42:32 -08001955int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001956 const SSL_CIPHER *cs = s->s3->tmp.new_cipher;
David Benjamin3fa27772015-04-17 22:32:19 -04001957 uint32_t alg_a = cs->algorithm_auth;
1958 int signature_nid = 0, md_nid = 0, pk_nid = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001959
Adam Langleyfcf25832014-12-18 17:42:32 -08001960 /* This call populates the ex_flags field correctly */
1961 X509_check_purpose(x, -1, 0);
1962 if (x->sig_alg && x->sig_alg->algorithm) {
1963 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
1964 OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
1965 }
1966 if (alg_a & SSL_aECDSA) {
1967 /* key usage, if present, must allow signing */
1968 if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE)) {
1969 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg,
1970 SSL_R_ECC_CERT_NOT_FOR_SIGNING);
1971 return 0;
1972 }
1973 }
Adam Langley95c29f32014-06-20 12:00:00 -07001974
Adam Langleyfcf25832014-12-18 17:42:32 -08001975 return 1; /* all checks are ok */
1976}
Adam Langley95c29f32014-06-20 12:00:00 -07001977
Adam Langleyfcf25832014-12-18 17:42:32 -08001978static int ssl_get_server_cert_index(const SSL *s) {
1979 int idx;
1980 idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1981 if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509) {
1982 idx = SSL_PKEY_RSA_SIGN;
1983 }
1984 if (idx == -1) {
1985 OPENSSL_PUT_ERROR(SSL, ssl_get_server_cert_index, ERR_R_INTERNAL_ERROR);
1986 }
1987 return idx;
1988}
Adam Langley95c29f32014-06-20 12:00:00 -07001989
Adam Langleyfcf25832014-12-18 17:42:32 -08001990CERT_PKEY *ssl_get_server_send_pkey(const SSL *s) {
1991 int i = ssl_get_server_cert_index(s);
Adam Langley95c29f32014-06-20 12:00:00 -07001992
Adam Langleyfcf25832014-12-18 17:42:32 -08001993 /* This may or may not be an error. */
1994 if (i < 0) {
1995 return NULL;
1996 }
Adam Langley95c29f32014-06-20 12:00:00 -07001997
Adam Langleyfcf25832014-12-18 17:42:32 -08001998 /* May be NULL. */
1999 return &s->cert->pkeys[i];
2000}
Adam Langley95c29f32014-06-20 12:00:00 -07002001
Adam Langleyfcf25832014-12-18 17:42:32 -08002002EVP_PKEY *ssl_get_sign_pkey(SSL *s, const SSL_CIPHER *cipher) {
David Benjamin3fa27772015-04-17 22:32:19 -04002003 uint32_t alg_a = cipher->algorithm_auth;
2004 CERT *c = s->cert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002005 int idx = -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002006
Adam Langleyfcf25832014-12-18 17:42:32 -08002007 if (alg_a & SSL_aRSA) {
2008 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL) {
2009 idx = SSL_PKEY_RSA_SIGN;
2010 } else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL) {
2011 idx = SSL_PKEY_RSA_ENC;
2012 }
2013 } else if ((alg_a & SSL_aECDSA) &&
2014 (c->pkeys[SSL_PKEY_ECC].privatekey != NULL)) {
2015 idx = SSL_PKEY_ECC;
2016 }
Adam Langley95c29f32014-06-20 12:00:00 -07002017
Adam Langleyfcf25832014-12-18 17:42:32 -08002018 if (idx == -1) {
2019 OPENSSL_PUT_ERROR(SSL, ssl_get_sign_pkey, ERR_R_INTERNAL_ERROR);
2020 return NULL;
2021 }
Adam Langley95c29f32014-06-20 12:00:00 -07002022
Adam Langleyfcf25832014-12-18 17:42:32 -08002023 return c->pkeys[idx].privatekey;
2024}
Adam Langley95c29f32014-06-20 12:00:00 -07002025
Adam Langleyfcf25832014-12-18 17:42:32 -08002026void ssl_update_cache(SSL *s, int mode) {
David Benjaminb6d0c6d2015-03-19 19:07:26 -04002027 /* Never cache sessions with empty session IDs. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002028 if (s->session->session_id_length == 0) {
2029 return;
2030 }
Adam Langley95c29f32014-06-20 12:00:00 -07002031
David Benjaminb6d0c6d2015-03-19 19:07:26 -04002032 SSL_CTX *ctx = s->initial_ctx;
2033 if ((ctx->session_cache_mode & mode) == mode && !s->hit &&
2034 ((ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) ||
2035 SSL_CTX_add_session(ctx, s->session)) &&
2036 ctx->new_session_cb != NULL) {
2037 /* Note: |new_session_cb| is called whether the internal session cache is
2038 * used or not. */
2039 if (!ctx->new_session_cb(s, SSL_SESSION_up_ref(s->session))) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002040 SSL_SESSION_free(s->session);
2041 }
2042 }
Adam Langley95c29f32014-06-20 12:00:00 -07002043
David Benjaminb6d0c6d2015-03-19 19:07:26 -04002044 if (!(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR) &&
2045 !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) &&
2046 (ctx->session_cache_mode & mode) == mode) {
2047 /* Automatically flush the internal session cache every 255 connections. */
2048 int flush_cache = 0;
2049 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2050 ctx->handshakes_since_cache_flush++;
2051 if (ctx->handshakes_since_cache_flush >= 255) {
2052 flush_cache = 1;
2053 ctx->handshakes_since_cache_flush = 0;
2054 }
2055 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
2056
2057 if (flush_cache) {
2058 SSL_CTX_flush_sessions(ctx, (unsigned long)time(NULL));
Adam Langleyfcf25832014-12-18 17:42:32 -08002059 }
2060 }
2061}
Adam Langley95c29f32014-06-20 12:00:00 -07002062
David Benjamin1a5c50f2015-03-11 16:22:37 -04002063int SSL_get_error(const SSL *s, int ret_code) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002064 int reason;
David Benjamin1a5c50f2015-03-11 16:22:37 -04002065 uint32_t err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002066 BIO *bio;
Adam Langley95c29f32014-06-20 12:00:00 -07002067
David Benjamin1a5c50f2015-03-11 16:22:37 -04002068 if (ret_code > 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002069 return SSL_ERROR_NONE;
2070 }
Adam Langley95c29f32014-06-20 12:00:00 -07002071
Adam Langleyfcf25832014-12-18 17:42:32 -08002072 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
2073 * where we do encode the error */
David Benjamin1a5c50f2015-03-11 16:22:37 -04002074 err = ERR_peek_error();
2075 if (err != 0) {
2076 if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002077 return SSL_ERROR_SYSCALL;
2078 }
2079 return SSL_ERROR_SSL;
2080 }
Adam Langley95c29f32014-06-20 12:00:00 -07002081
David Benjamin1a5c50f2015-03-11 16:22:37 -04002082 if (ret_code == 0) {
David Benjamin9a38e922015-01-22 16:06:11 -05002083 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2084 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) {
2085 /* The socket was cleanly shut down with a close_notify. */
2086 return SSL_ERROR_ZERO_RETURN;
2087 }
2088 /* An EOF was observed which violates the protocol, and the underlying
2089 * transport does not participate in the error queue. Bubble up to the
2090 * caller. */
2091 return SSL_ERROR_SYSCALL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002092 }
Adam Langleyb2ce0582014-06-20 12:00:00 -07002093
Adam Langleyfcf25832014-12-18 17:42:32 -08002094 if (SSL_want_session(s)) {
2095 return SSL_ERROR_PENDING_SESSION;
2096 }
Adam Langley95c29f32014-06-20 12:00:00 -07002097
Adam Langleyfcf25832014-12-18 17:42:32 -08002098 if (SSL_want_certificate(s)) {
2099 return SSL_ERROR_PENDING_CERTIFICATE;
2100 }
Adam Langley95c29f32014-06-20 12:00:00 -07002101
Adam Langleyfcf25832014-12-18 17:42:32 -08002102 if (SSL_want_read(s)) {
2103 bio = SSL_get_rbio(s);
2104 if (BIO_should_read(bio)) {
2105 return SSL_ERROR_WANT_READ;
2106 }
Adam Langley95c29f32014-06-20 12:00:00 -07002107
Adam Langleyfcf25832014-12-18 17:42:32 -08002108 if (BIO_should_write(bio)) {
2109 /* This one doesn't make too much sense ... We never try to write to the
2110 * rbio, and an application program where rbio and wbio are separate
2111 * couldn't even know what it should wait for. However if we ever set
2112 * s->rwstate incorrectly (so that we have SSL_want_read(s) instead of
2113 * SSL_want_write(s)) and rbio and wbio *are* the same, this test works
2114 * around that bug; so it might be safer to keep it. */
2115 return SSL_ERROR_WANT_WRITE;
2116 }
Adam Langley95c29f32014-06-20 12:00:00 -07002117
Adam Langleyfcf25832014-12-18 17:42:32 -08002118 if (BIO_should_io_special(bio)) {
2119 reason = BIO_get_retry_reason(bio);
2120 if (reason == BIO_RR_CONNECT) {
2121 return SSL_ERROR_WANT_CONNECT;
2122 }
Adam Langley95c29f32014-06-20 12:00:00 -07002123
Adam Langleyfcf25832014-12-18 17:42:32 -08002124 if (reason == BIO_RR_ACCEPT) {
2125 return SSL_ERROR_WANT_ACCEPT;
2126 }
Adam Langley95c29f32014-06-20 12:00:00 -07002127
Adam Langleyfcf25832014-12-18 17:42:32 -08002128 return SSL_ERROR_SYSCALL; /* unknown */
2129 }
2130 }
Adam Langley95c29f32014-06-20 12:00:00 -07002131
Adam Langleyfcf25832014-12-18 17:42:32 -08002132 if (SSL_want_write(s)) {
2133 bio = SSL_get_wbio(s);
2134 if (BIO_should_write(bio)) {
2135 return SSL_ERROR_WANT_WRITE;
2136 }
Adam Langley95c29f32014-06-20 12:00:00 -07002137
Adam Langleyfcf25832014-12-18 17:42:32 -08002138 if (BIO_should_read(bio)) {
2139 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2140 return SSL_ERROR_WANT_READ;
2141 }
Adam Langley95c29f32014-06-20 12:00:00 -07002142
Adam Langleyfcf25832014-12-18 17:42:32 -08002143 if (BIO_should_io_special(bio)) {
2144 reason = BIO_get_retry_reason(bio);
2145 if (reason == BIO_RR_CONNECT) {
2146 return SSL_ERROR_WANT_CONNECT;
2147 }
Adam Langley95c29f32014-06-20 12:00:00 -07002148
Adam Langleyfcf25832014-12-18 17:42:32 -08002149 if (reason == BIO_RR_ACCEPT) {
2150 return SSL_ERROR_WANT_ACCEPT;
2151 }
Adam Langley95c29f32014-06-20 12:00:00 -07002152
Adam Langleyfcf25832014-12-18 17:42:32 -08002153 return SSL_ERROR_SYSCALL;
2154 }
2155 }
Adam Langley95c29f32014-06-20 12:00:00 -07002156
Adam Langleyfcf25832014-12-18 17:42:32 -08002157 if (SSL_want_x509_lookup(s)) {
2158 return SSL_ERROR_WANT_X509_LOOKUP;
2159 }
Adam Langley95c29f32014-06-20 12:00:00 -07002160
Adam Langleyfcf25832014-12-18 17:42:32 -08002161 if (SSL_want_channel_id_lookup(s)) {
2162 return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
2163 }
Adam Langley0f4746e2014-08-13 12:26:32 -07002164
Adam Langleyfcf25832014-12-18 17:42:32 -08002165 return SSL_ERROR_SYSCALL;
2166}
Adam Langley0f4746e2014-08-13 12:26:32 -07002167
Adam Langleyfcf25832014-12-18 17:42:32 -08002168int SSL_do_handshake(SSL *s) {
2169 int ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002170
Adam Langleyfcf25832014-12-18 17:42:32 -08002171 if (s->handshake_func == NULL) {
2172 OPENSSL_PUT_ERROR(SSL, SSL_do_handshake, SSL_R_CONNECTION_TYPE_NOT_SET);
2173 return -1;
2174 }
Adam Langley95c29f32014-06-20 12:00:00 -07002175
Adam Langleyfcf25832014-12-18 17:42:32 -08002176 s->method->ssl_renegotiate_check(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002177
Adam Langleyfcf25832014-12-18 17:42:32 -08002178 if (SSL_in_init(s)) {
2179 ret = s->handshake_func(s);
2180 }
2181 return ret;
2182}
Adam Langley95c29f32014-06-20 12:00:00 -07002183
Adam Langleyfcf25832014-12-18 17:42:32 -08002184void SSL_set_accept_state(SSL *s) {
2185 s->server = 1;
2186 s->shutdown = 0;
2187 s->state = SSL_ST_ACCEPT | SSL_ST_BEFORE;
2188 s->handshake_func = s->method->ssl_accept;
2189 /* clear the current cipher */
2190 ssl_clear_cipher_ctx(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002191}
Adam Langley95c29f32014-06-20 12:00:00 -07002192
Adam Langleyfcf25832014-12-18 17:42:32 -08002193void SSL_set_connect_state(SSL *s) {
2194 s->server = 0;
2195 s->shutdown = 0;
2196 s->state = SSL_ST_CONNECT | SSL_ST_BEFORE;
2197 s->handshake_func = s->method->ssl_connect;
2198 /* clear the current cipher */
2199 ssl_clear_cipher_ctx(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002200}
Adam Langley95c29f32014-06-20 12:00:00 -07002201
Adam Langleyfcf25832014-12-18 17:42:32 -08002202int ssl_undefined_function(SSL *s) {
2203 OPENSSL_PUT_ERROR(SSL, ssl_undefined_function,
2204 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2205 return 0;
2206}
Adam Langley95c29f32014-06-20 12:00:00 -07002207
Adam Langleyfcf25832014-12-18 17:42:32 -08002208int ssl_undefined_void_function(void) {
2209 OPENSSL_PUT_ERROR(SSL, ssl_undefined_void_function,
2210 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2211 return 0;
2212}
Adam Langley95c29f32014-06-20 12:00:00 -07002213
Adam Langleyfcf25832014-12-18 17:42:32 -08002214int ssl_undefined_const_function(const SSL *s) {
2215 OPENSSL_PUT_ERROR(SSL, ssl_undefined_const_function,
2216 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2217 return 0;
2218}
Adam Langley95c29f32014-06-20 12:00:00 -07002219
Adam Langleyfcf25832014-12-18 17:42:32 -08002220static const char *ssl_get_version(int version) {
2221 switch (version) {
2222 case TLS1_2_VERSION:
2223 return "TLSv1.2";
Adam Langley95c29f32014-06-20 12:00:00 -07002224
Adam Langleyfcf25832014-12-18 17:42:32 -08002225 case TLS1_1_VERSION:
2226 return "TLSv1.1";
Adam Langley95c29f32014-06-20 12:00:00 -07002227
Adam Langleyfcf25832014-12-18 17:42:32 -08002228 case TLS1_VERSION:
2229 return "TLSv1";
Adam Langley95c29f32014-06-20 12:00:00 -07002230
Adam Langleyfcf25832014-12-18 17:42:32 -08002231 case SSL3_VERSION:
2232 return "SSLv3";
Adam Langley95c29f32014-06-20 12:00:00 -07002233
David Benjamin1c722b72015-04-20 13:53:10 -04002234 case DTLS1_VERSION:
2235 return "DTLSv1";
2236
2237 case DTLS1_2_VERSION:
2238 return "DTLSv1.2";
2239
Adam Langleyfcf25832014-12-18 17:42:32 -08002240 default:
2241 return "unknown";
2242 }
2243}
Adam Langley95c29f32014-06-20 12:00:00 -07002244
Adam Langleyfcf25832014-12-18 17:42:32 -08002245const char *SSL_get_version(const SSL *s) {
2246 return ssl_get_version(s->version);
2247}
Adam Langley95c29f32014-06-20 12:00:00 -07002248
Adam Langleyfcf25832014-12-18 17:42:32 -08002249const char *SSL_SESSION_get_version(const SSL_SESSION *sess) {
2250 return ssl_get_version(sess->ssl_version);
2251}
Adam Langley95c29f32014-06-20 12:00:00 -07002252
Adam Langleyfcf25832014-12-18 17:42:32 -08002253void ssl_clear_cipher_ctx(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002254 if (s->aead_read_ctx != NULL) {
2255 EVP_AEAD_CTX_cleanup(&s->aead_read_ctx->ctx);
2256 OPENSSL_free(s->aead_read_ctx);
2257 s->aead_read_ctx = NULL;
2258 }
Adam Langleya5dc5452014-06-20 12:00:00 -07002259
Adam Langleyfcf25832014-12-18 17:42:32 -08002260 if (s->aead_write_ctx != NULL) {
2261 EVP_AEAD_CTX_cleanup(&s->aead_write_ctx->ctx);
2262 OPENSSL_free(s->aead_write_ctx);
2263 s->aead_write_ctx = NULL;
2264 }
2265}
Adam Langley95c29f32014-06-20 12:00:00 -07002266
Adam Langleyfcf25832014-12-18 17:42:32 -08002267X509 *SSL_get_certificate(const SSL *s) {
2268 if (s->cert != NULL) {
2269 return s->cert->key->x509;
2270 }
2271
2272 return NULL;
2273}
2274
2275EVP_PKEY *SSL_get_privatekey(const SSL *s) {
2276 if (s->cert != NULL) {
2277 return s->cert->key->privatekey;
2278 }
2279
2280 return NULL;
2281}
2282
2283X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
2284 if (ctx->cert != NULL) {
2285 return ctx->cert->key->x509;
2286 }
2287
2288 return NULL;
2289}
2290
2291EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
2292 if (ctx->cert != NULL) {
2293 return ctx->cert->key->privatekey;
2294 }
2295
2296 return NULL;
2297}
2298
2299const SSL_CIPHER *SSL_get_current_cipher(const SSL *s) {
2300 if (s->session != NULL && s->session->cipher != NULL) {
2301 return s->session->cipher;
2302 }
2303
2304 return NULL;
2305}
2306
2307const void *SSL_get_current_compression(SSL *s) { return NULL; }
2308
2309const void *SSL_get_current_expansion(SSL *s) { return NULL; }
2310
2311int ssl_init_wbio_buffer(SSL *s, int push) {
2312 BIO *bbio;
2313
2314 if (s->bbio == NULL) {
2315 bbio = BIO_new(BIO_f_buffer());
2316 if (bbio == NULL) {
2317 return 0;
2318 }
2319 s->bbio = bbio;
2320 } else {
2321 bbio = s->bbio;
2322 if (s->bbio == s->wbio) {
2323 s->wbio = BIO_pop(s->wbio);
2324 }
2325 }
2326
2327 BIO_reset(bbio);
2328 if (!BIO_set_read_buffer_size(bbio, 1)) {
2329 OPENSSL_PUT_ERROR(SSL, ssl_init_wbio_buffer, ERR_R_BUF_LIB);
2330 return 0;
2331 }
2332
2333 if (push) {
2334 if (s->wbio != bbio) {
2335 s->wbio = BIO_push(bbio, s->wbio);
2336 }
2337 } else {
2338 if (s->wbio == bbio) {
2339 s->wbio = BIO_pop(bbio);
2340 }
2341 }
2342
2343 return 1;
2344}
2345
2346void ssl_free_wbio_buffer(SSL *s) {
2347 if (s->bbio == NULL) {
2348 return;
2349 }
2350
2351 if (s->bbio == s->wbio) {
2352 /* remove buffering */
2353 s->wbio = BIO_pop(s->wbio);
2354 }
2355
2356 BIO_free(s->bbio);
2357 s->bbio = NULL;
2358}
2359
2360void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
2361 ctx->quiet_shutdown = mode;
2362}
2363
2364int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
2365 return ctx->quiet_shutdown;
2366}
2367
2368void SSL_set_quiet_shutdown(SSL *s, int mode) { s->quiet_shutdown = mode; }
2369
2370int SSL_get_quiet_shutdown(const SSL *s) { return s->quiet_shutdown; }
2371
2372void SSL_set_shutdown(SSL *s, int mode) { s->shutdown = mode; }
2373
2374int SSL_get_shutdown(const SSL *s) { return s->shutdown; }
2375
2376int SSL_version(const SSL *s) { return s->version; }
2377
2378SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
2379
2380SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
2381 if (ssl->ctx == ctx) {
2382 return ssl->ctx;
2383 }
2384
2385 if (ctx == NULL) {
2386 ctx = ssl->initial_ctx;
2387 }
2388
David Benjamin2755a3e2015-04-22 16:17:58 -04002389 ssl_cert_free(ssl->cert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002390 ssl->cert = ssl_cert_dup(ctx->cert);
David Benjamin2755a3e2015-04-22 16:17:58 -04002391
Adam Langleyfcf25832014-12-18 17:42:32 -08002392 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
David Benjamin2755a3e2015-04-22 16:17:58 -04002393 SSL_CTX_free(ssl->ctx); /* decrement reference count */
Adam Langleyfcf25832014-12-18 17:42:32 -08002394 ssl->ctx = ctx;
2395
2396 ssl->sid_ctx_length = ctx->sid_ctx_length;
2397 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2398 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2399
2400 return ssl->ctx;
2401}
2402
2403int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
2404 return X509_STORE_set_default_paths(ctx->cert_store);
2405}
Adam Langley95c29f32014-06-20 12:00:00 -07002406
2407int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
Adam Langleyfcf25832014-12-18 17:42:32 -08002408 const char *CApath) {
2409 return X509_STORE_load_locations(ctx->cert_store, CAfile, CApath);
2410}
Adam Langley95c29f32014-06-20 12:00:00 -07002411
2412void SSL_set_info_callback(SSL *ssl,
Adam Langleyfcf25832014-12-18 17:42:32 -08002413 void (*cb)(const SSL *ssl, int type, int val)) {
2414 ssl->info_callback = cb;
2415}
Adam Langley95c29f32014-06-20 12:00:00 -07002416
Adam Langleyfcf25832014-12-18 17:42:32 -08002417void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/, int /*type*/,
2418 int /*val*/) {
2419 return ssl->info_callback;
2420}
Adam Langley95c29f32014-06-20 12:00:00 -07002421
Adam Langleyfcf25832014-12-18 17:42:32 -08002422int SSL_state(const SSL *ssl) { return ssl->state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002423
Adam Langleyfcf25832014-12-18 17:42:32 -08002424void SSL_set_state(SSL *ssl, int state) { ssl->state = state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002425
Adam Langleyfcf25832014-12-18 17:42:32 -08002426void SSL_set_verify_result(SSL *ssl, long arg) { ssl->verify_result = arg; }
Adam Langley95c29f32014-06-20 12:00:00 -07002427
Adam Langleyfcf25832014-12-18 17:42:32 -08002428long SSL_get_verify_result(const SSL *ssl) { return ssl->verify_result; }
Adam Langley95c29f32014-06-20 12:00:00 -07002429
Adam Langleyfcf25832014-12-18 17:42:32 -08002430int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2431 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002432 int index;
2433 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp,
2434 new_func, dup_func, free_func)) {
2435 return -1;
2436 }
2437 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002438}
Adam Langley95c29f32014-06-20 12:00:00 -07002439
Adam Langleyfcf25832014-12-18 17:42:32 -08002440int SSL_set_ex_data(SSL *s, int idx, void *arg) {
2441 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2442}
Adam Langley95c29f32014-06-20 12:00:00 -07002443
Adam Langleyfcf25832014-12-18 17:42:32 -08002444void *SSL_get_ex_data(const SSL *s, int idx) {
2445 return CRYPTO_get_ex_data(&s->ex_data, idx);
2446}
Adam Langley95c29f32014-06-20 12:00:00 -07002447
Adam Langleyfcf25832014-12-18 17:42:32 -08002448int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2449 CRYPTO_EX_dup *dup_func,
2450 CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002451 int index;
2452 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp,
2453 new_func, dup_func, free_func)) {
2454 return -1;
2455 }
2456 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002457}
Adam Langley95c29f32014-06-20 12:00:00 -07002458
Adam Langleyfcf25832014-12-18 17:42:32 -08002459int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg) {
2460 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2461}
Adam Langley95c29f32014-06-20 12:00:00 -07002462
Adam Langleyfcf25832014-12-18 17:42:32 -08002463void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx) {
2464 return CRYPTO_get_ex_data(&s->ex_data, idx);
2465}
Adam Langley95c29f32014-06-20 12:00:00 -07002466
Adam Langleyfcf25832014-12-18 17:42:32 -08002467int ssl_ok(SSL *s) { return 1; }
Adam Langley95c29f32014-06-20 12:00:00 -07002468
Adam Langleyfcf25832014-12-18 17:42:32 -08002469X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
2470 return ctx->cert_store;
2471}
Adam Langley95c29f32014-06-20 12:00:00 -07002472
Adam Langleyfcf25832014-12-18 17:42:32 -08002473void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
David Benjamin2755a3e2015-04-22 16:17:58 -04002474 X509_STORE_free(ctx->cert_store);
Adam Langleyfcf25832014-12-18 17:42:32 -08002475 ctx->cert_store = store;
2476}
Adam Langley95c29f32014-06-20 12:00:00 -07002477
Adam Langleyfcf25832014-12-18 17:42:32 -08002478int SSL_want(const SSL *s) { return s->rwstate; }
Adam Langley95c29f32014-06-20 12:00:00 -07002479
Adam Langleyfcf25832014-12-18 17:42:32 -08002480void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
2481 RSA *(*cb)(SSL *ssl, int is_export,
2482 int keylength)) {
2483 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_RSA_CB, (void (*)(void))cb);
2484}
Adam Langley95c29f32014-06-20 12:00:00 -07002485
Adam Langleyfcf25832014-12-18 17:42:32 -08002486void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
2487 int keylength)) {
2488 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_RSA_CB, (void (*)(void))cb);
2489}
2490
2491void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
2492 DH *(*dh)(SSL *ssl, int is_export,
2493 int keylength)) {
2494 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh);
2495}
2496
2497void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*dh)(SSL *ssl, int is_export,
2498 int keylength)) {
2499 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh);
2500}
2501
2502void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,
David Benjamindd978782015-04-24 15:20:13 -04002503 EC_KEY *(*callback)(SSL *ssl, int is_export,
2504 int keylength)) {
2505 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH_CB,
2506 (void (*)(void))callback);
Adam Langleyfcf25832014-12-18 17:42:32 -08002507}
2508
2509void SSL_set_tmp_ecdh_callback(SSL *ssl,
David Benjamindd978782015-04-24 15:20:13 -04002510 EC_KEY *(*callback)(SSL *ssl, int is_export,
2511 int keylength)) {
2512 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_ECDH_CB, (void (*)(void))callback);
Adam Langleyfcf25832014-12-18 17:42:32 -08002513}
2514
2515int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
2516 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2517 OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_psk_identity_hint,
2518 SSL_R_DATA_LENGTH_TOO_LONG);
2519 return 0;
2520 }
2521
David Benjamin2755a3e2015-04-22 16:17:58 -04002522 OPENSSL_free(ctx->psk_identity_hint);
Adam Langleyfcf25832014-12-18 17:42:32 -08002523
2524 if (identity_hint != NULL) {
2525 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2526 if (ctx->psk_identity_hint == NULL) {
2527 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002528 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002529 } else {
2530 ctx->psk_identity_hint = NULL;
2531 }
Adam Langley95c29f32014-06-20 12:00:00 -07002532
Adam Langleyfcf25832014-12-18 17:42:32 -08002533 return 1;
2534}
2535
2536int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint) {
2537 if (s == NULL) {
2538 return 0;
2539 }
2540
2541 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2542 OPENSSL_PUT_ERROR(SSL, SSL_use_psk_identity_hint,
2543 SSL_R_DATA_LENGTH_TOO_LONG);
2544 return 0;
2545 }
2546
2547 /* Clear currently configured hint, if any. */
David Benjamin2755a3e2015-04-22 16:17:58 -04002548 OPENSSL_free(s->psk_identity_hint);
2549 s->psk_identity_hint = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002550
2551 if (identity_hint != NULL) {
2552 s->psk_identity_hint = BUF_strdup(identity_hint);
2553 if (s->psk_identity_hint == NULL) {
2554 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002555 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002556 }
Adam Langley95c29f32014-06-20 12:00:00 -07002557
Adam Langleyfcf25832014-12-18 17:42:32 -08002558 return 1;
2559}
Adam Langley95c29f32014-06-20 12:00:00 -07002560
Adam Langleyfcf25832014-12-18 17:42:32 -08002561const char *SSL_get_psk_identity_hint(const SSL *s) {
2562 if (s == NULL) {
2563 return NULL;
2564 }
2565 return s->psk_identity_hint;
2566}
Adam Langley95c29f32014-06-20 12:00:00 -07002567
Adam Langleyfcf25832014-12-18 17:42:32 -08002568const char *SSL_get_psk_identity(const SSL *s) {
2569 if (s == NULL || s->session == NULL) {
2570 return NULL;
2571 }
Adam Langley95c29f32014-06-20 12:00:00 -07002572
Adam Langleyfcf25832014-12-18 17:42:32 -08002573 return s->session->psk_identity;
2574}
Adam Langley95c29f32014-06-20 12:00:00 -07002575
Adam Langleyfcf25832014-12-18 17:42:32 -08002576void SSL_set_psk_client_callback(
2577 SSL *s, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2578 unsigned int max_identity_len, uint8_t *psk,
2579 unsigned int max_psk_len)) {
2580 s->psk_client_callback = cb;
2581}
Adam Langley95c29f32014-06-20 12:00:00 -07002582
Adam Langleyfcf25832014-12-18 17:42:32 -08002583void SSL_CTX_set_psk_client_callback(
2584 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2585 unsigned int max_identity_len,
2586 uint8_t *psk, unsigned int max_psk_len)) {
2587 ctx->psk_client_callback = cb;
2588}
Adam Langley95c29f32014-06-20 12:00:00 -07002589
Adam Langleyfcf25832014-12-18 17:42:32 -08002590void SSL_set_psk_server_callback(
2591 SSL *s, unsigned int (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
2592 unsigned int max_psk_len)) {
2593 s->psk_server_callback = cb;
2594}
Adam Langley95c29f32014-06-20 12:00:00 -07002595
Adam Langleyfcf25832014-12-18 17:42:32 -08002596void SSL_CTX_set_psk_server_callback(
2597 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *identity,
2598 uint8_t *psk, unsigned int max_psk_len)) {
2599 ctx->psk_server_callback = cb;
2600}
Adam Langley95c29f32014-06-20 12:00:00 -07002601
Adam Langleyfcf25832014-12-18 17:42:32 -08002602void SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) {
2603 ctx->min_version = version;
2604}
Adam Langley95c29f32014-06-20 12:00:00 -07002605
Adam Langleyfcf25832014-12-18 17:42:32 -08002606void SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) {
2607 ctx->max_version = version;
2608}
Adam Langley0289c732014-06-20 12:00:00 -07002609
Adam Langleyfcf25832014-12-18 17:42:32 -08002610void SSL_set_min_version(SSL *ssl, uint16_t version) {
2611 ssl->min_version = version;
2612}
Adam Langley0289c732014-06-20 12:00:00 -07002613
Adam Langleyfcf25832014-12-18 17:42:32 -08002614void SSL_set_max_version(SSL *ssl, uint16_t version) {
2615 ssl->max_version = version;
2616}
Adam Langley95c29f32014-06-20 12:00:00 -07002617
Adam Langleyfcf25832014-12-18 17:42:32 -08002618void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
2619 void (*cb)(int write_p, int version,
2620 int content_type, const void *buf,
2621 size_t len, SSL *ssl, void *arg)) {
2622 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2623}
2624void SSL_set_msg_callback(SSL *ssl,
2625 void (*cb)(int write_p, int version, int content_type,
2626 const void *buf, size_t len, SSL *ssl,
2627 void *arg)) {
2628 SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2629}
Adam Langley95c29f32014-06-20 12:00:00 -07002630
Adam Langleyfcf25832014-12-18 17:42:32 -08002631void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio) {
David Benjamin2755a3e2015-04-22 16:17:58 -04002632 BIO_free(ctx->keylog_bio);
Adam Langleyfcf25832014-12-18 17:42:32 -08002633 ctx->keylog_bio = keylog_bio;
2634}
Adam Langley95c29f32014-06-20 12:00:00 -07002635
Adam Langleyfcf25832014-12-18 17:42:32 -08002636static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
2637 static const char hextable[] = "0123456789abcdef";
2638 uint8_t *out;
2639 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002640
Adam Langleyfcf25832014-12-18 17:42:32 -08002641 if (!CBB_add_space(cbb, &out, in_len * 2)) {
2642 return 0;
2643 }
Adam Langley95c29f32014-06-20 12:00:00 -07002644
Adam Langleyfcf25832014-12-18 17:42:32 -08002645 for (i = 0; i < in_len; i++) {
2646 *(out++) = (uint8_t)hextable[in[i] >> 4];
2647 *(out++) = (uint8_t)hextable[in[i] & 0xf];
2648 }
Adam Langley95c29f32014-06-20 12:00:00 -07002649
Adam Langleyfcf25832014-12-18 17:42:32 -08002650 return 1;
2651}
David Benjamin859ec3c2014-09-02 16:29:36 -04002652
2653int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -08002654 const uint8_t *encrypted_premaster,
2655 size_t encrypted_premaster_len,
2656 const uint8_t *premaster,
2657 size_t premaster_len) {
2658 BIO *bio = ctx->keylog_bio;
2659 CBB cbb;
2660 uint8_t *out;
2661 size_t out_len;
2662 int ret;
David Benjamin859ec3c2014-09-02 16:29:36 -04002663
Adam Langleyfcf25832014-12-18 17:42:32 -08002664 if (bio == NULL) {
2665 return 1;
2666 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002667
Adam Langleyfcf25832014-12-18 17:42:32 -08002668 if (encrypted_premaster_len < 8) {
2669 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_rsa_client_key_exchange,
2670 ERR_R_INTERNAL_ERROR);
2671 return 0;
2672 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002673
Adam Langleyfcf25832014-12-18 17:42:32 -08002674 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len * 2 + 1)) {
2675 return 0;
2676 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002677
Adam Langleyfcf25832014-12-18 17:42:32 -08002678 if (!CBB_add_bytes(&cbb, (const uint8_t *)"RSA ", 4) ||
2679 /* Only the first 8 bytes of the encrypted premaster secret are
2680 * logged. */
2681 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
2682 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2683 !cbb_add_hex(&cbb, premaster, premaster_len) ||
2684 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2685 !CBB_finish(&cbb, &out, &out_len)) {
2686 CBB_cleanup(&cbb);
2687 return 0;
2688 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002689
Adam Langleyfcf25832014-12-18 17:42:32 -08002690 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2691 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2692 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
David Benjamin859ec3c2014-09-02 16:29:36 -04002693
Adam Langleyfcf25832014-12-18 17:42:32 -08002694 OPENSSL_free(out);
2695 return ret;
Adam Langley95f22882014-06-20 12:00:00 -07002696}
2697
Adam Langleyfcf25832014-12-18 17:42:32 -08002698int ssl_ctx_log_master_secret(SSL_CTX *ctx, const uint8_t *client_random,
2699 size_t client_random_len, const uint8_t *master,
2700 size_t master_len) {
2701 BIO *bio = ctx->keylog_bio;
2702 CBB cbb;
2703 uint8_t *out;
2704 size_t out_len;
2705 int ret;
Adam Langleyadb739e2014-06-20 12:00:00 -07002706
Adam Langleyfcf25832014-12-18 17:42:32 -08002707 if (bio == NULL) {
2708 return 1;
2709 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002710
Adam Langleyfcf25832014-12-18 17:42:32 -08002711 if (client_random_len != 32) {
2712 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_master_secret, ERR_R_INTERNAL_ERROR);
2713 return 0;
2714 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002715
Adam Langleyfcf25832014-12-18 17:42:32 -08002716 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len * 2 + 1)) {
2717 return 0;
2718 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002719
Adam Langleyfcf25832014-12-18 17:42:32 -08002720 if (!CBB_add_bytes(&cbb, (const uint8_t *)"CLIENT_RANDOM ", 14) ||
2721 !cbb_add_hex(&cbb, client_random, 32) ||
2722 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2723 !cbb_add_hex(&cbb, master, master_len) ||
2724 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2725 !CBB_finish(&cbb, &out, &out_len)) {
2726 CBB_cleanup(&cbb);
2727 return 0;
2728 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002729
Adam Langleyfcf25832014-12-18 17:42:32 -08002730 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2731 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2732 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
David Benjamine99e9122014-12-11 01:46:01 -05002733
Adam Langleyfcf25832014-12-18 17:42:32 -08002734 OPENSSL_free(out);
2735 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -07002736}
2737
David Benjamined7c4752015-02-16 19:16:46 -05002738int SSL_in_false_start(const SSL *s) {
2739 return s->s3->tmp.in_false_start;
2740}
2741
Adam Langleyfcf25832014-12-18 17:42:32 -08002742int SSL_cutthrough_complete(const SSL *s) {
David Benjamined7c4752015-02-16 19:16:46 -05002743 return SSL_in_false_start(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002744}
Adam Langley95c29f32014-06-20 12:00:00 -07002745
Adam Langleyfcf25832014-12-18 17:42:32 -08002746void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
2747 size_t *ssl_session_size) {
2748 *ssl_size = sizeof(SSL);
2749 *ssl_ctx_size = sizeof(SSL_CTX);
2750 *ssl_session_size = sizeof(SSL_SESSION);
2751}
Feng Lu41aa3252014-11-21 22:47:56 -08002752
David Benjamined7c4752015-02-16 19:16:46 -05002753int ssl3_can_false_start(const SSL *s) {
David Benjamin195dc782015-02-19 13:27:05 -05002754 const SSL_CIPHER *const cipher = SSL_get_current_cipher(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002755
Adam Langleye631d962015-04-01 13:11:01 -07002756 /* False Start only for TLS 1.2 with an ECDHE+AEAD cipher and ALPN or NPN. */
David Benjamin195dc782015-02-19 13:27:05 -05002757 return !SSL_IS_DTLS(s) &&
2758 SSL_version(s) >= TLS1_2_VERSION &&
2759 (s->s3->alpn_selected || s->s3->next_proto_neg_seen) &&
2760 cipher != NULL &&
Adam Langleye631d962015-04-01 13:11:01 -07002761 cipher->algorithm_mkey == SSL_kECDHE &&
David Benjamin195dc782015-02-19 13:27:05 -05002762 (cipher->algorithm_enc == SSL_AES128GCM ||
2763 cipher->algorithm_enc == SSL_AES256GCM ||
2764 cipher->algorithm_enc == SSL_CHACHA20POLY1305);
Adam Langleyfcf25832014-12-18 17:42:32 -08002765}
2766
2767const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version) {
2768 switch (version) {
2769 case SSL3_VERSION:
2770 return &SSLv3_enc_data;
2771
2772 case TLS1_VERSION:
2773 return &TLSv1_enc_data;
2774
David Benjamin9e13e1a2015-03-05 01:56:32 -05002775 case DTLS1_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002776 case TLS1_1_VERSION:
2777 return &TLSv1_1_enc_data;
2778
David Benjamin9e13e1a2015-03-05 01:56:32 -05002779 case DTLS1_2_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002780 case TLS1_2_VERSION:
2781 return &TLSv1_2_enc_data;
2782
Adam Langleyfcf25832014-12-18 17:42:32 -08002783 default:
2784 return NULL;
2785 }
2786}
2787
2788uint16_t ssl3_get_max_server_version(const SSL *s) {
2789 uint16_t max_version;
2790
2791 if (SSL_IS_DTLS(s)) {
2792 max_version = (s->max_version != 0) ? s->max_version : DTLS1_2_VERSION;
2793 if (!(s->options & SSL_OP_NO_DTLSv1_2) && DTLS1_2_VERSION >= max_version) {
2794 return DTLS1_2_VERSION;
2795 }
2796 if (!(s->options & SSL_OP_NO_DTLSv1) && DTLS1_VERSION >= max_version) {
2797 return DTLS1_VERSION;
2798 }
2799 return 0;
2800 }
2801
2802 max_version = (s->max_version != 0) ? s->max_version : TLS1_2_VERSION;
2803 if (!(s->options & SSL_OP_NO_TLSv1_2) && TLS1_2_VERSION <= max_version) {
2804 return TLS1_2_VERSION;
2805 }
2806 if (!(s->options & SSL_OP_NO_TLSv1_1) && TLS1_1_VERSION <= max_version) {
2807 return TLS1_1_VERSION;
2808 }
2809 if (!(s->options & SSL_OP_NO_TLSv1) && TLS1_VERSION <= max_version) {
2810 return TLS1_VERSION;
2811 }
2812 if (!(s->options & SSL_OP_NO_SSLv3) && SSL3_VERSION <= max_version) {
2813 return SSL3_VERSION;
2814 }
2815 return 0;
2816}
2817
2818uint16_t ssl3_get_mutual_version(SSL *s, uint16_t client_version) {
2819 uint16_t version = 0;
2820
2821 if (SSL_IS_DTLS(s)) {
2822 /* Clamp client_version to max_version. */
2823 if (s->max_version != 0 && client_version < s->max_version) {
2824 client_version = s->max_version;
2825 }
2826
2827 if (client_version <= DTLS1_2_VERSION && !(s->options & SSL_OP_NO_DTLSv1_2)) {
2828 version = DTLS1_2_VERSION;
2829 } else if (client_version <= DTLS1_VERSION &&
David Benjaminb18f0242015-03-10 18:30:08 -04002830 !(s->options & SSL_OP_NO_DTLSv1)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002831 version = DTLS1_VERSION;
2832 }
2833
2834 /* Check against min_version. */
2835 if (version != 0 && s->min_version != 0 && version > s->min_version) {
2836 return 0;
2837 }
2838 return version;
2839 } else {
2840 /* Clamp client_version to max_version. */
2841 if (s->max_version != 0 && client_version > s->max_version) {
2842 client_version = s->max_version;
2843 }
2844
2845 if (client_version >= TLS1_2_VERSION && !(s->options & SSL_OP_NO_TLSv1_2)) {
2846 version = TLS1_2_VERSION;
2847 } else if (client_version >= TLS1_1_VERSION &&
2848 !(s->options & SSL_OP_NO_TLSv1_1)) {
2849 version = TLS1_1_VERSION;
2850 } else if (client_version >= TLS1_VERSION && !(s->options & SSL_OP_NO_TLSv1)) {
2851 version = TLS1_VERSION;
2852 } else if (client_version >= SSL3_VERSION && !(s->options & SSL_OP_NO_SSLv3)) {
2853 version = SSL3_VERSION;
2854 }
2855
2856 /* Check against min_version. */
2857 if (version != 0 && s->min_version != 0 && version < s->min_version) {
2858 return 0;
2859 }
2860 return version;
2861 }
2862}
2863
2864uint16_t ssl3_get_max_client_version(SSL *s) {
2865 unsigned long options = s->options;
2866 uint16_t version = 0;
2867
2868 /* OpenSSL's API for controlling versions entails blacklisting individual
2869 * protocols. This has two problems. First, on the client, the protocol can
2870 * only express a contiguous range of versions. Second, a library consumer
2871 * trying to set a maximum version cannot disable protocol versions that get
2872 * added in a future version of the library.
2873 *
2874 * To account for both of these, OpenSSL interprets the client-side bitmask
2875 * as a min/max range by picking the lowest contiguous non-empty range of
2876 * enabled protocols. Note that this means it is impossible to set a maximum
2877 * version of TLS 1.2 in a future-proof way.
2878 *
2879 * By this scheme, the maximum version is the lowest version V such that V is
2880 * enabled and V+1 is disabled or unimplemented. */
2881 if (SSL_IS_DTLS(s)) {
2882 if (!(options & SSL_OP_NO_DTLSv1_2)) {
2883 version = DTLS1_2_VERSION;
2884 }
2885 if (!(options & SSL_OP_NO_DTLSv1) && (options & SSL_OP_NO_DTLSv1_2)) {
2886 version = DTLS1_VERSION;
2887 }
2888 if (s->max_version != 0 && version < s->max_version) {
2889 version = s->max_version;
2890 }
2891 } else {
2892 if (!(options & SSL_OP_NO_TLSv1_2)) {
2893 version = TLS1_2_VERSION;
2894 }
2895 if (!(options & SSL_OP_NO_TLSv1_1) && (options & SSL_OP_NO_TLSv1_2)) {
2896 version = TLS1_1_VERSION;
2897 }
2898 if (!(options & SSL_OP_NO_TLSv1) && (options & SSL_OP_NO_TLSv1_1)) {
2899 version = TLS1_VERSION;
2900 }
2901 if (!(options & SSL_OP_NO_SSLv3) && (options & SSL_OP_NO_TLSv1)) {
2902 version = SSL3_VERSION;
2903 }
2904 if (s->max_version != 0 && version > s->max_version) {
2905 version = s->max_version;
2906 }
2907 }
2908
2909 return version;
2910}
2911
2912int ssl3_is_version_enabled(SSL *s, uint16_t version) {
2913 if (SSL_IS_DTLS(s)) {
2914 if (s->max_version != 0 && version < s->max_version) {
2915 return 0;
2916 }
2917 if (s->min_version != 0 && version > s->min_version) {
2918 return 0;
2919 }
2920
2921 switch (version) {
2922 case DTLS1_VERSION:
2923 return !(s->options & SSL_OP_NO_DTLSv1);
2924
2925 case DTLS1_2_VERSION:
2926 return !(s->options & SSL_OP_NO_DTLSv1_2);
2927
2928 default:
2929 return 0;
2930 }
2931 } else {
2932 if (s->max_version != 0 && version > s->max_version) {
2933 return 0;
2934 }
2935 if (s->min_version != 0 && version < s->min_version) {
2936 return 0;
2937 }
2938
2939 switch (version) {
2940 case SSL3_VERSION:
2941 return !(s->options & SSL_OP_NO_SSLv3);
2942
2943 case TLS1_VERSION:
2944 return !(s->options & SSL_OP_NO_TLSv1);
2945
2946 case TLS1_1_VERSION:
2947 return !(s->options & SSL_OP_NO_TLSv1_1);
2948
2949 case TLS1_2_VERSION:
2950 return !(s->options & SSL_OP_NO_TLSv1_2);
2951
2952 default:
2953 return 0;
2954 }
2955 }
2956}
2957
David Benjaminea72bd02014-12-21 21:27:41 -05002958uint16_t ssl3_version_from_wire(SSL *s, uint16_t wire_version) {
2959 if (!SSL_IS_DTLS(s)) {
2960 return wire_version;
2961 }
2962
2963 uint16_t tls_version = ~wire_version;
2964 uint16_t version = tls_version + 0x0201;
2965 /* If either component overflowed, clamp it so comparisons still work. */
2966 if ((version >> 8) < (tls_version >> 8)) {
2967 version = 0xff00 | (version & 0xff);
2968 }
2969 if ((version & 0xff) < (tls_version & 0xff)) {
2970 version = (version & 0xff00) | 0xff;
2971 }
2972 /* DTLS 1.0 maps to TLS 1.1, not TLS 1.0. */
2973 if (version == TLS1_VERSION) {
2974 version = TLS1_1_VERSION;
2975 }
2976 return version;
2977}
2978
Adam Langleyfcf25832014-12-18 17:42:32 -08002979int SSL_cache_hit(SSL *s) { return s->hit; }
2980
2981int SSL_is_server(SSL *s) { return s->server; }
2982
Adam Langley524e7172015-02-20 16:04:00 -08002983void SSL_CTX_set_dos_protection_cb(
2984 SSL_CTX *ctx, int (*cb)(const struct ssl_early_callback_ctx *)) {
2985 ctx->dos_protection_cb = cb;
2986}
2987
Adam Langleyfcf25832014-12-18 17:42:32 -08002988void SSL_enable_fastradio_padding(SSL *s, char on_off) {
2989 s->fastradio_padding = on_off;
2990}
Adam Langley44972942015-02-20 13:43:23 -08002991
David Benjaminb16346b2015-04-08 19:16:58 -04002992void SSL_set_reject_peer_renegotiations(SSL *s, int reject) {
2993 s->reject_peer_renegotiations = !!reject;
2994}
2995
Adam Langley44972942015-02-20 13:43:23 -08002996const SSL_CIPHER *SSL_get_cipher_by_value(uint16_t value) {
2997 return ssl3_get_cipher_by_value(value);
2998}
Adam Langley3f92d212015-02-20 15:32:52 -08002999
3000int SSL_get_rc4_state(const SSL *ssl, const RC4_KEY **read_key,
3001 const RC4_KEY **write_key) {
3002 if (ssl->aead_read_ctx == NULL || ssl->aead_write_ctx == NULL) {
3003 return 0;
3004 }
3005
3006 return EVP_AEAD_CTX_get_rc4_state(&ssl->aead_read_ctx->ctx, read_key) &&
3007 EVP_AEAD_CTX_get_rc4_state(&ssl->aead_write_ctx->ctx, write_key);
3008}