blob: d7101143da7908383df1626b8c12a5b19a13fdc2 [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
141#include <stdio.h>
142#include <assert.h>
143
David Benjamin39482a12014-07-20 13:30:15 -0400144#include <openssl/bytestring.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700145#include <openssl/dh.h>
146#include <openssl/engine.h>
147#include <openssl/lhash.h>
148#include <openssl/mem.h>
149#include <openssl/obj.h>
150#include <openssl/rand.h>
151#include <openssl/x509v3.h>
152
153#include "ssl_locl.h"
154
Adam Langleyfcf25832014-12-18 17:42:32 -0800155/* Some error codes are special. Ensure the make_errors.go script never
156 * regresses this. */
157OPENSSL_COMPILE_ASSERT(SSL_R_TLSV1_ALERT_NO_RENEGOTIATION ==
158 SSL_AD_NO_RENEGOTIATION + SSL_AD_REASON_OFFSET,
159 ssl_alert_reason_code_mismatch);
David Benjamine1136082014-09-20 12:28:58 -0400160
Adam Langleyfcf25832014-12-18 17:42:32 -0800161int SSL_clear(SSL *s) {
162 if (s->method == NULL) {
163 OPENSSL_PUT_ERROR(SSL, SSL_clear, SSL_R_NO_METHOD_SPECIFIED);
164 return 0;
165 }
Adam Langley95c29f32014-06-20 12:00:00 -0700166
Adam Langleyfcf25832014-12-18 17:42:32 -0800167 if (ssl_clear_bad_session(s)) {
168 SSL_SESSION_free(s->session);
169 s->session = NULL;
170 }
Adam Langley95c29f32014-06-20 12:00:00 -0700171
Adam Langleyfcf25832014-12-18 17:42:32 -0800172 s->hit = 0;
173 s->shutdown = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700174
Adam Langleyfcf25832014-12-18 17:42:32 -0800175 if (s->renegotiate) {
176 OPENSSL_PUT_ERROR(SSL, SSL_clear, ERR_R_INTERNAL_ERROR);
177 return 0;
178 }
Adam Langley95c29f32014-06-20 12:00:00 -0700179
Adam Langleyfcf25832014-12-18 17:42:32 -0800180 /* SSL_clear may be called before or after the |s| is initialized in either
181 * accept or connect state. In the latter case, SSL_clear should preserve the
182 * half and reset |s->state| accordingly. */
183 if (s->handshake_func != NULL) {
184 if (s->server) {
185 SSL_set_accept_state(s);
186 } else {
187 SSL_set_connect_state(s);
188 }
189 } else {
190 assert(s->state == 0);
191 }
Adam Langley95c29f32014-06-20 12:00:00 -0700192
David Benjamin62fd1622015-01-11 13:30:01 -0500193 /* TODO(davidben): Some state on |s| is reset both in |SSL_new| and
194 * |SSL_clear| because it is per-connection state rather than configuration
195 * state. Per-connection state should be on |s->s3| and |s->d1| so it is
196 * naturally reset at the right points between |SSL_new|, |SSL_clear|, and
197 * |ssl3_new|. */
198
Adam Langleyfcf25832014-12-18 17:42:32 -0800199 s->rwstate = SSL_NOTHING;
200 s->rstate = SSL_ST_READ_HEADER;
Adam Langley95c29f32014-06-20 12:00:00 -0700201
Adam Langleyfcf25832014-12-18 17:42:32 -0800202 if (s->init_buf != NULL) {
203 BUF_MEM_free(s->init_buf);
204 s->init_buf = NULL;
205 }
Adam Langley95c29f32014-06-20 12:00:00 -0700206
David Benjamin62fd1622015-01-11 13:30:01 -0500207 s->packet = NULL;
208 s->packet_length = 0;
209
Adam Langleyfcf25832014-12-18 17:42:32 -0800210 ssl_clear_cipher_ctx(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700211
David Benjamin62fd1622015-01-11 13:30:01 -0500212 if (s->next_proto_negotiated) {
213 OPENSSL_free(s->next_proto_negotiated);
214 s->next_proto_negotiated = NULL;
215 s->next_proto_negotiated_len = 0;
216 }
217
218 /* The s->d1->mtu is simultaneously configuration (preserved across
219 * clear) and connection-specific state (gets reset).
220 *
221 * TODO(davidben): Avoid this. */
222 unsigned mtu = 0;
223 if (s->d1 != NULL) {
224 mtu = s->d1->mtu;
225 }
226
227 s->method->ssl_free(s);
228 if (!s->method->ssl_new(s)) {
229 return 0;
230 }
231 s->enc_method = ssl3_get_enc_method(s->version);
232 assert(s->enc_method != NULL);
233
234 if (SSL_IS_DTLS(s) && (SSL_get_options(s) & SSL_OP_NO_QUERY_MTU)) {
235 s->d1->mtu = mtu;
236 }
237
Adam Langleyfcf25832014-12-18 17:42:32 -0800238 s->client_version = s->version;
Adam Langley95c29f32014-06-20 12:00:00 -0700239
Adam Langleyfcf25832014-12-18 17:42:32 -0800240 return 1;
241}
Adam Langley95c29f32014-06-20 12:00:00 -0700242
Adam Langleyfcf25832014-12-18 17:42:32 -0800243SSL *SSL_new(SSL_CTX *ctx) {
244 SSL *s;
Adam Langley95c29f32014-06-20 12:00:00 -0700245
Adam Langleyfcf25832014-12-18 17:42:32 -0800246 if (ctx == NULL) {
247 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_NULL_SSL_CTX);
248 return NULL;
249 }
250 if (ctx->method == NULL) {
251 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
252 return NULL;
253 }
Adam Langley95c29f32014-06-20 12:00:00 -0700254
Adam Langleyfcf25832014-12-18 17:42:32 -0800255 s = (SSL *)OPENSSL_malloc(sizeof(SSL));
256 if (s == NULL) {
257 goto err;
258 }
259 memset(s, 0, sizeof(SSL));
Adam Langley95c29f32014-06-20 12:00:00 -0700260
Adam Langleyfcf25832014-12-18 17:42:32 -0800261 s->min_version = ctx->min_version;
262 s->max_version = ctx->max_version;
David Benjamin1eb367c2014-12-12 18:17:51 -0500263
Adam Langleyfcf25832014-12-18 17:42:32 -0800264 s->options = ctx->options;
265 s->mode = ctx->mode;
266 s->max_cert_list = ctx->max_cert_list;
Adam Langley95c29f32014-06-20 12:00:00 -0700267
David Benjamina5a3eeb2015-03-18 20:26:30 -0400268 s->cert = ssl_cert_dup(ctx->cert);
269 if (s->cert == NULL) {
270 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -0800271 }
Adam Langley95c29f32014-06-20 12:00:00 -0700272
Adam Langleyfcf25832014-12-18 17:42:32 -0800273 s->read_ahead = ctx->read_ahead;
274 s->msg_callback = ctx->msg_callback;
275 s->msg_callback_arg = ctx->msg_callback_arg;
276 s->verify_mode = ctx->verify_mode;
277 s->sid_ctx_length = ctx->sid_ctx_length;
278 assert(s->sid_ctx_length <= sizeof s->sid_ctx);
279 memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));
280 s->verify_callback = ctx->default_verify_callback;
281 s->generate_session_id = ctx->generate_session_id;
Adam Langley95c29f32014-06-20 12:00:00 -0700282
Adam Langleyfcf25832014-12-18 17:42:32 -0800283 s->param = X509_VERIFY_PARAM_new();
284 if (!s->param) {
285 goto err;
286 }
287 X509_VERIFY_PARAM_inherit(s->param, ctx->param);
288 s->quiet_shutdown = ctx->quiet_shutdown;
289 s->max_send_fragment = ctx->max_send_fragment;
Adam Langley95c29f32014-06-20 12:00:00 -0700290
Adam Langleyfcf25832014-12-18 17:42:32 -0800291 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
292 s->ctx = ctx;
293 s->tlsext_debug_cb = 0;
294 s->tlsext_debug_arg = NULL;
295 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
Adam Langleyfcf25832014-12-18 17:42:32 -0800339 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_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:
Adam Langleyfcf25832014-12-18 17:42:32 -0800362 if (s != NULL) {
David Benjamin710d2272015-01-11 20:07:59 -0500363 SSL_free(s);
Adam Langleyfcf25832014-12-18 17:42:32 -0800364 }
365 OPENSSL_PUT_ERROR(SSL, SSL_new, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700366
Adam Langleyfcf25832014-12-18 17:42:32 -0800367 return NULL;
368}
Adam Langley95c29f32014-06-20 12:00:00 -0700369
Adam Langleyfcf25832014-12-18 17:42:32 -0800370int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
371 unsigned int sid_ctx_len) {
372 if (sid_ctx_len > sizeof ctx->sid_ctx) {
373 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_session_id_context,
374 SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
375 return 0;
376 }
377 ctx->sid_ctx_length = sid_ctx_len;
378 memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700379
Adam Langleyfcf25832014-12-18 17:42:32 -0800380 return 1;
381}
Adam Langley95c29f32014-06-20 12:00:00 -0700382
Adam Langleyfcf25832014-12-18 17:42:32 -0800383int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
384 unsigned int sid_ctx_len) {
385 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
386 OPENSSL_PUT_ERROR(SSL, SSL_set_session_id_context,
387 SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
388 return 0;
389 }
390 ssl->sid_ctx_length = sid_ctx_len;
391 memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700392
Adam Langleyfcf25832014-12-18 17:42:32 -0800393 return 1;
394}
Adam Langley95c29f32014-06-20 12:00:00 -0700395
Adam Langleyfcf25832014-12-18 17:42:32 -0800396int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb) {
397 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
398 ctx->generate_session_id = cb;
399 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
400 return 1;
401}
Adam Langley95c29f32014-06-20 12:00:00 -0700402
Adam Langleyfcf25832014-12-18 17:42:32 -0800403int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800404 ssl->generate_session_id = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -0800405 return 1;
406}
Adam Langley95c29f32014-06-20 12:00:00 -0700407
Adam Langleyfcf25832014-12-18 17:42:32 -0800408int SSL_has_matching_session_id(const SSL *ssl, const uint8_t *id,
409 unsigned int id_len) {
410 /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how we
411 * can "construct" a session to give us the desired check - ie. to find if
412 * there's a session in the hash table that would conflict with any new
413 * session built out of this id/id_len and the ssl_version in use by this
414 * SSL. */
415 SSL_SESSION r, *p;
Adam Langley95c29f32014-06-20 12:00:00 -0700416
Adam Langleyfcf25832014-12-18 17:42:32 -0800417 if (id_len > sizeof r.session_id) {
418 return 0;
419 }
Adam Langley95c29f32014-06-20 12:00:00 -0700420
Adam Langleyfcf25832014-12-18 17:42:32 -0800421 r.ssl_version = ssl->version;
422 r.session_id_length = id_len;
423 memcpy(r.session_id, id, id_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700424
Adam Langleyfcf25832014-12-18 17:42:32 -0800425 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
426 p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r);
427 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
428 return p != NULL;
429}
Adam Langley95c29f32014-06-20 12:00:00 -0700430
Adam Langleyfcf25832014-12-18 17:42:32 -0800431int SSL_CTX_set_purpose(SSL_CTX *s, int purpose) {
432 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
433}
434
435int SSL_set_purpose(SSL *s, int purpose) {
436 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
437}
438
439int SSL_CTX_set_trust(SSL_CTX *s, int trust) {
440 return X509_VERIFY_PARAM_set_trust(s->param, trust);
441}
442
443int SSL_set_trust(SSL *s, int trust) {
444 return X509_VERIFY_PARAM_set_trust(s->param, trust);
445}
446
447int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm) {
448 return X509_VERIFY_PARAM_set1(ctx->param, vpm);
449}
450
451int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm) {
452 return X509_VERIFY_PARAM_set1(ssl->param, vpm);
453}
Adam Langley95c29f32014-06-20 12:00:00 -0700454
Adam Langley858a88d2014-06-20 12:00:00 -0700455void ssl_cipher_preference_list_free(
Adam Langleyfcf25832014-12-18 17:42:32 -0800456 struct ssl_cipher_preference_list_st *cipher_list) {
457 sk_SSL_CIPHER_free(cipher_list->ciphers);
458 OPENSSL_free(cipher_list->in_group_flags);
459 OPENSSL_free(cipher_list);
460}
Adam Langley858a88d2014-06-20 12:00:00 -0700461
Adam Langleyfcf25832014-12-18 17:42:32 -0800462struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_dup(
463 struct ssl_cipher_preference_list_st *cipher_list) {
464 struct ssl_cipher_preference_list_st *ret = NULL;
465 size_t n = sk_SSL_CIPHER_num(cipher_list->ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700466
Adam Langleyfcf25832014-12-18 17:42:32 -0800467 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
468 if (!ret) {
469 goto err;
470 }
471
472 ret->ciphers = NULL;
473 ret->in_group_flags = NULL;
474 ret->ciphers = sk_SSL_CIPHER_dup(cipher_list->ciphers);
475 if (!ret->ciphers) {
476 goto err;
477 }
478 ret->in_group_flags = BUF_memdup(cipher_list->in_group_flags, n);
479 if (!ret->in_group_flags) {
480 goto err;
481 }
482
483 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -0700484
485err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800486 if (ret && ret->ciphers) {
487 sk_SSL_CIPHER_free(ret->ciphers);
488 }
489 if (ret) {
490 OPENSSL_free(ret);
491 }
492 return NULL;
493}
Adam Langley858a88d2014-06-20 12:00:00 -0700494
Adam Langleyfcf25832014-12-18 17:42:32 -0800495struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_from_ciphers(
496 STACK_OF(SSL_CIPHER) * ciphers) {
497 struct ssl_cipher_preference_list_st *ret = NULL;
498 size_t n = sk_SSL_CIPHER_num(ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700499
Adam Langleyfcf25832014-12-18 17:42:32 -0800500 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
501 if (!ret) {
502 goto err;
503 }
504 ret->ciphers = NULL;
505 ret->in_group_flags = NULL;
506 ret->ciphers = sk_SSL_CIPHER_dup(ciphers);
507 if (!ret->ciphers) {
508 goto err;
509 }
510 ret->in_group_flags = OPENSSL_malloc(n);
511 if (!ret->in_group_flags) {
512 goto err;
513 }
514 memset(ret->in_group_flags, 0, n);
515 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -0700516
517err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800518 if (ret && ret->ciphers) {
519 sk_SSL_CIPHER_free(ret->ciphers);
520 }
521 if (ret) {
522 OPENSSL_free(ret);
523 }
524 return NULL;
525}
Adam Langley858a88d2014-06-20 12:00:00 -0700526
Adam Langleyfcf25832014-12-18 17:42:32 -0800527X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) { return ctx->param; }
Adam Langley95c29f32014-06-20 12:00:00 -0700528
Adam Langleyfcf25832014-12-18 17:42:32 -0800529X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) { return ssl->param; }
Adam Langley95c29f32014-06-20 12:00:00 -0700530
Adam Langleyfcf25832014-12-18 17:42:32 -0800531void SSL_certs_clear(SSL *s) { ssl_cert_clear_certs(s->cert); }
Adam Langley95c29f32014-06-20 12:00:00 -0700532
Adam Langleyfcf25832014-12-18 17:42:32 -0800533void SSL_free(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800534 if (s == NULL) {
535 return;
536 }
Adam Langley95c29f32014-06-20 12:00:00 -0700537
Adam Langleyfcf25832014-12-18 17:42:32 -0800538 if (s->param) {
539 X509_VERIFY_PARAM_free(s->param);
540 }
Adam Langley95c29f32014-06-20 12:00:00 -0700541
Adam Langleyfcf25832014-12-18 17:42:32 -0800542 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700543
Adam Langleyfcf25832014-12-18 17:42:32 -0800544 if (s->bbio != NULL) {
545 /* If the buffering BIO is in place, pop it off */
546 if (s->bbio == s->wbio) {
547 s->wbio = BIO_pop(s->wbio);
548 }
549 BIO_free(s->bbio);
550 s->bbio = NULL;
551 }
Adam Langley95c29f32014-06-20 12:00:00 -0700552
Adam Langleyfcf25832014-12-18 17:42:32 -0800553 if (s->rbio != NULL) {
554 BIO_free_all(s->rbio);
555 }
Adam Langley95c29f32014-06-20 12:00:00 -0700556
Adam Langleyfcf25832014-12-18 17:42:32 -0800557 if (s->wbio != NULL && s->wbio != s->rbio) {
558 BIO_free_all(s->wbio);
559 }
Adam Langley95c29f32014-06-20 12:00:00 -0700560
Adam Langleyfcf25832014-12-18 17:42:32 -0800561 if (s->init_buf != NULL) {
562 BUF_MEM_free(s->init_buf);
563 }
Adam Langley95c29f32014-06-20 12:00:00 -0700564
Adam Langleyfcf25832014-12-18 17:42:32 -0800565 /* add extra stuff */
566 if (s->cipher_list != NULL) {
567 ssl_cipher_preference_list_free(s->cipher_list);
568 }
569 if (s->cipher_list_by_id != NULL) {
570 sk_SSL_CIPHER_free(s->cipher_list_by_id);
571 }
Adam Langley95c29f32014-06-20 12:00:00 -0700572
Adam Langleyfcf25832014-12-18 17:42:32 -0800573 if (s->session != NULL) {
574 ssl_clear_bad_session(s);
575 SSL_SESSION_free(s->session);
576 }
Adam Langley95c29f32014-06-20 12:00:00 -0700577
Adam Langleyfcf25832014-12-18 17:42:32 -0800578 ssl_clear_cipher_ctx(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700579
Adam Langleyfcf25832014-12-18 17:42:32 -0800580 if (s->cert != NULL) {
581 ssl_cert_free(s->cert);
582 }
Adam Langley0289c732014-06-20 12:00:00 -0700583
Adam Langleyfcf25832014-12-18 17:42:32 -0800584 if (s->tlsext_hostname) {
585 OPENSSL_free(s->tlsext_hostname);
586 }
587 if (s->initial_ctx) {
588 SSL_CTX_free(s->initial_ctx);
589 }
590 if (s->tlsext_ecpointformatlist) {
591 OPENSSL_free(s->tlsext_ecpointformatlist);
592 }
593 if (s->tlsext_ellipticcurvelist) {
594 OPENSSL_free(s->tlsext_ellipticcurvelist);
595 }
596 if (s->alpn_client_proto_list) {
597 OPENSSL_free(s->alpn_client_proto_list);
598 }
599 if (s->tlsext_channel_id_private) {
600 EVP_PKEY_free(s->tlsext_channel_id_private);
601 }
602 if (s->psk_identity_hint) {
603 OPENSSL_free(s->psk_identity_hint);
604 }
605 if (s->client_CA != NULL) {
606 sk_X509_NAME_pop_free(s->client_CA, X509_NAME_free);
607 }
608 if (s->next_proto_negotiated) {
609 OPENSSL_free(s->next_proto_negotiated);
610 }
611 if (s->srtp_profiles) {
612 sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);
613 }
Adam Langley95c29f32014-06-20 12:00:00 -0700614
Adam Langleyfcf25832014-12-18 17:42:32 -0800615 if (s->method != NULL) {
616 s->method->ssl_free(s);
617 }
618 if (s->ctx) {
619 SSL_CTX_free(s->ctx);
620 }
Adam Langley95c29f32014-06-20 12:00:00 -0700621
Adam Langleyfcf25832014-12-18 17:42:32 -0800622 OPENSSL_free(s);
623}
Adam Langley95c29f32014-06-20 12:00:00 -0700624
Adam Langleyfcf25832014-12-18 17:42:32 -0800625void SSL_set_bio(SSL *s, BIO *rbio, BIO *wbio) {
626 /* If the output buffering BIO is still in place, remove it. */
627 if (s->bbio != NULL) {
628 if (s->wbio == s->bbio) {
629 s->wbio = s->wbio->next_bio;
630 s->bbio->next_bio = NULL;
631 }
632 }
Adam Langley95c29f32014-06-20 12:00:00 -0700633
Adam Langleyfcf25832014-12-18 17:42:32 -0800634 if (s->rbio != NULL && s->rbio != rbio) {
635 BIO_free_all(s->rbio);
636 }
637 if (s->wbio != NULL && s->wbio != wbio && s->rbio != s->wbio) {
638 BIO_free_all(s->wbio);
639 }
640 s->rbio = rbio;
641 s->wbio = wbio;
642}
Adam Langley95c29f32014-06-20 12:00:00 -0700643
Adam Langleyfcf25832014-12-18 17:42:32 -0800644BIO *SSL_get_rbio(const SSL *s) { return s->rbio; }
Adam Langley95c29f32014-06-20 12:00:00 -0700645
Adam Langleyfcf25832014-12-18 17:42:32 -0800646BIO *SSL_get_wbio(const SSL *s) { return s->wbio; }
Adam Langley95c29f32014-06-20 12:00:00 -0700647
Adam Langleyfcf25832014-12-18 17:42:32 -0800648int SSL_get_fd(const SSL *s) { return SSL_get_rfd(s); }
Adam Langley95c29f32014-06-20 12:00:00 -0700649
Adam Langleyfcf25832014-12-18 17:42:32 -0800650int SSL_get_rfd(const SSL *s) {
651 int ret = -1;
652 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700653
Adam Langleyfcf25832014-12-18 17:42:32 -0800654 b = SSL_get_rbio(s);
655 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
656 if (r != NULL) {
657 BIO_get_fd(r, &ret);
658 }
659 return ret;
660}
Adam Langley95c29f32014-06-20 12:00:00 -0700661
Adam Langleyfcf25832014-12-18 17:42:32 -0800662int SSL_get_wfd(const SSL *s) {
663 int ret = -1;
664 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700665
Adam Langleyfcf25832014-12-18 17:42:32 -0800666 b = SSL_get_wbio(s);
667 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
668 if (r != NULL) {
669 BIO_get_fd(r, &ret);
670 }
Adam Langley95c29f32014-06-20 12:00:00 -0700671
Adam Langleyfcf25832014-12-18 17:42:32 -0800672 return ret;
673}
Adam Langley95c29f32014-06-20 12:00:00 -0700674
Adam Langleyfcf25832014-12-18 17:42:32 -0800675int SSL_set_fd(SSL *s, int fd) {
676 int ret = 0;
677 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700678
Adam Langleyfcf25832014-12-18 17:42:32 -0800679 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700680
Adam Langleyfcf25832014-12-18 17:42:32 -0800681 if (bio == NULL) {
682 OPENSSL_PUT_ERROR(SSL, SSL_set_fd, ERR_R_BUF_LIB);
683 goto err;
684 }
685 BIO_set_fd(bio, fd, BIO_NOCLOSE);
686 SSL_set_bio(s, bio, bio);
687 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700688
Adam Langley95c29f32014-06-20 12:00:00 -0700689err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800690 return ret;
691}
Adam Langley95c29f32014-06-20 12:00:00 -0700692
Adam Langleyfcf25832014-12-18 17:42:32 -0800693int SSL_set_wfd(SSL *s, int fd) {
694 int ret = 0;
695 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700696
Adam Langleyfcf25832014-12-18 17:42:32 -0800697 if (s->rbio == NULL || BIO_method_type(s->rbio) != BIO_TYPE_FD ||
698 (int)BIO_get_fd(s->rbio, NULL) != fd) {
699 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700700
Adam Langleyfcf25832014-12-18 17:42:32 -0800701 if (bio == NULL) {
702 OPENSSL_PUT_ERROR(SSL, SSL_set_wfd, ERR_R_BUF_LIB);
703 goto err;
704 }
705 BIO_set_fd(bio, fd, BIO_NOCLOSE);
706 SSL_set_bio(s, SSL_get_rbio(s), bio);
707 } else {
708 SSL_set_bio(s, SSL_get_rbio(s), SSL_get_rbio(s));
709 }
710
711 ret = 1;
712
Adam Langley95c29f32014-06-20 12:00:00 -0700713err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800714 return ret;
715}
Adam Langley95c29f32014-06-20 12:00:00 -0700716
Adam Langleyfcf25832014-12-18 17:42:32 -0800717int SSL_set_rfd(SSL *s, int fd) {
718 int ret = 0;
719 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700720
Adam Langleyfcf25832014-12-18 17:42:32 -0800721 if (s->wbio == NULL || BIO_method_type(s->wbio) != BIO_TYPE_FD ||
722 (int)BIO_get_fd(s->wbio, NULL) != fd) {
723 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700724
Adam Langleyfcf25832014-12-18 17:42:32 -0800725 if (bio == NULL) {
726 OPENSSL_PUT_ERROR(SSL, SSL_set_rfd, ERR_R_BUF_LIB);
727 goto err;
728 }
729 BIO_set_fd(bio, fd, BIO_NOCLOSE);
730 SSL_set_bio(s, bio, SSL_get_wbio(s));
731 } else {
732 SSL_set_bio(s, SSL_get_wbio(s), SSL_get_wbio(s));
733 }
734 ret = 1;
735
Adam Langley95c29f32014-06-20 12:00:00 -0700736err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800737 return ret;
738}
Adam Langley95c29f32014-06-20 12:00:00 -0700739
740/* return length of latest Finished message we sent, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -0800741size_t SSL_get_finished(const SSL *s, void *buf, size_t count) {
742 size_t ret = 0;
743
744 if (s->s3 != NULL) {
745 ret = s->s3->tmp.finish_md_len;
746 if (count > ret) {
747 count = ret;
748 }
749 memcpy(buf, s->s3->tmp.finish_md, count);
750 }
751
752 return ret;
753}
Adam Langley95c29f32014-06-20 12:00:00 -0700754
755/* return length of latest Finished message we expected, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -0800756size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count) {
757 size_t ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700758
Adam Langleyfcf25832014-12-18 17:42:32 -0800759 if (s->s3 != NULL) {
760 ret = s->s3->tmp.peer_finish_md_len;
761 if (count > ret) {
762 count = ret;
763 }
764 memcpy(buf, s->s3->tmp.peer_finish_md, count);
765 }
Adam Langley95c29f32014-06-20 12:00:00 -0700766
Adam Langleyfcf25832014-12-18 17:42:32 -0800767 return ret;
768}
Adam Langley95c29f32014-06-20 12:00:00 -0700769
Adam Langleyfcf25832014-12-18 17:42:32 -0800770int SSL_get_verify_mode(const SSL *s) { return s->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -0700771
Adam Langleyfcf25832014-12-18 17:42:32 -0800772int SSL_get_verify_depth(const SSL *s) {
773 return X509_VERIFY_PARAM_get_depth(s->param);
774}
Adam Langley95c29f32014-06-20 12:00:00 -0700775
Adam Langleyfcf25832014-12-18 17:42:32 -0800776int (*SSL_get_verify_callback(const SSL *s))(int, X509_STORE_CTX *) {
777 return s->verify_callback;
778}
Adam Langley95c29f32014-06-20 12:00:00 -0700779
Adam Langleyfcf25832014-12-18 17:42:32 -0800780int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) { return ctx->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -0700781
Adam Langleyfcf25832014-12-18 17:42:32 -0800782int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) {
783 return X509_VERIFY_PARAM_get_depth(ctx->param);
784}
Adam Langley95c29f32014-06-20 12:00:00 -0700785
Adam Langleyfcf25832014-12-18 17:42:32 -0800786int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int, X509_STORE_CTX *) {
787 return ctx->default_verify_callback;
788}
Adam Langley95c29f32014-06-20 12:00:00 -0700789
Adam Langleyfcf25832014-12-18 17:42:32 -0800790void SSL_set_verify(SSL *s, int mode,
791 int (*callback)(int ok, X509_STORE_CTX *ctx)) {
792 s->verify_mode = mode;
793 if (callback != NULL) {
794 s->verify_callback = callback;
795 }
796}
Adam Langley95c29f32014-06-20 12:00:00 -0700797
Adam Langleyfcf25832014-12-18 17:42:32 -0800798void SSL_set_verify_depth(SSL *s, int depth) {
799 X509_VERIFY_PARAM_set_depth(s->param, depth);
800}
Adam Langley95c29f32014-06-20 12:00:00 -0700801
Adam Langleyfcf25832014-12-18 17:42:32 -0800802void SSL_set_read_ahead(SSL *s, int yes) { s->read_ahead = yes; }
Adam Langley95c29f32014-06-20 12:00:00 -0700803
Adam Langleyfcf25832014-12-18 17:42:32 -0800804int SSL_get_read_ahead(const SSL *s) { return s->read_ahead; }
Adam Langley95c29f32014-06-20 12:00:00 -0700805
Adam Langleyfcf25832014-12-18 17:42:32 -0800806int SSL_pending(const SSL *s) {
807 /* SSL_pending cannot work properly if read-ahead is enabled
808 * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)), and it is
809 * impossible to fix since SSL_pending cannot report errors that may be
810 * observed while scanning the new data. (Note that SSL_pending() is often
811 * used as a boolean value, so we'd better not return -1.). */
812 return s->method->ssl_pending(s);
813}
Adam Langley95c29f32014-06-20 12:00:00 -0700814
Adam Langleyfcf25832014-12-18 17:42:32 -0800815X509 *SSL_get_peer_certificate(const SSL *s) {
816 X509 *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700817
Adam Langleyfcf25832014-12-18 17:42:32 -0800818 if (s == NULL || s->session == NULL) {
819 r = NULL;
820 } else {
821 r = s->session->peer;
822 }
Adam Langley95c29f32014-06-20 12:00:00 -0700823
Adam Langleyfcf25832014-12-18 17:42:32 -0800824 if (r == NULL) {
825 return NULL;
826 }
Adam Langley95c29f32014-06-20 12:00:00 -0700827
Adam Langleyfcf25832014-12-18 17:42:32 -0800828 return X509_up_ref(r);
829}
830
831STACK_OF(X509) * SSL_get_peer_cert_chain(const SSL *s) {
832 STACK_OF(X509) * r;
833
834 if (s == NULL || s->session == NULL || s->session->sess_cert == NULL) {
835 r = NULL;
836 } else {
837 r = s->session->sess_cert->cert_chain;
838 }
839
840 /* If we are a client, cert_chain includes the peer's own certificate; if we
841 * are a server, it does not. */
842 return r;
843}
Adam Langley95c29f32014-06-20 12:00:00 -0700844
Adam Langley95c29f32014-06-20 12:00:00 -0700845/* Fix this so it checks all the valid key/cert options */
Adam Langleyfcf25832014-12-18 17:42:32 -0800846int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
847 if (ctx == NULL || ctx->cert == NULL || ctx->cert->key->x509 == NULL) {
848 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key,
849 SSL_R_NO_CERTIFICATE_ASSIGNED);
850 return 0;
851 }
852
853 if (ctx->cert->key->privatekey == NULL) {
854 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key,
855 SSL_R_NO_PRIVATE_KEY_ASSIGNED);
856 return 0;
857 }
858
859 return X509_check_private_key(ctx->cert->key->x509,
860 ctx->cert->key->privatekey);
861}
Adam Langley95c29f32014-06-20 12:00:00 -0700862
863/* Fix this function so that it takes an optional type parameter */
Adam Langleyfcf25832014-12-18 17:42:32 -0800864int SSL_check_private_key(const SSL *ssl) {
865 if (ssl == NULL) {
866 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, ERR_R_PASSED_NULL_PARAMETER);
867 return 0;
868 }
Adam Langley95c29f32014-06-20 12:00:00 -0700869
Adam Langleyfcf25832014-12-18 17:42:32 -0800870 if (ssl->cert == NULL) {
871 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
872 SSL_R_NO_CERTIFICATE_ASSIGNED);
873 return 0;
874 }
Adam Langley95c29f32014-06-20 12:00:00 -0700875
Adam Langleyfcf25832014-12-18 17:42:32 -0800876 if (ssl->cert->key->x509 == NULL) {
877 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
878 SSL_R_NO_CERTIFICATE_ASSIGNED);
879 return 0;
880 }
David Benjamin0b145c22014-11-26 20:10:09 -0500881
Adam Langleyfcf25832014-12-18 17:42:32 -0800882 if (ssl->cert->key->privatekey == NULL) {
883 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
884 SSL_R_NO_PRIVATE_KEY_ASSIGNED);
885 return 0;
886 }
Adam Langley95c29f32014-06-20 12:00:00 -0700887
Adam Langleyfcf25832014-12-18 17:42:32 -0800888 return X509_check_private_key(ssl->cert->key->x509,
889 ssl->cert->key->privatekey);
890}
Adam Langley95c29f32014-06-20 12:00:00 -0700891
Adam Langleyfcf25832014-12-18 17:42:32 -0800892int SSL_accept(SSL *s) {
893 if (s->handshake_func == 0) {
894 /* Not properly initialized yet */
895 SSL_set_accept_state(s);
896 }
David Benjamin0b145c22014-11-26 20:10:09 -0500897
Adam Langleyfcf25832014-12-18 17:42:32 -0800898 if (s->handshake_func != s->method->ssl_accept) {
899 OPENSSL_PUT_ERROR(SSL, SSL_connect, ERR_R_INTERNAL_ERROR);
900 return -1;
901 }
Adam Langley95c29f32014-06-20 12:00:00 -0700902
Adam Langleyfcf25832014-12-18 17:42:32 -0800903 return s->handshake_func(s);
904}
Adam Langley95c29f32014-06-20 12:00:00 -0700905
Adam Langleyfcf25832014-12-18 17:42:32 -0800906int SSL_connect(SSL *s) {
907 if (s->handshake_func == 0) {
908 /* Not properly initialized yet */
909 SSL_set_connect_state(s);
910 }
Adam Langley95c29f32014-06-20 12:00:00 -0700911
Adam Langleyfcf25832014-12-18 17:42:32 -0800912 if (s->handshake_func != s->method->ssl_connect) {
913 OPENSSL_PUT_ERROR(SSL, SSL_connect, ERR_R_INTERNAL_ERROR);
914 return -1;
915 }
Adam Langley95c29f32014-06-20 12:00:00 -0700916
Adam Langleyfcf25832014-12-18 17:42:32 -0800917 return s->handshake_func(s);
918}
Adam Langley95c29f32014-06-20 12:00:00 -0700919
Adam Langleyfcf25832014-12-18 17:42:32 -0800920long SSL_get_default_timeout(const SSL *s) {
921 return SSL_DEFAULT_SESSION_TIMEOUT;
922}
Adam Langley95c29f32014-06-20 12:00:00 -0700923
Adam Langleyfcf25832014-12-18 17:42:32 -0800924int SSL_read(SSL *s, void *buf, int num) {
925 if (s->handshake_func == 0) {
926 OPENSSL_PUT_ERROR(SSL, SSL_read, SSL_R_UNINITIALIZED);
927 return -1;
928 }
Adam Langley95c29f32014-06-20 12:00:00 -0700929
Adam Langleyfcf25832014-12-18 17:42:32 -0800930 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
931 s->rwstate = SSL_NOTHING;
932 return 0;
933 }
Adam Langley95c29f32014-06-20 12:00:00 -0700934
Adam Langleyfcf25832014-12-18 17:42:32 -0800935 return s->method->ssl_read(s, buf, num);
936}
Adam Langley95c29f32014-06-20 12:00:00 -0700937
Adam Langleyfcf25832014-12-18 17:42:32 -0800938int SSL_peek(SSL *s, void *buf, int num) {
939 if (s->handshake_func == 0) {
940 OPENSSL_PUT_ERROR(SSL, SSL_peek, SSL_R_UNINITIALIZED);
941 return -1;
942 }
Adam Langley95c29f32014-06-20 12:00:00 -0700943
Adam Langleyfcf25832014-12-18 17:42:32 -0800944 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
945 return 0;
946 }
Adam Langley95c29f32014-06-20 12:00:00 -0700947
Adam Langleyfcf25832014-12-18 17:42:32 -0800948 return s->method->ssl_peek(s, buf, num);
949}
Adam Langley95c29f32014-06-20 12:00:00 -0700950
Adam Langleyfcf25832014-12-18 17:42:32 -0800951int SSL_write(SSL *s, const void *buf, int num) {
952 if (s->handshake_func == 0) {
953 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_UNINITIALIZED);
954 return -1;
955 }
Adam Langley95c29f32014-06-20 12:00:00 -0700956
Adam Langleyfcf25832014-12-18 17:42:32 -0800957 if (s->shutdown & SSL_SENT_SHUTDOWN) {
958 s->rwstate = SSL_NOTHING;
959 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_PROTOCOL_IS_SHUTDOWN);
960 return -1;
961 }
Adam Langley95c29f32014-06-20 12:00:00 -0700962
Adam Langleyfcf25832014-12-18 17:42:32 -0800963 return s->method->ssl_write(s, buf, num);
964}
Adam Langley95c29f32014-06-20 12:00:00 -0700965
Adam Langleyfcf25832014-12-18 17:42:32 -0800966int SSL_shutdown(SSL *s) {
967 /* Note that this function behaves differently from what one might expect.
968 * Return values are 0 for no success (yet), 1 for success; but calling it
969 * once is usually not enough, even if blocking I/O is used (see
970 * ssl3_shutdown). */
Adam Langley95c29f32014-06-20 12:00:00 -0700971
Adam Langleyfcf25832014-12-18 17:42:32 -0800972 if (s->handshake_func == 0) {
973 OPENSSL_PUT_ERROR(SSL, SSL_shutdown, SSL_R_UNINITIALIZED);
974 return -1;
975 }
Adam Langley95c29f32014-06-20 12:00:00 -0700976
Adam Langleyfcf25832014-12-18 17:42:32 -0800977 if (!SSL_in_init(s)) {
978 return s->method->ssl_shutdown(s);
979 }
Adam Langley95c29f32014-06-20 12:00:00 -0700980
Adam Langleyfcf25832014-12-18 17:42:32 -0800981 return 1;
982}
Adam Langley95c29f32014-06-20 12:00:00 -0700983
Adam Langleyfcf25832014-12-18 17:42:32 -0800984int SSL_renegotiate(SSL *s) {
David Benjamin2cdace92015-01-24 13:30:02 -0500985 if (SSL_IS_DTLS(s)) {
986 /* Renegotiation is not supported for DTLS. */
987 OPENSSL_PUT_ERROR(SSL, SSL_renegotiate, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
988 return 0;
989 }
990
Adam Langleyfcf25832014-12-18 17:42:32 -0800991 if (s->renegotiate == 0) {
992 s->renegotiate = 1;
993 }
Adam Langley95c29f32014-06-20 12:00:00 -0700994
Adam Langleyfcf25832014-12-18 17:42:32 -0800995 s->new_session = 1;
996 return s->method->ssl_renegotiate(s);
997}
Adam Langley95c29f32014-06-20 12:00:00 -0700998
Adam Langleyfcf25832014-12-18 17:42:32 -0800999int SSL_renegotiate_pending(SSL *s) {
1000 /* becomes true when negotiation is requested; false again once a handshake
1001 * has finished */
1002 return s->renegotiate != 0;
1003}
Adam Langley95c29f32014-06-20 12:00:00 -07001004
Adam Langleyfcf25832014-12-18 17:42:32 -08001005long SSL_ctrl(SSL *s, int cmd, long larg, void *parg) {
1006 long l;
Adam Langley95c29f32014-06-20 12:00:00 -07001007
Adam Langleyfcf25832014-12-18 17:42:32 -08001008 switch (cmd) {
1009 case SSL_CTRL_GET_READ_AHEAD:
1010 return s->read_ahead;
Adam Langley95c29f32014-06-20 12:00:00 -07001011
Adam Langleyfcf25832014-12-18 17:42:32 -08001012 case SSL_CTRL_SET_READ_AHEAD:
1013 l = s->read_ahead;
1014 s->read_ahead = larg;
1015 return l;
Adam Langley95c29f32014-06-20 12:00:00 -07001016
Adam Langleyfcf25832014-12-18 17:42:32 -08001017 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1018 s->msg_callback_arg = parg;
1019 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001020
Adam Langleyfcf25832014-12-18 17:42:32 -08001021 case SSL_CTRL_OPTIONS:
1022 return s->options |= larg;
Adam Langley95c29f32014-06-20 12:00:00 -07001023
Adam Langleyfcf25832014-12-18 17:42:32 -08001024 case SSL_CTRL_CLEAR_OPTIONS:
1025 return s->options &= ~larg;
Adam Langley95c29f32014-06-20 12:00:00 -07001026
Adam Langleyfcf25832014-12-18 17:42:32 -08001027 case SSL_CTRL_MODE:
1028 return s->mode |= larg;
Adam Langley95c29f32014-06-20 12:00:00 -07001029
Adam Langleyfcf25832014-12-18 17:42:32 -08001030 case SSL_CTRL_CLEAR_MODE:
1031 return s->mode &= ~larg;
Adam Langley95c29f32014-06-20 12:00:00 -07001032
Adam Langleyfcf25832014-12-18 17:42:32 -08001033 case SSL_CTRL_GET_MAX_CERT_LIST:
1034 return s->max_cert_list;
Adam Langley95c29f32014-06-20 12:00:00 -07001035
Adam Langleyfcf25832014-12-18 17:42:32 -08001036 case SSL_CTRL_SET_MAX_CERT_LIST:
1037 l = s->max_cert_list;
1038 s->max_cert_list = larg;
1039 return l;
Adam Langley95c29f32014-06-20 12:00:00 -07001040
Adam Langleyfcf25832014-12-18 17:42:32 -08001041 case SSL_CTRL_SET_MTU:
1042 if (larg < (long)dtls1_min_mtu()) {
1043 return 0;
1044 }
1045 if (SSL_IS_DTLS(s)) {
1046 s->d1->mtu = larg;
1047 return larg;
1048 }
1049 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001050
Adam Langleyfcf25832014-12-18 17:42:32 -08001051 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1052 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH) {
1053 return 0;
1054 }
1055 s->max_send_fragment = larg;
1056 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001057
Adam Langleyfcf25832014-12-18 17:42:32 -08001058 case SSL_CTRL_GET_RI_SUPPORT:
1059 if (s->s3) {
1060 return s->s3->send_connection_binding;
1061 }
1062 return 0;
1063
1064 case SSL_CTRL_CERT_FLAGS:
1065 return s->cert->cert_flags |= larg;
1066
1067 case SSL_CTRL_CLEAR_CERT_FLAGS:
1068 return s->cert->cert_flags &= ~larg;
1069
1070 case SSL_CTRL_GET_RAW_CIPHERLIST:
1071 if (parg) {
1072 if (s->cert->ciphers_raw == NULL) {
1073 return 0;
1074 }
1075 *(uint8_t **)parg = s->cert->ciphers_raw;
1076 return (int)s->cert->ciphers_rawlen;
1077 }
1078
1079 /* Passing a NULL |parg| returns the size of a single
1080 * cipher suite value. */
1081 return 2;
1082
1083 default:
1084 return s->method->ssl_ctrl(s, cmd, larg, parg);
1085 }
1086}
1087
1088long SSL_callback_ctrl(SSL *s, int cmd, void (*fp)(void)) {
1089 switch (cmd) {
1090 case SSL_CTRL_SET_MSG_CALLBACK:
1091 s->msg_callback =
1092 (void (*)(int write_p, int version, int content_type, const void *buf,
1093 size_t len, SSL *ssl, void *arg))(fp);
1094 return 1;
1095
1096 default:
1097 return s->method->ssl_callback_ctrl(s, cmd, fp);
1098 }
1099}
1100
1101LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) { return ctx->sessions; }
1102
1103long SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg) {
1104 long l;
1105
1106 switch (cmd) {
1107 case SSL_CTRL_GET_READ_AHEAD:
1108 return ctx->read_ahead;
1109
1110 case SSL_CTRL_SET_READ_AHEAD:
1111 l = ctx->read_ahead;
1112 ctx->read_ahead = larg;
1113 return l;
1114
1115 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1116 ctx->msg_callback_arg = parg;
1117 return 1;
1118
1119 case SSL_CTRL_GET_MAX_CERT_LIST:
1120 return ctx->max_cert_list;
1121
1122 case SSL_CTRL_SET_MAX_CERT_LIST:
1123 l = ctx->max_cert_list;
1124 ctx->max_cert_list = larg;
1125 return l;
1126
1127 case SSL_CTRL_SET_SESS_CACHE_SIZE:
1128 l = ctx->session_cache_size;
1129 ctx->session_cache_size = larg;
1130 return l;
1131
1132 case SSL_CTRL_GET_SESS_CACHE_SIZE:
1133 return ctx->session_cache_size;
1134
1135 case SSL_CTRL_SET_SESS_CACHE_MODE:
1136 l = ctx->session_cache_mode;
1137 ctx->session_cache_mode = larg;
1138 return l;
1139
1140 case SSL_CTRL_GET_SESS_CACHE_MODE:
1141 return ctx->session_cache_mode;
1142
1143 case SSL_CTRL_SESS_NUMBER:
1144 return lh_SSL_SESSION_num_items(ctx->sessions);
1145
1146 case SSL_CTRL_SESS_CONNECT:
1147 return ctx->stats.sess_connect;
1148
1149 case SSL_CTRL_SESS_CONNECT_GOOD:
1150 return ctx->stats.sess_connect_good;
1151
1152 case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
1153 return ctx->stats.sess_connect_renegotiate;
1154
1155 case SSL_CTRL_SESS_ACCEPT:
1156 return ctx->stats.sess_accept;
1157
1158 case SSL_CTRL_SESS_ACCEPT_GOOD:
1159 return ctx->stats.sess_accept_good;
1160
1161 case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
1162 return ctx->stats.sess_accept_renegotiate;
1163
1164 case SSL_CTRL_SESS_HIT:
1165 return ctx->stats.sess_hit;
1166
1167 case SSL_CTRL_SESS_CB_HIT:
1168 return ctx->stats.sess_cb_hit;
1169
1170 case SSL_CTRL_SESS_MISSES:
1171 return ctx->stats.sess_miss;
1172
1173 case SSL_CTRL_SESS_TIMEOUTS:
1174 return ctx->stats.sess_timeout;
1175
1176 case SSL_CTRL_SESS_CACHE_FULL:
1177 return ctx->stats.sess_cache_full;
1178
1179 case SSL_CTRL_OPTIONS:
1180 return ctx->options |= larg;
1181
1182 case SSL_CTRL_CLEAR_OPTIONS:
1183 return ctx->options &= ~larg;
1184
1185 case SSL_CTRL_MODE:
1186 return ctx->mode |= larg;
1187
1188 case SSL_CTRL_CLEAR_MODE:
1189 return ctx->mode &= ~larg;
1190
1191 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1192 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH) {
1193 return 0;
1194 }
1195 ctx->max_send_fragment = larg;
1196 return 1;
1197
1198 case SSL_CTRL_CERT_FLAGS:
1199 return ctx->cert->cert_flags |= larg;
1200
1201 case SSL_CTRL_CLEAR_CERT_FLAGS:
1202 return ctx->cert->cert_flags &= ~larg;
1203
1204 default:
1205 return ctx->method->ssl_ctx_ctrl(ctx, cmd, larg, parg);
1206 }
1207}
1208
1209long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void)) {
1210 switch (cmd) {
1211 case SSL_CTRL_SET_MSG_CALLBACK:
1212 ctx->msg_callback =
1213 (void (*)(int write_p, int version, int content_type, const void *buf,
1214 size_t len, SSL *ssl, void *arg))(fp);
1215 return 1;
1216
1217 default:
1218 return ctx->method->ssl_ctx_callback_ctrl(ctx, cmd, fp);
1219 }
1220}
1221
1222int ssl_cipher_id_cmp(const void *in_a, const void *in_b) {
1223 long l;
1224 const SSL_CIPHER *a = in_a;
1225 const SSL_CIPHER *b = in_b;
1226 const long a_id = a->id;
1227 const long b_id = b->id;
1228
1229 l = a_id - b_id;
1230 if (l == 0L) {
1231 return 0;
1232 } else {
1233 return (l > 0) ? 1 : -1;
1234 }
1235}
1236
1237int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **ap, const SSL_CIPHER **bp) {
1238 long l;
1239 const long a_id = (*ap)->id;
1240 const long b_id = (*bp)->id;
1241
1242 l = a_id - b_id;
1243 if (l == 0) {
1244 return 0;
1245 } else {
1246 return (l > 0) ? 1 : -1;
1247 }
1248}
1249
1250/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001251 * preference */
Adam Langleyfcf25832014-12-18 17:42:32 -08001252STACK_OF(SSL_CIPHER) * SSL_get_ciphers(const SSL *s) {
1253 if (s == NULL) {
1254 return NULL;
1255 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001256
Adam Langleyfcf25832014-12-18 17:42:32 -08001257 if (s->cipher_list != NULL) {
1258 return s->cipher_list->ciphers;
1259 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001260
Adam Langleyfcf25832014-12-18 17:42:32 -08001261 if (s->version >= TLS1_1_VERSION && s->ctx != NULL &&
1262 s->ctx->cipher_list_tls11 != NULL) {
1263 return s->ctx->cipher_list_tls11->ciphers;
1264 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001265
Adam Langleyfcf25832014-12-18 17:42:32 -08001266 if (s->ctx != NULL && s->ctx->cipher_list != NULL) {
1267 return s->ctx->cipher_list->ciphers;
1268 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001269
Adam Langleyfcf25832014-12-18 17:42:32 -08001270 return NULL;
1271}
Adam Langley95c29f32014-06-20 12:00:00 -07001272
Adam Langleyfcf25832014-12-18 17:42:32 -08001273/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001274 * algorithm id */
Adam Langleyfcf25832014-12-18 17:42:32 -08001275STACK_OF(SSL_CIPHER) * ssl_get_ciphers_by_id(SSL *s) {
1276 if (s == NULL) {
1277 return NULL;
1278 }
Adam Langley95c29f32014-06-20 12:00:00 -07001279
Adam Langleyfcf25832014-12-18 17:42:32 -08001280 if (s->cipher_list_by_id != NULL) {
1281 return s->cipher_list_by_id;
1282 }
Adam Langley95c29f32014-06-20 12:00:00 -07001283
Adam Langleyfcf25832014-12-18 17:42:32 -08001284 if (s->ctx != NULL && s->ctx->cipher_list_by_id != NULL) {
1285 return s->ctx->cipher_list_by_id;
1286 }
Adam Langley95c29f32014-06-20 12:00:00 -07001287
Adam Langleyfcf25832014-12-18 17:42:32 -08001288 return NULL;
1289}
Adam Langley95c29f32014-06-20 12:00:00 -07001290
Adam Langleyfcf25832014-12-18 17:42:32 -08001291/* The old interface to get the same thing as SSL_get_ciphers() */
1292const char *SSL_get_cipher_list(const SSL *s, int n) {
1293 const SSL_CIPHER *c;
1294 STACK_OF(SSL_CIPHER) * sk;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001295
Adam Langleyfcf25832014-12-18 17:42:32 -08001296 if (s == NULL) {
1297 return NULL;
1298 }
Adam Langley95c29f32014-06-20 12:00:00 -07001299
Adam Langleyfcf25832014-12-18 17:42:32 -08001300 sk = SSL_get_ciphers(s);
1301 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
1302 return NULL;
1303 }
Adam Langley95c29f32014-06-20 12:00:00 -07001304
Adam Langleyfcf25832014-12-18 17:42:32 -08001305 c = sk_SSL_CIPHER_value(sk, n);
1306 if (c == NULL) {
1307 return NULL;
1308 }
Adam Langley95c29f32014-06-20 12:00:00 -07001309
Adam Langleyfcf25832014-12-18 17:42:32 -08001310 return c->name;
1311}
David Benjamin5491e3f2014-09-29 19:33:09 -04001312
Adam Langleyfcf25832014-12-18 17:42:32 -08001313/* specify the ciphers to be used by default by the SSL_CTX */
1314int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
1315 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001316
Adam Langleyfcf25832014-12-18 17:42:32 -08001317 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list,
1318 &ctx->cipher_list_by_id, str, ctx->cert);
1319 /* ssl_create_cipher_list may return an empty stack if it was unable to find
1320 * a cipher matching the given rule string (for example if the rule string
1321 * specifies a cipher which has been disabled). This is not an error as far
1322 * as ssl_create_cipher_list is concerned, and hence ctx->cipher_list and
1323 * ctx->cipher_list_by_id has been updated. */
1324 if (sk == NULL) {
1325 return 0;
1326 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1327 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1328 return 0;
1329 }
Adam Langley95c29f32014-06-20 12:00:00 -07001330
Adam Langleyfcf25832014-12-18 17:42:32 -08001331 return 1;
1332}
David Benjamin39482a12014-07-20 13:30:15 -04001333
Adam Langleyfcf25832014-12-18 17:42:32 -08001334int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str) {
1335 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001336
Adam Langleyfcf25832014-12-18 17:42:32 -08001337 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str,
1338 ctx->cert);
1339 if (sk == NULL) {
1340 return 0;
1341 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1342 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list_tls11,
1343 SSL_R_NO_CIPHER_MATCH);
1344 return 0;
1345 }
David Benjamin9f2c0d72014-10-21 22:00:19 -04001346
Adam Langleyfcf25832014-12-18 17:42:32 -08001347 return 1;
1348}
Adam Langley95c29f32014-06-20 12:00:00 -07001349
Adam Langleyfcf25832014-12-18 17:42:32 -08001350/* specify the ciphers to be used by the SSL */
1351int SSL_set_cipher_list(SSL *s, const char *str) {
1352 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001353
Adam Langleyfcf25832014-12-18 17:42:32 -08001354 sk = ssl_create_cipher_list(s->ctx->method, &s->cipher_list,
1355 &s->cipher_list_by_id, str, s->cert);
David Benjamin39482a12014-07-20 13:30:15 -04001356
Adam Langleyfcf25832014-12-18 17:42:32 -08001357 /* see comment in SSL_CTX_set_cipher_list */
1358 if (sk == NULL) {
1359 return 0;
1360 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1361 OPENSSL_PUT_ERROR(SSL, SSL_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1362 return 0;
1363 }
David Benjamin39482a12014-07-20 13:30:15 -04001364
Adam Langleyfcf25832014-12-18 17:42:32 -08001365 return 1;
1366}
Adam Langley95c29f32014-06-20 12:00:00 -07001367
Adam Langleyfcf25832014-12-18 17:42:32 -08001368int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, uint8_t *p) {
1369 size_t i;
1370 const SSL_CIPHER *c;
1371 CERT *ct = s->cert;
1372 uint8_t *q;
1373 /* Set disabled masks for this session */
1374 ssl_set_client_disabled(s);
Adam Langley29707792014-06-20 12:00:00 -07001375
Adam Langleyfcf25832014-12-18 17:42:32 -08001376 if (sk == NULL) {
1377 return 0;
1378 }
1379 q = p;
Adam Langley95c29f32014-06-20 12:00:00 -07001380
Adam Langleyfcf25832014-12-18 17:42:32 -08001381 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
1382 c = sk_SSL_CIPHER_value(sk, i);
1383 /* Skip disabled ciphers */
1384 if (c->algorithm_ssl & ct->mask_ssl ||
1385 c->algorithm_mkey & ct->mask_k ||
1386 c->algorithm_auth & ct->mask_a) {
1387 continue;
1388 }
1389 s2n(ssl3_get_cipher_value(c), p);
1390 }
1391
1392 /* If all ciphers were disabled, return the error to the caller. */
1393 if (p == q) {
1394 return 0;
1395 }
1396
1397 /* Add SCSVs. */
1398 if (!s->renegotiate) {
1399 s2n(SSL3_CK_SCSV & 0xffff, p);
1400 }
1401
Adam Langley5f0efe02015-02-20 13:03:16 -08001402 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001403 s2n(SSL3_CK_FALLBACK_SCSV & 0xffff, p);
1404 }
1405
1406 return p - q;
1407}
1408
1409STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs) {
1410 CBS cipher_suites = *cbs;
1411 const SSL_CIPHER *c;
1412 STACK_OF(SSL_CIPHER) * sk;
1413
1414 if (s->s3) {
1415 s->s3->send_connection_binding = 0;
1416 }
1417
1418 if (CBS_len(&cipher_suites) % 2 != 0) {
1419 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1420 SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
1421 return NULL;
1422 }
1423
1424 sk = sk_SSL_CIPHER_new_null();
1425 if (sk == NULL) {
1426 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1427 goto err;
1428 }
1429
1430 if (!CBS_stow(&cipher_suites, &s->cert->ciphers_raw,
1431 &s->cert->ciphers_rawlen)) {
1432 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1433 goto err;
1434 }
1435
1436 while (CBS_len(&cipher_suites) > 0) {
1437 uint16_t cipher_suite;
1438
1439 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
1440 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_INTERNAL_ERROR);
1441 goto err;
1442 }
1443
1444 /* Check for SCSV. */
1445 if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff)) {
1446 /* SCSV is fatal if renegotiating. */
1447 if (s->renegotiate) {
1448 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1449 SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1450 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1451 goto err;
1452 }
1453 s->s3->send_connection_binding = 1;
1454 continue;
1455 }
1456
1457 /* Check for FALLBACK_SCSV. */
1458 if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff)) {
1459 uint16_t max_version = ssl3_get_max_server_version(s);
1460 if (SSL_IS_DTLS(s) ? (uint16_t)s->version > max_version
1461 : (uint16_t)s->version < max_version) {
1462 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1463 SSL_R_INAPPROPRIATE_FALLBACK);
1464 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
1465 goto err;
1466 }
1467 continue;
1468 }
1469
1470 c = ssl3_get_cipher_by_value(cipher_suite);
1471 if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
1472 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1473 goto err;
1474 }
1475 }
1476
1477 return sk;
David Benjamin9f2c0d72014-10-21 22:00:19 -04001478
Adam Langley95c29f32014-06-20 12:00:00 -07001479err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001480 if (sk != NULL) {
1481 sk_SSL_CIPHER_free(sk);
1482 }
1483 return NULL;
1484}
Adam Langley95c29f32014-06-20 12:00:00 -07001485
1486
Adam Langleyfcf25832014-12-18 17:42:32 -08001487/* return a servername extension value if provided in Client Hello, or NULL. So
1488 * far, only host_name types are defined (RFC 3546). */
1489const char *SSL_get_servername(const SSL *s, const int type) {
1490 if (type != TLSEXT_NAMETYPE_host_name) {
1491 return NULL;
1492 }
Adam Langley95c29f32014-06-20 12:00:00 -07001493
Adam Langleyfcf25832014-12-18 17:42:32 -08001494 return s->session && !s->tlsext_hostname ? s->session->tlsext_hostname
1495 : s->tlsext_hostname;
1496}
Adam Langley95c29f32014-06-20 12:00:00 -07001497
Adam Langleyfcf25832014-12-18 17:42:32 -08001498int SSL_get_servername_type(const SSL *s) {
1499 if (s->session &&
1500 (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname)) {
1501 return TLSEXT_NAMETYPE_host_name;
1502 }
Adam Langley95c29f32014-06-20 12:00:00 -07001503
Adam Langleyfcf25832014-12-18 17:42:32 -08001504 return -1;
1505}
Adam Langley95c29f32014-06-20 12:00:00 -07001506
Adam Langleyfcf25832014-12-18 17:42:32 -08001507void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
1508 ctx->signed_cert_timestamps_enabled = 1;
1509}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001510
Adam Langleyfcf25832014-12-18 17:42:32 -08001511int SSL_enable_signed_cert_timestamps(SSL *ssl) {
1512 ssl->signed_cert_timestamps_enabled = 1;
1513 return 1;
1514}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001515
Adam Langleyfcf25832014-12-18 17:42:32 -08001516void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
1517 ctx->ocsp_stapling_enabled = 1;
1518}
David Benjamin6c7aed02014-08-27 16:42:38 -04001519
Adam Langleyfcf25832014-12-18 17:42:32 -08001520int SSL_enable_ocsp_stapling(SSL *ssl) {
1521 ssl->ocsp_stapling_enabled = 1;
1522 return 1;
1523}
David Benjamin6c7aed02014-08-27 16:42:38 -04001524
Adam Langleyfcf25832014-12-18 17:42:32 -08001525void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
1526 size_t *out_len) {
1527 SSL_SESSION *session = ssl->session;
Adam Langley3cb50e02014-08-26 14:00:31 -07001528
Adam Langleyfcf25832014-12-18 17:42:32 -08001529 *out_len = 0;
1530 *out = NULL;
1531 if (ssl->server || !session || !session->tlsext_signed_cert_timestamp_list) {
1532 return;
1533 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001534
Adam Langleyfcf25832014-12-18 17:42:32 -08001535 *out = session->tlsext_signed_cert_timestamp_list;
1536 *out_len = session->tlsext_signed_cert_timestamp_list_length;
1537}
David Benjamin6c7aed02014-08-27 16:42:38 -04001538
Adam Langleyfcf25832014-12-18 17:42:32 -08001539void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
1540 size_t *out_len) {
1541 SSL_SESSION *session = ssl->session;
1542
1543 *out_len = 0;
1544 *out = NULL;
1545 if (ssl->server || !session || !session->ocsp_response) {
1546 return;
1547 }
1548 *out = session->ocsp_response;
1549 *out_len = session->ocsp_response_length;
1550}
David Benjamin6c7aed02014-08-27 16:42:38 -04001551
Adam Langley95c29f32014-06-20 12:00:00 -07001552/* SSL_select_next_proto implements the standard protocol selection. It is
1553 * expected that this function is called from the callback set by
1554 * SSL_CTX_set_next_proto_select_cb.
1555 *
1556 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1557 * strings. The length byte itself is not included in the length. A byte
1558 * string of length 0 is invalid. No byte string may be truncated.
1559 *
1560 * The current, but experimental algorithm for selecting the protocol is:
1561 *
1562 * 1) If the server doesn't support NPN then this is indicated to the
1563 * callback. In this case, the client application has to abort the connection
1564 * or have a default application level protocol.
1565 *
1566 * 2) If the server supports NPN, but advertises an empty list then the
1567 * client selects the first protcol in its list, but indicates via the
1568 * API that this fallback case was enacted.
1569 *
1570 * 3) Otherwise, the client finds the first protocol in the server's list
1571 * that it supports and selects this protocol. This is because it's
1572 * assumed that the server has better information about which protocol
1573 * a client should use.
1574 *
1575 * 4) If the client doesn't support any of the server's advertised
1576 * protocols, then this is treated the same as case 2.
1577 *
1578 * It returns either
1579 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1580 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1581 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001582int SSL_select_next_proto(uint8_t **out, uint8_t *outlen, const uint8_t *server,
1583 unsigned int server_len, const uint8_t *client,
1584 unsigned int client_len) {
1585 unsigned int i, j;
1586 const uint8_t *result;
1587 int status = OPENSSL_NPN_UNSUPPORTED;
Adam Langley95c29f32014-06-20 12:00:00 -07001588
Adam Langleyfcf25832014-12-18 17:42:32 -08001589 /* For each protocol in server preference order, see if we support it. */
1590 for (i = 0; i < server_len;) {
1591 for (j = 0; j < client_len;) {
1592 if (server[i] == client[j] &&
1593 memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
1594 /* We found a match */
1595 result = &server[i];
1596 status = OPENSSL_NPN_NEGOTIATED;
1597 goto found;
1598 }
1599 j += client[j];
1600 j++;
1601 }
1602 i += server[i];
1603 i++;
1604 }
Adam Langley95c29f32014-06-20 12:00:00 -07001605
Adam Langleyfcf25832014-12-18 17:42:32 -08001606 /* There's no overlap between our protocols and the server's list. */
1607 result = client;
1608 status = OPENSSL_NPN_NO_OVERLAP;
Adam Langley95c29f32014-06-20 12:00:00 -07001609
Adam Langleyfcf25832014-12-18 17:42:32 -08001610found:
1611 *out = (uint8_t *)result + 1;
1612 *outlen = result[0];
1613 return status;
1614}
Adam Langley95c29f32014-06-20 12:00:00 -07001615
Adam Langley95c29f32014-06-20 12:00:00 -07001616/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
1617 * requested protocol for this connection and returns 0. If the client didn't
1618 * request any protocol, then *data is set to NULL.
1619 *
1620 * Note that the client can request any protocol it chooses. The value returned
1621 * from this function need not be a member of the list of supported protocols
Adam Langleyfcf25832014-12-18 17:42:32 -08001622 * provided by the callback. */
1623void SSL_get0_next_proto_negotiated(const SSL *s, const uint8_t **data,
1624 unsigned *len) {
1625 *data = s->next_proto_negotiated;
1626 if (!*data) {
1627 *len = 0;
1628 } else {
1629 *len = s->next_proto_negotiated_len;
1630 }
Adam Langley95c29f32014-06-20 12:00:00 -07001631}
1632
1633/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
1634 * TLS server needs a list of supported protocols for Next Protocol
1635 * Negotiation. The returned list must be in wire format. The list is returned
1636 * by setting |out| to point to it and |outlen| to its length. This memory will
1637 * not be modified, but one should assume that the SSL* keeps a reference to
1638 * it.
1639 *
Adam Langleyfcf25832014-12-18 17:42:32 -08001640 * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise.
1641 * Otherwise, no such extension will be included in the ServerHello. */
1642void SSL_CTX_set_next_protos_advertised_cb(
1643 SSL_CTX *ctx,
1644 int (*cb)(SSL *ssl, const uint8_t **out, unsigned int *outlen, void *arg),
1645 void *arg) {
1646 ctx->next_protos_advertised_cb = cb;
1647 ctx->next_protos_advertised_cb_arg = arg;
1648}
Adam Langley95c29f32014-06-20 12:00:00 -07001649
1650/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1651 * client needs to select a protocol from the server's provided list. |out|
1652 * must be set to point to the selected protocol (which may be within |in|).
1653 * The length of the protocol name must be written into |outlen|. The server's
1654 * advertised protocols are provided in |in| and |inlen|. The callback can
1655 * assume that |in| is syntactically valid.
1656 *
1657 * The client must select a protocol. It is fatal to the connection if this
1658 * callback returns a value other than SSL_TLSEXT_ERR_OK.
1659 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001660void SSL_CTX_set_next_proto_select_cb(
1661 SSL_CTX *ctx, int (*cb)(SSL *s, uint8_t **out, uint8_t *outlen,
1662 const uint8_t *in, unsigned int inlen, void *arg),
1663 void *arg) {
1664 ctx->next_proto_select_cb = cb;
1665 ctx->next_proto_select_cb_arg = arg;
1666}
Adam Langley95c29f32014-06-20 12:00:00 -07001667
Adam Langleyfcf25832014-12-18 17:42:32 -08001668int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
1669 unsigned protos_len) {
1670 if (ctx->alpn_client_proto_list) {
1671 OPENSSL_free(ctx->alpn_client_proto_list);
1672 }
Adam Langley95c29f32014-06-20 12:00:00 -07001673
Adam Langleyfcf25832014-12-18 17:42:32 -08001674 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1675 if (!ctx->alpn_client_proto_list) {
1676 return 1;
1677 }
1678 ctx->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001679
Adam Langleyfcf25832014-12-18 17:42:32 -08001680 return 0;
1681}
Adam Langley95c29f32014-06-20 12:00:00 -07001682
Adam Langleyfcf25832014-12-18 17:42:32 -08001683int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
1684 if (ssl->alpn_client_proto_list) {
1685 OPENSSL_free(ssl->alpn_client_proto_list);
1686 }
Adam Langley95c29f32014-06-20 12:00:00 -07001687
Adam Langleyfcf25832014-12-18 17:42:32 -08001688 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1689 if (!ssl->alpn_client_proto_list) {
1690 return 1;
1691 }
1692 ssl->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001693
Adam Langleyfcf25832014-12-18 17:42:32 -08001694 return 0;
1695}
Adam Langley95c29f32014-06-20 12:00:00 -07001696
1697/* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
1698 * during ClientHello processing in order to select an ALPN protocol from the
1699 * client's list of offered protocols. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001700void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
1701 int (*cb)(SSL *ssl, const uint8_t **out,
1702 uint8_t *outlen, const uint8_t *in,
1703 unsigned int inlen, void *arg),
1704 void *arg) {
1705 ctx->alpn_select_cb = cb;
1706 ctx->alpn_select_cb_arg = arg;
1707}
Adam Langley95c29f32014-06-20 12:00:00 -07001708
1709/* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
1710 * On return it sets |*data| to point to |*len| bytes of protocol name (not
1711 * including the leading length-prefix byte). If the server didn't respond with
1712 * a negotiated protocol then |*len| will be zero. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001713void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **data,
1714 unsigned *len) {
1715 *data = NULL;
1716 if (ssl->s3) {
1717 *data = ssl->s3->alpn_selected;
1718 }
1719 if (*data == NULL) {
1720 *len = 0;
1721 } else {
1722 *len = ssl->s3->alpn_selected_len;
1723 }
1724}
Adam Langley95c29f32014-06-20 12:00:00 -07001725
Adam Langleyfcf25832014-12-18 17:42:32 -08001726int SSL_export_keying_material(SSL *s, uint8_t *out, size_t olen,
1727 const char *label, size_t llen, const uint8_t *p,
1728 size_t plen, int use_context) {
1729 if (s->version < TLS1_VERSION) {
1730 return -1;
1731 }
Adam Langley95c29f32014-06-20 12:00:00 -07001732
Adam Langleyfcf25832014-12-18 17:42:32 -08001733 return s->enc_method->export_keying_material(s, out, olen, label, llen, p,
1734 plen, use_context);
1735}
Adam Langley95c29f32014-06-20 12:00:00 -07001736
Adam Langleyfcf25832014-12-18 17:42:32 -08001737static uint32_t ssl_session_hash(const SSL_SESSION *a) {
1738 uint32_t hash =
1739 ((uint32_t)a->session_id[0]) ||
1740 ((uint32_t)a->session_id[1] << 8) ||
1741 ((uint32_t)a->session_id[2] << 16) ||
1742 ((uint32_t)a->session_id[3] << 24);
Adam Langley95c29f32014-06-20 12:00:00 -07001743
Adam Langleyfcf25832014-12-18 17:42:32 -08001744 return hash;
1745}
Adam Langley95c29f32014-06-20 12:00:00 -07001746
1747/* NB: If this function (or indeed the hash function which uses a sort of
1748 * coarser function than this one) is changed, ensure
1749 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1750 * able to construct an SSL_SESSION that will collide with any existing session
1751 * with a matching session ID. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001752static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
1753 if (a->ssl_version != b->ssl_version) {
1754 return 1;
1755 }
Adam Langley95c29f32014-06-20 12:00:00 -07001756
Adam Langleyfcf25832014-12-18 17:42:32 -08001757 if (a->session_id_length != b->session_id_length) {
1758 return 1;
1759 }
Adam Langley95c29f32014-06-20 12:00:00 -07001760
Adam Langleyfcf25832014-12-18 17:42:32 -08001761 return memcmp(a->session_id, b->session_id, a->session_id_length);
1762}
Adam Langley95c29f32014-06-20 12:00:00 -07001763
Adam Langleyfcf25832014-12-18 17:42:32 -08001764SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth) {
1765 SSL_CTX *ret = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001766
Adam Langleyfcf25832014-12-18 17:42:32 -08001767 if (meth == NULL) {
1768 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_NULL_SSL_METHOD_PASSED);
1769 return NULL;
1770 }
Adam Langley95c29f32014-06-20 12:00:00 -07001771
Adam Langleyfcf25832014-12-18 17:42:32 -08001772 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
1773 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
1774 goto err;
1775 }
Adam Langley95c29f32014-06-20 12:00:00 -07001776
Adam Langleyfcf25832014-12-18 17:42:32 -08001777 ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1778 if (ret == NULL) {
1779 goto err;
1780 }
Adam Langley95c29f32014-06-20 12:00:00 -07001781
Adam Langleyfcf25832014-12-18 17:42:32 -08001782 memset(ret, 0, sizeof(SSL_CTX));
Adam Langley95c29f32014-06-20 12:00:00 -07001783
Adam Langleyfcf25832014-12-18 17:42:32 -08001784 ret->method = meth->method;
Adam Langley95c29f32014-06-20 12:00:00 -07001785
Adam Langleyfcf25832014-12-18 17:42:32 -08001786 ret->cert_store = NULL;
1787 ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
1788 ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1789 ret->session_cache_head = NULL;
1790 ret->session_cache_tail = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001791
Adam Langleyfcf25832014-12-18 17:42:32 -08001792 /* We take the system default */
1793 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001794
Adam Langleyfcf25832014-12-18 17:42:32 -08001795 ret->new_session_cb = 0;
1796 ret->remove_session_cb = 0;
1797 ret->get_session_cb = 0;
1798 ret->generate_session_id = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001799
Adam Langleyfcf25832014-12-18 17:42:32 -08001800 memset((char *)&ret->stats, 0, sizeof(ret->stats));
Adam Langley95c29f32014-06-20 12:00:00 -07001801
Adam Langleyfcf25832014-12-18 17:42:32 -08001802 ret->references = 1;
1803 ret->quiet_shutdown = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001804
Adam Langleyfcf25832014-12-18 17:42:32 -08001805 ret->info_callback = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001806
Adam Langleyfcf25832014-12-18 17:42:32 -08001807 ret->app_verify_callback = 0;
1808 ret->app_verify_arg = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001809
Adam Langleyfcf25832014-12-18 17:42:32 -08001810 ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
1811 ret->read_ahead = 0;
1812 ret->msg_callback = 0;
1813 ret->msg_callback_arg = NULL;
1814 ret->verify_mode = SSL_VERIFY_NONE;
1815 ret->sid_ctx_length = 0;
1816 ret->default_verify_callback = NULL;
1817 ret->cert = ssl_cert_new();
1818 if (ret->cert == NULL) {
1819 goto err;
1820 }
Adam Langley95c29f32014-06-20 12:00:00 -07001821
Adam Langleyfcf25832014-12-18 17:42:32 -08001822 ret->default_passwd_callback = 0;
1823 ret->default_passwd_callback_userdata = NULL;
1824 ret->client_cert_cb = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001825
Adam Langleyfcf25832014-12-18 17:42:32 -08001826 ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
1827 if (ret->sessions == NULL) {
1828 goto err;
1829 }
1830 ret->cert_store = X509_STORE_new();
1831 if (ret->cert_store == NULL) {
1832 goto err;
1833 }
Adam Langley95c29f32014-06-20 12:00:00 -07001834
Adam Langleyfcf25832014-12-18 17:42:32 -08001835 ssl_create_cipher_list(ret->method, &ret->cipher_list,
1836 &ret->cipher_list_by_id, SSL_DEFAULT_CIPHER_LIST,
1837 ret->cert);
1838 if (ret->cipher_list == NULL ||
1839 sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0) {
1840 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_LIBRARY_HAS_NO_CIPHERS);
1841 goto err2;
1842 }
Adam Langley95c29f32014-06-20 12:00:00 -07001843
Adam Langleyfcf25832014-12-18 17:42:32 -08001844 ret->param = X509_VERIFY_PARAM_new();
1845 if (!ret->param) {
1846 goto err;
1847 }
Adam Langley95c29f32014-06-20 12:00:00 -07001848
Adam Langleyfcf25832014-12-18 17:42:32 -08001849 ret->client_CA = sk_X509_NAME_new_null();
1850 if (ret->client_CA == NULL) {
1851 goto err;
1852 }
Adam Langley95c29f32014-06-20 12:00:00 -07001853
Adam Langleyfcf25832014-12-18 17:42:32 -08001854 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001855
Adam Langleyfcf25832014-12-18 17:42:32 -08001856 ret->extra_certs = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001857
Adam Langleyfcf25832014-12-18 17:42:32 -08001858 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
David Benjamin422d3a42014-08-20 11:09:03 -04001859
Adam Langleyfcf25832014-12-18 17:42:32 -08001860 ret->tlsext_servername_callback = 0;
1861 ret->tlsext_servername_arg = NULL;
1862 /* Setup RFC4507 ticket keys */
1863 if (!RAND_bytes(ret->tlsext_tick_key_name, 16) ||
1864 !RAND_bytes(ret->tlsext_tick_hmac_key, 16) ||
1865 !RAND_bytes(ret->tlsext_tick_aes_key, 16)) {
1866 ret->options |= SSL_OP_NO_TICKET;
1867 }
Adam Langley95c29f32014-06-20 12:00:00 -07001868
Adam Langleyfcf25832014-12-18 17:42:32 -08001869 ret->tlsext_status_cb = 0;
1870 ret->tlsext_status_arg = NULL;
David Benjamin82c9e902014-12-12 15:55:27 -05001871
Adam Langleyfcf25832014-12-18 17:42:32 -08001872 ret->next_protos_advertised_cb = 0;
1873 ret->next_proto_select_cb = 0;
1874 ret->psk_identity_hint = NULL;
1875 ret->psk_client_callback = NULL;
1876 ret->psk_server_callback = NULL;
1877
1878 /* Default is to connect to non-RI servers. When RI is more widely deployed
1879 * might change this. */
1880 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
1881
1882 /* Lock the SSL_CTX to the specified version, for compatibility with legacy
1883 * uses of SSL_METHOD. */
1884 if (meth->version != 0) {
1885 SSL_CTX_set_max_version(ret, meth->version);
1886 SSL_CTX_set_min_version(ret, meth->version);
1887 }
1888
1889 return ret;
1890
Adam Langley95c29f32014-06-20 12:00:00 -07001891err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001892 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -07001893err2:
Adam Langleyfcf25832014-12-18 17:42:32 -08001894 if (ret != NULL) {
1895 SSL_CTX_free(ret);
1896 }
1897 return NULL;
1898}
Adam Langley95c29f32014-06-20 12:00:00 -07001899
Adam Langleyfcf25832014-12-18 17:42:32 -08001900void SSL_CTX_free(SSL_CTX *a) {
1901 int i;
Adam Langley95c29f32014-06-20 12:00:00 -07001902
Adam Langleyfcf25832014-12-18 17:42:32 -08001903 if (a == NULL) {
1904 return;
1905 }
Adam Langley95c29f32014-06-20 12:00:00 -07001906
Adam Langleyfcf25832014-12-18 17:42:32 -08001907 i = CRYPTO_add(&a->references, -1, CRYPTO_LOCK_SSL_CTX);
1908 if (i > 0) {
1909 return;
1910 }
Adam Langley95c29f32014-06-20 12:00:00 -07001911
Adam Langleyfcf25832014-12-18 17:42:32 -08001912 if (a->param) {
1913 X509_VERIFY_PARAM_free(a->param);
1914 }
Adam Langley95c29f32014-06-20 12:00:00 -07001915
Adam Langleyfcf25832014-12-18 17:42:32 -08001916 /* Free internal session cache. However: the remove_cb() may reference the
1917 * ex_data of SSL_CTX, thus the ex_data store can only be removed after the
1918 * sessions were flushed. As the ex_data handling routines might also touch
1919 * the session cache, the most secure solution seems to be: empty (flush) the
1920 * cache, then free ex_data, then finally free the cache. (See ticket
1921 * [openssl.org #212].) */
1922 if (a->sessions != NULL) {
1923 SSL_CTX_flush_sessions(a, 0);
1924 }
Adam Langley95c29f32014-06-20 12:00:00 -07001925
Adam Langleyfcf25832014-12-18 17:42:32 -08001926 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001927
Adam Langleyfcf25832014-12-18 17:42:32 -08001928 if (a->sessions != NULL) {
1929 lh_SSL_SESSION_free(a->sessions);
1930 }
1931 if (a->cert_store != NULL) {
1932 X509_STORE_free(a->cert_store);
1933 }
1934 if (a->cipher_list != NULL) {
1935 ssl_cipher_preference_list_free(a->cipher_list);
1936 }
1937 if (a->cipher_list_by_id != NULL) {
1938 sk_SSL_CIPHER_free(a->cipher_list_by_id);
1939 }
1940 if (a->cipher_list_tls11 != NULL) {
1941 ssl_cipher_preference_list_free(a->cipher_list_tls11);
1942 }
1943 if (a->cert != NULL) {
1944 ssl_cert_free(a->cert);
1945 }
1946 if (a->client_CA != NULL) {
1947 sk_X509_NAME_pop_free(a->client_CA, X509_NAME_free);
1948 }
1949 if (a->extra_certs != NULL) {
1950 sk_X509_pop_free(a->extra_certs, X509_free);
1951 }
1952 if (a->srtp_profiles) {
1953 sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);
1954 }
1955 if (a->psk_identity_hint) {
1956 OPENSSL_free(a->psk_identity_hint);
1957 }
1958 if (a->tlsext_ecpointformatlist) {
1959 OPENSSL_free(a->tlsext_ecpointformatlist);
1960 }
1961 if (a->tlsext_ellipticcurvelist) {
1962 OPENSSL_free(a->tlsext_ellipticcurvelist);
1963 }
1964 if (a->alpn_client_proto_list != NULL) {
1965 OPENSSL_free(a->alpn_client_proto_list);
1966 }
1967 if (a->tlsext_channel_id_private) {
1968 EVP_PKEY_free(a->tlsext_channel_id_private);
1969 }
1970 if (a->keylog_bio) {
1971 BIO_free(a->keylog_bio);
1972 }
Adam Langley95c29f32014-06-20 12:00:00 -07001973
Adam Langleyfcf25832014-12-18 17:42:32 -08001974 OPENSSL_free(a);
1975}
Adam Langley95c29f32014-06-20 12:00:00 -07001976
Adam Langleyfcf25832014-12-18 17:42:32 -08001977void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) {
1978 ctx->default_passwd_callback = cb;
1979}
Adam Langley95c29f32014-06-20 12:00:00 -07001980
Adam Langleyfcf25832014-12-18 17:42:32 -08001981void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u) {
1982 ctx->default_passwd_callback_userdata = u;
1983}
Adam Langley95c29f32014-06-20 12:00:00 -07001984
Adam Langleyfcf25832014-12-18 17:42:32 -08001985void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
1986 int (*cb)(X509_STORE_CTX *, void *),
1987 void *arg) {
1988 ctx->app_verify_callback = cb;
1989 ctx->app_verify_arg = arg;
1990}
Adam Langley95c29f32014-06-20 12:00:00 -07001991
Adam Langleyfcf25832014-12-18 17:42:32 -08001992void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
1993 int (*cb)(int, X509_STORE_CTX *)) {
1994 ctx->verify_mode = mode;
1995 ctx->default_verify_callback = cb;
1996}
Adam Langley95c29f32014-06-20 12:00:00 -07001997
Adam Langleyfcf25832014-12-18 17:42:32 -08001998void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
1999 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
2000}
Adam Langley1258b6a2014-06-20 12:00:00 -07002001
Adam Langleyfcf25832014-12-18 17:42:32 -08002002void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb)(SSL *ssl, void *arg),
2003 void *arg) {
2004 ssl_cert_set_cert_cb(c->cert, cb, arg);
2005}
David Benjamin859ec3c2014-09-02 16:29:36 -04002006
Adam Langleyfcf25832014-12-18 17:42:32 -08002007void SSL_set_cert_cb(SSL *s, int (*cb)(SSL *ssl, void *arg), void *arg) {
2008 ssl_cert_set_cert_cb(s->cert, cb, arg);
2009}
Adam Langley95c29f32014-06-20 12:00:00 -07002010
Adam Langleyfcf25832014-12-18 17:42:32 -08002011static int ssl_has_key(SSL *s, size_t idx) {
2012 CERT_PKEY *cpk = &s->cert->pkeys[idx];
2013 return cpk->x509 && cpk->privatekey;
2014}
David Benjamin033e5f42014-11-13 18:47:41 -05002015
David Benjaminf31e6812014-11-13 18:05:55 -05002016void ssl_get_compatible_server_ciphers(SSL *s, unsigned long *out_mask_k,
Adam Langleyfcf25832014-12-18 17:42:32 -08002017 unsigned long *out_mask_a) {
2018 CERT *c = s->cert;
2019 int rsa_enc, rsa_sign, dh_tmp;
2020 unsigned long mask_k, mask_a;
2021 int have_ecc_cert, ecdsa_ok;
2022 int have_ecdh_tmp;
2023 X509 *x;
Adam Langley95c29f32014-06-20 12:00:00 -07002024
Adam Langleyfcf25832014-12-18 17:42:32 -08002025 if (c == NULL) {
2026 /* TODO(davidben): Is this codepath possible? */
2027 *out_mask_k = 0;
2028 *out_mask_a = 0;
2029 return;
2030 }
Adam Langley95c29f32014-06-20 12:00:00 -07002031
Adam Langleyfcf25832014-12-18 17:42:32 -08002032 dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07002033
Adam Langleyfcf25832014-12-18 17:42:32 -08002034 have_ecdh_tmp = (c->ecdh_tmp || c->ecdh_tmp_cb || c->ecdh_tmp_auto);
2035 rsa_enc = ssl_has_key(s, SSL_PKEY_RSA_ENC);
2036 rsa_sign = ssl_has_key(s, SSL_PKEY_RSA_SIGN);
2037 have_ecc_cert = ssl_has_key(s, SSL_PKEY_ECC);
2038 mask_k = 0;
2039 mask_a = 0;
David Benjaminf31e6812014-11-13 18:05:55 -05002040
Adam Langleyfcf25832014-12-18 17:42:32 -08002041 if (rsa_enc) {
2042 mask_k |= SSL_kRSA;
2043 }
2044 if (dh_tmp) {
David Benjamin7061e282015-03-19 11:10:48 -04002045 mask_k |= SSL_kDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08002046 }
2047 if (rsa_enc || rsa_sign) {
2048 mask_a |= SSL_aRSA;
2049 }
Adam Langley95c29f32014-06-20 12:00:00 -07002050
Adam Langleyfcf25832014-12-18 17:42:32 -08002051 mask_a |= SSL_aNULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002052
Adam Langleyfcf25832014-12-18 17:42:32 -08002053 /* An ECC certificate may be usable for ECDSA cipher suites depending on the
2054 * key usage extension and on the client's curve preferences. */
2055 if (have_ecc_cert) {
2056 x = c->pkeys[SSL_PKEY_ECC].x509;
2057 /* This call populates extension flags (ex_flags). */
2058 X509_check_purpose(x, -1, 0);
2059 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE)
2060 ? (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)
2061 : 1;
2062 if (!tls1_check_ec_cert(s, x)) {
2063 ecdsa_ok = 0;
2064 }
2065 if (ecdsa_ok) {
2066 mask_a |= SSL_aECDSA;
2067 }
2068 }
Adam Langley95c29f32014-06-20 12:00:00 -07002069
Adam Langleyfcf25832014-12-18 17:42:32 -08002070 /* If we are considering an ECC cipher suite that uses an ephemeral EC
2071 * key, check it. */
2072 if (have_ecdh_tmp && tls1_check_ec_tmp_key(s)) {
David Benjamin7061e282015-03-19 11:10:48 -04002073 mask_k |= SSL_kECDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08002074 }
Adam Langley95c29f32014-06-20 12:00:00 -07002075
Adam Langleyfcf25832014-12-18 17:42:32 -08002076 /* PSK requires a server callback. */
2077 if (s->psk_server_callback != NULL) {
2078 mask_k |= SSL_kPSK;
2079 mask_a |= SSL_aPSK;
2080 }
Adam Langley95c29f32014-06-20 12:00:00 -07002081
Adam Langleyfcf25832014-12-18 17:42:32 -08002082 *out_mask_k = mask_k;
2083 *out_mask_a = mask_a;
2084}
Adam Langley95c29f32014-06-20 12:00:00 -07002085
2086/* This handy macro borrowed from crypto/x509v3/v3_purp.c */
2087#define ku_reject(x, usage) \
Adam Langleyfcf25832014-12-18 17:42:32 -08002088 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
Adam Langley95c29f32014-06-20 12:00:00 -07002089
Adam Langleyfcf25832014-12-18 17:42:32 -08002090int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s) {
2091 unsigned long alg_a;
2092 int signature_nid = 0, md_nid = 0, pk_nid = 0;
2093 const SSL_CIPHER *cs = s->s3->tmp.new_cipher;
Adam Langley95c29f32014-06-20 12:00:00 -07002094
Adam Langleyfcf25832014-12-18 17:42:32 -08002095 alg_a = cs->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -07002096
Adam Langleyfcf25832014-12-18 17:42:32 -08002097 /* This call populates the ex_flags field correctly */
2098 X509_check_purpose(x, -1, 0);
2099 if (x->sig_alg && x->sig_alg->algorithm) {
2100 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
2101 OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
2102 }
2103 if (alg_a & SSL_aECDSA) {
2104 /* key usage, if present, must allow signing */
2105 if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE)) {
2106 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg,
2107 SSL_R_ECC_CERT_NOT_FOR_SIGNING);
2108 return 0;
2109 }
2110 }
Adam Langley95c29f32014-06-20 12:00:00 -07002111
Adam Langleyfcf25832014-12-18 17:42:32 -08002112 return 1; /* all checks are ok */
2113}
Adam Langley95c29f32014-06-20 12:00:00 -07002114
Adam Langleyfcf25832014-12-18 17:42:32 -08002115static int ssl_get_server_cert_index(const SSL *s) {
2116 int idx;
2117 idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
2118 if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509) {
2119 idx = SSL_PKEY_RSA_SIGN;
2120 }
2121 if (idx == -1) {
2122 OPENSSL_PUT_ERROR(SSL, ssl_get_server_cert_index, ERR_R_INTERNAL_ERROR);
2123 }
2124 return idx;
2125}
Adam Langley95c29f32014-06-20 12:00:00 -07002126
Adam Langleyfcf25832014-12-18 17:42:32 -08002127CERT_PKEY *ssl_get_server_send_pkey(const SSL *s) {
2128 int i = ssl_get_server_cert_index(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002129
Adam Langleyfcf25832014-12-18 17:42:32 -08002130 /* This may or may not be an error. */
2131 if (i < 0) {
2132 return NULL;
2133 }
Adam Langley95c29f32014-06-20 12:00:00 -07002134
Adam Langleyfcf25832014-12-18 17:42:32 -08002135 /* May be NULL. */
2136 return &s->cert->pkeys[i];
2137}
Adam Langley95c29f32014-06-20 12:00:00 -07002138
Adam Langleyfcf25832014-12-18 17:42:32 -08002139EVP_PKEY *ssl_get_sign_pkey(SSL *s, const SSL_CIPHER *cipher) {
2140 unsigned long alg_a;
2141 CERT *c;
2142 int idx = -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002143
Adam Langleyfcf25832014-12-18 17:42:32 -08002144 alg_a = cipher->algorithm_auth;
2145 c = s->cert;
Adam Langley95c29f32014-06-20 12:00:00 -07002146
Adam Langleyfcf25832014-12-18 17:42:32 -08002147 if (alg_a & SSL_aRSA) {
2148 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL) {
2149 idx = SSL_PKEY_RSA_SIGN;
2150 } else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL) {
2151 idx = SSL_PKEY_RSA_ENC;
2152 }
2153 } else if ((alg_a & SSL_aECDSA) &&
2154 (c->pkeys[SSL_PKEY_ECC].privatekey != NULL)) {
2155 idx = SSL_PKEY_ECC;
2156 }
Adam Langley95c29f32014-06-20 12:00:00 -07002157
Adam Langleyfcf25832014-12-18 17:42:32 -08002158 if (idx == -1) {
2159 OPENSSL_PUT_ERROR(SSL, ssl_get_sign_pkey, ERR_R_INTERNAL_ERROR);
2160 return NULL;
2161 }
Adam Langley95c29f32014-06-20 12:00:00 -07002162
Adam Langleyfcf25832014-12-18 17:42:32 -08002163 return c->pkeys[idx].privatekey;
2164}
Adam Langley95c29f32014-06-20 12:00:00 -07002165
Adam Langleyfcf25832014-12-18 17:42:32 -08002166void ssl_update_cache(SSL *s, int mode) {
2167 int i;
Adam Langley95c29f32014-06-20 12:00:00 -07002168
Adam Langleyfcf25832014-12-18 17:42:32 -08002169 /* If the session_id_length is 0, we are not supposed to cache it, and it
2170 * would be rather hard to do anyway :-) */
2171 if (s->session->session_id_length == 0) {
2172 return;
2173 }
Adam Langley95c29f32014-06-20 12:00:00 -07002174
Adam Langleyfcf25832014-12-18 17:42:32 -08002175 i = s->initial_ctx->session_cache_mode;
2176 if ((i & mode) && !s->hit &&
2177 ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE) ||
2178 SSL_CTX_add_session(s->initial_ctx, s->session)) &&
2179 s->initial_ctx->new_session_cb != NULL) {
David Benjamin33639842015-02-09 03:34:47 -05002180 if (!s->initial_ctx->new_session_cb(s, SSL_SESSION_up_ref(s->session))) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002181 SSL_SESSION_free(s->session);
2182 }
2183 }
Adam Langley95c29f32014-06-20 12:00:00 -07002184
Adam Langleyfcf25832014-12-18 17:42:32 -08002185 /* auto flush every 255 connections */
2186 if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) && ((i & mode) == mode)) {
2187 if ((((mode & SSL_SESS_CACHE_CLIENT)
2188 ? s->initial_ctx->stats.sess_connect_good
2189 : s->initial_ctx->stats.sess_accept_good) &
2190 0xff) == 0xff) {
2191 SSL_CTX_flush_sessions(s->initial_ctx, (unsigned long)time(NULL));
2192 }
2193 }
2194}
Adam Langley95c29f32014-06-20 12:00:00 -07002195
David Benjamin1a5c50f2015-03-11 16:22:37 -04002196int SSL_get_error(const SSL *s, int ret_code) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002197 int reason;
David Benjamin1a5c50f2015-03-11 16:22:37 -04002198 uint32_t err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002199 BIO *bio;
Adam Langley95c29f32014-06-20 12:00:00 -07002200
David Benjamin1a5c50f2015-03-11 16:22:37 -04002201 if (ret_code > 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002202 return SSL_ERROR_NONE;
2203 }
Adam Langley95c29f32014-06-20 12:00:00 -07002204
Adam Langleyfcf25832014-12-18 17:42:32 -08002205 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
2206 * where we do encode the error */
David Benjamin1a5c50f2015-03-11 16:22:37 -04002207 err = ERR_peek_error();
2208 if (err != 0) {
2209 if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002210 return SSL_ERROR_SYSCALL;
2211 }
2212 return SSL_ERROR_SSL;
2213 }
Adam Langley95c29f32014-06-20 12:00:00 -07002214
David Benjamin1a5c50f2015-03-11 16:22:37 -04002215 if (ret_code == 0) {
David Benjamin9a38e922015-01-22 16:06:11 -05002216 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2217 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) {
2218 /* The socket was cleanly shut down with a close_notify. */
2219 return SSL_ERROR_ZERO_RETURN;
2220 }
2221 /* An EOF was observed which violates the protocol, and the underlying
2222 * transport does not participate in the error queue. Bubble up to the
2223 * caller. */
2224 return SSL_ERROR_SYSCALL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002225 }
Adam Langleyb2ce0582014-06-20 12:00:00 -07002226
Adam Langleyfcf25832014-12-18 17:42:32 -08002227 if (SSL_want_session(s)) {
2228 return SSL_ERROR_PENDING_SESSION;
2229 }
Adam Langley95c29f32014-06-20 12:00:00 -07002230
Adam Langleyfcf25832014-12-18 17:42:32 -08002231 if (SSL_want_certificate(s)) {
2232 return SSL_ERROR_PENDING_CERTIFICATE;
2233 }
Adam Langley95c29f32014-06-20 12:00:00 -07002234
Adam Langleyfcf25832014-12-18 17:42:32 -08002235 if (SSL_want_read(s)) {
2236 bio = SSL_get_rbio(s);
2237 if (BIO_should_read(bio)) {
2238 return SSL_ERROR_WANT_READ;
2239 }
Adam Langley95c29f32014-06-20 12:00:00 -07002240
Adam Langleyfcf25832014-12-18 17:42:32 -08002241 if (BIO_should_write(bio)) {
2242 /* This one doesn't make too much sense ... We never try to write to the
2243 * rbio, and an application program where rbio and wbio are separate
2244 * couldn't even know what it should wait for. However if we ever set
2245 * s->rwstate incorrectly (so that we have SSL_want_read(s) instead of
2246 * SSL_want_write(s)) and rbio and wbio *are* the same, this test works
2247 * around that bug; so it might be safer to keep it. */
2248 return SSL_ERROR_WANT_WRITE;
2249 }
Adam Langley95c29f32014-06-20 12:00:00 -07002250
Adam Langleyfcf25832014-12-18 17:42:32 -08002251 if (BIO_should_io_special(bio)) {
2252 reason = BIO_get_retry_reason(bio);
2253 if (reason == BIO_RR_CONNECT) {
2254 return SSL_ERROR_WANT_CONNECT;
2255 }
Adam Langley95c29f32014-06-20 12:00:00 -07002256
Adam Langleyfcf25832014-12-18 17:42:32 -08002257 if (reason == BIO_RR_ACCEPT) {
2258 return SSL_ERROR_WANT_ACCEPT;
2259 }
Adam Langley95c29f32014-06-20 12:00:00 -07002260
Adam Langleyfcf25832014-12-18 17:42:32 -08002261 return SSL_ERROR_SYSCALL; /* unknown */
2262 }
2263 }
Adam Langley95c29f32014-06-20 12:00:00 -07002264
Adam Langleyfcf25832014-12-18 17:42:32 -08002265 if (SSL_want_write(s)) {
2266 bio = SSL_get_wbio(s);
2267 if (BIO_should_write(bio)) {
2268 return SSL_ERROR_WANT_WRITE;
2269 }
Adam Langley95c29f32014-06-20 12:00:00 -07002270
Adam Langleyfcf25832014-12-18 17:42:32 -08002271 if (BIO_should_read(bio)) {
2272 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2273 return SSL_ERROR_WANT_READ;
2274 }
Adam Langley95c29f32014-06-20 12:00:00 -07002275
Adam Langleyfcf25832014-12-18 17:42:32 -08002276 if (BIO_should_io_special(bio)) {
2277 reason = BIO_get_retry_reason(bio);
2278 if (reason == BIO_RR_CONNECT) {
2279 return SSL_ERROR_WANT_CONNECT;
2280 }
Adam Langley95c29f32014-06-20 12:00:00 -07002281
Adam Langleyfcf25832014-12-18 17:42:32 -08002282 if (reason == BIO_RR_ACCEPT) {
2283 return SSL_ERROR_WANT_ACCEPT;
2284 }
Adam Langley95c29f32014-06-20 12:00:00 -07002285
Adam Langleyfcf25832014-12-18 17:42:32 -08002286 return SSL_ERROR_SYSCALL;
2287 }
2288 }
Adam Langley95c29f32014-06-20 12:00:00 -07002289
Adam Langleyfcf25832014-12-18 17:42:32 -08002290 if (SSL_want_x509_lookup(s)) {
2291 return SSL_ERROR_WANT_X509_LOOKUP;
2292 }
Adam Langley95c29f32014-06-20 12:00:00 -07002293
Adam Langleyfcf25832014-12-18 17:42:32 -08002294 if (SSL_want_channel_id_lookup(s)) {
2295 return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
2296 }
Adam Langley0f4746e2014-08-13 12:26:32 -07002297
Adam Langleyfcf25832014-12-18 17:42:32 -08002298 return SSL_ERROR_SYSCALL;
2299}
Adam Langley0f4746e2014-08-13 12:26:32 -07002300
Adam Langleyfcf25832014-12-18 17:42:32 -08002301int SSL_do_handshake(SSL *s) {
2302 int ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002303
Adam Langleyfcf25832014-12-18 17:42:32 -08002304 if (s->handshake_func == NULL) {
2305 OPENSSL_PUT_ERROR(SSL, SSL_do_handshake, SSL_R_CONNECTION_TYPE_NOT_SET);
2306 return -1;
2307 }
Adam Langley95c29f32014-06-20 12:00:00 -07002308
Adam Langleyfcf25832014-12-18 17:42:32 -08002309 s->method->ssl_renegotiate_check(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002310
Adam Langleyfcf25832014-12-18 17:42:32 -08002311 if (SSL_in_init(s)) {
2312 ret = s->handshake_func(s);
2313 }
2314 return ret;
2315}
Adam Langley95c29f32014-06-20 12:00:00 -07002316
Adam Langleyfcf25832014-12-18 17:42:32 -08002317void SSL_set_accept_state(SSL *s) {
2318 s->server = 1;
2319 s->shutdown = 0;
2320 s->state = SSL_ST_ACCEPT | SSL_ST_BEFORE;
2321 s->handshake_func = s->method->ssl_accept;
2322 /* clear the current cipher */
2323 ssl_clear_cipher_ctx(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002324}
Adam Langley95c29f32014-06-20 12:00:00 -07002325
Adam Langleyfcf25832014-12-18 17:42:32 -08002326void SSL_set_connect_state(SSL *s) {
2327 s->server = 0;
2328 s->shutdown = 0;
2329 s->state = SSL_ST_CONNECT | SSL_ST_BEFORE;
2330 s->handshake_func = s->method->ssl_connect;
2331 /* clear the current cipher */
2332 ssl_clear_cipher_ctx(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002333}
Adam Langley95c29f32014-06-20 12:00:00 -07002334
Adam Langleyfcf25832014-12-18 17:42:32 -08002335int ssl_undefined_function(SSL *s) {
2336 OPENSSL_PUT_ERROR(SSL, ssl_undefined_function,
2337 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2338 return 0;
2339}
Adam Langley95c29f32014-06-20 12:00:00 -07002340
Adam Langleyfcf25832014-12-18 17:42:32 -08002341int ssl_undefined_void_function(void) {
2342 OPENSSL_PUT_ERROR(SSL, ssl_undefined_void_function,
2343 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2344 return 0;
2345}
Adam Langley95c29f32014-06-20 12:00:00 -07002346
Adam Langleyfcf25832014-12-18 17:42:32 -08002347int ssl_undefined_const_function(const SSL *s) {
2348 OPENSSL_PUT_ERROR(SSL, ssl_undefined_const_function,
2349 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2350 return 0;
2351}
Adam Langley95c29f32014-06-20 12:00:00 -07002352
Adam Langleyfcf25832014-12-18 17:42:32 -08002353static const char *ssl_get_version(int version) {
2354 switch (version) {
2355 case TLS1_2_VERSION:
2356 return "TLSv1.2";
Adam Langley95c29f32014-06-20 12:00:00 -07002357
Adam Langleyfcf25832014-12-18 17:42:32 -08002358 case TLS1_1_VERSION:
2359 return "TLSv1.1";
Adam Langley95c29f32014-06-20 12:00:00 -07002360
Adam Langleyfcf25832014-12-18 17:42:32 -08002361 case TLS1_VERSION:
2362 return "TLSv1";
Adam Langley95c29f32014-06-20 12:00:00 -07002363
Adam Langleyfcf25832014-12-18 17:42:32 -08002364 case SSL3_VERSION:
2365 return "SSLv3";
Adam Langley95c29f32014-06-20 12:00:00 -07002366
Adam Langleyfcf25832014-12-18 17:42:32 -08002367 default:
2368 return "unknown";
2369 }
2370}
Adam Langley95c29f32014-06-20 12:00:00 -07002371
Adam Langleyfcf25832014-12-18 17:42:32 -08002372const char *SSL_get_version(const SSL *s) {
2373 return ssl_get_version(s->version);
2374}
Adam Langley95c29f32014-06-20 12:00:00 -07002375
Adam Langleyfcf25832014-12-18 17:42:32 -08002376const char *SSL_SESSION_get_version(const SSL_SESSION *sess) {
2377 return ssl_get_version(sess->ssl_version);
2378}
Adam Langley95c29f32014-06-20 12:00:00 -07002379
Adam Langleyfcf25832014-12-18 17:42:32 -08002380void ssl_clear_cipher_ctx(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002381 if (s->aead_read_ctx != NULL) {
2382 EVP_AEAD_CTX_cleanup(&s->aead_read_ctx->ctx);
2383 OPENSSL_free(s->aead_read_ctx);
2384 s->aead_read_ctx = NULL;
2385 }
Adam Langleya5dc5452014-06-20 12:00:00 -07002386
Adam Langleyfcf25832014-12-18 17:42:32 -08002387 if (s->aead_write_ctx != NULL) {
2388 EVP_AEAD_CTX_cleanup(&s->aead_write_ctx->ctx);
2389 OPENSSL_free(s->aead_write_ctx);
2390 s->aead_write_ctx = NULL;
2391 }
2392}
Adam Langley95c29f32014-06-20 12:00:00 -07002393
Adam Langleyfcf25832014-12-18 17:42:32 -08002394X509 *SSL_get_certificate(const SSL *s) {
2395 if (s->cert != NULL) {
2396 return s->cert->key->x509;
2397 }
2398
2399 return NULL;
2400}
2401
2402EVP_PKEY *SSL_get_privatekey(const SSL *s) {
2403 if (s->cert != NULL) {
2404 return s->cert->key->privatekey;
2405 }
2406
2407 return NULL;
2408}
2409
2410X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
2411 if (ctx->cert != NULL) {
2412 return ctx->cert->key->x509;
2413 }
2414
2415 return NULL;
2416}
2417
2418EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
2419 if (ctx->cert != NULL) {
2420 return ctx->cert->key->privatekey;
2421 }
2422
2423 return NULL;
2424}
2425
2426const SSL_CIPHER *SSL_get_current_cipher(const SSL *s) {
2427 if (s->session != NULL && s->session->cipher != NULL) {
2428 return s->session->cipher;
2429 }
2430
2431 return NULL;
2432}
2433
2434const void *SSL_get_current_compression(SSL *s) { return NULL; }
2435
2436const void *SSL_get_current_expansion(SSL *s) { return NULL; }
2437
2438int ssl_init_wbio_buffer(SSL *s, int push) {
2439 BIO *bbio;
2440
2441 if (s->bbio == NULL) {
2442 bbio = BIO_new(BIO_f_buffer());
2443 if (bbio == NULL) {
2444 return 0;
2445 }
2446 s->bbio = bbio;
2447 } else {
2448 bbio = s->bbio;
2449 if (s->bbio == s->wbio) {
2450 s->wbio = BIO_pop(s->wbio);
2451 }
2452 }
2453
2454 BIO_reset(bbio);
2455 if (!BIO_set_read_buffer_size(bbio, 1)) {
2456 OPENSSL_PUT_ERROR(SSL, ssl_init_wbio_buffer, ERR_R_BUF_LIB);
2457 return 0;
2458 }
2459
2460 if (push) {
2461 if (s->wbio != bbio) {
2462 s->wbio = BIO_push(bbio, s->wbio);
2463 }
2464 } else {
2465 if (s->wbio == bbio) {
2466 s->wbio = BIO_pop(bbio);
2467 }
2468 }
2469
2470 return 1;
2471}
2472
2473void ssl_free_wbio_buffer(SSL *s) {
2474 if (s->bbio == NULL) {
2475 return;
2476 }
2477
2478 if (s->bbio == s->wbio) {
2479 /* remove buffering */
2480 s->wbio = BIO_pop(s->wbio);
2481 }
2482
2483 BIO_free(s->bbio);
2484 s->bbio = NULL;
2485}
2486
2487void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
2488 ctx->quiet_shutdown = mode;
2489}
2490
2491int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
2492 return ctx->quiet_shutdown;
2493}
2494
2495void SSL_set_quiet_shutdown(SSL *s, int mode) { s->quiet_shutdown = mode; }
2496
2497int SSL_get_quiet_shutdown(const SSL *s) { return s->quiet_shutdown; }
2498
2499void SSL_set_shutdown(SSL *s, int mode) { s->shutdown = mode; }
2500
2501int SSL_get_shutdown(const SSL *s) { return s->shutdown; }
2502
2503int SSL_version(const SSL *s) { return s->version; }
2504
2505SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
2506
2507SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
2508 if (ssl->ctx == ctx) {
2509 return ssl->ctx;
2510 }
2511
2512 if (ctx == NULL) {
2513 ctx = ssl->initial_ctx;
2514 }
2515
2516 if (ssl->cert != NULL) {
2517 ssl_cert_free(ssl->cert);
2518 }
2519
2520 ssl->cert = ssl_cert_dup(ctx->cert);
2521 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
2522 if (ssl->ctx != NULL) {
2523 SSL_CTX_free(ssl->ctx); /* decrement reference count */
2524 }
2525 ssl->ctx = ctx;
2526
2527 ssl->sid_ctx_length = ctx->sid_ctx_length;
2528 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2529 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2530
2531 return ssl->ctx;
2532}
2533
2534int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
2535 return X509_STORE_set_default_paths(ctx->cert_store);
2536}
Adam Langley95c29f32014-06-20 12:00:00 -07002537
2538int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
Adam Langleyfcf25832014-12-18 17:42:32 -08002539 const char *CApath) {
2540 return X509_STORE_load_locations(ctx->cert_store, CAfile, CApath);
2541}
Adam Langley95c29f32014-06-20 12:00:00 -07002542
2543void SSL_set_info_callback(SSL *ssl,
Adam Langleyfcf25832014-12-18 17:42:32 -08002544 void (*cb)(const SSL *ssl, int type, int val)) {
2545 ssl->info_callback = cb;
2546}
Adam Langley95c29f32014-06-20 12:00:00 -07002547
Adam Langleyfcf25832014-12-18 17:42:32 -08002548void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/, int /*type*/,
2549 int /*val*/) {
2550 return ssl->info_callback;
2551}
Adam Langley95c29f32014-06-20 12:00:00 -07002552
Adam Langleyfcf25832014-12-18 17:42:32 -08002553int SSL_state(const SSL *ssl) { return ssl->state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002554
Adam Langleyfcf25832014-12-18 17:42:32 -08002555void SSL_set_state(SSL *ssl, int state) { ssl->state = state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002556
Adam Langleyfcf25832014-12-18 17:42:32 -08002557void SSL_set_verify_result(SSL *ssl, long arg) { ssl->verify_result = arg; }
Adam Langley95c29f32014-06-20 12:00:00 -07002558
Adam Langleyfcf25832014-12-18 17:42:32 -08002559long SSL_get_verify_result(const SSL *ssl) { return ssl->verify_result; }
Adam Langley95c29f32014-06-20 12:00:00 -07002560
Adam Langleyfcf25832014-12-18 17:42:32 -08002561int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2562 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
2563 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp, new_func,
2564 dup_func, free_func);
2565}
Adam Langley95c29f32014-06-20 12:00:00 -07002566
Adam Langleyfcf25832014-12-18 17:42:32 -08002567int SSL_set_ex_data(SSL *s, int idx, void *arg) {
2568 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2569}
Adam Langley95c29f32014-06-20 12:00:00 -07002570
Adam Langleyfcf25832014-12-18 17:42:32 -08002571void *SSL_get_ex_data(const SSL *s, int idx) {
2572 return CRYPTO_get_ex_data(&s->ex_data, idx);
2573}
Adam Langley95c29f32014-06-20 12:00:00 -07002574
Adam Langleyfcf25832014-12-18 17:42:32 -08002575int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2576 CRYPTO_EX_dup *dup_func,
2577 CRYPTO_EX_free *free_func) {
2578 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp, new_func,
2579 dup_func, free_func);
2580}
Adam Langley95c29f32014-06-20 12:00:00 -07002581
Adam Langleyfcf25832014-12-18 17:42:32 -08002582int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg) {
2583 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2584}
Adam Langley95c29f32014-06-20 12:00:00 -07002585
Adam Langleyfcf25832014-12-18 17:42:32 -08002586void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx) {
2587 return CRYPTO_get_ex_data(&s->ex_data, idx);
2588}
Adam Langley95c29f32014-06-20 12:00:00 -07002589
Adam Langleyfcf25832014-12-18 17:42:32 -08002590int ssl_ok(SSL *s) { return 1; }
Adam Langley95c29f32014-06-20 12:00:00 -07002591
Adam Langleyfcf25832014-12-18 17:42:32 -08002592X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
2593 return ctx->cert_store;
2594}
Adam Langley95c29f32014-06-20 12:00:00 -07002595
Adam Langleyfcf25832014-12-18 17:42:32 -08002596void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
2597 if (ctx->cert_store != NULL) {
2598 X509_STORE_free(ctx->cert_store);
2599 }
2600 ctx->cert_store = store;
2601}
Adam Langley95c29f32014-06-20 12:00:00 -07002602
Adam Langleyfcf25832014-12-18 17:42:32 -08002603int SSL_want(const SSL *s) { return s->rwstate; }
Adam Langley95c29f32014-06-20 12:00:00 -07002604
Adam Langleyfcf25832014-12-18 17:42:32 -08002605void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
2606 RSA *(*cb)(SSL *ssl, int is_export,
2607 int keylength)) {
2608 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_RSA_CB, (void (*)(void))cb);
2609}
Adam Langley95c29f32014-06-20 12:00:00 -07002610
Adam Langleyfcf25832014-12-18 17:42:32 -08002611void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
2612 int keylength)) {
2613 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_RSA_CB, (void (*)(void))cb);
2614}
2615
2616void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
2617 DH *(*dh)(SSL *ssl, int is_export,
2618 int keylength)) {
2619 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh);
2620}
2621
2622void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*dh)(SSL *ssl, int is_export,
2623 int keylength)) {
2624 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh);
2625}
2626
2627void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,
2628 EC_KEY *(*ecdh)(SSL *ssl, int is_export,
2629 int keylength)) {
2630 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH_CB, (void (*)(void))ecdh);
2631}
2632
2633void SSL_set_tmp_ecdh_callback(SSL *ssl,
2634 EC_KEY *(*ecdh)(SSL *ssl, int is_export,
2635 int keylength)) {
2636 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_ECDH_CB, (void (*)(void))ecdh);
2637}
2638
2639int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
2640 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2641 OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_psk_identity_hint,
2642 SSL_R_DATA_LENGTH_TOO_LONG);
2643 return 0;
2644 }
2645
2646 if (ctx->psk_identity_hint != NULL) {
2647 OPENSSL_free(ctx->psk_identity_hint);
2648 }
2649
2650 if (identity_hint != NULL) {
2651 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2652 if (ctx->psk_identity_hint == NULL) {
2653 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002654 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002655 } else {
2656 ctx->psk_identity_hint = NULL;
2657 }
Adam Langley95c29f32014-06-20 12:00:00 -07002658
Adam Langleyfcf25832014-12-18 17:42:32 -08002659 return 1;
2660}
2661
2662int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint) {
2663 if (s == NULL) {
2664 return 0;
2665 }
2666
2667 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2668 OPENSSL_PUT_ERROR(SSL, SSL_use_psk_identity_hint,
2669 SSL_R_DATA_LENGTH_TOO_LONG);
2670 return 0;
2671 }
2672
2673 /* Clear currently configured hint, if any. */
2674 if (s->psk_identity_hint != NULL) {
2675 OPENSSL_free(s->psk_identity_hint);
2676 s->psk_identity_hint = NULL;
2677 }
2678
2679 if (identity_hint != NULL) {
2680 s->psk_identity_hint = BUF_strdup(identity_hint);
2681 if (s->psk_identity_hint == NULL) {
2682 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002683 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002684 }
Adam Langley95c29f32014-06-20 12:00:00 -07002685
Adam Langleyfcf25832014-12-18 17:42:32 -08002686 return 1;
2687}
Adam Langley95c29f32014-06-20 12:00:00 -07002688
Adam Langleyfcf25832014-12-18 17:42:32 -08002689const char *SSL_get_psk_identity_hint(const SSL *s) {
2690 if (s == NULL) {
2691 return NULL;
2692 }
2693 return s->psk_identity_hint;
2694}
Adam Langley95c29f32014-06-20 12:00:00 -07002695
Adam Langleyfcf25832014-12-18 17:42:32 -08002696const char *SSL_get_psk_identity(const SSL *s) {
2697 if (s == NULL || s->session == NULL) {
2698 return NULL;
2699 }
Adam Langley95c29f32014-06-20 12:00:00 -07002700
Adam Langleyfcf25832014-12-18 17:42:32 -08002701 return s->session->psk_identity;
2702}
Adam Langley95c29f32014-06-20 12:00:00 -07002703
Adam Langleyfcf25832014-12-18 17:42:32 -08002704void SSL_set_psk_client_callback(
2705 SSL *s, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2706 unsigned int max_identity_len, uint8_t *psk,
2707 unsigned int max_psk_len)) {
2708 s->psk_client_callback = cb;
2709}
Adam Langley95c29f32014-06-20 12:00:00 -07002710
Adam Langleyfcf25832014-12-18 17:42:32 -08002711void SSL_CTX_set_psk_client_callback(
2712 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2713 unsigned int max_identity_len,
2714 uint8_t *psk, unsigned int max_psk_len)) {
2715 ctx->psk_client_callback = cb;
2716}
Adam Langley95c29f32014-06-20 12:00:00 -07002717
Adam Langleyfcf25832014-12-18 17:42:32 -08002718void SSL_set_psk_server_callback(
2719 SSL *s, unsigned int (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
2720 unsigned int max_psk_len)) {
2721 s->psk_server_callback = cb;
2722}
Adam Langley95c29f32014-06-20 12:00:00 -07002723
Adam Langleyfcf25832014-12-18 17:42:32 -08002724void SSL_CTX_set_psk_server_callback(
2725 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *identity,
2726 uint8_t *psk, unsigned int max_psk_len)) {
2727 ctx->psk_server_callback = cb;
2728}
Adam Langley95c29f32014-06-20 12:00:00 -07002729
Adam Langleyfcf25832014-12-18 17:42:32 -08002730void SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) {
2731 ctx->min_version = version;
2732}
Adam Langley95c29f32014-06-20 12:00:00 -07002733
Adam Langleyfcf25832014-12-18 17:42:32 -08002734void SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) {
2735 ctx->max_version = version;
2736}
Adam Langley0289c732014-06-20 12:00:00 -07002737
Adam Langleyfcf25832014-12-18 17:42:32 -08002738void SSL_set_min_version(SSL *ssl, uint16_t version) {
2739 ssl->min_version = version;
2740}
Adam Langley0289c732014-06-20 12:00:00 -07002741
Adam Langleyfcf25832014-12-18 17:42:32 -08002742void SSL_set_max_version(SSL *ssl, uint16_t version) {
2743 ssl->max_version = version;
2744}
Adam Langley95c29f32014-06-20 12:00:00 -07002745
Adam Langleyfcf25832014-12-18 17:42:32 -08002746void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
2747 void (*cb)(int write_p, int version,
2748 int content_type, const void *buf,
2749 size_t len, SSL *ssl, void *arg)) {
2750 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2751}
2752void SSL_set_msg_callback(SSL *ssl,
2753 void (*cb)(int write_p, int version, int content_type,
2754 const void *buf, size_t len, SSL *ssl,
2755 void *arg)) {
2756 SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2757}
Adam Langley95c29f32014-06-20 12:00:00 -07002758
Adam Langleyfcf25832014-12-18 17:42:32 -08002759void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio) {
2760 if (ctx->keylog_bio != NULL) {
2761 BIO_free(ctx->keylog_bio);
2762 }
2763 ctx->keylog_bio = keylog_bio;
2764}
Adam Langley95c29f32014-06-20 12:00:00 -07002765
Adam Langleyfcf25832014-12-18 17:42:32 -08002766static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
2767 static const char hextable[] = "0123456789abcdef";
2768 uint8_t *out;
2769 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002770
Adam Langleyfcf25832014-12-18 17:42:32 -08002771 if (!CBB_add_space(cbb, &out, in_len * 2)) {
2772 return 0;
2773 }
Adam Langley95c29f32014-06-20 12:00:00 -07002774
Adam Langleyfcf25832014-12-18 17:42:32 -08002775 for (i = 0; i < in_len; i++) {
2776 *(out++) = (uint8_t)hextable[in[i] >> 4];
2777 *(out++) = (uint8_t)hextable[in[i] & 0xf];
2778 }
Adam Langley95c29f32014-06-20 12:00:00 -07002779
Adam Langleyfcf25832014-12-18 17:42:32 -08002780 return 1;
2781}
David Benjamin859ec3c2014-09-02 16:29:36 -04002782
2783int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -08002784 const uint8_t *encrypted_premaster,
2785 size_t encrypted_premaster_len,
2786 const uint8_t *premaster,
2787 size_t premaster_len) {
2788 BIO *bio = ctx->keylog_bio;
2789 CBB cbb;
2790 uint8_t *out;
2791 size_t out_len;
2792 int ret;
David Benjamin859ec3c2014-09-02 16:29:36 -04002793
Adam Langleyfcf25832014-12-18 17:42:32 -08002794 if (bio == NULL) {
2795 return 1;
2796 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002797
Adam Langleyfcf25832014-12-18 17:42:32 -08002798 if (encrypted_premaster_len < 8) {
2799 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_rsa_client_key_exchange,
2800 ERR_R_INTERNAL_ERROR);
2801 return 0;
2802 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002803
Adam Langleyfcf25832014-12-18 17:42:32 -08002804 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len * 2 + 1)) {
2805 return 0;
2806 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002807
Adam Langleyfcf25832014-12-18 17:42:32 -08002808 if (!CBB_add_bytes(&cbb, (const uint8_t *)"RSA ", 4) ||
2809 /* Only the first 8 bytes of the encrypted premaster secret are
2810 * logged. */
2811 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
2812 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2813 !cbb_add_hex(&cbb, premaster, premaster_len) ||
2814 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2815 !CBB_finish(&cbb, &out, &out_len)) {
2816 CBB_cleanup(&cbb);
2817 return 0;
2818 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002819
Adam Langleyfcf25832014-12-18 17:42:32 -08002820 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2821 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2822 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
David Benjamin859ec3c2014-09-02 16:29:36 -04002823
Adam Langleyfcf25832014-12-18 17:42:32 -08002824 OPENSSL_free(out);
2825 return ret;
Adam Langley95f22882014-06-20 12:00:00 -07002826}
2827
Adam Langleyfcf25832014-12-18 17:42:32 -08002828int ssl_ctx_log_master_secret(SSL_CTX *ctx, const uint8_t *client_random,
2829 size_t client_random_len, const uint8_t *master,
2830 size_t master_len) {
2831 BIO *bio = ctx->keylog_bio;
2832 CBB cbb;
2833 uint8_t *out;
2834 size_t out_len;
2835 int ret;
Adam Langleyadb739e2014-06-20 12:00:00 -07002836
Adam Langleyfcf25832014-12-18 17:42:32 -08002837 if (bio == NULL) {
2838 return 1;
2839 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002840
Adam Langleyfcf25832014-12-18 17:42:32 -08002841 if (client_random_len != 32) {
2842 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_master_secret, ERR_R_INTERNAL_ERROR);
2843 return 0;
2844 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002845
Adam Langleyfcf25832014-12-18 17:42:32 -08002846 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len * 2 + 1)) {
2847 return 0;
2848 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002849
Adam Langleyfcf25832014-12-18 17:42:32 -08002850 if (!CBB_add_bytes(&cbb, (const uint8_t *)"CLIENT_RANDOM ", 14) ||
2851 !cbb_add_hex(&cbb, client_random, 32) ||
2852 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2853 !cbb_add_hex(&cbb, master, master_len) ||
2854 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2855 !CBB_finish(&cbb, &out, &out_len)) {
2856 CBB_cleanup(&cbb);
2857 return 0;
2858 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002859
Adam Langleyfcf25832014-12-18 17:42:32 -08002860 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2861 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2862 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
David Benjamine99e9122014-12-11 01:46:01 -05002863
Adam Langleyfcf25832014-12-18 17:42:32 -08002864 OPENSSL_free(out);
2865 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -07002866}
2867
David Benjamined7c4752015-02-16 19:16:46 -05002868int SSL_in_false_start(const SSL *s) {
2869 return s->s3->tmp.in_false_start;
2870}
2871
Adam Langleyfcf25832014-12-18 17:42:32 -08002872int SSL_cutthrough_complete(const SSL *s) {
David Benjamined7c4752015-02-16 19:16:46 -05002873 return SSL_in_false_start(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002874}
Adam Langley95c29f32014-06-20 12:00:00 -07002875
Adam Langleyfcf25832014-12-18 17:42:32 -08002876void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
2877 size_t *ssl_session_size) {
2878 *ssl_size = sizeof(SSL);
2879 *ssl_ctx_size = sizeof(SSL_CTX);
2880 *ssl_session_size = sizeof(SSL_SESSION);
2881}
Feng Lu41aa3252014-11-21 22:47:56 -08002882
David Benjamined7c4752015-02-16 19:16:46 -05002883int ssl3_can_false_start(const SSL *s) {
David Benjamin195dc782015-02-19 13:27:05 -05002884 const SSL_CIPHER *const cipher = SSL_get_current_cipher(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002885
David Benjamin195dc782015-02-19 13:27:05 -05002886 /* False Start only for TLS 1.2 with a forward-secure, AEAD cipher and ALPN or
2887 * NPN. */
2888 return !SSL_IS_DTLS(s) &&
2889 SSL_version(s) >= TLS1_2_VERSION &&
2890 (s->s3->alpn_selected || s->s3->next_proto_neg_seen) &&
2891 cipher != NULL &&
David Benjamin7061e282015-03-19 11:10:48 -04002892 (cipher->algorithm_mkey == SSL_kDHE ||
2893 cipher->algorithm_mkey == SSL_kECDHE) &&
David Benjamin195dc782015-02-19 13:27:05 -05002894 (cipher->algorithm_enc == SSL_AES128GCM ||
2895 cipher->algorithm_enc == SSL_AES256GCM ||
2896 cipher->algorithm_enc == SSL_CHACHA20POLY1305);
Adam Langleyfcf25832014-12-18 17:42:32 -08002897}
2898
2899const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version) {
2900 switch (version) {
2901 case SSL3_VERSION:
2902 return &SSLv3_enc_data;
2903
2904 case TLS1_VERSION:
2905 return &TLSv1_enc_data;
2906
David Benjamin9e13e1a2015-03-05 01:56:32 -05002907 case DTLS1_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002908 case TLS1_1_VERSION:
2909 return &TLSv1_1_enc_data;
2910
David Benjamin9e13e1a2015-03-05 01:56:32 -05002911 case DTLS1_2_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002912 case TLS1_2_VERSION:
2913 return &TLSv1_2_enc_data;
2914
Adam Langleyfcf25832014-12-18 17:42:32 -08002915 default:
2916 return NULL;
2917 }
2918}
2919
2920uint16_t ssl3_get_max_server_version(const SSL *s) {
2921 uint16_t max_version;
2922
2923 if (SSL_IS_DTLS(s)) {
2924 max_version = (s->max_version != 0) ? s->max_version : DTLS1_2_VERSION;
2925 if (!(s->options & SSL_OP_NO_DTLSv1_2) && DTLS1_2_VERSION >= max_version) {
2926 return DTLS1_2_VERSION;
2927 }
2928 if (!(s->options & SSL_OP_NO_DTLSv1) && DTLS1_VERSION >= max_version) {
2929 return DTLS1_VERSION;
2930 }
2931 return 0;
2932 }
2933
2934 max_version = (s->max_version != 0) ? s->max_version : TLS1_2_VERSION;
2935 if (!(s->options & SSL_OP_NO_TLSv1_2) && TLS1_2_VERSION <= max_version) {
2936 return TLS1_2_VERSION;
2937 }
2938 if (!(s->options & SSL_OP_NO_TLSv1_1) && TLS1_1_VERSION <= max_version) {
2939 return TLS1_1_VERSION;
2940 }
2941 if (!(s->options & SSL_OP_NO_TLSv1) && TLS1_VERSION <= max_version) {
2942 return TLS1_VERSION;
2943 }
2944 if (!(s->options & SSL_OP_NO_SSLv3) && SSL3_VERSION <= max_version) {
2945 return SSL3_VERSION;
2946 }
2947 return 0;
2948}
2949
2950uint16_t ssl3_get_mutual_version(SSL *s, uint16_t client_version) {
2951 uint16_t version = 0;
2952
2953 if (SSL_IS_DTLS(s)) {
2954 /* Clamp client_version to max_version. */
2955 if (s->max_version != 0 && client_version < s->max_version) {
2956 client_version = s->max_version;
2957 }
2958
2959 if (client_version <= DTLS1_2_VERSION && !(s->options & SSL_OP_NO_DTLSv1_2)) {
2960 version = DTLS1_2_VERSION;
2961 } else if (client_version <= DTLS1_VERSION &&
David Benjaminb18f0242015-03-10 18:30:08 -04002962 !(s->options & SSL_OP_NO_DTLSv1)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002963 version = DTLS1_VERSION;
2964 }
2965
2966 /* Check against min_version. */
2967 if (version != 0 && s->min_version != 0 && version > s->min_version) {
2968 return 0;
2969 }
2970 return version;
2971 } else {
2972 /* Clamp client_version to max_version. */
2973 if (s->max_version != 0 && client_version > s->max_version) {
2974 client_version = s->max_version;
2975 }
2976
2977 if (client_version >= TLS1_2_VERSION && !(s->options & SSL_OP_NO_TLSv1_2)) {
2978 version = TLS1_2_VERSION;
2979 } else if (client_version >= TLS1_1_VERSION &&
2980 !(s->options & SSL_OP_NO_TLSv1_1)) {
2981 version = TLS1_1_VERSION;
2982 } else if (client_version >= TLS1_VERSION && !(s->options & SSL_OP_NO_TLSv1)) {
2983 version = TLS1_VERSION;
2984 } else if (client_version >= SSL3_VERSION && !(s->options & SSL_OP_NO_SSLv3)) {
2985 version = SSL3_VERSION;
2986 }
2987
2988 /* Check against min_version. */
2989 if (version != 0 && s->min_version != 0 && version < s->min_version) {
2990 return 0;
2991 }
2992 return version;
2993 }
2994}
2995
2996uint16_t ssl3_get_max_client_version(SSL *s) {
2997 unsigned long options = s->options;
2998 uint16_t version = 0;
2999
3000 /* OpenSSL's API for controlling versions entails blacklisting individual
3001 * protocols. This has two problems. First, on the client, the protocol can
3002 * only express a contiguous range of versions. Second, a library consumer
3003 * trying to set a maximum version cannot disable protocol versions that get
3004 * added in a future version of the library.
3005 *
3006 * To account for both of these, OpenSSL interprets the client-side bitmask
3007 * as a min/max range by picking the lowest contiguous non-empty range of
3008 * enabled protocols. Note that this means it is impossible to set a maximum
3009 * version of TLS 1.2 in a future-proof way.
3010 *
3011 * By this scheme, the maximum version is the lowest version V such that V is
3012 * enabled and V+1 is disabled or unimplemented. */
3013 if (SSL_IS_DTLS(s)) {
3014 if (!(options & SSL_OP_NO_DTLSv1_2)) {
3015 version = DTLS1_2_VERSION;
3016 }
3017 if (!(options & SSL_OP_NO_DTLSv1) && (options & SSL_OP_NO_DTLSv1_2)) {
3018 version = DTLS1_VERSION;
3019 }
3020 if (s->max_version != 0 && version < s->max_version) {
3021 version = s->max_version;
3022 }
3023 } else {
3024 if (!(options & SSL_OP_NO_TLSv1_2)) {
3025 version = TLS1_2_VERSION;
3026 }
3027 if (!(options & SSL_OP_NO_TLSv1_1) && (options & SSL_OP_NO_TLSv1_2)) {
3028 version = TLS1_1_VERSION;
3029 }
3030 if (!(options & SSL_OP_NO_TLSv1) && (options & SSL_OP_NO_TLSv1_1)) {
3031 version = TLS1_VERSION;
3032 }
3033 if (!(options & SSL_OP_NO_SSLv3) && (options & SSL_OP_NO_TLSv1)) {
3034 version = SSL3_VERSION;
3035 }
3036 if (s->max_version != 0 && version > s->max_version) {
3037 version = s->max_version;
3038 }
3039 }
3040
3041 return version;
3042}
3043
3044int ssl3_is_version_enabled(SSL *s, uint16_t version) {
3045 if (SSL_IS_DTLS(s)) {
3046 if (s->max_version != 0 && version < s->max_version) {
3047 return 0;
3048 }
3049 if (s->min_version != 0 && version > s->min_version) {
3050 return 0;
3051 }
3052
3053 switch (version) {
3054 case DTLS1_VERSION:
3055 return !(s->options & SSL_OP_NO_DTLSv1);
3056
3057 case DTLS1_2_VERSION:
3058 return !(s->options & SSL_OP_NO_DTLSv1_2);
3059
3060 default:
3061 return 0;
3062 }
3063 } else {
3064 if (s->max_version != 0 && version > s->max_version) {
3065 return 0;
3066 }
3067 if (s->min_version != 0 && version < s->min_version) {
3068 return 0;
3069 }
3070
3071 switch (version) {
3072 case SSL3_VERSION:
3073 return !(s->options & SSL_OP_NO_SSLv3);
3074
3075 case TLS1_VERSION:
3076 return !(s->options & SSL_OP_NO_TLSv1);
3077
3078 case TLS1_1_VERSION:
3079 return !(s->options & SSL_OP_NO_TLSv1_1);
3080
3081 case TLS1_2_VERSION:
3082 return !(s->options & SSL_OP_NO_TLSv1_2);
3083
3084 default:
3085 return 0;
3086 }
3087 }
3088}
3089
David Benjaminea72bd02014-12-21 21:27:41 -05003090uint16_t ssl3_version_from_wire(SSL *s, uint16_t wire_version) {
3091 if (!SSL_IS_DTLS(s)) {
3092 return wire_version;
3093 }
3094
3095 uint16_t tls_version = ~wire_version;
3096 uint16_t version = tls_version + 0x0201;
3097 /* If either component overflowed, clamp it so comparisons still work. */
3098 if ((version >> 8) < (tls_version >> 8)) {
3099 version = 0xff00 | (version & 0xff);
3100 }
3101 if ((version & 0xff) < (tls_version & 0xff)) {
3102 version = (version & 0xff00) | 0xff;
3103 }
3104 /* DTLS 1.0 maps to TLS 1.1, not TLS 1.0. */
3105 if (version == TLS1_VERSION) {
3106 version = TLS1_1_VERSION;
3107 }
3108 return version;
3109}
3110
Adam Langleyfcf25832014-12-18 17:42:32 -08003111int SSL_cache_hit(SSL *s) { return s->hit; }
3112
3113int SSL_is_server(SSL *s) { return s->server; }
3114
Adam Langley524e7172015-02-20 16:04:00 -08003115void SSL_CTX_set_dos_protection_cb(
3116 SSL_CTX *ctx, int (*cb)(const struct ssl_early_callback_ctx *)) {
3117 ctx->dos_protection_cb = cb;
3118}
3119
Adam Langleyfcf25832014-12-18 17:42:32 -08003120void SSL_enable_fastradio_padding(SSL *s, char on_off) {
3121 s->fastradio_padding = on_off;
3122}
Adam Langley44972942015-02-20 13:43:23 -08003123
3124const SSL_CIPHER *SSL_get_cipher_by_value(uint16_t value) {
3125 return ssl3_get_cipher_by_value(value);
3126}
Adam Langley3f92d212015-02-20 15:32:52 -08003127
3128int SSL_get_rc4_state(const SSL *ssl, const RC4_KEY **read_key,
3129 const RC4_KEY **write_key) {
3130 if (ssl->aead_read_ctx == NULL || ssl->aead_write_ctx == NULL) {
3131 return 0;
3132 }
3133
3134 return EVP_AEAD_CTX_get_rc4_state(&ssl->aead_read_ctx->ctx, read_key) &&
3135 EVP_AEAD_CTX_get_rc4_state(&ssl->aead_write_ctx->ctx, write_key);
3136}