blob: d070e82dacddd237a87c562bd7ab93ae2857f1a8 [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 Langleyfcf25832014-12-18 17:42:32 -08001680int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
1681 unsigned protos_len) {
1682 if (ctx->alpn_client_proto_list) {
1683 OPENSSL_free(ctx->alpn_client_proto_list);
1684 }
Adam Langley95c29f32014-06-20 12:00:00 -07001685
Adam Langleyfcf25832014-12-18 17:42:32 -08001686 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1687 if (!ctx->alpn_client_proto_list) {
1688 return 1;
1689 }
1690 ctx->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001691
Adam Langleyfcf25832014-12-18 17:42:32 -08001692 return 0;
1693}
Adam Langley95c29f32014-06-20 12:00:00 -07001694
Adam Langleyfcf25832014-12-18 17:42:32 -08001695int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
1696 if (ssl->alpn_client_proto_list) {
1697 OPENSSL_free(ssl->alpn_client_proto_list);
1698 }
Adam Langley95c29f32014-06-20 12:00:00 -07001699
Adam Langleyfcf25832014-12-18 17:42:32 -08001700 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1701 if (!ssl->alpn_client_proto_list) {
1702 return 1;
1703 }
1704 ssl->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001705
Adam Langleyfcf25832014-12-18 17:42:32 -08001706 return 0;
1707}
Adam Langley95c29f32014-06-20 12:00:00 -07001708
1709/* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
1710 * during ClientHello processing in order to select an ALPN protocol from the
1711 * client's list of offered protocols. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001712void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
1713 int (*cb)(SSL *ssl, const uint8_t **out,
1714 uint8_t *outlen, const uint8_t *in,
1715 unsigned int inlen, void *arg),
1716 void *arg) {
1717 ctx->alpn_select_cb = cb;
1718 ctx->alpn_select_cb_arg = arg;
1719}
Adam Langley95c29f32014-06-20 12:00:00 -07001720
1721/* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
1722 * On return it sets |*data| to point to |*len| bytes of protocol name (not
1723 * including the leading length-prefix byte). If the server didn't respond with
1724 * a negotiated protocol then |*len| will be zero. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001725void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **data,
1726 unsigned *len) {
1727 *data = NULL;
1728 if (ssl->s3) {
1729 *data = ssl->s3->alpn_selected;
1730 }
1731 if (*data == NULL) {
1732 *len = 0;
1733 } else {
1734 *len = ssl->s3->alpn_selected_len;
1735 }
1736}
Adam Langley95c29f32014-06-20 12:00:00 -07001737
Adam Langleyfcf25832014-12-18 17:42:32 -08001738int SSL_export_keying_material(SSL *s, uint8_t *out, size_t olen,
1739 const char *label, size_t llen, const uint8_t *p,
1740 size_t plen, int use_context) {
1741 if (s->version < TLS1_VERSION) {
1742 return -1;
1743 }
Adam Langley95c29f32014-06-20 12:00:00 -07001744
Adam Langleyfcf25832014-12-18 17:42:32 -08001745 return s->enc_method->export_keying_material(s, out, olen, label, llen, p,
1746 plen, use_context);
1747}
Adam Langley95c29f32014-06-20 12:00:00 -07001748
Adam Langleyfcf25832014-12-18 17:42:32 -08001749static uint32_t ssl_session_hash(const SSL_SESSION *a) {
1750 uint32_t hash =
1751 ((uint32_t)a->session_id[0]) ||
1752 ((uint32_t)a->session_id[1] << 8) ||
1753 ((uint32_t)a->session_id[2] << 16) ||
1754 ((uint32_t)a->session_id[3] << 24);
Adam Langley95c29f32014-06-20 12:00:00 -07001755
Adam Langleyfcf25832014-12-18 17:42:32 -08001756 return hash;
1757}
Adam Langley95c29f32014-06-20 12:00:00 -07001758
1759/* NB: If this function (or indeed the hash function which uses a sort of
1760 * coarser function than this one) is changed, ensure
1761 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1762 * able to construct an SSL_SESSION that will collide with any existing session
1763 * with a matching session ID. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001764static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
1765 if (a->ssl_version != b->ssl_version) {
1766 return 1;
1767 }
Adam Langley95c29f32014-06-20 12:00:00 -07001768
Adam Langleyfcf25832014-12-18 17:42:32 -08001769 if (a->session_id_length != b->session_id_length) {
1770 return 1;
1771 }
Adam Langley95c29f32014-06-20 12:00:00 -07001772
Adam Langleyfcf25832014-12-18 17:42:32 -08001773 return memcmp(a->session_id, b->session_id, a->session_id_length);
1774}
Adam Langley95c29f32014-06-20 12:00:00 -07001775
Adam Langleyfcf25832014-12-18 17:42:32 -08001776SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth) {
1777 SSL_CTX *ret = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001778
Adam Langleyfcf25832014-12-18 17:42:32 -08001779 if (meth == NULL) {
1780 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_NULL_SSL_METHOD_PASSED);
1781 return NULL;
1782 }
Adam Langley95c29f32014-06-20 12:00:00 -07001783
Adam Langleyfcf25832014-12-18 17:42:32 -08001784 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
1785 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
1786 goto err;
1787 }
Adam Langley95c29f32014-06-20 12:00:00 -07001788
Adam Langleyfcf25832014-12-18 17:42:32 -08001789 ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1790 if (ret == NULL) {
1791 goto err;
1792 }
Adam Langley95c29f32014-06-20 12:00:00 -07001793
Adam Langleyfcf25832014-12-18 17:42:32 -08001794 memset(ret, 0, sizeof(SSL_CTX));
Adam Langley95c29f32014-06-20 12:00:00 -07001795
Adam Langleyfcf25832014-12-18 17:42:32 -08001796 ret->method = meth->method;
Adam Langley95c29f32014-06-20 12:00:00 -07001797
Adam Langleyfcf25832014-12-18 17:42:32 -08001798 ret->cert_store = NULL;
1799 ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
1800 ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1801 ret->session_cache_head = NULL;
1802 ret->session_cache_tail = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001803
Adam Langleyfcf25832014-12-18 17:42:32 -08001804 /* We take the system default */
1805 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001806
Adam Langleyfcf25832014-12-18 17:42:32 -08001807 ret->new_session_cb = 0;
1808 ret->remove_session_cb = 0;
1809 ret->get_session_cb = 0;
1810 ret->generate_session_id = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001811
Adam Langleyfcf25832014-12-18 17:42:32 -08001812 memset((char *)&ret->stats, 0, sizeof(ret->stats));
Adam Langley95c29f32014-06-20 12:00:00 -07001813
Adam Langleyfcf25832014-12-18 17:42:32 -08001814 ret->references = 1;
1815 ret->quiet_shutdown = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001816
Adam Langleyfcf25832014-12-18 17:42:32 -08001817 ret->info_callback = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001818
Adam Langleyfcf25832014-12-18 17:42:32 -08001819 ret->app_verify_callback = 0;
1820 ret->app_verify_arg = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001821
Adam Langleyfcf25832014-12-18 17:42:32 -08001822 ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
1823 ret->read_ahead = 0;
1824 ret->msg_callback = 0;
1825 ret->msg_callback_arg = NULL;
1826 ret->verify_mode = SSL_VERIFY_NONE;
1827 ret->sid_ctx_length = 0;
1828 ret->default_verify_callback = NULL;
1829 ret->cert = ssl_cert_new();
1830 if (ret->cert == NULL) {
1831 goto err;
1832 }
Adam Langley95c29f32014-06-20 12:00:00 -07001833
Adam Langleyfcf25832014-12-18 17:42:32 -08001834 ret->default_passwd_callback = 0;
1835 ret->default_passwd_callback_userdata = NULL;
1836 ret->client_cert_cb = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001837
Adam Langleyfcf25832014-12-18 17:42:32 -08001838 ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
1839 if (ret->sessions == NULL) {
1840 goto err;
1841 }
1842 ret->cert_store = X509_STORE_new();
1843 if (ret->cert_store == NULL) {
1844 goto err;
1845 }
Adam Langley95c29f32014-06-20 12:00:00 -07001846
Adam Langleyfcf25832014-12-18 17:42:32 -08001847 ssl_create_cipher_list(ret->method, &ret->cipher_list,
1848 &ret->cipher_list_by_id, SSL_DEFAULT_CIPHER_LIST,
1849 ret->cert);
1850 if (ret->cipher_list == NULL ||
1851 sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0) {
1852 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_LIBRARY_HAS_NO_CIPHERS);
1853 goto err2;
1854 }
Adam Langley95c29f32014-06-20 12:00:00 -07001855
Adam Langleyfcf25832014-12-18 17:42:32 -08001856 ret->param = X509_VERIFY_PARAM_new();
1857 if (!ret->param) {
1858 goto err;
1859 }
Adam Langley95c29f32014-06-20 12:00:00 -07001860
Adam Langleyfcf25832014-12-18 17:42:32 -08001861 ret->client_CA = sk_X509_NAME_new_null();
1862 if (ret->client_CA == NULL) {
1863 goto err;
1864 }
Adam Langley95c29f32014-06-20 12:00:00 -07001865
Adam Langleyfcf25832014-12-18 17:42:32 -08001866 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001867
Adam Langleyfcf25832014-12-18 17:42:32 -08001868 ret->extra_certs = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001869
Adam Langleyfcf25832014-12-18 17:42:32 -08001870 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
David Benjamin422d3a42014-08-20 11:09:03 -04001871
Adam Langleyfcf25832014-12-18 17:42:32 -08001872 ret->tlsext_servername_callback = 0;
1873 ret->tlsext_servername_arg = NULL;
1874 /* Setup RFC4507 ticket keys */
1875 if (!RAND_bytes(ret->tlsext_tick_key_name, 16) ||
1876 !RAND_bytes(ret->tlsext_tick_hmac_key, 16) ||
1877 !RAND_bytes(ret->tlsext_tick_aes_key, 16)) {
1878 ret->options |= SSL_OP_NO_TICKET;
1879 }
Adam Langley95c29f32014-06-20 12:00:00 -07001880
Adam Langleyfcf25832014-12-18 17:42:32 -08001881 ret->tlsext_status_cb = 0;
1882 ret->tlsext_status_arg = NULL;
David Benjamin82c9e902014-12-12 15:55:27 -05001883
Adam Langleyfcf25832014-12-18 17:42:32 -08001884 ret->next_protos_advertised_cb = 0;
1885 ret->next_proto_select_cb = 0;
1886 ret->psk_identity_hint = NULL;
1887 ret->psk_client_callback = NULL;
1888 ret->psk_server_callback = NULL;
1889
1890 /* Default is to connect to non-RI servers. When RI is more widely deployed
1891 * might change this. */
1892 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
1893
1894 /* Lock the SSL_CTX to the specified version, for compatibility with legacy
1895 * uses of SSL_METHOD. */
1896 if (meth->version != 0) {
1897 SSL_CTX_set_max_version(ret, meth->version);
1898 SSL_CTX_set_min_version(ret, meth->version);
1899 }
1900
1901 return ret;
1902
Adam Langley95c29f32014-06-20 12:00:00 -07001903err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001904 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -07001905err2:
Adam Langleyfcf25832014-12-18 17:42:32 -08001906 if (ret != NULL) {
1907 SSL_CTX_free(ret);
1908 }
1909 return NULL;
1910}
Adam Langley95c29f32014-06-20 12:00:00 -07001911
Adam Langleyfcf25832014-12-18 17:42:32 -08001912void SSL_CTX_free(SSL_CTX *a) {
1913 int i;
Adam Langley95c29f32014-06-20 12:00:00 -07001914
Adam Langleyfcf25832014-12-18 17:42:32 -08001915 if (a == NULL) {
1916 return;
1917 }
Adam Langley95c29f32014-06-20 12:00:00 -07001918
Adam Langleyfcf25832014-12-18 17:42:32 -08001919 i = CRYPTO_add(&a->references, -1, CRYPTO_LOCK_SSL_CTX);
1920 if (i > 0) {
1921 return;
1922 }
Adam Langley95c29f32014-06-20 12:00:00 -07001923
Adam Langleyfcf25832014-12-18 17:42:32 -08001924 if (a->param) {
1925 X509_VERIFY_PARAM_free(a->param);
1926 }
Adam Langley95c29f32014-06-20 12:00:00 -07001927
Adam Langleyfcf25832014-12-18 17:42:32 -08001928 /* Free internal session cache. However: the remove_cb() may reference the
1929 * ex_data of SSL_CTX, thus the ex_data store can only be removed after the
1930 * sessions were flushed. As the ex_data handling routines might also touch
1931 * the session cache, the most secure solution seems to be: empty (flush) the
1932 * cache, then free ex_data, then finally free the cache. (See ticket
1933 * [openssl.org #212].) */
1934 if (a->sessions != NULL) {
1935 SSL_CTX_flush_sessions(a, 0);
1936 }
Adam Langley95c29f32014-06-20 12:00:00 -07001937
Adam Langleyfcf25832014-12-18 17:42:32 -08001938 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001939
Adam Langleyfcf25832014-12-18 17:42:32 -08001940 if (a->sessions != NULL) {
1941 lh_SSL_SESSION_free(a->sessions);
1942 }
1943 if (a->cert_store != NULL) {
1944 X509_STORE_free(a->cert_store);
1945 }
1946 if (a->cipher_list != NULL) {
1947 ssl_cipher_preference_list_free(a->cipher_list);
1948 }
1949 if (a->cipher_list_by_id != NULL) {
1950 sk_SSL_CIPHER_free(a->cipher_list_by_id);
1951 }
1952 if (a->cipher_list_tls11 != NULL) {
1953 ssl_cipher_preference_list_free(a->cipher_list_tls11);
1954 }
1955 if (a->cert != NULL) {
1956 ssl_cert_free(a->cert);
1957 }
1958 if (a->client_CA != NULL) {
1959 sk_X509_NAME_pop_free(a->client_CA, X509_NAME_free);
1960 }
1961 if (a->extra_certs != NULL) {
1962 sk_X509_pop_free(a->extra_certs, X509_free);
1963 }
1964 if (a->srtp_profiles) {
1965 sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);
1966 }
1967 if (a->psk_identity_hint) {
1968 OPENSSL_free(a->psk_identity_hint);
1969 }
1970 if (a->tlsext_ecpointformatlist) {
1971 OPENSSL_free(a->tlsext_ecpointformatlist);
1972 }
1973 if (a->tlsext_ellipticcurvelist) {
1974 OPENSSL_free(a->tlsext_ellipticcurvelist);
1975 }
1976 if (a->alpn_client_proto_list != NULL) {
1977 OPENSSL_free(a->alpn_client_proto_list);
1978 }
1979 if (a->tlsext_channel_id_private) {
1980 EVP_PKEY_free(a->tlsext_channel_id_private);
1981 }
1982 if (a->keylog_bio) {
1983 BIO_free(a->keylog_bio);
1984 }
Adam Langley95c29f32014-06-20 12:00:00 -07001985
Adam Langleyfcf25832014-12-18 17:42:32 -08001986 OPENSSL_free(a);
1987}
Adam Langley95c29f32014-06-20 12:00:00 -07001988
Adam Langleyfcf25832014-12-18 17:42:32 -08001989void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) {
1990 ctx->default_passwd_callback = cb;
1991}
Adam Langley95c29f32014-06-20 12:00:00 -07001992
Adam Langleyfcf25832014-12-18 17:42:32 -08001993void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u) {
1994 ctx->default_passwd_callback_userdata = u;
1995}
Adam Langley95c29f32014-06-20 12:00:00 -07001996
Adam Langleyfcf25832014-12-18 17:42:32 -08001997void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
1998 int (*cb)(X509_STORE_CTX *, void *),
1999 void *arg) {
2000 ctx->app_verify_callback = cb;
2001 ctx->app_verify_arg = arg;
2002}
Adam Langley95c29f32014-06-20 12:00:00 -07002003
Adam Langleyfcf25832014-12-18 17:42:32 -08002004void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
2005 int (*cb)(int, X509_STORE_CTX *)) {
2006 ctx->verify_mode = mode;
2007 ctx->default_verify_callback = cb;
2008}
Adam Langley95c29f32014-06-20 12:00:00 -07002009
Adam Langleyfcf25832014-12-18 17:42:32 -08002010void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
2011 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
2012}
Adam Langley1258b6a2014-06-20 12:00:00 -07002013
Adam Langleyfcf25832014-12-18 17:42:32 -08002014void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb)(SSL *ssl, void *arg),
2015 void *arg) {
2016 ssl_cert_set_cert_cb(c->cert, cb, arg);
2017}
David Benjamin859ec3c2014-09-02 16:29:36 -04002018
Adam Langleyfcf25832014-12-18 17:42:32 -08002019void SSL_set_cert_cb(SSL *s, int (*cb)(SSL *ssl, void *arg), void *arg) {
2020 ssl_cert_set_cert_cb(s->cert, cb, arg);
2021}
Adam Langley95c29f32014-06-20 12:00:00 -07002022
Adam Langleyfcf25832014-12-18 17:42:32 -08002023static int ssl_has_key(SSL *s, size_t idx) {
2024 CERT_PKEY *cpk = &s->cert->pkeys[idx];
2025 return cpk->x509 && cpk->privatekey;
2026}
David Benjamin033e5f42014-11-13 18:47:41 -05002027
David Benjaminf31e6812014-11-13 18:05:55 -05002028void ssl_get_compatible_server_ciphers(SSL *s, unsigned long *out_mask_k,
Adam Langleyfcf25832014-12-18 17:42:32 -08002029 unsigned long *out_mask_a) {
2030 CERT *c = s->cert;
2031 int rsa_enc, rsa_sign, dh_tmp;
2032 unsigned long mask_k, mask_a;
2033 int have_ecc_cert, ecdsa_ok;
2034 int have_ecdh_tmp;
2035 X509 *x;
Adam Langley95c29f32014-06-20 12:00:00 -07002036
Adam Langleyfcf25832014-12-18 17:42:32 -08002037 if (c == NULL) {
2038 /* TODO(davidben): Is this codepath possible? */
2039 *out_mask_k = 0;
2040 *out_mask_a = 0;
2041 return;
2042 }
Adam Langley95c29f32014-06-20 12:00:00 -07002043
Adam Langleyfcf25832014-12-18 17:42:32 -08002044 dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07002045
Adam Langleyfcf25832014-12-18 17:42:32 -08002046 have_ecdh_tmp = (c->ecdh_tmp || c->ecdh_tmp_cb || c->ecdh_tmp_auto);
2047 rsa_enc = ssl_has_key(s, SSL_PKEY_RSA_ENC);
2048 rsa_sign = ssl_has_key(s, SSL_PKEY_RSA_SIGN);
2049 have_ecc_cert = ssl_has_key(s, SSL_PKEY_ECC);
2050 mask_k = 0;
2051 mask_a = 0;
David Benjaminf31e6812014-11-13 18:05:55 -05002052
Adam Langleyfcf25832014-12-18 17:42:32 -08002053 if (rsa_enc) {
2054 mask_k |= SSL_kRSA;
2055 }
2056 if (dh_tmp) {
2057 mask_k |= SSL_kEDH;
2058 }
2059 if (rsa_enc || rsa_sign) {
2060 mask_a |= SSL_aRSA;
2061 }
Adam Langley95c29f32014-06-20 12:00:00 -07002062
Adam Langleyfcf25832014-12-18 17:42:32 -08002063 mask_a |= SSL_aNULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002064
Adam Langleyfcf25832014-12-18 17:42:32 -08002065 /* An ECC certificate may be usable for ECDSA cipher suites depending on the
2066 * key usage extension and on the client's curve preferences. */
2067 if (have_ecc_cert) {
2068 x = c->pkeys[SSL_PKEY_ECC].x509;
2069 /* This call populates extension flags (ex_flags). */
2070 X509_check_purpose(x, -1, 0);
2071 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE)
2072 ? (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)
2073 : 1;
2074 if (!tls1_check_ec_cert(s, x)) {
2075 ecdsa_ok = 0;
2076 }
2077 if (ecdsa_ok) {
2078 mask_a |= SSL_aECDSA;
2079 }
2080 }
Adam Langley95c29f32014-06-20 12:00:00 -07002081
Adam Langleyfcf25832014-12-18 17:42:32 -08002082 /* If we are considering an ECC cipher suite that uses an ephemeral EC
2083 * key, check it. */
2084 if (have_ecdh_tmp && tls1_check_ec_tmp_key(s)) {
2085 mask_k |= SSL_kEECDH;
2086 }
Adam Langley95c29f32014-06-20 12:00:00 -07002087
Adam Langleyfcf25832014-12-18 17:42:32 -08002088 /* PSK requires a server callback. */
2089 if (s->psk_server_callback != NULL) {
2090 mask_k |= SSL_kPSK;
2091 mask_a |= SSL_aPSK;
2092 }
Adam Langley95c29f32014-06-20 12:00:00 -07002093
Adam Langleyfcf25832014-12-18 17:42:32 -08002094 *out_mask_k = mask_k;
2095 *out_mask_a = mask_a;
2096}
Adam Langley95c29f32014-06-20 12:00:00 -07002097
2098/* This handy macro borrowed from crypto/x509v3/v3_purp.c */
2099#define ku_reject(x, usage) \
Adam Langleyfcf25832014-12-18 17:42:32 -08002100 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
Adam Langley95c29f32014-06-20 12:00:00 -07002101
Adam Langleyfcf25832014-12-18 17:42:32 -08002102int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s) {
2103 unsigned long alg_a;
2104 int signature_nid = 0, md_nid = 0, pk_nid = 0;
2105 const SSL_CIPHER *cs = s->s3->tmp.new_cipher;
Adam Langley95c29f32014-06-20 12:00:00 -07002106
Adam Langleyfcf25832014-12-18 17:42:32 -08002107 alg_a = cs->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -07002108
Adam Langleyfcf25832014-12-18 17:42:32 -08002109 /* This call populates the ex_flags field correctly */
2110 X509_check_purpose(x, -1, 0);
2111 if (x->sig_alg && x->sig_alg->algorithm) {
2112 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
2113 OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
2114 }
2115 if (alg_a & SSL_aECDSA) {
2116 /* key usage, if present, must allow signing */
2117 if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE)) {
2118 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg,
2119 SSL_R_ECC_CERT_NOT_FOR_SIGNING);
2120 return 0;
2121 }
2122 }
Adam Langley95c29f32014-06-20 12:00:00 -07002123
Adam Langleyfcf25832014-12-18 17:42:32 -08002124 return 1; /* all checks are ok */
2125}
Adam Langley95c29f32014-06-20 12:00:00 -07002126
Adam Langleyfcf25832014-12-18 17:42:32 -08002127static int ssl_get_server_cert_index(const SSL *s) {
2128 int idx;
2129 idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
2130 if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509) {
2131 idx = SSL_PKEY_RSA_SIGN;
2132 }
2133 if (idx == -1) {
2134 OPENSSL_PUT_ERROR(SSL, ssl_get_server_cert_index, ERR_R_INTERNAL_ERROR);
2135 }
2136 return idx;
2137}
Adam Langley95c29f32014-06-20 12:00:00 -07002138
Adam Langleyfcf25832014-12-18 17:42:32 -08002139CERT_PKEY *ssl_get_server_send_pkey(const SSL *s) {
2140 int i = ssl_get_server_cert_index(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002141
Adam Langleyfcf25832014-12-18 17:42:32 -08002142 /* This may or may not be an error. */
2143 if (i < 0) {
2144 return NULL;
2145 }
Adam Langley95c29f32014-06-20 12:00:00 -07002146
Adam Langleyfcf25832014-12-18 17:42:32 -08002147 /* May be NULL. */
2148 return &s->cert->pkeys[i];
2149}
Adam Langley95c29f32014-06-20 12:00:00 -07002150
Adam Langleyfcf25832014-12-18 17:42:32 -08002151EVP_PKEY *ssl_get_sign_pkey(SSL *s, const SSL_CIPHER *cipher) {
2152 unsigned long alg_a;
2153 CERT *c;
2154 int idx = -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002155
Adam Langleyfcf25832014-12-18 17:42:32 -08002156 alg_a = cipher->algorithm_auth;
2157 c = s->cert;
Adam Langley95c29f32014-06-20 12:00:00 -07002158
Adam Langleyfcf25832014-12-18 17:42:32 -08002159 if (alg_a & SSL_aRSA) {
2160 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL) {
2161 idx = SSL_PKEY_RSA_SIGN;
2162 } else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL) {
2163 idx = SSL_PKEY_RSA_ENC;
2164 }
2165 } else if ((alg_a & SSL_aECDSA) &&
2166 (c->pkeys[SSL_PKEY_ECC].privatekey != NULL)) {
2167 idx = SSL_PKEY_ECC;
2168 }
Adam Langley95c29f32014-06-20 12:00:00 -07002169
Adam Langleyfcf25832014-12-18 17:42:32 -08002170 if (idx == -1) {
2171 OPENSSL_PUT_ERROR(SSL, ssl_get_sign_pkey, ERR_R_INTERNAL_ERROR);
2172 return NULL;
2173 }
Adam Langley95c29f32014-06-20 12:00:00 -07002174
Adam Langleyfcf25832014-12-18 17:42:32 -08002175 return c->pkeys[idx].privatekey;
2176}
Adam Langley95c29f32014-06-20 12:00:00 -07002177
Adam Langleyfcf25832014-12-18 17:42:32 -08002178void ssl_update_cache(SSL *s, int mode) {
2179 int i;
Adam Langley95c29f32014-06-20 12:00:00 -07002180
Adam Langleyfcf25832014-12-18 17:42:32 -08002181 /* If the session_id_length is 0, we are not supposed to cache it, and it
2182 * would be rather hard to do anyway :-) */
2183 if (s->session->session_id_length == 0) {
2184 return;
2185 }
Adam Langley95c29f32014-06-20 12:00:00 -07002186
Adam Langleyfcf25832014-12-18 17:42:32 -08002187 i = s->initial_ctx->session_cache_mode;
2188 if ((i & mode) && !s->hit &&
2189 ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE) ||
2190 SSL_CTX_add_session(s->initial_ctx, s->session)) &&
2191 s->initial_ctx->new_session_cb != NULL) {
David Benjamin33639842015-02-09 03:34:47 -05002192 if (!s->initial_ctx->new_session_cb(s, SSL_SESSION_up_ref(s->session))) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002193 SSL_SESSION_free(s->session);
2194 }
2195 }
Adam Langley95c29f32014-06-20 12:00:00 -07002196
Adam Langleyfcf25832014-12-18 17:42:32 -08002197 /* auto flush every 255 connections */
2198 if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) && ((i & mode) == mode)) {
2199 if ((((mode & SSL_SESS_CACHE_CLIENT)
2200 ? s->initial_ctx->stats.sess_connect_good
2201 : s->initial_ctx->stats.sess_accept_good) &
2202 0xff) == 0xff) {
2203 SSL_CTX_flush_sessions(s->initial_ctx, (unsigned long)time(NULL));
2204 }
2205 }
2206}
Adam Langley95c29f32014-06-20 12:00:00 -07002207
Adam Langleyfcf25832014-12-18 17:42:32 -08002208int SSL_get_error(const SSL *s, int i) {
2209 int reason;
2210 unsigned long l;
2211 BIO *bio;
Adam Langley95c29f32014-06-20 12:00:00 -07002212
Adam Langleyfcf25832014-12-18 17:42:32 -08002213 if (i > 0) {
2214 return SSL_ERROR_NONE;
2215 }
Adam Langley95c29f32014-06-20 12:00:00 -07002216
Adam Langleyfcf25832014-12-18 17:42:32 -08002217 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
2218 * where we do encode the error */
2219 l = ERR_peek_error();
2220 if (l != 0) {
2221 if (ERR_GET_LIB(l) == ERR_LIB_SYS) {
2222 return SSL_ERROR_SYSCALL;
2223 }
2224 return SSL_ERROR_SSL;
2225 }
Adam Langley95c29f32014-06-20 12:00:00 -07002226
David Benjamin9a38e922015-01-22 16:06:11 -05002227 if (i == 0) {
2228 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2229 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) {
2230 /* The socket was cleanly shut down with a close_notify. */
2231 return SSL_ERROR_ZERO_RETURN;
2232 }
2233 /* An EOF was observed which violates the protocol, and the underlying
2234 * transport does not participate in the error queue. Bubble up to the
2235 * caller. */
2236 return SSL_ERROR_SYSCALL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002237 }
Adam Langleyb2ce0582014-06-20 12:00:00 -07002238
Adam Langleyfcf25832014-12-18 17:42:32 -08002239 if (SSL_want_session(s)) {
2240 return SSL_ERROR_PENDING_SESSION;
2241 }
Adam Langley95c29f32014-06-20 12:00:00 -07002242
Adam Langleyfcf25832014-12-18 17:42:32 -08002243 if (SSL_want_certificate(s)) {
2244 return SSL_ERROR_PENDING_CERTIFICATE;
2245 }
Adam Langley95c29f32014-06-20 12:00:00 -07002246
Adam Langleyfcf25832014-12-18 17:42:32 -08002247 if (SSL_want_read(s)) {
2248 bio = SSL_get_rbio(s);
2249 if (BIO_should_read(bio)) {
2250 return SSL_ERROR_WANT_READ;
2251 }
Adam Langley95c29f32014-06-20 12:00:00 -07002252
Adam Langleyfcf25832014-12-18 17:42:32 -08002253 if (BIO_should_write(bio)) {
2254 /* This one doesn't make too much sense ... We never try to write to the
2255 * rbio, and an application program where rbio and wbio are separate
2256 * couldn't even know what it should wait for. However if we ever set
2257 * s->rwstate incorrectly (so that we have SSL_want_read(s) instead of
2258 * SSL_want_write(s)) and rbio and wbio *are* the same, this test works
2259 * around that bug; so it might be safer to keep it. */
2260 return SSL_ERROR_WANT_WRITE;
2261 }
Adam Langley95c29f32014-06-20 12:00:00 -07002262
Adam Langleyfcf25832014-12-18 17:42:32 -08002263 if (BIO_should_io_special(bio)) {
2264 reason = BIO_get_retry_reason(bio);
2265 if (reason == BIO_RR_CONNECT) {
2266 return SSL_ERROR_WANT_CONNECT;
2267 }
Adam Langley95c29f32014-06-20 12:00:00 -07002268
Adam Langleyfcf25832014-12-18 17:42:32 -08002269 if (reason == BIO_RR_ACCEPT) {
2270 return SSL_ERROR_WANT_ACCEPT;
2271 }
Adam Langley95c29f32014-06-20 12:00:00 -07002272
Adam Langleyfcf25832014-12-18 17:42:32 -08002273 return SSL_ERROR_SYSCALL; /* unknown */
2274 }
2275 }
Adam Langley95c29f32014-06-20 12:00:00 -07002276
Adam Langleyfcf25832014-12-18 17:42:32 -08002277 if (SSL_want_write(s)) {
2278 bio = SSL_get_wbio(s);
2279 if (BIO_should_write(bio)) {
2280 return SSL_ERROR_WANT_WRITE;
2281 }
Adam Langley95c29f32014-06-20 12:00:00 -07002282
Adam Langleyfcf25832014-12-18 17:42:32 -08002283 if (BIO_should_read(bio)) {
2284 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2285 return SSL_ERROR_WANT_READ;
2286 }
Adam Langley95c29f32014-06-20 12:00:00 -07002287
Adam Langleyfcf25832014-12-18 17:42:32 -08002288 if (BIO_should_io_special(bio)) {
2289 reason = BIO_get_retry_reason(bio);
2290 if (reason == BIO_RR_CONNECT) {
2291 return SSL_ERROR_WANT_CONNECT;
2292 }
Adam Langley95c29f32014-06-20 12:00:00 -07002293
Adam Langleyfcf25832014-12-18 17:42:32 -08002294 if (reason == BIO_RR_ACCEPT) {
2295 return SSL_ERROR_WANT_ACCEPT;
2296 }
Adam Langley95c29f32014-06-20 12:00:00 -07002297
Adam Langleyfcf25832014-12-18 17:42:32 -08002298 return SSL_ERROR_SYSCALL;
2299 }
2300 }
Adam Langley95c29f32014-06-20 12:00:00 -07002301
Adam Langleyfcf25832014-12-18 17:42:32 -08002302 if (SSL_want_x509_lookup(s)) {
2303 return SSL_ERROR_WANT_X509_LOOKUP;
2304 }
Adam Langley95c29f32014-06-20 12:00:00 -07002305
Adam Langleyfcf25832014-12-18 17:42:32 -08002306 if (SSL_want_channel_id_lookup(s)) {
2307 return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
2308 }
Adam Langley0f4746e2014-08-13 12:26:32 -07002309
Adam Langleyfcf25832014-12-18 17:42:32 -08002310 return SSL_ERROR_SYSCALL;
2311}
Adam Langley0f4746e2014-08-13 12:26:32 -07002312
Adam Langleyfcf25832014-12-18 17:42:32 -08002313int SSL_do_handshake(SSL *s) {
2314 int ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002315
Adam Langleyfcf25832014-12-18 17:42:32 -08002316 if (s->handshake_func == NULL) {
2317 OPENSSL_PUT_ERROR(SSL, SSL_do_handshake, SSL_R_CONNECTION_TYPE_NOT_SET);
2318 return -1;
2319 }
Adam Langley95c29f32014-06-20 12:00:00 -07002320
Adam Langleyfcf25832014-12-18 17:42:32 -08002321 s->method->ssl_renegotiate_check(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002322
Adam Langleyfcf25832014-12-18 17:42:32 -08002323 if (SSL_in_init(s)) {
2324 ret = s->handshake_func(s);
2325 }
2326 return ret;
2327}
Adam Langley95c29f32014-06-20 12:00:00 -07002328
Adam Langleyfcf25832014-12-18 17:42:32 -08002329void SSL_set_accept_state(SSL *s) {
2330 s->server = 1;
2331 s->shutdown = 0;
2332 s->state = SSL_ST_ACCEPT | SSL_ST_BEFORE;
2333 s->handshake_func = s->method->ssl_accept;
2334 /* clear the current cipher */
2335 ssl_clear_cipher_ctx(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002336}
Adam Langley95c29f32014-06-20 12:00:00 -07002337
Adam Langleyfcf25832014-12-18 17:42:32 -08002338void SSL_set_connect_state(SSL *s) {
2339 s->server = 0;
2340 s->shutdown = 0;
2341 s->state = SSL_ST_CONNECT | SSL_ST_BEFORE;
2342 s->handshake_func = s->method->ssl_connect;
2343 /* clear the current cipher */
2344 ssl_clear_cipher_ctx(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002345}
Adam Langley95c29f32014-06-20 12:00:00 -07002346
Adam Langleyfcf25832014-12-18 17:42:32 -08002347int ssl_undefined_function(SSL *s) {
2348 OPENSSL_PUT_ERROR(SSL, ssl_undefined_function,
2349 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2350 return 0;
2351}
Adam Langley95c29f32014-06-20 12:00:00 -07002352
Adam Langleyfcf25832014-12-18 17:42:32 -08002353int ssl_undefined_void_function(void) {
2354 OPENSSL_PUT_ERROR(SSL, ssl_undefined_void_function,
2355 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2356 return 0;
2357}
Adam Langley95c29f32014-06-20 12:00:00 -07002358
Adam Langleyfcf25832014-12-18 17:42:32 -08002359int ssl_undefined_const_function(const SSL *s) {
2360 OPENSSL_PUT_ERROR(SSL, ssl_undefined_const_function,
2361 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2362 return 0;
2363}
Adam Langley95c29f32014-06-20 12:00:00 -07002364
Adam Langleyfcf25832014-12-18 17:42:32 -08002365static const char *ssl_get_version(int version) {
2366 switch (version) {
2367 case TLS1_2_VERSION:
2368 return "TLSv1.2";
Adam Langley95c29f32014-06-20 12:00:00 -07002369
Adam Langleyfcf25832014-12-18 17:42:32 -08002370 case TLS1_1_VERSION:
2371 return "TLSv1.1";
Adam Langley95c29f32014-06-20 12:00:00 -07002372
Adam Langleyfcf25832014-12-18 17:42:32 -08002373 case TLS1_VERSION:
2374 return "TLSv1";
Adam Langley95c29f32014-06-20 12:00:00 -07002375
Adam Langleyfcf25832014-12-18 17:42:32 -08002376 case SSL3_VERSION:
2377 return "SSLv3";
Adam Langley95c29f32014-06-20 12:00:00 -07002378
Adam Langleyfcf25832014-12-18 17:42:32 -08002379 default:
2380 return "unknown";
2381 }
2382}
Adam Langley95c29f32014-06-20 12:00:00 -07002383
Adam Langleyfcf25832014-12-18 17:42:32 -08002384const char *SSL_get_version(const SSL *s) {
2385 return ssl_get_version(s->version);
2386}
Adam Langley95c29f32014-06-20 12:00:00 -07002387
Adam Langleyfcf25832014-12-18 17:42:32 -08002388const char *SSL_SESSION_get_version(const SSL_SESSION *sess) {
2389 return ssl_get_version(sess->ssl_version);
2390}
Adam Langley95c29f32014-06-20 12:00:00 -07002391
Adam Langleyfcf25832014-12-18 17:42:32 -08002392void ssl_clear_cipher_ctx(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002393 if (s->aead_read_ctx != NULL) {
2394 EVP_AEAD_CTX_cleanup(&s->aead_read_ctx->ctx);
2395 OPENSSL_free(s->aead_read_ctx);
2396 s->aead_read_ctx = NULL;
2397 }
Adam Langleya5dc5452014-06-20 12:00:00 -07002398
Adam Langleyfcf25832014-12-18 17:42:32 -08002399 if (s->aead_write_ctx != NULL) {
2400 EVP_AEAD_CTX_cleanup(&s->aead_write_ctx->ctx);
2401 OPENSSL_free(s->aead_write_ctx);
2402 s->aead_write_ctx = NULL;
2403 }
2404}
Adam Langley95c29f32014-06-20 12:00:00 -07002405
Adam Langleyfcf25832014-12-18 17:42:32 -08002406X509 *SSL_get_certificate(const SSL *s) {
2407 if (s->cert != NULL) {
2408 return s->cert->key->x509;
2409 }
2410
2411 return NULL;
2412}
2413
2414EVP_PKEY *SSL_get_privatekey(const SSL *s) {
2415 if (s->cert != NULL) {
2416 return s->cert->key->privatekey;
2417 }
2418
2419 return NULL;
2420}
2421
2422X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
2423 if (ctx->cert != NULL) {
2424 return ctx->cert->key->x509;
2425 }
2426
2427 return NULL;
2428}
2429
2430EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
2431 if (ctx->cert != NULL) {
2432 return ctx->cert->key->privatekey;
2433 }
2434
2435 return NULL;
2436}
2437
2438const SSL_CIPHER *SSL_get_current_cipher(const SSL *s) {
2439 if (s->session != NULL && s->session->cipher != NULL) {
2440 return s->session->cipher;
2441 }
2442
2443 return NULL;
2444}
2445
2446const void *SSL_get_current_compression(SSL *s) { return NULL; }
2447
2448const void *SSL_get_current_expansion(SSL *s) { return NULL; }
2449
2450int ssl_init_wbio_buffer(SSL *s, int push) {
2451 BIO *bbio;
2452
2453 if (s->bbio == NULL) {
2454 bbio = BIO_new(BIO_f_buffer());
2455 if (bbio == NULL) {
2456 return 0;
2457 }
2458 s->bbio = bbio;
2459 } else {
2460 bbio = s->bbio;
2461 if (s->bbio == s->wbio) {
2462 s->wbio = BIO_pop(s->wbio);
2463 }
2464 }
2465
2466 BIO_reset(bbio);
2467 if (!BIO_set_read_buffer_size(bbio, 1)) {
2468 OPENSSL_PUT_ERROR(SSL, ssl_init_wbio_buffer, ERR_R_BUF_LIB);
2469 return 0;
2470 }
2471
2472 if (push) {
2473 if (s->wbio != bbio) {
2474 s->wbio = BIO_push(bbio, s->wbio);
2475 }
2476 } else {
2477 if (s->wbio == bbio) {
2478 s->wbio = BIO_pop(bbio);
2479 }
2480 }
2481
2482 return 1;
2483}
2484
2485void ssl_free_wbio_buffer(SSL *s) {
2486 if (s->bbio == NULL) {
2487 return;
2488 }
2489
2490 if (s->bbio == s->wbio) {
2491 /* remove buffering */
2492 s->wbio = BIO_pop(s->wbio);
2493 }
2494
2495 BIO_free(s->bbio);
2496 s->bbio = NULL;
2497}
2498
2499void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
2500 ctx->quiet_shutdown = mode;
2501}
2502
2503int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
2504 return ctx->quiet_shutdown;
2505}
2506
2507void SSL_set_quiet_shutdown(SSL *s, int mode) { s->quiet_shutdown = mode; }
2508
2509int SSL_get_quiet_shutdown(const SSL *s) { return s->quiet_shutdown; }
2510
2511void SSL_set_shutdown(SSL *s, int mode) { s->shutdown = mode; }
2512
2513int SSL_get_shutdown(const SSL *s) { return s->shutdown; }
2514
2515int SSL_version(const SSL *s) { return s->version; }
2516
2517SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
2518
2519SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
2520 if (ssl->ctx == ctx) {
2521 return ssl->ctx;
2522 }
2523
2524 if (ctx == NULL) {
2525 ctx = ssl->initial_ctx;
2526 }
2527
2528 if (ssl->cert != NULL) {
2529 ssl_cert_free(ssl->cert);
2530 }
2531
2532 ssl->cert = ssl_cert_dup(ctx->cert);
2533 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
2534 if (ssl->ctx != NULL) {
2535 SSL_CTX_free(ssl->ctx); /* decrement reference count */
2536 }
2537 ssl->ctx = ctx;
2538
2539 ssl->sid_ctx_length = ctx->sid_ctx_length;
2540 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2541 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2542
2543 return ssl->ctx;
2544}
2545
2546int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
2547 return X509_STORE_set_default_paths(ctx->cert_store);
2548}
Adam Langley95c29f32014-06-20 12:00:00 -07002549
2550int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
Adam Langleyfcf25832014-12-18 17:42:32 -08002551 const char *CApath) {
2552 return X509_STORE_load_locations(ctx->cert_store, CAfile, CApath);
2553}
Adam Langley95c29f32014-06-20 12:00:00 -07002554
2555void SSL_set_info_callback(SSL *ssl,
Adam Langleyfcf25832014-12-18 17:42:32 -08002556 void (*cb)(const SSL *ssl, int type, int val)) {
2557 ssl->info_callback = cb;
2558}
Adam Langley95c29f32014-06-20 12:00:00 -07002559
Adam Langleyfcf25832014-12-18 17:42:32 -08002560void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/, int /*type*/,
2561 int /*val*/) {
2562 return ssl->info_callback;
2563}
Adam Langley95c29f32014-06-20 12:00:00 -07002564
Adam Langleyfcf25832014-12-18 17:42:32 -08002565int SSL_state(const SSL *ssl) { return ssl->state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002566
Adam Langleyfcf25832014-12-18 17:42:32 -08002567void SSL_set_state(SSL *ssl, int state) { ssl->state = state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002568
Adam Langleyfcf25832014-12-18 17:42:32 -08002569void SSL_set_verify_result(SSL *ssl, long arg) { ssl->verify_result = arg; }
Adam Langley95c29f32014-06-20 12:00:00 -07002570
Adam Langleyfcf25832014-12-18 17:42:32 -08002571long SSL_get_verify_result(const SSL *ssl) { return ssl->verify_result; }
Adam Langley95c29f32014-06-20 12:00:00 -07002572
Adam Langleyfcf25832014-12-18 17:42:32 -08002573int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2574 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
2575 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp, new_func,
2576 dup_func, free_func);
2577}
Adam Langley95c29f32014-06-20 12:00:00 -07002578
Adam Langleyfcf25832014-12-18 17:42:32 -08002579int SSL_set_ex_data(SSL *s, int idx, void *arg) {
2580 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2581}
Adam Langley95c29f32014-06-20 12:00:00 -07002582
Adam Langleyfcf25832014-12-18 17:42:32 -08002583void *SSL_get_ex_data(const SSL *s, int idx) {
2584 return CRYPTO_get_ex_data(&s->ex_data, idx);
2585}
Adam Langley95c29f32014-06-20 12:00:00 -07002586
Adam Langleyfcf25832014-12-18 17:42:32 -08002587int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2588 CRYPTO_EX_dup *dup_func,
2589 CRYPTO_EX_free *free_func) {
2590 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp, new_func,
2591 dup_func, free_func);
2592}
Adam Langley95c29f32014-06-20 12:00:00 -07002593
Adam Langleyfcf25832014-12-18 17:42:32 -08002594int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg) {
2595 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2596}
Adam Langley95c29f32014-06-20 12:00:00 -07002597
Adam Langleyfcf25832014-12-18 17:42:32 -08002598void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx) {
2599 return CRYPTO_get_ex_data(&s->ex_data, idx);
2600}
Adam Langley95c29f32014-06-20 12:00:00 -07002601
Adam Langleyfcf25832014-12-18 17:42:32 -08002602int ssl_ok(SSL *s) { return 1; }
Adam Langley95c29f32014-06-20 12:00:00 -07002603
Adam Langleyfcf25832014-12-18 17:42:32 -08002604X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
2605 return ctx->cert_store;
2606}
Adam Langley95c29f32014-06-20 12:00:00 -07002607
Adam Langleyfcf25832014-12-18 17:42:32 -08002608void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
2609 if (ctx->cert_store != NULL) {
2610 X509_STORE_free(ctx->cert_store);
2611 }
2612 ctx->cert_store = store;
2613}
Adam Langley95c29f32014-06-20 12:00:00 -07002614
Adam Langleyfcf25832014-12-18 17:42:32 -08002615int SSL_want(const SSL *s) { return s->rwstate; }
Adam Langley95c29f32014-06-20 12:00:00 -07002616
Adam Langleyfcf25832014-12-18 17:42:32 -08002617void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
2618 RSA *(*cb)(SSL *ssl, int is_export,
2619 int keylength)) {
2620 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_RSA_CB, (void (*)(void))cb);
2621}
Adam Langley95c29f32014-06-20 12:00:00 -07002622
Adam Langleyfcf25832014-12-18 17:42:32 -08002623void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
2624 int keylength)) {
2625 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_RSA_CB, (void (*)(void))cb);
2626}
2627
2628void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
2629 DH *(*dh)(SSL *ssl, int is_export,
2630 int keylength)) {
2631 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh);
2632}
2633
2634void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*dh)(SSL *ssl, int is_export,
2635 int keylength)) {
2636 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh);
2637}
2638
2639void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,
2640 EC_KEY *(*ecdh)(SSL *ssl, int is_export,
2641 int keylength)) {
2642 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH_CB, (void (*)(void))ecdh);
2643}
2644
2645void SSL_set_tmp_ecdh_callback(SSL *ssl,
2646 EC_KEY *(*ecdh)(SSL *ssl, int is_export,
2647 int keylength)) {
2648 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_ECDH_CB, (void (*)(void))ecdh);
2649}
2650
2651int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
2652 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2653 OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_psk_identity_hint,
2654 SSL_R_DATA_LENGTH_TOO_LONG);
2655 return 0;
2656 }
2657
2658 if (ctx->psk_identity_hint != NULL) {
2659 OPENSSL_free(ctx->psk_identity_hint);
2660 }
2661
2662 if (identity_hint != NULL) {
2663 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2664 if (ctx->psk_identity_hint == NULL) {
2665 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002666 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002667 } else {
2668 ctx->psk_identity_hint = NULL;
2669 }
Adam Langley95c29f32014-06-20 12:00:00 -07002670
Adam Langleyfcf25832014-12-18 17:42:32 -08002671 return 1;
2672}
2673
2674int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint) {
2675 if (s == NULL) {
2676 return 0;
2677 }
2678
2679 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2680 OPENSSL_PUT_ERROR(SSL, SSL_use_psk_identity_hint,
2681 SSL_R_DATA_LENGTH_TOO_LONG);
2682 return 0;
2683 }
2684
2685 /* Clear currently configured hint, if any. */
2686 if (s->psk_identity_hint != NULL) {
2687 OPENSSL_free(s->psk_identity_hint);
2688 s->psk_identity_hint = NULL;
2689 }
2690
2691 if (identity_hint != NULL) {
2692 s->psk_identity_hint = BUF_strdup(identity_hint);
2693 if (s->psk_identity_hint == NULL) {
2694 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002695 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002696 }
Adam Langley95c29f32014-06-20 12:00:00 -07002697
Adam Langleyfcf25832014-12-18 17:42:32 -08002698 return 1;
2699}
Adam Langley95c29f32014-06-20 12:00:00 -07002700
Adam Langleyfcf25832014-12-18 17:42:32 -08002701const char *SSL_get_psk_identity_hint(const SSL *s) {
2702 if (s == NULL) {
2703 return NULL;
2704 }
2705 return s->psk_identity_hint;
2706}
Adam Langley95c29f32014-06-20 12:00:00 -07002707
Adam Langleyfcf25832014-12-18 17:42:32 -08002708const char *SSL_get_psk_identity(const SSL *s) {
2709 if (s == NULL || s->session == NULL) {
2710 return NULL;
2711 }
Adam Langley95c29f32014-06-20 12:00:00 -07002712
Adam Langleyfcf25832014-12-18 17:42:32 -08002713 return s->session->psk_identity;
2714}
Adam Langley95c29f32014-06-20 12:00:00 -07002715
Adam Langleyfcf25832014-12-18 17:42:32 -08002716void SSL_set_psk_client_callback(
2717 SSL *s, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2718 unsigned int max_identity_len, uint8_t *psk,
2719 unsigned int max_psk_len)) {
2720 s->psk_client_callback = cb;
2721}
Adam Langley95c29f32014-06-20 12:00:00 -07002722
Adam Langleyfcf25832014-12-18 17:42:32 -08002723void SSL_CTX_set_psk_client_callback(
2724 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2725 unsigned int max_identity_len,
2726 uint8_t *psk, unsigned int max_psk_len)) {
2727 ctx->psk_client_callback = cb;
2728}
Adam Langley95c29f32014-06-20 12:00:00 -07002729
Adam Langleyfcf25832014-12-18 17:42:32 -08002730void SSL_set_psk_server_callback(
2731 SSL *s, unsigned int (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
2732 unsigned int max_psk_len)) {
2733 s->psk_server_callback = cb;
2734}
Adam Langley95c29f32014-06-20 12:00:00 -07002735
Adam Langleyfcf25832014-12-18 17:42:32 -08002736void SSL_CTX_set_psk_server_callback(
2737 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *identity,
2738 uint8_t *psk, unsigned int max_psk_len)) {
2739 ctx->psk_server_callback = cb;
2740}
Adam Langley95c29f32014-06-20 12:00:00 -07002741
Adam Langleyfcf25832014-12-18 17:42:32 -08002742void SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) {
2743 ctx->min_version = version;
2744}
Adam Langley95c29f32014-06-20 12:00:00 -07002745
Adam Langleyfcf25832014-12-18 17:42:32 -08002746void SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) {
2747 ctx->max_version = version;
2748}
Adam Langley0289c732014-06-20 12:00:00 -07002749
Adam Langleyfcf25832014-12-18 17:42:32 -08002750void SSL_set_min_version(SSL *ssl, uint16_t version) {
2751 ssl->min_version = version;
2752}
Adam Langley0289c732014-06-20 12:00:00 -07002753
Adam Langleyfcf25832014-12-18 17:42:32 -08002754void SSL_set_max_version(SSL *ssl, uint16_t version) {
2755 ssl->max_version = version;
2756}
Adam Langley95c29f32014-06-20 12:00:00 -07002757
Adam Langleyfcf25832014-12-18 17:42:32 -08002758void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
2759 void (*cb)(int write_p, int version,
2760 int content_type, const void *buf,
2761 size_t len, SSL *ssl, void *arg)) {
2762 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2763}
2764void SSL_set_msg_callback(SSL *ssl,
2765 void (*cb)(int write_p, int version, int content_type,
2766 const void *buf, size_t len, SSL *ssl,
2767 void *arg)) {
2768 SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2769}
Adam Langley95c29f32014-06-20 12:00:00 -07002770
Adam Langleyfcf25832014-12-18 17:42:32 -08002771void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio) {
2772 if (ctx->keylog_bio != NULL) {
2773 BIO_free(ctx->keylog_bio);
2774 }
2775 ctx->keylog_bio = keylog_bio;
2776}
Adam Langley95c29f32014-06-20 12:00:00 -07002777
Adam Langleyfcf25832014-12-18 17:42:32 -08002778static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
2779 static const char hextable[] = "0123456789abcdef";
2780 uint8_t *out;
2781 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002782
Adam Langleyfcf25832014-12-18 17:42:32 -08002783 if (!CBB_add_space(cbb, &out, in_len * 2)) {
2784 return 0;
2785 }
Adam Langley95c29f32014-06-20 12:00:00 -07002786
Adam Langleyfcf25832014-12-18 17:42:32 -08002787 for (i = 0; i < in_len; i++) {
2788 *(out++) = (uint8_t)hextable[in[i] >> 4];
2789 *(out++) = (uint8_t)hextable[in[i] & 0xf];
2790 }
Adam Langley95c29f32014-06-20 12:00:00 -07002791
Adam Langleyfcf25832014-12-18 17:42:32 -08002792 return 1;
2793}
David Benjamin859ec3c2014-09-02 16:29:36 -04002794
2795int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -08002796 const uint8_t *encrypted_premaster,
2797 size_t encrypted_premaster_len,
2798 const uint8_t *premaster,
2799 size_t premaster_len) {
2800 BIO *bio = ctx->keylog_bio;
2801 CBB cbb;
2802 uint8_t *out;
2803 size_t out_len;
2804 int ret;
David Benjamin859ec3c2014-09-02 16:29:36 -04002805
Adam Langleyfcf25832014-12-18 17:42:32 -08002806 if (bio == NULL) {
2807 return 1;
2808 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002809
Adam Langleyfcf25832014-12-18 17:42:32 -08002810 if (encrypted_premaster_len < 8) {
2811 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_rsa_client_key_exchange,
2812 ERR_R_INTERNAL_ERROR);
2813 return 0;
2814 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002815
Adam Langleyfcf25832014-12-18 17:42:32 -08002816 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len * 2 + 1)) {
2817 return 0;
2818 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002819
Adam Langleyfcf25832014-12-18 17:42:32 -08002820 if (!CBB_add_bytes(&cbb, (const uint8_t *)"RSA ", 4) ||
2821 /* Only the first 8 bytes of the encrypted premaster secret are
2822 * logged. */
2823 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
2824 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2825 !cbb_add_hex(&cbb, premaster, premaster_len) ||
2826 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2827 !CBB_finish(&cbb, &out, &out_len)) {
2828 CBB_cleanup(&cbb);
2829 return 0;
2830 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002831
Adam Langleyfcf25832014-12-18 17:42:32 -08002832 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2833 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2834 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
David Benjamin859ec3c2014-09-02 16:29:36 -04002835
Adam Langleyfcf25832014-12-18 17:42:32 -08002836 OPENSSL_free(out);
2837 return ret;
Adam Langley95f22882014-06-20 12:00:00 -07002838}
2839
Adam Langleyfcf25832014-12-18 17:42:32 -08002840int ssl_ctx_log_master_secret(SSL_CTX *ctx, const uint8_t *client_random,
2841 size_t client_random_len, const uint8_t *master,
2842 size_t master_len) {
2843 BIO *bio = ctx->keylog_bio;
2844 CBB cbb;
2845 uint8_t *out;
2846 size_t out_len;
2847 int ret;
Adam Langleyadb739e2014-06-20 12:00:00 -07002848
Adam Langleyfcf25832014-12-18 17:42:32 -08002849 if (bio == NULL) {
2850 return 1;
2851 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002852
Adam Langleyfcf25832014-12-18 17:42:32 -08002853 if (client_random_len != 32) {
2854 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_master_secret, ERR_R_INTERNAL_ERROR);
2855 return 0;
2856 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002857
Adam Langleyfcf25832014-12-18 17:42:32 -08002858 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len * 2 + 1)) {
2859 return 0;
2860 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002861
Adam Langleyfcf25832014-12-18 17:42:32 -08002862 if (!CBB_add_bytes(&cbb, (const uint8_t *)"CLIENT_RANDOM ", 14) ||
2863 !cbb_add_hex(&cbb, client_random, 32) ||
2864 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2865 !cbb_add_hex(&cbb, master, master_len) ||
2866 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2867 !CBB_finish(&cbb, &out, &out_len)) {
2868 CBB_cleanup(&cbb);
2869 return 0;
2870 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002871
Adam Langleyfcf25832014-12-18 17:42:32 -08002872 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2873 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2874 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
David Benjamine99e9122014-12-11 01:46:01 -05002875
Adam Langleyfcf25832014-12-18 17:42:32 -08002876 OPENSSL_free(out);
2877 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -07002878}
2879
David Benjamined7c4752015-02-16 19:16:46 -05002880int SSL_in_false_start(const SSL *s) {
2881 return s->s3->tmp.in_false_start;
2882}
2883
Adam Langleyfcf25832014-12-18 17:42:32 -08002884int SSL_cutthrough_complete(const SSL *s) {
David Benjamined7c4752015-02-16 19:16:46 -05002885 return SSL_in_false_start(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002886}
Adam Langley95c29f32014-06-20 12:00:00 -07002887
Adam Langleyfcf25832014-12-18 17:42:32 -08002888void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
2889 size_t *ssl_session_size) {
2890 *ssl_size = sizeof(SSL);
2891 *ssl_ctx_size = sizeof(SSL_CTX);
2892 *ssl_session_size = sizeof(SSL_SESSION);
2893}
Feng Lu41aa3252014-11-21 22:47:56 -08002894
David Benjamined7c4752015-02-16 19:16:46 -05002895int ssl3_can_false_start(const SSL *s) {
David Benjamin195dc782015-02-19 13:27:05 -05002896 const SSL_CIPHER *const cipher = SSL_get_current_cipher(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002897
David Benjamin195dc782015-02-19 13:27:05 -05002898 /* False Start only for TLS 1.2 with a forward-secure, AEAD cipher and ALPN or
2899 * NPN. */
2900 return !SSL_IS_DTLS(s) &&
2901 SSL_version(s) >= TLS1_2_VERSION &&
2902 (s->s3->alpn_selected || s->s3->next_proto_neg_seen) &&
2903 cipher != NULL &&
2904 (cipher->algorithm_mkey == SSL_kEDH ||
2905 cipher->algorithm_mkey == SSL_kEECDH) &&
2906 (cipher->algorithm_enc == SSL_AES128GCM ||
2907 cipher->algorithm_enc == SSL_AES256GCM ||
2908 cipher->algorithm_enc == SSL_CHACHA20POLY1305);
Adam Langleyfcf25832014-12-18 17:42:32 -08002909}
2910
2911const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version) {
2912 switch (version) {
2913 case SSL3_VERSION:
2914 return &SSLv3_enc_data;
2915
2916 case TLS1_VERSION:
2917 return &TLSv1_enc_data;
2918
2919 case TLS1_1_VERSION:
2920 return &TLSv1_1_enc_data;
2921
2922 case TLS1_2_VERSION:
2923 return &TLSv1_2_enc_data;
2924
2925 case DTLS1_VERSION:
2926 return &DTLSv1_enc_data;
2927
2928 case DTLS1_2_VERSION:
2929 return &DTLSv1_2_enc_data;
2930
2931 default:
2932 return NULL;
2933 }
2934}
2935
2936uint16_t ssl3_get_max_server_version(const SSL *s) {
2937 uint16_t max_version;
2938
2939 if (SSL_IS_DTLS(s)) {
2940 max_version = (s->max_version != 0) ? s->max_version : DTLS1_2_VERSION;
2941 if (!(s->options & SSL_OP_NO_DTLSv1_2) && DTLS1_2_VERSION >= max_version) {
2942 return DTLS1_2_VERSION;
2943 }
2944 if (!(s->options & SSL_OP_NO_DTLSv1) && DTLS1_VERSION >= max_version) {
2945 return DTLS1_VERSION;
2946 }
2947 return 0;
2948 }
2949
2950 max_version = (s->max_version != 0) ? s->max_version : TLS1_2_VERSION;
2951 if (!(s->options & SSL_OP_NO_TLSv1_2) && TLS1_2_VERSION <= max_version) {
2952 return TLS1_2_VERSION;
2953 }
2954 if (!(s->options & SSL_OP_NO_TLSv1_1) && TLS1_1_VERSION <= max_version) {
2955 return TLS1_1_VERSION;
2956 }
2957 if (!(s->options & SSL_OP_NO_TLSv1) && TLS1_VERSION <= max_version) {
2958 return TLS1_VERSION;
2959 }
2960 if (!(s->options & SSL_OP_NO_SSLv3) && SSL3_VERSION <= max_version) {
2961 return SSL3_VERSION;
2962 }
2963 return 0;
2964}
2965
2966uint16_t ssl3_get_mutual_version(SSL *s, uint16_t client_version) {
2967 uint16_t version = 0;
2968
2969 if (SSL_IS_DTLS(s)) {
2970 /* Clamp client_version to max_version. */
2971 if (s->max_version != 0 && client_version < s->max_version) {
2972 client_version = s->max_version;
2973 }
2974
2975 if (client_version <= DTLS1_2_VERSION && !(s->options & SSL_OP_NO_DTLSv1_2)) {
2976 version = DTLS1_2_VERSION;
2977 } else if (client_version <= DTLS1_VERSION &&
2978 !(s->options & SSL_OP_NO_DTLSv1)) {
2979 version = DTLS1_VERSION;
2980 }
2981
2982 /* Check against min_version. */
2983 if (version != 0 && s->min_version != 0 && version > s->min_version) {
2984 return 0;
2985 }
2986 return version;
2987 } else {
2988 /* Clamp client_version to max_version. */
2989 if (s->max_version != 0 && client_version > s->max_version) {
2990 client_version = s->max_version;
2991 }
2992
2993 if (client_version >= TLS1_2_VERSION && !(s->options & SSL_OP_NO_TLSv1_2)) {
2994 version = TLS1_2_VERSION;
2995 } else if (client_version >= TLS1_1_VERSION &&
2996 !(s->options & SSL_OP_NO_TLSv1_1)) {
2997 version = TLS1_1_VERSION;
2998 } else if (client_version >= TLS1_VERSION && !(s->options & SSL_OP_NO_TLSv1)) {
2999 version = TLS1_VERSION;
3000 } else if (client_version >= SSL3_VERSION && !(s->options & SSL_OP_NO_SSLv3)) {
3001 version = SSL3_VERSION;
3002 }
3003
3004 /* Check against min_version. */
3005 if (version != 0 && s->min_version != 0 && version < s->min_version) {
3006 return 0;
3007 }
3008 return version;
3009 }
3010}
3011
3012uint16_t ssl3_get_max_client_version(SSL *s) {
3013 unsigned long options = s->options;
3014 uint16_t version = 0;
3015
3016 /* OpenSSL's API for controlling versions entails blacklisting individual
3017 * protocols. This has two problems. First, on the client, the protocol can
3018 * only express a contiguous range of versions. Second, a library consumer
3019 * trying to set a maximum version cannot disable protocol versions that get
3020 * added in a future version of the library.
3021 *
3022 * To account for both of these, OpenSSL interprets the client-side bitmask
3023 * as a min/max range by picking the lowest contiguous non-empty range of
3024 * enabled protocols. Note that this means it is impossible to set a maximum
3025 * version of TLS 1.2 in a future-proof way.
3026 *
3027 * By this scheme, the maximum version is the lowest version V such that V is
3028 * enabled and V+1 is disabled or unimplemented. */
3029 if (SSL_IS_DTLS(s)) {
3030 if (!(options & SSL_OP_NO_DTLSv1_2)) {
3031 version = DTLS1_2_VERSION;
3032 }
3033 if (!(options & SSL_OP_NO_DTLSv1) && (options & SSL_OP_NO_DTLSv1_2)) {
3034 version = DTLS1_VERSION;
3035 }
3036 if (s->max_version != 0 && version < s->max_version) {
3037 version = s->max_version;
3038 }
3039 } else {
3040 if (!(options & SSL_OP_NO_TLSv1_2)) {
3041 version = TLS1_2_VERSION;
3042 }
3043 if (!(options & SSL_OP_NO_TLSv1_1) && (options & SSL_OP_NO_TLSv1_2)) {
3044 version = TLS1_1_VERSION;
3045 }
3046 if (!(options & SSL_OP_NO_TLSv1) && (options & SSL_OP_NO_TLSv1_1)) {
3047 version = TLS1_VERSION;
3048 }
3049 if (!(options & SSL_OP_NO_SSLv3) && (options & SSL_OP_NO_TLSv1)) {
3050 version = SSL3_VERSION;
3051 }
3052 if (s->max_version != 0 && version > s->max_version) {
3053 version = s->max_version;
3054 }
3055 }
3056
3057 return version;
3058}
3059
3060int ssl3_is_version_enabled(SSL *s, uint16_t version) {
3061 if (SSL_IS_DTLS(s)) {
3062 if (s->max_version != 0 && version < s->max_version) {
3063 return 0;
3064 }
3065 if (s->min_version != 0 && version > s->min_version) {
3066 return 0;
3067 }
3068
3069 switch (version) {
3070 case DTLS1_VERSION:
3071 return !(s->options & SSL_OP_NO_DTLSv1);
3072
3073 case DTLS1_2_VERSION:
3074 return !(s->options & SSL_OP_NO_DTLSv1_2);
3075
3076 default:
3077 return 0;
3078 }
3079 } else {
3080 if (s->max_version != 0 && version > s->max_version) {
3081 return 0;
3082 }
3083 if (s->min_version != 0 && version < s->min_version) {
3084 return 0;
3085 }
3086
3087 switch (version) {
3088 case SSL3_VERSION:
3089 return !(s->options & SSL_OP_NO_SSLv3);
3090
3091 case TLS1_VERSION:
3092 return !(s->options & SSL_OP_NO_TLSv1);
3093
3094 case TLS1_1_VERSION:
3095 return !(s->options & SSL_OP_NO_TLSv1_1);
3096
3097 case TLS1_2_VERSION:
3098 return !(s->options & SSL_OP_NO_TLSv1_2);
3099
3100 default:
3101 return 0;
3102 }
3103 }
3104}
3105
David Benjaminea72bd02014-12-21 21:27:41 -05003106uint16_t ssl3_version_from_wire(SSL *s, uint16_t wire_version) {
3107 if (!SSL_IS_DTLS(s)) {
3108 return wire_version;
3109 }
3110
3111 uint16_t tls_version = ~wire_version;
3112 uint16_t version = tls_version + 0x0201;
3113 /* If either component overflowed, clamp it so comparisons still work. */
3114 if ((version >> 8) < (tls_version >> 8)) {
3115 version = 0xff00 | (version & 0xff);
3116 }
3117 if ((version & 0xff) < (tls_version & 0xff)) {
3118 version = (version & 0xff00) | 0xff;
3119 }
3120 /* DTLS 1.0 maps to TLS 1.1, not TLS 1.0. */
3121 if (version == TLS1_VERSION) {
3122 version = TLS1_1_VERSION;
3123 }
3124 return version;
3125}
3126
Adam Langleyfcf25832014-12-18 17:42:32 -08003127int SSL_cache_hit(SSL *s) { return s->hit; }
3128
3129int SSL_is_server(SSL *s) { return s->server; }
3130
3131void SSL_enable_fastradio_padding(SSL *s, char on_off) {
3132 s->fastradio_padding = on_off;
3133}