blob: d723d330b11e07703bec6fa696b502614602b6d1 [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) {
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +0100899 OPENSSL_PUT_ERROR(SSL, SSL_accept, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800900 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:
Adam Langleyfcf25832014-12-18 17:42:32 -08001147 case SSL_CTRL_SESS_CONNECT_GOOD:
Adam Langleyfcf25832014-12-18 17:42:32 -08001148 case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001149 case SSL_CTRL_SESS_ACCEPT:
Adam Langleyfcf25832014-12-18 17:42:32 -08001150 case SSL_CTRL_SESS_ACCEPT_GOOD:
Adam Langleyfcf25832014-12-18 17:42:32 -08001151 case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001152 case SSL_CTRL_SESS_HIT:
Adam Langleyfcf25832014-12-18 17:42:32 -08001153 case SSL_CTRL_SESS_CB_HIT:
Adam Langleyfcf25832014-12-18 17:42:32 -08001154 case SSL_CTRL_SESS_MISSES:
Adam Langleyfcf25832014-12-18 17:42:32 -08001155 case SSL_CTRL_SESS_TIMEOUTS:
Adam Langleyfcf25832014-12-18 17:42:32 -08001156 case SSL_CTRL_SESS_CACHE_FULL:
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001157 /* Statistics are no longer supported.
1158 * TODO(davidben): Try to remove the accessors altogether. */
1159 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001160
1161 case SSL_CTRL_OPTIONS:
1162 return ctx->options |= larg;
1163
1164 case SSL_CTRL_CLEAR_OPTIONS:
1165 return ctx->options &= ~larg;
1166
1167 case SSL_CTRL_MODE:
1168 return ctx->mode |= larg;
1169
1170 case SSL_CTRL_CLEAR_MODE:
1171 return ctx->mode &= ~larg;
1172
1173 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1174 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH) {
1175 return 0;
1176 }
1177 ctx->max_send_fragment = larg;
1178 return 1;
1179
1180 case SSL_CTRL_CERT_FLAGS:
1181 return ctx->cert->cert_flags |= larg;
1182
1183 case SSL_CTRL_CLEAR_CERT_FLAGS:
1184 return ctx->cert->cert_flags &= ~larg;
1185
1186 default:
1187 return ctx->method->ssl_ctx_ctrl(ctx, cmd, larg, parg);
1188 }
1189}
1190
1191long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void)) {
1192 switch (cmd) {
1193 case SSL_CTRL_SET_MSG_CALLBACK:
1194 ctx->msg_callback =
1195 (void (*)(int write_p, int version, int content_type, const void *buf,
1196 size_t len, SSL *ssl, void *arg))(fp);
1197 return 1;
1198
1199 default:
1200 return ctx->method->ssl_ctx_callback_ctrl(ctx, cmd, fp);
1201 }
1202}
1203
1204int ssl_cipher_id_cmp(const void *in_a, const void *in_b) {
1205 long l;
1206 const SSL_CIPHER *a = in_a;
1207 const SSL_CIPHER *b = in_b;
1208 const long a_id = a->id;
1209 const long b_id = b->id;
1210
1211 l = a_id - b_id;
1212 if (l == 0L) {
1213 return 0;
1214 } else {
1215 return (l > 0) ? 1 : -1;
1216 }
1217}
1218
1219int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **ap, const SSL_CIPHER **bp) {
1220 long l;
1221 const long a_id = (*ap)->id;
1222 const long b_id = (*bp)->id;
1223
1224 l = a_id - b_id;
1225 if (l == 0) {
1226 return 0;
1227 } else {
1228 return (l > 0) ? 1 : -1;
1229 }
1230}
1231
1232/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001233 * preference */
Adam Langleyfcf25832014-12-18 17:42:32 -08001234STACK_OF(SSL_CIPHER) * SSL_get_ciphers(const SSL *s) {
1235 if (s == NULL) {
1236 return NULL;
1237 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001238
Adam Langleyfcf25832014-12-18 17:42:32 -08001239 if (s->cipher_list != NULL) {
1240 return s->cipher_list->ciphers;
1241 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001242
Adam Langleyfcf25832014-12-18 17:42:32 -08001243 if (s->version >= TLS1_1_VERSION && s->ctx != NULL &&
1244 s->ctx->cipher_list_tls11 != NULL) {
1245 return s->ctx->cipher_list_tls11->ciphers;
1246 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001247
Adam Langleyfcf25832014-12-18 17:42:32 -08001248 if (s->ctx != NULL && s->ctx->cipher_list != NULL) {
1249 return s->ctx->cipher_list->ciphers;
1250 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001251
Adam Langleyfcf25832014-12-18 17:42:32 -08001252 return NULL;
1253}
Adam Langley95c29f32014-06-20 12:00:00 -07001254
Adam Langleyfcf25832014-12-18 17:42:32 -08001255/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001256 * algorithm id */
Adam Langleyfcf25832014-12-18 17:42:32 -08001257STACK_OF(SSL_CIPHER) * ssl_get_ciphers_by_id(SSL *s) {
1258 if (s == NULL) {
1259 return NULL;
1260 }
Adam Langley95c29f32014-06-20 12:00:00 -07001261
Adam Langleyfcf25832014-12-18 17:42:32 -08001262 if (s->cipher_list_by_id != NULL) {
1263 return s->cipher_list_by_id;
1264 }
Adam Langley95c29f32014-06-20 12:00:00 -07001265
Adam Langleyfcf25832014-12-18 17:42:32 -08001266 if (s->ctx != NULL && s->ctx->cipher_list_by_id != NULL) {
1267 return s->ctx->cipher_list_by_id;
1268 }
Adam Langley95c29f32014-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/* The old interface to get the same thing as SSL_get_ciphers() */
1274const char *SSL_get_cipher_list(const SSL *s, int n) {
1275 const SSL_CIPHER *c;
1276 STACK_OF(SSL_CIPHER) * sk;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001277
Adam Langleyfcf25832014-12-18 17:42:32 -08001278 if (s == NULL) {
1279 return NULL;
1280 }
Adam Langley95c29f32014-06-20 12:00:00 -07001281
Adam Langleyfcf25832014-12-18 17:42:32 -08001282 sk = SSL_get_ciphers(s);
1283 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
1284 return NULL;
1285 }
Adam Langley95c29f32014-06-20 12:00:00 -07001286
Adam Langleyfcf25832014-12-18 17:42:32 -08001287 c = sk_SSL_CIPHER_value(sk, n);
1288 if (c == NULL) {
1289 return NULL;
1290 }
Adam Langley95c29f32014-06-20 12:00:00 -07001291
Adam Langleyfcf25832014-12-18 17:42:32 -08001292 return c->name;
1293}
David Benjamin5491e3f2014-09-29 19:33:09 -04001294
Adam Langleyfcf25832014-12-18 17:42:32 -08001295/* specify the ciphers to be used by default by the SSL_CTX */
1296int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
1297 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001298
Adam Langleyfcf25832014-12-18 17:42:32 -08001299 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list,
1300 &ctx->cipher_list_by_id, str, ctx->cert);
1301 /* ssl_create_cipher_list may return an empty stack if it was unable to find
1302 * a cipher matching the given rule string (for example if the rule string
1303 * specifies a cipher which has been disabled). This is not an error as far
1304 * as ssl_create_cipher_list is concerned, and hence ctx->cipher_list and
1305 * ctx->cipher_list_by_id has been updated. */
1306 if (sk == NULL) {
1307 return 0;
1308 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1309 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1310 return 0;
1311 }
Adam Langley95c29f32014-06-20 12:00:00 -07001312
Adam Langleyfcf25832014-12-18 17:42:32 -08001313 return 1;
1314}
David Benjamin39482a12014-07-20 13:30:15 -04001315
Adam Langleyfcf25832014-12-18 17:42:32 -08001316int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str) {
1317 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001318
Adam Langleyfcf25832014-12-18 17:42:32 -08001319 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str,
1320 ctx->cert);
1321 if (sk == NULL) {
1322 return 0;
1323 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1324 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list_tls11,
1325 SSL_R_NO_CIPHER_MATCH);
1326 return 0;
1327 }
David Benjamin9f2c0d72014-10-21 22:00:19 -04001328
Adam Langleyfcf25832014-12-18 17:42:32 -08001329 return 1;
1330}
Adam Langley95c29f32014-06-20 12:00:00 -07001331
Adam Langleyfcf25832014-12-18 17:42:32 -08001332/* specify the ciphers to be used by the SSL */
1333int SSL_set_cipher_list(SSL *s, const char *str) {
1334 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001335
Adam Langleyfcf25832014-12-18 17:42:32 -08001336 sk = ssl_create_cipher_list(s->ctx->method, &s->cipher_list,
1337 &s->cipher_list_by_id, str, s->cert);
David Benjamin39482a12014-07-20 13:30:15 -04001338
Adam Langleyfcf25832014-12-18 17:42:32 -08001339 /* see comment in SSL_CTX_set_cipher_list */
1340 if (sk == NULL) {
1341 return 0;
1342 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1343 OPENSSL_PUT_ERROR(SSL, SSL_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1344 return 0;
1345 }
David Benjamin39482a12014-07-20 13:30:15 -04001346
Adam Langleyfcf25832014-12-18 17:42:32 -08001347 return 1;
1348}
Adam Langley95c29f32014-06-20 12:00:00 -07001349
Adam Langleyfcf25832014-12-18 17:42:32 -08001350int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, uint8_t *p) {
1351 size_t i;
1352 const SSL_CIPHER *c;
1353 CERT *ct = s->cert;
1354 uint8_t *q;
1355 /* Set disabled masks for this session */
1356 ssl_set_client_disabled(s);
Adam Langley29707792014-06-20 12:00:00 -07001357
Adam Langleyfcf25832014-12-18 17:42:32 -08001358 if (sk == NULL) {
1359 return 0;
1360 }
1361 q = p;
Adam Langley95c29f32014-06-20 12:00:00 -07001362
Adam Langleyfcf25832014-12-18 17:42:32 -08001363 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
1364 c = sk_SSL_CIPHER_value(sk, i);
1365 /* Skip disabled ciphers */
1366 if (c->algorithm_ssl & ct->mask_ssl ||
1367 c->algorithm_mkey & ct->mask_k ||
1368 c->algorithm_auth & ct->mask_a) {
1369 continue;
1370 }
1371 s2n(ssl3_get_cipher_value(c), p);
1372 }
1373
1374 /* If all ciphers were disabled, return the error to the caller. */
1375 if (p == q) {
1376 return 0;
1377 }
1378
1379 /* Add SCSVs. */
1380 if (!s->renegotiate) {
1381 s2n(SSL3_CK_SCSV & 0xffff, p);
1382 }
1383
Adam Langley5f0efe02015-02-20 13:03:16 -08001384 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001385 s2n(SSL3_CK_FALLBACK_SCSV & 0xffff, p);
1386 }
1387
1388 return p - q;
1389}
1390
1391STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs) {
1392 CBS cipher_suites = *cbs;
1393 const SSL_CIPHER *c;
1394 STACK_OF(SSL_CIPHER) * sk;
1395
1396 if (s->s3) {
1397 s->s3->send_connection_binding = 0;
1398 }
1399
1400 if (CBS_len(&cipher_suites) % 2 != 0) {
1401 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1402 SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
1403 return NULL;
1404 }
1405
1406 sk = sk_SSL_CIPHER_new_null();
1407 if (sk == NULL) {
1408 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1409 goto err;
1410 }
1411
1412 if (!CBS_stow(&cipher_suites, &s->cert->ciphers_raw,
1413 &s->cert->ciphers_rawlen)) {
1414 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1415 goto err;
1416 }
1417
1418 while (CBS_len(&cipher_suites) > 0) {
1419 uint16_t cipher_suite;
1420
1421 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
1422 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_INTERNAL_ERROR);
1423 goto err;
1424 }
1425
1426 /* Check for SCSV. */
1427 if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff)) {
1428 /* SCSV is fatal if renegotiating. */
1429 if (s->renegotiate) {
1430 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1431 SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1432 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1433 goto err;
1434 }
1435 s->s3->send_connection_binding = 1;
1436 continue;
1437 }
1438
1439 /* Check for FALLBACK_SCSV. */
1440 if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff)) {
1441 uint16_t max_version = ssl3_get_max_server_version(s);
1442 if (SSL_IS_DTLS(s) ? (uint16_t)s->version > max_version
1443 : (uint16_t)s->version < max_version) {
1444 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1445 SSL_R_INAPPROPRIATE_FALLBACK);
1446 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
1447 goto err;
1448 }
1449 continue;
1450 }
1451
1452 c = ssl3_get_cipher_by_value(cipher_suite);
1453 if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
1454 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1455 goto err;
1456 }
1457 }
1458
1459 return sk;
David Benjamin9f2c0d72014-10-21 22:00:19 -04001460
Adam Langley95c29f32014-06-20 12:00:00 -07001461err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001462 if (sk != NULL) {
1463 sk_SSL_CIPHER_free(sk);
1464 }
1465 return NULL;
1466}
Adam Langley95c29f32014-06-20 12:00:00 -07001467
1468
Adam Langleyfcf25832014-12-18 17:42:32 -08001469/* return a servername extension value if provided in Client Hello, or NULL. So
1470 * far, only host_name types are defined (RFC 3546). */
1471const char *SSL_get_servername(const SSL *s, const int type) {
1472 if (type != TLSEXT_NAMETYPE_host_name) {
1473 return NULL;
1474 }
Adam Langley95c29f32014-06-20 12:00:00 -07001475
Adam Langleyfcf25832014-12-18 17:42:32 -08001476 return s->session && !s->tlsext_hostname ? s->session->tlsext_hostname
1477 : s->tlsext_hostname;
1478}
Adam Langley95c29f32014-06-20 12:00:00 -07001479
Adam Langleyfcf25832014-12-18 17:42:32 -08001480int SSL_get_servername_type(const SSL *s) {
1481 if (s->session &&
1482 (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname)) {
1483 return TLSEXT_NAMETYPE_host_name;
1484 }
Adam Langley95c29f32014-06-20 12:00:00 -07001485
Adam Langleyfcf25832014-12-18 17:42:32 -08001486 return -1;
1487}
Adam Langley95c29f32014-06-20 12:00:00 -07001488
Adam Langleyfcf25832014-12-18 17:42:32 -08001489void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
1490 ctx->signed_cert_timestamps_enabled = 1;
1491}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001492
Adam Langleyfcf25832014-12-18 17:42:32 -08001493int SSL_enable_signed_cert_timestamps(SSL *ssl) {
1494 ssl->signed_cert_timestamps_enabled = 1;
1495 return 1;
1496}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001497
Adam Langleyfcf25832014-12-18 17:42:32 -08001498void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
1499 ctx->ocsp_stapling_enabled = 1;
1500}
David Benjamin6c7aed02014-08-27 16:42:38 -04001501
Adam Langleyfcf25832014-12-18 17:42:32 -08001502int SSL_enable_ocsp_stapling(SSL *ssl) {
1503 ssl->ocsp_stapling_enabled = 1;
1504 return 1;
1505}
David Benjamin6c7aed02014-08-27 16:42:38 -04001506
Adam Langleyfcf25832014-12-18 17:42:32 -08001507void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
1508 size_t *out_len) {
1509 SSL_SESSION *session = ssl->session;
Adam Langley3cb50e02014-08-26 14:00:31 -07001510
Adam Langleyfcf25832014-12-18 17:42:32 -08001511 *out_len = 0;
1512 *out = NULL;
1513 if (ssl->server || !session || !session->tlsext_signed_cert_timestamp_list) {
1514 return;
1515 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001516
Adam Langleyfcf25832014-12-18 17:42:32 -08001517 *out = session->tlsext_signed_cert_timestamp_list;
1518 *out_len = session->tlsext_signed_cert_timestamp_list_length;
1519}
David Benjamin6c7aed02014-08-27 16:42:38 -04001520
Adam Langleyfcf25832014-12-18 17:42:32 -08001521void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
1522 size_t *out_len) {
1523 SSL_SESSION *session = ssl->session;
1524
1525 *out_len = 0;
1526 *out = NULL;
1527 if (ssl->server || !session || !session->ocsp_response) {
1528 return;
1529 }
1530 *out = session->ocsp_response;
1531 *out_len = session->ocsp_response_length;
1532}
David Benjamin6c7aed02014-08-27 16:42:38 -04001533
Adam Langley95c29f32014-06-20 12:00:00 -07001534/* SSL_select_next_proto implements the standard protocol selection. It is
1535 * expected that this function is called from the callback set by
1536 * SSL_CTX_set_next_proto_select_cb.
1537 *
1538 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1539 * strings. The length byte itself is not included in the length. A byte
1540 * string of length 0 is invalid. No byte string may be truncated.
1541 *
1542 * The current, but experimental algorithm for selecting the protocol is:
1543 *
1544 * 1) If the server doesn't support NPN then this is indicated to the
1545 * callback. In this case, the client application has to abort the connection
1546 * or have a default application level protocol.
1547 *
1548 * 2) If the server supports NPN, but advertises an empty list then the
1549 * client selects the first protcol in its list, but indicates via the
1550 * API that this fallback case was enacted.
1551 *
1552 * 3) Otherwise, the client finds the first protocol in the server's list
1553 * that it supports and selects this protocol. This is because it's
1554 * assumed that the server has better information about which protocol
1555 * a client should use.
1556 *
1557 * 4) If the client doesn't support any of the server's advertised
1558 * protocols, then this is treated the same as case 2.
1559 *
1560 * It returns either
1561 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1562 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1563 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001564int SSL_select_next_proto(uint8_t **out, uint8_t *outlen, const uint8_t *server,
1565 unsigned int server_len, const uint8_t *client,
1566 unsigned int client_len) {
1567 unsigned int i, j;
1568 const uint8_t *result;
1569 int status = OPENSSL_NPN_UNSUPPORTED;
Adam Langley95c29f32014-06-20 12:00:00 -07001570
Adam Langleyfcf25832014-12-18 17:42:32 -08001571 /* For each protocol in server preference order, see if we support it. */
1572 for (i = 0; i < server_len;) {
1573 for (j = 0; j < client_len;) {
1574 if (server[i] == client[j] &&
1575 memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
1576 /* We found a match */
1577 result = &server[i];
1578 status = OPENSSL_NPN_NEGOTIATED;
1579 goto found;
1580 }
1581 j += client[j];
1582 j++;
1583 }
1584 i += server[i];
1585 i++;
1586 }
Adam Langley95c29f32014-06-20 12:00:00 -07001587
Adam Langleyfcf25832014-12-18 17:42:32 -08001588 /* There's no overlap between our protocols and the server's list. */
1589 result = client;
1590 status = OPENSSL_NPN_NO_OVERLAP;
Adam Langley95c29f32014-06-20 12:00:00 -07001591
Adam Langleyfcf25832014-12-18 17:42:32 -08001592found:
1593 *out = (uint8_t *)result + 1;
1594 *outlen = result[0];
1595 return status;
1596}
Adam Langley95c29f32014-06-20 12:00:00 -07001597
Adam Langley95c29f32014-06-20 12:00:00 -07001598/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
1599 * requested protocol for this connection and returns 0. If the client didn't
1600 * request any protocol, then *data is set to NULL.
1601 *
1602 * Note that the client can request any protocol it chooses. The value returned
1603 * from this function need not be a member of the list of supported protocols
Adam Langleyfcf25832014-12-18 17:42:32 -08001604 * provided by the callback. */
1605void SSL_get0_next_proto_negotiated(const SSL *s, const uint8_t **data,
1606 unsigned *len) {
1607 *data = s->next_proto_negotiated;
1608 if (!*data) {
1609 *len = 0;
1610 } else {
1611 *len = s->next_proto_negotiated_len;
1612 }
Adam Langley95c29f32014-06-20 12:00:00 -07001613}
1614
1615/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
1616 * TLS server needs a list of supported protocols for Next Protocol
1617 * Negotiation. The returned list must be in wire format. The list is returned
1618 * by setting |out| to point to it and |outlen| to its length. This memory will
1619 * not be modified, but one should assume that the SSL* keeps a reference to
1620 * it.
1621 *
Adam Langleyfcf25832014-12-18 17:42:32 -08001622 * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise.
1623 * Otherwise, no such extension will be included in the ServerHello. */
1624void SSL_CTX_set_next_protos_advertised_cb(
1625 SSL_CTX *ctx,
1626 int (*cb)(SSL *ssl, const uint8_t **out, unsigned int *outlen, void *arg),
1627 void *arg) {
1628 ctx->next_protos_advertised_cb = cb;
1629 ctx->next_protos_advertised_cb_arg = arg;
1630}
Adam Langley95c29f32014-06-20 12:00:00 -07001631
1632/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1633 * client needs to select a protocol from the server's provided list. |out|
1634 * must be set to point to the selected protocol (which may be within |in|).
1635 * The length of the protocol name must be written into |outlen|. The server's
1636 * advertised protocols are provided in |in| and |inlen|. The callback can
1637 * assume that |in| is syntactically valid.
1638 *
1639 * The client must select a protocol. It is fatal to the connection if this
1640 * callback returns a value other than SSL_TLSEXT_ERR_OK.
1641 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001642void SSL_CTX_set_next_proto_select_cb(
1643 SSL_CTX *ctx, int (*cb)(SSL *s, uint8_t **out, uint8_t *outlen,
1644 const uint8_t *in, unsigned int inlen, void *arg),
1645 void *arg) {
1646 ctx->next_proto_select_cb = cb;
1647 ctx->next_proto_select_cb_arg = arg;
1648}
Adam Langley95c29f32014-06-20 12:00:00 -07001649
Adam Langleyfcf25832014-12-18 17:42:32 -08001650int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
1651 unsigned protos_len) {
1652 if (ctx->alpn_client_proto_list) {
1653 OPENSSL_free(ctx->alpn_client_proto_list);
1654 }
Adam Langley95c29f32014-06-20 12:00:00 -07001655
Adam Langleyfcf25832014-12-18 17:42:32 -08001656 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1657 if (!ctx->alpn_client_proto_list) {
1658 return 1;
1659 }
1660 ctx->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001661
Adam Langleyfcf25832014-12-18 17:42:32 -08001662 return 0;
1663}
Adam Langley95c29f32014-06-20 12:00:00 -07001664
Adam Langleyfcf25832014-12-18 17:42:32 -08001665int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
1666 if (ssl->alpn_client_proto_list) {
1667 OPENSSL_free(ssl->alpn_client_proto_list);
1668 }
Adam Langley95c29f32014-06-20 12:00:00 -07001669
Adam Langleyfcf25832014-12-18 17:42:32 -08001670 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1671 if (!ssl->alpn_client_proto_list) {
1672 return 1;
1673 }
1674 ssl->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001675
Adam Langleyfcf25832014-12-18 17:42:32 -08001676 return 0;
1677}
Adam Langley95c29f32014-06-20 12:00:00 -07001678
1679/* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
1680 * during ClientHello processing in order to select an ALPN protocol from the
1681 * client's list of offered protocols. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001682void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
1683 int (*cb)(SSL *ssl, const uint8_t **out,
1684 uint8_t *outlen, const uint8_t *in,
1685 unsigned int inlen, void *arg),
1686 void *arg) {
1687 ctx->alpn_select_cb = cb;
1688 ctx->alpn_select_cb_arg = arg;
1689}
Adam Langley95c29f32014-06-20 12:00:00 -07001690
1691/* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
1692 * On return it sets |*data| to point to |*len| bytes of protocol name (not
1693 * including the leading length-prefix byte). If the server didn't respond with
1694 * a negotiated protocol then |*len| will be zero. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001695void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **data,
1696 unsigned *len) {
1697 *data = NULL;
1698 if (ssl->s3) {
1699 *data = ssl->s3->alpn_selected;
1700 }
1701 if (*data == NULL) {
1702 *len = 0;
1703 } else {
1704 *len = ssl->s3->alpn_selected_len;
1705 }
1706}
Adam Langley95c29f32014-06-20 12:00:00 -07001707
Adam Langleyfcf25832014-12-18 17:42:32 -08001708int SSL_export_keying_material(SSL *s, uint8_t *out, size_t olen,
1709 const char *label, size_t llen, const uint8_t *p,
1710 size_t plen, int use_context) {
1711 if (s->version < TLS1_VERSION) {
1712 return -1;
1713 }
Adam Langley95c29f32014-06-20 12:00:00 -07001714
Adam Langleyfcf25832014-12-18 17:42:32 -08001715 return s->enc_method->export_keying_material(s, out, olen, label, llen, p,
1716 plen, use_context);
1717}
Adam Langley95c29f32014-06-20 12:00:00 -07001718
Adam Langleyfcf25832014-12-18 17:42:32 -08001719static uint32_t ssl_session_hash(const SSL_SESSION *a) {
1720 uint32_t hash =
1721 ((uint32_t)a->session_id[0]) ||
1722 ((uint32_t)a->session_id[1] << 8) ||
1723 ((uint32_t)a->session_id[2] << 16) ||
1724 ((uint32_t)a->session_id[3] << 24);
Adam Langley95c29f32014-06-20 12:00:00 -07001725
Adam Langleyfcf25832014-12-18 17:42:32 -08001726 return hash;
1727}
Adam Langley95c29f32014-06-20 12:00:00 -07001728
1729/* NB: If this function (or indeed the hash function which uses a sort of
1730 * coarser function than this one) is changed, ensure
1731 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1732 * able to construct an SSL_SESSION that will collide with any existing session
1733 * with a matching session ID. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001734static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
1735 if (a->ssl_version != b->ssl_version) {
1736 return 1;
1737 }
Adam Langley95c29f32014-06-20 12:00:00 -07001738
Adam Langleyfcf25832014-12-18 17:42:32 -08001739 if (a->session_id_length != b->session_id_length) {
1740 return 1;
1741 }
Adam Langley95c29f32014-06-20 12:00:00 -07001742
Adam Langleyfcf25832014-12-18 17:42:32 -08001743 return memcmp(a->session_id, b->session_id, a->session_id_length);
1744}
Adam Langley95c29f32014-06-20 12:00:00 -07001745
Adam Langleyfcf25832014-12-18 17:42:32 -08001746SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth) {
1747 SSL_CTX *ret = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001748
Adam Langleyfcf25832014-12-18 17:42:32 -08001749 if (meth == NULL) {
1750 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_NULL_SSL_METHOD_PASSED);
1751 return NULL;
1752 }
Adam Langley95c29f32014-06-20 12:00:00 -07001753
Adam Langleyfcf25832014-12-18 17:42:32 -08001754 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
1755 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
1756 goto err;
1757 }
Adam Langley95c29f32014-06-20 12:00:00 -07001758
Adam Langleyfcf25832014-12-18 17:42:32 -08001759 ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1760 if (ret == NULL) {
1761 goto err;
1762 }
Adam Langley95c29f32014-06-20 12:00:00 -07001763
Adam Langleyfcf25832014-12-18 17:42:32 -08001764 memset(ret, 0, sizeof(SSL_CTX));
Adam Langley95c29f32014-06-20 12:00:00 -07001765
Adam Langleyfcf25832014-12-18 17:42:32 -08001766 ret->method = meth->method;
Adam Langley95c29f32014-06-20 12:00:00 -07001767
Adam Langleyfcf25832014-12-18 17:42:32 -08001768 ret->cert_store = NULL;
1769 ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
1770 ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1771 ret->session_cache_head = NULL;
1772 ret->session_cache_tail = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001773
Adam Langleyfcf25832014-12-18 17:42:32 -08001774 /* We take the system default */
1775 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001776
Adam Langleyfcf25832014-12-18 17:42:32 -08001777 ret->new_session_cb = 0;
1778 ret->remove_session_cb = 0;
1779 ret->get_session_cb = 0;
1780 ret->generate_session_id = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001781
Adam Langleyfcf25832014-12-18 17:42:32 -08001782 ret->references = 1;
1783 ret->quiet_shutdown = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001784
Adam Langleyfcf25832014-12-18 17:42:32 -08001785 ret->info_callback = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001786
Adam Langleyfcf25832014-12-18 17:42:32 -08001787 ret->app_verify_callback = 0;
1788 ret->app_verify_arg = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001789
Adam Langleyfcf25832014-12-18 17:42:32 -08001790 ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
1791 ret->read_ahead = 0;
1792 ret->msg_callback = 0;
1793 ret->msg_callback_arg = NULL;
1794 ret->verify_mode = SSL_VERIFY_NONE;
1795 ret->sid_ctx_length = 0;
1796 ret->default_verify_callback = NULL;
1797 ret->cert = ssl_cert_new();
1798 if (ret->cert == NULL) {
1799 goto err;
1800 }
Adam Langley95c29f32014-06-20 12:00:00 -07001801
Adam Langleyfcf25832014-12-18 17:42:32 -08001802 ret->default_passwd_callback = 0;
1803 ret->default_passwd_callback_userdata = NULL;
1804 ret->client_cert_cb = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001805
Adam Langleyfcf25832014-12-18 17:42:32 -08001806 ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
1807 if (ret->sessions == NULL) {
1808 goto err;
1809 }
1810 ret->cert_store = X509_STORE_new();
1811 if (ret->cert_store == NULL) {
1812 goto err;
1813 }
Adam Langley95c29f32014-06-20 12:00:00 -07001814
Adam Langleyfcf25832014-12-18 17:42:32 -08001815 ssl_create_cipher_list(ret->method, &ret->cipher_list,
1816 &ret->cipher_list_by_id, SSL_DEFAULT_CIPHER_LIST,
1817 ret->cert);
1818 if (ret->cipher_list == NULL ||
1819 sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0) {
1820 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_LIBRARY_HAS_NO_CIPHERS);
1821 goto err2;
1822 }
Adam Langley95c29f32014-06-20 12:00:00 -07001823
Adam Langleyfcf25832014-12-18 17:42:32 -08001824 ret->param = X509_VERIFY_PARAM_new();
1825 if (!ret->param) {
1826 goto err;
1827 }
Adam Langley95c29f32014-06-20 12:00:00 -07001828
Adam Langleyfcf25832014-12-18 17:42:32 -08001829 ret->client_CA = sk_X509_NAME_new_null();
1830 if (ret->client_CA == NULL) {
1831 goto err;
1832 }
Adam Langley95c29f32014-06-20 12:00:00 -07001833
Adam Langleyfcf25832014-12-18 17:42:32 -08001834 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001835
Adam Langleyfcf25832014-12-18 17:42:32 -08001836 ret->extra_certs = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001837
Adam Langleyfcf25832014-12-18 17:42:32 -08001838 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
David Benjamin422d3a42014-08-20 11:09:03 -04001839
Adam Langleyfcf25832014-12-18 17:42:32 -08001840 ret->tlsext_servername_callback = 0;
1841 ret->tlsext_servername_arg = NULL;
1842 /* Setup RFC4507 ticket keys */
1843 if (!RAND_bytes(ret->tlsext_tick_key_name, 16) ||
1844 !RAND_bytes(ret->tlsext_tick_hmac_key, 16) ||
1845 !RAND_bytes(ret->tlsext_tick_aes_key, 16)) {
1846 ret->options |= SSL_OP_NO_TICKET;
1847 }
Adam Langley95c29f32014-06-20 12:00:00 -07001848
Adam Langleyfcf25832014-12-18 17:42:32 -08001849 ret->tlsext_status_cb = 0;
1850 ret->tlsext_status_arg = NULL;
David Benjamin82c9e902014-12-12 15:55:27 -05001851
Adam Langleyfcf25832014-12-18 17:42:32 -08001852 ret->next_protos_advertised_cb = 0;
1853 ret->next_proto_select_cb = 0;
1854 ret->psk_identity_hint = NULL;
1855 ret->psk_client_callback = NULL;
1856 ret->psk_server_callback = NULL;
1857
1858 /* Default is to connect to non-RI servers. When RI is more widely deployed
1859 * might change this. */
1860 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
1861
1862 /* Lock the SSL_CTX to the specified version, for compatibility with legacy
1863 * uses of SSL_METHOD. */
1864 if (meth->version != 0) {
1865 SSL_CTX_set_max_version(ret, meth->version);
1866 SSL_CTX_set_min_version(ret, meth->version);
1867 }
1868
1869 return ret;
1870
Adam Langley95c29f32014-06-20 12:00:00 -07001871err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001872 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -07001873err2:
Adam Langleyfcf25832014-12-18 17:42:32 -08001874 if (ret != NULL) {
1875 SSL_CTX_free(ret);
1876 }
1877 return NULL;
1878}
Adam Langley95c29f32014-06-20 12:00:00 -07001879
Adam Langleyfcf25832014-12-18 17:42:32 -08001880void SSL_CTX_free(SSL_CTX *a) {
1881 int i;
Adam Langley95c29f32014-06-20 12:00:00 -07001882
Adam Langleyfcf25832014-12-18 17:42:32 -08001883 if (a == NULL) {
1884 return;
1885 }
Adam Langley95c29f32014-06-20 12:00:00 -07001886
Adam Langleyfcf25832014-12-18 17:42:32 -08001887 i = CRYPTO_add(&a->references, -1, CRYPTO_LOCK_SSL_CTX);
1888 if (i > 0) {
1889 return;
1890 }
Adam Langley95c29f32014-06-20 12:00:00 -07001891
Adam Langleyfcf25832014-12-18 17:42:32 -08001892 if (a->param) {
1893 X509_VERIFY_PARAM_free(a->param);
1894 }
Adam Langley95c29f32014-06-20 12:00:00 -07001895
Adam Langleyfcf25832014-12-18 17:42:32 -08001896 /* Free internal session cache. However: the remove_cb() may reference the
1897 * ex_data of SSL_CTX, thus the ex_data store can only be removed after the
1898 * sessions were flushed. As the ex_data handling routines might also touch
1899 * the session cache, the most secure solution seems to be: empty (flush) the
1900 * cache, then free ex_data, then finally free the cache. (See ticket
1901 * [openssl.org #212].) */
1902 if (a->sessions != NULL) {
1903 SSL_CTX_flush_sessions(a, 0);
1904 }
Adam Langley95c29f32014-06-20 12:00:00 -07001905
Adam Langleyfcf25832014-12-18 17:42:32 -08001906 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001907
Adam Langleyfcf25832014-12-18 17:42:32 -08001908 if (a->sessions != NULL) {
1909 lh_SSL_SESSION_free(a->sessions);
1910 }
1911 if (a->cert_store != NULL) {
1912 X509_STORE_free(a->cert_store);
1913 }
1914 if (a->cipher_list != NULL) {
1915 ssl_cipher_preference_list_free(a->cipher_list);
1916 }
1917 if (a->cipher_list_by_id != NULL) {
1918 sk_SSL_CIPHER_free(a->cipher_list_by_id);
1919 }
1920 if (a->cipher_list_tls11 != NULL) {
1921 ssl_cipher_preference_list_free(a->cipher_list_tls11);
1922 }
1923 if (a->cert != NULL) {
1924 ssl_cert_free(a->cert);
1925 }
1926 if (a->client_CA != NULL) {
1927 sk_X509_NAME_pop_free(a->client_CA, X509_NAME_free);
1928 }
1929 if (a->extra_certs != NULL) {
1930 sk_X509_pop_free(a->extra_certs, X509_free);
1931 }
1932 if (a->srtp_profiles) {
1933 sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);
1934 }
1935 if (a->psk_identity_hint) {
1936 OPENSSL_free(a->psk_identity_hint);
1937 }
1938 if (a->tlsext_ecpointformatlist) {
1939 OPENSSL_free(a->tlsext_ecpointformatlist);
1940 }
1941 if (a->tlsext_ellipticcurvelist) {
1942 OPENSSL_free(a->tlsext_ellipticcurvelist);
1943 }
1944 if (a->alpn_client_proto_list != NULL) {
1945 OPENSSL_free(a->alpn_client_proto_list);
1946 }
1947 if (a->tlsext_channel_id_private) {
1948 EVP_PKEY_free(a->tlsext_channel_id_private);
1949 }
1950 if (a->keylog_bio) {
1951 BIO_free(a->keylog_bio);
1952 }
Adam Langley95c29f32014-06-20 12:00:00 -07001953
Adam Langleyfcf25832014-12-18 17:42:32 -08001954 OPENSSL_free(a);
1955}
Adam Langley95c29f32014-06-20 12:00:00 -07001956
Adam Langleyfcf25832014-12-18 17:42:32 -08001957void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) {
1958 ctx->default_passwd_callback = cb;
1959}
Adam Langley95c29f32014-06-20 12:00:00 -07001960
Adam Langleyfcf25832014-12-18 17:42:32 -08001961void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u) {
1962 ctx->default_passwd_callback_userdata = u;
1963}
Adam Langley95c29f32014-06-20 12:00:00 -07001964
Adam Langleyfcf25832014-12-18 17:42:32 -08001965void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
1966 int (*cb)(X509_STORE_CTX *, void *),
1967 void *arg) {
1968 ctx->app_verify_callback = cb;
1969 ctx->app_verify_arg = arg;
1970}
Adam Langley95c29f32014-06-20 12:00:00 -07001971
Adam Langleyfcf25832014-12-18 17:42:32 -08001972void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
1973 int (*cb)(int, X509_STORE_CTX *)) {
1974 ctx->verify_mode = mode;
1975 ctx->default_verify_callback = cb;
1976}
Adam Langley95c29f32014-06-20 12:00:00 -07001977
Adam Langleyfcf25832014-12-18 17:42:32 -08001978void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
1979 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
1980}
Adam Langley1258b6a2014-06-20 12:00:00 -07001981
Adam Langleyfcf25832014-12-18 17:42:32 -08001982void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb)(SSL *ssl, void *arg),
1983 void *arg) {
1984 ssl_cert_set_cert_cb(c->cert, cb, arg);
1985}
David Benjamin859ec3c2014-09-02 16:29:36 -04001986
Adam Langleyfcf25832014-12-18 17:42:32 -08001987void SSL_set_cert_cb(SSL *s, int (*cb)(SSL *ssl, void *arg), void *arg) {
1988 ssl_cert_set_cert_cb(s->cert, cb, arg);
1989}
Adam Langley95c29f32014-06-20 12:00:00 -07001990
Adam Langleyfcf25832014-12-18 17:42:32 -08001991static int ssl_has_key(SSL *s, size_t idx) {
1992 CERT_PKEY *cpk = &s->cert->pkeys[idx];
1993 return cpk->x509 && cpk->privatekey;
1994}
David Benjamin033e5f42014-11-13 18:47:41 -05001995
David Benjaminf31e6812014-11-13 18:05:55 -05001996void ssl_get_compatible_server_ciphers(SSL *s, unsigned long *out_mask_k,
Adam Langleyfcf25832014-12-18 17:42:32 -08001997 unsigned long *out_mask_a) {
1998 CERT *c = s->cert;
1999 int rsa_enc, rsa_sign, dh_tmp;
2000 unsigned long mask_k, mask_a;
2001 int have_ecc_cert, ecdsa_ok;
2002 int have_ecdh_tmp;
2003 X509 *x;
Adam Langley95c29f32014-06-20 12:00:00 -07002004
Adam Langleyfcf25832014-12-18 17:42:32 -08002005 if (c == NULL) {
2006 /* TODO(davidben): Is this codepath possible? */
2007 *out_mask_k = 0;
2008 *out_mask_a = 0;
2009 return;
2010 }
Adam Langley95c29f32014-06-20 12:00:00 -07002011
Adam Langleyfcf25832014-12-18 17:42:32 -08002012 dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07002013
Adam Langleyfcf25832014-12-18 17:42:32 -08002014 have_ecdh_tmp = (c->ecdh_tmp || c->ecdh_tmp_cb || c->ecdh_tmp_auto);
2015 rsa_enc = ssl_has_key(s, SSL_PKEY_RSA_ENC);
2016 rsa_sign = ssl_has_key(s, SSL_PKEY_RSA_SIGN);
2017 have_ecc_cert = ssl_has_key(s, SSL_PKEY_ECC);
2018 mask_k = 0;
2019 mask_a = 0;
David Benjaminf31e6812014-11-13 18:05:55 -05002020
Adam Langleyfcf25832014-12-18 17:42:32 -08002021 if (rsa_enc) {
2022 mask_k |= SSL_kRSA;
2023 }
2024 if (dh_tmp) {
David Benjamin7061e282015-03-19 11:10:48 -04002025 mask_k |= SSL_kDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08002026 }
2027 if (rsa_enc || rsa_sign) {
2028 mask_a |= SSL_aRSA;
2029 }
Adam Langley95c29f32014-06-20 12:00:00 -07002030
Adam Langleyfcf25832014-12-18 17:42:32 -08002031 mask_a |= SSL_aNULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002032
Adam Langleyfcf25832014-12-18 17:42:32 -08002033 /* An ECC certificate may be usable for ECDSA cipher suites depending on the
2034 * key usage extension and on the client's curve preferences. */
2035 if (have_ecc_cert) {
2036 x = c->pkeys[SSL_PKEY_ECC].x509;
2037 /* This call populates extension flags (ex_flags). */
2038 X509_check_purpose(x, -1, 0);
2039 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE)
2040 ? (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)
2041 : 1;
2042 if (!tls1_check_ec_cert(s, x)) {
2043 ecdsa_ok = 0;
2044 }
2045 if (ecdsa_ok) {
2046 mask_a |= SSL_aECDSA;
2047 }
2048 }
Adam Langley95c29f32014-06-20 12:00:00 -07002049
Adam Langleyfcf25832014-12-18 17:42:32 -08002050 /* If we are considering an ECC cipher suite that uses an ephemeral EC
2051 * key, check it. */
2052 if (have_ecdh_tmp && tls1_check_ec_tmp_key(s)) {
David Benjamin7061e282015-03-19 11:10:48 -04002053 mask_k |= SSL_kECDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08002054 }
Adam Langley95c29f32014-06-20 12:00:00 -07002055
Adam Langleyfcf25832014-12-18 17:42:32 -08002056 /* PSK requires a server callback. */
2057 if (s->psk_server_callback != NULL) {
2058 mask_k |= SSL_kPSK;
2059 mask_a |= SSL_aPSK;
2060 }
Adam Langley95c29f32014-06-20 12:00:00 -07002061
Adam Langleyfcf25832014-12-18 17:42:32 -08002062 *out_mask_k = mask_k;
2063 *out_mask_a = mask_a;
2064}
Adam Langley95c29f32014-06-20 12:00:00 -07002065
2066/* This handy macro borrowed from crypto/x509v3/v3_purp.c */
2067#define ku_reject(x, usage) \
Adam Langleyfcf25832014-12-18 17:42:32 -08002068 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
Adam Langley95c29f32014-06-20 12:00:00 -07002069
Adam Langleyfcf25832014-12-18 17:42:32 -08002070int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s) {
2071 unsigned long alg_a;
2072 int signature_nid = 0, md_nid = 0, pk_nid = 0;
2073 const SSL_CIPHER *cs = s->s3->tmp.new_cipher;
Adam Langley95c29f32014-06-20 12:00:00 -07002074
Adam Langleyfcf25832014-12-18 17:42:32 -08002075 alg_a = cs->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -07002076
Adam Langleyfcf25832014-12-18 17:42:32 -08002077 /* This call populates the ex_flags field correctly */
2078 X509_check_purpose(x, -1, 0);
2079 if (x->sig_alg && x->sig_alg->algorithm) {
2080 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
2081 OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
2082 }
2083 if (alg_a & SSL_aECDSA) {
2084 /* key usage, if present, must allow signing */
2085 if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE)) {
2086 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg,
2087 SSL_R_ECC_CERT_NOT_FOR_SIGNING);
2088 return 0;
2089 }
2090 }
Adam Langley95c29f32014-06-20 12:00:00 -07002091
Adam Langleyfcf25832014-12-18 17:42:32 -08002092 return 1; /* all checks are ok */
2093}
Adam Langley95c29f32014-06-20 12:00:00 -07002094
Adam Langleyfcf25832014-12-18 17:42:32 -08002095static int ssl_get_server_cert_index(const SSL *s) {
2096 int idx;
2097 idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
2098 if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509) {
2099 idx = SSL_PKEY_RSA_SIGN;
2100 }
2101 if (idx == -1) {
2102 OPENSSL_PUT_ERROR(SSL, ssl_get_server_cert_index, ERR_R_INTERNAL_ERROR);
2103 }
2104 return idx;
2105}
Adam Langley95c29f32014-06-20 12:00:00 -07002106
Adam Langleyfcf25832014-12-18 17:42:32 -08002107CERT_PKEY *ssl_get_server_send_pkey(const SSL *s) {
2108 int i = ssl_get_server_cert_index(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002109
Adam Langleyfcf25832014-12-18 17:42:32 -08002110 /* This may or may not be an error. */
2111 if (i < 0) {
2112 return NULL;
2113 }
Adam Langley95c29f32014-06-20 12:00:00 -07002114
Adam Langleyfcf25832014-12-18 17:42:32 -08002115 /* May be NULL. */
2116 return &s->cert->pkeys[i];
2117}
Adam Langley95c29f32014-06-20 12:00:00 -07002118
Adam Langleyfcf25832014-12-18 17:42:32 -08002119EVP_PKEY *ssl_get_sign_pkey(SSL *s, const SSL_CIPHER *cipher) {
2120 unsigned long alg_a;
2121 CERT *c;
2122 int idx = -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002123
Adam Langleyfcf25832014-12-18 17:42:32 -08002124 alg_a = cipher->algorithm_auth;
2125 c = s->cert;
Adam Langley95c29f32014-06-20 12:00:00 -07002126
Adam Langleyfcf25832014-12-18 17:42:32 -08002127 if (alg_a & SSL_aRSA) {
2128 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL) {
2129 idx = SSL_PKEY_RSA_SIGN;
2130 } else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL) {
2131 idx = SSL_PKEY_RSA_ENC;
2132 }
2133 } else if ((alg_a & SSL_aECDSA) &&
2134 (c->pkeys[SSL_PKEY_ECC].privatekey != NULL)) {
2135 idx = SSL_PKEY_ECC;
2136 }
Adam Langley95c29f32014-06-20 12:00:00 -07002137
Adam Langleyfcf25832014-12-18 17:42:32 -08002138 if (idx == -1) {
2139 OPENSSL_PUT_ERROR(SSL, ssl_get_sign_pkey, ERR_R_INTERNAL_ERROR);
2140 return NULL;
2141 }
Adam Langley95c29f32014-06-20 12:00:00 -07002142
Adam Langleyfcf25832014-12-18 17:42:32 -08002143 return c->pkeys[idx].privatekey;
2144}
Adam Langley95c29f32014-06-20 12:00:00 -07002145
Adam Langleyfcf25832014-12-18 17:42:32 -08002146void ssl_update_cache(SSL *s, int mode) {
David Benjaminb6d0c6d2015-03-19 19:07:26 -04002147 /* Never cache sessions with empty session IDs. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002148 if (s->session->session_id_length == 0) {
2149 return;
2150 }
Adam Langley95c29f32014-06-20 12:00:00 -07002151
David Benjaminb6d0c6d2015-03-19 19:07:26 -04002152 SSL_CTX *ctx = s->initial_ctx;
2153 if ((ctx->session_cache_mode & mode) == mode && !s->hit &&
2154 ((ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) ||
2155 SSL_CTX_add_session(ctx, s->session)) &&
2156 ctx->new_session_cb != NULL) {
2157 /* Note: |new_session_cb| is called whether the internal session cache is
2158 * used or not. */
2159 if (!ctx->new_session_cb(s, SSL_SESSION_up_ref(s->session))) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002160 SSL_SESSION_free(s->session);
2161 }
2162 }
Adam Langley95c29f32014-06-20 12:00:00 -07002163
David Benjaminb6d0c6d2015-03-19 19:07:26 -04002164 if (!(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR) &&
2165 !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) &&
2166 (ctx->session_cache_mode & mode) == mode) {
2167 /* Automatically flush the internal session cache every 255 connections. */
2168 int flush_cache = 0;
2169 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2170 ctx->handshakes_since_cache_flush++;
2171 if (ctx->handshakes_since_cache_flush >= 255) {
2172 flush_cache = 1;
2173 ctx->handshakes_since_cache_flush = 0;
2174 }
2175 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
2176
2177 if (flush_cache) {
2178 SSL_CTX_flush_sessions(ctx, (unsigned long)time(NULL));
Adam Langleyfcf25832014-12-18 17:42:32 -08002179 }
2180 }
2181}
Adam Langley95c29f32014-06-20 12:00:00 -07002182
David Benjamin1a5c50f2015-03-11 16:22:37 -04002183int SSL_get_error(const SSL *s, int ret_code) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002184 int reason;
David Benjamin1a5c50f2015-03-11 16:22:37 -04002185 uint32_t err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002186 BIO *bio;
Adam Langley95c29f32014-06-20 12:00:00 -07002187
David Benjamin1a5c50f2015-03-11 16:22:37 -04002188 if (ret_code > 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002189 return SSL_ERROR_NONE;
2190 }
Adam Langley95c29f32014-06-20 12:00:00 -07002191
Adam Langleyfcf25832014-12-18 17:42:32 -08002192 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
2193 * where we do encode the error */
David Benjamin1a5c50f2015-03-11 16:22:37 -04002194 err = ERR_peek_error();
2195 if (err != 0) {
2196 if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002197 return SSL_ERROR_SYSCALL;
2198 }
2199 return SSL_ERROR_SSL;
2200 }
Adam Langley95c29f32014-06-20 12:00:00 -07002201
David Benjamin1a5c50f2015-03-11 16:22:37 -04002202 if (ret_code == 0) {
David Benjamin9a38e922015-01-22 16:06:11 -05002203 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2204 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) {
2205 /* The socket was cleanly shut down with a close_notify. */
2206 return SSL_ERROR_ZERO_RETURN;
2207 }
2208 /* An EOF was observed which violates the protocol, and the underlying
2209 * transport does not participate in the error queue. Bubble up to the
2210 * caller. */
2211 return SSL_ERROR_SYSCALL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002212 }
Adam Langleyb2ce0582014-06-20 12:00:00 -07002213
Adam Langleyfcf25832014-12-18 17:42:32 -08002214 if (SSL_want_session(s)) {
2215 return SSL_ERROR_PENDING_SESSION;
2216 }
Adam Langley95c29f32014-06-20 12:00:00 -07002217
Adam Langleyfcf25832014-12-18 17:42:32 -08002218 if (SSL_want_certificate(s)) {
2219 return SSL_ERROR_PENDING_CERTIFICATE;
2220 }
Adam Langley95c29f32014-06-20 12:00:00 -07002221
Adam Langleyfcf25832014-12-18 17:42:32 -08002222 if (SSL_want_read(s)) {
2223 bio = SSL_get_rbio(s);
2224 if (BIO_should_read(bio)) {
2225 return SSL_ERROR_WANT_READ;
2226 }
Adam Langley95c29f32014-06-20 12:00:00 -07002227
Adam Langleyfcf25832014-12-18 17:42:32 -08002228 if (BIO_should_write(bio)) {
2229 /* This one doesn't make too much sense ... We never try to write to the
2230 * rbio, and an application program where rbio and wbio are separate
2231 * couldn't even know what it should wait for. However if we ever set
2232 * s->rwstate incorrectly (so that we have SSL_want_read(s) instead of
2233 * SSL_want_write(s)) and rbio and wbio *are* the same, this test works
2234 * around that bug; so it might be safer to keep it. */
2235 return SSL_ERROR_WANT_WRITE;
2236 }
Adam Langley95c29f32014-06-20 12:00:00 -07002237
Adam Langleyfcf25832014-12-18 17:42:32 -08002238 if (BIO_should_io_special(bio)) {
2239 reason = BIO_get_retry_reason(bio);
2240 if (reason == BIO_RR_CONNECT) {
2241 return SSL_ERROR_WANT_CONNECT;
2242 }
Adam Langley95c29f32014-06-20 12:00:00 -07002243
Adam Langleyfcf25832014-12-18 17:42:32 -08002244 if (reason == BIO_RR_ACCEPT) {
2245 return SSL_ERROR_WANT_ACCEPT;
2246 }
Adam Langley95c29f32014-06-20 12:00:00 -07002247
Adam Langleyfcf25832014-12-18 17:42:32 -08002248 return SSL_ERROR_SYSCALL; /* unknown */
2249 }
2250 }
Adam Langley95c29f32014-06-20 12:00:00 -07002251
Adam Langleyfcf25832014-12-18 17:42:32 -08002252 if (SSL_want_write(s)) {
2253 bio = SSL_get_wbio(s);
2254 if (BIO_should_write(bio)) {
2255 return SSL_ERROR_WANT_WRITE;
2256 }
Adam Langley95c29f32014-06-20 12:00:00 -07002257
Adam Langleyfcf25832014-12-18 17:42:32 -08002258 if (BIO_should_read(bio)) {
2259 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2260 return SSL_ERROR_WANT_READ;
2261 }
Adam Langley95c29f32014-06-20 12:00:00 -07002262
Adam Langleyfcf25832014-12-18 17:42:32 -08002263 if (BIO_should_io_special(bio)) {
2264 reason = BIO_get_retry_reason(bio);
2265 if (reason == BIO_RR_CONNECT) {
2266 return SSL_ERROR_WANT_CONNECT;
2267 }
Adam Langley95c29f32014-06-20 12:00:00 -07002268
Adam Langleyfcf25832014-12-18 17:42:32 -08002269 if (reason == BIO_RR_ACCEPT) {
2270 return SSL_ERROR_WANT_ACCEPT;
2271 }
Adam Langley95c29f32014-06-20 12:00:00 -07002272
Adam Langleyfcf25832014-12-18 17:42:32 -08002273 return SSL_ERROR_SYSCALL;
2274 }
2275 }
Adam Langley95c29f32014-06-20 12:00:00 -07002276
Adam Langleyfcf25832014-12-18 17:42:32 -08002277 if (SSL_want_x509_lookup(s)) {
2278 return SSL_ERROR_WANT_X509_LOOKUP;
2279 }
Adam Langley95c29f32014-06-20 12:00:00 -07002280
Adam Langleyfcf25832014-12-18 17:42:32 -08002281 if (SSL_want_channel_id_lookup(s)) {
2282 return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
2283 }
Adam Langley0f4746e2014-08-13 12:26:32 -07002284
Adam Langleyfcf25832014-12-18 17:42:32 -08002285 return SSL_ERROR_SYSCALL;
2286}
Adam Langley0f4746e2014-08-13 12:26:32 -07002287
Adam Langleyfcf25832014-12-18 17:42:32 -08002288int SSL_do_handshake(SSL *s) {
2289 int ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002290
Adam Langleyfcf25832014-12-18 17:42:32 -08002291 if (s->handshake_func == NULL) {
2292 OPENSSL_PUT_ERROR(SSL, SSL_do_handshake, SSL_R_CONNECTION_TYPE_NOT_SET);
2293 return -1;
2294 }
Adam Langley95c29f32014-06-20 12:00:00 -07002295
Adam Langleyfcf25832014-12-18 17:42:32 -08002296 s->method->ssl_renegotiate_check(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002297
Adam Langleyfcf25832014-12-18 17:42:32 -08002298 if (SSL_in_init(s)) {
2299 ret = s->handshake_func(s);
2300 }
2301 return ret;
2302}
Adam Langley95c29f32014-06-20 12:00:00 -07002303
Adam Langleyfcf25832014-12-18 17:42:32 -08002304void SSL_set_accept_state(SSL *s) {
2305 s->server = 1;
2306 s->shutdown = 0;
2307 s->state = SSL_ST_ACCEPT | SSL_ST_BEFORE;
2308 s->handshake_func = s->method->ssl_accept;
2309 /* clear the current cipher */
2310 ssl_clear_cipher_ctx(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002311}
Adam Langley95c29f32014-06-20 12:00:00 -07002312
Adam Langleyfcf25832014-12-18 17:42:32 -08002313void SSL_set_connect_state(SSL *s) {
2314 s->server = 0;
2315 s->shutdown = 0;
2316 s->state = SSL_ST_CONNECT | SSL_ST_BEFORE;
2317 s->handshake_func = s->method->ssl_connect;
2318 /* clear the current cipher */
2319 ssl_clear_cipher_ctx(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002320}
Adam Langley95c29f32014-06-20 12:00:00 -07002321
Adam Langleyfcf25832014-12-18 17:42:32 -08002322int ssl_undefined_function(SSL *s) {
2323 OPENSSL_PUT_ERROR(SSL, ssl_undefined_function,
2324 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2325 return 0;
2326}
Adam Langley95c29f32014-06-20 12:00:00 -07002327
Adam Langleyfcf25832014-12-18 17:42:32 -08002328int ssl_undefined_void_function(void) {
2329 OPENSSL_PUT_ERROR(SSL, ssl_undefined_void_function,
2330 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2331 return 0;
2332}
Adam Langley95c29f32014-06-20 12:00:00 -07002333
Adam Langleyfcf25832014-12-18 17:42:32 -08002334int ssl_undefined_const_function(const SSL *s) {
2335 OPENSSL_PUT_ERROR(SSL, ssl_undefined_const_function,
2336 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2337 return 0;
2338}
Adam Langley95c29f32014-06-20 12:00:00 -07002339
Adam Langleyfcf25832014-12-18 17:42:32 -08002340static const char *ssl_get_version(int version) {
2341 switch (version) {
2342 case TLS1_2_VERSION:
2343 return "TLSv1.2";
Adam Langley95c29f32014-06-20 12:00:00 -07002344
Adam Langleyfcf25832014-12-18 17:42:32 -08002345 case TLS1_1_VERSION:
2346 return "TLSv1.1";
Adam Langley95c29f32014-06-20 12:00:00 -07002347
Adam Langleyfcf25832014-12-18 17:42:32 -08002348 case TLS1_VERSION:
2349 return "TLSv1";
Adam Langley95c29f32014-06-20 12:00:00 -07002350
Adam Langleyfcf25832014-12-18 17:42:32 -08002351 case SSL3_VERSION:
2352 return "SSLv3";
Adam Langley95c29f32014-06-20 12:00:00 -07002353
Adam Langleyfcf25832014-12-18 17:42:32 -08002354 default:
2355 return "unknown";
2356 }
2357}
Adam Langley95c29f32014-06-20 12:00:00 -07002358
Adam Langleyfcf25832014-12-18 17:42:32 -08002359const char *SSL_get_version(const SSL *s) {
2360 return ssl_get_version(s->version);
2361}
Adam Langley95c29f32014-06-20 12:00:00 -07002362
Adam Langleyfcf25832014-12-18 17:42:32 -08002363const char *SSL_SESSION_get_version(const SSL_SESSION *sess) {
2364 return ssl_get_version(sess->ssl_version);
2365}
Adam Langley95c29f32014-06-20 12:00:00 -07002366
Adam Langleyfcf25832014-12-18 17:42:32 -08002367void ssl_clear_cipher_ctx(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002368 if (s->aead_read_ctx != NULL) {
2369 EVP_AEAD_CTX_cleanup(&s->aead_read_ctx->ctx);
2370 OPENSSL_free(s->aead_read_ctx);
2371 s->aead_read_ctx = NULL;
2372 }
Adam Langleya5dc5452014-06-20 12:00:00 -07002373
Adam Langleyfcf25832014-12-18 17:42:32 -08002374 if (s->aead_write_ctx != NULL) {
2375 EVP_AEAD_CTX_cleanup(&s->aead_write_ctx->ctx);
2376 OPENSSL_free(s->aead_write_ctx);
2377 s->aead_write_ctx = NULL;
2378 }
2379}
Adam Langley95c29f32014-06-20 12:00:00 -07002380
Adam Langleyfcf25832014-12-18 17:42:32 -08002381X509 *SSL_get_certificate(const SSL *s) {
2382 if (s->cert != NULL) {
2383 return s->cert->key->x509;
2384 }
2385
2386 return NULL;
2387}
2388
2389EVP_PKEY *SSL_get_privatekey(const SSL *s) {
2390 if (s->cert != NULL) {
2391 return s->cert->key->privatekey;
2392 }
2393
2394 return NULL;
2395}
2396
2397X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
2398 if (ctx->cert != NULL) {
2399 return ctx->cert->key->x509;
2400 }
2401
2402 return NULL;
2403}
2404
2405EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
2406 if (ctx->cert != NULL) {
2407 return ctx->cert->key->privatekey;
2408 }
2409
2410 return NULL;
2411}
2412
2413const SSL_CIPHER *SSL_get_current_cipher(const SSL *s) {
2414 if (s->session != NULL && s->session->cipher != NULL) {
2415 return s->session->cipher;
2416 }
2417
2418 return NULL;
2419}
2420
2421const void *SSL_get_current_compression(SSL *s) { return NULL; }
2422
2423const void *SSL_get_current_expansion(SSL *s) { return NULL; }
2424
2425int ssl_init_wbio_buffer(SSL *s, int push) {
2426 BIO *bbio;
2427
2428 if (s->bbio == NULL) {
2429 bbio = BIO_new(BIO_f_buffer());
2430 if (bbio == NULL) {
2431 return 0;
2432 }
2433 s->bbio = bbio;
2434 } else {
2435 bbio = s->bbio;
2436 if (s->bbio == s->wbio) {
2437 s->wbio = BIO_pop(s->wbio);
2438 }
2439 }
2440
2441 BIO_reset(bbio);
2442 if (!BIO_set_read_buffer_size(bbio, 1)) {
2443 OPENSSL_PUT_ERROR(SSL, ssl_init_wbio_buffer, ERR_R_BUF_LIB);
2444 return 0;
2445 }
2446
2447 if (push) {
2448 if (s->wbio != bbio) {
2449 s->wbio = BIO_push(bbio, s->wbio);
2450 }
2451 } else {
2452 if (s->wbio == bbio) {
2453 s->wbio = BIO_pop(bbio);
2454 }
2455 }
2456
2457 return 1;
2458}
2459
2460void ssl_free_wbio_buffer(SSL *s) {
2461 if (s->bbio == NULL) {
2462 return;
2463 }
2464
2465 if (s->bbio == s->wbio) {
2466 /* remove buffering */
2467 s->wbio = BIO_pop(s->wbio);
2468 }
2469
2470 BIO_free(s->bbio);
2471 s->bbio = NULL;
2472}
2473
2474void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
2475 ctx->quiet_shutdown = mode;
2476}
2477
2478int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
2479 return ctx->quiet_shutdown;
2480}
2481
2482void SSL_set_quiet_shutdown(SSL *s, int mode) { s->quiet_shutdown = mode; }
2483
2484int SSL_get_quiet_shutdown(const SSL *s) { return s->quiet_shutdown; }
2485
2486void SSL_set_shutdown(SSL *s, int mode) { s->shutdown = mode; }
2487
2488int SSL_get_shutdown(const SSL *s) { return s->shutdown; }
2489
2490int SSL_version(const SSL *s) { return s->version; }
2491
2492SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
2493
2494SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
2495 if (ssl->ctx == ctx) {
2496 return ssl->ctx;
2497 }
2498
2499 if (ctx == NULL) {
2500 ctx = ssl->initial_ctx;
2501 }
2502
2503 if (ssl->cert != NULL) {
2504 ssl_cert_free(ssl->cert);
2505 }
2506
2507 ssl->cert = ssl_cert_dup(ctx->cert);
2508 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
2509 if (ssl->ctx != NULL) {
2510 SSL_CTX_free(ssl->ctx); /* decrement reference count */
2511 }
2512 ssl->ctx = ctx;
2513
2514 ssl->sid_ctx_length = ctx->sid_ctx_length;
2515 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2516 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2517
2518 return ssl->ctx;
2519}
2520
2521int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
2522 return X509_STORE_set_default_paths(ctx->cert_store);
2523}
Adam Langley95c29f32014-06-20 12:00:00 -07002524
2525int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
Adam Langleyfcf25832014-12-18 17:42:32 -08002526 const char *CApath) {
2527 return X509_STORE_load_locations(ctx->cert_store, CAfile, CApath);
2528}
Adam Langley95c29f32014-06-20 12:00:00 -07002529
2530void SSL_set_info_callback(SSL *ssl,
Adam Langleyfcf25832014-12-18 17:42:32 -08002531 void (*cb)(const SSL *ssl, int type, int val)) {
2532 ssl->info_callback = cb;
2533}
Adam Langley95c29f32014-06-20 12:00:00 -07002534
Adam Langleyfcf25832014-12-18 17:42:32 -08002535void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/, int /*type*/,
2536 int /*val*/) {
2537 return ssl->info_callback;
2538}
Adam Langley95c29f32014-06-20 12:00:00 -07002539
Adam Langleyfcf25832014-12-18 17:42:32 -08002540int SSL_state(const SSL *ssl) { return ssl->state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002541
Adam Langleyfcf25832014-12-18 17:42:32 -08002542void SSL_set_state(SSL *ssl, int state) { ssl->state = state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002543
Adam Langleyfcf25832014-12-18 17:42:32 -08002544void SSL_set_verify_result(SSL *ssl, long arg) { ssl->verify_result = arg; }
Adam Langley95c29f32014-06-20 12:00:00 -07002545
Adam Langleyfcf25832014-12-18 17:42:32 -08002546long SSL_get_verify_result(const SSL *ssl) { return ssl->verify_result; }
Adam Langley95c29f32014-06-20 12:00:00 -07002547
Adam Langleyfcf25832014-12-18 17:42:32 -08002548int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2549 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
2550 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp, new_func,
2551 dup_func, free_func);
2552}
Adam Langley95c29f32014-06-20 12:00:00 -07002553
Adam Langleyfcf25832014-12-18 17:42:32 -08002554int SSL_set_ex_data(SSL *s, int idx, void *arg) {
2555 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2556}
Adam Langley95c29f32014-06-20 12:00:00 -07002557
Adam Langleyfcf25832014-12-18 17:42:32 -08002558void *SSL_get_ex_data(const SSL *s, int idx) {
2559 return CRYPTO_get_ex_data(&s->ex_data, idx);
2560}
Adam Langley95c29f32014-06-20 12:00:00 -07002561
Adam Langleyfcf25832014-12-18 17:42:32 -08002562int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2563 CRYPTO_EX_dup *dup_func,
2564 CRYPTO_EX_free *free_func) {
2565 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp, new_func,
2566 dup_func, free_func);
2567}
Adam Langley95c29f32014-06-20 12:00:00 -07002568
Adam Langleyfcf25832014-12-18 17:42:32 -08002569int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg) {
2570 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2571}
Adam Langley95c29f32014-06-20 12:00:00 -07002572
Adam Langleyfcf25832014-12-18 17:42:32 -08002573void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx) {
2574 return CRYPTO_get_ex_data(&s->ex_data, idx);
2575}
Adam Langley95c29f32014-06-20 12:00:00 -07002576
Adam Langleyfcf25832014-12-18 17:42:32 -08002577int ssl_ok(SSL *s) { return 1; }
Adam Langley95c29f32014-06-20 12:00:00 -07002578
Adam Langleyfcf25832014-12-18 17:42:32 -08002579X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
2580 return ctx->cert_store;
2581}
Adam Langley95c29f32014-06-20 12:00:00 -07002582
Adam Langleyfcf25832014-12-18 17:42:32 -08002583void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
2584 if (ctx->cert_store != NULL) {
2585 X509_STORE_free(ctx->cert_store);
2586 }
2587 ctx->cert_store = store;
2588}
Adam Langley95c29f32014-06-20 12:00:00 -07002589
Adam Langleyfcf25832014-12-18 17:42:32 -08002590int SSL_want(const SSL *s) { return s->rwstate; }
Adam Langley95c29f32014-06-20 12:00:00 -07002591
Adam Langleyfcf25832014-12-18 17:42:32 -08002592void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
2593 RSA *(*cb)(SSL *ssl, int is_export,
2594 int keylength)) {
2595 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_RSA_CB, (void (*)(void))cb);
2596}
Adam Langley95c29f32014-06-20 12:00:00 -07002597
Adam Langleyfcf25832014-12-18 17:42:32 -08002598void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
2599 int keylength)) {
2600 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_RSA_CB, (void (*)(void))cb);
2601}
2602
2603void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
2604 DH *(*dh)(SSL *ssl, int is_export,
2605 int keylength)) {
2606 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh);
2607}
2608
2609void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*dh)(SSL *ssl, int is_export,
2610 int keylength)) {
2611 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh);
2612}
2613
2614void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,
2615 EC_KEY *(*ecdh)(SSL *ssl, int is_export,
2616 int keylength)) {
2617 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH_CB, (void (*)(void))ecdh);
2618}
2619
2620void SSL_set_tmp_ecdh_callback(SSL *ssl,
2621 EC_KEY *(*ecdh)(SSL *ssl, int is_export,
2622 int keylength)) {
2623 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_ECDH_CB, (void (*)(void))ecdh);
2624}
2625
2626int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
2627 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2628 OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_psk_identity_hint,
2629 SSL_R_DATA_LENGTH_TOO_LONG);
2630 return 0;
2631 }
2632
2633 if (ctx->psk_identity_hint != NULL) {
2634 OPENSSL_free(ctx->psk_identity_hint);
2635 }
2636
2637 if (identity_hint != NULL) {
2638 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2639 if (ctx->psk_identity_hint == NULL) {
2640 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002641 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002642 } else {
2643 ctx->psk_identity_hint = NULL;
2644 }
Adam Langley95c29f32014-06-20 12:00:00 -07002645
Adam Langleyfcf25832014-12-18 17:42:32 -08002646 return 1;
2647}
2648
2649int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint) {
2650 if (s == NULL) {
2651 return 0;
2652 }
2653
2654 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2655 OPENSSL_PUT_ERROR(SSL, SSL_use_psk_identity_hint,
2656 SSL_R_DATA_LENGTH_TOO_LONG);
2657 return 0;
2658 }
2659
2660 /* Clear currently configured hint, if any. */
2661 if (s->psk_identity_hint != NULL) {
2662 OPENSSL_free(s->psk_identity_hint);
2663 s->psk_identity_hint = NULL;
2664 }
2665
2666 if (identity_hint != NULL) {
2667 s->psk_identity_hint = BUF_strdup(identity_hint);
2668 if (s->psk_identity_hint == NULL) {
2669 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002670 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002671 }
Adam Langley95c29f32014-06-20 12:00:00 -07002672
Adam Langleyfcf25832014-12-18 17:42:32 -08002673 return 1;
2674}
Adam Langley95c29f32014-06-20 12:00:00 -07002675
Adam Langleyfcf25832014-12-18 17:42:32 -08002676const char *SSL_get_psk_identity_hint(const SSL *s) {
2677 if (s == NULL) {
2678 return NULL;
2679 }
2680 return s->psk_identity_hint;
2681}
Adam Langley95c29f32014-06-20 12:00:00 -07002682
Adam Langleyfcf25832014-12-18 17:42:32 -08002683const char *SSL_get_psk_identity(const SSL *s) {
2684 if (s == NULL || s->session == NULL) {
2685 return NULL;
2686 }
Adam Langley95c29f32014-06-20 12:00:00 -07002687
Adam Langleyfcf25832014-12-18 17:42:32 -08002688 return s->session->psk_identity;
2689}
Adam Langley95c29f32014-06-20 12:00:00 -07002690
Adam Langleyfcf25832014-12-18 17:42:32 -08002691void SSL_set_psk_client_callback(
2692 SSL *s, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2693 unsigned int max_identity_len, uint8_t *psk,
2694 unsigned int max_psk_len)) {
2695 s->psk_client_callback = cb;
2696}
Adam Langley95c29f32014-06-20 12:00:00 -07002697
Adam Langleyfcf25832014-12-18 17:42:32 -08002698void SSL_CTX_set_psk_client_callback(
2699 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2700 unsigned int max_identity_len,
2701 uint8_t *psk, unsigned int max_psk_len)) {
2702 ctx->psk_client_callback = cb;
2703}
Adam Langley95c29f32014-06-20 12:00:00 -07002704
Adam Langleyfcf25832014-12-18 17:42:32 -08002705void SSL_set_psk_server_callback(
2706 SSL *s, unsigned int (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
2707 unsigned int max_psk_len)) {
2708 s->psk_server_callback = cb;
2709}
Adam Langley95c29f32014-06-20 12:00:00 -07002710
Adam Langleyfcf25832014-12-18 17:42:32 -08002711void SSL_CTX_set_psk_server_callback(
2712 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *identity,
2713 uint8_t *psk, unsigned int max_psk_len)) {
2714 ctx->psk_server_callback = cb;
2715}
Adam Langley95c29f32014-06-20 12:00:00 -07002716
Adam Langleyfcf25832014-12-18 17:42:32 -08002717void SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) {
2718 ctx->min_version = version;
2719}
Adam Langley95c29f32014-06-20 12:00:00 -07002720
Adam Langleyfcf25832014-12-18 17:42:32 -08002721void SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) {
2722 ctx->max_version = version;
2723}
Adam Langley0289c732014-06-20 12:00:00 -07002724
Adam Langleyfcf25832014-12-18 17:42:32 -08002725void SSL_set_min_version(SSL *ssl, uint16_t version) {
2726 ssl->min_version = version;
2727}
Adam Langley0289c732014-06-20 12:00:00 -07002728
Adam Langleyfcf25832014-12-18 17:42:32 -08002729void SSL_set_max_version(SSL *ssl, uint16_t version) {
2730 ssl->max_version = version;
2731}
Adam Langley95c29f32014-06-20 12:00:00 -07002732
Adam Langleyfcf25832014-12-18 17:42:32 -08002733void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
2734 void (*cb)(int write_p, int version,
2735 int content_type, const void *buf,
2736 size_t len, SSL *ssl, void *arg)) {
2737 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2738}
2739void SSL_set_msg_callback(SSL *ssl,
2740 void (*cb)(int write_p, int version, int content_type,
2741 const void *buf, size_t len, SSL *ssl,
2742 void *arg)) {
2743 SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2744}
Adam Langley95c29f32014-06-20 12:00:00 -07002745
Adam Langleyfcf25832014-12-18 17:42:32 -08002746void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio) {
2747 if (ctx->keylog_bio != NULL) {
2748 BIO_free(ctx->keylog_bio);
2749 }
2750 ctx->keylog_bio = keylog_bio;
2751}
Adam Langley95c29f32014-06-20 12:00:00 -07002752
Adam Langleyfcf25832014-12-18 17:42:32 -08002753static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
2754 static const char hextable[] = "0123456789abcdef";
2755 uint8_t *out;
2756 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002757
Adam Langleyfcf25832014-12-18 17:42:32 -08002758 if (!CBB_add_space(cbb, &out, in_len * 2)) {
2759 return 0;
2760 }
Adam Langley95c29f32014-06-20 12:00:00 -07002761
Adam Langleyfcf25832014-12-18 17:42:32 -08002762 for (i = 0; i < in_len; i++) {
2763 *(out++) = (uint8_t)hextable[in[i] >> 4];
2764 *(out++) = (uint8_t)hextable[in[i] & 0xf];
2765 }
Adam Langley95c29f32014-06-20 12:00:00 -07002766
Adam Langleyfcf25832014-12-18 17:42:32 -08002767 return 1;
2768}
David Benjamin859ec3c2014-09-02 16:29:36 -04002769
2770int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -08002771 const uint8_t *encrypted_premaster,
2772 size_t encrypted_premaster_len,
2773 const uint8_t *premaster,
2774 size_t premaster_len) {
2775 BIO *bio = ctx->keylog_bio;
2776 CBB cbb;
2777 uint8_t *out;
2778 size_t out_len;
2779 int ret;
David Benjamin859ec3c2014-09-02 16:29:36 -04002780
Adam Langleyfcf25832014-12-18 17:42:32 -08002781 if (bio == NULL) {
2782 return 1;
2783 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002784
Adam Langleyfcf25832014-12-18 17:42:32 -08002785 if (encrypted_premaster_len < 8) {
2786 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_rsa_client_key_exchange,
2787 ERR_R_INTERNAL_ERROR);
2788 return 0;
2789 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002790
Adam Langleyfcf25832014-12-18 17:42:32 -08002791 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len * 2 + 1)) {
2792 return 0;
2793 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002794
Adam Langleyfcf25832014-12-18 17:42:32 -08002795 if (!CBB_add_bytes(&cbb, (const uint8_t *)"RSA ", 4) ||
2796 /* Only the first 8 bytes of the encrypted premaster secret are
2797 * logged. */
2798 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
2799 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2800 !cbb_add_hex(&cbb, premaster, premaster_len) ||
2801 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2802 !CBB_finish(&cbb, &out, &out_len)) {
2803 CBB_cleanup(&cbb);
2804 return 0;
2805 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002806
Adam Langleyfcf25832014-12-18 17:42:32 -08002807 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2808 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2809 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
David Benjamin859ec3c2014-09-02 16:29:36 -04002810
Adam Langleyfcf25832014-12-18 17:42:32 -08002811 OPENSSL_free(out);
2812 return ret;
Adam Langley95f22882014-06-20 12:00:00 -07002813}
2814
Adam Langleyfcf25832014-12-18 17:42:32 -08002815int ssl_ctx_log_master_secret(SSL_CTX *ctx, const uint8_t *client_random,
2816 size_t client_random_len, const uint8_t *master,
2817 size_t master_len) {
2818 BIO *bio = ctx->keylog_bio;
2819 CBB cbb;
2820 uint8_t *out;
2821 size_t out_len;
2822 int ret;
Adam Langleyadb739e2014-06-20 12:00:00 -07002823
Adam Langleyfcf25832014-12-18 17:42:32 -08002824 if (bio == NULL) {
2825 return 1;
2826 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002827
Adam Langleyfcf25832014-12-18 17:42:32 -08002828 if (client_random_len != 32) {
2829 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_master_secret, ERR_R_INTERNAL_ERROR);
2830 return 0;
2831 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002832
Adam Langleyfcf25832014-12-18 17:42:32 -08002833 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len * 2 + 1)) {
2834 return 0;
2835 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002836
Adam Langleyfcf25832014-12-18 17:42:32 -08002837 if (!CBB_add_bytes(&cbb, (const uint8_t *)"CLIENT_RANDOM ", 14) ||
2838 !cbb_add_hex(&cbb, client_random, 32) ||
2839 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2840 !cbb_add_hex(&cbb, master, master_len) ||
2841 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2842 !CBB_finish(&cbb, &out, &out_len)) {
2843 CBB_cleanup(&cbb);
2844 return 0;
2845 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002846
Adam Langleyfcf25832014-12-18 17:42:32 -08002847 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2848 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2849 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
David Benjamine99e9122014-12-11 01:46:01 -05002850
Adam Langleyfcf25832014-12-18 17:42:32 -08002851 OPENSSL_free(out);
2852 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -07002853}
2854
David Benjamined7c4752015-02-16 19:16:46 -05002855int SSL_in_false_start(const SSL *s) {
2856 return s->s3->tmp.in_false_start;
2857}
2858
Adam Langleyfcf25832014-12-18 17:42:32 -08002859int SSL_cutthrough_complete(const SSL *s) {
David Benjamined7c4752015-02-16 19:16:46 -05002860 return SSL_in_false_start(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002861}
Adam Langley95c29f32014-06-20 12:00:00 -07002862
Adam Langleyfcf25832014-12-18 17:42:32 -08002863void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
2864 size_t *ssl_session_size) {
2865 *ssl_size = sizeof(SSL);
2866 *ssl_ctx_size = sizeof(SSL_CTX);
2867 *ssl_session_size = sizeof(SSL_SESSION);
2868}
Feng Lu41aa3252014-11-21 22:47:56 -08002869
David Benjamined7c4752015-02-16 19:16:46 -05002870int ssl3_can_false_start(const SSL *s) {
David Benjamin195dc782015-02-19 13:27:05 -05002871 const SSL_CIPHER *const cipher = SSL_get_current_cipher(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002872
David Benjamin195dc782015-02-19 13:27:05 -05002873 /* False Start only for TLS 1.2 with a forward-secure, AEAD cipher and ALPN or
2874 * NPN. */
2875 return !SSL_IS_DTLS(s) &&
2876 SSL_version(s) >= TLS1_2_VERSION &&
2877 (s->s3->alpn_selected || s->s3->next_proto_neg_seen) &&
2878 cipher != NULL &&
David Benjamin7061e282015-03-19 11:10:48 -04002879 (cipher->algorithm_mkey == SSL_kDHE ||
2880 cipher->algorithm_mkey == SSL_kECDHE) &&
David Benjamin195dc782015-02-19 13:27:05 -05002881 (cipher->algorithm_enc == SSL_AES128GCM ||
2882 cipher->algorithm_enc == SSL_AES256GCM ||
2883 cipher->algorithm_enc == SSL_CHACHA20POLY1305);
Adam Langleyfcf25832014-12-18 17:42:32 -08002884}
2885
2886const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version) {
2887 switch (version) {
2888 case SSL3_VERSION:
2889 return &SSLv3_enc_data;
2890
2891 case TLS1_VERSION:
2892 return &TLSv1_enc_data;
2893
David Benjamin9e13e1a2015-03-05 01:56:32 -05002894 case DTLS1_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002895 case TLS1_1_VERSION:
2896 return &TLSv1_1_enc_data;
2897
David Benjamin9e13e1a2015-03-05 01:56:32 -05002898 case DTLS1_2_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002899 case TLS1_2_VERSION:
2900 return &TLSv1_2_enc_data;
2901
Adam Langleyfcf25832014-12-18 17:42:32 -08002902 default:
2903 return NULL;
2904 }
2905}
2906
2907uint16_t ssl3_get_max_server_version(const SSL *s) {
2908 uint16_t max_version;
2909
2910 if (SSL_IS_DTLS(s)) {
2911 max_version = (s->max_version != 0) ? s->max_version : DTLS1_2_VERSION;
2912 if (!(s->options & SSL_OP_NO_DTLSv1_2) && DTLS1_2_VERSION >= max_version) {
2913 return DTLS1_2_VERSION;
2914 }
2915 if (!(s->options & SSL_OP_NO_DTLSv1) && DTLS1_VERSION >= max_version) {
2916 return DTLS1_VERSION;
2917 }
2918 return 0;
2919 }
2920
2921 max_version = (s->max_version != 0) ? s->max_version : TLS1_2_VERSION;
2922 if (!(s->options & SSL_OP_NO_TLSv1_2) && TLS1_2_VERSION <= max_version) {
2923 return TLS1_2_VERSION;
2924 }
2925 if (!(s->options & SSL_OP_NO_TLSv1_1) && TLS1_1_VERSION <= max_version) {
2926 return TLS1_1_VERSION;
2927 }
2928 if (!(s->options & SSL_OP_NO_TLSv1) && TLS1_VERSION <= max_version) {
2929 return TLS1_VERSION;
2930 }
2931 if (!(s->options & SSL_OP_NO_SSLv3) && SSL3_VERSION <= max_version) {
2932 return SSL3_VERSION;
2933 }
2934 return 0;
2935}
2936
2937uint16_t ssl3_get_mutual_version(SSL *s, uint16_t client_version) {
2938 uint16_t version = 0;
2939
2940 if (SSL_IS_DTLS(s)) {
2941 /* Clamp client_version to max_version. */
2942 if (s->max_version != 0 && client_version < s->max_version) {
2943 client_version = s->max_version;
2944 }
2945
2946 if (client_version <= DTLS1_2_VERSION && !(s->options & SSL_OP_NO_DTLSv1_2)) {
2947 version = DTLS1_2_VERSION;
2948 } else if (client_version <= DTLS1_VERSION &&
David Benjaminb18f0242015-03-10 18:30:08 -04002949 !(s->options & SSL_OP_NO_DTLSv1)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002950 version = DTLS1_VERSION;
2951 }
2952
2953 /* Check against min_version. */
2954 if (version != 0 && s->min_version != 0 && version > s->min_version) {
2955 return 0;
2956 }
2957 return version;
2958 } else {
2959 /* Clamp client_version to max_version. */
2960 if (s->max_version != 0 && client_version > s->max_version) {
2961 client_version = s->max_version;
2962 }
2963
2964 if (client_version >= TLS1_2_VERSION && !(s->options & SSL_OP_NO_TLSv1_2)) {
2965 version = TLS1_2_VERSION;
2966 } else if (client_version >= TLS1_1_VERSION &&
2967 !(s->options & SSL_OP_NO_TLSv1_1)) {
2968 version = TLS1_1_VERSION;
2969 } else if (client_version >= TLS1_VERSION && !(s->options & SSL_OP_NO_TLSv1)) {
2970 version = TLS1_VERSION;
2971 } else if (client_version >= SSL3_VERSION && !(s->options & SSL_OP_NO_SSLv3)) {
2972 version = SSL3_VERSION;
2973 }
2974
2975 /* Check against min_version. */
2976 if (version != 0 && s->min_version != 0 && version < s->min_version) {
2977 return 0;
2978 }
2979 return version;
2980 }
2981}
2982
2983uint16_t ssl3_get_max_client_version(SSL *s) {
2984 unsigned long options = s->options;
2985 uint16_t version = 0;
2986
2987 /* OpenSSL's API for controlling versions entails blacklisting individual
2988 * protocols. This has two problems. First, on the client, the protocol can
2989 * only express a contiguous range of versions. Second, a library consumer
2990 * trying to set a maximum version cannot disable protocol versions that get
2991 * added in a future version of the library.
2992 *
2993 * To account for both of these, OpenSSL interprets the client-side bitmask
2994 * as a min/max range by picking the lowest contiguous non-empty range of
2995 * enabled protocols. Note that this means it is impossible to set a maximum
2996 * version of TLS 1.2 in a future-proof way.
2997 *
2998 * By this scheme, the maximum version is the lowest version V such that V is
2999 * enabled and V+1 is disabled or unimplemented. */
3000 if (SSL_IS_DTLS(s)) {
3001 if (!(options & SSL_OP_NO_DTLSv1_2)) {
3002 version = DTLS1_2_VERSION;
3003 }
3004 if (!(options & SSL_OP_NO_DTLSv1) && (options & SSL_OP_NO_DTLSv1_2)) {
3005 version = DTLS1_VERSION;
3006 }
3007 if (s->max_version != 0 && version < s->max_version) {
3008 version = s->max_version;
3009 }
3010 } else {
3011 if (!(options & SSL_OP_NO_TLSv1_2)) {
3012 version = TLS1_2_VERSION;
3013 }
3014 if (!(options & SSL_OP_NO_TLSv1_1) && (options & SSL_OP_NO_TLSv1_2)) {
3015 version = TLS1_1_VERSION;
3016 }
3017 if (!(options & SSL_OP_NO_TLSv1) && (options & SSL_OP_NO_TLSv1_1)) {
3018 version = TLS1_VERSION;
3019 }
3020 if (!(options & SSL_OP_NO_SSLv3) && (options & SSL_OP_NO_TLSv1)) {
3021 version = SSL3_VERSION;
3022 }
3023 if (s->max_version != 0 && version > s->max_version) {
3024 version = s->max_version;
3025 }
3026 }
3027
3028 return version;
3029}
3030
3031int ssl3_is_version_enabled(SSL *s, uint16_t version) {
3032 if (SSL_IS_DTLS(s)) {
3033 if (s->max_version != 0 && version < s->max_version) {
3034 return 0;
3035 }
3036 if (s->min_version != 0 && version > s->min_version) {
3037 return 0;
3038 }
3039
3040 switch (version) {
3041 case DTLS1_VERSION:
3042 return !(s->options & SSL_OP_NO_DTLSv1);
3043
3044 case DTLS1_2_VERSION:
3045 return !(s->options & SSL_OP_NO_DTLSv1_2);
3046
3047 default:
3048 return 0;
3049 }
3050 } else {
3051 if (s->max_version != 0 && version > s->max_version) {
3052 return 0;
3053 }
3054 if (s->min_version != 0 && version < s->min_version) {
3055 return 0;
3056 }
3057
3058 switch (version) {
3059 case SSL3_VERSION:
3060 return !(s->options & SSL_OP_NO_SSLv3);
3061
3062 case TLS1_VERSION:
3063 return !(s->options & SSL_OP_NO_TLSv1);
3064
3065 case TLS1_1_VERSION:
3066 return !(s->options & SSL_OP_NO_TLSv1_1);
3067
3068 case TLS1_2_VERSION:
3069 return !(s->options & SSL_OP_NO_TLSv1_2);
3070
3071 default:
3072 return 0;
3073 }
3074 }
3075}
3076
David Benjaminea72bd02014-12-21 21:27:41 -05003077uint16_t ssl3_version_from_wire(SSL *s, uint16_t wire_version) {
3078 if (!SSL_IS_DTLS(s)) {
3079 return wire_version;
3080 }
3081
3082 uint16_t tls_version = ~wire_version;
3083 uint16_t version = tls_version + 0x0201;
3084 /* If either component overflowed, clamp it so comparisons still work. */
3085 if ((version >> 8) < (tls_version >> 8)) {
3086 version = 0xff00 | (version & 0xff);
3087 }
3088 if ((version & 0xff) < (tls_version & 0xff)) {
3089 version = (version & 0xff00) | 0xff;
3090 }
3091 /* DTLS 1.0 maps to TLS 1.1, not TLS 1.0. */
3092 if (version == TLS1_VERSION) {
3093 version = TLS1_1_VERSION;
3094 }
3095 return version;
3096}
3097
Adam Langleyfcf25832014-12-18 17:42:32 -08003098int SSL_cache_hit(SSL *s) { return s->hit; }
3099
3100int SSL_is_server(SSL *s) { return s->server; }
3101
Adam Langley524e7172015-02-20 16:04:00 -08003102void SSL_CTX_set_dos_protection_cb(
3103 SSL_CTX *ctx, int (*cb)(const struct ssl_early_callback_ctx *)) {
3104 ctx->dos_protection_cb = cb;
3105}
3106
Adam Langleyfcf25832014-12-18 17:42:32 -08003107void SSL_enable_fastradio_padding(SSL *s, char on_off) {
3108 s->fastradio_padding = on_off;
3109}
Adam Langley44972942015-02-20 13:43:23 -08003110
3111const SSL_CIPHER *SSL_get_cipher_by_value(uint16_t value) {
3112 return ssl3_get_cipher_by_value(value);
3113}
Adam Langley3f92d212015-02-20 15:32:52 -08003114
3115int SSL_get_rc4_state(const SSL *ssl, const RC4_KEY **read_key,
3116 const RC4_KEY **write_key) {
3117 if (ssl->aead_read_ctx == NULL || ssl->aead_write_ctx == NULL) {
3118 return 0;
3119 }
3120
3121 return EVP_AEAD_CTX_get_rc4_state(&ssl->aead_read_ctx->ctx, read_key) &&
3122 EVP_AEAD_CTX_get_rc4_state(&ssl->aead_write_ctx->ctx, write_key);
3123}