blob: 76f740846ed5528a27b4d571a52e5b85f06aae84 [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
Adam Langleyfcf25832014-12-18 17:42:32 -0800193 s->rwstate = SSL_NOTHING;
194 s->rstate = SSL_ST_READ_HEADER;
Adam Langley95c29f32014-06-20 12:00:00 -0700195
Adam Langleyfcf25832014-12-18 17:42:32 -0800196 if (s->init_buf != NULL) {
197 BUF_MEM_free(s->init_buf);
198 s->init_buf = NULL;
199 }
Adam Langley95c29f32014-06-20 12:00:00 -0700200
Adam Langleyfcf25832014-12-18 17:42:32 -0800201 ssl_clear_cipher_ctx(s);
202 ssl_clear_hash_ctx(&s->read_hash);
203 ssl_clear_hash_ctx(&s->write_hash);
Adam Langley95c29f32014-06-20 12:00:00 -0700204
Adam Langleyfcf25832014-12-18 17:42:32 -0800205 s->method->ssl_clear(s);
206 s->client_version = s->version;
Adam Langley95c29f32014-06-20 12:00:00 -0700207
Adam Langleyfcf25832014-12-18 17:42:32 -0800208 return 1;
209}
Adam Langley95c29f32014-06-20 12:00:00 -0700210
Adam Langleyfcf25832014-12-18 17:42:32 -0800211SSL *SSL_new(SSL_CTX *ctx) {
212 SSL *s;
Adam Langley95c29f32014-06-20 12:00:00 -0700213
Adam Langleyfcf25832014-12-18 17:42:32 -0800214 if (ctx == NULL) {
215 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_NULL_SSL_CTX);
216 return NULL;
217 }
218 if (ctx->method == NULL) {
219 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
220 return NULL;
221 }
Adam Langley95c29f32014-06-20 12:00:00 -0700222
Adam Langleyfcf25832014-12-18 17:42:32 -0800223 s = (SSL *)OPENSSL_malloc(sizeof(SSL));
224 if (s == NULL) {
225 goto err;
226 }
227 memset(s, 0, sizeof(SSL));
Adam Langley95c29f32014-06-20 12:00:00 -0700228
Adam Langleyfcf25832014-12-18 17:42:32 -0800229 s->min_version = ctx->min_version;
230 s->max_version = ctx->max_version;
David Benjamin1eb367c2014-12-12 18:17:51 -0500231
Adam Langleyfcf25832014-12-18 17:42:32 -0800232 s->options = ctx->options;
233 s->mode = ctx->mode;
234 s->max_cert_list = ctx->max_cert_list;
Adam Langley95c29f32014-06-20 12:00:00 -0700235
Adam Langleyfcf25832014-12-18 17:42:32 -0800236 if (ctx->cert != NULL) {
237 /* Earlier library versions used to copy the pointer to the CERT, not its
238 * contents; only when setting new parameters for the per-SSL copy,
239 * ssl_cert_new would be called (and the direct reference to the
240 * per-SSL_CTX settings would be lost, but those still were indirectly
241 * accessed for various purposes, and for that reason they used to be known
242 * as s->ctx->default_cert). Now we don't look at the SSL_CTX's CERT after
243 * having duplicated it once. */
Adam Langley95c29f32014-06-20 12:00:00 -0700244
Adam Langleyfcf25832014-12-18 17:42:32 -0800245 s->cert = ssl_cert_dup(ctx->cert);
246 if (s->cert == NULL) {
247 goto err;
248 }
249 } else {
250 s->cert = NULL; /* Cannot really happen (see SSL_CTX_new) */
251 }
Adam Langley95c29f32014-06-20 12:00:00 -0700252
Adam Langleyfcf25832014-12-18 17:42:32 -0800253 s->read_ahead = ctx->read_ahead;
254 s->msg_callback = ctx->msg_callback;
255 s->msg_callback_arg = ctx->msg_callback_arg;
256 s->verify_mode = ctx->verify_mode;
257 s->sid_ctx_length = ctx->sid_ctx_length;
258 assert(s->sid_ctx_length <= sizeof s->sid_ctx);
259 memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));
260 s->verify_callback = ctx->default_verify_callback;
261 s->generate_session_id = ctx->generate_session_id;
Adam Langley95c29f32014-06-20 12:00:00 -0700262
Adam Langleyfcf25832014-12-18 17:42:32 -0800263 s->param = X509_VERIFY_PARAM_new();
264 if (!s->param) {
265 goto err;
266 }
267 X509_VERIFY_PARAM_inherit(s->param, ctx->param);
268 s->quiet_shutdown = ctx->quiet_shutdown;
269 s->max_send_fragment = ctx->max_send_fragment;
Adam Langley95c29f32014-06-20 12:00:00 -0700270
Adam Langleyfcf25832014-12-18 17:42:32 -0800271 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
272 s->ctx = ctx;
273 s->tlsext_debug_cb = 0;
274 s->tlsext_debug_arg = NULL;
275 s->tlsext_ticket_expected = 0;
276 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
277 s->initial_ctx = ctx;
278 if (ctx->tlsext_ecpointformatlist) {
279 s->tlsext_ecpointformatlist = BUF_memdup(
280 ctx->tlsext_ecpointformatlist, ctx->tlsext_ecpointformatlist_length);
281 if (!s->tlsext_ecpointformatlist) {
282 goto err;
283 }
284 s->tlsext_ecpointformatlist_length = ctx->tlsext_ecpointformatlist_length;
285 }
Adam Langley95c29f32014-06-20 12:00:00 -0700286
Adam Langleyfcf25832014-12-18 17:42:32 -0800287 if (ctx->tlsext_ellipticcurvelist) {
288 s->tlsext_ellipticcurvelist =
289 BUF_memdup(ctx->tlsext_ellipticcurvelist,
290 ctx->tlsext_ellipticcurvelist_length * 2);
291 if (!s->tlsext_ellipticcurvelist) {
292 goto err;
293 }
294 s->tlsext_ellipticcurvelist_length = ctx->tlsext_ellipticcurvelist_length;
295 }
296 s->next_proto_negotiated = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700297
Adam Langleyfcf25832014-12-18 17:42:32 -0800298 if (s->ctx->alpn_client_proto_list) {
299 s->alpn_client_proto_list = BUF_memdup(s->ctx->alpn_client_proto_list,
300 s->ctx->alpn_client_proto_list_len);
301 if (s->alpn_client_proto_list == NULL) {
302 goto err;
303 }
304 s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;
305 }
Adam Langley95c29f32014-06-20 12:00:00 -0700306
Adam Langleyfcf25832014-12-18 17:42:32 -0800307 s->verify_result = X509_V_OK;
308 s->method = ctx->method;
Adam Langley95c29f32014-06-20 12:00:00 -0700309
Adam Langleyfcf25832014-12-18 17:42:32 -0800310 if (!s->method->ssl_new(s)) {
311 goto err;
312 }
313 s->enc_method = ssl3_get_enc_method(s->version);
314 assert(s->enc_method != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700315
Adam Langleyfcf25832014-12-18 17:42:32 -0800316 s->references = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700317
Adam Langleyfcf25832014-12-18 17:42:32 -0800318 SSL_clear(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700319
Adam Langleyfcf25832014-12-18 17:42:32 -0800320 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700321
Adam Langleyfcf25832014-12-18 17:42:32 -0800322 s->psk_identity_hint = NULL;
323 if (ctx->psk_identity_hint) {
324 s->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
325 if (s->psk_identity_hint == NULL) {
326 goto err;
327 }
328 }
329 s->psk_client_callback = ctx->psk_client_callback;
330 s->psk_server_callback = ctx->psk_server_callback;
Adam Langley95c29f32014-06-20 12:00:00 -0700331
David Benjamin02ddbfd2015-01-11 13:09:11 -0500332 s->tlsext_channel_id_enabled = ctx->tlsext_channel_id_enabled;
333 if (ctx->tlsext_channel_id_private) {
334 s->tlsext_channel_id_private = EVP_PKEY_dup(ctx->tlsext_channel_id_private);
335 }
336
Adam Langleyfcf25832014-12-18 17:42:32 -0800337 s->signed_cert_timestamps_enabled = s->ctx->signed_cert_timestamps_enabled;
338 s->ocsp_stapling_enabled = s->ctx->ocsp_stapling_enabled;
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200339
Adam Langleyfcf25832014-12-18 17:42:32 -0800340 return s;
341
Adam Langley95c29f32014-06-20 12:00:00 -0700342err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800343 if (s != NULL) {
344 if (s->cert != NULL) {
345 ssl_cert_free(s->cert);
Adam Langley95c29f32014-06-20 12:00:00 -0700346 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800347 if (s->ctx != NULL) {
348 SSL_CTX_free(s->ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700349 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800350 OPENSSL_free(s);
351 }
352 OPENSSL_PUT_ERROR(SSL, SSL_new, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700353
Adam Langleyfcf25832014-12-18 17:42:32 -0800354 return NULL;
355}
Adam Langley95c29f32014-06-20 12:00:00 -0700356
Adam Langleyfcf25832014-12-18 17:42:32 -0800357int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
358 unsigned int sid_ctx_len) {
359 if (sid_ctx_len > sizeof ctx->sid_ctx) {
360 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_session_id_context,
361 SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
362 return 0;
363 }
364 ctx->sid_ctx_length = sid_ctx_len;
365 memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700366
Adam Langleyfcf25832014-12-18 17:42:32 -0800367 return 1;
368}
Adam Langley95c29f32014-06-20 12:00:00 -0700369
Adam Langleyfcf25832014-12-18 17:42:32 -0800370int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
371 unsigned int sid_ctx_len) {
372 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
373 OPENSSL_PUT_ERROR(SSL, SSL_set_session_id_context,
374 SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
375 return 0;
376 }
377 ssl->sid_ctx_length = sid_ctx_len;
378 memcpy(ssl->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_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb) {
384 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
385 ctx->generate_session_id = cb;
386 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
387 return 1;
388}
Adam Langley95c29f32014-06-20 12:00:00 -0700389
Adam Langleyfcf25832014-12-18 17:42:32 -0800390int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb) {
391 CRYPTO_w_lock(CRYPTO_LOCK_SSL);
392 ssl->generate_session_id = cb;
393 CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
394 return 1;
395}
Adam Langley95c29f32014-06-20 12:00:00 -0700396
Adam Langleyfcf25832014-12-18 17:42:32 -0800397int SSL_has_matching_session_id(const SSL *ssl, const uint8_t *id,
398 unsigned int id_len) {
399 /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how we
400 * can "construct" a session to give us the desired check - ie. to find if
401 * there's a session in the hash table that would conflict with any new
402 * session built out of this id/id_len and the ssl_version in use by this
403 * SSL. */
404 SSL_SESSION r, *p;
Adam Langley95c29f32014-06-20 12:00:00 -0700405
Adam Langleyfcf25832014-12-18 17:42:32 -0800406 if (id_len > sizeof r.session_id) {
407 return 0;
408 }
Adam Langley95c29f32014-06-20 12:00:00 -0700409
Adam Langleyfcf25832014-12-18 17:42:32 -0800410 r.ssl_version = ssl->version;
411 r.session_id_length = id_len;
412 memcpy(r.session_id, id, id_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700413
Adam Langleyfcf25832014-12-18 17:42:32 -0800414 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
415 p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r);
416 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
417 return p != NULL;
418}
Adam Langley95c29f32014-06-20 12:00:00 -0700419
Adam Langleyfcf25832014-12-18 17:42:32 -0800420int SSL_CTX_set_purpose(SSL_CTX *s, int purpose) {
421 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
422}
423
424int SSL_set_purpose(SSL *s, int purpose) {
425 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
426}
427
428int SSL_CTX_set_trust(SSL_CTX *s, int trust) {
429 return X509_VERIFY_PARAM_set_trust(s->param, trust);
430}
431
432int SSL_set_trust(SSL *s, int trust) {
433 return X509_VERIFY_PARAM_set_trust(s->param, trust);
434}
435
436int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm) {
437 return X509_VERIFY_PARAM_set1(ctx->param, vpm);
438}
439
440int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm) {
441 return X509_VERIFY_PARAM_set1(ssl->param, vpm);
442}
Adam Langley95c29f32014-06-20 12:00:00 -0700443
Adam Langley858a88d2014-06-20 12:00:00 -0700444void ssl_cipher_preference_list_free(
Adam Langleyfcf25832014-12-18 17:42:32 -0800445 struct ssl_cipher_preference_list_st *cipher_list) {
446 sk_SSL_CIPHER_free(cipher_list->ciphers);
447 OPENSSL_free(cipher_list->in_group_flags);
448 OPENSSL_free(cipher_list);
449}
Adam Langley858a88d2014-06-20 12:00:00 -0700450
Adam Langleyfcf25832014-12-18 17:42:32 -0800451struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_dup(
452 struct ssl_cipher_preference_list_st *cipher_list) {
453 struct ssl_cipher_preference_list_st *ret = NULL;
454 size_t n = sk_SSL_CIPHER_num(cipher_list->ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700455
Adam Langleyfcf25832014-12-18 17:42:32 -0800456 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
457 if (!ret) {
458 goto err;
459 }
460
461 ret->ciphers = NULL;
462 ret->in_group_flags = NULL;
463 ret->ciphers = sk_SSL_CIPHER_dup(cipher_list->ciphers);
464 if (!ret->ciphers) {
465 goto err;
466 }
467 ret->in_group_flags = BUF_memdup(cipher_list->in_group_flags, n);
468 if (!ret->in_group_flags) {
469 goto err;
470 }
471
472 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -0700473
474err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800475 if (ret && ret->ciphers) {
476 sk_SSL_CIPHER_free(ret->ciphers);
477 }
478 if (ret) {
479 OPENSSL_free(ret);
480 }
481 return NULL;
482}
Adam Langley858a88d2014-06-20 12:00:00 -0700483
Adam Langleyfcf25832014-12-18 17:42:32 -0800484struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_from_ciphers(
485 STACK_OF(SSL_CIPHER) * ciphers) {
486 struct ssl_cipher_preference_list_st *ret = NULL;
487 size_t n = sk_SSL_CIPHER_num(ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700488
Adam Langleyfcf25832014-12-18 17:42:32 -0800489 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
490 if (!ret) {
491 goto err;
492 }
493 ret->ciphers = NULL;
494 ret->in_group_flags = NULL;
495 ret->ciphers = sk_SSL_CIPHER_dup(ciphers);
496 if (!ret->ciphers) {
497 goto err;
498 }
499 ret->in_group_flags = OPENSSL_malloc(n);
500 if (!ret->in_group_flags) {
501 goto err;
502 }
503 memset(ret->in_group_flags, 0, n);
504 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -0700505
506err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800507 if (ret && ret->ciphers) {
508 sk_SSL_CIPHER_free(ret->ciphers);
509 }
510 if (ret) {
511 OPENSSL_free(ret);
512 }
513 return NULL;
514}
Adam Langley858a88d2014-06-20 12:00:00 -0700515
Adam Langleyfcf25832014-12-18 17:42:32 -0800516X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) { return ctx->param; }
Adam Langley95c29f32014-06-20 12:00:00 -0700517
Adam Langleyfcf25832014-12-18 17:42:32 -0800518X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) { return ssl->param; }
Adam Langley95c29f32014-06-20 12:00:00 -0700519
Adam Langleyfcf25832014-12-18 17:42:32 -0800520void SSL_certs_clear(SSL *s) { ssl_cert_clear_certs(s->cert); }
Adam Langley95c29f32014-06-20 12:00:00 -0700521
Adam Langleyfcf25832014-12-18 17:42:32 -0800522void SSL_free(SSL *s) {
523 int i;
Adam Langley95c29f32014-06-20 12:00:00 -0700524
Adam Langleyfcf25832014-12-18 17:42:32 -0800525 if (s == NULL) {
526 return;
527 }
Adam Langley95c29f32014-06-20 12:00:00 -0700528
Adam Langleyfcf25832014-12-18 17:42:32 -0800529 i = CRYPTO_add(&s->references, -1, CRYPTO_LOCK_SSL);
530 if (i > 0) {
531 return;
532 }
Adam Langley95c29f32014-06-20 12:00:00 -0700533
Adam Langleyfcf25832014-12-18 17:42:32 -0800534 if (s->param) {
535 X509_VERIFY_PARAM_free(s->param);
536 }
Adam Langley95c29f32014-06-20 12:00:00 -0700537
Adam Langleyfcf25832014-12-18 17:42:32 -0800538 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700539
Adam Langleyfcf25832014-12-18 17:42:32 -0800540 if (s->bbio != NULL) {
541 /* If the buffering BIO is in place, pop it off */
542 if (s->bbio == s->wbio) {
543 s->wbio = BIO_pop(s->wbio);
544 }
545 BIO_free(s->bbio);
546 s->bbio = NULL;
547 }
Adam Langley95c29f32014-06-20 12:00:00 -0700548
Adam Langleyfcf25832014-12-18 17:42:32 -0800549 if (s->rbio != NULL) {
550 BIO_free_all(s->rbio);
551 }
Adam Langley95c29f32014-06-20 12:00:00 -0700552
Adam Langleyfcf25832014-12-18 17:42:32 -0800553 if (s->wbio != NULL && s->wbio != s->rbio) {
554 BIO_free_all(s->wbio);
555 }
Adam Langley95c29f32014-06-20 12:00:00 -0700556
Adam Langleyfcf25832014-12-18 17:42:32 -0800557 if (s->init_buf != NULL) {
558 BUF_MEM_free(s->init_buf);
559 }
Adam Langley95c29f32014-06-20 12:00:00 -0700560
Adam Langleyfcf25832014-12-18 17:42:32 -0800561 /* add extra stuff */
562 if (s->cipher_list != NULL) {
563 ssl_cipher_preference_list_free(s->cipher_list);
564 }
565 if (s->cipher_list_by_id != NULL) {
566 sk_SSL_CIPHER_free(s->cipher_list_by_id);
567 }
Adam Langley95c29f32014-06-20 12:00:00 -0700568
Adam Langleyfcf25832014-12-18 17:42:32 -0800569 if (s->session != NULL) {
570 ssl_clear_bad_session(s);
571 SSL_SESSION_free(s->session);
572 }
Adam Langley95c29f32014-06-20 12:00:00 -0700573
Adam Langleyfcf25832014-12-18 17:42:32 -0800574 ssl_clear_cipher_ctx(s);
575 ssl_clear_hash_ctx(&s->read_hash);
576 ssl_clear_hash_ctx(&s->write_hash);
Adam Langley95c29f32014-06-20 12:00:00 -0700577
Adam Langleyfcf25832014-12-18 17:42:32 -0800578 if (s->cert != NULL) {
579 ssl_cert_free(s->cert);
580 }
Adam Langley0289c732014-06-20 12:00:00 -0700581
Adam Langleyfcf25832014-12-18 17:42:32 -0800582 if (s->tlsext_hostname) {
583 OPENSSL_free(s->tlsext_hostname);
584 }
585 if (s->initial_ctx) {
586 SSL_CTX_free(s->initial_ctx);
587 }
588 if (s->tlsext_ecpointformatlist) {
589 OPENSSL_free(s->tlsext_ecpointformatlist);
590 }
591 if (s->tlsext_ellipticcurvelist) {
592 OPENSSL_free(s->tlsext_ellipticcurvelist);
593 }
594 if (s->alpn_client_proto_list) {
595 OPENSSL_free(s->alpn_client_proto_list);
596 }
597 if (s->tlsext_channel_id_private) {
598 EVP_PKEY_free(s->tlsext_channel_id_private);
599 }
600 if (s->psk_identity_hint) {
601 OPENSSL_free(s->psk_identity_hint);
602 }
603 if (s->client_CA != NULL) {
604 sk_X509_NAME_pop_free(s->client_CA, X509_NAME_free);
605 }
606 if (s->next_proto_negotiated) {
607 OPENSSL_free(s->next_proto_negotiated);
608 }
609 if (s->srtp_profiles) {
610 sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);
611 }
Adam Langley95c29f32014-06-20 12:00:00 -0700612
Adam Langleyfcf25832014-12-18 17:42:32 -0800613 if (s->method != NULL) {
614 s->method->ssl_free(s);
615 }
616 if (s->ctx) {
617 SSL_CTX_free(s->ctx);
618 }
Adam Langley95c29f32014-06-20 12:00:00 -0700619
Adam Langleyfcf25832014-12-18 17:42:32 -0800620 OPENSSL_free(s);
621}
Adam Langley95c29f32014-06-20 12:00:00 -0700622
Adam Langleyfcf25832014-12-18 17:42:32 -0800623void SSL_set_bio(SSL *s, BIO *rbio, BIO *wbio) {
624 /* If the output buffering BIO is still in place, remove it. */
625 if (s->bbio != NULL) {
626 if (s->wbio == s->bbio) {
627 s->wbio = s->wbio->next_bio;
628 s->bbio->next_bio = NULL;
629 }
630 }
Adam Langley95c29f32014-06-20 12:00:00 -0700631
Adam Langleyfcf25832014-12-18 17:42:32 -0800632 if (s->rbio != NULL && s->rbio != rbio) {
633 BIO_free_all(s->rbio);
634 }
635 if (s->wbio != NULL && s->wbio != wbio && s->rbio != s->wbio) {
636 BIO_free_all(s->wbio);
637 }
638 s->rbio = rbio;
639 s->wbio = wbio;
640}
Adam Langley95c29f32014-06-20 12:00:00 -0700641
Adam Langleyfcf25832014-12-18 17:42:32 -0800642BIO *SSL_get_rbio(const SSL *s) { return s->rbio; }
Adam Langley95c29f32014-06-20 12:00:00 -0700643
Adam Langleyfcf25832014-12-18 17:42:32 -0800644BIO *SSL_get_wbio(const SSL *s) { return s->wbio; }
Adam Langley95c29f32014-06-20 12:00:00 -0700645
Adam Langleyfcf25832014-12-18 17:42:32 -0800646int SSL_get_fd(const SSL *s) { return SSL_get_rfd(s); }
Adam Langley95c29f32014-06-20 12:00:00 -0700647
Adam Langleyfcf25832014-12-18 17:42:32 -0800648int SSL_get_rfd(const SSL *s) {
649 int ret = -1;
650 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700651
Adam Langleyfcf25832014-12-18 17:42:32 -0800652 b = SSL_get_rbio(s);
653 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
654 if (r != NULL) {
655 BIO_get_fd(r, &ret);
656 }
657 return ret;
658}
Adam Langley95c29f32014-06-20 12:00:00 -0700659
Adam Langleyfcf25832014-12-18 17:42:32 -0800660int SSL_get_wfd(const SSL *s) {
661 int ret = -1;
662 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700663
Adam Langleyfcf25832014-12-18 17:42:32 -0800664 b = SSL_get_wbio(s);
665 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
666 if (r != NULL) {
667 BIO_get_fd(r, &ret);
668 }
Adam Langley95c29f32014-06-20 12:00:00 -0700669
Adam Langleyfcf25832014-12-18 17:42:32 -0800670 return ret;
671}
Adam Langley95c29f32014-06-20 12:00:00 -0700672
Adam Langleyfcf25832014-12-18 17:42:32 -0800673int SSL_set_fd(SSL *s, int fd) {
674 int ret = 0;
675 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700676
Adam Langleyfcf25832014-12-18 17:42:32 -0800677 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700678
Adam Langleyfcf25832014-12-18 17:42:32 -0800679 if (bio == NULL) {
680 OPENSSL_PUT_ERROR(SSL, SSL_set_fd, ERR_R_BUF_LIB);
681 goto err;
682 }
683 BIO_set_fd(bio, fd, BIO_NOCLOSE);
684 SSL_set_bio(s, bio, bio);
685 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700686
Adam Langley95c29f32014-06-20 12:00:00 -0700687err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800688 return ret;
689}
Adam Langley95c29f32014-06-20 12:00:00 -0700690
Adam Langleyfcf25832014-12-18 17:42:32 -0800691int SSL_set_wfd(SSL *s, int fd) {
692 int ret = 0;
693 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700694
Adam Langleyfcf25832014-12-18 17:42:32 -0800695 if (s->rbio == NULL || BIO_method_type(s->rbio) != BIO_TYPE_FD ||
696 (int)BIO_get_fd(s->rbio, NULL) != fd) {
697 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700698
Adam Langleyfcf25832014-12-18 17:42:32 -0800699 if (bio == NULL) {
700 OPENSSL_PUT_ERROR(SSL, SSL_set_wfd, ERR_R_BUF_LIB);
701 goto err;
702 }
703 BIO_set_fd(bio, fd, BIO_NOCLOSE);
704 SSL_set_bio(s, SSL_get_rbio(s), bio);
705 } else {
706 SSL_set_bio(s, SSL_get_rbio(s), SSL_get_rbio(s));
707 }
708
709 ret = 1;
710
Adam Langley95c29f32014-06-20 12:00:00 -0700711err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800712 return ret;
713}
Adam Langley95c29f32014-06-20 12:00:00 -0700714
Adam Langleyfcf25832014-12-18 17:42:32 -0800715int SSL_set_rfd(SSL *s, int fd) {
716 int ret = 0;
717 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700718
Adam Langleyfcf25832014-12-18 17:42:32 -0800719 if (s->wbio == NULL || BIO_method_type(s->wbio) != BIO_TYPE_FD ||
720 (int)BIO_get_fd(s->wbio, NULL) != fd) {
721 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700722
Adam Langleyfcf25832014-12-18 17:42:32 -0800723 if (bio == NULL) {
724 OPENSSL_PUT_ERROR(SSL, SSL_set_rfd, ERR_R_BUF_LIB);
725 goto err;
726 }
727 BIO_set_fd(bio, fd, BIO_NOCLOSE);
728 SSL_set_bio(s, bio, SSL_get_wbio(s));
729 } else {
730 SSL_set_bio(s, SSL_get_wbio(s), SSL_get_wbio(s));
731 }
732 ret = 1;
733
Adam Langley95c29f32014-06-20 12:00:00 -0700734err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800735 return ret;
736}
Adam Langley95c29f32014-06-20 12:00:00 -0700737
738/* return length of latest Finished message we sent, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -0800739size_t SSL_get_finished(const SSL *s, void *buf, size_t count) {
740 size_t ret = 0;
741
742 if (s->s3 != NULL) {
743 ret = s->s3->tmp.finish_md_len;
744 if (count > ret) {
745 count = ret;
746 }
747 memcpy(buf, s->s3->tmp.finish_md, count);
748 }
749
750 return ret;
751}
Adam Langley95c29f32014-06-20 12:00:00 -0700752
753/* return length of latest Finished message we expected, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -0800754size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count) {
755 size_t ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700756
Adam Langleyfcf25832014-12-18 17:42:32 -0800757 if (s->s3 != NULL) {
758 ret = s->s3->tmp.peer_finish_md_len;
759 if (count > ret) {
760 count = ret;
761 }
762 memcpy(buf, s->s3->tmp.peer_finish_md, count);
763 }
Adam Langley95c29f32014-06-20 12:00:00 -0700764
Adam Langleyfcf25832014-12-18 17:42:32 -0800765 return ret;
766}
Adam Langley95c29f32014-06-20 12:00:00 -0700767
Adam Langleyfcf25832014-12-18 17:42:32 -0800768int SSL_get_verify_mode(const SSL *s) { return s->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -0700769
Adam Langleyfcf25832014-12-18 17:42:32 -0800770int SSL_get_verify_depth(const SSL *s) {
771 return X509_VERIFY_PARAM_get_depth(s->param);
772}
Adam Langley95c29f32014-06-20 12:00:00 -0700773
Adam Langleyfcf25832014-12-18 17:42:32 -0800774int (*SSL_get_verify_callback(const SSL *s))(int, X509_STORE_CTX *) {
775 return s->verify_callback;
776}
Adam Langley95c29f32014-06-20 12:00:00 -0700777
Adam Langleyfcf25832014-12-18 17:42:32 -0800778int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) { return ctx->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -0700779
Adam Langleyfcf25832014-12-18 17:42:32 -0800780int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) {
781 return X509_VERIFY_PARAM_get_depth(ctx->param);
782}
Adam Langley95c29f32014-06-20 12:00:00 -0700783
Adam Langleyfcf25832014-12-18 17:42:32 -0800784int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int, X509_STORE_CTX *) {
785 return ctx->default_verify_callback;
786}
Adam Langley95c29f32014-06-20 12:00:00 -0700787
Adam Langleyfcf25832014-12-18 17:42:32 -0800788void SSL_set_verify(SSL *s, int mode,
789 int (*callback)(int ok, X509_STORE_CTX *ctx)) {
790 s->verify_mode = mode;
791 if (callback != NULL) {
792 s->verify_callback = callback;
793 }
794}
Adam Langley95c29f32014-06-20 12:00:00 -0700795
Adam Langleyfcf25832014-12-18 17:42:32 -0800796void SSL_set_verify_depth(SSL *s, int depth) {
797 X509_VERIFY_PARAM_set_depth(s->param, depth);
798}
Adam Langley95c29f32014-06-20 12:00:00 -0700799
Adam Langleyfcf25832014-12-18 17:42:32 -0800800void SSL_set_read_ahead(SSL *s, int yes) { s->read_ahead = yes; }
Adam Langley95c29f32014-06-20 12:00:00 -0700801
Adam Langleyfcf25832014-12-18 17:42:32 -0800802int SSL_get_read_ahead(const SSL *s) { return s->read_ahead; }
Adam Langley95c29f32014-06-20 12:00:00 -0700803
Adam Langleyfcf25832014-12-18 17:42:32 -0800804int SSL_pending(const SSL *s) {
805 /* SSL_pending cannot work properly if read-ahead is enabled
806 * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)), and it is
807 * impossible to fix since SSL_pending cannot report errors that may be
808 * observed while scanning the new data. (Note that SSL_pending() is often
809 * used as a boolean value, so we'd better not return -1.). */
810 return s->method->ssl_pending(s);
811}
Adam Langley95c29f32014-06-20 12:00:00 -0700812
Adam Langleyfcf25832014-12-18 17:42:32 -0800813X509 *SSL_get_peer_certificate(const SSL *s) {
814 X509 *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700815
Adam Langleyfcf25832014-12-18 17:42:32 -0800816 if (s == NULL || s->session == NULL) {
817 r = NULL;
818 } else {
819 r = s->session->peer;
820 }
Adam Langley95c29f32014-06-20 12:00:00 -0700821
Adam Langleyfcf25832014-12-18 17:42:32 -0800822 if (r == NULL) {
823 return NULL;
824 }
Adam Langley95c29f32014-06-20 12:00:00 -0700825
Adam Langleyfcf25832014-12-18 17:42:32 -0800826 return X509_up_ref(r);
827}
828
829STACK_OF(X509) * SSL_get_peer_cert_chain(const SSL *s) {
830 STACK_OF(X509) * r;
831
832 if (s == NULL || s->session == NULL || s->session->sess_cert == NULL) {
833 r = NULL;
834 } else {
835 r = s->session->sess_cert->cert_chain;
836 }
837
838 /* If we are a client, cert_chain includes the peer's own certificate; if we
839 * are a server, it does not. */
840 return r;
841}
Adam Langley95c29f32014-06-20 12:00:00 -0700842
Adam Langley95c29f32014-06-20 12:00:00 -0700843/* Fix this so it checks all the valid key/cert options */
Adam Langleyfcf25832014-12-18 17:42:32 -0800844int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
845 if (ctx == NULL || ctx->cert == NULL || ctx->cert->key->x509 == NULL) {
846 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key,
847 SSL_R_NO_CERTIFICATE_ASSIGNED);
848 return 0;
849 }
850
851 if (ctx->cert->key->privatekey == NULL) {
852 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key,
853 SSL_R_NO_PRIVATE_KEY_ASSIGNED);
854 return 0;
855 }
856
857 return X509_check_private_key(ctx->cert->key->x509,
858 ctx->cert->key->privatekey);
859}
Adam Langley95c29f32014-06-20 12:00:00 -0700860
861/* Fix this function so that it takes an optional type parameter */
Adam Langleyfcf25832014-12-18 17:42:32 -0800862int SSL_check_private_key(const SSL *ssl) {
863 if (ssl == NULL) {
864 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, ERR_R_PASSED_NULL_PARAMETER);
865 return 0;
866 }
Adam Langley95c29f32014-06-20 12:00:00 -0700867
Adam Langleyfcf25832014-12-18 17:42:32 -0800868 if (ssl->cert == NULL) {
869 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
870 SSL_R_NO_CERTIFICATE_ASSIGNED);
871 return 0;
872 }
Adam Langley95c29f32014-06-20 12:00:00 -0700873
Adam Langleyfcf25832014-12-18 17:42:32 -0800874 if (ssl->cert->key->x509 == NULL) {
875 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
876 SSL_R_NO_CERTIFICATE_ASSIGNED);
877 return 0;
878 }
David Benjamin0b145c22014-11-26 20:10:09 -0500879
Adam Langleyfcf25832014-12-18 17:42:32 -0800880 if (ssl->cert->key->privatekey == NULL) {
881 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
882 SSL_R_NO_PRIVATE_KEY_ASSIGNED);
883 return 0;
884 }
Adam Langley95c29f32014-06-20 12:00:00 -0700885
Adam Langleyfcf25832014-12-18 17:42:32 -0800886 return X509_check_private_key(ssl->cert->key->x509,
887 ssl->cert->key->privatekey);
888}
Adam Langley95c29f32014-06-20 12:00:00 -0700889
Adam Langleyfcf25832014-12-18 17:42:32 -0800890int SSL_accept(SSL *s) {
891 if (s->handshake_func == 0) {
892 /* Not properly initialized yet */
893 SSL_set_accept_state(s);
894 }
David Benjamin0b145c22014-11-26 20:10:09 -0500895
Adam Langleyfcf25832014-12-18 17:42:32 -0800896 if (s->handshake_func != s->method->ssl_accept) {
897 OPENSSL_PUT_ERROR(SSL, SSL_connect, ERR_R_INTERNAL_ERROR);
898 return -1;
899 }
Adam Langley95c29f32014-06-20 12:00:00 -0700900
Adam Langleyfcf25832014-12-18 17:42:32 -0800901 return s->handshake_func(s);
902}
Adam Langley95c29f32014-06-20 12:00:00 -0700903
Adam Langleyfcf25832014-12-18 17:42:32 -0800904int SSL_connect(SSL *s) {
905 if (s->handshake_func == 0) {
906 /* Not properly initialized yet */
907 SSL_set_connect_state(s);
908 }
Adam Langley95c29f32014-06-20 12:00:00 -0700909
Adam Langleyfcf25832014-12-18 17:42:32 -0800910 if (s->handshake_func != s->method->ssl_connect) {
911 OPENSSL_PUT_ERROR(SSL, SSL_connect, ERR_R_INTERNAL_ERROR);
912 return -1;
913 }
Adam Langley95c29f32014-06-20 12:00:00 -0700914
Adam Langleyfcf25832014-12-18 17:42:32 -0800915 return s->handshake_func(s);
916}
Adam Langley95c29f32014-06-20 12:00:00 -0700917
Adam Langleyfcf25832014-12-18 17:42:32 -0800918long SSL_get_default_timeout(const SSL *s) {
919 return SSL_DEFAULT_SESSION_TIMEOUT;
920}
Adam Langley95c29f32014-06-20 12:00:00 -0700921
Adam Langleyfcf25832014-12-18 17:42:32 -0800922int SSL_read(SSL *s, void *buf, int num) {
923 if (s->handshake_func == 0) {
924 OPENSSL_PUT_ERROR(SSL, SSL_read, SSL_R_UNINITIALIZED);
925 return -1;
926 }
Adam Langley95c29f32014-06-20 12:00:00 -0700927
Adam Langleyfcf25832014-12-18 17:42:32 -0800928 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
929 s->rwstate = SSL_NOTHING;
930 return 0;
931 }
Adam Langley95c29f32014-06-20 12:00:00 -0700932
Adam Langleyfcf25832014-12-18 17:42:32 -0800933 return s->method->ssl_read(s, buf, num);
934}
Adam Langley95c29f32014-06-20 12:00:00 -0700935
Adam Langleyfcf25832014-12-18 17:42:32 -0800936int SSL_peek(SSL *s, void *buf, int num) {
937 if (s->handshake_func == 0) {
938 OPENSSL_PUT_ERROR(SSL, SSL_peek, SSL_R_UNINITIALIZED);
939 return -1;
940 }
Adam Langley95c29f32014-06-20 12:00:00 -0700941
Adam Langleyfcf25832014-12-18 17:42:32 -0800942 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
943 return 0;
944 }
Adam Langley95c29f32014-06-20 12:00:00 -0700945
Adam Langleyfcf25832014-12-18 17:42:32 -0800946 return s->method->ssl_peek(s, buf, num);
947}
Adam Langley95c29f32014-06-20 12:00:00 -0700948
Adam Langleyfcf25832014-12-18 17:42:32 -0800949int SSL_write(SSL *s, const void *buf, int num) {
950 if (s->handshake_func == 0) {
951 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_UNINITIALIZED);
952 return -1;
953 }
Adam Langley95c29f32014-06-20 12:00:00 -0700954
Adam Langleyfcf25832014-12-18 17:42:32 -0800955 if (s->shutdown & SSL_SENT_SHUTDOWN) {
956 s->rwstate = SSL_NOTHING;
957 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_PROTOCOL_IS_SHUTDOWN);
958 return -1;
959 }
Adam Langley95c29f32014-06-20 12:00:00 -0700960
Adam Langleyfcf25832014-12-18 17:42:32 -0800961 return s->method->ssl_write(s, buf, num);
962}
Adam Langley95c29f32014-06-20 12:00:00 -0700963
Adam Langleyfcf25832014-12-18 17:42:32 -0800964int SSL_shutdown(SSL *s) {
965 /* Note that this function behaves differently from what one might expect.
966 * Return values are 0 for no success (yet), 1 for success; but calling it
967 * once is usually not enough, even if blocking I/O is used (see
968 * ssl3_shutdown). */
Adam Langley95c29f32014-06-20 12:00:00 -0700969
Adam Langleyfcf25832014-12-18 17:42:32 -0800970 if (s->handshake_func == 0) {
971 OPENSSL_PUT_ERROR(SSL, SSL_shutdown, SSL_R_UNINITIALIZED);
972 return -1;
973 }
Adam Langley95c29f32014-06-20 12:00:00 -0700974
Adam Langleyfcf25832014-12-18 17:42:32 -0800975 if (!SSL_in_init(s)) {
976 return s->method->ssl_shutdown(s);
977 }
Adam Langley95c29f32014-06-20 12:00:00 -0700978
Adam Langleyfcf25832014-12-18 17:42:32 -0800979 return 1;
980}
Adam Langley95c29f32014-06-20 12:00:00 -0700981
Adam Langleyfcf25832014-12-18 17:42:32 -0800982int SSL_renegotiate(SSL *s) {
983 if (s->renegotiate == 0) {
984 s->renegotiate = 1;
985 }
Adam Langley95c29f32014-06-20 12:00:00 -0700986
Adam Langleyfcf25832014-12-18 17:42:32 -0800987 s->new_session = 1;
988 return s->method->ssl_renegotiate(s);
989}
Adam Langley95c29f32014-06-20 12:00:00 -0700990
Adam Langleyfcf25832014-12-18 17:42:32 -0800991int SSL_renegotiate_abbreviated(SSL *s) {
992 if (s->renegotiate == 0) {
993 s->renegotiate = 1;
994 }
Adam Langley95c29f32014-06-20 12:00:00 -0700995
Adam Langleyfcf25832014-12-18 17:42:32 -0800996 s->new_session = 0;
997 return s->method->ssl_renegotiate(s);
998}
Adam Langley95c29f32014-06-20 12:00:00 -0700999
Adam Langleyfcf25832014-12-18 17:42:32 -08001000int SSL_renegotiate_pending(SSL *s) {
1001 /* becomes true when negotiation is requested; false again once a handshake
1002 * has finished */
1003 return s->renegotiate != 0;
1004}
Adam Langley95c29f32014-06-20 12:00:00 -07001005
Adam Langleyfcf25832014-12-18 17:42:32 -08001006long SSL_ctrl(SSL *s, int cmd, long larg, void *parg) {
1007 long l;
Adam Langley95c29f32014-06-20 12:00:00 -07001008
Adam Langleyfcf25832014-12-18 17:42:32 -08001009 switch (cmd) {
1010 case SSL_CTRL_GET_READ_AHEAD:
1011 return s->read_ahead;
Adam Langley95c29f32014-06-20 12:00:00 -07001012
Adam Langleyfcf25832014-12-18 17:42:32 -08001013 case SSL_CTRL_SET_READ_AHEAD:
1014 l = s->read_ahead;
1015 s->read_ahead = larg;
1016 return l;
Adam Langley95c29f32014-06-20 12:00:00 -07001017
Adam Langleyfcf25832014-12-18 17:42:32 -08001018 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1019 s->msg_callback_arg = parg;
1020 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001021
Adam Langleyfcf25832014-12-18 17:42:32 -08001022 case SSL_CTRL_OPTIONS:
1023 return s->options |= larg;
Adam Langley95c29f32014-06-20 12:00:00 -07001024
Adam Langleyfcf25832014-12-18 17:42:32 -08001025 case SSL_CTRL_CLEAR_OPTIONS:
1026 return s->options &= ~larg;
Adam Langley95c29f32014-06-20 12:00:00 -07001027
Adam Langleyfcf25832014-12-18 17:42:32 -08001028 case SSL_CTRL_MODE:
1029 return s->mode |= larg;
Adam Langley95c29f32014-06-20 12:00:00 -07001030
Adam Langleyfcf25832014-12-18 17:42:32 -08001031 case SSL_CTRL_CLEAR_MODE:
1032 return s->mode &= ~larg;
Adam Langley95c29f32014-06-20 12:00:00 -07001033
Adam Langleyfcf25832014-12-18 17:42:32 -08001034 case SSL_CTRL_GET_MAX_CERT_LIST:
1035 return s->max_cert_list;
Adam Langley95c29f32014-06-20 12:00:00 -07001036
Adam Langleyfcf25832014-12-18 17:42:32 -08001037 case SSL_CTRL_SET_MAX_CERT_LIST:
1038 l = s->max_cert_list;
1039 s->max_cert_list = larg;
1040 return l;
Adam Langley95c29f32014-06-20 12:00:00 -07001041
Adam Langleyfcf25832014-12-18 17:42:32 -08001042 case SSL_CTRL_SET_MTU:
1043 if (larg < (long)dtls1_min_mtu()) {
1044 return 0;
1045 }
1046 if (SSL_IS_DTLS(s)) {
1047 s->d1->mtu = larg;
1048 return larg;
1049 }
1050 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001051
Adam Langleyfcf25832014-12-18 17:42:32 -08001052 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1053 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH) {
1054 return 0;
1055 }
1056 s->max_send_fragment = larg;
1057 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001058
Adam Langleyfcf25832014-12-18 17:42:32 -08001059 case SSL_CTRL_GET_RI_SUPPORT:
1060 if (s->s3) {
1061 return s->s3->send_connection_binding;
1062 }
1063 return 0;
1064
1065 case SSL_CTRL_CERT_FLAGS:
1066 return s->cert->cert_flags |= larg;
1067
1068 case SSL_CTRL_CLEAR_CERT_FLAGS:
1069 return s->cert->cert_flags &= ~larg;
1070
1071 case SSL_CTRL_GET_RAW_CIPHERLIST:
1072 if (parg) {
1073 if (s->cert->ciphers_raw == NULL) {
1074 return 0;
1075 }
1076 *(uint8_t **)parg = s->cert->ciphers_raw;
1077 return (int)s->cert->ciphers_rawlen;
1078 }
1079
1080 /* Passing a NULL |parg| returns the size of a single
1081 * cipher suite value. */
1082 return 2;
1083
1084 default:
1085 return s->method->ssl_ctrl(s, cmd, larg, parg);
1086 }
1087}
1088
1089long SSL_callback_ctrl(SSL *s, int cmd, void (*fp)(void)) {
1090 switch (cmd) {
1091 case SSL_CTRL_SET_MSG_CALLBACK:
1092 s->msg_callback =
1093 (void (*)(int write_p, int version, int content_type, const void *buf,
1094 size_t len, SSL *ssl, void *arg))(fp);
1095 return 1;
1096
1097 default:
1098 return s->method->ssl_callback_ctrl(s, cmd, fp);
1099 }
1100}
1101
1102LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) { return ctx->sessions; }
1103
1104long SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg) {
1105 long l;
1106
1107 switch (cmd) {
1108 case SSL_CTRL_GET_READ_AHEAD:
1109 return ctx->read_ahead;
1110
1111 case SSL_CTRL_SET_READ_AHEAD:
1112 l = ctx->read_ahead;
1113 ctx->read_ahead = larg;
1114 return l;
1115
1116 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1117 ctx->msg_callback_arg = parg;
1118 return 1;
1119
1120 case SSL_CTRL_GET_MAX_CERT_LIST:
1121 return ctx->max_cert_list;
1122
1123 case SSL_CTRL_SET_MAX_CERT_LIST:
1124 l = ctx->max_cert_list;
1125 ctx->max_cert_list = larg;
1126 return l;
1127
1128 case SSL_CTRL_SET_SESS_CACHE_SIZE:
1129 l = ctx->session_cache_size;
1130 ctx->session_cache_size = larg;
1131 return l;
1132
1133 case SSL_CTRL_GET_SESS_CACHE_SIZE:
1134 return ctx->session_cache_size;
1135
1136 case SSL_CTRL_SET_SESS_CACHE_MODE:
1137 l = ctx->session_cache_mode;
1138 ctx->session_cache_mode = larg;
1139 return l;
1140
1141 case SSL_CTRL_GET_SESS_CACHE_MODE:
1142 return ctx->session_cache_mode;
1143
1144 case SSL_CTRL_SESS_NUMBER:
1145 return lh_SSL_SESSION_num_items(ctx->sessions);
1146
1147 case SSL_CTRL_SESS_CONNECT:
1148 return ctx->stats.sess_connect;
1149
1150 case SSL_CTRL_SESS_CONNECT_GOOD:
1151 return ctx->stats.sess_connect_good;
1152
1153 case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
1154 return ctx->stats.sess_connect_renegotiate;
1155
1156 case SSL_CTRL_SESS_ACCEPT:
1157 return ctx->stats.sess_accept;
1158
1159 case SSL_CTRL_SESS_ACCEPT_GOOD:
1160 return ctx->stats.sess_accept_good;
1161
1162 case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
1163 return ctx->stats.sess_accept_renegotiate;
1164
1165 case SSL_CTRL_SESS_HIT:
1166 return ctx->stats.sess_hit;
1167
1168 case SSL_CTRL_SESS_CB_HIT:
1169 return ctx->stats.sess_cb_hit;
1170
1171 case SSL_CTRL_SESS_MISSES:
1172 return ctx->stats.sess_miss;
1173
1174 case SSL_CTRL_SESS_TIMEOUTS:
1175 return ctx->stats.sess_timeout;
1176
1177 case SSL_CTRL_SESS_CACHE_FULL:
1178 return ctx->stats.sess_cache_full;
1179
1180 case SSL_CTRL_OPTIONS:
1181 return ctx->options |= larg;
1182
1183 case SSL_CTRL_CLEAR_OPTIONS:
1184 return ctx->options &= ~larg;
1185
1186 case SSL_CTRL_MODE:
1187 return ctx->mode |= larg;
1188
1189 case SSL_CTRL_CLEAR_MODE:
1190 return ctx->mode &= ~larg;
1191
1192 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1193 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH) {
1194 return 0;
1195 }
1196 ctx->max_send_fragment = larg;
1197 return 1;
1198
1199 case SSL_CTRL_CERT_FLAGS:
1200 return ctx->cert->cert_flags |= larg;
1201
1202 case SSL_CTRL_CLEAR_CERT_FLAGS:
1203 return ctx->cert->cert_flags &= ~larg;
1204
1205 default:
1206 return ctx->method->ssl_ctx_ctrl(ctx, cmd, larg, parg);
1207 }
1208}
1209
1210long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void)) {
1211 switch (cmd) {
1212 case SSL_CTRL_SET_MSG_CALLBACK:
1213 ctx->msg_callback =
1214 (void (*)(int write_p, int version, int content_type, const void *buf,
1215 size_t len, SSL *ssl, void *arg))(fp);
1216 return 1;
1217
1218 default:
1219 return ctx->method->ssl_ctx_callback_ctrl(ctx, cmd, fp);
1220 }
1221}
1222
1223int ssl_cipher_id_cmp(const void *in_a, const void *in_b) {
1224 long l;
1225 const SSL_CIPHER *a = in_a;
1226 const SSL_CIPHER *b = in_b;
1227 const long a_id = a->id;
1228 const long b_id = b->id;
1229
1230 l = a_id - b_id;
1231 if (l == 0L) {
1232 return 0;
1233 } else {
1234 return (l > 0) ? 1 : -1;
1235 }
1236}
1237
1238int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **ap, const SSL_CIPHER **bp) {
1239 long l;
1240 const long a_id = (*ap)->id;
1241 const long b_id = (*bp)->id;
1242
1243 l = a_id - b_id;
1244 if (l == 0) {
1245 return 0;
1246 } else {
1247 return (l > 0) ? 1 : -1;
1248 }
1249}
1250
1251/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001252 * preference */
Adam Langleyfcf25832014-12-18 17:42:32 -08001253STACK_OF(SSL_CIPHER) * SSL_get_ciphers(const SSL *s) {
1254 if (s == NULL) {
1255 return NULL;
1256 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001257
Adam Langleyfcf25832014-12-18 17:42:32 -08001258 if (s->cipher_list != NULL) {
1259 return s->cipher_list->ciphers;
1260 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001261
Adam Langleyfcf25832014-12-18 17:42:32 -08001262 if (s->version >= TLS1_1_VERSION && s->ctx != NULL &&
1263 s->ctx->cipher_list_tls11 != NULL) {
1264 return s->ctx->cipher_list_tls11->ciphers;
1265 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001266
Adam Langleyfcf25832014-12-18 17:42:32 -08001267 if (s->ctx != NULL && s->ctx->cipher_list != NULL) {
1268 return s->ctx->cipher_list->ciphers;
1269 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001270
Adam Langleyfcf25832014-12-18 17:42:32 -08001271 return NULL;
1272}
Adam Langley95c29f32014-06-20 12:00:00 -07001273
Adam Langleyfcf25832014-12-18 17:42:32 -08001274/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001275 * algorithm id */
Adam Langleyfcf25832014-12-18 17:42:32 -08001276STACK_OF(SSL_CIPHER) * ssl_get_ciphers_by_id(SSL *s) {
1277 if (s == NULL) {
1278 return NULL;
1279 }
Adam Langley95c29f32014-06-20 12:00:00 -07001280
Adam Langleyfcf25832014-12-18 17:42:32 -08001281 if (s->cipher_list_by_id != NULL) {
1282 return s->cipher_list_by_id;
1283 }
Adam Langley95c29f32014-06-20 12:00:00 -07001284
Adam Langleyfcf25832014-12-18 17:42:32 -08001285 if (s->ctx != NULL && s->ctx->cipher_list_by_id != NULL) {
1286 return s->ctx->cipher_list_by_id;
1287 }
Adam Langley95c29f32014-06-20 12:00:00 -07001288
Adam Langleyfcf25832014-12-18 17:42:32 -08001289 return NULL;
1290}
Adam Langley95c29f32014-06-20 12:00:00 -07001291
Adam Langleyfcf25832014-12-18 17:42:32 -08001292/* The old interface to get the same thing as SSL_get_ciphers() */
1293const char *SSL_get_cipher_list(const SSL *s, int n) {
1294 const SSL_CIPHER *c;
1295 STACK_OF(SSL_CIPHER) * sk;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001296
Adam Langleyfcf25832014-12-18 17:42:32 -08001297 if (s == NULL) {
1298 return NULL;
1299 }
Adam Langley95c29f32014-06-20 12:00:00 -07001300
Adam Langleyfcf25832014-12-18 17:42:32 -08001301 sk = SSL_get_ciphers(s);
1302 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
1303 return NULL;
1304 }
Adam Langley95c29f32014-06-20 12:00:00 -07001305
Adam Langleyfcf25832014-12-18 17:42:32 -08001306 c = sk_SSL_CIPHER_value(sk, n);
1307 if (c == NULL) {
1308 return NULL;
1309 }
Adam Langley95c29f32014-06-20 12:00:00 -07001310
Adam Langleyfcf25832014-12-18 17:42:32 -08001311 return c->name;
1312}
David Benjamin5491e3f2014-09-29 19:33:09 -04001313
Adam Langleyfcf25832014-12-18 17:42:32 -08001314/* specify the ciphers to be used by default by the SSL_CTX */
1315int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
1316 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001317
Adam Langleyfcf25832014-12-18 17:42:32 -08001318 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list,
1319 &ctx->cipher_list_by_id, str, ctx->cert);
1320 /* ssl_create_cipher_list may return an empty stack if it was unable to find
1321 * a cipher matching the given rule string (for example if the rule string
1322 * specifies a cipher which has been disabled). This is not an error as far
1323 * as ssl_create_cipher_list is concerned, and hence ctx->cipher_list and
1324 * ctx->cipher_list_by_id has been updated. */
1325 if (sk == NULL) {
1326 return 0;
1327 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1328 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1329 return 0;
1330 }
Adam Langley95c29f32014-06-20 12:00:00 -07001331
Adam Langleyfcf25832014-12-18 17:42:32 -08001332 return 1;
1333}
David Benjamin39482a12014-07-20 13:30:15 -04001334
Adam Langleyfcf25832014-12-18 17:42:32 -08001335int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str) {
1336 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001337
Adam Langleyfcf25832014-12-18 17:42:32 -08001338 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str,
1339 ctx->cert);
1340 if (sk == NULL) {
1341 return 0;
1342 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1343 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list_tls11,
1344 SSL_R_NO_CIPHER_MATCH);
1345 return 0;
1346 }
David Benjamin9f2c0d72014-10-21 22:00:19 -04001347
Adam Langleyfcf25832014-12-18 17:42:32 -08001348 return 1;
1349}
Adam Langley95c29f32014-06-20 12:00:00 -07001350
Adam Langleyfcf25832014-12-18 17:42:32 -08001351/* specify the ciphers to be used by the SSL */
1352int SSL_set_cipher_list(SSL *s, const char *str) {
1353 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001354
Adam Langleyfcf25832014-12-18 17:42:32 -08001355 sk = ssl_create_cipher_list(s->ctx->method, &s->cipher_list,
1356 &s->cipher_list_by_id, str, s->cert);
David Benjamin39482a12014-07-20 13:30:15 -04001357
Adam Langleyfcf25832014-12-18 17:42:32 -08001358 /* see comment in SSL_CTX_set_cipher_list */
1359 if (sk == NULL) {
1360 return 0;
1361 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1362 OPENSSL_PUT_ERROR(SSL, SSL_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1363 return 0;
1364 }
David Benjamin39482a12014-07-20 13:30:15 -04001365
Adam Langleyfcf25832014-12-18 17:42:32 -08001366 return 1;
1367}
Adam Langley95c29f32014-06-20 12:00:00 -07001368
Adam Langleyfcf25832014-12-18 17:42:32 -08001369int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, uint8_t *p) {
1370 size_t i;
1371 const SSL_CIPHER *c;
1372 CERT *ct = s->cert;
1373 uint8_t *q;
1374 /* Set disabled masks for this session */
1375 ssl_set_client_disabled(s);
Adam Langley29707792014-06-20 12:00:00 -07001376
Adam Langleyfcf25832014-12-18 17:42:32 -08001377 if (sk == NULL) {
1378 return 0;
1379 }
1380 q = p;
Adam Langley95c29f32014-06-20 12:00:00 -07001381
Adam Langleyfcf25832014-12-18 17:42:32 -08001382 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
1383 c = sk_SSL_CIPHER_value(sk, i);
1384 /* Skip disabled ciphers */
1385 if (c->algorithm_ssl & ct->mask_ssl ||
1386 c->algorithm_mkey & ct->mask_k ||
1387 c->algorithm_auth & ct->mask_a) {
1388 continue;
1389 }
1390 s2n(ssl3_get_cipher_value(c), p);
1391 }
1392
1393 /* If all ciphers were disabled, return the error to the caller. */
1394 if (p == q) {
1395 return 0;
1396 }
1397
1398 /* Add SCSVs. */
1399 if (!s->renegotiate) {
1400 s2n(SSL3_CK_SCSV & 0xffff, p);
1401 }
1402
1403 if (s->fallback_scsv) {
1404 s2n(SSL3_CK_FALLBACK_SCSV & 0xffff, p);
1405 }
1406
1407 return p - q;
1408}
1409
1410STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs) {
1411 CBS cipher_suites = *cbs;
1412 const SSL_CIPHER *c;
1413 STACK_OF(SSL_CIPHER) * sk;
1414
1415 if (s->s3) {
1416 s->s3->send_connection_binding = 0;
1417 }
1418
1419 if (CBS_len(&cipher_suites) % 2 != 0) {
1420 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1421 SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
1422 return NULL;
1423 }
1424
1425 sk = sk_SSL_CIPHER_new_null();
1426 if (sk == NULL) {
1427 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1428 goto err;
1429 }
1430
1431 if (!CBS_stow(&cipher_suites, &s->cert->ciphers_raw,
1432 &s->cert->ciphers_rawlen)) {
1433 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1434 goto err;
1435 }
1436
1437 while (CBS_len(&cipher_suites) > 0) {
1438 uint16_t cipher_suite;
1439
1440 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
1441 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_INTERNAL_ERROR);
1442 goto err;
1443 }
1444
1445 /* Check for SCSV. */
1446 if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff)) {
1447 /* SCSV is fatal if renegotiating. */
1448 if (s->renegotiate) {
1449 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1450 SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1451 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1452 goto err;
1453 }
1454 s->s3->send_connection_binding = 1;
1455 continue;
1456 }
1457
1458 /* Check for FALLBACK_SCSV. */
1459 if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff)) {
1460 uint16_t max_version = ssl3_get_max_server_version(s);
1461 if (SSL_IS_DTLS(s) ? (uint16_t)s->version > max_version
1462 : (uint16_t)s->version < max_version) {
1463 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1464 SSL_R_INAPPROPRIATE_FALLBACK);
1465 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
1466 goto err;
1467 }
1468 continue;
1469 }
1470
1471 c = ssl3_get_cipher_by_value(cipher_suite);
1472 if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
1473 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1474 goto err;
1475 }
1476 }
1477
1478 return sk;
David Benjamin9f2c0d72014-10-21 22:00:19 -04001479
Adam Langley95c29f32014-06-20 12:00:00 -07001480err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001481 if (sk != NULL) {
1482 sk_SSL_CIPHER_free(sk);
1483 }
1484 return NULL;
1485}
Adam Langley95c29f32014-06-20 12:00:00 -07001486
1487
Adam Langleyfcf25832014-12-18 17:42:32 -08001488/* return a servername extension value if provided in Client Hello, or NULL. So
1489 * far, only host_name types are defined (RFC 3546). */
1490const char *SSL_get_servername(const SSL *s, const int type) {
1491 if (type != TLSEXT_NAMETYPE_host_name) {
1492 return NULL;
1493 }
Adam Langley95c29f32014-06-20 12:00:00 -07001494
Adam Langleyfcf25832014-12-18 17:42:32 -08001495 return s->session && !s->tlsext_hostname ? s->session->tlsext_hostname
1496 : s->tlsext_hostname;
1497}
Adam Langley95c29f32014-06-20 12:00:00 -07001498
Adam Langleyfcf25832014-12-18 17:42:32 -08001499int SSL_get_servername_type(const SSL *s) {
1500 if (s->session &&
1501 (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname)) {
1502 return TLSEXT_NAMETYPE_host_name;
1503 }
Adam Langley95c29f32014-06-20 12:00:00 -07001504
Adam Langleyfcf25832014-12-18 17:42:32 -08001505 return -1;
1506}
Adam Langley95c29f32014-06-20 12:00:00 -07001507
Adam Langleyfcf25832014-12-18 17:42:32 -08001508void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
1509 ctx->signed_cert_timestamps_enabled = 1;
1510}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001511
Adam Langleyfcf25832014-12-18 17:42:32 -08001512int SSL_enable_signed_cert_timestamps(SSL *ssl) {
1513 ssl->signed_cert_timestamps_enabled = 1;
1514 return 1;
1515}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001516
Adam Langleyfcf25832014-12-18 17:42:32 -08001517void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
1518 ctx->ocsp_stapling_enabled = 1;
1519}
David Benjamin6c7aed02014-08-27 16:42:38 -04001520
Adam Langleyfcf25832014-12-18 17:42:32 -08001521int SSL_enable_ocsp_stapling(SSL *ssl) {
1522 ssl->ocsp_stapling_enabled = 1;
1523 return 1;
1524}
David Benjamin6c7aed02014-08-27 16:42:38 -04001525
Adam Langleyfcf25832014-12-18 17:42:32 -08001526void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
1527 size_t *out_len) {
1528 SSL_SESSION *session = ssl->session;
Adam Langley3cb50e02014-08-26 14:00:31 -07001529
Adam Langleyfcf25832014-12-18 17:42:32 -08001530 *out_len = 0;
1531 *out = NULL;
1532 if (ssl->server || !session || !session->tlsext_signed_cert_timestamp_list) {
1533 return;
1534 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001535
Adam Langleyfcf25832014-12-18 17:42:32 -08001536 *out = session->tlsext_signed_cert_timestamp_list;
1537 *out_len = session->tlsext_signed_cert_timestamp_list_length;
1538}
David Benjamin6c7aed02014-08-27 16:42:38 -04001539
Adam Langleyfcf25832014-12-18 17:42:32 -08001540void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
1541 size_t *out_len) {
1542 SSL_SESSION *session = ssl->session;
1543
1544 *out_len = 0;
1545 *out = NULL;
1546 if (ssl->server || !session || !session->ocsp_response) {
1547 return;
1548 }
1549 *out = session->ocsp_response;
1550 *out_len = session->ocsp_response_length;
1551}
David Benjamin6c7aed02014-08-27 16:42:38 -04001552
Adam Langley95c29f32014-06-20 12:00:00 -07001553/* SSL_select_next_proto implements the standard protocol selection. It is
1554 * expected that this function is called from the callback set by
1555 * SSL_CTX_set_next_proto_select_cb.
1556 *
1557 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1558 * strings. The length byte itself is not included in the length. A byte
1559 * string of length 0 is invalid. No byte string may be truncated.
1560 *
1561 * The current, but experimental algorithm for selecting the protocol is:
1562 *
1563 * 1) If the server doesn't support NPN then this is indicated to the
1564 * callback. In this case, the client application has to abort the connection
1565 * or have a default application level protocol.
1566 *
1567 * 2) If the server supports NPN, but advertises an empty list then the
1568 * client selects the first protcol in its list, but indicates via the
1569 * API that this fallback case was enacted.
1570 *
1571 * 3) Otherwise, the client finds the first protocol in the server's list
1572 * that it supports and selects this protocol. This is because it's
1573 * assumed that the server has better information about which protocol
1574 * a client should use.
1575 *
1576 * 4) If the client doesn't support any of the server's advertised
1577 * protocols, then this is treated the same as case 2.
1578 *
1579 * It returns either
1580 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1581 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1582 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001583int SSL_select_next_proto(uint8_t **out, uint8_t *outlen, const uint8_t *server,
1584 unsigned int server_len, const uint8_t *client,
1585 unsigned int client_len) {
1586 unsigned int i, j;
1587 const uint8_t *result;
1588 int status = OPENSSL_NPN_UNSUPPORTED;
Adam Langley95c29f32014-06-20 12:00:00 -07001589
Adam Langleyfcf25832014-12-18 17:42:32 -08001590 /* For each protocol in server preference order, see if we support it. */
1591 for (i = 0; i < server_len;) {
1592 for (j = 0; j < client_len;) {
1593 if (server[i] == client[j] &&
1594 memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
1595 /* We found a match */
1596 result = &server[i];
1597 status = OPENSSL_NPN_NEGOTIATED;
1598 goto found;
1599 }
1600 j += client[j];
1601 j++;
1602 }
1603 i += server[i];
1604 i++;
1605 }
Adam Langley95c29f32014-06-20 12:00:00 -07001606
Adam Langleyfcf25832014-12-18 17:42:32 -08001607 /* There's no overlap between our protocols and the server's list. */
1608 result = client;
1609 status = OPENSSL_NPN_NO_OVERLAP;
Adam Langley95c29f32014-06-20 12:00:00 -07001610
Adam Langleyfcf25832014-12-18 17:42:32 -08001611found:
1612 *out = (uint8_t *)result + 1;
1613 *outlen = result[0];
1614 return status;
1615}
Adam Langley95c29f32014-06-20 12:00:00 -07001616
Adam Langley95c29f32014-06-20 12:00:00 -07001617/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
1618 * requested protocol for this connection and returns 0. If the client didn't
1619 * request any protocol, then *data is set to NULL.
1620 *
1621 * Note that the client can request any protocol it chooses. The value returned
1622 * from this function need not be a member of the list of supported protocols
Adam Langleyfcf25832014-12-18 17:42:32 -08001623 * provided by the callback. */
1624void SSL_get0_next_proto_negotiated(const SSL *s, const uint8_t **data,
1625 unsigned *len) {
1626 *data = s->next_proto_negotiated;
1627 if (!*data) {
1628 *len = 0;
1629 } else {
1630 *len = s->next_proto_negotiated_len;
1631 }
Adam Langley95c29f32014-06-20 12:00:00 -07001632}
1633
1634/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
1635 * TLS server needs a list of supported protocols for Next Protocol
1636 * Negotiation. The returned list must be in wire format. The list is returned
1637 * by setting |out| to point to it and |outlen| to its length. This memory will
1638 * not be modified, but one should assume that the SSL* keeps a reference to
1639 * it.
1640 *
Adam Langleyfcf25832014-12-18 17:42:32 -08001641 * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise.
1642 * Otherwise, no such extension will be included in the ServerHello. */
1643void SSL_CTX_set_next_protos_advertised_cb(
1644 SSL_CTX *ctx,
1645 int (*cb)(SSL *ssl, const uint8_t **out, unsigned int *outlen, void *arg),
1646 void *arg) {
1647 ctx->next_protos_advertised_cb = cb;
1648 ctx->next_protos_advertised_cb_arg = arg;
1649}
Adam Langley95c29f32014-06-20 12:00:00 -07001650
1651/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1652 * client needs to select a protocol from the server's provided list. |out|
1653 * must be set to point to the selected protocol (which may be within |in|).
1654 * The length of the protocol name must be written into |outlen|. The server's
1655 * advertised protocols are provided in |in| and |inlen|. The callback can
1656 * assume that |in| is syntactically valid.
1657 *
1658 * The client must select a protocol. It is fatal to the connection if this
1659 * callback returns a value other than SSL_TLSEXT_ERR_OK.
1660 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001661void SSL_CTX_set_next_proto_select_cb(
1662 SSL_CTX *ctx, int (*cb)(SSL *s, uint8_t **out, uint8_t *outlen,
1663 const uint8_t *in, unsigned int inlen, void *arg),
1664 void *arg) {
1665 ctx->next_proto_select_cb = cb;
1666 ctx->next_proto_select_cb_arg = arg;
1667}
Adam Langley95c29f32014-06-20 12:00:00 -07001668
Adam Langley95c29f32014-06-20 12:00:00 -07001669/* SSL_CTX_set_alpn_protos sets the ALPN protocol list on |ctx| to |protos|.
1670 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
1671 * length-prefixed strings).
1672 *
1673 * Returns 0 on success. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001674int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
1675 unsigned protos_len) {
1676 if (ctx->alpn_client_proto_list) {
1677 OPENSSL_free(ctx->alpn_client_proto_list);
1678 }
Adam Langley95c29f32014-06-20 12:00:00 -07001679
Adam Langleyfcf25832014-12-18 17:42:32 -08001680 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1681 if (!ctx->alpn_client_proto_list) {
1682 return 1;
1683 }
1684 ctx->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001685
Adam Langleyfcf25832014-12-18 17:42:32 -08001686 return 0;
1687}
Adam Langley95c29f32014-06-20 12:00:00 -07001688
1689/* SSL_set_alpn_protos sets the ALPN protocol list on |ssl| to |protos|.
1690 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
1691 * length-prefixed strings).
1692 *
1693 * Returns 0 on success. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001694int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
1695 if (ssl->alpn_client_proto_list) {
1696 OPENSSL_free(ssl->alpn_client_proto_list);
1697 }
Adam Langley95c29f32014-06-20 12:00:00 -07001698
Adam Langleyfcf25832014-12-18 17:42:32 -08001699 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1700 if (!ssl->alpn_client_proto_list) {
1701 return 1;
1702 }
1703 ssl->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001704
Adam Langleyfcf25832014-12-18 17:42:32 -08001705 return 0;
1706}
Adam Langley95c29f32014-06-20 12:00:00 -07001707
1708/* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
1709 * during ClientHello processing in order to select an ALPN protocol from the
1710 * client's list of offered protocols. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001711void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
1712 int (*cb)(SSL *ssl, const uint8_t **out,
1713 uint8_t *outlen, const uint8_t *in,
1714 unsigned int inlen, void *arg),
1715 void *arg) {
1716 ctx->alpn_select_cb = cb;
1717 ctx->alpn_select_cb_arg = arg;
1718}
Adam Langley95c29f32014-06-20 12:00:00 -07001719
1720/* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
1721 * On return it sets |*data| to point to |*len| bytes of protocol name (not
1722 * including the leading length-prefix byte). If the server didn't respond with
1723 * a negotiated protocol then |*len| will be zero. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001724void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **data,
1725 unsigned *len) {
1726 *data = NULL;
1727 if (ssl->s3) {
1728 *data = ssl->s3->alpn_selected;
1729 }
1730 if (*data == NULL) {
1731 *len = 0;
1732 } else {
1733 *len = ssl->s3->alpn_selected_len;
1734 }
1735}
Adam Langley95c29f32014-06-20 12:00:00 -07001736
Adam Langleyfcf25832014-12-18 17:42:32 -08001737int SSL_export_keying_material(SSL *s, uint8_t *out, size_t olen,
1738 const char *label, size_t llen, const uint8_t *p,
1739 size_t plen, int use_context) {
1740 if (s->version < TLS1_VERSION) {
1741 return -1;
1742 }
Adam Langley95c29f32014-06-20 12:00:00 -07001743
Adam Langleyfcf25832014-12-18 17:42:32 -08001744 return s->enc_method->export_keying_material(s, out, olen, label, llen, p,
1745 plen, use_context);
1746}
Adam Langley95c29f32014-06-20 12:00:00 -07001747
Adam Langleyfcf25832014-12-18 17:42:32 -08001748static uint32_t ssl_session_hash(const SSL_SESSION *a) {
1749 uint32_t hash =
1750 ((uint32_t)a->session_id[0]) ||
1751 ((uint32_t)a->session_id[1] << 8) ||
1752 ((uint32_t)a->session_id[2] << 16) ||
1753 ((uint32_t)a->session_id[3] << 24);
Adam Langley95c29f32014-06-20 12:00:00 -07001754
Adam Langleyfcf25832014-12-18 17:42:32 -08001755 return hash;
1756}
Adam Langley95c29f32014-06-20 12:00:00 -07001757
1758/* NB: If this function (or indeed the hash function which uses a sort of
1759 * coarser function than this one) is changed, ensure
1760 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1761 * able to construct an SSL_SESSION that will collide with any existing session
1762 * with a matching session ID. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001763static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
1764 if (a->ssl_version != b->ssl_version) {
1765 return 1;
1766 }
Adam Langley95c29f32014-06-20 12:00:00 -07001767
Adam Langleyfcf25832014-12-18 17:42:32 -08001768 if (a->session_id_length != b->session_id_length) {
1769 return 1;
1770 }
Adam Langley95c29f32014-06-20 12:00:00 -07001771
Adam Langleyfcf25832014-12-18 17:42:32 -08001772 return memcmp(a->session_id, b->session_id, a->session_id_length);
1773}
Adam Langley95c29f32014-06-20 12:00:00 -07001774
Adam Langleyfcf25832014-12-18 17:42:32 -08001775SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth) {
1776 SSL_CTX *ret = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001777
Adam Langleyfcf25832014-12-18 17:42:32 -08001778 if (meth == NULL) {
1779 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_NULL_SSL_METHOD_PASSED);
1780 return NULL;
1781 }
Adam Langley95c29f32014-06-20 12:00:00 -07001782
Adam Langleyfcf25832014-12-18 17:42:32 -08001783 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
1784 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
1785 goto err;
1786 }
Adam Langley95c29f32014-06-20 12:00:00 -07001787
Adam Langleyfcf25832014-12-18 17:42:32 -08001788 ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1789 if (ret == NULL) {
1790 goto err;
1791 }
Adam Langley95c29f32014-06-20 12:00:00 -07001792
Adam Langleyfcf25832014-12-18 17:42:32 -08001793 memset(ret, 0, sizeof(SSL_CTX));
Adam Langley95c29f32014-06-20 12:00:00 -07001794
Adam Langleyfcf25832014-12-18 17:42:32 -08001795 ret->method = meth->method;
Adam Langley95c29f32014-06-20 12:00:00 -07001796
Adam Langleyfcf25832014-12-18 17:42:32 -08001797 ret->cert_store = NULL;
1798 ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
1799 ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1800 ret->session_cache_head = NULL;
1801 ret->session_cache_tail = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001802
Adam Langleyfcf25832014-12-18 17:42:32 -08001803 /* We take the system default */
1804 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001805
Adam Langleyfcf25832014-12-18 17:42:32 -08001806 ret->new_session_cb = 0;
1807 ret->remove_session_cb = 0;
1808 ret->get_session_cb = 0;
1809 ret->generate_session_id = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001810
Adam Langleyfcf25832014-12-18 17:42:32 -08001811 memset((char *)&ret->stats, 0, sizeof(ret->stats));
Adam Langley95c29f32014-06-20 12:00:00 -07001812
Adam Langleyfcf25832014-12-18 17:42:32 -08001813 ret->references = 1;
1814 ret->quiet_shutdown = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001815
Adam Langleyfcf25832014-12-18 17:42:32 -08001816 ret->info_callback = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001817
Adam Langleyfcf25832014-12-18 17:42:32 -08001818 ret->app_verify_callback = 0;
1819 ret->app_verify_arg = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001820
Adam Langleyfcf25832014-12-18 17:42:32 -08001821 ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
1822 ret->read_ahead = 0;
1823 ret->msg_callback = 0;
1824 ret->msg_callback_arg = NULL;
1825 ret->verify_mode = SSL_VERIFY_NONE;
1826 ret->sid_ctx_length = 0;
1827 ret->default_verify_callback = NULL;
1828 ret->cert = ssl_cert_new();
1829 if (ret->cert == NULL) {
1830 goto err;
1831 }
Adam Langley95c29f32014-06-20 12:00:00 -07001832
Adam Langleyfcf25832014-12-18 17:42:32 -08001833 ret->default_passwd_callback = 0;
1834 ret->default_passwd_callback_userdata = NULL;
1835 ret->client_cert_cb = 0;
1836 ret->app_gen_cookie_cb = 0;
1837 ret->app_verify_cookie_cb = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001838
Adam Langleyfcf25832014-12-18 17:42:32 -08001839 ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
1840 if (ret->sessions == NULL) {
1841 goto err;
1842 }
1843 ret->cert_store = X509_STORE_new();
1844 if (ret->cert_store == NULL) {
1845 goto err;
1846 }
Adam Langley95c29f32014-06-20 12:00:00 -07001847
Adam Langleyfcf25832014-12-18 17:42:32 -08001848 ssl_create_cipher_list(ret->method, &ret->cipher_list,
1849 &ret->cipher_list_by_id, SSL_DEFAULT_CIPHER_LIST,
1850 ret->cert);
1851 if (ret->cipher_list == NULL ||
1852 sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0) {
1853 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_LIBRARY_HAS_NO_CIPHERS);
1854 goto err2;
1855 }
Adam Langley95c29f32014-06-20 12:00:00 -07001856
Adam Langleyfcf25832014-12-18 17:42:32 -08001857 ret->param = X509_VERIFY_PARAM_new();
1858 if (!ret->param) {
1859 goto err;
1860 }
Adam Langley95c29f32014-06-20 12:00:00 -07001861
Adam Langleyfcf25832014-12-18 17:42:32 -08001862 ret->client_CA = sk_X509_NAME_new_null();
1863 if (ret->client_CA == NULL) {
1864 goto err;
1865 }
Adam Langley95c29f32014-06-20 12:00:00 -07001866
Adam Langleyfcf25832014-12-18 17:42:32 -08001867 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001868
Adam Langleyfcf25832014-12-18 17:42:32 -08001869 ret->extra_certs = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001870
Adam Langleyfcf25832014-12-18 17:42:32 -08001871 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
David Benjamin422d3a42014-08-20 11:09:03 -04001872
Adam Langleyfcf25832014-12-18 17:42:32 -08001873 ret->tlsext_servername_callback = 0;
1874 ret->tlsext_servername_arg = NULL;
1875 /* Setup RFC4507 ticket keys */
1876 if (!RAND_bytes(ret->tlsext_tick_key_name, 16) ||
1877 !RAND_bytes(ret->tlsext_tick_hmac_key, 16) ||
1878 !RAND_bytes(ret->tlsext_tick_aes_key, 16)) {
1879 ret->options |= SSL_OP_NO_TICKET;
1880 }
Adam Langley95c29f32014-06-20 12:00:00 -07001881
Adam Langleyfcf25832014-12-18 17:42:32 -08001882 ret->tlsext_status_cb = 0;
1883 ret->tlsext_status_arg = NULL;
David Benjamin82c9e902014-12-12 15:55:27 -05001884
Adam Langleyfcf25832014-12-18 17:42:32 -08001885 ret->next_protos_advertised_cb = 0;
1886 ret->next_proto_select_cb = 0;
1887 ret->psk_identity_hint = NULL;
1888 ret->psk_client_callback = NULL;
1889 ret->psk_server_callback = NULL;
1890
1891 /* Default is to connect to non-RI servers. When RI is more widely deployed
1892 * might change this. */
1893 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
1894
1895 /* Lock the SSL_CTX to the specified version, for compatibility with legacy
1896 * uses of SSL_METHOD. */
1897 if (meth->version != 0) {
1898 SSL_CTX_set_max_version(ret, meth->version);
1899 SSL_CTX_set_min_version(ret, meth->version);
1900 }
1901
1902 return ret;
1903
Adam Langley95c29f32014-06-20 12:00:00 -07001904err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001905 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -07001906err2:
Adam Langleyfcf25832014-12-18 17:42:32 -08001907 if (ret != NULL) {
1908 SSL_CTX_free(ret);
1909 }
1910 return NULL;
1911}
Adam Langley95c29f32014-06-20 12:00:00 -07001912
Adam Langleyfcf25832014-12-18 17:42:32 -08001913void SSL_CTX_free(SSL_CTX *a) {
1914 int i;
Adam Langley95c29f32014-06-20 12:00:00 -07001915
Adam Langleyfcf25832014-12-18 17:42:32 -08001916 if (a == NULL) {
1917 return;
1918 }
Adam Langley95c29f32014-06-20 12:00:00 -07001919
Adam Langleyfcf25832014-12-18 17:42:32 -08001920 i = CRYPTO_add(&a->references, -1, CRYPTO_LOCK_SSL_CTX);
1921 if (i > 0) {
1922 return;
1923 }
Adam Langley95c29f32014-06-20 12:00:00 -07001924
Adam Langleyfcf25832014-12-18 17:42:32 -08001925 if (a->param) {
1926 X509_VERIFY_PARAM_free(a->param);
1927 }
Adam Langley95c29f32014-06-20 12:00:00 -07001928
Adam Langleyfcf25832014-12-18 17:42:32 -08001929 /* Free internal session cache. However: the remove_cb() may reference the
1930 * ex_data of SSL_CTX, thus the ex_data store can only be removed after the
1931 * sessions were flushed. As the ex_data handling routines might also touch
1932 * the session cache, the most secure solution seems to be: empty (flush) the
1933 * cache, then free ex_data, then finally free the cache. (See ticket
1934 * [openssl.org #212].) */
1935 if (a->sessions != NULL) {
1936 SSL_CTX_flush_sessions(a, 0);
1937 }
Adam Langley95c29f32014-06-20 12:00:00 -07001938
Adam Langleyfcf25832014-12-18 17:42:32 -08001939 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001940
Adam Langleyfcf25832014-12-18 17:42:32 -08001941 if (a->sessions != NULL) {
1942 lh_SSL_SESSION_free(a->sessions);
1943 }
1944 if (a->cert_store != NULL) {
1945 X509_STORE_free(a->cert_store);
1946 }
1947 if (a->cipher_list != NULL) {
1948 ssl_cipher_preference_list_free(a->cipher_list);
1949 }
1950 if (a->cipher_list_by_id != NULL) {
1951 sk_SSL_CIPHER_free(a->cipher_list_by_id);
1952 }
1953 if (a->cipher_list_tls11 != NULL) {
1954 ssl_cipher_preference_list_free(a->cipher_list_tls11);
1955 }
1956 if (a->cert != NULL) {
1957 ssl_cert_free(a->cert);
1958 }
1959 if (a->client_CA != NULL) {
1960 sk_X509_NAME_pop_free(a->client_CA, X509_NAME_free);
1961 }
1962 if (a->extra_certs != NULL) {
1963 sk_X509_pop_free(a->extra_certs, X509_free);
1964 }
1965 if (a->srtp_profiles) {
1966 sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);
1967 }
1968 if (a->psk_identity_hint) {
1969 OPENSSL_free(a->psk_identity_hint);
1970 }
1971 if (a->tlsext_ecpointformatlist) {
1972 OPENSSL_free(a->tlsext_ecpointformatlist);
1973 }
1974 if (a->tlsext_ellipticcurvelist) {
1975 OPENSSL_free(a->tlsext_ellipticcurvelist);
1976 }
1977 if (a->alpn_client_proto_list != NULL) {
1978 OPENSSL_free(a->alpn_client_proto_list);
1979 }
1980 if (a->tlsext_channel_id_private) {
1981 EVP_PKEY_free(a->tlsext_channel_id_private);
1982 }
1983 if (a->keylog_bio) {
1984 BIO_free(a->keylog_bio);
1985 }
Adam Langley95c29f32014-06-20 12:00:00 -07001986
Adam Langleyfcf25832014-12-18 17:42:32 -08001987 OPENSSL_free(a);
1988}
Adam Langley95c29f32014-06-20 12:00:00 -07001989
Adam Langleyfcf25832014-12-18 17:42:32 -08001990void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) {
1991 ctx->default_passwd_callback = cb;
1992}
Adam Langley95c29f32014-06-20 12:00:00 -07001993
Adam Langleyfcf25832014-12-18 17:42:32 -08001994void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u) {
1995 ctx->default_passwd_callback_userdata = u;
1996}
Adam Langley95c29f32014-06-20 12:00:00 -07001997
Adam Langleyfcf25832014-12-18 17:42:32 -08001998void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
1999 int (*cb)(X509_STORE_CTX *, void *),
2000 void *arg) {
2001 ctx->app_verify_callback = cb;
2002 ctx->app_verify_arg = arg;
2003}
Adam Langley95c29f32014-06-20 12:00:00 -07002004
Adam Langleyfcf25832014-12-18 17:42:32 -08002005void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
2006 int (*cb)(int, X509_STORE_CTX *)) {
2007 ctx->verify_mode = mode;
2008 ctx->default_verify_callback = cb;
2009}
Adam Langley95c29f32014-06-20 12:00:00 -07002010
Adam Langleyfcf25832014-12-18 17:42:32 -08002011void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
2012 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
2013}
Adam Langley1258b6a2014-06-20 12:00:00 -07002014
Adam Langleyfcf25832014-12-18 17:42:32 -08002015void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb)(SSL *ssl, void *arg),
2016 void *arg) {
2017 ssl_cert_set_cert_cb(c->cert, cb, arg);
2018}
David Benjamin859ec3c2014-09-02 16:29:36 -04002019
Adam Langleyfcf25832014-12-18 17:42:32 -08002020void SSL_set_cert_cb(SSL *s, int (*cb)(SSL *ssl, void *arg), void *arg) {
2021 ssl_cert_set_cert_cb(s->cert, cb, arg);
2022}
Adam Langley95c29f32014-06-20 12:00:00 -07002023
Adam Langleyfcf25832014-12-18 17:42:32 -08002024static int ssl_has_key(SSL *s, size_t idx) {
2025 CERT_PKEY *cpk = &s->cert->pkeys[idx];
2026 return cpk->x509 && cpk->privatekey;
2027}
David Benjamin033e5f42014-11-13 18:47:41 -05002028
David Benjaminf31e6812014-11-13 18:05:55 -05002029void ssl_get_compatible_server_ciphers(SSL *s, unsigned long *out_mask_k,
Adam Langleyfcf25832014-12-18 17:42:32 -08002030 unsigned long *out_mask_a) {
2031 CERT *c = s->cert;
2032 int rsa_enc, rsa_sign, dh_tmp;
2033 unsigned long mask_k, mask_a;
2034 int have_ecc_cert, ecdsa_ok;
2035 int have_ecdh_tmp;
2036 X509 *x;
Adam Langley95c29f32014-06-20 12:00:00 -07002037
Adam Langleyfcf25832014-12-18 17:42:32 -08002038 if (c == NULL) {
2039 /* TODO(davidben): Is this codepath possible? */
2040 *out_mask_k = 0;
2041 *out_mask_a = 0;
2042 return;
2043 }
Adam Langley95c29f32014-06-20 12:00:00 -07002044
Adam Langleyfcf25832014-12-18 17:42:32 -08002045 dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07002046
Adam Langleyfcf25832014-12-18 17:42:32 -08002047 have_ecdh_tmp = (c->ecdh_tmp || c->ecdh_tmp_cb || c->ecdh_tmp_auto);
2048 rsa_enc = ssl_has_key(s, SSL_PKEY_RSA_ENC);
2049 rsa_sign = ssl_has_key(s, SSL_PKEY_RSA_SIGN);
2050 have_ecc_cert = ssl_has_key(s, SSL_PKEY_ECC);
2051 mask_k = 0;
2052 mask_a = 0;
David Benjaminf31e6812014-11-13 18:05:55 -05002053
Adam Langleyfcf25832014-12-18 17:42:32 -08002054 if (rsa_enc) {
2055 mask_k |= SSL_kRSA;
2056 }
2057 if (dh_tmp) {
2058 mask_k |= SSL_kEDH;
2059 }
2060 if (rsa_enc || rsa_sign) {
2061 mask_a |= SSL_aRSA;
2062 }
Adam Langley95c29f32014-06-20 12:00:00 -07002063
Adam Langleyfcf25832014-12-18 17:42:32 -08002064 mask_a |= SSL_aNULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002065
Adam Langleyfcf25832014-12-18 17:42:32 -08002066 /* An ECC certificate may be usable for ECDSA cipher suites depending on the
2067 * key usage extension and on the client's curve preferences. */
2068 if (have_ecc_cert) {
2069 x = c->pkeys[SSL_PKEY_ECC].x509;
2070 /* This call populates extension flags (ex_flags). */
2071 X509_check_purpose(x, -1, 0);
2072 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE)
2073 ? (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)
2074 : 1;
2075 if (!tls1_check_ec_cert(s, x)) {
2076 ecdsa_ok = 0;
2077 }
2078 if (ecdsa_ok) {
2079 mask_a |= SSL_aECDSA;
2080 }
2081 }
Adam Langley95c29f32014-06-20 12:00:00 -07002082
Adam Langleyfcf25832014-12-18 17:42:32 -08002083 /* If we are considering an ECC cipher suite that uses an ephemeral EC
2084 * key, check it. */
2085 if (have_ecdh_tmp && tls1_check_ec_tmp_key(s)) {
2086 mask_k |= SSL_kEECDH;
2087 }
Adam Langley95c29f32014-06-20 12:00:00 -07002088
Adam Langleyfcf25832014-12-18 17:42:32 -08002089 /* PSK requires a server callback. */
2090 if (s->psk_server_callback != NULL) {
2091 mask_k |= SSL_kPSK;
2092 mask_a |= SSL_aPSK;
2093 }
Adam Langley95c29f32014-06-20 12:00:00 -07002094
Adam Langleyfcf25832014-12-18 17:42:32 -08002095 *out_mask_k = mask_k;
2096 *out_mask_a = mask_a;
2097}
Adam Langley95c29f32014-06-20 12:00:00 -07002098
2099/* This handy macro borrowed from crypto/x509v3/v3_purp.c */
2100#define ku_reject(x, usage) \
Adam Langleyfcf25832014-12-18 17:42:32 -08002101 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
Adam Langley95c29f32014-06-20 12:00:00 -07002102
Adam Langleyfcf25832014-12-18 17:42:32 -08002103int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s) {
2104 unsigned long alg_a;
2105 int signature_nid = 0, md_nid = 0, pk_nid = 0;
2106 const SSL_CIPHER *cs = s->s3->tmp.new_cipher;
Adam Langley95c29f32014-06-20 12:00:00 -07002107
Adam Langleyfcf25832014-12-18 17:42:32 -08002108 alg_a = cs->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -07002109
Adam Langleyfcf25832014-12-18 17:42:32 -08002110 /* This call populates the ex_flags field correctly */
2111 X509_check_purpose(x, -1, 0);
2112 if (x->sig_alg && x->sig_alg->algorithm) {
2113 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
2114 OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
2115 }
2116 if (alg_a & SSL_aECDSA) {
2117 /* key usage, if present, must allow signing */
2118 if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE)) {
2119 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg,
2120 SSL_R_ECC_CERT_NOT_FOR_SIGNING);
2121 return 0;
2122 }
2123 }
Adam Langley95c29f32014-06-20 12:00:00 -07002124
Adam Langleyfcf25832014-12-18 17:42:32 -08002125 return 1; /* all checks are ok */
2126}
Adam Langley95c29f32014-06-20 12:00:00 -07002127
Adam Langleyfcf25832014-12-18 17:42:32 -08002128static int ssl_get_server_cert_index(const SSL *s) {
2129 int idx;
2130 idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
2131 if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509) {
2132 idx = SSL_PKEY_RSA_SIGN;
2133 }
2134 if (idx == -1) {
2135 OPENSSL_PUT_ERROR(SSL, ssl_get_server_cert_index, ERR_R_INTERNAL_ERROR);
2136 }
2137 return idx;
2138}
Adam Langley95c29f32014-06-20 12:00:00 -07002139
Adam Langleyfcf25832014-12-18 17:42:32 -08002140CERT_PKEY *ssl_get_server_send_pkey(const SSL *s) {
2141 int i = ssl_get_server_cert_index(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002142
Adam Langleyfcf25832014-12-18 17:42:32 -08002143 /* This may or may not be an error. */
2144 if (i < 0) {
2145 return NULL;
2146 }
Adam Langley95c29f32014-06-20 12:00:00 -07002147
Adam Langleyfcf25832014-12-18 17:42:32 -08002148 /* May be NULL. */
2149 return &s->cert->pkeys[i];
2150}
Adam Langley95c29f32014-06-20 12:00:00 -07002151
Adam Langleyfcf25832014-12-18 17:42:32 -08002152EVP_PKEY *ssl_get_sign_pkey(SSL *s, const SSL_CIPHER *cipher) {
2153 unsigned long alg_a;
2154 CERT *c;
2155 int idx = -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002156
Adam Langleyfcf25832014-12-18 17:42:32 -08002157 alg_a = cipher->algorithm_auth;
2158 c = s->cert;
Adam Langley95c29f32014-06-20 12:00:00 -07002159
Adam Langleyfcf25832014-12-18 17:42:32 -08002160 if (alg_a & SSL_aRSA) {
2161 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL) {
2162 idx = SSL_PKEY_RSA_SIGN;
2163 } else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL) {
2164 idx = SSL_PKEY_RSA_ENC;
2165 }
2166 } else if ((alg_a & SSL_aECDSA) &&
2167 (c->pkeys[SSL_PKEY_ECC].privatekey != NULL)) {
2168 idx = SSL_PKEY_ECC;
2169 }
Adam Langley95c29f32014-06-20 12:00:00 -07002170
Adam Langleyfcf25832014-12-18 17:42:32 -08002171 if (idx == -1) {
2172 OPENSSL_PUT_ERROR(SSL, ssl_get_sign_pkey, ERR_R_INTERNAL_ERROR);
2173 return NULL;
2174 }
Adam Langley95c29f32014-06-20 12:00:00 -07002175
Adam Langleyfcf25832014-12-18 17:42:32 -08002176 return c->pkeys[idx].privatekey;
2177}
Adam Langley95c29f32014-06-20 12:00:00 -07002178
Adam Langleyfcf25832014-12-18 17:42:32 -08002179void ssl_update_cache(SSL *s, int mode) {
2180 int i;
Adam Langley95c29f32014-06-20 12:00:00 -07002181
Adam Langleyfcf25832014-12-18 17:42:32 -08002182 /* If the session_id_length is 0, we are not supposed to cache it, and it
2183 * would be rather hard to do anyway :-) */
2184 if (s->session->session_id_length == 0) {
2185 return;
2186 }
Adam Langley95c29f32014-06-20 12:00:00 -07002187
Adam Langleyfcf25832014-12-18 17:42:32 -08002188 i = s->initial_ctx->session_cache_mode;
2189 if ((i & mode) && !s->hit &&
2190 ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE) ||
2191 SSL_CTX_add_session(s->initial_ctx, s->session)) &&
2192 s->initial_ctx->new_session_cb != NULL) {
2193 CRYPTO_add(&s->session->references, 1, CRYPTO_LOCK_SSL_SESSION);
2194 if (!s->initial_ctx->new_session_cb(s, s->session)) {
2195 SSL_SESSION_free(s->session);
2196 }
2197 }
Adam Langley95c29f32014-06-20 12:00:00 -07002198
Adam Langleyfcf25832014-12-18 17:42:32 -08002199 /* auto flush every 255 connections */
2200 if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) && ((i & mode) == mode)) {
2201 if ((((mode & SSL_SESS_CACHE_CLIENT)
2202 ? s->initial_ctx->stats.sess_connect_good
2203 : s->initial_ctx->stats.sess_accept_good) &
2204 0xff) == 0xff) {
2205 SSL_CTX_flush_sessions(s->initial_ctx, (unsigned long)time(NULL));
2206 }
2207 }
2208}
Adam Langley95c29f32014-06-20 12:00:00 -07002209
Adam Langleyfcf25832014-12-18 17:42:32 -08002210int SSL_get_error(const SSL *s, int i) {
2211 int reason;
2212 unsigned long l;
2213 BIO *bio;
Adam Langley95c29f32014-06-20 12:00:00 -07002214
Adam Langleyfcf25832014-12-18 17:42:32 -08002215 if (i > 0) {
2216 return SSL_ERROR_NONE;
2217 }
Adam Langley95c29f32014-06-20 12:00:00 -07002218
Adam Langleyfcf25832014-12-18 17:42:32 -08002219 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
2220 * where we do encode the error */
2221 l = ERR_peek_error();
2222 if (l != 0) {
2223 if (ERR_GET_LIB(l) == ERR_LIB_SYS) {
2224 return SSL_ERROR_SYSCALL;
2225 }
2226 return SSL_ERROR_SSL;
2227 }
Adam Langley95c29f32014-06-20 12:00:00 -07002228
Adam Langleyfcf25832014-12-18 17:42:32 -08002229 if (i == 0 && (s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2230 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) {
2231 return SSL_ERROR_ZERO_RETURN;
2232 }
Adam Langleyb2ce0582014-06-20 12:00:00 -07002233
Adam Langleyfcf25832014-12-18 17:42:32 -08002234 assert(i < 0);
Adam Langleydc9b1412014-06-20 12:00:00 -07002235
Adam Langleyfcf25832014-12-18 17:42:32 -08002236 if (SSL_want_session(s)) {
2237 return SSL_ERROR_PENDING_SESSION;
2238 }
Adam Langley95c29f32014-06-20 12:00:00 -07002239
Adam Langleyfcf25832014-12-18 17:42:32 -08002240 if (SSL_want_certificate(s)) {
2241 return SSL_ERROR_PENDING_CERTIFICATE;
2242 }
Adam Langley95c29f32014-06-20 12:00:00 -07002243
Adam Langleyfcf25832014-12-18 17:42:32 -08002244 if (SSL_want_read(s)) {
2245 bio = SSL_get_rbio(s);
2246 if (BIO_should_read(bio)) {
2247 return SSL_ERROR_WANT_READ;
2248 }
Adam Langley95c29f32014-06-20 12:00:00 -07002249
Adam Langleyfcf25832014-12-18 17:42:32 -08002250 if (BIO_should_write(bio)) {
2251 /* This one doesn't make too much sense ... We never try to write to the
2252 * rbio, and an application program where rbio and wbio are separate
2253 * couldn't even know what it should wait for. However if we ever set
2254 * s->rwstate incorrectly (so that we have SSL_want_read(s) instead of
2255 * SSL_want_write(s)) and rbio and wbio *are* the same, this test works
2256 * around that bug; so it might be safer to keep it. */
2257 return SSL_ERROR_WANT_WRITE;
2258 }
Adam Langley95c29f32014-06-20 12:00:00 -07002259
Adam Langleyfcf25832014-12-18 17:42:32 -08002260 if (BIO_should_io_special(bio)) {
2261 reason = BIO_get_retry_reason(bio);
2262 if (reason == BIO_RR_CONNECT) {
2263 return SSL_ERROR_WANT_CONNECT;
2264 }
Adam Langley95c29f32014-06-20 12:00:00 -07002265
Adam Langleyfcf25832014-12-18 17:42:32 -08002266 if (reason == BIO_RR_ACCEPT) {
2267 return SSL_ERROR_WANT_ACCEPT;
2268 }
Adam Langley95c29f32014-06-20 12:00:00 -07002269
Adam Langleyfcf25832014-12-18 17:42:32 -08002270 return SSL_ERROR_SYSCALL; /* unknown */
2271 }
2272 }
Adam Langley95c29f32014-06-20 12:00:00 -07002273
Adam Langleyfcf25832014-12-18 17:42:32 -08002274 if (SSL_want_write(s)) {
2275 bio = SSL_get_wbio(s);
2276 if (BIO_should_write(bio)) {
2277 return SSL_ERROR_WANT_WRITE;
2278 }
Adam Langley95c29f32014-06-20 12:00:00 -07002279
Adam Langleyfcf25832014-12-18 17:42:32 -08002280 if (BIO_should_read(bio)) {
2281 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2282 return SSL_ERROR_WANT_READ;
2283 }
Adam Langley95c29f32014-06-20 12:00:00 -07002284
Adam Langleyfcf25832014-12-18 17:42:32 -08002285 if (BIO_should_io_special(bio)) {
2286 reason = BIO_get_retry_reason(bio);
2287 if (reason == BIO_RR_CONNECT) {
2288 return SSL_ERROR_WANT_CONNECT;
2289 }
Adam Langley95c29f32014-06-20 12:00:00 -07002290
Adam Langleyfcf25832014-12-18 17:42:32 -08002291 if (reason == BIO_RR_ACCEPT) {
2292 return SSL_ERROR_WANT_ACCEPT;
2293 }
Adam Langley95c29f32014-06-20 12:00:00 -07002294
Adam Langleyfcf25832014-12-18 17:42:32 -08002295 return SSL_ERROR_SYSCALL;
2296 }
2297 }
Adam Langley95c29f32014-06-20 12:00:00 -07002298
Adam Langleyfcf25832014-12-18 17:42:32 -08002299 if (SSL_want_x509_lookup(s)) {
2300 return SSL_ERROR_WANT_X509_LOOKUP;
2301 }
Adam Langley95c29f32014-06-20 12:00:00 -07002302
Adam Langleyfcf25832014-12-18 17:42:32 -08002303 if (SSL_want_channel_id_lookup(s)) {
2304 return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
2305 }
Adam Langley0f4746e2014-08-13 12:26:32 -07002306
Adam Langleyfcf25832014-12-18 17:42:32 -08002307 return SSL_ERROR_SYSCALL;
2308}
Adam Langley0f4746e2014-08-13 12:26:32 -07002309
Adam Langleyfcf25832014-12-18 17:42:32 -08002310int SSL_do_handshake(SSL *s) {
2311 int ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002312
Adam Langleyfcf25832014-12-18 17:42:32 -08002313 if (s->handshake_func == NULL) {
2314 OPENSSL_PUT_ERROR(SSL, SSL_do_handshake, SSL_R_CONNECTION_TYPE_NOT_SET);
2315 return -1;
2316 }
Adam Langley95c29f32014-06-20 12:00:00 -07002317
Adam Langleyfcf25832014-12-18 17:42:32 -08002318 s->method->ssl_renegotiate_check(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002319
Adam Langleyfcf25832014-12-18 17:42:32 -08002320 if (SSL_in_init(s)) {
2321 ret = s->handshake_func(s);
2322 }
2323 return ret;
2324}
Adam Langley95c29f32014-06-20 12:00:00 -07002325
Adam Langleyfcf25832014-12-18 17:42:32 -08002326void SSL_set_accept_state(SSL *s) {
2327 s->server = 1;
2328 s->shutdown = 0;
2329 s->state = SSL_ST_ACCEPT | SSL_ST_BEFORE;
2330 s->handshake_func = s->method->ssl_accept;
2331 /* clear the current cipher */
2332 ssl_clear_cipher_ctx(s);
2333 ssl_clear_hash_ctx(&s->read_hash);
2334 ssl_clear_hash_ctx(&s->write_hash);
2335}
Adam Langley95c29f32014-06-20 12:00:00 -07002336
Adam Langleyfcf25832014-12-18 17:42:32 -08002337void SSL_set_connect_state(SSL *s) {
2338 s->server = 0;
2339 s->shutdown = 0;
2340 s->state = SSL_ST_CONNECT | SSL_ST_BEFORE;
2341 s->handshake_func = s->method->ssl_connect;
2342 /* clear the current cipher */
2343 ssl_clear_cipher_ctx(s);
2344 ssl_clear_hash_ctx(&s->read_hash);
2345 ssl_clear_hash_ctx(&s->write_hash);
2346}
Adam Langley95c29f32014-06-20 12:00:00 -07002347
Adam Langleyfcf25832014-12-18 17:42:32 -08002348int ssl_undefined_function(SSL *s) {
2349 OPENSSL_PUT_ERROR(SSL, ssl_undefined_function,
2350 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2351 return 0;
2352}
Adam Langley95c29f32014-06-20 12:00:00 -07002353
Adam Langleyfcf25832014-12-18 17:42:32 -08002354int ssl_undefined_void_function(void) {
2355 OPENSSL_PUT_ERROR(SSL, ssl_undefined_void_function,
2356 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2357 return 0;
2358}
Adam Langley95c29f32014-06-20 12:00:00 -07002359
Adam Langleyfcf25832014-12-18 17:42:32 -08002360int ssl_undefined_const_function(const SSL *s) {
2361 OPENSSL_PUT_ERROR(SSL, ssl_undefined_const_function,
2362 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2363 return 0;
2364}
Adam Langley95c29f32014-06-20 12:00:00 -07002365
Adam Langleyfcf25832014-12-18 17:42:32 -08002366static const char *ssl_get_version(int version) {
2367 switch (version) {
2368 case TLS1_2_VERSION:
2369 return "TLSv1.2";
Adam Langley95c29f32014-06-20 12:00:00 -07002370
Adam Langleyfcf25832014-12-18 17:42:32 -08002371 case TLS1_1_VERSION:
2372 return "TLSv1.1";
Adam Langley95c29f32014-06-20 12:00:00 -07002373
Adam Langleyfcf25832014-12-18 17:42:32 -08002374 case TLS1_VERSION:
2375 return "TLSv1";
Adam Langley95c29f32014-06-20 12:00:00 -07002376
Adam Langleyfcf25832014-12-18 17:42:32 -08002377 case SSL3_VERSION:
2378 return "SSLv3";
Adam Langley95c29f32014-06-20 12:00:00 -07002379
Adam Langleyfcf25832014-12-18 17:42:32 -08002380 default:
2381 return "unknown";
2382 }
2383}
Adam Langley95c29f32014-06-20 12:00:00 -07002384
Adam Langleyfcf25832014-12-18 17:42:32 -08002385const char *SSL_get_version(const SSL *s) {
2386 return ssl_get_version(s->version);
2387}
Adam Langley95c29f32014-06-20 12:00:00 -07002388
Adam Langleyfcf25832014-12-18 17:42:32 -08002389const char *SSL_SESSION_get_version(const SSL_SESSION *sess) {
2390 return ssl_get_version(sess->ssl_version);
2391}
Adam Langley95c29f32014-06-20 12:00:00 -07002392
Adam Langleyfcf25832014-12-18 17:42:32 -08002393void ssl_clear_cipher_ctx(SSL *s) {
2394 if (s->enc_read_ctx != NULL) {
2395 EVP_CIPHER_CTX_cleanup(s->enc_read_ctx);
2396 OPENSSL_free(s->enc_read_ctx);
2397 s->enc_read_ctx = NULL;
2398 }
Adam Langley95c29f32014-06-20 12:00:00 -07002399
Adam Langleyfcf25832014-12-18 17:42:32 -08002400 if (s->enc_write_ctx != NULL) {
2401 EVP_CIPHER_CTX_cleanup(s->enc_write_ctx);
2402 OPENSSL_free(s->enc_write_ctx);
2403 s->enc_write_ctx = NULL;
2404 }
Adam Langleya5dc5452014-06-20 12:00:00 -07002405
Adam Langleyfcf25832014-12-18 17:42:32 -08002406 if (s->aead_read_ctx != NULL) {
2407 EVP_AEAD_CTX_cleanup(&s->aead_read_ctx->ctx);
2408 OPENSSL_free(s->aead_read_ctx);
2409 s->aead_read_ctx = NULL;
2410 }
Adam Langleya5dc5452014-06-20 12:00:00 -07002411
Adam Langleyfcf25832014-12-18 17:42:32 -08002412 if (s->aead_write_ctx != NULL) {
2413 EVP_AEAD_CTX_cleanup(&s->aead_write_ctx->ctx);
2414 OPENSSL_free(s->aead_write_ctx);
2415 s->aead_write_ctx = NULL;
2416 }
2417}
Adam Langley95c29f32014-06-20 12:00:00 -07002418
Adam Langleyfcf25832014-12-18 17:42:32 -08002419X509 *SSL_get_certificate(const SSL *s) {
2420 if (s->cert != NULL) {
2421 return s->cert->key->x509;
2422 }
2423
2424 return NULL;
2425}
2426
2427EVP_PKEY *SSL_get_privatekey(const SSL *s) {
2428 if (s->cert != NULL) {
2429 return s->cert->key->privatekey;
2430 }
2431
2432 return NULL;
2433}
2434
2435X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
2436 if (ctx->cert != NULL) {
2437 return ctx->cert->key->x509;
2438 }
2439
2440 return NULL;
2441}
2442
2443EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
2444 if (ctx->cert != NULL) {
2445 return ctx->cert->key->privatekey;
2446 }
2447
2448 return NULL;
2449}
2450
2451const SSL_CIPHER *SSL_get_current_cipher(const SSL *s) {
2452 if (s->session != NULL && s->session->cipher != NULL) {
2453 return s->session->cipher;
2454 }
2455
2456 return NULL;
2457}
2458
2459const void *SSL_get_current_compression(SSL *s) { return NULL; }
2460
2461const void *SSL_get_current_expansion(SSL *s) { return NULL; }
2462
2463int ssl_init_wbio_buffer(SSL *s, int push) {
2464 BIO *bbio;
2465
2466 if (s->bbio == NULL) {
2467 bbio = BIO_new(BIO_f_buffer());
2468 if (bbio == NULL) {
2469 return 0;
2470 }
2471 s->bbio = bbio;
2472 } else {
2473 bbio = s->bbio;
2474 if (s->bbio == s->wbio) {
2475 s->wbio = BIO_pop(s->wbio);
2476 }
2477 }
2478
2479 BIO_reset(bbio);
2480 if (!BIO_set_read_buffer_size(bbio, 1)) {
2481 OPENSSL_PUT_ERROR(SSL, ssl_init_wbio_buffer, ERR_R_BUF_LIB);
2482 return 0;
2483 }
2484
2485 if (push) {
2486 if (s->wbio != bbio) {
2487 s->wbio = BIO_push(bbio, s->wbio);
2488 }
2489 } else {
2490 if (s->wbio == bbio) {
2491 s->wbio = BIO_pop(bbio);
2492 }
2493 }
2494
2495 return 1;
2496}
2497
2498void ssl_free_wbio_buffer(SSL *s) {
2499 if (s->bbio == NULL) {
2500 return;
2501 }
2502
2503 if (s->bbio == s->wbio) {
2504 /* remove buffering */
2505 s->wbio = BIO_pop(s->wbio);
2506 }
2507
2508 BIO_free(s->bbio);
2509 s->bbio = NULL;
2510}
2511
2512void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
2513 ctx->quiet_shutdown = mode;
2514}
2515
2516int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
2517 return ctx->quiet_shutdown;
2518}
2519
2520void SSL_set_quiet_shutdown(SSL *s, int mode) { s->quiet_shutdown = mode; }
2521
2522int SSL_get_quiet_shutdown(const SSL *s) { return s->quiet_shutdown; }
2523
2524void SSL_set_shutdown(SSL *s, int mode) { s->shutdown = mode; }
2525
2526int SSL_get_shutdown(const SSL *s) { return s->shutdown; }
2527
2528int SSL_version(const SSL *s) { return s->version; }
2529
2530SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
2531
2532SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
2533 if (ssl->ctx == ctx) {
2534 return ssl->ctx;
2535 }
2536
2537 if (ctx == NULL) {
2538 ctx = ssl->initial_ctx;
2539 }
2540
2541 if (ssl->cert != NULL) {
2542 ssl_cert_free(ssl->cert);
2543 }
2544
2545 ssl->cert = ssl_cert_dup(ctx->cert);
2546 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
2547 if (ssl->ctx != NULL) {
2548 SSL_CTX_free(ssl->ctx); /* decrement reference count */
2549 }
2550 ssl->ctx = ctx;
2551
2552 ssl->sid_ctx_length = ctx->sid_ctx_length;
2553 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2554 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2555
2556 return ssl->ctx;
2557}
2558
2559int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
2560 return X509_STORE_set_default_paths(ctx->cert_store);
2561}
Adam Langley95c29f32014-06-20 12:00:00 -07002562
2563int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
Adam Langleyfcf25832014-12-18 17:42:32 -08002564 const char *CApath) {
2565 return X509_STORE_load_locations(ctx->cert_store, CAfile, CApath);
2566}
Adam Langley95c29f32014-06-20 12:00:00 -07002567
2568void SSL_set_info_callback(SSL *ssl,
Adam Langleyfcf25832014-12-18 17:42:32 -08002569 void (*cb)(const SSL *ssl, int type, int val)) {
2570 ssl->info_callback = cb;
2571}
Adam Langley95c29f32014-06-20 12:00:00 -07002572
Adam Langleyfcf25832014-12-18 17:42:32 -08002573void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/, int /*type*/,
2574 int /*val*/) {
2575 return ssl->info_callback;
2576}
Adam Langley95c29f32014-06-20 12:00:00 -07002577
Adam Langleyfcf25832014-12-18 17:42:32 -08002578int SSL_state(const SSL *ssl) { return ssl->state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002579
Adam Langleyfcf25832014-12-18 17:42:32 -08002580void SSL_set_state(SSL *ssl, int state) { ssl->state = state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002581
Adam Langleyfcf25832014-12-18 17:42:32 -08002582void SSL_set_verify_result(SSL *ssl, long arg) { ssl->verify_result = arg; }
Adam Langley95c29f32014-06-20 12:00:00 -07002583
Adam Langleyfcf25832014-12-18 17:42:32 -08002584long SSL_get_verify_result(const SSL *ssl) { return ssl->verify_result; }
Adam Langley95c29f32014-06-20 12:00:00 -07002585
Adam Langleyfcf25832014-12-18 17:42:32 -08002586int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2587 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
2588 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp, new_func,
2589 dup_func, free_func);
2590}
Adam Langley95c29f32014-06-20 12:00:00 -07002591
Adam Langleyfcf25832014-12-18 17:42:32 -08002592int SSL_set_ex_data(SSL *s, int idx, void *arg) {
2593 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2594}
Adam Langley95c29f32014-06-20 12:00:00 -07002595
Adam Langleyfcf25832014-12-18 17:42:32 -08002596void *SSL_get_ex_data(const SSL *s, int idx) {
2597 return CRYPTO_get_ex_data(&s->ex_data, idx);
2598}
Adam Langley95c29f32014-06-20 12:00:00 -07002599
Adam Langleyfcf25832014-12-18 17:42:32 -08002600int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2601 CRYPTO_EX_dup *dup_func,
2602 CRYPTO_EX_free *free_func) {
2603 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp, new_func,
2604 dup_func, free_func);
2605}
Adam Langley95c29f32014-06-20 12:00:00 -07002606
Adam Langleyfcf25832014-12-18 17:42:32 -08002607int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg) {
2608 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2609}
Adam Langley95c29f32014-06-20 12:00:00 -07002610
Adam Langleyfcf25832014-12-18 17:42:32 -08002611void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx) {
2612 return CRYPTO_get_ex_data(&s->ex_data, idx);
2613}
Adam Langley95c29f32014-06-20 12:00:00 -07002614
Adam Langleyfcf25832014-12-18 17:42:32 -08002615int ssl_ok(SSL *s) { return 1; }
Adam Langley95c29f32014-06-20 12:00:00 -07002616
Adam Langleyfcf25832014-12-18 17:42:32 -08002617X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
2618 return ctx->cert_store;
2619}
Adam Langley95c29f32014-06-20 12:00:00 -07002620
Adam Langleyfcf25832014-12-18 17:42:32 -08002621void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
2622 if (ctx->cert_store != NULL) {
2623 X509_STORE_free(ctx->cert_store);
2624 }
2625 ctx->cert_store = store;
2626}
Adam Langley95c29f32014-06-20 12:00:00 -07002627
Adam Langleyfcf25832014-12-18 17:42:32 -08002628int SSL_want(const SSL *s) { return s->rwstate; }
Adam Langley95c29f32014-06-20 12:00:00 -07002629
Adam Langleyfcf25832014-12-18 17:42:32 -08002630void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
2631 RSA *(*cb)(SSL *ssl, int is_export,
2632 int keylength)) {
2633 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_RSA_CB, (void (*)(void))cb);
2634}
Adam Langley95c29f32014-06-20 12:00:00 -07002635
Adam Langleyfcf25832014-12-18 17:42:32 -08002636void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
2637 int keylength)) {
2638 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_RSA_CB, (void (*)(void))cb);
2639}
2640
2641void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
2642 DH *(*dh)(SSL *ssl, int is_export,
2643 int keylength)) {
2644 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh);
2645}
2646
2647void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*dh)(SSL *ssl, int is_export,
2648 int keylength)) {
2649 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh);
2650}
2651
2652void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,
2653 EC_KEY *(*ecdh)(SSL *ssl, int is_export,
2654 int keylength)) {
2655 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH_CB, (void (*)(void))ecdh);
2656}
2657
2658void SSL_set_tmp_ecdh_callback(SSL *ssl,
2659 EC_KEY *(*ecdh)(SSL *ssl, int is_export,
2660 int keylength)) {
2661 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_ECDH_CB, (void (*)(void))ecdh);
2662}
2663
2664int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
2665 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2666 OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_psk_identity_hint,
2667 SSL_R_DATA_LENGTH_TOO_LONG);
2668 return 0;
2669 }
2670
2671 if (ctx->psk_identity_hint != NULL) {
2672 OPENSSL_free(ctx->psk_identity_hint);
2673 }
2674
2675 if (identity_hint != NULL) {
2676 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2677 if (ctx->psk_identity_hint == NULL) {
2678 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002679 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002680 } else {
2681 ctx->psk_identity_hint = NULL;
2682 }
Adam Langley95c29f32014-06-20 12:00:00 -07002683
Adam Langleyfcf25832014-12-18 17:42:32 -08002684 return 1;
2685}
2686
2687int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint) {
2688 if (s == NULL) {
2689 return 0;
2690 }
2691
2692 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2693 OPENSSL_PUT_ERROR(SSL, SSL_use_psk_identity_hint,
2694 SSL_R_DATA_LENGTH_TOO_LONG);
2695 return 0;
2696 }
2697
2698 /* Clear currently configured hint, if any. */
2699 if (s->psk_identity_hint != NULL) {
2700 OPENSSL_free(s->psk_identity_hint);
2701 s->psk_identity_hint = NULL;
2702 }
2703
2704 if (identity_hint != NULL) {
2705 s->psk_identity_hint = BUF_strdup(identity_hint);
2706 if (s->psk_identity_hint == NULL) {
2707 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002708 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002709 }
Adam Langley95c29f32014-06-20 12:00:00 -07002710
Adam Langleyfcf25832014-12-18 17:42:32 -08002711 return 1;
2712}
Adam Langley95c29f32014-06-20 12:00:00 -07002713
Adam Langleyfcf25832014-12-18 17:42:32 -08002714const char *SSL_get_psk_identity_hint(const SSL *s) {
2715 if (s == NULL) {
2716 return NULL;
2717 }
2718 return s->psk_identity_hint;
2719}
Adam Langley95c29f32014-06-20 12:00:00 -07002720
Adam Langleyfcf25832014-12-18 17:42:32 -08002721const char *SSL_get_psk_identity(const SSL *s) {
2722 if (s == NULL || s->session == NULL) {
2723 return NULL;
2724 }
Adam Langley95c29f32014-06-20 12:00:00 -07002725
Adam Langleyfcf25832014-12-18 17:42:32 -08002726 return s->session->psk_identity;
2727}
Adam Langley95c29f32014-06-20 12:00:00 -07002728
Adam Langleyfcf25832014-12-18 17:42:32 -08002729void SSL_set_psk_client_callback(
2730 SSL *s, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2731 unsigned int max_identity_len, uint8_t *psk,
2732 unsigned int max_psk_len)) {
2733 s->psk_client_callback = cb;
2734}
Adam Langley95c29f32014-06-20 12:00:00 -07002735
Adam Langleyfcf25832014-12-18 17:42:32 -08002736void SSL_CTX_set_psk_client_callback(
2737 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2738 unsigned int max_identity_len,
2739 uint8_t *psk, unsigned int max_psk_len)) {
2740 ctx->psk_client_callback = cb;
2741}
Adam Langley95c29f32014-06-20 12:00:00 -07002742
Adam Langleyfcf25832014-12-18 17:42:32 -08002743void SSL_set_psk_server_callback(
2744 SSL *s, unsigned int (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
2745 unsigned int max_psk_len)) {
2746 s->psk_server_callback = cb;
2747}
Adam Langley95c29f32014-06-20 12:00:00 -07002748
Adam Langleyfcf25832014-12-18 17:42:32 -08002749void SSL_CTX_set_psk_server_callback(
2750 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *identity,
2751 uint8_t *psk, unsigned int max_psk_len)) {
2752 ctx->psk_server_callback = cb;
2753}
Adam Langley95c29f32014-06-20 12:00:00 -07002754
Adam Langleyfcf25832014-12-18 17:42:32 -08002755void SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) {
2756 ctx->min_version = version;
2757}
Adam Langley95c29f32014-06-20 12:00:00 -07002758
Adam Langleyfcf25832014-12-18 17:42:32 -08002759void SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) {
2760 ctx->max_version = version;
2761}
Adam Langley0289c732014-06-20 12:00:00 -07002762
Adam Langleyfcf25832014-12-18 17:42:32 -08002763void SSL_set_min_version(SSL *ssl, uint16_t version) {
2764 ssl->min_version = version;
2765}
Adam Langley0289c732014-06-20 12:00:00 -07002766
Adam Langleyfcf25832014-12-18 17:42:32 -08002767void SSL_set_max_version(SSL *ssl, uint16_t version) {
2768 ssl->max_version = version;
2769}
Adam Langley95c29f32014-06-20 12:00:00 -07002770
Adam Langleyfcf25832014-12-18 17:42:32 -08002771void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
2772 void (*cb)(int write_p, int version,
2773 int content_type, const void *buf,
2774 size_t len, SSL *ssl, void *arg)) {
2775 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2776}
2777void SSL_set_msg_callback(SSL *ssl,
2778 void (*cb)(int write_p, int version, int content_type,
2779 const void *buf, size_t len, SSL *ssl,
2780 void *arg)) {
2781 SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2782}
Adam Langley95c29f32014-06-20 12:00:00 -07002783
Adam Langleyfcf25832014-12-18 17:42:32 -08002784void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio) {
2785 if (ctx->keylog_bio != NULL) {
2786 BIO_free(ctx->keylog_bio);
2787 }
2788 ctx->keylog_bio = keylog_bio;
2789}
Adam Langley95c29f32014-06-20 12:00:00 -07002790
Adam Langleyfcf25832014-12-18 17:42:32 -08002791static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
2792 static const char hextable[] = "0123456789abcdef";
2793 uint8_t *out;
2794 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002795
Adam Langleyfcf25832014-12-18 17:42:32 -08002796 if (!CBB_add_space(cbb, &out, in_len * 2)) {
2797 return 0;
2798 }
Adam Langley95c29f32014-06-20 12:00:00 -07002799
Adam Langleyfcf25832014-12-18 17:42:32 -08002800 for (i = 0; i < in_len; i++) {
2801 *(out++) = (uint8_t)hextable[in[i] >> 4];
2802 *(out++) = (uint8_t)hextable[in[i] & 0xf];
2803 }
Adam Langley95c29f32014-06-20 12:00:00 -07002804
Adam Langleyfcf25832014-12-18 17:42:32 -08002805 return 1;
2806}
David Benjamin859ec3c2014-09-02 16:29:36 -04002807
2808int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -08002809 const uint8_t *encrypted_premaster,
2810 size_t encrypted_premaster_len,
2811 const uint8_t *premaster,
2812 size_t premaster_len) {
2813 BIO *bio = ctx->keylog_bio;
2814 CBB cbb;
2815 uint8_t *out;
2816 size_t out_len;
2817 int ret;
David Benjamin859ec3c2014-09-02 16:29:36 -04002818
Adam Langleyfcf25832014-12-18 17:42:32 -08002819 if (bio == NULL) {
2820 return 1;
2821 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002822
Adam Langleyfcf25832014-12-18 17:42:32 -08002823 if (encrypted_premaster_len < 8) {
2824 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_rsa_client_key_exchange,
2825 ERR_R_INTERNAL_ERROR);
2826 return 0;
2827 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002828
Adam Langleyfcf25832014-12-18 17:42:32 -08002829 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len * 2 + 1)) {
2830 return 0;
2831 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002832
Adam Langleyfcf25832014-12-18 17:42:32 -08002833 if (!CBB_add_bytes(&cbb, (const uint8_t *)"RSA ", 4) ||
2834 /* Only the first 8 bytes of the encrypted premaster secret are
2835 * logged. */
2836 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
2837 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2838 !cbb_add_hex(&cbb, premaster, premaster_len) ||
2839 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2840 !CBB_finish(&cbb, &out, &out_len)) {
2841 CBB_cleanup(&cbb);
2842 return 0;
2843 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002844
Adam Langleyfcf25832014-12-18 17:42:32 -08002845 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2846 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2847 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
David Benjamin859ec3c2014-09-02 16:29:36 -04002848
Adam Langleyfcf25832014-12-18 17:42:32 -08002849 OPENSSL_free(out);
2850 return ret;
Adam Langley95f22882014-06-20 12:00:00 -07002851}
2852
Adam Langleyfcf25832014-12-18 17:42:32 -08002853int ssl_ctx_log_master_secret(SSL_CTX *ctx, const uint8_t *client_random,
2854 size_t client_random_len, const uint8_t *master,
2855 size_t master_len) {
2856 BIO *bio = ctx->keylog_bio;
2857 CBB cbb;
2858 uint8_t *out;
2859 size_t out_len;
2860 int ret;
Adam Langleyadb739e2014-06-20 12:00:00 -07002861
Adam Langleyfcf25832014-12-18 17:42:32 -08002862 if (bio == NULL) {
2863 return 1;
2864 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002865
Adam Langleyfcf25832014-12-18 17:42:32 -08002866 if (client_random_len != 32) {
2867 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_master_secret, ERR_R_INTERNAL_ERROR);
2868 return 0;
2869 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002870
Adam Langleyfcf25832014-12-18 17:42:32 -08002871 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len * 2 + 1)) {
2872 return 0;
2873 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002874
Adam Langleyfcf25832014-12-18 17:42:32 -08002875 if (!CBB_add_bytes(&cbb, (const uint8_t *)"CLIENT_RANDOM ", 14) ||
2876 !cbb_add_hex(&cbb, client_random, 32) ||
2877 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2878 !cbb_add_hex(&cbb, master, master_len) ||
2879 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2880 !CBB_finish(&cbb, &out, &out_len)) {
2881 CBB_cleanup(&cbb);
2882 return 0;
2883 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002884
Adam Langleyfcf25832014-12-18 17:42:32 -08002885 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2886 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2887 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
David Benjamine99e9122014-12-11 01:46:01 -05002888
Adam Langleyfcf25832014-12-18 17:42:32 -08002889 OPENSSL_free(out);
2890 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -07002891}
2892
Adam Langleyfcf25832014-12-18 17:42:32 -08002893int SSL_cutthrough_complete(const SSL *s) {
2894 return (
2895 !s->server && /* cutthrough only applies to clients */
2896 !s->hit && /* full-handshake */
2897 s->version >= SSL3_VERSION &&
2898 s->s3->in_read_app_data == 0 && /* cutthrough only applies to write() */
2899 (SSL_get_mode((SSL *)s) &
2900 SSL_MODE_HANDSHAKE_CUTTHROUGH) && /* cutthrough enabled */
2901 ssl3_can_cutthrough(s) && /* cutthrough allowed */
2902 s->s3->previous_server_finished_len ==
2903 0 && /* not a renegotiation handshake */
2904 (s->state == SSL3_ST_CR_SESSION_TICKET_A || /* ready to write app-data*/
2905 s->state == SSL3_ST_CR_CHANGE || s->state == SSL3_ST_CR_FINISHED_A));
2906}
Adam Langley95c29f32014-06-20 12:00:00 -07002907
Adam Langleyfcf25832014-12-18 17:42:32 -08002908void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
2909 size_t *ssl_session_size) {
2910 *ssl_size = sizeof(SSL);
2911 *ssl_ctx_size = sizeof(SSL_CTX);
2912 *ssl_session_size = sizeof(SSL_SESSION);
2913}
Feng Lu41aa3252014-11-21 22:47:56 -08002914
Adam Langleyfcf25832014-12-18 17:42:32 -08002915int ssl3_can_cutthrough(const SSL *s) {
2916 const SSL_CIPHER *c;
2917
2918 /* require a strong enough cipher */
2919 if (SSL_get_cipher_bits(s, NULL) < 128) {
2920 return 0;
2921 }
2922
2923 /* require ALPN or NPN extension */
2924 if (!s->s3->alpn_selected && !s->s3->next_proto_neg_seen) {
2925 return 0;
2926 }
2927
2928 /* require a forward-secret cipher */
2929 c = SSL_get_current_cipher(s);
2930 if (!c ||
2931 (c->algorithm_mkey != SSL_kEDH && c->algorithm_mkey != SSL_kEECDH)) {
2932 return 0;
2933 }
2934
2935 return 1;
2936}
2937
2938const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version) {
2939 switch (version) {
2940 case SSL3_VERSION:
2941 return &SSLv3_enc_data;
2942
2943 case TLS1_VERSION:
2944 return &TLSv1_enc_data;
2945
2946 case TLS1_1_VERSION:
2947 return &TLSv1_1_enc_data;
2948
2949 case TLS1_2_VERSION:
2950 return &TLSv1_2_enc_data;
2951
2952 case DTLS1_VERSION:
2953 return &DTLSv1_enc_data;
2954
2955 case DTLS1_2_VERSION:
2956 return &DTLSv1_2_enc_data;
2957
2958 default:
2959 return NULL;
2960 }
2961}
2962
2963uint16_t ssl3_get_max_server_version(const SSL *s) {
2964 uint16_t max_version;
2965
2966 if (SSL_IS_DTLS(s)) {
2967 max_version = (s->max_version != 0) ? s->max_version : DTLS1_2_VERSION;
2968 if (!(s->options & SSL_OP_NO_DTLSv1_2) && DTLS1_2_VERSION >= max_version) {
2969 return DTLS1_2_VERSION;
2970 }
2971 if (!(s->options & SSL_OP_NO_DTLSv1) && DTLS1_VERSION >= max_version) {
2972 return DTLS1_VERSION;
2973 }
2974 return 0;
2975 }
2976
2977 max_version = (s->max_version != 0) ? s->max_version : TLS1_2_VERSION;
2978 if (!(s->options & SSL_OP_NO_TLSv1_2) && TLS1_2_VERSION <= max_version) {
2979 return TLS1_2_VERSION;
2980 }
2981 if (!(s->options & SSL_OP_NO_TLSv1_1) && TLS1_1_VERSION <= max_version) {
2982 return TLS1_1_VERSION;
2983 }
2984 if (!(s->options & SSL_OP_NO_TLSv1) && TLS1_VERSION <= max_version) {
2985 return TLS1_VERSION;
2986 }
2987 if (!(s->options & SSL_OP_NO_SSLv3) && SSL3_VERSION <= max_version) {
2988 return SSL3_VERSION;
2989 }
2990 return 0;
2991}
2992
2993uint16_t ssl3_get_mutual_version(SSL *s, uint16_t client_version) {
2994 uint16_t version = 0;
2995
2996 if (SSL_IS_DTLS(s)) {
2997 /* Clamp client_version to max_version. */
2998 if (s->max_version != 0 && client_version < s->max_version) {
2999 client_version = s->max_version;
3000 }
3001
3002 if (client_version <= DTLS1_2_VERSION && !(s->options & SSL_OP_NO_DTLSv1_2)) {
3003 version = DTLS1_2_VERSION;
3004 } else if (client_version <= DTLS1_VERSION &&
3005 !(s->options & SSL_OP_NO_DTLSv1)) {
3006 version = DTLS1_VERSION;
3007 }
3008
3009 /* Check against min_version. */
3010 if (version != 0 && s->min_version != 0 && version > s->min_version) {
3011 return 0;
3012 }
3013 return version;
3014 } else {
3015 /* Clamp client_version to max_version. */
3016 if (s->max_version != 0 && client_version > s->max_version) {
3017 client_version = s->max_version;
3018 }
3019
3020 if (client_version >= TLS1_2_VERSION && !(s->options & SSL_OP_NO_TLSv1_2)) {
3021 version = TLS1_2_VERSION;
3022 } else if (client_version >= TLS1_1_VERSION &&
3023 !(s->options & SSL_OP_NO_TLSv1_1)) {
3024 version = TLS1_1_VERSION;
3025 } else if (client_version >= TLS1_VERSION && !(s->options & SSL_OP_NO_TLSv1)) {
3026 version = TLS1_VERSION;
3027 } else if (client_version >= SSL3_VERSION && !(s->options & SSL_OP_NO_SSLv3)) {
3028 version = SSL3_VERSION;
3029 }
3030
3031 /* Check against min_version. */
3032 if (version != 0 && s->min_version != 0 && version < s->min_version) {
3033 return 0;
3034 }
3035 return version;
3036 }
3037}
3038
3039uint16_t ssl3_get_max_client_version(SSL *s) {
3040 unsigned long options = s->options;
3041 uint16_t version = 0;
3042
3043 /* OpenSSL's API for controlling versions entails blacklisting individual
3044 * protocols. This has two problems. First, on the client, the protocol can
3045 * only express a contiguous range of versions. Second, a library consumer
3046 * trying to set a maximum version cannot disable protocol versions that get
3047 * added in a future version of the library.
3048 *
3049 * To account for both of these, OpenSSL interprets the client-side bitmask
3050 * as a min/max range by picking the lowest contiguous non-empty range of
3051 * enabled protocols. Note that this means it is impossible to set a maximum
3052 * version of TLS 1.2 in a future-proof way.
3053 *
3054 * By this scheme, the maximum version is the lowest version V such that V is
3055 * enabled and V+1 is disabled or unimplemented. */
3056 if (SSL_IS_DTLS(s)) {
3057 if (!(options & SSL_OP_NO_DTLSv1_2)) {
3058 version = DTLS1_2_VERSION;
3059 }
3060 if (!(options & SSL_OP_NO_DTLSv1) && (options & SSL_OP_NO_DTLSv1_2)) {
3061 version = DTLS1_VERSION;
3062 }
3063 if (s->max_version != 0 && version < s->max_version) {
3064 version = s->max_version;
3065 }
3066 } else {
3067 if (!(options & SSL_OP_NO_TLSv1_2)) {
3068 version = TLS1_2_VERSION;
3069 }
3070 if (!(options & SSL_OP_NO_TLSv1_1) && (options & SSL_OP_NO_TLSv1_2)) {
3071 version = TLS1_1_VERSION;
3072 }
3073 if (!(options & SSL_OP_NO_TLSv1) && (options & SSL_OP_NO_TLSv1_1)) {
3074 version = TLS1_VERSION;
3075 }
3076 if (!(options & SSL_OP_NO_SSLv3) && (options & SSL_OP_NO_TLSv1)) {
3077 version = SSL3_VERSION;
3078 }
3079 if (s->max_version != 0 && version > s->max_version) {
3080 version = s->max_version;
3081 }
3082 }
3083
3084 return version;
3085}
3086
3087int ssl3_is_version_enabled(SSL *s, uint16_t version) {
3088 if (SSL_IS_DTLS(s)) {
3089 if (s->max_version != 0 && version < s->max_version) {
3090 return 0;
3091 }
3092 if (s->min_version != 0 && version > s->min_version) {
3093 return 0;
3094 }
3095
3096 switch (version) {
3097 case DTLS1_VERSION:
3098 return !(s->options & SSL_OP_NO_DTLSv1);
3099
3100 case DTLS1_2_VERSION:
3101 return !(s->options & SSL_OP_NO_DTLSv1_2);
3102
3103 default:
3104 return 0;
3105 }
3106 } else {
3107 if (s->max_version != 0 && version > s->max_version) {
3108 return 0;
3109 }
3110 if (s->min_version != 0 && version < s->min_version) {
3111 return 0;
3112 }
3113
3114 switch (version) {
3115 case SSL3_VERSION:
3116 return !(s->options & SSL_OP_NO_SSLv3);
3117
3118 case TLS1_VERSION:
3119 return !(s->options & SSL_OP_NO_TLSv1);
3120
3121 case TLS1_1_VERSION:
3122 return !(s->options & SSL_OP_NO_TLSv1_1);
3123
3124 case TLS1_2_VERSION:
3125 return !(s->options & SSL_OP_NO_TLSv1_2);
3126
3127 default:
3128 return 0;
3129 }
3130 }
3131}
3132
3133/* Allocates new EVP_MD_CTX and sets pointer to it into given pointer vairable,
3134 * freeing EVP_MD_CTX previously stored in that variable, if any. If EVP_MD
3135 * pointer is passed, initializes ctx with this md Returns newly allocated
3136 * ctx. */
3137EVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash, const EVP_MD *md) {
3138 ssl_clear_hash_ctx(hash);
3139 *hash = EVP_MD_CTX_create();
3140 if (md != NULL && *hash != NULL && !EVP_DigestInit_ex(*hash, md, NULL)) {
3141 EVP_MD_CTX_destroy(*hash);
3142 *hash = NULL;
3143 }
3144 return *hash;
3145}
3146
3147void ssl_clear_hash_ctx(EVP_MD_CTX **hash) {
3148 if (*hash) {
3149 EVP_MD_CTX_destroy(*hash);
3150 }
3151 *hash = NULL;
3152}
3153
3154int SSL_cache_hit(SSL *s) { return s->hit; }
3155
3156int SSL_is_server(SSL *s) { return s->server; }
3157
3158void SSL_enable_fastradio_padding(SSL *s, char on_off) {
3159 s->fastradio_padding = on_off;
3160}