blob: 0cfcead2f67e8d4c975aba34986bc0ef4f2bc41e [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
108 *
109 */
110/* ====================================================================
111 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
112 * ECC cipher suite support in OpenSSL originally developed by
113 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
114 */
115/* ====================================================================
116 * Copyright 2005 Nokia. All rights reserved.
117 *
118 * The portions of the attached software ("Contribution") is developed by
119 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
120 * license.
121 *
122 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
123 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
124 * support (see RFC 4279) to OpenSSL.
125 *
126 * No patent licenses or other rights except those expressly stated in
127 * the OpenSSL open source license shall be deemed granted or received
128 * expressly, by implication, estoppel, or otherwise.
129 *
130 * No assurances are provided by Nokia that the Contribution does not
131 * infringe the patent or other intellectual property rights of any third
132 * party or that the license provides you with all the necessary rights
133 * to make use of the Contribution.
134 *
135 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
136 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
137 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
138 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
139 * OTHERWISE. */
140
Adam Langley95c29f32014-06-20 12:00:00 -0700141#include <assert.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400142#include <stdio.h>
143#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700144
David Benjamin39482a12014-07-20 13:30:15 -0400145#include <openssl/bytestring.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700146#include <openssl/dh.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400147#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700148#include <openssl/lhash.h>
149#include <openssl/mem.h>
150#include <openssl/obj.h>
151#include <openssl/rand.h>
152#include <openssl/x509v3.h>
153
David Benjamin2ee94aa2015-04-07 22:38:30 -0400154#include "internal.h"
David Benjamin546f1a52015-04-15 16:46:09 -0400155#include "../crypto/internal.h"
156
Adam Langley95c29f32014-06-20 12:00:00 -0700157
Adam Langleyfcf25832014-12-18 17:42:32 -0800158/* Some error codes are special. Ensure the make_errors.go script never
159 * regresses this. */
160OPENSSL_COMPILE_ASSERT(SSL_R_TLSV1_ALERT_NO_RENEGOTIATION ==
161 SSL_AD_NO_RENEGOTIATION + SSL_AD_REASON_OFFSET,
162 ssl_alert_reason_code_mismatch);
David Benjamine1136082014-09-20 12:28:58 -0400163
David Benjamin9f33fc62015-04-15 17:29:53 -0400164static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl = CRYPTO_EX_DATA_CLASS_INIT;
165static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl_ctx = CRYPTO_EX_DATA_CLASS_INIT;
166
Adam Langleyfcf25832014-12-18 17:42:32 -0800167int SSL_clear(SSL *s) {
168 if (s->method == NULL) {
169 OPENSSL_PUT_ERROR(SSL, SSL_clear, SSL_R_NO_METHOD_SPECIFIED);
170 return 0;
171 }
Adam Langley95c29f32014-06-20 12:00:00 -0700172
Adam Langleyfcf25832014-12-18 17:42:32 -0800173 if (ssl_clear_bad_session(s)) {
174 SSL_SESSION_free(s->session);
175 s->session = NULL;
176 }
Adam Langley95c29f32014-06-20 12:00:00 -0700177
Adam Langleyfcf25832014-12-18 17:42:32 -0800178 s->hit = 0;
179 s->shutdown = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700180
Adam Langleyfcf25832014-12-18 17:42:32 -0800181 if (s->renegotiate) {
182 OPENSSL_PUT_ERROR(SSL, SSL_clear, ERR_R_INTERNAL_ERROR);
183 return 0;
184 }
Adam Langley95c29f32014-06-20 12:00:00 -0700185
Adam Langleyfcf25832014-12-18 17:42:32 -0800186 /* SSL_clear may be called before or after the |s| is initialized in either
187 * accept or connect state. In the latter case, SSL_clear should preserve the
188 * half and reset |s->state| accordingly. */
189 if (s->handshake_func != NULL) {
190 if (s->server) {
191 SSL_set_accept_state(s);
192 } else {
193 SSL_set_connect_state(s);
194 }
195 } else {
196 assert(s->state == 0);
197 }
Adam Langley95c29f32014-06-20 12:00:00 -0700198
David Benjamin62fd1622015-01-11 13:30:01 -0500199 /* TODO(davidben): Some state on |s| is reset both in |SSL_new| and
200 * |SSL_clear| because it is per-connection state rather than configuration
201 * state. Per-connection state should be on |s->s3| and |s->d1| so it is
202 * naturally reset at the right points between |SSL_new|, |SSL_clear|, and
203 * |ssl3_new|. */
204
Adam Langleyfcf25832014-12-18 17:42:32 -0800205 s->rwstate = SSL_NOTHING;
206 s->rstate = SSL_ST_READ_HEADER;
Adam Langley95c29f32014-06-20 12:00:00 -0700207
David Benjamin2755a3e2015-04-22 16:17:58 -0400208 BUF_MEM_free(s->init_buf);
209 s->init_buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700210
David Benjamin62fd1622015-01-11 13:30:01 -0500211 s->packet = NULL;
212 s->packet_length = 0;
213
Adam Langleyfcf25832014-12-18 17:42:32 -0800214 ssl_clear_cipher_ctx(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700215
David Benjamin2755a3e2015-04-22 16:17:58 -0400216 OPENSSL_free(s->next_proto_negotiated);
217 s->next_proto_negotiated = NULL;
218 s->next_proto_negotiated_len = 0;
David Benjamin62fd1622015-01-11 13:30:01 -0500219
220 /* The s->d1->mtu is simultaneously configuration (preserved across
221 * clear) and connection-specific state (gets reset).
222 *
223 * TODO(davidben): Avoid this. */
224 unsigned mtu = 0;
225 if (s->d1 != NULL) {
226 mtu = s->d1->mtu;
227 }
228
229 s->method->ssl_free(s);
230 if (!s->method->ssl_new(s)) {
231 return 0;
232 }
233 s->enc_method = ssl3_get_enc_method(s->version);
234 assert(s->enc_method != NULL);
235
236 if (SSL_IS_DTLS(s) && (SSL_get_options(s) & SSL_OP_NO_QUERY_MTU)) {
237 s->d1->mtu = mtu;
238 }
239
Adam Langleyfcf25832014-12-18 17:42:32 -0800240 s->client_version = s->version;
Adam Langley95c29f32014-06-20 12:00:00 -0700241
Adam Langleyfcf25832014-12-18 17:42:32 -0800242 return 1;
243}
Adam Langley95c29f32014-06-20 12:00:00 -0700244
Adam Langleyfcf25832014-12-18 17:42:32 -0800245SSL *SSL_new(SSL_CTX *ctx) {
246 SSL *s;
Adam Langley95c29f32014-06-20 12:00:00 -0700247
Adam Langleyfcf25832014-12-18 17:42:32 -0800248 if (ctx == NULL) {
249 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_NULL_SSL_CTX);
250 return NULL;
251 }
252 if (ctx->method == NULL) {
253 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
254 return NULL;
255 }
Adam Langley95c29f32014-06-20 12:00:00 -0700256
Adam Langleyfcf25832014-12-18 17:42:32 -0800257 s = (SSL *)OPENSSL_malloc(sizeof(SSL));
258 if (s == NULL) {
259 goto err;
260 }
261 memset(s, 0, sizeof(SSL));
Adam Langley95c29f32014-06-20 12:00:00 -0700262
Adam Langleyfcf25832014-12-18 17:42:32 -0800263 s->min_version = ctx->min_version;
264 s->max_version = ctx->max_version;
David Benjamin1eb367c2014-12-12 18:17:51 -0500265
Adam Langleyfcf25832014-12-18 17:42:32 -0800266 s->options = ctx->options;
267 s->mode = ctx->mode;
268 s->max_cert_list = ctx->max_cert_list;
Adam Langley95c29f32014-06-20 12:00:00 -0700269
David Benjamina5a3eeb2015-03-18 20:26:30 -0400270 s->cert = ssl_cert_dup(ctx->cert);
271 if (s->cert == NULL) {
272 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -0800273 }
Adam Langley95c29f32014-06-20 12:00:00 -0700274
Adam Langleyfcf25832014-12-18 17:42:32 -0800275 s->read_ahead = ctx->read_ahead;
276 s->msg_callback = ctx->msg_callback;
277 s->msg_callback_arg = ctx->msg_callback_arg;
278 s->verify_mode = ctx->verify_mode;
279 s->sid_ctx_length = ctx->sid_ctx_length;
280 assert(s->sid_ctx_length <= sizeof s->sid_ctx);
281 memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));
282 s->verify_callback = ctx->default_verify_callback;
283 s->generate_session_id = ctx->generate_session_id;
Adam Langley95c29f32014-06-20 12:00:00 -0700284
Adam Langleyfcf25832014-12-18 17:42:32 -0800285 s->param = X509_VERIFY_PARAM_new();
286 if (!s->param) {
287 goto err;
288 }
289 X509_VERIFY_PARAM_inherit(s->param, ctx->param);
290 s->quiet_shutdown = ctx->quiet_shutdown;
291 s->max_send_fragment = ctx->max_send_fragment;
Adam Langley95c29f32014-06-20 12:00:00 -0700292
Adam Langleyfcf25832014-12-18 17:42:32 -0800293 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
294 s->ctx = ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -0800295 s->tlsext_ticket_expected = 0;
296 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
297 s->initial_ctx = ctx;
298 if (ctx->tlsext_ecpointformatlist) {
299 s->tlsext_ecpointformatlist = BUF_memdup(
300 ctx->tlsext_ecpointformatlist, ctx->tlsext_ecpointformatlist_length);
301 if (!s->tlsext_ecpointformatlist) {
302 goto err;
303 }
304 s->tlsext_ecpointformatlist_length = ctx->tlsext_ecpointformatlist_length;
305 }
Adam Langley95c29f32014-06-20 12:00:00 -0700306
Adam Langleyfcf25832014-12-18 17:42:32 -0800307 if (ctx->tlsext_ellipticcurvelist) {
308 s->tlsext_ellipticcurvelist =
309 BUF_memdup(ctx->tlsext_ellipticcurvelist,
310 ctx->tlsext_ellipticcurvelist_length * 2);
311 if (!s->tlsext_ellipticcurvelist) {
312 goto err;
313 }
314 s->tlsext_ellipticcurvelist_length = ctx->tlsext_ellipticcurvelist_length;
315 }
316 s->next_proto_negotiated = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700317
Adam Langleyfcf25832014-12-18 17:42:32 -0800318 if (s->ctx->alpn_client_proto_list) {
319 s->alpn_client_proto_list = BUF_memdup(s->ctx->alpn_client_proto_list,
320 s->ctx->alpn_client_proto_list_len);
321 if (s->alpn_client_proto_list == NULL) {
322 goto err;
323 }
324 s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;
325 }
Adam Langley95c29f32014-06-20 12:00:00 -0700326
Adam Langleyfcf25832014-12-18 17:42:32 -0800327 s->verify_result = X509_V_OK;
328 s->method = ctx->method;
Adam Langley95c29f32014-06-20 12:00:00 -0700329
Adam Langleyfcf25832014-12-18 17:42:32 -0800330 if (!s->method->ssl_new(s)) {
331 goto err;
332 }
333 s->enc_method = ssl3_get_enc_method(s->version);
334 assert(s->enc_method != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700335
David Benjamin62fd1622015-01-11 13:30:01 -0500336 s->rwstate = SSL_NOTHING;
337 s->rstate = SSL_ST_READ_HEADER;
Adam Langley95c29f32014-06-20 12:00:00 -0700338
David Benjamin9f33fc62015-04-15 17:29:53 -0400339 CRYPTO_new_ex_data(&g_ex_data_class_ssl, s, &s->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700340
Adam Langleyfcf25832014-12-18 17:42:32 -0800341 s->psk_identity_hint = NULL;
342 if (ctx->psk_identity_hint) {
343 s->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
344 if (s->psk_identity_hint == NULL) {
345 goto err;
346 }
347 }
348 s->psk_client_callback = ctx->psk_client_callback;
349 s->psk_server_callback = ctx->psk_server_callback;
Adam Langley95c29f32014-06-20 12:00:00 -0700350
David Benjamin02ddbfd2015-01-11 13:09:11 -0500351 s->tlsext_channel_id_enabled = ctx->tlsext_channel_id_enabled;
352 if (ctx->tlsext_channel_id_private) {
353 s->tlsext_channel_id_private = EVP_PKEY_dup(ctx->tlsext_channel_id_private);
354 }
355
Adam Langleyfcf25832014-12-18 17:42:32 -0800356 s->signed_cert_timestamps_enabled = s->ctx->signed_cert_timestamps_enabled;
357 s->ocsp_stapling_enabled = s->ctx->ocsp_stapling_enabled;
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200358
Adam Langleyfcf25832014-12-18 17:42:32 -0800359 return s;
360
Adam Langley95c29f32014-06-20 12:00:00 -0700361err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400362 SSL_free(s);
Adam Langleyfcf25832014-12-18 17:42:32 -0800363 OPENSSL_PUT_ERROR(SSL, SSL_new, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700364
Adam Langleyfcf25832014-12-18 17:42:32 -0800365 return NULL;
366}
Adam Langley95c29f32014-06-20 12:00:00 -0700367
Adam Langleyfcf25832014-12-18 17:42:32 -0800368int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
369 unsigned int sid_ctx_len) {
370 if (sid_ctx_len > sizeof ctx->sid_ctx) {
371 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_session_id_context,
372 SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
373 return 0;
374 }
375 ctx->sid_ctx_length = sid_ctx_len;
376 memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700377
Adam Langleyfcf25832014-12-18 17:42:32 -0800378 return 1;
379}
Adam Langley95c29f32014-06-20 12:00:00 -0700380
Adam Langleyfcf25832014-12-18 17:42:32 -0800381int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
382 unsigned int sid_ctx_len) {
383 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
384 OPENSSL_PUT_ERROR(SSL, SSL_set_session_id_context,
385 SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
386 return 0;
387 }
388 ssl->sid_ctx_length = sid_ctx_len;
389 memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700390
Adam Langleyfcf25832014-12-18 17:42:32 -0800391 return 1;
392}
Adam Langley95c29f32014-06-20 12:00:00 -0700393
Adam Langleyfcf25832014-12-18 17:42:32 -0800394int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb) {
395 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
396 ctx->generate_session_id = cb;
397 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
398 return 1;
399}
Adam Langley95c29f32014-06-20 12:00:00 -0700400
Adam Langleyfcf25832014-12-18 17:42:32 -0800401int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800402 ssl->generate_session_id = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -0800403 return 1;
404}
Adam Langley95c29f32014-06-20 12:00:00 -0700405
Adam Langleyfcf25832014-12-18 17:42:32 -0800406int SSL_has_matching_session_id(const SSL *ssl, const uint8_t *id,
407 unsigned int id_len) {
408 /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how we
409 * can "construct" a session to give us the desired check - ie. to find if
410 * there's a session in the hash table that would conflict with any new
411 * session built out of this id/id_len and the ssl_version in use by this
412 * SSL. */
413 SSL_SESSION r, *p;
Adam Langley95c29f32014-06-20 12:00:00 -0700414
Adam Langleyfcf25832014-12-18 17:42:32 -0800415 if (id_len > sizeof r.session_id) {
416 return 0;
417 }
Adam Langley95c29f32014-06-20 12:00:00 -0700418
Adam Langleyfcf25832014-12-18 17:42:32 -0800419 r.ssl_version = ssl->version;
420 r.session_id_length = id_len;
421 memcpy(r.session_id, id, id_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700422
Adam Langleyfcf25832014-12-18 17:42:32 -0800423 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
424 p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r);
425 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
426 return p != NULL;
427}
Adam Langley95c29f32014-06-20 12:00:00 -0700428
Adam Langleyfcf25832014-12-18 17:42:32 -0800429int SSL_CTX_set_purpose(SSL_CTX *s, int purpose) {
430 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
431}
432
433int SSL_set_purpose(SSL *s, int purpose) {
434 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
435}
436
437int SSL_CTX_set_trust(SSL_CTX *s, int trust) {
438 return X509_VERIFY_PARAM_set_trust(s->param, trust);
439}
440
441int SSL_set_trust(SSL *s, int trust) {
442 return X509_VERIFY_PARAM_set_trust(s->param, trust);
443}
444
445int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm) {
446 return X509_VERIFY_PARAM_set1(ctx->param, vpm);
447}
448
449int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm) {
450 return X509_VERIFY_PARAM_set1(ssl->param, vpm);
451}
Adam Langley95c29f32014-06-20 12:00:00 -0700452
Adam Langley858a88d2014-06-20 12:00:00 -0700453void ssl_cipher_preference_list_free(
Adam Langleyfcf25832014-12-18 17:42:32 -0800454 struct ssl_cipher_preference_list_st *cipher_list) {
David Benjamin5d1ec732015-04-22 13:38:00 -0400455 if (cipher_list == NULL) {
456 return;
457 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800458 sk_SSL_CIPHER_free(cipher_list->ciphers);
459 OPENSSL_free(cipher_list->in_group_flags);
460 OPENSSL_free(cipher_list);
461}
Adam Langley858a88d2014-06-20 12:00:00 -0700462
Adam Langleyfcf25832014-12-18 17:42:32 -0800463struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_dup(
464 struct ssl_cipher_preference_list_st *cipher_list) {
465 struct ssl_cipher_preference_list_st *ret = NULL;
466 size_t n = sk_SSL_CIPHER_num(cipher_list->ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700467
Adam Langleyfcf25832014-12-18 17:42:32 -0800468 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
469 if (!ret) {
470 goto err;
471 }
472
473 ret->ciphers = NULL;
474 ret->in_group_flags = NULL;
475 ret->ciphers = sk_SSL_CIPHER_dup(cipher_list->ciphers);
476 if (!ret->ciphers) {
477 goto err;
478 }
479 ret->in_group_flags = BUF_memdup(cipher_list->in_group_flags, n);
480 if (!ret->in_group_flags) {
481 goto err;
482 }
483
484 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -0700485
486err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400487 ssl_cipher_preference_list_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -0800488 return NULL;
489}
Adam Langley858a88d2014-06-20 12:00:00 -0700490
Adam Langleyfcf25832014-12-18 17:42:32 -0800491struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_from_ciphers(
492 STACK_OF(SSL_CIPHER) * ciphers) {
493 struct ssl_cipher_preference_list_st *ret = NULL;
494 size_t n = sk_SSL_CIPHER_num(ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700495
Adam Langleyfcf25832014-12-18 17:42:32 -0800496 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
497 if (!ret) {
498 goto err;
499 }
500 ret->ciphers = NULL;
501 ret->in_group_flags = NULL;
502 ret->ciphers = sk_SSL_CIPHER_dup(ciphers);
503 if (!ret->ciphers) {
504 goto err;
505 }
506 ret->in_group_flags = OPENSSL_malloc(n);
507 if (!ret->in_group_flags) {
508 goto err;
509 }
510 memset(ret->in_group_flags, 0, n);
511 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -0700512
513err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400514 ssl_cipher_preference_list_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -0800515 return NULL;
516}
Adam Langley858a88d2014-06-20 12:00:00 -0700517
Adam Langleyfcf25832014-12-18 17:42:32 -0800518X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) { return ctx->param; }
Adam Langley95c29f32014-06-20 12:00:00 -0700519
Adam Langleyfcf25832014-12-18 17:42:32 -0800520X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) { return ssl->param; }
Adam Langley95c29f32014-06-20 12:00:00 -0700521
Adam Langleyfcf25832014-12-18 17:42:32 -0800522void SSL_certs_clear(SSL *s) { ssl_cert_clear_certs(s->cert); }
Adam Langley95c29f32014-06-20 12:00:00 -0700523
Adam Langleyfcf25832014-12-18 17:42:32 -0800524void SSL_free(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800525 if (s == NULL) {
526 return;
527 }
Adam Langley95c29f32014-06-20 12:00:00 -0700528
David Benjamin2755a3e2015-04-22 16:17:58 -0400529 X509_VERIFY_PARAM_free(s->param);
Adam Langley95c29f32014-06-20 12:00:00 -0700530
David Benjamin9f33fc62015-04-15 17:29:53 -0400531 CRYPTO_free_ex_data(&g_ex_data_class_ssl, s, &s->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700532
Adam Langleyfcf25832014-12-18 17:42:32 -0800533 if (s->bbio != NULL) {
534 /* If the buffering BIO is in place, pop it off */
535 if (s->bbio == s->wbio) {
536 s->wbio = BIO_pop(s->wbio);
537 }
538 BIO_free(s->bbio);
539 s->bbio = NULL;
540 }
Adam Langley95c29f32014-06-20 12:00:00 -0700541
David Benjamin2755a3e2015-04-22 16:17:58 -0400542 int free_wbio = s->wbio != s->rbio;
543 BIO_free_all(s->rbio);
544 if (free_wbio) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800545 BIO_free_all(s->wbio);
546 }
Adam Langley95c29f32014-06-20 12:00:00 -0700547
David Benjamin2755a3e2015-04-22 16:17:58 -0400548 BUF_MEM_free(s->init_buf);
Adam Langley95c29f32014-06-20 12:00:00 -0700549
Adam Langleyfcf25832014-12-18 17:42:32 -0800550 /* add extra stuff */
David Benjamin2755a3e2015-04-22 16:17:58 -0400551 ssl_cipher_preference_list_free(s->cipher_list);
552 sk_SSL_CIPHER_free(s->cipher_list_by_id);
Adam Langley95c29f32014-06-20 12:00:00 -0700553
David Benjamin2755a3e2015-04-22 16:17:58 -0400554 ssl_clear_bad_session(s);
555 SSL_SESSION_free(s->session);
Adam Langley95c29f32014-06-20 12:00:00 -0700556
Adam Langleyfcf25832014-12-18 17:42:32 -0800557 ssl_clear_cipher_ctx(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700558
David Benjamin2755a3e2015-04-22 16:17:58 -0400559 ssl_cert_free(s->cert);
Adam Langley0289c732014-06-20 12:00:00 -0700560
David Benjamin2755a3e2015-04-22 16:17:58 -0400561 OPENSSL_free(s->tlsext_hostname);
562 SSL_CTX_free(s->initial_ctx);
563 OPENSSL_free(s->tlsext_ecpointformatlist);
564 OPENSSL_free(s->tlsext_ellipticcurvelist);
565 OPENSSL_free(s->alpn_client_proto_list);
566 EVP_PKEY_free(s->tlsext_channel_id_private);
567 OPENSSL_free(s->psk_identity_hint);
568 sk_X509_NAME_pop_free(s->client_CA, X509_NAME_free);
569 OPENSSL_free(s->next_proto_negotiated);
570 sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);
Adam Langley95c29f32014-06-20 12:00:00 -0700571
Adam Langleyfcf25832014-12-18 17:42:32 -0800572 if (s->method != NULL) {
573 s->method->ssl_free(s);
574 }
David Benjamin2755a3e2015-04-22 16:17:58 -0400575 SSL_CTX_free(s->ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700576
Adam Langleyfcf25832014-12-18 17:42:32 -0800577 OPENSSL_free(s);
578}
Adam Langley95c29f32014-06-20 12:00:00 -0700579
Adam Langleyfcf25832014-12-18 17:42:32 -0800580void SSL_set_bio(SSL *s, BIO *rbio, BIO *wbio) {
581 /* If the output buffering BIO is still in place, remove it. */
582 if (s->bbio != NULL) {
583 if (s->wbio == s->bbio) {
584 s->wbio = s->wbio->next_bio;
585 s->bbio->next_bio = NULL;
586 }
587 }
Adam Langley95c29f32014-06-20 12:00:00 -0700588
David Benjamin2755a3e2015-04-22 16:17:58 -0400589 if (s->rbio != rbio) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800590 BIO_free_all(s->rbio);
591 }
David Benjamin2755a3e2015-04-22 16:17:58 -0400592 if (s->wbio != wbio && s->rbio != s->wbio) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800593 BIO_free_all(s->wbio);
594 }
595 s->rbio = rbio;
596 s->wbio = wbio;
597}
Adam Langley95c29f32014-06-20 12:00:00 -0700598
Adam Langleyfcf25832014-12-18 17:42:32 -0800599BIO *SSL_get_rbio(const SSL *s) { return s->rbio; }
Adam Langley95c29f32014-06-20 12:00:00 -0700600
Adam Langleyfcf25832014-12-18 17:42:32 -0800601BIO *SSL_get_wbio(const SSL *s) { return s->wbio; }
Adam Langley95c29f32014-06-20 12:00:00 -0700602
Adam Langleyfcf25832014-12-18 17:42:32 -0800603int SSL_get_fd(const SSL *s) { return SSL_get_rfd(s); }
Adam Langley95c29f32014-06-20 12:00:00 -0700604
Adam Langleyfcf25832014-12-18 17:42:32 -0800605int SSL_get_rfd(const SSL *s) {
606 int ret = -1;
607 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700608
Adam Langleyfcf25832014-12-18 17:42:32 -0800609 b = SSL_get_rbio(s);
610 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
611 if (r != NULL) {
612 BIO_get_fd(r, &ret);
613 }
614 return ret;
615}
Adam Langley95c29f32014-06-20 12:00:00 -0700616
Adam Langleyfcf25832014-12-18 17:42:32 -0800617int SSL_get_wfd(const SSL *s) {
618 int ret = -1;
619 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700620
Adam Langleyfcf25832014-12-18 17:42:32 -0800621 b = SSL_get_wbio(s);
622 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
623 if (r != NULL) {
624 BIO_get_fd(r, &ret);
625 }
Adam Langley95c29f32014-06-20 12:00:00 -0700626
Adam Langleyfcf25832014-12-18 17:42:32 -0800627 return ret;
628}
Adam Langley95c29f32014-06-20 12:00:00 -0700629
Adam Langleyfcf25832014-12-18 17:42:32 -0800630int SSL_set_fd(SSL *s, int fd) {
631 int ret = 0;
632 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700633
Adam Langleyfcf25832014-12-18 17:42:32 -0800634 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700635
Adam Langleyfcf25832014-12-18 17:42:32 -0800636 if (bio == NULL) {
637 OPENSSL_PUT_ERROR(SSL, SSL_set_fd, ERR_R_BUF_LIB);
638 goto err;
639 }
640 BIO_set_fd(bio, fd, BIO_NOCLOSE);
641 SSL_set_bio(s, bio, bio);
642 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700643
Adam Langley95c29f32014-06-20 12:00:00 -0700644err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800645 return ret;
646}
Adam Langley95c29f32014-06-20 12:00:00 -0700647
Adam Langleyfcf25832014-12-18 17:42:32 -0800648int SSL_set_wfd(SSL *s, int fd) {
649 int ret = 0;
650 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700651
Adam Langleyfcf25832014-12-18 17:42:32 -0800652 if (s->rbio == NULL || BIO_method_type(s->rbio) != BIO_TYPE_FD ||
653 (int)BIO_get_fd(s->rbio, NULL) != fd) {
654 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700655
Adam Langleyfcf25832014-12-18 17:42:32 -0800656 if (bio == NULL) {
657 OPENSSL_PUT_ERROR(SSL, SSL_set_wfd, ERR_R_BUF_LIB);
658 goto err;
659 }
660 BIO_set_fd(bio, fd, BIO_NOCLOSE);
661 SSL_set_bio(s, SSL_get_rbio(s), bio);
662 } else {
663 SSL_set_bio(s, SSL_get_rbio(s), SSL_get_rbio(s));
664 }
665
666 ret = 1;
667
Adam Langley95c29f32014-06-20 12:00:00 -0700668err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800669 return ret;
670}
Adam Langley95c29f32014-06-20 12:00:00 -0700671
Adam Langleyfcf25832014-12-18 17:42:32 -0800672int SSL_set_rfd(SSL *s, int fd) {
673 int ret = 0;
674 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700675
Adam Langleyfcf25832014-12-18 17:42:32 -0800676 if (s->wbio == NULL || BIO_method_type(s->wbio) != BIO_TYPE_FD ||
677 (int)BIO_get_fd(s->wbio, NULL) != fd) {
678 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700679
Adam Langleyfcf25832014-12-18 17:42:32 -0800680 if (bio == NULL) {
681 OPENSSL_PUT_ERROR(SSL, SSL_set_rfd, ERR_R_BUF_LIB);
682 goto err;
683 }
684 BIO_set_fd(bio, fd, BIO_NOCLOSE);
685 SSL_set_bio(s, bio, SSL_get_wbio(s));
686 } else {
687 SSL_set_bio(s, SSL_get_wbio(s), SSL_get_wbio(s));
688 }
689 ret = 1;
690
Adam Langley95c29f32014-06-20 12:00:00 -0700691err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800692 return ret;
693}
Adam Langley95c29f32014-06-20 12:00:00 -0700694
695/* return length of latest Finished message we sent, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -0800696size_t SSL_get_finished(const SSL *s, void *buf, size_t count) {
697 size_t ret = 0;
698
699 if (s->s3 != NULL) {
700 ret = s->s3->tmp.finish_md_len;
701 if (count > ret) {
702 count = ret;
703 }
704 memcpy(buf, s->s3->tmp.finish_md, count);
705 }
706
707 return ret;
708}
Adam Langley95c29f32014-06-20 12:00:00 -0700709
710/* return length of latest Finished message we expected, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -0800711size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count) {
712 size_t ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700713
Adam Langleyfcf25832014-12-18 17:42:32 -0800714 if (s->s3 != NULL) {
715 ret = s->s3->tmp.peer_finish_md_len;
716 if (count > ret) {
717 count = ret;
718 }
719 memcpy(buf, s->s3->tmp.peer_finish_md, count);
720 }
Adam Langley95c29f32014-06-20 12:00:00 -0700721
Adam Langleyfcf25832014-12-18 17:42:32 -0800722 return ret;
723}
Adam Langley95c29f32014-06-20 12:00:00 -0700724
Adam Langleyfcf25832014-12-18 17:42:32 -0800725int SSL_get_verify_mode(const SSL *s) { return s->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -0700726
Adam Langleyfcf25832014-12-18 17:42:32 -0800727int SSL_get_verify_depth(const SSL *s) {
728 return X509_VERIFY_PARAM_get_depth(s->param);
729}
Adam Langley95c29f32014-06-20 12:00:00 -0700730
Adam Langleyfcf25832014-12-18 17:42:32 -0800731int (*SSL_get_verify_callback(const SSL *s))(int, X509_STORE_CTX *) {
732 return s->verify_callback;
733}
Adam Langley95c29f32014-06-20 12:00:00 -0700734
Adam Langleyfcf25832014-12-18 17:42:32 -0800735int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) { return ctx->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -0700736
Adam Langleyfcf25832014-12-18 17:42:32 -0800737int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) {
738 return X509_VERIFY_PARAM_get_depth(ctx->param);
739}
Adam Langley95c29f32014-06-20 12:00:00 -0700740
Adam Langleyfcf25832014-12-18 17:42:32 -0800741int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int, X509_STORE_CTX *) {
742 return ctx->default_verify_callback;
743}
Adam Langley95c29f32014-06-20 12:00:00 -0700744
Adam Langleyfcf25832014-12-18 17:42:32 -0800745void SSL_set_verify(SSL *s, int mode,
746 int (*callback)(int ok, X509_STORE_CTX *ctx)) {
747 s->verify_mode = mode;
748 if (callback != NULL) {
749 s->verify_callback = callback;
750 }
751}
Adam Langley95c29f32014-06-20 12:00:00 -0700752
Adam Langleyfcf25832014-12-18 17:42:32 -0800753void SSL_set_verify_depth(SSL *s, int depth) {
754 X509_VERIFY_PARAM_set_depth(s->param, depth);
755}
Adam Langley95c29f32014-06-20 12:00:00 -0700756
David Benjamin61ecccf2015-05-05 09:44:51 -0400757int SSL_CTX_get_read_ahead(const SSL_CTX *ctx) { return ctx->read_ahead; }
Adam Langley95c29f32014-06-20 12:00:00 -0700758
Adam Langleyfcf25832014-12-18 17:42:32 -0800759int SSL_get_read_ahead(const SSL *s) { return s->read_ahead; }
Adam Langley95c29f32014-06-20 12:00:00 -0700760
David Benjamin61ecccf2015-05-05 09:44:51 -0400761void SSL_CTX_set_read_ahead(SSL_CTX *ctx, int yes) { ctx->read_ahead = !!yes; }
762
763void SSL_set_read_ahead(SSL *s, int yes) { s->read_ahead = !!yes; }
764
Adam Langleyfcf25832014-12-18 17:42:32 -0800765int SSL_pending(const SSL *s) {
766 /* SSL_pending cannot work properly if read-ahead is enabled
767 * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)), and it is
768 * impossible to fix since SSL_pending cannot report errors that may be
769 * observed while scanning the new data. (Note that SSL_pending() is often
770 * used as a boolean value, so we'd better not return -1.). */
771 return s->method->ssl_pending(s);
772}
Adam Langley95c29f32014-06-20 12:00:00 -0700773
Adam Langleyfcf25832014-12-18 17:42:32 -0800774X509 *SSL_get_peer_certificate(const SSL *s) {
775 X509 *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700776
Adam Langleyfcf25832014-12-18 17:42:32 -0800777 if (s == NULL || s->session == NULL) {
778 r = NULL;
779 } else {
780 r = s->session->peer;
781 }
Adam Langley95c29f32014-06-20 12:00:00 -0700782
Adam Langleyfcf25832014-12-18 17:42:32 -0800783 if (r == NULL) {
784 return NULL;
785 }
Adam Langley95c29f32014-06-20 12:00:00 -0700786
Adam Langleyfcf25832014-12-18 17:42:32 -0800787 return X509_up_ref(r);
788}
789
790STACK_OF(X509) * SSL_get_peer_cert_chain(const SSL *s) {
791 STACK_OF(X509) * r;
792
793 if (s == NULL || s->session == NULL || s->session->sess_cert == NULL) {
794 r = NULL;
795 } else {
796 r = s->session->sess_cert->cert_chain;
797 }
798
799 /* If we are a client, cert_chain includes the peer's own certificate; if we
800 * are a server, it does not. */
801 return r;
802}
Adam Langley95c29f32014-06-20 12:00:00 -0700803
Adam Langley95c29f32014-06-20 12:00:00 -0700804/* Fix this so it checks all the valid key/cert options */
Adam Langleyfcf25832014-12-18 17:42:32 -0800805int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
806 if (ctx == NULL || ctx->cert == NULL || ctx->cert->key->x509 == NULL) {
807 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key,
808 SSL_R_NO_CERTIFICATE_ASSIGNED);
809 return 0;
810 }
811
812 if (ctx->cert->key->privatekey == NULL) {
813 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key,
814 SSL_R_NO_PRIVATE_KEY_ASSIGNED);
815 return 0;
816 }
817
818 return X509_check_private_key(ctx->cert->key->x509,
819 ctx->cert->key->privatekey);
820}
Adam Langley95c29f32014-06-20 12:00:00 -0700821
822/* Fix this function so that it takes an optional type parameter */
Adam Langleyfcf25832014-12-18 17:42:32 -0800823int SSL_check_private_key(const SSL *ssl) {
824 if (ssl == NULL) {
825 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, ERR_R_PASSED_NULL_PARAMETER);
826 return 0;
827 }
Adam Langley95c29f32014-06-20 12:00:00 -0700828
Adam Langleyfcf25832014-12-18 17:42:32 -0800829 if (ssl->cert == NULL) {
830 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
831 SSL_R_NO_CERTIFICATE_ASSIGNED);
832 return 0;
833 }
Adam Langley95c29f32014-06-20 12:00:00 -0700834
Adam Langleyfcf25832014-12-18 17:42:32 -0800835 if (ssl->cert->key->x509 == NULL) {
836 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
837 SSL_R_NO_CERTIFICATE_ASSIGNED);
838 return 0;
839 }
David Benjamin0b145c22014-11-26 20:10:09 -0500840
Adam Langleyfcf25832014-12-18 17:42:32 -0800841 if (ssl->cert->key->privatekey == NULL) {
842 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
843 SSL_R_NO_PRIVATE_KEY_ASSIGNED);
844 return 0;
845 }
Adam Langley95c29f32014-06-20 12:00:00 -0700846
Adam Langleyfcf25832014-12-18 17:42:32 -0800847 return X509_check_private_key(ssl->cert->key->x509,
848 ssl->cert->key->privatekey);
849}
Adam Langley95c29f32014-06-20 12:00:00 -0700850
Adam Langleyfcf25832014-12-18 17:42:32 -0800851int SSL_accept(SSL *s) {
852 if (s->handshake_func == 0) {
853 /* Not properly initialized yet */
854 SSL_set_accept_state(s);
855 }
David Benjamin0b145c22014-11-26 20:10:09 -0500856
Adam Langleyfcf25832014-12-18 17:42:32 -0800857 if (s->handshake_func != s->method->ssl_accept) {
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +0100858 OPENSSL_PUT_ERROR(SSL, SSL_accept, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800859 return -1;
860 }
Adam Langley95c29f32014-06-20 12:00:00 -0700861
Adam Langleyfcf25832014-12-18 17:42:32 -0800862 return s->handshake_func(s);
863}
Adam Langley95c29f32014-06-20 12:00:00 -0700864
Adam Langleyfcf25832014-12-18 17:42:32 -0800865int SSL_connect(SSL *s) {
866 if (s->handshake_func == 0) {
867 /* Not properly initialized yet */
868 SSL_set_connect_state(s);
869 }
Adam Langley95c29f32014-06-20 12:00:00 -0700870
Adam Langleyfcf25832014-12-18 17:42:32 -0800871 if (s->handshake_func != s->method->ssl_connect) {
872 OPENSSL_PUT_ERROR(SSL, SSL_connect, ERR_R_INTERNAL_ERROR);
873 return -1;
874 }
Adam Langley95c29f32014-06-20 12:00:00 -0700875
Adam Langleyfcf25832014-12-18 17:42:32 -0800876 return s->handshake_func(s);
877}
Adam Langley95c29f32014-06-20 12:00:00 -0700878
Adam Langleyfcf25832014-12-18 17:42:32 -0800879long SSL_get_default_timeout(const SSL *s) {
880 return SSL_DEFAULT_SESSION_TIMEOUT;
881}
Adam Langley95c29f32014-06-20 12:00:00 -0700882
Adam Langleyfcf25832014-12-18 17:42:32 -0800883int SSL_read(SSL *s, void *buf, int num) {
884 if (s->handshake_func == 0) {
885 OPENSSL_PUT_ERROR(SSL, SSL_read, SSL_R_UNINITIALIZED);
886 return -1;
887 }
Adam Langley95c29f32014-06-20 12:00:00 -0700888
Adam Langleyfcf25832014-12-18 17:42:32 -0800889 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
890 s->rwstate = SSL_NOTHING;
891 return 0;
892 }
Adam Langley95c29f32014-06-20 12:00:00 -0700893
Adam Langleyfcf25832014-12-18 17:42:32 -0800894 return s->method->ssl_read(s, buf, num);
895}
Adam Langley95c29f32014-06-20 12:00:00 -0700896
Adam Langleyfcf25832014-12-18 17:42:32 -0800897int SSL_peek(SSL *s, void *buf, int num) {
898 if (s->handshake_func == 0) {
899 OPENSSL_PUT_ERROR(SSL, SSL_peek, SSL_R_UNINITIALIZED);
900 return -1;
901 }
Adam Langley95c29f32014-06-20 12:00:00 -0700902
Adam Langleyfcf25832014-12-18 17:42:32 -0800903 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
904 return 0;
905 }
Adam Langley95c29f32014-06-20 12:00:00 -0700906
Adam Langleyfcf25832014-12-18 17:42:32 -0800907 return s->method->ssl_peek(s, buf, num);
908}
Adam Langley95c29f32014-06-20 12:00:00 -0700909
Adam Langleyfcf25832014-12-18 17:42:32 -0800910int SSL_write(SSL *s, const void *buf, int num) {
911 if (s->handshake_func == 0) {
912 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_UNINITIALIZED);
913 return -1;
914 }
Adam Langley95c29f32014-06-20 12:00:00 -0700915
Adam Langleyfcf25832014-12-18 17:42:32 -0800916 if (s->shutdown & SSL_SENT_SHUTDOWN) {
917 s->rwstate = SSL_NOTHING;
918 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_PROTOCOL_IS_SHUTDOWN);
919 return -1;
920 }
Adam Langley95c29f32014-06-20 12:00:00 -0700921
Adam Langleyfcf25832014-12-18 17:42:32 -0800922 return s->method->ssl_write(s, buf, num);
923}
Adam Langley95c29f32014-06-20 12:00:00 -0700924
Adam Langleyfcf25832014-12-18 17:42:32 -0800925int SSL_shutdown(SSL *s) {
926 /* Note that this function behaves differently from what one might expect.
927 * Return values are 0 for no success (yet), 1 for success; but calling it
928 * once is usually not enough, even if blocking I/O is used (see
929 * ssl3_shutdown). */
Adam Langley95c29f32014-06-20 12:00:00 -0700930
Adam Langleyfcf25832014-12-18 17:42:32 -0800931 if (s->handshake_func == 0) {
932 OPENSSL_PUT_ERROR(SSL, SSL_shutdown, SSL_R_UNINITIALIZED);
933 return -1;
934 }
Adam Langley95c29f32014-06-20 12:00:00 -0700935
Adam Langleyfcf25832014-12-18 17:42:32 -0800936 if (!SSL_in_init(s)) {
937 return s->method->ssl_shutdown(s);
938 }
Adam Langley95c29f32014-06-20 12:00:00 -0700939
Adam Langleyfcf25832014-12-18 17:42:32 -0800940 return 1;
941}
Adam Langley95c29f32014-06-20 12:00:00 -0700942
Adam Langleyfcf25832014-12-18 17:42:32 -0800943int SSL_renegotiate(SSL *s) {
David Benjamin2cdace92015-01-24 13:30:02 -0500944 if (SSL_IS_DTLS(s)) {
945 /* Renegotiation is not supported for DTLS. */
946 OPENSSL_PUT_ERROR(SSL, SSL_renegotiate, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
947 return 0;
948 }
949
Adam Langleyfcf25832014-12-18 17:42:32 -0800950 if (s->renegotiate == 0) {
951 s->renegotiate = 1;
952 }
Adam Langley95c29f32014-06-20 12:00:00 -0700953
Adam Langleyfcf25832014-12-18 17:42:32 -0800954 s->new_session = 1;
955 return s->method->ssl_renegotiate(s);
956}
Adam Langley95c29f32014-06-20 12:00:00 -0700957
Adam Langleyfcf25832014-12-18 17:42:32 -0800958int SSL_renegotiate_pending(SSL *s) {
959 /* becomes true when negotiation is requested; false again once a handshake
960 * has finished */
961 return s->renegotiate != 0;
962}
Adam Langley95c29f32014-06-20 12:00:00 -0700963
David Benjamin61ecccf2015-05-05 09:44:51 -0400964uint32_t SSL_CTX_set_options(SSL_CTX *ctx, uint32_t options) {
965 ctx->options |= options;
966 return ctx->options;
967}
968
969uint32_t SSL_set_options(SSL *ssl, uint32_t options) {
970 ssl->options |= options;
971 return ssl->options;
972}
973
974uint32_t SSL_CTX_clear_options(SSL_CTX *ctx, uint32_t options) {
975 ctx->options &= ~options;
976 return ctx->options;
977}
978
979uint32_t SSL_clear_options(SSL *ssl, uint32_t options) {
980 ssl->options &= ~options;
981 return ssl->options;
982}
983
984uint32_t SSL_CTX_get_options(const SSL_CTX *ctx) { return ctx->options; }
985
986uint32_t SSL_get_options(const SSL *ssl) { return ssl->options; }
987
988uint32_t SSL_CTX_set_mode(SSL_CTX *ctx, uint32_t mode) {
989 ctx->mode |= mode;
990 return ctx->mode;
991}
992
993uint32_t SSL_set_mode(SSL *ssl, uint32_t mode) {
994 ssl->mode |= mode;
995 return ssl->mode;
996}
997
998uint32_t SSL_CTX_clear_mode(SSL_CTX *ctx, uint32_t mode) {
999 ctx->mode &= ~mode;
1000 return ctx->mode;
1001}
1002
1003uint32_t SSL_clear_mode(SSL *ssl, uint32_t mode) {
1004 ssl->mode &= ~mode;
1005 return ssl->mode;
1006}
1007
1008uint32_t SSL_CTX_get_mode(const SSL_CTX *ctx) { return ctx->mode; }
1009
1010uint32_t SSL_get_mode(const SSL *ssl) { return ssl->mode; }
1011
Adam Langleyfcf25832014-12-18 17:42:32 -08001012long SSL_ctrl(SSL *s, int cmd, long larg, void *parg) {
1013 long l;
Adam Langley95c29f32014-06-20 12:00:00 -07001014
Adam Langleyfcf25832014-12-18 17:42:32 -08001015 switch (cmd) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001016 case SSL_CTRL_GET_MAX_CERT_LIST:
1017 return s->max_cert_list;
Adam Langley95c29f32014-06-20 12:00:00 -07001018
Adam Langleyfcf25832014-12-18 17:42:32 -08001019 case SSL_CTRL_SET_MAX_CERT_LIST:
1020 l = s->max_cert_list;
1021 s->max_cert_list = larg;
1022 return l;
Adam Langley95c29f32014-06-20 12:00:00 -07001023
Adam Langleyfcf25832014-12-18 17:42:32 -08001024 case SSL_CTRL_SET_MTU:
1025 if (larg < (long)dtls1_min_mtu()) {
1026 return 0;
1027 }
1028 if (SSL_IS_DTLS(s)) {
1029 s->d1->mtu = larg;
1030 return larg;
1031 }
1032 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001033
Adam Langleyfcf25832014-12-18 17:42:32 -08001034 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1035 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH) {
1036 return 0;
1037 }
1038 s->max_send_fragment = larg;
1039 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001040
Adam Langleyfcf25832014-12-18 17:42:32 -08001041 case SSL_CTRL_GET_RI_SUPPORT:
1042 if (s->s3) {
1043 return s->s3->send_connection_binding;
1044 }
1045 return 0;
1046
Adam Langleyfcf25832014-12-18 17:42:32 -08001047 case SSL_CTRL_GET_RAW_CIPHERLIST:
1048 if (parg) {
1049 if (s->cert->ciphers_raw == NULL) {
1050 return 0;
1051 }
1052 *(uint8_t **)parg = s->cert->ciphers_raw;
1053 return (int)s->cert->ciphers_rawlen;
1054 }
1055
1056 /* Passing a NULL |parg| returns the size of a single
1057 * cipher suite value. */
1058 return 2;
1059
1060 default:
1061 return s->method->ssl_ctrl(s, cmd, larg, parg);
1062 }
1063}
1064
Adam Langleyfcf25832014-12-18 17:42:32 -08001065LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) { return ctx->sessions; }
1066
1067long SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg) {
1068 long l;
1069
1070 switch (cmd) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001071 case SSL_CTRL_GET_MAX_CERT_LIST:
1072 return ctx->max_cert_list;
1073
1074 case SSL_CTRL_SET_MAX_CERT_LIST:
1075 l = ctx->max_cert_list;
1076 ctx->max_cert_list = larg;
1077 return l;
1078
1079 case SSL_CTRL_SET_SESS_CACHE_SIZE:
1080 l = ctx->session_cache_size;
1081 ctx->session_cache_size = larg;
1082 return l;
1083
1084 case SSL_CTRL_GET_SESS_CACHE_SIZE:
1085 return ctx->session_cache_size;
1086
1087 case SSL_CTRL_SET_SESS_CACHE_MODE:
1088 l = ctx->session_cache_mode;
1089 ctx->session_cache_mode = larg;
1090 return l;
1091
1092 case SSL_CTRL_GET_SESS_CACHE_MODE:
1093 return ctx->session_cache_mode;
1094
1095 case SSL_CTRL_SESS_NUMBER:
1096 return lh_SSL_SESSION_num_items(ctx->sessions);
1097
1098 case SSL_CTRL_SESS_CONNECT:
Adam Langleyfcf25832014-12-18 17:42:32 -08001099 case SSL_CTRL_SESS_CONNECT_GOOD:
Adam Langleyfcf25832014-12-18 17:42:32 -08001100 case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001101 case SSL_CTRL_SESS_ACCEPT:
Adam Langleyfcf25832014-12-18 17:42:32 -08001102 case SSL_CTRL_SESS_ACCEPT_GOOD:
Adam Langleyfcf25832014-12-18 17:42:32 -08001103 case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001104 case SSL_CTRL_SESS_HIT:
Adam Langleyfcf25832014-12-18 17:42:32 -08001105 case SSL_CTRL_SESS_CB_HIT:
Adam Langleyfcf25832014-12-18 17:42:32 -08001106 case SSL_CTRL_SESS_MISSES:
Adam Langleyfcf25832014-12-18 17:42:32 -08001107 case SSL_CTRL_SESS_TIMEOUTS:
Adam Langleyfcf25832014-12-18 17:42:32 -08001108 case SSL_CTRL_SESS_CACHE_FULL:
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001109 /* Statistics are no longer supported.
1110 * TODO(davidben): Try to remove the accessors altogether. */
1111 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001112
Adam Langleyfcf25832014-12-18 17:42:32 -08001113 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1114 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH) {
1115 return 0;
1116 }
1117 ctx->max_send_fragment = larg;
1118 return 1;
1119
Adam Langleyfcf25832014-12-18 17:42:32 -08001120 default:
1121 return ctx->method->ssl_ctx_ctrl(ctx, cmd, larg, parg);
1122 }
1123}
1124
Adam Langleyfcf25832014-12-18 17:42:32 -08001125int ssl_cipher_id_cmp(const void *in_a, const void *in_b) {
1126 long l;
1127 const SSL_CIPHER *a = in_a;
1128 const SSL_CIPHER *b = in_b;
1129 const long a_id = a->id;
1130 const long b_id = b->id;
1131
1132 l = a_id - b_id;
1133 if (l == 0L) {
1134 return 0;
1135 } else {
1136 return (l > 0) ? 1 : -1;
1137 }
1138}
1139
1140int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **ap, const SSL_CIPHER **bp) {
1141 long l;
1142 const long a_id = (*ap)->id;
1143 const long b_id = (*bp)->id;
1144
1145 l = a_id - b_id;
1146 if (l == 0) {
1147 return 0;
1148 } else {
1149 return (l > 0) ? 1 : -1;
1150 }
1151}
1152
1153/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001154 * preference */
Adam Langleyfcf25832014-12-18 17:42:32 -08001155STACK_OF(SSL_CIPHER) * SSL_get_ciphers(const SSL *s) {
1156 if (s == NULL) {
1157 return NULL;
1158 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001159
Adam Langleyfcf25832014-12-18 17:42:32 -08001160 if (s->cipher_list != NULL) {
1161 return s->cipher_list->ciphers;
1162 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001163
Adam Langleyfcf25832014-12-18 17:42:32 -08001164 if (s->version >= TLS1_1_VERSION && s->ctx != NULL &&
1165 s->ctx->cipher_list_tls11 != NULL) {
1166 return s->ctx->cipher_list_tls11->ciphers;
1167 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001168
Adam Langleyfcf25832014-12-18 17:42:32 -08001169 if (s->ctx != NULL && s->ctx->cipher_list != NULL) {
1170 return s->ctx->cipher_list->ciphers;
1171 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001172
Adam Langleyfcf25832014-12-18 17:42:32 -08001173 return NULL;
1174}
Adam Langley95c29f32014-06-20 12:00:00 -07001175
Adam Langleyfcf25832014-12-18 17:42:32 -08001176/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001177 * algorithm id */
Adam Langleyfcf25832014-12-18 17:42:32 -08001178STACK_OF(SSL_CIPHER) * ssl_get_ciphers_by_id(SSL *s) {
1179 if (s == NULL) {
1180 return NULL;
1181 }
Adam Langley95c29f32014-06-20 12:00:00 -07001182
Adam Langleyfcf25832014-12-18 17:42:32 -08001183 if (s->cipher_list_by_id != NULL) {
1184 return s->cipher_list_by_id;
1185 }
Adam Langley95c29f32014-06-20 12:00:00 -07001186
Adam Langleyfcf25832014-12-18 17:42:32 -08001187 if (s->ctx != NULL && s->ctx->cipher_list_by_id != NULL) {
1188 return s->ctx->cipher_list_by_id;
1189 }
Adam Langley95c29f32014-06-20 12:00:00 -07001190
Adam Langleyfcf25832014-12-18 17:42:32 -08001191 return NULL;
1192}
Adam Langley95c29f32014-06-20 12:00:00 -07001193
Adam Langleyfcf25832014-12-18 17:42:32 -08001194/* The old interface to get the same thing as SSL_get_ciphers() */
1195const char *SSL_get_cipher_list(const SSL *s, int n) {
1196 const SSL_CIPHER *c;
1197 STACK_OF(SSL_CIPHER) * sk;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001198
Adam Langleyfcf25832014-12-18 17:42:32 -08001199 if (s == NULL) {
1200 return NULL;
1201 }
Adam Langley95c29f32014-06-20 12:00:00 -07001202
Adam Langleyfcf25832014-12-18 17:42:32 -08001203 sk = SSL_get_ciphers(s);
1204 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
1205 return NULL;
1206 }
Adam Langley95c29f32014-06-20 12:00:00 -07001207
Adam Langleyfcf25832014-12-18 17:42:32 -08001208 c = sk_SSL_CIPHER_value(sk, n);
1209 if (c == NULL) {
1210 return NULL;
1211 }
Adam Langley95c29f32014-06-20 12:00:00 -07001212
Adam Langleyfcf25832014-12-18 17:42:32 -08001213 return c->name;
1214}
David Benjamin5491e3f2014-09-29 19:33:09 -04001215
Adam Langleyfcf25832014-12-18 17:42:32 -08001216/* specify the ciphers to be used by default by the SSL_CTX */
1217int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
1218 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001219
Adam Langleyfcf25832014-12-18 17:42:32 -08001220 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001221 &ctx->cipher_list_by_id, str);
Adam Langleyfcf25832014-12-18 17:42:32 -08001222 /* ssl_create_cipher_list may return an empty stack if it was unable to find
1223 * a cipher matching the given rule string (for example if the rule string
1224 * specifies a cipher which has been disabled). This is not an error as far
1225 * as ssl_create_cipher_list is concerned, and hence ctx->cipher_list and
1226 * ctx->cipher_list_by_id has been updated. */
1227 if (sk == NULL) {
1228 return 0;
1229 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1230 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1231 return 0;
1232 }
Adam Langley95c29f32014-06-20 12:00:00 -07001233
Adam Langleyfcf25832014-12-18 17:42:32 -08001234 return 1;
1235}
David Benjamin39482a12014-07-20 13:30:15 -04001236
Adam Langleyfcf25832014-12-18 17:42:32 -08001237int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str) {
1238 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001239
David Benjamin71f07942015-04-08 02:36:59 -04001240 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str);
Adam Langleyfcf25832014-12-18 17:42:32 -08001241 if (sk == NULL) {
1242 return 0;
1243 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1244 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list_tls11,
1245 SSL_R_NO_CIPHER_MATCH);
1246 return 0;
1247 }
David Benjamin9f2c0d72014-10-21 22:00:19 -04001248
Adam Langleyfcf25832014-12-18 17:42:32 -08001249 return 1;
1250}
Adam Langley95c29f32014-06-20 12:00:00 -07001251
Adam Langleyfcf25832014-12-18 17:42:32 -08001252/* specify the ciphers to be used by the SSL */
1253int SSL_set_cipher_list(SSL *s, const char *str) {
1254 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001255
Adam Langleyfcf25832014-12-18 17:42:32 -08001256 sk = ssl_create_cipher_list(s->ctx->method, &s->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001257 &s->cipher_list_by_id, str);
David Benjamin39482a12014-07-20 13:30:15 -04001258
Adam Langleyfcf25832014-12-18 17:42:32 -08001259 /* see comment in SSL_CTX_set_cipher_list */
1260 if (sk == NULL) {
1261 return 0;
1262 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1263 OPENSSL_PUT_ERROR(SSL, SSL_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1264 return 0;
1265 }
David Benjamin39482a12014-07-20 13:30:15 -04001266
Adam Langleyfcf25832014-12-18 17:42:32 -08001267 return 1;
1268}
Adam Langley95c29f32014-06-20 12:00:00 -07001269
Adam Langleyfcf25832014-12-18 17:42:32 -08001270int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, uint8_t *p) {
1271 size_t i;
1272 const SSL_CIPHER *c;
1273 CERT *ct = s->cert;
1274 uint8_t *q;
1275 /* Set disabled masks for this session */
1276 ssl_set_client_disabled(s);
Adam Langley29707792014-06-20 12:00:00 -07001277
Adam Langleyfcf25832014-12-18 17:42:32 -08001278 if (sk == NULL) {
1279 return 0;
1280 }
1281 q = p;
Adam Langley95c29f32014-06-20 12:00:00 -07001282
Adam Langleyfcf25832014-12-18 17:42:32 -08001283 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
1284 c = sk_SSL_CIPHER_value(sk, i);
1285 /* Skip disabled ciphers */
1286 if (c->algorithm_ssl & ct->mask_ssl ||
1287 c->algorithm_mkey & ct->mask_k ||
1288 c->algorithm_auth & ct->mask_a) {
1289 continue;
1290 }
1291 s2n(ssl3_get_cipher_value(c), p);
1292 }
1293
1294 /* If all ciphers were disabled, return the error to the caller. */
1295 if (p == q) {
1296 return 0;
1297 }
1298
1299 /* Add SCSVs. */
1300 if (!s->renegotiate) {
1301 s2n(SSL3_CK_SCSV & 0xffff, p);
1302 }
1303
Adam Langley5f0efe02015-02-20 13:03:16 -08001304 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001305 s2n(SSL3_CK_FALLBACK_SCSV & 0xffff, p);
1306 }
1307
1308 return p - q;
1309}
1310
1311STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs) {
1312 CBS cipher_suites = *cbs;
1313 const SSL_CIPHER *c;
1314 STACK_OF(SSL_CIPHER) * sk;
1315
1316 if (s->s3) {
1317 s->s3->send_connection_binding = 0;
1318 }
1319
1320 if (CBS_len(&cipher_suites) % 2 != 0) {
1321 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1322 SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
1323 return NULL;
1324 }
1325
1326 sk = sk_SSL_CIPHER_new_null();
1327 if (sk == NULL) {
1328 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1329 goto err;
1330 }
1331
1332 if (!CBS_stow(&cipher_suites, &s->cert->ciphers_raw,
1333 &s->cert->ciphers_rawlen)) {
1334 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1335 goto err;
1336 }
1337
1338 while (CBS_len(&cipher_suites) > 0) {
1339 uint16_t cipher_suite;
1340
1341 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
1342 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_INTERNAL_ERROR);
1343 goto err;
1344 }
1345
1346 /* Check for SCSV. */
1347 if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff)) {
1348 /* SCSV is fatal if renegotiating. */
1349 if (s->renegotiate) {
1350 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1351 SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1352 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1353 goto err;
1354 }
1355 s->s3->send_connection_binding = 1;
1356 continue;
1357 }
1358
1359 /* Check for FALLBACK_SCSV. */
1360 if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff)) {
1361 uint16_t max_version = ssl3_get_max_server_version(s);
1362 if (SSL_IS_DTLS(s) ? (uint16_t)s->version > max_version
1363 : (uint16_t)s->version < max_version) {
1364 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1365 SSL_R_INAPPROPRIATE_FALLBACK);
1366 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
1367 goto err;
1368 }
1369 continue;
1370 }
1371
1372 c = ssl3_get_cipher_by_value(cipher_suite);
1373 if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
1374 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1375 goto err;
1376 }
1377 }
1378
1379 return sk;
David Benjamin9f2c0d72014-10-21 22:00:19 -04001380
Adam Langley95c29f32014-06-20 12:00:00 -07001381err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001382 sk_SSL_CIPHER_free(sk);
Adam Langleyfcf25832014-12-18 17:42:32 -08001383 return NULL;
1384}
Adam Langley95c29f32014-06-20 12:00:00 -07001385
1386
Adam Langleyfcf25832014-12-18 17:42:32 -08001387/* return a servername extension value if provided in Client Hello, or NULL. So
1388 * far, only host_name types are defined (RFC 3546). */
1389const char *SSL_get_servername(const SSL *s, const int type) {
1390 if (type != TLSEXT_NAMETYPE_host_name) {
1391 return NULL;
1392 }
Adam Langley95c29f32014-06-20 12:00:00 -07001393
Adam Langleyfcf25832014-12-18 17:42:32 -08001394 return s->session && !s->tlsext_hostname ? s->session->tlsext_hostname
1395 : s->tlsext_hostname;
1396}
Adam Langley95c29f32014-06-20 12:00:00 -07001397
Adam Langleyfcf25832014-12-18 17:42:32 -08001398int SSL_get_servername_type(const SSL *s) {
1399 if (s->session &&
1400 (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname)) {
1401 return TLSEXT_NAMETYPE_host_name;
1402 }
Adam Langley95c29f32014-06-20 12:00:00 -07001403
Adam Langleyfcf25832014-12-18 17:42:32 -08001404 return -1;
1405}
Adam Langley95c29f32014-06-20 12:00:00 -07001406
Adam Langleyfcf25832014-12-18 17:42:32 -08001407void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
1408 ctx->signed_cert_timestamps_enabled = 1;
1409}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001410
Adam Langleyfcf25832014-12-18 17:42:32 -08001411int SSL_enable_signed_cert_timestamps(SSL *ssl) {
1412 ssl->signed_cert_timestamps_enabled = 1;
1413 return 1;
1414}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001415
Adam Langleyfcf25832014-12-18 17:42:32 -08001416void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
1417 ctx->ocsp_stapling_enabled = 1;
1418}
David Benjamin6c7aed02014-08-27 16:42:38 -04001419
Adam Langleyfcf25832014-12-18 17:42:32 -08001420int SSL_enable_ocsp_stapling(SSL *ssl) {
1421 ssl->ocsp_stapling_enabled = 1;
1422 return 1;
1423}
David Benjamin6c7aed02014-08-27 16:42:38 -04001424
Adam Langleyfcf25832014-12-18 17:42:32 -08001425void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
1426 size_t *out_len) {
1427 SSL_SESSION *session = ssl->session;
Adam Langley3cb50e02014-08-26 14:00:31 -07001428
Adam Langleyfcf25832014-12-18 17:42:32 -08001429 *out_len = 0;
1430 *out = NULL;
1431 if (ssl->server || !session || !session->tlsext_signed_cert_timestamp_list) {
1432 return;
1433 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001434
Adam Langleyfcf25832014-12-18 17:42:32 -08001435 *out = session->tlsext_signed_cert_timestamp_list;
1436 *out_len = session->tlsext_signed_cert_timestamp_list_length;
1437}
David Benjamin6c7aed02014-08-27 16:42:38 -04001438
Adam Langleyfcf25832014-12-18 17:42:32 -08001439void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
1440 size_t *out_len) {
1441 SSL_SESSION *session = ssl->session;
1442
1443 *out_len = 0;
1444 *out = NULL;
1445 if (ssl->server || !session || !session->ocsp_response) {
1446 return;
1447 }
1448 *out = session->ocsp_response;
1449 *out_len = session->ocsp_response_length;
1450}
David Benjamin6c7aed02014-08-27 16:42:38 -04001451
Adam Langley95c29f32014-06-20 12:00:00 -07001452/* SSL_select_next_proto implements the standard protocol selection. It is
1453 * expected that this function is called from the callback set by
1454 * SSL_CTX_set_next_proto_select_cb.
1455 *
1456 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1457 * strings. The length byte itself is not included in the length. A byte
1458 * string of length 0 is invalid. No byte string may be truncated.
1459 *
1460 * The current, but experimental algorithm for selecting the protocol is:
1461 *
1462 * 1) If the server doesn't support NPN then this is indicated to the
1463 * callback. In this case, the client application has to abort the connection
1464 * or have a default application level protocol.
1465 *
1466 * 2) If the server supports NPN, but advertises an empty list then the
1467 * client selects the first protcol in its list, but indicates via the
1468 * API that this fallback case was enacted.
1469 *
1470 * 3) Otherwise, the client finds the first protocol in the server's list
1471 * that it supports and selects this protocol. This is because it's
1472 * assumed that the server has better information about which protocol
1473 * a client should use.
1474 *
1475 * 4) If the client doesn't support any of the server's advertised
1476 * protocols, then this is treated the same as case 2.
1477 *
1478 * It returns either
1479 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1480 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1481 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001482int SSL_select_next_proto(uint8_t **out, uint8_t *outlen, const uint8_t *server,
1483 unsigned int server_len, const uint8_t *client,
1484 unsigned int client_len) {
1485 unsigned int i, j;
1486 const uint8_t *result;
1487 int status = OPENSSL_NPN_UNSUPPORTED;
Adam Langley95c29f32014-06-20 12:00:00 -07001488
Adam Langleyfcf25832014-12-18 17:42:32 -08001489 /* For each protocol in server preference order, see if we support it. */
1490 for (i = 0; i < server_len;) {
1491 for (j = 0; j < client_len;) {
1492 if (server[i] == client[j] &&
1493 memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
1494 /* We found a match */
1495 result = &server[i];
1496 status = OPENSSL_NPN_NEGOTIATED;
1497 goto found;
1498 }
1499 j += client[j];
1500 j++;
1501 }
1502 i += server[i];
1503 i++;
1504 }
Adam Langley95c29f32014-06-20 12:00:00 -07001505
Adam Langleyfcf25832014-12-18 17:42:32 -08001506 /* There's no overlap between our protocols and the server's list. */
1507 result = client;
1508 status = OPENSSL_NPN_NO_OVERLAP;
Adam Langley95c29f32014-06-20 12:00:00 -07001509
Adam Langleyfcf25832014-12-18 17:42:32 -08001510found:
1511 *out = (uint8_t *)result + 1;
1512 *outlen = result[0];
1513 return status;
1514}
Adam Langley95c29f32014-06-20 12:00:00 -07001515
Adam Langley95c29f32014-06-20 12:00:00 -07001516/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
1517 * requested protocol for this connection and returns 0. If the client didn't
1518 * request any protocol, then *data is set to NULL.
1519 *
1520 * Note that the client can request any protocol it chooses. The value returned
1521 * from this function need not be a member of the list of supported protocols
Adam Langleyfcf25832014-12-18 17:42:32 -08001522 * provided by the callback. */
1523void SSL_get0_next_proto_negotiated(const SSL *s, const uint8_t **data,
1524 unsigned *len) {
1525 *data = s->next_proto_negotiated;
1526 if (!*data) {
1527 *len = 0;
1528 } else {
1529 *len = s->next_proto_negotiated_len;
1530 }
Adam Langley95c29f32014-06-20 12:00:00 -07001531}
1532
1533/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
1534 * TLS server needs a list of supported protocols for Next Protocol
1535 * Negotiation. The returned list must be in wire format. The list is returned
1536 * by setting |out| to point to it and |outlen| to its length. This memory will
1537 * not be modified, but one should assume that the SSL* keeps a reference to
1538 * it.
1539 *
Adam Langleyfcf25832014-12-18 17:42:32 -08001540 * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise.
1541 * Otherwise, no such extension will be included in the ServerHello. */
1542void SSL_CTX_set_next_protos_advertised_cb(
1543 SSL_CTX *ctx,
1544 int (*cb)(SSL *ssl, const uint8_t **out, unsigned int *outlen, void *arg),
1545 void *arg) {
1546 ctx->next_protos_advertised_cb = cb;
1547 ctx->next_protos_advertised_cb_arg = arg;
1548}
Adam Langley95c29f32014-06-20 12:00:00 -07001549
1550/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1551 * client needs to select a protocol from the server's provided list. |out|
1552 * must be set to point to the selected protocol (which may be within |in|).
1553 * The length of the protocol name must be written into |outlen|. The server's
1554 * advertised protocols are provided in |in| and |inlen|. The callback can
1555 * assume that |in| is syntactically valid.
1556 *
1557 * The client must select a protocol. It is fatal to the connection if this
1558 * callback returns a value other than SSL_TLSEXT_ERR_OK.
1559 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001560void SSL_CTX_set_next_proto_select_cb(
1561 SSL_CTX *ctx, int (*cb)(SSL *s, uint8_t **out, uint8_t *outlen,
1562 const uint8_t *in, unsigned int inlen, void *arg),
1563 void *arg) {
1564 ctx->next_proto_select_cb = cb;
1565 ctx->next_proto_select_cb_arg = arg;
1566}
Adam Langley95c29f32014-06-20 12:00:00 -07001567
Adam Langleyfcf25832014-12-18 17:42:32 -08001568int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
1569 unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001570 OPENSSL_free(ctx->alpn_client_proto_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001571 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1572 if (!ctx->alpn_client_proto_list) {
1573 return 1;
1574 }
1575 ctx->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001576
Adam Langleyfcf25832014-12-18 17:42:32 -08001577 return 0;
1578}
Adam Langley95c29f32014-06-20 12:00:00 -07001579
Adam Langleyfcf25832014-12-18 17:42:32 -08001580int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001581 OPENSSL_free(ssl->alpn_client_proto_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001582 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1583 if (!ssl->alpn_client_proto_list) {
1584 return 1;
1585 }
1586 ssl->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001587
Adam Langleyfcf25832014-12-18 17:42:32 -08001588 return 0;
1589}
Adam Langley95c29f32014-06-20 12:00:00 -07001590
1591/* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
1592 * during ClientHello processing in order to select an ALPN protocol from the
1593 * client's list of offered protocols. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001594void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
1595 int (*cb)(SSL *ssl, const uint8_t **out,
1596 uint8_t *outlen, const uint8_t *in,
1597 unsigned int inlen, void *arg),
1598 void *arg) {
1599 ctx->alpn_select_cb = cb;
1600 ctx->alpn_select_cb_arg = arg;
1601}
Adam Langley95c29f32014-06-20 12:00:00 -07001602
1603/* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
1604 * On return it sets |*data| to point to |*len| bytes of protocol name (not
1605 * including the leading length-prefix byte). If the server didn't respond with
1606 * a negotiated protocol then |*len| will be zero. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001607void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **data,
1608 unsigned *len) {
1609 *data = NULL;
1610 if (ssl->s3) {
1611 *data = ssl->s3->alpn_selected;
1612 }
1613 if (*data == NULL) {
1614 *len = 0;
1615 } else {
1616 *len = ssl->s3->alpn_selected_len;
1617 }
1618}
Adam Langley95c29f32014-06-20 12:00:00 -07001619
David Benjamincfd248b2015-04-03 11:02:24 -04001620int SSL_export_keying_material(SSL *s, uint8_t *out, size_t out_len,
1621 const char *label, size_t label_len,
1622 const uint8_t *context, size_t context_len,
1623 int use_context) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001624 if (s->version < TLS1_VERSION) {
David Benjaminc565ebb2015-04-03 04:06:36 -04001625 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001626 }
Adam Langley95c29f32014-06-20 12:00:00 -07001627
David Benjamincfd248b2015-04-03 11:02:24 -04001628 return s->enc_method->export_keying_material(
1629 s, out, out_len, label, label_len, context, context_len, use_context);
Adam Langleyfcf25832014-12-18 17:42:32 -08001630}
Adam Langley95c29f32014-06-20 12:00:00 -07001631
Adam Langleyfcf25832014-12-18 17:42:32 -08001632static uint32_t ssl_session_hash(const SSL_SESSION *a) {
1633 uint32_t hash =
1634 ((uint32_t)a->session_id[0]) ||
1635 ((uint32_t)a->session_id[1] << 8) ||
1636 ((uint32_t)a->session_id[2] << 16) ||
1637 ((uint32_t)a->session_id[3] << 24);
Adam Langley95c29f32014-06-20 12:00:00 -07001638
Adam Langleyfcf25832014-12-18 17:42:32 -08001639 return hash;
1640}
Adam Langley95c29f32014-06-20 12:00:00 -07001641
1642/* NB: If this function (or indeed the hash function which uses a sort of
1643 * coarser function than this one) is changed, ensure
1644 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1645 * able to construct an SSL_SESSION that will collide with any existing session
1646 * with a matching session ID. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001647static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
1648 if (a->ssl_version != b->ssl_version) {
1649 return 1;
1650 }
Adam Langley95c29f32014-06-20 12:00:00 -07001651
Adam Langleyfcf25832014-12-18 17:42:32 -08001652 if (a->session_id_length != b->session_id_length) {
1653 return 1;
1654 }
Adam Langley95c29f32014-06-20 12:00:00 -07001655
Adam Langleyfcf25832014-12-18 17:42:32 -08001656 return memcmp(a->session_id, b->session_id, a->session_id_length);
1657}
Adam Langley95c29f32014-06-20 12:00:00 -07001658
Adam Langleyfcf25832014-12-18 17:42:32 -08001659SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth) {
1660 SSL_CTX *ret = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001661
Adam Langleyfcf25832014-12-18 17:42:32 -08001662 if (meth == NULL) {
1663 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_NULL_SSL_METHOD_PASSED);
1664 return NULL;
1665 }
Adam Langley95c29f32014-06-20 12:00:00 -07001666
Adam Langleyfcf25832014-12-18 17:42:32 -08001667 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
1668 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
1669 goto err;
1670 }
Adam Langley95c29f32014-06-20 12:00:00 -07001671
Adam Langleyfcf25832014-12-18 17:42:32 -08001672 ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1673 if (ret == NULL) {
1674 goto err;
1675 }
Adam Langley95c29f32014-06-20 12:00:00 -07001676
Adam Langleyfcf25832014-12-18 17:42:32 -08001677 memset(ret, 0, sizeof(SSL_CTX));
Adam Langley95c29f32014-06-20 12:00:00 -07001678
Adam Langleyfcf25832014-12-18 17:42:32 -08001679 ret->method = meth->method;
Adam Langley95c29f32014-06-20 12:00:00 -07001680
Adam Langleyfcf25832014-12-18 17:42:32 -08001681 ret->cert_store = NULL;
1682 ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
1683 ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1684 ret->session_cache_head = NULL;
1685 ret->session_cache_tail = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001686
Adam Langleyfcf25832014-12-18 17:42:32 -08001687 /* We take the system default */
1688 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001689
Adam Langleyfcf25832014-12-18 17:42:32 -08001690 ret->new_session_cb = 0;
1691 ret->remove_session_cb = 0;
1692 ret->get_session_cb = 0;
1693 ret->generate_session_id = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001694
Adam Langleyfcf25832014-12-18 17:42:32 -08001695 ret->references = 1;
1696 ret->quiet_shutdown = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001697
Adam Langleyfcf25832014-12-18 17:42:32 -08001698 ret->info_callback = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001699
Adam Langleyfcf25832014-12-18 17:42:32 -08001700 ret->app_verify_callback = 0;
1701 ret->app_verify_arg = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001702
Adam Langleyfcf25832014-12-18 17:42:32 -08001703 ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
1704 ret->read_ahead = 0;
1705 ret->msg_callback = 0;
1706 ret->msg_callback_arg = NULL;
1707 ret->verify_mode = SSL_VERIFY_NONE;
1708 ret->sid_ctx_length = 0;
1709 ret->default_verify_callback = NULL;
1710 ret->cert = ssl_cert_new();
1711 if (ret->cert == NULL) {
1712 goto err;
1713 }
Adam Langley95c29f32014-06-20 12:00:00 -07001714
Adam Langleyfcf25832014-12-18 17:42:32 -08001715 ret->default_passwd_callback = 0;
1716 ret->default_passwd_callback_userdata = NULL;
1717 ret->client_cert_cb = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001718
Adam Langleyfcf25832014-12-18 17:42:32 -08001719 ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
1720 if (ret->sessions == NULL) {
1721 goto err;
1722 }
1723 ret->cert_store = X509_STORE_new();
1724 if (ret->cert_store == NULL) {
1725 goto err;
1726 }
Adam Langley95c29f32014-06-20 12:00:00 -07001727
Adam Langleyfcf25832014-12-18 17:42:32 -08001728 ssl_create_cipher_list(ret->method, &ret->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001729 &ret->cipher_list_by_id, SSL_DEFAULT_CIPHER_LIST);
Adam Langleyfcf25832014-12-18 17:42:32 -08001730 if (ret->cipher_list == NULL ||
1731 sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0) {
1732 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_LIBRARY_HAS_NO_CIPHERS);
1733 goto err2;
1734 }
Adam Langley95c29f32014-06-20 12:00:00 -07001735
Adam Langleyfcf25832014-12-18 17:42:32 -08001736 ret->param = X509_VERIFY_PARAM_new();
1737 if (!ret->param) {
1738 goto err;
1739 }
Adam Langley95c29f32014-06-20 12:00:00 -07001740
Adam Langleyfcf25832014-12-18 17:42:32 -08001741 ret->client_CA = sk_X509_NAME_new_null();
1742 if (ret->client_CA == NULL) {
1743 goto err;
1744 }
Adam Langley95c29f32014-06-20 12:00:00 -07001745
David Benjamin9f33fc62015-04-15 17:29:53 -04001746 CRYPTO_new_ex_data(&g_ex_data_class_ssl_ctx, ret, &ret->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001747
Adam Langleyfcf25832014-12-18 17:42:32 -08001748 ret->extra_certs = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001749
Adam Langleyfcf25832014-12-18 17:42:32 -08001750 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
David Benjamin422d3a42014-08-20 11:09:03 -04001751
Adam Langleyfcf25832014-12-18 17:42:32 -08001752 ret->tlsext_servername_callback = 0;
1753 ret->tlsext_servername_arg = NULL;
1754 /* Setup RFC4507 ticket keys */
1755 if (!RAND_bytes(ret->tlsext_tick_key_name, 16) ||
1756 !RAND_bytes(ret->tlsext_tick_hmac_key, 16) ||
1757 !RAND_bytes(ret->tlsext_tick_aes_key, 16)) {
1758 ret->options |= SSL_OP_NO_TICKET;
1759 }
Adam Langley95c29f32014-06-20 12:00:00 -07001760
Adam Langleyfcf25832014-12-18 17:42:32 -08001761 ret->next_protos_advertised_cb = 0;
1762 ret->next_proto_select_cb = 0;
1763 ret->psk_identity_hint = NULL;
1764 ret->psk_client_callback = NULL;
1765 ret->psk_server_callback = NULL;
1766
1767 /* Default is to connect to non-RI servers. When RI is more widely deployed
1768 * might change this. */
1769 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
1770
1771 /* Lock the SSL_CTX to the specified version, for compatibility with legacy
1772 * uses of SSL_METHOD. */
1773 if (meth->version != 0) {
1774 SSL_CTX_set_max_version(ret, meth->version);
1775 SSL_CTX_set_min_version(ret, meth->version);
1776 }
1777
1778 return ret;
1779
Adam Langley95c29f32014-06-20 12:00:00 -07001780err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001781 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -07001782err2:
David Benjamin2755a3e2015-04-22 16:17:58 -04001783 SSL_CTX_free(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -08001784 return NULL;
1785}
Adam Langley95c29f32014-06-20 12:00:00 -07001786
David Benjamin4fcc2e22015-04-22 12:58:16 -04001787void SSL_CTX_free(SSL_CTX *ctx) {
1788 if (ctx == NULL ||
1789 CRYPTO_add(&ctx->references, -1, CRYPTO_LOCK_SSL_CTX) > 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001790 return;
1791 }
Adam Langley95c29f32014-06-20 12:00:00 -07001792
David Benjamin2755a3e2015-04-22 16:17:58 -04001793 X509_VERIFY_PARAM_free(ctx->param);
Adam Langley95c29f32014-06-20 12:00:00 -07001794
Adam Langleyfcf25832014-12-18 17:42:32 -08001795 /* Free internal session cache. However: the remove_cb() may reference the
1796 * ex_data of SSL_CTX, thus the ex_data store can only be removed after the
1797 * sessions were flushed. As the ex_data handling routines might also touch
1798 * the session cache, the most secure solution seems to be: empty (flush) the
1799 * cache, then free ex_data, then finally free the cache. (See ticket
1800 * [openssl.org #212].) */
David Benjamin2755a3e2015-04-22 16:17:58 -04001801 SSL_CTX_flush_sessions(ctx, 0);
Adam Langley95c29f32014-06-20 12:00:00 -07001802
David Benjamin4fcc2e22015-04-22 12:58:16 -04001803 CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, ctx, &ctx->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001804
David Benjamin2755a3e2015-04-22 16:17:58 -04001805 lh_SSL_SESSION_free(ctx->sessions);
1806 X509_STORE_free(ctx->cert_store);
1807 ssl_cipher_preference_list_free(ctx->cipher_list);
1808 sk_SSL_CIPHER_free(ctx->cipher_list_by_id);
1809 ssl_cipher_preference_list_free(ctx->cipher_list_tls11);
1810 ssl_cert_free(ctx->cert);
1811 sk_X509_NAME_pop_free(ctx->client_CA, X509_NAME_free);
1812 sk_X509_pop_free(ctx->extra_certs, X509_free);
1813 sk_SRTP_PROTECTION_PROFILE_free(ctx->srtp_profiles);
1814 OPENSSL_free(ctx->psk_identity_hint);
1815 OPENSSL_free(ctx->tlsext_ecpointformatlist);
1816 OPENSSL_free(ctx->tlsext_ellipticcurvelist);
1817 OPENSSL_free(ctx->alpn_client_proto_list);
1818 EVP_PKEY_free(ctx->tlsext_channel_id_private);
1819 BIO_free(ctx->keylog_bio);
Adam Langley95c29f32014-06-20 12:00:00 -07001820
David Benjamin4fcc2e22015-04-22 12:58:16 -04001821 OPENSSL_free(ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08001822}
Adam Langley95c29f32014-06-20 12:00:00 -07001823
Adam Langleyfcf25832014-12-18 17:42:32 -08001824void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) {
1825 ctx->default_passwd_callback = cb;
1826}
Adam Langley95c29f32014-06-20 12:00:00 -07001827
Adam Langleyfcf25832014-12-18 17:42:32 -08001828void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u) {
1829 ctx->default_passwd_callback_userdata = u;
1830}
Adam Langley95c29f32014-06-20 12:00:00 -07001831
Adam Langleyfcf25832014-12-18 17:42:32 -08001832void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
1833 int (*cb)(X509_STORE_CTX *, void *),
1834 void *arg) {
1835 ctx->app_verify_callback = cb;
1836 ctx->app_verify_arg = arg;
1837}
Adam Langley95c29f32014-06-20 12:00:00 -07001838
Adam Langleyfcf25832014-12-18 17:42:32 -08001839void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
1840 int (*cb)(int, X509_STORE_CTX *)) {
1841 ctx->verify_mode = mode;
1842 ctx->default_verify_callback = cb;
1843}
Adam Langley95c29f32014-06-20 12:00:00 -07001844
Adam Langleyfcf25832014-12-18 17:42:32 -08001845void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
1846 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
1847}
Adam Langley1258b6a2014-06-20 12:00:00 -07001848
Adam Langleyfcf25832014-12-18 17:42:32 -08001849void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb)(SSL *ssl, void *arg),
1850 void *arg) {
1851 ssl_cert_set_cert_cb(c->cert, cb, arg);
1852}
David Benjamin859ec3c2014-09-02 16:29:36 -04001853
Adam Langleyfcf25832014-12-18 17:42:32 -08001854void SSL_set_cert_cb(SSL *s, int (*cb)(SSL *ssl, void *arg), void *arg) {
1855 ssl_cert_set_cert_cb(s->cert, cb, arg);
1856}
Adam Langley95c29f32014-06-20 12:00:00 -07001857
Adam Langleyfcf25832014-12-18 17:42:32 -08001858static int ssl_has_key(SSL *s, size_t idx) {
1859 CERT_PKEY *cpk = &s->cert->pkeys[idx];
1860 return cpk->x509 && cpk->privatekey;
1861}
David Benjamin033e5f42014-11-13 18:47:41 -05001862
David Benjamin107db582015-04-08 00:41:59 -04001863void ssl_get_compatible_server_ciphers(SSL *s, uint32_t *out_mask_k,
1864 uint32_t *out_mask_a) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001865 CERT *c = s->cert;
1866 int rsa_enc, rsa_sign, dh_tmp;
David Benjamin107db582015-04-08 00:41:59 -04001867 uint32_t mask_k, mask_a;
Adam Langleyfcf25832014-12-18 17:42:32 -08001868 int have_ecc_cert, ecdsa_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001869 X509 *x;
Adam Langley95c29f32014-06-20 12:00:00 -07001870
Adam Langleyfcf25832014-12-18 17:42:32 -08001871 if (c == NULL) {
1872 /* TODO(davidben): Is this codepath possible? */
1873 *out_mask_k = 0;
1874 *out_mask_a = 0;
1875 return;
1876 }
Adam Langley95c29f32014-06-20 12:00:00 -07001877
Adam Langleyfcf25832014-12-18 17:42:32 -08001878 dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001879
Adam Langleyfcf25832014-12-18 17:42:32 -08001880 rsa_enc = ssl_has_key(s, SSL_PKEY_RSA_ENC);
1881 rsa_sign = ssl_has_key(s, SSL_PKEY_RSA_SIGN);
1882 have_ecc_cert = ssl_has_key(s, SSL_PKEY_ECC);
1883 mask_k = 0;
1884 mask_a = 0;
David Benjaminf31e6812014-11-13 18:05:55 -05001885
Adam Langleyfcf25832014-12-18 17:42:32 -08001886 if (rsa_enc) {
1887 mask_k |= SSL_kRSA;
1888 }
1889 if (dh_tmp) {
David Benjamin7061e282015-03-19 11:10:48 -04001890 mask_k |= SSL_kDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001891 }
1892 if (rsa_enc || rsa_sign) {
1893 mask_a |= SSL_aRSA;
1894 }
Adam Langley95c29f32014-06-20 12:00:00 -07001895
Adam Langleyfcf25832014-12-18 17:42:32 -08001896 /* An ECC certificate may be usable for ECDSA cipher suites depending on the
1897 * key usage extension and on the client's curve preferences. */
1898 if (have_ecc_cert) {
1899 x = c->pkeys[SSL_PKEY_ECC].x509;
1900 /* This call populates extension flags (ex_flags). */
1901 X509_check_purpose(x, -1, 0);
1902 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE)
1903 ? (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)
1904 : 1;
1905 if (!tls1_check_ec_cert(s, x)) {
1906 ecdsa_ok = 0;
1907 }
1908 if (ecdsa_ok) {
1909 mask_a |= SSL_aECDSA;
1910 }
1911 }
Adam Langley95c29f32014-06-20 12:00:00 -07001912
Adam Langleyfcf25832014-12-18 17:42:32 -08001913 /* If we are considering an ECC cipher suite that uses an ephemeral EC
1914 * key, check it. */
David Benjamindd978782015-04-24 15:20:13 -04001915 if (tls1_check_ec_tmp_key(s)) {
David Benjamin7061e282015-03-19 11:10:48 -04001916 mask_k |= SSL_kECDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001917 }
Adam Langley95c29f32014-06-20 12:00:00 -07001918
Adam Langleyfcf25832014-12-18 17:42:32 -08001919 /* PSK requires a server callback. */
1920 if (s->psk_server_callback != NULL) {
1921 mask_k |= SSL_kPSK;
1922 mask_a |= SSL_aPSK;
1923 }
Adam Langley95c29f32014-06-20 12:00:00 -07001924
Adam Langleyfcf25832014-12-18 17:42:32 -08001925 *out_mask_k = mask_k;
1926 *out_mask_a = mask_a;
1927}
Adam Langley95c29f32014-06-20 12:00:00 -07001928
1929/* This handy macro borrowed from crypto/x509v3/v3_purp.c */
1930#define ku_reject(x, usage) \
Adam Langleyfcf25832014-12-18 17:42:32 -08001931 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
Adam Langley95c29f32014-06-20 12:00:00 -07001932
Adam Langleyfcf25832014-12-18 17:42:32 -08001933int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001934 const SSL_CIPHER *cs = s->s3->tmp.new_cipher;
David Benjamin3fa27772015-04-17 22:32:19 -04001935 uint32_t alg_a = cs->algorithm_auth;
1936 int signature_nid = 0, md_nid = 0, pk_nid = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001937
Adam Langleyfcf25832014-12-18 17:42:32 -08001938 /* This call populates the ex_flags field correctly */
1939 X509_check_purpose(x, -1, 0);
1940 if (x->sig_alg && x->sig_alg->algorithm) {
1941 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
1942 OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
1943 }
1944 if (alg_a & SSL_aECDSA) {
1945 /* key usage, if present, must allow signing */
1946 if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE)) {
1947 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg,
1948 SSL_R_ECC_CERT_NOT_FOR_SIGNING);
1949 return 0;
1950 }
1951 }
Adam Langley95c29f32014-06-20 12:00:00 -07001952
Adam Langleyfcf25832014-12-18 17:42:32 -08001953 return 1; /* all checks are ok */
1954}
Adam Langley95c29f32014-06-20 12:00:00 -07001955
Adam Langleyfcf25832014-12-18 17:42:32 -08001956static int ssl_get_server_cert_index(const SSL *s) {
1957 int idx;
1958 idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1959 if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509) {
1960 idx = SSL_PKEY_RSA_SIGN;
1961 }
1962 if (idx == -1) {
1963 OPENSSL_PUT_ERROR(SSL, ssl_get_server_cert_index, ERR_R_INTERNAL_ERROR);
1964 }
1965 return idx;
1966}
Adam Langley95c29f32014-06-20 12:00:00 -07001967
Adam Langleyfcf25832014-12-18 17:42:32 -08001968CERT_PKEY *ssl_get_server_send_pkey(const SSL *s) {
1969 int i = ssl_get_server_cert_index(s);
Adam Langley95c29f32014-06-20 12:00:00 -07001970
Adam Langleyfcf25832014-12-18 17:42:32 -08001971 /* This may or may not be an error. */
1972 if (i < 0) {
1973 return NULL;
1974 }
Adam Langley95c29f32014-06-20 12:00:00 -07001975
Adam Langleyfcf25832014-12-18 17:42:32 -08001976 /* May be NULL. */
1977 return &s->cert->pkeys[i];
1978}
Adam Langley95c29f32014-06-20 12:00:00 -07001979
Adam Langleyfcf25832014-12-18 17:42:32 -08001980EVP_PKEY *ssl_get_sign_pkey(SSL *s, const SSL_CIPHER *cipher) {
David Benjamin3fa27772015-04-17 22:32:19 -04001981 uint32_t alg_a = cipher->algorithm_auth;
1982 CERT *c = s->cert;
Adam Langleyfcf25832014-12-18 17:42:32 -08001983 int idx = -1;
Adam Langley95c29f32014-06-20 12:00:00 -07001984
Adam Langleyfcf25832014-12-18 17:42:32 -08001985 if (alg_a & SSL_aRSA) {
1986 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL) {
1987 idx = SSL_PKEY_RSA_SIGN;
1988 } else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL) {
1989 idx = SSL_PKEY_RSA_ENC;
1990 }
1991 } else if ((alg_a & SSL_aECDSA) &&
1992 (c->pkeys[SSL_PKEY_ECC].privatekey != NULL)) {
1993 idx = SSL_PKEY_ECC;
1994 }
Adam Langley95c29f32014-06-20 12:00:00 -07001995
Adam Langleyfcf25832014-12-18 17:42:32 -08001996 if (idx == -1) {
1997 OPENSSL_PUT_ERROR(SSL, ssl_get_sign_pkey, ERR_R_INTERNAL_ERROR);
1998 return NULL;
1999 }
Adam Langley95c29f32014-06-20 12:00:00 -07002000
Adam Langleyfcf25832014-12-18 17:42:32 -08002001 return c->pkeys[idx].privatekey;
2002}
Adam Langley95c29f32014-06-20 12:00:00 -07002003
Adam Langleyfcf25832014-12-18 17:42:32 -08002004void ssl_update_cache(SSL *s, int mode) {
David Benjaminb6d0c6d2015-03-19 19:07:26 -04002005 /* Never cache sessions with empty session IDs. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002006 if (s->session->session_id_length == 0) {
2007 return;
2008 }
Adam Langley95c29f32014-06-20 12:00:00 -07002009
David Benjaminb6d0c6d2015-03-19 19:07:26 -04002010 SSL_CTX *ctx = s->initial_ctx;
2011 if ((ctx->session_cache_mode & mode) == mode && !s->hit &&
2012 ((ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) ||
2013 SSL_CTX_add_session(ctx, s->session)) &&
2014 ctx->new_session_cb != NULL) {
2015 /* Note: |new_session_cb| is called whether the internal session cache is
2016 * used or not. */
2017 if (!ctx->new_session_cb(s, SSL_SESSION_up_ref(s->session))) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002018 SSL_SESSION_free(s->session);
2019 }
2020 }
Adam Langley95c29f32014-06-20 12:00:00 -07002021
David Benjaminb6d0c6d2015-03-19 19:07:26 -04002022 if (!(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR) &&
2023 !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) &&
2024 (ctx->session_cache_mode & mode) == mode) {
2025 /* Automatically flush the internal session cache every 255 connections. */
2026 int flush_cache = 0;
2027 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2028 ctx->handshakes_since_cache_flush++;
2029 if (ctx->handshakes_since_cache_flush >= 255) {
2030 flush_cache = 1;
2031 ctx->handshakes_since_cache_flush = 0;
2032 }
2033 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
2034
2035 if (flush_cache) {
2036 SSL_CTX_flush_sessions(ctx, (unsigned long)time(NULL));
Adam Langleyfcf25832014-12-18 17:42:32 -08002037 }
2038 }
2039}
Adam Langley95c29f32014-06-20 12:00:00 -07002040
David Benjamin1a5c50f2015-03-11 16:22:37 -04002041int SSL_get_error(const SSL *s, int ret_code) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002042 int reason;
David Benjamin1a5c50f2015-03-11 16:22:37 -04002043 uint32_t err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002044 BIO *bio;
Adam Langley95c29f32014-06-20 12:00:00 -07002045
David Benjamin1a5c50f2015-03-11 16:22:37 -04002046 if (ret_code > 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002047 return SSL_ERROR_NONE;
2048 }
Adam Langley95c29f32014-06-20 12:00:00 -07002049
Adam Langleyfcf25832014-12-18 17:42:32 -08002050 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
2051 * where we do encode the error */
David Benjamin1a5c50f2015-03-11 16:22:37 -04002052 err = ERR_peek_error();
2053 if (err != 0) {
2054 if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002055 return SSL_ERROR_SYSCALL;
2056 }
2057 return SSL_ERROR_SSL;
2058 }
Adam Langley95c29f32014-06-20 12:00:00 -07002059
David Benjamin1a5c50f2015-03-11 16:22:37 -04002060 if (ret_code == 0) {
David Benjamin9a38e922015-01-22 16:06:11 -05002061 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2062 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) {
2063 /* The socket was cleanly shut down with a close_notify. */
2064 return SSL_ERROR_ZERO_RETURN;
2065 }
2066 /* An EOF was observed which violates the protocol, and the underlying
2067 * transport does not participate in the error queue. Bubble up to the
2068 * caller. */
2069 return SSL_ERROR_SYSCALL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002070 }
Adam Langleyb2ce0582014-06-20 12:00:00 -07002071
Adam Langleyfcf25832014-12-18 17:42:32 -08002072 if (SSL_want_session(s)) {
2073 return SSL_ERROR_PENDING_SESSION;
2074 }
Adam Langley95c29f32014-06-20 12:00:00 -07002075
Adam Langleyfcf25832014-12-18 17:42:32 -08002076 if (SSL_want_certificate(s)) {
2077 return SSL_ERROR_PENDING_CERTIFICATE;
2078 }
Adam Langley95c29f32014-06-20 12:00:00 -07002079
Adam Langleyfcf25832014-12-18 17:42:32 -08002080 if (SSL_want_read(s)) {
2081 bio = SSL_get_rbio(s);
2082 if (BIO_should_read(bio)) {
2083 return SSL_ERROR_WANT_READ;
2084 }
Adam Langley95c29f32014-06-20 12:00:00 -07002085
Adam Langleyfcf25832014-12-18 17:42:32 -08002086 if (BIO_should_write(bio)) {
2087 /* This one doesn't make too much sense ... We never try to write to the
2088 * rbio, and an application program where rbio and wbio are separate
2089 * couldn't even know what it should wait for. However if we ever set
2090 * s->rwstate incorrectly (so that we have SSL_want_read(s) instead of
2091 * SSL_want_write(s)) and rbio and wbio *are* the same, this test works
2092 * around that bug; so it might be safer to keep it. */
2093 return SSL_ERROR_WANT_WRITE;
2094 }
Adam Langley95c29f32014-06-20 12:00:00 -07002095
Adam Langleyfcf25832014-12-18 17:42:32 -08002096 if (BIO_should_io_special(bio)) {
2097 reason = BIO_get_retry_reason(bio);
2098 if (reason == BIO_RR_CONNECT) {
2099 return SSL_ERROR_WANT_CONNECT;
2100 }
Adam Langley95c29f32014-06-20 12:00:00 -07002101
Adam Langleyfcf25832014-12-18 17:42:32 -08002102 if (reason == BIO_RR_ACCEPT) {
2103 return SSL_ERROR_WANT_ACCEPT;
2104 }
Adam Langley95c29f32014-06-20 12:00:00 -07002105
Adam Langleyfcf25832014-12-18 17:42:32 -08002106 return SSL_ERROR_SYSCALL; /* unknown */
2107 }
2108 }
Adam Langley95c29f32014-06-20 12:00:00 -07002109
Adam Langleyfcf25832014-12-18 17:42:32 -08002110 if (SSL_want_write(s)) {
2111 bio = SSL_get_wbio(s);
2112 if (BIO_should_write(bio)) {
2113 return SSL_ERROR_WANT_WRITE;
2114 }
Adam Langley95c29f32014-06-20 12:00:00 -07002115
Adam Langleyfcf25832014-12-18 17:42:32 -08002116 if (BIO_should_read(bio)) {
2117 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2118 return SSL_ERROR_WANT_READ;
2119 }
Adam Langley95c29f32014-06-20 12:00:00 -07002120
Adam Langleyfcf25832014-12-18 17:42:32 -08002121 if (BIO_should_io_special(bio)) {
2122 reason = BIO_get_retry_reason(bio);
2123 if (reason == BIO_RR_CONNECT) {
2124 return SSL_ERROR_WANT_CONNECT;
2125 }
Adam Langley95c29f32014-06-20 12:00:00 -07002126
Adam Langleyfcf25832014-12-18 17:42:32 -08002127 if (reason == BIO_RR_ACCEPT) {
2128 return SSL_ERROR_WANT_ACCEPT;
2129 }
Adam Langley95c29f32014-06-20 12:00:00 -07002130
Adam Langleyfcf25832014-12-18 17:42:32 -08002131 return SSL_ERROR_SYSCALL;
2132 }
2133 }
Adam Langley95c29f32014-06-20 12:00:00 -07002134
Adam Langleyfcf25832014-12-18 17:42:32 -08002135 if (SSL_want_x509_lookup(s)) {
2136 return SSL_ERROR_WANT_X509_LOOKUP;
2137 }
Adam Langley95c29f32014-06-20 12:00:00 -07002138
Adam Langleyfcf25832014-12-18 17:42:32 -08002139 if (SSL_want_channel_id_lookup(s)) {
2140 return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
2141 }
Adam Langley0f4746e2014-08-13 12:26:32 -07002142
Adam Langleyfcf25832014-12-18 17:42:32 -08002143 return SSL_ERROR_SYSCALL;
2144}
Adam Langley0f4746e2014-08-13 12:26:32 -07002145
Adam Langleyfcf25832014-12-18 17:42:32 -08002146int SSL_do_handshake(SSL *s) {
2147 int ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002148
Adam Langleyfcf25832014-12-18 17:42:32 -08002149 if (s->handshake_func == NULL) {
2150 OPENSSL_PUT_ERROR(SSL, SSL_do_handshake, SSL_R_CONNECTION_TYPE_NOT_SET);
2151 return -1;
2152 }
Adam Langley95c29f32014-06-20 12:00:00 -07002153
Adam Langleyfcf25832014-12-18 17:42:32 -08002154 s->method->ssl_renegotiate_check(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002155
Adam Langleyfcf25832014-12-18 17:42:32 -08002156 if (SSL_in_init(s)) {
2157 ret = s->handshake_func(s);
2158 }
2159 return ret;
2160}
Adam Langley95c29f32014-06-20 12:00:00 -07002161
Adam Langleyfcf25832014-12-18 17:42:32 -08002162void SSL_set_accept_state(SSL *s) {
2163 s->server = 1;
2164 s->shutdown = 0;
2165 s->state = SSL_ST_ACCEPT | SSL_ST_BEFORE;
2166 s->handshake_func = s->method->ssl_accept;
2167 /* clear the current cipher */
2168 ssl_clear_cipher_ctx(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002169}
Adam Langley95c29f32014-06-20 12:00:00 -07002170
Adam Langleyfcf25832014-12-18 17:42:32 -08002171void SSL_set_connect_state(SSL *s) {
2172 s->server = 0;
2173 s->shutdown = 0;
2174 s->state = SSL_ST_CONNECT | SSL_ST_BEFORE;
2175 s->handshake_func = s->method->ssl_connect;
2176 /* clear the current cipher */
2177 ssl_clear_cipher_ctx(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002178}
Adam Langley95c29f32014-06-20 12:00:00 -07002179
Adam Langleyfcf25832014-12-18 17:42:32 -08002180int ssl_undefined_function(SSL *s) {
2181 OPENSSL_PUT_ERROR(SSL, ssl_undefined_function,
2182 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2183 return 0;
2184}
Adam Langley95c29f32014-06-20 12:00:00 -07002185
Adam Langleyfcf25832014-12-18 17:42:32 -08002186int ssl_undefined_void_function(void) {
2187 OPENSSL_PUT_ERROR(SSL, ssl_undefined_void_function,
2188 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2189 return 0;
2190}
Adam Langley95c29f32014-06-20 12:00:00 -07002191
Adam Langleyfcf25832014-12-18 17:42:32 -08002192int ssl_undefined_const_function(const SSL *s) {
2193 OPENSSL_PUT_ERROR(SSL, ssl_undefined_const_function,
2194 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2195 return 0;
2196}
Adam Langley95c29f32014-06-20 12:00:00 -07002197
Adam Langleyfcf25832014-12-18 17:42:32 -08002198static const char *ssl_get_version(int version) {
2199 switch (version) {
2200 case TLS1_2_VERSION:
2201 return "TLSv1.2";
Adam Langley95c29f32014-06-20 12:00:00 -07002202
Adam Langleyfcf25832014-12-18 17:42:32 -08002203 case TLS1_1_VERSION:
2204 return "TLSv1.1";
Adam Langley95c29f32014-06-20 12:00:00 -07002205
Adam Langleyfcf25832014-12-18 17:42:32 -08002206 case TLS1_VERSION:
2207 return "TLSv1";
Adam Langley95c29f32014-06-20 12:00:00 -07002208
Adam Langleyfcf25832014-12-18 17:42:32 -08002209 case SSL3_VERSION:
2210 return "SSLv3";
Adam Langley95c29f32014-06-20 12:00:00 -07002211
David Benjamin1c722b72015-04-20 13:53:10 -04002212 case DTLS1_VERSION:
2213 return "DTLSv1";
2214
2215 case DTLS1_2_VERSION:
2216 return "DTLSv1.2";
2217
Adam Langleyfcf25832014-12-18 17:42:32 -08002218 default:
2219 return "unknown";
2220 }
2221}
Adam Langley95c29f32014-06-20 12:00:00 -07002222
Adam Langleyfcf25832014-12-18 17:42:32 -08002223const char *SSL_get_version(const SSL *s) {
2224 return ssl_get_version(s->version);
2225}
Adam Langley95c29f32014-06-20 12:00:00 -07002226
Adam Langleyfcf25832014-12-18 17:42:32 -08002227const char *SSL_SESSION_get_version(const SSL_SESSION *sess) {
2228 return ssl_get_version(sess->ssl_version);
2229}
Adam Langley95c29f32014-06-20 12:00:00 -07002230
Adam Langleyfcf25832014-12-18 17:42:32 -08002231void ssl_clear_cipher_ctx(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002232 if (s->aead_read_ctx != NULL) {
2233 EVP_AEAD_CTX_cleanup(&s->aead_read_ctx->ctx);
2234 OPENSSL_free(s->aead_read_ctx);
2235 s->aead_read_ctx = NULL;
2236 }
Adam Langleya5dc5452014-06-20 12:00:00 -07002237
Adam Langleyfcf25832014-12-18 17:42:32 -08002238 if (s->aead_write_ctx != NULL) {
2239 EVP_AEAD_CTX_cleanup(&s->aead_write_ctx->ctx);
2240 OPENSSL_free(s->aead_write_ctx);
2241 s->aead_write_ctx = NULL;
2242 }
2243}
Adam Langley95c29f32014-06-20 12:00:00 -07002244
Adam Langleyfcf25832014-12-18 17:42:32 -08002245X509 *SSL_get_certificate(const SSL *s) {
2246 if (s->cert != NULL) {
2247 return s->cert->key->x509;
2248 }
2249
2250 return NULL;
2251}
2252
2253EVP_PKEY *SSL_get_privatekey(const SSL *s) {
2254 if (s->cert != NULL) {
2255 return s->cert->key->privatekey;
2256 }
2257
2258 return NULL;
2259}
2260
2261X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
2262 if (ctx->cert != NULL) {
2263 return ctx->cert->key->x509;
2264 }
2265
2266 return NULL;
2267}
2268
2269EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
2270 if (ctx->cert != NULL) {
2271 return ctx->cert->key->privatekey;
2272 }
2273
2274 return NULL;
2275}
2276
2277const SSL_CIPHER *SSL_get_current_cipher(const SSL *s) {
2278 if (s->session != NULL && s->session->cipher != NULL) {
2279 return s->session->cipher;
2280 }
2281
2282 return NULL;
2283}
2284
2285const void *SSL_get_current_compression(SSL *s) { return NULL; }
2286
2287const void *SSL_get_current_expansion(SSL *s) { return NULL; }
2288
2289int ssl_init_wbio_buffer(SSL *s, int push) {
2290 BIO *bbio;
2291
2292 if (s->bbio == NULL) {
2293 bbio = BIO_new(BIO_f_buffer());
2294 if (bbio == NULL) {
2295 return 0;
2296 }
2297 s->bbio = bbio;
2298 } else {
2299 bbio = s->bbio;
2300 if (s->bbio == s->wbio) {
2301 s->wbio = BIO_pop(s->wbio);
2302 }
2303 }
2304
2305 BIO_reset(bbio);
2306 if (!BIO_set_read_buffer_size(bbio, 1)) {
2307 OPENSSL_PUT_ERROR(SSL, ssl_init_wbio_buffer, ERR_R_BUF_LIB);
2308 return 0;
2309 }
2310
2311 if (push) {
2312 if (s->wbio != bbio) {
2313 s->wbio = BIO_push(bbio, s->wbio);
2314 }
2315 } else {
2316 if (s->wbio == bbio) {
2317 s->wbio = BIO_pop(bbio);
2318 }
2319 }
2320
2321 return 1;
2322}
2323
2324void ssl_free_wbio_buffer(SSL *s) {
2325 if (s->bbio == NULL) {
2326 return;
2327 }
2328
2329 if (s->bbio == s->wbio) {
2330 /* remove buffering */
2331 s->wbio = BIO_pop(s->wbio);
2332 }
2333
2334 BIO_free(s->bbio);
2335 s->bbio = NULL;
2336}
2337
2338void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
2339 ctx->quiet_shutdown = mode;
2340}
2341
2342int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
2343 return ctx->quiet_shutdown;
2344}
2345
2346void SSL_set_quiet_shutdown(SSL *s, int mode) { s->quiet_shutdown = mode; }
2347
2348int SSL_get_quiet_shutdown(const SSL *s) { return s->quiet_shutdown; }
2349
2350void SSL_set_shutdown(SSL *s, int mode) { s->shutdown = mode; }
2351
2352int SSL_get_shutdown(const SSL *s) { return s->shutdown; }
2353
2354int SSL_version(const SSL *s) { return s->version; }
2355
2356SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
2357
2358SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
2359 if (ssl->ctx == ctx) {
2360 return ssl->ctx;
2361 }
2362
2363 if (ctx == NULL) {
2364 ctx = ssl->initial_ctx;
2365 }
2366
David Benjamin2755a3e2015-04-22 16:17:58 -04002367 ssl_cert_free(ssl->cert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002368 ssl->cert = ssl_cert_dup(ctx->cert);
David Benjamin2755a3e2015-04-22 16:17:58 -04002369
Adam Langleyfcf25832014-12-18 17:42:32 -08002370 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
David Benjamin2755a3e2015-04-22 16:17:58 -04002371 SSL_CTX_free(ssl->ctx); /* decrement reference count */
Adam Langleyfcf25832014-12-18 17:42:32 -08002372 ssl->ctx = ctx;
2373
2374 ssl->sid_ctx_length = ctx->sid_ctx_length;
2375 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2376 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2377
2378 return ssl->ctx;
2379}
2380
2381int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
2382 return X509_STORE_set_default_paths(ctx->cert_store);
2383}
Adam Langley95c29f32014-06-20 12:00:00 -07002384
2385int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
Adam Langleyfcf25832014-12-18 17:42:32 -08002386 const char *CApath) {
2387 return X509_STORE_load_locations(ctx->cert_store, CAfile, CApath);
2388}
Adam Langley95c29f32014-06-20 12:00:00 -07002389
2390void SSL_set_info_callback(SSL *ssl,
Adam Langleyfcf25832014-12-18 17:42:32 -08002391 void (*cb)(const SSL *ssl, int type, int val)) {
2392 ssl->info_callback = cb;
2393}
Adam Langley95c29f32014-06-20 12:00:00 -07002394
Adam Langleyfcf25832014-12-18 17:42:32 -08002395void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/, int /*type*/,
2396 int /*val*/) {
2397 return ssl->info_callback;
2398}
Adam Langley95c29f32014-06-20 12:00:00 -07002399
Adam Langleyfcf25832014-12-18 17:42:32 -08002400int SSL_state(const SSL *ssl) { return ssl->state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002401
Adam Langleyfcf25832014-12-18 17:42:32 -08002402void SSL_set_state(SSL *ssl, int state) { ssl->state = state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002403
Adam Langleyfcf25832014-12-18 17:42:32 -08002404void SSL_set_verify_result(SSL *ssl, long arg) { ssl->verify_result = arg; }
Adam Langley95c29f32014-06-20 12:00:00 -07002405
Adam Langleyfcf25832014-12-18 17:42:32 -08002406long SSL_get_verify_result(const SSL *ssl) { return ssl->verify_result; }
Adam Langley95c29f32014-06-20 12:00:00 -07002407
Adam Langleyfcf25832014-12-18 17:42:32 -08002408int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2409 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002410 int index;
2411 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp,
2412 new_func, dup_func, free_func)) {
2413 return -1;
2414 }
2415 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002416}
Adam Langley95c29f32014-06-20 12:00:00 -07002417
Adam Langleyfcf25832014-12-18 17:42:32 -08002418int SSL_set_ex_data(SSL *s, int idx, void *arg) {
2419 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2420}
Adam Langley95c29f32014-06-20 12:00:00 -07002421
Adam Langleyfcf25832014-12-18 17:42:32 -08002422void *SSL_get_ex_data(const SSL *s, int idx) {
2423 return CRYPTO_get_ex_data(&s->ex_data, idx);
2424}
Adam Langley95c29f32014-06-20 12:00:00 -07002425
Adam Langleyfcf25832014-12-18 17:42:32 -08002426int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2427 CRYPTO_EX_dup *dup_func,
2428 CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -04002429 int index;
2430 if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp,
2431 new_func, dup_func, free_func)) {
2432 return -1;
2433 }
2434 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -08002435}
Adam Langley95c29f32014-06-20 12:00:00 -07002436
Adam Langleyfcf25832014-12-18 17:42:32 -08002437int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg) {
2438 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2439}
Adam Langley95c29f32014-06-20 12:00:00 -07002440
Adam Langleyfcf25832014-12-18 17:42:32 -08002441void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx) {
2442 return CRYPTO_get_ex_data(&s->ex_data, idx);
2443}
Adam Langley95c29f32014-06-20 12:00:00 -07002444
Adam Langleyfcf25832014-12-18 17:42:32 -08002445int ssl_ok(SSL *s) { return 1; }
Adam Langley95c29f32014-06-20 12:00:00 -07002446
Adam Langleyfcf25832014-12-18 17:42:32 -08002447X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
2448 return ctx->cert_store;
2449}
Adam Langley95c29f32014-06-20 12:00:00 -07002450
Adam Langleyfcf25832014-12-18 17:42:32 -08002451void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
David Benjamin2755a3e2015-04-22 16:17:58 -04002452 X509_STORE_free(ctx->cert_store);
Adam Langleyfcf25832014-12-18 17:42:32 -08002453 ctx->cert_store = store;
2454}
Adam Langley95c29f32014-06-20 12:00:00 -07002455
Adam Langleyfcf25832014-12-18 17:42:32 -08002456int SSL_want(const SSL *s) { return s->rwstate; }
Adam Langley95c29f32014-06-20 12:00:00 -07002457
Adam Langleyfcf25832014-12-18 17:42:32 -08002458void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
2459 RSA *(*cb)(SSL *ssl, int is_export,
2460 int keylength)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002461}
Adam Langley95c29f32014-06-20 12:00:00 -07002462
Adam Langleyfcf25832014-12-18 17:42:32 -08002463void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
2464 int keylength)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002465}
2466
2467void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
David Benjamin59015c32015-04-26 13:13:55 -04002468 DH *(*callback)(SSL *ssl, int is_export,
2469 int keylength)) {
2470 ctx->cert->dh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002471}
2472
David Benjamin59015c32015-04-26 13:13:55 -04002473void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*callback)(SSL *ssl, int is_export,
2474 int keylength)) {
2475 ssl->cert->dh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002476}
2477
2478void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,
David Benjamindd978782015-04-24 15:20:13 -04002479 EC_KEY *(*callback)(SSL *ssl, int is_export,
2480 int keylength)) {
David Benjamin59015c32015-04-26 13:13:55 -04002481 ctx->cert->ecdh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002482}
2483
2484void SSL_set_tmp_ecdh_callback(SSL *ssl,
David Benjamindd978782015-04-24 15:20:13 -04002485 EC_KEY *(*callback)(SSL *ssl, int is_export,
2486 int keylength)) {
David Benjamin59015c32015-04-26 13:13:55 -04002487 ssl->cert->ecdh_tmp_cb = callback;
Adam Langleyfcf25832014-12-18 17:42:32 -08002488}
2489
2490int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
2491 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2492 OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_psk_identity_hint,
2493 SSL_R_DATA_LENGTH_TOO_LONG);
2494 return 0;
2495 }
2496
David Benjamin2755a3e2015-04-22 16:17:58 -04002497 OPENSSL_free(ctx->psk_identity_hint);
Adam Langleyfcf25832014-12-18 17:42:32 -08002498
2499 if (identity_hint != NULL) {
2500 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2501 if (ctx->psk_identity_hint == NULL) {
2502 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002503 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002504 } else {
2505 ctx->psk_identity_hint = NULL;
2506 }
Adam Langley95c29f32014-06-20 12:00:00 -07002507
Adam Langleyfcf25832014-12-18 17:42:32 -08002508 return 1;
2509}
2510
2511int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint) {
2512 if (s == NULL) {
2513 return 0;
2514 }
2515
2516 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2517 OPENSSL_PUT_ERROR(SSL, SSL_use_psk_identity_hint,
2518 SSL_R_DATA_LENGTH_TOO_LONG);
2519 return 0;
2520 }
2521
2522 /* Clear currently configured hint, if any. */
David Benjamin2755a3e2015-04-22 16:17:58 -04002523 OPENSSL_free(s->psk_identity_hint);
2524 s->psk_identity_hint = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002525
2526 if (identity_hint != NULL) {
2527 s->psk_identity_hint = BUF_strdup(identity_hint);
2528 if (s->psk_identity_hint == NULL) {
2529 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002530 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002531 }
Adam Langley95c29f32014-06-20 12:00:00 -07002532
Adam Langleyfcf25832014-12-18 17:42:32 -08002533 return 1;
2534}
Adam Langley95c29f32014-06-20 12:00:00 -07002535
Adam Langleyfcf25832014-12-18 17:42:32 -08002536const char *SSL_get_psk_identity_hint(const SSL *s) {
2537 if (s == NULL) {
2538 return NULL;
2539 }
2540 return s->psk_identity_hint;
2541}
Adam Langley95c29f32014-06-20 12:00:00 -07002542
Adam Langleyfcf25832014-12-18 17:42:32 -08002543const char *SSL_get_psk_identity(const SSL *s) {
2544 if (s == NULL || s->session == NULL) {
2545 return NULL;
2546 }
Adam Langley95c29f32014-06-20 12:00:00 -07002547
Adam Langleyfcf25832014-12-18 17:42:32 -08002548 return s->session->psk_identity;
2549}
Adam Langley95c29f32014-06-20 12:00:00 -07002550
Adam Langleyfcf25832014-12-18 17:42:32 -08002551void SSL_set_psk_client_callback(
2552 SSL *s, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2553 unsigned int max_identity_len, uint8_t *psk,
2554 unsigned int max_psk_len)) {
2555 s->psk_client_callback = cb;
2556}
Adam Langley95c29f32014-06-20 12:00:00 -07002557
Adam Langleyfcf25832014-12-18 17:42:32 -08002558void SSL_CTX_set_psk_client_callback(
2559 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2560 unsigned int max_identity_len,
2561 uint8_t *psk, unsigned int max_psk_len)) {
2562 ctx->psk_client_callback = cb;
2563}
Adam Langley95c29f32014-06-20 12:00:00 -07002564
Adam Langleyfcf25832014-12-18 17:42:32 -08002565void SSL_set_psk_server_callback(
2566 SSL *s, unsigned int (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
2567 unsigned int max_psk_len)) {
2568 s->psk_server_callback = cb;
2569}
Adam Langley95c29f32014-06-20 12:00:00 -07002570
Adam Langleyfcf25832014-12-18 17:42:32 -08002571void SSL_CTX_set_psk_server_callback(
2572 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *identity,
2573 uint8_t *psk, unsigned int max_psk_len)) {
2574 ctx->psk_server_callback = cb;
2575}
Adam Langley95c29f32014-06-20 12:00:00 -07002576
Adam Langleyfcf25832014-12-18 17:42:32 -08002577void SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) {
2578 ctx->min_version = version;
2579}
Adam Langley95c29f32014-06-20 12:00:00 -07002580
Adam Langleyfcf25832014-12-18 17:42:32 -08002581void SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) {
2582 ctx->max_version = version;
2583}
Adam Langley0289c732014-06-20 12:00:00 -07002584
Adam Langleyfcf25832014-12-18 17:42:32 -08002585void SSL_set_min_version(SSL *ssl, uint16_t version) {
2586 ssl->min_version = version;
2587}
Adam Langley0289c732014-06-20 12:00:00 -07002588
Adam Langleyfcf25832014-12-18 17:42:32 -08002589void SSL_set_max_version(SSL *ssl, uint16_t version) {
2590 ssl->max_version = version;
2591}
Adam Langley95c29f32014-06-20 12:00:00 -07002592
Adam Langleyfcf25832014-12-18 17:42:32 -08002593void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
2594 void (*cb)(int write_p, int version,
2595 int content_type, const void *buf,
2596 size_t len, SSL *ssl, void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002597 ctx->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002598}
David Benjamin61ecccf2015-05-05 09:44:51 -04002599
2600void SSL_CTX_set_msg_callback_arg(SSL_CTX *ctx, void *arg) {
2601 ctx->msg_callback_arg = arg;
2602}
2603
Adam Langleyfcf25832014-12-18 17:42:32 -08002604void SSL_set_msg_callback(SSL *ssl,
2605 void (*cb)(int write_p, int version, int content_type,
2606 const void *buf, size_t len, SSL *ssl,
2607 void *arg)) {
David Benjamin59015c32015-04-26 13:13:55 -04002608 ssl->msg_callback = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -08002609}
Adam Langley95c29f32014-06-20 12:00:00 -07002610
David Benjamin61ecccf2015-05-05 09:44:51 -04002611void SSL_set_msg_callback_arg(SSL *ssl, void *arg) {
2612 ssl->msg_callback_arg = arg;
2613}
2614
Adam Langleyfcf25832014-12-18 17:42:32 -08002615void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio) {
David Benjamin2755a3e2015-04-22 16:17:58 -04002616 BIO_free(ctx->keylog_bio);
Adam Langleyfcf25832014-12-18 17:42:32 -08002617 ctx->keylog_bio = keylog_bio;
2618}
Adam Langley95c29f32014-06-20 12:00:00 -07002619
Adam Langleyfcf25832014-12-18 17:42:32 -08002620static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
2621 static const char hextable[] = "0123456789abcdef";
2622 uint8_t *out;
2623 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002624
Adam Langleyfcf25832014-12-18 17:42:32 -08002625 if (!CBB_add_space(cbb, &out, in_len * 2)) {
2626 return 0;
2627 }
Adam Langley95c29f32014-06-20 12:00:00 -07002628
Adam Langleyfcf25832014-12-18 17:42:32 -08002629 for (i = 0; i < in_len; i++) {
2630 *(out++) = (uint8_t)hextable[in[i] >> 4];
2631 *(out++) = (uint8_t)hextable[in[i] & 0xf];
2632 }
Adam Langley95c29f32014-06-20 12:00:00 -07002633
Adam Langleyfcf25832014-12-18 17:42:32 -08002634 return 1;
2635}
David Benjamin859ec3c2014-09-02 16:29:36 -04002636
2637int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -08002638 const uint8_t *encrypted_premaster,
2639 size_t encrypted_premaster_len,
2640 const uint8_t *premaster,
2641 size_t premaster_len) {
2642 BIO *bio = ctx->keylog_bio;
2643 CBB cbb;
2644 uint8_t *out;
2645 size_t out_len;
2646 int ret;
David Benjamin859ec3c2014-09-02 16:29:36 -04002647
Adam Langleyfcf25832014-12-18 17:42:32 -08002648 if (bio == NULL) {
2649 return 1;
2650 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002651
Adam Langleyfcf25832014-12-18 17:42:32 -08002652 if (encrypted_premaster_len < 8) {
2653 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_rsa_client_key_exchange,
2654 ERR_R_INTERNAL_ERROR);
2655 return 0;
2656 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002657
Adam Langleyfcf25832014-12-18 17:42:32 -08002658 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len * 2 + 1)) {
2659 return 0;
2660 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002661
Adam Langleyfcf25832014-12-18 17:42:32 -08002662 if (!CBB_add_bytes(&cbb, (const uint8_t *)"RSA ", 4) ||
2663 /* Only the first 8 bytes of the encrypted premaster secret are
2664 * logged. */
2665 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
2666 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2667 !cbb_add_hex(&cbb, premaster, premaster_len) ||
2668 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2669 !CBB_finish(&cbb, &out, &out_len)) {
2670 CBB_cleanup(&cbb);
2671 return 0;
2672 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002673
Adam Langleyfcf25832014-12-18 17:42:32 -08002674 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2675 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2676 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
David Benjamin859ec3c2014-09-02 16:29:36 -04002677
Adam Langleyfcf25832014-12-18 17:42:32 -08002678 OPENSSL_free(out);
2679 return ret;
Adam Langley95f22882014-06-20 12:00:00 -07002680}
2681
Adam Langleyfcf25832014-12-18 17:42:32 -08002682int ssl_ctx_log_master_secret(SSL_CTX *ctx, const uint8_t *client_random,
2683 size_t client_random_len, const uint8_t *master,
2684 size_t master_len) {
2685 BIO *bio = ctx->keylog_bio;
2686 CBB cbb;
2687 uint8_t *out;
2688 size_t out_len;
2689 int ret;
Adam Langleyadb739e2014-06-20 12:00:00 -07002690
Adam Langleyfcf25832014-12-18 17:42:32 -08002691 if (bio == NULL) {
2692 return 1;
2693 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002694
Adam Langleyfcf25832014-12-18 17:42:32 -08002695 if (client_random_len != 32) {
2696 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_master_secret, ERR_R_INTERNAL_ERROR);
2697 return 0;
2698 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002699
Adam Langleyfcf25832014-12-18 17:42:32 -08002700 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len * 2 + 1)) {
2701 return 0;
2702 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002703
Adam Langleyfcf25832014-12-18 17:42:32 -08002704 if (!CBB_add_bytes(&cbb, (const uint8_t *)"CLIENT_RANDOM ", 14) ||
2705 !cbb_add_hex(&cbb, client_random, 32) ||
2706 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2707 !cbb_add_hex(&cbb, master, master_len) ||
2708 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2709 !CBB_finish(&cbb, &out, &out_len)) {
2710 CBB_cleanup(&cbb);
2711 return 0;
2712 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002713
Adam Langleyfcf25832014-12-18 17:42:32 -08002714 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2715 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2716 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
David Benjamine99e9122014-12-11 01:46:01 -05002717
Adam Langleyfcf25832014-12-18 17:42:32 -08002718 OPENSSL_free(out);
2719 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -07002720}
2721
David Benjamined7c4752015-02-16 19:16:46 -05002722int SSL_in_false_start(const SSL *s) {
2723 return s->s3->tmp.in_false_start;
2724}
2725
Adam Langleyfcf25832014-12-18 17:42:32 -08002726int SSL_cutthrough_complete(const SSL *s) {
David Benjamined7c4752015-02-16 19:16:46 -05002727 return SSL_in_false_start(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002728}
Adam Langley95c29f32014-06-20 12:00:00 -07002729
Adam Langleyfcf25832014-12-18 17:42:32 -08002730void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
2731 size_t *ssl_session_size) {
2732 *ssl_size = sizeof(SSL);
2733 *ssl_ctx_size = sizeof(SSL_CTX);
2734 *ssl_session_size = sizeof(SSL_SESSION);
2735}
Feng Lu41aa3252014-11-21 22:47:56 -08002736
David Benjamined7c4752015-02-16 19:16:46 -05002737int ssl3_can_false_start(const SSL *s) {
David Benjamin195dc782015-02-19 13:27:05 -05002738 const SSL_CIPHER *const cipher = SSL_get_current_cipher(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002739
Adam Langleye631d962015-04-01 13:11:01 -07002740 /* False Start only for TLS 1.2 with an ECDHE+AEAD cipher and ALPN or NPN. */
David Benjamin195dc782015-02-19 13:27:05 -05002741 return !SSL_IS_DTLS(s) &&
2742 SSL_version(s) >= TLS1_2_VERSION &&
2743 (s->s3->alpn_selected || s->s3->next_proto_neg_seen) &&
2744 cipher != NULL &&
Adam Langleye631d962015-04-01 13:11:01 -07002745 cipher->algorithm_mkey == SSL_kECDHE &&
David Benjamin195dc782015-02-19 13:27:05 -05002746 (cipher->algorithm_enc == SSL_AES128GCM ||
2747 cipher->algorithm_enc == SSL_AES256GCM ||
2748 cipher->algorithm_enc == SSL_CHACHA20POLY1305);
Adam Langleyfcf25832014-12-18 17:42:32 -08002749}
2750
2751const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version) {
2752 switch (version) {
2753 case SSL3_VERSION:
2754 return &SSLv3_enc_data;
2755
2756 case TLS1_VERSION:
2757 return &TLSv1_enc_data;
2758
David Benjamin9e13e1a2015-03-05 01:56:32 -05002759 case DTLS1_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002760 case TLS1_1_VERSION:
2761 return &TLSv1_1_enc_data;
2762
David Benjamin9e13e1a2015-03-05 01:56:32 -05002763 case DTLS1_2_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002764 case TLS1_2_VERSION:
2765 return &TLSv1_2_enc_data;
2766
Adam Langleyfcf25832014-12-18 17:42:32 -08002767 default:
2768 return NULL;
2769 }
2770}
2771
2772uint16_t ssl3_get_max_server_version(const SSL *s) {
2773 uint16_t max_version;
2774
2775 if (SSL_IS_DTLS(s)) {
2776 max_version = (s->max_version != 0) ? s->max_version : DTLS1_2_VERSION;
2777 if (!(s->options & SSL_OP_NO_DTLSv1_2) && DTLS1_2_VERSION >= max_version) {
2778 return DTLS1_2_VERSION;
2779 }
2780 if (!(s->options & SSL_OP_NO_DTLSv1) && DTLS1_VERSION >= max_version) {
2781 return DTLS1_VERSION;
2782 }
2783 return 0;
2784 }
2785
2786 max_version = (s->max_version != 0) ? s->max_version : TLS1_2_VERSION;
2787 if (!(s->options & SSL_OP_NO_TLSv1_2) && TLS1_2_VERSION <= max_version) {
2788 return TLS1_2_VERSION;
2789 }
2790 if (!(s->options & SSL_OP_NO_TLSv1_1) && TLS1_1_VERSION <= max_version) {
2791 return TLS1_1_VERSION;
2792 }
2793 if (!(s->options & SSL_OP_NO_TLSv1) && TLS1_VERSION <= max_version) {
2794 return TLS1_VERSION;
2795 }
2796 if (!(s->options & SSL_OP_NO_SSLv3) && SSL3_VERSION <= max_version) {
2797 return SSL3_VERSION;
2798 }
2799 return 0;
2800}
2801
2802uint16_t ssl3_get_mutual_version(SSL *s, uint16_t client_version) {
2803 uint16_t version = 0;
2804
2805 if (SSL_IS_DTLS(s)) {
2806 /* Clamp client_version to max_version. */
2807 if (s->max_version != 0 && client_version < s->max_version) {
2808 client_version = s->max_version;
2809 }
2810
2811 if (client_version <= DTLS1_2_VERSION && !(s->options & SSL_OP_NO_DTLSv1_2)) {
2812 version = DTLS1_2_VERSION;
2813 } else if (client_version <= DTLS1_VERSION &&
David Benjaminb18f0242015-03-10 18:30:08 -04002814 !(s->options & SSL_OP_NO_DTLSv1)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002815 version = DTLS1_VERSION;
2816 }
2817
2818 /* Check against min_version. */
2819 if (version != 0 && s->min_version != 0 && version > s->min_version) {
2820 return 0;
2821 }
2822 return version;
2823 } else {
2824 /* Clamp client_version to max_version. */
2825 if (s->max_version != 0 && client_version > s->max_version) {
2826 client_version = s->max_version;
2827 }
2828
2829 if (client_version >= TLS1_2_VERSION && !(s->options & SSL_OP_NO_TLSv1_2)) {
2830 version = TLS1_2_VERSION;
2831 } else if (client_version >= TLS1_1_VERSION &&
2832 !(s->options & SSL_OP_NO_TLSv1_1)) {
2833 version = TLS1_1_VERSION;
2834 } else if (client_version >= TLS1_VERSION && !(s->options & SSL_OP_NO_TLSv1)) {
2835 version = TLS1_VERSION;
2836 } else if (client_version >= SSL3_VERSION && !(s->options & SSL_OP_NO_SSLv3)) {
2837 version = SSL3_VERSION;
2838 }
2839
2840 /* Check against min_version. */
2841 if (version != 0 && s->min_version != 0 && version < s->min_version) {
2842 return 0;
2843 }
2844 return version;
2845 }
2846}
2847
2848uint16_t ssl3_get_max_client_version(SSL *s) {
David Benjamin123a8fd2015-04-26 14:16:41 -04002849 uint32_t options = s->options;
Adam Langleyfcf25832014-12-18 17:42:32 -08002850 uint16_t version = 0;
2851
2852 /* OpenSSL's API for controlling versions entails blacklisting individual
2853 * protocols. This has two problems. First, on the client, the protocol can
2854 * only express a contiguous range of versions. Second, a library consumer
2855 * trying to set a maximum version cannot disable protocol versions that get
2856 * added in a future version of the library.
2857 *
2858 * To account for both of these, OpenSSL interprets the client-side bitmask
2859 * as a min/max range by picking the lowest contiguous non-empty range of
2860 * enabled protocols. Note that this means it is impossible to set a maximum
2861 * version of TLS 1.2 in a future-proof way.
2862 *
2863 * By this scheme, the maximum version is the lowest version V such that V is
2864 * enabled and V+1 is disabled or unimplemented. */
2865 if (SSL_IS_DTLS(s)) {
2866 if (!(options & SSL_OP_NO_DTLSv1_2)) {
2867 version = DTLS1_2_VERSION;
2868 }
2869 if (!(options & SSL_OP_NO_DTLSv1) && (options & SSL_OP_NO_DTLSv1_2)) {
2870 version = DTLS1_VERSION;
2871 }
2872 if (s->max_version != 0 && version < s->max_version) {
2873 version = s->max_version;
2874 }
2875 } else {
2876 if (!(options & SSL_OP_NO_TLSv1_2)) {
2877 version = TLS1_2_VERSION;
2878 }
2879 if (!(options & SSL_OP_NO_TLSv1_1) && (options & SSL_OP_NO_TLSv1_2)) {
2880 version = TLS1_1_VERSION;
2881 }
2882 if (!(options & SSL_OP_NO_TLSv1) && (options & SSL_OP_NO_TLSv1_1)) {
2883 version = TLS1_VERSION;
2884 }
2885 if (!(options & SSL_OP_NO_SSLv3) && (options & SSL_OP_NO_TLSv1)) {
2886 version = SSL3_VERSION;
2887 }
2888 if (s->max_version != 0 && version > s->max_version) {
2889 version = s->max_version;
2890 }
2891 }
2892
2893 return version;
2894}
2895
2896int ssl3_is_version_enabled(SSL *s, uint16_t version) {
2897 if (SSL_IS_DTLS(s)) {
2898 if (s->max_version != 0 && version < s->max_version) {
2899 return 0;
2900 }
2901 if (s->min_version != 0 && version > s->min_version) {
2902 return 0;
2903 }
2904
2905 switch (version) {
2906 case DTLS1_VERSION:
2907 return !(s->options & SSL_OP_NO_DTLSv1);
2908
2909 case DTLS1_2_VERSION:
2910 return !(s->options & SSL_OP_NO_DTLSv1_2);
2911
2912 default:
2913 return 0;
2914 }
2915 } else {
2916 if (s->max_version != 0 && version > s->max_version) {
2917 return 0;
2918 }
2919 if (s->min_version != 0 && version < s->min_version) {
2920 return 0;
2921 }
2922
2923 switch (version) {
2924 case SSL3_VERSION:
2925 return !(s->options & SSL_OP_NO_SSLv3);
2926
2927 case TLS1_VERSION:
2928 return !(s->options & SSL_OP_NO_TLSv1);
2929
2930 case TLS1_1_VERSION:
2931 return !(s->options & SSL_OP_NO_TLSv1_1);
2932
2933 case TLS1_2_VERSION:
2934 return !(s->options & SSL_OP_NO_TLSv1_2);
2935
2936 default:
2937 return 0;
2938 }
2939 }
2940}
2941
David Benjaminea72bd02014-12-21 21:27:41 -05002942uint16_t ssl3_version_from_wire(SSL *s, uint16_t wire_version) {
2943 if (!SSL_IS_DTLS(s)) {
2944 return wire_version;
2945 }
2946
2947 uint16_t tls_version = ~wire_version;
2948 uint16_t version = tls_version + 0x0201;
2949 /* If either component overflowed, clamp it so comparisons still work. */
2950 if ((version >> 8) < (tls_version >> 8)) {
2951 version = 0xff00 | (version & 0xff);
2952 }
2953 if ((version & 0xff) < (tls_version & 0xff)) {
2954 version = (version & 0xff00) | 0xff;
2955 }
2956 /* DTLS 1.0 maps to TLS 1.1, not TLS 1.0. */
2957 if (version == TLS1_VERSION) {
2958 version = TLS1_1_VERSION;
2959 }
2960 return version;
2961}
2962
Adam Langleyfcf25832014-12-18 17:42:32 -08002963int SSL_cache_hit(SSL *s) { return s->hit; }
2964
2965int SSL_is_server(SSL *s) { return s->server; }
2966
Adam Langley524e7172015-02-20 16:04:00 -08002967void SSL_CTX_set_dos_protection_cb(
2968 SSL_CTX *ctx, int (*cb)(const struct ssl_early_callback_ctx *)) {
2969 ctx->dos_protection_cb = cb;
2970}
2971
Adam Langleyfcf25832014-12-18 17:42:32 -08002972void SSL_enable_fastradio_padding(SSL *s, char on_off) {
2973 s->fastradio_padding = on_off;
2974}
Adam Langley44972942015-02-20 13:43:23 -08002975
David Benjaminb16346b2015-04-08 19:16:58 -04002976void SSL_set_reject_peer_renegotiations(SSL *s, int reject) {
2977 s->reject_peer_renegotiations = !!reject;
2978}
2979
Adam Langley44972942015-02-20 13:43:23 -08002980const SSL_CIPHER *SSL_get_cipher_by_value(uint16_t value) {
2981 return ssl3_get_cipher_by_value(value);
2982}
Adam Langley3f92d212015-02-20 15:32:52 -08002983
2984int SSL_get_rc4_state(const SSL *ssl, const RC4_KEY **read_key,
2985 const RC4_KEY **write_key) {
2986 if (ssl->aead_read_ctx == NULL || ssl->aead_write_ctx == NULL) {
2987 return 0;
2988 }
2989
2990 return EVP_AEAD_CTX_get_rc4_state(&ssl->aead_read_ctx->ctx, read_key) &&
2991 EVP_AEAD_CTX_get_rc4_state(&ssl->aead_write_ctx->ctx, write_key);
2992}