blob: 257ffbc60fbde332101d1ebcc443929e7adc356f [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
Adam Langley95c29f32014-06-20 12:00:00 -0700141#include <assert.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400142#include <stdio.h>
143#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700144
David Benjamin39482a12014-07-20 13:30:15 -0400145#include <openssl/bytestring.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700146#include <openssl/dh.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400147#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700148#include <openssl/lhash.h>
149#include <openssl/mem.h>
150#include <openssl/obj.h>
151#include <openssl/rand.h>
152#include <openssl/x509v3.h>
153
David Benjamin2ee94aa2015-04-07 22:38:30 -0400154#include "internal.h"
David Benjamin546f1a52015-04-15 16:46:09 -0400155#include "../crypto/internal.h"
156
Adam Langley95c29f32014-06-20 12:00:00 -0700157
Adam Langleyfcf25832014-12-18 17:42:32 -0800158/* Some error codes are special. Ensure the make_errors.go script never
159 * regresses this. */
160OPENSSL_COMPILE_ASSERT(SSL_R_TLSV1_ALERT_NO_RENEGOTIATION ==
161 SSL_AD_NO_RENEGOTIATION + SSL_AD_REASON_OFFSET,
162 ssl_alert_reason_code_mismatch);
David Benjamine1136082014-09-20 12:28:58 -0400163
Adam Langleyfcf25832014-12-18 17:42:32 -0800164int SSL_clear(SSL *s) {
165 if (s->method == NULL) {
166 OPENSSL_PUT_ERROR(SSL, SSL_clear, SSL_R_NO_METHOD_SPECIFIED);
167 return 0;
168 }
Adam Langley95c29f32014-06-20 12:00:00 -0700169
Adam Langleyfcf25832014-12-18 17:42:32 -0800170 if (ssl_clear_bad_session(s)) {
171 SSL_SESSION_free(s->session);
172 s->session = NULL;
173 }
Adam Langley95c29f32014-06-20 12:00:00 -0700174
Adam Langleyfcf25832014-12-18 17:42:32 -0800175 s->hit = 0;
176 s->shutdown = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700177
Adam Langleyfcf25832014-12-18 17:42:32 -0800178 if (s->renegotiate) {
179 OPENSSL_PUT_ERROR(SSL, SSL_clear, ERR_R_INTERNAL_ERROR);
180 return 0;
181 }
Adam Langley95c29f32014-06-20 12:00:00 -0700182
Adam Langleyfcf25832014-12-18 17:42:32 -0800183 /* SSL_clear may be called before or after the |s| is initialized in either
184 * accept or connect state. In the latter case, SSL_clear should preserve the
185 * half and reset |s->state| accordingly. */
186 if (s->handshake_func != NULL) {
187 if (s->server) {
188 SSL_set_accept_state(s);
189 } else {
190 SSL_set_connect_state(s);
191 }
192 } else {
193 assert(s->state == 0);
194 }
Adam Langley95c29f32014-06-20 12:00:00 -0700195
David Benjamin62fd1622015-01-11 13:30:01 -0500196 /* TODO(davidben): Some state on |s| is reset both in |SSL_new| and
197 * |SSL_clear| because it is per-connection state rather than configuration
198 * state. Per-connection state should be on |s->s3| and |s->d1| so it is
199 * naturally reset at the right points between |SSL_new|, |SSL_clear|, and
200 * |ssl3_new|. */
201
Adam Langleyfcf25832014-12-18 17:42:32 -0800202 s->rwstate = SSL_NOTHING;
203 s->rstate = SSL_ST_READ_HEADER;
Adam Langley95c29f32014-06-20 12:00:00 -0700204
Adam Langleyfcf25832014-12-18 17:42:32 -0800205 if (s->init_buf != NULL) {
206 BUF_MEM_free(s->init_buf);
207 s->init_buf = NULL;
208 }
Adam Langley95c29f32014-06-20 12:00:00 -0700209
David Benjamin62fd1622015-01-11 13:30:01 -0500210 s->packet = NULL;
211 s->packet_length = 0;
212
Adam Langleyfcf25832014-12-18 17:42:32 -0800213 ssl_clear_cipher_ctx(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700214
David Benjamin62fd1622015-01-11 13:30:01 -0500215 if (s->next_proto_negotiated) {
216 OPENSSL_free(s->next_proto_negotiated);
217 s->next_proto_negotiated = NULL;
218 s->next_proto_negotiated_len = 0;
219 }
220
221 /* The s->d1->mtu is simultaneously configuration (preserved across
222 * clear) and connection-specific state (gets reset).
223 *
224 * TODO(davidben): Avoid this. */
225 unsigned mtu = 0;
226 if (s->d1 != NULL) {
227 mtu = s->d1->mtu;
228 }
229
230 s->method->ssl_free(s);
231 if (!s->method->ssl_new(s)) {
232 return 0;
233 }
234 s->enc_method = ssl3_get_enc_method(s->version);
235 assert(s->enc_method != NULL);
236
237 if (SSL_IS_DTLS(s) && (SSL_get_options(s) & SSL_OP_NO_QUERY_MTU)) {
238 s->d1->mtu = mtu;
239 }
240
Adam Langleyfcf25832014-12-18 17:42:32 -0800241 s->client_version = s->version;
Adam Langley95c29f32014-06-20 12:00:00 -0700242
Adam Langleyfcf25832014-12-18 17:42:32 -0800243 return 1;
244}
Adam Langley95c29f32014-06-20 12:00:00 -0700245
Adam Langleyfcf25832014-12-18 17:42:32 -0800246SSL *SSL_new(SSL_CTX *ctx) {
247 SSL *s;
Adam Langley95c29f32014-06-20 12:00:00 -0700248
Adam Langleyfcf25832014-12-18 17:42:32 -0800249 if (ctx == NULL) {
250 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_NULL_SSL_CTX);
251 return NULL;
252 }
253 if (ctx->method == NULL) {
254 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
255 return NULL;
256 }
Adam Langley95c29f32014-06-20 12:00:00 -0700257
Adam Langleyfcf25832014-12-18 17:42:32 -0800258 s = (SSL *)OPENSSL_malloc(sizeof(SSL));
259 if (s == NULL) {
260 goto err;
261 }
262 memset(s, 0, sizeof(SSL));
Adam Langley95c29f32014-06-20 12:00:00 -0700263
Adam Langleyfcf25832014-12-18 17:42:32 -0800264 s->min_version = ctx->min_version;
265 s->max_version = ctx->max_version;
David Benjamin1eb367c2014-12-12 18:17:51 -0500266
Adam Langleyfcf25832014-12-18 17:42:32 -0800267 s->options = ctx->options;
268 s->mode = ctx->mode;
269 s->max_cert_list = ctx->max_cert_list;
Adam Langley95c29f32014-06-20 12:00:00 -0700270
David Benjamina5a3eeb2015-03-18 20:26:30 -0400271 s->cert = ssl_cert_dup(ctx->cert);
272 if (s->cert == NULL) {
273 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -0800274 }
Adam Langley95c29f32014-06-20 12:00:00 -0700275
Adam Langleyfcf25832014-12-18 17:42:32 -0800276 s->read_ahead = ctx->read_ahead;
277 s->msg_callback = ctx->msg_callback;
278 s->msg_callback_arg = ctx->msg_callback_arg;
279 s->verify_mode = ctx->verify_mode;
280 s->sid_ctx_length = ctx->sid_ctx_length;
281 assert(s->sid_ctx_length <= sizeof s->sid_ctx);
282 memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));
283 s->verify_callback = ctx->default_verify_callback;
284 s->generate_session_id = ctx->generate_session_id;
Adam Langley95c29f32014-06-20 12:00:00 -0700285
Adam Langleyfcf25832014-12-18 17:42:32 -0800286 s->param = X509_VERIFY_PARAM_new();
287 if (!s->param) {
288 goto err;
289 }
290 X509_VERIFY_PARAM_inherit(s->param, ctx->param);
291 s->quiet_shutdown = ctx->quiet_shutdown;
292 s->max_send_fragment = ctx->max_send_fragment;
Adam Langley95c29f32014-06-20 12:00:00 -0700293
Adam Langleyfcf25832014-12-18 17:42:32 -0800294 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
295 s->ctx = ctx;
296 s->tlsext_debug_cb = 0;
297 s->tlsext_debug_arg = NULL;
298 s->tlsext_ticket_expected = 0;
299 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
300 s->initial_ctx = ctx;
301 if (ctx->tlsext_ecpointformatlist) {
302 s->tlsext_ecpointformatlist = BUF_memdup(
303 ctx->tlsext_ecpointformatlist, ctx->tlsext_ecpointformatlist_length);
304 if (!s->tlsext_ecpointformatlist) {
305 goto err;
306 }
307 s->tlsext_ecpointformatlist_length = ctx->tlsext_ecpointformatlist_length;
308 }
Adam Langley95c29f32014-06-20 12:00:00 -0700309
Adam Langleyfcf25832014-12-18 17:42:32 -0800310 if (ctx->tlsext_ellipticcurvelist) {
311 s->tlsext_ellipticcurvelist =
312 BUF_memdup(ctx->tlsext_ellipticcurvelist,
313 ctx->tlsext_ellipticcurvelist_length * 2);
314 if (!s->tlsext_ellipticcurvelist) {
315 goto err;
316 }
317 s->tlsext_ellipticcurvelist_length = ctx->tlsext_ellipticcurvelist_length;
318 }
319 s->next_proto_negotiated = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700320
Adam Langleyfcf25832014-12-18 17:42:32 -0800321 if (s->ctx->alpn_client_proto_list) {
322 s->alpn_client_proto_list = BUF_memdup(s->ctx->alpn_client_proto_list,
323 s->ctx->alpn_client_proto_list_len);
324 if (s->alpn_client_proto_list == NULL) {
325 goto err;
326 }
327 s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;
328 }
Adam Langley95c29f32014-06-20 12:00:00 -0700329
Adam Langleyfcf25832014-12-18 17:42:32 -0800330 s->verify_result = X509_V_OK;
331 s->method = ctx->method;
Adam Langley95c29f32014-06-20 12:00:00 -0700332
Adam Langleyfcf25832014-12-18 17:42:32 -0800333 if (!s->method->ssl_new(s)) {
334 goto err;
335 }
336 s->enc_method = ssl3_get_enc_method(s->version);
337 assert(s->enc_method != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700338
David Benjamin62fd1622015-01-11 13:30:01 -0500339 s->rwstate = SSL_NOTHING;
340 s->rstate = SSL_ST_READ_HEADER;
Adam Langley95c29f32014-06-20 12:00:00 -0700341
Adam Langleyfcf25832014-12-18 17:42:32 -0800342 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700343
Adam Langleyfcf25832014-12-18 17:42:32 -0800344 s->psk_identity_hint = NULL;
345 if (ctx->psk_identity_hint) {
346 s->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
347 if (s->psk_identity_hint == NULL) {
348 goto err;
349 }
350 }
351 s->psk_client_callback = ctx->psk_client_callback;
352 s->psk_server_callback = ctx->psk_server_callback;
Adam Langley95c29f32014-06-20 12:00:00 -0700353
David Benjamin02ddbfd2015-01-11 13:09:11 -0500354 s->tlsext_channel_id_enabled = ctx->tlsext_channel_id_enabled;
355 if (ctx->tlsext_channel_id_private) {
356 s->tlsext_channel_id_private = EVP_PKEY_dup(ctx->tlsext_channel_id_private);
357 }
358
Adam Langleyfcf25832014-12-18 17:42:32 -0800359 s->signed_cert_timestamps_enabled = s->ctx->signed_cert_timestamps_enabled;
360 s->ocsp_stapling_enabled = s->ctx->ocsp_stapling_enabled;
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200361
Adam Langleyfcf25832014-12-18 17:42:32 -0800362 return s;
363
Adam Langley95c29f32014-06-20 12:00:00 -0700364err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800365 if (s != NULL) {
David Benjamin710d2272015-01-11 20:07:59 -0500366 SSL_free(s);
Adam Langleyfcf25832014-12-18 17:42:32 -0800367 }
368 OPENSSL_PUT_ERROR(SSL, SSL_new, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700369
Adam Langleyfcf25832014-12-18 17:42:32 -0800370 return NULL;
371}
Adam Langley95c29f32014-06-20 12:00:00 -0700372
Adam Langleyfcf25832014-12-18 17:42:32 -0800373int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
374 unsigned int sid_ctx_len) {
375 if (sid_ctx_len > sizeof ctx->sid_ctx) {
376 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_session_id_context,
377 SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
378 return 0;
379 }
380 ctx->sid_ctx_length = sid_ctx_len;
381 memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700382
Adam Langleyfcf25832014-12-18 17:42:32 -0800383 return 1;
384}
Adam Langley95c29f32014-06-20 12:00:00 -0700385
Adam Langleyfcf25832014-12-18 17:42:32 -0800386int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
387 unsigned int sid_ctx_len) {
388 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
389 OPENSSL_PUT_ERROR(SSL, SSL_set_session_id_context,
390 SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
391 return 0;
392 }
393 ssl->sid_ctx_length = sid_ctx_len;
394 memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700395
Adam Langleyfcf25832014-12-18 17:42:32 -0800396 return 1;
397}
Adam Langley95c29f32014-06-20 12:00:00 -0700398
Adam Langleyfcf25832014-12-18 17:42:32 -0800399int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb) {
400 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
401 ctx->generate_session_id = cb;
402 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
403 return 1;
404}
Adam Langley95c29f32014-06-20 12:00:00 -0700405
Adam Langleyfcf25832014-12-18 17:42:32 -0800406int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800407 ssl->generate_session_id = cb;
Adam Langleyfcf25832014-12-18 17:42:32 -0800408 return 1;
409}
Adam Langley95c29f32014-06-20 12:00:00 -0700410
Adam Langleyfcf25832014-12-18 17:42:32 -0800411int SSL_has_matching_session_id(const SSL *ssl, const uint8_t *id,
412 unsigned int id_len) {
413 /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how we
414 * can "construct" a session to give us the desired check - ie. to find if
415 * there's a session in the hash table that would conflict with any new
416 * session built out of this id/id_len and the ssl_version in use by this
417 * SSL. */
418 SSL_SESSION r, *p;
Adam Langley95c29f32014-06-20 12:00:00 -0700419
Adam Langleyfcf25832014-12-18 17:42:32 -0800420 if (id_len > sizeof r.session_id) {
421 return 0;
422 }
Adam Langley95c29f32014-06-20 12:00:00 -0700423
Adam Langleyfcf25832014-12-18 17:42:32 -0800424 r.ssl_version = ssl->version;
425 r.session_id_length = id_len;
426 memcpy(r.session_id, id, id_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700427
Adam Langleyfcf25832014-12-18 17:42:32 -0800428 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
429 p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r);
430 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
431 return p != NULL;
432}
Adam Langley95c29f32014-06-20 12:00:00 -0700433
Adam Langleyfcf25832014-12-18 17:42:32 -0800434int SSL_CTX_set_purpose(SSL_CTX *s, int purpose) {
435 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
436}
437
438int SSL_set_purpose(SSL *s, int purpose) {
439 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
440}
441
442int SSL_CTX_set_trust(SSL_CTX *s, int trust) {
443 return X509_VERIFY_PARAM_set_trust(s->param, trust);
444}
445
446int SSL_set_trust(SSL *s, int trust) {
447 return X509_VERIFY_PARAM_set_trust(s->param, trust);
448}
449
450int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm) {
451 return X509_VERIFY_PARAM_set1(ctx->param, vpm);
452}
453
454int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm) {
455 return X509_VERIFY_PARAM_set1(ssl->param, vpm);
456}
Adam Langley95c29f32014-06-20 12:00:00 -0700457
Adam Langley858a88d2014-06-20 12:00:00 -0700458void ssl_cipher_preference_list_free(
Adam Langleyfcf25832014-12-18 17:42:32 -0800459 struct ssl_cipher_preference_list_st *cipher_list) {
460 sk_SSL_CIPHER_free(cipher_list->ciphers);
461 OPENSSL_free(cipher_list->in_group_flags);
462 OPENSSL_free(cipher_list);
463}
Adam Langley858a88d2014-06-20 12:00:00 -0700464
Adam Langleyfcf25832014-12-18 17:42:32 -0800465struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_dup(
466 struct ssl_cipher_preference_list_st *cipher_list) {
467 struct ssl_cipher_preference_list_st *ret = NULL;
468 size_t n = sk_SSL_CIPHER_num(cipher_list->ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700469
Adam Langleyfcf25832014-12-18 17:42:32 -0800470 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
471 if (!ret) {
472 goto err;
473 }
474
475 ret->ciphers = NULL;
476 ret->in_group_flags = NULL;
477 ret->ciphers = sk_SSL_CIPHER_dup(cipher_list->ciphers);
478 if (!ret->ciphers) {
479 goto err;
480 }
481 ret->in_group_flags = BUF_memdup(cipher_list->in_group_flags, n);
482 if (!ret->in_group_flags) {
483 goto err;
484 }
485
486 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -0700487
488err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800489 if (ret && ret->ciphers) {
490 sk_SSL_CIPHER_free(ret->ciphers);
491 }
492 if (ret) {
493 OPENSSL_free(ret);
494 }
495 return NULL;
496}
Adam Langley858a88d2014-06-20 12:00:00 -0700497
Adam Langleyfcf25832014-12-18 17:42:32 -0800498struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_from_ciphers(
499 STACK_OF(SSL_CIPHER) * ciphers) {
500 struct ssl_cipher_preference_list_st *ret = NULL;
501 size_t n = sk_SSL_CIPHER_num(ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700502
Adam Langleyfcf25832014-12-18 17:42:32 -0800503 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
504 if (!ret) {
505 goto err;
506 }
507 ret->ciphers = NULL;
508 ret->in_group_flags = NULL;
509 ret->ciphers = sk_SSL_CIPHER_dup(ciphers);
510 if (!ret->ciphers) {
511 goto err;
512 }
513 ret->in_group_flags = OPENSSL_malloc(n);
514 if (!ret->in_group_flags) {
515 goto err;
516 }
517 memset(ret->in_group_flags, 0, n);
518 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -0700519
520err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800521 if (ret && ret->ciphers) {
522 sk_SSL_CIPHER_free(ret->ciphers);
523 }
524 if (ret) {
525 OPENSSL_free(ret);
526 }
527 return NULL;
528}
Adam Langley858a88d2014-06-20 12:00:00 -0700529
Adam Langleyfcf25832014-12-18 17:42:32 -0800530X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) { return ctx->param; }
Adam Langley95c29f32014-06-20 12:00:00 -0700531
Adam Langleyfcf25832014-12-18 17:42:32 -0800532X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) { return ssl->param; }
Adam Langley95c29f32014-06-20 12:00:00 -0700533
Adam Langleyfcf25832014-12-18 17:42:32 -0800534void SSL_certs_clear(SSL *s) { ssl_cert_clear_certs(s->cert); }
Adam Langley95c29f32014-06-20 12:00:00 -0700535
Adam Langleyfcf25832014-12-18 17:42:32 -0800536void SSL_free(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800537 if (s == NULL) {
538 return;
539 }
Adam Langley95c29f32014-06-20 12:00:00 -0700540
Adam Langleyfcf25832014-12-18 17:42:32 -0800541 if (s->param) {
542 X509_VERIFY_PARAM_free(s->param);
543 }
Adam Langley95c29f32014-06-20 12:00:00 -0700544
Adam Langleyfcf25832014-12-18 17:42:32 -0800545 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700546
Adam Langleyfcf25832014-12-18 17:42:32 -0800547 if (s->bbio != NULL) {
548 /* If the buffering BIO is in place, pop it off */
549 if (s->bbio == s->wbio) {
550 s->wbio = BIO_pop(s->wbio);
551 }
552 BIO_free(s->bbio);
553 s->bbio = NULL;
554 }
Adam Langley95c29f32014-06-20 12:00:00 -0700555
Adam Langleyfcf25832014-12-18 17:42:32 -0800556 if (s->rbio != NULL) {
557 BIO_free_all(s->rbio);
558 }
Adam Langley95c29f32014-06-20 12:00:00 -0700559
Adam Langleyfcf25832014-12-18 17:42:32 -0800560 if (s->wbio != NULL && s->wbio != s->rbio) {
561 BIO_free_all(s->wbio);
562 }
Adam Langley95c29f32014-06-20 12:00:00 -0700563
Adam Langleyfcf25832014-12-18 17:42:32 -0800564 if (s->init_buf != NULL) {
565 BUF_MEM_free(s->init_buf);
566 }
Adam Langley95c29f32014-06-20 12:00:00 -0700567
Adam Langleyfcf25832014-12-18 17:42:32 -0800568 /* add extra stuff */
569 if (s->cipher_list != NULL) {
570 ssl_cipher_preference_list_free(s->cipher_list);
571 }
572 if (s->cipher_list_by_id != NULL) {
573 sk_SSL_CIPHER_free(s->cipher_list_by_id);
574 }
Adam Langley95c29f32014-06-20 12:00:00 -0700575
Adam Langleyfcf25832014-12-18 17:42:32 -0800576 if (s->session != NULL) {
577 ssl_clear_bad_session(s);
578 SSL_SESSION_free(s->session);
579 }
Adam Langley95c29f32014-06-20 12:00:00 -0700580
Adam Langleyfcf25832014-12-18 17:42:32 -0800581 ssl_clear_cipher_ctx(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700582
Adam Langleyfcf25832014-12-18 17:42:32 -0800583 if (s->cert != NULL) {
584 ssl_cert_free(s->cert);
585 }
Adam Langley0289c732014-06-20 12:00:00 -0700586
Adam Langleyfcf25832014-12-18 17:42:32 -0800587 if (s->tlsext_hostname) {
588 OPENSSL_free(s->tlsext_hostname);
589 }
590 if (s->initial_ctx) {
591 SSL_CTX_free(s->initial_ctx);
592 }
593 if (s->tlsext_ecpointformatlist) {
594 OPENSSL_free(s->tlsext_ecpointformatlist);
595 }
596 if (s->tlsext_ellipticcurvelist) {
597 OPENSSL_free(s->tlsext_ellipticcurvelist);
598 }
599 if (s->alpn_client_proto_list) {
600 OPENSSL_free(s->alpn_client_proto_list);
601 }
602 if (s->tlsext_channel_id_private) {
603 EVP_PKEY_free(s->tlsext_channel_id_private);
604 }
605 if (s->psk_identity_hint) {
606 OPENSSL_free(s->psk_identity_hint);
607 }
608 if (s->client_CA != NULL) {
609 sk_X509_NAME_pop_free(s->client_CA, X509_NAME_free);
610 }
611 if (s->next_proto_negotiated) {
612 OPENSSL_free(s->next_proto_negotiated);
613 }
614 if (s->srtp_profiles) {
615 sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);
616 }
Adam Langley95c29f32014-06-20 12:00:00 -0700617
Adam Langleyfcf25832014-12-18 17:42:32 -0800618 if (s->method != NULL) {
619 s->method->ssl_free(s);
620 }
621 if (s->ctx) {
622 SSL_CTX_free(s->ctx);
623 }
Adam Langley95c29f32014-06-20 12:00:00 -0700624
Adam Langleyfcf25832014-12-18 17:42:32 -0800625 OPENSSL_free(s);
626}
Adam Langley95c29f32014-06-20 12:00:00 -0700627
Adam Langleyfcf25832014-12-18 17:42:32 -0800628void SSL_set_bio(SSL *s, BIO *rbio, BIO *wbio) {
629 /* If the output buffering BIO is still in place, remove it. */
630 if (s->bbio != NULL) {
631 if (s->wbio == s->bbio) {
632 s->wbio = s->wbio->next_bio;
633 s->bbio->next_bio = NULL;
634 }
635 }
Adam Langley95c29f32014-06-20 12:00:00 -0700636
Adam Langleyfcf25832014-12-18 17:42:32 -0800637 if (s->rbio != NULL && s->rbio != rbio) {
638 BIO_free_all(s->rbio);
639 }
640 if (s->wbio != NULL && s->wbio != wbio && s->rbio != s->wbio) {
641 BIO_free_all(s->wbio);
642 }
643 s->rbio = rbio;
644 s->wbio = wbio;
645}
Adam Langley95c29f32014-06-20 12:00:00 -0700646
Adam Langleyfcf25832014-12-18 17:42:32 -0800647BIO *SSL_get_rbio(const SSL *s) { return s->rbio; }
Adam Langley95c29f32014-06-20 12:00:00 -0700648
Adam Langleyfcf25832014-12-18 17:42:32 -0800649BIO *SSL_get_wbio(const SSL *s) { return s->wbio; }
Adam Langley95c29f32014-06-20 12:00:00 -0700650
Adam Langleyfcf25832014-12-18 17:42:32 -0800651int SSL_get_fd(const SSL *s) { return SSL_get_rfd(s); }
Adam Langley95c29f32014-06-20 12:00:00 -0700652
Adam Langleyfcf25832014-12-18 17:42:32 -0800653int SSL_get_rfd(const SSL *s) {
654 int ret = -1;
655 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700656
Adam Langleyfcf25832014-12-18 17:42:32 -0800657 b = SSL_get_rbio(s);
658 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
659 if (r != NULL) {
660 BIO_get_fd(r, &ret);
661 }
662 return ret;
663}
Adam Langley95c29f32014-06-20 12:00:00 -0700664
Adam Langleyfcf25832014-12-18 17:42:32 -0800665int SSL_get_wfd(const SSL *s) {
666 int ret = -1;
667 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700668
Adam Langleyfcf25832014-12-18 17:42:32 -0800669 b = SSL_get_wbio(s);
670 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
671 if (r != NULL) {
672 BIO_get_fd(r, &ret);
673 }
Adam Langley95c29f32014-06-20 12:00:00 -0700674
Adam Langleyfcf25832014-12-18 17:42:32 -0800675 return ret;
676}
Adam Langley95c29f32014-06-20 12:00:00 -0700677
Adam Langleyfcf25832014-12-18 17:42:32 -0800678int SSL_set_fd(SSL *s, int fd) {
679 int ret = 0;
680 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700681
Adam Langleyfcf25832014-12-18 17:42:32 -0800682 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700683
Adam Langleyfcf25832014-12-18 17:42:32 -0800684 if (bio == NULL) {
685 OPENSSL_PUT_ERROR(SSL, SSL_set_fd, ERR_R_BUF_LIB);
686 goto err;
687 }
688 BIO_set_fd(bio, fd, BIO_NOCLOSE);
689 SSL_set_bio(s, bio, bio);
690 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700691
Adam Langley95c29f32014-06-20 12:00:00 -0700692err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800693 return ret;
694}
Adam Langley95c29f32014-06-20 12:00:00 -0700695
Adam Langleyfcf25832014-12-18 17:42:32 -0800696int SSL_set_wfd(SSL *s, int fd) {
697 int ret = 0;
698 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700699
Adam Langleyfcf25832014-12-18 17:42:32 -0800700 if (s->rbio == NULL || BIO_method_type(s->rbio) != BIO_TYPE_FD ||
701 (int)BIO_get_fd(s->rbio, NULL) != fd) {
702 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700703
Adam Langleyfcf25832014-12-18 17:42:32 -0800704 if (bio == NULL) {
705 OPENSSL_PUT_ERROR(SSL, SSL_set_wfd, ERR_R_BUF_LIB);
706 goto err;
707 }
708 BIO_set_fd(bio, fd, BIO_NOCLOSE);
709 SSL_set_bio(s, SSL_get_rbio(s), bio);
710 } else {
711 SSL_set_bio(s, SSL_get_rbio(s), SSL_get_rbio(s));
712 }
713
714 ret = 1;
715
Adam Langley95c29f32014-06-20 12:00:00 -0700716err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800717 return ret;
718}
Adam Langley95c29f32014-06-20 12:00:00 -0700719
Adam Langleyfcf25832014-12-18 17:42:32 -0800720int SSL_set_rfd(SSL *s, int fd) {
721 int ret = 0;
722 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700723
Adam Langleyfcf25832014-12-18 17:42:32 -0800724 if (s->wbio == NULL || BIO_method_type(s->wbio) != BIO_TYPE_FD ||
725 (int)BIO_get_fd(s->wbio, NULL) != fd) {
726 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700727
Adam Langleyfcf25832014-12-18 17:42:32 -0800728 if (bio == NULL) {
729 OPENSSL_PUT_ERROR(SSL, SSL_set_rfd, ERR_R_BUF_LIB);
730 goto err;
731 }
732 BIO_set_fd(bio, fd, BIO_NOCLOSE);
733 SSL_set_bio(s, bio, SSL_get_wbio(s));
734 } else {
735 SSL_set_bio(s, SSL_get_wbio(s), SSL_get_wbio(s));
736 }
737 ret = 1;
738
Adam Langley95c29f32014-06-20 12:00:00 -0700739err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800740 return ret;
741}
Adam Langley95c29f32014-06-20 12:00:00 -0700742
743/* return length of latest Finished message we sent, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -0800744size_t SSL_get_finished(const SSL *s, void *buf, size_t count) {
745 size_t ret = 0;
746
747 if (s->s3 != NULL) {
748 ret = s->s3->tmp.finish_md_len;
749 if (count > ret) {
750 count = ret;
751 }
752 memcpy(buf, s->s3->tmp.finish_md, count);
753 }
754
755 return ret;
756}
Adam Langley95c29f32014-06-20 12:00:00 -0700757
758/* return length of latest Finished message we expected, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -0800759size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count) {
760 size_t ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700761
Adam Langleyfcf25832014-12-18 17:42:32 -0800762 if (s->s3 != NULL) {
763 ret = s->s3->tmp.peer_finish_md_len;
764 if (count > ret) {
765 count = ret;
766 }
767 memcpy(buf, s->s3->tmp.peer_finish_md, count);
768 }
Adam Langley95c29f32014-06-20 12:00:00 -0700769
Adam Langleyfcf25832014-12-18 17:42:32 -0800770 return ret;
771}
Adam Langley95c29f32014-06-20 12:00:00 -0700772
Adam Langleyfcf25832014-12-18 17:42:32 -0800773int SSL_get_verify_mode(const SSL *s) { return s->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -0700774
Adam Langleyfcf25832014-12-18 17:42:32 -0800775int SSL_get_verify_depth(const SSL *s) {
776 return X509_VERIFY_PARAM_get_depth(s->param);
777}
Adam Langley95c29f32014-06-20 12:00:00 -0700778
Adam Langleyfcf25832014-12-18 17:42:32 -0800779int (*SSL_get_verify_callback(const SSL *s))(int, X509_STORE_CTX *) {
780 return s->verify_callback;
781}
Adam Langley95c29f32014-06-20 12:00:00 -0700782
Adam Langleyfcf25832014-12-18 17:42:32 -0800783int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) { return ctx->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -0700784
Adam Langleyfcf25832014-12-18 17:42:32 -0800785int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) {
786 return X509_VERIFY_PARAM_get_depth(ctx->param);
787}
Adam Langley95c29f32014-06-20 12:00:00 -0700788
Adam Langleyfcf25832014-12-18 17:42:32 -0800789int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int, X509_STORE_CTX *) {
790 return ctx->default_verify_callback;
791}
Adam Langley95c29f32014-06-20 12:00:00 -0700792
Adam Langleyfcf25832014-12-18 17:42:32 -0800793void SSL_set_verify(SSL *s, int mode,
794 int (*callback)(int ok, X509_STORE_CTX *ctx)) {
795 s->verify_mode = mode;
796 if (callback != NULL) {
797 s->verify_callback = callback;
798 }
799}
Adam Langley95c29f32014-06-20 12:00:00 -0700800
Adam Langleyfcf25832014-12-18 17:42:32 -0800801void SSL_set_verify_depth(SSL *s, int depth) {
802 X509_VERIFY_PARAM_set_depth(s->param, depth);
803}
Adam Langley95c29f32014-06-20 12:00:00 -0700804
Adam Langleyfcf25832014-12-18 17:42:32 -0800805void SSL_set_read_ahead(SSL *s, int yes) { s->read_ahead = yes; }
Adam Langley95c29f32014-06-20 12:00:00 -0700806
Adam Langleyfcf25832014-12-18 17:42:32 -0800807int SSL_get_read_ahead(const SSL *s) { return s->read_ahead; }
Adam Langley95c29f32014-06-20 12:00:00 -0700808
Adam Langleyfcf25832014-12-18 17:42:32 -0800809int SSL_pending(const SSL *s) {
810 /* SSL_pending cannot work properly if read-ahead is enabled
811 * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)), and it is
812 * impossible to fix since SSL_pending cannot report errors that may be
813 * observed while scanning the new data. (Note that SSL_pending() is often
814 * used as a boolean value, so we'd better not return -1.). */
815 return s->method->ssl_pending(s);
816}
Adam Langley95c29f32014-06-20 12:00:00 -0700817
Adam Langleyfcf25832014-12-18 17:42:32 -0800818X509 *SSL_get_peer_certificate(const SSL *s) {
819 X509 *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700820
Adam Langleyfcf25832014-12-18 17:42:32 -0800821 if (s == NULL || s->session == NULL) {
822 r = NULL;
823 } else {
824 r = s->session->peer;
825 }
Adam Langley95c29f32014-06-20 12:00:00 -0700826
Adam Langleyfcf25832014-12-18 17:42:32 -0800827 if (r == NULL) {
828 return NULL;
829 }
Adam Langley95c29f32014-06-20 12:00:00 -0700830
Adam Langleyfcf25832014-12-18 17:42:32 -0800831 return X509_up_ref(r);
832}
833
834STACK_OF(X509) * SSL_get_peer_cert_chain(const SSL *s) {
835 STACK_OF(X509) * r;
836
837 if (s == NULL || s->session == NULL || s->session->sess_cert == NULL) {
838 r = NULL;
839 } else {
840 r = s->session->sess_cert->cert_chain;
841 }
842
843 /* If we are a client, cert_chain includes the peer's own certificate; if we
844 * are a server, it does not. */
845 return r;
846}
Adam Langley95c29f32014-06-20 12:00:00 -0700847
Adam Langley95c29f32014-06-20 12:00:00 -0700848/* Fix this so it checks all the valid key/cert options */
Adam Langleyfcf25832014-12-18 17:42:32 -0800849int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
850 if (ctx == NULL || ctx->cert == NULL || ctx->cert->key->x509 == NULL) {
851 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key,
852 SSL_R_NO_CERTIFICATE_ASSIGNED);
853 return 0;
854 }
855
856 if (ctx->cert->key->privatekey == NULL) {
857 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key,
858 SSL_R_NO_PRIVATE_KEY_ASSIGNED);
859 return 0;
860 }
861
862 return X509_check_private_key(ctx->cert->key->x509,
863 ctx->cert->key->privatekey);
864}
Adam Langley95c29f32014-06-20 12:00:00 -0700865
866/* Fix this function so that it takes an optional type parameter */
Adam Langleyfcf25832014-12-18 17:42:32 -0800867int SSL_check_private_key(const SSL *ssl) {
868 if (ssl == NULL) {
869 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, ERR_R_PASSED_NULL_PARAMETER);
870 return 0;
871 }
Adam Langley95c29f32014-06-20 12:00:00 -0700872
Adam Langleyfcf25832014-12-18 17:42:32 -0800873 if (ssl->cert == NULL) {
874 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
875 SSL_R_NO_CERTIFICATE_ASSIGNED);
876 return 0;
877 }
Adam Langley95c29f32014-06-20 12:00:00 -0700878
Adam Langleyfcf25832014-12-18 17:42:32 -0800879 if (ssl->cert->key->x509 == NULL) {
880 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
881 SSL_R_NO_CERTIFICATE_ASSIGNED);
882 return 0;
883 }
David Benjamin0b145c22014-11-26 20:10:09 -0500884
Adam Langleyfcf25832014-12-18 17:42:32 -0800885 if (ssl->cert->key->privatekey == NULL) {
886 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
887 SSL_R_NO_PRIVATE_KEY_ASSIGNED);
888 return 0;
889 }
Adam Langley95c29f32014-06-20 12:00:00 -0700890
Adam Langleyfcf25832014-12-18 17:42:32 -0800891 return X509_check_private_key(ssl->cert->key->x509,
892 ssl->cert->key->privatekey);
893}
Adam Langley95c29f32014-06-20 12:00:00 -0700894
Adam Langleyfcf25832014-12-18 17:42:32 -0800895int SSL_accept(SSL *s) {
896 if (s->handshake_func == 0) {
897 /* Not properly initialized yet */
898 SSL_set_accept_state(s);
899 }
David Benjamin0b145c22014-11-26 20:10:09 -0500900
Adam Langleyfcf25832014-12-18 17:42:32 -0800901 if (s->handshake_func != s->method->ssl_accept) {
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +0100902 OPENSSL_PUT_ERROR(SSL, SSL_accept, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800903 return -1;
904 }
Adam Langley95c29f32014-06-20 12:00:00 -0700905
Adam Langleyfcf25832014-12-18 17:42:32 -0800906 return s->handshake_func(s);
907}
Adam Langley95c29f32014-06-20 12:00:00 -0700908
Adam Langleyfcf25832014-12-18 17:42:32 -0800909int SSL_connect(SSL *s) {
910 if (s->handshake_func == 0) {
911 /* Not properly initialized yet */
912 SSL_set_connect_state(s);
913 }
Adam Langley95c29f32014-06-20 12:00:00 -0700914
Adam Langleyfcf25832014-12-18 17:42:32 -0800915 if (s->handshake_func != s->method->ssl_connect) {
916 OPENSSL_PUT_ERROR(SSL, SSL_connect, ERR_R_INTERNAL_ERROR);
917 return -1;
918 }
Adam Langley95c29f32014-06-20 12:00:00 -0700919
Adam Langleyfcf25832014-12-18 17:42:32 -0800920 return s->handshake_func(s);
921}
Adam Langley95c29f32014-06-20 12:00:00 -0700922
Adam Langleyfcf25832014-12-18 17:42:32 -0800923long SSL_get_default_timeout(const SSL *s) {
924 return SSL_DEFAULT_SESSION_TIMEOUT;
925}
Adam Langley95c29f32014-06-20 12:00:00 -0700926
Adam Langleyfcf25832014-12-18 17:42:32 -0800927int SSL_read(SSL *s, void *buf, int num) {
928 if (s->handshake_func == 0) {
929 OPENSSL_PUT_ERROR(SSL, SSL_read, SSL_R_UNINITIALIZED);
930 return -1;
931 }
Adam Langley95c29f32014-06-20 12:00:00 -0700932
Adam Langleyfcf25832014-12-18 17:42:32 -0800933 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
934 s->rwstate = SSL_NOTHING;
935 return 0;
936 }
Adam Langley95c29f32014-06-20 12:00:00 -0700937
Adam Langleyfcf25832014-12-18 17:42:32 -0800938 return s->method->ssl_read(s, buf, num);
939}
Adam Langley95c29f32014-06-20 12:00:00 -0700940
Adam Langleyfcf25832014-12-18 17:42:32 -0800941int SSL_peek(SSL *s, void *buf, int num) {
942 if (s->handshake_func == 0) {
943 OPENSSL_PUT_ERROR(SSL, SSL_peek, SSL_R_UNINITIALIZED);
944 return -1;
945 }
Adam Langley95c29f32014-06-20 12:00:00 -0700946
Adam Langleyfcf25832014-12-18 17:42:32 -0800947 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
948 return 0;
949 }
Adam Langley95c29f32014-06-20 12:00:00 -0700950
Adam Langleyfcf25832014-12-18 17:42:32 -0800951 return s->method->ssl_peek(s, buf, num);
952}
Adam Langley95c29f32014-06-20 12:00:00 -0700953
Adam Langleyfcf25832014-12-18 17:42:32 -0800954int SSL_write(SSL *s, const void *buf, int num) {
955 if (s->handshake_func == 0) {
956 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_UNINITIALIZED);
957 return -1;
958 }
Adam Langley95c29f32014-06-20 12:00:00 -0700959
Adam Langleyfcf25832014-12-18 17:42:32 -0800960 if (s->shutdown & SSL_SENT_SHUTDOWN) {
961 s->rwstate = SSL_NOTHING;
962 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_PROTOCOL_IS_SHUTDOWN);
963 return -1;
964 }
Adam Langley95c29f32014-06-20 12:00:00 -0700965
Adam Langleyfcf25832014-12-18 17:42:32 -0800966 return s->method->ssl_write(s, buf, num);
967}
Adam Langley95c29f32014-06-20 12:00:00 -0700968
Adam Langleyfcf25832014-12-18 17:42:32 -0800969int SSL_shutdown(SSL *s) {
970 /* Note that this function behaves differently from what one might expect.
971 * Return values are 0 for no success (yet), 1 for success; but calling it
972 * once is usually not enough, even if blocking I/O is used (see
973 * ssl3_shutdown). */
Adam Langley95c29f32014-06-20 12:00:00 -0700974
Adam Langleyfcf25832014-12-18 17:42:32 -0800975 if (s->handshake_func == 0) {
976 OPENSSL_PUT_ERROR(SSL, SSL_shutdown, SSL_R_UNINITIALIZED);
977 return -1;
978 }
Adam Langley95c29f32014-06-20 12:00:00 -0700979
Adam Langleyfcf25832014-12-18 17:42:32 -0800980 if (!SSL_in_init(s)) {
981 return s->method->ssl_shutdown(s);
982 }
Adam Langley95c29f32014-06-20 12:00:00 -0700983
Adam Langleyfcf25832014-12-18 17:42:32 -0800984 return 1;
985}
Adam Langley95c29f32014-06-20 12:00:00 -0700986
Adam Langleyfcf25832014-12-18 17:42:32 -0800987int SSL_renegotiate(SSL *s) {
David Benjamin2cdace92015-01-24 13:30:02 -0500988 if (SSL_IS_DTLS(s)) {
989 /* Renegotiation is not supported for DTLS. */
990 OPENSSL_PUT_ERROR(SSL, SSL_renegotiate, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
991 return 0;
992 }
993
Adam Langleyfcf25832014-12-18 17:42:32 -0800994 if (s->renegotiate == 0) {
995 s->renegotiate = 1;
996 }
Adam Langley95c29f32014-06-20 12:00:00 -0700997
Adam Langleyfcf25832014-12-18 17:42:32 -0800998 s->new_session = 1;
999 return s->method->ssl_renegotiate(s);
1000}
Adam Langley95c29f32014-06-20 12:00:00 -07001001
Adam Langleyfcf25832014-12-18 17:42:32 -08001002int SSL_renegotiate_pending(SSL *s) {
1003 /* becomes true when negotiation is requested; false again once a handshake
1004 * has finished */
1005 return s->renegotiate != 0;
1006}
Adam Langley95c29f32014-06-20 12:00:00 -07001007
Adam Langleyfcf25832014-12-18 17:42:32 -08001008long SSL_ctrl(SSL *s, int cmd, long larg, void *parg) {
1009 long l;
Adam Langley95c29f32014-06-20 12:00:00 -07001010
Adam Langleyfcf25832014-12-18 17:42:32 -08001011 switch (cmd) {
1012 case SSL_CTRL_GET_READ_AHEAD:
1013 return s->read_ahead;
Adam Langley95c29f32014-06-20 12:00:00 -07001014
Adam Langleyfcf25832014-12-18 17:42:32 -08001015 case SSL_CTRL_SET_READ_AHEAD:
1016 l = s->read_ahead;
1017 s->read_ahead = larg;
1018 return l;
Adam Langley95c29f32014-06-20 12:00:00 -07001019
Adam Langleyfcf25832014-12-18 17:42:32 -08001020 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1021 s->msg_callback_arg = parg;
1022 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001023
Adam Langleyfcf25832014-12-18 17:42:32 -08001024 case SSL_CTRL_OPTIONS:
1025 return s->options |= larg;
Adam Langley95c29f32014-06-20 12:00:00 -07001026
Adam Langleyfcf25832014-12-18 17:42:32 -08001027 case SSL_CTRL_CLEAR_OPTIONS:
1028 return s->options &= ~larg;
Adam Langley95c29f32014-06-20 12:00:00 -07001029
Adam Langleyfcf25832014-12-18 17:42:32 -08001030 case SSL_CTRL_MODE:
1031 return s->mode |= larg;
Adam Langley95c29f32014-06-20 12:00:00 -07001032
Adam Langleyfcf25832014-12-18 17:42:32 -08001033 case SSL_CTRL_CLEAR_MODE:
1034 return s->mode &= ~larg;
Adam Langley95c29f32014-06-20 12:00:00 -07001035
Adam Langleyfcf25832014-12-18 17:42:32 -08001036 case SSL_CTRL_GET_MAX_CERT_LIST:
1037 return s->max_cert_list;
Adam Langley95c29f32014-06-20 12:00:00 -07001038
Adam Langleyfcf25832014-12-18 17:42:32 -08001039 case SSL_CTRL_SET_MAX_CERT_LIST:
1040 l = s->max_cert_list;
1041 s->max_cert_list = larg;
1042 return l;
Adam Langley95c29f32014-06-20 12:00:00 -07001043
Adam Langleyfcf25832014-12-18 17:42:32 -08001044 case SSL_CTRL_SET_MTU:
1045 if (larg < (long)dtls1_min_mtu()) {
1046 return 0;
1047 }
1048 if (SSL_IS_DTLS(s)) {
1049 s->d1->mtu = larg;
1050 return larg;
1051 }
1052 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001053
Adam Langleyfcf25832014-12-18 17:42:32 -08001054 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1055 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH) {
1056 return 0;
1057 }
1058 s->max_send_fragment = larg;
1059 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001060
Adam Langleyfcf25832014-12-18 17:42:32 -08001061 case SSL_CTRL_GET_RI_SUPPORT:
1062 if (s->s3) {
1063 return s->s3->send_connection_binding;
1064 }
1065 return 0;
1066
1067 case SSL_CTRL_CERT_FLAGS:
1068 return s->cert->cert_flags |= larg;
1069
1070 case SSL_CTRL_CLEAR_CERT_FLAGS:
1071 return s->cert->cert_flags &= ~larg;
1072
1073 case SSL_CTRL_GET_RAW_CIPHERLIST:
1074 if (parg) {
1075 if (s->cert->ciphers_raw == NULL) {
1076 return 0;
1077 }
1078 *(uint8_t **)parg = s->cert->ciphers_raw;
1079 return (int)s->cert->ciphers_rawlen;
1080 }
1081
1082 /* Passing a NULL |parg| returns the size of a single
1083 * cipher suite value. */
1084 return 2;
1085
1086 default:
1087 return s->method->ssl_ctrl(s, cmd, larg, parg);
1088 }
1089}
1090
1091long SSL_callback_ctrl(SSL *s, int cmd, void (*fp)(void)) {
1092 switch (cmd) {
1093 case SSL_CTRL_SET_MSG_CALLBACK:
1094 s->msg_callback =
1095 (void (*)(int write_p, int version, int content_type, const void *buf,
1096 size_t len, SSL *ssl, void *arg))(fp);
1097 return 1;
1098
1099 default:
1100 return s->method->ssl_callback_ctrl(s, cmd, fp);
1101 }
1102}
1103
1104LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) { return ctx->sessions; }
1105
1106long SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg) {
1107 long l;
1108
1109 switch (cmd) {
1110 case SSL_CTRL_GET_READ_AHEAD:
1111 return ctx->read_ahead;
1112
1113 case SSL_CTRL_SET_READ_AHEAD:
1114 l = ctx->read_ahead;
1115 ctx->read_ahead = larg;
1116 return l;
1117
1118 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1119 ctx->msg_callback_arg = parg;
1120 return 1;
1121
1122 case SSL_CTRL_GET_MAX_CERT_LIST:
1123 return ctx->max_cert_list;
1124
1125 case SSL_CTRL_SET_MAX_CERT_LIST:
1126 l = ctx->max_cert_list;
1127 ctx->max_cert_list = larg;
1128 return l;
1129
1130 case SSL_CTRL_SET_SESS_CACHE_SIZE:
1131 l = ctx->session_cache_size;
1132 ctx->session_cache_size = larg;
1133 return l;
1134
1135 case SSL_CTRL_GET_SESS_CACHE_SIZE:
1136 return ctx->session_cache_size;
1137
1138 case SSL_CTRL_SET_SESS_CACHE_MODE:
1139 l = ctx->session_cache_mode;
1140 ctx->session_cache_mode = larg;
1141 return l;
1142
1143 case SSL_CTRL_GET_SESS_CACHE_MODE:
1144 return ctx->session_cache_mode;
1145
1146 case SSL_CTRL_SESS_NUMBER:
1147 return lh_SSL_SESSION_num_items(ctx->sessions);
1148
1149 case SSL_CTRL_SESS_CONNECT:
Adam Langleyfcf25832014-12-18 17:42:32 -08001150 case SSL_CTRL_SESS_CONNECT_GOOD:
Adam Langleyfcf25832014-12-18 17:42:32 -08001151 case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001152 case SSL_CTRL_SESS_ACCEPT:
Adam Langleyfcf25832014-12-18 17:42:32 -08001153 case SSL_CTRL_SESS_ACCEPT_GOOD:
Adam Langleyfcf25832014-12-18 17:42:32 -08001154 case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001155 case SSL_CTRL_SESS_HIT:
Adam Langleyfcf25832014-12-18 17:42:32 -08001156 case SSL_CTRL_SESS_CB_HIT:
Adam Langleyfcf25832014-12-18 17:42:32 -08001157 case SSL_CTRL_SESS_MISSES:
Adam Langleyfcf25832014-12-18 17:42:32 -08001158 case SSL_CTRL_SESS_TIMEOUTS:
Adam Langleyfcf25832014-12-18 17:42:32 -08001159 case SSL_CTRL_SESS_CACHE_FULL:
David Benjaminb6d0c6d2015-03-19 19:07:26 -04001160 /* Statistics are no longer supported.
1161 * TODO(davidben): Try to remove the accessors altogether. */
1162 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001163
1164 case SSL_CTRL_OPTIONS:
1165 return ctx->options |= larg;
1166
1167 case SSL_CTRL_CLEAR_OPTIONS:
1168 return ctx->options &= ~larg;
1169
1170 case SSL_CTRL_MODE:
1171 return ctx->mode |= larg;
1172
1173 case SSL_CTRL_CLEAR_MODE:
1174 return ctx->mode &= ~larg;
1175
1176 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1177 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH) {
1178 return 0;
1179 }
1180 ctx->max_send_fragment = larg;
1181 return 1;
1182
1183 case SSL_CTRL_CERT_FLAGS:
1184 return ctx->cert->cert_flags |= larg;
1185
1186 case SSL_CTRL_CLEAR_CERT_FLAGS:
1187 return ctx->cert->cert_flags &= ~larg;
1188
1189 default:
1190 return ctx->method->ssl_ctx_ctrl(ctx, cmd, larg, parg);
1191 }
1192}
1193
1194long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void)) {
1195 switch (cmd) {
1196 case SSL_CTRL_SET_MSG_CALLBACK:
1197 ctx->msg_callback =
1198 (void (*)(int write_p, int version, int content_type, const void *buf,
1199 size_t len, SSL *ssl, void *arg))(fp);
1200 return 1;
1201
1202 default:
1203 return ctx->method->ssl_ctx_callback_ctrl(ctx, cmd, fp);
1204 }
1205}
1206
1207int ssl_cipher_id_cmp(const void *in_a, const void *in_b) {
1208 long l;
1209 const SSL_CIPHER *a = in_a;
1210 const SSL_CIPHER *b = in_b;
1211 const long a_id = a->id;
1212 const long b_id = b->id;
1213
1214 l = a_id - b_id;
1215 if (l == 0L) {
1216 return 0;
1217 } else {
1218 return (l > 0) ? 1 : -1;
1219 }
1220}
1221
1222int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **ap, const SSL_CIPHER **bp) {
1223 long l;
1224 const long a_id = (*ap)->id;
1225 const long b_id = (*bp)->id;
1226
1227 l = a_id - b_id;
1228 if (l == 0) {
1229 return 0;
1230 } else {
1231 return (l > 0) ? 1 : -1;
1232 }
1233}
1234
1235/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001236 * preference */
Adam Langleyfcf25832014-12-18 17:42:32 -08001237STACK_OF(SSL_CIPHER) * SSL_get_ciphers(const SSL *s) {
1238 if (s == NULL) {
1239 return NULL;
1240 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001241
Adam Langleyfcf25832014-12-18 17:42:32 -08001242 if (s->cipher_list != NULL) {
1243 return s->cipher_list->ciphers;
1244 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001245
Adam Langleyfcf25832014-12-18 17:42:32 -08001246 if (s->version >= TLS1_1_VERSION && s->ctx != NULL &&
1247 s->ctx->cipher_list_tls11 != NULL) {
1248 return s->ctx->cipher_list_tls11->ciphers;
1249 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001250
Adam Langleyfcf25832014-12-18 17:42:32 -08001251 if (s->ctx != NULL && s->ctx->cipher_list != NULL) {
1252 return s->ctx->cipher_list->ciphers;
1253 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001254
Adam Langleyfcf25832014-12-18 17:42:32 -08001255 return NULL;
1256}
Adam Langley95c29f32014-06-20 12:00:00 -07001257
Adam Langleyfcf25832014-12-18 17:42:32 -08001258/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001259 * algorithm id */
Adam Langleyfcf25832014-12-18 17:42:32 -08001260STACK_OF(SSL_CIPHER) * ssl_get_ciphers_by_id(SSL *s) {
1261 if (s == NULL) {
1262 return NULL;
1263 }
Adam Langley95c29f32014-06-20 12:00:00 -07001264
Adam Langleyfcf25832014-12-18 17:42:32 -08001265 if (s->cipher_list_by_id != NULL) {
1266 return s->cipher_list_by_id;
1267 }
Adam Langley95c29f32014-06-20 12:00:00 -07001268
Adam Langleyfcf25832014-12-18 17:42:32 -08001269 if (s->ctx != NULL && s->ctx->cipher_list_by_id != NULL) {
1270 return s->ctx->cipher_list_by_id;
1271 }
Adam Langley95c29f32014-06-20 12:00:00 -07001272
Adam Langleyfcf25832014-12-18 17:42:32 -08001273 return NULL;
1274}
Adam Langley95c29f32014-06-20 12:00:00 -07001275
Adam Langleyfcf25832014-12-18 17:42:32 -08001276/* The old interface to get the same thing as SSL_get_ciphers() */
1277const char *SSL_get_cipher_list(const SSL *s, int n) {
1278 const SSL_CIPHER *c;
1279 STACK_OF(SSL_CIPHER) * sk;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001280
Adam Langleyfcf25832014-12-18 17:42:32 -08001281 if (s == NULL) {
1282 return NULL;
1283 }
Adam Langley95c29f32014-06-20 12:00:00 -07001284
Adam Langleyfcf25832014-12-18 17:42:32 -08001285 sk = SSL_get_ciphers(s);
1286 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
1287 return NULL;
1288 }
Adam Langley95c29f32014-06-20 12:00:00 -07001289
Adam Langleyfcf25832014-12-18 17:42:32 -08001290 c = sk_SSL_CIPHER_value(sk, n);
1291 if (c == NULL) {
1292 return NULL;
1293 }
Adam Langley95c29f32014-06-20 12:00:00 -07001294
Adam Langleyfcf25832014-12-18 17:42:32 -08001295 return c->name;
1296}
David Benjamin5491e3f2014-09-29 19:33:09 -04001297
Adam Langleyfcf25832014-12-18 17:42:32 -08001298/* specify the ciphers to be used by default by the SSL_CTX */
1299int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
1300 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001301
Adam Langleyfcf25832014-12-18 17:42:32 -08001302 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001303 &ctx->cipher_list_by_id, str);
Adam Langleyfcf25832014-12-18 17:42:32 -08001304 /* ssl_create_cipher_list may return an empty stack if it was unable to find
1305 * a cipher matching the given rule string (for example if the rule string
1306 * specifies a cipher which has been disabled). This is not an error as far
1307 * as ssl_create_cipher_list is concerned, and hence ctx->cipher_list and
1308 * ctx->cipher_list_by_id has been updated. */
1309 if (sk == NULL) {
1310 return 0;
1311 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1312 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1313 return 0;
1314 }
Adam Langley95c29f32014-06-20 12:00:00 -07001315
Adam Langleyfcf25832014-12-18 17:42:32 -08001316 return 1;
1317}
David Benjamin39482a12014-07-20 13:30:15 -04001318
Adam Langleyfcf25832014-12-18 17:42:32 -08001319int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str) {
1320 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001321
David Benjamin71f07942015-04-08 02:36:59 -04001322 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str);
Adam Langleyfcf25832014-12-18 17:42:32 -08001323 if (sk == NULL) {
1324 return 0;
1325 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1326 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list_tls11,
1327 SSL_R_NO_CIPHER_MATCH);
1328 return 0;
1329 }
David Benjamin9f2c0d72014-10-21 22:00:19 -04001330
Adam Langleyfcf25832014-12-18 17:42:32 -08001331 return 1;
1332}
Adam Langley95c29f32014-06-20 12:00:00 -07001333
Adam Langleyfcf25832014-12-18 17:42:32 -08001334/* specify the ciphers to be used by the SSL */
1335int SSL_set_cipher_list(SSL *s, const char *str) {
1336 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001337
Adam Langleyfcf25832014-12-18 17:42:32 -08001338 sk = ssl_create_cipher_list(s->ctx->method, &s->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001339 &s->cipher_list_by_id, str);
David Benjamin39482a12014-07-20 13:30:15 -04001340
Adam Langleyfcf25832014-12-18 17:42:32 -08001341 /* see comment in SSL_CTX_set_cipher_list */
1342 if (sk == NULL) {
1343 return 0;
1344 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1345 OPENSSL_PUT_ERROR(SSL, SSL_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1346 return 0;
1347 }
David Benjamin39482a12014-07-20 13:30:15 -04001348
Adam Langleyfcf25832014-12-18 17:42:32 -08001349 return 1;
1350}
Adam Langley95c29f32014-06-20 12:00:00 -07001351
Adam Langleyfcf25832014-12-18 17:42:32 -08001352int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, uint8_t *p) {
1353 size_t i;
1354 const SSL_CIPHER *c;
1355 CERT *ct = s->cert;
1356 uint8_t *q;
1357 /* Set disabled masks for this session */
1358 ssl_set_client_disabled(s);
Adam Langley29707792014-06-20 12:00:00 -07001359
Adam Langleyfcf25832014-12-18 17:42:32 -08001360 if (sk == NULL) {
1361 return 0;
1362 }
1363 q = p;
Adam Langley95c29f32014-06-20 12:00:00 -07001364
Adam Langleyfcf25832014-12-18 17:42:32 -08001365 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
1366 c = sk_SSL_CIPHER_value(sk, i);
1367 /* Skip disabled ciphers */
1368 if (c->algorithm_ssl & ct->mask_ssl ||
1369 c->algorithm_mkey & ct->mask_k ||
1370 c->algorithm_auth & ct->mask_a) {
1371 continue;
1372 }
1373 s2n(ssl3_get_cipher_value(c), p);
1374 }
1375
1376 /* If all ciphers were disabled, return the error to the caller. */
1377 if (p == q) {
1378 return 0;
1379 }
1380
1381 /* Add SCSVs. */
1382 if (!s->renegotiate) {
1383 s2n(SSL3_CK_SCSV & 0xffff, p);
1384 }
1385
Adam Langley5f0efe02015-02-20 13:03:16 -08001386 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001387 s2n(SSL3_CK_FALLBACK_SCSV & 0xffff, p);
1388 }
1389
1390 return p - q;
1391}
1392
1393STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs) {
1394 CBS cipher_suites = *cbs;
1395 const SSL_CIPHER *c;
1396 STACK_OF(SSL_CIPHER) * sk;
1397
1398 if (s->s3) {
1399 s->s3->send_connection_binding = 0;
1400 }
1401
1402 if (CBS_len(&cipher_suites) % 2 != 0) {
1403 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1404 SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
1405 return NULL;
1406 }
1407
1408 sk = sk_SSL_CIPHER_new_null();
1409 if (sk == NULL) {
1410 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1411 goto err;
1412 }
1413
1414 if (!CBS_stow(&cipher_suites, &s->cert->ciphers_raw,
1415 &s->cert->ciphers_rawlen)) {
1416 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1417 goto err;
1418 }
1419
1420 while (CBS_len(&cipher_suites) > 0) {
1421 uint16_t cipher_suite;
1422
1423 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
1424 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_INTERNAL_ERROR);
1425 goto err;
1426 }
1427
1428 /* Check for SCSV. */
1429 if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff)) {
1430 /* SCSV is fatal if renegotiating. */
1431 if (s->renegotiate) {
1432 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1433 SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1434 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1435 goto err;
1436 }
1437 s->s3->send_connection_binding = 1;
1438 continue;
1439 }
1440
1441 /* Check for FALLBACK_SCSV. */
1442 if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff)) {
1443 uint16_t max_version = ssl3_get_max_server_version(s);
1444 if (SSL_IS_DTLS(s) ? (uint16_t)s->version > max_version
1445 : (uint16_t)s->version < max_version) {
1446 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1447 SSL_R_INAPPROPRIATE_FALLBACK);
1448 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
1449 goto err;
1450 }
1451 continue;
1452 }
1453
1454 c = ssl3_get_cipher_by_value(cipher_suite);
1455 if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
1456 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1457 goto err;
1458 }
1459 }
1460
1461 return sk;
David Benjamin9f2c0d72014-10-21 22:00:19 -04001462
Adam Langley95c29f32014-06-20 12:00:00 -07001463err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001464 if (sk != NULL) {
1465 sk_SSL_CIPHER_free(sk);
1466 }
1467 return NULL;
1468}
Adam Langley95c29f32014-06-20 12:00:00 -07001469
1470
Adam Langleyfcf25832014-12-18 17:42:32 -08001471/* return a servername extension value if provided in Client Hello, or NULL. So
1472 * far, only host_name types are defined (RFC 3546). */
1473const char *SSL_get_servername(const SSL *s, const int type) {
1474 if (type != TLSEXT_NAMETYPE_host_name) {
1475 return NULL;
1476 }
Adam Langley95c29f32014-06-20 12:00:00 -07001477
Adam Langleyfcf25832014-12-18 17:42:32 -08001478 return s->session && !s->tlsext_hostname ? s->session->tlsext_hostname
1479 : s->tlsext_hostname;
1480}
Adam Langley95c29f32014-06-20 12:00:00 -07001481
Adam Langleyfcf25832014-12-18 17:42:32 -08001482int SSL_get_servername_type(const SSL *s) {
1483 if (s->session &&
1484 (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname)) {
1485 return TLSEXT_NAMETYPE_host_name;
1486 }
Adam Langley95c29f32014-06-20 12:00:00 -07001487
Adam Langleyfcf25832014-12-18 17:42:32 -08001488 return -1;
1489}
Adam Langley95c29f32014-06-20 12:00:00 -07001490
Adam Langleyfcf25832014-12-18 17:42:32 -08001491void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
1492 ctx->signed_cert_timestamps_enabled = 1;
1493}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001494
Adam Langleyfcf25832014-12-18 17:42:32 -08001495int SSL_enable_signed_cert_timestamps(SSL *ssl) {
1496 ssl->signed_cert_timestamps_enabled = 1;
1497 return 1;
1498}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001499
Adam Langleyfcf25832014-12-18 17:42:32 -08001500void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
1501 ctx->ocsp_stapling_enabled = 1;
1502}
David Benjamin6c7aed02014-08-27 16:42:38 -04001503
Adam Langleyfcf25832014-12-18 17:42:32 -08001504int SSL_enable_ocsp_stapling(SSL *ssl) {
1505 ssl->ocsp_stapling_enabled = 1;
1506 return 1;
1507}
David Benjamin6c7aed02014-08-27 16:42:38 -04001508
Adam Langleyfcf25832014-12-18 17:42:32 -08001509void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
1510 size_t *out_len) {
1511 SSL_SESSION *session = ssl->session;
Adam Langley3cb50e02014-08-26 14:00:31 -07001512
Adam Langleyfcf25832014-12-18 17:42:32 -08001513 *out_len = 0;
1514 *out = NULL;
1515 if (ssl->server || !session || !session->tlsext_signed_cert_timestamp_list) {
1516 return;
1517 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001518
Adam Langleyfcf25832014-12-18 17:42:32 -08001519 *out = session->tlsext_signed_cert_timestamp_list;
1520 *out_len = session->tlsext_signed_cert_timestamp_list_length;
1521}
David Benjamin6c7aed02014-08-27 16:42:38 -04001522
Adam Langleyfcf25832014-12-18 17:42:32 -08001523void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
1524 size_t *out_len) {
1525 SSL_SESSION *session = ssl->session;
1526
1527 *out_len = 0;
1528 *out = NULL;
1529 if (ssl->server || !session || !session->ocsp_response) {
1530 return;
1531 }
1532 *out = session->ocsp_response;
1533 *out_len = session->ocsp_response_length;
1534}
David Benjamin6c7aed02014-08-27 16:42:38 -04001535
Adam Langley95c29f32014-06-20 12:00:00 -07001536/* SSL_select_next_proto implements the standard protocol selection. It is
1537 * expected that this function is called from the callback set by
1538 * SSL_CTX_set_next_proto_select_cb.
1539 *
1540 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1541 * strings. The length byte itself is not included in the length. A byte
1542 * string of length 0 is invalid. No byte string may be truncated.
1543 *
1544 * The current, but experimental algorithm for selecting the protocol is:
1545 *
1546 * 1) If the server doesn't support NPN then this is indicated to the
1547 * callback. In this case, the client application has to abort the connection
1548 * or have a default application level protocol.
1549 *
1550 * 2) If the server supports NPN, but advertises an empty list then the
1551 * client selects the first protcol in its list, but indicates via the
1552 * API that this fallback case was enacted.
1553 *
1554 * 3) Otherwise, the client finds the first protocol in the server's list
1555 * that it supports and selects this protocol. This is because it's
1556 * assumed that the server has better information about which protocol
1557 * a client should use.
1558 *
1559 * 4) If the client doesn't support any of the server's advertised
1560 * protocols, then this is treated the same as case 2.
1561 *
1562 * It returns either
1563 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1564 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1565 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001566int SSL_select_next_proto(uint8_t **out, uint8_t *outlen, const uint8_t *server,
1567 unsigned int server_len, const uint8_t *client,
1568 unsigned int client_len) {
1569 unsigned int i, j;
1570 const uint8_t *result;
1571 int status = OPENSSL_NPN_UNSUPPORTED;
Adam Langley95c29f32014-06-20 12:00:00 -07001572
Adam Langleyfcf25832014-12-18 17:42:32 -08001573 /* For each protocol in server preference order, see if we support it. */
1574 for (i = 0; i < server_len;) {
1575 for (j = 0; j < client_len;) {
1576 if (server[i] == client[j] &&
1577 memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
1578 /* We found a match */
1579 result = &server[i];
1580 status = OPENSSL_NPN_NEGOTIATED;
1581 goto found;
1582 }
1583 j += client[j];
1584 j++;
1585 }
1586 i += server[i];
1587 i++;
1588 }
Adam Langley95c29f32014-06-20 12:00:00 -07001589
Adam Langleyfcf25832014-12-18 17:42:32 -08001590 /* There's no overlap between our protocols and the server's list. */
1591 result = client;
1592 status = OPENSSL_NPN_NO_OVERLAP;
Adam Langley95c29f32014-06-20 12:00:00 -07001593
Adam Langleyfcf25832014-12-18 17:42:32 -08001594found:
1595 *out = (uint8_t *)result + 1;
1596 *outlen = result[0];
1597 return status;
1598}
Adam Langley95c29f32014-06-20 12:00:00 -07001599
Adam Langley95c29f32014-06-20 12:00:00 -07001600/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
1601 * requested protocol for this connection and returns 0. If the client didn't
1602 * request any protocol, then *data is set to NULL.
1603 *
1604 * Note that the client can request any protocol it chooses. The value returned
1605 * from this function need not be a member of the list of supported protocols
Adam Langleyfcf25832014-12-18 17:42:32 -08001606 * provided by the callback. */
1607void SSL_get0_next_proto_negotiated(const SSL *s, const uint8_t **data,
1608 unsigned *len) {
1609 *data = s->next_proto_negotiated;
1610 if (!*data) {
1611 *len = 0;
1612 } else {
1613 *len = s->next_proto_negotiated_len;
1614 }
Adam Langley95c29f32014-06-20 12:00:00 -07001615}
1616
1617/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
1618 * TLS server needs a list of supported protocols for Next Protocol
1619 * Negotiation. The returned list must be in wire format. The list is returned
1620 * by setting |out| to point to it and |outlen| to its length. This memory will
1621 * not be modified, but one should assume that the SSL* keeps a reference to
1622 * it.
1623 *
Adam Langleyfcf25832014-12-18 17:42:32 -08001624 * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise.
1625 * Otherwise, no such extension will be included in the ServerHello. */
1626void SSL_CTX_set_next_protos_advertised_cb(
1627 SSL_CTX *ctx,
1628 int (*cb)(SSL *ssl, const uint8_t **out, unsigned int *outlen, void *arg),
1629 void *arg) {
1630 ctx->next_protos_advertised_cb = cb;
1631 ctx->next_protos_advertised_cb_arg = arg;
1632}
Adam Langley95c29f32014-06-20 12:00:00 -07001633
1634/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1635 * client needs to select a protocol from the server's provided list. |out|
1636 * must be set to point to the selected protocol (which may be within |in|).
1637 * The length of the protocol name must be written into |outlen|. The server's
1638 * advertised protocols are provided in |in| and |inlen|. The callback can
1639 * assume that |in| is syntactically valid.
1640 *
1641 * The client must select a protocol. It is fatal to the connection if this
1642 * callback returns a value other than SSL_TLSEXT_ERR_OK.
1643 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001644void SSL_CTX_set_next_proto_select_cb(
1645 SSL_CTX *ctx, int (*cb)(SSL *s, uint8_t **out, uint8_t *outlen,
1646 const uint8_t *in, unsigned int inlen, void *arg),
1647 void *arg) {
1648 ctx->next_proto_select_cb = cb;
1649 ctx->next_proto_select_cb_arg = arg;
1650}
Adam Langley95c29f32014-06-20 12:00:00 -07001651
Adam Langleyfcf25832014-12-18 17:42:32 -08001652int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
1653 unsigned protos_len) {
1654 if (ctx->alpn_client_proto_list) {
1655 OPENSSL_free(ctx->alpn_client_proto_list);
1656 }
Adam Langley95c29f32014-06-20 12:00:00 -07001657
Adam Langleyfcf25832014-12-18 17:42:32 -08001658 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1659 if (!ctx->alpn_client_proto_list) {
1660 return 1;
1661 }
1662 ctx->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001663
Adam Langleyfcf25832014-12-18 17:42:32 -08001664 return 0;
1665}
Adam Langley95c29f32014-06-20 12:00:00 -07001666
Adam Langleyfcf25832014-12-18 17:42:32 -08001667int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
1668 if (ssl->alpn_client_proto_list) {
1669 OPENSSL_free(ssl->alpn_client_proto_list);
1670 }
Adam Langley95c29f32014-06-20 12:00:00 -07001671
Adam Langleyfcf25832014-12-18 17:42:32 -08001672 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1673 if (!ssl->alpn_client_proto_list) {
1674 return 1;
1675 }
1676 ssl->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001677
Adam Langleyfcf25832014-12-18 17:42:32 -08001678 return 0;
1679}
Adam Langley95c29f32014-06-20 12:00:00 -07001680
1681/* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
1682 * during ClientHello processing in order to select an ALPN protocol from the
1683 * client's list of offered protocols. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001684void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
1685 int (*cb)(SSL *ssl, const uint8_t **out,
1686 uint8_t *outlen, const uint8_t *in,
1687 unsigned int inlen, void *arg),
1688 void *arg) {
1689 ctx->alpn_select_cb = cb;
1690 ctx->alpn_select_cb_arg = arg;
1691}
Adam Langley95c29f32014-06-20 12:00:00 -07001692
1693/* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
1694 * On return it sets |*data| to point to |*len| bytes of protocol name (not
1695 * including the leading length-prefix byte). If the server didn't respond with
1696 * a negotiated protocol then |*len| will be zero. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001697void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **data,
1698 unsigned *len) {
1699 *data = NULL;
1700 if (ssl->s3) {
1701 *data = ssl->s3->alpn_selected;
1702 }
1703 if (*data == NULL) {
1704 *len = 0;
1705 } else {
1706 *len = ssl->s3->alpn_selected_len;
1707 }
1708}
Adam Langley95c29f32014-06-20 12:00:00 -07001709
David Benjamincfd248b2015-04-03 11:02:24 -04001710int SSL_export_keying_material(SSL *s, uint8_t *out, size_t out_len,
1711 const char *label, size_t label_len,
1712 const uint8_t *context, size_t context_len,
1713 int use_context) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001714 if (s->version < TLS1_VERSION) {
David Benjaminc565ebb2015-04-03 04:06:36 -04001715 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001716 }
Adam Langley95c29f32014-06-20 12:00:00 -07001717
David Benjamincfd248b2015-04-03 11:02:24 -04001718 return s->enc_method->export_keying_material(
1719 s, out, out_len, label, label_len, context, context_len, use_context);
Adam Langleyfcf25832014-12-18 17:42:32 -08001720}
Adam Langley95c29f32014-06-20 12:00:00 -07001721
Adam Langleyfcf25832014-12-18 17:42:32 -08001722static uint32_t ssl_session_hash(const SSL_SESSION *a) {
1723 uint32_t hash =
1724 ((uint32_t)a->session_id[0]) ||
1725 ((uint32_t)a->session_id[1] << 8) ||
1726 ((uint32_t)a->session_id[2] << 16) ||
1727 ((uint32_t)a->session_id[3] << 24);
Adam Langley95c29f32014-06-20 12:00:00 -07001728
Adam Langleyfcf25832014-12-18 17:42:32 -08001729 return hash;
1730}
Adam Langley95c29f32014-06-20 12:00:00 -07001731
1732/* NB: If this function (or indeed the hash function which uses a sort of
1733 * coarser function than this one) is changed, ensure
1734 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1735 * able to construct an SSL_SESSION that will collide with any existing session
1736 * with a matching session ID. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001737static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
1738 if (a->ssl_version != b->ssl_version) {
1739 return 1;
1740 }
Adam Langley95c29f32014-06-20 12:00:00 -07001741
Adam Langleyfcf25832014-12-18 17:42:32 -08001742 if (a->session_id_length != b->session_id_length) {
1743 return 1;
1744 }
Adam Langley95c29f32014-06-20 12:00:00 -07001745
Adam Langleyfcf25832014-12-18 17:42:32 -08001746 return memcmp(a->session_id, b->session_id, a->session_id_length);
1747}
Adam Langley95c29f32014-06-20 12:00:00 -07001748
Adam Langleyfcf25832014-12-18 17:42:32 -08001749SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth) {
1750 SSL_CTX *ret = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001751
Adam Langleyfcf25832014-12-18 17:42:32 -08001752 if (meth == NULL) {
1753 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_NULL_SSL_METHOD_PASSED);
1754 return NULL;
1755 }
Adam Langley95c29f32014-06-20 12:00:00 -07001756
Adam Langleyfcf25832014-12-18 17:42:32 -08001757 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
1758 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
1759 goto err;
1760 }
Adam Langley95c29f32014-06-20 12:00:00 -07001761
Adam Langleyfcf25832014-12-18 17:42:32 -08001762 ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1763 if (ret == NULL) {
1764 goto err;
1765 }
Adam Langley95c29f32014-06-20 12:00:00 -07001766
Adam Langleyfcf25832014-12-18 17:42:32 -08001767 memset(ret, 0, sizeof(SSL_CTX));
Adam Langley95c29f32014-06-20 12:00:00 -07001768
Adam Langleyfcf25832014-12-18 17:42:32 -08001769 ret->method = meth->method;
Adam Langley95c29f32014-06-20 12:00:00 -07001770
Adam Langleyfcf25832014-12-18 17:42:32 -08001771 ret->cert_store = NULL;
1772 ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
1773 ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1774 ret->session_cache_head = NULL;
1775 ret->session_cache_tail = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001776
Adam Langleyfcf25832014-12-18 17:42:32 -08001777 /* We take the system default */
1778 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001779
Adam Langleyfcf25832014-12-18 17:42:32 -08001780 ret->new_session_cb = 0;
1781 ret->remove_session_cb = 0;
1782 ret->get_session_cb = 0;
1783 ret->generate_session_id = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001784
Adam Langleyfcf25832014-12-18 17:42:32 -08001785 ret->references = 1;
1786 ret->quiet_shutdown = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001787
Adam Langleyfcf25832014-12-18 17:42:32 -08001788 ret->info_callback = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001789
Adam Langleyfcf25832014-12-18 17:42:32 -08001790 ret->app_verify_callback = 0;
1791 ret->app_verify_arg = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001792
Adam Langleyfcf25832014-12-18 17:42:32 -08001793 ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
1794 ret->read_ahead = 0;
1795 ret->msg_callback = 0;
1796 ret->msg_callback_arg = NULL;
1797 ret->verify_mode = SSL_VERIFY_NONE;
1798 ret->sid_ctx_length = 0;
1799 ret->default_verify_callback = NULL;
1800 ret->cert = ssl_cert_new();
1801 if (ret->cert == NULL) {
1802 goto err;
1803 }
Adam Langley95c29f32014-06-20 12:00:00 -07001804
Adam Langleyfcf25832014-12-18 17:42:32 -08001805 ret->default_passwd_callback = 0;
1806 ret->default_passwd_callback_userdata = NULL;
1807 ret->client_cert_cb = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001808
Adam Langleyfcf25832014-12-18 17:42:32 -08001809 ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
1810 if (ret->sessions == NULL) {
1811 goto err;
1812 }
1813 ret->cert_store = X509_STORE_new();
1814 if (ret->cert_store == NULL) {
1815 goto err;
1816 }
Adam Langley95c29f32014-06-20 12:00:00 -07001817
Adam Langleyfcf25832014-12-18 17:42:32 -08001818 ssl_create_cipher_list(ret->method, &ret->cipher_list,
David Benjamin71f07942015-04-08 02:36:59 -04001819 &ret->cipher_list_by_id, SSL_DEFAULT_CIPHER_LIST);
Adam Langleyfcf25832014-12-18 17:42:32 -08001820 if (ret->cipher_list == NULL ||
1821 sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0) {
1822 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_LIBRARY_HAS_NO_CIPHERS);
1823 goto err2;
1824 }
Adam Langley95c29f32014-06-20 12:00:00 -07001825
Adam Langleyfcf25832014-12-18 17:42:32 -08001826 ret->param = X509_VERIFY_PARAM_new();
1827 if (!ret->param) {
1828 goto err;
1829 }
Adam Langley95c29f32014-06-20 12:00:00 -07001830
Adam Langleyfcf25832014-12-18 17:42:32 -08001831 ret->client_CA = sk_X509_NAME_new_null();
1832 if (ret->client_CA == NULL) {
1833 goto err;
1834 }
Adam Langley95c29f32014-06-20 12:00:00 -07001835
Adam Langleyfcf25832014-12-18 17:42:32 -08001836 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001837
Adam Langleyfcf25832014-12-18 17:42:32 -08001838 ret->extra_certs = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001839
Adam Langleyfcf25832014-12-18 17:42:32 -08001840 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
David Benjamin422d3a42014-08-20 11:09:03 -04001841
Adam Langleyfcf25832014-12-18 17:42:32 -08001842 ret->tlsext_servername_callback = 0;
1843 ret->tlsext_servername_arg = NULL;
1844 /* Setup RFC4507 ticket keys */
1845 if (!RAND_bytes(ret->tlsext_tick_key_name, 16) ||
1846 !RAND_bytes(ret->tlsext_tick_hmac_key, 16) ||
1847 !RAND_bytes(ret->tlsext_tick_aes_key, 16)) {
1848 ret->options |= SSL_OP_NO_TICKET;
1849 }
Adam Langley95c29f32014-06-20 12:00:00 -07001850
Adam Langleyfcf25832014-12-18 17:42:32 -08001851 ret->tlsext_status_cb = 0;
1852 ret->tlsext_status_arg = NULL;
David Benjamin82c9e902014-12-12 15:55:27 -05001853
Adam Langleyfcf25832014-12-18 17:42:32 -08001854 ret->next_protos_advertised_cb = 0;
1855 ret->next_proto_select_cb = 0;
1856 ret->psk_identity_hint = NULL;
1857 ret->psk_client_callback = NULL;
1858 ret->psk_server_callback = NULL;
1859
1860 /* Default is to connect to non-RI servers. When RI is more widely deployed
1861 * might change this. */
1862 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
1863
1864 /* Lock the SSL_CTX to the specified version, for compatibility with legacy
1865 * uses of SSL_METHOD. */
1866 if (meth->version != 0) {
1867 SSL_CTX_set_max_version(ret, meth->version);
1868 SSL_CTX_set_min_version(ret, meth->version);
1869 }
1870
1871 return ret;
1872
Adam Langley95c29f32014-06-20 12:00:00 -07001873err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001874 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -07001875err2:
Adam Langleyfcf25832014-12-18 17:42:32 -08001876 if (ret != NULL) {
1877 SSL_CTX_free(ret);
1878 }
1879 return NULL;
1880}
Adam Langley95c29f32014-06-20 12:00:00 -07001881
Adam Langleyfcf25832014-12-18 17:42:32 -08001882void SSL_CTX_free(SSL_CTX *a) {
1883 int i;
Adam Langley95c29f32014-06-20 12:00:00 -07001884
Adam Langleyfcf25832014-12-18 17:42:32 -08001885 if (a == NULL) {
1886 return;
1887 }
Adam Langley95c29f32014-06-20 12:00:00 -07001888
Adam Langleyfcf25832014-12-18 17:42:32 -08001889 i = CRYPTO_add(&a->references, -1, CRYPTO_LOCK_SSL_CTX);
1890 if (i > 0) {
1891 return;
1892 }
Adam Langley95c29f32014-06-20 12:00:00 -07001893
Adam Langleyfcf25832014-12-18 17:42:32 -08001894 if (a->param) {
1895 X509_VERIFY_PARAM_free(a->param);
1896 }
Adam Langley95c29f32014-06-20 12:00:00 -07001897
Adam Langleyfcf25832014-12-18 17:42:32 -08001898 /* Free internal session cache. However: the remove_cb() may reference the
1899 * ex_data of SSL_CTX, thus the ex_data store can only be removed after the
1900 * sessions were flushed. As the ex_data handling routines might also touch
1901 * the session cache, the most secure solution seems to be: empty (flush) the
1902 * cache, then free ex_data, then finally free the cache. (See ticket
1903 * [openssl.org #212].) */
1904 if (a->sessions != NULL) {
1905 SSL_CTX_flush_sessions(a, 0);
1906 }
Adam Langley95c29f32014-06-20 12:00:00 -07001907
Adam Langleyfcf25832014-12-18 17:42:32 -08001908 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001909
Adam Langleyfcf25832014-12-18 17:42:32 -08001910 if (a->sessions != NULL) {
1911 lh_SSL_SESSION_free(a->sessions);
1912 }
1913 if (a->cert_store != NULL) {
1914 X509_STORE_free(a->cert_store);
1915 }
1916 if (a->cipher_list != NULL) {
1917 ssl_cipher_preference_list_free(a->cipher_list);
1918 }
1919 if (a->cipher_list_by_id != NULL) {
1920 sk_SSL_CIPHER_free(a->cipher_list_by_id);
1921 }
1922 if (a->cipher_list_tls11 != NULL) {
1923 ssl_cipher_preference_list_free(a->cipher_list_tls11);
1924 }
1925 if (a->cert != NULL) {
1926 ssl_cert_free(a->cert);
1927 }
1928 if (a->client_CA != NULL) {
1929 sk_X509_NAME_pop_free(a->client_CA, X509_NAME_free);
1930 }
1931 if (a->extra_certs != NULL) {
1932 sk_X509_pop_free(a->extra_certs, X509_free);
1933 }
1934 if (a->srtp_profiles) {
1935 sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);
1936 }
1937 if (a->psk_identity_hint) {
1938 OPENSSL_free(a->psk_identity_hint);
1939 }
1940 if (a->tlsext_ecpointformatlist) {
1941 OPENSSL_free(a->tlsext_ecpointformatlist);
1942 }
1943 if (a->tlsext_ellipticcurvelist) {
1944 OPENSSL_free(a->tlsext_ellipticcurvelist);
1945 }
1946 if (a->alpn_client_proto_list != NULL) {
1947 OPENSSL_free(a->alpn_client_proto_list);
1948 }
1949 if (a->tlsext_channel_id_private) {
1950 EVP_PKEY_free(a->tlsext_channel_id_private);
1951 }
1952 if (a->keylog_bio) {
1953 BIO_free(a->keylog_bio);
1954 }
Adam Langley95c29f32014-06-20 12:00:00 -07001955
Adam Langleyfcf25832014-12-18 17:42:32 -08001956 OPENSSL_free(a);
1957}
Adam Langley95c29f32014-06-20 12:00:00 -07001958
Adam Langleyfcf25832014-12-18 17:42:32 -08001959void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) {
1960 ctx->default_passwd_callback = cb;
1961}
Adam Langley95c29f32014-06-20 12:00:00 -07001962
Adam Langleyfcf25832014-12-18 17:42:32 -08001963void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u) {
1964 ctx->default_passwd_callback_userdata = u;
1965}
Adam Langley95c29f32014-06-20 12:00:00 -07001966
Adam Langleyfcf25832014-12-18 17:42:32 -08001967void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
1968 int (*cb)(X509_STORE_CTX *, void *),
1969 void *arg) {
1970 ctx->app_verify_callback = cb;
1971 ctx->app_verify_arg = arg;
1972}
Adam Langley95c29f32014-06-20 12:00:00 -07001973
Adam Langleyfcf25832014-12-18 17:42:32 -08001974void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
1975 int (*cb)(int, X509_STORE_CTX *)) {
1976 ctx->verify_mode = mode;
1977 ctx->default_verify_callback = cb;
1978}
Adam Langley95c29f32014-06-20 12:00:00 -07001979
Adam Langleyfcf25832014-12-18 17:42:32 -08001980void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
1981 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
1982}
Adam Langley1258b6a2014-06-20 12:00:00 -07001983
Adam Langleyfcf25832014-12-18 17:42:32 -08001984void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb)(SSL *ssl, void *arg),
1985 void *arg) {
1986 ssl_cert_set_cert_cb(c->cert, cb, arg);
1987}
David Benjamin859ec3c2014-09-02 16:29:36 -04001988
Adam Langleyfcf25832014-12-18 17:42:32 -08001989void SSL_set_cert_cb(SSL *s, int (*cb)(SSL *ssl, void *arg), void *arg) {
1990 ssl_cert_set_cert_cb(s->cert, cb, arg);
1991}
Adam Langley95c29f32014-06-20 12:00:00 -07001992
Adam Langleyfcf25832014-12-18 17:42:32 -08001993static int ssl_has_key(SSL *s, size_t idx) {
1994 CERT_PKEY *cpk = &s->cert->pkeys[idx];
1995 return cpk->x509 && cpk->privatekey;
1996}
David Benjamin033e5f42014-11-13 18:47:41 -05001997
David Benjamin107db582015-04-08 00:41:59 -04001998void ssl_get_compatible_server_ciphers(SSL *s, uint32_t *out_mask_k,
1999 uint32_t *out_mask_a) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002000 CERT *c = s->cert;
2001 int rsa_enc, rsa_sign, dh_tmp;
David Benjamin107db582015-04-08 00:41:59 -04002002 uint32_t mask_k, mask_a;
Adam Langleyfcf25832014-12-18 17:42:32 -08002003 int have_ecc_cert, ecdsa_ok;
2004 int have_ecdh_tmp;
2005 X509 *x;
Adam Langley95c29f32014-06-20 12:00:00 -07002006
Adam Langleyfcf25832014-12-18 17:42:32 -08002007 if (c == NULL) {
2008 /* TODO(davidben): Is this codepath possible? */
2009 *out_mask_k = 0;
2010 *out_mask_a = 0;
2011 return;
2012 }
Adam Langley95c29f32014-06-20 12:00:00 -07002013
Adam Langleyfcf25832014-12-18 17:42:32 -08002014 dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07002015
David Benjaminc0f763b2015-03-27 02:05:39 -04002016 have_ecdh_tmp = (c->ecdh_nid != NID_undef || c->ecdh_tmp_cb != NULL ||
2017 c->ecdh_tmp_auto);
Adam Langleyfcf25832014-12-18 17:42:32 -08002018 rsa_enc = ssl_has_key(s, SSL_PKEY_RSA_ENC);
2019 rsa_sign = ssl_has_key(s, SSL_PKEY_RSA_SIGN);
2020 have_ecc_cert = ssl_has_key(s, SSL_PKEY_ECC);
2021 mask_k = 0;
2022 mask_a = 0;
David Benjaminf31e6812014-11-13 18:05:55 -05002023
Adam Langleyfcf25832014-12-18 17:42:32 -08002024 if (rsa_enc) {
2025 mask_k |= SSL_kRSA;
2026 }
2027 if (dh_tmp) {
David Benjamin7061e282015-03-19 11:10:48 -04002028 mask_k |= SSL_kDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08002029 }
2030 if (rsa_enc || rsa_sign) {
2031 mask_a |= SSL_aRSA;
2032 }
Adam Langley95c29f32014-06-20 12:00:00 -07002033
Adam Langleyfcf25832014-12-18 17:42:32 -08002034 /* An ECC certificate may be usable for ECDSA cipher suites depending on the
2035 * key usage extension and on the client's curve preferences. */
2036 if (have_ecc_cert) {
2037 x = c->pkeys[SSL_PKEY_ECC].x509;
2038 /* This call populates extension flags (ex_flags). */
2039 X509_check_purpose(x, -1, 0);
2040 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE)
2041 ? (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)
2042 : 1;
2043 if (!tls1_check_ec_cert(s, x)) {
2044 ecdsa_ok = 0;
2045 }
2046 if (ecdsa_ok) {
2047 mask_a |= SSL_aECDSA;
2048 }
2049 }
Adam Langley95c29f32014-06-20 12:00:00 -07002050
Adam Langleyfcf25832014-12-18 17:42:32 -08002051 /* If we are considering an ECC cipher suite that uses an ephemeral EC
2052 * key, check it. */
2053 if (have_ecdh_tmp && tls1_check_ec_tmp_key(s)) {
David Benjamin7061e282015-03-19 11:10:48 -04002054 mask_k |= SSL_kECDHE;
Adam Langleyfcf25832014-12-18 17:42:32 -08002055 }
Adam Langley95c29f32014-06-20 12:00:00 -07002056
Adam Langleyfcf25832014-12-18 17:42:32 -08002057 /* PSK requires a server callback. */
2058 if (s->psk_server_callback != NULL) {
2059 mask_k |= SSL_kPSK;
2060 mask_a |= SSL_aPSK;
2061 }
Adam Langley95c29f32014-06-20 12:00:00 -07002062
Adam Langleyfcf25832014-12-18 17:42:32 -08002063 *out_mask_k = mask_k;
2064 *out_mask_a = mask_a;
2065}
Adam Langley95c29f32014-06-20 12:00:00 -07002066
2067/* This handy macro borrowed from crypto/x509v3/v3_purp.c */
2068#define ku_reject(x, usage) \
Adam Langleyfcf25832014-12-18 17:42:32 -08002069 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
Adam Langley95c29f32014-06-20 12:00:00 -07002070
Adam Langleyfcf25832014-12-18 17:42:32 -08002071int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s) {
2072 unsigned long alg_a;
2073 int signature_nid = 0, md_nid = 0, pk_nid = 0;
2074 const SSL_CIPHER *cs = s->s3->tmp.new_cipher;
Adam Langley95c29f32014-06-20 12:00:00 -07002075
Adam Langleyfcf25832014-12-18 17:42:32 -08002076 alg_a = cs->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -07002077
Adam Langleyfcf25832014-12-18 17:42:32 -08002078 /* This call populates the ex_flags field correctly */
2079 X509_check_purpose(x, -1, 0);
2080 if (x->sig_alg && x->sig_alg->algorithm) {
2081 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
2082 OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
2083 }
2084 if (alg_a & SSL_aECDSA) {
2085 /* key usage, if present, must allow signing */
2086 if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE)) {
2087 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg,
2088 SSL_R_ECC_CERT_NOT_FOR_SIGNING);
2089 return 0;
2090 }
2091 }
Adam Langley95c29f32014-06-20 12:00:00 -07002092
Adam Langleyfcf25832014-12-18 17:42:32 -08002093 return 1; /* all checks are ok */
2094}
Adam Langley95c29f32014-06-20 12:00:00 -07002095
Adam Langleyfcf25832014-12-18 17:42:32 -08002096static int ssl_get_server_cert_index(const SSL *s) {
2097 int idx;
2098 idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
2099 if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509) {
2100 idx = SSL_PKEY_RSA_SIGN;
2101 }
2102 if (idx == -1) {
2103 OPENSSL_PUT_ERROR(SSL, ssl_get_server_cert_index, ERR_R_INTERNAL_ERROR);
2104 }
2105 return idx;
2106}
Adam Langley95c29f32014-06-20 12:00:00 -07002107
Adam Langleyfcf25832014-12-18 17:42:32 -08002108CERT_PKEY *ssl_get_server_send_pkey(const SSL *s) {
2109 int i = ssl_get_server_cert_index(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002110
Adam Langleyfcf25832014-12-18 17:42:32 -08002111 /* This may or may not be an error. */
2112 if (i < 0) {
2113 return NULL;
2114 }
Adam Langley95c29f32014-06-20 12:00:00 -07002115
Adam Langleyfcf25832014-12-18 17:42:32 -08002116 /* May be NULL. */
2117 return &s->cert->pkeys[i];
2118}
Adam Langley95c29f32014-06-20 12:00:00 -07002119
Adam Langleyfcf25832014-12-18 17:42:32 -08002120EVP_PKEY *ssl_get_sign_pkey(SSL *s, const SSL_CIPHER *cipher) {
2121 unsigned long alg_a;
2122 CERT *c;
2123 int idx = -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002124
Adam Langleyfcf25832014-12-18 17:42:32 -08002125 alg_a = cipher->algorithm_auth;
2126 c = s->cert;
Adam Langley95c29f32014-06-20 12:00:00 -07002127
Adam Langleyfcf25832014-12-18 17:42:32 -08002128 if (alg_a & SSL_aRSA) {
2129 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL) {
2130 idx = SSL_PKEY_RSA_SIGN;
2131 } else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL) {
2132 idx = SSL_PKEY_RSA_ENC;
2133 }
2134 } else if ((alg_a & SSL_aECDSA) &&
2135 (c->pkeys[SSL_PKEY_ECC].privatekey != NULL)) {
2136 idx = SSL_PKEY_ECC;
2137 }
Adam Langley95c29f32014-06-20 12:00:00 -07002138
Adam Langleyfcf25832014-12-18 17:42:32 -08002139 if (idx == -1) {
2140 OPENSSL_PUT_ERROR(SSL, ssl_get_sign_pkey, ERR_R_INTERNAL_ERROR);
2141 return NULL;
2142 }
Adam Langley95c29f32014-06-20 12:00:00 -07002143
Adam Langleyfcf25832014-12-18 17:42:32 -08002144 return c->pkeys[idx].privatekey;
2145}
Adam Langley95c29f32014-06-20 12:00:00 -07002146
Adam Langleyfcf25832014-12-18 17:42:32 -08002147void ssl_update_cache(SSL *s, int mode) {
David Benjaminb6d0c6d2015-03-19 19:07:26 -04002148 /* Never cache sessions with empty session IDs. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002149 if (s->session->session_id_length == 0) {
2150 return;
2151 }
Adam Langley95c29f32014-06-20 12:00:00 -07002152
David Benjaminb6d0c6d2015-03-19 19:07:26 -04002153 SSL_CTX *ctx = s->initial_ctx;
2154 if ((ctx->session_cache_mode & mode) == mode && !s->hit &&
2155 ((ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) ||
2156 SSL_CTX_add_session(ctx, s->session)) &&
2157 ctx->new_session_cb != NULL) {
2158 /* Note: |new_session_cb| is called whether the internal session cache is
2159 * used or not. */
2160 if (!ctx->new_session_cb(s, SSL_SESSION_up_ref(s->session))) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002161 SSL_SESSION_free(s->session);
2162 }
2163 }
Adam Langley95c29f32014-06-20 12:00:00 -07002164
David Benjaminb6d0c6d2015-03-19 19:07:26 -04002165 if (!(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR) &&
2166 !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) &&
2167 (ctx->session_cache_mode & mode) == mode) {
2168 /* Automatically flush the internal session cache every 255 connections. */
2169 int flush_cache = 0;
2170 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2171 ctx->handshakes_since_cache_flush++;
2172 if (ctx->handshakes_since_cache_flush >= 255) {
2173 flush_cache = 1;
2174 ctx->handshakes_since_cache_flush = 0;
2175 }
2176 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
2177
2178 if (flush_cache) {
2179 SSL_CTX_flush_sessions(ctx, (unsigned long)time(NULL));
Adam Langleyfcf25832014-12-18 17:42:32 -08002180 }
2181 }
2182}
Adam Langley95c29f32014-06-20 12:00:00 -07002183
David Benjamin1a5c50f2015-03-11 16:22:37 -04002184int SSL_get_error(const SSL *s, int ret_code) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002185 int reason;
David Benjamin1a5c50f2015-03-11 16:22:37 -04002186 uint32_t err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002187 BIO *bio;
Adam Langley95c29f32014-06-20 12:00:00 -07002188
David Benjamin1a5c50f2015-03-11 16:22:37 -04002189 if (ret_code > 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002190 return SSL_ERROR_NONE;
2191 }
Adam Langley95c29f32014-06-20 12:00:00 -07002192
Adam Langleyfcf25832014-12-18 17:42:32 -08002193 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
2194 * where we do encode the error */
David Benjamin1a5c50f2015-03-11 16:22:37 -04002195 err = ERR_peek_error();
2196 if (err != 0) {
2197 if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002198 return SSL_ERROR_SYSCALL;
2199 }
2200 return SSL_ERROR_SSL;
2201 }
Adam Langley95c29f32014-06-20 12:00:00 -07002202
David Benjamin1a5c50f2015-03-11 16:22:37 -04002203 if (ret_code == 0) {
David Benjamin9a38e922015-01-22 16:06:11 -05002204 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2205 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) {
2206 /* The socket was cleanly shut down with a close_notify. */
2207 return SSL_ERROR_ZERO_RETURN;
2208 }
2209 /* An EOF was observed which violates the protocol, and the underlying
2210 * transport does not participate in the error queue. Bubble up to the
2211 * caller. */
2212 return SSL_ERROR_SYSCALL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002213 }
Adam Langleyb2ce0582014-06-20 12:00:00 -07002214
Adam Langleyfcf25832014-12-18 17:42:32 -08002215 if (SSL_want_session(s)) {
2216 return SSL_ERROR_PENDING_SESSION;
2217 }
Adam Langley95c29f32014-06-20 12:00:00 -07002218
Adam Langleyfcf25832014-12-18 17:42:32 -08002219 if (SSL_want_certificate(s)) {
2220 return SSL_ERROR_PENDING_CERTIFICATE;
2221 }
Adam Langley95c29f32014-06-20 12:00:00 -07002222
Adam Langleyfcf25832014-12-18 17:42:32 -08002223 if (SSL_want_read(s)) {
2224 bio = SSL_get_rbio(s);
2225 if (BIO_should_read(bio)) {
2226 return SSL_ERROR_WANT_READ;
2227 }
Adam Langley95c29f32014-06-20 12:00:00 -07002228
Adam Langleyfcf25832014-12-18 17:42:32 -08002229 if (BIO_should_write(bio)) {
2230 /* This one doesn't make too much sense ... We never try to write to the
2231 * rbio, and an application program where rbio and wbio are separate
2232 * couldn't even know what it should wait for. However if we ever set
2233 * s->rwstate incorrectly (so that we have SSL_want_read(s) instead of
2234 * SSL_want_write(s)) and rbio and wbio *are* the same, this test works
2235 * around that bug; so it might be safer to keep it. */
2236 return SSL_ERROR_WANT_WRITE;
2237 }
Adam Langley95c29f32014-06-20 12:00:00 -07002238
Adam Langleyfcf25832014-12-18 17:42:32 -08002239 if (BIO_should_io_special(bio)) {
2240 reason = BIO_get_retry_reason(bio);
2241 if (reason == BIO_RR_CONNECT) {
2242 return SSL_ERROR_WANT_CONNECT;
2243 }
Adam Langley95c29f32014-06-20 12:00:00 -07002244
Adam Langleyfcf25832014-12-18 17:42:32 -08002245 if (reason == BIO_RR_ACCEPT) {
2246 return SSL_ERROR_WANT_ACCEPT;
2247 }
Adam Langley95c29f32014-06-20 12:00:00 -07002248
Adam Langleyfcf25832014-12-18 17:42:32 -08002249 return SSL_ERROR_SYSCALL; /* unknown */
2250 }
2251 }
Adam Langley95c29f32014-06-20 12:00:00 -07002252
Adam Langleyfcf25832014-12-18 17:42:32 -08002253 if (SSL_want_write(s)) {
2254 bio = SSL_get_wbio(s);
2255 if (BIO_should_write(bio)) {
2256 return SSL_ERROR_WANT_WRITE;
2257 }
Adam Langley95c29f32014-06-20 12:00:00 -07002258
Adam Langleyfcf25832014-12-18 17:42:32 -08002259 if (BIO_should_read(bio)) {
2260 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2261 return SSL_ERROR_WANT_READ;
2262 }
Adam Langley95c29f32014-06-20 12:00:00 -07002263
Adam Langleyfcf25832014-12-18 17:42:32 -08002264 if (BIO_should_io_special(bio)) {
2265 reason = BIO_get_retry_reason(bio);
2266 if (reason == BIO_RR_CONNECT) {
2267 return SSL_ERROR_WANT_CONNECT;
2268 }
Adam Langley95c29f32014-06-20 12:00:00 -07002269
Adam Langleyfcf25832014-12-18 17:42:32 -08002270 if (reason == BIO_RR_ACCEPT) {
2271 return SSL_ERROR_WANT_ACCEPT;
2272 }
Adam Langley95c29f32014-06-20 12:00:00 -07002273
Adam Langleyfcf25832014-12-18 17:42:32 -08002274 return SSL_ERROR_SYSCALL;
2275 }
2276 }
Adam Langley95c29f32014-06-20 12:00:00 -07002277
Adam Langleyfcf25832014-12-18 17:42:32 -08002278 if (SSL_want_x509_lookup(s)) {
2279 return SSL_ERROR_WANT_X509_LOOKUP;
2280 }
Adam Langley95c29f32014-06-20 12:00:00 -07002281
Adam Langleyfcf25832014-12-18 17:42:32 -08002282 if (SSL_want_channel_id_lookup(s)) {
2283 return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
2284 }
Adam Langley0f4746e2014-08-13 12:26:32 -07002285
Adam Langleyfcf25832014-12-18 17:42:32 -08002286 return SSL_ERROR_SYSCALL;
2287}
Adam Langley0f4746e2014-08-13 12:26:32 -07002288
Adam Langleyfcf25832014-12-18 17:42:32 -08002289int SSL_do_handshake(SSL *s) {
2290 int ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002291
Adam Langleyfcf25832014-12-18 17:42:32 -08002292 if (s->handshake_func == NULL) {
2293 OPENSSL_PUT_ERROR(SSL, SSL_do_handshake, SSL_R_CONNECTION_TYPE_NOT_SET);
2294 return -1;
2295 }
Adam Langley95c29f32014-06-20 12:00:00 -07002296
Adam Langleyfcf25832014-12-18 17:42:32 -08002297 s->method->ssl_renegotiate_check(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002298
Adam Langleyfcf25832014-12-18 17:42:32 -08002299 if (SSL_in_init(s)) {
2300 ret = s->handshake_func(s);
2301 }
2302 return ret;
2303}
Adam Langley95c29f32014-06-20 12:00:00 -07002304
Adam Langleyfcf25832014-12-18 17:42:32 -08002305void SSL_set_accept_state(SSL *s) {
2306 s->server = 1;
2307 s->shutdown = 0;
2308 s->state = SSL_ST_ACCEPT | SSL_ST_BEFORE;
2309 s->handshake_func = s->method->ssl_accept;
2310 /* clear the current cipher */
2311 ssl_clear_cipher_ctx(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002312}
Adam Langley95c29f32014-06-20 12:00:00 -07002313
Adam Langleyfcf25832014-12-18 17:42:32 -08002314void SSL_set_connect_state(SSL *s) {
2315 s->server = 0;
2316 s->shutdown = 0;
2317 s->state = SSL_ST_CONNECT | SSL_ST_BEFORE;
2318 s->handshake_func = s->method->ssl_connect;
2319 /* clear the current cipher */
2320 ssl_clear_cipher_ctx(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002321}
Adam Langley95c29f32014-06-20 12:00:00 -07002322
Adam Langleyfcf25832014-12-18 17:42:32 -08002323int ssl_undefined_function(SSL *s) {
2324 OPENSSL_PUT_ERROR(SSL, ssl_undefined_function,
2325 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2326 return 0;
2327}
Adam Langley95c29f32014-06-20 12:00:00 -07002328
Adam Langleyfcf25832014-12-18 17:42:32 -08002329int ssl_undefined_void_function(void) {
2330 OPENSSL_PUT_ERROR(SSL, ssl_undefined_void_function,
2331 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2332 return 0;
2333}
Adam Langley95c29f32014-06-20 12:00:00 -07002334
Adam Langleyfcf25832014-12-18 17:42:32 -08002335int ssl_undefined_const_function(const SSL *s) {
2336 OPENSSL_PUT_ERROR(SSL, ssl_undefined_const_function,
2337 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2338 return 0;
2339}
Adam Langley95c29f32014-06-20 12:00:00 -07002340
Adam Langleyfcf25832014-12-18 17:42:32 -08002341static const char *ssl_get_version(int version) {
2342 switch (version) {
2343 case TLS1_2_VERSION:
2344 return "TLSv1.2";
Adam Langley95c29f32014-06-20 12:00:00 -07002345
Adam Langleyfcf25832014-12-18 17:42:32 -08002346 case TLS1_1_VERSION:
2347 return "TLSv1.1";
Adam Langley95c29f32014-06-20 12:00:00 -07002348
Adam Langleyfcf25832014-12-18 17:42:32 -08002349 case TLS1_VERSION:
2350 return "TLSv1";
Adam Langley95c29f32014-06-20 12:00:00 -07002351
Adam Langleyfcf25832014-12-18 17:42:32 -08002352 case SSL3_VERSION:
2353 return "SSLv3";
Adam Langley95c29f32014-06-20 12:00:00 -07002354
Adam Langleyfcf25832014-12-18 17:42:32 -08002355 default:
2356 return "unknown";
2357 }
2358}
Adam Langley95c29f32014-06-20 12:00:00 -07002359
Adam Langleyfcf25832014-12-18 17:42:32 -08002360const char *SSL_get_version(const SSL *s) {
2361 return ssl_get_version(s->version);
2362}
Adam Langley95c29f32014-06-20 12:00:00 -07002363
Adam Langleyfcf25832014-12-18 17:42:32 -08002364const char *SSL_SESSION_get_version(const SSL_SESSION *sess) {
2365 return ssl_get_version(sess->ssl_version);
2366}
Adam Langley95c29f32014-06-20 12:00:00 -07002367
Adam Langleyfcf25832014-12-18 17:42:32 -08002368void ssl_clear_cipher_ctx(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002369 if (s->aead_read_ctx != NULL) {
2370 EVP_AEAD_CTX_cleanup(&s->aead_read_ctx->ctx);
2371 OPENSSL_free(s->aead_read_ctx);
2372 s->aead_read_ctx = NULL;
2373 }
Adam Langleya5dc5452014-06-20 12:00:00 -07002374
Adam Langleyfcf25832014-12-18 17:42:32 -08002375 if (s->aead_write_ctx != NULL) {
2376 EVP_AEAD_CTX_cleanup(&s->aead_write_ctx->ctx);
2377 OPENSSL_free(s->aead_write_ctx);
2378 s->aead_write_ctx = NULL;
2379 }
2380}
Adam Langley95c29f32014-06-20 12:00:00 -07002381
Adam Langleyfcf25832014-12-18 17:42:32 -08002382X509 *SSL_get_certificate(const SSL *s) {
2383 if (s->cert != NULL) {
2384 return s->cert->key->x509;
2385 }
2386
2387 return NULL;
2388}
2389
2390EVP_PKEY *SSL_get_privatekey(const SSL *s) {
2391 if (s->cert != NULL) {
2392 return s->cert->key->privatekey;
2393 }
2394
2395 return NULL;
2396}
2397
2398X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
2399 if (ctx->cert != NULL) {
2400 return ctx->cert->key->x509;
2401 }
2402
2403 return NULL;
2404}
2405
2406EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
2407 if (ctx->cert != NULL) {
2408 return ctx->cert->key->privatekey;
2409 }
2410
2411 return NULL;
2412}
2413
2414const SSL_CIPHER *SSL_get_current_cipher(const SSL *s) {
2415 if (s->session != NULL && s->session->cipher != NULL) {
2416 return s->session->cipher;
2417 }
2418
2419 return NULL;
2420}
2421
2422const void *SSL_get_current_compression(SSL *s) { return NULL; }
2423
2424const void *SSL_get_current_expansion(SSL *s) { return NULL; }
2425
2426int ssl_init_wbio_buffer(SSL *s, int push) {
2427 BIO *bbio;
2428
2429 if (s->bbio == NULL) {
2430 bbio = BIO_new(BIO_f_buffer());
2431 if (bbio == NULL) {
2432 return 0;
2433 }
2434 s->bbio = bbio;
2435 } else {
2436 bbio = s->bbio;
2437 if (s->bbio == s->wbio) {
2438 s->wbio = BIO_pop(s->wbio);
2439 }
2440 }
2441
2442 BIO_reset(bbio);
2443 if (!BIO_set_read_buffer_size(bbio, 1)) {
2444 OPENSSL_PUT_ERROR(SSL, ssl_init_wbio_buffer, ERR_R_BUF_LIB);
2445 return 0;
2446 }
2447
2448 if (push) {
2449 if (s->wbio != bbio) {
2450 s->wbio = BIO_push(bbio, s->wbio);
2451 }
2452 } else {
2453 if (s->wbio == bbio) {
2454 s->wbio = BIO_pop(bbio);
2455 }
2456 }
2457
2458 return 1;
2459}
2460
2461void ssl_free_wbio_buffer(SSL *s) {
2462 if (s->bbio == NULL) {
2463 return;
2464 }
2465
2466 if (s->bbio == s->wbio) {
2467 /* remove buffering */
2468 s->wbio = BIO_pop(s->wbio);
2469 }
2470
2471 BIO_free(s->bbio);
2472 s->bbio = NULL;
2473}
2474
2475void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
2476 ctx->quiet_shutdown = mode;
2477}
2478
2479int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
2480 return ctx->quiet_shutdown;
2481}
2482
2483void SSL_set_quiet_shutdown(SSL *s, int mode) { s->quiet_shutdown = mode; }
2484
2485int SSL_get_quiet_shutdown(const SSL *s) { return s->quiet_shutdown; }
2486
2487void SSL_set_shutdown(SSL *s, int mode) { s->shutdown = mode; }
2488
2489int SSL_get_shutdown(const SSL *s) { return s->shutdown; }
2490
2491int SSL_version(const SSL *s) { return s->version; }
2492
2493SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
2494
2495SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
2496 if (ssl->ctx == ctx) {
2497 return ssl->ctx;
2498 }
2499
2500 if (ctx == NULL) {
2501 ctx = ssl->initial_ctx;
2502 }
2503
2504 if (ssl->cert != NULL) {
2505 ssl_cert_free(ssl->cert);
2506 }
2507
2508 ssl->cert = ssl_cert_dup(ctx->cert);
2509 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
2510 if (ssl->ctx != NULL) {
2511 SSL_CTX_free(ssl->ctx); /* decrement reference count */
2512 }
2513 ssl->ctx = ctx;
2514
2515 ssl->sid_ctx_length = ctx->sid_ctx_length;
2516 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2517 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2518
2519 return ssl->ctx;
2520}
2521
2522int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
2523 return X509_STORE_set_default_paths(ctx->cert_store);
2524}
Adam Langley95c29f32014-06-20 12:00:00 -07002525
2526int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
Adam Langleyfcf25832014-12-18 17:42:32 -08002527 const char *CApath) {
2528 return X509_STORE_load_locations(ctx->cert_store, CAfile, CApath);
2529}
Adam Langley95c29f32014-06-20 12:00:00 -07002530
2531void SSL_set_info_callback(SSL *ssl,
Adam Langleyfcf25832014-12-18 17:42:32 -08002532 void (*cb)(const SSL *ssl, int type, int val)) {
2533 ssl->info_callback = cb;
2534}
Adam Langley95c29f32014-06-20 12:00:00 -07002535
Adam Langleyfcf25832014-12-18 17:42:32 -08002536void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/, int /*type*/,
2537 int /*val*/) {
2538 return ssl->info_callback;
2539}
Adam Langley95c29f32014-06-20 12:00:00 -07002540
Adam Langleyfcf25832014-12-18 17:42:32 -08002541int SSL_state(const SSL *ssl) { return ssl->state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002542
Adam Langleyfcf25832014-12-18 17:42:32 -08002543void SSL_set_state(SSL *ssl, int state) { ssl->state = state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002544
Adam Langleyfcf25832014-12-18 17:42:32 -08002545void SSL_set_verify_result(SSL *ssl, long arg) { ssl->verify_result = arg; }
Adam Langley95c29f32014-06-20 12:00:00 -07002546
Adam Langleyfcf25832014-12-18 17:42:32 -08002547long SSL_get_verify_result(const SSL *ssl) { return ssl->verify_result; }
Adam Langley95c29f32014-06-20 12:00:00 -07002548
Adam Langleyfcf25832014-12-18 17:42:32 -08002549int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2550 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
2551 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp, new_func,
2552 dup_func, free_func);
2553}
Adam Langley95c29f32014-06-20 12:00:00 -07002554
Adam Langleyfcf25832014-12-18 17:42:32 -08002555int SSL_set_ex_data(SSL *s, int idx, void *arg) {
2556 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2557}
Adam Langley95c29f32014-06-20 12:00:00 -07002558
Adam Langleyfcf25832014-12-18 17:42:32 -08002559void *SSL_get_ex_data(const SSL *s, int idx) {
2560 return CRYPTO_get_ex_data(&s->ex_data, idx);
2561}
Adam Langley95c29f32014-06-20 12:00:00 -07002562
Adam Langleyfcf25832014-12-18 17:42:32 -08002563int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2564 CRYPTO_EX_dup *dup_func,
2565 CRYPTO_EX_free *free_func) {
2566 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp, new_func,
2567 dup_func, free_func);
2568}
Adam Langley95c29f32014-06-20 12:00:00 -07002569
Adam Langleyfcf25832014-12-18 17:42:32 -08002570int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg) {
2571 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2572}
Adam Langley95c29f32014-06-20 12:00:00 -07002573
Adam Langleyfcf25832014-12-18 17:42:32 -08002574void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx) {
2575 return CRYPTO_get_ex_data(&s->ex_data, idx);
2576}
Adam Langley95c29f32014-06-20 12:00:00 -07002577
Adam Langleyfcf25832014-12-18 17:42:32 -08002578int ssl_ok(SSL *s) { return 1; }
Adam Langley95c29f32014-06-20 12:00:00 -07002579
Adam Langleyfcf25832014-12-18 17:42:32 -08002580X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
2581 return ctx->cert_store;
2582}
Adam Langley95c29f32014-06-20 12:00:00 -07002583
Adam Langleyfcf25832014-12-18 17:42:32 -08002584void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
2585 if (ctx->cert_store != NULL) {
2586 X509_STORE_free(ctx->cert_store);
2587 }
2588 ctx->cert_store = store;
2589}
Adam Langley95c29f32014-06-20 12:00:00 -07002590
Adam Langleyfcf25832014-12-18 17:42:32 -08002591int SSL_want(const SSL *s) { return s->rwstate; }
Adam Langley95c29f32014-06-20 12:00:00 -07002592
Adam Langleyfcf25832014-12-18 17:42:32 -08002593void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
2594 RSA *(*cb)(SSL *ssl, int is_export,
2595 int keylength)) {
2596 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_RSA_CB, (void (*)(void))cb);
2597}
Adam Langley95c29f32014-06-20 12:00:00 -07002598
Adam Langleyfcf25832014-12-18 17:42:32 -08002599void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
2600 int keylength)) {
2601 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_RSA_CB, (void (*)(void))cb);
2602}
2603
2604void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
2605 DH *(*dh)(SSL *ssl, int is_export,
2606 int keylength)) {
2607 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh);
2608}
2609
2610void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*dh)(SSL *ssl, int is_export,
2611 int keylength)) {
2612 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh);
2613}
2614
2615void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,
2616 EC_KEY *(*ecdh)(SSL *ssl, int is_export,
2617 int keylength)) {
2618 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH_CB, (void (*)(void))ecdh);
2619}
2620
2621void SSL_set_tmp_ecdh_callback(SSL *ssl,
2622 EC_KEY *(*ecdh)(SSL *ssl, int is_export,
2623 int keylength)) {
2624 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_ECDH_CB, (void (*)(void))ecdh);
2625}
2626
2627int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
2628 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2629 OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_psk_identity_hint,
2630 SSL_R_DATA_LENGTH_TOO_LONG);
2631 return 0;
2632 }
2633
2634 if (ctx->psk_identity_hint != NULL) {
2635 OPENSSL_free(ctx->psk_identity_hint);
2636 }
2637
2638 if (identity_hint != NULL) {
2639 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2640 if (ctx->psk_identity_hint == NULL) {
2641 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002642 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002643 } else {
2644 ctx->psk_identity_hint = NULL;
2645 }
Adam Langley95c29f32014-06-20 12:00:00 -07002646
Adam Langleyfcf25832014-12-18 17:42:32 -08002647 return 1;
2648}
2649
2650int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint) {
2651 if (s == NULL) {
2652 return 0;
2653 }
2654
2655 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2656 OPENSSL_PUT_ERROR(SSL, SSL_use_psk_identity_hint,
2657 SSL_R_DATA_LENGTH_TOO_LONG);
2658 return 0;
2659 }
2660
2661 /* Clear currently configured hint, if any. */
2662 if (s->psk_identity_hint != NULL) {
2663 OPENSSL_free(s->psk_identity_hint);
2664 s->psk_identity_hint = NULL;
2665 }
2666
2667 if (identity_hint != NULL) {
2668 s->psk_identity_hint = BUF_strdup(identity_hint);
2669 if (s->psk_identity_hint == NULL) {
2670 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002671 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002672 }
Adam Langley95c29f32014-06-20 12:00:00 -07002673
Adam Langleyfcf25832014-12-18 17:42:32 -08002674 return 1;
2675}
Adam Langley95c29f32014-06-20 12:00:00 -07002676
Adam Langleyfcf25832014-12-18 17:42:32 -08002677const char *SSL_get_psk_identity_hint(const SSL *s) {
2678 if (s == NULL) {
2679 return NULL;
2680 }
2681 return s->psk_identity_hint;
2682}
Adam Langley95c29f32014-06-20 12:00:00 -07002683
Adam Langleyfcf25832014-12-18 17:42:32 -08002684const char *SSL_get_psk_identity(const SSL *s) {
2685 if (s == NULL || s->session == NULL) {
2686 return NULL;
2687 }
Adam Langley95c29f32014-06-20 12:00:00 -07002688
Adam Langleyfcf25832014-12-18 17:42:32 -08002689 return s->session->psk_identity;
2690}
Adam Langley95c29f32014-06-20 12:00:00 -07002691
Adam Langleyfcf25832014-12-18 17:42:32 -08002692void SSL_set_psk_client_callback(
2693 SSL *s, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2694 unsigned int max_identity_len, uint8_t *psk,
2695 unsigned int max_psk_len)) {
2696 s->psk_client_callback = cb;
2697}
Adam Langley95c29f32014-06-20 12:00:00 -07002698
Adam Langleyfcf25832014-12-18 17:42:32 -08002699void SSL_CTX_set_psk_client_callback(
2700 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2701 unsigned int max_identity_len,
2702 uint8_t *psk, unsigned int max_psk_len)) {
2703 ctx->psk_client_callback = cb;
2704}
Adam Langley95c29f32014-06-20 12:00:00 -07002705
Adam Langleyfcf25832014-12-18 17:42:32 -08002706void SSL_set_psk_server_callback(
2707 SSL *s, unsigned int (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
2708 unsigned int max_psk_len)) {
2709 s->psk_server_callback = cb;
2710}
Adam Langley95c29f32014-06-20 12:00:00 -07002711
Adam Langleyfcf25832014-12-18 17:42:32 -08002712void SSL_CTX_set_psk_server_callback(
2713 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *identity,
2714 uint8_t *psk, unsigned int max_psk_len)) {
2715 ctx->psk_server_callback = cb;
2716}
Adam Langley95c29f32014-06-20 12:00:00 -07002717
Adam Langleyfcf25832014-12-18 17:42:32 -08002718void SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) {
2719 ctx->min_version = version;
2720}
Adam Langley95c29f32014-06-20 12:00:00 -07002721
Adam Langleyfcf25832014-12-18 17:42:32 -08002722void SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) {
2723 ctx->max_version = version;
2724}
Adam Langley0289c732014-06-20 12:00:00 -07002725
Adam Langleyfcf25832014-12-18 17:42:32 -08002726void SSL_set_min_version(SSL *ssl, uint16_t version) {
2727 ssl->min_version = version;
2728}
Adam Langley0289c732014-06-20 12:00:00 -07002729
Adam Langleyfcf25832014-12-18 17:42:32 -08002730void SSL_set_max_version(SSL *ssl, uint16_t version) {
2731 ssl->max_version = version;
2732}
Adam Langley95c29f32014-06-20 12:00:00 -07002733
Adam Langleyfcf25832014-12-18 17:42:32 -08002734void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
2735 void (*cb)(int write_p, int version,
2736 int content_type, const void *buf,
2737 size_t len, SSL *ssl, void *arg)) {
2738 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2739}
2740void SSL_set_msg_callback(SSL *ssl,
2741 void (*cb)(int write_p, int version, int content_type,
2742 const void *buf, size_t len, SSL *ssl,
2743 void *arg)) {
2744 SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2745}
Adam Langley95c29f32014-06-20 12:00:00 -07002746
Adam Langleyfcf25832014-12-18 17:42:32 -08002747void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio) {
2748 if (ctx->keylog_bio != NULL) {
2749 BIO_free(ctx->keylog_bio);
2750 }
2751 ctx->keylog_bio = keylog_bio;
2752}
Adam Langley95c29f32014-06-20 12:00:00 -07002753
Adam Langleyfcf25832014-12-18 17:42:32 -08002754static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
2755 static const char hextable[] = "0123456789abcdef";
2756 uint8_t *out;
2757 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002758
Adam Langleyfcf25832014-12-18 17:42:32 -08002759 if (!CBB_add_space(cbb, &out, in_len * 2)) {
2760 return 0;
2761 }
Adam Langley95c29f32014-06-20 12:00:00 -07002762
Adam Langleyfcf25832014-12-18 17:42:32 -08002763 for (i = 0; i < in_len; i++) {
2764 *(out++) = (uint8_t)hextable[in[i] >> 4];
2765 *(out++) = (uint8_t)hextable[in[i] & 0xf];
2766 }
Adam Langley95c29f32014-06-20 12:00:00 -07002767
Adam Langleyfcf25832014-12-18 17:42:32 -08002768 return 1;
2769}
David Benjamin859ec3c2014-09-02 16:29:36 -04002770
2771int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -08002772 const uint8_t *encrypted_premaster,
2773 size_t encrypted_premaster_len,
2774 const uint8_t *premaster,
2775 size_t premaster_len) {
2776 BIO *bio = ctx->keylog_bio;
2777 CBB cbb;
2778 uint8_t *out;
2779 size_t out_len;
2780 int ret;
David Benjamin859ec3c2014-09-02 16:29:36 -04002781
Adam Langleyfcf25832014-12-18 17:42:32 -08002782 if (bio == NULL) {
2783 return 1;
2784 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002785
Adam Langleyfcf25832014-12-18 17:42:32 -08002786 if (encrypted_premaster_len < 8) {
2787 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_rsa_client_key_exchange,
2788 ERR_R_INTERNAL_ERROR);
2789 return 0;
2790 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002791
Adam Langleyfcf25832014-12-18 17:42:32 -08002792 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len * 2 + 1)) {
2793 return 0;
2794 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002795
Adam Langleyfcf25832014-12-18 17:42:32 -08002796 if (!CBB_add_bytes(&cbb, (const uint8_t *)"RSA ", 4) ||
2797 /* Only the first 8 bytes of the encrypted premaster secret are
2798 * logged. */
2799 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
2800 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2801 !cbb_add_hex(&cbb, premaster, premaster_len) ||
2802 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2803 !CBB_finish(&cbb, &out, &out_len)) {
2804 CBB_cleanup(&cbb);
2805 return 0;
2806 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002807
Adam Langleyfcf25832014-12-18 17:42:32 -08002808 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2809 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2810 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
David Benjamin859ec3c2014-09-02 16:29:36 -04002811
Adam Langleyfcf25832014-12-18 17:42:32 -08002812 OPENSSL_free(out);
2813 return ret;
Adam Langley95f22882014-06-20 12:00:00 -07002814}
2815
Adam Langleyfcf25832014-12-18 17:42:32 -08002816int ssl_ctx_log_master_secret(SSL_CTX *ctx, const uint8_t *client_random,
2817 size_t client_random_len, const uint8_t *master,
2818 size_t master_len) {
2819 BIO *bio = ctx->keylog_bio;
2820 CBB cbb;
2821 uint8_t *out;
2822 size_t out_len;
2823 int ret;
Adam Langleyadb739e2014-06-20 12:00:00 -07002824
Adam Langleyfcf25832014-12-18 17:42:32 -08002825 if (bio == NULL) {
2826 return 1;
2827 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002828
Adam Langleyfcf25832014-12-18 17:42:32 -08002829 if (client_random_len != 32) {
2830 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_master_secret, ERR_R_INTERNAL_ERROR);
2831 return 0;
2832 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002833
Adam Langleyfcf25832014-12-18 17:42:32 -08002834 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len * 2 + 1)) {
2835 return 0;
2836 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002837
Adam Langleyfcf25832014-12-18 17:42:32 -08002838 if (!CBB_add_bytes(&cbb, (const uint8_t *)"CLIENT_RANDOM ", 14) ||
2839 !cbb_add_hex(&cbb, client_random, 32) ||
2840 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2841 !cbb_add_hex(&cbb, master, master_len) ||
2842 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2843 !CBB_finish(&cbb, &out, &out_len)) {
2844 CBB_cleanup(&cbb);
2845 return 0;
2846 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002847
Adam Langleyfcf25832014-12-18 17:42:32 -08002848 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2849 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2850 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
David Benjamine99e9122014-12-11 01:46:01 -05002851
Adam Langleyfcf25832014-12-18 17:42:32 -08002852 OPENSSL_free(out);
2853 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -07002854}
2855
David Benjamined7c4752015-02-16 19:16:46 -05002856int SSL_in_false_start(const SSL *s) {
2857 return s->s3->tmp.in_false_start;
2858}
2859
Adam Langleyfcf25832014-12-18 17:42:32 -08002860int SSL_cutthrough_complete(const SSL *s) {
David Benjamined7c4752015-02-16 19:16:46 -05002861 return SSL_in_false_start(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002862}
Adam Langley95c29f32014-06-20 12:00:00 -07002863
Adam Langleyfcf25832014-12-18 17:42:32 -08002864void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
2865 size_t *ssl_session_size) {
2866 *ssl_size = sizeof(SSL);
2867 *ssl_ctx_size = sizeof(SSL_CTX);
2868 *ssl_session_size = sizeof(SSL_SESSION);
2869}
Feng Lu41aa3252014-11-21 22:47:56 -08002870
David Benjamined7c4752015-02-16 19:16:46 -05002871int ssl3_can_false_start(const SSL *s) {
David Benjamin195dc782015-02-19 13:27:05 -05002872 const SSL_CIPHER *const cipher = SSL_get_current_cipher(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002873
Adam Langleye631d962015-04-01 13:11:01 -07002874 /* False Start only for TLS 1.2 with an ECDHE+AEAD cipher and ALPN or NPN. */
David Benjamin195dc782015-02-19 13:27:05 -05002875 return !SSL_IS_DTLS(s) &&
2876 SSL_version(s) >= TLS1_2_VERSION &&
2877 (s->s3->alpn_selected || s->s3->next_proto_neg_seen) &&
2878 cipher != NULL &&
Adam Langleye631d962015-04-01 13:11:01 -07002879 cipher->algorithm_mkey == SSL_kECDHE &&
David Benjamin195dc782015-02-19 13:27:05 -05002880 (cipher->algorithm_enc == SSL_AES128GCM ||
2881 cipher->algorithm_enc == SSL_AES256GCM ||
2882 cipher->algorithm_enc == SSL_CHACHA20POLY1305);
Adam Langleyfcf25832014-12-18 17:42:32 -08002883}
2884
2885const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version) {
2886 switch (version) {
2887 case SSL3_VERSION:
2888 return &SSLv3_enc_data;
2889
2890 case TLS1_VERSION:
2891 return &TLSv1_enc_data;
2892
David Benjamin9e13e1a2015-03-05 01:56:32 -05002893 case DTLS1_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002894 case TLS1_1_VERSION:
2895 return &TLSv1_1_enc_data;
2896
David Benjamin9e13e1a2015-03-05 01:56:32 -05002897 case DTLS1_2_VERSION:
Adam Langleyfcf25832014-12-18 17:42:32 -08002898 case TLS1_2_VERSION:
2899 return &TLSv1_2_enc_data;
2900
Adam Langleyfcf25832014-12-18 17:42:32 -08002901 default:
2902 return NULL;
2903 }
2904}
2905
2906uint16_t ssl3_get_max_server_version(const SSL *s) {
2907 uint16_t max_version;
2908
2909 if (SSL_IS_DTLS(s)) {
2910 max_version = (s->max_version != 0) ? s->max_version : DTLS1_2_VERSION;
2911 if (!(s->options & SSL_OP_NO_DTLSv1_2) && DTLS1_2_VERSION >= max_version) {
2912 return DTLS1_2_VERSION;
2913 }
2914 if (!(s->options & SSL_OP_NO_DTLSv1) && DTLS1_VERSION >= max_version) {
2915 return DTLS1_VERSION;
2916 }
2917 return 0;
2918 }
2919
2920 max_version = (s->max_version != 0) ? s->max_version : TLS1_2_VERSION;
2921 if (!(s->options & SSL_OP_NO_TLSv1_2) && TLS1_2_VERSION <= max_version) {
2922 return TLS1_2_VERSION;
2923 }
2924 if (!(s->options & SSL_OP_NO_TLSv1_1) && TLS1_1_VERSION <= max_version) {
2925 return TLS1_1_VERSION;
2926 }
2927 if (!(s->options & SSL_OP_NO_TLSv1) && TLS1_VERSION <= max_version) {
2928 return TLS1_VERSION;
2929 }
2930 if (!(s->options & SSL_OP_NO_SSLv3) && SSL3_VERSION <= max_version) {
2931 return SSL3_VERSION;
2932 }
2933 return 0;
2934}
2935
2936uint16_t ssl3_get_mutual_version(SSL *s, uint16_t client_version) {
2937 uint16_t version = 0;
2938
2939 if (SSL_IS_DTLS(s)) {
2940 /* Clamp client_version to max_version. */
2941 if (s->max_version != 0 && client_version < s->max_version) {
2942 client_version = s->max_version;
2943 }
2944
2945 if (client_version <= DTLS1_2_VERSION && !(s->options & SSL_OP_NO_DTLSv1_2)) {
2946 version = DTLS1_2_VERSION;
2947 } else if (client_version <= DTLS1_VERSION &&
David Benjaminb18f0242015-03-10 18:30:08 -04002948 !(s->options & SSL_OP_NO_DTLSv1)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002949 version = DTLS1_VERSION;
2950 }
2951
2952 /* Check against min_version. */
2953 if (version != 0 && s->min_version != 0 && version > s->min_version) {
2954 return 0;
2955 }
2956 return version;
2957 } else {
2958 /* Clamp client_version to max_version. */
2959 if (s->max_version != 0 && client_version > s->max_version) {
2960 client_version = s->max_version;
2961 }
2962
2963 if (client_version >= TLS1_2_VERSION && !(s->options & SSL_OP_NO_TLSv1_2)) {
2964 version = TLS1_2_VERSION;
2965 } else if (client_version >= TLS1_1_VERSION &&
2966 !(s->options & SSL_OP_NO_TLSv1_1)) {
2967 version = TLS1_1_VERSION;
2968 } else if (client_version >= TLS1_VERSION && !(s->options & SSL_OP_NO_TLSv1)) {
2969 version = TLS1_VERSION;
2970 } else if (client_version >= SSL3_VERSION && !(s->options & SSL_OP_NO_SSLv3)) {
2971 version = SSL3_VERSION;
2972 }
2973
2974 /* Check against min_version. */
2975 if (version != 0 && s->min_version != 0 && version < s->min_version) {
2976 return 0;
2977 }
2978 return version;
2979 }
2980}
2981
2982uint16_t ssl3_get_max_client_version(SSL *s) {
2983 unsigned long options = s->options;
2984 uint16_t version = 0;
2985
2986 /* OpenSSL's API for controlling versions entails blacklisting individual
2987 * protocols. This has two problems. First, on the client, the protocol can
2988 * only express a contiguous range of versions. Second, a library consumer
2989 * trying to set a maximum version cannot disable protocol versions that get
2990 * added in a future version of the library.
2991 *
2992 * To account for both of these, OpenSSL interprets the client-side bitmask
2993 * as a min/max range by picking the lowest contiguous non-empty range of
2994 * enabled protocols. Note that this means it is impossible to set a maximum
2995 * version of TLS 1.2 in a future-proof way.
2996 *
2997 * By this scheme, the maximum version is the lowest version V such that V is
2998 * enabled and V+1 is disabled or unimplemented. */
2999 if (SSL_IS_DTLS(s)) {
3000 if (!(options & SSL_OP_NO_DTLSv1_2)) {
3001 version = DTLS1_2_VERSION;
3002 }
3003 if (!(options & SSL_OP_NO_DTLSv1) && (options & SSL_OP_NO_DTLSv1_2)) {
3004 version = DTLS1_VERSION;
3005 }
3006 if (s->max_version != 0 && version < s->max_version) {
3007 version = s->max_version;
3008 }
3009 } else {
3010 if (!(options & SSL_OP_NO_TLSv1_2)) {
3011 version = TLS1_2_VERSION;
3012 }
3013 if (!(options & SSL_OP_NO_TLSv1_1) && (options & SSL_OP_NO_TLSv1_2)) {
3014 version = TLS1_1_VERSION;
3015 }
3016 if (!(options & SSL_OP_NO_TLSv1) && (options & SSL_OP_NO_TLSv1_1)) {
3017 version = TLS1_VERSION;
3018 }
3019 if (!(options & SSL_OP_NO_SSLv3) && (options & SSL_OP_NO_TLSv1)) {
3020 version = SSL3_VERSION;
3021 }
3022 if (s->max_version != 0 && version > s->max_version) {
3023 version = s->max_version;
3024 }
3025 }
3026
3027 return version;
3028}
3029
3030int ssl3_is_version_enabled(SSL *s, uint16_t version) {
3031 if (SSL_IS_DTLS(s)) {
3032 if (s->max_version != 0 && version < s->max_version) {
3033 return 0;
3034 }
3035 if (s->min_version != 0 && version > s->min_version) {
3036 return 0;
3037 }
3038
3039 switch (version) {
3040 case DTLS1_VERSION:
3041 return !(s->options & SSL_OP_NO_DTLSv1);
3042
3043 case DTLS1_2_VERSION:
3044 return !(s->options & SSL_OP_NO_DTLSv1_2);
3045
3046 default:
3047 return 0;
3048 }
3049 } else {
3050 if (s->max_version != 0 && version > s->max_version) {
3051 return 0;
3052 }
3053 if (s->min_version != 0 && version < s->min_version) {
3054 return 0;
3055 }
3056
3057 switch (version) {
3058 case SSL3_VERSION:
3059 return !(s->options & SSL_OP_NO_SSLv3);
3060
3061 case TLS1_VERSION:
3062 return !(s->options & SSL_OP_NO_TLSv1);
3063
3064 case TLS1_1_VERSION:
3065 return !(s->options & SSL_OP_NO_TLSv1_1);
3066
3067 case TLS1_2_VERSION:
3068 return !(s->options & SSL_OP_NO_TLSv1_2);
3069
3070 default:
3071 return 0;
3072 }
3073 }
3074}
3075
David Benjaminea72bd02014-12-21 21:27:41 -05003076uint16_t ssl3_version_from_wire(SSL *s, uint16_t wire_version) {
3077 if (!SSL_IS_DTLS(s)) {
3078 return wire_version;
3079 }
3080
3081 uint16_t tls_version = ~wire_version;
3082 uint16_t version = tls_version + 0x0201;
3083 /* If either component overflowed, clamp it so comparisons still work. */
3084 if ((version >> 8) < (tls_version >> 8)) {
3085 version = 0xff00 | (version & 0xff);
3086 }
3087 if ((version & 0xff) < (tls_version & 0xff)) {
3088 version = (version & 0xff00) | 0xff;
3089 }
3090 /* DTLS 1.0 maps to TLS 1.1, not TLS 1.0. */
3091 if (version == TLS1_VERSION) {
3092 version = TLS1_1_VERSION;
3093 }
3094 return version;
3095}
3096
Adam Langleyfcf25832014-12-18 17:42:32 -08003097int SSL_cache_hit(SSL *s) { return s->hit; }
3098
3099int SSL_is_server(SSL *s) { return s->server; }
3100
Adam Langley524e7172015-02-20 16:04:00 -08003101void SSL_CTX_set_dos_protection_cb(
3102 SSL_CTX *ctx, int (*cb)(const struct ssl_early_callback_ctx *)) {
3103 ctx->dos_protection_cb = cb;
3104}
3105
Adam Langleyfcf25832014-12-18 17:42:32 -08003106void SSL_enable_fastradio_padding(SSL *s, char on_off) {
3107 s->fastradio_padding = on_off;
3108}
Adam Langley44972942015-02-20 13:43:23 -08003109
David Benjaminb16346b2015-04-08 19:16:58 -04003110void SSL_set_reject_peer_renegotiations(SSL *s, int reject) {
3111 s->reject_peer_renegotiations = !!reject;
3112}
3113
Adam Langley44972942015-02-20 13:43:23 -08003114const SSL_CIPHER *SSL_get_cipher_by_value(uint16_t value) {
3115 return ssl3_get_cipher_by_value(value);
3116}
Adam Langley3f92d212015-02-20 15:32:52 -08003117
3118int SSL_get_rc4_state(const SSL *ssl, const RC4_KEY **read_key,
3119 const RC4_KEY **write_key) {
3120 if (ssl->aead_read_ctx == NULL || ssl->aead_write_ctx == NULL) {
3121 return 0;
3122 }
3123
3124 return EVP_AEAD_CTX_get_rc4_state(&ssl->aead_read_ctx->ctx, read_key) &&
3125 EVP_AEAD_CTX_get_rc4_state(&ssl->aead_write_ctx->ctx, write_key);
3126}