blob: 348e2a5dfe205865aaa29f1cd40d9153a922fb35 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
108 *
109 */
110/* ====================================================================
111 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
112 * ECC cipher suite support in OpenSSL originally developed by
113 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
114 */
115/* ====================================================================
116 * Copyright 2005 Nokia. All rights reserved.
117 *
118 * The portions of the attached software ("Contribution") is developed by
119 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
120 * license.
121 *
122 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
123 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
124 * support (see RFC 4279) to OpenSSL.
125 *
126 * No patent licenses or other rights except those expressly stated in
127 * the OpenSSL open source license shall be deemed granted or received
128 * expressly, by implication, estoppel, or otherwise.
129 *
130 * No assurances are provided by Nokia that the Contribution does not
131 * infringe the patent or other intellectual property rights of any third
132 * party or that the license provides you with all the necessary rights
133 * to make use of the Contribution.
134 *
135 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
136 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
137 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
138 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
139 * OTHERWISE. */
140
141#include <stdio.h>
142#include <assert.h>
143
David Benjamin39482a12014-07-20 13:30:15 -0400144#include <openssl/bytestring.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700145#include <openssl/dh.h>
146#include <openssl/engine.h>
147#include <openssl/lhash.h>
148#include <openssl/mem.h>
149#include <openssl/obj.h>
150#include <openssl/rand.h>
151#include <openssl/x509v3.h>
152
153#include "ssl_locl.h"
154
Adam Langleyfcf25832014-12-18 17:42:32 -0800155/* Some error codes are special. Ensure the make_errors.go script never
156 * regresses this. */
157OPENSSL_COMPILE_ASSERT(SSL_R_TLSV1_ALERT_NO_RENEGOTIATION ==
158 SSL_AD_NO_RENEGOTIATION + SSL_AD_REASON_OFFSET,
159 ssl_alert_reason_code_mismatch);
David Benjamine1136082014-09-20 12:28:58 -0400160
Adam Langleyfcf25832014-12-18 17:42:32 -0800161int SSL_clear(SSL *s) {
162 if (s->method == NULL) {
163 OPENSSL_PUT_ERROR(SSL, SSL_clear, SSL_R_NO_METHOD_SPECIFIED);
164 return 0;
165 }
Adam Langley95c29f32014-06-20 12:00:00 -0700166
Adam Langleyfcf25832014-12-18 17:42:32 -0800167 if (ssl_clear_bad_session(s)) {
168 SSL_SESSION_free(s->session);
169 s->session = NULL;
170 }
Adam Langley95c29f32014-06-20 12:00:00 -0700171
Adam Langleyfcf25832014-12-18 17:42:32 -0800172 s->hit = 0;
173 s->shutdown = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700174
Adam Langleyfcf25832014-12-18 17:42:32 -0800175 if (s->renegotiate) {
176 OPENSSL_PUT_ERROR(SSL, SSL_clear, ERR_R_INTERNAL_ERROR);
177 return 0;
178 }
Adam Langley95c29f32014-06-20 12:00:00 -0700179
Adam Langleyfcf25832014-12-18 17:42:32 -0800180 /* SSL_clear may be called before or after the |s| is initialized in either
181 * accept or connect state. In the latter case, SSL_clear should preserve the
182 * half and reset |s->state| accordingly. */
183 if (s->handshake_func != NULL) {
184 if (s->server) {
185 SSL_set_accept_state(s);
186 } else {
187 SSL_set_connect_state(s);
188 }
189 } else {
190 assert(s->state == 0);
191 }
Adam Langley95c29f32014-06-20 12:00:00 -0700192
David Benjamin62fd1622015-01-11 13:30:01 -0500193 /* TODO(davidben): Some state on |s| is reset both in |SSL_new| and
194 * |SSL_clear| because it is per-connection state rather than configuration
195 * state. Per-connection state should be on |s->s3| and |s->d1| so it is
196 * naturally reset at the right points between |SSL_new|, |SSL_clear|, and
197 * |ssl3_new|. */
198
Adam Langleyfcf25832014-12-18 17:42:32 -0800199 s->rwstate = SSL_NOTHING;
200 s->rstate = SSL_ST_READ_HEADER;
Adam Langley95c29f32014-06-20 12:00:00 -0700201
Adam Langleyfcf25832014-12-18 17:42:32 -0800202 if (s->init_buf != NULL) {
203 BUF_MEM_free(s->init_buf);
204 s->init_buf = NULL;
205 }
Adam Langley95c29f32014-06-20 12:00:00 -0700206
David Benjamin62fd1622015-01-11 13:30:01 -0500207 s->packet = NULL;
208 s->packet_length = 0;
209
Adam Langleyfcf25832014-12-18 17:42:32 -0800210 ssl_clear_cipher_ctx(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700211
David Benjamin62fd1622015-01-11 13:30:01 -0500212 if (s->next_proto_negotiated) {
213 OPENSSL_free(s->next_proto_negotiated);
214 s->next_proto_negotiated = NULL;
215 s->next_proto_negotiated_len = 0;
216 }
217
218 /* The s->d1->mtu is simultaneously configuration (preserved across
219 * clear) and connection-specific state (gets reset).
220 *
221 * TODO(davidben): Avoid this. */
222 unsigned mtu = 0;
223 if (s->d1 != NULL) {
224 mtu = s->d1->mtu;
225 }
226
227 s->method->ssl_free(s);
228 if (!s->method->ssl_new(s)) {
229 return 0;
230 }
231 s->enc_method = ssl3_get_enc_method(s->version);
232 assert(s->enc_method != NULL);
233
234 if (SSL_IS_DTLS(s) && (SSL_get_options(s) & SSL_OP_NO_QUERY_MTU)) {
235 s->d1->mtu = mtu;
236 }
237
Adam Langleyfcf25832014-12-18 17:42:32 -0800238 s->client_version = s->version;
Adam Langley95c29f32014-06-20 12:00:00 -0700239
Adam Langleyfcf25832014-12-18 17:42:32 -0800240 return 1;
241}
Adam Langley95c29f32014-06-20 12:00:00 -0700242
Adam Langleyfcf25832014-12-18 17:42:32 -0800243SSL *SSL_new(SSL_CTX *ctx) {
244 SSL *s;
Adam Langley95c29f32014-06-20 12:00:00 -0700245
Adam Langleyfcf25832014-12-18 17:42:32 -0800246 if (ctx == NULL) {
247 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_NULL_SSL_CTX);
248 return NULL;
249 }
250 if (ctx->method == NULL) {
251 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
252 return NULL;
253 }
Adam Langley95c29f32014-06-20 12:00:00 -0700254
Adam Langleyfcf25832014-12-18 17:42:32 -0800255 s = (SSL *)OPENSSL_malloc(sizeof(SSL));
256 if (s == NULL) {
257 goto err;
258 }
259 memset(s, 0, sizeof(SSL));
Adam Langley95c29f32014-06-20 12:00:00 -0700260
Adam Langleyfcf25832014-12-18 17:42:32 -0800261 s->min_version = ctx->min_version;
262 s->max_version = ctx->max_version;
David Benjamin1eb367c2014-12-12 18:17:51 -0500263
Adam Langleyfcf25832014-12-18 17:42:32 -0800264 s->options = ctx->options;
265 s->mode = ctx->mode;
266 s->max_cert_list = ctx->max_cert_list;
Adam Langley95c29f32014-06-20 12:00:00 -0700267
Adam Langleyfcf25832014-12-18 17:42:32 -0800268 if (ctx->cert != NULL) {
269 /* Earlier library versions used to copy the pointer to the CERT, not its
270 * contents; only when setting new parameters for the per-SSL copy,
271 * ssl_cert_new would be called (and the direct reference to the
272 * per-SSL_CTX settings would be lost, but those still were indirectly
273 * accessed for various purposes, and for that reason they used to be known
274 * as s->ctx->default_cert). Now we don't look at the SSL_CTX's CERT after
275 * having duplicated it once. */
Adam Langley95c29f32014-06-20 12:00:00 -0700276
Adam Langleyfcf25832014-12-18 17:42:32 -0800277 s->cert = ssl_cert_dup(ctx->cert);
278 if (s->cert == NULL) {
279 goto err;
280 }
281 } else {
282 s->cert = NULL; /* Cannot really happen (see SSL_CTX_new) */
283 }
Adam Langley95c29f32014-06-20 12:00:00 -0700284
Adam Langleyfcf25832014-12-18 17:42:32 -0800285 s->read_ahead = ctx->read_ahead;
286 s->msg_callback = ctx->msg_callback;
287 s->msg_callback_arg = ctx->msg_callback_arg;
288 s->verify_mode = ctx->verify_mode;
289 s->sid_ctx_length = ctx->sid_ctx_length;
290 assert(s->sid_ctx_length <= sizeof s->sid_ctx);
291 memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));
292 s->verify_callback = ctx->default_verify_callback;
293 s->generate_session_id = ctx->generate_session_id;
Adam Langley95c29f32014-06-20 12:00:00 -0700294
Adam Langleyfcf25832014-12-18 17:42:32 -0800295 s->param = X509_VERIFY_PARAM_new();
296 if (!s->param) {
297 goto err;
298 }
299 X509_VERIFY_PARAM_inherit(s->param, ctx->param);
300 s->quiet_shutdown = ctx->quiet_shutdown;
301 s->max_send_fragment = ctx->max_send_fragment;
Adam Langley95c29f32014-06-20 12:00:00 -0700302
Adam Langleyfcf25832014-12-18 17:42:32 -0800303 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
304 s->ctx = ctx;
305 s->tlsext_debug_cb = 0;
306 s->tlsext_debug_arg = NULL;
307 s->tlsext_ticket_expected = 0;
308 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
309 s->initial_ctx = ctx;
310 if (ctx->tlsext_ecpointformatlist) {
311 s->tlsext_ecpointformatlist = BUF_memdup(
312 ctx->tlsext_ecpointformatlist, ctx->tlsext_ecpointformatlist_length);
313 if (!s->tlsext_ecpointformatlist) {
314 goto err;
315 }
316 s->tlsext_ecpointformatlist_length = ctx->tlsext_ecpointformatlist_length;
317 }
Adam Langley95c29f32014-06-20 12:00:00 -0700318
Adam Langleyfcf25832014-12-18 17:42:32 -0800319 if (ctx->tlsext_ellipticcurvelist) {
320 s->tlsext_ellipticcurvelist =
321 BUF_memdup(ctx->tlsext_ellipticcurvelist,
322 ctx->tlsext_ellipticcurvelist_length * 2);
323 if (!s->tlsext_ellipticcurvelist) {
324 goto err;
325 }
326 s->tlsext_ellipticcurvelist_length = ctx->tlsext_ellipticcurvelist_length;
327 }
328 s->next_proto_negotiated = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700329
Adam Langleyfcf25832014-12-18 17:42:32 -0800330 if (s->ctx->alpn_client_proto_list) {
331 s->alpn_client_proto_list = BUF_memdup(s->ctx->alpn_client_proto_list,
332 s->ctx->alpn_client_proto_list_len);
333 if (s->alpn_client_proto_list == NULL) {
334 goto err;
335 }
336 s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;
337 }
Adam Langley95c29f32014-06-20 12:00:00 -0700338
Adam Langleyfcf25832014-12-18 17:42:32 -0800339 s->verify_result = X509_V_OK;
340 s->method = ctx->method;
Adam Langley95c29f32014-06-20 12:00:00 -0700341
Adam Langleyfcf25832014-12-18 17:42:32 -0800342 if (!s->method->ssl_new(s)) {
343 goto err;
344 }
345 s->enc_method = ssl3_get_enc_method(s->version);
346 assert(s->enc_method != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700347
David Benjamin62fd1622015-01-11 13:30:01 -0500348 s->rwstate = SSL_NOTHING;
349 s->rstate = SSL_ST_READ_HEADER;
Adam Langley95c29f32014-06-20 12:00:00 -0700350
Adam Langleyfcf25832014-12-18 17:42:32 -0800351 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700352
Adam Langleyfcf25832014-12-18 17:42:32 -0800353 s->psk_identity_hint = NULL;
354 if (ctx->psk_identity_hint) {
355 s->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
356 if (s->psk_identity_hint == NULL) {
357 goto err;
358 }
359 }
360 s->psk_client_callback = ctx->psk_client_callback;
361 s->psk_server_callback = ctx->psk_server_callback;
Adam Langley95c29f32014-06-20 12:00:00 -0700362
David Benjamin02ddbfd2015-01-11 13:09:11 -0500363 s->tlsext_channel_id_enabled = ctx->tlsext_channel_id_enabled;
364 if (ctx->tlsext_channel_id_private) {
365 s->tlsext_channel_id_private = EVP_PKEY_dup(ctx->tlsext_channel_id_private);
366 }
367
Adam Langleyfcf25832014-12-18 17:42:32 -0800368 s->signed_cert_timestamps_enabled = s->ctx->signed_cert_timestamps_enabled;
369 s->ocsp_stapling_enabled = s->ctx->ocsp_stapling_enabled;
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200370
Adam Langleyfcf25832014-12-18 17:42:32 -0800371 return s;
372
Adam Langley95c29f32014-06-20 12:00:00 -0700373err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800374 if (s != NULL) {
David Benjamin710d2272015-01-11 20:07:59 -0500375 SSL_free(s);
Adam Langleyfcf25832014-12-18 17:42:32 -0800376 }
377 OPENSSL_PUT_ERROR(SSL, SSL_new, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700378
Adam Langleyfcf25832014-12-18 17:42:32 -0800379 return NULL;
380}
Adam Langley95c29f32014-06-20 12:00:00 -0700381
Adam Langleyfcf25832014-12-18 17:42:32 -0800382int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
383 unsigned int sid_ctx_len) {
384 if (sid_ctx_len > sizeof ctx->sid_ctx) {
385 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_session_id_context,
386 SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
387 return 0;
388 }
389 ctx->sid_ctx_length = sid_ctx_len;
390 memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700391
Adam Langleyfcf25832014-12-18 17:42:32 -0800392 return 1;
393}
Adam Langley95c29f32014-06-20 12:00:00 -0700394
Adam Langleyfcf25832014-12-18 17:42:32 -0800395int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
396 unsigned int sid_ctx_len) {
397 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
398 OPENSSL_PUT_ERROR(SSL, SSL_set_session_id_context,
399 SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
400 return 0;
401 }
402 ssl->sid_ctx_length = sid_ctx_len;
403 memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700404
Adam Langleyfcf25832014-12-18 17:42:32 -0800405 return 1;
406}
Adam Langley95c29f32014-06-20 12:00:00 -0700407
Adam Langleyfcf25832014-12-18 17:42:32 -0800408int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb) {
409 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
410 ctx->generate_session_id = cb;
411 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
412 return 1;
413}
Adam Langley95c29f32014-06-20 12:00:00 -0700414
Adam Langleyfcf25832014-12-18 17:42:32 -0800415int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800416 ssl->generate_session_id = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -0800417 return 1;
418}
Adam Langley95c29f32014-06-20 12:00:00 -0700419
Adam Langleyfcf25832014-12-18 17:42:32 -0800420int SSL_has_matching_session_id(const SSL *ssl, const uint8_t *id,
421 unsigned int id_len) {
422 /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how we
423 * can "construct" a session to give us the desired check - ie. to find if
424 * there's a session in the hash table that would conflict with any new
425 * session built out of this id/id_len and the ssl_version in use by this
426 * SSL. */
427 SSL_SESSION r, *p;
Adam Langley95c29f32014-06-20 12:00:00 -0700428
Adam Langleyfcf25832014-12-18 17:42:32 -0800429 if (id_len > sizeof r.session_id) {
430 return 0;
431 }
Adam Langley95c29f32014-06-20 12:00:00 -0700432
Adam Langleyfcf25832014-12-18 17:42:32 -0800433 r.ssl_version = ssl->version;
434 r.session_id_length = id_len;
435 memcpy(r.session_id, id, id_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700436
Adam Langleyfcf25832014-12-18 17:42:32 -0800437 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
438 p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r);
439 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
440 return p != NULL;
441}
Adam Langley95c29f32014-06-20 12:00:00 -0700442
Adam Langleyfcf25832014-12-18 17:42:32 -0800443int SSL_CTX_set_purpose(SSL_CTX *s, int purpose) {
444 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
445}
446
447int SSL_set_purpose(SSL *s, int purpose) {
448 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
449}
450
451int SSL_CTX_set_trust(SSL_CTX *s, int trust) {
452 return X509_VERIFY_PARAM_set_trust(s->param, trust);
453}
454
455int SSL_set_trust(SSL *s, int trust) {
456 return X509_VERIFY_PARAM_set_trust(s->param, trust);
457}
458
459int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm) {
460 return X509_VERIFY_PARAM_set1(ctx->param, vpm);
461}
462
463int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm) {
464 return X509_VERIFY_PARAM_set1(ssl->param, vpm);
465}
Adam Langley95c29f32014-06-20 12:00:00 -0700466
Adam Langley858a88d2014-06-20 12:00:00 -0700467void ssl_cipher_preference_list_free(
Adam Langleyfcf25832014-12-18 17:42:32 -0800468 struct ssl_cipher_preference_list_st *cipher_list) {
469 sk_SSL_CIPHER_free(cipher_list->ciphers);
470 OPENSSL_free(cipher_list->in_group_flags);
471 OPENSSL_free(cipher_list);
472}
Adam Langley858a88d2014-06-20 12:00:00 -0700473
Adam Langleyfcf25832014-12-18 17:42:32 -0800474struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_dup(
475 struct ssl_cipher_preference_list_st *cipher_list) {
476 struct ssl_cipher_preference_list_st *ret = NULL;
477 size_t n = sk_SSL_CIPHER_num(cipher_list->ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700478
Adam Langleyfcf25832014-12-18 17:42:32 -0800479 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
480 if (!ret) {
481 goto err;
482 }
483
484 ret->ciphers = NULL;
485 ret->in_group_flags = NULL;
486 ret->ciphers = sk_SSL_CIPHER_dup(cipher_list->ciphers);
487 if (!ret->ciphers) {
488 goto err;
489 }
490 ret->in_group_flags = BUF_memdup(cipher_list->in_group_flags, n);
491 if (!ret->in_group_flags) {
492 goto err;
493 }
494
495 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -0700496
497err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800498 if (ret && ret->ciphers) {
499 sk_SSL_CIPHER_free(ret->ciphers);
500 }
501 if (ret) {
502 OPENSSL_free(ret);
503 }
504 return NULL;
505}
Adam Langley858a88d2014-06-20 12:00:00 -0700506
Adam Langleyfcf25832014-12-18 17:42:32 -0800507struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_from_ciphers(
508 STACK_OF(SSL_CIPHER) * ciphers) {
509 struct ssl_cipher_preference_list_st *ret = NULL;
510 size_t n = sk_SSL_CIPHER_num(ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700511
Adam Langleyfcf25832014-12-18 17:42:32 -0800512 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
513 if (!ret) {
514 goto err;
515 }
516 ret->ciphers = NULL;
517 ret->in_group_flags = NULL;
518 ret->ciphers = sk_SSL_CIPHER_dup(ciphers);
519 if (!ret->ciphers) {
520 goto err;
521 }
522 ret->in_group_flags = OPENSSL_malloc(n);
523 if (!ret->in_group_flags) {
524 goto err;
525 }
526 memset(ret->in_group_flags, 0, n);
527 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -0700528
529err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800530 if (ret && ret->ciphers) {
531 sk_SSL_CIPHER_free(ret->ciphers);
532 }
533 if (ret) {
534 OPENSSL_free(ret);
535 }
536 return NULL;
537}
Adam Langley858a88d2014-06-20 12:00:00 -0700538
Adam Langleyfcf25832014-12-18 17:42:32 -0800539X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) { return ctx->param; }
Adam Langley95c29f32014-06-20 12:00:00 -0700540
Adam Langleyfcf25832014-12-18 17:42:32 -0800541X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) { return ssl->param; }
Adam Langley95c29f32014-06-20 12:00:00 -0700542
Adam Langleyfcf25832014-12-18 17:42:32 -0800543void SSL_certs_clear(SSL *s) { ssl_cert_clear_certs(s->cert); }
Adam Langley95c29f32014-06-20 12:00:00 -0700544
Adam Langleyfcf25832014-12-18 17:42:32 -0800545void SSL_free(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800546 if (s == NULL) {
547 return;
548 }
Adam Langley95c29f32014-06-20 12:00:00 -0700549
Adam Langleyfcf25832014-12-18 17:42:32 -0800550 if (s->param) {
551 X509_VERIFY_PARAM_free(s->param);
552 }
Adam Langley95c29f32014-06-20 12:00:00 -0700553
Adam Langleyfcf25832014-12-18 17:42:32 -0800554 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700555
Adam Langleyfcf25832014-12-18 17:42:32 -0800556 if (s->bbio != NULL) {
557 /* If the buffering BIO is in place, pop it off */
558 if (s->bbio == s->wbio) {
559 s->wbio = BIO_pop(s->wbio);
560 }
561 BIO_free(s->bbio);
562 s->bbio = NULL;
563 }
Adam Langley95c29f32014-06-20 12:00:00 -0700564
Adam Langleyfcf25832014-12-18 17:42:32 -0800565 if (s->rbio != NULL) {
566 BIO_free_all(s->rbio);
567 }
Adam Langley95c29f32014-06-20 12:00:00 -0700568
Adam Langleyfcf25832014-12-18 17:42:32 -0800569 if (s->wbio != NULL && s->wbio != s->rbio) {
570 BIO_free_all(s->wbio);
571 }
Adam Langley95c29f32014-06-20 12:00:00 -0700572
Adam Langleyfcf25832014-12-18 17:42:32 -0800573 if (s->init_buf != NULL) {
574 BUF_MEM_free(s->init_buf);
575 }
Adam Langley95c29f32014-06-20 12:00:00 -0700576
Adam Langleyfcf25832014-12-18 17:42:32 -0800577 /* add extra stuff */
578 if (s->cipher_list != NULL) {
579 ssl_cipher_preference_list_free(s->cipher_list);
580 }
581 if (s->cipher_list_by_id != NULL) {
582 sk_SSL_CIPHER_free(s->cipher_list_by_id);
583 }
Adam Langley95c29f32014-06-20 12:00:00 -0700584
Adam Langleyfcf25832014-12-18 17:42:32 -0800585 if (s->session != NULL) {
586 ssl_clear_bad_session(s);
587 SSL_SESSION_free(s->session);
588 }
Adam Langley95c29f32014-06-20 12:00:00 -0700589
Adam Langleyfcf25832014-12-18 17:42:32 -0800590 ssl_clear_cipher_ctx(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700591
Adam Langleyfcf25832014-12-18 17:42:32 -0800592 if (s->cert != NULL) {
593 ssl_cert_free(s->cert);
594 }
Adam Langley0289c732014-06-20 12:00:00 -0700595
Adam Langleyfcf25832014-12-18 17:42:32 -0800596 if (s->tlsext_hostname) {
597 OPENSSL_free(s->tlsext_hostname);
598 }
599 if (s->initial_ctx) {
600 SSL_CTX_free(s->initial_ctx);
601 }
602 if (s->tlsext_ecpointformatlist) {
603 OPENSSL_free(s->tlsext_ecpointformatlist);
604 }
605 if (s->tlsext_ellipticcurvelist) {
606 OPENSSL_free(s->tlsext_ellipticcurvelist);
607 }
608 if (s->alpn_client_proto_list) {
609 OPENSSL_free(s->alpn_client_proto_list);
610 }
611 if (s->tlsext_channel_id_private) {
612 EVP_PKEY_free(s->tlsext_channel_id_private);
613 }
614 if (s->psk_identity_hint) {
615 OPENSSL_free(s->psk_identity_hint);
616 }
617 if (s->client_CA != NULL) {
618 sk_X509_NAME_pop_free(s->client_CA, X509_NAME_free);
619 }
620 if (s->next_proto_negotiated) {
621 OPENSSL_free(s->next_proto_negotiated);
622 }
623 if (s->srtp_profiles) {
624 sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);
625 }
Adam Langley95c29f32014-06-20 12:00:00 -0700626
Adam Langleyfcf25832014-12-18 17:42:32 -0800627 if (s->method != NULL) {
628 s->method->ssl_free(s);
629 }
630 if (s->ctx) {
631 SSL_CTX_free(s->ctx);
632 }
Adam Langley95c29f32014-06-20 12:00:00 -0700633
Adam Langleyfcf25832014-12-18 17:42:32 -0800634 OPENSSL_free(s);
635}
Adam Langley95c29f32014-06-20 12:00:00 -0700636
Adam Langleyfcf25832014-12-18 17:42:32 -0800637void SSL_set_bio(SSL *s, BIO *rbio, BIO *wbio) {
638 /* If the output buffering BIO is still in place, remove it. */
639 if (s->bbio != NULL) {
640 if (s->wbio == s->bbio) {
641 s->wbio = s->wbio->next_bio;
642 s->bbio->next_bio = NULL;
643 }
644 }
Adam Langley95c29f32014-06-20 12:00:00 -0700645
Adam Langleyfcf25832014-12-18 17:42:32 -0800646 if (s->rbio != NULL && s->rbio != rbio) {
647 BIO_free_all(s->rbio);
648 }
649 if (s->wbio != NULL && s->wbio != wbio && s->rbio != s->wbio) {
650 BIO_free_all(s->wbio);
651 }
652 s->rbio = rbio;
653 s->wbio = wbio;
654}
Adam Langley95c29f32014-06-20 12:00:00 -0700655
Adam Langleyfcf25832014-12-18 17:42:32 -0800656BIO *SSL_get_rbio(const SSL *s) { return s->rbio; }
Adam Langley95c29f32014-06-20 12:00:00 -0700657
Adam Langleyfcf25832014-12-18 17:42:32 -0800658BIO *SSL_get_wbio(const SSL *s) { return s->wbio; }
Adam Langley95c29f32014-06-20 12:00:00 -0700659
Adam Langleyfcf25832014-12-18 17:42:32 -0800660int SSL_get_fd(const SSL *s) { return SSL_get_rfd(s); }
Adam Langley95c29f32014-06-20 12:00:00 -0700661
Adam Langleyfcf25832014-12-18 17:42:32 -0800662int SSL_get_rfd(const SSL *s) {
663 int ret = -1;
664 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700665
Adam Langleyfcf25832014-12-18 17:42:32 -0800666 b = SSL_get_rbio(s);
667 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
668 if (r != NULL) {
669 BIO_get_fd(r, &ret);
670 }
671 return ret;
672}
Adam Langley95c29f32014-06-20 12:00:00 -0700673
Adam Langleyfcf25832014-12-18 17:42:32 -0800674int SSL_get_wfd(const SSL *s) {
675 int ret = -1;
676 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700677
Adam Langleyfcf25832014-12-18 17:42:32 -0800678 b = SSL_get_wbio(s);
679 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
680 if (r != NULL) {
681 BIO_get_fd(r, &ret);
682 }
Adam Langley95c29f32014-06-20 12:00:00 -0700683
Adam Langleyfcf25832014-12-18 17:42:32 -0800684 return ret;
685}
Adam Langley95c29f32014-06-20 12:00:00 -0700686
Adam Langleyfcf25832014-12-18 17:42:32 -0800687int SSL_set_fd(SSL *s, int fd) {
688 int ret = 0;
689 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700690
Adam Langleyfcf25832014-12-18 17:42:32 -0800691 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700692
Adam Langleyfcf25832014-12-18 17:42:32 -0800693 if (bio == NULL) {
694 OPENSSL_PUT_ERROR(SSL, SSL_set_fd, ERR_R_BUF_LIB);
695 goto err;
696 }
697 BIO_set_fd(bio, fd, BIO_NOCLOSE);
698 SSL_set_bio(s, bio, bio);
699 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700700
Adam Langley95c29f32014-06-20 12:00:00 -0700701err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800702 return ret;
703}
Adam Langley95c29f32014-06-20 12:00:00 -0700704
Adam Langleyfcf25832014-12-18 17:42:32 -0800705int SSL_set_wfd(SSL *s, int fd) {
706 int ret = 0;
707 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700708
Adam Langleyfcf25832014-12-18 17:42:32 -0800709 if (s->rbio == NULL || BIO_method_type(s->rbio) != BIO_TYPE_FD ||
710 (int)BIO_get_fd(s->rbio, NULL) != fd) {
711 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700712
Adam Langleyfcf25832014-12-18 17:42:32 -0800713 if (bio == NULL) {
714 OPENSSL_PUT_ERROR(SSL, SSL_set_wfd, ERR_R_BUF_LIB);
715 goto err;
716 }
717 BIO_set_fd(bio, fd, BIO_NOCLOSE);
718 SSL_set_bio(s, SSL_get_rbio(s), bio);
719 } else {
720 SSL_set_bio(s, SSL_get_rbio(s), SSL_get_rbio(s));
721 }
722
723 ret = 1;
724
Adam Langley95c29f32014-06-20 12:00:00 -0700725err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800726 return ret;
727}
Adam Langley95c29f32014-06-20 12:00:00 -0700728
Adam Langleyfcf25832014-12-18 17:42:32 -0800729int SSL_set_rfd(SSL *s, int fd) {
730 int ret = 0;
731 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700732
Adam Langleyfcf25832014-12-18 17:42:32 -0800733 if (s->wbio == NULL || BIO_method_type(s->wbio) != BIO_TYPE_FD ||
734 (int)BIO_get_fd(s->wbio, NULL) != fd) {
735 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700736
Adam Langleyfcf25832014-12-18 17:42:32 -0800737 if (bio == NULL) {
738 OPENSSL_PUT_ERROR(SSL, SSL_set_rfd, ERR_R_BUF_LIB);
739 goto err;
740 }
741 BIO_set_fd(bio, fd, BIO_NOCLOSE);
742 SSL_set_bio(s, bio, SSL_get_wbio(s));
743 } else {
744 SSL_set_bio(s, SSL_get_wbio(s), SSL_get_wbio(s));
745 }
746 ret = 1;
747
Adam Langley95c29f32014-06-20 12:00:00 -0700748err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800749 return ret;
750}
Adam Langley95c29f32014-06-20 12:00:00 -0700751
752/* return length of latest Finished message we sent, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -0800753size_t SSL_get_finished(const SSL *s, void *buf, size_t count) {
754 size_t ret = 0;
755
756 if (s->s3 != NULL) {
757 ret = s->s3->tmp.finish_md_len;
758 if (count > ret) {
759 count = ret;
760 }
761 memcpy(buf, s->s3->tmp.finish_md, count);
762 }
763
764 return ret;
765}
Adam Langley95c29f32014-06-20 12:00:00 -0700766
767/* return length of latest Finished message we expected, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -0800768size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count) {
769 size_t ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700770
Adam Langleyfcf25832014-12-18 17:42:32 -0800771 if (s->s3 != NULL) {
772 ret = s->s3->tmp.peer_finish_md_len;
773 if (count > ret) {
774 count = ret;
775 }
776 memcpy(buf, s->s3->tmp.peer_finish_md, count);
777 }
Adam Langley95c29f32014-06-20 12:00:00 -0700778
Adam Langleyfcf25832014-12-18 17:42:32 -0800779 return ret;
780}
Adam Langley95c29f32014-06-20 12:00:00 -0700781
Adam Langleyfcf25832014-12-18 17:42:32 -0800782int SSL_get_verify_mode(const SSL *s) { return s->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -0700783
Adam Langleyfcf25832014-12-18 17:42:32 -0800784int SSL_get_verify_depth(const SSL *s) {
785 return X509_VERIFY_PARAM_get_depth(s->param);
786}
Adam Langley95c29f32014-06-20 12:00:00 -0700787
Adam Langleyfcf25832014-12-18 17:42:32 -0800788int (*SSL_get_verify_callback(const SSL *s))(int, X509_STORE_CTX *) {
789 return s->verify_callback;
790}
Adam Langley95c29f32014-06-20 12:00:00 -0700791
Adam Langleyfcf25832014-12-18 17:42:32 -0800792int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) { return ctx->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -0700793
Adam Langleyfcf25832014-12-18 17:42:32 -0800794int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) {
795 return X509_VERIFY_PARAM_get_depth(ctx->param);
796}
Adam Langley95c29f32014-06-20 12:00:00 -0700797
Adam Langleyfcf25832014-12-18 17:42:32 -0800798int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int, X509_STORE_CTX *) {
799 return ctx->default_verify_callback;
800}
Adam Langley95c29f32014-06-20 12:00:00 -0700801
Adam Langleyfcf25832014-12-18 17:42:32 -0800802void SSL_set_verify(SSL *s, int mode,
803 int (*callback)(int ok, X509_STORE_CTX *ctx)) {
804 s->verify_mode = mode;
805 if (callback != NULL) {
806 s->verify_callback = callback;
807 }
808}
Adam Langley95c29f32014-06-20 12:00:00 -0700809
Adam Langleyfcf25832014-12-18 17:42:32 -0800810void SSL_set_verify_depth(SSL *s, int depth) {
811 X509_VERIFY_PARAM_set_depth(s->param, depth);
812}
Adam Langley95c29f32014-06-20 12:00:00 -0700813
Adam Langleyfcf25832014-12-18 17:42:32 -0800814void SSL_set_read_ahead(SSL *s, int yes) { s->read_ahead = yes; }
Adam Langley95c29f32014-06-20 12:00:00 -0700815
Adam Langleyfcf25832014-12-18 17:42:32 -0800816int SSL_get_read_ahead(const SSL *s) { return s->read_ahead; }
Adam Langley95c29f32014-06-20 12:00:00 -0700817
Adam Langleyfcf25832014-12-18 17:42:32 -0800818int SSL_pending(const SSL *s) {
819 /* SSL_pending cannot work properly if read-ahead is enabled
820 * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)), and it is
821 * impossible to fix since SSL_pending cannot report errors that may be
822 * observed while scanning the new data. (Note that SSL_pending() is often
823 * used as a boolean value, so we'd better not return -1.). */
824 return s->method->ssl_pending(s);
825}
Adam Langley95c29f32014-06-20 12:00:00 -0700826
Adam Langleyfcf25832014-12-18 17:42:32 -0800827X509 *SSL_get_peer_certificate(const SSL *s) {
828 X509 *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700829
Adam Langleyfcf25832014-12-18 17:42:32 -0800830 if (s == NULL || s->session == NULL) {
831 r = NULL;
832 } else {
833 r = s->session->peer;
834 }
Adam Langley95c29f32014-06-20 12:00:00 -0700835
Adam Langleyfcf25832014-12-18 17:42:32 -0800836 if (r == NULL) {
837 return NULL;
838 }
Adam Langley95c29f32014-06-20 12:00:00 -0700839
Adam Langleyfcf25832014-12-18 17:42:32 -0800840 return X509_up_ref(r);
841}
842
843STACK_OF(X509) * SSL_get_peer_cert_chain(const SSL *s) {
844 STACK_OF(X509) * r;
845
846 if (s == NULL || s->session == NULL || s->session->sess_cert == NULL) {
847 r = NULL;
848 } else {
849 r = s->session->sess_cert->cert_chain;
850 }
851
852 /* If we are a client, cert_chain includes the peer's own certificate; if we
853 * are a server, it does not. */
854 return r;
855}
Adam Langley95c29f32014-06-20 12:00:00 -0700856
Adam Langley95c29f32014-06-20 12:00:00 -0700857/* Fix this so it checks all the valid key/cert options */
Adam Langleyfcf25832014-12-18 17:42:32 -0800858int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
859 if (ctx == NULL || ctx->cert == NULL || ctx->cert->key->x509 == NULL) {
860 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key,
861 SSL_R_NO_CERTIFICATE_ASSIGNED);
862 return 0;
863 }
864
865 if (ctx->cert->key->privatekey == NULL) {
866 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key,
867 SSL_R_NO_PRIVATE_KEY_ASSIGNED);
868 return 0;
869 }
870
871 return X509_check_private_key(ctx->cert->key->x509,
872 ctx->cert->key->privatekey);
873}
Adam Langley95c29f32014-06-20 12:00:00 -0700874
875/* Fix this function so that it takes an optional type parameter */
Adam Langleyfcf25832014-12-18 17:42:32 -0800876int SSL_check_private_key(const SSL *ssl) {
877 if (ssl == NULL) {
878 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, ERR_R_PASSED_NULL_PARAMETER);
879 return 0;
880 }
Adam Langley95c29f32014-06-20 12:00:00 -0700881
Adam Langleyfcf25832014-12-18 17:42:32 -0800882 if (ssl->cert == NULL) {
883 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
884 SSL_R_NO_CERTIFICATE_ASSIGNED);
885 return 0;
886 }
Adam Langley95c29f32014-06-20 12:00:00 -0700887
Adam Langleyfcf25832014-12-18 17:42:32 -0800888 if (ssl->cert->key->x509 == NULL) {
889 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
890 SSL_R_NO_CERTIFICATE_ASSIGNED);
891 return 0;
892 }
David Benjamin0b145c22014-11-26 20:10:09 -0500893
Adam Langleyfcf25832014-12-18 17:42:32 -0800894 if (ssl->cert->key->privatekey == NULL) {
895 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
896 SSL_R_NO_PRIVATE_KEY_ASSIGNED);
897 return 0;
898 }
Adam Langley95c29f32014-06-20 12:00:00 -0700899
Adam Langleyfcf25832014-12-18 17:42:32 -0800900 return X509_check_private_key(ssl->cert->key->x509,
901 ssl->cert->key->privatekey);
902}
Adam Langley95c29f32014-06-20 12:00:00 -0700903
Adam Langleyfcf25832014-12-18 17:42:32 -0800904int SSL_accept(SSL *s) {
905 if (s->handshake_func == 0) {
906 /* Not properly initialized yet */
907 SSL_set_accept_state(s);
908 }
David Benjamin0b145c22014-11-26 20:10:09 -0500909
Adam Langleyfcf25832014-12-18 17:42:32 -0800910 if (s->handshake_func != s->method->ssl_accept) {
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 -0800918int SSL_connect(SSL *s) {
919 if (s->handshake_func == 0) {
920 /* Not properly initialized yet */
921 SSL_set_connect_state(s);
922 }
Adam Langley95c29f32014-06-20 12:00:00 -0700923
Adam Langleyfcf25832014-12-18 17:42:32 -0800924 if (s->handshake_func != s->method->ssl_connect) {
925 OPENSSL_PUT_ERROR(SSL, SSL_connect, ERR_R_INTERNAL_ERROR);
926 return -1;
927 }
Adam Langley95c29f32014-06-20 12:00:00 -0700928
Adam Langleyfcf25832014-12-18 17:42:32 -0800929 return s->handshake_func(s);
930}
Adam Langley95c29f32014-06-20 12:00:00 -0700931
Adam Langleyfcf25832014-12-18 17:42:32 -0800932long SSL_get_default_timeout(const SSL *s) {
933 return SSL_DEFAULT_SESSION_TIMEOUT;
934}
Adam Langley95c29f32014-06-20 12:00:00 -0700935
Adam Langleyfcf25832014-12-18 17:42:32 -0800936int SSL_read(SSL *s, void *buf, int num) {
937 if (s->handshake_func == 0) {
938 OPENSSL_PUT_ERROR(SSL, SSL_read, 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 s->rwstate = SSL_NOTHING;
944 return 0;
945 }
Adam Langley95c29f32014-06-20 12:00:00 -0700946
Adam Langleyfcf25832014-12-18 17:42:32 -0800947 return s->method->ssl_read(s, buf, num);
948}
Adam Langley95c29f32014-06-20 12:00:00 -0700949
Adam Langleyfcf25832014-12-18 17:42:32 -0800950int SSL_peek(SSL *s, void *buf, int num) {
951 if (s->handshake_func == 0) {
952 OPENSSL_PUT_ERROR(SSL, SSL_peek, SSL_R_UNINITIALIZED);
953 return -1;
954 }
Adam Langley95c29f32014-06-20 12:00:00 -0700955
Adam Langleyfcf25832014-12-18 17:42:32 -0800956 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
957 return 0;
958 }
Adam Langley95c29f32014-06-20 12:00:00 -0700959
Adam Langleyfcf25832014-12-18 17:42:32 -0800960 return s->method->ssl_peek(s, buf, num);
961}
Adam Langley95c29f32014-06-20 12:00:00 -0700962
Adam Langleyfcf25832014-12-18 17:42:32 -0800963int SSL_write(SSL *s, const void *buf, int num) {
964 if (s->handshake_func == 0) {
965 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_UNINITIALIZED);
966 return -1;
967 }
Adam Langley95c29f32014-06-20 12:00:00 -0700968
Adam Langleyfcf25832014-12-18 17:42:32 -0800969 if (s->shutdown & SSL_SENT_SHUTDOWN) {
970 s->rwstate = SSL_NOTHING;
971 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_PROTOCOL_IS_SHUTDOWN);
972 return -1;
973 }
Adam Langley95c29f32014-06-20 12:00:00 -0700974
Adam Langleyfcf25832014-12-18 17:42:32 -0800975 return s->method->ssl_write(s, buf, num);
976}
Adam Langley95c29f32014-06-20 12:00:00 -0700977
Adam Langleyfcf25832014-12-18 17:42:32 -0800978int SSL_shutdown(SSL *s) {
979 /* Note that this function behaves differently from what one might expect.
980 * Return values are 0 for no success (yet), 1 for success; but calling it
981 * once is usually not enough, even if blocking I/O is used (see
982 * ssl3_shutdown). */
Adam Langley95c29f32014-06-20 12:00:00 -0700983
Adam Langleyfcf25832014-12-18 17:42:32 -0800984 if (s->handshake_func == 0) {
985 OPENSSL_PUT_ERROR(SSL, SSL_shutdown, SSL_R_UNINITIALIZED);
986 return -1;
987 }
Adam Langley95c29f32014-06-20 12:00:00 -0700988
Adam Langleyfcf25832014-12-18 17:42:32 -0800989 if (!SSL_in_init(s)) {
990 return s->method->ssl_shutdown(s);
991 }
Adam Langley95c29f32014-06-20 12:00:00 -0700992
Adam Langleyfcf25832014-12-18 17:42:32 -0800993 return 1;
994}
Adam Langley95c29f32014-06-20 12:00:00 -0700995
Adam Langleyfcf25832014-12-18 17:42:32 -0800996int SSL_renegotiate(SSL *s) {
David Benjamin2cdace92015-01-24 13:30:02 -0500997 if (SSL_IS_DTLS(s)) {
998 /* Renegotiation is not supported for DTLS. */
999 OPENSSL_PUT_ERROR(SSL, SSL_renegotiate, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1000 return 0;
1001 }
1002
Adam Langleyfcf25832014-12-18 17:42:32 -08001003 if (s->renegotiate == 0) {
1004 s->renegotiate = 1;
1005 }
Adam Langley95c29f32014-06-20 12:00:00 -07001006
Adam Langleyfcf25832014-12-18 17:42:32 -08001007 s->new_session = 1;
1008 return s->method->ssl_renegotiate(s);
1009}
Adam Langley95c29f32014-06-20 12:00:00 -07001010
Adam Langleyfcf25832014-12-18 17:42:32 -08001011int SSL_renegotiate_pending(SSL *s) {
1012 /* becomes true when negotiation is requested; false again once a handshake
1013 * has finished */
1014 return s->renegotiate != 0;
1015}
Adam Langley95c29f32014-06-20 12:00:00 -07001016
Adam Langleyfcf25832014-12-18 17:42:32 -08001017long SSL_ctrl(SSL *s, int cmd, long larg, void *parg) {
1018 long l;
Adam Langley95c29f32014-06-20 12:00:00 -07001019
Adam Langleyfcf25832014-12-18 17:42:32 -08001020 switch (cmd) {
1021 case SSL_CTRL_GET_READ_AHEAD:
1022 return s->read_ahead;
Adam Langley95c29f32014-06-20 12:00:00 -07001023
Adam Langleyfcf25832014-12-18 17:42:32 -08001024 case SSL_CTRL_SET_READ_AHEAD:
1025 l = s->read_ahead;
1026 s->read_ahead = larg;
1027 return l;
Adam Langley95c29f32014-06-20 12:00:00 -07001028
Adam Langleyfcf25832014-12-18 17:42:32 -08001029 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1030 s->msg_callback_arg = parg;
1031 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001032
Adam Langleyfcf25832014-12-18 17:42:32 -08001033 case SSL_CTRL_OPTIONS:
1034 return s->options |= larg;
Adam Langley95c29f32014-06-20 12:00:00 -07001035
Adam Langleyfcf25832014-12-18 17:42:32 -08001036 case SSL_CTRL_CLEAR_OPTIONS:
1037 return s->options &= ~larg;
Adam Langley95c29f32014-06-20 12:00:00 -07001038
Adam Langleyfcf25832014-12-18 17:42:32 -08001039 case SSL_CTRL_MODE:
1040 return s->mode |= larg;
Adam Langley95c29f32014-06-20 12:00:00 -07001041
Adam Langleyfcf25832014-12-18 17:42:32 -08001042 case SSL_CTRL_CLEAR_MODE:
1043 return s->mode &= ~larg;
Adam Langley95c29f32014-06-20 12:00:00 -07001044
Adam Langleyfcf25832014-12-18 17:42:32 -08001045 case SSL_CTRL_GET_MAX_CERT_LIST:
1046 return s->max_cert_list;
Adam Langley95c29f32014-06-20 12:00:00 -07001047
Adam Langleyfcf25832014-12-18 17:42:32 -08001048 case SSL_CTRL_SET_MAX_CERT_LIST:
1049 l = s->max_cert_list;
1050 s->max_cert_list = larg;
1051 return l;
Adam Langley95c29f32014-06-20 12:00:00 -07001052
Adam Langleyfcf25832014-12-18 17:42:32 -08001053 case SSL_CTRL_SET_MTU:
1054 if (larg < (long)dtls1_min_mtu()) {
1055 return 0;
1056 }
1057 if (SSL_IS_DTLS(s)) {
1058 s->d1->mtu = larg;
1059 return larg;
1060 }
1061 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001062
Adam Langleyfcf25832014-12-18 17:42:32 -08001063 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1064 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH) {
1065 return 0;
1066 }
1067 s->max_send_fragment = larg;
1068 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001069
Adam Langleyfcf25832014-12-18 17:42:32 -08001070 case SSL_CTRL_GET_RI_SUPPORT:
1071 if (s->s3) {
1072 return s->s3->send_connection_binding;
1073 }
1074 return 0;
1075
1076 case SSL_CTRL_CERT_FLAGS:
1077 return s->cert->cert_flags |= larg;
1078
1079 case SSL_CTRL_CLEAR_CERT_FLAGS:
1080 return s->cert->cert_flags &= ~larg;
1081
1082 case SSL_CTRL_GET_RAW_CIPHERLIST:
1083 if (parg) {
1084 if (s->cert->ciphers_raw == NULL) {
1085 return 0;
1086 }
1087 *(uint8_t **)parg = s->cert->ciphers_raw;
1088 return (int)s->cert->ciphers_rawlen;
1089 }
1090
1091 /* Passing a NULL |parg| returns the size of a single
1092 * cipher suite value. */
1093 return 2;
1094
1095 default:
1096 return s->method->ssl_ctrl(s, cmd, larg, parg);
1097 }
1098}
1099
1100long SSL_callback_ctrl(SSL *s, int cmd, void (*fp)(void)) {
1101 switch (cmd) {
1102 case SSL_CTRL_SET_MSG_CALLBACK:
1103 s->msg_callback =
1104 (void (*)(int write_p, int version, int content_type, const void *buf,
1105 size_t len, SSL *ssl, void *arg))(fp);
1106 return 1;
1107
1108 default:
1109 return s->method->ssl_callback_ctrl(s, cmd, fp);
1110 }
1111}
1112
1113LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) { return ctx->sessions; }
1114
1115long SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg) {
1116 long l;
1117
1118 switch (cmd) {
1119 case SSL_CTRL_GET_READ_AHEAD:
1120 return ctx->read_ahead;
1121
1122 case SSL_CTRL_SET_READ_AHEAD:
1123 l = ctx->read_ahead;
1124 ctx->read_ahead = larg;
1125 return l;
1126
1127 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1128 ctx->msg_callback_arg = parg;
1129 return 1;
1130
1131 case SSL_CTRL_GET_MAX_CERT_LIST:
1132 return ctx->max_cert_list;
1133
1134 case SSL_CTRL_SET_MAX_CERT_LIST:
1135 l = ctx->max_cert_list;
1136 ctx->max_cert_list = larg;
1137 return l;
1138
1139 case SSL_CTRL_SET_SESS_CACHE_SIZE:
1140 l = ctx->session_cache_size;
1141 ctx->session_cache_size = larg;
1142 return l;
1143
1144 case SSL_CTRL_GET_SESS_CACHE_SIZE:
1145 return ctx->session_cache_size;
1146
1147 case SSL_CTRL_SET_SESS_CACHE_MODE:
1148 l = ctx->session_cache_mode;
1149 ctx->session_cache_mode = larg;
1150 return l;
1151
1152 case SSL_CTRL_GET_SESS_CACHE_MODE:
1153 return ctx->session_cache_mode;
1154
1155 case SSL_CTRL_SESS_NUMBER:
1156 return lh_SSL_SESSION_num_items(ctx->sessions);
1157
1158 case SSL_CTRL_SESS_CONNECT:
1159 return ctx->stats.sess_connect;
1160
1161 case SSL_CTRL_SESS_CONNECT_GOOD:
1162 return ctx->stats.sess_connect_good;
1163
1164 case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
1165 return ctx->stats.sess_connect_renegotiate;
1166
1167 case SSL_CTRL_SESS_ACCEPT:
1168 return ctx->stats.sess_accept;
1169
1170 case SSL_CTRL_SESS_ACCEPT_GOOD:
1171 return ctx->stats.sess_accept_good;
1172
1173 case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
1174 return ctx->stats.sess_accept_renegotiate;
1175
1176 case SSL_CTRL_SESS_HIT:
1177 return ctx->stats.sess_hit;
1178
1179 case SSL_CTRL_SESS_CB_HIT:
1180 return ctx->stats.sess_cb_hit;
1181
1182 case SSL_CTRL_SESS_MISSES:
1183 return ctx->stats.sess_miss;
1184
1185 case SSL_CTRL_SESS_TIMEOUTS:
1186 return ctx->stats.sess_timeout;
1187
1188 case SSL_CTRL_SESS_CACHE_FULL:
1189 return ctx->stats.sess_cache_full;
1190
1191 case SSL_CTRL_OPTIONS:
1192 return ctx->options |= larg;
1193
1194 case SSL_CTRL_CLEAR_OPTIONS:
1195 return ctx->options &= ~larg;
1196
1197 case SSL_CTRL_MODE:
1198 return ctx->mode |= larg;
1199
1200 case SSL_CTRL_CLEAR_MODE:
1201 return ctx->mode &= ~larg;
1202
1203 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1204 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH) {
1205 return 0;
1206 }
1207 ctx->max_send_fragment = larg;
1208 return 1;
1209
1210 case SSL_CTRL_CERT_FLAGS:
1211 return ctx->cert->cert_flags |= larg;
1212
1213 case SSL_CTRL_CLEAR_CERT_FLAGS:
1214 return ctx->cert->cert_flags &= ~larg;
1215
1216 default:
1217 return ctx->method->ssl_ctx_ctrl(ctx, cmd, larg, parg);
1218 }
1219}
1220
1221long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void)) {
1222 switch (cmd) {
1223 case SSL_CTRL_SET_MSG_CALLBACK:
1224 ctx->msg_callback =
1225 (void (*)(int write_p, int version, int content_type, const void *buf,
1226 size_t len, SSL *ssl, void *arg))(fp);
1227 return 1;
1228
1229 default:
1230 return ctx->method->ssl_ctx_callback_ctrl(ctx, cmd, fp);
1231 }
1232}
1233
1234int ssl_cipher_id_cmp(const void *in_a, const void *in_b) {
1235 long l;
1236 const SSL_CIPHER *a = in_a;
1237 const SSL_CIPHER *b = in_b;
1238 const long a_id = a->id;
1239 const long b_id = b->id;
1240
1241 l = a_id - b_id;
1242 if (l == 0L) {
1243 return 0;
1244 } else {
1245 return (l > 0) ? 1 : -1;
1246 }
1247}
1248
1249int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **ap, const SSL_CIPHER **bp) {
1250 long l;
1251 const long a_id = (*ap)->id;
1252 const long b_id = (*bp)->id;
1253
1254 l = a_id - b_id;
1255 if (l == 0) {
1256 return 0;
1257 } else {
1258 return (l > 0) ? 1 : -1;
1259 }
1260}
1261
1262/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001263 * preference */
Adam Langleyfcf25832014-12-18 17:42:32 -08001264STACK_OF(SSL_CIPHER) * SSL_get_ciphers(const SSL *s) {
1265 if (s == NULL) {
1266 return NULL;
1267 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001268
Adam Langleyfcf25832014-12-18 17:42:32 -08001269 if (s->cipher_list != NULL) {
1270 return s->cipher_list->ciphers;
1271 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001272
Adam Langleyfcf25832014-12-18 17:42:32 -08001273 if (s->version >= TLS1_1_VERSION && s->ctx != NULL &&
1274 s->ctx->cipher_list_tls11 != NULL) {
1275 return s->ctx->cipher_list_tls11->ciphers;
1276 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001277
Adam Langleyfcf25832014-12-18 17:42:32 -08001278 if (s->ctx != NULL && s->ctx->cipher_list != NULL) {
1279 return s->ctx->cipher_list->ciphers;
1280 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001281
Adam Langleyfcf25832014-12-18 17:42:32 -08001282 return NULL;
1283}
Adam Langley95c29f32014-06-20 12:00:00 -07001284
Adam Langleyfcf25832014-12-18 17:42:32 -08001285/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001286 * algorithm id */
Adam Langleyfcf25832014-12-18 17:42:32 -08001287STACK_OF(SSL_CIPHER) * ssl_get_ciphers_by_id(SSL *s) {
1288 if (s == NULL) {
1289 return NULL;
1290 }
Adam Langley95c29f32014-06-20 12:00:00 -07001291
Adam Langleyfcf25832014-12-18 17:42:32 -08001292 if (s->cipher_list_by_id != NULL) {
1293 return s->cipher_list_by_id;
1294 }
Adam Langley95c29f32014-06-20 12:00:00 -07001295
Adam Langleyfcf25832014-12-18 17:42:32 -08001296 if (s->ctx != NULL && s->ctx->cipher_list_by_id != NULL) {
1297 return s->ctx->cipher_list_by_id;
1298 }
Adam Langley95c29f32014-06-20 12:00:00 -07001299
Adam Langleyfcf25832014-12-18 17:42:32 -08001300 return NULL;
1301}
Adam Langley95c29f32014-06-20 12:00:00 -07001302
Adam Langleyfcf25832014-12-18 17:42:32 -08001303/* The old interface to get the same thing as SSL_get_ciphers() */
1304const char *SSL_get_cipher_list(const SSL *s, int n) {
1305 const SSL_CIPHER *c;
1306 STACK_OF(SSL_CIPHER) * sk;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001307
Adam Langleyfcf25832014-12-18 17:42:32 -08001308 if (s == NULL) {
1309 return NULL;
1310 }
Adam Langley95c29f32014-06-20 12:00:00 -07001311
Adam Langleyfcf25832014-12-18 17:42:32 -08001312 sk = SSL_get_ciphers(s);
1313 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
1314 return NULL;
1315 }
Adam Langley95c29f32014-06-20 12:00:00 -07001316
Adam Langleyfcf25832014-12-18 17:42:32 -08001317 c = sk_SSL_CIPHER_value(sk, n);
1318 if (c == NULL) {
1319 return NULL;
1320 }
Adam Langley95c29f32014-06-20 12:00:00 -07001321
Adam Langleyfcf25832014-12-18 17:42:32 -08001322 return c->name;
1323}
David Benjamin5491e3f2014-09-29 19:33:09 -04001324
Adam Langleyfcf25832014-12-18 17:42:32 -08001325/* specify the ciphers to be used by default by the SSL_CTX */
1326int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
1327 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001328
Adam Langleyfcf25832014-12-18 17:42:32 -08001329 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list,
1330 &ctx->cipher_list_by_id, str, ctx->cert);
1331 /* ssl_create_cipher_list may return an empty stack if it was unable to find
1332 * a cipher matching the given rule string (for example if the rule string
1333 * specifies a cipher which has been disabled). This is not an error as far
1334 * as ssl_create_cipher_list is concerned, and hence ctx->cipher_list and
1335 * ctx->cipher_list_by_id has been updated. */
1336 if (sk == NULL) {
1337 return 0;
1338 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1339 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1340 return 0;
1341 }
Adam Langley95c29f32014-06-20 12:00:00 -07001342
Adam Langleyfcf25832014-12-18 17:42:32 -08001343 return 1;
1344}
David Benjamin39482a12014-07-20 13:30:15 -04001345
Adam Langleyfcf25832014-12-18 17:42:32 -08001346int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str) {
1347 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001348
Adam Langleyfcf25832014-12-18 17:42:32 -08001349 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str,
1350 ctx->cert);
1351 if (sk == NULL) {
1352 return 0;
1353 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1354 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list_tls11,
1355 SSL_R_NO_CIPHER_MATCH);
1356 return 0;
1357 }
David Benjamin9f2c0d72014-10-21 22:00:19 -04001358
Adam Langleyfcf25832014-12-18 17:42:32 -08001359 return 1;
1360}
Adam Langley95c29f32014-06-20 12:00:00 -07001361
Adam Langleyfcf25832014-12-18 17:42:32 -08001362/* specify the ciphers to be used by the SSL */
1363int SSL_set_cipher_list(SSL *s, const char *str) {
1364 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001365
Adam Langleyfcf25832014-12-18 17:42:32 -08001366 sk = ssl_create_cipher_list(s->ctx->method, &s->cipher_list,
1367 &s->cipher_list_by_id, str, s->cert);
David Benjamin39482a12014-07-20 13:30:15 -04001368
Adam Langleyfcf25832014-12-18 17:42:32 -08001369 /* see comment in SSL_CTX_set_cipher_list */
1370 if (sk == NULL) {
1371 return 0;
1372 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1373 OPENSSL_PUT_ERROR(SSL, SSL_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1374 return 0;
1375 }
David Benjamin39482a12014-07-20 13:30:15 -04001376
Adam Langleyfcf25832014-12-18 17:42:32 -08001377 return 1;
1378}
Adam Langley95c29f32014-06-20 12:00:00 -07001379
Adam Langleyfcf25832014-12-18 17:42:32 -08001380int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, uint8_t *p) {
1381 size_t i;
1382 const SSL_CIPHER *c;
1383 CERT *ct = s->cert;
1384 uint8_t *q;
1385 /* Set disabled masks for this session */
1386 ssl_set_client_disabled(s);
Adam Langley29707792014-06-20 12:00:00 -07001387
Adam Langleyfcf25832014-12-18 17:42:32 -08001388 if (sk == NULL) {
1389 return 0;
1390 }
1391 q = p;
Adam Langley95c29f32014-06-20 12:00:00 -07001392
Adam Langleyfcf25832014-12-18 17:42:32 -08001393 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
1394 c = sk_SSL_CIPHER_value(sk, i);
1395 /* Skip disabled ciphers */
1396 if (c->algorithm_ssl & ct->mask_ssl ||
1397 c->algorithm_mkey & ct->mask_k ||
1398 c->algorithm_auth & ct->mask_a) {
1399 continue;
1400 }
1401 s2n(ssl3_get_cipher_value(c), p);
1402 }
1403
1404 /* If all ciphers were disabled, return the error to the caller. */
1405 if (p == q) {
1406 return 0;
1407 }
1408
1409 /* Add SCSVs. */
1410 if (!s->renegotiate) {
1411 s2n(SSL3_CK_SCSV & 0xffff, p);
1412 }
1413
1414 if (s->fallback_scsv) {
1415 s2n(SSL3_CK_FALLBACK_SCSV & 0xffff, p);
1416 }
1417
1418 return p - q;
1419}
1420
1421STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs) {
1422 CBS cipher_suites = *cbs;
1423 const SSL_CIPHER *c;
1424 STACK_OF(SSL_CIPHER) * sk;
1425
1426 if (s->s3) {
1427 s->s3->send_connection_binding = 0;
1428 }
1429
1430 if (CBS_len(&cipher_suites) % 2 != 0) {
1431 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1432 SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
1433 return NULL;
1434 }
1435
1436 sk = sk_SSL_CIPHER_new_null();
1437 if (sk == NULL) {
1438 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1439 goto err;
1440 }
1441
1442 if (!CBS_stow(&cipher_suites, &s->cert->ciphers_raw,
1443 &s->cert->ciphers_rawlen)) {
1444 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1445 goto err;
1446 }
1447
1448 while (CBS_len(&cipher_suites) > 0) {
1449 uint16_t cipher_suite;
1450
1451 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
1452 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_INTERNAL_ERROR);
1453 goto err;
1454 }
1455
1456 /* Check for SCSV. */
1457 if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff)) {
1458 /* SCSV is fatal if renegotiating. */
1459 if (s->renegotiate) {
1460 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1461 SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1462 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1463 goto err;
1464 }
1465 s->s3->send_connection_binding = 1;
1466 continue;
1467 }
1468
1469 /* Check for FALLBACK_SCSV. */
1470 if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff)) {
1471 uint16_t max_version = ssl3_get_max_server_version(s);
1472 if (SSL_IS_DTLS(s) ? (uint16_t)s->version > max_version
1473 : (uint16_t)s->version < max_version) {
1474 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1475 SSL_R_INAPPROPRIATE_FALLBACK);
1476 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
1477 goto err;
1478 }
1479 continue;
1480 }
1481
1482 c = ssl3_get_cipher_by_value(cipher_suite);
1483 if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
1484 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1485 goto err;
1486 }
1487 }
1488
1489 return sk;
David Benjamin9f2c0d72014-10-21 22:00:19 -04001490
Adam Langley95c29f32014-06-20 12:00:00 -07001491err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001492 if (sk != NULL) {
1493 sk_SSL_CIPHER_free(sk);
1494 }
1495 return NULL;
1496}
Adam Langley95c29f32014-06-20 12:00:00 -07001497
1498
Adam Langleyfcf25832014-12-18 17:42:32 -08001499/* return a servername extension value if provided in Client Hello, or NULL. So
1500 * far, only host_name types are defined (RFC 3546). */
1501const char *SSL_get_servername(const SSL *s, const int type) {
1502 if (type != TLSEXT_NAMETYPE_host_name) {
1503 return NULL;
1504 }
Adam Langley95c29f32014-06-20 12:00:00 -07001505
Adam Langleyfcf25832014-12-18 17:42:32 -08001506 return s->session && !s->tlsext_hostname ? s->session->tlsext_hostname
1507 : s->tlsext_hostname;
1508}
Adam Langley95c29f32014-06-20 12:00:00 -07001509
Adam Langleyfcf25832014-12-18 17:42:32 -08001510int SSL_get_servername_type(const SSL *s) {
1511 if (s->session &&
1512 (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname)) {
1513 return TLSEXT_NAMETYPE_host_name;
1514 }
Adam Langley95c29f32014-06-20 12:00:00 -07001515
Adam Langleyfcf25832014-12-18 17:42:32 -08001516 return -1;
1517}
Adam Langley95c29f32014-06-20 12:00:00 -07001518
Adam Langleyfcf25832014-12-18 17:42:32 -08001519void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
1520 ctx->signed_cert_timestamps_enabled = 1;
1521}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001522
Adam Langleyfcf25832014-12-18 17:42:32 -08001523int SSL_enable_signed_cert_timestamps(SSL *ssl) {
1524 ssl->signed_cert_timestamps_enabled = 1;
1525 return 1;
1526}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001527
Adam Langleyfcf25832014-12-18 17:42:32 -08001528void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
1529 ctx->ocsp_stapling_enabled = 1;
1530}
David Benjamin6c7aed02014-08-27 16:42:38 -04001531
Adam Langleyfcf25832014-12-18 17:42:32 -08001532int SSL_enable_ocsp_stapling(SSL *ssl) {
1533 ssl->ocsp_stapling_enabled = 1;
1534 return 1;
1535}
David Benjamin6c7aed02014-08-27 16:42:38 -04001536
Adam Langleyfcf25832014-12-18 17:42:32 -08001537void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
1538 size_t *out_len) {
1539 SSL_SESSION *session = ssl->session;
Adam Langley3cb50e02014-08-26 14:00:31 -07001540
Adam Langleyfcf25832014-12-18 17:42:32 -08001541 *out_len = 0;
1542 *out = NULL;
1543 if (ssl->server || !session || !session->tlsext_signed_cert_timestamp_list) {
1544 return;
1545 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001546
Adam Langleyfcf25832014-12-18 17:42:32 -08001547 *out = session->tlsext_signed_cert_timestamp_list;
1548 *out_len = session->tlsext_signed_cert_timestamp_list_length;
1549}
David Benjamin6c7aed02014-08-27 16:42:38 -04001550
Adam Langleyfcf25832014-12-18 17:42:32 -08001551void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
1552 size_t *out_len) {
1553 SSL_SESSION *session = ssl->session;
1554
1555 *out_len = 0;
1556 *out = NULL;
1557 if (ssl->server || !session || !session->ocsp_response) {
1558 return;
1559 }
1560 *out = session->ocsp_response;
1561 *out_len = session->ocsp_response_length;
1562}
David Benjamin6c7aed02014-08-27 16:42:38 -04001563
Adam Langley95c29f32014-06-20 12:00:00 -07001564/* SSL_select_next_proto implements the standard protocol selection. It is
1565 * expected that this function is called from the callback set by
1566 * SSL_CTX_set_next_proto_select_cb.
1567 *
1568 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1569 * strings. The length byte itself is not included in the length. A byte
1570 * string of length 0 is invalid. No byte string may be truncated.
1571 *
1572 * The current, but experimental algorithm for selecting the protocol is:
1573 *
1574 * 1) If the server doesn't support NPN then this is indicated to the
1575 * callback. In this case, the client application has to abort the connection
1576 * or have a default application level protocol.
1577 *
1578 * 2) If the server supports NPN, but advertises an empty list then the
1579 * client selects the first protcol in its list, but indicates via the
1580 * API that this fallback case was enacted.
1581 *
1582 * 3) Otherwise, the client finds the first protocol in the server's list
1583 * that it supports and selects this protocol. This is because it's
1584 * assumed that the server has better information about which protocol
1585 * a client should use.
1586 *
1587 * 4) If the client doesn't support any of the server's advertised
1588 * protocols, then this is treated the same as case 2.
1589 *
1590 * It returns either
1591 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1592 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1593 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001594int SSL_select_next_proto(uint8_t **out, uint8_t *outlen, const uint8_t *server,
1595 unsigned int server_len, const uint8_t *client,
1596 unsigned int client_len) {
1597 unsigned int i, j;
1598 const uint8_t *result;
1599 int status = OPENSSL_NPN_UNSUPPORTED;
Adam Langley95c29f32014-06-20 12:00:00 -07001600
Adam Langleyfcf25832014-12-18 17:42:32 -08001601 /* For each protocol in server preference order, see if we support it. */
1602 for (i = 0; i < server_len;) {
1603 for (j = 0; j < client_len;) {
1604 if (server[i] == client[j] &&
1605 memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
1606 /* We found a match */
1607 result = &server[i];
1608 status = OPENSSL_NPN_NEGOTIATED;
1609 goto found;
1610 }
1611 j += client[j];
1612 j++;
1613 }
1614 i += server[i];
1615 i++;
1616 }
Adam Langley95c29f32014-06-20 12:00:00 -07001617
Adam Langleyfcf25832014-12-18 17:42:32 -08001618 /* There's no overlap between our protocols and the server's list. */
1619 result = client;
1620 status = OPENSSL_NPN_NO_OVERLAP;
Adam Langley95c29f32014-06-20 12:00:00 -07001621
Adam Langleyfcf25832014-12-18 17:42:32 -08001622found:
1623 *out = (uint8_t *)result + 1;
1624 *outlen = result[0];
1625 return status;
1626}
Adam Langley95c29f32014-06-20 12:00:00 -07001627
Adam Langley95c29f32014-06-20 12:00:00 -07001628/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
1629 * requested protocol for this connection and returns 0. If the client didn't
1630 * request any protocol, then *data is set to NULL.
1631 *
1632 * Note that the client can request any protocol it chooses. The value returned
1633 * from this function need not be a member of the list of supported protocols
Adam Langleyfcf25832014-12-18 17:42:32 -08001634 * provided by the callback. */
1635void SSL_get0_next_proto_negotiated(const SSL *s, const uint8_t **data,
1636 unsigned *len) {
1637 *data = s->next_proto_negotiated;
1638 if (!*data) {
1639 *len = 0;
1640 } else {
1641 *len = s->next_proto_negotiated_len;
1642 }
Adam Langley95c29f32014-06-20 12:00:00 -07001643}
1644
1645/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
1646 * TLS server needs a list of supported protocols for Next Protocol
1647 * Negotiation. The returned list must be in wire format. The list is returned
1648 * by setting |out| to point to it and |outlen| to its length. This memory will
1649 * not be modified, but one should assume that the SSL* keeps a reference to
1650 * it.
1651 *
Adam Langleyfcf25832014-12-18 17:42:32 -08001652 * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise.
1653 * Otherwise, no such extension will be included in the ServerHello. */
1654void SSL_CTX_set_next_protos_advertised_cb(
1655 SSL_CTX *ctx,
1656 int (*cb)(SSL *ssl, const uint8_t **out, unsigned int *outlen, void *arg),
1657 void *arg) {
1658 ctx->next_protos_advertised_cb = cb;
1659 ctx->next_protos_advertised_cb_arg = arg;
1660}
Adam Langley95c29f32014-06-20 12:00:00 -07001661
1662/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1663 * client needs to select a protocol from the server's provided list. |out|
1664 * must be set to point to the selected protocol (which may be within |in|).
1665 * The length of the protocol name must be written into |outlen|. The server's
1666 * advertised protocols are provided in |in| and |inlen|. The callback can
1667 * assume that |in| is syntactically valid.
1668 *
1669 * The client must select a protocol. It is fatal to the connection if this
1670 * callback returns a value other than SSL_TLSEXT_ERR_OK.
1671 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001672void SSL_CTX_set_next_proto_select_cb(
1673 SSL_CTX *ctx, int (*cb)(SSL *s, uint8_t **out, uint8_t *outlen,
1674 const uint8_t *in, unsigned int inlen, void *arg),
1675 void *arg) {
1676 ctx->next_proto_select_cb = cb;
1677 ctx->next_proto_select_cb_arg = arg;
1678}
Adam Langley95c29f32014-06-20 12:00:00 -07001679
Adam Langley95c29f32014-06-20 12:00:00 -07001680/* SSL_CTX_set_alpn_protos sets the ALPN protocol list on |ctx| to |protos|.
1681 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
1682 * length-prefixed strings).
1683 *
1684 * Returns 0 on success. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001685int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
1686 unsigned protos_len) {
1687 if (ctx->alpn_client_proto_list) {
1688 OPENSSL_free(ctx->alpn_client_proto_list);
1689 }
Adam Langley95c29f32014-06-20 12:00:00 -07001690
Adam Langleyfcf25832014-12-18 17:42:32 -08001691 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1692 if (!ctx->alpn_client_proto_list) {
1693 return 1;
1694 }
1695 ctx->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001696
Adam Langleyfcf25832014-12-18 17:42:32 -08001697 return 0;
1698}
Adam Langley95c29f32014-06-20 12:00:00 -07001699
1700/* SSL_set_alpn_protos sets the ALPN protocol list on |ssl| to |protos|.
1701 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
1702 * length-prefixed strings).
1703 *
1704 * Returns 0 on success. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001705int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
1706 if (ssl->alpn_client_proto_list) {
1707 OPENSSL_free(ssl->alpn_client_proto_list);
1708 }
Adam Langley95c29f32014-06-20 12:00:00 -07001709
Adam Langleyfcf25832014-12-18 17:42:32 -08001710 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1711 if (!ssl->alpn_client_proto_list) {
1712 return 1;
1713 }
1714 ssl->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001715
Adam Langleyfcf25832014-12-18 17:42:32 -08001716 return 0;
1717}
Adam Langley95c29f32014-06-20 12:00:00 -07001718
1719/* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
1720 * during ClientHello processing in order to select an ALPN protocol from the
1721 * client's list of offered protocols. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001722void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
1723 int (*cb)(SSL *ssl, const uint8_t **out,
1724 uint8_t *outlen, const uint8_t *in,
1725 unsigned int inlen, void *arg),
1726 void *arg) {
1727 ctx->alpn_select_cb = cb;
1728 ctx->alpn_select_cb_arg = arg;
1729}
Adam Langley95c29f32014-06-20 12:00:00 -07001730
1731/* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
1732 * On return it sets |*data| to point to |*len| bytes of protocol name (not
1733 * including the leading length-prefix byte). If the server didn't respond with
1734 * a negotiated protocol then |*len| will be zero. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001735void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **data,
1736 unsigned *len) {
1737 *data = NULL;
1738 if (ssl->s3) {
1739 *data = ssl->s3->alpn_selected;
1740 }
1741 if (*data == NULL) {
1742 *len = 0;
1743 } else {
1744 *len = ssl->s3->alpn_selected_len;
1745 }
1746}
Adam Langley95c29f32014-06-20 12:00:00 -07001747
Adam Langleyfcf25832014-12-18 17:42:32 -08001748int SSL_export_keying_material(SSL *s, uint8_t *out, size_t olen,
1749 const char *label, size_t llen, const uint8_t *p,
1750 size_t plen, int use_context) {
1751 if (s->version < TLS1_VERSION) {
1752 return -1;
1753 }
Adam Langley95c29f32014-06-20 12:00:00 -07001754
Adam Langleyfcf25832014-12-18 17:42:32 -08001755 return s->enc_method->export_keying_material(s, out, olen, label, llen, p,
1756 plen, use_context);
1757}
Adam Langley95c29f32014-06-20 12:00:00 -07001758
Adam Langleyfcf25832014-12-18 17:42:32 -08001759static uint32_t ssl_session_hash(const SSL_SESSION *a) {
1760 uint32_t hash =
1761 ((uint32_t)a->session_id[0]) ||
1762 ((uint32_t)a->session_id[1] << 8) ||
1763 ((uint32_t)a->session_id[2] << 16) ||
1764 ((uint32_t)a->session_id[3] << 24);
Adam Langley95c29f32014-06-20 12:00:00 -07001765
Adam Langleyfcf25832014-12-18 17:42:32 -08001766 return hash;
1767}
Adam Langley95c29f32014-06-20 12:00:00 -07001768
1769/* NB: If this function (or indeed the hash function which uses a sort of
1770 * coarser function than this one) is changed, ensure
1771 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1772 * able to construct an SSL_SESSION that will collide with any existing session
1773 * with a matching session ID. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001774static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
1775 if (a->ssl_version != b->ssl_version) {
1776 return 1;
1777 }
Adam Langley95c29f32014-06-20 12:00:00 -07001778
Adam Langleyfcf25832014-12-18 17:42:32 -08001779 if (a->session_id_length != b->session_id_length) {
1780 return 1;
1781 }
Adam Langley95c29f32014-06-20 12:00:00 -07001782
Adam Langleyfcf25832014-12-18 17:42:32 -08001783 return memcmp(a->session_id, b->session_id, a->session_id_length);
1784}
Adam Langley95c29f32014-06-20 12:00:00 -07001785
Adam Langleyfcf25832014-12-18 17:42:32 -08001786SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth) {
1787 SSL_CTX *ret = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001788
Adam Langleyfcf25832014-12-18 17:42:32 -08001789 if (meth == NULL) {
1790 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_NULL_SSL_METHOD_PASSED);
1791 return NULL;
1792 }
Adam Langley95c29f32014-06-20 12:00:00 -07001793
Adam Langleyfcf25832014-12-18 17:42:32 -08001794 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
1795 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
1796 goto err;
1797 }
Adam Langley95c29f32014-06-20 12:00:00 -07001798
Adam Langleyfcf25832014-12-18 17:42:32 -08001799 ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1800 if (ret == NULL) {
1801 goto err;
1802 }
Adam Langley95c29f32014-06-20 12:00:00 -07001803
Adam Langleyfcf25832014-12-18 17:42:32 -08001804 memset(ret, 0, sizeof(SSL_CTX));
Adam Langley95c29f32014-06-20 12:00:00 -07001805
Adam Langleyfcf25832014-12-18 17:42:32 -08001806 ret->method = meth->method;
Adam Langley95c29f32014-06-20 12:00:00 -07001807
Adam Langleyfcf25832014-12-18 17:42:32 -08001808 ret->cert_store = NULL;
1809 ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
1810 ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1811 ret->session_cache_head = NULL;
1812 ret->session_cache_tail = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001813
Adam Langleyfcf25832014-12-18 17:42:32 -08001814 /* We take the system default */
1815 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001816
Adam Langleyfcf25832014-12-18 17:42:32 -08001817 ret->new_session_cb = 0;
1818 ret->remove_session_cb = 0;
1819 ret->get_session_cb = 0;
1820 ret->generate_session_id = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001821
Adam Langleyfcf25832014-12-18 17:42:32 -08001822 memset((char *)&ret->stats, 0, sizeof(ret->stats));
Adam Langley95c29f32014-06-20 12:00:00 -07001823
Adam Langleyfcf25832014-12-18 17:42:32 -08001824 ret->references = 1;
1825 ret->quiet_shutdown = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001826
Adam Langleyfcf25832014-12-18 17:42:32 -08001827 ret->info_callback = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001828
Adam Langleyfcf25832014-12-18 17:42:32 -08001829 ret->app_verify_callback = 0;
1830 ret->app_verify_arg = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001831
Adam Langleyfcf25832014-12-18 17:42:32 -08001832 ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
1833 ret->read_ahead = 0;
1834 ret->msg_callback = 0;
1835 ret->msg_callback_arg = NULL;
1836 ret->verify_mode = SSL_VERIFY_NONE;
1837 ret->sid_ctx_length = 0;
1838 ret->default_verify_callback = NULL;
1839 ret->cert = ssl_cert_new();
1840 if (ret->cert == NULL) {
1841 goto err;
1842 }
Adam Langley95c29f32014-06-20 12:00:00 -07001843
Adam Langleyfcf25832014-12-18 17:42:32 -08001844 ret->default_passwd_callback = 0;
1845 ret->default_passwd_callback_userdata = NULL;
1846 ret->client_cert_cb = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001847
Adam Langleyfcf25832014-12-18 17:42:32 -08001848 ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
1849 if (ret->sessions == NULL) {
1850 goto err;
1851 }
1852 ret->cert_store = X509_STORE_new();
1853 if (ret->cert_store == NULL) {
1854 goto err;
1855 }
Adam Langley95c29f32014-06-20 12:00:00 -07001856
Adam Langleyfcf25832014-12-18 17:42:32 -08001857 ssl_create_cipher_list(ret->method, &ret->cipher_list,
1858 &ret->cipher_list_by_id, SSL_DEFAULT_CIPHER_LIST,
1859 ret->cert);
1860 if (ret->cipher_list == NULL ||
1861 sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0) {
1862 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_LIBRARY_HAS_NO_CIPHERS);
1863 goto err2;
1864 }
Adam Langley95c29f32014-06-20 12:00:00 -07001865
Adam Langleyfcf25832014-12-18 17:42:32 -08001866 ret->param = X509_VERIFY_PARAM_new();
1867 if (!ret->param) {
1868 goto err;
1869 }
Adam Langley95c29f32014-06-20 12:00:00 -07001870
Adam Langleyfcf25832014-12-18 17:42:32 -08001871 ret->client_CA = sk_X509_NAME_new_null();
1872 if (ret->client_CA == NULL) {
1873 goto err;
1874 }
Adam Langley95c29f32014-06-20 12:00:00 -07001875
Adam Langleyfcf25832014-12-18 17:42:32 -08001876 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001877
Adam Langleyfcf25832014-12-18 17:42:32 -08001878 ret->extra_certs = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001879
Adam Langleyfcf25832014-12-18 17:42:32 -08001880 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
David Benjamin422d3a42014-08-20 11:09:03 -04001881
Adam Langleyfcf25832014-12-18 17:42:32 -08001882 ret->tlsext_servername_callback = 0;
1883 ret->tlsext_servername_arg = NULL;
1884 /* Setup RFC4507 ticket keys */
1885 if (!RAND_bytes(ret->tlsext_tick_key_name, 16) ||
1886 !RAND_bytes(ret->tlsext_tick_hmac_key, 16) ||
1887 !RAND_bytes(ret->tlsext_tick_aes_key, 16)) {
1888 ret->options |= SSL_OP_NO_TICKET;
1889 }
Adam Langley95c29f32014-06-20 12:00:00 -07001890
Adam Langleyfcf25832014-12-18 17:42:32 -08001891 ret->tlsext_status_cb = 0;
1892 ret->tlsext_status_arg = NULL;
David Benjamin82c9e902014-12-12 15:55:27 -05001893
Adam Langleyfcf25832014-12-18 17:42:32 -08001894 ret->next_protos_advertised_cb = 0;
1895 ret->next_proto_select_cb = 0;
1896 ret->psk_identity_hint = NULL;
1897 ret->psk_client_callback = NULL;
1898 ret->psk_server_callback = NULL;
1899
1900 /* Default is to connect to non-RI servers. When RI is more widely deployed
1901 * might change this. */
1902 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
1903
1904 /* Lock the SSL_CTX to the specified version, for compatibility with legacy
1905 * uses of SSL_METHOD. */
1906 if (meth->version != 0) {
1907 SSL_CTX_set_max_version(ret, meth->version);
1908 SSL_CTX_set_min_version(ret, meth->version);
1909 }
1910
1911 return ret;
1912
Adam Langley95c29f32014-06-20 12:00:00 -07001913err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001914 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -07001915err2:
Adam Langleyfcf25832014-12-18 17:42:32 -08001916 if (ret != NULL) {
1917 SSL_CTX_free(ret);
1918 }
1919 return NULL;
1920}
Adam Langley95c29f32014-06-20 12:00:00 -07001921
Adam Langleyfcf25832014-12-18 17:42:32 -08001922void SSL_CTX_free(SSL_CTX *a) {
1923 int i;
Adam Langley95c29f32014-06-20 12:00:00 -07001924
Adam Langleyfcf25832014-12-18 17:42:32 -08001925 if (a == NULL) {
1926 return;
1927 }
Adam Langley95c29f32014-06-20 12:00:00 -07001928
Adam Langleyfcf25832014-12-18 17:42:32 -08001929 i = CRYPTO_add(&a->references, -1, CRYPTO_LOCK_SSL_CTX);
1930 if (i > 0) {
1931 return;
1932 }
Adam Langley95c29f32014-06-20 12:00:00 -07001933
Adam Langleyfcf25832014-12-18 17:42:32 -08001934 if (a->param) {
1935 X509_VERIFY_PARAM_free(a->param);
1936 }
Adam Langley95c29f32014-06-20 12:00:00 -07001937
Adam Langleyfcf25832014-12-18 17:42:32 -08001938 /* Free internal session cache. However: the remove_cb() may reference the
1939 * ex_data of SSL_CTX, thus the ex_data store can only be removed after the
1940 * sessions were flushed. As the ex_data handling routines might also touch
1941 * the session cache, the most secure solution seems to be: empty (flush) the
1942 * cache, then free ex_data, then finally free the cache. (See ticket
1943 * [openssl.org #212].) */
1944 if (a->sessions != NULL) {
1945 SSL_CTX_flush_sessions(a, 0);
1946 }
Adam Langley95c29f32014-06-20 12:00:00 -07001947
Adam Langleyfcf25832014-12-18 17:42:32 -08001948 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001949
Adam Langleyfcf25832014-12-18 17:42:32 -08001950 if (a->sessions != NULL) {
1951 lh_SSL_SESSION_free(a->sessions);
1952 }
1953 if (a->cert_store != NULL) {
1954 X509_STORE_free(a->cert_store);
1955 }
1956 if (a->cipher_list != NULL) {
1957 ssl_cipher_preference_list_free(a->cipher_list);
1958 }
1959 if (a->cipher_list_by_id != NULL) {
1960 sk_SSL_CIPHER_free(a->cipher_list_by_id);
1961 }
1962 if (a->cipher_list_tls11 != NULL) {
1963 ssl_cipher_preference_list_free(a->cipher_list_tls11);
1964 }
1965 if (a->cert != NULL) {
1966 ssl_cert_free(a->cert);
1967 }
1968 if (a->client_CA != NULL) {
1969 sk_X509_NAME_pop_free(a->client_CA, X509_NAME_free);
1970 }
1971 if (a->extra_certs != NULL) {
1972 sk_X509_pop_free(a->extra_certs, X509_free);
1973 }
1974 if (a->srtp_profiles) {
1975 sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);
1976 }
1977 if (a->psk_identity_hint) {
1978 OPENSSL_free(a->psk_identity_hint);
1979 }
1980 if (a->tlsext_ecpointformatlist) {
1981 OPENSSL_free(a->tlsext_ecpointformatlist);
1982 }
1983 if (a->tlsext_ellipticcurvelist) {
1984 OPENSSL_free(a->tlsext_ellipticcurvelist);
1985 }
1986 if (a->alpn_client_proto_list != NULL) {
1987 OPENSSL_free(a->alpn_client_proto_list);
1988 }
1989 if (a->tlsext_channel_id_private) {
1990 EVP_PKEY_free(a->tlsext_channel_id_private);
1991 }
1992 if (a->keylog_bio) {
1993 BIO_free(a->keylog_bio);
1994 }
Adam Langley95c29f32014-06-20 12:00:00 -07001995
Adam Langleyfcf25832014-12-18 17:42:32 -08001996 OPENSSL_free(a);
1997}
Adam Langley95c29f32014-06-20 12:00:00 -07001998
Adam Langleyfcf25832014-12-18 17:42:32 -08001999void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) {
2000 ctx->default_passwd_callback = cb;
2001}
Adam Langley95c29f32014-06-20 12:00:00 -07002002
Adam Langleyfcf25832014-12-18 17:42:32 -08002003void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u) {
2004 ctx->default_passwd_callback_userdata = u;
2005}
Adam Langley95c29f32014-06-20 12:00:00 -07002006
Adam Langleyfcf25832014-12-18 17:42:32 -08002007void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
2008 int (*cb)(X509_STORE_CTX *, void *),
2009 void *arg) {
2010 ctx->app_verify_callback = cb;
2011 ctx->app_verify_arg = arg;
2012}
Adam Langley95c29f32014-06-20 12:00:00 -07002013
Adam Langleyfcf25832014-12-18 17:42:32 -08002014void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
2015 int (*cb)(int, X509_STORE_CTX *)) {
2016 ctx->verify_mode = mode;
2017 ctx->default_verify_callback = cb;
2018}
Adam Langley95c29f32014-06-20 12:00:00 -07002019
Adam Langleyfcf25832014-12-18 17:42:32 -08002020void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
2021 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
2022}
Adam Langley1258b6a2014-06-20 12:00:00 -07002023
Adam Langleyfcf25832014-12-18 17:42:32 -08002024void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb)(SSL *ssl, void *arg),
2025 void *arg) {
2026 ssl_cert_set_cert_cb(c->cert, cb, arg);
2027}
David Benjamin859ec3c2014-09-02 16:29:36 -04002028
Adam Langleyfcf25832014-12-18 17:42:32 -08002029void SSL_set_cert_cb(SSL *s, int (*cb)(SSL *ssl, void *arg), void *arg) {
2030 ssl_cert_set_cert_cb(s->cert, cb, arg);
2031}
Adam Langley95c29f32014-06-20 12:00:00 -07002032
Adam Langleyfcf25832014-12-18 17:42:32 -08002033static int ssl_has_key(SSL *s, size_t idx) {
2034 CERT_PKEY *cpk = &s->cert->pkeys[idx];
2035 return cpk->x509 && cpk->privatekey;
2036}
David Benjamin033e5f42014-11-13 18:47:41 -05002037
David Benjaminf31e6812014-11-13 18:05:55 -05002038void ssl_get_compatible_server_ciphers(SSL *s, unsigned long *out_mask_k,
Adam Langleyfcf25832014-12-18 17:42:32 -08002039 unsigned long *out_mask_a) {
2040 CERT *c = s->cert;
2041 int rsa_enc, rsa_sign, dh_tmp;
2042 unsigned long mask_k, mask_a;
2043 int have_ecc_cert, ecdsa_ok;
2044 int have_ecdh_tmp;
2045 X509 *x;
Adam Langley95c29f32014-06-20 12:00:00 -07002046
Adam Langleyfcf25832014-12-18 17:42:32 -08002047 if (c == NULL) {
2048 /* TODO(davidben): Is this codepath possible? */
2049 *out_mask_k = 0;
2050 *out_mask_a = 0;
2051 return;
2052 }
Adam Langley95c29f32014-06-20 12:00:00 -07002053
Adam Langleyfcf25832014-12-18 17:42:32 -08002054 dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07002055
Adam Langleyfcf25832014-12-18 17:42:32 -08002056 have_ecdh_tmp = (c->ecdh_tmp || c->ecdh_tmp_cb || c->ecdh_tmp_auto);
2057 rsa_enc = ssl_has_key(s, SSL_PKEY_RSA_ENC);
2058 rsa_sign = ssl_has_key(s, SSL_PKEY_RSA_SIGN);
2059 have_ecc_cert = ssl_has_key(s, SSL_PKEY_ECC);
2060 mask_k = 0;
2061 mask_a = 0;
David Benjaminf31e6812014-11-13 18:05:55 -05002062
Adam Langleyfcf25832014-12-18 17:42:32 -08002063 if (rsa_enc) {
2064 mask_k |= SSL_kRSA;
2065 }
2066 if (dh_tmp) {
2067 mask_k |= SSL_kEDH;
2068 }
2069 if (rsa_enc || rsa_sign) {
2070 mask_a |= SSL_aRSA;
2071 }
Adam Langley95c29f32014-06-20 12:00:00 -07002072
Adam Langleyfcf25832014-12-18 17:42:32 -08002073 mask_a |= SSL_aNULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002074
Adam Langleyfcf25832014-12-18 17:42:32 -08002075 /* An ECC certificate may be usable for ECDSA cipher suites depending on the
2076 * key usage extension and on the client's curve preferences. */
2077 if (have_ecc_cert) {
2078 x = c->pkeys[SSL_PKEY_ECC].x509;
2079 /* This call populates extension flags (ex_flags). */
2080 X509_check_purpose(x, -1, 0);
2081 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE)
2082 ? (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)
2083 : 1;
2084 if (!tls1_check_ec_cert(s, x)) {
2085 ecdsa_ok = 0;
2086 }
2087 if (ecdsa_ok) {
2088 mask_a |= SSL_aECDSA;
2089 }
2090 }
Adam Langley95c29f32014-06-20 12:00:00 -07002091
Adam Langleyfcf25832014-12-18 17:42:32 -08002092 /* If we are considering an ECC cipher suite that uses an ephemeral EC
2093 * key, check it. */
2094 if (have_ecdh_tmp && tls1_check_ec_tmp_key(s)) {
2095 mask_k |= SSL_kEECDH;
2096 }
Adam Langley95c29f32014-06-20 12:00:00 -07002097
Adam Langleyfcf25832014-12-18 17:42:32 -08002098 /* PSK requires a server callback. */
2099 if (s->psk_server_callback != NULL) {
2100 mask_k |= SSL_kPSK;
2101 mask_a |= SSL_aPSK;
2102 }
Adam Langley95c29f32014-06-20 12:00:00 -07002103
Adam Langleyfcf25832014-12-18 17:42:32 -08002104 *out_mask_k = mask_k;
2105 *out_mask_a = mask_a;
2106}
Adam Langley95c29f32014-06-20 12:00:00 -07002107
2108/* This handy macro borrowed from crypto/x509v3/v3_purp.c */
2109#define ku_reject(x, usage) \
Adam Langleyfcf25832014-12-18 17:42:32 -08002110 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
Adam Langley95c29f32014-06-20 12:00:00 -07002111
Adam Langleyfcf25832014-12-18 17:42:32 -08002112int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s) {
2113 unsigned long alg_a;
2114 int signature_nid = 0, md_nid = 0, pk_nid = 0;
2115 const SSL_CIPHER *cs = s->s3->tmp.new_cipher;
Adam Langley95c29f32014-06-20 12:00:00 -07002116
Adam Langleyfcf25832014-12-18 17:42:32 -08002117 alg_a = cs->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -07002118
Adam Langleyfcf25832014-12-18 17:42:32 -08002119 /* This call populates the ex_flags field correctly */
2120 X509_check_purpose(x, -1, 0);
2121 if (x->sig_alg && x->sig_alg->algorithm) {
2122 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
2123 OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
2124 }
2125 if (alg_a & SSL_aECDSA) {
2126 /* key usage, if present, must allow signing */
2127 if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE)) {
2128 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg,
2129 SSL_R_ECC_CERT_NOT_FOR_SIGNING);
2130 return 0;
2131 }
2132 }
Adam Langley95c29f32014-06-20 12:00:00 -07002133
Adam Langleyfcf25832014-12-18 17:42:32 -08002134 return 1; /* all checks are ok */
2135}
Adam Langley95c29f32014-06-20 12:00:00 -07002136
Adam Langleyfcf25832014-12-18 17:42:32 -08002137static int ssl_get_server_cert_index(const SSL *s) {
2138 int idx;
2139 idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
2140 if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509) {
2141 idx = SSL_PKEY_RSA_SIGN;
2142 }
2143 if (idx == -1) {
2144 OPENSSL_PUT_ERROR(SSL, ssl_get_server_cert_index, ERR_R_INTERNAL_ERROR);
2145 }
2146 return idx;
2147}
Adam Langley95c29f32014-06-20 12:00:00 -07002148
Adam Langleyfcf25832014-12-18 17:42:32 -08002149CERT_PKEY *ssl_get_server_send_pkey(const SSL *s) {
2150 int i = ssl_get_server_cert_index(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002151
Adam Langleyfcf25832014-12-18 17:42:32 -08002152 /* This may or may not be an error. */
2153 if (i < 0) {
2154 return NULL;
2155 }
Adam Langley95c29f32014-06-20 12:00:00 -07002156
Adam Langleyfcf25832014-12-18 17:42:32 -08002157 /* May be NULL. */
2158 return &s->cert->pkeys[i];
2159}
Adam Langley95c29f32014-06-20 12:00:00 -07002160
Adam Langleyfcf25832014-12-18 17:42:32 -08002161EVP_PKEY *ssl_get_sign_pkey(SSL *s, const SSL_CIPHER *cipher) {
2162 unsigned long alg_a;
2163 CERT *c;
2164 int idx = -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002165
Adam Langleyfcf25832014-12-18 17:42:32 -08002166 alg_a = cipher->algorithm_auth;
2167 c = s->cert;
Adam Langley95c29f32014-06-20 12:00:00 -07002168
Adam Langleyfcf25832014-12-18 17:42:32 -08002169 if (alg_a & SSL_aRSA) {
2170 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL) {
2171 idx = SSL_PKEY_RSA_SIGN;
2172 } else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL) {
2173 idx = SSL_PKEY_RSA_ENC;
2174 }
2175 } else if ((alg_a & SSL_aECDSA) &&
2176 (c->pkeys[SSL_PKEY_ECC].privatekey != NULL)) {
2177 idx = SSL_PKEY_ECC;
2178 }
Adam Langley95c29f32014-06-20 12:00:00 -07002179
Adam Langleyfcf25832014-12-18 17:42:32 -08002180 if (idx == -1) {
2181 OPENSSL_PUT_ERROR(SSL, ssl_get_sign_pkey, ERR_R_INTERNAL_ERROR);
2182 return NULL;
2183 }
Adam Langley95c29f32014-06-20 12:00:00 -07002184
Adam Langleyfcf25832014-12-18 17:42:32 -08002185 return c->pkeys[idx].privatekey;
2186}
Adam Langley95c29f32014-06-20 12:00:00 -07002187
Adam Langleyfcf25832014-12-18 17:42:32 -08002188void ssl_update_cache(SSL *s, int mode) {
2189 int i;
Adam Langley95c29f32014-06-20 12:00:00 -07002190
Adam Langleyfcf25832014-12-18 17:42:32 -08002191 /* If the session_id_length is 0, we are not supposed to cache it, and it
2192 * would be rather hard to do anyway :-) */
2193 if (s->session->session_id_length == 0) {
2194 return;
2195 }
Adam Langley95c29f32014-06-20 12:00:00 -07002196
Adam Langleyfcf25832014-12-18 17:42:32 -08002197 i = s->initial_ctx->session_cache_mode;
2198 if ((i & mode) && !s->hit &&
2199 ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE) ||
2200 SSL_CTX_add_session(s->initial_ctx, s->session)) &&
2201 s->initial_ctx->new_session_cb != NULL) {
David Benjamin33639842015-02-09 03:34:47 -05002202 if (!s->initial_ctx->new_session_cb(s, SSL_SESSION_up_ref(s->session))) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002203 SSL_SESSION_free(s->session);
2204 }
2205 }
Adam Langley95c29f32014-06-20 12:00:00 -07002206
Adam Langleyfcf25832014-12-18 17:42:32 -08002207 /* auto flush every 255 connections */
2208 if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) && ((i & mode) == mode)) {
2209 if ((((mode & SSL_SESS_CACHE_CLIENT)
2210 ? s->initial_ctx->stats.sess_connect_good
2211 : s->initial_ctx->stats.sess_accept_good) &
2212 0xff) == 0xff) {
2213 SSL_CTX_flush_sessions(s->initial_ctx, (unsigned long)time(NULL));
2214 }
2215 }
2216}
Adam Langley95c29f32014-06-20 12:00:00 -07002217
Adam Langleyfcf25832014-12-18 17:42:32 -08002218int SSL_get_error(const SSL *s, int i) {
2219 int reason;
2220 unsigned long l;
2221 BIO *bio;
Adam Langley95c29f32014-06-20 12:00:00 -07002222
Adam Langleyfcf25832014-12-18 17:42:32 -08002223 if (i > 0) {
2224 return SSL_ERROR_NONE;
2225 }
Adam Langley95c29f32014-06-20 12:00:00 -07002226
Adam Langleyfcf25832014-12-18 17:42:32 -08002227 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
2228 * where we do encode the error */
2229 l = ERR_peek_error();
2230 if (l != 0) {
2231 if (ERR_GET_LIB(l) == ERR_LIB_SYS) {
2232 return SSL_ERROR_SYSCALL;
2233 }
2234 return SSL_ERROR_SSL;
2235 }
Adam Langley95c29f32014-06-20 12:00:00 -07002236
David Benjamin9a38e922015-01-22 16:06:11 -05002237 if (i == 0) {
2238 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2239 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) {
2240 /* The socket was cleanly shut down with a close_notify. */
2241 return SSL_ERROR_ZERO_RETURN;
2242 }
2243 /* An EOF was observed which violates the protocol, and the underlying
2244 * transport does not participate in the error queue. Bubble up to the
2245 * caller. */
2246 return SSL_ERROR_SYSCALL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002247 }
Adam Langleyb2ce0582014-06-20 12:00:00 -07002248
Adam Langleyfcf25832014-12-18 17:42:32 -08002249 if (SSL_want_session(s)) {
2250 return SSL_ERROR_PENDING_SESSION;
2251 }
Adam Langley95c29f32014-06-20 12:00:00 -07002252
Adam Langleyfcf25832014-12-18 17:42:32 -08002253 if (SSL_want_certificate(s)) {
2254 return SSL_ERROR_PENDING_CERTIFICATE;
2255 }
Adam Langley95c29f32014-06-20 12:00:00 -07002256
Adam Langleyfcf25832014-12-18 17:42:32 -08002257 if (SSL_want_read(s)) {
2258 bio = SSL_get_rbio(s);
2259 if (BIO_should_read(bio)) {
2260 return SSL_ERROR_WANT_READ;
2261 }
Adam Langley95c29f32014-06-20 12:00:00 -07002262
Adam Langleyfcf25832014-12-18 17:42:32 -08002263 if (BIO_should_write(bio)) {
2264 /* This one doesn't make too much sense ... We never try to write to the
2265 * rbio, and an application program where rbio and wbio are separate
2266 * couldn't even know what it should wait for. However if we ever set
2267 * s->rwstate incorrectly (so that we have SSL_want_read(s) instead of
2268 * SSL_want_write(s)) and rbio and wbio *are* the same, this test works
2269 * around that bug; so it might be safer to keep it. */
2270 return SSL_ERROR_WANT_WRITE;
2271 }
Adam Langley95c29f32014-06-20 12:00:00 -07002272
Adam Langleyfcf25832014-12-18 17:42:32 -08002273 if (BIO_should_io_special(bio)) {
2274 reason = BIO_get_retry_reason(bio);
2275 if (reason == BIO_RR_CONNECT) {
2276 return SSL_ERROR_WANT_CONNECT;
2277 }
Adam Langley95c29f32014-06-20 12:00:00 -07002278
Adam Langleyfcf25832014-12-18 17:42:32 -08002279 if (reason == BIO_RR_ACCEPT) {
2280 return SSL_ERROR_WANT_ACCEPT;
2281 }
Adam Langley95c29f32014-06-20 12:00:00 -07002282
Adam Langleyfcf25832014-12-18 17:42:32 -08002283 return SSL_ERROR_SYSCALL; /* unknown */
2284 }
2285 }
Adam Langley95c29f32014-06-20 12:00:00 -07002286
Adam Langleyfcf25832014-12-18 17:42:32 -08002287 if (SSL_want_write(s)) {
2288 bio = SSL_get_wbio(s);
2289 if (BIO_should_write(bio)) {
2290 return SSL_ERROR_WANT_WRITE;
2291 }
Adam Langley95c29f32014-06-20 12:00:00 -07002292
Adam Langleyfcf25832014-12-18 17:42:32 -08002293 if (BIO_should_read(bio)) {
2294 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2295 return SSL_ERROR_WANT_READ;
2296 }
Adam Langley95c29f32014-06-20 12:00:00 -07002297
Adam Langleyfcf25832014-12-18 17:42:32 -08002298 if (BIO_should_io_special(bio)) {
2299 reason = BIO_get_retry_reason(bio);
2300 if (reason == BIO_RR_CONNECT) {
2301 return SSL_ERROR_WANT_CONNECT;
2302 }
Adam Langley95c29f32014-06-20 12:00:00 -07002303
Adam Langleyfcf25832014-12-18 17:42:32 -08002304 if (reason == BIO_RR_ACCEPT) {
2305 return SSL_ERROR_WANT_ACCEPT;
2306 }
Adam Langley95c29f32014-06-20 12:00:00 -07002307
Adam Langleyfcf25832014-12-18 17:42:32 -08002308 return SSL_ERROR_SYSCALL;
2309 }
2310 }
Adam Langley95c29f32014-06-20 12:00:00 -07002311
Adam Langleyfcf25832014-12-18 17:42:32 -08002312 if (SSL_want_x509_lookup(s)) {
2313 return SSL_ERROR_WANT_X509_LOOKUP;
2314 }
Adam Langley95c29f32014-06-20 12:00:00 -07002315
Adam Langleyfcf25832014-12-18 17:42:32 -08002316 if (SSL_want_channel_id_lookup(s)) {
2317 return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
2318 }
Adam Langley0f4746e2014-08-13 12:26:32 -07002319
Adam Langleyfcf25832014-12-18 17:42:32 -08002320 return SSL_ERROR_SYSCALL;
2321}
Adam Langley0f4746e2014-08-13 12:26:32 -07002322
Adam Langleyfcf25832014-12-18 17:42:32 -08002323int SSL_do_handshake(SSL *s) {
2324 int ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002325
Adam Langleyfcf25832014-12-18 17:42:32 -08002326 if (s->handshake_func == NULL) {
2327 OPENSSL_PUT_ERROR(SSL, SSL_do_handshake, SSL_R_CONNECTION_TYPE_NOT_SET);
2328 return -1;
2329 }
Adam Langley95c29f32014-06-20 12:00:00 -07002330
Adam Langleyfcf25832014-12-18 17:42:32 -08002331 s->method->ssl_renegotiate_check(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002332
Adam Langleyfcf25832014-12-18 17:42:32 -08002333 if (SSL_in_init(s)) {
2334 ret = s->handshake_func(s);
2335 }
2336 return ret;
2337}
Adam Langley95c29f32014-06-20 12:00:00 -07002338
Adam Langleyfcf25832014-12-18 17:42:32 -08002339void SSL_set_accept_state(SSL *s) {
2340 s->server = 1;
2341 s->shutdown = 0;
2342 s->state = SSL_ST_ACCEPT | SSL_ST_BEFORE;
2343 s->handshake_func = s->method->ssl_accept;
2344 /* clear the current cipher */
2345 ssl_clear_cipher_ctx(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002346}
Adam Langley95c29f32014-06-20 12:00:00 -07002347
Adam Langleyfcf25832014-12-18 17:42:32 -08002348void SSL_set_connect_state(SSL *s) {
2349 s->server = 0;
2350 s->shutdown = 0;
2351 s->state = SSL_ST_CONNECT | SSL_ST_BEFORE;
2352 s->handshake_func = s->method->ssl_connect;
2353 /* clear the current cipher */
2354 ssl_clear_cipher_ctx(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002355}
Adam Langley95c29f32014-06-20 12:00:00 -07002356
Adam Langleyfcf25832014-12-18 17:42:32 -08002357int ssl_undefined_function(SSL *s) {
2358 OPENSSL_PUT_ERROR(SSL, ssl_undefined_function,
2359 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2360 return 0;
2361}
Adam Langley95c29f32014-06-20 12:00:00 -07002362
Adam Langleyfcf25832014-12-18 17:42:32 -08002363int ssl_undefined_void_function(void) {
2364 OPENSSL_PUT_ERROR(SSL, ssl_undefined_void_function,
2365 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2366 return 0;
2367}
Adam Langley95c29f32014-06-20 12:00:00 -07002368
Adam Langleyfcf25832014-12-18 17:42:32 -08002369int ssl_undefined_const_function(const SSL *s) {
2370 OPENSSL_PUT_ERROR(SSL, ssl_undefined_const_function,
2371 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2372 return 0;
2373}
Adam Langley95c29f32014-06-20 12:00:00 -07002374
Adam Langleyfcf25832014-12-18 17:42:32 -08002375static const char *ssl_get_version(int version) {
2376 switch (version) {
2377 case TLS1_2_VERSION:
2378 return "TLSv1.2";
Adam Langley95c29f32014-06-20 12:00:00 -07002379
Adam Langleyfcf25832014-12-18 17:42:32 -08002380 case TLS1_1_VERSION:
2381 return "TLSv1.1";
Adam Langley95c29f32014-06-20 12:00:00 -07002382
Adam Langleyfcf25832014-12-18 17:42:32 -08002383 case TLS1_VERSION:
2384 return "TLSv1";
Adam Langley95c29f32014-06-20 12:00:00 -07002385
Adam Langleyfcf25832014-12-18 17:42:32 -08002386 case SSL3_VERSION:
2387 return "SSLv3";
Adam Langley95c29f32014-06-20 12:00:00 -07002388
Adam Langleyfcf25832014-12-18 17:42:32 -08002389 default:
2390 return "unknown";
2391 }
2392}
Adam Langley95c29f32014-06-20 12:00:00 -07002393
Adam Langleyfcf25832014-12-18 17:42:32 -08002394const char *SSL_get_version(const SSL *s) {
2395 return ssl_get_version(s->version);
2396}
Adam Langley95c29f32014-06-20 12:00:00 -07002397
Adam Langleyfcf25832014-12-18 17:42:32 -08002398const char *SSL_SESSION_get_version(const SSL_SESSION *sess) {
2399 return ssl_get_version(sess->ssl_version);
2400}
Adam Langley95c29f32014-06-20 12:00:00 -07002401
Adam Langleyfcf25832014-12-18 17:42:32 -08002402void ssl_clear_cipher_ctx(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002403 if (s->aead_read_ctx != NULL) {
2404 EVP_AEAD_CTX_cleanup(&s->aead_read_ctx->ctx);
2405 OPENSSL_free(s->aead_read_ctx);
2406 s->aead_read_ctx = NULL;
2407 }
Adam Langleya5dc5452014-06-20 12:00:00 -07002408
Adam Langleyfcf25832014-12-18 17:42:32 -08002409 if (s->aead_write_ctx != NULL) {
2410 EVP_AEAD_CTX_cleanup(&s->aead_write_ctx->ctx);
2411 OPENSSL_free(s->aead_write_ctx);
2412 s->aead_write_ctx = NULL;
2413 }
2414}
Adam Langley95c29f32014-06-20 12:00:00 -07002415
Adam Langleyfcf25832014-12-18 17:42:32 -08002416X509 *SSL_get_certificate(const SSL *s) {
2417 if (s->cert != NULL) {
2418 return s->cert->key->x509;
2419 }
2420
2421 return NULL;
2422}
2423
2424EVP_PKEY *SSL_get_privatekey(const SSL *s) {
2425 if (s->cert != NULL) {
2426 return s->cert->key->privatekey;
2427 }
2428
2429 return NULL;
2430}
2431
2432X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
2433 if (ctx->cert != NULL) {
2434 return ctx->cert->key->x509;
2435 }
2436
2437 return NULL;
2438}
2439
2440EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
2441 if (ctx->cert != NULL) {
2442 return ctx->cert->key->privatekey;
2443 }
2444
2445 return NULL;
2446}
2447
2448const SSL_CIPHER *SSL_get_current_cipher(const SSL *s) {
2449 if (s->session != NULL && s->session->cipher != NULL) {
2450 return s->session->cipher;
2451 }
2452
2453 return NULL;
2454}
2455
2456const void *SSL_get_current_compression(SSL *s) { return NULL; }
2457
2458const void *SSL_get_current_expansion(SSL *s) { return NULL; }
2459
2460int ssl_init_wbio_buffer(SSL *s, int push) {
2461 BIO *bbio;
2462
2463 if (s->bbio == NULL) {
2464 bbio = BIO_new(BIO_f_buffer());
2465 if (bbio == NULL) {
2466 return 0;
2467 }
2468 s->bbio = bbio;
2469 } else {
2470 bbio = s->bbio;
2471 if (s->bbio == s->wbio) {
2472 s->wbio = BIO_pop(s->wbio);
2473 }
2474 }
2475
2476 BIO_reset(bbio);
2477 if (!BIO_set_read_buffer_size(bbio, 1)) {
2478 OPENSSL_PUT_ERROR(SSL, ssl_init_wbio_buffer, ERR_R_BUF_LIB);
2479 return 0;
2480 }
2481
2482 if (push) {
2483 if (s->wbio != bbio) {
2484 s->wbio = BIO_push(bbio, s->wbio);
2485 }
2486 } else {
2487 if (s->wbio == bbio) {
2488 s->wbio = BIO_pop(bbio);
2489 }
2490 }
2491
2492 return 1;
2493}
2494
2495void ssl_free_wbio_buffer(SSL *s) {
2496 if (s->bbio == NULL) {
2497 return;
2498 }
2499
2500 if (s->bbio == s->wbio) {
2501 /* remove buffering */
2502 s->wbio = BIO_pop(s->wbio);
2503 }
2504
2505 BIO_free(s->bbio);
2506 s->bbio = NULL;
2507}
2508
2509void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
2510 ctx->quiet_shutdown = mode;
2511}
2512
2513int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
2514 return ctx->quiet_shutdown;
2515}
2516
2517void SSL_set_quiet_shutdown(SSL *s, int mode) { s->quiet_shutdown = mode; }
2518
2519int SSL_get_quiet_shutdown(const SSL *s) { return s->quiet_shutdown; }
2520
2521void SSL_set_shutdown(SSL *s, int mode) { s->shutdown = mode; }
2522
2523int SSL_get_shutdown(const SSL *s) { return s->shutdown; }
2524
2525int SSL_version(const SSL *s) { return s->version; }
2526
2527SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
2528
2529SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
2530 if (ssl->ctx == ctx) {
2531 return ssl->ctx;
2532 }
2533
2534 if (ctx == NULL) {
2535 ctx = ssl->initial_ctx;
2536 }
2537
2538 if (ssl->cert != NULL) {
2539 ssl_cert_free(ssl->cert);
2540 }
2541
2542 ssl->cert = ssl_cert_dup(ctx->cert);
2543 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
2544 if (ssl->ctx != NULL) {
2545 SSL_CTX_free(ssl->ctx); /* decrement reference count */
2546 }
2547 ssl->ctx = ctx;
2548
2549 ssl->sid_ctx_length = ctx->sid_ctx_length;
2550 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2551 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2552
2553 return ssl->ctx;
2554}
2555
2556int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
2557 return X509_STORE_set_default_paths(ctx->cert_store);
2558}
Adam Langley95c29f32014-06-20 12:00:00 -07002559
2560int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
Adam Langleyfcf25832014-12-18 17:42:32 -08002561 const char *CApath) {
2562 return X509_STORE_load_locations(ctx->cert_store, CAfile, CApath);
2563}
Adam Langley95c29f32014-06-20 12:00:00 -07002564
2565void SSL_set_info_callback(SSL *ssl,
Adam Langleyfcf25832014-12-18 17:42:32 -08002566 void (*cb)(const SSL *ssl, int type, int val)) {
2567 ssl->info_callback = cb;
2568}
Adam Langley95c29f32014-06-20 12:00:00 -07002569
Adam Langleyfcf25832014-12-18 17:42:32 -08002570void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/, int /*type*/,
2571 int /*val*/) {
2572 return ssl->info_callback;
2573}
Adam Langley95c29f32014-06-20 12:00:00 -07002574
Adam Langleyfcf25832014-12-18 17:42:32 -08002575int SSL_state(const SSL *ssl) { return ssl->state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002576
Adam Langleyfcf25832014-12-18 17:42:32 -08002577void SSL_set_state(SSL *ssl, int state) { ssl->state = state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002578
Adam Langleyfcf25832014-12-18 17:42:32 -08002579void SSL_set_verify_result(SSL *ssl, long arg) { ssl->verify_result = arg; }
Adam Langley95c29f32014-06-20 12:00:00 -07002580
Adam Langleyfcf25832014-12-18 17:42:32 -08002581long SSL_get_verify_result(const SSL *ssl) { return ssl->verify_result; }
Adam Langley95c29f32014-06-20 12:00:00 -07002582
Adam Langleyfcf25832014-12-18 17:42:32 -08002583int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2584 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
2585 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp, new_func,
2586 dup_func, free_func);
2587}
Adam Langley95c29f32014-06-20 12:00:00 -07002588
Adam Langleyfcf25832014-12-18 17:42:32 -08002589int SSL_set_ex_data(SSL *s, int idx, void *arg) {
2590 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2591}
Adam Langley95c29f32014-06-20 12:00:00 -07002592
Adam Langleyfcf25832014-12-18 17:42:32 -08002593void *SSL_get_ex_data(const SSL *s, int idx) {
2594 return CRYPTO_get_ex_data(&s->ex_data, idx);
2595}
Adam Langley95c29f32014-06-20 12:00:00 -07002596
Adam Langleyfcf25832014-12-18 17:42:32 -08002597int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2598 CRYPTO_EX_dup *dup_func,
2599 CRYPTO_EX_free *free_func) {
2600 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp, new_func,
2601 dup_func, free_func);
2602}
Adam Langley95c29f32014-06-20 12:00:00 -07002603
Adam Langleyfcf25832014-12-18 17:42:32 -08002604int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg) {
2605 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2606}
Adam Langley95c29f32014-06-20 12:00:00 -07002607
Adam Langleyfcf25832014-12-18 17:42:32 -08002608void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx) {
2609 return CRYPTO_get_ex_data(&s->ex_data, idx);
2610}
Adam Langley95c29f32014-06-20 12:00:00 -07002611
Adam Langleyfcf25832014-12-18 17:42:32 -08002612int ssl_ok(SSL *s) { return 1; }
Adam Langley95c29f32014-06-20 12:00:00 -07002613
Adam Langleyfcf25832014-12-18 17:42:32 -08002614X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
2615 return ctx->cert_store;
2616}
Adam Langley95c29f32014-06-20 12:00:00 -07002617
Adam Langleyfcf25832014-12-18 17:42:32 -08002618void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
2619 if (ctx->cert_store != NULL) {
2620 X509_STORE_free(ctx->cert_store);
2621 }
2622 ctx->cert_store = store;
2623}
Adam Langley95c29f32014-06-20 12:00:00 -07002624
Adam Langleyfcf25832014-12-18 17:42:32 -08002625int SSL_want(const SSL *s) { return s->rwstate; }
Adam Langley95c29f32014-06-20 12:00:00 -07002626
Adam Langleyfcf25832014-12-18 17:42:32 -08002627void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
2628 RSA *(*cb)(SSL *ssl, int is_export,
2629 int keylength)) {
2630 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_RSA_CB, (void (*)(void))cb);
2631}
Adam Langley95c29f32014-06-20 12:00:00 -07002632
Adam Langleyfcf25832014-12-18 17:42:32 -08002633void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
2634 int keylength)) {
2635 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_RSA_CB, (void (*)(void))cb);
2636}
2637
2638void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
2639 DH *(*dh)(SSL *ssl, int is_export,
2640 int keylength)) {
2641 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh);
2642}
2643
2644void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*dh)(SSL *ssl, int is_export,
2645 int keylength)) {
2646 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh);
2647}
2648
2649void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,
2650 EC_KEY *(*ecdh)(SSL *ssl, int is_export,
2651 int keylength)) {
2652 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH_CB, (void (*)(void))ecdh);
2653}
2654
2655void SSL_set_tmp_ecdh_callback(SSL *ssl,
2656 EC_KEY *(*ecdh)(SSL *ssl, int is_export,
2657 int keylength)) {
2658 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_ECDH_CB, (void (*)(void))ecdh);
2659}
2660
2661int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
2662 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2663 OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_psk_identity_hint,
2664 SSL_R_DATA_LENGTH_TOO_LONG);
2665 return 0;
2666 }
2667
2668 if (ctx->psk_identity_hint != NULL) {
2669 OPENSSL_free(ctx->psk_identity_hint);
2670 }
2671
2672 if (identity_hint != NULL) {
2673 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2674 if (ctx->psk_identity_hint == NULL) {
2675 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002676 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002677 } else {
2678 ctx->psk_identity_hint = NULL;
2679 }
Adam Langley95c29f32014-06-20 12:00:00 -07002680
Adam Langleyfcf25832014-12-18 17:42:32 -08002681 return 1;
2682}
2683
2684int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint) {
2685 if (s == NULL) {
2686 return 0;
2687 }
2688
2689 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2690 OPENSSL_PUT_ERROR(SSL, SSL_use_psk_identity_hint,
2691 SSL_R_DATA_LENGTH_TOO_LONG);
2692 return 0;
2693 }
2694
2695 /* Clear currently configured hint, if any. */
2696 if (s->psk_identity_hint != NULL) {
2697 OPENSSL_free(s->psk_identity_hint);
2698 s->psk_identity_hint = NULL;
2699 }
2700
2701 if (identity_hint != NULL) {
2702 s->psk_identity_hint = BUF_strdup(identity_hint);
2703 if (s->psk_identity_hint == NULL) {
2704 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002705 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002706 }
Adam Langley95c29f32014-06-20 12:00:00 -07002707
Adam Langleyfcf25832014-12-18 17:42:32 -08002708 return 1;
2709}
Adam Langley95c29f32014-06-20 12:00:00 -07002710
Adam Langleyfcf25832014-12-18 17:42:32 -08002711const char *SSL_get_psk_identity_hint(const SSL *s) {
2712 if (s == NULL) {
2713 return NULL;
2714 }
2715 return s->psk_identity_hint;
2716}
Adam Langley95c29f32014-06-20 12:00:00 -07002717
Adam Langleyfcf25832014-12-18 17:42:32 -08002718const char *SSL_get_psk_identity(const SSL *s) {
2719 if (s == NULL || s->session == NULL) {
2720 return NULL;
2721 }
Adam Langley95c29f32014-06-20 12:00:00 -07002722
Adam Langleyfcf25832014-12-18 17:42:32 -08002723 return s->session->psk_identity;
2724}
Adam Langley95c29f32014-06-20 12:00:00 -07002725
Adam Langleyfcf25832014-12-18 17:42:32 -08002726void SSL_set_psk_client_callback(
2727 SSL *s, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2728 unsigned int max_identity_len, uint8_t *psk,
2729 unsigned int max_psk_len)) {
2730 s->psk_client_callback = cb;
2731}
Adam Langley95c29f32014-06-20 12:00:00 -07002732
Adam Langleyfcf25832014-12-18 17:42:32 -08002733void SSL_CTX_set_psk_client_callback(
2734 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2735 unsigned int max_identity_len,
2736 uint8_t *psk, unsigned int max_psk_len)) {
2737 ctx->psk_client_callback = cb;
2738}
Adam Langley95c29f32014-06-20 12:00:00 -07002739
Adam Langleyfcf25832014-12-18 17:42:32 -08002740void SSL_set_psk_server_callback(
2741 SSL *s, unsigned int (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
2742 unsigned int max_psk_len)) {
2743 s->psk_server_callback = cb;
2744}
Adam Langley95c29f32014-06-20 12:00:00 -07002745
Adam Langleyfcf25832014-12-18 17:42:32 -08002746void SSL_CTX_set_psk_server_callback(
2747 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *identity,
2748 uint8_t *psk, unsigned int max_psk_len)) {
2749 ctx->psk_server_callback = cb;
2750}
Adam Langley95c29f32014-06-20 12:00:00 -07002751
Adam Langleyfcf25832014-12-18 17:42:32 -08002752void SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) {
2753 ctx->min_version = version;
2754}
Adam Langley95c29f32014-06-20 12:00:00 -07002755
Adam Langleyfcf25832014-12-18 17:42:32 -08002756void SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) {
2757 ctx->max_version = version;
2758}
Adam Langley0289c732014-06-20 12:00:00 -07002759
Adam Langleyfcf25832014-12-18 17:42:32 -08002760void SSL_set_min_version(SSL *ssl, uint16_t version) {
2761 ssl->min_version = version;
2762}
Adam Langley0289c732014-06-20 12:00:00 -07002763
Adam Langleyfcf25832014-12-18 17:42:32 -08002764void SSL_set_max_version(SSL *ssl, uint16_t version) {
2765 ssl->max_version = version;
2766}
Adam Langley95c29f32014-06-20 12:00:00 -07002767
Adam Langleyfcf25832014-12-18 17:42:32 -08002768void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
2769 void (*cb)(int write_p, int version,
2770 int content_type, const void *buf,
2771 size_t len, SSL *ssl, void *arg)) {
2772 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2773}
2774void SSL_set_msg_callback(SSL *ssl,
2775 void (*cb)(int write_p, int version, int content_type,
2776 const void *buf, size_t len, SSL *ssl,
2777 void *arg)) {
2778 SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2779}
Adam Langley95c29f32014-06-20 12:00:00 -07002780
Adam Langleyfcf25832014-12-18 17:42:32 -08002781void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio) {
2782 if (ctx->keylog_bio != NULL) {
2783 BIO_free(ctx->keylog_bio);
2784 }
2785 ctx->keylog_bio = keylog_bio;
2786}
Adam Langley95c29f32014-06-20 12:00:00 -07002787
Adam Langleyfcf25832014-12-18 17:42:32 -08002788static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
2789 static const char hextable[] = "0123456789abcdef";
2790 uint8_t *out;
2791 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002792
Adam Langleyfcf25832014-12-18 17:42:32 -08002793 if (!CBB_add_space(cbb, &out, in_len * 2)) {
2794 return 0;
2795 }
Adam Langley95c29f32014-06-20 12:00:00 -07002796
Adam Langleyfcf25832014-12-18 17:42:32 -08002797 for (i = 0; i < in_len; i++) {
2798 *(out++) = (uint8_t)hextable[in[i] >> 4];
2799 *(out++) = (uint8_t)hextable[in[i] & 0xf];
2800 }
Adam Langley95c29f32014-06-20 12:00:00 -07002801
Adam Langleyfcf25832014-12-18 17:42:32 -08002802 return 1;
2803}
David Benjamin859ec3c2014-09-02 16:29:36 -04002804
2805int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -08002806 const uint8_t *encrypted_premaster,
2807 size_t encrypted_premaster_len,
2808 const uint8_t *premaster,
2809 size_t premaster_len) {
2810 BIO *bio = ctx->keylog_bio;
2811 CBB cbb;
2812 uint8_t *out;
2813 size_t out_len;
2814 int ret;
David Benjamin859ec3c2014-09-02 16:29:36 -04002815
Adam Langleyfcf25832014-12-18 17:42:32 -08002816 if (bio == NULL) {
2817 return 1;
2818 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002819
Adam Langleyfcf25832014-12-18 17:42:32 -08002820 if (encrypted_premaster_len < 8) {
2821 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_rsa_client_key_exchange,
2822 ERR_R_INTERNAL_ERROR);
2823 return 0;
2824 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002825
Adam Langleyfcf25832014-12-18 17:42:32 -08002826 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len * 2 + 1)) {
2827 return 0;
2828 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002829
Adam Langleyfcf25832014-12-18 17:42:32 -08002830 if (!CBB_add_bytes(&cbb, (const uint8_t *)"RSA ", 4) ||
2831 /* Only the first 8 bytes of the encrypted premaster secret are
2832 * logged. */
2833 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
2834 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2835 !cbb_add_hex(&cbb, premaster, premaster_len) ||
2836 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2837 !CBB_finish(&cbb, &out, &out_len)) {
2838 CBB_cleanup(&cbb);
2839 return 0;
2840 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002841
Adam Langleyfcf25832014-12-18 17:42:32 -08002842 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2843 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2844 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
David Benjamin859ec3c2014-09-02 16:29:36 -04002845
Adam Langleyfcf25832014-12-18 17:42:32 -08002846 OPENSSL_free(out);
2847 return ret;
Adam Langley95f22882014-06-20 12:00:00 -07002848}
2849
Adam Langleyfcf25832014-12-18 17:42:32 -08002850int ssl_ctx_log_master_secret(SSL_CTX *ctx, const uint8_t *client_random,
2851 size_t client_random_len, const uint8_t *master,
2852 size_t master_len) {
2853 BIO *bio = ctx->keylog_bio;
2854 CBB cbb;
2855 uint8_t *out;
2856 size_t out_len;
2857 int ret;
Adam Langleyadb739e2014-06-20 12:00:00 -07002858
Adam Langleyfcf25832014-12-18 17:42:32 -08002859 if (bio == NULL) {
2860 return 1;
2861 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002862
Adam Langleyfcf25832014-12-18 17:42:32 -08002863 if (client_random_len != 32) {
2864 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_master_secret, ERR_R_INTERNAL_ERROR);
2865 return 0;
2866 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002867
Adam Langleyfcf25832014-12-18 17:42:32 -08002868 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len * 2 + 1)) {
2869 return 0;
2870 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002871
Adam Langleyfcf25832014-12-18 17:42:32 -08002872 if (!CBB_add_bytes(&cbb, (const uint8_t *)"CLIENT_RANDOM ", 14) ||
2873 !cbb_add_hex(&cbb, client_random, 32) ||
2874 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2875 !cbb_add_hex(&cbb, master, master_len) ||
2876 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2877 !CBB_finish(&cbb, &out, &out_len)) {
2878 CBB_cleanup(&cbb);
2879 return 0;
2880 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002881
Adam Langleyfcf25832014-12-18 17:42:32 -08002882 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2883 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2884 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
David Benjamine99e9122014-12-11 01:46:01 -05002885
Adam Langleyfcf25832014-12-18 17:42:32 -08002886 OPENSSL_free(out);
2887 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -07002888}
2889
David Benjamined7c4752015-02-16 19:16:46 -05002890int SSL_in_false_start(const SSL *s) {
2891 return s->s3->tmp.in_false_start;
2892}
2893
Adam Langleyfcf25832014-12-18 17:42:32 -08002894int SSL_cutthrough_complete(const SSL *s) {
David Benjamined7c4752015-02-16 19:16:46 -05002895 return SSL_in_false_start(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002896}
Adam Langley95c29f32014-06-20 12:00:00 -07002897
Adam Langleyfcf25832014-12-18 17:42:32 -08002898void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
2899 size_t *ssl_session_size) {
2900 *ssl_size = sizeof(SSL);
2901 *ssl_ctx_size = sizeof(SSL_CTX);
2902 *ssl_session_size = sizeof(SSL_SESSION);
2903}
Feng Lu41aa3252014-11-21 22:47:56 -08002904
David Benjamined7c4752015-02-16 19:16:46 -05002905int ssl3_can_false_start(const SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002906 const SSL_CIPHER *c;
2907
2908 /* require a strong enough cipher */
2909 if (SSL_get_cipher_bits(s, NULL) < 128) {
2910 return 0;
2911 }
2912
2913 /* require ALPN or NPN extension */
2914 if (!s->s3->alpn_selected && !s->s3->next_proto_neg_seen) {
2915 return 0;
2916 }
2917
2918 /* require a forward-secret cipher */
2919 c = SSL_get_current_cipher(s);
2920 if (!c ||
2921 (c->algorithm_mkey != SSL_kEDH && c->algorithm_mkey != SSL_kEECDH)) {
2922 return 0;
2923 }
2924
2925 return 1;
2926}
2927
2928const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version) {
2929 switch (version) {
2930 case SSL3_VERSION:
2931 return &SSLv3_enc_data;
2932
2933 case TLS1_VERSION:
2934 return &TLSv1_enc_data;
2935
2936 case TLS1_1_VERSION:
2937 return &TLSv1_1_enc_data;
2938
2939 case TLS1_2_VERSION:
2940 return &TLSv1_2_enc_data;
2941
2942 case DTLS1_VERSION:
2943 return &DTLSv1_enc_data;
2944
2945 case DTLS1_2_VERSION:
2946 return &DTLSv1_2_enc_data;
2947
2948 default:
2949 return NULL;
2950 }
2951}
2952
2953uint16_t ssl3_get_max_server_version(const SSL *s) {
2954 uint16_t max_version;
2955
2956 if (SSL_IS_DTLS(s)) {
2957 max_version = (s->max_version != 0) ? s->max_version : DTLS1_2_VERSION;
2958 if (!(s->options & SSL_OP_NO_DTLSv1_2) && DTLS1_2_VERSION >= max_version) {
2959 return DTLS1_2_VERSION;
2960 }
2961 if (!(s->options & SSL_OP_NO_DTLSv1) && DTLS1_VERSION >= max_version) {
2962 return DTLS1_VERSION;
2963 }
2964 return 0;
2965 }
2966
2967 max_version = (s->max_version != 0) ? s->max_version : TLS1_2_VERSION;
2968 if (!(s->options & SSL_OP_NO_TLSv1_2) && TLS1_2_VERSION <= max_version) {
2969 return TLS1_2_VERSION;
2970 }
2971 if (!(s->options & SSL_OP_NO_TLSv1_1) && TLS1_1_VERSION <= max_version) {
2972 return TLS1_1_VERSION;
2973 }
2974 if (!(s->options & SSL_OP_NO_TLSv1) && TLS1_VERSION <= max_version) {
2975 return TLS1_VERSION;
2976 }
2977 if (!(s->options & SSL_OP_NO_SSLv3) && SSL3_VERSION <= max_version) {
2978 return SSL3_VERSION;
2979 }
2980 return 0;
2981}
2982
2983uint16_t ssl3_get_mutual_version(SSL *s, uint16_t client_version) {
2984 uint16_t version = 0;
2985
2986 if (SSL_IS_DTLS(s)) {
2987 /* Clamp client_version to max_version. */
2988 if (s->max_version != 0 && client_version < s->max_version) {
2989 client_version = s->max_version;
2990 }
2991
2992 if (client_version <= DTLS1_2_VERSION && !(s->options & SSL_OP_NO_DTLSv1_2)) {
2993 version = DTLS1_2_VERSION;
2994 } else if (client_version <= DTLS1_VERSION &&
2995 !(s->options & SSL_OP_NO_DTLSv1)) {
2996 version = DTLS1_VERSION;
2997 }
2998
2999 /* Check against min_version. */
3000 if (version != 0 && s->min_version != 0 && version > s->min_version) {
3001 return 0;
3002 }
3003 return version;
3004 } else {
3005 /* Clamp client_version to max_version. */
3006 if (s->max_version != 0 && client_version > s->max_version) {
3007 client_version = s->max_version;
3008 }
3009
3010 if (client_version >= TLS1_2_VERSION && !(s->options & SSL_OP_NO_TLSv1_2)) {
3011 version = TLS1_2_VERSION;
3012 } else if (client_version >= TLS1_1_VERSION &&
3013 !(s->options & SSL_OP_NO_TLSv1_1)) {
3014 version = TLS1_1_VERSION;
3015 } else if (client_version >= TLS1_VERSION && !(s->options & SSL_OP_NO_TLSv1)) {
3016 version = TLS1_VERSION;
3017 } else if (client_version >= SSL3_VERSION && !(s->options & SSL_OP_NO_SSLv3)) {
3018 version = SSL3_VERSION;
3019 }
3020
3021 /* Check against min_version. */
3022 if (version != 0 && s->min_version != 0 && version < s->min_version) {
3023 return 0;
3024 }
3025 return version;
3026 }
3027}
3028
3029uint16_t ssl3_get_max_client_version(SSL *s) {
3030 unsigned long options = s->options;
3031 uint16_t version = 0;
3032
3033 /* OpenSSL's API for controlling versions entails blacklisting individual
3034 * protocols. This has two problems. First, on the client, the protocol can
3035 * only express a contiguous range of versions. Second, a library consumer
3036 * trying to set a maximum version cannot disable protocol versions that get
3037 * added in a future version of the library.
3038 *
3039 * To account for both of these, OpenSSL interprets the client-side bitmask
3040 * as a min/max range by picking the lowest contiguous non-empty range of
3041 * enabled protocols. Note that this means it is impossible to set a maximum
3042 * version of TLS 1.2 in a future-proof way.
3043 *
3044 * By this scheme, the maximum version is the lowest version V such that V is
3045 * enabled and V+1 is disabled or unimplemented. */
3046 if (SSL_IS_DTLS(s)) {
3047 if (!(options & SSL_OP_NO_DTLSv1_2)) {
3048 version = DTLS1_2_VERSION;
3049 }
3050 if (!(options & SSL_OP_NO_DTLSv1) && (options & SSL_OP_NO_DTLSv1_2)) {
3051 version = DTLS1_VERSION;
3052 }
3053 if (s->max_version != 0 && version < s->max_version) {
3054 version = s->max_version;
3055 }
3056 } else {
3057 if (!(options & SSL_OP_NO_TLSv1_2)) {
3058 version = TLS1_2_VERSION;
3059 }
3060 if (!(options & SSL_OP_NO_TLSv1_1) && (options & SSL_OP_NO_TLSv1_2)) {
3061 version = TLS1_1_VERSION;
3062 }
3063 if (!(options & SSL_OP_NO_TLSv1) && (options & SSL_OP_NO_TLSv1_1)) {
3064 version = TLS1_VERSION;
3065 }
3066 if (!(options & SSL_OP_NO_SSLv3) && (options & SSL_OP_NO_TLSv1)) {
3067 version = SSL3_VERSION;
3068 }
3069 if (s->max_version != 0 && version > s->max_version) {
3070 version = s->max_version;
3071 }
3072 }
3073
3074 return version;
3075}
3076
3077int ssl3_is_version_enabled(SSL *s, uint16_t version) {
3078 if (SSL_IS_DTLS(s)) {
3079 if (s->max_version != 0 && version < s->max_version) {
3080 return 0;
3081 }
3082 if (s->min_version != 0 && version > s->min_version) {
3083 return 0;
3084 }
3085
3086 switch (version) {
3087 case DTLS1_VERSION:
3088 return !(s->options & SSL_OP_NO_DTLSv1);
3089
3090 case DTLS1_2_VERSION:
3091 return !(s->options & SSL_OP_NO_DTLSv1_2);
3092
3093 default:
3094 return 0;
3095 }
3096 } else {
3097 if (s->max_version != 0 && version > s->max_version) {
3098 return 0;
3099 }
3100 if (s->min_version != 0 && version < s->min_version) {
3101 return 0;
3102 }
3103
3104 switch (version) {
3105 case SSL3_VERSION:
3106 return !(s->options & SSL_OP_NO_SSLv3);
3107
3108 case TLS1_VERSION:
3109 return !(s->options & SSL_OP_NO_TLSv1);
3110
3111 case TLS1_1_VERSION:
3112 return !(s->options & SSL_OP_NO_TLSv1_1);
3113
3114 case TLS1_2_VERSION:
3115 return !(s->options & SSL_OP_NO_TLSv1_2);
3116
3117 default:
3118 return 0;
3119 }
3120 }
3121}
3122
David Benjaminea72bd02014-12-21 21:27:41 -05003123uint16_t ssl3_version_from_wire(SSL *s, uint16_t wire_version) {
3124 if (!SSL_IS_DTLS(s)) {
3125 return wire_version;
3126 }
3127
3128 uint16_t tls_version = ~wire_version;
3129 uint16_t version = tls_version + 0x0201;
3130 /* If either component overflowed, clamp it so comparisons still work. */
3131 if ((version >> 8) < (tls_version >> 8)) {
3132 version = 0xff00 | (version & 0xff);
3133 }
3134 if ((version & 0xff) < (tls_version & 0xff)) {
3135 version = (version & 0xff00) | 0xff;
3136 }
3137 /* DTLS 1.0 maps to TLS 1.1, not TLS 1.0. */
3138 if (version == TLS1_VERSION) {
3139 version = TLS1_1_VERSION;
3140 }
3141 return version;
3142}
3143
Adam Langleyfcf25832014-12-18 17:42:32 -08003144int SSL_cache_hit(SSL *s) { return s->hit; }
3145
3146int SSL_is_server(SSL *s) { return s->server; }
3147
3148void SSL_enable_fastradio_padding(SSL *s, char on_off) {
3149 s->fastradio_padding = on_off;
3150}